spi-pxa2xx-pxadma.c 13 KB

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