[PATCH] hwmon: Discard bogus comment about init setting limits
[linux-block.git] / drivers / i2c / busses / i2c-ali1535.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
56#include <linux/module.h>
57#include <linux/pci.h>
58#include <linux/kernel.h>
59#include <linux/stddef.h>
60#include <linux/sched.h>
61#include <linux/delay.h>
62#include <linux/ioport.h>
63#include <linux/i2c.h>
64#include <linux/init.h>
65#include <asm/io.h>
66#include <asm/semaphore.h>
67
68
69/* ALI1535 SMBus address offsets */
70#define SMBHSTSTS (0 + ali1535_smba)
71#define SMBHSTTYP (1 + ali1535_smba)
72#define SMBHSTPORT (2 + ali1535_smba)
73#define SMBHSTCMD (7 + ali1535_smba)
74#define SMBHSTADD (3 + ali1535_smba)
75#define SMBHSTDAT0 (4 + ali1535_smba)
76#define SMBHSTDAT1 (5 + ali1535_smba)
77#define SMBBLKDAT (6 + ali1535_smba)
78
79/* PCI Address Constants */
80#define SMBCOM 0x004
81#define SMBREV 0x008
82#define SMBCFG 0x0D1
83#define SMBBA 0x0E2
84#define SMBHSTCFG 0x0F0
85#define SMBCLK 0x0F2
86
87/* Other settings */
88#define MAX_TIMEOUT 500 /* times 1/100 sec */
89#define ALI1535_SMB_IOSIZE 32
90
91#define ALI1535_SMB_DEFAULTBASE 0x8040
92
93/* ALI1535 address lock bits */
94#define ALI1535_LOCK 0x06 /* dwe */
95
96/* ALI1535 command constants */
97#define ALI1535_QUICK 0x00
98#define ALI1535_BYTE 0x10
99#define ALI1535_BYTE_DATA 0x20
100#define ALI1535_WORD_DATA 0x30
101#define ALI1535_BLOCK_DATA 0x40
102#define ALI1535_I2C_READ 0x60
103
104#define ALI1535_DEV10B_EN 0x80 /* Enable 10-bit addressing in */
105 /* I2C read */
106#define ALI1535_T_OUT 0x08 /* Time-out Command (write) */
107#define ALI1535_A_HIGH_BIT9 0x08 /* Bit 9 of 10-bit address in */
108 /* Alert-Response-Address */
109 /* (read) */
110#define ALI1535_KILL 0x04 /* Kill Command (write) */
111#define ALI1535_A_HIGH_BIT8 0x04 /* Bit 8 of 10-bit address in */
112 /* Alert-Response-Address */
113 /* (read) */
114
115#define ALI1535_D_HI_MASK 0x03 /* Mask for isolating bits 9-8 */
116 /* of 10-bit address in I2C */
117 /* Read Command */
118
119/* ALI1535 status register bits */
120#define ALI1535_STS_IDLE 0x04
121#define ALI1535_STS_BUSY 0x08 /* host busy */
122#define ALI1535_STS_DONE 0x10 /* transaction complete */
123#define ALI1535_STS_DEV 0x20 /* device error */
124#define ALI1535_STS_BUSERR 0x40 /* bus error */
125#define ALI1535_STS_FAIL 0x80 /* failed bus transaction */
126#define ALI1535_STS_ERR 0xE0 /* all the bad error bits */
127
128#define ALI1535_BLOCK_CLR 0x04 /* reset block data index */
129
130/* ALI1535 device address register bits */
131#define ALI1535_RD_ADDR 0x01 /* Read/Write Bit in Device */
132 /* Address field */
133 /* -> Write = 0 */
134 /* -> Read = 1 */
135#define ALI1535_SMBIO_EN 0x04 /* SMB I/O Space enable */
136
137
138static unsigned short ali1535_smba;
139static DECLARE_MUTEX(i2c_ali1535_sem);
140
141/* Detect whether a ALI1535 can be found, and initialize it, where necessary.
142 Note the differences between kernels with the old PCI BIOS interface and
143 newer kernels with the real PCI interface. In compat.h some things are
144 defined to make the transition easier. */
145static int ali1535_setup(struct pci_dev *dev)
146{
147 int retval = -ENODEV;
148 unsigned char temp;
149
150 /* Check the following things:
151 - SMB I/O address is initialized
152 - Device is enabled
153 - We can use the addresses
154 */
155
156 /* Determine the address of the SMBus area */
157 pci_read_config_word(dev, SMBBA, &ali1535_smba);
158 ali1535_smba &= (0xffff & ~(ALI1535_SMB_IOSIZE - 1));
159 if (ali1535_smba == 0) {
160 dev_warn(&dev->dev,
161 "ALI1535_smb region uninitialized - upgrade BIOS?\n");
162 goto exit;
163 }
164
165 if (!request_region(ali1535_smba, ALI1535_SMB_IOSIZE, "ali1535-smb")) {
166 dev_err(&dev->dev, "ALI1535_smb region 0x%x already in use!\n",
167 ali1535_smba);
168 goto exit;
169 }
170
171 /* check if whole device is enabled */
172 pci_read_config_byte(dev, SMBCFG, &temp);
173 if ((temp & ALI1535_SMBIO_EN) == 0) {
174 dev_err(&dev->dev, "SMB device not enabled - upgrade BIOS?\n");
175 goto exit_free;
176 }
177
178 /* Is SMB Host controller enabled? */
179 pci_read_config_byte(dev, SMBHSTCFG, &temp);
180 if ((temp & 1) == 0) {
181 dev_err(&dev->dev, "SMBus controller not enabled - upgrade BIOS?\n");
182 goto exit_free;
183 }
184
185 /* set SMB clock to 74KHz as recommended in data sheet */
186 pci_write_config_byte(dev, SMBCLK, 0x20);
187
188 /*
189 The interrupt routing for SMB is set up in register 0x77 in the
190 1533 ISA Bridge device, NOT in the 7101 device.
191 Don't bother with finding the 1533 device and reading the register.
192 if ((....... & 0x0F) == 1)
193 dev_dbg(&dev->dev, "ALI1535 using Interrupt 9 for SMBus.\n");
194 */
195 pci_read_config_byte(dev, SMBREV, &temp);
196 dev_dbg(&dev->dev, "SMBREV = 0x%X\n", temp);
197 dev_dbg(&dev->dev, "ALI1535_smba = 0x%X\n", ali1535_smba);
198
199 retval = 0;
200exit:
201 return retval;
202
203exit_free:
204 release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
205 return retval;
206}
207
208static int ali1535_transaction(struct i2c_adapter *adap)
209{
210 int temp;
211 int result = 0;
212 int timeout = 0;
213
214 dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, TYP=%02x, "
215 "CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
216 inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
217 inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
218
219 /* get status */
220 temp = inb_p(SMBHSTSTS);
221
222 /* Make sure the SMBus host is ready to start transmitting */
223 /* Check the busy bit first */
224 if (temp & ALI1535_STS_BUSY) {
225 /* If the host controller is still busy, it may have timed out
226 * in the previous transaction, resulting in a "SMBus Timeout"
227 * printk. I've tried the following to reset a stuck busy bit.
228 * 1. Reset the controller with an KILL command. (this
229 * doesn't seem to clear the controller if an external
230 * device is hung)
231 * 2. Reset the controller and the other SMBus devices with a
232 * T_OUT command. (this clears the host busy bit if an
233 * external device is hung, but it comes back upon a new
234 * access to a device)
235 * 3. Disable and reenable the controller in SMBHSTCFG. Worst
236 * case, nothing seems to work except power reset.
237 */
238
239 /* Try resetting entire SMB bus, including other devices - This
240 * may not work either - it clears the BUSY bit but then the
241 * BUSY bit may come back on when you try and use the chip
242 * again. If that's the case you are stuck.
243 */
244 dev_info(&adap->dev,
245 "Resetting entire SMB Bus to clear busy condition (%02x)\n",
246 temp);
247 outb_p(ALI1535_T_OUT, SMBHSTTYP);
248 temp = inb_p(SMBHSTSTS);
249 }
250
251 /* now check the error bits and the busy bit */
252 if (temp & (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
253 /* do a clear-on-write */
254 outb_p(0xFF, SMBHSTSTS);
255 if ((temp = inb_p(SMBHSTSTS)) &
256 (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
257 /* This is probably going to be correctable only by a
258 * power reset as one of the bits now appears to be
259 * stuck */
260 /* This may be a bus or device with electrical problems. */
261 dev_err(&adap->dev,
262 "SMBus reset failed! (0x%02x) - controller or "
263 "device on bus is probably hung\n", temp);
264 return -1;
265 }
266 } else {
267 /* check and clear done bit */
268 if (temp & ALI1535_STS_DONE) {
269 outb_p(temp, SMBHSTSTS);
270 }
271 }
272
273 /* start the transaction by writing anything to the start register */
274 outb_p(0xFF, SMBHSTPORT);
275
276 /* We will always wait for a fraction of a second! */
277 timeout = 0;
278 do {
279 msleep(1);
280 temp = inb_p(SMBHSTSTS);
281 } while (((temp & ALI1535_STS_BUSY) && !(temp & ALI1535_STS_IDLE))
282 && (timeout++ < MAX_TIMEOUT));
283
284 /* If the SMBus is still busy, we give up */
285 if (timeout >= MAX_TIMEOUT) {
286 result = -1;
287 dev_err(&adap->dev, "SMBus Timeout!\n");
288 }
289
290 if (temp & ALI1535_STS_FAIL) {
291 result = -1;
292 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
293 }
294
295 /* Unfortunately the ALI SMB controller maps "no response" and "bus
296 * collision" into a single bit. No reponse is the usual case so don't
297 * do a printk. This means that bus collisions go unreported.
298 */
299 if (temp & ALI1535_STS_BUSERR) {
300 result = -1;
301 dev_dbg(&adap->dev,
302 "Error: no response or bus collision ADD=%02x\n",
303 inb_p(SMBHSTADD));
304 }
305
306 /* haven't ever seen this */
307 if (temp & ALI1535_STS_DEV) {
308 result = -1;
309 dev_err(&adap->dev, "Error: device error\n");
310 }
311
312 /* check to see if the "command complete" indication is set */
313 if (!(temp & ALI1535_STS_DONE)) {
314 result = -1;
315 dev_err(&adap->dev, "Error: command never completed\n");
316 }
317
318 dev_dbg(&adap->dev, "Transaction (post): STS=%02x, TYP=%02x, "
319 "CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
320 inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
321 inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
322
323 /* take consequent actions for error conditions */
324 if (!(temp & ALI1535_STS_DONE)) {
325 /* issue "kill" to reset host controller */
326 outb_p(ALI1535_KILL,SMBHSTTYP);
327 outb_p(0xFF,SMBHSTSTS);
328 } else if (temp & ALI1535_STS_ERR) {
329 /* issue "timeout" to reset all devices on bus */
330 outb_p(ALI1535_T_OUT,SMBHSTTYP);
331 outb_p(0xFF,SMBHSTSTS);
332 }
333
334 return result;
335}
336
337/* Return -1 on error. */
338static s32 ali1535_access(struct i2c_adapter *adap, u16 addr,
339 unsigned short flags, char read_write, u8 command,
340 int size, union i2c_smbus_data *data)
341{
342 int i, len;
343 int temp;
344 int timeout;
345 s32 result = 0;
346
347 down(&i2c_ali1535_sem);
348 /* make sure SMBus is idle */
349 temp = inb_p(SMBHSTSTS);
350 for (timeout = 0;
351 (timeout < MAX_TIMEOUT) && !(temp & ALI1535_STS_IDLE);
352 timeout++) {
353 msleep(1);
354 temp = inb_p(SMBHSTSTS);
355 }
356 if (timeout >= MAX_TIMEOUT)
357 dev_warn(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);
358
359 /* clear status register (clear-on-write) */
360 outb_p(0xFF, SMBHSTSTS);
361
362 switch (size) {
363 case I2C_SMBUS_PROC_CALL:
364 dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
365 result = -1;
366 goto EXIT;
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);
399 }
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;
412 }
413 if (len > 32) {
414 len = 32;
415 data->block[0] = len;
416 }
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);
422 }
423 break;
424 }
425
426 if (ali1535_transaction(adap)) {
427 /* Error in transaction */
428 result = -1;
429 goto EXIT;
430 }
431
432 if ((read_write == I2C_SMBUS_WRITE) || (size == ALI1535_QUICK)) {
433 result = 0;
434 goto EXIT;
435 }
436
437 switch (size) {
438 case ALI1535_BYTE: /* Result put in SMBHSTDAT0 */
439 data->byte = inb_p(SMBHSTDAT0);
440 break;
441 case ALI1535_BYTE_DATA:
442 data->byte = inb_p(SMBHSTDAT0);
443 break;
444 case ALI1535_WORD_DATA:
445 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
446 break;
447 case ALI1535_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(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);
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 }
461EXIT:
462 up(&i2c_ali1535_sem);
463 return result;
464}
465
466
467static u32 ali1535_func(struct i2c_adapter *adapter)
468{
469 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
470 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
471 I2C_FUNC_SMBUS_BLOCK_DATA;
472}
473
474static struct i2c_algorithm smbus_algorithm = {
1da177e4
LT
475 .smbus_xfer = ali1535_access,
476 .functionality = ali1535_func,
477};
478
479static struct i2c_adapter ali1535_adapter = {
480 .owner = THIS_MODULE,
481 .class = I2C_CLASS_HWMON,
482 .algo = &smbus_algorithm,
483 .name = "unset",
484};
485
486static struct pci_device_id ali1535_ids[] = {
487 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
488 { },
489};
490
491MODULE_DEVICE_TABLE (pci, ali1535_ids);
492
493static int __devinit ali1535_probe(struct pci_dev *dev, const struct pci_device_id *id)
494{
495 if (ali1535_setup(dev)) {
496 dev_warn(&dev->dev,
497 "ALI1535 not detected, module not inserted.\n");
498 return -ENODEV;
499 }
500
501 /* set up the driverfs linkage to our parent device */
502 ali1535_adapter.dev.parent = &dev->dev;
503
504 snprintf(ali1535_adapter.name, I2C_NAME_SIZE,
505 "SMBus ALI1535 adapter at %04x", ali1535_smba);
506 return i2c_add_adapter(&ali1535_adapter);
507}
508
509static void __devexit ali1535_remove(struct pci_dev *dev)
510{
511 i2c_del_adapter(&ali1535_adapter);
512 release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
513}
514
515static struct pci_driver ali1535_driver = {
516 .name = "ali1535_smbus",
517 .id_table = ali1535_ids,
518 .probe = ali1535_probe,
519 .remove = __devexit_p(ali1535_remove),
520};
521
522static int __init i2c_ali1535_init(void)
523{
524 return pci_register_driver(&ali1535_driver);
525}
526
527static void __exit i2c_ali1535_exit(void)
528{
529 pci_unregister_driver(&ali1535_driver);
530}
531
532MODULE_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>");
536MODULE_DESCRIPTION("ALI1535 SMBus driver");
537MODULE_LICENSE("GPL");
538
539module_init(i2c_ali1535_init);
540module_exit(i2c_ali1535_exit);