]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/busses/i2c-ali15x3.c
a030abd3b32bb1df44b8c41da249f34ef638a404
[linux-2.6-omap-h63xx.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 #include <linux/module.h>
64 #include <linux/pci.h>
65 #include <linux/kernel.h>
66 #include <linux/stddef.h>
67 #include <linux/ioport.h>
68 #include <linux/delay.h>
69 #include <linux/i2c.h>
70 #include <linux/init.h>
71 #include <linux/acpi.h>
72 #include <asm/io.h>
73
74 /* ALI15X3 SMBus address offsets */
75 #define SMBHSTSTS       (0 + ali15x3_smba)
76 #define SMBHSTCNT       (1 + ali15x3_smba)
77 #define SMBHSTSTART     (2 + ali15x3_smba)
78 #define SMBHSTCMD       (7 + ali15x3_smba)
79 #define SMBHSTADD       (3 + ali15x3_smba)
80 #define SMBHSTDAT0      (4 + ali15x3_smba)
81 #define SMBHSTDAT1      (5 + ali15x3_smba)
82 #define SMBBLKDAT       (6 + ali15x3_smba)
83
84 /* PCI Address Constants */
85 #define SMBCOM          0x004
86 #define SMBBA           0x014
87 #define SMBATPC         0x05B   /* used to unlock xxxBA registers */
88 #define SMBHSTCFG       0x0E0
89 #define SMBSLVC         0x0E1
90 #define SMBCLK          0x0E2
91 #define SMBREV          0x008
92
93 /* Other settings */
94 #define MAX_TIMEOUT             200     /* times 1/100 sec */
95 #define ALI15X3_SMB_IOSIZE      32
96
97 /* this is what the Award 1004 BIOS sets them to on a ASUS P5A MB.
98    We don't use these here. If the bases aren't set to some value we
99    tell user to upgrade BIOS and we fail.
100 */
101 #define ALI15X3_SMB_DEFAULTBASE 0xE800
102
103 /* ALI15X3 address lock bits */
104 #define ALI15X3_LOCK            0x06
105
106 /* ALI15X3 command constants */
107 #define ALI15X3_ABORT           0x02
108 #define ALI15X3_T_OUT           0x04
109 #define ALI15X3_QUICK           0x00
110 #define ALI15X3_BYTE            0x10
111 #define ALI15X3_BYTE_DATA       0x20
112 #define ALI15X3_WORD_DATA       0x30
113 #define ALI15X3_BLOCK_DATA      0x40
114 #define ALI15X3_BLOCK_CLR       0x80
115
116 /* ALI15X3 status register bits */
117 #define ALI15X3_STS_IDLE        0x04
118 #define ALI15X3_STS_BUSY        0x08
119 #define ALI15X3_STS_DONE        0x10
120 #define ALI15X3_STS_DEV         0x20    /* device error */
121 #define ALI15X3_STS_COLL        0x40    /* collision or no response */
122 #define ALI15X3_STS_TERM        0x80    /* terminated by abort */
123 #define ALI15X3_STS_ERR         0xE0    /* all the bad error bits */
124
125
126 /* If force_addr is set to anything different from 0, we forcibly enable
127    the device at the given address. */
128 static u16 force_addr;
129 module_param(force_addr, ushort, 0);
130 MODULE_PARM_DESC(force_addr,
131                  "Initialize the base address of the i2c controller");
132
133 static struct pci_driver ali15x3_driver;
134 static unsigned short ali15x3_smba;
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 (acpi_check_region(ali15x3_smba, ALI15X3_SMB_IOSIZE,
171                               ali15x3_driver.name))
172                 return -EBUSY;
173
174         if (!request_region(ali15x3_smba, ALI15X3_SMB_IOSIZE,
175                             ali15x3_driver.name)) {
176                 dev_err(&ALI15X3_dev->dev,
177                         "ALI15X3_smb region 0x%x already in use!\n",
178                         ali15x3_smba);
179                 return -ENODEV;
180         }
181
182         if(force_addr) {
183                 dev_info(&ALI15X3_dev->dev, "forcing ISA address 0x%04X\n",
184                         ali15x3_smba);
185                 if (PCIBIOS_SUCCESSFUL != pci_write_config_word(ALI15X3_dev,
186                                                                 SMBBA,
187                                                                 ali15x3_smba))
188                         goto error;
189                 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(ALI15X3_dev,
190                                                                 SMBBA, &a))
191                         goto error;
192                 if ((a & ~(ALI15X3_SMB_IOSIZE - 1)) != ali15x3_smba) {
193                         /* make sure it works */
194                         dev_err(&ALI15X3_dev->dev,
195                                 "force address failed - not supported?\n");
196                         goto error;
197                 }
198         }
199         /* check if whole device is enabled */
200         pci_read_config_byte(ALI15X3_dev, SMBCOM, &temp);
201         if ((temp & 1) == 0) {
202                 dev_info(&ALI15X3_dev->dev, "enabling SMBus device\n");
203                 pci_write_config_byte(ALI15X3_dev, SMBCOM, temp | 0x01);
204         }
205
206         /* Is SMB Host controller enabled? */
207         pci_read_config_byte(ALI15X3_dev, SMBHSTCFG, &temp);
208         if ((temp & 1) == 0) {
209                 dev_info(&ALI15X3_dev->dev, "enabling SMBus controller\n");
210                 pci_write_config_byte(ALI15X3_dev, SMBHSTCFG, temp | 0x01);
211         }
212
213         /* set SMB clock to 74KHz as recommended in data sheet */
214         pci_write_config_byte(ALI15X3_dev, SMBCLK, 0x20);
215
216         /*
217           The interrupt routing for SMB is set up in register 0x77 in the
218           1533 ISA Bridge device, NOT in the 7101 device.
219           Don't bother with finding the 1533 device and reading the register.
220         if ((....... & 0x0F) == 1)
221                 dev_dbg(&ALI15X3_dev->dev, "ALI15X3 using Interrupt 9 for SMBus.\n");
222         */
223         pci_read_config_byte(ALI15X3_dev, SMBREV, &temp);
224         dev_dbg(&ALI15X3_dev->dev, "SMBREV = 0x%X\n", temp);
225         dev_dbg(&ALI15X3_dev->dev, "iALI15X3_smba = 0x%X\n", ali15x3_smba);
226
227         return 0;
228 error:
229         release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
230         return -ENODEV;
231 }
232
233 /* Another internally used function */
234 static int ali15x3_transaction(struct i2c_adapter *adap)
235 {
236         int temp;
237         int result = 0;
238         int timeout = 0;
239
240         dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, CNT=%02x, CMD=%02x, "
241                 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTSTS),
242                 inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),
243                 inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
244
245         /* get status */
246         temp = inb_p(SMBHSTSTS);
247
248         /* Make sure the SMBus host is ready to start transmitting */
249         /* Check the busy bit first */
250         if (temp & ALI15X3_STS_BUSY) {
251         /*
252            If the host controller is still busy, it may have timed out in the
253            previous transaction, resulting in a "SMBus Timeout" Dev.
254            I've tried the following to reset a stuck busy bit.
255                 1. Reset the controller with an ABORT command.
256                    (this doesn't seem to clear the controller if an external
257                    device is hung)
258                 2. Reset the controller and the other SMBus devices with a
259                    T_OUT command.  (this clears the host busy bit if an
260                    external device is hung, but it comes back upon a new access
261                    to a device)
262                 3. Disable and reenable the controller in SMBHSTCFG
263            Worst case, nothing seems to work except power reset.
264         */
265         /* Abort - reset the host controller */
266         /*
267            Try resetting entire SMB bus, including other devices -
268            This may not work either - it clears the BUSY bit but
269            then the BUSY bit may come back on when you try and use the chip again.
270            If that's the case you are stuck.
271         */
272                 dev_info(&adap->dev, "Resetting entire SMB Bus to "
273                         "clear busy condition (%02x)\n", temp);
274                 outb_p(ALI15X3_T_OUT, SMBHSTCNT);
275                 temp = inb_p(SMBHSTSTS);
276         }
277
278         /* now check the error bits and the busy bit */
279         if (temp & (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) {
280                 /* do a clear-on-write */
281                 outb_p(0xFF, SMBHSTSTS);
282                 if ((temp = inb_p(SMBHSTSTS)) &
283                     (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) {
284                         /* this is probably going to be correctable only by a power reset
285                            as one of the bits now appears to be stuck */
286                         /* This may be a bus or device with electrical problems. */
287                         dev_err(&adap->dev, "SMBus reset failed! (0x%02x) - "
288                                 "controller or device on bus is probably hung\n",
289                                 temp);
290                         return -EBUSY;
291                 }
292         } else {
293                 /* check and clear done bit */
294                 if (temp & ALI15X3_STS_DONE) {
295                         outb_p(temp, SMBHSTSTS);
296                 }
297         }
298
299         /* start the transaction by writing anything to the start register */
300         outb_p(0xFF, SMBHSTSTART);
301
302         /* We will always wait for a fraction of a second! */
303         timeout = 0;
304         do {
305                 msleep(1);
306                 temp = inb_p(SMBHSTSTS);
307         } while ((!(temp & (ALI15X3_STS_ERR | ALI15X3_STS_DONE)))
308                  && (timeout++ < MAX_TIMEOUT));
309
310         /* If the SMBus is still busy, we give up */
311         if (timeout >= MAX_TIMEOUT) {
312                 result = -ETIMEDOUT;
313                 dev_err(&adap->dev, "SMBus Timeout!\n");
314         }
315
316         if (temp & ALI15X3_STS_TERM) {
317                 result = -EIO;
318                 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
319         }
320
321         /*
322           Unfortunately the ALI SMB controller maps "no response" and "bus
323           collision" into a single bit. No reponse is the usual case so don't
324           do a printk.
325           This means that bus collisions go unreported.
326         */
327         if (temp & ALI15X3_STS_COLL) {
328                 result = -ENXIO;
329                 dev_dbg(&adap->dev,
330                         "Error: no response or bus collision ADD=%02x\n",
331                         inb_p(SMBHSTADD));
332         }
333
334         /* haven't ever seen this */
335         if (temp & ALI15X3_STS_DEV) {
336                 result = -EIO;
337                 dev_err(&adap->dev, "Error: device error\n");
338         }
339         dev_dbg(&adap->dev, "Transaction (post): STS=%02x, CNT=%02x, CMD=%02x, "
340                 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTSTS),
341                 inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),
342                 inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
343         return result;
344 }
345
346 /* Return negative errno on error. */
347 static s32 ali15x3_access(struct i2c_adapter * adap, u16 addr,
348                    unsigned short flags, char read_write, u8 command,
349                    int size, union i2c_smbus_data * data)
350 {
351         int i, len;
352         int temp;
353         int timeout;
354
355         /* clear all the bits (clear-on-write) */
356         outb_p(0xFF, SMBHSTSTS);
357         /* make sure SMBus is idle */
358         temp = inb_p(SMBHSTSTS);
359         for (timeout = 0;
360              (timeout < MAX_TIMEOUT) && !(temp & ALI15X3_STS_IDLE);
361              timeout++) {
362                 msleep(1);
363                 temp = inb_p(SMBHSTSTS);
364         }
365         if (timeout >= MAX_TIMEOUT) {
366                 dev_err(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);
367         }
368
369         switch (size) {
370         case I2C_SMBUS_QUICK:
371                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
372                        SMBHSTADD);
373                 size = ALI15X3_QUICK;
374                 break;
375         case I2C_SMBUS_BYTE:
376                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
377                        SMBHSTADD);
378                 if (read_write == I2C_SMBUS_WRITE)
379                         outb_p(command, SMBHSTCMD);
380                 size = ALI15X3_BYTE;
381                 break;
382         case I2C_SMBUS_BYTE_DATA:
383                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
384                        SMBHSTADD);
385                 outb_p(command, SMBHSTCMD);
386                 if (read_write == I2C_SMBUS_WRITE)
387                         outb_p(data->byte, SMBHSTDAT0);
388                 size = ALI15X3_BYTE_DATA;
389                 break;
390         case I2C_SMBUS_WORD_DATA:
391                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
392                        SMBHSTADD);
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                 size = ALI15X3_WORD_DATA;
399                 break;
400         case I2C_SMBUS_BLOCK_DATA:
401                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
402                        SMBHSTADD);
403                 outb_p(command, SMBHSTCMD);
404                 if (read_write == I2C_SMBUS_WRITE) {
405                         len = data->block[0];
406                         if (len < 0) {
407                                 len = 0;
408                                 data->block[0] = len;
409                         }
410                         if (len > 32) {
411                                 len = 32;
412                                 data->block[0] = len;
413                         }
414                         outb_p(len, SMBHSTDAT0);
415                         /* Reset SMBBLKDAT */
416                         outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);
417                         for (i = 1; i <= len; i++)
418                                 outb_p(data->block[i], SMBBLKDAT);
419                 }
420                 size = ALI15X3_BLOCK_DATA;
421                 break;
422         default:
423                 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
424                 return -EOPNOTSUPP;
425         }
426
427         outb_p(size, SMBHSTCNT);        /* output command */
428
429         temp = ali15x3_transaction(adap);
430         if (temp)
431                 return temp;
432
433         if ((read_write == I2C_SMBUS_WRITE) || (size == ALI15X3_QUICK))
434                 return 0;
435
436
437         switch (size) {
438         case ALI15X3_BYTE:      /* Result put in SMBHSTDAT0 */
439                 data->byte = inb_p(SMBHSTDAT0);
440                 break;
441         case ALI15X3_BYTE_DATA:
442                 data->byte = inb_p(SMBHSTDAT0);
443                 break;
444         case ALI15X3_WORD_DATA:
445                 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
446                 break;
447         case ALI15X3_BLOCK_DATA:
448                 len = inb_p(SMBHSTDAT0);
449                 if (len > 32)
450                         len = 32;
451                 data->block[0] = len;
452                 /* Reset SMBBLKDAT */
453                 outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);
454                 for (i = 1; i <= data->block[0]; i++) {
455                         data->block[i] = inb_p(SMBBLKDAT);
456                         dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",
457                                 len, i, data->block[i]);
458                 }
459                 break;
460         }
461         return 0;
462 }
463
464 static u32 ali15x3_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     = ali15x3_access,
473         .functionality  = ali15x3_func,
474 };
475
476 static struct i2c_adapter ali15x3_adapter = {
477         .owner          = THIS_MODULE,
478         .id             = I2C_HW_SMBUS_ALI15X3,
479         .class          = I2C_CLASS_HWMON | I2C_CLASS_SPD,
480         .algo           = &smbus_algorithm,
481 };
482
483 static struct pci_device_id ali15x3_ids[] = {
484         { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
485         { 0, }
486 };
487
488 MODULE_DEVICE_TABLE (pci, ali15x3_ids);
489
490 static int __devinit ali15x3_probe(struct pci_dev *dev, const struct pci_device_id *id)
491 {
492         if (ali15x3_setup(dev)) {
493                 dev_err(&dev->dev,
494                         "ALI15X3 not detected, module not inserted.\n");
495                 return -ENODEV;
496         }
497
498         /* set up the sysfs linkage to our parent device */
499         ali15x3_adapter.dev.parent = &dev->dev;
500
501         snprintf(ali15x3_adapter.name, sizeof(ali15x3_adapter.name),
502                 "SMBus ALI15X3 adapter at %04x", ali15x3_smba);
503         return i2c_add_adapter(&ali15x3_adapter);
504 }
505
506 static void __devexit ali15x3_remove(struct pci_dev *dev)
507 {
508         i2c_del_adapter(&ali15x3_adapter);
509         release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
510 }
511
512 static struct pci_driver ali15x3_driver = {
513         .name           = "ali15x3_smbus",
514         .id_table       = ali15x3_ids,
515         .probe          = ali15x3_probe,
516         .remove         = __devexit_p(ali15x3_remove),
517 };
518
519 static int __init i2c_ali15x3_init(void)
520 {
521         return pci_register_driver(&ali15x3_driver);
522 }
523
524 static void __exit i2c_ali15x3_exit(void)
525 {
526         pci_unregister_driver(&ali15x3_driver);
527 }
528
529 MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl>, "
530                 "Philip Edelbrock <phil@netroedge.com>, "
531                 "and Mark D. Studebaker <mdsxyz123@yahoo.com>");
532 MODULE_DESCRIPTION("ALI15X3 SMBus driver");
533 MODULE_LICENSE("GPL");
534
535 module_init(i2c_ali15x3_init);
536 module_exit(i2c_ali15x3_exit);