1 /* $NetBSD: weasel_pci.c,v 1.1 2002/01/01 16:48:34 hpeyerl Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Herb Peyerl and Jason Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Device driver for the control space on the Middle Digital, Inc. 41 * PCI-Weasel serial console board. 42 * 43 * Since the other functions of the PCI-Weasel already appear in 44 * PCI configuration space, we just need to hook up the watchdog 45 * timer. 46 */ 47 48 #include <sys/cdefs.h> 49 __KERNEL_RCSID(0, "$NetBSD: weasel_pci.c,v 1.1 2002/01/01 16:48:34 hpeyerl Exp $"); 50 51 #include <sys/param.h> 52 #include <sys/systm.h> 53 #include <sys/device.h> 54 #include <sys/wdog.h> 55 #include <sys/endian.h> 56 57 #include <machine/bus.h> 58 59 #include <dev/pci/pcireg.h> 60 #include <dev/pci/pcivar.h> 61 #include <dev/pci/pcidevs.h> 62 63 #include <dev/pci/weaselreg.h> 64 65 #include <dev/sysmon/sysmonvar.h> 66 67 struct weasel_softc { 68 struct device sc_dev; /* generic device glue */ 69 70 bus_space_tag_t sc_st; 71 bus_space_handle_t sc_sh; 72 73 struct sysmon_wdog sc_smw; 74 75 int sc_wdog_armed; 76 int sc_wdog_period; 77 }; 78 79 int weasel_pci_match(struct device *, struct cfdata *, void *); 80 void weasel_pci_attach(struct device *, struct device *, void *); 81 extern int sysmon_wdog_setmode(struct sysmon_wdog *, int, u_int); 82 83 struct cfattach weasel_pci_ca = { 84 sizeof(struct weasel_softc), weasel_pci_match, weasel_pci_attach, 85 }; 86 87 int weasel_pci_wdog_setmode(struct sysmon_wdog *); 88 int weasel_pci_wdog_tickle(struct sysmon_wdog *); 89 90 int weasel_wait_response(struct weasel_softc *); 91 int weasel_issue_command(struct weasel_softc *, uint8_t cmd); 92 93 int weasel_pci_wdog_arm(struct weasel_softc *); 94 int weasel_pci_wdog_disarm(struct weasel_softc *); 95 96 int weasel_pci_wdog_query_state(struct weasel_softc *); 97 98 int 99 weasel_pci_match(struct device *parent, struct cfdata *cf, void *aux) 100 { 101 struct pci_attach_args *pa = aux; 102 103 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_MIDDLE_DIGITAL && 104 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_MIDDLE_DIGITAL_WEASEL_CONTROL) 105 return (1); 106 107 return (0); 108 } 109 110 void 111 weasel_pci_attach(struct device *parent, struct device *self, void *aux) 112 { 113 struct weasel_softc *sc = (void *) self; 114 struct pci_attach_args *pa = aux; 115 struct weasel_config_block cfg; 116 const char *vers, *mode; 117 uint8_t v, *cp; 118 uint16_t cfg_size; 119 uint8_t buf[8]; 120 121 printf(": PCI-Weasel watchdog timer\n"); 122 123 if (pci_mapreg_map(pa, PCI_MAPREG_START, 124 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0, 125 &sc->sc_st, &sc->sc_sh, NULL, NULL) != 0) { 126 printf("%s: unable to map device registers\n", 127 sc->sc_dev.dv_xname); 128 return; 129 } 130 131 /* Ping the Weasel to see if it's alive. */ 132 if (weasel_issue_command(sc, OS_CMD_PING)) { 133 printf("%s: Weasel didn't respond to PING\n", 134 sc->sc_dev.dv_xname); 135 return; 136 } 137 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0); 138 if ((v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD)) != 139 OS_RET_PONG) { 140 printf("%s: unexpected PING response from Weasel: 0x%02x\n", 141 sc->sc_dev.dv_xname, v); 142 return; 143 } 144 145 /* Read the config block. */ 146 if (weasel_issue_command(sc, OS_CMD_SHOW_CONFIG)) { 147 printf("%s: Weasel didn't respond to SHOW_CONFIG\n", 148 sc->sc_dev.dv_xname); 149 return; 150 } 151 cfg_size = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD); 152 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0); 153 154 if (++cfg_size != sizeof(cfg)) { 155 printf("%s: weird config block size from Weasel: 0x%03x\n", 156 sc->sc_dev.dv_xname, cfg_size); 157 return; 158 } 159 160 for (cp = (uint8_t *) &cfg; cfg_size != 0; cfg_size--) { 161 if (weasel_wait_response(sc)) { 162 printf("%s: Weasel stopped providing config block(%d)\n", 163 sc->sc_dev.dv_xname, cfg_size); 164 return; 165 } 166 *cp++ = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD); 167 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0); 168 } 169 170 switch (cfg.cfg_version) { 171 case CFG_VERSION_2: 172 vers="2"; 173 switch (cfg.enable_duart_switching) { 174 case 0: 175 mode = "emulation"; 176 break; 177 case 1: 178 mode = "serial"; 179 break; 180 case 2: 181 mode = "autoswitch"; 182 break; 183 default: 184 mode = "unknown"; 185 } 186 break; 187 188 default: 189 vers = mode = NULL; 190 } 191 192 if (vers != NULL) 193 printf("%s: %s mode\n", sc->sc_dev.dv_xname, 194 mode); 195 else 196 printf("%s: unknown config version 0x%02x\n", sc->sc_dev.dv_xname, 197 cfg.cfg_version); 198 199 /* 200 * Fetch sw version. 201 */ 202 if (weasel_issue_command(sc, OS_CMD_QUERY_SW_VER)) { 203 printf("%s: didn't reply to software version query.\n", 204 sc->sc_dev.dv_xname); 205 } 206 else { 207 v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD); 208 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0); 209 if (v>7) 210 printf("%s: weird length for version string(%d).\n", 211 sc->sc_dev.dv_xname, v); 212 bzero(buf, sizeof(buf)); 213 for (cp = buf; v != 0; v--) { 214 if (weasel_wait_response(sc)) { 215 printf("%s: Weasel stopped providing version\n", 216 sc->sc_dev.dv_xname); 217 } 218 *cp++ = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD); 219 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0); 220 } 221 printf("%s: sw: %s", sc->sc_dev.dv_xname, buf); 222 } 223 /* 224 * Fetch logic version. 225 */ 226 if (weasel_issue_command(sc, OS_CMD_QUERY_L_VER)) { 227 printf("\n%s: didn't reply to logic version query.\n", 228 sc->sc_dev.dv_xname); 229 } 230 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0); 231 v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD); 232 printf(" logic: %03d", v); 233 /* 234 * Fetch vga bios version. 235 */ 236 if (weasel_issue_command(sc, OS_CMD_QUERY_VB_VER)) { 237 printf("\n%s: didn't reply to vga bios version query.\n", 238 sc->sc_dev.dv_xname); 239 } 240 v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD); 241 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0); 242 printf(" vga bios: %d.%d", (v>>4), (v&0x0f)); 243 /* 244 * Fetch hw version. 245 */ 246 if (weasel_issue_command(sc, OS_CMD_QUERY_HW_VER)) { 247 printf("\n%s: didn't reply to hardware version query.\n", 248 sc->sc_dev.dv_xname); 249 } 250 v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD); 251 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0); 252 printf(" hw: %d.%d", (v>>4), (v&0x0f)); 253 254 printf("\n%s: break passthrough %s", sc->sc_dev.dv_xname, 255 cfg.break_passthru ? "enabled" : "disabled"); 256 257 if ((sc->sc_wdog_armed = weasel_pci_wdog_query_state(sc)) == -1) 258 sc->sc_wdog_armed = 0; 259 260 /* Weasel is big-endian */ 261 sc->sc_wdog_period = be16toh(cfg.wdt_msec) / 1000; 262 263 printf(", watchdog timer %d sec.\n", sc->sc_wdog_period); 264 sc->sc_smw.smw_name = "weasel"; 265 sc->sc_smw.smw_cookie = sc; 266 sc->sc_smw.smw_setmode = weasel_pci_wdog_setmode; 267 sc->sc_smw.smw_tickle = weasel_pci_wdog_tickle; 268 sc->sc_smw.smw_period = sc->sc_wdog_period; 269 270 if (sysmon_wdog_register(&sc->sc_smw) != 0) 271 printf("%s: unable to register PC-Weasel watchdog " 272 "with sysmon\n", sc->sc_dev.dv_xname); 273 } 274 275 int 276 weasel_wait_response(struct weasel_softc *sc) 277 { 278 int i; 279 280 for (i = 10000; i ; i--) { 281 delay(100); 282 if (bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS) == 283 OS_WS_HOST_READ) 284 return(0); 285 } 286 return (1); 287 } 288 289 int 290 weasel_issue_command(struct weasel_softc *sc, uint8_t cmd) 291 { 292 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_WR, cmd); 293 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_HOST_STATUS, OS_HS_WEASEL_READ); 294 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0); 295 return (weasel_wait_response(sc)); 296 } 297 298 int 299 weasel_pci_wdog_setmode(struct sysmon_wdog *smw) 300 { 301 struct weasel_softc *sc = smw->smw_cookie; 302 int error = 0; 303 304 if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) { 305 error = weasel_pci_wdog_disarm(sc); 306 } else { 307 if (smw->smw_period == WDOG_PERIOD_DEFAULT) 308 smw->smw_period = sc->sc_wdog_period; 309 else if (smw->smw_period != sc->sc_wdog_period) { 310 /* Can't change the period on the Weasel. */ 311 return (EINVAL); 312 } 313 error = weasel_pci_wdog_arm(sc); 314 weasel_pci_wdog_tickle(smw); 315 } 316 317 return (error); 318 } 319 320 int 321 weasel_pci_wdog_tickle(struct sysmon_wdog *smw) 322 { 323 struct weasel_softc *sc = smw->smw_cookie; 324 u_int8_t reg; 325 int x; 326 int s; 327 int error = 0; 328 329 s = splhigh(); 330 /* 331 * first we tickle the watchdog 332 */ 333 reg = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_CHALLENGE); 334 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_RESPONSE, ~reg); 335 336 /* 337 * then we check to make sure the weasel is still armed. If someone 338 * has rebooted the weasel for whatever reason (firmware update), 339 * then the watchdog timer would no longer be armed and we'd be 340 * servicing nothing. Let the user know that the machine is no 341 * longer being monitored by the weasel. 342 */ 343 if((x = weasel_pci_wdog_query_state(sc)) == -1) 344 error = EIO; 345 if (x == 1) { 346 error = 0; 347 } else { 348 printf("%s: Watchdog timer disabled on PC/Weasel! Disarming wdog.\n", 349 sc->sc_dev.dv_xname); 350 sc->sc_wdog_armed = 0; 351 sysmon_wdog_setmode(smw, WDOG_MODE_DISARMED, 0); 352 error = 1; 353 } 354 splx(s); 355 356 return (error); 357 } 358 359 int 360 weasel_pci_wdog_arm(struct weasel_softc *sc) 361 { 362 u_int8_t reg; 363 int x; 364 int s; 365 int error = 0; 366 367 s = splhigh(); 368 if (weasel_issue_command(sc, OS_CMD_WDT_ENABLE)) { 369 printf("%s: no reply to watchdog enable. Check Weasel \"Allow Watchdog\" setting.\n", 370 sc->sc_dev.dv_xname); 371 error = EIO; 372 } 373 reg = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD); 374 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0); 375 376 /* 377 * Ensure that the Weasel thinks it's in the same mode we want it to 378 * be in. EIO if not. 379 */ 380 x = weasel_pci_wdog_query_state(sc); 381 switch (x) { 382 case -1: 383 error = EIO; 384 break; 385 case 0: 386 sc->sc_wdog_armed = 0; 387 error = EIO; 388 break; 389 case 1: 390 sc->sc_wdog_armed = 1; 391 error = 0; 392 break; 393 } 394 395 splx(s); 396 return(error); 397 } 398 399 400 int 401 weasel_pci_wdog_disarm(struct weasel_softc *sc) 402 { 403 u_int8_t reg; 404 int x; 405 int s; 406 int error = 0; 407 408 s = splhigh(); 409 410 if (weasel_issue_command(sc, OS_CMD_WDT_DISABLE)) { 411 printf("%s: didn't reply to watchdog disable.\n", 412 sc->sc_dev.dv_xname); 413 error = EIO; 414 } 415 reg = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD); 416 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0); 417 418 /* 419 * Ensure that the Weasel thinks it's in the same mode we want it to 420 * be in. EIO if not. 421 */ 422 x = weasel_pci_wdog_query_state(sc); 423 switch (x) { 424 case -1: 425 error = EIO; 426 break; 427 case 0: 428 sc->sc_wdog_armed = 0; 429 error = 0; 430 break; 431 case 1: 432 sc->sc_wdog_armed = 1; 433 error = EIO; 434 break; 435 } 436 437 splx(s); 438 return(error); 439 } 440 441 int 442 weasel_pci_wdog_query_state(struct weasel_softc *sc) 443 { 444 445 u_int8_t v; 446 447 if (weasel_issue_command(sc, OS_CMD_WDT_QUERY)) { 448 printf("%s: didn't reply to watchdog state query.\n", 449 sc->sc_dev.dv_xname); 450 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0); 451 return(-1); 452 } 453 v = bus_space_read_1(sc->sc_st, sc->sc_sh, WEASEL_DATA_RD); 454 bus_space_write_1(sc->sc_st, sc->sc_sh, WEASEL_STATUS, 0); 455 return(v); 456 } 457