ux500_dma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * drivers/usb/musb/ux500_dma.c
  3. *
  4. * U8500 and U5500 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 <mach/usb.h>
  33. #include "musb_core.h"
  34. struct ux500_dma_channel {
  35. struct dma_channel channel;
  36. struct ux500_dma_controller *controller;
  37. struct musb_hw_ep *hw_ep;
  38. struct dma_chan *dma_chan;
  39. unsigned int cur_len;
  40. dma_cookie_t cookie;
  41. u8 ch_num;
  42. u8 is_tx;
  43. u8 is_allocated;
  44. };
  45. struct ux500_dma_controller {
  46. struct dma_controller controller;
  47. struct ux500_dma_channel rx_channel[UX500_MUSB_DMA_NUM_RX_CHANNELS];
  48. struct ux500_dma_channel tx_channel[UX500_MUSB_DMA_NUM_TX_CHANNELS];
  49. u32 num_rx_channels;
  50. u32 num_tx_channels;
  51. void *private_data;
  52. dma_addr_t phy_base;
  53. };
  54. /* Work function invoked from DMA callback to handle rx transfers. */
  55. void ux500_dma_callback(void *private_data)
  56. {
  57. struct dma_channel *channel = private_data;
  58. struct ux500_dma_channel *ux500_channel = channel->private_data;
  59. struct musb_hw_ep *hw_ep = ux500_channel->hw_ep;
  60. struct musb *musb = hw_ep->musb;
  61. unsigned long flags;
  62. dev_dbg(musb->controller, "DMA rx transfer done on hw_ep=%d\n",
  63. hw_ep->epnum);
  64. spin_lock_irqsave(&musb->lock, flags);
  65. ux500_channel->channel.actual_len = ux500_channel->cur_len;
  66. ux500_channel->channel.status = MUSB_DMA_STATUS_FREE;
  67. musb_dma_completion(musb, hw_ep->epnum,
  68. ux500_channel->is_tx);
  69. spin_unlock_irqrestore(&musb->lock, flags);
  70. }
  71. static bool ux500_configure_channel(struct dma_channel *channel,
  72. u16 packet_sz, u8 mode,
  73. dma_addr_t dma_addr, u32 len)
  74. {
  75. struct ux500_dma_channel *ux500_channel = channel->private_data;
  76. struct musb_hw_ep *hw_ep = ux500_channel->hw_ep;
  77. struct dma_chan *dma_chan = ux500_channel->dma_chan;
  78. struct dma_async_tx_descriptor *dma_desc;
  79. enum dma_transfer_direction direction;
  80. struct scatterlist sg;
  81. struct dma_slave_config slave_conf;
  82. enum dma_slave_buswidth addr_width;
  83. dma_addr_t usb_fifo_addr = (MUSB_FIFO_OFFSET(hw_ep->epnum) +
  84. ux500_channel->controller->phy_base);
  85. struct musb *musb = ux500_channel->controller->private_data;
  86. dev_dbg(musb->controller,
  87. "packet_sz=%d, mode=%d, dma_addr=0x%x, len=%d is_tx=%d\n",
  88. packet_sz, mode, dma_addr, len, ux500_channel->is_tx);
  89. ux500_channel->cur_len = len;
  90. sg_init_table(&sg, 1);
  91. sg_set_page(&sg, pfn_to_page(PFN_DOWN(dma_addr)), len,
  92. offset_in_page(dma_addr));
  93. sg_dma_address(&sg) = dma_addr;
  94. sg_dma_len(&sg) = len;
  95. direction = ux500_channel->is_tx ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
  96. addr_width = (len & 0x3) ? DMA_SLAVE_BUSWIDTH_1_BYTE :
  97. DMA_SLAVE_BUSWIDTH_4_BYTES;
  98. slave_conf.direction = direction;
  99. slave_conf.src_addr = usb_fifo_addr;
  100. slave_conf.src_addr_width = addr_width;
  101. slave_conf.src_maxburst = 16;
  102. slave_conf.dst_addr = usb_fifo_addr;
  103. slave_conf.dst_addr_width = addr_width;
  104. slave_conf.dst_maxburst = 16;
  105. slave_conf.device_fc = false;
  106. dma_chan->device->device_control(dma_chan, DMA_SLAVE_CONFIG,
  107. (unsigned long) &slave_conf);
  108. dma_desc = dmaengine_prep_slave_sg(dma_chan, &sg, 1, direction,
  109. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  110. if (!dma_desc)
  111. return false;
  112. dma_desc->callback = ux500_dma_callback;
  113. dma_desc->callback_param = channel;
  114. ux500_channel->cookie = dma_desc->tx_submit(dma_desc);
  115. dma_async_issue_pending(dma_chan);
  116. return true;
  117. }
  118. static struct dma_channel *ux500_dma_channel_allocate(struct dma_controller *c,
  119. struct musb_hw_ep *hw_ep, u8 is_tx)
  120. {
  121. struct ux500_dma_controller *controller = container_of(c,
  122. struct ux500_dma_controller, controller);
  123. struct ux500_dma_channel *ux500_channel = NULL;
  124. struct musb *musb = controller->private_data;
  125. u8 ch_num = hw_ep->epnum - 1;
  126. u32 max_ch;
  127. /* Max 8 DMA channels (0 - 7). Each DMA channel can only be allocated
  128. * to specified hw_ep. For example DMA channel 0 can only be allocated
  129. * to hw_ep 1 and 9.
  130. */
  131. if (ch_num > 7)
  132. ch_num -= 8;
  133. max_ch = is_tx ? controller->num_tx_channels :
  134. controller->num_rx_channels;
  135. if (ch_num >= max_ch)
  136. return NULL;
  137. ux500_channel = is_tx ? &(controller->tx_channel[ch_num]) :
  138. &(controller->rx_channel[ch_num]) ;
  139. /* Check if channel is already used. */
  140. if (ux500_channel->is_allocated)
  141. return NULL;
  142. ux500_channel->hw_ep = hw_ep;
  143. ux500_channel->is_allocated = 1;
  144. dev_dbg(musb->controller, "hw_ep=%d, is_tx=0x%x, channel=%d\n",
  145. hw_ep->epnum, is_tx, ch_num);
  146. return &(ux500_channel->channel);
  147. }
  148. static void ux500_dma_channel_release(struct dma_channel *channel)
  149. {
  150. struct ux500_dma_channel *ux500_channel = channel->private_data;
  151. struct musb *musb = ux500_channel->controller->private_data;
  152. dev_dbg(musb->controller, "channel=%d\n", ux500_channel->ch_num);
  153. if (ux500_channel->is_allocated) {
  154. ux500_channel->is_allocated = 0;
  155. channel->status = MUSB_DMA_STATUS_FREE;
  156. channel->actual_len = 0;
  157. }
  158. }
  159. static int ux500_dma_is_compatible(struct dma_channel *channel,
  160. u16 maxpacket, void *buf, u32 length)
  161. {
  162. if ((maxpacket & 0x3) ||
  163. ((int)buf & 0x3) ||
  164. (length < 512) ||
  165. (length & 0x3))
  166. return false;
  167. else
  168. return true;
  169. }
  170. static int ux500_dma_channel_program(struct dma_channel *channel,
  171. u16 packet_sz, u8 mode,
  172. dma_addr_t dma_addr, u32 len)
  173. {
  174. int ret;
  175. BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
  176. channel->status == MUSB_DMA_STATUS_BUSY);
  177. if (!ux500_dma_is_compatible(channel, packet_sz, (void *)dma_addr, len))
  178. return false;
  179. channel->status = MUSB_DMA_STATUS_BUSY;
  180. channel->actual_len = 0;
  181. ret = ux500_configure_channel(channel, packet_sz, mode, dma_addr, len);
  182. if (!ret)
  183. channel->status = MUSB_DMA_STATUS_FREE;
  184. return ret;
  185. }
  186. static int ux500_dma_channel_abort(struct dma_channel *channel)
  187. {
  188. struct ux500_dma_channel *ux500_channel = channel->private_data;
  189. struct ux500_dma_controller *controller = ux500_channel->controller;
  190. struct musb *musb = controller->private_data;
  191. void __iomem *epio = musb->endpoints[ux500_channel->hw_ep->epnum].regs;
  192. u16 csr;
  193. dev_dbg(musb->controller, "channel=%d, is_tx=%d\n",
  194. ux500_channel->ch_num, ux500_channel->is_tx);
  195. if (channel->status == MUSB_DMA_STATUS_BUSY) {
  196. if (ux500_channel->is_tx) {
  197. csr = musb_readw(epio, MUSB_TXCSR);
  198. csr &= ~(MUSB_TXCSR_AUTOSET |
  199. MUSB_TXCSR_DMAENAB |
  200. MUSB_TXCSR_DMAMODE);
  201. musb_writew(epio, MUSB_TXCSR, csr);
  202. } else {
  203. csr = musb_readw(epio, MUSB_RXCSR);
  204. csr &= ~(MUSB_RXCSR_AUTOCLEAR |
  205. MUSB_RXCSR_DMAENAB |
  206. MUSB_RXCSR_DMAMODE);
  207. musb_writew(epio, MUSB_RXCSR, csr);
  208. }
  209. ux500_channel->dma_chan->device->
  210. device_control(ux500_channel->dma_chan,
  211. DMA_TERMINATE_ALL, 0);
  212. channel->status = MUSB_DMA_STATUS_FREE;
  213. }
  214. return 0;
  215. }
  216. static int ux500_dma_controller_stop(struct dma_controller *c)
  217. {
  218. struct ux500_dma_controller *controller = container_of(c,
  219. struct ux500_dma_controller, controller);
  220. struct ux500_dma_channel *ux500_channel;
  221. struct dma_channel *channel;
  222. u8 ch_num;
  223. for (ch_num = 0; ch_num < controller->num_rx_channels; ch_num++) {
  224. channel = &controller->rx_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. for (ch_num = 0; ch_num < controller->num_tx_channels; ch_num++) {
  231. channel = &controller->tx_channel[ch_num].channel;
  232. ux500_channel = channel->private_data;
  233. ux500_dma_channel_release(channel);
  234. if (ux500_channel->dma_chan)
  235. dma_release_channel(ux500_channel->dma_chan);
  236. }
  237. return 0;
  238. }
  239. static int ux500_dma_controller_start(struct dma_controller *c)
  240. {
  241. struct ux500_dma_controller *controller = container_of(c,
  242. struct ux500_dma_controller, controller);
  243. struct ux500_dma_channel *ux500_channel = NULL;
  244. struct musb *musb = controller->private_data;
  245. struct device *dev = musb->controller;
  246. struct musb_hdrc_platform_data *plat = dev->platform_data;
  247. struct ux500_musb_board_data *data = plat->board_data;
  248. struct dma_channel *dma_channel = NULL;
  249. u32 ch_num;
  250. u8 dir;
  251. u8 is_tx = 0;
  252. void **param_array;
  253. struct ux500_dma_channel *channel_array;
  254. u32 ch_count;
  255. dma_cap_mask_t mask;
  256. if ((data->num_rx_channels > UX500_MUSB_DMA_NUM_RX_CHANNELS) ||
  257. (data->num_tx_channels > UX500_MUSB_DMA_NUM_TX_CHANNELS))
  258. return -EINVAL;
  259. controller->num_rx_channels = data->num_rx_channels;
  260. controller->num_tx_channels = data->num_tx_channels;
  261. dma_cap_zero(mask);
  262. dma_cap_set(DMA_SLAVE, mask);
  263. /* Prepare the loop for RX channels */
  264. channel_array = controller->rx_channel;
  265. ch_count = data->num_rx_channels;
  266. param_array = data->dma_rx_param_array;
  267. for (dir = 0; dir < 2; dir++) {
  268. for (ch_num = 0; ch_num < ch_count; ch_num++) {
  269. ux500_channel = &channel_array[ch_num];
  270. ux500_channel->controller = controller;
  271. ux500_channel->ch_num = ch_num;
  272. ux500_channel->is_tx = is_tx;
  273. dma_channel = &(ux500_channel->channel);
  274. dma_channel->private_data = ux500_channel;
  275. dma_channel->status = MUSB_DMA_STATUS_FREE;
  276. dma_channel->max_len = SZ_16M;
  277. ux500_channel->dma_chan = dma_request_channel(mask,
  278. data->dma_filter,
  279. param_array[ch_num]);
  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(c);
  285. return -EBUSY;
  286. }
  287. }
  288. /* Prepare the loop for TX channels */
  289. channel_array = controller->tx_channel;
  290. ch_count = data->num_tx_channels;
  291. param_array = data->dma_tx_param_array;
  292. is_tx = 1;
  293. }
  294. return 0;
  295. }
  296. void dma_controller_destroy(struct dma_controller *c)
  297. {
  298. struct ux500_dma_controller *controller = container_of(c,
  299. struct ux500_dma_controller, controller);
  300. kfree(controller);
  301. }
  302. struct dma_controller *__init
  303. dma_controller_create(struct musb *musb, void __iomem *base)
  304. {
  305. struct ux500_dma_controller *controller;
  306. struct platform_device *pdev = to_platform_device(musb->controller);
  307. struct resource *iomem;
  308. controller = kzalloc(sizeof(*controller), GFP_KERNEL);
  309. if (!controller)
  310. return NULL;
  311. controller->private_data = musb;
  312. /* Save physical address for DMA controller. */
  313. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  314. controller->phy_base = (dma_addr_t) iomem->start;
  315. controller->controller.start = ux500_dma_controller_start;
  316. controller->controller.stop = ux500_dma_controller_stop;
  317. controller->controller.channel_alloc = ux500_dma_channel_allocate;
  318. controller->controller.channel_release = ux500_dma_channel_release;
  319. controller->controller.channel_program = ux500_dma_channel_program;
  320. controller->controller.channel_abort = ux500_dma_channel_abort;
  321. controller->controller.is_compatible = ux500_dma_is_compatible;
  322. return &controller->controller;
  323. }