ux500_dma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * drivers/usb/musb/ux500_dma.c
  3. *
  4. * U8500 DMA support code
  5. *
  6. * Copyright (C) 2009 STMicroelectronics
  7. * Copyright (C) 2011 ST-Ericsson SA
  8. * Authors:
  9. * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
  10. * Praveena Nadahally <praveen.nadahally@stericsson.com>
  11. * Rajaram Regupathy <ragupathy.rajaram@stericsson.com>
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. #include <linux/device.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/pfn.h>
  32. #include <linux/sizes.h>
  33. #include <linux/platform_data/usb-musb-ux500.h>
  34. #include "musb_core.h"
  35. static const char *iep_chan_names[] = { "iep_1_9", "iep_2_10", "iep_3_11", "iep_4_12",
  36. "iep_5_13", "iep_6_14", "iep_7_15", "iep_8" };
  37. static const char *oep_chan_names[] = { "oep_1_9", "oep_2_10", "oep_3_11", "oep_4_12",
  38. "oep_5_13", "oep_6_14", "oep_7_15", "oep_8" };
  39. struct ux500_dma_channel {
  40. struct dma_channel channel;
  41. struct ux500_dma_controller *controller;
  42. struct musb_hw_ep *hw_ep;
  43. struct dma_chan *dma_chan;
  44. unsigned int cur_len;
  45. dma_cookie_t cookie;
  46. u8 ch_num;
  47. u8 is_tx;
  48. u8 is_allocated;
  49. };
  50. struct ux500_dma_controller {
  51. struct dma_controller controller;
  52. struct ux500_dma_channel rx_channel[UX500_MUSB_DMA_NUM_RX_TX_CHANNELS];
  53. struct ux500_dma_channel tx_channel[UX500_MUSB_DMA_NUM_RX_TX_CHANNELS];
  54. void *private_data;
  55. dma_addr_t phy_base;
  56. };
  57. /* Work function invoked from DMA callback to handle rx transfers. */
  58. static void ux500_dma_callback(void *private_data)
  59. {
  60. struct dma_channel *channel = private_data;
  61. struct ux500_dma_channel *ux500_channel = channel->private_data;
  62. struct musb_hw_ep *hw_ep = ux500_channel->hw_ep;
  63. struct musb *musb = hw_ep->musb;
  64. unsigned long flags;
  65. dev_dbg(musb->controller, "DMA rx transfer done on hw_ep=%d\n",
  66. hw_ep->epnum);
  67. spin_lock_irqsave(&musb->lock, flags);
  68. ux500_channel->channel.actual_len = ux500_channel->cur_len;
  69. ux500_channel->channel.status = MUSB_DMA_STATUS_FREE;
  70. musb_dma_completion(musb, hw_ep->epnum, ux500_channel->is_tx);
  71. spin_unlock_irqrestore(&musb->lock, flags);
  72. }
  73. static bool ux500_configure_channel(struct dma_channel *channel,
  74. u16 packet_sz, u8 mode,
  75. dma_addr_t dma_addr, u32 len)
  76. {
  77. struct ux500_dma_channel *ux500_channel = channel->private_data;
  78. struct musb_hw_ep *hw_ep = ux500_channel->hw_ep;
  79. struct dma_chan *dma_chan = ux500_channel->dma_chan;
  80. struct dma_async_tx_descriptor *dma_desc;
  81. enum dma_transfer_direction direction;
  82. struct scatterlist sg;
  83. struct dma_slave_config slave_conf;
  84. enum dma_slave_buswidth addr_width;
  85. struct musb *musb = ux500_channel->controller->private_data;
  86. dma_addr_t usb_fifo_addr = (musb->io.fifo_offset(hw_ep->epnum) +
  87. ux500_channel->controller->phy_base);
  88. dev_dbg(musb->controller,
  89. "packet_sz=%d, mode=%d, dma_addr=0x%llx, len=%d is_tx=%d\n",
  90. packet_sz, mode, (unsigned long long) dma_addr,
  91. len, ux500_channel->is_tx);
  92. ux500_channel->cur_len = len;
  93. sg_init_table(&sg, 1);
  94. sg_set_page(&sg, pfn_to_page(PFN_DOWN(dma_addr)), len,
  95. offset_in_page(dma_addr));
  96. sg_dma_address(&sg) = dma_addr;
  97. sg_dma_len(&sg) = len;
  98. direction = ux500_channel->is_tx ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
  99. addr_width = (len & 0x3) ? DMA_SLAVE_BUSWIDTH_1_BYTE :
  100. DMA_SLAVE_BUSWIDTH_4_BYTES;
  101. slave_conf.direction = direction;
  102. slave_conf.src_addr = usb_fifo_addr;
  103. slave_conf.src_addr_width = addr_width;
  104. slave_conf.src_maxburst = 16;
  105. slave_conf.dst_addr = usb_fifo_addr;
  106. slave_conf.dst_addr_width = addr_width;
  107. slave_conf.dst_maxburst = 16;
  108. slave_conf.device_fc = false;
  109. dmaengine_slave_config(dma_chan, &slave_conf);
  110. dma_desc = dmaengine_prep_slave_sg(dma_chan, &sg, 1, direction,
  111. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  112. if (!dma_desc)
  113. return false;
  114. dma_desc->callback = ux500_dma_callback;
  115. dma_desc->callback_param = channel;
  116. ux500_channel->cookie = dma_desc->tx_submit(dma_desc);
  117. dma_async_issue_pending(dma_chan);
  118. return true;
  119. }
  120. static struct dma_channel *ux500_dma_channel_allocate(struct dma_controller *c,
  121. struct musb_hw_ep *hw_ep, u8 is_tx)
  122. {
  123. struct ux500_dma_controller *controller = container_of(c,
  124. struct ux500_dma_controller, controller);
  125. struct ux500_dma_channel *ux500_channel = NULL;
  126. struct musb *musb = controller->private_data;
  127. u8 ch_num = hw_ep->epnum - 1;
  128. /* 8 DMA channels (0 - 7). Each DMA channel can only be allocated
  129. * to specified hw_ep. For example DMA channel 0 can only be allocated
  130. * to hw_ep 1 and 9.
  131. */
  132. if (ch_num > 7)
  133. ch_num -= 8;
  134. if (ch_num >= UX500_MUSB_DMA_NUM_RX_TX_CHANNELS)
  135. return NULL;
  136. ux500_channel = is_tx ? &(controller->tx_channel[ch_num]) :
  137. &(controller->rx_channel[ch_num]) ;
  138. /* Check if channel is already used. */
  139. if (ux500_channel->is_allocated)
  140. return NULL;
  141. ux500_channel->hw_ep = hw_ep;
  142. ux500_channel->is_allocated = 1;
  143. dev_dbg(musb->controller, "hw_ep=%d, is_tx=0x%x, channel=%d\n",
  144. hw_ep->epnum, is_tx, ch_num);
  145. return &(ux500_channel->channel);
  146. }
  147. static void ux500_dma_channel_release(struct dma_channel *channel)
  148. {
  149. struct ux500_dma_channel *ux500_channel = channel->private_data;
  150. struct musb *musb = ux500_channel->controller->private_data;
  151. dev_dbg(musb->controller, "channel=%d\n", ux500_channel->ch_num);
  152. if (ux500_channel->is_allocated) {
  153. ux500_channel->is_allocated = 0;
  154. channel->status = MUSB_DMA_STATUS_FREE;
  155. channel->actual_len = 0;
  156. }
  157. }
  158. static int ux500_dma_is_compatible(struct dma_channel *channel,
  159. u16 maxpacket, void *buf, u32 length)
  160. {
  161. if ((maxpacket & 0x3) ||
  162. ((unsigned long int) buf & 0x3) ||
  163. (length < 512) ||
  164. (length & 0x3))
  165. return false;
  166. else
  167. return true;
  168. }
  169. static int ux500_dma_channel_program(struct dma_channel *channel,
  170. u16 packet_sz, u8 mode,
  171. dma_addr_t dma_addr, u32 len)
  172. {
  173. int ret;
  174. BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
  175. channel->status == MUSB_DMA_STATUS_BUSY);
  176. channel->status = MUSB_DMA_STATUS_BUSY;
  177. channel->actual_len = 0;
  178. ret = ux500_configure_channel(channel, packet_sz, mode, dma_addr, len);
  179. if (!ret)
  180. channel->status = MUSB_DMA_STATUS_FREE;
  181. return ret;
  182. }
  183. static int ux500_dma_channel_abort(struct dma_channel *channel)
  184. {
  185. struct ux500_dma_channel *ux500_channel = channel->private_data;
  186. struct ux500_dma_controller *controller = ux500_channel->controller;
  187. struct musb *musb = controller->private_data;
  188. void __iomem *epio = musb->endpoints[ux500_channel->hw_ep->epnum].regs;
  189. u16 csr;
  190. dev_dbg(musb->controller, "channel=%d, is_tx=%d\n",
  191. ux500_channel->ch_num, ux500_channel->is_tx);
  192. if (channel->status == MUSB_DMA_STATUS_BUSY) {
  193. if (ux500_channel->is_tx) {
  194. csr = musb_readw(epio, MUSB_TXCSR);
  195. csr &= ~(MUSB_TXCSR_AUTOSET |
  196. MUSB_TXCSR_DMAENAB |
  197. MUSB_TXCSR_DMAMODE);
  198. musb_writew(epio, MUSB_TXCSR, csr);
  199. } else {
  200. csr = musb_readw(epio, MUSB_RXCSR);
  201. csr &= ~(MUSB_RXCSR_AUTOCLEAR |
  202. MUSB_RXCSR_DMAENAB |
  203. MUSB_RXCSR_DMAMODE);
  204. musb_writew(epio, MUSB_RXCSR, csr);
  205. }
  206. dmaengine_terminate_all(ux500_channel->dma_chan);
  207. channel->status = MUSB_DMA_STATUS_FREE;
  208. }
  209. return 0;
  210. }
  211. static void ux500_dma_controller_stop(struct ux500_dma_controller *controller)
  212. {
  213. struct ux500_dma_channel *ux500_channel;
  214. struct dma_channel *channel;
  215. u8 ch_num;
  216. for (ch_num = 0; ch_num < UX500_MUSB_DMA_NUM_RX_TX_CHANNELS; ch_num++) {
  217. channel = &controller->rx_channel[ch_num].channel;
  218. ux500_channel = channel->private_data;
  219. ux500_dma_channel_release(channel);
  220. if (ux500_channel->dma_chan)
  221. dma_release_channel(ux500_channel->dma_chan);
  222. }
  223. for (ch_num = 0; ch_num < UX500_MUSB_DMA_NUM_RX_TX_CHANNELS; ch_num++) {
  224. channel = &controller->tx_channel[ch_num].channel;
  225. ux500_channel = channel->private_data;
  226. ux500_dma_channel_release(channel);
  227. if (ux500_channel->dma_chan)
  228. dma_release_channel(ux500_channel->dma_chan);
  229. }
  230. }
  231. static int ux500_dma_controller_start(struct ux500_dma_controller *controller)
  232. {
  233. struct ux500_dma_channel *ux500_channel = NULL;
  234. struct musb *musb = controller->private_data;
  235. struct device *dev = musb->controller;
  236. struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
  237. struct ux500_musb_board_data *data;
  238. struct dma_channel *dma_channel = NULL;
  239. char **chan_names;
  240. u32 ch_num;
  241. u8 dir;
  242. u8 is_tx = 0;
  243. void **param_array;
  244. struct ux500_dma_channel *channel_array;
  245. dma_cap_mask_t mask;
  246. if (!plat) {
  247. dev_err(musb->controller, "No platform data\n");
  248. return -EINVAL;
  249. }
  250. data = plat->board_data;
  251. dma_cap_zero(mask);
  252. dma_cap_set(DMA_SLAVE, mask);
  253. /* Prepare the loop for RX channels */
  254. channel_array = controller->rx_channel;
  255. param_array = data ? data->dma_rx_param_array : NULL;
  256. chan_names = (char **)iep_chan_names;
  257. for (dir = 0; dir < 2; dir++) {
  258. for (ch_num = 0;
  259. ch_num < UX500_MUSB_DMA_NUM_RX_TX_CHANNELS;
  260. ch_num++) {
  261. ux500_channel = &channel_array[ch_num];
  262. ux500_channel->controller = controller;
  263. ux500_channel->ch_num = ch_num;
  264. ux500_channel->is_tx = is_tx;
  265. dma_channel = &(ux500_channel->channel);
  266. dma_channel->private_data = ux500_channel;
  267. dma_channel->status = MUSB_DMA_STATUS_FREE;
  268. dma_channel->max_len = SZ_16M;
  269. ux500_channel->dma_chan =
  270. dma_request_slave_channel(dev, chan_names[ch_num]);
  271. if (!ux500_channel->dma_chan)
  272. ux500_channel->dma_chan =
  273. dma_request_channel(mask,
  274. data ?
  275. data->dma_filter :
  276. NULL,
  277. param_array ?
  278. param_array[ch_num] :
  279. NULL);
  280. if (!ux500_channel->dma_chan) {
  281. ERR("Dma pipe allocation error dir=%d ch=%d\n",
  282. dir, ch_num);
  283. /* Release already allocated channels */
  284. ux500_dma_controller_stop(controller);
  285. return -EBUSY;
  286. }
  287. }
  288. /* Prepare the loop for TX channels */
  289. channel_array = controller->tx_channel;
  290. param_array = data ? data->dma_tx_param_array : NULL;
  291. chan_names = (char **)oep_chan_names;
  292. is_tx = 1;
  293. }
  294. return 0;
  295. }
  296. void ux500_dma_controller_destroy(struct dma_controller *c)
  297. {
  298. struct ux500_dma_controller *controller = container_of(c,
  299. struct ux500_dma_controller, controller);
  300. ux500_dma_controller_stop(controller);
  301. kfree(controller);
  302. }
  303. EXPORT_SYMBOL_GPL(ux500_dma_controller_destroy);
  304. struct dma_controller *
  305. ux500_dma_controller_create(struct musb *musb, void __iomem *base)
  306. {
  307. struct ux500_dma_controller *controller;
  308. struct platform_device *pdev = to_platform_device(musb->controller);
  309. struct resource *iomem;
  310. int ret;
  311. controller = kzalloc(sizeof(*controller), GFP_KERNEL);
  312. if (!controller)
  313. goto kzalloc_fail;
  314. controller->private_data = musb;
  315. /* Save physical address for DMA controller. */
  316. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  317. if (!iomem) {
  318. dev_err(musb->controller, "no memory resource defined\n");
  319. goto plat_get_fail;
  320. }
  321. controller->phy_base = (dma_addr_t) iomem->start;
  322. controller->controller.channel_alloc = ux500_dma_channel_allocate;
  323. controller->controller.channel_release = ux500_dma_channel_release;
  324. controller->controller.channel_program = ux500_dma_channel_program;
  325. controller->controller.channel_abort = ux500_dma_channel_abort;
  326. controller->controller.is_compatible = ux500_dma_is_compatible;
  327. ret = ux500_dma_controller_start(controller);
  328. if (ret)
  329. goto plat_get_fail;
  330. return &controller->controller;
  331. plat_get_fail:
  332. kfree(controller);
  333. kzalloc_fail:
  334. return NULL;
  335. }
  336. EXPORT_SYMBOL_GPL(ux500_dma_controller_create);