2 SMBus driver for nVidia nForce2 MCP
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>,
8 SMBus 2.0 driver for AMD-8111 IO-Hub
9 Copyright (c) 2002 Vojtech Pavlik
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.
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.
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.
27 SUPPORTED DEVICES PCI ID
29 nForce2 Ultra 400 MCP 0084
30 nForce3 Pro150 MCP 00D4
31 nForce3 250Gb MCP 00E4
39 This driver supports the 2 SMBuses that are included in the MCP of the
40 nForce2/3/4/5xx chipsets.
43 /* Note: we assume there can only be one nForce2, with two SMBus interfaces */
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>
57 MODULE_LICENSE("GPL");
58 MODULE_AUTHOR ("Hans-Frieder Vogt <hfvogt@gmx.net>");
59 MODULE_DESCRIPTION("nForce2/3/4/5xx SMBus driver");
62 struct nforce2_smbus {
63 struct i2c_adapter adapter;
72 * nVidia nForce2 SMBus control register definitions
73 * (Newer incarnations use standard BARs 4 and 5 instead)
75 #define NFORCE_PCI_SMB1 0x50
76 #define NFORCE_PCI_SMB2 0x54
80 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
82 #define NVIDIA_SMB_PRTCL (smbus->base + 0x00) /* protocol, PEC */
83 #define NVIDIA_SMB_STS (smbus->base + 0x01) /* status */
84 #define NVIDIA_SMB_ADDR (smbus->base + 0x02) /* address */
85 #define NVIDIA_SMB_CMD (smbus->base + 0x03) /* command */
86 #define NVIDIA_SMB_DATA (smbus->base + 0x04) /* 32 data registers */
87 #define NVIDIA_SMB_BCNT (smbus->base + 0x24) /* number of data
89 #define NVIDIA_SMB_STATUS_ABRT (smbus->base + 0x3c) /* register used to
92 #define NVIDIA_SMB_CTRL (smbus->base + 0x3e) /* control register */
94 #define NVIDIA_SMB_STATUS_ABRT_STS 0x01 /* Bit to notify that
96 #define NVIDIA_SMB_CTRL_ABORT 0x20
97 #define NVIDIA_SMB_STS_DONE 0x80
98 #define NVIDIA_SMB_STS_ALRM 0x40
99 #define NVIDIA_SMB_STS_RES 0x20
100 #define NVIDIA_SMB_STS_STATUS 0x1f
102 #define NVIDIA_SMB_PRTCL_WRITE 0x00
103 #define NVIDIA_SMB_PRTCL_READ 0x01
104 #define NVIDIA_SMB_PRTCL_QUICK 0x02
105 #define NVIDIA_SMB_PRTCL_BYTE 0x04
106 #define NVIDIA_SMB_PRTCL_BYTE_DATA 0x06
107 #define NVIDIA_SMB_PRTCL_WORD_DATA 0x08
108 #define NVIDIA_SMB_PRTCL_BLOCK_DATA 0x0a
109 #define NVIDIA_SMB_PRTCL_PEC 0x80
111 /* Misc definitions */
112 #define MAX_TIMEOUT 100
114 /* We disable the second SMBus channel on these boards */
115 static struct dmi_system_id __devinitdata nforce2_dmi_blacklist2[] = {
117 .ident = "DFI Lanparty NF4 Expert",
119 DMI_MATCH(DMI_BOARD_VENDOR, "DFI Corp,LTD"),
120 DMI_MATCH(DMI_BOARD_NAME, "LP UT NF4 Expert"),
126 static struct pci_driver nforce2_driver;
128 /* For multiplexing support, we need a global reference to the 1st
130 #if defined CONFIG_I2C_NFORCE2_S4985 || defined CONFIG_I2C_NFORCE2_S4985_MODULE
131 struct i2c_adapter *nforce2_smbus;
132 EXPORT_SYMBOL_GPL(nforce2_smbus);
134 static void nforce2_set_reference(struct i2c_adapter *adap)
136 nforce2_smbus = adap;
139 static inline void nforce2_set_reference(struct i2c_adapter *adap) { }
142 static void nforce2_abort(struct i2c_adapter *adap)
144 struct nforce2_smbus *smbus = adap->algo_data;
148 dev_dbg(&adap->dev, "Aborting current transaction\n");
150 outb_p(NVIDIA_SMB_CTRL_ABORT, NVIDIA_SMB_CTRL);
153 temp = inb_p(NVIDIA_SMB_STATUS_ABRT);
154 } while (!(temp & NVIDIA_SMB_STATUS_ABRT_STS) &&
155 (timeout++ < MAX_TIMEOUT));
156 if (!(temp & NVIDIA_SMB_STATUS_ABRT_STS))
157 dev_err(&adap->dev, "Can't reset the smbus\n");
158 outb_p(NVIDIA_SMB_STATUS_ABRT_STS, NVIDIA_SMB_STATUS_ABRT);
161 static int nforce2_check_status(struct i2c_adapter *adap)
163 struct nforce2_smbus *smbus = adap->algo_data;
169 temp = inb_p(NVIDIA_SMB_STS);
170 } while ((!temp) && (timeout++ < MAX_TIMEOUT));
172 if (timeout >= MAX_TIMEOUT) {
173 dev_dbg(&adap->dev, "SMBus Timeout!\n");
174 if (smbus->can_abort)
178 if (!(temp & NVIDIA_SMB_STS_DONE) || (temp & NVIDIA_SMB_STS_STATUS)) {
179 dev_dbg(&adap->dev, "Transaction failed (0x%02x)!\n", temp);
185 /* Return negative errno on error */
186 static s32 nforce2_access(struct i2c_adapter * adap, u16 addr,
187 unsigned short flags, char read_write,
188 u8 command, int size, union i2c_smbus_data * data)
190 struct nforce2_smbus *smbus = adap->algo_data;
191 unsigned char protocol, pec;
195 protocol = (read_write == I2C_SMBUS_READ) ? NVIDIA_SMB_PRTCL_READ :
196 NVIDIA_SMB_PRTCL_WRITE;
197 pec = (flags & I2C_CLIENT_PEC) ? NVIDIA_SMB_PRTCL_PEC : 0;
201 case I2C_SMBUS_QUICK:
202 protocol |= NVIDIA_SMB_PRTCL_QUICK;
203 read_write = I2C_SMBUS_WRITE;
207 if (read_write == I2C_SMBUS_WRITE)
208 outb_p(command, NVIDIA_SMB_CMD);
209 protocol |= NVIDIA_SMB_PRTCL_BYTE;
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;
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);
225 protocol |= NVIDIA_SMB_PRTCL_WORD_DATA | pec;
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)) {
234 "Transaction failed "
235 "(requested block size: %d)\n",
239 outb_p(len, NVIDIA_SMB_BCNT);
240 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
241 outb_p(data->block[i + 1],
244 protocol |= NVIDIA_SMB_PRTCL_BLOCK_DATA | pec;
248 dev_err(&adap->dev, "Unsupported transaction %d\n", size);
252 outb_p((addr & 0x7f) << 1, NVIDIA_SMB_ADDR);
253 outb_p(protocol, NVIDIA_SMB_PRTCL);
255 status = nforce2_check_status(adap);
259 if (read_write == I2C_SMBUS_WRITE)
265 case I2C_SMBUS_BYTE_DATA:
266 data->byte = inb_p(NVIDIA_SMB_DATA);
269 case I2C_SMBUS_WORD_DATA:
270 data->word = inb_p(NVIDIA_SMB_DATA) | (inb_p(NVIDIA_SMB_DATA+1) << 8);
273 case I2C_SMBUS_BLOCK_DATA:
274 len = inb_p(NVIDIA_SMB_BCNT);
275 if ((len <= 0) || (len > I2C_SMBUS_BLOCK_MAX)) {
276 dev_err(&adap->dev, "Transaction failed "
277 "(received block size: 0x%02x)\n",
281 for (i = 0; i < len; i++)
282 data->block[i+1] = inb_p(NVIDIA_SMB_DATA + i);
283 data->block[0] = len;
291 static u32 nforce2_func(struct i2c_adapter *adapter)
293 /* other functionality might be possible, but is not tested */
294 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
295 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
297 (((struct nforce2_smbus*)adapter->algo_data)->blockops ?
298 I2C_FUNC_SMBUS_BLOCK_DATA : 0);
301 static struct i2c_algorithm smbus_algorithm = {
302 .smbus_xfer = nforce2_access,
303 .functionality = nforce2_func,
307 static struct pci_device_id nforce2_ids[] = {
308 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS) },
309 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SMBUS) },
310 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_SMBUS) },
311 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SMBUS) },
312 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE4_SMBUS) },
313 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SMBUS) },
314 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS) },
315 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS) },
316 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SMBUS) },
317 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP65_SMBUS) },
321 MODULE_DEVICE_TABLE (pci, nforce2_ids);
324 static int __devinit nforce2_probe_smb (struct pci_dev *dev, int bar,
325 int alt_reg, struct nforce2_smbus *smbus, const char *name)
329 smbus->base = pci_resource_start(dev, bar);
331 smbus->size = pci_resource_len(dev, bar);
333 /* Older incarnations of the device used non-standard BARs */
336 if (pci_read_config_word(dev, alt_reg, &iobase)
337 != PCIBIOS_SUCCESSFUL) {
338 dev_err(&dev->dev, "Error reading PCI config for %s\n",
343 smbus->base = iobase & PCI_BASE_ADDRESS_IO_MASK;
347 error = acpi_check_region(smbus->base, smbus->size,
348 nforce2_driver.name);
352 if (!request_region(smbus->base, smbus->size, nforce2_driver.name)) {
353 dev_err(&smbus->adapter.dev, "Error requesting region %02x .. %02X for %s\n",
354 smbus->base, smbus->base+smbus->size-1, name);
357 smbus->adapter.owner = THIS_MODULE;
358 smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
359 smbus->adapter.algo = &smbus_algorithm;
360 smbus->adapter.algo_data = smbus;
361 smbus->adapter.dev.parent = &dev->dev;
362 snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
363 "SMBus nForce2 adapter at %04x", smbus->base);
365 error = i2c_add_adapter(&smbus->adapter);
367 dev_err(&smbus->adapter.dev, "Failed to register adapter.\n");
368 release_region(smbus->base, smbus->size);
371 dev_info(&smbus->adapter.dev, "nForce2 SMBus adapter at %#x\n", smbus->base);
376 static int __devinit nforce2_probe(struct pci_dev *dev, const struct pci_device_id *id)
378 struct nforce2_smbus *smbuses;
381 /* we support 2 SMBus adapters */
382 if (!(smbuses = kzalloc(2*sizeof(struct nforce2_smbus), GFP_KERNEL)))
384 pci_set_drvdata(dev, smbuses);
386 switch(dev->device) {
387 case PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS:
388 case PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS:
389 case PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS:
390 smbuses[0].blockops = 1;
391 smbuses[1].blockops = 1;
392 smbuses[0].can_abort = 1;
393 smbuses[1].can_abort = 1;
396 /* SMBus adapter 1 */
397 res1 = nforce2_probe_smb(dev, 4, NFORCE_PCI_SMB1, &smbuses[0], "SMB1");
399 dev_err(&dev->dev, "Error probing SMB1.\n");
400 smbuses[0].base = 0; /* to have a check value */
402 /* SMBus adapter 2 */
403 if (dmi_check_system(nforce2_dmi_blacklist2)) {
404 dev_err(&dev->dev, "Disabling SMB2 for safety reasons.\n");
408 res2 = nforce2_probe_smb(dev, 5, NFORCE_PCI_SMB2, &smbuses[1],
411 dev_err(&dev->dev, "Error probing SMB2.\n");
412 smbuses[1].base = 0; /* to have a check value */
415 if ((res1 < 0) && (res2 < 0)) {
416 /* we did not find even one of the SMBuses, so we give up */
421 nforce2_set_reference(&smbuses[0].adapter);
426 static void __devexit nforce2_remove(struct pci_dev *dev)
428 struct nforce2_smbus *smbuses = (void*) pci_get_drvdata(dev);
430 nforce2_set_reference(NULL);
431 if (smbuses[0].base) {
432 i2c_del_adapter(&smbuses[0].adapter);
433 release_region(smbuses[0].base, smbuses[0].size);
435 if (smbuses[1].base) {
436 i2c_del_adapter(&smbuses[1].adapter);
437 release_region(smbuses[1].base, smbuses[1].size);
442 static struct pci_driver nforce2_driver = {
443 .name = "nForce2_smbus",
444 .id_table = nforce2_ids,
445 .probe = nforce2_probe,
446 .remove = __devexit_p(nforce2_remove),
449 static int __init nforce2_init(void)
451 return pci_register_driver(&nforce2_driver);
454 static void __exit nforce2_exit(void)
456 pci_unregister_driver(&nforce2_driver);
459 module_init(nforce2_init);
460 module_exit(nforce2_exit);