musb_cppi41.c 19 KB

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