dbdma.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /*
  2. *
  3. * BRIEF MODULE DESCRIPTION
  4. * The Descriptor Based DMA channel manager that first appeared
  5. * on the Au1550. I started with dma.c, but I think all that is
  6. * left is this initial comment :-)
  7. *
  8. * Copyright 2004 Embedded Edge, LLC
  9. * dan@embeddededge.com
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  19. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  22. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  23. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * You should have received a copy of the GNU General Public License along
  28. * with this program; if not, write to the Free Software Foundation, Inc.,
  29. * 675 Mass Ave, Cambridge, MA 02139, USA.
  30. *
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/slab.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/module.h>
  37. #include <asm/mach-au1x00/au1000.h>
  38. #include <asm/mach-au1x00/au1xxx_dbdma.h>
  39. #if defined(CONFIG_SOC_AU1550) || defined(CONFIG_SOC_AU1200)
  40. /*
  41. * The Descriptor Based DMA supports up to 16 channels.
  42. *
  43. * There are 32 devices defined. We keep an internal structure
  44. * of devices using these channels, along with additional
  45. * information.
  46. *
  47. * We allocate the descriptors and allow access to them through various
  48. * functions. The drivers allocate the data buffers and assign them
  49. * to the descriptors.
  50. */
  51. static DEFINE_SPINLOCK(au1xxx_dbdma_spin_lock);
  52. /* I couldn't find a macro that did this... */
  53. #define ALIGN_ADDR(x, a) ((((u32)(x)) + (a-1)) & ~(a-1))
  54. static dbdma_global_t *dbdma_gptr = (dbdma_global_t *)DDMA_GLOBAL_BASE;
  55. static int dbdma_initialized;
  56. static void au1xxx_dbdma_init(void);
  57. static dbdev_tab_t dbdev_tab[] = {
  58. #ifdef CONFIG_SOC_AU1550
  59. /* UARTS */
  60. { DSCR_CMD0_UART0_TX, DEV_FLAGS_OUT, 0, 8, 0x11100004, 0, 0 },
  61. { DSCR_CMD0_UART0_RX, DEV_FLAGS_IN, 0, 8, 0x11100000, 0, 0 },
  62. { DSCR_CMD0_UART3_TX, DEV_FLAGS_OUT, 0, 8, 0x11400004, 0, 0 },
  63. { DSCR_CMD0_UART3_RX, DEV_FLAGS_IN, 0, 8, 0x11400000, 0, 0 },
  64. /* EXT DMA */
  65. { DSCR_CMD0_DMA_REQ0, 0, 0, 0, 0x00000000, 0, 0 },
  66. { DSCR_CMD0_DMA_REQ1, 0, 0, 0, 0x00000000, 0, 0 },
  67. { DSCR_CMD0_DMA_REQ2, 0, 0, 0, 0x00000000, 0, 0 },
  68. { DSCR_CMD0_DMA_REQ3, 0, 0, 0, 0x00000000, 0, 0 },
  69. /* USB DEV */
  70. { DSCR_CMD0_USBDEV_RX0, DEV_FLAGS_IN, 4, 8, 0x10200000, 0, 0 },
  71. { DSCR_CMD0_USBDEV_TX0, DEV_FLAGS_OUT, 4, 8, 0x10200004, 0, 0 },
  72. { DSCR_CMD0_USBDEV_TX1, DEV_FLAGS_OUT, 4, 8, 0x10200008, 0, 0 },
  73. { DSCR_CMD0_USBDEV_TX2, DEV_FLAGS_OUT, 4, 8, 0x1020000c, 0, 0 },
  74. { DSCR_CMD0_USBDEV_RX3, DEV_FLAGS_IN, 4, 8, 0x10200010, 0, 0 },
  75. { DSCR_CMD0_USBDEV_RX4, DEV_FLAGS_IN, 4, 8, 0x10200014, 0, 0 },
  76. /* PSC 0 */
  77. { DSCR_CMD0_PSC0_TX, DEV_FLAGS_OUT, 0, 0, 0x11a0001c, 0, 0 },
  78. { DSCR_CMD0_PSC0_RX, DEV_FLAGS_IN, 0, 0, 0x11a0001c, 0, 0 },
  79. /* PSC 1 */
  80. { DSCR_CMD0_PSC1_TX, DEV_FLAGS_OUT, 0, 0, 0x11b0001c, 0, 0 },
  81. { DSCR_CMD0_PSC1_RX, DEV_FLAGS_IN, 0, 0, 0x11b0001c, 0, 0 },
  82. /* PSC 2 */
  83. { DSCR_CMD0_PSC2_TX, DEV_FLAGS_OUT, 0, 0, 0x10a0001c, 0, 0 },
  84. { DSCR_CMD0_PSC2_RX, DEV_FLAGS_IN, 0, 0, 0x10a0001c, 0, 0 },
  85. /* PSC 3 */
  86. { DSCR_CMD0_PSC3_TX, DEV_FLAGS_OUT, 0, 0, 0x10b0001c, 0, 0 },
  87. { DSCR_CMD0_PSC3_RX, DEV_FLAGS_IN, 0, 0, 0x10b0001c, 0, 0 },
  88. { DSCR_CMD0_PCI_WRITE, 0, 0, 0, 0x00000000, 0, 0 }, /* PCI */
  89. { DSCR_CMD0_NAND_FLASH, 0, 0, 0, 0x00000000, 0, 0 }, /* NAND */
  90. /* MAC 0 */
  91. { DSCR_CMD0_MAC0_RX, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },
  92. { DSCR_CMD0_MAC0_TX, DEV_FLAGS_OUT, 0, 0, 0x00000000, 0, 0 },
  93. /* MAC 1 */
  94. { DSCR_CMD0_MAC1_RX, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },
  95. { DSCR_CMD0_MAC1_TX, DEV_FLAGS_OUT, 0, 0, 0x00000000, 0, 0 },
  96. #endif /* CONFIG_SOC_AU1550 */
  97. #ifdef CONFIG_SOC_AU1200
  98. { DSCR_CMD0_UART0_TX, DEV_FLAGS_OUT, 0, 8, 0x11100004, 0, 0 },
  99. { DSCR_CMD0_UART0_RX, DEV_FLAGS_IN, 0, 8, 0x11100000, 0, 0 },
  100. { DSCR_CMD0_UART1_TX, DEV_FLAGS_OUT, 0, 8, 0x11200004, 0, 0 },
  101. { DSCR_CMD0_UART1_RX, DEV_FLAGS_IN, 0, 8, 0x11200000, 0, 0 },
  102. { DSCR_CMD0_DMA_REQ0, 0, 0, 0, 0x00000000, 0, 0 },
  103. { DSCR_CMD0_DMA_REQ1, 0, 0, 0, 0x00000000, 0, 0 },
  104. { DSCR_CMD0_MAE_BE, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  105. { DSCR_CMD0_MAE_FE, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  106. { DSCR_CMD0_MAE_BOTH, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  107. { DSCR_CMD0_LCD, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  108. { DSCR_CMD0_SDMS_TX0, DEV_FLAGS_OUT, 4, 8, 0x10600000, 0, 0 },
  109. { DSCR_CMD0_SDMS_RX0, DEV_FLAGS_IN, 4, 8, 0x10600004, 0, 0 },
  110. { DSCR_CMD0_SDMS_TX1, DEV_FLAGS_OUT, 4, 8, 0x10680000, 0, 0 },
  111. { DSCR_CMD0_SDMS_RX1, DEV_FLAGS_IN, 4, 8, 0x10680004, 0, 0 },
  112. { DSCR_CMD0_AES_RX, DEV_FLAGS_IN , 4, 32, 0x10300008, 0, 0 },
  113. { DSCR_CMD0_AES_TX, DEV_FLAGS_OUT, 4, 32, 0x10300004, 0, 0 },
  114. { DSCR_CMD0_PSC0_TX, DEV_FLAGS_OUT, 0, 16, 0x11a0001c, 0, 0 },
  115. { DSCR_CMD0_PSC0_RX, DEV_FLAGS_IN, 0, 16, 0x11a0001c, 0, 0 },
  116. { DSCR_CMD0_PSC0_SYNC, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  117. { DSCR_CMD0_PSC1_TX, DEV_FLAGS_OUT, 0, 16, 0x11b0001c, 0, 0 },
  118. { DSCR_CMD0_PSC1_RX, DEV_FLAGS_IN, 0, 16, 0x11b0001c, 0, 0 },
  119. { DSCR_CMD0_PSC1_SYNC, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  120. { DSCR_CMD0_CIM_RXA, DEV_FLAGS_IN, 0, 32, 0x14004020, 0, 0 },
  121. { DSCR_CMD0_CIM_RXB, DEV_FLAGS_IN, 0, 32, 0x14004040, 0, 0 },
  122. { DSCR_CMD0_CIM_RXC, DEV_FLAGS_IN, 0, 32, 0x14004060, 0, 0 },
  123. { DSCR_CMD0_CIM_SYNC, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  124. { DSCR_CMD0_NAND_FLASH, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },
  125. #endif /* CONFIG_SOC_AU1200 */
  126. { DSCR_CMD0_THROTTLE, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  127. { DSCR_CMD0_ALWAYS, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  128. /* Provide 16 user definable device types */
  129. { ~0, 0, 0, 0, 0, 0, 0 },
  130. { ~0, 0, 0, 0, 0, 0, 0 },
  131. { ~0, 0, 0, 0, 0, 0, 0 },
  132. { ~0, 0, 0, 0, 0, 0, 0 },
  133. { ~0, 0, 0, 0, 0, 0, 0 },
  134. { ~0, 0, 0, 0, 0, 0, 0 },
  135. { ~0, 0, 0, 0, 0, 0, 0 },
  136. { ~0, 0, 0, 0, 0, 0, 0 },
  137. { ~0, 0, 0, 0, 0, 0, 0 },
  138. { ~0, 0, 0, 0, 0, 0, 0 },
  139. { ~0, 0, 0, 0, 0, 0, 0 },
  140. { ~0, 0, 0, 0, 0, 0, 0 },
  141. { ~0, 0, 0, 0, 0, 0, 0 },
  142. { ~0, 0, 0, 0, 0, 0, 0 },
  143. { ~0, 0, 0, 0, 0, 0, 0 },
  144. { ~0, 0, 0, 0, 0, 0, 0 },
  145. };
  146. #define DBDEV_TAB_SIZE ARRAY_SIZE(dbdev_tab)
  147. static chan_tab_t *chan_tab_ptr[NUM_DBDMA_CHANS];
  148. static dbdev_tab_t *find_dbdev_id(u32 id)
  149. {
  150. int i;
  151. dbdev_tab_t *p;
  152. for (i = 0; i < DBDEV_TAB_SIZE; ++i) {
  153. p = &dbdev_tab[i];
  154. if (p->dev_id == id)
  155. return p;
  156. }
  157. return NULL;
  158. }
  159. void *au1xxx_ddma_get_nextptr_virt(au1x_ddma_desc_t *dp)
  160. {
  161. return phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  162. }
  163. EXPORT_SYMBOL(au1xxx_ddma_get_nextptr_virt);
  164. u32 au1xxx_ddma_add_device(dbdev_tab_t *dev)
  165. {
  166. u32 ret = 0;
  167. dbdev_tab_t *p;
  168. static u16 new_id = 0x1000;
  169. p = find_dbdev_id(~0);
  170. if (NULL != p) {
  171. memcpy(p, dev, sizeof(dbdev_tab_t));
  172. p->dev_id = DSCR_DEV2CUSTOM_ID(new_id, dev->dev_id);
  173. ret = p->dev_id;
  174. new_id++;
  175. #if 0
  176. printk(KERN_DEBUG "add_device: id:%x flags:%x padd:%x\n",
  177. p->dev_id, p->dev_flags, p->dev_physaddr);
  178. #endif
  179. }
  180. return ret;
  181. }
  182. EXPORT_SYMBOL(au1xxx_ddma_add_device);
  183. /* Allocate a channel and return a non-zero descriptor if successful. */
  184. u32 au1xxx_dbdma_chan_alloc(u32 srcid, u32 destid,
  185. void (*callback)(int, void *), void *callparam)
  186. {
  187. unsigned long flags;
  188. u32 used, chan, rv;
  189. u32 dcp;
  190. int i;
  191. dbdev_tab_t *stp, *dtp;
  192. chan_tab_t *ctp;
  193. au1x_dma_chan_t *cp;
  194. /*
  195. * We do the intialization on the first channel allocation.
  196. * We have to wait because of the interrupt handler initialization
  197. * which can't be done successfully during board set up.
  198. */
  199. if (!dbdma_initialized)
  200. au1xxx_dbdma_init();
  201. dbdma_initialized = 1;
  202. stp = find_dbdev_id(srcid);
  203. if (stp == NULL)
  204. return 0;
  205. dtp = find_dbdev_id(destid);
  206. if (dtp == NULL)
  207. return 0;
  208. used = 0;
  209. rv = 0;
  210. /* Check to see if we can get both channels. */
  211. spin_lock_irqsave(&au1xxx_dbdma_spin_lock, flags);
  212. if (!(stp->dev_flags & DEV_FLAGS_INUSE) ||
  213. (stp->dev_flags & DEV_FLAGS_ANYUSE)) {
  214. /* Got source */
  215. stp->dev_flags |= DEV_FLAGS_INUSE;
  216. if (!(dtp->dev_flags & DEV_FLAGS_INUSE) ||
  217. (dtp->dev_flags & DEV_FLAGS_ANYUSE)) {
  218. /* Got destination */
  219. dtp->dev_flags |= DEV_FLAGS_INUSE;
  220. } else {
  221. /* Can't get dest. Release src. */
  222. stp->dev_flags &= ~DEV_FLAGS_INUSE;
  223. used++;
  224. }
  225. } else
  226. used++;
  227. spin_unlock_irqrestore(&au1xxx_dbdma_spin_lock, flags);
  228. if (!used) {
  229. /* Let's see if we can allocate a channel for it. */
  230. ctp = NULL;
  231. chan = 0;
  232. spin_lock_irqsave(&au1xxx_dbdma_spin_lock, flags);
  233. for (i = 0; i < NUM_DBDMA_CHANS; i++)
  234. if (chan_tab_ptr[i] == NULL) {
  235. /*
  236. * If kmalloc fails, it is caught below same
  237. * as a channel not available.
  238. */
  239. ctp = kmalloc(sizeof(chan_tab_t), GFP_ATOMIC);
  240. chan_tab_ptr[i] = ctp;
  241. break;
  242. }
  243. spin_unlock_irqrestore(&au1xxx_dbdma_spin_lock, flags);
  244. if (ctp != NULL) {
  245. memset(ctp, 0, sizeof(chan_tab_t));
  246. ctp->chan_index = chan = i;
  247. dcp = DDMA_CHANNEL_BASE;
  248. dcp += (0x0100 * chan);
  249. ctp->chan_ptr = (au1x_dma_chan_t *)dcp;
  250. cp = (au1x_dma_chan_t *)dcp;
  251. ctp->chan_src = stp;
  252. ctp->chan_dest = dtp;
  253. ctp->chan_callback = callback;
  254. ctp->chan_callparam = callparam;
  255. /* Initialize channel configuration. */
  256. i = 0;
  257. if (stp->dev_intlevel)
  258. i |= DDMA_CFG_SED;
  259. if (stp->dev_intpolarity)
  260. i |= DDMA_CFG_SP;
  261. if (dtp->dev_intlevel)
  262. i |= DDMA_CFG_DED;
  263. if (dtp->dev_intpolarity)
  264. i |= DDMA_CFG_DP;
  265. if ((stp->dev_flags & DEV_FLAGS_SYNC) ||
  266. (dtp->dev_flags & DEV_FLAGS_SYNC))
  267. i |= DDMA_CFG_SYNC;
  268. cp->ddma_cfg = i;
  269. au_sync();
  270. /* Return a non-zero value that can be used to
  271. * find the channel information in subsequent
  272. * operations.
  273. */
  274. rv = (u32)(&chan_tab_ptr[chan]);
  275. } else {
  276. /* Release devices */
  277. stp->dev_flags &= ~DEV_FLAGS_INUSE;
  278. dtp->dev_flags &= ~DEV_FLAGS_INUSE;
  279. }
  280. }
  281. return rv;
  282. }
  283. EXPORT_SYMBOL(au1xxx_dbdma_chan_alloc);
  284. /*
  285. * Set the device width if source or destination is a FIFO.
  286. * Should be 8, 16, or 32 bits.
  287. */
  288. u32 au1xxx_dbdma_set_devwidth(u32 chanid, int bits)
  289. {
  290. u32 rv;
  291. chan_tab_t *ctp;
  292. dbdev_tab_t *stp, *dtp;
  293. ctp = *((chan_tab_t **)chanid);
  294. stp = ctp->chan_src;
  295. dtp = ctp->chan_dest;
  296. rv = 0;
  297. if (stp->dev_flags & DEV_FLAGS_IN) { /* Source in fifo */
  298. rv = stp->dev_devwidth;
  299. stp->dev_devwidth = bits;
  300. }
  301. if (dtp->dev_flags & DEV_FLAGS_OUT) { /* Destination out fifo */
  302. rv = dtp->dev_devwidth;
  303. dtp->dev_devwidth = bits;
  304. }
  305. return rv;
  306. }
  307. EXPORT_SYMBOL(au1xxx_dbdma_set_devwidth);
  308. /* Allocate a descriptor ring, initializing as much as possible. */
  309. u32 au1xxx_dbdma_ring_alloc(u32 chanid, int entries)
  310. {
  311. int i;
  312. u32 desc_base, srcid, destid;
  313. u32 cmd0, cmd1, src1, dest1;
  314. u32 src0, dest0;
  315. chan_tab_t *ctp;
  316. dbdev_tab_t *stp, *dtp;
  317. au1x_ddma_desc_t *dp;
  318. /*
  319. * I guess we could check this to be within the
  320. * range of the table......
  321. */
  322. ctp = *((chan_tab_t **)chanid);
  323. stp = ctp->chan_src;
  324. dtp = ctp->chan_dest;
  325. /*
  326. * The descriptors must be 32-byte aligned. There is a
  327. * possibility the allocation will give us such an address,
  328. * and if we try that first we are likely to not waste larger
  329. * slabs of memory.
  330. */
  331. desc_base = (u32)kmalloc(entries * sizeof(au1x_ddma_desc_t),
  332. GFP_KERNEL|GFP_DMA);
  333. if (desc_base == 0)
  334. return 0;
  335. if (desc_base & 0x1f) {
  336. /*
  337. * Lost....do it again, allocate extra, and round
  338. * the address base.
  339. */
  340. kfree((const void *)desc_base);
  341. i = entries * sizeof(au1x_ddma_desc_t);
  342. i += (sizeof(au1x_ddma_desc_t) - 1);
  343. desc_base = (u32)kmalloc(i, GFP_KERNEL|GFP_DMA);
  344. if (desc_base == 0)
  345. return 0;
  346. desc_base = ALIGN_ADDR(desc_base, sizeof(au1x_ddma_desc_t));
  347. }
  348. dp = (au1x_ddma_desc_t *)desc_base;
  349. /* Keep track of the base descriptor. */
  350. ctp->chan_desc_base = dp;
  351. /* Initialize the rings with as much information as we know. */
  352. srcid = stp->dev_id;
  353. destid = dtp->dev_id;
  354. cmd0 = cmd1 = src1 = dest1 = 0;
  355. src0 = dest0 = 0;
  356. cmd0 |= DSCR_CMD0_SID(srcid);
  357. cmd0 |= DSCR_CMD0_DID(destid);
  358. cmd0 |= DSCR_CMD0_IE | DSCR_CMD0_CV;
  359. cmd0 |= DSCR_CMD0_ST(DSCR_CMD0_ST_NOCHANGE);
  360. /* Is it mem to mem transfer? */
  361. if (((DSCR_CUSTOM2DEV_ID(srcid) == DSCR_CMD0_THROTTLE) ||
  362. (DSCR_CUSTOM2DEV_ID(srcid) == DSCR_CMD0_ALWAYS)) &&
  363. ((DSCR_CUSTOM2DEV_ID(destid) == DSCR_CMD0_THROTTLE) ||
  364. (DSCR_CUSTOM2DEV_ID(destid) == DSCR_CMD0_ALWAYS)))
  365. cmd0 |= DSCR_CMD0_MEM;
  366. switch (stp->dev_devwidth) {
  367. case 8:
  368. cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_BYTE);
  369. break;
  370. case 16:
  371. cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_HALFWORD);
  372. break;
  373. case 32:
  374. default:
  375. cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_WORD);
  376. break;
  377. }
  378. switch (dtp->dev_devwidth) {
  379. case 8:
  380. cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_BYTE);
  381. break;
  382. case 16:
  383. cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_HALFWORD);
  384. break;
  385. case 32:
  386. default:
  387. cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_WORD);
  388. break;
  389. }
  390. /*
  391. * If the device is marked as an in/out FIFO, ensure it is
  392. * set non-coherent.
  393. */
  394. if (stp->dev_flags & DEV_FLAGS_IN)
  395. cmd0 |= DSCR_CMD0_SN; /* Source in FIFO */
  396. if (dtp->dev_flags & DEV_FLAGS_OUT)
  397. cmd0 |= DSCR_CMD0_DN; /* Destination out FIFO */
  398. /*
  399. * Set up source1. For now, assume no stride and increment.
  400. * A channel attribute update can change this later.
  401. */
  402. switch (stp->dev_tsize) {
  403. case 1:
  404. src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE1);
  405. break;
  406. case 2:
  407. src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE2);
  408. break;
  409. case 4:
  410. src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE4);
  411. break;
  412. case 8:
  413. default:
  414. src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE8);
  415. break;
  416. }
  417. /* If source input is FIFO, set static address. */
  418. if (stp->dev_flags & DEV_FLAGS_IN) {
  419. if (stp->dev_flags & DEV_FLAGS_BURSTABLE)
  420. src1 |= DSCR_SRC1_SAM(DSCR_xAM_BURST);
  421. else
  422. src1 |= DSCR_SRC1_SAM(DSCR_xAM_STATIC);
  423. }
  424. if (stp->dev_physaddr)
  425. src0 = stp->dev_physaddr;
  426. /*
  427. * Set up dest1. For now, assume no stride and increment.
  428. * A channel attribute update can change this later.
  429. */
  430. switch (dtp->dev_tsize) {
  431. case 1:
  432. dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE1);
  433. break;
  434. case 2:
  435. dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE2);
  436. break;
  437. case 4:
  438. dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE4);
  439. break;
  440. case 8:
  441. default:
  442. dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE8);
  443. break;
  444. }
  445. /* If destination output is FIFO, set static address. */
  446. if (dtp->dev_flags & DEV_FLAGS_OUT) {
  447. if (dtp->dev_flags & DEV_FLAGS_BURSTABLE)
  448. dest1 |= DSCR_DEST1_DAM(DSCR_xAM_BURST);
  449. else
  450. dest1 |= DSCR_DEST1_DAM(DSCR_xAM_STATIC);
  451. }
  452. if (dtp->dev_physaddr)
  453. dest0 = dtp->dev_physaddr;
  454. #if 0
  455. printk(KERN_DEBUG "did:%x sid:%x cmd0:%x cmd1:%x source0:%x "
  456. "source1:%x dest0:%x dest1:%x\n",
  457. dtp->dev_id, stp->dev_id, cmd0, cmd1, src0,
  458. src1, dest0, dest1);
  459. #endif
  460. for (i = 0; i < entries; i++) {
  461. dp->dscr_cmd0 = cmd0;
  462. dp->dscr_cmd1 = cmd1;
  463. dp->dscr_source0 = src0;
  464. dp->dscr_source1 = src1;
  465. dp->dscr_dest0 = dest0;
  466. dp->dscr_dest1 = dest1;
  467. dp->dscr_stat = 0;
  468. dp->sw_context = 0;
  469. dp->sw_status = 0;
  470. dp->dscr_nxtptr = DSCR_NXTPTR(virt_to_phys(dp + 1));
  471. dp++;
  472. }
  473. /* Make last descrptor point to the first. */
  474. dp--;
  475. dp->dscr_nxtptr = DSCR_NXTPTR(virt_to_phys(ctp->chan_desc_base));
  476. ctp->get_ptr = ctp->put_ptr = ctp->cur_ptr = ctp->chan_desc_base;
  477. return (u32)ctp->chan_desc_base;
  478. }
  479. EXPORT_SYMBOL(au1xxx_dbdma_ring_alloc);
  480. /*
  481. * Put a source buffer into the DMA ring.
  482. * This updates the source pointer and byte count. Normally used
  483. * for memory to fifo transfers.
  484. */
  485. u32 _au1xxx_dbdma_put_source(u32 chanid, void *buf, int nbytes, u32 flags)
  486. {
  487. chan_tab_t *ctp;
  488. au1x_ddma_desc_t *dp;
  489. /*
  490. * I guess we could check this to be within the
  491. * range of the table......
  492. */
  493. ctp = *(chan_tab_t **)chanid;
  494. /*
  495. * We should have multiple callers for a particular channel,
  496. * an interrupt doesn't affect this pointer nor the descriptor,
  497. * so no locking should be needed.
  498. */
  499. dp = ctp->put_ptr;
  500. /*
  501. * If the descriptor is valid, we are way ahead of the DMA
  502. * engine, so just return an error condition.
  503. */
  504. if (dp->dscr_cmd0 & DSCR_CMD0_V)
  505. return 0;
  506. /* Load up buffer address and byte count. */
  507. dp->dscr_source0 = virt_to_phys(buf);
  508. dp->dscr_cmd1 = nbytes;
  509. /* Check flags */
  510. if (flags & DDMA_FLAGS_IE)
  511. dp->dscr_cmd0 |= DSCR_CMD0_IE;
  512. if (flags & DDMA_FLAGS_NOIE)
  513. dp->dscr_cmd0 &= ~DSCR_CMD0_IE;
  514. /*
  515. * There is an errata on the Au1200/Au1550 parts that could result
  516. * in "stale" data being DMA'ed. It has to do with the snoop logic on
  517. * the cache eviction buffer. DMA_NONCOHERENT is on by default for
  518. * these parts. If it is fixed in the future, these dma_cache_inv will
  519. * just be nothing more than empty macros. See io.h.
  520. */
  521. dma_cache_wback_inv((unsigned long)buf, nbytes);
  522. dp->dscr_cmd0 |= DSCR_CMD0_V; /* Let it rip */
  523. au_sync();
  524. dma_cache_wback_inv((unsigned long)dp, sizeof(dp));
  525. ctp->chan_ptr->ddma_dbell = 0;
  526. /* Get next descriptor pointer. */
  527. ctp->put_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  528. /* Return something non-zero. */
  529. return nbytes;
  530. }
  531. EXPORT_SYMBOL(_au1xxx_dbdma_put_source);
  532. /* Put a destination buffer into the DMA ring.
  533. * This updates the destination pointer and byte count. Normally used
  534. * to place an empty buffer into the ring for fifo to memory transfers.
  535. */
  536. u32
  537. _au1xxx_dbdma_put_dest(u32 chanid, void *buf, int nbytes, u32 flags)
  538. {
  539. chan_tab_t *ctp;
  540. au1x_ddma_desc_t *dp;
  541. /* I guess we could check this to be within the
  542. * range of the table......
  543. */
  544. ctp = *((chan_tab_t **)chanid);
  545. /* We should have multiple callers for a particular channel,
  546. * an interrupt doesn't affect this pointer nor the descriptor,
  547. * so no locking should be needed.
  548. */
  549. dp = ctp->put_ptr;
  550. /* If the descriptor is valid, we are way ahead of the DMA
  551. * engine, so just return an error condition.
  552. */
  553. if (dp->dscr_cmd0 & DSCR_CMD0_V)
  554. return 0;
  555. /* Load up buffer address and byte count */
  556. /* Check flags */
  557. if (flags & DDMA_FLAGS_IE)
  558. dp->dscr_cmd0 |= DSCR_CMD0_IE;
  559. if (flags & DDMA_FLAGS_NOIE)
  560. dp->dscr_cmd0 &= ~DSCR_CMD0_IE;
  561. dp->dscr_dest0 = virt_to_phys(buf);
  562. dp->dscr_cmd1 = nbytes;
  563. #if 0
  564. printk(KERN_DEBUG "cmd0:%x cmd1:%x source0:%x source1:%x dest0:%x dest1:%x\n",
  565. dp->dscr_cmd0, dp->dscr_cmd1, dp->dscr_source0,
  566. dp->dscr_source1, dp->dscr_dest0, dp->dscr_dest1);
  567. #endif
  568. /*
  569. * There is an errata on the Au1200/Au1550 parts that could result in
  570. * "stale" data being DMA'ed. It has to do with the snoop logic on the
  571. * cache eviction buffer. DMA_NONCOHERENT is on by default for these
  572. * parts. If it is fixed in the future, these dma_cache_inv will just
  573. * be nothing more than empty macros. See io.h.
  574. */
  575. dma_cache_inv((unsigned long)buf, nbytes);
  576. dp->dscr_cmd0 |= DSCR_CMD0_V; /* Let it rip */
  577. au_sync();
  578. dma_cache_wback_inv((unsigned long)dp, sizeof(dp));
  579. ctp->chan_ptr->ddma_dbell = 0;
  580. /* Get next descriptor pointer. */
  581. ctp->put_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  582. /* Return something non-zero. */
  583. return nbytes;
  584. }
  585. EXPORT_SYMBOL(_au1xxx_dbdma_put_dest);
  586. /*
  587. * Get a destination buffer into the DMA ring.
  588. * Normally used to get a full buffer from the ring during fifo
  589. * to memory transfers. This does not set the valid bit, you will
  590. * have to put another destination buffer to keep the DMA going.
  591. */
  592. u32 au1xxx_dbdma_get_dest(u32 chanid, void **buf, int *nbytes)
  593. {
  594. chan_tab_t *ctp;
  595. au1x_ddma_desc_t *dp;
  596. u32 rv;
  597. /*
  598. * I guess we could check this to be within the
  599. * range of the table......
  600. */
  601. ctp = *((chan_tab_t **)chanid);
  602. /*
  603. * We should have multiple callers for a particular channel,
  604. * an interrupt doesn't affect this pointer nor the descriptor,
  605. * so no locking should be needed.
  606. */
  607. dp = ctp->get_ptr;
  608. /*
  609. * If the descriptor is valid, we are way ahead of the DMA
  610. * engine, so just return an error condition.
  611. */
  612. if (dp->dscr_cmd0 & DSCR_CMD0_V)
  613. return 0;
  614. /* Return buffer address and byte count. */
  615. *buf = (void *)(phys_to_virt(dp->dscr_dest0));
  616. *nbytes = dp->dscr_cmd1;
  617. rv = dp->dscr_stat;
  618. /* Get next descriptor pointer. */
  619. ctp->get_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  620. /* Return something non-zero. */
  621. return rv;
  622. }
  623. EXPORT_SYMBOL_GPL(au1xxx_dbdma_get_dest);
  624. void au1xxx_dbdma_stop(u32 chanid)
  625. {
  626. chan_tab_t *ctp;
  627. au1x_dma_chan_t *cp;
  628. int halt_timeout = 0;
  629. ctp = *((chan_tab_t **)chanid);
  630. cp = ctp->chan_ptr;
  631. cp->ddma_cfg &= ~DDMA_CFG_EN; /* Disable channel */
  632. au_sync();
  633. while (!(cp->ddma_stat & DDMA_STAT_H)) {
  634. udelay(1);
  635. halt_timeout++;
  636. if (halt_timeout > 100) {
  637. printk(KERN_WARNING "warning: DMA channel won't halt\n");
  638. break;
  639. }
  640. }
  641. /* clear current desc valid and doorbell */
  642. cp->ddma_stat |= (DDMA_STAT_DB | DDMA_STAT_V);
  643. au_sync();
  644. }
  645. EXPORT_SYMBOL(au1xxx_dbdma_stop);
  646. /*
  647. * Start using the current descriptor pointer. If the DBDMA encounters
  648. * a non-valid descriptor, it will stop. In this case, we can just
  649. * continue by adding a buffer to the list and starting again.
  650. */
  651. void au1xxx_dbdma_start(u32 chanid)
  652. {
  653. chan_tab_t *ctp;
  654. au1x_dma_chan_t *cp;
  655. ctp = *((chan_tab_t **)chanid);
  656. cp = ctp->chan_ptr;
  657. cp->ddma_desptr = virt_to_phys(ctp->cur_ptr);
  658. cp->ddma_cfg |= DDMA_CFG_EN; /* Enable channel */
  659. au_sync();
  660. cp->ddma_dbell = 0;
  661. au_sync();
  662. }
  663. EXPORT_SYMBOL(au1xxx_dbdma_start);
  664. void au1xxx_dbdma_reset(u32 chanid)
  665. {
  666. chan_tab_t *ctp;
  667. au1x_ddma_desc_t *dp;
  668. au1xxx_dbdma_stop(chanid);
  669. ctp = *((chan_tab_t **)chanid);
  670. ctp->get_ptr = ctp->put_ptr = ctp->cur_ptr = ctp->chan_desc_base;
  671. /* Run through the descriptors and reset the valid indicator. */
  672. dp = ctp->chan_desc_base;
  673. do {
  674. dp->dscr_cmd0 &= ~DSCR_CMD0_V;
  675. /*
  676. * Reset our software status -- this is used to determine
  677. * if a descriptor is in use by upper level software. Since
  678. * posting can reset 'V' bit.
  679. */
  680. dp->sw_status = 0;
  681. dp = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  682. } while (dp != ctp->chan_desc_base);
  683. }
  684. EXPORT_SYMBOL(au1xxx_dbdma_reset);
  685. u32 au1xxx_get_dma_residue(u32 chanid)
  686. {
  687. chan_tab_t *ctp;
  688. au1x_dma_chan_t *cp;
  689. u32 rv;
  690. ctp = *((chan_tab_t **)chanid);
  691. cp = ctp->chan_ptr;
  692. /* This is only valid if the channel is stopped. */
  693. rv = cp->ddma_bytecnt;
  694. au_sync();
  695. return rv;
  696. }
  697. EXPORT_SYMBOL_GPL(au1xxx_get_dma_residue);
  698. void au1xxx_dbdma_chan_free(u32 chanid)
  699. {
  700. chan_tab_t *ctp;
  701. dbdev_tab_t *stp, *dtp;
  702. ctp = *((chan_tab_t **)chanid);
  703. stp = ctp->chan_src;
  704. dtp = ctp->chan_dest;
  705. au1xxx_dbdma_stop(chanid);
  706. kfree((void *)ctp->chan_desc_base);
  707. stp->dev_flags &= ~DEV_FLAGS_INUSE;
  708. dtp->dev_flags &= ~DEV_FLAGS_INUSE;
  709. chan_tab_ptr[ctp->chan_index] = NULL;
  710. kfree(ctp);
  711. }
  712. EXPORT_SYMBOL(au1xxx_dbdma_chan_free);
  713. static irqreturn_t dbdma_interrupt(int irq, void *dev_id)
  714. {
  715. u32 intstat;
  716. u32 chan_index;
  717. chan_tab_t *ctp;
  718. au1x_ddma_desc_t *dp;
  719. au1x_dma_chan_t *cp;
  720. intstat = dbdma_gptr->ddma_intstat;
  721. au_sync();
  722. chan_index = __ffs(intstat);
  723. ctp = chan_tab_ptr[chan_index];
  724. cp = ctp->chan_ptr;
  725. dp = ctp->cur_ptr;
  726. /* Reset interrupt. */
  727. cp->ddma_irq = 0;
  728. au_sync();
  729. if (ctp->chan_callback)
  730. ctp->chan_callback(irq, ctp->chan_callparam);
  731. ctp->cur_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  732. return IRQ_RETVAL(1);
  733. }
  734. static void au1xxx_dbdma_init(void)
  735. {
  736. int irq_nr;
  737. dbdma_gptr->ddma_config = 0;
  738. dbdma_gptr->ddma_throttle = 0;
  739. dbdma_gptr->ddma_inten = 0xffff;
  740. au_sync();
  741. #if defined(CONFIG_SOC_AU1550)
  742. irq_nr = AU1550_DDMA_INT;
  743. #elif defined(CONFIG_SOC_AU1200)
  744. irq_nr = AU1200_DDMA_INT;
  745. #else
  746. #error Unknown Au1x00 SOC
  747. #endif
  748. if (request_irq(irq_nr, dbdma_interrupt, IRQF_DISABLED,
  749. "Au1xxx dbdma", (void *)dbdma_gptr))
  750. printk(KERN_ERR "Can't get 1550 dbdma irq");
  751. }
  752. void au1xxx_dbdma_dump(u32 chanid)
  753. {
  754. chan_tab_t *ctp;
  755. au1x_ddma_desc_t *dp;
  756. dbdev_tab_t *stp, *dtp;
  757. au1x_dma_chan_t *cp;
  758. u32 i = 0;
  759. ctp = *((chan_tab_t **)chanid);
  760. stp = ctp->chan_src;
  761. dtp = ctp->chan_dest;
  762. cp = ctp->chan_ptr;
  763. printk(KERN_DEBUG "Chan %x, stp %x (dev %d) dtp %x (dev %d) \n",
  764. (u32)ctp, (u32)stp, stp - dbdev_tab, (u32)dtp,
  765. dtp - dbdev_tab);
  766. printk(KERN_DEBUG "desc base %x, get %x, put %x, cur %x\n",
  767. (u32)(ctp->chan_desc_base), (u32)(ctp->get_ptr),
  768. (u32)(ctp->put_ptr), (u32)(ctp->cur_ptr));
  769. printk(KERN_DEBUG "dbdma chan %x\n", (u32)cp);
  770. printk(KERN_DEBUG "cfg %08x, desptr %08x, statptr %08x\n",
  771. cp->ddma_cfg, cp->ddma_desptr, cp->ddma_statptr);
  772. printk(KERN_DEBUG "dbell %08x, irq %08x, stat %08x, bytecnt %08x\n",
  773. cp->ddma_dbell, cp->ddma_irq, cp->ddma_stat,
  774. cp->ddma_bytecnt);
  775. /* Run through the descriptors */
  776. dp = ctp->chan_desc_base;
  777. do {
  778. printk(KERN_DEBUG "Dp[%d]= %08x, cmd0 %08x, cmd1 %08x\n",
  779. i++, (u32)dp, dp->dscr_cmd0, dp->dscr_cmd1);
  780. printk(KERN_DEBUG "src0 %08x, src1 %08x, dest0 %08x, dest1 %08x\n",
  781. dp->dscr_source0, dp->dscr_source1,
  782. dp->dscr_dest0, dp->dscr_dest1);
  783. printk(KERN_DEBUG "stat %08x, nxtptr %08x\n",
  784. dp->dscr_stat, dp->dscr_nxtptr);
  785. dp = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  786. } while (dp != ctp->chan_desc_base);
  787. }
  788. /* Put a descriptor into the DMA ring.
  789. * This updates the source/destination pointers and byte count.
  790. */
  791. u32 au1xxx_dbdma_put_dscr(u32 chanid, au1x_ddma_desc_t *dscr)
  792. {
  793. chan_tab_t *ctp;
  794. au1x_ddma_desc_t *dp;
  795. u32 nbytes = 0;
  796. /*
  797. * I guess we could check this to be within the
  798. * range of the table......
  799. */
  800. ctp = *((chan_tab_t **)chanid);
  801. /*
  802. * We should have multiple callers for a particular channel,
  803. * an interrupt doesn't affect this pointer nor the descriptor,
  804. * so no locking should be needed.
  805. */
  806. dp = ctp->put_ptr;
  807. /*
  808. * If the descriptor is valid, we are way ahead of the DMA
  809. * engine, so just return an error condition.
  810. */
  811. if (dp->dscr_cmd0 & DSCR_CMD0_V)
  812. return 0;
  813. /* Load up buffer addresses and byte count. */
  814. dp->dscr_dest0 = dscr->dscr_dest0;
  815. dp->dscr_source0 = dscr->dscr_source0;
  816. dp->dscr_dest1 = dscr->dscr_dest1;
  817. dp->dscr_source1 = dscr->dscr_source1;
  818. dp->dscr_cmd1 = dscr->dscr_cmd1;
  819. nbytes = dscr->dscr_cmd1;
  820. /* Allow the caller to specifiy if an interrupt is generated */
  821. dp->dscr_cmd0 &= ~DSCR_CMD0_IE;
  822. dp->dscr_cmd0 |= dscr->dscr_cmd0 | DSCR_CMD0_V;
  823. ctp->chan_ptr->ddma_dbell = 0;
  824. /* Get next descriptor pointer. */
  825. ctp->put_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  826. /* Return something non-zero. */
  827. return nbytes;
  828. }
  829. #endif /* defined(CONFIG_SOC_AU1550) || defined(CONFIG_SOC_AU1200) */