aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/mach-rdc321x/gpio.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2008-03-27 16:20:47 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2008-03-27 16:20:47 -0400
commitfb8c7fb25d7d754a992481e9f763ec0b5889c4d9 (patch)
treed43cd30836988e263ddeecd54a6c74e9ac03aad2 /arch/x86/mach-rdc321x/gpio.c
parent074fcab574a36edec35d2e74955250f2e17d6000 (diff)
parent2e8fe719b57bbdc9e313daed1204bb55fed3ed44 (diff)
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/x86/linux-2.6-x86
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/x86/linux-2.6-x86: xen: fix UP setup of shared_info xen: fix RMW when unmasking events x86, documentation: nmi_watchdog=2 works on x86_64 x86: stricter check in follow_huge_addr() rdc321x: GPIO routines bugfixes x86: ptrace.c: fix defined-but-unused warnings x86: fix prefetch workaround
Diffstat (limited to 'arch/x86/mach-rdc321x/gpio.c')
-rw-r--r--arch/x86/mach-rdc321x/gpio.c199
1 files changed, 151 insertions, 48 deletions
diff --git a/arch/x86/mach-rdc321x/gpio.c b/arch/x86/mach-rdc321x/gpio.c
index 031269163bd6..247f33d3a407 100644
--- a/arch/x86/mach-rdc321x/gpio.c
+++ b/arch/x86/mach-rdc321x/gpio.c
@@ -1,91 +1,194 @@
1/* 1/*
2 * Copyright (C) 2007, OpenWrt.org, Florian Fainelli <florian@openwrt.org> 2 * GPIO support for RDC SoC R3210/R8610
3 * RDC321x architecture specific GPIO support 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.
4 * 20 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 */ 21 */
10 22
11#include <linux/autoconf.h> 23
12#include <linux/init.h> 24#include <linux/spinlock.h>
13#include <linux/io.h> 25#include <linux/io.h>
14#include <linux/types.h> 26#include <linux/types.h>
15#include <linux/module.h> 27#include <linux/module.h>
16#include <linux/delay.h>
17 28
29#include <asm/gpio.h>
18#include <asm/mach-rdc321x/rdc321x_defs.h> 30#include <asm/mach-rdc321x/rdc321x_defs.h>
19 31
20static inline int rdc_gpio_is_valid(unsigned gpio) 32
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)
21{ 45{
22 return (gpio <= RDC_MAX_GPIO); 46 outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
47 outl(value, RDC3210_CFGREG_DATA);
23} 48}
24 49
25static unsigned int rdc_gpio_read(unsigned gpio) 50static inline void rdc321x_conf_or(unsigned addr, u32 value)
26{ 51{
27 unsigned int val; 52 outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
28 53 value |= inl(RDC3210_CFGREG_DATA);
29 val = 0x80000000 | (7 << 11) | ((gpio&0x20?0x84:0x48)); 54 outl(value, RDC3210_CFGREG_DATA);
30 outl(val, RDC3210_CFGREG_ADDR);
31 udelay(10);
32 val = inl(RDC3210_CFGREG_DATA);
33 val |= (0x1 << (gpio & 0x1F));
34 outl(val, RDC3210_CFGREG_DATA);
35 udelay(10);
36 val = 0x80000000 | (7 << 11) | ((gpio&0x20?0x88:0x4C));
37 outl(val, RDC3210_CFGREG_ADDR);
38 udelay(10);
39 val = inl(RDC3210_CFGREG_DATA);
40
41 return val;
42} 55}
43 56
44static void rdc_gpio_write(unsigned int val) 57static inline u32 rdc321x_conf_read(unsigned addr)
45{ 58{
46 if (val) { 59 outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
47 outl(val, RDC3210_CFGREG_DATA); 60
48 udelay(10); 61 return inl(RDC3210_CFGREG_DATA);
49 }
50} 62}
51 63
52int rdc_gpio_get_value(unsigned gpio) 64/* 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)
53{ 96{
54 if (rdc_gpio_is_valid(gpio)) 97 unsigned long flags;
55 return (int)rdc_gpio_read(gpio); 98
56 else 99 if (!rdc321x_is_gpio(gpio))
57 return -EINVAL; 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;
58} 112}
59EXPORT_SYMBOL(rdc_gpio_get_value); 113EXPORT_SYMBOL(rdc_gpio_request);
60 114
61void rdc_gpio_set_value(unsigned gpio, int value) 115/* release previously-claimed GPIO */
116void rdc_gpio_free(unsigned gpio)
62{ 117{
63 unsigned int val; 118 unsigned long flags;
64 119
65 if (!rdc_gpio_is_valid(gpio)) 120 if (!rdc321x_is_gpio(gpio))
66 return; 121 return;
67 122
68 val = rdc_gpio_read(gpio); 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 */
130int rdc_gpio_get_value(unsigned gpio)
131{
132 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);
69 139
70 if (value) 140 return (1 << (gpio & 0x1f)) & reg ? 1 : 0;
71 val &= ~(0x1 << (gpio & 0x1F)); 141}
72 else 142EXPORT_SYMBOL(rdc_gpio_get_value);
73 val |= (0x1 << (gpio & 0x1F));
74 143
75 rdc_gpio_write(val); 144/* set GPIO pin to value */
145void rdc_gpio_set_value(unsigned gpio, int value)
146{
147 unsigned long flags;
148 u32 reg;
149
150 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 }
76} 168}
77EXPORT_SYMBOL(rdc_gpio_set_value); 169EXPORT_SYMBOL(rdc_gpio_set_value);
78 170
171/* configure GPIO pin as input */
79int rdc_gpio_direction_input(unsigned gpio) 172int rdc_gpio_direction_input(unsigned gpio)
80{ 173{
174 if (!rdc321x_is_gpio(gpio))
175 return -EINVAL;
176
177 rdc321x_configure_gpio(gpio);
178
81 return 0; 179 return 0;
82} 180}
83EXPORT_SYMBOL(rdc_gpio_direction_input); 181EXPORT_SYMBOL(rdc_gpio_direction_input);
84 182
183/* configure GPIO pin as output and set value */
85int rdc_gpio_direction_output(unsigned gpio, int value) 184int rdc_gpio_direction_output(unsigned gpio, int value)
86{ 185{
186 if (!rdc321x_is_gpio(gpio))
187 return -EINVAL;
188
189 gpio_set_value(gpio, value);
190 rdc321x_configure_gpio(gpio);
191
87 return 0; 192 return 0;
88} 193}
89EXPORT_SYMBOL(rdc_gpio_direction_output); 194EXPORT_SYMBOL(rdc_gpio_direction_output);
90
91