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

Revision 1527, 11.8 KB (checked in by amalysh, 11 years ago)

don't set Host Master Clock to 56KHz (it's hung my Laptop)

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