ppc4xx_sgdma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * IBM PPC4xx DMA engine scatter/gather library
  3. *
  4. * Copyright 2002-2003 MontaVista Software Inc.
  5. *
  6. * Cleaned up and converted to new DCR access
  7. * Matt Porter <mporter@kernel.crashing.org>
  8. *
  9. * Original code by Armin Kuster <akuster@mvista.com>
  10. * and Pete Popov <ppopov@mvista.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/pci.h>
  26. #include <asm/system.h>
  27. #include <asm/io.h>
  28. #include <asm/ppc4xx_dma.h>
  29. void
  30. ppc4xx_set_sg_addr(int dmanr, phys_addr_t sg_addr)
  31. {
  32. if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
  33. printk("ppc4xx_set_sg_addr: bad channel: %d\n", dmanr);
  34. return;
  35. }
  36. #ifdef PPC4xx_DMA_64BIT
  37. mtdcr(DCRN_ASGH0 + (dmanr * 0x8), (u32)(sg_addr >> 32));
  38. #endif
  39. mtdcr(DCRN_ASG0 + (dmanr * 0x8), (u32)sg_addr);
  40. }
  41. /*
  42. * Add a new sgl descriptor to the end of a scatter/gather list
  43. * which was created by alloc_dma_handle().
  44. *
  45. * For a memory to memory transfer, both dma addresses must be
  46. * valid. For a peripheral to memory transfer, one of the addresses
  47. * must be set to NULL, depending on the direction of the transfer:
  48. * memory to peripheral: set dst_addr to NULL,
  49. * peripheral to memory: set src_addr to NULL.
  50. */
  51. int
  52. ppc4xx_add_dma_sgl(sgl_handle_t handle, phys_addr_t src_addr, phys_addr_t dst_addr,
  53. unsigned int count)
  54. {
  55. sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
  56. ppc_dma_ch_t *p_dma_ch;
  57. if (!handle) {
  58. printk("ppc4xx_add_dma_sgl: null handle\n");
  59. return DMA_STATUS_BAD_HANDLE;
  60. }
  61. if (psgl->dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
  62. printk("ppc4xx_add_dma_sgl: bad channel: %d\n", psgl->dmanr);
  63. return DMA_STATUS_BAD_CHANNEL;
  64. }
  65. p_dma_ch = &dma_channels[psgl->dmanr];
  66. #ifdef DEBUG_4xxDMA
  67. {
  68. int error = 0;
  69. unsigned int aligned =
  70. (unsigned) src_addr | (unsigned) dst_addr | count;
  71. switch (p_dma_ch->pwidth) {
  72. case PW_8:
  73. break;
  74. case PW_16:
  75. if (aligned & 0x1)
  76. error = 1;
  77. break;
  78. case PW_32:
  79. if (aligned & 0x3)
  80. error = 1;
  81. break;
  82. case PW_64:
  83. if (aligned & 0x7)
  84. error = 1;
  85. break;
  86. default:
  87. printk("ppc4xx_add_dma_sgl: invalid bus width: 0x%x\n",
  88. p_dma_ch->pwidth);
  89. return DMA_STATUS_GENERAL_ERROR;
  90. }
  91. if (error)
  92. printk
  93. ("Alignment warning: ppc4xx_add_dma_sgl src 0x%x dst 0x%x count 0x%x bus width var %d\n",
  94. src_addr, dst_addr, count, p_dma_ch->pwidth);
  95. }
  96. #endif
  97. if ((unsigned) (psgl->ptail + 1) >= ((unsigned) psgl + SGL_LIST_SIZE)) {
  98. printk("sgl handle out of memory \n");
  99. return DMA_STATUS_OUT_OF_MEMORY;
  100. }
  101. if (!psgl->ptail) {
  102. psgl->phead = (ppc_sgl_t *)
  103. ((unsigned) psgl + sizeof (sgl_list_info_t));
  104. psgl->phead_dma = psgl->dma_addr + sizeof(sgl_list_info_t);
  105. psgl->ptail = psgl->phead;
  106. psgl->ptail_dma = psgl->phead_dma;
  107. } else {
  108. if(p_dma_ch->int_on_final_sg) {
  109. /* mask out all dma interrupts, except error, on tail
  110. before adding new tail. */
  111. psgl->ptail->control_count &=
  112. ~(SG_TCI_ENABLE | SG_ETI_ENABLE);
  113. }
  114. psgl->ptail->next = psgl->ptail_dma + sizeof(ppc_sgl_t);
  115. psgl->ptail++;
  116. psgl->ptail_dma += sizeof(ppc_sgl_t);
  117. }
  118. psgl->ptail->control = psgl->control;
  119. psgl->ptail->src_addr = src_addr;
  120. psgl->ptail->dst_addr = dst_addr;
  121. psgl->ptail->control_count = (count >> p_dma_ch->shift) |
  122. psgl->sgl_control;
  123. psgl->ptail->next = (uint32_t) NULL;
  124. return DMA_STATUS_GOOD;
  125. }
  126. /*
  127. * Enable (start) the DMA described by the sgl handle.
  128. */
  129. void
  130. ppc4xx_enable_dma_sgl(sgl_handle_t handle)
  131. {
  132. sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
  133. ppc_dma_ch_t *p_dma_ch;
  134. uint32_t sg_command;
  135. if (!handle) {
  136. printk("ppc4xx_enable_dma_sgl: null handle\n");
  137. return;
  138. } else if (psgl->dmanr > (MAX_PPC4xx_DMA_CHANNELS - 1)) {
  139. printk("ppc4xx_enable_dma_sgl: bad channel in handle %d\n",
  140. psgl->dmanr);
  141. return;
  142. } else if (!psgl->phead) {
  143. printk("ppc4xx_enable_dma_sgl: sg list empty\n");
  144. return;
  145. }
  146. p_dma_ch = &dma_channels[psgl->dmanr];
  147. psgl->ptail->control_count &= ~SG_LINK; /* make this the last dscrptr */
  148. sg_command = mfdcr(DCRN_ASGC);
  149. ppc4xx_set_sg_addr(psgl->dmanr, psgl->phead_dma);
  150. sg_command |= SSG_ENABLE(psgl->dmanr);
  151. mtdcr(DCRN_ASGC, sg_command); /* start transfer */
  152. }
  153. /*
  154. * Halt an active scatter/gather DMA operation.
  155. */
  156. void
  157. ppc4xx_disable_dma_sgl(sgl_handle_t handle)
  158. {
  159. sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
  160. uint32_t sg_command;
  161. if (!handle) {
  162. printk("ppc4xx_enable_dma_sgl: null handle\n");
  163. return;
  164. } else if (psgl->dmanr > (MAX_PPC4xx_DMA_CHANNELS - 1)) {
  165. printk("ppc4xx_enable_dma_sgl: bad channel in handle %d\n",
  166. psgl->dmanr);
  167. return;
  168. }
  169. sg_command = mfdcr(DCRN_ASGC);
  170. sg_command &= ~SSG_ENABLE(psgl->dmanr);
  171. mtdcr(DCRN_ASGC, sg_command); /* stop transfer */
  172. }
  173. /*
  174. * Returns number of bytes left to be transferred from the entire sgl list.
  175. * *src_addr and *dst_addr get set to the source/destination address of
  176. * the sgl descriptor where the DMA stopped.
  177. *
  178. * An sgl transfer must NOT be active when this function is called.
  179. */
  180. int
  181. ppc4xx_get_dma_sgl_residue(sgl_handle_t handle, phys_addr_t * src_addr,
  182. phys_addr_t * dst_addr)
  183. {
  184. sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
  185. ppc_dma_ch_t *p_dma_ch;
  186. ppc_sgl_t *pnext, *sgl_addr;
  187. uint32_t count_left;
  188. if (!handle) {
  189. printk("ppc4xx_get_dma_sgl_residue: null handle\n");
  190. return DMA_STATUS_BAD_HANDLE;
  191. } else if (psgl->dmanr > (MAX_PPC4xx_DMA_CHANNELS - 1)) {
  192. printk("ppc4xx_get_dma_sgl_residue: bad channel in handle %d\n",
  193. psgl->dmanr);
  194. return DMA_STATUS_BAD_CHANNEL;
  195. }
  196. sgl_addr = (ppc_sgl_t *) __va(mfdcr(DCRN_ASG0 + (psgl->dmanr * 0x8)));
  197. count_left = mfdcr(DCRN_DMACT0 + (psgl->dmanr * 0x8)) & SG_COUNT_MASK;
  198. if (!sgl_addr) {
  199. printk("ppc4xx_get_dma_sgl_residue: sgl addr register is null\n");
  200. goto error;
  201. }
  202. pnext = psgl->phead;
  203. while (pnext &&
  204. ((unsigned) pnext < ((unsigned) psgl + SGL_LIST_SIZE) &&
  205. (pnext != sgl_addr))
  206. ) {
  207. pnext++;
  208. }
  209. if (pnext == sgl_addr) { /* found the sgl descriptor */
  210. *src_addr = pnext->src_addr;
  211. *dst_addr = pnext->dst_addr;
  212. /*
  213. * Now search the remaining descriptors and add their count.
  214. * We already have the remaining count from this descriptor in
  215. * count_left.
  216. */
  217. pnext++;
  218. while ((pnext != psgl->ptail) &&
  219. ((unsigned) pnext < ((unsigned) psgl + SGL_LIST_SIZE))
  220. ) {
  221. count_left += pnext->control_count & SG_COUNT_MASK;
  222. }
  223. if (pnext != psgl->ptail) { /* should never happen */
  224. printk
  225. ("ppc4xx_get_dma_sgl_residue error (1) psgl->ptail 0x%x handle 0x%x\n",
  226. (unsigned int) psgl->ptail, (unsigned int) handle);
  227. goto error;
  228. }
  229. /* success */
  230. p_dma_ch = &dma_channels[psgl->dmanr];
  231. return (count_left << p_dma_ch->shift); /* count in bytes */
  232. } else {
  233. /* this shouldn't happen */
  234. printk
  235. ("get_dma_sgl_residue, unable to match current address 0x%x, handle 0x%x\n",
  236. (unsigned int) sgl_addr, (unsigned int) handle);
  237. }
  238. error:
  239. *src_addr = (phys_addr_t) NULL;
  240. *dst_addr = (phys_addr_t) NULL;
  241. return 0;
  242. }
  243. /*
  244. * Returns the address(es) of the buffer(s) contained in the head element of
  245. * the scatter/gather list. The element is removed from the scatter/gather
  246. * list and the next element becomes the head.
  247. *
  248. * This function should only be called when the DMA is not active.
  249. */
  250. int
  251. ppc4xx_delete_dma_sgl_element(sgl_handle_t handle, phys_addr_t * src_dma_addr,
  252. phys_addr_t * dst_dma_addr)
  253. {
  254. sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
  255. if (!handle) {
  256. printk("ppc4xx_delete_sgl_element: null handle\n");
  257. return DMA_STATUS_BAD_HANDLE;
  258. } else if (psgl->dmanr > (MAX_PPC4xx_DMA_CHANNELS - 1)) {
  259. printk("ppc4xx_delete_sgl_element: bad channel in handle %d\n",
  260. psgl->dmanr);
  261. return DMA_STATUS_BAD_CHANNEL;
  262. }
  263. if (!psgl->phead) {
  264. printk("ppc4xx_delete_sgl_element: sgl list empty\n");
  265. *src_dma_addr = (phys_addr_t) NULL;
  266. *dst_dma_addr = (phys_addr_t) NULL;
  267. return DMA_STATUS_SGL_LIST_EMPTY;
  268. }
  269. *src_dma_addr = (phys_addr_t) psgl->phead->src_addr;
  270. *dst_dma_addr = (phys_addr_t) psgl->phead->dst_addr;
  271. if (psgl->phead == psgl->ptail) {
  272. /* last descriptor on the list */
  273. psgl->phead = NULL;
  274. psgl->ptail = NULL;
  275. } else {
  276. psgl->phead++;
  277. psgl->phead_dma += sizeof(ppc_sgl_t);
  278. }
  279. return DMA_STATUS_GOOD;
  280. }
  281. /*
  282. * Create a scatter/gather list handle. This is simply a structure which
  283. * describes a scatter/gather list.
  284. *
  285. * A handle is returned in "handle" which the driver should save in order to
  286. * be able to access this list later. A chunk of memory will be allocated
  287. * to be used by the API for internal management purposes, including managing
  288. * the sg list and allocating memory for the sgl descriptors. One page should
  289. * be more than enough for that purpose. Perhaps it's a bit wasteful to use
  290. * a whole page for a single sg list, but most likely there will be only one
  291. * sg list per channel.
  292. *
  293. * Interrupt notes:
  294. * Each sgl descriptor has a copy of the DMA control word which the DMA engine
  295. * loads in the control register. The control word has a "global" interrupt
  296. * enable bit for that channel. Interrupts are further qualified by a few bits
  297. * in the sgl descriptor count register. In order to setup an sgl, we have to
  298. * know ahead of time whether or not interrupts will be enabled at the completion
  299. * of the transfers. Thus, enable_dma_interrupt()/disable_dma_interrupt() MUST
  300. * be called before calling alloc_dma_handle(). If the interrupt mode will never
  301. * change after powerup, then enable_dma_interrupt()/disable_dma_interrupt()
  302. * do not have to be called -- interrupts will be enabled or disabled based
  303. * on how the channel was configured after powerup by the hw_init_dma_channel()
  304. * function. Each sgl descriptor will be setup to interrupt if an error occurs;
  305. * however, only the last descriptor will be setup to interrupt. Thus, an
  306. * interrupt will occur (if interrupts are enabled) only after the complete
  307. * sgl transfer is done.
  308. */
  309. int
  310. ppc4xx_alloc_dma_handle(sgl_handle_t * phandle, unsigned int mode, unsigned int dmanr)
  311. {
  312. sgl_list_info_t *psgl=NULL;
  313. dma_addr_t dma_addr;
  314. ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
  315. uint32_t sg_command;
  316. uint32_t ctc_settings;
  317. void *ret;
  318. if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
  319. printk("ppc4xx_alloc_dma_handle: invalid channel 0x%x\n", dmanr);
  320. return DMA_STATUS_BAD_CHANNEL;
  321. }
  322. if (!phandle) {
  323. printk("ppc4xx_alloc_dma_handle: null handle pointer\n");
  324. return DMA_STATUS_NULL_POINTER;
  325. }
  326. /* Get a page of memory, which is zeroed out by consistent_alloc() */
  327. ret = dma_alloc_coherent(NULL, DMA_PPC4xx_SIZE, &dma_addr, GFP_KERNEL);
  328. if (ret != NULL) {
  329. memset(ret, 0, DMA_PPC4xx_SIZE);
  330. psgl = (sgl_list_info_t *) ret;
  331. }
  332. if (psgl == NULL) {
  333. *phandle = (sgl_handle_t) NULL;
  334. return DMA_STATUS_OUT_OF_MEMORY;
  335. }
  336. psgl->dma_addr = dma_addr;
  337. psgl->dmanr = dmanr;
  338. /*
  339. * Modify and save the control word. These words will be
  340. * written to each sgl descriptor. The DMA engine then
  341. * loads this control word into the control register
  342. * every time it reads a new descriptor.
  343. */
  344. psgl->control = p_dma_ch->control;
  345. /* Clear all mode bits */
  346. psgl->control &= ~(DMA_TM_MASK | DMA_TD);
  347. /* Save control word and mode */
  348. psgl->control |= (mode | DMA_CE_ENABLE);
  349. /* In MM mode, we must set ETD/TCE */
  350. if (mode == DMA_MODE_MM)
  351. psgl->control |= DMA_ETD_OUTPUT | DMA_TCE_ENABLE;
  352. if (p_dma_ch->int_enable) {
  353. /* Enable channel interrupt */
  354. psgl->control |= DMA_CIE_ENABLE;
  355. } else {
  356. psgl->control &= ~DMA_CIE_ENABLE;
  357. }
  358. sg_command = mfdcr(DCRN_ASGC);
  359. sg_command |= SSG_MASK_ENABLE(dmanr);
  360. /* Enable SGL control access */
  361. mtdcr(DCRN_ASGC, sg_command);
  362. psgl->sgl_control = SG_ERI_ENABLE | SG_LINK;
  363. /* keep control count register settings */
  364. ctc_settings = mfdcr(DCRN_DMACT0 + (dmanr * 0x8))
  365. & (DMA_CTC_BSIZ_MSK | DMA_CTC_BTEN); /*burst mode settings*/
  366. psgl->sgl_control |= ctc_settings;
  367. if (p_dma_ch->int_enable) {
  368. if (p_dma_ch->tce_enable)
  369. psgl->sgl_control |= SG_TCI_ENABLE;
  370. else
  371. psgl->sgl_control |= SG_ETI_ENABLE;
  372. }
  373. *phandle = (sgl_handle_t) psgl;
  374. return DMA_STATUS_GOOD;
  375. }
  376. /*
  377. * Destroy a scatter/gather list handle that was created by alloc_dma_handle().
  378. * The list must be empty (contain no elements).
  379. */
  380. void
  381. ppc4xx_free_dma_handle(sgl_handle_t handle)
  382. {
  383. sgl_list_info_t *psgl = (sgl_list_info_t *) handle;
  384. if (!handle) {
  385. printk("ppc4xx_free_dma_handle: got NULL\n");
  386. return;
  387. } else if (psgl->phead) {
  388. printk("ppc4xx_free_dma_handle: list not empty\n");
  389. return;
  390. } else if (!psgl->dma_addr) { /* should never happen */
  391. printk("ppc4xx_free_dma_handle: no dma address\n");
  392. return;
  393. }
  394. dma_free_coherent(NULL, DMA_PPC4xx_SIZE, (void *) psgl, 0);
  395. }
  396. EXPORT_SYMBOL(ppc4xx_alloc_dma_handle);
  397. EXPORT_SYMBOL(ppc4xx_free_dma_handle);
  398. EXPORT_SYMBOL(ppc4xx_add_dma_sgl);
  399. EXPORT_SYMBOL(ppc4xx_delete_dma_sgl_element);
  400. EXPORT_SYMBOL(ppc4xx_enable_dma_sgl);
  401. EXPORT_SYMBOL(ppc4xx_disable_dma_sgl);
  402. EXPORT_SYMBOL(ppc4xx_get_dma_sgl_residue);