musb_cppi41.c 19 KB

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