musb_cppi41.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. #include <linux/device.h>
  2. #include <linux/dma-mapping.h>
  3. #include <linux/dmaengine.h>
  4. #include <linux/sizes.h>
  5. #include <linux/platform_device.h>
  6. #include <linux/of.h>
  7. #include "cppi_dma.h"
  8. #include "musb_core.h"
  9. #include "musb_trace.h"
  10. #define RNDIS_REG(x) (0x80 + ((x - 1) * 4))
  11. #define EP_MODE_AUTOREQ_NONE 0
  12. #define EP_MODE_AUTOREQ_ALL_NEOP 1
  13. #define EP_MODE_AUTOREQ_ALWAYS 3
  14. #define EP_MODE_DMA_TRANSPARENT 0
  15. #define EP_MODE_DMA_RNDIS 1
  16. #define EP_MODE_DMA_GEN_RNDIS 3
  17. #define USB_CTRL_TX_MODE 0x70
  18. #define USB_CTRL_RX_MODE 0x74
  19. #define USB_CTRL_AUTOREQ 0xd0
  20. #define USB_TDOWN 0xd8
  21. #define MUSB_DMA_NUM_CHANNELS 15
  22. struct cppi41_dma_controller {
  23. struct dma_controller controller;
  24. struct cppi41_dma_channel rx_channel[MUSB_DMA_NUM_CHANNELS];
  25. struct cppi41_dma_channel tx_channel[MUSB_DMA_NUM_CHANNELS];
  26. struct musb *musb;
  27. struct hrtimer early_tx;
  28. struct list_head early_tx_list;
  29. u32 rx_mode;
  30. u32 tx_mode;
  31. u32 auto_req;
  32. };
  33. static void save_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
  34. {
  35. u16 csr;
  36. u8 toggle;
  37. if (cppi41_channel->is_tx)
  38. return;
  39. if (!is_host_active(cppi41_channel->controller->musb))
  40. return;
  41. csr = musb_readw(cppi41_channel->hw_ep->regs, MUSB_RXCSR);
  42. toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
  43. cppi41_channel->usb_toggle = toggle;
  44. }
  45. static void update_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
  46. {
  47. struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
  48. struct musb *musb = hw_ep->musb;
  49. u16 csr;
  50. u8 toggle;
  51. if (cppi41_channel->is_tx)
  52. return;
  53. if (!is_host_active(musb))
  54. return;
  55. musb_ep_select(musb->mregs, hw_ep->epnum);
  56. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  57. toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
  58. /*
  59. * AM335x Advisory 1.0.13: Due to internal synchronisation error the
  60. * data toggle may reset from DATA1 to DATA0 during receiving data from
  61. * more than one endpoint.
  62. */
  63. if (!toggle && toggle == cppi41_channel->usb_toggle) {
  64. csr |= MUSB_RXCSR_H_DATATOGGLE | MUSB_RXCSR_H_WR_DATATOGGLE;
  65. musb_writew(cppi41_channel->hw_ep->regs, MUSB_RXCSR, csr);
  66. musb_dbg(cppi41_channel->controller->musb,
  67. "Restoring DATA1 toggle.");
  68. }
  69. cppi41_channel->usb_toggle = toggle;
  70. }
  71. static bool musb_is_tx_fifo_empty(struct musb_hw_ep *hw_ep)
  72. {
  73. u8 epnum = hw_ep->epnum;
  74. struct musb *musb = hw_ep->musb;
  75. void __iomem *epio = musb->endpoints[epnum].regs;
  76. u16 csr;
  77. musb_ep_select(musb->mregs, hw_ep->epnum);
  78. csr = musb_readw(epio, MUSB_TXCSR);
  79. if (csr & MUSB_TXCSR_TXPKTRDY)
  80. return false;
  81. return true;
  82. }
  83. static void cppi41_dma_callback(void *private_data);
  84. static void cppi41_trans_done(struct cppi41_dma_channel *cppi41_channel)
  85. {
  86. struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
  87. struct musb *musb = hw_ep->musb;
  88. void __iomem *epio = hw_ep->regs;
  89. u16 csr;
  90. if (!cppi41_channel->prog_len ||
  91. (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)) {
  92. /* done, complete */
  93. cppi41_channel->channel.actual_len =
  94. cppi41_channel->transferred;
  95. cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
  96. cppi41_channel->channel.rx_packet_done = true;
  97. /*
  98. * transmit ZLP using PIO mode for transfers which size is
  99. * multiple of EP packet size.
  100. */
  101. if (cppi41_channel->tx_zlp && (cppi41_channel->transferred %
  102. cppi41_channel->packet_sz) == 0) {
  103. musb_ep_select(musb->mregs, hw_ep->epnum);
  104. csr = MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY;
  105. musb_writew(epio, MUSB_TXCSR, csr);
  106. }
  107. trace_musb_cppi41_done(cppi41_channel);
  108. musb_dma_completion(musb, hw_ep->epnum, cppi41_channel->is_tx);
  109. } else {
  110. /* next iteration, reload */
  111. struct dma_chan *dc = cppi41_channel->dc;
  112. struct dma_async_tx_descriptor *dma_desc;
  113. enum dma_transfer_direction direction;
  114. u32 remain_bytes;
  115. cppi41_channel->buf_addr += cppi41_channel->packet_sz;
  116. remain_bytes = cppi41_channel->total_len;
  117. remain_bytes -= cppi41_channel->transferred;
  118. remain_bytes = min(remain_bytes, cppi41_channel->packet_sz);
  119. cppi41_channel->prog_len = remain_bytes;
  120. direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV
  121. : DMA_DEV_TO_MEM;
  122. dma_desc = dmaengine_prep_slave_single(dc,
  123. cppi41_channel->buf_addr,
  124. remain_bytes,
  125. direction,
  126. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  127. if (WARN_ON(!dma_desc))
  128. return;
  129. dma_desc->callback = cppi41_dma_callback;
  130. dma_desc->callback_param = &cppi41_channel->channel;
  131. cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
  132. trace_musb_cppi41_cont(cppi41_channel);
  133. dma_async_issue_pending(dc);
  134. if (!cppi41_channel->is_tx) {
  135. musb_ep_select(musb->mregs, hw_ep->epnum);
  136. csr = musb_readw(epio, MUSB_RXCSR);
  137. csr |= MUSB_RXCSR_H_REQPKT;
  138. musb_writew(epio, MUSB_RXCSR, csr);
  139. }
  140. }
  141. }
  142. static enum hrtimer_restart cppi41_recheck_tx_req(struct hrtimer *timer)
  143. {
  144. struct cppi41_dma_controller *controller;
  145. struct cppi41_dma_channel *cppi41_channel, *n;
  146. struct musb *musb;
  147. unsigned long flags;
  148. enum hrtimer_restart ret = HRTIMER_NORESTART;
  149. controller = container_of(timer, struct cppi41_dma_controller,
  150. early_tx);
  151. musb = controller->musb;
  152. spin_lock_irqsave(&musb->lock, flags);
  153. list_for_each_entry_safe(cppi41_channel, n, &controller->early_tx_list,
  154. tx_check) {
  155. bool empty;
  156. struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
  157. empty = musb_is_tx_fifo_empty(hw_ep);
  158. if (empty) {
  159. list_del_init(&cppi41_channel->tx_check);
  160. cppi41_trans_done(cppi41_channel);
  161. }
  162. }
  163. if (!list_empty(&controller->early_tx_list) &&
  164. !hrtimer_is_queued(&controller->early_tx)) {
  165. ret = HRTIMER_RESTART;
  166. hrtimer_forward_now(&controller->early_tx,
  167. ktime_set(0, 20 * NSEC_PER_USEC));
  168. }
  169. spin_unlock_irqrestore(&musb->lock, flags);
  170. return ret;
  171. }
  172. static void cppi41_dma_callback(void *private_data)
  173. {
  174. struct dma_channel *channel = private_data;
  175. struct cppi41_dma_channel *cppi41_channel = channel->private_data;
  176. struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
  177. struct cppi41_dma_controller *controller;
  178. struct musb *musb = hw_ep->musb;
  179. unsigned long flags;
  180. struct dma_tx_state txstate;
  181. u32 transferred;
  182. int is_hs = 0;
  183. bool empty;
  184. spin_lock_irqsave(&musb->lock, flags);
  185. dmaengine_tx_status(cppi41_channel->dc, cppi41_channel->cookie,
  186. &txstate);
  187. transferred = cppi41_channel->prog_len - txstate.residue;
  188. cppi41_channel->transferred += transferred;
  189. trace_musb_cppi41_gb(cppi41_channel);
  190. update_rx_toggle(cppi41_channel);
  191. if (cppi41_channel->transferred == cppi41_channel->total_len ||
  192. transferred < cppi41_channel->packet_sz)
  193. cppi41_channel->prog_len = 0;
  194. if (cppi41_channel->is_tx) {
  195. u8 type;
  196. if (is_host_active(musb))
  197. type = hw_ep->out_qh->type;
  198. else
  199. type = hw_ep->ep_in.type;
  200. if (type == USB_ENDPOINT_XFER_ISOC)
  201. /*
  202. * Don't use the early-TX-interrupt workaround below
  203. * for Isoch transfter. Since Isoch are periodic
  204. * transfer, by the time the next transfer is
  205. * scheduled, the current one should be done already.
  206. *
  207. * This avoids audio playback underrun issue.
  208. */
  209. empty = true;
  210. else
  211. empty = musb_is_tx_fifo_empty(hw_ep);
  212. }
  213. if (!cppi41_channel->is_tx || empty) {
  214. cppi41_trans_done(cppi41_channel);
  215. goto out;
  216. }
  217. /*
  218. * On AM335x it has been observed that the TX interrupt fires
  219. * too early that means the TXFIFO is not yet empty but the DMA
  220. * engine says that it is done with the transfer. We don't
  221. * receive a FIFO empty interrupt so the only thing we can do is
  222. * to poll for the bit. On HS it usually takes 2us, on FS around
  223. * 110us - 150us depending on the transfer size.
  224. * We spin on HS (no longer than than 25us and setup a timer on
  225. * FS to check for the bit and complete the transfer.
  226. */
  227. controller = cppi41_channel->controller;
  228. if (is_host_active(musb)) {
  229. if (musb->port1_status & USB_PORT_STAT_HIGH_SPEED)
  230. is_hs = 1;
  231. } else {
  232. if (musb->g.speed == USB_SPEED_HIGH)
  233. is_hs = 1;
  234. }
  235. if (is_hs) {
  236. unsigned wait = 25;
  237. do {
  238. empty = musb_is_tx_fifo_empty(hw_ep);
  239. if (empty) {
  240. cppi41_trans_done(cppi41_channel);
  241. goto out;
  242. }
  243. wait--;
  244. if (!wait)
  245. break;
  246. cpu_relax();
  247. } while (1);
  248. }
  249. list_add_tail(&cppi41_channel->tx_check,
  250. &controller->early_tx_list);
  251. if (!hrtimer_is_queued(&controller->early_tx)) {
  252. unsigned long usecs = cppi41_channel->total_len / 10;
  253. hrtimer_start_range_ns(&controller->early_tx,
  254. ktime_set(0, usecs * NSEC_PER_USEC),
  255. 20 * NSEC_PER_USEC,
  256. HRTIMER_MODE_REL);
  257. }
  258. out:
  259. spin_unlock_irqrestore(&musb->lock, flags);
  260. }
  261. static u32 update_ep_mode(unsigned ep, unsigned mode, u32 old)
  262. {
  263. unsigned shift;
  264. shift = (ep - 1) * 2;
  265. old &= ~(3 << shift);
  266. old |= mode << shift;
  267. return old;
  268. }
  269. static void cppi41_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
  270. unsigned mode)
  271. {
  272. struct cppi41_dma_controller *controller = cppi41_channel->controller;
  273. u32 port;
  274. u32 new_mode;
  275. u32 old_mode;
  276. if (cppi41_channel->is_tx)
  277. old_mode = controller->tx_mode;
  278. else
  279. old_mode = controller->rx_mode;
  280. port = cppi41_channel->port_num;
  281. new_mode = update_ep_mode(port, mode, old_mode);
  282. if (new_mode == old_mode)
  283. return;
  284. if (cppi41_channel->is_tx) {
  285. controller->tx_mode = new_mode;
  286. musb_writel(controller->musb->ctrl_base, USB_CTRL_TX_MODE,
  287. new_mode);
  288. } else {
  289. controller->rx_mode = new_mode;
  290. musb_writel(controller->musb->ctrl_base, USB_CTRL_RX_MODE,
  291. new_mode);
  292. }
  293. }
  294. static void cppi41_set_autoreq_mode(struct cppi41_dma_channel *cppi41_channel,
  295. unsigned mode)
  296. {
  297. struct cppi41_dma_controller *controller = cppi41_channel->controller;
  298. u32 port;
  299. u32 new_mode;
  300. u32 old_mode;
  301. old_mode = controller->auto_req;
  302. port = cppi41_channel->port_num;
  303. new_mode = update_ep_mode(port, mode, old_mode);
  304. if (new_mode == old_mode)
  305. return;
  306. controller->auto_req = new_mode;
  307. musb_writel(controller->musb->ctrl_base, USB_CTRL_AUTOREQ, new_mode);
  308. }
  309. static bool cppi41_configure_channel(struct dma_channel *channel,
  310. u16 packet_sz, u8 mode,
  311. dma_addr_t dma_addr, u32 len)
  312. {
  313. struct cppi41_dma_channel *cppi41_channel = channel->private_data;
  314. struct dma_chan *dc = cppi41_channel->dc;
  315. struct dma_async_tx_descriptor *dma_desc;
  316. enum dma_transfer_direction direction;
  317. struct musb *musb = cppi41_channel->controller->musb;
  318. unsigned use_gen_rndis = 0;
  319. cppi41_channel->buf_addr = dma_addr;
  320. cppi41_channel->total_len = len;
  321. cppi41_channel->transferred = 0;
  322. cppi41_channel->packet_sz = packet_sz;
  323. cppi41_channel->tx_zlp = (cppi41_channel->is_tx && mode) ? 1 : 0;
  324. /*
  325. * Due to AM335x' Advisory 1.0.13 we are not allowed to transfer more
  326. * than max packet size at a time.
  327. */
  328. if (cppi41_channel->is_tx)
  329. use_gen_rndis = 1;
  330. if (use_gen_rndis) {
  331. /* RNDIS mode */
  332. if (len > packet_sz) {
  333. musb_writel(musb->ctrl_base,
  334. RNDIS_REG(cppi41_channel->port_num), len);
  335. /* gen rndis */
  336. cppi41_set_dma_mode(cppi41_channel,
  337. EP_MODE_DMA_GEN_RNDIS);
  338. /* auto req */
  339. cppi41_set_autoreq_mode(cppi41_channel,
  340. EP_MODE_AUTOREQ_ALL_NEOP);
  341. } else {
  342. musb_writel(musb->ctrl_base,
  343. RNDIS_REG(cppi41_channel->port_num), 0);
  344. cppi41_set_dma_mode(cppi41_channel,
  345. EP_MODE_DMA_TRANSPARENT);
  346. cppi41_set_autoreq_mode(cppi41_channel,
  347. EP_MODE_AUTOREQ_NONE);
  348. }
  349. } else {
  350. /* fallback mode */
  351. cppi41_set_dma_mode(cppi41_channel, EP_MODE_DMA_TRANSPARENT);
  352. cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
  353. len = min_t(u32, packet_sz, len);
  354. }
  355. cppi41_channel->prog_len = len;
  356. direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
  357. dma_desc = dmaengine_prep_slave_single(dc, dma_addr, len, direction,
  358. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  359. if (!dma_desc)
  360. return false;
  361. dma_desc->callback = cppi41_dma_callback;
  362. dma_desc->callback_param = channel;
  363. cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
  364. cppi41_channel->channel.rx_packet_done = false;
  365. trace_musb_cppi41_config(cppi41_channel);
  366. save_rx_toggle(cppi41_channel);
  367. dma_async_issue_pending(dc);
  368. return true;
  369. }
  370. static struct dma_channel *cppi41_dma_channel_allocate(struct dma_controller *c,
  371. struct musb_hw_ep *hw_ep, u8 is_tx)
  372. {
  373. struct cppi41_dma_controller *controller = container_of(c,
  374. struct cppi41_dma_controller, controller);
  375. struct cppi41_dma_channel *cppi41_channel = NULL;
  376. u8 ch_num = hw_ep->epnum - 1;
  377. if (ch_num >= MUSB_DMA_NUM_CHANNELS)
  378. return NULL;
  379. if (is_tx)
  380. cppi41_channel = &controller->tx_channel[ch_num];
  381. else
  382. cppi41_channel = &controller->rx_channel[ch_num];
  383. if (!cppi41_channel->dc)
  384. return NULL;
  385. if (cppi41_channel->is_allocated)
  386. return NULL;
  387. cppi41_channel->hw_ep = hw_ep;
  388. cppi41_channel->is_allocated = 1;
  389. trace_musb_cppi41_alloc(cppi41_channel);
  390. return &cppi41_channel->channel;
  391. }
  392. static void cppi41_dma_channel_release(struct dma_channel *channel)
  393. {
  394. struct cppi41_dma_channel *cppi41_channel = channel->private_data;
  395. trace_musb_cppi41_free(cppi41_channel);
  396. if (cppi41_channel->is_allocated) {
  397. cppi41_channel->is_allocated = 0;
  398. channel->status = MUSB_DMA_STATUS_FREE;
  399. channel->actual_len = 0;
  400. }
  401. }
  402. static int cppi41_dma_channel_program(struct dma_channel *channel,
  403. u16 packet_sz, u8 mode,
  404. dma_addr_t dma_addr, u32 len)
  405. {
  406. int ret;
  407. struct cppi41_dma_channel *cppi41_channel = channel->private_data;
  408. int hb_mult = 0;
  409. BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
  410. channel->status == MUSB_DMA_STATUS_BUSY);
  411. if (is_host_active(cppi41_channel->controller->musb)) {
  412. if (cppi41_channel->is_tx)
  413. hb_mult = cppi41_channel->hw_ep->out_qh->hb_mult;
  414. else
  415. hb_mult = cppi41_channel->hw_ep->in_qh->hb_mult;
  416. }
  417. channel->status = MUSB_DMA_STATUS_BUSY;
  418. channel->actual_len = 0;
  419. if (hb_mult)
  420. packet_sz = hb_mult * (packet_sz & 0x7FF);
  421. ret = cppi41_configure_channel(channel, packet_sz, mode, dma_addr, len);
  422. if (!ret)
  423. channel->status = MUSB_DMA_STATUS_FREE;
  424. return ret;
  425. }
  426. static int cppi41_is_compatible(struct dma_channel *channel, u16 maxpacket,
  427. void *buf, u32 length)
  428. {
  429. struct cppi41_dma_channel *cppi41_channel = channel->private_data;
  430. struct cppi41_dma_controller *controller = cppi41_channel->controller;
  431. struct musb *musb = controller->musb;
  432. if (is_host_active(musb)) {
  433. WARN_ON(1);
  434. return 1;
  435. }
  436. if (cppi41_channel->hw_ep->ep_in.type != USB_ENDPOINT_XFER_BULK)
  437. return 0;
  438. if (cppi41_channel->is_tx)
  439. return 1;
  440. /* AM335x Advisory 1.0.13. No workaround for device RX mode */
  441. return 0;
  442. }
  443. static int cppi41_dma_channel_abort(struct dma_channel *channel)
  444. {
  445. struct cppi41_dma_channel *cppi41_channel = channel->private_data;
  446. struct cppi41_dma_controller *controller = cppi41_channel->controller;
  447. struct musb *musb = controller->musb;
  448. void __iomem *epio = cppi41_channel->hw_ep->regs;
  449. int tdbit;
  450. int ret;
  451. unsigned is_tx;
  452. u16 csr;
  453. is_tx = cppi41_channel->is_tx;
  454. trace_musb_cppi41_abort(cppi41_channel);
  455. if (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)
  456. return 0;
  457. list_del_init(&cppi41_channel->tx_check);
  458. if (is_tx) {
  459. csr = musb_readw(epio, MUSB_TXCSR);
  460. csr &= ~MUSB_TXCSR_DMAENAB;
  461. musb_writew(epio, MUSB_TXCSR, csr);
  462. } else {
  463. cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREQ_NONE);
  464. /* delay to drain to cppi dma pipeline for isoch */
  465. udelay(250);
  466. csr = musb_readw(epio, MUSB_RXCSR);
  467. csr &= ~(MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_DMAENAB);
  468. musb_writew(epio, MUSB_RXCSR, csr);
  469. /* wait to drain cppi dma pipe line */
  470. udelay(50);
  471. csr = musb_readw(epio, MUSB_RXCSR);
  472. if (csr & MUSB_RXCSR_RXPKTRDY) {
  473. csr |= MUSB_RXCSR_FLUSHFIFO;
  474. musb_writew(epio, MUSB_RXCSR, csr);
  475. musb_writew(epio, MUSB_RXCSR, csr);
  476. }
  477. }
  478. tdbit = 1 << cppi41_channel->port_num;
  479. if (is_tx)
  480. tdbit <<= 16;
  481. do {
  482. if (is_tx)
  483. musb_writel(musb->ctrl_base, USB_TDOWN, tdbit);
  484. ret = dmaengine_terminate_all(cppi41_channel->dc);
  485. } while (ret == -EAGAIN);
  486. if (is_tx) {
  487. musb_writel(musb->ctrl_base, USB_TDOWN, tdbit);
  488. csr = musb_readw(epio, MUSB_TXCSR);
  489. if (csr & MUSB_TXCSR_TXPKTRDY) {
  490. csr |= MUSB_TXCSR_FLUSHFIFO;
  491. musb_writew(epio, MUSB_TXCSR, csr);
  492. }
  493. }
  494. cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
  495. return 0;
  496. }
  497. static void cppi41_release_all_dma_chans(struct cppi41_dma_controller *ctrl)
  498. {
  499. struct dma_chan *dc;
  500. int i;
  501. for (i = 0; i < MUSB_DMA_NUM_CHANNELS; i++) {
  502. dc = ctrl->tx_channel[i].dc;
  503. if (dc)
  504. dma_release_channel(dc);
  505. dc = ctrl->rx_channel[i].dc;
  506. if (dc)
  507. dma_release_channel(dc);
  508. }
  509. }
  510. static void cppi41_dma_controller_stop(struct cppi41_dma_controller *controller)
  511. {
  512. cppi41_release_all_dma_chans(controller);
  513. }
  514. static int cppi41_dma_controller_start(struct cppi41_dma_controller *controller)
  515. {
  516. struct musb *musb = controller->musb;
  517. struct device *dev = musb->controller;
  518. struct device_node *np = dev->parent->of_node;
  519. struct cppi41_dma_channel *cppi41_channel;
  520. int count;
  521. int i;
  522. int ret;
  523. count = of_property_count_strings(np, "dma-names");
  524. if (count < 0)
  525. return count;
  526. for (i = 0; i < count; i++) {
  527. struct dma_chan *dc;
  528. struct dma_channel *musb_dma;
  529. const char *str;
  530. unsigned is_tx;
  531. unsigned int port;
  532. ret = of_property_read_string_index(np, "dma-names", i, &str);
  533. if (ret)
  534. goto err;
  535. if (strstarts(str, "tx"))
  536. is_tx = 1;
  537. else if (strstarts(str, "rx"))
  538. is_tx = 0;
  539. else {
  540. dev_err(dev, "Wrong dmatype %s\n", str);
  541. goto err;
  542. }
  543. ret = kstrtouint(str + 2, 0, &port);
  544. if (ret)
  545. goto err;
  546. ret = -EINVAL;
  547. if (port > MUSB_DMA_NUM_CHANNELS || !port)
  548. goto err;
  549. if (is_tx)
  550. cppi41_channel = &controller->tx_channel[port - 1];
  551. else
  552. cppi41_channel = &controller->rx_channel[port - 1];
  553. cppi41_channel->controller = controller;
  554. cppi41_channel->port_num = port;
  555. cppi41_channel->is_tx = is_tx;
  556. INIT_LIST_HEAD(&cppi41_channel->tx_check);
  557. musb_dma = &cppi41_channel->channel;
  558. musb_dma->private_data = cppi41_channel;
  559. musb_dma->status = MUSB_DMA_STATUS_FREE;
  560. musb_dma->max_len = SZ_4M;
  561. dc = dma_request_slave_channel(dev->parent, str);
  562. if (!dc) {
  563. dev_err(dev, "Failed to request %s.\n", str);
  564. ret = -EPROBE_DEFER;
  565. goto err;
  566. }
  567. cppi41_channel->dc = dc;
  568. }
  569. return 0;
  570. err:
  571. cppi41_release_all_dma_chans(controller);
  572. return ret;
  573. }
  574. void cppi41_dma_controller_destroy(struct dma_controller *c)
  575. {
  576. struct cppi41_dma_controller *controller = container_of(c,
  577. struct cppi41_dma_controller, controller);
  578. hrtimer_cancel(&controller->early_tx);
  579. cppi41_dma_controller_stop(controller);
  580. kfree(controller);
  581. }
  582. EXPORT_SYMBOL_GPL(cppi41_dma_controller_destroy);
  583. struct dma_controller *
  584. cppi41_dma_controller_create(struct musb *musb, void __iomem *base)
  585. {
  586. struct cppi41_dma_controller *controller;
  587. int ret = 0;
  588. if (!musb->controller->parent->of_node) {
  589. dev_err(musb->controller, "Need DT for the DMA engine.\n");
  590. return NULL;
  591. }
  592. controller = kzalloc(sizeof(*controller), GFP_KERNEL);
  593. if (!controller)
  594. goto kzalloc_fail;
  595. hrtimer_init(&controller->early_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  596. controller->early_tx.function = cppi41_recheck_tx_req;
  597. INIT_LIST_HEAD(&controller->early_tx_list);
  598. controller->musb = musb;
  599. controller->controller.channel_alloc = cppi41_dma_channel_allocate;
  600. controller->controller.channel_release = cppi41_dma_channel_release;
  601. controller->controller.channel_program = cppi41_dma_channel_program;
  602. controller->controller.channel_abort = cppi41_dma_channel_abort;
  603. controller->controller.is_compatible = cppi41_is_compatible;
  604. ret = cppi41_dma_controller_start(controller);
  605. if (ret)
  606. goto plat_get_fail;
  607. return &controller->controller;
  608. plat_get_fail:
  609. kfree(controller);
  610. kzalloc_fail:
  611. if (ret == -EPROBE_DEFER)
  612. return ERR_PTR(ret);
  613. return NULL;
  614. }
  615. EXPORT_SYMBOL_GPL(cppi41_dma_controller_create);