tusb6010_omap.c 18 KB

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