1fc467a26SManuel Lauss /* 2fc467a26SManuel Lauss * SH7760 DMABRG IRQ handling 3fc467a26SManuel Lauss * 4fc467a26SManuel Lauss * (c) 2007 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com> 5fc467a26SManuel Lauss * licensed under the GPLv2. 6fc467a26SManuel Lauss * 7fc467a26SManuel Lauss */ 8fc467a26SManuel Lauss 9fc467a26SManuel Lauss #include <linux/interrupt.h> 10fc467a26SManuel Lauss #include <linux/kernel.h> 11*5a0e3ad6STejun Heo #include <linux/slab.h> 12fc467a26SManuel Lauss #include <asm/dma.h> 13fc467a26SManuel Lauss #include <asm/dmabrg.h> 14fc467a26SManuel Lauss #include <asm/io.h> 15fc467a26SManuel Lauss 16fc467a26SManuel Lauss /* 17fc467a26SManuel Lauss * The DMABRG is a special DMA unit within the SH7760. It does transfers 18fc467a26SManuel Lauss * from USB-SRAM/Audio units to main memory (and also the LCDC; but that 19fc467a26SManuel Lauss * part is sensibly placed in the LCDC registers and requires no irqs) 20fc467a26SManuel Lauss * It has 3 IRQ lines which trigger 10 events, and works independently 21fc467a26SManuel Lauss * from the traditional SH DMAC (although it blocks usage of DMAC 0) 22fc467a26SManuel Lauss * 23fc467a26SManuel Lauss * BRGIRQID | component | dir | meaning | source 24fc467a26SManuel Lauss * ----------------------------------------------------- 25fc467a26SManuel Lauss * 0 | USB-DMA | ... | xfer done | DMABRGI1 26fc467a26SManuel Lauss * 1 | USB-UAE | ... | USB addr err.| DMABRGI0 27fc467a26SManuel Lauss * 2 | HAC0/SSI0 | play| all done | DMABRGI1 28fc467a26SManuel Lauss * 3 | HAC0/SSI0 | play| half done | DMABRGI2 29fc467a26SManuel Lauss * 4 | HAC0/SSI0 | rec | all done | DMABRGI1 30fc467a26SManuel Lauss * 5 | HAC0/SSI0 | rec | half done | DMABRGI2 31fc467a26SManuel Lauss * 6 | HAC1/SSI1 | play| all done | DMABRGI1 32fc467a26SManuel Lauss * 7 | HAC1/SSI1 | play| half done | DMABRGI2 33fc467a26SManuel Lauss * 8 | HAC1/SSI1 | rec | all done | DMABRGI1 34fc467a26SManuel Lauss * 9 | HAC1/SSI1 | rec | half done | DMABRGI2 35fc467a26SManuel Lauss * 36fc467a26SManuel Lauss * all can be enabled/disabled in the DMABRGCR register, 37e868d612SSimon Arlott * as well as checked if they occurred. 38fc467a26SManuel Lauss * 39fc467a26SManuel Lauss * DMABRGI0 services USB DMA Address errors, but it still must be 40fc467a26SManuel Lauss * enabled/acked in the DMABRGCR register. USB-DMA complete indicator 41fc467a26SManuel Lauss * is grouped together with the audio buffer end indicators, too bad... 42fc467a26SManuel Lauss * 43fc467a26SManuel Lauss * DMABRGCR: Bits 31-24: audio-dma ENABLE flags, 44fc467a26SManuel Lauss * Bits 23-16: audio-dma STATUS flags, 45fc467a26SManuel Lauss * Bits 9-8: USB error/xfer ENABLE, 46fc467a26SManuel Lauss * Bits 1-0: USB error/xfer STATUS. 47fc467a26SManuel Lauss * Ack an IRQ by writing 0 to the STATUS flag. 48fc467a26SManuel Lauss * Mask IRQ by writing 0 to ENABLE flag. 49fc467a26SManuel Lauss * 50fc467a26SManuel Lauss * Usage is almost like with any other IRQ: 51fc467a26SManuel Lauss * dmabrg_request_irq(BRGIRQID, handler, data) 52fc467a26SManuel Lauss * dmabrg_free_irq(BRGIRQID) 53fc467a26SManuel Lauss * 54fc467a26SManuel Lauss * handler prototype: void brgirqhandler(void *data) 55fc467a26SManuel Lauss */ 56fc467a26SManuel Lauss 57fc467a26SManuel Lauss #define DMARSRA 0xfe090000 58fc467a26SManuel Lauss #define DMAOR 0xffa00040 59fc467a26SManuel Lauss #define DMACHCR0 0xffa0000c 60fc467a26SManuel Lauss #define DMABRGCR 0xfe3c0000 61fc467a26SManuel Lauss 62fc467a26SManuel Lauss #define DMAOR_BRG 0x0000c000 63fc467a26SManuel Lauss #define DMAOR_DMEN 0x00000001 64fc467a26SManuel Lauss 65fc467a26SManuel Lauss #define DMABRGI0 68 66fc467a26SManuel Lauss #define DMABRGI1 69 67fc467a26SManuel Lauss #define DMABRGI2 70 68fc467a26SManuel Lauss 69fc467a26SManuel Lauss struct dmabrg_handler { 70fc467a26SManuel Lauss void (*handler)(void *); 71fc467a26SManuel Lauss void *data; 72fc467a26SManuel Lauss } *dmabrg_handlers; 73fc467a26SManuel Lauss 74fc467a26SManuel Lauss static inline void dmabrg_call_handler(int i) 75fc467a26SManuel Lauss { 76fc467a26SManuel Lauss dmabrg_handlers[i].handler(dmabrg_handlers[i].data); 77fc467a26SManuel Lauss } 78fc467a26SManuel Lauss 79fc467a26SManuel Lauss /* 80fc467a26SManuel Lauss * main DMABRG irq handler. It acks irqs and then 81fc467a26SManuel Lauss * handles every set and unmasked bit sequentially. 82fc467a26SManuel Lauss * No locking and no validity checks; it should be 83fc467a26SManuel Lauss * as fast as possible (audio!) 84fc467a26SManuel Lauss */ 85fc467a26SManuel Lauss static irqreturn_t dmabrg_irq(int irq, void *data) 86fc467a26SManuel Lauss { 87fc467a26SManuel Lauss unsigned long dcr; 88fc467a26SManuel Lauss unsigned int i; 89fc467a26SManuel Lauss 909d56dd3bSPaul Mundt dcr = __raw_readl(DMABRGCR); 919d56dd3bSPaul Mundt __raw_writel(dcr & ~0x00ff0003, DMABRGCR); /* ack all */ 92fc467a26SManuel Lauss dcr &= dcr >> 8; /* ignore masked */ 93fc467a26SManuel Lauss 94fc467a26SManuel Lauss /* USB stuff, get it out of the way first */ 95fc467a26SManuel Lauss if (dcr & 1) 96fc467a26SManuel Lauss dmabrg_call_handler(DMABRGIRQ_USBDMA); 97fc467a26SManuel Lauss if (dcr & 2) 98fc467a26SManuel Lauss dmabrg_call_handler(DMABRGIRQ_USBDMAERR); 99fc467a26SManuel Lauss 100fc467a26SManuel Lauss /* Audio */ 101fc467a26SManuel Lauss dcr >>= 16; 102fc467a26SManuel Lauss while (dcr) { 103fc467a26SManuel Lauss i = __ffs(dcr); 104fc467a26SManuel Lauss dcr &= dcr - 1; 105fc467a26SManuel Lauss dmabrg_call_handler(i + DMABRGIRQ_A0TXF); 106fc467a26SManuel Lauss } 107fc467a26SManuel Lauss return IRQ_HANDLED; 108fc467a26SManuel Lauss } 109fc467a26SManuel Lauss 110fc467a26SManuel Lauss static void dmabrg_disable_irq(unsigned int dmairq) 111fc467a26SManuel Lauss { 112fc467a26SManuel Lauss unsigned long dcr; 1139d56dd3bSPaul Mundt dcr = __raw_readl(DMABRGCR); 114fc467a26SManuel Lauss dcr &= ~(1 << ((dmairq > 1) ? dmairq + 22 : dmairq + 8)); 1159d56dd3bSPaul Mundt __raw_writel(dcr, DMABRGCR); 116fc467a26SManuel Lauss } 117fc467a26SManuel Lauss 118fc467a26SManuel Lauss static void dmabrg_enable_irq(unsigned int dmairq) 119fc467a26SManuel Lauss { 120fc467a26SManuel Lauss unsigned long dcr; 1219d56dd3bSPaul Mundt dcr = __raw_readl(DMABRGCR); 122fc467a26SManuel Lauss dcr |= (1 << ((dmairq > 1) ? dmairq + 22 : dmairq + 8)); 1239d56dd3bSPaul Mundt __raw_writel(dcr, DMABRGCR); 124fc467a26SManuel Lauss } 125fc467a26SManuel Lauss 126fc467a26SManuel Lauss int dmabrg_request_irq(unsigned int dmairq, void(*handler)(void*), 127fc467a26SManuel Lauss void *data) 128fc467a26SManuel Lauss { 129fc467a26SManuel Lauss if ((dmairq > 9) || !handler) 130fc467a26SManuel Lauss return -ENOENT; 131fc467a26SManuel Lauss if (dmabrg_handlers[dmairq].handler) 132fc467a26SManuel Lauss return -EBUSY; 133fc467a26SManuel Lauss 134fc467a26SManuel Lauss dmabrg_handlers[dmairq].handler = handler; 135fc467a26SManuel Lauss dmabrg_handlers[dmairq].data = data; 136fc467a26SManuel Lauss 137fc467a26SManuel Lauss dmabrg_enable_irq(dmairq); 138fc467a26SManuel Lauss return 0; 139fc467a26SManuel Lauss } 140fc467a26SManuel Lauss EXPORT_SYMBOL_GPL(dmabrg_request_irq); 141fc467a26SManuel Lauss 142fc467a26SManuel Lauss void dmabrg_free_irq(unsigned int dmairq) 143fc467a26SManuel Lauss { 144fc467a26SManuel Lauss if (likely(dmairq < 10)) { 145fc467a26SManuel Lauss dmabrg_disable_irq(dmairq); 146fc467a26SManuel Lauss dmabrg_handlers[dmairq].handler = NULL; 147fc467a26SManuel Lauss dmabrg_handlers[dmairq].data = NULL; 148fc467a26SManuel Lauss } 149fc467a26SManuel Lauss } 150fc467a26SManuel Lauss EXPORT_SYMBOL_GPL(dmabrg_free_irq); 151fc467a26SManuel Lauss 152fc467a26SManuel Lauss static int __init dmabrg_init(void) 153fc467a26SManuel Lauss { 154fc467a26SManuel Lauss unsigned long or; 155fc467a26SManuel Lauss int ret; 156fc467a26SManuel Lauss 157fc467a26SManuel Lauss dmabrg_handlers = kzalloc(10 * sizeof(struct dmabrg_handler), 158fc467a26SManuel Lauss GFP_KERNEL); 159fc467a26SManuel Lauss if (!dmabrg_handlers) 160fc467a26SManuel Lauss return -ENOMEM; 161fc467a26SManuel Lauss 162fc467a26SManuel Lauss #ifdef CONFIG_SH_DMA 163fc467a26SManuel Lauss /* request DMAC channel 0 before anyone else can get it */ 164fc467a26SManuel Lauss ret = request_dma(0, "DMAC 0 (DMABRG)"); 165fc467a26SManuel Lauss if (ret < 0) 166fc467a26SManuel Lauss printk(KERN_INFO "DMABRG: DMAC ch0 not reserved!\n"); 167fc467a26SManuel Lauss #endif 168fc467a26SManuel Lauss 1699d56dd3bSPaul Mundt __raw_writel(0, DMABRGCR); 1709d56dd3bSPaul Mundt __raw_writel(0, DMACHCR0); 1719d56dd3bSPaul Mundt __raw_writel(0x94000000, DMARSRA); /* enable DMABRG in DMAC 0 */ 172fc467a26SManuel Lauss 173fc467a26SManuel Lauss /* enable DMABRG mode, enable the DMAC */ 1749d56dd3bSPaul Mundt or = __raw_readl(DMAOR); 1759d56dd3bSPaul Mundt __raw_writel(or | DMAOR_BRG | DMAOR_DMEN, DMAOR); 176fc467a26SManuel Lauss 177fc467a26SManuel Lauss ret = request_irq(DMABRGI0, dmabrg_irq, IRQF_DISABLED, 178fc467a26SManuel Lauss "DMABRG USB address error", NULL); 179fc467a26SManuel Lauss if (ret) 180fc467a26SManuel Lauss goto out0; 181fc467a26SManuel Lauss 182fc467a26SManuel Lauss ret = request_irq(DMABRGI1, dmabrg_irq, IRQF_DISABLED, 183fc467a26SManuel Lauss "DMABRG Transfer End", NULL); 184fc467a26SManuel Lauss if (ret) 185fc467a26SManuel Lauss goto out1; 186fc467a26SManuel Lauss 187fc467a26SManuel Lauss ret = request_irq(DMABRGI2, dmabrg_irq, IRQF_DISABLED, 188fc467a26SManuel Lauss "DMABRG Transfer Half", NULL); 189fc467a26SManuel Lauss if (ret == 0) 190fc467a26SManuel Lauss return ret; 191fc467a26SManuel Lauss 192fc467a26SManuel Lauss free_irq(DMABRGI1, 0); 193fc467a26SManuel Lauss out1: free_irq(DMABRGI0, 0); 194fc467a26SManuel Lauss out0: kfree(dmabrg_handlers); 195fc467a26SManuel Lauss return ret; 196fc467a26SManuel Lauss } 197fc467a26SManuel Lauss subsys_initcall(dmabrg_init); 198