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
69 /* PCI Configuration space registers */
70 #define NF2PCI_SMBASE_1 PCIR_BAR(4)
71 #define NF2PCI_SMBASE_2 PCIR_BAR(5)
74 * ACPI 3.0, Chapter 12, SMBus Host Controller Interface.
76 #define SMB_PRTCL 0x00 /* protocol */
77 #define SMB_STS 0x01 /* status */
78 #define SMB_ADDR 0x02 /* address */
79 #define SMB_CMD 0x03 /* command */
80 #define SMB_DATA 0x04 /* 32 data registers */
81 #define SMB_BCNT 0x24 /* number of data bytes */
82 #define SMB_ALRM_A 0x25 /* alarm address */
83 #define SMB_ALRM_D 0x26 /* 2 bytes alarm data */
85 #define SMB_STS_DONE 0x80
86 #define SMB_STS_ALRM 0x40
87 #define SMB_STS_RES 0x20
88 #define SMB_STS_STATUS 0x1f
89 #define SMB_STS_OK 0x00 /* OK */
90 #define SMB_STS_UF 0x07 /* Unknown Failure */
91 #define SMB_STS_DANA 0x10 /* Device Address Not Acknowledged */
92 #define SMB_STS_DED 0x11 /* Device Error Detected */
93 #define SMB_STS_DCAD 0x12 /* Device Command Access Denied */
94 #define SMB_STS_UE 0x13 /* Unknown Error */
95 #define SMB_STS_DAD 0x17 /* Device Access Denied */
96 #define SMB_STS_T 0x18 /* Timeout */
97 #define SMB_STS_HUP 0x19 /* Host Unsupported Protocol */
98 #define SMB_STS_B 0x1A /* Busy */
99 #define SMB_STS_PEC 0x1F /* PEC (CRC-8) Error */
101 #define SMB_PRTCL_WRITE 0x00
102 #define SMB_PRTCL_READ 0x01
103 #define SMB_PRTCL_QUICK 0x02
104 #define SMB_PRTCL_BYTE 0x04
105 #define SMB_PRTCL_BYTE_DATA 0x06
106 #define SMB_PRTCL_WORD_DATA 0x08
107 #define SMB_PRTCL_BLOCK_DATA 0x0a
108 #define SMB_PRTCL_PROC_CALL 0x0c
109 #define SMB_PRTCL_BLOCK_PROC_CALL 0x0d
110 #define SMB_PRTCL_PEC 0x80
114 struct resource *res;
115 bus_space_tag_t smbst;
116 bus_space_handle_t smbsh;
122 #define NFSMB_LOCK(nfsmb) mtx_lock(&(nfsmb)->lock)
123 #define NFSMB_UNLOCK(nfsmb) mtx_unlock(&(nfsmb)->lock)
124 #define NFSMB_LOCK_ASSERT(nfsmb) mtx_assert(&(nfsmb)->lock, MA_OWNED)
126 #define NFSMB_SMBINB(nfsmb, register) \
127 (bus_space_read_1(nfsmb->smbst, nfsmb->smbsh, register))
128 #define NFSMB_SMBOUTB(nfsmb, register, value) \
129 (bus_space_write_1(nfsmb->smbst, nfsmb->smbsh, register, value))
131 static int nfsmb_detach(device_t dev);
132 static int nfsmbsub_detach(device_t dev);
135 nfsmbsub_probe(device_t dev)
138 device_set_desc(dev, "nForce2/3/4 MCP SMBus Controller");
139 return (BUS_PROBE_DEFAULT);
143 nfsmb_probe(device_t dev)
148 vid = pci_get_vendor(dev);
149 did = pci_get_device(dev);
151 if (vid == NFSMB_VENDORID_NVIDIA) {
153 case NFSMB_DEVICEID_NF2_SMB:
154 case NFSMB_DEVICEID_NF2_ULTRA_SMB:
155 case NFSMB_DEVICEID_NF3_PRO150_SMB:
156 case NFSMB_DEVICEID_NF3_250GB_SMB:
157 case NFSMB_DEVICEID_NF4_SMB:
158 case NFSMB_DEVICEID_NF4_04_SMB:
159 case NFSMB_DEVICEID_NF4_51_SMB:
160 case NFSMB_DEVICEID_NF4_55_SMB:
161 case NFSMB_DEVICEID_NF4_61_SMB:
162 case NFSMB_DEVICEID_NF4_65_SMB:
163 device_set_desc(dev, "nForce2/3/4 MCP SMBus Controller");
164 return (BUS_PROBE_DEFAULT);
172 nfsmbsub_attach(device_t dev)
175 struct nfsmb_softc *nfsmbsub_sc = device_get_softc(dev);
177 parent = device_get_parent(dev);
179 nfsmbsub_sc->rid = NF2PCI_SMBASE_2;
181 nfsmbsub_sc->res = bus_alloc_resource_any(parent, SYS_RES_IOPORT,
182 &nfsmbsub_sc->rid, RF_ACTIVE);
183 if (nfsmbsub_sc->res == NULL) {
184 /* Older incarnations of the device used non-standard BARs. */
185 nfsmbsub_sc->rid = 0x54;
186 nfsmbsub_sc->res = bus_alloc_resource_any(parent,
187 SYS_RES_IOPORT, &nfsmbsub_sc->rid, RF_ACTIVE);
188 if (nfsmbsub_sc->res == NULL) {
189 device_printf(dev, "could not map i/o space\n");
193 nfsmbsub_sc->smbst = rman_get_bustag(nfsmbsub_sc->res);
194 nfsmbsub_sc->smbsh = rman_get_bushandle(nfsmbsub_sc->res);
195 mtx_init(&nfsmbsub_sc->lock, device_get_nameunit(dev), "nfsmb",
198 nfsmbsub_sc->smbus = device_add_child(dev, "smbus", -1);
199 if (nfsmbsub_sc->smbus == NULL) {
200 nfsmbsub_detach(dev);
204 bus_generic_attach(dev);
210 nfsmb_attach(device_t dev)
212 struct nfsmb_softc *nfsmb_sc = device_get_softc(dev);
214 /* Allocate I/O space */
215 nfsmb_sc->rid = NF2PCI_SMBASE_1;
217 nfsmb_sc->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
218 &nfsmb_sc->rid, RF_ACTIVE);
220 if (nfsmb_sc->res == NULL) {
221 /* Older incarnations of the device used non-standard BARs. */
222 nfsmb_sc->rid = 0x50;
223 nfsmb_sc->res = bus_alloc_resource_any(dev,
224 SYS_RES_IOPORT, &nfsmb_sc->rid, RF_ACTIVE);
225 if (nfsmb_sc->res == NULL) {
226 device_printf(dev, "could not map i/o space\n");
231 nfsmb_sc->smbst = rman_get_bustag(nfsmb_sc->res);
232 nfsmb_sc->smbsh = rman_get_bushandle(nfsmb_sc->res);
233 mtx_init(&nfsmb_sc->lock, device_get_nameunit(dev), "nfsmb", MTX_DEF);
235 /* Allocate a new smbus device */
236 nfsmb_sc->smbus = device_add_child(dev, "smbus", -1);
237 if (!nfsmb_sc->smbus) {
242 nfsmb_sc->subdev = NULL;
243 switch (pci_get_device(dev)) {
244 case NFSMB_DEVICEID_NF2_SMB:
245 case NFSMB_DEVICEID_NF2_ULTRA_SMB:
246 case NFSMB_DEVICEID_NF3_PRO150_SMB:
247 case NFSMB_DEVICEID_NF3_250GB_SMB:
248 case NFSMB_DEVICEID_NF4_SMB:
249 case NFSMB_DEVICEID_NF4_04_SMB:
250 case NFSMB_DEVICEID_NF4_51_SMB:
251 case NFSMB_DEVICEID_NF4_55_SMB:
252 case NFSMB_DEVICEID_NF4_61_SMB:
253 case NFSMB_DEVICEID_NF4_65_SMB:
254 /* Trying to add secondary device as slave */
255 nfsmb_sc->subdev = device_add_child(dev, "nfsmb", -1);
256 if (!nfsmb_sc->subdev) {
265 bus_generic_attach(dev);
271 nfsmbsub_detach(device_t dev)
274 struct nfsmb_softc *nfsmbsub_sc = device_get_softc(dev);
276 parent = device_get_parent(dev);
278 if (nfsmbsub_sc->smbus) {
279 device_delete_child(dev, nfsmbsub_sc->smbus);
280 nfsmbsub_sc->smbus = NULL;
282 mtx_destroy(&nfsmbsub_sc->lock);
283 if (nfsmbsub_sc->res) {
284 bus_release_resource(parent, SYS_RES_IOPORT, nfsmbsub_sc->rid,
286 nfsmbsub_sc->res = NULL;
292 nfsmb_detach(device_t dev)
294 struct nfsmb_softc *nfsmb_sc = device_get_softc(dev);
296 if (nfsmb_sc->subdev) {
297 device_delete_child(dev, nfsmb_sc->subdev);
298 nfsmb_sc->subdev = NULL;
301 if (nfsmb_sc->smbus) {
302 device_delete_child(dev, nfsmb_sc->smbus);
303 nfsmb_sc->smbus = NULL;
306 mtx_destroy(&nfsmb_sc->lock);
308 bus_release_resource(dev, SYS_RES_IOPORT, nfsmb_sc->rid,
310 nfsmb_sc->res = NULL;
317 nfsmb_callback(device_t dev, int index, void *data)
322 case SMB_REQUEST_BUS:
323 case SMB_RELEASE_BUS:
333 nfsmb_wait(struct nfsmb_softc *sc)
338 NFSMB_LOCK_ASSERT(sc);
339 if (NFSMB_SMBINB(sc, SMB_PRTCL) != 0)
344 } while (NFSMB_SMBINB(sc, SMB_PRTCL) != 0 && count--);
346 return (SMB_ETIMEOUT);
349 sts = NFSMB_SMBINB(sc, SMB_STS) & SMB_STS_STATUS;
350 NFSMB_DEBUG(printf("nfsmb: STS=0x%x\n", sts));
363 error = SMB_ETIMEOUT;
368 error = SMB_ENOTSUPP;
379 nfsmb_quick(device_t dev, u_char slave, int how)
381 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
385 protocol = SMB_PRTCL_QUICK;
389 protocol |= SMB_PRTCL_WRITE;
390 NFSMB_DEBUG(printf("nfsmb: QWRITE to 0x%x", slave));
393 protocol |= SMB_PRTCL_READ;
394 NFSMB_DEBUG(printf("nfsmb: QREAD to 0x%x", slave));
397 panic("%s: unknown QUICK command (%x)!", __func__, how);
401 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
402 NFSMB_SMBOUTB(sc, SMB_PRTCL, protocol);
404 error = nfsmb_wait(sc);
406 NFSMB_DEBUG(printf(", error=0x%x\n", error));
413 nfsmb_sendb(device_t dev, u_char slave, char byte)
415 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
419 NFSMB_SMBOUTB(sc, SMB_CMD, byte);
420 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
421 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BYTE);
423 error = nfsmb_wait(sc);
425 NFSMB_DEBUG(printf("nfsmb: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
432 nfsmb_recvb(device_t dev, u_char slave, char *byte)
434 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
438 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
439 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BYTE);
441 if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
442 *byte = NFSMB_SMBINB(sc, SMB_DATA);
444 NFSMB_DEBUG(printf("nfsmb: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
451 nfsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
453 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
457 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
458 NFSMB_SMBOUTB(sc, SMB_DATA, byte);
459 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
460 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BYTE_DATA);
462 error = nfsmb_wait(sc);
464 NFSMB_DEBUG(printf("nfsmb: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
471 nfsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
473 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
477 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
478 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
479 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BYTE_DATA);
481 if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
482 *byte = NFSMB_SMBINB(sc, SMB_DATA);
484 NFSMB_DEBUG(printf("nfsmb: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, (unsigned char)*byte, error));
491 nfsmb_writew(device_t dev, u_char slave, char cmd, short word)
493 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
497 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
498 NFSMB_SMBOUTB(sc, SMB_DATA, word);
499 NFSMB_SMBOUTB(sc, SMB_DATA + 1, word >> 8);
500 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
501 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_WORD_DATA);
503 error = nfsmb_wait(sc);
505 NFSMB_DEBUG(printf("nfsmb: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
512 nfsmb_readw(device_t dev, u_char slave, char cmd, short *word)
514 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
518 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
519 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
520 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_WORD_DATA);
522 if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
523 *word = NFSMB_SMBINB(sc, SMB_DATA) |
524 (NFSMB_SMBINB(sc, SMB_DATA + 1) << 8);
526 NFSMB_DEBUG(printf("nfsmb: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, (unsigned short)*word, error));
533 nfsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
535 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
539 if (count < 1 || count > 32)
543 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
544 NFSMB_SMBOUTB(sc, SMB_BCNT, count);
545 for (i = 0; i < count; i++)
546 NFSMB_SMBOUTB(sc, SMB_DATA + i, buf[i]);
547 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
548 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BLOCK_DATA);
550 error = nfsmb_wait(sc);
552 NFSMB_DEBUG(printf("nfsmb: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
559 nfsmb_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf)
561 struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
565 if (*count < 1 || *count > 32)
569 NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
570 NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
571 NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BLOCK_DATA);
573 if ((error = nfsmb_wait(sc)) == SMB_ENOERR) {
574 len = NFSMB_SMBINB(sc, SMB_BCNT);
575 for (i = 0; i < len; i++) {
576 data = NFSMB_SMBINB(sc, SMB_DATA + i);
583 NFSMB_DEBUG(printf("nfsmb: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, *count, cmd, error));
589 static device_method_t nfsmb_methods[] = {
590 /* Device interface */
591 DEVMETHOD(device_probe, nfsmb_probe),
592 DEVMETHOD(device_attach, nfsmb_attach),
593 DEVMETHOD(device_detach, nfsmb_detach),
595 /* SMBus interface */
596 DEVMETHOD(smbus_callback, nfsmb_callback),
597 DEVMETHOD(smbus_quick, nfsmb_quick),
598 DEVMETHOD(smbus_sendb, nfsmb_sendb),
599 DEVMETHOD(smbus_recvb, nfsmb_recvb),
600 DEVMETHOD(smbus_writeb, nfsmb_writeb),
601 DEVMETHOD(smbus_readb, nfsmb_readb),
602 DEVMETHOD(smbus_writew, nfsmb_writew),
603 DEVMETHOD(smbus_readw, nfsmb_readw),
604 DEVMETHOD(smbus_bwrite, nfsmb_bwrite),
605 DEVMETHOD(smbus_bread, nfsmb_bread),
610 static device_method_t nfsmbsub_methods[] = {
611 /* Device interface */
612 DEVMETHOD(device_probe, nfsmbsub_probe),
613 DEVMETHOD(device_attach, nfsmbsub_attach),
614 DEVMETHOD(device_detach, nfsmbsub_detach),
616 /* SMBus interface */
617 DEVMETHOD(smbus_callback, nfsmb_callback),
618 DEVMETHOD(smbus_quick, nfsmb_quick),
619 DEVMETHOD(smbus_sendb, nfsmb_sendb),
620 DEVMETHOD(smbus_recvb, nfsmb_recvb),
621 DEVMETHOD(smbus_writeb, nfsmb_writeb),
622 DEVMETHOD(smbus_readb, nfsmb_readb),
623 DEVMETHOD(smbus_writew, nfsmb_writew),
624 DEVMETHOD(smbus_readw, nfsmb_readw),
625 DEVMETHOD(smbus_bwrite, nfsmb_bwrite),
626 DEVMETHOD(smbus_bread, nfsmb_bread),
631 static devclass_t nfsmb_devclass;
633 static driver_t nfsmb_driver = {
636 sizeof(struct nfsmb_softc),
639 static driver_t nfsmbsub_driver = {
642 sizeof(struct nfsmb_softc),
645 DRIVER_MODULE(nfsmb, pci, nfsmb_driver, nfsmb_devclass, 0, 0);
646 DRIVER_MODULE(nfsmb, nfsmb, nfsmbsub_driver, nfsmb_devclass, 0, 0);
647 DRIVER_MODULE(smbus, nfsmb, smbus_driver, smbus_devclass, 0, 0);
649 MODULE_DEPEND(nfsmb, pci, 1, 1, 1);
650 MODULE_DEPEND(nfsmb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
651 MODULE_VERSION(nfsmb, 1);