2 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl> and
3 Philip Edelbrock <phil@netroedge.com>
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.
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.
19 Serverworks OSB4, CSB5, CSB6, HT-1000, HT-1100
20 ATI IXP200, IXP300, IXP400, SB600, SB700/SP5100, SB800
24 Note: we assume there can only be one device, with one or more
26 The device can register multiple i2c_adapters (up to PIIX4_MAX_ADAPTERS).
27 For devices supporting multiple ports the i2c_adapter should provide
28 an i2c_algorithm to access them.
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/pci.h>
34 #include <linux/kernel.h>
35 #include <linux/delay.h>
36 #include <linux/stddef.h>
37 #include <linux/ioport.h>
38 #include <linux/i2c.h>
39 #include <linux/slab.h>
40 #include <linux/dmi.h>
41 #include <linux/acpi.h>
45 /* PIIX4 SMBus address offsets */
46 #define SMBHSTSTS (0 + piix4_smba)
47 #define SMBHSLVSTS (1 + piix4_smba)
48 #define SMBHSTCNT (2 + piix4_smba)
49 #define SMBHSTCMD (3 + piix4_smba)
50 #define SMBHSTADD (4 + piix4_smba)
51 #define SMBHSTDAT0 (5 + piix4_smba)
52 #define SMBHSTDAT1 (6 + piix4_smba)
53 #define SMBBLKDAT (7 + piix4_smba)
54 #define SMBSLVCNT (8 + piix4_smba)
55 #define SMBSHDWCMD (9 + piix4_smba)
56 #define SMBSLVEVT (0xA + piix4_smba)
57 #define SMBSLVDAT (0xC + piix4_smba)
59 /* count for request_region */
62 /* PCI Address Constants */
64 #define SMBHSTCFG 0x0D2
66 #define SMBSHDW1 0x0D4
67 #define SMBSHDW2 0x0D5
71 #define MAX_TIMEOUT 500
75 #define PIIX4_QUICK 0x00
76 #define PIIX4_BYTE 0x04
77 #define PIIX4_BYTE_DATA 0x08
78 #define PIIX4_WORD_DATA 0x0C
79 #define PIIX4_BLOCK_DATA 0x14
81 /* Multi-port constants */
82 #define PIIX4_MAX_ADAPTERS 4
85 #define SB800_PIIX4_SMB_IDX 0xcd6
87 #define KERNCZ_IMC_IDX 0x3e
88 #define KERNCZ_IMC_DATA 0x3f
91 * SB800 port is selected by bits 2:1 of the smb_en register (0x2c)
92 * or the smb_sel register (0x2e), depending on bit 0 of register 0x2f.
93 * Hudson-2/Bolton port is always selected by bits 2:1 of register 0x2f.
95 #define SB800_PIIX4_PORT_IDX 0x2c
96 #define SB800_PIIX4_PORT_IDX_ALT 0x2e
97 #define SB800_PIIX4_PORT_IDX_SEL 0x2f
98 #define SB800_PIIX4_PORT_IDX_MASK 0x06
99 #define SB800_PIIX4_PORT_IDX_SHIFT 1
101 /* On kerncz and Hudson2, SmBus0Sel is at bit 20:19 of PMx00 DecodeEn */
102 #define SB800_PIIX4_PORT_IDX_KERNCZ 0x02
103 #define SB800_PIIX4_PORT_IDX_MASK_KERNCZ 0x18
104 #define SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ 3
106 /* insmod parameters */
108 /* If force is set to anything different from 0, we forcibly enable the
111 module_param (force, int, 0);
112 MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!");
114 /* If force_addr is set to anything different from 0, we forcibly enable
115 the PIIX4 at the given address. VERY DANGEROUS! */
116 static int force_addr;
117 module_param_hw(force_addr, int, ioport, 0);
118 MODULE_PARM_DESC(force_addr,
119 "Forcibly enable the PIIX4 at the given address. "
120 "EXTREMELY DANGEROUS!");
122 static int srvrworks_csb5_delay;
123 static struct pci_driver piix4_driver;
125 static const struct dmi_system_id piix4_dmi_blacklist[] = {
127 .ident = "Sapphire AM2RD790",
129 DMI_MATCH(DMI_BOARD_VENDOR, "SAPPHIRE Inc."),
130 DMI_MATCH(DMI_BOARD_NAME, "PC-AM2RD790"),
134 .ident = "DFI Lanparty UT 790FX",
136 DMI_MATCH(DMI_BOARD_VENDOR, "DFI Inc."),
137 DMI_MATCH(DMI_BOARD_NAME, "LP UT 790FX"),
143 /* The IBM entry is in a separate table because we only check it
144 on Intel-based systems */
145 static const struct dmi_system_id piix4_dmi_ibm[] = {
148 .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), },
156 static u8 piix4_port_sel_sb800;
157 static u8 piix4_port_mask_sb800;
158 static u8 piix4_port_shift_sb800;
159 static const char *piix4_main_port_names_sb800[PIIX4_MAX_ADAPTERS] = {
160 " port 0", " port 2", " port 3", " port 4"
162 static const char *piix4_aux_port_name_sb800 = " port 1";
164 struct i2c_piix4_adapdata {
170 u8 port; /* Port number, shifted */
173 static int piix4_setup(struct pci_dev *PIIX4_dev,
174 const struct pci_device_id *id)
177 unsigned short piix4_smba;
179 if ((PIIX4_dev->vendor == PCI_VENDOR_ID_SERVERWORKS) &&
180 (PIIX4_dev->device == PCI_DEVICE_ID_SERVERWORKS_CSB5))
181 srvrworks_csb5_delay = 1;
183 /* On some motherboards, it was reported that accessing the SMBus
184 caused severe hardware problems */
185 if (dmi_check_system(piix4_dmi_blacklist)) {
186 dev_err(&PIIX4_dev->dev,
187 "Accessing the SMBus on this system is unsafe!\n");
191 /* Don't access SMBus on IBM systems which get corrupted eeproms */
192 if (dmi_check_system(piix4_dmi_ibm) &&
193 PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) {
194 dev_err(&PIIX4_dev->dev, "IBM system detected; this module "
195 "may corrupt your serial eeprom! Refusing to load "
200 /* Determine the address of the SMBus areas */
202 piix4_smba = force_addr & 0xfff0;
205 pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba);
206 piix4_smba &= 0xfff0;
207 if(piix4_smba == 0) {
208 dev_err(&PIIX4_dev->dev, "SMBus base address "
209 "uninitialized - upgrade BIOS or use "
210 "force_addr=0xaddr\n");
215 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
218 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
219 dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
224 pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp);
226 /* If force_addr is set, we program the new address here. Just to make
227 sure, we disable the PIIX4 first. */
229 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe);
230 pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba);
231 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01);
232 dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to "
233 "new address %04x!\n", piix4_smba);
234 } else if ((temp & 1) == 0) {
236 /* This should never need to be done, but has been
237 * noted that many Dell machines have the SMBus
238 * interface on the PIIX4 disabled!? NOTE: This assumes
239 * I/O space and other allocations WERE done by the
240 * Bios! Don't complain if your hardware does weird
241 * things after enabling this. :') Check for Bios
242 * updates before resorting to this.
244 pci_write_config_byte(PIIX4_dev, SMBHSTCFG,
246 dev_notice(&PIIX4_dev->dev,
247 "WARNING: SMBus interface has been FORCEFULLY ENABLED!\n");
249 dev_err(&PIIX4_dev->dev,
250 "SMBus Host Controller not enabled!\n");
251 release_region(piix4_smba, SMBIOSIZE);
256 if (((temp & 0x0E) == 8) || ((temp & 0x0E) == 2))
257 dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
258 else if ((temp & 0x0E) == 0)
259 dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
261 dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration "
262 "(or code out of date)!\n");
264 pci_read_config_byte(PIIX4_dev, SMBREV, &temp);
265 dev_info(&PIIX4_dev->dev,
266 "SMBus Host Controller at 0x%x, revision %d\n",
272 static int piix4_setup_sb800(struct pci_dev *PIIX4_dev,
273 const struct pci_device_id *id, u8 aux)
275 unsigned short piix4_smba;
276 u8 smba_en_lo, smba_en_hi, smb_en, smb_en_status, port_sel;
277 u8 i2ccfg, i2ccfg_offset = 0x10;
279 /* SB800 and later SMBus does not support forcing address */
280 if (force || force_addr) {
281 dev_err(&PIIX4_dev->dev, "SMBus does not support "
282 "forcing address!\n");
286 /* Determine the address of the SMBus areas */
287 if ((PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
288 PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
289 PIIX4_dev->revision >= 0x41) ||
290 (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
291 PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS &&
292 PIIX4_dev->revision >= 0x49))
295 smb_en = (aux) ? 0x28 : 0x2c;
297 if (!request_muxed_region(SB800_PIIX4_SMB_IDX, 2, "sb800_piix4_smb")) {
298 dev_err(&PIIX4_dev->dev,
299 "SMB base address index region 0x%x already in use.\n",
300 SB800_PIIX4_SMB_IDX);
304 outb_p(smb_en, SB800_PIIX4_SMB_IDX);
305 smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
306 outb_p(smb_en + 1, SB800_PIIX4_SMB_IDX);
307 smba_en_hi = inb_p(SB800_PIIX4_SMB_IDX + 1);
309 release_region(SB800_PIIX4_SMB_IDX, 2);
312 smb_en_status = smba_en_lo & 0x10;
313 piix4_smba = smba_en_hi << 8;
317 smb_en_status = smba_en_lo & 0x01;
318 piix4_smba = ((smba_en_hi << 8) | smba_en_lo) & 0xffe0;
321 if (!smb_en_status) {
322 dev_err(&PIIX4_dev->dev,
323 "SMBus Host Controller not enabled!\n");
327 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
330 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
331 dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
336 /* Aux SMBus does not support IRQ information */
338 dev_info(&PIIX4_dev->dev,
339 "Auxiliary SMBus Host Controller at 0x%x\n",
344 /* Request the SMBus I2C bus config region */
345 if (!request_region(piix4_smba + i2ccfg_offset, 1, "i2ccfg")) {
346 dev_err(&PIIX4_dev->dev, "SMBus I2C bus config region "
347 "0x%x already in use!\n", piix4_smba + i2ccfg_offset);
348 release_region(piix4_smba, SMBIOSIZE);
351 i2ccfg = inb_p(piix4_smba + i2ccfg_offset);
352 release_region(piix4_smba + i2ccfg_offset, 1);
355 dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
357 dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
359 dev_info(&PIIX4_dev->dev,
360 "SMBus Host Controller at 0x%x, revision %d\n",
361 piix4_smba, i2ccfg >> 4);
363 /* Find which register is used for port selection */
364 if (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD) {
365 if (PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS ||
366 (PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
367 PIIX4_dev->revision >= 0x1F)) {
368 piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_KERNCZ;
369 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK_KERNCZ;
370 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ;
372 piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_ALT;
373 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
374 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
377 if (!request_muxed_region(SB800_PIIX4_SMB_IDX, 2,
378 "sb800_piix4_smb")) {
379 release_region(piix4_smba, SMBIOSIZE);
383 outb_p(SB800_PIIX4_PORT_IDX_SEL, SB800_PIIX4_SMB_IDX);
384 port_sel = inb_p(SB800_PIIX4_SMB_IDX + 1);
385 piix4_port_sel_sb800 = (port_sel & 0x01) ?
386 SB800_PIIX4_PORT_IDX_ALT :
387 SB800_PIIX4_PORT_IDX;
388 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
389 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
390 release_region(SB800_PIIX4_SMB_IDX, 2);
393 dev_info(&PIIX4_dev->dev,
394 "Using register 0x%02x for SMBus port selection\n",
395 (unsigned int)piix4_port_sel_sb800);
400 static int piix4_setup_aux(struct pci_dev *PIIX4_dev,
401 const struct pci_device_id *id,
402 unsigned short base_reg_addr)
404 /* Set up auxiliary SMBus controllers found on some
405 * AMD chipsets e.g. SP5100 (SB700 derivative) */
407 unsigned short piix4_smba;
409 /* Read address of auxiliary SMBus controller */
410 pci_read_config_word(PIIX4_dev, base_reg_addr, &piix4_smba);
411 if ((piix4_smba & 1) == 0) {
412 dev_dbg(&PIIX4_dev->dev,
413 "Auxiliary SMBus controller not enabled\n");
417 piix4_smba &= 0xfff0;
418 if (piix4_smba == 0) {
419 dev_dbg(&PIIX4_dev->dev,
420 "Auxiliary SMBus base address uninitialized\n");
424 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
427 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
428 dev_err(&PIIX4_dev->dev, "Auxiliary SMBus region 0x%x "
429 "already in use!\n", piix4_smba);
433 dev_info(&PIIX4_dev->dev,
434 "Auxiliary SMBus Host Controller at 0x%x\n",
440 static int piix4_transaction(struct i2c_adapter *piix4_adapter)
442 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(piix4_adapter);
443 unsigned short piix4_smba = adapdata->smba;
448 dev_dbg(&piix4_adapter->dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
449 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
450 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
453 /* Make sure the SMBus host is ready to start transmitting */
454 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
455 dev_dbg(&piix4_adapter->dev, "SMBus busy (%02x). "
456 "Resetting...\n", temp);
457 outb_p(temp, SMBHSTSTS);
458 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
459 dev_err(&piix4_adapter->dev, "Failed! (%02x)\n", temp);
462 dev_dbg(&piix4_adapter->dev, "Successful!\n");
466 /* start the transaction by setting bit 6 */
467 outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
469 /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
470 if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */
471 usleep_range(2000, 2100);
473 usleep_range(250, 500);
475 while ((++timeout < MAX_TIMEOUT) &&
476 ((temp = inb_p(SMBHSTSTS)) & 0x01))
477 usleep_range(250, 500);
479 /* If the SMBus is still busy, we give up */
480 if (timeout == MAX_TIMEOUT) {
481 dev_err(&piix4_adapter->dev, "SMBus Timeout!\n");
487 dev_err(&piix4_adapter->dev, "Error: Failed bus transaction\n");
492 dev_dbg(&piix4_adapter->dev, "Bus collision! SMBus may be "
493 "locked until next hard reset. (sorry!)\n");
494 /* Clock stops and slave is stuck in mid-transmission */
499 dev_dbg(&piix4_adapter->dev, "Error: no response!\n");
502 if (inb_p(SMBHSTSTS) != 0x00)
503 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
505 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
506 dev_err(&piix4_adapter->dev, "Failed reset at end of "
507 "transaction (%02x)\n", temp);
509 dev_dbg(&piix4_adapter->dev, "Transaction (post): CNT=%02x, CMD=%02x, "
510 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
511 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
516 /* Return negative errno on error. */
517 static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
518 unsigned short flags, char read_write,
519 u8 command, int size, union i2c_smbus_data * data)
521 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
522 unsigned short piix4_smba = adapdata->smba;
527 case I2C_SMBUS_QUICK:
528 outb_p((addr << 1) | read_write,
533 outb_p((addr << 1) | read_write,
535 if (read_write == I2C_SMBUS_WRITE)
536 outb_p(command, SMBHSTCMD);
539 case I2C_SMBUS_BYTE_DATA:
540 outb_p((addr << 1) | read_write,
542 outb_p(command, SMBHSTCMD);
543 if (read_write == I2C_SMBUS_WRITE)
544 outb_p(data->byte, SMBHSTDAT0);
545 size = PIIX4_BYTE_DATA;
547 case I2C_SMBUS_WORD_DATA:
548 outb_p((addr << 1) | read_write,
550 outb_p(command, SMBHSTCMD);
551 if (read_write == I2C_SMBUS_WRITE) {
552 outb_p(data->word & 0xff, SMBHSTDAT0);
553 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
555 size = PIIX4_WORD_DATA;
557 case I2C_SMBUS_BLOCK_DATA:
558 outb_p((addr << 1) | read_write,
560 outb_p(command, SMBHSTCMD);
561 if (read_write == I2C_SMBUS_WRITE) {
562 len = data->block[0];
563 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
565 outb_p(len, SMBHSTDAT0);
566 inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
567 for (i = 1; i <= len; i++)
568 outb_p(data->block[i], SMBBLKDAT);
570 size = PIIX4_BLOCK_DATA;
573 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
577 outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
579 status = piix4_transaction(adap);
583 if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
589 case PIIX4_BYTE_DATA:
590 data->byte = inb_p(SMBHSTDAT0);
592 case PIIX4_WORD_DATA:
593 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
595 case PIIX4_BLOCK_DATA:
596 data->block[0] = inb_p(SMBHSTDAT0);
597 if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX)
599 inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
600 for (i = 1; i <= data->block[0]; i++)
601 data->block[i] = inb_p(SMBBLKDAT);
607 static uint8_t piix4_imc_read(uint8_t idx)
609 outb_p(idx, KERNCZ_IMC_IDX);
610 return inb_p(KERNCZ_IMC_DATA);
613 static void piix4_imc_write(uint8_t idx, uint8_t value)
615 outb_p(idx, KERNCZ_IMC_IDX);
616 outb_p(value, KERNCZ_IMC_DATA);
619 static int piix4_imc_sleep(void)
621 int timeout = MAX_TIMEOUT;
623 if (!request_muxed_region(KERNCZ_IMC_IDX, 2, "smbus_kerncz_imc"))
626 /* clear response register */
627 piix4_imc_write(0x82, 0x00);
628 /* request ownership flag */
629 piix4_imc_write(0x83, 0xB4);
630 /* kick off IMC Mailbox command 96 */
631 piix4_imc_write(0x80, 0x96);
634 if (piix4_imc_read(0x82) == 0xfa) {
635 release_region(KERNCZ_IMC_IDX, 2);
638 usleep_range(1000, 2000);
641 release_region(KERNCZ_IMC_IDX, 2);
645 static void piix4_imc_wakeup(void)
647 int timeout = MAX_TIMEOUT;
649 if (!request_muxed_region(KERNCZ_IMC_IDX, 2, "smbus_kerncz_imc"))
652 /* clear response register */
653 piix4_imc_write(0x82, 0x00);
654 /* release ownership flag */
655 piix4_imc_write(0x83, 0xB5);
656 /* kick off IMC Mailbox command 96 */
657 piix4_imc_write(0x80, 0x96);
660 if (piix4_imc_read(0x82) == 0xfa)
662 usleep_range(1000, 2000);
665 release_region(KERNCZ_IMC_IDX, 2);
669 * Handles access to multiple SMBus ports on the SB800.
670 * The port is selected by bits 2:1 of the smb_en register (0x2c).
671 * Returns negative errno on error.
673 * Note: The selected port must be returned to the initial selection to avoid
674 * problems on certain systems.
676 static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr,
677 unsigned short flags, char read_write,
678 u8 command, int size, union i2c_smbus_data *data)
680 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
681 unsigned short piix4_smba = adapdata->smba;
682 int retries = MAX_TIMEOUT;
688 if (!request_muxed_region(SB800_PIIX4_SMB_IDX, 2, "sb800_piix4_smb"))
691 /* Request the SMBUS semaphore, avoid conflicts with the IMC */
692 smbslvcnt = inb_p(SMBSLVCNT);
694 outb_p(smbslvcnt | 0x10, SMBSLVCNT);
696 /* Check the semaphore status */
697 smbslvcnt = inb_p(SMBSLVCNT);
698 if (smbslvcnt & 0x10)
701 usleep_range(1000, 2000);
703 /* SMBus is still owned by the IMC, we give up */
710 * Notify the IMC (Integrated Micro Controller) if required.
711 * Among other responsibilities, the IMC is in charge of monitoring
712 * the System fans and temperature sensors, and act accordingly.
713 * All this is done through SMBus and can/will collide
714 * with our transactions if they are long (BLOCK_DATA).
715 * Therefore we need to request the ownership flag during those
718 if ((size == I2C_SMBUS_BLOCK_DATA) && adapdata->notify_imc) {
721 ret = piix4_imc_sleep();
725 "IMC base address index region 0x%x already in use.\n",
730 "Failed to communicate with the IMC.\n");
736 /* If IMC communication fails do not retry */
739 "Continuing without IMC notification.\n");
740 adapdata->notify_imc = false;
744 outb_p(piix4_port_sel_sb800, SB800_PIIX4_SMB_IDX);
745 smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
747 port = adapdata->port;
748 if ((smba_en_lo & piix4_port_mask_sb800) != port)
749 outb_p((smba_en_lo & ~piix4_port_mask_sb800) | port,
750 SB800_PIIX4_SMB_IDX + 1);
752 retval = piix4_access(adap, addr, flags, read_write,
753 command, size, data);
755 outb_p(smba_en_lo, SB800_PIIX4_SMB_IDX + 1);
757 /* Release the semaphore */
758 outb_p(smbslvcnt | 0x20, SMBSLVCNT);
760 if ((size == I2C_SMBUS_BLOCK_DATA) && adapdata->notify_imc)
764 release_region(SB800_PIIX4_SMB_IDX, 2);
768 static u32 piix4_func(struct i2c_adapter *adapter)
770 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
771 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
772 I2C_FUNC_SMBUS_BLOCK_DATA;
775 static const struct i2c_algorithm smbus_algorithm = {
776 .smbus_xfer = piix4_access,
777 .functionality = piix4_func,
780 static const struct i2c_algorithm piix4_smbus_algorithm_sb800 = {
781 .smbus_xfer = piix4_access_sb800,
782 .functionality = piix4_func,
785 static const struct pci_device_id piix4_ids[] = {
786 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
787 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3) },
788 { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3) },
789 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_SMBUS) },
790 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_SMBUS) },
791 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS) },
792 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS) },
793 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) },
794 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) },
795 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
796 PCI_DEVICE_ID_SERVERWORKS_OSB4) },
797 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
798 PCI_DEVICE_ID_SERVERWORKS_CSB5) },
799 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
800 PCI_DEVICE_ID_SERVERWORKS_CSB6) },
801 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
802 PCI_DEVICE_ID_SERVERWORKS_HT1000SB) },
803 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
804 PCI_DEVICE_ID_SERVERWORKS_HT1100LD) },
808 MODULE_DEVICE_TABLE (pci, piix4_ids);
810 static struct i2c_adapter *piix4_main_adapters[PIIX4_MAX_ADAPTERS];
811 static struct i2c_adapter *piix4_aux_adapter;
813 static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
814 bool sb800_main, u8 port, bool notify_imc,
815 const char *name, struct i2c_adapter **padap)
817 struct i2c_adapter *adap;
818 struct i2c_piix4_adapdata *adapdata;
821 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
823 release_region(smba, SMBIOSIZE);
827 adap->owner = THIS_MODULE;
828 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
829 adap->algo = sb800_main ? &piix4_smbus_algorithm_sb800
832 adapdata = kzalloc(sizeof(*adapdata), GFP_KERNEL);
833 if (adapdata == NULL) {
835 release_region(smba, SMBIOSIZE);
839 adapdata->smba = smba;
840 adapdata->sb800_main = sb800_main;
841 adapdata->port = port << piix4_port_shift_sb800;
842 adapdata->notify_imc = notify_imc;
844 /* set up the sysfs linkage to our parent device */
845 adap->dev.parent = &dev->dev;
847 snprintf(adap->name, sizeof(adap->name),
848 "SMBus PIIX4 adapter%s at %04x", name, smba);
850 i2c_set_adapdata(adap, adapdata);
852 retval = i2c_add_adapter(adap);
856 release_region(smba, SMBIOSIZE);
864 static int piix4_add_adapters_sb800(struct pci_dev *dev, unsigned short smba,
867 struct i2c_piix4_adapdata *adapdata;
871 for (port = 0; port < PIIX4_MAX_ADAPTERS; port++) {
872 retval = piix4_add_adapter(dev, smba, true, port, notify_imc,
873 piix4_main_port_names_sb800[port],
874 &piix4_main_adapters[port]);
883 "Error setting up SB800 adapters. Unregistering!\n");
884 while (--port >= 0) {
885 adapdata = i2c_get_adapdata(piix4_main_adapters[port]);
886 if (adapdata->smba) {
887 i2c_del_adapter(piix4_main_adapters[port]);
889 kfree(piix4_main_adapters[port]);
890 piix4_main_adapters[port] = NULL;
897 static int piix4_probe(struct pci_dev *dev, const struct pci_device_id *id)
900 bool is_sb800 = false;
902 if ((dev->vendor == PCI_VENDOR_ID_ATI &&
903 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS &&
904 dev->revision >= 0x40) ||
905 dev->vendor == PCI_VENDOR_ID_AMD) {
906 bool notify_imc = false;
909 if (dev->vendor == PCI_VENDOR_ID_AMD &&
910 dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) {
914 * Detect if IMC is active or not, this method is
915 * described on coreboot's AMD IMC notes
917 pci_bus_read_config_byte(dev->bus, PCI_DEVFN(0x14, 3),
923 /* base address location etc changed in SB800 */
924 retval = piix4_setup_sb800(dev, id, 0);
929 * Try to register multiplexed main SMBus adapter,
930 * give up if we can't
932 retval = piix4_add_adapters_sb800(dev, retval, notify_imc);
936 retval = piix4_setup(dev, id);
940 /* Try to register main SMBus adapter, give up if we can't */
941 retval = piix4_add_adapter(dev, retval, false, 0, false, "",
942 &piix4_main_adapters[0]);
947 /* Check for auxiliary SMBus on some AMD chipsets */
950 if (dev->vendor == PCI_VENDOR_ID_ATI &&
951 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS) {
952 if (dev->revision < 0x40) {
953 retval = piix4_setup_aux(dev, id, 0x58);
955 /* SB800 added aux bus too */
956 retval = piix4_setup_sb800(dev, id, 1);
960 if (dev->vendor == PCI_VENDOR_ID_AMD &&
961 (dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS ||
962 dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS)) {
963 retval = piix4_setup_sb800(dev, id, 1);
967 /* Try to add the aux adapter if it exists,
968 * piix4_add_adapter will clean up if this fails */
969 piix4_add_adapter(dev, retval, false, 0, false,
970 is_sb800 ? piix4_aux_port_name_sb800 : "",
977 static void piix4_adap_remove(struct i2c_adapter *adap)
979 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
981 if (adapdata->smba) {
982 i2c_del_adapter(adap);
983 if (adapdata->port == (0 << piix4_port_shift_sb800))
984 release_region(adapdata->smba, SMBIOSIZE);
990 static void piix4_remove(struct pci_dev *dev)
992 int port = PIIX4_MAX_ADAPTERS;
994 while (--port >= 0) {
995 if (piix4_main_adapters[port]) {
996 piix4_adap_remove(piix4_main_adapters[port]);
997 piix4_main_adapters[port] = NULL;
1001 if (piix4_aux_adapter) {
1002 piix4_adap_remove(piix4_aux_adapter);
1003 piix4_aux_adapter = NULL;
1007 static struct pci_driver piix4_driver = {
1008 .name = "piix4_smbus",
1009 .id_table = piix4_ids,
1010 .probe = piix4_probe,
1011 .remove = piix4_remove,
1014 module_pci_driver(piix4_driver);
1016 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
1017 "Philip Edelbrock <phil@netroedge.com>");
1018 MODULE_DESCRIPTION("PIIX4 SMBus driver");
1019 MODULE_LICENSE("GPL");