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