musbhsdma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 void dma_channel_release(struct dma_channel *channel);
  40. static void dma_controller_stop(struct musb_dma_controller *controller)
  41. {
  42. struct musb *musb = controller->private_data;
  43. struct dma_channel *channel;
  44. u8 bit;
  45. if (controller->used_channels != 0) {
  46. dev_err(musb->controller,
  47. "Stopping DMA controller while channel active\n");
  48. for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) {
  49. if (controller->used_channels & (1 << bit)) {
  50. channel = &controller->channel[bit].channel;
  51. dma_channel_release(channel);
  52. if (!controller->used_channels)
  53. break;
  54. }
  55. }
  56. }
  57. }
  58. static struct dma_channel *dma_channel_allocate(struct dma_controller *c,
  59. struct musb_hw_ep *hw_ep, u8 transmit)
  60. {
  61. struct musb_dma_controller *controller = container_of(c,
  62. struct musb_dma_controller, controller);
  63. struct musb_dma_channel *musb_channel = NULL;
  64. struct dma_channel *channel = NULL;
  65. u8 bit;
  66. for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) {
  67. if (!(controller->used_channels & (1 << bit))) {
  68. controller->used_channels |= (1 << bit);
  69. musb_channel = &(controller->channel[bit]);
  70. musb_channel->controller = controller;
  71. musb_channel->idx = bit;
  72. musb_channel->epnum = hw_ep->epnum;
  73. musb_channel->transmit = transmit;
  74. channel = &(musb_channel->channel);
  75. channel->private_data = musb_channel;
  76. channel->status = MUSB_DMA_STATUS_FREE;
  77. channel->max_len = 0x100000;
  78. /* Tx => mode 1; Rx => mode 0 */
  79. channel->desired_mode = transmit;
  80. channel->actual_len = 0;
  81. break;
  82. }
  83. }
  84. return channel;
  85. }
  86. static void dma_channel_release(struct dma_channel *channel)
  87. {
  88. struct musb_dma_channel *musb_channel = channel->private_data;
  89. channel->actual_len = 0;
  90. musb_channel->start_addr = 0;
  91. musb_channel->len = 0;
  92. musb_channel->controller->used_channels &=
  93. ~(1 << musb_channel->idx);
  94. channel->status = MUSB_DMA_STATUS_UNKNOWN;
  95. }
  96. static void configure_channel(struct dma_channel *channel,
  97. u16 packet_sz, u8 mode,
  98. dma_addr_t dma_addr, u32 len)
  99. {
  100. struct musb_dma_channel *musb_channel = channel->private_data;
  101. struct musb_dma_controller *controller = musb_channel->controller;
  102. struct musb *musb = controller->private_data;
  103. void __iomem *mbase = controller->base;
  104. u8 bchannel = musb_channel->idx;
  105. u16 csr = 0;
  106. dev_dbg(musb->controller, "%p, pkt_sz %d, addr 0x%x, len %d, mode %d\n",
  107. channel, packet_sz, dma_addr, len, mode);
  108. if (mode) {
  109. csr |= 1 << MUSB_HSDMA_MODE1_SHIFT;
  110. BUG_ON(len < packet_sz);
  111. }
  112. csr |= MUSB_HSDMA_BURSTMODE_INCR16
  113. << MUSB_HSDMA_BURSTMODE_SHIFT;
  114. csr |= (musb_channel->epnum << MUSB_HSDMA_ENDPOINT_SHIFT)
  115. | (1 << MUSB_HSDMA_ENABLE_SHIFT)
  116. | (1 << MUSB_HSDMA_IRQENABLE_SHIFT)
  117. | (musb_channel->transmit
  118. ? (1 << MUSB_HSDMA_TRANSMIT_SHIFT)
  119. : 0);
  120. /* address/count */
  121. musb_write_hsdma_addr(mbase, bchannel, dma_addr);
  122. musb_write_hsdma_count(mbase, bchannel, len);
  123. /* control (this should start things) */
  124. musb_writew(mbase,
  125. MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL),
  126. csr);
  127. }
  128. static int dma_channel_program(struct dma_channel *channel,
  129. u16 packet_sz, u8 mode,
  130. dma_addr_t dma_addr, u32 len)
  131. {
  132. struct musb_dma_channel *musb_channel = channel->private_data;
  133. struct musb_dma_controller *controller = musb_channel->controller;
  134. struct musb *musb = controller->private_data;
  135. dev_dbg(musb->controller, "ep%d-%s pkt_sz %d, dma_addr 0x%x length %d, mode %d\n",
  136. musb_channel->epnum,
  137. musb_channel->transmit ? "Tx" : "Rx",
  138. packet_sz, dma_addr, len, mode);
  139. BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
  140. channel->status == MUSB_DMA_STATUS_BUSY);
  141. /* Let targets check/tweak the arguments */
  142. if (musb->ops->adjust_channel_params) {
  143. int ret = musb->ops->adjust_channel_params(channel,
  144. packet_sz, &mode, &dma_addr, &len);
  145. if (ret)
  146. return ret;
  147. }
  148. /*
  149. * The DMA engine in RTL1.8 and above cannot handle
  150. * DMA addresses that are not aligned to a 4 byte boundary.
  151. * It ends up masking the last two bits of the address
  152. * programmed in DMA_ADDR.
  153. *
  154. * Fail such DMA transfers, so that the backup PIO mode
  155. * can carry out the transfer
  156. */
  157. if ((musb->hwvers >= MUSB_HWVERS_1800) && (dma_addr % 4))
  158. return false;
  159. channel->actual_len = 0;
  160. musb_channel->start_addr = dma_addr;
  161. musb_channel->len = len;
  162. musb_channel->max_packet_sz = packet_sz;
  163. channel->status = MUSB_DMA_STATUS_BUSY;
  164. configure_channel(channel, packet_sz, mode, dma_addr, len);
  165. return true;
  166. }
  167. static int dma_channel_abort(struct dma_channel *channel)
  168. {
  169. struct musb_dma_channel *musb_channel = channel->private_data;
  170. void __iomem *mbase = musb_channel->controller->base;
  171. struct musb *musb = musb_channel->controller->private_data;
  172. u8 bchannel = musb_channel->idx;
  173. int offset;
  174. u16 csr;
  175. if (channel->status == MUSB_DMA_STATUS_BUSY) {
  176. if (musb_channel->transmit) {
  177. offset = musb->io.ep_offset(musb_channel->epnum,
  178. MUSB_TXCSR);
  179. /*
  180. * The programming guide says that we must clear
  181. * the DMAENAB bit before the DMAMODE bit...
  182. */
  183. csr = musb_readw(mbase, offset);
  184. csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
  185. musb_writew(mbase, offset, csr);
  186. csr &= ~MUSB_TXCSR_DMAMODE;
  187. musb_writew(mbase, offset, csr);
  188. } else {
  189. offset = musb->io.ep_offset(musb_channel->epnum,
  190. MUSB_RXCSR);
  191. csr = musb_readw(mbase, offset);
  192. csr &= ~(MUSB_RXCSR_AUTOCLEAR |
  193. MUSB_RXCSR_DMAENAB |
  194. MUSB_RXCSR_DMAMODE);
  195. musb_writew(mbase, offset, csr);
  196. }
  197. musb_writew(mbase,
  198. MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL),
  199. 0);
  200. musb_write_hsdma_addr(mbase, bchannel, 0);
  201. musb_write_hsdma_count(mbase, bchannel, 0);
  202. channel->status = MUSB_DMA_STATUS_FREE;
  203. }
  204. return 0;
  205. }
  206. static irqreturn_t dma_controller_irq(int irq, void *private_data)
  207. {
  208. struct musb_dma_controller *controller = private_data;
  209. struct musb *musb = controller->private_data;
  210. struct musb_dma_channel *musb_channel;
  211. struct dma_channel *channel;
  212. void __iomem *mbase = controller->base;
  213. irqreturn_t retval = IRQ_NONE;
  214. unsigned long flags;
  215. u8 bchannel;
  216. u8 int_hsdma;
  217. u32 addr, count;
  218. u16 csr;
  219. spin_lock_irqsave(&musb->lock, flags);
  220. int_hsdma = musb_readb(mbase, MUSB_HSDMA_INTR);
  221. #ifdef CONFIG_BLACKFIN
  222. /* Clear DMA interrupt flags */
  223. musb_writeb(mbase, MUSB_HSDMA_INTR, int_hsdma);
  224. #endif
  225. if (!int_hsdma) {
  226. dev_dbg(musb->controller, "spurious DMA irq\n");
  227. for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
  228. musb_channel = (struct musb_dma_channel *)
  229. &(controller->channel[bchannel]);
  230. channel = &musb_channel->channel;
  231. if (channel->status == MUSB_DMA_STATUS_BUSY) {
  232. count = musb_read_hsdma_count(mbase, bchannel);
  233. if (count == 0)
  234. int_hsdma |= (1 << bchannel);
  235. }
  236. }
  237. dev_dbg(musb->controller, "int_hsdma = 0x%x\n", int_hsdma);
  238. if (!int_hsdma)
  239. goto done;
  240. }
  241. for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
  242. if (int_hsdma & (1 << bchannel)) {
  243. musb_channel = (struct musb_dma_channel *)
  244. &(controller->channel[bchannel]);
  245. channel = &musb_channel->channel;
  246. csr = musb_readw(mbase,
  247. MUSB_HSDMA_CHANNEL_OFFSET(bchannel,
  248. MUSB_HSDMA_CONTROL));
  249. if (csr & (1 << MUSB_HSDMA_BUSERROR_SHIFT)) {
  250. musb_channel->channel.status =
  251. MUSB_DMA_STATUS_BUS_ABORT;
  252. } else {
  253. u8 devctl;
  254. addr = musb_read_hsdma_addr(mbase,
  255. bchannel);
  256. channel->actual_len = addr
  257. - musb_channel->start_addr;
  258. dev_dbg(musb->controller, "ch %p, 0x%x -> 0x%x (%zu / %d) %s\n",
  259. channel, musb_channel->start_addr,
  260. addr, channel->actual_len,
  261. musb_channel->len,
  262. (channel->actual_len
  263. < musb_channel->len) ?
  264. "=> reconfig 0" : "=> complete");
  265. devctl = musb_readb(mbase, MUSB_DEVCTL);
  266. channel->status = MUSB_DMA_STATUS_FREE;
  267. /* completed */
  268. if ((devctl & MUSB_DEVCTL_HM)
  269. && (musb_channel->transmit)
  270. && ((channel->desired_mode == 0)
  271. || (channel->actual_len &
  272. (musb_channel->max_packet_sz - 1)))
  273. ) {
  274. u8 epnum = musb_channel->epnum;
  275. int offset = musb->io.ep_offset(epnum,
  276. MUSB_TXCSR);
  277. u16 txcsr;
  278. /*
  279. * The programming guide says that we
  280. * must clear DMAENAB before DMAMODE.
  281. */
  282. musb_ep_select(mbase, epnum);
  283. txcsr = musb_readw(mbase, offset);
  284. txcsr &= ~(MUSB_TXCSR_DMAENAB
  285. | MUSB_TXCSR_AUTOSET);
  286. musb_writew(mbase, offset, txcsr);
  287. /* Send out the packet */
  288. txcsr &= ~MUSB_TXCSR_DMAMODE;
  289. txcsr |= MUSB_TXCSR_TXPKTRDY;
  290. musb_writew(mbase, offset, txcsr);
  291. }
  292. musb_dma_completion(musb, musb_channel->epnum,
  293. musb_channel->transmit);
  294. }
  295. }
  296. }
  297. retval = IRQ_HANDLED;
  298. done:
  299. spin_unlock_irqrestore(&musb->lock, flags);
  300. return retval;
  301. }
  302. void musbhs_dma_controller_destroy(struct dma_controller *c)
  303. {
  304. struct musb_dma_controller *controller = container_of(c,
  305. struct musb_dma_controller, controller);
  306. dma_controller_stop(controller);
  307. if (controller->irq)
  308. free_irq(controller->irq, c);
  309. kfree(controller);
  310. }
  311. EXPORT_SYMBOL_GPL(musbhs_dma_controller_destroy);
  312. struct dma_controller *musbhs_dma_controller_create(struct musb *musb,
  313. void __iomem *base)
  314. {
  315. struct musb_dma_controller *controller;
  316. struct device *dev = musb->controller;
  317. struct platform_device *pdev = to_platform_device(dev);
  318. int irq = platform_get_irq_byname(pdev, "dma");
  319. if (irq <= 0) {
  320. dev_err(dev, "No DMA interrupt line!\n");
  321. return NULL;
  322. }
  323. controller = kzalloc(sizeof(*controller), GFP_KERNEL);
  324. if (!controller)
  325. return NULL;
  326. controller->channel_count = MUSB_HSDMA_CHANNELS;
  327. controller->private_data = musb;
  328. controller->base = base;
  329. controller->controller.channel_alloc = dma_channel_allocate;
  330. controller->controller.channel_release = dma_channel_release;
  331. controller->controller.channel_program = dma_channel_program;
  332. controller->controller.channel_abort = dma_channel_abort;
  333. if (request_irq(irq, dma_controller_irq, 0,
  334. dev_name(musb->controller), &controller->controller)) {
  335. dev_err(dev, "request_irq %d failed!\n", irq);
  336. musb_dma_controller_destroy(&controller->controller);
  337. return NULL;
  338. }
  339. controller->irq = irq;
  340. return &controller->controller;
  341. }
  342. EXPORT_SYMBOL_GPL(musbhs_dma_controller_create);