Path: blob/master/drivers/i2c/busses/i2c-ali1535.c
15111 views
/*1Copyright (c) 2000 Frodo Looijaard <[email protected]>,2Philip Edelbrock <[email protected]>,3Mark D. Studebaker <[email protected]>,4Dan Eaton <[email protected]> and5Stephen Rousset<[email protected]>67This program is free software; you can redistribute it and/or modify8it under the terms of the GNU General Public License as published by9the Free Software Foundation; either version 2 of the License, or10(at your option) any later version.1112This program is distributed in the hope that it will be useful,13but WITHOUT ANY WARRANTY; without even the implied warranty of14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15GNU General Public License for more details.1617You should have received a copy of the GNU General Public License18along with this program; if not, write to the Free Software19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.20*/2122/*23This is the driver for the SMB Host controller on24Acer Labs Inc. (ALI) M1535 South Bridge.2526The M1535 is a South bridge for portable systems.27It is very similar to the M15x3 South bridges also produced28by Acer Labs Inc. Some of the registers within the part29have moved and some have been redefined slightly. Additionally,30the sequencing of the SMBus transactions has been modified31to be more consistent with the sequencing recommended by32the manufacturer and observed through testing. These33changes are reflected in this driver and can be identified34by comparing this driver to the i2c-ali15x3 driver.35For an overview of these chips see http://www.acerlabs.com3637The SMB controller is part of the 7101 device, which is an38ACPI-compliant Power Management Unit (PMU).3940The whole 7101 device has to be enabled for the SMB to work.41You can't just enable the SMB alone.42The SMB and the ACPI have separate I/O spaces.43We make sure that the SMB is enabled. We leave the ACPI alone.4445This driver controls the SMB Host only.4647This driver does not use interrupts.48*/495051/* Note: we assume there can only be one ALI1535, with one SMBus interface */5253#include <linux/module.h>54#include <linux/pci.h>55#include <linux/kernel.h>56#include <linux/stddef.h>57#include <linux/delay.h>58#include <linux/ioport.h>59#include <linux/i2c.h>60#include <linux/init.h>61#include <linux/acpi.h>62#include <linux/io.h>636465/* ALI1535 SMBus address offsets */66#define SMBHSTSTS (0 + ali1535_smba)67#define SMBHSTTYP (1 + ali1535_smba)68#define SMBHSTPORT (2 + ali1535_smba)69#define SMBHSTCMD (7 + ali1535_smba)70#define SMBHSTADD (3 + ali1535_smba)71#define SMBHSTDAT0 (4 + ali1535_smba)72#define SMBHSTDAT1 (5 + ali1535_smba)73#define SMBBLKDAT (6 + ali1535_smba)7475/* PCI Address Constants */76#define SMBCOM 0x00477#define SMBREV 0x00878#define SMBCFG 0x0D179#define SMBBA 0x0E280#define SMBHSTCFG 0x0F081#define SMBCLK 0x0F28283/* Other settings */84#define MAX_TIMEOUT 500 /* times 1/100 sec */85#define ALI1535_SMB_IOSIZE 328687#define ALI1535_SMB_DEFAULTBASE 0x80408889/* ALI1535 address lock bits */90#define ALI1535_LOCK 0x06 /* dwe */9192/* ALI1535 command constants */93#define ALI1535_QUICK 0x0094#define ALI1535_BYTE 0x1095#define ALI1535_BYTE_DATA 0x2096#define ALI1535_WORD_DATA 0x3097#define ALI1535_BLOCK_DATA 0x4098#define ALI1535_I2C_READ 0x6099100#define ALI1535_DEV10B_EN 0x80 /* Enable 10-bit addressing in */101/* I2C read */102#define ALI1535_T_OUT 0x08 /* Time-out Command (write) */103#define ALI1535_A_HIGH_BIT9 0x08 /* Bit 9 of 10-bit address in */104/* Alert-Response-Address */105/* (read) */106#define ALI1535_KILL 0x04 /* Kill Command (write) */107#define ALI1535_A_HIGH_BIT8 0x04 /* Bit 8 of 10-bit address in */108/* Alert-Response-Address */109/* (read) */110111#define ALI1535_D_HI_MASK 0x03 /* Mask for isolating bits 9-8 */112/* of 10-bit address in I2C */113/* Read Command */114115/* ALI1535 status register bits */116#define ALI1535_STS_IDLE 0x04117#define ALI1535_STS_BUSY 0x08 /* host busy */118#define ALI1535_STS_DONE 0x10 /* transaction complete */119#define ALI1535_STS_DEV 0x20 /* device error */120#define ALI1535_STS_BUSERR 0x40 /* bus error */121#define ALI1535_STS_FAIL 0x80 /* failed bus transaction */122#define ALI1535_STS_ERR 0xE0 /* all the bad error bits */123124#define ALI1535_BLOCK_CLR 0x04 /* reset block data index */125126/* ALI1535 device address register bits */127#define ALI1535_RD_ADDR 0x01 /* Read/Write Bit in Device */128/* Address field */129/* -> Write = 0 */130/* -> Read = 1 */131#define ALI1535_SMBIO_EN 0x04 /* SMB I/O Space enable */132133static struct pci_driver ali1535_driver;134static unsigned short ali1535_smba;135136/* Detect whether a ALI1535 can be found, and initialize it, where necessary.137Note the differences between kernels with the old PCI BIOS interface and138newer kernels with the real PCI interface. In compat.h some things are139defined to make the transition easier. */140static int __devinit ali1535_setup(struct pci_dev *dev)141{142int retval = -ENODEV;143unsigned char temp;144145/* Check the following things:146- SMB I/O address is initialized147- Device is enabled148- We can use the addresses149*/150151/* Determine the address of the SMBus area */152pci_read_config_word(dev, SMBBA, &ali1535_smba);153ali1535_smba &= (0xffff & ~(ALI1535_SMB_IOSIZE - 1));154if (ali1535_smba == 0) {155dev_warn(&dev->dev,156"ALI1535_smb region uninitialized - upgrade BIOS?\n");157goto exit;158}159160retval = acpi_check_region(ali1535_smba, ALI1535_SMB_IOSIZE,161ali1535_driver.name);162if (retval)163goto exit;164165if (!request_region(ali1535_smba, ALI1535_SMB_IOSIZE,166ali1535_driver.name)) {167dev_err(&dev->dev, "ALI1535_smb region 0x%x already in use!\n",168ali1535_smba);169goto exit;170}171172/* check if whole device is enabled */173pci_read_config_byte(dev, SMBCFG, &temp);174if ((temp & ALI1535_SMBIO_EN) == 0) {175dev_err(&dev->dev, "SMB device not enabled - upgrade BIOS?\n");176goto exit_free;177}178179/* Is SMB Host controller enabled? */180pci_read_config_byte(dev, SMBHSTCFG, &temp);181if ((temp & 1) == 0) {182dev_err(&dev->dev, "SMBus controller not enabled - upgrade BIOS?\n");183goto exit_free;184}185186/* set SMB clock to 74KHz as recommended in data sheet */187pci_write_config_byte(dev, SMBCLK, 0x20);188189/*190The interrupt routing for SMB is set up in register 0x77 in the1911533 ISA Bridge device, NOT in the 7101 device.192Don't bother with finding the 1533 device and reading the register.193if ((....... & 0x0F) == 1)194dev_dbg(&dev->dev, "ALI1535 using Interrupt 9 for SMBus.\n");195*/196pci_read_config_byte(dev, SMBREV, &temp);197dev_dbg(&dev->dev, "SMBREV = 0x%X\n", temp);198dev_dbg(&dev->dev, "ALI1535_smba = 0x%X\n", ali1535_smba);199200retval = 0;201exit:202return retval;203204exit_free:205release_region(ali1535_smba, ALI1535_SMB_IOSIZE);206return retval;207}208209static int ali1535_transaction(struct i2c_adapter *adap)210{211int temp;212int result = 0;213int timeout = 0;214215dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, TYP=%02x, "216"CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",217inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),218inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));219220/* get status */221temp = inb_p(SMBHSTSTS);222223/* Make sure the SMBus host is ready to start transmitting */224/* Check the busy bit first */225if (temp & ALI1535_STS_BUSY) {226/* If the host controller is still busy, it may have timed out227* in the previous transaction, resulting in a "SMBus Timeout"228* printk. I've tried the following to reset a stuck busy bit.229* 1. Reset the controller with an KILL command. (this230* doesn't seem to clear the controller if an external231* device is hung)232* 2. Reset the controller and the other SMBus devices with a233* T_OUT command. (this clears the host busy bit if an234* external device is hung, but it comes back upon a new235* access to a device)236* 3. Disable and reenable the controller in SMBHSTCFG. Worst237* case, nothing seems to work except power reset.238*/239240/* Try resetting entire SMB bus, including other devices - This241* may not work either - it clears the BUSY bit but then the242* BUSY bit may come back on when you try and use the chip243* again. If that's the case you are stuck.244*/245dev_info(&adap->dev,246"Resetting entire SMB Bus to clear busy condition (%02x)\n",247temp);248outb_p(ALI1535_T_OUT, SMBHSTTYP);249temp = inb_p(SMBHSTSTS);250}251252/* now check the error bits and the busy bit */253if (temp & (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {254/* do a clear-on-write */255outb_p(0xFF, SMBHSTSTS);256if ((temp = inb_p(SMBHSTSTS)) &257(ALI1535_STS_ERR | ALI1535_STS_BUSY)) {258/* This is probably going to be correctable only by a259* power reset as one of the bits now appears to be260* stuck */261/* This may be a bus or device with electrical problems. */262dev_err(&adap->dev,263"SMBus reset failed! (0x%02x) - controller or "264"device on bus is probably hung\n", temp);265return -EBUSY;266}267} else {268/* check and clear done bit */269if (temp & ALI1535_STS_DONE) {270outb_p(temp, SMBHSTSTS);271}272}273274/* start the transaction by writing anything to the start register */275outb_p(0xFF, SMBHSTPORT);276277/* We will always wait for a fraction of a second! */278timeout = 0;279do {280msleep(1);281temp = inb_p(SMBHSTSTS);282} while (((temp & ALI1535_STS_BUSY) && !(temp & ALI1535_STS_IDLE))283&& (timeout++ < MAX_TIMEOUT));284285/* If the SMBus is still busy, we give up */286if (timeout > MAX_TIMEOUT) {287result = -ETIMEDOUT;288dev_err(&adap->dev, "SMBus Timeout!\n");289}290291if (temp & ALI1535_STS_FAIL) {292result = -EIO;293dev_dbg(&adap->dev, "Error: Failed bus transaction\n");294}295296/* Unfortunately the ALI SMB controller maps "no response" and "bus297* collision" into a single bit. No response is the usual case so don't298* do a printk. This means that bus collisions go unreported.299*/300if (temp & ALI1535_STS_BUSERR) {301result = -ENXIO;302dev_dbg(&adap->dev,303"Error: no response or bus collision ADD=%02x\n",304inb_p(SMBHSTADD));305}306307/* haven't ever seen this */308if (temp & ALI1535_STS_DEV) {309result = -EIO;310dev_err(&adap->dev, "Error: device error\n");311}312313/* check to see if the "command complete" indication is set */314if (!(temp & ALI1535_STS_DONE)) {315result = -ETIMEDOUT;316dev_err(&adap->dev, "Error: command never completed\n");317}318319dev_dbg(&adap->dev, "Transaction (post): STS=%02x, TYP=%02x, "320"CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",321inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),322inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));323324/* take consequent actions for error conditions */325if (!(temp & ALI1535_STS_DONE)) {326/* issue "kill" to reset host controller */327outb_p(ALI1535_KILL,SMBHSTTYP);328outb_p(0xFF,SMBHSTSTS);329} else if (temp & ALI1535_STS_ERR) {330/* issue "timeout" to reset all devices on bus */331outb_p(ALI1535_T_OUT,SMBHSTTYP);332outb_p(0xFF,SMBHSTSTS);333}334335return result;336}337338/* Return negative errno on error. */339static s32 ali1535_access(struct i2c_adapter *adap, u16 addr,340unsigned short flags, char read_write, u8 command,341int size, union i2c_smbus_data *data)342{343int i, len;344int temp;345int timeout;346s32 result = 0;347348/* make sure SMBus is idle */349temp = inb_p(SMBHSTSTS);350for (timeout = 0;351(timeout < MAX_TIMEOUT) && !(temp & ALI1535_STS_IDLE);352timeout++) {353msleep(1);354temp = inb_p(SMBHSTSTS);355}356if (timeout >= MAX_TIMEOUT)357dev_warn(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);358359/* clear status register (clear-on-write) */360outb_p(0xFF, SMBHSTSTS);361362switch (size) {363case I2C_SMBUS_QUICK:364outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),365SMBHSTADD);366size = ALI1535_QUICK;367outb_p(size, SMBHSTTYP); /* output command */368break;369case I2C_SMBUS_BYTE:370outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),371SMBHSTADD);372size = ALI1535_BYTE;373outb_p(size, SMBHSTTYP); /* output command */374if (read_write == I2C_SMBUS_WRITE)375outb_p(command, SMBHSTCMD);376break;377case I2C_SMBUS_BYTE_DATA:378outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),379SMBHSTADD);380size = ALI1535_BYTE_DATA;381outb_p(size, SMBHSTTYP); /* output command */382outb_p(command, SMBHSTCMD);383if (read_write == I2C_SMBUS_WRITE)384outb_p(data->byte, SMBHSTDAT0);385break;386case I2C_SMBUS_WORD_DATA:387outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),388SMBHSTADD);389size = ALI1535_WORD_DATA;390outb_p(size, SMBHSTTYP); /* output command */391outb_p(command, SMBHSTCMD);392if (read_write == I2C_SMBUS_WRITE) {393outb_p(data->word & 0xff, SMBHSTDAT0);394outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);395}396break;397case I2C_SMBUS_BLOCK_DATA:398outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),399SMBHSTADD);400size = ALI1535_BLOCK_DATA;401outb_p(size, SMBHSTTYP); /* output command */402outb_p(command, SMBHSTCMD);403if (read_write == I2C_SMBUS_WRITE) {404len = data->block[0];405if (len < 0) {406len = 0;407data->block[0] = len;408}409if (len > 32) {410len = 32;411data->block[0] = len;412}413outb_p(len, SMBHSTDAT0);414/* Reset SMBBLKDAT */415outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);416for (i = 1; i <= len; i++)417outb_p(data->block[i], SMBBLKDAT);418}419break;420default:421dev_warn(&adap->dev, "Unsupported transaction %d\n", size);422result = -EOPNOTSUPP;423goto EXIT;424}425426result = ali1535_transaction(adap);427if (result)428goto EXIT;429430if ((read_write == I2C_SMBUS_WRITE) || (size == ALI1535_QUICK)) {431result = 0;432goto EXIT;433}434435switch (size) {436case ALI1535_BYTE: /* Result put in SMBHSTDAT0 */437data->byte = inb_p(SMBHSTDAT0);438break;439case ALI1535_BYTE_DATA:440data->byte = inb_p(SMBHSTDAT0);441break;442case ALI1535_WORD_DATA:443data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);444break;445case ALI1535_BLOCK_DATA:446len = inb_p(SMBHSTDAT0);447if (len > 32)448len = 32;449data->block[0] = len;450/* Reset SMBBLKDAT */451outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);452for (i = 1; i <= data->block[0]; i++) {453data->block[i] = inb_p(SMBBLKDAT);454dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",455len, i, data->block[i]);456}457break;458}459EXIT:460return result;461}462463464static u32 ali1535_func(struct i2c_adapter *adapter)465{466return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |467I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |468I2C_FUNC_SMBUS_BLOCK_DATA;469}470471static const struct i2c_algorithm smbus_algorithm = {472.smbus_xfer = ali1535_access,473.functionality = ali1535_func,474};475476static struct i2c_adapter ali1535_adapter = {477.owner = THIS_MODULE,478.class = I2C_CLASS_HWMON | I2C_CLASS_SPD,479.algo = &smbus_algorithm,480};481482static const struct pci_device_id ali1535_ids[] = {483{ PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },484{ },485};486487MODULE_DEVICE_TABLE (pci, ali1535_ids);488489static int __devinit ali1535_probe(struct pci_dev *dev, const struct pci_device_id *id)490{491if (ali1535_setup(dev)) {492dev_warn(&dev->dev,493"ALI1535 not detected, module not inserted.\n");494return -ENODEV;495}496497/* set up the sysfs linkage to our parent device */498ali1535_adapter.dev.parent = &dev->dev;499500snprintf(ali1535_adapter.name, sizeof(ali1535_adapter.name),501"SMBus ALI1535 adapter at %04x", ali1535_smba);502return i2c_add_adapter(&ali1535_adapter);503}504505static void __devexit ali1535_remove(struct pci_dev *dev)506{507i2c_del_adapter(&ali1535_adapter);508release_region(ali1535_smba, ALI1535_SMB_IOSIZE);509}510511static struct pci_driver ali1535_driver = {512.name = "ali1535_smbus",513.id_table = ali1535_ids,514.probe = ali1535_probe,515.remove = __devexit_p(ali1535_remove),516};517518static int __init i2c_ali1535_init(void)519{520return pci_register_driver(&ali1535_driver);521}522523static void __exit i2c_ali1535_exit(void)524{525pci_unregister_driver(&ali1535_driver);526}527528MODULE_AUTHOR("Frodo Looijaard <[email protected]>, "529"Philip Edelbrock <[email protected]>, "530"Mark D. Studebaker <[email protected]> "531"and Dan Eaton <[email protected]>");532MODULE_DESCRIPTION("ALI1535 SMBus driver");533MODULE_LICENSE("GPL");534535module_init(i2c_ali1535_init);536module_exit(i2c_ali1535_exit);537538539