blob: 9c32eb124b7285f8427bd11b5e6e3f94fa38aa39 [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026*/
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/pci.h>
31#include <linux/kernel.h>
32#include <linux/delay.h>
33#include <linux/stddef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/ioport.h>
35#include <linux/i2c.h>
Daniel J Bluemanc415b302012-10-05 22:23:55 +020036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/dmi.h>
Jean Delvare54fb4a052008-07-14 22:38:33 +020038#include <linux/acpi.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020039#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* PIIX4 SMBus address offsets */
43#define SMBHSTSTS (0 + piix4_smba)
44#define SMBHSLVSTS (1 + piix4_smba)
45#define SMBHSTCNT (2 + piix4_smba)
46#define SMBHSTCMD (3 + piix4_smba)
47#define SMBHSTADD (4 + piix4_smba)
48#define SMBHSTDAT0 (5 + piix4_smba)
49#define SMBHSTDAT1 (6 + piix4_smba)
50#define SMBBLKDAT (7 + piix4_smba)
51#define SMBSLVCNT (8 + piix4_smba)
52#define SMBSHDWCMD (9 + piix4_smba)
53#define SMBSLVEVT (0xA + piix4_smba)
54#define SMBSLVDAT (0xC + piix4_smba)
55
56/* count for request_region */
57#define SMBIOSIZE 8
58
59/* PCI Address Constants */
60#define SMBBA 0x090
61#define SMBHSTCFG 0x0D2
62#define SMBSLVC 0x0D3
63#define SMBSHDW1 0x0D4
64#define SMBSHDW2 0x0D5
65#define SMBREV 0x0D6
66
67/* Other settings */
68#define MAX_TIMEOUT 500
69#define ENABLE_INT9 0
70
71/* PIIX4 constants */
72#define PIIX4_QUICK 0x00
73#define PIIX4_BYTE 0x04
74#define PIIX4_BYTE_DATA 0x08
75#define PIIX4_WORD_DATA 0x0C
76#define PIIX4_BLOCK_DATA 0x14
77
Christian Fetzerca2061e2015-11-19 20:13:47 +010078/* Multi-port constants */
79#define PIIX4_MAX_ADAPTERS 4
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* insmod parameters */
82
83/* If force is set to anything different from 0, we forcibly enable the
84 PIIX4. DANGEROUS! */
Jean Delvare60507092005-09-25 16:23:07 +020085static int force;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086module_param (force, int, 0);
87MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!");
88
89/* If force_addr is set to anything different from 0, we forcibly enable
90 the PIIX4 at the given address. VERY DANGEROUS! */
Jean Delvare60507092005-09-25 16:23:07 +020091static int force_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092module_param (force_addr, int, 0);
93MODULE_PARM_DESC(force_addr,
94 "Forcibly enable the PIIX4 at the given address. "
95 "EXTREMELY DANGEROUS!");
96
David Milburnb1c17592008-05-11 20:37:05 +020097static int srvrworks_csb5_delay;
Jean Delvared6072f82005-09-25 16:37:04 +020098static struct pci_driver piix4_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Bill Pemberton0b255e92012-11-27 15:59:38 -0500100static const struct dmi_system_id piix4_dmi_blacklist[] = {
Jean Delvarec2fc54f2008-05-11 20:37:05 +0200101 {
102 .ident = "Sapphire AM2RD790",
103 .matches = {
104 DMI_MATCH(DMI_BOARD_VENDOR, "SAPPHIRE Inc."),
105 DMI_MATCH(DMI_BOARD_NAME, "PC-AM2RD790"),
106 },
107 },
108 {
109 .ident = "DFI Lanparty UT 790FX",
110 .matches = {
111 DMI_MATCH(DMI_BOARD_VENDOR, "DFI Inc."),
112 DMI_MATCH(DMI_BOARD_NAME, "LP UT 790FX"),
113 },
114 },
115 { }
116};
117
118/* The IBM entry is in a separate table because we only check it
119 on Intel-based systems */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500120static const struct dmi_system_id piix4_dmi_ibm[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 {
122 .ident = "IBM",
123 .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), },
124 },
125 { },
126};
127
Andrew Armenia14a80862012-07-24 14:13:56 +0200128struct i2c_piix4_adapdata {
129 unsigned short smba;
130};
131
Bill Pemberton0b255e92012-11-27 15:59:38 -0500132static int piix4_setup(struct pci_dev *PIIX4_dev,
133 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 unsigned char temp;
Andrew Armenia14a80862012-07-24 14:13:56 +0200136 unsigned short piix4_smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
David Milburnb1c17592008-05-11 20:37:05 +0200138 if ((PIIX4_dev->vendor == PCI_VENDOR_ID_SERVERWORKS) &&
139 (PIIX4_dev->device == PCI_DEVICE_ID_SERVERWORKS_CSB5))
140 srvrworks_csb5_delay = 1;
141
Jean Delvarec2fc54f2008-05-11 20:37:05 +0200142 /* On some motherboards, it was reported that accessing the SMBus
143 caused severe hardware problems */
144 if (dmi_check_system(piix4_dmi_blacklist)) {
145 dev_err(&PIIX4_dev->dev,
146 "Accessing the SMBus on this system is unsafe!\n");
147 return -EPERM;
148 }
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /* Don't access SMBus on IBM systems which get corrupted eeproms */
Jean Delvarec2fc54f2008-05-11 20:37:05 +0200151 if (dmi_check_system(piix4_dmi_ibm) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) {
Jean Delvaref9ba6c02006-04-25 13:37:25 +0200153 dev_err(&PIIX4_dev->dev, "IBM system detected; this module "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 "may corrupt your serial eeprom! Refusing to load "
155 "module!\n");
156 return -EPERM;
157 }
158
159 /* Determine the address of the SMBus areas */
160 if (force_addr) {
161 piix4_smba = force_addr & 0xfff0;
162 force = 0;
163 } else {
164 pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba);
165 piix4_smba &= 0xfff0;
166 if(piix4_smba == 0) {
Jean Delvarefa63cd52008-07-14 22:38:25 +0200167 dev_err(&PIIX4_dev->dev, "SMBus base address "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 "uninitialized - upgrade BIOS or use "
169 "force_addr=0xaddr\n");
170 return -ENODEV;
171 }
172 }
173
Jean Delvare54fb4a052008-07-14 22:38:33 +0200174 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
Jean Delvare18669ea2009-10-04 22:53:45 +0200175 return -ENODEV;
Jean Delvare54fb4a052008-07-14 22:38:33 +0200176
Jean Delvared6072f82005-09-25 16:37:04 +0200177 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
Jean Delvarefa63cd52008-07-14 22:38:25 +0200178 dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 piix4_smba);
Jean Delvarefa63cd52008-07-14 22:38:25 +0200180 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182
183 pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /* If force_addr is set, we program the new address here. Just to make
186 sure, we disable the PIIX4 first. */
187 if (force_addr) {
188 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe);
189 pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba);
190 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01);
191 dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to "
192 "new address %04x!\n", piix4_smba);
193 } else if ((temp & 1) == 0) {
194 if (force) {
195 /* This should never need to be done, but has been
196 * noted that many Dell machines have the SMBus
197 * interface on the PIIX4 disabled!? NOTE: This assumes
198 * I/O space and other allocations WERE done by the
199 * Bios! Don't complain if your hardware does weird
200 * things after enabling this. :') Check for Bios
201 * updates before resorting to this.
202 */
203 pci_write_config_byte(PIIX4_dev, SMBHSTCFG,
204 temp | 1);
Joe Perches8117e412012-12-16 21:11:55 +0100205 dev_notice(&PIIX4_dev->dev,
206 "WARNING: SMBus interface has been FORCEFULLY ENABLED!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 } else {
208 dev_err(&PIIX4_dev->dev,
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100209 "SMBus Host Controller not enabled!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 release_region(piix4_smba, SMBIOSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return -ENODEV;
212 }
213 }
214
Rudolf Marek54aaa1c2006-04-25 13:06:41 +0200215 if (((temp & 0x0E) == 8) || ((temp & 0x0E) == 2))
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100216 dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 else if ((temp & 0x0E) == 0)
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100218 dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 else
220 dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration "
221 "(or code out of date)!\n");
222
223 pci_read_config_byte(PIIX4_dev, SMBREV, &temp);
Jean Delvarefa63cd52008-07-14 22:38:25 +0200224 dev_info(&PIIX4_dev->dev,
225 "SMBus Host Controller at 0x%x, revision %d\n",
226 piix4_smba, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Andrew Armenia14a80862012-07-24 14:13:56 +0200228 return piix4_smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Bill Pemberton0b255e92012-11-27 15:59:38 -0500231static int piix4_setup_sb800(struct pci_dev *PIIX4_dev,
Rudolf Mareka94dd002013-07-14 23:17:26 +0200232 const struct pci_device_id *id, u8 aux)
Shane Huang87e19602009-03-28 21:34:46 +0100233{
Andrew Armenia14a80862012-07-24 14:13:56 +0200234 unsigned short piix4_smba;
Shane Huang87e19602009-03-28 21:34:46 +0100235 unsigned short smba_idx = 0xcd6;
Shane Huang032f7082014-01-22 14:05:46 -0800236 u8 smba_en_lo, smba_en_hi, smb_en, smb_en_status;
237 u8 i2ccfg, i2ccfg_offset = 0x10;
Shane Huang87e19602009-03-28 21:34:46 +0100238
Crane Cai3806e94b2009-11-07 13:10:46 +0100239 /* SB800 and later SMBus does not support forcing address */
Shane Huang87e19602009-03-28 21:34:46 +0100240 if (force || force_addr) {
Crane Cai3806e94b2009-11-07 13:10:46 +0100241 dev_err(&PIIX4_dev->dev, "SMBus does not support "
Shane Huang87e19602009-03-28 21:34:46 +0100242 "forcing address!\n");
243 return -EINVAL;
244 }
245
246 /* Determine the address of the SMBus areas */
Shane Huang032f7082014-01-22 14:05:46 -0800247 if ((PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
248 PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
249 PIIX4_dev->revision >= 0x41) ||
250 (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
Vincent Wanbcb29992015-06-11 20:11:46 +0800251 PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS &&
Shane Huang032f7082014-01-22 14:05:46 -0800252 PIIX4_dev->revision >= 0x49))
253 smb_en = 0x00;
254 else
255 smb_en = (aux) ? 0x28 : 0x2c;
Rudolf Mareka94dd002013-07-14 23:17:26 +0200256
Shane Huang87e19602009-03-28 21:34:46 +0100257 if (!request_region(smba_idx, 2, "smba_idx")) {
258 dev_err(&PIIX4_dev->dev, "SMBus base address index region "
259 "0x%x already in use!\n", smba_idx);
260 return -EBUSY;
261 }
262 outb_p(smb_en, smba_idx);
263 smba_en_lo = inb_p(smba_idx + 1);
264 outb_p(smb_en + 1, smba_idx);
265 smba_en_hi = inb_p(smba_idx + 1);
266 release_region(smba_idx, 2);
267
Shane Huang032f7082014-01-22 14:05:46 -0800268 if (!smb_en) {
269 smb_en_status = smba_en_lo & 0x10;
270 piix4_smba = smba_en_hi << 8;
271 if (aux)
272 piix4_smba |= 0x20;
273 } else {
274 smb_en_status = smba_en_lo & 0x01;
275 piix4_smba = ((smba_en_hi << 8) | smba_en_lo) & 0xffe0;
276 }
277
278 if (!smb_en_status) {
Shane Huang87e19602009-03-28 21:34:46 +0100279 dev_err(&PIIX4_dev->dev,
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100280 "SMBus Host Controller not enabled!\n");
Shane Huang87e19602009-03-28 21:34:46 +0100281 return -ENODEV;
282 }
283
Shane Huang87e19602009-03-28 21:34:46 +0100284 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
Jean Delvare18669ea2009-10-04 22:53:45 +0200285 return -ENODEV;
Shane Huang87e19602009-03-28 21:34:46 +0100286
287 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
288 dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
289 piix4_smba);
290 return -EBUSY;
291 }
292
Rudolf Mareka94dd002013-07-14 23:17:26 +0200293 /* Aux SMBus does not support IRQ information */
294 if (aux) {
295 dev_info(&PIIX4_dev->dev,
Shane Huang85fd0fe2014-01-22 14:06:52 -0800296 "Auxiliary SMBus Host Controller at 0x%x\n",
297 piix4_smba);
Rudolf Mareka94dd002013-07-14 23:17:26 +0200298 return piix4_smba;
299 }
300
Shane Huang87e19602009-03-28 21:34:46 +0100301 /* Request the SMBus I2C bus config region */
302 if (!request_region(piix4_smba + i2ccfg_offset, 1, "i2ccfg")) {
303 dev_err(&PIIX4_dev->dev, "SMBus I2C bus config region "
304 "0x%x already in use!\n", piix4_smba + i2ccfg_offset);
305 release_region(piix4_smba, SMBIOSIZE);
Shane Huang87e19602009-03-28 21:34:46 +0100306 return -EBUSY;
307 }
308 i2ccfg = inb_p(piix4_smba + i2ccfg_offset);
309 release_region(piix4_smba + i2ccfg_offset, 1);
310
311 if (i2ccfg & 1)
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100312 dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
Shane Huang87e19602009-03-28 21:34:46 +0100313 else
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100314 dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
Shane Huang87e19602009-03-28 21:34:46 +0100315
316 dev_info(&PIIX4_dev->dev,
317 "SMBus Host Controller at 0x%x, revision %d\n",
318 piix4_smba, i2ccfg >> 4);
319
Andrew Armenia14a80862012-07-24 14:13:56 +0200320 return piix4_smba;
Shane Huang87e19602009-03-28 21:34:46 +0100321}
322
Bill Pemberton0b255e92012-11-27 15:59:38 -0500323static int piix4_setup_aux(struct pci_dev *PIIX4_dev,
324 const struct pci_device_id *id,
325 unsigned short base_reg_addr)
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200326{
327 /* Set up auxiliary SMBus controllers found on some
328 * AMD chipsets e.g. SP5100 (SB700 derivative) */
329
330 unsigned short piix4_smba;
331
332 /* Read address of auxiliary SMBus controller */
333 pci_read_config_word(PIIX4_dev, base_reg_addr, &piix4_smba);
334 if ((piix4_smba & 1) == 0) {
335 dev_dbg(&PIIX4_dev->dev,
336 "Auxiliary SMBus controller not enabled\n");
337 return -ENODEV;
338 }
339
340 piix4_smba &= 0xfff0;
341 if (piix4_smba == 0) {
342 dev_dbg(&PIIX4_dev->dev,
343 "Auxiliary SMBus base address uninitialized\n");
344 return -ENODEV;
345 }
346
347 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
348 return -ENODEV;
349
350 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
351 dev_err(&PIIX4_dev->dev, "Auxiliary SMBus region 0x%x "
352 "already in use!\n", piix4_smba);
353 return -EBUSY;
354 }
355
356 dev_info(&PIIX4_dev->dev,
357 "Auxiliary SMBus Host Controller at 0x%x\n",
358 piix4_smba);
359
360 return piix4_smba;
361}
362
Andrew Armeniae154bf62012-07-24 14:13:56 +0200363static int piix4_transaction(struct i2c_adapter *piix4_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Andrew Armeniae154bf62012-07-24 14:13:56 +0200365 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(piix4_adapter);
366 unsigned short piix4_smba = adapdata->smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int temp;
368 int result = 0;
369 int timeout = 0;
370
Andrew Armeniae154bf62012-07-24 14:13:56 +0200371 dev_dbg(&piix4_adapter->dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
373 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
374 inb_p(SMBHSTDAT1));
375
376 /* Make sure the SMBus host is ready to start transmitting */
377 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200378 dev_dbg(&piix4_adapter->dev, "SMBus busy (%02x). "
Jean Delvare541e6a02005-06-23 22:18:08 +0200379 "Resetting...\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 outb_p(temp, SMBHSTSTS);
381 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200382 dev_err(&piix4_adapter->dev, "Failed! (%02x)\n", temp);
David Brownell97140342008-07-14 22:38:25 +0200383 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 } else {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200385 dev_dbg(&piix4_adapter->dev, "Successful!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 }
388
389 /* start the transaction by setting bit 6 */
390 outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
391
392 /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
David Milburnb1c17592008-05-11 20:37:05 +0200393 if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */
394 msleep(2);
395 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 msleep(1);
David Milburnb1c17592008-05-11 20:37:05 +0200397
Roel Kluinb6a31952010-01-16 20:43:12 +0100398 while ((++timeout < MAX_TIMEOUT) &&
David Milburnb1c17592008-05-11 20:37:05 +0200399 ((temp = inb_p(SMBHSTSTS)) & 0x01))
400 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /* If the SMBus is still busy, we give up */
Roel Kluinb6a31952010-01-16 20:43:12 +0100403 if (timeout == MAX_TIMEOUT) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200404 dev_err(&piix4_adapter->dev, "SMBus Timeout!\n");
David Brownell97140342008-07-14 22:38:25 +0200405 result = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
408 if (temp & 0x10) {
David Brownell97140342008-07-14 22:38:25 +0200409 result = -EIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200410 dev_err(&piix4_adapter->dev, "Error: Failed bus transaction\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
413 if (temp & 0x08) {
David Brownell97140342008-07-14 22:38:25 +0200414 result = -EIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200415 dev_dbg(&piix4_adapter->dev, "Bus collision! SMBus may be "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 "locked until next hard reset. (sorry!)\n");
417 /* Clock stops and slave is stuck in mid-transmission */
418 }
419
420 if (temp & 0x04) {
David Brownell97140342008-07-14 22:38:25 +0200421 result = -ENXIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200422 dev_dbg(&piix4_adapter->dev, "Error: no response!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
425 if (inb_p(SMBHSTSTS) != 0x00)
426 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
427
428 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200429 dev_err(&piix4_adapter->dev, "Failed reset at end of "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 "transaction (%02x)\n", temp);
431 }
Andrew Armeniae154bf62012-07-24 14:13:56 +0200432 dev_dbg(&piix4_adapter->dev, "Transaction (post): CNT=%02x, CMD=%02x, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
434 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
435 inb_p(SMBHSTDAT1));
436 return result;
437}
438
David Brownell97140342008-07-14 22:38:25 +0200439/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
441 unsigned short flags, char read_write,
442 u8 command, int size, union i2c_smbus_data * data)
443{
Andrew Armenia14a80862012-07-24 14:13:56 +0200444 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
445 unsigned short piix4_smba = adapdata->smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 int i, len;
David Brownell97140342008-07-14 22:38:25 +0200447 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 case I2C_SMBUS_QUICK:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200451 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 SMBHSTADD);
453 size = PIIX4_QUICK;
454 break;
455 case I2C_SMBUS_BYTE:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200456 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 SMBHSTADD);
458 if (read_write == I2C_SMBUS_WRITE)
459 outb_p(command, SMBHSTCMD);
460 size = PIIX4_BYTE;
461 break;
462 case I2C_SMBUS_BYTE_DATA:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200463 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 SMBHSTADD);
465 outb_p(command, SMBHSTCMD);
466 if (read_write == I2C_SMBUS_WRITE)
467 outb_p(data->byte, SMBHSTDAT0);
468 size = PIIX4_BYTE_DATA;
469 break;
470 case I2C_SMBUS_WORD_DATA:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200471 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 SMBHSTADD);
473 outb_p(command, SMBHSTCMD);
474 if (read_write == I2C_SMBUS_WRITE) {
475 outb_p(data->word & 0xff, SMBHSTDAT0);
476 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
477 }
478 size = PIIX4_WORD_DATA;
479 break;
480 case I2C_SMBUS_BLOCK_DATA:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200481 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 SMBHSTADD);
483 outb_p(command, SMBHSTCMD);
484 if (read_write == I2C_SMBUS_WRITE) {
485 len = data->block[0];
Jean Delvarefa63cd52008-07-14 22:38:25 +0200486 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
487 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 outb_p(len, SMBHSTDAT0);
489 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
490 for (i = 1; i <= len; i++)
491 outb_p(data->block[i], SMBBLKDAT);
492 }
493 size = PIIX4_BLOCK_DATA;
494 break;
Jean Delvareac7fc4f2008-07-14 22:38:25 +0200495 default:
496 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
497 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
500 outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
501
Andrew Armeniae154bf62012-07-24 14:13:56 +0200502 status = piix4_transaction(adap);
David Brownell97140342008-07-14 22:38:25 +0200503 if (status)
504 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
507 return 0;
508
509
510 switch (size) {
Jean Delvare3578a072008-04-29 23:11:37 +0200511 case PIIX4_BYTE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 case PIIX4_BYTE_DATA:
513 data->byte = inb_p(SMBHSTDAT0);
514 break;
515 case PIIX4_WORD_DATA:
516 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
517 break;
518 case PIIX4_BLOCK_DATA:
519 data->block[0] = inb_p(SMBHSTDAT0);
Jean Delvarefa63cd52008-07-14 22:38:25 +0200520 if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX)
521 return -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
523 for (i = 1; i <= data->block[0]; i++)
524 data->block[i] = inb_p(SMBBLKDAT);
525 break;
526 }
527 return 0;
528}
529
530static u32 piix4_func(struct i2c_adapter *adapter)
531{
532 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
533 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
534 I2C_FUNC_SMBUS_BLOCK_DATA;
535}
536
Jean Delvare8f9082c2006-09-03 22:39:46 +0200537static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 .smbus_xfer = piix4_access,
539 .functionality = piix4_func,
540};
541
Jingoo Han392debf2013-12-03 08:11:20 +0900542static const struct pci_device_id piix4_ids[] = {
Jean Delvare9b7389c2008-01-27 18:14:51 +0100543 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
544 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3) },
545 { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3) },
546 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_SMBUS) },
547 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_SMBUS) },
548 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS) },
549 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS) },
Crane Cai3806e94b2009-11-07 13:10:46 +0100550 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) },
Vincent Wanbcb29992015-06-11 20:11:46 +0800551 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) },
Jean Delvare9b7389c2008-01-27 18:14:51 +0100552 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
553 PCI_DEVICE_ID_SERVERWORKS_OSB4) },
554 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
555 PCI_DEVICE_ID_SERVERWORKS_CSB5) },
556 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
557 PCI_DEVICE_ID_SERVERWORKS_CSB6) },
558 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
559 PCI_DEVICE_ID_SERVERWORKS_HT1000SB) },
Flavio Leitner506a8b62009-03-28 21:34:46 +0100560 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
561 PCI_DEVICE_ID_SERVERWORKS_HT1100LD) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 { 0, }
563};
564
565MODULE_DEVICE_TABLE (pci, piix4_ids);
566
Christian Fetzerca2061e2015-11-19 20:13:47 +0100567static struct i2c_adapter *piix4_main_adapters[PIIX4_MAX_ADAPTERS];
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200568static struct i2c_adapter *piix4_aux_adapter;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200569
Bill Pemberton0b255e92012-11-27 15:59:38 -0500570static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
571 struct i2c_adapter **padap)
Andrew Armeniae154bf62012-07-24 14:13:56 +0200572{
573 struct i2c_adapter *adap;
574 struct i2c_piix4_adapdata *adapdata;
575 int retval;
576
577 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
578 if (adap == NULL) {
579 release_region(smba, SMBIOSIZE);
580 return -ENOMEM;
581 }
582
583 adap->owner = THIS_MODULE;
584 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
585 adap->algo = &smbus_algorithm;
586
587 adapdata = kzalloc(sizeof(*adapdata), GFP_KERNEL);
588 if (adapdata == NULL) {
589 kfree(adap);
590 release_region(smba, SMBIOSIZE);
591 return -ENOMEM;
592 }
593
594 adapdata->smba = smba;
595
596 /* set up the sysfs linkage to our parent device */
597 adap->dev.parent = &dev->dev;
598
599 snprintf(adap->name, sizeof(adap->name),
600 "SMBus PIIX4 adapter at %04x", smba);
601
602 i2c_set_adapdata(adap, adapdata);
603
604 retval = i2c_add_adapter(adap);
605 if (retval) {
606 dev_err(&dev->dev, "Couldn't register adapter!\n");
607 kfree(adapdata);
608 kfree(adap);
609 release_region(smba, SMBIOSIZE);
610 return retval;
611 }
612
613 *padap = adap;
614 return 0;
615}
616
Bill Pemberton0b255e92012-11-27 15:59:38 -0500617static int piix4_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 int retval;
620
Crane Cai76b3e282009-09-18 22:45:50 +0200621 if ((dev->vendor == PCI_VENDOR_ID_ATI &&
622 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS &&
623 dev->revision >= 0x40) ||
624 dev->vendor == PCI_VENDOR_ID_AMD)
Shane Huang87e19602009-03-28 21:34:46 +0100625 /* base address location etc changed in SB800 */
Rudolf Mareka94dd002013-07-14 23:17:26 +0200626 retval = piix4_setup_sb800(dev, id, 0);
Shane Huang87e19602009-03-28 21:34:46 +0100627 else
628 retval = piix4_setup(dev, id);
629
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200630 /* If no main SMBus found, give up */
Andrew Armenia14a80862012-07-24 14:13:56 +0200631 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return retval;
633
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200634 /* Try to register main SMBus adapter, give up if we can't */
Christian Fetzerca2061e2015-11-19 20:13:47 +0100635 retval = piix4_add_adapter(dev, retval, &piix4_main_adapters[0]);
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200636 if (retval < 0)
637 return retval;
638
639 /* Check for auxiliary SMBus on some AMD chipsets */
Rudolf Mareka94dd002013-07-14 23:17:26 +0200640 retval = -ENODEV;
641
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200642 if (dev->vendor == PCI_VENDOR_ID_ATI &&
Rudolf Mareka94dd002013-07-14 23:17:26 +0200643 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS) {
644 if (dev->revision < 0x40) {
645 retval = piix4_setup_aux(dev, id, 0x58);
646 } else {
647 /* SB800 added aux bus too */
648 retval = piix4_setup_sb800(dev, id, 1);
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200649 }
650 }
651
Rudolf Mareka94dd002013-07-14 23:17:26 +0200652 if (dev->vendor == PCI_VENDOR_ID_AMD &&
653 dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) {
654 retval = piix4_setup_sb800(dev, id, 1);
655 }
656
657 if (retval > 0) {
658 /* Try to add the aux adapter if it exists,
659 * piix4_add_adapter will clean up if this fails */
660 piix4_add_adapter(dev, retval, &piix4_aux_adapter);
661 }
662
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Bill Pemberton0b255e92012-11-27 15:59:38 -0500666static void piix4_adap_remove(struct i2c_adapter *adap)
Andrew Armenia14a80862012-07-24 14:13:56 +0200667{
668 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
669
670 if (adapdata->smba) {
671 i2c_del_adapter(adap);
672 release_region(adapdata->smba, SMBIOSIZE);
Andrew Armeniae154bf62012-07-24 14:13:56 +0200673 kfree(adapdata);
674 kfree(adap);
Andrew Armenia14a80862012-07-24 14:13:56 +0200675 }
676}
677
Bill Pemberton0b255e92012-11-27 15:59:38 -0500678static void piix4_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Christian Fetzerca2061e2015-11-19 20:13:47 +0100680 int port = PIIX4_MAX_ADAPTERS;
681
682 while (--port >= 0) {
683 if (piix4_main_adapters[port]) {
684 piix4_adap_remove(piix4_main_adapters[port]);
685 piix4_main_adapters[port] = NULL;
686 }
Andrew Armeniae154bf62012-07-24 14:13:56 +0200687 }
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200688
689 if (piix4_aux_adapter) {
690 piix4_adap_remove(piix4_aux_adapter);
691 piix4_aux_adapter = NULL;
692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695static struct pci_driver piix4_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 .name = "piix4_smbus",
697 .id_table = piix4_ids,
698 .probe = piix4_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500699 .remove = piix4_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700};
701
Axel Lin56f21782012-07-24 14:13:56 +0200702module_pci_driver(piix4_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
705 "Philip Edelbrock <phil@netroedge.com>");
706MODULE_DESCRIPTION("PIIX4 SMBus driver");
707MODULE_LICENSE("GPL");