blob: 7b09a2f7d37d8488c3218e7d4bf58b6d7f895171 [file] [log] [blame]
Stefan Roesef6c7b762007-03-24 15:45:34 +01001/*
2 * (C) Copyright 2007
3 * Stefan Roese, DENX Software Engineering, sr@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (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,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <asm/processor.h>
26#include <asm/io.h>
27#include <asm/gpio.h>
28
Stefan Roese1bca9192007-11-15 14:23:55 +010029#if defined(CFG_4xx_GPIO_TABLE)
30gpio_param_s gpio_tab[GPIO_GROUP_MAX][GPIO_MAX] = CFG_4xx_GPIO_TABLE;
Stefan Roesef6c7b762007-03-24 15:45:34 +010031#endif
32
33#if defined(GPIO0_OSRL)
34/* Only some 4xx variants support alternate funtions on the GPIO's */
35void gpio_config(int pin, int in_out, int gpio_alt, int out_val)
36{
37 u32 mask;
38 u32 mask2;
39 u32 val;
40 u32 offs = 0;
41 u32 offs2 = 0;
42 int pin2 = pin << 1;
43
44 if (pin >= GPIO_MAX) {
45 offs = 0x100;
46 pin -= GPIO_MAX;
47 }
48
49 if (pin >= GPIO_MAX/2) {
Stefan Roese3c59eed2007-12-11 13:38:19 +010050 offs2 = 0x4;
Stefan Roesef6c7b762007-03-24 15:45:34 +010051 pin2 = (pin - GPIO_MAX/2) << 1;
52 }
53
54 mask = 0x80000000 >> pin;
55 mask2 = 0xc0000000 >> (pin2 << 1);
56
57 /* first set TCR to 0 */
Stefan Roese1bca9192007-11-15 14:23:55 +010058 out_be32((void *)GPIO0_TCR + offs, in_be32((void *)GPIO0_TCR + offs) & ~mask);
Stefan Roesef6c7b762007-03-24 15:45:34 +010059
60 if (in_out == GPIO_OUT) {
Stefan Roese1bca9192007-11-15 14:23:55 +010061 val = in_be32((void *)GPIO0_OSRL + offs + offs2) & ~mask2;
Stefan Roesef6c7b762007-03-24 15:45:34 +010062 switch (gpio_alt) {
63 case GPIO_ALT1:
64 val |= GPIO_ALT1_SEL >> pin2;
65 break;
66 case GPIO_ALT2:
67 val |= GPIO_ALT2_SEL >> pin2;
68 break;
69 case GPIO_ALT3:
70 val |= GPIO_ALT3_SEL >> pin2;
71 break;
72 }
Stefan Roese1bca9192007-11-15 14:23:55 +010073 out_be32((void *)GPIO0_OSRL + offs + offs2, val);
Stefan Roesef6c7b762007-03-24 15:45:34 +010074
75 /* setup requested output value */
76 if (out_val == GPIO_OUT_0)
Stefan Roese1bca9192007-11-15 14:23:55 +010077 out_be32((void *)GPIO0_OR + offs,
78 in_be32((void *)GPIO0_OR + offs) & ~mask);
Stefan Roesef6c7b762007-03-24 15:45:34 +010079 else if (out_val == GPIO_OUT_1)
Stefan Roese1bca9192007-11-15 14:23:55 +010080 out_be32((void *)GPIO0_OR + offs,
81 in_be32((void *)GPIO0_OR + offs) | mask);
Stefan Roesef6c7b762007-03-24 15:45:34 +010082
83 /* now configure TCR to drive output if selected */
Stefan Roese1bca9192007-11-15 14:23:55 +010084 out_be32((void *)GPIO0_TCR + offs,
85 in_be32((void *)GPIO0_TCR + offs) | mask);
Stefan Roesef6c7b762007-03-24 15:45:34 +010086 } else {
Stefan Roese1bca9192007-11-15 14:23:55 +010087 val = in_be32((void *)GPIO0_ISR1L + offs + offs2) & ~mask2;
Stefan Roesef6c7b762007-03-24 15:45:34 +010088 val |= GPIO_IN_SEL >> pin2;
Stefan Roese1bca9192007-11-15 14:23:55 +010089 out_be32((void *)GPIO0_ISR1L + offs + offs2, val);
Stefan Roesef6c7b762007-03-24 15:45:34 +010090 }
91}
92#endif /* GPIO_OSRL */
93
94void gpio_write_bit(int pin, int val)
95{
96 u32 offs = 0;
97
98 if (pin >= GPIO_MAX) {
99 offs = 0x100;
100 pin -= GPIO_MAX;
101 }
102
103 if (val)
Stefan Roese1bca9192007-11-15 14:23:55 +0100104 out_be32((void *)GPIO0_OR + offs,
105 in_be32((void *)GPIO0_OR + offs) | GPIO_VAL(pin));
Stefan Roesef6c7b762007-03-24 15:45:34 +0100106 else
Stefan Roese1bca9192007-11-15 14:23:55 +0100107 out_be32((void *)GPIO0_OR + offs,
108 in_be32((void *)GPIO0_OR + offs) & ~GPIO_VAL(pin));
Stefan Roesef6c7b762007-03-24 15:45:34 +0100109}
110
Stefan Roese843b3a82007-06-15 07:39:43 +0200111int gpio_read_out_bit(int pin)
112{
113 u32 offs = 0;
114
115 if (pin >= GPIO_MAX) {
116 offs = 0x100;
117 pin -= GPIO_MAX;
118 }
119
Stefan Roese1bca9192007-11-15 14:23:55 +0100120 return (in_be32((void *)GPIO0_OR + offs) & GPIO_VAL(pin) ? 1 : 0);
Stefan Roese843b3a82007-06-15 07:39:43 +0200121}
122
Stefan Roese1bca9192007-11-15 14:23:55 +0100123#if defined(CFG_4xx_GPIO_TABLE)
Stefan Roesef6c7b762007-03-24 15:45:34 +0100124void gpio_set_chip_configuration(void)
125{
126 unsigned char i=0, j=0, offs=0, gpio_core;
127 unsigned long reg, core_add;
128
129 for (gpio_core=0; gpio_core<GPIO_GROUP_MAX; gpio_core++) {
130 j = 0;
131 offs = 0;
132 /* GPIO config of the GPIOs 0 to 31 */
133 for (i=0; i<GPIO_MAX; i++, j++) {
134 if (i == GPIO_MAX/2) {
135 offs = 4;
136 j = i-16;
137 }
138
139 core_add = gpio_tab[gpio_core][i].add;
140
141 if ((gpio_tab[gpio_core][i].in_out == GPIO_IN) ||
142 (gpio_tab[gpio_core][i].in_out == GPIO_BI)) {
143
144 switch (gpio_tab[gpio_core][i].alt_nb) {
145 case GPIO_SEL:
146 break;
147
148 case GPIO_ALT1:
Stefan Roese1bca9192007-11-15 14:23:55 +0100149 reg = in_be32((void *)GPIO_IS1(core_add+offs))
Stefan Roesef6c7b762007-03-24 15:45:34 +0100150 & ~(GPIO_MASK >> (j*2));
151 reg = reg | (GPIO_IN_SEL >> (j*2));
Stefan Roese1bca9192007-11-15 14:23:55 +0100152 out_be32((void *)GPIO_IS1(core_add+offs), reg);
Stefan Roesef6c7b762007-03-24 15:45:34 +0100153 break;
154
155 case GPIO_ALT2:
Stefan Roese1bca9192007-11-15 14:23:55 +0100156 reg = in_be32((void *)GPIO_IS2(core_add+offs))
Stefan Roesef6c7b762007-03-24 15:45:34 +0100157 & ~(GPIO_MASK >> (j*2));
158 reg = reg | (GPIO_IN_SEL >> (j*2));
Stefan Roese1bca9192007-11-15 14:23:55 +0100159 out_be32((void *)GPIO_IS2(core_add+offs), reg);
Stefan Roesef6c7b762007-03-24 15:45:34 +0100160 break;
161
162 case GPIO_ALT3:
Stefan Roese1bca9192007-11-15 14:23:55 +0100163 reg = in_be32((void *)GPIO_IS3(core_add+offs))
Stefan Roesef6c7b762007-03-24 15:45:34 +0100164 & ~(GPIO_MASK >> (j*2));
165 reg = reg | (GPIO_IN_SEL >> (j*2));
Stefan Roese1bca9192007-11-15 14:23:55 +0100166 out_be32((void *)GPIO_IS3(core_add+offs), reg);
Stefan Roesef6c7b762007-03-24 15:45:34 +0100167 break;
168 }
169 }
170
171 if ((gpio_tab[gpio_core][i].in_out == GPIO_OUT) ||
172 (gpio_tab[gpio_core][i].in_out == GPIO_BI)) {
173
174 switch (gpio_tab[gpio_core][i].alt_nb) {
175 case GPIO_SEL:
Stefan Roese1bca9192007-11-15 14:23:55 +0100176 /*
177 * Setup output value
178 * 1 -> high level
179 * 0 -> low level
180 * else -> don't touch
181 */
182 reg = in_be32((void *)GPIO_OR(core_add));
183 if (gpio_tab[gpio_core][i].out_val == GPIO_OUT_1)
184 reg |= (0x80000000 >> (i));
185 else if (gpio_tab[gpio_core][i].out_val == GPIO_OUT_0)
186 reg &= ~(0x80000000 >> (i));
187 out_be32((void *)GPIO_OR(core_add), reg);
Stefan Roese843b3a82007-06-15 07:39:43 +0200188
Stefan Roese1bca9192007-11-15 14:23:55 +0100189 reg = in_be32((void *)GPIO_TCR(core_add)) |
190 (0x80000000 >> (i));
191 out_be32((void *)GPIO_TCR(core_add), reg);
Stefan Roesef6c7b762007-03-24 15:45:34 +0100192
Stefan Roese1bca9192007-11-15 14:23:55 +0100193 reg = in_be32((void *)GPIO_OS(core_add+offs))
Stefan Roesef6c7b762007-03-24 15:45:34 +0100194 & ~(GPIO_MASK >> (j*2));
Stefan Roese1bca9192007-11-15 14:23:55 +0100195 out_be32((void *)GPIO_OS(core_add+offs), reg);
196 reg = in_be32((void *)GPIO_TS(core_add+offs))
Stefan Roesef6c7b762007-03-24 15:45:34 +0100197 & ~(GPIO_MASK >> (j*2));
Stefan Roese1bca9192007-11-15 14:23:55 +0100198 out_be32((void *)GPIO_TS(core_add+offs), reg);
Stefan Roesef6c7b762007-03-24 15:45:34 +0100199 break;
200
201 case GPIO_ALT1:
Stefan Roese1bca9192007-11-15 14:23:55 +0100202 reg = in_be32((void *)GPIO_OS(core_add+offs))
Stefan Roesef6c7b762007-03-24 15:45:34 +0100203 & ~(GPIO_MASK >> (j*2));
204 reg = reg | (GPIO_ALT1_SEL >> (j*2));
Stefan Roese1bca9192007-11-15 14:23:55 +0100205 out_be32((void *)GPIO_OS(core_add+offs), reg);
206 reg = in_be32((void *)GPIO_TS(core_add+offs))
Stefan Roesef6c7b762007-03-24 15:45:34 +0100207 & ~(GPIO_MASK >> (j*2));
208 reg = reg | (GPIO_ALT1_SEL >> (j*2));
Stefan Roese1bca9192007-11-15 14:23:55 +0100209 out_be32((void *)GPIO_TS(core_add+offs), reg);
Stefan Roesef6c7b762007-03-24 15:45:34 +0100210 break;
211
212 case GPIO_ALT2:
Stefan Roese1bca9192007-11-15 14:23:55 +0100213 reg = in_be32((void *)GPIO_OS(core_add+offs))
Stefan Roesef6c7b762007-03-24 15:45:34 +0100214 & ~(GPIO_MASK >> (j*2));
215 reg = reg | (GPIO_ALT2_SEL >> (j*2));
Stefan Roese1bca9192007-11-15 14:23:55 +0100216 out_be32((void *)GPIO_OS(core_add+offs), reg);
217 reg = in_be32((void *)GPIO_TS(core_add+offs))
Stefan Roesef6c7b762007-03-24 15:45:34 +0100218 & ~(GPIO_MASK >> (j*2));
219 reg = reg | (GPIO_ALT2_SEL >> (j*2));
Stefan Roese1bca9192007-11-15 14:23:55 +0100220 out_be32((void *)GPIO_TS(core_add+offs), reg);
Stefan Roesef6c7b762007-03-24 15:45:34 +0100221 break;
222
223 case GPIO_ALT3:
Stefan Roese1bca9192007-11-15 14:23:55 +0100224 reg = in_be32((void *)GPIO_OS(core_add+offs))
Stefan Roesef6c7b762007-03-24 15:45:34 +0100225 & ~(GPIO_MASK >> (j*2));
226 reg = reg | (GPIO_ALT3_SEL >> (j*2));
Stefan Roese1bca9192007-11-15 14:23:55 +0100227 out_be32((void *)GPIO_OS(core_add+offs), reg);
228 reg = in_be32((void *)GPIO_TS(core_add+offs))
Stefan Roesef6c7b762007-03-24 15:45:34 +0100229 & ~(GPIO_MASK >> (j*2));
230 reg = reg | (GPIO_ALT3_SEL >> (j*2));
Stefan Roese1bca9192007-11-15 14:23:55 +0100231 out_be32((void *)GPIO_TS(core_add+offs), reg);
Stefan Roesef6c7b762007-03-24 15:45:34 +0100232 break;
233 }
234 }
235 }
236 }
237}
Stefan Roese1bca9192007-11-15 14:23:55 +0100238#endif /* CFG_4xx_GPIO_TABLE */