tusb6010_omap.c 18 KB

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