1 /* $NetBSD: pinctrl_single.c,v 1.6 2021/11/07 17:12:25 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2019 Jared McNeill <jmcneill@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: pinctrl_single.c,v 1.6 2021/11/07 17:12:25 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/systm.h>
36 #include <sys/gpio.h>
37
38 #include <dev/fdt/fdtvar.h>
39
40 #define PINCTRL_FLAG_PINCONF __BIT(0) /* supports generic pinconf */
41
42 struct pinctrl_single_config {
43 uint32_t flags;
44 };
45
46 static const struct pinctrl_single_config pinctrl_config = {
47 };
48
49 static const struct pinctrl_single_config pinconf_config = {
50 .flags = PINCTRL_FLAG_PINCONF
51 };
52
53 static const struct device_compatible_entry compat_data[] = {
54 { .compat = "pinctrl-single", .data = &pinctrl_config },
55 { .compat = "pinconf-single", .data = &pinconf_config },
56 DEVICE_COMPAT_EOL
57 };
58
59 struct pinctrl_single_softc {
60 device_t sc_dev;
61 int sc_phandle;
62 bus_space_tag_t sc_bst;
63 bus_space_handle_t sc_bsh;
64 uint32_t sc_flags;
65 u_int sc_regwidth;
66 u_int sc_funcmask;
67 };
68
69 static void
pinctrl_single_pins_write(struct pinctrl_single_softc * sc,u_int off,u_int val)70 pinctrl_single_pins_write(struct pinctrl_single_softc *sc, u_int off, u_int val)
71 {
72 union {
73 uint32_t reg32;
74 uint16_t reg16;
75 uint8_t reg8;
76 } u;
77
78 aprint_debug_dev(sc->sc_dev, "writing %#x with %#x\n", off, val);
79
80 switch (sc->sc_regwidth) {
81 case 8:
82 u.reg8 = bus_space_read_1(sc->sc_bst, sc->sc_bsh, off);
83 u.reg8 &= ~sc->sc_funcmask;
84 u.reg8 |= val;
85 bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, u.reg8);
86 break;
87 case 16:
88 u.reg16 = bus_space_read_2(sc->sc_bst, sc->sc_bsh, off);
89 u.reg16 &= ~sc->sc_funcmask;
90 u.reg16 |= val;
91 bus_space_write_2(sc->sc_bst, sc->sc_bsh, off, u.reg16);
92 break;
93 case 32:
94 u.reg32 = bus_space_read_4(sc->sc_bst, sc->sc_bsh, off);
95 u.reg32 &= ~sc->sc_funcmask;
96 u.reg32 |= val;
97 bus_space_write_4(sc->sc_bst, sc->sc_bsh, off, u.reg32);
98 break;
99 default:
100 device_printf(sc->sc_dev, "%s: unsupported reg width %d\n",
101 __func__, sc->sc_regwidth);
102 break;
103 }
104 }
105
106 static int
pinctrl_single_pins_set_config(device_t dev,const void * data,size_t len)107 pinctrl_single_pins_set_config(device_t dev, const void *data, size_t len)
108 {
109 struct pinctrl_single_softc * const sc = device_private(dev);
110 const u_int *pins;
111 int pinslen;
112
113 if (len != 4 && len != 8)
114 return -1;
115
116 const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
117
118 pins = fdtbus_get_prop(phandle, "pinctrl-single,pins", &pinslen);
119 if (pins == NULL)
120 return -1;
121
122 while (pinslen >= 4 + len) {
123 const int off = be32toh(pins[0]);
124 const int val = be32toh(pins[1]);
125 const int mux = len == 4 ? 0 : be32toh(pins[2]);
126
127 pinctrl_single_pins_write(sc, off, val | mux);
128 pins += 1 + (len / 4);
129 pinslen -= (4 + len);
130 }
131
132 return 0;
133 }
134
135 static struct fdtbus_pinctrl_controller_func pinctrl_single_pins_funcs = {
136 .set_config = pinctrl_single_pins_set_config,
137 };
138
139 static int
pinctrl_single_match(device_t parent,cfdata_t cf,void * aux)140 pinctrl_single_match(device_t parent, cfdata_t cf, void *aux)
141 {
142 struct fdt_attach_args * const faa = aux;
143
144 return of_compatible_match(faa->faa_phandle, compat_data);
145 }
146
147 static void
pinctrl_single_attach(device_t parent,device_t self,void * aux)148 pinctrl_single_attach(device_t parent, device_t self, void *aux)
149 {
150 struct pinctrl_single_softc * const sc = device_private(self);
151 struct fdt_attach_args * const faa = aux;
152 const int phandle = faa->faa_phandle;
153 const struct pinctrl_single_config *conf;
154 bus_addr_t addr;
155 bus_size_t size;
156 int child;
157
158 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
159 aprint_error(": couldn't get registers\n");
160 return;
161 }
162
163 conf = of_compatible_lookup(phandle, compat_data)->data;
164
165 sc->sc_dev = self;
166 sc->sc_phandle = phandle;
167 sc->sc_bst = faa->faa_bst;
168 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
169 aprint_error(": couldn't map registers\n");
170 return;
171 }
172 sc->sc_flags = conf->flags;
173
174 if (of_getprop_uint32(phandle, "pinctrl-single,register-width", &sc->sc_regwidth) != 0) {
175 aprint_error(": missing 'pinctrl-single,register-width' property\n");
176 return;
177 }
178 if (of_getprop_uint32(phandle, "pinctrl-single,function-mask", &sc->sc_funcmask) != 0) {
179 aprint_error(": missing 'pinctrl-single,function-mask' property\n");
180 return;
181 }
182
183 switch (sc->sc_regwidth) {
184 case 8:
185 case 16:
186 case 32:
187 break;
188 default:
189 aprint_error(": register width %d not supported\n", sc->sc_regwidth);
190 return;
191 }
192
193 aprint_naive("\n");
194 aprint_normal("\n");
195
196 for (child = OF_child(phandle); child; child = OF_peer(child)) {
197 if (of_hasprop(child, "pinctrl-single,pins"))
198 fdtbus_register_pinctrl_config(self, child, &pinctrl_single_pins_funcs);
199 }
200
201 fdtbus_pinctrl_set_config(phandle, "default");
202 }
203
204 CFATTACH_DECL_NEW(pinctrl_single, sizeof(struct pinctrl_single_softc),
205 pinctrl_single_match, pinctrl_single_attach, NULL, NULL);
206