2 * Copyright (c) 2005 Ruslan Ermilov
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/systm.h>
38 #include <machine/bus.h>
39 #include <machine/resource.h>
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcireg.h>
45 #include <dev/smbus/smbconf.h>
48 #define NFSMB_DEBUG(x) if (nfsmb_debug) (x)
51 static int nfsmb_debug = 1;
53 static int nfsmb_debug = 0;
56 /* NVIDIA nForce2/3/4 MCP */
57 #define NFSMB_VENDORID_NVIDIA 0x10de
58 #define NFSMB_DEVICEID_NF2_SMB 0x0064
59 #define NFSMB_DEVICEID_NF2_ULTRA_SMB 0x0084
60 #define NFSMB_DEVICEID_NF3_PRO150_SMB 0x00d4
61 #define NFSMB_DEVICEID_NF3_250GB_SMB 0x00e4
62 #define NFSMB_DEVICEID_NF4_SMB 0x0052
63 #define NFSMB_DEVICEID_NF4_04_SMB 0x0034
64 #define NFSMB_DEVICEID_NF4_51_SMB 0x0264
65 #define NFSMB_DEVICEID_NF4_55_SMB 0x0368
66 #define NFSMB_DEVICEID_NF4_61_SMB 0x03eb
67 #define NFSMB_DEVICEID_NF4_65_SMB 0x0446
68 #define NFSMB_DEVICEID_NF4_67_SMB 0x0542
69 #define NFSMB_DEVICEID_NF4_73_SMB 0x07d8
70 #define NFSMB_DEVICEID_NF4_78S_SMB 0x0752
71 #define NFSMB_DEVICEID_NF4_79_SMB 0x0aa2
73 /* PCI Configuration space registers */
74 #define NF2PCI_SMBASE_1 PCIR_BAR(4)
75 #define NF2PCI_SMBASE_2 PCIR_BAR(5)
78 * ACPI 3.0, Chapter 12, SMBus Host Controller Interface.
80 #define SMB_PRTCL 0x00 /* protocol */
81 #define SMB_STS 0x01 /* status */
82 #define SMB_ADDR 0x02 /* address */
83 #define SMB_CMD 0x03 /* command */
84 #define SMB_DATA 0x04 /* 32 data registers */
85 #define SMB_BCNT 0x24 /* number of data bytes */
86 #define SMB_ALRM_A 0x25 /* alarm address */
87 #define SMB_ALRM_D 0x26 /* 2 bytes alarm data */
89 #define SMB_STS_DONE 0x80
90 #define SMB_STS_ALRM 0x40
91 #define SMB_STS_RES 0x20
92 #define SMB_STS_STATUS 0x1f
93 #define SMB_STS_OK 0x00 /* OK */
94 #define SMB_STS_UF 0x07 /* Unknown Failure */
95 #define SMB_STS_DANA 0x10 /* Device Address Not Acknowledged */
96 #define SMB_STS_DED 0x11 /* Device Error Detected */
97 #define SMB_STS_DCAD 0x12 /* Device Command Access Denied */
98 #define SMB_STS_UE 0x13 /* Unknown Error */
99 #define SMB_STS_DAD 0x17 /* Device Access Denied */
100 #define SMB_STS_T 0x18 /* Timeout */
101 #define SMB_STS_HUP 0x19 /* Host Unsupported Protocol */
102 #define SMB_STS_B 0x1A /* Busy */
103 #define SMB_STS_PEC 0x1F /* PEC (CRC-8) Error */
105 #define SMB_PRTCL_WRITE 0x00
106 #define SMB_PRTCL_READ 0x01
107 #define SMB_PRTCL_QUICK 0x02
108 #define SMB_PRTCL_BYTE 0x04
109 #define SMB_PRTCL_BYTE_DATA 0x06
110 #define SMB_PRTCL_WORD_DATA 0x08
111 #define SMB_PRTCL_BLOCK_DATA 0x0a
112 #define SMB_PRTCL_PROC_CALL 0x0c
113 #define SMB_PRTCL_BLOCK_PROC_CALL 0x0d
114 #define SMB_PRTCL_PEC 0x80
118 struct resource *res;
124 #define NFSMB_LOCK(nfsmb) mtx_lock(&(nfsmb)->lock)
125 #define NFSMB_UNLOCK(nfsmb) mtx_unlock(&(nfsmb)->lock)
126 #define NFSMB_LOCK_ASSERT(nfsmb) mtx_assert(&(nfsmb)->lock, MA_OWNED)
128 #define NFSMB_SMBINB(nfsmb, register) \
129 (bus_read_1(nfsmb->res, register))
130 #define NFSMB_SMBOUTB(nfsmb, register, value) \
131 (bus_write_1(nfsmb->res, register, value))
133 static int nfsmb_detach(device_t dev);
134 static int nfsmbsub_detach(device_t dev);
137 nfsmbsub_probe(device_t dev)
140 device_set_desc(dev, "nForce2/3/4 MCP SMBus Controller");
141 return (BUS_PROBE_DEFAULT);
145 nfsmb_probe(device_t dev)
150 vid = pci_get_vendor(dev);
151 did = pci_get_device(dev);
153 if (vid == NFSMB_VENDORID_NVIDIA) {
155 case NFSMB_DEVICEID_NF2_SMB:
156 case NFSMB_DEVICEID_NF2_ULTRA_SMB:
157 case NFSMB_DEVICEID_NF3_PRO150_SMB:
158 case NFSMB_DEVICEID_NF3_250GB_SMB:
159 case NFSMB_DEVICEID_NF4_SMB:
160 case NFSMB_DEVICEID_NF4_04_SMB:
161 case NFSMB_DEVICEID_NF4_51_SMB:
162 case NFSMB_DEVICEID_NF4_55_SMB:
163 case NFSMB_DEVICEID_NF4_61_SMB:
164 case NFSMB_DEVICEID_NF4_65_SMB:
165 case NFSMB_DEVICEID_NF4_67_SMB:
166 case NFSMB_DEVICEID_NF4_73_SMB:
167 case NFSMB_DEVICEID_NF4_78S_SMB:
168 case NFSMB_DEVICEID_NF4_79_SMB:
169 device_set_desc(dev, "nForce2/3/4 MCP SMBus Controller");
170 return (BUS_PROBE_DEFAULT);
178 nfsmbsub_attach(device_t dev)
181 struct nfsmb_softc *nfsmbsub_sc = device_get_softc(dev);
183 parent = device_get_parent(dev);
185 nfsmbsub_sc->rid = NF2PCI_SMBASE_2;
187 nfsmbsub_sc->res = bus_alloc_resource_any(parent, SYS_RES_IOPORT,
188 &nfsmbsub_sc->rid, RF_ACTIVE);
189 if (nfsmbsub_sc->res == NULL) {
190 /* Older incarnations of the device used non-standard BARs. */
191 nfsmbsub_sc->rid = 0x54;
192 nfsmbsub_sc->res = bus_alloc_resource_any(parent,
193 SYS_RES_IOPORT, &nfsmbsub_sc->rid, RF_ACTIVE);
194 if (nfsmbsub_sc->res == NULL) {
195 device_printf(dev, "could not map i/o space\n");
199 mtx_init(&nfsmbsub_sc->lock, device_get_nameunit(dev), "nfsmb",
202 nfsmbsub_sc->smbus = device_add_child(dev, "smbus", -1);
203 if (nfsmbsub_sc->smbus == NULL) {
204 nfsmbsub_detach(dev);
208 bus_generic_attach(dev);
214 nfsmb_attach(device_t dev)
216 struct nfsmb_softc *nfsmb_sc = device_get_softc(dev);
218 /* Allocate I/O space */
219 nfsmb_sc->rid = NF2PCI_SMBASE_1;
221 nfsmb_sc->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
222 &nfsmb_sc->rid, RF_ACTIVE);
224 if (nfsmb_sc->res == NULL) {
225 /* Older incarnations of the device used non-standard BARs. */
226 nfsmb_sc->rid = 0x50;
227 nfsmb_sc->res = bus_alloc_resource_any(dev,
228 SYS_RES_IOPORT, &nfsmb_sc->rid, RF_ACTIVE);
229 if (nfsmb_sc->res == NULL) {
230 device_printf(dev, "could not map i/o space\n");
235 mtx_init(&nfsmb_sc->lock, device_get_nameunit(dev), "nfsmb", MTX_DEF);
237 /* Allocate a new smbus device */
238 nfsmb_sc->smbus = device_add_child(dev, "smbus", -1);
239 if (!nfsmb_sc->smbus) {
244 nfsmb_sc->subdev = NULL;
245 switch (pci_get_device(dev)) {
246 case NFSMB_DEVICEID_NF2_SMB:
247 case NFSMB_DEVICEID_NF2_ULTRA_SMB:
248 case NFSMB_DEVICEID_NF3_PRO150_SMB:
249 case NFSMB_DEVICEID_NF3_250GB_SMB:
250 case NFSMB_DEVICEID_NF4_SMB:
251 case NFSMB_DEVICEID_NF4_04_SMB:
252 case NFSMB_DEVICEID_NF4_51_SMB:
253 case NFSMB_DEVICEID_NF4_55_SMB:
254 case NFSMB_DEVICEID_NF4_61_SMB:
255 case NFSMB_DEVICEID_NF4_65_SMB:
256 case NFSMB_DEVICEID_NF4_67_SMB:
257 case NFSMB_DEVICEID_NF4_73_SMB:
258 case NFSMB_DEVICEID_NF4_78S_SMB:
259 case NFSMB_DEVICEID_NF4_79_SMB:
260 /* Trying to add secondary device as slave */
261 nfsmb_sc->subdev = device_add_child(dev, "nfsmb", -1);
262 if (!nfsmb_sc->subdev) {
271 bus_generic_attach(dev);
277 nfsmbsub_detach(device_t dev)
280 struct nfsmb_softc *nfsmbsub_sc = device_get_softc(dev);
282 parent = device_get_parent(dev);
284 if (nfsmbsub_sc->smbus) {
285 device_delete_child(dev, nfsmbsub_sc->smbus);
286 nfsmbsub_sc->smbus = NULL;
288 mtx_destroy(&nfsmbsub_sc->lock);
289 if (nfsmbsub_sc->res) {
290 bus_release_resource(parent, SYS_RES_IOPORT, nfsmbsub_sc->rid,
292 nfsmbsub_sc->res = NULL;
298 nfsmb_detach(device_t dev)
300 struct nfsmb_softc *nfsmb_sc = device_get_softc(dev);
302 if (nfsmb_sc->subdev) {
303 device_delete_child(dev, nfsmb_sc->subdev);
304 nfsmb_sc->subdev = NULL;
307 if (nfsmb_sc->smbus) {
308 device_delete_child(dev, nfsmb_sc->smbus);
309 nfsmb_sc->smbus = NULL;
312 mtx_destroy(&nfsmb_sc->lock);
314 bus_release_resource(dev, SYS_RES_IOPORT, nfsmb_sc->rid,
316 nfsmb_sc->res = NULL;
323 nfsmb_callback(device_t dev, int index, void *data)
328 case SMB_REQUEST_BUS:
329 case SMB_RELEASE_BUS:
339 nfsmb_wait(struct nfsmb_softc *sc)
344 NFSMB_LOCK_ASSERT(sc);
345 if (NFSMB_SMBINB(sc, SMB_PRTCL) != 0)
350 } while (NFSMB_SMBINB(sc, SMB_PRTCL) != 0 && count--);
352 return (SMB_ETIMEOUT);
355 sts = NFSMB_SMBINB(sc, SMB_STS) & SMB_STS_STATUS;
356 NFSMB_DEBUG(printf("nfsmb: STS=0x%x\n", sts));
369 error = SMB_ETIMEOUT;
374 error = SMB_ENOTSUPP;
385 nfsmb_quick(device_t dev, u_char slave, int how)
387 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
391 protocol = SMB_PRTCL_QUICK;
395 protocol |= SMB_PRTCL_WRITE;
396 NFSMB_DEBUG(printf("nfsmb: QWRITE to 0x%x", slave));
399 protocol |= SMB_PRTCL_READ;
400 NFSMB_DEBUG(printf("nfsmb: QREAD to 0x%x", slave));
403 panic("%s: unknown QUICK command (%x)!", __func__, how);
407 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
408 NFSMB_SMBOUTB(sc, SMB_PRTCL, protocol);
410 error = nfsmb_wait(sc);
412 NFSMB_DEBUG(printf(", error=0x%x\n", error));
419 nfsmb_sendb(device_t dev, u_char slave, char byte)
421 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
425 NFSMB_SMBOUTB(sc, SMB_CMD, byte);
426 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
427 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BYTE);
429 error = nfsmb_wait(sc);
431 NFSMB_DEBUG(printf("nfsmb: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
438 nfsmb_recvb(device_t dev, u_char slave, char *byte)
440 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
444 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
445 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BYTE);
447 if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
448 *byte = NFSMB_SMBINB(sc, SMB_DATA);
450 NFSMB_DEBUG(printf("nfsmb: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
457 nfsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
459 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
463 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
464 NFSMB_SMBOUTB(sc, SMB_DATA, byte);
465 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
466 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BYTE_DATA);
468 error = nfsmb_wait(sc);
470 NFSMB_DEBUG(printf("nfsmb: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
477 nfsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
479 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
483 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
484 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
485 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BYTE_DATA);
487 if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
488 *byte = NFSMB_SMBINB(sc, SMB_DATA);
490 NFSMB_DEBUG(printf("nfsmb: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, (unsigned char)*byte, error));
497 nfsmb_writew(device_t dev, u_char slave, char cmd, short word)
499 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
503 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
504 NFSMB_SMBOUTB(sc, SMB_DATA, word);
505 NFSMB_SMBOUTB(sc, SMB_DATA + 1, word >> 8);
506 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
507 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_WORD_DATA);
509 error = nfsmb_wait(sc);
511 NFSMB_DEBUG(printf("nfsmb: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
518 nfsmb_readw(device_t dev, u_char slave, char cmd, short *word)
520 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
524 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
525 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
526 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_WORD_DATA);
528 if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
529 *word = NFSMB_SMBINB(sc, SMB_DATA) |
530 (NFSMB_SMBINB(sc, SMB_DATA + 1) << 8);
532 NFSMB_DEBUG(printf("nfsmb: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, (unsigned short)*word, error));
539 nfsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
541 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
545 if (count < 1 || count > 32)
549 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
550 NFSMB_SMBOUTB(sc, SMB_BCNT, count);
551 for (i = 0; i < count; i++)
552 NFSMB_SMBOUTB(sc, SMB_DATA + i, buf[i]);
553 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
554 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BLOCK_DATA);
556 error = nfsmb_wait(sc);
558 NFSMB_DEBUG(printf("nfsmb: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
565 nfsmb_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf)
567 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
571 if (*count < 1 || *count > 32)
575 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
576 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
577 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BLOCK_DATA);
579 if ((error = nfsmb_wait(sc)) == SMB_ENOERR) {
580 len = NFSMB_SMBINB(sc, SMB_BCNT);
581 for (i = 0; i < len; i++) {
582 data = NFSMB_SMBINB(sc, SMB_DATA + i);
589 NFSMB_DEBUG(printf("nfsmb: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, *count, cmd, error));
595 static device_method_t nfsmb_methods[] = {
596 /* Device interface */
597 DEVMETHOD(device_probe, nfsmb_probe),
598 DEVMETHOD(device_attach, nfsmb_attach),
599 DEVMETHOD(device_detach, nfsmb_detach),
601 /* SMBus interface */
602 DEVMETHOD(smbus_callback, nfsmb_callback),
603 DEVMETHOD(smbus_quick, nfsmb_quick),
604 DEVMETHOD(smbus_sendb, nfsmb_sendb),
605 DEVMETHOD(smbus_recvb, nfsmb_recvb),
606 DEVMETHOD(smbus_writeb, nfsmb_writeb),
607 DEVMETHOD(smbus_readb, nfsmb_readb),
608 DEVMETHOD(smbus_writew, nfsmb_writew),
609 DEVMETHOD(smbus_readw, nfsmb_readw),
610 DEVMETHOD(smbus_bwrite, nfsmb_bwrite),
611 DEVMETHOD(smbus_bread, nfsmb_bread),
616 static device_method_t nfsmbsub_methods[] = {
617 /* Device interface */
618 DEVMETHOD(device_probe, nfsmbsub_probe),
619 DEVMETHOD(device_attach, nfsmbsub_attach),
620 DEVMETHOD(device_detach, nfsmbsub_detach),
622 /* SMBus interface */
623 DEVMETHOD(smbus_callback, nfsmb_callback),
624 DEVMETHOD(smbus_quick, nfsmb_quick),
625 DEVMETHOD(smbus_sendb, nfsmb_sendb),
626 DEVMETHOD(smbus_recvb, nfsmb_recvb),
627 DEVMETHOD(smbus_writeb, nfsmb_writeb),
628 DEVMETHOD(smbus_readb, nfsmb_readb),
629 DEVMETHOD(smbus_writew, nfsmb_writew),
630 DEVMETHOD(smbus_readw, nfsmb_readw),
631 DEVMETHOD(smbus_bwrite, nfsmb_bwrite),
632 DEVMETHOD(smbus_bread, nfsmb_bread),
637 static devclass_t nfsmb_devclass;
639 static driver_t nfsmb_driver = {
642 sizeof(struct nfsmb_softc),
645 static driver_t nfsmbsub_driver = {
648 sizeof(struct nfsmb_softc),
651 DRIVER_MODULE(nfsmb, pci, nfsmb_driver, nfsmb_devclass, 0, 0);
652 DRIVER_MODULE(nfsmb, nfsmb, nfsmbsub_driver, nfsmb_devclass, 0, 0);
653 DRIVER_MODULE(smbus, nfsmb, smbus_driver, smbus_devclass, 0, 0);
655 MODULE_DEPEND(nfsmb, pci, 1, 1, 1);
656 MODULE_DEPEND(nfsmb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
657 MODULE_VERSION(nfsmb, 1);