2 sis96x.c - Part of lm_sensors, Linux kernel modules for hardware
5 Copyright (c) 2003 Mark M. Hoffman <mhoffman@lightlink.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 This module must be considered BETA unless and until
24 the chipset manufacturer releases a datasheet.
25 The register definitions are based on the SiS630.
27 This module relies on quirk_sis_96x_smbus (drivers/pci/quirks.c)
28 for just about every machine for which users have reported.
29 If this module isn't detecting your 96x south bridge, have a
32 We assume there can only be one SiS96x with one SMBus interface.
35 #include <linux/config.h>
36 #include <linux/module.h>
37 #include <linux/pci.h>
38 #include <linux/kernel.h>
39 #include <linux/stddef.h>
40 #include <linux/sched.h>
41 #include <linux/ioport.h>
42 #include <linux/i2c.h>
43 #include <linux/init.h>
48 2003-05-11 1.0.0 Updated from lm_sensors project for kernel 2.5
49 (was i2c-sis645.c from lm_sensors 2.7.0)
51 #define SIS96x_VERSION "1.0.0"
53 /* SiS96x SMBus PCI device ID */
54 #define PCI_DEVICE_ID_SI_SMBUS 0x16
56 /* base address register in PCI config space */
57 #define SIS96x_BAR 0x04
59 /* SiS96x SMBus registers */
63 #define SMB_HOST_CNT 0x03
66 #define SMB_PCOUNT 0x06
67 #define SMB_COUNT 0x07
69 #define SMB_DEV_ADDR 0x10
74 /* register count for request_region */
75 #define SMB_IOSIZE 0x20
78 #define MAX_TIMEOUT 500
80 /* SiS96x SMBus constants */
81 #define SIS96x_QUICK 0x00
82 #define SIS96x_BYTE 0x01
83 #define SIS96x_BYTE_DATA 0x02
84 #define SIS96x_WORD_DATA 0x03
85 #define SIS96x_PROC_CALL 0x04
86 #define SIS96x_BLOCK_DATA 0x05
88 static struct i2c_adapter sis96x_adapter;
89 static u16 sis96x_smbus_base = 0;
91 static inline u8 sis96x_read(u8 reg)
93 return inb(sis96x_smbus_base + reg) ;
96 static inline void sis96x_write(u8 reg, u8 data)
98 outb(data, sis96x_smbus_base + reg) ;
101 /* Execute a SMBus transaction.
102 int size is from SIS96x_QUICK to SIS96x_BLOCK_DATA
104 static int sis96x_transaction(int size)
110 dev_dbg(&sis96x_adapter.dev, "SMBus transaction %d\n", size);
112 /* Make sure the SMBus host is ready to start transmitting */
113 if (((temp = sis96x_read(SMB_CNT)) & 0x03) != 0x00) {
115 dev_dbg(&sis96x_adapter.dev, "SMBus busy (0x%02x). "
116 "Resetting...\n", temp);
118 /* kill the transaction */
119 sis96x_write(SMB_HOST_CNT, 0x20);
122 if (((temp = sis96x_read(SMB_CNT)) & 0x03) != 0x00) {
123 dev_dbg(&sis96x_adapter.dev, "Failed (0x%02x)\n", temp);
126 dev_dbg(&sis96x_adapter.dev, "Successful\n");
130 /* Turn off timeout interrupts, set fast host clock */
131 sis96x_write(SMB_CNT, 0x20);
133 /* clear all (sticky) status flags */
134 temp = sis96x_read(SMB_STS);
135 sis96x_write(SMB_STS, temp & 0x1e);
137 /* start the transaction by setting bit 4 and size bits */
138 sis96x_write(SMB_HOST_CNT, 0x10 | (size & 0x07));
140 /* We will always wait for a fraction of a second! */
143 temp = sis96x_read(SMB_STS);
144 } while (!(temp & 0x0e) && (timeout++ < MAX_TIMEOUT));
146 /* If the SMBus is still busy, we give up */
147 if (timeout >= MAX_TIMEOUT) {
148 dev_dbg(&sis96x_adapter.dev, "SMBus Timeout! (0x%02x)\n", temp);
152 /* device error - probably missing ACK */
154 dev_dbg(&sis96x_adapter.dev, "Failed bus transaction!\n");
160 dev_dbg(&sis96x_adapter.dev, "Bus collision!\n");
164 /* Finish up by resetting the bus */
165 sis96x_write(SMB_STS, temp);
166 if ((temp = sis96x_read(SMB_STS))) {
167 dev_dbg(&sis96x_adapter.dev, "Failed reset at "
168 "end of transaction! (0x%02x)\n", temp);
174 /* Return -1 on error. */
175 static s32 sis96x_access(struct i2c_adapter * adap, u16 addr,
176 unsigned short flags, char read_write,
177 u8 command, int size, union i2c_smbus_data * data)
181 case I2C_SMBUS_QUICK:
182 sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
187 sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
188 if (read_write == I2C_SMBUS_WRITE)
189 sis96x_write(SMB_CMD, command);
193 case I2C_SMBUS_BYTE_DATA:
194 sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
195 sis96x_write(SMB_CMD, command);
196 if (read_write == I2C_SMBUS_WRITE)
197 sis96x_write(SMB_BYTE, data->byte);
198 size = SIS96x_BYTE_DATA;
201 case I2C_SMBUS_PROC_CALL:
202 case I2C_SMBUS_WORD_DATA:
203 sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
204 sis96x_write(SMB_CMD, command);
205 if (read_write == I2C_SMBUS_WRITE) {
206 sis96x_write(SMB_BYTE, data->word & 0xff);
207 sis96x_write(SMB_BYTE + 1, (data->word & 0xff00) >> 8);
209 size = (size == I2C_SMBUS_PROC_CALL ?
210 SIS96x_PROC_CALL : SIS96x_WORD_DATA);
213 case I2C_SMBUS_BLOCK_DATA:
215 dev_info(&adap->dev, "SMBus block not implemented!\n");
220 dev_info(&adap->dev, "Unsupported I2C size\n");
225 if (sis96x_transaction(size))
228 if ((size != SIS96x_PROC_CALL) &&
229 ((read_write == I2C_SMBUS_WRITE) || (size == SIS96x_QUICK)))
234 case SIS96x_BYTE_DATA:
235 data->byte = sis96x_read(SMB_BYTE);
238 case SIS96x_WORD_DATA:
239 case SIS96x_PROC_CALL:
240 data->word = sis96x_read(SMB_BYTE) +
241 (sis96x_read(SMB_BYTE + 1) << 8);
247 static u32 sis96x_func(struct i2c_adapter *adapter)
249 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
250 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
251 I2C_FUNC_SMBUS_PROC_CALL;
254 static struct i2c_algorithm smbus_algorithm = {
255 .name = "Non-I2C SMBus adapter",
256 .id = I2C_ALGO_SMBUS,
257 .smbus_xfer = sis96x_access,
258 .functionality = sis96x_func,
261 static struct i2c_adapter sis96x_adapter = {
262 .owner = THIS_MODULE,
263 .class = I2C_ADAP_CLASS_SMBUS,
264 .algo = &smbus_algorithm,
268 static struct pci_device_id sis96x_ids[] = {
271 .vendor = PCI_VENDOR_ID_SI,
272 .device = PCI_DEVICE_ID_SI_SMBUS,
273 .subvendor = PCI_ANY_ID,
274 .subdevice = PCI_ANY_ID,
280 static int __devinit sis96x_probe(struct pci_dev *dev,
281 const struct pci_device_id *id)
286 if (sis96x_smbus_base) {
287 dev_err(&dev->dev, "Only one device supported.\n");
291 pci_read_config_word(dev, PCI_CLASS_DEVICE, &ww);
292 if (PCI_CLASS_SERIAL_SMBUS != ww) {
293 dev_err(&dev->dev, "Unsupported device class 0x%04x!\n", ww);
297 sis96x_smbus_base = pci_resource_start(dev, SIS96x_BAR);
298 if (!sis96x_smbus_base) {
299 dev_err(&dev->dev, "SiS96x SMBus base address "
300 "not initialized!\n");
303 dev_info(&dev->dev, "SiS96x SMBus base address: 0x%04x\n",
306 /* Everything is happy, let's grab the memory and set things up. */
307 if (!request_region(sis96x_smbus_base, SMB_IOSIZE, "sis96x-smbus")) {
308 dev_err(&dev->dev, "SMBus registers 0x%04x-0x%04x "
309 "already in use!\n", sis96x_smbus_base,
310 sis96x_smbus_base + SMB_IOSIZE - 1);
312 sis96x_smbus_base = 0;
316 /* set up the driverfs linkage to our parent device */
317 sis96x_adapter.dev.parent = &dev->dev;
319 snprintf(sis96x_adapter.name, I2C_NAME_SIZE,
320 "SiS96x SMBus adapter at 0x%04x", sis96x_smbus_base);
322 if ((retval = i2c_add_adapter(&sis96x_adapter))) {
323 dev_err(&dev->dev, "Couldn't register adapter!\n");
324 release_region(sis96x_smbus_base, SMB_IOSIZE);
325 sis96x_smbus_base = 0;
331 static void __devexit sis96x_remove(struct pci_dev *dev)
333 if (sis96x_smbus_base) {
334 i2c_del_adapter(&sis96x_adapter);
335 release_region(sis96x_smbus_base, SMB_IOSIZE);
336 sis96x_smbus_base = 0;
340 static struct pci_driver sis96x_driver = {
341 .name = "sis96x smbus",
342 .id_table = sis96x_ids,
343 .probe = sis96x_probe,
344 .remove = __devexit_p(sis96x_remove),
347 static int __init i2c_sis96x_init(void)
349 printk(KERN_INFO "i2c-sis96x version %s\n", SIS96x_VERSION);
350 return pci_module_init(&sis96x_driver);
353 static void __exit i2c_sis96x_exit(void)
355 pci_unregister_driver(&sis96x_driver);
358 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
359 MODULE_DESCRIPTION("SiS96x SMBus driver");
360 MODULE_LICENSE("GPL");
362 /* Register initialization functions using helper macros */
363 module_init(i2c_sis96x_init);
364 module_exit(i2c_sis96x_exit);