rk_spi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. /*
  74. * We should try not to exceed the speed requested by the caller:
  75. * when selecting a divider, we need to make sure we round up.
  76. */
  77. uint clk_div = DIV_ROUND_UP(priv->input_rate, speed);
  78. /* The baudrate register (BAUDR) is defined as a 32bit register where
  79. * the upper 16bit are reserved and having 'Fsclk_out' in the lower
  80. * 16bits with 'Fsclk_out' defined as follows:
  81. *
  82. * Fsclk_out = Fspi_clk/ SCKDV
  83. * Where SCKDV is any even value between 2 and 65534.
  84. */
  85. if (clk_div > 0xfffe) {
  86. clk_div = 0xfffe;
  87. debug("%s: can't divide down to %d hz (actual will be %d hz)\n",
  88. __func__, speed, priv->input_rate / clk_div);
  89. }
  90. /* Round up to the next even 16bit number */
  91. clk_div = (clk_div + 1) & 0xfffe;
  92. debug("spi speed %u, div %u\n", speed, clk_div);
  93. clrsetbits_le32(&priv->regs->baudr, 0xffff, clk_div);
  94. priv->last_speed_hz = speed;
  95. }
  96. static int rkspi_wait_till_not_busy(struct rockchip_spi *regs)
  97. {
  98. unsigned long start;
  99. start = get_timer(0);
  100. while (readl(&regs->sr) & SR_BUSY) {
  101. if (get_timer(start) > ROCKCHIP_SPI_TIMEOUT_MS) {
  102. debug("RK SPI: Status keeps busy for 1000us after a read/write!\n");
  103. return -ETIMEDOUT;
  104. }
  105. }
  106. return 0;
  107. }
  108. static void spi_cs_activate(struct udevice *dev, uint cs)
  109. {
  110. struct udevice *bus = dev->parent;
  111. struct rockchip_spi_platdata *plat = bus->platdata;
  112. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  113. struct rockchip_spi *regs = priv->regs;
  114. /* If it's too soon to do another transaction, wait */
  115. if (plat->deactivate_delay_us && priv->last_transaction_us) {
  116. ulong delay_us; /* The delay completed so far */
  117. delay_us = timer_get_us() - priv->last_transaction_us;
  118. if (delay_us < plat->deactivate_delay_us)
  119. udelay(plat->deactivate_delay_us - delay_us);
  120. }
  121. debug("activate cs%u\n", cs);
  122. writel(1 << cs, &regs->ser);
  123. if (plat->activate_delay_us)
  124. udelay(plat->activate_delay_us);
  125. }
  126. static void spi_cs_deactivate(struct udevice *dev, uint cs)
  127. {
  128. struct udevice *bus = dev->parent;
  129. struct rockchip_spi_platdata *plat = bus->platdata;
  130. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  131. struct rockchip_spi *regs = priv->regs;
  132. debug("deactivate cs%u\n", cs);
  133. writel(0, &regs->ser);
  134. /* Remember time of this transaction so we can honour the bus delay */
  135. if (plat->deactivate_delay_us)
  136. priv->last_transaction_us = timer_get_us();
  137. }
  138. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  139. static int conv_of_platdata(struct udevice *dev)
  140. {
  141. struct rockchip_spi_platdata *plat = dev->platdata;
  142. struct dtd_rockchip_rk3288_spi *dtplat = &plat->of_plat;
  143. struct rockchip_spi_priv *priv = dev_get_priv(dev);
  144. int ret;
  145. plat->base = dtplat->reg[0];
  146. plat->frequency = 20000000;
  147. ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->clk);
  148. if (ret < 0)
  149. return ret;
  150. dev->req_seq = 0;
  151. return 0;
  152. }
  153. #endif
  154. static int rockchip_spi_ofdata_to_platdata(struct udevice *bus)
  155. {
  156. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  157. struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
  158. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  159. int ret;
  160. plat->base = devfdt_get_addr(bus);
  161. ret = clk_get_by_index(bus, 0, &priv->clk);
  162. if (ret < 0) {
  163. debug("%s: Could not get clock for %s: %d\n", __func__,
  164. bus->name, ret);
  165. return ret;
  166. }
  167. plat->frequency =
  168. dev_read_u32_default(bus, "spi-max-frequency", 50000000);
  169. plat->deactivate_delay_us =
  170. dev_read_u32_default(bus, "spi-deactivate-delay", 0);
  171. plat->activate_delay_us =
  172. dev_read_u32_default(bus, "spi-activate-delay", 0);
  173. debug("%s: base=%x, max-frequency=%d, deactivate_delay=%d\n",
  174. __func__, (uint)plat->base, plat->frequency,
  175. plat->deactivate_delay_us);
  176. #endif
  177. return 0;
  178. }
  179. static int rockchip_spi_calc_modclk(ulong max_freq)
  180. {
  181. /*
  182. * While this is not strictly correct for the RK3368, as the
  183. * GPLL will be 576MHz, things will still work, as the
  184. * clk_set_rate(...) implementation in our clock-driver will
  185. * chose the next closest rate not exceeding what we request
  186. * based on the output of this function.
  187. */
  188. unsigned div;
  189. const unsigned long gpll_hz = 594000000UL;
  190. /*
  191. * We need to find an input clock that provides at least twice
  192. * the maximum frequency and can be generated from the assumed
  193. * speed of GPLL (594MHz) using an integer divider.
  194. *
  195. * To give us more achievable bitrates at higher speeds (these
  196. * are generated by dividing by an even 16-bit integer from
  197. * this frequency), we try to have an input frequency of at
  198. * least 4x our max_freq.
  199. */
  200. div = DIV_ROUND_UP(gpll_hz, max_freq * 4);
  201. return gpll_hz / div;
  202. }
  203. static int rockchip_spi_probe(struct udevice *bus)
  204. {
  205. struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
  206. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  207. int ret;
  208. debug("%s: probe\n", __func__);
  209. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  210. ret = conv_of_platdata(bus);
  211. if (ret)
  212. return ret;
  213. #endif
  214. priv->regs = (struct rockchip_spi *)plat->base;
  215. priv->last_transaction_us = timer_get_us();
  216. priv->max_freq = plat->frequency;
  217. /* Clamp the value from the DTS against any hardware limits */
  218. if (priv->max_freq > ROCKCHIP_SPI_MAX_RATE)
  219. priv->max_freq = ROCKCHIP_SPI_MAX_RATE;
  220. /* Find a module-input clock that fits with the max_freq setting */
  221. ret = clk_set_rate(&priv->clk,
  222. rockchip_spi_calc_modclk(priv->max_freq));
  223. if (ret < 0) {
  224. debug("%s: Failed to set clock: %d\n", __func__, ret);
  225. return ret;
  226. }
  227. priv->input_rate = ret;
  228. debug("%s: rate = %u\n", __func__, priv->input_rate);
  229. priv->bits_per_word = 8;
  230. priv->tmode = TMOD_TR; /* Tx & Rx */
  231. return 0;
  232. }
  233. static int rockchip_spi_claim_bus(struct udevice *dev)
  234. {
  235. struct udevice *bus = dev->parent;
  236. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  237. struct rockchip_spi *regs = priv->regs;
  238. u8 spi_dfs, spi_tf;
  239. uint ctrlr0;
  240. /* Disable the SPI hardware */
  241. rkspi_enable_chip(regs, 0);
  242. switch (priv->bits_per_word) {
  243. case 8:
  244. priv->n_bytes = 1;
  245. spi_dfs = DFS_8BIT;
  246. spi_tf = HALF_WORD_OFF;
  247. break;
  248. case 16:
  249. priv->n_bytes = 2;
  250. spi_dfs = DFS_16BIT;
  251. spi_tf = HALF_WORD_ON;
  252. break;
  253. default:
  254. debug("%s: unsupported bits: %dbits\n", __func__,
  255. priv->bits_per_word);
  256. return -EPROTONOSUPPORT;
  257. }
  258. if (priv->speed_hz != priv->last_speed_hz)
  259. rkspi_set_clk(priv, priv->speed_hz);
  260. /* Operation Mode */
  261. ctrlr0 = OMOD_MASTER << OMOD_SHIFT;
  262. /* Data Frame Size */
  263. ctrlr0 |= spi_dfs << DFS_SHIFT;
  264. /* set SPI mode 0..3 */
  265. if (priv->mode & SPI_CPOL)
  266. ctrlr0 |= SCOL_HIGH << SCOL_SHIFT;
  267. if (priv->mode & SPI_CPHA)
  268. ctrlr0 |= SCPH_TOGSTA << SCPH_SHIFT;
  269. /* Chip Select Mode */
  270. ctrlr0 |= CSM_KEEP << CSM_SHIFT;
  271. /* SSN to Sclk_out delay */
  272. ctrlr0 |= SSN_DELAY_ONE << SSN_DELAY_SHIFT;
  273. /* Serial Endian Mode */
  274. ctrlr0 |= SEM_LITTLE << SEM_SHIFT;
  275. /* First Bit Mode */
  276. ctrlr0 |= FBM_MSB << FBM_SHIFT;
  277. /* Byte and Halfword Transform */
  278. ctrlr0 |= spi_tf << HALF_WORD_TX_SHIFT;
  279. /* Rxd Sample Delay */
  280. ctrlr0 |= 0 << RXDSD_SHIFT;
  281. /* Frame Format */
  282. ctrlr0 |= FRF_SPI << FRF_SHIFT;
  283. /* Tx and Rx mode */
  284. ctrlr0 |= (priv->tmode & TMOD_MASK) << TMOD_SHIFT;
  285. writel(ctrlr0, &regs->ctrlr0);
  286. return 0;
  287. }
  288. static int rockchip_spi_release_bus(struct udevice *dev)
  289. {
  290. struct udevice *bus = dev->parent;
  291. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  292. rkspi_enable_chip(priv->regs, false);
  293. return 0;
  294. }
  295. static int rockchip_spi_xfer(struct udevice *dev, unsigned int bitlen,
  296. const void *dout, void *din, unsigned long flags)
  297. {
  298. struct udevice *bus = dev->parent;
  299. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  300. struct rockchip_spi *regs = priv->regs;
  301. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  302. int len = bitlen >> 3;
  303. const u8 *out = dout;
  304. u8 *in = din;
  305. int toread, towrite;
  306. int ret;
  307. debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din,
  308. len, flags);
  309. if (DEBUG_RK_SPI)
  310. rkspi_dump_regs(regs);
  311. /* Assert CS before transfer */
  312. if (flags & SPI_XFER_BEGIN)
  313. spi_cs_activate(dev, slave_plat->cs);
  314. while (len > 0) {
  315. int todo = min(len, 0xffff);
  316. rkspi_enable_chip(regs, false);
  317. writel(todo - 1, &regs->ctrlr1);
  318. rkspi_enable_chip(regs, true);
  319. toread = todo;
  320. towrite = todo;
  321. while (toread || towrite) {
  322. u32 status = readl(&regs->sr);
  323. if (towrite && !(status & SR_TF_FULL)) {
  324. writel(out ? *out++ : 0, regs->txdr);
  325. towrite--;
  326. }
  327. if (toread && !(status & SR_RF_EMPT)) {
  328. u32 byte = readl(regs->rxdr);
  329. if (in)
  330. *in++ = byte;
  331. toread--;
  332. }
  333. }
  334. ret = rkspi_wait_till_not_busy(regs);
  335. if (ret)
  336. break;
  337. len -= todo;
  338. }
  339. /* Deassert CS after transfer */
  340. if (flags & SPI_XFER_END)
  341. spi_cs_deactivate(dev, slave_plat->cs);
  342. rkspi_enable_chip(regs, false);
  343. return ret;
  344. }
  345. static int rockchip_spi_set_speed(struct udevice *bus, uint speed)
  346. {
  347. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  348. /* Clamp to the maximum frequency specified in the DTS */
  349. if (speed > priv->max_freq)
  350. speed = priv->max_freq;
  351. priv->speed_hz = speed;
  352. return 0;
  353. }
  354. static int rockchip_spi_set_mode(struct udevice *bus, uint mode)
  355. {
  356. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  357. priv->mode = mode;
  358. return 0;
  359. }
  360. static const struct dm_spi_ops rockchip_spi_ops = {
  361. .claim_bus = rockchip_spi_claim_bus,
  362. .release_bus = rockchip_spi_release_bus,
  363. .xfer = rockchip_spi_xfer,
  364. .set_speed = rockchip_spi_set_speed,
  365. .set_mode = rockchip_spi_set_mode,
  366. /*
  367. * cs_info is not needed, since we require all chip selects to be
  368. * in the device tree explicitly
  369. */
  370. };
  371. static const struct udevice_id rockchip_spi_ids[] = {
  372. { .compatible = "rockchip,rk3288-spi" },
  373. { .compatible = "rockchip,rk3368-spi" },
  374. { .compatible = "rockchip,rk3399-spi" },
  375. { }
  376. };
  377. U_BOOT_DRIVER(rockchip_spi) = {
  378. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  379. .name = "rockchip_rk3288_spi",
  380. #else
  381. .name = "rockchip_spi",
  382. #endif
  383. .id = UCLASS_SPI,
  384. .of_match = rockchip_spi_ids,
  385. .ops = &rockchip_spi_ops,
  386. .ofdata_to_platdata = rockchip_spi_ofdata_to_platdata,
  387. .platdata_auto_alloc_size = sizeof(struct rockchip_spi_platdata),
  388. .priv_auto_alloc_size = sizeof(struct rockchip_spi_priv),
  389. .probe = rockchip_spi_probe,
  390. };