dma-m2p.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * arch/arm/mach-ep93xx/dma-m2p.c
  3. * M2P DMA handling for Cirrus EP93xx chips.
  4. *
  5. * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
  6. * Copyright (C) 2006 Applied Data Systems
  7. *
  8. * Copyright (C) 2009 Ryan Mallon <ryan@bluewatersys.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. */
  15. /*
  16. * On the EP93xx chip the following peripherals my be allocated to the 10
  17. * Memory to Internal Peripheral (M2P) channels (5 transmit + 5 receive).
  18. *
  19. * I2S contains 3 Tx and 3 Rx DMA Channels
  20. * AAC contains 3 Tx and 3 Rx DMA Channels
  21. * UART1 contains 1 Tx and 1 Rx DMA Channels
  22. * UART2 contains 1 Tx and 1 Rx DMA Channels
  23. * UART3 contains 1 Tx and 1 Rx DMA Channels
  24. * IrDA contains 1 Tx and 1 Rx DMA Channels
  25. *
  26. * SSP and IDE use the Memory to Memory (M2M) channels and are not covered
  27. * with this implementation.
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/clk.h>
  31. #include <linux/err.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/module.h>
  34. #include <mach/dma.h>
  35. #include <mach/hardware.h>
  36. #define M2P_CONTROL 0x00
  37. #define M2P_CONTROL_STALL_IRQ_EN (1 << 0)
  38. #define M2P_CONTROL_NFB_IRQ_EN (1 << 1)
  39. #define M2P_CONTROL_ERROR_IRQ_EN (1 << 3)
  40. #define M2P_CONTROL_ENABLE (1 << 4)
  41. #define M2P_INTERRUPT 0x04
  42. #define M2P_INTERRUPT_STALL (1 << 0)
  43. #define M2P_INTERRUPT_NFB (1 << 1)
  44. #define M2P_INTERRUPT_ERROR (1 << 3)
  45. #define M2P_PPALLOC 0x08
  46. #define M2P_STATUS 0x0c
  47. #define M2P_REMAIN 0x14
  48. #define M2P_MAXCNT0 0x20
  49. #define M2P_BASE0 0x24
  50. #define M2P_MAXCNT1 0x30
  51. #define M2P_BASE1 0x34
  52. #define STATE_IDLE 0 /* Channel is inactive. */
  53. #define STATE_STALL 1 /* Channel is active, no buffers pending. */
  54. #define STATE_ON 2 /* Channel is active, one buffer pending. */
  55. #define STATE_NEXT 3 /* Channel is active, two buffers pending. */
  56. struct m2p_channel {
  57. char *name;
  58. void __iomem *base;
  59. int irq;
  60. struct clk *clk;
  61. spinlock_t lock;
  62. void *client;
  63. unsigned next_slot:1;
  64. struct ep93xx_dma_buffer *buffer_xfer;
  65. struct ep93xx_dma_buffer *buffer_next;
  66. struct list_head buffers_pending;
  67. };
  68. static struct m2p_channel m2p_rx[] = {
  69. {"m2p1", EP93XX_DMA_BASE + 0x0040, IRQ_EP93XX_DMAM2P1},
  70. {"m2p3", EP93XX_DMA_BASE + 0x00c0, IRQ_EP93XX_DMAM2P3},
  71. {"m2p5", EP93XX_DMA_BASE + 0x0200, IRQ_EP93XX_DMAM2P5},
  72. {"m2p7", EP93XX_DMA_BASE + 0x0280, IRQ_EP93XX_DMAM2P7},
  73. {"m2p9", EP93XX_DMA_BASE + 0x0300, IRQ_EP93XX_DMAM2P9},
  74. {NULL},
  75. };
  76. static struct m2p_channel m2p_tx[] = {
  77. {"m2p0", EP93XX_DMA_BASE + 0x0000, IRQ_EP93XX_DMAM2P0},
  78. {"m2p2", EP93XX_DMA_BASE + 0x0080, IRQ_EP93XX_DMAM2P2},
  79. {"m2p4", EP93XX_DMA_BASE + 0x0240, IRQ_EP93XX_DMAM2P4},
  80. {"m2p6", EP93XX_DMA_BASE + 0x02c0, IRQ_EP93XX_DMAM2P6},
  81. {"m2p8", EP93XX_DMA_BASE + 0x0340, IRQ_EP93XX_DMAM2P8},
  82. {NULL},
  83. };
  84. static void feed_buf(struct m2p_channel *ch, struct ep93xx_dma_buffer *buf)
  85. {
  86. if (ch->next_slot == 0) {
  87. writel(buf->size, ch->base + M2P_MAXCNT0);
  88. writel(buf->bus_addr, ch->base + M2P_BASE0);
  89. } else {
  90. writel(buf->size, ch->base + M2P_MAXCNT1);
  91. writel(buf->bus_addr, ch->base + M2P_BASE1);
  92. }
  93. ch->next_slot ^= 1;
  94. }
  95. static void choose_buffer_xfer(struct m2p_channel *ch)
  96. {
  97. struct ep93xx_dma_buffer *buf;
  98. ch->buffer_xfer = NULL;
  99. if (!list_empty(&ch->buffers_pending)) {
  100. buf = list_entry(ch->buffers_pending.next,
  101. struct ep93xx_dma_buffer, list);
  102. list_del(&buf->list);
  103. feed_buf(ch, buf);
  104. ch->buffer_xfer = buf;
  105. }
  106. }
  107. static void choose_buffer_next(struct m2p_channel *ch)
  108. {
  109. struct ep93xx_dma_buffer *buf;
  110. ch->buffer_next = NULL;
  111. if (!list_empty(&ch->buffers_pending)) {
  112. buf = list_entry(ch->buffers_pending.next,
  113. struct ep93xx_dma_buffer, list);
  114. list_del(&buf->list);
  115. feed_buf(ch, buf);
  116. ch->buffer_next = buf;
  117. }
  118. }
  119. static inline void m2p_set_control(struct m2p_channel *ch, u32 v)
  120. {
  121. /*
  122. * The control register must be read immediately after being written so
  123. * that the internal state machine is correctly updated. See the ep93xx
  124. * users' guide for details.
  125. */
  126. writel(v, ch->base + M2P_CONTROL);
  127. readl(ch->base + M2P_CONTROL);
  128. }
  129. static inline int m2p_channel_state(struct m2p_channel *ch)
  130. {
  131. return (readl(ch->base + M2P_STATUS) >> 4) & 0x3;
  132. }
  133. static irqreturn_t m2p_irq(int irq, void *dev_id)
  134. {
  135. struct m2p_channel *ch = dev_id;
  136. struct ep93xx_dma_m2p_client *cl;
  137. u32 irq_status, v;
  138. int error = 0;
  139. cl = ch->client;
  140. spin_lock(&ch->lock);
  141. irq_status = readl(ch->base + M2P_INTERRUPT);
  142. if (irq_status & M2P_INTERRUPT_ERROR) {
  143. writel(M2P_INTERRUPT_ERROR, ch->base + M2P_INTERRUPT);
  144. error = 1;
  145. }
  146. if ((irq_status & (M2P_INTERRUPT_STALL | M2P_INTERRUPT_NFB)) == 0) {
  147. spin_unlock(&ch->lock);
  148. return IRQ_NONE;
  149. }
  150. switch (m2p_channel_state(ch)) {
  151. case STATE_IDLE:
  152. pr_crit("m2p_irq: dma interrupt without a dma buffer\n");
  153. BUG();
  154. break;
  155. case STATE_STALL:
  156. cl->buffer_finished(cl->cookie, ch->buffer_xfer, 0, error);
  157. if (ch->buffer_next != NULL) {
  158. cl->buffer_finished(cl->cookie, ch->buffer_next,
  159. 0, error);
  160. }
  161. choose_buffer_xfer(ch);
  162. choose_buffer_next(ch);
  163. if (ch->buffer_xfer != NULL)
  164. cl->buffer_started(cl->cookie, ch->buffer_xfer);
  165. break;
  166. case STATE_ON:
  167. cl->buffer_finished(cl->cookie, ch->buffer_xfer, 0, error);
  168. ch->buffer_xfer = ch->buffer_next;
  169. choose_buffer_next(ch);
  170. cl->buffer_started(cl->cookie, ch->buffer_xfer);
  171. break;
  172. case STATE_NEXT:
  173. pr_crit("m2p_irq: dma interrupt while next\n");
  174. BUG();
  175. break;
  176. }
  177. v = readl(ch->base + M2P_CONTROL) & ~(M2P_CONTROL_STALL_IRQ_EN |
  178. M2P_CONTROL_NFB_IRQ_EN);
  179. if (ch->buffer_xfer != NULL)
  180. v |= M2P_CONTROL_STALL_IRQ_EN;
  181. if (ch->buffer_next != NULL)
  182. v |= M2P_CONTROL_NFB_IRQ_EN;
  183. m2p_set_control(ch, v);
  184. spin_unlock(&ch->lock);
  185. return IRQ_HANDLED;
  186. }
  187. static struct m2p_channel *find_free_channel(struct ep93xx_dma_m2p_client *cl)
  188. {
  189. struct m2p_channel *ch;
  190. int i;
  191. if (cl->flags & EP93XX_DMA_M2P_RX)
  192. ch = m2p_rx;
  193. else
  194. ch = m2p_tx;
  195. for (i = 0; ch[i].base; i++) {
  196. struct ep93xx_dma_m2p_client *client;
  197. client = ch[i].client;
  198. if (client != NULL) {
  199. int port;
  200. port = cl->flags & EP93XX_DMA_M2P_PORT_MASK;
  201. if (port == (client->flags &
  202. EP93XX_DMA_M2P_PORT_MASK)) {
  203. pr_warning("DMA channel already used by %s\n",
  204. cl->name ? : "unknown client");
  205. return ERR_PTR(-EBUSY);
  206. }
  207. }
  208. }
  209. for (i = 0; ch[i].base; i++) {
  210. if (ch[i].client == NULL)
  211. return ch + i;
  212. }
  213. pr_warning("No free DMA channel for %s\n",
  214. cl->name ? : "unknown client");
  215. return ERR_PTR(-ENODEV);
  216. }
  217. static void channel_enable(struct m2p_channel *ch)
  218. {
  219. struct ep93xx_dma_m2p_client *cl = ch->client;
  220. u32 v;
  221. clk_enable(ch->clk);
  222. v = cl->flags & EP93XX_DMA_M2P_PORT_MASK;
  223. writel(v, ch->base + M2P_PPALLOC);
  224. v = cl->flags & EP93XX_DMA_M2P_ERROR_MASK;
  225. v |= M2P_CONTROL_ENABLE | M2P_CONTROL_ERROR_IRQ_EN;
  226. m2p_set_control(ch, v);
  227. }
  228. static void channel_disable(struct m2p_channel *ch)
  229. {
  230. u32 v;
  231. v = readl(ch->base + M2P_CONTROL);
  232. v &= ~(M2P_CONTROL_STALL_IRQ_EN | M2P_CONTROL_NFB_IRQ_EN);
  233. m2p_set_control(ch, v);
  234. while (m2p_channel_state(ch) == STATE_ON)
  235. cpu_relax();
  236. m2p_set_control(ch, 0x0);
  237. while (m2p_channel_state(ch) == STATE_STALL)
  238. cpu_relax();
  239. clk_disable(ch->clk);
  240. }
  241. int ep93xx_dma_m2p_client_register(struct ep93xx_dma_m2p_client *cl)
  242. {
  243. struct m2p_channel *ch;
  244. int err;
  245. ch = find_free_channel(cl);
  246. if (IS_ERR(ch))
  247. return PTR_ERR(ch);
  248. err = request_irq(ch->irq, m2p_irq, 0, cl->name ? : "dma-m2p", ch);
  249. if (err)
  250. return err;
  251. ch->client = cl;
  252. ch->next_slot = 0;
  253. ch->buffer_xfer = NULL;
  254. ch->buffer_next = NULL;
  255. INIT_LIST_HEAD(&ch->buffers_pending);
  256. cl->channel = ch;
  257. channel_enable(ch);
  258. return 0;
  259. }
  260. EXPORT_SYMBOL_GPL(ep93xx_dma_m2p_client_register);
  261. void ep93xx_dma_m2p_client_unregister(struct ep93xx_dma_m2p_client *cl)
  262. {
  263. struct m2p_channel *ch = cl->channel;
  264. channel_disable(ch);
  265. free_irq(ch->irq, ch);
  266. ch->client = NULL;
  267. }
  268. EXPORT_SYMBOL_GPL(ep93xx_dma_m2p_client_unregister);
  269. void ep93xx_dma_m2p_submit(struct ep93xx_dma_m2p_client *cl,
  270. struct ep93xx_dma_buffer *buf)
  271. {
  272. struct m2p_channel *ch = cl->channel;
  273. unsigned long flags;
  274. u32 v;
  275. spin_lock_irqsave(&ch->lock, flags);
  276. v = readl(ch->base + M2P_CONTROL);
  277. if (ch->buffer_xfer == NULL) {
  278. ch->buffer_xfer = buf;
  279. feed_buf(ch, buf);
  280. cl->buffer_started(cl->cookie, buf);
  281. v |= M2P_CONTROL_STALL_IRQ_EN;
  282. m2p_set_control(ch, v);
  283. } else if (ch->buffer_next == NULL) {
  284. ch->buffer_next = buf;
  285. feed_buf(ch, buf);
  286. v |= M2P_CONTROL_NFB_IRQ_EN;
  287. m2p_set_control(ch, v);
  288. } else {
  289. list_add_tail(&buf->list, &ch->buffers_pending);
  290. }
  291. spin_unlock_irqrestore(&ch->lock, flags);
  292. }
  293. EXPORT_SYMBOL_GPL(ep93xx_dma_m2p_submit);
  294. void ep93xx_dma_m2p_submit_recursive(struct ep93xx_dma_m2p_client *cl,
  295. struct ep93xx_dma_buffer *buf)
  296. {
  297. struct m2p_channel *ch = cl->channel;
  298. list_add_tail(&buf->list, &ch->buffers_pending);
  299. }
  300. EXPORT_SYMBOL_GPL(ep93xx_dma_m2p_submit_recursive);
  301. void ep93xx_dma_m2p_flush(struct ep93xx_dma_m2p_client *cl)
  302. {
  303. struct m2p_channel *ch = cl->channel;
  304. channel_disable(ch);
  305. ch->next_slot = 0;
  306. ch->buffer_xfer = NULL;
  307. ch->buffer_next = NULL;
  308. INIT_LIST_HEAD(&ch->buffers_pending);
  309. channel_enable(ch);
  310. }
  311. EXPORT_SYMBOL_GPL(ep93xx_dma_m2p_flush);
  312. static int init_channel(struct m2p_channel *ch)
  313. {
  314. ch->clk = clk_get(NULL, ch->name);
  315. if (IS_ERR(ch->clk))
  316. return PTR_ERR(ch->clk);
  317. spin_lock_init(&ch->lock);
  318. ch->client = NULL;
  319. return 0;
  320. }
  321. static int __init ep93xx_dma_m2p_init(void)
  322. {
  323. int i;
  324. int ret;
  325. for (i = 0; m2p_rx[i].base; i++) {
  326. ret = init_channel(m2p_rx + i);
  327. if (ret)
  328. return ret;
  329. }
  330. for (i = 0; m2p_tx[i].base; i++) {
  331. ret = init_channel(m2p_tx + i);
  332. if (ret)
  333. return ret;
  334. }
  335. pr_info("M2P DMA subsystem initialized\n");
  336. return 0;
  337. }
  338. arch_initcall(ep93xx_dma_m2p_init);