musb_cppi41.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. };
  35. #define MUSB_DMA_NUM_CHANNELS 15
  36. struct cppi41_dma_controller {
  37. struct dma_controller controller;
  38. struct cppi41_dma_channel rx_channel[MUSB_DMA_NUM_CHANNELS];
  39. struct cppi41_dma_channel tx_channel[MUSB_DMA_NUM_CHANNELS];
  40. struct musb *musb;
  41. u32 rx_mode;
  42. u32 tx_mode;
  43. u32 auto_req;
  44. };
  45. static void save_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
  46. {
  47. u16 csr;
  48. u8 toggle;
  49. if (cppi41_channel->is_tx)
  50. return;
  51. if (!is_host_active(cppi41_channel->controller->musb))
  52. return;
  53. csr = musb_readw(cppi41_channel->hw_ep->regs, MUSB_RXCSR);
  54. toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
  55. cppi41_channel->usb_toggle = toggle;
  56. }
  57. static void update_rx_toggle(struct cppi41_dma_channel *cppi41_channel)
  58. {
  59. u16 csr;
  60. u8 toggle;
  61. if (cppi41_channel->is_tx)
  62. return;
  63. if (!is_host_active(cppi41_channel->controller->musb))
  64. return;
  65. csr = musb_readw(cppi41_channel->hw_ep->regs, MUSB_RXCSR);
  66. toggle = csr & MUSB_RXCSR_H_DATATOGGLE ? 1 : 0;
  67. /*
  68. * AM335x Advisory 1.0.13: Due to internal synchronisation error the
  69. * data toggle may reset from DATA1 to DATA0 during receiving data from
  70. * more than one endpoint.
  71. */
  72. if (!toggle && toggle == cppi41_channel->usb_toggle) {
  73. csr |= MUSB_RXCSR_H_DATATOGGLE | MUSB_RXCSR_H_WR_DATATOGGLE;
  74. musb_writew(cppi41_channel->hw_ep->regs, MUSB_RXCSR, csr);
  75. dev_dbg(cppi41_channel->controller->musb->controller,
  76. "Restoring DATA1 toggle.\n");
  77. }
  78. cppi41_channel->usb_toggle = toggle;
  79. }
  80. static void cppi41_dma_callback(void *private_data)
  81. {
  82. struct dma_channel *channel = private_data;
  83. struct cppi41_dma_channel *cppi41_channel = channel->private_data;
  84. struct musb_hw_ep *hw_ep = cppi41_channel->hw_ep;
  85. struct musb *musb = hw_ep->musb;
  86. unsigned long flags;
  87. struct dma_tx_state txstate;
  88. u32 transferred;
  89. spin_lock_irqsave(&musb->lock, flags);
  90. dmaengine_tx_status(cppi41_channel->dc, cppi41_channel->cookie,
  91. &txstate);
  92. transferred = cppi41_channel->prog_len - txstate.residue;
  93. cppi41_channel->transferred += transferred;
  94. dev_dbg(musb->controller, "DMA transfer done on hw_ep=%d bytes=%d/%d\n",
  95. hw_ep->epnum, cppi41_channel->transferred,
  96. cppi41_channel->total_len);
  97. update_rx_toggle(cppi41_channel);
  98. if (cppi41_channel->transferred == cppi41_channel->total_len ||
  99. transferred < cppi41_channel->packet_sz) {
  100. /* done, complete */
  101. cppi41_channel->channel.actual_len =
  102. cppi41_channel->transferred;
  103. cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
  104. musb_dma_completion(musb, hw_ep->epnum, cppi41_channel->is_tx);
  105. } else {
  106. /* next iteration, reload */
  107. struct dma_chan *dc = cppi41_channel->dc;
  108. struct dma_async_tx_descriptor *dma_desc;
  109. enum dma_transfer_direction direction;
  110. u16 csr;
  111. u32 remain_bytes;
  112. void __iomem *epio = cppi41_channel->hw_ep->regs;
  113. cppi41_channel->buf_addr += cppi41_channel->packet_sz;
  114. remain_bytes = cppi41_channel->total_len;
  115. remain_bytes -= cppi41_channel->transferred;
  116. remain_bytes = min(remain_bytes, cppi41_channel->packet_sz);
  117. cppi41_channel->prog_len = remain_bytes;
  118. direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV
  119. : DMA_DEV_TO_MEM;
  120. dma_desc = dmaengine_prep_slave_single(dc,
  121. cppi41_channel->buf_addr,
  122. remain_bytes,
  123. direction,
  124. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  125. if (WARN_ON(!dma_desc)) {
  126. spin_unlock_irqrestore(&musb->lock, flags);
  127. return;
  128. }
  129. dma_desc->callback = cppi41_dma_callback;
  130. dma_desc->callback_param = channel;
  131. cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
  132. dma_async_issue_pending(dc);
  133. if (!cppi41_channel->is_tx) {
  134. csr = musb_readw(epio, MUSB_RXCSR);
  135. csr |= MUSB_RXCSR_H_REQPKT;
  136. musb_writew(epio, MUSB_RXCSR, csr);
  137. }
  138. }
  139. spin_unlock_irqrestore(&musb->lock, flags);
  140. }
  141. static u32 update_ep_mode(unsigned ep, unsigned mode, u32 old)
  142. {
  143. unsigned shift;
  144. shift = (ep - 1) * 2;
  145. old &= ~(3 << shift);
  146. old |= mode << shift;
  147. return old;
  148. }
  149. static void cppi41_set_dma_mode(struct cppi41_dma_channel *cppi41_channel,
  150. unsigned mode)
  151. {
  152. struct cppi41_dma_controller *controller = cppi41_channel->controller;
  153. u32 port;
  154. u32 new_mode;
  155. u32 old_mode;
  156. if (cppi41_channel->is_tx)
  157. old_mode = controller->tx_mode;
  158. else
  159. old_mode = controller->rx_mode;
  160. port = cppi41_channel->port_num;
  161. new_mode = update_ep_mode(port, mode, old_mode);
  162. if (new_mode == old_mode)
  163. return;
  164. if (cppi41_channel->is_tx) {
  165. controller->tx_mode = new_mode;
  166. musb_writel(controller->musb->ctrl_base, USB_CTRL_TX_MODE,
  167. new_mode);
  168. } else {
  169. controller->rx_mode = new_mode;
  170. musb_writel(controller->musb->ctrl_base, USB_CTRL_RX_MODE,
  171. new_mode);
  172. }
  173. }
  174. static void cppi41_set_autoreq_mode(struct cppi41_dma_channel *cppi41_channel,
  175. unsigned mode)
  176. {
  177. struct cppi41_dma_controller *controller = cppi41_channel->controller;
  178. u32 port;
  179. u32 new_mode;
  180. u32 old_mode;
  181. old_mode = controller->auto_req;
  182. port = cppi41_channel->port_num;
  183. new_mode = update_ep_mode(port, mode, old_mode);
  184. if (new_mode == old_mode)
  185. return;
  186. controller->auto_req = new_mode;
  187. musb_writel(controller->musb->ctrl_base, USB_CTRL_AUTOREQ, new_mode);
  188. }
  189. static bool cppi41_configure_channel(struct dma_channel *channel,
  190. u16 packet_sz, u8 mode,
  191. dma_addr_t dma_addr, u32 len)
  192. {
  193. struct cppi41_dma_channel *cppi41_channel = channel->private_data;
  194. struct dma_chan *dc = cppi41_channel->dc;
  195. struct dma_async_tx_descriptor *dma_desc;
  196. enum dma_transfer_direction direction;
  197. struct musb *musb = cppi41_channel->controller->musb;
  198. unsigned use_gen_rndis = 0;
  199. dev_dbg(musb->controller,
  200. "configure ep%d/%x packet_sz=%d, mode=%d, dma_addr=0x%llx, len=%d is_tx=%d\n",
  201. cppi41_channel->port_num, RNDIS_REG(cppi41_channel->port_num),
  202. packet_sz, mode, (unsigned long long) dma_addr,
  203. len, cppi41_channel->is_tx);
  204. cppi41_channel->buf_addr = dma_addr;
  205. cppi41_channel->total_len = len;
  206. cppi41_channel->transferred = 0;
  207. cppi41_channel->packet_sz = packet_sz;
  208. /*
  209. * Due to AM335x' Advisory 1.0.13 we are not allowed to transfer more
  210. * than max packet size at a time.
  211. */
  212. if (cppi41_channel->is_tx)
  213. use_gen_rndis = 1;
  214. if (use_gen_rndis) {
  215. /* RNDIS mode */
  216. if (len > packet_sz) {
  217. musb_writel(musb->ctrl_base,
  218. RNDIS_REG(cppi41_channel->port_num), len);
  219. /* gen rndis */
  220. cppi41_set_dma_mode(cppi41_channel,
  221. EP_MODE_DMA_GEN_RNDIS);
  222. /* auto req */
  223. cppi41_set_autoreq_mode(cppi41_channel,
  224. EP_MODE_AUTOREG_ALL_NEOP);
  225. } else {
  226. musb_writel(musb->ctrl_base,
  227. RNDIS_REG(cppi41_channel->port_num), 0);
  228. cppi41_set_dma_mode(cppi41_channel,
  229. EP_MODE_DMA_TRANSPARENT);
  230. cppi41_set_autoreq_mode(cppi41_channel,
  231. EP_MODE_AUTOREG_NONE);
  232. }
  233. } else {
  234. /* fallback mode */
  235. cppi41_set_dma_mode(cppi41_channel, EP_MODE_DMA_TRANSPARENT);
  236. cppi41_set_autoreq_mode(cppi41_channel, EP_MODE_AUTOREG_NONE);
  237. len = min_t(u32, packet_sz, len);
  238. }
  239. cppi41_channel->prog_len = len;
  240. direction = cppi41_channel->is_tx ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
  241. dma_desc = dmaengine_prep_slave_single(dc, dma_addr, len, direction,
  242. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  243. if (!dma_desc)
  244. return false;
  245. dma_desc->callback = cppi41_dma_callback;
  246. dma_desc->callback_param = channel;
  247. cppi41_channel->cookie = dma_desc->tx_submit(dma_desc);
  248. save_rx_toggle(cppi41_channel);
  249. dma_async_issue_pending(dc);
  250. return true;
  251. }
  252. static struct dma_channel *cppi41_dma_channel_allocate(struct dma_controller *c,
  253. struct musb_hw_ep *hw_ep, u8 is_tx)
  254. {
  255. struct cppi41_dma_controller *controller = container_of(c,
  256. struct cppi41_dma_controller, controller);
  257. struct cppi41_dma_channel *cppi41_channel = NULL;
  258. u8 ch_num = hw_ep->epnum - 1;
  259. if (ch_num >= MUSB_DMA_NUM_CHANNELS)
  260. return NULL;
  261. if (is_tx)
  262. cppi41_channel = &controller->tx_channel[ch_num];
  263. else
  264. cppi41_channel = &controller->rx_channel[ch_num];
  265. if (!cppi41_channel->dc)
  266. return NULL;
  267. if (cppi41_channel->is_allocated)
  268. return NULL;
  269. cppi41_channel->hw_ep = hw_ep;
  270. cppi41_channel->is_allocated = 1;
  271. return &cppi41_channel->channel;
  272. }
  273. static void cppi41_dma_channel_release(struct dma_channel *channel)
  274. {
  275. struct cppi41_dma_channel *cppi41_channel = channel->private_data;
  276. if (cppi41_channel->is_allocated) {
  277. cppi41_channel->is_allocated = 0;
  278. channel->status = MUSB_DMA_STATUS_FREE;
  279. channel->actual_len = 0;
  280. }
  281. }
  282. static int cppi41_dma_channel_program(struct dma_channel *channel,
  283. u16 packet_sz, u8 mode,
  284. dma_addr_t dma_addr, u32 len)
  285. {
  286. int ret;
  287. BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
  288. channel->status == MUSB_DMA_STATUS_BUSY);
  289. channel->status = MUSB_DMA_STATUS_BUSY;
  290. channel->actual_len = 0;
  291. ret = cppi41_configure_channel(channel, packet_sz, mode, dma_addr, len);
  292. if (!ret)
  293. channel->status = MUSB_DMA_STATUS_FREE;
  294. return ret;
  295. }
  296. static int cppi41_is_compatible(struct dma_channel *channel, u16 maxpacket,
  297. void *buf, u32 length)
  298. {
  299. struct cppi41_dma_channel *cppi41_channel = channel->private_data;
  300. struct cppi41_dma_controller *controller = cppi41_channel->controller;
  301. struct musb *musb = controller->musb;
  302. if (is_host_active(musb)) {
  303. WARN_ON(1);
  304. return 1;
  305. }
  306. if (cppi41_channel->is_tx)
  307. return 1;
  308. /* AM335x Advisory 1.0.13. No workaround for device RX mode */
  309. return 0;
  310. }
  311. static int cppi41_dma_channel_abort(struct dma_channel *channel)
  312. {
  313. struct cppi41_dma_channel *cppi41_channel = channel->private_data;
  314. struct cppi41_dma_controller *controller = cppi41_channel->controller;
  315. struct musb *musb = controller->musb;
  316. void __iomem *epio = cppi41_channel->hw_ep->regs;
  317. int tdbit;
  318. int ret;
  319. unsigned is_tx;
  320. u16 csr;
  321. is_tx = cppi41_channel->is_tx;
  322. dev_dbg(musb->controller, "abort channel=%d, is_tx=%d\n",
  323. cppi41_channel->port_num, is_tx);
  324. if (cppi41_channel->channel.status == MUSB_DMA_STATUS_FREE)
  325. return 0;
  326. if (is_tx) {
  327. csr = musb_readw(epio, MUSB_TXCSR);
  328. csr &= ~MUSB_TXCSR_DMAENAB;
  329. musb_writew(epio, MUSB_TXCSR, csr);
  330. } else {
  331. csr = musb_readw(epio, MUSB_RXCSR);
  332. csr &= ~(MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_DMAENAB);
  333. musb_writew(epio, MUSB_RXCSR, csr);
  334. csr = musb_readw(epio, MUSB_RXCSR);
  335. if (csr & MUSB_RXCSR_RXPKTRDY) {
  336. csr |= MUSB_RXCSR_FLUSHFIFO;
  337. musb_writew(epio, MUSB_RXCSR, csr);
  338. musb_writew(epio, MUSB_RXCSR, csr);
  339. }
  340. }
  341. tdbit = 1 << cppi41_channel->port_num;
  342. if (is_tx)
  343. tdbit <<= 16;
  344. do {
  345. musb_writel(musb->ctrl_base, USB_TDOWN, tdbit);
  346. ret = dmaengine_terminate_all(cppi41_channel->dc);
  347. } while (ret == -EAGAIN);
  348. musb_writel(musb->ctrl_base, USB_TDOWN, tdbit);
  349. if (is_tx) {
  350. csr = musb_readw(epio, MUSB_TXCSR);
  351. if (csr & MUSB_TXCSR_TXPKTRDY) {
  352. csr |= MUSB_TXCSR_FLUSHFIFO;
  353. musb_writew(epio, MUSB_TXCSR, csr);
  354. }
  355. }
  356. cppi41_channel->channel.status = MUSB_DMA_STATUS_FREE;
  357. return 0;
  358. }
  359. static void cppi41_release_all_dma_chans(struct cppi41_dma_controller *ctrl)
  360. {
  361. struct dma_chan *dc;
  362. int i;
  363. for (i = 0; i < MUSB_DMA_NUM_CHANNELS; i++) {
  364. dc = ctrl->tx_channel[i].dc;
  365. if (dc)
  366. dma_release_channel(dc);
  367. dc = ctrl->rx_channel[i].dc;
  368. if (dc)
  369. dma_release_channel(dc);
  370. }
  371. }
  372. static void cppi41_dma_controller_stop(struct cppi41_dma_controller *controller)
  373. {
  374. cppi41_release_all_dma_chans(controller);
  375. }
  376. static int cppi41_dma_controller_start(struct cppi41_dma_controller *controller)
  377. {
  378. struct musb *musb = controller->musb;
  379. struct device *dev = musb->controller;
  380. struct device_node *np = dev->of_node;
  381. struct cppi41_dma_channel *cppi41_channel;
  382. int count;
  383. int i;
  384. int ret;
  385. count = of_property_count_strings(np, "dma-names");
  386. if (count < 0)
  387. return count;
  388. for (i = 0; i < count; i++) {
  389. struct dma_chan *dc;
  390. struct dma_channel *musb_dma;
  391. const char *str;
  392. unsigned is_tx;
  393. unsigned int port;
  394. ret = of_property_read_string_index(np, "dma-names", i, &str);
  395. if (ret)
  396. goto err;
  397. if (!strncmp(str, "tx", 2))
  398. is_tx = 1;
  399. else if (!strncmp(str, "rx", 2))
  400. is_tx = 0;
  401. else {
  402. dev_err(dev, "Wrong dmatype %s\n", str);
  403. goto err;
  404. }
  405. ret = kstrtouint(str + 2, 0, &port);
  406. if (ret)
  407. goto err;
  408. if (port > MUSB_DMA_NUM_CHANNELS || !port)
  409. goto err;
  410. if (is_tx)
  411. cppi41_channel = &controller->tx_channel[port - 1];
  412. else
  413. cppi41_channel = &controller->rx_channel[port - 1];
  414. cppi41_channel->controller = controller;
  415. cppi41_channel->port_num = port;
  416. cppi41_channel->is_tx = is_tx;
  417. musb_dma = &cppi41_channel->channel;
  418. musb_dma->private_data = cppi41_channel;
  419. musb_dma->status = MUSB_DMA_STATUS_FREE;
  420. musb_dma->max_len = SZ_4M;
  421. dc = dma_request_slave_channel(dev, str);
  422. if (!dc) {
  423. dev_err(dev, "Falied to request %s.\n", str);
  424. goto err;
  425. }
  426. cppi41_channel->dc = dc;
  427. }
  428. return 0;
  429. err:
  430. cppi41_release_all_dma_chans(controller);
  431. return -EINVAL;
  432. }
  433. void dma_controller_destroy(struct dma_controller *c)
  434. {
  435. struct cppi41_dma_controller *controller = container_of(c,
  436. struct cppi41_dma_controller, controller);
  437. cppi41_dma_controller_stop(controller);
  438. kfree(controller);
  439. }
  440. struct dma_controller *dma_controller_create(struct musb *musb,
  441. void __iomem *base)
  442. {
  443. struct cppi41_dma_controller *controller;
  444. int ret;
  445. if (!musb->controller->of_node) {
  446. dev_err(musb->controller, "Need DT for the DMA engine.\n");
  447. return NULL;
  448. }
  449. controller = kzalloc(sizeof(*controller), GFP_KERNEL);
  450. if (!controller)
  451. goto kzalloc_fail;
  452. controller->musb = musb;
  453. controller->controller.channel_alloc = cppi41_dma_channel_allocate;
  454. controller->controller.channel_release = cppi41_dma_channel_release;
  455. controller->controller.channel_program = cppi41_dma_channel_program;
  456. controller->controller.channel_abort = cppi41_dma_channel_abort;
  457. controller->controller.is_compatible = cppi41_is_compatible;
  458. ret = cppi41_dma_controller_start(controller);
  459. if (ret)
  460. goto plat_get_fail;
  461. return &controller->controller;
  462. plat_get_fail:
  463. kfree(controller);
  464. kzalloc_fail:
  465. return NULL;
  466. }