blob: c46c4bddc7ca360f2241aa1d213da9d8b9dc2486 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl> and
4 Philip Edelbrock <phil@netroedge.com>
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006*/
7
8/*
9 Supports:
10 Intel PIIX4, 440MX
Flavio Leitner506a8b62009-03-28 21:34:46 +010011 Serverworks OSB4, CSB5, CSB6, HT-1000, HT-1100
Andrew Armenia2a2f7402012-07-24 14:13:57 +020012 ATI IXP200, IXP300, IXP400, SB600, SB700/SP5100, SB800
Shane Huang032f7082014-01-22 14:05:46 -080013 AMD Hudson-2, ML, CZ
Pu Wen24beb832019-04-30 00:08:43 +080014 Hygon CZ
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 SMSC Victory66
16
Andrew Armenia2a2f7402012-07-24 14:13:57 +020017 Note: we assume there can only be one device, with one or more
18 SMBus interfaces.
Christian Fetzer2fee61d2015-11-19 20:13:48 +010019 The device can register multiple i2c_adapters (up to PIIX4_MAX_ADAPTERS).
20 For devices supporting multiple ports the i2c_adapter should provide
21 an i2c_algorithm to access them.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022*/
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/pci.h>
27#include <linux/kernel.h>
28#include <linux/delay.h>
29#include <linux/stddef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ioport.h>
31#include <linux/i2c.h>
Daniel J Bluemanc415b302012-10-05 22:23:55 +020032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/dmi.h>
Jean Delvare54fb4a052008-07-14 22:38:33 +020034#include <linux/acpi.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020035#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/* PIIX4 SMBus address offsets */
39#define SMBHSTSTS (0 + piix4_smba)
40#define SMBHSLVSTS (1 + piix4_smba)
41#define SMBHSTCNT (2 + piix4_smba)
42#define SMBHSTCMD (3 + piix4_smba)
43#define SMBHSTADD (4 + piix4_smba)
44#define SMBHSTDAT0 (5 + piix4_smba)
45#define SMBHSTDAT1 (6 + piix4_smba)
46#define SMBBLKDAT (7 + piix4_smba)
47#define SMBSLVCNT (8 + piix4_smba)
48#define SMBSHDWCMD (9 + piix4_smba)
49#define SMBSLVEVT (0xA + piix4_smba)
50#define SMBSLVDAT (0xC + piix4_smba)
51
52/* count for request_region */
Ricardo Ribaldaf43128c2017-01-27 15:59:30 +010053#define SMBIOSIZE 9
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/* PCI Address Constants */
56#define SMBBA 0x090
57#define SMBHSTCFG 0x0D2
58#define SMBSLVC 0x0D3
59#define SMBSHDW1 0x0D4
60#define SMBSHDW2 0x0D5
61#define SMBREV 0x0D6
62
63/* Other settings */
64#define MAX_TIMEOUT 500
65#define ENABLE_INT9 0
66
67/* PIIX4 constants */
68#define PIIX4_QUICK 0x00
69#define PIIX4_BYTE 0x04
70#define PIIX4_BYTE_DATA 0x08
71#define PIIX4_WORD_DATA 0x0C
72#define PIIX4_BLOCK_DATA 0x14
73
Christian Fetzerca2061e2015-11-19 20:13:47 +010074/* Multi-port constants */
75#define PIIX4_MAX_ADAPTERS 4
76
Christian Fetzer2fee61d2015-11-19 20:13:48 +010077/* SB800 constants */
78#define SB800_PIIX4_SMB_IDX 0xcd6
79
Ricardo Ribalda Delgado88fa2df2017-10-10 18:11:15 +020080#define KERNCZ_IMC_IDX 0x3e
81#define KERNCZ_IMC_DATA 0x3f
82
Jean Delvare6befa3f2016-02-17 10:26:35 +010083/*
84 * SB800 port is selected by bits 2:1 of the smb_en register (0x2c)
85 * or the smb_sel register (0x2e), depending on bit 0 of register 0x2f.
86 * Hudson-2/Bolton port is always selected by bits 2:1 of register 0x2f.
87 */
Christian Fetzer2fee61d2015-11-19 20:13:48 +010088#define SB800_PIIX4_PORT_IDX 0x2c
Jean Delvare6befa3f2016-02-17 10:26:35 +010089#define SB800_PIIX4_PORT_IDX_ALT 0x2e
90#define SB800_PIIX4_PORT_IDX_SEL 0x2f
Christian Fetzer2fee61d2015-11-19 20:13:48 +010091#define SB800_PIIX4_PORT_IDX_MASK 0x06
Guenter Roeck0fe16192017-07-15 16:51:26 -070092#define SB800_PIIX4_PORT_IDX_SHIFT 1
93
94/* On kerncz, SmBus0Sel is at bit 20:19 of PMx00 DecodeEn */
95#define SB800_PIIX4_PORT_IDX_KERNCZ 0x02
96#define SB800_PIIX4_PORT_IDX_MASK_KERNCZ 0x18
97#define SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ 3
Christian Fetzer2fee61d2015-11-19 20:13:48 +010098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* insmod parameters */
100
101/* If force is set to anything different from 0, we forcibly enable the
102 PIIX4. DANGEROUS! */
Jean Delvare60507092005-09-25 16:23:07 +0200103static int force;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104module_param (force, int, 0);
105MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!");
106
107/* If force_addr is set to anything different from 0, we forcibly enable
108 the PIIX4 at the given address. VERY DANGEROUS! */
Jean Delvare60507092005-09-25 16:23:07 +0200109static int force_addr;
David Howellsc78babc2017-04-04 16:54:23 +0100110module_param_hw(force_addr, int, ioport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111MODULE_PARM_DESC(force_addr,
112 "Forcibly enable the PIIX4 at the given address. "
113 "EXTREMELY DANGEROUS!");
114
David Milburnb1c17592008-05-11 20:37:05 +0200115static int srvrworks_csb5_delay;
Jean Delvared6072f82005-09-25 16:37:04 +0200116static struct pci_driver piix4_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Bill Pemberton0b255e92012-11-27 15:59:38 -0500118static const struct dmi_system_id piix4_dmi_blacklist[] = {
Jean Delvarec2fc54f2008-05-11 20:37:05 +0200119 {
120 .ident = "Sapphire AM2RD790",
121 .matches = {
122 DMI_MATCH(DMI_BOARD_VENDOR, "SAPPHIRE Inc."),
123 DMI_MATCH(DMI_BOARD_NAME, "PC-AM2RD790"),
124 },
125 },
126 {
127 .ident = "DFI Lanparty UT 790FX",
128 .matches = {
129 DMI_MATCH(DMI_BOARD_VENDOR, "DFI Inc."),
130 DMI_MATCH(DMI_BOARD_NAME, "LP UT 790FX"),
131 },
132 },
133 { }
134};
135
136/* The IBM entry is in a separate table because we only check it
137 on Intel-based systems */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500138static const struct dmi_system_id piix4_dmi_ibm[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 {
140 .ident = "IBM",
141 .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), },
142 },
143 { },
144};
145
Jean Delvare6befa3f2016-02-17 10:26:35 +0100146/*
147 * SB800 globals
Jean Delvare6befa3f2016-02-17 10:26:35 +0100148 */
Jean Delvare6befa3f2016-02-17 10:26:35 +0100149static u8 piix4_port_sel_sb800;
Guenter Roeck0fe16192017-07-15 16:51:26 -0700150static u8 piix4_port_mask_sb800;
151static u8 piix4_port_shift_sb800;
Christian Fetzer725d2e32015-11-19 20:13:49 +0100152static const char *piix4_main_port_names_sb800[PIIX4_MAX_ADAPTERS] = {
Jean Delvare52795f62016-01-27 14:40:33 +0100153 " port 0", " port 2", " port 3", " port 4"
Christian Fetzer725d2e32015-11-19 20:13:49 +0100154};
Jean Delvare52795f62016-01-27 14:40:33 +0100155static const char *piix4_aux_port_name_sb800 = " port 1";
Christian Fetzer725d2e32015-11-19 20:13:49 +0100156
Andrew Armenia14a80862012-07-24 14:13:56 +0200157struct i2c_piix4_adapdata {
158 unsigned short smba;
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100159
160 /* SB800 */
161 bool sb800_main;
Ricardo Ribalda Delgado88fa2df2017-10-10 18:11:15 +0200162 bool notify_imc;
Jean Delvare33f5ccc2016-01-29 10:46:37 +0100163 u8 port; /* Port number, shifted */
Andrew Armenia14a80862012-07-24 14:13:56 +0200164};
165
Bill Pemberton0b255e92012-11-27 15:59:38 -0500166static int piix4_setup(struct pci_dev *PIIX4_dev,
167 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 unsigned char temp;
Andrew Armenia14a80862012-07-24 14:13:56 +0200170 unsigned short piix4_smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
David Milburnb1c17592008-05-11 20:37:05 +0200172 if ((PIIX4_dev->vendor == PCI_VENDOR_ID_SERVERWORKS) &&
173 (PIIX4_dev->device == PCI_DEVICE_ID_SERVERWORKS_CSB5))
174 srvrworks_csb5_delay = 1;
175
Jean Delvarec2fc54f2008-05-11 20:37:05 +0200176 /* On some motherboards, it was reported that accessing the SMBus
177 caused severe hardware problems */
178 if (dmi_check_system(piix4_dmi_blacklist)) {
179 dev_err(&PIIX4_dev->dev,
180 "Accessing the SMBus on this system is unsafe!\n");
181 return -EPERM;
182 }
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /* Don't access SMBus on IBM systems which get corrupted eeproms */
Jean Delvarec2fc54f2008-05-11 20:37:05 +0200185 if (dmi_check_system(piix4_dmi_ibm) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) {
Jean Delvaref9ba6c02006-04-25 13:37:25 +0200187 dev_err(&PIIX4_dev->dev, "IBM system detected; this module "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 "may corrupt your serial eeprom! Refusing to load "
189 "module!\n");
190 return -EPERM;
191 }
192
193 /* Determine the address of the SMBus areas */
194 if (force_addr) {
195 piix4_smba = force_addr & 0xfff0;
196 force = 0;
197 } else {
198 pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba);
199 piix4_smba &= 0xfff0;
200 if(piix4_smba == 0) {
Jean Delvarefa63cd52008-07-14 22:38:25 +0200201 dev_err(&PIIX4_dev->dev, "SMBus base address "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 "uninitialized - upgrade BIOS or use "
203 "force_addr=0xaddr\n");
204 return -ENODEV;
205 }
206 }
207
Jean Delvare54fb4a052008-07-14 22:38:33 +0200208 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
Jean Delvare18669ea2009-10-04 22:53:45 +0200209 return -ENODEV;
Jean Delvare54fb4a052008-07-14 22:38:33 +0200210
Jean Delvared6072f82005-09-25 16:37:04 +0200211 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
Jean Delvarefa63cd52008-07-14 22:38:25 +0200212 dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 piix4_smba);
Jean Delvarefa63cd52008-07-14 22:38:25 +0200214 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
217 pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /* If force_addr is set, we program the new address here. Just to make
220 sure, we disable the PIIX4 first. */
221 if (force_addr) {
222 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe);
223 pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba);
224 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01);
225 dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to "
226 "new address %04x!\n", piix4_smba);
227 } else if ((temp & 1) == 0) {
228 if (force) {
229 /* This should never need to be done, but has been
230 * noted that many Dell machines have the SMBus
231 * interface on the PIIX4 disabled!? NOTE: This assumes
232 * I/O space and other allocations WERE done by the
233 * Bios! Don't complain if your hardware does weird
234 * things after enabling this. :') Check for Bios
235 * updates before resorting to this.
236 */
237 pci_write_config_byte(PIIX4_dev, SMBHSTCFG,
238 temp | 1);
Joe Perches8117e412012-12-16 21:11:55 +0100239 dev_notice(&PIIX4_dev->dev,
240 "WARNING: SMBus interface has been FORCEFULLY ENABLED!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 } else {
242 dev_err(&PIIX4_dev->dev,
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100243 "SMBus Host Controller not enabled!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 release_region(piix4_smba, SMBIOSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return -ENODEV;
246 }
247 }
248
Rudolf Marek54aaa1c2006-04-25 13:06:41 +0200249 if (((temp & 0x0E) == 8) || ((temp & 0x0E) == 2))
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100250 dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 else if ((temp & 0x0E) == 0)
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100252 dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 else
254 dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration "
255 "(or code out of date)!\n");
256
257 pci_read_config_byte(PIIX4_dev, SMBREV, &temp);
Jean Delvarefa63cd52008-07-14 22:38:25 +0200258 dev_info(&PIIX4_dev->dev,
259 "SMBus Host Controller at 0x%x, revision %d\n",
260 piix4_smba, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Andrew Armenia14a80862012-07-24 14:13:56 +0200262 return piix4_smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Bill Pemberton0b255e92012-11-27 15:59:38 -0500265static int piix4_setup_sb800(struct pci_dev *PIIX4_dev,
Rudolf Mareka94dd002013-07-14 23:17:26 +0200266 const struct pci_device_id *id, u8 aux)
Shane Huang87e19602009-03-28 21:34:46 +0100267{
Andrew Armenia14a80862012-07-24 14:13:56 +0200268 unsigned short piix4_smba;
Jean Delvare6befa3f2016-02-17 10:26:35 +0100269 u8 smba_en_lo, smba_en_hi, smb_en, smb_en_status, port_sel;
Shane Huang032f7082014-01-22 14:05:46 -0800270 u8 i2ccfg, i2ccfg_offset = 0x10;
Shane Huang87e19602009-03-28 21:34:46 +0100271
Crane Cai3806e94b2009-11-07 13:10:46 +0100272 /* SB800 and later SMBus does not support forcing address */
Shane Huang87e19602009-03-28 21:34:46 +0100273 if (force || force_addr) {
Crane Cai3806e94b2009-11-07 13:10:46 +0100274 dev_err(&PIIX4_dev->dev, "SMBus does not support "
Shane Huang87e19602009-03-28 21:34:46 +0100275 "forcing address!\n");
276 return -EINVAL;
277 }
278
279 /* Determine the address of the SMBus areas */
Shane Huang032f7082014-01-22 14:05:46 -0800280 if ((PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
281 PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
282 PIIX4_dev->revision >= 0x41) ||
283 (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
Vincent Wanbcb29992015-06-11 20:11:46 +0800284 PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS &&
Pu Wen24beb832019-04-30 00:08:43 +0800285 PIIX4_dev->revision >= 0x49) ||
286 (PIIX4_dev->vendor == PCI_VENDOR_ID_HYGON &&
287 PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS))
Shane Huang032f7082014-01-22 14:05:46 -0800288 smb_en = 0x00;
289 else
290 smb_en = (aux) ? 0x28 : 0x2c;
Rudolf Mareka94dd002013-07-14 23:17:26 +0200291
Guenter Roeck04b6fca2018-02-26 12:46:53 -0800292 if (!request_muxed_region(SB800_PIIX4_SMB_IDX, 2, "sb800_piix4_smb")) {
293 dev_err(&PIIX4_dev->dev,
294 "SMB base address index region 0x%x already in use.\n",
295 SB800_PIIX4_SMB_IDX);
296 return -EBUSY;
297 }
298
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100299 outb_p(smb_en, SB800_PIIX4_SMB_IDX);
300 smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
301 outb_p(smb_en + 1, SB800_PIIX4_SMB_IDX);
302 smba_en_hi = inb_p(SB800_PIIX4_SMB_IDX + 1);
Guenter Roeck04b6fca2018-02-26 12:46:53 -0800303
304 release_region(SB800_PIIX4_SMB_IDX, 2);
Shane Huang87e19602009-03-28 21:34:46 +0100305
Shane Huang032f7082014-01-22 14:05:46 -0800306 if (!smb_en) {
307 smb_en_status = smba_en_lo & 0x10;
308 piix4_smba = smba_en_hi << 8;
309 if (aux)
310 piix4_smba |= 0x20;
311 } else {
312 smb_en_status = smba_en_lo & 0x01;
313 piix4_smba = ((smba_en_hi << 8) | smba_en_lo) & 0xffe0;
314 }
315
316 if (!smb_en_status) {
Shane Huang87e19602009-03-28 21:34:46 +0100317 dev_err(&PIIX4_dev->dev,
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100318 "SMBus Host Controller not enabled!\n");
Shane Huang87e19602009-03-28 21:34:46 +0100319 return -ENODEV;
320 }
321
Shane Huang87e19602009-03-28 21:34:46 +0100322 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
Jean Delvare18669ea2009-10-04 22:53:45 +0200323 return -ENODEV;
Shane Huang87e19602009-03-28 21:34:46 +0100324
325 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
326 dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
327 piix4_smba);
328 return -EBUSY;
329 }
330
Rudolf Mareka94dd002013-07-14 23:17:26 +0200331 /* Aux SMBus does not support IRQ information */
332 if (aux) {
333 dev_info(&PIIX4_dev->dev,
Shane Huang85fd0fe2014-01-22 14:06:52 -0800334 "Auxiliary SMBus Host Controller at 0x%x\n",
335 piix4_smba);
Rudolf Mareka94dd002013-07-14 23:17:26 +0200336 return piix4_smba;
337 }
338
Shane Huang87e19602009-03-28 21:34:46 +0100339 /* Request the SMBus I2C bus config region */
340 if (!request_region(piix4_smba + i2ccfg_offset, 1, "i2ccfg")) {
341 dev_err(&PIIX4_dev->dev, "SMBus I2C bus config region "
342 "0x%x already in use!\n", piix4_smba + i2ccfg_offset);
343 release_region(piix4_smba, SMBIOSIZE);
Shane Huang87e19602009-03-28 21:34:46 +0100344 return -EBUSY;
345 }
346 i2ccfg = inb_p(piix4_smba + i2ccfg_offset);
347 release_region(piix4_smba + i2ccfg_offset, 1);
348
349 if (i2ccfg & 1)
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100350 dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
Shane Huang87e19602009-03-28 21:34:46 +0100351 else
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100352 dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
Shane Huang87e19602009-03-28 21:34:46 +0100353
354 dev_info(&PIIX4_dev->dev,
355 "SMBus Host Controller at 0x%x, revision %d\n",
356 piix4_smba, i2ccfg >> 4);
357
Jean Delvare6befa3f2016-02-17 10:26:35 +0100358 /* Find which register is used for port selection */
Pu Wen24beb832019-04-30 00:08:43 +0800359 if (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD ||
360 PIIX4_dev->vendor == PCI_VENDOR_ID_HYGON) {
Guenter Roeck0fe16192017-07-15 16:51:26 -0700361 switch (PIIX4_dev->device) {
362 case PCI_DEVICE_ID_AMD_KERNCZ_SMBUS:
363 piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_KERNCZ;
364 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK_KERNCZ;
365 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ;
366 break;
367 case PCI_DEVICE_ID_AMD_HUDSON2_SMBUS:
368 default:
369 piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_ALT;
370 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
371 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
372 break;
373 }
Jean Delvare6befa3f2016-02-17 10:26:35 +0100374 } else {
Guenter Roeck04b6fca2018-02-26 12:46:53 -0800375 if (!request_muxed_region(SB800_PIIX4_SMB_IDX, 2,
376 "sb800_piix4_smb")) {
377 release_region(piix4_smba, SMBIOSIZE);
378 return -EBUSY;
379 }
380
Jean Delvare6befa3f2016-02-17 10:26:35 +0100381 outb_p(SB800_PIIX4_PORT_IDX_SEL, SB800_PIIX4_SMB_IDX);
382 port_sel = inb_p(SB800_PIIX4_SMB_IDX + 1);
383 piix4_port_sel_sb800 = (port_sel & 0x01) ?
384 SB800_PIIX4_PORT_IDX_ALT :
385 SB800_PIIX4_PORT_IDX;
Guenter Roeck0fe16192017-07-15 16:51:26 -0700386 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
387 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
Guenter Roeck04b6fca2018-02-26 12:46:53 -0800388 release_region(SB800_PIIX4_SMB_IDX, 2);
Jean Delvare6befa3f2016-02-17 10:26:35 +0100389 }
390
391 dev_info(&PIIX4_dev->dev,
392 "Using register 0x%02x for SMBus port selection\n",
393 (unsigned int)piix4_port_sel_sb800);
394
Andrew Armenia14a80862012-07-24 14:13:56 +0200395 return piix4_smba;
Shane Huang87e19602009-03-28 21:34:46 +0100396}
397
Bill Pemberton0b255e92012-11-27 15:59:38 -0500398static int piix4_setup_aux(struct pci_dev *PIIX4_dev,
399 const struct pci_device_id *id,
400 unsigned short base_reg_addr)
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200401{
402 /* Set up auxiliary SMBus controllers found on some
403 * AMD chipsets e.g. SP5100 (SB700 derivative) */
404
405 unsigned short piix4_smba;
406
407 /* Read address of auxiliary SMBus controller */
408 pci_read_config_word(PIIX4_dev, base_reg_addr, &piix4_smba);
409 if ((piix4_smba & 1) == 0) {
410 dev_dbg(&PIIX4_dev->dev,
411 "Auxiliary SMBus controller not enabled\n");
412 return -ENODEV;
413 }
414
415 piix4_smba &= 0xfff0;
416 if (piix4_smba == 0) {
417 dev_dbg(&PIIX4_dev->dev,
418 "Auxiliary SMBus base address uninitialized\n");
419 return -ENODEV;
420 }
421
422 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
423 return -ENODEV;
424
425 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
426 dev_err(&PIIX4_dev->dev, "Auxiliary SMBus region 0x%x "
427 "already in use!\n", piix4_smba);
428 return -EBUSY;
429 }
430
431 dev_info(&PIIX4_dev->dev,
432 "Auxiliary SMBus Host Controller at 0x%x\n",
433 piix4_smba);
434
435 return piix4_smba;
436}
437
Andrew Armeniae154bf62012-07-24 14:13:56 +0200438static int piix4_transaction(struct i2c_adapter *piix4_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Andrew Armeniae154bf62012-07-24 14:13:56 +0200440 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(piix4_adapter);
441 unsigned short piix4_smba = adapdata->smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int temp;
443 int result = 0;
444 int timeout = 0;
445
Andrew Armeniae154bf62012-07-24 14:13:56 +0200446 dev_dbg(&piix4_adapter->dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
448 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
449 inb_p(SMBHSTDAT1));
450
451 /* Make sure the SMBus host is ready to start transmitting */
452 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200453 dev_dbg(&piix4_adapter->dev, "SMBus busy (%02x). "
Jean Delvare541e6a02005-06-23 22:18:08 +0200454 "Resetting...\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 outb_p(temp, SMBHSTSTS);
456 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200457 dev_err(&piix4_adapter->dev, "Failed! (%02x)\n", temp);
David Brownell97140342008-07-14 22:38:25 +0200458 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 } else {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200460 dev_dbg(&piix4_adapter->dev, "Successful!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 }
463
464 /* start the transaction by setting bit 6 */
465 outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
466
467 /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
David Milburnb1c17592008-05-11 20:37:05 +0200468 if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */
Guenter Roeck0e89b2f2018-02-26 12:46:52 -0800469 usleep_range(2000, 2100);
David Milburnb1c17592008-05-11 20:37:05 +0200470 else
Guenter Roeck0e89b2f2018-02-26 12:46:52 -0800471 usleep_range(250, 500);
David Milburnb1c17592008-05-11 20:37:05 +0200472
Roel Kluinb6a31952010-01-16 20:43:12 +0100473 while ((++timeout < MAX_TIMEOUT) &&
David Milburnb1c17592008-05-11 20:37:05 +0200474 ((temp = inb_p(SMBHSTSTS)) & 0x01))
Guenter Roeck0e89b2f2018-02-26 12:46:52 -0800475 usleep_range(250, 500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /* If the SMBus is still busy, we give up */
Roel Kluinb6a31952010-01-16 20:43:12 +0100478 if (timeout == MAX_TIMEOUT) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200479 dev_err(&piix4_adapter->dev, "SMBus Timeout!\n");
David Brownell97140342008-07-14 22:38:25 +0200480 result = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482
483 if (temp & 0x10) {
David Brownell97140342008-07-14 22:38:25 +0200484 result = -EIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200485 dev_err(&piix4_adapter->dev, "Error: Failed bus transaction\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
488 if (temp & 0x08) {
David Brownell97140342008-07-14 22:38:25 +0200489 result = -EIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200490 dev_dbg(&piix4_adapter->dev, "Bus collision! SMBus may be "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 "locked until next hard reset. (sorry!)\n");
492 /* Clock stops and slave is stuck in mid-transmission */
493 }
494
495 if (temp & 0x04) {
David Brownell97140342008-07-14 22:38:25 +0200496 result = -ENXIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200497 dev_dbg(&piix4_adapter->dev, "Error: no response!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
500 if (inb_p(SMBHSTSTS) != 0x00)
501 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
502
503 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200504 dev_err(&piix4_adapter->dev, "Failed reset at end of "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 "transaction (%02x)\n", temp);
506 }
Andrew Armeniae154bf62012-07-24 14:13:56 +0200507 dev_dbg(&piix4_adapter->dev, "Transaction (post): CNT=%02x, CMD=%02x, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
509 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
510 inb_p(SMBHSTDAT1));
511 return result;
512}
513
David Brownell97140342008-07-14 22:38:25 +0200514/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
516 unsigned short flags, char read_write,
517 u8 command, int size, union i2c_smbus_data * data)
518{
Andrew Armenia14a80862012-07-24 14:13:56 +0200519 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
520 unsigned short piix4_smba = adapdata->smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int i, len;
David Brownell97140342008-07-14 22:38:25 +0200522 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 case I2C_SMBUS_QUICK:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200526 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 SMBHSTADD);
528 size = PIIX4_QUICK;
529 break;
530 case I2C_SMBUS_BYTE:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200531 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 SMBHSTADD);
533 if (read_write == I2C_SMBUS_WRITE)
534 outb_p(command, SMBHSTCMD);
535 size = PIIX4_BYTE;
536 break;
537 case I2C_SMBUS_BYTE_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->byte, SMBHSTDAT0);
543 size = PIIX4_BYTE_DATA;
544 break;
545 case I2C_SMBUS_WORD_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 outb_p(data->word & 0xff, SMBHSTDAT0);
551 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
552 }
553 size = PIIX4_WORD_DATA;
554 break;
555 case I2C_SMBUS_BLOCK_DATA:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200556 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 SMBHSTADD);
558 outb_p(command, SMBHSTCMD);
559 if (read_write == I2C_SMBUS_WRITE) {
560 len = data->block[0];
Jean Delvarefa63cd52008-07-14 22:38:25 +0200561 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
562 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 outb_p(len, SMBHSTDAT0);
Wolfram Sangd7a4c762015-11-30 14:43:09 +0100564 inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 for (i = 1; i <= len; i++)
566 outb_p(data->block[i], SMBBLKDAT);
567 }
568 size = PIIX4_BLOCK_DATA;
569 break;
Jean Delvareac7fc4f2008-07-14 22:38:25 +0200570 default:
571 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
572 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
575 outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
576
Andrew Armeniae154bf62012-07-24 14:13:56 +0200577 status = piix4_transaction(adap);
David Brownell97140342008-07-14 22:38:25 +0200578 if (status)
579 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
582 return 0;
583
584
585 switch (size) {
Jean Delvare3578a072008-04-29 23:11:37 +0200586 case PIIX4_BYTE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 case PIIX4_BYTE_DATA:
588 data->byte = inb_p(SMBHSTDAT0);
589 break;
590 case PIIX4_WORD_DATA:
591 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
592 break;
593 case PIIX4_BLOCK_DATA:
594 data->block[0] = inb_p(SMBHSTDAT0);
Jean Delvarefa63cd52008-07-14 22:38:25 +0200595 if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX)
596 return -EPROTO;
Wolfram Sangd7a4c762015-11-30 14:43:09 +0100597 inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 for (i = 1; i <= data->block[0]; i++)
599 data->block[i] = inb_p(SMBBLKDAT);
600 break;
601 }
602 return 0;
603}
604
Ricardo Ribalda Delgado88fa2df2017-10-10 18:11:15 +0200605static uint8_t piix4_imc_read(uint8_t idx)
606{
607 outb_p(idx, KERNCZ_IMC_IDX);
608 return inb_p(KERNCZ_IMC_DATA);
609}
610
611static void piix4_imc_write(uint8_t idx, uint8_t value)
612{
613 outb_p(idx, KERNCZ_IMC_IDX);
614 outb_p(value, KERNCZ_IMC_DATA);
615}
616
617static int piix4_imc_sleep(void)
618{
619 int timeout = MAX_TIMEOUT;
620
621 if (!request_muxed_region(KERNCZ_IMC_IDX, 2, "smbus_kerncz_imc"))
622 return -EBUSY;
623
624 /* clear response register */
625 piix4_imc_write(0x82, 0x00);
626 /* request ownership flag */
627 piix4_imc_write(0x83, 0xB4);
628 /* kick off IMC Mailbox command 96 */
629 piix4_imc_write(0x80, 0x96);
630
631 while (timeout--) {
632 if (piix4_imc_read(0x82) == 0xfa) {
633 release_region(KERNCZ_IMC_IDX, 2);
634 return 0;
635 }
636 usleep_range(1000, 2000);
637 }
638
639 release_region(KERNCZ_IMC_IDX, 2);
640 return -ETIMEDOUT;
641}
642
643static void piix4_imc_wakeup(void)
644{
645 int timeout = MAX_TIMEOUT;
646
647 if (!request_muxed_region(KERNCZ_IMC_IDX, 2, "smbus_kerncz_imc"))
648 return;
649
650 /* clear response register */
651 piix4_imc_write(0x82, 0x00);
652 /* release ownership flag */
653 piix4_imc_write(0x83, 0xB5);
654 /* kick off IMC Mailbox command 96 */
655 piix4_imc_write(0x80, 0x96);
656
657 while (timeout--) {
658 if (piix4_imc_read(0x82) == 0xfa)
659 break;
660 usleep_range(1000, 2000);
661 }
662
663 release_region(KERNCZ_IMC_IDX, 2);
664}
665
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100666/*
667 * Handles access to multiple SMBus ports on the SB800.
668 * The port is selected by bits 2:1 of the smb_en register (0x2c).
669 * Returns negative errno on error.
670 *
671 * Note: The selected port must be returned to the initial selection to avoid
672 * problems on certain systems.
673 */
674static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr,
675 unsigned short flags, char read_write,
676 u8 command, int size, union i2c_smbus_data *data)
677{
678 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
Ricardo Ribalda701dc202017-01-11 10:11:44 +0100679 unsigned short piix4_smba = adapdata->smba;
680 int retries = MAX_TIMEOUT;
681 int smbslvcnt;
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100682 u8 smba_en_lo;
683 u8 port;
684 int retval;
685
Guenter Roeck04b6fca2018-02-26 12:46:53 -0800686 if (!request_muxed_region(SB800_PIIX4_SMB_IDX, 2, "sb800_piix4_smb"))
687 return -EBUSY;
Ricardo Ribaldabbb27fc2017-02-02 20:15:16 +0100688
Ricardo Ribalda701dc202017-01-11 10:11:44 +0100689 /* Request the SMBUS semaphore, avoid conflicts with the IMC */
690 smbslvcnt = inb_p(SMBSLVCNT);
691 do {
692 outb_p(smbslvcnt | 0x10, SMBSLVCNT);
693
694 /* Check the semaphore status */
695 smbslvcnt = inb_p(SMBSLVCNT);
696 if (smbslvcnt & 0x10)
697 break;
698
699 usleep_range(1000, 2000);
700 } while (--retries);
701 /* SMBus is still owned by the IMC, we give up */
Ricardo Ribaldabbb27fc2017-02-02 20:15:16 +0100702 if (!retries) {
Guenter Roeck04b6fca2018-02-26 12:46:53 -0800703 retval = -EBUSY;
704 goto release;
Ricardo Ribaldabbb27fc2017-02-02 20:15:16 +0100705 }
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100706
Ricardo Ribalda Delgado88fa2df2017-10-10 18:11:15 +0200707 /*
708 * Notify the IMC (Integrated Micro Controller) if required.
709 * Among other responsibilities, the IMC is in charge of monitoring
710 * the System fans and temperature sensors, and act accordingly.
711 * All this is done through SMBus and can/will collide
712 * with our transactions if they are long (BLOCK_DATA).
713 * Therefore we need to request the ownership flag during those
714 * transactions.
715 */
716 if ((size == I2C_SMBUS_BLOCK_DATA) && adapdata->notify_imc) {
717 int ret;
718
719 ret = piix4_imc_sleep();
720 switch (ret) {
721 case -EBUSY:
722 dev_warn(&adap->dev,
723 "IMC base address index region 0x%x already in use.\n",
724 KERNCZ_IMC_IDX);
725 break;
726 case -ETIMEDOUT:
727 dev_warn(&adap->dev,
728 "Failed to communicate with the IMC.\n");
729 break;
730 default:
731 break;
732 }
733
734 /* If IMC communication fails do not retry */
735 if (ret) {
736 dev_warn(&adap->dev,
737 "Continuing without IMC notification.\n");
738 adapdata->notify_imc = false;
739 }
740 }
741
Jean Delvare6befa3f2016-02-17 10:26:35 +0100742 outb_p(piix4_port_sel_sb800, SB800_PIIX4_SMB_IDX);
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100743 smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
744
745 port = adapdata->port;
Guenter Roeck0fe16192017-07-15 16:51:26 -0700746 if ((smba_en_lo & piix4_port_mask_sb800) != port)
747 outb_p((smba_en_lo & ~piix4_port_mask_sb800) | port,
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100748 SB800_PIIX4_SMB_IDX + 1);
749
750 retval = piix4_access(adap, addr, flags, read_write,
751 command, size, data);
752
753 outb_p(smba_en_lo, SB800_PIIX4_SMB_IDX + 1);
754
Ricardo Ribalda701dc202017-01-11 10:11:44 +0100755 /* Release the semaphore */
756 outb_p(smbslvcnt | 0x20, SMBSLVCNT);
757
Ricardo Ribalda Delgado88fa2df2017-10-10 18:11:15 +0200758 if ((size == I2C_SMBUS_BLOCK_DATA) && adapdata->notify_imc)
759 piix4_imc_wakeup();
760
Guenter Roeck04b6fca2018-02-26 12:46:53 -0800761release:
762 release_region(SB800_PIIX4_SMB_IDX, 2);
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100763 return retval;
764}
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766static u32 piix4_func(struct i2c_adapter *adapter)
767{
768 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
769 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
770 I2C_FUNC_SMBUS_BLOCK_DATA;
771}
772
Jean Delvare8f9082c2006-09-03 22:39:46 +0200773static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 .smbus_xfer = piix4_access,
775 .functionality = piix4_func,
776};
777
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100778static const struct i2c_algorithm piix4_smbus_algorithm_sb800 = {
779 .smbus_xfer = piix4_access_sb800,
780 .functionality = piix4_func,
781};
782
Jingoo Han392debf2013-12-03 08:11:20 +0900783static const struct pci_device_id piix4_ids[] = {
Jean Delvare9b7389c2008-01-27 18:14:51 +0100784 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
785 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3) },
786 { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3) },
787 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_SMBUS) },
788 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_SMBUS) },
789 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS) },
790 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS) },
Crane Cai3806e94b2009-11-07 13:10:46 +0100791 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) },
Vincent Wanbcb29992015-06-11 20:11:46 +0800792 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) },
Pu Wen24beb832019-04-30 00:08:43 +0800793 { PCI_DEVICE(PCI_VENDOR_ID_HYGON, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) },
Jean Delvare9b7389c2008-01-27 18:14:51 +0100794 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
795 PCI_DEVICE_ID_SERVERWORKS_OSB4) },
796 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
797 PCI_DEVICE_ID_SERVERWORKS_CSB5) },
798 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
799 PCI_DEVICE_ID_SERVERWORKS_CSB6) },
800 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
801 PCI_DEVICE_ID_SERVERWORKS_HT1000SB) },
Flavio Leitner506a8b62009-03-28 21:34:46 +0100802 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
803 PCI_DEVICE_ID_SERVERWORKS_HT1100LD) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 { 0, }
805};
806
807MODULE_DEVICE_TABLE (pci, piix4_ids);
808
Christian Fetzerca2061e2015-11-19 20:13:47 +0100809static struct i2c_adapter *piix4_main_adapters[PIIX4_MAX_ADAPTERS];
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200810static struct i2c_adapter *piix4_aux_adapter;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200811
Bill Pemberton0b255e92012-11-27 15:59:38 -0500812static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
Ricardo Ribalda Delgado88fa2df2017-10-10 18:11:15 +0200813 bool sb800_main, u8 port, bool notify_imc,
Christian Fetzer725d2e32015-11-19 20:13:49 +0100814 const char *name, struct i2c_adapter **padap)
Andrew Armeniae154bf62012-07-24 14:13:56 +0200815{
816 struct i2c_adapter *adap;
817 struct i2c_piix4_adapdata *adapdata;
818 int retval;
819
820 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
821 if (adap == NULL) {
822 release_region(smba, SMBIOSIZE);
823 return -ENOMEM;
824 }
825
826 adap->owner = THIS_MODULE;
827 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Jean Delvare83c60152016-01-25 12:17:07 +0100828 adap->algo = sb800_main ? &piix4_smbus_algorithm_sb800
829 : &smbus_algorithm;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200830
831 adapdata = kzalloc(sizeof(*adapdata), GFP_KERNEL);
832 if (adapdata == NULL) {
833 kfree(adap);
834 release_region(smba, SMBIOSIZE);
835 return -ENOMEM;
836 }
837
838 adapdata->smba = smba;
Jean Delvare83c60152016-01-25 12:17:07 +0100839 adapdata->sb800_main = sb800_main;
Guenter Roeck0fe16192017-07-15 16:51:26 -0700840 adapdata->port = port << piix4_port_shift_sb800;
Ricardo Ribalda Delgado88fa2df2017-10-10 18:11:15 +0200841 adapdata->notify_imc = notify_imc;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200842
843 /* set up the sysfs linkage to our parent device */
844 adap->dev.parent = &dev->dev;
845
846 snprintf(adap->name, sizeof(adap->name),
Jean Delvare52795f62016-01-27 14:40:33 +0100847 "SMBus PIIX4 adapter%s at %04x", name, smba);
Andrew Armeniae154bf62012-07-24 14:13:56 +0200848
849 i2c_set_adapdata(adap, adapdata);
850
851 retval = i2c_add_adapter(adap);
852 if (retval) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200853 kfree(adapdata);
854 kfree(adap);
855 release_region(smba, SMBIOSIZE);
856 return retval;
857 }
858
859 *padap = adap;
860 return 0;
861}
862
Ricardo Ribalda Delgado88fa2df2017-10-10 18:11:15 +0200863static int piix4_add_adapters_sb800(struct pci_dev *dev, unsigned short smba,
864 bool notify_imc)
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100865{
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100866 struct i2c_piix4_adapdata *adapdata;
867 int port;
868 int retval;
869
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100870 for (port = 0; port < PIIX4_MAX_ADAPTERS; port++) {
Ricardo Ribalda Delgado88fa2df2017-10-10 18:11:15 +0200871 retval = piix4_add_adapter(dev, smba, true, port, notify_imc,
Christian Fetzer725d2e32015-11-19 20:13:49 +0100872 piix4_main_port_names_sb800[port],
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100873 &piix4_main_adapters[port]);
874 if (retval < 0)
875 goto error;
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100876 }
877
878 return retval;
879
880error:
881 dev_err(&dev->dev,
882 "Error setting up SB800 adapters. Unregistering!\n");
883 while (--port >= 0) {
884 adapdata = i2c_get_adapdata(piix4_main_adapters[port]);
885 if (adapdata->smba) {
886 i2c_del_adapter(piix4_main_adapters[port]);
887 kfree(adapdata);
888 kfree(piix4_main_adapters[port]);
889 piix4_main_adapters[port] = NULL;
890 }
891 }
892
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100893 return retval;
894}
895
Bill Pemberton0b255e92012-11-27 15:59:38 -0500896static int piix4_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 int retval;
Jean Delvare52795f62016-01-27 14:40:33 +0100899 bool is_sb800 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Crane Cai76b3e282009-09-18 22:45:50 +0200901 if ((dev->vendor == PCI_VENDOR_ID_ATI &&
902 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS &&
903 dev->revision >= 0x40) ||
Pu Wen24beb832019-04-30 00:08:43 +0800904 dev->vendor == PCI_VENDOR_ID_AMD ||
905 dev->vendor == PCI_VENDOR_ID_HYGON) {
Ricardo Ribalda Delgado88fa2df2017-10-10 18:11:15 +0200906 bool notify_imc = false;
Jean Delvare52795f62016-01-27 14:40:33 +0100907 is_sb800 = true;
908
Pu Wen24beb832019-04-30 00:08:43 +0800909 if ((dev->vendor == PCI_VENDOR_ID_AMD ||
910 dev->vendor == PCI_VENDOR_ID_HYGON) &&
Ricardo Ribalda Delgado88fa2df2017-10-10 18:11:15 +0200911 dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) {
912 u8 imc;
913
914 /*
915 * Detect if IMC is active or not, this method is
916 * described on coreboot's AMD IMC notes
917 */
918 pci_bus_read_config_byte(dev->bus, PCI_DEVFN(0x14, 3),
919 0x40, &imc);
920 if (imc & 0x80)
921 notify_imc = true;
922 }
923
Shane Huang87e19602009-03-28 21:34:46 +0100924 /* base address location etc changed in SB800 */
Rudolf Mareka94dd002013-07-14 23:17:26 +0200925 retval = piix4_setup_sb800(dev, id, 0);
Guenter Roeck04b6fca2018-02-26 12:46:53 -0800926 if (retval < 0)
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100927 return retval;
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100928
929 /*
930 * Try to register multiplexed main SMBus adapter,
931 * give up if we can't
932 */
Ricardo Ribalda Delgado88fa2df2017-10-10 18:11:15 +0200933 retval = piix4_add_adapters_sb800(dev, retval, notify_imc);
Guenter Roeck04b6fca2018-02-26 12:46:53 -0800934 if (retval < 0)
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100935 return retval;
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100936 } else {
Shane Huang87e19602009-03-28 21:34:46 +0100937 retval = piix4_setup(dev, id);
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100938 if (retval < 0)
939 return retval;
Shane Huang87e19602009-03-28 21:34:46 +0100940
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100941 /* Try to register main SMBus adapter, give up if we can't */
Ricardo Ribalda Delgado88fa2df2017-10-10 18:11:15 +0200942 retval = piix4_add_adapter(dev, retval, false, 0, false, "",
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100943 &piix4_main_adapters[0]);
944 if (retval < 0)
945 return retval;
946 }
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200947
948 /* Check for auxiliary SMBus on some AMD chipsets */
Rudolf Mareka94dd002013-07-14 23:17:26 +0200949 retval = -ENODEV;
950
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200951 if (dev->vendor == PCI_VENDOR_ID_ATI &&
Rudolf Mareka94dd002013-07-14 23:17:26 +0200952 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS) {
953 if (dev->revision < 0x40) {
954 retval = piix4_setup_aux(dev, id, 0x58);
955 } else {
956 /* SB800 added aux bus too */
957 retval = piix4_setup_sb800(dev, id, 1);
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200958 }
959 }
960
Rudolf Mareka94dd002013-07-14 23:17:26 +0200961 if (dev->vendor == PCI_VENDOR_ID_AMD &&
962 dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) {
963 retval = piix4_setup_sb800(dev, id, 1);
964 }
965
966 if (retval > 0) {
967 /* Try to add the aux adapter if it exists,
968 * piix4_add_adapter will clean up if this fails */
Ricardo Ribalda Delgado88fa2df2017-10-10 18:11:15 +0200969 piix4_add_adapter(dev, retval, false, 0, false,
Jean Delvare52795f62016-01-27 14:40:33 +0100970 is_sb800 ? piix4_aux_port_name_sb800 : "",
Christian Fetzer725d2e32015-11-19 20:13:49 +0100971 &piix4_aux_adapter);
Rudolf Mareka94dd002013-07-14 23:17:26 +0200972 }
973
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Bill Pemberton0b255e92012-11-27 15:59:38 -0500977static void piix4_adap_remove(struct i2c_adapter *adap)
Andrew Armenia14a80862012-07-24 14:13:56 +0200978{
979 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
980
981 if (adapdata->smba) {
982 i2c_del_adapter(adap);
Guenter Roeck04b6fca2018-02-26 12:46:53 -0800983 if (adapdata->port == (0 << piix4_port_shift_sb800))
Christian Fetzer2fee61d2015-11-19 20:13:48 +0100984 release_region(adapdata->smba, SMBIOSIZE);
Andrew Armeniae154bf62012-07-24 14:13:56 +0200985 kfree(adapdata);
986 kfree(adap);
Andrew Armenia14a80862012-07-24 14:13:56 +0200987 }
988}
989
Bill Pemberton0b255e92012-11-27 15:59:38 -0500990static void piix4_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Christian Fetzerca2061e2015-11-19 20:13:47 +0100992 int port = PIIX4_MAX_ADAPTERS;
993
994 while (--port >= 0) {
995 if (piix4_main_adapters[port]) {
996 piix4_adap_remove(piix4_main_adapters[port]);
997 piix4_main_adapters[port] = NULL;
998 }
Andrew Armeniae154bf62012-07-24 14:13:56 +0200999 }
Andrew Armenia2a2f7402012-07-24 14:13:57 +02001000
1001 if (piix4_aux_adapter) {
1002 piix4_adap_remove(piix4_aux_adapter);
1003 piix4_aux_adapter = NULL;
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
1007static struct pci_driver piix4_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 .name = "piix4_smbus",
1009 .id_table = piix4_ids,
1010 .probe = piix4_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001011 .remove = piix4_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012};
1013
Axel Lin56f21782012-07-24 14:13:56 +02001014module_pci_driver(piix4_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
1017 "Philip Edelbrock <phil@netroedge.com>");
1018MODULE_DESCRIPTION("PIIX4 SMBus driver");
1019MODULE_LICENSE("GPL");