vserver 1.9.5.x5
[linux-2.6.git] / arch / arm / mach-s3c2410 / gpio.c
1 /* linux/arch/arm/mach-s3c2410/gpio.c
2  *
3  * Copyright (c) 2004 Simtec Electronics
4  * Ben Dooks <ben@simtec.co.uk>
5  *
6  * S3C2410 GPIO support
7  *
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.
12  *
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.
17  *
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
21  *
22  * Changelog
23  *      13-Sep-2004  BJD  Implemented change of MISCCR
24  *      14-Sep-2004  BJD  Added getpin call
25  *      14-Sep-2004  BJD  Fixed bug in setpin() call
26  *      30-Sep-2004  BJD  Fixed cfgpin() mask bug
27  *      01-Oct-2004  BJD  Added getcfg() to get pin configuration
28  *      01-Oct-2004  BJD  Fixed mask bug in pullup() call
29  *      01-Oct-2004  BJD  Added getirq() to turn pin into irqno
30  *      04-Oct-2004  BJD  Added irq filter controls for GPIO
31  *      05-Nov-2004  BJD  EXPORT_SYMBOL() added for all code
32  */
33
34
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/interrupt.h>
38 #include <linux/ioport.h>
39
40 #include <asm/hardware.h>
41 #include <asm/irq.h>
42 #include <asm/io.h>
43
44 #include <asm/arch/regs-gpio.h>
45
46 void s3c2410_gpio_cfgpin(unsigned int pin, unsigned int function)
47 {
48         unsigned long base = S3C2410_GPIO_BASE(pin);
49         unsigned long mask;
50         unsigned long con;
51         unsigned long flags;
52
53         if (pin < S3C2410_GPIO_BANKB) {
54                 mask = 1 << S3C2410_GPIO_OFFSET(pin);
55         } else {
56                 mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
57         }
58
59         local_irq_save(flags);
60
61         con  = __raw_readl(base + 0x00);
62         con &= ~mask;
63         con |= function;
64
65         __raw_writel(con, base + 0x00);
66
67         local_irq_restore(flags);
68 }
69
70 EXPORT_SYMBOL(s3c2410_gpio_cfgpin);
71
72 unsigned int s3c2410_gpio_getcfg(unsigned int pin)
73 {
74         unsigned long base = S3C2410_GPIO_BASE(pin);
75         unsigned long mask;
76
77         if (pin < S3C2410_GPIO_BANKB) {
78                 mask = 1 << S3C2410_GPIO_OFFSET(pin);
79         } else {
80                 mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
81         }
82
83         return __raw_readl(base) & mask;
84 }
85
86 EXPORT_SYMBOL(s3c2410_gpio_getcfg);
87
88 void s3c2410_gpio_pullup(unsigned int pin, unsigned int to)
89 {
90         unsigned long base = S3C2410_GPIO_BASE(pin);
91         unsigned long offs = S3C2410_GPIO_OFFSET(pin);
92         unsigned long flags;
93         unsigned long up;
94
95         if (pin < S3C2410_GPIO_BANKB)
96                 return;
97
98         local_irq_save(flags);
99
100         up = __raw_readl(base + 0x08);
101         up &= ~(1L << offs);
102         up |= to << offs;
103         __raw_writel(up, base + 0x08);
104
105         local_irq_restore(flags);
106 }
107
108 EXPORT_SYMBOL(s3c2410_gpio_pullup);
109
110 void s3c2410_gpio_setpin(unsigned int pin, unsigned int to)
111 {
112         unsigned long base = S3C2410_GPIO_BASE(pin);
113         unsigned long offs = S3C2410_GPIO_OFFSET(pin);
114         unsigned long flags;
115         unsigned long dat;
116
117         local_irq_save(flags);
118
119         dat = __raw_readl(base + 0x04);
120         dat &= ~(1 << offs);
121         dat |= to << offs;
122         __raw_writel(dat, base + 0x04);
123
124         local_irq_restore(flags);
125 }
126
127 EXPORT_SYMBOL(s3c2410_gpio_setpin);
128
129 unsigned int s3c2410_gpio_getpin(unsigned int pin)
130 {
131         unsigned long base = S3C2410_GPIO_BASE(pin);
132         unsigned long offs = S3C2410_GPIO_OFFSET(pin);
133
134         return __raw_readl(base + 0x04) & (1<< offs);
135 }
136
137 EXPORT_SYMBOL(s3c2410_gpio_getpin);
138
139 unsigned int s3c2410_modify_misccr(unsigned int clear, unsigned int change)
140 {
141         unsigned long flags;
142         unsigned long misccr;
143
144         local_irq_save(flags);
145         misccr = __raw_readl(S3C2410_MISCCR);
146         misccr &= ~clear;
147         misccr ^= change;
148         __raw_writel(misccr, S3C2410_MISCCR);
149         local_irq_restore(flags);
150
151         return misccr;
152 }
153
154 EXPORT_SYMBOL(s3c2410_modify_misccr);
155
156 int s3c2410_gpio_getirq(unsigned int pin)
157 {
158         if (pin < S3C2410_GPF0 || pin > S3C2410_GPG15_EINT23)
159                 return -1;      /* not valid interrupts */
160
161         if (pin < S3C2410_GPG0 && pin > S3C2410_GPF7)
162                 return -1;      /* not valid pin */
163
164         if (pin < S3C2410_GPF4)
165                 return (pin - S3C2410_GPF0) + IRQ_EINT0;
166
167         if (pin < S3C2410_GPG0)
168                 return (pin - S3C2410_GPF4) + IRQ_EINT4;
169
170         return (pin - S3C2410_GPG0) + IRQ_EINT8;
171 }
172
173 EXPORT_SYMBOL(s3c2410_gpio_getirq);
174
175 int s3c2410_gpio_irqfilter(unsigned int pin, unsigned int on,
176                            unsigned int config)
177 {
178         unsigned long reg = S3C2410_EINFLT0;
179         unsigned long flags;
180         unsigned long val;
181
182         if (pin < S3C2410_GPG8 || pin > S3C2410_GPG15)
183                 return -1;
184
185         config &= 0xff;
186
187         pin -= S3C2410_GPG8_EINT16;
188         reg += pin & ~3;
189
190         local_irq_save(flags);
191
192         /* update filter width and clock source */
193
194         val = __raw_readl(reg);
195         val &= ~(0xff << ((pin & 3) * 8));
196         val |= config << ((pin & 3) * 8);
197         __raw_writel(val, reg);
198
199         /* update filter enable */
200
201         val = __raw_readl(S3C2410_EXTINT2);
202         val &= ~(1 << ((pin * 4) + 3));
203         val |= on << ((pin * 4) + 3);
204         __raw_writel(val, S3C2410_EXTINT2);
205
206         local_irq_restore(flags);
207
208         return 0;
209 }
210
211 EXPORT_SYMBOL(s3c2410_gpio_irqfilter);