ux500_dma.c 12 KB

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