tusb6010_omap.c 19 KB

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