tusb6010_omap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * TUSB6010 USB 2.0 OTG Dual Role controller OMAP DMA interface
  3. *
  4. * Copyright (C) 2006 Nokia Corporation
  5. * Tony Lindgren <tony@atomide.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/usb.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/slab.h>
  18. #include <linux/omap-dma.h>
  19. #include "musb_core.h"
  20. #include "tusb6010.h"
  21. #define to_chdat(c) ((struct tusb_omap_dma_ch *)(c)->private_data)
  22. #define MAX_DMAREQ 5 /* REVISIT: Really 6, but req5 not OK */
  23. #define OMAP24XX_DMA_EXT_DMAREQ0 2
  24. #define OMAP24XX_DMA_EXT_DMAREQ1 3
  25. #define OMAP242X_DMA_EXT_DMAREQ2 14
  26. #define OMAP242X_DMA_EXT_DMAREQ3 15
  27. #define OMAP242X_DMA_EXT_DMAREQ4 16
  28. #define OMAP242X_DMA_EXT_DMAREQ5 64
  29. struct tusb_omap_dma_ch {
  30. struct musb *musb;
  31. void __iomem *tbase;
  32. unsigned long phys_offset;
  33. int epnum;
  34. u8 tx;
  35. struct musb_hw_ep *hw_ep;
  36. int ch;
  37. s8 dmareq;
  38. s8 sync_dev;
  39. struct tusb_omap_dma *tusb_dma;
  40. dma_addr_t dma_addr;
  41. u32 len;
  42. u16 packet_sz;
  43. u16 transfer_packet_sz;
  44. u32 transfer_len;
  45. u32 completed_len;
  46. };
  47. struct tusb_omap_dma {
  48. struct dma_controller controller;
  49. struct musb *musb;
  50. void __iomem *tbase;
  51. int ch;
  52. s8 dmareq;
  53. s8 sync_dev;
  54. unsigned multichannel:1;
  55. };
  56. /*
  57. * Allocate dmareq0 to the current channel unless it's already taken
  58. */
  59. static inline int tusb_omap_use_shared_dmareq(struct tusb_omap_dma_ch *chdat)
  60. {
  61. u32 reg = musb_readl(chdat->tbase, TUSB_DMA_EP_MAP);
  62. if (reg != 0) {
  63. dev_dbg(chdat->musb->controller, "ep%i dmareq0 is busy for ep%i\n",
  64. chdat->epnum, reg & 0xf);
  65. return -EAGAIN;
  66. }
  67. if (chdat->tx)
  68. reg = (1 << 4) | chdat->epnum;
  69. else
  70. reg = chdat->epnum;
  71. musb_writel(chdat->tbase, TUSB_DMA_EP_MAP, reg);
  72. return 0;
  73. }
  74. static inline void tusb_omap_free_shared_dmareq(struct tusb_omap_dma_ch *chdat)
  75. {
  76. u32 reg = musb_readl(chdat->tbase, TUSB_DMA_EP_MAP);
  77. if ((reg & 0xf) != chdat->epnum) {
  78. printk(KERN_ERR "ep%i trying to release dmareq0 for ep%i\n",
  79. chdat->epnum, reg & 0xf);
  80. return;
  81. }
  82. musb_writel(chdat->tbase, TUSB_DMA_EP_MAP, 0);
  83. }
  84. /*
  85. * See also musb_dma_completion in plat_uds.c and musb_g_[tx|rx]() in
  86. * musb_gadget.c.
  87. */
  88. static void tusb_omap_dma_cb(int lch, u16 ch_status, void *data)
  89. {
  90. struct dma_channel *channel = (struct dma_channel *)data;
  91. struct tusb_omap_dma_ch *chdat = to_chdat(channel);
  92. struct tusb_omap_dma *tusb_dma = chdat->tusb_dma;
  93. struct musb *musb = chdat->musb;
  94. struct device *dev = musb->controller;
  95. struct musb_hw_ep *hw_ep = chdat->hw_ep;
  96. void __iomem *ep_conf = hw_ep->conf;
  97. void __iomem *mbase = musb->mregs;
  98. unsigned long remaining, flags, pio;
  99. int ch;
  100. spin_lock_irqsave(&musb->lock, flags);
  101. if (tusb_dma->multichannel)
  102. ch = chdat->ch;
  103. else
  104. ch = tusb_dma->ch;
  105. if (ch_status != OMAP_DMA_BLOCK_IRQ)
  106. printk(KERN_ERR "TUSB DMA error status: %i\n", ch_status);
  107. dev_dbg(musb->controller, "ep%i %s dma callback ch: %i status: %x\n",
  108. chdat->epnum, chdat->tx ? "tx" : "rx",
  109. ch, ch_status);
  110. if (chdat->tx)
  111. remaining = musb_readl(ep_conf, TUSB_EP_TX_OFFSET);
  112. else
  113. remaining = musb_readl(ep_conf, TUSB_EP_RX_OFFSET);
  114. remaining = TUSB_EP_CONFIG_XFR_SIZE(remaining);
  115. /* HW issue #10: XFR_SIZE may get corrupt on DMA (both async & sync) */
  116. if (unlikely(remaining > chdat->transfer_len)) {
  117. dev_dbg(musb->controller, "Corrupt %s dma ch%i XFR_SIZE: 0x%08lx\n",
  118. chdat->tx ? "tx" : "rx", chdat->ch,
  119. remaining);
  120. remaining = 0;
  121. }
  122. channel->actual_len = chdat->transfer_len - remaining;
  123. pio = chdat->len - channel->actual_len;
  124. dev_dbg(musb->controller, "DMA remaining %lu/%u\n", remaining, chdat->transfer_len);
  125. /* Transfer remaining 1 - 31 bytes */
  126. if (pio > 0 && pio < 32) {
  127. u8 *buf;
  128. dev_dbg(musb->controller, "Using PIO for remaining %lu bytes\n", pio);
  129. buf = phys_to_virt((u32)chdat->dma_addr) + chdat->transfer_len;
  130. if (chdat->tx) {
  131. dma_unmap_single(dev, chdat->dma_addr,
  132. chdat->transfer_len,
  133. DMA_TO_DEVICE);
  134. musb_write_fifo(hw_ep, pio, buf);
  135. } else {
  136. dma_unmap_single(dev, chdat->dma_addr,
  137. chdat->transfer_len,
  138. DMA_FROM_DEVICE);
  139. musb_read_fifo(hw_ep, pio, buf);
  140. }
  141. channel->actual_len += pio;
  142. }
  143. if (!tusb_dma->multichannel)
  144. tusb_omap_free_shared_dmareq(chdat);
  145. channel->status = MUSB_DMA_STATUS_FREE;
  146. /* Handle only RX callbacks here. TX callbacks must be handled based
  147. * on the TUSB DMA status interrupt.
  148. * REVISIT: Use both TUSB DMA status interrupt and OMAP DMA callback
  149. * interrupt for RX and TX.
  150. */
  151. if (!chdat->tx)
  152. musb_dma_completion(musb, chdat->epnum, chdat->tx);
  153. /* We must terminate short tx transfers manually by setting TXPKTRDY.
  154. * REVISIT: This same problem may occur with other MUSB dma as well.
  155. * Easy to test with g_ether by pinging the MUSB board with ping -s54.
  156. */
  157. if ((chdat->transfer_len < chdat->packet_sz)
  158. || (chdat->transfer_len % chdat->packet_sz != 0)) {
  159. u16 csr;
  160. if (chdat->tx) {
  161. dev_dbg(musb->controller, "terminating short tx packet\n");
  162. musb_ep_select(mbase, chdat->epnum);
  163. csr = musb_readw(hw_ep->regs, MUSB_TXCSR);
  164. csr |= MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY
  165. | MUSB_TXCSR_P_WZC_BITS;
  166. musb_writew(hw_ep->regs, MUSB_TXCSR, csr);
  167. }
  168. }
  169. spin_unlock_irqrestore(&musb->lock, flags);
  170. }
  171. static int tusb_omap_dma_program(struct dma_channel *channel, u16 packet_sz,
  172. u8 rndis_mode, dma_addr_t dma_addr, u32 len)
  173. {
  174. struct tusb_omap_dma_ch *chdat = to_chdat(channel);
  175. struct tusb_omap_dma *tusb_dma = chdat->tusb_dma;
  176. struct musb *musb = chdat->musb;
  177. struct device *dev = musb->controller;
  178. struct musb_hw_ep *hw_ep = chdat->hw_ep;
  179. void __iomem *mbase = musb->mregs;
  180. void __iomem *ep_conf = hw_ep->conf;
  181. dma_addr_t fifo = hw_ep->fifo_sync;
  182. struct omap_dma_channel_params dma_params;
  183. u32 dma_remaining;
  184. int src_burst, dst_burst;
  185. u16 csr;
  186. u32 psize;
  187. int ch;
  188. s8 dmareq;
  189. s8 sync_dev;
  190. if (unlikely(dma_addr & 0x1) || (len < 32) || (len > packet_sz))
  191. return false;
  192. /*
  193. * HW issue #10: Async dma will eventually corrupt the XFR_SIZE
  194. * register which will cause missed DMA interrupt. We could try to
  195. * use a timer for the callback, but it is unsafe as the XFR_SIZE
  196. * register is corrupt, and we won't know if the DMA worked.
  197. */
  198. if (dma_addr & 0x2)
  199. return false;
  200. /*
  201. * Because of HW issue #10, it seems like mixing sync DMA and async
  202. * PIO access can confuse the DMA. Make sure XFR_SIZE is reset before
  203. * using the channel for DMA.
  204. */
  205. if (chdat->tx)
  206. dma_remaining = musb_readl(ep_conf, TUSB_EP_TX_OFFSET);
  207. else
  208. dma_remaining = musb_readl(ep_conf, TUSB_EP_RX_OFFSET);
  209. dma_remaining = TUSB_EP_CONFIG_XFR_SIZE(dma_remaining);
  210. if (dma_remaining) {
  211. dev_dbg(musb->controller, "Busy %s dma ch%i, not using: %08x\n",
  212. chdat->tx ? "tx" : "rx", chdat->ch,
  213. dma_remaining);
  214. return false;
  215. }
  216. chdat->transfer_len = len & ~0x1f;
  217. if (len < packet_sz)
  218. chdat->transfer_packet_sz = chdat->transfer_len;
  219. else
  220. chdat->transfer_packet_sz = packet_sz;
  221. if (tusb_dma->multichannel) {
  222. ch = chdat->ch;
  223. dmareq = chdat->dmareq;
  224. sync_dev = chdat->sync_dev;
  225. } else {
  226. if (tusb_omap_use_shared_dmareq(chdat) != 0) {
  227. dev_dbg(musb->controller, "could not get dma for ep%i\n", chdat->epnum);
  228. return false;
  229. }
  230. if (tusb_dma->ch < 0) {
  231. /* REVISIT: This should get blocked earlier, happens
  232. * with MSC ErrorRecoveryTest
  233. */
  234. WARN_ON(1);
  235. return false;
  236. }
  237. ch = tusb_dma->ch;
  238. dmareq = tusb_dma->dmareq;
  239. sync_dev = tusb_dma->sync_dev;
  240. omap_set_dma_callback(ch, tusb_omap_dma_cb, channel);
  241. }
  242. chdat->packet_sz = packet_sz;
  243. chdat->len = len;
  244. channel->actual_len = 0;
  245. chdat->dma_addr = dma_addr;
  246. channel->status = MUSB_DMA_STATUS_BUSY;
  247. /* Since we're recycling dma areas, we need to clean or invalidate */
  248. if (chdat->tx)
  249. dma_map_single(dev, phys_to_virt(dma_addr), len,
  250. DMA_TO_DEVICE);
  251. else
  252. dma_map_single(dev, phys_to_virt(dma_addr), len,
  253. DMA_FROM_DEVICE);
  254. /* Use 16-bit transfer if dma_addr is not 32-bit aligned */
  255. if ((dma_addr & 0x3) == 0) {
  256. dma_params.data_type = OMAP_DMA_DATA_TYPE_S32;
  257. dma_params.elem_count = 8; /* Elements in frame */
  258. } else {
  259. dma_params.data_type = OMAP_DMA_DATA_TYPE_S16;
  260. dma_params.elem_count = 16; /* Elements in frame */
  261. fifo = hw_ep->fifo_async;
  262. }
  263. dma_params.frame_count = chdat->transfer_len / 32; /* Burst sz frame */
  264. dev_dbg(musb->controller, "ep%i %s dma ch%i dma: %pad len: %u(%u) packet_sz: %i(%i)\n",
  265. chdat->epnum, chdat->tx ? "tx" : "rx",
  266. ch, &dma_addr, chdat->transfer_len, len,
  267. chdat->transfer_packet_sz, packet_sz);
  268. /*
  269. * Prepare omap DMA for transfer
  270. */
  271. if (chdat->tx) {
  272. dma_params.src_amode = OMAP_DMA_AMODE_POST_INC;
  273. dma_params.src_start = (unsigned long)dma_addr;
  274. dma_params.src_ei = 0;
  275. dma_params.src_fi = 0;
  276. dma_params.dst_amode = OMAP_DMA_AMODE_DOUBLE_IDX;
  277. dma_params.dst_start = (unsigned long)fifo;
  278. dma_params.dst_ei = 1;
  279. dma_params.dst_fi = -31; /* Loop 32 byte window */
  280. dma_params.trigger = sync_dev;
  281. dma_params.sync_mode = OMAP_DMA_SYNC_FRAME;
  282. dma_params.src_or_dst_synch = 0; /* Dest sync */
  283. src_burst = OMAP_DMA_DATA_BURST_16; /* 16x32 read */
  284. dst_burst = OMAP_DMA_DATA_BURST_8; /* 8x32 write */
  285. } else {
  286. dma_params.src_amode = OMAP_DMA_AMODE_DOUBLE_IDX;
  287. dma_params.src_start = (unsigned long)fifo;
  288. dma_params.src_ei = 1;
  289. dma_params.src_fi = -31; /* Loop 32 byte window */
  290. dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC;
  291. dma_params.dst_start = (unsigned long)dma_addr;
  292. dma_params.dst_ei = 0;
  293. dma_params.dst_fi = 0;
  294. dma_params.trigger = sync_dev;
  295. dma_params.sync_mode = OMAP_DMA_SYNC_FRAME;
  296. dma_params.src_or_dst_synch = 1; /* Source sync */
  297. src_burst = OMAP_DMA_DATA_BURST_8; /* 8x32 read */
  298. dst_burst = OMAP_DMA_DATA_BURST_16; /* 16x32 write */
  299. }
  300. dev_dbg(musb->controller, "ep%i %s using %i-bit %s dma from 0x%08lx to 0x%08lx\n",
  301. chdat->epnum, chdat->tx ? "tx" : "rx",
  302. (dma_params.data_type == OMAP_DMA_DATA_TYPE_S32) ? 32 : 16,
  303. ((dma_addr & 0x3) == 0) ? "sync" : "async",
  304. dma_params.src_start, dma_params.dst_start);
  305. omap_set_dma_params(ch, &dma_params);
  306. omap_set_dma_src_burst_mode(ch, src_burst);
  307. omap_set_dma_dest_burst_mode(ch, dst_burst);
  308. omap_set_dma_write_mode(ch, OMAP_DMA_WRITE_LAST_NON_POSTED);
  309. /*
  310. * Prepare MUSB for DMA transfer
  311. */
  312. if (chdat->tx) {
  313. musb_ep_select(mbase, chdat->epnum);
  314. csr = musb_readw(hw_ep->regs, MUSB_TXCSR);
  315. csr |= (MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB
  316. | MUSB_TXCSR_DMAMODE | MUSB_TXCSR_MODE);
  317. csr &= ~MUSB_TXCSR_P_UNDERRUN;
  318. musb_writew(hw_ep->regs, MUSB_TXCSR, csr);
  319. } else {
  320. musb_ep_select(mbase, chdat->epnum);
  321. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  322. csr |= MUSB_RXCSR_DMAENAB;
  323. csr &= ~(MUSB_RXCSR_AUTOCLEAR | MUSB_RXCSR_DMAMODE);
  324. musb_writew(hw_ep->regs, MUSB_RXCSR,
  325. csr | MUSB_RXCSR_P_WZC_BITS);
  326. }
  327. /*
  328. * Start DMA transfer
  329. */
  330. omap_start_dma(ch);
  331. if (chdat->tx) {
  332. /* Send transfer_packet_sz packets at a time */
  333. psize = musb_readl(ep_conf, TUSB_EP_MAX_PACKET_SIZE_OFFSET);
  334. psize &= ~0x7ff;
  335. psize |= chdat->transfer_packet_sz;
  336. musb_writel(ep_conf, TUSB_EP_MAX_PACKET_SIZE_OFFSET, psize);
  337. musb_writel(ep_conf, TUSB_EP_TX_OFFSET,
  338. TUSB_EP_CONFIG_XFR_SIZE(chdat->transfer_len));
  339. } else {
  340. /* Receive transfer_packet_sz packets at a time */
  341. psize = musb_readl(ep_conf, TUSB_EP_MAX_PACKET_SIZE_OFFSET);
  342. psize &= ~(0x7ff << 16);
  343. psize |= (chdat->transfer_packet_sz << 16);
  344. musb_writel(ep_conf, TUSB_EP_MAX_PACKET_SIZE_OFFSET, psize);
  345. musb_writel(ep_conf, TUSB_EP_RX_OFFSET,
  346. TUSB_EP_CONFIG_XFR_SIZE(chdat->transfer_len));
  347. }
  348. return true;
  349. }
  350. static int tusb_omap_dma_abort(struct dma_channel *channel)
  351. {
  352. struct tusb_omap_dma_ch *chdat = to_chdat(channel);
  353. struct tusb_omap_dma *tusb_dma = chdat->tusb_dma;
  354. if (!tusb_dma->multichannel) {
  355. if (tusb_dma->ch >= 0) {
  356. omap_stop_dma(tusb_dma->ch);
  357. omap_free_dma(tusb_dma->ch);
  358. tusb_dma->ch = -1;
  359. }
  360. tusb_dma->dmareq = -1;
  361. tusb_dma->sync_dev = -1;
  362. }
  363. channel->status = MUSB_DMA_STATUS_FREE;
  364. return 0;
  365. }
  366. static inline int tusb_omap_dma_allocate_dmareq(struct tusb_omap_dma_ch *chdat)
  367. {
  368. u32 reg = musb_readl(chdat->tbase, TUSB_DMA_EP_MAP);
  369. int i, dmareq_nr = -1;
  370. const int sync_dev[6] = {
  371. OMAP24XX_DMA_EXT_DMAREQ0,
  372. OMAP24XX_DMA_EXT_DMAREQ1,
  373. OMAP242X_DMA_EXT_DMAREQ2,
  374. OMAP242X_DMA_EXT_DMAREQ3,
  375. OMAP242X_DMA_EXT_DMAREQ4,
  376. OMAP242X_DMA_EXT_DMAREQ5,
  377. };
  378. for (i = 0; i < MAX_DMAREQ; i++) {
  379. int cur = (reg & (0xf << (i * 5))) >> (i * 5);
  380. if (cur == 0) {
  381. dmareq_nr = i;
  382. break;
  383. }
  384. }
  385. if (dmareq_nr == -1)
  386. return -EAGAIN;
  387. reg |= (chdat->epnum << (dmareq_nr * 5));
  388. if (chdat->tx)
  389. reg |= ((1 << 4) << (dmareq_nr * 5));
  390. musb_writel(chdat->tbase, TUSB_DMA_EP_MAP, reg);
  391. chdat->dmareq = dmareq_nr;
  392. chdat->sync_dev = sync_dev[chdat->dmareq];
  393. return 0;
  394. }
  395. static inline void tusb_omap_dma_free_dmareq(struct tusb_omap_dma_ch *chdat)
  396. {
  397. u32 reg;
  398. if (!chdat || chdat->dmareq < 0)
  399. return;
  400. reg = musb_readl(chdat->tbase, TUSB_DMA_EP_MAP);
  401. reg &= ~(0x1f << (chdat->dmareq * 5));
  402. musb_writel(chdat->tbase, TUSB_DMA_EP_MAP, reg);
  403. chdat->dmareq = -1;
  404. chdat->sync_dev = -1;
  405. }
  406. static struct dma_channel *dma_channel_pool[MAX_DMAREQ];
  407. static struct dma_channel *
  408. tusb_omap_dma_allocate(struct dma_controller *c,
  409. struct musb_hw_ep *hw_ep,
  410. u8 tx)
  411. {
  412. int ret, i;
  413. const char *dev_name;
  414. struct tusb_omap_dma *tusb_dma;
  415. struct musb *musb;
  416. void __iomem *tbase;
  417. struct dma_channel *channel = NULL;
  418. struct tusb_omap_dma_ch *chdat = NULL;
  419. u32 reg;
  420. tusb_dma = container_of(c, struct tusb_omap_dma, controller);
  421. musb = tusb_dma->musb;
  422. tbase = musb->ctrl_base;
  423. reg = musb_readl(tbase, TUSB_DMA_INT_MASK);
  424. if (tx)
  425. reg &= ~(1 << hw_ep->epnum);
  426. else
  427. reg &= ~(1 << (hw_ep->epnum + 15));
  428. musb_writel(tbase, TUSB_DMA_INT_MASK, reg);
  429. /* REVISIT: Why does dmareq5 not work? */
  430. if (hw_ep->epnum == 0) {
  431. dev_dbg(musb->controller, "Not allowing DMA for ep0 %s\n", tx ? "tx" : "rx");
  432. return NULL;
  433. }
  434. for (i = 0; i < MAX_DMAREQ; i++) {
  435. struct dma_channel *ch = dma_channel_pool[i];
  436. if (ch->status == MUSB_DMA_STATUS_UNKNOWN) {
  437. ch->status = MUSB_DMA_STATUS_FREE;
  438. channel = ch;
  439. chdat = ch->private_data;
  440. break;
  441. }
  442. }
  443. if (!channel)
  444. return NULL;
  445. if (tx) {
  446. chdat->tx = 1;
  447. dev_name = "TUSB transmit";
  448. } else {
  449. chdat->tx = 0;
  450. dev_name = "TUSB receive";
  451. }
  452. chdat->musb = tusb_dma->musb;
  453. chdat->tbase = tusb_dma->tbase;
  454. chdat->hw_ep = hw_ep;
  455. chdat->epnum = hw_ep->epnum;
  456. chdat->dmareq = -1;
  457. chdat->completed_len = 0;
  458. chdat->tusb_dma = tusb_dma;
  459. channel->max_len = 0x7fffffff;
  460. channel->desired_mode = 0;
  461. channel->actual_len = 0;
  462. if (tusb_dma->multichannel) {
  463. ret = tusb_omap_dma_allocate_dmareq(chdat);
  464. if (ret != 0)
  465. goto free_dmareq;
  466. ret = omap_request_dma(chdat->sync_dev, dev_name,
  467. tusb_omap_dma_cb, channel, &chdat->ch);
  468. if (ret != 0)
  469. goto free_dmareq;
  470. } else if (tusb_dma->ch == -1) {
  471. tusb_dma->dmareq = 0;
  472. tusb_dma->sync_dev = OMAP24XX_DMA_EXT_DMAREQ0;
  473. /* Callback data gets set later in the shared dmareq case */
  474. ret = omap_request_dma(tusb_dma->sync_dev, "TUSB shared",
  475. tusb_omap_dma_cb, NULL, &tusb_dma->ch);
  476. if (ret != 0)
  477. goto free_dmareq;
  478. chdat->dmareq = -1;
  479. chdat->ch = -1;
  480. }
  481. dev_dbg(musb->controller, "ep%i %s dma: %s dma%i dmareq%i sync%i\n",
  482. chdat->epnum,
  483. chdat->tx ? "tx" : "rx",
  484. chdat->ch >= 0 ? "dedicated" : "shared",
  485. chdat->ch >= 0 ? chdat->ch : tusb_dma->ch,
  486. chdat->dmareq >= 0 ? chdat->dmareq : tusb_dma->dmareq,
  487. chdat->sync_dev >= 0 ? chdat->sync_dev : tusb_dma->sync_dev);
  488. return channel;
  489. free_dmareq:
  490. tusb_omap_dma_free_dmareq(chdat);
  491. dev_dbg(musb->controller, "ep%i: Could not get a DMA channel\n", chdat->epnum);
  492. channel->status = MUSB_DMA_STATUS_UNKNOWN;
  493. return NULL;
  494. }
  495. static void tusb_omap_dma_release(struct dma_channel *channel)
  496. {
  497. struct tusb_omap_dma_ch *chdat = to_chdat(channel);
  498. struct musb *musb = chdat->musb;
  499. void __iomem *tbase = musb->ctrl_base;
  500. u32 reg;
  501. dev_dbg(musb->controller, "ep%i ch%i\n", chdat->epnum, chdat->ch);
  502. reg = musb_readl(tbase, TUSB_DMA_INT_MASK);
  503. if (chdat->tx)
  504. reg |= (1 << chdat->epnum);
  505. else
  506. reg |= (1 << (chdat->epnum + 15));
  507. musb_writel(tbase, TUSB_DMA_INT_MASK, reg);
  508. reg = musb_readl(tbase, TUSB_DMA_INT_CLEAR);
  509. if (chdat->tx)
  510. reg |= (1 << chdat->epnum);
  511. else
  512. reg |= (1 << (chdat->epnum + 15));
  513. musb_writel(tbase, TUSB_DMA_INT_CLEAR, reg);
  514. channel->status = MUSB_DMA_STATUS_UNKNOWN;
  515. if (chdat->ch >= 0) {
  516. omap_stop_dma(chdat->ch);
  517. omap_free_dma(chdat->ch);
  518. chdat->ch = -1;
  519. }
  520. if (chdat->dmareq >= 0)
  521. tusb_omap_dma_free_dmareq(chdat);
  522. channel = NULL;
  523. }
  524. void tusb_dma_controller_destroy(struct dma_controller *c)
  525. {
  526. struct tusb_omap_dma *tusb_dma;
  527. int i;
  528. tusb_dma = container_of(c, struct tusb_omap_dma, controller);
  529. for (i = 0; i < MAX_DMAREQ; i++) {
  530. struct dma_channel *ch = dma_channel_pool[i];
  531. if (ch) {
  532. kfree(ch->private_data);
  533. kfree(ch);
  534. }
  535. }
  536. if (tusb_dma && !tusb_dma->multichannel && tusb_dma->ch >= 0)
  537. omap_free_dma(tusb_dma->ch);
  538. kfree(tusb_dma);
  539. }
  540. EXPORT_SYMBOL_GPL(tusb_dma_controller_destroy);
  541. struct dma_controller *
  542. tusb_dma_controller_create(struct musb *musb, void __iomem *base)
  543. {
  544. void __iomem *tbase = musb->ctrl_base;
  545. struct tusb_omap_dma *tusb_dma;
  546. int i;
  547. /* REVISIT: Get dmareq lines used from board-*.c */
  548. musb_writel(musb->ctrl_base, TUSB_DMA_INT_MASK, 0x7fffffff);
  549. musb_writel(musb->ctrl_base, TUSB_DMA_EP_MAP, 0);
  550. musb_writel(tbase, TUSB_DMA_REQ_CONF,
  551. TUSB_DMA_REQ_CONF_BURST_SIZE(2)
  552. | TUSB_DMA_REQ_CONF_DMA_REQ_EN(0x3f)
  553. | TUSB_DMA_REQ_CONF_DMA_REQ_ASSER(2));
  554. tusb_dma = kzalloc(sizeof(struct tusb_omap_dma), GFP_KERNEL);
  555. if (!tusb_dma)
  556. goto out;
  557. tusb_dma->musb = musb;
  558. tusb_dma->tbase = musb->ctrl_base;
  559. tusb_dma->ch = -1;
  560. tusb_dma->dmareq = -1;
  561. tusb_dma->sync_dev = -1;
  562. tusb_dma->controller.channel_alloc = tusb_omap_dma_allocate;
  563. tusb_dma->controller.channel_release = tusb_omap_dma_release;
  564. tusb_dma->controller.channel_program = tusb_omap_dma_program;
  565. tusb_dma->controller.channel_abort = tusb_omap_dma_abort;
  566. if (musb->tusb_revision >= TUSB_REV_30)
  567. tusb_dma->multichannel = 1;
  568. for (i = 0; i < MAX_DMAREQ; i++) {
  569. struct dma_channel *ch;
  570. struct tusb_omap_dma_ch *chdat;
  571. ch = kzalloc(sizeof(struct dma_channel), GFP_KERNEL);
  572. if (!ch)
  573. goto cleanup;
  574. dma_channel_pool[i] = ch;
  575. chdat = kzalloc(sizeof(struct tusb_omap_dma_ch), GFP_KERNEL);
  576. if (!chdat)
  577. goto cleanup;
  578. ch->status = MUSB_DMA_STATUS_UNKNOWN;
  579. ch->private_data = chdat;
  580. }
  581. return &tusb_dma->controller;
  582. cleanup:
  583. musb_dma_controller_destroy(&tusb_dma->controller);
  584. out:
  585. return NULL;
  586. }
  587. EXPORT_SYMBOL_GPL(tusb_dma_controller_create);