dbdma.c 26 KB

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