blob: 489d378480c47a7423613fdd7222b08992c85a5c [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.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20/*
21 Supports:
22 Intel PIIX4, 440MX
Flavio Leitner506a8b62009-03-28 21:34:46 +010023 Serverworks OSB4, CSB5, CSB6, HT-1000, HT-1100
Andrew Armenia2a2f7402012-07-24 14:13:57 +020024 ATI IXP200, IXP300, IXP400, SB600, SB700/SP5100, SB800
Shane Huang032f7082014-01-22 14:05:46 -080025 AMD Hudson-2, ML, CZ
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 SMSC Victory66
27
Andrew Armenia2a2f7402012-07-24 14:13:57 +020028 Note: we assume there can only be one device, with one or more
29 SMBus interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030*/
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/pci.h>
35#include <linux/kernel.h>
36#include <linux/delay.h>
37#include <linux/stddef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ioport.h>
39#include <linux/i2c.h>
Daniel J Bluemanc415b302012-10-05 22:23:55 +020040#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/dmi.h>
Jean Delvare54fb4a052008-07-14 22:38:33 +020043#include <linux/acpi.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020044#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* PIIX4 SMBus address offsets */
48#define SMBHSTSTS (0 + piix4_smba)
49#define SMBHSLVSTS (1 + piix4_smba)
50#define SMBHSTCNT (2 + piix4_smba)
51#define SMBHSTCMD (3 + piix4_smba)
52#define SMBHSTADD (4 + piix4_smba)
53#define SMBHSTDAT0 (5 + piix4_smba)
54#define SMBHSTDAT1 (6 + piix4_smba)
55#define SMBBLKDAT (7 + piix4_smba)
56#define SMBSLVCNT (8 + piix4_smba)
57#define SMBSHDWCMD (9 + piix4_smba)
58#define SMBSLVEVT (0xA + piix4_smba)
59#define SMBSLVDAT (0xC + piix4_smba)
60
61/* count for request_region */
62#define SMBIOSIZE 8
63
64/* PCI Address Constants */
65#define SMBBA 0x090
66#define SMBHSTCFG 0x0D2
67#define SMBSLVC 0x0D3
68#define SMBSHDW1 0x0D4
69#define SMBSHDW2 0x0D5
70#define SMBREV 0x0D6
71
72/* Other settings */
73#define MAX_TIMEOUT 500
74#define ENABLE_INT9 0
75
76/* PIIX4 constants */
77#define PIIX4_QUICK 0x00
78#define PIIX4_BYTE 0x04
79#define PIIX4_BYTE_DATA 0x08
80#define PIIX4_WORD_DATA 0x0C
81#define PIIX4_BLOCK_DATA 0x14
82
83/* insmod parameters */
84
85/* If force is set to anything different from 0, we forcibly enable the
86 PIIX4. DANGEROUS! */
Jean Delvare60507092005-09-25 16:23:07 +020087static int force;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088module_param (force, int, 0);
89MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!");
90
91/* If force_addr is set to anything different from 0, we forcibly enable
92 the PIIX4 at the given address. VERY DANGEROUS! */
Jean Delvare60507092005-09-25 16:23:07 +020093static int force_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094module_param (force_addr, int, 0);
95MODULE_PARM_DESC(force_addr,
96 "Forcibly enable the PIIX4 at the given address. "
97 "EXTREMELY DANGEROUS!");
98
David Milburnb1c17592008-05-11 20:37:05 +020099static int srvrworks_csb5_delay;
Jean Delvared6072f82005-09-25 16:37:04 +0200100static struct pci_driver piix4_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Bill Pemberton0b255e92012-11-27 15:59:38 -0500102static const struct dmi_system_id piix4_dmi_blacklist[] = {
Jean Delvarec2fc54f2008-05-11 20:37:05 +0200103 {
104 .ident = "Sapphire AM2RD790",
105 .matches = {
106 DMI_MATCH(DMI_BOARD_VENDOR, "SAPPHIRE Inc."),
107 DMI_MATCH(DMI_BOARD_NAME, "PC-AM2RD790"),
108 },
109 },
110 {
111 .ident = "DFI Lanparty UT 790FX",
112 .matches = {
113 DMI_MATCH(DMI_BOARD_VENDOR, "DFI Inc."),
114 DMI_MATCH(DMI_BOARD_NAME, "LP UT 790FX"),
115 },
116 },
117 { }
118};
119
120/* The IBM entry is in a separate table because we only check it
121 on Intel-based systems */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500122static const struct dmi_system_id piix4_dmi_ibm[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 {
124 .ident = "IBM",
125 .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), },
126 },
127 { },
128};
129
Andrew Armenia14a80862012-07-24 14:13:56 +0200130struct i2c_piix4_adapdata {
131 unsigned short smba;
132};
133
Bill Pemberton0b255e92012-11-27 15:59:38 -0500134static int piix4_setup(struct pci_dev *PIIX4_dev,
135 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 unsigned char temp;
Andrew Armenia14a80862012-07-24 14:13:56 +0200138 unsigned short piix4_smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
David Milburnb1c17592008-05-11 20:37:05 +0200140 if ((PIIX4_dev->vendor == PCI_VENDOR_ID_SERVERWORKS) &&
141 (PIIX4_dev->device == PCI_DEVICE_ID_SERVERWORKS_CSB5))
142 srvrworks_csb5_delay = 1;
143
Jean Delvarec2fc54f2008-05-11 20:37:05 +0200144 /* On some motherboards, it was reported that accessing the SMBus
145 caused severe hardware problems */
146 if (dmi_check_system(piix4_dmi_blacklist)) {
147 dev_err(&PIIX4_dev->dev,
148 "Accessing the SMBus on this system is unsafe!\n");
149 return -EPERM;
150 }
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* Don't access SMBus on IBM systems which get corrupted eeproms */
Jean Delvarec2fc54f2008-05-11 20:37:05 +0200153 if (dmi_check_system(piix4_dmi_ibm) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) {
Jean Delvaref9ba6c02006-04-25 13:37:25 +0200155 dev_err(&PIIX4_dev->dev, "IBM system detected; this module "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 "may corrupt your serial eeprom! Refusing to load "
157 "module!\n");
158 return -EPERM;
159 }
160
161 /* Determine the address of the SMBus areas */
162 if (force_addr) {
163 piix4_smba = force_addr & 0xfff0;
164 force = 0;
165 } else {
166 pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba);
167 piix4_smba &= 0xfff0;
168 if(piix4_smba == 0) {
Jean Delvarefa63cd52008-07-14 22:38:25 +0200169 dev_err(&PIIX4_dev->dev, "SMBus base address "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 "uninitialized - upgrade BIOS or use "
171 "force_addr=0xaddr\n");
172 return -ENODEV;
173 }
174 }
175
Jean Delvare54fb4a052008-07-14 22:38:33 +0200176 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
Jean Delvare18669ea2009-10-04 22:53:45 +0200177 return -ENODEV;
Jean Delvare54fb4a052008-07-14 22:38:33 +0200178
Jean Delvared6072f82005-09-25 16:37:04 +0200179 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
Jean Delvarefa63cd52008-07-14 22:38:25 +0200180 dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 piix4_smba);
Jean Delvarefa63cd52008-07-14 22:38:25 +0200182 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184
185 pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* If force_addr is set, we program the new address here. Just to make
188 sure, we disable the PIIX4 first. */
189 if (force_addr) {
190 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe);
191 pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba);
192 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01);
193 dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to "
194 "new address %04x!\n", piix4_smba);
195 } else if ((temp & 1) == 0) {
196 if (force) {
197 /* This should never need to be done, but has been
198 * noted that many Dell machines have the SMBus
199 * interface on the PIIX4 disabled!? NOTE: This assumes
200 * I/O space and other allocations WERE done by the
201 * Bios! Don't complain if your hardware does weird
202 * things after enabling this. :') Check for Bios
203 * updates before resorting to this.
204 */
205 pci_write_config_byte(PIIX4_dev, SMBHSTCFG,
206 temp | 1);
Joe Perches8117e412012-12-16 21:11:55 +0100207 dev_notice(&PIIX4_dev->dev,
208 "WARNING: SMBus interface has been FORCEFULLY ENABLED!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 } else {
210 dev_err(&PIIX4_dev->dev,
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100211 "SMBus Host Controller not enabled!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 release_region(piix4_smba, SMBIOSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return -ENODEV;
214 }
215 }
216
Rudolf Marek54aaa1c2006-04-25 13:06:41 +0200217 if (((temp & 0x0E) == 8) || ((temp & 0x0E) == 2))
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100218 dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 else if ((temp & 0x0E) == 0)
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100220 dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 else
222 dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration "
223 "(or code out of date)!\n");
224
225 pci_read_config_byte(PIIX4_dev, SMBREV, &temp);
Jean Delvarefa63cd52008-07-14 22:38:25 +0200226 dev_info(&PIIX4_dev->dev,
227 "SMBus Host Controller at 0x%x, revision %d\n",
228 piix4_smba, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Andrew Armenia14a80862012-07-24 14:13:56 +0200230 return piix4_smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Bill Pemberton0b255e92012-11-27 15:59:38 -0500233static int piix4_setup_sb800(struct pci_dev *PIIX4_dev,
Rudolf Mareka94dd002013-07-14 23:17:26 +0200234 const struct pci_device_id *id, u8 aux)
Shane Huang87e19602009-03-28 21:34:46 +0100235{
Andrew Armenia14a80862012-07-24 14:13:56 +0200236 unsigned short piix4_smba;
Shane Huang87e19602009-03-28 21:34:46 +0100237 unsigned short smba_idx = 0xcd6;
Shane Huang032f7082014-01-22 14:05:46 -0800238 u8 smba_en_lo, smba_en_hi, smb_en, smb_en_status;
239 u8 i2ccfg, i2ccfg_offset = 0x10;
Shane Huang87e19602009-03-28 21:34:46 +0100240
Crane Cai3806e94b2009-11-07 13:10:46 +0100241 /* SB800 and later SMBus does not support forcing address */
Shane Huang87e19602009-03-28 21:34:46 +0100242 if (force || force_addr) {
Crane Cai3806e94b2009-11-07 13:10:46 +0100243 dev_err(&PIIX4_dev->dev, "SMBus does not support "
Shane Huang87e19602009-03-28 21:34:46 +0100244 "forcing address!\n");
245 return -EINVAL;
246 }
247
248 /* Determine the address of the SMBus areas */
Shane Huang032f7082014-01-22 14:05:46 -0800249 if ((PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
250 PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
251 PIIX4_dev->revision >= 0x41) ||
252 (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
253 PIIX4_dev->device == 0x790b &&
254 PIIX4_dev->revision >= 0x49))
255 smb_en = 0x00;
256 else
257 smb_en = (aux) ? 0x28 : 0x2c;
Rudolf Mareka94dd002013-07-14 23:17:26 +0200258
Shane Huang87e19602009-03-28 21:34:46 +0100259 if (!request_region(smba_idx, 2, "smba_idx")) {
260 dev_err(&PIIX4_dev->dev, "SMBus base address index region "
261 "0x%x already in use!\n", smba_idx);
262 return -EBUSY;
263 }
264 outb_p(smb_en, smba_idx);
265 smba_en_lo = inb_p(smba_idx + 1);
266 outb_p(smb_en + 1, smba_idx);
267 smba_en_hi = inb_p(smba_idx + 1);
268 release_region(smba_idx, 2);
269
Shane Huang032f7082014-01-22 14:05:46 -0800270 if (!smb_en) {
271 smb_en_status = smba_en_lo & 0x10;
272 piix4_smba = smba_en_hi << 8;
273 if (aux)
274 piix4_smba |= 0x20;
275 } else {
276 smb_en_status = smba_en_lo & 0x01;
277 piix4_smba = ((smba_en_hi << 8) | smba_en_lo) & 0xffe0;
278 }
279
280 if (!smb_en_status) {
Shane Huang87e19602009-03-28 21:34:46 +0100281 dev_err(&PIIX4_dev->dev,
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100282 "SMBus Host Controller not enabled!\n");
Shane Huang87e19602009-03-28 21:34:46 +0100283 return -ENODEV;
284 }
285
Shane Huang87e19602009-03-28 21:34:46 +0100286 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
Jean Delvare18669ea2009-10-04 22:53:45 +0200287 return -ENODEV;
Shane Huang87e19602009-03-28 21:34:46 +0100288
289 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
290 dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
291 piix4_smba);
292 return -EBUSY;
293 }
294
Rudolf Mareka94dd002013-07-14 23:17:26 +0200295 /* Aux SMBus does not support IRQ information */
296 if (aux) {
297 dev_info(&PIIX4_dev->dev,
Shane Huang85fd0fe2014-01-22 14:06:52 -0800298 "Auxiliary SMBus Host Controller at 0x%x\n",
299 piix4_smba);
Rudolf Mareka94dd002013-07-14 23:17:26 +0200300 return piix4_smba;
301 }
302
Shane Huang87e19602009-03-28 21:34:46 +0100303 /* Request the SMBus I2C bus config region */
304 if (!request_region(piix4_smba + i2ccfg_offset, 1, "i2ccfg")) {
305 dev_err(&PIIX4_dev->dev, "SMBus I2C bus config region "
306 "0x%x already in use!\n", piix4_smba + i2ccfg_offset);
307 release_region(piix4_smba, SMBIOSIZE);
Shane Huang87e19602009-03-28 21:34:46 +0100308 return -EBUSY;
309 }
310 i2ccfg = inb_p(piix4_smba + i2ccfg_offset);
311 release_region(piix4_smba + i2ccfg_offset, 1);
312
313 if (i2ccfg & 1)
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100314 dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
Shane Huang87e19602009-03-28 21:34:46 +0100315 else
Jean Delvare66f8a8f2014-01-23 16:59:38 +0100316 dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
Shane Huang87e19602009-03-28 21:34:46 +0100317
318 dev_info(&PIIX4_dev->dev,
319 "SMBus Host Controller at 0x%x, revision %d\n",
320 piix4_smba, i2ccfg >> 4);
321
Andrew Armenia14a80862012-07-24 14:13:56 +0200322 return piix4_smba;
Shane Huang87e19602009-03-28 21:34:46 +0100323}
324
Bill Pemberton0b255e92012-11-27 15:59:38 -0500325static int piix4_setup_aux(struct pci_dev *PIIX4_dev,
326 const struct pci_device_id *id,
327 unsigned short base_reg_addr)
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200328{
329 /* Set up auxiliary SMBus controllers found on some
330 * AMD chipsets e.g. SP5100 (SB700 derivative) */
331
332 unsigned short piix4_smba;
333
334 /* Read address of auxiliary SMBus controller */
335 pci_read_config_word(PIIX4_dev, base_reg_addr, &piix4_smba);
336 if ((piix4_smba & 1) == 0) {
337 dev_dbg(&PIIX4_dev->dev,
338 "Auxiliary SMBus controller not enabled\n");
339 return -ENODEV;
340 }
341
342 piix4_smba &= 0xfff0;
343 if (piix4_smba == 0) {
344 dev_dbg(&PIIX4_dev->dev,
345 "Auxiliary SMBus base address uninitialized\n");
346 return -ENODEV;
347 }
348
349 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
350 return -ENODEV;
351
352 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
353 dev_err(&PIIX4_dev->dev, "Auxiliary SMBus region 0x%x "
354 "already in use!\n", piix4_smba);
355 return -EBUSY;
356 }
357
358 dev_info(&PIIX4_dev->dev,
359 "Auxiliary SMBus Host Controller at 0x%x\n",
360 piix4_smba);
361
362 return piix4_smba;
363}
364
Andrew Armeniae154bf62012-07-24 14:13:56 +0200365static int piix4_transaction(struct i2c_adapter *piix4_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Andrew Armeniae154bf62012-07-24 14:13:56 +0200367 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(piix4_adapter);
368 unsigned short piix4_smba = adapdata->smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int temp;
370 int result = 0;
371 int timeout = 0;
372
Andrew Armeniae154bf62012-07-24 14:13:56 +0200373 dev_dbg(&piix4_adapter->dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
375 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
376 inb_p(SMBHSTDAT1));
377
378 /* Make sure the SMBus host is ready to start transmitting */
379 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200380 dev_dbg(&piix4_adapter->dev, "SMBus busy (%02x). "
Jean Delvare541e6a02005-06-23 22:18:08 +0200381 "Resetting...\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 outb_p(temp, SMBHSTSTS);
383 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200384 dev_err(&piix4_adapter->dev, "Failed! (%02x)\n", temp);
David Brownell97140342008-07-14 22:38:25 +0200385 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 } else {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200387 dev_dbg(&piix4_adapter->dev, "Successful!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389 }
390
391 /* start the transaction by setting bit 6 */
392 outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
393
394 /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
David Milburnb1c17592008-05-11 20:37:05 +0200395 if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */
396 msleep(2);
397 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 msleep(1);
David Milburnb1c17592008-05-11 20:37:05 +0200399
Roel Kluinb6a31952010-01-16 20:43:12 +0100400 while ((++timeout < MAX_TIMEOUT) &&
David Milburnb1c17592008-05-11 20:37:05 +0200401 ((temp = inb_p(SMBHSTSTS)) & 0x01))
402 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /* If the SMBus is still busy, we give up */
Roel Kluinb6a31952010-01-16 20:43:12 +0100405 if (timeout == MAX_TIMEOUT) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200406 dev_err(&piix4_adapter->dev, "SMBus Timeout!\n");
David Brownell97140342008-07-14 22:38:25 +0200407 result = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
410 if (temp & 0x10) {
David Brownell97140342008-07-14 22:38:25 +0200411 result = -EIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200412 dev_err(&piix4_adapter->dev, "Error: Failed bus transaction\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 if (temp & 0x08) {
David Brownell97140342008-07-14 22:38:25 +0200416 result = -EIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200417 dev_dbg(&piix4_adapter->dev, "Bus collision! SMBus may be "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 "locked until next hard reset. (sorry!)\n");
419 /* Clock stops and slave is stuck in mid-transmission */
420 }
421
422 if (temp & 0x04) {
David Brownell97140342008-07-14 22:38:25 +0200423 result = -ENXIO;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200424 dev_dbg(&piix4_adapter->dev, "Error: no response!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426
427 if (inb_p(SMBHSTSTS) != 0x00)
428 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
429
430 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
Andrew Armeniae154bf62012-07-24 14:13:56 +0200431 dev_err(&piix4_adapter->dev, "Failed reset at end of "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 "transaction (%02x)\n", temp);
433 }
Andrew Armeniae154bf62012-07-24 14:13:56 +0200434 dev_dbg(&piix4_adapter->dev, "Transaction (post): CNT=%02x, CMD=%02x, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
436 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
437 inb_p(SMBHSTDAT1));
438 return result;
439}
440
David Brownell97140342008-07-14 22:38:25 +0200441/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
443 unsigned short flags, char read_write,
444 u8 command, int size, union i2c_smbus_data * data)
445{
Andrew Armenia14a80862012-07-24 14:13:56 +0200446 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
447 unsigned short piix4_smba = adapdata->smba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int i, len;
David Brownell97140342008-07-14 22:38:25 +0200449 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 case I2C_SMBUS_QUICK:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200453 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 SMBHSTADD);
455 size = PIIX4_QUICK;
456 break;
457 case I2C_SMBUS_BYTE:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200458 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 SMBHSTADD);
460 if (read_write == I2C_SMBUS_WRITE)
461 outb_p(command, SMBHSTCMD);
462 size = PIIX4_BYTE;
463 break;
464 case I2C_SMBUS_BYTE_DATA:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200465 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 SMBHSTADD);
467 outb_p(command, SMBHSTCMD);
468 if (read_write == I2C_SMBUS_WRITE)
469 outb_p(data->byte, SMBHSTDAT0);
470 size = PIIX4_BYTE_DATA;
471 break;
472 case I2C_SMBUS_WORD_DATA:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200473 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 SMBHSTADD);
475 outb_p(command, SMBHSTCMD);
476 if (read_write == I2C_SMBUS_WRITE) {
477 outb_p(data->word & 0xff, SMBHSTDAT0);
478 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
479 }
480 size = PIIX4_WORD_DATA;
481 break;
482 case I2C_SMBUS_BLOCK_DATA:
Jean Delvarefa63cd52008-07-14 22:38:25 +0200483 outb_p((addr << 1) | read_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 SMBHSTADD);
485 outb_p(command, SMBHSTCMD);
486 if (read_write == I2C_SMBUS_WRITE) {
487 len = data->block[0];
Jean Delvarefa63cd52008-07-14 22:38:25 +0200488 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
489 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 outb_p(len, SMBHSTDAT0);
491 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
492 for (i = 1; i <= len; i++)
493 outb_p(data->block[i], SMBBLKDAT);
494 }
495 size = PIIX4_BLOCK_DATA;
496 break;
Jean Delvareac7fc4f2008-07-14 22:38:25 +0200497 default:
498 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
499 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
502 outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
503
Andrew Armeniae154bf62012-07-24 14:13:56 +0200504 status = piix4_transaction(adap);
David Brownell97140342008-07-14 22:38:25 +0200505 if (status)
506 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
509 return 0;
510
511
512 switch (size) {
Jean Delvare3578a072008-04-29 23:11:37 +0200513 case PIIX4_BYTE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 case PIIX4_BYTE_DATA:
515 data->byte = inb_p(SMBHSTDAT0);
516 break;
517 case PIIX4_WORD_DATA:
518 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
519 break;
520 case PIIX4_BLOCK_DATA:
521 data->block[0] = inb_p(SMBHSTDAT0);
Jean Delvarefa63cd52008-07-14 22:38:25 +0200522 if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX)
523 return -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
525 for (i = 1; i <= data->block[0]; i++)
526 data->block[i] = inb_p(SMBBLKDAT);
527 break;
528 }
529 return 0;
530}
531
532static u32 piix4_func(struct i2c_adapter *adapter)
533{
534 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
535 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
536 I2C_FUNC_SMBUS_BLOCK_DATA;
537}
538
Jean Delvare8f9082c2006-09-03 22:39:46 +0200539static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 .smbus_xfer = piix4_access,
541 .functionality = piix4_func,
542};
543
Axel Lin3527bd52012-01-12 20:32:04 +0100544static DEFINE_PCI_DEVICE_TABLE(piix4_ids) = {
Jean Delvare9b7389c2008-01-27 18:14:51 +0100545 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
546 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3) },
547 { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3) },
548 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_SMBUS) },
549 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_SMBUS) },
550 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS) },
551 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS) },
Crane Cai3806e94b2009-11-07 13:10:46 +0100552 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) },
Shane Huangb996ac92013-06-03 18:24:55 +0800553 { PCI_DEVICE(PCI_VENDOR_ID_AMD, 0x790b) },
Jean Delvare9b7389c2008-01-27 18:14:51 +0100554 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
555 PCI_DEVICE_ID_SERVERWORKS_OSB4) },
556 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
557 PCI_DEVICE_ID_SERVERWORKS_CSB5) },
558 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
559 PCI_DEVICE_ID_SERVERWORKS_CSB6) },
560 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
561 PCI_DEVICE_ID_SERVERWORKS_HT1000SB) },
Flavio Leitner506a8b62009-03-28 21:34:46 +0100562 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
563 PCI_DEVICE_ID_SERVERWORKS_HT1100LD) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 { 0, }
565};
566
567MODULE_DEVICE_TABLE (pci, piix4_ids);
568
Andrew Armeniae154bf62012-07-24 14:13:56 +0200569static struct i2c_adapter *piix4_main_adapter;
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200570static struct i2c_adapter *piix4_aux_adapter;
Andrew Armeniae154bf62012-07-24 14:13:56 +0200571
Bill Pemberton0b255e92012-11-27 15:59:38 -0500572static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
573 struct i2c_adapter **padap)
Andrew Armeniae154bf62012-07-24 14:13:56 +0200574{
575 struct i2c_adapter *adap;
576 struct i2c_piix4_adapdata *adapdata;
577 int retval;
578
579 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
580 if (adap == NULL) {
581 release_region(smba, SMBIOSIZE);
582 return -ENOMEM;
583 }
584
585 adap->owner = THIS_MODULE;
586 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
587 adap->algo = &smbus_algorithm;
588
589 adapdata = kzalloc(sizeof(*adapdata), GFP_KERNEL);
590 if (adapdata == NULL) {
591 kfree(adap);
592 release_region(smba, SMBIOSIZE);
593 return -ENOMEM;
594 }
595
596 adapdata->smba = smba;
597
598 /* set up the sysfs linkage to our parent device */
599 adap->dev.parent = &dev->dev;
600
601 snprintf(adap->name, sizeof(adap->name),
602 "SMBus PIIX4 adapter at %04x", smba);
603
604 i2c_set_adapdata(adap, adapdata);
605
606 retval = i2c_add_adapter(adap);
607 if (retval) {
608 dev_err(&dev->dev, "Couldn't register adapter!\n");
609 kfree(adapdata);
610 kfree(adap);
611 release_region(smba, SMBIOSIZE);
612 return retval;
613 }
614
615 *padap = adap;
616 return 0;
617}
618
Bill Pemberton0b255e92012-11-27 15:59:38 -0500619static int piix4_probe(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 int retval;
622
Crane Cai76b3e282009-09-18 22:45:50 +0200623 if ((dev->vendor == PCI_VENDOR_ID_ATI &&
624 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS &&
625 dev->revision >= 0x40) ||
626 dev->vendor == PCI_VENDOR_ID_AMD)
Shane Huang87e19602009-03-28 21:34:46 +0100627 /* base address location etc changed in SB800 */
Rudolf Mareka94dd002013-07-14 23:17:26 +0200628 retval = piix4_setup_sb800(dev, id, 0);
Shane Huang87e19602009-03-28 21:34:46 +0100629 else
630 retval = piix4_setup(dev, id);
631
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200632 /* If no main SMBus found, give up */
Andrew Armenia14a80862012-07-24 14:13:56 +0200633 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return retval;
635
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200636 /* Try to register main SMBus adapter, give up if we can't */
637 retval = piix4_add_adapter(dev, retval, &piix4_main_adapter);
638 if (retval < 0)
639 return retval;
640
641 /* Check for auxiliary SMBus on some AMD chipsets */
Rudolf Mareka94dd002013-07-14 23:17:26 +0200642 retval = -ENODEV;
643
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200644 if (dev->vendor == PCI_VENDOR_ID_ATI &&
Rudolf Mareka94dd002013-07-14 23:17:26 +0200645 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS) {
646 if (dev->revision < 0x40) {
647 retval = piix4_setup_aux(dev, id, 0x58);
648 } else {
649 /* SB800 added aux bus too */
650 retval = piix4_setup_sb800(dev, id, 1);
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200651 }
652 }
653
Rudolf Mareka94dd002013-07-14 23:17:26 +0200654 if (dev->vendor == PCI_VENDOR_ID_AMD &&
655 dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) {
656 retval = piix4_setup_sb800(dev, id, 1);
657 }
658
659 if (retval > 0) {
660 /* Try to add the aux adapter if it exists,
661 * piix4_add_adapter will clean up if this fails */
662 piix4_add_adapter(dev, retval, &piix4_aux_adapter);
663 }
664
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200665 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Bill Pemberton0b255e92012-11-27 15:59:38 -0500668static void piix4_adap_remove(struct i2c_adapter *adap)
Andrew Armenia14a80862012-07-24 14:13:56 +0200669{
670 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
671
672 if (adapdata->smba) {
673 i2c_del_adapter(adap);
674 release_region(adapdata->smba, SMBIOSIZE);
Andrew Armeniae154bf62012-07-24 14:13:56 +0200675 kfree(adapdata);
676 kfree(adap);
Andrew Armenia14a80862012-07-24 14:13:56 +0200677 }
678}
679
Bill Pemberton0b255e92012-11-27 15:59:38 -0500680static void piix4_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Andrew Armeniae154bf62012-07-24 14:13:56 +0200682 if (piix4_main_adapter) {
683 piix4_adap_remove(piix4_main_adapter);
684 piix4_main_adapter = NULL;
685 }
Andrew Armenia2a2f7402012-07-24 14:13:57 +0200686
687 if (piix4_aux_adapter) {
688 piix4_adap_remove(piix4_aux_adapter);
689 piix4_aux_adapter = NULL;
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693static struct pci_driver piix4_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 .name = "piix4_smbus",
695 .id_table = piix4_ids,
696 .probe = piix4_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500697 .remove = piix4_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698};
699
Axel Lin56f21782012-07-24 14:13:56 +0200700module_pci_driver(piix4_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
703 "Philip Edelbrock <phil@netroedge.com>");
704MODULE_DESCRIPTION("PIIX4 SMBus driver");
705MODULE_LICENSE("GPL");