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/acpi.h>
56 MODULE_LICENSE("GPL");
57 MODULE_AUTHOR ("Hans-Frieder Vogt <hfvogt@gmx.net>");
58 MODULE_DESCRIPTION("nForce2/3/4/5xx SMBus driver");
61 struct nforce2_smbus {
62 struct i2c_adapter adapter;
71 * nVidia nForce2 SMBus control register definitions
72 * (Newer incarnations use standard BARs 4 and 5 instead)
74 #define NFORCE_PCI_SMB1 0x50
75 #define NFORCE_PCI_SMB2 0x54
79 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
81 #define NVIDIA_SMB_PRTCL (smbus->base + 0x00) /* protocol, PEC */
82 #define NVIDIA_SMB_STS (smbus->base + 0x01) /* status */
83 #define NVIDIA_SMB_ADDR (smbus->base + 0x02) /* address */
84 #define NVIDIA_SMB_CMD (smbus->base + 0x03) /* command */
85 #define NVIDIA_SMB_DATA (smbus->base + 0x04) /* 32 data registers */
86 #define NVIDIA_SMB_BCNT (smbus->base + 0x24) /* number of data
88 #define NVIDIA_SMB_STATUS_ABRT (smbus->base + 0x3c) /* register used to
91 #define NVIDIA_SMB_CTRL (smbus->base + 0x3e) /* control register */
93 #define NVIDIA_SMB_STATUS_ABRT_STS 0x01 /* Bit to notify that
95 #define NVIDIA_SMB_CTRL_ABORT 0x20
96 #define NVIDIA_SMB_STS_DONE 0x80
97 #define NVIDIA_SMB_STS_ALRM 0x40
98 #define NVIDIA_SMB_STS_RES 0x20
99 #define NVIDIA_SMB_STS_STATUS 0x1f
101 #define NVIDIA_SMB_PRTCL_WRITE 0x00
102 #define NVIDIA_SMB_PRTCL_READ 0x01
103 #define NVIDIA_SMB_PRTCL_QUICK 0x02
104 #define NVIDIA_SMB_PRTCL_BYTE 0x04
105 #define NVIDIA_SMB_PRTCL_BYTE_DATA 0x06
106 #define NVIDIA_SMB_PRTCL_WORD_DATA 0x08
107 #define NVIDIA_SMB_PRTCL_BLOCK_DATA 0x0a
108 #define NVIDIA_SMB_PRTCL_PEC 0x80
110 /* Misc definitions */
111 #define MAX_TIMEOUT 100
113 static struct pci_driver nforce2_driver;
115 static void nforce2_abort(struct i2c_adapter *adap)
117 struct nforce2_smbus *smbus = adap->algo_data;
121 dev_dbg(&adap->dev, "Aborting current transaction\n");
123 outb_p(NVIDIA_SMB_CTRL_ABORT, NVIDIA_SMB_CTRL);
126 temp = inb_p(NVIDIA_SMB_STATUS_ABRT);
127 } while (!(temp & NVIDIA_SMB_STATUS_ABRT_STS) &&
128 (timeout++ < MAX_TIMEOUT));
129 if (!(temp & NVIDIA_SMB_STATUS_ABRT_STS))
130 dev_err(&adap->dev, "Can't reset the smbus\n");
131 outb_p(NVIDIA_SMB_STATUS_ABRT_STS, NVIDIA_SMB_STATUS_ABRT);
134 static int nforce2_check_status(struct i2c_adapter *adap)
136 struct nforce2_smbus *smbus = adap->algo_data;
142 temp = inb_p(NVIDIA_SMB_STS);
143 } while ((!temp) && (timeout++ < MAX_TIMEOUT));
145 if (timeout >= MAX_TIMEOUT) {
146 dev_dbg(&adap->dev, "SMBus Timeout!\n");
147 if (smbus->can_abort)
151 if (!(temp & NVIDIA_SMB_STS_DONE) || (temp & NVIDIA_SMB_STS_STATUS)) {
152 dev_dbg(&adap->dev, "Transaction failed (0x%02x)!\n", temp);
158 /* Return -1 on error */
159 static s32 nforce2_access(struct i2c_adapter * adap, u16 addr,
160 unsigned short flags, char read_write,
161 u8 command, int size, union i2c_smbus_data * data)
163 struct nforce2_smbus *smbus = adap->algo_data;
164 unsigned char protocol, pec;
168 protocol = (read_write == I2C_SMBUS_READ) ? NVIDIA_SMB_PRTCL_READ :
169 NVIDIA_SMB_PRTCL_WRITE;
170 pec = (flags & I2C_CLIENT_PEC) ? NVIDIA_SMB_PRTCL_PEC : 0;
174 case I2C_SMBUS_QUICK:
175 protocol |= NVIDIA_SMB_PRTCL_QUICK;
176 read_write = I2C_SMBUS_WRITE;
180 if (read_write == I2C_SMBUS_WRITE)
181 outb_p(command, NVIDIA_SMB_CMD);
182 protocol |= NVIDIA_SMB_PRTCL_BYTE;
185 case I2C_SMBUS_BYTE_DATA:
186 outb_p(command, NVIDIA_SMB_CMD);
187 if (read_write == I2C_SMBUS_WRITE)
188 outb_p(data->byte, NVIDIA_SMB_DATA);
189 protocol |= NVIDIA_SMB_PRTCL_BYTE_DATA;
192 case I2C_SMBUS_WORD_DATA:
193 outb_p(command, NVIDIA_SMB_CMD);
194 if (read_write == I2C_SMBUS_WRITE) {
195 outb_p(data->word, NVIDIA_SMB_DATA);
196 outb_p(data->word >> 8, NVIDIA_SMB_DATA+1);
198 protocol |= NVIDIA_SMB_PRTCL_WORD_DATA | pec;
201 case I2C_SMBUS_BLOCK_DATA:
202 outb_p(command, NVIDIA_SMB_CMD);
203 if (read_write == I2C_SMBUS_WRITE) {
204 len = data->block[0];
205 if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX)) {
207 "Transaction failed "
208 "(requested block size: %d)\n",
212 outb_p(len, NVIDIA_SMB_BCNT);
213 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
214 outb_p(data->block[i + 1],
217 protocol |= NVIDIA_SMB_PRTCL_BLOCK_DATA | pec;
221 dev_err(&adap->dev, "Unsupported transaction %d\n", size);
225 outb_p((addr & 0x7f) << 1, NVIDIA_SMB_ADDR);
226 outb_p(protocol, NVIDIA_SMB_PRTCL);
228 if (nforce2_check_status(adap))
231 if (read_write == I2C_SMBUS_WRITE)
237 case I2C_SMBUS_BYTE_DATA:
238 data->byte = inb_p(NVIDIA_SMB_DATA);
241 case I2C_SMBUS_WORD_DATA:
242 data->word = inb_p(NVIDIA_SMB_DATA) | (inb_p(NVIDIA_SMB_DATA+1) << 8);
245 case I2C_SMBUS_BLOCK_DATA:
246 len = inb_p(NVIDIA_SMB_BCNT);
247 if ((len <= 0) || (len > I2C_SMBUS_BLOCK_MAX)) {
248 dev_err(&adap->dev, "Transaction failed "
249 "(received block size: 0x%02x)\n",
253 for (i = 0; i < len; i++)
254 data->block[i+1] = inb_p(NVIDIA_SMB_DATA + i);
255 data->block[0] = len;
263 static u32 nforce2_func(struct i2c_adapter *adapter)
265 /* other functionality might be possible, but is not tested */
266 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
267 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
269 (((struct nforce2_smbus*)adapter->algo_data)->blockops ?
270 I2C_FUNC_SMBUS_BLOCK_DATA : 0);
273 static struct i2c_algorithm smbus_algorithm = {
274 .smbus_xfer = nforce2_access,
275 .functionality = nforce2_func,
279 static struct pci_device_id nforce2_ids[] = {
280 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS) },
281 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SMBUS) },
282 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_SMBUS) },
283 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SMBUS) },
284 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE4_SMBUS) },
285 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SMBUS) },
286 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS) },
287 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS) },
288 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SMBUS) },
289 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP65_SMBUS) },
293 MODULE_DEVICE_TABLE (pci, nforce2_ids);
296 static int __devinit nforce2_probe_smb (struct pci_dev *dev, int bar,
297 int alt_reg, struct nforce2_smbus *smbus, const char *name)
301 smbus->base = pci_resource_start(dev, bar);
303 smbus->size = pci_resource_len(dev, bar);
305 /* Older incarnations of the device used non-standard BARs */
308 if (pci_read_config_word(dev, alt_reg, &iobase)
309 != PCIBIOS_SUCCESSFUL) {
310 dev_err(&dev->dev, "Error reading PCI config for %s\n",
315 smbus->base = iobase & PCI_BASE_ADDRESS_IO_MASK;
319 error = acpi_check_region(smbus->base, smbus->size,
320 nforce2_driver.name);
324 if (!request_region(smbus->base, smbus->size, nforce2_driver.name)) {
325 dev_err(&smbus->adapter.dev, "Error requesting region %02x .. %02X for %s\n",
326 smbus->base, smbus->base+smbus->size-1, name);
329 smbus->adapter.owner = THIS_MODULE;
330 smbus->adapter.id = I2C_HW_SMBUS_NFORCE2;
331 smbus->adapter.class = I2C_CLASS_HWMON;
332 smbus->adapter.algo = &smbus_algorithm;
333 smbus->adapter.algo_data = smbus;
334 smbus->adapter.dev.parent = &dev->dev;
335 snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
336 "SMBus nForce2 adapter at %04x", smbus->base);
338 error = i2c_add_adapter(&smbus->adapter);
340 dev_err(&smbus->adapter.dev, "Failed to register adapter.\n");
341 release_region(smbus->base, smbus->size);
344 dev_info(&smbus->adapter.dev, "nForce2 SMBus adapter at %#x\n", smbus->base);
349 static int __devinit nforce2_probe(struct pci_dev *dev, const struct pci_device_id *id)
351 struct nforce2_smbus *smbuses;
354 /* we support 2 SMBus adapters */
355 if (!(smbuses = kzalloc(2*sizeof(struct nforce2_smbus), GFP_KERNEL)))
357 pci_set_drvdata(dev, smbuses);
359 switch(dev->device) {
360 case PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS:
361 case PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS:
362 case PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS:
363 smbuses[0].blockops = 1;
364 smbuses[1].blockops = 1;
365 smbuses[0].can_abort = 1;
366 smbuses[1].can_abort = 1;
369 /* SMBus adapter 1 */
370 res1 = nforce2_probe_smb(dev, 4, NFORCE_PCI_SMB1, &smbuses[0], "SMB1");
372 dev_err(&dev->dev, "Error probing SMB1.\n");
373 smbuses[0].base = 0; /* to have a check value */
375 /* SMBus adapter 2 */
376 res2 = nforce2_probe_smb(dev, 5, NFORCE_PCI_SMB2, &smbuses[1], "SMB2");
378 dev_err(&dev->dev, "Error probing SMB2.\n");
379 smbuses[1].base = 0; /* to have a check value */
381 if ((res1 < 0) && (res2 < 0)) {
382 /* we did not find even one of the SMBuses, so we give up */
391 static void __devexit nforce2_remove(struct pci_dev *dev)
393 struct nforce2_smbus *smbuses = (void*) pci_get_drvdata(dev);
395 if (smbuses[0].base) {
396 i2c_del_adapter(&smbuses[0].adapter);
397 release_region(smbuses[0].base, smbuses[0].size);
399 if (smbuses[1].base) {
400 i2c_del_adapter(&smbuses[1].adapter);
401 release_region(smbuses[1].base, smbuses[1].size);
406 static struct pci_driver nforce2_driver = {
407 .name = "nForce2_smbus",
408 .id_table = nforce2_ids,
409 .probe = nforce2_probe,
410 .remove = __devexit_p(nforce2_remove),
413 static int __init nforce2_init(void)
415 return pci_register_driver(&nforce2_driver);
418 static void __exit nforce2_exit(void)
420 pci_unregister_driver(&nforce2_driver);
423 module_init(nforce2_init);
424 module_exit(nforce2_exit);