1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/uio.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#include <dev/iicbus/iiconf.h>
#include <dev/smbus/smbconf.h>
#include "smbus_if.h"
#define NFSMB_DEBUG(x) if (nfsmb_debug) (x)
#ifdef DEBUG
static int nfsmb_debug = 1;
#else
static int nfsmb_debug = 0;
#endif
/* NVIDIA nForce2/3/4 MCP */
#define NFSMB_VENDORID_NVIDIA 0x10de
#define NFSMB_DEVICEID_NF2_SMB 0x0064
#define NFSMB_DEVICEID_NF2_ULTRA_SMB 0x0084
#define NFSMB_DEVICEID_NF3_PRO150_SMB 0x00d4
#define NFSMB_DEVICEID_NF3_250GB_SMB 0x00e4
#define NFSMB_DEVICEID_NF4_SMB 0x0052
#define NFSMB_DEVICEID_NF4_04_SMB 0x0034
#define NFSMB_DEVICEID_NF4_51_SMB 0x0264
#define NFSMB_DEVICEID_NF4_55_SMB 0x0368
/* PCI Configuration space registers */
#define NF2PCI_SMBASE_1 PCIR_BAR(4)
#define NF2PCI_SMBASE_2 PCIR_BAR(5)
/*
* ACPI 3.0, Chapter 12, SMBus Host Controller Interface.
*/
#define SMB_PRTCL 0x00 /* protocol */
#define SMB_STS 0x01 /* status */
#define SMB_ADDR 0x02 /* address */
#define SMB_CMD 0x03 /* command */
#define SMB_DATA 0x04 /* 32 data registers */
#define SMB_BCNT 0x24 /* number of data bytes */
#define SMB_ALRM_A 0x25 /* alarm address */
#define SMB_ALRM_D 0x26 /* 2 bytes alarm data */
#define SMB_STS_DONE 0x80
#define SMB_STS_ALRM 0x40
#define SMB_STS_RES 0x20
#define SMB_STS_STATUS 0x1f
#define SMB_STS_OK 0x00 /* OK */
#define SMB_STS_UF 0x07 /* Unknown Failure */
#define SMB_STS_DANA 0x10 /* Device Address Not Acknowledged */
#define SMB_STS_DED 0x11 /* Device Error Detected */
#define SMB_STS_DCAD 0x12 /* Device Command Access Denied */
#define SMB_STS_UE 0x13 /* Unknown Error */
#define SMB_STS_DAD 0x17 /* Device Access Denied */
#define SMB_STS_T 0x18 /* Timeout */
#define SMB_STS_HUP 0x19 /* Host Unsupported Protocol */
#define SMB_STS_B 0x1A /* Busy */
#define SMB_STS_PEC 0x1F /* PEC (CRC-8) Error */
#define SMB_PRTCL_WRITE 0x00
#define SMB_PRTCL_READ 0x01
#define SMB_PRTCL_QUICK 0x02
#define SMB_PRTCL_BYTE 0x04
#define SMB_PRTCL_BYTE_DATA 0x06
#define SMB_PRTCL_WORD_DATA 0x08
#define SMB_PRTCL_BLOCK_DATA 0x0a
#define SMB_PRTCL_PROC_CALL 0x0c
#define SMB_PRTCL_BLOCK_PROC_CALL 0x0d
#define SMB_PRTCL_PEC 0x80
struct nfsmb_softc {
int rid;
struct resource *res;
bus_space_tag_t smbst;
bus_space_handle_t smbsh;
device_t smbus;
device_t subdev;
};
#define NFSMB_SMBINB(nfsmb, register) \
(bus_space_read_1(nfsmb->smbst, nfsmb->smbsh, register))
#define NFSMB_SMBOUTB(nfsmb, register, value) \
(bus_space_write_1(nfsmb->smbst, nfsmb->smbsh, register, value))
static int
nfsmbsub_probe(device_t dev)
{
device_set_desc(dev, "nForce2/3/4 MCP SMBus Controller");
return (BUS_PROBE_DEFAULT);
}
static int
nfsmb_probe(device_t dev)
{
u_int16_t vid;
u_int16_t did;
vid = pci_get_vendor(dev);
did = pci_get_device(dev);
if (vid == NFSMB_VENDORID_NVIDIA) {
switch(did) {
case NFSMB_DEVICEID_NF2_SMB:
case NFSMB_DEVICEID_NF2_ULTRA_SMB:
case NFSMB_DEVICEID_NF3_PRO150_SMB:
case NFSMB_DEVICEID_NF3_250GB_SMB:
case NFSMB_DEVICEID_NF4_SMB:
case NFSMB_DEVICEID_NF4_04_SMB:
case NFSMB_DEVICEID_NF4_51_SMB:
case NFSMB_DEVICEID_NF4_55_SMB:
device_set_desc(dev, "nForce2/3/4 MCP SMBus Controller");
return (BUS_PROBE_DEFAULT);
}
}
return (ENXIO);
}
static int
nfsmbsub_attach(device_t dev)
{
device_t parent;
struct nfsmb_softc *nfsmbsub_sc = device_get_softc(dev);
parent = device_get_parent(dev);
nfsmbsub_sc->rid = NF2PCI_SMBASE_2;
nfsmbsub_sc->res = bus_alloc_resource_any(parent, SYS_RES_IOPORT,
&nfsmbsub_sc->rid, RF_ACTIVE);
if (nfsmbsub_sc->res == NULL) {
device_printf(dev, "could not map i/o space\n");
return (ENXIO);
}
nfsmbsub_sc->smbst = rman_get_bustag(nfsmbsub_sc->res);
nfsmbsub_sc->smbsh = rman_get_bushandle(nfsmbsub_sc->res);
nfsmbsub_sc->smbus = device_add_child(dev, "smbus", -1);
if (nfsmbsub_sc->smbus == NULL)
return (EINVAL);
bus_generic_attach(dev);
return (0);
}
static int
nfsmb_attach(device_t dev)
{
struct nfsmb_softc *nfsmb_sc = device_get_softc(dev);
/* Allocate I/O space */
nfsmb_sc->rid = NF2PCI_SMBASE_1;
nfsmb_sc->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
&nfsmb_sc->rid, RF_ACTIVE);
if (nfsmb_sc->res == NULL) {
device_printf(dev, "could not map i/o space\n");
return (ENXIO);
}
nfsmb_sc->smbst = rman_get_bustag(nfsmb_sc->res);
nfsmb_sc->smbsh = rman_get_bushandle(nfsmb_sc->res);
/* Allocate a new smbus device */
nfsmb_sc->smbus = device_add_child(dev, "smbus", -1);
if (!nfsmb_sc->smbus)
return (EINVAL);
nfsmb_sc->subdev = NULL;
switch (pci_get_device(dev)) {
case NFSMB_DEVICEID_NF2_SMB:
case NFSMB_DEVICEID_NF2_ULTRA_SMB:
case NFSMB_DEVICEID_NF3_PRO150_SMB:
case NFSMB_DEVICEID_NF3_250GB_SMB:
case NFSMB_DEVICEID_NF4_SMB:
case NFSMB_DEVICEID_NF4_04_SMB:
case NFSMB_DEVICEID_NF4_51_SMB:
case NFSMB_DEVICEID_NF4_55_SMB:
/* Trying to add secondary device as slave */
nfsmb_sc->subdev = device_add_child(dev, "nfsmb", -1);
if (!nfsmb_sc->subdev)
return (EINVAL);
break;
default:
break;
}
bus_generic_attach(dev);
return (0);
}
static int
nfsmbsub_detach(device_t dev)
{
device_t parent;
struct nfsmb_softc *nfsmbsub_sc = device_get_softc(dev);
parent = device_get_parent(dev);
if (nfsmbsub_sc->smbus) {
device_delete_child(dev, nfsmbsub_sc->smbus);
nfsmbsub_sc->smbus = NULL;
}
if (nfsmbsub_sc->res) {
bus_release_resource(parent, SYS_RES_IOPORT, nfsmbsub_sc->rid,
nfsmbsub_sc->res);
nfsmbsub_sc->res = NULL;
}
return (0);
}
static int
nfsmb_detach(device_t dev)
{
struct nfsmb_softc *nfsmb_sc = device_get_softc(dev);
if (nfsmb_sc->subdev) {
device_delete_child(dev, nfsmb_sc->subdev);
nfsmb_sc->subdev = NULL;
}
if (nfsmb_sc->smbus) {
device_delete_child(dev, nfsmb_sc->smbus);
nfsmb_sc->smbus = NULL;
}
if (nfsmb_sc->res) {
bus_release_resource(dev, SYS_RES_IOPORT, nfsmb_sc->rid,
nfsmb_sc->res);
nfsmb_sc->res = NULL;
}
return (0);
}
static int
nfsmb_callback(device_t dev, int index, void *data)
{
int error = 0;
switch (index) {
case SMB_REQUEST_BUS:
case SMB_RELEASE_BUS:
break;
default:
error = EINVAL;
}
return (error);
}
static int
nfsmb_wait(struct nfsmb_softc *sc)
{
u_char sts;
int error, count;
if (NFSMB_SMBINB(sc, SMB_PRTCL) != 0)
{
count = 10000;
do {
DELAY(500);
} while (NFSMB_SMBINB(sc, SMB_PRTCL) != 0 && count--);
if (count == 0)
return (SMB_ETIMEOUT);
}
sts = NFSMB_SMBINB(sc, SMB_STS) & SMB_STS_STATUS;
NFSMB_DEBUG(printf("nfsmb: STS=0x%x\n", sts));
switch (sts) {
case SMB_STS_OK:
error = SMB_ENOERR;
break;
case SMB_STS_DANA:
error = SMB_ENOACK;
break;
case SMB_STS_B:
error = SMB_EBUSY;
break;
case SMB_STS_T:
error = SMB_ETIMEOUT;
break;
case SMB_STS_DCAD:
case SMB_STS_DAD:
case SMB_STS_HUP:
error = SMB_ENOTSUPP;
break;
default:
error = SMB_EBUSERR;
break;
}
return (error);
}
static int
nfsmb_quick(device_t dev, u_char slave, int how)
{
struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
u_char protocol;
int error;
protocol = SMB_PRTCL_QUICK;
switch (how) {
case SMB_QWRITE:
protocol |= SMB_PRTCL_WRITE;
NFSMB_DEBUG(printf("nfsmb: QWRITE to 0x%x", slave));
break;
case SMB_QREAD:
protocol |= SMB_PRTCL_READ;
NFSMB_DEBUG(printf("nfsmb: QREAD to 0x%x", slave));
break;
default:
panic("%s: unknown QUICK command (%x)!", __func__, how);
}
NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
NFSMB_SMBOUTB(sc, SMB_PRTCL, protocol);
error = nfsmb_wait(sc);
NFSMB_DEBUG(printf(", error=0x%x\n", error));
return (error);
}
static int
nfsmb_sendb(device_t dev, u_char slave, char byte)
{
struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
int error;
NFSMB_SMBOUTB(sc, SMB_CMD, byte);
NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BYTE);
error = nfsmb_wait(sc);
NFSMB_DEBUG(printf("nfsmb: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
return (error);
}
static int
nfsmb_recvb(device_t dev, u_char slave, char *byte)
{
struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
int error;
NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BYTE);
if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
*byte = NFSMB_SMBINB(sc, SMB_DATA);
NFSMB_DEBUG(printf("nfsmb: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
return (error);
}
static int
nfsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
{
struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
int error;
NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
NFSMB_SMBOUTB(sc, SMB_DATA, byte);
NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BYTE_DATA);
error = nfsmb_wait(sc);
NFSMB_DEBUG(printf("nfsmb: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
return (error);
}
static int
nfsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
{
struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
int error;
NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BYTE_DATA);
if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
*byte = NFSMB_SMBINB(sc, SMB_DATA);
NFSMB_DEBUG(printf("nfsmb: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, (unsigned char)*byte, error));
return (error);
}
static int
nfsmb_writew(device_t dev, u_char slave, char cmd, short word)
{
struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
int error;
NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
NFSMB_SMBOUTB(sc, SMB_DATA, word);
NFSMB_SMBOUTB(sc, SMB_DATA + 1, word >> 8);
NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_WORD_DATA);
error = nfsmb_wait(sc);
NFSMB_DEBUG(printf("nfsmb: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
return (error);
}
static int
nfsmb_readw(device_t dev, u_char slave, char cmd, short *word)
{
struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
int error;
NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_WORD_DATA);
if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
*word = NFSMB_SMBINB(sc, SMB_DATA) |
(NFSMB_SMBINB(sc, SMB_DATA + 1) << 8);
NFSMB_DEBUG(printf("nfsmb: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, (unsigned short)*word, error));
return (error);
}
static int
nfsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
{
struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
u_char i;
int error;
if (count < 1 || count > 32)
return (SMB_EINVAL);
NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
NFSMB_SMBOUTB(sc, SMB_BCNT, count);
for (i = 0; i < count; i++)
NFSMB_SMBOUTB(sc, SMB_DATA + i, buf[i]);
NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BLOCK_DATA);
error = nfsmb_wait(sc);
NFSMB_DEBUG(printf("nfsmb: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
return (error);
}
static int
nfsmb_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf)
{
struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
u_char data, len, i;
int error;
if (*count < 1 || *count > 32)
return (SMB_EINVAL);
NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BLOCK_DATA);
if ((error = nfsmb_wait(sc)) == SMB_ENOERR) {
len = NFSMB_SMBINB(sc, SMB_BCNT);
for (i = 0; i < len; i++) {
data = NFSMB_SMBINB(sc, SMB_DATA + i);
if (i < *count)
buf[i] = data;
}
*count = len;
}
NFSMB_DEBUG(printf("nfsmb: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, *count, cmd, error));
return (error);
}
static device_method_t nfsmb_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, nfsmb_probe),
DEVMETHOD(device_attach, nfsmb_attach),
DEVMETHOD(device_detach, nfsmb_detach),
/* SMBus interface */
DEVMETHOD(smbus_callback, nfsmb_callback),
DEVMETHOD(smbus_quick, nfsmb_quick),
DEVMETHOD(smbus_sendb, nfsmb_sendb),
DEVMETHOD(smbus_recvb, nfsmb_recvb),
DEVMETHOD(smbus_writeb, nfsmb_writeb),
DEVMETHOD(smbus_readb, nfsmb_readb),
DEVMETHOD(smbus_writew, nfsmb_writew),
DEVMETHOD(smbus_readw, nfsmb_readw),
DEVMETHOD(smbus_bwrite, nfsmb_bwrite),
DEVMETHOD(smbus_bread, nfsmb_bread),
{ 0, 0 }
};
static device_method_t nfsmbsub_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, nfsmbsub_probe),
DEVMETHOD(device_attach, nfsmbsub_attach),
DEVMETHOD(device_detach, nfsmbsub_detach),
/* SMBus interface */
DEVMETHOD(smbus_callback, nfsmb_callback),
DEVMETHOD(smbus_quick, nfsmb_quick),
DEVMETHOD(smbus_sendb, nfsmb_sendb),
DEVMETHOD(smbus_recvb, nfsmb_recvb),
DEVMETHOD(smbus_writeb, nfsmb_writeb),
DEVMETHOD(smbus_readb, nfsmb_readb),
DEVMETHOD(smbus_writew, nfsmb_writew),
DEVMETHOD(smbus_readw, nfsmb_readw),
DEVMETHOD(smbus_bwrite, nfsmb_bwrite),
DEVMETHOD(smbus_bread, nfsmb_bread),
{ 0, 0 }
};
static devclass_t nfsmb_devclass;
static driver_t nfsmb_driver = {
"nfsmb",
nfsmb_methods,
sizeof(struct nfsmb_softc),
};
static driver_t nfsmbsub_driver = {
"nfsmb",
nfsmbsub_methods,
sizeof(struct nfsmb_softc),
};
DRIVER_MODULE(nfsmb, pci, nfsmb_driver, nfsmb_devclass, 0, 0);
DRIVER_MODULE(nfsmb, nfsmb, nfsmbsub_driver, nfsmb_devclass, 0, 0);
DRIVER_MODULE(smbus, nfsmb, smbus_driver, smbus_devclass, 0, 0);
MODULE_DEPEND(nfsmb, pci, 1, 1, 1);
MODULE_DEPEND(nfsmb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
MODULE_VERSION(nfsmb, 1);
|