Path: blob/master/drivers/i2c/busses/i2c-ali15x3.c
15111 views
/*1Copyright (c) 1999 Frodo Looijaard <[email protected]> and2Philip Edelbrock <[email protected]> and3Mark D. Studebaker <[email protected]>45This program is free software; you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation; either version 2 of the License, or8(at your option) any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program; if not, write to the Free Software17Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.18*/1920/*21This is the driver for the SMB Host controller on22Acer Labs Inc. (ALI) M1541 and M1543C South Bridges.2324The M1543C is a South bridge for desktop systems.25The M1533 is a South bridge for portable systems.26They are part of the following ALI chipsets:27"Aladdin Pro 2": Includes the M1621 Slot 1 North bridge28with AGP and 100MHz CPU Front Side bus29"Aladdin V": Includes the M1541 Socket 7 North bridge30with AGP and 100MHz CPU Front Side bus31"Aladdin IV": Includes the M1541 Socket 7 North bridge32with host bus up to 83.3 MHz.33For an overview of these chips see http://www.acerlabs.com3435The M1533/M1543C devices appear as FOUR separate devices36on the PCI bus. An output of lspci will show something similar37to the following:383900:02.0 USB Controller: Acer Laboratories Inc. M52374000:03.0 Bridge: Acer Laboratories Inc. M71014100:07.0 ISA bridge: Acer Laboratories Inc. M15334200:0f.0 IDE interface: Acer Laboratories Inc. M52294344The SMB controller is part of the 7101 device, which is an45ACPI-compliant Power Management Unit (PMU).4647The whole 7101 device has to be enabled for the SMB to work.48You can't just enable the SMB alone.49The SMB and the ACPI have separate I/O spaces.50We make sure that the SMB is enabled. We leave the ACPI alone.5152This driver controls the SMB Host only.53The SMB Slave controller on the M15X3 is not enabled.5455This driver does not use interrupts.56*/5758/* Note: we assume there can only be one ALI15X3, with one SMBus interface */5960#include <linux/module.h>61#include <linux/pci.h>62#include <linux/kernel.h>63#include <linux/stddef.h>64#include <linux/ioport.h>65#include <linux/delay.h>66#include <linux/i2c.h>67#include <linux/init.h>68#include <linux/acpi.h>69#include <linux/io.h>7071/* ALI15X3 SMBus address offsets */72#define SMBHSTSTS (0 + ali15x3_smba)73#define SMBHSTCNT (1 + ali15x3_smba)74#define SMBHSTSTART (2 + ali15x3_smba)75#define SMBHSTCMD (7 + ali15x3_smba)76#define SMBHSTADD (3 + ali15x3_smba)77#define SMBHSTDAT0 (4 + ali15x3_smba)78#define SMBHSTDAT1 (5 + ali15x3_smba)79#define SMBBLKDAT (6 + ali15x3_smba)8081/* PCI Address Constants */82#define SMBCOM 0x00483#define SMBBA 0x01484#define SMBATPC 0x05B /* used to unlock xxxBA registers */85#define SMBHSTCFG 0x0E086#define SMBSLVC 0x0E187#define SMBCLK 0x0E288#define SMBREV 0x0088990/* Other settings */91#define MAX_TIMEOUT 200 /* times 1/100 sec */92#define ALI15X3_SMB_IOSIZE 329394/* this is what the Award 1004 BIOS sets them to on a ASUS P5A MB.95We don't use these here. If the bases aren't set to some value we96tell user to upgrade BIOS and we fail.97*/98#define ALI15X3_SMB_DEFAULTBASE 0xE80099100/* ALI15X3 address lock bits */101#define ALI15X3_LOCK 0x06102103/* ALI15X3 command constants */104#define ALI15X3_ABORT 0x02105#define ALI15X3_T_OUT 0x04106#define ALI15X3_QUICK 0x00107#define ALI15X3_BYTE 0x10108#define ALI15X3_BYTE_DATA 0x20109#define ALI15X3_WORD_DATA 0x30110#define ALI15X3_BLOCK_DATA 0x40111#define ALI15X3_BLOCK_CLR 0x80112113/* ALI15X3 status register bits */114#define ALI15X3_STS_IDLE 0x04115#define ALI15X3_STS_BUSY 0x08116#define ALI15X3_STS_DONE 0x10117#define ALI15X3_STS_DEV 0x20 /* device error */118#define ALI15X3_STS_COLL 0x40 /* collision or no response */119#define ALI15X3_STS_TERM 0x80 /* terminated by abort */120#define ALI15X3_STS_ERR 0xE0 /* all the bad error bits */121122123/* If force_addr is set to anything different from 0, we forcibly enable124the device at the given address. */125static u16 force_addr;126module_param(force_addr, ushort, 0);127MODULE_PARM_DESC(force_addr,128"Initialize the base address of the i2c controller");129130static struct pci_driver ali15x3_driver;131static unsigned short ali15x3_smba;132133static int __devinit ali15x3_setup(struct pci_dev *ALI15X3_dev)134{135u16 a;136unsigned char temp;137138/* Check the following things:139- SMB I/O address is initialized140- Device is enabled141- We can use the addresses142*/143144/* Unlock the register.145The data sheet says that the address registers are read-only146if the lock bits are 1, but in fact the address registers147are zero unless you clear the lock bits.148*/149pci_read_config_byte(ALI15X3_dev, SMBATPC, &temp);150if (temp & ALI15X3_LOCK) {151temp &= ~ALI15X3_LOCK;152pci_write_config_byte(ALI15X3_dev, SMBATPC, temp);153}154155/* Determine the address of the SMBus area */156pci_read_config_word(ALI15X3_dev, SMBBA, &ali15x3_smba);157ali15x3_smba &= (0xffff & ~(ALI15X3_SMB_IOSIZE - 1));158if (ali15x3_smba == 0 && force_addr == 0) {159dev_err(&ALI15X3_dev->dev, "ALI15X3_smb region uninitialized "160"- upgrade BIOS or use force_addr=0xaddr\n");161return -ENODEV;162}163164if(force_addr)165ali15x3_smba = force_addr & ~(ALI15X3_SMB_IOSIZE - 1);166167if (acpi_check_region(ali15x3_smba, ALI15X3_SMB_IOSIZE,168ali15x3_driver.name))169return -EBUSY;170171if (!request_region(ali15x3_smba, ALI15X3_SMB_IOSIZE,172ali15x3_driver.name)) {173dev_err(&ALI15X3_dev->dev,174"ALI15X3_smb region 0x%x already in use!\n",175ali15x3_smba);176return -ENODEV;177}178179if(force_addr) {180dev_info(&ALI15X3_dev->dev, "forcing ISA address 0x%04X\n",181ali15x3_smba);182if (PCIBIOS_SUCCESSFUL != pci_write_config_word(ALI15X3_dev,183SMBBA,184ali15x3_smba))185goto error;186if (PCIBIOS_SUCCESSFUL != pci_read_config_word(ALI15X3_dev,187SMBBA, &a))188goto error;189if ((a & ~(ALI15X3_SMB_IOSIZE - 1)) != ali15x3_smba) {190/* make sure it works */191dev_err(&ALI15X3_dev->dev,192"force address failed - not supported?\n");193goto error;194}195}196/* check if whole device is enabled */197pci_read_config_byte(ALI15X3_dev, SMBCOM, &temp);198if ((temp & 1) == 0) {199dev_info(&ALI15X3_dev->dev, "enabling SMBus device\n");200pci_write_config_byte(ALI15X3_dev, SMBCOM, temp | 0x01);201}202203/* Is SMB Host controller enabled? */204pci_read_config_byte(ALI15X3_dev, SMBHSTCFG, &temp);205if ((temp & 1) == 0) {206dev_info(&ALI15X3_dev->dev, "enabling SMBus controller\n");207pci_write_config_byte(ALI15X3_dev, SMBHSTCFG, temp | 0x01);208}209210/* set SMB clock to 74KHz as recommended in data sheet */211pci_write_config_byte(ALI15X3_dev, SMBCLK, 0x20);212213/*214The interrupt routing for SMB is set up in register 0x77 in the2151533 ISA Bridge device, NOT in the 7101 device.216Don't bother with finding the 1533 device and reading the register.217if ((....... & 0x0F) == 1)218dev_dbg(&ALI15X3_dev->dev, "ALI15X3 using Interrupt 9 for SMBus.\n");219*/220pci_read_config_byte(ALI15X3_dev, SMBREV, &temp);221dev_dbg(&ALI15X3_dev->dev, "SMBREV = 0x%X\n", temp);222dev_dbg(&ALI15X3_dev->dev, "iALI15X3_smba = 0x%X\n", ali15x3_smba);223224return 0;225error:226release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);227return -ENODEV;228}229230/* Another internally used function */231static int ali15x3_transaction(struct i2c_adapter *adap)232{233int temp;234int result = 0;235int timeout = 0;236237dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, CNT=%02x, CMD=%02x, "238"ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTSTS),239inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),240inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));241242/* get status */243temp = inb_p(SMBHSTSTS);244245/* Make sure the SMBus host is ready to start transmitting */246/* Check the busy bit first */247if (temp & ALI15X3_STS_BUSY) {248/*249If the host controller is still busy, it may have timed out in the250previous transaction, resulting in a "SMBus Timeout" Dev.251I've tried the following to reset a stuck busy bit.2521. Reset the controller with an ABORT command.253(this doesn't seem to clear the controller if an external254device is hung)2552. Reset the controller and the other SMBus devices with a256T_OUT command. (this clears the host busy bit if an257external device is hung, but it comes back upon a new access258to a device)2593. Disable and reenable the controller in SMBHSTCFG260Worst case, nothing seems to work except power reset.261*/262/* Abort - reset the host controller */263/*264Try resetting entire SMB bus, including other devices -265This may not work either - it clears the BUSY bit but266then the BUSY bit may come back on when you try and use the chip again.267If that's the case you are stuck.268*/269dev_info(&adap->dev, "Resetting entire SMB Bus to "270"clear busy condition (%02x)\n", temp);271outb_p(ALI15X3_T_OUT, SMBHSTCNT);272temp = inb_p(SMBHSTSTS);273}274275/* now check the error bits and the busy bit */276if (temp & (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) {277/* do a clear-on-write */278outb_p(0xFF, SMBHSTSTS);279if ((temp = inb_p(SMBHSTSTS)) &280(ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) {281/* this is probably going to be correctable only by a power reset282as one of the bits now appears to be stuck */283/* This may be a bus or device with electrical problems. */284dev_err(&adap->dev, "SMBus reset failed! (0x%02x) - "285"controller or device on bus is probably hung\n",286temp);287return -EBUSY;288}289} else {290/* check and clear done bit */291if (temp & ALI15X3_STS_DONE) {292outb_p(temp, SMBHSTSTS);293}294}295296/* start the transaction by writing anything to the start register */297outb_p(0xFF, SMBHSTSTART);298299/* We will always wait for a fraction of a second! */300timeout = 0;301do {302msleep(1);303temp = inb_p(SMBHSTSTS);304} while ((!(temp & (ALI15X3_STS_ERR | ALI15X3_STS_DONE)))305&& (timeout++ < MAX_TIMEOUT));306307/* If the SMBus is still busy, we give up */308if (timeout > MAX_TIMEOUT) {309result = -ETIMEDOUT;310dev_err(&adap->dev, "SMBus Timeout!\n");311}312313if (temp & ALI15X3_STS_TERM) {314result = -EIO;315dev_dbg(&adap->dev, "Error: Failed bus transaction\n");316}317318/*319Unfortunately the ALI SMB controller maps "no response" and "bus320collision" into a single bit. No response is the usual case so don't321do a printk.322This means that bus collisions go unreported.323*/324if (temp & ALI15X3_STS_COLL) {325result = -ENXIO;326dev_dbg(&adap->dev,327"Error: no response or bus collision ADD=%02x\n",328inb_p(SMBHSTADD));329}330331/* haven't ever seen this */332if (temp & ALI15X3_STS_DEV) {333result = -EIO;334dev_err(&adap->dev, "Error: device error\n");335}336dev_dbg(&adap->dev, "Transaction (post): STS=%02x, CNT=%02x, CMD=%02x, "337"ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTSTS),338inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),339inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));340return result;341}342343/* Return negative errno on error. */344static s32 ali15x3_access(struct i2c_adapter * adap, u16 addr,345unsigned short flags, char read_write, u8 command,346int size, union i2c_smbus_data * data)347{348int i, len;349int temp;350int timeout;351352/* clear all the bits (clear-on-write) */353outb_p(0xFF, SMBHSTSTS);354/* make sure SMBus is idle */355temp = inb_p(SMBHSTSTS);356for (timeout = 0;357(timeout < MAX_TIMEOUT) && !(temp & ALI15X3_STS_IDLE);358timeout++) {359msleep(1);360temp = inb_p(SMBHSTSTS);361}362if (timeout >= MAX_TIMEOUT) {363dev_err(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);364}365366switch (size) {367case I2C_SMBUS_QUICK:368outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),369SMBHSTADD);370size = ALI15X3_QUICK;371break;372case I2C_SMBUS_BYTE:373outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),374SMBHSTADD);375if (read_write == I2C_SMBUS_WRITE)376outb_p(command, SMBHSTCMD);377size = ALI15X3_BYTE;378break;379case I2C_SMBUS_BYTE_DATA:380outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),381SMBHSTADD);382outb_p(command, SMBHSTCMD);383if (read_write == I2C_SMBUS_WRITE)384outb_p(data->byte, SMBHSTDAT0);385size = ALI15X3_BYTE_DATA;386break;387case I2C_SMBUS_WORD_DATA:388outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),389SMBHSTADD);390outb_p(command, SMBHSTCMD);391if (read_write == I2C_SMBUS_WRITE) {392outb_p(data->word & 0xff, SMBHSTDAT0);393outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);394}395size = ALI15X3_WORD_DATA;396break;397case I2C_SMBUS_BLOCK_DATA:398outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),399SMBHSTADD);400outb_p(command, SMBHSTCMD);401if (read_write == I2C_SMBUS_WRITE) {402len = data->block[0];403if (len < 0) {404len = 0;405data->block[0] = len;406}407if (len > 32) {408len = 32;409data->block[0] = len;410}411outb_p(len, SMBHSTDAT0);412/* Reset SMBBLKDAT */413outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);414for (i = 1; i <= len; i++)415outb_p(data->block[i], SMBBLKDAT);416}417size = ALI15X3_BLOCK_DATA;418break;419default:420dev_warn(&adap->dev, "Unsupported transaction %d\n", size);421return -EOPNOTSUPP;422}423424outb_p(size, SMBHSTCNT); /* output command */425426temp = ali15x3_transaction(adap);427if (temp)428return temp;429430if ((read_write == I2C_SMBUS_WRITE) || (size == ALI15X3_QUICK))431return 0;432433434switch (size) {435case ALI15X3_BYTE: /* Result put in SMBHSTDAT0 */436data->byte = inb_p(SMBHSTDAT0);437break;438case ALI15X3_BYTE_DATA:439data->byte = inb_p(SMBHSTDAT0);440break;441case ALI15X3_WORD_DATA:442data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);443break;444case ALI15X3_BLOCK_DATA:445len = inb_p(SMBHSTDAT0);446if (len > 32)447len = 32;448data->block[0] = len;449/* Reset SMBBLKDAT */450outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);451for (i = 1; i <= data->block[0]; i++) {452data->block[i] = inb_p(SMBBLKDAT);453dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",454len, i, data->block[i]);455}456break;457}458return 0;459}460461static u32 ali15x3_func(struct i2c_adapter *adapter)462{463return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |464I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |465I2C_FUNC_SMBUS_BLOCK_DATA;466}467468static const struct i2c_algorithm smbus_algorithm = {469.smbus_xfer = ali15x3_access,470.functionality = ali15x3_func,471};472473static struct i2c_adapter ali15x3_adapter = {474.owner = THIS_MODULE,475.class = I2C_CLASS_HWMON | I2C_CLASS_SPD,476.algo = &smbus_algorithm,477};478479static const struct pci_device_id ali15x3_ids[] = {480{ PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },481{ 0, }482};483484MODULE_DEVICE_TABLE (pci, ali15x3_ids);485486static int __devinit ali15x3_probe(struct pci_dev *dev, const struct pci_device_id *id)487{488if (ali15x3_setup(dev)) {489dev_err(&dev->dev,490"ALI15X3 not detected, module not inserted.\n");491return -ENODEV;492}493494/* set up the sysfs linkage to our parent device */495ali15x3_adapter.dev.parent = &dev->dev;496497snprintf(ali15x3_adapter.name, sizeof(ali15x3_adapter.name),498"SMBus ALI15X3 adapter at %04x", ali15x3_smba);499return i2c_add_adapter(&ali15x3_adapter);500}501502static void __devexit ali15x3_remove(struct pci_dev *dev)503{504i2c_del_adapter(&ali15x3_adapter);505release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);506}507508static struct pci_driver ali15x3_driver = {509.name = "ali15x3_smbus",510.id_table = ali15x3_ids,511.probe = ali15x3_probe,512.remove = __devexit_p(ali15x3_remove),513};514515static int __init i2c_ali15x3_init(void)516{517return pci_register_driver(&ali15x3_driver);518}519520static void __exit i2c_ali15x3_exit(void)521{522pci_unregister_driver(&ali15x3_driver);523}524525MODULE_AUTHOR ("Frodo Looijaard <[email protected]>, "526"Philip Edelbrock <[email protected]>, "527"and Mark D. Studebaker <[email protected]>");528MODULE_DESCRIPTION("ALI15X3 SMBus driver");529MODULE_LICENSE("GPL");530531module_init(i2c_ali15x3_init);532module_exit(i2c_ali15x3_exit);533534535