1 // SPDX-License-Identifier: GPL-2.0-only
3 * SMBus 2.0 driver for AMD-8111 IO-Hub.
5 * Copyright (c) 2002 Vojtech Pavlik
8 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/stddef.h>
12 #include <linux/ioport.h>
13 #include <linux/i2c.h>
14 #include <linux/delay.h>
15 #include <linux/acpi.h>
16 #include <linux/slab.h>
19 MODULE_LICENSE("GPL");
20 MODULE_AUTHOR ("Vojtech Pavlik <vojtech@suse.cz>");
21 MODULE_DESCRIPTION("AMD8111 SMBus 2.0 driver");
25 struct i2c_adapter adapter
;
30 static struct pci_driver amd8111_driver
;
33 * AMD PCI control registers definitions.
36 #define AMD_PCI_MISC 0x48
38 #define AMD_PCI_MISC_SCI 0x04 /* deliver SCI */
39 #define AMD_PCI_MISC_INT 0x02 /* deliver PCI IRQ */
40 #define AMD_PCI_MISC_SPEEDUP 0x01 /* 16x clock speedup */
43 * ACPI 2.0 chapter 13 PCI interface definitions.
46 #define AMD_EC_DATA 0x00 /* data register */
47 #define AMD_EC_SC 0x04 /* status of controller */
48 #define AMD_EC_CMD 0x04 /* command register */
49 #define AMD_EC_ICR 0x08 /* interrupt control register */
51 #define AMD_EC_SC_SMI 0x04 /* smi event pending */
52 #define AMD_EC_SC_SCI 0x02 /* sci event pending */
53 #define AMD_EC_SC_BURST 0x01 /* burst mode enabled */
54 #define AMD_EC_SC_CMD 0x08 /* byte in data reg is command */
55 #define AMD_EC_SC_IBF 0x02 /* data ready for embedded controller */
56 #define AMD_EC_SC_OBF 0x01 /* data ready for host */
58 #define AMD_EC_CMD_RD 0x80 /* read EC */
59 #define AMD_EC_CMD_WR 0x81 /* write EC */
60 #define AMD_EC_CMD_BE 0x82 /* enable burst mode */
61 #define AMD_EC_CMD_BD 0x83 /* disable burst mode */
62 #define AMD_EC_CMD_QR 0x84 /* query EC */
65 * ACPI 2.0 chapter 13 access of registers of the EC
68 static int amd_ec_wait_write(struct amd_smbus
*smbus
)
72 while ((inb(smbus
->base
+ AMD_EC_SC
) & AMD_EC_SC_IBF
) && --timeout
)
76 dev_warn(&smbus
->dev
->dev
,
77 "Timeout while waiting for IBF to clear\n");
84 static int amd_ec_wait_read(struct amd_smbus
*smbus
)
88 while ((~inb(smbus
->base
+ AMD_EC_SC
) & AMD_EC_SC_OBF
) && --timeout
)
92 dev_warn(&smbus
->dev
->dev
,
93 "Timeout while waiting for OBF to set\n");
100 static int amd_ec_read(struct amd_smbus
*smbus
, unsigned char address
,
105 status
= amd_ec_wait_write(smbus
);
108 outb(AMD_EC_CMD_RD
, smbus
->base
+ AMD_EC_CMD
);
110 status
= amd_ec_wait_write(smbus
);
113 outb(address
, smbus
->base
+ AMD_EC_DATA
);
115 status
= amd_ec_wait_read(smbus
);
118 *data
= inb(smbus
->base
+ AMD_EC_DATA
);
123 static int amd_ec_write(struct amd_smbus
*smbus
, unsigned char address
,
128 status
= amd_ec_wait_write(smbus
);
131 outb(AMD_EC_CMD_WR
, smbus
->base
+ AMD_EC_CMD
);
133 status
= amd_ec_wait_write(smbus
);
136 outb(address
, smbus
->base
+ AMD_EC_DATA
);
138 status
= amd_ec_wait_write(smbus
);
141 outb(data
, smbus
->base
+ AMD_EC_DATA
);
147 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
150 #define AMD_SMB_PRTCL 0x00 /* protocol, PEC */
151 #define AMD_SMB_STS 0x01 /* status */
152 #define AMD_SMB_ADDR 0x02 /* address */
153 #define AMD_SMB_CMD 0x03 /* command */
154 #define AMD_SMB_DATA 0x04 /* 32 data registers */
155 #define AMD_SMB_BCNT 0x24 /* number of data bytes */
156 #define AMD_SMB_ALRM_A 0x25 /* alarm address */
157 #define AMD_SMB_ALRM_D 0x26 /* 2 bytes alarm data */
159 #define AMD_SMB_STS_DONE 0x80
160 #define AMD_SMB_STS_ALRM 0x40
161 #define AMD_SMB_STS_RES 0x20
162 #define AMD_SMB_STS_STATUS 0x1f
164 #define AMD_SMB_STATUS_OK 0x00
165 #define AMD_SMB_STATUS_FAIL 0x07
166 #define AMD_SMB_STATUS_DNAK 0x10
167 #define AMD_SMB_STATUS_DERR 0x11
168 #define AMD_SMB_STATUS_CMD_DENY 0x12
169 #define AMD_SMB_STATUS_UNKNOWN 0x13
170 #define AMD_SMB_STATUS_ACC_DENY 0x17
171 #define AMD_SMB_STATUS_TIMEOUT 0x18
172 #define AMD_SMB_STATUS_NOTSUP 0x19
173 #define AMD_SMB_STATUS_BUSY 0x1A
174 #define AMD_SMB_STATUS_PEC 0x1F
176 #define AMD_SMB_PRTCL_WRITE 0x00
177 #define AMD_SMB_PRTCL_READ 0x01
178 #define AMD_SMB_PRTCL_QUICK 0x02
179 #define AMD_SMB_PRTCL_BYTE 0x04
180 #define AMD_SMB_PRTCL_BYTE_DATA 0x06
181 #define AMD_SMB_PRTCL_WORD_DATA 0x08
182 #define AMD_SMB_PRTCL_BLOCK_DATA 0x0a
183 #define AMD_SMB_PRTCL_PROC_CALL 0x0c
184 #define AMD_SMB_PRTCL_BLOCK_PROC_CALL 0x0d
185 #define AMD_SMB_PRTCL_I2C_BLOCK_DATA 0x4a
186 #define AMD_SMB_PRTCL_PEC 0x80
189 static s32
amd8111_access(struct i2c_adapter
* adap
, u16 addr
,
190 unsigned short flags
, char read_write
, u8 command
, int size
,
191 union i2c_smbus_data
* data
)
193 struct amd_smbus
*smbus
= adap
->algo_data
;
194 unsigned char protocol
, len
, pec
, temp
[2];
197 protocol
= (read_write
== I2C_SMBUS_READ
) ? AMD_SMB_PRTCL_READ
198 : AMD_SMB_PRTCL_WRITE
;
199 pec
= (flags
& I2C_CLIENT_PEC
) ? AMD_SMB_PRTCL_PEC
: 0;
202 case I2C_SMBUS_QUICK
:
203 protocol
|= AMD_SMB_PRTCL_QUICK
;
204 read_write
= I2C_SMBUS_WRITE
;
208 if (read_write
== I2C_SMBUS_WRITE
) {
209 status
= amd_ec_write(smbus
, AMD_SMB_CMD
,
214 protocol
|= AMD_SMB_PRTCL_BYTE
;
217 case I2C_SMBUS_BYTE_DATA
:
218 status
= amd_ec_write(smbus
, AMD_SMB_CMD
, command
);
221 if (read_write
== I2C_SMBUS_WRITE
) {
222 status
= amd_ec_write(smbus
, AMD_SMB_DATA
,
227 protocol
|= AMD_SMB_PRTCL_BYTE_DATA
;
230 case I2C_SMBUS_WORD_DATA
:
231 status
= amd_ec_write(smbus
, AMD_SMB_CMD
, command
);
234 if (read_write
== I2C_SMBUS_WRITE
) {
235 status
= amd_ec_write(smbus
, AMD_SMB_DATA
,
239 status
= amd_ec_write(smbus
, AMD_SMB_DATA
+ 1,
244 protocol
|= AMD_SMB_PRTCL_WORD_DATA
| pec
;
247 case I2C_SMBUS_BLOCK_DATA
:
248 status
= amd_ec_write(smbus
, AMD_SMB_CMD
, command
);
251 if (read_write
== I2C_SMBUS_WRITE
) {
252 len
= min_t(u8
, data
->block
[0],
253 I2C_SMBUS_BLOCK_MAX
);
254 status
= amd_ec_write(smbus
, AMD_SMB_BCNT
, len
);
257 for (i
= 0; i
< len
; i
++) {
259 amd_ec_write(smbus
, AMD_SMB_DATA
+ i
,
265 protocol
|= AMD_SMB_PRTCL_BLOCK_DATA
| pec
;
268 case I2C_SMBUS_I2C_BLOCK_DATA
:
269 len
= min_t(u8
, data
->block
[0],
270 I2C_SMBUS_BLOCK_MAX
);
271 status
= amd_ec_write(smbus
, AMD_SMB_CMD
, command
);
274 status
= amd_ec_write(smbus
, AMD_SMB_BCNT
, len
);
277 if (read_write
== I2C_SMBUS_WRITE
)
278 for (i
= 0; i
< len
; i
++) {
280 amd_ec_write(smbus
, AMD_SMB_DATA
+ i
,
285 protocol
|= AMD_SMB_PRTCL_I2C_BLOCK_DATA
;
288 case I2C_SMBUS_PROC_CALL
:
289 status
= amd_ec_write(smbus
, AMD_SMB_CMD
, command
);
292 status
= amd_ec_write(smbus
, AMD_SMB_DATA
,
296 status
= amd_ec_write(smbus
, AMD_SMB_DATA
+ 1,
300 protocol
= AMD_SMB_PRTCL_PROC_CALL
| pec
;
301 read_write
= I2C_SMBUS_READ
;
304 case I2C_SMBUS_BLOCK_PROC_CALL
:
305 len
= min_t(u8
, data
->block
[0],
306 I2C_SMBUS_BLOCK_MAX
- 1);
307 status
= amd_ec_write(smbus
, AMD_SMB_CMD
, command
);
310 status
= amd_ec_write(smbus
, AMD_SMB_BCNT
, len
);
313 for (i
= 0; i
< len
; i
++) {
314 status
= amd_ec_write(smbus
, AMD_SMB_DATA
+ i
,
319 protocol
= AMD_SMB_PRTCL_BLOCK_PROC_CALL
| pec
;
320 read_write
= I2C_SMBUS_READ
;
324 dev_warn(&adap
->dev
, "Unsupported transaction %d\n", size
);
328 status
= amd_ec_write(smbus
, AMD_SMB_ADDR
, addr
<< 1);
331 status
= amd_ec_write(smbus
, AMD_SMB_PRTCL
, protocol
);
335 status
= amd_ec_read(smbus
, AMD_SMB_STS
, temp
+ 0);
339 if (~temp
[0] & AMD_SMB_STS_DONE
) {
341 status
= amd_ec_read(smbus
, AMD_SMB_STS
, temp
+ 0);
346 if (~temp
[0] & AMD_SMB_STS_DONE
) {
348 status
= amd_ec_read(smbus
, AMD_SMB_STS
, temp
+ 0);
353 if ((~temp
[0] & AMD_SMB_STS_DONE
) || (temp
[0] & AMD_SMB_STS_STATUS
))
356 if (read_write
== I2C_SMBUS_WRITE
)
361 case I2C_SMBUS_BYTE_DATA
:
362 status
= amd_ec_read(smbus
, AMD_SMB_DATA
, &data
->byte
);
367 case I2C_SMBUS_WORD_DATA
:
368 case I2C_SMBUS_PROC_CALL
:
369 status
= amd_ec_read(smbus
, AMD_SMB_DATA
, temp
+ 0);
372 status
= amd_ec_read(smbus
, AMD_SMB_DATA
+ 1, temp
+ 1);
375 data
->word
= (temp
[1] << 8) | temp
[0];
378 case I2C_SMBUS_BLOCK_DATA
:
379 case I2C_SMBUS_BLOCK_PROC_CALL
:
380 status
= amd_ec_read(smbus
, AMD_SMB_BCNT
, &len
);
383 len
= min_t(u8
, len
, I2C_SMBUS_BLOCK_MAX
);
385 case I2C_SMBUS_I2C_BLOCK_DATA
:
386 for (i
= 0; i
< len
; i
++) {
387 status
= amd_ec_read(smbus
, AMD_SMB_DATA
+ i
,
388 data
->block
+ i
+ 1);
392 data
->block
[0] = len
;
400 static u32
amd8111_func(struct i2c_adapter
*adapter
)
402 return I2C_FUNC_SMBUS_QUICK
| I2C_FUNC_SMBUS_BYTE
|
403 I2C_FUNC_SMBUS_BYTE_DATA
|
404 I2C_FUNC_SMBUS_WORD_DATA
| I2C_FUNC_SMBUS_BLOCK_DATA
|
405 I2C_FUNC_SMBUS_PROC_CALL
| I2C_FUNC_SMBUS_BLOCK_PROC_CALL
|
406 I2C_FUNC_SMBUS_I2C_BLOCK
| I2C_FUNC_SMBUS_PEC
;
409 static const struct i2c_algorithm smbus_algorithm
= {
410 .smbus_xfer
= amd8111_access
,
411 .functionality
= amd8111_func
,
415 static const struct pci_device_id amd8111_ids
[] = {
416 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_8111_SMBUS2
) },
420 MODULE_DEVICE_TABLE (pci
, amd8111_ids
);
422 static int amd8111_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
424 struct amd_smbus
*smbus
;
427 if (!(pci_resource_flags(dev
, 0) & IORESOURCE_IO
))
430 smbus
= kzalloc(sizeof(struct amd_smbus
), GFP_KERNEL
);
435 smbus
->base
= pci_resource_start(dev
, 0);
436 smbus
->size
= pci_resource_len(dev
, 0);
438 error
= acpi_check_resource_conflict(&dev
->resource
[0]);
444 if (!request_region(smbus
->base
, smbus
->size
, amd8111_driver
.name
)) {
449 smbus
->adapter
.owner
= THIS_MODULE
;
450 snprintf(smbus
->adapter
.name
, sizeof(smbus
->adapter
.name
),
451 "SMBus2 AMD8111 adapter at %04x", smbus
->base
);
452 smbus
->adapter
.class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
;
453 smbus
->adapter
.algo
= &smbus_algorithm
;
454 smbus
->adapter
.algo_data
= smbus
;
456 /* set up the sysfs linkage to our parent device */
457 smbus
->adapter
.dev
.parent
= &dev
->dev
;
459 pci_write_config_dword(smbus
->dev
, AMD_PCI_MISC
, 0);
460 error
= i2c_add_adapter(&smbus
->adapter
);
462 goto out_release_region
;
464 pci_set_drvdata(dev
, smbus
);
468 release_region(smbus
->base
, smbus
->size
);
474 static void amd8111_remove(struct pci_dev
*dev
)
476 struct amd_smbus
*smbus
= pci_get_drvdata(dev
);
478 i2c_del_adapter(&smbus
->adapter
);
479 release_region(smbus
->base
, smbus
->size
);
483 static struct pci_driver amd8111_driver
= {
484 .name
= "amd8111_smbus2",
485 .id_table
= amd8111_ids
,
486 .probe
= amd8111_probe
,
487 .remove
= amd8111_remove
,
490 module_pci_driver(amd8111_driver
);