Merge tag 'for-4.15-rc3-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[sfrench/cifs-2.6.git] / arch / cris / arch-v32 / mach-a3 / pinmux.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Allocator for I/O pins. All pins are allocated to GPIO at bootup.
4  * Unassigned pins and GPIO pins can be allocated to a fixed interface
5  * or the I/O processor instead.
6  *
7  * Copyright (c) 2005-2007 Axis Communications AB.
8  */
9
10 #include <linux/init.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/spinlock.h>
15 #include <hwregs/reg_map.h>
16 #include <hwregs/reg_rdwr.h>
17 #include <pinmux.h>
18 #include <hwregs/pinmux_defs.h>
19 #include <hwregs/clkgen_defs.h>
20
21 #undef DEBUG
22
23 #define PINS 80
24 #define PORT_PINS 32
25 #define PORTS 3
26
27 static char pins[PINS];
28 static DEFINE_SPINLOCK(pinmux_lock);
29
30 static void crisv32_pinmux_set(int port);
31
32 int
33 crisv32_pinmux_init(void)
34 {
35         static int initialized;
36
37         if (!initialized) {
38                 initialized = 1;
39                 REG_WR_INT(pinmux, regi_pinmux, rw_hwprot, 0);
40                 crisv32_pinmux_alloc(PORT_A, 0, 31, pinmux_gpio);
41                 crisv32_pinmux_alloc(PORT_B, 0, 31, pinmux_gpio);
42                 crisv32_pinmux_alloc(PORT_C, 0, 15, pinmux_gpio);
43         }
44
45         return 0;
46 }
47
48 int
49 crisv32_pinmux_alloc(int port, int first_pin, int last_pin, enum pin_mode mode)
50 {
51         int i;
52         unsigned long flags;
53
54         crisv32_pinmux_init();
55
56         if (port >= PORTS)
57                 return -EINVAL;
58
59         spin_lock_irqsave(&pinmux_lock, flags);
60
61         for (i = first_pin; i <= last_pin; i++) {
62                 if ((pins[port * PORT_PINS + i] != pinmux_none) &&
63                     (pins[port * PORT_PINS + i] != pinmux_gpio) &&
64                     (pins[port * PORT_PINS + i] != mode)) {
65                         spin_unlock_irqrestore(&pinmux_lock, flags);
66 #ifdef DEBUG
67                         panic("Pinmux alloc failed!\n");
68 #endif
69                         return -EPERM;
70                 }
71         }
72
73         for (i = first_pin; i <= last_pin; i++)
74                 pins[port * PORT_PINS + i] = mode;
75
76         crisv32_pinmux_set(port);
77
78         spin_unlock_irqrestore(&pinmux_lock, flags);
79
80         return 0;
81 }
82
83 int
84 crisv32_pinmux_alloc_fixed(enum fixed_function function)
85 {
86         int ret = -EINVAL;
87         char saved[sizeof pins];
88         unsigned long flags;
89         reg_pinmux_rw_hwprot hwprot;
90         reg_clkgen_rw_clk_ctrl clk_ctrl;
91
92         spin_lock_irqsave(&pinmux_lock, flags);
93
94         /* Save internal data for recovery */
95         memcpy(saved, pins, sizeof pins);
96
97         crisv32_pinmux_init(); /* must be done before we read rw_hwprot */
98
99         hwprot = REG_RD(pinmux, regi_pinmux, rw_hwprot);
100         clk_ctrl = REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
101
102         switch (function) {
103         case pinmux_eth:
104                 clk_ctrl.eth = regk_clkgen_yes;
105                 clk_ctrl.dma0_1_eth = regk_clkgen_yes;
106                 ret = crisv32_pinmux_alloc(PORT_B, 8, 23, pinmux_fixed);
107                 ret |= crisv32_pinmux_alloc(PORT_B, 24, 25, pinmux_fixed);
108                 hwprot.eth = hwprot.eth_mdio = regk_pinmux_yes;
109                 break;
110         case pinmux_geth:
111                 ret = crisv32_pinmux_alloc(PORT_B, 0, 7, pinmux_fixed);
112                 hwprot.geth = regk_pinmux_yes;
113                 break;
114         case pinmux_tg_cmos:
115                 clk_ctrl.ccd_tg_100 = clk_ctrl.ccd_tg_200 = regk_clkgen_yes;
116                 ret = crisv32_pinmux_alloc(PORT_B, 27, 29, pinmux_fixed);
117                 hwprot.tg_clk = regk_pinmux_yes;
118                 break;
119         case pinmux_tg_ccd:
120                 clk_ctrl.ccd_tg_100 = clk_ctrl.ccd_tg_200 = regk_clkgen_yes;
121                 ret = crisv32_pinmux_alloc(PORT_B, 27, 31, pinmux_fixed);
122                 ret |= crisv32_pinmux_alloc(PORT_C, 0, 15, pinmux_fixed);
123                 hwprot.tg = hwprot.tg_clk = regk_pinmux_yes;
124                 break;
125         case pinmux_vout:
126                 clk_ctrl.strdma0_2_video = regk_clkgen_yes;
127                 ret = crisv32_pinmux_alloc(PORT_A, 8, 18, pinmux_fixed);
128                 hwprot.vout = hwprot.vout_sync = regk_pinmux_yes;
129                 break;
130         case pinmux_ser1:
131                 clk_ctrl.sser_ser_dma6_7 = regk_clkgen_yes;
132                 ret = crisv32_pinmux_alloc(PORT_A, 24, 25, pinmux_fixed);
133                 hwprot.ser1 = regk_pinmux_yes;
134                 break;
135         case pinmux_ser2:
136                 clk_ctrl.sser_ser_dma6_7 = regk_clkgen_yes;
137                 ret = crisv32_pinmux_alloc(PORT_A, 26, 27, pinmux_fixed);
138                 hwprot.ser2 = regk_pinmux_yes;
139                 break;
140         case pinmux_ser3:
141                 clk_ctrl.sser_ser_dma6_7 = regk_clkgen_yes;
142                 ret = crisv32_pinmux_alloc(PORT_A, 28, 29, pinmux_fixed);
143                 hwprot.ser3 = regk_pinmux_yes;
144                 break;
145         case pinmux_ser4:
146                 clk_ctrl.sser_ser_dma6_7 = regk_clkgen_yes;
147                 ret = crisv32_pinmux_alloc(PORT_A, 30, 31, pinmux_fixed);
148                 hwprot.ser4 = regk_pinmux_yes;
149                 break;
150         case pinmux_sser:
151                 clk_ctrl.sser_ser_dma6_7 = regk_clkgen_yes;
152                 ret = crisv32_pinmux_alloc(PORT_A, 19, 23, pinmux_fixed);
153                 hwprot.sser = regk_pinmux_yes;
154                 break;
155         case pinmux_pio:
156                 hwprot.pio = regk_pinmux_yes;
157                 ret = 0;
158                 break;
159         case pinmux_pwm0:
160                 ret = crisv32_pinmux_alloc(PORT_A, 30, 30, pinmux_fixed);
161                 hwprot.pwm0 = regk_pinmux_yes;
162                 break;
163         case pinmux_pwm1:
164                 ret = crisv32_pinmux_alloc(PORT_A, 31, 31, pinmux_fixed);
165                 hwprot.pwm1 = regk_pinmux_yes;
166                 break;
167         case pinmux_pwm2:
168                 ret = crisv32_pinmux_alloc(PORT_B, 26, 26, pinmux_fixed);
169                 hwprot.pwm2 = regk_pinmux_yes;
170                 break;
171         case pinmux_i2c0:
172                 ret = crisv32_pinmux_alloc(PORT_A, 0, 1, pinmux_fixed);
173                 hwprot.i2c0 = regk_pinmux_yes;
174                 break;
175         case pinmux_i2c1:
176                 ret = crisv32_pinmux_alloc(PORT_A, 2, 3, pinmux_fixed);
177                 hwprot.i2c1 = regk_pinmux_yes;
178                 break;
179         case pinmux_i2c1_3wire:
180                 ret = crisv32_pinmux_alloc(PORT_A, 2, 3, pinmux_fixed);
181                 ret |= crisv32_pinmux_alloc(PORT_A, 7, 7, pinmux_fixed);
182                 hwprot.i2c1 = hwprot.i2c1_sen = regk_pinmux_yes;
183                 break;
184         case pinmux_i2c1_sda1:
185                 ret = crisv32_pinmux_alloc(PORT_A, 2, 4, pinmux_fixed);
186                 hwprot.i2c1 = hwprot.i2c1_sda1 = regk_pinmux_yes;
187                 break;
188         case pinmux_i2c1_sda2:
189                 ret = crisv32_pinmux_alloc(PORT_A, 2, 3, pinmux_fixed);
190                 ret |= crisv32_pinmux_alloc(PORT_A, 5, 5, pinmux_fixed);
191                 hwprot.i2c1 = hwprot.i2c1_sda2 = regk_pinmux_yes;
192                 break;
193         case pinmux_i2c1_sda3:
194                 ret = crisv32_pinmux_alloc(PORT_A, 2, 3, pinmux_fixed);
195                 ret |= crisv32_pinmux_alloc(PORT_A, 6, 6, pinmux_fixed);
196                 hwprot.i2c1 = hwprot.i2c1_sda3 = regk_pinmux_yes;
197                 break;
198         default:
199                 ret = -EINVAL;
200                 break;
201         }
202
203         if (!ret) {
204                 REG_WR(pinmux, regi_pinmux, rw_hwprot, hwprot);
205                 REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, clk_ctrl);
206         } else
207                 memcpy(pins, saved, sizeof pins);
208
209   spin_unlock_irqrestore(&pinmux_lock, flags);
210
211   return ret;
212 }
213
214 void
215 crisv32_pinmux_set(int port)
216 {
217         int i;
218         int gpio_val = 0;
219         int iop_val = 0;
220         int pin = port * PORT_PINS;
221
222         for (i = 0; (i < PORT_PINS) && (pin < PINS); i++, pin++) {
223                 if (pins[pin] == pinmux_gpio)
224                         gpio_val |= (1 << i);
225                 else if (pins[pin] == pinmux_iop)
226                         iop_val |= (1 << i);
227         }
228
229         REG_WRITE(int, regi_pinmux + REG_RD_ADDR_pinmux_rw_gio_pa + 4 * port,
230                 gpio_val);
231         REG_WRITE(int, regi_pinmux + REG_RD_ADDR_pinmux_rw_iop_pa + 4 * port,
232                 iop_val);
233
234 #ifdef DEBUG
235        crisv32_pinmux_dump();
236 #endif
237 }
238
239 int
240 crisv32_pinmux_dealloc(int port, int first_pin, int last_pin)
241 {
242         int i;
243         unsigned long flags;
244
245         crisv32_pinmux_init();
246
247         if (port > PORTS || port < 0)
248                 return -EINVAL;
249
250         spin_lock_irqsave(&pinmux_lock, flags);
251
252         for (i = first_pin; i <= last_pin; i++)
253                 pins[port * PORT_PINS + i] = pinmux_none;
254
255         crisv32_pinmux_set(port);
256         spin_unlock_irqrestore(&pinmux_lock, flags);
257
258         return 0;
259 }
260
261 int
262 crisv32_pinmux_dealloc_fixed(enum fixed_function function)
263 {
264         int ret = -EINVAL;
265         char saved[sizeof pins];
266         unsigned long flags;
267         reg_pinmux_rw_hwprot hwprot;
268
269         spin_lock_irqsave(&pinmux_lock, flags);
270
271         /* Save internal data for recovery */
272         memcpy(saved, pins, sizeof pins);
273
274         crisv32_pinmux_init(); /* must be done before we read rw_hwprot */
275
276         hwprot = REG_RD(pinmux, regi_pinmux, rw_hwprot);
277
278         switch (function) {
279         case pinmux_eth:
280                 ret = crisv32_pinmux_dealloc(PORT_B, 8, 23);
281                 ret |= crisv32_pinmux_dealloc(PORT_B, 24, 25);
282                 ret |= crisv32_pinmux_dealloc(PORT_B, 0, 7);
283                 hwprot.eth = hwprot.eth_mdio = hwprot.geth = regk_pinmux_no;
284                 break;
285         case pinmux_tg_cmos:
286                 ret = crisv32_pinmux_dealloc(PORT_B, 27, 29);
287                 hwprot.tg_clk = regk_pinmux_no;
288                 break;
289         case pinmux_tg_ccd:
290                 ret = crisv32_pinmux_dealloc(PORT_B, 27, 31);
291                 ret |= crisv32_pinmux_dealloc(PORT_C, 0, 15);
292                 hwprot.tg = hwprot.tg_clk = regk_pinmux_no;
293                 break;
294         case pinmux_vout:
295                 ret = crisv32_pinmux_dealloc(PORT_A, 8, 18);
296                 hwprot.vout = hwprot.vout_sync = regk_pinmux_no;
297                 break;
298         case pinmux_ser1:
299                 ret = crisv32_pinmux_dealloc(PORT_A, 24, 25);
300                 hwprot.ser1 = regk_pinmux_no;
301                 break;
302         case pinmux_ser2:
303                 ret = crisv32_pinmux_dealloc(PORT_A, 26, 27);
304                 hwprot.ser2 = regk_pinmux_no;
305                 break;
306         case pinmux_ser3:
307                 ret = crisv32_pinmux_dealloc(PORT_A, 28, 29);
308                 hwprot.ser3 = regk_pinmux_no;
309                 break;
310         case pinmux_ser4:
311                 ret = crisv32_pinmux_dealloc(PORT_A, 30, 31);
312                 hwprot.ser4 = regk_pinmux_no;
313                 break;
314         case pinmux_sser:
315                 ret = crisv32_pinmux_dealloc(PORT_A, 19, 23);
316                 hwprot.sser = regk_pinmux_no;
317                 break;
318         case pinmux_pwm0:
319                 ret = crisv32_pinmux_dealloc(PORT_A, 30, 30);
320                 hwprot.pwm0 = regk_pinmux_no;
321                 break;
322         case pinmux_pwm1:
323                 ret = crisv32_pinmux_dealloc(PORT_A, 31, 31);
324                 hwprot.pwm1 = regk_pinmux_no;
325                 break;
326         case pinmux_pwm2:
327                 ret = crisv32_pinmux_dealloc(PORT_B, 26, 26);
328                 hwprot.pwm2 = regk_pinmux_no;
329                 break;
330         case pinmux_i2c0:
331                 ret = crisv32_pinmux_dealloc(PORT_A, 0, 1);
332                 hwprot.i2c0 = regk_pinmux_no;
333                 break;
334         case pinmux_i2c1:
335                 ret = crisv32_pinmux_dealloc(PORT_A, 2, 3);
336                 hwprot.i2c1 = regk_pinmux_no;
337                 break;
338         case pinmux_i2c1_3wire:
339                 ret = crisv32_pinmux_dealloc(PORT_A, 2, 3);
340                 ret |= crisv32_pinmux_dealloc(PORT_A, 7, 7);
341                 hwprot.i2c1 = hwprot.i2c1_sen = regk_pinmux_no;
342                 break;
343         case pinmux_i2c1_sda1:
344                 ret = crisv32_pinmux_dealloc(PORT_A, 2, 4);
345                 hwprot.i2c1_sda1 = regk_pinmux_no;
346                 break;
347         case pinmux_i2c1_sda2:
348                 ret = crisv32_pinmux_dealloc(PORT_A, 2, 3);
349                 ret |= crisv32_pinmux_dealloc(PORT_A, 5, 5);
350                 hwprot.i2c1_sda2 = regk_pinmux_no;
351                 break;
352         case pinmux_i2c1_sda3:
353                 ret = crisv32_pinmux_dealloc(PORT_A, 2, 3);
354                 ret |= crisv32_pinmux_dealloc(PORT_A, 6, 6);
355                 hwprot.i2c1_sda3 = regk_pinmux_no;
356                 break;
357         default:
358                 ret = -EINVAL;
359                 break;
360         }
361
362         if (!ret)
363                 REG_WR(pinmux, regi_pinmux, rw_hwprot, hwprot);
364         else
365                 memcpy(pins, saved, sizeof pins);
366
367   spin_unlock_irqrestore(&pinmux_lock, flags);
368
369   return ret;
370 }
371
372 void
373 crisv32_pinmux_dump(void)
374 {
375         int i, j;
376         int pin = 0;
377
378         crisv32_pinmux_init();
379
380         for (i = 0; i < PORTS; i++) {
381                 pin++;
382                 printk(KERN_DEBUG "Port %c\n", 'A'+i);
383                 for (j = 0; (j < PORT_PINS) && (pin < PINS); j++, pin++)
384                         printk(KERN_DEBUG
385                                 "  Pin %d = %d\n", j, pins[i * PORT_PINS + j]);
386         }
387 }
388
389 __initcall(crisv32_pinmux_init);