4964cd9401fcfd2fe2a8769f3343058faf13cf0d
[linux-flexiantxendom0-3.2.10.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 /* #define DEBUG 1 */
57
58 #include <linux/module.h>
59 #include <linux/pci.h>
60 #include <linux/kernel.h>
61 #include <linux/stddef.h>
62 #include <linux/sched.h>
63 #include <linux/ioport.h>
64 #include <linux/i2c.h>
65 #include <linux/init.h>
66 #include <asm/io.h>
67 #include <asm/semaphore.h>
68
69
70 /* ALI1535 SMBus address offsets */
71 #define SMBHSTSTS       (0 + ali1535_smba)
72 #define SMBHSTTYP       (1 + ali1535_smba)
73 #define SMBHSTPORT      (2 + ali1535_smba)
74 #define SMBHSTCMD       (7 + ali1535_smba)
75 #define SMBHSTADD       (3 + ali1535_smba)
76 #define SMBHSTDAT0      (4 + ali1535_smba)
77 #define SMBHSTDAT1      (5 + ali1535_smba)
78 #define SMBBLKDAT       (6 + ali1535_smba)
79
80 /* PCI Address Constants */
81 #define SMBCOM          0x004
82 #define SMBREV          0x008
83 #define SMBCFG          0x0D1
84 #define SMBBA           0x0E2
85 #define SMBHSTCFG       0x0F0
86 #define SMBCLK          0x0F2
87
88 /* Other settings */
89 #define MAX_TIMEOUT             500     /* times 1/100 sec */
90 #define ALI1535_SMB_IOSIZE      32
91
92 #define ALI1535_SMB_DEFAULTBASE 0x8040
93
94 /* ALI1535 address lock bits */
95 #define ALI1535_LOCK            0x06    /* dwe */
96
97 /* ALI1535 command constants */
98 #define ALI1535_QUICK           0x00
99 #define ALI1535_BYTE            0x10
100 #define ALI1535_BYTE_DATA       0x20
101 #define ALI1535_WORD_DATA       0x30
102 #define ALI1535_BLOCK_DATA      0x40
103 #define ALI1535_I2C_READ        0x60
104
105 #define ALI1535_DEV10B_EN       0x80    /* Enable 10-bit addressing in  */
106                                         /*  I2C read                    */
107 #define ALI1535_T_OUT           0x08    /* Time-out Command (write)     */
108 #define ALI1535_A_HIGH_BIT9     0x08    /* Bit 9 of 10-bit address in   */
109                                         /* Alert-Response-Address       */
110                                         /* (read)                       */
111 #define ALI1535_KILL            0x04    /* Kill Command (write)         */
112 #define ALI1535_A_HIGH_BIT8     0x04    /* Bit 8 of 10-bit address in   */
113                                         /*  Alert-Response-Address      */
114                                         /*  (read)                      */
115
116 #define ALI1535_D_HI_MASK       0x03    /* Mask for isolating bits 9-8  */
117                                         /*  of 10-bit address in I2C    */
118                                         /*  Read Command                */
119
120 /* ALI1535 status register bits */
121 #define ALI1535_STS_IDLE        0x04
122 #define ALI1535_STS_BUSY        0x08    /* host busy */
123 #define ALI1535_STS_DONE        0x10    /* transaction complete */
124 #define ALI1535_STS_DEV         0x20    /* device error */
125 #define ALI1535_STS_BUSERR      0x40    /* bus error    */
126 #define ALI1535_STS_FAIL        0x80    /* failed bus transaction */
127 #define ALI1535_STS_ERR         0xE0    /* all the bad error bits */
128
129 #define ALI1535_BLOCK_CLR       0x04    /* reset block data index */
130
131 /* ALI1535 device address register bits */
132 #define ALI1535_RD_ADDR         0x01    /* Read/Write Bit in Device     */
133                                         /*  Address field               */
134                                         /*  -> Write = 0                */
135                                         /*  -> Read  = 1                */
136 #define ALI1535_SMBIO_EN        0x04    /* SMB I/O Space enable         */
137
138
139 static unsigned short ali1535_smba;
140 DECLARE_MUTEX(i2c_ali1535_sem);
141
142 /* Detect whether a ALI1535 can be found, and initialize it, where necessary.
143    Note the differences between kernels with the old PCI BIOS interface and
144    newer kernels with the real PCI interface. In compat.h some things are
145    defined to make the transition easier. */
146 static int ali1535_setup(struct pci_dev *dev)
147 {
148         int retval = -ENODEV;
149         unsigned char temp;
150
151         /* Check the following things:
152                 - SMB I/O address is initialized
153                 - Device is enabled
154                 - We can use the addresses
155         */
156
157         /* Determine the address of the SMBus area */
158         pci_read_config_word(dev, SMBBA, &ali1535_smba);
159         ali1535_smba &= (0xffff & ~(ALI1535_SMB_IOSIZE - 1));
160         if (ali1535_smba == 0) {
161                 dev_warn(&dev->dev,
162                         "ALI1535_smb region uninitialized - upgrade BIOS?\n");
163                 goto exit;
164         }
165
166         if (!request_region(ali1535_smba, ALI1535_SMB_IOSIZE, "ali1535-smb")) {
167                 dev_err(&dev->dev, "ALI1535_smb region 0x%x already in use!\n",
168                         ali1535_smba);
169                 goto exit;
170         }
171
172         /* check if whole device is enabled */
173         pci_read_config_byte(dev, SMBCFG, &temp);
174         if ((temp & ALI1535_SMBIO_EN) == 0) {
175                 dev_err(&dev->dev, "SMB device not enabled - upgrade BIOS?\n");
176                 goto exit_free;
177         }
178
179         /* Is SMB Host controller enabled? */
180         pci_read_config_byte(dev, SMBHSTCFG, &temp);
181         if ((temp & 1) == 0) {
182                 dev_err(&dev->dev, "SMBus controller not enabled - upgrade BIOS?\n");
183                 goto exit_free;
184         }
185
186         /* set SMB clock to 74KHz as recommended in data sheet */
187         pci_write_config_byte(dev, SMBCLK, 0x20);
188
189         /*
190           The interrupt routing for SMB is set up in register 0x77 in the
191           1533 ISA Bridge device, NOT in the 7101 device.
192           Don't bother with finding the 1533 device and reading the register.
193         if ((....... & 0x0F) == 1)
194                 dev_dbg(&dev->dev, "ALI1535 using Interrupt 9 for SMBus.\n");
195         */
196         pci_read_config_byte(dev, SMBREV, &temp);
197         dev_dbg(&dev->dev, "SMBREV = 0x%X\n", temp);
198         dev_dbg(&dev->dev, "ALI1535_smba = 0x%X\n", ali1535_smba);
199
200         retval = 0;
201 exit:
202         return retval;
203
204 exit_free:
205         release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
206         return retval;
207 }
208
209 static void ali1535_do_pause(unsigned int amount)
210 {
211         current->state = TASK_INTERRUPTIBLE;
212         schedule_timeout(amount);
213 }
214
215 static int ali1535_transaction(struct i2c_adapter *adap)
216 {
217         int temp;
218         int result = 0;
219         int timeout = 0;
220
221         dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, TYP=%02x, "
222                 "CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
223                 inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
224                 inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
225
226         /* get status */
227         temp = inb_p(SMBHSTSTS);
228
229         /* Make sure the SMBus host is ready to start transmitting */
230         /* Check the busy bit first */
231         if (temp & ALI1535_STS_BUSY) {
232                 /* If the host controller is still busy, it may have timed out
233                  * in the previous transaction, resulting in a "SMBus Timeout"
234                  * printk.  I've tried the following to reset a stuck busy bit.
235                  *   1. Reset the controller with an KILL command. (this
236                  *      doesn't seem to clear the controller if an external
237                  *      device is hung)
238                  *   2. Reset the controller and the other SMBus devices with a
239                  *      T_OUT command. (this clears the host busy bit if an
240                  *      external device is hung, but it comes back upon a new
241                  *      access to a device)
242                  *   3. Disable and reenable the controller in SMBHSTCFG. Worst
243                  *      case, nothing seems to work except power reset.
244                  */
245
246                 /* Try resetting entire SMB bus, including other devices - This
247                  * may not work either - it clears the BUSY bit but then the
248                  * BUSY bit may come back on when you try and use the chip
249                  * again.  If that's the case you are stuck.
250                  */
251                 dev_info(&adap->dev,
252                         "Resetting entire SMB Bus to clear busy condition (%02x)\n",
253                         temp);
254                 outb_p(ALI1535_T_OUT, SMBHSTTYP);
255                 temp = inb_p(SMBHSTSTS);
256         }
257
258         /* now check the error bits and the busy bit */
259         if (temp & (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
260                 /* do a clear-on-write */
261                 outb_p(0xFF, SMBHSTSTS);
262                 if ((temp = inb_p(SMBHSTSTS)) &
263                     (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
264                         /* This is probably going to be correctable only by a
265                          * power reset as one of the bits now appears to be
266                          * stuck */
267                         /* This may be a bus or device with electrical problems. */
268                         dev_err(&adap->dev,
269                                 "SMBus reset failed! (0x%02x) - controller or "
270                                 "device on bus is probably hung\n", temp);
271                         return -1;
272                 }
273         } else {
274                 /* check and clear done bit */
275                 if (temp & ALI1535_STS_DONE) {
276                         outb_p(temp, SMBHSTSTS);
277                 }
278         }
279
280         /* start the transaction by writing anything to the start register */
281         outb_p(0xFF, SMBHSTPORT);
282
283         /* We will always wait for a fraction of a second! */
284         timeout = 0;
285         do {
286                 ali1535_do_pause(1);
287                 temp = inb_p(SMBHSTSTS);
288         } while (((temp & ALI1535_STS_BUSY) && !(temp & ALI1535_STS_IDLE))
289                  && (timeout++ < MAX_TIMEOUT));
290
291         /* If the SMBus is still busy, we give up */
292         if (timeout >= MAX_TIMEOUT) {
293                 result = -1;
294                 dev_err(&adap->dev, "SMBus Timeout!\n");
295         }
296
297         if (temp & ALI1535_STS_FAIL) {
298                 result = -1;
299                 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
300         }
301
302         /* Unfortunately the ALI SMB controller maps "no response" and "bus
303          * collision" into a single bit. No reponse is the usual case so don't
304          * do a printk.  This means that bus collisions go unreported.
305          */
306         if (temp & ALI1535_STS_BUSERR) {
307                 result = -1;
308                 dev_dbg(&adap->dev,
309                         "Error: no response or bus collision ADD=%02x\n",
310                         inb_p(SMBHSTADD));
311         }
312
313         /* haven't ever seen this */
314         if (temp & ALI1535_STS_DEV) {
315                 result = -1;
316                 dev_err(&adap->dev, "Error: device error\n");
317         }
318
319         /* check to see if the "command complete" indication is set */
320         if (!(temp & ALI1535_STS_DONE)) {
321                 result = -1;
322                 dev_err(&adap->dev, "Error: command never completed\n");
323         }
324
325         dev_dbg(&adap->dev, "Transaction (post): STS=%02x, TYP=%02x, "
326                 "CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
327                 inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
328                 inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
329
330         /* take consequent actions for error conditions */
331         if (!(temp & ALI1535_STS_DONE)) {
332                 /* issue "kill" to reset host controller */
333                 outb_p(ALI1535_KILL,SMBHSTTYP);
334                 outb_p(0xFF,SMBHSTSTS);
335         } else if (temp & ALI1535_STS_ERR) {
336                 /* issue "timeout" to reset all devices on bus */
337                 outb_p(ALI1535_T_OUT,SMBHSTTYP);
338                 outb_p(0xFF,SMBHSTSTS);
339         }
340
341         return result;
342 }
343
344 /* Return -1 on error. */
345 static s32 ali1535_access(struct i2c_adapter *adap, u16 addr,
346                           unsigned short flags, char read_write, u8 command,
347                           int size, union i2c_smbus_data *data)
348 {
349         int i, len;
350         int temp;
351         int timeout;
352         s32 result = 0;
353
354         down(&i2c_ali1535_sem);
355         /* make sure SMBus is idle */
356         temp = inb_p(SMBHSTSTS);
357         for (timeout = 0;
358              (timeout < MAX_TIMEOUT) && !(temp & ALI1535_STS_IDLE);
359              timeout++) {
360                 ali1535_do_pause(1);
361                 temp = inb_p(SMBHSTSTS);
362         }
363         if (timeout >= MAX_TIMEOUT)
364                 dev_warn(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);
365
366         /* clear status register (clear-on-write) */
367         outb_p(0xFF, SMBHSTSTS);
368
369         switch (size) {
370         case I2C_SMBUS_PROC_CALL:
371                 dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
372                 result = -1;
373                 goto EXIT;
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         }
432
433         if (ali1535_transaction(adap)) {
434                 /* Error in transaction */
435                 result = -1;
436                 goto EXIT;
437         }
438
439         if ((read_write == I2C_SMBUS_WRITE) || (size == ALI1535_QUICK)) {
440                 result = 0;
441                 goto EXIT;
442         }
443
444         switch (size) {
445         case ALI1535_BYTE:      /* Result put in SMBHSTDAT0 */
446                 data->byte = inb_p(SMBHSTDAT0);
447                 break;
448         case ALI1535_BYTE_DATA:
449                 data->byte = inb_p(SMBHSTDAT0);
450                 break;
451         case ALI1535_WORD_DATA:
452                 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
453                 break;
454         case ALI1535_BLOCK_DATA:
455                 len = inb_p(SMBHSTDAT0);
456                 if (len > 32)
457                         len = 32;
458                 data->block[0] = len;
459                 /* Reset SMBBLKDAT */
460                 outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);
461                 for (i = 1; i <= data->block[0]; i++) {
462                         data->block[i] = inb_p(SMBBLKDAT);
463                         dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",
464                                 len, i, data->block[i]);
465                 }
466                 break;
467         }
468 EXIT:
469         up(&i2c_ali1535_sem);
470         return result;
471 }
472
473
474 u32 ali1535_func(struct i2c_adapter *adapter)
475 {
476         return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
477             I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
478             I2C_FUNC_SMBUS_BLOCK_DATA;
479 }
480
481 static struct i2c_algorithm smbus_algorithm = {
482         .name           = "Non-i2c SMBus adapter",
483         .id             = I2C_ALGO_SMBUS,
484         .smbus_xfer     = ali1535_access,
485         .functionality  = ali1535_func,
486 };
487
488 static struct i2c_adapter ali1535_adapter = {
489         .owner          = THIS_MODULE,
490         .id             = I2C_ALGO_SMBUS | I2C_HW_SMBUS_ALI1535,
491         .algo           = &smbus_algorithm,
492         .dev            = {
493                 .name   = "unset",
494         }
495 };
496
497 static struct pci_device_id ali1535_ids[] __devinitdata = {
498         {
499                 .vendor =       PCI_VENDOR_ID_AL,
500                 .device =       PCI_DEVICE_ID_AL_M7101,
501                 .subvendor =    PCI_ANY_ID,
502                 .subdevice =    PCI_ANY_ID,
503         },
504         { },
505 };
506
507 static int __devinit ali1535_probe(struct pci_dev *dev, const struct pci_device_id *id)
508 {
509         if (ali1535_setup(dev)) {
510                 dev_warn(&dev->dev,
511                         "ALI1535 not detected, module not inserted.\n");
512                 return -ENODEV;
513         }
514
515         /* set up the driverfs linkage to our parent device */
516         ali1535_adapter.dev.parent = &dev->dev;
517
518         snprintf(ali1535_adapter.dev.name, DEVICE_NAME_SIZE, 
519                 "SMBus ALI1535 adapter at %04x", ali1535_smba);
520         return i2c_add_adapter(&ali1535_adapter);
521 }
522
523 static void __devexit ali1535_remove(struct pci_dev *dev)
524 {
525         i2c_del_adapter(&ali1535_adapter);
526 }
527
528 static struct pci_driver ali1535_driver = {
529         .name           = "ali1535_smbus",
530         .id_table       = ali1535_ids,
531         .probe          = ali1535_probe,
532         .remove         = __devexit_p(ali1535_remove),
533 };
534
535 static int __init i2c_ali1535_init(void)
536 {
537         printk(KERN_INFO "i2c-ali1535 version %s (%s)\n", I2C_VERSION, I2C_DATE);
538         return pci_module_init(&ali1535_driver);
539 }
540
541 static void __exit i2c_ali1535_exit(void)
542 {
543         pci_unregister_driver(&ali1535_driver);
544         release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
545 }
546
547 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
548               "Philip Edelbrock <phil@netroedge.com>, "
549               "Mark D. Studebaker <mdsxyz123@yahoo.com> "
550               "and Dan Eaton <dan.eaton@rocketlogix.com>");
551 MODULE_DESCRIPTION("ALI1535 SMBus driver");
552 MODULE_LICENSE("GPL");
553
554 module_init(i2c_ali1535_init);
555 module_exit(i2c_ali1535_exit);