1 /* $NetBSD: piixpm.c,v 1.19 2007/12/11 11:25:53 lukem Exp $ */ 2 /* $OpenBSD: piixpm.c,v 1.20 2006/02/27 08:25:02 grange Exp $ */ 3 4 /* 5 * Copyright (c) 2005, 2006 Alexander Yurchenko <grange@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* 21 * Intel PIIX and compatible Power Management controller driver. 22 */ 23 24 #include <sys/cdefs.h> 25 __KERNEL_RCSID(0, "$NetBSD: piixpm.c,v 1.19 2007/12/11 11:25:53 lukem Exp $"); 26 27 #include <sys/param.h> 28 #include <sys/systm.h> 29 #include <sys/device.h> 30 #include <sys/kernel.h> 31 #include <sys/rwlock.h> 32 #include <sys/proc.h> 33 34 #include <sys/bus.h> 35 36 #include <dev/pci/pcidevs.h> 37 #include <dev/pci/pcireg.h> 38 #include <dev/pci/pcivar.h> 39 40 #include <dev/pci/piixpmreg.h> 41 42 #include <dev/i2c/i2cvar.h> 43 44 #include <dev/ic/acpipmtimer.h> 45 46 #ifdef PIIXPM_DEBUG 47 #define DPRINTF(x) printf x 48 #else 49 #define DPRINTF(x) 50 #endif 51 52 #define PIIXPM_DELAY 200 53 #define PIIXPM_TIMEOUT 1 54 55 struct piixpm_softc { 56 struct device sc_dev; 57 58 bus_space_tag_t sc_smb_iot; 59 bus_space_handle_t sc_smb_ioh; 60 void * sc_smb_ih; 61 int sc_poll; 62 63 bus_space_tag_t sc_pm_iot; 64 bus_space_handle_t sc_pm_ioh; 65 66 pci_chipset_tag_t sc_pc; 67 pcitag_t sc_pcitag; 68 69 struct i2c_controller sc_i2c_tag; 70 krwlock_t sc_i2c_rwlock; 71 struct { 72 i2c_op_t op; 73 void * buf; 74 size_t len; 75 int flags; 76 volatile int error; 77 } sc_i2c_xfer; 78 79 pcireg_t sc_devact[2]; 80 }; 81 82 int piixpm_match(struct device *, struct cfdata *, void *); 83 void piixpm_attach(struct device *, struct device *, void *); 84 85 static bool piixpm_suspend(device_t); 86 static bool piixpm_resume(device_t); 87 88 int piixpm_i2c_acquire_bus(void *, int); 89 void piixpm_i2c_release_bus(void *, int); 90 int piixpm_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t, 91 void *, size_t, int); 92 93 int piixpm_intr(void *); 94 95 CFATTACH_DECL(piixpm, sizeof(struct piixpm_softc), 96 piixpm_match, piixpm_attach, NULL, NULL); 97 98 int 99 piixpm_match(struct device *parent, struct cfdata *match, 100 void *aux) 101 { 102 struct pci_attach_args *pa; 103 104 pa = (struct pci_attach_args *)aux; 105 switch (PCI_VENDOR(pa->pa_id)) { 106 case PCI_VENDOR_INTEL: 107 switch (PCI_PRODUCT(pa->pa_id)) { 108 case PCI_PRODUCT_INTEL_82371AB_PMC: 109 case PCI_PRODUCT_INTEL_82440MX_PMC: 110 return 1; 111 } 112 break; 113 case PCI_VENDOR_ATI: 114 switch (PCI_PRODUCT(pa->pa_id)) { 115 case PCI_PRODUCT_ATI_SB200_SMB: 116 case PCI_PRODUCT_ATI_SB300_SMB: 117 case PCI_PRODUCT_ATI_SB400_SMB: 118 return 1; 119 } 120 break; 121 case PCI_VENDOR_SERVERWORKS: 122 switch (PCI_PRODUCT(pa->pa_id)) { 123 case PCI_PRODUCT_SERVERWORKS_OSB4: 124 case PCI_PRODUCT_SERVERWORKS_CSB5: 125 case PCI_PRODUCT_SERVERWORKS_CSB6: 126 case PCI_PRODUCT_SERVERWORKS_HT1000SB: 127 return 1; 128 } 129 } 130 131 return 0; 132 } 133 134 void 135 piixpm_attach(struct device *parent, struct device *self, void *aux) 136 { 137 struct piixpm_softc *sc = (struct piixpm_softc *)self; 138 struct pci_attach_args *pa = aux; 139 struct i2cbus_attach_args iba; 140 pcireg_t base, conf; 141 pcireg_t pmmisc; 142 pci_intr_handle_t ih; 143 char devinfo[256]; 144 const char *intrstr = NULL; 145 146 sc->sc_pc = pa->pa_pc; 147 sc->sc_pcitag = pa->pa_tag; 148 149 aprint_naive("\n"); 150 151 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 152 aprint_normal("\n%s: %s (rev. 0x%02x)\n", 153 device_xname(self), devinfo, PCI_REVISION(pa->pa_class)); 154 155 if (!pmf_device_register(self, piixpm_suspend, piixpm_resume)) 156 aprint_error_dev(self, "couldn't establish power handler\n"); 157 158 /* Read configuration */ 159 conf = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_SMB_HOSTC); 160 DPRINTF((": conf 0x%x", conf)); 161 162 if ((PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL) || 163 (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_INTEL_82371AB_PMC)) 164 goto nopowermanagement; 165 166 /* check whether I/O access to PM regs is enabled */ 167 pmmisc = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_PMREGMISC); 168 if (!(pmmisc & 1)) 169 goto nopowermanagement; 170 171 sc->sc_pm_iot = pa->pa_iot; 172 /* Map I/O space */ 173 base = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_PM_BASE); 174 if (bus_space_map(sc->sc_pm_iot, PCI_MAPREG_IO_ADDR(base), 175 PIIX_PM_SIZE, 0, &sc->sc_pm_ioh)) { 176 aprint_error("%s: can't map power management I/O space\n", 177 sc->sc_dev.dv_xname); 178 goto nopowermanagement; 179 } 180 181 /* 182 * Revision 0 and 1 are PIIX4, 2 is PIIX4E, 3 is PIIX4M. 183 * PIIX4 and PIIX4E have a bug in the timer latch, see Errata #20 184 * in the "Specification update" (document #297738). 185 */ 186 acpipmtimer_attach(&sc->sc_dev, sc->sc_pm_iot, sc->sc_pm_ioh, 187 PIIX_PM_PMTMR, 188 (PCI_REVISION(pa->pa_class) < 3) ? ACPIPMT_BADLATCH : 0 ); 189 190 nopowermanagement: 191 if ((conf & PIIX_SMB_HOSTC_HSTEN) == 0) { 192 aprint_normal("%s: SMBus disabled\n", sc->sc_dev.dv_xname); 193 return; 194 } 195 196 /* Map I/O space */ 197 sc->sc_smb_iot = pa->pa_iot; 198 base = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_SMB_BASE) & 0xffff; 199 if (bus_space_map(sc->sc_smb_iot, PCI_MAPREG_IO_ADDR(base), 200 PIIX_SMB_SIZE, 0, &sc->sc_smb_ioh)) { 201 aprint_error("%s: can't map smbus I/O space\n", 202 sc->sc_dev.dv_xname); 203 return; 204 } 205 206 sc->sc_poll = 1; 207 if ((conf & PIIX_SMB_HOSTC_INTMASK) == PIIX_SMB_HOSTC_SMI) { 208 /* No PCI IRQ */ 209 aprint_normal("%s: interrupting at SMI", sc->sc_dev.dv_xname); 210 } else if ((conf & PIIX_SMB_HOSTC_INTMASK) == PIIX_SMB_HOSTC_IRQ) { 211 /* Install interrupt handler */ 212 if (pci_intr_map(pa, &ih) == 0) { 213 intrstr = pci_intr_string(pa->pa_pc, ih); 214 sc->sc_smb_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, 215 piixpm_intr, sc); 216 if (sc->sc_smb_ih != NULL) { 217 aprint_normal("%s: interrupting at %s", 218 sc->sc_dev.dv_xname, intrstr); 219 sc->sc_poll = 0; 220 } 221 } 222 if (sc->sc_poll) 223 aprint_normal("%s: polling", sc->sc_dev.dv_xname); 224 } 225 226 aprint_normal("\n"); 227 228 /* Attach I2C bus */ 229 rw_init(&sc->sc_i2c_rwlock); 230 sc->sc_i2c_tag.ic_cookie = sc; 231 sc->sc_i2c_tag.ic_acquire_bus = piixpm_i2c_acquire_bus; 232 sc->sc_i2c_tag.ic_release_bus = piixpm_i2c_release_bus; 233 sc->sc_i2c_tag.ic_exec = piixpm_i2c_exec; 234 235 bzero(&iba, sizeof(iba)); 236 iba.iba_tag = &sc->sc_i2c_tag; 237 config_found_ia(self, "i2cbus", &iba, iicbus_print); 238 239 return; 240 } 241 242 static bool 243 piixpm_suspend(device_t dv) 244 { 245 struct piixpm_softc *sc = device_private(dv); 246 247 sc->sc_devact[0] = pci_conf_read(sc->sc_pc, sc->sc_pcitag, 248 PIIX_DEVACTA); 249 sc->sc_devact[1] = pci_conf_read(sc->sc_pc, sc->sc_pcitag, 250 PIIX_DEVACTB); 251 252 return true; 253 } 254 255 static bool 256 piixpm_resume(device_t dv) 257 { 258 struct piixpm_softc *sc = device_private(dv); 259 260 pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX_DEVACTA, 261 sc->sc_devact[0]); 262 pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX_DEVACTB, 263 sc->sc_devact[1]); 264 265 return true; 266 } 267 268 int 269 piixpm_i2c_acquire_bus(void *cookie, int flags) 270 { 271 struct piixpm_softc *sc = cookie; 272 273 if (cold || sc->sc_poll || (flags & I2C_F_POLL)) 274 return (0); 275 276 rw_enter(&sc->sc_i2c_rwlock, RW_WRITER); 277 return 0; 278 } 279 280 void 281 piixpm_i2c_release_bus(void *cookie, int flags) 282 { 283 struct piixpm_softc *sc = cookie; 284 285 if (cold || sc->sc_poll || (flags & I2C_F_POLL)) 286 return; 287 288 rw_exit(&sc->sc_i2c_rwlock); 289 } 290 291 int 292 piixpm_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, 293 const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags) 294 { 295 struct piixpm_softc *sc = cookie; 296 const u_int8_t *b; 297 u_int8_t ctl = 0, st; 298 int retries; 299 300 DPRINTF(("%s: exec: op %d, addr 0x%x, cmdlen %d, len %d, flags 0x%x\n", 301 sc->sc_dev.dv_xname, op, addr, cmdlen, len, flags)); 302 303 /* Wait for bus to be idle */ 304 for (retries = 100; retries > 0; retries--) { 305 st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh, 306 PIIX_SMB_HS); 307 if (!(st & PIIX_SMB_HS_BUSY)) 308 break; 309 DELAY(PIIXPM_DELAY); 310 } 311 DPRINTF(("%s: exec: st 0x%d\n", sc->sc_dev.dv_xname, st & 0xff)); 312 if (st & PIIX_SMB_HS_BUSY) 313 return (1); 314 315 if (cold || sc->sc_poll) 316 flags |= I2C_F_POLL; 317 318 if (!I2C_OP_STOP_P(op) || cmdlen > 1 || len > 2) 319 return (1); 320 321 /* Setup transfer */ 322 sc->sc_i2c_xfer.op = op; 323 sc->sc_i2c_xfer.buf = buf; 324 sc->sc_i2c_xfer.len = len; 325 sc->sc_i2c_xfer.flags = flags; 326 sc->sc_i2c_xfer.error = 0; 327 328 /* Set slave address and transfer direction */ 329 bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_TXSLVA, 330 PIIX_SMB_TXSLVA_ADDR(addr) | 331 (I2C_OP_READ_P(op) ? PIIX_SMB_TXSLVA_READ : 0)); 332 333 b = cmdbuf; 334 if (cmdlen > 0) 335 /* Set command byte */ 336 bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, 337 PIIX_SMB_HCMD, b[0]); 338 339 if (I2C_OP_WRITE_P(op)) { 340 /* Write data */ 341 b = buf; 342 if (len > 0) 343 bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, 344 PIIX_SMB_HD0, b[0]); 345 if (len > 1) 346 bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, 347 PIIX_SMB_HD1, b[1]); 348 } 349 350 /* Set SMBus command */ 351 if (len == 0) 352 ctl = PIIX_SMB_HC_CMD_BYTE; 353 else if (len == 1) 354 ctl = PIIX_SMB_HC_CMD_BDATA; 355 else if (len == 2) 356 ctl = PIIX_SMB_HC_CMD_WDATA; 357 358 if ((flags & I2C_F_POLL) == 0) 359 ctl |= PIIX_SMB_HC_INTREN; 360 361 /* Start transaction */ 362 ctl |= PIIX_SMB_HC_START; 363 bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HC, ctl); 364 365 if (flags & I2C_F_POLL) { 366 /* Poll for completion */ 367 DELAY(PIIXPM_DELAY); 368 for (retries = 1000; retries > 0; retries--) { 369 st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh, 370 PIIX_SMB_HS); 371 if ((st & PIIX_SMB_HS_BUSY) == 0) 372 break; 373 DELAY(PIIXPM_DELAY); 374 } 375 if (st & PIIX_SMB_HS_BUSY) 376 goto timeout; 377 piixpm_intr(sc); 378 } else { 379 /* Wait for interrupt */ 380 if (tsleep(sc, PRIBIO, "iicexec", PIIXPM_TIMEOUT * hz)) 381 goto timeout; 382 } 383 384 if (sc->sc_i2c_xfer.error) 385 return (1); 386 387 return (0); 388 389 timeout: 390 /* 391 * Transfer timeout. Kill the transaction and clear status bits. 392 */ 393 aprint_error("%s: timeout, status 0x%x\n", sc->sc_dev.dv_xname, st); 394 bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HC, 395 PIIX_SMB_HC_KILL); 396 DELAY(PIIXPM_DELAY); 397 st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS); 398 if ((st & PIIX_SMB_HS_FAILED) == 0) 399 aprint_error("%s: transaction abort failed, status 0x%x\n", 400 sc->sc_dev.dv_xname, st); 401 bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS, st); 402 return (1); 403 } 404 405 int 406 piixpm_intr(void *arg) 407 { 408 struct piixpm_softc *sc = arg; 409 u_int8_t st; 410 u_int8_t *b; 411 size_t len; 412 413 /* Read status */ 414 st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS); 415 if ((st & PIIX_SMB_HS_BUSY) != 0 || (st & (PIIX_SMB_HS_INTR | 416 PIIX_SMB_HS_DEVERR | PIIX_SMB_HS_BUSERR | 417 PIIX_SMB_HS_FAILED)) == 0) 418 /* Interrupt was not for us */ 419 return (0); 420 421 DPRINTF(("%s: intr st 0x%d\n", sc->sc_dev.dv_xname, st & 0xff)); 422 423 /* Clear status bits */ 424 bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS, st); 425 426 /* Check for errors */ 427 if (st & (PIIX_SMB_HS_DEVERR | PIIX_SMB_HS_BUSERR | 428 PIIX_SMB_HS_FAILED)) { 429 sc->sc_i2c_xfer.error = 1; 430 goto done; 431 } 432 433 if (st & PIIX_SMB_HS_INTR) { 434 if (I2C_OP_WRITE_P(sc->sc_i2c_xfer.op)) 435 goto done; 436 437 /* Read data */ 438 b = sc->sc_i2c_xfer.buf; 439 len = sc->sc_i2c_xfer.len; 440 if (len > 0) 441 b[0] = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh, 442 PIIX_SMB_HD0); 443 if (len > 1) 444 b[1] = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh, 445 PIIX_SMB_HD1); 446 } 447 448 done: 449 if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0) 450 wakeup(sc); 451 return (1); 452 } 453