tusb6010_omap.c 19 KB

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