blob: 7b029b147a8e70a95b509440953dca7dc2468d09 [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);
David Brownell97140342008-07-14 22:38:25 +0200285 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
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) {
David Brownell97140342008-07-14 22:38:25 +0200307 result = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 dev_err(&adap->dev, "SMBus Timeout!\n");
309 }
310
311 if (temp & ALI15X3_STS_TERM) {
David Brownell97140342008-07-14 22:38:25 +0200312 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 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) {
David Brownell97140342008-07-14 22:38:25 +0200323 result = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 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) {
David Brownell97140342008-07-14 22:38:25 +0200331 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 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
David Brownell97140342008-07-14 22:38:25 +0200341/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342static 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 case I2C_SMBUS_QUICK:
366 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
367 SMBHSTADD);
368 size = ALI15X3_QUICK;
369 break;
370 case I2C_SMBUS_BYTE:
371 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
372 SMBHSTADD);
373 if (read_write == I2C_SMBUS_WRITE)
374 outb_p(command, SMBHSTCMD);
375 size = ALI15X3_BYTE;
376 break;
377 case I2C_SMBUS_BYTE_DATA:
378 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
379 SMBHSTADD);
380 outb_p(command, SMBHSTCMD);
381 if (read_write == I2C_SMBUS_WRITE)
382 outb_p(data->byte, SMBHSTDAT0);
383 size = ALI15X3_BYTE_DATA;
384 break;
385 case I2C_SMBUS_WORD_DATA:
386 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
387 SMBHSTADD);
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 size = ALI15X3_WORD_DATA;
394 break;
395 case I2C_SMBUS_BLOCK_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 len = data->block[0];
401 if (len < 0) {
402 len = 0;
403 data->block[0] = len;
404 }
405 if (len > 32) {
406 len = 32;
407 data->block[0] = len;
408 }
409 outb_p(len, SMBHSTDAT0);
410 /* Reset SMBBLKDAT */
411 outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);
412 for (i = 1; i <= len; i++)
413 outb_p(data->block[i], SMBBLKDAT);
414 }
415 size = ALI15X3_BLOCK_DATA;
416 break;
Jean Delvareac7fc4f2008-07-14 22:38:25 +0200417 default:
418 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
419 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 outb_p(size, SMBHSTCNT); /* output command */
423
David Brownell97140342008-07-14 22:38:25 +0200424 temp = ali15x3_transaction(adap);
425 if (temp)
426 return temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if ((read_write == I2C_SMBUS_WRITE) || (size == ALI15X3_QUICK))
429 return 0;
430
431
432 switch (size) {
433 case ALI15X3_BYTE: /* Result put in SMBHSTDAT0 */
434 data->byte = inb_p(SMBHSTDAT0);
435 break;
436 case ALI15X3_BYTE_DATA:
437 data->byte = inb_p(SMBHSTDAT0);
438 break;
439 case ALI15X3_WORD_DATA:
440 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
441 break;
442 case ALI15X3_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(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT);
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 }
456 return 0;
457}
458
459static u32 ali15x3_func(struct i2c_adapter *adapter)
460{
461 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
462 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
463 I2C_FUNC_SMBUS_BLOCK_DATA;
464}
465
Jean Delvare8f9082c2006-09-03 22:39:46 +0200466static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 .smbus_xfer = ali15x3_access,
468 .functionality = ali15x3_func,
469};
470
471static struct i2c_adapter ali15x3_adapter = {
472 .owner = THIS_MODULE,
Stephen Hemminger9ace5552007-02-13 22:09:01 +0100473 .id = I2C_HW_SMBUS_ALI15X3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 .class = I2C_CLASS_HWMON,
475 .algo = &smbus_algorithm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476};
477
478static struct pci_device_id ali15x3_ids[] = {
479 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
480 { 0, }
481};
482
483MODULE_DEVICE_TABLE (pci, ali15x3_ids);
484
485static int __devinit ali15x3_probe(struct pci_dev *dev, const struct pci_device_id *id)
486{
487 if (ali15x3_setup(dev)) {
488 dev_err(&dev->dev,
489 "ALI15X3 not detected, module not inserted.\n");
490 return -ENODEV;
491 }
492
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100493 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 ali15x3_adapter.dev.parent = &dev->dev;
495
David Brownell2096b952007-05-01 23:26:28 +0200496 snprintf(ali15x3_adapter.name, sizeof(ali15x3_adapter.name),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 "SMBus ALI15X3 adapter at %04x", ali15x3_smba);
498 return i2c_add_adapter(&ali15x3_adapter);
499}
500
501static void __devexit ali15x3_remove(struct pci_dev *dev)
502{
503 i2c_del_adapter(&ali15x3_adapter);
504 release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
505}
506
507static struct pci_driver ali15x3_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 .name = "ali15x3_smbus",
509 .id_table = ali15x3_ids,
510 .probe = ali15x3_probe,
511 .remove = __devexit_p(ali15x3_remove),
512};
513
514static int __init i2c_ali15x3_init(void)
515{
516 return pci_register_driver(&ali15x3_driver);
517}
518
519static void __exit i2c_ali15x3_exit(void)
520{
521 pci_unregister_driver(&ali15x3_driver);
522}
523
524MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl>, "
525 "Philip Edelbrock <phil@netroedge.com>, "
526 "and Mark D. Studebaker <mdsxyz123@yahoo.com>");
527MODULE_DESCRIPTION("ALI15X3 SMBus driver");
528MODULE_LICENSE("GPL");
529
530module_init(i2c_ali15x3_init);
531module_exit(i2c_ali15x3_exit);