92df2201df6af39e39799afeae9c84f548360d8c
[openwrt.git] / target / linux / rdc / files-2.6.30 / arch / x86 / mach-rdc321x / gpio.c
1 /*
2  *  GPIO support for RDC SoC R3210/R8610
3  *
4  *  Copyright (C) 2007, Florian Fainelli <florian@openwrt.org>
5  *  Copyright (C) 2008, Volker Weiss <dev@tintuc.de>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23
24 #include <linux/spinlock.h>
25 #include <linux/io.h>
26 #include <linux/types.h>
27 #include <linux/module.h>
28
29 #include <asm/rdc321x_gpio.h>
30 #include <asm/rdc321x_defs.h>
31
32
33 /* spin lock to protect our private copy of GPIO data register plus
34    the access to PCI conf registers. */
35 static DEFINE_SPINLOCK(gpio_lock);
36
37 /* copy of GPIO data registers */
38 static u32 gpio_data_reg1;
39 static u32 gpio_data_reg2;
40
41 static u32 gpio_request_data[2];
42
43
44 static inline void rdc321x_conf_write(unsigned addr, u32 value)
45 {
46         outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
47         outl(value, RDC3210_CFGREG_DATA);
48 }
49
50 static inline void rdc321x_conf_or(unsigned addr, u32 value)
51 {
52         outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
53         value |= inl(RDC3210_CFGREG_DATA);
54         outl(value, RDC3210_CFGREG_DATA);
55 }
56
57 static inline u32 rdc321x_conf_read(unsigned addr)
58 {
59         outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
60
61         return inl(RDC3210_CFGREG_DATA);
62 }
63
64 /* configure pin as GPIO */
65 static void rdc321x_configure_gpio(unsigned gpio)
66 {
67         unsigned long flags;
68
69         spin_lock_irqsave(&gpio_lock, flags);
70         rdc321x_conf_or(gpio < 32
71                 ? RDC321X_GPIO_CTRL_REG1 : RDC321X_GPIO_CTRL_REG2,
72                 1 << (gpio & 0x1f));
73         spin_unlock_irqrestore(&gpio_lock, flags);
74 }
75
76 /* initially setup the 2 copies of the gpio data registers.
77    This function is called before the platform setup code. */
78 static int __init rdc321x_gpio_setup(void)
79 {
80         /* this might not be, what others (BIOS, bootloader, etc.)
81            wrote to these registers before, but it's a good guess. Still
82            better than just using 0xffffffff. */
83
84         gpio_data_reg1 = rdc321x_conf_read(RDC321X_GPIO_DATA_REG1);
85         gpio_data_reg2 = rdc321x_conf_read(RDC321X_GPIO_DATA_REG2);
86
87         return 0;
88 }
89
90 /* determine, if gpio number is valid */
91 static inline int rdc321x_is_gpio(unsigned gpio)
92 {
93         return gpio <= RDC321X_MAX_GPIO;
94 }
95
96 /* request GPIO */
97 int rdc_gpio_request(unsigned gpio, const char *label)
98 {
99         unsigned long flags;
100
101         if (!rdc321x_is_gpio(gpio))
102                 return -EINVAL;
103
104         spin_lock_irqsave(&gpio_lock, flags);
105         if (gpio_request_data[(gpio & 0x20) ? 1 : 0] & (1 << (gpio & 0x1f)))
106                 goto inuse;
107         gpio_request_data[(gpio & 0x20) ? 1 : 0] |= (1 << (gpio & 0x1f));
108         spin_unlock_irqrestore(&gpio_lock, flags);
109
110         return 0;
111 inuse:
112         spin_unlock_irqrestore(&gpio_lock, flags);
113         return -EINVAL;
114 }
115 EXPORT_SYMBOL(rdc_gpio_request);
116
117 /* release previously-claimed GPIO */
118 void rdc_gpio_free(unsigned gpio)
119 {
120         unsigned long flags;
121
122         if (!rdc321x_is_gpio(gpio))
123                 return;
124
125         spin_lock_irqsave(&gpio_lock, flags);
126         gpio_request_data[(gpio & 0x20) ? 1 : 0] &= ~(1 << (gpio & 0x1f));
127         spin_unlock_irqrestore(&gpio_lock, flags);
128 }
129 EXPORT_SYMBOL(rdc_gpio_free);
130
131 /* read GPIO pin */
132 int rdc_gpio_get_value(unsigned gpio)
133 {
134         u32 reg;
135         unsigned long flags;
136
137         spin_lock_irqsave(&gpio_lock, flags);
138         reg = rdc321x_conf_read(gpio < 32
139                 ? RDC321X_GPIO_DATA_REG1 : RDC321X_GPIO_DATA_REG2);
140         spin_unlock_irqrestore(&gpio_lock, flags);
141
142         return (1 << (gpio & 0x1f)) & reg ? 1 : 0;
143 }
144 EXPORT_SYMBOL(rdc_gpio_get_value);
145
146 /* set GPIO pin to value */
147 void rdc_gpio_set_value(unsigned gpio, int value)
148 {
149         unsigned long flags;
150         u32 reg;
151
152         reg = 1 << (gpio & 0x1f);
153         if (gpio < 32) {
154                 spin_lock_irqsave(&gpio_lock, flags);
155                 if (value)
156                         gpio_data_reg1 |= reg;
157                 else
158                         gpio_data_reg1 &= ~reg;
159                 rdc321x_conf_write(RDC321X_GPIO_DATA_REG1, gpio_data_reg1);
160                 spin_unlock_irqrestore(&gpio_lock, flags);
161         } else {
162                 spin_lock_irqsave(&gpio_lock, flags);
163                 if (value)
164                         gpio_data_reg2 |= reg;
165                 else
166                         gpio_data_reg2 &= ~reg;
167                 rdc321x_conf_write(RDC321X_GPIO_DATA_REG2, gpio_data_reg2);
168                 spin_unlock_irqrestore(&gpio_lock, flags);
169         }
170 }
171 EXPORT_SYMBOL(rdc_gpio_set_value);
172
173 /* configure GPIO pin as input */
174 int rdc_gpio_direction_input(unsigned gpio)
175 {
176         if (!rdc321x_is_gpio(gpio))
177                 return -EINVAL;
178
179         rdc321x_configure_gpio(gpio);
180
181         return 0;
182 }
183 EXPORT_SYMBOL(rdc_gpio_direction_input);
184
185 /* configure GPIO pin as output and set value */
186 int rdc_gpio_direction_output(unsigned gpio, int value)
187 {
188         if (!rdc321x_is_gpio(gpio))
189                 return -EINVAL;
190
191         gpio_set_value(gpio, value);
192         rdc321x_configure_gpio(gpio);
193
194         return 0;
195 }
196 EXPORT_SYMBOL(rdc_gpio_direction_output);
197
198 arch_initcall(rdc321x_gpio_setup);