rk3288_iomux.c revision 1.1 1 1.1 jmcneill /* $NetBSD: rk3288_iomux.c,v 1.1 2021/11/12 22:02:08 jmcneill 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.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: rk3288_iomux.c,v 1.1 2021/11/12 22:02:08 jmcneill 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 GPIO_P_CTL_Z 0
45 1.1 jmcneill #define GPIO_P_CTL_PULLUP 1
46 1.1 jmcneill #define GPIO_P_CTL_PULLDOWN 2
47 1.1 jmcneill #define GPIO_P_CTL_MASK 0x3U
48 1.1 jmcneill
49 1.1 jmcneill #define GPIO_E_CTL_2MA 0
50 1.1 jmcneill #define GPIO_E_CTL_4MA 1
51 1.1 jmcneill #define GPIO_E_CTL_8MA 2
52 1.1 jmcneill #define GPIO_E_CTL_12MA 3
53 1.1 jmcneill #define GPIO_E_CTL_MASK 0x3U
54 1.1 jmcneill
55 1.1 jmcneill static const struct device_compatible_entry compat_data[] = {
56 1.1 jmcneill { .compat = "rockchip,rk3288-pinctrl" },
57 1.1 jmcneill DEVICE_COMPAT_EOL
58 1.1 jmcneill };
59 1.1 jmcneill
60 1.1 jmcneill struct rk3288_iomux_softc {
61 1.1 jmcneill device_t sc_dev;
62 1.1 jmcneill struct syscon *sc_grf;
63 1.1 jmcneill struct syscon *sc_pmu;
64 1.1 jmcneill };
65 1.1 jmcneill
66 1.1 jmcneill struct rk3288_iomux_reg {
67 1.1 jmcneill struct syscon *syscon;
68 1.1 jmcneill int mux_reg;
69 1.1 jmcneill uint32_t mux_bit;
70 1.1 jmcneill int pull_reg;
71 1.1 jmcneill uint32_t pull_bit;
72 1.1 jmcneill int drv_reg;
73 1.1 jmcneill uint32_t drv_bit;
74 1.1 jmcneill uint32_t flags;
75 1.1 jmcneill #define IOMUX_NOROUTE __BIT(0)
76 1.1 jmcneill #define IOMUX_4BIT __BIT(1)
77 1.1 jmcneill };
78 1.1 jmcneill
79 1.1 jmcneill #define LOCK(reg) \
80 1.1 jmcneill syscon_lock((reg)->syscon)
81 1.1 jmcneill #define UNLOCK(reg) \
82 1.1 jmcneill syscon_unlock((reg)->syscon)
83 1.1 jmcneill #define RD4(reg, off) \
84 1.1 jmcneill syscon_read_4((reg)->syscon, (off))
85 1.1 jmcneill #define WR4(reg, off, val) \
86 1.1 jmcneill syscon_write_4((reg)->syscon, (off), (val))
87 1.1 jmcneill
88 1.1 jmcneill static int rk3288_iomux_match(device_t, cfdata_t, void *);
89 1.1 jmcneill static void rk3288_iomux_attach(device_t, device_t, void *);
90 1.1 jmcneill
91 1.1 jmcneill CFATTACH_DECL_NEW(rk3288_iomux, sizeof(struct rk3288_iomux_softc),
92 1.1 jmcneill rk3288_iomux_match, rk3288_iomux_attach, NULL, NULL);
93 1.1 jmcneill
94 1.1 jmcneill #define NBANKS 9
95 1.1 jmcneill #define NPINSPERBANK 32
96 1.1 jmcneill
97 1.1 jmcneill static uint32_t rk3288_iomux_flags[NBANKS][NPINSPERBANK / 8] = {
98 1.1 jmcneill [0] = { 0, 0, 0, IOMUX_NOROUTE },
99 1.1 jmcneill [1] = { IOMUX_NOROUTE, IOMUX_NOROUTE, IOMUX_NOROUTE, 0 },
100 1.1 jmcneill [2] = { 0, 0, 0, IOMUX_NOROUTE },
101 1.1 jmcneill [3] = { 0, 0, 0, IOMUX_4BIT },
102 1.1 jmcneill [4] = { IOMUX_4BIT, IOMUX_4BIT, 0, 0 },
103 1.1 jmcneill [5] = { IOMUX_NOROUTE, 0, 0, IOMUX_NOROUTE },
104 1.1 jmcneill [6] = { 0, 0, 0, IOMUX_NOROUTE },
105 1.1 jmcneill [7] = { 0, 0, IOMUX_4BIT, IOMUX_NOROUTE },
106 1.1 jmcneill [8] = { 0, 0, IOMUX_NOROUTE, IOMUX_NOROUTE }
107 1.1 jmcneill };
108 1.1 jmcneill
109 1.1 jmcneill static int rk3288_iomux_offset[NBANKS][NPINSPERBANK / 8] = {
110 1.1 jmcneill /* PMU offsets */
111 1.1 jmcneill [0] = { 0x84, 0x88, 0x8c, -1 },
112 1.1 jmcneill /* GRF offsets */
113 1.1 jmcneill [1] = { -1, -1, -1, 0x0c },
114 1.1 jmcneill [2] = { 0x10, 0x14, 0x18, -1 },
115 1.1 jmcneill [3] = { 0x20, 0x24, 0x28, 0x2c },
116 1.1 jmcneill [4] = { 0x34, 0x3c, 0x44, 0x48 },
117 1.1 jmcneill [5] = { -1, 0x50, 0x54, -1 },
118 1.1 jmcneill [6] = { 0x5c, 0x60, 0x64, -1 },
119 1.1 jmcneill [7] = { 0x6c, 0x70, 0x74, -1 },
120 1.1 jmcneill [8] = { 0x80, 0x84, -1, -1 },
121 1.1 jmcneill };
122 1.1 jmcneill
123 1.1 jmcneill static bool
124 1.1 jmcneill rk3288_iomux_get_reg(struct rk3288_iomux_softc *sc, u_int bank, u_int idx,
125 1.1 jmcneill struct rk3288_iomux_reg *reg)
126 1.1 jmcneill {
127 1.1 jmcneill if (bank >= NBANKS || idx >= NPINSPERBANK) {
128 1.1 jmcneill return false;
129 1.1 jmcneill }
130 1.1 jmcneill
131 1.1 jmcneill if (bank == 0) {
132 1.1 jmcneill reg->syscon = sc->sc_pmu;
133 1.1 jmcneill reg->pull_reg = 0x64 + (idx / 8) * 4;
134 1.1 jmcneill reg->pull_bit = idx % 8 * 2;
135 1.1 jmcneill reg->drv_reg = 0x70 + (idx / 8) * 4;
136 1.1 jmcneill reg->drv_bit = idx % 8 * 2;
137 1.1 jmcneill } else {
138 1.1 jmcneill reg->syscon = sc->sc_grf;
139 1.1 jmcneill reg->pull_reg = 0x130 + (bank * 0x10) + (idx / 8) * 4;
140 1.1 jmcneill reg->pull_bit = idx % 8 * 2;
141 1.1 jmcneill reg->drv_reg = 0x1b0 + (bank * 0x10) + (idx / 8) * 4;
142 1.1 jmcneill reg->drv_bit = idx % 8 * 2;
143 1.1 jmcneill }
144 1.1 jmcneill reg->flags = rk3288_iomux_flags[bank][idx / 8];
145 1.1 jmcneill reg->mux_reg = rk3288_iomux_offset[bank][idx / 8];
146 1.1 jmcneill if (reg->mux_reg != -1) {
147 1.1 jmcneill if ((reg->flags & IOMUX_4BIT) == 0) {
148 1.1 jmcneill reg->mux_bit = idx % 8 * 2;
149 1.1 jmcneill } else {
150 1.1 jmcneill reg->mux_bit = idx % 4 * 4;
151 1.1 jmcneill if ((idx % 8) >= 4) {
152 1.1 jmcneill reg->mux_reg += 0x4;
153 1.1 jmcneill }
154 1.1 jmcneill }
155 1.1 jmcneill }
156 1.1 jmcneill
157 1.1 jmcneill return true;
158 1.1 jmcneill }
159 1.1 jmcneill
160 1.1 jmcneill static void
161 1.1 jmcneill rk3288_iomux_set_bias(struct rk3288_iomux_softc *sc, struct rk3288_iomux_reg *reg,
162 1.1 jmcneill int bias)
163 1.1 jmcneill {
164 1.1 jmcneill uint32_t val;
165 1.1 jmcneill u_int p;
166 1.1 jmcneill
167 1.1 jmcneill switch (bias) {
168 1.1 jmcneill case 0:
169 1.1 jmcneill p = GPIO_P_CTL_Z;
170 1.1 jmcneill break;
171 1.1 jmcneill case GPIO_PIN_PULLUP:
172 1.1 jmcneill p = GPIO_P_CTL_PULLUP;
173 1.1 jmcneill break;
174 1.1 jmcneill case GPIO_PIN_PULLDOWN:
175 1.1 jmcneill p = GPIO_P_CTL_PULLDOWN;
176 1.1 jmcneill break;
177 1.1 jmcneill default:
178 1.1 jmcneill return;
179 1.1 jmcneill }
180 1.1 jmcneill
181 1.1 jmcneill val = GPIO_P_CTL_MASK << (reg->pull_bit + 16);
182 1.1 jmcneill val |= p << reg->pull_bit;
183 1.1 jmcneill
184 1.1 jmcneill #ifdef RK3288_IOMUX_DEBUG
185 1.1 jmcneill const uint32_t oval = RD4(reg, reg->pull_reg);
186 1.1 jmcneill printf("%s: wr %#x -> %#x (%#x)\n", __func__,
187 1.1 jmcneill oval & (GPIO_P_CTL_MASK << reg->pull_bit),
188 1.1 jmcneill val & 0xffff,
189 1.1 jmcneill GPIO_P_CTL_MASK << reg->pull_bit);
190 1.1 jmcneill #endif
191 1.1 jmcneill
192 1.1 jmcneill WR4(reg, reg->pull_reg, val);
193 1.1 jmcneill }
194 1.1 jmcneill
195 1.1 jmcneill static void
196 1.1 jmcneill rk3288_iomux_set_drive_strength(struct rk3288_iomux_softc *sc,
197 1.1 jmcneill struct rk3288_iomux_reg *reg, int drv)
198 1.1 jmcneill {
199 1.1 jmcneill uint32_t val;
200 1.1 jmcneill u_int e;
201 1.1 jmcneill
202 1.1 jmcneill switch (drv) {
203 1.1 jmcneill case 2:
204 1.1 jmcneill e = GPIO_E_CTL_2MA;
205 1.1 jmcneill break;
206 1.1 jmcneill case 4:
207 1.1 jmcneill e = GPIO_E_CTL_4MA;
208 1.1 jmcneill break;
209 1.1 jmcneill case 8:
210 1.1 jmcneill e = GPIO_E_CTL_8MA;
211 1.1 jmcneill break;
212 1.1 jmcneill case 12:
213 1.1 jmcneill e = GPIO_E_CTL_12MA;
214 1.1 jmcneill break;
215 1.1 jmcneill default:
216 1.1 jmcneill return;
217 1.1 jmcneill }
218 1.1 jmcneill
219 1.1 jmcneill val = GPIO_E_CTL_MASK << (reg->drv_bit + 16);
220 1.1 jmcneill val |= e << reg->drv_bit;
221 1.1 jmcneill WR4(reg, reg->drv_reg, val);
222 1.1 jmcneill }
223 1.1 jmcneill
224 1.1 jmcneill static void
225 1.1 jmcneill rk3288_iomux_set_mux(struct rk3288_iomux_softc *sc,
226 1.1 jmcneill struct rk3288_iomux_reg *reg, u_int mux)
227 1.1 jmcneill {
228 1.1 jmcneill uint32_t val;
229 1.1 jmcneill
230 1.1 jmcneill KASSERT(reg->mux_reg != -1);
231 1.1 jmcneill
232 1.1 jmcneill val = ((reg->flags & IOMUX_4BIT) ? 0xf : 0x3) << (reg->mux_bit + 16);
233 1.1 jmcneill val |= mux << reg->mux_bit;
234 1.1 jmcneill
235 1.1 jmcneill WR4(reg, reg->mux_reg, val);
236 1.1 jmcneill }
237 1.1 jmcneill
238 1.1 jmcneill static void
239 1.1 jmcneill rk3288_iomux_config(struct rk3288_iomux_softc *sc, struct rk3288_iomux_reg *reg,
240 1.1 jmcneill u_int mux, const int phandle)
241 1.1 jmcneill {
242 1.1 jmcneill int bias, drv;
243 1.1 jmcneill
244 1.1 jmcneill bias = fdtbus_pinctrl_parse_bias(phandle, NULL);
245 1.1 jmcneill drv = fdtbus_pinctrl_parse_drive_strength(phandle);
246 1.1 jmcneill
247 1.1 jmcneill #ifdef RK3288_IOMUX_DEBUG
248 1.1 jmcneill printf(" -> %s mux %#x/%d, pull %#x/%d, drv %#x/%d, flags %#x\n",
249 1.1 jmcneill reg->syscon == sc->sc_pmu ? "pmu" : "grf",
250 1.1 jmcneill reg->mux_reg, reg->mux_bit,
251 1.1 jmcneill reg->pull_reg, reg->pull_bit,
252 1.1 jmcneill reg->drv_reg, reg->drv_bit,
253 1.1 jmcneill reg->flags);
254 1.1 jmcneill printf(" bias %d drv %d mux %u\n", bias, drv, mux);
255 1.1 jmcneill #endif
256 1.1 jmcneill
257 1.1 jmcneill /* XXX
258 1.1 jmcneill * ASUS Tinkerboard goes nuts if we update any PMU bias fields.
259 1.1 jmcneill * Skip them until we figure out why.
260 1.1 jmcneill */
261 1.1 jmcneill if (reg->syscon == sc->sc_pmu) {
262 1.1 jmcneill bias = -1;
263 1.1 jmcneill }
264 1.1 jmcneill
265 1.1 jmcneill LOCK(reg);
266 1.1 jmcneill
267 1.1 jmcneill if (bias != -1) {
268 1.1 jmcneill rk3288_iomux_set_bias(sc, reg, bias);
269 1.1 jmcneill }
270 1.1 jmcneill if (drv != -1) {
271 1.1 jmcneill rk3288_iomux_set_drive_strength(sc, reg, drv);
272 1.1 jmcneill }
273 1.1 jmcneill if ((reg->flags & IOMUX_NOROUTE) == 0) {
274 1.1 jmcneill rk3288_iomux_set_mux(sc, reg, mux);
275 1.1 jmcneill }
276 1.1 jmcneill
277 1.1 jmcneill UNLOCK(reg);
278 1.1 jmcneill }
279 1.1 jmcneill
280 1.1 jmcneill static int
281 1.1 jmcneill rk3288_iomux_pinctrl_set_config(device_t dev, const void *data, size_t len)
282 1.1 jmcneill {
283 1.1 jmcneill struct rk3288_iomux_softc * const sc = device_private(dev);
284 1.1 jmcneill int pins_len = 0;
285 1.1 jmcneill
286 1.1 jmcneill if (len != 4) {
287 1.1 jmcneill return -1;
288 1.1 jmcneill }
289 1.1 jmcneill
290 1.1 jmcneill const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
291 1.1 jmcneill const u_int *pins = fdtbus_get_prop(phandle, "rockchip,pins", &pins_len);
292 1.1 jmcneill
293 1.1 jmcneill while (pins_len >= 16) {
294 1.1 jmcneill const u_int bank = be32toh(pins[0]);
295 1.1 jmcneill const u_int idx = be32toh(pins[1]);
296 1.1 jmcneill const u_int mux = be32toh(pins[2]);
297 1.1 jmcneill const int cfg = fdtbus_get_phandle_from_native(be32toh(pins[3]));
298 1.1 jmcneill struct rk3288_iomux_reg regdef = {};
299 1.1 jmcneill
300 1.1 jmcneill if (rk3288_iomux_get_reg(sc, bank, idx, ®def)) {
301 1.1 jmcneill #ifdef RK3288_IOMUX_DEBUG
302 1.1 jmcneill printf(" -> gpio%u P%c%u (%u)\n", bank, 'A' + (idx / 8), idx % 8, idx);
303 1.1 jmcneill #endif
304 1.1 jmcneill rk3288_iomux_config(sc, ®def, mux, cfg);
305 1.1 jmcneill } else {
306 1.1 jmcneill aprint_error_dev(dev, "unsupported iomux bank %u idx %u\n",
307 1.1 jmcneill bank, mux);
308 1.1 jmcneill }
309 1.1 jmcneill
310 1.1 jmcneill pins_len -= 16;
311 1.1 jmcneill pins += 4;
312 1.1 jmcneill }
313 1.1 jmcneill
314 1.1 jmcneill return 0;
315 1.1 jmcneill }
316 1.1 jmcneill
317 1.1 jmcneill static struct fdtbus_pinctrl_controller_func rk3288_iomux_pinctrl_funcs = {
318 1.1 jmcneill .set_config = rk3288_iomux_pinctrl_set_config,
319 1.1 jmcneill };
320 1.1 jmcneill
321 1.1 jmcneill static int
322 1.1 jmcneill rk3288_iomux_match(device_t parent, cfdata_t cf, void *aux)
323 1.1 jmcneill {
324 1.1 jmcneill struct fdt_attach_args * const faa = aux;
325 1.1 jmcneill
326 1.1 jmcneill return of_compatible_match(faa->faa_phandle, compat_data);
327 1.1 jmcneill }
328 1.1 jmcneill
329 1.1 jmcneill static void
330 1.1 jmcneill rk3288_iomux_attach(device_t parent, device_t self, void *aux)
331 1.1 jmcneill {
332 1.1 jmcneill struct rk3288_iomux_softc * const sc = device_private(self);
333 1.1 jmcneill struct fdt_attach_args * const faa = aux;
334 1.1 jmcneill const int phandle = faa->faa_phandle;
335 1.1 jmcneill
336 1.1 jmcneill sc->sc_dev = self;
337 1.1 jmcneill sc->sc_grf = fdtbus_syscon_acquire(phandle, "rockchip,grf");
338 1.1 jmcneill if (sc->sc_grf == NULL) {
339 1.1 jmcneill aprint_error(": couldn't acquire grf syscon\n");
340 1.1 jmcneill return;
341 1.1 jmcneill }
342 1.1 jmcneill sc->sc_pmu = fdtbus_syscon_acquire(phandle, "rockchip,pmu");
343 1.1 jmcneill if (sc->sc_pmu == NULL) {
344 1.1 jmcneill aprint_error(": couldn't acquire pmu syscon\n");
345 1.1 jmcneill return;
346 1.1 jmcneill }
347 1.1 jmcneill
348 1.1 jmcneill aprint_naive("\n");
349 1.1 jmcneill aprint_normal(": RK3288 IOMUX control\n");
350 1.1 jmcneill
351 1.1 jmcneill for (int child = OF_child(phandle); child; child = OF_peer(child)) {
352 1.1 jmcneill for (int sub = OF_child(child); sub; sub = OF_peer(sub)) {
353 1.1 jmcneill if (!of_hasprop(sub, "rockchip,pins"))
354 1.1 jmcneill continue;
355 1.1 jmcneill fdtbus_register_pinctrl_config(self, sub, &rk3288_iomux_pinctrl_funcs);
356 1.1 jmcneill }
357 1.1 jmcneill }
358 1.1 jmcneill
359 1.1 jmcneill for (int child = OF_child(phandle); child; child = OF_peer(child)) {
360 1.1 jmcneill struct fdt_attach_args cfaa = *faa;
361 1.1 jmcneill cfaa.faa_phandle = child;
362 1.1 jmcneill cfaa.faa_name = fdtbus_get_string(child, "name");
363 1.1 jmcneill cfaa.faa_quiet = false;
364 1.1 jmcneill
365 1.1 jmcneill config_found(self, &cfaa, NULL, CFARGS_NONE);
366 1.1 jmcneill }
367 1.1 jmcneill }
368