ppc4xx_sgdma.c 13 KB

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