rk_spi.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * spi driver for rockchip
  3. *
  4. * (C) Copyright 2015 Google, Inc
  5. *
  6. * (C) Copyright 2008-2013 Rockchip Electronics
  7. * Peter, Software Engineering, <superpeter.cai@gmail.com>.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <clk.h>
  13. #include <dm.h>
  14. #include <errno.h>
  15. #include <spi.h>
  16. #include <linux/errno.h>
  17. #include <asm/io.h>
  18. #include <asm/arch/clock.h>
  19. #include <asm/arch/periph.h>
  20. #include <dm/pinctrl.h>
  21. #include "rk_spi.h"
  22. DECLARE_GLOBAL_DATA_PTR;
  23. /* Change to 1 to output registers at the start of each transaction */
  24. #define DEBUG_RK_SPI 0
  25. struct rockchip_spi_platdata {
  26. s32 frequency; /* Default clock frequency, -1 for none */
  27. fdt_addr_t base;
  28. uint deactivate_delay_us; /* Delay to wait after deactivate */
  29. uint activate_delay_us; /* Delay to wait after activate */
  30. };
  31. struct rockchip_spi_priv {
  32. struct rockchip_spi *regs;
  33. struct clk clk;
  34. unsigned int max_freq;
  35. unsigned int mode;
  36. ulong last_transaction_us; /* Time of last transaction end */
  37. u8 bits_per_word; /* max 16 bits per word */
  38. u8 n_bytes;
  39. unsigned int speed_hz;
  40. unsigned int last_speed_hz;
  41. unsigned int tmode;
  42. uint input_rate;
  43. };
  44. #define SPI_FIFO_DEPTH 32
  45. static void rkspi_dump_regs(struct rockchip_spi *regs)
  46. {
  47. debug("ctrl0: \t\t0x%08x\n", readl(&regs->ctrlr0));
  48. debug("ctrl1: \t\t0x%08x\n", readl(&regs->ctrlr1));
  49. debug("ssienr: \t\t0x%08x\n", readl(&regs->enr));
  50. debug("ser: \t\t0x%08x\n", readl(&regs->ser));
  51. debug("baudr: \t\t0x%08x\n", readl(&regs->baudr));
  52. debug("txftlr: \t\t0x%08x\n", readl(&regs->txftlr));
  53. debug("rxftlr: \t\t0x%08x\n", readl(&regs->rxftlr));
  54. debug("txflr: \t\t0x%08x\n", readl(&regs->txflr));
  55. debug("rxflr: \t\t0x%08x\n", readl(&regs->rxflr));
  56. debug("sr: \t\t0x%08x\n", readl(&regs->sr));
  57. debug("imr: \t\t0x%08x\n", readl(&regs->imr));
  58. debug("isr: \t\t0x%08x\n", readl(&regs->isr));
  59. debug("dmacr: \t\t0x%08x\n", readl(&regs->dmacr));
  60. debug("dmatdlr: \t0x%08x\n", readl(&regs->dmatdlr));
  61. debug("dmardlr: \t0x%08x\n", readl(&regs->dmardlr));
  62. }
  63. static void rkspi_enable_chip(struct rockchip_spi *regs, bool enable)
  64. {
  65. writel(enable ? 1 : 0, &regs->enr);
  66. }
  67. static void rkspi_set_clk(struct rockchip_spi_priv *priv, uint speed)
  68. {
  69. uint clk_div;
  70. clk_div = clk_get_divisor(priv->input_rate, speed);
  71. debug("spi speed %u, div %u\n", speed, clk_div);
  72. writel(clk_div, &priv->regs->baudr);
  73. priv->last_speed_hz = speed;
  74. }
  75. static int rkspi_wait_till_not_busy(struct rockchip_spi *regs)
  76. {
  77. unsigned long start;
  78. start = get_timer(0);
  79. while (readl(&regs->sr) & SR_BUSY) {
  80. if (get_timer(start) > ROCKCHIP_SPI_TIMEOUT_MS) {
  81. debug("RK SPI: Status keeps busy for 1000us after a read/write!\n");
  82. return -ETIMEDOUT;
  83. }
  84. }
  85. return 0;
  86. }
  87. static void spi_cs_activate(struct udevice *dev, uint cs)
  88. {
  89. struct udevice *bus = dev->parent;
  90. struct rockchip_spi_platdata *plat = bus->platdata;
  91. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  92. struct rockchip_spi *regs = priv->regs;
  93. debug("activate cs%u\n", cs);
  94. writel(1 << cs, &regs->ser);
  95. if (plat->activate_delay_us)
  96. udelay(plat->activate_delay_us);
  97. }
  98. static void spi_cs_deactivate(struct udevice *dev, uint cs)
  99. {
  100. struct udevice *bus = dev->parent;
  101. struct rockchip_spi_platdata *plat = bus->platdata;
  102. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  103. struct rockchip_spi *regs = priv->regs;
  104. debug("deactivate cs%u\n", cs);
  105. writel(0, &regs->ser);
  106. /* Remember time of this transaction so we can honour the bus delay */
  107. if (plat->deactivate_delay_us)
  108. priv->last_transaction_us = timer_get_us();
  109. }
  110. static int rockchip_spi_ofdata_to_platdata(struct udevice *bus)
  111. {
  112. struct rockchip_spi_platdata *plat = bus->platdata;
  113. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  114. const void *blob = gd->fdt_blob;
  115. int node = bus->of_offset;
  116. int ret;
  117. plat->base = dev_get_addr(bus);
  118. ret = clk_get_by_index(bus, 0, &priv->clk);
  119. if (ret < 0) {
  120. debug("%s: Could not get clock for %s: %d\n", __func__,
  121. bus->name, ret);
  122. return ret;
  123. }
  124. plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  125. 50000000);
  126. plat->deactivate_delay_us = fdtdec_get_int(blob, node,
  127. "spi-deactivate-delay", 0);
  128. plat->activate_delay_us = fdtdec_get_int(blob, node,
  129. "spi-activate-delay", 0);
  130. debug("%s: base=%x, max-frequency=%d, deactivate_delay=%d\n",
  131. __func__, (uint)plat->base, plat->frequency,
  132. plat->deactivate_delay_us);
  133. return 0;
  134. }
  135. static int rockchip_spi_probe(struct udevice *bus)
  136. {
  137. struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
  138. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  139. int ret;
  140. debug("%s: probe\n", __func__);
  141. priv->regs = (struct rockchip_spi *)plat->base;
  142. priv->last_transaction_us = timer_get_us();
  143. priv->max_freq = plat->frequency;
  144. /*
  145. * Use 99 MHz as our clock since it divides nicely into 594 MHz which
  146. * is the assumed speed for CLK_GENERAL.
  147. */
  148. ret = clk_set_rate(&priv->clk, 99000000);
  149. if (ret < 0) {
  150. debug("%s: Failed to set clock: %d\n", __func__, ret);
  151. return ret;
  152. }
  153. priv->input_rate = ret;
  154. debug("%s: rate = %u\n", __func__, priv->input_rate);
  155. priv->bits_per_word = 8;
  156. priv->tmode = TMOD_TR; /* Tx & Rx */
  157. return 0;
  158. }
  159. static int rockchip_spi_claim_bus(struct udevice *dev)
  160. {
  161. struct udevice *bus = dev->parent;
  162. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  163. struct rockchip_spi *regs = priv->regs;
  164. u8 spi_dfs, spi_tf;
  165. uint ctrlr0;
  166. /* Disable the SPI hardware */
  167. rkspi_enable_chip(regs, 0);
  168. switch (priv->bits_per_word) {
  169. case 8:
  170. priv->n_bytes = 1;
  171. spi_dfs = DFS_8BIT;
  172. spi_tf = HALF_WORD_OFF;
  173. break;
  174. case 16:
  175. priv->n_bytes = 2;
  176. spi_dfs = DFS_16BIT;
  177. spi_tf = HALF_WORD_ON;
  178. break;
  179. default:
  180. debug("%s: unsupported bits: %dbits\n", __func__,
  181. priv->bits_per_word);
  182. return -EPROTONOSUPPORT;
  183. }
  184. if (priv->speed_hz != priv->last_speed_hz)
  185. rkspi_set_clk(priv, priv->speed_hz);
  186. /* Operation Mode */
  187. ctrlr0 = OMOD_MASTER << OMOD_SHIFT;
  188. /* Data Frame Size */
  189. ctrlr0 |= spi_dfs << DFS_SHIFT;
  190. /* set SPI mode 0..3 */
  191. if (priv->mode & SPI_CPOL)
  192. ctrlr0 |= SCOL_HIGH << SCOL_SHIFT;
  193. if (priv->mode & SPI_CPHA)
  194. ctrlr0 |= SCPH_TOGSTA << SCPH_SHIFT;
  195. /* Chip Select Mode */
  196. ctrlr0 |= CSM_KEEP << CSM_SHIFT;
  197. /* SSN to Sclk_out delay */
  198. ctrlr0 |= SSN_DELAY_ONE << SSN_DELAY_SHIFT;
  199. /* Serial Endian Mode */
  200. ctrlr0 |= SEM_LITTLE << SEM_SHIFT;
  201. /* First Bit Mode */
  202. ctrlr0 |= FBM_MSB << FBM_SHIFT;
  203. /* Byte and Halfword Transform */
  204. ctrlr0 |= spi_tf << HALF_WORD_TX_SHIFT;
  205. /* Rxd Sample Delay */
  206. ctrlr0 |= 0 << RXDSD_SHIFT;
  207. /* Frame Format */
  208. ctrlr0 |= FRF_SPI << FRF_SHIFT;
  209. /* Tx and Rx mode */
  210. ctrlr0 |= (priv->tmode & TMOD_MASK) << TMOD_SHIFT;
  211. writel(ctrlr0, &regs->ctrlr0);
  212. return 0;
  213. }
  214. static int rockchip_spi_release_bus(struct udevice *dev)
  215. {
  216. struct udevice *bus = dev->parent;
  217. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  218. rkspi_enable_chip(priv->regs, false);
  219. return 0;
  220. }
  221. static int rockchip_spi_xfer(struct udevice *dev, unsigned int bitlen,
  222. const void *dout, void *din, unsigned long flags)
  223. {
  224. struct udevice *bus = dev->parent;
  225. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  226. struct rockchip_spi *regs = priv->regs;
  227. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  228. int len = bitlen >> 3;
  229. const u8 *out = dout;
  230. u8 *in = din;
  231. int toread, towrite;
  232. int ret;
  233. debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din,
  234. len, flags);
  235. if (DEBUG_RK_SPI)
  236. rkspi_dump_regs(regs);
  237. /* Assert CS before transfer */
  238. if (flags & SPI_XFER_BEGIN)
  239. spi_cs_activate(dev, slave_plat->cs);
  240. while (len > 0) {
  241. int todo = min(len, 0xffff);
  242. rkspi_enable_chip(regs, false);
  243. writel(todo - 1, &regs->ctrlr1);
  244. rkspi_enable_chip(regs, true);
  245. toread = todo;
  246. towrite = todo;
  247. while (toread || towrite) {
  248. u32 status = readl(&regs->sr);
  249. if (towrite && !(status & SR_TF_FULL)) {
  250. writel(out ? *out++ : 0, regs->txdr);
  251. towrite--;
  252. }
  253. if (toread && !(status & SR_RF_EMPT)) {
  254. u32 byte = readl(regs->rxdr);
  255. if (in)
  256. *in++ = byte;
  257. toread--;
  258. }
  259. }
  260. ret = rkspi_wait_till_not_busy(regs);
  261. if (ret)
  262. break;
  263. len -= todo;
  264. }
  265. /* Deassert CS after transfer */
  266. if (flags & SPI_XFER_END)
  267. spi_cs_deactivate(dev, slave_plat->cs);
  268. rkspi_enable_chip(regs, false);
  269. return ret;
  270. }
  271. static int rockchip_spi_set_speed(struct udevice *bus, uint speed)
  272. {
  273. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  274. if (speed > ROCKCHIP_SPI_MAX_RATE)
  275. return -EINVAL;
  276. if (speed > priv->max_freq)
  277. speed = priv->max_freq;
  278. priv->speed_hz = speed;
  279. return 0;
  280. }
  281. static int rockchip_spi_set_mode(struct udevice *bus, uint mode)
  282. {
  283. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  284. priv->mode = mode;
  285. return 0;
  286. }
  287. static const struct dm_spi_ops rockchip_spi_ops = {
  288. .claim_bus = rockchip_spi_claim_bus,
  289. .release_bus = rockchip_spi_release_bus,
  290. .xfer = rockchip_spi_xfer,
  291. .set_speed = rockchip_spi_set_speed,
  292. .set_mode = rockchip_spi_set_mode,
  293. /*
  294. * cs_info is not needed, since we require all chip selects to be
  295. * in the device tree explicitly
  296. */
  297. };
  298. static const struct udevice_id rockchip_spi_ids[] = {
  299. { .compatible = "rockchip,rk3288-spi" },
  300. { }
  301. };
  302. U_BOOT_DRIVER(rockchip_spi) = {
  303. .name = "rockchip_spi",
  304. .id = UCLASS_SPI,
  305. .of_match = rockchip_spi_ids,
  306. .ops = &rockchip_spi_ops,
  307. .ofdata_to_platdata = rockchip_spi_ofdata_to_platdata,
  308. .platdata_auto_alloc_size = sizeof(struct rockchip_spi_platdata),
  309. .priv_auto_alloc_size = sizeof(struct rockchip_spi_priv),
  310. .probe = rockchip_spi_probe,
  311. };