root/lm-sensors/trunk/kernel/busses/i2c-viapro.c @ 4039

Revision 4039, 13.5 KB (checked in by khali, 7 years ago)

i2c-viapro.c: Move definition of PCI IDs before they are used.
This fixes compilation with old kernels (2.4.20 and earlier). Thanks
to Yuan Mu for reporting.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    i2c-viapro.c - Part of lm_sensors, Linux kernel modules for hardware
3              monitoring
4    Copyright (c) 1998 - 2002  Frodo Looijaard <frodol@dds.nl>,
5    Philip Edelbrock <phil@netroedge.com>, Kyösti Mälkki <kmalkki@cc.hut.fi>,
6    Mark D. Studebaker <mdsxyz123@yahoo.com>
7    Copyright (C) 2005  Jean Delvare <khali@linux-fr.org>
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24/*
25   Supports the following VIA south bridges:
26
27   Chip name          PCI ID  REV     I2C block
28   VT82C596A          0x3050             no
29   VT82C596B          0x3051             no
30   VT82C686A          0x3057  0x30       no
31   VT82C686B          0x3057  0x40       yes
32   VT8231             0x8235             no?
33   VT8233             0x3074             yes
34   VT8233A            0x3147             yes?
35   VT8235             0x3177             yes
36   VT8237R            0x3227             yes
37
38   Note: we assume there can only be one device, with one SMBus interface.
39*/
40
41#include <linux/module.h>
42#include <linux/pci.h>
43#include <linux/kernel.h>
44#include <linux/stddef.h>
45#include <linux/ioport.h>
46#include <linux/i2c.h>
47#include <linux/init.h>
48#include <asm/io.h>
49#include "version.h"
50#include "sensors_compat.h"
51
52/* 8233A is undefined before kernel 2.4.19 */
53#ifndef PCI_DEVICE_ID_VIA_8233A
54#define PCI_DEVICE_ID_VIA_8233A 0x3147
55#endif
56/* 8235 is undefined before kernel 2.4.21 */
57#ifndef PCI_DEVICE_ID_VIA_8235
58#define PCI_DEVICE_ID_VIA_8235  0x3177
59#endif
60/* 8237 is undefined before kernel 2.4.21 */
61#ifndef PCI_DEVICE_ID_VIA_8237
62#define PCI_DEVICE_ID_VIA_8237  0x3227
63#endif
64
65#define SMBBA1          0x90
66#define SMBBA2          0x80
67#define SMBBA3          0xD0
68
69/* SMBus address offsets */
70static unsigned short vt596_smba;
71#define SMBHSTSTS       (vt596_smba + 0)
72#define SMBHSTCNT       (vt596_smba + 2)
73#define SMBHSTCMD       (vt596_smba + 3)
74#define SMBHSTADD       (vt596_smba + 4)
75#define SMBHSTDAT0      (vt596_smba + 5)
76#define SMBHSTDAT1      (vt596_smba + 6)
77#define SMBBLKDAT       (vt596_smba + 7)
78
79/* PCI Address Constants */
80
81/* SMBus data in configuration space can be found in two places,
82   We try to select the better one */
83
84static unsigned short SMBHSTCFG = 0xD2;
85
86/* Other settings */
87#define MAX_TIMEOUT     500
88
89/* VT82C596 constants */
90#define VT596_QUICK             0x00
91#define VT596_BYTE              0x04
92#define VT596_BYTE_DATA         0x08
93#define VT596_WORD_DATA         0x0C
94#define VT596_BLOCK_DATA        0x14
95#define VT596_I2C_BLOCK_DATA    0x34
96
97
98/* If force is set to anything different from 0, we forcibly enable the
99   VT596. DANGEROUS! */
100static int force;
101MODULE_PARM(force, "i");
102MODULE_PARM_DESC(force, "Forcibly enable the SMBus. DANGEROUS!");
103
104/* If force_addr is set to anything different from 0, we forcibly enable
105   the VT596 at the given address. VERY DANGEROUS! */
106static int force_addr;
107MODULE_PARM(force_addr, "i");
108MODULE_PARM_DESC(force_addr,
109                 "Forcibly enable the SMBus at the given address. "
110                 "EXTREMELY DANGEROUS!");
111
112
113static struct i2c_adapter vt596_adapter;
114
115#define FEATURE_I2CBLOCK        (1<<0)
116static unsigned int vt596_features;
117
118#ifdef DEBUG
119static void vt596_dump_regs(const char *msg, u8 size)
120{
121        dev_dbg(&vt596_adapter, "%s: STS=%02x CNT=%02x CMD=%02x ADD=%02x "
122                "DAT=%02x,%02x\n", msg, inb_p(SMBHSTSTS), inb_p(SMBHSTCNT),
123                inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
124                inb_p(SMBHSTDAT1));
125
126        if (size == VT596_BLOCK_DATA
127         || size == VT596_I2C_BLOCK_DATA) {
128                int i;
129
130                dev_dbg(&vt596_adapter, "BLK=");
131                for (i = 0; i < I2C_SMBUS_BLOCK_MAX / 2; i++)
132                        printk("%02x,", inb_p(SMBBLKDAT));
133                printk("\n");
134                dev_dbg(&vt596_adapter, "    ");
135                for (; i < I2C_SMBUS_BLOCK_MAX - 1; i++)
136                        printk("%02x,", inb_p(SMBBLKDAT));
137                printk("%02x\n", inb_p(SMBBLKDAT));
138        }
139}
140#else
141static inline void vt596_dump_regs(const char *msg, u8 size) { }
142#endif
143
144/* Return -1 on error, 0 on success */
145static int vt596_transaction(u8 size)
146{
147        int temp;
148        int result = 0;
149        int timeout = 0;
150
151        vt596_dump_regs("Transaction (pre)", size);
152
153        /* Make sure the SMBus host is ready to start transmitting */
154        if ((temp = inb_p(SMBHSTSTS)) & 0x1F) {
155                dev_dbg(&vt596_adapter, "SMBus busy (0x%02x). "
156                        "Resetting...\n", temp);
157
158                outb_p(temp, SMBHSTSTS);
159                if ((temp = inb_p(SMBHSTSTS)) & 0x1F) {
160                        dev_err(&vt596_adapter, "SMBus reset failed! "
161                                "(0x%02x)\n", temp);
162                        return -1;
163                }
164        }
165
166        /* Start the transaction by setting bit 6 */
167        outb_p(0x40 | size, SMBHSTCNT);
168
169        /* We will always wait for a fraction of a second */
170        do {
171                i2c_delay(1);
172                temp = inb_p(SMBHSTSTS);
173        } while ((temp & 0x01) && (timeout++ < MAX_TIMEOUT));
174
175        /* If the SMBus is still busy, we give up */
176        if (timeout >= MAX_TIMEOUT) {
177                result = -1;
178                dev_err(&vt596_adapter, "SMBus Timeout!\n");
179        }
180
181        if (temp & 0x10) {
182                result = -1;
183                dev_err(&vt596_adapter, "Transaction failed (0x%02x)\n", size);
184        }
185
186        if (temp & 0x08) {
187                result = -1;
188                dev_err(&vt596_adapter, "SMBus collision!\n");
189        }
190
191        if (temp & 0x04) {
192                int read = inb_p(SMBHSTADD) & 0x01;
193                result = -1;
194                /* The quick and receive byte commands are used to probe
195                   for chips, so errors are expected, and we don't want
196                   to frighten the user. */
197                if (!((size == VT596_QUICK && !read) ||
198                      (size == VT596_BYTE && read)))
199                        dev_err(&vt596_adapter, "Transaction error!\n");
200        }
201
202        /* Resetting status register */
203        if (temp & 0x1F)
204                outb_p(temp, SMBHSTSTS);
205
206        vt596_dump_regs("Transaction (post)", size);
207
208        return result;
209}
210
211/* Return -1 on error, 0 on success */
212static s32 vt596_access(struct i2c_adapter *adap, u16 addr,
213                unsigned short flags, char read_write, u8 command,
214                int size, union i2c_smbus_data *data)
215{
216        int i;
217
218        switch (size) {
219        case I2C_SMBUS_QUICK:
220                size = VT596_QUICK;
221                break;
222        case I2C_SMBUS_BYTE:
223                if (read_write == I2C_SMBUS_WRITE)
224                        outb_p(command, SMBHSTCMD);
225                size = VT596_BYTE;
226                break;
227        case I2C_SMBUS_BYTE_DATA:
228                outb_p(command, SMBHSTCMD);
229                if (read_write == I2C_SMBUS_WRITE)
230                        outb_p(data->byte, SMBHSTDAT0);
231                size = VT596_BYTE_DATA;
232                break;
233        case I2C_SMBUS_WORD_DATA:
234                outb_p(command, SMBHSTCMD);
235                if (read_write == I2C_SMBUS_WRITE) {
236                        outb_p(data->word & 0xff, SMBHSTDAT0);
237                        outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
238                }
239                size = VT596_WORD_DATA;
240                break;
241        case I2C_SMBUS_I2C_BLOCK_DATA:
242                if (!(vt596_features & FEATURE_I2CBLOCK))
243                        goto exit_unsupported;
244                if (read_write == I2C_SMBUS_READ)
245                        outb_p(I2C_SMBUS_BLOCK_MAX, SMBHSTDAT0);
246                /* Fall through */
247        case I2C_SMBUS_BLOCK_DATA:
248                outb_p(command, SMBHSTCMD);
249                if (read_write == I2C_SMBUS_WRITE) {
250                        u8 len = data->block[0];
251                        if (len > I2C_SMBUS_BLOCK_MAX)
252                                len = I2C_SMBUS_BLOCK_MAX;
253                        outb_p(len, SMBHSTDAT0);
254                        inb_p(SMBHSTCNT);       /* Reset SMBBLKDAT */
255                        for (i = 1; i <= len; i++)
256                                outb_p(data->block[i], SMBBLKDAT);
257                }
258                size = (size == I2C_SMBUS_I2C_BLOCK_DATA) ?
259                       VT596_I2C_BLOCK_DATA : VT596_BLOCK_DATA;
260                break;
261        default:
262                goto exit_unsupported;
263        }
264
265        outb_p(((addr & 0x7f) << 1) | read_write, SMBHSTADD);
266
267        if (vt596_transaction(size)) /* Error in transaction */
268                return -1;
269
270        if ((read_write == I2C_SMBUS_WRITE) || (size == VT596_QUICK))
271                return 0;
272
273        switch (size) {
274        case VT596_BYTE:
275        case VT596_BYTE_DATA:
276                data->byte = inb_p(SMBHSTDAT0);
277                break;
278        case VT596_WORD_DATA:
279                data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
280                break;
281        case VT596_I2C_BLOCK_DATA:
282        case VT596_BLOCK_DATA:
283                data->block[0] = inb_p(SMBHSTDAT0);
284                if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
285                        data->block[0] = I2C_SMBUS_BLOCK_MAX;
286                inb_p(SMBHSTCNT);       /* Reset SMBBLKDAT */
287                for (i = 1; i <= data->block[0]; i++)
288                        data->block[i] = inb_p(SMBBLKDAT);
289                break;
290        }
291        return 0;
292
293exit_unsupported:
294        dev_warn(&vt596_adapter, "Unsupported command invoked! (0x%02x)\n",
295                 size);
296        return -1;
297}
298
299static void vt596_inc(struct i2c_adapter *adapter)
300{
301#ifdef MODULE
302        MOD_INC_USE_COUNT;
303#endif
304}
305
306static void vt596_dec(struct i2c_adapter *adapter)
307{
308#ifdef MODULE
309        MOD_DEC_USE_COUNT;
310#endif
311}
312
313static u32 vt596_func(struct i2c_adapter *adapter)
314{
315        u32 func = I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
316            I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
317            I2C_FUNC_SMBUS_BLOCK_DATA;
318
319        if (vt596_features & FEATURE_I2CBLOCK)
320                func |= I2C_FUNC_SMBUS_I2C_BLOCK;
321        return func;
322}
323
324static struct i2c_algorithm smbus_algorithm = {
325        .name           = "Non-I2C SMBus adapter",
326        .id             = I2C_ALGO_SMBUS,
327        .smbus_xfer     = vt596_access,
328        .functionality  = vt596_func,
329};
330
331static struct i2c_adapter vt596_adapter = {
332        .id             = I2C_ALGO_SMBUS | I2C_HW_SMBUS_VIA2,
333        .algo           = &smbus_algorithm,
334        .inc_use        = vt596_inc,
335        .dec_use        = vt596_dec,
336};
337
338static int __init vt596_probe(struct pci_dev *pdev,
339                              const struct pci_device_id *id)
340{
341        unsigned char temp;
342        int error = -ENODEV;
343
344        /* Determine the address of the SMBus areas */
345        if (force_addr) {
346                vt596_smba = force_addr & 0xfff0;
347                force = 0;
348                goto found;
349        }
350
351        if ((pci_read_config_word(pdev, id->driver_data, &vt596_smba)) ||
352            !(vt596_smba & 0x0001)) {
353                /* try 2nd address and config reg. for 596 */
354                if (id->device == PCI_DEVICE_ID_VIA_82C596_3 &&
355                    !pci_read_config_word(pdev, SMBBA2, &vt596_smba) &&
356                    (vt596_smba & 0x0001)) {
357                        SMBHSTCFG = 0x84;
358                } else {
359                        /* no matches at all */
360                        dev_err(pdev, "Cannot configure "
361                                "SMBus I/O Base address\n");
362                        return -ENODEV;
363                }
364        }
365
366        vt596_smba &= 0xfff0;
367        if (vt596_smba == 0) {
368                dev_err(pdev, "SMBus base address "
369                        "uninitialized - upgrade BIOS or use "
370                        "force_addr=0xaddr\n");
371                return -ENODEV;
372        }
373
374found:
375        if (!request_region(vt596_smba, 8, "viapro-smbus")) {
376                dev_err(pdev, "SMBus region 0x%x already in use!\n",
377                        vt596_smba);
378                return -ENODEV;
379        }
380
381        pci_read_config_byte(pdev, SMBHSTCFG, &temp);
382        /* If force_addr is set, we program the new address here. Just to make
383           sure, we disable the VT596 first. */
384        if (force_addr) {
385                pci_write_config_byte(pdev, SMBHSTCFG, temp & 0xfe);
386                pci_write_config_word(pdev, id->driver_data, vt596_smba);
387                pci_write_config_byte(pdev, SMBHSTCFG, temp | 0x01);
388                dev_warn(pdev, "WARNING: SMBus interface set to new "
389                         "address 0x%04x!\n", vt596_smba);
390        } else if (!(temp & 0x01)) {
391                if (force) {
392                        /* NOTE: This assumes I/O space and other allocations
393                         * WERE done by the Bios!  Don't complain if your
394                         * hardware does weird things after enabling this.
395                         * :') Check for Bios updates before resorting to
396                         * this.
397                         */
398                        pci_write_config_byte(pdev, SMBHSTCFG, temp | 0x01);
399                        dev_info(pdev, "Enabling SMBus device\n");
400                } else {
401                        dev_err(pdev, "SMBUS: Error: Host SMBus "
402                                "controller not enabled! - upgrade BIOS or "
403                                "use force=1\n");
404                        goto release_region;
405                }
406        }
407
408        dev_dbg(pdev, "VT596_smba = 0x%X\n", vt596_smba);
409
410        switch (id->device) {
411        case PCI_DEVICE_ID_VIA_8237:
412        case PCI_DEVICE_ID_VIA_8235:
413        case PCI_DEVICE_ID_VIA_8233A:
414        case PCI_DEVICE_ID_VIA_8233_0:
415                vt596_features |= FEATURE_I2CBLOCK;
416                break;
417        case PCI_DEVICE_ID_VIA_82C686_4:
418                /* The VT82C686B (rev 0x40) does support I2C block
419                   transactions, but the VT82C686A (rev 0x30) doesn't */
420                if (!pci_read_config_byte(pdev, PCI_REVISION_ID, &temp)
421                 && temp >= 0x40)
422                        vt596_features |= FEATURE_I2CBLOCK;
423                break;
424        }
425
426        snprintf(vt596_adapter.name, 32,
427                 "SMBus Via Pro adapter at %04x", vt596_smba);
428
429        return i2c_add_adapter(&vt596_adapter);
430
431release_region:
432        release_region(vt596_smba, 8);
433        return error;
434}
435
436static struct pci_device_id vt596_ids[] __initdata = {
437        {
438                .vendor         = PCI_VENDOR_ID_VIA,
439                .device         = PCI_DEVICE_ID_VIA_82C596_3,
440                .subvendor      = PCI_ANY_ID,
441                .subdevice      = PCI_ANY_ID,
442                .driver_data    = SMBBA1,
443        },
444        {
445                .vendor         = PCI_VENDOR_ID_VIA,
446                .device         = PCI_DEVICE_ID_VIA_82C596B_3,
447                .subvendor      = PCI_ANY_ID,
448                .subdevice      = PCI_ANY_ID,
449                .driver_data    = SMBBA1,
450        },
451        {
452                .vendor         = PCI_VENDOR_ID_VIA,
453                .device         = PCI_DEVICE_ID_VIA_82C686_4,
454                .subvendor      = PCI_ANY_ID,
455                .subdevice      = PCI_ANY_ID,
456                .driver_data    = SMBBA1,
457        },
458        {
459                .vendor         = PCI_VENDOR_ID_VIA,
460                .device         = PCI_DEVICE_ID_VIA_8233_0,
461                .subvendor      = PCI_ANY_ID,
462                .subdevice      = PCI_ANY_ID,
463                .driver_data    = SMBBA3
464        },
465        {
466                .vendor         = PCI_VENDOR_ID_VIA,
467                .device         = PCI_DEVICE_ID_VIA_8233A,
468                .subvendor      = PCI_ANY_ID,
469                .subdevice      = PCI_ANY_ID,
470                .driver_data    = SMBBA3,
471        },
472        {
473                .vendor         = PCI_VENDOR_ID_VIA,
474                .device         = PCI_DEVICE_ID_VIA_8235,
475                .subvendor      = PCI_ANY_ID,
476                .subdevice      = PCI_ANY_ID,
477                .driver_data    = SMBBA3
478        },
479        {
480                .vendor         = PCI_VENDOR_ID_VIA,
481                .device         = PCI_DEVICE_ID_VIA_8237,
482                .subvendor      = PCI_ANY_ID,
483                .subdevice      = PCI_ANY_ID,
484                .driver_data    = SMBBA3
485        },
486        {
487                .vendor         = PCI_VENDOR_ID_VIA,
488                .device         = PCI_DEVICE_ID_VIA_8231_4,
489                .subvendor      = PCI_ANY_ID,
490                .subdevice      = PCI_ANY_ID,
491                .driver_data    = SMBBA1,
492        },
493        { 0, }
494};
495
496static int __init i2c_vt596_init(void)
497{
498        struct pci_dev *dev;
499        const struct pci_device_id *id;
500
501        printk("i2c-viapro.o version %s (%s)\n", LM_VERSION, LM_DATE);
502        pci_for_each_dev(dev) {
503                id = pci_match_device(vt596_ids, dev);
504                if(id)
505                        if(vt596_probe(dev, id) >= 0)
506                                return 0;
507        }
508        return -ENODEV;
509}
510
511
512static void __exit i2c_vt596_exit(void)
513{
514        i2c_del_adapter(&vt596_adapter);
515        release_region(vt596_smba, 8);
516}
517
518MODULE_AUTHOR("Kyosti Malkki <kmalkki@cc.hut.fi>, "
519              "Mark D. Studebaker <mdsxyz123@yahoo.com> and "
520              "Jean Delvare <khali@linux-fr.org>");
521MODULE_DESCRIPTION("vt82c596 SMBus driver");
522MODULE_LICENSE("GPL");
523
524module_init(i2c_vt596_init);
525module_exit(i2c_vt596_exit);
Note: See TracBrowser for help on using the browser.