2 * Copyright (c) 2000 Matthew C. Forman
4 * Based (heavily) on alpm.c which is:
6 * Copyright (c) 1998, 1999 Nicolas Souchu
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Power management function/SMBus function support for the AMD 756 chip.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
40 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/systm.h>
46 #include <machine/bus.h>
47 #include <machine/resource.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcireg.h>
53 #include <dev/smbus/smbconf.h>
56 #define AMDPM_DEBUG(x) if (amdpm_debug) (x)
59 static int amdpm_debug = 1;
61 static int amdpm_debug = 0;
64 #define AMDPM_VENDORID_AMD 0x1022
65 #define AMDPM_DEVICEID_AMD756PM 0x740b
66 #define AMDPM_DEVICEID_AMD766PM 0x7413
67 #define AMDPM_DEVICEID_AMD768PM 0x7443
68 #define AMDPM_DEVICEID_AMD8111PM 0x746B
70 /* nVidia nForce chipset */
71 #define AMDPM_VENDORID_NVIDIA 0x10de
72 #define AMDPM_DEVICEID_NF_SMB 0x01b4
74 /* PCI Configuration space registers */
75 #define AMDPCI_PMBASE 0x58
76 #define NFPCI_PMBASE 0x14
78 #define AMDPCI_GEN_CONFIG_PM 0x41
79 #define AMDPCI_PMIOEN (1<<7)
81 #define AMDPCI_SCIINT_CONFIG_PM 0x42
82 #define AMDPCI_SCISEL_IRQ11 11
84 #define AMDPCI_REVID 0x08
88 * Base address programmed via AMDPCI_PMBASE.
91 #define AMDSMB_GLOBAL_STATUS (0x00)
92 #define AMDSMB_GS_TO_STS (1<<5)
93 #define AMDSMB_GS_HCYC_STS (1<<4)
94 #define AMDSMB_GS_HST_STS (1<<3)
95 #define AMDSMB_GS_PRERR_STS (1<<2)
96 #define AMDSMB_GS_COL_STS (1<<1)
97 #define AMDSMB_GS_ABRT_STS (1<<0)
98 #define AMDSMB_GS_CLEAR_STS (AMDSMB_GS_TO_STS|AMDSMB_GS_HCYC_STS|AMDSMB_GS_PRERR_STS|AMDSMB_GS_COL_STS|AMDSMB_GS_ABRT_STS)
100 #define AMDSMB_GLOBAL_ENABLE (0x02)
101 #define AMDSMB_GE_ABORT (1<<5)
102 #define AMDSMB_GE_HCYC_EN (1<<4)
103 #define AMDSMB_GE_HOST_STC (1<<3)
104 #define AMDSMB_GE_CYC_QUICK 0
105 #define AMDSMB_GE_CYC_BYTE 1
106 #define AMDSMB_GE_CYC_BDATA 2
107 #define AMDSMB_GE_CYC_WDATA 3
108 #define AMDSMB_GE_CYC_PROCCALL 4
109 #define AMDSMB_GE_CYC_BLOCK 5
111 #define LSB 0x1 /* XXX: Better name: Read/Write? */
113 #define AMDSMB_HSTADDR (0x04)
114 #define AMDSMB_HSTDATA (0x06)
115 #define AMDSMB_HSTCMD (0x08)
116 #define AMDSMB_HSTDFIFO (0x09)
117 #define AMDSMB_HSLVDATA (0x0A)
118 #define AMDSMB_HSLVDA (0x0C)
119 #define AMDSMB_HSLVDDR (0x0E)
120 #define AMDSMB_SNPADDR (0x0F)
125 struct resource *res;
126 bus_space_tag_t smbst;
127 bus_space_handle_t smbsh;
132 #define AMDPM_LOCK(amdpm) mtx_lock(&(amdpm)->lock)
133 #define AMDPM_UNLOCK(amdpm) mtx_unlock(&(amdpm)->lock)
134 #define AMDPM_LOCK_ASSERT(amdpm) mtx_assert(&(amdpm)->lock, MA_OWNED)
136 #define AMDPM_SMBINB(amdpm,register) \
137 (bus_space_read_1(amdpm->smbst, amdpm->smbsh, register))
138 #define AMDPM_SMBOUTB(amdpm,register,value) \
139 (bus_space_write_1(amdpm->smbst, amdpm->smbsh, register, value))
140 #define AMDPM_SMBINW(amdpm,register) \
141 (bus_space_read_2(amdpm->smbst, amdpm->smbsh, register))
142 #define AMDPM_SMBOUTW(amdpm,register,value) \
143 (bus_space_write_2(amdpm->smbst, amdpm->smbsh, register, value))
145 static int amdpm_detach(device_t dev);
148 amdpm_probe(device_t dev)
154 vid = pci_get_vendor(dev);
155 did = pci_get_device(dev);
156 if ((vid == AMDPM_VENDORID_AMD) &&
157 ((did == AMDPM_DEVICEID_AMD756PM) ||
158 (did == AMDPM_DEVICEID_AMD766PM) ||
159 (did == AMDPM_DEVICEID_AMD768PM) ||
160 (did == AMDPM_DEVICEID_AMD8111PM))) {
161 device_set_desc(dev, "AMD 756/766/768/8111 Power Management Controller");
164 * We have to do this, since the BIOS won't give us the
165 * resource info (not mine, anyway).
167 base = pci_read_config(dev, AMDPCI_PMBASE, 4);
169 bus_set_resource(dev, SYS_RES_IOPORT, AMDPCI_PMBASE,
171 return (BUS_PROBE_DEFAULT);
174 if ((vid == AMDPM_VENDORID_NVIDIA) &&
175 (did == AMDPM_DEVICEID_NF_SMB)) {
176 device_set_desc(dev, "nForce SMBus Controller");
179 * We have to do this, since the BIOS won't give us the
180 * resource info (not mine, anyway).
182 base = pci_read_config(dev, NFPCI_PMBASE, 4);
184 bus_set_resource(dev, SYS_RES_IOPORT, NFPCI_PMBASE,
187 return (BUS_PROBE_DEFAULT);
194 amdpm_attach(device_t dev)
196 struct amdpm_softc *amdpm_sc = device_get_softc(dev);
199 /* Enable I/O block access */
200 val_b = pci_read_config(dev, AMDPCI_GEN_CONFIG_PM, 1);
201 pci_write_config(dev, AMDPCI_GEN_CONFIG_PM, val_b | AMDPCI_PMIOEN, 1);
203 /* Allocate I/O space */
204 if (pci_get_vendor(dev) == AMDPM_VENDORID_AMD)
205 amdpm_sc->rid = AMDPCI_PMBASE;
207 amdpm_sc->rid = NFPCI_PMBASE;
208 amdpm_sc->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
209 &amdpm_sc->rid, RF_ACTIVE);
211 if (amdpm_sc->res == NULL) {
212 device_printf(dev, "could not map i/o space\n");
216 amdpm_sc->smbst = rman_get_bustag(amdpm_sc->res);
217 amdpm_sc->smbsh = rman_get_bushandle(amdpm_sc->res);
218 mtx_init(&amdpm_sc->lock, device_get_nameunit(dev), "amdpm", MTX_DEF);
220 /* Allocate a new smbus device */
221 amdpm_sc->smbus = device_add_child(dev, "smbus", -1);
222 if (!amdpm_sc->smbus) {
227 bus_generic_attach(dev);
233 amdpm_detach(device_t dev)
235 struct amdpm_softc *amdpm_sc = device_get_softc(dev);
237 if (amdpm_sc->smbus) {
238 device_delete_child(dev, amdpm_sc->smbus);
239 amdpm_sc->smbus = NULL;
242 mtx_destroy(&amdpm_sc->lock);
244 bus_release_resource(dev, SYS_RES_IOPORT, amdpm_sc->rid,
251 amdpm_callback(device_t dev, int index, void *data)
256 case SMB_REQUEST_BUS:
257 case SMB_RELEASE_BUS:
267 amdpm_clear(struct amdpm_softc *sc)
270 AMDPM_LOCK_ASSERT(sc);
271 AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_STATUS, AMDSMB_GS_CLEAR_STS);
279 amdpm_abort(struct amdpm_softc *sc)
283 l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
284 AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, l | AMDSMB_GE_ABORT);
291 amdpm_idle(struct amdpm_softc *sc)
295 AMDPM_LOCK_ASSERT(sc);
296 sts = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_STATUS);
298 AMDPM_DEBUG(printf("amdpm: busy? STS=0x%x\n", sts));
300 return (~(sts & AMDSMB_GS_HST_STS));
304 * Poll the SMBus controller
307 amdpm_wait(struct amdpm_softc *sc)
313 AMDPM_LOCK_ASSERT(sc);
314 /* Wait for command to complete (SMBus controller is idle) */
317 sts = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_STATUS);
318 if (!(sts & AMDSMB_GS_HST_STS))
322 AMDPM_DEBUG(printf("amdpm: STS=0x%x (count=%d)\n", sts, count));
327 error |= SMB_ETIMEOUT;
329 if (sts & AMDSMB_GS_ABRT_STS)
332 if (sts & AMDSMB_GS_COL_STS)
335 if (sts & AMDSMB_GS_PRERR_STS)
336 error |= SMB_EBUSERR;
338 if (error != SMB_ENOERR)
345 amdpm_quick(device_t dev, u_char slave, int how)
347 struct amdpm_softc *sc = (struct amdpm_softc *)device_get_softc(dev);
353 if (!amdpm_idle(sc)) {
360 AMDPM_DEBUG(printf("amdpm: QWRITE to 0x%x", slave));
361 AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave & ~LSB);
364 AMDPM_DEBUG(printf("amdpm: QREAD to 0x%x", slave));
365 AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave | LSB);
368 panic("%s: unknown QUICK command (%x)!", __func__, how);
370 l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
371 AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_QUICK | AMDSMB_GE_HOST_STC);
373 error = amdpm_wait(sc);
375 AMDPM_DEBUG(printf(", error=0x%x\n", error));
382 amdpm_sendb(device_t dev, u_char slave, char byte)
384 struct amdpm_softc *sc = (struct amdpm_softc *)device_get_softc(dev);
390 if (!amdpm_idle(sc)) {
395 AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave & ~LSB);
396 AMDPM_SMBOUTW(sc, AMDSMB_HSTDATA, byte);
397 l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
398 AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_BYTE | AMDSMB_GE_HOST_STC);
400 error = amdpm_wait(sc);
402 AMDPM_DEBUG(printf("amdpm: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
409 amdpm_recvb(device_t dev, u_char slave, char *byte)
411 struct amdpm_softc *sc = (struct amdpm_softc *)device_get_softc(dev);
417 if (!amdpm_idle(sc)) {
422 AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave | LSB);
423 l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
424 AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_BYTE | AMDSMB_GE_HOST_STC);
426 if ((error = amdpm_wait(sc)) == SMB_ENOERR)
427 *byte = AMDPM_SMBINW(sc, AMDSMB_HSTDATA);
429 AMDPM_DEBUG(printf("amdpm: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
436 amdpm_writeb(device_t dev, u_char slave, char cmd, char byte)
438 struct amdpm_softc *sc = (struct amdpm_softc *)device_get_softc(dev);
444 if (!amdpm_idle(sc)) {
449 AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave & ~LSB);
450 AMDPM_SMBOUTW(sc, AMDSMB_HSTDATA, byte);
451 AMDPM_SMBOUTB(sc, AMDSMB_HSTCMD, cmd);
452 l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
453 AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_BDATA | AMDSMB_GE_HOST_STC);
455 error = amdpm_wait(sc);
457 AMDPM_DEBUG(printf("amdpm: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
464 amdpm_readb(device_t dev, u_char slave, char cmd, char *byte)
466 struct amdpm_softc *sc = (struct amdpm_softc *)device_get_softc(dev);
472 if (!amdpm_idle(sc)) {
477 AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave | LSB);
478 AMDPM_SMBOUTB(sc, AMDSMB_HSTCMD, cmd);
479 l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
480 AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_BDATA | AMDSMB_GE_HOST_STC);
482 if ((error = amdpm_wait(sc)) == SMB_ENOERR)
483 *byte = AMDPM_SMBINW(sc, AMDSMB_HSTDATA);
485 AMDPM_DEBUG(printf("amdpm: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, *byte, error));
492 amdpm_writew(device_t dev, u_char slave, char cmd, short word)
494 struct amdpm_softc *sc = (struct amdpm_softc *)device_get_softc(dev);
500 if (!amdpm_idle(sc)) {
505 AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave & ~LSB);
506 AMDPM_SMBOUTW(sc, AMDSMB_HSTDATA, word);
507 AMDPM_SMBOUTB(sc, AMDSMB_HSTCMD, cmd);
508 l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
509 AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_WDATA | AMDSMB_GE_HOST_STC);
511 error = amdpm_wait(sc);
513 AMDPM_DEBUG(printf("amdpm: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
520 amdpm_readw(device_t dev, u_char slave, char cmd, short *word)
522 struct amdpm_softc *sc = (struct amdpm_softc *)device_get_softc(dev);
528 if (!amdpm_idle(sc)) {
533 AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave | LSB);
534 AMDPM_SMBOUTB(sc, AMDSMB_HSTCMD, cmd);
535 l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
536 AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE, (l & 0xfff8) | AMDSMB_GE_CYC_WDATA | AMDSMB_GE_HOST_STC);
538 if ((error = amdpm_wait(sc)) == SMB_ENOERR)
539 *word = AMDPM_SMBINW(sc, AMDSMB_HSTDATA);
541 AMDPM_DEBUG(printf("amdpm: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, *word, error));
548 amdpm_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
550 struct amdpm_softc *sc = (struct amdpm_softc *)device_get_softc(dev);
555 if (count < 1 || count > 32)
560 if (!amdpm_idle(sc)) {
565 AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave & ~LSB);
568 * Do we have to reset the internal 32-byte buffer?
569 * Can't see how to do this from the data sheet.
571 AMDPM_SMBOUTW(sc, AMDSMB_HSTDATA, count);
573 /* Fill the 32-byte internal buffer */
574 for (i = 0; i < count; i++) {
575 AMDPM_SMBOUTB(sc, AMDSMB_HSTDFIFO, buf[i]);
578 AMDPM_SMBOUTB(sc, AMDSMB_HSTCMD, cmd);
579 l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
580 AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE,
581 (l & 0xfff8) | AMDSMB_GE_CYC_BLOCK | AMDSMB_GE_HOST_STC);
583 error = amdpm_wait(sc);
585 AMDPM_DEBUG(printf("amdpm: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
592 amdpm_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf)
594 struct amdpm_softc *sc = (struct amdpm_softc *)device_get_softc(dev);
599 if (*count < 1 || *count > 32)
604 if (!amdpm_idle(sc)) {
609 AMDPM_SMBOUTW(sc, AMDSMB_HSTADDR, slave | LSB);
611 AMDPM_SMBOUTB(sc, AMDSMB_HSTCMD, cmd);
613 l = AMDPM_SMBINW(sc, AMDSMB_GLOBAL_ENABLE);
614 AMDPM_SMBOUTW(sc, AMDSMB_GLOBAL_ENABLE,
615 (l & 0xfff8) | AMDSMB_GE_CYC_BLOCK | AMDSMB_GE_HOST_STC);
617 if ((error = amdpm_wait(sc)) != SMB_ENOERR)
620 len = AMDPM_SMBINW(sc, AMDSMB_HSTDATA);
622 /* Read the 32-byte internal buffer */
623 for (i = 0; i < len; i++) {
624 data = AMDPM_SMBINB(sc, AMDSMB_HSTDFIFO);
632 AMDPM_DEBUG(printf("amdpm: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, *count, cmd, error));
638 static devclass_t amdpm_devclass;
640 static device_method_t amdpm_methods[] = {
641 /* Device interface */
642 DEVMETHOD(device_probe, amdpm_probe),
643 DEVMETHOD(device_attach, amdpm_attach),
644 DEVMETHOD(device_detach, amdpm_detach),
646 /* SMBus interface */
647 DEVMETHOD(smbus_callback, amdpm_callback),
648 DEVMETHOD(smbus_quick, amdpm_quick),
649 DEVMETHOD(smbus_sendb, amdpm_sendb),
650 DEVMETHOD(smbus_recvb, amdpm_recvb),
651 DEVMETHOD(smbus_writeb, amdpm_writeb),
652 DEVMETHOD(smbus_readb, amdpm_readb),
653 DEVMETHOD(smbus_writew, amdpm_writew),
654 DEVMETHOD(smbus_readw, amdpm_readw),
655 DEVMETHOD(smbus_bwrite, amdpm_bwrite),
656 DEVMETHOD(smbus_bread, amdpm_bread),
661 static driver_t amdpm_driver = {
664 sizeof(struct amdpm_softc),
667 DRIVER_MODULE(amdpm, pci, amdpm_driver, amdpm_devclass, 0, 0);
668 DRIVER_MODULE(smbus, amdpm, smbus_driver, smbus_devclass, 0, 0);
670 MODULE_DEPEND(amdpm, pci, 1, 1, 1);
671 MODULE_DEPEND(amdpm, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
672 MODULE_VERSION(amdpm, 1);