spi-pic32-sqi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*
  2. * PIC32 Quad 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/dma-mapping.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/iopoll.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/spi/spi.h>
  26. /* SQI registers */
  27. #define PESQI_XIP_CONF1_REG 0x00
  28. #define PESQI_XIP_CONF2_REG 0x04
  29. #define PESQI_CONF_REG 0x08
  30. #define PESQI_CTRL_REG 0x0C
  31. #define PESQI_CLK_CTRL_REG 0x10
  32. #define PESQI_CMD_THRES_REG 0x14
  33. #define PESQI_INT_THRES_REG 0x18
  34. #define PESQI_INT_ENABLE_REG 0x1C
  35. #define PESQI_INT_STAT_REG 0x20
  36. #define PESQI_TX_DATA_REG 0x24
  37. #define PESQI_RX_DATA_REG 0x28
  38. #define PESQI_STAT1_REG 0x2C
  39. #define PESQI_STAT2_REG 0x30
  40. #define PESQI_BD_CTRL_REG 0x34
  41. #define PESQI_BD_CUR_ADDR_REG 0x38
  42. #define PESQI_BD_BASE_ADDR_REG 0x40
  43. #define PESQI_BD_STAT_REG 0x44
  44. #define PESQI_BD_POLL_CTRL_REG 0x48
  45. #define PESQI_BD_TX_DMA_STAT_REG 0x4C
  46. #define PESQI_BD_RX_DMA_STAT_REG 0x50
  47. #define PESQI_THRES_REG 0x54
  48. #define PESQI_INT_SIGEN_REG 0x58
  49. /* PESQI_CONF_REG fields */
  50. #define PESQI_MODE 0x7
  51. #define PESQI_MODE_BOOT 0
  52. #define PESQI_MODE_PIO 1
  53. #define PESQI_MODE_DMA 2
  54. #define PESQI_MODE_XIP 3
  55. #define PESQI_MODE_SHIFT 0
  56. #define PESQI_CPHA BIT(3)
  57. #define PESQI_CPOL BIT(4)
  58. #define PESQI_LSBF BIT(5)
  59. #define PESQI_RXLATCH BIT(7)
  60. #define PESQI_SERMODE BIT(8)
  61. #define PESQI_WP_EN BIT(9)
  62. #define PESQI_HOLD_EN BIT(10)
  63. #define PESQI_BURST_EN BIT(12)
  64. #define PESQI_CS_CTRL_HW BIT(15)
  65. #define PESQI_SOFT_RESET BIT(16)
  66. #define PESQI_LANES_SHIFT 20
  67. #define PESQI_SINGLE_LANE 0
  68. #define PESQI_DUAL_LANE 1
  69. #define PESQI_QUAD_LANE 2
  70. #define PESQI_CSEN_SHIFT 24
  71. #define PESQI_EN BIT(23)
  72. /* PESQI_CLK_CTRL_REG fields */
  73. #define PESQI_CLK_EN BIT(0)
  74. #define PESQI_CLK_STABLE BIT(1)
  75. #define PESQI_CLKDIV_SHIFT 8
  76. #define PESQI_CLKDIV 0xff
  77. /* PESQI_INT_THR/CMD_THR_REG */
  78. #define PESQI_TXTHR_MASK 0x1f
  79. #define PESQI_TXTHR_SHIFT 8
  80. #define PESQI_RXTHR_MASK 0x1f
  81. #define PESQI_RXTHR_SHIFT 0
  82. /* PESQI_INT_EN/INT_STAT/INT_SIG_EN_REG */
  83. #define PESQI_TXEMPTY BIT(0)
  84. #define PESQI_TXFULL BIT(1)
  85. #define PESQI_TXTHR BIT(2)
  86. #define PESQI_RXEMPTY BIT(3)
  87. #define PESQI_RXFULL BIT(4)
  88. #define PESQI_RXTHR BIT(5)
  89. #define PESQI_BDDONE BIT(9) /* BD processing complete */
  90. #define PESQI_PKTCOMP BIT(10) /* packet processing complete */
  91. #define PESQI_DMAERR BIT(11) /* error */
  92. /* PESQI_BD_CTRL_REG */
  93. #define PESQI_DMA_EN BIT(0) /* enable DMA engine */
  94. #define PESQI_POLL_EN BIT(1) /* enable polling */
  95. #define PESQI_BDP_START BIT(2) /* start BD processor */
  96. /* PESQI controller buffer descriptor */
  97. struct buf_desc {
  98. u32 bd_ctrl; /* control */
  99. u32 bd_status; /* reserved */
  100. u32 bd_addr; /* DMA buffer addr */
  101. u32 bd_nextp; /* next item in chain */
  102. };
  103. /* bd_ctrl */
  104. #define BD_BUFLEN 0x1ff
  105. #define BD_CBD_INT_EN BIT(16) /* Current BD is processed */
  106. #define BD_PKT_INT_EN BIT(17) /* All BDs of PKT processed */
  107. #define BD_LIFM BIT(18) /* last data of pkt */
  108. #define BD_LAST BIT(19) /* end of list */
  109. #define BD_DATA_RECV BIT(20) /* receive data */
  110. #define BD_DDR BIT(21) /* DDR mode */
  111. #define BD_DUAL BIT(22) /* Dual SPI */
  112. #define BD_QUAD BIT(23) /* Quad SPI */
  113. #define BD_LSBF BIT(25) /* LSB First */
  114. #define BD_STAT_CHECK BIT(27) /* Status poll */
  115. #define BD_DEVSEL_SHIFT 28 /* CS */
  116. #define BD_CS_DEASSERT BIT(30) /* de-assert CS after current BD */
  117. #define BD_EN BIT(31) /* BD owned by H/W */
  118. /**
  119. * struct ring_desc - Representation of SQI ring descriptor
  120. * @list: list element to add to free or used list.
  121. * @bd: PESQI controller buffer descriptor
  122. * @bd_dma: DMA address of PESQI controller buffer descriptor
  123. * @xfer_len: transfer length
  124. */
  125. struct ring_desc {
  126. struct list_head list;
  127. struct buf_desc *bd;
  128. dma_addr_t bd_dma;
  129. u32 xfer_len;
  130. };
  131. /* Global constants */
  132. #define PESQI_BD_BUF_LEN_MAX 256
  133. #define PESQI_BD_COUNT 256 /* max 64KB data per spi message */
  134. struct pic32_sqi {
  135. void __iomem *regs;
  136. struct clk *sys_clk;
  137. struct clk *base_clk; /* drives spi clock */
  138. struct spi_master *master;
  139. int irq;
  140. struct completion xfer_done;
  141. struct ring_desc *ring;
  142. void *bd;
  143. dma_addr_t bd_dma;
  144. struct list_head bd_list_free; /* free */
  145. struct list_head bd_list_used; /* allocated */
  146. struct spi_device *cur_spi;
  147. u32 cur_speed;
  148. u8 cur_mode;
  149. };
  150. static inline void pic32_setbits(void __iomem *reg, u32 set)
  151. {
  152. writel(readl(reg) | set, reg);
  153. }
  154. static inline void pic32_clrbits(void __iomem *reg, u32 clr)
  155. {
  156. writel(readl(reg) & ~clr, reg);
  157. }
  158. static int pic32_sqi_set_clk_rate(struct pic32_sqi *sqi, u32 sck)
  159. {
  160. u32 val, div;
  161. /* div = base_clk / (2 * spi_clk) */
  162. div = clk_get_rate(sqi->base_clk) / (2 * sck);
  163. div &= PESQI_CLKDIV;
  164. val = readl(sqi->regs + PESQI_CLK_CTRL_REG);
  165. /* apply new divider */
  166. val &= ~(PESQI_CLK_STABLE | (PESQI_CLKDIV << PESQI_CLKDIV_SHIFT));
  167. val |= div << PESQI_CLKDIV_SHIFT;
  168. writel(val, sqi->regs + PESQI_CLK_CTRL_REG);
  169. /* wait for stability */
  170. return readl_poll_timeout(sqi->regs + PESQI_CLK_CTRL_REG, val,
  171. val & PESQI_CLK_STABLE, 1, 5000);
  172. }
  173. static inline void pic32_sqi_enable_int(struct pic32_sqi *sqi)
  174. {
  175. u32 mask = PESQI_DMAERR | PESQI_BDDONE | PESQI_PKTCOMP;
  176. writel(mask, sqi->regs + PESQI_INT_ENABLE_REG);
  177. /* INT_SIGEN works as interrupt-gate to INTR line */
  178. writel(mask, sqi->regs + PESQI_INT_SIGEN_REG);
  179. }
  180. static inline void pic32_sqi_disable_int(struct pic32_sqi *sqi)
  181. {
  182. writel(0, sqi->regs + PESQI_INT_ENABLE_REG);
  183. writel(0, sqi->regs + PESQI_INT_SIGEN_REG);
  184. }
  185. static irqreturn_t pic32_sqi_isr(int irq, void *dev_id)
  186. {
  187. struct pic32_sqi *sqi = dev_id;
  188. u32 enable, status;
  189. enable = readl(sqi->regs + PESQI_INT_ENABLE_REG);
  190. status = readl(sqi->regs + PESQI_INT_STAT_REG);
  191. /* check spurious interrupt */
  192. if (!status)
  193. return IRQ_NONE;
  194. if (status & PESQI_DMAERR) {
  195. enable = 0;
  196. goto irq_done;
  197. }
  198. if (status & PESQI_TXTHR)
  199. enable &= ~(PESQI_TXTHR | PESQI_TXFULL | PESQI_TXEMPTY);
  200. if (status & PESQI_RXTHR)
  201. enable &= ~(PESQI_RXTHR | PESQI_RXFULL | PESQI_RXEMPTY);
  202. if (status & PESQI_BDDONE)
  203. enable &= ~PESQI_BDDONE;
  204. /* packet processing completed */
  205. if (status & PESQI_PKTCOMP) {
  206. /* mask all interrupts */
  207. enable = 0;
  208. /* complete trasaction */
  209. complete(&sqi->xfer_done);
  210. }
  211. irq_done:
  212. /* interrupts are sticky, so mask when handled */
  213. writel(enable, sqi->regs + PESQI_INT_ENABLE_REG);
  214. return IRQ_HANDLED;
  215. }
  216. static struct ring_desc *ring_desc_get(struct pic32_sqi *sqi)
  217. {
  218. struct ring_desc *rdesc;
  219. if (list_empty(&sqi->bd_list_free))
  220. return NULL;
  221. rdesc = list_first_entry(&sqi->bd_list_free, struct ring_desc, list);
  222. list_move_tail(&rdesc->list, &sqi->bd_list_used);
  223. return rdesc;
  224. }
  225. static void ring_desc_put(struct pic32_sqi *sqi, struct ring_desc *rdesc)
  226. {
  227. list_move(&rdesc->list, &sqi->bd_list_free);
  228. }
  229. static int pic32_sqi_one_transfer(struct pic32_sqi *sqi,
  230. struct spi_message *mesg,
  231. struct spi_transfer *xfer)
  232. {
  233. struct spi_device *spi = mesg->spi;
  234. struct scatterlist *sg, *sgl;
  235. struct ring_desc *rdesc;
  236. struct buf_desc *bd;
  237. int nents, i;
  238. u32 bd_ctrl;
  239. u32 nbits;
  240. /* Device selection */
  241. bd_ctrl = spi->chip_select << BD_DEVSEL_SHIFT;
  242. /* half-duplex: select transfer buffer, direction and lane */
  243. if (xfer->rx_buf) {
  244. bd_ctrl |= BD_DATA_RECV;
  245. nbits = xfer->rx_nbits;
  246. sgl = xfer->rx_sg.sgl;
  247. nents = xfer->rx_sg.nents;
  248. } else {
  249. nbits = xfer->tx_nbits;
  250. sgl = xfer->tx_sg.sgl;
  251. nents = xfer->tx_sg.nents;
  252. }
  253. if (nbits & SPI_NBITS_QUAD)
  254. bd_ctrl |= BD_QUAD;
  255. else if (nbits & SPI_NBITS_DUAL)
  256. bd_ctrl |= BD_DUAL;
  257. /* LSB first */
  258. if (spi->mode & SPI_LSB_FIRST)
  259. bd_ctrl |= BD_LSBF;
  260. /* ownership to hardware */
  261. bd_ctrl |= BD_EN;
  262. for_each_sg(sgl, sg, nents, i) {
  263. /* get ring descriptor */
  264. rdesc = ring_desc_get(sqi);
  265. if (!rdesc)
  266. break;
  267. bd = rdesc->bd;
  268. /* BD CTRL: length */
  269. rdesc->xfer_len = sg_dma_len(sg);
  270. bd->bd_ctrl = bd_ctrl;
  271. bd->bd_ctrl |= rdesc->xfer_len;
  272. /* BD STAT */
  273. bd->bd_status = 0;
  274. /* BD BUFFER ADDRESS */
  275. bd->bd_addr = sg->dma_address;
  276. }
  277. return 0;
  278. }
  279. static int pic32_sqi_prepare_hardware(struct spi_master *master)
  280. {
  281. struct pic32_sqi *sqi = spi_master_get_devdata(master);
  282. /* enable spi interface */
  283. pic32_setbits(sqi->regs + PESQI_CONF_REG, PESQI_EN);
  284. /* enable spi clk */
  285. pic32_setbits(sqi->regs + PESQI_CLK_CTRL_REG, PESQI_CLK_EN);
  286. return 0;
  287. }
  288. static bool pic32_sqi_can_dma(struct spi_master *master,
  289. struct spi_device *spi,
  290. struct spi_transfer *x)
  291. {
  292. /* Do DMA irrespective of transfer size */
  293. return true;
  294. }
  295. static int pic32_sqi_one_message(struct spi_master *master,
  296. struct spi_message *msg)
  297. {
  298. struct spi_device *spi = msg->spi;
  299. struct ring_desc *rdesc, *next;
  300. struct spi_transfer *xfer;
  301. struct pic32_sqi *sqi;
  302. int ret = 0, mode;
  303. unsigned long timeout;
  304. u32 val;
  305. sqi = spi_master_get_devdata(master);
  306. reinit_completion(&sqi->xfer_done);
  307. msg->actual_length = 0;
  308. /* We can't handle spi_transfer specific "speed_hz", "bits_per_word"
  309. * and "delay_usecs". But spi_device specific speed and mode change
  310. * can be handled at best during spi chip-select switch.
  311. */
  312. if (sqi->cur_spi != spi) {
  313. /* set spi speed */
  314. if (sqi->cur_speed != spi->max_speed_hz) {
  315. sqi->cur_speed = spi->max_speed_hz;
  316. ret = pic32_sqi_set_clk_rate(sqi, spi->max_speed_hz);
  317. if (ret)
  318. dev_warn(&spi->dev, "set_clk, %d\n", ret);
  319. }
  320. /* set spi mode */
  321. mode = spi->mode & (SPI_MODE_3 | SPI_LSB_FIRST);
  322. if (sqi->cur_mode != mode) {
  323. val = readl(sqi->regs + PESQI_CONF_REG);
  324. val &= ~(PESQI_CPOL | PESQI_CPHA | PESQI_LSBF);
  325. if (mode & SPI_CPOL)
  326. val |= PESQI_CPOL;
  327. if (mode & SPI_LSB_FIRST)
  328. val |= PESQI_LSBF;
  329. val |= PESQI_CPHA;
  330. writel(val, sqi->regs + PESQI_CONF_REG);
  331. sqi->cur_mode = mode;
  332. }
  333. sqi->cur_spi = spi;
  334. }
  335. /* prepare hardware desc-list(BD) for transfer(s) */
  336. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  337. ret = pic32_sqi_one_transfer(sqi, msg, xfer);
  338. if (ret) {
  339. dev_err(&spi->dev, "xfer %p err\n", xfer);
  340. goto xfer_out;
  341. }
  342. }
  343. /* BDs are prepared and chained. Now mark LAST_BD, CS_DEASSERT at last
  344. * element of the list.
  345. */
  346. rdesc = list_last_entry(&sqi->bd_list_used, struct ring_desc, list);
  347. rdesc->bd->bd_ctrl |= BD_LAST | BD_CS_DEASSERT |
  348. BD_LIFM | BD_PKT_INT_EN;
  349. /* set base address BD list for DMA engine */
  350. rdesc = list_first_entry(&sqi->bd_list_used, struct ring_desc, list);
  351. writel(rdesc->bd_dma, sqi->regs + PESQI_BD_BASE_ADDR_REG);
  352. /* enable interrupt */
  353. pic32_sqi_enable_int(sqi);
  354. /* enable DMA engine */
  355. val = PESQI_DMA_EN | PESQI_POLL_EN | PESQI_BDP_START;
  356. writel(val, sqi->regs + PESQI_BD_CTRL_REG);
  357. /* wait for xfer completion */
  358. timeout = wait_for_completion_timeout(&sqi->xfer_done, 5 * HZ);
  359. if (timeout == 0) {
  360. dev_err(&sqi->master->dev, "wait timedout/interrupted\n");
  361. ret = -ETIMEDOUT;
  362. msg->status = ret;
  363. } else {
  364. /* success */
  365. msg->status = 0;
  366. ret = 0;
  367. }
  368. /* disable DMA */
  369. writel(0, sqi->regs + PESQI_BD_CTRL_REG);
  370. pic32_sqi_disable_int(sqi);
  371. xfer_out:
  372. list_for_each_entry_safe_reverse(rdesc, next,
  373. &sqi->bd_list_used, list) {
  374. /* Update total byte transferred */
  375. msg->actual_length += rdesc->xfer_len;
  376. /* release ring descr */
  377. ring_desc_put(sqi, rdesc);
  378. }
  379. spi_finalize_current_message(spi->master);
  380. return ret;
  381. }
  382. static int pic32_sqi_unprepare_hardware(struct spi_master *master)
  383. {
  384. struct pic32_sqi *sqi = spi_master_get_devdata(master);
  385. /* disable clk */
  386. pic32_clrbits(sqi->regs + PESQI_CLK_CTRL_REG, PESQI_CLK_EN);
  387. /* disable spi */
  388. pic32_clrbits(sqi->regs + PESQI_CONF_REG, PESQI_EN);
  389. return 0;
  390. }
  391. static int ring_desc_ring_alloc(struct pic32_sqi *sqi)
  392. {
  393. struct ring_desc *rdesc;
  394. struct buf_desc *bd;
  395. int i;
  396. /* allocate coherent DMAable memory for hardware buffer descriptors. */
  397. sqi->bd = dma_zalloc_coherent(&sqi->master->dev,
  398. sizeof(*bd) * PESQI_BD_COUNT,
  399. &sqi->bd_dma, GFP_DMA32);
  400. if (!sqi->bd) {
  401. dev_err(&sqi->master->dev, "failed allocating dma buffer\n");
  402. return -ENOMEM;
  403. }
  404. /* allocate software ring descriptors */
  405. sqi->ring = kcalloc(PESQI_BD_COUNT, sizeof(*rdesc), GFP_KERNEL);
  406. if (!sqi->ring) {
  407. dma_free_coherent(&sqi->master->dev,
  408. sizeof(*bd) * PESQI_BD_COUNT,
  409. sqi->bd, sqi->bd_dma);
  410. return -ENOMEM;
  411. }
  412. bd = (struct buf_desc *)sqi->bd;
  413. INIT_LIST_HEAD(&sqi->bd_list_free);
  414. INIT_LIST_HEAD(&sqi->bd_list_used);
  415. /* initialize ring-desc */
  416. for (i = 0, rdesc = sqi->ring; i < PESQI_BD_COUNT; i++, rdesc++) {
  417. INIT_LIST_HEAD(&rdesc->list);
  418. rdesc->bd = &bd[i];
  419. rdesc->bd_dma = sqi->bd_dma + (void *)&bd[i] - (void *)bd;
  420. list_add_tail(&rdesc->list, &sqi->bd_list_free);
  421. }
  422. /* Prepare BD: chain to next BD(s) */
  423. for (i = 0, rdesc = sqi->ring; i < PESQI_BD_COUNT - 1; i++)
  424. bd[i].bd_nextp = rdesc[i + 1].bd_dma;
  425. bd[PESQI_BD_COUNT - 1].bd_nextp = 0;
  426. return 0;
  427. }
  428. static void ring_desc_ring_free(struct pic32_sqi *sqi)
  429. {
  430. dma_free_coherent(&sqi->master->dev,
  431. sizeof(struct buf_desc) * PESQI_BD_COUNT,
  432. sqi->bd, sqi->bd_dma);
  433. kfree(sqi->ring);
  434. }
  435. static void pic32_sqi_hw_init(struct pic32_sqi *sqi)
  436. {
  437. unsigned long flags;
  438. u32 val;
  439. /* Soft-reset of PESQI controller triggers interrupt.
  440. * We are not yet ready to handle them so disable CPU
  441. * interrupt for the time being.
  442. */
  443. local_irq_save(flags);
  444. /* assert soft-reset */
  445. writel(PESQI_SOFT_RESET, sqi->regs + PESQI_CONF_REG);
  446. /* wait until clear */
  447. readl_poll_timeout_atomic(sqi->regs + PESQI_CONF_REG, val,
  448. !(val & PESQI_SOFT_RESET), 1, 5000);
  449. /* disable all interrupts */
  450. pic32_sqi_disable_int(sqi);
  451. /* Now it is safe to enable back CPU interrupt */
  452. local_irq_restore(flags);
  453. /* tx and rx fifo interrupt threshold */
  454. val = readl(sqi->regs + PESQI_CMD_THRES_REG);
  455. val &= ~(PESQI_TXTHR_MASK << PESQI_TXTHR_SHIFT);
  456. val &= ~(PESQI_RXTHR_MASK << PESQI_RXTHR_SHIFT);
  457. val |= (1U << PESQI_TXTHR_SHIFT) | (1U << PESQI_RXTHR_SHIFT);
  458. writel(val, sqi->regs + PESQI_CMD_THRES_REG);
  459. val = readl(sqi->regs + PESQI_INT_THRES_REG);
  460. val &= ~(PESQI_TXTHR_MASK << PESQI_TXTHR_SHIFT);
  461. val &= ~(PESQI_RXTHR_MASK << PESQI_RXTHR_SHIFT);
  462. val |= (1U << PESQI_TXTHR_SHIFT) | (1U << PESQI_RXTHR_SHIFT);
  463. writel(val, sqi->regs + PESQI_INT_THRES_REG);
  464. /* default configuration */
  465. val = readl(sqi->regs + PESQI_CONF_REG);
  466. /* set mode: DMA */
  467. val &= ~PESQI_MODE;
  468. val |= PESQI_MODE_DMA << PESQI_MODE_SHIFT;
  469. writel(val, sqi->regs + PESQI_CONF_REG);
  470. /* DATAEN - SQIID0-ID3 */
  471. val |= PESQI_QUAD_LANE << PESQI_LANES_SHIFT;
  472. /* burst/INCR4 enable */
  473. val |= PESQI_BURST_EN;
  474. /* CSEN - all CS */
  475. val |= 3U << PESQI_CSEN_SHIFT;
  476. writel(val, sqi->regs + PESQI_CONF_REG);
  477. /* write poll count */
  478. writel(0, sqi->regs + PESQI_BD_POLL_CTRL_REG);
  479. sqi->cur_speed = 0;
  480. sqi->cur_mode = -1;
  481. }
  482. static int pic32_sqi_probe(struct platform_device *pdev)
  483. {
  484. struct spi_master *master;
  485. struct pic32_sqi *sqi;
  486. struct resource *reg;
  487. int ret;
  488. master = spi_alloc_master(&pdev->dev, sizeof(*sqi));
  489. if (!master)
  490. return -ENOMEM;
  491. sqi = spi_master_get_devdata(master);
  492. sqi->master = master;
  493. reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  494. sqi->regs = devm_ioremap_resource(&pdev->dev, reg);
  495. if (IS_ERR(sqi->regs)) {
  496. ret = PTR_ERR(sqi->regs);
  497. goto err_free_master;
  498. }
  499. /* irq */
  500. sqi->irq = platform_get_irq(pdev, 0);
  501. if (sqi->irq < 0) {
  502. dev_err(&pdev->dev, "no irq found\n");
  503. ret = sqi->irq;
  504. goto err_free_master;
  505. }
  506. /* clocks */
  507. sqi->sys_clk = devm_clk_get(&pdev->dev, "reg_ck");
  508. if (IS_ERR(sqi->sys_clk)) {
  509. ret = PTR_ERR(sqi->sys_clk);
  510. dev_err(&pdev->dev, "no sys_clk ?\n");
  511. goto err_free_master;
  512. }
  513. sqi->base_clk = devm_clk_get(&pdev->dev, "spi_ck");
  514. if (IS_ERR(sqi->base_clk)) {
  515. ret = PTR_ERR(sqi->base_clk);
  516. dev_err(&pdev->dev, "no base clk ?\n");
  517. goto err_free_master;
  518. }
  519. ret = clk_prepare_enable(sqi->sys_clk);
  520. if (ret) {
  521. dev_err(&pdev->dev, "sys clk enable failed\n");
  522. goto err_free_master;
  523. }
  524. ret = clk_prepare_enable(sqi->base_clk);
  525. if (ret) {
  526. dev_err(&pdev->dev, "base clk enable failed\n");
  527. clk_disable_unprepare(sqi->sys_clk);
  528. goto err_free_master;
  529. }
  530. init_completion(&sqi->xfer_done);
  531. /* initialize hardware */
  532. pic32_sqi_hw_init(sqi);
  533. /* allocate buffers & descriptors */
  534. ret = ring_desc_ring_alloc(sqi);
  535. if (ret) {
  536. dev_err(&pdev->dev, "ring alloc failed\n");
  537. goto err_disable_clk;
  538. }
  539. /* install irq handlers */
  540. ret = request_irq(sqi->irq, pic32_sqi_isr, 0,
  541. dev_name(&pdev->dev), sqi);
  542. if (ret < 0) {
  543. dev_err(&pdev->dev, "request_irq(%d), failed\n", sqi->irq);
  544. goto err_free_ring;
  545. }
  546. /* register master */
  547. master->num_chipselect = 2;
  548. master->max_speed_hz = clk_get_rate(sqi->base_clk);
  549. master->dma_alignment = 32;
  550. master->max_dma_len = PESQI_BD_BUF_LEN_MAX;
  551. master->dev.of_node = of_node_get(pdev->dev.of_node);
  552. master->mode_bits = SPI_MODE_3 | SPI_MODE_0 | SPI_TX_DUAL |
  553. SPI_RX_DUAL | SPI_TX_QUAD | SPI_RX_QUAD;
  554. master->flags = SPI_MASTER_HALF_DUPLEX;
  555. master->can_dma = pic32_sqi_can_dma;
  556. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32);
  557. master->transfer_one_message = pic32_sqi_one_message;
  558. master->prepare_transfer_hardware = pic32_sqi_prepare_hardware;
  559. master->unprepare_transfer_hardware = pic32_sqi_unprepare_hardware;
  560. ret = devm_spi_register_master(&pdev->dev, master);
  561. if (ret) {
  562. dev_err(&master->dev, "failed registering spi master\n");
  563. free_irq(sqi->irq, sqi);
  564. goto err_free_ring;
  565. }
  566. platform_set_drvdata(pdev, sqi);
  567. return 0;
  568. err_free_ring:
  569. ring_desc_ring_free(sqi);
  570. err_disable_clk:
  571. clk_disable_unprepare(sqi->base_clk);
  572. clk_disable_unprepare(sqi->sys_clk);
  573. err_free_master:
  574. spi_master_put(master);
  575. return ret;
  576. }
  577. static int pic32_sqi_remove(struct platform_device *pdev)
  578. {
  579. struct pic32_sqi *sqi = platform_get_drvdata(pdev);
  580. /* release resources */
  581. free_irq(sqi->irq, sqi);
  582. ring_desc_ring_free(sqi);
  583. /* disable clk */
  584. clk_disable_unprepare(sqi->base_clk);
  585. clk_disable_unprepare(sqi->sys_clk);
  586. return 0;
  587. }
  588. static const struct of_device_id pic32_sqi_of_ids[] = {
  589. {.compatible = "microchip,pic32mzda-sqi",},
  590. {},
  591. };
  592. MODULE_DEVICE_TABLE(of, pic32_sqi_of_ids);
  593. static struct platform_driver pic32_sqi_driver = {
  594. .driver = {
  595. .name = "sqi-pic32",
  596. .of_match_table = of_match_ptr(pic32_sqi_of_ids),
  597. },
  598. .probe = pic32_sqi_probe,
  599. .remove = pic32_sqi_remove,
  600. };
  601. module_platform_driver(pic32_sqi_driver);
  602. MODULE_AUTHOR("Purna Chandra Mandal <purna.mandal@microchip.com>");
  603. MODULE_DESCRIPTION("Microchip SPI driver for PIC32 SQI controller.");
  604. MODULE_LICENSE("GPL v2");