xref: /openbmc/linux/arch/mips/alchemy/devboards/bcsr.c (revision 58e16d792a6a8c6b750f637a4649967fcac853dc)
1*457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29bdcf336SManuel Lauss /*
39bdcf336SManuel Lauss  * bcsr.h -- Db1xxx/Pb1xxx Devboard CPLD registers ("BCSR") abstraction.
49bdcf336SManuel Lauss  *
59bdcf336SManuel Lauss  * All Alchemy development boards (except, of course, the weird PB1000)
69bdcf336SManuel Lauss  * have a few registers in a CPLD with standardised layout; they mostly
79bdcf336SManuel Lauss  * only differ in base address.
89bdcf336SManuel Lauss  * All registers are 16bits wide with 32bit spacing.
99bdcf336SManuel Lauss  */
109bdcf336SManuel Lauss 
1195a43796SManuel Lauss #include <linux/interrupt.h>
12e0288a0aSThomas Gleixner #include <linux/irqchip/chained_irq.h>
1326dd3e4fSPaul Gortmaker #include <linux/init.h>
1426dd3e4fSPaul Gortmaker #include <linux/export.h>
159bdcf336SManuel Lauss #include <linux/spinlock.h>
16ca4d3e67SDavid Howells #include <linux/irq.h>
179bdcf336SManuel Lauss #include <asm/addrspace.h>
189bdcf336SManuel Lauss #include <asm/io.h>
199bdcf336SManuel Lauss #include <asm/mach-db1x00/bcsr.h>
209bdcf336SManuel Lauss 
219bdcf336SManuel Lauss static struct bcsr_reg {
229bdcf336SManuel Lauss 	void __iomem *raddr;
239bdcf336SManuel Lauss 	spinlock_t lock;
249bdcf336SManuel Lauss } bcsr_regs[BCSR_CNT];
259bdcf336SManuel Lauss 
2695a43796SManuel Lauss static void __iomem *bcsr_virt; /* KSEG1 addr of BCSR base */
2795a43796SManuel Lauss static int bcsr_csc_base;	/* linux-irq of first cascaded irq */
2895a43796SManuel Lauss 
bcsr_init(unsigned long bcsr1_phys,unsigned long bcsr2_phys)299bdcf336SManuel Lauss void __init bcsr_init(unsigned long bcsr1_phys, unsigned long bcsr2_phys)
309bdcf336SManuel Lauss {
319bdcf336SManuel Lauss 	int i;
329bdcf336SManuel Lauss 
339bdcf336SManuel Lauss 	bcsr1_phys = KSEG1ADDR(CPHYSADDR(bcsr1_phys));
349bdcf336SManuel Lauss 	bcsr2_phys = KSEG1ADDR(CPHYSADDR(bcsr2_phys));
359bdcf336SManuel Lauss 
3695a43796SManuel Lauss 	bcsr_virt = (void __iomem *)bcsr1_phys;
3795a43796SManuel Lauss 
389bdcf336SManuel Lauss 	for (i = 0; i < BCSR_CNT; i++) {
399bdcf336SManuel Lauss 		if (i >= BCSR_HEXLEDS)
409bdcf336SManuel Lauss 			bcsr_regs[i].raddr = (void __iomem *)bcsr2_phys +
419bdcf336SManuel Lauss 					(0x04 * (i - BCSR_HEXLEDS));
429bdcf336SManuel Lauss 		else
439bdcf336SManuel Lauss 			bcsr_regs[i].raddr = (void __iomem *)bcsr1_phys +
449bdcf336SManuel Lauss 					(0x04 * i);
459bdcf336SManuel Lauss 
469bdcf336SManuel Lauss 		spin_lock_init(&bcsr_regs[i].lock);
479bdcf336SManuel Lauss 	}
489bdcf336SManuel Lauss }
499bdcf336SManuel Lauss 
bcsr_read(enum bcsr_id reg)509bdcf336SManuel Lauss unsigned short bcsr_read(enum bcsr_id reg)
519bdcf336SManuel Lauss {
529bdcf336SManuel Lauss 	unsigned short r;
539bdcf336SManuel Lauss 	unsigned long flags;
549bdcf336SManuel Lauss 
559bdcf336SManuel Lauss 	spin_lock_irqsave(&bcsr_regs[reg].lock, flags);
569bdcf336SManuel Lauss 	r = __raw_readw(bcsr_regs[reg].raddr);
579bdcf336SManuel Lauss 	spin_unlock_irqrestore(&bcsr_regs[reg].lock, flags);
589bdcf336SManuel Lauss 	return r;
599bdcf336SManuel Lauss }
609bdcf336SManuel Lauss EXPORT_SYMBOL_GPL(bcsr_read);
619bdcf336SManuel Lauss 
bcsr_write(enum bcsr_id reg,unsigned short val)629bdcf336SManuel Lauss void bcsr_write(enum bcsr_id reg, unsigned short val)
639bdcf336SManuel Lauss {
649bdcf336SManuel Lauss 	unsigned long flags;
659bdcf336SManuel Lauss 
669bdcf336SManuel Lauss 	spin_lock_irqsave(&bcsr_regs[reg].lock, flags);
679bdcf336SManuel Lauss 	__raw_writew(val, bcsr_regs[reg].raddr);
689bdcf336SManuel Lauss 	wmb();
699bdcf336SManuel Lauss 	spin_unlock_irqrestore(&bcsr_regs[reg].lock, flags);
709bdcf336SManuel Lauss }
719bdcf336SManuel Lauss EXPORT_SYMBOL_GPL(bcsr_write);
729bdcf336SManuel Lauss 
bcsr_mod(enum bcsr_id reg,unsigned short clr,unsigned short set)739bdcf336SManuel Lauss void bcsr_mod(enum bcsr_id reg, unsigned short clr, unsigned short set)
749bdcf336SManuel Lauss {
759bdcf336SManuel Lauss 	unsigned short r;
769bdcf336SManuel Lauss 	unsigned long flags;
779bdcf336SManuel Lauss 
789bdcf336SManuel Lauss 	spin_lock_irqsave(&bcsr_regs[reg].lock, flags);
799bdcf336SManuel Lauss 	r = __raw_readw(bcsr_regs[reg].raddr);
809bdcf336SManuel Lauss 	r &= ~clr;
819bdcf336SManuel Lauss 	r |= set;
829bdcf336SManuel Lauss 	__raw_writew(r, bcsr_regs[reg].raddr);
839bdcf336SManuel Lauss 	wmb();
849bdcf336SManuel Lauss 	spin_unlock_irqrestore(&bcsr_regs[reg].lock, flags);
859bdcf336SManuel Lauss }
869bdcf336SManuel Lauss EXPORT_SYMBOL_GPL(bcsr_mod);
8795a43796SManuel Lauss 
8895a43796SManuel Lauss /*
8995a43796SManuel Lauss  * DB1200/PB1200 CPLD IRQ muxer
9095a43796SManuel Lauss  */
bcsr_csc_handler(struct irq_desc * d)91bd0b9ac4SThomas Gleixner static void bcsr_csc_handler(struct irq_desc *d)
9295a43796SManuel Lauss {
9395a43796SManuel Lauss 	unsigned short bisr = __raw_readw(bcsr_virt + BCSR_REG_INTSTAT);
94e0288a0aSThomas Gleixner 	struct irq_chip *chip = irq_desc_get_chip(d);
9595a43796SManuel Lauss 
96e0288a0aSThomas Gleixner 	chained_irq_enter(chip, d);
9795a43796SManuel Lauss 	generic_handle_irq(bcsr_csc_base + __ffs(bisr));
98e0288a0aSThomas Gleixner 	chained_irq_exit(chip, d);
9995a43796SManuel Lauss }
10095a43796SManuel Lauss 
bcsr_irq_mask(struct irq_data * d)101d24c1a26SThomas Gleixner static void bcsr_irq_mask(struct irq_data *d)
10295a43796SManuel Lauss {
103d24c1a26SThomas Gleixner 	unsigned short v = 1 << (d->irq - bcsr_csc_base);
10495a43796SManuel Lauss 	__raw_writew(v, bcsr_virt + BCSR_REG_MASKCLR);
10595a43796SManuel Lauss 	wmb();
10695a43796SManuel Lauss }
10795a43796SManuel Lauss 
bcsr_irq_maskack(struct irq_data * d)108d24c1a26SThomas Gleixner static void bcsr_irq_maskack(struct irq_data *d)
10995a43796SManuel Lauss {
110d24c1a26SThomas Gleixner 	unsigned short v = 1 << (d->irq - bcsr_csc_base);
11195a43796SManuel Lauss 	__raw_writew(v, bcsr_virt + BCSR_REG_MASKCLR);
11295a43796SManuel Lauss 	__raw_writew(v, bcsr_virt + BCSR_REG_INTSTAT);	/* ack */
11395a43796SManuel Lauss 	wmb();
11495a43796SManuel Lauss }
11595a43796SManuel Lauss 
bcsr_irq_unmask(struct irq_data * d)116d24c1a26SThomas Gleixner static void bcsr_irq_unmask(struct irq_data *d)
11795a43796SManuel Lauss {
118d24c1a26SThomas Gleixner 	unsigned short v = 1 << (d->irq - bcsr_csc_base);
11995a43796SManuel Lauss 	__raw_writew(v, bcsr_virt + BCSR_REG_MASKSET);
12095a43796SManuel Lauss 	wmb();
12195a43796SManuel Lauss }
12295a43796SManuel Lauss 
12395a43796SManuel Lauss static struct irq_chip bcsr_irq_type = {
12495a43796SManuel Lauss 	.name		= "CPLD",
125d24c1a26SThomas Gleixner 	.irq_mask	= bcsr_irq_mask,
126d24c1a26SThomas Gleixner 	.irq_mask_ack	= bcsr_irq_maskack,
127d24c1a26SThomas Gleixner 	.irq_unmask	= bcsr_irq_unmask,
12895a43796SManuel Lauss };
12995a43796SManuel Lauss 
bcsr_init_irq(int csc_start,int csc_end,int hook_irq)13095a43796SManuel Lauss void __init bcsr_init_irq(int csc_start, int csc_end, int hook_irq)
13195a43796SManuel Lauss {
13295a43796SManuel Lauss 	unsigned int irq;
13395a43796SManuel Lauss 
134fb469f08SManuel Lauss 	/* mask & enable & ack all */
13595a43796SManuel Lauss 	__raw_writew(0xffff, bcsr_virt + BCSR_REG_MASKCLR);
136fb469f08SManuel Lauss 	__raw_writew(0xffff, bcsr_virt + BCSR_REG_INTSET);
13795a43796SManuel Lauss 	__raw_writew(0xffff, bcsr_virt + BCSR_REG_INTSTAT);
13895a43796SManuel Lauss 	wmb();
13995a43796SManuel Lauss 
14095a43796SManuel Lauss 	bcsr_csc_base = csc_start;
14195a43796SManuel Lauss 
14295a43796SManuel Lauss 	for (irq = csc_start; irq <= csc_end; irq++)
143e4ec7989SThomas Gleixner 		irq_set_chip_and_handler_name(irq, &bcsr_irq_type,
14495a43796SManuel Lauss 					      handle_level_irq, "level");
14595a43796SManuel Lauss 
146e4ec7989SThomas Gleixner 	irq_set_chained_handler(hook_irq, bcsr_csc_handler);
14795a43796SManuel Lauss }
148