Home | History | Annotate | Line # | Download | only in pci
piixpm.c revision 1.28
      1 /* $NetBSD: piixpm.c,v 1.28 2009/02/13 19:19:52 pgoyette 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 (at) 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.28 2009/02/13 19:19:52 pgoyette 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 	device_t		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 static int	piixpm_match(device_t, cfdata_t, void *);
     83 static void	piixpm_attach(device_t, device_t, void *);
     84 
     85 static bool	piixpm_suspend(device_t PMF_FN_PROTO);
     86 static bool	piixpm_resume(device_t PMF_FN_PROTO);
     87 
     88 static int	piixpm_i2c_acquire_bus(void *, int);
     89 static void	piixpm_i2c_release_bus(void *, int);
     90 static int	piixpm_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
     91     size_t, void *, size_t, int);
     92 
     93 static int	piixpm_intr(void *);
     94 
     95 CFATTACH_DECL_NEW(piixpm, sizeof(struct piixpm_softc),
     96     piixpm_match, piixpm_attach, NULL, NULL);
     97 
     98 static int
     99 piixpm_match(device_t parent, cfdata_t match, void *aux)
    100 {
    101 	struct pci_attach_args *pa;
    102 
    103 	pa = (struct pci_attach_args *)aux;
    104 	switch (PCI_VENDOR(pa->pa_id)) {
    105 	case PCI_VENDOR_INTEL:
    106 		switch (PCI_PRODUCT(pa->pa_id)) {
    107 		case PCI_PRODUCT_INTEL_82371AB_PMC:
    108 		case PCI_PRODUCT_INTEL_82440MX_PMC:
    109 			return 1;
    110 		}
    111 		break;
    112 	case PCI_VENDOR_ATI:
    113 		switch (PCI_PRODUCT(pa->pa_id)) {
    114 		case PCI_PRODUCT_ATI_SB200_SMB:
    115 		case PCI_PRODUCT_ATI_SB300_SMB:
    116 		case PCI_PRODUCT_ATI_SB400_SMB:
    117 		case PCI_PRODUCT_ATI_SB600_SMB:	/* matches SB600/SB700/SB800 */
    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 static void
    135 piixpm_attach(device_t parent, device_t self, void *aux)
    136 {
    137 	struct piixpm_softc *sc = device_private(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_dev = self;
    147 	sc->sc_pc = pa->pa_pc;
    148 	sc->sc_pcitag = pa->pa_tag;
    149 
    150 	aprint_naive("\n");
    151 	aprint_normal("\n");
    152 
    153 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    154 	aprint_normal_dev(self, "%s (rev. 0x%02x)\n", devinfo,
    155 	    PCI_REVISION(pa->pa_class));
    156 
    157 	if (!pmf_device_register(self, piixpm_suspend, piixpm_resume))
    158 		aprint_error_dev(self, "couldn't establish power handler\n");
    159 
    160 	/* Read configuration */
    161 	conf = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_SMB_HOSTC);
    162 	DPRINTF(("%s: conf 0x%x\n", device_xname(self), conf));
    163 
    164 	if ((PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL) ||
    165 	    (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_INTEL_82371AB_PMC))
    166 		goto nopowermanagement;
    167 
    168 	/* check whether I/O access to PM regs is enabled */
    169 	pmmisc = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_PMREGMISC);
    170 	if (!(pmmisc & 1))
    171 		goto nopowermanagement;
    172 
    173 	sc->sc_pm_iot = pa->pa_iot;
    174 	/* Map I/O space */
    175 	base = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_PM_BASE);
    176 	if (bus_space_map(sc->sc_pm_iot, PCI_MAPREG_IO_ADDR(base),
    177 	    PIIX_PM_SIZE, 0, &sc->sc_pm_ioh)) {
    178 		aprint_error_dev(self, "can't map power management I/O space\n");
    179 		goto nopowermanagement;
    180 	}
    181 
    182 	/*
    183 	 * Revision 0 and 1 are PIIX4, 2 is PIIX4E, 3 is PIIX4M.
    184 	 * PIIX4 and PIIX4E have a bug in the timer latch, see Errata #20
    185 	 * in the "Specification update" (document #297738).
    186 	 */
    187 	acpipmtimer_attach(self, sc->sc_pm_iot, sc->sc_pm_ioh,
    188 			   PIIX_PM_PMTMR,
    189 		(PCI_REVISION(pa->pa_class) < 3) ? ACPIPMT_BADLATCH : 0 );
    190 
    191 nopowermanagement:
    192 	if ((conf & PIIX_SMB_HOSTC_HSTEN) == 0) {
    193 		aprint_normal_dev(self, "SMBus disabled\n");
    194 		return;
    195 	}
    196 
    197 	/* Map I/O space */
    198 	sc->sc_smb_iot = pa->pa_iot;
    199 	base = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_SMB_BASE) & 0xffff;
    200 	if (bus_space_map(sc->sc_smb_iot, PCI_MAPREG_IO_ADDR(base),
    201 	    PIIX_SMB_SIZE, 0, &sc->sc_smb_ioh)) {
    202 		aprint_error_dev(self, "can't map smbus I/O space\n");
    203 		return;
    204 	}
    205 
    206 	sc->sc_poll = 1;
    207 	aprint_normal_dev(self, "");
    208 	if ((conf & PIIX_SMB_HOSTC_INTMASK) == PIIX_SMB_HOSTC_SMI) {
    209 		/* No PCI IRQ */
    210 		aprint_normal("interrupting at SMI, ");
    211 	} else if ((conf & PIIX_SMB_HOSTC_INTMASK) == PIIX_SMB_HOSTC_IRQ) {
    212 		/* Install interrupt handler */
    213 		if (pci_intr_map(pa, &ih) == 0) {
    214 			intrstr = pci_intr_string(pa->pa_pc, ih);
    215 			sc->sc_smb_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO,
    216 			    piixpm_intr, sc);
    217 			if (sc->sc_smb_ih != NULL) {
    218 				aprint_normal("interrupting at %s", intrstr);
    219 				sc->sc_poll = 0;
    220 			}
    221 		}
    222 	}
    223 	if (sc->sc_poll)
    224 		aprint_normal("polling");
    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 PMF_FN_ARGS)
    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 PMF_FN_ARGS)
    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 static 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 static 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 static 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 	    device_xname(sc->sc_dev), 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", device_xname(sc->sc_dev), 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 		if (cmdlen == 0)
    353 			ctl = PIIX_SMB_HC_CMD_QUICK;
    354 		else
    355 			ctl = PIIX_SMB_HC_CMD_BYTE;
    356 	} else if (len == 1)
    357 		ctl = PIIX_SMB_HC_CMD_BDATA;
    358 	else if (len == 2)
    359 		ctl = PIIX_SMB_HC_CMD_WDATA;
    360 
    361 	if ((flags & I2C_F_POLL) == 0)
    362 		ctl |= PIIX_SMB_HC_INTREN;
    363 
    364 	/* Start transaction */
    365 	ctl |= PIIX_SMB_HC_START;
    366 	bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HC, ctl);
    367 
    368 	if (flags & I2C_F_POLL) {
    369 		/* Poll for completion */
    370 		DELAY(PIIXPM_DELAY);
    371 		for (retries = 1000; retries > 0; retries--) {
    372 			st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
    373 			    PIIX_SMB_HS);
    374 			if ((st & PIIX_SMB_HS_BUSY) == 0)
    375 				break;
    376 			DELAY(PIIXPM_DELAY);
    377 		}
    378 		if (st & PIIX_SMB_HS_BUSY)
    379 			goto timeout;
    380 		piixpm_intr(sc);
    381 	} else {
    382 		/* Wait for interrupt */
    383 		if (tsleep(sc, PRIBIO, "iicexec", PIIXPM_TIMEOUT * hz))
    384 			goto timeout;
    385 	}
    386 
    387 	if (sc->sc_i2c_xfer.error)
    388 		return (1);
    389 
    390 	return (0);
    391 
    392 timeout:
    393 	/*
    394 	 * Transfer timeout. Kill the transaction and clear status bits.
    395 	 */
    396 	aprint_error_dev(sc->sc_dev, "timeout, status 0x%x\n", st);
    397 	bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HC,
    398 	    PIIX_SMB_HC_KILL);
    399 	DELAY(PIIXPM_DELAY);
    400 	st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS);
    401 	if ((st & PIIX_SMB_HS_FAILED) == 0)
    402 		aprint_error_dev(sc->sc_dev, "transaction abort failed, status 0x%x\n", st);
    403 	bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS, st);
    404 	return (1);
    405 }
    406 
    407 static int
    408 piixpm_intr(void *arg)
    409 {
    410 	struct piixpm_softc *sc = arg;
    411 	u_int8_t st;
    412 	u_int8_t *b;
    413 	size_t len;
    414 
    415 	/* Read status */
    416 	st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS);
    417 	if ((st & PIIX_SMB_HS_BUSY) != 0 || (st & (PIIX_SMB_HS_INTR |
    418 	    PIIX_SMB_HS_DEVERR | PIIX_SMB_HS_BUSERR |
    419 	    PIIX_SMB_HS_FAILED)) == 0)
    420 		/* Interrupt was not for us */
    421 		return (0);
    422 
    423 	DPRINTF(("%s: intr st 0x%d\n", device_xname(sc->sc_dev), st & 0xff));
    424 
    425 	/* Clear status bits */
    426 	bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS, st);
    427 
    428 	/* Check for errors */
    429 	if (st & (PIIX_SMB_HS_DEVERR | PIIX_SMB_HS_BUSERR |
    430 	    PIIX_SMB_HS_FAILED)) {
    431 		sc->sc_i2c_xfer.error = 1;
    432 		goto done;
    433 	}
    434 
    435 	if (st & PIIX_SMB_HS_INTR) {
    436 		if (I2C_OP_WRITE_P(sc->sc_i2c_xfer.op))
    437 			goto done;
    438 
    439 		/* Read data */
    440 		b = sc->sc_i2c_xfer.buf;
    441 		len = sc->sc_i2c_xfer.len;
    442 		if (len > 0)
    443 			b[0] = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
    444 			    PIIX_SMB_HD0);
    445 		if (len > 1)
    446 			b[1] = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
    447 			    PIIX_SMB_HD1);
    448 	}
    449 
    450 done:
    451 	if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0)
    452 		wakeup(sc);
    453 	return (1);
    454 }
    455