2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, 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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
41 #include <sys/mutex.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <machine/intr.h>
47 #include <dev/fdt/simplebus.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/fdt/fdt_pinctrl.h>
54 #include <dev/extres/syscon/syscon.h>
56 #include "syscon_if.h"
60 struct rk_pinctrl_pin_drive {
65 struct rk_pinctrl_bank {
72 struct rk_pinctrl_pin_fixup {
81 struct rk_pinctrl_conf {
82 struct rk_pinctrl_bank *iomux_conf;
83 uint32_t iomux_nbanks;
84 struct rk_pinctrl_pin_fixup *pin_fixup;
86 struct rk_pinctrl_pin_drive *pin_drive;
89 uint32_t drive_offset;
92 struct rk_pinctrl_softc {
93 struct simplebus_softc simplebus_sc;
96 struct rk_pinctrl_conf *conf;
99 static struct rk_pinctrl_bank rk3328_iomux_bank[] = {
198 static struct rk_pinctrl_pin_fixup rk3328_pin_fixup[] = {
222 static struct rk_pinctrl_pin_drive rk3328_pin_drive[] = {
241 struct rk_pinctrl_conf rk3328_conf = {
242 .iomux_conf = rk3328_iomux_bank,
243 .iomux_nbanks = nitems(rk3328_iomux_bank),
244 .pin_fixup = rk3328_pin_fixup,
245 .npin_fixup = nitems(rk3328_pin_fixup),
246 .pin_drive = rk3328_pin_drive,
247 .npin_drive = nitems(rk3328_pin_drive),
249 .drive_offset = 0x200,
252 static struct ofw_compat_data compat_data[] = {
253 #ifdef SOC_ROCKCHIP_RK3328
254 {"rockchip,rk3328-pinctrl", (uintptr_t)&rk3328_conf},
260 rk_pinctrl_parse_bias(phandle_t node)
262 if (OF_hasprop(node, "bias-disable"))
264 if (OF_hasprop(node, "bias-pull-up"))
266 if (OF_hasprop(node, "bias-pull-down"))
272 static int rk_pinctrl_parse_drive(struct rk_pinctrl_softc *sc, phandle_t node,
278 if (OF_getencprop(node, "drive-strength", &value,
282 /* Map to the correct drive value */
283 for (i = 0; i < sc->conf->npin_drive; i++)
284 if (sc->conf->pin_drive[i].ma == value) {
285 *drive = sc->conf->pin_drive[i].value;
293 rk_pinctrl_get_fixup(struct rk_pinctrl_softc *sc, uint32_t bank, uint32_t pin,
294 uint32_t *reg, uint32_t *mask, uint32_t *bit)
298 for (i = 0; i < sc->conf->npin_fixup; i++)
299 if (sc->conf->pin_fixup[i].bank == bank &&
300 sc->conf->pin_fixup[i].pin == pin) {
301 *reg = sc->conf->pin_fixup[i].reg;
302 *mask = sc->conf->pin_fixup[i].mask;
303 *bit = sc->conf->pin_fixup[i].bit;
310 rk_pinctrl_configure_pin(struct rk_pinctrl_softc *sc, uint32_t *pindata)
313 uint32_t bank, subbank, pin, function, bias;
314 uint32_t bit, mask, reg, drive;
319 function = pindata[2];
320 pin_conf = OF_node_from_xref(pindata[3]);
323 for (i = 0; i < sc->conf->iomux_nbanks; i++)
324 if (sc->conf->iomux_conf[i].bank_num == bank &&
325 sc->conf->iomux_conf[i].subbank_num == subbank)
328 if (i == sc->conf->iomux_nbanks) {
329 device_printf(sc->dev, "Unknown pin %d in bank %d\n", pin,
334 /* Parse pin function */
335 reg = sc->conf->iomux_conf[i].offset;
336 switch (sc->conf->iomux_conf[i].nbits) {
340 bit = (pin % 8 % 5) * 3;
341 mask = (0x7 << bit) << 16;
346 mask = (0x3 << bit) << 16;
349 rk_pinctrl_get_fixup(sc, bank, pin, ®, &mask, &bit);
350 SYSCON_WRITE_4(sc->grf, reg, function << bit | mask);
353 bias = rk_pinctrl_parse_bias(pin_conf);
355 reg = sc->conf->pd_offset;
357 reg += bank * 0x10 + ((pin / 8) * 0x4);
359 mask = (0x3 << bit) << 16;
360 SYSCON_WRITE_4(sc->grf, reg, bias << bit | mask);
364 if (rk_pinctrl_parse_drive(sc, pin_conf, &drive) == 0) {
365 reg = sc->conf->drive_offset;
367 reg += bank * 0x10 + ((pin / 8) * 0x4);
369 mask = (0x3 << bit) << 16;
370 SYSCON_WRITE_4(sc->grf, reg, bias << bit | mask);
375 rk_pinctrl_configure_pins(device_t dev, phandle_t cfgxref)
377 struct rk_pinctrl_softc *sc;
382 sc = device_get_softc(dev);
383 node = OF_node_from_xref(cfgxref);
385 npins = OF_getencprop_alloc_multi(node, "rockchip,pins", sizeof(*pins),
390 for (i = 0; i != npins; i += 4)
391 rk_pinctrl_configure_pin(sc, pins + i);
397 rk_pinctrl_probe(device_t dev)
400 if (!ofw_bus_status_okay(dev))
403 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
406 device_set_desc(dev, "RockChip Pinctrl controller");
407 return (BUS_PROBE_DEFAULT);
411 rk_pinctrl_attach(device_t dev)
413 struct rk_pinctrl_softc *sc;
417 sc = device_get_softc(dev);
420 node = ofw_bus_get_node(dev);
422 if (OF_hasprop(node, "rockchip,grf") &&
423 syscon_get_by_ofw_property(dev, node,
424 "rockchip,grf", &sc->grf) != 0) {
425 device_printf(dev, "cannot get grf driver handle\n");
429 sc->conf = (struct rk_pinctrl_conf *)ofw_bus_search_compatible(dev,
430 compat_data)->ocd_data;
432 fdt_pinctrl_register(dev, "rockchip,pins");
433 fdt_pinctrl_configure_tree(dev);
435 simplebus_init(dev, node);
437 bus_generic_probe(dev);
439 /* Attach child devices */
440 for (node = OF_child(node); node > 0; node = OF_peer(node)) {
441 cdev = simplebus_add_device(dev, node, 0, NULL, -1, NULL);
443 device_probe_and_attach(cdev);
446 return (bus_generic_attach(dev));
450 rk_pinctrl_detach(device_t dev)
456 static device_method_t rk_pinctrl_methods[] = {
457 /* Device interface */
458 DEVMETHOD(device_probe, rk_pinctrl_probe),
459 DEVMETHOD(device_attach, rk_pinctrl_attach),
460 DEVMETHOD(device_detach, rk_pinctrl_detach),
462 /* fdt_pinctrl interface */
463 DEVMETHOD(fdt_pinctrl_configure,rk_pinctrl_configure_pins),
468 static devclass_t rk_pinctrl_devclass;
470 DEFINE_CLASS_1(rk_pinctrl, rk_pinctrl_driver, rk_pinctrl_methods,
471 sizeof(struct rk_pinctrl_softc), simplebus_driver);
473 EARLY_DRIVER_MODULE(rk_pinctrl, simplebus, rk_pinctrl_driver,
474 rk_pinctrl_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
475 MODULE_VERSION(rk_pinctrl, 1);