1 /*-
2 * Copyright (c) 2014 Warner Losh. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/resource.h>
34 #include <sys/systm.h>
35 #include <sys/rman.h>
36
37 #include <machine/bus.h>
38
39 #include <arm/at91/at91var.h>
40 #include <arm/at91/at91_piovar.h>
41
42 #include <dev/fdt/fdt_common.h>
43 #include <dev/fdt/fdt_pinctrl.h>
44 #include <dev/ofw/openfirm.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47
48 #define BUS_PASS_PINMUX (BUS_PASS_INTERRUPT + 1)
49
50 struct pinctrl_range {
51 uint64_t bus;
52 uint64_t host;
53 uint64_t size;
54 };
55
56 struct pinctrl_softc {
57 device_t dev;
58 phandle_t node;
59
60 struct pinctrl_range *ranges;
61 int nranges;
62
63 pcell_t acells, scells;
64 int done_pinmux;
65 };
66
67 struct pinctrl_devinfo {
68 struct ofw_bus_devinfo obdinfo;
69 struct resource_list rl;
70 };
71
72 static int
at91_pinctrl_probe(device_t dev)73 at91_pinctrl_probe(device_t dev)
74 {
75
76 if (!ofw_bus_is_compatible(dev, "atmel,at91rm9200-pinctrl"))
77 return (ENXIO);
78 device_set_desc(dev, "pincontrol bus");
79 return (0);
80 }
81
82 /* XXX Make this a subclass of simplebus */
83
84 static struct pinctrl_devinfo *
at91_pinctrl_setup_dinfo(device_t dev,phandle_t node)85 at91_pinctrl_setup_dinfo(device_t dev, phandle_t node)
86 {
87 struct pinctrl_softc *sc;
88 struct pinctrl_devinfo *ndi;
89 uint32_t *reg, *intr, icells;
90 uint64_t phys, size;
91 phandle_t iparent;
92 int i, j, k;
93 int nintr;
94 int nreg;
95
96 sc = device_get_softc(dev);
97
98 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
99 if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
100 free(ndi, M_DEVBUF);
101 return (NULL);
102 }
103
104 resource_list_init(&ndi->rl);
105 nreg = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)®);
106 if (nreg == -1)
107 nreg = 0;
108 if (nreg % (sc->acells + sc->scells) != 0) {
109 // if (bootverbose)
110 device_printf(dev, "Malformed reg property on <%s>\n",
111 ndi->obdinfo.obd_name);
112 nreg = 0;
113 }
114
115 for (i = 0, k = 0; i < nreg; i += sc->acells + sc->scells, k++) {
116 phys = size = 0;
117 for (j = 0; j < sc->acells; j++) {
118 phys <<= 32;
119 phys |= reg[i + j];
120 }
121 for (j = 0; j < sc->scells; j++) {
122 size <<= 32;
123 size |= reg[i + sc->acells + j];
124 }
125
126 resource_list_add(&ndi->rl, SYS_RES_MEMORY, k,
127 phys, phys + size - 1, size);
128 }
129 OF_prop_free(reg);
130
131 nintr = OF_getencprop_alloc(node, "interrupts", sizeof(*intr),
132 (void **)&intr);
133 if (nintr > 0) {
134 if (OF_searchencprop(node, "interrupt-parent", &iparent,
135 sizeof(iparent)) == -1) {
136 device_printf(dev, "No interrupt-parent found, "
137 "assuming direct parent\n");
138 iparent = OF_parent(node);
139 }
140 if (OF_searchencprop(OF_node_from_xref(iparent),
141 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
142 device_printf(dev, "Missing #interrupt-cells property,"
143 " assuming <1>\n");
144 icells = 1;
145 }
146 if (icells < 1 || icells > nintr) {
147 device_printf(dev, "Invalid #interrupt-cells property "
148 "value <%d>, assuming <1>\n", icells);
149 icells = 1;
150 }
151 for (i = 0, k = 0; i < nintr; i += icells, k++) {
152 intr[i] = ofw_bus_map_intr(dev, iparent, icells,
153 &intr[i]);
154 resource_list_add(&ndi->rl, SYS_RES_IRQ, k, intr[i],
155 intr[i], 1);
156 }
157 OF_prop_free(intr);
158 }
159
160 return (ndi);
161 }
162
163 static int
at91_pinctrl_fill_ranges(phandle_t node,struct pinctrl_softc * sc)164 at91_pinctrl_fill_ranges(phandle_t node, struct pinctrl_softc *sc)
165 {
166 int host_address_cells;
167 cell_t *base_ranges;
168 ssize_t nbase_ranges;
169 int err;
170 int i, j, k;
171
172 err = OF_searchencprop(OF_parent(node), "#address-cells",
173 &host_address_cells, sizeof(host_address_cells));
174 if (err <= 0)
175 return (-1);
176
177 nbase_ranges = OF_getproplen(node, "ranges");
178 if (nbase_ranges < 0)
179 return (-1);
180 sc->nranges = nbase_ranges / sizeof(cell_t) /
181 (sc->acells + host_address_cells + sc->scells);
182 if (sc->nranges == 0)
183 return (0);
184
185 sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
186 M_DEVBUF, M_WAITOK);
187 base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
188 OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
189
190 for (i = 0, j = 0; i < sc->nranges; i++) {
191 sc->ranges[i].bus = 0;
192 for (k = 0; k < sc->acells; k++) {
193 sc->ranges[i].bus <<= 32;
194 sc->ranges[i].bus |= base_ranges[j++];
195 }
196 sc->ranges[i].host = 0;
197 for (k = 0; k < host_address_cells; k++) {
198 sc->ranges[i].host <<= 32;
199 sc->ranges[i].host |= base_ranges[j++];
200 }
201 sc->ranges[i].size = 0;
202 for (k = 0; k < sc->scells; k++) {
203 sc->ranges[i].size <<= 32;
204 sc->ranges[i].size |= base_ranges[j++];
205 }
206 }
207
208 free(base_ranges, M_DEVBUF);
209 return (sc->nranges);
210 }
211
212 static int
at91_pinctrl_attach(device_t dev)213 at91_pinctrl_attach(device_t dev)
214 {
215 struct pinctrl_softc *sc;
216 struct pinctrl_devinfo *di;
217 phandle_t node;
218 device_t cdev;
219
220 sc = device_get_softc(dev);
221 node = ofw_bus_get_node(dev);
222
223 sc->dev = dev;
224 sc->node = node;
225
226 /*
227 * Some important numbers
228 */
229 sc->acells = 2;
230 OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
231 sc->scells = 1;
232 OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
233
234 if (at91_pinctrl_fill_ranges(node, sc) < 0) {
235 device_printf(dev, "could not get ranges\n");
236 return (ENXIO);
237 }
238
239 for (node = OF_child(node); node > 0; node = OF_peer(node)) {
240 if ((di = at91_pinctrl_setup_dinfo(dev, node)) == NULL)
241 continue;
242 cdev = device_add_child(dev, NULL, -1);
243 if (cdev == NULL) {
244 device_printf(dev, "<%s>: device_add_child failed\n",
245 di->obdinfo.obd_name);
246 resource_list_free(&di->rl);
247 ofw_bus_gen_destroy_devinfo(&di->obdinfo);
248 free(di, M_DEVBUF);
249 continue;
250 }
251 device_set_ivars(cdev, di);
252 }
253
254 fdt_pinctrl_register(dev, "atmel,pins");
255
256 return (bus_generic_attach(dev));
257 }
258
259 static const struct ofw_bus_devinfo *
pinctrl_get_devinfo(device_t bus __unused,device_t child)260 pinctrl_get_devinfo(device_t bus __unused, device_t child)
261 {
262 struct pinctrl_devinfo *ndi;
263
264 ndi = device_get_ivars(child);
265 return (&ndi->obdinfo);
266 }
267
268 static struct resource *
pinctrl_alloc_resource(device_t bus,device_t child,int type,int * rid,u_long start,u_long end,u_long count,u_int flags)269 pinctrl_alloc_resource(device_t bus, device_t child, int type, int *rid,
270 u_long start, u_long end, u_long count, u_int flags)
271 {
272 struct pinctrl_softc *sc;
273 struct pinctrl_devinfo *di;
274 struct resource_list_entry *rle;
275 int j;
276
277 sc = device_get_softc(bus);
278
279 /*
280 * Request for the default allocation with a given rid: use resource
281 * list stored in the local device info.
282 */
283 if (RMAN_IS_DEFAULT_RANGE(start, end)) {
284 if ((di = device_get_ivars(child)) == NULL)
285 return (NULL);
286
287 if (type == SYS_RES_IOPORT)
288 type = SYS_RES_MEMORY;
289
290 rle = resource_list_find(&di->rl, type, *rid);
291 if (rle == NULL) {
292 // if (bootverbose)
293 device_printf(bus, "no default resources for "
294 "rid = %d, type = %d\n", *rid, type);
295 return (NULL);
296 }
297 start = rle->start;
298 end = rle->end;
299 count = rle->count;
300 }
301
302 if (type == SYS_RES_MEMORY) {
303 /* Remap through ranges property */
304 for (j = 0; j < sc->nranges; j++) {
305 if (start >= sc->ranges[j].bus && end <
306 sc->ranges[j].bus + sc->ranges[j].size) {
307 start -= sc->ranges[j].bus;
308 start += sc->ranges[j].host;
309 end -= sc->ranges[j].bus;
310 end += sc->ranges[j].host;
311 break;
312 }
313 }
314 if (j == sc->nranges && sc->nranges != 0) {
315 // if (bootverbose)
316 device_printf(bus, "Could not map resource "
317 "%#lx-%#lx\n", start, end);
318
319 return (NULL);
320 }
321 }
322
323 return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
324 count, flags));
325 }
326
327 static int
pinctrl_print_res(struct pinctrl_devinfo * di)328 pinctrl_print_res(struct pinctrl_devinfo *di)
329 {
330 int rv;
331
332 rv = 0;
333 rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#jx");
334 rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%jd");
335 return (rv);
336 }
337
338 static void
pinctrl_probe_nomatch(device_t bus,device_t child)339 pinctrl_probe_nomatch(device_t bus, device_t child)
340 {
341 const char *name, *type, *compat;
342
343 // if (!bootverbose)
344 return;
345
346 name = ofw_bus_get_name(child);
347 type = ofw_bus_get_type(child);
348 compat = ofw_bus_get_compat(child);
349
350 device_printf(bus, "<%s>", name != NULL ? name : "unknown");
351 pinctrl_print_res(device_get_ivars(child));
352 if (!ofw_bus_status_okay(child))
353 printf(" disabled");
354 if (type)
355 printf(" type %s", type);
356 if (compat)
357 printf(" compat %s", compat);
358 printf(" (no driver attached)\n");
359 }
360
361 static int
pinctrl_print_child(device_t bus,device_t child)362 pinctrl_print_child(device_t bus, device_t child)
363 {
364 int rv;
365
366 rv = bus_print_child_header(bus, child);
367 rv += pinctrl_print_res(device_get_ivars(child));
368 if (!ofw_bus_status_okay(child))
369 rv += printf(" disabled");
370 rv += bus_print_child_footer(bus, child);
371 return (rv);
372 }
373
374 const char *periphs[] = {"gpio", "periph A", "periph B", "periph C", "periph D", "periph E" };
375
376 struct pincfg {
377 uint32_t unit;
378 uint32_t pin;
379 uint32_t periph;
380 uint32_t flags;
381 };
382
383 static int
pinctrl_configure_pins(device_t bus,phandle_t cfgxref)384 pinctrl_configure_pins(device_t bus, phandle_t cfgxref)
385 {
386 struct pinctrl_softc *sc;
387 struct pincfg *cfg, *cfgdata;
388 char name[32];
389 phandle_t node;
390 ssize_t npins;
391 int i;
392
393 sc = device_get_softc(bus);
394 node = OF_node_from_xref(cfgxref);
395 memset(name, 0, sizeof(name));
396 OF_getprop(node, "name", name, sizeof(name));
397 npins = OF_getencprop_alloc(node, "atmel,pins", sizeof(*cfgdata),
398 (void **)&cfgdata);
399 if (npins < 0) {
400 printf("We're doing it wrong %s\n", name);
401 return (ENXIO);
402 }
403 if (npins == 0)
404 return (0);
405 for (i = 0, cfg = cfgdata; i < npins; i++, cfg++) {
406 uint32_t pio;
407 pio = (0xfffffff & sc->ranges[0].bus) + 0x200 * cfg->unit;
408 printf("P%c%d %s %#x\n", cfg->unit + 'A', cfg->pin,
409 periphs[cfg->periph], cfg->flags);
410 switch (cfg->periph) {
411 case 0:
412 at91_pio_use_gpio(pio, 1u << cfg->pin);
413 at91_pio_gpio_pullup(pio, 1u << cfg->pin,
414 !!(cfg->flags & 1));
415 at91_pio_gpio_high_z(pio, 1u << cfg->pin,
416 !!(cfg->flags & 2));
417 at91_pio_gpio_set_deglitch(pio,
418 1u << cfg->pin, !!(cfg->flags & 4));
419 // at91_pio_gpio_pulldown(pio, 1u << cfg->pin,
420 // !!(cfg->flags & 8));
421 // at91_pio_gpio_dis_schmidt(pio,
422 // 1u << cfg->pin, !!(cfg->flags & 16));
423 break;
424 case 1:
425 at91_pio_use_periph_a(pio, 1u << cfg->pin, cfg->flags);
426 break;
427 case 2:
428 at91_pio_use_periph_b(pio, 1u << cfg->pin, cfg->flags);
429 break;
430 }
431 }
432 OF_prop_free(cfgdata);
433 return (0);
434 }
435
436 static void
pinctrl_new_pass(device_t bus)437 pinctrl_new_pass(device_t bus)
438 {
439 struct pinctrl_softc *sc;
440
441 sc = device_get_softc(bus);
442
443 bus_generic_new_pass(bus);
444
445 if (sc->done_pinmux || bus_current_pass < BUS_PASS_PINMUX)
446 return;
447 sc->done_pinmux++;
448
449 fdt_pinctrl_configure_tree(bus);
450 }
451
452 static device_method_t at91_pinctrl_methods[] = {
453 DEVMETHOD(device_probe, at91_pinctrl_probe),
454 DEVMETHOD(device_attach, at91_pinctrl_attach),
455
456 DEVMETHOD(bus_print_child, pinctrl_print_child),
457 DEVMETHOD(bus_probe_nomatch, pinctrl_probe_nomatch),
458 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
459 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
460 DEVMETHOD(bus_alloc_resource, pinctrl_alloc_resource),
461 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
462 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
463 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
464 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
465 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
466 DEVMETHOD(bus_new_pass, pinctrl_new_pass),
467
468 /* ofw_bus interface */
469 DEVMETHOD(ofw_bus_get_devinfo, pinctrl_get_devinfo),
470 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
471 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
472 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
473 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
474 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
475
476 /* fdt_pintrl interface */
477 DEVMETHOD(fdt_pinctrl_configure,pinctrl_configure_pins),
478 DEVMETHOD_END
479 };
480
481 static driver_t at91_pinctrl_driver = {
482 "at91_pinctrl",
483 at91_pinctrl_methods,
484 sizeof(struct pinctrl_softc),
485 };
486
487 static devclass_t at91_pinctrl_devclass;
488
489 EARLY_DRIVER_MODULE(at91_pinctrl, simplebus, at91_pinctrl_driver,
490 at91_pinctrl_devclass, NULL, NULL, BUS_PASS_BUS);
491
492 /*
493 * dummy driver to force pass BUS_PASS_PINMUX to happen.
494 */
495 static int
at91_pingroup_probe(device_t dev)496 at91_pingroup_probe(device_t dev)
497 {
498 return ENXIO;
499 }
500
501 static device_method_t at91_pingroup_methods[] = {
502 DEVMETHOD(device_probe, at91_pingroup_probe),
503
504 DEVMETHOD_END
505 };
506
507
508 static driver_t at91_pingroup_driver = {
509 "at91_pingroup",
510 at91_pingroup_methods,
511 0,
512 };
513
514 static devclass_t at91_pingroup_devclass;
515
516 EARLY_DRIVER_MODULE(at91_pingroup, at91_pinctrl, at91_pingroup_driver,
517 at91_pingroup_devclass, NULL, NULL, BUS_PASS_PINMUX);
518