musbhsdma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * MUSB OTG driver - support for Mentor's DMA controller
  3. *
  4. * Copyright 2005 Mentor Graphics Corporation
  5. * Copyright (C) 2005-2007 by Texas Instruments
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. *
  21. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  24. * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  27. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  28. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <linux/device.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/slab.h>
  37. #include "musb_core.h"
  38. #include "musbhsdma.h"
  39. static int dma_controller_start(struct dma_controller *c)
  40. {
  41. /* nothing to do */
  42. return 0;
  43. }
  44. static void dma_channel_release(struct dma_channel *channel);
  45. static int dma_controller_stop(struct dma_controller *c)
  46. {
  47. struct musb_dma_controller *controller = container_of(c,
  48. struct musb_dma_controller, controller);
  49. struct musb *musb = controller->private_data;
  50. struct dma_channel *channel;
  51. u8 bit;
  52. if (controller->used_channels != 0) {
  53. dev_err(musb->controller,
  54. "Stopping DMA controller while channel active\n");
  55. for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) {
  56. if (controller->used_channels & (1 << bit)) {
  57. channel = &controller->channel[bit].channel;
  58. dma_channel_release(channel);
  59. if (!controller->used_channels)
  60. break;
  61. }
  62. }
  63. }
  64. return 0;
  65. }
  66. static struct dma_channel *dma_channel_allocate(struct dma_controller *c,
  67. struct musb_hw_ep *hw_ep, u8 transmit)
  68. {
  69. struct musb_dma_controller *controller = container_of(c,
  70. struct musb_dma_controller, controller);
  71. struct musb_dma_channel *musb_channel = NULL;
  72. struct dma_channel *channel = NULL;
  73. u8 bit;
  74. for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) {
  75. if (!(controller->used_channels & (1 << bit))) {
  76. controller->used_channels |= (1 << bit);
  77. musb_channel = &(controller->channel[bit]);
  78. musb_channel->controller = controller;
  79. musb_channel->idx = bit;
  80. musb_channel->epnum = hw_ep->epnum;
  81. musb_channel->transmit = transmit;
  82. channel = &(musb_channel->channel);
  83. channel->private_data = musb_channel;
  84. channel->status = MUSB_DMA_STATUS_FREE;
  85. channel->max_len = 0x10000;
  86. /* Tx => mode 1; Rx => mode 0 */
  87. channel->desired_mode = transmit;
  88. channel->actual_len = 0;
  89. break;
  90. }
  91. }
  92. return channel;
  93. }
  94. static void dma_channel_release(struct dma_channel *channel)
  95. {
  96. struct musb_dma_channel *musb_channel = channel->private_data;
  97. channel->actual_len = 0;
  98. musb_channel->start_addr = 0;
  99. musb_channel->len = 0;
  100. musb_channel->controller->used_channels &=
  101. ~(1 << musb_channel->idx);
  102. channel->status = MUSB_DMA_STATUS_UNKNOWN;
  103. }
  104. static void configure_channel(struct dma_channel *channel,
  105. u16 packet_sz, u8 mode,
  106. dma_addr_t dma_addr, u32 len)
  107. {
  108. struct musb_dma_channel *musb_channel = channel->private_data;
  109. struct musb_dma_controller *controller = musb_channel->controller;
  110. void __iomem *mbase = controller->base;
  111. u8 bchannel = musb_channel->idx;
  112. u16 csr = 0;
  113. DBG(4, "%p, pkt_sz %d, addr 0x%x, len %d, mode %d\n",
  114. channel, packet_sz, dma_addr, len, mode);
  115. if (mode) {
  116. csr |= 1 << MUSB_HSDMA_MODE1_SHIFT;
  117. BUG_ON(len < packet_sz);
  118. }
  119. csr |= MUSB_HSDMA_BURSTMODE_INCR16
  120. << MUSB_HSDMA_BURSTMODE_SHIFT;
  121. csr |= (musb_channel->epnum << MUSB_HSDMA_ENDPOINT_SHIFT)
  122. | (1 << MUSB_HSDMA_ENABLE_SHIFT)
  123. | (1 << MUSB_HSDMA_IRQENABLE_SHIFT)
  124. | (musb_channel->transmit
  125. ? (1 << MUSB_HSDMA_TRANSMIT_SHIFT)
  126. : 0);
  127. /* address/count */
  128. musb_write_hsdma_addr(mbase, bchannel, dma_addr);
  129. musb_write_hsdma_count(mbase, bchannel, len);
  130. /* control (this should start things) */
  131. musb_writew(mbase,
  132. MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL),
  133. csr);
  134. }
  135. static int dma_channel_program(struct dma_channel *channel,
  136. u16 packet_sz, u8 mode,
  137. dma_addr_t dma_addr, u32 len)
  138. {
  139. struct musb_dma_channel *musb_channel = channel->private_data;
  140. DBG(2, "ep%d-%s pkt_sz %d, dma_addr 0x%x length %d, mode %d\n",
  141. musb_channel->epnum,
  142. musb_channel->transmit ? "Tx" : "Rx",
  143. packet_sz, dma_addr, len, mode);
  144. BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
  145. channel->status == MUSB_DMA_STATUS_BUSY);
  146. channel->actual_len = 0;
  147. musb_channel->start_addr = dma_addr;
  148. musb_channel->len = len;
  149. musb_channel->max_packet_sz = packet_sz;
  150. channel->status = MUSB_DMA_STATUS_BUSY;
  151. if ((mode == 1) && (len >= packet_sz))
  152. configure_channel(channel, packet_sz, 1, dma_addr, len);
  153. else
  154. configure_channel(channel, packet_sz, 0, dma_addr, len);
  155. return true;
  156. }
  157. static int dma_channel_abort(struct dma_channel *channel)
  158. {
  159. struct musb_dma_channel *musb_channel = channel->private_data;
  160. void __iomem *mbase = musb_channel->controller->base;
  161. u8 bchannel = musb_channel->idx;
  162. int offset;
  163. u16 csr;
  164. if (channel->status == MUSB_DMA_STATUS_BUSY) {
  165. if (musb_channel->transmit) {
  166. offset = MUSB_EP_OFFSET(musb_channel->epnum,
  167. MUSB_TXCSR);
  168. /*
  169. * The programming guide says that we must clear
  170. * the DMAENAB bit before the DMAMODE bit...
  171. */
  172. csr = musb_readw(mbase, offset);
  173. csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
  174. musb_writew(mbase, offset, csr);
  175. csr &= ~MUSB_TXCSR_DMAMODE;
  176. musb_writew(mbase, offset, csr);
  177. } else {
  178. offset = MUSB_EP_OFFSET(musb_channel->epnum,
  179. MUSB_RXCSR);
  180. csr = musb_readw(mbase, offset);
  181. csr &= ~(MUSB_RXCSR_AUTOCLEAR |
  182. MUSB_RXCSR_DMAENAB |
  183. MUSB_RXCSR_DMAMODE);
  184. musb_writew(mbase, offset, csr);
  185. }
  186. musb_writew(mbase,
  187. MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL),
  188. 0);
  189. musb_write_hsdma_addr(mbase, bchannel, 0);
  190. musb_write_hsdma_count(mbase, bchannel, 0);
  191. channel->status = MUSB_DMA_STATUS_FREE;
  192. }
  193. return 0;
  194. }
  195. static irqreturn_t dma_controller_irq(int irq, void *private_data)
  196. {
  197. struct musb_dma_controller *controller = private_data;
  198. struct musb *musb = controller->private_data;
  199. struct musb_dma_channel *musb_channel;
  200. struct dma_channel *channel;
  201. void __iomem *mbase = controller->base;
  202. irqreturn_t retval = IRQ_NONE;
  203. unsigned long flags;
  204. u8 bchannel;
  205. u8 int_hsdma;
  206. u32 addr, count;
  207. u16 csr;
  208. spin_lock_irqsave(&musb->lock, flags);
  209. int_hsdma = musb_readb(mbase, MUSB_HSDMA_INTR);
  210. #ifdef CONFIG_BLACKFIN
  211. /* Clear DMA interrupt flags */
  212. musb_writeb(mbase, MUSB_HSDMA_INTR, int_hsdma);
  213. #endif
  214. if (!int_hsdma) {
  215. DBG(2, "spurious DMA irq\n");
  216. for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
  217. musb_channel = (struct musb_dma_channel *)
  218. &(controller->channel[bchannel]);
  219. channel = &musb_channel->channel;
  220. if (channel->status == MUSB_DMA_STATUS_BUSY) {
  221. count = musb_read_hsdma_count(mbase, bchannel);
  222. if (count == 0)
  223. int_hsdma |= (1 << bchannel);
  224. }
  225. }
  226. DBG(2, "int_hsdma = 0x%x\n", int_hsdma);
  227. if (!int_hsdma)
  228. goto done;
  229. }
  230. for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
  231. if (int_hsdma & (1 << bchannel)) {
  232. musb_channel = (struct musb_dma_channel *)
  233. &(controller->channel[bchannel]);
  234. channel = &musb_channel->channel;
  235. csr = musb_readw(mbase,
  236. MUSB_HSDMA_CHANNEL_OFFSET(bchannel,
  237. MUSB_HSDMA_CONTROL));
  238. if (csr & (1 << MUSB_HSDMA_BUSERROR_SHIFT)) {
  239. musb_channel->channel.status =
  240. MUSB_DMA_STATUS_BUS_ABORT;
  241. } else {
  242. u8 devctl;
  243. addr = musb_read_hsdma_addr(mbase,
  244. bchannel);
  245. channel->actual_len = addr
  246. - musb_channel->start_addr;
  247. DBG(2, "ch %p, 0x%x -> 0x%x (%zu / %d) %s\n",
  248. channel, musb_channel->start_addr,
  249. addr, channel->actual_len,
  250. musb_channel->len,
  251. (channel->actual_len
  252. < musb_channel->len) ?
  253. "=> reconfig 0" : "=> complete");
  254. devctl = musb_readb(mbase, MUSB_DEVCTL);
  255. channel->status = MUSB_DMA_STATUS_FREE;
  256. /* completed */
  257. if ((devctl & MUSB_DEVCTL_HM)
  258. && (musb_channel->transmit)
  259. && ((channel->desired_mode == 0)
  260. || (channel->actual_len &
  261. (musb_channel->max_packet_sz - 1)))
  262. ) {
  263. u8 epnum = musb_channel->epnum;
  264. int offset = MUSB_EP_OFFSET(epnum,
  265. MUSB_TXCSR);
  266. u16 txcsr;
  267. /*
  268. * The programming guide says that we
  269. * must clear DMAENAB before DMAMODE.
  270. */
  271. musb_ep_select(mbase, epnum);
  272. txcsr = musb_readw(mbase, offset);
  273. txcsr &= ~(MUSB_TXCSR_DMAENAB
  274. | MUSB_TXCSR_AUTOSET);
  275. musb_writew(mbase, offset, txcsr);
  276. /* Send out the packet */
  277. txcsr &= ~MUSB_TXCSR_DMAMODE;
  278. txcsr |= MUSB_TXCSR_TXPKTRDY;
  279. musb_writew(mbase, offset, txcsr);
  280. }
  281. musb_dma_completion(musb, musb_channel->epnum,
  282. musb_channel->transmit);
  283. }
  284. }
  285. }
  286. retval = IRQ_HANDLED;
  287. done:
  288. spin_unlock_irqrestore(&musb->lock, flags);
  289. return retval;
  290. }
  291. void dma_controller_destroy(struct dma_controller *c)
  292. {
  293. struct musb_dma_controller *controller = container_of(c,
  294. struct musb_dma_controller, controller);
  295. if (!controller)
  296. return;
  297. if (controller->irq)
  298. free_irq(controller->irq, c);
  299. kfree(controller);
  300. }
  301. struct dma_controller *__init
  302. dma_controller_create(struct musb *musb, void __iomem *base)
  303. {
  304. struct musb_dma_controller *controller;
  305. struct device *dev = musb->controller;
  306. struct platform_device *pdev = to_platform_device(dev);
  307. int irq = platform_get_irq(pdev, 1);
  308. if (irq == 0) {
  309. dev_err(dev, "No DMA interrupt line!\n");
  310. return NULL;
  311. }
  312. controller = kzalloc(sizeof(*controller), GFP_KERNEL);
  313. if (!controller)
  314. return NULL;
  315. controller->channel_count = MUSB_HSDMA_CHANNELS;
  316. controller->private_data = musb;
  317. controller->base = base;
  318. controller->controller.start = dma_controller_start;
  319. controller->controller.stop = dma_controller_stop;
  320. controller->controller.channel_alloc = dma_channel_allocate;
  321. controller->controller.channel_release = dma_channel_release;
  322. controller->controller.channel_program = dma_channel_program;
  323. controller->controller.channel_abort = dma_channel_abort;
  324. if (request_irq(irq, dma_controller_irq, IRQF_DISABLED,
  325. dev_name(musb->controller), &controller->controller)) {
  326. dev_err(dev, "request_irq %d failed!\n", irq);
  327. dma_controller_destroy(&controller->controller);
  328. return NULL;
  329. }
  330. controller->irq = irq;
  331. return &controller->controller;
  332. }