]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/arm/at91/at91_pinctrl.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / arm / at91 / at91_pinctrl.c
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/ofw/openfirm.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46
47 #define BUS_PASS_PINMUX (BUS_PASS_INTERRUPT + 1)
48
49 struct pinctrl_range {
50         uint64_t bus;
51         uint64_t host;
52         uint64_t size;
53 };
54
55 struct pinctrl_softc {
56         device_t dev;
57         phandle_t node;
58
59         struct pinctrl_range *ranges;
60         int nranges;
61
62         pcell_t acells, scells;
63         int done_pinmux;
64 };
65
66 struct pinctrl_devinfo {
67         struct ofw_bus_devinfo  obdinfo;
68         struct resource_list    rl;
69 };
70
71 static int
72 at91_pinctrl_probe(device_t dev)
73 {
74
75         if (!ofw_bus_is_compatible(dev, "atmel,at91rm9200-pinctrl"))
76                 return (ENXIO);
77         device_set_desc(dev, "pincontrol bus");
78         return (0);
79 }
80
81 /* XXX Make this a subclass of simplebus */
82
83 static struct pinctrl_devinfo *
84 at91_pinctrl_setup_dinfo(device_t dev, phandle_t node)
85 {
86         struct pinctrl_softc *sc;
87         struct pinctrl_devinfo *ndi;
88         uint32_t *reg, *intr, icells;
89         uint64_t phys, size;
90         phandle_t iparent;
91         int i, j, k;
92         int nintr;
93         int nreg;
94
95         sc = device_get_softc(dev);
96
97         ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
98         if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
99                 free(ndi, M_DEVBUF);
100                 return (NULL);
101         }
102
103         resource_list_init(&ndi->rl);
104         nreg = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
105         if (nreg == -1)
106                 nreg = 0;
107         if (nreg % (sc->acells + sc->scells) != 0) {
108 //              if (bootverbose)
109                         device_printf(dev, "Malformed reg property on <%s>\n",
110                             ndi->obdinfo.obd_name);
111                 nreg = 0;
112         }
113
114         for (i = 0, k = 0; i < nreg; i += sc->acells + sc->scells, k++) {
115                 phys = size = 0;
116                 for (j = 0; j < sc->acells; j++) {
117                         phys <<= 32;
118                         phys |= reg[i + j];
119                 }
120                 for (j = 0; j < sc->scells; j++) {
121                         size <<= 32;
122                         size |= reg[i + sc->acells + j];
123                 }
124                 
125                 resource_list_add(&ndi->rl, SYS_RES_MEMORY, k,
126                     phys, phys + size - 1, size);
127         }
128         free(reg, M_OFWPROP);
129
130         nintr = OF_getencprop_alloc(node, "interrupts",  sizeof(*intr),
131             (void **)&intr);
132         if (nintr > 0) {
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);
138                 }
139                 if (OF_searchencprop(OF_node_from_xref(iparent), 
140                     "#interrupt-cells", &icells, sizeof(icells)) == -1) {
141                         device_printf(dev, "Missing #interrupt-cells property, "
142                             "assuming <1>\n");
143                         icells = 1;
144                 }
145                 if (icells < 1 || icells > nintr) {
146                         device_printf(dev, "Invalid #interrupt-cells property "
147                             "value <%d>, assuming <1>\n", icells);
148                         icells = 1;
149                 }
150                 for (i = 0, k = 0; i < nintr; i += icells, k++) {
151                         intr[i] = ofw_bus_map_intr(dev, iparent, icells,
152                             &intr[i]);
153                         resource_list_add(&ndi->rl, SYS_RES_IRQ, k, intr[i],
154                             intr[i], 1);
155                 }
156                 free(intr, M_OFWPROP);
157         }
158
159         return (ndi);
160 }
161
162 static int
163 at91_pinctrl_fill_ranges(phandle_t node, struct pinctrl_softc *sc)
164 {
165         int host_address_cells;
166         cell_t *base_ranges;
167         ssize_t nbase_ranges;
168         int err;
169         int i, j, k;
170
171         err = OF_searchencprop(OF_parent(node), "#address-cells",
172             &host_address_cells, sizeof(host_address_cells));
173         if (err <= 0)
174                 return (-1);
175
176         nbase_ranges = OF_getproplen(node, "ranges");
177         if (nbase_ranges < 0)
178                 return (-1);
179         sc->nranges = nbase_ranges / sizeof(cell_t) /
180             (sc->acells + host_address_cells + sc->scells);
181         if (sc->nranges == 0)
182                 return (0);
183
184         sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
185             M_DEVBUF, M_WAITOK);
186         base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
187         OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
188
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++];
194                 }
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++];
199                 }
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++];
204                 }
205         }
206
207         free(base_ranges, M_DEVBUF);
208         return (sc->nranges);
209 }
210
211 static int
212 at91_pinctrl_attach(device_t dev)
213 {
214         struct pinctrl_softc *sc;
215         struct pinctrl_devinfo *di;
216         phandle_t       node;
217         device_t        cdev;
218
219         sc = device_get_softc(dev);
220         node = ofw_bus_get_node(dev);
221
222         sc->dev = dev;
223         sc->node = node;
224         
225         /*
226          * Some important numbers
227          */
228         sc->acells = 2;
229         OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
230         sc->scells = 1;
231         OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
232
233         if (at91_pinctrl_fill_ranges(node, sc) < 0) {
234                 device_printf(dev, "could not get ranges\n");
235                 return (ENXIO);
236         }
237
238         for (node = OF_child(node); node > 0; node = OF_peer(node)) {
239                 if ((di = at91_pinctrl_setup_dinfo(dev, node)) == NULL)
240                         continue;
241                 cdev = device_add_child(dev, NULL, -1);
242                 if (cdev == NULL) {
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);
247                         free(di, M_DEVBUF);
248                         continue;
249                 }
250                 device_set_ivars(cdev, di);
251         }
252
253         return (bus_generic_attach(dev));
254 }
255
256 static const struct ofw_bus_devinfo *
257 pinctrl_get_devinfo(device_t bus __unused, device_t child)
258 {
259         struct pinctrl_devinfo *ndi;
260         
261         ndi = device_get_ivars(child);
262         return (&ndi->obdinfo);
263 }
264
265 static struct resource *
266 pinctrl_alloc_resource(device_t bus, device_t child, int type, int *rid,
267     u_long start, u_long end, u_long count, u_int flags)
268 {
269         struct pinctrl_softc *sc;
270         struct pinctrl_devinfo *di;
271         struct resource_list_entry *rle;
272         int j;
273
274         sc = device_get_softc(bus);
275
276         /*
277          * Request for the default allocation with a given rid: use resource
278          * list stored in the local device info.
279          */
280         if ((start == 0UL) && (end == ~0UL)) {
281                 if ((di = device_get_ivars(child)) == NULL)
282                         return (NULL);
283
284                 if (type == SYS_RES_IOPORT)
285                         type = SYS_RES_MEMORY;
286
287                 rle = resource_list_find(&di->rl, type, *rid);
288                 if (rle == NULL) {
289 //                      if (bootverbose)
290                                 device_printf(bus, "no default resources for "
291                                     "rid = %d, type = %d\n", *rid, type);
292                         return (NULL);
293                 }
294                 start = rle->start;
295                 end = rle->end;
296                 count = rle->count;
297         }
298
299         if (type == SYS_RES_MEMORY) {
300                 /* Remap through ranges property */
301                 for (j = 0; j < sc->nranges; j++) {
302                         if (start >= sc->ranges[j].bus && end <
303                             sc->ranges[j].bus + sc->ranges[j].size) {
304                                 start -= sc->ranges[j].bus;
305                                 start += sc->ranges[j].host;
306                                 end -= sc->ranges[j].bus;
307                                 end += sc->ranges[j].host;
308                                 break;
309                         }
310                 }
311                 if (j == sc->nranges && sc->nranges != 0) {
312 //                      if (bootverbose)
313                                 device_printf(bus, "Could not map resource "
314                                     "%#lx-%#lx\n", start, end);
315
316                         return (NULL);
317                 }
318         }
319
320         return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
321             count, flags));
322 }
323
324 static int
325 pinctrl_print_res(struct pinctrl_devinfo *di)
326 {
327         int rv;
328
329         rv = 0;
330         rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#lx");
331         rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%ld");
332         return (rv);
333 }
334
335 static void
336 pinctrl_probe_nomatch(device_t bus, device_t child)
337 {
338         const char *name, *type, *compat;
339
340 //      if (!bootverbose)
341                 return;
342
343         name = ofw_bus_get_name(child);
344         type = ofw_bus_get_type(child);
345         compat = ofw_bus_get_compat(child);
346
347         device_printf(bus, "<%s>", name != NULL ? name : "unknown");
348         pinctrl_print_res(device_get_ivars(child));
349         if (!ofw_bus_status_okay(child))
350                 printf(" disabled");
351         if (type)
352                 printf(" type %s", type);
353         if (compat)
354                 printf(" compat %s", compat);
355         printf(" (no driver attached)\n");
356 }
357
358 static int
359 pinctrl_print_child(device_t bus, device_t child)
360 {
361         int rv;
362
363         rv = bus_print_child_header(bus, child);
364         rv += pinctrl_print_res(device_get_ivars(child));
365         if (!ofw_bus_status_okay(child))
366                 rv += printf(" disabled");
367         rv += bus_print_child_footer(bus, child);
368         return (rv);
369 }
370
371 const char *periphs[] = {"gpio", "periph A", "periph B", "periph C", "periph D", "periph E" };
372
373 static void
374 pinctrl_walk_tree(device_t bus, phandle_t node)
375 {
376         struct pinctrl_softc *sc;
377         char status[10];
378         char name[32];
379         phandle_t pinctrl[32], pins[32 * 4], scratch;
380         ssize_t len, npins;
381         int i, j;
382
383         sc = device_get_softc(bus);
384         for (node = OF_child(node); node > 0; node = OF_peer(node)) {
385                 pinctrl_walk_tree(bus, node);
386                 memset(status, 0, sizeof(status));
387                 memset(name, 0, sizeof(name));
388                 OF_getprop(node, "status", status, sizeof(status));
389                 OF_getprop(node, "name", name, sizeof(name));
390                 if (strcmp(status, "okay") != 0) {
391 //                      printf("pinctrl: omitting node %s since it isn't active\n", name);
392                         continue;
393                 }
394                 len = OF_getencprop(node, "pinctrl-0", pinctrl, sizeof(pinctrl));
395                 if (len <= 0) {
396 //                      printf("pinctrl: no pinctrl-0 property for node %s, omitting\n", name);
397                         continue;
398                 }
399                 len /= sizeof(phandle_t);
400                 printf("pinctrl: Found active node %s\n", name);
401                 for (i = 0; i < len; i++) {
402                         scratch = OF_node_from_xref(pinctrl[i]);
403                         npins = OF_getencprop(scratch, "atmel,pins", pins, sizeof(pins));
404                         if (npins <= 0) {
405                                 printf("We're doing it wrong %s\n", name);
406                                 continue;
407                         }
408                         memset(name, 0, sizeof(name));
409                         OF_getprop(scratch, "name", name, sizeof(name));
410                         npins /= (4 * 4);
411                         printf("----> need to cope with %d more pins for %s\n", npins, name);
412                         for (j = 0; j < npins; j++) {
413                                 uint32_t unit = pins[j * 4];
414                                 uint32_t pin = pins[j * 4 + 1];
415                                 uint32_t periph = pins[j * 4 + 2];
416                                 uint32_t flags = pins[j * 4 + 3];
417                                 uint32_t pio = (0xfffffff & sc->ranges[0].bus) + 0x200 * unit;
418                                 printf("P%c%d %s %#x\n", unit + 'A', pin, periphs[periph],
419                                        flags);
420                                 switch (periph) {
421                                 case 0:
422                                         at91_pio_use_gpio(pio, 1u << pin);
423                                         at91_pio_gpio_pullup(pio, 1u << pin, !!(flags & 1));
424                                         at91_pio_gpio_high_z(pio, 1u << pin, !!(flags & 2));
425                                         at91_pio_gpio_set_deglitch(pio, 1u << pin, !!(flags & 4));
426                                         // at91_pio_gpio_pulldown(pio, 1u << pin, !!(flags & 8));
427                                         // at91_pio_gpio_dis_schmidt(pio, 1u << pin, !!(flags & 16));
428                                         break;
429                                 case 1:
430                                         at91_pio_use_periph_a(pio, 1u << pin, flags);
431                                         break;
432                                 case 2:
433                                         at91_pio_use_periph_b(pio, 1u << pin, flags);
434                                         break;
435                                 }
436                         }
437                 }
438         }
439 }
440
441 static void
442 pinctrl_new_pass(device_t bus)
443 {
444         struct pinctrl_softc *sc;
445         phandle_t node;
446
447         sc = device_get_softc(bus);
448
449         bus_generic_new_pass(bus);
450
451         if (sc->done_pinmux || bus_current_pass < BUS_PASS_PINMUX)
452                 return;
453         sc->done_pinmux++;
454
455         node = OF_peer(0);
456         if (node == -1)
457                 return;
458         pinctrl_walk_tree(bus, node);
459 }
460
461 static device_method_t at91_pinctrl_methods[] = {
462         DEVMETHOD(device_probe, at91_pinctrl_probe),
463         DEVMETHOD(device_attach, at91_pinctrl_attach),
464
465         DEVMETHOD(bus_print_child,      pinctrl_print_child),
466         DEVMETHOD(bus_probe_nomatch,    pinctrl_probe_nomatch),
467         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
468         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
469         DEVMETHOD(bus_alloc_resource,   pinctrl_alloc_resource),
470         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
471         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
472         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
473         DEVMETHOD(bus_adjust_resource,  bus_generic_adjust_resource),
474         DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
475         DEVMETHOD(bus_new_pass,         pinctrl_new_pass),
476
477         /* ofw_bus interface */
478         DEVMETHOD(ofw_bus_get_devinfo,  pinctrl_get_devinfo),
479         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
480         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
481         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
482         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
483         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
484
485         DEVMETHOD_END
486 };
487
488 static driver_t at91_pinctrl_driver = {
489         "at91_pinctrl",
490         at91_pinctrl_methods,
491         sizeof(struct pinctrl_softc),
492 };
493
494 static devclass_t at91_pinctrl_devclass;
495
496 EARLY_DRIVER_MODULE(at91_pinctrl, simplebus, at91_pinctrl_driver, at91_pinctrl_devclass,
497     NULL, NULL, BUS_PASS_BUS);
498
499 /*
500  * dummy driver to force pass BUS_PASS_PINMUX to happen.
501  */
502 static int
503 at91_pingroup_probe(device_t dev)
504 {
505         return ENXIO;
506 }
507
508 static device_method_t at91_pingroup_methods[] = {
509         DEVMETHOD(device_probe, at91_pingroup_probe),
510
511         DEVMETHOD_END
512 };
513         
514
515 static driver_t at91_pingroup_driver = {
516         "at91_pingroup",
517         at91_pingroup_methods,
518         0,
519 };
520
521 static devclass_t at91_pingroup_devclass;
522
523 EARLY_DRIVER_MODULE(at91_pingroup, at91_pinctrl, at91_pingroup_driver, at91_pingroup_devclass,
524     NULL, NULL, BUS_PASS_PINMUX);