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