tusb6010_omap.c 19 KB

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