blob: ba346546150b6b1cbb86ee2588ad15e6eaa0a726 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* linux/arch/arm/mach-s3c2410/gpio.c
2 *
3 * Copyright (c) 2004-2005 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
5 *
Ben Dooks94c52fd2006-10-30 02:27:45 +01006 * S3C24XX GPIO support
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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
Ben Dooks7f61a842006-09-20 20:54:54 +010021*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23
24#include <linux/kernel.h>
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/interrupt.h>
28#include <linux/ioport.h>
29
30#include <asm/hardware.h>
31#include <asm/irq.h>
32#include <asm/io.h>
33
34#include <asm/arch/regs-gpio.h>
35
36void s3c2410_gpio_cfgpin(unsigned int pin, unsigned int function)
37{
Lucas Correia Villa Real0ca5bc32006-02-01 21:24:23 +000038 void __iomem *base = S3C24XX_GPIO_BASE(pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 unsigned long mask;
40 unsigned long con;
41 unsigned long flags;
42
43 if (pin < S3C2410_GPIO_BANKB) {
44 mask = 1 << S3C2410_GPIO_OFFSET(pin);
45 } else {
46 mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
47 }
48
Ben Dooks42d3a122005-10-28 15:26:41 +010049 switch (function) {
50 case S3C2410_GPIO_LEAVE:
51 mask = 0;
52 function = 0;
53 break;
54
55 case S3C2410_GPIO_INPUT:
56 case S3C2410_GPIO_OUTPUT:
57 case S3C2410_GPIO_SFN2:
58 case S3C2410_GPIO_SFN3:
59 if (pin < S3C2410_GPIO_BANKB) {
60 function &= 1;
61 function <<= S3C2410_GPIO_OFFSET(pin);
62 } else {
63 function &= 3;
64 function <<= S3C2410_GPIO_OFFSET(pin)*2;
65 }
66 }
67
68 /* modify the specified register wwith IRQs off */
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 local_irq_save(flags);
71
72 con = __raw_readl(base + 0x00);
73 con &= ~mask;
74 con |= function;
75
76 __raw_writel(con, base + 0x00);
77
78 local_irq_restore(flags);
79}
80
81EXPORT_SYMBOL(s3c2410_gpio_cfgpin);
82
83unsigned int s3c2410_gpio_getcfg(unsigned int pin)
84{
Lucas Correia Villa Real0ca5bc32006-02-01 21:24:23 +000085 void __iomem *base = S3C24XX_GPIO_BASE(pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 unsigned long mask;
87
88 if (pin < S3C2410_GPIO_BANKB) {
89 mask = 1 << S3C2410_GPIO_OFFSET(pin);
90 } else {
91 mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
92 }
93
94 return __raw_readl(base) & mask;
95}
96
97EXPORT_SYMBOL(s3c2410_gpio_getcfg);
98
99void s3c2410_gpio_pullup(unsigned int pin, unsigned int to)
100{
Lucas Correia Villa Real0ca5bc32006-02-01 21:24:23 +0000101 void __iomem *base = S3C24XX_GPIO_BASE(pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 unsigned long offs = S3C2410_GPIO_OFFSET(pin);
103 unsigned long flags;
104 unsigned long up;
105
106 if (pin < S3C2410_GPIO_BANKB)
107 return;
108
109 local_irq_save(flags);
110
111 up = __raw_readl(base + 0x08);
112 up &= ~(1L << offs);
113 up |= to << offs;
114 __raw_writel(up, base + 0x08);
115
116 local_irq_restore(flags);
117}
118
119EXPORT_SYMBOL(s3c2410_gpio_pullup);
120
121void s3c2410_gpio_setpin(unsigned int pin, unsigned int to)
122{
Lucas Correia Villa Real0ca5bc32006-02-01 21:24:23 +0000123 void __iomem *base = S3C24XX_GPIO_BASE(pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 unsigned long offs = S3C2410_GPIO_OFFSET(pin);
125 unsigned long flags;
126 unsigned long dat;
127
128 local_irq_save(flags);
129
130 dat = __raw_readl(base + 0x04);
131 dat &= ~(1 << offs);
132 dat |= to << offs;
133 __raw_writel(dat, base + 0x04);
134
135 local_irq_restore(flags);
136}
137
138EXPORT_SYMBOL(s3c2410_gpio_setpin);
139
140unsigned int s3c2410_gpio_getpin(unsigned int pin)
141{
Lucas Correia Villa Real0ca5bc32006-02-01 21:24:23 +0000142 void __iomem *base = S3C24XX_GPIO_BASE(pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 unsigned long offs = S3C2410_GPIO_OFFSET(pin);
144
145 return __raw_readl(base + 0x04) & (1<< offs);
146}
147
148EXPORT_SYMBOL(s3c2410_gpio_getpin);
149
150unsigned int s3c2410_modify_misccr(unsigned int clear, unsigned int change)
151{
152 unsigned long flags;
153 unsigned long misccr;
154
155 local_irq_save(flags);
Lucas Correia Villa Real0ca5bc32006-02-01 21:24:23 +0000156 misccr = __raw_readl(S3C24XX_MISCCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 misccr &= ~clear;
158 misccr ^= change;
Lucas Correia Villa Real0ca5bc32006-02-01 21:24:23 +0000159 __raw_writel(misccr, S3C24XX_MISCCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 local_irq_restore(flags);
161
162 return misccr;
163}
164
165EXPORT_SYMBOL(s3c2410_modify_misccr);
Ben Dooks94c52fd2006-10-30 02:27:45 +0100166
167int s3c2410_gpio_getirq(unsigned int pin)
168{
169 if (pin < S3C2410_GPF0 || pin > S3C2410_GPG15)
170 return -1; /* not valid interrupts */
171
172 if (pin < S3C2410_GPG0 && pin > S3C2410_GPF7)
173 return -1; /* not valid pin */
174
175 if (pin < S3C2410_GPF4)
176 return (pin - S3C2410_GPF0) + IRQ_EINT0;
177
178 if (pin < S3C2410_GPG0)
179 return (pin - S3C2410_GPF4) + IRQ_EINT4;
180
181 return (pin - S3C2410_GPG0) + IRQ_EINT8;
182}
183
184EXPORT_SYMBOL(s3c2410_gpio_getirq);