dbdma.c 26 KB

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