blob: 8f1c5f24c1df5a96034429791035f89cc22aef15 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl> and
3 Philip Edelbrock <phil@netroedge.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014*/
15
16/*
17 Supports:
18 Intel PIIX4, 440MX
Flavio Leitner506a8b62009-03-28 21:34:46 +010019 Serverworks OSB4, CSB5, CSB6, HT-1000, HT-1100
Andrew Armenia2a2f7402012-07-24 14:13:57 +020020 ATI IXP200, IXP300, IXP400, SB600, SB700/SP5100, SB800
Shane Huang032f7082014-01-22 14:05:46 -080021 AMD Hudson-2, ML, CZ
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SMSC Victory66
23
Andrew Armenia2a2f7402012-07-24 14:13:57 +020024 Note: we assume there can only be one device, with one or more
25 SMBus interfaces.
Christian Fetzer2fee61d2015-11-19 20:13:48 +010026 The device can register multiple i2c_adapters (up to PIIX4_MAX_ADAPTERS).
27 For devices supporting multiple ports the i2c_adapter should provide
28 an i2c_algorithm to access them.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029*/
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/moduleparam.h>
33#include <linux/pci.h>
34#include <linux/kernel.h>
35#include <linux/delay.h>
36#include <linux/stddef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/ioport.h>
38#include <linux/i2c.h>
Daniel J Bluemanc415b302012-10-05 22:23:55 +020039#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/dmi.h>
Jean Delvare54fb4a052008-07-14 22:38:33 +020041#include <linux/acpi.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020042#include <linux/io.h>
Christian Fetzer2fee61d2015-11-19 20:13:48 +010043#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* PIIX4 SMBus address offsets */
47#define SMBHSTSTS (0 + piix4_smba)
48#define SMBHSLVSTS (1 + piix4_smba)
49#define SMBHSTCNT (2 + piix4_smba)
50#define SMBHSTCMD (3 + piix4_smba)
51#define SMBHSTADD (4 + piix4_smba)
52#define SMBHSTDAT0 (5 + piix4_smba)
53#define SMBHSTDAT1 (6 + piix4_smba)
54#define SMBBLKDAT (7 + piix4_smba)
55#define SMBSLVCNT (8 + piix4_smba)
56#define SMBSHDWCMD (9 + piix4_smba)
57#define SMBSLVEVT (0xA + piix4_smba)
58#define SMBSLVDAT (0xC + piix4_smba)
59
60/* count for request_region */
Ricardo Ribalda47a5aab2017-05-23 21:53:45 -040061#define SMBIOSIZE 9
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/* PCI Address Constants */
64#define SMBBA 0x090
65#define SMBHSTCFG 0x0D2
66#define SMBSLVC 0x0D3
67#define SMBSHDW1 0x0D4
68#define SMBSHDW2 0x0D5
69#define SMBREV 0x0D6
70
71/* Other settings */
72#define MAX_TIMEOUT 500
73#define ENABLE_INT9 0
74
75/* PIIX4 constants */
76#define PIIX4_QUICK 0x00
77#define PIIX4_BYTE 0x04
78#define PIIX4_BYTE_DATA 0x08
79#define PIIX4_WORD_DATA 0x0C
80#define PIIX4_BLOCK_DATA 0x14
81
Christian Fetzerca2061e2015-11-19 20:13:47 +010082/* Multi-port constants */
83#define PIIX4_MAX_ADAPTERS 4
84
Christian Fetzer2fee61d2015-11-19 20:13:48 +010085/* SB800 constants */
86#define SB800_PIIX4_SMB_IDX 0xcd6
87
Jean Delvare6befa3f2016-02-17 10:26:35 +010088/*
89 * SB800 port is selected by bits 2:1 of the smb_en register (0x2c)
90 * or the smb_sel register (0x2e), depending on bit 0 of register 0x2f.
91 * Hudson-2/Bolton port is always selected by bits 2:1 of register 0x2f.
92 */
Christian Fetzer2fee61d2015-11-19 20:13:48 +010093#define SB800_PIIX4_PORT_IDX 0x2c
Jean Delvare6befa3f2016-02-17 10:26:35 +010094#define SB800_PIIX4_PORT_IDX_ALT 0x2e
95#define SB800_PIIX4_PORT_IDX_SEL 0x2f
Christian Fetzer2fee61d2015-11-19 20:13:48 +010096#define SB800_PIIX4_PORT_IDX_MASK 0x06
Guenter Roeck717ef37d2017-07-15 16:51:26 -070097#define SB800_PIIX4_PORT_IDX_SHIFT 1
98
99/* On kerncz, SmBus0Sel is at bit 20:19 of PMx00 DecodeEn */
100#define SB800_PIIX4_PORT_IDX_KERNCZ 0x02
101#define SB800_PIIX4_PORT_IDX_MASK_KERNCZ 0x18
102#define SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ 3
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* insmod parameters */
105
106/* If force is set to anything different from 0, we forcibly enable the
107 PIIX4. DANGEROUS! */
Jean Delvare60507092005-09-25 16:23:07 +0200108static int force;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109module_param (force, int, 0);
110MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!");
111
112/* If force_addr is set to anything different from 0, we forcibly enable
113 the PIIX4 at the given address. VERY DANGEROUS! */
Jean Delvare60507092005-09-25 16:23:07 +0200114static int force_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115module_param (force_addr, int, 0);
116MODULE_PARM_DESC(force_addr,
117 "Forcibly enable the PIIX4 at the given address. "
118 "EXTREMELY DANGEROUS!");
119
David Milburnb1c17592008-05-11 20:37:05 +0200120static int srvrworks_csb5_delay;
Jean Delvared6072f82005-09-25 16:37:04 +0200121static struct pci_driver piix4_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Bill Pemberton0b255e92012-11-27 15:59:38 -0500123static const struct dmi_system_id piix4_dmi_blacklist[] = {
Jean Delvarec2fc54f2008-05-11 20:37:05 +0200124 {
125 .ident = "Sapphire AM2RD790",
126 .matches = {
127 DMI_MATCH(DMI_BOARD_VENDOR, "SAPPHIRE Inc."),
128 DMI_MATCH(DMI_BOARD_NAME, "PC-AM2RD790"),
129 },
130 },
131 {
132 .ident = "DFI Lanparty UT 790FX",
133 .matches = {
134 DMI_MATCH(DMI_BOARD_VENDOR, "DFI Inc."),
135 DMI_MATCH(DMI_BOARD_NAME, "LP UT 790FX"),
136 },
137 },
138 { }
139};
140
141/* The IBM entry is in a separate table because we only check it
142 on Intel-based systems */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500143static const struct dmi_system_id piix4_dmi_ibm[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 {
145 .ident = "IBM",
146 .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), },
147 },
148 { },
149};
150
Jean Delvare6befa3f2016-02-17 10:26:35 +0100151/*
152 * SB800 globals
153 * piix4_mutex_sb800 protects piix4_port_sel_sb800 and the pair
154 * of I/O ports at SB800_PIIX4_SMB_IDX.
155 */
Jean Delvarea28e3512016-01-22 14:12:02 +0100156static DEFINE_MUTEX(piix4_mutex_sb800);
Jean Delvare6befa3f2016-02-17 10:26:35 +0100157static u8 piix4_port_sel_sb800;
Guenter Roeck717ef37d2017-07-15 16:51:26 -0700158static u8 piix4_port_mask_sb800;
159static u8 piix4_port_shift_sb800;
Christian Fetzer725d2e32015-11-19 20:13:49 +0100160static const char *piix4_main_port_names_sb800[PIIX4_MAX_ADAPTERS] = {
Jean Delvare52795f62016-01-27 14:40:33 +0100161 " port 0", " port 2", " port 3", " port 4"
Christian Fetzer725d2e32015-11-19 20:13:49 +0100162};
Jean Delvare52795f62016-01-27 14:40:33 +0100163static const char *piix4_aux_port_name_sb800 = " port 1";
Christian Fetzer725d2e32015-11-19 20:13:49 +0100164
Andrew Armenia14a80862012-07-24 14:13:56 +0200165struct i2c_piix4_adapdata {
166 unsigned short smba;
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100167
168 /* SB800 */
169 bool sb800_main;
Jean Delvare33f5ccc2016-01-29 10:46:37 +0100170 u8 port; /* Port number, shifted */
Andrew Armenia14a80862012-07-24 14:13:56 +0200171};
172
Bill Pemberton0b255e92012-11-27 15:59:38 -0500173static int piix4_setup(struct pci_dev *PIIX4_dev,
174 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 unsigned char temp;
Andrew Armenia14a80862012-07-24 14:13:56 +0200177 unsigned short piix4_smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
David Milburnb1c17592008-05-11 20:37:05 +0200179 if ((PIIX4_dev->vendor == PCI_VENDOR_ID_SERVERWORKS) &&
180 (PIIX4_dev->device == PCI_DEVICE_ID_SERVERWORKS_CSB5))
181 srvrworks_csb5_delay = 1;
182
Jean Delvarec2fc54f2008-05-11 20:37:05 +0200183 /* On some motherboards, it was reported that accessing the SMBus
184 caused severe hardware problems */
185 if (dmi_check_system(piix4_dmi_blacklist)) {
186 dev_err(&PIIX4_dev->dev,
187 "Accessing the SMBus on this system is unsafe!\n");
188 return -EPERM;
189 }
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* Don't access SMBus on IBM systems which get corrupted eeproms */
Jean Delvarec2fc54f2008-05-11 20:37:05 +0200192 if (dmi_check_system(piix4_dmi_ibm) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) {
Jean Delvaref9ba6c02006-04-25 13:37:25 +0200194 dev_err(&PIIX4_dev->dev, "IBM system detected; this module "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 "may corrupt your serial eeprom! Refusing to load "
196 "module!\n");
197 return -EPERM;
198 }
199
200 /* Determine the address of the SMBus areas */
201 if (force_addr) {
202 piix4_smba = force_addr & 0xfff0;
203 force = 0;
204 } else {
205 pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba);
206 piix4_smba &= 0xfff0;
207 if(piix4_smba == 0) {
Jean Delvarefa63cd52008-07-14 22:38:25 +0200208 dev_err(&PIIX4_dev->dev, "SMBus base address "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 "uninitialized - upgrade BIOS or use "
210 "force_addr=0xaddr\n");
211 return -ENODEV;
212 }
213 }
214
Jean Delvare54fb4a052008-07-14 22:38:33 +0200215 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
Jean Delvare18669ea2009-10-04 22:53:45 +0200216 return -ENODEV;
Jean Delvare54fb4a052008-07-14 22:38:33 +0200217
Jean Delvared6072f82005-09-25 16:37:04 +0200218 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
Jean Delvarefa63cd52008-07-14 22:38:25 +0200219 dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 piix4_smba);
Jean Delvarefa63cd52008-07-14 22:38:25 +0200221 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
224 pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp);
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* If force_addr is set, we program the new address here. Just to make
227 sure, we disable the PIIX4 first. */
228 if (force_addr) {
229 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe);
230 pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba);
231 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01);
232 dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to "
233 "new address %04x!\n", piix4_smba);
234 } else if ((temp & 1) == 0) {
235 if (force) {
236 /* This should never need to be done, but has been
237 * noted that many Dell machines have the SMBus
238 * interface on the PIIX4 disabled!? NOTE: This assumes
239 * I/O space and other allocations WERE done by the
240 * Bios! Don't complain if your hardware does weird
241 * things after enabling this. :') Check for Bios
242 * updates before resorting to this.
243 */
244 pci_write_config_byte(PIIX4_dev, SMBHSTCFG,
245 temp | 1);
Joe Perches8117e412012-12-16 21:11:55 +0100246 dev_notice(&PIIX4_dev->dev,
247 "WARNING: SMBus interface has been FORCEFULLY ENABLED!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 } else {
249 dev_err(&PIIX4_dev->dev,
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100250 "SMBus Host Controller not enabled!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 release_region(piix4_smba, SMBIOSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return -ENODEV;
253 }
254 }
255
Rudolf Marek54aaa1c2006-04-25 13:06:41 +0200256 if (((temp & 0x0E) == 8) || ((temp & 0x0E) == 2))
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100257 dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 else if ((temp & 0x0E) == 0)
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100259 dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 else
261 dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration "
262 "(or code out of date)!\n");
263
264 pci_read_config_byte(PIIX4_dev, SMBREV, &temp);
Jean Delvarefa63cd52008-07-14 22:38:25 +0200265 dev_info(&PIIX4_dev->dev,
266 "SMBus Host Controller at 0x%x, revision %d\n",
267 piix4_smba, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Andrew Armenia14a80862012-07-24 14:13:56 +0200269 return piix4_smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Bill Pemberton0b255e92012-11-27 15:59:38 -0500272static int piix4_setup_sb800(struct pci_dev *PIIX4_dev,
Rudolf Mareka94dd002013-07-14 23:17:26 +0200273 const struct pci_device_id *id, u8 aux)
Shane Huang87e19602009-03-28 21:34:46 +0100274{
Andrew Armenia14a80862012-07-24 14:13:56 +0200275 unsigned short piix4_smba;
Jean Delvare6befa3f2016-02-17 10:26:35 +0100276 u8 smba_en_lo, smba_en_hi, smb_en, smb_en_status, port_sel;
Shane Huang032f7082014-01-22 14:05:46 -0800277 u8 i2ccfg, i2ccfg_offset = 0x10;
Shane Huang87e19602009-03-28 21:34:46 +0100278
Crane Cai3806e94b2009-11-07 13:10:46 +0100279 /* SB800 and later SMBus does not support forcing address */
Shane Huang87e19602009-03-28 21:34:46 +0100280 if (force || force_addr) {
Crane Cai3806e94b2009-11-07 13:10:46 +0100281 dev_err(&PIIX4_dev->dev, "SMBus does not support "
Shane Huang87e19602009-03-28 21:34:46 +0100282 "forcing address!\n");
283 return -EINVAL;
284 }
285
286 /* Determine the address of the SMBus areas */
Shane Huang032f7082014-01-22 14:05:46 -0800287 if ((PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
288 PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
289 PIIX4_dev->revision >= 0x41) ||
290 (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
Vincent Wanbcb29992015-06-11 20:11:46 +0800291 PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS &&
Shane Huang032f7082014-01-22 14:05:46 -0800292 PIIX4_dev->revision >= 0x49))
293 smb_en = 0x00;
294 else
295 smb_en = (aux) ? 0x28 : 0x2c;
Rudolf Mareka94dd002013-07-14 23:17:26 +0200296
Jean Delvarea28e3512016-01-22 14:12:02 +0100297 mutex_lock(&piix4_mutex_sb800);
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100298 outb_p(smb_en, SB800_PIIX4_SMB_IDX);
299 smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
300 outb_p(smb_en + 1, SB800_PIIX4_SMB_IDX);
301 smba_en_hi = inb_p(SB800_PIIX4_SMB_IDX + 1);
Jean Delvarea28e3512016-01-22 14:12:02 +0100302 mutex_unlock(&piix4_mutex_sb800);
Shane Huang87e19602009-03-28 21:34:46 +0100303
Shane Huang032f7082014-01-22 14:05:46 -0800304 if (!smb_en) {
305 smb_en_status = smba_en_lo & 0x10;
306 piix4_smba = smba_en_hi << 8;
307 if (aux)
308 piix4_smba |= 0x20;
309 } else {
310 smb_en_status = smba_en_lo & 0x01;
311 piix4_smba = ((smba_en_hi << 8) | smba_en_lo) & 0xffe0;
312 }
313
314 if (!smb_en_status) {
Shane Huang87e19602009-03-28 21:34:46 +0100315 dev_err(&PIIX4_dev->dev,
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100316 "SMBus Host Controller not enabled!\n");
Shane Huang87e19602009-03-28 21:34:46 +0100317 return -ENODEV;
318 }
319
Shane Huang87e19602009-03-28 21:34:46 +0100320 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
Jean Delvare18669ea2009-10-04 22:53:45 +0200321 return -ENODEV;
Shane Huang87e19602009-03-28 21:34:46 +0100322
323 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
324 dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
325 piix4_smba);
326 return -EBUSY;
327 }
328
Rudolf Mareka94dd002013-07-14 23:17:26 +0200329 /* Aux SMBus does not support IRQ information */
330 if (aux) {
331 dev_info(&PIIX4_dev->dev,
Shane Huang85fd0fe2014-01-22 14:06:52 -0800332 "Auxiliary SMBus Host Controller at 0x%x\n",
333 piix4_smba);
Rudolf Mareka94dd002013-07-14 23:17:26 +0200334 return piix4_smba;
335 }
336
Shane Huang87e19602009-03-28 21:34:46 +0100337 /* Request the SMBus I2C bus config region */
338 if (!request_region(piix4_smba + i2ccfg_offset, 1, "i2ccfg")) {
339 dev_err(&PIIX4_dev->dev, "SMBus I2C bus config region "
340 "0x%x already in use!\n", piix4_smba + i2ccfg_offset);
341 release_region(piix4_smba, SMBIOSIZE);
Shane Huang87e19602009-03-28 21:34:46 +0100342 return -EBUSY;
343 }
344 i2ccfg = inb_p(piix4_smba + i2ccfg_offset);
345 release_region(piix4_smba + i2ccfg_offset, 1);
346
347 if (i2ccfg & 1)
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100348 dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
Shane Huang87e19602009-03-28 21:34:46 +0100349 else
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100350 dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
Shane Huang87e19602009-03-28 21:34:46 +0100351
352 dev_info(&PIIX4_dev->dev,
353 "SMBus Host Controller at 0x%x, revision %d\n",
354 piix4_smba, i2ccfg >> 4);
355
Jean Delvare6befa3f2016-02-17 10:26:35 +0100356 /* Find which register is used for port selection */
357 if (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD) {
Guenter Roeck717ef37d2017-07-15 16:51:26 -0700358 switch (PIIX4_dev->device) {
359 case PCI_DEVICE_ID_AMD_KERNCZ_SMBUS:
360 piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_KERNCZ;
361 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK_KERNCZ;
362 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ;
363 break;
364 case PCI_DEVICE_ID_AMD_HUDSON2_SMBUS:
365 default:
366 piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_ALT;
367 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
368 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
369 break;
370 }
Jean Delvare6befa3f2016-02-17 10:26:35 +0100371 } else {
372 mutex_lock(&piix4_mutex_sb800);
373 outb_p(SB800_PIIX4_PORT_IDX_SEL, SB800_PIIX4_SMB_IDX);
374 port_sel = inb_p(SB800_PIIX4_SMB_IDX + 1);
375 piix4_port_sel_sb800 = (port_sel & 0x01) ?
376 SB800_PIIX4_PORT_IDX_ALT :
377 SB800_PIIX4_PORT_IDX;
Guenter Roeck717ef37d2017-07-15 16:51:26 -0700378 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
379 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
Jean Delvare6befa3f2016-02-17 10:26:35 +0100380 mutex_unlock(&piix4_mutex_sb800);
381 }
382
383 dev_info(&PIIX4_dev->dev,
384 "Using register 0x%02x for SMBus port selection\n",
385 (unsigned int)piix4_port_sel_sb800);
386
Andrew Armenia14a80862012-07-24 14:13:56 +0200387 return piix4_smba;
Shane Huang87e19602009-03-28 21:34:46 +0100388}
389
Bill Pemberton0b255e92012-11-27 15:59:38 -0500390static int piix4_setup_aux(struct pci_dev *PIIX4_dev,
391 const struct pci_device_id *id,
392 unsigned short base_reg_addr)
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200393{
394 /* Set up auxiliary SMBus controllers found on some
395 * AMD chipsets e.g. SP5100 (SB700 derivative) */
396
397 unsigned short piix4_smba;
398
399 /* Read address of auxiliary SMBus controller */
400 pci_read_config_word(PIIX4_dev, base_reg_addr, &piix4_smba);
401 if ((piix4_smba & 1) == 0) {
402 dev_dbg(&PIIX4_dev->dev,
403 "Auxiliary SMBus controller not enabled\n");
404 return -ENODEV;
405 }
406
407 piix4_smba &= 0xfff0;
408 if (piix4_smba == 0) {
409 dev_dbg(&PIIX4_dev->dev,
410 "Auxiliary SMBus base address uninitialized\n");
411 return -ENODEV;
412 }
413
414 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
415 return -ENODEV;
416
417 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
418 dev_err(&PIIX4_dev->dev, "Auxiliary SMBus region 0x%x "
419 "already in use!\n", piix4_smba);
420 return -EBUSY;
421 }
422
423 dev_info(&PIIX4_dev->dev,
424 "Auxiliary SMBus Host Controller at 0x%x\n",
425 piix4_smba);
426
427 return piix4_smba;
428}
429
Andrew Armeniae154bf62012-07-24 14:13:56 +0200430static int piix4_transaction(struct i2c_adapter *piix4_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Andrew Armeniae154bf62012-07-24 14:13:56 +0200432 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(piix4_adapter);
433 unsigned short piix4_smba = adapdata->smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 int temp;
435 int result = 0;
436 int timeout = 0;
437
Andrew Armeniae154bf62012-07-24 14:13:56 +0200438 dev_dbg(&piix4_adapter->dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
440 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
441 inb_p(SMBHSTDAT1));
442
443 /* Make sure the SMBus host is ready to start transmitting */
444 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200445 dev_dbg(&piix4_adapter->dev, "SMBus busy (%02x). "
Jean Delvare541e6a02005-06-23 22:18:08 +0200446 "Resetting...\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 outb_p(temp, SMBHSTSTS);
448 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200449 dev_err(&piix4_adapter->dev, "Failed! (%02x)\n", temp);
David Brownell97140342008-07-14 22:38:25 +0200450 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 } else {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200452 dev_dbg(&piix4_adapter->dev, "Successful!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 }
455
456 /* start the transaction by setting bit 6 */
457 outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
458
459 /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
David Milburnb1c17592008-05-11 20:37:05 +0200460 if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */
461 msleep(2);
462 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 msleep(1);
David Milburnb1c17592008-05-11 20:37:05 +0200464
Roel Kluinb6a31952010-01-16 20:43:12 +0100465 while ((++timeout < MAX_TIMEOUT) &&
David Milburnb1c17592008-05-11 20:37:05 +0200466 ((temp = inb_p(SMBHSTSTS)) & 0x01))
467 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 /* If the SMBus is still busy, we give up */
Roel Kluinb6a31952010-01-16 20:43:12 +0100470 if (timeout == MAX_TIMEOUT) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200471 dev_err(&piix4_adapter->dev, "SMBus Timeout!\n");
David Brownell97140342008-07-14 22:38:25 +0200472 result = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
475 if (temp & 0x10) {
David Brownell97140342008-07-14 22:38:25 +0200476 result = -EIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200477 dev_err(&piix4_adapter->dev, "Error: Failed bus transaction\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479
480 if (temp & 0x08) {
David Brownell97140342008-07-14 22:38:25 +0200481 result = -EIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200482 dev_dbg(&piix4_adapter->dev, "Bus collision! SMBus may be "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 "locked until next hard reset. (sorry!)\n");
484 /* Clock stops and slave is stuck in mid-transmission */
485 }
486
487 if (temp & 0x04) {
David Brownell97140342008-07-14 22:38:25 +0200488 result = -ENXIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200489 dev_dbg(&piix4_adapter->dev, "Error: no response!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491
492 if (inb_p(SMBHSTSTS) != 0x00)
493 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
494
495 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200496 dev_err(&piix4_adapter->dev, "Failed reset at end of "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 "transaction (%02x)\n", temp);
498 }
Andrew Armeniae154bf62012-07-24 14:13:56 +0200499 dev_dbg(&piix4_adapter->dev, "Transaction (post): CNT=%02x, CMD=%02x, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
501 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
502 inb_p(SMBHSTDAT1));
503 return result;
504}
505
David Brownell97140342008-07-14 22:38:25 +0200506/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
508 unsigned short flags, char read_write,
509 u8 command, int size, union i2c_smbus_data * data)
510{
Andrew Armenia14a80862012-07-24 14:13:56 +0200511 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
512 unsigned short piix4_smba = adapdata->smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 int i, len;
David Brownell97140342008-07-14 22:38:25 +0200514 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 case I2C_SMBUS_QUICK:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200518 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 SMBHSTADD);
520 size = PIIX4_QUICK;
521 break;
522 case I2C_SMBUS_BYTE:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200523 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 SMBHSTADD);
525 if (read_write == I2C_SMBUS_WRITE)
526 outb_p(command, SMBHSTCMD);
527 size = PIIX4_BYTE;
528 break;
529 case I2C_SMBUS_BYTE_DATA:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200530 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 SMBHSTADD);
532 outb_p(command, SMBHSTCMD);
533 if (read_write == I2C_SMBUS_WRITE)
534 outb_p(data->byte, SMBHSTDAT0);
535 size = PIIX4_BYTE_DATA;
536 break;
537 case I2C_SMBUS_WORD_DATA:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200538 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 SMBHSTADD);
540 outb_p(command, SMBHSTCMD);
541 if (read_write == I2C_SMBUS_WRITE) {
542 outb_p(data->word & 0xff, SMBHSTDAT0);
543 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
544 }
545 size = PIIX4_WORD_DATA;
546 break;
547 case I2C_SMBUS_BLOCK_DATA:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200548 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 SMBHSTADD);
550 outb_p(command, SMBHSTCMD);
551 if (read_write == I2C_SMBUS_WRITE) {
552 len = data->block[0];
Jean Delvarefa63cd52008-07-14 22:38:25 +0200553 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
554 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 outb_p(len, SMBHSTDAT0);
Wolfram Sangd7a4c762015-11-30 14:43:09 +0100556 inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 for (i = 1; i <= len; i++)
558 outb_p(data->block[i], SMBBLKDAT);
559 }
560 size = PIIX4_BLOCK_DATA;
561 break;
Jean Delvareac7fc4f2008-07-14 22:38:25 +0200562 default:
563 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
564 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
567 outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
568
Andrew Armeniae154bf62012-07-24 14:13:56 +0200569 status = piix4_transaction(adap);
David Brownell97140342008-07-14 22:38:25 +0200570 if (status)
571 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
574 return 0;
575
576
577 switch (size) {
Jean Delvare3578a072008-04-29 23:11:37 +0200578 case PIIX4_BYTE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 case PIIX4_BYTE_DATA:
580 data->byte = inb_p(SMBHSTDAT0);
581 break;
582 case PIIX4_WORD_DATA:
583 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
584 break;
585 case PIIX4_BLOCK_DATA:
586 data->block[0] = inb_p(SMBHSTDAT0);
Jean Delvarefa63cd52008-07-14 22:38:25 +0200587 if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX)
588 return -EPROTO;
Wolfram Sangd7a4c762015-11-30 14:43:09 +0100589 inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 for (i = 1; i <= data->block[0]; i++)
591 data->block[i] = inb_p(SMBBLKDAT);
592 break;
593 }
594 return 0;
595}
596
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100597/*
598 * Handles access to multiple SMBus ports on the SB800.
599 * The port is selected by bits 2:1 of the smb_en register (0x2c).
600 * Returns negative errno on error.
601 *
602 * Note: The selected port must be returned to the initial selection to avoid
603 * problems on certain systems.
604 */
605static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr,
606 unsigned short flags, char read_write,
607 u8 command, int size, union i2c_smbus_data *data)
608{
609 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
Ricardo Ribalda Delgado64e23682017-01-11 10:11:44 +0100610 unsigned short piix4_smba = adapdata->smba;
611 int retries = MAX_TIMEOUT;
612 int smbslvcnt;
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100613 u8 smba_en_lo;
614 u8 port;
615 int retval;
616
Ricardo Ribaldac1a43062017-05-23 21:53:44 -0400617 mutex_lock(&piix4_mutex_sb800);
618
Ricardo Ribalda Delgado64e23682017-01-11 10:11:44 +0100619 /* Request the SMBUS semaphore, avoid conflicts with the IMC */
620 smbslvcnt = inb_p(SMBSLVCNT);
621 do {
622 outb_p(smbslvcnt | 0x10, SMBSLVCNT);
623
624 /* Check the semaphore status */
625 smbslvcnt = inb_p(SMBSLVCNT);
626 if (smbslvcnt & 0x10)
627 break;
628
629 usleep_range(1000, 2000);
630 } while (--retries);
631 /* SMBus is still owned by the IMC, we give up */
Ricardo Ribaldac1a43062017-05-23 21:53:44 -0400632 if (!retries) {
633 mutex_unlock(&piix4_mutex_sb800);
Ricardo Ribalda Delgado64e23682017-01-11 10:11:44 +0100634 return -EBUSY;
Ricardo Ribaldac1a43062017-05-23 21:53:44 -0400635 }
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100636
Jean Delvare6befa3f2016-02-17 10:26:35 +0100637 outb_p(piix4_port_sel_sb800, SB800_PIIX4_SMB_IDX);
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100638 smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
639
640 port = adapdata->port;
Guenter Roeck717ef37d2017-07-15 16:51:26 -0700641 if ((smba_en_lo & piix4_port_mask_sb800) != port)
642 outb_p((smba_en_lo & ~piix4_port_mask_sb800) | port,
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100643 SB800_PIIX4_SMB_IDX + 1);
644
645 retval = piix4_access(adap, addr, flags, read_write,
646 command, size, data);
647
648 outb_p(smba_en_lo, SB800_PIIX4_SMB_IDX + 1);
649
Ricardo Ribalda Delgado64e23682017-01-11 10:11:44 +0100650 /* Release the semaphore */
651 outb_p(smbslvcnt | 0x20, SMBSLVCNT);
652
Ricardo Ribaldac1a43062017-05-23 21:53:44 -0400653 mutex_unlock(&piix4_mutex_sb800);
654
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100655 return retval;
656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658static u32 piix4_func(struct i2c_adapter *adapter)
659{
660 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
661 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
662 I2C_FUNC_SMBUS_BLOCK_DATA;
663}
664
Jean Delvare8f9082c2006-09-03 22:39:46 +0200665static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 .smbus_xfer = piix4_access,
667 .functionality = piix4_func,
668};
669
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100670static const struct i2c_algorithm piix4_smbus_algorithm_sb800 = {
671 .smbus_xfer = piix4_access_sb800,
672 .functionality = piix4_func,
673};
674
Jingoo Han392debf2013-12-03 08:11:20 +0900675static const struct pci_device_id piix4_ids[] = {
Jean Delvare9b7389c2008-01-27 18:14:51 +0100676 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
677 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3) },
678 { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3) },
679 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_SMBUS) },
680 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_SMBUS) },
681 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS) },
682 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS) },
Crane Cai3806e94b2009-11-07 13:10:46 +0100683 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) },
Vincent Wanbcb29992015-06-11 20:11:46 +0800684 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) },
Jean Delvare9b7389c2008-01-27 18:14:51 +0100685 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
686 PCI_DEVICE_ID_SERVERWORKS_OSB4) },
687 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
688 PCI_DEVICE_ID_SERVERWORKS_CSB5) },
689 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
690 PCI_DEVICE_ID_SERVERWORKS_CSB6) },
691 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
692 PCI_DEVICE_ID_SERVERWORKS_HT1000SB) },
Flavio Leitner506a8b62009-03-28 21:34:46 +0100693 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
694 PCI_DEVICE_ID_SERVERWORKS_HT1100LD) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 { 0, }
696};
697
698MODULE_DEVICE_TABLE (pci, piix4_ids);
699
Christian Fetzerca2061e2015-11-19 20:13:47 +0100700static struct i2c_adapter *piix4_main_adapters[PIIX4_MAX_ADAPTERS];
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200701static struct i2c_adapter *piix4_aux_adapter;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200702
Bill Pemberton0b255e92012-11-27 15:59:38 -0500703static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
Jean Delvare62194e82016-01-29 10:45:30 +0100704 bool sb800_main, u8 port,
Christian Fetzer725d2e32015-11-19 20:13:49 +0100705 const char *name, struct i2c_adapter **padap)
Andrew Armeniae154bf62012-07-24 14:13:56 +0200706{
707 struct i2c_adapter *adap;
708 struct i2c_piix4_adapdata *adapdata;
709 int retval;
710
711 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
712 if (adap == NULL) {
713 release_region(smba, SMBIOSIZE);
714 return -ENOMEM;
715 }
716
717 adap->owner = THIS_MODULE;
718 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Jean Delvare83c60152016-01-25 12:17:07 +0100719 adap->algo = sb800_main ? &piix4_smbus_algorithm_sb800
720 : &smbus_algorithm;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200721
722 adapdata = kzalloc(sizeof(*adapdata), GFP_KERNEL);
723 if (adapdata == NULL) {
724 kfree(adap);
725 release_region(smba, SMBIOSIZE);
726 return -ENOMEM;
727 }
728
729 adapdata->smba = smba;
Jean Delvare83c60152016-01-25 12:17:07 +0100730 adapdata->sb800_main = sb800_main;
Guenter Roeck717ef37d2017-07-15 16:51:26 -0700731 adapdata->port = port << piix4_port_shift_sb800;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200732
733 /* set up the sysfs linkage to our parent device */
734 adap->dev.parent = &dev->dev;
735
736 snprintf(adap->name, sizeof(adap->name),
Jean Delvare52795f62016-01-27 14:40:33 +0100737 "SMBus PIIX4 adapter%s at %04x", name, smba);
Andrew Armeniae154bf62012-07-24 14:13:56 +0200738
739 i2c_set_adapdata(adap, adapdata);
740
741 retval = i2c_add_adapter(adap);
742 if (retval) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200743 kfree(adapdata);
744 kfree(adap);
745 release_region(smba, SMBIOSIZE);
746 return retval;
747 }
748
749 *padap = adap;
750 return 0;
751}
752
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100753static int piix4_add_adapters_sb800(struct pci_dev *dev, unsigned short smba)
754{
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100755 struct i2c_piix4_adapdata *adapdata;
756 int port;
757 int retval;
758
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100759 for (port = 0; port < PIIX4_MAX_ADAPTERS; port++) {
Jean Delvare83c60152016-01-25 12:17:07 +0100760 retval = piix4_add_adapter(dev, smba, true, port,
Christian Fetzer725d2e32015-11-19 20:13:49 +0100761 piix4_main_port_names_sb800[port],
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100762 &piix4_main_adapters[port]);
763 if (retval < 0)
764 goto error;
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100765 }
766
767 return retval;
768
769error:
770 dev_err(&dev->dev,
771 "Error setting up SB800 adapters. Unregistering!\n");
772 while (--port >= 0) {
773 adapdata = i2c_get_adapdata(piix4_main_adapters[port]);
774 if (adapdata->smba) {
775 i2c_del_adapter(piix4_main_adapters[port]);
776 kfree(adapdata);
777 kfree(piix4_main_adapters[port]);
778 piix4_main_adapters[port] = NULL;
779 }
780 }
781
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100782 return retval;
783}
784
Bill Pemberton0b255e92012-11-27 15:59:38 -0500785static int piix4_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 int retval;
Jean Delvare52795f62016-01-27 14:40:33 +0100788 bool is_sb800 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Crane Cai76b3e282009-09-18 22:45:50 +0200790 if ((dev->vendor == PCI_VENDOR_ID_ATI &&
791 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS &&
792 dev->revision >= 0x40) ||
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100793 dev->vendor == PCI_VENDOR_ID_AMD) {
Jean Delvare52795f62016-01-27 14:40:33 +0100794 is_sb800 = true;
795
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100796 if (!request_region(SB800_PIIX4_SMB_IDX, 2, "smba_idx")) {
797 dev_err(&dev->dev,
798 "SMBus base address index region 0x%x already in use!\n",
799 SB800_PIIX4_SMB_IDX);
800 return -EBUSY;
801 }
802
Shane Huang87e19602009-03-28 21:34:46 +0100803 /* base address location etc changed in SB800 */
Rudolf Mareka94dd002013-07-14 23:17:26 +0200804 retval = piix4_setup_sb800(dev, id, 0);
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100805 if (retval < 0) {
806 release_region(SB800_PIIX4_SMB_IDX, 2);
807 return retval;
808 }
809
810 /*
811 * Try to register multiplexed main SMBus adapter,
812 * give up if we can't
813 */
814 retval = piix4_add_adapters_sb800(dev, retval);
815 if (retval < 0) {
816 release_region(SB800_PIIX4_SMB_IDX, 2);
817 return retval;
818 }
819 } else {
Shane Huang87e19602009-03-28 21:34:46 +0100820 retval = piix4_setup(dev, id);
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100821 if (retval < 0)
822 return retval;
Shane Huang87e19602009-03-28 21:34:46 +0100823
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100824 /* Try to register main SMBus adapter, give up if we can't */
Jean Delvare52795f62016-01-27 14:40:33 +0100825 retval = piix4_add_adapter(dev, retval, false, 0, "",
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100826 &piix4_main_adapters[0]);
827 if (retval < 0)
828 return retval;
829 }
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200830
831 /* Check for auxiliary SMBus on some AMD chipsets */
Rudolf Mareka94dd002013-07-14 23:17:26 +0200832 retval = -ENODEV;
833
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200834 if (dev->vendor == PCI_VENDOR_ID_ATI &&
Rudolf Mareka94dd002013-07-14 23:17:26 +0200835 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS) {
836 if (dev->revision < 0x40) {
837 retval = piix4_setup_aux(dev, id, 0x58);
838 } else {
839 /* SB800 added aux bus too */
840 retval = piix4_setup_sb800(dev, id, 1);
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200841 }
842 }
843
Rudolf Mareka94dd002013-07-14 23:17:26 +0200844 if (dev->vendor == PCI_VENDOR_ID_AMD &&
845 dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) {
846 retval = piix4_setup_sb800(dev, id, 1);
847 }
848
849 if (retval > 0) {
850 /* Try to add the aux adapter if it exists,
851 * piix4_add_adapter will clean up if this fails */
Jean Delvare83c60152016-01-25 12:17:07 +0100852 piix4_add_adapter(dev, retval, false, 0,
Jean Delvare52795f62016-01-27 14:40:33 +0100853 is_sb800 ? piix4_aux_port_name_sb800 : "",
Christian Fetzer725d2e32015-11-19 20:13:49 +0100854 &piix4_aux_adapter);
Rudolf Mareka94dd002013-07-14 23:17:26 +0200855 }
856
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Bill Pemberton0b255e92012-11-27 15:59:38 -0500860static void piix4_adap_remove(struct i2c_adapter *adap)
Andrew Armenia14a80862012-07-24 14:13:56 +0200861{
862 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
863
864 if (adapdata->smba) {
865 i2c_del_adapter(adap);
Jean Delvare33f5ccc2016-01-29 10:46:37 +0100866 if (adapdata->port == (0 << 1)) {
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100867 release_region(adapdata->smba, SMBIOSIZE);
Jean Delvarea28e3512016-01-22 14:12:02 +0100868 if (adapdata->sb800_main)
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100869 release_region(SB800_PIIX4_SMB_IDX, 2);
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100870 }
Andrew Armeniae154bf62012-07-24 14:13:56 +0200871 kfree(adapdata);
872 kfree(adap);
Andrew Armenia14a80862012-07-24 14:13:56 +0200873 }
874}
875
Bill Pemberton0b255e92012-11-27 15:59:38 -0500876static void piix4_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Christian Fetzerca2061e2015-11-19 20:13:47 +0100878 int port = PIIX4_MAX_ADAPTERS;
879
880 while (--port >= 0) {
881 if (piix4_main_adapters[port]) {
882 piix4_adap_remove(piix4_main_adapters[port]);
883 piix4_main_adapters[port] = NULL;
884 }
Andrew Armeniae154bf62012-07-24 14:13:56 +0200885 }
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200886
887 if (piix4_aux_adapter) {
888 piix4_adap_remove(piix4_aux_adapter);
889 piix4_aux_adapter = NULL;
890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893static struct pci_driver piix4_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 .name = "piix4_smbus",
895 .id_table = piix4_ids,
896 .probe = piix4_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500897 .remove = piix4_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898};
899
Axel Lin56f21782012-07-24 14:13:56 +0200900module_pci_driver(piix4_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
903 "Philip Edelbrock <phil@netroedge.com>");
904MODULE_DESCRIPTION("PIIX4 SMBus driver");
905MODULE_LICENSE("GPL");