5 #include <sys/kernel.h>
7 #include <sys/module.h>
11 #include <machine/bus.h>
12 #include <machine/clock.h>
13 #include <machine/resource.h>
16 #include <dev/pci/pcivar.h>
17 #include <dev/pci/pcireg.h>
19 #include <dev/iicbus/iiconf.h>
20 #include <dev/smbus/smbconf.h>
23 #define NFSMB_DEBUG(x) if (nfsmb_debug) (x)
26 static int nfsmb_debug = 1;
28 static int nfsmb_debug = 0;
31 /* NVIDIA nForce2/3/4 MCP */
32 #define NFSMB_VENDORID_NVIDIA 0x10de
33 #define NFSMB_DEVICEID_NF2_SMB 0x0064
34 #define NFSMB_DEVICEID_NF2_ULTRA_SMB 0x0084
35 #define NFSMB_DEVICEID_NF3_PRO150_SMB 0x00d4
36 #define NFSMB_DEVICEID_NF3_250GB_SMB 0x00e4
37 #define NFSMB_DEVICEID_NF4_SMB 0x0052
39 /* PCI Configuration space registers */
40 #define NF2PCI_SMBASE_1 PCIR_BAR(4)
41 #define NF2PCI_SMBASE_2 PCIR_BAR(5)
44 * ACPI 3.0, Chapter 12, SMBus Host Controller Interface.
46 #define SMB_PRTCL 0x00 /* protocol */
47 #define SMB_STS 0x01 /* status */
48 #define SMB_ADDR 0x02 /* address */
49 #define SMB_CMD 0x03 /* command */
50 #define SMB_DATA 0x04 /* 32 data registers */
51 #define SMB_BCNT 0x24 /* number of data bytes */
52 #define SMB_ALRM_A 0x25 /* alarm address */
53 #define SMB_ALRM_D 0x26 /* 2 bytes alarm data */
55 #define SMB_STS_DONE 0x80
56 #define SMB_STS_ALRM 0x40
57 #define SMB_STS_RES 0x20
58 #define SMB_STS_STATUS 0x1f
59 #define SMB_STS_OK 0x00 /* OK */
60 #define SMB_STS_UF 0x07 /* Unknown Failure */
61 #define SMB_STS_DANA 0x10 /* Device Address Not Acknowledged */
62 #define SMB_STS_DED 0x11 /* Device Error Detected */
63 #define SMB_STS_DCAD 0x12 /* Device Command Access Denied */
64 #define SMB_STS_UE 0x13 /* Unknown Error */
65 #define SMB_STS_DAD 0x17 /* Device Access Denied */
66 #define SMB_STS_T 0x18 /* Timeout */
67 #define SMB_STS_HUP 0x19 /* Host Unsupported Protocol */
68 #define SMB_STS_B 0x1A /* Busy */
69 #define SMB_STS_PEC 0x1F /* PEC (CRC-8) Error */
71 #define SMB_PRTCL_WRITE 0x00
72 #define SMB_PRTCL_READ 0x01
73 #define SMB_PRTCL_QUICK 0x02
74 #define SMB_PRTCL_BYTE 0x04
75 #define SMB_PRTCL_BYTE_DATA 0x06
76 #define SMB_PRTCL_WORD_DATA 0x08
77 #define SMB_PRTCL_BLOCK_DATA 0x0a
78 #define SMB_PRTCL_PROC_CALL 0x0c
79 #define SMB_PRTCL_BLOCK_PROC_CALL 0x0d
80 #define SMB_PRTCL_PEC 0x80
85 bus_space_tag_t smbst;
86 bus_space_handle_t smbsh;
92 #define NFSMB_SMBINB(nfsmb, register) \
93 (bus_space_read_1(nfsmb->smbst, nfsmb->smbsh, register))
94 #define NFSMB_SMBOUTB(nfsmb, register, value) \
95 (bus_space_write_1(nfsmb->smbst, nfsmb->smbsh, register, value))
98 nfsmbsub_probe(device_t dev)
101 device_set_desc(dev, "nForce2/3/4 MCP SMBus Controller");
102 return (BUS_PROBE_DEFAULT);
106 nfsmb_probe(device_t dev)
111 vid = pci_get_vendor(dev);
112 did = pci_get_device(dev);
114 if (vid == NFSMB_VENDORID_NVIDIA) {
116 case NFSMB_DEVICEID_NF2_SMB:
117 case NFSMB_DEVICEID_NF2_ULTRA_SMB:
118 case NFSMB_DEVICEID_NF3_PRO150_SMB:
119 case NFSMB_DEVICEID_NF3_250GB_SMB:
120 case NFSMB_DEVICEID_NF4_SMB:
121 device_set_desc(dev, "nForce2/3/4 MCP SMBus Controller");
122 return (BUS_PROBE_DEFAULT);
130 nfsmbsub_attach(device_t dev)
133 struct nfsmb_softc *nfsmbsub_sc = device_get_softc(dev);
135 parent = device_get_parent(dev);
137 nfsmbsub_sc->rid = NF2PCI_SMBASE_2;
139 nfsmbsub_sc->res = bus_alloc_resource_any(parent, SYS_RES_IOPORT,
140 &nfsmbsub_sc->rid, RF_ACTIVE);
141 if (nfsmbsub_sc->res == NULL) {
142 device_printf(dev, "could not map i/o space\n");
145 nfsmbsub_sc->smbst = rman_get_bustag(nfsmbsub_sc->res);
146 nfsmbsub_sc->smbsh = rman_get_bushandle(nfsmbsub_sc->res);
148 nfsmbsub_sc->smbus = device_add_child(dev, "smbus", -1);
149 if (nfsmbsub_sc->smbus == NULL)
152 bus_generic_attach(dev);
158 nfsmb_attach(device_t dev)
160 struct nfsmb_softc *nfsmb_sc = device_get_softc(dev);
162 /* Allocate I/O space */
163 nfsmb_sc->rid = NF2PCI_SMBASE_1;
165 nfsmb_sc->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
166 &nfsmb_sc->rid, RF_ACTIVE);
168 if (nfsmb_sc->res == NULL) {
169 device_printf(dev, "could not map i/o space\n");
173 nfsmb_sc->smbst = rman_get_bustag(nfsmb_sc->res);
174 nfsmb_sc->smbsh = rman_get_bushandle(nfsmb_sc->res);
176 /* Allocate a new smbus device */
177 nfsmb_sc->smbus = device_add_child(dev, "smbus", -1);
178 if (!nfsmb_sc->smbus)
181 nfsmb_sc->subdev = NULL;
182 switch (pci_get_device(dev)) {
183 case NFSMB_DEVICEID_NF2_SMB:
184 case NFSMB_DEVICEID_NF2_ULTRA_SMB:
185 case NFSMB_DEVICEID_NF3_PRO150_SMB:
186 case NFSMB_DEVICEID_NF3_250GB_SMB:
187 case NFSMB_DEVICEID_NF4_SMB:
188 /* Trying to add secondary device as slave */
189 nfsmb_sc->subdev = device_add_child(dev, "nfsmb", -1);
190 if (!nfsmb_sc->subdev)
197 bus_generic_attach(dev);
203 nfsmbsub_detach(device_t dev)
206 struct nfsmb_softc *nfsmbsub_sc = device_get_softc(dev);
208 parent = device_get_parent(dev);
210 if (nfsmbsub_sc->smbus) {
211 device_delete_child(dev, nfsmbsub_sc->smbus);
212 nfsmbsub_sc->smbus = NULL;
214 if (nfsmbsub_sc->res) {
215 bus_release_resource(parent, SYS_RES_IOPORT, nfsmbsub_sc->rid,
217 nfsmbsub_sc->res = NULL;
223 nfsmb_detach(device_t dev)
225 struct nfsmb_softc *nfsmb_sc = device_get_softc(dev);
227 if (nfsmb_sc->subdev) {
228 device_delete_child(dev, nfsmb_sc->subdev);
229 nfsmb_sc->subdev = NULL;
232 if (nfsmb_sc->smbus) {
233 device_delete_child(dev, nfsmb_sc->smbus);
234 nfsmb_sc->smbus = NULL;
238 bus_release_resource(dev, SYS_RES_IOPORT, nfsmb_sc->rid,
240 nfsmb_sc->res = NULL;
247 nfsmb_callback(device_t dev, int index, caddr_t *data)
252 case SMB_REQUEST_BUS:
253 case SMB_RELEASE_BUS:
263 nfsmb_wait(struct nfsmb_softc *sc)
268 if (NFSMB_SMBINB(sc, SMB_PRTCL) != 0)
273 } while (NFSMB_SMBINB(sc, SMB_PRTCL) != 0 && count--);
275 return (SMB_ETIMEOUT);
278 sts = NFSMB_SMBINB(sc, SMB_STS) & SMB_STS_STATUS;
279 NFSMB_DEBUG(printf("nfsmb: STS=0x%x\n", sts));
292 error = SMB_ETIMEOUT;
297 error = SMB_ENOTSUPP;
308 nfsmb_quick(device_t dev, u_char slave, int how)
310 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
314 protocol = SMB_PRTCL_QUICK;
318 protocol |= SMB_PRTCL_WRITE;
319 NFSMB_DEBUG(printf("nfsmb: QWRITE to 0x%x", slave));
322 protocol |= SMB_PRTCL_READ;
323 NFSMB_DEBUG(printf("nfsmb: QREAD to 0x%x", slave));
326 panic("%s: unknown QUICK command (%x)!", __func__, how);
329 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
330 NFSMB_SMBOUTB(sc, SMB_PRTCL, protocol);
332 error = nfsmb_wait(sc);
334 NFSMB_DEBUG(printf(", error=0x%x\n", error));
340 nfsmb_sendb(device_t dev, u_char slave, char byte)
342 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
345 NFSMB_SMBOUTB(sc, SMB_CMD, byte);
346 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
347 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BYTE);
349 error = nfsmb_wait(sc);
351 NFSMB_DEBUG(printf("nfsmb: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
357 nfsmb_recvb(device_t dev, u_char slave, char *byte)
359 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
362 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
363 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BYTE);
365 if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
366 *byte = NFSMB_SMBINB(sc, SMB_DATA);
368 NFSMB_DEBUG(printf("nfsmb: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
374 nfsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
376 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
379 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
380 NFSMB_SMBOUTB(sc, SMB_DATA, byte);
381 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
382 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BYTE_DATA);
384 error = nfsmb_wait(sc);
386 NFSMB_DEBUG(printf("nfsmb: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
392 nfsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
394 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
397 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
398 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
399 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BYTE_DATA);
401 if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
402 *byte = NFSMB_SMBINB(sc, SMB_DATA);
404 NFSMB_DEBUG(printf("nfsmb: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, (unsigned char)*byte, error));
410 nfsmb_writew(device_t dev, u_char slave, char cmd, short word)
412 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
415 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
416 NFSMB_SMBOUTB(sc, SMB_DATA, word);
417 NFSMB_SMBOUTB(sc, SMB_DATA + 1, word >> 8);
418 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
419 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_WORD_DATA);
421 error = nfsmb_wait(sc);
423 NFSMB_DEBUG(printf("nfsmb: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
429 nfsmb_readw(device_t dev, u_char slave, char cmd, short *word)
431 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
434 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
435 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
436 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_WORD_DATA);
438 if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
439 *word = NFSMB_SMBINB(sc, SMB_DATA) |
440 (NFSMB_SMBINB(sc, SMB_DATA + 1) << 8);
442 NFSMB_DEBUG(printf("nfsmb: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, (unsigned short)*word, error));
448 nfsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
450 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
454 len = min(count, 32);
455 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
456 NFSMB_SMBOUTB(sc, SMB_BCNT, len);
457 for (i = 0; i < len; i++)
458 NFSMB_SMBOUTB(sc, SMB_DATA + i, buf[i]);
459 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
460 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BLOCK_DATA);
462 error = nfsmb_wait(sc);
464 NFSMB_DEBUG(printf("nfsmb: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
470 nfsmb_bread(device_t dev, u_char slave, char cmd, u_char count, char *buf)
472 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
476 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
477 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
478 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BLOCK_DATA);
480 if ((error = nfsmb_wait(sc)) == SMB_ENOERR) {
481 len = NFSMB_SMBINB(sc, SMB_BCNT);
483 for (i = 0; i < len; i++)
484 buf[i] = NFSMB_SMBINB(sc, SMB_DATA + i);
487 NFSMB_DEBUG(printf("nfsmb: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
492 static device_method_t nfsmb_methods[] = {
493 /* Device interface */
494 DEVMETHOD(device_probe, nfsmb_probe),
495 DEVMETHOD(device_attach, nfsmb_attach),
496 DEVMETHOD(device_detach, nfsmb_detach),
498 /* SMBus interface */
499 DEVMETHOD(smbus_callback, nfsmb_callback),
500 DEVMETHOD(smbus_quick, nfsmb_quick),
501 DEVMETHOD(smbus_sendb, nfsmb_sendb),
502 DEVMETHOD(smbus_recvb, nfsmb_recvb),
503 DEVMETHOD(smbus_writeb, nfsmb_writeb),
504 DEVMETHOD(smbus_readb, nfsmb_readb),
505 DEVMETHOD(smbus_writew, nfsmb_writew),
506 DEVMETHOD(smbus_readw, nfsmb_readw),
507 DEVMETHOD(smbus_bwrite, nfsmb_bwrite),
508 DEVMETHOD(smbus_bread, nfsmb_bread),
513 static device_method_t nfsmbsub_methods[] = {
514 /* Device interface */
515 DEVMETHOD(device_probe, nfsmbsub_probe),
516 DEVMETHOD(device_attach, nfsmbsub_attach),
517 DEVMETHOD(device_detach, nfsmbsub_detach),
519 /* SMBus interface */
520 DEVMETHOD(smbus_callback, nfsmb_callback),
521 DEVMETHOD(smbus_quick, nfsmb_quick),
522 DEVMETHOD(smbus_sendb, nfsmb_sendb),
523 DEVMETHOD(smbus_recvb, nfsmb_recvb),
524 DEVMETHOD(smbus_writeb, nfsmb_writeb),
525 DEVMETHOD(smbus_readb, nfsmb_readb),
526 DEVMETHOD(smbus_writew, nfsmb_writew),
527 DEVMETHOD(smbus_readw, nfsmb_readw),
528 DEVMETHOD(smbus_bwrite, nfsmb_bwrite),
529 DEVMETHOD(smbus_bread, nfsmb_bread),
534 static devclass_t nfsmb_devclass;
536 static driver_t nfsmb_driver = {
539 sizeof(struct nfsmb_softc),
542 static driver_t nfsmbsub_driver = {
545 sizeof(struct nfsmb_softc),
548 DRIVER_MODULE(nfsmb, pci, nfsmb_driver, nfsmb_devclass, 0, 0);
549 DRIVER_MODULE(nfsmb, nfsmb, nfsmbsub_driver, nfsmb_devclass, 0, 0);
551 MODULE_DEPEND(nfsmb, pci, 1, 1, 1);
552 MODULE_DEPEND(nfsmb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
553 MODULE_VERSION(nfsmb, 1);