1 /* $OpenBSD: pinctrl.c,v 1.3 2019/04/23 18:32:26 kettenis Exp $ */ 2 /* 3 * Copyright (c) 2018, 2019 Mark Kettenis <kettenis@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/device.h> 21 #include <sys/malloc.h> 22 23 #include <machine/intr.h> 24 #include <machine/bus.h> 25 #include <machine/fdt.h> 26 27 #include <dev/ofw/openfirm.h> 28 #include <dev/ofw/ofw_misc.h> 29 #include <dev/ofw/ofw_pinctrl.h> 30 #include <dev/ofw/fdt.h> 31 32 #define HREAD2(sc, reg) \ 33 (bus_space_read_2((sc)->sc_iot, (sc)->sc_ioh, (reg))) 34 #define HWRITE2(sc, reg, val) \ 35 bus_space_write_2((sc)->sc_iot, (sc)->sc_ioh, (reg), (val)) 36 #define HREAD4(sc, reg) \ 37 (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))) 38 #define HWRITE4(sc, reg, val) \ 39 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val)) 40 41 struct pinctrl_softc { 42 struct device sc_dev; 43 bus_space_tag_t sc_iot; 44 bus_space_handle_t sc_ioh; 45 46 uint32_t sc_reg_width; 47 uint32_t sc_func_mask; 48 }; 49 50 int pinctrl_match(struct device *, void *, void *); 51 void pinctrl_attach(struct device *, struct device *, void *); 52 53 struct cfattach pinctrl_ca = { 54 sizeof (struct pinctrl_softc), pinctrl_match, pinctrl_attach 55 }; 56 57 struct cfdriver pinctrl_cd = { 58 NULL, "pinctrl", DV_DULL 59 }; 60 61 int pinctrl_pinctrl(uint32_t, void *); 62 63 int 64 pinctrl_match(struct device *parent, void *match, void *aux) 65 { 66 struct fdt_attach_args *faa = aux; 67 68 return (OF_is_compatible(faa->fa_node, "pinctrl-single") || 69 OF_is_compatible(faa->fa_node, "pinconf-single")); 70 } 71 72 void 73 pinctrl_attach(struct device *parent, struct device *self, void *aux) 74 { 75 struct pinctrl_softc *sc = (struct pinctrl_softc *)self; 76 struct fdt_attach_args *faa = aux; 77 78 if (faa->fa_nreg < 1) { 79 printf(": no registers\n"); 80 return; 81 } 82 83 sc->sc_reg_width = OF_getpropint(faa->fa_node, 84 "pinctrl-single,register-width", 0); 85 if (sc->sc_reg_width != 16 && 86 sc->sc_reg_width != 32) { 87 printf(": unsupported register width\n"); 88 return; 89 } 90 91 sc->sc_func_mask = OF_getpropint(faa->fa_node, 92 "pinctrl-single,function-mask", 0); 93 94 sc->sc_iot = faa->fa_iot; 95 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 96 faa->fa_reg[0].size, 0, &sc->sc_ioh)) { 97 printf(": can't map registers\n"); 98 return; 99 } 100 101 pinctrl_register(faa->fa_node, pinctrl_pinctrl, sc); 102 103 printf("\n"); 104 } 105 106 uint32_t 107 pinctrl_set2(int node, char *setting, uint32_t val) 108 { 109 uint32_t values[2]; 110 111 if (OF_getpropintarray(node, setting, values, sizeof(values)) != 112 sizeof(values)) 113 return val; 114 115 val &= ~values[1]; 116 val |= (values[0] & values[1]); 117 return val; 118 } 119 120 uint32_t 121 pinctrl_set4(int node, char *setting, uint32_t val) 122 { 123 uint32_t values[4]; 124 125 if (OF_getpropintarray(node, setting, values, sizeof(values)) != 126 sizeof(values)) 127 return val; 128 129 val &= ~values[3]; 130 val |= (values[0] & values[3]); 131 return val; 132 } 133 134 int 135 pinctrl_pinctrl(uint32_t phandle, void *cookie) 136 { 137 struct pinctrl_softc *sc = cookie; 138 uint32_t *pins; 139 int node, len, i; 140 141 node = OF_getnodebyphandle(phandle); 142 if (node == 0) 143 return -1; 144 145 len = OF_getproplen(node, "pinctrl-single,pins"); 146 if (len <= 0) 147 return -1; 148 149 pins = malloc(len, M_TEMP, M_WAITOK); 150 OF_getpropintarray(node, "pinctrl-single,pins", pins, len); 151 152 for (i = 0; i < len / sizeof(uint32_t); i += 2) { 153 uint32_t reg = pins[i]; 154 uint32_t func = pins[i + 1]; 155 uint32_t val = 0; 156 157 if (sc->sc_reg_width == 16) 158 val = HREAD2(sc, reg); 159 else if (sc->sc_reg_width == 32) 160 val = HREAD4(sc, reg); 161 162 val &= ~sc->sc_func_mask; 163 val |= (func & sc->sc_func_mask); 164 165 val = pinctrl_set2(node, "pinctrl-single,drive-strength", val); 166 val = pinctrl_set4(node, "pinctrl-single,bias-pulldown", val); 167 val = pinctrl_set4(node, "pinctrl-single,bias-pullup", val); 168 169 if (sc->sc_reg_width == 16) 170 HWRITE2(sc, reg, val); 171 else if (sc->sc_reg_width == 32) 172 HWRITE4(sc, reg, val); 173 } 174 175 free(pins, M_TEMP, len); 176 return 0; 177 } 178