blob: 5ad049d9555f8d74f303fd936aea319569dadbd3 [file] [log] [blame]
Sean Anderson087dfce2020-09-14 11:01:58 -04001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
4 */
5
6#include <common.h>
7#include <clk.h>
8#include <dm.h>
9#include <dm/pinctrl.h>
10#include <dt-bindings/pinctrl/k210-pinctrl.h>
11#include <mapmem.h>
12#include <regmap.h>
13#include <syscon.h>
14#include <asm/io.h>
15#include <linux/err.h>
16#include <linux/bitfield.h>
17#include <linux/bitops.h>
18
19/*
20 * The K210 only implements 8 drive levels, even though there is register space
21 * for 16
22 */
23#define K210_PC_DRIVE_MASK GENMASK(11, 8)
24#define K210_PC_DRIVE_SHIFT 8
25#define K210_PC_DRIVE_0 (0 << K210_PC_DRIVE_SHIFT)
26#define K210_PC_DRIVE_1 (1 << K210_PC_DRIVE_SHIFT)
27#define K210_PC_DRIVE_2 (2 << K210_PC_DRIVE_SHIFT)
28#define K210_PC_DRIVE_3 (3 << K210_PC_DRIVE_SHIFT)
29#define K210_PC_DRIVE_4 (4 << K210_PC_DRIVE_SHIFT)
30#define K210_PC_DRIVE_5 (5 << K210_PC_DRIVE_SHIFT)
31#define K210_PC_DRIVE_6 (6 << K210_PC_DRIVE_SHIFT)
32#define K210_PC_DRIVE_7 (7 << K210_PC_DRIVE_SHIFT)
33#define K210_PC_DRIVE_MAX 7
34
35#define K210_PC_MODE_MASK GENMASK(23, 12)
36/*
37 * output enabled == PC_OE & (PC_OE_INV ^ FUNCTION_OE) where FUNCTION_OE is a
38 * physical signal from the function
39 */
40#define K210_PC_OE BIT(12) /* Output Enable */
41#define K210_PC_OE_INV BIT(13) /* INVert function-controlled Output Enable */
42#define K210_PC_DO_OE BIT(14) /* set Data Out to the Output Enable signal */
43#define K210_PC_DO_INV BIT(15) /* INVert final Data Output */
44#define K210_PC_PU BIT(16) /* Pull Up */
45#define K210_PC_PD BIT(17) /* Pull Down */
46/* Strong pull up not implemented on K210 */
47#define K210_PC_SL BIT(19) /* reduce SLew rate to prevent overshoot */
48/* Same semantics as OE above */
49#define K210_PC_IE BIT(20) /* Input Enable */
50#define K210_PC_IE_INV BIT(21) /* INVert function-controlled Input Enable */
51#define K210_PC_DI_INV BIT(22) /* INVert Data Input */
52#define K210_PC_ST BIT(23) /* Schmitt Trigger */
53#define K210_PC_DI BIT(31) /* raw Data Input */
54#define K210_PC_BIAS_MASK (K210_PC_PU & K210_PC_PD)
55
56#define K210_PC_MODE_IN (K210_PC_IE | K210_PC_ST)
57#define K210_PC_MODE_OUT (K210_PC_DRIVE_7 | K210_PC_OE)
58#define K210_PC_MODE_I2C (K210_PC_MODE_IN | K210_PC_IE_INV | K210_PC_SL | \
59 K210_PC_OE | K210_PC_OE_INV | K210_PC_PU)
60#define K210_PC_MODE_SPI (K210_PC_MODE_IN | K210_PC_IE_INV | \
61 K210_PC_MODE_OUT | K210_PC_OE_INV)
62#define K210_PC_MODE_GPIO (K210_PC_MODE_IN | K210_PC_MODE_OUT)
63
64#define K210_PG_FUNC GENMASK(7, 0)
65#define K210_PG_DO BIT(8)
66#define K210_PG_PIN GENMASK(22, 16)
67
68#define PIN_CONFIG_OUTPUT_INVERT (PIN_CONFIG_END + 1)
69#define PIN_CONFIG_INPUT_INVERT (PIN_CONFIG_END + 2)
70
71struct k210_fpioa {
72 u32 pins[48];
73 u32 tie_en[8];
74 u32 tie_val[8];
75};
76
77struct k210_pc_priv {
78 struct clk clk;
79 struct k210_fpioa __iomem *fpioa; /* FPIOA register */
80 struct regmap *sysctl; /* Sysctl regmap */
81 u32 power_offset; /* Power bank register offset */
82};
83
84#ifdef CONFIG_CMD_PINMUX
85static const char k210_pc_pin_names[][6] = {
86#define PIN(i) \
87 [i] = "IO_" #i
88 PIN(0),
89 PIN(1),
90 PIN(2),
91 PIN(3),
92 PIN(4),
93 PIN(5),
94 PIN(6),
95 PIN(7),
96 PIN(8),
97 PIN(9),
98 PIN(10),
99 PIN(11),
100 PIN(12),
101 PIN(13),
102 PIN(14),
103 PIN(15),
104 PIN(16),
105 PIN(17),
106 PIN(18),
107 PIN(19),
108 PIN(20),
109 PIN(21),
110 PIN(22),
111 PIN(23),
112 PIN(24),
113 PIN(25),
114 PIN(26),
115 PIN(27),
116 PIN(28),
117 PIN(29),
118 PIN(30),
119 PIN(31),
120 PIN(32),
121 PIN(33),
122 PIN(34),
123 PIN(35),
124 PIN(36),
125 PIN(37),
126 PIN(38),
127 PIN(39),
128 PIN(40),
129 PIN(41),
130 PIN(42),
131 PIN(43),
132 PIN(44),
133 PIN(45),
134 PIN(46),
135 PIN(47),
136#undef PIN
137};
138
139static int k210_pc_get_pins_count(struct udevice *dev)
140{
141 return ARRAY_SIZE(k210_pc_pin_names);
142};
143
144static const char *k210_pc_get_pin_name(struct udevice *dev, unsigned selector)
145{
146 return k210_pc_pin_names[selector];
147}
148#endif /* CONFIG_CMD_PINMUX */
149
150/* These are just power domains */
151static const char k210_pc_group_names[][3] = {
152 [0] = "A0",
153 [1] = "A1",
154 [2] = "A2",
155 [3] = "B0",
156 [4] = "B1",
157 [5] = "B2",
158 [6] = "C0",
159 [7] = "C1",
160};
161
162static int k210_pc_get_groups_count(struct udevice *dev)
163{
164 return ARRAY_SIZE(k210_pc_group_names);
165}
166
167static const char *k210_pc_get_group_name(struct udevice *dev,
168 unsigned selector)
169{
170 return k210_pc_group_names[selector];
171}
172
173enum k210_pc_mode_id {
174 K210_PC_DEFAULT_DISABLED,
175 K210_PC_DEFAULT_IN,
176 K210_PC_DEFAULT_IN_TIE,
177 K210_PC_DEFAULT_OUT,
178 K210_PC_DEFAULT_I2C,
179 K210_PC_DEFAULT_SPI,
180 K210_PC_DEFAULT_GPIO,
181 K210_PC_DEFAULT_INT13,
182};
183
184static const u32 k210_pc_mode_id_to_mode[] = {
185#define DEFAULT(mode) \
186 [K210_PC_DEFAULT_##mode] = K210_PC_MODE_##mode
187 [K210_PC_DEFAULT_DISABLED] = 0,
188 DEFAULT(IN),
189 [K210_PC_DEFAULT_IN_TIE] = K210_PC_MODE_IN,
190 DEFAULT(OUT),
191 DEFAULT(I2C),
192 DEFAULT(SPI),
193 DEFAULT(GPIO),
194 [K210_PC_DEFAULT_INT13] = K210_PC_MODE_IN | K210_PC_PU,
195#undef DEFAULT
196};
197
198/* This saves around 2K vs having a pointer+mode */
199struct k210_pcf_info {
200#ifdef CONFIG_CMD_PINMUX
201 char name[15];
202#endif
203 u8 mode_id;
204};
205
206static const struct k210_pcf_info k210_pcf_infos[] = {
207#ifdef CONFIG_CMD_PINMUX
208#define FUNC(id, mode) \
209 [K210_PCF_##id] = { \
210 .name = #id, \
211 .mode_id = K210_PC_DEFAULT_##mode \
212 }
213#else
214#define FUNC(id, mode) \
215 [K210_PCF_##id] = { \
216 .mode_id = K210_PC_DEFAULT_##mode \
217 }
218#endif
219 FUNC(JTAG_TCLK, IN),
220 FUNC(JTAG_TDI, IN),
221 FUNC(JTAG_TMS, IN),
222 FUNC(JTAG_TDO, OUT),
223 FUNC(SPI0_D0, SPI),
224 FUNC(SPI0_D1, SPI),
225 FUNC(SPI0_D2, SPI),
226 FUNC(SPI0_D3, SPI),
227 FUNC(SPI0_D4, SPI),
228 FUNC(SPI0_D5, SPI),
229 FUNC(SPI0_D6, SPI),
230 FUNC(SPI0_D7, SPI),
231 FUNC(SPI0_SS0, OUT),
232 FUNC(SPI0_SS1, OUT),
233 FUNC(SPI0_SS2, OUT),
234 FUNC(SPI0_SS3, OUT),
235 FUNC(SPI0_ARB, IN_TIE),
236 FUNC(SPI0_SCLK, OUT),
237 FUNC(UARTHS_RX, IN),
238 FUNC(UARTHS_TX, OUT),
239 FUNC(RESV6, IN),
240 FUNC(RESV7, IN),
241 FUNC(CLK_SPI1, OUT),
242 FUNC(CLK_I2C1, OUT),
243 FUNC(GPIOHS0, GPIO),
244 FUNC(GPIOHS1, GPIO),
245 FUNC(GPIOHS2, GPIO),
246 FUNC(GPIOHS3, GPIO),
247 FUNC(GPIOHS4, GPIO),
248 FUNC(GPIOHS5, GPIO),
249 FUNC(GPIOHS6, GPIO),
250 FUNC(GPIOHS7, GPIO),
251 FUNC(GPIOHS8, GPIO),
252 FUNC(GPIOHS9, GPIO),
253 FUNC(GPIOHS10, GPIO),
254 FUNC(GPIOHS11, GPIO),
255 FUNC(GPIOHS12, GPIO),
256 FUNC(GPIOHS13, GPIO),
257 FUNC(GPIOHS14, GPIO),
258 FUNC(GPIOHS15, GPIO),
259 FUNC(GPIOHS16, GPIO),
260 FUNC(GPIOHS17, GPIO),
261 FUNC(GPIOHS18, GPIO),
262 FUNC(GPIOHS19, GPIO),
263 FUNC(GPIOHS20, GPIO),
264 FUNC(GPIOHS21, GPIO),
265 FUNC(GPIOHS22, GPIO),
266 FUNC(GPIOHS23, GPIO),
267 FUNC(GPIOHS24, GPIO),
268 FUNC(GPIOHS25, GPIO),
269 FUNC(GPIOHS26, GPIO),
270 FUNC(GPIOHS27, GPIO),
271 FUNC(GPIOHS28, GPIO),
272 FUNC(GPIOHS29, GPIO),
273 FUNC(GPIOHS30, GPIO),
274 FUNC(GPIOHS31, GPIO),
275 FUNC(GPIO0, GPIO),
276 FUNC(GPIO1, GPIO),
277 FUNC(GPIO2, GPIO),
278 FUNC(GPIO3, GPIO),
279 FUNC(GPIO4, GPIO),
280 FUNC(GPIO5, GPIO),
281 FUNC(GPIO6, GPIO),
282 FUNC(GPIO7, GPIO),
283 FUNC(UART1_RX, IN),
284 FUNC(UART1_TX, OUT),
285 FUNC(UART2_RX, IN),
286 FUNC(UART2_TX, OUT),
287 FUNC(UART3_RX, IN),
288 FUNC(UART3_TX, OUT),
289 FUNC(SPI1_D0, SPI),
290 FUNC(SPI1_D1, SPI),
291 FUNC(SPI1_D2, SPI),
292 FUNC(SPI1_D3, SPI),
293 FUNC(SPI1_D4, SPI),
294 FUNC(SPI1_D5, SPI),
295 FUNC(SPI1_D6, SPI),
296 FUNC(SPI1_D7, SPI),
297 FUNC(SPI1_SS0, OUT),
298 FUNC(SPI1_SS1, OUT),
299 FUNC(SPI1_SS2, OUT),
300 FUNC(SPI1_SS3, OUT),
301 FUNC(SPI1_ARB, IN_TIE),
302 FUNC(SPI1_SCLK, OUT),
303 FUNC(SPI2_D0, SPI),
304 FUNC(SPI2_SS, IN),
305 FUNC(SPI2_SCLK, IN),
306 FUNC(I2S0_MCLK, OUT),
307 FUNC(I2S0_SCLK, OUT),
308 FUNC(I2S0_WS, OUT),
309 FUNC(I2S0_IN_D0, IN),
310 FUNC(I2S0_IN_D1, IN),
311 FUNC(I2S0_IN_D2, IN),
312 FUNC(I2S0_IN_D3, IN),
313 FUNC(I2S0_OUT_D0, OUT),
314 FUNC(I2S0_OUT_D1, OUT),
315 FUNC(I2S0_OUT_D2, OUT),
316 FUNC(I2S0_OUT_D3, OUT),
317 FUNC(I2S1_MCLK, OUT),
318 FUNC(I2S1_SCLK, OUT),
319 FUNC(I2S1_WS, OUT),
320 FUNC(I2S1_IN_D0, IN),
321 FUNC(I2S1_IN_D1, IN),
322 FUNC(I2S1_IN_D2, IN),
323 FUNC(I2S1_IN_D3, IN),
324 FUNC(I2S1_OUT_D0, OUT),
325 FUNC(I2S1_OUT_D1, OUT),
326 FUNC(I2S1_OUT_D2, OUT),
327 FUNC(I2S1_OUT_D3, OUT),
328 FUNC(I2S2_MCLK, OUT),
329 FUNC(I2S2_SCLK, OUT),
330 FUNC(I2S2_WS, OUT),
331 FUNC(I2S2_IN_D0, IN),
332 FUNC(I2S2_IN_D1, IN),
333 FUNC(I2S2_IN_D2, IN),
334 FUNC(I2S2_IN_D3, IN),
335 FUNC(I2S2_OUT_D0, OUT),
336 FUNC(I2S2_OUT_D1, OUT),
337 FUNC(I2S2_OUT_D2, OUT),
338 FUNC(I2S2_OUT_D3, OUT),
339 FUNC(RESV0, DISABLED),
340 FUNC(RESV1, DISABLED),
341 FUNC(RESV2, DISABLED),
342 FUNC(RESV3, DISABLED),
343 FUNC(RESV4, DISABLED),
344 FUNC(RESV5, DISABLED),
345 FUNC(I2C0_SCLK, I2C),
346 FUNC(I2C0_SDA, I2C),
347 FUNC(I2C1_SCLK, I2C),
348 FUNC(I2C1_SDA, I2C),
349 FUNC(I2C2_SCLK, I2C),
350 FUNC(I2C2_SDA, I2C),
351 FUNC(DVP_XCLK, OUT),
352 FUNC(DVP_RST, OUT),
353 FUNC(DVP_PWDN, OUT),
354 FUNC(DVP_VSYNC, IN),
355 FUNC(DVP_HSYNC, IN),
356 FUNC(DVP_PCLK, IN),
357 FUNC(DVP_D0, IN),
358 FUNC(DVP_D1, IN),
359 FUNC(DVP_D2, IN),
360 FUNC(DVP_D3, IN),
361 FUNC(DVP_D4, IN),
362 FUNC(DVP_D5, IN),
363 FUNC(DVP_D6, IN),
364 FUNC(DVP_D7, IN),
365 FUNC(SCCB_SCLK, I2C),
366 FUNC(SCCB_SDA, I2C),
367 FUNC(UART1_CTS, IN),
368 FUNC(UART1_DSR, IN),
369 FUNC(UART1_DCD, IN),
370 FUNC(UART1_RI, IN),
371 FUNC(UART1_SIR_IN, IN),
372 FUNC(UART1_DTR, OUT),
373 FUNC(UART1_RTS, OUT),
374 FUNC(UART1_OUT2, OUT),
375 FUNC(UART1_OUT1, OUT),
376 FUNC(UART1_SIR_OUT, OUT),
377 FUNC(UART1_BAUD, OUT),
378 FUNC(UART1_RE, OUT),
379 FUNC(UART1_DE, OUT),
380 FUNC(UART1_RS485_EN, OUT),
381 FUNC(UART2_CTS, IN),
382 FUNC(UART2_DSR, IN),
383 FUNC(UART2_DCD, IN),
384 FUNC(UART2_RI, IN),
385 FUNC(UART2_SIR_IN, IN),
386 FUNC(UART2_DTR, OUT),
387 FUNC(UART2_RTS, OUT),
388 FUNC(UART2_OUT2, OUT),
389 FUNC(UART2_OUT1, OUT),
390 FUNC(UART2_SIR_OUT, OUT),
391 FUNC(UART2_BAUD, OUT),
392 FUNC(UART2_RE, OUT),
393 FUNC(UART2_DE, OUT),
394 FUNC(UART2_RS485_EN, OUT),
395 FUNC(UART3_CTS, IN),
396 FUNC(UART3_DSR, IN),
397 FUNC(UART3_DCD, IN),
398 FUNC(UART3_RI, IN),
399 FUNC(UART3_SIR_IN, IN),
400 FUNC(UART3_DTR, OUT),
401 FUNC(UART3_RTS, OUT),
402 FUNC(UART3_OUT2, OUT),
403 FUNC(UART3_OUT1, OUT),
404 FUNC(UART3_SIR_OUT, OUT),
405 FUNC(UART3_BAUD, OUT),
406 FUNC(UART3_RE, OUT),
407 FUNC(UART3_DE, OUT),
408 FUNC(UART3_RS485_EN, OUT),
409 FUNC(TIMER0_TOGGLE1, OUT),
410 FUNC(TIMER0_TOGGLE2, OUT),
411 FUNC(TIMER0_TOGGLE3, OUT),
412 FUNC(TIMER0_TOGGLE4, OUT),
413 FUNC(TIMER1_TOGGLE1, OUT),
414 FUNC(TIMER1_TOGGLE2, OUT),
415 FUNC(TIMER1_TOGGLE3, OUT),
416 FUNC(TIMER1_TOGGLE4, OUT),
417 FUNC(TIMER2_TOGGLE1, OUT),
418 FUNC(TIMER2_TOGGLE2, OUT),
419 FUNC(TIMER2_TOGGLE3, OUT),
420 FUNC(TIMER2_TOGGLE4, OUT),
421 FUNC(CLK_SPI2, OUT),
422 FUNC(CLK_I2C2, OUT),
423 FUNC(INTERNAL0, OUT),
424 FUNC(INTERNAL1, OUT),
425 FUNC(INTERNAL2, OUT),
426 FUNC(INTERNAL3, OUT),
427 FUNC(INTERNAL4, OUT),
428 FUNC(INTERNAL5, OUT),
429 FUNC(INTERNAL6, OUT),
430 FUNC(INTERNAL7, OUT),
431 FUNC(INTERNAL8, OUT),
432 FUNC(INTERNAL9, IN),
433 FUNC(INTERNAL10, IN),
434 FUNC(INTERNAL11, IN),
435 FUNC(INTERNAL12, IN),
436 FUNC(INTERNAL13, INT13),
437 FUNC(INTERNAL14, I2C),
438 FUNC(INTERNAL15, IN),
439 FUNC(INTERNAL16, IN),
440 FUNC(INTERNAL17, IN),
441 FUNC(CONSTANT, DISABLED),
442 FUNC(INTERNAL18, IN),
443 FUNC(DEBUG0, OUT),
444 FUNC(DEBUG1, OUT),
445 FUNC(DEBUG2, OUT),
446 FUNC(DEBUG3, OUT),
447 FUNC(DEBUG4, OUT),
448 FUNC(DEBUG5, OUT),
449 FUNC(DEBUG6, OUT),
450 FUNC(DEBUG7, OUT),
451 FUNC(DEBUG8, OUT),
452 FUNC(DEBUG9, OUT),
453 FUNC(DEBUG10, OUT),
454 FUNC(DEBUG11, OUT),
455 FUNC(DEBUG12, OUT),
456 FUNC(DEBUG13, OUT),
457 FUNC(DEBUG14, OUT),
458 FUNC(DEBUG15, OUT),
459 FUNC(DEBUG16, OUT),
460 FUNC(DEBUG17, OUT),
461 FUNC(DEBUG18, OUT),
462 FUNC(DEBUG19, OUT),
463 FUNC(DEBUG20, OUT),
464 FUNC(DEBUG21, OUT),
465 FUNC(DEBUG22, OUT),
466 FUNC(DEBUG23, OUT),
467 FUNC(DEBUG24, OUT),
468 FUNC(DEBUG25, OUT),
469 FUNC(DEBUG26, OUT),
470 FUNC(DEBUG27, OUT),
471 FUNC(DEBUG28, OUT),
472 FUNC(DEBUG29, OUT),
473 FUNC(DEBUG30, OUT),
474 FUNC(DEBUG31, OUT),
475#undef FUNC
476};
477
478static int k210_pc_pinmux_set(struct udevice *dev, u32 pinmux_group)
479{
480 unsigned pin = FIELD_GET(K210_PG_PIN, pinmux_group);
481 bool do_oe = FIELD_GET(K210_PG_DO, pinmux_group);
482 unsigned func = FIELD_GET(K210_PG_FUNC, pinmux_group);
483 struct k210_pc_priv *priv = dev_get_priv(dev);
484 const struct k210_pcf_info *info = &k210_pcf_infos[func];
485 u32 mode = k210_pc_mode_id_to_mode[info->mode_id];
486 u32 val = func | mode | (do_oe ? K210_PC_DO_OE : 0);
487
488 debug("%s(%.8x): IO_%.2u = %3u | %.8x\n", __func__, pinmux_group, pin,
489 func, mode);
490
491 writel(val, &priv->fpioa->pins[pin]);
492 return pin;
493}
494
495/* Max drive strength in uA */
496static const int k210_pc_drive_strength[] = {
497 [0] = 11200,
498 [1] = 16800,
499 [2] = 22300,
500 [3] = 27800,
501 [4] = 33300,
502 [5] = 38700,
503 [6] = 44100,
504 [7] = 49500,
505};
506
507static int k210_pc_get_drive(unsigned max_strength_ua)
508{
509 int i;
510
511 for (i = K210_PC_DRIVE_MAX; i; i--)
512 if (k210_pc_drive_strength[i] < max_strength_ua)
513 return i;
514
515 return -EINVAL;
516}
517
518static int k210_pc_pinconf_set(struct udevice *dev, unsigned pin_selector,
519 unsigned param, unsigned argument)
520{
521 struct k210_pc_priv *priv = dev_get_priv(dev);
522 u32 val = readl(&priv->fpioa->pins[pin_selector]);
523
524 switch (param) {
525 case PIN_CONFIG_BIAS_DISABLE:
526 val &= ~K210_PC_BIAS_MASK;
527 break;
528 case PIN_CONFIG_BIAS_PULL_DOWN:
529 if (argument)
530 val |= K210_PC_PD;
531 else
532 return -EINVAL;
533 break;
534 case PIN_CONFIG_BIAS_PULL_UP:
535 if (argument)
536 val |= K210_PC_PD;
537 else
538 return -EINVAL;
539 break;
540 case PIN_CONFIG_DRIVE_STRENGTH:
541 argument *= 1000;
542 case PIN_CONFIG_DRIVE_STRENGTH_UA: {
543 int drive = k210_pc_get_drive(argument);
544
545 if (IS_ERR_VALUE(drive))
546 return drive;
547 val &= ~K210_PC_DRIVE_MASK;
548 val |= FIELD_PREP(K210_PC_DRIVE_MASK, drive);
549 break;
550 }
551 case PIN_CONFIG_INPUT_ENABLE:
552 if (argument)
553 val |= K210_PC_IE;
554 else
555 val &= ~K210_PC_IE;
556 break;
557 case PIN_CONFIG_INPUT_SCHMITT:
558 argument = 1;
559 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
560 if (argument)
561 val |= K210_PC_ST;
562 else
563 val &= ~K210_PC_ST;
564 break;
565 case PIN_CONFIG_OUTPUT:
566 k210_pc_pinmux_set(dev,
567 K210_FPIOA(pin_selector, K210_PCF_CONSTANT));
568 val = readl(&priv->fpioa->pins[pin_selector]);
569 val |= K210_PC_MODE_OUT;
570
571 if (!argument)
572 val |= K210_PC_DO_INV;
573 break;
574 case PIN_CONFIG_OUTPUT_ENABLE:
575 if (argument)
576 val |= K210_PC_OE;
577 else
578 val &= ~K210_PC_OE;
579 break;
580 case PIN_CONFIG_SLEW_RATE:
581 if (argument)
582 val |= K210_PC_SL;
583 else
584 val &= ~K210_PC_SL;
585 break;
586 case PIN_CONFIG_OUTPUT_INVERT:
587 if (argument)
588 val |= K210_PC_DO_INV;
589 else
590 val &= ~K210_PC_DO_INV;
591 break;
592 case PIN_CONFIG_INPUT_INVERT:
593 if (argument)
594 val |= K210_PC_DI_INV;
595 else
596 val &= ~K210_PC_DI_INV;
597 break;
598 default:
599 return -EINVAL;
600 }
601
602 writel(val, &priv->fpioa->pins[pin_selector]);
603 return 0;
604}
605
606static int k210_pc_pinconf_group_set(struct udevice *dev,
607 unsigned group_selector, unsigned param,
608 unsigned argument)
609{
610 struct k210_pc_priv *priv = dev_get_priv(dev);
611
612 if (param == PIN_CONFIG_POWER_SOURCE) {
613 u32 bit = BIT(group_selector);
614
615 regmap_update_bits(priv->sysctl, priv->power_offset, bit,
616 argument ? bit : 0);
617 } else {
618 return -EINVAL;
619 }
620
621 return 0;
622}
623
624#ifdef CONFIG_CMD_PINMUX
625static int k210_pc_get_pin_muxing(struct udevice *dev, unsigned int selector,
626 char *buf, int size)
627{
628 struct k210_pc_priv *priv = dev_get_priv(dev);
629 u32 val = readl(&priv->fpioa->pins[selector]);
630 const struct k210_pcf_info *info = &k210_pcf_infos[val & K210_PCF_MASK];
631
632 strncpy(buf, info->name, min((size_t)size, sizeof(info->name)));
633 return 0;
634}
635#endif
636
637static const struct pinconf_param k210_pc_pinconf_params[] = {
638 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
639 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
640 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
641 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, U32_MAX },
642 { "drive-strength-ua", PIN_CONFIG_DRIVE_STRENGTH_UA, U32_MAX },
643 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
644 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
645 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
646 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
647 { "power-source", PIN_CONFIG_POWER_SOURCE, K210_PC_POWER_1V8 },
648 { "output-low", PIN_CONFIG_OUTPUT, 0 },
649 { "output-high", PIN_CONFIG_OUTPUT, 1 },
650 { "output-enable", PIN_CONFIG_OUTPUT_ENABLE, 1 },
651 { "output-disable", PIN_CONFIG_OUTPUT_ENABLE, 0 },
652 { "slew-rate", PIN_CONFIG_SLEW_RATE, 1 },
653 { "output-polarity-invert", PIN_CONFIG_OUTPUT_INVERT, 1},
654 { "input-polarity-invert", PIN_CONFIG_INPUT_INVERT, 1},
655};
656
657static const struct pinctrl_ops k210_pc_pinctrl_ops = {
658#ifdef CONFIG_CMD_PINMUX
659 .get_pins_count = k210_pc_get_pins_count,
660 .get_pin_name = k210_pc_get_pin_name,
661#endif
662 .get_groups_count = k210_pc_get_groups_count,
663 .get_group_name = k210_pc_get_group_name,
664 .pinmux_property_set = k210_pc_pinmux_set,
665 .pinconf_num_params = ARRAY_SIZE(k210_pc_pinconf_params),
666 .pinconf_params = k210_pc_pinconf_params,
667 .pinconf_set = k210_pc_pinconf_set,
668 .pinconf_group_set = k210_pc_pinconf_group_set,
669 .set_state = pinctrl_generic_set_state,
670#ifdef CONFIG_CMD_PINMUX
671 .get_pin_muxing = k210_pc_get_pin_muxing,
672#endif
673};
674
675static int k210_pc_probe(struct udevice *dev)
676{
677 int ret, i, j;
678 struct k210_pc_priv *priv = dev_get_priv(dev);
679
680 priv->fpioa = dev_read_addr_ptr(dev);
681 if (!priv->fpioa)
682 return -EINVAL;
683
684 ret = clk_get_by_index(dev, 0, &priv->clk);
685 if (ret)
686 return ret;
687
688 ret = clk_enable(&priv->clk);
689 if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
690 goto err;
691
692 priv->sysctl = syscon_regmap_lookup_by_phandle(dev, "kendryte,sysctl");
693 if (IS_ERR(priv->sysctl)) {
694 ret = -ENODEV;
695 goto err;
696 }
697
698 ret = dev_read_u32(dev, "kendryte,power-offset", &priv->power_offset);
699 if (ret)
700 goto err;
701
702 debug("%s: fpioa = %p sysctl = %p power offset = %x\n", __func__,
703 priv->fpioa, (void *)priv->sysctl->ranges[0].start,
704 priv->power_offset);
705
706 /* Init input ties */
707 for (i = 0; i < ARRAY_SIZE(priv->fpioa->tie_en); i++) {
708 u32 val = 0;
709
710 for (j = 0; j < 32; j++)
711 if (k210_pcf_infos[i * 32 + j].mode_id ==
712 K210_PC_DEFAULT_IN_TIE)
713 val |= BIT(j);
714 writel(val, &priv->fpioa->tie_en[i]);
715 writel(val, &priv->fpioa->tie_val[i]);
716 }
717
718 return 0;
719
720err:
721 clk_free(&priv->clk);
722 return ret;
723}
724
725static const struct udevice_id k210_pc_ids[] = {
726 { .compatible = "kendryte,k210-fpioa" },
727 { }
728};
729
730U_BOOT_DRIVER(pinctrl_k210) = {
731 .name = "pinctrl_k210",
732 .id = UCLASS_PINCTRL,
733 .of_match = k210_pc_ids,
734 .probe = k210_pc_probe,
735 .priv_auto_alloc_size = sizeof(struct k210_pc_priv),
736 .ops = &k210_pc_pinctrl_ops,
737};