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 161da177e4SLinus Torvalds /* 171da177e4SLinus Torvalds Supports: 181da177e4SLinus Torvalds Intel PIIX4, 440MX 19506a8b6cSFlavio Leitner Serverworks OSB4, CSB5, CSB6, HT-1000, HT-1100 202a2f7404SAndrew Armenia ATI IXP200, IXP300, IXP400, SB600, SB700/SP5100, SB800 21032f708bSShane Huang AMD Hudson-2, ML, CZ 221da177e4SLinus Torvalds SMSC Victory66 231da177e4SLinus Torvalds 242a2f7404SAndrew Armenia Note: we assume there can only be one device, with one or more 252a2f7404SAndrew Armenia SMBus interfaces. 262fee61d2SChristian Fetzer The device can register multiple i2c_adapters (up to PIIX4_MAX_ADAPTERS). 272fee61d2SChristian Fetzer For devices supporting multiple ports the i2c_adapter should provide 282fee61d2SChristian Fetzer an i2c_algorithm to access them. 291da177e4SLinus Torvalds */ 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds #include <linux/module.h> 321da177e4SLinus Torvalds #include <linux/moduleparam.h> 331da177e4SLinus Torvalds #include <linux/pci.h> 341da177e4SLinus Torvalds #include <linux/kernel.h> 351da177e4SLinus Torvalds #include <linux/delay.h> 361da177e4SLinus Torvalds #include <linux/stddef.h> 371da177e4SLinus Torvalds #include <linux/ioport.h> 381da177e4SLinus Torvalds #include <linux/i2c.h> 39c415b303SDaniel J Blueman #include <linux/slab.h> 401da177e4SLinus Torvalds #include <linux/dmi.h> 4154fb4a05SJean Delvare #include <linux/acpi.h> 4221782180SH Hartley Sweeten #include <linux/io.h> 432fee61d2SChristian Fetzer #include <linux/mutex.h> 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds /* PIIX4 SMBus address offsets */ 471da177e4SLinus Torvalds #define SMBHSTSTS (0 + piix4_smba) 481da177e4SLinus Torvalds #define SMBHSLVSTS (1 + piix4_smba) 491da177e4SLinus Torvalds #define SMBHSTCNT (2 + piix4_smba) 501da177e4SLinus Torvalds #define SMBHSTCMD (3 + piix4_smba) 511da177e4SLinus Torvalds #define SMBHSTADD (4 + piix4_smba) 521da177e4SLinus Torvalds #define SMBHSTDAT0 (5 + piix4_smba) 531da177e4SLinus Torvalds #define SMBHSTDAT1 (6 + piix4_smba) 541da177e4SLinus Torvalds #define SMBBLKDAT (7 + piix4_smba) 551da177e4SLinus Torvalds #define SMBSLVCNT (8 + piix4_smba) 561da177e4SLinus Torvalds #define SMBSHDWCMD (9 + piix4_smba) 571da177e4SLinus Torvalds #define SMBSLVEVT (0xA + piix4_smba) 581da177e4SLinus Torvalds #define SMBSLVDAT (0xC + piix4_smba) 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds /* count for request_region */ 611da177e4SLinus Torvalds #define SMBIOSIZE 8 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds /* PCI Address Constants */ 641da177e4SLinus Torvalds #define SMBBA 0x090 651da177e4SLinus Torvalds #define SMBHSTCFG 0x0D2 661da177e4SLinus Torvalds #define SMBSLVC 0x0D3 671da177e4SLinus Torvalds #define SMBSHDW1 0x0D4 681da177e4SLinus Torvalds #define SMBSHDW2 0x0D5 691da177e4SLinus Torvalds #define SMBREV 0x0D6 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds /* Other settings */ 721da177e4SLinus Torvalds #define MAX_TIMEOUT 500 731da177e4SLinus Torvalds #define ENABLE_INT9 0 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds /* PIIX4 constants */ 761da177e4SLinus Torvalds #define PIIX4_QUICK 0x00 771da177e4SLinus Torvalds #define PIIX4_BYTE 0x04 781da177e4SLinus Torvalds #define PIIX4_BYTE_DATA 0x08 791da177e4SLinus Torvalds #define PIIX4_WORD_DATA 0x0C 801da177e4SLinus Torvalds #define PIIX4_BLOCK_DATA 0x14 811da177e4SLinus Torvalds 82ca2061e1SChristian Fetzer /* Multi-port constants */ 83ca2061e1SChristian Fetzer #define PIIX4_MAX_ADAPTERS 4 84ca2061e1SChristian Fetzer 852fee61d2SChristian Fetzer /* SB800 constants */ 862fee61d2SChristian Fetzer #define SB800_PIIX4_SMB_IDX 0xcd6 872fee61d2SChristian Fetzer 886befa3fdSJean Delvare /* 896befa3fdSJean Delvare * SB800 port is selected by bits 2:1 of the smb_en register (0x2c) 906befa3fdSJean Delvare * or the smb_sel register (0x2e), depending on bit 0 of register 0x2f. 916befa3fdSJean Delvare * Hudson-2/Bolton port is always selected by bits 2:1 of register 0x2f. 926befa3fdSJean Delvare */ 932fee61d2SChristian Fetzer #define SB800_PIIX4_PORT_IDX 0x2c 946befa3fdSJean Delvare #define SB800_PIIX4_PORT_IDX_ALT 0x2e 956befa3fdSJean Delvare #define SB800_PIIX4_PORT_IDX_SEL 0x2f 962fee61d2SChristian Fetzer #define SB800_PIIX4_PORT_IDX_MASK 0x06 972fee61d2SChristian Fetzer 981da177e4SLinus Torvalds /* insmod parameters */ 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /* If force is set to anything different from 0, we forcibly enable the 1011da177e4SLinus Torvalds PIIX4. DANGEROUS! */ 10260507095SJean Delvare static int force; 1031da177e4SLinus Torvalds module_param (force, int, 0); 1041da177e4SLinus Torvalds MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!"); 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /* If force_addr is set to anything different from 0, we forcibly enable 1071da177e4SLinus Torvalds the PIIX4 at the given address. VERY DANGEROUS! */ 10860507095SJean Delvare static int force_addr; 1091da177e4SLinus Torvalds module_param (force_addr, int, 0); 1101da177e4SLinus Torvalds MODULE_PARM_DESC(force_addr, 1111da177e4SLinus Torvalds "Forcibly enable the PIIX4 at the given address. " 1121da177e4SLinus Torvalds "EXTREMELY DANGEROUS!"); 1131da177e4SLinus Torvalds 114b1c1759cSDavid Milburn static int srvrworks_csb5_delay; 115d6072f84SJean Delvare static struct pci_driver piix4_driver; 1161da177e4SLinus Torvalds 1170b255e92SBill Pemberton static const struct dmi_system_id piix4_dmi_blacklist[] = { 118c2fc54fcSJean Delvare { 119c2fc54fcSJean Delvare .ident = "Sapphire AM2RD790", 120c2fc54fcSJean Delvare .matches = { 121c2fc54fcSJean Delvare DMI_MATCH(DMI_BOARD_VENDOR, "SAPPHIRE Inc."), 122c2fc54fcSJean Delvare DMI_MATCH(DMI_BOARD_NAME, "PC-AM2RD790"), 123c2fc54fcSJean Delvare }, 124c2fc54fcSJean Delvare }, 125c2fc54fcSJean Delvare { 126c2fc54fcSJean Delvare .ident = "DFI Lanparty UT 790FX", 127c2fc54fcSJean Delvare .matches = { 128c2fc54fcSJean Delvare DMI_MATCH(DMI_BOARD_VENDOR, "DFI Inc."), 129c2fc54fcSJean Delvare DMI_MATCH(DMI_BOARD_NAME, "LP UT 790FX"), 130c2fc54fcSJean Delvare }, 131c2fc54fcSJean Delvare }, 132c2fc54fcSJean Delvare { } 133c2fc54fcSJean Delvare }; 134c2fc54fcSJean Delvare 135c2fc54fcSJean Delvare /* The IBM entry is in a separate table because we only check it 136c2fc54fcSJean Delvare on Intel-based systems */ 1370b255e92SBill Pemberton static const struct dmi_system_id piix4_dmi_ibm[] = { 1381da177e4SLinus Torvalds { 1391da177e4SLinus Torvalds .ident = "IBM", 1401da177e4SLinus Torvalds .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), }, 1411da177e4SLinus Torvalds }, 1421da177e4SLinus Torvalds { }, 1431da177e4SLinus Torvalds }; 1441da177e4SLinus Torvalds 1456befa3fdSJean Delvare /* 1466befa3fdSJean Delvare * SB800 globals 1476befa3fdSJean Delvare * piix4_mutex_sb800 protects piix4_port_sel_sb800 and the pair 1486befa3fdSJean Delvare * of I/O ports at SB800_PIIX4_SMB_IDX. 1496befa3fdSJean Delvare */ 150a28e3517SJean Delvare static DEFINE_MUTEX(piix4_mutex_sb800); 1516befa3fdSJean Delvare static u8 piix4_port_sel_sb800; 152725d2e3fSChristian Fetzer static const char *piix4_main_port_names_sb800[PIIX4_MAX_ADAPTERS] = { 15352795f6fSJean Delvare " port 0", " port 2", " port 3", " port 4" 154725d2e3fSChristian Fetzer }; 15552795f6fSJean Delvare static const char *piix4_aux_port_name_sb800 = " port 1"; 156725d2e3fSChristian Fetzer 15714a8086dSAndrew Armenia struct i2c_piix4_adapdata { 15814a8086dSAndrew Armenia unsigned short smba; 1592fee61d2SChristian Fetzer 1602fee61d2SChristian Fetzer /* SB800 */ 1612fee61d2SChristian Fetzer bool sb800_main; 16233f5ccc3SJean Delvare u8 port; /* Port number, shifted */ 16314a8086dSAndrew Armenia }; 16414a8086dSAndrew Armenia 1650b255e92SBill Pemberton static int piix4_setup(struct pci_dev *PIIX4_dev, 1661da177e4SLinus Torvalds const struct pci_device_id *id) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds unsigned char temp; 16914a8086dSAndrew Armenia unsigned short piix4_smba; 1701da177e4SLinus Torvalds 171b1c1759cSDavid Milburn if ((PIIX4_dev->vendor == PCI_VENDOR_ID_SERVERWORKS) && 172b1c1759cSDavid Milburn (PIIX4_dev->device == PCI_DEVICE_ID_SERVERWORKS_CSB5)) 173b1c1759cSDavid Milburn srvrworks_csb5_delay = 1; 174b1c1759cSDavid Milburn 175c2fc54fcSJean Delvare /* On some motherboards, it was reported that accessing the SMBus 176c2fc54fcSJean Delvare caused severe hardware problems */ 177c2fc54fcSJean Delvare if (dmi_check_system(piix4_dmi_blacklist)) { 178c2fc54fcSJean Delvare dev_err(&PIIX4_dev->dev, 179c2fc54fcSJean Delvare "Accessing the SMBus on this system is unsafe!\n"); 180c2fc54fcSJean Delvare return -EPERM; 181c2fc54fcSJean Delvare } 182c2fc54fcSJean Delvare 1831da177e4SLinus Torvalds /* Don't access SMBus on IBM systems which get corrupted eeproms */ 184c2fc54fcSJean Delvare if (dmi_check_system(piix4_dmi_ibm) && 1851da177e4SLinus Torvalds PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) { 186f9ba6c04SJean Delvare dev_err(&PIIX4_dev->dev, "IBM system detected; this module " 1871da177e4SLinus Torvalds "may corrupt your serial eeprom! Refusing to load " 1881da177e4SLinus Torvalds "module!\n"); 1891da177e4SLinus Torvalds return -EPERM; 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds /* Determine the address of the SMBus areas */ 1931da177e4SLinus Torvalds if (force_addr) { 1941da177e4SLinus Torvalds piix4_smba = force_addr & 0xfff0; 1951da177e4SLinus Torvalds force = 0; 1961da177e4SLinus Torvalds } else { 1971da177e4SLinus Torvalds pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba); 1981da177e4SLinus Torvalds piix4_smba &= 0xfff0; 1991da177e4SLinus Torvalds if(piix4_smba == 0) { 200fa63cd56SJean Delvare dev_err(&PIIX4_dev->dev, "SMBus base address " 2011da177e4SLinus Torvalds "uninitialized - upgrade BIOS or use " 2021da177e4SLinus Torvalds "force_addr=0xaddr\n"); 2031da177e4SLinus Torvalds return -ENODEV; 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds 20754fb4a05SJean Delvare if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) 20818669eabSJean Delvare return -ENODEV; 20954fb4a05SJean Delvare 210d6072f84SJean Delvare if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) { 211fa63cd56SJean Delvare dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n", 2121da177e4SLinus Torvalds piix4_smba); 213fa63cd56SJean Delvare return -EBUSY; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp); 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds /* If force_addr is set, we program the new address here. Just to make 2191da177e4SLinus Torvalds sure, we disable the PIIX4 first. */ 2201da177e4SLinus Torvalds if (force_addr) { 2211da177e4SLinus Torvalds pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe); 2221da177e4SLinus Torvalds pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba); 2231da177e4SLinus Torvalds pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01); 2241da177e4SLinus Torvalds dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to " 2251da177e4SLinus Torvalds "new address %04x!\n", piix4_smba); 2261da177e4SLinus Torvalds } else if ((temp & 1) == 0) { 2271da177e4SLinus Torvalds if (force) { 2281da177e4SLinus Torvalds /* This should never need to be done, but has been 2291da177e4SLinus Torvalds * noted that many Dell machines have the SMBus 2301da177e4SLinus Torvalds * interface on the PIIX4 disabled!? NOTE: This assumes 2311da177e4SLinus Torvalds * I/O space and other allocations WERE done by the 2321da177e4SLinus Torvalds * Bios! Don't complain if your hardware does weird 2331da177e4SLinus Torvalds * things after enabling this. :') Check for Bios 2341da177e4SLinus Torvalds * updates before resorting to this. 2351da177e4SLinus Torvalds */ 2361da177e4SLinus Torvalds pci_write_config_byte(PIIX4_dev, SMBHSTCFG, 2371da177e4SLinus Torvalds temp | 1); 2388117e41eSJoe Perches dev_notice(&PIIX4_dev->dev, 2398117e41eSJoe Perches "WARNING: SMBus interface has been FORCEFULLY ENABLED!\n"); 2401da177e4SLinus Torvalds } else { 2411da177e4SLinus Torvalds dev_err(&PIIX4_dev->dev, 24266f8a8ffSJean Delvare "SMBus Host Controller not enabled!\n"); 2431da177e4SLinus Torvalds release_region(piix4_smba, SMBIOSIZE); 2441da177e4SLinus Torvalds return -ENODEV; 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 24854aaa1caSRudolf Marek if (((temp & 0x0E) == 8) || ((temp & 0x0E) == 2)) 24966f8a8ffSJean Delvare dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n"); 2501da177e4SLinus Torvalds else if ((temp & 0x0E) == 0) 25166f8a8ffSJean Delvare dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n"); 2521da177e4SLinus Torvalds else 2531da177e4SLinus Torvalds dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration " 2541da177e4SLinus Torvalds "(or code out of date)!\n"); 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds pci_read_config_byte(PIIX4_dev, SMBREV, &temp); 257fa63cd56SJean Delvare dev_info(&PIIX4_dev->dev, 258fa63cd56SJean Delvare "SMBus Host Controller at 0x%x, revision %d\n", 259fa63cd56SJean Delvare piix4_smba, temp); 2601da177e4SLinus Torvalds 26114a8086dSAndrew Armenia return piix4_smba; 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds 2640b255e92SBill Pemberton static int piix4_setup_sb800(struct pci_dev *PIIX4_dev, 265a94dd00fSRudolf Marek const struct pci_device_id *id, u8 aux) 26687e1960eSShane Huang { 26714a8086dSAndrew Armenia unsigned short piix4_smba; 2686befa3fdSJean Delvare u8 smba_en_lo, smba_en_hi, smb_en, smb_en_status, port_sel; 269032f708bSShane Huang u8 i2ccfg, i2ccfg_offset = 0x10; 27087e1960eSShane Huang 2713806e94bSCrane Cai /* SB800 and later SMBus does not support forcing address */ 27287e1960eSShane Huang if (force || force_addr) { 2733806e94bSCrane Cai dev_err(&PIIX4_dev->dev, "SMBus does not support " 27487e1960eSShane Huang "forcing address!\n"); 27587e1960eSShane Huang return -EINVAL; 27687e1960eSShane Huang } 27787e1960eSShane Huang 27887e1960eSShane Huang /* Determine the address of the SMBus areas */ 279032f708bSShane Huang if ((PIIX4_dev->vendor == PCI_VENDOR_ID_AMD && 280032f708bSShane Huang PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS && 281032f708bSShane Huang PIIX4_dev->revision >= 0x41) || 282032f708bSShane Huang (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD && 283bcb29994SVincent Wan PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS && 284032f708bSShane Huang PIIX4_dev->revision >= 0x49)) 285032f708bSShane Huang smb_en = 0x00; 286032f708bSShane Huang else 287a94dd00fSRudolf Marek smb_en = (aux) ? 0x28 : 0x2c; 288a94dd00fSRudolf Marek 289a28e3517SJean Delvare mutex_lock(&piix4_mutex_sb800); 2902fee61d2SChristian Fetzer outb_p(smb_en, SB800_PIIX4_SMB_IDX); 2912fee61d2SChristian Fetzer smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1); 2922fee61d2SChristian Fetzer outb_p(smb_en + 1, SB800_PIIX4_SMB_IDX); 2932fee61d2SChristian Fetzer smba_en_hi = inb_p(SB800_PIIX4_SMB_IDX + 1); 294a28e3517SJean Delvare mutex_unlock(&piix4_mutex_sb800); 29587e1960eSShane Huang 296032f708bSShane Huang if (!smb_en) { 297032f708bSShane Huang smb_en_status = smba_en_lo & 0x10; 298032f708bSShane Huang piix4_smba = smba_en_hi << 8; 299032f708bSShane Huang if (aux) 300032f708bSShane Huang piix4_smba |= 0x20; 301032f708bSShane Huang } else { 302032f708bSShane Huang smb_en_status = smba_en_lo & 0x01; 303032f708bSShane Huang piix4_smba = ((smba_en_hi << 8) | smba_en_lo) & 0xffe0; 304032f708bSShane Huang } 305032f708bSShane Huang 306032f708bSShane Huang if (!smb_en_status) { 30787e1960eSShane Huang dev_err(&PIIX4_dev->dev, 30866f8a8ffSJean Delvare "SMBus Host Controller not enabled!\n"); 30987e1960eSShane Huang return -ENODEV; 31087e1960eSShane Huang } 31187e1960eSShane Huang 31287e1960eSShane Huang if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) 31318669eabSJean Delvare return -ENODEV; 31487e1960eSShane Huang 31587e1960eSShane Huang if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) { 31687e1960eSShane Huang dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n", 31787e1960eSShane Huang piix4_smba); 31887e1960eSShane Huang return -EBUSY; 31987e1960eSShane Huang } 32087e1960eSShane Huang 321a94dd00fSRudolf Marek /* Aux SMBus does not support IRQ information */ 322a94dd00fSRudolf Marek if (aux) { 323a94dd00fSRudolf Marek dev_info(&PIIX4_dev->dev, 32485fd0fe6SShane Huang "Auxiliary SMBus Host Controller at 0x%x\n", 32585fd0fe6SShane Huang piix4_smba); 326a94dd00fSRudolf Marek return piix4_smba; 327a94dd00fSRudolf Marek } 328a94dd00fSRudolf Marek 32987e1960eSShane Huang /* Request the SMBus I2C bus config region */ 33087e1960eSShane Huang if (!request_region(piix4_smba + i2ccfg_offset, 1, "i2ccfg")) { 33187e1960eSShane Huang dev_err(&PIIX4_dev->dev, "SMBus I2C bus config region " 33287e1960eSShane Huang "0x%x already in use!\n", piix4_smba + i2ccfg_offset); 33387e1960eSShane Huang release_region(piix4_smba, SMBIOSIZE); 33487e1960eSShane Huang return -EBUSY; 33587e1960eSShane Huang } 33687e1960eSShane Huang i2ccfg = inb_p(piix4_smba + i2ccfg_offset); 33787e1960eSShane Huang release_region(piix4_smba + i2ccfg_offset, 1); 33887e1960eSShane Huang 33987e1960eSShane Huang if (i2ccfg & 1) 34066f8a8ffSJean Delvare dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n"); 34187e1960eSShane Huang else 34266f8a8ffSJean Delvare dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n"); 34387e1960eSShane Huang 34487e1960eSShane Huang dev_info(&PIIX4_dev->dev, 34587e1960eSShane Huang "SMBus Host Controller at 0x%x, revision %d\n", 34687e1960eSShane Huang piix4_smba, i2ccfg >> 4); 34787e1960eSShane Huang 3486befa3fdSJean Delvare /* Find which register is used for port selection */ 3496befa3fdSJean Delvare if (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD) { 3506befa3fdSJean Delvare piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_ALT; 3516befa3fdSJean Delvare } else { 3526befa3fdSJean Delvare mutex_lock(&piix4_mutex_sb800); 3536befa3fdSJean Delvare outb_p(SB800_PIIX4_PORT_IDX_SEL, SB800_PIIX4_SMB_IDX); 3546befa3fdSJean Delvare port_sel = inb_p(SB800_PIIX4_SMB_IDX + 1); 3556befa3fdSJean Delvare piix4_port_sel_sb800 = (port_sel & 0x01) ? 3566befa3fdSJean Delvare SB800_PIIX4_PORT_IDX_ALT : 3576befa3fdSJean Delvare SB800_PIIX4_PORT_IDX; 3586befa3fdSJean Delvare mutex_unlock(&piix4_mutex_sb800); 3596befa3fdSJean Delvare } 3606befa3fdSJean Delvare 3616befa3fdSJean Delvare dev_info(&PIIX4_dev->dev, 3626befa3fdSJean Delvare "Using register 0x%02x for SMBus port selection\n", 3636befa3fdSJean Delvare (unsigned int)piix4_port_sel_sb800); 3646befa3fdSJean Delvare 36514a8086dSAndrew Armenia return piix4_smba; 36687e1960eSShane Huang } 36787e1960eSShane Huang 3680b255e92SBill Pemberton static int piix4_setup_aux(struct pci_dev *PIIX4_dev, 3692a2f7404SAndrew Armenia const struct pci_device_id *id, 3702a2f7404SAndrew Armenia unsigned short base_reg_addr) 3712a2f7404SAndrew Armenia { 3722a2f7404SAndrew Armenia /* Set up auxiliary SMBus controllers found on some 3732a2f7404SAndrew Armenia * AMD chipsets e.g. SP5100 (SB700 derivative) */ 3742a2f7404SAndrew Armenia 3752a2f7404SAndrew Armenia unsigned short piix4_smba; 3762a2f7404SAndrew Armenia 3772a2f7404SAndrew Armenia /* Read address of auxiliary SMBus controller */ 3782a2f7404SAndrew Armenia pci_read_config_word(PIIX4_dev, base_reg_addr, &piix4_smba); 3792a2f7404SAndrew Armenia if ((piix4_smba & 1) == 0) { 3802a2f7404SAndrew Armenia dev_dbg(&PIIX4_dev->dev, 3812a2f7404SAndrew Armenia "Auxiliary SMBus controller not enabled\n"); 3822a2f7404SAndrew Armenia return -ENODEV; 3832a2f7404SAndrew Armenia } 3842a2f7404SAndrew Armenia 3852a2f7404SAndrew Armenia piix4_smba &= 0xfff0; 3862a2f7404SAndrew Armenia if (piix4_smba == 0) { 3872a2f7404SAndrew Armenia dev_dbg(&PIIX4_dev->dev, 3882a2f7404SAndrew Armenia "Auxiliary SMBus base address uninitialized\n"); 3892a2f7404SAndrew Armenia return -ENODEV; 3902a2f7404SAndrew Armenia } 3912a2f7404SAndrew Armenia 3922a2f7404SAndrew Armenia if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) 3932a2f7404SAndrew Armenia return -ENODEV; 3942a2f7404SAndrew Armenia 3952a2f7404SAndrew Armenia if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) { 3962a2f7404SAndrew Armenia dev_err(&PIIX4_dev->dev, "Auxiliary SMBus region 0x%x " 3972a2f7404SAndrew Armenia "already in use!\n", piix4_smba); 3982a2f7404SAndrew Armenia return -EBUSY; 3992a2f7404SAndrew Armenia } 4002a2f7404SAndrew Armenia 4012a2f7404SAndrew Armenia dev_info(&PIIX4_dev->dev, 4022a2f7404SAndrew Armenia "Auxiliary SMBus Host Controller at 0x%x\n", 4032a2f7404SAndrew Armenia piix4_smba); 4042a2f7404SAndrew Armenia 4052a2f7404SAndrew Armenia return piix4_smba; 4062a2f7404SAndrew Armenia } 4072a2f7404SAndrew Armenia 408e154bf6fSAndrew Armenia static int piix4_transaction(struct i2c_adapter *piix4_adapter) 4091da177e4SLinus Torvalds { 410e154bf6fSAndrew Armenia struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(piix4_adapter); 411e154bf6fSAndrew Armenia unsigned short piix4_smba = adapdata->smba; 4121da177e4SLinus Torvalds int temp; 4131da177e4SLinus Torvalds int result = 0; 4141da177e4SLinus Torvalds int timeout = 0; 4151da177e4SLinus Torvalds 416e154bf6fSAndrew Armenia dev_dbg(&piix4_adapter->dev, "Transaction (pre): CNT=%02x, CMD=%02x, " 4171da177e4SLinus Torvalds "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT), 4181da177e4SLinus Torvalds inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), 4191da177e4SLinus Torvalds inb_p(SMBHSTDAT1)); 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds /* Make sure the SMBus host is ready to start transmitting */ 4221da177e4SLinus Torvalds if ((temp = inb_p(SMBHSTSTS)) != 0x00) { 423e154bf6fSAndrew Armenia dev_dbg(&piix4_adapter->dev, "SMBus busy (%02x). " 4241da177e4SLinus Torvalds "Resetting...\n", temp); 4251da177e4SLinus Torvalds outb_p(temp, SMBHSTSTS); 4261da177e4SLinus Torvalds if ((temp = inb_p(SMBHSTSTS)) != 0x00) { 427e154bf6fSAndrew Armenia dev_err(&piix4_adapter->dev, "Failed! (%02x)\n", temp); 42897140342SDavid Brownell return -EBUSY; 4291da177e4SLinus Torvalds } else { 430e154bf6fSAndrew Armenia dev_dbg(&piix4_adapter->dev, "Successful!\n"); 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds /* start the transaction by setting bit 6 */ 4351da177e4SLinus Torvalds outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT); 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */ 438b1c1759cSDavid Milburn if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */ 439b1c1759cSDavid Milburn msleep(2); 440b1c1759cSDavid Milburn else 4411da177e4SLinus Torvalds msleep(1); 442b1c1759cSDavid Milburn 443b6a31950SRoel Kluin while ((++timeout < MAX_TIMEOUT) && 444b1c1759cSDavid Milburn ((temp = inb_p(SMBHSTSTS)) & 0x01)) 445b1c1759cSDavid Milburn msleep(1); 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds /* If the SMBus is still busy, we give up */ 448b6a31950SRoel Kluin if (timeout == MAX_TIMEOUT) { 449e154bf6fSAndrew Armenia dev_err(&piix4_adapter->dev, "SMBus Timeout!\n"); 45097140342SDavid Brownell result = -ETIMEDOUT; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds if (temp & 0x10) { 45497140342SDavid Brownell result = -EIO; 455e154bf6fSAndrew Armenia dev_err(&piix4_adapter->dev, "Error: Failed bus transaction\n"); 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds if (temp & 0x08) { 45997140342SDavid Brownell result = -EIO; 460e154bf6fSAndrew Armenia dev_dbg(&piix4_adapter->dev, "Bus collision! SMBus may be " 4611da177e4SLinus Torvalds "locked until next hard reset. (sorry!)\n"); 4621da177e4SLinus Torvalds /* Clock stops and slave is stuck in mid-transmission */ 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds if (temp & 0x04) { 46697140342SDavid Brownell result = -ENXIO; 467e154bf6fSAndrew Armenia dev_dbg(&piix4_adapter->dev, "Error: no response!\n"); 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds if (inb_p(SMBHSTSTS) != 0x00) 4711da177e4SLinus Torvalds outb_p(inb(SMBHSTSTS), SMBHSTSTS); 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds if ((temp = inb_p(SMBHSTSTS)) != 0x00) { 474e154bf6fSAndrew Armenia dev_err(&piix4_adapter->dev, "Failed reset at end of " 4751da177e4SLinus Torvalds "transaction (%02x)\n", temp); 4761da177e4SLinus Torvalds } 477e154bf6fSAndrew Armenia dev_dbg(&piix4_adapter->dev, "Transaction (post): CNT=%02x, CMD=%02x, " 4781da177e4SLinus Torvalds "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT), 4791da177e4SLinus Torvalds inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), 4801da177e4SLinus Torvalds inb_p(SMBHSTDAT1)); 4811da177e4SLinus Torvalds return result; 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds 48497140342SDavid Brownell /* Return negative errno on error. */ 4851da177e4SLinus Torvalds static s32 piix4_access(struct i2c_adapter * adap, u16 addr, 4861da177e4SLinus Torvalds unsigned short flags, char read_write, 4871da177e4SLinus Torvalds u8 command, int size, union i2c_smbus_data * data) 4881da177e4SLinus Torvalds { 48914a8086dSAndrew Armenia struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap); 49014a8086dSAndrew Armenia unsigned short piix4_smba = adapdata->smba; 4911da177e4SLinus Torvalds int i, len; 49297140342SDavid Brownell int status; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds switch (size) { 4951da177e4SLinus Torvalds case I2C_SMBUS_QUICK: 496fa63cd56SJean Delvare outb_p((addr << 1) | read_write, 4971da177e4SLinus Torvalds SMBHSTADD); 4981da177e4SLinus Torvalds size = PIIX4_QUICK; 4991da177e4SLinus Torvalds break; 5001da177e4SLinus Torvalds case I2C_SMBUS_BYTE: 501fa63cd56SJean Delvare outb_p((addr << 1) | read_write, 5021da177e4SLinus Torvalds SMBHSTADD); 5031da177e4SLinus Torvalds if (read_write == I2C_SMBUS_WRITE) 5041da177e4SLinus Torvalds outb_p(command, SMBHSTCMD); 5051da177e4SLinus Torvalds size = PIIX4_BYTE; 5061da177e4SLinus Torvalds break; 5071da177e4SLinus Torvalds case I2C_SMBUS_BYTE_DATA: 508fa63cd56SJean Delvare outb_p((addr << 1) | read_write, 5091da177e4SLinus Torvalds SMBHSTADD); 5101da177e4SLinus Torvalds outb_p(command, SMBHSTCMD); 5111da177e4SLinus Torvalds if (read_write == I2C_SMBUS_WRITE) 5121da177e4SLinus Torvalds outb_p(data->byte, SMBHSTDAT0); 5131da177e4SLinus Torvalds size = PIIX4_BYTE_DATA; 5141da177e4SLinus Torvalds break; 5151da177e4SLinus Torvalds case I2C_SMBUS_WORD_DATA: 516fa63cd56SJean Delvare outb_p((addr << 1) | read_write, 5171da177e4SLinus Torvalds SMBHSTADD); 5181da177e4SLinus Torvalds outb_p(command, SMBHSTCMD); 5191da177e4SLinus Torvalds if (read_write == I2C_SMBUS_WRITE) { 5201da177e4SLinus Torvalds outb_p(data->word & 0xff, SMBHSTDAT0); 5211da177e4SLinus Torvalds outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1); 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds size = PIIX4_WORD_DATA; 5241da177e4SLinus Torvalds break; 5251da177e4SLinus Torvalds case I2C_SMBUS_BLOCK_DATA: 526fa63cd56SJean Delvare outb_p((addr << 1) | read_write, 5271da177e4SLinus Torvalds SMBHSTADD); 5281da177e4SLinus Torvalds outb_p(command, SMBHSTCMD); 5291da177e4SLinus Torvalds if (read_write == I2C_SMBUS_WRITE) { 5301da177e4SLinus Torvalds len = data->block[0]; 531fa63cd56SJean Delvare if (len == 0 || len > I2C_SMBUS_BLOCK_MAX) 532fa63cd56SJean Delvare return -EINVAL; 5331da177e4SLinus Torvalds outb_p(len, SMBHSTDAT0); 534d7a4c763SWolfram Sang inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */ 5351da177e4SLinus Torvalds for (i = 1; i <= len; i++) 5361da177e4SLinus Torvalds outb_p(data->block[i], SMBBLKDAT); 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds size = PIIX4_BLOCK_DATA; 5391da177e4SLinus Torvalds break; 540ac7fc4fbSJean Delvare default: 541ac7fc4fbSJean Delvare dev_warn(&adap->dev, "Unsupported transaction %d\n", size); 542ac7fc4fbSJean Delvare return -EOPNOTSUPP; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT); 5461da177e4SLinus Torvalds 547e154bf6fSAndrew Armenia status = piix4_transaction(adap); 54897140342SDavid Brownell if (status) 54997140342SDavid Brownell return status; 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK)) 5521da177e4SLinus Torvalds return 0; 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds switch (size) { 5563578a075SJean Delvare case PIIX4_BYTE: 5571da177e4SLinus Torvalds case PIIX4_BYTE_DATA: 5581da177e4SLinus Torvalds data->byte = inb_p(SMBHSTDAT0); 5591da177e4SLinus Torvalds break; 5601da177e4SLinus Torvalds case PIIX4_WORD_DATA: 5611da177e4SLinus Torvalds data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8); 5621da177e4SLinus Torvalds break; 5631da177e4SLinus Torvalds case PIIX4_BLOCK_DATA: 5641da177e4SLinus Torvalds data->block[0] = inb_p(SMBHSTDAT0); 565fa63cd56SJean Delvare if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX) 566fa63cd56SJean Delvare return -EPROTO; 567d7a4c763SWolfram Sang inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */ 5681da177e4SLinus Torvalds for (i = 1; i <= data->block[0]; i++) 5691da177e4SLinus Torvalds data->block[i] = inb_p(SMBBLKDAT); 5701da177e4SLinus Torvalds break; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds return 0; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 5752fee61d2SChristian Fetzer /* 5762fee61d2SChristian Fetzer * Handles access to multiple SMBus ports on the SB800. 5772fee61d2SChristian Fetzer * The port is selected by bits 2:1 of the smb_en register (0x2c). 5782fee61d2SChristian Fetzer * Returns negative errno on error. 5792fee61d2SChristian Fetzer * 5802fee61d2SChristian Fetzer * Note: The selected port must be returned to the initial selection to avoid 5812fee61d2SChristian Fetzer * problems on certain systems. 5822fee61d2SChristian Fetzer */ 5832fee61d2SChristian Fetzer static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr, 5842fee61d2SChristian Fetzer unsigned short flags, char read_write, 5852fee61d2SChristian Fetzer u8 command, int size, union i2c_smbus_data *data) 5862fee61d2SChristian Fetzer { 5872fee61d2SChristian Fetzer struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap); 588*701dc207SRicardo Ribalda unsigned short piix4_smba = adapdata->smba; 589*701dc207SRicardo Ribalda int retries = MAX_TIMEOUT; 590*701dc207SRicardo Ribalda int smbslvcnt; 5912fee61d2SChristian Fetzer u8 smba_en_lo; 5922fee61d2SChristian Fetzer u8 port; 5932fee61d2SChristian Fetzer int retval; 5942fee61d2SChristian Fetzer 595*701dc207SRicardo Ribalda /* Request the SMBUS semaphore, avoid conflicts with the IMC */ 596*701dc207SRicardo Ribalda smbslvcnt = inb_p(SMBSLVCNT); 597*701dc207SRicardo Ribalda do { 598*701dc207SRicardo Ribalda outb_p(smbslvcnt | 0x10, SMBSLVCNT); 599*701dc207SRicardo Ribalda 600*701dc207SRicardo Ribalda /* Check the semaphore status */ 601*701dc207SRicardo Ribalda smbslvcnt = inb_p(SMBSLVCNT); 602*701dc207SRicardo Ribalda if (smbslvcnt & 0x10) 603*701dc207SRicardo Ribalda break; 604*701dc207SRicardo Ribalda 605*701dc207SRicardo Ribalda usleep_range(1000, 2000); 606*701dc207SRicardo Ribalda } while (--retries); 607*701dc207SRicardo Ribalda /* SMBus is still owned by the IMC, we give up */ 608*701dc207SRicardo Ribalda if (!retries) 609*701dc207SRicardo Ribalda return -EBUSY; 610*701dc207SRicardo Ribalda 611a28e3517SJean Delvare mutex_lock(&piix4_mutex_sb800); 6122fee61d2SChristian Fetzer 6136befa3fdSJean Delvare outb_p(piix4_port_sel_sb800, SB800_PIIX4_SMB_IDX); 6142fee61d2SChristian Fetzer smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1); 6152fee61d2SChristian Fetzer 6162fee61d2SChristian Fetzer port = adapdata->port; 61733f5ccc3SJean Delvare if ((smba_en_lo & SB800_PIIX4_PORT_IDX_MASK) != port) 61833f5ccc3SJean Delvare outb_p((smba_en_lo & ~SB800_PIIX4_PORT_IDX_MASK) | port, 6192fee61d2SChristian Fetzer SB800_PIIX4_SMB_IDX + 1); 6202fee61d2SChristian Fetzer 6212fee61d2SChristian Fetzer retval = piix4_access(adap, addr, flags, read_write, 6222fee61d2SChristian Fetzer command, size, data); 6232fee61d2SChristian Fetzer 6242fee61d2SChristian Fetzer outb_p(smba_en_lo, SB800_PIIX4_SMB_IDX + 1); 6252fee61d2SChristian Fetzer 626a28e3517SJean Delvare mutex_unlock(&piix4_mutex_sb800); 6272fee61d2SChristian Fetzer 628*701dc207SRicardo Ribalda /* Release the semaphore */ 629*701dc207SRicardo Ribalda outb_p(smbslvcnt | 0x20, SMBSLVCNT); 630*701dc207SRicardo Ribalda 6312fee61d2SChristian Fetzer return retval; 6322fee61d2SChristian Fetzer } 6332fee61d2SChristian Fetzer 6341da177e4SLinus Torvalds static u32 piix4_func(struct i2c_adapter *adapter) 6351da177e4SLinus Torvalds { 6361da177e4SLinus Torvalds return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | 6371da177e4SLinus Torvalds I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | 6381da177e4SLinus Torvalds I2C_FUNC_SMBUS_BLOCK_DATA; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6418f9082c5SJean Delvare static const struct i2c_algorithm smbus_algorithm = { 6421da177e4SLinus Torvalds .smbus_xfer = piix4_access, 6431da177e4SLinus Torvalds .functionality = piix4_func, 6441da177e4SLinus Torvalds }; 6451da177e4SLinus Torvalds 6462fee61d2SChristian Fetzer static const struct i2c_algorithm piix4_smbus_algorithm_sb800 = { 6472fee61d2SChristian Fetzer .smbus_xfer = piix4_access_sb800, 6482fee61d2SChristian Fetzer .functionality = piix4_func, 6492fee61d2SChristian Fetzer }; 6502fee61d2SChristian Fetzer 651392debf1SJingoo Han static const struct pci_device_id piix4_ids[] = { 6529b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) }, 6539b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3) }, 6549b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3) }, 6559b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_SMBUS) }, 6569b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_SMBUS) }, 6579b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS) }, 6589b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS) }, 6593806e94bSCrane Cai { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) }, 660bcb29994SVincent Wan { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) }, 6619b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, 6629b7389c0SJean Delvare PCI_DEVICE_ID_SERVERWORKS_OSB4) }, 6639b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, 6649b7389c0SJean Delvare PCI_DEVICE_ID_SERVERWORKS_CSB5) }, 6659b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, 6669b7389c0SJean Delvare PCI_DEVICE_ID_SERVERWORKS_CSB6) }, 6679b7389c0SJean Delvare { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, 6689b7389c0SJean Delvare PCI_DEVICE_ID_SERVERWORKS_HT1000SB) }, 669506a8b6cSFlavio Leitner { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, 670506a8b6cSFlavio Leitner PCI_DEVICE_ID_SERVERWORKS_HT1100LD) }, 6711da177e4SLinus Torvalds { 0, } 6721da177e4SLinus Torvalds }; 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds MODULE_DEVICE_TABLE (pci, piix4_ids); 6751da177e4SLinus Torvalds 676ca2061e1SChristian Fetzer static struct i2c_adapter *piix4_main_adapters[PIIX4_MAX_ADAPTERS]; 6772a2f7404SAndrew Armenia static struct i2c_adapter *piix4_aux_adapter; 678e154bf6fSAndrew Armenia 6790b255e92SBill Pemberton static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba, 68062194e86SJean Delvare bool sb800_main, u8 port, 681725d2e3fSChristian Fetzer const char *name, struct i2c_adapter **padap) 682e154bf6fSAndrew Armenia { 683e154bf6fSAndrew Armenia struct i2c_adapter *adap; 684e154bf6fSAndrew Armenia struct i2c_piix4_adapdata *adapdata; 685e154bf6fSAndrew Armenia int retval; 686e154bf6fSAndrew Armenia 687e154bf6fSAndrew Armenia adap = kzalloc(sizeof(*adap), GFP_KERNEL); 688e154bf6fSAndrew Armenia if (adap == NULL) { 689e154bf6fSAndrew Armenia release_region(smba, SMBIOSIZE); 690e154bf6fSAndrew Armenia return -ENOMEM; 691e154bf6fSAndrew Armenia } 692e154bf6fSAndrew Armenia 693e154bf6fSAndrew Armenia adap->owner = THIS_MODULE; 694e154bf6fSAndrew Armenia adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; 69583c60158SJean Delvare adap->algo = sb800_main ? &piix4_smbus_algorithm_sb800 69683c60158SJean Delvare : &smbus_algorithm; 697e154bf6fSAndrew Armenia 698e154bf6fSAndrew Armenia adapdata = kzalloc(sizeof(*adapdata), GFP_KERNEL); 699e154bf6fSAndrew Armenia if (adapdata == NULL) { 700e154bf6fSAndrew Armenia kfree(adap); 701e154bf6fSAndrew Armenia release_region(smba, SMBIOSIZE); 702e154bf6fSAndrew Armenia return -ENOMEM; 703e154bf6fSAndrew Armenia } 704e154bf6fSAndrew Armenia 705e154bf6fSAndrew Armenia adapdata->smba = smba; 70683c60158SJean Delvare adapdata->sb800_main = sb800_main; 70733f5ccc3SJean Delvare adapdata->port = port << 1; 708e154bf6fSAndrew Armenia 709e154bf6fSAndrew Armenia /* set up the sysfs linkage to our parent device */ 710e154bf6fSAndrew Armenia adap->dev.parent = &dev->dev; 711e154bf6fSAndrew Armenia 712e154bf6fSAndrew Armenia snprintf(adap->name, sizeof(adap->name), 713725d2e3fSChristian Fetzer "SMBus PIIX4 adapter%s at %04x", name, smba); 714e154bf6fSAndrew Armenia 715e154bf6fSAndrew Armenia i2c_set_adapdata(adap, adapdata); 716e154bf6fSAndrew Armenia 717e154bf6fSAndrew Armenia retval = i2c_add_adapter(adap); 718e154bf6fSAndrew Armenia if (retval) { 719e154bf6fSAndrew Armenia kfree(adapdata); 720e154bf6fSAndrew Armenia kfree(adap); 721e154bf6fSAndrew Armenia release_region(smba, SMBIOSIZE); 722e154bf6fSAndrew Armenia return retval; 723e154bf6fSAndrew Armenia } 724e154bf6fSAndrew Armenia 725e154bf6fSAndrew Armenia *padap = adap; 726e154bf6fSAndrew Armenia return 0; 727e154bf6fSAndrew Armenia } 728e154bf6fSAndrew Armenia 7292fee61d2SChristian Fetzer static int piix4_add_adapters_sb800(struct pci_dev *dev, unsigned short smba) 7302fee61d2SChristian Fetzer { 7312fee61d2SChristian Fetzer struct i2c_piix4_adapdata *adapdata; 7322fee61d2SChristian Fetzer int port; 7332fee61d2SChristian Fetzer int retval; 7342fee61d2SChristian Fetzer 7352fee61d2SChristian Fetzer for (port = 0; port < PIIX4_MAX_ADAPTERS; port++) { 73683c60158SJean Delvare retval = piix4_add_adapter(dev, smba, true, port, 737725d2e3fSChristian Fetzer piix4_main_port_names_sb800[port], 7382fee61d2SChristian Fetzer &piix4_main_adapters[port]); 7392fee61d2SChristian Fetzer if (retval < 0) 7402fee61d2SChristian Fetzer goto error; 7412fee61d2SChristian Fetzer } 7422fee61d2SChristian Fetzer 7432fee61d2SChristian Fetzer return retval; 7442fee61d2SChristian Fetzer 7452fee61d2SChristian Fetzer error: 7462fee61d2SChristian Fetzer dev_err(&dev->dev, 7472fee61d2SChristian Fetzer "Error setting up SB800 adapters. Unregistering!\n"); 7482fee61d2SChristian Fetzer while (--port >= 0) { 7492fee61d2SChristian Fetzer adapdata = i2c_get_adapdata(piix4_main_adapters[port]); 7502fee61d2SChristian Fetzer if (adapdata->smba) { 7512fee61d2SChristian Fetzer i2c_del_adapter(piix4_main_adapters[port]); 7522fee61d2SChristian Fetzer kfree(adapdata); 7532fee61d2SChristian Fetzer kfree(piix4_main_adapters[port]); 7542fee61d2SChristian Fetzer piix4_main_adapters[port] = NULL; 7552fee61d2SChristian Fetzer } 7562fee61d2SChristian Fetzer } 7572fee61d2SChristian Fetzer 7582fee61d2SChristian Fetzer return retval; 7592fee61d2SChristian Fetzer } 7602fee61d2SChristian Fetzer 7610b255e92SBill Pemberton static int piix4_probe(struct pci_dev *dev, const struct pci_device_id *id) 7621da177e4SLinus Torvalds { 7631da177e4SLinus Torvalds int retval; 76452795f6fSJean Delvare bool is_sb800 = false; 7651da177e4SLinus Torvalds 76676b3e28fSCrane Cai if ((dev->vendor == PCI_VENDOR_ID_ATI && 76776b3e28fSCrane Cai dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS && 76876b3e28fSCrane Cai dev->revision >= 0x40) || 7692fee61d2SChristian Fetzer dev->vendor == PCI_VENDOR_ID_AMD) { 77052795f6fSJean Delvare is_sb800 = true; 77152795f6fSJean Delvare 7722fee61d2SChristian Fetzer if (!request_region(SB800_PIIX4_SMB_IDX, 2, "smba_idx")) { 7732fee61d2SChristian Fetzer dev_err(&dev->dev, 7742fee61d2SChristian Fetzer "SMBus base address index region 0x%x already in use!\n", 7752fee61d2SChristian Fetzer SB800_PIIX4_SMB_IDX); 7762fee61d2SChristian Fetzer return -EBUSY; 7772fee61d2SChristian Fetzer } 7782fee61d2SChristian Fetzer 77987e1960eSShane Huang /* base address location etc changed in SB800 */ 780a94dd00fSRudolf Marek retval = piix4_setup_sb800(dev, id, 0); 7812fee61d2SChristian Fetzer if (retval < 0) { 7822fee61d2SChristian Fetzer release_region(SB800_PIIX4_SMB_IDX, 2); 7832fee61d2SChristian Fetzer return retval; 7842fee61d2SChristian Fetzer } 78587e1960eSShane Huang 7862fee61d2SChristian Fetzer /* 7872fee61d2SChristian Fetzer * Try to register multiplexed main SMBus adapter, 7882fee61d2SChristian Fetzer * give up if we can't 7892fee61d2SChristian Fetzer */ 7902fee61d2SChristian Fetzer retval = piix4_add_adapters_sb800(dev, retval); 7912fee61d2SChristian Fetzer if (retval < 0) { 7922fee61d2SChristian Fetzer release_region(SB800_PIIX4_SMB_IDX, 2); 7932fee61d2SChristian Fetzer return retval; 7942fee61d2SChristian Fetzer } 7952fee61d2SChristian Fetzer } else { 7962fee61d2SChristian Fetzer retval = piix4_setup(dev, id); 79714a8086dSAndrew Armenia if (retval < 0) 7981da177e4SLinus Torvalds return retval; 7991da177e4SLinus Torvalds 8002a2f7404SAndrew Armenia /* Try to register main SMBus adapter, give up if we can't */ 80152795f6fSJean Delvare retval = piix4_add_adapter(dev, retval, false, 0, "", 8022fee61d2SChristian Fetzer &piix4_main_adapters[0]); 8032a2f7404SAndrew Armenia if (retval < 0) 8042a2f7404SAndrew Armenia return retval; 8052fee61d2SChristian Fetzer } 8062a2f7404SAndrew Armenia 8072a2f7404SAndrew Armenia /* Check for auxiliary SMBus on some AMD chipsets */ 808a94dd00fSRudolf Marek retval = -ENODEV; 809a94dd00fSRudolf Marek 8102a2f7404SAndrew Armenia if (dev->vendor == PCI_VENDOR_ID_ATI && 811a94dd00fSRudolf Marek dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS) { 812a94dd00fSRudolf Marek if (dev->revision < 0x40) { 8132a2f7404SAndrew Armenia retval = piix4_setup_aux(dev, id, 0x58); 814a94dd00fSRudolf Marek } else { 815a94dd00fSRudolf Marek /* SB800 added aux bus too */ 816a94dd00fSRudolf Marek retval = piix4_setup_sb800(dev, id, 1); 817a94dd00fSRudolf Marek } 818a94dd00fSRudolf Marek } 819a94dd00fSRudolf Marek 820a94dd00fSRudolf Marek if (dev->vendor == PCI_VENDOR_ID_AMD && 821a94dd00fSRudolf Marek dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) { 822a94dd00fSRudolf Marek retval = piix4_setup_sb800(dev, id, 1); 823a94dd00fSRudolf Marek } 824a94dd00fSRudolf Marek 8252a2f7404SAndrew Armenia if (retval > 0) { 8262a2f7404SAndrew Armenia /* Try to add the aux adapter if it exists, 8272a2f7404SAndrew Armenia * piix4_add_adapter will clean up if this fails */ 82883c60158SJean Delvare piix4_add_adapter(dev, retval, false, 0, 82952795f6fSJean Delvare is_sb800 ? piix4_aux_port_name_sb800 : "", 830725d2e3fSChristian Fetzer &piix4_aux_adapter); 8312a2f7404SAndrew Armenia } 8322a2f7404SAndrew Armenia 8332a2f7404SAndrew Armenia return 0; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 8360b255e92SBill Pemberton static void piix4_adap_remove(struct i2c_adapter *adap) 83714a8086dSAndrew Armenia { 83814a8086dSAndrew Armenia struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap); 83914a8086dSAndrew Armenia 84014a8086dSAndrew Armenia if (adapdata->smba) { 84114a8086dSAndrew Armenia i2c_del_adapter(adap); 84233f5ccc3SJean Delvare if (adapdata->port == (0 << 1)) { 84314a8086dSAndrew Armenia release_region(adapdata->smba, SMBIOSIZE); 844a28e3517SJean Delvare if (adapdata->sb800_main) 8452fee61d2SChristian Fetzer release_region(SB800_PIIX4_SMB_IDX, 2); 8462fee61d2SChristian Fetzer } 847e154bf6fSAndrew Armenia kfree(adapdata); 848e154bf6fSAndrew Armenia kfree(adap); 84914a8086dSAndrew Armenia } 85014a8086dSAndrew Armenia } 85114a8086dSAndrew Armenia 8520b255e92SBill Pemberton static void piix4_remove(struct pci_dev *dev) 8531da177e4SLinus Torvalds { 854ca2061e1SChristian Fetzer int port = PIIX4_MAX_ADAPTERS; 855ca2061e1SChristian Fetzer 856ca2061e1SChristian Fetzer while (--port >= 0) { 857ca2061e1SChristian Fetzer if (piix4_main_adapters[port]) { 858ca2061e1SChristian Fetzer piix4_adap_remove(piix4_main_adapters[port]); 859ca2061e1SChristian Fetzer piix4_main_adapters[port] = NULL; 860ca2061e1SChristian Fetzer } 861e154bf6fSAndrew Armenia } 8622a2f7404SAndrew Armenia 8632a2f7404SAndrew Armenia if (piix4_aux_adapter) { 8642a2f7404SAndrew Armenia piix4_adap_remove(piix4_aux_adapter); 8652a2f7404SAndrew Armenia piix4_aux_adapter = NULL; 8662a2f7404SAndrew Armenia } 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds static struct pci_driver piix4_driver = { 8701da177e4SLinus Torvalds .name = "piix4_smbus", 8711da177e4SLinus Torvalds .id_table = piix4_ids, 8721da177e4SLinus Torvalds .probe = piix4_probe, 8730b255e92SBill Pemberton .remove = piix4_remove, 8741da177e4SLinus Torvalds }; 8751da177e4SLinus Torvalds 87656f21788SAxel Lin module_pci_driver(piix4_driver); 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and " 8791da177e4SLinus Torvalds "Philip Edelbrock <phil@netroedge.com>"); 8801da177e4SLinus Torvalds MODULE_DESCRIPTION("PIIX4 SMBus driver"); 8811da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 882