xref: /openbmc/linux/drivers/soc/rockchip/grf.c (revision fb574682)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Rockchip Generic Register Files setup
4  *
5  * Copyright (c) 2016 Heiko Stuebner <heiko@sntech.de>
6  */
7 
8 #include <linux/err.h>
9 #include <linux/mfd/syscon.h>
10 #include <linux/of_device.h>
11 #include <linux/platform_device.h>
12 #include <linux/regmap.h>
13 
14 #define HIWORD_UPDATE(val, mask, shift) \
15 		((val) << (shift) | (mask) << ((shift) + 16))
16 
17 struct rockchip_grf_value {
18 	const char *desc;
19 	u32 reg;
20 	u32 val;
21 };
22 
23 struct rockchip_grf_info {
24 	const struct rockchip_grf_value *values;
25 	int num_values;
26 };
27 
28 #define RK3036_GRF_SOC_CON0		0x140
29 
30 static const struct rockchip_grf_value rk3036_defaults[] __initconst = {
31 	/*
32 	 * Disable auto jtag/sdmmc switching that causes issues with the
33 	 * clock-framework and the mmc controllers making them unreliable.
34 	 */
35 	{ "jtag switching", RK3036_GRF_SOC_CON0, HIWORD_UPDATE(0, 1, 11) },
36 };
37 
38 static const struct rockchip_grf_info rk3036_grf __initconst = {
39 	.values = rk3036_defaults,
40 	.num_values = ARRAY_SIZE(rk3036_defaults),
41 };
42 
43 #define RK3128_GRF_SOC_CON0		0x140
44 
45 static const struct rockchip_grf_value rk3128_defaults[] __initconst = {
46 	{ "jtag switching", RK3128_GRF_SOC_CON0, HIWORD_UPDATE(0, 1, 8) },
47 };
48 
49 static const struct rockchip_grf_info rk3128_grf __initconst = {
50 	.values = rk3128_defaults,
51 	.num_values = ARRAY_SIZE(rk3128_defaults),
52 };
53 
54 #define RK3228_GRF_SOC_CON6		0x418
55 
56 static const struct rockchip_grf_value rk3228_defaults[] __initconst = {
57 	{ "jtag switching", RK3228_GRF_SOC_CON6, HIWORD_UPDATE(0, 1, 8) },
58 };
59 
60 static const struct rockchip_grf_info rk3228_grf __initconst = {
61 	.values = rk3228_defaults,
62 	.num_values = ARRAY_SIZE(rk3228_defaults),
63 };
64 
65 #define RK3288_GRF_SOC_CON0		0x244
66 #define RK3288_GRF_SOC_CON2		0x24c
67 
68 static const struct rockchip_grf_value rk3288_defaults[] __initconst = {
69 	{ "jtag switching", RK3288_GRF_SOC_CON0, HIWORD_UPDATE(0, 1, 12) },
70 	{ "pwm select", RK3288_GRF_SOC_CON2, HIWORD_UPDATE(1, 1, 0) },
71 };
72 
73 static const struct rockchip_grf_info rk3288_grf __initconst = {
74 	.values = rk3288_defaults,
75 	.num_values = ARRAY_SIZE(rk3288_defaults),
76 };
77 
78 #define RK3328_GRF_SOC_CON4		0x410
79 
80 static const struct rockchip_grf_value rk3328_defaults[] __initconst = {
81 	{ "jtag switching", RK3328_GRF_SOC_CON4, HIWORD_UPDATE(0, 1, 12) },
82 };
83 
84 static const struct rockchip_grf_info rk3328_grf __initconst = {
85 	.values = rk3328_defaults,
86 	.num_values = ARRAY_SIZE(rk3328_defaults),
87 };
88 
89 #define RK3368_GRF_SOC_CON15		0x43c
90 
91 static const struct rockchip_grf_value rk3368_defaults[] __initconst = {
92 	{ "jtag switching", RK3368_GRF_SOC_CON15, HIWORD_UPDATE(0, 1, 13) },
93 };
94 
95 static const struct rockchip_grf_info rk3368_grf __initconst = {
96 	.values = rk3368_defaults,
97 	.num_values = ARRAY_SIZE(rk3368_defaults),
98 };
99 
100 #define RK3399_GRF_SOC_CON7		0xe21c
101 
102 static const struct rockchip_grf_value rk3399_defaults[] __initconst = {
103 	{ "jtag switching", RK3399_GRF_SOC_CON7, HIWORD_UPDATE(0, 1, 12) },
104 };
105 
106 static const struct rockchip_grf_info rk3399_grf __initconst = {
107 	.values = rk3399_defaults,
108 	.num_values = ARRAY_SIZE(rk3399_defaults),
109 };
110 
111 static const struct of_device_id rockchip_grf_dt_match[] __initconst = {
112 	{
113 		.compatible = "rockchip,rk3036-grf",
114 		.data = (void *)&rk3036_grf,
115 	}, {
116 		.compatible = "rockchip,rk3128-grf",
117 		.data = (void *)&rk3128_grf,
118 	}, {
119 		.compatible = "rockchip,rk3228-grf",
120 		.data = (void *)&rk3228_grf,
121 	}, {
122 		.compatible = "rockchip,rk3288-grf",
123 		.data = (void *)&rk3288_grf,
124 	}, {
125 		.compatible = "rockchip,rk3328-grf",
126 		.data = (void *)&rk3328_grf,
127 	}, {
128 		.compatible = "rockchip,rk3368-grf",
129 		.data = (void *)&rk3368_grf,
130 	}, {
131 		.compatible = "rockchip,rk3399-grf",
132 		.data = (void *)&rk3399_grf,
133 	},
134 	{ /* sentinel */ },
135 };
136 
137 static int __init rockchip_grf_init(void)
138 {
139 	const struct rockchip_grf_info *grf_info;
140 	const struct of_device_id *match;
141 	struct device_node *np;
142 	struct regmap *grf;
143 	int ret, i;
144 
145 	np = of_find_matching_node_and_match(NULL, rockchip_grf_dt_match,
146 					     &match);
147 	if (!np)
148 		return -ENODEV;
149 	if (!match || !match->data) {
150 		pr_err("%s: missing grf data\n", __func__);
151 		return -EINVAL;
152 	}
153 
154 	grf_info = match->data;
155 
156 	grf = syscon_node_to_regmap(np);
157 	if (IS_ERR(grf)) {
158 		pr_err("%s: could not get grf syscon\n", __func__);
159 		return PTR_ERR(grf);
160 	}
161 
162 	for (i = 0; i < grf_info->num_values; i++) {
163 		const struct rockchip_grf_value *val = &grf_info->values[i];
164 
165 		pr_debug("%s: adjusting %s in %#6x to %#10x\n", __func__,
166 			val->desc, val->reg, val->val);
167 		ret = regmap_write(grf, val->reg, val->val);
168 		if (ret < 0)
169 			pr_err("%s: write to %#6x failed with %d\n",
170 			       __func__, val->reg, ret);
171 	}
172 
173 	return 0;
174 }
175 postcore_initcall(rockchip_grf_init);
176