spi-img-spfi.c 21 KB

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