1 1.3 christos /* $NetBSD: rdcpcib.c,v 1.3 2020/04/07 12:42:11 christos Exp $ */ 2 1.1 bouyer 3 1.1 bouyer /* 4 1.1 bouyer * Copyright (c) 2011 Manuel Bouyer. 5 1.1 bouyer * 6 1.1 bouyer * Redistribution and use in source and binary forms, with or without 7 1.1 bouyer * modification, are permitted provided that the following conditions 8 1.1 bouyer * are met: 9 1.1 bouyer * 1. Redistributions of source code must retain the above copyright 10 1.1 bouyer * notice, this list of conditions and the following disclaimer. 11 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 bouyer * notice, this list of conditions and the following disclaimer in the 13 1.1 bouyer * documentation and/or other materials provided with the distribution. 14 1.1 bouyer * 15 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 1.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 1.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 1.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 1.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 1.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 1.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 1.1 bouyer */ 26 1.1 bouyer 27 1.1 bouyer /* 28 1.1 bouyer * driver for the RDC vortex86/PMX-1000 SoC PCI-ISA bridge, which also drives 29 1.1 bouyer * the watchdog timer 30 1.1 bouyer */ 31 1.1 bouyer 32 1.1 bouyer 33 1.1 bouyer #include <sys/cdefs.h> 34 1.3 christos __KERNEL_RCSID(0, "$NetBSD: rdcpcib.c,v 1.3 2020/04/07 12:42:11 christos Exp $"); 35 1.1 bouyer 36 1.1 bouyer #include <sys/types.h> 37 1.1 bouyer #include <sys/param.h> 38 1.1 bouyer #include <sys/systm.h> 39 1.1 bouyer #include <sys/device.h> 40 1.1 bouyer #include <sys/sysctl.h> 41 1.1 bouyer #include <sys/timetc.h> 42 1.1 bouyer #include <sys/gpio.h> 43 1.2 dyoung #include <sys/bus.h> 44 1.1 bouyer 45 1.1 bouyer #include <dev/pci/pcivar.h> 46 1.1 bouyer #include <dev/pci/pcireg.h> 47 1.1 bouyer #include <dev/pci/pcidevs.h> 48 1.1 bouyer 49 1.1 bouyer #include <dev/gpio/gpiovar.h> 50 1.1 bouyer #include <dev/sysmon/sysmonvar.h> 51 1.1 bouyer 52 1.1 bouyer #include "pcibvar.h" 53 1.1 bouyer 54 1.1 bouyer /* 55 1.1 bouyer * special registers: iospace-registers for indirect access to timer and GPIO 56 1.1 bouyer */ 57 1.1 bouyer #define RDC_IND_BASE 0x22 58 1.1 bouyer #define RDC_IND_SIZE 0x2 59 1.1 bouyer #define RDC_IND_ADDR 0 60 1.1 bouyer #define RDC_IND_DATA 1 61 1.1 bouyer 62 1.1 bouyer struct rdcpcib_softc { 63 1.1 bouyer struct pcib_softc rdc_pcib; 64 1.1 bouyer 65 1.1 bouyer /* indirect registers mapping */ 66 1.1 bouyer bus_space_tag_t rdc_iot; 67 1.1 bouyer bus_space_handle_t rdc_ioh; 68 1.1 bouyer 69 1.1 bouyer /* Watchdog suppoprt */ 70 1.1 bouyer struct sysmon_wdog rdc_smw; 71 1.1 bouyer }; 72 1.1 bouyer 73 1.1 bouyer static int rdcpcibmatch(device_t, cfdata_t, void *); 74 1.1 bouyer static void rdcpcibattach(device_t, device_t, void *); 75 1.1 bouyer static int rdcpcibdetach(device_t, int); 76 1.1 bouyer 77 1.1 bouyer static uint8_t rdc_ind_read(struct rdcpcib_softc *, uint8_t); 78 1.1 bouyer static void rdc_ind_write(struct rdcpcib_softc *, uint8_t, uint8_t); 79 1.1 bouyer 80 1.1 bouyer static void rdc_wdtimer_configure(device_t); 81 1.1 bouyer static int rdc_wdtimer_unconfigure(device_t, int); 82 1.1 bouyer static int rdc_wdtimer_setmode(struct sysmon_wdog *); 83 1.1 bouyer static int rdc_wdtimer_tickle(struct sysmon_wdog *); 84 1.1 bouyer static void rdc_wdtimer_stop(struct rdcpcib_softc *); 85 1.1 bouyer static void rdc_wdtimer_start(struct rdcpcib_softc *); 86 1.1 bouyer 87 1.1 bouyer CFATTACH_DECL2_NEW(rdcpcib, sizeof(struct rdcpcib_softc), 88 1.1 bouyer rdcpcibmatch, rdcpcibattach, rdcpcibdetach, NULL, 89 1.1 bouyer pcibrescan, pcibchilddet); 90 1.1 bouyer 91 1.3 christos 92 1.3 christos static const struct rdcpcib_device { 93 1.3 christos pcireg_t vendor, product; 94 1.3 christos } rdcpcib_devices[] = { 95 1.3 christos { PCI_VENDOR_RDC, PCI_PRODUCT_RDC_R6011_PCIB}, 96 1.3 christos { PCI_VENDOR_RDC, PCI_PRODUCT_RDC_R6013_PCIB}, 97 1.3 christos { PCI_VENDOR_RDC, PCI_PRODUCT_RDC_R6031_PCIB}, 98 1.3 christos { PCI_VENDOR_RDC, PCI_PRODUCT_RDC_R6035_PCIB}, 99 1.3 christos { PCI_VENDOR_RDC, PCI_PRODUCT_RDC_R6036_PCIB}, 100 1.3 christos }; 101 1.3 christos 102 1.1 bouyer static int 103 1.1 bouyer rdcpcibmatch(device_t parent, cfdata_t match, void *aux) 104 1.1 bouyer { 105 1.1 bouyer struct pci_attach_args *pa = aux; 106 1.1 bouyer 107 1.1 bouyer if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE || 108 1.1 bouyer PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA) 109 1.1 bouyer return 0; 110 1.1 bouyer 111 1.3 christos for (size_t i = 0; i < __arraycount(rdcpcib_devices); i++) { 112 1.3 christos if (PCI_VENDOR(pa->pa_id) == rdcpcib_devices[i].vendor && 113 1.3 christos PCI_PRODUCT(pa->pa_id) == rdcpcib_devices[i].product) 114 1.1 bouyer return 10; 115 1.3 christos } 116 1.1 bouyer 117 1.1 bouyer return 0; 118 1.1 bouyer } 119 1.1 bouyer 120 1.1 bouyer static void 121 1.1 bouyer rdcpcibattach(device_t parent, device_t self, void *aux) 122 1.1 bouyer { 123 1.1 bouyer struct rdcpcib_softc *sc = device_private(self); 124 1.1 bouyer 125 1.1 bouyer /* generic PCI/ISA bridge */ 126 1.1 bouyer pcibattach(parent, self, aux); 127 1.1 bouyer 128 1.1 bouyer /* map indirect registers */ 129 1.1 bouyer sc->rdc_iot = x86_bus_space_io; 130 1.1 bouyer if (bus_space_map(sc->rdc_iot, RDC_IND_BASE, RDC_IND_SIZE, 0, 131 1.1 bouyer &sc->rdc_ioh) != 0) { 132 1.1 bouyer aprint_error_dev(self, "couldn't map indirect registers\n"); 133 1.1 bouyer return; 134 1.1 bouyer } 135 1.1 bouyer 136 1.1 bouyer /* Set up the watchdog. */ 137 1.1 bouyer rdc_wdtimer_configure(self); 138 1.1 bouyer 139 1.1 bouyer /* Install power handler XXX */ 140 1.1 bouyer if (!pmf_device_register(self, NULL, NULL)) 141 1.1 bouyer aprint_error_dev(self, "couldn't establish power handler\n"); 142 1.1 bouyer } 143 1.1 bouyer 144 1.1 bouyer static int 145 1.1 bouyer rdcpcibdetach(device_t self, int flags) 146 1.1 bouyer { 147 1.1 bouyer struct rdcpcib_softc *sc = device_private(self); 148 1.1 bouyer int rc; 149 1.1 bouyer 150 1.1 bouyer pmf_device_deregister(self); 151 1.1 bouyer 152 1.1 bouyer if ((rc = rdc_wdtimer_unconfigure(self, flags)) != 0) 153 1.1 bouyer return rc; 154 1.1 bouyer 155 1.1 bouyer bus_space_unmap(sc->rdc_iot, sc->rdc_ioh, RDC_IND_SIZE); 156 1.1 bouyer return pcibdetach(self, flags); 157 1.1 bouyer } 158 1.1 bouyer 159 1.1 bouyer /* indirect registers read/write */ 160 1.1 bouyer static uint8_t 161 1.1 bouyer rdc_ind_read(struct rdcpcib_softc *sc, uint8_t addr) 162 1.1 bouyer { 163 1.1 bouyer bus_space_write_1(sc->rdc_iot, sc->rdc_ioh, RDC_IND_ADDR, addr); 164 1.1 bouyer return bus_space_read_1(sc->rdc_iot, sc->rdc_ioh, RDC_IND_DATA); 165 1.1 bouyer } 166 1.1 bouyer 167 1.1 bouyer static void 168 1.1 bouyer rdc_ind_write(struct rdcpcib_softc *sc, uint8_t addr, uint8_t data) 169 1.1 bouyer { 170 1.1 bouyer bus_space_write_1(sc->rdc_iot, sc->rdc_ioh, RDC_IND_ADDR, addr); 171 1.1 bouyer bus_space_write_1(sc->rdc_iot, sc->rdc_ioh, RDC_IND_DATA, data); 172 1.1 bouyer } 173 1.1 bouyer 174 1.1 bouyer /* 175 1.1 bouyer * watchdog timer registers 176 1.1 bouyer */ 177 1.1 bouyer 178 1.1 bouyer /* control */ 179 1.1 bouyer #define RDC_WDT0_CTRL 0x37 180 1.1 bouyer #define RDC_WDT0_CTRL_EN 0x40 181 1.1 bouyer 182 1.1 bouyer /* signal select */ 183 1.1 bouyer #define RDC_WDT0_SSEL 0x38 184 1.1 bouyer #define RDC_WDT0_SSEL_MSK 0xf0 185 1.1 bouyer #define RDC_WDT0_SSEL_NMI 0xc0 186 1.1 bouyer #define RDC_WDT0_SSEL_RST 0xd0 187 1.1 bouyer 188 1.1 bouyer /* counter */ 189 1.1 bouyer #define RDC_WDT0_CNTL 0x39 190 1.1 bouyer #define RDC_WDT0_CNTH 0x3A 191 1.1 bouyer #define RDC_WDT0_CNTU 0x3B 192 1.1 bouyer #define RDC_WDT0_FREQ 32768 /* Hz */ 193 1.1 bouyer #define RDC_WDT0_PERIOD_MAX (1 << 24) 194 1.1 bouyer 195 1.1 bouyer /* clear counter */ 196 1.1 bouyer #define RDC_WDT0_CTRL1 0x3c 197 1.1 bouyer #define RDC_WDT0_CTRL1_RELOAD 0x40 198 1.1 bouyer #define RDC_WDT0_CRTL1_FIRE 0x80 199 1.1 bouyer 200 1.1 bouyer 201 1.1 bouyer /* 202 1.1 bouyer * Initialize the watchdog timer. 203 1.1 bouyer */ 204 1.1 bouyer static void 205 1.1 bouyer rdc_wdtimer_configure(device_t self) 206 1.1 bouyer { 207 1.1 bouyer struct rdcpcib_softc *sc = device_private(self); 208 1.1 bouyer uint8_t reg; 209 1.1 bouyer 210 1.1 bouyer /* Explicitly stop the timer. */ 211 1.1 bouyer rdc_wdtimer_stop(sc); 212 1.1 bouyer 213 1.1 bouyer /* 214 1.1 bouyer * Register the driver with the sysmon watchdog framework. 215 1.1 bouyer */ 216 1.1 bouyer sc->rdc_smw.smw_name = device_xname(self); 217 1.1 bouyer sc->rdc_smw.smw_cookie = sc; 218 1.1 bouyer sc->rdc_smw.smw_setmode = rdc_wdtimer_setmode; 219 1.1 bouyer sc->rdc_smw.smw_tickle = rdc_wdtimer_tickle; 220 1.1 bouyer sc->rdc_smw.smw_period = RDC_WDT0_PERIOD_MAX / RDC_WDT0_FREQ; 221 1.1 bouyer 222 1.1 bouyer if (sysmon_wdog_register(&sc->rdc_smw)) { 223 1.1 bouyer aprint_error_dev(self, "unable to register wdt" 224 1.1 bouyer "as a sysmon watchdog device.\n"); 225 1.1 bouyer return; 226 1.1 bouyer } 227 1.1 bouyer 228 1.1 bouyer aprint_verbose_dev(self, "watchdog timer configured.\n"); 229 1.1 bouyer reg = rdc_ind_read(sc, RDC_WDT0_CTRL1); 230 1.1 bouyer if (reg & RDC_WDT0_CRTL1_FIRE) { 231 1.1 bouyer aprint_error_dev(self, "watchdog fired bit set, clearing\n"); 232 1.1 bouyer rdc_ind_write(sc, RDC_WDT0_CTRL1, reg & ~RDC_WDT0_CRTL1_FIRE); 233 1.1 bouyer } 234 1.1 bouyer } 235 1.1 bouyer 236 1.1 bouyer static int 237 1.1 bouyer rdc_wdtimer_unconfigure(device_t self, int flags) 238 1.1 bouyer { 239 1.1 bouyer struct rdcpcib_softc *sc = device_private(self); 240 1.1 bouyer int rc; 241 1.1 bouyer 242 1.1 bouyer if ((rc = sysmon_wdog_unregister(&sc->rdc_smw)) != 0) { 243 1.1 bouyer if (rc == ERESTART) 244 1.1 bouyer rc = EINTR; 245 1.1 bouyer return rc; 246 1.1 bouyer } 247 1.1 bouyer 248 1.1 bouyer /* Explicitly stop the timer. */ 249 1.1 bouyer rdc_wdtimer_stop(sc); 250 1.1 bouyer 251 1.1 bouyer return 0; 252 1.1 bouyer } 253 1.1 bouyer 254 1.1 bouyer 255 1.1 bouyer /* 256 1.1 bouyer * Sysmon watchdog callbacks. 257 1.1 bouyer */ 258 1.1 bouyer static int 259 1.1 bouyer rdc_wdtimer_setmode(struct sysmon_wdog *smw) 260 1.1 bouyer { 261 1.1 bouyer struct rdcpcib_softc *sc = smw->smw_cookie; 262 1.1 bouyer unsigned int period; 263 1.1 bouyer 264 1.1 bouyer if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) { 265 1.1 bouyer /* Stop the timer. */ 266 1.1 bouyer rdc_wdtimer_stop(sc); 267 1.1 bouyer } else { 268 1.1 bouyer period = smw->smw_period * RDC_WDT0_FREQ; 269 1.1 bouyer if (period < 1 || 270 1.1 bouyer period > RDC_WDT0_PERIOD_MAX) 271 1.1 bouyer return EINVAL; 272 1.1 bouyer period = period - 1; 273 1.1 bouyer 274 1.1 bouyer /* Stop the timer, */ 275 1.1 bouyer rdc_wdtimer_stop(sc); 276 1.1 bouyer 277 1.1 bouyer /* set the timeout, */ 278 1.1 bouyer rdc_ind_write(sc, RDC_WDT0_CNTL, (period >> 0) & 0xff); 279 1.1 bouyer rdc_ind_write(sc, RDC_WDT0_CNTH, (period >> 8) & 0xff); 280 1.1 bouyer rdc_ind_write(sc, RDC_WDT0_CNTU, (period >> 16) & 0xff); 281 1.1 bouyer 282 1.1 bouyer /* and start the timer again */ 283 1.1 bouyer rdc_wdtimer_start(sc); 284 1.1 bouyer } 285 1.1 bouyer return 0; 286 1.1 bouyer } 287 1.1 bouyer 288 1.1 bouyer static int 289 1.1 bouyer rdc_wdtimer_tickle(struct sysmon_wdog *smw) 290 1.1 bouyer { 291 1.1 bouyer struct rdcpcib_softc *sc = smw->smw_cookie; 292 1.1 bouyer uint8_t reg; 293 1.1 bouyer 294 1.1 bouyer reg = rdc_ind_read(sc, RDC_WDT0_CTRL1); 295 1.1 bouyer rdc_ind_write(sc, RDC_WDT0_CTRL1, reg | RDC_WDT0_CTRL1_RELOAD); 296 1.1 bouyer return 0; 297 1.1 bouyer } 298 1.1 bouyer 299 1.1 bouyer static void 300 1.1 bouyer rdc_wdtimer_stop(struct rdcpcib_softc *sc) 301 1.1 bouyer { 302 1.1 bouyer uint8_t reg; 303 1.1 bouyer reg = rdc_ind_read(sc, RDC_WDT0_CTRL); 304 1.1 bouyer rdc_ind_write(sc, RDC_WDT0_CTRL, reg & ~RDC_WDT0_CTRL_EN); 305 1.1 bouyer } 306 1.1 bouyer 307 1.1 bouyer static void 308 1.1 bouyer rdc_wdtimer_start(struct rdcpcib_softc *sc) 309 1.1 bouyer { 310 1.1 bouyer uint8_t reg; 311 1.1 bouyer rdc_ind_write(sc, RDC_WDT0_SSEL, RDC_WDT0_SSEL_RST); 312 1.1 bouyer reg = rdc_ind_read(sc, RDC_WDT0_CTRL); 313 1.1 bouyer rdc_ind_write(sc, RDC_WDT0_CTRL, reg | RDC_WDT0_CTRL_EN); 314 1.1 bouyer } 315