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

Revision 1198, 14.3 KB (checked in by mds, 12 years ago)

fix detection thinko broken yesterday - thanks martin

  • 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 - 2001  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
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   Supports Via devices:
25        82C596A/B (0x3050)
26        82C596B (0x3051)
27        82C686A/B
28        8233
29   Note: we assume there can only be one device, with one SMBus interface.
30*/
31
32#include <linux/version.h>
33#include <linux/module.h>
34#include <linux/pci.h>
35#include <asm/io.h>
36#include <linux/kernel.h>
37#include <linux/stddef.h>
38#include <linux/sched.h>
39#include <linux/ioport.h>
40#include <linux/i2c.h>
41#include "version.h"
42
43#include <linux/init.h>
44
45#ifndef PCI_DEVICE_ID_VIA_82C596_3
46#define PCI_DEVICE_ID_VIA_82C596_3      0x3050
47#endif
48#ifndef PCI_DEVICE_ID_VIA_82C596B_3
49#define PCI_DEVICE_ID_VIA_82C596B_3     0x3051
50#endif
51#ifndef PCI_DEVICE_ID_VIA_82C686_4
52#define PCI_DEVICE_ID_VIA_82C686_4      0x3057
53#endif
54#ifndef PCI_DEVICE_ID_VIA_8233_0
55#define PCI_DEVICE_ID_VIA_8233_0        0x3074
56#endif
57
58#define SMBBA1      0x90
59#define SMBBA2      0x80
60#define SMBBA3      0xD0
61
62struct sd {
63        const unsigned short dev;
64        const unsigned char base;
65        const unsigned char hstcfg;
66        const char *name;
67};
68
69static struct sd supported[] = {
70        {PCI_DEVICE_ID_VIA_82C596_3, SMBBA1, 0xD2, "VT82C596A/B"},
71        {PCI_DEVICE_ID_VIA_82C596B_3, SMBBA1, 0xD2, "VT82C596B"},
72        {PCI_DEVICE_ID_VIA_82C686_4, SMBBA1, 0xD2, "VT82C686A/B"},
73        {PCI_DEVICE_ID_VIA_8233_0, SMBBA3, 0xD2, "VT8233"},
74        {0, 0, 0, NULL}
75};
76
77static struct sd *num = supported;
78
79/* SMBus address offsets */
80#define SMBHSTSTS (0 + vt596_smba)
81#define SMBHSLVSTS (1 + vt596_smba)
82#define SMBHSTCNT (2 + vt596_smba)
83#define SMBHSTCMD (3 + vt596_smba)
84#define SMBHSTADD (4 + vt596_smba)
85#define SMBHSTDAT0 (5 + vt596_smba)
86#define SMBHSTDAT1 (6 + vt596_smba)
87#define SMBBLKDAT (7 + vt596_smba)
88#define SMBSLVCNT (8 + vt596_smba)
89#define SMBSHDWCMD (9 + vt596_smba)
90#define SMBSLVEVT (0xA + vt596_smba)
91#define SMBSLVDAT (0xC + vt596_smba)
92
93/* PCI Address Constants */
94
95/* SMBus data in configuration space can be found in two places,
96   We try to select the better one*/
97
98static unsigned short smb_cf_hstcfg;
99
100#define SMBHSTCFG   (smb_cf_hstcfg)
101#define SMBSLVC     (SMBHSTCFG+1)
102#define SMBSHDW1    (SMBHSTCFG+2)
103#define SMBSHDW2    (SMBHSTCFG+3)
104#define SMBREV      (SMBHSTCFG+4)
105
106/* Other settings */
107#define MAX_TIMEOUT 500
108#define  ENABLE_INT9 0
109
110/* VT82C596 constants */
111#define VT596_QUICK      0x00
112#define VT596_BYTE       0x04
113#define VT596_BYTE_DATA  0x08
114#define VT596_WORD_DATA  0x0C
115#define VT596_BLOCK_DATA 0x14
116
117/* insmod parameters */
118
119/* If force is set to anything different from 0, we forcibly enable the
120   VT596. DANGEROUS! */
121static int force = 0;
122MODULE_PARM(force, "i");
123MODULE_PARM_DESC(force, "Forcibly enable the SMBus. DANGEROUS!");
124
125/* If force_addr is set to anything different from 0, we forcibly enable
126   the VT596 at the given address. VERY DANGEROUS! */
127static int force_addr = 0;
128MODULE_PARM(force_addr, "i");
129MODULE_PARM_DESC(force_addr,
130                 "Forcibly enable the SMBus at the given address. "
131                 "EXTREMELY DANGEROUS!");
132
133#ifdef MODULE
134static
135#else
136extern
137#endif
138int __init i2c_vt596_init(void);
139static int __init vt596_cleanup(void);
140static int vt596_setup(void);
141static s32 vt596_access(struct i2c_adapter *adap, u16 addr,
142                        unsigned short flags, char read_write,
143                        u8 command, int size, union i2c_smbus_data *data);
144static void vt596_do_pause(unsigned int amount);
145static int vt596_transaction(void);
146static void vt596_inc(struct i2c_adapter *adapter);
147static void vt596_dec(struct i2c_adapter *adapter);
148static u32 vt596_func(struct i2c_adapter *adapter);
149
150#ifdef MODULE
151extern int init_module(void);
152extern int cleanup_module(void);
153#endif                          /* MODULE */
154
155static struct i2c_algorithm smbus_algorithm = {
156        /* name */ "Non-I2C SMBus adapter",
157        /* id */ I2C_ALGO_SMBUS,
158        /* master_xfer */ NULL,
159        /* smbus_access */ vt596_access,
160        /* slave_send */ NULL,
161        /* slave_rcv */ NULL,
162        /* algo_control */ NULL,
163        /* functionality */ vt596_func,
164};
165
166static struct i2c_adapter vt596_adapter = {
167        "unset",
168        I2C_ALGO_SMBUS | I2C_HW_SMBUS_VIA2,
169        &smbus_algorithm,
170        NULL,
171        vt596_inc,
172        vt596_dec,
173        NULL,
174        NULL,
175};
176
177static int __initdata vt596_initialized;
178static unsigned short vt596_smba = 0;
179
180
181/* Detect whether a compatible device can be found, and initialize it. */
182int vt596_setup(void)
183{
184        unsigned char temp;
185
186        struct pci_dev *VT596_dev = NULL;
187
188        /* First check whether we can access PCI at all */
189        if (pci_present() == 0)
190                return(-ENODEV);
191
192        /* Look for a supported device/function */
193        do {
194                if((VT596_dev = pci_find_device(PCI_VENDOR_ID_VIA, num->dev,
195                                                VT596_dev)))
196                        break;
197        } while ((++num)->dev);
198
199        if (VT596_dev == NULL)
200                return(-ENODEV);
201        printk("i2c-viapro.o: Found Via %s device\n", num->name);
202
203/* Determine the address of the SMBus areas */
204        smb_cf_hstcfg = num->hstcfg;
205        if (force_addr) {
206                vt596_smba = force_addr;
207                force = 0;
208        } else {
209                if ((pci_read_config_word(VT596_dev, num->base, &vt596_smba))
210                    || !(vt596_smba & 0x1)) {
211                        /* try 2nd address and config reg. for 596 */
212                        if((num->dev == PCI_DEVICE_ID_VIA_82C596_3) &&
213                           (!pci_read_config_word(VT596_dev, SMBBA2, &vt596_smba)) &&
214                           (vt596_smba & 0x1)) {
215                                smb_cf_hstcfg = 0x84;
216                        } else {
217                                /* no matches at all */
218                                printk("i2c-viapro.o: Cannot configure SMBus "
219                                       "I/O Base address\n");
220                                return(-ENODEV);
221                        }
222                }
223        }
224        vt596_smba &= 0xfff0;
225
226        if (check_region(vt596_smba, 8)) {
227                printk("i2c-viapro.o: SMB region 0x%x already in use!\n",
228                        vt596_smba);
229                return(-ENODEV);
230        }
231
232        pci_read_config_byte(VT596_dev, SMBHSTCFG, &temp);
233/* If force_addr is set, we program the new address here. Just to make
234   sure, we disable the VT596 first. */
235        if (force_addr) {
236                pci_write_config_byte(VT596_dev, SMBHSTCFG, temp & 0xfe);
237                pci_write_config_word(VT596_dev, num->base, vt596_smba);
238                pci_write_config_byte(VT596_dev, SMBHSTCFG, temp | 0x01);
239                printk
240                    ("i2c-viapro.o: WARNING: SMBus interface set to new "
241                     "address 0x%04x!\n", vt596_smba);
242        } else if ((temp & 1) == 0) {
243                if (force) {
244/* NOTE: This assumes I/O space and other allocations WERE
245   done by the Bios!  Don't complain if your hardware does weird
246   things after enabling this. :') Check for Bios updates before
247   resorting to this.  */
248                        pci_write_config_byte(VT596_dev, SMBHSTCFG,
249                                              temp | 1);
250                        printk
251                            ("i2c-viapro.o: WARNING: SMBus interface has been FORCEFULLY "
252                             "ENABLED!\n");
253                } else {
254                        printk
255                            ("SMBUS: Error: Host SMBus controller not enabled!\n");
256                        return(-ENODEV);
257                }
258        }
259
260        /* Everything is happy, let's grab the memory and set things up. */
261        request_region(vt596_smba, 8, "via2-smbus");
262
263#ifdef DEBUG
264        if ((temp & 0x0E) == 8)
265                printk("i2c-viapro.o: using Interrupt 9 for SMBus.\n");
266        else if ((temp & 0x0E) == 0)
267                printk("i2c-viapro.o: using Interrupt SMI# for SMBus.\n");
268        else
269                printk
270                    ("i2c-viapro.o: Illegal Interrupt configuration (or code out "
271                     "of date)!\n");
272
273        pci_read_config_byte(VT596_dev, SMBREV, &temp);
274        printk("i2c-viapro.o: SMBREV = 0x%X\n", temp);
275        printk("i2c-viapro.o: VT596_smba = 0x%X\n", vt596_smba);
276#endif                          /* DEBUG */
277
278        return(0);
279}
280
281
282/* Internally used pause function */
283void vt596_do_pause(unsigned int amount)
284{
285        current->state = TASK_INTERRUPTIBLE;
286        schedule_timeout(amount);
287}
288
289/* Another internally used function */
290int vt596_transaction(void)
291{
292        int temp;
293        int result = 0;
294        int timeout = 0;
295
296#ifdef DEBUG
297        printk
298            ("i2c-viapro.o: Transaction (pre): CNT=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, "
299             "DAT1=%02x\n", inb_p(SMBHSTCNT), inb_p(SMBHSTCMD),
300             inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
301#endif
302
303        /* Make sure the SMBus host is ready to start transmitting */
304        if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
305#ifdef DEBUG
306                printk("i2c-viapro.o: SMBus busy (0x%02x). Resetting... \n",
307                       temp);
308#endif
309                outb_p(temp, SMBHSTSTS);
310                if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
311#ifdef DEBUG
312                        printk("i2c-viapro.o: Failed! (0x%02x)\n", temp);
313#endif
314                        return -1;
315                } else {
316#ifdef DEBUG
317                        printk("i2c-viapro.o: Successfull!\n");
318#endif
319                }
320        }
321
322        /* start the transaction by setting bit 6 */
323        outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
324
325        /* We will always wait for a fraction of a second!
326           I don't know if VIA needs this, Intel did  */
327        do {
328                vt596_do_pause(1);
329                temp = inb_p(SMBHSTSTS);
330        } while ((temp & 0x01) && (timeout++ < MAX_TIMEOUT));
331
332        /* If the SMBus is still busy, we give up */
333        if (timeout >= MAX_TIMEOUT) {
334#ifdef DEBUG
335                printk("i2c-viapro.o: SMBus Timeout!\n");
336                result = -1;
337#endif
338        }
339
340        if (temp & 0x10) {
341                result = -1;
342#ifdef DEBUG
343                printk("i2c-viapro.o: Error: Failed bus transaction\n");
344#endif
345        }
346
347        if (temp & 0x08) {
348                result = -1;
349                printk
350                    ("i2c-viapro.o: Bus collision! SMBus may be locked until next hard\n"
351                     "reset. (sorry!)\n");
352                /* Clock stops and slave is stuck in mid-transmission */
353        }
354
355        if (temp & 0x04) {
356                result = -1;
357#ifdef DEBUG
358                printk("i2c-viapro.o: Error: no response!\n");
359#endif
360        }
361
362        if (inb_p(SMBHSTSTS) != 0x00)
363                outb_p(inb(SMBHSTSTS), SMBHSTSTS);
364
365        if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
366#ifdef DEBUG
367                printk
368                    ("i2c-viapro.o: Failed reset at end of transaction (%02x)\n",
369                     temp);
370#endif
371        }
372#ifdef DEBUG
373        printk
374            ("i2c-viapro.o: Transaction (post): CNT=%02x, CMD=%02x, ADD=%02x, "
375             "DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT), inb_p(SMBHSTCMD),
376             inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
377#endif
378        return result;
379}
380
381/* Return -1 on error. See smbus.h for more information */
382s32 vt596_access(struct i2c_adapter * adap, u16 addr, unsigned short flags,
383                 char read_write,
384                 u8 command, int size, union i2c_smbus_data * data)
385{
386        int i, len;
387
388        switch (size) {
389        case I2C_SMBUS_PROC_CALL:
390                printk
391                    ("i2c-viapro.o: I2C_SMBUS_PROC_CALL not supported!\n");
392                return -1;
393        case I2C_SMBUS_QUICK:
394                outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
395                       SMBHSTADD);
396                size = VT596_QUICK;
397                break;
398        case I2C_SMBUS_BYTE:
399                outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
400                       SMBHSTADD);
401                if (read_write == I2C_SMBUS_WRITE)
402                        outb_p(command, SMBHSTCMD);
403                size = VT596_BYTE;
404                break;
405        case I2C_SMBUS_BYTE_DATA:
406                outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
407                       SMBHSTADD);
408                outb_p(command, SMBHSTCMD);
409                if (read_write == I2C_SMBUS_WRITE)
410                        outb_p(data->byte, SMBHSTDAT0);
411                size = VT596_BYTE_DATA;
412                break;
413        case I2C_SMBUS_WORD_DATA:
414                outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
415                       SMBHSTADD);
416                outb_p(command, SMBHSTCMD);
417                if (read_write == I2C_SMBUS_WRITE) {
418                        outb_p(data->word & 0xff, SMBHSTDAT0);
419                        outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
420                }
421                size = VT596_WORD_DATA;
422                break;
423        case I2C_SMBUS_BLOCK_DATA:
424                outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
425                       SMBHSTADD);
426                outb_p(command, SMBHSTCMD);
427                if (read_write == I2C_SMBUS_WRITE) {
428                        len = data->block[0];
429                        if (len < 0)
430                                len = 0;
431                        if (len > 32)
432                                len = 32;
433                        outb_p(len, SMBHSTDAT0);
434                        i = inb_p(SMBHSTCNT);   /* Reset SMBBLKDAT */
435                        for (i = 1; i <= len; i++)
436                                outb_p(data->block[i], SMBBLKDAT);
437                }
438                size = VT596_BLOCK_DATA;
439                break;
440        }
441
442        outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
443
444        if (vt596_transaction())        /* Error in transaction */
445                return -1;
446
447        if ((read_write == I2C_SMBUS_WRITE) || (size == VT596_QUICK))
448                return 0;
449
450
451        switch (size) {
452        case VT596_BYTE:        /* Where is the result put? I assume here it is in
453                                   SMBHSTDAT0 but it might just as well be in the
454                                   SMBHSTCMD. No clue in the docs */
455
456                data->byte = inb_p(SMBHSTDAT0);
457                break;
458        case VT596_BYTE_DATA:
459                data->byte = inb_p(SMBHSTDAT0);
460                break;
461        case VT596_WORD_DATA:
462                data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
463                break;
464        case VT596_BLOCK_DATA:
465                data->block[0] = inb_p(SMBHSTDAT0);
466                i = inb_p(SMBHSTCNT);   /* Reset SMBBLKDAT */
467                for (i = 1; i <= data->block[0]; i++)
468                        data->block[i] = inb_p(SMBBLKDAT);
469                break;
470        }
471        return 0;
472}
473
474void vt596_inc(struct i2c_adapter *adapter)
475{
476        MOD_INC_USE_COUNT;
477}
478
479void vt596_dec(struct i2c_adapter *adapter)
480{
481
482        MOD_DEC_USE_COUNT;
483}
484
485u32 vt596_func(struct i2c_adapter *adapter)
486{
487        return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
488            I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
489            I2C_FUNC_SMBUS_BLOCK_DATA;
490}
491
492int __init i2c_vt596_init(void)
493{
494        int res;
495        printk("i2c-viapro.o version %s (%s)\n", LM_VERSION, LM_DATE);
496#ifdef DEBUG
497/* PE- It might be good to make this a permanent part of the code! */
498        if (vt596_initialized) {
499                printk
500                    ("i2c-viapro.o: Oops, vt596_init called a second time!\n");
501                return -EBUSY;
502        }
503#endif
504        vt596_initialized = 0;
505        if ((res = vt596_setup())) {
506                printk
507                    ("i2c-viapro.o: Can't detect vt82c596 or compatible device, module not inserted.\n");
508                vt596_cleanup();
509                return res;
510        }
511        vt596_initialized++;
512        sprintf(vt596_adapter.name, "SMBus Via Pro adapter at %04x",
513                vt596_smba);
514        if ((res = i2c_add_adapter(&vt596_adapter))) {
515                printk
516                    ("i2c-viapro.o: Adapter registration failed, module not inserted.\n");
517                vt596_cleanup();
518                return res;
519        }
520        vt596_initialized++;
521        printk("i2c-viapro.o: Via Pro SMBus detected and initialized\n");
522        return 0;
523}
524
525int __init vt596_cleanup(void)
526{
527        int res;
528        if (vt596_initialized >= 2) {
529                if ((res = i2c_del_adapter(&vt596_adapter))) {
530                        printk
531                            ("i2c-viapro.o: i2c_del_adapter failed, module not removed\n");
532                        return res;
533                } else
534                        vt596_initialized--;
535        }
536        if (vt596_initialized >= 1) {
537                release_region(vt596_smba, 8);
538                vt596_initialized--;
539        }
540        return 0;
541}
542
543EXPORT_NO_SYMBOLS;
544
545#ifdef MODULE
546
547MODULE_AUTHOR
548    ("Frodo Looijaard <frodol@dds.nl> and Philip Edelbrock <phil@netroedge.com>");
549MODULE_DESCRIPTION("vt82c596 SMBus driver");
550
551
552int init_module(void)
553{
554        return i2c_vt596_init();
555}
556
557int cleanup_module(void)
558{
559        return vt596_cleanup();
560}
561
562#endif                          /* MODULE */
Note: See TracBrowser for help on using the browser.