| 
									
										
										
										
											2009-10-04 14:55:24 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * bcsr.h -- Db1xxx/Pb1xxx Devboard CPLD registers ("BCSR") abstraction. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * All Alchemy development boards (except, of course, the weird PB1000) | 
					
						
							|  |  |  |  * have a few registers in a CPLD with standardised layout; they mostly | 
					
						
							|  |  |  |  * only differ in base address. | 
					
						
							|  |  |  |  * All registers are 16bits wide with 32bit spacing. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | #include <linux/interrupt.h>
 | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:24 +02:00
										 |  |  | #include <linux/module.h>
 | 
					
						
							|  |  |  | #include <linux/spinlock.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-07 14:08:54 +01:00
										 |  |  | #include <linux/irq.h>
 | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:24 +02:00
										 |  |  | #include <asm/addrspace.h>
 | 
					
						
							|  |  |  | #include <asm/io.h>
 | 
					
						
							|  |  |  | #include <asm/mach-db1x00/bcsr.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct bcsr_reg { | 
					
						
							|  |  |  | 	void __iomem *raddr; | 
					
						
							|  |  |  | 	spinlock_t lock; | 
					
						
							|  |  |  | } bcsr_regs[BCSR_CNT]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-22 12:59:30 +01:00
										 |  |  | static void __iomem *bcsr_virt; /* KSEG1 addr of BCSR base */ | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | static int bcsr_csc_base;	/* linux-irq of first cascaded irq */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:24 +02:00
										 |  |  | void __init bcsr_init(unsigned long bcsr1_phys, unsigned long bcsr2_phys) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bcsr1_phys = KSEG1ADDR(CPHYSADDR(bcsr1_phys)); | 
					
						
							|  |  |  | 	bcsr2_phys = KSEG1ADDR(CPHYSADDR(bcsr2_phys)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | 	bcsr_virt = (void __iomem *)bcsr1_phys; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:24 +02:00
										 |  |  | 	for (i = 0; i < BCSR_CNT; i++) { | 
					
						
							|  |  |  | 		if (i >= BCSR_HEXLEDS) | 
					
						
							|  |  |  | 			bcsr_regs[i].raddr = (void __iomem *)bcsr2_phys + | 
					
						
							|  |  |  | 					(0x04 * (i - BCSR_HEXLEDS)); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			bcsr_regs[i].raddr = (void __iomem *)bcsr1_phys + | 
					
						
							|  |  |  | 					(0x04 * i); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		spin_lock_init(&bcsr_regs[i].lock); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | unsigned short bcsr_read(enum bcsr_id reg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned short r; | 
					
						
							|  |  |  | 	unsigned long flags; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spin_lock_irqsave(&bcsr_regs[reg].lock, flags); | 
					
						
							|  |  |  | 	r = __raw_readw(bcsr_regs[reg].raddr); | 
					
						
							|  |  |  | 	spin_unlock_irqrestore(&bcsr_regs[reg].lock, flags); | 
					
						
							|  |  |  | 	return r; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(bcsr_read); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void bcsr_write(enum bcsr_id reg, unsigned short val) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned long flags; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spin_lock_irqsave(&bcsr_regs[reg].lock, flags); | 
					
						
							|  |  |  | 	__raw_writew(val, bcsr_regs[reg].raddr); | 
					
						
							|  |  |  | 	wmb(); | 
					
						
							|  |  |  | 	spin_unlock_irqrestore(&bcsr_regs[reg].lock, flags); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(bcsr_write); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void bcsr_mod(enum bcsr_id reg, unsigned short clr, unsigned short set) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned short r; | 
					
						
							|  |  |  | 	unsigned long flags; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	spin_lock_irqsave(&bcsr_regs[reg].lock, flags); | 
					
						
							|  |  |  | 	r = __raw_readw(bcsr_regs[reg].raddr); | 
					
						
							|  |  |  | 	r &= ~clr; | 
					
						
							|  |  |  | 	r |= set; | 
					
						
							|  |  |  | 	__raw_writew(r, bcsr_regs[reg].raddr); | 
					
						
							|  |  |  | 	wmb(); | 
					
						
							|  |  |  | 	spin_unlock_irqrestore(&bcsr_regs[reg].lock, flags); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(bcsr_mod); | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * DB1200/PB1200 CPLD IRQ muxer | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void bcsr_csc_handler(unsigned int irq, struct irq_desc *d) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned short bisr = __raw_readw(bcsr_virt + BCSR_REG_INTSTAT); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-12 08:28:35 +02:00
										 |  |  | 	disable_irq_nosync(irq); | 
					
						
							| 
									
										
										
										
											2012-01-21 18:13:15 +01:00
										 |  |  | 	generic_handle_irq(bcsr_csc_base + __ffs(bisr)); | 
					
						
							| 
									
										
										
										
											2011-08-12 08:28:35 +02:00
										 |  |  | 	enable_irq(irq); | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 21:08:44 +00:00
										 |  |  | static void bcsr_irq_mask(struct irq_data *d) | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 21:08:44 +00:00
										 |  |  | 	unsigned short v = 1 << (d->irq - bcsr_csc_base); | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | 	__raw_writew(v, bcsr_virt + BCSR_REG_MASKCLR); | 
					
						
							|  |  |  | 	wmb(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 21:08:44 +00:00
										 |  |  | static void bcsr_irq_maskack(struct irq_data *d) | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 21:08:44 +00:00
										 |  |  | 	unsigned short v = 1 << (d->irq - bcsr_csc_base); | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | 	__raw_writew(v, bcsr_virt + BCSR_REG_MASKCLR); | 
					
						
							|  |  |  | 	__raw_writew(v, bcsr_virt + BCSR_REG_INTSTAT);	/* ack */ | 
					
						
							|  |  |  | 	wmb(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 21:08:44 +00:00
										 |  |  | static void bcsr_irq_unmask(struct irq_data *d) | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-23 21:08:44 +00:00
										 |  |  | 	unsigned short v = 1 << (d->irq - bcsr_csc_base); | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | 	__raw_writew(v, bcsr_virt + BCSR_REG_MASKSET); | 
					
						
							|  |  |  | 	wmb(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct irq_chip bcsr_irq_type = { | 
					
						
							|  |  |  | 	.name		= "CPLD", | 
					
						
							| 
									
										
										
										
											2011-03-23 21:08:44 +00:00
										 |  |  | 	.irq_mask	= bcsr_irq_mask, | 
					
						
							|  |  |  | 	.irq_mask_ack	= bcsr_irq_maskack, | 
					
						
							|  |  |  | 	.irq_unmask	= bcsr_irq_unmask, | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void __init bcsr_init_irq(int csc_start, int csc_end, int hook_irq) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned int irq; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-01 20:03:29 +01:00
										 |  |  | 	/* mask & enable & ack all */ | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | 	__raw_writew(0xffff, bcsr_virt + BCSR_REG_MASKCLR); | 
					
						
							| 
									
										
										
										
											2011-11-01 20:03:29 +01:00
										 |  |  | 	__raw_writew(0xffff, bcsr_virt + BCSR_REG_INTSET); | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | 	__raw_writew(0xffff, bcsr_virt + BCSR_REG_INTSTAT); | 
					
						
							|  |  |  | 	wmb(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bcsr_csc_base = csc_start; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (irq = csc_start; irq <= csc_end; irq++) | 
					
						
							| 
									
										
										
										
											2011-03-27 15:19:28 +02:00
										 |  |  | 		irq_set_chip_and_handler_name(irq, &bcsr_irq_type, | 
					
						
							|  |  |  | 					      handle_level_irq, "level"); | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-27 15:19:28 +02:00
										 |  |  | 	irq_set_chained_handler(hook_irq, bcsr_csc_handler); | 
					
						
							| 
									
										
										
										
											2009-10-04 14:55:25 +02:00
										 |  |  | } |