Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  i2c-ali1563.c - i2c driver for the ALi 1563 Southbridge
0004  *
0005  *  Copyright (C) 2004 Patrick Mochel
0006  *            2005 Rudolf Marek <r.marek@assembler.cz>
0007  *
0008  *  The 1563 southbridge is deceptively similar to the 1533, with a
0009  *  few notable exceptions. One of those happens to be the fact they
0010  *  upgraded the i2c core to be 2.0 compliant, and happens to be almost
0011  *  identical to the i2c controller found in the Intel 801 south
0012  *  bridges.
0013  *
0014  *  This driver is based on a mix of the 15x3, 1535, and i801 drivers,
0015  *  with a little help from the ALi 1563 spec.
0016  */
0017 
0018 #include <linux/module.h>
0019 #include <linux/delay.h>
0020 #include <linux/i2c.h>
0021 #include <linux/pci.h>
0022 #include <linux/acpi.h>
0023 
0024 #define ALI1563_MAX_TIMEOUT 500
0025 #define ALI1563_SMBBA       0x80
0026 #define ALI1563_SMB_IOEN    1
0027 #define ALI1563_SMB_HOSTEN  2
0028 #define ALI1563_SMB_IOSIZE  16
0029 
0030 #define SMB_HST_STS (ali1563_smba + 0)
0031 #define SMB_HST_CNTL1   (ali1563_smba + 1)
0032 #define SMB_HST_CNTL2   (ali1563_smba + 2)
0033 #define SMB_HST_CMD (ali1563_smba + 3)
0034 #define SMB_HST_ADD (ali1563_smba + 4)
0035 #define SMB_HST_DAT0    (ali1563_smba + 5)
0036 #define SMB_HST_DAT1    (ali1563_smba + 6)
0037 #define SMB_BLK_DAT (ali1563_smba + 7)
0038 
0039 #define HST_STS_BUSY    0x01
0040 #define HST_STS_INTR    0x02
0041 #define HST_STS_DEVERR  0x04
0042 #define HST_STS_BUSERR  0x08
0043 #define HST_STS_FAIL    0x10
0044 #define HST_STS_DONE    0x80
0045 #define HST_STS_BAD 0x1c
0046 
0047 
0048 #define HST_CNTL1_TIMEOUT   0x80
0049 #define HST_CNTL1_LAST      0x40
0050 
0051 #define HST_CNTL2_KILL      0x04
0052 #define HST_CNTL2_START     0x40
0053 #define HST_CNTL2_QUICK     0x00
0054 #define HST_CNTL2_BYTE      0x01
0055 #define HST_CNTL2_BYTE_DATA 0x02
0056 #define HST_CNTL2_WORD_DATA 0x03
0057 #define HST_CNTL2_BLOCK     0x05
0058 
0059 
0060 #define HST_CNTL2_SIZEMASK  0x38
0061 
0062 static struct pci_driver ali1563_pci_driver;
0063 static unsigned short ali1563_smba;
0064 
0065 static int ali1563_transaction(struct i2c_adapter *a, int size)
0066 {
0067     u32 data;
0068     int timeout;
0069     int status = -EIO;
0070 
0071     dev_dbg(&a->dev, "Transaction (pre): STS=%02x, CNTL1=%02x, "
0072         "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
0073         inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
0074         inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
0075         inb_p(SMB_HST_DAT1));
0076 
0077     data = inb_p(SMB_HST_STS);
0078     if (data & HST_STS_BAD) {
0079         dev_err(&a->dev, "ali1563: Trying to reset busy device\n");
0080         outb_p(data | HST_STS_BAD, SMB_HST_STS);
0081         data = inb_p(SMB_HST_STS);
0082         if (data & HST_STS_BAD)
0083             return -EBUSY;
0084     }
0085     outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_START, SMB_HST_CNTL2);
0086 
0087     timeout = ALI1563_MAX_TIMEOUT;
0088     do {
0089         msleep(1);
0090     } while (((data = inb_p(SMB_HST_STS)) & HST_STS_BUSY) && --timeout);
0091 
0092     dev_dbg(&a->dev, "Transaction (post): STS=%02x, CNTL1=%02x, "
0093         "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
0094         inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
0095         inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
0096         inb_p(SMB_HST_DAT1));
0097 
0098     if (timeout && !(data & HST_STS_BAD))
0099         return 0;
0100 
0101     if (!timeout) {
0102         dev_err(&a->dev, "Timeout - Trying to KILL transaction!\n");
0103         /* Issue 'kill' to host controller */
0104         outb_p(HST_CNTL2_KILL, SMB_HST_CNTL2);
0105         data = inb_p(SMB_HST_STS);
0106         status = -ETIMEDOUT;
0107     }
0108 
0109     /* device error - no response, ignore the autodetection case */
0110     if (data & HST_STS_DEVERR) {
0111         if (size != HST_CNTL2_QUICK)
0112             dev_err(&a->dev, "Device error!\n");
0113         status = -ENXIO;
0114     }
0115     /* bus collision */
0116     if (data & HST_STS_BUSERR) {
0117         dev_err(&a->dev, "Bus collision!\n");
0118         /* Issue timeout, hoping it helps */
0119         outb_p(HST_CNTL1_TIMEOUT, SMB_HST_CNTL1);
0120     }
0121 
0122     if (data & HST_STS_FAIL) {
0123         dev_err(&a->dev, "Cleaning fail after KILL!\n");
0124         outb_p(0x0, SMB_HST_CNTL2);
0125     }
0126 
0127     return status;
0128 }
0129 
0130 static int ali1563_block_start(struct i2c_adapter *a)
0131 {
0132     u32 data;
0133     int timeout;
0134     int status = -EIO;
0135 
0136     dev_dbg(&a->dev, "Block (pre): STS=%02x, CNTL1=%02x, "
0137         "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
0138         inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
0139         inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
0140         inb_p(SMB_HST_DAT1));
0141 
0142     data = inb_p(SMB_HST_STS);
0143     if (data & HST_STS_BAD) {
0144         dev_warn(&a->dev, "ali1563: Trying to reset busy device\n");
0145         outb_p(data | HST_STS_BAD, SMB_HST_STS);
0146         data = inb_p(SMB_HST_STS);
0147         if (data & HST_STS_BAD)
0148             return -EBUSY;
0149     }
0150 
0151     /* Clear byte-ready bit */
0152     outb_p(data | HST_STS_DONE, SMB_HST_STS);
0153 
0154     /* Start transaction and wait for byte-ready bit to be set */
0155     outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_START, SMB_HST_CNTL2);
0156 
0157     timeout = ALI1563_MAX_TIMEOUT;
0158     do {
0159         msleep(1);
0160     } while (!((data = inb_p(SMB_HST_STS)) & HST_STS_DONE) && --timeout);
0161 
0162     dev_dbg(&a->dev, "Block (post): STS=%02x, CNTL1=%02x, "
0163         "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
0164         inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
0165         inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
0166         inb_p(SMB_HST_DAT1));
0167 
0168     if (timeout && !(data & HST_STS_BAD))
0169         return 0;
0170 
0171     if (timeout == 0)
0172         status = -ETIMEDOUT;
0173 
0174     if (data & HST_STS_DEVERR)
0175         status = -ENXIO;
0176 
0177     dev_err(&a->dev, "SMBus Error: %s%s%s%s%s\n",
0178         timeout ? "" : "Timeout ",
0179         data & HST_STS_FAIL ? "Transaction Failed " : "",
0180         data & HST_STS_BUSERR ? "No response or Bus Collision " : "",
0181         data & HST_STS_DEVERR ? "Device Error " : "",
0182         !(data & HST_STS_DONE) ? "Transaction Never Finished " : "");
0183     return status;
0184 }
0185 
0186 static int ali1563_block(struct i2c_adapter *a,
0187              union i2c_smbus_data *data, u8 rw)
0188 {
0189     int i, len;
0190     int error = 0;
0191 
0192     /* Do we need this? */
0193     outb_p(HST_CNTL1_LAST, SMB_HST_CNTL1);
0194 
0195     if (rw == I2C_SMBUS_WRITE) {
0196         len = data->block[0];
0197         if (len < 1)
0198             len = 1;
0199         else if (len > 32)
0200             len = 32;
0201         outb_p(len, SMB_HST_DAT0);
0202         outb_p(data->block[1], SMB_BLK_DAT);
0203     } else
0204         len = 32;
0205 
0206     outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_BLOCK, SMB_HST_CNTL2);
0207 
0208     for (i = 0; i < len; i++) {
0209         if (rw == I2C_SMBUS_WRITE) {
0210             outb_p(data->block[i + 1], SMB_BLK_DAT);
0211             error = ali1563_block_start(a);
0212             if (error)
0213                 break;
0214         } else {
0215             error = ali1563_block_start(a);
0216             if (error)
0217                 break;
0218             if (i == 0) {
0219                 len = inb_p(SMB_HST_DAT0);
0220                 if (len < 1)
0221                     len = 1;
0222                 else if (len > 32)
0223                     len = 32;
0224             }
0225             data->block[i+1] = inb_p(SMB_BLK_DAT);
0226         }
0227     }
0228     /* Do we need this? */
0229     outb_p(HST_CNTL1_LAST, SMB_HST_CNTL1);
0230     return error;
0231 }
0232 
0233 static s32 ali1563_access(struct i2c_adapter *a, u16 addr,
0234               unsigned short flags, char rw, u8 cmd,
0235               int size, union i2c_smbus_data *data)
0236 {
0237     int error = 0;
0238     int timeout;
0239     u32 reg;
0240 
0241     for (timeout = ALI1563_MAX_TIMEOUT; timeout; timeout--) {
0242         reg = inb_p(SMB_HST_STS);
0243         if (!(reg & HST_STS_BUSY))
0244             break;
0245     }
0246     if (!timeout)
0247         dev_warn(&a->dev, "SMBus not idle. HST_STS = %02x\n", reg);
0248     outb_p(0xff, SMB_HST_STS);
0249 
0250     /* Map the size to what the chip understands */
0251     switch (size) {
0252     case I2C_SMBUS_QUICK:
0253         size = HST_CNTL2_QUICK;
0254         break;
0255     case I2C_SMBUS_BYTE:
0256         size = HST_CNTL2_BYTE;
0257         break;
0258     case I2C_SMBUS_BYTE_DATA:
0259         size = HST_CNTL2_BYTE_DATA;
0260         break;
0261     case I2C_SMBUS_WORD_DATA:
0262         size = HST_CNTL2_WORD_DATA;
0263         break;
0264     case I2C_SMBUS_BLOCK_DATA:
0265         size = HST_CNTL2_BLOCK;
0266         break;
0267     default:
0268         dev_warn(&a->dev, "Unsupported transaction %d\n", size);
0269         error = -EOPNOTSUPP;
0270         goto Done;
0271     }
0272 
0273     outb_p(((addr & 0x7f) << 1) | (rw & 0x01), SMB_HST_ADD);
0274     outb_p((inb_p(SMB_HST_CNTL2) & ~HST_CNTL2_SIZEMASK) |
0275            (size << 3), SMB_HST_CNTL2);
0276 
0277     /* Write the command register */
0278 
0279     switch (size) {
0280     case HST_CNTL2_BYTE:
0281         if (rw == I2C_SMBUS_WRITE)
0282             /* Beware it uses DAT0 register and not CMD! */
0283             outb_p(cmd, SMB_HST_DAT0);
0284         break;
0285     case HST_CNTL2_BYTE_DATA:
0286         outb_p(cmd, SMB_HST_CMD);
0287         if (rw == I2C_SMBUS_WRITE)
0288             outb_p(data->byte, SMB_HST_DAT0);
0289         break;
0290     case HST_CNTL2_WORD_DATA:
0291         outb_p(cmd, SMB_HST_CMD);
0292         if (rw == I2C_SMBUS_WRITE) {
0293             outb_p(data->word & 0xff, SMB_HST_DAT0);
0294             outb_p((data->word & 0xff00) >> 8, SMB_HST_DAT1);
0295         }
0296         break;
0297     case HST_CNTL2_BLOCK:
0298         outb_p(cmd, SMB_HST_CMD);
0299         error = ali1563_block(a, data, rw);
0300         goto Done;
0301     }
0302 
0303     error = ali1563_transaction(a, size);
0304     if (error)
0305         goto Done;
0306 
0307     if ((rw == I2C_SMBUS_WRITE) || (size == HST_CNTL2_QUICK))
0308         goto Done;
0309 
0310     switch (size) {
0311     case HST_CNTL2_BYTE:    /* Result put in SMBHSTDAT0 */
0312         data->byte = inb_p(SMB_HST_DAT0);
0313         break;
0314     case HST_CNTL2_BYTE_DATA:
0315         data->byte = inb_p(SMB_HST_DAT0);
0316         break;
0317     case HST_CNTL2_WORD_DATA:
0318         data->word = inb_p(SMB_HST_DAT0) + (inb_p(SMB_HST_DAT1) << 8);
0319         break;
0320     }
0321 Done:
0322     return error;
0323 }
0324 
0325 static u32 ali1563_func(struct i2c_adapter *a)
0326 {
0327     return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
0328         I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
0329         I2C_FUNC_SMBUS_BLOCK_DATA;
0330 }
0331 
0332 
0333 static int ali1563_setup(struct pci_dev *dev)
0334 {
0335     u16 ctrl;
0336 
0337     pci_read_config_word(dev, ALI1563_SMBBA, &ctrl);
0338 
0339     /* SMB I/O Base in high 12 bits and must be aligned with the
0340      * size of the I/O space. */
0341     ali1563_smba = ctrl & ~(ALI1563_SMB_IOSIZE - 1);
0342     if (!ali1563_smba) {
0343         dev_warn(&dev->dev, "ali1563_smba Uninitialized\n");
0344         goto Err;
0345     }
0346 
0347     /* Check if device is enabled */
0348     if (!(ctrl & ALI1563_SMB_HOSTEN)) {
0349         dev_warn(&dev->dev, "Host Controller not enabled\n");
0350         goto Err;
0351     }
0352     if (!(ctrl & ALI1563_SMB_IOEN)) {
0353         dev_warn(&dev->dev, "I/O space not enabled, trying manually\n");
0354         pci_write_config_word(dev, ALI1563_SMBBA,
0355                       ctrl | ALI1563_SMB_IOEN);
0356         pci_read_config_word(dev, ALI1563_SMBBA, &ctrl);
0357         if (!(ctrl & ALI1563_SMB_IOEN)) {
0358             dev_err(&dev->dev,
0359                 "I/O space still not enabled, giving up\n");
0360             goto Err;
0361         }
0362     }
0363 
0364     if (acpi_check_region(ali1563_smba, ALI1563_SMB_IOSIZE,
0365                   ali1563_pci_driver.name))
0366         goto Err;
0367 
0368     if (!request_region(ali1563_smba, ALI1563_SMB_IOSIZE,
0369                 ali1563_pci_driver.name)) {
0370         dev_err(&dev->dev, "Could not allocate I/O space at 0x%04x\n",
0371             ali1563_smba);
0372         goto Err;
0373     }
0374     dev_info(&dev->dev, "Found ALi1563 SMBus at 0x%04x\n", ali1563_smba);
0375 
0376     return 0;
0377 Err:
0378     return -ENODEV;
0379 }
0380 
0381 static void ali1563_shutdown(struct pci_dev *dev)
0382 {
0383     release_region(ali1563_smba, ALI1563_SMB_IOSIZE);
0384 }
0385 
0386 static const struct i2c_algorithm ali1563_algorithm = {
0387     .smbus_xfer = ali1563_access,
0388     .functionality  = ali1563_func,
0389 };
0390 
0391 static struct i2c_adapter ali1563_adapter = {
0392     .owner  = THIS_MODULE,
0393     .class  = I2C_CLASS_HWMON | I2C_CLASS_SPD,
0394     .algo   = &ali1563_algorithm,
0395 };
0396 
0397 static int ali1563_probe(struct pci_dev *dev,
0398              const struct pci_device_id *id_table)
0399 {
0400     int error;
0401 
0402     error = ali1563_setup(dev);
0403     if (error)
0404         goto exit;
0405     ali1563_adapter.dev.parent = &dev->dev;
0406     snprintf(ali1563_adapter.name, sizeof(ali1563_adapter.name),
0407          "SMBus ALi 1563 Adapter @ %04x", ali1563_smba);
0408     error = i2c_add_adapter(&ali1563_adapter);
0409     if (error)
0410         goto exit_shutdown;
0411     return 0;
0412 
0413 exit_shutdown:
0414     ali1563_shutdown(dev);
0415 exit:
0416     dev_warn(&dev->dev, "ALi1563 SMBus probe failed (%d)\n", error);
0417     return error;
0418 }
0419 
0420 static void ali1563_remove(struct pci_dev *dev)
0421 {
0422     i2c_del_adapter(&ali1563_adapter);
0423     ali1563_shutdown(dev);
0424 }
0425 
0426 static const struct pci_device_id ali1563_id_table[] = {
0427     { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1563) },
0428     {},
0429 };
0430 
0431 MODULE_DEVICE_TABLE(pci, ali1563_id_table);
0432 
0433 static struct pci_driver ali1563_pci_driver = {
0434     .name       = "ali1563_smbus",
0435     .id_table   = ali1563_id_table,
0436     .probe      = ali1563_probe,
0437     .remove     = ali1563_remove,
0438 };
0439 
0440 module_pci_driver(ali1563_pci_driver);
0441 
0442 MODULE_LICENSE("GPL");