musbhsdma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. 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. struct musb_dma_controller *controller = musb_channel->controller;
  141. struct musb *musb = controller->private_data;
  142. DBG(2, "ep%d-%s pkt_sz %d, dma_addr 0x%x length %d, mode %d\n",
  143. musb_channel->epnum,
  144. musb_channel->transmit ? "Tx" : "Rx",
  145. packet_sz, dma_addr, len, mode);
  146. BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
  147. channel->status == MUSB_DMA_STATUS_BUSY);
  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. u8 bchannel = musb_channel->idx;
  172. int offset;
  173. u16 csr;
  174. if (channel->status == MUSB_DMA_STATUS_BUSY) {
  175. if (musb_channel->transmit) {
  176. offset = MUSB_EP_OFFSET(musb_channel->epnum,
  177. MUSB_TXCSR);
  178. /*
  179. * The programming guide says that we must clear
  180. * the DMAENAB bit before the DMAMODE bit...
  181. */
  182. csr = musb_readw(mbase, offset);
  183. csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
  184. musb_writew(mbase, offset, csr);
  185. csr &= ~MUSB_TXCSR_DMAMODE;
  186. musb_writew(mbase, offset, csr);
  187. } else {
  188. offset = MUSB_EP_OFFSET(musb_channel->epnum,
  189. MUSB_RXCSR);
  190. csr = musb_readw(mbase, offset);
  191. csr &= ~(MUSB_RXCSR_AUTOCLEAR |
  192. MUSB_RXCSR_DMAENAB |
  193. MUSB_RXCSR_DMAMODE);
  194. musb_writew(mbase, offset, csr);
  195. }
  196. musb_writew(mbase,
  197. MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL),
  198. 0);
  199. musb_write_hsdma_addr(mbase, bchannel, 0);
  200. musb_write_hsdma_count(mbase, bchannel, 0);
  201. channel->status = MUSB_DMA_STATUS_FREE;
  202. }
  203. return 0;
  204. }
  205. static irqreturn_t dma_controller_irq(int irq, void *private_data)
  206. {
  207. struct musb_dma_controller *controller = private_data;
  208. struct musb *musb = controller->private_data;
  209. struct musb_dma_channel *musb_channel;
  210. struct dma_channel *channel;
  211. void __iomem *mbase = controller->base;
  212. irqreturn_t retval = IRQ_NONE;
  213. unsigned long flags;
  214. u8 bchannel;
  215. u8 int_hsdma;
  216. u32 addr, count;
  217. u16 csr;
  218. spin_lock_irqsave(&musb->lock, flags);
  219. int_hsdma = musb_readb(mbase, MUSB_HSDMA_INTR);
  220. #ifdef CONFIG_BLACKFIN
  221. /* Clear DMA interrupt flags */
  222. musb_writeb(mbase, MUSB_HSDMA_INTR, int_hsdma);
  223. #endif
  224. if (!int_hsdma) {
  225. DBG(2, "spurious DMA irq\n");
  226. for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
  227. musb_channel = (struct musb_dma_channel *)
  228. &(controller->channel[bchannel]);
  229. channel = &musb_channel->channel;
  230. if (channel->status == MUSB_DMA_STATUS_BUSY) {
  231. count = musb_read_hsdma_count(mbase, bchannel);
  232. if (count == 0)
  233. int_hsdma |= (1 << bchannel);
  234. }
  235. }
  236. DBG(2, "int_hsdma = 0x%x\n", int_hsdma);
  237. if (!int_hsdma)
  238. goto done;
  239. }
  240. for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
  241. if (int_hsdma & (1 << bchannel)) {
  242. musb_channel = (struct musb_dma_channel *)
  243. &(controller->channel[bchannel]);
  244. channel = &musb_channel->channel;
  245. csr = musb_readw(mbase,
  246. MUSB_HSDMA_CHANNEL_OFFSET(bchannel,
  247. MUSB_HSDMA_CONTROL));
  248. if (csr & (1 << MUSB_HSDMA_BUSERROR_SHIFT)) {
  249. musb_channel->channel.status =
  250. MUSB_DMA_STATUS_BUS_ABORT;
  251. } else {
  252. u8 devctl;
  253. addr = musb_read_hsdma_addr(mbase,
  254. bchannel);
  255. channel->actual_len = addr
  256. - musb_channel->start_addr;
  257. DBG(2, "ch %p, 0x%x -> 0x%x (%zu / %d) %s\n",
  258. channel, musb_channel->start_addr,
  259. addr, channel->actual_len,
  260. musb_channel->len,
  261. (channel->actual_len
  262. < musb_channel->len) ?
  263. "=> reconfig 0" : "=> complete");
  264. devctl = musb_readb(mbase, MUSB_DEVCTL);
  265. channel->status = MUSB_DMA_STATUS_FREE;
  266. /* completed */
  267. if ((devctl & MUSB_DEVCTL_HM)
  268. && (musb_channel->transmit)
  269. && ((channel->desired_mode == 0)
  270. || (channel->actual_len &
  271. (musb_channel->max_packet_sz - 1)))
  272. ) {
  273. u8 epnum = musb_channel->epnum;
  274. int offset = MUSB_EP_OFFSET(epnum,
  275. MUSB_TXCSR);
  276. u16 txcsr;
  277. /*
  278. * The programming guide says that we
  279. * must clear DMAENAB before DMAMODE.
  280. */
  281. musb_ep_select(mbase, epnum);
  282. txcsr = musb_readw(mbase, offset);
  283. txcsr &= ~(MUSB_TXCSR_DMAENAB
  284. | MUSB_TXCSR_AUTOSET);
  285. musb_writew(mbase, offset, txcsr);
  286. /* Send out the packet */
  287. txcsr &= ~MUSB_TXCSR_DMAMODE;
  288. txcsr |= MUSB_TXCSR_TXPKTRDY;
  289. musb_writew(mbase, offset, txcsr);
  290. }
  291. musb_dma_completion(musb, musb_channel->epnum,
  292. musb_channel->transmit);
  293. }
  294. }
  295. }
  296. retval = IRQ_HANDLED;
  297. done:
  298. spin_unlock_irqrestore(&musb->lock, flags);
  299. return retval;
  300. }
  301. void dma_controller_destroy(struct dma_controller *c)
  302. {
  303. struct musb_dma_controller *controller = container_of(c,
  304. struct musb_dma_controller, controller);
  305. if (!controller)
  306. return;
  307. if (controller->irq)
  308. free_irq(controller->irq, c);
  309. kfree(controller);
  310. }
  311. struct dma_controller *__init
  312. dma_controller_create(struct musb *musb, void __iomem *base)
  313. {
  314. struct musb_dma_controller *controller;
  315. struct device *dev = musb->controller;
  316. struct platform_device *pdev = to_platform_device(dev);
  317. int irq = platform_get_irq_byname(pdev, "dma");
  318. if (irq == 0) {
  319. dev_err(dev, "No DMA interrupt line!\n");
  320. return NULL;
  321. }
  322. controller = kzalloc(sizeof(*controller), GFP_KERNEL);
  323. if (!controller)
  324. return NULL;
  325. controller->channel_count = MUSB_HSDMA_CHANNELS;
  326. controller->private_data = musb;
  327. controller->base = base;
  328. controller->controller.start = dma_controller_start;
  329. controller->controller.stop = dma_controller_stop;
  330. controller->controller.channel_alloc = dma_channel_allocate;
  331. controller->controller.channel_release = dma_channel_release;
  332. controller->controller.channel_program = dma_channel_program;
  333. controller->controller.channel_abort = dma_channel_abort;
  334. if (request_irq(irq, dma_controller_irq, IRQF_DISABLED,
  335. dev_name(musb->controller), &controller->controller)) {
  336. dev_err(dev, "request_irq %d failed!\n", irq);
  337. dma_controller_destroy(&controller->controller);
  338. return NULL;
  339. }
  340. controller->irq = irq;
  341. return &controller->controller;
  342. }