Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
420 giacomo 1
/*
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
 
63
/* #define DEBUG 1 */
64
 
65
#include <linux/module.h>
66
#include <linux/pci.h>
67
#include <linux/kernel.h>
68
#include <linux/stddef.h>
69
#include <linux/sched.h>
70
#include <linux/ioport.h>
71
#include <linux/i2c.h>
72
#include <linux/init.h>
73
#include <asm/io.h>
74
 
75
/* ALI15X3 SMBus address offsets */
76
#define SMBHSTSTS       (0 + ali15x3_smba)
77
#define SMBHSTCNT       (1 + ali15x3_smba)
78
#define SMBHSTSTART     (2 + ali15x3_smba)
79
#define SMBHSTCMD       (7 + ali15x3_smba)
80
#define SMBHSTADD       (3 + ali15x3_smba)
81
#define SMBHSTDAT0      (4 + ali15x3_smba)
82
#define SMBHSTDAT1      (5 + ali15x3_smba)
83
#define SMBBLKDAT       (6 + ali15x3_smba)
84
 
85
/* PCI Address Constants */
86
#define SMBCOM          0x004
87
#define SMBBA           0x014
88
#define SMBATPC         0x05B   /* used to unlock xxxBA registers */
89
#define SMBHSTCFG       0x0E0
90
#define SMBSLVC         0x0E1
91
#define SMBCLK          0x0E2
92
#define SMBREV          0x008
93
 
94
/* Other settings */
95
#define MAX_TIMEOUT             200     /* times 1/100 sec */
96
#define ALI15X3_SMB_IOSIZE      32
97
 
98
/* this is what the Award 1004 BIOS sets them to on a ASUS P5A MB.
99
   We don't use these here. If the bases aren't set to some value we
100
   tell user to upgrade BIOS and we fail.
101
*/
102
#define ALI15X3_SMB_DEFAULTBASE 0xE800
103
 
104
/* ALI15X3 address lock bits */
105
#define ALI15X3_LOCK            0x06
106
 
107
/* ALI15X3 command constants */
108
#define ALI15X3_ABORT           0x02
109
#define ALI15X3_T_OUT           0x04
110
#define ALI15X3_QUICK           0x00
111
#define ALI15X3_BYTE            0x10
112
#define ALI15X3_BYTE_DATA       0x20
113
#define ALI15X3_WORD_DATA       0x30
114
#define ALI15X3_BLOCK_DATA      0x40
115
#define ALI15X3_BLOCK_CLR       0x80
116
 
117
/* ALI15X3 status register bits */
118
#define ALI15X3_STS_IDLE        0x04
119
#define ALI15X3_STS_BUSY        0x08
120
#define ALI15X3_STS_DONE        0x10
121
#define ALI15X3_STS_DEV         0x20    /* device error */
122
#define ALI15X3_STS_COLL        0x40    /* collision or no response */
123
#define ALI15X3_STS_TERM        0x80    /* terminated by abort */
124
#define ALI15X3_STS_ERR         0xE0    /* all the bad error bits */
125
 
126
 
127
/* If force_addr is set to anything different from 0, we forcibly enable
128
   the device at the given address. */
129
static int force_addr = 0;
130
MODULE_PARM(force_addr, "i");
131
MODULE_PARM_DESC(force_addr,
132
                 "Initialize the base address of the i2c controller");
133
 
134
static unsigned short ali15x3_smba = 0;
135
 
136
static int ali15x3_setup(struct pci_dev *ALI15X3_dev)
137
{
138
        u16 a;
139
        unsigned char temp;
140
 
141
        /* Check the following things:
142
                - SMB I/O address is initialized
143
                - Device is enabled
144
                - We can use the addresses
145
        */
146
 
147
        /* Unlock the register.
148
           The data sheet says that the address registers are read-only
149
           if the lock bits are 1, but in fact the address registers
150
           are zero unless you clear the lock bits.
151
        */
152
        pci_read_config_byte(ALI15X3_dev, SMBATPC, &temp);
153
        if (temp & ALI15X3_LOCK) {
154
                temp &= ~ALI15X3_LOCK;
155
                pci_write_config_byte(ALI15X3_dev, SMBATPC, temp);
156
        }
157
 
158
        /* Determine the address of the SMBus area */
159
        pci_read_config_word(ALI15X3_dev, SMBBA, &ali15x3_smba);
160
        ali15x3_smba &= (0xffff & ~(ALI15X3_SMB_IOSIZE - 1));
161
        if (ali15x3_smba == 0 && force_addr == 0) {
162
                dev_err(&ALI15X3_dev->dev, "ALI15X3_smb region uninitialized "
163
                        "- upgrade BIOS or use force_addr=0xaddr\n");
164
                return -ENODEV;
165
        }
166
 
167
        if(force_addr)
168
                ali15x3_smba = force_addr & ~(ALI15X3_SMB_IOSIZE - 1);
169
 
170
        if (!request_region(ali15x3_smba, ALI15X3_SMB_IOSIZE, "ali15x3-smb")) {
171
                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;
223
error:
224
        release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
225
        return -ENODEV;
226
}
227
 
228
/* Another internally used function */
229
static 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
                i2c_delay(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. */
342
static 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
                i2c_delay(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
 
458
static 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
 
465
static struct i2c_algorithm smbus_algorithm = {
466
        .name           = "Non-I2C SMBus adapter",
467
        .id             = I2C_ALGO_SMBUS,
468
        .smbus_xfer     = ali15x3_access,
469
        .functionality  = ali15x3_func,
470
};
471
 
472
static struct i2c_adapter ali15x3_adapter = {
473
        .owner          = THIS_MODULE,
474
        .class          = I2C_ADAP_CLASS_SMBUS,
475
        .algo           = &smbus_algorithm,
476
        .name           = "unset",
477
};
478
 
479
static struct pci_device_id ali15x3_ids[] = {
480
        {
481
        .vendor =       PCI_VENDOR_ID_AL,
482
        .device =       PCI_DEVICE_ID_AL_M7101,
483
        .subvendor =    PCI_ANY_ID,
484
        .subdevice =    PCI_ANY_ID,
485
        },
486
        { 0, }
487
};
488
 
489
static int __devinit ali15x3_probe(struct pci_dev *dev, const struct pci_device_id *id)
490
{
491
        if (ali15x3_setup(dev)) {
492
                dev_err(&dev->dev,
493
                        "ALI15X3 not detected, module not inserted.\n");
494
                return -ENODEV;
495
        }
496
 
497
        /* set up the driverfs linkage to our parent device */
498
        ali15x3_adapter.dev.parent = &dev->dev;
499
 
500
        snprintf(ali15x3_adapter.name, I2C_NAME_SIZE,
501
                "SMBus ALI15X3 adapter at %04x", ali15x3_smba);
502
        return i2c_add_adapter(&ali15x3_adapter);
503
}
504
 
505
static void __devexit ali15x3_remove(struct pci_dev *dev)
506
{
507
        i2c_del_adapter(&ali15x3_adapter);
508
        release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE);
509
}
510
 
511
static struct pci_driver ali15x3_driver = {
512
        .name           = "ali15x3 smbus",
513
        .id_table       = ali15x3_ids,
514
        .probe          = ali15x3_probe,
515
        .remove         = __devexit_p(ali15x3_remove),
516
};
517
 
518
static int __init i2c_ali15x3_init(void)
519
{
520
        return pci_module_init(&ali15x3_driver);
521
}
522
 
523
static void __exit i2c_ali15x3_exit(void)
524
{
525
        pci_unregister_driver(&ali15x3_driver);
526
}
527
 
528
MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl>, "
529
                "Philip Edelbrock <phil@netroedge.com>, "
530
                "and Mark D. Studebaker <mdsxyz123@yahoo.com>");
531
MODULE_DESCRIPTION("ALI15X3 SMBus driver");
532
MODULE_LICENSE("GPL");
533
 
534
module_init(i2c_ali15x3_init);
535
module_exit(i2c_ali15x3_exit);