blob: d8a3a3c439dd0810bd1593b18587840a92b35ad0 [file] [log] [blame]
Jesper Nilsson035e1112007-11-29 17:11:23 +01001/*
2 * Allocator for I/O pins. All pins are allocated to GPIO at bootup.
3 * Unassigned pins and GPIO pins can be allocated to a fixed interface
4 * or the I/O processor instead.
5 *
6 * Copyright (c) 2004-2007 Axis Communications AB.
7 */
8
9#include <linux/init.h>
10#include <linux/errno.h>
11#include <linux/kernel.h>
12#include <linux/string.h>
13#include <linux/spinlock.h>
14#include <hwregs/reg_map.h>
15#include <hwregs/reg_rdwr.h>
16#include <pinmux.h>
17#include <hwregs/pinmux_defs.h>
18
19#undef DEBUG
20
21#define PORT_PINS 18
22#define PORTS 4
23
24static char pins[PORTS][PORT_PINS];
25static DEFINE_SPINLOCK(pinmux_lock);
26
27static void crisv32_pinmux_set(int port);
28
Jesper Nilssona3199ad2014-10-27 09:11:31 +010029static int __crisv32_pinmux_alloc(int port, int first_pin, int last_pin,
30 enum pin_mode mode)
Jesper Nilsson035e1112007-11-29 17:11:23 +010031{
32 int i;
Jesper Nilsson035e1112007-11-29 17:11:23 +010033
34 for (i = first_pin; i <= last_pin; i++) {
35 if ((pins[port][i] != pinmux_none)
36 && (pins[port][i] != pinmux_gpio)
37 && (pins[port][i] != mode)) {
Jesper Nilsson035e1112007-11-29 17:11:23 +010038#ifdef DEBUG
39 panic("Pinmux alloc failed!\n");
40#endif
41 return -EPERM;
42 }
43 }
44
45 for (i = first_pin; i <= last_pin; i++)
46 pins[port][i] = mode;
47
48 crisv32_pinmux_set(port);
Jesper Nilsson94230172015-06-03 12:25:51 +020049
50 return 0;
Jesper Nilssona3199ad2014-10-27 09:11:31 +010051}
52
53static int crisv32_pinmux_init(void)
54{
55 static int initialized;
56
57 if (!initialized) {
58 reg_pinmux_rw_pa pa = REG_RD(pinmux, regi_pinmux, rw_pa);
59 initialized = 1;
60 REG_WR_INT(pinmux, regi_pinmux, rw_hwprot, 0);
61 pa.pa0 = pa.pa1 = pa.pa2 = pa.pa3 =
62 pa.pa4 = pa.pa5 = pa.pa6 = pa.pa7 = regk_pinmux_yes;
63 REG_WR(pinmux, regi_pinmux, rw_pa, pa);
64 __crisv32_pinmux_alloc(PORT_B, 0, PORT_PINS - 1, pinmux_gpio);
65 __crisv32_pinmux_alloc(PORT_C, 0, PORT_PINS - 1, pinmux_gpio);
66 __crisv32_pinmux_alloc(PORT_D, 0, PORT_PINS - 1, pinmux_gpio);
67 __crisv32_pinmux_alloc(PORT_E, 0, PORT_PINS - 1, pinmux_gpio);
68 }
69
70 return 0;
71}
72
73int crisv32_pinmux_alloc(int port, int first_pin, int last_pin,
74 enum pin_mode mode)
75{
76 unsigned long flags;
77 int ret;
78
79 crisv32_pinmux_init();
80
81 if (port > PORTS || port < 0)
82 return -EINVAL;
83
84 spin_lock_irqsave(&pinmux_lock, flags);
85
86 ret = __crisv32_pinmux_alloc(port, first_pin, last_pin, mode);
Jesper Nilsson035e1112007-11-29 17:11:23 +010087
88 spin_unlock_irqrestore(&pinmux_lock, flags);
89
Jesper Nilssona3199ad2014-10-27 09:11:31 +010090 return ret;
Jesper Nilsson035e1112007-11-29 17:11:23 +010091}
92
93int crisv32_pinmux_alloc_fixed(enum fixed_function function)
94{
95 int ret = -EINVAL;
96 char saved[sizeof pins];
97 unsigned long flags;
Jesper Nilsson94230172015-06-03 12:25:51 +020098 reg_pinmux_rw_hwprot hwprot;
Jesper Nilsson035e1112007-11-29 17:11:23 +010099
100 spin_lock_irqsave(&pinmux_lock, flags);
101
102 /* Save internal data for recovery */
103 memcpy(saved, pins, sizeof pins);
104
105 crisv32_pinmux_init(); /* Must be done before we read rw_hwprot */
106
Jesper Nilsson94230172015-06-03 12:25:51 +0200107 hwprot = REG_RD(pinmux, regi_pinmux, rw_hwprot);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100108
109 switch (function) {
110 case pinmux_ser1:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100111 ret = __crisv32_pinmux_alloc(PORT_C, 4, 7, pinmux_fixed);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100112 hwprot.ser1 = regk_pinmux_yes;
113 break;
114 case pinmux_ser2:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100115 ret = __crisv32_pinmux_alloc(PORT_C, 8, 11, pinmux_fixed);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100116 hwprot.ser2 = regk_pinmux_yes;
117 break;
118 case pinmux_ser3:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100119 ret = __crisv32_pinmux_alloc(PORT_C, 12, 15, pinmux_fixed);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100120 hwprot.ser3 = regk_pinmux_yes;
121 break;
122 case pinmux_sser0:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100123 ret = __crisv32_pinmux_alloc(PORT_C, 0, 3, pinmux_fixed);
124 ret |= __crisv32_pinmux_alloc(PORT_C, 16, 16, pinmux_fixed);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100125 hwprot.sser0 = regk_pinmux_yes;
126 break;
127 case pinmux_sser1:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100128 ret = __crisv32_pinmux_alloc(PORT_D, 0, 4, pinmux_fixed);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100129 hwprot.sser1 = regk_pinmux_yes;
130 break;
131 case pinmux_ata0:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100132 ret = __crisv32_pinmux_alloc(PORT_D, 5, 7, pinmux_fixed);
133 ret |= __crisv32_pinmux_alloc(PORT_D, 15, 17, pinmux_fixed);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100134 hwprot.ata0 = regk_pinmux_yes;
135 break;
136 case pinmux_ata1:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100137 ret = __crisv32_pinmux_alloc(PORT_D, 0, 4, pinmux_fixed);
138 ret |= __crisv32_pinmux_alloc(PORT_E, 17, 17, pinmux_fixed);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100139 hwprot.ata1 = regk_pinmux_yes;
140 break;
141 case pinmux_ata2:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100142 ret = __crisv32_pinmux_alloc(PORT_C, 11, 15, pinmux_fixed);
143 ret |= __crisv32_pinmux_alloc(PORT_E, 3, 3, pinmux_fixed);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100144 hwprot.ata2 = regk_pinmux_yes;
145 break;
146 case pinmux_ata3:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100147 ret = __crisv32_pinmux_alloc(PORT_C, 8, 10, pinmux_fixed);
148 ret |= __crisv32_pinmux_alloc(PORT_C, 0, 2, pinmux_fixed);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100149 hwprot.ata2 = regk_pinmux_yes;
150 break;
151 case pinmux_ata:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100152 ret = __crisv32_pinmux_alloc(PORT_B, 0, 15, pinmux_fixed);
153 ret |= __crisv32_pinmux_alloc(PORT_D, 8, 15, pinmux_fixed);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100154 hwprot.ata = regk_pinmux_yes;
155 break;
156 case pinmux_eth1:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100157 ret = __crisv32_pinmux_alloc(PORT_E, 0, 17, pinmux_fixed);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100158 hwprot.eth1 = regk_pinmux_yes;
159 hwprot.eth1_mgm = regk_pinmux_yes;
160 break;
161 case pinmux_timer:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100162 ret = __crisv32_pinmux_alloc(PORT_C, 16, 16, pinmux_fixed);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100163 hwprot.timer = regk_pinmux_yes;
164 spin_unlock_irqrestore(&pinmux_lock, flags);
165 return ret;
166 }
167
168 if (!ret)
169 REG_WR(pinmux, regi_pinmux, rw_hwprot, hwprot);
170 else
171 memcpy(pins, saved, sizeof pins);
172
173 spin_unlock_irqrestore(&pinmux_lock, flags);
174
175 return ret;
176}
177
178void crisv32_pinmux_set(int port)
179{
180 int i;
181 int gpio_val = 0;
182 int iop_val = 0;
183
184 for (i = 0; i < PORT_PINS; i++) {
185 if (pins[port][i] == pinmux_gpio)
186 gpio_val |= (1 << i);
187 else if (pins[port][i] == pinmux_iop)
188 iop_val |= (1 << i);
189 }
190
191 REG_WRITE(int, regi_pinmux + REG_RD_ADDR_pinmux_rw_pb_gio + 8 * port,
192 gpio_val);
193 REG_WRITE(int, regi_pinmux + REG_RD_ADDR_pinmux_rw_pb_iop + 8 * port,
194 iop_val);
195
196#ifdef DEBUG
197 crisv32_pinmux_dump();
198#endif
199}
200
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100201static int __crisv32_pinmux_dealloc(int port, int first_pin, int last_pin)
Jesper Nilsson035e1112007-11-29 17:11:23 +0100202{
203 int i;
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100204
205 for (i = first_pin; i <= last_pin; i++)
206 pins[port][i] = pinmux_none;
207
208 crisv32_pinmux_set(port);
209 return 0;
210}
211
212int crisv32_pinmux_dealloc(int port, int first_pin, int last_pin)
213{
Jesper Nilsson035e1112007-11-29 17:11:23 +0100214 unsigned long flags;
215
216 crisv32_pinmux_init();
217
Roel Kluinf25234f2009-11-01 14:46:26 +0100218 if (port > PORTS || port < 0)
Jesper Nilsson035e1112007-11-29 17:11:23 +0100219 return -EINVAL;
220
221 spin_lock_irqsave(&pinmux_lock, flags);
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100222 __crisv32_pinmux_dealloc(port, first_pin, last_pin);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100223 spin_unlock_irqrestore(&pinmux_lock, flags);
224
225 return 0;
226}
227
228int crisv32_pinmux_dealloc_fixed(enum fixed_function function)
229{
230 int ret = -EINVAL;
231 char saved[sizeof pins];
232 unsigned long flags;
Jesper Nilsson94230172015-06-03 12:25:51 +0200233 reg_pinmux_rw_hwprot hwprot;
Jesper Nilsson035e1112007-11-29 17:11:23 +0100234
235 spin_lock_irqsave(&pinmux_lock, flags);
236
237 /* Save internal data for recovery */
238 memcpy(saved, pins, sizeof pins);
239
240 crisv32_pinmux_init(); /* Must be done before we read rw_hwprot */
241
Jesper Nilsson94230172015-06-03 12:25:51 +0200242 hwprot = REG_RD(pinmux, regi_pinmux, rw_hwprot);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100243
244 switch (function) {
245 case pinmux_ser1:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100246 ret = __crisv32_pinmux_dealloc(PORT_C, 4, 7);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100247 hwprot.ser1 = regk_pinmux_no;
248 break;
249 case pinmux_ser2:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100250 ret = __crisv32_pinmux_dealloc(PORT_C, 8, 11);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100251 hwprot.ser2 = regk_pinmux_no;
252 break;
253 case pinmux_ser3:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100254 ret = __crisv32_pinmux_dealloc(PORT_C, 12, 15);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100255 hwprot.ser3 = regk_pinmux_no;
256 break;
257 case pinmux_sser0:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100258 ret = __crisv32_pinmux_dealloc(PORT_C, 0, 3);
259 ret |= __crisv32_pinmux_dealloc(PORT_C, 16, 16);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100260 hwprot.sser0 = regk_pinmux_no;
261 break;
262 case pinmux_sser1:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100263 ret = __crisv32_pinmux_dealloc(PORT_D, 0, 4);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100264 hwprot.sser1 = regk_pinmux_no;
265 break;
266 case pinmux_ata0:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100267 ret = __crisv32_pinmux_dealloc(PORT_D, 5, 7);
268 ret |= __crisv32_pinmux_dealloc(PORT_D, 15, 17);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100269 hwprot.ata0 = regk_pinmux_no;
270 break;
271 case pinmux_ata1:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100272 ret = __crisv32_pinmux_dealloc(PORT_D, 0, 4);
273 ret |= __crisv32_pinmux_dealloc(PORT_E, 17, 17);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100274 hwprot.ata1 = regk_pinmux_no;
275 break;
276 case pinmux_ata2:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100277 ret = __crisv32_pinmux_dealloc(PORT_C, 11, 15);
278 ret |= __crisv32_pinmux_dealloc(PORT_E, 3, 3);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100279 hwprot.ata2 = regk_pinmux_no;
280 break;
281 case pinmux_ata3:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100282 ret = __crisv32_pinmux_dealloc(PORT_C, 8, 10);
283 ret |= __crisv32_pinmux_dealloc(PORT_C, 0, 2);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100284 hwprot.ata2 = regk_pinmux_no;
285 break;
286 case pinmux_ata:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100287 ret = __crisv32_pinmux_dealloc(PORT_B, 0, 15);
288 ret |= __crisv32_pinmux_dealloc(PORT_D, 8, 15);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100289 hwprot.ata = regk_pinmux_no;
290 break;
291 case pinmux_eth1:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100292 ret = __crisv32_pinmux_dealloc(PORT_E, 0, 17);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100293 hwprot.eth1 = regk_pinmux_no;
294 hwprot.eth1_mgm = regk_pinmux_no;
295 break;
296 case pinmux_timer:
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100297 ret = __crisv32_pinmux_dealloc(PORT_C, 16, 16);
Jesper Nilsson035e1112007-11-29 17:11:23 +0100298 hwprot.timer = regk_pinmux_no;
299 spin_unlock_irqrestore(&pinmux_lock, flags);
300 return ret;
301 }
302
303 if (!ret)
304 REG_WR(pinmux, regi_pinmux, rw_hwprot, hwprot);
305 else
306 memcpy(pins, saved, sizeof pins);
307
308 spin_unlock_irqrestore(&pinmux_lock, flags);
309
310 return ret;
311}
312
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100313#ifdef DEBUG
314static void crisv32_pinmux_dump(void)
Jesper Nilsson035e1112007-11-29 17:11:23 +0100315{
316 int i, j;
317
318 crisv32_pinmux_init();
319
320 for (i = 0; i < PORTS; i++) {
321 printk(KERN_DEBUG "Port %c\n", 'B' + i);
322 for (j = 0; j < PORT_PINS; j++)
323 printk(KERN_DEBUG " Pin %d = %d\n", j, pins[i][j]);
324 }
325}
Jesper Nilssona3199ad2014-10-27 09:11:31 +0100326#endif
Jesper Nilsson035e1112007-11-29 17:11:23 +0100327__initcall(crisv32_pinmux_init);