2 i2c-viapro.c - Part of lm_sensors, Linux kernel modules for hardware
4 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>, Kyösti Mälkki <kmalkki@cc.hut.fi>,
6 Mark D. Studebaker <mdsxyz123@yahoo.com>
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.
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.
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.
30 8233A (0x3147 and 0x3177)
33 Note: we assume there can only be one device, with one SMBus interface.
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/kernel.h>
40 #include <linux/stddef.h>
41 #include <linux/sched.h>
42 #include <linux/ioport.h>
43 #include <linux/i2c.h>
44 #include <linux/init.h>
51 /* SMBus address offsets */
52 static unsigned short vt596_smba;
53 #define SMBHSTSTS (vt596_smba + 0)
54 #define SMBHSLVSTS (vt596_smba + 1)
55 #define SMBHSTCNT (vt596_smba + 2)
56 #define SMBHSTCMD (vt596_smba + 3)
57 #define SMBHSTADD (vt596_smba + 4)
58 #define SMBHSTDAT0 (vt596_smba + 5)
59 #define SMBHSTDAT1 (vt596_smba + 6)
60 #define SMBBLKDAT (vt596_smba + 7)
61 #define SMBSLVCNT (vt596_smba + 8)
62 #define SMBSHDWCMD (vt596_smba + 9)
63 #define SMBSLVEVT (vt596_smba + 0xA)
64 #define SMBSLVDAT (vt596_smba + 0xC)
66 /* PCI Address Constants */
68 /* SMBus data in configuration space can be found in two places,
69 We try to select the better one*/
71 static unsigned short smb_cf_hstcfg = 0xD2;
73 #define SMBHSTCFG (smb_cf_hstcfg)
74 #define SMBSLVC (smb_cf_hstcfg + 1)
75 #define SMBSHDW1 (smb_cf_hstcfg + 2)
76 #define SMBSHDW2 (smb_cf_hstcfg + 3)
77 #define SMBREV (smb_cf_hstcfg + 4)
80 #define MAX_TIMEOUT 500
83 /* VT82C596 constants */
84 #define VT596_QUICK 0x00
85 #define VT596_BYTE 0x04
86 #define VT596_BYTE_DATA 0x08
87 #define VT596_WORD_DATA 0x0C
88 #define VT596_BLOCK_DATA 0x14
91 /* If force is set to anything different from 0, we forcibly enable the
94 MODULE_PARM(force, "i");
95 MODULE_PARM_DESC(force, "Forcibly enable the SMBus. DANGEROUS!");
97 /* If force_addr is set to anything different from 0, we forcibly enable
98 the VT596 at the given address. VERY DANGEROUS! */
99 static int force_addr;
100 MODULE_PARM(force_addr, "i");
101 MODULE_PARM_DESC(force_addr,
102 "Forcibly enable the SMBus at the given address. "
103 "EXTREMELY DANGEROUS!");
106 static struct i2c_adapter vt596_adapter;
108 /* Another internally used function */
109 static int vt596_transaction(void)
115 dev_dbg(&vt596_adapter.dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
116 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
117 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
120 /* Make sure the SMBus host is ready to start transmitting */
121 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
122 dev_dbg(&vt596_adapter.dev, "SMBus busy (0x%02x). "
123 "Resetting...\n", temp);
125 outb_p(temp, SMBHSTSTS);
126 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
127 dev_dbg(&vt596_adapter.dev, "Failed! (0x%02x)\n", temp);
131 dev_dbg(&vt596_adapter.dev, "Successfull!\n");
135 /* start the transaction by setting bit 6 */
136 outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
138 /* We will always wait for a fraction of a second!
139 I don't know if VIA needs this, Intel did */
142 temp = inb_p(SMBHSTSTS);
143 } while ((temp & 0x01) && (timeout++ < MAX_TIMEOUT));
145 /* If the SMBus is still busy, we give up */
146 if (timeout >= MAX_TIMEOUT) {
148 dev_dbg(&vt596_adapter.dev, "SMBus Timeout!\n");
153 dev_dbg(&vt596_adapter.dev, "Error: Failed bus transaction\n");
158 dev_info(&vt596_adapter.dev, "Bus collision! SMBus may be "
159 "locked until next hard\nreset. (sorry!)\n");
160 /* Clock stops and slave is stuck in mid-transmission */
165 dev_dbg(&vt596_adapter.dev, "Error: no response!\n");
168 if (inb_p(SMBHSTSTS) != 0x00)
169 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
171 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
172 dev_dbg(&vt596_adapter.dev, "Failed reset at end of "
173 "transaction (%02x)\n", temp);
175 dev_dbg(&vt596_adapter.dev, "Transaction (post): CNT=%02x, CMD=%02x, "
176 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
177 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
183 /* Return -1 on error. */
184 static s32 vt596_access(struct i2c_adapter *adap, u16 addr,
185 unsigned short flags, char read_write, u8 command,
186 int size, union i2c_smbus_data *data)
191 case I2C_SMBUS_PROC_CALL:
192 dev_info(&vt596_adapter.dev,
193 "I2C_SMBUS_PROC_CALL not supported!\n");
195 case I2C_SMBUS_QUICK:
196 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
201 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
203 if (read_write == I2C_SMBUS_WRITE)
204 outb_p(command, SMBHSTCMD);
207 case I2C_SMBUS_BYTE_DATA:
208 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
210 outb_p(command, SMBHSTCMD);
211 if (read_write == I2C_SMBUS_WRITE)
212 outb_p(data->byte, SMBHSTDAT0);
213 size = VT596_BYTE_DATA;
215 case I2C_SMBUS_WORD_DATA:
216 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
218 outb_p(command, SMBHSTCMD);
219 if (read_write == I2C_SMBUS_WRITE) {
220 outb_p(data->word & 0xff, SMBHSTDAT0);
221 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
223 size = VT596_WORD_DATA;
225 case I2C_SMBUS_BLOCK_DATA:
226 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
228 outb_p(command, SMBHSTCMD);
229 if (read_write == I2C_SMBUS_WRITE) {
230 len = data->block[0];
235 outb_p(len, SMBHSTDAT0);
236 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
237 for (i = 1; i <= len; i++)
238 outb_p(data->block[i], SMBBLKDAT);
240 size = VT596_BLOCK_DATA;
244 outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
246 if (vt596_transaction()) /* Error in transaction */
249 if ((read_write == I2C_SMBUS_WRITE) || (size == VT596_QUICK))
254 /* Where is the result put? I assume here it is in
255 * SMBHSTDAT0 but it might just as well be in the
256 * SMBHSTCMD. No clue in the docs
258 data->byte = inb_p(SMBHSTDAT0);
260 case VT596_BYTE_DATA:
261 data->byte = inb_p(SMBHSTDAT0);
263 case VT596_WORD_DATA:
264 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
266 case VT596_BLOCK_DATA:
267 data->block[0] = inb_p(SMBHSTDAT0);
268 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
269 for (i = 1; i <= data->block[0]; i++)
270 data->block[i] = inb_p(SMBBLKDAT);
276 static u32 vt596_func(struct i2c_adapter *adapter)
278 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
279 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
280 I2C_FUNC_SMBUS_BLOCK_DATA;
283 static struct i2c_algorithm smbus_algorithm = {
284 .name = "Non-I2C SMBus adapter",
285 .id = I2C_ALGO_SMBUS,
286 .smbus_xfer = vt596_access,
287 .functionality = vt596_func,
290 static struct i2c_adapter vt596_adapter = {
291 .owner = THIS_MODULE,
292 .class = I2C_ADAP_CLASS_SMBUS,
293 .algo = &smbus_algorithm,
297 static int __devinit vt596_probe(struct pci_dev *pdev,
298 const struct pci_device_id *id)
303 /* Determine the address of the SMBus areas */
305 vt596_smba = force_addr & 0xfff0;
310 if ((pci_read_config_word(pdev, id->driver_data, &vt596_smba)) ||
311 !(vt596_smba & 0x1)) {
312 /* try 2nd address and config reg. for 596 */
313 if (id->device == PCI_DEVICE_ID_VIA_82C596_3 &&
314 !pci_read_config_word(pdev, SMBBA2, &vt596_smba) &&
315 (vt596_smba & 0x1)) {
316 smb_cf_hstcfg = 0x84;
318 /* no matches at all */
319 dev_err(&pdev->dev, "Cannot configure "
320 "SMBus I/O Base address\n");
325 vt596_smba &= 0xfff0;
326 if (vt596_smba == 0) {
327 dev_err(&pdev->dev, "SMBus base address "
328 "uninitialized - upgrade BIOS or use "
329 "force_addr=0xaddr\n");
334 if (!request_region(vt596_smba, 8, "viapro-smbus")) {
335 dev_err(&pdev->dev, "SMBus region 0x%x already in use!\n",
340 pci_read_config_byte(pdev, SMBHSTCFG, &temp);
341 /* If force_addr is set, we program the new address here. Just to make
342 sure, we disable the VT596 first. */
344 pci_write_config_byte(pdev, SMBHSTCFG, temp & 0xfe);
345 pci_write_config_word(pdev, id->driver_data, vt596_smba);
346 pci_write_config_byte(pdev, SMBHSTCFG, temp | 0x01);
347 dev_warn(&pdev->dev, "WARNING: SMBus interface set to new "
348 "address 0x%04x!\n", vt596_smba);
349 } else if ((temp & 1) == 0) {
351 /* NOTE: This assumes I/O space and other allocations
352 * WERE done by the Bios! Don't complain if your
353 * hardware does weird things after enabling this.
354 * :') Check for Bios updates before resorting to
357 pci_write_config_byte(pdev, SMBHSTCFG, temp | 1);
358 dev_info(&pdev->dev, "Enabling SMBus device\n");
360 dev_err(&pdev->dev, "SMBUS: Error: Host SMBus "
361 "controller not enabled! - upgrade BIOS or "
367 if ((temp & 0x0E) == 8)
368 dev_dbg(&pdev->dev, "using Interrupt 9 for SMBus.\n");
369 else if ((temp & 0x0E) == 0)
370 dev_dbg(&pdev->dev, "using Interrupt SMI# for SMBus.\n");
372 dev_dbg(&pdev->dev, "Illegal Interrupt configuration "
373 "(or code out of date)!\n");
375 pci_read_config_byte(pdev, SMBREV, &temp);
376 dev_dbg(&pdev->dev, "SMBREV = 0x%X\n", temp);
377 dev_dbg(&pdev->dev, "VT596_smba = 0x%X\n", vt596_smba);
379 vt596_adapter.dev.parent = &pdev->dev;
380 snprintf(vt596_adapter.name, I2C_NAME_SIZE,
381 "SMBus Via Pro adapter at %04x", vt596_smba);
383 return i2c_add_adapter(&vt596_adapter);
386 release_region(vt596_smba, 8);
390 static void __devexit vt596_remove(struct pci_dev *pdev)
392 i2c_del_adapter(&vt596_adapter);
393 release_region(vt596_smba, 8);
396 static struct pci_device_id vt596_ids[] = {
398 .vendor = PCI_VENDOR_ID_VIA,
399 .device = PCI_DEVICE_ID_VIA_82C596_3,
400 .subvendor = PCI_ANY_ID,
401 .subdevice = PCI_ANY_ID,
402 .driver_data = SMBBA1,
405 .vendor = PCI_VENDOR_ID_VIA,
406 .device = PCI_DEVICE_ID_VIA_82C596B_3,
407 .subvendor = PCI_ANY_ID,
408 .subdevice = PCI_ANY_ID,
409 .driver_data = SMBBA1,
412 .vendor = PCI_VENDOR_ID_VIA,
413 .device = PCI_DEVICE_ID_VIA_82C686_4,
414 .subvendor = PCI_ANY_ID,
415 .subdevice = PCI_ANY_ID,
416 .driver_data = SMBBA1,
419 .vendor = PCI_VENDOR_ID_VIA,
420 .device = PCI_DEVICE_ID_VIA_8233_0,
421 .subvendor = PCI_ANY_ID,
422 .subdevice = PCI_ANY_ID,
423 .driver_data = SMBBA3
426 .vendor = PCI_VENDOR_ID_VIA,
427 .device = PCI_DEVICE_ID_VIA_8233A,
428 .subvendor = PCI_ANY_ID,
429 .subdevice = PCI_ANY_ID,
430 .driver_data = SMBBA3,
433 .vendor = PCI_VENDOR_ID_VIA,
434 .device = PCI_DEVICE_ID_VIA_8235,
435 .subvendor = PCI_ANY_ID,
436 .subdevice = PCI_ANY_ID,
437 .driver_data = SMBBA3
440 .vendor = PCI_VENDOR_ID_VIA,
441 .device = PCI_DEVICE_ID_VIA_8237,
442 .subvendor = PCI_ANY_ID,
443 .subdevice = PCI_ANY_ID,
444 .driver_data = SMBBA3
447 .vendor = PCI_VENDOR_ID_VIA,
448 .device = PCI_DEVICE_ID_VIA_8231_4,
449 .subvendor = PCI_ANY_ID,
450 .subdevice = PCI_ANY_ID,
451 .driver_data = SMBBA1,
456 static struct pci_driver vt596_driver = {
457 .name = "vt596 smbus",
458 .id_table = vt596_ids,
459 .probe = vt596_probe,
460 .remove = __devexit_p(vt596_remove),
463 static int __init i2c_vt596_init(void)
465 return pci_module_init(&vt596_driver);
469 static void __exit i2c_vt596_exit(void)
471 pci_unregister_driver(&vt596_driver);
475 "Frodo Looijaard <frodol@dds.nl> and "
476 "Philip Edelbrock <phil@netroedge.com>");
477 MODULE_DESCRIPTION("vt82c596 SMBus driver");
478 MODULE_LICENSE("GPL");
480 module_init(i2c_vt596_init);
481 module_exit(i2c_vt596_exit);