18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * bcsr.h -- Db1xxx/Pb1xxx Devboard CPLD registers ("BCSR") abstraction. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * All Alchemy development boards (except, of course, the weird PB1000) 68c2ecf20Sopenharmony_ci * have a few registers in a CPLD with standardised layout; they mostly 78c2ecf20Sopenharmony_ci * only differ in base address. 88c2ecf20Sopenharmony_ci * All registers are 16bits wide with 32bit spacing. 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 128c2ecf20Sopenharmony_ci#include <linux/irqchip/chained_irq.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci#include <linux/export.h> 158c2ecf20Sopenharmony_ci#include <linux/spinlock.h> 168c2ecf20Sopenharmony_ci#include <linux/irq.h> 178c2ecf20Sopenharmony_ci#include <asm/addrspace.h> 188c2ecf20Sopenharmony_ci#include <asm/io.h> 198c2ecf20Sopenharmony_ci#include <asm/mach-db1x00/bcsr.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistatic struct bcsr_reg { 228c2ecf20Sopenharmony_ci void __iomem *raddr; 238c2ecf20Sopenharmony_ci spinlock_t lock; 248c2ecf20Sopenharmony_ci} bcsr_regs[BCSR_CNT]; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic void __iomem *bcsr_virt; /* KSEG1 addr of BCSR base */ 278c2ecf20Sopenharmony_cistatic int bcsr_csc_base; /* linux-irq of first cascaded irq */ 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_civoid __init bcsr_init(unsigned long bcsr1_phys, unsigned long bcsr2_phys) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci int i; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci bcsr1_phys = KSEG1ADDR(CPHYSADDR(bcsr1_phys)); 348c2ecf20Sopenharmony_ci bcsr2_phys = KSEG1ADDR(CPHYSADDR(bcsr2_phys)); 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci bcsr_virt = (void __iomem *)bcsr1_phys; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci for (i = 0; i < BCSR_CNT; i++) { 398c2ecf20Sopenharmony_ci if (i >= BCSR_HEXLEDS) 408c2ecf20Sopenharmony_ci bcsr_regs[i].raddr = (void __iomem *)bcsr2_phys + 418c2ecf20Sopenharmony_ci (0x04 * (i - BCSR_HEXLEDS)); 428c2ecf20Sopenharmony_ci else 438c2ecf20Sopenharmony_ci bcsr_regs[i].raddr = (void __iomem *)bcsr1_phys + 448c2ecf20Sopenharmony_ci (0x04 * i); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci spin_lock_init(&bcsr_regs[i].lock); 478c2ecf20Sopenharmony_ci } 488c2ecf20Sopenharmony_ci} 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ciunsigned short bcsr_read(enum bcsr_id reg) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci unsigned short r; 538c2ecf20Sopenharmony_ci unsigned long flags; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci spin_lock_irqsave(&bcsr_regs[reg].lock, flags); 568c2ecf20Sopenharmony_ci r = __raw_readw(bcsr_regs[reg].raddr); 578c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bcsr_regs[reg].lock, flags); 588c2ecf20Sopenharmony_ci return r; 598c2ecf20Sopenharmony_ci} 608c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcsr_read); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_civoid bcsr_write(enum bcsr_id reg, unsigned short val) 638c2ecf20Sopenharmony_ci{ 648c2ecf20Sopenharmony_ci unsigned long flags; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci spin_lock_irqsave(&bcsr_regs[reg].lock, flags); 678c2ecf20Sopenharmony_ci __raw_writew(val, bcsr_regs[reg].raddr); 688c2ecf20Sopenharmony_ci wmb(); 698c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bcsr_regs[reg].lock, flags); 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcsr_write); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_civoid bcsr_mod(enum bcsr_id reg, unsigned short clr, unsigned short set) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci unsigned short r; 768c2ecf20Sopenharmony_ci unsigned long flags; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci spin_lock_irqsave(&bcsr_regs[reg].lock, flags); 798c2ecf20Sopenharmony_ci r = __raw_readw(bcsr_regs[reg].raddr); 808c2ecf20Sopenharmony_ci r &= ~clr; 818c2ecf20Sopenharmony_ci r |= set; 828c2ecf20Sopenharmony_ci __raw_writew(r, bcsr_regs[reg].raddr); 838c2ecf20Sopenharmony_ci wmb(); 848c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bcsr_regs[reg].lock, flags); 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bcsr_mod); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci/* 898c2ecf20Sopenharmony_ci * DB1200/PB1200 CPLD IRQ muxer 908c2ecf20Sopenharmony_ci */ 918c2ecf20Sopenharmony_cistatic void bcsr_csc_handler(struct irq_desc *d) 928c2ecf20Sopenharmony_ci{ 938c2ecf20Sopenharmony_ci unsigned short bisr = __raw_readw(bcsr_virt + BCSR_REG_INTSTAT); 948c2ecf20Sopenharmony_ci struct irq_chip *chip = irq_desc_get_chip(d); 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci chained_irq_enter(chip, d); 978c2ecf20Sopenharmony_ci generic_handle_irq(bcsr_csc_base + __ffs(bisr)); 988c2ecf20Sopenharmony_ci chained_irq_exit(chip, d); 998c2ecf20Sopenharmony_ci} 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_cistatic void bcsr_irq_mask(struct irq_data *d) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci unsigned short v = 1 << (d->irq - bcsr_csc_base); 1048c2ecf20Sopenharmony_ci __raw_writew(v, bcsr_virt + BCSR_REG_MASKCLR); 1058c2ecf20Sopenharmony_ci wmb(); 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_cistatic void bcsr_irq_maskack(struct irq_data *d) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci unsigned short v = 1 << (d->irq - bcsr_csc_base); 1118c2ecf20Sopenharmony_ci __raw_writew(v, bcsr_virt + BCSR_REG_MASKCLR); 1128c2ecf20Sopenharmony_ci __raw_writew(v, bcsr_virt + BCSR_REG_INTSTAT); /* ack */ 1138c2ecf20Sopenharmony_ci wmb(); 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic void bcsr_irq_unmask(struct irq_data *d) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci unsigned short v = 1 << (d->irq - bcsr_csc_base); 1198c2ecf20Sopenharmony_ci __raw_writew(v, bcsr_virt + BCSR_REG_MASKSET); 1208c2ecf20Sopenharmony_ci wmb(); 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic struct irq_chip bcsr_irq_type = { 1248c2ecf20Sopenharmony_ci .name = "CPLD", 1258c2ecf20Sopenharmony_ci .irq_mask = bcsr_irq_mask, 1268c2ecf20Sopenharmony_ci .irq_mask_ack = bcsr_irq_maskack, 1278c2ecf20Sopenharmony_ci .irq_unmask = bcsr_irq_unmask, 1288c2ecf20Sopenharmony_ci}; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_civoid __init bcsr_init_irq(int csc_start, int csc_end, int hook_irq) 1318c2ecf20Sopenharmony_ci{ 1328c2ecf20Sopenharmony_ci unsigned int irq; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci /* mask & enable & ack all */ 1358c2ecf20Sopenharmony_ci __raw_writew(0xffff, bcsr_virt + BCSR_REG_MASKCLR); 1368c2ecf20Sopenharmony_ci __raw_writew(0xffff, bcsr_virt + BCSR_REG_INTSET); 1378c2ecf20Sopenharmony_ci __raw_writew(0xffff, bcsr_virt + BCSR_REG_INTSTAT); 1388c2ecf20Sopenharmony_ci wmb(); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci bcsr_csc_base = csc_start; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci for (irq = csc_start; irq <= csc_end; irq++) 1438c2ecf20Sopenharmony_ci irq_set_chip_and_handler_name(irq, &bcsr_irq_type, 1448c2ecf20Sopenharmony_ci handle_level_irq, "level"); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci irq_set_chained_handler(hook_irq, bcsr_csc_handler); 1478c2ecf20Sopenharmony_ci} 148