1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> and
4 Philip Edelbrock <phil@netroedge.com> and
5 Mark D. Studebaker <mdsxyz123@yahoo.com>
10 This is the driver for the SMB Host controller on
11 Acer Labs Inc. (ALI) M1541 and M1543C South Bridges.
13 The M1543C is a South bridge for desktop systems.
14 The M1533 is a South bridge for portable systems.
15 They are part of the following ALI chipsets:
16 "Aladdin Pro 2": Includes the M1621 Slot 1 North bridge
17 with AGP and 100MHz CPU Front Side bus
18 "Aladdin V": Includes the M1541 Socket 7 North bridge
19 with AGP and 100MHz CPU Front Side bus
20 "Aladdin IV": Includes the M1541 Socket 7 North bridge
21 with host bus up to 83.3 MHz.
22 For an overview of these chips see http://www.acerlabs.com
24 The M1533/M1543C devices appear as FOUR separate devices
25 on the PCI bus. An output of lspci will show something similar
28 00:02.0 USB Controller: Acer Laboratories Inc. M5237
29 00:03.0 Bridge: Acer Laboratories Inc. M7101
30 00:07.0 ISA bridge: Acer Laboratories Inc. M1533
31 00:0f.0 IDE interface: Acer Laboratories Inc. M5229
33 The SMB controller is part of the 7101 device, which is an
34 ACPI-compliant Power Management Unit (PMU).
36 The whole 7101 device has to be enabled for the SMB to work.
37 You can't just enable the SMB alone.
38 The SMB and the ACPI have separate I/O spaces.
39 We make sure that the SMB is enabled. We leave the ACPI alone.
41 This driver controls the SMB Host only.
42 The SMB Slave controller on the M15X3 is not enabled.
44 This driver does not use interrupts.
47 /* Note: we assume there can only be one ALI15X3, with one SMBus interface */
49 #include <linux/module.h>
50 #include <linux/pci.h>
51 #include <linux/kernel.h>
52 #include <linux/stddef.h>
53 #include <linux/ioport.h>
54 #include <linux/delay.h>
55 #include <linux/i2c.h>
56 #include <linux/acpi.h>
59 /* ALI15X3 SMBus address offsets */
60 #define SMBHSTSTS (0 + ali15x3_smba)
61 #define SMBHSTCNT (1 + ali15x3_smba)
62 #define SMBHSTSTART (2 + ali15x3_smba)
63 #define SMBHSTCMD (7 + ali15x3_smba)
64 #define SMBHSTADD (3 + ali15x3_smba)
65 #define SMBHSTDAT0 (4 + ali15x3_smba)
66 #define SMBHSTDAT1 (5 + ali15x3_smba)
67 #define SMBBLKDAT (6 + ali15x3_smba)
69 /* PCI Address Constants */
72 #define SMBATPC 0x05B /* used to unlock xxxBA registers */
73 #define SMBHSTCFG 0x0E0
79 #define MAX_TIMEOUT 200 /* times 1/100 sec */
80 #define ALI15X3_SMB_IOSIZE 32
82 /* this is what the Award 1004 BIOS sets them to on a ASUS P5A MB.
83 We don't use these here. If the bases aren't set to some value we
84 tell user to upgrade BIOS and we fail.
86 #define ALI15X3_SMB_DEFAULTBASE 0xE800
88 /* ALI15X3 address lock bits */
89 #define ALI15X3_LOCK 0x06
91 /* ALI15X3 command constants */
92 #define ALI15X3_ABORT 0x02
93 #define ALI15X3_T_OUT 0x04
94 #define ALI15X3_QUICK 0x00
95 #define ALI15X3_BYTE 0x10
96 #define ALI15X3_BYTE_DATA 0x20
97 #define ALI15X3_WORD_DATA 0x30
98 #define ALI15X3_BLOCK_DATA 0x40
99 #define ALI15X3_BLOCK_CLR 0x80
101 /* ALI15X3 status register bits */
102 #define ALI15X3_STS_IDLE 0x04
103 #define ALI15X3_STS_BUSY 0x08
104 #define ALI15X3_STS_DONE 0x10
105 #define ALI15X3_STS_DEV 0x20 /* device error */
106 #define ALI15X3_STS_COLL 0x40 /* collision or no response */
107 #define ALI15X3_STS_TERM 0x80 /* terminated by abort */
108 #define ALI15X3_STS_ERR 0xE0 /* all the bad error bits */
111 /* If force_addr is set to anything different from 0, we forcibly enable
112 the device at the given address. */
113 static u16 force_addr;
114 module_param_hw(force_addr, ushort, ioport, 0);
115 MODULE_PARM_DESC(force_addr,
116 "Initialize the base address of the i2c controller");
118 static struct pci_driver ali15x3_driver;
119 static unsigned short ali15x3_smba;
121 static int ali15x3_setup(struct pci_dev *ALI15X3_dev)
126 /* Check the following things:
127 - SMB I/O address is initialized
129 - We can use the addresses
132 /* Unlock the register.
133 The data sheet says that the address registers are read-only
134 if the lock bits are 1, but in fact the address registers
135 are zero unless you clear the lock bits.
137 pci_read_config_byte(ALI15X3_dev, SMBATPC, &temp);
138 if (temp & ALI15X3_LOCK) {
139 temp &= ~ALI15X3_LOCK;
140 pci_write_config_byte(ALI15X3_dev, SMBATPC, temp);
143 /* Determine the address of the SMBus area */
144 pci_read_config_word(ALI15X3_dev, SMBBA, &ali15x3_smba);
145 ali15x3_smba &= (0xffff & ~(ALI15X3_SMB_IOSIZE - 1));
146 if (ali15x3_smba == 0 && force_addr == 0) {
147 dev_err(&ALI15X3_dev->dev, "ALI15X3_smb region uninitialized "
148 "- upgrade BIOS or use force_addr=0xaddr\n");
153 ali15x3_smba = force_addr & ~(ALI15X3_SMB_IOSIZE - 1);
155 if (acpi_check_region(ali15x3_smba, ALI15X3_SMB_IOSIZE,
156 ali15x3_driver.name))
159 if (!request_region(ali15x3_smba, ALI15X3_SMB_IOSIZE,
160 ali15x3_driver.name)) {
161 dev_err(&ALI15X3_dev->dev,
162 "ALI15X3_smb region 0x%x already in use!\n",
170 dev_info(&ALI15X3_dev->dev, "forcing ISA address 0x%04X\n",
172 ret = pci_write_config_word(ALI15X3_dev, SMBBA, ali15x3_smba);
173 if (ret != PCIBIOS_SUCCESSFUL)
175 ret = pci_read_config_word(ALI15X3_dev, SMBBA, &a);
176 if (ret != PCIBIOS_SUCCESSFUL)
178 if ((a & ~(ALI15X3_SMB_IOSIZE - 1)) != ali15x3_smba) {
179 /* make sure it works */
180 dev_err(&ALI15X3_dev->dev,
181 "force address failed - not supported?\n");
185 /* check if whole device is enabled */
186 pci_read_config_byte(ALI15X3_dev, SMBCOM, &temp);
187 if ((temp & 1) == 0) {
188 dev_info(&ALI15X3_dev->dev, "enabling SMBus device\n");
189 pci_write_config_byte(ALI15X3_dev, SMBCOM, temp | 0x01);
192 /* Is SMB Host controller enabled? */
193 pci_read_config_byte(ALI15X3_dev, SMBHSTCFG, &temp);
194 if ((temp & 1) == 0) {
195 dev_info(&ALI15X3_dev->dev, "enabling SMBus controller\n");
196 pci_write_config_byte(ALI15X3_dev, SMBHSTCFG, temp | 0x01);
199 /* set SMB clock to 74KHz as recommended in data sheet */
200 pci_write_config_byte(ALI15X3_dev, SMBCLK, 0x20);
203 The interrupt routing for SMB is set up in register 0x77 in the
204 1533 ISA Bridge device, NOT in the 7101 device.
205 Don't bother with finding the 1533 device and reading the register.
206 if ((....... & 0x0F) == 1)
207 dev_dbg(&ALI15X3_dev->dev, "ALI15X3 using Interrupt 9 for SMBus.\n");
209 pci_read_config_byte(ALI15X3_dev, SMBREV, &temp);
210 dev_dbg(&ALI15X3_dev->dev, "SMBREV = 0x%X\n", temp);
211 dev_dbg(&ALI15X3_dev->dev, "iALI15X3_smba = 0x%X\n", ali15x3_smba);
215 release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
219 /* Another internally used function */
220 static int ali15x3_transaction(struct i2c_adapter *adap)
226 dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, CNT=%02x, CMD=%02x, "
227 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTSTS),
228 inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),
229 inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
232 temp = inb_p(SMBHSTSTS);
234 /* Make sure the SMBus host is ready to start transmitting */
235 /* Check the busy bit first */
236 if (temp & ALI15X3_STS_BUSY) {
238 If the host controller is still busy, it may have timed out in the
239 previous transaction, resulting in a "SMBus Timeout" Dev.
240 I've tried the following to reset a stuck busy bit.
241 1. Reset the controller with an ABORT command.
242 (this doesn't seem to clear the controller if an external
244 2. Reset the controller and the other SMBus devices with a
245 T_OUT command. (this clears the host busy bit if an
246 external device is hung, but it comes back upon a new access
248 3. Disable and reenable the controller in SMBHSTCFG
249 Worst case, nothing seems to work except power reset.
251 /* Abort - reset the host controller */
253 Try resetting entire SMB bus, including other devices -
254 This may not work either - it clears the BUSY bit but
255 then the BUSY bit may come back on when you try and use the chip again.
256 If that's the case you are stuck.
258 dev_info(&adap->dev, "Resetting entire SMB Bus to "
259 "clear busy condition (%02x)\n", temp);
260 outb_p(ALI15X3_T_OUT, SMBHSTCNT);
261 temp = inb_p(SMBHSTSTS);
264 /* now check the error bits and the busy bit */
265 if (temp & (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) {
266 /* do a clear-on-write */
267 outb_p(0xFF, SMBHSTSTS);
268 if ((temp = inb_p(SMBHSTSTS)) &
269 (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) {
270 /* this is probably going to be correctable only by a power reset
271 as one of the bits now appears to be stuck */
272 /* This may be a bus or device with electrical problems. */
273 dev_err(&adap->dev, "SMBus reset failed! (0x%02x) - "
274 "controller or device on bus is probably hung\n",
279 /* check and clear done bit */
280 if (temp & ALI15X3_STS_DONE) {
281 outb_p(temp, SMBHSTSTS);
285 /* start the transaction by writing anything to the start register */
286 outb_p(0xFF, SMBHSTSTART);
288 /* We will always wait for a fraction of a second! */
292 temp = inb_p(SMBHSTSTS);
293 } while ((!(temp & (ALI15X3_STS_ERR | ALI15X3_STS_DONE)))
294 && (timeout++ < MAX_TIMEOUT));
296 /* If the SMBus is still busy, we give up */
297 if (timeout > MAX_TIMEOUT) {
299 dev_err(&adap->dev, "SMBus Timeout!\n");
302 if (temp & ALI15X3_STS_TERM) {
304 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
308 Unfortunately the ALI SMB controller maps "no response" and "bus
309 collision" into a single bit. No response is the usual case so don't
311 This means that bus collisions go unreported.
313 if (temp & ALI15X3_STS_COLL) {
316 "Error: no response or bus collision ADD=%02x\n",
320 /* haven't ever seen this */
321 if (temp & ALI15X3_STS_DEV) {
323 dev_err(&adap->dev, "Error: device error\n");
325 dev_dbg(&adap->dev, "Transaction (post): STS=%02x, CNT=%02x, CMD=%02x, "
326 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTSTS),
327 inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),
328 inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
332 /* Return negative errno on error. */
333 static s32 ali15x3_access(struct i2c_adapter * adap, u16 addr,
334 unsigned short flags, char read_write, u8 command,
335 int size, union i2c_smbus_data * data)
341 /* clear all the bits (clear-on-write) */
342 outb_p(0xFF, SMBHSTSTS);
343 /* make sure SMBus is idle */
344 temp = inb_p(SMBHSTSTS);
346 (timeout < MAX_TIMEOUT) && !(temp & ALI15X3_STS_IDLE);
349 temp = inb_p(SMBHSTSTS);
351 if (timeout >= MAX_TIMEOUT) {
352 dev_err(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);
356 case I2C_SMBUS_QUICK:
357 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
359 size = ALI15X3_QUICK;
362 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
364 if (read_write == I2C_SMBUS_WRITE)
365 outb_p(command, SMBHSTCMD);
368 case I2C_SMBUS_BYTE_DATA:
369 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
371 outb_p(command, SMBHSTCMD);
372 if (read_write == I2C_SMBUS_WRITE)
373 outb_p(data->byte, SMBHSTDAT0);
374 size = ALI15X3_BYTE_DATA;
376 case I2C_SMBUS_WORD_DATA:
377 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
379 outb_p(command, SMBHSTCMD);
380 if (read_write == I2C_SMBUS_WRITE) {
381 outb_p(data->word & 0xff, SMBHSTDAT0);
382 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
384 size = ALI15X3_WORD_DATA;
386 case I2C_SMBUS_BLOCK_DATA:
387 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
389 outb_p(command, SMBHSTCMD);
390 if (read_write == I2C_SMBUS_WRITE) {
391 len = data->block[0];
394 data->block[0] = len;
398 data->block[0] = len;
400 outb_p(len, SMBHSTDAT0);
401 /* Reset SMBBLKDAT */
402 outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);
403 for (i = 1; i <= len; i++)
404 outb_p(data->block[i], SMBBLKDAT);
406 size = ALI15X3_BLOCK_DATA;
409 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
413 outb_p(size, SMBHSTCNT); /* output command */
415 temp = ali15x3_transaction(adap);
419 if ((read_write == I2C_SMBUS_WRITE) || (size == ALI15X3_QUICK))
424 case ALI15X3_BYTE: /* Result put in SMBHSTDAT0 */
425 data->byte = inb_p(SMBHSTDAT0);
427 case ALI15X3_BYTE_DATA:
428 data->byte = inb_p(SMBHSTDAT0);
430 case ALI15X3_WORD_DATA:
431 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
433 case ALI15X3_BLOCK_DATA:
434 len = inb_p(SMBHSTDAT0);
437 data->block[0] = len;
438 /* Reset SMBBLKDAT */
439 outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);
440 for (i = 1; i <= data->block[0]; i++) {
441 data->block[i] = inb_p(SMBBLKDAT);
442 dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",
443 len, i, data->block[i]);
450 static u32 ali15x3_func(struct i2c_adapter *adapter)
452 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
453 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
454 I2C_FUNC_SMBUS_BLOCK_DATA;
457 static const struct i2c_algorithm smbus_algorithm = {
458 .smbus_xfer = ali15x3_access,
459 .functionality = ali15x3_func,
462 static struct i2c_adapter ali15x3_adapter = {
463 .owner = THIS_MODULE,
464 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
465 .algo = &smbus_algorithm,
468 static const struct pci_device_id ali15x3_ids[] = {
469 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
473 MODULE_DEVICE_TABLE (pci, ali15x3_ids);
475 static int ali15x3_probe(struct pci_dev *dev, const struct pci_device_id *id)
477 if (ali15x3_setup(dev)) {
479 "ALI15X3 not detected, module not inserted.\n");
483 /* set up the sysfs linkage to our parent device */
484 ali15x3_adapter.dev.parent = &dev->dev;
486 snprintf(ali15x3_adapter.name, sizeof(ali15x3_adapter.name),
487 "SMBus ALI15X3 adapter at %04x", ali15x3_smba);
488 return i2c_add_adapter(&ali15x3_adapter);
491 static void ali15x3_remove(struct pci_dev *dev)
493 i2c_del_adapter(&ali15x3_adapter);
494 release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
497 static struct pci_driver ali15x3_driver = {
498 .name = "ali15x3_smbus",
499 .id_table = ali15x3_ids,
500 .probe = ali15x3_probe,
501 .remove = ali15x3_remove,
504 module_pci_driver(ali15x3_driver);
506 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
507 MODULE_AUTHOR("Philip Edelbrock <phil@netroedge.com>");
508 MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
509 MODULE_DESCRIPTION("ALI15X3 SMBus driver");
510 MODULE_LICENSE("GPL");