rk_spi.c 12 KB

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