blob: 704436cdec8e62769e2a69ed39a7302e7e2c3fab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/module.h>
57#include <linux/pci.h>
58#include <linux/kernel.h>
59#include <linux/stddef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/delay.h>
61#include <linux/ioport.h>
62#include <linux/i2c.h>
63#include <linux/init.h>
64#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66
67/* ALI1535 SMBus address offsets */
68#define SMBHSTSTS (0 + ali1535_smba)
69#define SMBHSTTYP (1 + ali1535_smba)
70#define SMBHSTPORT (2 + ali1535_smba)
71#define SMBHSTCMD (7 + ali1535_smba)
72#define SMBHSTADD (3 + ali1535_smba)
73#define SMBHSTDAT0 (4 + ali1535_smba)
74#define SMBHSTDAT1 (5 + ali1535_smba)
75#define SMBBLKDAT (6 + ali1535_smba)
76
77/* PCI Address Constants */
78#define SMBCOM 0x004
79#define SMBREV 0x008
80#define SMBCFG 0x0D1
81#define SMBBA 0x0E2
82#define SMBHSTCFG 0x0F0
83#define SMBCLK 0x0F2
84
85/* Other settings */
86#define MAX_TIMEOUT 500 /* times 1/100 sec */
87#define ALI1535_SMB_IOSIZE 32
88
89#define ALI1535_SMB_DEFAULTBASE 0x8040
90
91/* ALI1535 address lock bits */
92#define ALI1535_LOCK 0x06 /* dwe */
93
94/* ALI1535 command constants */
95#define ALI1535_QUICK 0x00
96#define ALI1535_BYTE 0x10
97#define ALI1535_BYTE_DATA 0x20
98#define ALI1535_WORD_DATA 0x30
99#define ALI1535_BLOCK_DATA 0x40
100#define ALI1535_I2C_READ 0x60
101
102#define ALI1535_DEV10B_EN 0x80 /* Enable 10-bit addressing in */
103 /* I2C read */
104#define ALI1535_T_OUT 0x08 /* Time-out Command (write) */
105#define ALI1535_A_HIGH_BIT9 0x08 /* Bit 9 of 10-bit address in */
106 /* Alert-Response-Address */
107 /* (read) */
108#define ALI1535_KILL 0x04 /* Kill Command (write) */
109#define ALI1535_A_HIGH_BIT8 0x04 /* Bit 8 of 10-bit address in */
110 /* Alert-Response-Address */
111 /* (read) */
112
113#define ALI1535_D_HI_MASK 0x03 /* Mask for isolating bits 9-8 */
114 /* of 10-bit address in I2C */
115 /* Read Command */
116
117/* ALI1535 status register bits */
118#define ALI1535_STS_IDLE 0x04
119#define ALI1535_STS_BUSY 0x08 /* host busy */
120#define ALI1535_STS_DONE 0x10 /* transaction complete */
121#define ALI1535_STS_DEV 0x20 /* device error */
122#define ALI1535_STS_BUSERR 0x40 /* bus error */
123#define ALI1535_STS_FAIL 0x80 /* failed bus transaction */
124#define ALI1535_STS_ERR 0xE0 /* all the bad error bits */
125
126#define ALI1535_BLOCK_CLR 0x04 /* reset block data index */
127
128/* ALI1535 device address register bits */
129#define ALI1535_RD_ADDR 0x01 /* Read/Write Bit in Device */
130 /* Address field */
131 /* -> Write = 0 */
132 /* -> Read = 1 */
133#define ALI1535_SMBIO_EN 0x04 /* SMB I/O Space enable */
134
Jean Delvared6072f82005-09-25 16:37:04 +0200135static struct pci_driver ali1535_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static unsigned short ali1535_smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/* Detect whether a ALI1535 can be found, and initialize it, where necessary.
139 Note the differences between kernels with the old PCI BIOS interface and
140 newer kernels with the real PCI interface. In compat.h some things are
141 defined to make the transition easier. */
142static int ali1535_setup(struct pci_dev *dev)
143{
144 int retval = -ENODEV;
145 unsigned char temp;
146
147 /* Check the following things:
148 - SMB I/O address is initialized
149 - Device is enabled
150 - We can use the addresses
151 */
152
153 /* Determine the address of the SMBus area */
154 pci_read_config_word(dev, SMBBA, &ali1535_smba);
155 ali1535_smba &= (0xffff & ~(ALI1535_SMB_IOSIZE - 1));
156 if (ali1535_smba == 0) {
157 dev_warn(&dev->dev,
158 "ALI1535_smb region uninitialized - upgrade BIOS?\n");
159 goto exit;
160 }
161
Jean Delvared6072f82005-09-25 16:37:04 +0200162 if (!request_region(ali1535_smba, ALI1535_SMB_IOSIZE,
163 ali1535_driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 dev_err(&dev->dev, "ALI1535_smb region 0x%x already in use!\n",
165 ali1535_smba);
166 goto exit;
167 }
168
169 /* check if whole device is enabled */
170 pci_read_config_byte(dev, SMBCFG, &temp);
171 if ((temp & ALI1535_SMBIO_EN) == 0) {
172 dev_err(&dev->dev, "SMB device not enabled - upgrade BIOS?\n");
173 goto exit_free;
174 }
175
176 /* Is SMB Host controller enabled? */
177 pci_read_config_byte(dev, SMBHSTCFG, &temp);
178 if ((temp & 1) == 0) {
179 dev_err(&dev->dev, "SMBus controller not enabled - upgrade BIOS?\n");
180 goto exit_free;
181 }
182
183 /* set SMB clock to 74KHz as recommended in data sheet */
184 pci_write_config_byte(dev, SMBCLK, 0x20);
185
186 /*
187 The interrupt routing for SMB is set up in register 0x77 in the
188 1533 ISA Bridge device, NOT in the 7101 device.
189 Don't bother with finding the 1533 device and reading the register.
190 if ((....... & 0x0F) == 1)
191 dev_dbg(&dev->dev, "ALI1535 using Interrupt 9 for SMBus.\n");
192 */
193 pci_read_config_byte(dev, SMBREV, &temp);
194 dev_dbg(&dev->dev, "SMBREV = 0x%X\n", temp);
195 dev_dbg(&dev->dev, "ALI1535_smba = 0x%X\n", ali1535_smba);
196
197 retval = 0;
198exit:
199 return retval;
200
201exit_free:
202 release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
203 return retval;
204}
205
206static int ali1535_transaction(struct i2c_adapter *adap)
207{
208 int temp;
209 int result = 0;
210 int timeout = 0;
211
212 dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, TYP=%02x, "
213 "CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
214 inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
215 inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
216
217 /* get status */
218 temp = inb_p(SMBHSTSTS);
219
220 /* Make sure the SMBus host is ready to start transmitting */
221 /* Check the busy bit first */
222 if (temp & ALI1535_STS_BUSY) {
223 /* If the host controller is still busy, it may have timed out
224 * in the previous transaction, resulting in a "SMBus Timeout"
225 * printk. I've tried the following to reset a stuck busy bit.
226 * 1. Reset the controller with an KILL command. (this
227 * doesn't seem to clear the controller if an external
228 * device is hung)
229 * 2. Reset the controller and the other SMBus devices with a
230 * T_OUT command. (this clears the host busy bit if an
231 * external device is hung, but it comes back upon a new
232 * access to a device)
233 * 3. Disable and reenable the controller in SMBHSTCFG. Worst
234 * case, nothing seems to work except power reset.
235 */
236
237 /* Try resetting entire SMB bus, including other devices - This
238 * may not work either - it clears the BUSY bit but then the
239 * BUSY bit may come back on when you try and use the chip
240 * again. If that's the case you are stuck.
241 */
242 dev_info(&adap->dev,
243 "Resetting entire SMB Bus to clear busy condition (%02x)\n",
244 temp);
245 outb_p(ALI1535_T_OUT, SMBHSTTYP);
246 temp = inb_p(SMBHSTSTS);
247 }
248
249 /* now check the error bits and the busy bit */
250 if (temp & (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
251 /* do a clear-on-write */
252 outb_p(0xFF, SMBHSTSTS);
253 if ((temp = inb_p(SMBHSTSTS)) &
254 (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
255 /* This is probably going to be correctable only by a
256 * power reset as one of the bits now appears to be
257 * stuck */
258 /* This may be a bus or device with electrical problems. */
259 dev_err(&adap->dev,
260 "SMBus reset failed! (0x%02x) - controller or "
261 "device on bus is probably hung\n", temp);
David Brownell97140342008-07-14 22:38:25 +0200262 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 } else {
265 /* check and clear done bit */
266 if (temp & ALI1535_STS_DONE) {
267 outb_p(temp, SMBHSTSTS);
268 }
269 }
270
271 /* start the transaction by writing anything to the start register */
272 outb_p(0xFF, SMBHSTPORT);
273
274 /* We will always wait for a fraction of a second! */
275 timeout = 0;
276 do {
277 msleep(1);
278 temp = inb_p(SMBHSTSTS);
279 } while (((temp & ALI1535_STS_BUSY) && !(temp & ALI1535_STS_IDLE))
280 && (timeout++ < MAX_TIMEOUT));
281
282 /* If the SMBus is still busy, we give up */
283 if (timeout >= MAX_TIMEOUT) {
David Brownell97140342008-07-14 22:38:25 +0200284 result = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 dev_err(&adap->dev, "SMBus Timeout!\n");
286 }
287
288 if (temp & ALI1535_STS_FAIL) {
David Brownell97140342008-07-14 22:38:25 +0200289 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
291 }
292
293 /* Unfortunately the ALI SMB controller maps "no response" and "bus
294 * collision" into a single bit. No reponse is the usual case so don't
295 * do a printk. This means that bus collisions go unreported.
296 */
297 if (temp & ALI1535_STS_BUSERR) {
David Brownell97140342008-07-14 22:38:25 +0200298 result = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 dev_dbg(&adap->dev,
300 "Error: no response or bus collision ADD=%02x\n",
301 inb_p(SMBHSTADD));
302 }
303
304 /* haven't ever seen this */
305 if (temp & ALI1535_STS_DEV) {
David Brownell97140342008-07-14 22:38:25 +0200306 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 dev_err(&adap->dev, "Error: device error\n");
308 }
309
310 /* check to see if the "command complete" indication is set */
311 if (!(temp & ALI1535_STS_DONE)) {
David Brownell97140342008-07-14 22:38:25 +0200312 result = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 dev_err(&adap->dev, "Error: command never completed\n");
314 }
315
316 dev_dbg(&adap->dev, "Transaction (post): STS=%02x, TYP=%02x, "
317 "CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
318 inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
319 inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
320
321 /* take consequent actions for error conditions */
322 if (!(temp & ALI1535_STS_DONE)) {
323 /* issue "kill" to reset host controller */
324 outb_p(ALI1535_KILL,SMBHSTTYP);
325 outb_p(0xFF,SMBHSTSTS);
326 } else if (temp & ALI1535_STS_ERR) {
327 /* issue "timeout" to reset all devices on bus */
328 outb_p(ALI1535_T_OUT,SMBHSTTYP);
329 outb_p(0xFF,SMBHSTSTS);
330 }
331
332 return result;
333}
334
David Brownell97140342008-07-14 22:38:25 +0200335/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static s32 ali1535_access(struct i2c_adapter *adap, u16 addr,
337 unsigned short flags, char read_write, u8 command,
338 int size, union i2c_smbus_data *data)
339{
340 int i, len;
341 int temp;
342 int timeout;
343 s32 result = 0;
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /* make sure SMBus is idle */
346 temp = inb_p(SMBHSTSTS);
347 for (timeout = 0;
348 (timeout < MAX_TIMEOUT) && !(temp & ALI1535_STS_IDLE);
349 timeout++) {
350 msleep(1);
351 temp = inb_p(SMBHSTSTS);
352 }
353 if (timeout >= MAX_TIMEOUT)
354 dev_warn(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);
355
356 /* clear status register (clear-on-write) */
357 outb_p(0xFF, SMBHSTSTS);
358
359 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 case I2C_SMBUS_QUICK:
361 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
362 SMBHSTADD);
363 size = ALI1535_QUICK;
364 outb_p(size, SMBHSTTYP); /* output command */
365 break;
366 case I2C_SMBUS_BYTE:
367 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
368 SMBHSTADD);
369 size = ALI1535_BYTE;
370 outb_p(size, SMBHSTTYP); /* output command */
371 if (read_write == I2C_SMBUS_WRITE)
372 outb_p(command, SMBHSTCMD);
373 break;
374 case I2C_SMBUS_BYTE_DATA:
375 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
376 SMBHSTADD);
377 size = ALI1535_BYTE_DATA;
378 outb_p(size, SMBHSTTYP); /* output command */
379 outb_p(command, SMBHSTCMD);
380 if (read_write == I2C_SMBUS_WRITE)
381 outb_p(data->byte, SMBHSTDAT0);
382 break;
383 case I2C_SMBUS_WORD_DATA:
384 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
385 SMBHSTADD);
386 size = ALI1535_WORD_DATA;
387 outb_p(size, SMBHSTTYP); /* output command */
388 outb_p(command, SMBHSTCMD);
389 if (read_write == I2C_SMBUS_WRITE) {
390 outb_p(data->word & 0xff, SMBHSTDAT0);
391 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
392 }
393 break;
394 case I2C_SMBUS_BLOCK_DATA:
395 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
396 SMBHSTADD);
397 size = ALI1535_BLOCK_DATA;
398 outb_p(size, SMBHSTTYP); /* output command */
399 outb_p(command, SMBHSTCMD);
400 if (read_write == I2C_SMBUS_WRITE) {
401 len = data->block[0];
402 if (len < 0) {
403 len = 0;
404 data->block[0] = len;
405 }
406 if (len > 32) {
407 len = 32;
408 data->block[0] = len;
409 }
410 outb_p(len, SMBHSTDAT0);
411 /* Reset SMBBLKDAT */
412 outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);
413 for (i = 1; i <= len; i++)
414 outb_p(data->block[i], SMBBLKDAT);
415 }
416 break;
Jean Delvareac7fc4f2008-07-14 22:38:25 +0200417 default:
418 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
419 result = -EOPNOTSUPP;
420 goto EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
David Brownell97140342008-07-14 22:38:25 +0200423 result = ali1535_transaction(adap);
424 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 goto EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 if ((read_write == I2C_SMBUS_WRITE) || (size == ALI1535_QUICK)) {
428 result = 0;
429 goto EXIT;
430 }
431
432 switch (size) {
433 case ALI1535_BYTE: /* Result put in SMBHSTDAT0 */
434 data->byte = inb_p(SMBHSTDAT0);
435 break;
436 case ALI1535_BYTE_DATA:
437 data->byte = inb_p(SMBHSTDAT0);
438 break;
439 case ALI1535_WORD_DATA:
440 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
441 break;
442 case ALI1535_BLOCK_DATA:
443 len = inb_p(SMBHSTDAT0);
444 if (len > 32)
445 len = 32;
446 data->block[0] = len;
447 /* Reset SMBBLKDAT */
448 outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);
449 for (i = 1; i <= data->block[0]; i++) {
450 data->block[i] = inb_p(SMBBLKDAT);
451 dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",
452 len, i, data->block[i]);
453 }
454 break;
455 }
456EXIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return result;
458}
459
460
461static u32 ali1535_func(struct i2c_adapter *adapter)
462{
463 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
464 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
465 I2C_FUNC_SMBUS_BLOCK_DATA;
466}
467
Jean Delvare8f9082c2006-09-03 22:39:46 +0200468static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 .smbus_xfer = ali1535_access,
470 .functionality = ali1535_func,
471};
472
473static struct i2c_adapter ali1535_adapter = {
474 .owner = THIS_MODULE,
Stephen Hemminger9ace5552007-02-13 22:09:01 +0100475 .id = I2C_HW_SMBUS_ALI1535,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 .class = I2C_CLASS_HWMON,
477 .algo = &smbus_algorithm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478};
479
480static struct pci_device_id ali1535_ids[] = {
481 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
482 { },
483};
484
485MODULE_DEVICE_TABLE (pci, ali1535_ids);
486
487static int __devinit ali1535_probe(struct pci_dev *dev, const struct pci_device_id *id)
488{
489 if (ali1535_setup(dev)) {
490 dev_warn(&dev->dev,
491 "ALI1535 not detected, module not inserted.\n");
492 return -ENODEV;
493 }
494
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100495 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 ali1535_adapter.dev.parent = &dev->dev;
497
David Brownell2096b952007-05-01 23:26:28 +0200498 snprintf(ali1535_adapter.name, sizeof(ali1535_adapter.name),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 "SMBus ALI1535 adapter at %04x", ali1535_smba);
500 return i2c_add_adapter(&ali1535_adapter);
501}
502
503static void __devexit ali1535_remove(struct pci_dev *dev)
504{
505 i2c_del_adapter(&ali1535_adapter);
506 release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
507}
508
509static struct pci_driver ali1535_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .name = "ali1535_smbus",
511 .id_table = ali1535_ids,
512 .probe = ali1535_probe,
513 .remove = __devexit_p(ali1535_remove),
514};
515
516static int __init i2c_ali1535_init(void)
517{
518 return pci_register_driver(&ali1535_driver);
519}
520
521static void __exit i2c_ali1535_exit(void)
522{
523 pci_unregister_driver(&ali1535_driver);
524}
525
526MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
527 "Philip Edelbrock <phil@netroedge.com>, "
528 "Mark D. Studebaker <mdsxyz123@yahoo.com> "
529 "and Dan Eaton <dan.eaton@rocketlogix.com>");
530MODULE_DESCRIPTION("ALI1535 SMBus driver");
531MODULE_LICENSE("GPL");
532
533module_init(i2c_ali1535_init);
534module_exit(i2c_ali1535_exit);