rk_spi.c 10 KB

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