1 // SPDX-License-Identifier: GPL-2.0-or-later
11 Serverworks OSB4, CSB5, CSB6, HT-1000, HT-1100
12 ATI IXP200, IXP300, IXP400, SB600, SB700/SP5100, SB800
17 Note: we assume there can only be one device, with one or more
19 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.
24 #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>
30 #include <linux/ioport.h>
31 #include <linux/i2c.h>
32 #include <linux/i2c-smbus.h>
33 #include <linux/slab.h>
34 #include <linux/dmi.h>
35 #include <linux/acpi.h>
39 /* PIIX4 SMBus address offsets */
40 #define SMBHSTSTS (0 + piix4_smba)
41 #define SMBHSLVSTS (1 + piix4_smba)
42 #define SMBHSTCNT (2 + piix4_smba)
43 #define SMBHSTCMD (3 + piix4_smba)
44 #define SMBHSTADD (4 + piix4_smba)
45 #define SMBHSTDAT0 (5 + piix4_smba)
46 #define SMBHSTDAT1 (6 + piix4_smba)
47 #define SMBBLKDAT (7 + piix4_smba)
48 #define SMBSLVCNT (8 + piix4_smba)
49 #define SMBSHDWCMD (9 + piix4_smba)
50 #define SMBSLVEVT (0xA + piix4_smba)
51 #define SMBSLVDAT (0xC + piix4_smba)
53 /* count for request_region */
56 /* PCI Address Constants */
58 #define SMBHSTCFG 0x0D2
60 #define SMBSHDW1 0x0D4
61 #define SMBSHDW2 0x0D5
65 #define MAX_TIMEOUT 500
69 #define PIIX4_QUICK 0x00
70 #define PIIX4_BYTE 0x04
71 #define PIIX4_BYTE_DATA 0x08
72 #define PIIX4_WORD_DATA 0x0C
73 #define PIIX4_BLOCK_DATA 0x14
75 /* Multi-port constants */
76 #define PIIX4_MAX_ADAPTERS 4
77 #define HUDSON2_MAIN_PORTS 2 /* HUDSON2, KERNCZ reserves ports 3, 4 */
80 #define SB800_PIIX4_SMB_IDX 0xcd6
81 #define SB800_PIIX4_SMB_MAP_SIZE 2
83 #define KERNCZ_IMC_IDX 0x3e
84 #define KERNCZ_IMC_DATA 0x3f
87 * SB800 port is selected by bits 2:1 of the smb_en register (0x2c)
88 * or the smb_sel register (0x2e), depending on bit 0 of register 0x2f.
89 * Hudson-2/Bolton port is always selected by bits 2:1 of register 0x2f.
91 #define SB800_PIIX4_PORT_IDX 0x2c
92 #define SB800_PIIX4_PORT_IDX_ALT 0x2e
93 #define SB800_PIIX4_PORT_IDX_SEL 0x2f
94 #define SB800_PIIX4_PORT_IDX_MASK 0x06
95 #define SB800_PIIX4_PORT_IDX_SHIFT 1
97 /* On kerncz and Hudson2, SmBus0Sel is at bit 20:19 of PMx00 DecodeEn */
98 #define SB800_PIIX4_PORT_IDX_KERNCZ 0x02
99 #define SB800_PIIX4_PORT_IDX_MASK_KERNCZ 0x18
100 #define SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ 3
102 #define SB800_PIIX4_FCH_PM_ADDR 0xFED80300
103 #define SB800_PIIX4_FCH_PM_SIZE 8
105 /* insmod parameters */
107 /* If force is set to anything different from 0, we forcibly enable the
110 module_param (force, int, 0);
111 MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!");
113 /* If force_addr is set to anything different from 0, we forcibly enable
114 the PIIX4 at the given address. VERY DANGEROUS! */
115 static int force_addr;
116 module_param_hw(force_addr, int, ioport, 0);
117 MODULE_PARM_DESC(force_addr,
118 "Forcibly enable the PIIX4 at the given address. "
119 "EXTREMELY DANGEROUS!");
121 static int srvrworks_csb5_delay;
122 static struct pci_driver piix4_driver;
124 static const struct dmi_system_id piix4_dmi_blacklist[] = {
126 .ident = "Sapphire AM2RD790",
128 DMI_MATCH(DMI_BOARD_VENDOR, "SAPPHIRE Inc."),
129 DMI_MATCH(DMI_BOARD_NAME, "PC-AM2RD790"),
133 .ident = "DFI Lanparty UT 790FX",
135 DMI_MATCH(DMI_BOARD_VENDOR, "DFI Inc."),
136 DMI_MATCH(DMI_BOARD_NAME, "LP UT 790FX"),
142 /* The IBM entry is in a separate table because we only check it
143 on Intel-based systems */
144 static const struct dmi_system_id piix4_dmi_ibm[] = {
147 .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), },
155 static u8 piix4_port_sel_sb800;
156 static u8 piix4_port_mask_sb800;
157 static u8 piix4_port_shift_sb800;
158 static const char *piix4_main_port_names_sb800[PIIX4_MAX_ADAPTERS] = {
159 " port 0", " port 2", " port 3", " port 4"
161 static const char *piix4_aux_port_name_sb800 = " port 1";
163 struct sb800_mmio_cfg {
168 struct i2c_piix4_adapdata {
174 u8 port; /* Port number, shifted */
175 struct sb800_mmio_cfg mmio_cfg;
178 static int piix4_sb800_region_request(struct device *dev,
179 struct sb800_mmio_cfg *mmio_cfg)
181 if (mmio_cfg->use_mmio) {
184 if (!request_mem_region_muxed(SB800_PIIX4_FCH_PM_ADDR,
185 SB800_PIIX4_FCH_PM_SIZE,
186 "sb800_piix4_smb")) {
188 "SMBus base address memory region 0x%x already in use.\n",
189 SB800_PIIX4_FCH_PM_ADDR);
193 addr = ioremap(SB800_PIIX4_FCH_PM_ADDR,
194 SB800_PIIX4_FCH_PM_SIZE);
196 release_mem_region(SB800_PIIX4_FCH_PM_ADDR,
197 SB800_PIIX4_FCH_PM_SIZE);
198 dev_err(dev, "SMBus base address mapping failed.\n");
202 mmio_cfg->addr = addr;
207 if (!request_muxed_region(SB800_PIIX4_SMB_IDX, SB800_PIIX4_SMB_MAP_SIZE,
208 "sb800_piix4_smb")) {
210 "SMBus base address index region 0x%x already in use.\n",
211 SB800_PIIX4_SMB_IDX);
218 static void piix4_sb800_region_release(struct device *dev,
219 struct sb800_mmio_cfg *mmio_cfg)
221 if (mmio_cfg->use_mmio) {
222 iounmap(mmio_cfg->addr);
223 release_mem_region(SB800_PIIX4_FCH_PM_ADDR,
224 SB800_PIIX4_FCH_PM_SIZE);
228 release_region(SB800_PIIX4_SMB_IDX, SB800_PIIX4_SMB_MAP_SIZE);
231 static bool piix4_sb800_use_mmio(struct pci_dev *PIIX4_dev)
234 * cd6h/cd7h port I/O accesses can be disabled on AMD processors
235 * w/ SMBus PCI revision ID 0x51 or greater. MMIO is supported on
236 * the same processors and is the recommended access method.
238 return (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
239 PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS &&
240 PIIX4_dev->revision >= 0x51);
243 static int piix4_setup(struct pci_dev *PIIX4_dev,
244 const struct pci_device_id *id)
247 unsigned short piix4_smba;
249 if ((PIIX4_dev->vendor == PCI_VENDOR_ID_SERVERWORKS) &&
250 (PIIX4_dev->device == PCI_DEVICE_ID_SERVERWORKS_CSB5))
251 srvrworks_csb5_delay = 1;
253 /* On some motherboards, it was reported that accessing the SMBus
254 caused severe hardware problems */
255 if (dmi_check_system(piix4_dmi_blacklist)) {
256 dev_err(&PIIX4_dev->dev,
257 "Accessing the SMBus on this system is unsafe!\n");
261 /* Don't access SMBus on IBM systems which get corrupted eeproms */
262 if (dmi_check_system(piix4_dmi_ibm) &&
263 PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) {
264 dev_err(&PIIX4_dev->dev, "IBM system detected; this module "
265 "may corrupt your serial eeprom! Refusing to load "
270 /* Determine the address of the SMBus areas */
272 piix4_smba = force_addr & 0xfff0;
275 pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba);
276 piix4_smba &= 0xfff0;
277 if(piix4_smba == 0) {
278 dev_err(&PIIX4_dev->dev, "SMBus base address "
279 "uninitialized - upgrade BIOS or use "
280 "force_addr=0xaddr\n");
285 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
288 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
289 dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
294 pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp);
296 /* If force_addr is set, we program the new address here. Just to make
297 sure, we disable the PIIX4 first. */
299 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe);
300 pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba);
301 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01);
302 dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to "
303 "new address %04x!\n", piix4_smba);
304 } else if ((temp & 1) == 0) {
306 /* This should never need to be done, but has been
307 * noted that many Dell machines have the SMBus
308 * interface on the PIIX4 disabled!? NOTE: This assumes
309 * I/O space and other allocations WERE done by the
310 * Bios! Don't complain if your hardware does weird
311 * things after enabling this. :') Check for Bios
312 * updates before resorting to this.
314 pci_write_config_byte(PIIX4_dev, SMBHSTCFG,
316 dev_notice(&PIIX4_dev->dev,
317 "WARNING: SMBus interface has been FORCEFULLY ENABLED!\n");
319 dev_err(&PIIX4_dev->dev,
320 "SMBus Host Controller not enabled!\n");
321 release_region(piix4_smba, SMBIOSIZE);
326 if (((temp & 0x0E) == 8) || ((temp & 0x0E) == 2))
327 dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
328 else if ((temp & 0x0E) == 0)
329 dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
331 dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration "
332 "(or code out of date)!\n");
334 pci_read_config_byte(PIIX4_dev, SMBREV, &temp);
335 dev_info(&PIIX4_dev->dev,
336 "SMBus Host Controller at 0x%x, revision %d\n",
342 static int piix4_setup_sb800_smba(struct pci_dev *PIIX4_dev,
346 unsigned short *piix4_smba)
348 struct sb800_mmio_cfg mmio_cfg;
353 mmio_cfg.use_mmio = piix4_sb800_use_mmio(PIIX4_dev);
354 retval = piix4_sb800_region_request(&PIIX4_dev->dev, &mmio_cfg);
358 if (mmio_cfg.use_mmio) {
359 smba_en_lo = ioread8(mmio_cfg.addr);
360 smba_en_hi = ioread8(mmio_cfg.addr + 1);
362 outb_p(smb_en, SB800_PIIX4_SMB_IDX);
363 smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
364 outb_p(smb_en + 1, SB800_PIIX4_SMB_IDX);
365 smba_en_hi = inb_p(SB800_PIIX4_SMB_IDX + 1);
368 piix4_sb800_region_release(&PIIX4_dev->dev, &mmio_cfg);
371 *smb_en_status = smba_en_lo & 0x10;
372 *piix4_smba = smba_en_hi << 8;
376 *smb_en_status = smba_en_lo & 0x01;
377 *piix4_smba = ((smba_en_hi << 8) | smba_en_lo) & 0xffe0;
380 if (!*smb_en_status) {
381 dev_err(&PIIX4_dev->dev,
382 "SMBus Host Controller not enabled!\n");
389 static int piix4_setup_sb800(struct pci_dev *PIIX4_dev,
390 const struct pci_device_id *id, u8 aux)
392 unsigned short piix4_smba;
393 u8 smb_en, smb_en_status, port_sel;
394 u8 i2ccfg, i2ccfg_offset = 0x10;
395 struct sb800_mmio_cfg mmio_cfg;
398 /* SB800 and later SMBus does not support forcing address */
399 if (force || force_addr) {
400 dev_err(&PIIX4_dev->dev, "SMBus does not support "
401 "forcing address!\n");
405 /* Determine the address of the SMBus areas */
406 if ((PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
407 PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
408 PIIX4_dev->revision >= 0x41) ||
409 (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
410 PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS &&
411 PIIX4_dev->revision >= 0x49) ||
412 (PIIX4_dev->vendor == PCI_VENDOR_ID_HYGON &&
413 PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS))
416 smb_en = (aux) ? 0x28 : 0x2c;
418 retval = piix4_setup_sb800_smba(PIIX4_dev, smb_en, aux, &smb_en_status,
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, "SMBus region 0x%x already in use!\n",
433 /* Aux SMBus does not support IRQ information */
435 dev_info(&PIIX4_dev->dev,
436 "Auxiliary SMBus Host Controller at 0x%x\n",
441 /* Request the SMBus I2C bus config region */
442 if (!request_region(piix4_smba + i2ccfg_offset, 1, "i2ccfg")) {
443 dev_err(&PIIX4_dev->dev, "SMBus I2C bus config region "
444 "0x%x already in use!\n", piix4_smba + i2ccfg_offset);
445 release_region(piix4_smba, SMBIOSIZE);
448 i2ccfg = inb_p(piix4_smba + i2ccfg_offset);
449 release_region(piix4_smba + i2ccfg_offset, 1);
452 dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
454 dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
456 dev_info(&PIIX4_dev->dev,
457 "SMBus Host Controller at 0x%x, revision %d\n",
458 piix4_smba, i2ccfg >> 4);
460 /* Find which register is used for port selection */
461 if (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD ||
462 PIIX4_dev->vendor == PCI_VENDOR_ID_HYGON) {
463 if (PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS ||
464 (PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
465 PIIX4_dev->revision >= 0x1F)) {
466 piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_KERNCZ;
467 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK_KERNCZ;
468 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ;
470 piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_ALT;
471 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
472 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
475 mmio_cfg.use_mmio = piix4_sb800_use_mmio(PIIX4_dev);
476 retval = piix4_sb800_region_request(&PIIX4_dev->dev, &mmio_cfg);
478 release_region(piix4_smba, SMBIOSIZE);
482 outb_p(SB800_PIIX4_PORT_IDX_SEL, SB800_PIIX4_SMB_IDX);
483 port_sel = inb_p(SB800_PIIX4_SMB_IDX + 1);
484 piix4_port_sel_sb800 = (port_sel & 0x01) ?
485 SB800_PIIX4_PORT_IDX_ALT :
486 SB800_PIIX4_PORT_IDX;
487 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
488 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
489 piix4_sb800_region_release(&PIIX4_dev->dev, &mmio_cfg);
492 dev_info(&PIIX4_dev->dev,
493 "Using register 0x%02x for SMBus port selection\n",
494 (unsigned int)piix4_port_sel_sb800);
499 static int piix4_setup_aux(struct pci_dev *PIIX4_dev,
500 const struct pci_device_id *id,
501 unsigned short base_reg_addr)
503 /* Set up auxiliary SMBus controllers found on some
504 * AMD chipsets e.g. SP5100 (SB700 derivative) */
506 unsigned short piix4_smba;
508 /* Read address of auxiliary SMBus controller */
509 pci_read_config_word(PIIX4_dev, base_reg_addr, &piix4_smba);
510 if ((piix4_smba & 1) == 0) {
511 dev_dbg(&PIIX4_dev->dev,
512 "Auxiliary SMBus controller not enabled\n");
516 piix4_smba &= 0xfff0;
517 if (piix4_smba == 0) {
518 dev_dbg(&PIIX4_dev->dev,
519 "Auxiliary SMBus base address uninitialized\n");
523 if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
526 if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
527 dev_err(&PIIX4_dev->dev, "Auxiliary SMBus region 0x%x "
528 "already in use!\n", piix4_smba);
532 dev_info(&PIIX4_dev->dev,
533 "Auxiliary SMBus Host Controller at 0x%x\n",
539 static int piix4_transaction(struct i2c_adapter *piix4_adapter)
541 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(piix4_adapter);
542 unsigned short piix4_smba = adapdata->smba;
547 dev_dbg(&piix4_adapter->dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
548 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
549 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
552 /* Make sure the SMBus host is ready to start transmitting */
553 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
554 dev_dbg(&piix4_adapter->dev, "SMBus busy (%02x). "
555 "Resetting...\n", temp);
556 outb_p(temp, SMBHSTSTS);
557 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
558 dev_err(&piix4_adapter->dev, "Failed! (%02x)\n", temp);
561 dev_dbg(&piix4_adapter->dev, "Successful!\n");
565 /* start the transaction by setting bit 6 */
566 outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
568 /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
569 if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */
570 usleep_range(2000, 2100);
572 usleep_range(250, 500);
574 while ((++timeout < MAX_TIMEOUT) &&
575 ((temp = inb_p(SMBHSTSTS)) & 0x01))
576 usleep_range(250, 500);
578 /* If the SMBus is still busy, we give up */
579 if (timeout == MAX_TIMEOUT) {
580 dev_err(&piix4_adapter->dev, "SMBus Timeout!\n");
586 dev_err(&piix4_adapter->dev, "Error: Failed bus transaction\n");
591 dev_dbg(&piix4_adapter->dev, "Bus collision! SMBus may be "
592 "locked until next hard reset. (sorry!)\n");
593 /* Clock stops and target is stuck in mid-transmission */
598 dev_dbg(&piix4_adapter->dev, "Error: no response!\n");
601 if (inb_p(SMBHSTSTS) != 0x00)
602 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
604 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
605 dev_err(&piix4_adapter->dev, "Failed reset at end of "
606 "transaction (%02x)\n", temp);
608 dev_dbg(&piix4_adapter->dev, "Transaction (post): CNT=%02x, CMD=%02x, "
609 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
610 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
615 /* Return negative errno on error. */
616 static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
617 unsigned short flags, char read_write,
618 u8 command, int size, union i2c_smbus_data * data)
620 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
621 unsigned short piix4_smba = adapdata->smba;
626 case I2C_SMBUS_QUICK:
627 outb_p((addr << 1) | read_write,
632 outb_p((addr << 1) | read_write,
634 if (read_write == I2C_SMBUS_WRITE)
635 outb_p(command, SMBHSTCMD);
638 case I2C_SMBUS_BYTE_DATA:
639 outb_p((addr << 1) | read_write,
641 outb_p(command, SMBHSTCMD);
642 if (read_write == I2C_SMBUS_WRITE)
643 outb_p(data->byte, SMBHSTDAT0);
644 size = PIIX4_BYTE_DATA;
646 case I2C_SMBUS_WORD_DATA:
647 outb_p((addr << 1) | read_write,
649 outb_p(command, SMBHSTCMD);
650 if (read_write == I2C_SMBUS_WRITE) {
651 outb_p(data->word & 0xff, SMBHSTDAT0);
652 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
654 size = PIIX4_WORD_DATA;
656 case I2C_SMBUS_BLOCK_DATA:
657 outb_p((addr << 1) | read_write,
659 outb_p(command, SMBHSTCMD);
660 if (read_write == I2C_SMBUS_WRITE) {
661 len = data->block[0];
662 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
664 outb_p(len, SMBHSTDAT0);
665 inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
666 for (i = 1; i <= len; i++)
667 outb_p(data->block[i], SMBBLKDAT);
669 size = PIIX4_BLOCK_DATA;
672 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
676 outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
678 status = piix4_transaction(adap);
682 if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
688 case PIIX4_BYTE_DATA:
689 data->byte = inb_p(SMBHSTDAT0);
691 case PIIX4_WORD_DATA:
692 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
694 case PIIX4_BLOCK_DATA:
695 data->block[0] = inb_p(SMBHSTDAT0);
696 if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX)
698 inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
699 for (i = 1; i <= data->block[0]; i++)
700 data->block[i] = inb_p(SMBBLKDAT);
706 static uint8_t piix4_imc_read(uint8_t idx)
708 outb_p(idx, KERNCZ_IMC_IDX);
709 return inb_p(KERNCZ_IMC_DATA);
712 static void piix4_imc_write(uint8_t idx, uint8_t value)
714 outb_p(idx, KERNCZ_IMC_IDX);
715 outb_p(value, KERNCZ_IMC_DATA);
718 static int piix4_imc_sleep(void)
720 int timeout = MAX_TIMEOUT;
722 if (!request_muxed_region(KERNCZ_IMC_IDX, 2, "smbus_kerncz_imc"))
725 /* clear response register */
726 piix4_imc_write(0x82, 0x00);
727 /* request ownership flag */
728 piix4_imc_write(0x83, 0xB4);
729 /* kick off IMC Mailbox command 96 */
730 piix4_imc_write(0x80, 0x96);
733 if (piix4_imc_read(0x82) == 0xfa) {
734 release_region(KERNCZ_IMC_IDX, 2);
737 usleep_range(1000, 2000);
740 release_region(KERNCZ_IMC_IDX, 2);
744 static void piix4_imc_wakeup(void)
746 int timeout = MAX_TIMEOUT;
748 if (!request_muxed_region(KERNCZ_IMC_IDX, 2, "smbus_kerncz_imc"))
751 /* clear response register */
752 piix4_imc_write(0x82, 0x00);
753 /* release ownership flag */
754 piix4_imc_write(0x83, 0xB5);
755 /* kick off IMC Mailbox command 96 */
756 piix4_imc_write(0x80, 0x96);
759 if (piix4_imc_read(0x82) == 0xfa)
761 usleep_range(1000, 2000);
764 release_region(KERNCZ_IMC_IDX, 2);
767 static int piix4_sb800_port_sel(u8 port, struct sb800_mmio_cfg *mmio_cfg)
771 if (mmio_cfg->use_mmio) {
772 smba_en_lo = ioread8(mmio_cfg->addr + piix4_port_sel_sb800);
773 val = (smba_en_lo & ~piix4_port_mask_sb800) | port;
774 if (smba_en_lo != val)
775 iowrite8(val, mmio_cfg->addr + piix4_port_sel_sb800);
777 return (smba_en_lo & piix4_port_mask_sb800);
780 outb_p(piix4_port_sel_sb800, SB800_PIIX4_SMB_IDX);
781 smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
783 val = (smba_en_lo & ~piix4_port_mask_sb800) | port;
784 if (smba_en_lo != val)
785 outb_p(val, SB800_PIIX4_SMB_IDX + 1);
787 return (smba_en_lo & piix4_port_mask_sb800);
791 * Handles access to multiple SMBus ports on the SB800.
792 * The port is selected by bits 2:1 of the smb_en register (0x2c).
793 * Returns negative errno on error.
795 * Note: The selected port must be returned to the initial selection to avoid
796 * problems on certain systems.
798 static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr,
799 unsigned short flags, char read_write,
800 u8 command, int size, union i2c_smbus_data *data)
802 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
803 unsigned short piix4_smba = adapdata->smba;
804 int retries = MAX_TIMEOUT;
809 retval = piix4_sb800_region_request(&adap->dev, &adapdata->mmio_cfg);
813 /* Request the SMBUS semaphore, avoid conflicts with the IMC */
814 smbslvcnt = inb_p(SMBSLVCNT);
816 outb_p(smbslvcnt | 0x10, SMBSLVCNT);
818 /* Check the semaphore status */
819 smbslvcnt = inb_p(SMBSLVCNT);
820 if (smbslvcnt & 0x10)
823 usleep_range(1000, 2000);
825 /* SMBus is still owned by the IMC, we give up */
832 * Notify the IMC (Integrated Micro Controller) if required.
833 * Among other responsibilities, the IMC is in charge of monitoring
834 * the System fans and temperature sensors, and act accordingly.
835 * All this is done through SMBus and can/will collide
836 * with our transactions if they are long (BLOCK_DATA).
837 * Therefore we need to request the ownership flag during those
840 if ((size == I2C_SMBUS_BLOCK_DATA) && adapdata->notify_imc) {
843 ret = piix4_imc_sleep();
847 "IMC base address index region 0x%x already in use.\n",
852 "Failed to communicate with the IMC.\n");
858 /* If IMC communication fails do not retry */
861 "Continuing without IMC notification.\n");
862 adapdata->notify_imc = false;
866 prev_port = piix4_sb800_port_sel(adapdata->port, &adapdata->mmio_cfg);
868 retval = piix4_access(adap, addr, flags, read_write,
869 command, size, data);
871 piix4_sb800_port_sel(prev_port, &adapdata->mmio_cfg);
873 /* Release the semaphore */
874 outb_p(smbslvcnt | 0x20, SMBSLVCNT);
876 if ((size == I2C_SMBUS_BLOCK_DATA) && adapdata->notify_imc)
880 piix4_sb800_region_release(&adap->dev, &adapdata->mmio_cfg);
884 static u32 piix4_func(struct i2c_adapter *adapter)
886 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
887 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
888 I2C_FUNC_SMBUS_BLOCK_DATA;
891 static const struct i2c_algorithm smbus_algorithm = {
892 .smbus_xfer = piix4_access,
893 .functionality = piix4_func,
896 static const struct i2c_algorithm piix4_smbus_algorithm_sb800 = {
897 .smbus_xfer = piix4_access_sb800,
898 .functionality = piix4_func,
901 static const struct pci_device_id piix4_ids[] = {
902 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
903 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3) },
904 { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3) },
905 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_SMBUS) },
906 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_SMBUS) },
907 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS) },
908 { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS) },
909 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) },
910 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) },
911 { PCI_DEVICE(PCI_VENDOR_ID_HYGON, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) },
912 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
913 PCI_DEVICE_ID_SERVERWORKS_OSB4) },
914 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
915 PCI_DEVICE_ID_SERVERWORKS_CSB5) },
916 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
917 PCI_DEVICE_ID_SERVERWORKS_CSB6) },
918 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
919 PCI_DEVICE_ID_SERVERWORKS_HT1000SB) },
920 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
921 PCI_DEVICE_ID_SERVERWORKS_HT1100LD) },
925 MODULE_DEVICE_TABLE (pci, piix4_ids);
927 static struct i2c_adapter *piix4_main_adapters[PIIX4_MAX_ADAPTERS];
928 static struct i2c_adapter *piix4_aux_adapter;
929 static int piix4_adapter_count;
931 static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
932 bool sb800_main, u8 port, bool notify_imc,
933 u8 hw_port_nr, const char *name,
934 struct i2c_adapter **padap)
936 struct i2c_adapter *adap;
937 struct i2c_piix4_adapdata *adapdata;
940 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
942 release_region(smba, SMBIOSIZE);
946 adap->owner = THIS_MODULE;
947 adap->class = I2C_CLASS_HWMON;
948 adap->algo = sb800_main ? &piix4_smbus_algorithm_sb800
951 adapdata = kzalloc(sizeof(*adapdata), GFP_KERNEL);
952 if (adapdata == NULL) {
954 release_region(smba, SMBIOSIZE);
958 adapdata->mmio_cfg.use_mmio = piix4_sb800_use_mmio(dev);
959 adapdata->smba = smba;
960 adapdata->sb800_main = sb800_main;
961 adapdata->port = port << piix4_port_shift_sb800;
962 adapdata->notify_imc = notify_imc;
964 /* set up the sysfs linkage to our parent device */
965 adap->dev.parent = &dev->dev;
967 if (has_acpi_companion(&dev->dev)) {
968 acpi_preset_companion(&adap->dev,
969 ACPI_COMPANION(&dev->dev),
973 snprintf(adap->name, sizeof(adap->name),
974 "SMBus PIIX4 adapter%s at %04x", name, smba);
976 i2c_set_adapdata(adap, adapdata);
978 retval = i2c_add_adapter(adap);
982 release_region(smba, SMBIOSIZE);
987 * The AUX bus can not be probed as on some platforms it reports all
988 * devices present and all reads return "0".
989 * This would allow the ee1004 to be probed incorrectly.
992 i2c_register_spd(adap);
998 static int piix4_add_adapters_sb800(struct pci_dev *dev, unsigned short smba,
1001 struct i2c_piix4_adapdata *adapdata;
1005 if (dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS ||
1006 (dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
1007 dev->revision >= 0x1F)) {
1008 piix4_adapter_count = HUDSON2_MAIN_PORTS;
1010 piix4_adapter_count = PIIX4_MAX_ADAPTERS;
1013 for (port = 0; port < piix4_adapter_count; port++) {
1014 u8 hw_port_nr = port == 0 ? 0 : port + 1;
1016 retval = piix4_add_adapter(dev, smba, true, port, notify_imc,
1018 piix4_main_port_names_sb800[port],
1019 &piix4_main_adapters[port]);
1028 "Error setting up SB800 adapters. Unregistering!\n");
1029 while (--port >= 0) {
1030 adapdata = i2c_get_adapdata(piix4_main_adapters[port]);
1031 if (adapdata->smba) {
1032 i2c_del_adapter(piix4_main_adapters[port]);
1034 kfree(piix4_main_adapters[port]);
1035 piix4_main_adapters[port] = NULL;
1042 static int piix4_probe(struct pci_dev *dev, const struct pci_device_id *id)
1045 bool is_sb800 = false;
1047 if ((dev->vendor == PCI_VENDOR_ID_ATI &&
1048 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS &&
1049 dev->revision >= 0x40) ||
1050 dev->vendor == PCI_VENDOR_ID_AMD ||
1051 dev->vendor == PCI_VENDOR_ID_HYGON) {
1052 bool notify_imc = false;
1055 if ((dev->vendor == PCI_VENDOR_ID_AMD ||
1056 dev->vendor == PCI_VENDOR_ID_HYGON) &&
1057 dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) {
1061 * Detect if IMC is active or not, this method is
1062 * described on coreboot's AMD IMC notes
1064 pci_bus_read_config_byte(dev->bus, PCI_DEVFN(0x14, 3),
1070 /* base address location etc changed in SB800 */
1071 retval = piix4_setup_sb800(dev, id, 0);
1076 * Try to register multiplexed main SMBus adapter,
1077 * give up if we can't
1079 retval = piix4_add_adapters_sb800(dev, retval, notify_imc);
1083 retval = piix4_setup(dev, id);
1087 /* Try to register main SMBus adapter, give up if we can't */
1088 retval = piix4_add_adapter(dev, retval, false, 0, false, 0,
1089 "", &piix4_main_adapters[0]);
1092 piix4_adapter_count = 1;
1095 /* Check for auxiliary SMBus on some AMD chipsets */
1098 if (dev->vendor == PCI_VENDOR_ID_ATI &&
1099 dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS) {
1100 if (dev->revision < 0x40) {
1101 retval = piix4_setup_aux(dev, id, 0x58);
1103 /* SB800 added aux bus too */
1104 retval = piix4_setup_sb800(dev, id, 1);
1108 if (dev->vendor == PCI_VENDOR_ID_AMD &&
1109 (dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS ||
1110 dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS)) {
1111 retval = piix4_setup_sb800(dev, id, 1);
1115 /* Try to add the aux adapter if it exists,
1116 * piix4_add_adapter will clean up if this fails */
1117 piix4_add_adapter(dev, retval, false, 0, false, 1,
1118 is_sb800 ? piix4_aux_port_name_sb800 : "",
1119 &piix4_aux_adapter);
1125 static void piix4_adap_remove(struct i2c_adapter *adap)
1127 struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
1129 if (adapdata->smba) {
1130 i2c_del_adapter(adap);
1131 if (adapdata->port == (0 << piix4_port_shift_sb800))
1132 release_region(adapdata->smba, SMBIOSIZE);
1138 static void piix4_remove(struct pci_dev *dev)
1140 int port = piix4_adapter_count;
1142 while (--port >= 0) {
1143 if (piix4_main_adapters[port]) {
1144 piix4_adap_remove(piix4_main_adapters[port]);
1145 piix4_main_adapters[port] = NULL;
1149 if (piix4_aux_adapter) {
1150 piix4_adap_remove(piix4_aux_adapter);
1151 piix4_aux_adapter = NULL;
1155 static struct pci_driver piix4_driver = {
1156 .name = "piix4_smbus",
1157 .id_table = piix4_ids,
1158 .probe = piix4_probe,
1159 .remove = piix4_remove,
1162 module_pci_driver(piix4_driver);
1166 MODULE_DESCRIPTION("PIIX4 SMBus driver");
1167 MODULE_LICENSE("GPL");