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

Revision 3141, 13.3 KB (checked in by khali, 8 years ago)

Update the list of supported chips, stating which ones support
I2C block reads, and which don't. This is a backport from Linux 2.6.

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