Merge branch 'tracing-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.git] / drivers / i2c / busses / i2c-nforce2.c
CommitLineData
1da177e4
LT
1/*
2 SMBus driver for nVidia nForce2 MCP
3
4 Added nForce3 Pro 150 Thomas Leibold <thomas@plx.com>,
5 Ported to 2.5 Patrick Dreker <patrick@dreker.de>,
6 Copyright (c) 2003 Hans-Frieder Vogt <hfvogt@arcor.de>,
7 Based on
8 SMBus 2.0 driver for AMD-8111 IO-Hub
9 Copyright (c) 2002 Vojtech Pavlik
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24*/
25
26/*
27 SUPPORTED DEVICES PCI ID
28 nForce2 MCP 0064
29 nForce2 Ultra 400 MCP 0084
30 nForce3 Pro150 MCP 00D4
31 nForce3 250Gb MCP 00E4
32 nForce4 MCP 0052
7c72ccf0 33 nForce4 MCP-04 0034
d2dd14ac
JD
34 nForce MCP51 0264
35 nForce MCP55 0368
f75803de
JD
36 nForce MCP61 03EB
37 nForce MCP65 0446
d2dd14ac
JD
38 nForce MCP67 0542
39 nForce MCP73 07D8
40 nForce MCP78S 0752
41 nForce MCP79 0AA2
1da177e4
LT
42
43 This driver supports the 2 SMBuses that are included in the MCP of the
ad04d5c3 44 nForce2/3/4/5xx chipsets.
1da177e4
LT
45*/
46
47/* Note: we assume there can only be one nForce2, with two SMBus interfaces */
48
1da177e4
LT
49#include <linux/module.h>
50#include <linux/pci.h>
51#include <linux/kernel.h>
52#include <linux/stddef.h>
1da177e4
LT
53#include <linux/ioport.h>
54#include <linux/init.h>
55#include <linux/i2c.h>
56#include <linux/delay.h>
08851d6e 57#include <linux/dmi.h>
54fb4a05 58#include <linux/acpi.h>
1da177e4
LT
59#include <asm/io.h>
60
61MODULE_LICENSE("GPL");
ad04d5c3
HFV
62MODULE_AUTHOR ("Hans-Frieder Vogt <hfvogt@gmx.net>");
63MODULE_DESCRIPTION("nForce2/3/4/5xx SMBus driver");
1da177e4
LT
64
65
66struct nforce2_smbus {
1da177e4
LT
67 struct i2c_adapter adapter;
68 int base;
69 int size;
b53c8221 70 int blockops;
d49584c4 71 int can_abort;
1da177e4
LT
72};
73
74
75/*
76 * nVidia nForce2 SMBus control register definitions
5c7ae658 77 * (Newer incarnations use standard BARs 4 and 5 instead)
1da177e4
LT
78 */
79#define NFORCE_PCI_SMB1 0x50
80#define NFORCE_PCI_SMB2 0x54
81
82
83/*
84 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
85 */
86#define NVIDIA_SMB_PRTCL (smbus->base + 0x00) /* protocol, PEC */
87#define NVIDIA_SMB_STS (smbus->base + 0x01) /* status */
88#define NVIDIA_SMB_ADDR (smbus->base + 0x02) /* address */
89#define NVIDIA_SMB_CMD (smbus->base + 0x03) /* command */
90#define NVIDIA_SMB_DATA (smbus->base + 0x04) /* 32 data registers */
b53c8221
OR
91#define NVIDIA_SMB_BCNT (smbus->base + 0x24) /* number of data
92 bytes */
d49584c4
OR
93#define NVIDIA_SMB_STATUS_ABRT (smbus->base + 0x3c) /* register used to
94 check the status of
95 the abort command */
96#define NVIDIA_SMB_CTRL (smbus->base + 0x3e) /* control register */
97
98#define NVIDIA_SMB_STATUS_ABRT_STS 0x01 /* Bit to notify that
99 abort succeeded */
100#define NVIDIA_SMB_CTRL_ABORT 0x20
1da177e4
LT
101#define NVIDIA_SMB_STS_DONE 0x80
102#define NVIDIA_SMB_STS_ALRM 0x40
103#define NVIDIA_SMB_STS_RES 0x20
104#define NVIDIA_SMB_STS_STATUS 0x1f
105
106#define NVIDIA_SMB_PRTCL_WRITE 0x00
107#define NVIDIA_SMB_PRTCL_READ 0x01
108#define NVIDIA_SMB_PRTCL_QUICK 0x02
109#define NVIDIA_SMB_PRTCL_BYTE 0x04
110#define NVIDIA_SMB_PRTCL_BYTE_DATA 0x06
111#define NVIDIA_SMB_PRTCL_WORD_DATA 0x08
b53c8221 112#define NVIDIA_SMB_PRTCL_BLOCK_DATA 0x0a
1da177e4
LT
113#define NVIDIA_SMB_PRTCL_PEC 0x80
114
41535497
OR
115/* Misc definitions */
116#define MAX_TIMEOUT 100
117
08851d6e
JD
118/* We disable the second SMBus channel on these boards */
119static struct dmi_system_id __devinitdata nforce2_dmi_blacklist2[] = {
120 {
121 .ident = "DFI Lanparty NF4 Expert",
122 .matches = {
123 DMI_MATCH(DMI_BOARD_VENDOR, "DFI Corp,LTD"),
124 DMI_MATCH(DMI_BOARD_NAME, "LP UT NF4 Expert"),
125 },
126 },
127 { }
128};
129
d6072f84 130static struct pci_driver nforce2_driver;
1da177e4 131
279e9024
JD
132/* For multiplexing support, we need a global reference to the 1st
133 SMBus channel */
134#if defined CONFIG_I2C_NFORCE2_S4985 || defined CONFIG_I2C_NFORCE2_S4985_MODULE
135struct i2c_adapter *nforce2_smbus;
136EXPORT_SYMBOL_GPL(nforce2_smbus);
137
138static void nforce2_set_reference(struct i2c_adapter *adap)
139{
140 nforce2_smbus = adap;
141}
142#else
143static inline void nforce2_set_reference(struct i2c_adapter *adap) { }
144#endif
145
d49584c4
OR
146static void nforce2_abort(struct i2c_adapter *adap)
147{
148 struct nforce2_smbus *smbus = adap->algo_data;
149 int timeout = 0;
150 unsigned char temp;
151
152 dev_dbg(&adap->dev, "Aborting current transaction\n");
153
154 outb_p(NVIDIA_SMB_CTRL_ABORT, NVIDIA_SMB_CTRL);
155 do {
156 msleep(1);
157 temp = inb_p(NVIDIA_SMB_STATUS_ABRT);
158 } while (!(temp & NVIDIA_SMB_STATUS_ABRT_STS) &&
159 (timeout++ < MAX_TIMEOUT));
160 if (!(temp & NVIDIA_SMB_STATUS_ABRT_STS))
161 dev_err(&adap->dev, "Can't reset the smbus\n");
162 outb_p(NVIDIA_SMB_STATUS_ABRT_STS, NVIDIA_SMB_STATUS_ABRT);
163}
164
41535497
OR
165static int nforce2_check_status(struct i2c_adapter *adap)
166{
167 struct nforce2_smbus *smbus = adap->algo_data;
168 int timeout = 0;
169 unsigned char temp;
170
171 do {
172 msleep(1);
173 temp = inb_p(NVIDIA_SMB_STS);
174 } while ((!temp) && (timeout++ < MAX_TIMEOUT));
175
4ccc28f7 176 if (timeout > MAX_TIMEOUT) {
41535497 177 dev_dbg(&adap->dev, "SMBus Timeout!\n");
d49584c4
OR
178 if (smbus->can_abort)
179 nforce2_abort(adap);
97140342 180 return -ETIMEDOUT;
41535497
OR
181 }
182 if (!(temp & NVIDIA_SMB_STS_DONE) || (temp & NVIDIA_SMB_STS_STATUS)) {
183 dev_dbg(&adap->dev, "Transaction failed (0x%02x)!\n", temp);
97140342 184 return -EIO;
41535497
OR
185 }
186 return 0;
187}
188
97140342 189/* Return negative errno on error */
1da177e4
LT
190static s32 nforce2_access(struct i2c_adapter * adap, u16 addr,
191 unsigned short flags, char read_write,
192 u8 command, int size, union i2c_smbus_data * data)
193{
194 struct nforce2_smbus *smbus = adap->algo_data;
41535497 195 unsigned char protocol, pec;
b53c8221 196 u8 len;
97140342 197 int i, status;
1da177e4
LT
198
199 protocol = (read_write == I2C_SMBUS_READ) ? NVIDIA_SMB_PRTCL_READ :
200 NVIDIA_SMB_PRTCL_WRITE;
201 pec = (flags & I2C_CLIENT_PEC) ? NVIDIA_SMB_PRTCL_PEC : 0;
202
203 switch (size) {
204
205 case I2C_SMBUS_QUICK:
206 protocol |= NVIDIA_SMB_PRTCL_QUICK;
207 read_write = I2C_SMBUS_WRITE;
208 break;
209
210 case I2C_SMBUS_BYTE:
211 if (read_write == I2C_SMBUS_WRITE)
212 outb_p(command, NVIDIA_SMB_CMD);
213 protocol |= NVIDIA_SMB_PRTCL_BYTE;
214 break;
215
216 case I2C_SMBUS_BYTE_DATA:
217 outb_p(command, NVIDIA_SMB_CMD);
218 if (read_write == I2C_SMBUS_WRITE)
219 outb_p(data->byte, NVIDIA_SMB_DATA);
220 protocol |= NVIDIA_SMB_PRTCL_BYTE_DATA;
221 break;
222
223 case I2C_SMBUS_WORD_DATA:
224 outb_p(command, NVIDIA_SMB_CMD);
225 if (read_write == I2C_SMBUS_WRITE) {
226 outb_p(data->word, NVIDIA_SMB_DATA);
227 outb_p(data->word >> 8, NVIDIA_SMB_DATA+1);
228 }
229 protocol |= NVIDIA_SMB_PRTCL_WORD_DATA | pec;
230 break;
231
b53c8221
OR
232 case I2C_SMBUS_BLOCK_DATA:
233 outb_p(command, NVIDIA_SMB_CMD);
234 if (read_write == I2C_SMBUS_WRITE) {
235 len = data->block[0];
236 if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX)) {
237 dev_err(&adap->dev,
238 "Transaction failed "
239 "(requested block size: %d)\n",
240 len);
97140342 241 return -EINVAL;
b53c8221
OR
242 }
243 outb_p(len, NVIDIA_SMB_BCNT);
244 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
245 outb_p(data->block[i + 1],
246 NVIDIA_SMB_DATA+i);
247 }
248 protocol |= NVIDIA_SMB_PRTCL_BLOCK_DATA | pec;
249 break;
250
1da177e4
LT
251 default:
252 dev_err(&adap->dev, "Unsupported transaction %d\n", size);
97140342 253 return -EOPNOTSUPP;
1da177e4
LT
254 }
255
256 outb_p((addr & 0x7f) << 1, NVIDIA_SMB_ADDR);
257 outb_p(protocol, NVIDIA_SMB_PRTCL);
258
97140342
DB
259 status = nforce2_check_status(adap);
260 if (status)
261 return status;
1da177e4
LT
262
263 if (read_write == I2C_SMBUS_WRITE)
264 return 0;
265
266 switch (size) {
267
268 case I2C_SMBUS_BYTE:
269 case I2C_SMBUS_BYTE_DATA:
270 data->byte = inb_p(NVIDIA_SMB_DATA);
271 break;
272
273 case I2C_SMBUS_WORD_DATA:
1da177e4
LT
274 data->word = inb_p(NVIDIA_SMB_DATA) | (inb_p(NVIDIA_SMB_DATA+1) << 8);
275 break;
b53c8221
OR
276
277 case I2C_SMBUS_BLOCK_DATA:
278 len = inb_p(NVIDIA_SMB_BCNT);
41535497
OR
279 if ((len <= 0) || (len > I2C_SMBUS_BLOCK_MAX)) {
280 dev_err(&adap->dev, "Transaction failed "
281 "(received block size: 0x%02x)\n",
282 len);
97140342 283 return -EPROTO;
41535497 284 }
b53c8221
OR
285 for (i = 0; i < len; i++)
286 data->block[i+1] = inb_p(NVIDIA_SMB_DATA + i);
287 data->block[0] = len;
288 break;
1da177e4
LT
289 }
290
291 return 0;
292}
293
294
295static u32 nforce2_func(struct i2c_adapter *adapter)
296{
297 /* other functionality might be possible, but is not tested */
298 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
b53c8221 299 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
ac3f5753 300 I2C_FUNC_SMBUS_PEC |
b53c8221
OR
301 (((struct nforce2_smbus*)adapter->algo_data)->blockops ?
302 I2C_FUNC_SMBUS_BLOCK_DATA : 0);
1da177e4
LT
303}
304
ad04d5c3
HFV
305static struct i2c_algorithm smbus_algorithm = {
306 .smbus_xfer = nforce2_access,
307 .functionality = nforce2_func,
308};
309
1da177e4
LT
310
311static struct pci_device_id nforce2_ids[] = {
312 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS) },
313 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SMBUS) },
314 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_SMBUS) },
315 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SMBUS) },
316 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE4_SMBUS) },
7c72ccf0 317 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SMBUS) },
5c7ae658
JD
318 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS) },
319 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS) },
f75803de
JD
320 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SMBUS) },
321 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP65_SMBUS) },
d2dd14ac
JD
322 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP67_SMBUS) },
323 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP73_SMBUS) },
324 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP78S_SMBUS) },
325 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP79_SMBUS) },
1da177e4
LT
326 { 0 }
327};
328
1da177e4
LT
329MODULE_DEVICE_TABLE (pci, nforce2_ids);
330
331
5c7ae658
JD
332static int __devinit nforce2_probe_smb (struct pci_dev *dev, int bar,
333 int alt_reg, struct nforce2_smbus *smbus, const char *name)
1da177e4 334{
1da177e4
LT
335 int error;
336
5c7ae658
JD
337 smbus->base = pci_resource_start(dev, bar);
338 if (smbus->base) {
339 smbus->size = pci_resource_len(dev, bar);
340 } else {
341 /* Older incarnations of the device used non-standard BARs */
342 u16 iobase;
343
344 if (pci_read_config_word(dev, alt_reg, &iobase)
345 != PCIBIOS_SUCCESSFUL) {
346 dev_err(&dev->dev, "Error reading PCI config for %s\n",
347 name);
97140342 348 return -EIO;
5c7ae658
JD
349 }
350
351 smbus->base = iobase & PCI_BASE_ADDRESS_IO_MASK;
ad04d5c3 352 smbus->size = 64;
1da177e4 353 }
1da177e4 354
54fb4a05
JD
355 error = acpi_check_region(smbus->base, smbus->size,
356 nforce2_driver.name);
357 if (error)
358 return -1;
359
d6072f84 360 if (!request_region(smbus->base, smbus->size, nforce2_driver.name)) {
1da177e4
LT
361 dev_err(&smbus->adapter.dev, "Error requesting region %02x .. %02X for %s\n",
362 smbus->base, smbus->base+smbus->size-1, name);
97140342 363 return -EBUSY;
1da177e4 364 }
ad04d5c3 365 smbus->adapter.owner = THIS_MODULE;
3401b2ff 366 smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
ad04d5c3 367 smbus->adapter.algo = &smbus_algorithm;
1da177e4
LT
368 smbus->adapter.algo_data = smbus;
369 smbus->adapter.dev.parent = &dev->dev;
2096b956 370 snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
1da177e4
LT
371 "SMBus nForce2 adapter at %04x", smbus->base);
372
373 error = i2c_add_adapter(&smbus->adapter);
374 if (error) {
375 dev_err(&smbus->adapter.dev, "Failed to register adapter.\n");
376 release_region(smbus->base, smbus->size);
97140342 377 return error;
1da177e4
LT
378 }
379 dev_info(&smbus->adapter.dev, "nForce2 SMBus adapter at %#x\n", smbus->base);
380 return 0;
381}
382
383
384static int __devinit nforce2_probe(struct pci_dev *dev, const struct pci_device_id *id)
385{
386 struct nforce2_smbus *smbuses;
387 int res1, res2;
388
389 /* we support 2 SMBus adapters */
078d9fe4 390 if (!(smbuses = kzalloc(2*sizeof(struct nforce2_smbus), GFP_KERNEL)))
1da177e4 391 return -ENOMEM;
1da177e4
LT
392 pci_set_drvdata(dev, smbuses);
393
b53c8221 394 switch(dev->device) {
541b6a7a 395 case PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS:
b53c8221
OR
396 case PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS:
397 case PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS:
398 smbuses[0].blockops = 1;
399 smbuses[1].blockops = 1;
d49584c4
OR
400 smbuses[0].can_abort = 1;
401 smbuses[1].can_abort = 1;
b53c8221
OR
402 }
403
1da177e4 404 /* SMBus adapter 1 */
5c7ae658 405 res1 = nforce2_probe_smb(dev, 4, NFORCE_PCI_SMB1, &smbuses[0], "SMB1");
1da177e4
LT
406 if (res1 < 0) {
407 dev_err(&dev->dev, "Error probing SMB1.\n");
408 smbuses[0].base = 0; /* to have a check value */
409 }
5c7ae658 410 /* SMBus adapter 2 */
08851d6e
JD
411 if (dmi_check_system(nforce2_dmi_blacklist2)) {
412 dev_err(&dev->dev, "Disabling SMB2 for safety reasons.\n");
413 res2 = -EPERM;
414 smbuses[1].base = 0;
415 } else {
416 res2 = nforce2_probe_smb(dev, 5, NFORCE_PCI_SMB2, &smbuses[1],
417 "SMB2");
418 if (res2 < 0) {
419 dev_err(&dev->dev, "Error probing SMB2.\n");
420 smbuses[1].base = 0; /* to have a check value */
421 }
1da177e4
LT
422 }
423 if ((res1 < 0) && (res2 < 0)) {
424 /* we did not find even one of the SMBuses, so we give up */
425 kfree(smbuses);
426 return -ENODEV;
427 }
428
279e9024 429 nforce2_set_reference(&smbuses[0].adapter);
1da177e4
LT
430 return 0;
431}
432
433
434static void __devexit nforce2_remove(struct pci_dev *dev)
435{
436 struct nforce2_smbus *smbuses = (void*) pci_get_drvdata(dev);
437
279e9024 438 nforce2_set_reference(NULL);
1da177e4
LT
439 if (smbuses[0].base) {
440 i2c_del_adapter(&smbuses[0].adapter);
441 release_region(smbuses[0].base, smbuses[0].size);
442 }
443 if (smbuses[1].base) {
444 i2c_del_adapter(&smbuses[1].adapter);
445 release_region(smbuses[1].base, smbuses[1].size);
446 }
447 kfree(smbuses);
448}
449
450static struct pci_driver nforce2_driver = {
451 .name = "nForce2_smbus",
452 .id_table = nforce2_ids,
453 .probe = nforce2_probe,
454 .remove = __devexit_p(nforce2_remove),
455};
456
457static int __init nforce2_init(void)
458{
459 return pci_register_driver(&nforce2_driver);
460}
461
462static void __exit nforce2_exit(void)
463{
464 pci_unregister_driver(&nforce2_driver);
465}
466
467module_init(nforce2_init);
468module_exit(nforce2_exit);
469
This page took 0.553885 seconds and 5 git commands to generate.