blob: 62785aa76b3fb5a1e09f63c7b639bcebc7766733 [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
Andrew Cooks625ee1a2019-08-02 14:52:46 +020099/* On kerncz and Hudson2, SmBus0Sel is at bit 20:19 of PMx00 DecodeEn */
Guenter Roeck717ef37d2017-07-15 16:51:26 -0700100#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) {
Andrew Cooks625ee1a2019-08-02 14:52:46 +0200358 if (PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS ||
359 (PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
360 PIIX4_dev->revision >= 0x1F)) {
Guenter Roeck717ef37d2017-07-15 16:51:26 -0700361 piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_KERNCZ;
362 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK_KERNCZ;
363 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ;
Andrew Cooks625ee1a2019-08-02 14:52:46 +0200364 } else {
Guenter Roeck717ef37d2017-07-15 16:51:26 -0700365 piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_ALT;
366 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
367 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
Guenter Roeck717ef37d2017-07-15 16:51:26 -0700368 }
Jean Delvare6befa3f2016-02-17 10:26:35 +0100369 } else {
370 mutex_lock(&piix4_mutex_sb800);
371 outb_p(SB800_PIIX4_PORT_IDX_SEL, SB800_PIIX4_SMB_IDX);
372 port_sel = inb_p(SB800_PIIX4_SMB_IDX + 1);
373 piix4_port_sel_sb800 = (port_sel & 0x01) ?
374 SB800_PIIX4_PORT_IDX_ALT :
375 SB800_PIIX4_PORT_IDX;
Guenter Roeck717ef37d2017-07-15 16:51:26 -0700376 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
377 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
Jean Delvare6befa3f2016-02-17 10:26:35 +0100378 mutex_unlock(&piix4_mutex_sb800);
379 }
380
381 dev_info(&PIIX4_dev->dev,
382 "Using register 0x%02x for SMBus port selection\n",
383 (unsigned int)piix4_port_sel_sb800);
384
Andrew Armenia14a80862012-07-24 14:13:56 +0200385 return piix4_smba;
Shane Huang87e19602009-03-28 21:34:46 +0100386}
387
Bill Pemberton0b255e92012-11-27 15:59:38 -0500388static int piix4_setup_aux(struct pci_dev *PIIX4_dev,
389 const struct pci_device_id *id,
390 unsigned short base_reg_addr)
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200391{
392 /* Set up auxiliary SMBus controllers found on some
393 * AMD chipsets e.g. SP5100 (SB700 derivative) */
394
395 unsigned short piix4_smba;
396
397 /* Read address of auxiliary SMBus controller */
398 pci_read_config_word(PIIX4_dev, base_reg_addr, &piix4_smba);
399 if ((piix4_smba & 1) == 0) {
400 dev_dbg(&PIIX4_dev->dev,
401 "Auxiliary SMBus controller not enabled\n");
402 return -ENODEV;
403 }
404
405 piix4_smba &= 0xfff0;
406 if (piix4_smba == 0) {
407 dev_dbg(&PIIX4_dev->dev,
408 "Auxiliary SMBus base address uninitialized\n");
409 return -ENODEV;
410 }
411
412 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
413 return -ENODEV;
414
415 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
416 dev_err(&PIIX4_dev->dev, "Auxiliary SMBus region 0x%x "
417 "already in use!\n", piix4_smba);
418 return -EBUSY;
419 }
420
421 dev_info(&PIIX4_dev->dev,
422 "Auxiliary SMBus Host Controller at 0x%x\n",
423 piix4_smba);
424
425 return piix4_smba;
426}
427
Andrew Armeniae154bf62012-07-24 14:13:56 +0200428static int piix4_transaction(struct i2c_adapter *piix4_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Andrew Armeniae154bf62012-07-24 14:13:56 +0200430 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(piix4_adapter);
431 unsigned short piix4_smba = adapdata->smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int temp;
433 int result = 0;
434 int timeout = 0;
435
Andrew Armeniae154bf62012-07-24 14:13:56 +0200436 dev_dbg(&piix4_adapter->dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
438 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
439 inb_p(SMBHSTDAT1));
440
441 /* Make sure the SMBus host is ready to start transmitting */
442 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200443 dev_dbg(&piix4_adapter->dev, "SMBus busy (%02x). "
Jean Delvare541e6a02005-06-23 22:18:08 +0200444 "Resetting...\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 outb_p(temp, SMBHSTSTS);
446 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200447 dev_err(&piix4_adapter->dev, "Failed! (%02x)\n", temp);
David Brownell97140342008-07-14 22:38:25 +0200448 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 } else {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200450 dev_dbg(&piix4_adapter->dev, "Successful!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452 }
453
454 /* start the transaction by setting bit 6 */
455 outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
456
457 /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
David Milburnb1c17592008-05-11 20:37:05 +0200458 if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */
459 msleep(2);
460 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 msleep(1);
David Milburnb1c17592008-05-11 20:37:05 +0200462
Roel Kluinb6a31952010-01-16 20:43:12 +0100463 while ((++timeout < MAX_TIMEOUT) &&
David Milburnb1c17592008-05-11 20:37:05 +0200464 ((temp = inb_p(SMBHSTSTS)) & 0x01))
465 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /* If the SMBus is still busy, we give up */
Roel Kluinb6a31952010-01-16 20:43:12 +0100468 if (timeout == MAX_TIMEOUT) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200469 dev_err(&piix4_adapter->dev, "SMBus Timeout!\n");
David Brownell97140342008-07-14 22:38:25 +0200470 result = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
473 if (temp & 0x10) {
David Brownell97140342008-07-14 22:38:25 +0200474 result = -EIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200475 dev_err(&piix4_adapter->dev, "Error: Failed bus transaction\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477
478 if (temp & 0x08) {
David Brownell97140342008-07-14 22:38:25 +0200479 result = -EIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200480 dev_dbg(&piix4_adapter->dev, "Bus collision! SMBus may be "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 "locked until next hard reset. (sorry!)\n");
482 /* Clock stops and slave is stuck in mid-transmission */
483 }
484
485 if (temp & 0x04) {
David Brownell97140342008-07-14 22:38:25 +0200486 result = -ENXIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200487 dev_dbg(&piix4_adapter->dev, "Error: no response!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
490 if (inb_p(SMBHSTSTS) != 0x00)
491 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
492
493 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200494 dev_err(&piix4_adapter->dev, "Failed reset at end of "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 "transaction (%02x)\n", temp);
496 }
Andrew Armeniae154bf62012-07-24 14:13:56 +0200497 dev_dbg(&piix4_adapter->dev, "Transaction (post): CNT=%02x, CMD=%02x, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
499 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
500 inb_p(SMBHSTDAT1));
501 return result;
502}
503
David Brownell97140342008-07-14 22:38:25 +0200504/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
506 unsigned short flags, char read_write,
507 u8 command, int size, union i2c_smbus_data * data)
508{
Andrew Armenia14a80862012-07-24 14:13:56 +0200509 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
510 unsigned short piix4_smba = adapdata->smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 int i, len;
David Brownell97140342008-07-14 22:38:25 +0200512 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 case I2C_SMBUS_QUICK:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200516 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 SMBHSTADD);
518 size = PIIX4_QUICK;
519 break;
520 case I2C_SMBUS_BYTE:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200521 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 SMBHSTADD);
523 if (read_write == I2C_SMBUS_WRITE)
524 outb_p(command, SMBHSTCMD);
525 size = PIIX4_BYTE;
526 break;
527 case I2C_SMBUS_BYTE_DATA:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200528 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 SMBHSTADD);
530 outb_p(command, SMBHSTCMD);
531 if (read_write == I2C_SMBUS_WRITE)
532 outb_p(data->byte, SMBHSTDAT0);
533 size = PIIX4_BYTE_DATA;
534 break;
535 case I2C_SMBUS_WORD_DATA:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200536 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 SMBHSTADD);
538 outb_p(command, SMBHSTCMD);
539 if (read_write == I2C_SMBUS_WRITE) {
540 outb_p(data->word & 0xff, SMBHSTDAT0);
541 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
542 }
543 size = PIIX4_WORD_DATA;
544 break;
545 case I2C_SMBUS_BLOCK_DATA:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200546 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 SMBHSTADD);
548 outb_p(command, SMBHSTCMD);
549 if (read_write == I2C_SMBUS_WRITE) {
550 len = data->block[0];
Jean Delvarefa63cd52008-07-14 22:38:25 +0200551 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
552 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 outb_p(len, SMBHSTDAT0);
Wolfram Sangd7a4c762015-11-30 14:43:09 +0100554 inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 for (i = 1; i <= len; i++)
556 outb_p(data->block[i], SMBBLKDAT);
557 }
558 size = PIIX4_BLOCK_DATA;
559 break;
Jean Delvareac7fc4f2008-07-14 22:38:25 +0200560 default:
561 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
562 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
565 outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
566
Andrew Armeniae154bf62012-07-24 14:13:56 +0200567 status = piix4_transaction(adap);
David Brownell97140342008-07-14 22:38:25 +0200568 if (status)
569 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
572 return 0;
573
574
575 switch (size) {
Jean Delvare3578a072008-04-29 23:11:37 +0200576 case PIIX4_BYTE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 case PIIX4_BYTE_DATA:
578 data->byte = inb_p(SMBHSTDAT0);
579 break;
580 case PIIX4_WORD_DATA:
581 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
582 break;
583 case PIIX4_BLOCK_DATA:
584 data->block[0] = inb_p(SMBHSTDAT0);
Jean Delvarefa63cd52008-07-14 22:38:25 +0200585 if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX)
586 return -EPROTO;
Wolfram Sangd7a4c762015-11-30 14:43:09 +0100587 inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 for (i = 1; i <= data->block[0]; i++)
589 data->block[i] = inb_p(SMBBLKDAT);
590 break;
591 }
592 return 0;
593}
594
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100595/*
596 * Handles access to multiple SMBus ports on the SB800.
597 * The port is selected by bits 2:1 of the smb_en register (0x2c).
598 * Returns negative errno on error.
599 *
600 * Note: The selected port must be returned to the initial selection to avoid
601 * problems on certain systems.
602 */
603static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr,
604 unsigned short flags, char read_write,
605 u8 command, int size, union i2c_smbus_data *data)
606{
607 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
Ricardo Ribalda Delgado64e23682017-01-11 10:11:44 +0100608 unsigned short piix4_smba = adapdata->smba;
609 int retries = MAX_TIMEOUT;
610 int smbslvcnt;
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100611 u8 smba_en_lo;
612 u8 port;
613 int retval;
614
Ricardo Ribaldac1a43062017-05-23 21:53:44 -0400615 mutex_lock(&piix4_mutex_sb800);
616
Ricardo Ribalda Delgado64e23682017-01-11 10:11:44 +0100617 /* Request the SMBUS semaphore, avoid conflicts with the IMC */
618 smbslvcnt = inb_p(SMBSLVCNT);
619 do {
620 outb_p(smbslvcnt | 0x10, SMBSLVCNT);
621
622 /* Check the semaphore status */
623 smbslvcnt = inb_p(SMBSLVCNT);
624 if (smbslvcnt & 0x10)
625 break;
626
627 usleep_range(1000, 2000);
628 } while (--retries);
629 /* SMBus is still owned by the IMC, we give up */
Ricardo Ribaldac1a43062017-05-23 21:53:44 -0400630 if (!retries) {
631 mutex_unlock(&piix4_mutex_sb800);
Ricardo Ribalda Delgado64e23682017-01-11 10:11:44 +0100632 return -EBUSY;
Ricardo Ribaldac1a43062017-05-23 21:53:44 -0400633 }
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100634
Jean Delvare6befa3f2016-02-17 10:26:35 +0100635 outb_p(piix4_port_sel_sb800, SB800_PIIX4_SMB_IDX);
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100636 smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
637
638 port = adapdata->port;
Guenter Roeck717ef37d2017-07-15 16:51:26 -0700639 if ((smba_en_lo & piix4_port_mask_sb800) != port)
640 outb_p((smba_en_lo & ~piix4_port_mask_sb800) | port,
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100641 SB800_PIIX4_SMB_IDX + 1);
642
643 retval = piix4_access(adap, addr, flags, read_write,
644 command, size, data);
645
646 outb_p(smba_en_lo, SB800_PIIX4_SMB_IDX + 1);
647
Ricardo Ribalda Delgado64e23682017-01-11 10:11:44 +0100648 /* Release the semaphore */
649 outb_p(smbslvcnt | 0x20, SMBSLVCNT);
650
Ricardo Ribaldac1a43062017-05-23 21:53:44 -0400651 mutex_unlock(&piix4_mutex_sb800);
652
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100653 return retval;
654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656static u32 piix4_func(struct i2c_adapter *adapter)
657{
658 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
659 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
660 I2C_FUNC_SMBUS_BLOCK_DATA;
661}
662
Jean Delvare8f9082c2006-09-03 22:39:46 +0200663static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 .smbus_xfer = piix4_access,
665 .functionality = piix4_func,
666};
667
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100668static const struct i2c_algorithm piix4_smbus_algorithm_sb800 = {
669 .smbus_xfer = piix4_access_sb800,
670 .functionality = piix4_func,
671};
672
Jingoo Han392debf2013-12-03 08:11:20 +0900673static const struct pci_device_id piix4_ids[] = {
Jean Delvare9b7389c2008-01-27 18:14:51 +0100674 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
675 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3) },
676 { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3) },
677 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_SMBUS) },
678 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_SMBUS) },
679 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS) },
680 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS) },
Crane Cai3806e94b2009-11-07 13:10:46 +0100681 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) },
Vincent Wanbcb29992015-06-11 20:11:46 +0800682 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) },
Jean Delvare9b7389c2008-01-27 18:14:51 +0100683 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
684 PCI_DEVICE_ID_SERVERWORKS_OSB4) },
685 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
686 PCI_DEVICE_ID_SERVERWORKS_CSB5) },
687 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
688 PCI_DEVICE_ID_SERVERWORKS_CSB6) },
689 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
690 PCI_DEVICE_ID_SERVERWORKS_HT1000SB) },
Flavio Leitner506a8b62009-03-28 21:34:46 +0100691 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
692 PCI_DEVICE_ID_SERVERWORKS_HT1100LD) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 { 0, }
694};
695
696MODULE_DEVICE_TABLE (pci, piix4_ids);
697
Christian Fetzerca2061e2015-11-19 20:13:47 +0100698static struct i2c_adapter *piix4_main_adapters[PIIX4_MAX_ADAPTERS];
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200699static struct i2c_adapter *piix4_aux_adapter;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200700
Bill Pemberton0b255e92012-11-27 15:59:38 -0500701static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
Jean Delvare62194e82016-01-29 10:45:30 +0100702 bool sb800_main, u8 port,
Christian Fetzer725d2e32015-11-19 20:13:49 +0100703 const char *name, struct i2c_adapter **padap)
Andrew Armeniae154bf62012-07-24 14:13:56 +0200704{
705 struct i2c_adapter *adap;
706 struct i2c_piix4_adapdata *adapdata;
707 int retval;
708
709 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
710 if (adap == NULL) {
711 release_region(smba, SMBIOSIZE);
712 return -ENOMEM;
713 }
714
715 adap->owner = THIS_MODULE;
716 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Jean Delvare83c60152016-01-25 12:17:07 +0100717 adap->algo = sb800_main ? &piix4_smbus_algorithm_sb800
718 : &smbus_algorithm;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200719
720 adapdata = kzalloc(sizeof(*adapdata), GFP_KERNEL);
721 if (adapdata == NULL) {
722 kfree(adap);
723 release_region(smba, SMBIOSIZE);
724 return -ENOMEM;
725 }
726
727 adapdata->smba = smba;
Jean Delvare83c60152016-01-25 12:17:07 +0100728 adapdata->sb800_main = sb800_main;
Guenter Roeck717ef37d2017-07-15 16:51:26 -0700729 adapdata->port = port << piix4_port_shift_sb800;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200730
731 /* set up the sysfs linkage to our parent device */
732 adap->dev.parent = &dev->dev;
733
734 snprintf(adap->name, sizeof(adap->name),
Jean Delvare52795f62016-01-27 14:40:33 +0100735 "SMBus PIIX4 adapter%s at %04x", name, smba);
Andrew Armeniae154bf62012-07-24 14:13:56 +0200736
737 i2c_set_adapdata(adap, adapdata);
738
739 retval = i2c_add_adapter(adap);
740 if (retval) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200741 kfree(adapdata);
742 kfree(adap);
743 release_region(smba, SMBIOSIZE);
744 return retval;
745 }
746
747 *padap = adap;
748 return 0;
749}
750
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100751static int piix4_add_adapters_sb800(struct pci_dev *dev, unsigned short smba)
752{
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100753 struct i2c_piix4_adapdata *adapdata;
754 int port;
755 int retval;
756
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100757 for (port = 0; port < PIIX4_MAX_ADAPTERS; port++) {
Jean Delvare83c60152016-01-25 12:17:07 +0100758 retval = piix4_add_adapter(dev, smba, true, port,
Christian Fetzer725d2e32015-11-19 20:13:49 +0100759 piix4_main_port_names_sb800[port],
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100760 &piix4_main_adapters[port]);
761 if (retval < 0)
762 goto error;
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100763 }
764
765 return retval;
766
767error:
768 dev_err(&dev->dev,
769 "Error setting up SB800 adapters. Unregistering!\n");
770 while (--port >= 0) {
771 adapdata = i2c_get_adapdata(piix4_main_adapters[port]);
772 if (adapdata->smba) {
773 i2c_del_adapter(piix4_main_adapters[port]);
774 kfree(adapdata);
775 kfree(piix4_main_adapters[port]);
776 piix4_main_adapters[port] = NULL;
777 }
778 }
779
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100780 return retval;
781}
782
Bill Pemberton0b255e92012-11-27 15:59:38 -0500783static int piix4_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 int retval;
Jean Delvare52795f62016-01-27 14:40:33 +0100786 bool is_sb800 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Crane Cai76b3e282009-09-18 22:45:50 +0200788 if ((dev->vendor == PCI_VENDOR_ID_ATI &&
789 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS &&
790 dev->revision >= 0x40) ||
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100791 dev->vendor == PCI_VENDOR_ID_AMD) {
Jean Delvare52795f62016-01-27 14:40:33 +0100792 is_sb800 = true;
793
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100794 if (!request_region(SB800_PIIX4_SMB_IDX, 2, "smba_idx")) {
795 dev_err(&dev->dev,
796 "SMBus base address index region 0x%x already in use!\n",
797 SB800_PIIX4_SMB_IDX);
798 return -EBUSY;
799 }
800
Shane Huang87e19602009-03-28 21:34:46 +0100801 /* base address location etc changed in SB800 */
Rudolf Mareka94dd002013-07-14 23:17:26 +0200802 retval = piix4_setup_sb800(dev, id, 0);
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100803 if (retval < 0) {
804 release_region(SB800_PIIX4_SMB_IDX, 2);
805 return retval;
806 }
807
808 /*
809 * Try to register multiplexed main SMBus adapter,
810 * give up if we can't
811 */
812 retval = piix4_add_adapters_sb800(dev, retval);
813 if (retval < 0) {
814 release_region(SB800_PIIX4_SMB_IDX, 2);
815 return retval;
816 }
817 } else {
Shane Huang87e19602009-03-28 21:34:46 +0100818 retval = piix4_setup(dev, id);
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100819 if (retval < 0)
820 return retval;
Shane Huang87e19602009-03-28 21:34:46 +0100821
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100822 /* Try to register main SMBus adapter, give up if we can't */
Jean Delvare52795f62016-01-27 14:40:33 +0100823 retval = piix4_add_adapter(dev, retval, false, 0, "",
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100824 &piix4_main_adapters[0]);
825 if (retval < 0)
826 return retval;
827 }
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200828
829 /* Check for auxiliary SMBus on some AMD chipsets */
Rudolf Mareka94dd002013-07-14 23:17:26 +0200830 retval = -ENODEV;
831
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200832 if (dev->vendor == PCI_VENDOR_ID_ATI &&
Rudolf Mareka94dd002013-07-14 23:17:26 +0200833 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS) {
834 if (dev->revision < 0x40) {
835 retval = piix4_setup_aux(dev, id, 0x58);
836 } else {
837 /* SB800 added aux bus too */
838 retval = piix4_setup_sb800(dev, id, 1);
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200839 }
840 }
841
Rudolf Mareka94dd002013-07-14 23:17:26 +0200842 if (dev->vendor == PCI_VENDOR_ID_AMD &&
843 dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) {
844 retval = piix4_setup_sb800(dev, id, 1);
845 }
846
847 if (retval > 0) {
848 /* Try to add the aux adapter if it exists,
849 * piix4_add_adapter will clean up if this fails */
Jean Delvare83c60152016-01-25 12:17:07 +0100850 piix4_add_adapter(dev, retval, false, 0,
Jean Delvare52795f62016-01-27 14:40:33 +0100851 is_sb800 ? piix4_aux_port_name_sb800 : "",
Christian Fetzer725d2e32015-11-19 20:13:49 +0100852 &piix4_aux_adapter);
Rudolf Mareka94dd002013-07-14 23:17:26 +0200853 }
854
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Bill Pemberton0b255e92012-11-27 15:59:38 -0500858static void piix4_adap_remove(struct i2c_adapter *adap)
Andrew Armenia14a80862012-07-24 14:13:56 +0200859{
860 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
861
862 if (adapdata->smba) {
863 i2c_del_adapter(adap);
Jean Delvare33f5ccc2016-01-29 10:46:37 +0100864 if (adapdata->port == (0 << 1)) {
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100865 release_region(adapdata->smba, SMBIOSIZE);
Jean Delvarea28e3512016-01-22 14:12:02 +0100866 if (adapdata->sb800_main)
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100867 release_region(SB800_PIIX4_SMB_IDX, 2);
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100868 }
Andrew Armeniae154bf62012-07-24 14:13:56 +0200869 kfree(adapdata);
870 kfree(adap);
Andrew Armenia14a80862012-07-24 14:13:56 +0200871 }
872}
873
Bill Pemberton0b255e92012-11-27 15:59:38 -0500874static void piix4_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Christian Fetzerca2061e2015-11-19 20:13:47 +0100876 int port = PIIX4_MAX_ADAPTERS;
877
878 while (--port >= 0) {
879 if (piix4_main_adapters[port]) {
880 piix4_adap_remove(piix4_main_adapters[port]);
881 piix4_main_adapters[port] = NULL;
882 }
Andrew Armeniae154bf62012-07-24 14:13:56 +0200883 }
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200884
885 if (piix4_aux_adapter) {
886 piix4_adap_remove(piix4_aux_adapter);
887 piix4_aux_adapter = NULL;
888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891static struct pci_driver piix4_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 .name = "piix4_smbus",
893 .id_table = piix4_ids,
894 .probe = piix4_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500895 .remove = piix4_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896};
897
Axel Lin56f21782012-07-24 14:13:56 +0200898module_pci_driver(piix4_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
901 "Philip Edelbrock <phil@netroedge.com>");
902MODULE_DESCRIPTION("PIIX4 SMBus driver");
903MODULE_LICENSE("GPL");