rk3399_iomux.c revision 1.1 1 1.1 jmcneill /* $NetBSD: rk3399_iomux.c,v 1.1 2018/08/12 16:48:05 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 //#define RK3399_IOMUX_DEBUG
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: rk3399_iomux.c,v 1.1 2018/08/12 16:48:05 jmcneill Exp $");
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/bus.h>
36 1.1 jmcneill #include <sys/device.h>
37 1.1 jmcneill #include <sys/intr.h>
38 1.1 jmcneill #include <sys/systm.h>
39 1.1 jmcneill #include <sys/mutex.h>
40 1.1 jmcneill #include <sys/kmem.h>
41 1.1 jmcneill #include <sys/gpio.h>
42 1.1 jmcneill #include <sys/lwp.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <dev/fdt/fdtvar.h>
45 1.1 jmcneill #include <dev/fdt/syscon.h>
46 1.1 jmcneill
47 1.1 jmcneill /* PU/PD control */
48 1.1 jmcneill #define GRF_GPIO_P_CTL(_idx) (0x3 << (((_idx) & 7) * 2))
49 1.1 jmcneill #define GRF_GPIO_P_WRITE_EN(_idx) (0x3 << (((_idx) & 7) * 2 + 16))
50 1.1 jmcneill /* Different bias value mapping for GRF and PMU registers */
51 1.1 jmcneill #define GRF_GPIO_P_CTL_Z 0
52 1.1 jmcneill #define GRF_GPIO_P_CTL_PULLDOWN 1
53 1.1 jmcneill #define GRF_GPIO_P_CTL_Z_ALT 2
54 1.1 jmcneill #define GRF_GPIO_P_CTL_PULLUP 3
55 1.1 jmcneill #define PMU_GPIO_P_CTL_Z 0
56 1.1 jmcneill #define PMU_GPIO_P_CTL_PULLUP 1
57 1.1 jmcneill #define PMU_GPIO_P_CTL_PULLDOWN 2
58 1.1 jmcneill #define PMU_GPIO_P_CTL_RESERVED 3
59 1.1 jmcneill
60 1.1 jmcneill /* Drive strength control */
61 1.1 jmcneill /* Different drive strength value mapping for GRF and PMU registers */
62 1.1 jmcneill #define GRF_GPIO_E_CTL_2MA 0
63 1.1 jmcneill #define GRF_GPIO_E_CTL_4MA 1
64 1.1 jmcneill #define GRF_GPIO_E_CTL_8MA 2
65 1.1 jmcneill #define GRF_GPIO_E_CTL_12MA 3
66 1.1 jmcneill #define PMU_GPIO_E_CTL_5MA 0
67 1.1 jmcneill #define PMU_GPIO_E_CTL_10MA 1
68 1.1 jmcneill #define PMU_GPIO_E_CTL_15MA 2
69 1.1 jmcneill #define PMU_GPIO_E_CTL_20MA 3
70 1.1 jmcneill
71 1.1 jmcneill enum rk3399_drv_type {
72 1.1 jmcneill RK3399_DRV_TYPE_IO_DEFAULT,
73 1.1 jmcneill RK3399_DRV_TYPE_IO_1V8_3V0,
74 1.1 jmcneill RK3399_DRV_TYPE_IO_1V8,
75 1.1 jmcneill RK3399_DRV_TYPE_IO_1V8_3V0_AUTO,
76 1.1 jmcneill RK3399_DRV_TYPE_IO_3V3,
77 1.1 jmcneill };
78 1.1 jmcneill
79 1.1 jmcneill static int rk3399_drv_strength[5][9] = {
80 1.1 jmcneill [RK3399_DRV_TYPE_IO_DEFAULT] = { 2, 4, 8, 12, -1 },
81 1.1 jmcneill [RK3399_DRV_TYPE_IO_1V8_3V0] = { 3, 6, 9, 12, -1 },
82 1.1 jmcneill [RK3399_DRV_TYPE_IO_1V8] = { 5, 10, 15, 20, -1 },
83 1.1 jmcneill [RK3399_DRV_TYPE_IO_1V8_3V0_AUTO] = { 4, 6, 8, 10, 12, 14, 16, 18, -1 },
84 1.1 jmcneill [RK3399_DRV_TYPE_IO_3V3] = { 4, 7, 10, 13, 16, 19, 22, 26, -1 },
85 1.1 jmcneill };
86 1.1 jmcneill
87 1.1 jmcneill struct rk3399_iomux {
88 1.1 jmcneill enum rk3399_drv_type drv_type;
89 1.1 jmcneill };
90 1.1 jmcneill
91 1.1 jmcneill struct rk3399_iomux_bank {
92 1.1 jmcneill struct rk3399_iomux iomux[5];
93 1.1 jmcneill u_int regs;
94 1.1 jmcneill #define RK_IOMUX_REGS_GRF 0
95 1.1 jmcneill #define RK_IOMUX_REGS_PMU 1
96 1.1 jmcneill };
97 1.1 jmcneill
98 1.1 jmcneill static const struct rk3399_iomux_bank rk3399_iomux_banks[] = {
99 1.1 jmcneill [0] = {
100 1.1 jmcneill .regs = RK_IOMUX_REGS_PMU,
101 1.1 jmcneill .iomux = {
102 1.1 jmcneill [0] = { .drv_type = RK3399_DRV_TYPE_IO_1V8 },
103 1.1 jmcneill [1] = { .drv_type = RK3399_DRV_TYPE_IO_1V8 },
104 1.1 jmcneill [2] = { .drv_type = RK3399_DRV_TYPE_IO_DEFAULT },
105 1.1 jmcneill [3] = { .drv_type = RK3399_DRV_TYPE_IO_DEFAULT },
106 1.1 jmcneill },
107 1.1 jmcneill },
108 1.1 jmcneill [1] = {
109 1.1 jmcneill .regs = RK_IOMUX_REGS_PMU,
110 1.1 jmcneill .iomux = {
111 1.1 jmcneill [0] = { .drv_type = RK3399_DRV_TYPE_IO_1V8_3V0 },
112 1.1 jmcneill [1] = { .drv_type = RK3399_DRV_TYPE_IO_1V8_3V0 },
113 1.1 jmcneill [2] = { .drv_type = RK3399_DRV_TYPE_IO_1V8_3V0 },
114 1.1 jmcneill [3] = { .drv_type = RK3399_DRV_TYPE_IO_1V8_3V0 },
115 1.1 jmcneill }
116 1.1 jmcneill },
117 1.1 jmcneill [2] = {
118 1.1 jmcneill .regs = RK_IOMUX_REGS_GRF,
119 1.1 jmcneill .iomux = {
120 1.1 jmcneill [0] = { .drv_type = RK3399_DRV_TYPE_IO_1V8_3V0 },
121 1.1 jmcneill [1] = { .drv_type = RK3399_DRV_TYPE_IO_1V8_3V0 },
122 1.1 jmcneill [2] = { .drv_type = RK3399_DRV_TYPE_IO_1V8 },
123 1.1 jmcneill [3] = { .drv_type = RK3399_DRV_TYPE_IO_1V8 },
124 1.1 jmcneill },
125 1.1 jmcneill },
126 1.1 jmcneill [3] = {
127 1.1 jmcneill .regs = RK_IOMUX_REGS_GRF,
128 1.1 jmcneill .iomux = {
129 1.1 jmcneill [0] = { .drv_type = RK3399_DRV_TYPE_IO_3V3 },
130 1.1 jmcneill [1] = { .drv_type = RK3399_DRV_TYPE_IO_3V3 },
131 1.1 jmcneill [2] = { .drv_type = RK3399_DRV_TYPE_IO_3V3 },
132 1.1 jmcneill [3] = { .drv_type = RK3399_DRV_TYPE_IO_1V8_3V0 },
133 1.1 jmcneill },
134 1.1 jmcneill },
135 1.1 jmcneill [4] = {
136 1.1 jmcneill .regs = RK_IOMUX_REGS_GRF,
137 1.1 jmcneill .iomux = {
138 1.1 jmcneill [0] = { .drv_type = RK3399_DRV_TYPE_IO_1V8_3V0 },
139 1.1 jmcneill [1] = { .drv_type = RK3399_DRV_TYPE_IO_1V8_3V0_AUTO },
140 1.1 jmcneill [2] = { .drv_type = RK3399_DRV_TYPE_IO_1V8_3V0 },
141 1.1 jmcneill [3] = { .drv_type = RK3399_DRV_TYPE_IO_1V8_3V0 },
142 1.1 jmcneill },
143 1.1 jmcneill },
144 1.1 jmcneill };
145 1.1 jmcneill
146 1.1 jmcneill #define RK3399_IOMUX_BANK_IS_PMU(_bank) (rk3399_iomux_banks[(_bank)].regs == RK_IOMUX_REGS_PMU)
147 1.1 jmcneill
148 1.1 jmcneill struct rk3399_iomux_conf {
149 1.1 jmcneill const struct rk3399_iomux_bank *banks;
150 1.1 jmcneill u_int nbanks;
151 1.1 jmcneill };
152 1.1 jmcneill
153 1.1 jmcneill static const struct rk3399_iomux_conf rk3399_iomux_conf = {
154 1.1 jmcneill .banks = rk3399_iomux_banks,
155 1.1 jmcneill .nbanks = __arraycount(rk3399_iomux_banks),
156 1.1 jmcneill };
157 1.1 jmcneill
158 1.1 jmcneill static const struct of_compat_data compat_data[] = {
159 1.1 jmcneill { "rockchip,rk3399-pinctrl", (uintptr_t)&rk3399_iomux_conf },
160 1.1 jmcneill { NULL }
161 1.1 jmcneill };
162 1.1 jmcneill
163 1.1 jmcneill struct rk3399_iomux_softc {
164 1.1 jmcneill device_t sc_dev;
165 1.1 jmcneill struct syscon *sc_syscon[2];
166 1.1 jmcneill
167 1.1 jmcneill const struct rk3399_iomux_conf *sc_conf;
168 1.1 jmcneill };
169 1.1 jmcneill
170 1.1 jmcneill #define LOCK(syscon) \
171 1.1 jmcneill syscon_lock(syscon)
172 1.1 jmcneill #define UNLOCK(syscon) \
173 1.1 jmcneill syscon_unlock(syscon)
174 1.1 jmcneill #define RD4(syscon, reg) \
175 1.1 jmcneill syscon_read_4(syscon, (reg))
176 1.1 jmcneill #define WR4(syscon, reg, val) \
177 1.1 jmcneill syscon_write_4(syscon, (reg), (val))
178 1.1 jmcneill
179 1.1 jmcneill static int rk3399_iomux_match(device_t, cfdata_t, void *);
180 1.1 jmcneill static void rk3399_iomux_attach(device_t, device_t, void *);
181 1.1 jmcneill
182 1.1 jmcneill CFATTACH_DECL_NEW(rk3399_iomux, sizeof(struct rk3399_iomux_softc),
183 1.1 jmcneill rk3399_iomux_match, rk3399_iomux_attach, NULL, NULL);
184 1.1 jmcneill
185 1.1 jmcneill static void
186 1.1 jmcneill rk3399_iomux_set_bias(struct rk3399_iomux_softc *sc, u_int bank, u_int idx, int flags)
187 1.1 jmcneill {
188 1.1 jmcneill const struct rk3399_iomux_bank *banks = sc->sc_conf->banks;
189 1.1 jmcneill bus_size_t reg;
190 1.1 jmcneill u_int bias;
191 1.1 jmcneill
192 1.1 jmcneill KASSERT(bank < sc->sc_conf->nbanks);
193 1.1 jmcneill
194 1.1 jmcneill struct syscon * const syscon = sc->sc_syscon[banks[bank].regs];
195 1.1 jmcneill if (RK3399_IOMUX_BANK_IS_PMU(bank)) {
196 1.1 jmcneill reg = 0x00040 + (0x10 * bank);
197 1.1 jmcneill } else {
198 1.1 jmcneill reg = 0x0e040 + (0x10 * (bank - 2));
199 1.1 jmcneill }
200 1.1 jmcneill reg += 0x4 * (idx / 8);
201 1.1 jmcneill
202 1.1 jmcneill if (flags == GPIO_PIN_PULLUP) {
203 1.1 jmcneill bias = RK3399_IOMUX_BANK_IS_PMU(bank) ? PMU_GPIO_P_CTL_PULLUP : GRF_GPIO_P_CTL_PULLUP;
204 1.1 jmcneill } else if (flags == GPIO_PIN_PULLDOWN) {
205 1.1 jmcneill bias = RK3399_IOMUX_BANK_IS_PMU(bank) ? PMU_GPIO_P_CTL_PULLDOWN : GRF_GPIO_P_CTL_PULLDOWN;
206 1.1 jmcneill } else {
207 1.1 jmcneill bias = RK3399_IOMUX_BANK_IS_PMU(bank) ? PMU_GPIO_P_CTL_Z : GRF_GPIO_P_CTL_Z;
208 1.1 jmcneill }
209 1.1 jmcneill
210 1.1 jmcneill const uint32_t bias_val = __SHIFTIN(bias, GRF_GPIO_P_CTL(idx));
211 1.1 jmcneill const uint32_t bias_mask = GRF_GPIO_P_WRITE_EN(idx);
212 1.1 jmcneill
213 1.1 jmcneill #ifdef RK3399_IOMUX_DEBUG
214 1.1 jmcneill printf("%s: bank %d idx %d flags %#x: %08x -> ", __func__, bank, idx, flags, RD4(syscon, reg));
215 1.1 jmcneill #endif
216 1.1 jmcneill WR4(syscon, reg, bias_val | bias_mask);
217 1.1 jmcneill #ifdef RK3399_IOMUX_DEBUG
218 1.1 jmcneill printf("%08x (reg %#lx)\n", RD4(syscon, reg), reg);
219 1.1 jmcneill #endif
220 1.1 jmcneill }
221 1.1 jmcneill
222 1.1 jmcneill static int
223 1.1 jmcneill rk3399_iomux_map_drive_strength(struct rk3399_iomux_softc *sc, enum rk3399_drv_type drv_type, u_int val)
224 1.1 jmcneill {
225 1.1 jmcneill for (int n = 0; rk3399_drv_strength[drv_type][n] != -1; n++)
226 1.1 jmcneill if (rk3399_drv_strength[drv_type][n] == val)
227 1.1 jmcneill return n;
228 1.1 jmcneill return -1;
229 1.1 jmcneill }
230 1.1 jmcneill
231 1.1 jmcneill static int
232 1.1 jmcneill rk3399_iomux_set_drive_strength(struct rk3399_iomux_softc *sc, u_int bank, u_int idx, u_int val)
233 1.1 jmcneill {
234 1.1 jmcneill const struct rk3399_iomux_bank *banks = sc->sc_conf->banks;
235 1.1 jmcneill uint32_t drv_mask, drv_val;
236 1.1 jmcneill bus_size_t reg;
237 1.1 jmcneill
238 1.1 jmcneill KASSERT(bank < sc->sc_conf->nbanks);
239 1.1 jmcneill
240 1.1 jmcneill if (idx >= 32)
241 1.1 jmcneill return EINVAL;
242 1.1 jmcneill
243 1.1 jmcneill const int drv = rk3399_iomux_map_drive_strength(sc, banks[bank].iomux[idx / 8].drv_type, val);
244 1.1 jmcneill if (drv == -1)
245 1.1 jmcneill return EINVAL;
246 1.1 jmcneill
247 1.1 jmcneill struct syscon * const syscon = sc->sc_syscon[banks[bank].regs];
248 1.1 jmcneill switch (bank) {
249 1.1 jmcneill case 0:
250 1.1 jmcneill case 1:
251 1.1 jmcneill reg = 0x00040 + (0x10 * bank) + 0x4 * (idx / 4);
252 1.1 jmcneill drv_mask = 0x3 << ((idx & 7) * 2);
253 1.1 jmcneill break;
254 1.1 jmcneill case 2:
255 1.1 jmcneill reg = 0x0e100 + 0x4 * (idx / 4);
256 1.1 jmcneill drv_mask = 0x3 << ((idx & 7) * 2);
257 1.1 jmcneill break;
258 1.1 jmcneill case 3:
259 1.1 jmcneill switch (idx / 8) {
260 1.1 jmcneill case 0:
261 1.1 jmcneill case 1:
262 1.1 jmcneill case 2:
263 1.1 jmcneill reg = 0x0e110 + 0x8 * (idx / 4);
264 1.1 jmcneill drv_mask = 0x7 << ((idx & 7) * 3);
265 1.1 jmcneill break;
266 1.1 jmcneill case 3:
267 1.1 jmcneill reg = 0x0e128;
268 1.1 jmcneill drv_mask = 0x3 << ((idx & 7) * 2);
269 1.1 jmcneill break;
270 1.1 jmcneill default:
271 1.1 jmcneill return EINVAL;
272 1.1 jmcneill }
273 1.1 jmcneill break;
274 1.1 jmcneill case 4:
275 1.1 jmcneill switch (idx / 8) {
276 1.1 jmcneill case 0:
277 1.1 jmcneill reg = 0x0e12c;
278 1.1 jmcneill drv_mask = 0x3 << ((idx & 7) * 2);
279 1.1 jmcneill break;
280 1.1 jmcneill case 1:
281 1.1 jmcneill reg = 0x0e130;
282 1.1 jmcneill drv_mask = 0x7 << ((idx & 7) * 3);
283 1.1 jmcneill break;
284 1.1 jmcneill case 2:
285 1.1 jmcneill reg = 0x0e138;
286 1.1 jmcneill drv_mask = 0x3 << ((idx & 7) * 2);
287 1.1 jmcneill break;
288 1.1 jmcneill case 3:
289 1.1 jmcneill reg = 0x0e13c;
290 1.1 jmcneill drv_mask = 0x3 << ((idx & 7) * 2);
291 1.1 jmcneill break;
292 1.1 jmcneill default:
293 1.1 jmcneill return EINVAL;
294 1.1 jmcneill }
295 1.1 jmcneill break;
296 1.1 jmcneill default:
297 1.1 jmcneill return EINVAL;
298 1.1 jmcneill }
299 1.1 jmcneill drv_val = __SHIFTIN(val, drv_mask);
300 1.1 jmcneill
301 1.1 jmcneill while (drv_mask) {
302 1.1 jmcneill const uint32_t write_val = drv_val & 0xffff;
303 1.1 jmcneill const uint32_t write_mask = (drv_mask & 0xffff) << 16;
304 1.1 jmcneill if (write_mask) {
305 1.1 jmcneill #ifdef RK3399_IOMUX_DEBUG
306 1.1 jmcneill printf("%s: bank %d idx %d val %d: %08x -> ", __func__, bank, idx, val, RD4(syscon, reg));
307 1.1 jmcneill #endif
308 1.1 jmcneill WR4(syscon, reg, write_val | write_mask);
309 1.1 jmcneill #ifdef RK3399_IOMUX_DEBUG
310 1.1 jmcneill printf("%08x (reg %#lx)\n", RD4(syscon, reg), reg);
311 1.1 jmcneill #endif
312 1.1 jmcneill }
313 1.1 jmcneill reg += 0x4;
314 1.1 jmcneill drv_val >>= 16;
315 1.1 jmcneill drv_mask >>= 16;
316 1.1 jmcneill }
317 1.1 jmcneill
318 1.1 jmcneill return 0;
319 1.1 jmcneill }
320 1.1 jmcneill
321 1.1 jmcneill static void
322 1.1 jmcneill rk3399_iomux_set_mux(struct rk3399_iomux_softc *sc, u_int bank, u_int idx, u_int mux)
323 1.1 jmcneill {
324 1.1 jmcneill const struct rk3399_iomux_bank *banks = sc->sc_conf->banks;
325 1.1 jmcneill bus_size_t reg;
326 1.1 jmcneill uint32_t mask;
327 1.1 jmcneill
328 1.1 jmcneill KASSERT(bank < sc->sc_conf->nbanks);
329 1.1 jmcneill
330 1.1 jmcneill struct syscon * const syscon = sc->sc_syscon[banks[bank].regs];
331 1.1 jmcneill if (RK3399_IOMUX_BANK_IS_PMU(bank)) {
332 1.1 jmcneill reg = 0x00000 + (0x10 * bank);
333 1.1 jmcneill } else {
334 1.1 jmcneill reg = 0x0e000 + (0x10 * (bank - 2));
335 1.1 jmcneill }
336 1.1 jmcneill reg += 0x4 * (idx / 4);
337 1.1 jmcneill mask = 3 << ((idx & 7) * 2);
338 1.1 jmcneill
339 1.1 jmcneill #ifdef RK3399_IOMUX_DEBUG
340 1.1 jmcneill printf("%s: bank %d idx %d mux %#x: %08x -> ", __func__, bank, idx, mux, RD4(syscon, reg));
341 1.1 jmcneill #endif
342 1.1 jmcneill WR4(syscon, reg, (mask << 16) | __SHIFTIN(mux, mask));
343 1.1 jmcneill #ifdef RK3399_IOMUX_DEBUG
344 1.1 jmcneill printf("%08x (reg %#lx)\n", RD4(syscon, reg), reg);
345 1.1 jmcneill #endif
346 1.1 jmcneill }
347 1.1 jmcneill
348 1.1 jmcneill static int
349 1.1 jmcneill rk3399_iomux_config(struct rk3399_iomux_softc *sc, const int phandle, u_int bank, u_int idx, u_int mux)
350 1.1 jmcneill {
351 1.1 jmcneill u_int drv;
352 1.1 jmcneill
353 1.1 jmcneill if (of_hasprop(phandle, "bias-disable"))
354 1.1 jmcneill rk3399_iomux_set_bias(sc, bank, idx, 0);
355 1.1 jmcneill else if (of_hasprop(phandle, "bias-pull-up"))
356 1.1 jmcneill rk3399_iomux_set_bias(sc, bank, idx, GPIO_PIN_PULLUP);
357 1.1 jmcneill else if (of_hasprop(phandle, "bias-pull-down"))
358 1.1 jmcneill rk3399_iomux_set_bias(sc, bank, idx, GPIO_PIN_PULLDOWN);
359 1.1 jmcneill
360 1.1 jmcneill if (of_getprop_uint32(phandle, "drive-strength", &drv) == 0) {
361 1.1 jmcneill if (rk3399_iomux_set_drive_strength(sc, bank, idx, drv) != 0)
362 1.1 jmcneill return EINVAL;
363 1.1 jmcneill }
364 1.1 jmcneill
365 1.1 jmcneill #if notyet
366 1.1 jmcneill if (of_hasprop(phandle, "input-enable"))
367 1.1 jmcneill rk3399_iomux_set_direction(sc, bank, idx, GPIO_PIN_INPUT, -1);
368 1.1 jmcneill else if (of_hasprop(phandle, "output-high"))
369 1.1 jmcneill rk3399_iomux_set_direction(sc, bank, idx, GPIO_PIN_OUTPUT, GPIO_PIN_HIGH);
370 1.1 jmcneill else if (of_hasprop(phandle, "output-low"))
371 1.1 jmcneill rk3399_iomux_set_direction(sc, bank, idx, GPIO_PIN_OUTPUT, GPIO_PIN_LOW);
372 1.1 jmcneill #endif
373 1.1 jmcneill
374 1.1 jmcneill rk3399_iomux_set_mux(sc, bank, idx, mux);
375 1.1 jmcneill
376 1.1 jmcneill return 0;
377 1.1 jmcneill }
378 1.1 jmcneill
379 1.1 jmcneill static int
380 1.1 jmcneill rk3399_iomux_pinctrl_set_config(device_t dev, const void *data, size_t len)
381 1.1 jmcneill {
382 1.1 jmcneill struct rk3399_iomux_softc * const sc = device_private(dev);
383 1.1 jmcneill const struct rk3399_iomux_bank *banks = sc->sc_conf->banks;
384 1.1 jmcneill int pins_len;
385 1.1 jmcneill
386 1.1 jmcneill if (len != 4)
387 1.1 jmcneill return -1;
388 1.1 jmcneill
389 1.1 jmcneill const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
390 1.1 jmcneill const u_int *pins = fdtbus_get_prop(phandle, "rockchip,pins", &pins_len);
391 1.1 jmcneill
392 1.1 jmcneill while (pins_len >= 16) {
393 1.1 jmcneill const u_int bank = be32toh(pins[0]);
394 1.1 jmcneill const u_int idx = be32toh(pins[1]);
395 1.1 jmcneill const u_int mux = be32toh(pins[2]);
396 1.1 jmcneill const int cfg = fdtbus_get_phandle_from_native(be32toh(pins[3]));
397 1.1 jmcneill
398 1.1 jmcneill struct syscon * const syscon = sc->sc_syscon[banks[bank].regs];
399 1.1 jmcneill LOCK(syscon);
400 1.1 jmcneill rk3399_iomux_config(sc, cfg, bank, idx, mux);
401 1.1 jmcneill UNLOCK(syscon);
402 1.1 jmcneill
403 1.1 jmcneill pins_len -= 16;
404 1.1 jmcneill pins += 4;
405 1.1 jmcneill }
406 1.1 jmcneill
407 1.1 jmcneill return 0;
408 1.1 jmcneill }
409 1.1 jmcneill
410 1.1 jmcneill static struct fdtbus_pinctrl_controller_func rk3399_iomux_pinctrl_funcs = {
411 1.1 jmcneill .set_config = rk3399_iomux_pinctrl_set_config,
412 1.1 jmcneill };
413 1.1 jmcneill
414 1.1 jmcneill static int
415 1.1 jmcneill rk3399_iomux_match(device_t parent, cfdata_t cf, void *aux)
416 1.1 jmcneill {
417 1.1 jmcneill struct fdt_attach_args * const faa = aux;
418 1.1 jmcneill
419 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
420 1.1 jmcneill }
421 1.1 jmcneill
422 1.1 jmcneill static void
423 1.1 jmcneill rk3399_iomux_attach(device_t parent, device_t self, void *aux)
424 1.1 jmcneill {
425 1.1 jmcneill struct rk3399_iomux_softc * const sc = device_private(self);
426 1.1 jmcneill struct fdt_attach_args * const faa = aux;
427 1.1 jmcneill const int phandle = faa->faa_phandle;
428 1.1 jmcneill int child, sub;
429 1.1 jmcneill
430 1.1 jmcneill sc->sc_dev = self;
431 1.1 jmcneill sc->sc_syscon[RK_IOMUX_REGS_GRF] = fdtbus_syscon_acquire(phandle, "rockchip,grf");
432 1.1 jmcneill if (sc->sc_syscon[RK_IOMUX_REGS_GRF] == NULL) {
433 1.1 jmcneill aprint_error(": couldn't acquire grf syscon\n");
434 1.1 jmcneill return;
435 1.1 jmcneill }
436 1.1 jmcneill sc->sc_syscon[RK_IOMUX_REGS_PMU] = fdtbus_syscon_acquire(phandle, "rockchip,pmu");
437 1.1 jmcneill if (sc->sc_syscon[RK_IOMUX_REGS_PMU] == NULL) {
438 1.1 jmcneill aprint_error(": couldn't acquire pmu syscon\n");
439 1.1 jmcneill return;
440 1.1 jmcneill }
441 1.1 jmcneill sc->sc_conf = (void *)of_search_compatible(phandle, compat_data)->data;
442 1.1 jmcneill
443 1.1 jmcneill aprint_naive("\n");
444 1.1 jmcneill aprint_normal(": RK3399 IOMUX control\n");
445 1.1 jmcneill
446 1.1 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
447 1.1 jmcneill for (sub = OF_child(child); sub; sub = OF_peer(sub)) {
448 1.1 jmcneill if (!of_hasprop(sub, "rockchip,pins"))
449 1.1 jmcneill continue;
450 1.1 jmcneill fdtbus_register_pinctrl_config(self, sub, &rk3399_iomux_pinctrl_funcs);
451 1.1 jmcneill }
452 1.1 jmcneill }
453 1.1 jmcneill
454 1.1 jmcneill fdtbus_pinctrl_configure();
455 1.1 jmcneill
456 1.1 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
457 1.1 jmcneill struct fdt_attach_args cfaa = *faa;
458 1.1 jmcneill cfaa.faa_phandle = child;
459 1.1 jmcneill cfaa.faa_name = fdtbus_get_string(child, "name");
460 1.1 jmcneill cfaa.faa_quiet = false;
461 1.1 jmcneill
462 1.1 jmcneill config_found(self, &cfaa, NULL);
463 1.1 jmcneill }
464 1.1 jmcneill }
465