root/lm-sensors/trunk/kernel/busses/i2c-sis630.c @ 1536

Revision 1536, 12.6 KB (checked in by amalysh, 11 years ago)

added sis730 as supported device
as of info from Philip Edelbrock <phil@…>
My Info:

pci device id 6300 is Intergrated VGA for sis630/730 chips
(see  http://www.plasma-online.de/index.html?content=http%3A//www.plasma-online.de/english/identify/picture/sis.html)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    SIS 630 SMBUS access implementation based on i2c-sis5595.
3
4    Status: beta
5
6    Copyright (c) 2002 Alexander Malysh <amalysh@web.de>
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   Changes:
25   24.08.2002
26        Fixed the typo in sis630_access (Thanks to Mark M. Hoffman)
27        Changed sis630_transaction. Now it's 2x faster (Thanks to Mark M. Hoffman)
28*/
29
30/*
31   TODO:
32     Implement block data write/read
33     Check SMBALT# : why it can't be cleared ????
34     Test on 2.2 kernel
35*/
36/*
37   Supports:
38        SIS 630
39
40   Note: we assume there can only be one device, with one SMBus interface.
41*/
42
43#include <linux/version.h>
44#include <linux/module.h>
45#include <asm/io.h>
46#include <linux/kernel.h>
47#include <linux/pci.h>
48#include <linux/stddef.h>
49#include <linux/sched.h>
50#include <linux/ioport.h>
51#include <linux/i2c.h>
52#include "version.h"
53#include <linux/init.h>
54
55#ifndef I2C_HW_SMBUS_SIS630
56#define I2C_HW_SMBUS_SIS630     0x08
57#endif
58
59#ifndef PCI_VENDOR_ID_SI
60#define PCI_VENDOR_ID_SI        0x1039
61#endif
62
63#ifndef PCI_DEVICE_ID_SI_630
64#define PCI_DEVICE_ID_SI_630    0x0630
65#endif
66
67#ifndef PCI_DEVICE_ID_SI_730
68#define PCI_DEVICE_ID_SI_730    0x0730
69#endif
70
71#ifndef PCI_DEVICE_ID_SI_503
72#define PCI_DEVICE_ID_SI_503    0x0008
73#endif
74
75struct sd {
76        const unsigned short mfr;
77        const unsigned short dev;
78        const unsigned char fn;
79        const char *name;
80};
81
82/* supported chips */
83static struct sd supported[] = {
84        {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_630, 0, "SIS630"},
85        {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_730, 0, "SIS730"},
86        {0, 0, 0, NULL}
87};
88
89
90/* SIS630 SMBus registers */
91#define SMB_STS     0x80 /* status */
92#define SMB_EN      0x81 /* status enable */
93#define SMB_CNT     0x82
94#define SMBHOST_CNT 0x83
95#define SMB_ADDR    0x84
96#define SMB_CMD     0x85
97#define SMB_PCOUNT  0x86 /* processed count */
98#define SMB_COUNT   0x87
99#define SMB_BYTE    0x88 /* ~0x8F data byte field */
100#define SMBDEV_ADDR 0x90
101#define SMB_DB0     0x91
102#define SMB_DB1     0x92
103#define SMB_SAA     0x93
104
105/* register count for request_region */
106#define SIS630_SMB_IOREGION 20
107
108/* PCI address constants */
109/* acpi base address register  */
110#define SIS630_ACPI_BASE_REG 0x74
111/* bios control register */
112#define SIS630_BIOS_CTL_REG  0x40
113
114/* Other settings */
115#define MAX_TIMEOUT   500
116
117/* SIS630 constants */
118#define SIS630_QUICK      0x00
119#define SIS630_BYTE       0x01
120#define SIS630_BYTE_DATA  0x02
121#define SIS630_WORD_DATA  0x03
122#define SIS630_PCALL      0x04
123#define SIS630_BLOCK_DATA 0x05
124
125/* insmod parameters */
126
127/* If force is set to anything different from 0, we forcibly enable the
128   SIS630. DANGEROUS! */
129static int force = 0;
130MODULE_PARM(force, "i");
131MODULE_PARM_DESC(force, "Forcibly enable the SIS630. DANGEROUS!");
132
133
134#ifdef MODULE
135static
136#else
137extern
138#endif
139int __init i2c_sis630_init(void);
140static int __init i2c_sis630_cleanup(void);
141static int sis630_setup(void);
142static s32 sis630_access(struct i2c_adapter *adap, u16 addr,
143                          unsigned short flags, char read_write,
144                          u8 command, int size,
145                          union i2c_smbus_data *data);
146static void sis630_do_pause(unsigned int amount);
147static int sis630_transaction(int size);
148static void sis630_inc(struct i2c_adapter *adapter);
149static void sis630_dec(struct i2c_adapter *adapter);
150static u32 sis630_func(struct i2c_adapter *adapter);
151static u8 sis630_read(u8 reg);
152static void sis630_write(u8 reg, u8 data);
153
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 */ sis630_access,
160        /* slave_send */ NULL,
161        /* slave_rcv */ NULL,
162        /* algo_control */ NULL,
163        /* functionality */ sis630_func,
164};
165
166static struct i2c_adapter sis630_adapter = {
167        "unset",
168        I2C_ALGO_SMBUS | I2C_HW_SMBUS_SIS630,
169        &smbus_algorithm,
170        NULL,
171        sis630_inc,
172        sis630_dec,
173        NULL,
174        NULL,
175};
176
177static unsigned short acpi_base = 0;
178static int __initdata sis630_initialized;
179
180u8 sis630_read(u8 reg) {
181        return inb(acpi_base + reg);
182}
183
184void sis630_write(u8 reg, u8 data) {
185        outb(data, acpi_base + reg);
186}
187
188/* Internally used pause function */
189void sis630_do_pause(unsigned int amount)
190{
191        current->state = TASK_INTERRUPTIBLE;
192        schedule_timeout(amount);
193}
194
195int sis630_transaction(int size) {
196        int temp;
197        int result = 0;
198        int timeout = 0;
199
200        /*
201          Make sure the SMBus host is ready to start transmitting.
202        */
203        if ((temp = sis630_read(SMB_CNT) & 0x03) != 0x00) {
204#ifdef DEBUG
205                printk(KERN_DEBUG "i2c-sis630.o: SMBus busy (%02x). "
206                        "Resetting...\n",temp);
207#endif
208                /* kill smbus transaction */
209                sis630_write(SMBHOST_CNT, 0x02);
210
211                if ((temp = sis630_read(SMB_CNT) & 0x03) != 0x00) {
212#ifdef DEBUG
213                        printk(KERN_DEBUG "i2c-sis630.o: Failed! (%02x)\n",
214                                temp);
215#endif
216                        return -1;
217                } else {
218#ifdef DEBUG
219                        printk(KERN_DEBUG "i2c-sis630.o: Successfull!\n");
220#endif
221                }
222        }
223
224        /* disable timeout interrupt , don't set Host Master Clock to 56KHz (it's hung my Laptop)*/
225        sis630_write(SMB_CNT, 0x00);
226
227        /* clear all sticky bits */
228        temp = sis630_read(SMB_STS);
229        sis630_write(SMB_STS, temp & 0x1e);
230
231        /* start the transaction by setting bit 4 and size */
232        sis630_write(SMBHOST_CNT,0x10 | (size & 0x07));
233
234        /* We will always wait for a fraction of a second! */
235        do {
236                sis630_do_pause(1);
237                temp = sis630_read(SMB_STS);
238        } while (!(temp & 0x0e) && (timeout++ < MAX_TIMEOUT));
239
240        /* If the SMBus is still busy, we give up */
241        if (timeout >= MAX_TIMEOUT) {
242#ifdef DEBUG
243                printk(KERN_DEBUG "i2c-sis630.o: SMBus Timeout!\n");
244#endif
245                result = -1;
246        }
247
248        if (temp & 0x02) {
249                result = -1;
250#ifdef DEBUG
251                printk(KERN_DEBUG "i2c-sis630.o: Error: Failed bus "
252                        "transaction\n");
253#endif
254        }
255
256        if (temp & 0x04) {
257                result = -1;
258                printk(KERN_ERR "i2c-sis630.o: Bus collision! "
259                        "SMBus may be locked until next hard reset (or not...)\n");
260                /* TBD: Datasheet say:
261                   the software should clear this bit and restart SMBUS operation
262                */
263        }
264
265        /* clear all status "sticky" bits */
266        sis630_write(SMB_STS, temp);
267
268        return result;
269}
270
271/* Return -1 on error. */
272s32 sis630_access(struct i2c_adapter * adap, u16 addr,
273                   unsigned short flags, char read_write,
274                   u8 command, int size, union i2c_smbus_data * data) {
275
276        switch (size) {
277                case I2C_SMBUS_QUICK:
278                        sis630_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
279                        size = SIS630_QUICK;
280                        break;
281                case I2C_SMBUS_BYTE:
282                        sis630_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
283                        if (read_write == I2C_SMBUS_WRITE)
284                                sis630_write(SMB_CMD, command);
285                        size = SIS630_BYTE;
286                        break;
287                case I2C_SMBUS_BYTE_DATA:
288                        sis630_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
289                        sis630_write(SMB_CMD, command);
290                        if (read_write == I2C_SMBUS_WRITE)
291                                sis630_write(SMB_BYTE, data->byte);
292                        size = SIS630_BYTE_DATA;
293                        break;
294                case I2C_SMBUS_PROC_CALL:
295                case I2C_SMBUS_WORD_DATA:
296                        sis630_write(SMB_ADDR,((addr & 0x7f) << 1) | (read_write & 0x01));
297                        sis630_write(SMB_CMD, command);
298                        if (read_write == I2C_SMBUS_WRITE) {
299                                sis630_write(SMB_BYTE, data->word & 0xff);
300                                sis630_write(SMB_BYTE + 1,(data->word & 0xff00) >> 8);
301                        }
302                        size = (size == I2C_SMBUS_PROC_CALL ? SIS630_PCALL : SIS630_WORD_DATA);
303                        break;
304                case I2C_SMBUS_BLOCK_DATA:
305/* it's not implemented yet, but comming soon */
306#if 0
307                        sis630_write(SMB_ADDR,((addr & 0x7f) << 1) | (read_write & 0x01));
308                        sis630_write(SMB_CMD, command);
309                        size = SIS630_BLOCK_DATA;
310#endif
311                default:
312                        printk("Unsupported I2C size\n");
313                        return -1;
314                        break;
315        }
316
317
318        if (sis630_transaction(size))
319                return -1;
320
321        if ((size != I2C_SMBUS_PROC_CALL) &&
322                ((read_write == I2C_SMBUS_WRITE) || (size == I2C_SMBUS_QUICK))) {
323                return 0;
324        }
325
326        switch(size) {
327                case SIS630_BYTE:
328                case SIS630_BYTE_DATA:
329                        data->byte = sis630_read(SMB_BYTE);
330                        break;
331                case SIS630_PCALL:
332                case SIS630_WORD_DATA:
333                        data->word = sis630_read(SMB_BYTE) + (sis630_read(SMB_BYTE + 1) << 8);
334                        break;
335                default:
336                        return -1;
337                        break;
338        }
339
340        return 0;
341}
342
343void sis630_inc(struct i2c_adapter *adapter) {
344        MOD_INC_USE_COUNT;
345}
346
347void sis630_dec(struct i2c_adapter *adapter) {
348        MOD_DEC_USE_COUNT;
349}
350
351u32 sis630_func(struct i2c_adapter *adapter) {
352        return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA |
353                I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_PROC_CALL;
354}
355
356int sis630_setup(void) {
357        unsigned char b;
358        struct pci_dev *sis630_dev = NULL,*tmp = NULL;
359        struct sd *en = supported;
360
361        /* First check whether we can access PCI at all */
362        if (pci_present() == 0) {
363                printk("i2c-sis630.o: Error: No PCI-bus found!\n");
364                return -ENODEV;
365        }
366
367        /* Look for the SIS630 and compatible */
368        if (!(sis630_dev = pci_find_device(PCI_VENDOR_ID_SI,
369                                            PCI_DEVICE_ID_SI_503,
370                                            sis630_dev))) {
371                printk(KERN_ERR "i2c-sis630.o: Error: Can't detect 85C503/5513 ISA bridge!\n");
372                return -ENODEV;
373        }
374        do {
375                if ((tmp = pci_find_device(en->mfr,en->dev,NULL))) {
376                        if (PCI_FUNC(tmp->devfn) != en->fn) 
377                                continue;
378
379                        break;
380                }
381                en++;
382        }
383        while(en->mfr);
384        if (tmp == NULL && force == 0 ) {
385                        printk(KERN_ERR "i2c-sis630.o: Error: Can't detect SIS630 compatible device!\n");
386                        return -ENODEV;
387        }
388        else if (tmp == NULL && force > 0) {
389                printk(KERN_NOTICE "i2c-sis630.o: WARNING: Can't detect SIS630 compatible device, but "
390                        "loading because of force option enabled\n");
391        }
392
393        /*
394           Enable ACPI first , so we can accsess reg 74-75
395           in acpi io space and read acpi base addr
396        */
397        if (PCIBIOS_SUCCESSFUL !=
398            pci_read_config_byte(sis630_dev, SIS630_BIOS_CTL_REG,&b)) {
399                printk(KERN_ERR "i2c-sis630.o: Error: Can't read bios ctl reg\n");
400                return -ENODEV;
401        }
402        /* if ACPI already anbled , do nothing */
403        if (!(b & 0x80) &&
404            PCIBIOS_SUCCESSFUL !=
405            pci_write_config_byte(sis630_dev,SIS630_BIOS_CTL_REG,b|0x80)) {
406                printk(KERN_ERR "i2c-sis630.o: Error: Can't enable ACPI\n");
407                return -ENODEV;
408        }
409        /* Determine the ACPI base address */
410        if (PCIBIOS_SUCCESSFUL !=
411            pci_read_config_word(sis630_dev,SIS630_ACPI_BASE_REG,&acpi_base)) {
412                printk(KERN_ERR "i2c-sis630.o: Error: Can't determine ACPI base address\n");
413                return -ENODEV;
414        }
415
416#ifdef DEBUG
417        printk(KERN_DEBUG "i2c-sis630.o: ACPI base at 0x%04x\n", acpi_base);
418#endif
419
420        /* Everything is happy, let's grab the memory and set things up. */
421        if (!request_region(acpi_base + SMB_STS, SIS630_SMB_IOREGION, "sis630-smbus")){
422                printk(KERN_ERR "i2c-sis630.o: SMBus registers 0x%04x-0x%04x "
423                        "already in use!\n",acpi_base + SMB_STS, acpi_base + SMB_SAA);
424                return -ENODEV;
425        }
426
427        return 0;
428}
429
430int __init i2c_sis630_init(void) {
431        int res;
432        printk("i2c-sis630.o version %s (%s)\n", LM_VERSION, LM_DATE);
433
434        if (sis630_initialized) {
435                printk(KERN_ERR "i2c-sis630.o: Oops, sis630_init called a second time!\n");
436                return -EBUSY;
437        }
438
439        sis630_initialized = 0;
440        if ((res = sis630_setup())) {
441                printk(KERN_ERR "i2c-sis630.o: SIS630 compatible bus not detected, module not inserted.\n");
442                i2c_sis630_cleanup();
443                return res;
444        }
445        sis630_initialized++;
446        sprintf(sis630_adapter.name, "SMBus SIS630 adapter at %04x",
447                acpi_base + SMB_STS);
448        if ((res = i2c_add_adapter(&sis630_adapter))) {
449                printk(KERN_ERR "i2c-sis630.o: Adapter registration failed, "
450                        "module not inserted.\n");
451                i2c_sis630_cleanup();
452                return res;
453        }
454        sis630_initialized++;
455        printk(KERN_INFO "i2c-sis630.o: SIS630 compatible bus detected and initialized\n");
456
457        return 0;
458}
459
460int __init i2c_sis630_cleanup(void) {
461        int res;
462        if (sis630_initialized >= 2) {
463                if ((res = i2c_del_adapter(&sis630_adapter))) {
464                        printk(KERN_ERR "i2c-sis630.o: i2c_del_adapter failed, module not"
465                                "removed\n");
466                        return res;
467                } else
468                        sis630_initialized--;
469        }
470        if (sis630_initialized >= 1) {
471                release_region(acpi_base + SMB_STS, SIS630_SMB_IOREGION);
472                sis630_initialized--;
473        }
474        return 0;
475}
476
477
478EXPORT_NO_SYMBOLS;
479
480#ifdef MODULE_LICENSE
481MODULE_LICENSE("GPL");
482#endif
483
484#ifdef MODULE
485MODULE_AUTHOR("Alexander Malysh <amalysh@web.de>");
486MODULE_DESCRIPTION("SIS630 SMBus driver");
487
488int init_module(void) {
489        return i2c_sis630_init();
490}
491
492int cleanup_module(void) {
493        return i2c_sis630_cleanup();
494}
495
496#endif  /* MODULE */
Note: See TracBrowser for help on using the browser.