exynos_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2012 SAMSUNG Electronics
  4. * Padmavathi Venna <padma.v@samsung.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <malloc.h>
  10. #include <spi.h>
  11. #include <fdtdec.h>
  12. #include <asm/arch/clk.h>
  13. #include <asm/arch/clock.h>
  14. #include <asm/arch/cpu.h>
  15. #include <asm/arch/gpio.h>
  16. #include <asm/arch/pinmux.h>
  17. #include <asm/arch/spi.h>
  18. #include <asm/io.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. struct exynos_spi_platdata {
  21. enum periph_id periph_id;
  22. s32 frequency; /* Default clock frequency, -1 for none */
  23. struct exynos_spi *regs;
  24. uint deactivate_delay_us; /* Delay to wait after deactivate */
  25. };
  26. struct exynos_spi_priv {
  27. struct exynos_spi *regs;
  28. unsigned int freq; /* Default frequency */
  29. unsigned int mode;
  30. enum periph_id periph_id; /* Peripheral ID for this device */
  31. unsigned int fifo_size;
  32. int skip_preamble;
  33. ulong last_transaction_us; /* Time of last transaction end */
  34. };
  35. /**
  36. * Flush spi tx, rx fifos and reset the SPI controller
  37. *
  38. * @param regs Pointer to SPI registers
  39. */
  40. static void spi_flush_fifo(struct exynos_spi *regs)
  41. {
  42. clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
  43. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  44. setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
  45. }
  46. static void spi_get_fifo_levels(struct exynos_spi *regs,
  47. int *rx_lvl, int *tx_lvl)
  48. {
  49. uint32_t spi_sts = readl(&regs->spi_sts);
  50. *rx_lvl = (spi_sts >> SPI_RX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
  51. *tx_lvl = (spi_sts >> SPI_TX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
  52. }
  53. /**
  54. * If there's something to transfer, do a software reset and set a
  55. * transaction size.
  56. *
  57. * @param regs SPI peripheral registers
  58. * @param count Number of bytes to transfer
  59. * @param step Number of bytes to transfer in each packet (1 or 4)
  60. */
  61. static void spi_request_bytes(struct exynos_spi *regs, int count, int step)
  62. {
  63. debug("%s: regs=%p, count=%d, step=%d\n", __func__, regs, count, step);
  64. /* For word address we need to swap bytes */
  65. if (step == 4) {
  66. setbits_le32(&regs->mode_cfg,
  67. SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD);
  68. count /= 4;
  69. setbits_le32(&regs->swap_cfg, SPI_TX_SWAP_EN | SPI_RX_SWAP_EN |
  70. SPI_TX_BYTE_SWAP | SPI_RX_BYTE_SWAP |
  71. SPI_TX_HWORD_SWAP | SPI_RX_HWORD_SWAP);
  72. } else {
  73. /* Select byte access and clear the swap configuration */
  74. clrbits_le32(&regs->mode_cfg,
  75. SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD);
  76. writel(0, &regs->swap_cfg);
  77. }
  78. assert(count && count < (1 << 16));
  79. setbits_le32(&regs->ch_cfg, SPI_CH_RST);
  80. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  81. writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
  82. }
  83. static int spi_rx_tx(struct exynos_spi_priv *priv, int todo,
  84. void **dinp, void const **doutp, unsigned long flags)
  85. {
  86. struct exynos_spi *regs = priv->regs;
  87. uchar *rxp = *dinp;
  88. const uchar *txp = *doutp;
  89. int rx_lvl, tx_lvl;
  90. uint out_bytes, in_bytes;
  91. int toread;
  92. unsigned start = get_timer(0);
  93. int stopping;
  94. int step;
  95. out_bytes = in_bytes = todo;
  96. stopping = priv->skip_preamble && (flags & SPI_XFER_END) &&
  97. !(priv->mode & SPI_SLAVE);
  98. /*
  99. * Try to transfer words if we can. This helps read performance at
  100. * SPI clock speeds above about 20MHz.
  101. */
  102. step = 1;
  103. if (!((todo | (uintptr_t)rxp | (uintptr_t)txp) & 3) &&
  104. !priv->skip_preamble)
  105. step = 4;
  106. /*
  107. * If there's something to send, do a software reset and set a
  108. * transaction size.
  109. */
  110. spi_request_bytes(regs, todo, step);
  111. /*
  112. * Bytes are transmitted/received in pairs. Wait to receive all the
  113. * data because then transmission will be done as well.
  114. */
  115. toread = in_bytes;
  116. while (in_bytes) {
  117. int temp;
  118. /* Keep the fifos full/empty. */
  119. spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
  120. /*
  121. * Don't completely fill the txfifo, since we don't want our
  122. * rxfifo to overflow, and it may already contain data.
  123. */
  124. while (tx_lvl < priv->fifo_size/2 && out_bytes) {
  125. if (!txp)
  126. temp = -1;
  127. else if (step == 4)
  128. temp = *(uint32_t *)txp;
  129. else
  130. temp = *txp;
  131. writel(temp, &regs->tx_data);
  132. out_bytes -= step;
  133. if (txp)
  134. txp += step;
  135. tx_lvl += step;
  136. }
  137. if (rx_lvl >= step) {
  138. while (rx_lvl >= step) {
  139. temp = readl(&regs->rx_data);
  140. if (priv->skip_preamble) {
  141. if (temp == SPI_PREAMBLE_END_BYTE) {
  142. priv->skip_preamble = 0;
  143. stopping = 0;
  144. }
  145. } else {
  146. if (rxp || stopping) {
  147. if (step == 4)
  148. *(uint32_t *)rxp = temp;
  149. else
  150. *rxp = temp;
  151. rxp += step;
  152. }
  153. in_bytes -= step;
  154. }
  155. toread -= step;
  156. rx_lvl -= step;
  157. }
  158. } else if (!toread) {
  159. /*
  160. * We have run out of input data, but haven't read
  161. * enough bytes after the preamble yet. Read some more,
  162. * and make sure that we transmit dummy bytes too, to
  163. * keep things going.
  164. */
  165. assert(!out_bytes);
  166. out_bytes = in_bytes;
  167. toread = in_bytes;
  168. txp = NULL;
  169. spi_request_bytes(regs, toread, step);
  170. }
  171. if (priv->skip_preamble && get_timer(start) > 100) {
  172. debug("SPI timeout: in_bytes=%d, out_bytes=%d, ",
  173. in_bytes, out_bytes);
  174. return -ETIMEDOUT;
  175. }
  176. }
  177. *dinp = rxp;
  178. *doutp = txp;
  179. return 0;
  180. }
  181. /**
  182. * Activate the CS by driving it LOW
  183. *
  184. * @param slave Pointer to spi_slave to which controller has to
  185. * communicate with
  186. */
  187. static void spi_cs_activate(struct udevice *dev)
  188. {
  189. struct udevice *bus = dev->parent;
  190. struct exynos_spi_platdata *pdata = dev_get_platdata(bus);
  191. struct exynos_spi_priv *priv = dev_get_priv(bus);
  192. /* If it's too soon to do another transaction, wait */
  193. if (pdata->deactivate_delay_us &&
  194. priv->last_transaction_us) {
  195. ulong delay_us; /* The delay completed so far */
  196. delay_us = timer_get_us() - priv->last_transaction_us;
  197. if (delay_us < pdata->deactivate_delay_us)
  198. udelay(pdata->deactivate_delay_us - delay_us);
  199. }
  200. clrbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  201. debug("Activate CS, bus '%s'\n", bus->name);
  202. priv->skip_preamble = priv->mode & SPI_PREAMBLE;
  203. }
  204. /**
  205. * Deactivate the CS by driving it HIGH
  206. *
  207. * @param slave Pointer to spi_slave to which controller has to
  208. * communicate with
  209. */
  210. static void spi_cs_deactivate(struct udevice *dev)
  211. {
  212. struct udevice *bus = dev->parent;
  213. struct exynos_spi_platdata *pdata = dev_get_platdata(bus);
  214. struct exynos_spi_priv *priv = dev_get_priv(bus);
  215. setbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  216. /* Remember time of this transaction so we can honour the bus delay */
  217. if (pdata->deactivate_delay_us)
  218. priv->last_transaction_us = timer_get_us();
  219. debug("Deactivate CS, bus '%s'\n", bus->name);
  220. }
  221. static int exynos_spi_ofdata_to_platdata(struct udevice *bus)
  222. {
  223. struct exynos_spi_platdata *plat = bus->platdata;
  224. const void *blob = gd->fdt_blob;
  225. int node = dev_of_offset(bus);
  226. plat->regs = (struct exynos_spi *)devfdt_get_addr(bus);
  227. plat->periph_id = pinmux_decode_periph_id(blob, node);
  228. if (plat->periph_id == PERIPH_ID_NONE) {
  229. debug("%s: Invalid peripheral ID %d\n", __func__,
  230. plat->periph_id);
  231. return -FDT_ERR_NOTFOUND;
  232. }
  233. /* Use 500KHz as a suitable default */
  234. plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  235. 500000);
  236. plat->deactivate_delay_us = fdtdec_get_int(blob, node,
  237. "spi-deactivate-delay", 0);
  238. debug("%s: regs=%p, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
  239. __func__, plat->regs, plat->periph_id, plat->frequency,
  240. plat->deactivate_delay_us);
  241. return 0;
  242. }
  243. static int exynos_spi_probe(struct udevice *bus)
  244. {
  245. struct exynos_spi_platdata *plat = dev_get_platdata(bus);
  246. struct exynos_spi_priv *priv = dev_get_priv(bus);
  247. priv->regs = plat->regs;
  248. if (plat->periph_id == PERIPH_ID_SPI1 ||
  249. plat->periph_id == PERIPH_ID_SPI2)
  250. priv->fifo_size = 64;
  251. else
  252. priv->fifo_size = 256;
  253. priv->skip_preamble = 0;
  254. priv->last_transaction_us = timer_get_us();
  255. priv->freq = plat->frequency;
  256. priv->periph_id = plat->periph_id;
  257. return 0;
  258. }
  259. static int exynos_spi_claim_bus(struct udevice *dev)
  260. {
  261. struct udevice *bus = dev->parent;
  262. struct exynos_spi_priv *priv = dev_get_priv(bus);
  263. exynos_pinmux_config(priv->periph_id, PINMUX_FLAG_NONE);
  264. spi_flush_fifo(priv->regs);
  265. writel(SPI_FB_DELAY_180, &priv->regs->fb_clk);
  266. return 0;
  267. }
  268. static int exynos_spi_release_bus(struct udevice *dev)
  269. {
  270. struct udevice *bus = dev->parent;
  271. struct exynos_spi_priv *priv = dev_get_priv(bus);
  272. spi_flush_fifo(priv->regs);
  273. return 0;
  274. }
  275. static int exynos_spi_xfer(struct udevice *dev, unsigned int bitlen,
  276. const void *dout, void *din, unsigned long flags)
  277. {
  278. struct udevice *bus = dev->parent;
  279. struct exynos_spi_priv *priv = dev_get_priv(bus);
  280. int upto, todo;
  281. int bytelen;
  282. int ret = 0;
  283. /* spi core configured to do 8 bit transfers */
  284. if (bitlen % 8) {
  285. debug("Non byte aligned SPI transfer.\n");
  286. return -1;
  287. }
  288. /* Start the transaction, if necessary. */
  289. if ((flags & SPI_XFER_BEGIN))
  290. spi_cs_activate(dev);
  291. /*
  292. * Exynos SPI limits each transfer to 65535 transfers. To keep
  293. * things simple, allow a maximum of 65532 bytes. We could allow
  294. * more in word mode, but the performance difference is small.
  295. */
  296. bytelen = bitlen / 8;
  297. for (upto = 0; !ret && upto < bytelen; upto += todo) {
  298. todo = min(bytelen - upto, (1 << 16) - 4);
  299. ret = spi_rx_tx(priv, todo, &din, &dout, flags);
  300. if (ret)
  301. break;
  302. }
  303. /* Stop the transaction, if necessary. */
  304. if ((flags & SPI_XFER_END) && !(priv->mode & SPI_SLAVE)) {
  305. spi_cs_deactivate(dev);
  306. if (priv->skip_preamble) {
  307. assert(!priv->skip_preamble);
  308. debug("Failed to complete premable transaction\n");
  309. ret = -1;
  310. }
  311. }
  312. return ret;
  313. }
  314. static int exynos_spi_set_speed(struct udevice *bus, uint speed)
  315. {
  316. struct exynos_spi_platdata *plat = bus->platdata;
  317. struct exynos_spi_priv *priv = dev_get_priv(bus);
  318. int ret;
  319. if (speed > plat->frequency)
  320. speed = plat->frequency;
  321. ret = set_spi_clk(priv->periph_id, speed);
  322. if (ret)
  323. return ret;
  324. priv->freq = speed;
  325. debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
  326. return 0;
  327. }
  328. static int exynos_spi_set_mode(struct udevice *bus, uint mode)
  329. {
  330. struct exynos_spi_priv *priv = dev_get_priv(bus);
  331. uint32_t reg;
  332. reg = readl(&priv->regs->ch_cfg);
  333. reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
  334. if (mode & SPI_CPHA)
  335. reg |= SPI_CH_CPHA_B;
  336. if (mode & SPI_CPOL)
  337. reg |= SPI_CH_CPOL_L;
  338. writel(reg, &priv->regs->ch_cfg);
  339. priv->mode = mode;
  340. debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
  341. return 0;
  342. }
  343. static const struct dm_spi_ops exynos_spi_ops = {
  344. .claim_bus = exynos_spi_claim_bus,
  345. .release_bus = exynos_spi_release_bus,
  346. .xfer = exynos_spi_xfer,
  347. .set_speed = exynos_spi_set_speed,
  348. .set_mode = exynos_spi_set_mode,
  349. /*
  350. * cs_info is not needed, since we require all chip selects to be
  351. * in the device tree explicitly
  352. */
  353. };
  354. static const struct udevice_id exynos_spi_ids[] = {
  355. { .compatible = "samsung,exynos-spi" },
  356. { }
  357. };
  358. U_BOOT_DRIVER(exynos_spi) = {
  359. .name = "exynos_spi",
  360. .id = UCLASS_SPI,
  361. .of_match = exynos_spi_ids,
  362. .ops = &exynos_spi_ops,
  363. .ofdata_to_platdata = exynos_spi_ofdata_to_platdata,
  364. .platdata_auto_alloc_size = sizeof(struct exynos_spi_platdata),
  365. .priv_auto_alloc_size = sizeof(struct exynos_spi_priv),
  366. .probe = exynos_spi_probe,
  367. };