musbhsdma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 "musb_core.h"
  37. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3430)
  38. #include "omap2430.h"
  39. #endif
  40. #define MUSB_HSDMA_BASE 0x200
  41. #define MUSB_HSDMA_INTR (MUSB_HSDMA_BASE + 0)
  42. #define MUSB_HSDMA_CONTROL 0x4
  43. #define MUSB_HSDMA_ADDRESS 0x8
  44. #define MUSB_HSDMA_COUNT 0xc
  45. #define MUSB_HSDMA_CHANNEL_OFFSET(_bchannel, _offset) \
  46. (MUSB_HSDMA_BASE + (_bchannel << 4) + _offset)
  47. /* control register (16-bit): */
  48. #define MUSB_HSDMA_ENABLE_SHIFT 0
  49. #define MUSB_HSDMA_TRANSMIT_SHIFT 1
  50. #define MUSB_HSDMA_MODE1_SHIFT 2
  51. #define MUSB_HSDMA_IRQENABLE_SHIFT 3
  52. #define MUSB_HSDMA_ENDPOINT_SHIFT 4
  53. #define MUSB_HSDMA_BUSERROR_SHIFT 8
  54. #define MUSB_HSDMA_BURSTMODE_SHIFT 9
  55. #define MUSB_HSDMA_BURSTMODE (3 << MUSB_HSDMA_BURSTMODE_SHIFT)
  56. #define MUSB_HSDMA_BURSTMODE_UNSPEC 0
  57. #define MUSB_HSDMA_BURSTMODE_INCR4 1
  58. #define MUSB_HSDMA_BURSTMODE_INCR8 2
  59. #define MUSB_HSDMA_BURSTMODE_INCR16 3
  60. #define MUSB_HSDMA_CHANNELS 8
  61. struct musb_dma_controller;
  62. struct musb_dma_channel {
  63. struct dma_channel channel;
  64. struct musb_dma_controller *controller;
  65. u32 start_addr;
  66. u32 len;
  67. u16 max_packet_sz;
  68. u8 idx;
  69. u8 epnum;
  70. u8 transmit;
  71. };
  72. struct musb_dma_controller {
  73. struct dma_controller controller;
  74. struct musb_dma_channel channel[MUSB_HSDMA_CHANNELS];
  75. void *private_data;
  76. void __iomem *base;
  77. u8 channel_count;
  78. u8 used_channels;
  79. u8 irq;
  80. };
  81. static int dma_controller_start(struct dma_controller *c)
  82. {
  83. /* nothing to do */
  84. return 0;
  85. }
  86. static void dma_channel_release(struct dma_channel *channel);
  87. static int dma_controller_stop(struct dma_controller *c)
  88. {
  89. struct musb_dma_controller *controller = container_of(c,
  90. struct musb_dma_controller, controller);
  91. struct musb *musb = controller->private_data;
  92. struct dma_channel *channel;
  93. u8 bit;
  94. if (controller->used_channels != 0) {
  95. dev_err(musb->controller,
  96. "Stopping DMA controller while channel active\n");
  97. for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) {
  98. if (controller->used_channels & (1 << bit)) {
  99. channel = &controller->channel[bit].channel;
  100. dma_channel_release(channel);
  101. if (!controller->used_channels)
  102. break;
  103. }
  104. }
  105. }
  106. return 0;
  107. }
  108. static struct dma_channel *dma_channel_allocate(struct dma_controller *c,
  109. struct musb_hw_ep *hw_ep, u8 transmit)
  110. {
  111. struct musb_dma_controller *controller = container_of(c,
  112. struct musb_dma_controller, controller);
  113. struct musb_dma_channel *musb_channel = NULL;
  114. struct dma_channel *channel = NULL;
  115. u8 bit;
  116. for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) {
  117. if (!(controller->used_channels & (1 << bit))) {
  118. controller->used_channels |= (1 << bit);
  119. musb_channel = &(controller->channel[bit]);
  120. musb_channel->controller = controller;
  121. musb_channel->idx = bit;
  122. musb_channel->epnum = hw_ep->epnum;
  123. musb_channel->transmit = transmit;
  124. channel = &(musb_channel->channel);
  125. channel->private_data = musb_channel;
  126. channel->status = MUSB_DMA_STATUS_FREE;
  127. channel->max_len = 0x10000;
  128. /* Tx => mode 1; Rx => mode 0 */
  129. channel->desired_mode = transmit;
  130. channel->actual_len = 0;
  131. break;
  132. }
  133. }
  134. return channel;
  135. }
  136. static void dma_channel_release(struct dma_channel *channel)
  137. {
  138. struct musb_dma_channel *musb_channel = channel->private_data;
  139. channel->actual_len = 0;
  140. musb_channel->start_addr = 0;
  141. musb_channel->len = 0;
  142. musb_channel->controller->used_channels &=
  143. ~(1 << musb_channel->idx);
  144. channel->status = MUSB_DMA_STATUS_UNKNOWN;
  145. }
  146. static void configure_channel(struct dma_channel *channel,
  147. u16 packet_sz, u8 mode,
  148. dma_addr_t dma_addr, u32 len)
  149. {
  150. struct musb_dma_channel *musb_channel = channel->private_data;
  151. struct musb_dma_controller *controller = musb_channel->controller;
  152. void __iomem *mbase = controller->base;
  153. u8 bchannel = musb_channel->idx;
  154. u16 csr = 0;
  155. DBG(4, "%p, pkt_sz %d, addr 0x%x, len %d, mode %d\n",
  156. channel, packet_sz, dma_addr, len, mode);
  157. if (mode) {
  158. csr |= 1 << MUSB_HSDMA_MODE1_SHIFT;
  159. BUG_ON(len < packet_sz);
  160. if (packet_sz >= 64) {
  161. csr |= MUSB_HSDMA_BURSTMODE_INCR16
  162. << MUSB_HSDMA_BURSTMODE_SHIFT;
  163. } else if (packet_sz >= 32) {
  164. csr |= MUSB_HSDMA_BURSTMODE_INCR8
  165. << MUSB_HSDMA_BURSTMODE_SHIFT;
  166. } else if (packet_sz >= 16) {
  167. csr |= MUSB_HSDMA_BURSTMODE_INCR4
  168. << MUSB_HSDMA_BURSTMODE_SHIFT;
  169. }
  170. }
  171. csr |= (musb_channel->epnum << MUSB_HSDMA_ENDPOINT_SHIFT)
  172. | (1 << MUSB_HSDMA_ENABLE_SHIFT)
  173. | (1 << MUSB_HSDMA_IRQENABLE_SHIFT)
  174. | (musb_channel->transmit
  175. ? (1 << MUSB_HSDMA_TRANSMIT_SHIFT)
  176. : 0);
  177. /* address/count */
  178. musb_writel(mbase,
  179. MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_ADDRESS),
  180. dma_addr);
  181. musb_writel(mbase,
  182. MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_COUNT),
  183. len);
  184. /* control (this should start things) */
  185. musb_writew(mbase,
  186. MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL),
  187. csr);
  188. }
  189. static int dma_channel_program(struct dma_channel *channel,
  190. u16 packet_sz, u8 mode,
  191. dma_addr_t dma_addr, u32 len)
  192. {
  193. struct musb_dma_channel *musb_channel = channel->private_data;
  194. DBG(2, "ep%d-%s pkt_sz %d, dma_addr 0x%x length %d, mode %d\n",
  195. musb_channel->epnum,
  196. musb_channel->transmit ? "Tx" : "Rx",
  197. packet_sz, dma_addr, len, mode);
  198. BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
  199. channel->status == MUSB_DMA_STATUS_BUSY);
  200. channel->actual_len = 0;
  201. musb_channel->start_addr = dma_addr;
  202. musb_channel->len = len;
  203. musb_channel->max_packet_sz = packet_sz;
  204. channel->status = MUSB_DMA_STATUS_BUSY;
  205. if ((mode == 1) && (len >= packet_sz))
  206. configure_channel(channel, packet_sz, 1, dma_addr, len);
  207. else
  208. configure_channel(channel, packet_sz, 0, dma_addr, len);
  209. return true;
  210. }
  211. static int dma_channel_abort(struct dma_channel *channel)
  212. {
  213. struct musb_dma_channel *musb_channel = channel->private_data;
  214. void __iomem *mbase = musb_channel->controller->base;
  215. u8 bchannel = musb_channel->idx;
  216. u16 csr;
  217. if (channel->status == MUSB_DMA_STATUS_BUSY) {
  218. if (musb_channel->transmit) {
  219. csr = musb_readw(mbase,
  220. MUSB_EP_OFFSET(musb_channel->epnum,
  221. MUSB_TXCSR));
  222. csr &= ~(MUSB_TXCSR_AUTOSET |
  223. MUSB_TXCSR_DMAENAB |
  224. MUSB_TXCSR_DMAMODE);
  225. musb_writew(mbase,
  226. MUSB_EP_OFFSET(musb_channel->epnum, MUSB_TXCSR),
  227. csr);
  228. } else {
  229. csr = musb_readw(mbase,
  230. MUSB_EP_OFFSET(musb_channel->epnum,
  231. MUSB_RXCSR));
  232. csr &= ~(MUSB_RXCSR_AUTOCLEAR |
  233. MUSB_RXCSR_DMAENAB |
  234. MUSB_RXCSR_DMAMODE);
  235. musb_writew(mbase,
  236. MUSB_EP_OFFSET(musb_channel->epnum, MUSB_RXCSR),
  237. csr);
  238. }
  239. musb_writew(mbase,
  240. MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL),
  241. 0);
  242. musb_writel(mbase,
  243. MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_ADDRESS),
  244. 0);
  245. musb_writel(mbase,
  246. MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_COUNT),
  247. 0);
  248. channel->status = MUSB_DMA_STATUS_FREE;
  249. }
  250. return 0;
  251. }
  252. static irqreturn_t dma_controller_irq(int irq, void *private_data)
  253. {
  254. struct musb_dma_controller *controller = private_data;
  255. struct musb *musb = controller->private_data;
  256. struct musb_dma_channel *musb_channel;
  257. struct dma_channel *channel;
  258. void __iomem *mbase = controller->base;
  259. irqreturn_t retval = IRQ_NONE;
  260. unsigned long flags;
  261. u8 bchannel;
  262. u8 int_hsdma;
  263. u32 addr;
  264. u16 csr;
  265. spin_lock_irqsave(&musb->lock, flags);
  266. int_hsdma = musb_readb(mbase, MUSB_HSDMA_INTR);
  267. if (!int_hsdma)
  268. goto done;
  269. for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
  270. if (int_hsdma & (1 << bchannel)) {
  271. musb_channel = (struct musb_dma_channel *)
  272. &(controller->channel[bchannel]);
  273. channel = &musb_channel->channel;
  274. csr = musb_readw(mbase,
  275. MUSB_HSDMA_CHANNEL_OFFSET(bchannel,
  276. MUSB_HSDMA_CONTROL));
  277. if (csr & (1 << MUSB_HSDMA_BUSERROR_SHIFT)) {
  278. musb_channel->channel.status =
  279. MUSB_DMA_STATUS_BUS_ABORT;
  280. } else {
  281. u8 devctl;
  282. addr = musb_readl(mbase,
  283. MUSB_HSDMA_CHANNEL_OFFSET(
  284. bchannel,
  285. MUSB_HSDMA_ADDRESS));
  286. channel->actual_len = addr
  287. - musb_channel->start_addr;
  288. DBG(2, "ch %p, 0x%x -> 0x%x (%d / %d) %s\n",
  289. channel, musb_channel->start_addr,
  290. addr, channel->actual_len,
  291. musb_channel->len,
  292. (channel->actual_len
  293. < musb_channel->len) ?
  294. "=> reconfig 0" : "=> complete");
  295. devctl = musb_readb(mbase, MUSB_DEVCTL);
  296. channel->status = MUSB_DMA_STATUS_FREE;
  297. /* completed */
  298. if ((devctl & MUSB_DEVCTL_HM)
  299. && (musb_channel->transmit)
  300. && ((channel->desired_mode == 0)
  301. || (channel->actual_len &
  302. (musb_channel->max_packet_sz - 1)))
  303. ) {
  304. /* Send out the packet */
  305. musb_ep_select(mbase,
  306. musb_channel->epnum);
  307. musb_writew(mbase, MUSB_EP_OFFSET(
  308. musb_channel->epnum,
  309. MUSB_TXCSR),
  310. MUSB_TXCSR_TXPKTRDY);
  311. } else {
  312. musb_dma_completion(
  313. musb,
  314. musb_channel->epnum,
  315. musb_channel->transmit);
  316. }
  317. }
  318. }
  319. }
  320. retval = IRQ_HANDLED;
  321. done:
  322. spin_unlock_irqrestore(&musb->lock, flags);
  323. return retval;
  324. }
  325. void dma_controller_destroy(struct dma_controller *c)
  326. {
  327. struct musb_dma_controller *controller = container_of(c,
  328. struct musb_dma_controller, controller);
  329. if (!controller)
  330. return;
  331. if (controller->irq)
  332. free_irq(controller->irq, c);
  333. kfree(controller);
  334. }
  335. struct dma_controller *__init
  336. dma_controller_create(struct musb *musb, void __iomem *base)
  337. {
  338. struct musb_dma_controller *controller;
  339. struct device *dev = musb->controller;
  340. struct platform_device *pdev = to_platform_device(dev);
  341. int irq = platform_get_irq(pdev, 1);
  342. if (irq == 0) {
  343. dev_err(dev, "No DMA interrupt line!\n");
  344. return NULL;
  345. }
  346. controller = kzalloc(sizeof(*controller), GFP_KERNEL);
  347. if (!controller)
  348. return NULL;
  349. controller->channel_count = MUSB_HSDMA_CHANNELS;
  350. controller->private_data = musb;
  351. controller->base = base;
  352. controller->controller.start = dma_controller_start;
  353. controller->controller.stop = dma_controller_stop;
  354. controller->controller.channel_alloc = dma_channel_allocate;
  355. controller->controller.channel_release = dma_channel_release;
  356. controller->controller.channel_program = dma_channel_program;
  357. controller->controller.channel_abort = dma_channel_abort;
  358. if (request_irq(irq, dma_controller_irq, IRQF_DISABLED,
  359. musb->controller->bus_id, &controller->controller)) {
  360. dev_err(dev, "request_irq %d failed!\n", irq);
  361. dma_controller_destroy(&controller->controller);
  362. return NULL;
  363. }
  364. controller->irq = irq;
  365. return &controller->controller;
  366. }