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