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#include <linux/module.h>
46#include <linux/pci.h>
47#include <linux/kernel.h>
48#include <linux/stddef.h>
49#include <linux/ioport.h>
50#include <linux/init.h>
51#include <linux/i2c.h>
52#include <linux/delay.h>
53#include <linux/dmi.h>
54#include <linux/acpi.h>
55#include <linux/slab.h>
56#include <linux/io.h>
57
58MODULE_LICENSE("GPL");
59MODULE_AUTHOR("Hans-Frieder Vogt <hfvogt@gmx.net>");
60MODULE_DESCRIPTION("nForce2/3/4/5xx SMBus driver");
61
62
63struct nforce2_smbus {
64 struct i2c_adapter adapter;
65 int base;
66 int size;
67 int blockops;
68 int can_abort;
69};
70
71
72
73
74
75
76#define NFORCE_PCI_SMB1 0x50
77#define NFORCE_PCI_SMB2 0x54
78
79
80
81
82
83#define NVIDIA_SMB_PRTCL (smbus->base + 0x00)
84#define NVIDIA_SMB_STS (smbus->base + 0x01)
85#define NVIDIA_SMB_ADDR (smbus->base + 0x02)
86#define NVIDIA_SMB_CMD (smbus->base + 0x03)
87#define NVIDIA_SMB_DATA (smbus->base + 0x04)
88#define NVIDIA_SMB_BCNT (smbus->base + 0x24)
89
90#define NVIDIA_SMB_STATUS_ABRT (smbus->base + 0x3c)
91
92
93#define NVIDIA_SMB_CTRL (smbus->base + 0x3e)
94
95#define NVIDIA_SMB_STATUS_ABRT_STS 0x01
96
97#define NVIDIA_SMB_CTRL_ABORT 0x20
98#define NVIDIA_SMB_STS_DONE 0x80
99#define NVIDIA_SMB_STS_ALRM 0x40
100#define NVIDIA_SMB_STS_RES 0x20
101#define NVIDIA_SMB_STS_STATUS 0x1f
102
103#define NVIDIA_SMB_PRTCL_WRITE 0x00
104#define NVIDIA_SMB_PRTCL_READ 0x01
105#define NVIDIA_SMB_PRTCL_QUICK 0x02
106#define NVIDIA_SMB_PRTCL_BYTE 0x04
107#define NVIDIA_SMB_PRTCL_BYTE_DATA 0x06
108#define NVIDIA_SMB_PRTCL_WORD_DATA 0x08
109#define NVIDIA_SMB_PRTCL_BLOCK_DATA 0x0a
110#define NVIDIA_SMB_PRTCL_PEC 0x80
111
112
113#define MAX_TIMEOUT 100
114
115
116static const struct dmi_system_id nforce2_dmi_blacklist2[] = {
117 {
118 .ident = "DFI Lanparty NF4 Expert",
119 .matches = {
120 DMI_MATCH(DMI_BOARD_VENDOR, "DFI Corp,LTD"),
121 DMI_MATCH(DMI_BOARD_NAME, "LP UT NF4 Expert"),
122 },
123 },
124 { }
125};
126
127static struct pci_driver nforce2_driver;
128
129
130
131#if defined CONFIG_I2C_NFORCE2_S4985 || defined CONFIG_I2C_NFORCE2_S4985_MODULE
132struct i2c_adapter *nforce2_smbus;
133EXPORT_SYMBOL_GPL(nforce2_smbus);
134
135static void nforce2_set_reference(struct i2c_adapter *adap)
136{
137 nforce2_smbus = adap;
138}
139#else
140static inline void nforce2_set_reference(struct i2c_adapter *adap) { }
141#endif
142
143static void nforce2_abort(struct i2c_adapter *adap)
144{
145 struct nforce2_smbus *smbus = adap->algo_data;
146 int timeout = 0;
147 unsigned char temp;
148
149 dev_dbg(&adap->dev, "Aborting current transaction\n");
150
151 outb_p(NVIDIA_SMB_CTRL_ABORT, NVIDIA_SMB_CTRL);
152 do {
153 msleep(1);
154 temp = inb_p(NVIDIA_SMB_STATUS_ABRT);
155 } while (!(temp & NVIDIA_SMB_STATUS_ABRT_STS) &&
156 (timeout++ < MAX_TIMEOUT));
157 if (!(temp & NVIDIA_SMB_STATUS_ABRT_STS))
158 dev_err(&adap->dev, "Can't reset the smbus\n");
159 outb_p(NVIDIA_SMB_STATUS_ABRT_STS, NVIDIA_SMB_STATUS_ABRT);
160}
161
162static int nforce2_check_status(struct i2c_adapter *adap)
163{
164 struct nforce2_smbus *smbus = adap->algo_data;
165 int timeout = 0;
166 unsigned char temp;
167
168 do {
169 msleep(1);
170 temp = inb_p(NVIDIA_SMB_STS);
171 } while ((!temp) && (timeout++ < MAX_TIMEOUT));
172
173 if (timeout > MAX_TIMEOUT) {
174 dev_dbg(&adap->dev, "SMBus Timeout!\n");
175 if (smbus->can_abort)
176 nforce2_abort(adap);
177 return -ETIMEDOUT;
178 }
179 if (!(temp & NVIDIA_SMB_STS_DONE) || (temp & NVIDIA_SMB_STS_STATUS)) {
180 dev_dbg(&adap->dev, "Transaction failed (0x%02x)!\n", temp);
181 return -EIO;
182 }
183 return 0;
184}
185
186
187static s32 nforce2_access(struct i2c_adapter *adap, u16 addr,
188 unsigned short flags, char read_write,
189 u8 command, int size, union i2c_smbus_data *data)
190{
191 struct nforce2_smbus *smbus = adap->algo_data;
192 unsigned char protocol, pec;
193 u8 len;
194 int i, status;
195
196 protocol = (read_write == I2C_SMBUS_READ) ? NVIDIA_SMB_PRTCL_READ :
197 NVIDIA_SMB_PRTCL_WRITE;
198 pec = (flags & I2C_CLIENT_PEC) ? NVIDIA_SMB_PRTCL_PEC : 0;
199
200 switch (size) {
201 case I2C_SMBUS_QUICK:
202 protocol |= NVIDIA_SMB_PRTCL_QUICK;
203 read_write = I2C_SMBUS_WRITE;
204 break;
205
206 case I2C_SMBUS_BYTE:
207 if (read_write == I2C_SMBUS_WRITE)
208 outb_p(command, NVIDIA_SMB_CMD);
209 protocol |= NVIDIA_SMB_PRTCL_BYTE;
210 break;
211
212 case I2C_SMBUS_BYTE_DATA:
213 outb_p(command, NVIDIA_SMB_CMD);
214 if (read_write == I2C_SMBUS_WRITE)
215 outb_p(data->byte, NVIDIA_SMB_DATA);
216 protocol |= NVIDIA_SMB_PRTCL_BYTE_DATA;
217 break;
218
219 case I2C_SMBUS_WORD_DATA:
220 outb_p(command, NVIDIA_SMB_CMD);
221 if (read_write == I2C_SMBUS_WRITE) {
222 outb_p(data->word, NVIDIA_SMB_DATA);
223 outb_p(data->word >> 8, NVIDIA_SMB_DATA + 1);
224 }
225 protocol |= NVIDIA_SMB_PRTCL_WORD_DATA | pec;
226 break;
227
228 case I2C_SMBUS_BLOCK_DATA:
229 outb_p(command, NVIDIA_SMB_CMD);
230 if (read_write == I2C_SMBUS_WRITE) {
231 len = data->block[0];
232 if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX)) {
233 dev_err(&adap->dev,
234 "Transaction failed (requested block size: %d)\n",
235 len);
236 return -EINVAL;
237 }
238 outb_p(len, NVIDIA_SMB_BCNT);
239 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
240 outb_p(data->block[i + 1],
241 NVIDIA_SMB_DATA + i);
242 }
243 protocol |= NVIDIA_SMB_PRTCL_BLOCK_DATA | pec;
244 break;
245
246 default:
247 dev_err(&adap->dev, "Unsupported transaction %d\n", size);
248 return -EOPNOTSUPP;
249 }
250
251 outb_p((addr & 0x7f) << 1, NVIDIA_SMB_ADDR);
252 outb_p(protocol, NVIDIA_SMB_PRTCL);
253
254 status = nforce2_check_status(adap);
255 if (status)
256 return status;
257
258 if (read_write == I2C_SMBUS_WRITE)
259 return 0;
260
261 switch (size) {
262 case I2C_SMBUS_BYTE:
263 case I2C_SMBUS_BYTE_DATA:
264 data->byte = inb_p(NVIDIA_SMB_DATA);
265 break;
266
267 case I2C_SMBUS_WORD_DATA:
268 data->word = inb_p(NVIDIA_SMB_DATA) |
269 (inb_p(NVIDIA_SMB_DATA + 1) << 8);
270 break;
271
272 case I2C_SMBUS_BLOCK_DATA:
273 len = inb_p(NVIDIA_SMB_BCNT);
274 if ((len <= 0) || (len > I2C_SMBUS_BLOCK_MAX)) {
275 dev_err(&adap->dev,
276 "Transaction failed (received block size: 0x%02x)\n",
277 len);
278 return -EPROTO;
279 }
280 for (i = 0; i < len; i++)
281 data->block[i + 1] = inb_p(NVIDIA_SMB_DATA + i);
282 data->block[0] = len;
283 break;
284 }
285
286 return 0;
287}
288
289
290static u32 nforce2_func(struct i2c_adapter *adapter)
291{
292
293 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
294 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
295 I2C_FUNC_SMBUS_PEC |
296 (((struct nforce2_smbus *)adapter->algo_data)->blockops ?
297 I2C_FUNC_SMBUS_BLOCK_DATA : 0);
298}
299
300static struct i2c_algorithm smbus_algorithm = {
301 .smbus_xfer = nforce2_access,
302 .functionality = nforce2_func,
303};
304
305
306static const struct pci_device_id nforce2_ids[] = {
307 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS) },
308 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SMBUS) },
309 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_SMBUS) },
310 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SMBUS) },
311 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE4_SMBUS) },
312 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SMBUS) },
313 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS) },
314 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS) },
315 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SMBUS) },
316 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP65_SMBUS) },
317 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP67_SMBUS) },
318 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP73_SMBUS) },
319 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP78S_SMBUS) },
320 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP79_SMBUS) },
321 { 0 }
322};
323
324MODULE_DEVICE_TABLE(pci, nforce2_ids);
325
326
327static int nforce2_probe_smb(struct pci_dev *dev, int bar, int alt_reg,
328 struct nforce2_smbus *smbus, const char *name)
329{
330 int error;
331
332 smbus->base = pci_resource_start(dev, bar);
333 if (smbus->base) {
334 smbus->size = pci_resource_len(dev, bar);
335 } else {
336
337 u16 iobase;
338
339 if (pci_read_config_word(dev, alt_reg, &iobase)
340 != PCIBIOS_SUCCESSFUL) {
341 dev_err(&dev->dev, "Error reading PCI config for %s\n",
342 name);
343 return -EIO;
344 }
345
346 smbus->base = iobase & PCI_BASE_ADDRESS_IO_MASK;
347 smbus->size = 64;
348 }
349
350 error = acpi_check_region(smbus->base, smbus->size,
351 nforce2_driver.name);
352 if (error)
353 return error;
354
355 if (!request_region(smbus->base, smbus->size, nforce2_driver.name)) {
356 dev_err(&smbus->adapter.dev, "Error requesting region %02x .. %02X for %s\n",
357 smbus->base, smbus->base+smbus->size-1, name);
358 return -EBUSY;
359 }
360 smbus->adapter.owner = THIS_MODULE;
361 smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
362 smbus->adapter.algo = &smbus_algorithm;
363 smbus->adapter.algo_data = smbus;
364 smbus->adapter.dev.parent = &dev->dev;
365 snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
366 "SMBus nForce2 adapter at %04x", smbus->base);
367
368 error = i2c_add_adapter(&smbus->adapter);
369 if (error) {
370 dev_err(&smbus->adapter.dev, "Failed to register adapter.\n");
371 release_region(smbus->base, smbus->size);
372 return error;
373 }
374 dev_info(&smbus->adapter.dev, "nForce2 SMBus adapter at %#x\n",
375 smbus->base);
376 return 0;
377}
378
379
380static int nforce2_probe(struct pci_dev *dev, const struct pci_device_id *id)
381{
382 struct nforce2_smbus *smbuses;
383 int res1, res2;
384
385
386 smbuses = kzalloc(2 * sizeof(struct nforce2_smbus), GFP_KERNEL);
387 if (!smbuses)
388 return -ENOMEM;
389 pci_set_drvdata(dev, smbuses);
390
391 switch (dev->device) {
392 case PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS:
393 case PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS:
394 case PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS:
395 smbuses[0].blockops = 1;
396 smbuses[1].blockops = 1;
397 smbuses[0].can_abort = 1;
398 smbuses[1].can_abort = 1;
399 }
400
401
402 res1 = nforce2_probe_smb(dev, 4, NFORCE_PCI_SMB1, &smbuses[0], "SMB1");
403 if (res1 < 0)
404 smbuses[0].base = 0;
405
406
407 if (dmi_check_system(nforce2_dmi_blacklist2)) {
408 dev_err(&dev->dev, "Disabling SMB2 for safety reasons.\n");
409 res2 = -EPERM;
410 smbuses[1].base = 0;
411 } else {
412 res2 = nforce2_probe_smb(dev, 5, NFORCE_PCI_SMB2, &smbuses[1],
413 "SMB2");
414 if (res2 < 0)
415 smbuses[1].base = 0;
416 }
417
418 if ((res1 < 0) && (res2 < 0)) {
419
420 kfree(smbuses);
421 return -ENODEV;
422 }
423
424 nforce2_set_reference(&smbuses[0].adapter);
425 return 0;
426}
427
428
429static void nforce2_remove(struct pci_dev *dev)
430{
431 struct nforce2_smbus *smbuses = pci_get_drvdata(dev);
432
433 nforce2_set_reference(NULL);
434 if (smbuses[0].base) {
435 i2c_del_adapter(&smbuses[0].adapter);
436 release_region(smbuses[0].base, smbuses[0].size);
437 }
438 if (smbuses[1].base) {
439 i2c_del_adapter(&smbuses[1].adapter);
440 release_region(smbuses[1].base, smbuses[1].size);
441 }
442 kfree(smbuses);
443}
444
445static struct pci_driver nforce2_driver = {
446 .name = "nForce2_smbus",
447 .id_table = nforce2_ids,
448 .probe = nforce2_probe,
449 .remove = nforce2_remove,
450};
451
452module_pci_driver(nforce2_driver);
453