blob: 283769cecee297531fd80c66e1cc34f1ee1bbf64 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 sis5595.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.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/* Note: we assume there can only be one SIS5595 with one SMBus interface */
23
24/*
25 Note: all have mfr. ID 0x1039.
26 SUPPORTED PCI ID
27 5595 0008
28
29 Note: these chips contain a 0008 device which is incompatible with the
30 5595. We recognize these by the presence of the listed
31 "blacklist" PCI ID and refuse to load.
32
33 NOT SUPPORTED PCI ID BLACKLIST PCI ID
34 540 0008 0540
35 550 0008 0550
36 5513 0008 5511
37 5581 0008 5597
38 5582 0008 5597
39 5597 0008 5597
40 5598 0008 5597/5598
41 630 0008 0630
42 645 0008 0645
43 646 0008 0646
44 648 0008 0648
45 650 0008 0650
46 651 0008 0651
47 730 0008 0730
48 735 0008 0735
49 745 0008 0745
50 746 0008 0746
51*/
52
53/* TO DO:
54 * Add Block Transfers (ugly, but supported by the adapter)
55 * Add adapter resets
56 */
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/kernel.h>
59#include <linux/module.h>
60#include <linux/delay.h>
61#include <linux/pci.h>
62#include <linux/ioport.h>
63#include <linux/init.h>
64#include <linux/i2c.h>
65#include <asm/io.h>
66
67static int blacklist[] = {
68 PCI_DEVICE_ID_SI_540,
69 PCI_DEVICE_ID_SI_550,
70 PCI_DEVICE_ID_SI_630,
71 PCI_DEVICE_ID_SI_645,
72 PCI_DEVICE_ID_SI_646,
73 PCI_DEVICE_ID_SI_648,
74 PCI_DEVICE_ID_SI_650,
75 PCI_DEVICE_ID_SI_651,
76 PCI_DEVICE_ID_SI_730,
77 PCI_DEVICE_ID_SI_735,
78 PCI_DEVICE_ID_SI_745,
79 PCI_DEVICE_ID_SI_746,
80 PCI_DEVICE_ID_SI_5511, /* 5513 chip has the 0008 device but that ID
81 shows up in other chips so we use the 5511
82 ID for recognition */
83 PCI_DEVICE_ID_SI_5597,
84 PCI_DEVICE_ID_SI_5598,
85 0, /* terminates the list */
86};
87
88/* Length of ISA address segment */
89#define SIS5595_EXTENT 8
90/* SIS5595 SMBus registers */
91#define SMB_STS_LO 0x00
92#define SMB_STS_HI 0x01
93#define SMB_CTL_LO 0x02
94#define SMB_CTL_HI 0x03
95#define SMB_ADDR 0x04
96#define SMB_CMD 0x05
97#define SMB_PCNT 0x06
98#define SMB_CNT 0x07
99#define SMB_BYTE 0x08
100#define SMB_DEV 0x10
101#define SMB_DB0 0x11
102#define SMB_DB1 0x12
103#define SMB_HAA 0x13
104
105/* PCI Address Constants */
106#define SMB_INDEX 0x38
107#define SMB_DAT 0x39
108#define SIS5595_ENABLE_REG 0x40
109#define ACPI_BASE 0x90
110
111/* Other settings */
112#define MAX_TIMEOUT 500
113
114/* SIS5595 constants */
115#define SIS5595_QUICK 0x00
116#define SIS5595_BYTE 0x02
117#define SIS5595_BYTE_DATA 0x04
118#define SIS5595_WORD_DATA 0x06
119#define SIS5595_PROC_CALL 0x08
120#define SIS5595_BLOCK_DATA 0x0A
121
122/* insmod parameters */
123
124/* If force_addr is set to anything different from 0, we forcibly enable
125 the device at the given address. */
Jean Delvare60507092005-09-25 16:23:07 +0200126static u16 force_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127module_param(force_addr, ushort, 0);
128MODULE_PARM_DESC(force_addr, "Initialize the base address of the i2c controller");
129
Jean Delvared6072f82005-09-25 16:37:04 +0200130static struct pci_driver sis5595_driver;
Jean Delvare60507092005-09-25 16:23:07 +0200131static unsigned short sis5595_base;
Jean Delvare7375cd82007-07-12 14:12:30 +0200132static struct pci_dev *sis5595_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134static u8 sis5595_read(u8 reg)
135{
136 outb(reg, sis5595_base + SMB_INDEX);
137 return inb(sis5595_base + SMB_DAT);
138}
139
140static void sis5595_write(u8 reg, u8 data)
141{
142 outb(reg, sis5595_base + SMB_INDEX);
143 outb(data, sis5595_base + SMB_DAT);
144}
145
146static int sis5595_setup(struct pci_dev *SIS5595_dev)
147{
148 u16 a;
149 u8 val;
150 int *i;
151 int retval = -ENODEV;
152
153 /* Look for imposters */
154 for (i = blacklist; *i != 0; i++) {
155 struct pci_dev *dev;
156 dev = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
157 if (dev) {
158 dev_err(&SIS5595_dev->dev, "Looked for SIS5595 but found unsupported device %.4x\n", *i);
159 pci_dev_put(dev);
160 return -ENODEV;
161 }
162 }
163
164 /* Determine the address of the SMBus areas */
165 pci_read_config_word(SIS5595_dev, ACPI_BASE, &sis5595_base);
166 if (sis5595_base == 0 && force_addr == 0) {
167 dev_err(&SIS5595_dev->dev, "ACPI base address uninitialized - upgrade BIOS or use force_addr=0xaddr\n");
168 return -ENODEV;
169 }
170
171 if (force_addr)
172 sis5595_base = force_addr & ~(SIS5595_EXTENT - 1);
173 dev_dbg(&SIS5595_dev->dev, "ACPI Base address: %04x\n", sis5595_base);
174
175 /* NB: We grab just the two SMBus registers here, but this may still
176 * interfere with ACPI :-( */
Jean Delvared6072f82005-09-25 16:37:04 +0200177 if (!request_region(sis5595_base + SMB_INDEX, 2,
178 sis5595_driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 dev_err(&SIS5595_dev->dev, "SMBus registers 0x%04x-0x%04x already in use!\n",
180 sis5595_base + SMB_INDEX, sis5595_base + SMB_INDEX + 1);
181 return -ENODEV;
182 }
183
184 if (force_addr) {
185 dev_info(&SIS5595_dev->dev, "forcing ISA address 0x%04X\n", sis5595_base);
186 if (pci_write_config_word(SIS5595_dev, ACPI_BASE, sis5595_base)
187 != PCIBIOS_SUCCESSFUL)
188 goto error;
189 if (pci_read_config_word(SIS5595_dev, ACPI_BASE, &a)
190 != PCIBIOS_SUCCESSFUL)
191 goto error;
192 if ((a & ~(SIS5595_EXTENT - 1)) != sis5595_base) {
193 /* doesn't work for some chips! */
194 dev_err(&SIS5595_dev->dev, "force address failed - not supported?\n");
195 goto error;
196 }
197 }
198
199 if (pci_read_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, &val)
200 != PCIBIOS_SUCCESSFUL)
201 goto error;
202 if ((val & 0x80) == 0) {
203 dev_info(&SIS5595_dev->dev, "enabling ACPI\n");
204 if (pci_write_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, val | 0x80)
205 != PCIBIOS_SUCCESSFUL)
206 goto error;
207 if (pci_read_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, &val)
208 != PCIBIOS_SUCCESSFUL)
209 goto error;
210 if ((val & 0x80) == 0) {
211 /* doesn't work for some chips? */
212 dev_err(&SIS5595_dev->dev, "ACPI enable failed - not supported?\n");
213 goto error;
214 }
215 }
216
217 /* Everything is happy */
218 return 0;
219
220error:
221 release_region(sis5595_base + SMB_INDEX, 2);
222 return retval;
223}
224
225static int sis5595_transaction(struct i2c_adapter *adap)
226{
227 int temp;
228 int result = 0;
229 int timeout = 0;
230
231 /* Make sure the SMBus host is ready to start transmitting */
232 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
233 if (temp != 0x00) {
Jean Delvare541e6a02005-06-23 22:18:08 +0200234 dev_dbg(&adap->dev, "SMBus busy (%04x). Resetting...\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 sis5595_write(SMB_STS_LO, temp & 0xff);
236 sis5595_write(SMB_STS_HI, temp >> 8);
237 if ((temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8)) != 0x00) {
238 dev_dbg(&adap->dev, "Failed! (%02x)\n", temp);
239 return -1;
240 } else {
241 dev_dbg(&adap->dev, "Successfull!\n");
242 }
243 }
244
245 /* start the transaction by setting bit 4 */
246 sis5595_write(SMB_CTL_LO, sis5595_read(SMB_CTL_LO) | 0x10);
247
248 /* We will always wait for a fraction of a second! */
249 do {
250 msleep(1);
251 temp = sis5595_read(SMB_STS_LO);
252 } while (!(temp & 0x40) && (timeout++ < MAX_TIMEOUT));
253
254 /* If the SMBus is still busy, we give up */
255 if (timeout >= MAX_TIMEOUT) {
256 dev_dbg(&adap->dev, "SMBus Timeout!\n");
257 result = -1;
258 }
259
260 if (temp & 0x10) {
261 dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
262 result = -1;
263 }
264
265 if (temp & 0x20) {
266 dev_err(&adap->dev, "Bus collision! SMBus may be locked until "
267 "next hard reset (or not...)\n");
268 /* Clock stops and slave is stuck in mid-transmission */
269 result = -1;
270 }
271
272 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
273 if (temp != 0x00) {
274 sis5595_write(SMB_STS_LO, temp & 0xff);
275 sis5595_write(SMB_STS_HI, temp >> 8);
276 }
277
278 temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
279 if (temp != 0x00)
280 dev_dbg(&adap->dev, "Failed reset at end of transaction (%02x)\n", temp);
281
282 return result;
283}
284
285/* Return -1 on error. */
286static s32 sis5595_access(struct i2c_adapter *adap, u16 addr,
287 unsigned short flags, char read_write,
288 u8 command, int size, union i2c_smbus_data *data)
289{
290 switch (size) {
291 case I2C_SMBUS_QUICK:
292 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
293 size = SIS5595_QUICK;
294 break;
295 case I2C_SMBUS_BYTE:
296 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
297 if (read_write == I2C_SMBUS_WRITE)
298 sis5595_write(SMB_CMD, command);
299 size = SIS5595_BYTE;
300 break;
301 case I2C_SMBUS_BYTE_DATA:
302 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
303 sis5595_write(SMB_CMD, command);
304 if (read_write == I2C_SMBUS_WRITE)
305 sis5595_write(SMB_BYTE, data->byte);
306 size = SIS5595_BYTE_DATA;
307 break;
308 case I2C_SMBUS_PROC_CALL:
309 case I2C_SMBUS_WORD_DATA:
310 sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
311 sis5595_write(SMB_CMD, command);
312 if (read_write == I2C_SMBUS_WRITE) {
313 sis5595_write(SMB_BYTE, data->word & 0xff);
314 sis5595_write(SMB_BYTE + 1,
315 (data->word & 0xff00) >> 8);
316 }
317 size = (size == I2C_SMBUS_PROC_CALL) ? SIS5595_PROC_CALL : SIS5595_WORD_DATA;
318 break;
319/*
320 case I2C_SMBUS_BLOCK_DATA:
321 printk(KERN_WARNING "sis5595.o: Block data not yet implemented!\n");
322 return -1;
323 break;
324*/
325 default:
326 printk(KERN_WARNING "sis5595.o: Unsupported transaction %d\n", size);
327 return -1;
328 }
329
330 sis5595_write(SMB_CTL_LO, ((size & 0x0E)));
331
332 if (sis5595_transaction(adap))
333 return -1;
334
335 if ((size != SIS5595_PROC_CALL) &&
336 ((read_write == I2C_SMBUS_WRITE) || (size == SIS5595_QUICK)))
337 return 0;
338
339
340 switch (size) {
341 case SIS5595_BYTE: /* Where is the result put? I assume here it is in
342 SMB_DATA but it might just as well be in the
343 SMB_CMD. No clue in the docs */
344 case SIS5595_BYTE_DATA:
345 data->byte = sis5595_read(SMB_BYTE);
346 break;
347 case SIS5595_WORD_DATA:
348 case SIS5595_PROC_CALL:
349 data->word = sis5595_read(SMB_BYTE) + (sis5595_read(SMB_BYTE + 1) << 8);
350 break;
351 }
352 return 0;
353}
354
355static u32 sis5595_func(struct i2c_adapter *adapter)
356{
357 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
358 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
359 I2C_FUNC_SMBUS_PROC_CALL;
360}
361
Jean Delvare8f9082c2006-09-03 22:39:46 +0200362static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 .smbus_xfer = sis5595_access,
364 .functionality = sis5595_func,
365};
366
367static struct i2c_adapter sis5595_adapter = {
368 .owner = THIS_MODULE,
Stephen Hemminger9ace5552007-02-13 22:09:01 +0100369 .id = I2C_HW_SMBUS_SIS5595,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 .class = I2C_CLASS_HWMON,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 .algo = &smbus_algorithm,
372};
373
374static struct pci_device_id sis5595_ids[] __devinitdata = {
375 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
376 { 0, }
377};
378
379MODULE_DEVICE_TABLE (pci, sis5595_ids);
380
381static int __devinit sis5595_probe(struct pci_dev *dev, const struct pci_device_id *id)
382{
Jean Delvare7375cd82007-07-12 14:12:30 +0200383 int err;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (sis5595_setup(dev)) {
386 dev_err(&dev->dev, "SIS5595 not detected, module not inserted.\n");
387 return -ENODEV;
388 }
389
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100390 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 sis5595_adapter.dev.parent = &dev->dev;
392
393 sprintf(sis5595_adapter.name, "SMBus SIS5595 adapter at %04x",
394 sis5595_base + SMB_INDEX);
Jean Delvare7375cd82007-07-12 14:12:30 +0200395 err = i2c_add_adapter(&sis5595_adapter);
396 if (err) {
397 release_region(sis5595_base + SMB_INDEX, 2);
398 return err;
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Jean Delvare7375cd82007-07-12 14:12:30 +0200401 /* Always return failure here. This is to allow other drivers to bind
402 * to this pci device. We don't really want to have control over the
403 * pci device, we only wanted to read as few register values from it.
404 */
405 sis5595_pdev = pci_dev_get(dev);
406 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409static struct pci_driver sis5595_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 .name = "sis5595_smbus",
411 .id_table = sis5595_ids,
412 .probe = sis5595_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413};
414
415static int __init i2c_sis5595_init(void)
416{
417 return pci_register_driver(&sis5595_driver);
418}
419
420static void __exit i2c_sis5595_exit(void)
421{
422 pci_unregister_driver(&sis5595_driver);
Jean Delvare7375cd82007-07-12 14:12:30 +0200423 if (sis5595_pdev) {
424 i2c_del_adapter(&sis5595_adapter);
425 release_region(sis5595_base + SMB_INDEX, 2);
426 pci_dev_put(sis5595_pdev);
427 sis5595_pdev = NULL;
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
432MODULE_DESCRIPTION("SIS5595 SMBus driver");
433MODULE_LICENSE("GPL");
434
435module_init(i2c_sis5595_init);
436module_exit(i2c_sis5595_exit);