dbdma.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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 *), 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)
  213. return 0;
  214. if ((dtp = find_dbdev_id(destid)) == NULL)
  215. return 0;
  216. used = 0;
  217. rv = 0;
  218. /* Check to see if we can get both channels.
  219. */
  220. spin_lock_irqsave(&au1xxx_dbdma_spin_lock, flags);
  221. if (!(stp->dev_flags & DEV_FLAGS_INUSE) ||
  222. (stp->dev_flags & DEV_FLAGS_ANYUSE)) {
  223. /* Got source */
  224. stp->dev_flags |= DEV_FLAGS_INUSE;
  225. if (!(dtp->dev_flags & DEV_FLAGS_INUSE) ||
  226. (dtp->dev_flags & DEV_FLAGS_ANYUSE)) {
  227. /* Got destination */
  228. dtp->dev_flags |= DEV_FLAGS_INUSE;
  229. }
  230. else {
  231. /* Can't get dest. Release src.
  232. */
  233. stp->dev_flags &= ~DEV_FLAGS_INUSE;
  234. used++;
  235. }
  236. }
  237. else {
  238. used++;
  239. }
  240. spin_unlock_irqrestore(&au1xxx_dbdma_spin_lock, flags);
  241. if (!used) {
  242. /* Let's see if we can allocate a channel for it.
  243. */
  244. ctp = NULL;
  245. chan = 0;
  246. spin_lock_irqsave(&au1xxx_dbdma_spin_lock, flags);
  247. for (i=0; i<NUM_DBDMA_CHANS; i++) {
  248. if (chan_tab_ptr[i] == NULL) {
  249. /* If kmalloc fails, it is caught below same
  250. * as a channel not available.
  251. */
  252. ctp = kmalloc(sizeof(chan_tab_t), GFP_ATOMIC);
  253. chan_tab_ptr[i] = ctp;
  254. break;
  255. }
  256. }
  257. spin_unlock_irqrestore(&au1xxx_dbdma_spin_lock, flags);
  258. if (ctp != NULL) {
  259. memset(ctp, 0, sizeof(chan_tab_t));
  260. ctp->chan_index = chan = i;
  261. dcp = DDMA_CHANNEL_BASE;
  262. dcp += (0x0100 * chan);
  263. ctp->chan_ptr = (au1x_dma_chan_t *)dcp;
  264. cp = (au1x_dma_chan_t *)dcp;
  265. ctp->chan_src = stp;
  266. ctp->chan_dest = dtp;
  267. ctp->chan_callback = callback;
  268. ctp->chan_callparam = callparam;
  269. /* Initialize channel configuration.
  270. */
  271. i = 0;
  272. if (stp->dev_intlevel)
  273. i |= DDMA_CFG_SED;
  274. if (stp->dev_intpolarity)
  275. i |= DDMA_CFG_SP;
  276. if (dtp->dev_intlevel)
  277. i |= DDMA_CFG_DED;
  278. if (dtp->dev_intpolarity)
  279. i |= DDMA_CFG_DP;
  280. if ((stp->dev_flags & DEV_FLAGS_SYNC) ||
  281. (dtp->dev_flags & DEV_FLAGS_SYNC))
  282. i |= DDMA_CFG_SYNC;
  283. cp->ddma_cfg = i;
  284. au_sync();
  285. /* Return a non-zero value that can be used to
  286. * find the channel information in subsequent
  287. * operations.
  288. */
  289. rv = (u32)(&chan_tab_ptr[chan]);
  290. }
  291. else {
  292. /* Release devices */
  293. stp->dev_flags &= ~DEV_FLAGS_INUSE;
  294. dtp->dev_flags &= ~DEV_FLAGS_INUSE;
  295. }
  296. }
  297. return rv;
  298. }
  299. EXPORT_SYMBOL(au1xxx_dbdma_chan_alloc);
  300. /* Set the device width if source or destination is a FIFO.
  301. * Should be 8, 16, or 32 bits.
  302. */
  303. u32
  304. au1xxx_dbdma_set_devwidth(u32 chanid, int bits)
  305. {
  306. u32 rv;
  307. chan_tab_t *ctp;
  308. dbdev_tab_t *stp, *dtp;
  309. ctp = *((chan_tab_t **)chanid);
  310. stp = ctp->chan_src;
  311. dtp = ctp->chan_dest;
  312. rv = 0;
  313. if (stp->dev_flags & DEV_FLAGS_IN) { /* Source in fifo */
  314. rv = stp->dev_devwidth;
  315. stp->dev_devwidth = bits;
  316. }
  317. if (dtp->dev_flags & DEV_FLAGS_OUT) { /* Destination out fifo */
  318. rv = dtp->dev_devwidth;
  319. dtp->dev_devwidth = bits;
  320. }
  321. return rv;
  322. }
  323. EXPORT_SYMBOL(au1xxx_dbdma_set_devwidth);
  324. /* Allocate a descriptor ring, initializing as much as possible.
  325. */
  326. u32
  327. au1xxx_dbdma_ring_alloc(u32 chanid, int entries)
  328. {
  329. int i;
  330. u32 desc_base, srcid, destid;
  331. u32 cmd0, cmd1, src1, dest1;
  332. u32 src0, dest0;
  333. chan_tab_t *ctp;
  334. dbdev_tab_t *stp, *dtp;
  335. au1x_ddma_desc_t *dp;
  336. /* I guess we could check this to be within the
  337. * range of the table......
  338. */
  339. ctp = *((chan_tab_t **)chanid);
  340. stp = ctp->chan_src;
  341. dtp = ctp->chan_dest;
  342. /* The descriptors must be 32-byte aligned. There is a
  343. * possibility the allocation will give us such an address,
  344. * and if we try that first we are likely to not waste larger
  345. * slabs of memory.
  346. */
  347. desc_base = (u32)kmalloc(entries * sizeof(au1x_ddma_desc_t),
  348. GFP_KERNEL|GFP_DMA);
  349. if (desc_base == 0)
  350. return 0;
  351. if (desc_base & 0x1f) {
  352. /* Lost....do it again, allocate extra, and round
  353. * the address base.
  354. */
  355. kfree((const void *)desc_base);
  356. i = entries * sizeof(au1x_ddma_desc_t);
  357. i += (sizeof(au1x_ddma_desc_t) - 1);
  358. if ((desc_base = (u32)kmalloc(i, GFP_KERNEL|GFP_DMA)) == 0)
  359. return 0;
  360. desc_base = ALIGN_ADDR(desc_base, sizeof(au1x_ddma_desc_t));
  361. }
  362. dp = (au1x_ddma_desc_t *)desc_base;
  363. /* Keep track of the base descriptor.
  364. */
  365. ctp->chan_desc_base = dp;
  366. /* Initialize the rings with as much information as we know.
  367. */
  368. srcid = stp->dev_id;
  369. destid = dtp->dev_id;
  370. cmd0 = cmd1 = src1 = dest1 = 0;
  371. src0 = dest0 = 0;
  372. cmd0 |= DSCR_CMD0_SID(srcid);
  373. cmd0 |= DSCR_CMD0_DID(destid);
  374. cmd0 |= DSCR_CMD0_IE | DSCR_CMD0_CV;
  375. cmd0 |= DSCR_CMD0_ST(DSCR_CMD0_ST_NOCHANGE);
  376. /* is it mem to mem transfer? */
  377. if(((DSCR_CUSTOM2DEV_ID(srcid) == DSCR_CMD0_THROTTLE) || (DSCR_CUSTOM2DEV_ID(srcid) == DSCR_CMD0_ALWAYS)) &&
  378. ((DSCR_CUSTOM2DEV_ID(destid) == DSCR_CMD0_THROTTLE) || (DSCR_CUSTOM2DEV_ID(destid) == DSCR_CMD0_ALWAYS))) {
  379. cmd0 |= DSCR_CMD0_MEM;
  380. }
  381. switch (stp->dev_devwidth) {
  382. case 8:
  383. cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_BYTE);
  384. break;
  385. case 16:
  386. cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_HALFWORD);
  387. break;
  388. case 32:
  389. default:
  390. cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_WORD);
  391. break;
  392. }
  393. switch (dtp->dev_devwidth) {
  394. case 8:
  395. cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_BYTE);
  396. break;
  397. case 16:
  398. cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_HALFWORD);
  399. break;
  400. case 32:
  401. default:
  402. cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_WORD);
  403. break;
  404. }
  405. /* If the device is marked as an in/out FIFO, ensure it is
  406. * set non-coherent.
  407. */
  408. if (stp->dev_flags & DEV_FLAGS_IN)
  409. cmd0 |= DSCR_CMD0_SN; /* Source in fifo */
  410. if (dtp->dev_flags & DEV_FLAGS_OUT)
  411. cmd0 |= DSCR_CMD0_DN; /* Destination out fifo */
  412. /* Set up source1. For now, assume no stride and increment.
  413. * A channel attribute update can change this later.
  414. */
  415. switch (stp->dev_tsize) {
  416. case 1:
  417. src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE1);
  418. break;
  419. case 2:
  420. src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE2);
  421. break;
  422. case 4:
  423. src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE4);
  424. break;
  425. case 8:
  426. default:
  427. src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE8);
  428. break;
  429. }
  430. /* If source input is fifo, set static address.
  431. */
  432. if (stp->dev_flags & DEV_FLAGS_IN) {
  433. if ( stp->dev_flags & DEV_FLAGS_BURSTABLE )
  434. src1 |= DSCR_SRC1_SAM(DSCR_xAM_BURST);
  435. else
  436. src1 |= DSCR_SRC1_SAM(DSCR_xAM_STATIC);
  437. }
  438. if (stp->dev_physaddr)
  439. src0 = stp->dev_physaddr;
  440. /* Set up dest1. For now, assume no stride and increment.
  441. * A channel attribute update can change this later.
  442. */
  443. switch (dtp->dev_tsize) {
  444. case 1:
  445. dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE1);
  446. break;
  447. case 2:
  448. dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE2);
  449. break;
  450. case 4:
  451. dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE4);
  452. break;
  453. case 8:
  454. default:
  455. dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE8);
  456. break;
  457. }
  458. /* If destination output is fifo, set static address.
  459. */
  460. if (dtp->dev_flags & DEV_FLAGS_OUT) {
  461. if ( dtp->dev_flags & DEV_FLAGS_BURSTABLE )
  462. dest1 |= DSCR_DEST1_DAM(DSCR_xAM_BURST);
  463. else
  464. dest1 |= DSCR_DEST1_DAM(DSCR_xAM_STATIC);
  465. }
  466. if (dtp->dev_physaddr)
  467. dest0 = dtp->dev_physaddr;
  468. #if 0
  469. printk("did:%x sid:%x cmd0:%x cmd1:%x source0:%x source1:%x dest0:%x dest1:%x\n",
  470. dtp->dev_id, stp->dev_id, cmd0, cmd1, src0, src1, dest0, dest1 );
  471. #endif
  472. for (i=0; i<entries; i++) {
  473. dp->dscr_cmd0 = cmd0;
  474. dp->dscr_cmd1 = cmd1;
  475. dp->dscr_source0 = src0;
  476. dp->dscr_source1 = src1;
  477. dp->dscr_dest0 = dest0;
  478. dp->dscr_dest1 = dest1;
  479. dp->dscr_stat = 0;
  480. dp->sw_context = 0;
  481. dp->sw_status = 0;
  482. dp->dscr_nxtptr = DSCR_NXTPTR(virt_to_phys(dp + 1));
  483. dp++;
  484. }
  485. /* Make last descrptor point to the first.
  486. */
  487. dp--;
  488. dp->dscr_nxtptr = DSCR_NXTPTR(virt_to_phys(ctp->chan_desc_base));
  489. ctp->get_ptr = ctp->put_ptr = ctp->cur_ptr = ctp->chan_desc_base;
  490. return (u32)(ctp->chan_desc_base);
  491. }
  492. EXPORT_SYMBOL(au1xxx_dbdma_ring_alloc);
  493. /* Put a source buffer into the DMA ring.
  494. * This updates the source pointer and byte count. Normally used
  495. * for memory to fifo transfers.
  496. */
  497. u32
  498. _au1xxx_dbdma_put_source(u32 chanid, void *buf, int nbytes, u32 flags)
  499. {
  500. chan_tab_t *ctp;
  501. au1x_ddma_desc_t *dp;
  502. /* I guess we could check this to be within the
  503. * range of the table......
  504. */
  505. ctp = *((chan_tab_t **)chanid);
  506. /* We should have multiple callers for a particular channel,
  507. * an interrupt doesn't affect this pointer nor the descriptor,
  508. * so no locking should be needed.
  509. */
  510. dp = ctp->put_ptr;
  511. /* If the descriptor is valid, we are way ahead of the DMA
  512. * engine, so just return an error condition.
  513. */
  514. if (dp->dscr_cmd0 & DSCR_CMD0_V) {
  515. return 0;
  516. }
  517. /* Load up buffer address and byte count.
  518. */
  519. dp->dscr_source0 = virt_to_phys(buf);
  520. dp->dscr_cmd1 = nbytes;
  521. /* Check flags */
  522. if (flags & DDMA_FLAGS_IE)
  523. dp->dscr_cmd0 |= DSCR_CMD0_IE;
  524. if (flags & DDMA_FLAGS_NOIE)
  525. dp->dscr_cmd0 &= ~DSCR_CMD0_IE;
  526. /*
  527. * There is an errata on the Au1200/Au1550 parts that could result
  528. * in "stale" data being DMA'd. It has to do with the snoop logic on
  529. * the dache eviction buffer. NONCOHERENT_IO is on by default for
  530. * these parts. If it is fixedin the future, these dma_cache_inv will
  531. * just be nothing more than empty macros. See io.h.
  532. * */
  533. dma_cache_wback_inv((unsigned long)buf, nbytes);
  534. dp->dscr_cmd0 |= DSCR_CMD0_V; /* Let it rip */
  535. au_sync();
  536. dma_cache_wback_inv((unsigned long)dp, sizeof(dp));
  537. ctp->chan_ptr->ddma_dbell = 0;
  538. /* Get next descriptor pointer.
  539. */
  540. ctp->put_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  541. /* return something not zero.
  542. */
  543. return nbytes;
  544. }
  545. EXPORT_SYMBOL(_au1xxx_dbdma_put_source);
  546. /* Put a destination buffer into the DMA ring.
  547. * This updates the destination pointer and byte count. Normally used
  548. * to place an empty buffer into the ring for fifo to memory transfers.
  549. */
  550. u32
  551. _au1xxx_dbdma_put_dest(u32 chanid, void *buf, int nbytes, u32 flags)
  552. {
  553. chan_tab_t *ctp;
  554. au1x_ddma_desc_t *dp;
  555. /* I guess we could check this to be within the
  556. * range of the table......
  557. */
  558. ctp = *((chan_tab_t **)chanid);
  559. /* We should have multiple callers for a particular channel,
  560. * an interrupt doesn't affect this pointer nor the descriptor,
  561. * so no locking should be needed.
  562. */
  563. dp = ctp->put_ptr;
  564. /* If the descriptor is valid, we are way ahead of the DMA
  565. * engine, so just return an error condition.
  566. */
  567. if (dp->dscr_cmd0 & DSCR_CMD0_V)
  568. return 0;
  569. /* Load up buffer address and byte count */
  570. /* Check flags */
  571. if (flags & DDMA_FLAGS_IE)
  572. dp->dscr_cmd0 |= DSCR_CMD0_IE;
  573. if (flags & DDMA_FLAGS_NOIE)
  574. dp->dscr_cmd0 &= ~DSCR_CMD0_IE;
  575. dp->dscr_dest0 = virt_to_phys(buf);
  576. dp->dscr_cmd1 = nbytes;
  577. #if 0
  578. printk("cmd0:%x cmd1:%x source0:%x source1:%x dest0:%x dest1:%x\n",
  579. dp->dscr_cmd0, dp->dscr_cmd1, dp->dscr_source0,
  580. dp->dscr_source1, dp->dscr_dest0, dp->dscr_dest1 );
  581. #endif
  582. /*
  583. * There is an errata on the Au1200/Au1550 parts that could result in
  584. * "stale" data being DMA'd. It has to do with the snoop logic on the
  585. * dache eviction buffer. NONCOHERENT_IO is on by default for these
  586. * parts. If it is fixedin the future, these dma_cache_inv will just
  587. * be nothing more than empty macros. See io.h.
  588. * */
  589. dma_cache_inv((unsigned long)buf,nbytes);
  590. dp->dscr_cmd0 |= DSCR_CMD0_V; /* Let it rip */
  591. au_sync();
  592. dma_cache_wback_inv((unsigned long)dp, sizeof(dp));
  593. ctp->chan_ptr->ddma_dbell = 0;
  594. /* Get next descriptor pointer.
  595. */
  596. ctp->put_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  597. /* return something not zero.
  598. */
  599. return nbytes;
  600. }
  601. EXPORT_SYMBOL(_au1xxx_dbdma_put_dest);
  602. /* Get a destination buffer into the DMA ring.
  603. * Normally used to get a full buffer from the ring during fifo
  604. * to memory transfers. This does not set the valid bit, you will
  605. * have to put another destination buffer to keep the DMA going.
  606. */
  607. u32
  608. au1xxx_dbdma_get_dest(u32 chanid, void **buf, int *nbytes)
  609. {
  610. chan_tab_t *ctp;
  611. au1x_ddma_desc_t *dp;
  612. u32 rv;
  613. /* I guess we could check this to be within the
  614. * range of the table......
  615. */
  616. ctp = *((chan_tab_t **)chanid);
  617. /* We should have multiple callers for a particular channel,
  618. * an interrupt doesn't affect this pointer nor the descriptor,
  619. * so no locking should be needed.
  620. */
  621. dp = ctp->get_ptr;
  622. /* If the descriptor is valid, we are way ahead of the DMA
  623. * engine, so just return an error condition.
  624. */
  625. if (dp->dscr_cmd0 & DSCR_CMD0_V)
  626. return 0;
  627. /* Return buffer address and byte count.
  628. */
  629. *buf = (void *)(phys_to_virt(dp->dscr_dest0));
  630. *nbytes = dp->dscr_cmd1;
  631. rv = dp->dscr_stat;
  632. /* Get next descriptor pointer.
  633. */
  634. ctp->get_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  635. /* return something not zero.
  636. */
  637. return rv;
  638. }
  639. EXPORT_SYMBOL_GPL(au1xxx_dbdma_get_dest);
  640. void
  641. au1xxx_dbdma_stop(u32 chanid)
  642. {
  643. chan_tab_t *ctp;
  644. au1x_dma_chan_t *cp;
  645. int halt_timeout = 0;
  646. ctp = *((chan_tab_t **)chanid);
  647. cp = ctp->chan_ptr;
  648. cp->ddma_cfg &= ~DDMA_CFG_EN; /* Disable channel */
  649. au_sync();
  650. while (!(cp->ddma_stat & DDMA_STAT_H)) {
  651. udelay(1);
  652. halt_timeout++;
  653. if (halt_timeout > 100) {
  654. printk("warning: DMA channel won't halt\n");
  655. break;
  656. }
  657. }
  658. /* clear current desc valid and doorbell */
  659. cp->ddma_stat |= (DDMA_STAT_DB | DDMA_STAT_V);
  660. au_sync();
  661. }
  662. EXPORT_SYMBOL(au1xxx_dbdma_stop);
  663. /* Start using the current descriptor pointer. If the dbdma encounters
  664. * a not valid descriptor, it will stop. In this case, we can just
  665. * continue by adding a buffer to the list and starting again.
  666. */
  667. void
  668. au1xxx_dbdma_start(u32 chanid)
  669. {
  670. chan_tab_t *ctp;
  671. au1x_dma_chan_t *cp;
  672. ctp = *((chan_tab_t **)chanid);
  673. cp = ctp->chan_ptr;
  674. cp->ddma_desptr = virt_to_phys(ctp->cur_ptr);
  675. cp->ddma_cfg |= DDMA_CFG_EN; /* Enable channel */
  676. au_sync();
  677. cp->ddma_dbell = 0;
  678. au_sync();
  679. }
  680. EXPORT_SYMBOL(au1xxx_dbdma_start);
  681. void
  682. au1xxx_dbdma_reset(u32 chanid)
  683. {
  684. chan_tab_t *ctp;
  685. au1x_ddma_desc_t *dp;
  686. au1xxx_dbdma_stop(chanid);
  687. ctp = *((chan_tab_t **)chanid);
  688. ctp->get_ptr = ctp->put_ptr = ctp->cur_ptr = ctp->chan_desc_base;
  689. /* Run through the descriptors and reset the valid indicator.
  690. */
  691. dp = ctp->chan_desc_base;
  692. do {
  693. dp->dscr_cmd0 &= ~DSCR_CMD0_V;
  694. /* reset our SW status -- this is used to determine
  695. * if a descriptor is in use by upper level SW. Since
  696. * posting can reset 'V' bit.
  697. */
  698. dp->sw_status = 0;
  699. dp = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  700. } while (dp != ctp->chan_desc_base);
  701. }
  702. EXPORT_SYMBOL(au1xxx_dbdma_reset);
  703. u32
  704. au1xxx_get_dma_residue(u32 chanid)
  705. {
  706. chan_tab_t *ctp;
  707. au1x_dma_chan_t *cp;
  708. u32 rv;
  709. ctp = *((chan_tab_t **)chanid);
  710. cp = ctp->chan_ptr;
  711. /* This is only valid if the channel is stopped.
  712. */
  713. rv = cp->ddma_bytecnt;
  714. au_sync();
  715. return rv;
  716. }
  717. EXPORT_SYMBOL_GPL(au1xxx_get_dma_residue);
  718. void
  719. au1xxx_dbdma_chan_free(u32 chanid)
  720. {
  721. chan_tab_t *ctp;
  722. dbdev_tab_t *stp, *dtp;
  723. ctp = *((chan_tab_t **)chanid);
  724. stp = ctp->chan_src;
  725. dtp = ctp->chan_dest;
  726. au1xxx_dbdma_stop(chanid);
  727. kfree((void *)ctp->chan_desc_base);
  728. stp->dev_flags &= ~DEV_FLAGS_INUSE;
  729. dtp->dev_flags &= ~DEV_FLAGS_INUSE;
  730. chan_tab_ptr[ctp->chan_index] = NULL;
  731. kfree(ctp);
  732. }
  733. EXPORT_SYMBOL(au1xxx_dbdma_chan_free);
  734. static irqreturn_t
  735. dbdma_interrupt(int irq, void *dev_id)
  736. {
  737. u32 intstat;
  738. u32 chan_index;
  739. chan_tab_t *ctp;
  740. au1x_ddma_desc_t *dp;
  741. au1x_dma_chan_t *cp;
  742. intstat = dbdma_gptr->ddma_intstat;
  743. au_sync();
  744. chan_index = au_ffs(intstat) - 1;
  745. ctp = chan_tab_ptr[chan_index];
  746. cp = ctp->chan_ptr;
  747. dp = ctp->cur_ptr;
  748. /* Reset interrupt.
  749. */
  750. cp->ddma_irq = 0;
  751. au_sync();
  752. if (ctp->chan_callback)
  753. (ctp->chan_callback)(irq, ctp->chan_callparam);
  754. ctp->cur_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  755. return IRQ_RETVAL(1);
  756. }
  757. static void au1xxx_dbdma_init(void)
  758. {
  759. int irq_nr;
  760. dbdma_gptr->ddma_config = 0;
  761. dbdma_gptr->ddma_throttle = 0;
  762. dbdma_gptr->ddma_inten = 0xffff;
  763. au_sync();
  764. #if defined(CONFIG_SOC_AU1550)
  765. irq_nr = AU1550_DDMA_INT;
  766. #elif defined(CONFIG_SOC_AU1200)
  767. irq_nr = AU1200_DDMA_INT;
  768. #else
  769. #error Unknown Au1x00 SOC
  770. #endif
  771. if (request_irq(irq_nr, dbdma_interrupt, IRQF_DISABLED,
  772. "Au1xxx dbdma", (void *)dbdma_gptr))
  773. printk("Can't get 1550 dbdma irq");
  774. }
  775. void
  776. au1xxx_dbdma_dump(u32 chanid)
  777. {
  778. chan_tab_t *ctp;
  779. au1x_ddma_desc_t *dp;
  780. dbdev_tab_t *stp, *dtp;
  781. au1x_dma_chan_t *cp;
  782. u32 i = 0;
  783. ctp = *((chan_tab_t **)chanid);
  784. stp = ctp->chan_src;
  785. dtp = ctp->chan_dest;
  786. cp = ctp->chan_ptr;
  787. printk("Chan %x, stp %x (dev %d) dtp %x (dev %d) \n",
  788. (u32)ctp, (u32)stp, stp - dbdev_tab, (u32)dtp, dtp - dbdev_tab);
  789. printk("desc base %x, get %x, put %x, cur %x\n",
  790. (u32)(ctp->chan_desc_base), (u32)(ctp->get_ptr),
  791. (u32)(ctp->put_ptr), (u32)(ctp->cur_ptr));
  792. printk("dbdma chan %x\n", (u32)cp);
  793. printk("cfg %08x, desptr %08x, statptr %08x\n",
  794. cp->ddma_cfg, cp->ddma_desptr, cp->ddma_statptr);
  795. printk("dbell %08x, irq %08x, stat %08x, bytecnt %08x\n",
  796. cp->ddma_dbell, cp->ddma_irq, cp->ddma_stat, cp->ddma_bytecnt);
  797. /* Run through the descriptors
  798. */
  799. dp = ctp->chan_desc_base;
  800. do {
  801. printk("Dp[%d]= %08x, cmd0 %08x, cmd1 %08x\n",
  802. i++, (u32)dp, dp->dscr_cmd0, dp->dscr_cmd1);
  803. printk("src0 %08x, src1 %08x, dest0 %08x, dest1 %08x\n",
  804. dp->dscr_source0, dp->dscr_source1, dp->dscr_dest0, dp->dscr_dest1);
  805. printk("stat %08x, nxtptr %08x\n",
  806. dp->dscr_stat, dp->dscr_nxtptr);
  807. dp = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  808. } while (dp != ctp->chan_desc_base);
  809. }
  810. /* Put a descriptor into the DMA ring.
  811. * This updates the source/destination pointers and byte count.
  812. */
  813. u32
  814. au1xxx_dbdma_put_dscr(u32 chanid, au1x_ddma_desc_t *dscr )
  815. {
  816. chan_tab_t *ctp;
  817. au1x_ddma_desc_t *dp;
  818. u32 nbytes=0;
  819. /* I guess we could check this to be within the
  820. * range of the table......
  821. */
  822. ctp = *((chan_tab_t **)chanid);
  823. /* We should have multiple callers for a particular channel,
  824. * an interrupt doesn't affect this pointer nor the descriptor,
  825. * so no locking should be needed.
  826. */
  827. dp = ctp->put_ptr;
  828. /* If the descriptor is valid, we are way ahead of the DMA
  829. * engine, so just return an error condition.
  830. */
  831. if (dp->dscr_cmd0 & DSCR_CMD0_V)
  832. return 0;
  833. /* Load up buffer addresses and byte count.
  834. */
  835. dp->dscr_dest0 = dscr->dscr_dest0;
  836. dp->dscr_source0 = dscr->dscr_source0;
  837. dp->dscr_dest1 = dscr->dscr_dest1;
  838. dp->dscr_source1 = dscr->dscr_source1;
  839. dp->dscr_cmd1 = dscr->dscr_cmd1;
  840. nbytes = dscr->dscr_cmd1;
  841. /* Allow the caller to specifiy if an interrupt is generated */
  842. dp->dscr_cmd0 &= ~DSCR_CMD0_IE;
  843. dp->dscr_cmd0 |= dscr->dscr_cmd0 | DSCR_CMD0_V;
  844. ctp->chan_ptr->ddma_dbell = 0;
  845. /* Get next descriptor pointer.
  846. */
  847. ctp->put_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  848. /* return something not zero.
  849. */
  850. return nbytes;
  851. }
  852. #endif /* defined(CONFIG_SOC_AU1550) || defined(CONFIG_SOC_AU1200) */