Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / i2c / busses / i2c-piix4.c
blob646381b6b3bf9e277aaedf15b2e7f41a2ca25b62
1 /*
2 piix4.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 Supports:
24 Intel PIIX4, 440MX
25 Serverworks OSB4, CSB5, CSB6
26 SMSC Victory66
28 Note: we assume there can only be one device, with one SMBus interface.
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/pci.h>
35 #include <linux/kernel.h>
36 #include <linux/delay.h>
37 #include <linux/stddef.h>
38 #include <linux/sched.h>
39 #include <linux/ioport.h>
40 #include <linux/i2c.h>
41 #include <linux/init.h>
42 #include <linux/apm_bios.h>
43 #include <linux/dmi.h>
44 #include <asm/io.h>
47 struct sd {
48 const unsigned short mfr;
49 const unsigned short dev;
50 const unsigned char fn;
51 const char *name;
54 /* PIIX4 SMBus address offsets */
55 #define SMBHSTSTS (0 + piix4_smba)
56 #define SMBHSLVSTS (1 + piix4_smba)
57 #define SMBHSTCNT (2 + piix4_smba)
58 #define SMBHSTCMD (3 + piix4_smba)
59 #define SMBHSTADD (4 + piix4_smba)
60 #define SMBHSTDAT0 (5 + piix4_smba)
61 #define SMBHSTDAT1 (6 + piix4_smba)
62 #define SMBBLKDAT (7 + piix4_smba)
63 #define SMBSLVCNT (8 + piix4_smba)
64 #define SMBSHDWCMD (9 + piix4_smba)
65 #define SMBSLVEVT (0xA + piix4_smba)
66 #define SMBSLVDAT (0xC + piix4_smba)
68 /* count for request_region */
69 #define SMBIOSIZE 8
71 /* PCI Address Constants */
72 #define SMBBA 0x090
73 #define SMBHSTCFG 0x0D2
74 #define SMBSLVC 0x0D3
75 #define SMBSHDW1 0x0D4
76 #define SMBSHDW2 0x0D5
77 #define SMBREV 0x0D6
79 /* Other settings */
80 #define MAX_TIMEOUT 500
81 #define ENABLE_INT9 0
83 /* PIIX4 constants */
84 #define PIIX4_QUICK 0x00
85 #define PIIX4_BYTE 0x04
86 #define PIIX4_BYTE_DATA 0x08
87 #define PIIX4_WORD_DATA 0x0C
88 #define PIIX4_BLOCK_DATA 0x14
90 /* insmod parameters */
92 /* If force is set to anything different from 0, we forcibly enable the
93 PIIX4. DANGEROUS! */
94 static int force = 0;
95 module_param (force, int, 0);
96 MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!");
98 /* If force_addr is set to anything different from 0, we forcibly enable
99 the PIIX4 at the given address. VERY DANGEROUS! */
100 static int force_addr = 0;
101 module_param (force_addr, int, 0);
102 MODULE_PARM_DESC(force_addr,
103 "Forcibly enable the PIIX4 at the given address. "
104 "EXTREMELY DANGEROUS!");
106 /* If fix_hstcfg is set to anything different from 0, we reset one of the
107 registers to be a valid value. */
108 static int fix_hstcfg = 0;
109 module_param (fix_hstcfg, int, 0);
110 MODULE_PARM_DESC(fix_hstcfg,
111 "Fix config register. Needed on some boards (Force CPCI735).");
113 static int piix4_transaction(void);
115 static unsigned short piix4_smba = 0;
116 static struct i2c_adapter piix4_adapter;
118 static struct dmi_system_id __devinitdata piix4_dmi_table[] = {
120 .ident = "IBM",
121 .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), },
123 { },
126 static int __devinit piix4_setup(struct pci_dev *PIIX4_dev,
127 const struct pci_device_id *id)
129 unsigned char temp;
131 /* match up the function */
132 if (PCI_FUNC(PIIX4_dev->devfn) != id->driver_data)
133 return -ENODEV;
135 dev_info(&PIIX4_dev->dev, "Found %s device\n", pci_name(PIIX4_dev));
137 /* Don't access SMBus on IBM systems which get corrupted eeproms */
138 if (dmi_check_system(piix4_dmi_table) &&
139 PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) {
140 dev_err(&PIIX4_dev->dev, "IBM Laptop detected; this module "
141 "may corrupt your serial eeprom! Refusing to load "
142 "module!\n");
143 return -EPERM;
146 /* Determine the address of the SMBus areas */
147 if (force_addr) {
148 piix4_smba = force_addr & 0xfff0;
149 force = 0;
150 } else {
151 pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba);
152 piix4_smba &= 0xfff0;
153 if(piix4_smba == 0) {
154 dev_err(&PIIX4_dev->dev, "SMB base address "
155 "uninitialized - upgrade BIOS or use "
156 "force_addr=0xaddr\n");
157 return -ENODEV;
161 if (!request_region(piix4_smba, SMBIOSIZE, "piix4-smbus")) {
162 dev_err(&PIIX4_dev->dev, "SMB region 0x%x already in use!\n",
163 piix4_smba);
164 return -ENODEV;
167 pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp);
169 /* Some BIOS will set up the chipset incorrectly and leave a register
170 in an undefined state (causing I2C to act very strangely). */
171 if (temp & 0x02) {
172 if (fix_hstcfg) {
173 dev_info(&PIIX4_dev->dev, "Working around buggy BIOS "
174 "(I2C)\n");
175 temp &= 0xfd;
176 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp);
177 } else {
178 dev_info(&PIIX4_dev->dev, "Unusual config register "
179 "value\n");
180 dev_info(&PIIX4_dev->dev, "Try using fix_hstcfg=1 if "
181 "you experience problems\n");
185 /* If force_addr is set, we program the new address here. Just to make
186 sure, we disable the PIIX4 first. */
187 if (force_addr) {
188 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe);
189 pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba);
190 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01);
191 dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to "
192 "new address %04x!\n", piix4_smba);
193 } else if ((temp & 1) == 0) {
194 if (force) {
195 /* This should never need to be done, but has been
196 * noted that many Dell machines have the SMBus
197 * interface on the PIIX4 disabled!? NOTE: This assumes
198 * I/O space and other allocations WERE done by the
199 * Bios! Don't complain if your hardware does weird
200 * things after enabling this. :') Check for Bios
201 * updates before resorting to this.
203 pci_write_config_byte(PIIX4_dev, SMBHSTCFG,
204 temp | 1);
205 dev_printk(KERN_NOTICE, &PIIX4_dev->dev,
206 "WARNING: SMBus interface has been "
207 "FORCEFULLY ENABLED!\n");
208 } else {
209 dev_err(&PIIX4_dev->dev,
210 "Host SMBus controller not enabled!\n");
211 release_region(piix4_smba, SMBIOSIZE);
212 piix4_smba = 0;
213 return -ENODEV;
217 if ((temp & 0x0E) == 8)
218 dev_dbg(&PIIX4_dev->dev, "Using Interrupt 9 for SMBus.\n");
219 else if ((temp & 0x0E) == 0)
220 dev_dbg(&PIIX4_dev->dev, "Using Interrupt SMI# for SMBus.\n");
221 else
222 dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration "
223 "(or code out of date)!\n");
225 pci_read_config_byte(PIIX4_dev, SMBREV, &temp);
226 dev_dbg(&PIIX4_dev->dev, "SMBREV = 0x%X\n", temp);
227 dev_dbg(&PIIX4_dev->dev, "SMBA = 0x%X\n", piix4_smba);
229 return 0;
232 /* Another internally used function */
233 static int piix4_transaction(void)
235 int temp;
236 int result = 0;
237 int timeout = 0;
239 dev_dbg(&piix4_adapter.dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
240 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
241 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
242 inb_p(SMBHSTDAT1));
244 /* Make sure the SMBus host is ready to start transmitting */
245 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
246 dev_dbg(&piix4_adapter.dev, "SMBus busy (%02x). "
247 "Resetting... \n", temp);
248 outb_p(temp, SMBHSTSTS);
249 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
250 dev_err(&piix4_adapter.dev, "Failed! (%02x)\n", temp);
251 return -1;
252 } else {
253 dev_dbg(&piix4_adapter.dev, "Successfull!\n");
257 /* start the transaction by setting bit 6 */
258 outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
260 /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
261 do {
262 msleep(1);
263 temp = inb_p(SMBHSTSTS);
264 } while ((temp & 0x01) && (timeout++ < MAX_TIMEOUT));
266 /* If the SMBus is still busy, we give up */
267 if (timeout >= MAX_TIMEOUT) {
268 dev_err(&piix4_adapter.dev, "SMBus Timeout!\n");
269 result = -1;
272 if (temp & 0x10) {
273 result = -1;
274 dev_err(&piix4_adapter.dev, "Error: Failed bus transaction\n");
277 if (temp & 0x08) {
278 result = -1;
279 dev_dbg(&piix4_adapter.dev, "Bus collision! SMBus may be "
280 "locked until next hard reset. (sorry!)\n");
281 /* Clock stops and slave is stuck in mid-transmission */
284 if (temp & 0x04) {
285 result = -1;
286 dev_dbg(&piix4_adapter.dev, "Error: no response!\n");
289 if (inb_p(SMBHSTSTS) != 0x00)
290 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
292 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
293 dev_err(&piix4_adapter.dev, "Failed reset at end of "
294 "transaction (%02x)\n", temp);
296 dev_dbg(&piix4_adapter.dev, "Transaction (post): CNT=%02x, CMD=%02x, "
297 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
298 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
299 inb_p(SMBHSTDAT1));
300 return result;
303 /* Return -1 on error. */
304 static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
305 unsigned short flags, char read_write,
306 u8 command, int size, union i2c_smbus_data * data)
308 int i, len;
310 switch (size) {
311 case I2C_SMBUS_PROC_CALL:
312 dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
313 return -1;
314 case I2C_SMBUS_QUICK:
315 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
316 SMBHSTADD);
317 size = PIIX4_QUICK;
318 break;
319 case I2C_SMBUS_BYTE:
320 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
321 SMBHSTADD);
322 if (read_write == I2C_SMBUS_WRITE)
323 outb_p(command, SMBHSTCMD);
324 size = PIIX4_BYTE;
325 break;
326 case I2C_SMBUS_BYTE_DATA:
327 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
328 SMBHSTADD);
329 outb_p(command, SMBHSTCMD);
330 if (read_write == I2C_SMBUS_WRITE)
331 outb_p(data->byte, SMBHSTDAT0);
332 size = PIIX4_BYTE_DATA;
333 break;
334 case I2C_SMBUS_WORD_DATA:
335 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
336 SMBHSTADD);
337 outb_p(command, SMBHSTCMD);
338 if (read_write == I2C_SMBUS_WRITE) {
339 outb_p(data->word & 0xff, SMBHSTDAT0);
340 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
342 size = PIIX4_WORD_DATA;
343 break;
344 case I2C_SMBUS_BLOCK_DATA:
345 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
346 SMBHSTADD);
347 outb_p(command, SMBHSTCMD);
348 if (read_write == I2C_SMBUS_WRITE) {
349 len = data->block[0];
350 if (len < 0)
351 len = 0;
352 if (len > 32)
353 len = 32;
354 outb_p(len, SMBHSTDAT0);
355 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
356 for (i = 1; i <= len; i++)
357 outb_p(data->block[i], SMBBLKDAT);
359 size = PIIX4_BLOCK_DATA;
360 break;
363 outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
365 if (piix4_transaction()) /* Error in transaction */
366 return -1;
368 if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
369 return 0;
372 switch (size) {
373 case PIIX4_BYTE: /* Where is the result put? I assume here it is in
374 SMBHSTDAT0 but it might just as well be in the
375 SMBHSTCMD. No clue in the docs */
377 data->byte = inb_p(SMBHSTDAT0);
378 break;
379 case PIIX4_BYTE_DATA:
380 data->byte = inb_p(SMBHSTDAT0);
381 break;
382 case PIIX4_WORD_DATA:
383 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
384 break;
385 case PIIX4_BLOCK_DATA:
386 data->block[0] = inb_p(SMBHSTDAT0);
387 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
388 for (i = 1; i <= data->block[0]; i++)
389 data->block[i] = inb_p(SMBBLKDAT);
390 break;
392 return 0;
395 static u32 piix4_func(struct i2c_adapter *adapter)
397 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
398 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
399 I2C_FUNC_SMBUS_BLOCK_DATA;
402 static struct i2c_algorithm smbus_algorithm = {
403 .name = "Non-I2C SMBus adapter",
404 .id = I2C_ALGO_SMBUS,
405 .smbus_xfer = piix4_access,
406 .functionality = piix4_func,
409 static struct i2c_adapter piix4_adapter = {
410 .owner = THIS_MODULE,
411 .class = I2C_CLASS_HWMON,
412 .algo = &smbus_algorithm,
413 .name = "unset",
416 static struct pci_device_id piix4_ids[] = {
417 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3),
418 .driver_data = 3 },
419 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_OSB4),
420 .driver_data = 0 },
421 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_CSB5),
422 .driver_data = 0 },
423 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_CSB6),
424 .driver_data = 0 },
425 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3),
426 .driver_data = 3 },
427 { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3),
428 .driver_data = 0 },
429 { 0, }
432 MODULE_DEVICE_TABLE (pci, piix4_ids);
434 static int __devinit piix4_probe(struct pci_dev *dev,
435 const struct pci_device_id *id)
437 int retval;
439 retval = piix4_setup(dev, id);
440 if (retval)
441 return retval;
443 /* set up the driverfs linkage to our parent device */
444 piix4_adapter.dev.parent = &dev->dev;
446 snprintf(piix4_adapter.name, I2C_NAME_SIZE,
447 "SMBus PIIX4 adapter at %04x", piix4_smba);
449 if ((retval = i2c_add_adapter(&piix4_adapter))) {
450 dev_err(&dev->dev, "Couldn't register adapter!\n");
451 release_region(piix4_smba, SMBIOSIZE);
452 piix4_smba = 0;
455 return retval;
458 static void __devexit piix4_remove(struct pci_dev *dev)
460 if (piix4_smba) {
461 i2c_del_adapter(&piix4_adapter);
462 release_region(piix4_smba, SMBIOSIZE);
463 piix4_smba = 0;
467 static struct pci_driver piix4_driver = {
468 .name = "piix4_smbus",
469 .id_table = piix4_ids,
470 .probe = piix4_probe,
471 .remove = __devexit_p(piix4_remove),
474 static int __init i2c_piix4_init(void)
476 return pci_register_driver(&piix4_driver);
479 static void __exit i2c_piix4_exit(void)
481 pci_unregister_driver(&piix4_driver);
484 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
485 "Philip Edelbrock <phil@netroedge.com>");
486 MODULE_DESCRIPTION("PIIX4 SMBus driver");
487 MODULE_LICENSE("GPL");
489 module_init(i2c_piix4_init);
490 module_exit(i2c_piix4_exit);