blob: 93bf87d709618fff0ba489aa17028b2d84a39f23 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/module.h>
64#include <linux/pci.h>
65#include <linux/kernel.h>
66#include <linux/stddef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/ioport.h>
68#include <linux/delay.h>
69#include <linux/i2c.h>
70#include <linux/init.h>
71#include <asm/io.h>
72
73/* ALI15X3 SMBus address offsets */
74#define SMBHSTSTS (0 + ali15x3_smba)
75#define SMBHSTCNT (1 + ali15x3_smba)
76#define SMBHSTSTART (2 + ali15x3_smba)
77#define SMBHSTCMD (7 + ali15x3_smba)
78#define SMBHSTADD (3 + ali15x3_smba)
79#define SMBHSTDAT0 (4 + ali15x3_smba)
80#define SMBHSTDAT1 (5 + ali15x3_smba)
81#define SMBBLKDAT (6 + ali15x3_smba)
82
83/* PCI Address Constants */
84#define SMBCOM 0x004
85#define SMBBA 0x014
86#define SMBATPC 0x05B /* used to unlock xxxBA registers */
87#define SMBHSTCFG 0x0E0
88#define SMBSLVC 0x0E1
89#define SMBCLK 0x0E2
90#define SMBREV 0x008
91
92/* Other settings */
93#define MAX_TIMEOUT 200 /* times 1/100 sec */
94#define ALI15X3_SMB_IOSIZE 32
95
96/* this is what the Award 1004 BIOS sets them to on a ASUS P5A MB.
97 We don't use these here. If the bases aren't set to some value we
98 tell user to upgrade BIOS and we fail.
99*/
100#define ALI15X3_SMB_DEFAULTBASE 0xE800
101
102/* ALI15X3 address lock bits */
103#define ALI15X3_LOCK 0x06
104
105/* ALI15X3 command constants */
106#define ALI15X3_ABORT 0x02
107#define ALI15X3_T_OUT 0x04
108#define ALI15X3_QUICK 0x00
109#define ALI15X3_BYTE 0x10
110#define ALI15X3_BYTE_DATA 0x20
111#define ALI15X3_WORD_DATA 0x30
112#define ALI15X3_BLOCK_DATA 0x40
113#define ALI15X3_BLOCK_CLR 0x80
114
115/* ALI15X3 status register bits */
116#define ALI15X3_STS_IDLE 0x04
117#define ALI15X3_STS_BUSY 0x08
118#define ALI15X3_STS_DONE 0x10
119#define ALI15X3_STS_DEV 0x20 /* device error */
120#define ALI15X3_STS_COLL 0x40 /* collision or no response */
121#define ALI15X3_STS_TERM 0x80 /* terminated by abort */
122#define ALI15X3_STS_ERR 0xE0 /* all the bad error bits */
123
124
125/* If force_addr is set to anything different from 0, we forcibly enable
126 the device at the given address. */
Jean Delvare60507092005-09-25 16:23:07 +0200127static u16 force_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128module_param(force_addr, ushort, 0);
129MODULE_PARM_DESC(force_addr,
130 "Initialize the base address of the i2c controller");
131
Jean Delvared6072f82005-09-25 16:37:04 +0200132static struct pci_driver ali15x3_driver;
Jean Delvare60507092005-09-25 16:23:07 +0200133static unsigned short ali15x3_smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135static int ali15x3_setup(struct pci_dev *ALI15X3_dev)
136{
137 u16 a;
138 unsigned char temp;
139
140 /* Check the following things:
141 - SMB I/O address is initialized
142 - Device is enabled
143 - We can use the addresses
144 */
145
146 /* Unlock the register.
147 The data sheet says that the address registers are read-only
148 if the lock bits are 1, but in fact the address registers
149 are zero unless you clear the lock bits.
150 */
151 pci_read_config_byte(ALI15X3_dev, SMBATPC, &temp);
152 if (temp & ALI15X3_LOCK) {
153 temp &= ~ALI15X3_LOCK;
154 pci_write_config_byte(ALI15X3_dev, SMBATPC, temp);
155 }
156
157 /* Determine the address of the SMBus area */
158 pci_read_config_word(ALI15X3_dev, SMBBA, &ali15x3_smba);
159 ali15x3_smba &= (0xffff & ~(ALI15X3_SMB_IOSIZE - 1));
160 if (ali15x3_smba == 0 && force_addr == 0) {
161 dev_err(&ALI15X3_dev->dev, "ALI15X3_smb region uninitialized "
162 "- upgrade BIOS or use force_addr=0xaddr\n");
163 return -ENODEV;
164 }
165
166 if(force_addr)
167 ali15x3_smba = force_addr & ~(ALI15X3_SMB_IOSIZE - 1);
168
Jean Delvared6072f82005-09-25 16:37:04 +0200169 if (!request_region(ali15x3_smba, ALI15X3_SMB_IOSIZE,
170 ali15x3_driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 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;
223error:
224 release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
225 return -ENODEV;
226}
227
228/* Another internally used function */
229static int ali15x3_transaction(struct i2c_adapter *adap)
230{
231 int temp;
232 int result = 0;
233 int timeout = 0;
234
235 dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, CNT=%02x, CMD=%02x, "
236 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTSTS),
237 inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),
238 inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
239
240 /* get status */
241 temp = inb_p(SMBHSTSTS);
242
243 /* Make sure the SMBus host is ready to start transmitting */
244 /* Check the busy bit first */
245 if (temp & ALI15X3_STS_BUSY) {
246 /*
247 If the host controller is still busy, it may have timed out in the
248 previous transaction, resulting in a "SMBus Timeout" Dev.
249 I've tried the following to reset a stuck busy bit.
250 1. Reset the controller with an ABORT command.
251 (this doesn't seem to clear the controller if an external
252 device is hung)
253 2. Reset the controller and the other SMBus devices with a
254 T_OUT command. (this clears the host busy bit if an
255 external device is hung, but it comes back upon a new access
256 to a device)
257 3. Disable and reenable the controller in SMBHSTCFG
258 Worst case, nothing seems to work except power reset.
259 */
260 /* Abort - reset the host controller */
261 /*
262 Try resetting entire SMB bus, including other devices -
263 This may not work either - it clears the BUSY bit but
264 then the BUSY bit may come back on when you try and use the chip again.
265 If that's the case you are stuck.
266 */
267 dev_info(&adap->dev, "Resetting entire SMB Bus to "
268 "clear busy condition (%02x)\n", temp);
269 outb_p(ALI15X3_T_OUT, SMBHSTCNT);
270 temp = inb_p(SMBHSTSTS);
271 }
272
273 /* now check the error bits and the busy bit */
274 if (temp & (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) {
275 /* do a clear-on-write */
276 outb_p(0xFF, SMBHSTSTS);
277 if ((temp = inb_p(SMBHSTSTS)) &
278 (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) {
279 /* this is probably going to be correctable only by a power reset
280 as one of the bits now appears to be stuck */
281 /* This may be a bus or device with electrical problems. */
282 dev_err(&adap->dev, "SMBus reset failed! (0x%02x) - "
283 "controller or device on bus is probably hung\n",
284 temp);
285 return -1;
286 }
287 } else {
288 /* check and clear done bit */
289 if (temp & ALI15X3_STS_DONE) {
290 outb_p(temp, SMBHSTSTS);
291 }
292 }
293
294 /* start the transaction by writing anything to the start register */
295 outb_p(0xFF, SMBHSTSTART);
296
297 /* We will always wait for a fraction of a second! */
298 timeout = 0;
299 do {
300 msleep(1);
301 temp = inb_p(SMBHSTSTS);
302 } while ((!(temp & (ALI15X3_STS_ERR | ALI15X3_STS_DONE)))
303 && (timeout++ < MAX_TIMEOUT));
304
305 /* If the SMBus is still busy, we give up */
306 if (timeout >= MAX_TIMEOUT) {
307 result = -1;
308 dev_err(&adap->dev, "SMBus Timeout!\n");
309 }
310
311 if (temp & ALI15X3_STS_TERM) {
312 result = -1;
313 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
314 }
315
316 /*
317 Unfortunately the ALI SMB controller maps "no response" and "bus
318 collision" into a single bit. No reponse is the usual case so don't
319 do a printk.
320 This means that bus collisions go unreported.
321 */
322 if (temp & ALI15X3_STS_COLL) {
323 result = -1;
324 dev_dbg(&adap->dev,
325 "Error: no response or bus collision ADD=%02x\n",
326 inb_p(SMBHSTADD));
327 }
328
329 /* haven't ever seen this */
330 if (temp & ALI15X3_STS_DEV) {
331 result = -1;
332 dev_err(&adap->dev, "Error: device error\n");
333 }
334 dev_dbg(&adap->dev, "Transaction (post): STS=%02x, CNT=%02x, CMD=%02x, "
335 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTSTS),
336 inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD),
337 inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
338 return result;
339}
340
341/* Return -1 on error. */
342static s32 ali15x3_access(struct i2c_adapter * adap, u16 addr,
343 unsigned short flags, char read_write, u8 command,
344 int size, union i2c_smbus_data * data)
345{
346 int i, len;
347 int temp;
348 int timeout;
349
350 /* clear all the bits (clear-on-write) */
351 outb_p(0xFF, SMBHSTSTS);
352 /* make sure SMBus is idle */
353 temp = inb_p(SMBHSTSTS);
354 for (timeout = 0;
355 (timeout < MAX_TIMEOUT) && !(temp & ALI15X3_STS_IDLE);
356 timeout++) {
357 msleep(1);
358 temp = inb_p(SMBHSTSTS);
359 }
360 if (timeout >= MAX_TIMEOUT) {
361 dev_err(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);
362 }
363
364 switch (size) {
365 case I2C_SMBUS_PROC_CALL:
366 dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
367 return -1;
368 case I2C_SMBUS_QUICK:
369 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
370 SMBHSTADD);
371 size = ALI15X3_QUICK;
372 break;
373 case I2C_SMBUS_BYTE:
374 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
375 SMBHSTADD);
376 if (read_write == I2C_SMBUS_WRITE)
377 outb_p(command, SMBHSTCMD);
378 size = ALI15X3_BYTE;
379 break;
380 case I2C_SMBUS_BYTE_DATA:
381 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
382 SMBHSTADD);
383 outb_p(command, SMBHSTCMD);
384 if (read_write == I2C_SMBUS_WRITE)
385 outb_p(data->byte, SMBHSTDAT0);
386 size = ALI15X3_BYTE_DATA;
387 break;
388 case I2C_SMBUS_WORD_DATA:
389 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
390 SMBHSTADD);
391 outb_p(command, SMBHSTCMD);
392 if (read_write == I2C_SMBUS_WRITE) {
393 outb_p(data->word & 0xff, SMBHSTDAT0);
394 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
395 }
396 size = ALI15X3_WORD_DATA;
397 break;
398 case I2C_SMBUS_BLOCK_DATA:
399 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
400 SMBHSTADD);
401 outb_p(command, SMBHSTCMD);
402 if (read_write == I2C_SMBUS_WRITE) {
403 len = data->block[0];
404 if (len < 0) {
405 len = 0;
406 data->block[0] = len;
407 }
408 if (len > 32) {
409 len = 32;
410 data->block[0] = len;
411 }
412 outb_p(len, SMBHSTDAT0);
413 /* Reset SMBBLKDAT */
414 outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);
415 for (i = 1; i <= len; i++)
416 outb_p(data->block[i], SMBBLKDAT);
417 }
418 size = ALI15X3_BLOCK_DATA;
419 break;
420 }
421
422 outb_p(size, SMBHSTCNT); /* output command */
423
424 if (ali15x3_transaction(adap)) /* Error in transaction */
425 return -1;
426
427 if ((read_write == I2C_SMBUS_WRITE) || (size == ALI15X3_QUICK))
428 return 0;
429
430
431 switch (size) {
432 case ALI15X3_BYTE: /* Result put in SMBHSTDAT0 */
433 data->byte = inb_p(SMBHSTDAT0);
434 break;
435 case ALI15X3_BYTE_DATA:
436 data->byte = inb_p(SMBHSTDAT0);
437 break;
438 case ALI15X3_WORD_DATA:
439 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
440 break;
441 case ALI15X3_BLOCK_DATA:
442 len = inb_p(SMBHSTDAT0);
443 if (len > 32)
444 len = 32;
445 data->block[0] = len;
446 /* Reset SMBBLKDAT */
447 outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);
448 for (i = 1; i <= data->block[0]; i++) {
449 data->block[i] = inb_p(SMBBLKDAT);
450 dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",
451 len, i, data->block[i]);
452 }
453 break;
454 }
455 return 0;
456}
457
458static u32 ali15x3_func(struct i2c_adapter *adapter)
459{
460 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
461 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
462 I2C_FUNC_SMBUS_BLOCK_DATA;
463}
464
Jean Delvare8f9082c2006-09-03 22:39:46 +0200465static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 .smbus_xfer = ali15x3_access,
467 .functionality = ali15x3_func,
468};
469
470static struct i2c_adapter ali15x3_adapter = {
471 .owner = THIS_MODULE,
Stephen Hemminger9ace5552007-02-13 22:09:01 +0100472 .id = I2C_HW_SMBUS_ALI15X3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 .class = I2C_CLASS_HWMON,
474 .algo = &smbus_algorithm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475};
476
477static struct pci_device_id ali15x3_ids[] = {
478 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
479 { 0, }
480};
481
482MODULE_DEVICE_TABLE (pci, ali15x3_ids);
483
484static int __devinit ali15x3_probe(struct pci_dev *dev, const struct pci_device_id *id)
485{
486 if (ali15x3_setup(dev)) {
487 dev_err(&dev->dev,
488 "ALI15X3 not detected, module not inserted.\n");
489 return -ENODEV;
490 }
491
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100492 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 ali15x3_adapter.dev.parent = &dev->dev;
494
David Brownell2096b952007-05-01 23:26:28 +0200495 snprintf(ali15x3_adapter.name, sizeof(ali15x3_adapter.name),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 "SMBus ALI15X3 adapter at %04x", ali15x3_smba);
497 return i2c_add_adapter(&ali15x3_adapter);
498}
499
500static void __devexit ali15x3_remove(struct pci_dev *dev)
501{
502 i2c_del_adapter(&ali15x3_adapter);
503 release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
504}
505
506static struct pci_driver ali15x3_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 .name = "ali15x3_smbus",
508 .id_table = ali15x3_ids,
509 .probe = ali15x3_probe,
510 .remove = __devexit_p(ali15x3_remove),
511};
512
513static int __init i2c_ali15x3_init(void)
514{
515 return pci_register_driver(&ali15x3_driver);
516}
517
518static void __exit i2c_ali15x3_exit(void)
519{
520 pci_unregister_driver(&ali15x3_driver);
521}
522
523MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl>, "
524 "Philip Edelbrock <phil@netroedge.com>, "
525 "and Mark D. Studebaker <mdsxyz123@yahoo.com>");
526MODULE_DESCRIPTION("ALI15X3 SMBus driver");
527MODULE_LICENSE("GPL");
528
529module_init(i2c_ali15x3_init);
530module_exit(i2c_ali15x3_exit);