blob: 413e9e477723dfc53aca0674da678a7d7b096c32 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 sis96x.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4
5 Copyright (c) 2003 Mark M. Hoffman <mhoffman@lightlink.com>
6
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.
11
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.
16
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.
20*/
21
22/*
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.
26
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
30 look there.
31
32 We assume there can only be one SiS96x with one SMBus interface.
33*/
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
36#include <linux/pci.h>
37#include <linux/kernel.h>
38#include <linux/delay.h>
39#include <linux/stddef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/ioport.h>
41#include <linux/i2c.h>
42#include <linux/init.h>
43#include <asm/io.h>
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/* base address register in PCI config space */
46#define SIS96x_BAR 0x04
47
48/* SiS96x SMBus registers */
49#define SMB_STS 0x00
50#define SMB_EN 0x01
51#define SMB_CNT 0x02
52#define SMB_HOST_CNT 0x03
53#define SMB_ADDR 0x04
54#define SMB_CMD 0x05
55#define SMB_PCOUNT 0x06
56#define SMB_COUNT 0x07
57#define SMB_BYTE 0x08
58#define SMB_DEV_ADDR 0x10
59#define SMB_DB0 0x11
60#define SMB_DB1 0x12
61#define SMB_SAA 0x13
62
63/* register count for request_region */
64#define SMB_IOSIZE 0x20
65
66/* Other settings */
67#define MAX_TIMEOUT 500
68
69/* SiS96x SMBus constants */
70#define SIS96x_QUICK 0x00
71#define SIS96x_BYTE 0x01
72#define SIS96x_BYTE_DATA 0x02
73#define SIS96x_WORD_DATA 0x03
74#define SIS96x_PROC_CALL 0x04
75#define SIS96x_BLOCK_DATA 0x05
76
Jean Delvared6072f82005-09-25 16:37:04 +020077static struct pci_driver sis96x_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static struct i2c_adapter sis96x_adapter;
Jean Delvare60507092005-09-25 16:23:07 +020079static u16 sis96x_smbus_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81static inline u8 sis96x_read(u8 reg)
82{
83 return inb(sis96x_smbus_base + reg) ;
84}
85
86static inline void sis96x_write(u8 reg, u8 data)
87{
88 outb(data, sis96x_smbus_base + reg) ;
89}
90
91/* Execute a SMBus transaction.
92 int size is from SIS96x_QUICK to SIS96x_BLOCK_DATA
93 */
94static int sis96x_transaction(int size)
95{
96 int temp;
97 int result = 0;
98 int timeout = 0;
99
100 dev_dbg(&sis96x_adapter.dev, "SMBus transaction %d\n", size);
101
102 /* Make sure the SMBus host is ready to start transmitting */
103 if (((temp = sis96x_read(SMB_CNT)) & 0x03) != 0x00) {
104
105 dev_dbg(&sis96x_adapter.dev, "SMBus busy (0x%02x). "
106 "Resetting...\n", temp);
107
108 /* kill the transaction */
109 sis96x_write(SMB_HOST_CNT, 0x20);
110
111 /* check it again */
112 if (((temp = sis96x_read(SMB_CNT)) & 0x03) != 0x00) {
113 dev_dbg(&sis96x_adapter.dev, "Failed (0x%02x)\n", temp);
David Brownell97140342008-07-14 22:38:25 +0200114 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 } else {
116 dev_dbg(&sis96x_adapter.dev, "Successful\n");
117 }
118 }
119
120 /* Turn off timeout interrupts, set fast host clock */
121 sis96x_write(SMB_CNT, 0x20);
122
123 /* clear all (sticky) status flags */
124 temp = sis96x_read(SMB_STS);
125 sis96x_write(SMB_STS, temp & 0x1e);
126
127 /* start the transaction by setting bit 4 and size bits */
128 sis96x_write(SMB_HOST_CNT, 0x10 | (size & 0x07));
129
130 /* We will always wait for a fraction of a second! */
131 do {
132 msleep(1);
133 temp = sis96x_read(SMB_STS);
134 } while (!(temp & 0x0e) && (timeout++ < MAX_TIMEOUT));
135
136 /* If the SMBus is still busy, we give up */
137 if (timeout >= MAX_TIMEOUT) {
138 dev_dbg(&sis96x_adapter.dev, "SMBus Timeout! (0x%02x)\n", temp);
David Brownell97140342008-07-14 22:38:25 +0200139 result = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141
142 /* device error - probably missing ACK */
143 if (temp & 0x02) {
144 dev_dbg(&sis96x_adapter.dev, "Failed bus transaction!\n");
David Brownell97140342008-07-14 22:38:25 +0200145 result = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147
148 /* bus collision */
149 if (temp & 0x04) {
150 dev_dbg(&sis96x_adapter.dev, "Bus collision!\n");
David Brownell97140342008-07-14 22:38:25 +0200151 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153
154 /* Finish up by resetting the bus */
155 sis96x_write(SMB_STS, temp);
156 if ((temp = sis96x_read(SMB_STS))) {
157 dev_dbg(&sis96x_adapter.dev, "Failed reset at "
158 "end of transaction! (0x%02x)\n", temp);
159 }
160
161 return result;
162}
163
David Brownell97140342008-07-14 22:38:25 +0200164/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static s32 sis96x_access(struct i2c_adapter * adap, u16 addr,
166 unsigned short flags, char read_write,
167 u8 command, int size, union i2c_smbus_data * data)
168{
David Brownell97140342008-07-14 22:38:25 +0200169 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 switch (size) {
172 case I2C_SMBUS_QUICK:
173 sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
174 size = SIS96x_QUICK;
175 break;
176
177 case I2C_SMBUS_BYTE:
178 sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
179 if (read_write == I2C_SMBUS_WRITE)
180 sis96x_write(SMB_CMD, command);
181 size = SIS96x_BYTE;
182 break;
183
184 case I2C_SMBUS_BYTE_DATA:
185 sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
186 sis96x_write(SMB_CMD, command);
187 if (read_write == I2C_SMBUS_WRITE)
188 sis96x_write(SMB_BYTE, data->byte);
189 size = SIS96x_BYTE_DATA;
190 break;
191
192 case I2C_SMBUS_PROC_CALL:
193 case I2C_SMBUS_WORD_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->word & 0xff);
198 sis96x_write(SMB_BYTE + 1, (data->word & 0xff00) >> 8);
199 }
200 size = (size == I2C_SMBUS_PROC_CALL ?
201 SIS96x_PROC_CALL : SIS96x_WORD_DATA);
202 break;
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 default:
Jean Delvareac7fc4f2008-07-14 22:38:25 +0200205 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
David Brownell97140342008-07-14 22:38:25 +0200206 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
David Brownell97140342008-07-14 22:38:25 +0200209 status = sis96x_transaction(size);
210 if (status)
211 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 if ((size != SIS96x_PROC_CALL) &&
214 ((read_write == I2C_SMBUS_WRITE) || (size == SIS96x_QUICK)))
215 return 0;
216
217 switch (size) {
218 case SIS96x_BYTE:
219 case SIS96x_BYTE_DATA:
220 data->byte = sis96x_read(SMB_BYTE);
221 break;
222
223 case SIS96x_WORD_DATA:
224 case SIS96x_PROC_CALL:
225 data->word = sis96x_read(SMB_BYTE) +
226 (sis96x_read(SMB_BYTE + 1) << 8);
227 break;
228 }
229 return 0;
230}
231
232static u32 sis96x_func(struct i2c_adapter *adapter)
233{
234 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
235 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
236 I2C_FUNC_SMBUS_PROC_CALL;
237}
238
Jean Delvare8f9082c2006-09-03 22:39:46 +0200239static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 .smbus_xfer = sis96x_access,
241 .functionality = sis96x_func,
242};
243
244static struct i2c_adapter sis96x_adapter = {
245 .owner = THIS_MODULE,
Stephen Hemminger9ace5552007-02-13 22:09:01 +0100246 .id = I2C_HW_SMBUS_SIS96X,
Jean Delvare3401b2f2008-07-14 22:38:29 +0200247 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .algo = &smbus_algorithm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250
251static struct pci_device_id sis96x_ids[] = {
252 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_SMBUS) },
253 { 0, }
254};
255
256MODULE_DEVICE_TABLE (pci, sis96x_ids);
257
258static int __devinit sis96x_probe(struct pci_dev *dev,
259 const struct pci_device_id *id)
260{
261 u16 ww = 0;
262 int retval;
263
264 if (sis96x_smbus_base) {
265 dev_err(&dev->dev, "Only one device supported.\n");
266 return -EBUSY;
267 }
268
269 pci_read_config_word(dev, PCI_CLASS_DEVICE, &ww);
270 if (PCI_CLASS_SERIAL_SMBUS != ww) {
271 dev_err(&dev->dev, "Unsupported device class 0x%04x!\n", ww);
272 return -ENODEV;
273 }
274
275 sis96x_smbus_base = pci_resource_start(dev, SIS96x_BAR);
276 if (!sis96x_smbus_base) {
277 dev_err(&dev->dev, "SiS96x SMBus base address "
278 "not initialized!\n");
279 return -EINVAL;
280 }
281 dev_info(&dev->dev, "SiS96x SMBus base address: 0x%04x\n",
282 sis96x_smbus_base);
283
284 /* Everything is happy, let's grab the memory and set things up. */
Jean Delvared6072f82005-09-25 16:37:04 +0200285 if (!request_region(sis96x_smbus_base, SMB_IOSIZE,
286 sis96x_driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 dev_err(&dev->dev, "SMBus registers 0x%04x-0x%04x "
288 "already in use!\n", sis96x_smbus_base,
289 sis96x_smbus_base + SMB_IOSIZE - 1);
290
291 sis96x_smbus_base = 0;
292 return -EINVAL;
293 }
294
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100295 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 sis96x_adapter.dev.parent = &dev->dev;
297
David Brownell2096b952007-05-01 23:26:28 +0200298 snprintf(sis96x_adapter.name, sizeof(sis96x_adapter.name),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 "SiS96x SMBus adapter at 0x%04x", sis96x_smbus_base);
300
301 if ((retval = i2c_add_adapter(&sis96x_adapter))) {
302 dev_err(&dev->dev, "Couldn't register adapter!\n");
303 release_region(sis96x_smbus_base, SMB_IOSIZE);
304 sis96x_smbus_base = 0;
305 }
306
307 return retval;
308}
309
310static void __devexit sis96x_remove(struct pci_dev *dev)
311{
312 if (sis96x_smbus_base) {
313 i2c_del_adapter(&sis96x_adapter);
314 release_region(sis96x_smbus_base, SMB_IOSIZE);
315 sis96x_smbus_base = 0;
316 }
317}
318
319static struct pci_driver sis96x_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 .name = "sis96x_smbus",
321 .id_table = sis96x_ids,
322 .probe = sis96x_probe,
323 .remove = __devexit_p(sis96x_remove),
324};
325
326static int __init i2c_sis96x_init(void)
327{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return pci_register_driver(&sis96x_driver);
329}
330
331static void __exit i2c_sis96x_exit(void)
332{
333 pci_unregister_driver(&sis96x_driver);
334}
335
336MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
337MODULE_DESCRIPTION("SiS96x SMBus driver");
338MODULE_LICENSE("GPL");
339
340/* Register initialization functions using helper macros */
341module_init(i2c_sis96x_init);
342module_exit(i2c_sis96x_exit);
343