blob: 247f33d3a4078af864ae842225298d8bc17e2b54 [file] [log] [blame]
Florian Fainelli5e3a77e2008-01-30 13:33:36 +01001/*
Florian Fainellib2ef7492008-03-26 22:39:15 +01002 * GPIO support for RDC SoC R3210/R8610
Florian Fainelli5e3a77e2008-01-30 13:33:36 +01003 *
Florian Fainellib2ef7492008-03-26 22:39:15 +01004 * 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 *
Florian Fainelli5e3a77e2008-01-30 13:33:36 +010021 */
22
Florian Fainellib2ef7492008-03-26 22:39:15 +010023
24#include <linux/spinlock.h>
Florian Fainelli5e3a77e2008-01-30 13:33:36 +010025#include <linux/io.h>
26#include <linux/types.h>
27#include <linux/module.h>
Florian Fainelli5e3a77e2008-01-30 13:33:36 +010028
Florian Fainellib2ef7492008-03-26 22:39:15 +010029#include <asm/gpio.h>
Florian Fainelli5e3a77e2008-01-30 13:33:36 +010030#include <asm/mach-rdc321x/rdc321x_defs.h>
31
Florian Fainellib2ef7492008-03-26 22:39:15 +010032
33/* spin lock to protect our private copy of GPIO data register plus
34 the access to PCI conf registers. */
35static DEFINE_SPINLOCK(gpio_lock);
36
37/* copy of GPIO data registers */
38static u32 gpio_data_reg1;
39static u32 gpio_data_reg2;
40
41static u32 gpio_request_data[2];
42
43
44static inline void rdc321x_conf_write(unsigned addr, u32 value)
Florian Fainelli5e3a77e2008-01-30 13:33:36 +010045{
Florian Fainellib2ef7492008-03-26 22:39:15 +010046 outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
47 outl(value, RDC3210_CFGREG_DATA);
Florian Fainelli5e3a77e2008-01-30 13:33:36 +010048}
49
Florian Fainellib2ef7492008-03-26 22:39:15 +010050static inline void rdc321x_conf_or(unsigned addr, u32 value)
Florian Fainelli5e3a77e2008-01-30 13:33:36 +010051{
Florian Fainellib2ef7492008-03-26 22:39:15 +010052 outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
53 value |= inl(RDC3210_CFGREG_DATA);
54 outl(value, RDC3210_CFGREG_DATA);
Florian Fainelli5e3a77e2008-01-30 13:33:36 +010055}
56
Florian Fainellib2ef7492008-03-26 22:39:15 +010057static inline u32 rdc321x_conf_read(unsigned addr)
Florian Fainelli5e3a77e2008-01-30 13:33:36 +010058{
Florian Fainellib2ef7492008-03-26 22:39:15 +010059 outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
60
61 return inl(RDC3210_CFGREG_DATA);
Florian Fainelli5e3a77e2008-01-30 13:33:36 +010062}
63
Florian Fainellib2ef7492008-03-26 22:39:15 +010064/* configure pin as GPIO */
65static 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 must be called by the platform setup code. */
78void __init rdc321x_gpio_setup()
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
88/* determine, if gpio number is valid */
89static inline int rdc321x_is_gpio(unsigned gpio)
90{
91 return gpio <= RDC321X_MAX_GPIO;
92}
93
94/* request GPIO */
95int rdc_gpio_request(unsigned gpio, const char *label)
96{
97 unsigned long flags;
98
99 if (!rdc321x_is_gpio(gpio))
100 return -EINVAL;
101
102 spin_lock_irqsave(&gpio_lock, flags);
103 if (gpio_request_data[(gpio & 0x20) ? 1 : 0] & (1 << (gpio & 0x1f)))
104 goto inuse;
105 gpio_request_data[(gpio & 0x20) ? 1 : 0] |= (1 << (gpio & 0x1f));
106 spin_unlock_irqrestore(&gpio_lock, flags);
107
108 return 0;
109inuse:
110 spin_unlock_irqrestore(&gpio_lock, flags);
111 return -EINVAL;
112}
113EXPORT_SYMBOL(rdc_gpio_request);
114
115/* release previously-claimed GPIO */
116void rdc_gpio_free(unsigned gpio)
117{
118 unsigned long flags;
119
120 if (!rdc321x_is_gpio(gpio))
121 return;
122
123 spin_lock_irqsave(&gpio_lock, flags);
124 gpio_request_data[(gpio & 0x20) ? 1 : 0] &= ~(1 << (gpio & 0x1f));
125 spin_unlock_irqrestore(&gpio_lock, flags);
126}
127EXPORT_SYMBOL(rdc_gpio_free);
128
129/* read GPIO pin */
Florian Fainelli5e3a77e2008-01-30 13:33:36 +0100130int rdc_gpio_get_value(unsigned gpio)
131{
Florian Fainellib2ef7492008-03-26 22:39:15 +0100132 u32 reg;
133 unsigned long flags;
134
135 spin_lock_irqsave(&gpio_lock, flags);
136 reg = rdc321x_conf_read(gpio < 32
137 ? RDC321X_GPIO_DATA_REG1 : RDC321X_GPIO_DATA_REG2);
138 spin_unlock_irqrestore(&gpio_lock, flags);
139
140 return (1 << (gpio & 0x1f)) & reg ? 1 : 0;
Florian Fainelli5e3a77e2008-01-30 13:33:36 +0100141}
142EXPORT_SYMBOL(rdc_gpio_get_value);
143
Florian Fainellib2ef7492008-03-26 22:39:15 +0100144/* set GPIO pin to value */
Florian Fainelli5e3a77e2008-01-30 13:33:36 +0100145void rdc_gpio_set_value(unsigned gpio, int value)
146{
Florian Fainellib2ef7492008-03-26 22:39:15 +0100147 unsigned long flags;
148 u32 reg;
Florian Fainelli5e3a77e2008-01-30 13:33:36 +0100149
Florian Fainellib2ef7492008-03-26 22:39:15 +0100150 reg = 1 << (gpio & 0x1f);
151 if (gpio < 32) {
152 spin_lock_irqsave(&gpio_lock, flags);
153 if (value)
154 gpio_data_reg1 |= reg;
155 else
156 gpio_data_reg1 &= ~reg;
157 rdc321x_conf_write(RDC321X_GPIO_DATA_REG1, gpio_data_reg1);
158 spin_unlock_irqrestore(&gpio_lock, flags);
159 } else {
160 spin_lock_irqsave(&gpio_lock, flags);
161 if (value)
162 gpio_data_reg2 |= reg;
163 else
164 gpio_data_reg2 &= ~reg;
165 rdc321x_conf_write(RDC321X_GPIO_DATA_REG2, gpio_data_reg2);
166 spin_unlock_irqrestore(&gpio_lock, flags);
167 }
Florian Fainelli5e3a77e2008-01-30 13:33:36 +0100168}
169EXPORT_SYMBOL(rdc_gpio_set_value);
170
Florian Fainellib2ef7492008-03-26 22:39:15 +0100171/* configure GPIO pin as input */
Florian Fainelli5e3a77e2008-01-30 13:33:36 +0100172int rdc_gpio_direction_input(unsigned gpio)
173{
Florian Fainellib2ef7492008-03-26 22:39:15 +0100174 if (!rdc321x_is_gpio(gpio))
175 return -EINVAL;
176
177 rdc321x_configure_gpio(gpio);
178
Florian Fainelli5e3a77e2008-01-30 13:33:36 +0100179 return 0;
180}
181EXPORT_SYMBOL(rdc_gpio_direction_input);
182
Florian Fainellib2ef7492008-03-26 22:39:15 +0100183/* configure GPIO pin as output and set value */
Florian Fainelli5e3a77e2008-01-30 13:33:36 +0100184int rdc_gpio_direction_output(unsigned gpio, int value)
185{
Florian Fainellib2ef7492008-03-26 22:39:15 +0100186 if (!rdc321x_is_gpio(gpio))
187 return -EINVAL;
188
189 gpio_set_value(gpio, value);
190 rdc321x_configure_gpio(gpio);
191
Florian Fainelli5e3a77e2008-01-30 13:33:36 +0100192 return 0;
193}
194EXPORT_SYMBOL(rdc_gpio_direction_output);