musb_cppi41.c 21 KB

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