103a4a9b3e7c222a37b2c423cf4a6d92c381e874
[linux-flexiantxendom0-3.2.10.git] / drivers / i2c / busses / i2c-ali15x3.c
1 /*
2     ali15x3.c - Part of lm_sensors, Linux kernel modules for hardware
3               monitoring
4     Copyright (c) 1999  Frodo Looijaard <frodol@dds.nl> and
5     Philip Edelbrock <phil@netroedge.com> and
6     Mark D. Studebaker <mdsxyz123@yahoo.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     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*
24     This is the driver for the SMB Host controller on
25     Acer Labs Inc. (ALI) M1541 and M1543C South Bridges.
26
27     The M1543C is a South bridge for desktop systems.
28     The M1533 is a South bridge for portable systems.
29     They are part of the following ALI chipsets:
30        "Aladdin Pro 2": Includes the M1621 Slot 1 North bridge
31        with AGP and 100MHz CPU Front Side bus
32        "Aladdin V": Includes the M1541 Socket 7 North bridge
33        with AGP and 100MHz CPU Front Side bus
34        "Aladdin IV": Includes the M1541 Socket 7 North bridge
35        with host bus up to 83.3 MHz.
36     For an overview of these chips see http://www.acerlabs.com
37
38     The M1533/M1543C devices appear as FOUR separate devices
39     on the PCI bus. An output of lspci will show something similar
40     to the following:
41
42         00:02.0 USB Controller: Acer Laboratories Inc. M5237
43         00:03.0 Bridge: Acer Laboratories Inc. M7101
44         00:07.0 ISA bridge: Acer Laboratories Inc. M1533
45         00:0f.0 IDE interface: Acer Laboratories Inc. M5229
46
47     The SMB controller is part of the 7101 device, which is an
48     ACPI-compliant Power Management Unit (PMU).
49
50     The whole 7101 device has to be enabled for the SMB to work.
51     You can't just enable the SMB alone.
52     The SMB and the ACPI have separate I/O spaces.
53     We make sure that the SMB is enabled. We leave the ACPI alone.
54
55     This driver controls the SMB Host only.
56     The SMB Slave controller on the M15X3 is not enabled.
57
58     This driver does not use interrupts.
59 */
60
61 /* Note: we assume there can only be one ALI15X3, with one SMBus interface */
62
63 /* #define DEBUG 1 */
64
65 #include <linux/module.h>
66 #include <linux/pci.h>
67 #include <linux/kernel.h>
68 #include <linux/stddef.h>
69 #include <linux/sched.h>
70 #include <linux/ioport.h>
71 #include <linux/i2c.h>
72 #include <linux/init.h>
73 #include <asm/io.h>
74
75 /* ALI15X3 SMBus address offsets */
76 #define SMBHSTSTS       (0 + ali15x3_smba)
77 #define SMBHSTCNT       (1 + ali15x3_smba)
78 #define SMBHSTSTART     (2 + ali15x3_smba)
79 #define SMBHSTCMD       (7 + ali15x3_smba)
80 #define SMBHSTADD       (3 + ali15x3_smba)
81 #define SMBHSTDAT0      (4 + ali15x3_smba)
82 #define SMBHSTDAT1      (5 + ali15x3_smba)
83 #define SMBBLKDAT       (6 + ali15x3_smba)
84
85 /* PCI Address Constants */
86 #define SMBCOM          0x004
87 #define SMBBA           0x014
88 #define SMBATPC         0x05B   /* used to unlock xxxBA registers */
89 #define SMBHSTCFG       0x0E0
90 #define SMBSLVC         0x0E1
91 #define SMBCLK          0x0E2
92 #define SMBREV          0x008
93
94 /* Other settings */
95 #define MAX_TIMEOUT             200     /* times 1/100 sec */
96 #define ALI15X3_SMB_IOSIZE      32
97
98 /* this is what the Award 1004 BIOS sets them to on a ASUS P5A MB.
99    We don't use these here. If the bases aren't set to some value we
100    tell user to upgrade BIOS and we fail.
101 */
102 #define ALI15X3_SMB_DEFAULTBASE 0xE800
103
104 /* ALI15X3 address lock bits */
105 #define ALI15X3_LOCK            0x06
106
107 /* ALI15X3 command constants */
108 #define ALI15X3_ABORT           0x02
109 #define ALI15X3_T_OUT           0x04
110 #define ALI15X3_QUICK           0x00
111 #define ALI15X3_BYTE            0x10
112 #define ALI15X3_BYTE_DATA       0x20
113 #define ALI15X3_WORD_DATA       0x30
114 #define ALI15X3_BLOCK_DATA      0x40
115 #define ALI15X3_BLOCK_CLR       0x80
116
117 /* ALI15X3 status register bits */
118 #define ALI15X3_STS_IDLE        0x04
119 #define ALI15X3_STS_BUSY        0x08
120 #define ALI15X3_STS_DONE        0x10
121 #define ALI15X3_STS_DEV         0x20    /* device error */
122 #define ALI15X3_STS_COLL        0x40    /* collision or no response */
123 #define ALI15X3_STS_TERM        0x80    /* terminated by abort */
124 #define ALI15X3_STS_ERR         0xE0    /* all the bad error bits */
125
126
127 /* If force_addr is set to anything different from 0, we forcibly enable
128    the device at the given address. */
129 static int force_addr = 0;
130 MODULE_PARM(force_addr, "i");
131 MODULE_PARM_DESC(force_addr,
132                  "Initialize the base address of the i2c controller");
133
134 static unsigned short ali15x3_smba = 0;
135
136 static int ali15x3_setup(struct pci_dev *ALI15X3_dev)
137 {
138         u16 a;
139         unsigned char temp;
140
141         /* Check the following things:
142                 - SMB I/O address is initialized
143                 - Device is enabled
144                 - We can use the addresses
145         */
146
147         /* Unlock the register.
148            The data sheet says that the address registers are read-only
149            if the lock bits are 1, but in fact the address registers
150            are zero unless you clear the lock bits.
151         */
152         pci_read_config_byte(ALI15X3_dev, SMBATPC, &temp);
153         if (temp & ALI15X3_LOCK) {
154                 temp &= ~ALI15X3_LOCK;
155                 pci_write_config_byte(ALI15X3_dev, SMBATPC, temp);
156         }
157
158         /* Determine the address of the SMBus area */
159         pci_read_config_word(ALI15X3_dev, SMBBA, &ali15x3_smba);
160         ali15x3_smba &= (0xffff & ~(ALI15X3_SMB_IOSIZE - 1));
161         if (ali15x3_smba == 0 && force_addr == 0) {
162                 dev_err(&ALI15X3_dev->dev, "ALI15X3_smb region uninitialized "
163                         "- upgrade BIOS or use force_addr=0xaddr\n");
164                 return -ENODEV;
165         }
166
167         if(force_addr)
168                 ali15x3_smba = force_addr & ~(ALI15X3_SMB_IOSIZE - 1);
169
170         if (!request_region(ali15x3_smba, ALI15X3_SMB_IOSIZE, "ali15x3-smb")) {
171                 dev_err(&ALI15X3_dev->dev,
172                         "ALI15X3_smb region 0x%x already in use!\n",
173                         ali15x3_smba);
174                 return -ENODEV;
175         }
176
177         if(force_addr) {
178                 dev_info(&ALI15X3_dev->dev, "forcing ISA address 0x%04X\n",
179                         ali15x3_smba);
180                 if (PCIBIOS_SUCCESSFUL != pci_write_config_word(ALI15X3_dev,
181                                                                 SMBBA,
182                                                                 ali15x3_smba))
183                         goto error;
184                 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(ALI15X3_dev,
185                                                                 SMBBA, &a))
186                         goto error;
187                 if ((a & ~(ALI15X3_SMB_IOSIZE - 1)) != ali15x3_smba) {
188                         /* make sure it works */
189                         dev_err(&ALI15X3_dev->dev,
190                                 "force address failed - not supported?\n");
191                         goto error;
192                 }
193         }
194         /* check if whole device is enabled */
195         pci_read_config_byte(ALI15X3_dev, SMBCOM, &temp);
196         if ((temp & 1) == 0) {
197                 dev_info(&ALI15X3_dev->dev, "enabling SMBus device\n");
198                 pci_write_config_byte(ALI15X3_dev, SMBCOM, temp | 0x01);
199         }
200
201         /* Is SMB Host controller enabled? */
202         pci_read_config_byte(ALI15X3_dev, SMBHSTCFG, &temp);
203         if ((temp & 1) == 0) {
204                 dev_info(&ALI15X3_dev->dev, "enabling SMBus controller\n");
205                 pci_write_config_byte(ALI15X3_dev, SMBHSTCFG, temp | 0x01);
206         }
207
208         /* set SMB clock to 74KHz as recommended in data sheet */
209         pci_write_config_byte(ALI15X3_dev, SMBCLK, 0x20);
210
211         /*
212           The interrupt routing for SMB is set up in register 0x77 in the
213           1533 ISA Bridge device, NOT in the 7101 device.
214           Don't bother with finding the 1533 device and reading the register.
215         if ((....... & 0x0F) == 1)
216                 dev_dbg(&ALI15X3_dev->dev, "ALI15X3 using Interrupt 9 for SMBus.\n");
217         */
218         pci_read_config_byte(ALI15X3_dev, SMBREV, &temp);
219         dev_dbg(&ALI15X3_dev->dev, "SMBREV = 0x%X\n", temp);
220         dev_dbg(&ALI15X3_dev->dev, "iALI15X3_smba = 0x%X\n", ali15x3_smba);
221
222         return 0;
223 error:
224         release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
225         return -ENODEV;
226 }
227
228 /* Internally used pause function */
229 static void ali15x3_do_pause(unsigned int amount)
230 {
231         current->state = TASK_INTERRUPTIBLE;
232         schedule_timeout(amount);
233 }
234
235 /* Another internally used function */
236 static int ali15x3_transaction(struct i2c_adapter *adap)
237 {
238         int temp;
239         int result = 0;
240         int timeout = 0;
241
242         dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, CNT=%02x, CMD=%02x, "
243                 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTSTS),
244                 inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),
245                 inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
246
247         /* get status */
248         temp = inb_p(SMBHSTSTS);
249
250         /* Make sure the SMBus host is ready to start transmitting */
251         /* Check the busy bit first */
252         if (temp & ALI15X3_STS_BUSY) {
253         /*
254            If the host controller is still busy, it may have timed out in the
255            previous transaction, resulting in a "SMBus Timeout" Dev.
256            I've tried the following to reset a stuck busy bit.
257                 1. Reset the controller with an ABORT command.
258                    (this doesn't seem to clear the controller if an external
259                    device is hung)
260                 2. Reset the controller and the other SMBus devices with a
261                    T_OUT command.  (this clears the host busy bit if an
262                    external device is hung, but it comes back upon a new access
263                    to a device)
264                 3. Disable and reenable the controller in SMBHSTCFG
265            Worst case, nothing seems to work except power reset.
266         */
267         /* Abort - reset the host controller */
268         /*
269            Try resetting entire SMB bus, including other devices -
270            This may not work either - it clears the BUSY bit but
271            then the BUSY bit may come back on when you try and use the chip again.
272            If that's the case you are stuck.
273         */
274                 dev_info(&adap->dev, "Resetting entire SMB Bus to "
275                         "clear busy condition (%02x)\n", temp);
276                 outb_p(ALI15X3_T_OUT, SMBHSTCNT);
277                 temp = inb_p(SMBHSTSTS);
278         }
279
280         /* now check the error bits and the busy bit */
281         if (temp & (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) {
282                 /* do a clear-on-write */
283                 outb_p(0xFF, SMBHSTSTS);
284                 if ((temp = inb_p(SMBHSTSTS)) &
285                     (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) {
286                         /* this is probably going to be correctable only by a power reset
287                            as one of the bits now appears to be stuck */
288                         /* This may be a bus or device with electrical problems. */
289                         dev_err(&adap->dev, "SMBus reset failed! (0x%02x) - "
290                                 "controller or device on bus is probably hung\n",
291                                 temp);
292                         return -1;
293                 }
294         } else {
295                 /* check and clear done bit */
296                 if (temp & ALI15X3_STS_DONE) {
297                         outb_p(temp, SMBHSTSTS);
298                 }
299         }
300
301         /* start the transaction by writing anything to the start register */
302         outb_p(0xFF, SMBHSTSTART);
303
304         /* We will always wait for a fraction of a second! */
305         timeout = 0;
306         do {
307                 ali15x3_do_pause(1);
308                 temp = inb_p(SMBHSTSTS);
309         } while ((!(temp & (ALI15X3_STS_ERR | ALI15X3_STS_DONE)))
310                  && (timeout++ < MAX_TIMEOUT));
311
312         /* If the SMBus is still busy, we give up */
313         if (timeout >= MAX_TIMEOUT) {
314                 result = -1;
315                 dev_err(&adap->dev, "SMBus Timeout!\n");
316         }
317
318         if (temp & ALI15X3_STS_TERM) {
319                 result = -1;
320                 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
321         }
322
323         /*
324           Unfortunately the ALI SMB controller maps "no response" and "bus
325           collision" into a single bit. No reponse is the usual case so don't
326           do a printk.
327           This means that bus collisions go unreported.
328         */
329         if (temp & ALI15X3_STS_COLL) {
330                 result = -1;
331                 dev_dbg(&adap->dev,
332                         "Error: no response or bus collision ADD=%02x\n",
333                         inb_p(SMBHSTADD));
334         }
335
336         /* haven't ever seen this */
337         if (temp & ALI15X3_STS_DEV) {
338                 result = -1;
339                 dev_err(&adap->dev, "Error: device error\n");
340         }
341         dev_dbg(&adap->dev, "Transaction (post): STS=%02x, CNT=%02x, CMD=%02x, "
342                 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTSTS),
343                 inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),
344                 inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
345         return result;
346 }
347
348 /* Return -1 on error. */
349 static s32 ali15x3_access(struct i2c_adapter * adap, u16 addr,
350                    unsigned short flags, char read_write, u8 command,
351                    int size, union i2c_smbus_data * data)
352 {
353         int i, len;
354         int temp;
355         int timeout;
356
357         /* clear all the bits (clear-on-write) */
358         outb_p(0xFF, SMBHSTSTS);
359         /* make sure SMBus is idle */
360         temp = inb_p(SMBHSTSTS);
361         for (timeout = 0;
362              (timeout < MAX_TIMEOUT) && !(temp & ALI15X3_STS_IDLE);
363              timeout++) {
364                 ali15x3_do_pause(1);
365                 temp = inb_p(SMBHSTSTS);
366         }
367         if (timeout >= MAX_TIMEOUT) {
368                 dev_err(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);
369         }
370
371         switch (size) {
372         case I2C_SMBUS_PROC_CALL:
373                 dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
374                 return -1;
375         case I2C_SMBUS_QUICK:
376                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
377                        SMBHSTADD);
378                 size = ALI15X3_QUICK;
379                 break;
380         case I2C_SMBUS_BYTE:
381                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
382                        SMBHSTADD);
383                 if (read_write == I2C_SMBUS_WRITE)
384                         outb_p(command, SMBHSTCMD);
385                 size = ALI15X3_BYTE;
386                 break;
387         case I2C_SMBUS_BYTE_DATA:
388                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
389                        SMBHSTADD);
390                 outb_p(command, SMBHSTCMD);
391                 if (read_write == I2C_SMBUS_WRITE)
392                         outb_p(data->byte, SMBHSTDAT0);
393                 size = ALI15X3_BYTE_DATA;
394                 break;
395         case I2C_SMBUS_WORD_DATA:
396                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
397                        SMBHSTADD);
398                 outb_p(command, SMBHSTCMD);
399                 if (read_write == I2C_SMBUS_WRITE) {
400                         outb_p(data->word & 0xff, SMBHSTDAT0);
401                         outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
402                 }
403                 size = ALI15X3_WORD_DATA;
404                 break;
405         case I2C_SMBUS_BLOCK_DATA:
406                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
407                        SMBHSTADD);
408                 outb_p(command, SMBHSTCMD);
409                 if (read_write == I2C_SMBUS_WRITE) {
410                         len = data->block[0];
411                         if (len < 0) {
412                                 len = 0;
413                                 data->block[0] = len;
414                         }
415                         if (len > 32) {
416                                 len = 32;
417                                 data->block[0] = len;
418                         }
419                         outb_p(len, SMBHSTDAT0);
420                         /* Reset SMBBLKDAT */
421                         outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);
422                         for (i = 1; i <= len; i++)
423                                 outb_p(data->block[i], SMBBLKDAT);
424                 }
425                 size = ALI15X3_BLOCK_DATA;
426                 break;
427         }
428
429         outb_p(size, SMBHSTCNT);        /* output command */
430
431         if (ali15x3_transaction(adap))  /* Error in transaction */
432                 return -1;
433
434         if ((read_write == I2C_SMBUS_WRITE) || (size == ALI15X3_QUICK))
435                 return 0;
436
437
438         switch (size) {
439         case ALI15X3_BYTE:      /* Result put in SMBHSTDAT0 */
440                 data->byte = inb_p(SMBHSTDAT0);
441                 break;
442         case ALI15X3_BYTE_DATA:
443                 data->byte = inb_p(SMBHSTDAT0);
444                 break;
445         case ALI15X3_WORD_DATA:
446                 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
447                 break;
448         case ALI15X3_BLOCK_DATA:
449                 len = inb_p(SMBHSTDAT0);
450                 if (len > 32)
451                         len = 32;
452                 data->block[0] = len;
453                 /* Reset SMBBLKDAT */
454                 outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);
455                 for (i = 1; i <= data->block[0]; i++) {
456                         data->block[i] = inb_p(SMBBLKDAT);
457                         dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",
458                                 len, i, data->block[i]);
459                 }
460                 break;
461         }
462         return 0;
463 }
464
465 static u32 ali15x3_func(struct i2c_adapter *adapter)
466 {
467         return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
468             I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
469             I2C_FUNC_SMBUS_BLOCK_DATA;
470 }
471
472 static struct i2c_algorithm smbus_algorithm = {
473         .name           = "Non-I2C SMBus adapter",
474         .id             = I2C_ALGO_SMBUS,
475         .smbus_xfer     = ali15x3_access,
476         .functionality  = ali15x3_func,
477 };
478
479 static struct i2c_adapter ali15x3_adapter = {
480         .owner          = THIS_MODULE,
481         .id             = I2C_ALGO_SMBUS | I2C_HW_SMBUS_ALI15X3,
482         .class          = I2C_ADAP_CLASS_SMBUS,
483         .algo           = &smbus_algorithm,
484         .dev            = {
485                 .name   = "unset",
486         },
487 };
488
489 static struct pci_device_id ali15x3_ids[] __devinitdata = {
490         {
491         .vendor =       PCI_VENDOR_ID_AL,
492         .device =       PCI_DEVICE_ID_AL_M7101,
493         .subvendor =    PCI_ANY_ID,
494         .subdevice =    PCI_ANY_ID,
495         },
496         { 0, }
497 };
498
499 static int __devinit ali15x3_probe(struct pci_dev *dev, const struct pci_device_id *id)
500 {
501         if (ali15x3_setup(dev)) {
502                 dev_err(&dev->dev,
503                         "ALI15X3 not detected, module not inserted.\n");
504                 return -ENODEV;
505         }
506
507         /* set up the driverfs linkage to our parent device */
508         ali15x3_adapter.dev.parent = &dev->dev;
509
510         snprintf(ali15x3_adapter.dev.name, DEVICE_NAME_SIZE,
511                 "SMBus ALI15X3 adapter at %04x", ali15x3_smba);
512         return i2c_add_adapter(&ali15x3_adapter);
513 }
514
515 static void __devexit ali15x3_remove(struct pci_dev *dev)
516 {
517         i2c_del_adapter(&ali15x3_adapter);
518 }
519
520 static struct pci_driver ali15x3_driver = {
521         .name           = "ali15x3 smbus",
522         .id_table       = ali15x3_ids,
523         .probe          = ali15x3_probe,
524         .remove         = __devexit_p(ali15x3_remove),
525 };
526
527 static int __init i2c_ali15x3_init(void)
528 {
529         printk("i2c-ali15x3.o version %s (%s)\n", I2C_VERSION, I2C_DATE);
530         return pci_module_init(&ali15x3_driver);
531 }
532
533 static void __exit i2c_ali15x3_exit(void)
534 {
535         pci_unregister_driver(&ali15x3_driver);
536         release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
537 }
538
539 MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl>, "
540                 "Philip Edelbrock <phil@netroedge.com>, "
541                 "and Mark D. Studebaker <mdsxyz123@yahoo.com>");
542 MODULE_DESCRIPTION("ALI15X3 SMBus driver");
543 MODULE_LICENSE("GPL");
544
545 module_init(i2c_ali15x3_init);
546 module_exit(i2c_ali15x3_exit);