musb_cppi41.c 20 KB

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