rk_spi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * spi driver for rockchip
  4. *
  5. * (C) 2019 Theobroma Systems Design und Consulting GmbH
  6. *
  7. * (C) Copyright 2015 Google, Inc
  8. *
  9. * (C) Copyright 2008-2013 Rockchip Electronics
  10. * Peter, Software Engineering, <superpeter.cai@gmail.com>.
  11. */
  12. #include <common.h>
  13. #include <clk.h>
  14. #include <dm.h>
  15. #include <dt-structs.h>
  16. #include <errno.h>
  17. #include <log.h>
  18. #include <spi.h>
  19. #include <time.h>
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <asm/io.h>
  23. #include <asm/arch-rockchip/clock.h>
  24. #include <asm/arch-rockchip/periph.h>
  25. #include <dm/pinctrl.h>
  26. #include "rk_spi.h"
  27. /* Change to 1 to output registers at the start of each transaction */
  28. #define DEBUG_RK_SPI 0
  29. /*
  30. * ctrlr1 is 16-bits, so we should support lengths of 0xffff + 1. However,
  31. * the controller seems to hang when given 0x10000, so stick with this for now.
  32. */
  33. #define ROCKCHIP_SPI_MAX_TRANLEN 0xffff
  34. struct rockchip_spi_params {
  35. /* RXFIFO overruns and TXFIFO underruns stop the master clock */
  36. bool master_manages_fifo;
  37. };
  38. struct rockchip_spi_platdata {
  39. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  40. struct dtd_rockchip_rk3288_spi of_plat;
  41. #endif
  42. s32 frequency; /* Default clock frequency, -1 for none */
  43. fdt_addr_t base;
  44. uint deactivate_delay_us; /* Delay to wait after deactivate */
  45. uint activate_delay_us; /* Delay to wait after activate */
  46. };
  47. struct rockchip_spi_priv {
  48. struct rockchip_spi *regs;
  49. struct clk clk;
  50. unsigned int max_freq;
  51. unsigned int mode;
  52. ulong last_transaction_us; /* Time of last transaction end */
  53. unsigned int speed_hz;
  54. unsigned int last_speed_hz;
  55. uint input_rate;
  56. };
  57. #define SPI_FIFO_DEPTH 32
  58. static void rkspi_dump_regs(struct rockchip_spi *regs)
  59. {
  60. debug("ctrl0: \t\t0x%08x\n", readl(&regs->ctrlr0));
  61. debug("ctrl1: \t\t0x%08x\n", readl(&regs->ctrlr1));
  62. debug("ssienr: \t\t0x%08x\n", readl(&regs->enr));
  63. debug("ser: \t\t0x%08x\n", readl(&regs->ser));
  64. debug("baudr: \t\t0x%08x\n", readl(&regs->baudr));
  65. debug("txftlr: \t\t0x%08x\n", readl(&regs->txftlr));
  66. debug("rxftlr: \t\t0x%08x\n", readl(&regs->rxftlr));
  67. debug("txflr: \t\t0x%08x\n", readl(&regs->txflr));
  68. debug("rxflr: \t\t0x%08x\n", readl(&regs->rxflr));
  69. debug("sr: \t\t0x%08x\n", readl(&regs->sr));
  70. debug("imr: \t\t0x%08x\n", readl(&regs->imr));
  71. debug("isr: \t\t0x%08x\n", readl(&regs->isr));
  72. debug("dmacr: \t\t0x%08x\n", readl(&regs->dmacr));
  73. debug("dmatdlr: \t0x%08x\n", readl(&regs->dmatdlr));
  74. debug("dmardlr: \t0x%08x\n", readl(&regs->dmardlr));
  75. }
  76. static void rkspi_enable_chip(struct rockchip_spi *regs, bool enable)
  77. {
  78. writel(enable ? 1 : 0, &regs->enr);
  79. }
  80. static void rkspi_set_clk(struct rockchip_spi_priv *priv, uint speed)
  81. {
  82. /*
  83. * We should try not to exceed the speed requested by the caller:
  84. * when selecting a divider, we need to make sure we round up.
  85. */
  86. uint clk_div = DIV_ROUND_UP(priv->input_rate, speed);
  87. /* The baudrate register (BAUDR) is defined as a 32bit register where
  88. * the upper 16bit are reserved and having 'Fsclk_out' in the lower
  89. * 16bits with 'Fsclk_out' defined as follows:
  90. *
  91. * Fsclk_out = Fspi_clk/ SCKDV
  92. * Where SCKDV is any even value between 2 and 65534.
  93. */
  94. if (clk_div > 0xfffe) {
  95. clk_div = 0xfffe;
  96. debug("%s: can't divide down to %d Hz (actual will be %d Hz)\n",
  97. __func__, speed, priv->input_rate / clk_div);
  98. }
  99. /* Round up to the next even 16bit number */
  100. clk_div = (clk_div + 1) & 0xfffe;
  101. debug("spi speed %u, div %u\n", speed, clk_div);
  102. clrsetbits_le32(&priv->regs->baudr, 0xffff, clk_div);
  103. priv->last_speed_hz = speed;
  104. }
  105. static int rkspi_wait_till_not_busy(struct rockchip_spi *regs)
  106. {
  107. unsigned long start;
  108. start = get_timer(0);
  109. while (readl(&regs->sr) & SR_BUSY) {
  110. if (get_timer(start) > ROCKCHIP_SPI_TIMEOUT_MS) {
  111. debug("RK SPI: Status keeps busy for 1000us after a read/write!\n");
  112. return -ETIMEDOUT;
  113. }
  114. }
  115. return 0;
  116. }
  117. static void spi_cs_activate(struct udevice *dev, uint cs)
  118. {
  119. struct udevice *bus = dev->parent;
  120. struct rockchip_spi_platdata *plat = bus->platdata;
  121. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  122. struct rockchip_spi *regs = priv->regs;
  123. /* If it's too soon to do another transaction, wait */
  124. if (plat->deactivate_delay_us && priv->last_transaction_us) {
  125. ulong delay_us; /* The delay completed so far */
  126. delay_us = timer_get_us() - priv->last_transaction_us;
  127. if (delay_us < plat->deactivate_delay_us) {
  128. ulong additional_delay_us =
  129. plat->deactivate_delay_us - delay_us;
  130. debug("%s: delaying by %ld us\n",
  131. __func__, additional_delay_us);
  132. udelay(additional_delay_us);
  133. }
  134. }
  135. debug("activate cs%u\n", cs);
  136. writel(1 << cs, &regs->ser);
  137. if (plat->activate_delay_us)
  138. udelay(plat->activate_delay_us);
  139. }
  140. static void spi_cs_deactivate(struct udevice *dev, uint cs)
  141. {
  142. struct udevice *bus = dev->parent;
  143. struct rockchip_spi_platdata *plat = bus->platdata;
  144. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  145. struct rockchip_spi *regs = priv->regs;
  146. debug("deactivate cs%u\n", cs);
  147. writel(0, &regs->ser);
  148. /* Remember time of this transaction so we can honour the bus delay */
  149. if (plat->deactivate_delay_us)
  150. priv->last_transaction_us = timer_get_us();
  151. }
  152. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  153. static int conv_of_platdata(struct udevice *dev)
  154. {
  155. struct rockchip_spi_platdata *plat = dev->platdata;
  156. struct dtd_rockchip_rk3288_spi *dtplat = &plat->of_plat;
  157. struct rockchip_spi_priv *priv = dev_get_priv(dev);
  158. int ret;
  159. plat->base = dtplat->reg[0];
  160. plat->frequency = 20000000;
  161. ret = clk_get_by_driver_info(dev, dtplat->clocks, &priv->clk);
  162. if (ret < 0)
  163. return ret;
  164. dev->req_seq = 0;
  165. return 0;
  166. }
  167. #endif
  168. static int rockchip_spi_ofdata_to_platdata(struct udevice *bus)
  169. {
  170. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  171. struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
  172. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  173. int ret;
  174. plat->base = dev_read_addr(bus);
  175. ret = clk_get_by_index(bus, 0, &priv->clk);
  176. if (ret < 0) {
  177. debug("%s: Could not get clock for %s: %d\n", __func__,
  178. bus->name, ret);
  179. return ret;
  180. }
  181. plat->frequency =
  182. dev_read_u32_default(bus, "spi-max-frequency", 50000000);
  183. plat->deactivate_delay_us =
  184. dev_read_u32_default(bus, "spi-deactivate-delay", 0);
  185. plat->activate_delay_us =
  186. dev_read_u32_default(bus, "spi-activate-delay", 0);
  187. debug("%s: base=%x, max-frequency=%d, deactivate_delay=%d\n",
  188. __func__, (uint)plat->base, plat->frequency,
  189. plat->deactivate_delay_us);
  190. #endif
  191. return 0;
  192. }
  193. static int rockchip_spi_calc_modclk(ulong max_freq)
  194. {
  195. /*
  196. * While this is not strictly correct for the RK3368, as the
  197. * GPLL will be 576MHz, things will still work, as the
  198. * clk_set_rate(...) implementation in our clock-driver will
  199. * chose the next closest rate not exceeding what we request
  200. * based on the output of this function.
  201. */
  202. unsigned div;
  203. const unsigned long gpll_hz = 594000000UL;
  204. /*
  205. * We need to find an input clock that provides at least twice
  206. * the maximum frequency and can be generated from the assumed
  207. * speed of GPLL (594MHz) using an integer divider.
  208. *
  209. * To give us more achievable bitrates at higher speeds (these
  210. * are generated by dividing by an even 16-bit integer from
  211. * this frequency), we try to have an input frequency of at
  212. * least 4x our max_freq.
  213. */
  214. div = DIV_ROUND_UP(gpll_hz, max_freq * 4);
  215. return gpll_hz / div;
  216. }
  217. static int rockchip_spi_probe(struct udevice *bus)
  218. {
  219. struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
  220. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  221. int ret;
  222. debug("%s: probe\n", __func__);
  223. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  224. ret = conv_of_platdata(bus);
  225. if (ret)
  226. return ret;
  227. #endif
  228. priv->regs = (struct rockchip_spi *)plat->base;
  229. priv->last_transaction_us = timer_get_us();
  230. priv->max_freq = plat->frequency;
  231. /* Clamp the value from the DTS against any hardware limits */
  232. if (priv->max_freq > ROCKCHIP_SPI_MAX_RATE)
  233. priv->max_freq = ROCKCHIP_SPI_MAX_RATE;
  234. /* Find a module-input clock that fits with the max_freq setting */
  235. ret = clk_set_rate(&priv->clk,
  236. rockchip_spi_calc_modclk(priv->max_freq));
  237. if (ret < 0) {
  238. debug("%s: Failed to set clock: %d\n", __func__, ret);
  239. return ret;
  240. }
  241. priv->input_rate = ret;
  242. debug("%s: rate = %u\n", __func__, priv->input_rate);
  243. return 0;
  244. }
  245. static int rockchip_spi_claim_bus(struct udevice *dev)
  246. {
  247. struct udevice *bus = dev->parent;
  248. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  249. struct rockchip_spi *regs = priv->regs;
  250. uint ctrlr0;
  251. /* Disable the SPI hardware */
  252. rkspi_enable_chip(regs, false);
  253. if (priv->speed_hz != priv->last_speed_hz)
  254. rkspi_set_clk(priv, priv->speed_hz);
  255. /* Operation Mode */
  256. ctrlr0 = OMOD_MASTER << OMOD_SHIFT;
  257. /* Data Frame Size */
  258. ctrlr0 |= DFS_8BIT << DFS_SHIFT;
  259. /* set SPI mode 0..3 */
  260. if (priv->mode & SPI_CPOL)
  261. ctrlr0 |= SCOL_HIGH << SCOL_SHIFT;
  262. if (priv->mode & SPI_CPHA)
  263. ctrlr0 |= SCPH_TOGSTA << SCPH_SHIFT;
  264. /* Chip Select Mode */
  265. ctrlr0 |= CSM_KEEP << CSM_SHIFT;
  266. /* SSN to Sclk_out delay */
  267. ctrlr0 |= SSN_DELAY_ONE << SSN_DELAY_SHIFT;
  268. /* Serial Endian Mode */
  269. ctrlr0 |= SEM_LITTLE << SEM_SHIFT;
  270. /* First Bit Mode */
  271. ctrlr0 |= FBM_MSB << FBM_SHIFT;
  272. /* Byte and Halfword Transform */
  273. ctrlr0 |= HALF_WORD_OFF << HALF_WORD_TX_SHIFT;
  274. /* Rxd Sample Delay */
  275. ctrlr0 |= 0 << RXDSD_SHIFT;
  276. /* Frame Format */
  277. ctrlr0 |= FRF_SPI << FRF_SHIFT;
  278. /* Tx and Rx mode */
  279. ctrlr0 |= TMOD_TR << TMOD_SHIFT;
  280. writel(ctrlr0, &regs->ctrlr0);
  281. return 0;
  282. }
  283. static int rockchip_spi_release_bus(struct udevice *dev)
  284. {
  285. struct udevice *bus = dev->parent;
  286. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  287. rkspi_enable_chip(priv->regs, false);
  288. return 0;
  289. }
  290. static inline int rockchip_spi_16bit_reader(struct udevice *dev,
  291. u8 **din, int *len)
  292. {
  293. struct udevice *bus = dev->parent;
  294. const struct rockchip_spi_params * const data =
  295. (void *)dev_get_driver_data(bus);
  296. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  297. struct rockchip_spi *regs = priv->regs;
  298. const u32 saved_ctrlr0 = readl(&regs->ctrlr0);
  299. #if defined(DEBUG)
  300. u32 statistics_rxlevels[33] = { };
  301. #endif
  302. u32 frames = *len / 2;
  303. u8 *in = (u8 *)(*din);
  304. u32 max_chunk_size = SPI_FIFO_DEPTH;
  305. if (!frames)
  306. return 0;
  307. /*
  308. * If we know that the hardware will manage RXFIFO overruns
  309. * (i.e. stop the SPI clock until there's space in the FIFO),
  310. * we the allow largest possible chunk size that can be
  311. * represented in CTRLR1.
  312. */
  313. if (data && data->master_manages_fifo)
  314. max_chunk_size = ROCKCHIP_SPI_MAX_TRANLEN;
  315. // rockchip_spi_configure(dev, mode, size)
  316. rkspi_enable_chip(regs, false);
  317. clrsetbits_le32(&regs->ctrlr0,
  318. TMOD_MASK << TMOD_SHIFT,
  319. TMOD_RO << TMOD_SHIFT);
  320. /* 16bit data frame size */
  321. clrsetbits_le32(&regs->ctrlr0, DFS_MASK, DFS_16BIT);
  322. /* Update caller's context */
  323. const u32 bytes_to_process = 2 * frames;
  324. *din += bytes_to_process;
  325. *len -= bytes_to_process;
  326. /* Process our frames */
  327. while (frames) {
  328. u32 chunk_size = min(frames, max_chunk_size);
  329. frames -= chunk_size;
  330. writew(chunk_size - 1, &regs->ctrlr1);
  331. rkspi_enable_chip(regs, true);
  332. do {
  333. u32 rx_level = readw(&regs->rxflr);
  334. #if defined(DEBUG)
  335. statistics_rxlevels[rx_level]++;
  336. #endif
  337. chunk_size -= rx_level;
  338. while (rx_level--) {
  339. u16 val = readw(regs->rxdr);
  340. *in++ = val & 0xff;
  341. *in++ = val >> 8;
  342. }
  343. } while (chunk_size);
  344. rkspi_enable_chip(regs, false);
  345. }
  346. #if defined(DEBUG)
  347. debug("%s: observed rx_level during processing:\n", __func__);
  348. for (int i = 0; i <= 32; ++i)
  349. if (statistics_rxlevels[i])
  350. debug("\t%2d: %d\n", i, statistics_rxlevels[i]);
  351. #endif
  352. /* Restore the original transfer setup and return error-free. */
  353. writel(saved_ctrlr0, &regs->ctrlr0);
  354. return 0;
  355. }
  356. static int rockchip_spi_xfer(struct udevice *dev, unsigned int bitlen,
  357. const void *dout, void *din, unsigned long flags)
  358. {
  359. struct udevice *bus = dev->parent;
  360. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  361. struct rockchip_spi *regs = priv->regs;
  362. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  363. int len = bitlen >> 3;
  364. const u8 *out = dout;
  365. u8 *in = din;
  366. int toread, towrite;
  367. int ret = 0;
  368. debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din,
  369. len, flags);
  370. if (DEBUG_RK_SPI)
  371. rkspi_dump_regs(regs);
  372. /* Assert CS before transfer */
  373. if (flags & SPI_XFER_BEGIN)
  374. spi_cs_activate(dev, slave_plat->cs);
  375. /*
  376. * To ensure fast loading of firmware images (e.g. full U-Boot
  377. * stage, ATF, Linux kernel) from SPI flash, we optimise the
  378. * case of read-only transfers by using the full 16bits of each
  379. * FIFO element.
  380. */
  381. if (!out)
  382. ret = rockchip_spi_16bit_reader(dev, &in, &len);
  383. /* This is the original 8bit reader/writer code */
  384. while (len > 0) {
  385. int todo = min(len, ROCKCHIP_SPI_MAX_TRANLEN);
  386. rkspi_enable_chip(regs, false);
  387. writel(todo - 1, &regs->ctrlr1);
  388. rkspi_enable_chip(regs, true);
  389. toread = todo;
  390. towrite = todo;
  391. while (toread || towrite) {
  392. u32 status = readl(&regs->sr);
  393. if (towrite && !(status & SR_TF_FULL)) {
  394. writel(out ? *out++ : 0, regs->txdr);
  395. towrite--;
  396. }
  397. if (toread && !(status & SR_RF_EMPT)) {
  398. u32 byte = readl(regs->rxdr);
  399. if (in)
  400. *in++ = byte;
  401. toread--;
  402. }
  403. }
  404. /*
  405. * In case that there's a transmit-component, we need to wait
  406. * until the control goes idle before we can disable the SPI
  407. * control logic (as this will implictly flush the FIFOs).
  408. */
  409. if (out) {
  410. ret = rkspi_wait_till_not_busy(regs);
  411. if (ret)
  412. break;
  413. }
  414. len -= todo;
  415. }
  416. /* Deassert CS after transfer */
  417. if (flags & SPI_XFER_END)
  418. spi_cs_deactivate(dev, slave_plat->cs);
  419. rkspi_enable_chip(regs, false);
  420. return ret;
  421. }
  422. static int rockchip_spi_set_speed(struct udevice *bus, uint speed)
  423. {
  424. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  425. /* Clamp to the maximum frequency specified in the DTS */
  426. if (speed > priv->max_freq)
  427. speed = priv->max_freq;
  428. priv->speed_hz = speed;
  429. return 0;
  430. }
  431. static int rockchip_spi_set_mode(struct udevice *bus, uint mode)
  432. {
  433. struct rockchip_spi_priv *priv = dev_get_priv(bus);
  434. priv->mode = mode;
  435. return 0;
  436. }
  437. static const struct dm_spi_ops rockchip_spi_ops = {
  438. .claim_bus = rockchip_spi_claim_bus,
  439. .release_bus = rockchip_spi_release_bus,
  440. .xfer = rockchip_spi_xfer,
  441. .set_speed = rockchip_spi_set_speed,
  442. .set_mode = rockchip_spi_set_mode,
  443. /*
  444. * cs_info is not needed, since we require all chip selects to be
  445. * in the device tree explicitly
  446. */
  447. };
  448. const struct rockchip_spi_params rk3399_spi_params = {
  449. .master_manages_fifo = true,
  450. };
  451. static const struct udevice_id rockchip_spi_ids[] = {
  452. { .compatible = "rockchip,rk3066-spi" },
  453. { .compatible = "rockchip,rk3288-spi" },
  454. { .compatible = "rockchip,rk3328-spi" },
  455. { .compatible = "rockchip,rk3368-spi",
  456. .data = (ulong)&rk3399_spi_params },
  457. { .compatible = "rockchip,rk3399-spi",
  458. .data = (ulong)&rk3399_spi_params },
  459. { }
  460. };
  461. U_BOOT_DRIVER(rockchip_rk3288_spi) = {
  462. .name = "rockchip_rk3288_spi",
  463. .id = UCLASS_SPI,
  464. .of_match = rockchip_spi_ids,
  465. .ops = &rockchip_spi_ops,
  466. .ofdata_to_platdata = rockchip_spi_ofdata_to_platdata,
  467. .platdata_auto_alloc_size = sizeof(struct rockchip_spi_platdata),
  468. .priv_auto_alloc_size = sizeof(struct rockchip_spi_priv),
  469. .probe = rockchip_spi_probe,
  470. };
  471. U_BOOT_DRIVER_ALIAS(rockchip_rk3288_spi, rockchip_rk3368_spi)