rk3328_iomux.c revision 1.4 1 1.4 thorpej /* $NetBSD: rk3328_iomux.c,v 1.4 2021/01/18 02:35:49 thorpej Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.4 thorpej __KERNEL_RCSID(0, "$NetBSD: rk3328_iomux.c,v 1.4 2021/01/18 02:35:49 thorpej Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/intr.h>
36 1.1 jmcneill #include <sys/systm.h>
37 1.1 jmcneill #include <sys/mutex.h>
38 1.1 jmcneill #include <sys/kmem.h>
39 1.1 jmcneill #include <sys/lwp.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <dev/fdt/fdtvar.h>
42 1.1 jmcneill #include <dev/fdt/syscon.h>
43 1.1 jmcneill
44 1.1 jmcneill #define GRF_GPIO_P_REG(_bank, _idx) (0x0100 + (_bank) * 0x10 + ((_idx) >> 3) * 4)
45 1.1 jmcneill #define GRF_GPIO_P_CTL(_idx) (0x3 << (((_idx) & 7) * 2))
46 1.1 jmcneill #define GRF_GPIO_P_CTL_Z 0
47 1.1 jmcneill #define GRF_GPIO_P_CTL_PULLUP 1
48 1.1 jmcneill #define GRF_GPIO_P_CTL_PULLDOWN 2
49 1.1 jmcneill #define GRF_GPIO_P_CTL_REPEATER 3
50 1.1 jmcneill #define GRF_GPIO_P_CTL_MASK 0x3
51 1.1 jmcneill #define GRF_GPIO_P_WRITE_EN(_idx) (0x3 << (((_idx) & 7) * 2 + 16))
52 1.1 jmcneill
53 1.1 jmcneill #define GRF_GPIO_E_REG(_bank, _idx) (0x0200 + (_bank) * 0x10 + ((_idx) >> 3) * 4)
54 1.1 jmcneill #define GRF_GPIO_E_CTL(_idx) (0x3 << (((_idx) & 7) * 2))
55 1.1 jmcneill #define GRF_GPIO_E_CTL_2MA 0
56 1.1 jmcneill #define GRF_GPIO_E_CTL_4MA 1
57 1.1 jmcneill #define GRF_GPIO_E_CTL_8MA 2
58 1.1 jmcneill #define GRF_GPIO_E_CTL_12MA 3
59 1.1 jmcneill #define GRF_GPIO_E_CTL_MASK 0x3
60 1.1 jmcneill #define GRF_GPIO_E_WRITE_EN(_idx) (0x3 << (((_idx) & 7) * 2 + 16))
61 1.1 jmcneill
62 1.1 jmcneill struct rk3328_iomux {
63 1.1 jmcneill bus_size_t base;
64 1.1 jmcneill u_int type;
65 1.1 jmcneill #define RK3328_IOMUX_TYPE_3BIT 0x01
66 1.1 jmcneill };
67 1.1 jmcneill
68 1.1 jmcneill struct rk3328_iomux_bank {
69 1.1 jmcneill struct rk3328_iomux iomux[4];
70 1.1 jmcneill };
71 1.1 jmcneill
72 1.1 jmcneill static const struct rk3328_iomux_bank rk3328_iomux_banks[] = {
73 1.1 jmcneill [0] = {
74 1.1 jmcneill .iomux = {
75 1.1 jmcneill [0] = { .base = 0x0000 },
76 1.1 jmcneill [1] = { .base = 0x0004 },
77 1.1 jmcneill [2] = { .base = 0x0008 },
78 1.1 jmcneill [3] = { .base = 0x000c },
79 1.1 jmcneill },
80 1.1 jmcneill },
81 1.1 jmcneill [1] = {
82 1.1 jmcneill .iomux = {
83 1.1 jmcneill [0] = { .base = 0x0010 },
84 1.1 jmcneill [1] = { .base = 0x0014 },
85 1.1 jmcneill [2] = { .base = 0x0018 },
86 1.1 jmcneill [3] = { .base = 0x001c },
87 1.1 jmcneill }
88 1.1 jmcneill },
89 1.1 jmcneill [2] = {
90 1.1 jmcneill .iomux = {
91 1.1 jmcneill [0] = { .base = 0x0020 },
92 1.1 jmcneill [1] = { .base = 0x0024, .type = RK3328_IOMUX_TYPE_3BIT },
93 1.1 jmcneill [2] = { .base = 0x002c, .type = RK3328_IOMUX_TYPE_3BIT },
94 1.1 jmcneill [3] = { .base = 0x0034 },
95 1.1 jmcneill },
96 1.1 jmcneill },
97 1.1 jmcneill [3] = {
98 1.1 jmcneill .iomux = {
99 1.1 jmcneill [0] = { .base = 0x0038, .type = RK3328_IOMUX_TYPE_3BIT },
100 1.1 jmcneill [1] = { .base = 0x0040, .type = RK3328_IOMUX_TYPE_3BIT },
101 1.1 jmcneill [2] = { .base = 0x0048 },
102 1.1 jmcneill [3] = { .base = 0x004c },
103 1.1 jmcneill },
104 1.1 jmcneill },
105 1.1 jmcneill };
106 1.1 jmcneill
107 1.1 jmcneill struct rk3328_iomux_conf {
108 1.1 jmcneill const struct rk3328_iomux_bank *banks;
109 1.1 jmcneill u_int nbanks;
110 1.1 jmcneill };
111 1.1 jmcneill
112 1.1 jmcneill static const struct rk3328_iomux_conf rk3328_iomux_conf = {
113 1.1 jmcneill .banks = rk3328_iomux_banks,
114 1.1 jmcneill .nbanks = __arraycount(rk3328_iomux_banks),
115 1.1 jmcneill };
116 1.1 jmcneill
117 1.4 thorpej static const struct device_compatible_entry compat_data[] = {
118 1.4 thorpej { .compat = "rockchip,rk3328-pinctrl", .data = &rk3328_iomux_conf },
119 1.4 thorpej
120 1.4 thorpej { 0 }
121 1.1 jmcneill };
122 1.1 jmcneill
123 1.1 jmcneill struct rk3328_iomux_softc {
124 1.1 jmcneill device_t sc_dev;
125 1.1 jmcneill struct syscon *sc_syscon;
126 1.1 jmcneill
127 1.1 jmcneill const struct rk3328_iomux_conf *sc_conf;
128 1.1 jmcneill };
129 1.1 jmcneill
130 1.1 jmcneill #define LOCK(sc) \
131 1.1 jmcneill syscon_lock((sc)->sc_syscon)
132 1.1 jmcneill #define UNLOCK(sc) \
133 1.1 jmcneill syscon_unlock((sc)->sc_syscon)
134 1.1 jmcneill #define RD4(sc, reg) \
135 1.1 jmcneill syscon_read_4((sc)->sc_syscon, (reg))
136 1.1 jmcneill #define WR4(sc, reg, val) \
137 1.1 jmcneill syscon_write_4((sc)->sc_syscon, (reg), (val))
138 1.1 jmcneill
139 1.1 jmcneill static int rk3328_iomux_match(device_t, cfdata_t, void *);
140 1.1 jmcneill static void rk3328_iomux_attach(device_t, device_t, void *);
141 1.1 jmcneill
142 1.1 jmcneill CFATTACH_DECL_NEW(rk3328_iomux, sizeof(struct rk3328_iomux_softc),
143 1.1 jmcneill rk3328_iomux_match, rk3328_iomux_attach, NULL, NULL);
144 1.1 jmcneill
145 1.1 jmcneill static void
146 1.1 jmcneill rk3328_iomux_calc_iomux_reg(struct rk3328_iomux_softc *sc, u_int bank, u_int pin, bus_size_t *reg, uint32_t *mask)
147 1.1 jmcneill {
148 1.1 jmcneill const struct rk3328_iomux_bank *banks = sc->sc_conf->banks;
149 1.1 jmcneill
150 1.1 jmcneill KASSERT(bank < sc->sc_conf->nbanks);
151 1.1 jmcneill
152 1.1 jmcneill *reg = banks[bank].iomux[pin / 8].base;
153 1.1 jmcneill if (banks[bank].iomux[pin / 8].type & RK3328_IOMUX_TYPE_3BIT) {
154 1.1 jmcneill if ((pin % 8) >= 5)
155 1.1 jmcneill *reg += 0x04;
156 1.1 jmcneill const u_int bit = (pin % 8 % 5) * 3;
157 1.1 jmcneill *mask = 7 << bit;
158 1.1 jmcneill } else {
159 1.1 jmcneill const u_int bit = (pin % 8) * 2;
160 1.1 jmcneill *mask = 3 << bit;
161 1.1 jmcneill }
162 1.1 jmcneill }
163 1.1 jmcneill
164 1.1 jmcneill static void
165 1.1 jmcneill rk3328_iomux_set_bias(struct rk3328_iomux_softc *sc, u_int bank, u_int idx, u_int bias)
166 1.1 jmcneill {
167 1.1 jmcneill WR4(sc, GRF_GPIO_P_REG(bank, idx),
168 1.1 jmcneill __SHIFTIN(GRF_GPIO_P_CTL_MASK, GRF_GPIO_P_WRITE_EN(idx)) |
169 1.1 jmcneill __SHIFTIN(bias, GRF_GPIO_P_CTL(idx)));
170 1.1 jmcneill }
171 1.1 jmcneill
172 1.1 jmcneill static void
173 1.1 jmcneill rk3328_iomux_set_drive_strength(struct rk3328_iomux_softc *sc, u_int bank, u_int idx, u_int drv)
174 1.1 jmcneill {
175 1.1 jmcneill WR4(sc, GRF_GPIO_E_REG(bank, idx),
176 1.1 jmcneill __SHIFTIN(GRF_GPIO_E_CTL_MASK, GRF_GPIO_E_WRITE_EN(idx)) |
177 1.1 jmcneill __SHIFTIN(drv, GRF_GPIO_E_CTL(idx)));
178 1.1 jmcneill }
179 1.1 jmcneill
180 1.1 jmcneill static void
181 1.1 jmcneill rk3328_iomux_set_mux(struct rk3328_iomux_softc *sc, u_int bank, u_int idx, u_int mux)
182 1.1 jmcneill {
183 1.1 jmcneill bus_size_t reg;
184 1.1 jmcneill uint32_t mask;
185 1.1 jmcneill
186 1.1 jmcneill rk3328_iomux_calc_iomux_reg(sc, bank, idx, ®, &mask);
187 1.1 jmcneill
188 1.1 jmcneill WR4(sc, reg, (mask << 16) | __SHIFTIN(mux, mask));
189 1.1 jmcneill }
190 1.1 jmcneill
191 1.1 jmcneill static int
192 1.1 jmcneill rk3328_iomux_config(struct rk3328_iomux_softc *sc, const int phandle, u_int bank, u_int idx, u_int mux)
193 1.1 jmcneill {
194 1.1 jmcneill
195 1.2 thorpej const int bias = fdtbus_pinctrl_parse_bias(phandle, NULL);
196 1.2 thorpej switch (bias) {
197 1.2 thorpej case 0:
198 1.1 jmcneill rk3328_iomux_set_bias(sc, bank, idx, GRF_GPIO_P_CTL_Z);
199 1.2 thorpej break;
200 1.2 thorpej case GPIO_PIN_PULLUP:
201 1.1 jmcneill rk3328_iomux_set_bias(sc, bank, idx, GRF_GPIO_P_CTL_PULLUP);
202 1.2 thorpej break;
203 1.2 thorpej case GPIO_PIN_PULLDOWN:
204 1.1 jmcneill rk3328_iomux_set_bias(sc, bank, idx, GRF_GPIO_P_CTL_PULLDOWN);
205 1.2 thorpej break;
206 1.2 thorpej }
207 1.1 jmcneill
208 1.2 thorpej const int drv = fdtbus_pinctrl_parse_drive_strength(phandle);
209 1.2 thorpej switch (drv) {
210 1.2 thorpej case -1:
211 1.2 thorpej break;
212 1.2 thorpej case 2:
213 1.2 thorpej rk3328_iomux_set_drive_strength(sc, bank, idx, GRF_GPIO_E_CTL_2MA);
214 1.2 thorpej break;
215 1.2 thorpej case 4:
216 1.2 thorpej rk3328_iomux_set_drive_strength(sc, bank, idx, GRF_GPIO_E_CTL_4MA);
217 1.2 thorpej break;
218 1.2 thorpej case 8:
219 1.2 thorpej rk3328_iomux_set_drive_strength(sc, bank, idx, GRF_GPIO_E_CTL_8MA);
220 1.2 thorpej break;
221 1.2 thorpej case 12:
222 1.2 thorpej rk3328_iomux_set_drive_strength(sc, bank, idx, GRF_GPIO_E_CTL_12MA);
223 1.2 thorpej break;
224 1.2 thorpej default:
225 1.2 thorpej aprint_error_dev(sc->sc_dev, "unsupported drive-strength %u\n", drv);
226 1.2 thorpej return EINVAL;
227 1.1 jmcneill }
228 1.1 jmcneill
229 1.1 jmcneill #if notyet
230 1.2 thorpej int output_value;
231 1.2 thorpej const int direction =
232 1.2 thorpej fdtbus_pinctrl_parse_input_output(phandle, &output_value);
233 1.2 thorpej if (direction != -1) {
234 1.2 thorpej rk3328_iomux_set_direction(sc, bank, idx, direction,
235 1.2 thorpej output_value);
236 1.2 thorpej }
237 1.1 jmcneill #endif
238 1.1 jmcneill
239 1.1 jmcneill rk3328_iomux_set_mux(sc, bank, idx, mux);
240 1.1 jmcneill
241 1.1 jmcneill return 0;
242 1.1 jmcneill }
243 1.1 jmcneill
244 1.1 jmcneill static int
245 1.1 jmcneill rk3328_iomux_pinctrl_set_config(device_t dev, const void *data, size_t len)
246 1.1 jmcneill {
247 1.1 jmcneill struct rk3328_iomux_softc * const sc = device_private(dev);
248 1.1 jmcneill int pins_len;
249 1.1 jmcneill
250 1.1 jmcneill if (len != 4)
251 1.1 jmcneill return -1;
252 1.1 jmcneill
253 1.1 jmcneill const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
254 1.1 jmcneill const u_int *pins = fdtbus_get_prop(phandle, "rockchip,pins", &pins_len);
255 1.1 jmcneill
256 1.1 jmcneill while (pins_len >= 16) {
257 1.1 jmcneill const u_int bank = be32toh(pins[0]);
258 1.1 jmcneill const u_int idx = be32toh(pins[1]);
259 1.1 jmcneill const u_int mux = be32toh(pins[2]);
260 1.1 jmcneill const int cfg = fdtbus_get_phandle_from_native(be32toh(pins[3]));
261 1.1 jmcneill
262 1.1 jmcneill LOCK(sc);
263 1.1 jmcneill rk3328_iomux_config(sc, cfg, bank, idx, mux);
264 1.1 jmcneill UNLOCK(sc);
265 1.1 jmcneill
266 1.1 jmcneill pins_len -= 16;
267 1.1 jmcneill pins += 4;
268 1.1 jmcneill }
269 1.1 jmcneill
270 1.1 jmcneill return 0;
271 1.1 jmcneill }
272 1.1 jmcneill
273 1.1 jmcneill static struct fdtbus_pinctrl_controller_func rk3328_iomux_pinctrl_funcs = {
274 1.1 jmcneill .set_config = rk3328_iomux_pinctrl_set_config,
275 1.1 jmcneill };
276 1.1 jmcneill
277 1.1 jmcneill static int
278 1.1 jmcneill rk3328_iomux_match(device_t parent, cfdata_t cf, void *aux)
279 1.1 jmcneill {
280 1.1 jmcneill struct fdt_attach_args * const faa = aux;
281 1.1 jmcneill
282 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
283 1.1 jmcneill }
284 1.1 jmcneill
285 1.1 jmcneill static void
286 1.1 jmcneill rk3328_iomux_attach(device_t parent, device_t self, void *aux)
287 1.1 jmcneill {
288 1.1 jmcneill struct rk3328_iomux_softc * const sc = device_private(self);
289 1.1 jmcneill struct fdt_attach_args * const faa = aux;
290 1.1 jmcneill const int phandle = faa->faa_phandle;
291 1.1 jmcneill int child, sub;
292 1.1 jmcneill
293 1.1 jmcneill sc->sc_dev = self;
294 1.1 jmcneill sc->sc_syscon = fdtbus_syscon_acquire(phandle, "rockchip,grf");
295 1.1 jmcneill if (sc->sc_syscon == NULL) {
296 1.1 jmcneill aprint_error(": couldn't acquire grf syscon\n");
297 1.1 jmcneill return;
298 1.1 jmcneill }
299 1.4 thorpej sc->sc_conf = of_search_compatible(phandle, compat_data)->data;
300 1.1 jmcneill
301 1.1 jmcneill aprint_naive("\n");
302 1.1 jmcneill aprint_normal(": RK3328 IOMUX control\n");
303 1.1 jmcneill
304 1.1 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
305 1.1 jmcneill for (sub = OF_child(child); sub; sub = OF_peer(sub)) {
306 1.1 jmcneill if (!of_hasprop(sub, "rockchip,pins"))
307 1.1 jmcneill continue;
308 1.1 jmcneill fdtbus_register_pinctrl_config(self, sub, &rk3328_iomux_pinctrl_funcs);
309 1.1 jmcneill }
310 1.1 jmcneill }
311 1.1 jmcneill
312 1.1 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
313 1.1 jmcneill struct fdt_attach_args cfaa = *faa;
314 1.1 jmcneill cfaa.faa_phandle = child;
315 1.1 jmcneill cfaa.faa_name = fdtbus_get_string(child, "name");
316 1.1 jmcneill cfaa.faa_quiet = false;
317 1.1 jmcneill
318 1.1 jmcneill config_found(self, &cfaa, NULL);
319 1.1 jmcneill }
320 1.1 jmcneill }
321