blob: d1740643a1350c7f20496cde342b1b0fea4bcb39 [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) {
Ben Dooks6c3c5bb2007-01-16 12:33:35 +010060 function -= 1;
Ben Dooks42d3a122005-10-28 15:26:41 +010061 function &= 1;
62 function <<= S3C2410_GPIO_OFFSET(pin);
63 } else {
64 function &= 3;
65 function <<= S3C2410_GPIO_OFFSET(pin)*2;
66 }
67 }
68
69 /* modify the specified register wwith IRQs off */
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 local_irq_save(flags);
72
73 con = __raw_readl(base + 0x00);
74 con &= ~mask;
75 con |= function;
76
77 __raw_writel(con, base + 0x00);
78
79 local_irq_restore(flags);
80}
81
82EXPORT_SYMBOL(s3c2410_gpio_cfgpin);
83
84unsigned int s3c2410_gpio_getcfg(unsigned int pin)
85{
Lucas Correia Villa Real0ca5bc32006-02-01 21:24:23 +000086 void __iomem *base = S3C24XX_GPIO_BASE(pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned long mask;
88
89 if (pin < S3C2410_GPIO_BANKB) {
90 mask = 1 << S3C2410_GPIO_OFFSET(pin);
91 } else {
92 mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
93 }
94
95 return __raw_readl(base) & mask;
96}
97
98EXPORT_SYMBOL(s3c2410_gpio_getcfg);
99
100void s3c2410_gpio_pullup(unsigned int pin, unsigned int to)
101{
Lucas Correia Villa Real0ca5bc32006-02-01 21:24:23 +0000102 void __iomem *base = S3C24XX_GPIO_BASE(pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned long offs = S3C2410_GPIO_OFFSET(pin);
104 unsigned long flags;
105 unsigned long up;
106
107 if (pin < S3C2410_GPIO_BANKB)
108 return;
109
110 local_irq_save(flags);
111
112 up = __raw_readl(base + 0x08);
113 up &= ~(1L << offs);
114 up |= to << offs;
115 __raw_writel(up, base + 0x08);
116
117 local_irq_restore(flags);
118}
119
120EXPORT_SYMBOL(s3c2410_gpio_pullup);
121
122void s3c2410_gpio_setpin(unsigned int pin, unsigned int to)
123{
Lucas Correia Villa Real0ca5bc32006-02-01 21:24:23 +0000124 void __iomem *base = S3C24XX_GPIO_BASE(pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned long offs = S3C2410_GPIO_OFFSET(pin);
126 unsigned long flags;
127 unsigned long dat;
128
129 local_irq_save(flags);
130
131 dat = __raw_readl(base + 0x04);
132 dat &= ~(1 << offs);
133 dat |= to << offs;
134 __raw_writel(dat, base + 0x04);
135
136 local_irq_restore(flags);
137}
138
139EXPORT_SYMBOL(s3c2410_gpio_setpin);
140
141unsigned int s3c2410_gpio_getpin(unsigned int pin)
142{
Lucas Correia Villa Real0ca5bc32006-02-01 21:24:23 +0000143 void __iomem *base = S3C24XX_GPIO_BASE(pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned long offs = S3C2410_GPIO_OFFSET(pin);
145
146 return __raw_readl(base + 0x04) & (1<< offs);
147}
148
149EXPORT_SYMBOL(s3c2410_gpio_getpin);
150
151unsigned int s3c2410_modify_misccr(unsigned int clear, unsigned int change)
152{
153 unsigned long flags;
154 unsigned long misccr;
155
156 local_irq_save(flags);
Lucas Correia Villa Real0ca5bc32006-02-01 21:24:23 +0000157 misccr = __raw_readl(S3C24XX_MISCCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 misccr &= ~clear;
159 misccr ^= change;
Lucas Correia Villa Real0ca5bc32006-02-01 21:24:23 +0000160 __raw_writel(misccr, S3C24XX_MISCCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 local_irq_restore(flags);
162
163 return misccr;
164}
165
166EXPORT_SYMBOL(s3c2410_modify_misccr);
Ben Dooks94c52fd2006-10-30 02:27:45 +0100167
168int s3c2410_gpio_getirq(unsigned int pin)
169{
170 if (pin < S3C2410_GPF0 || pin > S3C2410_GPG15)
171 return -1; /* not valid interrupts */
172
173 if (pin < S3C2410_GPG0 && pin > S3C2410_GPF7)
174 return -1; /* not valid pin */
175
176 if (pin < S3C2410_GPF4)
177 return (pin - S3C2410_GPF0) + IRQ_EINT0;
178
179 if (pin < S3C2410_GPG0)
180 return (pin - S3C2410_GPF4) + IRQ_EINT4;
181
182 return (pin - S3C2410_GPG0) + IRQ_EINT8;
183}
184
185EXPORT_SYMBOL(s3c2410_gpio_getirq);