spi-img-spfi.c 19 KB

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