5 #include <sys/kernel.h>
7 #include <sys/module.h>
11 #include <machine/bus.h>
12 #include <machine/resource.h>
15 #include <dev/pci/pcivar.h>
16 #include <dev/pci/pcireg.h>
18 #include <dev/iicbus/iiconf.h>
19 #include <dev/smbus/smbconf.h>
22 #define AMDSMB_DEBUG(x) if (amdsmb_debug) (x)
25 static int amdsmb_debug = 1;
27 static int amdsmb_debug = 0;
30 #define AMDSMB_VENDORID_AMD 0x1022
31 #define AMDSMB_DEVICEID_AMD8111_SMB2 0x746a
34 * ACPI 3.0, Chapter 12, Embedded Controller Interface.
36 #define EC_DATA 0x00 /* data register */
37 #define EC_SC 0x04 /* status of controller */
38 #define EC_CMD 0x04 /* command register */
40 #define EC_SC_IBF 0x02 /* data ready for embedded controller */
41 #define EC_SC_OBF 0x01 /* data ready for host */
42 #define EC_CMD_WR 0x81 /* write EC */
43 #define EC_CMD_RD 0x80 /* read EC */
46 * ACPI 3.0, Chapter 12, SMBus Host Controller Interface.
48 #define SMB_PRTCL 0x00 /* protocol */
49 #define SMB_STS 0x01 /* status */
50 #define SMB_ADDR 0x02 /* address */
51 #define SMB_CMD 0x03 /* command */
52 #define SMB_DATA 0x04 /* 32 data registers */
53 #define SMB_BCNT 0x24 /* number of data bytes */
54 #define SMB_ALRM_A 0x25 /* alarm address */
55 #define SMB_ALRM_D 0x26 /* 2 bytes alarm data */
57 #define SMB_STS_DONE 0x80
58 #define SMB_STS_ALRM 0x40
59 #define SMB_STS_RES 0x20
60 #define SMB_STS_STATUS 0x1f
61 #define SMB_STS_OK 0x00 /* OK */
62 #define SMB_STS_UF 0x07 /* Unknown Failure */
63 #define SMB_STS_DANA 0x10 /* Device Address Not Acknowledged */
64 #define SMB_STS_DED 0x11 /* Device Error Detected */
65 #define SMB_STS_DCAD 0x12 /* Device Command Access Denied */
66 #define SMB_STS_UE 0x13 /* Unknown Error */
67 #define SMB_STS_DAD 0x17 /* Device Access Denied */
68 #define SMB_STS_T 0x18 /* Timeout */
69 #define SMB_STS_HUP 0x19 /* Host Unsupported Protocol */
70 #define SMB_STS_B 0x1a /* Busy */
71 #define SMB_STS_PEC 0x1f /* PEC (CRC-8) Error */
73 #define SMB_PRTCL_WRITE 0x00
74 #define SMB_PRTCL_READ 0x01
75 #define SMB_PRTCL_QUICK 0x02
76 #define SMB_PRTCL_BYTE 0x04
77 #define SMB_PRTCL_BYTE_DATA 0x06
78 #define SMB_PRTCL_WORD_DATA 0x08
79 #define SMB_PRTCL_BLOCK_DATA 0x0a
80 #define SMB_PRTCL_PROC_CALL 0x0c
81 #define SMB_PRTCL_BLOCK_PROC_CALL 0x0d
82 #define SMB_PRTCL_PEC 0x80
87 bus_space_tag_t smbst;
88 bus_space_handle_t smbsh;
93 #define AMDSMB_ECINB(amdsmb, register) \
94 (bus_space_read_1(amdsmb->smbst, amdsmb->smbsh, register))
95 #define AMDSMB_ECOUTB(amdsmb, register, value) \
96 (bus_space_write_1(amdsmb->smbst, amdsmb->smbsh, register, value))
99 amdsmb_probe(device_t dev)
104 vid = pci_get_vendor(dev);
105 did = pci_get_device(dev);
107 if (vid == AMDSMB_VENDORID_AMD) {
109 case AMDSMB_DEVICEID_AMD8111_SMB2:
110 device_set_desc(dev, "AMD-8111 SMBus 2.0 Controller");
111 return (BUS_PROBE_DEFAULT);
119 amdsmb_attach(device_t dev)
121 struct amdsmb_softc *amdsmb_sc = device_get_softc(dev);
123 /* Allocate I/O space */
124 amdsmb_sc->rid = PCIR_BAR(0);
126 amdsmb_sc->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
127 &amdsmb_sc->rid, RF_ACTIVE);
129 if (amdsmb_sc->res == NULL) {
130 device_printf(dev, "could not map i/o space\n");
134 amdsmb_sc->smbst = rman_get_bustag(amdsmb_sc->res);
135 amdsmb_sc->smbsh = rman_get_bushandle(amdsmb_sc->res);
137 /* Allocate a new smbus device */
138 amdsmb_sc->smbus = device_add_child(dev, "smbus", -1);
139 if (!amdsmb_sc->smbus)
142 bus_generic_attach(dev);
148 amdsmb_detach(device_t dev)
150 struct amdsmb_softc *amdsmb_sc = device_get_softc(dev);
152 if (amdsmb_sc->smbus) {
153 device_delete_child(dev, amdsmb_sc->smbus);
154 amdsmb_sc->smbus = NULL;
158 bus_release_resource(dev, SYS_RES_IOPORT, amdsmb_sc->rid,
165 amdsmb_callback(device_t dev, int index, void *data)
170 case SMB_REQUEST_BUS:
171 case SMB_RELEASE_BUS:
181 amdsmb_ec_wait_write(struct amdsmb_softc *sc)
185 while (timeout-- && AMDSMB_ECINB(sc, EC_SC) & EC_SC_IBF)
188 device_printf(sc->smbus, "timeout waiting for IBF to clear\n");
195 amdsmb_ec_wait_read(struct amdsmb_softc *sc)
199 while (timeout-- && ~AMDSMB_ECINB(sc, EC_SC) & EC_SC_OBF)
202 device_printf(sc->smbus, "timeout waiting for OBF to set\n");
209 amdsmb_ec_read(struct amdsmb_softc *sc, u_char addr, u_char *data)
212 if (amdsmb_ec_wait_write(sc))
214 AMDSMB_ECOUTB(sc, EC_CMD, EC_CMD_RD);
216 if (amdsmb_ec_wait_write(sc))
218 AMDSMB_ECOUTB(sc, EC_DATA, addr);
220 if (amdsmb_ec_wait_read(sc))
222 *data = AMDSMB_ECINB(sc, EC_DATA);
228 amdsmb_ec_write(struct amdsmb_softc *sc, u_char addr, u_char data)
231 if (amdsmb_ec_wait_write(sc))
233 AMDSMB_ECOUTB(sc, EC_CMD, EC_CMD_WR);
235 if (amdsmb_ec_wait_write(sc))
237 AMDSMB_ECOUTB(sc, EC_DATA, addr);
239 if (amdsmb_ec_wait_write(sc))
241 AMDSMB_ECOUTB(sc, EC_DATA, data);
247 amdsmb_wait(struct amdsmb_softc *sc)
252 amdsmb_ec_read(sc, SMB_PRTCL, &temp);
258 amdsmb_ec_read(sc, SMB_PRTCL, &temp);
259 } while (temp != 0 && count--);
261 return (SMB_ETIMEOUT);
264 amdsmb_ec_read(sc, SMB_STS, &sts);
265 sts &= SMB_STS_STATUS;
266 AMDSMB_DEBUG(printf("amdsmb: STS=0x%x\n", sts));
279 error = SMB_ETIMEOUT;
284 error = SMB_ENOTSUPP;
295 amdsmb_quick(device_t dev, u_char slave, int how)
297 struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
301 protocol = SMB_PRTCL_QUICK;
305 protocol |= SMB_PRTCL_WRITE;
306 AMDSMB_DEBUG(printf("amdsmb: QWRITE to 0x%x", slave));
309 protocol |= SMB_PRTCL_READ;
310 AMDSMB_DEBUG(printf("amdsmb: QREAD to 0x%x", slave));
313 panic("%s: unknown QUICK command (%x)!", __func__, how);
316 amdsmb_ec_write(sc, SMB_ADDR, slave);
317 amdsmb_ec_write(sc, SMB_PRTCL, protocol);
319 error = amdsmb_wait(sc);
321 AMDSMB_DEBUG(printf(", error=0x%x\n", error));
327 amdsmb_sendb(device_t dev, u_char slave, char byte)
329 struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
332 amdsmb_ec_write(sc, SMB_CMD, byte);
333 amdsmb_ec_write(sc, SMB_ADDR, slave);
334 amdsmb_ec_write(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BYTE);
336 error = amdsmb_wait(sc);
338 AMDSMB_DEBUG(printf("amdsmb: SENDB to 0x%x, byte=0x%x, error=0x%x\n",
339 slave, byte, error));
345 amdsmb_recvb(device_t dev, u_char slave, char *byte)
347 struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
350 amdsmb_ec_write(sc, SMB_ADDR, slave);
351 amdsmb_ec_write(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BYTE);
353 if ((error = amdsmb_wait(sc)) == SMB_ENOERR)
354 amdsmb_ec_read(sc, SMB_DATA, byte);
356 AMDSMB_DEBUG(printf("amdsmb: RECVB from 0x%x, byte=0x%x, error=0x%x\n",
357 slave, *byte, error));
363 amdsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
365 struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
368 amdsmb_ec_write(sc, SMB_CMD, cmd);
369 amdsmb_ec_write(sc, SMB_DATA, byte);
370 amdsmb_ec_write(sc, SMB_ADDR, slave);
371 amdsmb_ec_write(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BYTE_DATA);
373 error = amdsmb_wait(sc);
375 AMDSMB_DEBUG(printf("amdsmb: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, "
376 "error=0x%x\n", slave, cmd, byte, error));
382 amdsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
384 struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
387 amdsmb_ec_write(sc, SMB_CMD, cmd);
388 amdsmb_ec_write(sc, SMB_ADDR, slave);
389 amdsmb_ec_write(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BYTE_DATA);
391 if ((error = amdsmb_wait(sc)) == SMB_ENOERR)
392 amdsmb_ec_read(sc, SMB_DATA, byte);
394 AMDSMB_DEBUG(printf("amdsmb: READB from 0x%x, cmd=0x%x, byte=0x%x, "
395 "error=0x%x\n", slave, cmd, (unsigned char)*byte, error));
401 amdsmb_writew(device_t dev, u_char slave, char cmd, short word)
403 struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
406 amdsmb_ec_write(sc, SMB_CMD, cmd);
407 amdsmb_ec_write(sc, SMB_DATA, word);
408 amdsmb_ec_write(sc, SMB_DATA + 1, word >> 8);
409 amdsmb_ec_write(sc, SMB_ADDR, slave);
410 amdsmb_ec_write(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_WORD_DATA);
412 error = amdsmb_wait(sc);
414 AMDSMB_DEBUG(printf("amdsmb: WRITEW to 0x%x, cmd=0x%x, word=0x%x, "
415 "error=0x%x\n", slave, cmd, word, error));
421 amdsmb_readw(device_t dev, u_char slave, char cmd, short *word)
423 struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
427 amdsmb_ec_write(sc, SMB_CMD, cmd);
428 amdsmb_ec_write(sc, SMB_ADDR, slave);
429 amdsmb_ec_write(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_WORD_DATA);
431 if ((error = amdsmb_wait(sc)) == SMB_ENOERR) {
432 amdsmb_ec_read(sc, SMB_DATA + 0, &temp[0]);
433 amdsmb_ec_read(sc, SMB_DATA + 1, &temp[1]);
434 *word = temp[0] | (temp[1] << 8);
437 AMDSMB_DEBUG(printf("amdsmb: READW from 0x%x, cmd=0x%x, word=0x%x, "
438 "error=0x%x\n", slave, cmd, (unsigned short)*word, error));
444 amdsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
446 struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
450 if (count < 1 || count > 32)
452 amdsmb_ec_write(sc, SMB_CMD, cmd);
453 amdsmb_ec_write(sc, SMB_BCNT, count);
454 for (i = 0; i < count; i++)
455 amdsmb_ec_write(sc, SMB_DATA + i, buf[i]);
456 amdsmb_ec_write(sc, SMB_ADDR, slave);
457 amdsmb_ec_write(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BLOCK_DATA);
459 error = amdsmb_wait(sc);
461 AMDSMB_DEBUG(printf("amdsmb: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, "
462 "error=0x%x", slave, count, cmd, error));
468 amdsmb_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf)
470 struct amdsmb_softc *sc = (struct amdsmb_softc *)device_get_softc(dev);
474 if (*count < 1 || *count > 32)
476 amdsmb_ec_write(sc, SMB_CMD, cmd);
477 amdsmb_ec_write(sc, SMB_ADDR, slave);
478 amdsmb_ec_write(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BLOCK_DATA);
480 if ((error = amdsmb_wait(sc)) == SMB_ENOERR) {
481 amdsmb_ec_read(sc, SMB_BCNT, &len);
482 for (i = 0; i < len; i++) {
483 amdsmb_ec_read(sc, SMB_DATA + i, &data);
490 AMDSMB_DEBUG(printf("amdsmb: READBLK to 0x%x, count=0x%x, cmd=0x%x, "
491 "error=0x%x", slave, *count, cmd, error));
496 static device_method_t amdsmb_methods[] = {
497 /* Device interface */
498 DEVMETHOD(device_probe, amdsmb_probe),
499 DEVMETHOD(device_attach, amdsmb_attach),
500 DEVMETHOD(device_detach, amdsmb_detach),
502 /* SMBus interface */
503 DEVMETHOD(smbus_callback, amdsmb_callback),
504 DEVMETHOD(smbus_quick, amdsmb_quick),
505 DEVMETHOD(smbus_sendb, amdsmb_sendb),
506 DEVMETHOD(smbus_recvb, amdsmb_recvb),
507 DEVMETHOD(smbus_writeb, amdsmb_writeb),
508 DEVMETHOD(smbus_readb, amdsmb_readb),
509 DEVMETHOD(smbus_writew, amdsmb_writew),
510 DEVMETHOD(smbus_readw, amdsmb_readw),
511 DEVMETHOD(smbus_bwrite, amdsmb_bwrite),
512 DEVMETHOD(smbus_bread, amdsmb_bread),
517 static devclass_t amdsmb_devclass;
519 static driver_t amdsmb_driver = {
522 sizeof(struct amdsmb_softc),
525 DRIVER_MODULE(amdsmb, pci, amdsmb_driver, amdsmb_devclass, 0, 0);
526 DRIVER_MODULE(smbus, amdsmb, smbus_driver, smbus_devclass, 0, 0);
528 MODULE_DEPEND(amdsmb, pci, 1, 1, 1);
529 MODULE_DEPEND(amdsmb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
530 MODULE_VERSION(amdsmb, 1);