2 * Copyright (c) 2014 Warner Losh. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/resource.h>
34 #include <sys/systm.h>
37 #include <machine/bus.h>
39 #include <arm/at91/at91var.h>
40 #include <arm/at91/at91_piovar.h>
42 #include <dev/fdt/fdt_pinctrl.h>
43 #include <dev/ofw/openfirm.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
47 #define BUS_PASS_PINMUX (BUS_PASS_INTERRUPT + 1)
49 struct pinctrl_range {
55 struct pinctrl_softc {
59 struct pinctrl_range *ranges;
62 pcell_t acells, scells;
66 struct pinctrl_devinfo {
67 struct ofw_bus_devinfo obdinfo;
68 struct resource_list rl;
72 at91_pinctrl_probe(device_t dev)
75 if (!ofw_bus_is_compatible(dev, "atmel,at91rm9200-pinctrl"))
77 device_set_desc(dev, "pincontrol bus");
81 /* XXX Make this a subclass of simplebus */
83 static struct pinctrl_devinfo *
84 at91_pinctrl_setup_dinfo(device_t dev, phandle_t node)
86 struct pinctrl_softc *sc;
87 struct pinctrl_devinfo *ndi;
88 uint32_t *reg, *intr, icells;
95 sc = device_get_softc(dev);
97 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
98 if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
103 resource_list_init(&ndi->rl);
104 nreg = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)®);
107 if (nreg % (sc->acells + sc->scells) != 0) {
109 device_printf(dev, "Malformed reg property on <%s>\n",
110 ndi->obdinfo.obd_name);
114 for (i = 0, k = 0; i < nreg; i += sc->acells + sc->scells, k++) {
116 for (j = 0; j < sc->acells; j++) {
120 for (j = 0; j < sc->scells; j++) {
122 size |= reg[i + sc->acells + j];
125 resource_list_add(&ndi->rl, SYS_RES_MEMORY, k,
126 phys, phys + size - 1, size);
130 nintr = OF_getencprop_alloc(node, "interrupts", sizeof(*intr),
133 if (OF_searchencprop(node, "interrupt-parent", &iparent,
134 sizeof(iparent)) == -1) {
135 device_printf(dev, "No interrupt-parent found, "
136 "assuming direct parent\n");
137 iparent = OF_parent(node);
139 if (OF_searchencprop(OF_node_from_xref(iparent),
140 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
141 device_printf(dev, "Missing #interrupt-cells property,"
145 if (icells < 1 || icells > nintr) {
146 device_printf(dev, "Invalid #interrupt-cells property "
147 "value <%d>, assuming <1>\n", icells);
150 for (i = 0, k = 0; i < nintr; i += icells, k++) {
151 intr[i] = ofw_bus_map_intr(dev, iparent, icells,
153 resource_list_add(&ndi->rl, SYS_RES_IRQ, k, intr[i],
163 at91_pinctrl_fill_ranges(phandle_t node, struct pinctrl_softc *sc)
165 int host_address_cells;
167 ssize_t nbase_ranges;
171 err = OF_searchencprop(OF_parent(node), "#address-cells",
172 &host_address_cells, sizeof(host_address_cells));
176 nbase_ranges = OF_getproplen(node, "ranges");
177 if (nbase_ranges < 0)
179 sc->nranges = nbase_ranges / sizeof(cell_t) /
180 (sc->acells + host_address_cells + sc->scells);
181 if (sc->nranges == 0)
184 sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
186 base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
187 OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
189 for (i = 0, j = 0; i < sc->nranges; i++) {
190 sc->ranges[i].bus = 0;
191 for (k = 0; k < sc->acells; k++) {
192 sc->ranges[i].bus <<= 32;
193 sc->ranges[i].bus |= base_ranges[j++];
195 sc->ranges[i].host = 0;
196 for (k = 0; k < host_address_cells; k++) {
197 sc->ranges[i].host <<= 32;
198 sc->ranges[i].host |= base_ranges[j++];
200 sc->ranges[i].size = 0;
201 for (k = 0; k < sc->scells; k++) {
202 sc->ranges[i].size <<= 32;
203 sc->ranges[i].size |= base_ranges[j++];
207 free(base_ranges, M_DEVBUF);
208 return (sc->nranges);
212 at91_pinctrl_attach(device_t dev)
214 struct pinctrl_softc *sc;
215 struct pinctrl_devinfo *di;
219 sc = device_get_softc(dev);
220 node = ofw_bus_get_node(dev);
226 * Some important numbers
229 OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
231 OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
233 if (at91_pinctrl_fill_ranges(node, sc) < 0) {
234 device_printf(dev, "could not get ranges\n");
238 for (node = OF_child(node); node > 0; node = OF_peer(node)) {
239 if ((di = at91_pinctrl_setup_dinfo(dev, node)) == NULL)
241 cdev = device_add_child(dev, NULL, -1);
243 device_printf(dev, "<%s>: device_add_child failed\n",
244 di->obdinfo.obd_name);
245 resource_list_free(&di->rl);
246 ofw_bus_gen_destroy_devinfo(&di->obdinfo);
250 device_set_ivars(cdev, di);
253 fdt_pinctrl_register(dev, "atmel,pins");
255 return (bus_generic_attach(dev));
258 static const struct ofw_bus_devinfo *
259 pinctrl_get_devinfo(device_t bus __unused, device_t child)
261 struct pinctrl_devinfo *ndi;
263 ndi = device_get_ivars(child);
264 return (&ndi->obdinfo);
267 static struct resource *
268 pinctrl_alloc_resource(device_t bus, device_t child, int type, int *rid,
269 u_long start, u_long end, u_long count, u_int flags)
271 struct pinctrl_softc *sc;
272 struct pinctrl_devinfo *di;
273 struct resource_list_entry *rle;
276 sc = device_get_softc(bus);
279 * Request for the default allocation with a given rid: use resource
280 * list stored in the local device info.
282 if (RMAN_IS_DEFAULT_RANGE(start, end)) {
283 if ((di = device_get_ivars(child)) == NULL)
286 if (type == SYS_RES_IOPORT)
287 type = SYS_RES_MEMORY;
289 rle = resource_list_find(&di->rl, type, *rid);
292 device_printf(bus, "no default resources for "
293 "rid = %d, type = %d\n", *rid, type);
301 if (type == SYS_RES_MEMORY) {
302 /* Remap through ranges property */
303 for (j = 0; j < sc->nranges; j++) {
304 if (start >= sc->ranges[j].bus && end <
305 sc->ranges[j].bus + sc->ranges[j].size) {
306 start -= sc->ranges[j].bus;
307 start += sc->ranges[j].host;
308 end -= sc->ranges[j].bus;
309 end += sc->ranges[j].host;
313 if (j == sc->nranges && sc->nranges != 0) {
315 device_printf(bus, "Could not map resource "
316 "%#lx-%#lx\n", start, end);
322 return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
327 pinctrl_print_res(struct pinctrl_devinfo *di)
332 rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#jx");
333 rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%jd");
338 pinctrl_probe_nomatch(device_t bus, device_t child)
340 const char *name, *type, *compat;
345 name = ofw_bus_get_name(child);
346 type = ofw_bus_get_type(child);
347 compat = ofw_bus_get_compat(child);
349 device_printf(bus, "<%s>", name != NULL ? name : "unknown");
350 pinctrl_print_res(device_get_ivars(child));
351 if (!ofw_bus_status_okay(child))
354 printf(" type %s", type);
356 printf(" compat %s", compat);
357 printf(" (no driver attached)\n");
361 pinctrl_print_child(device_t bus, device_t child)
365 rv = bus_print_child_header(bus, child);
366 rv += pinctrl_print_res(device_get_ivars(child));
367 if (!ofw_bus_status_okay(child))
368 rv += printf(" disabled");
369 rv += bus_print_child_footer(bus, child);
373 const char *periphs[] = {"gpio", "periph A", "periph B", "periph C", "periph D", "periph E" };
383 pinctrl_configure_pins(device_t bus, phandle_t cfgxref)
385 struct pinctrl_softc *sc;
386 struct pincfg *cfg, *cfgdata;
392 sc = device_get_softc(bus);
393 node = OF_node_from_xref(cfgxref);
394 memset(name, 0, sizeof(name));
395 OF_getprop(node, "name", name, sizeof(name));
396 npins = OF_getencprop_alloc(node, "atmel,pins", sizeof(*cfgdata),
399 printf("We're doing it wrong %s\n", name);
404 for (i = 0, cfg = cfgdata; i < npins; i++, cfg++) {
406 pio = (0xfffffff & sc->ranges[0].bus) + 0x200 * cfg->unit;
407 printf("P%c%d %s %#x\n", cfg->unit + 'A', cfg->pin,
408 periphs[cfg->periph], cfg->flags);
409 switch (cfg->periph) {
411 at91_pio_use_gpio(pio, 1u << cfg->pin);
412 at91_pio_gpio_pullup(pio, 1u << cfg->pin,
414 at91_pio_gpio_high_z(pio, 1u << cfg->pin,
416 at91_pio_gpio_set_deglitch(pio,
417 1u << cfg->pin, !!(cfg->flags & 4));
418 // at91_pio_gpio_pulldown(pio, 1u << cfg->pin,
419 // !!(cfg->flags & 8));
420 // at91_pio_gpio_dis_schmidt(pio,
421 // 1u << cfg->pin, !!(cfg->flags & 16));
424 at91_pio_use_periph_a(pio, 1u << cfg->pin, cfg->flags);
427 at91_pio_use_periph_b(pio, 1u << cfg->pin, cfg->flags);
431 OF_prop_free(cfgdata);
436 pinctrl_new_pass(device_t bus)
438 struct pinctrl_softc *sc;
440 sc = device_get_softc(bus);
442 bus_generic_new_pass(bus);
444 if (sc->done_pinmux || bus_current_pass < BUS_PASS_PINMUX)
448 fdt_pinctrl_configure_tree(bus);
451 static device_method_t at91_pinctrl_methods[] = {
452 DEVMETHOD(device_probe, at91_pinctrl_probe),
453 DEVMETHOD(device_attach, at91_pinctrl_attach),
455 DEVMETHOD(bus_print_child, pinctrl_print_child),
456 DEVMETHOD(bus_probe_nomatch, pinctrl_probe_nomatch),
457 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
458 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
459 DEVMETHOD(bus_alloc_resource, pinctrl_alloc_resource),
460 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
461 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
462 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
463 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
464 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
465 DEVMETHOD(bus_new_pass, pinctrl_new_pass),
467 /* ofw_bus interface */
468 DEVMETHOD(ofw_bus_get_devinfo, pinctrl_get_devinfo),
469 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
470 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
471 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
472 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
473 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
475 /* fdt_pintrl interface */
476 DEVMETHOD(fdt_pinctrl_configure,pinctrl_configure_pins),
480 static driver_t at91_pinctrl_driver = {
482 at91_pinctrl_methods,
483 sizeof(struct pinctrl_softc),
486 static devclass_t at91_pinctrl_devclass;
488 EARLY_DRIVER_MODULE(at91_pinctrl, simplebus, at91_pinctrl_driver,
489 at91_pinctrl_devclass, NULL, NULL, BUS_PASS_BUS);
492 * dummy driver to force pass BUS_PASS_PINMUX to happen.
495 at91_pingroup_probe(device_t dev)
500 static device_method_t at91_pingroup_methods[] = {
501 DEVMETHOD(device_probe, at91_pingroup_probe),
507 static driver_t at91_pingroup_driver = {
509 at91_pingroup_methods,
513 static devclass_t at91_pingroup_devclass;
515 EARLY_DRIVER_MODULE(at91_pingroup, at91_pinctrl, at91_pingroup_driver,
516 at91_pingroup_devclass, NULL, NULL, BUS_PASS_PINMUX);