2 ali15x3.c - Part of lm_sensors, Linux kernel modules for hardware
4 Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com> and
6 Mark D. Studebaker <mdsxyz123@yahoo.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 This is the driver for the SMB Host controller on
25 Acer Labs Inc. (ALI) M1541 and M1543C South Bridges.
27 The M1543C is a South bridge for desktop systems.
28 The M1533 is a South bridge for portable systems.
29 They are part of the following ALI chipsets:
30 "Aladdin Pro 2": Includes the M1621 Slot 1 North bridge
31 with AGP and 100MHz CPU Front Side bus
32 "Aladdin V": Includes the M1541 Socket 7 North bridge
33 with AGP and 100MHz CPU Front Side bus
34 "Aladdin IV": Includes the M1541 Socket 7 North bridge
35 with host bus up to 83.3 MHz.
36 For an overview of these chips see http://www.acerlabs.com
38 The M1533/M1543C devices appear as FOUR separate devices
39 on the PCI bus. An output of lspci will show something similar
42 00:02.0 USB Controller: Acer Laboratories Inc. M5237
43 00:03.0 Bridge: Acer Laboratories Inc. M7101
44 00:07.0 ISA bridge: Acer Laboratories Inc. M1533
45 00:0f.0 IDE interface: Acer Laboratories Inc. M5229
47 The SMB controller is part of the 7101 device, which is an
48 ACPI-compliant Power Management Unit (PMU).
50 The whole 7101 device has to be enabled for the SMB to work.
51 You can't just enable the SMB alone.
52 The SMB and the ACPI have separate I/O spaces.
53 We make sure that the SMB is enabled. We leave the ACPI alone.
55 This driver controls the SMB Host only.
56 The SMB Slave controller on the M15X3 is not enabled.
58 This driver does not use interrupts.
61 /* Note: we assume there can only be one ALI15X3, with one SMBus interface */
63 #include <linux/module.h>
64 #include <linux/pci.h>
65 #include <linux/kernel.h>
66 #include <linux/stddef.h>
67 #include <linux/ioport.h>
68 #include <linux/delay.h>
69 #include <linux/i2c.h>
70 #include <linux/init.h>
73 /* ALI15X3 SMBus address offsets */
74 #define SMBHSTSTS (0 + ali15x3_smba)
75 #define SMBHSTCNT (1 + ali15x3_smba)
76 #define SMBHSTSTART (2 + ali15x3_smba)
77 #define SMBHSTCMD (7 + ali15x3_smba)
78 #define SMBHSTADD (3 + ali15x3_smba)
79 #define SMBHSTDAT0 (4 + ali15x3_smba)
80 #define SMBHSTDAT1 (5 + ali15x3_smba)
81 #define SMBBLKDAT (6 + ali15x3_smba)
83 /* PCI Address Constants */
86 #define SMBATPC 0x05B /* used to unlock xxxBA registers */
87 #define SMBHSTCFG 0x0E0
93 #define MAX_TIMEOUT 200 /* times 1/100 sec */
94 #define ALI15X3_SMB_IOSIZE 32
96 /* this is what the Award 1004 BIOS sets them to on a ASUS P5A MB.
97 We don't use these here. If the bases aren't set to some value we
98 tell user to upgrade BIOS and we fail.
100 #define ALI15X3_SMB_DEFAULTBASE 0xE800
102 /* ALI15X3 address lock bits */
103 #define ALI15X3_LOCK 0x06
105 /* ALI15X3 command constants */
106 #define ALI15X3_ABORT 0x02
107 #define ALI15X3_T_OUT 0x04
108 #define ALI15X3_QUICK 0x00
109 #define ALI15X3_BYTE 0x10
110 #define ALI15X3_BYTE_DATA 0x20
111 #define ALI15X3_WORD_DATA 0x30
112 #define ALI15X3_BLOCK_DATA 0x40
113 #define ALI15X3_BLOCK_CLR 0x80
115 /* ALI15X3 status register bits */
116 #define ALI15X3_STS_IDLE 0x04
117 #define ALI15X3_STS_BUSY 0x08
118 #define ALI15X3_STS_DONE 0x10
119 #define ALI15X3_STS_DEV 0x20 /* device error */
120 #define ALI15X3_STS_COLL 0x40 /* collision or no response */
121 #define ALI15X3_STS_TERM 0x80 /* terminated by abort */
122 #define ALI15X3_STS_ERR 0xE0 /* all the bad error bits */
125 /* If force_addr is set to anything different from 0, we forcibly enable
126 the device at the given address. */
127 static u16 force_addr;
128 module_param(force_addr, ushort, 0);
129 MODULE_PARM_DESC(force_addr,
130 "Initialize the base address of the i2c controller");
132 static struct pci_driver ali15x3_driver;
133 static unsigned short ali15x3_smba;
135 static int ali15x3_setup(struct pci_dev *ALI15X3_dev)
140 /* Check the following things:
141 - SMB I/O address is initialized
143 - We can use the addresses
146 /* Unlock the register.
147 The data sheet says that the address registers are read-only
148 if the lock bits are 1, but in fact the address registers
149 are zero unless you clear the lock bits.
151 pci_read_config_byte(ALI15X3_dev, SMBATPC, &temp);
152 if (temp & ALI15X3_LOCK) {
153 temp &= ~ALI15X3_LOCK;
154 pci_write_config_byte(ALI15X3_dev, SMBATPC, temp);
157 /* Determine the address of the SMBus area */
158 pci_read_config_word(ALI15X3_dev, SMBBA, &ali15x3_smba);
159 ali15x3_smba &= (0xffff & ~(ALI15X3_SMB_IOSIZE - 1));
160 if (ali15x3_smba == 0 && force_addr == 0) {
161 dev_err(&ALI15X3_dev->dev, "ALI15X3_smb region uninitialized "
162 "- upgrade BIOS or use force_addr=0xaddr\n");
167 ali15x3_smba = force_addr & ~(ALI15X3_SMB_IOSIZE - 1);
169 if (!request_region(ali15x3_smba, ALI15X3_SMB_IOSIZE,
170 ali15x3_driver.name)) {
171 dev_err(&ALI15X3_dev->dev,
172 "ALI15X3_smb region 0x%x already in use!\n",
178 dev_info(&ALI15X3_dev->dev, "forcing ISA address 0x%04X\n",
180 if (PCIBIOS_SUCCESSFUL != pci_write_config_word(ALI15X3_dev,
184 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(ALI15X3_dev,
187 if ((a & ~(ALI15X3_SMB_IOSIZE - 1)) != ali15x3_smba) {
188 /* make sure it works */
189 dev_err(&ALI15X3_dev->dev,
190 "force address failed - not supported?\n");
194 /* check if whole device is enabled */
195 pci_read_config_byte(ALI15X3_dev, SMBCOM, &temp);
196 if ((temp & 1) == 0) {
197 dev_info(&ALI15X3_dev->dev, "enabling SMBus device\n");
198 pci_write_config_byte(ALI15X3_dev, SMBCOM, temp | 0x01);
201 /* Is SMB Host controller enabled? */
202 pci_read_config_byte(ALI15X3_dev, SMBHSTCFG, &temp);
203 if ((temp & 1) == 0) {
204 dev_info(&ALI15X3_dev->dev, "enabling SMBus controller\n");
205 pci_write_config_byte(ALI15X3_dev, SMBHSTCFG, temp | 0x01);
208 /* set SMB clock to 74KHz as recommended in data sheet */
209 pci_write_config_byte(ALI15X3_dev, SMBCLK, 0x20);
212 The interrupt routing for SMB is set up in register 0x77 in the
213 1533 ISA Bridge device, NOT in the 7101 device.
214 Don't bother with finding the 1533 device and reading the register.
215 if ((....... & 0x0F) == 1)
216 dev_dbg(&ALI15X3_dev->dev, "ALI15X3 using Interrupt 9 for SMBus.\n");
218 pci_read_config_byte(ALI15X3_dev, SMBREV, &temp);
219 dev_dbg(&ALI15X3_dev->dev, "SMBREV = 0x%X\n", temp);
220 dev_dbg(&ALI15X3_dev->dev, "iALI15X3_smba = 0x%X\n", ali15x3_smba);
224 release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
228 /* Another internally used function */
229 static int ali15x3_transaction(struct i2c_adapter *adap)
235 dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, CNT=%02x, CMD=%02x, "
236 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTSTS),
237 inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),
238 inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
241 temp = inb_p(SMBHSTSTS);
243 /* Make sure the SMBus host is ready to start transmitting */
244 /* Check the busy bit first */
245 if (temp & ALI15X3_STS_BUSY) {
247 If the host controller is still busy, it may have timed out in the
248 previous transaction, resulting in a "SMBus Timeout" Dev.
249 I've tried the following to reset a stuck busy bit.
250 1. Reset the controller with an ABORT command.
251 (this doesn't seem to clear the controller if an external
253 2. Reset the controller and the other SMBus devices with a
254 T_OUT command. (this clears the host busy bit if an
255 external device is hung, but it comes back upon a new access
257 3. Disable and reenable the controller in SMBHSTCFG
258 Worst case, nothing seems to work except power reset.
260 /* Abort - reset the host controller */
262 Try resetting entire SMB bus, including other devices -
263 This may not work either - it clears the BUSY bit but
264 then the BUSY bit may come back on when you try and use the chip again.
265 If that's the case you are stuck.
267 dev_info(&adap->dev, "Resetting entire SMB Bus to "
268 "clear busy condition (%02x)\n", temp);
269 outb_p(ALI15X3_T_OUT, SMBHSTCNT);
270 temp = inb_p(SMBHSTSTS);
273 /* now check the error bits and the busy bit */
274 if (temp & (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) {
275 /* do a clear-on-write */
276 outb_p(0xFF, SMBHSTSTS);
277 if ((temp = inb_p(SMBHSTSTS)) &
278 (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) {
279 /* this is probably going to be correctable only by a power reset
280 as one of the bits now appears to be stuck */
281 /* This may be a bus or device with electrical problems. */
282 dev_err(&adap->dev, "SMBus reset failed! (0x%02x) - "
283 "controller or device on bus is probably hung\n",
288 /* check and clear done bit */
289 if (temp & ALI15X3_STS_DONE) {
290 outb_p(temp, SMBHSTSTS);
294 /* start the transaction by writing anything to the start register */
295 outb_p(0xFF, SMBHSTSTART);
297 /* We will always wait for a fraction of a second! */
301 temp = inb_p(SMBHSTSTS);
302 } while ((!(temp & (ALI15X3_STS_ERR | ALI15X3_STS_DONE)))
303 && (timeout++ < MAX_TIMEOUT));
305 /* If the SMBus is still busy, we give up */
306 if (timeout >= MAX_TIMEOUT) {
308 dev_err(&adap->dev, "SMBus Timeout!\n");
311 if (temp & ALI15X3_STS_TERM) {
313 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
317 Unfortunately the ALI SMB controller maps "no response" and "bus
318 collision" into a single bit. No reponse is the usual case so don't
320 This means that bus collisions go unreported.
322 if (temp & ALI15X3_STS_COLL) {
325 "Error: no response or bus collision ADD=%02x\n",
329 /* haven't ever seen this */
330 if (temp & ALI15X3_STS_DEV) {
332 dev_err(&adap->dev, "Error: device error\n");
334 dev_dbg(&adap->dev, "Transaction (post): STS=%02x, CNT=%02x, CMD=%02x, "
335 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTSTS),
336 inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),
337 inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
341 /* Return -1 on error. */
342 static s32 ali15x3_access(struct i2c_adapter * adap, u16 addr,
343 unsigned short flags, char read_write, u8 command,
344 int size, union i2c_smbus_data * data)
350 /* clear all the bits (clear-on-write) */
351 outb_p(0xFF, SMBHSTSTS);
352 /* make sure SMBus is idle */
353 temp = inb_p(SMBHSTSTS);
355 (timeout < MAX_TIMEOUT) && !(temp & ALI15X3_STS_IDLE);
358 temp = inb_p(SMBHSTSTS);
360 if (timeout >= MAX_TIMEOUT) {
361 dev_err(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);
365 case I2C_SMBUS_PROC_CALL:
366 dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
368 case I2C_SMBUS_QUICK:
369 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
371 size = ALI15X3_QUICK;
374 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
376 if (read_write == I2C_SMBUS_WRITE)
377 outb_p(command, SMBHSTCMD);
380 case I2C_SMBUS_BYTE_DATA:
381 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
383 outb_p(command, SMBHSTCMD);
384 if (read_write == I2C_SMBUS_WRITE)
385 outb_p(data->byte, SMBHSTDAT0);
386 size = ALI15X3_BYTE_DATA;
388 case I2C_SMBUS_WORD_DATA:
389 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
391 outb_p(command, SMBHSTCMD);
392 if (read_write == I2C_SMBUS_WRITE) {
393 outb_p(data->word & 0xff, SMBHSTDAT0);
394 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
396 size = ALI15X3_WORD_DATA;
398 case I2C_SMBUS_BLOCK_DATA:
399 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
401 outb_p(command, SMBHSTCMD);
402 if (read_write == I2C_SMBUS_WRITE) {
403 len = data->block[0];
406 data->block[0] = len;
410 data->block[0] = len;
412 outb_p(len, SMBHSTDAT0);
413 /* Reset SMBBLKDAT */
414 outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);
415 for (i = 1; i <= len; i++)
416 outb_p(data->block[i], SMBBLKDAT);
418 size = ALI15X3_BLOCK_DATA;
422 outb_p(size, SMBHSTCNT); /* output command */
424 if (ali15x3_transaction(adap)) /* Error in transaction */
427 if ((read_write == I2C_SMBUS_WRITE) || (size == ALI15X3_QUICK))
432 case ALI15X3_BYTE: /* Result put in SMBHSTDAT0 */
433 data->byte = inb_p(SMBHSTDAT0);
435 case ALI15X3_BYTE_DATA:
436 data->byte = inb_p(SMBHSTDAT0);
438 case ALI15X3_WORD_DATA:
439 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
441 case ALI15X3_BLOCK_DATA:
442 len = inb_p(SMBHSTDAT0);
445 data->block[0] = len;
446 /* Reset SMBBLKDAT */
447 outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);
448 for (i = 1; i <= data->block[0]; i++) {
449 data->block[i] = inb_p(SMBBLKDAT);
450 dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",
451 len, i, data->block[i]);
458 static u32 ali15x3_func(struct i2c_adapter *adapter)
460 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
461 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
462 I2C_FUNC_SMBUS_BLOCK_DATA;
465 static const struct i2c_algorithm smbus_algorithm = {
466 .smbus_xfer = ali15x3_access,
467 .functionality = ali15x3_func,
470 static struct i2c_adapter ali15x3_adapter = {
471 .owner = THIS_MODULE,
472 .id = I2C_HW_SMBUS_ALI15X3,
473 .class = I2C_CLASS_HWMON,
474 .algo = &smbus_algorithm,
477 static struct pci_device_id ali15x3_ids[] = {
478 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
482 MODULE_DEVICE_TABLE (pci, ali15x3_ids);
484 static int __devinit ali15x3_probe(struct pci_dev *dev, const struct pci_device_id *id)
486 if (ali15x3_setup(dev)) {
488 "ALI15X3 not detected, module not inserted.\n");
492 /* set up the driverfs linkage to our parent device */
493 ali15x3_adapter.dev.parent = &dev->dev;
495 snprintf(ali15x3_adapter.name, I2C_NAME_SIZE,
496 "SMBus ALI15X3 adapter at %04x", ali15x3_smba);
497 return i2c_add_adapter(&ali15x3_adapter);
500 static void __devexit ali15x3_remove(struct pci_dev *dev)
502 i2c_del_adapter(&ali15x3_adapter);
503 release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
506 static struct pci_driver ali15x3_driver = {
507 .name = "ali15x3_smbus",
508 .id_table = ali15x3_ids,
509 .probe = ali15x3_probe,
510 .remove = __devexit_p(ali15x3_remove),
513 static int __init i2c_ali15x3_init(void)
515 return pci_register_driver(&ali15x3_driver);
518 static void __exit i2c_ali15x3_exit(void)
520 pci_unregister_driver(&ali15x3_driver);
523 MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl>, "
524 "Philip Edelbrock <phil@netroedge.com>, "
525 "and Mark D. Studebaker <mdsxyz123@yahoo.com>");
526 MODULE_DESCRIPTION("ALI15X3 SMBus driver");
527 MODULE_LICENSE("GPL");
529 module_init(i2c_ali15x3_init);
530 module_exit(i2c_ali15x3_exit);