musb_cppi41.c 20 KB

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