tusb6010_omap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. int ch;
  187. s8 dmareq;
  188. s8 sync_dev;
  189. if (unlikely(dma_addr & 0x1) || (len < 32) || (len > packet_sz))
  190. return false;
  191. /*
  192. * HW issue #10: Async dma will eventually corrupt the XFR_SIZE
  193. * register which will cause missed DMA interrupt. We could try to
  194. * use a timer for the callback, but it is unsafe as the XFR_SIZE
  195. * register is corrupt, and we won't know if the DMA worked.
  196. */
  197. if (dma_addr & 0x2)
  198. return false;
  199. /*
  200. * Because of HW issue #10, it seems like mixing sync DMA and async
  201. * PIO access can confuse the DMA. Make sure XFR_SIZE is reset before
  202. * using the channel for DMA.
  203. */
  204. if (chdat->tx)
  205. dma_remaining = musb_readl(ep_conf, TUSB_EP_TX_OFFSET);
  206. else
  207. dma_remaining = musb_readl(ep_conf, TUSB_EP_RX_OFFSET);
  208. dma_remaining = TUSB_EP_CONFIG_XFR_SIZE(dma_remaining);
  209. if (dma_remaining) {
  210. dev_dbg(musb->controller, "Busy %s dma ch%i, not using: %08x\n",
  211. chdat->tx ? "tx" : "rx", chdat->ch,
  212. dma_remaining);
  213. return false;
  214. }
  215. chdat->transfer_len = len & ~0x1f;
  216. if (len < packet_sz)
  217. chdat->transfer_packet_sz = chdat->transfer_len;
  218. else
  219. chdat->transfer_packet_sz = packet_sz;
  220. if (tusb_dma->multichannel) {
  221. ch = chdat->ch;
  222. dmareq = chdat->dmareq;
  223. sync_dev = chdat->sync_dev;
  224. } else {
  225. if (tusb_omap_use_shared_dmareq(chdat) != 0) {
  226. dev_dbg(musb->controller, "could not get dma for ep%i\n", chdat->epnum);
  227. return false;
  228. }
  229. if (tusb_dma->ch < 0) {
  230. /* REVISIT: This should get blocked earlier, happens
  231. * with MSC ErrorRecoveryTest
  232. */
  233. WARN_ON(1);
  234. return false;
  235. }
  236. ch = tusb_dma->ch;
  237. dmareq = tusb_dma->dmareq;
  238. sync_dev = tusb_dma->sync_dev;
  239. omap_set_dma_callback(ch, tusb_omap_dma_cb, channel);
  240. }
  241. chdat->packet_sz = packet_sz;
  242. chdat->len = len;
  243. channel->actual_len = 0;
  244. chdat->dma_addr = dma_addr;
  245. channel->status = MUSB_DMA_STATUS_BUSY;
  246. /* Since we're recycling dma areas, we need to clean or invalidate */
  247. if (chdat->tx)
  248. dma_map_single(dev, phys_to_virt(dma_addr), len,
  249. DMA_TO_DEVICE);
  250. else
  251. dma_map_single(dev, phys_to_virt(dma_addr), len,
  252. DMA_FROM_DEVICE);
  253. /* Use 16-bit transfer if dma_addr is not 32-bit aligned */
  254. if ((dma_addr & 0x3) == 0) {
  255. dma_params.data_type = OMAP_DMA_DATA_TYPE_S32;
  256. dma_params.elem_count = 8; /* Elements in frame */
  257. } else {
  258. dma_params.data_type = OMAP_DMA_DATA_TYPE_S16;
  259. dma_params.elem_count = 16; /* Elements in frame */
  260. fifo = hw_ep->fifo_async;
  261. }
  262. dma_params.frame_count = chdat->transfer_len / 32; /* Burst sz frame */
  263. dev_dbg(musb->controller, "ep%i %s dma ch%i dma: %08x len: %u(%u) packet_sz: %i(%i)\n",
  264. chdat->epnum, chdat->tx ? "tx" : "rx",
  265. ch, dma_addr, chdat->transfer_len, len,
  266. chdat->transfer_packet_sz, packet_sz);
  267. /*
  268. * Prepare omap DMA for transfer
  269. */
  270. if (chdat->tx) {
  271. dma_params.src_amode = OMAP_DMA_AMODE_POST_INC;
  272. dma_params.src_start = (unsigned long)dma_addr;
  273. dma_params.src_ei = 0;
  274. dma_params.src_fi = 0;
  275. dma_params.dst_amode = OMAP_DMA_AMODE_DOUBLE_IDX;
  276. dma_params.dst_start = (unsigned long)fifo;
  277. dma_params.dst_ei = 1;
  278. dma_params.dst_fi = -31; /* Loop 32 byte window */
  279. dma_params.trigger = sync_dev;
  280. dma_params.sync_mode = OMAP_DMA_SYNC_FRAME;
  281. dma_params.src_or_dst_synch = 0; /* Dest sync */
  282. src_burst = OMAP_DMA_DATA_BURST_16; /* 16x32 read */
  283. dst_burst = OMAP_DMA_DATA_BURST_8; /* 8x32 write */
  284. } else {
  285. dma_params.src_amode = OMAP_DMA_AMODE_DOUBLE_IDX;
  286. dma_params.src_start = (unsigned long)fifo;
  287. dma_params.src_ei = 1;
  288. dma_params.src_fi = -31; /* Loop 32 byte window */
  289. dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC;
  290. dma_params.dst_start = (unsigned long)dma_addr;
  291. dma_params.dst_ei = 0;
  292. dma_params.dst_fi = 0;
  293. dma_params.trigger = sync_dev;
  294. dma_params.sync_mode = OMAP_DMA_SYNC_FRAME;
  295. dma_params.src_or_dst_synch = 1; /* Source sync */
  296. src_burst = OMAP_DMA_DATA_BURST_8; /* 8x32 read */
  297. dst_burst = OMAP_DMA_DATA_BURST_16; /* 16x32 write */
  298. }
  299. dev_dbg(musb->controller, "ep%i %s using %i-bit %s dma from 0x%08lx to 0x%08lx\n",
  300. chdat->epnum, chdat->tx ? "tx" : "rx",
  301. (dma_params.data_type == OMAP_DMA_DATA_TYPE_S32) ? 32 : 16,
  302. ((dma_addr & 0x3) == 0) ? "sync" : "async",
  303. dma_params.src_start, dma_params.dst_start);
  304. omap_set_dma_params(ch, &dma_params);
  305. omap_set_dma_src_burst_mode(ch, src_burst);
  306. omap_set_dma_dest_burst_mode(ch, dst_burst);
  307. omap_set_dma_write_mode(ch, OMAP_DMA_WRITE_LAST_NON_POSTED);
  308. /*
  309. * Prepare MUSB for DMA transfer
  310. */
  311. if (chdat->tx) {
  312. musb_ep_select(mbase, chdat->epnum);
  313. csr = musb_readw(hw_ep->regs, MUSB_TXCSR);
  314. csr |= (MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB
  315. | MUSB_TXCSR_DMAMODE | MUSB_TXCSR_MODE);
  316. csr &= ~MUSB_TXCSR_P_UNDERRUN;
  317. musb_writew(hw_ep->regs, MUSB_TXCSR, csr);
  318. } else {
  319. musb_ep_select(mbase, chdat->epnum);
  320. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  321. csr |= MUSB_RXCSR_DMAENAB;
  322. csr &= ~(MUSB_RXCSR_AUTOCLEAR | MUSB_RXCSR_DMAMODE);
  323. musb_writew(hw_ep->regs, MUSB_RXCSR,
  324. csr | MUSB_RXCSR_P_WZC_BITS);
  325. }
  326. /*
  327. * Start DMA transfer
  328. */
  329. omap_start_dma(ch);
  330. if (chdat->tx) {
  331. /* Send transfer_packet_sz packets at a time */
  332. musb_writel(ep_conf, TUSB_EP_MAX_PACKET_SIZE_OFFSET,
  333. chdat->transfer_packet_sz);
  334. musb_writel(ep_conf, TUSB_EP_TX_OFFSET,
  335. TUSB_EP_CONFIG_XFR_SIZE(chdat->transfer_len));
  336. } else {
  337. /* Receive transfer_packet_sz packets at a time */
  338. musb_writel(ep_conf, TUSB_EP_MAX_PACKET_SIZE_OFFSET,
  339. chdat->transfer_packet_sz << 16);
  340. musb_writel(ep_conf, TUSB_EP_RX_OFFSET,
  341. TUSB_EP_CONFIG_XFR_SIZE(chdat->transfer_len));
  342. }
  343. return true;
  344. }
  345. static int tusb_omap_dma_abort(struct dma_channel *channel)
  346. {
  347. struct tusb_omap_dma_ch *chdat = to_chdat(channel);
  348. struct tusb_omap_dma *tusb_dma = chdat->tusb_dma;
  349. if (!tusb_dma->multichannel) {
  350. if (tusb_dma->ch >= 0) {
  351. omap_stop_dma(tusb_dma->ch);
  352. omap_free_dma(tusb_dma->ch);
  353. tusb_dma->ch = -1;
  354. }
  355. tusb_dma->dmareq = -1;
  356. tusb_dma->sync_dev = -1;
  357. }
  358. channel->status = MUSB_DMA_STATUS_FREE;
  359. return 0;
  360. }
  361. static inline int tusb_omap_dma_allocate_dmareq(struct tusb_omap_dma_ch *chdat)
  362. {
  363. u32 reg = musb_readl(chdat->tbase, TUSB_DMA_EP_MAP);
  364. int i, dmareq_nr = -1;
  365. const int sync_dev[6] = {
  366. OMAP24XX_DMA_EXT_DMAREQ0,
  367. OMAP24XX_DMA_EXT_DMAREQ1,
  368. OMAP242X_DMA_EXT_DMAREQ2,
  369. OMAP242X_DMA_EXT_DMAREQ3,
  370. OMAP242X_DMA_EXT_DMAREQ4,
  371. OMAP242X_DMA_EXT_DMAREQ5,
  372. };
  373. for (i = 0; i < MAX_DMAREQ; i++) {
  374. int cur = (reg & (0xf << (i * 5))) >> (i * 5);
  375. if (cur == 0) {
  376. dmareq_nr = i;
  377. break;
  378. }
  379. }
  380. if (dmareq_nr == -1)
  381. return -EAGAIN;
  382. reg |= (chdat->epnum << (dmareq_nr * 5));
  383. if (chdat->tx)
  384. reg |= ((1 << 4) << (dmareq_nr * 5));
  385. musb_writel(chdat->tbase, TUSB_DMA_EP_MAP, reg);
  386. chdat->dmareq = dmareq_nr;
  387. chdat->sync_dev = sync_dev[chdat->dmareq];
  388. return 0;
  389. }
  390. static inline void tusb_omap_dma_free_dmareq(struct tusb_omap_dma_ch *chdat)
  391. {
  392. u32 reg;
  393. if (!chdat || chdat->dmareq < 0)
  394. return;
  395. reg = musb_readl(chdat->tbase, TUSB_DMA_EP_MAP);
  396. reg &= ~(0x1f << (chdat->dmareq * 5));
  397. musb_writel(chdat->tbase, TUSB_DMA_EP_MAP, reg);
  398. chdat->dmareq = -1;
  399. chdat->sync_dev = -1;
  400. }
  401. static struct dma_channel *dma_channel_pool[MAX_DMAREQ];
  402. static struct dma_channel *
  403. tusb_omap_dma_allocate(struct dma_controller *c,
  404. struct musb_hw_ep *hw_ep,
  405. u8 tx)
  406. {
  407. int ret, i;
  408. const char *dev_name;
  409. struct tusb_omap_dma *tusb_dma;
  410. struct musb *musb;
  411. void __iomem *tbase;
  412. struct dma_channel *channel = NULL;
  413. struct tusb_omap_dma_ch *chdat = NULL;
  414. u32 reg;
  415. tusb_dma = container_of(c, struct tusb_omap_dma, controller);
  416. musb = tusb_dma->musb;
  417. tbase = musb->ctrl_base;
  418. reg = musb_readl(tbase, TUSB_DMA_INT_MASK);
  419. if (tx)
  420. reg &= ~(1 << hw_ep->epnum);
  421. else
  422. reg &= ~(1 << (hw_ep->epnum + 15));
  423. musb_writel(tbase, TUSB_DMA_INT_MASK, reg);
  424. /* REVISIT: Why does dmareq5 not work? */
  425. if (hw_ep->epnum == 0) {
  426. dev_dbg(musb->controller, "Not allowing DMA for ep0 %s\n", tx ? "tx" : "rx");
  427. return NULL;
  428. }
  429. for (i = 0; i < MAX_DMAREQ; i++) {
  430. struct dma_channel *ch = dma_channel_pool[i];
  431. if (ch->status == MUSB_DMA_STATUS_UNKNOWN) {
  432. ch->status = MUSB_DMA_STATUS_FREE;
  433. channel = ch;
  434. chdat = ch->private_data;
  435. break;
  436. }
  437. }
  438. if (!channel)
  439. return NULL;
  440. if (tx) {
  441. chdat->tx = 1;
  442. dev_name = "TUSB transmit";
  443. } else {
  444. chdat->tx = 0;
  445. dev_name = "TUSB receive";
  446. }
  447. chdat->musb = tusb_dma->musb;
  448. chdat->tbase = tusb_dma->tbase;
  449. chdat->hw_ep = hw_ep;
  450. chdat->epnum = hw_ep->epnum;
  451. chdat->dmareq = -1;
  452. chdat->completed_len = 0;
  453. chdat->tusb_dma = tusb_dma;
  454. channel->max_len = 0x7fffffff;
  455. channel->desired_mode = 0;
  456. channel->actual_len = 0;
  457. if (tusb_dma->multichannel) {
  458. ret = tusb_omap_dma_allocate_dmareq(chdat);
  459. if (ret != 0)
  460. goto free_dmareq;
  461. ret = omap_request_dma(chdat->sync_dev, dev_name,
  462. tusb_omap_dma_cb, channel, &chdat->ch);
  463. if (ret != 0)
  464. goto free_dmareq;
  465. } else if (tusb_dma->ch == -1) {
  466. tusb_dma->dmareq = 0;
  467. tusb_dma->sync_dev = OMAP24XX_DMA_EXT_DMAREQ0;
  468. /* Callback data gets set later in the shared dmareq case */
  469. ret = omap_request_dma(tusb_dma->sync_dev, "TUSB shared",
  470. tusb_omap_dma_cb, NULL, &tusb_dma->ch);
  471. if (ret != 0)
  472. goto free_dmareq;
  473. chdat->dmareq = -1;
  474. chdat->ch = -1;
  475. }
  476. dev_dbg(musb->controller, "ep%i %s dma: %s dma%i dmareq%i sync%i\n",
  477. chdat->epnum,
  478. chdat->tx ? "tx" : "rx",
  479. chdat->ch >= 0 ? "dedicated" : "shared",
  480. chdat->ch >= 0 ? chdat->ch : tusb_dma->ch,
  481. chdat->dmareq >= 0 ? chdat->dmareq : tusb_dma->dmareq,
  482. chdat->sync_dev >= 0 ? chdat->sync_dev : tusb_dma->sync_dev);
  483. return channel;
  484. free_dmareq:
  485. tusb_omap_dma_free_dmareq(chdat);
  486. dev_dbg(musb->controller, "ep%i: Could not get a DMA channel\n", chdat->epnum);
  487. channel->status = MUSB_DMA_STATUS_UNKNOWN;
  488. return NULL;
  489. }
  490. static void tusb_omap_dma_release(struct dma_channel *channel)
  491. {
  492. struct tusb_omap_dma_ch *chdat = to_chdat(channel);
  493. struct musb *musb = chdat->musb;
  494. void __iomem *tbase = musb->ctrl_base;
  495. u32 reg;
  496. dev_dbg(musb->controller, "ep%i ch%i\n", chdat->epnum, chdat->ch);
  497. reg = musb_readl(tbase, TUSB_DMA_INT_MASK);
  498. if (chdat->tx)
  499. reg |= (1 << chdat->epnum);
  500. else
  501. reg |= (1 << (chdat->epnum + 15));
  502. musb_writel(tbase, TUSB_DMA_INT_MASK, reg);
  503. reg = musb_readl(tbase, TUSB_DMA_INT_CLEAR);
  504. if (chdat->tx)
  505. reg |= (1 << chdat->epnum);
  506. else
  507. reg |= (1 << (chdat->epnum + 15));
  508. musb_writel(tbase, TUSB_DMA_INT_CLEAR, reg);
  509. channel->status = MUSB_DMA_STATUS_UNKNOWN;
  510. if (chdat->ch >= 0) {
  511. omap_stop_dma(chdat->ch);
  512. omap_free_dma(chdat->ch);
  513. chdat->ch = -1;
  514. }
  515. if (chdat->dmareq >= 0)
  516. tusb_omap_dma_free_dmareq(chdat);
  517. channel = NULL;
  518. }
  519. void dma_controller_destroy(struct dma_controller *c)
  520. {
  521. struct tusb_omap_dma *tusb_dma;
  522. int i;
  523. tusb_dma = container_of(c, struct tusb_omap_dma, controller);
  524. for (i = 0; i < MAX_DMAREQ; i++) {
  525. struct dma_channel *ch = dma_channel_pool[i];
  526. if (ch) {
  527. kfree(ch->private_data);
  528. kfree(ch);
  529. }
  530. }
  531. if (tusb_dma && !tusb_dma->multichannel && tusb_dma->ch >= 0)
  532. omap_free_dma(tusb_dma->ch);
  533. kfree(tusb_dma);
  534. }
  535. struct dma_controller *dma_controller_create(struct musb *musb, void __iomem *base)
  536. {
  537. void __iomem *tbase = musb->ctrl_base;
  538. struct tusb_omap_dma *tusb_dma;
  539. int i;
  540. /* REVISIT: Get dmareq lines used from board-*.c */
  541. musb_writel(musb->ctrl_base, TUSB_DMA_INT_MASK, 0x7fffffff);
  542. musb_writel(musb->ctrl_base, TUSB_DMA_EP_MAP, 0);
  543. musb_writel(tbase, TUSB_DMA_REQ_CONF,
  544. TUSB_DMA_REQ_CONF_BURST_SIZE(2)
  545. | TUSB_DMA_REQ_CONF_DMA_REQ_EN(0x3f)
  546. | TUSB_DMA_REQ_CONF_DMA_REQ_ASSER(2));
  547. tusb_dma = kzalloc(sizeof(struct tusb_omap_dma), GFP_KERNEL);
  548. if (!tusb_dma)
  549. goto out;
  550. tusb_dma->musb = musb;
  551. tusb_dma->tbase = musb->ctrl_base;
  552. tusb_dma->ch = -1;
  553. tusb_dma->dmareq = -1;
  554. tusb_dma->sync_dev = -1;
  555. tusb_dma->controller.channel_alloc = tusb_omap_dma_allocate;
  556. tusb_dma->controller.channel_release = tusb_omap_dma_release;
  557. tusb_dma->controller.channel_program = tusb_omap_dma_program;
  558. tusb_dma->controller.channel_abort = tusb_omap_dma_abort;
  559. if (musb->tusb_revision >= TUSB_REV_30)
  560. tusb_dma->multichannel = 1;
  561. for (i = 0; i < MAX_DMAREQ; i++) {
  562. struct dma_channel *ch;
  563. struct tusb_omap_dma_ch *chdat;
  564. ch = kzalloc(sizeof(struct dma_channel), GFP_KERNEL);
  565. if (!ch)
  566. goto cleanup;
  567. dma_channel_pool[i] = ch;
  568. chdat = kzalloc(sizeof(struct tusb_omap_dma_ch), GFP_KERNEL);
  569. if (!chdat)
  570. goto cleanup;
  571. ch->status = MUSB_DMA_STATUS_UNKNOWN;
  572. ch->private_data = chdat;
  573. }
  574. return &tusb_dma->controller;
  575. cleanup:
  576. dma_controller_destroy(&tusb_dma->controller);
  577. out:
  578. return NULL;
  579. }