musb_cppi41.c 19 KB

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