blob: 7217410f4251a99baff369647535e912c40ab4d4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
6
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.
11
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.
16
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.
20*/
21
22/*
23 Supports:
24 Intel PIIX4, 440MX
Martin Devera5f7ea3c2006-02-27 23:11:45 +010025 Serverworks OSB4, CSB5, CSB6, HT-1000
Shane Huang60693e52007-08-30 23:56:38 -070026 ATI IXP200, IXP300, IXP400, SB600, SB700, SB800
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 SMSC Victory66
28
29 Note: we assume there can only be one device, with one SMBus interface.
30*/
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ioport.h>
39#include <linux/i2c.h>
40#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/dmi.h>
42#include <asm/io.h>
43
44
45struct sd {
46 const unsigned short mfr;
47 const unsigned short dev;
48 const unsigned char fn;
49 const char *name;
50};
51
52/* PIIX4 SMBus address offsets */
53#define SMBHSTSTS (0 + piix4_smba)
54#define SMBHSLVSTS (1 + piix4_smba)
55#define SMBHSTCNT (2 + piix4_smba)
56#define SMBHSTCMD (3 + piix4_smba)
57#define SMBHSTADD (4 + piix4_smba)
58#define SMBHSTDAT0 (5 + piix4_smba)
59#define SMBHSTDAT1 (6 + piix4_smba)
60#define SMBBLKDAT (7 + piix4_smba)
61#define SMBSLVCNT (8 + piix4_smba)
62#define SMBSHDWCMD (9 + piix4_smba)
63#define SMBSLVEVT (0xA + piix4_smba)
64#define SMBSLVDAT (0xC + piix4_smba)
65
66/* count for request_region */
67#define SMBIOSIZE 8
68
69/* PCI Address Constants */
70#define SMBBA 0x090
71#define SMBHSTCFG 0x0D2
72#define SMBSLVC 0x0D3
73#define SMBSHDW1 0x0D4
74#define SMBSHDW2 0x0D5
75#define SMBREV 0x0D6
76
77/* Other settings */
78#define MAX_TIMEOUT 500
79#define ENABLE_INT9 0
80
81/* PIIX4 constants */
82#define PIIX4_QUICK 0x00
83#define PIIX4_BYTE 0x04
84#define PIIX4_BYTE_DATA 0x08
85#define PIIX4_WORD_DATA 0x0C
86#define PIIX4_BLOCK_DATA 0x14
87
88/* insmod parameters */
89
90/* If force is set to anything different from 0, we forcibly enable the
91 PIIX4. DANGEROUS! */
Jean Delvare60507092005-09-25 16:23:07 +020092static int force;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093module_param (force, int, 0);
94MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!");
95
96/* If force_addr is set to anything different from 0, we forcibly enable
97 the PIIX4 at the given address. VERY DANGEROUS! */
Jean Delvare60507092005-09-25 16:23:07 +020098static int force_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099module_param (force_addr, int, 0);
100MODULE_PARM_DESC(force_addr,
101 "Forcibly enable the PIIX4 at the given address. "
102 "EXTREMELY DANGEROUS!");
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static int piix4_transaction(void);
105
Jean Delvare60507092005-09-25 16:23:07 +0200106static unsigned short piix4_smba;
David Milburnb1c17592008-05-11 20:37:05 +0200107static int srvrworks_csb5_delay;
Jean Delvared6072f82005-09-25 16:37:04 +0200108static struct pci_driver piix4_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static struct i2c_adapter piix4_adapter;
110
111static struct dmi_system_id __devinitdata piix4_dmi_table[] = {
112 {
113 .ident = "IBM",
114 .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), },
115 },
116 { },
117};
118
119static int __devinit piix4_setup(struct pci_dev *PIIX4_dev,
120 const struct pci_device_id *id)
121{
122 unsigned char temp;
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 dev_info(&PIIX4_dev->dev, "Found %s device\n", pci_name(PIIX4_dev));
125
David Milburnb1c17592008-05-11 20:37:05 +0200126 if ((PIIX4_dev->vendor == PCI_VENDOR_ID_SERVERWORKS) &&
127 (PIIX4_dev->device == PCI_DEVICE_ID_SERVERWORKS_CSB5))
128 srvrworks_csb5_delay = 1;
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /* Don't access SMBus on IBM systems which get corrupted eeproms */
131 if (dmi_check_system(piix4_dmi_table) &&
132 PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) {
Jean Delvaref9ba6c02006-04-25 13:37:25 +0200133 dev_err(&PIIX4_dev->dev, "IBM system detected; this module "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 "may corrupt your serial eeprom! Refusing to load "
135 "module!\n");
136 return -EPERM;
137 }
138
139 /* Determine the address of the SMBus areas */
140 if (force_addr) {
141 piix4_smba = force_addr & 0xfff0;
142 force = 0;
143 } else {
144 pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba);
145 piix4_smba &= 0xfff0;
146 if(piix4_smba == 0) {
147 dev_err(&PIIX4_dev->dev, "SMB base address "
148 "uninitialized - upgrade BIOS or use "
149 "force_addr=0xaddr\n");
150 return -ENODEV;
151 }
152 }
153
Jean Delvared6072f82005-09-25 16:37:04 +0200154 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 dev_err(&PIIX4_dev->dev, "SMB region 0x%x already in use!\n",
156 piix4_smba);
157 return -ENODEV;
158 }
159
160 pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /* If force_addr is set, we program the new address here. Just to make
163 sure, we disable the PIIX4 first. */
164 if (force_addr) {
165 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe);
166 pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba);
167 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01);
168 dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to "
169 "new address %04x!\n", piix4_smba);
170 } else if ((temp & 1) == 0) {
171 if (force) {
172 /* This should never need to be done, but has been
173 * noted that many Dell machines have the SMBus
174 * interface on the PIIX4 disabled!? NOTE: This assumes
175 * I/O space and other allocations WERE done by the
176 * Bios! Don't complain if your hardware does weird
177 * things after enabling this. :') Check for Bios
178 * updates before resorting to this.
179 */
180 pci_write_config_byte(PIIX4_dev, SMBHSTCFG,
181 temp | 1);
182 dev_printk(KERN_NOTICE, &PIIX4_dev->dev,
183 "WARNING: SMBus interface has been "
184 "FORCEFULLY ENABLED!\n");
185 } else {
186 dev_err(&PIIX4_dev->dev,
187 "Host SMBus controller not enabled!\n");
188 release_region(piix4_smba, SMBIOSIZE);
189 piix4_smba = 0;
190 return -ENODEV;
191 }
192 }
193
Rudolf Marek54aaa1c2006-04-25 13:06:41 +0200194 if (((temp & 0x0E) == 8) || ((temp & 0x0E) == 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 dev_dbg(&PIIX4_dev->dev, "Using Interrupt 9 for SMBus.\n");
196 else if ((temp & 0x0E) == 0)
197 dev_dbg(&PIIX4_dev->dev, "Using Interrupt SMI# for SMBus.\n");
198 else
199 dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration "
200 "(or code out of date)!\n");
201
202 pci_read_config_byte(PIIX4_dev, SMBREV, &temp);
203 dev_dbg(&PIIX4_dev->dev, "SMBREV = 0x%X\n", temp);
204 dev_dbg(&PIIX4_dev->dev, "SMBA = 0x%X\n", piix4_smba);
205
206 return 0;
207}
208
209/* Another internally used function */
210static int piix4_transaction(void)
211{
212 int temp;
213 int result = 0;
214 int timeout = 0;
215
216 dev_dbg(&piix4_adapter.dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
217 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
218 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
219 inb_p(SMBHSTDAT1));
220
221 /* Make sure the SMBus host is ready to start transmitting */
222 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
223 dev_dbg(&piix4_adapter.dev, "SMBus busy (%02x). "
Jean Delvare541e6a02005-06-23 22:18:08 +0200224 "Resetting...\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 outb_p(temp, SMBHSTSTS);
226 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
227 dev_err(&piix4_adapter.dev, "Failed! (%02x)\n", temp);
228 return -1;
229 } else {
Jean Delvarec5d21b72008-04-29 23:11:37 +0200230 dev_dbg(&piix4_adapter.dev, "Successful!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232 }
233
234 /* start the transaction by setting bit 6 */
235 outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
236
237 /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
David Milburnb1c17592008-05-11 20:37:05 +0200238 if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */
239 msleep(2);
240 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 msleep(1);
David Milburnb1c17592008-05-11 20:37:05 +0200242
243 while ((timeout++ < MAX_TIMEOUT) &&
244 ((temp = inb_p(SMBHSTSTS)) & 0x01))
245 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 /* If the SMBus is still busy, we give up */
248 if (timeout >= MAX_TIMEOUT) {
249 dev_err(&piix4_adapter.dev, "SMBus Timeout!\n");
250 result = -1;
251 }
252
253 if (temp & 0x10) {
254 result = -1;
255 dev_err(&piix4_adapter.dev, "Error: Failed bus transaction\n");
256 }
257
258 if (temp & 0x08) {
259 result = -1;
260 dev_dbg(&piix4_adapter.dev, "Bus collision! SMBus may be "
261 "locked until next hard reset. (sorry!)\n");
262 /* Clock stops and slave is stuck in mid-transmission */
263 }
264
265 if (temp & 0x04) {
266 result = -1;
267 dev_dbg(&piix4_adapter.dev, "Error: no response!\n");
268 }
269
270 if (inb_p(SMBHSTSTS) != 0x00)
271 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
272
273 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
274 dev_err(&piix4_adapter.dev, "Failed reset at end of "
275 "transaction (%02x)\n", temp);
276 }
277 dev_dbg(&piix4_adapter.dev, "Transaction (post): CNT=%02x, CMD=%02x, "
278 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
279 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
280 inb_p(SMBHSTDAT1));
281 return result;
282}
283
284/* Return -1 on error. */
285static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
286 unsigned short flags, char read_write,
287 u8 command, int size, union i2c_smbus_data * data)
288{
289 int i, len;
290
291 switch (size) {
292 case I2C_SMBUS_PROC_CALL:
293 dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
294 return -1;
295 case I2C_SMBUS_QUICK:
296 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
297 SMBHSTADD);
298 size = PIIX4_QUICK;
299 break;
300 case I2C_SMBUS_BYTE:
301 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
302 SMBHSTADD);
303 if (read_write == I2C_SMBUS_WRITE)
304 outb_p(command, SMBHSTCMD);
305 size = PIIX4_BYTE;
306 break;
307 case I2C_SMBUS_BYTE_DATA:
308 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
309 SMBHSTADD);
310 outb_p(command, SMBHSTCMD);
311 if (read_write == I2C_SMBUS_WRITE)
312 outb_p(data->byte, SMBHSTDAT0);
313 size = PIIX4_BYTE_DATA;
314 break;
315 case I2C_SMBUS_WORD_DATA:
316 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
317 SMBHSTADD);
318 outb_p(command, SMBHSTCMD);
319 if (read_write == I2C_SMBUS_WRITE) {
320 outb_p(data->word & 0xff, SMBHSTDAT0);
321 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
322 }
323 size = PIIX4_WORD_DATA;
324 break;
325 case I2C_SMBUS_BLOCK_DATA:
326 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
327 SMBHSTADD);
328 outb_p(command, SMBHSTCMD);
329 if (read_write == I2C_SMBUS_WRITE) {
330 len = data->block[0];
331 if (len < 0)
332 len = 0;
333 if (len > 32)
334 len = 32;
335 outb_p(len, SMBHSTDAT0);
336 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
337 for (i = 1; i <= len; i++)
338 outb_p(data->block[i], SMBBLKDAT);
339 }
340 size = PIIX4_BLOCK_DATA;
341 break;
342 }
343
344 outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
345
346 if (piix4_transaction()) /* Error in transaction */
347 return -1;
348
349 if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
350 return 0;
351
352
353 switch (size) {
Jean Delvare3578a072008-04-29 23:11:37 +0200354 case PIIX4_BYTE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 case PIIX4_BYTE_DATA:
356 data->byte = inb_p(SMBHSTDAT0);
357 break;
358 case PIIX4_WORD_DATA:
359 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
360 break;
361 case PIIX4_BLOCK_DATA:
362 data->block[0] = inb_p(SMBHSTDAT0);
363 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
364 for (i = 1; i <= data->block[0]; i++)
365 data->block[i] = inb_p(SMBBLKDAT);
366 break;
367 }
368 return 0;
369}
370
371static u32 piix4_func(struct i2c_adapter *adapter)
372{
373 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
374 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
375 I2C_FUNC_SMBUS_BLOCK_DATA;
376}
377
Jean Delvare8f9082c2006-09-03 22:39:46 +0200378static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 .smbus_xfer = piix4_access,
380 .functionality = piix4_func,
381};
382
383static struct i2c_adapter piix4_adapter = {
384 .owner = THIS_MODULE,
Stephen Hemminger9ace5552007-02-13 22:09:01 +0100385 .id = I2C_HW_SMBUS_PIIX4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 .class = I2C_CLASS_HWMON,
387 .algo = &smbus_algorithm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388};
389
390static struct pci_device_id piix4_ids[] = {
Jean Delvare9b7389c2008-01-27 18:14:51 +0100391 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
392 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3) },
393 { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3) },
394 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_SMBUS) },
395 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_SMBUS) },
396 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS) },
397 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS) },
398 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
399 PCI_DEVICE_ID_SERVERWORKS_OSB4) },
400 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
401 PCI_DEVICE_ID_SERVERWORKS_CSB5) },
402 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
403 PCI_DEVICE_ID_SERVERWORKS_CSB6) },
404 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
405 PCI_DEVICE_ID_SERVERWORKS_HT1000SB) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 { 0, }
407};
408
409MODULE_DEVICE_TABLE (pci, piix4_ids);
410
411static int __devinit piix4_probe(struct pci_dev *dev,
412 const struct pci_device_id *id)
413{
414 int retval;
415
416 retval = piix4_setup(dev, id);
417 if (retval)
418 return retval;
419
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100420 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 piix4_adapter.dev.parent = &dev->dev;
422
David Brownell2096b952007-05-01 23:26:28 +0200423 snprintf(piix4_adapter.name, sizeof(piix4_adapter.name),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 "SMBus PIIX4 adapter at %04x", piix4_smba);
425
426 if ((retval = i2c_add_adapter(&piix4_adapter))) {
427 dev_err(&dev->dev, "Couldn't register adapter!\n");
428 release_region(piix4_smba, SMBIOSIZE);
429 piix4_smba = 0;
430 }
431
432 return retval;
433}
434
435static void __devexit piix4_remove(struct pci_dev *dev)
436{
437 if (piix4_smba) {
438 i2c_del_adapter(&piix4_adapter);
439 release_region(piix4_smba, SMBIOSIZE);
440 piix4_smba = 0;
441 }
442}
443
444static struct pci_driver piix4_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 .name = "piix4_smbus",
446 .id_table = piix4_ids,
447 .probe = piix4_probe,
448 .remove = __devexit_p(piix4_remove),
449};
450
451static int __init i2c_piix4_init(void)
452{
453 return pci_register_driver(&piix4_driver);
454}
455
456static void __exit i2c_piix4_exit(void)
457{
458 pci_unregister_driver(&piix4_driver);
459}
460
461MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
462 "Philip Edelbrock <phil@netroedge.com>");
463MODULE_DESCRIPTION("PIIX4 SMBus driver");
464MODULE_LICENSE("GPL");
465
466module_init(i2c_piix4_init);
467module_exit(i2c_piix4_exit);