2 * Copyright 2015 John Wehle <john@feith.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Amlogic aml8726 pinctrl driver.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/resource.h>
46 #include <machine/bus.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 #include <dev/fdt/fdt_pinctrl.h>
52 #include <arm/amlogic/aml8726/aml8726_soc.h>
53 #include <arm/amlogic/aml8726/aml8726_pinctrl.h>
55 struct aml8726_pinctrl_softc {
58 struct aml8726_pinctrl_function *func;
59 struct aml8726_pinctrl_pkg_pin *ppin;
62 struct resource *res[6];
66 static struct resource_spec aml8726_pinctrl_spec[] = {
67 { SYS_RES_MEMORY, 0, RF_ACTIVE }, /* mux */
68 { SYS_RES_MEMORY, 1, RF_ACTIVE | RF_SHAREABLE }, /* pu/pd */
69 { SYS_RES_MEMORY, 2, RF_ACTIVE | RF_SHAREABLE }, /* pull enable */
70 { SYS_RES_MEMORY, 3, RF_ACTIVE }, /* ao mux */
71 { SYS_RES_MEMORY, 4, RF_ACTIVE | RF_SHAREABLE }, /* ao pu/pd */
72 { SYS_RES_MEMORY, 5, RF_ACTIVE | RF_SHAREABLE }, /* ao pull enable */
76 #define AML_PINCTRL_LOCK(sc) mtx_lock(&(sc)->mtx)
77 #define AML_PINCTRL_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
78 #define AML_PINCTRL_LOCK_INIT(sc) \
79 mtx_init(&(sc)->mtx, device_get_nameunit((sc)->dev), \
81 #define AML_PINCTRL_LOCK_DESTROY(sc) mtx_destroy(&(sc)->mtx);
83 #define MUX_WRITE_4(sc, reg, val) bus_write_4((sc)->res[0], reg, (val))
84 #define MUX_READ_4(sc, reg) bus_read_4((sc)->res[0], reg)
86 #define PUD_WRITE_4(sc, reg, val) bus_write_4((sc)->res[1], reg, (val))
87 #define PUD_READ_4(sc, reg) bus_read_4((sc)->res[1], reg)
89 #define PEN_WRITE_4(sc, reg, val) bus_write_4((sc)->res[2], reg, (val))
90 #define PEN_READ_4(sc, reg) bus_read_4((sc)->res[2], reg)
92 #define AOMUX_WRITE_4(sc, reg, val) bus_write_4((sc)->res[3], reg, (val))
93 #define AOMUX_READ_4(sc, reg) bus_read_4((sc)->res[3], reg)
95 #define AOPUD_WRITE_4(sc, reg, val) bus_write_4((sc)->res[4], reg, (val))
96 #define AOPUD_READ_4(sc, reg) bus_read_4((sc)->res[4], reg)
98 #define AOPEN_WRITE_4(sc, reg, val) bus_write_4((sc)->res[5], reg, (val))
99 #define AOPEN_READ_4(sc, reg) bus_read_4((sc)->res[5], reg)
102 aml8726_pinctrl_probe(device_t dev)
105 if (!ofw_bus_status_okay(dev))
108 if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-pinctrl"))
111 device_set_desc(dev, "Amlogic aml8726 pinctrl");
113 return (BUS_PROBE_DEFAULT);
117 aml8726_pinctrl_attach(device_t dev)
119 struct aml8726_pinctrl_softc *sc = device_get_softc(dev);
123 sc->soc.pud_ctrl = false;
125 switch (aml8726_soc_hw_rev) {
126 case AML_SOC_HW_REV_M3:
127 sc->soc.func = aml8726_m3_pinctrl;
128 sc->soc.ppin = aml8726_m3_pkg_pin;
130 case AML_SOC_HW_REV_M6:
131 sc->soc.func = aml8726_m6_pinctrl;
132 sc->soc.ppin = aml8726_m6_pkg_pin;
134 case AML_SOC_HW_REV_M8:
135 sc->soc.func = aml8726_m8_pinctrl;
136 sc->soc.ppin = aml8726_m8_pkg_pin;
137 sc->soc.pud_ctrl = true;
139 case AML_SOC_HW_REV_M8B:
140 sc->soc.func = aml8726_m8b_pinctrl;
141 sc->soc.ppin = aml8726_m8b_pkg_pin;
142 sc->soc.pud_ctrl = true;
145 device_printf(dev, "unsupported SoC\n");
150 if (bus_alloc_resources(dev, aml8726_pinctrl_spec, sc->res)) {
151 device_printf(dev, "could not allocate resources for device\n");
155 AML_PINCTRL_LOCK_INIT(sc);
157 fdt_pinctrl_register(dev, "amlogic,pins");
158 fdt_pinctrl_configure_tree(dev);
164 aml8726_pinctrl_detach(device_t dev)
166 struct aml8726_pinctrl_softc *sc = device_get_softc(dev);
168 AML_PINCTRL_LOCK_DESTROY(sc);
170 bus_release_resources(dev, aml8726_pinctrl_spec, sc->res);
177 aml8726_pinctrl_configure_pins(device_t dev, phandle_t cfgxref)
179 struct aml8726_pinctrl_softc *sc = device_get_softc(dev);
180 struct aml8726_pinctrl_function *cf;
181 struct aml8726_pinctrl_function *f;
182 struct aml8726_pinctrl_pkg_pin *pp;
183 struct aml8726_pinctrl_pin *cp;
184 struct aml8726_pinctrl_pin *p;
185 enum aml8726_pinctrl_pull_mode pm;
194 node = OF_node_from_xref(cfgxref);
196 len = OF_getprop_alloc(node, "amlogic,function",
197 sizeof(char), (void **)&function_name);
201 "missing amlogic,function attribute in FDT\n");
205 for (f = sc->soc.func; f->name != NULL; f++)
206 if (strncmp(f->name, function_name, len) == 0)
209 if (f->name == NULL) {
210 device_printf(dev, "unknown function attribute %.*s in FDT\n",
212 OF_prop_free(function_name);
216 OF_prop_free(function_name);
218 len = OF_getprop_alloc(node, "amlogic,pull",
219 sizeof(char), (void **)&pull);
221 pm = aml8726_unknown_pm;
224 if (strncmp(pull, "enable", len) == 0)
225 pm = aml8726_enable_pm;
226 else if (strncmp(pull, "disable", len) == 0)
227 pm = aml8726_disable_pm;
228 else if (strncmp(pull, "down", len) == 0)
229 pm = aml8726_enable_down_pm;
230 else if (strncmp(pull, "up", len) == 0)
231 pm = aml8726_enable_up_pm;
234 "unknown pull attribute %.*s in FDT\n",
244 * Setting the pull direction isn't supported on all SoC.
247 case aml8726_enable_down_pm:
248 case aml8726_enable_up_pm:
249 if (sc->soc.pud_ctrl == false) {
251 "SoC doesn't support setting pull direction.\n");
259 len = OF_getprop_alloc(node, "amlogic,pins",
260 sizeof(char), (void **)&pins);
263 device_printf(dev, "missing amlogic,pins attribute in FDT\n");
270 for (p = f->pins; p->name != NULL; p++)
271 if (strncmp(p->name, pin_name, len) == 0)
274 if (p->name == NULL) {
275 /* display message prior to queuing up next string */
276 device_printf(dev, "unknown pin attribute %.*s in FDT\n",
280 /* queue up next string */
281 while (*pin_name && len) {
293 for (pp = sc->soc.ppin; pp->pkg_name != NULL; pp++)
294 if (strcmp(pp->pkg_name, p->pkg_name) == 0)
297 if (pp->pkg_name == NULL) {
299 "missing entry for package pin %s\n",
304 if (pm != aml8726_unknown_pm && pp->pull_bits == 0x00000000) {
306 "missing pull info for package pin %s\n",
311 AML_PINCTRL_LOCK(sc);
314 * First clear all other mux bits associated with this
315 * package pin. This may briefly configure the pin as
316 * GPIO ... however this should be fine since after
317 * reset the default GPIO mode is input.
320 for (cf = sc->soc.func; cf->name != NULL; cf++)
321 for (cp = cf->pins; cp->name != NULL; cp++) {
324 if (strcmp(cp->pkg_name, p->pkg_name) != 0)
326 if (cp->mux_bits == 0)
328 if (pp->aobus == false) {
329 value = MUX_READ_4(sc, cp->mux_addr);
330 value &= ~cp->mux_bits;
331 MUX_WRITE_4(sc, cp->mux_addr, value);
333 value = AOMUX_READ_4(sc, cp->mux_addr);
334 value &= ~cp->mux_bits;
335 AOMUX_WRITE_4(sc, cp->mux_addr, value);
340 * Now set the desired mux bits.
342 * In the case of GPIO there's no bits to set.
345 if (p->mux_bits != 0) {
346 if (pp->aobus == false) {
347 value = MUX_READ_4(sc, p->mux_addr);
348 value |= p->mux_bits;
349 MUX_WRITE_4(sc, p->mux_addr, value);
351 value = AOMUX_READ_4(sc, p->mux_addr);
352 value |= p->mux_bits;
353 AOMUX_WRITE_4(sc, p->mux_addr, value);
358 * Finally set the pull mode if it was specified.
362 case aml8726_enable_down_pm:
363 case aml8726_enable_up_pm:
364 if (pp->aobus == false) {
365 value = PUD_READ_4(sc, pp->pull_addr);
366 if (pm == aml8726_enable_down_pm)
367 value &= ~pp->pull_bits;
369 value |= pp->pull_bits;
370 PUD_WRITE_4(sc, pp->pull_addr, value);
372 value = AOPUD_READ_4(sc, pp->pull_addr);
373 if (pm == aml8726_enable_down_pm)
374 value &= ~(pp->pull_bits << 16);
376 value |= (pp->pull_bits << 16);
377 AOPUD_WRITE_4(sc, pp->pull_addr, value);
380 case aml8726_disable_pm:
381 case aml8726_enable_pm:
382 if (pp->aobus == false) {
383 value = PEN_READ_4(sc, pp->pull_addr);
384 if (pm == aml8726_disable_pm)
385 value &= ~pp->pull_bits;
387 value |= pp->pull_bits;
388 PEN_WRITE_4(sc, pp->pull_addr, value);
390 value = AOPEN_READ_4(sc, pp->pull_addr);
391 if (pm == aml8726_disable_pm)
392 value &= ~pp->pull_bits;
394 value |= pp->pull_bits;
395 AOPEN_WRITE_4(sc, pp->pull_addr, value);
402 AML_PINCTRL_UNLOCK(sc);
411 static device_method_t aml8726_pinctrl_methods[] = {
412 /* Device interface */
413 DEVMETHOD(device_probe, aml8726_pinctrl_probe),
414 DEVMETHOD(device_attach, aml8726_pinctrl_attach),
415 DEVMETHOD(device_detach, aml8726_pinctrl_detach),
417 /* fdt_pinctrl interface */
418 DEVMETHOD(fdt_pinctrl_configure,aml8726_pinctrl_configure_pins),
423 static driver_t aml8726_pinctrl_driver = {
425 aml8726_pinctrl_methods,
426 sizeof(struct aml8726_pinctrl_softc),
429 static devclass_t aml8726_pinctrl_devclass;
431 EARLY_DRIVER_MODULE(pinctrl, simplebus, aml8726_pinctrl_driver,
432 aml8726_pinctrl_devclass, 0, 0, BUS_PASS_CPU + BUS_PASS_ORDER_LATE);