// SPDX-License-Identifier: GPL-2.01/*2* SH7760 DMABRG IRQ handling3*4* (c) 2007 MSC Vertriebsges.m.b.H, Manuel Lauss <[email protected]>5*/67#include <linux/interrupt.h>8#include <linux/kernel.h>9#include <linux/slab.h>10#include <asm/dma.h>11#include <asm/dmabrg.h>12#include <asm/io.h>1314/*15* The DMABRG is a special DMA unit within the SH7760. It does transfers16* from USB-SRAM/Audio units to main memory (and also the LCDC; but that17* part is sensibly placed in the LCDC registers and requires no irqs)18* It has 3 IRQ lines which trigger 10 events, and works independently19* from the traditional SH DMAC (although it blocks usage of DMAC 0)20*21* BRGIRQID | component | dir | meaning | source22* -----------------------------------------------------23* 0 | USB-DMA | ... | xfer done | DMABRGI124* 1 | USB-UAE | ... | USB addr err.| DMABRGI025* 2 | HAC0/SSI0 | play| all done | DMABRGI126* 3 | HAC0/SSI0 | play| half done | DMABRGI227* 4 | HAC0/SSI0 | rec | all done | DMABRGI128* 5 | HAC0/SSI0 | rec | half done | DMABRGI229* 6 | HAC1/SSI1 | play| all done | DMABRGI130* 7 | HAC1/SSI1 | play| half done | DMABRGI231* 8 | HAC1/SSI1 | rec | all done | DMABRGI132* 9 | HAC1/SSI1 | rec | half done | DMABRGI233*34* all can be enabled/disabled in the DMABRGCR register,35* as well as checked if they occurred.36*37* DMABRGI0 services USB DMA Address errors, but it still must be38* enabled/acked in the DMABRGCR register. USB-DMA complete indicator39* is grouped together with the audio buffer end indicators, too bad...40*41* DMABRGCR: Bits 31-24: audio-dma ENABLE flags,42* Bits 23-16: audio-dma STATUS flags,43* Bits 9-8: USB error/xfer ENABLE,44* Bits 1-0: USB error/xfer STATUS.45* Ack an IRQ by writing 0 to the STATUS flag.46* Mask IRQ by writing 0 to ENABLE flag.47*48* Usage is almost like with any other IRQ:49* dmabrg_request_irq(BRGIRQID, handler, data)50* dmabrg_free_irq(BRGIRQID)51*52* handler prototype: void brgirqhandler(void *data)53*/5455#define DMARSRA 0xfe09000056#define DMAOR 0xffa0004057#define DMACHCR0 0xffa0000c58#define DMABRGCR 0xfe3c00005960#define DMAOR_BRG 0x0000c00061#define DMAOR_DMEN 0x000000016263#define DMABRGI0 6864#define DMABRGI1 6965#define DMABRGI2 706667struct dmabrg_handler {68void (*handler)(void *);69void *data;70} *dmabrg_handlers;7172static inline void dmabrg_call_handler(int i)73{74dmabrg_handlers[i].handler(dmabrg_handlers[i].data);75}7677/*78* main DMABRG irq handler. It acks irqs and then79* handles every set and unmasked bit sequentially.80* No locking and no validity checks; it should be81* as fast as possible (audio!)82*/83static irqreturn_t dmabrg_irq(int irq, void *data)84{85unsigned long dcr;86unsigned int i;8788dcr = __raw_readl(DMABRGCR);89__raw_writel(dcr & ~0x00ff0003, DMABRGCR); /* ack all */90dcr &= dcr >> 8; /* ignore masked */9192/* USB stuff, get it out of the way first */93if (dcr & 1)94dmabrg_call_handler(DMABRGIRQ_USBDMA);95if (dcr & 2)96dmabrg_call_handler(DMABRGIRQ_USBDMAERR);9798/* Audio */99dcr >>= 16;100while (dcr) {101i = __ffs(dcr);102dcr &= dcr - 1;103dmabrg_call_handler(i + DMABRGIRQ_A0TXF);104}105return IRQ_HANDLED;106}107108static void dmabrg_disable_irq(unsigned int dmairq)109{110unsigned long dcr;111dcr = __raw_readl(DMABRGCR);112dcr &= ~(1 << ((dmairq > 1) ? dmairq + 22 : dmairq + 8));113__raw_writel(dcr, DMABRGCR);114}115116static void dmabrg_enable_irq(unsigned int dmairq)117{118unsigned long dcr;119dcr = __raw_readl(DMABRGCR);120dcr |= (1 << ((dmairq > 1) ? dmairq + 22 : dmairq + 8));121__raw_writel(dcr, DMABRGCR);122}123124int dmabrg_request_irq(unsigned int dmairq, void(*handler)(void*),125void *data)126{127if ((dmairq > 9) || !handler)128return -ENOENT;129if (dmabrg_handlers[dmairq].handler)130return -EBUSY;131132dmabrg_handlers[dmairq].handler = handler;133dmabrg_handlers[dmairq].data = data;134135dmabrg_enable_irq(dmairq);136return 0;137}138EXPORT_SYMBOL_GPL(dmabrg_request_irq);139140void dmabrg_free_irq(unsigned int dmairq)141{142if (likely(dmairq < 10)) {143dmabrg_disable_irq(dmairq);144dmabrg_handlers[dmairq].handler = NULL;145dmabrg_handlers[dmairq].data = NULL;146}147}148EXPORT_SYMBOL_GPL(dmabrg_free_irq);149150static int __init dmabrg_init(void)151{152unsigned long or;153int ret;154155dmabrg_handlers = kcalloc(10, sizeof(struct dmabrg_handler),156GFP_KERNEL);157if (!dmabrg_handlers)158return -ENOMEM;159160#ifdef CONFIG_SH_DMA161/* request DMAC channel 0 before anyone else can get it */162ret = request_dma(0, "DMAC 0 (DMABRG)");163if (ret < 0)164printk(KERN_INFO "DMABRG: DMAC ch0 not reserved!\n");165#endif166167__raw_writel(0, DMABRGCR);168__raw_writel(0, DMACHCR0);169__raw_writel(0x94000000, DMARSRA); /* enable DMABRG in DMAC 0 */170171/* enable DMABRG mode, enable the DMAC */172or = __raw_readl(DMAOR);173__raw_writel(or | DMAOR_BRG | DMAOR_DMEN, DMAOR);174175ret = request_irq(DMABRGI0, dmabrg_irq, 0,176"DMABRG USB address error", NULL);177if (ret)178goto out0;179180ret = request_irq(DMABRGI1, dmabrg_irq, 0,181"DMABRG Transfer End", NULL);182if (ret)183goto out1;184185ret = request_irq(DMABRGI2, dmabrg_irq, 0,186"DMABRG Transfer Half", NULL);187if (ret == 0)188return ret;189190free_irq(DMABRGI1, NULL);191out1: free_irq(DMABRGI0, NULL);192out0: kfree(dmabrg_handlers);193return ret;194}195subsys_initcall(dmabrg_init);196197198