1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2018 Rubicon Communications, LLC (Netgate) 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/bus.h> 32 33 #include <sys/kernel.h> 34 #include <sys/module.h> 35 #include <sys/rman.h> 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 39 #include <machine/bus.h> 40 #include <machine/resource.h> 41 #include <machine/intr.h> 42 43 #include <dev/extres/syscon/syscon.h> 44 45 #include <dev/fdt/fdt_pinctrl.h> 46 47 #include <dev/ofw/ofw_bus.h> 48 #include <dev/ofw/ofw_bus_subr.h> 49 50 #include "syscon_if.h" 51 52 #define PINS_PER_REG 8 53 #define BITS_PER_PIN 4 54 #define PINS_MASK 0xf 55 #define MAX_PIN_FUNC 5 56 57 struct mv_pins { 58 const char *name; 59 const char *functions[MAX_PIN_FUNC]; 60 }; 61 62 struct mv_padconf { 63 const struct mv_pins *pins; 64 size_t npins; 65 }; 66 67 const static struct mv_pins ap806_pins[] = { 68 {"mpp0", {"gpio", "sdio", NULL, "spi0"}}, 69 {"mpp1", {"gpio", "sdio", NULL, "spi0"}}, 70 {"mpp2", {"gpio", "sdio", NULL, "spi0"}}, 71 {"mpp3", {"gpio", "sdio", NULL, "spi0"}}, 72 {"mpp4", {"gpio", "sdio", NULL, "i2c0"}}, 73 {"mpp5", {"gpio", "sdio", NULL, "i2c0"}}, 74 {"mpp6", {"gpio", "sdio", NULL, NULL}}, 75 {"mpp7", {"gpio", "sdio", NULL, "uart1"}}, 76 {"mpp8", {"gpio", "sdio", NULL, "uart1"}}, 77 {"mpp9", {"gpio", "sdio", NULL, "spi0"}}, 78 {"mpp10", {"gpio", "sdio", NULL, NULL}}, 79 {"mpp11", {"gpio", NULL, NULL, "uart0"}}, 80 {"mpp12", {"gpio", "sdio", "sdio", NULL}}, 81 {"mpp13", {"gpio", NULL, NULL}}, 82 {"mpp14", {"gpio", NULL, NULL}}, 83 {"mpp15", {"gpio", NULL, NULL}}, 84 {"mpp16", {"gpio", NULL, NULL}}, 85 {"mpp17", {"gpio", NULL, NULL}}, 86 {"mpp18", {"gpio", NULL, NULL}}, 87 {"mpp19", {"gpio", NULL, NULL, "uart0", "sdio"}}, 88 }; 89 90 const struct mv_padconf ap806_padconf = { 91 .npins = nitems(ap806_pins), 92 .pins = ap806_pins, 93 }; 94 95 struct mv_pinctrl_softc { 96 device_t dev; 97 struct syscon *syscon; 98 99 struct mv_padconf *padconf; 100 }; 101 102 static struct ofw_compat_data compat_data[] = { 103 {"marvell,ap806-pinctrl", (uintptr_t)&ap806_padconf}, 104 {NULL, 0} 105 }; 106 107 #define RD4(sc, reg) SYSCON_READ_4((sc)->syscon, (reg)) 108 #define WR4(sc, reg, val) SYSCON_WRITE_4((sc)->syscon, (reg), (val)) 109 110 static void 111 mv_pinctrl_configure_pin(struct mv_pinctrl_softc *sc, uint32_t pin, 112 uint32_t function) 113 { 114 uint32_t offset, shift, reg; 115 116 offset = (pin / PINS_PER_REG) * BITS_PER_PIN; 117 shift = (pin % PINS_PER_REG) * BITS_PER_PIN; 118 reg = RD4(sc, offset); 119 reg &= ~(PINS_MASK << shift); 120 reg |= function << shift; 121 WR4(sc, offset, reg); 122 } 123 124 static int 125 mv_pinctrl_configure_pins(device_t dev, phandle_t cfgxref) 126 { 127 struct mv_pinctrl_softc *sc; 128 phandle_t node; 129 char *function; 130 const char **pins; 131 int i, pin_num, pin_func, npins; 132 133 sc = device_get_softc(dev); 134 node = OF_node_from_xref(cfgxref); 135 136 if (OF_getprop_alloc(node, "marvell,function", 137 (void **)&function) == -1) 138 return (ENOMEM); 139 140 npins = ofw_bus_string_list_to_array(node, "marvell,pins", &pins); 141 if (npins == -1) 142 return (ENOMEM); 143 144 for (i = 0; i < npins; i++) { 145 for (pin_num = 0; pin_num < sc->padconf->npins; pin_num++) { 146 if (strcmp(pins[i], sc->padconf->pins[pin_num].name) == 0) 147 break; 148 } 149 if (pin_num == sc->padconf->npins) 150 continue; 151 152 for (pin_func = 0; pin_func < MAX_PIN_FUNC; pin_func++) 153 if (sc->padconf->pins[pin_num].functions[pin_func] && 154 strcmp(function, sc->padconf->pins[pin_num].functions[pin_func]) == 0) 155 break; 156 157 if (pin_func == MAX_PIN_FUNC) 158 continue; 159 160 mv_pinctrl_configure_pin(sc, pin_num, pin_func); 161 } 162 163 OF_prop_free(pins); 164 165 return (0); 166 } 167 168 static int 169 mv_pinctrl_probe(device_t dev) 170 { 171 172 if (!ofw_bus_status_okay(dev)) 173 return (ENXIO); 174 175 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 176 return (ENXIO); 177 178 device_set_desc(dev, "Marvell Pinctrl controller"); 179 return (BUS_PROBE_DEFAULT); 180 } 181 182 static int 183 mv_pinctrl_attach(device_t dev) 184 { 185 struct mv_pinctrl_softc *sc; 186 187 sc = device_get_softc(dev); 188 sc->dev = dev; 189 sc->padconf = (struct mv_padconf *) 190 ofw_bus_search_compatible(dev,compat_data)->ocd_data; 191 192 if (SYSCON_GET_HANDLE(sc->dev, &sc->syscon) != 0 || 193 sc->syscon == NULL) { 194 device_printf(dev, "cannot get syscon for device\n"); 195 return (ENXIO); 196 } 197 198 fdt_pinctrl_register(dev, "marvell,pins"); 199 fdt_pinctrl_configure_tree(dev); 200 201 return (0); 202 } 203 204 static int 205 mv_pinctrl_detach(device_t dev) 206 { 207 208 return (EBUSY); 209 } 210 211 static device_method_t mv_pinctrl_methods[] = { 212 /* Device interface */ 213 DEVMETHOD(device_probe, mv_pinctrl_probe), 214 DEVMETHOD(device_attach, mv_pinctrl_attach), 215 DEVMETHOD(device_detach, mv_pinctrl_detach), 216 217 /* fdt_pinctrl interface */ 218 DEVMETHOD(fdt_pinctrl_configure,mv_pinctrl_configure_pins), 219 220 DEVMETHOD_END 221 }; 222 223 static driver_t mv_pinctrl_driver = { 224 "mv_pinctrl", 225 mv_pinctrl_methods, 226 sizeof(struct mv_pinctrl_softc), 227 }; 228 229 EARLY_DRIVER_MODULE(mv_pinctrl, simplebus, mv_pinctrl_driver, 0, 0, 230 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE); 231