musb_cppi41.c 19 KB

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