spi-pic32.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. * Microchip PIC32 SPI controller driver.
  3. *
  4. * Purna Chandra Mandal <purna.mandal@microchip.com>
  5. * Copyright (c) 2016, Microchip Technology Inc.
  6. *
  7. * This program is free software; you can distribute it and/or modify it
  8. * under the terms of the GNU General Public License (Version 2) as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clkdev.h>
  18. #include <linux/delay.h>
  19. #include <linux/dmaengine.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/highmem.h>
  22. #include <linux/module.h>
  23. #include <linux/io.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/of.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/of_address.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/spi/spi.h>
  31. /* SPI controller registers */
  32. struct pic32_spi_regs {
  33. u32 ctrl;
  34. u32 ctrl_clr;
  35. u32 ctrl_set;
  36. u32 ctrl_inv;
  37. u32 status;
  38. u32 status_clr;
  39. u32 status_set;
  40. u32 status_inv;
  41. u32 buf;
  42. u32 dontuse[3];
  43. u32 baud;
  44. u32 dontuse2[3];
  45. u32 ctrl2;
  46. u32 ctrl2_clr;
  47. u32 ctrl2_set;
  48. u32 ctrl2_inv;
  49. };
  50. /* Bit fields of SPI Control Register */
  51. #define CTRL_RX_INT_SHIFT 0 /* Rx interrupt generation */
  52. #define RX_FIFO_EMPTY 0
  53. #define RX_FIFO_NOT_EMPTY 1 /* not empty */
  54. #define RX_FIFO_HALF_FULL 2 /* full by half or more */
  55. #define RX_FIFO_FULL 3 /* completely full */
  56. #define CTRL_TX_INT_SHIFT 2 /* TX interrupt generation */
  57. #define TX_FIFO_ALL_EMPTY 0 /* completely empty */
  58. #define TX_FIFO_EMPTY 1 /* empty */
  59. #define TX_FIFO_HALF_EMPTY 2 /* empty by half or more */
  60. #define TX_FIFO_NOT_FULL 3 /* atleast one empty */
  61. #define CTRL_MSTEN BIT(5) /* enable master mode */
  62. #define CTRL_CKP BIT(6) /* active low */
  63. #define CTRL_CKE BIT(8) /* Tx on falling edge */
  64. #define CTRL_SMP BIT(9) /* Rx at middle or end of tx */
  65. #define CTRL_BPW_MASK 0x03 /* bits per word/sample */
  66. #define CTRL_BPW_SHIFT 10
  67. #define PIC32_BPW_8 0
  68. #define PIC32_BPW_16 1
  69. #define PIC32_BPW_32 2
  70. #define CTRL_SIDL BIT(13) /* sleep when idle */
  71. #define CTRL_ON BIT(15) /* enable macro */
  72. #define CTRL_ENHBUF BIT(16) /* enable enhanced buffering */
  73. #define CTRL_MCLKSEL BIT(23) /* select clock source */
  74. #define CTRL_MSSEN BIT(28) /* macro driven /SS */
  75. #define CTRL_FRMEN BIT(31) /* enable framing mode */
  76. /* Bit fields of SPI Status Register */
  77. #define STAT_RF_EMPTY BIT(5) /* RX Fifo empty */
  78. #define STAT_RX_OV BIT(6) /* err, s/w needs to clear */
  79. #define STAT_TX_UR BIT(8) /* UR in Framed SPI modes */
  80. #define STAT_FRM_ERR BIT(12) /* Multiple Frame Sync pulse */
  81. #define STAT_TF_LVL_MASK 0x1F
  82. #define STAT_TF_LVL_SHIFT 16
  83. #define STAT_RF_LVL_MASK 0x1F
  84. #define STAT_RF_LVL_SHIFT 24
  85. /* Bit fields of SPI Baud Register */
  86. #define BAUD_MASK 0x1ff
  87. /* Bit fields of SPI Control2 Register */
  88. #define CTRL2_TX_UR_EN BIT(10) /* Enable int on Tx under-run */
  89. #define CTRL2_RX_OV_EN BIT(11) /* Enable int on Rx over-run */
  90. #define CTRL2_FRM_ERR_EN BIT(12) /* Enable frame err int */
  91. /* Minimum DMA transfer size */
  92. #define PIC32_DMA_LEN_MIN 64
  93. struct pic32_spi {
  94. dma_addr_t dma_base;
  95. struct pic32_spi_regs __iomem *regs;
  96. int fault_irq;
  97. int rx_irq;
  98. int tx_irq;
  99. u32 fifo_n_byte; /* FIFO depth in bytes */
  100. struct clk *clk;
  101. struct spi_master *master;
  102. /* Current controller setting */
  103. u32 speed_hz; /* spi-clk rate */
  104. u32 mode;
  105. u32 bits_per_word;
  106. u32 fifo_n_elm; /* FIFO depth in words */
  107. #define PIC32F_DMA_PREP 0 /* DMA chnls configured */
  108. unsigned long flags;
  109. /* Current transfer state */
  110. struct completion xfer_done;
  111. /* PIO transfer specific */
  112. const void *tx;
  113. const void *tx_end;
  114. const void *rx;
  115. const void *rx_end;
  116. int len;
  117. void (*rx_fifo)(struct pic32_spi *);
  118. void (*tx_fifo)(struct pic32_spi *);
  119. };
  120. static inline void pic32_spi_enable(struct pic32_spi *pic32s)
  121. {
  122. writel(CTRL_ON | CTRL_SIDL, &pic32s->regs->ctrl_set);
  123. }
  124. static inline void pic32_spi_disable(struct pic32_spi *pic32s)
  125. {
  126. writel(CTRL_ON | CTRL_SIDL, &pic32s->regs->ctrl_clr);
  127. /* avoid SPI registers read/write at immediate next CPU clock */
  128. ndelay(20);
  129. }
  130. static void pic32_spi_set_clk_rate(struct pic32_spi *pic32s, u32 spi_ck)
  131. {
  132. u32 div;
  133. /* div = (clk_in / 2 * spi_ck) - 1 */
  134. div = DIV_ROUND_CLOSEST(clk_get_rate(pic32s->clk), 2 * spi_ck) - 1;
  135. writel(div & BAUD_MASK, &pic32s->regs->baud);
  136. }
  137. static inline u32 pic32_rx_fifo_level(struct pic32_spi *pic32s)
  138. {
  139. u32 sr = readl(&pic32s->regs->status);
  140. return (sr >> STAT_RF_LVL_SHIFT) & STAT_RF_LVL_MASK;
  141. }
  142. static inline u32 pic32_tx_fifo_level(struct pic32_spi *pic32s)
  143. {
  144. u32 sr = readl(&pic32s->regs->status);
  145. return (sr >> STAT_TF_LVL_SHIFT) & STAT_TF_LVL_MASK;
  146. }
  147. /* Return the max entries we can fill into tx fifo */
  148. static u32 pic32_tx_max(struct pic32_spi *pic32s, int n_bytes)
  149. {
  150. u32 tx_left, tx_room, rxtx_gap;
  151. tx_left = (pic32s->tx_end - pic32s->tx) / n_bytes;
  152. tx_room = pic32s->fifo_n_elm - pic32_tx_fifo_level(pic32s);
  153. /*
  154. * Another concern is about the tx/rx mismatch, we
  155. * though to use (pic32s->fifo_n_byte - rxfl - txfl) as
  156. * one maximum value for tx, but it doesn't cover the
  157. * data which is out of tx/rx fifo and inside the
  158. * shift registers. So a ctrl from sw point of
  159. * view is taken.
  160. */
  161. rxtx_gap = ((pic32s->rx_end - pic32s->rx) -
  162. (pic32s->tx_end - pic32s->tx)) / n_bytes;
  163. return min3(tx_left, tx_room, (u32)(pic32s->fifo_n_elm - rxtx_gap));
  164. }
  165. /* Return the max entries we should read out of rx fifo */
  166. static u32 pic32_rx_max(struct pic32_spi *pic32s, int n_bytes)
  167. {
  168. u32 rx_left = (pic32s->rx_end - pic32s->rx) / n_bytes;
  169. return min_t(u32, rx_left, pic32_rx_fifo_level(pic32s));
  170. }
  171. #define BUILD_SPI_FIFO_RW(__name, __type, __bwl) \
  172. static void pic32_spi_rx_##__name(struct pic32_spi *pic32s) \
  173. { \
  174. __type v; \
  175. u32 mx = pic32_rx_max(pic32s, sizeof(__type)); \
  176. for (; mx; mx--) { \
  177. v = read##__bwl(&pic32s->regs->buf); \
  178. if (pic32s->rx_end - pic32s->len) \
  179. *(__type *)(pic32s->rx) = v; \
  180. pic32s->rx += sizeof(__type); \
  181. } \
  182. } \
  183. \
  184. static void pic32_spi_tx_##__name(struct pic32_spi *pic32s) \
  185. { \
  186. __type v; \
  187. u32 mx = pic32_tx_max(pic32s, sizeof(__type)); \
  188. for (; mx ; mx--) { \
  189. v = (__type)~0U; \
  190. if (pic32s->tx_end - pic32s->len) \
  191. v = *(__type *)(pic32s->tx); \
  192. write##__bwl(v, &pic32s->regs->buf); \
  193. pic32s->tx += sizeof(__type); \
  194. } \
  195. }
  196. BUILD_SPI_FIFO_RW(byte, u8, b);
  197. BUILD_SPI_FIFO_RW(word, u16, w);
  198. BUILD_SPI_FIFO_RW(dword, u32, l);
  199. static void pic32_err_stop(struct pic32_spi *pic32s, const char *msg)
  200. {
  201. /* disable all interrupts */
  202. disable_irq_nosync(pic32s->fault_irq);
  203. disable_irq_nosync(pic32s->rx_irq);
  204. disable_irq_nosync(pic32s->tx_irq);
  205. /* Show err message and abort xfer with err */
  206. dev_err(&pic32s->master->dev, "%s\n", msg);
  207. if (pic32s->master->cur_msg)
  208. pic32s->master->cur_msg->status = -EIO;
  209. complete(&pic32s->xfer_done);
  210. }
  211. static irqreturn_t pic32_spi_fault_irq(int irq, void *dev_id)
  212. {
  213. struct pic32_spi *pic32s = dev_id;
  214. u32 status;
  215. status = readl(&pic32s->regs->status);
  216. /* Error handling */
  217. if (status & (STAT_RX_OV | STAT_TX_UR)) {
  218. writel(STAT_RX_OV, &pic32s->regs->status_clr);
  219. writel(STAT_TX_UR, &pic32s->regs->status_clr);
  220. pic32_err_stop(pic32s, "err_irq: fifo ov/ur-run\n");
  221. return IRQ_HANDLED;
  222. }
  223. if (status & STAT_FRM_ERR) {
  224. pic32_err_stop(pic32s, "err_irq: frame error");
  225. return IRQ_HANDLED;
  226. }
  227. if (!pic32s->master->cur_msg) {
  228. pic32_err_stop(pic32s, "err_irq: no mesg");
  229. return IRQ_NONE;
  230. }
  231. return IRQ_NONE;
  232. }
  233. static irqreturn_t pic32_spi_rx_irq(int irq, void *dev_id)
  234. {
  235. struct pic32_spi *pic32s = dev_id;
  236. pic32s->rx_fifo(pic32s);
  237. /* rx complete ? */
  238. if (pic32s->rx_end == pic32s->rx) {
  239. /* disable all interrupts */
  240. disable_irq_nosync(pic32s->fault_irq);
  241. disable_irq_nosync(pic32s->rx_irq);
  242. /* complete current xfer */
  243. complete(&pic32s->xfer_done);
  244. }
  245. return IRQ_HANDLED;
  246. }
  247. static irqreturn_t pic32_spi_tx_irq(int irq, void *dev_id)
  248. {
  249. struct pic32_spi *pic32s = dev_id;
  250. pic32s->tx_fifo(pic32s);
  251. /* tx complete? disable tx interrupt */
  252. if (pic32s->tx_end == pic32s->tx)
  253. disable_irq_nosync(pic32s->tx_irq);
  254. return IRQ_HANDLED;
  255. }
  256. static void pic32_spi_dma_rx_notify(void *data)
  257. {
  258. struct pic32_spi *pic32s = data;
  259. complete(&pic32s->xfer_done);
  260. }
  261. static int pic32_spi_dma_transfer(struct pic32_spi *pic32s,
  262. struct spi_transfer *xfer)
  263. {
  264. struct spi_master *master = pic32s->master;
  265. struct dma_async_tx_descriptor *desc_rx;
  266. struct dma_async_tx_descriptor *desc_tx;
  267. dma_cookie_t cookie;
  268. int ret;
  269. if (!master->dma_rx || !master->dma_tx)
  270. return -ENODEV;
  271. desc_rx = dmaengine_prep_slave_sg(master->dma_rx,
  272. xfer->rx_sg.sgl,
  273. xfer->rx_sg.nents,
  274. DMA_DEV_TO_MEM,
  275. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  276. if (!desc_rx) {
  277. ret = -EINVAL;
  278. goto err_dma;
  279. }
  280. desc_tx = dmaengine_prep_slave_sg(master->dma_tx,
  281. xfer->tx_sg.sgl,
  282. xfer->tx_sg.nents,
  283. DMA_MEM_TO_DEV,
  284. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  285. if (!desc_tx) {
  286. ret = -EINVAL;
  287. goto err_dma;
  288. }
  289. /* Put callback on the RX transfer, that should finish last */
  290. desc_rx->callback = pic32_spi_dma_rx_notify;
  291. desc_rx->callback_param = pic32s;
  292. cookie = dmaengine_submit(desc_rx);
  293. ret = dma_submit_error(cookie);
  294. if (ret)
  295. goto err_dma;
  296. cookie = dmaengine_submit(desc_tx);
  297. ret = dma_submit_error(cookie);
  298. if (ret)
  299. goto err_dma_tx;
  300. dma_async_issue_pending(master->dma_rx);
  301. dma_async_issue_pending(master->dma_tx);
  302. return 0;
  303. err_dma_tx:
  304. dmaengine_terminate_all(master->dma_rx);
  305. err_dma:
  306. return ret;
  307. }
  308. static int pic32_spi_dma_config(struct pic32_spi *pic32s, u32 dma_width)
  309. {
  310. int buf_offset = offsetof(struct pic32_spi_regs, buf);
  311. struct spi_master *master = pic32s->master;
  312. struct dma_slave_config cfg;
  313. int ret;
  314. cfg.device_fc = true;
  315. cfg.src_addr = pic32s->dma_base + buf_offset;
  316. cfg.dst_addr = pic32s->dma_base + buf_offset;
  317. cfg.src_maxburst = pic32s->fifo_n_elm / 2; /* fill one-half */
  318. cfg.dst_maxburst = pic32s->fifo_n_elm / 2; /* drain one-half */
  319. cfg.src_addr_width = dma_width;
  320. cfg.dst_addr_width = dma_width;
  321. /* tx channel */
  322. cfg.slave_id = pic32s->tx_irq;
  323. cfg.direction = DMA_MEM_TO_DEV;
  324. ret = dmaengine_slave_config(master->dma_tx, &cfg);
  325. if (ret) {
  326. dev_err(&master->dev, "tx channel setup failed\n");
  327. return ret;
  328. }
  329. /* rx channel */
  330. cfg.slave_id = pic32s->rx_irq;
  331. cfg.direction = DMA_DEV_TO_MEM;
  332. ret = dmaengine_slave_config(master->dma_rx, &cfg);
  333. if (ret)
  334. dev_err(&master->dev, "rx channel setup failed\n");
  335. return ret;
  336. }
  337. static int pic32_spi_set_word_size(struct pic32_spi *pic32s, u8 bits_per_word)
  338. {
  339. enum dma_slave_buswidth dmawidth;
  340. u32 buswidth, v;
  341. switch (bits_per_word) {
  342. case 8:
  343. pic32s->rx_fifo = pic32_spi_rx_byte;
  344. pic32s->tx_fifo = pic32_spi_tx_byte;
  345. buswidth = PIC32_BPW_8;
  346. dmawidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  347. break;
  348. case 16:
  349. pic32s->rx_fifo = pic32_spi_rx_word;
  350. pic32s->tx_fifo = pic32_spi_tx_word;
  351. buswidth = PIC32_BPW_16;
  352. dmawidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  353. break;
  354. case 32:
  355. pic32s->rx_fifo = pic32_spi_rx_dword;
  356. pic32s->tx_fifo = pic32_spi_tx_dword;
  357. buswidth = PIC32_BPW_32;
  358. dmawidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  359. break;
  360. default:
  361. /* not supported */
  362. return -EINVAL;
  363. }
  364. /* calculate maximum number of words fifos can hold */
  365. pic32s->fifo_n_elm = DIV_ROUND_UP(pic32s->fifo_n_byte,
  366. bits_per_word / 8);
  367. /* set word size */
  368. v = readl(&pic32s->regs->ctrl);
  369. v &= ~(CTRL_BPW_MASK << CTRL_BPW_SHIFT);
  370. v |= buswidth << CTRL_BPW_SHIFT;
  371. writel(v, &pic32s->regs->ctrl);
  372. /* re-configure dma width, if required */
  373. if (test_bit(PIC32F_DMA_PREP, &pic32s->flags))
  374. pic32_spi_dma_config(pic32s, dmawidth);
  375. return 0;
  376. }
  377. static int pic32_spi_prepare_hardware(struct spi_master *master)
  378. {
  379. struct pic32_spi *pic32s = spi_master_get_devdata(master);
  380. pic32_spi_enable(pic32s);
  381. return 0;
  382. }
  383. static int pic32_spi_prepare_message(struct spi_master *master,
  384. struct spi_message *msg)
  385. {
  386. struct pic32_spi *pic32s = spi_master_get_devdata(master);
  387. struct spi_device *spi = msg->spi;
  388. u32 val;
  389. /* set device specific bits_per_word */
  390. if (pic32s->bits_per_word != spi->bits_per_word) {
  391. pic32_spi_set_word_size(pic32s, spi->bits_per_word);
  392. pic32s->bits_per_word = spi->bits_per_word;
  393. }
  394. /* device specific speed change */
  395. if (pic32s->speed_hz != spi->max_speed_hz) {
  396. pic32_spi_set_clk_rate(pic32s, spi->max_speed_hz);
  397. pic32s->speed_hz = spi->max_speed_hz;
  398. }
  399. /* device specific mode change */
  400. if (pic32s->mode != spi->mode) {
  401. val = readl(&pic32s->regs->ctrl);
  402. /* active low */
  403. if (spi->mode & SPI_CPOL)
  404. val |= CTRL_CKP;
  405. else
  406. val &= ~CTRL_CKP;
  407. /* tx on rising edge */
  408. if (spi->mode & SPI_CPHA)
  409. val &= ~CTRL_CKE;
  410. else
  411. val |= CTRL_CKE;
  412. /* rx at end of tx */
  413. val |= CTRL_SMP;
  414. writel(val, &pic32s->regs->ctrl);
  415. pic32s->mode = spi->mode;
  416. }
  417. return 0;
  418. }
  419. static bool pic32_spi_can_dma(struct spi_master *master,
  420. struct spi_device *spi,
  421. struct spi_transfer *xfer)
  422. {
  423. struct pic32_spi *pic32s = spi_master_get_devdata(master);
  424. /* skip using DMA on small size transfer to avoid overhead.*/
  425. return (xfer->len >= PIC32_DMA_LEN_MIN) &&
  426. test_bit(PIC32F_DMA_PREP, &pic32s->flags);
  427. }
  428. static int pic32_spi_one_transfer(struct spi_master *master,
  429. struct spi_device *spi,
  430. struct spi_transfer *transfer)
  431. {
  432. struct pic32_spi *pic32s;
  433. bool dma_issued = false;
  434. unsigned long timeout;
  435. int ret;
  436. pic32s = spi_master_get_devdata(master);
  437. /* handle transfer specific word size change */
  438. if (transfer->bits_per_word &&
  439. (transfer->bits_per_word != pic32s->bits_per_word)) {
  440. ret = pic32_spi_set_word_size(pic32s, transfer->bits_per_word);
  441. if (ret)
  442. return ret;
  443. pic32s->bits_per_word = transfer->bits_per_word;
  444. }
  445. /* handle transfer specific speed change */
  446. if (transfer->speed_hz && (transfer->speed_hz != pic32s->speed_hz)) {
  447. pic32_spi_set_clk_rate(pic32s, transfer->speed_hz);
  448. pic32s->speed_hz = transfer->speed_hz;
  449. }
  450. reinit_completion(&pic32s->xfer_done);
  451. /* transact by DMA mode */
  452. if (transfer->rx_sg.nents && transfer->tx_sg.nents) {
  453. ret = pic32_spi_dma_transfer(pic32s, transfer);
  454. if (ret) {
  455. dev_err(&spi->dev, "dma submit error\n");
  456. return ret;
  457. }
  458. /* DMA issued */
  459. dma_issued = true;
  460. } else {
  461. /* set current transfer information */
  462. pic32s->tx = (const void *)transfer->tx_buf;
  463. pic32s->rx = (const void *)transfer->rx_buf;
  464. pic32s->tx_end = pic32s->tx + transfer->len;
  465. pic32s->rx_end = pic32s->rx + transfer->len;
  466. pic32s->len = transfer->len;
  467. /* transact by interrupt driven PIO */
  468. enable_irq(pic32s->fault_irq);
  469. enable_irq(pic32s->rx_irq);
  470. enable_irq(pic32s->tx_irq);
  471. }
  472. /* wait for completion */
  473. timeout = wait_for_completion_timeout(&pic32s->xfer_done, 2 * HZ);
  474. if (timeout == 0) {
  475. dev_err(&spi->dev, "wait error/timedout\n");
  476. if (dma_issued) {
  477. dmaengine_terminate_all(master->dma_rx);
  478. dmaengine_terminate_all(master->dma_rx);
  479. }
  480. ret = -ETIMEDOUT;
  481. } else {
  482. ret = 0;
  483. }
  484. return ret;
  485. }
  486. static int pic32_spi_unprepare_message(struct spi_master *master,
  487. struct spi_message *msg)
  488. {
  489. /* nothing to do */
  490. return 0;
  491. }
  492. static int pic32_spi_unprepare_hardware(struct spi_master *master)
  493. {
  494. struct pic32_spi *pic32s = spi_master_get_devdata(master);
  495. pic32_spi_disable(pic32s);
  496. return 0;
  497. }
  498. /* This may be called multiple times by same spi dev */
  499. static int pic32_spi_setup(struct spi_device *spi)
  500. {
  501. if (!spi->max_speed_hz) {
  502. dev_err(&spi->dev, "No max speed HZ parameter\n");
  503. return -EINVAL;
  504. }
  505. /* PIC32 spi controller can drive /CS during transfer depending
  506. * on tx fifo fill-level. /CS will stay asserted as long as TX
  507. * fifo is non-empty, else will be deasserted indicating
  508. * completion of the ongoing transfer. This might result into
  509. * unreliable/erroneous SPI transactions.
  510. * To avoid that we will always handle /CS by toggling GPIO.
  511. */
  512. if (!gpio_is_valid(spi->cs_gpio))
  513. return -EINVAL;
  514. gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  515. return 0;
  516. }
  517. static void pic32_spi_cleanup(struct spi_device *spi)
  518. {
  519. /* de-activate cs-gpio */
  520. gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  521. }
  522. static void pic32_spi_dma_prep(struct pic32_spi *pic32s, struct device *dev)
  523. {
  524. struct spi_master *master = pic32s->master;
  525. dma_cap_mask_t mask;
  526. dma_cap_zero(mask);
  527. dma_cap_set(DMA_SLAVE, mask);
  528. master->dma_rx = dma_request_slave_channel_compat(mask, NULL, NULL,
  529. dev, "spi-rx");
  530. if (!master->dma_rx) {
  531. dev_warn(dev, "RX channel not found.\n");
  532. goto out_err;
  533. }
  534. master->dma_tx = dma_request_slave_channel_compat(mask, NULL, NULL,
  535. dev, "spi-tx");
  536. if (!master->dma_tx) {
  537. dev_warn(dev, "TX channel not found.\n");
  538. goto out_err;
  539. }
  540. if (pic32_spi_dma_config(pic32s, DMA_SLAVE_BUSWIDTH_1_BYTE))
  541. goto out_err;
  542. /* DMA chnls allocated and prepared */
  543. set_bit(PIC32F_DMA_PREP, &pic32s->flags);
  544. return;
  545. out_err:
  546. if (master->dma_rx)
  547. dma_release_channel(master->dma_rx);
  548. if (master->dma_tx)
  549. dma_release_channel(master->dma_tx);
  550. }
  551. static void pic32_spi_dma_unprep(struct pic32_spi *pic32s)
  552. {
  553. if (!test_bit(PIC32F_DMA_PREP, &pic32s->flags))
  554. return;
  555. clear_bit(PIC32F_DMA_PREP, &pic32s->flags);
  556. if (pic32s->master->dma_rx)
  557. dma_release_channel(pic32s->master->dma_rx);
  558. if (pic32s->master->dma_tx)
  559. dma_release_channel(pic32s->master->dma_tx);
  560. }
  561. static void pic32_spi_hw_init(struct pic32_spi *pic32s)
  562. {
  563. u32 ctrl;
  564. /* disable hardware */
  565. pic32_spi_disable(pic32s);
  566. ctrl = readl(&pic32s->regs->ctrl);
  567. /* enable enhanced fifo of 128bit deep */
  568. ctrl |= CTRL_ENHBUF;
  569. pic32s->fifo_n_byte = 16;
  570. /* disable framing mode */
  571. ctrl &= ~CTRL_FRMEN;
  572. /* enable master mode while disabled */
  573. ctrl |= CTRL_MSTEN;
  574. /* set tx fifo threshold interrupt */
  575. ctrl &= ~(0x3 << CTRL_TX_INT_SHIFT);
  576. ctrl |= (TX_FIFO_HALF_EMPTY << CTRL_TX_INT_SHIFT);
  577. /* set rx fifo threshold interrupt */
  578. ctrl &= ~(0x3 << CTRL_RX_INT_SHIFT);
  579. ctrl |= (RX_FIFO_NOT_EMPTY << CTRL_RX_INT_SHIFT);
  580. /* select clk source */
  581. ctrl &= ~CTRL_MCLKSEL;
  582. /* set manual /CS mode */
  583. ctrl &= ~CTRL_MSSEN;
  584. writel(ctrl, &pic32s->regs->ctrl);
  585. /* enable error reporting */
  586. ctrl = CTRL2_TX_UR_EN | CTRL2_RX_OV_EN | CTRL2_FRM_ERR_EN;
  587. writel(ctrl, &pic32s->regs->ctrl2_set);
  588. }
  589. static int pic32_spi_hw_probe(struct platform_device *pdev,
  590. struct pic32_spi *pic32s)
  591. {
  592. struct resource *mem;
  593. int ret;
  594. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  595. pic32s->regs = devm_ioremap_resource(&pdev->dev, mem);
  596. if (IS_ERR(pic32s->regs))
  597. return PTR_ERR(pic32s->regs);
  598. pic32s->dma_base = mem->start;
  599. /* get irq resources: err-irq, rx-irq, tx-irq */
  600. pic32s->fault_irq = platform_get_irq_byname(pdev, "fault");
  601. if (pic32s->fault_irq < 0) {
  602. dev_err(&pdev->dev, "fault-irq not found\n");
  603. return pic32s->fault_irq;
  604. }
  605. pic32s->rx_irq = platform_get_irq_byname(pdev, "rx");
  606. if (pic32s->rx_irq < 0) {
  607. dev_err(&pdev->dev, "rx-irq not found\n");
  608. return pic32s->rx_irq;
  609. }
  610. pic32s->tx_irq = platform_get_irq_byname(pdev, "tx");
  611. if (pic32s->tx_irq < 0) {
  612. dev_err(&pdev->dev, "tx-irq not found\n");
  613. return pic32s->tx_irq;
  614. }
  615. /* get clock */
  616. pic32s->clk = devm_clk_get(&pdev->dev, "mck0");
  617. if (IS_ERR(pic32s->clk)) {
  618. dev_err(&pdev->dev, "clk not found\n");
  619. ret = PTR_ERR(pic32s->clk);
  620. goto err_unmap_mem;
  621. }
  622. ret = clk_prepare_enable(pic32s->clk);
  623. if (ret)
  624. goto err_unmap_mem;
  625. pic32_spi_hw_init(pic32s);
  626. return 0;
  627. err_unmap_mem:
  628. dev_err(&pdev->dev, "%s failed, err %d\n", __func__, ret);
  629. return ret;
  630. }
  631. static int pic32_spi_probe(struct platform_device *pdev)
  632. {
  633. struct spi_master *master;
  634. struct pic32_spi *pic32s;
  635. int ret;
  636. master = spi_alloc_master(&pdev->dev, sizeof(*pic32s));
  637. if (!master)
  638. return -ENOMEM;
  639. pic32s = spi_master_get_devdata(master);
  640. pic32s->master = master;
  641. ret = pic32_spi_hw_probe(pdev, pic32s);
  642. if (ret)
  643. goto err_master;
  644. master->dev.of_node = of_node_get(pdev->dev.of_node);
  645. master->mode_bits = SPI_MODE_3 | SPI_MODE_0 | SPI_CS_HIGH;
  646. master->num_chipselect = 1; /* single chip-select */
  647. master->max_speed_hz = clk_get_rate(pic32s->clk);
  648. master->setup = pic32_spi_setup;
  649. master->cleanup = pic32_spi_cleanup;
  650. master->flags = SPI_MASTER_MUST_TX | SPI_MASTER_MUST_RX;
  651. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) |
  652. SPI_BPW_MASK(32);
  653. master->transfer_one = pic32_spi_one_transfer;
  654. master->prepare_message = pic32_spi_prepare_message;
  655. master->unprepare_message = pic32_spi_unprepare_message;
  656. master->prepare_transfer_hardware = pic32_spi_prepare_hardware;
  657. master->unprepare_transfer_hardware = pic32_spi_unprepare_hardware;
  658. /* optional DMA support */
  659. pic32_spi_dma_prep(pic32s, &pdev->dev);
  660. if (test_bit(PIC32F_DMA_PREP, &pic32s->flags))
  661. master->can_dma = pic32_spi_can_dma;
  662. init_completion(&pic32s->xfer_done);
  663. pic32s->mode = -1;
  664. /* install irq handlers (with irq-disabled) */
  665. irq_set_status_flags(pic32s->fault_irq, IRQ_NOAUTOEN);
  666. ret = devm_request_irq(&pdev->dev, pic32s->fault_irq,
  667. pic32_spi_fault_irq, IRQF_NO_THREAD,
  668. dev_name(&pdev->dev), pic32s);
  669. if (ret < 0) {
  670. dev_err(&pdev->dev, "request fault-irq %d\n", pic32s->rx_irq);
  671. goto err_bailout;
  672. }
  673. /* receive interrupt handler */
  674. irq_set_status_flags(pic32s->rx_irq, IRQ_NOAUTOEN);
  675. ret = devm_request_irq(&pdev->dev, pic32s->rx_irq,
  676. pic32_spi_rx_irq, IRQF_NO_THREAD,
  677. dev_name(&pdev->dev), pic32s);
  678. if (ret < 0) {
  679. dev_err(&pdev->dev, "request rx-irq %d\n", pic32s->rx_irq);
  680. goto err_bailout;
  681. }
  682. /* transmit interrupt handler */
  683. irq_set_status_flags(pic32s->tx_irq, IRQ_NOAUTOEN);
  684. ret = devm_request_irq(&pdev->dev, pic32s->tx_irq,
  685. pic32_spi_tx_irq, IRQF_NO_THREAD,
  686. dev_name(&pdev->dev), pic32s);
  687. if (ret < 0) {
  688. dev_err(&pdev->dev, "request tx-irq %d\n", pic32s->tx_irq);
  689. goto err_bailout;
  690. }
  691. /* register master */
  692. ret = devm_spi_register_master(&pdev->dev, master);
  693. if (ret) {
  694. dev_err(&master->dev, "failed registering spi master\n");
  695. goto err_bailout;
  696. }
  697. platform_set_drvdata(pdev, pic32s);
  698. return 0;
  699. err_bailout:
  700. clk_disable_unprepare(pic32s->clk);
  701. err_master:
  702. spi_master_put(master);
  703. return ret;
  704. }
  705. static int pic32_spi_remove(struct platform_device *pdev)
  706. {
  707. struct pic32_spi *pic32s;
  708. pic32s = platform_get_drvdata(pdev);
  709. pic32_spi_disable(pic32s);
  710. clk_disable_unprepare(pic32s->clk);
  711. pic32_spi_dma_unprep(pic32s);
  712. return 0;
  713. }
  714. static const struct of_device_id pic32_spi_of_match[] = {
  715. {.compatible = "microchip,pic32mzda-spi",},
  716. {},
  717. };
  718. MODULE_DEVICE_TABLE(of, pic32_spi_of_match);
  719. static struct platform_driver pic32_spi_driver = {
  720. .driver = {
  721. .name = "spi-pic32",
  722. .of_match_table = of_match_ptr(pic32_spi_of_match),
  723. },
  724. .probe = pic32_spi_probe,
  725. .remove = pic32_spi_remove,
  726. };
  727. module_platform_driver(pic32_spi_driver);
  728. MODULE_AUTHOR("Purna Chandra Mandal <purna.mandal@microchip.com>");
  729. MODULE_DESCRIPTION("Microchip SPI driver for PIC32 SPI controller.");
  730. MODULE_LICENSE("GPL v2");