musbhsdma.c 12 KB

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