pic32_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Microchip PIC32 SPI controller driver.
  3. *
  4. * Copyright (c) 2015, Microchip Technology Inc.
  5. * Purna Chandra Mandal <purna.mandal@microchip.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <clk.h>
  11. #include <dm.h>
  12. #include <linux/compat.h>
  13. #include <malloc.h>
  14. #include <spi.h>
  15. #include <asm/types.h>
  16. #include <asm/io.h>
  17. #include <asm/gpio.h>
  18. #include <dt-bindings/clock/microchip,clock.h>
  19. #include <mach/pic32.h>
  20. DECLARE_GLOBAL_DATA_PTR;
  21. /* PIC32 SPI controller registers */
  22. struct pic32_reg_spi {
  23. struct pic32_reg_atomic ctrl;
  24. struct pic32_reg_atomic status;
  25. struct pic32_reg_atomic buf;
  26. struct pic32_reg_atomic baud;
  27. struct pic32_reg_atomic ctrl2;
  28. };
  29. /* Bit fields in SPI Control Register */
  30. #define PIC32_SPI_CTRL_MSTEN BIT(5) /* Enable SPI Master */
  31. #define PIC32_SPI_CTRL_CKP BIT(6) /* active low */
  32. #define PIC32_SPI_CTRL_CKE BIT(8) /* Tx on falling edge */
  33. #define PIC32_SPI_CTRL_SMP BIT(9) /* Rx at middle or end of tx */
  34. #define PIC32_SPI_CTRL_BPW_MASK 0x03 /* Bits per word */
  35. #define PIC32_SPI_CTRL_BPW_8 0x0
  36. #define PIC32_SPI_CTRL_BPW_16 0x1
  37. #define PIC32_SPI_CTRL_BPW_32 0x2
  38. #define PIC32_SPI_CTRL_BPW_SHIFT 10
  39. #define PIC32_SPI_CTRL_ON BIT(15) /* Macro enable */
  40. #define PIC32_SPI_CTRL_ENHBUF BIT(16) /* Enable enhanced buffering */
  41. #define PIC32_SPI_CTRL_MCLKSEL BIT(23) /* Select SPI Clock src */
  42. #define PIC32_SPI_CTRL_MSSEN BIT(28) /* SPI macro will drive SS */
  43. #define PIC32_SPI_CTRL_FRMEN BIT(31) /* Enable framing mode */
  44. /* Bit fields in SPI Status Register */
  45. #define PIC32_SPI_STAT_RX_OV BIT(6) /* err, s/w needs to clear */
  46. #define PIC32_SPI_STAT_TF_LVL_MASK 0x1f
  47. #define PIC32_SPI_STAT_TF_LVL_SHIFT 16
  48. #define PIC32_SPI_STAT_RF_LVL_MASK 0x1f
  49. #define PIC32_SPI_STAT_RF_LVL_SHIFT 24
  50. /* Bit fields in SPI Baud Register */
  51. #define PIC32_SPI_BAUD_MASK 0x1ff
  52. struct pic32_spi_priv {
  53. struct pic32_reg_spi *regs;
  54. u32 fifo_depth; /* FIFO depth in bytes */
  55. u32 fifo_n_word; /* FIFO depth in words */
  56. struct gpio_desc cs_gpio;
  57. /* Current SPI slave specific */
  58. ulong clk_rate;
  59. u32 speed_hz; /* spi-clk rate */
  60. int mode;
  61. /* Current message/transfer state */
  62. const void *tx;
  63. const void *tx_end;
  64. const void *rx;
  65. const void *rx_end;
  66. u32 len;
  67. /* SPI FiFo accessor */
  68. void (*rx_fifo)(struct pic32_spi_priv *);
  69. void (*tx_fifo)(struct pic32_spi_priv *);
  70. };
  71. static inline void pic32_spi_enable(struct pic32_spi_priv *priv)
  72. {
  73. writel(PIC32_SPI_CTRL_ON, &priv->regs->ctrl.set);
  74. }
  75. static inline void pic32_spi_disable(struct pic32_spi_priv *priv)
  76. {
  77. writel(PIC32_SPI_CTRL_ON, &priv->regs->ctrl.clr);
  78. }
  79. static inline u32 pic32_spi_rx_fifo_level(struct pic32_spi_priv *priv)
  80. {
  81. u32 sr = readl(&priv->regs->status.raw);
  82. return (sr >> PIC32_SPI_STAT_RF_LVL_SHIFT) & PIC32_SPI_STAT_RF_LVL_MASK;
  83. }
  84. static inline u32 pic32_spi_tx_fifo_level(struct pic32_spi_priv *priv)
  85. {
  86. u32 sr = readl(&priv->regs->status.raw);
  87. return (sr >> PIC32_SPI_STAT_TF_LVL_SHIFT) & PIC32_SPI_STAT_TF_LVL_MASK;
  88. }
  89. /* Return the max entries we can fill into tx fifo */
  90. static u32 pic32_tx_max(struct pic32_spi_priv *priv, int n_bytes)
  91. {
  92. u32 tx_left, tx_room, rxtx_gap;
  93. tx_left = (priv->tx_end - priv->tx) / n_bytes;
  94. tx_room = priv->fifo_n_word - pic32_spi_tx_fifo_level(priv);
  95. rxtx_gap = (priv->rx_end - priv->rx) - (priv->tx_end - priv->tx);
  96. rxtx_gap /= n_bytes;
  97. return min3(tx_left, tx_room, (u32)(priv->fifo_n_word - rxtx_gap));
  98. }
  99. /* Return the max entries we should read out of rx fifo */
  100. static u32 pic32_rx_max(struct pic32_spi_priv *priv, int n_bytes)
  101. {
  102. u32 rx_left = (priv->rx_end - priv->rx) / n_bytes;
  103. return min_t(u32, rx_left, pic32_spi_rx_fifo_level(priv));
  104. }
  105. #define BUILD_SPI_FIFO_RW(__name, __type, __bwl) \
  106. static void pic32_spi_rx_##__name(struct pic32_spi_priv *priv) \
  107. { \
  108. __type val; \
  109. u32 mx = pic32_rx_max(priv, sizeof(__type)); \
  110. \
  111. for (; mx; mx--) { \
  112. val = read##__bwl(&priv->regs->buf.raw); \
  113. if (priv->rx_end - priv->len) \
  114. *(__type *)(priv->rx) = val; \
  115. priv->rx += sizeof(__type); \
  116. } \
  117. } \
  118. \
  119. static void pic32_spi_tx_##__name(struct pic32_spi_priv *priv) \
  120. { \
  121. __type val; \
  122. u32 mx = pic32_tx_max(priv, sizeof(__type)); \
  123. \
  124. for (; mx ; mx--) { \
  125. val = (__type) ~0U; \
  126. if (priv->tx_end - priv->len) \
  127. val = *(__type *)(priv->tx); \
  128. write##__bwl(val, &priv->regs->buf.raw); \
  129. priv->tx += sizeof(__type); \
  130. } \
  131. }
  132. BUILD_SPI_FIFO_RW(byte, u8, b);
  133. BUILD_SPI_FIFO_RW(word, u16, w);
  134. BUILD_SPI_FIFO_RW(dword, u32, l);
  135. static int pic32_spi_set_word_size(struct pic32_spi_priv *priv,
  136. unsigned int wordlen)
  137. {
  138. u32 bits_per_word;
  139. u32 val;
  140. switch (wordlen) {
  141. case 8:
  142. priv->rx_fifo = pic32_spi_rx_byte;
  143. priv->tx_fifo = pic32_spi_tx_byte;
  144. bits_per_word = PIC32_SPI_CTRL_BPW_8;
  145. break;
  146. case 16:
  147. priv->rx_fifo = pic32_spi_rx_word;
  148. priv->tx_fifo = pic32_spi_tx_word;
  149. bits_per_word = PIC32_SPI_CTRL_BPW_16;
  150. break;
  151. case 32:
  152. priv->rx_fifo = pic32_spi_rx_dword;
  153. priv->tx_fifo = pic32_spi_tx_dword;
  154. bits_per_word = PIC32_SPI_CTRL_BPW_32;
  155. break;
  156. default:
  157. printf("pic32-spi: unsupported wordlen\n");
  158. return -EINVAL;
  159. }
  160. /* set bits-per-word */
  161. val = readl(&priv->regs->ctrl.raw);
  162. val &= ~(PIC32_SPI_CTRL_BPW_MASK << PIC32_SPI_CTRL_BPW_SHIFT);
  163. val |= bits_per_word << PIC32_SPI_CTRL_BPW_SHIFT;
  164. writel(val, &priv->regs->ctrl.raw);
  165. /* calculate maximum number of words fifo can hold */
  166. priv->fifo_n_word = DIV_ROUND_UP(priv->fifo_depth, wordlen / 8);
  167. return 0;
  168. }
  169. static int pic32_spi_claim_bus(struct udevice *slave)
  170. {
  171. struct pic32_spi_priv *priv = dev_get_priv(slave->parent);
  172. /* enable chip */
  173. pic32_spi_enable(priv);
  174. return 0;
  175. }
  176. static int pic32_spi_release_bus(struct udevice *slave)
  177. {
  178. struct pic32_spi_priv *priv = dev_get_priv(slave->parent);
  179. /* disable chip */
  180. pic32_spi_disable(priv);
  181. return 0;
  182. }
  183. static void spi_cs_activate(struct pic32_spi_priv *priv)
  184. {
  185. if (!dm_gpio_is_valid(&priv->cs_gpio))
  186. return;
  187. dm_gpio_set_value(&priv->cs_gpio, 1);
  188. }
  189. static void spi_cs_deactivate(struct pic32_spi_priv *priv)
  190. {
  191. if (!dm_gpio_is_valid(&priv->cs_gpio))
  192. return;
  193. dm_gpio_set_value(&priv->cs_gpio, 0);
  194. }
  195. static int pic32_spi_xfer(struct udevice *slave, unsigned int bitlen,
  196. const void *tx_buf, void *rx_buf,
  197. unsigned long flags)
  198. {
  199. struct dm_spi_slave_platdata *slave_plat;
  200. struct udevice *bus = slave->parent;
  201. struct pic32_spi_priv *priv;
  202. int len = bitlen / 8;
  203. int ret = 0;
  204. ulong tbase;
  205. priv = dev_get_priv(bus);
  206. slave_plat = dev_get_parent_platdata(slave);
  207. debug("spi_xfer: bus:%i cs:%i flags:%lx\n",
  208. bus->seq, slave_plat->cs, flags);
  209. debug("msg tx %p, rx %p submitted of %d byte(s)\n",
  210. tx_buf, rx_buf, len);
  211. /* assert cs */
  212. if (flags & SPI_XFER_BEGIN)
  213. spi_cs_activate(priv);
  214. /* set current transfer information */
  215. priv->tx = tx_buf;
  216. priv->rx = rx_buf;
  217. priv->tx_end = priv->tx + len;
  218. priv->rx_end = priv->rx + len;
  219. priv->len = len;
  220. /* transact by polling */
  221. tbase = get_timer(0);
  222. for (;;) {
  223. priv->tx_fifo(priv);
  224. priv->rx_fifo(priv);
  225. /* received sufficient data */
  226. if (priv->rx >= priv->rx_end) {
  227. ret = 0;
  228. break;
  229. }
  230. if (get_timer(tbase) > 5 * CONFIG_SYS_HZ) {
  231. printf("pic32_spi: error, xfer timedout.\n");
  232. flags |= SPI_XFER_END;
  233. ret = -ETIMEDOUT;
  234. break;
  235. }
  236. }
  237. /* deassert cs */
  238. if (flags & SPI_XFER_END)
  239. spi_cs_deactivate(priv);
  240. return ret;
  241. }
  242. static int pic32_spi_set_speed(struct udevice *bus, uint speed)
  243. {
  244. struct pic32_spi_priv *priv = dev_get_priv(bus);
  245. u32 div;
  246. debug("%s: %s, speed %u\n", __func__, bus->name, speed);
  247. /* div = [clk_in / (2 * spi_clk)] - 1 */
  248. div = (priv->clk_rate / 2 / speed) - 1;
  249. div &= PIC32_SPI_BAUD_MASK;
  250. writel(div, &priv->regs->baud.raw);
  251. priv->speed_hz = speed;
  252. return 0;
  253. }
  254. static int pic32_spi_set_mode(struct udevice *bus, uint mode)
  255. {
  256. struct pic32_spi_priv *priv = dev_get_priv(bus);
  257. u32 val;
  258. debug("%s: %s, mode %d\n", __func__, bus->name, mode);
  259. /* set spi-clk mode */
  260. val = readl(&priv->regs->ctrl.raw);
  261. /* HIGH when idle */
  262. if (mode & SPI_CPOL)
  263. val |= PIC32_SPI_CTRL_CKP;
  264. else
  265. val &= ~PIC32_SPI_CTRL_CKP;
  266. /* TX at idle-to-active clk transition */
  267. if (mode & SPI_CPHA)
  268. val &= ~PIC32_SPI_CTRL_CKE;
  269. else
  270. val |= PIC32_SPI_CTRL_CKE;
  271. /* RX at end of tx */
  272. val |= PIC32_SPI_CTRL_SMP;
  273. writel(val, &priv->regs->ctrl.raw);
  274. priv->mode = mode;
  275. return 0;
  276. }
  277. static int pic32_spi_set_wordlen(struct udevice *slave, unsigned int wordlen)
  278. {
  279. struct pic32_spi_priv *priv = dev_get_priv(slave->parent);
  280. return pic32_spi_set_word_size(priv, wordlen);
  281. }
  282. static void pic32_spi_hw_init(struct pic32_spi_priv *priv)
  283. {
  284. u32 val;
  285. /* disable module */
  286. pic32_spi_disable(priv);
  287. val = readl(&priv->regs->ctrl);
  288. /* enable enhanced fifo of 128bit deep */
  289. val |= PIC32_SPI_CTRL_ENHBUF;
  290. priv->fifo_depth = 16;
  291. /* disable framing mode */
  292. val &= ~PIC32_SPI_CTRL_FRMEN;
  293. /* enable master mode */
  294. val |= PIC32_SPI_CTRL_MSTEN;
  295. /* select clk source */
  296. val &= ~PIC32_SPI_CTRL_MCLKSEL;
  297. /* set manual /CS mode */
  298. val &= ~PIC32_SPI_CTRL_MSSEN;
  299. writel(val, &priv->regs->ctrl);
  300. /* clear rx overflow indicator */
  301. writel(PIC32_SPI_STAT_RX_OV, &priv->regs->status.clr);
  302. }
  303. static int pic32_spi_probe(struct udevice *bus)
  304. {
  305. struct pic32_spi_priv *priv = dev_get_priv(bus);
  306. struct dm_spi_bus *dm_spi = dev_get_uclass_priv(bus);
  307. struct udevice *clkdev;
  308. fdt_addr_t addr;
  309. fdt_size_t size;
  310. int ret;
  311. debug("%s: %d, bus: %i\n", __func__, __LINE__, bus->seq);
  312. addr = fdtdec_get_addr_size(gd->fdt_blob, bus->of_offset, "reg", &size);
  313. if (addr == FDT_ADDR_T_NONE)
  314. return -EINVAL;
  315. priv->regs = ioremap(addr, size);
  316. if (!priv->regs)
  317. return -EINVAL;
  318. dm_spi->max_hz = fdtdec_get_int(gd->fdt_blob, bus->of_offset,
  319. "spi-max-frequency", 250000000);
  320. /* get clock rate */
  321. ret = clk_get_by_index(bus, 0, &clkdev);
  322. if (ret < 0) {
  323. printf("pic32-spi: error, clk not found\n");
  324. return ret;
  325. }
  326. priv->clk_rate = clk_get_periph_rate(clkdev, ret);
  327. /* initialize HW */
  328. pic32_spi_hw_init(priv);
  329. /* set word len */
  330. pic32_spi_set_word_size(priv, SPI_DEFAULT_WORDLEN);
  331. /* PIC32 SPI controller can automatically drive /CS during transfer
  332. * depending on fifo fill-level. /CS will stay asserted as long as
  333. * TX fifo is non-empty, else will be deasserted confirming completion
  334. * of the ongoing transfer. To avoid this sort of error we will drive
  335. * /CS manually by toggling cs-gpio pins.
  336. */
  337. ret = gpio_request_by_name_nodev(gd->fdt_blob, bus->of_offset,
  338. "cs-gpios", 0,
  339. &priv->cs_gpio, GPIOD_IS_OUT);
  340. if (ret) {
  341. printf("pic32-spi: error, cs-gpios not found\n");
  342. return ret;
  343. }
  344. return 0;
  345. }
  346. static const struct dm_spi_ops pic32_spi_ops = {
  347. .claim_bus = pic32_spi_claim_bus,
  348. .release_bus = pic32_spi_release_bus,
  349. .xfer = pic32_spi_xfer,
  350. .set_speed = pic32_spi_set_speed,
  351. .set_mode = pic32_spi_set_mode,
  352. .set_wordlen = pic32_spi_set_wordlen,
  353. };
  354. static const struct udevice_id pic32_spi_ids[] = {
  355. { .compatible = "microchip,pic32mzda-spi" },
  356. { }
  357. };
  358. U_BOOT_DRIVER(pic32_spi) = {
  359. .name = "pic32_spi",
  360. .id = UCLASS_SPI,
  361. .of_match = pic32_spi_ids,
  362. .ops = &pic32_spi_ops,
  363. .priv_auto_alloc_size = sizeof(struct pic32_spi_priv),
  364. .probe = pic32_spi_probe,
  365. };