dbdma.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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 <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 volatile dbdma_global_t *dbdma_gptr = (dbdma_global_t *)DDMA_GLOBAL_BASE;
  61. static int dbdma_initialized;
  62. static void au1xxx_dbdma_init(void);
  63. typedef struct dbdma_device_table {
  64. u32 dev_id;
  65. u32 dev_flags;
  66. u32 dev_tsize;
  67. u32 dev_devwidth;
  68. u32 dev_physaddr; /* If FIFO */
  69. u32 dev_intlevel;
  70. u32 dev_intpolarity;
  71. } dbdev_tab_t;
  72. typedef struct dbdma_chan_config {
  73. u32 chan_flags;
  74. u32 chan_index;
  75. dbdev_tab_t *chan_src;
  76. dbdev_tab_t *chan_dest;
  77. au1x_dma_chan_t *chan_ptr;
  78. au1x_ddma_desc_t *chan_desc_base;
  79. au1x_ddma_desc_t *get_ptr, *put_ptr, *cur_ptr;
  80. void *chan_callparam;
  81. void (*chan_callback)(int, void *, struct pt_regs *);
  82. } chan_tab_t;
  83. #define DEV_FLAGS_INUSE (1 << 0)
  84. #define DEV_FLAGS_ANYUSE (1 << 1)
  85. #define DEV_FLAGS_OUT (1 << 2)
  86. #define DEV_FLAGS_IN (1 << 3)
  87. static dbdev_tab_t dbdev_tab[] = {
  88. #ifdef CONFIG_SOC_AU1550
  89. /* UARTS */
  90. { DSCR_CMD0_UART0_TX, DEV_FLAGS_OUT, 0, 8, 0x11100004, 0, 0 },
  91. { DSCR_CMD0_UART0_RX, DEV_FLAGS_IN, 0, 8, 0x11100000, 0, 0 },
  92. { DSCR_CMD0_UART3_TX, DEV_FLAGS_OUT, 0, 8, 0x11400004, 0, 0 },
  93. { DSCR_CMD0_UART3_RX, DEV_FLAGS_IN, 0, 8, 0x11400000, 0, 0 },
  94. /* EXT DMA */
  95. { DSCR_CMD0_DMA_REQ0, 0, 0, 0, 0x00000000, 0, 0 },
  96. { DSCR_CMD0_DMA_REQ1, 0, 0, 0, 0x00000000, 0, 0 },
  97. { DSCR_CMD0_DMA_REQ2, 0, 0, 0, 0x00000000, 0, 0 },
  98. { DSCR_CMD0_DMA_REQ3, 0, 0, 0, 0x00000000, 0, 0 },
  99. /* USB DEV */
  100. { DSCR_CMD0_USBDEV_RX0, DEV_FLAGS_IN, 4, 8, 0x10200000, 0, 0 },
  101. { DSCR_CMD0_USBDEV_TX0, DEV_FLAGS_OUT, 4, 8, 0x10200004, 0, 0 },
  102. { DSCR_CMD0_USBDEV_TX1, DEV_FLAGS_OUT, 4, 8, 0x10200008, 0, 0 },
  103. { DSCR_CMD0_USBDEV_TX2, DEV_FLAGS_OUT, 4, 8, 0x1020000c, 0, 0 },
  104. { DSCR_CMD0_USBDEV_RX3, DEV_FLAGS_IN, 4, 8, 0x10200010, 0, 0 },
  105. { DSCR_CMD0_USBDEV_RX4, DEV_FLAGS_IN, 4, 8, 0x10200014, 0, 0 },
  106. /* PSC 0 */
  107. { DSCR_CMD0_PSC0_TX, DEV_FLAGS_OUT, 0, 0, 0x11a0001c, 0, 0 },
  108. { DSCR_CMD0_PSC0_RX, DEV_FLAGS_IN, 0, 0, 0x11a0001c, 0, 0 },
  109. /* PSC 1 */
  110. { DSCR_CMD0_PSC1_TX, DEV_FLAGS_OUT, 0, 0, 0x11b0001c, 0, 0 },
  111. { DSCR_CMD0_PSC1_RX, DEV_FLAGS_IN, 0, 0, 0x11b0001c, 0, 0 },
  112. /* PSC 2 */
  113. { DSCR_CMD0_PSC2_TX, DEV_FLAGS_OUT, 0, 0, 0x10a0001c, 0, 0 },
  114. { DSCR_CMD0_PSC2_RX, DEV_FLAGS_IN, 0, 0, 0x10a0001c, 0, 0 },
  115. /* PSC 3 */
  116. { DSCR_CMD0_PSC3_TX, DEV_FLAGS_OUT, 0, 0, 0x10b0001c, 0, 0 },
  117. { DSCR_CMD0_PSC3_RX, DEV_FLAGS_IN, 0, 0, 0x10b0001c, 0, 0 },
  118. { DSCR_CMD0_PCI_WRITE, 0, 0, 0, 0x00000000, 0, 0 }, /* PCI */
  119. { DSCR_CMD0_NAND_FLASH, 0, 0, 0, 0x00000000, 0, 0 }, /* NAND */
  120. /* MAC 0 */
  121. { DSCR_CMD0_MAC0_RX, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },
  122. { DSCR_CMD0_MAC0_TX, DEV_FLAGS_OUT, 0, 0, 0x00000000, 0, 0 },
  123. /* MAC 1 */
  124. { DSCR_CMD0_MAC1_RX, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },
  125. { DSCR_CMD0_MAC1_TX, DEV_FLAGS_OUT, 0, 0, 0x00000000, 0, 0 },
  126. #endif /* CONFIG_SOC_AU1550 */
  127. #ifdef CONFIG_SOC_AU1200
  128. { DSCR_CMD0_UART0_TX, DEV_FLAGS_OUT, 0, 8, 0x11100004, 0, 0 },
  129. { DSCR_CMD0_UART0_RX, DEV_FLAGS_IN, 0, 8, 0x11100000, 0, 0 },
  130. { DSCR_CMD0_UART1_TX, DEV_FLAGS_OUT, 0, 8, 0x11200004, 0, 0 },
  131. { DSCR_CMD0_UART1_RX, DEV_FLAGS_IN, 0, 8, 0x11200000, 0, 0 },
  132. { DSCR_CMD0_DMA_REQ0, 0, 0, 0, 0x00000000, 0, 0 },
  133. { DSCR_CMD0_DMA_REQ1, 0, 0, 0, 0x00000000, 0, 0 },
  134. { DSCR_CMD0_MAE_BE, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  135. { DSCR_CMD0_MAE_FE, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  136. { DSCR_CMD0_MAE_BOTH, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  137. { DSCR_CMD0_LCD, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  138. { DSCR_CMD0_SDMS_TX0, DEV_FLAGS_OUT, 0, 0, 0x00000000, 0, 0 },
  139. { DSCR_CMD0_SDMS_RX0, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },
  140. { DSCR_CMD0_SDMS_TX1, DEV_FLAGS_OUT, 0, 0, 0x00000000, 0, 0 },
  141. { DSCR_CMD0_SDMS_RX1, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },
  142. { DSCR_CMD0_AES_TX, DEV_FLAGS_OUT, 0, 0, 0x00000000, 0, 0 },
  143. { DSCR_CMD0_AES_RX, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },
  144. { DSCR_CMD0_PSC0_TX, DEV_FLAGS_OUT, 0, 0, 0x11a0001c, 0, 0 },
  145. { DSCR_CMD0_PSC0_RX, DEV_FLAGS_IN, 0, 0, 0x11a0001c, 0, 0 },
  146. { DSCR_CMD0_PSC0_SYNC, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  147. { DSCR_CMD0_PSC1_TX, DEV_FLAGS_OUT, 0, 0, 0x11b0001c, 0, 0 },
  148. { DSCR_CMD0_PSC1_RX, DEV_FLAGS_IN, 0, 0, 0x11b0001c, 0, 0 },
  149. { DSCR_CMD0_PSC1_SYNC, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  150. { DSCR_CMD0_CIM_RXA, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },
  151. { DSCR_CMD0_CIM_RXB, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },
  152. { DSCR_CMD0_CIM_RXC, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },
  153. { DSCR_CMD0_CIM_SYNC, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  154. { DSCR_CMD0_NAND_FLASH, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },
  155. #endif // CONFIG_SOC_AU1200
  156. { DSCR_CMD0_THROTTLE, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  157. { DSCR_CMD0_ALWAYS, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },
  158. };
  159. #define DBDEV_TAB_SIZE (sizeof(dbdev_tab) / sizeof(dbdev_tab_t))
  160. static chan_tab_t *chan_tab_ptr[NUM_DBDMA_CHANS];
  161. static dbdev_tab_t *
  162. find_dbdev_id (u32 id)
  163. {
  164. int i;
  165. dbdev_tab_t *p;
  166. for (i = 0; i < DBDEV_TAB_SIZE; ++i) {
  167. p = &dbdev_tab[i];
  168. if (p->dev_id == id)
  169. return p;
  170. }
  171. return NULL;
  172. }
  173. /* Allocate a channel and return a non-zero descriptor if successful.
  174. */
  175. u32
  176. au1xxx_dbdma_chan_alloc(u32 srcid, u32 destid,
  177. void (*callback)(int, void *, struct pt_regs *), void *callparam)
  178. {
  179. unsigned long flags;
  180. u32 used, chan, rv;
  181. u32 dcp;
  182. int i;
  183. dbdev_tab_t *stp, *dtp;
  184. chan_tab_t *ctp;
  185. volatile au1x_dma_chan_t *cp;
  186. /* We do the intialization on the first channel allocation.
  187. * We have to wait because of the interrupt handler initialization
  188. * which can't be done successfully during board set up.
  189. */
  190. if (!dbdma_initialized)
  191. au1xxx_dbdma_init();
  192. dbdma_initialized = 1;
  193. if ((srcid > DSCR_NDEV_IDS) || (destid > DSCR_NDEV_IDS))
  194. return 0;
  195. if ((stp = find_dbdev_id(srcid)) == NULL) return 0;
  196. if ((dtp = find_dbdev_id(destid)) == NULL) return 0;
  197. used = 0;
  198. rv = 0;
  199. /* Check to see if we can get both channels.
  200. */
  201. spin_lock_irqsave(&au1xxx_dbdma_spin_lock, flags);
  202. if (!(stp->dev_flags & DEV_FLAGS_INUSE) ||
  203. (stp->dev_flags & DEV_FLAGS_ANYUSE)) {
  204. /* Got source */
  205. stp->dev_flags |= DEV_FLAGS_INUSE;
  206. if (!(dtp->dev_flags & DEV_FLAGS_INUSE) ||
  207. (dtp->dev_flags & DEV_FLAGS_ANYUSE)) {
  208. /* Got destination */
  209. dtp->dev_flags |= DEV_FLAGS_INUSE;
  210. }
  211. else {
  212. /* Can't get dest. Release src.
  213. */
  214. stp->dev_flags &= ~DEV_FLAGS_INUSE;
  215. used++;
  216. }
  217. }
  218. else {
  219. used++;
  220. }
  221. spin_unlock_irqrestore(&au1xxx_dbdma_spin_lock, flags);
  222. if (!used) {
  223. /* Let's see if we can allocate a channel for it.
  224. */
  225. ctp = NULL;
  226. chan = 0;
  227. spin_lock_irqsave(&au1xxx_dbdma_spin_lock, flags);
  228. for (i=0; i<NUM_DBDMA_CHANS; i++) {
  229. if (chan_tab_ptr[i] == NULL) {
  230. /* If kmalloc fails, it is caught below same
  231. * as a channel not available.
  232. */
  233. ctp = kmalloc(sizeof(chan_tab_t), GFP_KERNEL);
  234. chan_tab_ptr[i] = ctp;
  235. ctp->chan_index = chan = i;
  236. break;
  237. }
  238. }
  239. spin_unlock_irqrestore(&au1xxx_dbdma_spin_lock, flags);
  240. if (ctp != NULL) {
  241. memset(ctp, 0, sizeof(chan_tab_t));
  242. dcp = DDMA_CHANNEL_BASE;
  243. dcp += (0x0100 * chan);
  244. ctp->chan_ptr = (au1x_dma_chan_t *)dcp;
  245. cp = (volatile au1x_dma_chan_t *)dcp;
  246. ctp->chan_src = stp;
  247. ctp->chan_dest = dtp;
  248. ctp->chan_callback = callback;
  249. ctp->chan_callparam = callparam;
  250. /* Initialize channel configuration.
  251. */
  252. i = 0;
  253. if (stp->dev_intlevel)
  254. i |= DDMA_CFG_SED;
  255. if (stp->dev_intpolarity)
  256. i |= DDMA_CFG_SP;
  257. if (dtp->dev_intlevel)
  258. i |= DDMA_CFG_DED;
  259. if (dtp->dev_intpolarity)
  260. i |= DDMA_CFG_DP;
  261. cp->ddma_cfg = i;
  262. au_sync();
  263. /* Return a non-zero value that can be used to
  264. * find the channel information in subsequent
  265. * operations.
  266. */
  267. rv = (u32)(&chan_tab_ptr[chan]);
  268. }
  269. else {
  270. /* Release devices.
  271. */
  272. stp->dev_flags &= ~DEV_FLAGS_INUSE;
  273. dtp->dev_flags &= ~DEV_FLAGS_INUSE;
  274. }
  275. }
  276. return rv;
  277. }
  278. /* Set the device width if source or destination is a FIFO.
  279. * Should be 8, 16, or 32 bits.
  280. */
  281. u32
  282. au1xxx_dbdma_set_devwidth(u32 chanid, int bits)
  283. {
  284. u32 rv;
  285. chan_tab_t *ctp;
  286. dbdev_tab_t *stp, *dtp;
  287. ctp = *((chan_tab_t **)chanid);
  288. stp = ctp->chan_src;
  289. dtp = ctp->chan_dest;
  290. rv = 0;
  291. if (stp->dev_flags & DEV_FLAGS_IN) { /* Source in fifo */
  292. rv = stp->dev_devwidth;
  293. stp->dev_devwidth = bits;
  294. }
  295. if (dtp->dev_flags & DEV_FLAGS_OUT) { /* Destination out fifo */
  296. rv = dtp->dev_devwidth;
  297. dtp->dev_devwidth = bits;
  298. }
  299. return rv;
  300. }
  301. /* Allocate a descriptor ring, initializing as much as possible.
  302. */
  303. u32
  304. au1xxx_dbdma_ring_alloc(u32 chanid, int entries)
  305. {
  306. int i;
  307. u32 desc_base, srcid, destid;
  308. u32 cmd0, cmd1, src1, dest1;
  309. u32 src0, dest0;
  310. chan_tab_t *ctp;
  311. dbdev_tab_t *stp, *dtp;
  312. au1x_ddma_desc_t *dp;
  313. /* I guess we could check this to be within the
  314. * range of the table......
  315. */
  316. ctp = *((chan_tab_t **)chanid);
  317. stp = ctp->chan_src;
  318. dtp = ctp->chan_dest;
  319. /* The descriptors must be 32-byte aligned. There is a
  320. * possibility the allocation will give us such an address,
  321. * and if we try that first we are likely to not waste larger
  322. * slabs of memory.
  323. */
  324. desc_base = (u32)kmalloc(entries * sizeof(au1x_ddma_desc_t), GFP_KERNEL);
  325. if (desc_base == 0)
  326. return 0;
  327. if (desc_base & 0x1f) {
  328. /* Lost....do it again, allocate extra, and round
  329. * the address base.
  330. */
  331. kfree((const void *)desc_base);
  332. i = entries * sizeof(au1x_ddma_desc_t);
  333. i += (sizeof(au1x_ddma_desc_t) - 1);
  334. if ((desc_base = (u32)kmalloc(i, GFP_KERNEL)) == 0)
  335. return 0;
  336. desc_base = ALIGN_ADDR(desc_base, sizeof(au1x_ddma_desc_t));
  337. }
  338. dp = (au1x_ddma_desc_t *)desc_base;
  339. /* Keep track of the base descriptor.
  340. */
  341. ctp->chan_desc_base = dp;
  342. /* Initialize the rings with as much information as we know.
  343. */
  344. srcid = stp->dev_id;
  345. destid = dtp->dev_id;
  346. cmd0 = cmd1 = src1 = dest1 = 0;
  347. src0 = dest0 = 0;
  348. cmd0 |= DSCR_CMD0_SID(srcid);
  349. cmd0 |= DSCR_CMD0_DID(destid);
  350. cmd0 |= DSCR_CMD0_IE | DSCR_CMD0_CV;
  351. cmd0 |= DSCR_CMD0_ST(DSCR_CMD0_ST_CURRENT);
  352. switch (stp->dev_devwidth) {
  353. case 8:
  354. cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_BYTE);
  355. break;
  356. case 16:
  357. cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_HALFWORD);
  358. break;
  359. case 32:
  360. default:
  361. cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_WORD);
  362. break;
  363. }
  364. switch (dtp->dev_devwidth) {
  365. case 8:
  366. cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_BYTE);
  367. break;
  368. case 16:
  369. cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_HALFWORD);
  370. break;
  371. case 32:
  372. default:
  373. cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_WORD);
  374. break;
  375. }
  376. /* If the device is marked as an in/out FIFO, ensure it is
  377. * set non-coherent.
  378. */
  379. if (stp->dev_flags & DEV_FLAGS_IN)
  380. cmd0 |= DSCR_CMD0_SN; /* Source in fifo */
  381. if (dtp->dev_flags & DEV_FLAGS_OUT)
  382. cmd0 |= DSCR_CMD0_DN; /* Destination out fifo */
  383. /* Set up source1. For now, assume no stride and increment.
  384. * A channel attribute update can change this later.
  385. */
  386. switch (stp->dev_tsize) {
  387. case 1:
  388. src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE1);
  389. break;
  390. case 2:
  391. src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE2);
  392. break;
  393. case 4:
  394. src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE4);
  395. break;
  396. case 8:
  397. default:
  398. src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE8);
  399. break;
  400. }
  401. /* If source input is fifo, set static address.
  402. */
  403. if (stp->dev_flags & DEV_FLAGS_IN) {
  404. src0 = stp->dev_physaddr;
  405. src1 |= DSCR_SRC1_SAM(DSCR_xAM_STATIC);
  406. }
  407. /* Set up dest1. For now, assume no stride and increment.
  408. * A channel attribute update can change this later.
  409. */
  410. switch (dtp->dev_tsize) {
  411. case 1:
  412. dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE1);
  413. break;
  414. case 2:
  415. dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE2);
  416. break;
  417. case 4:
  418. dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE4);
  419. break;
  420. case 8:
  421. default:
  422. dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE8);
  423. break;
  424. }
  425. /* If destination output is fifo, set static address.
  426. */
  427. if (dtp->dev_flags & DEV_FLAGS_OUT) {
  428. dest0 = dtp->dev_physaddr;
  429. dest1 |= DSCR_DEST1_DAM(DSCR_xAM_STATIC);
  430. }
  431. for (i=0; i<entries; i++) {
  432. dp->dscr_cmd0 = cmd0;
  433. dp->dscr_cmd1 = cmd1;
  434. dp->dscr_source0 = src0;
  435. dp->dscr_source1 = src1;
  436. dp->dscr_dest0 = dest0;
  437. dp->dscr_dest1 = dest1;
  438. dp->dscr_stat = 0;
  439. dp->dscr_nxtptr = DSCR_NXTPTR(virt_to_phys(dp + 1));
  440. dp++;
  441. }
  442. /* Make last descrptor point to the first.
  443. */
  444. dp--;
  445. dp->dscr_nxtptr = DSCR_NXTPTR(virt_to_phys(ctp->chan_desc_base));
  446. ctp->get_ptr = ctp->put_ptr = ctp->cur_ptr = ctp->chan_desc_base;
  447. return (u32)(ctp->chan_desc_base);
  448. }
  449. /* Put a source buffer into the DMA ring.
  450. * This updates the source pointer and byte count. Normally used
  451. * for memory to fifo transfers.
  452. */
  453. u32
  454. au1xxx_dbdma_put_source(u32 chanid, void *buf, int nbytes)
  455. {
  456. chan_tab_t *ctp;
  457. au1x_ddma_desc_t *dp;
  458. /* I guess we could check this to be within the
  459. * range of the table......
  460. */
  461. ctp = *((chan_tab_t **)chanid);
  462. /* We should have multiple callers for a particular channel,
  463. * an interrupt doesn't affect this pointer nor the descriptor,
  464. * so no locking should be needed.
  465. */
  466. dp = ctp->put_ptr;
  467. /* If the descriptor is valid, we are way ahead of the DMA
  468. * engine, so just return an error condition.
  469. */
  470. if (dp->dscr_cmd0 & DSCR_CMD0_V) {
  471. return 0;
  472. }
  473. /* Load up buffer address and byte count.
  474. */
  475. dp->dscr_source0 = virt_to_phys(buf);
  476. dp->dscr_cmd1 = nbytes;
  477. dp->dscr_cmd0 |= DSCR_CMD0_V; /* Let it rip */
  478. ctp->chan_ptr->ddma_dbell = 0xffffffff; /* Make it go */
  479. /* Get next descriptor pointer.
  480. */
  481. ctp->put_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  482. /* return something not zero.
  483. */
  484. return nbytes;
  485. }
  486. /* Put a destination buffer into the DMA ring.
  487. * This updates the destination pointer and byte count. Normally used
  488. * to place an empty buffer into the ring for fifo to memory transfers.
  489. */
  490. u32
  491. au1xxx_dbdma_put_dest(u32 chanid, void *buf, int nbytes)
  492. {
  493. chan_tab_t *ctp;
  494. au1x_ddma_desc_t *dp;
  495. /* I guess we could check this to be within the
  496. * range of the table......
  497. */
  498. ctp = *((chan_tab_t **)chanid);
  499. /* We should have multiple callers for a particular channel,
  500. * an interrupt doesn't affect this pointer nor the descriptor,
  501. * so no locking should be needed.
  502. */
  503. dp = ctp->put_ptr;
  504. /* If the descriptor is valid, we are way ahead of the DMA
  505. * engine, so just return an error condition.
  506. */
  507. if (dp->dscr_cmd0 & DSCR_CMD0_V)
  508. return 0;
  509. /* Load up buffer address and byte count.
  510. */
  511. dp->dscr_dest0 = virt_to_phys(buf);
  512. dp->dscr_cmd1 = nbytes;
  513. dp->dscr_cmd0 |= DSCR_CMD0_V; /* Let it rip */
  514. /* Get next descriptor pointer.
  515. */
  516. ctp->put_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  517. /* return something not zero.
  518. */
  519. return nbytes;
  520. }
  521. /* Get a destination buffer into the DMA ring.
  522. * Normally used to get a full buffer from the ring during fifo
  523. * to memory transfers. This does not set the valid bit, you will
  524. * have to put another destination buffer to keep the DMA going.
  525. */
  526. u32
  527. au1xxx_dbdma_get_dest(u32 chanid, void **buf, int *nbytes)
  528. {
  529. chan_tab_t *ctp;
  530. au1x_ddma_desc_t *dp;
  531. u32 rv;
  532. /* I guess we could check this to be within the
  533. * range of the table......
  534. */
  535. ctp = *((chan_tab_t **)chanid);
  536. /* We should have multiple callers for a particular channel,
  537. * an interrupt doesn't affect this pointer nor the descriptor,
  538. * so no locking should be needed.
  539. */
  540. dp = ctp->get_ptr;
  541. /* If the descriptor is valid, we are way ahead of the DMA
  542. * engine, so just return an error condition.
  543. */
  544. if (dp->dscr_cmd0 & DSCR_CMD0_V)
  545. return 0;
  546. /* Return buffer address and byte count.
  547. */
  548. *buf = (void *)(phys_to_virt(dp->dscr_dest0));
  549. *nbytes = dp->dscr_cmd1;
  550. rv = dp->dscr_stat;
  551. /* Get next descriptor pointer.
  552. */
  553. ctp->get_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  554. /* return something not zero.
  555. */
  556. return rv;
  557. }
  558. void
  559. au1xxx_dbdma_stop(u32 chanid)
  560. {
  561. chan_tab_t *ctp;
  562. volatile au1x_dma_chan_t *cp;
  563. int halt_timeout = 0;
  564. ctp = *((chan_tab_t **)chanid);
  565. cp = ctp->chan_ptr;
  566. cp->ddma_cfg &= ~DDMA_CFG_EN; /* Disable channel */
  567. au_sync();
  568. while (!(cp->ddma_stat & DDMA_STAT_H)) {
  569. udelay(1);
  570. halt_timeout++;
  571. if (halt_timeout > 100) {
  572. printk("warning: DMA channel won't halt\n");
  573. break;
  574. }
  575. }
  576. /* clear current desc valid and doorbell */
  577. cp->ddma_stat |= (DDMA_STAT_DB | DDMA_STAT_V);
  578. au_sync();
  579. }
  580. /* Start using the current descriptor pointer. If the dbdma encounters
  581. * a not valid descriptor, it will stop. In this case, we can just
  582. * continue by adding a buffer to the list and starting again.
  583. */
  584. void
  585. au1xxx_dbdma_start(u32 chanid)
  586. {
  587. chan_tab_t *ctp;
  588. volatile au1x_dma_chan_t *cp;
  589. ctp = *((chan_tab_t **)chanid);
  590. cp = ctp->chan_ptr;
  591. cp->ddma_desptr = virt_to_phys(ctp->cur_ptr);
  592. cp->ddma_cfg |= DDMA_CFG_EN; /* Enable channel */
  593. au_sync();
  594. cp->ddma_dbell = 0xffffffff; /* Make it go */
  595. au_sync();
  596. }
  597. void
  598. au1xxx_dbdma_reset(u32 chanid)
  599. {
  600. chan_tab_t *ctp;
  601. au1x_ddma_desc_t *dp;
  602. au1xxx_dbdma_stop(chanid);
  603. ctp = *((chan_tab_t **)chanid);
  604. ctp->get_ptr = ctp->put_ptr = ctp->cur_ptr = ctp->chan_desc_base;
  605. /* Run through the descriptors and reset the valid indicator.
  606. */
  607. dp = ctp->chan_desc_base;
  608. do {
  609. dp->dscr_cmd0 &= ~DSCR_CMD0_V;
  610. dp = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  611. } while (dp != ctp->chan_desc_base);
  612. }
  613. u32
  614. au1xxx_get_dma_residue(u32 chanid)
  615. {
  616. chan_tab_t *ctp;
  617. volatile au1x_dma_chan_t *cp;
  618. u32 rv;
  619. ctp = *((chan_tab_t **)chanid);
  620. cp = ctp->chan_ptr;
  621. /* This is only valid if the channel is stopped.
  622. */
  623. rv = cp->ddma_bytecnt;
  624. au_sync();
  625. return rv;
  626. }
  627. void
  628. au1xxx_dbdma_chan_free(u32 chanid)
  629. {
  630. chan_tab_t *ctp;
  631. dbdev_tab_t *stp, *dtp;
  632. ctp = *((chan_tab_t **)chanid);
  633. stp = ctp->chan_src;
  634. dtp = ctp->chan_dest;
  635. au1xxx_dbdma_stop(chanid);
  636. if (ctp->chan_desc_base != NULL)
  637. kfree(ctp->chan_desc_base);
  638. stp->dev_flags &= ~DEV_FLAGS_INUSE;
  639. dtp->dev_flags &= ~DEV_FLAGS_INUSE;
  640. chan_tab_ptr[ctp->chan_index] = NULL;
  641. kfree(ctp);
  642. }
  643. static irqreturn_t
  644. dbdma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  645. {
  646. u32 intstat;
  647. u32 chan_index;
  648. chan_tab_t *ctp;
  649. au1x_ddma_desc_t *dp;
  650. volatile au1x_dma_chan_t *cp;
  651. intstat = dbdma_gptr->ddma_intstat;
  652. au_sync();
  653. chan_index = au_ffs(intstat) - 1;
  654. ctp = chan_tab_ptr[chan_index];
  655. cp = ctp->chan_ptr;
  656. dp = ctp->cur_ptr;
  657. /* Reset interrupt.
  658. */
  659. cp->ddma_irq = 0;
  660. au_sync();
  661. if (ctp->chan_callback)
  662. (ctp->chan_callback)(irq, ctp->chan_callparam, regs);
  663. ctp->cur_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  664. return IRQ_HANDLED;
  665. }
  666. static void
  667. au1xxx_dbdma_init(void)
  668. {
  669. dbdma_gptr->ddma_config = 0;
  670. dbdma_gptr->ddma_throttle = 0;
  671. dbdma_gptr->ddma_inten = 0xffff;
  672. au_sync();
  673. if (request_irq(AU1550_DDMA_INT, dbdma_interrupt, SA_INTERRUPT,
  674. "Au1xxx dbdma", (void *)dbdma_gptr))
  675. printk("Can't get 1550 dbdma irq");
  676. }
  677. void
  678. au1xxx_dbdma_dump(u32 chanid)
  679. {
  680. chan_tab_t *ctp;
  681. au1x_ddma_desc_t *dp;
  682. dbdev_tab_t *stp, *dtp;
  683. volatile au1x_dma_chan_t *cp;
  684. ctp = *((chan_tab_t **)chanid);
  685. stp = ctp->chan_src;
  686. dtp = ctp->chan_dest;
  687. cp = ctp->chan_ptr;
  688. printk("Chan %x, stp %x (dev %d) dtp %x (dev %d) \n",
  689. (u32)ctp, (u32)stp, stp - dbdev_tab, (u32)dtp, dtp - dbdev_tab);
  690. printk("desc base %x, get %x, put %x, cur %x\n",
  691. (u32)(ctp->chan_desc_base), (u32)(ctp->get_ptr),
  692. (u32)(ctp->put_ptr), (u32)(ctp->cur_ptr));
  693. printk("dbdma chan %x\n", (u32)cp);
  694. printk("cfg %08x, desptr %08x, statptr %08x\n",
  695. cp->ddma_cfg, cp->ddma_desptr, cp->ddma_statptr);
  696. printk("dbell %08x, irq %08x, stat %08x, bytecnt %08x\n",
  697. cp->ddma_dbell, cp->ddma_irq, cp->ddma_stat, cp->ddma_bytecnt);
  698. /* Run through the descriptors
  699. */
  700. dp = ctp->chan_desc_base;
  701. do {
  702. printk("dp %08x, cmd0 %08x, cmd1 %08x\n",
  703. (u32)dp, dp->dscr_cmd0, dp->dscr_cmd1);
  704. printk("src0 %08x, src1 %08x, dest0 %08x\n",
  705. dp->dscr_source0, dp->dscr_source1, dp->dscr_dest0);
  706. printk("dest1 %08x, stat %08x, nxtptr %08x\n",
  707. dp->dscr_dest1, dp->dscr_stat, dp->dscr_nxtptr);
  708. dp = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
  709. } while (dp != ctp->chan_desc_base);
  710. }
  711. #endif /* defined(CONFIG_SOC_AU1550) || defined(CONFIG_SOC_AU1200) */