1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl> and
4 Philip Edelbrock <phil@netroedge.com>
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>
38 #include "i2c-piix4.h"
40 /* count for request_region */
43 /* PCI Address Constants */
45 #define SMBHSTCFG 0x0D2
47 #define SMBSHDW1 0x0D4
48 #define SMBSHDW2 0x0D5
52 #define MAX_TIMEOUT 500
56 #define PIIX4_QUICK 0x00
57 #define PIIX4_BYTE 0x04
58 #define PIIX4_BYTE_DATA 0x08
59 #define PIIX4_WORD_DATA 0x0C
61 /* Multi-port constants */
62 #define PIIX4_MAX_ADAPTERS 4
63 #define HUDSON2_MAIN_PORTS 2 /* HUDSON2, KERNCZ reserves ports 3, 4 */
66 #define SB800_PIIX4_SMB_IDX 0xcd6
67 #define SB800_PIIX4_SMB_MAP_SIZE 2
69 #define KERNCZ_IMC_IDX 0x3e
70 #define KERNCZ_IMC_DATA 0x3f
73 * SB800 port is selected by bits 2:1 of the smb_en register (0x2c)
74 * or the smb_sel register (0x2e), depending on bit 0 of register 0x2f.
75 * Hudson-2/Bolton port is always selected by bits 2:1 of register 0x2f.
77 #define SB800_PIIX4_PORT_IDX 0x2c
78 #define SB800_PIIX4_PORT_IDX_ALT 0x2e
79 #define SB800_PIIX4_PORT_IDX_SEL 0x2f
80 #define SB800_PIIX4_PORT_IDX_MASK 0x06
81 #define SB800_PIIX4_PORT_IDX_SHIFT 1
83 /* On kerncz and Hudson2, SmBus0Sel is at bit 20:19 of PMx00 DecodeEn */
84 #define SB800_PIIX4_PORT_IDX_KERNCZ 0x02
85 #define SB800_PIIX4_PORT_IDX_MASK_KERNCZ 0x18
86 #define SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ 3
88 #define SB800_PIIX4_FCH_PM_ADDR 0xFED80300
89 #define SB800_PIIX4_FCH_PM_SIZE 8
90 #define SB800_ASF_ACPI_PATH "\\_SB.ASFC"
92 /* insmod parameters */
94 /* If force is set to anything different from 0, we forcibly enable the
97 module_param (force
, int, 0);
98 MODULE_PARM_DESC(force
, "Forcibly enable the PIIX4. DANGEROUS!");
100 /* If force_addr is set to anything different from 0, we forcibly enable
101 the PIIX4 at the given address. VERY DANGEROUS! */
102 static int force_addr
;
103 module_param_hw(force_addr
, int, ioport
, 0);
104 MODULE_PARM_DESC(force_addr
,
105 "Forcibly enable the PIIX4 at the given address. "
106 "EXTREMELY DANGEROUS!");
108 static int srvrworks_csb5_delay
;
109 static struct pci_driver piix4_driver
;
111 static const struct dmi_system_id piix4_dmi_blacklist
[] = {
113 .ident
= "Sapphire AM2RD790",
115 DMI_MATCH(DMI_BOARD_VENDOR
, "SAPPHIRE Inc."),
116 DMI_MATCH(DMI_BOARD_NAME
, "PC-AM2RD790"),
120 .ident
= "DFI Lanparty UT 790FX",
122 DMI_MATCH(DMI_BOARD_VENDOR
, "DFI Inc."),
123 DMI_MATCH(DMI_BOARD_NAME
, "LP UT 790FX"),
129 /* The IBM entry is in a separate table because we only check it
130 on Intel-based systems */
131 static const struct dmi_system_id piix4_dmi_ibm
[] = {
134 .matches
= { DMI_MATCH(DMI_SYS_VENDOR
, "IBM"), },
142 static u8 piix4_port_sel_sb800
;
143 static u8 piix4_port_mask_sb800
;
144 static u8 piix4_port_shift_sb800
;
145 static const char *piix4_main_port_names_sb800
[PIIX4_MAX_ADAPTERS
] = {
146 " port 0", " port 2", " port 3", " port 4"
148 static const char *piix4_aux_port_name_sb800
= " port 1";
150 struct i2c_piix4_adapdata
{
156 u8 port
; /* Port number, shifted */
157 struct sb800_mmio_cfg mmio_cfg
;
160 int piix4_sb800_region_request(struct device
*dev
, struct sb800_mmio_cfg
*mmio_cfg
)
162 if (mmio_cfg
->use_mmio
) {
165 if (!request_mem_region_muxed(SB800_PIIX4_FCH_PM_ADDR
,
166 SB800_PIIX4_FCH_PM_SIZE
,
167 "sb800_piix4_smb")) {
169 "SMBus base address memory region 0x%x already in use.\n",
170 SB800_PIIX4_FCH_PM_ADDR
);
174 addr
= ioremap(SB800_PIIX4_FCH_PM_ADDR
,
175 SB800_PIIX4_FCH_PM_SIZE
);
177 release_mem_region(SB800_PIIX4_FCH_PM_ADDR
,
178 SB800_PIIX4_FCH_PM_SIZE
);
179 dev_err(dev
, "SMBus base address mapping failed.\n");
183 mmio_cfg
->addr
= addr
;
188 if (!request_muxed_region(SB800_PIIX4_SMB_IDX
, SB800_PIIX4_SMB_MAP_SIZE
,
189 "sb800_piix4_smb")) {
191 "SMBus base address index region 0x%x already in use.\n",
192 SB800_PIIX4_SMB_IDX
);
198 EXPORT_SYMBOL_NS_GPL(piix4_sb800_region_request
, "PIIX4_SMBUS");
200 void piix4_sb800_region_release(struct device
*dev
, struct sb800_mmio_cfg
*mmio_cfg
)
202 if (mmio_cfg
->use_mmio
) {
203 iounmap(mmio_cfg
->addr
);
204 release_mem_region(SB800_PIIX4_FCH_PM_ADDR
,
205 SB800_PIIX4_FCH_PM_SIZE
);
209 release_region(SB800_PIIX4_SMB_IDX
, SB800_PIIX4_SMB_MAP_SIZE
);
211 EXPORT_SYMBOL_NS_GPL(piix4_sb800_region_release
, "PIIX4_SMBUS");
213 static bool piix4_sb800_use_mmio(struct pci_dev
*PIIX4_dev
)
216 * cd6h/cd7h port I/O accesses can be disabled on AMD processors
217 * w/ SMBus PCI revision ID 0x51 or greater. MMIO is supported on
218 * the same processors and is the recommended access method.
220 return (PIIX4_dev
->vendor
== PCI_VENDOR_ID_AMD
&&
221 PIIX4_dev
->device
== PCI_DEVICE_ID_AMD_KERNCZ_SMBUS
&&
222 PIIX4_dev
->revision
>= 0x51);
225 static int piix4_setup(struct pci_dev
*PIIX4_dev
,
226 const struct pci_device_id
*id
)
229 unsigned short piix4_smba
;
231 if ((PIIX4_dev
->vendor
== PCI_VENDOR_ID_SERVERWORKS
) &&
232 (PIIX4_dev
->device
== PCI_DEVICE_ID_SERVERWORKS_CSB5
))
233 srvrworks_csb5_delay
= 1;
235 /* On some motherboards, it was reported that accessing the SMBus
236 caused severe hardware problems */
237 if (dmi_check_system(piix4_dmi_blacklist
)) {
238 dev_err(&PIIX4_dev
->dev
,
239 "Accessing the SMBus on this system is unsafe!\n");
243 /* Don't access SMBus on IBM systems which get corrupted eeproms */
244 if (dmi_check_system(piix4_dmi_ibm
) &&
245 PIIX4_dev
->vendor
== PCI_VENDOR_ID_INTEL
) {
246 dev_err(&PIIX4_dev
->dev
, "IBM system detected; this module "
247 "may corrupt your serial eeprom! Refusing to load "
252 /* Determine the address of the SMBus areas */
254 piix4_smba
= force_addr
& 0xfff0;
257 pci_read_config_word(PIIX4_dev
, SMBBA
, &piix4_smba
);
258 piix4_smba
&= 0xfff0;
259 if(piix4_smba
== 0) {
260 dev_err(&PIIX4_dev
->dev
, "SMBus base address "
261 "uninitialized - upgrade BIOS or use "
262 "force_addr=0xaddr\n");
267 if (acpi_check_region(piix4_smba
, SMBIOSIZE
, piix4_driver
.name
))
270 if (!request_region(piix4_smba
, SMBIOSIZE
, piix4_driver
.name
)) {
271 dev_err(&PIIX4_dev
->dev
, "SMBus region 0x%x already in use!\n",
276 pci_read_config_byte(PIIX4_dev
, SMBHSTCFG
, &temp
);
278 /* If force_addr is set, we program the new address here. Just to make
279 sure, we disable the PIIX4 first. */
281 pci_write_config_byte(PIIX4_dev
, SMBHSTCFG
, temp
& 0xfe);
282 pci_write_config_word(PIIX4_dev
, SMBBA
, piix4_smba
);
283 pci_write_config_byte(PIIX4_dev
, SMBHSTCFG
, temp
| 0x01);
284 dev_info(&PIIX4_dev
->dev
, "WARNING: SMBus interface set to "
285 "new address %04x!\n", piix4_smba
);
286 } else if ((temp
& 1) == 0) {
288 /* This should never need to be done, but has been
289 * noted that many Dell machines have the SMBus
290 * interface on the PIIX4 disabled!? NOTE: This assumes
291 * I/O space and other allocations WERE done by the
292 * Bios! Don't complain if your hardware does weird
293 * things after enabling this. :') Check for Bios
294 * updates before resorting to this.
296 pci_write_config_byte(PIIX4_dev
, SMBHSTCFG
,
298 dev_notice(&PIIX4_dev
->dev
,
299 "WARNING: SMBus interface has been FORCEFULLY ENABLED!\n");
301 dev_err(&PIIX4_dev
->dev
,
302 "SMBus Host Controller not enabled!\n");
303 release_region(piix4_smba
, SMBIOSIZE
);
308 if (((temp
& 0x0E) == 8) || ((temp
& 0x0E) == 2))
309 dev_dbg(&PIIX4_dev
->dev
, "Using IRQ for SMBus\n");
310 else if ((temp
& 0x0E) == 0)
311 dev_dbg(&PIIX4_dev
->dev
, "Using SMI# for SMBus\n");
313 dev_err(&PIIX4_dev
->dev
, "Illegal Interrupt configuration "
314 "(or code out of date)!\n");
316 pci_read_config_byte(PIIX4_dev
, SMBREV
, &temp
);
317 dev_info(&PIIX4_dev
->dev
,
318 "SMBus Host Controller at 0x%x, revision %d\n",
324 static int piix4_setup_sb800_smba(struct pci_dev
*PIIX4_dev
,
328 unsigned short *piix4_smba
)
330 struct sb800_mmio_cfg mmio_cfg
;
335 mmio_cfg
.use_mmio
= piix4_sb800_use_mmio(PIIX4_dev
);
336 retval
= piix4_sb800_region_request(&PIIX4_dev
->dev
, &mmio_cfg
);
340 if (mmio_cfg
.use_mmio
) {
341 smba_en_lo
= ioread8(mmio_cfg
.addr
);
342 smba_en_hi
= ioread8(mmio_cfg
.addr
+ 1);
344 outb_p(smb_en
, SB800_PIIX4_SMB_IDX
);
345 smba_en_lo
= inb_p(SB800_PIIX4_SMB_IDX
+ 1);
346 outb_p(smb_en
+ 1, SB800_PIIX4_SMB_IDX
);
347 smba_en_hi
= inb_p(SB800_PIIX4_SMB_IDX
+ 1);
350 piix4_sb800_region_release(&PIIX4_dev
->dev
, &mmio_cfg
);
353 *smb_en_status
= smba_en_lo
& 0x10;
354 *piix4_smba
= smba_en_hi
<< 8;
358 *smb_en_status
= smba_en_lo
& 0x01;
359 *piix4_smba
= ((smba_en_hi
<< 8) | smba_en_lo
) & 0xffe0;
362 if (!*smb_en_status
) {
363 dev_err(&PIIX4_dev
->dev
,
364 "SMBus Host Controller not enabled!\n");
371 static int piix4_setup_sb800(struct pci_dev
*PIIX4_dev
,
372 const struct pci_device_id
*id
, u8 aux
)
374 unsigned short piix4_smba
;
375 u8 smb_en
, smb_en_status
, port_sel
;
376 u8 i2ccfg
, i2ccfg_offset
= 0x10;
377 struct sb800_mmio_cfg mmio_cfg
;
380 /* SB800 and later SMBus does not support forcing address */
381 if (force
|| force_addr
) {
382 dev_err(&PIIX4_dev
->dev
, "SMBus does not support "
383 "forcing address!\n");
387 /* Determine the address of the SMBus areas */
388 if ((PIIX4_dev
->vendor
== PCI_VENDOR_ID_AMD
&&
389 PIIX4_dev
->device
== PCI_DEVICE_ID_AMD_HUDSON2_SMBUS
&&
390 PIIX4_dev
->revision
>= 0x41) ||
391 (PIIX4_dev
->vendor
== PCI_VENDOR_ID_AMD
&&
392 PIIX4_dev
->device
== PCI_DEVICE_ID_AMD_KERNCZ_SMBUS
&&
393 PIIX4_dev
->revision
>= 0x49) ||
394 (PIIX4_dev
->vendor
== PCI_VENDOR_ID_HYGON
&&
395 PIIX4_dev
->device
== PCI_DEVICE_ID_AMD_KERNCZ_SMBUS
))
398 smb_en
= (aux
) ? 0x28 : 0x2c;
400 retval
= piix4_setup_sb800_smba(PIIX4_dev
, smb_en
, aux
, &smb_en_status
,
406 if (acpi_check_region(piix4_smba
, SMBIOSIZE
, piix4_driver
.name
))
409 if (!request_region(piix4_smba
, SMBIOSIZE
, piix4_driver
.name
)) {
410 dev_err(&PIIX4_dev
->dev
, "SMBus region 0x%x already in use!\n",
415 /* Aux SMBus does not support IRQ information */
417 dev_info(&PIIX4_dev
->dev
,
418 "Auxiliary SMBus Host Controller at 0x%x\n",
423 /* Request the SMBus I2C bus config region */
424 if (!request_region(piix4_smba
+ i2ccfg_offset
, 1, "i2ccfg")) {
425 dev_err(&PIIX4_dev
->dev
, "SMBus I2C bus config region "
426 "0x%x already in use!\n", piix4_smba
+ i2ccfg_offset
);
427 release_region(piix4_smba
, SMBIOSIZE
);
430 i2ccfg
= inb_p(piix4_smba
+ i2ccfg_offset
);
431 release_region(piix4_smba
+ i2ccfg_offset
, 1);
434 dev_dbg(&PIIX4_dev
->dev
, "Using IRQ for SMBus\n");
436 dev_dbg(&PIIX4_dev
->dev
, "Using SMI# for SMBus\n");
438 dev_info(&PIIX4_dev
->dev
,
439 "SMBus Host Controller at 0x%x, revision %d\n",
440 piix4_smba
, i2ccfg
>> 4);
442 /* Find which register is used for port selection */
443 if (PIIX4_dev
->vendor
== PCI_VENDOR_ID_AMD
||
444 PIIX4_dev
->vendor
== PCI_VENDOR_ID_HYGON
) {
445 if (PIIX4_dev
->device
== PCI_DEVICE_ID_AMD_KERNCZ_SMBUS
||
446 (PIIX4_dev
->device
== PCI_DEVICE_ID_AMD_HUDSON2_SMBUS
&&
447 PIIX4_dev
->revision
>= 0x1F)) {
448 piix4_port_sel_sb800
= SB800_PIIX4_PORT_IDX_KERNCZ
;
449 piix4_port_mask_sb800
= SB800_PIIX4_PORT_IDX_MASK_KERNCZ
;
450 piix4_port_shift_sb800
= SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ
;
452 piix4_port_sel_sb800
= SB800_PIIX4_PORT_IDX_ALT
;
453 piix4_port_mask_sb800
= SB800_PIIX4_PORT_IDX_MASK
;
454 piix4_port_shift_sb800
= SB800_PIIX4_PORT_IDX_SHIFT
;
457 mmio_cfg
.use_mmio
= piix4_sb800_use_mmio(PIIX4_dev
);
458 retval
= piix4_sb800_region_request(&PIIX4_dev
->dev
, &mmio_cfg
);
460 release_region(piix4_smba
, SMBIOSIZE
);
464 outb_p(SB800_PIIX4_PORT_IDX_SEL
, SB800_PIIX4_SMB_IDX
);
465 port_sel
= inb_p(SB800_PIIX4_SMB_IDX
+ 1);
466 piix4_port_sel_sb800
= (port_sel
& 0x01) ?
467 SB800_PIIX4_PORT_IDX_ALT
:
468 SB800_PIIX4_PORT_IDX
;
469 piix4_port_mask_sb800
= SB800_PIIX4_PORT_IDX_MASK
;
470 piix4_port_shift_sb800
= SB800_PIIX4_PORT_IDX_SHIFT
;
471 piix4_sb800_region_release(&PIIX4_dev
->dev
, &mmio_cfg
);
474 dev_info(&PIIX4_dev
->dev
,
475 "Using register 0x%02x for SMBus port selection\n",
476 (unsigned int)piix4_port_sel_sb800
);
481 static int piix4_setup_aux(struct pci_dev
*PIIX4_dev
,
482 const struct pci_device_id
*id
,
483 unsigned short base_reg_addr
)
485 /* Set up auxiliary SMBus controllers found on some
486 * AMD chipsets e.g. SP5100 (SB700 derivative) */
488 unsigned short piix4_smba
;
490 /* Read address of auxiliary SMBus controller */
491 pci_read_config_word(PIIX4_dev
, base_reg_addr
, &piix4_smba
);
492 if ((piix4_smba
& 1) == 0) {
493 dev_dbg(&PIIX4_dev
->dev
,
494 "Auxiliary SMBus controller not enabled\n");
498 piix4_smba
&= 0xfff0;
499 if (piix4_smba
== 0) {
500 dev_dbg(&PIIX4_dev
->dev
,
501 "Auxiliary SMBus base address uninitialized\n");
505 if (acpi_check_region(piix4_smba
, SMBIOSIZE
, piix4_driver
.name
))
508 if (!request_region(piix4_smba
, SMBIOSIZE
, piix4_driver
.name
)) {
509 dev_err(&PIIX4_dev
->dev
, "Auxiliary SMBus region 0x%x "
510 "already in use!\n", piix4_smba
);
514 dev_info(&PIIX4_dev
->dev
,
515 "Auxiliary SMBus Host Controller at 0x%x\n",
521 int piix4_transaction(struct i2c_adapter
*piix4_adapter
, unsigned short piix4_smba
)
527 dev_dbg(&piix4_adapter
->dev
, "Transaction (pre): CNT=%02x, CMD=%02x, "
528 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT
),
529 inb_p(SMBHSTCMD
), inb_p(SMBHSTADD
), inb_p(SMBHSTDAT0
),
532 /* Make sure the SMBus host is ready to start transmitting */
533 if ((temp
= inb_p(SMBHSTSTS
)) != 0x00) {
534 dev_dbg(&piix4_adapter
->dev
, "SMBus busy (%02x). "
535 "Resetting...\n", temp
);
536 outb_p(temp
, SMBHSTSTS
);
537 if ((temp
= inb_p(SMBHSTSTS
)) != 0x00) {
538 dev_err(&piix4_adapter
->dev
, "Failed! (%02x)\n", temp
);
541 dev_dbg(&piix4_adapter
->dev
, "Successful!\n");
545 /* start the transaction by setting bit 6 */
546 outb_p(inb(SMBHSTCNT
) | 0x040, SMBHSTCNT
);
548 /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
549 if (srvrworks_csb5_delay
) /* Extra delay for SERVERWORKS_CSB5 */
550 usleep_range(2000, 2100);
552 usleep_range(250, 500);
554 while ((++timeout
< MAX_TIMEOUT
) &&
555 ((temp
= inb_p(SMBHSTSTS
)) & 0x01))
556 usleep_range(250, 500);
558 /* If the SMBus is still busy, we give up */
559 if (timeout
== MAX_TIMEOUT
) {
560 dev_err(&piix4_adapter
->dev
, "SMBus Timeout!\n");
566 dev_err(&piix4_adapter
->dev
, "Error: Failed bus transaction\n");
571 dev_dbg(&piix4_adapter
->dev
, "Bus collision! SMBus may be "
572 "locked until next hard reset. (sorry!)\n");
573 /* Clock stops and target is stuck in mid-transmission */
578 dev_dbg(&piix4_adapter
->dev
, "Error: no response!\n");
581 if (inb_p(SMBHSTSTS
) != 0x00)
582 outb_p(inb(SMBHSTSTS
), SMBHSTSTS
);
584 if ((temp
= inb_p(SMBHSTSTS
)) != 0x00) {
585 dev_err(&piix4_adapter
->dev
, "Failed reset at end of "
586 "transaction (%02x)\n", temp
);
588 dev_dbg(&piix4_adapter
->dev
, "Transaction (post): CNT=%02x, CMD=%02x, "
589 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT
),
590 inb_p(SMBHSTCMD
), inb_p(SMBHSTADD
), inb_p(SMBHSTDAT0
),
594 EXPORT_SYMBOL_NS_GPL(piix4_transaction
, "PIIX4_SMBUS");
596 /* Return negative errno on error. */
597 static s32
piix4_access(struct i2c_adapter
* adap
, u16 addr
,
598 unsigned short flags
, char read_write
,
599 u8 command
, int size
, union i2c_smbus_data
* data
)
601 struct i2c_piix4_adapdata
*adapdata
= i2c_get_adapdata(adap
);
602 unsigned short piix4_smba
= adapdata
->smba
;
607 case I2C_SMBUS_QUICK
:
608 outb_p((addr
<< 1) | read_write
,
613 outb_p((addr
<< 1) | read_write
,
615 if (read_write
== I2C_SMBUS_WRITE
)
616 outb_p(command
, SMBHSTCMD
);
619 case I2C_SMBUS_BYTE_DATA
:
620 outb_p((addr
<< 1) | read_write
,
622 outb_p(command
, SMBHSTCMD
);
623 if (read_write
== I2C_SMBUS_WRITE
)
624 outb_p(data
->byte
, SMBHSTDAT0
);
625 size
= PIIX4_BYTE_DATA
;
627 case I2C_SMBUS_WORD_DATA
:
628 outb_p((addr
<< 1) | read_write
,
630 outb_p(command
, SMBHSTCMD
);
631 if (read_write
== I2C_SMBUS_WRITE
) {
632 outb_p(data
->word
& 0xff, SMBHSTDAT0
);
633 outb_p((data
->word
& 0xff00) >> 8, SMBHSTDAT1
);
635 size
= PIIX4_WORD_DATA
;
637 case I2C_SMBUS_BLOCK_DATA
:
638 outb_p((addr
<< 1) | read_write
,
640 outb_p(command
, SMBHSTCMD
);
641 if (read_write
== I2C_SMBUS_WRITE
) {
642 len
= data
->block
[0];
643 if (len
== 0 || len
> I2C_SMBUS_BLOCK_MAX
)
645 outb_p(len
, SMBHSTDAT0
);
646 inb_p(SMBHSTCNT
); /* Reset SMBBLKDAT */
647 for (i
= 1; i
<= len
; i
++)
648 outb_p(data
->block
[i
], SMBBLKDAT
);
650 size
= PIIX4_BLOCK_DATA
;
653 dev_warn(&adap
->dev
, "Unsupported transaction %d\n", size
);
657 outb_p((size
& 0x1C) + (ENABLE_INT9
& 1), SMBHSTCNT
);
659 status
= piix4_transaction(adap
, piix4_smba
);
663 if ((read_write
== I2C_SMBUS_WRITE
) || (size
== PIIX4_QUICK
))
669 case PIIX4_BYTE_DATA
:
670 data
->byte
= inb_p(SMBHSTDAT0
);
672 case PIIX4_WORD_DATA
:
673 data
->word
= inb_p(SMBHSTDAT0
) + (inb_p(SMBHSTDAT1
) << 8);
675 case PIIX4_BLOCK_DATA
:
676 data
->block
[0] = inb_p(SMBHSTDAT0
);
677 if (data
->block
[0] == 0 || data
->block
[0] > I2C_SMBUS_BLOCK_MAX
)
679 inb_p(SMBHSTCNT
); /* Reset SMBBLKDAT */
680 for (i
= 1; i
<= data
->block
[0]; i
++)
681 data
->block
[i
] = inb_p(SMBBLKDAT
);
687 static uint8_t piix4_imc_read(uint8_t idx
)
689 outb_p(idx
, KERNCZ_IMC_IDX
);
690 return inb_p(KERNCZ_IMC_DATA
);
693 static void piix4_imc_write(uint8_t idx
, uint8_t value
)
695 outb_p(idx
, KERNCZ_IMC_IDX
);
696 outb_p(value
, KERNCZ_IMC_DATA
);
699 static int piix4_imc_sleep(void)
701 int timeout
= MAX_TIMEOUT
;
703 if (!request_muxed_region(KERNCZ_IMC_IDX
, 2, "smbus_kerncz_imc"))
706 /* clear response register */
707 piix4_imc_write(0x82, 0x00);
708 /* request ownership flag */
709 piix4_imc_write(0x83, 0xB4);
710 /* kick off IMC Mailbox command 96 */
711 piix4_imc_write(0x80, 0x96);
714 if (piix4_imc_read(0x82) == 0xfa) {
715 release_region(KERNCZ_IMC_IDX
, 2);
718 usleep_range(1000, 2000);
721 release_region(KERNCZ_IMC_IDX
, 2);
725 static void piix4_imc_wakeup(void)
727 int timeout
= MAX_TIMEOUT
;
729 if (!request_muxed_region(KERNCZ_IMC_IDX
, 2, "smbus_kerncz_imc"))
732 /* clear response register */
733 piix4_imc_write(0x82, 0x00);
734 /* release ownership flag */
735 piix4_imc_write(0x83, 0xB5);
736 /* kick off IMC Mailbox command 96 */
737 piix4_imc_write(0x80, 0x96);
740 if (piix4_imc_read(0x82) == 0xfa)
742 usleep_range(1000, 2000);
745 release_region(KERNCZ_IMC_IDX
, 2);
748 int piix4_sb800_port_sel(u8 port
, struct sb800_mmio_cfg
*mmio_cfg
)
752 if (mmio_cfg
->use_mmio
) {
753 smba_en_lo
= ioread8(mmio_cfg
->addr
+ piix4_port_sel_sb800
);
754 val
= (smba_en_lo
& ~piix4_port_mask_sb800
) | port
;
755 if (smba_en_lo
!= val
)
756 iowrite8(val
, mmio_cfg
->addr
+ piix4_port_sel_sb800
);
758 return (smba_en_lo
& piix4_port_mask_sb800
);
761 outb_p(piix4_port_sel_sb800
, SB800_PIIX4_SMB_IDX
);
762 smba_en_lo
= inb_p(SB800_PIIX4_SMB_IDX
+ 1);
764 val
= (smba_en_lo
& ~piix4_port_mask_sb800
) | port
;
765 if (smba_en_lo
!= val
)
766 outb_p(val
, SB800_PIIX4_SMB_IDX
+ 1);
768 return (smba_en_lo
& piix4_port_mask_sb800
);
770 EXPORT_SYMBOL_NS_GPL(piix4_sb800_port_sel
, "PIIX4_SMBUS");
773 * Handles access to multiple SMBus ports on the SB800.
774 * The port is selected by bits 2:1 of the smb_en register (0x2c).
775 * Returns negative errno on error.
777 * Note: The selected port must be returned to the initial selection to avoid
778 * problems on certain systems.
780 static s32
piix4_access_sb800(struct i2c_adapter
*adap
, u16 addr
,
781 unsigned short flags
, char read_write
,
782 u8 command
, int size
, union i2c_smbus_data
*data
)
784 struct i2c_piix4_adapdata
*adapdata
= i2c_get_adapdata(adap
);
785 unsigned short piix4_smba
= adapdata
->smba
;
786 int retries
= MAX_TIMEOUT
;
791 retval
= piix4_sb800_region_request(&adap
->dev
, &adapdata
->mmio_cfg
);
795 /* Request the SMBUS semaphore, avoid conflicts with the IMC */
796 smbslvcnt
= inb_p(SMBSLVCNT
);
798 outb_p(smbslvcnt
| 0x10, SMBSLVCNT
);
800 /* Check the semaphore status */
801 smbslvcnt
= inb_p(SMBSLVCNT
);
802 if (smbslvcnt
& 0x10)
805 usleep_range(1000, 2000);
807 /* SMBus is still owned by the IMC, we give up */
814 * Notify the IMC (Integrated Micro Controller) if required.
815 * Among other responsibilities, the IMC is in charge of monitoring
816 * the System fans and temperature sensors, and act accordingly.
817 * All this is done through SMBus and can/will collide
818 * with our transactions if they are long (BLOCK_DATA).
819 * Therefore we need to request the ownership flag during those
822 if ((size
== I2C_SMBUS_BLOCK_DATA
) && adapdata
->notify_imc
) {
825 ret
= piix4_imc_sleep();
829 "IMC base address index region 0x%x already in use.\n",
834 "Failed to communicate with the IMC.\n");
840 /* If IMC communication fails do not retry */
843 "Continuing without IMC notification.\n");
844 adapdata
->notify_imc
= false;
848 prev_port
= piix4_sb800_port_sel(adapdata
->port
, &adapdata
->mmio_cfg
);
850 retval
= piix4_access(adap
, addr
, flags
, read_write
,
851 command
, size
, data
);
853 piix4_sb800_port_sel(prev_port
, &adapdata
->mmio_cfg
);
855 /* Release the semaphore */
856 outb_p(smbslvcnt
| 0x20, SMBSLVCNT
);
858 if ((size
== I2C_SMBUS_BLOCK_DATA
) && adapdata
->notify_imc
)
862 piix4_sb800_region_release(&adap
->dev
, &adapdata
->mmio_cfg
);
866 static u32
piix4_func(struct i2c_adapter
*adapter
)
868 return I2C_FUNC_SMBUS_QUICK
| I2C_FUNC_SMBUS_BYTE
|
869 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
|
870 I2C_FUNC_SMBUS_BLOCK_DATA
;
873 static const struct i2c_algorithm smbus_algorithm
= {
874 .smbus_xfer
= piix4_access
,
875 .functionality
= piix4_func
,
878 static const struct i2c_algorithm piix4_smbus_algorithm_sb800
= {
879 .smbus_xfer
= piix4_access_sb800
,
880 .functionality
= piix4_func
,
883 static const struct pci_device_id piix4_ids
[] = {
884 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82371AB_3
) },
885 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82443MX_3
) },
886 { PCI_DEVICE(PCI_VENDOR_ID_EFAR
, PCI_DEVICE_ID_EFAR_SLC90E66_3
) },
887 { PCI_DEVICE(PCI_VENDOR_ID_ATI
, PCI_DEVICE_ID_ATI_IXP200_SMBUS
) },
888 { PCI_DEVICE(PCI_VENDOR_ID_ATI
, PCI_DEVICE_ID_ATI_IXP300_SMBUS
) },
889 { PCI_DEVICE(PCI_VENDOR_ID_ATI
, PCI_DEVICE_ID_ATI_IXP400_SMBUS
) },
890 { PCI_DEVICE(PCI_VENDOR_ID_ATI
, PCI_DEVICE_ID_ATI_SBX00_SMBUS
) },
891 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS
) },
892 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS
) },
893 { PCI_DEVICE(PCI_VENDOR_ID_HYGON
, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS
) },
894 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS
,
895 PCI_DEVICE_ID_SERVERWORKS_OSB4
) },
896 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS
,
897 PCI_DEVICE_ID_SERVERWORKS_CSB5
) },
898 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS
,
899 PCI_DEVICE_ID_SERVERWORKS_CSB6
) },
900 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS
,
901 PCI_DEVICE_ID_SERVERWORKS_HT1000SB
) },
902 { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS
,
903 PCI_DEVICE_ID_SERVERWORKS_HT1100LD
) },
907 MODULE_DEVICE_TABLE (pci
, piix4_ids
);
909 static struct i2c_adapter
*piix4_main_adapters
[PIIX4_MAX_ADAPTERS
];
910 static struct i2c_adapter
*piix4_aux_adapter
;
911 static int piix4_adapter_count
;
913 static int piix4_add_adapter(struct pci_dev
*dev
, unsigned short smba
,
914 bool sb800_main
, u8 port
, bool notify_imc
,
915 u8 hw_port_nr
, const char *name
,
916 struct i2c_adapter
**padap
)
918 struct i2c_adapter
*adap
;
919 struct i2c_piix4_adapdata
*adapdata
;
922 adap
= kzalloc(sizeof(*adap
), GFP_KERNEL
);
924 release_region(smba
, SMBIOSIZE
);
928 adap
->owner
= THIS_MODULE
;
929 adap
->class = I2C_CLASS_HWMON
;
930 adap
->algo
= sb800_main
? &piix4_smbus_algorithm_sb800
933 adapdata
= kzalloc(sizeof(*adapdata
), GFP_KERNEL
);
934 if (adapdata
== NULL
) {
936 release_region(smba
, SMBIOSIZE
);
940 adapdata
->mmio_cfg
.use_mmio
= piix4_sb800_use_mmio(dev
);
941 adapdata
->smba
= smba
;
942 adapdata
->sb800_main
= sb800_main
;
943 adapdata
->port
= port
<< piix4_port_shift_sb800
;
944 adapdata
->notify_imc
= notify_imc
;
946 /* set up the sysfs linkage to our parent device */
947 adap
->dev
.parent
= &dev
->dev
;
949 if (has_acpi_companion(&dev
->dev
)) {
950 acpi_preset_companion(&adap
->dev
,
951 ACPI_COMPANION(&dev
->dev
),
955 snprintf(adap
->name
, sizeof(adap
->name
),
956 "SMBus PIIX4 adapter%s at %04x", name
, smba
);
958 i2c_set_adapdata(adap
, adapdata
);
960 retval
= i2c_add_adapter(adap
);
964 release_region(smba
, SMBIOSIZE
);
969 * The AUX bus can not be probed as on some platforms it reports all
970 * devices present and all reads return "0".
971 * This would allow the ee1004 to be probed incorrectly.
974 i2c_register_spd(adap
);
980 static int piix4_add_adapters_sb800(struct pci_dev
*dev
, unsigned short smba
,
983 struct i2c_piix4_adapdata
*adapdata
;
987 if (dev
->device
== PCI_DEVICE_ID_AMD_KERNCZ_SMBUS
||
988 (dev
->device
== PCI_DEVICE_ID_AMD_HUDSON2_SMBUS
&&
989 dev
->revision
>= 0x1F)) {
990 piix4_adapter_count
= HUDSON2_MAIN_PORTS
;
992 piix4_adapter_count
= PIIX4_MAX_ADAPTERS
;
995 for (port
= 0; port
< piix4_adapter_count
; port
++) {
996 u8 hw_port_nr
= port
== 0 ? 0 : port
+ 1;
998 retval
= piix4_add_adapter(dev
, smba
, true, port
, notify_imc
,
1000 piix4_main_port_names_sb800
[port
],
1001 &piix4_main_adapters
[port
]);
1010 "Error setting up SB800 adapters. Unregistering!\n");
1011 while (--port
>= 0) {
1012 adapdata
= i2c_get_adapdata(piix4_main_adapters
[port
]);
1013 if (adapdata
->smba
) {
1014 i2c_del_adapter(piix4_main_adapters
[port
]);
1016 kfree(piix4_main_adapters
[port
]);
1017 piix4_main_adapters
[port
] = NULL
;
1024 static int piix4_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
1027 bool is_sb800
= false;
1028 bool is_asf
= false;
1032 if ((dev
->vendor
== PCI_VENDOR_ID_ATI
&&
1033 dev
->device
== PCI_DEVICE_ID_ATI_SBX00_SMBUS
&&
1034 dev
->revision
>= 0x40) ||
1035 dev
->vendor
== PCI_VENDOR_ID_AMD
||
1036 dev
->vendor
== PCI_VENDOR_ID_HYGON
) {
1037 bool notify_imc
= false;
1040 if ((dev
->vendor
== PCI_VENDOR_ID_AMD
||
1041 dev
->vendor
== PCI_VENDOR_ID_HYGON
) &&
1042 dev
->device
== PCI_DEVICE_ID_AMD_KERNCZ_SMBUS
) {
1046 * Detect if IMC is active or not, this method is
1047 * described on coreboot's AMD IMC notes
1049 pci_bus_read_config_byte(dev
->bus
, PCI_DEVFN(0x14, 3),
1055 /* base address location etc changed in SB800 */
1056 retval
= piix4_setup_sb800(dev
, id
, 0);
1061 * Try to register multiplexed main SMBus adapter,
1062 * give up if we can't
1064 retval
= piix4_add_adapters_sb800(dev
, retval
, notify_imc
);
1068 retval
= piix4_setup(dev
, id
);
1072 /* Try to register main SMBus adapter, give up if we can't */
1073 retval
= piix4_add_adapter(dev
, retval
, false, 0, false, 0,
1074 "", &piix4_main_adapters
[0]);
1077 piix4_adapter_count
= 1;
1080 /* Check for auxiliary SMBus on some AMD chipsets */
1083 if (dev
->vendor
== PCI_VENDOR_ID_ATI
&&
1084 dev
->device
== PCI_DEVICE_ID_ATI_SBX00_SMBUS
) {
1085 if (dev
->revision
< 0x40) {
1086 retval
= piix4_setup_aux(dev
, id
, 0x58);
1088 /* SB800 added aux bus too */
1089 retval
= piix4_setup_sb800(dev
, id
, 1);
1093 status
= acpi_get_handle(NULL
, (acpi_string
)SB800_ASF_ACPI_PATH
, &handle
);
1094 if (ACPI_SUCCESS(status
))
1097 if (dev
->vendor
== PCI_VENDOR_ID_AMD
&&
1098 (dev
->device
== PCI_DEVICE_ID_AMD_HUDSON2_SMBUS
||
1099 dev
->device
== PCI_DEVICE_ID_AMD_KERNCZ_SMBUS
)) {
1100 /* Do not setup AUX port if ASF is enabled */
1102 retval
= piix4_setup_sb800(dev
, id
, 1);
1106 /* Try to add the aux adapter if it exists,
1107 * piix4_add_adapter will clean up if this fails */
1108 piix4_add_adapter(dev
, retval
, false, 0, false, 1,
1109 is_sb800
? piix4_aux_port_name_sb800
: "",
1110 &piix4_aux_adapter
);
1116 static void piix4_adap_remove(struct i2c_adapter
*adap
)
1118 struct i2c_piix4_adapdata
*adapdata
= i2c_get_adapdata(adap
);
1120 if (adapdata
->smba
) {
1121 i2c_del_adapter(adap
);
1122 if (adapdata
->port
== (0 << piix4_port_shift_sb800
))
1123 release_region(adapdata
->smba
, SMBIOSIZE
);
1129 static void piix4_remove(struct pci_dev
*dev
)
1131 int port
= piix4_adapter_count
;
1133 while (--port
>= 0) {
1134 if (piix4_main_adapters
[port
]) {
1135 piix4_adap_remove(piix4_main_adapters
[port
]);
1136 piix4_main_adapters
[port
] = NULL
;
1140 if (piix4_aux_adapter
) {
1141 piix4_adap_remove(piix4_aux_adapter
);
1142 piix4_aux_adapter
= NULL
;
1146 static struct pci_driver piix4_driver
= {
1147 .name
= "piix4_smbus",
1148 .id_table
= piix4_ids
,
1149 .probe
= piix4_probe
,
1150 .remove
= piix4_remove
,
1153 module_pci_driver(piix4_driver
);
1155 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
1156 MODULE_AUTHOR("Philip Edelbrock <phil@netroedge.com>");
1157 MODULE_DESCRIPTION("PIIX4 SMBus driver");
1158 MODULE_LICENSE("GPL");