tegra_pinmux.c revision 1.1 1 1.1 jmcneill /* $NetBSD: tegra_pinmux.c,v 1.1 2017/09/22 14:36:22 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2015-2017 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 "opt_tegra.h"
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: tegra_pinmux.c,v 1.1 2017/09/22 14:36:22 jmcneill Exp $");
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/types.h>
36 1.1 jmcneill #include <sys/bus.h>
37 1.1 jmcneill #include <sys/device.h>
38 1.1 jmcneill #include <sys/intr.h>
39 1.1 jmcneill #include <sys/systm.h>
40 1.1 jmcneill #include <sys/kernel.h>
41 1.1 jmcneill #include <sys/kmem.h>
42 1.1 jmcneill
43 1.1 jmcneill #include <arm/nvidia/tegra_reg.h>
44 1.1 jmcneill #include <arm/nvidia/tegra_var.h>
45 1.1 jmcneill #include <arm/nvidia/tegra_pinmux.h>
46 1.1 jmcneill
47 1.1 jmcneill #include <dev/fdt/fdtvar.h>
48 1.1 jmcneill
49 1.1 jmcneill /* PINMUX fields */
50 1.1 jmcneill #define PINMUX_DRV_TYPE __BITS(14,13)
51 1.1 jmcneill #define PINMUX_E_SCHMT __BIT(12)
52 1.1 jmcneill #define PINMUX_E_OD __BIT(11)
53 1.1 jmcneill #define PINMUX_E_IO_HV __BIT(10)
54 1.1 jmcneill #define PINMUX_E_HSM __BIT(9)
55 1.1 jmcneill #define PINMUX_LOCK __BIT(7)
56 1.1 jmcneill #define PINMUX_E_INPUT __BIT(6)
57 1.1 jmcneill #define PINMUX_PARK __BIT(5)
58 1.1 jmcneill #define PINMUX_TRISTATE __BIT(4)
59 1.1 jmcneill #define PINMUX_PUPD __BITS(3,2)
60 1.1 jmcneill #define PINMUX_PM __BITS(1,0)
61 1.1 jmcneill
62 1.1 jmcneill struct tegra_pinmux_softc {
63 1.1 jmcneill device_t sc_dev;
64 1.1 jmcneill bus_space_tag_t sc_bst;
65 1.1 jmcneill bus_space_handle_t sc_bsh[2];
66 1.1 jmcneill const struct tegra_pinmux_conf *sc_conf;
67 1.1 jmcneill };
68 1.1 jmcneill
69 1.1 jmcneill #define PADCTRL_WRITE(sc, reg, val) \
70 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh[0], (reg), (val))
71 1.1 jmcneill #define PADCTRL_READ(sc, reg) \
72 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh[0], (reg))
73 1.1 jmcneill #define PINMUX_WRITE(sc, reg, val) \
74 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh[1], (reg), (val))
75 1.1 jmcneill #define PINMUX_READ(sc, reg) \
76 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh[1], (reg))
77 1.1 jmcneill
78 1.1 jmcneill static const struct of_compat_data compat_data[] = {
79 1.1 jmcneill #ifdef SOC_TEGRA210
80 1.1 jmcneill { "nvidia,tegra210-pinmux", (uintptr_t)&tegra210_pinmux_conf },
81 1.1 jmcneill #endif
82 1.1 jmcneill { NULL }
83 1.1 jmcneill };
84 1.1 jmcneill
85 1.1 jmcneill static const struct tegra_pinmux_pins *
86 1.1 jmcneill tegra_pinmux_lookup_byname(struct tegra_pinmux_softc *sc, const char *name)
87 1.1 jmcneill {
88 1.1 jmcneill const struct tegra_pinmux_pins *pin_def;
89 1.1 jmcneill u_int n;
90 1.1 jmcneill
91 1.1 jmcneill for (n = 0; n < sc->sc_conf->npins; n++) {
92 1.1 jmcneill pin_def = &sc->sc_conf->pins[n];
93 1.1 jmcneill if (strcmp(pin_def->name, name) == 0)
94 1.1 jmcneill return pin_def;
95 1.1 jmcneill }
96 1.1 jmcneill
97 1.1 jmcneill return NULL;
98 1.1 jmcneill }
99 1.1 jmcneill
100 1.1 jmcneill static int
101 1.1 jmcneill tegra_pinmux_lookup_func(const struct tegra_pinmux_pins *pin_def, const int phandle)
102 1.1 jmcneill {
103 1.1 jmcneill const char *func;
104 1.1 jmcneill u_int n, valid;
105 1.1 jmcneill
106 1.1 jmcneill func = fdtbus_get_string(phandle, "nvidia,function");
107 1.1 jmcneill if (func == NULL)
108 1.1 jmcneill return -1;
109 1.1 jmcneill
110 1.1 jmcneill for (n = 0, valid = 0; n < TEGRA_PINMUX_MAXFUNC; n++) {
111 1.1 jmcneill if (pin_def->functions[n] == NULL)
112 1.1 jmcneill continue;
113 1.1 jmcneill ++valid;
114 1.1 jmcneill if (strcmp(pin_def->functions[n], func) == 0)
115 1.1 jmcneill return n;
116 1.1 jmcneill }
117 1.1 jmcneill
118 1.1 jmcneill if (valid > 0)
119 1.1 jmcneill aprint_error("%s: pin %s does not support function %s\n",
120 1.1 jmcneill __func__, pin_def->name, func);
121 1.1 jmcneill
122 1.1 jmcneill return -1;
123 1.1 jmcneill }
124 1.1 jmcneill
125 1.1 jmcneill static void
126 1.1 jmcneill tegra_pinmux_pin_config(struct tegra_pinmux_softc *sc,
127 1.1 jmcneill const struct tegra_pinmux_pins *pin_def, const int phandle)
128 1.1 jmcneill {
129 1.1 jmcneill uint32_t cfg;
130 1.1 jmcneill u_int val;
131 1.1 jmcneill
132 1.1 jmcneill cfg = PINMUX_READ(sc, pin_def->reg);
133 1.1 jmcneill const uint32_t ocfg = cfg;
134 1.1 jmcneill
135 1.1 jmcneill const int func = tegra_pinmux_lookup_func(pin_def, phandle);
136 1.1 jmcneill if (func != -1) {
137 1.1 jmcneill cfg &= ~PINMUX_PM;
138 1.1 jmcneill cfg |= __SHIFTIN(func, PINMUX_PM);
139 1.1 jmcneill }
140 1.1 jmcneill if (of_getprop_uint32(phandle, "nvidia,pull", &val) == 0) {
141 1.1 jmcneill cfg &= ~PINMUX_PUPD;
142 1.1 jmcneill cfg |= __SHIFTIN(val, PINMUX_PUPD);
143 1.1 jmcneill }
144 1.1 jmcneill if (of_getprop_uint32(phandle, "nvidia,tristate", &val) == 0) {
145 1.1 jmcneill cfg &= ~PINMUX_TRISTATE;
146 1.1 jmcneill cfg |= __SHIFTIN(val, PINMUX_TRISTATE);
147 1.1 jmcneill }
148 1.1 jmcneill if (of_getprop_uint32(phandle, "nvidia,open-drain", &val) == 0) {
149 1.1 jmcneill cfg &= ~PINMUX_E_OD;
150 1.1 jmcneill cfg |= __SHIFTIN(val, PINMUX_E_OD);
151 1.1 jmcneill }
152 1.1 jmcneill if (of_getprop_uint32(phandle, "nvidia,lock", &val) == 0) {
153 1.1 jmcneill cfg &= ~PINMUX_LOCK;
154 1.1 jmcneill cfg |= __SHIFTIN(val, PINMUX_LOCK);
155 1.1 jmcneill }
156 1.1 jmcneill if (of_getprop_uint32(phandle, "nvidia,io-hv", &val) == 0) {
157 1.1 jmcneill cfg &= ~PINMUX_E_IO_HV;
158 1.1 jmcneill cfg |= __SHIFTIN(val, PINMUX_E_IO_HV);
159 1.1 jmcneill }
160 1.1 jmcneill if (of_getprop_uint32(phandle, "nvidia,high-speed-mode", &val) == 0) {
161 1.1 jmcneill cfg &= ~PINMUX_E_HSM;
162 1.1 jmcneill cfg |= __SHIFTIN(val, PINMUX_E_HSM);
163 1.1 jmcneill }
164 1.1 jmcneill if (of_getprop_uint32(phandle, "nvidia,schmitt", &val) == 0) {
165 1.1 jmcneill cfg &= ~PINMUX_E_SCHMT;
166 1.1 jmcneill cfg |= __SHIFTIN(val, PINMUX_E_SCHMT);
167 1.1 jmcneill }
168 1.1 jmcneill if (of_getprop_uint32(phandle, "nvidia,drive-type", &val) == 0) {
169 1.1 jmcneill cfg &= ~PINMUX_DRV_TYPE;
170 1.1 jmcneill cfg |= __SHIFTIN(val, PINMUX_DRV_TYPE);
171 1.1 jmcneill }
172 1.1 jmcneill
173 1.1 jmcneill aprint_debug_dev(sc->sc_dev, "pin %s %08x -> %08x\n", pin_def->name, ocfg, cfg);
174 1.1 jmcneill if (cfg != ocfg)
175 1.1 jmcneill PINMUX_WRITE(sc, pin_def->reg, cfg);
176 1.1 jmcneill
177 1.1 jmcneill /*
178 1.1 jmcneill * Not yet supported (PADCTRL):
179 1.1 jmcneill * nvidia,pull-down-strength, nvidia,pull-up-strength
180 1.1 jmcneill * nvidia,slew-rate-rising, nvidia,slew-rate-falling
181 1.1 jmcneill */
182 1.1 jmcneill }
183 1.1 jmcneill
184 1.1 jmcneill static int
185 1.1 jmcneill tegra_pinmux_set_config(device_t dev, const void *data, size_t len)
186 1.1 jmcneill {
187 1.1 jmcneill struct tegra_pinmux_softc * const sc = device_private(dev);
188 1.1 jmcneill const struct tegra_pinmux_pins *pin_def;
189 1.1 jmcneill int child;
190 1.1 jmcneill
191 1.1 jmcneill if (len != 4)
192 1.1 jmcneill return -1;
193 1.1 jmcneill
194 1.1 jmcneill const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
195 1.1 jmcneill
196 1.1 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
197 1.1 jmcneill const char *pins = fdtbus_get_string(child, "nvidia,pins");
198 1.1 jmcneill if (pins == NULL) {
199 1.1 jmcneill aprint_error_dev(dev, "skipping %s (no nvidia,pins property)\n",
200 1.1 jmcneill fdtbus_get_string(child, "name"));
201 1.1 jmcneill continue;
202 1.1 jmcneill }
203 1.1 jmcneill int pins_len = OF_getproplen(child, "nvidia,pins");
204 1.1 jmcneill
205 1.1 jmcneill for (; pins_len > 0;
206 1.1 jmcneill pins_len -= strlen(pins) + 1, pins += strlen(pins) + 1) {
207 1.1 jmcneill pin_def = tegra_pinmux_lookup_byname(sc, pins);
208 1.1 jmcneill if (pin_def == NULL) {
209 1.1 jmcneill aprint_error_dev(dev, "unknown pin name '%s'\n", pins);
210 1.1 jmcneill continue;
211 1.1 jmcneill }
212 1.1 jmcneill
213 1.1 jmcneill tegra_pinmux_pin_config(sc, pin_def, child);
214 1.1 jmcneill }
215 1.1 jmcneill }
216 1.1 jmcneill
217 1.1 jmcneill return 0;
218 1.1 jmcneill }
219 1.1 jmcneill
220 1.1 jmcneill static struct fdtbus_pinctrl_controller_func tegra_pinmux_funcs = {
221 1.1 jmcneill .set_config = tegra_pinmux_set_config,
222 1.1 jmcneill };
223 1.1 jmcneill
224 1.1 jmcneill static int
225 1.1 jmcneill tegra_pinmux_match(device_t parent, cfdata_t cf, void *aux)
226 1.1 jmcneill {
227 1.1 jmcneill struct fdt_attach_args * const faa = aux;
228 1.1 jmcneill
229 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
230 1.1 jmcneill }
231 1.1 jmcneill
232 1.1 jmcneill static void
233 1.1 jmcneill tegra_pinmux_attach(device_t parent, device_t self, void *aux)
234 1.1 jmcneill {
235 1.1 jmcneill struct tegra_pinmux_softc * const sc = device_private(self);
236 1.1 jmcneill struct fdt_attach_args * const faa = aux;
237 1.1 jmcneill const int phandle = faa->faa_phandle;
238 1.1 jmcneill bus_addr_t addr;
239 1.1 jmcneill bus_size_t size;
240 1.1 jmcneill int error, res;
241 1.1 jmcneill int child;
242 1.1 jmcneill
243 1.1 jmcneill sc->sc_dev = self;
244 1.1 jmcneill sc->sc_bst = faa->faa_bst;
245 1.1 jmcneill for (res = 0; res < __arraycount(sc->sc_bsh); res++) {
246 1.1 jmcneill error = fdtbus_get_reg(phandle, res, &addr, &size);
247 1.1 jmcneill if (error != 0) {
248 1.1 jmcneill aprint_error(": couldn't get resource %d: %d\n", res, error);
249 1.1 jmcneill return;
250 1.1 jmcneill }
251 1.1 jmcneill error = bus_space_map(sc->sc_bst, addr, size, res, &sc->sc_bsh[res]);
252 1.1 jmcneill if (error) {
253 1.1 jmcneill aprint_error(": couldn't map %#llx: %d", (uint64_t)addr, error);
254 1.1 jmcneill return;
255 1.1 jmcneill }
256 1.1 jmcneill }
257 1.1 jmcneill sc->sc_conf = (void *)of_search_compatible(phandle, compat_data)->data;
258 1.1 jmcneill
259 1.1 jmcneill aprint_naive("\n");
260 1.1 jmcneill aprint_normal(": Pinmux\n");
261 1.1 jmcneill
262 1.1 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child))
263 1.1 jmcneill fdtbus_register_pinctrl_config(self, child, &tegra_pinmux_funcs);
264 1.1 jmcneill
265 1.1 jmcneill fdtbus_pinctrl_configure();
266 1.1 jmcneill }
267 1.1 jmcneill
268 1.1 jmcneill CFATTACH_DECL_NEW(tegra_pinmux, sizeof(struct tegra_pinmux_softc),
269 1.1 jmcneill tegra_pinmux_match, tegra_pinmux_attach, NULL, NULL);
270