2 SMBus driver for nVidia nForce2 MCP
4 Ported to 2.5 Patrick Dreker <patrick@dreker.de>,
5 Copyright (c) 2003 Hans-Frieder Vogt <hfvogt@arcor.de>,
7 SMBus 2.0 driver for AMD-8111 IO-Hub
8 Copyright (c) 2002 Vojtech Pavlik
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 SUPPORTED DEVICES PCI ID
29 This driver supports the 2 SMBuses that are included in the MCP2 of the
33 /* Note: we assume there can only be one nForce2, with two SMBus interfaces */
35 #include <linux/module.h>
36 #include <linux/pci.h>
37 #include <linux/kernel.h>
38 #include <linux/stddef.h>
39 #include <linux/sched.h>
40 #include <linux/ioport.h>
41 #include <linux/init.h>
42 #include <linux/i2c.h>
43 #include <linux/delay.h>
46 MODULE_LICENSE("GPL");
47 MODULE_AUTHOR ("Hans-Frieder Vogt <hfvogt@arcor.de>");
48 MODULE_DESCRIPTION("nForce2 SMBus driver");
51 #ifndef PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS
52 #define PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS 0x0064
54 /* TODO: sync with lm-sensors */
55 #ifndef I2C_HW_SMBUS_NFORCE2
56 #define I2C_HW_SMBUS_NFORCE2 0x0c
60 struct nforce2_smbus {
62 struct i2c_adapter adapter;
69 * nVidia nForce2 SMBus control register definitions
71 #define NFORCE_PCI_SMB1 0x50
72 #define NFORCE_PCI_SMB2 0x54
76 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
78 #define NVIDIA_SMB_PRTCL (smbus->base + 0x00) /* protocol, PEC */
79 #define NVIDIA_SMB_STS (smbus->base + 0x01) /* status */
80 #define NVIDIA_SMB_ADDR (smbus->base + 0x02) /* address */
81 #define NVIDIA_SMB_CMD (smbus->base + 0x03) /* command */
82 #define NVIDIA_SMB_DATA (smbus->base + 0x04) /* 32 data registers */
83 #define NVIDIA_SMB_BCNT (smbus->base + 0x24) /* number of data bytes */
84 #define NVIDIA_SMB_ALRM_A (smbus->base + 0x25) /* alarm address */
85 #define NVIDIA_SMB_ALRM_D (smbus->base + 0x26) /* 2 bytes alarm 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_PROC_CALL 0x0c
100 #define NVIDIA_SMB_PRTCL_BLOCK_PROC_CALL 0x0d
101 #define NVIDIA_SMB_PRTCL_I2C_BLOCK_DATA 0x4a
102 #define NVIDIA_SMB_PRTCL_PEC 0x80
106 #define MAX_TIMEOUT 256
110 static s32 nforce2_access(struct i2c_adapter *adap, u16 addr,
111 unsigned short flags, char read_write,
112 u8 command, int size, union i2c_smbus_data *data);
113 static u32 nforce2_func(struct i2c_adapter *adapter);
116 static struct i2c_algorithm smbus_algorithm = {
117 .name = "Non-I2C SMBus adapter",
118 .id = I2C_ALGO_SMBUS,
119 .smbus_xfer = nforce2_access,
120 .functionality = nforce2_func,
123 static struct i2c_adapter nforce2_adapter = {
124 .owner = THIS_MODULE,
125 .id = I2C_ALGO_SMBUS | I2C_HW_SMBUS_NFORCE2,
126 .class = I2C_ADAP_CLASS_SMBUS,
127 .algo = &smbus_algorithm,
133 /* Internally used pause function */
134 static void nforce2_do_pause(unsigned int amount)
136 current->state = TASK_INTERRUPTIBLE;
137 schedule_timeout(amount);
141 /* Return -1 on error. See smbus.h for more information */
142 static s32 nforce2_access(struct i2c_adapter * adap, u16 addr, unsigned short flags,
143 char read_write, u8 command, int size,
144 union i2c_smbus_data * data)
146 struct nforce2_smbus *smbus = adap->algo_data;
147 unsigned char protocol, pec, temp;
148 unsigned char len = 0; /* to keep the compiler quiet */
152 protocol = (read_write == I2C_SMBUS_READ) ? NVIDIA_SMB_PRTCL_READ :
153 NVIDIA_SMB_PRTCL_WRITE;
154 pec = (flags & I2C_CLIENT_PEC) ? NVIDIA_SMB_PRTCL_PEC : 0;
158 case I2C_SMBUS_QUICK:
159 protocol |= NVIDIA_SMB_PRTCL_QUICK;
160 read_write = I2C_SMBUS_WRITE;
164 if (read_write == I2C_SMBUS_WRITE)
165 outb_p(data->byte, NVIDIA_SMB_DATA);
166 protocol |= NVIDIA_SMB_PRTCL_BYTE;
169 case I2C_SMBUS_BYTE_DATA:
170 outb_p(command, NVIDIA_SMB_CMD);
171 if (read_write == I2C_SMBUS_WRITE)
172 outb_p(data->byte, NVIDIA_SMB_DATA);
173 protocol |= NVIDIA_SMB_PRTCL_BYTE_DATA;
176 case I2C_SMBUS_WORD_DATA:
177 outb_p(command, NVIDIA_SMB_CMD);
178 if (read_write == I2C_SMBUS_WRITE) {
179 outb_p(data->word, NVIDIA_SMB_DATA);
180 outb_p(data->word >> 8, NVIDIA_SMB_DATA+1);
182 protocol |= NVIDIA_SMB_PRTCL_WORD_DATA | pec;
185 case I2C_SMBUS_BLOCK_DATA:
186 outb_p(command, NVIDIA_SMB_CMD);
187 if (read_write == I2C_SMBUS_WRITE) {
188 len = min_t(u8, data->block[0], 32);
189 outb_p(len, NVIDIA_SMB_BCNT);
190 for (i = 0; i < len; i++)
191 outb_p(data->block[i + 1], NVIDIA_SMB_DATA+i);
193 protocol |= NVIDIA_SMB_PRTCL_BLOCK_DATA | pec;
196 case I2C_SMBUS_I2C_BLOCK_DATA:
197 len = min_t(u8, data->block[0], 32);
198 outb_p(command, NVIDIA_SMB_CMD);
199 outb_p(len, NVIDIA_SMB_BCNT);
200 if (read_write == I2C_SMBUS_WRITE)
201 for (i = 0; i < len; i++)
202 outb_p(data->block[i + 1], NVIDIA_SMB_DATA+i);
203 protocol |= NVIDIA_SMB_PRTCL_I2C_BLOCK_DATA;
206 case I2C_SMBUS_PROC_CALL:
207 dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
210 outb_p(command, NVIDIA_SMB_CMD);
211 outb_p(data->word, NVIDIA_SMB_DATA);
212 outb_p(data->word >> 8, NVIDIA_SMB_DATA + 1);
213 protocol = NVIDIA_SMB_PRTCL_PROC_CALL | pec;
214 read_write = I2C_SMBUS_READ;
218 case I2C_SMBUS_BLOCK_PROC_CALL:
219 dev_err(&adap->dev, "I2C_SMBUS_BLOCK_PROC_CALL not supported!\n");
223 len = min_t(u8, data->block[0], 31);
224 outb_p(command, NVIDIA_SMB_CMD);
225 outb_p(len, NVIDIA_SMB_BCNT);
226 for (i = 0; i < len; i++)
227 outb_p(data->block[i + 1], NVIDIA_SMB_DATA + i);
228 protocol = NVIDIA_SMB_PRTCL_BLOCK_PROC_CALL | pec;
229 read_write = I2C_SMBUS_READ;
233 case I2C_SMBUS_WORD_DATA_PEC:
234 case I2C_SMBUS_BLOCK_DATA_PEC:
235 case I2C_SMBUS_PROC_CALL_PEC:
236 case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
237 dev_err(&adap->dev, "Unexpected software PEC transaction %d\n.", size);
241 dev_err(&adap->dev, "Unsupported transaction %d\n", size);
245 outb_p((addr & 0x7f) << 1, NVIDIA_SMB_ADDR);
246 outb_p(protocol, NVIDIA_SMB_PRTCL);
248 temp = inb_p(NVIDIA_SMB_STS);
253 temp = inb_p(NVIDIA_SMB_STS);
254 } while (((temp & NVIDIA_SMB_STS_DONE) == 0) && (timeout++ < MAX_TIMEOUT));
256 if (~temp & NVIDIA_SMB_STS_DONE) {
258 temp = inb_p(NVIDIA_SMB_STS);
260 if (~temp & NVIDIA_SMB_STS_DONE) {
261 current->state = TASK_INTERRUPTIBLE;
262 schedule_timeout(HZ/100);
263 temp = inb_p(NVIDIA_SMB_STS);
266 if ((timeout >= MAX_TIMEOUT) || (~temp & NVIDIA_SMB_STS_DONE)
267 || (temp & NVIDIA_SMB_STS_STATUS))
270 if (read_write == I2C_SMBUS_WRITE)
276 case I2C_SMBUS_BYTE_DATA:
277 data->byte = inb_p(NVIDIA_SMB_DATA);
280 case I2C_SMBUS_WORD_DATA:
281 /* case I2C_SMBUS_PROC_CALL: not supported */
282 data->word = inb_p(NVIDIA_SMB_DATA) | (inb_p(NVIDIA_SMB_DATA+1) << 8);
285 case I2C_SMBUS_BLOCK_DATA:
286 /* case I2C_SMBUS_BLOCK_PROC_CALL: not supported */
287 len = inb_p(NVIDIA_SMB_BCNT);
288 len = min_t(u8, len, 32);
289 case I2C_SMBUS_I2C_BLOCK_DATA:
290 for (i = 0; i < len; i++)
291 data->block[i+1] = inb_p(NVIDIA_SMB_DATA + i);
292 data->block[0] = len;
300 static u32 nforce2_func(struct i2c_adapter *adapter)
302 /* other functionality might be possible, but is not tested */
303 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
304 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA /* |
305 I2C_FUNC_SMBUS_BLOCK_DATA */;
309 static struct pci_device_id nforce2_ids[] = {
310 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS,
311 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
316 static int __devinit nforce2_probe_smb (struct pci_dev *dev, int reg,
317 struct nforce2_smbus *smbus, char *name)
322 if (pci_read_config_word(dev, reg, &iobase) != PCIBIOS_SUCCESSFUL) {
323 dev_err(&smbus->adapter.dev, "Error reading PCI config for %s\n", name);
327 smbus->base = iobase & 0xfffc;
330 if (!request_region(smbus->base, smbus->size, "nForce2 SMBus")) {
331 dev_err(&smbus->adapter.dev, "Error requesting region %02x .. %02X for %s\n",
332 smbus->base, smbus->base+smbus->size-1, name);
336 smbus->adapter.owner = THIS_MODULE;
337 smbus->adapter.id = I2C_ALGO_SMBUS | I2C_HW_SMBUS_NFORCE2;
338 smbus->adapter.algo = &smbus_algorithm;
339 smbus->adapter.algo_data = smbus;
341 smbus->adapter = nforce2_adapter;
342 smbus->adapter.dev.parent = &dev->dev;
343 snprintf(smbus->adapter.name, DEVICE_NAME_SIZE,
344 "SMBus nForce2 adapter at %04x", smbus->base);
346 error = i2c_add_adapter(&smbus->adapter);
348 dev_err(&smbus->adapter.dev, "Failed to register adapter.\n");
349 release_region(smbus->base, smbus->size);
352 dev_info(&smbus->adapter.dev, "nForce2 SMBus adapter at %#x\n", smbus->base);
357 static int __devinit nforce2_probe(struct pci_dev *dev, const struct pci_device_id *id)
359 struct nforce2_smbus *smbuses;
362 /* we support 2 SMBus adapters */
363 if (!(smbuses = (void *)kmalloc(2*sizeof(struct nforce2_smbus),
366 memset (smbuses, 0, 2*sizeof(struct nforce2_smbus));
367 pci_set_drvdata(dev, smbuses);
369 /* SMBus adapter 1 */
370 res1 = nforce2_probe_smb (dev, 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 res2 = nforce2_probe_smb (dev, NFORCE_PCI_SMB2, &smbuses[1], "SMB2");
377 dev_err(&dev->dev, "Error probing SMB2.\n");
378 smbuses[1].base = 0; /* to have a check value */
380 if ((res1 < 0) && (res2 < 0)) {
381 /* we did not find even one of the SMBuses, so we give up */
390 static void __devexit nforce2_remove(struct pci_dev *dev)
392 struct nforce2_smbus *smbuses = (void*) pci_get_drvdata(dev);
394 if (smbuses[0].base) {
395 i2c_del_adapter(&smbuses[0].adapter);
396 release_region(smbuses[0].base, smbuses[0].size);
398 if (smbuses[1].base) {
399 i2c_del_adapter(&smbuses[1].adapter);
400 release_region(smbuses[1].base, smbuses[1].size);
405 static struct pci_driver nforce2_driver = {
406 .name = "nForce2 SMBus",
407 .id_table = nforce2_ids,
408 .probe = nforce2_probe,
409 .remove = __devexit_p(nforce2_remove),
412 static int __init nforce2_init(void)
414 return pci_module_init(&nforce2_driver);
417 static void __exit nforce2_exit(void)
419 pci_unregister_driver(&nforce2_driver);
422 module_init(nforce2_init);
423 module_exit(nforce2_exit);