11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl> and 31da177e4SLinus Torvalds Philip Edelbrock <phil@netroedge.com> 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds This program is free software; you can redistribute it and/or modify 61da177e4SLinus Torvalds it under the terms of the GNU General Public License as published by 71da177e4SLinus Torvalds the Free Software Foundation; either version 2 of the License, or 81da177e4SLinus Torvalds (at your option) any later version. 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds This program is distributed in the hope that it will be useful, 111da177e4SLinus Torvalds but WITHOUT ANY WARRANTY; without even the implied warranty of 121da177e4SLinus Torvalds MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131da177e4SLinus Torvalds GNU General Public License for more details. 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds You should have received a copy of the GNU General Public License 161da177e4SLinus Torvalds along with this program; if not, write to the Free Software 171da177e4SLinus Torvalds Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds /* 211da177e4SLinus Torvalds Supports: 221da177e4SLinus Torvalds Intel PIIX4, 440MX 23506a8b6cSFlavio Leitner Serverworks OSB4, CSB5, CSB6, HT-1000, HT-1100 242a2f7404SAndrew Armenia ATI IXP200, IXP300, IXP400, SB600, SB700/SP5100, SB800 253806e94bSCrane Cai AMD Hudson-2 261da177e4SLinus Torvalds SMSC Victory66 271da177e4SLinus Torvalds 282a2f7404SAndrew Armenia Note: we assume there can only be one device, with one or more 292a2f7404SAndrew Armenia SMBus interfaces. 301da177e4SLinus Torvalds */ 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #include <linux/module.h> 331da177e4SLinus Torvalds #include <linux/moduleparam.h> 341da177e4SLinus Torvalds #include <linux/pci.h> 351da177e4SLinus Torvalds #include <linux/kernel.h> 361da177e4SLinus Torvalds #include <linux/delay.h> 371da177e4SLinus Torvalds #include <linux/stddef.h> 381da177e4SLinus Torvalds #include <linux/ioport.h> 391da177e4SLinus Torvalds #include <linux/i2c.h> 40c415b303SDaniel J Blueman #include <linux/slab.h> 411da177e4SLinus Torvalds #include <linux/init.h> 421da177e4SLinus Torvalds #include <linux/dmi.h> 4354fb4a05SJean Delvare #include <linux/acpi.h> 4421782180SH Hartley Sweeten #include <linux/io.h> 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds /* PIIX4 SMBus address offsets */ 481da177e4SLinus Torvalds #define SMBHSTSTS (0 + piix4_smba) 491da177e4SLinus Torvalds #define SMBHSLVSTS (1 + piix4_smba) 501da177e4SLinus Torvalds #define SMBHSTCNT (2 + piix4_smba) 511da177e4SLinus Torvalds #define SMBHSTCMD (3 + piix4_smba) 521da177e4SLinus Torvalds #define SMBHSTADD (4 + piix4_smba) 531da177e4SLinus Torvalds #define SMBHSTDAT0 (5 + piix4_smba) 541da177e4SLinus Torvalds #define SMBHSTDAT1 (6 + piix4_smba) 551da177e4SLinus Torvalds #define SMBBLKDAT (7 + piix4_smba) 561da177e4SLinus Torvalds #define SMBSLVCNT (8 + piix4_smba) 571da177e4SLinus Torvalds #define SMBSHDWCMD (9 + piix4_smba) 581da177e4SLinus Torvalds #define SMBSLVEVT (0xA + piix4_smba) 591da177e4SLinus Torvalds #define SMBSLVDAT (0xC + piix4_smba) 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds /* count for request_region */ 621da177e4SLinus Torvalds #define SMBIOSIZE 8 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* PCI Address Constants */ 651da177e4SLinus Torvalds #define SMBBA 0x090 661da177e4SLinus Torvalds #define SMBHSTCFG 0x0D2 671da177e4SLinus Torvalds #define SMBSLVC 0x0D3 681da177e4SLinus Torvalds #define SMBSHDW1 0x0D4 691da177e4SLinus Torvalds #define SMBSHDW2 0x0D5 701da177e4SLinus Torvalds #define SMBREV 0x0D6 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds /* Other settings */ 731da177e4SLinus Torvalds #define MAX_TIMEOUT 500 741da177e4SLinus Torvalds #define ENABLE_INT9 0 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds /* PIIX4 constants */ 771da177e4SLinus Torvalds #define PIIX4_QUICK 0x00 781da177e4SLinus Torvalds #define PIIX4_BYTE 0x04 791da177e4SLinus Torvalds #define PIIX4_BYTE_DATA 0x08 801da177e4SLinus Torvalds #define PIIX4_WORD_DATA 0x0C 811da177e4SLinus Torvalds #define PIIX4_BLOCK_DATA 0x14 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds /* insmod parameters */ 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* If force is set to anything different from 0, we forcibly enable the 861da177e4SLinus Torvalds PIIX4. DANGEROUS! */ 8760507095SJean Delvare static int force; 881da177e4SLinus Torvalds module_param (force, int, 0); 891da177e4SLinus Torvalds MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!"); 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* If force_addr is set to anything different from 0, we forcibly enable 921da177e4SLinus Torvalds the PIIX4 at the given address. VERY DANGEROUS! */ 9360507095SJean Delvare static int force_addr; 941da177e4SLinus Torvalds module_param (force_addr, int, 0); 951da177e4SLinus Torvalds MODULE_PARM_DESC(force_addr, 961da177e4SLinus Torvalds "Forcibly enable the PIIX4 at the given address. " 971da177e4SLinus Torvalds "EXTREMELY DANGEROUS!"); 981da177e4SLinus Torvalds 99b1c1759cSDavid Milburn static int srvrworks_csb5_delay; 100d6072f84SJean Delvare static struct pci_driver piix4_driver; 1011da177e4SLinus Torvalds 102c2fc54fcSJean Delvare static struct dmi_system_id __devinitdata piix4_dmi_blacklist[] = { 103c2fc54fcSJean Delvare { 104c2fc54fcSJean Delvare .ident = "Sapphire AM2RD790", 105c2fc54fcSJean Delvare .matches = { 106c2fc54fcSJean Delvare DMI_MATCH(DMI_BOARD_VENDOR, "SAPPHIRE Inc."), 107c2fc54fcSJean Delvare DMI_MATCH(DMI_BOARD_NAME, "PC-AM2RD790"), 108c2fc54fcSJean Delvare }, 109c2fc54fcSJean Delvare }, 110c2fc54fcSJean Delvare { 111c2fc54fcSJean Delvare .ident = "DFI Lanparty UT 790FX", 112c2fc54fcSJean Delvare .matches = { 113c2fc54fcSJean Delvare DMI_MATCH(DMI_BOARD_VENDOR, "DFI Inc."), 114c2fc54fcSJean Delvare DMI_MATCH(DMI_BOARD_NAME, "LP UT 790FX"), 115c2fc54fcSJean Delvare }, 116c2fc54fcSJean Delvare }, 117c2fc54fcSJean Delvare { } 118c2fc54fcSJean Delvare }; 119c2fc54fcSJean Delvare 120c2fc54fcSJean Delvare /* The IBM entry is in a separate table because we only check it 121c2fc54fcSJean Delvare on Intel-based systems */ 122c2fc54fcSJean Delvare static struct dmi_system_id __devinitdata piix4_dmi_ibm[] = { 1231da177e4SLinus Torvalds { 1241da177e4SLinus Torvalds .ident = "IBM", 1251da177e4SLinus Torvalds .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), }, 1261da177e4SLinus Torvalds }, 1271da177e4SLinus Torvalds { }, 1281da177e4SLinus Torvalds }; 1291da177e4SLinus Torvalds 13014a8086dSAndrew Armenia struct i2c_piix4_adapdata { 13114a8086dSAndrew Armenia unsigned short smba; 13214a8086dSAndrew Armenia }; 13314a8086dSAndrew Armenia 1341da177e4SLinus Torvalds static int __devinit piix4_setup(struct pci_dev *PIIX4_dev, 1351da177e4SLinus Torvalds const struct pci_device_id *id) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds unsigned char temp; 13814a8086dSAndrew Armenia unsigned short piix4_smba; 1391da177e4SLinus Torvalds 140b1c1759cSDavid Milburn if ((PIIX4_dev->vendor == PCI_VENDOR_ID_SERVERWORKS) && 141b1c1759cSDavid Milburn (PIIX4_dev->device == PCI_DEVICE_ID_SERVERWORKS_CSB5)) 142b1c1759cSDavid Milburn srvrworks_csb5_delay = 1; 143b1c1759cSDavid Milburn 144c2fc54fcSJean Delvare /* On some motherboards, it was reported that accessing the SMBus 145c2fc54fcSJean Delvare caused severe hardware problems */ 146c2fc54fcSJean Delvare if (dmi_check_system(piix4_dmi_blacklist)) { 147c2fc54fcSJean Delvare dev_err(&PIIX4_dev->dev, 148c2fc54fcSJean Delvare "Accessing the SMBus on this system is unsafe!\n"); 149c2fc54fcSJean Delvare return -EPERM; 150c2fc54fcSJean Delvare } 151c2fc54fcSJean Delvare 1521da177e4SLinus Torvalds /* Don't access SMBus on IBM systems which get corrupted eeproms */ 153c2fc54fcSJean Delvare if (dmi_check_system(piix4_dmi_ibm) && 1541da177e4SLinus Torvalds PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) { 155f9ba6c04SJean Delvare dev_err(&PIIX4_dev->dev, "IBM system detected; this module " 1561da177e4SLinus Torvalds "may corrupt your serial eeprom! Refusing to load " 1571da177e4SLinus Torvalds "module!\n"); 1581da177e4SLinus Torvalds return -EPERM; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds /* Determine the address of the SMBus areas */ 1621da177e4SLinus Torvalds if (force_addr) { 1631da177e4SLinus Torvalds piix4_smba = force_addr & 0xfff0; 1641da177e4SLinus Torvalds force = 0; 1651da177e4SLinus Torvalds } else { 1661da177e4SLinus Torvalds pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba); 1671da177e4SLinus Torvalds piix4_smba &= 0xfff0; 1681da177e4SLinus Torvalds if(piix4_smba == 0) { 169fa63cd56SJean Delvare dev_err(&PIIX4_dev->dev, "SMBus base address " 1701da177e4SLinus Torvalds "uninitialized - upgrade BIOS or use " 1711da177e4SLinus Torvalds "force_addr=0xaddr\n"); 1721da177e4SLinus Torvalds return -ENODEV; 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 17654fb4a05SJean Delvare if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) 17718669eabSJean Delvare return -ENODEV; 17854fb4a05SJean Delvare 179d6072f84SJean Delvare if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) { 180fa63cd56SJean Delvare dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n", 1811da177e4SLinus Torvalds piix4_smba); 182fa63cd56SJean Delvare return -EBUSY; 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp); 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* If force_addr is set, we program the new address here. Just to make 1881da177e4SLinus Torvalds sure, we disable the PIIX4 first. */ 1891da177e4SLinus Torvalds if (force_addr) { 1901da177e4SLinus Torvalds pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe); 1911da177e4SLinus Torvalds pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba); 1921da177e4SLinus Torvalds pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01); 1931da177e4SLinus Torvalds dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to " 1941da177e4SLinus Torvalds "new address %04x!\n", piix4_smba); 1951da177e4SLinus Torvalds } else if ((temp & 1) == 0) { 1961da177e4SLinus Torvalds if (force) { 1971da177e4SLinus Torvalds /* This should never need to be done, but has been 1981da177e4SLinus Torvalds * noted that many Dell machines have the SMBus 1991da177e4SLinus Torvalds * interface on the PIIX4 disabled!? NOTE: This assumes 2001da177e4SLinus Torvalds * I/O space and other allocations WERE done by the 2011da177e4SLinus Torvalds * Bios! Don't complain if your hardware does weird 2021da177e4SLinus Torvalds * things after enabling this. :') Check for Bios 2031da177e4SLinus Torvalds * updates before resorting to this. 2041da177e4SLinus Torvalds */ 2051da177e4SLinus Torvalds pci_write_config_byte(PIIX4_dev, SMBHSTCFG, 2061da177e4SLinus Torvalds temp | 1); 207*8117e41eSJoe Perches dev_notice(&PIIX4_dev->dev, 208*8117e41eSJoe Perches "WARNING: SMBus interface has been FORCEFULLY ENABLED!\n"); 2091da177e4SLinus Torvalds } else { 2101da177e4SLinus Torvalds dev_err(&PIIX4_dev->dev, 2111da177e4SLinus Torvalds "Host SMBus controller not enabled!\n"); 2121da177e4SLinus Torvalds release_region(piix4_smba, SMBIOSIZE); 2131da177e4SLinus Torvalds return -ENODEV; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds 21754aaa1caSRudolf Marek if (((temp & 0x0E) == 8) || ((temp & 0x0E) == 2)) 2181da177e4SLinus Torvalds dev_dbg(&PIIX4_dev->dev, "Using Interrupt 9 for SMBus.\n"); 2191da177e4SLinus Torvalds else if ((temp & 0x0E) == 0) 2201da177e4SLinus Torvalds dev_dbg(&PIIX4_dev->dev, "Using Interrupt SMI# for SMBus.\n"); 2211da177e4SLinus Torvalds else 2221da177e4SLinus Torvalds dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration " 2231da177e4SLinus Torvalds "(or code out of date)!\n"); 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds pci_read_config_byte(PIIX4_dev, SMBREV, &temp); 226fa63cd56SJean Delvare dev_info(&PIIX4_dev->dev, 227fa63cd56SJean Delvare "SMBus Host Controller at 0x%x, revision %d\n", 228fa63cd56SJean Delvare piix4_smba, temp); 2291da177e4SLinus Torvalds 23014a8086dSAndrew Armenia return piix4_smba; 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds 23387e1960eSShane Huang static int __devinit piix4_setup_sb800(struct pci_dev *PIIX4_dev, 23487e1960eSShane Huang const struct pci_device_id *id) 23587e1960eSShane Huang { 23614a8086dSAndrew Armenia unsigned short piix4_smba; 23787e1960eSShane Huang unsigned short smba_idx = 0xcd6; 23887e1960eSShane Huang u8 smba_en_lo, smba_en_hi, i2ccfg, i2ccfg_offset = 0x10, smb_en = 0x2c; 23987e1960eSShane Huang 2403806e94bSCrane Cai /* SB800 and later SMBus does not support forcing address */ 24187e1960eSShane Huang if (force || force_addr) { 2423806e94bSCrane Cai dev_err(&PIIX4_dev->dev, "SMBus does not support " 24387e1960eSShane Huang "forcing address!\n"); 24487e1960eSShane Huang return -EINVAL; 24587e1960eSShane Huang } 24687e1960eSShane Huang 24787e1960eSShane Huang /* Determine the address of the SMBus areas */ 24887e1960eSShane Huang if (!request_region(smba_idx, 2, "smba_idx")) { 24987e1960eSShane Huang dev_err(&PIIX4_dev->dev, "SMBus base address index region " 25087e1960eSShane Huang "0x%x already in use!\n", smba_idx); 25187e1960eSShane Huang return -EBUSY; 25287e1960eSShane Huang } 25387e1960eSShane Huang outb_p(smb_en, smba_idx); 25487e1960eSShane Huang smba_en_lo = inb_p(smba_idx + 1); 25587e1960eSShane Huang outb_p(smb_en + 1, smba_idx); 25687e1960eSShane Huang smba_en_hi = inb_p(smba_idx + 1); 25787e1960eSShane Huang release_region(smba_idx, 2); 25887e1960eSShane Huang 25987e1960eSShane Huang if ((smba_en_lo & 1) == 0) { 26087e1960eSShane Huang dev_err(&PIIX4_dev->dev, 26187e1960eSShane Huang "Host SMBus controller not enabled!\n"); 26287e1960eSShane Huang return -ENODEV; 26387e1960eSShane Huang } 26487e1960eSShane Huang 26587e1960eSShane Huang piix4_smba = ((smba_en_hi << 8) | smba_en_lo) & 0xffe0; 26687e1960eSShane Huang if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) 26718669eabSJean Delvare return -ENODEV; 26887e1960eSShane Huang 26987e1960eSShane Huang if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) { 27087e1960eSShane Huang dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n", 27187e1960eSShane Huang piix4_smba); 27287e1960eSShane Huang return -EBUSY; 27387e1960eSShane Huang } 27487e1960eSShane Huang 27587e1960eSShane Huang /* Request the SMBus I2C bus config region */ 27687e1960eSShane Huang if (!request_region(piix4_smba + i2ccfg_offset, 1, "i2ccfg")) { 27787e1960eSShane Huang dev_err(&PIIX4_dev->dev, "SMBus I2C bus config region " 27887e1960eSShane Huang "0x%x already in use!\n", piix4_smba + i2ccfg_offset); 27987e1960eSShane Huang release_region(piix4_smba, SMBIOSIZE); 28087e1960eSShane Huang return -EBUSY; 28187e1960eSShane Huang } 28287e1960eSShane Huang i2ccfg = inb_p(piix4_smba + i2ccfg_offset); 28387e1960eSShane Huang release_region(piix4_smba + i2ccfg_offset, 1); 28487e1960eSShane Huang 28587e1960eSShane Huang if (i2ccfg & 1) 28687e1960eSShane Huang dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus.\n"); 28787e1960eSShane Huang else 28887e1960eSShane Huang dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus.\n"); 28987e1960eSShane Huang 29087e1960eSShane Huang dev_info(&PIIX4_dev->dev, 29187e1960eSShane Huang "SMBus Host Controller at 0x%x, revision %d\n", 29287e1960eSShane Huang piix4_smba, i2ccfg >> 4); 29387e1960eSShane Huang 29414a8086dSAndrew Armenia return piix4_smba; 29587e1960eSShane Huang } 29687e1960eSShane Huang 2972a2f7404SAndrew Armenia static int __devinit piix4_setup_aux(struct pci_dev *PIIX4_dev, 2982a2f7404SAndrew Armenia const struct pci_device_id *id, 2992a2f7404SAndrew Armenia unsigned short base_reg_addr) 3002a2f7404SAndrew Armenia { 3012a2f7404SAndrew Armenia /* Set up auxiliary SMBus controllers found on some 3022a2f7404SAndrew Armenia * AMD chipsets e.g. SP5100 (SB700 derivative) */ 3032a2f7404SAndrew Armenia 3042a2f7404SAndrew Armenia unsigned short piix4_smba; 3052a2f7404SAndrew Armenia 3062a2f7404SAndrew Armenia /* Read address of auxiliary SMBus controller */ 3072a2f7404SAndrew Armenia pci_read_config_word(PIIX4_dev, base_reg_addr, &piix4_smba); 3082a2f7404SAndrew Armenia if ((piix4_smba & 1) == 0) { 3092a2f7404SAndrew Armenia dev_dbg(&PIIX4_dev->dev, 3102a2f7404SAndrew Armenia "Auxiliary SMBus controller not enabled\n"); 3112a2f7404SAndrew Armenia return -ENODEV; 3122a2f7404SAndrew Armenia } 3132a2f7404SAndrew Armenia 3142a2f7404SAndrew Armenia piix4_smba &= 0xfff0; 3152a2f7404SAndrew Armenia if (piix4_smba == 0) { 3162a2f7404SAndrew Armenia dev_dbg(&PIIX4_dev->dev, 3172a2f7404SAndrew Armenia "Auxiliary SMBus base address uninitialized\n"); 3182a2f7404SAndrew Armenia return -ENODEV; 3192a2f7404SAndrew Armenia } 3202a2f7404SAndrew Armenia 3212a2f7404SAndrew Armenia if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) 3222a2f7404SAndrew Armenia return -ENODEV; 3232a2f7404SAndrew Armenia 3242a2f7404SAndrew Armenia if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) { 3252a2f7404SAndrew Armenia dev_err(&PIIX4_dev->dev, "Auxiliary SMBus region 0x%x " 3262a2f7404SAndrew Armenia "already in use!\n", piix4_smba); 3272a2f7404SAndrew Armenia return -EBUSY; 3282a2f7404SAndrew Armenia } 3292a2f7404SAndrew Armenia 3302a2f7404SAndrew Armenia dev_info(&PIIX4_dev->dev, 3312a2f7404SAndrew Armenia "Auxiliary SMBus Host Controller at 0x%x\n", 3322a2f7404SAndrew Armenia piix4_smba); 3332a2f7404SAndrew Armenia 3342a2f7404SAndrew Armenia return piix4_smba; 3352a2f7404SAndrew Armenia } 3362a2f7404SAndrew Armenia 337e154bf6fSAndrew Armenia static int piix4_transaction(struct i2c_adapter *piix4_adapter) 3381da177e4SLinus Torvalds { 339e154bf6fSAndrew Armenia struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(piix4_adapter); 340e154bf6fSAndrew Armenia unsigned short piix4_smba = adapdata->smba; 3411da177e4SLinus Torvalds int temp; 3421da177e4SLinus Torvalds int result = 0; 3431da177e4SLinus Torvalds int timeout = 0; 3441da177e4SLinus Torvalds 345e154bf6fSAndrew Armenia dev_dbg(&piix4_adapter->dev, "Transaction (pre): CNT=%02x, CMD=%02x, " 3461da177e4SLinus Torvalds "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT), 3471da177e4SLinus Torvalds inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), 3481da177e4SLinus Torvalds inb_p(SMBHSTDAT1)); 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds /* Make sure the SMBus host is ready to start transmitting */ 3511da177e4SLinus Torvalds if ((temp = inb_p(SMBHSTSTS)) != 0x00) { 352e154bf6fSAndrew Armenia dev_dbg(&piix4_adapter->dev, "SMBus busy (%02x). " 3531da177e4SLinus Torvalds "Resetting...\n", temp); 3541da177e4SLinus Torvalds outb_p(temp, SMBHSTSTS); 3551da177e4SLinus Torvalds if ((temp = inb_p(SMBHSTSTS)) != 0x00) { 356e154bf6fSAndrew Armenia dev_err(&piix4_adapter->dev, "Failed! (%02x)\n", temp); 35797140342SDavid Brownell return -EBUSY; 3581da177e4SLinus Torvalds } else { 359e154bf6fSAndrew Armenia dev_dbg(&piix4_adapter->dev, "Successful!\n"); 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds /* start the transaction by setting bit 6 */ 3641da177e4SLinus Torvalds outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT); 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */ 367b1c1759cSDavid Milburn if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */ 368b1c1759cSDavid Milburn msleep(2); 369b1c1759cSDavid Milburn else 3701da177e4SLinus Torvalds msleep(1); 371b1c1759cSDavid Milburn 372b6a31950SRoel Kluin while ((++timeout < MAX_TIMEOUT) && 373b1c1759cSDavid Milburn ((temp = inb_p(SMBHSTSTS)) & 0x01)) 374b1c1759cSDavid Milburn msleep(1); 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds /* If the SMBus is still busy, we give up */ 377b6a31950SRoel Kluin if (timeout == MAX_TIMEOUT) { 378e154bf6fSAndrew Armenia dev_err(&piix4_adapter->dev, "SMBus Timeout!\n"); 37997140342SDavid Brownell result = -ETIMEDOUT; 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds if (temp & 0x10) { 38397140342SDavid Brownell result = -EIO; 384e154bf6fSAndrew Armenia dev_err(&piix4_adapter->dev, "Error: Failed bus transaction\n"); 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds if (temp & 0x08) { 38897140342SDavid Brownell result = -EIO; 389e154bf6fSAndrew Armenia dev_dbg(&piix4_adapter->dev, "Bus collision! SMBus may be " 3901da177e4SLinus Torvalds "locked until next hard reset. (sorry!)\n"); 3911da177e4SLinus Torvalds /* Clock stops and slave is stuck in mid-transmission */ 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds if (temp & 0x04) { 39597140342SDavid Brownell result = -ENXIO; 396e154bf6fSAndrew Armenia dev_dbg(&piix4_adapter->dev, "Error: no response!\n"); 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds if (inb_p(SMBHSTSTS) != 0x00) 4001da177e4SLinus Torvalds outb_p(inb(SMBHSTSTS), SMBHSTSTS); 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds if ((temp = inb_p(SMBHSTSTS)) != 0x00) { 403e154bf6fSAndrew Armenia dev_err(&piix4_adapter->dev, "Failed reset at end of " 4041da177e4SLinus Torvalds "transaction (%02x)\n", temp); 4051da177e4SLinus Torvalds } 406e154bf6fSAndrew Armenia dev_dbg(&piix4_adapter->dev, "Transaction (post): CNT=%02x, CMD=%02x, " 4071da177e4SLinus Torvalds "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT), 4081da177e4SLinus Torvalds inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), 4091da177e4SLinus Torvalds inb_p(SMBHSTDAT1)); 4101da177e4SLinus Torvalds return result; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 41397140342SDavid Brownell /* Return negative errno on error. */ 4141da177e4SLinus Torvalds static s32 piix4_access(struct i2c_adapter * adap, u16 addr, 4151da177e4SLinus Torvalds unsigned short flags, char read_write, 4161da177e4SLinus Torvalds u8 command, int size, union i2c_smbus_data * data) 4171da177e4SLinus Torvalds { 41814a8086dSAndrew Armenia struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap); 41914a8086dSAndrew Armenia unsigned short piix4_smba = adapdata->smba; 4201da177e4SLinus Torvalds int i, len; 42197140342SDavid Brownell int status; 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds switch (size) { 4241da177e4SLinus Torvalds case I2C_SMBUS_QUICK: 425fa63cd56SJean Delvare outb_p((addr << 1) | read_write, 4261da177e4SLinus Torvalds SMBHSTADD); 4271da177e4SLinus Torvalds size = PIIX4_QUICK; 4281da177e4SLinus Torvalds break; 4291da177e4SLinus Torvalds case I2C_SMBUS_BYTE: 430fa63cd56SJean Delvare outb_p((addr << 1) | read_write, 4311da177e4SLinus Torvalds SMBHSTADD); 4321da177e4SLinus Torvalds if (read_write == I2C_SMBUS_WRITE) 4331da177e4SLinus Torvalds outb_p(command, SMBHSTCMD); 4341da177e4SLinus Torvalds size = PIIX4_BYTE; 4351da177e4SLinus Torvalds break; 4361da177e4SLinus Torvalds case I2C_SMBUS_BYTE_DATA: 437fa63cd56SJean Delvare outb_p((addr << 1) | read_write, 4381da177e4SLinus Torvalds SMBHSTADD); 4391da177e4SLinus Torvalds outb_p(command, SMBHSTCMD); 4401da177e4SLinus Torvalds if (read_write == I2C_SMBUS_WRITE) 4411da177e4SLinus Torvalds outb_p(data->byte, SMBHSTDAT0); 4421da177e4SLinus Torvalds size = PIIX4_BYTE_DATA; 4431da177e4SLinus Torvalds break; 4441da177e4SLinus Torvalds case I2C_SMBUS_WORD_DATA: 445fa63cd56SJean Delvare outb_p((addr << 1) | read_write, 4461da177e4SLinus Torvalds SMBHSTADD); 4471da177e4SLinus Torvalds outb_p(command, SMBHSTCMD); 4481da177e4SLinus Torvalds if (read_write == I2C_SMBUS_WRITE) { 4491da177e4SLinus Torvalds outb_p(data->word & 0xff, SMBHSTDAT0); 4501da177e4SLinus Torvalds outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1); 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds size = PIIX4_WORD_DATA; 4531da177e4SLinus Torvalds break; 4541da177e4SLinus Torvalds case I2C_SMBUS_BLOCK_DATA: 455fa63cd56SJean Delvare outb_p((addr << 1) | read_write, 4561da177e4SLinus Torvalds SMBHSTADD); 4571da177e4SLinus Torvalds outb_p(command, SMBHSTCMD); 4581da177e4SLinus Torvalds if (read_write == I2C_SMBUS_WRITE) { 4591da177e4SLinus Torvalds len = data->block[0]; 460fa63cd56SJean Delvare if (len == 0 || len > I2C_SMBUS_BLOCK_MAX) 461fa63cd56SJean Delvare return -EINVAL; 4621da177e4SLinus Torvalds outb_p(len, SMBHSTDAT0); 4631da177e4SLinus Torvalds i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */ 4641da177e4SLinus Torvalds for (i = 1; i <= len; i++) 4651da177e4SLinus Torvalds outb_p(data->block[i], SMBBLKDAT); 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds size = PIIX4_BLOCK_DATA; 4681da177e4SLinus Torvalds break; 469ac7fc4fbSJean Delvare default: 470ac7fc4fbSJean Delvare dev_warn(&adap->dev, "Unsupported transaction %d\n", size); 471ac7fc4fbSJean Delvare return -EOPNOTSUPP; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT); 4751da177e4SLinus Torvalds 476e154bf6fSAndrew Armenia status = piix4_transaction(adap); 47797140342SDavid Brownell if (status) 47897140342SDavid Brownell return status; 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK)) 4811da177e4SLinus Torvalds return 0; 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds switch (size) { 4853578a075SJean Delvare case PIIX4_BYTE: 4861da177e4SLinus Torvalds case PIIX4_BYTE_DATA: 4871da177e4SLinus Torvalds data->byte = inb_p(SMBHSTDAT0); 4881da177e4SLinus Torvalds break; 4891da177e4SLinus Torvalds case PIIX4_WORD_DATA: 4901da177e4SLinus Torvalds data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8); 4911da177e4SLinus Torvalds break; 4921da177e4SLinus Torvalds case PIIX4_BLOCK_DATA: 4931da177e4SLinus Torvalds data->block[0] = inb_p(SMBHSTDAT0); 494fa63cd56SJean Delvare if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX) 495fa63cd56SJean Delvare return -EPROTO; 4961da177e4SLinus Torvalds i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */ 4971da177e4SLinus Torvalds for (i = 1; i <= data->block[0]; i++) 4981da177e4SLinus Torvalds data->block[i] = inb_p(SMBBLKDAT); 4991da177e4SLinus Torvalds break; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds return 0; 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds static u32 piix4_func(struct i2c_adapter *adapter) 5051da177e4SLinus Torvalds { 5061da177e4SLinus Torvalds return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | 5071da177e4SLinus Torvalds I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | 5081da177e4SLinus Torvalds I2C_FUNC_SMBUS_BLOCK_DATA; 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 5118f9082c5SJean Delvare static const struct i2c_algorithm smbus_algorithm = { 5121da177e4SLinus Torvalds .smbus_xfer = piix4_access, 5131da177e4SLinus Torvalds .functionality = piix4_func, 5141da177e4SLinus Torvalds }; 5151da177e4SLinus Torvalds 5163527bd50SAxel Lin static DEFINE_PCI_DEVICE_TABLE(piix4_ids) = { 5179b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) }, 5189b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3) }, 5199b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3) }, 5209b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_SMBUS) }, 5219b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_SMBUS) }, 5229b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS) }, 5239b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS) }, 5243806e94bSCrane Cai { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) }, 5259b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, 5269b7389c0SJean Delvare PCI_DEVICE_ID_SERVERWORKS_OSB4) }, 5279b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, 5289b7389c0SJean Delvare PCI_DEVICE_ID_SERVERWORKS_CSB5) }, 5299b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, 5309b7389c0SJean Delvare PCI_DEVICE_ID_SERVERWORKS_CSB6) }, 5319b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, 5329b7389c0SJean Delvare PCI_DEVICE_ID_SERVERWORKS_HT1000SB) }, 533506a8b6cSFlavio Leitner { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, 534506a8b6cSFlavio Leitner PCI_DEVICE_ID_SERVERWORKS_HT1100LD) }, 5351da177e4SLinus Torvalds { 0, } 5361da177e4SLinus Torvalds }; 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds MODULE_DEVICE_TABLE (pci, piix4_ids); 5391da177e4SLinus Torvalds 540e154bf6fSAndrew Armenia static struct i2c_adapter *piix4_main_adapter; 5412a2f7404SAndrew Armenia static struct i2c_adapter *piix4_aux_adapter; 542e154bf6fSAndrew Armenia 543e154bf6fSAndrew Armenia static int __devinit piix4_add_adapter(struct pci_dev *dev, 544e154bf6fSAndrew Armenia unsigned short smba, 545e154bf6fSAndrew Armenia struct i2c_adapter **padap) 546e154bf6fSAndrew Armenia { 547e154bf6fSAndrew Armenia struct i2c_adapter *adap; 548e154bf6fSAndrew Armenia struct i2c_piix4_adapdata *adapdata; 549e154bf6fSAndrew Armenia int retval; 550e154bf6fSAndrew Armenia 551e154bf6fSAndrew Armenia adap = kzalloc(sizeof(*adap), GFP_KERNEL); 552e154bf6fSAndrew Armenia if (adap == NULL) { 553e154bf6fSAndrew Armenia release_region(smba, SMBIOSIZE); 554e154bf6fSAndrew Armenia return -ENOMEM; 555e154bf6fSAndrew Armenia } 556e154bf6fSAndrew Armenia 557e154bf6fSAndrew Armenia adap->owner = THIS_MODULE; 558e154bf6fSAndrew Armenia adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; 559e154bf6fSAndrew Armenia adap->algo = &smbus_algorithm; 560e154bf6fSAndrew Armenia 561e154bf6fSAndrew Armenia adapdata = kzalloc(sizeof(*adapdata), GFP_KERNEL); 562e154bf6fSAndrew Armenia if (adapdata == NULL) { 563e154bf6fSAndrew Armenia kfree(adap); 564e154bf6fSAndrew Armenia release_region(smba, SMBIOSIZE); 565e154bf6fSAndrew Armenia return -ENOMEM; 566e154bf6fSAndrew Armenia } 567e154bf6fSAndrew Armenia 568e154bf6fSAndrew Armenia adapdata->smba = smba; 569e154bf6fSAndrew Armenia 570e154bf6fSAndrew Armenia /* set up the sysfs linkage to our parent device */ 571e154bf6fSAndrew Armenia adap->dev.parent = &dev->dev; 572e154bf6fSAndrew Armenia 573e154bf6fSAndrew Armenia snprintf(adap->name, sizeof(adap->name), 574e154bf6fSAndrew Armenia "SMBus PIIX4 adapter at %04x", smba); 575e154bf6fSAndrew Armenia 576e154bf6fSAndrew Armenia i2c_set_adapdata(adap, adapdata); 577e154bf6fSAndrew Armenia 578e154bf6fSAndrew Armenia retval = i2c_add_adapter(adap); 579e154bf6fSAndrew Armenia if (retval) { 580e154bf6fSAndrew Armenia dev_err(&dev->dev, "Couldn't register adapter!\n"); 581e154bf6fSAndrew Armenia kfree(adapdata); 582e154bf6fSAndrew Armenia kfree(adap); 583e154bf6fSAndrew Armenia release_region(smba, SMBIOSIZE); 584e154bf6fSAndrew Armenia return retval; 585e154bf6fSAndrew Armenia } 586e154bf6fSAndrew Armenia 587e154bf6fSAndrew Armenia *padap = adap; 588e154bf6fSAndrew Armenia return 0; 589e154bf6fSAndrew Armenia } 590e154bf6fSAndrew Armenia 5911da177e4SLinus Torvalds static int __devinit piix4_probe(struct pci_dev *dev, 5921da177e4SLinus Torvalds const struct pci_device_id *id) 5931da177e4SLinus Torvalds { 5941da177e4SLinus Torvalds int retval; 5951da177e4SLinus Torvalds 59676b3e28fSCrane Cai if ((dev->vendor == PCI_VENDOR_ID_ATI && 59776b3e28fSCrane Cai dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS && 59876b3e28fSCrane Cai dev->revision >= 0x40) || 59976b3e28fSCrane Cai dev->vendor == PCI_VENDOR_ID_AMD) 60087e1960eSShane Huang /* base address location etc changed in SB800 */ 60187e1960eSShane Huang retval = piix4_setup_sb800(dev, id); 60287e1960eSShane Huang else 6031da177e4SLinus Torvalds retval = piix4_setup(dev, id); 60487e1960eSShane Huang 6052a2f7404SAndrew Armenia /* If no main SMBus found, give up */ 60614a8086dSAndrew Armenia if (retval < 0) 6071da177e4SLinus Torvalds return retval; 6081da177e4SLinus Torvalds 6092a2f7404SAndrew Armenia /* Try to register main SMBus adapter, give up if we can't */ 6102a2f7404SAndrew Armenia retval = piix4_add_adapter(dev, retval, &piix4_main_adapter); 6112a2f7404SAndrew Armenia if (retval < 0) 6122a2f7404SAndrew Armenia return retval; 6132a2f7404SAndrew Armenia 6142a2f7404SAndrew Armenia /* Check for auxiliary SMBus on some AMD chipsets */ 6152a2f7404SAndrew Armenia if (dev->vendor == PCI_VENDOR_ID_ATI && 6162a2f7404SAndrew Armenia dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS && 6172a2f7404SAndrew Armenia dev->revision < 0x40) { 6182a2f7404SAndrew Armenia retval = piix4_setup_aux(dev, id, 0x58); 6192a2f7404SAndrew Armenia if (retval > 0) { 6202a2f7404SAndrew Armenia /* Try to add the aux adapter if it exists, 6212a2f7404SAndrew Armenia * piix4_add_adapter will clean up if this fails */ 6222a2f7404SAndrew Armenia piix4_add_adapter(dev, retval, &piix4_aux_adapter); 6232a2f7404SAndrew Armenia } 6242a2f7404SAndrew Armenia } 6252a2f7404SAndrew Armenia 6262a2f7404SAndrew Armenia return 0; 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds 62914a8086dSAndrew Armenia static void __devexit piix4_adap_remove(struct i2c_adapter *adap) 63014a8086dSAndrew Armenia { 63114a8086dSAndrew Armenia struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap); 63214a8086dSAndrew Armenia 63314a8086dSAndrew Armenia if (adapdata->smba) { 63414a8086dSAndrew Armenia i2c_del_adapter(adap); 63514a8086dSAndrew Armenia release_region(adapdata->smba, SMBIOSIZE); 636e154bf6fSAndrew Armenia kfree(adapdata); 637e154bf6fSAndrew Armenia kfree(adap); 63814a8086dSAndrew Armenia } 63914a8086dSAndrew Armenia } 64014a8086dSAndrew Armenia 6411da177e4SLinus Torvalds static void __devexit piix4_remove(struct pci_dev *dev) 6421da177e4SLinus Torvalds { 643e154bf6fSAndrew Armenia if (piix4_main_adapter) { 644e154bf6fSAndrew Armenia piix4_adap_remove(piix4_main_adapter); 645e154bf6fSAndrew Armenia piix4_main_adapter = NULL; 646e154bf6fSAndrew Armenia } 6472a2f7404SAndrew Armenia 6482a2f7404SAndrew Armenia if (piix4_aux_adapter) { 6492a2f7404SAndrew Armenia piix4_adap_remove(piix4_aux_adapter); 6502a2f7404SAndrew Armenia piix4_aux_adapter = NULL; 6512a2f7404SAndrew Armenia } 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds static struct pci_driver piix4_driver = { 6551da177e4SLinus Torvalds .name = "piix4_smbus", 6561da177e4SLinus Torvalds .id_table = piix4_ids, 6571da177e4SLinus Torvalds .probe = piix4_probe, 6581da177e4SLinus Torvalds .remove = __devexit_p(piix4_remove), 6591da177e4SLinus Torvalds }; 6601da177e4SLinus Torvalds 66156f21788SAxel Lin module_pci_driver(piix4_driver); 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and " 6641da177e4SLinus Torvalds "Philip Edelbrock <phil@netroedge.com>"); 6651da177e4SLinus Torvalds MODULE_DESCRIPTION("PIIX4 SMBus driver"); 6661da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 667