ppc4xx_sgdma.c 13 KB

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