blob: c8074f47e7604c8adf0edb230e4b783d7911bdf9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Ley Foon Tan9ea8c5b2017-04-26 02:44:42 +08002/*
3 * Copyright (C) 2016-2017 Intel Corporation <www.intel.com>
Ley Foon Tan9ea8c5b2017-04-26 02:44:42 +08004 */
5
Simon Glass0f2af882020-05-10 11:40:05 -06006#include <log.h>
Tom Rinidec7ea02024-05-20 13:35:03 -06007#include <linux/errno.h>
Ley Foon Tan9ea8c5b2017-04-26 02:44:42 +08008#include <asm/arch/pinmux.h>
9#include <asm/io.h>
Ley Foon Tan9ea8c5b2017-04-26 02:44:42 +080010#include <fdtdec.h>
11
12static int do_pinctr_pin(const void *blob, int child, const char *node_name)
13{
14 int len;
15 fdt_addr_t base_addr;
16 fdt_size_t size;
17 const u32 *cell;
18 u32 offset, value;
19
20 base_addr = fdtdec_get_addr_size(blob, child, "reg", &size);
21 if (base_addr != FDT_ADDR_T_NONE) {
22 cell = fdt_getprop(blob, child, "pinctrl-single,pins", &len);
23 if (!cell || len <= 0)
24 return -EFAULT;
25
26 debug("%p %d\n", cell, len);
27 for (; len > 0; len -= (2 * sizeof(u32))) {
28 offset = fdt32_to_cpu(*cell++);
29 value = fdt32_to_cpu(*cell++);
30 debug("<0x%x 0x%x>\n", offset, value);
31 writel(value, base_addr + offset);
32 }
33 return 0;
34 }
35 return -EFAULT;
36}
37
38static int do_pinctrl_pins(const void *blob, int node, const char *child_name)
39{
40 int child, len;
41 const char *node_name;
42
43 child = fdt_first_subnode(blob, node);
44
45 if (child < 0)
46 return -EINVAL;
47
48 node_name = fdt_get_name(blob, child, &len);
49
50 while (node_name) {
51 if (!strcmp(child_name, node_name))
52 return do_pinctr_pin(blob, child, node_name);
53
54 child = fdt_next_subnode(blob, child);
55
56 if (child < 0)
57 break;
58
59 node_name = fdt_get_name(blob, child, &len);
60 }
61
62 return -EFAULT;
63}
64
65int config_dedicated_pins(const void *blob)
66{
67 int node;
68
69 node = fdtdec_next_compatible(blob, 0,
70 COMPAT_ALTERA_SOCFPGA_PINCTRL_SINGLE);
71 if (node < 0)
72 return -EINVAL;
73
74 if (do_pinctrl_pins(blob, node, "dedicated_cfg"))
75 return -EFAULT;
76
77 if (do_pinctrl_pins(blob, node, "dedicated"))
78 return -EFAULT;
79
80 return 0;
81}
82
83int config_pins(const void *blob, const char *pin_grp)
84{
85 int node;
86
87 node = fdtdec_next_compatible(blob, 0,
88 COMPAT_ALTERA_SOCFPGA_PINCTRL_SINGLE);
89 if (node < 0)
90 return -EINVAL;
91
92 if (do_pinctrl_pins(blob, node, pin_grp))
93 return -EFAULT;
94
95 return 0;
96}