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 NFSMB_DEBUG(x) if (nfsmb_debug) (x)
25 static int nfsmb_debug = 1;
27 static int nfsmb_debug = 0;
30 /* NVIDIA nForce2/3/4 MCP */
31 #define NFSMB_VENDORID_NVIDIA 0x10de
32 #define NFSMB_DEVICEID_NF2_SMB 0x0064
33 #define NFSMB_DEVICEID_NF2_ULTRA_SMB 0x0084
34 #define NFSMB_DEVICEID_NF3_PRO150_SMB 0x00d4
35 #define NFSMB_DEVICEID_NF3_250GB_SMB 0x00e4
36 #define NFSMB_DEVICEID_NF4_SMB 0x0052
38 /* PCI Configuration space registers */
39 #define NF2PCI_SMBASE_1 PCIR_BAR(4)
40 #define NF2PCI_SMBASE_2 PCIR_BAR(5)
43 * ACPI 3.0, Chapter 12, SMBus Host Controller Interface.
45 #define SMB_PRTCL 0x00 /* protocol */
46 #define SMB_STS 0x01 /* status */
47 #define SMB_ADDR 0x02 /* address */
48 #define SMB_CMD 0x03 /* command */
49 #define SMB_DATA 0x04 /* 32 data registers */
50 #define SMB_BCNT 0x24 /* number of data bytes */
51 #define SMB_ALRM_A 0x25 /* alarm address */
52 #define SMB_ALRM_D 0x26 /* 2 bytes alarm data */
54 #define SMB_STS_DONE 0x80
55 #define SMB_STS_ALRM 0x40
56 #define SMB_STS_RES 0x20
57 #define SMB_STS_STATUS 0x1f
58 #define SMB_STS_OK 0x00 /* OK */
59 #define SMB_STS_UF 0x07 /* Unknown Failure */
60 #define SMB_STS_DANA 0x10 /* Device Address Not Acknowledged */
61 #define SMB_STS_DED 0x11 /* Device Error Detected */
62 #define SMB_STS_DCAD 0x12 /* Device Command Access Denied */
63 #define SMB_STS_UE 0x13 /* Unknown Error */
64 #define SMB_STS_DAD 0x17 /* Device Access Denied */
65 #define SMB_STS_T 0x18 /* Timeout */
66 #define SMB_STS_HUP 0x19 /* Host Unsupported Protocol */
67 #define SMB_STS_B 0x1A /* Busy */
68 #define SMB_STS_PEC 0x1F /* PEC (CRC-8) Error */
70 #define SMB_PRTCL_WRITE 0x00
71 #define SMB_PRTCL_READ 0x01
72 #define SMB_PRTCL_QUICK 0x02
73 #define SMB_PRTCL_BYTE 0x04
74 #define SMB_PRTCL_BYTE_DATA 0x06
75 #define SMB_PRTCL_WORD_DATA 0x08
76 #define SMB_PRTCL_BLOCK_DATA 0x0a
77 #define SMB_PRTCL_PROC_CALL 0x0c
78 #define SMB_PRTCL_BLOCK_PROC_CALL 0x0d
79 #define SMB_PRTCL_PEC 0x80
84 bus_space_tag_t smbst;
85 bus_space_handle_t smbsh;
91 #define NFSMB_SMBINB(nfsmb, register) \
92 (bus_space_read_1(nfsmb->smbst, nfsmb->smbsh, register))
93 #define NFSMB_SMBOUTB(nfsmb, register, value) \
94 (bus_space_write_1(nfsmb->smbst, nfsmb->smbsh, register, value))
97 nfsmbsub_probe(device_t dev)
100 device_set_desc(dev, "nForce2/3/4 MCP SMBus Controller");
101 return (BUS_PROBE_DEFAULT);
105 nfsmb_probe(device_t dev)
110 vid = pci_get_vendor(dev);
111 did = pci_get_device(dev);
113 if (vid == NFSMB_VENDORID_NVIDIA) {
115 case NFSMB_DEVICEID_NF2_SMB:
116 case NFSMB_DEVICEID_NF2_ULTRA_SMB:
117 case NFSMB_DEVICEID_NF3_PRO150_SMB:
118 case NFSMB_DEVICEID_NF3_250GB_SMB:
119 case NFSMB_DEVICEID_NF4_SMB:
120 device_set_desc(dev, "nForce2/3/4 MCP SMBus Controller");
121 return (BUS_PROBE_DEFAULT);
129 nfsmbsub_attach(device_t dev)
132 struct nfsmb_softc *nfsmbsub_sc = device_get_softc(dev);
134 parent = device_get_parent(dev);
136 nfsmbsub_sc->rid = NF2PCI_SMBASE_2;
138 nfsmbsub_sc->res = bus_alloc_resource_any(parent, SYS_RES_IOPORT,
139 &nfsmbsub_sc->rid, RF_ACTIVE);
140 if (nfsmbsub_sc->res == NULL) {
141 device_printf(dev, "could not map i/o space\n");
144 nfsmbsub_sc->smbst = rman_get_bustag(nfsmbsub_sc->res);
145 nfsmbsub_sc->smbsh = rman_get_bushandle(nfsmbsub_sc->res);
147 nfsmbsub_sc->smbus = device_add_child(dev, "smbus", -1);
148 if (nfsmbsub_sc->smbus == NULL)
151 bus_generic_attach(dev);
157 nfsmb_attach(device_t dev)
159 struct nfsmb_softc *nfsmb_sc = device_get_softc(dev);
161 /* Allocate I/O space */
162 nfsmb_sc->rid = NF2PCI_SMBASE_1;
164 nfsmb_sc->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
165 &nfsmb_sc->rid, RF_ACTIVE);
167 if (nfsmb_sc->res == NULL) {
168 device_printf(dev, "could not map i/o space\n");
172 nfsmb_sc->smbst = rman_get_bustag(nfsmb_sc->res);
173 nfsmb_sc->smbsh = rman_get_bushandle(nfsmb_sc->res);
175 /* Allocate a new smbus device */
176 nfsmb_sc->smbus = device_add_child(dev, "smbus", -1);
177 if (!nfsmb_sc->smbus)
180 nfsmb_sc->subdev = NULL;
181 switch (pci_get_device(dev)) {
182 case NFSMB_DEVICEID_NF2_SMB:
183 case NFSMB_DEVICEID_NF2_ULTRA_SMB:
184 case NFSMB_DEVICEID_NF3_PRO150_SMB:
185 case NFSMB_DEVICEID_NF3_250GB_SMB:
186 case NFSMB_DEVICEID_NF4_SMB:
187 /* Trying to add secondary device as slave */
188 nfsmb_sc->subdev = device_add_child(dev, "nfsmb", -1);
189 if (!nfsmb_sc->subdev)
196 bus_generic_attach(dev);
202 nfsmbsub_detach(device_t dev)
205 struct nfsmb_softc *nfsmbsub_sc = device_get_softc(dev);
207 parent = device_get_parent(dev);
209 if (nfsmbsub_sc->smbus) {
210 device_delete_child(dev, nfsmbsub_sc->smbus);
211 nfsmbsub_sc->smbus = NULL;
213 if (nfsmbsub_sc->res) {
214 bus_release_resource(parent, SYS_RES_IOPORT, nfsmbsub_sc->rid,
216 nfsmbsub_sc->res = NULL;
222 nfsmb_detach(device_t dev)
224 struct nfsmb_softc *nfsmb_sc = device_get_softc(dev);
226 if (nfsmb_sc->subdev) {
227 device_delete_child(dev, nfsmb_sc->subdev);
228 nfsmb_sc->subdev = NULL;
231 if (nfsmb_sc->smbus) {
232 device_delete_child(dev, nfsmb_sc->smbus);
233 nfsmb_sc->smbus = NULL;
237 bus_release_resource(dev, SYS_RES_IOPORT, nfsmb_sc->rid,
239 nfsmb_sc->res = NULL;
246 nfsmb_callback(device_t dev, int index, caddr_t *data)
251 case SMB_REQUEST_BUS:
252 case SMB_RELEASE_BUS:
262 nfsmb_wait(struct nfsmb_softc *sc)
267 if (NFSMB_SMBINB(sc, SMB_PRTCL) != 0)
272 } while (NFSMB_SMBINB(sc, SMB_PRTCL) != 0 && count--);
274 return (SMB_ETIMEOUT);
277 sts = NFSMB_SMBINB(sc, SMB_STS) & SMB_STS_STATUS;
278 NFSMB_DEBUG(printf("nfsmb: STS=0x%x\n", sts));
291 error = SMB_ETIMEOUT;
296 error = SMB_ENOTSUPP;
307 nfsmb_quick(device_t dev, u_char slave, int how)
309 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
313 protocol = SMB_PRTCL_QUICK;
317 protocol |= SMB_PRTCL_WRITE;
318 NFSMB_DEBUG(printf("nfsmb: QWRITE to 0x%x", slave));
321 protocol |= SMB_PRTCL_READ;
322 NFSMB_DEBUG(printf("nfsmb: QREAD to 0x%x", slave));
325 panic("%s: unknown QUICK command (%x)!", __func__, how);
328 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
329 NFSMB_SMBOUTB(sc, SMB_PRTCL, protocol);
331 error = nfsmb_wait(sc);
333 NFSMB_DEBUG(printf(", error=0x%x\n", error));
339 nfsmb_sendb(device_t dev, u_char slave, char byte)
341 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
344 NFSMB_SMBOUTB(sc, SMB_CMD, byte);
345 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
346 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BYTE);
348 error = nfsmb_wait(sc);
350 NFSMB_DEBUG(printf("nfsmb: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
356 nfsmb_recvb(device_t dev, u_char slave, char *byte)
358 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
361 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
362 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BYTE);
364 if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
365 *byte = NFSMB_SMBINB(sc, SMB_DATA);
367 NFSMB_DEBUG(printf("nfsmb: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
373 nfsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
375 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
378 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
379 NFSMB_SMBOUTB(sc, SMB_DATA, byte);
380 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
381 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BYTE_DATA);
383 error = nfsmb_wait(sc);
385 NFSMB_DEBUG(printf("nfsmb: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
391 nfsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
393 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
396 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
397 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
398 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BYTE_DATA);
400 if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
401 *byte = NFSMB_SMBINB(sc, SMB_DATA);
403 NFSMB_DEBUG(printf("nfsmb: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, (unsigned char)*byte, error));
409 nfsmb_writew(device_t dev, u_char slave, char cmd, short word)
411 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
414 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
415 NFSMB_SMBOUTB(sc, SMB_DATA, word);
416 NFSMB_SMBOUTB(sc, SMB_DATA + 1, word >> 8);
417 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
418 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_WORD_DATA);
420 error = nfsmb_wait(sc);
422 NFSMB_DEBUG(printf("nfsmb: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
428 nfsmb_readw(device_t dev, u_char slave, char cmd, short *word)
430 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
433 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
434 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
435 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_WORD_DATA);
437 if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
438 *word = NFSMB_SMBINB(sc, SMB_DATA) |
439 (NFSMB_SMBINB(sc, SMB_DATA + 1) << 8);
441 NFSMB_DEBUG(printf("nfsmb: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, (unsigned short)*word, error));
447 nfsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
449 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
453 len = min(count, 32);
454 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
455 NFSMB_SMBOUTB(sc, SMB_BCNT, len);
456 for (i = 0; i < len; i++)
457 NFSMB_SMBOUTB(sc, SMB_DATA + i, buf[i]);
458 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
459 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BLOCK_DATA);
461 error = nfsmb_wait(sc);
463 NFSMB_DEBUG(printf("nfsmb: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
469 nfsmb_bread(device_t dev, u_char slave, char cmd, u_char count, char *buf)
471 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
475 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
476 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
477 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BLOCK_DATA);
479 if ((error = nfsmb_wait(sc)) == SMB_ENOERR) {
480 len = NFSMB_SMBINB(sc, SMB_BCNT);
482 for (i = 0; i < len; i++)
483 buf[i] = NFSMB_SMBINB(sc, SMB_DATA + i);
486 NFSMB_DEBUG(printf("nfsmb: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
491 static device_method_t nfsmb_methods[] = {
492 /* Device interface */
493 DEVMETHOD(device_probe, nfsmb_probe),
494 DEVMETHOD(device_attach, nfsmb_attach),
495 DEVMETHOD(device_detach, nfsmb_detach),
497 /* SMBus interface */
498 DEVMETHOD(smbus_callback, nfsmb_callback),
499 DEVMETHOD(smbus_quick, nfsmb_quick),
500 DEVMETHOD(smbus_sendb, nfsmb_sendb),
501 DEVMETHOD(smbus_recvb, nfsmb_recvb),
502 DEVMETHOD(smbus_writeb, nfsmb_writeb),
503 DEVMETHOD(smbus_readb, nfsmb_readb),
504 DEVMETHOD(smbus_writew, nfsmb_writew),
505 DEVMETHOD(smbus_readw, nfsmb_readw),
506 DEVMETHOD(smbus_bwrite, nfsmb_bwrite),
507 DEVMETHOD(smbus_bread, nfsmb_bread),
512 static device_method_t nfsmbsub_methods[] = {
513 /* Device interface */
514 DEVMETHOD(device_probe, nfsmbsub_probe),
515 DEVMETHOD(device_attach, nfsmbsub_attach),
516 DEVMETHOD(device_detach, nfsmbsub_detach),
518 /* SMBus interface */
519 DEVMETHOD(smbus_callback, nfsmb_callback),
520 DEVMETHOD(smbus_quick, nfsmb_quick),
521 DEVMETHOD(smbus_sendb, nfsmb_sendb),
522 DEVMETHOD(smbus_recvb, nfsmb_recvb),
523 DEVMETHOD(smbus_writeb, nfsmb_writeb),
524 DEVMETHOD(smbus_readb, nfsmb_readb),
525 DEVMETHOD(smbus_writew, nfsmb_writew),
526 DEVMETHOD(smbus_readw, nfsmb_readw),
527 DEVMETHOD(smbus_bwrite, nfsmb_bwrite),
528 DEVMETHOD(smbus_bread, nfsmb_bread),
533 static devclass_t nfsmb_devclass;
535 static driver_t nfsmb_driver = {
538 sizeof(struct nfsmb_softc),
541 static driver_t nfsmbsub_driver = {
544 sizeof(struct nfsmb_softc),
547 DRIVER_MODULE(nfsmb, pci, nfsmb_driver, nfsmb_devclass, 0, 0);
548 DRIVER_MODULE(nfsmb, nfsmb, nfsmbsub_driver, nfsmb_devclass, 0, 0);
550 MODULE_DEPEND(nfsmb, pci, 1, 1, 1);
551 MODULE_DEPEND(nfsmb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
552 MODULE_VERSION(nfsmb, 1);