blob: 078894bc3b9a2f59e59b941d7d1259749a8ff2a8 [file] [log] [blame]
Lennert Buytenhek9569dae2008-10-20 01:51:03 +02001/*
2 * arch/arm/plat-orion/gpio.c
3 *
4 * Marvell Orion SoC GPIO handling.
5 *
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
Lennert Buytenhek07332312008-10-20 01:51:03 +020013#include <linux/irq.h>
Lennert Buytenhek9569dae2008-10-20 01:51:03 +020014#include <linux/module.h>
15#include <linux/spinlock.h>
16#include <linux/bitops.h>
17#include <linux/io.h>
Erik Benadaa8865652009-05-28 17:08:55 -070018#include <linux/gpio.h>
Lennert Buytenhek9569dae2008-10-20 01:51:03 +020019
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +010020/*
21 * GPIO unit register offsets.
22 */
23#define GPIO_OUT_OFF 0x0000
24#define GPIO_IO_CONF_OFF 0x0004
25#define GPIO_BLINK_EN_OFF 0x0008
26#define GPIO_IN_POL_OFF 0x000c
27#define GPIO_DATA_IN_OFF 0x0010
28#define GPIO_EDGE_CAUSE_OFF 0x0014
29#define GPIO_EDGE_MASK_OFF 0x0018
30#define GPIO_LEVEL_MASK_OFF 0x001c
Lennert Buytenhek9569dae2008-10-20 01:51:03 +020031
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +010032struct orion_gpio_chip {
33 struct gpio_chip chip;
34 spinlock_t lock;
35 void __iomem *base;
36 unsigned long valid_input;
37 unsigned long valid_output;
38 int mask_offset;
39 int secondary_irq_base;
40};
41
42static void __iomem *GPIO_OUT(struct orion_gpio_chip *ochip)
43{
44 return ochip->base + GPIO_OUT_OFF;
45}
46
47static void __iomem *GPIO_IO_CONF(struct orion_gpio_chip *ochip)
48{
49 return ochip->base + GPIO_IO_CONF_OFF;
50}
51
52static void __iomem *GPIO_BLINK_EN(struct orion_gpio_chip *ochip)
53{
54 return ochip->base + GPIO_BLINK_EN_OFF;
55}
56
57static void __iomem *GPIO_IN_POL(struct orion_gpio_chip *ochip)
58{
59 return ochip->base + GPIO_IN_POL_OFF;
60}
61
62static void __iomem *GPIO_DATA_IN(struct orion_gpio_chip *ochip)
63{
64 return ochip->base + GPIO_DATA_IN_OFF;
65}
66
67static void __iomem *GPIO_EDGE_CAUSE(struct orion_gpio_chip *ochip)
68{
69 return ochip->base + GPIO_EDGE_CAUSE_OFF;
70}
71
72static void __iomem *GPIO_EDGE_MASK(struct orion_gpio_chip *ochip)
73{
74 return ochip->base + ochip->mask_offset + GPIO_EDGE_MASK_OFF;
75}
76
77static void __iomem *GPIO_LEVEL_MASK(struct orion_gpio_chip *ochip)
78{
79 return ochip->base + ochip->mask_offset + GPIO_LEVEL_MASK_OFF;
80}
81
82
83static struct orion_gpio_chip orion_gpio_chips[2];
84static int orion_gpio_chip_count;
85
86static inline void
87__set_direction(struct orion_gpio_chip *ochip, unsigned pin, int input)
Lennert Buytenhek9569dae2008-10-20 01:51:03 +020088{
89 u32 u;
90
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +010091 u = readl(GPIO_IO_CONF(ochip));
Lennert Buytenhek9569dae2008-10-20 01:51:03 +020092 if (input)
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +010093 u |= 1 << pin;
Lennert Buytenhek9569dae2008-10-20 01:51:03 +020094 else
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +010095 u &= ~(1 << pin);
96 writel(u, GPIO_IO_CONF(ochip));
Lennert Buytenhek9569dae2008-10-20 01:51:03 +020097}
98
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +010099static void __set_level(struct orion_gpio_chip *ochip, unsigned pin, int high)
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200100{
101 u32 u;
102
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100103 u = readl(GPIO_OUT(ochip));
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200104 if (high)
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100105 u |= 1 << pin;
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200106 else
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100107 u &= ~(1 << pin);
108 writel(u, GPIO_OUT(ochip));
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200109}
110
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100111static inline void
112__set_blinking(struct orion_gpio_chip *ochip, unsigned pin, int blink)
Erik Benadaa8865652009-05-28 17:08:55 -0700113{
114 u32 u;
115
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100116 u = readl(GPIO_BLINK_EN(ochip));
Erik Benadaa8865652009-05-28 17:08:55 -0700117 if (blink)
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100118 u |= 1 << pin;
Erik Benadaa8865652009-05-28 17:08:55 -0700119 else
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100120 u &= ~(1 << pin);
121 writel(u, GPIO_BLINK_EN(ochip));
Erik Benadaa8865652009-05-28 17:08:55 -0700122}
123
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100124static inline int
125orion_gpio_is_valid(struct orion_gpio_chip *ochip, unsigned pin, int mode)
Erik Benadaa8865652009-05-28 17:08:55 -0700126{
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100127 if (pin >= ochip->chip.ngpio)
128 goto err_out;
129
130 if ((mode & GPIO_INPUT_OK) && !test_bit(pin, &ochip->valid_input))
131 goto err_out;
132
133 if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, &ochip->valid_output))
134 goto err_out;
135
136 return 1;
Erik Benadaa8865652009-05-28 17:08:55 -0700137
138err_out:
139 pr_debug("%s: invalid GPIO %d\n", __func__, pin);
140 return false;
141}
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200142
143/*
144 * GENERIC_GPIO primitives.
145 */
Erik Benadaa8865652009-05-28 17:08:55 -0700146static int orion_gpio_request(struct gpio_chip *chip, unsigned pin)
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200147{
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100148 struct orion_gpio_chip *ochip =
149 container_of(chip, struct orion_gpio_chip, chip);
150
151 if (orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK) ||
152 orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
Erik Benadaa8865652009-05-28 17:08:55 -0700153 return 0;
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100154
Erik Benadaa8865652009-05-28 17:08:55 -0700155 return -EINVAL;
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200156}
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200157
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100158static int orion_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200159{
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100160 struct orion_gpio_chip *ochip =
161 container_of(chip, struct orion_gpio_chip, chip);
162 unsigned long flags;
163
164 if (!orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK))
165 return -EINVAL;
166
167 spin_lock_irqsave(&ochip->lock, flags);
168 __set_direction(ochip, pin, 1);
169 spin_unlock_irqrestore(&ochip->lock, flags);
170
171 return 0;
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200172}
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200173
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100174static int orion_gpio_get(struct gpio_chip *chip, unsigned pin)
175{
176 struct orion_gpio_chip *ochip =
177 container_of(chip, struct orion_gpio_chip, chip);
178 int val;
179
180 if (readl(GPIO_IO_CONF(ochip)) & (1 << pin)) {
181 val = readl(GPIO_DATA_IN(ochip)) ^ readl(GPIO_IN_POL(ochip));
182 } else {
183 val = readl(GPIO_OUT(ochip));
184 }
185
186 return (val >> pin) & 1;
187}
188
189static int
190orion_gpio_direction_output(struct gpio_chip *chip, unsigned pin, int value)
191{
192 struct orion_gpio_chip *ochip =
193 container_of(chip, struct orion_gpio_chip, chip);
194 unsigned long flags;
195
196 if (!orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
197 return -EINVAL;
198
199 spin_lock_irqsave(&ochip->lock, flags);
200 __set_blinking(ochip, pin, 0);
201 __set_level(ochip, pin, value);
202 __set_direction(ochip, pin, 0);
203 spin_unlock_irqrestore(&ochip->lock, flags);
204
205 return 0;
206}
207
208static void orion_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
209{
210 struct orion_gpio_chip *ochip =
211 container_of(chip, struct orion_gpio_chip, chip);
212 unsigned long flags;
213
214 spin_lock_irqsave(&ochip->lock, flags);
215 __set_level(ochip, pin, value);
216 spin_unlock_irqrestore(&ochip->lock, flags);
217}
218
219static int orion_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
220{
221 struct orion_gpio_chip *ochip =
222 container_of(chip, struct orion_gpio_chip, chip);
223
224 return ochip->secondary_irq_base + pin;
225}
226
227
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200228/*
229 * Orion-specific GPIO API extensions.
230 */
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100231static struct orion_gpio_chip *orion_gpio_chip_find(int pin)
232{
233 int i;
234
235 for (i = 0; i < orion_gpio_chip_count; i++) {
236 struct orion_gpio_chip *ochip = orion_gpio_chips + i;
237 struct gpio_chip *chip = &ochip->chip;
238
239 if (pin >= chip->base && pin < chip->base + chip->ngpio)
240 return ochip;
241 }
242
243 return NULL;
244}
245
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200246void __init orion_gpio_set_unused(unsigned pin)
247{
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100248 struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
249
250 if (ochip == NULL)
251 return;
252
253 pin -= ochip->chip.base;
254
Erik Benadaa8865652009-05-28 17:08:55 -0700255 /* Configure as output, drive low. */
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100256 __set_level(ochip, pin, 0);
257 __set_direction(ochip, pin, 0);
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200258}
259
Nicolas Pitre28d27cf2009-02-02 15:27:55 -0500260void __init orion_gpio_set_valid(unsigned pin, int mode)
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200261{
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100262 struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
263
264 if (ochip == NULL)
265 return;
266
267 pin -= ochip->chip.base;
268
Nicolas Pitre28d27cf2009-02-02 15:27:55 -0500269 if (mode == 1)
270 mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100271
Nicolas Pitre28d27cf2009-02-02 15:27:55 -0500272 if (mode & GPIO_INPUT_OK)
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100273 __set_bit(pin, &ochip->valid_input);
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200274 else
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100275 __clear_bit(pin, &ochip->valid_input);
276
Nicolas Pitre28d27cf2009-02-02 15:27:55 -0500277 if (mode & GPIO_OUTPUT_OK)
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100278 __set_bit(pin, &ochip->valid_output);
Nicolas Pitre28d27cf2009-02-02 15:27:55 -0500279 else
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100280 __clear_bit(pin, &ochip->valid_output);
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200281}
282
283void orion_gpio_set_blink(unsigned pin, int blink)
284{
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100285 struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200286 unsigned long flags;
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200287
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100288 if (ochip == NULL)
289 return;
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200290
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100291 spin_lock_irqsave(&ochip->lock, flags);
292 __set_level(ochip, pin, 0);
293 __set_blinking(ochip, pin, blink);
294 spin_unlock_irqrestore(&ochip->lock, flags);
Lennert Buytenhek9569dae2008-10-20 01:51:03 +0200295}
296EXPORT_SYMBOL(orion_gpio_set_blink);
Lennert Buytenhek07332312008-10-20 01:51:03 +0200297
298
299/*****************************************************************************
300 * Orion GPIO IRQ
301 *
302 * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
303 * value of the line or the opposite value.
304 *
305 * Level IRQ handlers: DATA_IN is used directly as cause register.
306 * Interrupt are masked by LEVEL_MASK registers.
307 * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
308 * Interrupt are masked by EDGE_MASK registers.
309 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
310 * the polarity to catch the next line transaction.
311 * This is a race condition that might not perfectly
312 * work on some use cases.
313 *
314 * Every eight GPIO lines are grouped (OR'ed) before going up to main
315 * cause register.
316 *
317 * EDGE cause mask
318 * data-in /--------| |-----| |----\
319 * -----| |----- ---- to main cause reg
320 * X \----------------| |----/
321 * polarity LEVEL mask
322 *
323 ****************************************************************************/
Lennert Buytenhek3b0c8d42010-11-29 11:17:38 +0100324static void gpio_irq_ack(struct irq_data *d)
Nicolas Pitrefd4b9b32009-02-17 20:45:50 +0100325{
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100326 struct orion_gpio_chip *ochip = irq_data_get_irq_chip_data(d);
327 int type;
328
329 type = irq_desc[d->irq].status & IRQ_TYPE_SENSE_MASK;
Nicolas Pitrefd4b9b32009-02-17 20:45:50 +0100330 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100331 int pin = d->irq - ochip->secondary_irq_base;
332
333 writel(~(1 << pin), GPIO_EDGE_CAUSE(ochip));
Nicolas Pitrefd4b9b32009-02-17 20:45:50 +0100334 }
Lennert Buytenhek07332312008-10-20 01:51:03 +0200335}
336
Lennert Buytenhek3b0c8d42010-11-29 11:17:38 +0100337static void gpio_irq_mask(struct irq_data *d)
Lennert Buytenhek07332312008-10-20 01:51:03 +0200338{
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100339 struct orion_gpio_chip *ochip = irq_data_get_irq_chip_data(d);
340 int type;
341 void __iomem *reg;
342 int pin;
343
344 type = irq_desc[d->irq].status & IRQ_TYPE_SENSE_MASK;
345 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
346 reg = GPIO_EDGE_MASK(ochip);
347 else
348 reg = GPIO_LEVEL_MASK(ochip);
349
350 pin = d->irq - ochip->secondary_irq_base;
351
352 writel(readl(reg) & ~(1 << pin), reg);
Lennert Buytenhek07332312008-10-20 01:51:03 +0200353}
354
Lennert Buytenhek3b0c8d42010-11-29 11:17:38 +0100355static void gpio_irq_unmask(struct irq_data *d)
Lennert Buytenhek07332312008-10-20 01:51:03 +0200356{
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100357 struct orion_gpio_chip *ochip = irq_data_get_irq_chip_data(d);
358 int type;
359 void __iomem *reg;
360 int pin;
361
362 type = irq_desc[d->irq].status & IRQ_TYPE_SENSE_MASK;
363 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
364 reg = GPIO_EDGE_MASK(ochip);
365 else
366 reg = GPIO_LEVEL_MASK(ochip);
367
368 pin = d->irq - ochip->secondary_irq_base;
369
370 writel(readl(reg) | (1 << pin), reg);
Lennert Buytenhek07332312008-10-20 01:51:03 +0200371}
372
Lennert Buytenhek3b0c8d42010-11-29 11:17:38 +0100373static int gpio_irq_set_type(struct irq_data *d, u32 type)
Lennert Buytenhek07332312008-10-20 01:51:03 +0200374{
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100375 struct orion_gpio_chip *ochip = irq_data_get_irq_chip_data(d);
376 int pin;
Lennert Buytenhek07332312008-10-20 01:51:03 +0200377 u32 u;
378
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100379 pin = d->irq - ochip->secondary_irq_base;
380
381 u = readl(GPIO_IO_CONF(ochip)) & (1 << pin);
Lennert Buytenhek07332312008-10-20 01:51:03 +0200382 if (!u) {
383 printk(KERN_ERR "orion gpio_irq_set_type failed "
Lennert Buytenhek3b0c8d42010-11-29 11:17:38 +0100384 "(irq %d, pin %d).\n", d->irq, pin);
Lennert Buytenhek07332312008-10-20 01:51:03 +0200385 return -EINVAL;
386 }
387
Lennert Buytenhek07332312008-10-20 01:51:03 +0200388 /*
389 * Set edge/level type.
390 */
391 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100392 set_irq_handler(d->irq, handle_edge_irq);
Lennert Buytenhek07332312008-10-20 01:51:03 +0200393 } else if (type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100394 set_irq_handler(d->irq, handle_level_irq);
Lennert Buytenhek07332312008-10-20 01:51:03 +0200395 } else {
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100396 printk(KERN_ERR "failed to set irq=%d (type=%d)\n",
397 d->irq, type);
Lennert Buytenhek07332312008-10-20 01:51:03 +0200398 return -EINVAL;
399 }
400
401 /*
402 * Configure interrupt polarity.
403 */
404 if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH) {
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100405 u = readl(GPIO_IN_POL(ochip));
406 u &= ~(1 << pin);
407 writel(u, GPIO_IN_POL(ochip));
Lennert Buytenhek07332312008-10-20 01:51:03 +0200408 } else if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW) {
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100409 u = readl(GPIO_IN_POL(ochip));
410 u |= 1 << pin;
411 writel(u, GPIO_IN_POL(ochip));
Lennert Buytenhek07332312008-10-20 01:51:03 +0200412 } else if (type == IRQ_TYPE_EDGE_BOTH) {
413 u32 v;
414
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100415 v = readl(GPIO_IN_POL(ochip)) ^ readl(GPIO_DATA_IN(ochip));
Lennert Buytenhek07332312008-10-20 01:51:03 +0200416
417 /*
418 * set initial polarity based on current input level
419 */
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100420 u = readl(GPIO_IN_POL(ochip));
421 if (v & (1 << pin))
422 u |= 1 << pin; /* falling */
Lennert Buytenhek07332312008-10-20 01:51:03 +0200423 else
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100424 u &= ~(1 << pin); /* rising */
425 writel(u, GPIO_IN_POL(ochip));
Lennert Buytenhek07332312008-10-20 01:51:03 +0200426 }
427
Lennert Buytenhek07332312008-10-20 01:51:03 +0200428 return 0;
429}
430
Nicolas Pitrefd4b9b32009-02-17 20:45:50 +0100431struct irq_chip orion_gpio_irq_chip = {
Erik Benadaa8865652009-05-28 17:08:55 -0700432 .name = "orion_gpio_irq",
Lennert Buytenhek3b0c8d42010-11-29 11:17:38 +0100433 .irq_ack = gpio_irq_ack,
434 .irq_mask = gpio_irq_mask,
435 .irq_unmask = gpio_irq_unmask,
436 .irq_set_type = gpio_irq_set_type,
Lennert Buytenhek07332312008-10-20 01:51:03 +0200437};
438
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100439void __init orion_gpio_init(int gpio_base, int ngpio,
440 u32 base, int mask_offset, int secondary_irq_base)
441{
442 struct orion_gpio_chip *ochip;
443 int i;
444
445 if (orion_gpio_chip_count == ARRAY_SIZE(orion_gpio_chips))
446 return;
447
448 ochip = orion_gpio_chips + orion_gpio_chip_count;
449 ochip->chip.label = "orion_gpio";
450 ochip->chip.request = orion_gpio_request;
451 ochip->chip.direction_input = orion_gpio_direction_input;
452 ochip->chip.get = orion_gpio_get;
453 ochip->chip.direction_output = orion_gpio_direction_output;
454 ochip->chip.set = orion_gpio_set;
455 ochip->chip.to_irq = orion_gpio_to_irq;
456 ochip->chip.base = gpio_base;
457 ochip->chip.ngpio = ngpio;
458 ochip->chip.can_sleep = 0;
459 spin_lock_init(&ochip->lock);
460 ochip->base = (void __iomem *)base;
461 ochip->valid_input = 0;
462 ochip->valid_output = 0;
463 ochip->mask_offset = mask_offset;
464 ochip->secondary_irq_base = secondary_irq_base;
465
466 gpiochip_add(&ochip->chip);
467
468 orion_gpio_chip_count++;
469
470 /*
471 * Mask and clear GPIO interrupts.
472 */
473 writel(0, GPIO_EDGE_CAUSE(ochip));
474 writel(0, GPIO_EDGE_MASK(ochip));
475 writel(0, GPIO_LEVEL_MASK(ochip));
476
477 for (i = 0; i < ngpio; i++) {
478 unsigned int irq = secondary_irq_base + i;
479
480 set_irq_chip(irq, &orion_gpio_irq_chip);
481 set_irq_handler(irq, handle_level_irq);
482 set_irq_chip_data(irq, ochip);
483 irq_desc[irq].status |= IRQ_LEVEL;
484 set_irq_flags(irq, IRQF_VALID);
485 }
486}
487
Lennert Buytenhek07332312008-10-20 01:51:03 +0200488void orion_gpio_irq_handler(int pinoff)
489{
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100490 struct orion_gpio_chip *ochip;
Lennert Buytenhek07332312008-10-20 01:51:03 +0200491 u32 cause;
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100492 int i;
Lennert Buytenhek07332312008-10-20 01:51:03 +0200493
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100494 ochip = orion_gpio_chip_find(pinoff);
495 if (ochip == NULL)
496 return;
Lennert Buytenhek07332312008-10-20 01:51:03 +0200497
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100498 cause = readl(GPIO_DATA_IN(ochip)) & readl(GPIO_LEVEL_MASK(ochip));
499 cause |= readl(GPIO_EDGE_CAUSE(ochip)) & readl(GPIO_EDGE_MASK(ochip));
Lennert Buytenhek07332312008-10-20 01:51:03 +0200500
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100501 for (i = 0; i < ochip->chip.ngpio; i++) {
502 int irq;
503 struct irq_desc *desc;
504
505 irq = ochip->secondary_irq_base + i;
506
507 if (!(cause & (1 << i)))
Lennert Buytenhek07332312008-10-20 01:51:03 +0200508 continue;
509
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100510 desc = irq_desc + irq;
Lennert Buytenhek07332312008-10-20 01:51:03 +0200511 if ((desc->status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
512 /* Swap polarity (race with GPIO line) */
513 u32 polarity;
514
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100515 polarity = readl(GPIO_IN_POL(ochip));
516 polarity ^= 1 << i;
517 writel(polarity, GPIO_IN_POL(ochip));
Lennert Buytenhek07332312008-10-20 01:51:03 +0200518 }
Lennert Buytenhek9eac6d02010-12-14 12:54:03 +0100519
Lennert Buytenhek07332312008-10-20 01:51:03 +0200520 desc_handle_irq(irq, desc);
521 }
522}