spi-pxa2xx-pxadma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * PXA2xx SPI private DMA support.
  3. *
  4. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/pxa2xx_ssp.h>
  25. #include <linux/spi/spi.h>
  26. #include <linux/spi/pxa2xx_spi.h>
  27. #include "spi-pxa2xx.h"
  28. #define DMA_INT_MASK (DCSR_ENDINTR | DCSR_STARTINTR | DCSR_BUSERR)
  29. #define RESET_DMA_CHANNEL (DCSR_NODESC | DMA_INT_MASK)
  30. bool pxa2xx_spi_dma_is_possible(size_t len)
  31. {
  32. /* Try to map dma buffer and do a dma transfer if successful, but
  33. * only if the length is non-zero and less than MAX_DMA_LEN.
  34. *
  35. * Zero-length non-descriptor DMA is illegal on PXA2xx; force use
  36. * of PIO instead. Care is needed above because the transfer may
  37. * have have been passed with buffers that are already dma mapped.
  38. * A zero-length transfer in PIO mode will not try to write/read
  39. * to/from the buffers
  40. *
  41. * REVISIT large transfers are exactly where we most want to be
  42. * using DMA. If this happens much, split those transfers into
  43. * multiple DMA segments rather than forcing PIO.
  44. */
  45. return len > 0 && len <= MAX_DMA_LEN;
  46. }
  47. int pxa2xx_spi_map_dma_buffers(struct driver_data *drv_data)
  48. {
  49. struct spi_message *msg = drv_data->cur_msg;
  50. struct device *dev = &msg->spi->dev;
  51. if (!drv_data->cur_chip->enable_dma)
  52. return 0;
  53. if (msg->is_dma_mapped)
  54. return drv_data->rx_dma && drv_data->tx_dma;
  55. if (!IS_DMA_ALIGNED(drv_data->rx) || !IS_DMA_ALIGNED(drv_data->tx))
  56. return 0;
  57. /* Modify setup if rx buffer is null */
  58. if (drv_data->rx == NULL) {
  59. *drv_data->null_dma_buf = 0;
  60. drv_data->rx = drv_data->null_dma_buf;
  61. drv_data->rx_map_len = 4;
  62. } else
  63. drv_data->rx_map_len = drv_data->len;
  64. /* Modify setup if tx buffer is null */
  65. if (drv_data->tx == NULL) {
  66. *drv_data->null_dma_buf = 0;
  67. drv_data->tx = drv_data->null_dma_buf;
  68. drv_data->tx_map_len = 4;
  69. } else
  70. drv_data->tx_map_len = drv_data->len;
  71. /* Stream map the tx buffer. Always do DMA_TO_DEVICE first
  72. * so we flush the cache *before* invalidating it, in case
  73. * the tx and rx buffers overlap.
  74. */
  75. drv_data->tx_dma = dma_map_single(dev, drv_data->tx,
  76. drv_data->tx_map_len, DMA_TO_DEVICE);
  77. if (dma_mapping_error(dev, drv_data->tx_dma))
  78. return 0;
  79. /* Stream map the rx buffer */
  80. drv_data->rx_dma = dma_map_single(dev, drv_data->rx,
  81. drv_data->rx_map_len, DMA_FROM_DEVICE);
  82. if (dma_mapping_error(dev, drv_data->rx_dma)) {
  83. dma_unmap_single(dev, drv_data->tx_dma,
  84. drv_data->tx_map_len, DMA_TO_DEVICE);
  85. return 0;
  86. }
  87. return 1;
  88. }
  89. static void pxa2xx_spi_unmap_dma_buffers(struct driver_data *drv_data)
  90. {
  91. struct device *dev;
  92. if (!drv_data->dma_mapped)
  93. return;
  94. if (!drv_data->cur_msg->is_dma_mapped) {
  95. dev = &drv_data->cur_msg->spi->dev;
  96. dma_unmap_single(dev, drv_data->rx_dma,
  97. drv_data->rx_map_len, DMA_FROM_DEVICE);
  98. dma_unmap_single(dev, drv_data->tx_dma,
  99. drv_data->tx_map_len, DMA_TO_DEVICE);
  100. }
  101. drv_data->dma_mapped = 0;
  102. }
  103. static int wait_ssp_rx_stall(void const __iomem *ioaddr)
  104. {
  105. unsigned long limit = loops_per_jiffy << 1;
  106. while ((read_SSSR(ioaddr) & SSSR_BSY) && --limit)
  107. cpu_relax();
  108. return limit;
  109. }
  110. static int wait_dma_channel_stop(int channel)
  111. {
  112. unsigned long limit = loops_per_jiffy << 1;
  113. while (!(DCSR(channel) & DCSR_STOPSTATE) && --limit)
  114. cpu_relax();
  115. return limit;
  116. }
  117. static void pxa2xx_spi_dma_error_stop(struct driver_data *drv_data,
  118. const char *msg)
  119. {
  120. void __iomem *reg = drv_data->ioaddr;
  121. /* Stop and reset */
  122. DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
  123. DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
  124. write_SSSR_CS(drv_data, drv_data->clear_sr);
  125. write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
  126. if (!pxa25x_ssp_comp(drv_data))
  127. write_SSTO(0, reg);
  128. pxa2xx_spi_flush(drv_data);
  129. write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
  130. pxa2xx_spi_unmap_dma_buffers(drv_data);
  131. dev_err(&drv_data->pdev->dev, "%s\n", msg);
  132. drv_data->cur_msg->state = ERROR_STATE;
  133. tasklet_schedule(&drv_data->pump_transfers);
  134. }
  135. static void pxa2xx_spi_dma_transfer_complete(struct driver_data *drv_data)
  136. {
  137. void __iomem *reg = drv_data->ioaddr;
  138. struct spi_message *msg = drv_data->cur_msg;
  139. /* Clear and disable interrupts on SSP and DMA channels*/
  140. write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
  141. write_SSSR_CS(drv_data, drv_data->clear_sr);
  142. DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
  143. DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
  144. if (wait_dma_channel_stop(drv_data->rx_channel) == 0)
  145. dev_err(&drv_data->pdev->dev,
  146. "dma_handler: dma rx channel stop failed\n");
  147. if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
  148. dev_err(&drv_data->pdev->dev,
  149. "dma_transfer: ssp rx stall failed\n");
  150. pxa2xx_spi_unmap_dma_buffers(drv_data);
  151. /* update the buffer pointer for the amount completed in dma */
  152. drv_data->rx += drv_data->len -
  153. (DCMD(drv_data->rx_channel) & DCMD_LENGTH);
  154. /* read trailing data from fifo, it does not matter how many
  155. * bytes are in the fifo just read until buffer is full
  156. * or fifo is empty, which ever occurs first */
  157. drv_data->read(drv_data);
  158. /* return count of what was actually read */
  159. msg->actual_length += drv_data->len -
  160. (drv_data->rx_end - drv_data->rx);
  161. /* Transfer delays and chip select release are
  162. * handled in pump_transfers or giveback
  163. */
  164. /* Move to next transfer */
  165. msg->state = pxa2xx_spi_next_transfer(drv_data);
  166. /* Schedule transfer tasklet */
  167. tasklet_schedule(&drv_data->pump_transfers);
  168. }
  169. void pxa2xx_spi_dma_handler(int channel, void *data)
  170. {
  171. struct driver_data *drv_data = data;
  172. u32 irq_status = DCSR(channel) & DMA_INT_MASK;
  173. if (irq_status & DCSR_BUSERR) {
  174. if (channel == drv_data->tx_channel)
  175. pxa2xx_spi_dma_error_stop(drv_data,
  176. "dma_handler: bad bus address on tx channel");
  177. else
  178. pxa2xx_spi_dma_error_stop(drv_data,
  179. "dma_handler: bad bus address on rx channel");
  180. return;
  181. }
  182. /* PXA255x_SSP has no timeout interrupt, wait for tailing bytes */
  183. if ((channel == drv_data->tx_channel)
  184. && (irq_status & DCSR_ENDINTR)
  185. && (drv_data->ssp_type == PXA25x_SSP)) {
  186. /* Wait for rx to stall */
  187. if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
  188. dev_err(&drv_data->pdev->dev,
  189. "dma_handler: ssp rx stall failed\n");
  190. /* finish this transfer, start the next */
  191. pxa2xx_spi_dma_transfer_complete(drv_data);
  192. }
  193. }
  194. irqreturn_t pxa2xx_spi_dma_transfer(struct driver_data *drv_data)
  195. {
  196. u32 irq_status;
  197. void __iomem *reg = drv_data->ioaddr;
  198. irq_status = read_SSSR(reg) & drv_data->mask_sr;
  199. if (irq_status & SSSR_ROR) {
  200. pxa2xx_spi_dma_error_stop(drv_data,
  201. "dma_transfer: fifo overrun");
  202. return IRQ_HANDLED;
  203. }
  204. /* Check for false positive timeout */
  205. if ((irq_status & SSSR_TINT)
  206. && (DCSR(drv_data->tx_channel) & DCSR_RUN)) {
  207. write_SSSR(SSSR_TINT, reg);
  208. return IRQ_HANDLED;
  209. }
  210. if (irq_status & SSSR_TINT || drv_data->rx == drv_data->rx_end) {
  211. /* Clear and disable timeout interrupt, do the rest in
  212. * dma_transfer_complete */
  213. if (!pxa25x_ssp_comp(drv_data))
  214. write_SSTO(0, reg);
  215. /* finish this transfer, start the next */
  216. pxa2xx_spi_dma_transfer_complete(drv_data);
  217. return IRQ_HANDLED;
  218. }
  219. /* Opps problem detected */
  220. return IRQ_NONE;
  221. }
  222. int pxa2xx_spi_dma_prepare(struct driver_data *drv_data, u32 dma_burst)
  223. {
  224. u32 dma_width;
  225. switch (drv_data->n_bytes) {
  226. case 1:
  227. dma_width = DCMD_WIDTH1;
  228. break;
  229. case 2:
  230. dma_width = DCMD_WIDTH2;
  231. break;
  232. default:
  233. dma_width = DCMD_WIDTH4;
  234. break;
  235. }
  236. /* Setup rx DMA Channel */
  237. DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
  238. DSADR(drv_data->rx_channel) = drv_data->ssdr_physical;
  239. DTADR(drv_data->rx_channel) = drv_data->rx_dma;
  240. if (drv_data->rx == drv_data->null_dma_buf)
  241. /* No target address increment */
  242. DCMD(drv_data->rx_channel) = DCMD_FLOWSRC
  243. | dma_width
  244. | dma_burst
  245. | drv_data->len;
  246. else
  247. DCMD(drv_data->rx_channel) = DCMD_INCTRGADDR
  248. | DCMD_FLOWSRC
  249. | dma_width
  250. | dma_burst
  251. | drv_data->len;
  252. /* Setup tx DMA Channel */
  253. DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
  254. DSADR(drv_data->tx_channel) = drv_data->tx_dma;
  255. DTADR(drv_data->tx_channel) = drv_data->ssdr_physical;
  256. if (drv_data->tx == drv_data->null_dma_buf)
  257. /* No source address increment */
  258. DCMD(drv_data->tx_channel) = DCMD_FLOWTRG
  259. | dma_width
  260. | dma_burst
  261. | drv_data->len;
  262. else
  263. DCMD(drv_data->tx_channel) = DCMD_INCSRCADDR
  264. | DCMD_FLOWTRG
  265. | dma_width
  266. | dma_burst
  267. | drv_data->len;
  268. /* Enable dma end irqs on SSP to detect end of transfer */
  269. if (drv_data->ssp_type == PXA25x_SSP)
  270. DCMD(drv_data->tx_channel) |= DCMD_ENDIRQEN;
  271. return 0;
  272. }
  273. void pxa2xx_spi_dma_start(struct driver_data *drv_data)
  274. {
  275. DCSR(drv_data->rx_channel) |= DCSR_RUN;
  276. DCSR(drv_data->tx_channel) |= DCSR_RUN;
  277. }
  278. int pxa2xx_spi_dma_setup(struct driver_data *drv_data)
  279. {
  280. struct device *dev = &drv_data->pdev->dev;
  281. struct ssp_device *ssp = drv_data->ssp;
  282. /* Get two DMA channels (rx and tx) */
  283. drv_data->rx_channel = pxa_request_dma("pxa2xx_spi_ssp_rx",
  284. DMA_PRIO_HIGH,
  285. pxa2xx_spi_dma_handler,
  286. drv_data);
  287. if (drv_data->rx_channel < 0) {
  288. dev_err(dev, "problem (%d) requesting rx channel\n",
  289. drv_data->rx_channel);
  290. return -ENODEV;
  291. }
  292. drv_data->tx_channel = pxa_request_dma("pxa2xx_spi_ssp_tx",
  293. DMA_PRIO_MEDIUM,
  294. pxa2xx_spi_dma_handler,
  295. drv_data);
  296. if (drv_data->tx_channel < 0) {
  297. dev_err(dev, "problem (%d) requesting tx channel\n",
  298. drv_data->tx_channel);
  299. pxa_free_dma(drv_data->rx_channel);
  300. return -ENODEV;
  301. }
  302. DRCMR(ssp->drcmr_rx) = DRCMR_MAPVLD | drv_data->rx_channel;
  303. DRCMR(ssp->drcmr_tx) = DRCMR_MAPVLD | drv_data->tx_channel;
  304. return 0;
  305. }
  306. void pxa2xx_spi_dma_release(struct driver_data *drv_data)
  307. {
  308. struct ssp_device *ssp = drv_data->ssp;
  309. DRCMR(ssp->drcmr_rx) = 0;
  310. DRCMR(ssp->drcmr_tx) = 0;
  311. if (drv_data->tx_channel != 0)
  312. pxa_free_dma(drv_data->tx_channel);
  313. if (drv_data->rx_channel != 0)
  314. pxa_free_dma(drv_data->rx_channel);
  315. }
  316. void pxa2xx_spi_dma_resume(struct driver_data *drv_data)
  317. {
  318. if (drv_data->rx_channel != -1)
  319. DRCMR(drv_data->ssp->drcmr_rx) =
  320. DRCMR_MAPVLD | drv_data->rx_channel;
  321. if (drv_data->tx_channel != -1)
  322. DRCMR(drv_data->ssp->drcmr_tx) =
  323. DRCMR_MAPVLD | drv_data->tx_channel;
  324. }
  325. int pxa2xx_spi_set_dma_burst_and_threshold(struct chip_data *chip,
  326. struct spi_device *spi,
  327. u8 bits_per_word, u32 *burst_code,
  328. u32 *threshold)
  329. {
  330. struct pxa2xx_spi_chip *chip_info =
  331. (struct pxa2xx_spi_chip *)spi->controller_data;
  332. int bytes_per_word;
  333. int burst_bytes;
  334. int thresh_words;
  335. int req_burst_size;
  336. int retval = 0;
  337. /* Set the threshold (in registers) to equal the same amount of data
  338. * as represented by burst size (in bytes). The computation below
  339. * is (burst_size rounded up to nearest 8 byte, word or long word)
  340. * divided by (bytes/register); the tx threshold is the inverse of
  341. * the rx, so that there will always be enough data in the rx fifo
  342. * to satisfy a burst, and there will always be enough space in the
  343. * tx fifo to accept a burst (a tx burst will overwrite the fifo if
  344. * there is not enough space), there must always remain enough empty
  345. * space in the rx fifo for any data loaded to the tx fifo.
  346. * Whenever burst_size (in bytes) equals bits/word, the fifo threshold
  347. * will be 8, or half the fifo;
  348. * The threshold can only be set to 2, 4 or 8, but not 16, because
  349. * to burst 16 to the tx fifo, the fifo would have to be empty;
  350. * however, the minimum fifo trigger level is 1, and the tx will
  351. * request service when the fifo is at this level, with only 15 spaces.
  352. */
  353. /* find bytes/word */
  354. if (bits_per_word <= 8)
  355. bytes_per_word = 1;
  356. else if (bits_per_word <= 16)
  357. bytes_per_word = 2;
  358. else
  359. bytes_per_word = 4;
  360. /* use struct pxa2xx_spi_chip->dma_burst_size if available */
  361. if (chip_info)
  362. req_burst_size = chip_info->dma_burst_size;
  363. else {
  364. switch (chip->dma_burst_size) {
  365. default:
  366. /* if the default burst size is not set,
  367. * do it now */
  368. chip->dma_burst_size = DCMD_BURST8;
  369. case DCMD_BURST8:
  370. req_burst_size = 8;
  371. break;
  372. case DCMD_BURST16:
  373. req_burst_size = 16;
  374. break;
  375. case DCMD_BURST32:
  376. req_burst_size = 32;
  377. break;
  378. }
  379. }
  380. if (req_burst_size <= 8) {
  381. *burst_code = DCMD_BURST8;
  382. burst_bytes = 8;
  383. } else if (req_burst_size <= 16) {
  384. if (bytes_per_word == 1) {
  385. /* don't burst more than 1/2 the fifo */
  386. *burst_code = DCMD_BURST8;
  387. burst_bytes = 8;
  388. retval = 1;
  389. } else {
  390. *burst_code = DCMD_BURST16;
  391. burst_bytes = 16;
  392. }
  393. } else {
  394. if (bytes_per_word == 1) {
  395. /* don't burst more than 1/2 the fifo */
  396. *burst_code = DCMD_BURST8;
  397. burst_bytes = 8;
  398. retval = 1;
  399. } else if (bytes_per_word == 2) {
  400. /* don't burst more than 1/2 the fifo */
  401. *burst_code = DCMD_BURST16;
  402. burst_bytes = 16;
  403. retval = 1;
  404. } else {
  405. *burst_code = DCMD_BURST32;
  406. burst_bytes = 32;
  407. }
  408. }
  409. thresh_words = burst_bytes / bytes_per_word;
  410. /* thresh_words will be between 2 and 8 */
  411. *threshold = (SSCR1_RxTresh(thresh_words) & SSCR1_RFT)
  412. | (SSCR1_TxTresh(16-thresh_words) & SSCR1_TFT);
  413. return retval;
  414. }