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>
55 MODULE_LICENSE("GPL");
56 MODULE_AUTHOR ("Hans-Frieder Vogt <hfvogt@gmx.net>");
57 MODULE_DESCRIPTION("nForce2/3/4/5xx SMBus driver");
60 struct nforce2_smbus {
61 struct i2c_adapter adapter;
69 * nVidia nForce2 SMBus control register definitions
70 * (Newer incarnations use standard BARs 4 and 5 instead)
72 #define NFORCE_PCI_SMB1 0x50
73 #define NFORCE_PCI_SMB2 0x54
77 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
79 #define NVIDIA_SMB_PRTCL (smbus->base + 0x00) /* protocol, PEC */
80 #define NVIDIA_SMB_STS (smbus->base + 0x01) /* status */
81 #define NVIDIA_SMB_ADDR (smbus->base + 0x02) /* address */
82 #define NVIDIA_SMB_CMD (smbus->base + 0x03) /* command */
83 #define NVIDIA_SMB_DATA (smbus->base + 0x04) /* 32 data registers */
84 #define NVIDIA_SMB_BCNT (smbus->base + 0x24) /* number of data
87 #define NVIDIA_SMB_STS_DONE 0x80
88 #define NVIDIA_SMB_STS_ALRM 0x40
89 #define NVIDIA_SMB_STS_RES 0x20
90 #define NVIDIA_SMB_STS_STATUS 0x1f
92 #define NVIDIA_SMB_PRTCL_WRITE 0x00
93 #define NVIDIA_SMB_PRTCL_READ 0x01
94 #define NVIDIA_SMB_PRTCL_QUICK 0x02
95 #define NVIDIA_SMB_PRTCL_BYTE 0x04
96 #define NVIDIA_SMB_PRTCL_BYTE_DATA 0x06
97 #define NVIDIA_SMB_PRTCL_WORD_DATA 0x08
98 #define NVIDIA_SMB_PRTCL_BLOCK_DATA 0x0a
99 #define NVIDIA_SMB_PRTCL_PEC 0x80
101 static struct pci_driver nforce2_driver;
103 /* Return -1 on error */
104 static s32 nforce2_access(struct i2c_adapter * adap, u16 addr,
105 unsigned short flags, char read_write,
106 u8 command, int size, union i2c_smbus_data * data)
108 struct nforce2_smbus *smbus = adap->algo_data;
109 unsigned char protocol, pec, temp;
113 protocol = (read_write == I2C_SMBUS_READ) ? NVIDIA_SMB_PRTCL_READ :
114 NVIDIA_SMB_PRTCL_WRITE;
115 pec = (flags & I2C_CLIENT_PEC) ? NVIDIA_SMB_PRTCL_PEC : 0;
119 case I2C_SMBUS_QUICK:
120 protocol |= NVIDIA_SMB_PRTCL_QUICK;
121 read_write = I2C_SMBUS_WRITE;
125 if (read_write == I2C_SMBUS_WRITE)
126 outb_p(command, NVIDIA_SMB_CMD);
127 protocol |= NVIDIA_SMB_PRTCL_BYTE;
130 case I2C_SMBUS_BYTE_DATA:
131 outb_p(command, NVIDIA_SMB_CMD);
132 if (read_write == I2C_SMBUS_WRITE)
133 outb_p(data->byte, NVIDIA_SMB_DATA);
134 protocol |= NVIDIA_SMB_PRTCL_BYTE_DATA;
137 case I2C_SMBUS_WORD_DATA:
138 outb_p(command, NVIDIA_SMB_CMD);
139 if (read_write == I2C_SMBUS_WRITE) {
140 outb_p(data->word, NVIDIA_SMB_DATA);
141 outb_p(data->word >> 8, NVIDIA_SMB_DATA+1);
143 protocol |= NVIDIA_SMB_PRTCL_WORD_DATA | pec;
146 case I2C_SMBUS_BLOCK_DATA:
147 outb_p(command, NVIDIA_SMB_CMD);
148 if (read_write == I2C_SMBUS_WRITE) {
149 len = data->block[0];
150 if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX)) {
152 "Transaction failed "
153 "(requested block size: %d)\n",
157 outb_p(len, NVIDIA_SMB_BCNT);
158 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
159 outb_p(data->block[i + 1],
162 protocol |= NVIDIA_SMB_PRTCL_BLOCK_DATA | pec;
166 dev_err(&adap->dev, "Unsupported transaction %d\n", size);
170 outb_p((addr & 0x7f) << 1, NVIDIA_SMB_ADDR);
171 outb_p(protocol, NVIDIA_SMB_PRTCL);
173 temp = inb_p(NVIDIA_SMB_STS);
175 if (~temp & NVIDIA_SMB_STS_DONE) {
177 temp = inb_p(NVIDIA_SMB_STS);
179 if (~temp & NVIDIA_SMB_STS_DONE) {
181 temp = inb_p(NVIDIA_SMB_STS);
184 if ((~temp & NVIDIA_SMB_STS_DONE) || (temp & NVIDIA_SMB_STS_STATUS)) {
185 dev_dbg(&adap->dev, "SMBus Timeout! (0x%02x)\n", temp);
189 if (read_write == I2C_SMBUS_WRITE)
195 case I2C_SMBUS_BYTE_DATA:
196 data->byte = inb_p(NVIDIA_SMB_DATA);
199 case I2C_SMBUS_WORD_DATA:
200 data->word = inb_p(NVIDIA_SMB_DATA) | (inb_p(NVIDIA_SMB_DATA+1) << 8);
203 case I2C_SMBUS_BLOCK_DATA:
204 len = inb_p(NVIDIA_SMB_BCNT);
205 len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX);
206 for (i = 0; i < len; i++)
207 data->block[i+1] = inb_p(NVIDIA_SMB_DATA + i);
208 data->block[0] = len;
216 static u32 nforce2_func(struct i2c_adapter *adapter)
218 /* other functionality might be possible, but is not tested */
219 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
220 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
221 (((struct nforce2_smbus*)adapter->algo_data)->blockops ?
222 I2C_FUNC_SMBUS_BLOCK_DATA : 0);
225 static struct i2c_algorithm smbus_algorithm = {
226 .smbus_xfer = nforce2_access,
227 .functionality = nforce2_func,
231 static struct pci_device_id nforce2_ids[] = {
232 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS) },
233 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SMBUS) },
234 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_SMBUS) },
235 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SMBUS) },
236 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE4_SMBUS) },
237 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SMBUS) },
238 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS) },
239 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS) },
240 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SMBUS) },
241 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP65_SMBUS) },
245 MODULE_DEVICE_TABLE (pci, nforce2_ids);
248 static int __devinit nforce2_probe_smb (struct pci_dev *dev, int bar,
249 int alt_reg, struct nforce2_smbus *smbus, const char *name)
253 smbus->base = pci_resource_start(dev, bar);
255 smbus->size = pci_resource_len(dev, bar);
257 /* Older incarnations of the device used non-standard BARs */
260 if (pci_read_config_word(dev, alt_reg, &iobase)
261 != PCIBIOS_SUCCESSFUL) {
262 dev_err(&dev->dev, "Error reading PCI config for %s\n",
267 smbus->base = iobase & PCI_BASE_ADDRESS_IO_MASK;
271 if (!request_region(smbus->base, smbus->size, nforce2_driver.name)) {
272 dev_err(&smbus->adapter.dev, "Error requesting region %02x .. %02X for %s\n",
273 smbus->base, smbus->base+smbus->size-1, name);
276 smbus->adapter.owner = THIS_MODULE;
277 smbus->adapter.id = I2C_HW_SMBUS_NFORCE2;
278 smbus->adapter.class = I2C_CLASS_HWMON;
279 smbus->adapter.algo = &smbus_algorithm;
280 smbus->adapter.algo_data = smbus;
281 smbus->adapter.dev.parent = &dev->dev;
282 snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
283 "SMBus nForce2 adapter at %04x", smbus->base);
285 error = i2c_add_adapter(&smbus->adapter);
287 dev_err(&smbus->adapter.dev, "Failed to register adapter.\n");
288 release_region(smbus->base, smbus->size);
291 dev_info(&smbus->adapter.dev, "nForce2 SMBus adapter at %#x\n", smbus->base);
296 static int __devinit nforce2_probe(struct pci_dev *dev, const struct pci_device_id *id)
298 struct nforce2_smbus *smbuses;
301 /* we support 2 SMBus adapters */
302 if (!(smbuses = kzalloc(2*sizeof(struct nforce2_smbus), GFP_KERNEL)))
304 pci_set_drvdata(dev, smbuses);
306 switch(dev->device) {
307 case PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS:
308 case PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS:
309 smbuses[0].blockops = 1;
310 smbuses[1].blockops = 1;
313 /* SMBus adapter 1 */
314 res1 = nforce2_probe_smb(dev, 4, NFORCE_PCI_SMB1, &smbuses[0], "SMB1");
316 dev_err(&dev->dev, "Error probing SMB1.\n");
317 smbuses[0].base = 0; /* to have a check value */
319 /* SMBus adapter 2 */
320 res2 = nforce2_probe_smb(dev, 5, NFORCE_PCI_SMB2, &smbuses[1], "SMB2");
322 dev_err(&dev->dev, "Error probing SMB2.\n");
323 smbuses[1].base = 0; /* to have a check value */
325 if ((res1 < 0) && (res2 < 0)) {
326 /* we did not find even one of the SMBuses, so we give up */
335 static void __devexit nforce2_remove(struct pci_dev *dev)
337 struct nforce2_smbus *smbuses = (void*) pci_get_drvdata(dev);
339 if (smbuses[0].base) {
340 i2c_del_adapter(&smbuses[0].adapter);
341 release_region(smbuses[0].base, smbuses[0].size);
343 if (smbuses[1].base) {
344 i2c_del_adapter(&smbuses[1].adapter);
345 release_region(smbuses[1].base, smbuses[1].size);
350 static struct pci_driver nforce2_driver = {
351 .name = "nForce2_smbus",
352 .id_table = nforce2_ids,
353 .probe = nforce2_probe,
354 .remove = __devexit_p(nforce2_remove),
357 static int __init nforce2_init(void)
359 return pci_register_driver(&nforce2_driver);
362 static void __exit nforce2_exit(void)
364 pci_unregister_driver(&nforce2_driver);
367 module_init(nforce2_init);
368 module_exit(nforce2_exit);