2 * Driver for NEC VR4100 series General-purpose I/O Unit.
4 * Copyright (C) 2002 MontaVista Software Inc.
5 * Author: Yoichi Yuasa <yyuasa@mvista.com or source@mvista.com>
6 * Copyright (C) 2003-2005 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/platform_device.h>
23 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/irq.h>
27 #include <linux/interrupt.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/spinlock.h>
31 #include <linux/types.h>
35 #include <asm/vr41xx/giu.h>
36 #include <asm/vr41xx/vr41xx.h>
38 MODULE_AUTHOR("Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>");
39 MODULE_DESCRIPTION("NEC VR4100 series General-purpose I/O Unit driver");
40 MODULE_LICENSE("GPL");
42 static int major
; /* default is dynamic major device number */
43 module_param(major
, int, 0);
44 MODULE_PARM_DESC(major
, "Major device number");
46 #define GIU_TYPE1_START 0x0b000100UL
47 #define GIU_TYPE1_SIZE 0x20UL
49 #define GIU_TYPE2_START 0x0f000140UL
50 #define GIU_TYPE2_SIZE 0x20UL
52 #define GIU_TYPE3_START 0x0f000140UL
53 #define GIU_TYPE3_SIZE 0x28UL
55 #define GIU_PULLUPDOWN_START 0x0b0002e0UL
56 #define GIU_PULLUPDOWN_SIZE 0x04UL
58 #define GIUIOSELL 0x00
59 #define GIUIOSELH 0x02
62 #define GIUINTSTATL 0x08
63 #define GIUINTSTATH 0x0a
64 #define GIUINTENL 0x0c
65 #define GIUINTENH 0x0e
66 #define GIUINTTYPL 0x10
67 #define GIUINTTYPH 0x12
68 #define GIUINTALSELL 0x14
69 #define GIUINTALSELH 0x16
70 #define GIUINTHTSELL 0x18
71 #define GIUINTHTSELH 0x1a
72 #define GIUPODATL 0x1c
73 #define GIUPODATEN 0x1c
74 #define GIUPODATH 0x1e
78 #define GIUFEDGEINHL 0x20
79 #define GIUFEDGEINHH 0x22
80 #define GIUREDGEINHL 0x24
81 #define GIUREDGEINHH 0x26
83 #define GIUUSEUPDN 0x1e0
84 #define GIUTERMUPDN 0x1e2
86 #define GPIO_HAS_PULLUPDOWN_IO 0x0001
87 #define GPIO_HAS_OUTPUT_ENABLE 0x0002
88 #define GPIO_HAS_INTERRUPT_EDGE_SELECT 0x0100
90 static spinlock_t giu_lock
;
91 static struct resource
*giu_resource1
;
92 static struct resource
*giu_resource2
;
93 static unsigned long giu_flags
;
94 static unsigned int giu_nr_pins
;
96 static void __iomem
*giu_base
;
98 #define giu_read(offset) readw(giu_base + (offset))
99 #define giu_write(offset, value) writew((value), giu_base + (offset))
101 #define GPIO_PIN_OF_IRQ(irq) ((irq) - GIU_IRQ_BASE)
102 #define GIUINT_HIGH_OFFSET 16
103 #define GIUINT_HIGH_MAX 32
105 static inline uint16_t giu_set(uint16_t offset
, uint16_t set
)
109 data
= giu_read(offset
);
111 giu_write(offset
, data
);
116 static inline uint16_t giu_clear(uint16_t offset
, uint16_t clear
)
120 data
= giu_read(offset
);
122 giu_write(offset
, data
);
127 static unsigned int startup_giuint_low_irq(unsigned int irq
)
131 pin
= GPIO_PIN_OF_IRQ(irq
);
132 giu_write(GIUINTSTATL
, 1 << pin
);
133 giu_set(GIUINTENL
, 1 << pin
);
138 static void shutdown_giuint_low_irq(unsigned int irq
)
140 giu_clear(GIUINTENL
, 1 << GPIO_PIN_OF_IRQ(irq
));
143 static void enable_giuint_low_irq(unsigned int irq
)
145 giu_set(GIUINTENL
, 1 << GPIO_PIN_OF_IRQ(irq
));
148 #define disable_giuint_low_irq shutdown_giuint_low_irq
150 static void ack_giuint_low_irq(unsigned int irq
)
154 pin
= GPIO_PIN_OF_IRQ(irq
);
155 giu_clear(GIUINTENL
, 1 << pin
);
156 giu_write(GIUINTSTATL
, 1 << pin
);
159 static void end_giuint_low_irq(unsigned int irq
)
161 if (!(irq_desc
[irq
].status
& (IRQ_DISABLED
| IRQ_INPROGRESS
)))
162 giu_set(GIUINTENL
, 1 << GPIO_PIN_OF_IRQ(irq
));
165 static struct hw_interrupt_type giuint_low_irq_type
= {
166 .typename
= "GIUINTL",
167 .startup
= startup_giuint_low_irq
,
168 .shutdown
= shutdown_giuint_low_irq
,
169 .enable
= enable_giuint_low_irq
,
170 .disable
= disable_giuint_low_irq
,
171 .ack
= ack_giuint_low_irq
,
172 .end
= end_giuint_low_irq
,
175 static unsigned int startup_giuint_high_irq(unsigned int irq
)
179 pin
= GPIO_PIN_OF_IRQ(irq
) - GIUINT_HIGH_OFFSET
;
180 giu_write(GIUINTSTATH
, 1 << pin
);
181 giu_set(GIUINTENH
, 1 << pin
);
186 static void shutdown_giuint_high_irq(unsigned int irq
)
188 giu_clear(GIUINTENH
, 1 << (GPIO_PIN_OF_IRQ(irq
) - GIUINT_HIGH_OFFSET
));
191 static void enable_giuint_high_irq(unsigned int irq
)
193 giu_set(GIUINTENH
, 1 << (GPIO_PIN_OF_IRQ(irq
) - GIUINT_HIGH_OFFSET
));
196 #define disable_giuint_high_irq shutdown_giuint_high_irq
198 static void ack_giuint_high_irq(unsigned int irq
)
202 pin
= GPIO_PIN_OF_IRQ(irq
) - GIUINT_HIGH_OFFSET
;
203 giu_clear(GIUINTENH
, 1 << pin
);
204 giu_write(GIUINTSTATH
, 1 << pin
);
207 static void end_giuint_high_irq(unsigned int irq
)
209 if (!(irq_desc
[irq
].status
& (IRQ_DISABLED
| IRQ_INPROGRESS
)))
210 giu_set(GIUINTENH
, 1 << (GPIO_PIN_OF_IRQ(irq
) - GIUINT_HIGH_OFFSET
));
213 static struct hw_interrupt_type giuint_high_irq_type
= {
214 .typename
= "GIUINTH",
215 .startup
= startup_giuint_high_irq
,
216 .shutdown
= shutdown_giuint_high_irq
,
217 .enable
= enable_giuint_high_irq
,
218 .disable
= disable_giuint_high_irq
,
219 .ack
= ack_giuint_high_irq
,
220 .end
= end_giuint_high_irq
,
223 static int giu_get_irq(unsigned int irq
, struct pt_regs
*regs
)
225 uint16_t pendl
, pendh
, maskl
, maskh
;
228 pendl
= giu_read(GIUINTSTATL
);
229 pendh
= giu_read(GIUINTSTATH
);
230 maskl
= giu_read(GIUINTENL
);
231 maskh
= giu_read(GIUINTENH
);
237 for (i
= 0; i
< 16; i
++) {
238 if (maskl
& (1 << i
))
242 for (i
= 0; i
< 16; i
++) {
243 if (maskh
& (1 << i
))
244 return GIU_IRQ(i
+ GIUINT_HIGH_OFFSET
);
248 printk(KERN_ERR
"spurious GIU interrupt: %04x(%04x),%04x(%04x)\n",
249 maskl
, pendl
, maskh
, pendh
);
251 atomic_inc(&irq_err_count
);
256 void vr41xx_set_irq_trigger(unsigned int pin
, irq_trigger_t trigger
, irq_signal_t signal
)
260 if (pin
< GIUINT_HIGH_OFFSET
) {
262 if (trigger
!= IRQ_TRIGGER_LEVEL
) {
263 giu_set(GIUINTTYPL
, mask
);
264 if (signal
== IRQ_SIGNAL_HOLD
)
265 giu_set(GIUINTHTSELL
, mask
);
267 giu_clear(GIUINTHTSELL
, mask
);
268 if (current_cpu_data
.cputype
== CPU_VR4133
) {
270 case IRQ_TRIGGER_EDGE_FALLING
:
271 giu_set(GIUFEDGEINHL
, mask
);
272 giu_clear(GIUREDGEINHL
, mask
);
274 case IRQ_TRIGGER_EDGE_RISING
:
275 giu_clear(GIUFEDGEINHL
, mask
);
276 giu_set(GIUREDGEINHL
, mask
);
279 giu_set(GIUFEDGEINHL
, mask
);
280 giu_set(GIUREDGEINHL
, mask
);
285 giu_clear(GIUINTTYPL
, mask
);
286 giu_clear(GIUINTHTSELL
, mask
);
288 giu_write(GIUINTSTATL
, mask
);
289 } else if (pin
< GIUINT_HIGH_MAX
) {
290 mask
= 1 << (pin
- GIUINT_HIGH_OFFSET
);
291 if (trigger
!= IRQ_TRIGGER_LEVEL
) {
292 giu_set(GIUINTTYPH
, mask
);
293 if (signal
== IRQ_SIGNAL_HOLD
)
294 giu_set(GIUINTHTSELH
, mask
);
296 giu_clear(GIUINTHTSELH
, mask
);
297 if (current_cpu_data
.cputype
== CPU_VR4133
) {
299 case IRQ_TRIGGER_EDGE_FALLING
:
300 giu_set(GIUFEDGEINHH
, mask
);
301 giu_clear(GIUREDGEINHH
, mask
);
303 case IRQ_TRIGGER_EDGE_RISING
:
304 giu_clear(GIUFEDGEINHH
, mask
);
305 giu_set(GIUREDGEINHH
, mask
);
308 giu_set(GIUFEDGEINHH
, mask
);
309 giu_set(GIUREDGEINHH
, mask
);
314 giu_clear(GIUINTTYPH
, mask
);
315 giu_clear(GIUINTHTSELH
, mask
);
317 giu_write(GIUINTSTATH
, mask
);
321 EXPORT_SYMBOL_GPL(vr41xx_set_irq_trigger
);
323 void vr41xx_set_irq_level(unsigned int pin
, irq_level_t level
)
327 if (pin
< GIUINT_HIGH_OFFSET
) {
329 if (level
== IRQ_LEVEL_HIGH
)
330 giu_set(GIUINTALSELL
, mask
);
332 giu_clear(GIUINTALSELL
, mask
);
333 giu_write(GIUINTSTATL
, mask
);
334 } else if (pin
< GIUINT_HIGH_MAX
) {
335 mask
= 1 << (pin
- GIUINT_HIGH_OFFSET
);
336 if (level
== IRQ_LEVEL_HIGH
)
337 giu_set(GIUINTALSELH
, mask
);
339 giu_clear(GIUINTALSELH
, mask
);
340 giu_write(GIUINTSTATH
, mask
);
344 EXPORT_SYMBOL_GPL(vr41xx_set_irq_level
);
346 gpio_data_t
vr41xx_gpio_get_pin(unsigned int pin
)
350 if (pin
>= giu_nr_pins
)
351 return GPIO_DATA_INVAL
;
354 reg
= giu_read(GIUPIODL
);
355 mask
= (uint16_t)1 << pin
;
356 } else if (pin
< 32) {
357 reg
= giu_read(GIUPIODH
);
358 mask
= (uint16_t)1 << (pin
- 16);
359 } else if (pin
< 48) {
360 reg
= giu_read(GIUPODATL
);
361 mask
= (uint16_t)1 << (pin
- 32);
363 reg
= giu_read(GIUPODATH
);
364 mask
= (uint16_t)1 << (pin
- 48);
368 return GPIO_DATA_HIGH
;
370 return GPIO_DATA_LOW
;
373 EXPORT_SYMBOL_GPL(vr41xx_gpio_get_pin
);
375 int vr41xx_gpio_set_pin(unsigned int pin
, gpio_data_t data
)
377 uint16_t offset
, mask
, reg
;
380 if (pin
>= giu_nr_pins
)
385 mask
= (uint16_t)1 << pin
;
386 } else if (pin
< 32) {
388 mask
= (uint16_t)1 << (pin
- 16);
389 } else if (pin
< 48) {
391 mask
= (uint16_t)1 << (pin
- 32);
394 mask
= (uint16_t)1 << (pin
- 48);
397 spin_lock_irqsave(&giu_lock
, flags
);
399 reg
= giu_read(offset
);
400 if (data
== GPIO_DATA_HIGH
)
404 giu_write(offset
, reg
);
406 spin_unlock_irqrestore(&giu_lock
, flags
);
411 EXPORT_SYMBOL_GPL(vr41xx_gpio_set_pin
);
413 int vr41xx_gpio_set_direction(unsigned int pin
, gpio_direction_t dir
)
415 uint16_t offset
, mask
, reg
;
418 if (pin
>= giu_nr_pins
)
423 mask
= (uint16_t)1 << pin
;
424 } else if (pin
< 32) {
426 mask
= (uint16_t)1 << (pin
- 16);
428 if (giu_flags
& GPIO_HAS_OUTPUT_ENABLE
) {
430 mask
= (uint16_t)1 << (pin
- 32);
447 spin_lock_irqsave(&giu_lock
, flags
);
449 reg
= giu_read(offset
);
450 if (dir
== GPIO_OUTPUT
)
454 giu_write(offset
, reg
);
456 spin_unlock_irqrestore(&giu_lock
, flags
);
461 EXPORT_SYMBOL_GPL(vr41xx_gpio_set_direction
);
463 int vr41xx_gpio_pullupdown(unsigned int pin
, gpio_pull_t pull
)
468 if ((giu_flags
& GPIO_HAS_PULLUPDOWN_IO
) != GPIO_HAS_PULLUPDOWN_IO
)
474 mask
= (uint16_t)1 << pin
;
476 spin_lock_irqsave(&giu_lock
, flags
);
478 if (pull
== GPIO_PULL_UP
|| pull
== GPIO_PULL_DOWN
) {
479 reg
= giu_read(GIUTERMUPDN
);
480 if (pull
== GPIO_PULL_UP
)
484 giu_write(GIUTERMUPDN
, reg
);
486 reg
= giu_read(GIUUSEUPDN
);
488 giu_write(GIUUSEUPDN
, reg
);
490 reg
= giu_read(GIUUSEUPDN
);
492 giu_write(GIUUSEUPDN
, reg
);
495 spin_unlock_irqrestore(&giu_lock
, flags
);
500 EXPORT_SYMBOL_GPL(vr41xx_gpio_pullupdown
);
502 static ssize_t
gpio_read(struct file
*file
, char __user
*buf
, size_t len
,
508 pin
= iminor(file
->f_dentry
->d_inode
);
509 if (pin
>= giu_nr_pins
)
512 if (vr41xx_gpio_get_pin(pin
) == GPIO_DATA_HIGH
)
518 if (put_user(value
, buf
))
524 static ssize_t
gpio_write(struct file
*file
, const char __user
*data
,
525 size_t len
, loff_t
*ppos
)
532 pin
= iminor(file
->f_dentry
->d_inode
);
533 if (pin
>= giu_nr_pins
)
536 for (i
= 0; i
< len
; i
++) {
537 if (get_user(c
, data
+ i
))
542 retval
= vr41xx_gpio_set_pin(pin
, GPIO_DATA_LOW
);
545 retval
= vr41xx_gpio_set_pin(pin
, GPIO_DATA_HIGH
);
548 printk(KERN_INFO
"GPIO%d: pull down\n", pin
);
549 retval
= vr41xx_gpio_pullupdown(pin
, GPIO_PULL_DOWN
);
552 printk(KERN_INFO
"GPIO%d: pull up/down disable\n", pin
);
553 retval
= vr41xx_gpio_pullupdown(pin
, GPIO_PULL_DISABLE
);
556 printk(KERN_INFO
"GPIO%d: input\n", pin
);
557 retval
= vr41xx_gpio_set_direction(pin
, GPIO_INPUT
);
560 printk(KERN_INFO
"GPIO%d: output\n", pin
);
561 retval
= vr41xx_gpio_set_direction(pin
, GPIO_OUTPUT
);
564 printk(KERN_INFO
"GPIO%d: output disable\n", pin
);
565 retval
= vr41xx_gpio_set_direction(pin
, GPIO_OUTPUT_DISABLE
);
568 printk(KERN_INFO
"GPIO%d: pull up\n", pin
);
569 retval
= vr41xx_gpio_pullupdown(pin
, GPIO_PULL_UP
);
572 printk(KERN_INFO
"GPIO%d: pull up/down disable\n", pin
);
573 retval
= vr41xx_gpio_pullupdown(pin
, GPIO_PULL_DISABLE
);
586 static int gpio_open(struct inode
*inode
, struct file
*file
)
591 if (pin
>= giu_nr_pins
)
594 return nonseekable_open(inode
, file
);
597 static int gpio_release(struct inode
*inode
, struct file
*file
)
602 if (pin
>= giu_nr_pins
)
608 static struct file_operations gpio_fops
= {
609 .owner
= THIS_MODULE
,
613 .release
= gpio_release
,
616 static int __devinit
giu_probe(struct platform_device
*dev
)
618 unsigned long start
, size
, flags
= 0;
619 unsigned int nr_pins
= 0;
620 struct resource
*res1
, *res2
= NULL
;
624 switch (current_cpu_data
.cputype
) {
627 start
= GIU_TYPE1_START
;
628 size
= GIU_TYPE1_SIZE
;
629 flags
= GPIO_HAS_PULLUPDOWN_IO
;
634 start
= GIU_TYPE2_START
;
635 size
= GIU_TYPE2_SIZE
;
639 start
= GIU_TYPE3_START
;
640 size
= GIU_TYPE3_SIZE
;
641 flags
= GPIO_HAS_INTERRUPT_EDGE_SELECT
;
648 res1
= request_mem_region(start
, size
, "GIU");
652 base
= ioremap(start
, size
);
654 release_resource(res1
);
658 if (flags
& GPIO_HAS_PULLUPDOWN_IO
) {
659 res2
= request_mem_region(GIU_PULLUPDOWN_START
, GIU_PULLUPDOWN_SIZE
, "GIU");
662 release_resource(res1
);
667 retval
= register_chrdev(major
, "GIU", &gpio_fops
);
670 release_resource(res1
);
671 release_resource(res2
);
677 printk(KERN_INFO
"GIU: major number %d\n", major
);
680 spin_lock_init(&giu_lock
);
682 giu_resource1
= res1
;
683 giu_resource2
= res2
;
685 giu_nr_pins
= nr_pins
;
687 giu_write(GIUINTENL
, 0);
688 giu_write(GIUINTENH
, 0);
690 for (i
= GIU_IRQ_BASE
; i
<= GIU_IRQ_LAST
; i
++) {
691 if (i
< GIU_IRQ(GIUINT_HIGH_OFFSET
))
692 irq_desc
[i
].handler
= &giuint_low_irq_type
;
694 irq_desc
[i
].handler
= &giuint_high_irq_type
;
697 return cascade_irq(GIUINT_IRQ
, giu_get_irq
);
700 static int __devexit
giu_remove(struct platform_device
*dev
)
704 release_resource(giu_resource1
);
705 if (giu_flags
& GPIO_HAS_PULLUPDOWN_IO
)
706 release_resource(giu_resource2
);
711 static struct platform_device
*giu_platform_device
;
713 static struct platform_driver giu_device_driver
= {
715 .remove
= __devexit_p(giu_remove
),
718 .owner
= THIS_MODULE
,
722 static int __init
vr41xx_giu_init(void)
726 giu_platform_device
= platform_device_alloc("GIU", -1);
727 if (!giu_platform_device
)
730 retval
= platform_device_add(giu_platform_device
);
732 platform_device_put(giu_platform_device
);
736 retval
= platform_driver_register(&giu_device_driver
);
738 platform_device_unregister(giu_platform_device
);
743 static void __exit
vr41xx_giu_exit(void)
745 platform_driver_unregister(&giu_device_driver
);
747 platform_device_unregister(giu_platform_device
);
750 module_init(vr41xx_giu_init
);
751 module_exit(vr41xx_giu_exit
);