2 * Copyright (c) 1998, 1999 Takanori Watanabe
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/pci/intpm.c,v 1.45 2009/09/19 08:56:28 avg Exp $
29 #include <sys/param.h>
30 #include <sys/systm.h>
32 #include <sys/globaldata.h>
33 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
38 #include <bus/smbus/smbconf.h>
42 #include <bus/pci/pcireg.h>
43 #include <bus/pci/pcivar.h>
44 #include <dev/powermng/intpm/intpmreg.h>
46 #include "opt_intpm.h"
50 struct resource *io_res;
51 struct resource *irq_res;
60 #define INTSMB_LOCK(sc) lockmgr(&(sc)->lock, LK_EXCLUSIVE)
61 #define INTSMB_UNLOCK(sc) lockmgr(&(sc)->lock, LK_RELEASE)
62 #define INTSMB_LOCK_ASSERT(sc) KKASSERT(lockstatus(&(sc)->lock, curthread) != 0)
64 static int intsmb_probe(device_t);
65 static int intsmb_attach(device_t);
66 static int intsmb_detach(device_t);
67 static int intsmb_intr(struct intsmb_softc *sc);
68 static int intsmb_slvintr(struct intsmb_softc *sc);
69 static void intsmb_alrintr(struct intsmb_softc *sc);
70 static int intsmb_callback(device_t dev, int index, void *data);
71 static int intsmb_quick(device_t dev, u_char slave, int how);
72 static int intsmb_sendb(device_t dev, u_char slave, char byte);
73 static int intsmb_recvb(device_t dev, u_char slave, char *byte);
74 static int intsmb_writeb(device_t dev, u_char slave, char cmd, char byte);
75 static int intsmb_writew(device_t dev, u_char slave, char cmd, short word);
76 static int intsmb_readb(device_t dev, u_char slave, char cmd, char *byte);
77 static int intsmb_readw(device_t dev, u_char slave, char cmd, short *word);
78 static int intsmb_pcall(device_t dev, u_char slave, char cmd, short sdata, short *rdata);
79 static int intsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf);
80 static int intsmb_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf);
81 static void intsmb_start(struct intsmb_softc *sc, u_char cmd, int nointr);
82 static int intsmb_stop(struct intsmb_softc *sc);
83 static int intsmb_stop_poll(struct intsmb_softc *sc);
84 static int intsmb_free(struct intsmb_softc *sc);
85 static void intsmb_rawintr(void *arg);
88 intsmb_probe(device_t dev)
91 switch (pci_get_devid(dev)) {
92 case 0x71138086: /* Intel 82371AB */
93 case 0x719b8086: /* Intel 82443MX */
95 /* Not a good idea yet, this stops isab0 functioning */
96 case 0x02001166: /* ServerWorks OSB4 */
98 device_set_desc(dev, "Intel PIIX4 SMBUS Interface");
101 device_set_desc(dev, "AMD SB600/700/710/750 SMBus Controller");
102 /* XXX Maybe force polling right here? */
108 return (BUS_PROBE_DEFAULT);
112 intsmb_attach(device_t dev)
114 struct intsmb_softc *sc = device_get_softc(dev);
115 int error, rid, value;
121 lockinit(&sc->lock, "intsmb", 0, LK_CANRECURSE);
124 #ifndef NO_CHANGE_PCICONF
125 switch (pci_get_devid(dev)) {
126 case 0x71138086: /* Intel 82371AB */
127 case 0x719b8086: /* Intel 82443MX */
128 /* Changing configuration is allowed. */
134 rid = PCI_BASE_ADDR_SMB;
135 sc->io_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
137 if (sc->io_res == NULL) {
138 device_printf(dev, "Could not allocate I/O space\n");
144 pci_write_config(dev, PCIR_INTLINE, 0x9, 1);
145 pci_write_config(dev, PCI_HST_CFG_SMB,
146 PCI_INTR_SMB_IRQ9 | PCI_INTR_SMB_ENABLE, 1);
148 value = pci_read_config(dev, PCI_HST_CFG_SMB, 1);
149 sc->poll = (value & PCI_INTR_SMB_ENABLE) == 0;
150 intr = value & PCI_INTR_SMB_MASK;
152 case PCI_INTR_SMB_SMI:
155 case PCI_INTR_SMB_IRQ9:
158 case PCI_INTR_SMB_IRQ_PCI:
165 device_printf(dev, "intr %s %s ", str,
166 sc->poll == 0 ? "enabled" : "disabled");
167 kprintf("revision %d\n", pci_read_config(dev, PCI_REVID_SMB, 1));
169 if (!sc->poll && intr == PCI_INTR_SMB_SMI) {
171 "using polling mode when configured interrupt is SMI\n");
178 if (intr != PCI_INTR_SMB_IRQ9 && intr != PCI_INTR_SMB_IRQ_PCI) {
179 device_printf(dev, "Unsupported interrupt mode\n");
187 bus_set_resource(dev, SYS_RES_IRQ, rid, 9, 1);
189 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
190 RF_SHAREABLE | RF_ACTIVE);
191 if (sc->irq_res == NULL) {
192 device_printf(dev, "Could not allocate irq\n");
197 error = bus_setup_intr(dev, sc->irq_res, 0,
198 intsmb_rawintr, sc, &sc->irq_hand, NULL);
200 device_printf(dev, "Failed to map intr\n");
206 sc->smbus = device_add_child(dev, "smbus", -1);
207 if (sc->smbus == NULL) {
211 error = device_probe_and_attach(sc->smbus);
217 bus_write_1(sc->io_res, PIIX4_SMBSLVCNT, PIIX4_SMBSLVCNT_ALTEN);
227 intsmb_detach(device_t dev)
229 struct intsmb_softc *sc = device_get_softc(dev);
232 error = bus_generic_detach(dev);
237 device_delete_child(dev, sc->smbus);
239 bus_teardown_intr(dev, sc->irq_res, sc->irq_hand);
241 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
243 bus_release_resource(dev, SYS_RES_IOPORT, PCI_BASE_ADDR_SMB,
245 lockuninit(&sc->lock);
250 intsmb_rawintr(void *arg)
252 struct intsmb_softc *sc = arg;
261 intsmb_callback(device_t dev, int index, void *data)
266 case SMB_REQUEST_BUS:
268 case SMB_RELEASE_BUS:
277 /* Counterpart of smbtx_smb_free(). */
279 intsmb_free(struct intsmb_softc *sc)
282 INTSMB_LOCK_ASSERT(sc);
283 if ((bus_read_1(sc->io_res, PIIX4_SMBHSTSTS) & PIIX4_SMBHSTSTAT_BUSY) ||
285 (bus_read_1(sc->io_res, PIIX4_SMBSLVSTS) & PIIX4_SMBSLVSTS_BUSY) ||
291 /* Disable Interrupt in slave part. */
293 bus_write_1(sc->io_res, PIIX4_SMBSLVCNT, 0);
295 /* Reset INTR Flag to prepare INTR. */
296 bus_write_1(sc->io_res, PIIX4_SMBHSTSTS,
297 PIIX4_SMBHSTSTAT_INTR | PIIX4_SMBHSTSTAT_ERR |
298 PIIX4_SMBHSTSTAT_BUSC | PIIX4_SMBHSTSTAT_FAIL);
303 intsmb_intr(struct intsmb_softc *sc)
307 status = bus_read_1(sc->io_res, PIIX4_SMBHSTSTS);
308 if (status & PIIX4_SMBHSTSTAT_BUSY)
311 if (status & (PIIX4_SMBHSTSTAT_INTR | PIIX4_SMBHSTSTAT_ERR |
312 PIIX4_SMBHSTSTAT_BUSC | PIIX4_SMBHSTSTAT_FAIL)) {
314 tmp = bus_read_1(sc->io_res, PIIX4_SMBHSTCNT);
315 bus_write_1(sc->io_res, PIIX4_SMBHSTCNT,
316 tmp & ~PIIX4_SMBHSTCNT_INTREN);
323 return (1); /* Not Completed */
327 intsmb_slvintr(struct intsmb_softc *sc)
331 status = bus_read_1(sc->io_res, PIIX4_SMBSLVSTS);
332 if (status & PIIX4_SMBSLVSTS_BUSY)
334 if (status & PIIX4_SMBSLVSTS_ALART)
336 else if (status & ~(PIIX4_SMBSLVSTS_ALART | PIIX4_SMBSLVSTS_SDW2
337 | PIIX4_SMBSLVSTS_SDW1)) {
340 /* Reset Status Register */
341 bus_write_1(sc->io_res, PIIX4_SMBSLVSTS,
342 PIIX4_SMBSLVSTS_ALART | PIIX4_SMBSLVSTS_SDW2 |
343 PIIX4_SMBSLVSTS_SDW1 | PIIX4_SMBSLVSTS_SLV);
348 intsmb_alrintr(struct intsmb_softc *sc)
356 /* Stop generating INTR from ALART. */
357 slvcnt = bus_read_1(sc->io_res, PIIX4_SMBSLVCNT);
359 bus_write_1(sc->io_res, PIIX4_SMBSLVCNT,
360 slvcnt & ~PIIX4_SMBSLVCNT_ALTEN);
364 /* Ask bus who asserted it and then ask it what's the matter. */
366 error = intsmb_free(sc);
370 bus_write_1(sc->io_res, PIIX4_SMBHSTADD, SMBALTRESP | LSB);
371 intsmb_start(sc, PIIX4_SMBHSTCNT_PROT_BYTE, 1);
372 error = intsmb_stop_poll(sc);
374 device_printf(sc->dev, "ALART: ERROR\n");
376 addr = bus_read_1(sc->io_res, PIIX4_SMBHSTDAT0);
377 device_printf(sc->dev, "ALART_RESPONSE: 0x%x\n", addr);
380 /* Re-enable INTR from ALART. */
381 bus_write_1(sc->io_res, PIIX4_SMBSLVCNT,
382 slvcnt | PIIX4_SMBSLVCNT_ALTEN);
388 intsmb_start(struct intsmb_softc *sc, unsigned char cmd, int nointr)
392 INTSMB_LOCK_ASSERT(sc);
393 tmp = bus_read_1(sc->io_res, PIIX4_SMBHSTCNT);
396 tmp |= PIIX4_SMBHSTCNT_START;
398 /* While not in autoconfiguration enable interrupts. */
399 if (!sc->poll && !cold && !nointr)
400 tmp |= PIIX4_SMBHSTCNT_INTREN;
401 bus_write_1(sc->io_res, PIIX4_SMBHSTCNT, tmp);
405 intsmb_error(device_t dev, int status)
409 if (status & PIIX4_SMBHSTSTAT_ERR)
410 error |= SMB_EBUSERR;
411 if (status & PIIX4_SMBHSTSTAT_BUSC)
413 if (status & PIIX4_SMBHSTSTAT_FAIL)
416 if (error != 0 && bootverbose)
417 device_printf(dev, "error = %d, status = %#x\n", error, status);
425 * Polling is not encouraged because it requires waiting for the
426 * device if it is busy.
427 * (29063505.pdf from Intel) But during boot, interrupt cannot be used, so use
431 intsmb_stop_poll(struct intsmb_softc *sc)
433 int error, i, status, tmp;
435 INTSMB_LOCK_ASSERT(sc);
437 /* First, wait for busy to be set. */
438 for (i = 0; i < 0x7fff; i++)
439 if (bus_read_1(sc->io_res, PIIX4_SMBHSTSTS) &
440 PIIX4_SMBHSTSTAT_BUSY)
443 /* Wait for busy to clear. */
444 for (i = 0; i < 0x7fff; i++) {
445 status = bus_read_1(sc->io_res, PIIX4_SMBHSTSTS);
446 if (!(status & PIIX4_SMBHSTSTAT_BUSY)) {
448 error = intsmb_error(sc->dev, status);
453 /* Timed out waiting for busy to clear. */
455 tmp = bus_read_1(sc->io_res, PIIX4_SMBHSTCNT);
456 bus_write_1(sc->io_res, PIIX4_SMBHSTCNT, tmp & ~PIIX4_SMBHSTCNT_INTREN);
457 return (SMB_ETIMEOUT);
461 * Wait for completion and return result.
464 intsmb_stop(struct intsmb_softc *sc)
468 INTSMB_LOCK_ASSERT(sc);
470 if (sc->poll || cold)
471 /* So that it can use device during device probe on SMBus. */
472 return (intsmb_stop_poll(sc));
474 error = lksleep(sc, &sc->lock, PCATCH, "SMBWAI", hz / 8);
476 status = bus_read_1(sc->io_res, PIIX4_SMBHSTSTS);
477 if (!(status & PIIX4_SMBHSTSTAT_BUSY)) {
478 error = intsmb_error(sc->dev, status);
479 if (error == 0 && !(status & PIIX4_SMBHSTSTAT_INTR))
480 device_printf(sc->dev, "unknown cause why?\n");
482 bus_write_1(sc->io_res, PIIX4_SMBSLVCNT,
483 PIIX4_SMBSLVCNT_ALTEN);
489 /* Timeout Procedure. */
492 /* Re-enable supressed interrupt from slave part. */
493 bus_write_1(sc->io_res, PIIX4_SMBSLVCNT, PIIX4_SMBSLVCNT_ALTEN);
494 if (error == EWOULDBLOCK)
495 return (SMB_ETIMEOUT);
501 intsmb_quick(device_t dev, u_char slave, int how)
503 struct intsmb_softc *sc = device_get_softc(dev);
509 /* Quick command is part of Address, I think. */
522 error = intsmb_free(sc);
527 bus_write_1(sc->io_res, PIIX4_SMBHSTADD, data);
528 intsmb_start(sc, PIIX4_SMBHSTCNT_PROT_QUICK, 0);
529 error = intsmb_stop(sc);
535 intsmb_sendb(device_t dev, u_char slave, char byte)
537 struct intsmb_softc *sc = device_get_softc(dev);
541 error = intsmb_free(sc);
546 bus_write_1(sc->io_res, PIIX4_SMBHSTADD, slave & ~LSB);
547 bus_write_1(sc->io_res, PIIX4_SMBHSTCMD, byte);
548 intsmb_start(sc, PIIX4_SMBHSTCNT_PROT_BYTE, 0);
549 error = intsmb_stop(sc);
555 intsmb_recvb(device_t dev, u_char slave, char *byte)
557 struct intsmb_softc *sc = device_get_softc(dev);
561 error = intsmb_free(sc);
566 bus_write_1(sc->io_res, PIIX4_SMBHSTADD, slave | LSB);
567 intsmb_start(sc, PIIX4_SMBHSTCNT_PROT_BYTE, 0);
568 error = intsmb_stop(sc);
570 #ifdef RECV_IS_IN_CMD
572 * Linux SMBus stuff also troubles
573 * Because Intel's datasheet does not make clear.
575 *byte = bus_read_1(sc->io_res, PIIX4_SMBHSTCMD);
577 *byte = bus_read_1(sc->io_res, PIIX4_SMBHSTDAT0);
585 intsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
587 struct intsmb_softc *sc = device_get_softc(dev);
591 error = intsmb_free(sc);
596 bus_write_1(sc->io_res, PIIX4_SMBHSTADD, slave & ~LSB);
597 bus_write_1(sc->io_res, PIIX4_SMBHSTCMD, cmd);
598 bus_write_1(sc->io_res, PIIX4_SMBHSTDAT0, byte);
599 intsmb_start(sc, PIIX4_SMBHSTCNT_PROT_BDATA, 0);
600 error = intsmb_stop(sc);
606 intsmb_writew(device_t dev, u_char slave, char cmd, short word)
608 struct intsmb_softc *sc = device_get_softc(dev);
612 error = intsmb_free(sc);
617 bus_write_1(sc->io_res, PIIX4_SMBHSTADD, slave & ~LSB);
618 bus_write_1(sc->io_res, PIIX4_SMBHSTCMD, cmd);
619 bus_write_1(sc->io_res, PIIX4_SMBHSTDAT0, word & 0xff);
620 bus_write_1(sc->io_res, PIIX4_SMBHSTDAT1, (word >> 8) & 0xff);
621 intsmb_start(sc, PIIX4_SMBHSTCNT_PROT_WDATA, 0);
622 error = intsmb_stop(sc);
628 intsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
630 struct intsmb_softc *sc = device_get_softc(dev);
634 error = intsmb_free(sc);
639 bus_write_1(sc->io_res, PIIX4_SMBHSTADD, slave | LSB);
640 bus_write_1(sc->io_res, PIIX4_SMBHSTCMD, cmd);
641 intsmb_start(sc, PIIX4_SMBHSTCNT_PROT_BDATA, 0);
642 error = intsmb_stop(sc);
644 *byte = bus_read_1(sc->io_res, PIIX4_SMBHSTDAT0);
650 intsmb_readw(device_t dev, u_char slave, char cmd, short *word)
652 struct intsmb_softc *sc = device_get_softc(dev);
656 error = intsmb_free(sc);
661 bus_write_1(sc->io_res, PIIX4_SMBHSTADD, slave | LSB);
662 bus_write_1(sc->io_res, PIIX4_SMBHSTCMD, cmd);
663 intsmb_start(sc, PIIX4_SMBHSTCNT_PROT_WDATA, 0);
664 error = intsmb_stop(sc);
666 *word = bus_read_1(sc->io_res, PIIX4_SMBHSTDAT0);
667 *word |= bus_read_1(sc->io_res, PIIX4_SMBHSTDAT1) << 8;
674 * Data sheet claims that it implements all function, but also claims
675 * that it implements 7 function and not mention PCALL. So I don't know
676 * whether it will work.
679 intsmb_pcall(device_t dev, u_char slave, char cmd, short sdata, short *rdata)
682 struct intsmb_softc *sc = device_get_softc(dev);
686 error = intsmb_free(sc);
691 bus_write_1(sc->io_res, PIIX4_SMBHSTADD, slave & ~LSB);
692 bus_write_1(sc->io_res, PIIX4_SMBHSTCMD, cmd);
693 bus_write_1(sc->io_res, PIIX4_SMBHSTDAT0, sdata & 0xff);
694 bus_write_1(sc->io_res, PIIX4_SMBHSTDAT1, (sdata & 0xff) >> 8);
695 intsmb_start(sc, PIIX4_SMBHSTCNT_PROT_WDATA, 0);
696 error = intsmb_stop(sc);
698 *rdata = bus_read_1(sc->io_res, PIIX4_SMBHSTDAT0);
699 *rdata |= bus_read_1(sc->io_res, PIIX4_SMBHSTDAT1) << 8;
704 return (SMB_ENOTSUPP);
709 intsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
711 struct intsmb_softc *sc = device_get_softc(dev);
714 if (count > SMBBLOCKTRANS_MAX || count == 0)
718 error = intsmb_free(sc);
724 /* Reset internal array index. */
725 bus_read_1(sc->io_res, PIIX4_SMBHSTCNT);
727 bus_write_1(sc->io_res, PIIX4_SMBHSTADD, slave & ~LSB);
728 bus_write_1(sc->io_res, PIIX4_SMBHSTCMD, cmd);
729 for (i = 0; i < count; i++)
730 bus_write_1(sc->io_res, PIIX4_SMBBLKDAT, buf[i]);
731 bus_write_1(sc->io_res, PIIX4_SMBHSTDAT0, count);
732 intsmb_start(sc, PIIX4_SMBHSTCNT_PROT_BLOCK, 0);
733 error = intsmb_stop(sc);
739 intsmb_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf)
741 struct intsmb_softc *sc = device_get_softc(dev);
745 if (*count > SMBBLOCKTRANS_MAX || *count == 0)
749 error = intsmb_free(sc);
755 /* Reset internal array index. */
756 bus_read_1(sc->io_res, PIIX4_SMBHSTCNT);
758 bus_write_1(sc->io_res, PIIX4_SMBHSTADD, slave | LSB);
759 bus_write_1(sc->io_res, PIIX4_SMBHSTCMD, cmd);
760 bus_write_1(sc->io_res, PIIX4_SMBHSTDAT0, *count);
761 intsmb_start(sc, PIIX4_SMBHSTCNT_PROT_BLOCK, 0);
762 error = intsmb_stop(sc);
764 nread = bus_read_1(sc->io_res, PIIX4_SMBHSTDAT0);
765 if (nread != 0 && nread <= SMBBLOCKTRANS_MAX) {
766 for (i = 0; i < nread; i++) {
767 data = bus_read_1(sc->io_res, PIIX4_SMBBLKDAT);
779 static devclass_t intsmb_devclass;
781 static device_method_t intsmb_methods[] = {
782 /* Device interface */
783 DEVMETHOD(device_probe, intsmb_probe),
784 DEVMETHOD(device_attach, intsmb_attach),
785 DEVMETHOD(device_detach, intsmb_detach),
788 DEVMETHOD(bus_print_child, bus_generic_print_child),
790 /* SMBus interface */
791 DEVMETHOD(smbus_callback, intsmb_callback),
792 DEVMETHOD(smbus_quick, intsmb_quick),
793 DEVMETHOD(smbus_sendb, intsmb_sendb),
794 DEVMETHOD(smbus_recvb, intsmb_recvb),
795 DEVMETHOD(smbus_writeb, intsmb_writeb),
796 DEVMETHOD(smbus_writew, intsmb_writew),
797 DEVMETHOD(smbus_readb, intsmb_readb),
798 DEVMETHOD(smbus_readw, intsmb_readw),
799 DEVMETHOD(smbus_pcall, intsmb_pcall),
800 DEVMETHOD(smbus_bwrite, intsmb_bwrite),
801 DEVMETHOD(smbus_bread, intsmb_bread),
806 static driver_t intsmb_driver = {
809 sizeof(struct intsmb_softc),
812 DRIVER_MODULE(intsmb, pci, intsmb_driver, intsmb_devclass, 0, 0);
813 DRIVER_MODULE(smbus, intsmb, smbus_driver, smbus_devclass, 0, 0);
814 MODULE_DEPEND(intsmb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
815 MODULE_VERSION(intsmb, 1);