spi-img-spfi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * IMG SPFI controller driver
  3. *
  4. * Copyright (C) 2007,2008,2013 Imagination Technologies Ltd.
  5. * Copyright (C) 2014 Google, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/irq.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/slab.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/spinlock.h>
  25. #define SPFI_DEVICE_PARAMETER(x) (0x00 + 0x4 * (x))
  26. #define SPFI_DEVICE_PARAMETER_BITCLK_SHIFT 24
  27. #define SPFI_DEVICE_PARAMETER_BITCLK_MASK 0xff
  28. #define SPFI_DEVICE_PARAMETER_CSSETUP_SHIFT 16
  29. #define SPFI_DEVICE_PARAMETER_CSSETUP_MASK 0xff
  30. #define SPFI_DEVICE_PARAMETER_CSHOLD_SHIFT 8
  31. #define SPFI_DEVICE_PARAMETER_CSHOLD_MASK 0xff
  32. #define SPFI_DEVICE_PARAMETER_CSDELAY_SHIFT 0
  33. #define SPFI_DEVICE_PARAMETER_CSDELAY_MASK 0xff
  34. #define SPFI_CONTROL 0x14
  35. #define SPFI_CONTROL_CONTINUE BIT(12)
  36. #define SPFI_CONTROL_SOFT_RESET BIT(11)
  37. #define SPFI_CONTROL_SEND_DMA BIT(10)
  38. #define SPFI_CONTROL_GET_DMA BIT(9)
  39. #define SPFI_CONTROL_TMODE_SHIFT 5
  40. #define SPFI_CONTROL_TMODE_MASK 0x7
  41. #define SPFI_CONTROL_TMODE_SINGLE 0
  42. #define SPFI_CONTROL_TMODE_DUAL 1
  43. #define SPFI_CONTROL_TMODE_QUAD 2
  44. #define SPFI_CONTROL_SPFI_EN BIT(0)
  45. #define SPFI_TRANSACTION 0x18
  46. #define SPFI_TRANSACTION_TSIZE_SHIFT 16
  47. #define SPFI_TRANSACTION_TSIZE_MASK 0xffff
  48. #define SPFI_PORT_STATE 0x1c
  49. #define SPFI_PORT_STATE_DEV_SEL_SHIFT 20
  50. #define SPFI_PORT_STATE_DEV_SEL_MASK 0x7
  51. #define SPFI_PORT_STATE_CK_POL(x) BIT(19 - (x))
  52. #define SPFI_PORT_STATE_CK_PHASE(x) BIT(14 - (x))
  53. #define SPFI_TX_32BIT_VALID_DATA 0x20
  54. #define SPFI_TX_8BIT_VALID_DATA 0x24
  55. #define SPFI_RX_32BIT_VALID_DATA 0x28
  56. #define SPFI_RX_8BIT_VALID_DATA 0x2c
  57. #define SPFI_INTERRUPT_STATUS 0x30
  58. #define SPFI_INTERRUPT_ENABLE 0x34
  59. #define SPFI_INTERRUPT_CLEAR 0x38
  60. #define SPFI_INTERRUPT_IACCESS BIT(12)
  61. #define SPFI_INTERRUPT_GDEX8BIT BIT(11)
  62. #define SPFI_INTERRUPT_ALLDONETRIG BIT(9)
  63. #define SPFI_INTERRUPT_GDFUL BIT(8)
  64. #define SPFI_INTERRUPT_GDHF BIT(7)
  65. #define SPFI_INTERRUPT_GDEX32BIT BIT(6)
  66. #define SPFI_INTERRUPT_GDTRIG BIT(5)
  67. #define SPFI_INTERRUPT_SDFUL BIT(3)
  68. #define SPFI_INTERRUPT_SDHF BIT(2)
  69. #define SPFI_INTERRUPT_SDE BIT(1)
  70. #define SPFI_INTERRUPT_SDTRIG BIT(0)
  71. /*
  72. * There are four parallel FIFOs of 16 bytes each. The word buffer
  73. * (*_32BIT_VALID_DATA) accesses all four FIFOs at once, resulting in an
  74. * effective FIFO size of 64 bytes. The byte buffer (*_8BIT_VALID_DATA)
  75. * accesses only a single FIFO, resulting in an effective FIFO size of
  76. * 16 bytes.
  77. */
  78. #define SPFI_32BIT_FIFO_SIZE 64
  79. #define SPFI_8BIT_FIFO_SIZE 16
  80. struct img_spfi {
  81. struct device *dev;
  82. struct spi_master *master;
  83. spinlock_t lock;
  84. void __iomem *regs;
  85. phys_addr_t phys;
  86. int irq;
  87. struct clk *spfi_clk;
  88. struct clk *sys_clk;
  89. struct dma_chan *rx_ch;
  90. struct dma_chan *tx_ch;
  91. bool tx_dma_busy;
  92. bool rx_dma_busy;
  93. };
  94. static inline u32 spfi_readl(struct img_spfi *spfi, u32 reg)
  95. {
  96. return readl(spfi->regs + reg);
  97. }
  98. static inline void spfi_writel(struct img_spfi *spfi, u32 val, u32 reg)
  99. {
  100. writel(val, spfi->regs + reg);
  101. }
  102. static inline void spfi_start(struct img_spfi *spfi)
  103. {
  104. u32 val;
  105. val = spfi_readl(spfi, SPFI_CONTROL);
  106. val |= SPFI_CONTROL_SPFI_EN;
  107. spfi_writel(spfi, val, SPFI_CONTROL);
  108. }
  109. static inline void spfi_stop(struct img_spfi *spfi)
  110. {
  111. u32 val;
  112. val = spfi_readl(spfi, SPFI_CONTROL);
  113. val &= ~SPFI_CONTROL_SPFI_EN;
  114. spfi_writel(spfi, val, SPFI_CONTROL);
  115. }
  116. static inline void spfi_reset(struct img_spfi *spfi)
  117. {
  118. spfi_writel(spfi, SPFI_CONTROL_SOFT_RESET, SPFI_CONTROL);
  119. udelay(1);
  120. spfi_writel(spfi, 0, SPFI_CONTROL);
  121. }
  122. static void spfi_flush_tx_fifo(struct img_spfi *spfi)
  123. {
  124. unsigned long timeout = jiffies + msecs_to_jiffies(10);
  125. spfi_writel(spfi, SPFI_INTERRUPT_SDE, SPFI_INTERRUPT_CLEAR);
  126. while (time_before(jiffies, timeout)) {
  127. if (spfi_readl(spfi, SPFI_INTERRUPT_STATUS) &
  128. SPFI_INTERRUPT_SDE)
  129. return;
  130. cpu_relax();
  131. }
  132. dev_err(spfi->dev, "Timed out waiting for FIFO to drain\n");
  133. spfi_reset(spfi);
  134. }
  135. static unsigned int spfi_pio_write32(struct img_spfi *spfi, const u32 *buf,
  136. unsigned int max)
  137. {
  138. unsigned int count = 0;
  139. u32 status;
  140. while (count < max) {
  141. spfi_writel(spfi, SPFI_INTERRUPT_SDFUL, SPFI_INTERRUPT_CLEAR);
  142. status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS);
  143. if (status & SPFI_INTERRUPT_SDFUL)
  144. break;
  145. spfi_writel(spfi, buf[count / 4], SPFI_TX_32BIT_VALID_DATA);
  146. count += 4;
  147. }
  148. return count;
  149. }
  150. static unsigned int spfi_pio_write8(struct img_spfi *spfi, const u8 *buf,
  151. unsigned int max)
  152. {
  153. unsigned int count = 0;
  154. u32 status;
  155. while (count < max) {
  156. spfi_writel(spfi, SPFI_INTERRUPT_SDFUL, SPFI_INTERRUPT_CLEAR);
  157. status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS);
  158. if (status & SPFI_INTERRUPT_SDFUL)
  159. break;
  160. spfi_writel(spfi, buf[count], SPFI_TX_8BIT_VALID_DATA);
  161. count++;
  162. }
  163. return count;
  164. }
  165. static unsigned int spfi_pio_read32(struct img_spfi *spfi, u32 *buf,
  166. unsigned int max)
  167. {
  168. unsigned int count = 0;
  169. u32 status;
  170. while (count < max) {
  171. spfi_writel(spfi, SPFI_INTERRUPT_GDEX32BIT,
  172. SPFI_INTERRUPT_CLEAR);
  173. status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS);
  174. if (!(status & SPFI_INTERRUPT_GDEX32BIT))
  175. break;
  176. buf[count / 4] = spfi_readl(spfi, SPFI_RX_32BIT_VALID_DATA);
  177. count += 4;
  178. }
  179. return count;
  180. }
  181. static unsigned int spfi_pio_read8(struct img_spfi *spfi, u8 *buf,
  182. unsigned int max)
  183. {
  184. unsigned int count = 0;
  185. u32 status;
  186. while (count < max) {
  187. spfi_writel(spfi, SPFI_INTERRUPT_GDEX8BIT,
  188. SPFI_INTERRUPT_CLEAR);
  189. status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS);
  190. if (!(status & SPFI_INTERRUPT_GDEX8BIT))
  191. break;
  192. buf[count] = spfi_readl(spfi, SPFI_RX_8BIT_VALID_DATA);
  193. count++;
  194. }
  195. return count;
  196. }
  197. static int img_spfi_start_pio(struct spi_master *master,
  198. struct spi_device *spi,
  199. struct spi_transfer *xfer)
  200. {
  201. struct img_spfi *spfi = spi_master_get_devdata(spi->master);
  202. unsigned int tx_bytes = 0, rx_bytes = 0;
  203. const void *tx_buf = xfer->tx_buf;
  204. void *rx_buf = xfer->rx_buf;
  205. unsigned long timeout;
  206. if (tx_buf)
  207. tx_bytes = xfer->len;
  208. if (rx_buf)
  209. rx_bytes = xfer->len;
  210. spfi_start(spfi);
  211. timeout = jiffies +
  212. msecs_to_jiffies(xfer->len * 8 * 1000 / xfer->speed_hz + 100);
  213. while ((tx_bytes > 0 || rx_bytes > 0) &&
  214. time_before(jiffies, timeout)) {
  215. unsigned int tx_count, rx_count;
  216. switch (xfer->bits_per_word) {
  217. case 32:
  218. tx_count = spfi_pio_write32(spfi, tx_buf, tx_bytes);
  219. rx_count = spfi_pio_read32(spfi, rx_buf, rx_bytes);
  220. break;
  221. case 8:
  222. default:
  223. tx_count = spfi_pio_write8(spfi, tx_buf, tx_bytes);
  224. rx_count = spfi_pio_read8(spfi, rx_buf, rx_bytes);
  225. break;
  226. }
  227. tx_buf += tx_count;
  228. rx_buf += rx_count;
  229. tx_bytes -= tx_count;
  230. rx_bytes -= rx_count;
  231. cpu_relax();
  232. }
  233. if (rx_bytes > 0 || tx_bytes > 0) {
  234. dev_err(spfi->dev, "PIO transfer timed out\n");
  235. spfi_reset(spfi);
  236. return -ETIMEDOUT;
  237. }
  238. if (tx_buf)
  239. spfi_flush_tx_fifo(spfi);
  240. spfi_stop(spfi);
  241. return 0;
  242. }
  243. static void img_spfi_dma_rx_cb(void *data)
  244. {
  245. struct img_spfi *spfi = data;
  246. unsigned long flags;
  247. spin_lock_irqsave(&spfi->lock, flags);
  248. spfi->rx_dma_busy = false;
  249. if (!spfi->tx_dma_busy) {
  250. spfi_stop(spfi);
  251. spi_finalize_current_transfer(spfi->master);
  252. }
  253. spin_unlock_irqrestore(&spfi->lock, flags);
  254. }
  255. static void img_spfi_dma_tx_cb(void *data)
  256. {
  257. struct img_spfi *spfi = data;
  258. unsigned long flags;
  259. spfi_flush_tx_fifo(spfi);
  260. spin_lock_irqsave(&spfi->lock, flags);
  261. spfi->tx_dma_busy = false;
  262. if (!spfi->rx_dma_busy) {
  263. spfi_stop(spfi);
  264. spi_finalize_current_transfer(spfi->master);
  265. }
  266. spin_unlock_irqrestore(&spfi->lock, flags);
  267. }
  268. static int img_spfi_start_dma(struct spi_master *master,
  269. struct spi_device *spi,
  270. struct spi_transfer *xfer)
  271. {
  272. struct img_spfi *spfi = spi_master_get_devdata(spi->master);
  273. struct dma_async_tx_descriptor *rxdesc = NULL, *txdesc = NULL;
  274. struct dma_slave_config rxconf, txconf;
  275. spfi->rx_dma_busy = false;
  276. spfi->tx_dma_busy = false;
  277. if (xfer->rx_buf) {
  278. rxconf.direction = DMA_DEV_TO_MEM;
  279. switch (xfer->bits_per_word) {
  280. case 32:
  281. rxconf.src_addr = spfi->phys + SPFI_RX_32BIT_VALID_DATA;
  282. rxconf.src_addr_width = 4;
  283. rxconf.src_maxburst = 4;
  284. break;
  285. case 8:
  286. default:
  287. rxconf.src_addr = spfi->phys + SPFI_RX_8BIT_VALID_DATA;
  288. rxconf.src_addr_width = 1;
  289. rxconf.src_maxburst = 4;
  290. }
  291. dmaengine_slave_config(spfi->rx_ch, &rxconf);
  292. rxdesc = dmaengine_prep_slave_sg(spfi->rx_ch, xfer->rx_sg.sgl,
  293. xfer->rx_sg.nents,
  294. DMA_DEV_TO_MEM,
  295. DMA_PREP_INTERRUPT);
  296. if (!rxdesc)
  297. goto stop_dma;
  298. rxdesc->callback = img_spfi_dma_rx_cb;
  299. rxdesc->callback_param = spfi;
  300. }
  301. if (xfer->tx_buf) {
  302. txconf.direction = DMA_MEM_TO_DEV;
  303. switch (xfer->bits_per_word) {
  304. case 32:
  305. txconf.dst_addr = spfi->phys + SPFI_TX_32BIT_VALID_DATA;
  306. txconf.dst_addr_width = 4;
  307. txconf.dst_maxburst = 4;
  308. break;
  309. case 8:
  310. default:
  311. txconf.dst_addr = spfi->phys + SPFI_TX_8BIT_VALID_DATA;
  312. txconf.dst_addr_width = 1;
  313. txconf.dst_maxburst = 4;
  314. break;
  315. }
  316. dmaengine_slave_config(spfi->tx_ch, &txconf);
  317. txdesc = dmaengine_prep_slave_sg(spfi->tx_ch, xfer->tx_sg.sgl,
  318. xfer->tx_sg.nents,
  319. DMA_MEM_TO_DEV,
  320. DMA_PREP_INTERRUPT);
  321. if (!txdesc)
  322. goto stop_dma;
  323. txdesc->callback = img_spfi_dma_tx_cb;
  324. txdesc->callback_param = spfi;
  325. }
  326. if (xfer->rx_buf) {
  327. spfi->rx_dma_busy = true;
  328. dmaengine_submit(rxdesc);
  329. dma_async_issue_pending(spfi->rx_ch);
  330. }
  331. spfi_start(spfi);
  332. if (xfer->tx_buf) {
  333. spfi->tx_dma_busy = true;
  334. dmaengine_submit(txdesc);
  335. dma_async_issue_pending(spfi->tx_ch);
  336. }
  337. return 1;
  338. stop_dma:
  339. dmaengine_terminate_all(spfi->rx_ch);
  340. dmaengine_terminate_all(spfi->tx_ch);
  341. return -EIO;
  342. }
  343. static void img_spfi_config(struct spi_master *master, struct spi_device *spi,
  344. struct spi_transfer *xfer)
  345. {
  346. struct img_spfi *spfi = spi_master_get_devdata(spi->master);
  347. u32 val, div;
  348. /*
  349. * output = spfi_clk * (BITCLK / 512), where BITCLK must be a
  350. * power of 2 up to 256 (where 255 == 256 since BITCLK is 8 bits)
  351. */
  352. div = DIV_ROUND_UP(master->max_speed_hz, xfer->speed_hz);
  353. div = clamp(512 / (1 << get_count_order(div)), 1, 255);
  354. val = spfi_readl(spfi, SPFI_DEVICE_PARAMETER(spi->chip_select));
  355. val &= ~(SPFI_DEVICE_PARAMETER_BITCLK_MASK <<
  356. SPFI_DEVICE_PARAMETER_BITCLK_SHIFT);
  357. val |= div << SPFI_DEVICE_PARAMETER_BITCLK_SHIFT;
  358. spfi_writel(spfi, val, SPFI_DEVICE_PARAMETER(spi->chip_select));
  359. val = spfi_readl(spfi, SPFI_CONTROL);
  360. val &= ~(SPFI_CONTROL_SEND_DMA | SPFI_CONTROL_GET_DMA);
  361. if (xfer->tx_buf)
  362. val |= SPFI_CONTROL_SEND_DMA;
  363. if (xfer->rx_buf)
  364. val |= SPFI_CONTROL_GET_DMA;
  365. val &= ~(SPFI_CONTROL_TMODE_MASK << SPFI_CONTROL_TMODE_SHIFT);
  366. if (xfer->tx_nbits == SPI_NBITS_DUAL &&
  367. xfer->rx_nbits == SPI_NBITS_DUAL)
  368. val |= SPFI_CONTROL_TMODE_DUAL << SPFI_CONTROL_TMODE_SHIFT;
  369. else if (xfer->tx_nbits == SPI_NBITS_QUAD &&
  370. xfer->rx_nbits == SPI_NBITS_QUAD)
  371. val |= SPFI_CONTROL_TMODE_QUAD << SPFI_CONTROL_TMODE_SHIFT;
  372. val &= ~SPFI_CONTROL_CONTINUE;
  373. if (!xfer->cs_change && !list_is_last(&xfer->transfer_list,
  374. &master->cur_msg->transfers))
  375. val |= SPFI_CONTROL_CONTINUE;
  376. spfi_writel(spfi, val, SPFI_CONTROL);
  377. val = spfi_readl(spfi, SPFI_PORT_STATE);
  378. if (spi->mode & SPI_CPHA)
  379. val |= SPFI_PORT_STATE_CK_PHASE(spi->chip_select);
  380. else
  381. val &= ~SPFI_PORT_STATE_CK_PHASE(spi->chip_select);
  382. if (spi->mode & SPI_CPOL)
  383. val |= SPFI_PORT_STATE_CK_POL(spi->chip_select);
  384. else
  385. val &= ~SPFI_PORT_STATE_CK_POL(spi->chip_select);
  386. spfi_writel(spfi, val, SPFI_PORT_STATE);
  387. spfi_writel(spfi, xfer->len << SPFI_TRANSACTION_TSIZE_SHIFT,
  388. SPFI_TRANSACTION);
  389. }
  390. static int img_spfi_transfer_one(struct spi_master *master,
  391. struct spi_device *spi,
  392. struct spi_transfer *xfer)
  393. {
  394. struct img_spfi *spfi = spi_master_get_devdata(spi->master);
  395. bool dma_reset = false;
  396. unsigned long flags;
  397. int ret;
  398. /*
  399. * Stop all DMA and reset the controller if the previous transaction
  400. * timed-out and never completed it's DMA.
  401. */
  402. spin_lock_irqsave(&spfi->lock, flags);
  403. if (spfi->tx_dma_busy || spfi->rx_dma_busy) {
  404. dev_err(spfi->dev, "SPI DMA still busy\n");
  405. dma_reset = true;
  406. }
  407. spin_unlock_irqrestore(&spfi->lock, flags);
  408. if (dma_reset) {
  409. dmaengine_terminate_all(spfi->tx_ch);
  410. dmaengine_terminate_all(spfi->rx_ch);
  411. spfi_reset(spfi);
  412. }
  413. img_spfi_config(master, spi, xfer);
  414. if (master->can_dma && master->can_dma(master, spi, xfer))
  415. ret = img_spfi_start_dma(master, spi, xfer);
  416. else
  417. ret = img_spfi_start_pio(master, spi, xfer);
  418. return ret;
  419. }
  420. static void img_spfi_set_cs(struct spi_device *spi, bool enable)
  421. {
  422. struct img_spfi *spfi = spi_master_get_devdata(spi->master);
  423. u32 val;
  424. val = spfi_readl(spfi, SPFI_PORT_STATE);
  425. val &= ~(SPFI_PORT_STATE_DEV_SEL_MASK << SPFI_PORT_STATE_DEV_SEL_SHIFT);
  426. val |= spi->chip_select << SPFI_PORT_STATE_DEV_SEL_SHIFT;
  427. spfi_writel(spfi, val, SPFI_PORT_STATE);
  428. }
  429. static bool img_spfi_can_dma(struct spi_master *master, struct spi_device *spi,
  430. struct spi_transfer *xfer)
  431. {
  432. if (xfer->bits_per_word == 8 && xfer->len > SPFI_8BIT_FIFO_SIZE)
  433. return true;
  434. if (xfer->bits_per_word == 32 && xfer->len > SPFI_32BIT_FIFO_SIZE)
  435. return true;
  436. return false;
  437. }
  438. static irqreturn_t img_spfi_irq(int irq, void *dev_id)
  439. {
  440. struct img_spfi *spfi = (struct img_spfi *)dev_id;
  441. u32 status;
  442. status = spfi_readl(spfi, SPFI_INTERRUPT_STATUS);
  443. if (status & SPFI_INTERRUPT_IACCESS) {
  444. spfi_writel(spfi, SPFI_INTERRUPT_IACCESS, SPFI_INTERRUPT_CLEAR);
  445. dev_err(spfi->dev, "Illegal access interrupt");
  446. return IRQ_HANDLED;
  447. }
  448. return IRQ_NONE;
  449. }
  450. static int img_spfi_probe(struct platform_device *pdev)
  451. {
  452. struct spi_master *master;
  453. struct img_spfi *spfi;
  454. struct resource *res;
  455. int ret;
  456. master = spi_alloc_master(&pdev->dev, sizeof(*spfi));
  457. if (!master)
  458. return -ENOMEM;
  459. platform_set_drvdata(pdev, master);
  460. spfi = spi_master_get_devdata(master);
  461. spfi->dev = &pdev->dev;
  462. spfi->master = master;
  463. spin_lock_init(&spfi->lock);
  464. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  465. spfi->regs = devm_ioremap_resource(spfi->dev, res);
  466. if (IS_ERR(spfi->regs)) {
  467. ret = PTR_ERR(spfi->regs);
  468. goto put_spi;
  469. }
  470. spfi->phys = res->start;
  471. spfi->irq = platform_get_irq(pdev, 0);
  472. if (spfi->irq < 0) {
  473. ret = spfi->irq;
  474. goto put_spi;
  475. }
  476. ret = devm_request_irq(spfi->dev, spfi->irq, img_spfi_irq,
  477. IRQ_TYPE_LEVEL_HIGH, dev_name(spfi->dev), spfi);
  478. if (ret)
  479. goto put_spi;
  480. spfi->sys_clk = devm_clk_get(spfi->dev, "sys");
  481. if (IS_ERR(spfi->sys_clk)) {
  482. ret = PTR_ERR(spfi->sys_clk);
  483. goto put_spi;
  484. }
  485. spfi->spfi_clk = devm_clk_get(spfi->dev, "spfi");
  486. if (IS_ERR(spfi->spfi_clk)) {
  487. ret = PTR_ERR(spfi->spfi_clk);
  488. goto put_spi;
  489. }
  490. ret = clk_prepare_enable(spfi->sys_clk);
  491. if (ret)
  492. goto put_spi;
  493. ret = clk_prepare_enable(spfi->spfi_clk);
  494. if (ret)
  495. goto disable_pclk;
  496. spfi_reset(spfi);
  497. /*
  498. * Only enable the error (IACCESS) interrupt. In PIO mode we'll
  499. * poll the status of the FIFOs.
  500. */
  501. spfi_writel(spfi, SPFI_INTERRUPT_IACCESS, SPFI_INTERRUPT_ENABLE);
  502. master->auto_runtime_pm = true;
  503. master->bus_num = pdev->id;
  504. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_TX_DUAL | SPI_RX_DUAL;
  505. if (of_property_read_bool(spfi->dev->of_node, "img,supports-quad-mode"))
  506. master->mode_bits |= SPI_TX_QUAD | SPI_RX_QUAD;
  507. master->num_chipselect = 5;
  508. master->dev.of_node = pdev->dev.of_node;
  509. master->bits_per_word_mask = SPI_BPW_MASK(32) | SPI_BPW_MASK(8);
  510. master->max_speed_hz = clk_get_rate(spfi->spfi_clk);
  511. master->min_speed_hz = master->max_speed_hz / 512;
  512. master->set_cs = img_spfi_set_cs;
  513. master->transfer_one = img_spfi_transfer_one;
  514. spfi->tx_ch = dma_request_slave_channel(spfi->dev, "tx");
  515. spfi->rx_ch = dma_request_slave_channel(spfi->dev, "rx");
  516. if (!spfi->tx_ch || !spfi->rx_ch) {
  517. if (spfi->tx_ch)
  518. dma_release_channel(spfi->tx_ch);
  519. if (spfi->rx_ch)
  520. dma_release_channel(spfi->rx_ch);
  521. dev_warn(spfi->dev, "Failed to get DMA channels, falling back to PIO mode\n");
  522. } else {
  523. master->dma_tx = spfi->tx_ch;
  524. master->dma_rx = spfi->rx_ch;
  525. master->can_dma = img_spfi_can_dma;
  526. }
  527. pm_runtime_set_active(spfi->dev);
  528. pm_runtime_enable(spfi->dev);
  529. ret = devm_spi_register_master(spfi->dev, master);
  530. if (ret)
  531. goto disable_pm;
  532. return 0;
  533. disable_pm:
  534. pm_runtime_disable(spfi->dev);
  535. if (spfi->rx_ch)
  536. dma_release_channel(spfi->rx_ch);
  537. if (spfi->tx_ch)
  538. dma_release_channel(spfi->tx_ch);
  539. clk_disable_unprepare(spfi->spfi_clk);
  540. disable_pclk:
  541. clk_disable_unprepare(spfi->sys_clk);
  542. put_spi:
  543. spi_master_put(master);
  544. return ret;
  545. }
  546. static int img_spfi_remove(struct platform_device *pdev)
  547. {
  548. struct spi_master *master = platform_get_drvdata(pdev);
  549. struct img_spfi *spfi = spi_master_get_devdata(master);
  550. if (spfi->tx_ch)
  551. dma_release_channel(spfi->tx_ch);
  552. if (spfi->rx_ch)
  553. dma_release_channel(spfi->rx_ch);
  554. pm_runtime_disable(spfi->dev);
  555. if (!pm_runtime_status_suspended(spfi->dev)) {
  556. clk_disable_unprepare(spfi->spfi_clk);
  557. clk_disable_unprepare(spfi->sys_clk);
  558. }
  559. spi_master_put(master);
  560. return 0;
  561. }
  562. #ifdef CONFIG_PM
  563. static int img_spfi_runtime_suspend(struct device *dev)
  564. {
  565. struct spi_master *master = dev_get_drvdata(dev);
  566. struct img_spfi *spfi = spi_master_get_devdata(master);
  567. clk_disable_unprepare(spfi->spfi_clk);
  568. clk_disable_unprepare(spfi->sys_clk);
  569. return 0;
  570. }
  571. static int img_spfi_runtime_resume(struct device *dev)
  572. {
  573. struct spi_master *master = dev_get_drvdata(dev);
  574. struct img_spfi *spfi = spi_master_get_devdata(master);
  575. int ret;
  576. ret = clk_prepare_enable(spfi->sys_clk);
  577. if (ret)
  578. return ret;
  579. ret = clk_prepare_enable(spfi->spfi_clk);
  580. if (ret) {
  581. clk_disable_unprepare(spfi->sys_clk);
  582. return ret;
  583. }
  584. return 0;
  585. }
  586. #endif /* CONFIG_PM */
  587. #ifdef CONFIG_PM_SLEEP
  588. static int img_spfi_suspend(struct device *dev)
  589. {
  590. struct spi_master *master = dev_get_drvdata(dev);
  591. return spi_master_suspend(master);
  592. }
  593. static int img_spfi_resume(struct device *dev)
  594. {
  595. struct spi_master *master = dev_get_drvdata(dev);
  596. struct img_spfi *spfi = spi_master_get_devdata(master);
  597. int ret;
  598. ret = pm_runtime_get_sync(dev);
  599. if (ret)
  600. return ret;
  601. spfi_reset(spfi);
  602. pm_runtime_put(dev);
  603. return spi_master_resume(master);
  604. }
  605. #endif /* CONFIG_PM_SLEEP */
  606. static const struct dev_pm_ops img_spfi_pm_ops = {
  607. SET_RUNTIME_PM_OPS(img_spfi_runtime_suspend, img_spfi_runtime_resume,
  608. NULL)
  609. SET_SYSTEM_SLEEP_PM_OPS(img_spfi_suspend, img_spfi_resume)
  610. };
  611. static const struct of_device_id img_spfi_of_match[] = {
  612. { .compatible = "img,spfi", },
  613. { },
  614. };
  615. MODULE_DEVICE_TABLE(of, img_spfi_of_match);
  616. static struct platform_driver img_spfi_driver = {
  617. .driver = {
  618. .name = "img-spfi",
  619. .pm = &img_spfi_pm_ops,
  620. .of_match_table = of_match_ptr(img_spfi_of_match),
  621. },
  622. .probe = img_spfi_probe,
  623. .remove = img_spfi_remove,
  624. };
  625. module_platform_driver(img_spfi_driver);
  626. MODULE_DESCRIPTION("IMG SPFI controller driver");
  627. MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
  628. MODULE_LICENSE("GPL v2");