4 * Copyright (c) 2009 emlix GmbH
5 * Authors: Oskar Schirmer <os@emlix.com>
6 * Johannes Weiner <jw@emlix.com>
7 * Daniel Gloeckner <dg@emlix.com>
9 #include <linux/bitops.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
14 #include <linux/irq.h>
15 #include <linux/gpio.h>
17 #include <variant/hardware.h>
19 #define IRQ_BASE XTENSA_NR_IRQS
21 #define S6_GPIO_DATA 0x000
22 #define S6_GPIO_IS 0x404
23 #define S6_GPIO_IBE 0x408
24 #define S6_GPIO_IEV 0x40C
25 #define S6_GPIO_IE 0x410
26 #define S6_GPIO_RIS 0x414
27 #define S6_GPIO_MIS 0x418
28 #define S6_GPIO_IC 0x41C
29 #define S6_GPIO_AFSEL 0x420
30 #define S6_GPIO_DIR 0x800
31 #define S6_GPIO_BANK(nr) ((nr) * 0x1000)
32 #define S6_GPIO_MASK(nr) (4 << (nr))
33 #define S6_GPIO_OFFSET(nr) \
34 (S6_GPIO_BANK((nr) >> 3) + S6_GPIO_MASK((nr) & 7))
36 static int direction_input(struct gpio_chip *chip, unsigned int off)
38 writeb(0, S6_REG_GPIO + S6_GPIO_DIR + S6_GPIO_OFFSET(off));
42 static int get(struct gpio_chip *chip, unsigned int off)
44 return readb(S6_REG_GPIO + S6_GPIO_DATA + S6_GPIO_OFFSET(off));
47 static int direction_output(struct gpio_chip *chip, unsigned int off, int val)
49 unsigned rel = S6_GPIO_OFFSET(off);
50 writeb(~0, S6_REG_GPIO + S6_GPIO_DIR + rel);
51 writeb(val ? ~0 : 0, S6_REG_GPIO + S6_GPIO_DATA + rel);
55 static void set(struct gpio_chip *chip, unsigned int off, int val)
57 writeb(val ? ~0 : 0, S6_REG_GPIO + S6_GPIO_DATA + S6_GPIO_OFFSET(off));
60 static int to_irq(struct gpio_chip *chip, unsigned offset)
63 return offset + IRQ_BASE;
67 static struct gpio_chip gpiochip = {
69 .direction_input = direction_input,
71 .direction_output = direction_output,
76 .can_sleep = 0, /* no blocking io needed */
77 .exported = 0, /* no exporting to userspace */
80 int s6_gpio_init(u32 afsel)
82 writeb(afsel, S6_REG_GPIO + S6_GPIO_BANK(0) + S6_GPIO_AFSEL);
83 writeb(afsel >> 8, S6_REG_GPIO + S6_GPIO_BANK(1) + S6_GPIO_AFSEL);
84 writeb(afsel >> 16, S6_REG_GPIO + S6_GPIO_BANK(2) + S6_GPIO_AFSEL);
85 return gpiochip_add(&gpiochip);
88 static void ack(unsigned int irq)
90 writeb(1 << (irq - IRQ_BASE), S6_REG_GPIO + S6_GPIO_IC);
93 static void mask(unsigned int irq)
95 u8 r = readb(S6_REG_GPIO + S6_GPIO_IE);
96 r &= ~(1 << (irq - IRQ_BASE));
97 writeb(r, S6_REG_GPIO + S6_GPIO_IE);
100 static void unmask(unsigned int irq)
102 u8 m = readb(S6_REG_GPIO + S6_GPIO_IE);
103 m |= 1 << (irq - IRQ_BASE);
104 writeb(m, S6_REG_GPIO + S6_GPIO_IE);
107 static int set_type(unsigned int irq, unsigned int type)
109 const u8 m = 1 << (irq - IRQ_BASE);
110 irq_flow_handler_t handler;
111 struct irq_desc *desc;
114 if (type == IRQ_TYPE_PROBE) {
115 if ((readb(S6_REG_GPIO + S6_GPIO_BANK(0) + S6_GPIO_AFSEL) & m)
116 || (readb(S6_REG_GPIO + S6_GPIO_BANK(0) + S6_GPIO_IE) & m)
117 || readb(S6_REG_GPIO + S6_GPIO_BANK(0) + S6_GPIO_DIR
118 + S6_GPIO_MASK(irq - IRQ_BASE)))
120 type = IRQ_TYPE_EDGE_BOTH;
123 reg = readb(S6_REG_GPIO + S6_GPIO_BANK(0) + S6_GPIO_IS);
124 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH)) {
126 handler = handle_level_irq;
129 handler = handle_edge_irq;
131 writeb(reg, S6_REG_GPIO + S6_GPIO_BANK(0) + S6_GPIO_IS);
132 desc = irq_to_desc(irq);
133 desc->handle_irq = handler;
135 reg = readb(S6_REG_GPIO + S6_GPIO_BANK(0) + S6_GPIO_IEV);
136 if (type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_EDGE_RISING))
140 writeb(reg, S6_REG_GPIO + S6_GPIO_BANK(0) + S6_GPIO_IEV);
142 reg = readb(S6_REG_GPIO + S6_GPIO_BANK(0) + S6_GPIO_IBE);
143 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
147 writeb(reg, S6_REG_GPIO + S6_GPIO_BANK(0) + S6_GPIO_IBE);
151 static struct irq_chip gpioirqs = {
156 .set_type = set_type,
159 static u8 demux_masks[4];
161 static void demux_irqs(unsigned int irq, struct irq_desc *desc)
163 u8 *mask = get_irq_desc_data(desc);
167 desc->chip->mask(irq);
168 desc->chip->ack(irq);
169 pending = readb(S6_REG_GPIO + S6_GPIO_BANK(0) + S6_GPIO_MIS) & *mask;
172 int n = ffs(pending);
175 generic_handle_irq(cirq);
177 desc->chip->unmask(irq);
180 extern const signed char *platform_irq_mappings[XTENSA_NR_IRQS];
182 void __init variant_init_irq(void)
185 writeb(0, S6_REG_GPIO + S6_GPIO_BANK(0) + S6_GPIO_IE);
186 for (irq = n = 0; irq < XTENSA_NR_IRQS; irq++) {
187 const signed char *mapping = platform_irq_mappings[irq];
192 for(mask = 0; *mapping != -1; mapping++)
194 case S6_INTC_GPIO(0):
197 case S6_INTC_GPIO(1):
200 case S6_INTC_GPIO(2):
203 case S6_INTC_GPIO(3):
212 printk(KERN_ERR "chained irq chips can't share"
213 " parent irq %i\n", irq);
216 demux_masks[n] = mask;
222 set_irq_chip(cirq, &gpioirqs);
223 set_irq_type(irq, IRQ_TYPE_LEVEL_LOW);
225 set_irq_data(irq, demux_masks + n);
226 set_irq_chained_handler(irq, demux_irqs);
227 if (++n == ARRAY_SIZE(demux_masks))