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)
32 Note: we assume there can only be one device, with one SMBus interface.
35 #include <linux/module.h>
36 #include <linux/pci.h>
37 #include <linux/kernel.h>
38 #include <linux/stddef.h>
39 #include <linux/sched.h>
40 #include <linux/ioport.h>
41 #include <linux/i2c.h>
42 #include <linux/init.h>
49 /* SMBus address offsets */
50 static unsigned short vt596_smba;
51 #define SMBHSTSTS (vt596_smba + 0)
52 #define SMBHSLVSTS (vt596_smba + 1)
53 #define SMBHSTCNT (vt596_smba + 2)
54 #define SMBHSTCMD (vt596_smba + 3)
55 #define SMBHSTADD (vt596_smba + 4)
56 #define SMBHSTDAT0 (vt596_smba + 5)
57 #define SMBHSTDAT1 (vt596_smba + 6)
58 #define SMBBLKDAT (vt596_smba + 7)
59 #define SMBSLVCNT (vt596_smba + 8)
60 #define SMBSHDWCMD (vt596_smba + 9)
61 #define SMBSLVEVT (vt596_smba + 0xA)
62 #define SMBSLVDAT (vt596_smba + 0xC)
64 /* PCI Address Constants */
66 /* SMBus data in configuration space can be found in two places,
67 We try to select the better one*/
69 static unsigned short smb_cf_hstcfg = 0xD2;
71 #define SMBHSTCFG (smb_cf_hstcfg)
72 #define SMBSLVC (smb_cf_hstcfg + 1)
73 #define SMBSHDW1 (smb_cf_hstcfg + 2)
74 #define SMBSHDW2 (smb_cf_hstcfg + 3)
75 #define SMBREV (smb_cf_hstcfg + 4)
78 #define MAX_TIMEOUT 500
81 /* VT82C596 constants */
82 #define VT596_QUICK 0x00
83 #define VT596_BYTE 0x04
84 #define VT596_BYTE_DATA 0x08
85 #define VT596_WORD_DATA 0x0C
86 #define VT596_BLOCK_DATA 0x14
89 /* If force is set to anything different from 0, we forcibly enable the
92 MODULE_PARM(force, "i");
93 MODULE_PARM_DESC(force, "Forcibly enable the SMBus. DANGEROUS!");
95 /* If force_addr is set to anything different from 0, we forcibly enable
96 the VT596 at the given address. VERY DANGEROUS! */
97 static int force_addr;
98 MODULE_PARM(force_addr, "i");
99 MODULE_PARM_DESC(force_addr,
100 "Forcibly enable the SMBus at the given address. "
101 "EXTREMELY DANGEROUS!");
104 static struct i2c_adapter vt596_adapter;
106 /* Another internally used function */
107 static int vt596_transaction(void)
113 dev_dbg(&vt596_adapter.dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
114 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
115 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
118 /* Make sure the SMBus host is ready to start transmitting */
119 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
120 dev_dbg(&vt596_adapter.dev, "SMBus busy (0x%02x). "
121 "Resetting...\n", temp);
123 outb_p(temp, SMBHSTSTS);
124 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
125 dev_dbg(&vt596_adapter.dev, "Failed! (0x%02x)\n", temp);
129 dev_dbg(&vt596_adapter.dev, "Successfull!\n");
133 /* start the transaction by setting bit 6 */
134 outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
136 /* We will always wait for a fraction of a second!
137 I don't know if VIA needs this, Intel did */
140 temp = inb_p(SMBHSTSTS);
141 } while ((temp & 0x01) && (timeout++ < MAX_TIMEOUT));
143 /* If the SMBus is still busy, we give up */
144 if (timeout >= MAX_TIMEOUT) {
146 dev_dbg(&vt596_adapter.dev, "SMBus Timeout!\n");
151 dev_dbg(&vt596_adapter.dev, "Error: Failed bus transaction\n");
156 dev_info(&vt596_adapter.dev, "Bus collision! SMBus may be "
157 "locked until next hard\nreset. (sorry!)\n");
158 /* Clock stops and slave is stuck in mid-transmission */
163 dev_dbg(&vt596_adapter.dev, "Error: no response!\n");
166 if (inb_p(SMBHSTSTS) != 0x00)
167 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
169 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
170 dev_dbg(&vt596_adapter.dev, "Failed reset at end of "
171 "transaction (%02x)\n", temp);
173 dev_dbg(&vt596_adapter.dev, "Transaction (post): CNT=%02x, CMD=%02x, "
174 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
175 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
181 /* Return -1 on error. */
182 static s32 vt596_access(struct i2c_adapter *adap, u16 addr,
183 unsigned short flags, char read_write, u8 command,
184 int size, union i2c_smbus_data *data)
189 case I2C_SMBUS_PROC_CALL:
190 dev_info(&vt596_adapter.dev,
191 "I2C_SMBUS_PROC_CALL not supported!\n");
193 case I2C_SMBUS_QUICK:
194 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
199 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
201 if (read_write == I2C_SMBUS_WRITE)
202 outb_p(command, SMBHSTCMD);
205 case I2C_SMBUS_BYTE_DATA:
206 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
208 outb_p(command, SMBHSTCMD);
209 if (read_write == I2C_SMBUS_WRITE)
210 outb_p(data->byte, SMBHSTDAT0);
211 size = VT596_BYTE_DATA;
213 case I2C_SMBUS_WORD_DATA:
214 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
216 outb_p(command, SMBHSTCMD);
217 if (read_write == I2C_SMBUS_WRITE) {
218 outb_p(data->word & 0xff, SMBHSTDAT0);
219 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
221 size = VT596_WORD_DATA;
223 case I2C_SMBUS_BLOCK_DATA:
224 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
226 outb_p(command, SMBHSTCMD);
227 if (read_write == I2C_SMBUS_WRITE) {
228 len = data->block[0];
233 outb_p(len, SMBHSTDAT0);
234 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
235 for (i = 1; i <= len; i++)
236 outb_p(data->block[i], SMBBLKDAT);
238 size = VT596_BLOCK_DATA;
242 outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
244 if (vt596_transaction()) /* Error in transaction */
247 if ((read_write == I2C_SMBUS_WRITE) || (size == VT596_QUICK))
252 /* Where is the result put? I assume here it is in
253 * SMBHSTDAT0 but it might just as well be in the
254 * SMBHSTCMD. No clue in the docs
256 data->byte = inb_p(SMBHSTDAT0);
258 case VT596_BYTE_DATA:
259 data->byte = inb_p(SMBHSTDAT0);
261 case VT596_WORD_DATA:
262 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
264 case VT596_BLOCK_DATA:
265 data->block[0] = inb_p(SMBHSTDAT0);
266 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
267 for (i = 1; i <= data->block[0]; i++)
268 data->block[i] = inb_p(SMBBLKDAT);
274 static u32 vt596_func(struct i2c_adapter *adapter)
276 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
277 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
278 I2C_FUNC_SMBUS_BLOCK_DATA;
281 static struct i2c_algorithm smbus_algorithm = {
282 .name = "Non-I2C SMBus adapter",
283 .id = I2C_ALGO_SMBUS,
284 .smbus_xfer = vt596_access,
285 .functionality = vt596_func,
288 static struct i2c_adapter vt596_adapter = {
289 .owner = THIS_MODULE,
290 .id = I2C_ALGO_SMBUS | I2C_HW_SMBUS_VIA2,
291 .class = I2C_ADAP_CLASS_SMBUS,
292 .algo = &smbus_algorithm,
296 static int __devinit vt596_probe(struct pci_dev *pdev,
297 const struct pci_device_id *id)
302 /* Determine the address of the SMBus areas */
304 vt596_smba = force_addr & 0xfff0;
309 if ((pci_read_config_word(pdev, id->driver_data, &vt596_smba)) ||
310 !(vt596_smba & 0x1)) {
311 /* try 2nd address and config reg. for 596 */
312 if (id->device == PCI_DEVICE_ID_VIA_82C596_3 &&
313 !pci_read_config_word(pdev, SMBBA2, &vt596_smba) &&
314 (vt596_smba & 0x1)) {
315 smb_cf_hstcfg = 0x84;
317 /* no matches at all */
318 dev_err(&pdev->dev, "Cannot configure "
319 "SMBus I/O Base address\n");
324 vt596_smba &= 0xfff0;
325 if (vt596_smba == 0) {
326 dev_err(&pdev->dev, "SMBus base address "
327 "uninitialized - upgrade BIOS or use "
328 "force_addr=0xaddr\n");
333 if (!request_region(vt596_smba, 8, "viapro-smbus")) {
334 dev_err(&pdev->dev, "SMBus region 0x%x already in use!\n",
339 pci_read_config_byte(pdev, SMBHSTCFG, &temp);
340 /* If force_addr is set, we program the new address here. Just to make
341 sure, we disable the VT596 first. */
343 pci_write_config_byte(pdev, SMBHSTCFG, temp & 0xfe);
344 pci_write_config_word(pdev, id->driver_data, vt596_smba);
345 pci_write_config_byte(pdev, SMBHSTCFG, temp | 0x01);
346 dev_warn(&pdev->dev, "WARNING: SMBus interface set to new "
347 "address 0x%04x!\n", vt596_smba);
348 } else if ((temp & 1) == 0) {
350 /* NOTE: This assumes I/O space and other allocations
351 * WERE done by the Bios! Don't complain if your
352 * hardware does weird things after enabling this.
353 * :') Check for Bios updates before resorting to
356 pci_write_config_byte(pdev, SMBHSTCFG, temp | 1);
357 dev_info(&pdev->dev, "Enabling SMBus device\n");
359 dev_err(&pdev->dev, "SMBUS: Error: Host SMBus "
360 "controller not enabled! - upgrade BIOS or "
366 if ((temp & 0x0E) == 8)
367 dev_dbg(&pdev->dev, "using Interrupt 9 for SMBus.\n");
368 else if ((temp & 0x0E) == 0)
369 dev_dbg(&pdev->dev, "using Interrupt SMI# for SMBus.\n");
371 dev_dbg(&pdev->dev, "Illegal Interrupt configuration "
372 "(or code out of date)!\n");
374 pci_read_config_byte(pdev, SMBREV, &temp);
375 dev_dbg(&pdev->dev, "SMBREV = 0x%X\n", temp);
376 dev_dbg(&pdev->dev, "VT596_smba = 0x%X\n", vt596_smba);
378 vt596_adapter.dev.parent = &pdev->dev;
379 snprintf(vt596_adapter.name, DEVICE_NAME_SIZE,
380 "SMBus Via Pro adapter at %04x", vt596_smba);
382 return i2c_add_adapter(&vt596_adapter);
385 release_region(vt596_smba, 8);
389 static void __devexit vt596_remove(struct pci_dev *pdev)
391 i2c_del_adapter(&vt596_adapter);
392 release_region(vt596_smba, 8);
395 static struct pci_device_id vt596_ids[] = {
397 .vendor = PCI_VENDOR_ID_VIA,
398 .device = PCI_DEVICE_ID_VIA_82C596_3,
399 .subvendor = PCI_ANY_ID,
400 .subdevice = PCI_ANY_ID,
401 .driver_data = SMBBA1,
404 .vendor = PCI_VENDOR_ID_VIA,
405 .device = PCI_DEVICE_ID_VIA_82C596B_3,
406 .subvendor = PCI_ANY_ID,
407 .subdevice = PCI_ANY_ID,
408 .driver_data = SMBBA1,
411 .vendor = PCI_VENDOR_ID_VIA,
412 .device = PCI_DEVICE_ID_VIA_82C686_4,
413 .subvendor = PCI_ANY_ID,
414 .subdevice = PCI_ANY_ID,
415 .driver_data = SMBBA1,
418 .vendor = PCI_VENDOR_ID_VIA,
419 .device = PCI_DEVICE_ID_VIA_8233_0,
420 .subvendor = PCI_ANY_ID,
421 .subdevice = PCI_ANY_ID,
422 .driver_data = SMBBA3
425 .vendor = PCI_VENDOR_ID_VIA,
426 .device = PCI_DEVICE_ID_VIA_8233A,
427 .subvendor = PCI_ANY_ID,
428 .subdevice = PCI_ANY_ID,
429 .driver_data = SMBBA3,
432 .vendor = PCI_VENDOR_ID_VIA,
433 .device = PCI_DEVICE_ID_VIA_8235,
434 .subvendor = PCI_ANY_ID,
435 .subdevice = PCI_ANY_ID,
436 .driver_data = SMBBA3
439 .vendor = PCI_VENDOR_ID_VIA,
440 .device = PCI_DEVICE_ID_VIA_8231_4,
441 .subvendor = PCI_ANY_ID,
442 .subdevice = PCI_ANY_ID,
443 .driver_data = SMBBA1,
448 static struct pci_driver vt596_driver = {
449 .name = "vt596 smbus",
450 .id_table = vt596_ids,
451 .probe = vt596_probe,
452 .remove = __devexit_p(vt596_remove),
455 static int __init i2c_vt596_init(void)
457 return pci_module_init(&vt596_driver);
461 static void __exit i2c_vt596_exit(void)
463 pci_unregister_driver(&vt596_driver);
467 "Frodo Looijaard <frodol@dds.nl> and "
468 "Philip Edelbrock <phil@netroedge.com>");
469 MODULE_DESCRIPTION("vt82c596 SMBus driver");
470 MODULE_LICENSE("GPL");
472 module_init(i2c_vt596_init);
473 module_exit(i2c_vt596_exit);