Version:  2.0.40 2.2.26 2.4.37 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18

Linux/drivers/i2c/busses/i2c-ali1535.c

  1 /*
  2  * Copyright (c) 2000  Frodo Looijaard <frodol@dds.nl>,
  3  *                      Philip Edelbrock <phil@netroedge.com>,
  4  *                      Mark D. Studebaker <mdsxyz123@yahoo.com>,
  5  *                      Dan Eaton <dan.eaton@rocketlogix.com> and
  6  *                      Stephen Rousset <stephen.rousset@rocketlogix.com>
  7  *
  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.
 12  *
 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.
 17 */
 18 
 19 /*
 20     This is the driver for the SMB Host controller on
 21     Acer Labs Inc. (ALI) M1535 South Bridge.
 22 
 23     The M1535 is a South bridge for portable systems.
 24     It is very similar to the M15x3 South bridges also produced
 25     by Acer Labs Inc.  Some of the registers within the part
 26     have moved and some have been redefined slightly. Additionally,
 27     the sequencing of the SMBus transactions has been modified
 28     to be more consistent with the sequencing recommended by
 29     the manufacturer and observed through testing.  These
 30     changes are reflected in this driver and can be identified
 31     by comparing this driver to the i2c-ali15x3 driver.
 32     For an overview of these chips see http://www.acerlabs.com
 33 
 34     The SMB controller is part of the 7101 device, which is an
 35     ACPI-compliant Power Management Unit (PMU).
 36 
 37     The whole 7101 device has to be enabled for the SMB to work.
 38     You can't just enable the SMB alone.
 39     The SMB and the ACPI have separate I/O spaces.
 40     We make sure that the SMB is enabled. We leave the ACPI alone.
 41 
 42     This driver controls the SMB Host only.
 43 
 44     This driver does not use interrupts.
 45 */
 46 
 47 
 48 /* Note: we assume there can only be one ALI1535, with one SMBus interface */
 49 
 50 #include <linux/module.h>
 51 #include <linux/pci.h>
 52 #include <linux/kernel.h>
 53 #include <linux/stddef.h>
 54 #include <linux/delay.h>
 55 #include <linux/ioport.h>
 56 #include <linux/i2c.h>
 57 #include <linux/acpi.h>
 58 #include <linux/io.h>
 59 
 60 
 61 /* ALI1535 SMBus address offsets */
 62 #define SMBHSTSTS       (0 + ali1535_smba)
 63 #define SMBHSTTYP       (1 + ali1535_smba)
 64 #define SMBHSTPORT      (2 + ali1535_smba)
 65 #define SMBHSTCMD       (7 + ali1535_smba)
 66 #define SMBHSTADD       (3 + ali1535_smba)
 67 #define SMBHSTDAT0      (4 + ali1535_smba)
 68 #define SMBHSTDAT1      (5 + ali1535_smba)
 69 #define SMBBLKDAT       (6 + ali1535_smba)
 70 
 71 /* PCI Address Constants */
 72 #define SMBCOM          0x004
 73 #define SMBREV          0x008
 74 #define SMBCFG          0x0D1
 75 #define SMBBA           0x0E2
 76 #define SMBHSTCFG       0x0F0
 77 #define SMBCLK          0x0F2
 78 
 79 /* Other settings */
 80 #define MAX_TIMEOUT             500     /* times 1/100 sec */
 81 #define ALI1535_SMB_IOSIZE      32
 82 
 83 #define ALI1535_SMB_DEFAULTBASE 0x8040
 84 
 85 /* ALI1535 address lock bits */
 86 #define ALI1535_LOCK            0x06    /* dwe */
 87 
 88 /* ALI1535 command constants */
 89 #define ALI1535_QUICK           0x00
 90 #define ALI1535_BYTE            0x10
 91 #define ALI1535_BYTE_DATA       0x20
 92 #define ALI1535_WORD_DATA       0x30
 93 #define ALI1535_BLOCK_DATA      0x40
 94 #define ALI1535_I2C_READ        0x60
 95 
 96 #define ALI1535_DEV10B_EN       0x80    /* Enable 10-bit addressing in  */
 97                                         /*  I2C read                    */
 98 #define ALI1535_T_OUT           0x08    /* Time-out Command (write)     */
 99 #define ALI1535_A_HIGH_BIT9     0x08    /* Bit 9 of 10-bit address in   */
100                                         /* Alert-Response-Address       */
101                                         /* (read)                       */
102 #define ALI1535_KILL            0x04    /* Kill Command (write)         */
103 #define ALI1535_A_HIGH_BIT8     0x04    /* Bit 8 of 10-bit address in   */
104                                         /*  Alert-Response-Address      */
105                                         /*  (read)                      */
106 
107 #define ALI1535_D_HI_MASK       0x03    /* Mask for isolating bits 9-8  */
108                                         /*  of 10-bit address in I2C    */
109                                         /*  Read Command                */
110 
111 /* ALI1535 status register bits */
112 #define ALI1535_STS_IDLE        0x04
113 #define ALI1535_STS_BUSY        0x08    /* host busy */
114 #define ALI1535_STS_DONE        0x10    /* transaction complete */
115 #define ALI1535_STS_DEV         0x20    /* device error */
116 #define ALI1535_STS_BUSERR      0x40    /* bus error    */
117 #define ALI1535_STS_FAIL        0x80    /* failed bus transaction */
118 #define ALI1535_STS_ERR         0xE0    /* all the bad error bits */
119 
120 #define ALI1535_BLOCK_CLR       0x04    /* reset block data index */
121 
122 /* ALI1535 device address register bits */
123 #define ALI1535_RD_ADDR         0x01    /* Read/Write Bit in Device     */
124                                         /*  Address field               */
125                                         /*  -> Write = 0                */
126                                         /*  -> Read  = 1                */
127 #define ALI1535_SMBIO_EN        0x04    /* SMB I/O Space enable         */
128 
129 static struct pci_driver ali1535_driver;
130 static unsigned long ali1535_smba;
131 static unsigned short ali1535_offset;
132 
133 /* Detect whether a ALI1535 can be found, and initialize it, where necessary.
134    Note the differences between kernels with the old PCI BIOS interface and
135    newer kernels with the real PCI interface. In compat.h some things are
136    defined to make the transition easier. */
137 static int ali1535_setup(struct pci_dev *dev)
138 {
139         int retval;
140         unsigned char temp;
141 
142         /* Check the following things:
143                 - SMB I/O address is initialized
144                 - Device is enabled
145                 - We can use the addresses
146         */
147 
148         retval = pci_enable_device(dev);
149         if (retval) {
150                 dev_err(&dev->dev, "ALI1535_smb can't enable device\n");
151                 goto exit;
152         }
153 
154         /* Determine the address of the SMBus area */
155         pci_read_config_word(dev, SMBBA, &ali1535_offset);
156         dev_dbg(&dev->dev, "ALI1535_smb is at offset 0x%04x\n", ali1535_offset);
157         ali1535_offset &= (0xffff & ~(ALI1535_SMB_IOSIZE - 1));
158         if (ali1535_offset == 0) {
159                 dev_warn(&dev->dev,
160                         "ALI1535_smb region uninitialized - upgrade BIOS?\n");
161                 retval = -ENODEV;
162                 goto exit;
163         }
164 
165         if (pci_resource_flags(dev, 0) & IORESOURCE_IO)
166                 ali1535_smba = pci_resource_start(dev, 0) + ali1535_offset;
167         else
168                 ali1535_smba = ali1535_offset;
169 
170         retval = acpi_check_region(ali1535_smba, ALI1535_SMB_IOSIZE,
171                                    ali1535_driver.name);
172         if (retval)
173                 goto exit;
174 
175         if (!request_region(ali1535_smba, ALI1535_SMB_IOSIZE,
176                             ali1535_driver.name)) {
177                 dev_err(&dev->dev, "ALI1535_smb region 0x%lx already in use!\n",
178                         ali1535_smba);
179                 retval = -EBUSY;
180                 goto exit;
181         }
182 
183         /* check if whole device is enabled */
184         pci_read_config_byte(dev, SMBCFG, &temp);
185         if ((temp & ALI1535_SMBIO_EN) == 0) {
186                 dev_err(&dev->dev, "SMB device not enabled - upgrade BIOS?\n");
187                 retval = -ENODEV;
188                 goto exit_free;
189         }
190 
191         /* Is SMB Host controller enabled? */
192         pci_read_config_byte(dev, SMBHSTCFG, &temp);
193         if ((temp & 1) == 0) {
194                 dev_err(&dev->dev, "SMBus controller not enabled - upgrade BIOS?\n");
195                 retval = -ENODEV;
196                 goto exit_free;
197         }
198 
199         /* set SMB clock to 74KHz as recommended in data sheet */
200         pci_write_config_byte(dev, SMBCLK, 0x20);
201 
202         /*
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(&dev->dev, "ALI1535 using Interrupt 9 for SMBus.\n");
208         */
209         pci_read_config_byte(dev, SMBREV, &temp);
210         dev_dbg(&dev->dev, "SMBREV = 0x%X\n", temp);
211         dev_dbg(&dev->dev, "ALI1535_smba = 0x%lx\n", ali1535_smba);
212 
213         return 0;
214 
215 exit_free:
216         release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
217 exit:
218         return retval;
219 }
220 
221 static int ali1535_transaction(struct i2c_adapter *adap)
222 {
223         int temp;
224         int result = 0;
225         int timeout = 0;
226 
227         dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, TYP=%02x, "
228                 "CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
229                 inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
230                 inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
231 
232         /* get status */
233         temp = inb_p(SMBHSTSTS);
234 
235         /* Make sure the SMBus host is ready to start transmitting */
236         /* Check the busy bit first */
237         if (temp & ALI1535_STS_BUSY) {
238                 /* If the host controller is still busy, it may have timed out
239                  * in the previous transaction, resulting in a "SMBus Timeout"
240                  * printk.  I've tried the following to reset a stuck busy bit.
241                  *   1. Reset the controller with an KILL command. (this
242                  *      doesn't seem to clear the controller if an external
243                  *      device is hung)
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
247                  *      access to a device)
248                  *   3. Disable and reenable the controller in SMBHSTCFG. Worst
249                  *      case, nothing seems to work except power reset.
250                  */
251 
252                 /* Try resetting entire SMB bus, including other devices - This
253                  * may not work either - it clears the BUSY bit but then the
254                  * BUSY bit may come back on when you try and use the chip
255                  * again.  If that's the case you are stuck.
256                  */
257                 dev_info(&adap->dev,
258                         "Resetting entire SMB Bus to clear busy condition (%02x)\n",
259                         temp);
260                 outb_p(ALI1535_T_OUT, SMBHSTTYP);
261                 temp = inb_p(SMBHSTSTS);
262         }
263 
264         /* now check the error bits and the busy bit */
265         if (temp & (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
266                 /* do a clear-on-write */
267                 outb_p(0xFF, SMBHSTSTS);
268                 temp = inb_p(SMBHSTSTS);
269                 if (temp & (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
270                         /* This is probably going to be correctable only by a
271                          * power reset as one of the bits now appears to be
272                          * stuck */
273                         /* This may be a bus or device with electrical problems. */
274                         dev_err(&adap->dev,
275                                 "SMBus reset failed! (0x%02x) - controller or "
276                                 "device on bus is probably hung\n", temp);
277                         return -EBUSY;
278                 }
279         } else {
280                 /* check and clear done bit */
281                 if (temp & ALI1535_STS_DONE)
282                         outb_p(temp, SMBHSTSTS);
283         }
284 
285         /* start the transaction by writing anything to the start register */
286         outb_p(0xFF, SMBHSTPORT);
287 
288         /* We will always wait for a fraction of a second! */
289         timeout = 0;
290         do {
291                 usleep_range(1000, 2000);
292                 temp = inb_p(SMBHSTSTS);
293         } while (((temp & ALI1535_STS_BUSY) && !(temp & ALI1535_STS_IDLE))
294                  && (timeout++ < MAX_TIMEOUT));
295 
296         /* If the SMBus is still busy, we give up */
297         if (timeout > MAX_TIMEOUT) {
298                 result = -ETIMEDOUT;
299                 dev_err(&adap->dev, "SMBus Timeout!\n");
300         }
301 
302         if (temp & ALI1535_STS_FAIL) {
303                 result = -EIO;
304                 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
305         }
306 
307         /* Unfortunately the ALI SMB controller maps "no response" and "bus
308          * collision" into a single bit. No response is the usual case so don't
309          * do a printk.  This means that bus collisions go unreported.
310          */
311         if (temp & ALI1535_STS_BUSERR) {
312                 result = -ENXIO;
313                 dev_dbg(&adap->dev,
314                         "Error: no response or bus collision ADD=%02x\n",
315                         inb_p(SMBHSTADD));
316         }
317 
318         /* haven't ever seen this */
319         if (temp & ALI1535_STS_DEV) {
320                 result = -EIO;
321                 dev_err(&adap->dev, "Error: device error\n");
322         }
323 
324         /* check to see if the "command complete" indication is set */
325         if (!(temp & ALI1535_STS_DONE)) {
326                 result = -ETIMEDOUT;
327                 dev_err(&adap->dev, "Error: command never completed\n");
328         }
329 
330         dev_dbg(&adap->dev, "Transaction (post): STS=%02x, TYP=%02x, "
331                 "CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
332                 inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
333                 inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
334 
335         /* take consequent actions for error conditions */
336         if (!(temp & ALI1535_STS_DONE)) {
337                 /* issue "kill" to reset host controller */
338                 outb_p(ALI1535_KILL, SMBHSTTYP);
339                 outb_p(0xFF, SMBHSTSTS);
340         } else if (temp & ALI1535_STS_ERR) {
341                 /* issue "timeout" to reset all devices on bus */
342                 outb_p(ALI1535_T_OUT, SMBHSTTYP);
343                 outb_p(0xFF, SMBHSTSTS);
344         }
345 
346         return result;
347 }
348 
349 /* Return negative errno on error. */
350 static s32 ali1535_access(struct i2c_adapter *adap, u16 addr,
351                           unsigned short flags, char read_write, u8 command,
352                           int size, union i2c_smbus_data *data)
353 {
354         int i, len;
355         int temp;
356         int timeout;
357         s32 result = 0;
358 
359         /* make sure SMBus is idle */
360         temp = inb_p(SMBHSTSTS);
361         for (timeout = 0;
362              (timeout < MAX_TIMEOUT) && !(temp & ALI1535_STS_IDLE);
363              timeout++) {
364                 usleep_range(1000, 2000);
365                 temp = inb_p(SMBHSTSTS);
366         }
367         if (timeout >= MAX_TIMEOUT)
368                 dev_warn(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);
369 
370         /* clear status register (clear-on-write) */
371         outb_p(0xFF, SMBHSTSTS);
372 
373         switch (size) {
374         case I2C_SMBUS_QUICK:
375                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
376                        SMBHSTADD);
377                 size = ALI1535_QUICK;
378                 outb_p(size, SMBHSTTYP);        /* output command */
379                 break;
380         case I2C_SMBUS_BYTE:
381                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
382                        SMBHSTADD);
383                 size = ALI1535_BYTE;
384                 outb_p(size, SMBHSTTYP);        /* output command */
385                 if (read_write == I2C_SMBUS_WRITE)
386                         outb_p(command, SMBHSTCMD);
387                 break;
388         case I2C_SMBUS_BYTE_DATA:
389                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
390                        SMBHSTADD);
391                 size = ALI1535_BYTE_DATA;
392                 outb_p(size, SMBHSTTYP);        /* output command */
393                 outb_p(command, SMBHSTCMD);
394                 if (read_write == I2C_SMBUS_WRITE)
395                         outb_p(data->byte, SMBHSTDAT0);
396                 break;
397         case I2C_SMBUS_WORD_DATA:
398                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
399                        SMBHSTADD);
400                 size = ALI1535_WORD_DATA;
401                 outb_p(size, SMBHSTTYP);        /* output command */
402                 outb_p(command, SMBHSTCMD);
403                 if (read_write == I2C_SMBUS_WRITE) {
404                         outb_p(data->word & 0xff, SMBHSTDAT0);
405                         outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
406                 }
407                 break;
408         case I2C_SMBUS_BLOCK_DATA:
409                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
410                        SMBHSTADD);
411                 size = ALI1535_BLOCK_DATA;
412                 outb_p(size, SMBHSTTYP);        /* output command */
413                 outb_p(command, SMBHSTCMD);
414                 if (read_write == I2C_SMBUS_WRITE) {
415                         len = data->block[0];
416                         if (len < 0) {
417                                 len = 0;
418                                 data->block[0] = len;
419                         }
420                         if (len > 32) {
421                                 len = 32;
422                                 data->block[0] = len;
423                         }
424                         outb_p(len, SMBHSTDAT0);
425                         /* Reset SMBBLKDAT */
426                         outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);
427                         for (i = 1; i <= len; i++)
428                                 outb_p(data->block[i], SMBBLKDAT);
429                 }
430                 break;
431         default:
432                 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
433                 result = -EOPNOTSUPP;
434                 goto EXIT;
435         }
436 
437         result = ali1535_transaction(adap);
438         if (result)
439                 goto EXIT;
440 
441         if ((read_write == I2C_SMBUS_WRITE) || (size == ALI1535_QUICK)) {
442                 result = 0;
443                 goto EXIT;
444         }
445 
446         switch (size) {
447         case ALI1535_BYTE:      /* Result put in SMBHSTDAT0 */
448                 data->byte = inb_p(SMBHSTDAT0);
449                 break;
450         case ALI1535_BYTE_DATA:
451                 data->byte = inb_p(SMBHSTDAT0);
452                 break;
453         case ALI1535_WORD_DATA:
454                 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
455                 break;
456         case ALI1535_BLOCK_DATA:
457                 len = inb_p(SMBHSTDAT0);
458                 if (len > 32)
459                         len = 32;
460                 data->block[0] = len;
461                 /* Reset SMBBLKDAT */
462                 outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);
463                 for (i = 1; i <= data->block[0]; i++) {
464                         data->block[i] = inb_p(SMBBLKDAT);
465                         dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",
466                                 len, i, data->block[i]);
467                 }
468                 break;
469         }
470 EXIT:
471         return result;
472 }
473 
474 
475 static u32 ali1535_func(struct i2c_adapter *adapter)
476 {
477         return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
478             I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
479             I2C_FUNC_SMBUS_BLOCK_DATA;
480 }
481 
482 static const struct i2c_algorithm smbus_algorithm = {
483         .smbus_xfer     = ali1535_access,
484         .functionality  = ali1535_func,
485 };
486 
487 static struct i2c_adapter ali1535_adapter = {
488         .owner          = THIS_MODULE,
489         .class          = I2C_CLASS_HWMON | I2C_CLASS_SPD,
490         .algo           = &smbus_algorithm,
491 };
492 
493 static const struct pci_device_id ali1535_ids[] = {
494         { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
495         { },
496 };
497 
498 MODULE_DEVICE_TABLE(pci, ali1535_ids);
499 
500 static int ali1535_probe(struct pci_dev *dev, const struct pci_device_id *id)
501 {
502         if (ali1535_setup(dev)) {
503                 dev_warn(&dev->dev,
504                         "ALI1535 not detected, module not inserted.\n");
505                 return -ENODEV;
506         }
507 
508         /* set up the sysfs linkage to our parent device */
509         ali1535_adapter.dev.parent = &dev->dev;
510 
511         snprintf(ali1535_adapter.name, sizeof(ali1535_adapter.name),
512                 "SMBus ALI1535 adapter at %04x", ali1535_offset);
513         return i2c_add_adapter(&ali1535_adapter);
514 }
515 
516 static void ali1535_remove(struct pci_dev *dev)
517 {
518         i2c_del_adapter(&ali1535_adapter);
519         release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
520 }
521 
522 static struct pci_driver ali1535_driver = {
523         .name           = "ali1535_smbus",
524         .id_table       = ali1535_ids,
525         .probe          = ali1535_probe,
526         .remove         = ali1535_remove,
527 };
528 
529 module_pci_driver(ali1535_driver);
530 
531 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
532               "Philip Edelbrock <phil@netroedge.com>, "
533               "Mark D. Studebaker <mdsxyz123@yahoo.com> "
534               "and Dan Eaton <dan.eaton@rocketlogix.com>");
535 MODULE_DESCRIPTION("ALI1535 SMBus driver");
536 MODULE_LICENSE("GPL");
537 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us