2 * SMBus 2.0 driver for AMD-8111 IO-Hub.
4 * Copyright (c) 2002 Vojtech Pavlik
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation version 2.
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/kernel.h>
14 #include <linux/stddef.h>
15 #include <linux/ioport.h>
16 #include <linux/init.h>
17 #include <linux/i2c.h>
18 #include <linux/delay.h>
19 #include <linux/acpi.h>
20 #include <linux/slab.h>
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR ("Vojtech Pavlik <vojtech@suse.cz>");
25 MODULE_DESCRIPTION("AMD8111 SMBus 2.0 driver");
29 struct i2c_adapter adapter
;
34 static struct pci_driver amd8111_driver
;
37 * AMD PCI control registers definitions.
40 #define AMD_PCI_MISC 0x48
42 #define AMD_PCI_MISC_SCI 0x04 /* deliver SCI */
43 #define AMD_PCI_MISC_INT 0x02 /* deliver PCI IRQ */
44 #define AMD_PCI_MISC_SPEEDUP 0x01 /* 16x clock speedup */
47 * ACPI 2.0 chapter 13 PCI interface definitions.
50 #define AMD_EC_DATA 0x00 /* data register */
51 #define AMD_EC_SC 0x04 /* status of controller */
52 #define AMD_EC_CMD 0x04 /* command register */
53 #define AMD_EC_ICR 0x08 /* interrupt control register */
55 #define AMD_EC_SC_SMI 0x04 /* smi event pending */
56 #define AMD_EC_SC_SCI 0x02 /* sci event pending */
57 #define AMD_EC_SC_BURST 0x01 /* burst mode enabled */
58 #define AMD_EC_SC_CMD 0x08 /* byte in data reg is command */
59 #define AMD_EC_SC_IBF 0x02 /* data ready for embedded controller */
60 #define AMD_EC_SC_OBF 0x01 /* data ready for host */
62 #define AMD_EC_CMD_RD 0x80 /* read EC */
63 #define AMD_EC_CMD_WR 0x81 /* write EC */
64 #define AMD_EC_CMD_BE 0x82 /* enable burst mode */
65 #define AMD_EC_CMD_BD 0x83 /* disable burst mode */
66 #define AMD_EC_CMD_QR 0x84 /* query EC */
69 * ACPI 2.0 chapter 13 access of registers of the EC
72 static int amd_ec_wait_write(struct amd_smbus
*smbus
)
76 while ((inb(smbus
->base
+ AMD_EC_SC
) & AMD_EC_SC_IBF
) && --timeout
)
80 dev_warn(&smbus
->dev
->dev
,
81 "Timeout while waiting for IBF to clear\n");
88 static int amd_ec_wait_read(struct amd_smbus
*smbus
)
92 while ((~inb(smbus
->base
+ AMD_EC_SC
) & AMD_EC_SC_OBF
) && --timeout
)
96 dev_warn(&smbus
->dev
->dev
,
97 "Timeout while waiting for OBF to set\n");
104 static int amd_ec_read(struct amd_smbus
*smbus
, unsigned char address
,
109 status
= amd_ec_wait_write(smbus
);
112 outb(AMD_EC_CMD_RD
, smbus
->base
+ AMD_EC_CMD
);
114 status
= amd_ec_wait_write(smbus
);
117 outb(address
, smbus
->base
+ AMD_EC_DATA
);
119 status
= amd_ec_wait_read(smbus
);
122 *data
= inb(smbus
->base
+ AMD_EC_DATA
);
127 static int amd_ec_write(struct amd_smbus
*smbus
, unsigned char address
,
132 status
= amd_ec_wait_write(smbus
);
135 outb(AMD_EC_CMD_WR
, smbus
->base
+ AMD_EC_CMD
);
137 status
= amd_ec_wait_write(smbus
);
140 outb(address
, smbus
->base
+ AMD_EC_DATA
);
142 status
= amd_ec_wait_write(smbus
);
145 outb(data
, smbus
->base
+ AMD_EC_DATA
);
151 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
154 #define AMD_SMB_PRTCL 0x00 /* protocol, PEC */
155 #define AMD_SMB_STS 0x01 /* status */
156 #define AMD_SMB_ADDR 0x02 /* address */
157 #define AMD_SMB_CMD 0x03 /* command */
158 #define AMD_SMB_DATA 0x04 /* 32 data registers */
159 #define AMD_SMB_BCNT 0x24 /* number of data bytes */
160 #define AMD_SMB_ALRM_A 0x25 /* alarm address */
161 #define AMD_SMB_ALRM_D 0x26 /* 2 bytes alarm data */
163 #define AMD_SMB_STS_DONE 0x80
164 #define AMD_SMB_STS_ALRM 0x40
165 #define AMD_SMB_STS_RES 0x20
166 #define AMD_SMB_STS_STATUS 0x1f
168 #define AMD_SMB_STATUS_OK 0x00
169 #define AMD_SMB_STATUS_FAIL 0x07
170 #define AMD_SMB_STATUS_DNAK 0x10
171 #define AMD_SMB_STATUS_DERR 0x11
172 #define AMD_SMB_STATUS_CMD_DENY 0x12
173 #define AMD_SMB_STATUS_UNKNOWN 0x13
174 #define AMD_SMB_STATUS_ACC_DENY 0x17
175 #define AMD_SMB_STATUS_TIMEOUT 0x18
176 #define AMD_SMB_STATUS_NOTSUP 0x19
177 #define AMD_SMB_STATUS_BUSY 0x1A
178 #define AMD_SMB_STATUS_PEC 0x1F
180 #define AMD_SMB_PRTCL_WRITE 0x00
181 #define AMD_SMB_PRTCL_READ 0x01
182 #define AMD_SMB_PRTCL_QUICK 0x02
183 #define AMD_SMB_PRTCL_BYTE 0x04
184 #define AMD_SMB_PRTCL_BYTE_DATA 0x06
185 #define AMD_SMB_PRTCL_WORD_DATA 0x08
186 #define AMD_SMB_PRTCL_BLOCK_DATA 0x0a
187 #define AMD_SMB_PRTCL_PROC_CALL 0x0c
188 #define AMD_SMB_PRTCL_BLOCK_PROC_CALL 0x0d
189 #define AMD_SMB_PRTCL_I2C_BLOCK_DATA 0x4a
190 #define AMD_SMB_PRTCL_PEC 0x80
193 static s32
amd8111_access(struct i2c_adapter
* adap
, u16 addr
,
194 unsigned short flags
, char read_write
, u8 command
, int size
,
195 union i2c_smbus_data
* data
)
197 struct amd_smbus
*smbus
= adap
->algo_data
;
198 unsigned char protocol
, len
, pec
, temp
[2];
201 protocol
= (read_write
== I2C_SMBUS_READ
) ? AMD_SMB_PRTCL_READ
202 : AMD_SMB_PRTCL_WRITE
;
203 pec
= (flags
& I2C_CLIENT_PEC
) ? AMD_SMB_PRTCL_PEC
: 0;
206 case I2C_SMBUS_QUICK
:
207 protocol
|= AMD_SMB_PRTCL_QUICK
;
208 read_write
= I2C_SMBUS_WRITE
;
212 if (read_write
== I2C_SMBUS_WRITE
) {
213 status
= amd_ec_write(smbus
, AMD_SMB_CMD
,
218 protocol
|= AMD_SMB_PRTCL_BYTE
;
221 case I2C_SMBUS_BYTE_DATA
:
222 status
= amd_ec_write(smbus
, AMD_SMB_CMD
, command
);
225 if (read_write
== I2C_SMBUS_WRITE
) {
226 status
= amd_ec_write(smbus
, AMD_SMB_DATA
,
231 protocol
|= AMD_SMB_PRTCL_BYTE_DATA
;
234 case I2C_SMBUS_WORD_DATA
:
235 status
= amd_ec_write(smbus
, AMD_SMB_CMD
, command
);
238 if (read_write
== I2C_SMBUS_WRITE
) {
239 status
= amd_ec_write(smbus
, AMD_SMB_DATA
,
243 status
= amd_ec_write(smbus
, AMD_SMB_DATA
+ 1,
248 protocol
|= AMD_SMB_PRTCL_WORD_DATA
| pec
;
251 case I2C_SMBUS_BLOCK_DATA
:
252 status
= amd_ec_write(smbus
, AMD_SMB_CMD
, command
);
255 if (read_write
== I2C_SMBUS_WRITE
) {
256 len
= min_t(u8
, data
->block
[0],
257 I2C_SMBUS_BLOCK_MAX
);
258 status
= amd_ec_write(smbus
, AMD_SMB_BCNT
, len
);
261 for (i
= 0; i
< len
; i
++) {
263 amd_ec_write(smbus
, AMD_SMB_DATA
+ i
,
269 protocol
|= AMD_SMB_PRTCL_BLOCK_DATA
| pec
;
272 case I2C_SMBUS_I2C_BLOCK_DATA
:
273 len
= min_t(u8
, data
->block
[0],
274 I2C_SMBUS_BLOCK_MAX
);
275 status
= amd_ec_write(smbus
, AMD_SMB_CMD
, command
);
278 status
= amd_ec_write(smbus
, AMD_SMB_BCNT
, len
);
281 if (read_write
== I2C_SMBUS_WRITE
)
282 for (i
= 0; i
< len
; i
++) {
284 amd_ec_write(smbus
, AMD_SMB_DATA
+ i
,
289 protocol
|= AMD_SMB_PRTCL_I2C_BLOCK_DATA
;
292 case I2C_SMBUS_PROC_CALL
:
293 status
= amd_ec_write(smbus
, AMD_SMB_CMD
, command
);
296 status
= amd_ec_write(smbus
, AMD_SMB_DATA
,
300 status
= amd_ec_write(smbus
, AMD_SMB_DATA
+ 1,
304 protocol
= AMD_SMB_PRTCL_PROC_CALL
| pec
;
305 read_write
= I2C_SMBUS_READ
;
308 case I2C_SMBUS_BLOCK_PROC_CALL
:
309 len
= min_t(u8
, data
->block
[0],
310 I2C_SMBUS_BLOCK_MAX
- 1);
311 status
= amd_ec_write(smbus
, AMD_SMB_CMD
, command
);
314 status
= amd_ec_write(smbus
, AMD_SMB_BCNT
, len
);
317 for (i
= 0; i
< len
; i
++) {
318 status
= amd_ec_write(smbus
, AMD_SMB_DATA
+ i
,
323 protocol
= AMD_SMB_PRTCL_BLOCK_PROC_CALL
| pec
;
324 read_write
= I2C_SMBUS_READ
;
328 dev_warn(&adap
->dev
, "Unsupported transaction %d\n", size
);
332 status
= amd_ec_write(smbus
, AMD_SMB_ADDR
, addr
<< 1);
335 status
= amd_ec_write(smbus
, AMD_SMB_PRTCL
, protocol
);
339 status
= amd_ec_read(smbus
, AMD_SMB_STS
, temp
+ 0);
343 if (~temp
[0] & AMD_SMB_STS_DONE
) {
345 status
= amd_ec_read(smbus
, AMD_SMB_STS
, temp
+ 0);
350 if (~temp
[0] & AMD_SMB_STS_DONE
) {
352 status
= amd_ec_read(smbus
, AMD_SMB_STS
, temp
+ 0);
357 if ((~temp
[0] & AMD_SMB_STS_DONE
) || (temp
[0] & AMD_SMB_STS_STATUS
))
360 if (read_write
== I2C_SMBUS_WRITE
)
365 case I2C_SMBUS_BYTE_DATA
:
366 status
= amd_ec_read(smbus
, AMD_SMB_DATA
, &data
->byte
);
371 case I2C_SMBUS_WORD_DATA
:
372 case I2C_SMBUS_PROC_CALL
:
373 status
= amd_ec_read(smbus
, AMD_SMB_DATA
, temp
+ 0);
376 status
= amd_ec_read(smbus
, AMD_SMB_DATA
+ 1, temp
+ 1);
379 data
->word
= (temp
[1] << 8) | temp
[0];
382 case I2C_SMBUS_BLOCK_DATA
:
383 case I2C_SMBUS_BLOCK_PROC_CALL
:
384 status
= amd_ec_read(smbus
, AMD_SMB_BCNT
, &len
);
387 len
= min_t(u8
, len
, I2C_SMBUS_BLOCK_MAX
);
388 case I2C_SMBUS_I2C_BLOCK_DATA
:
389 for (i
= 0; i
< len
; i
++) {
390 status
= amd_ec_read(smbus
, AMD_SMB_DATA
+ i
,
391 data
->block
+ i
+ 1);
395 data
->block
[0] = len
;
403 static u32
amd8111_func(struct i2c_adapter
*adapter
)
405 return I2C_FUNC_SMBUS_QUICK
| I2C_FUNC_SMBUS_BYTE
|
406 I2C_FUNC_SMBUS_BYTE_DATA
|
407 I2C_FUNC_SMBUS_WORD_DATA
| I2C_FUNC_SMBUS_BLOCK_DATA
|
408 I2C_FUNC_SMBUS_PROC_CALL
| I2C_FUNC_SMBUS_BLOCK_PROC_CALL
|
409 I2C_FUNC_SMBUS_I2C_BLOCK
| I2C_FUNC_SMBUS_PEC
;
412 static const struct i2c_algorithm smbus_algorithm
= {
413 .smbus_xfer
= amd8111_access
,
414 .functionality
= amd8111_func
,
418 static DEFINE_PCI_DEVICE_TABLE(amd8111_ids
) = {
419 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_8111_SMBUS2
) },
423 MODULE_DEVICE_TABLE (pci
, amd8111_ids
);
425 static int amd8111_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
427 struct amd_smbus
*smbus
;
430 if (!(pci_resource_flags(dev
, 0) & IORESOURCE_IO
))
433 smbus
= kzalloc(sizeof(struct amd_smbus
), GFP_KERNEL
);
438 smbus
->base
= pci_resource_start(dev
, 0);
439 smbus
->size
= pci_resource_len(dev
, 0);
441 error
= acpi_check_resource_conflict(&dev
->resource
[0]);
447 if (!request_region(smbus
->base
, smbus
->size
, amd8111_driver
.name
)) {
452 smbus
->adapter
.owner
= THIS_MODULE
;
453 snprintf(smbus
->adapter
.name
, sizeof(smbus
->adapter
.name
),
454 "SMBus2 AMD8111 adapter at %04x", smbus
->base
);
455 smbus
->adapter
.class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
;
456 smbus
->adapter
.algo
= &smbus_algorithm
;
457 smbus
->adapter
.algo_data
= smbus
;
459 /* set up the sysfs linkage to our parent device */
460 smbus
->adapter
.dev
.parent
= &dev
->dev
;
462 pci_write_config_dword(smbus
->dev
, AMD_PCI_MISC
, 0);
463 error
= i2c_add_adapter(&smbus
->adapter
);
465 goto out_release_region
;
467 pci_set_drvdata(dev
, smbus
);
471 release_region(smbus
->base
, smbus
->size
);
477 static void amd8111_remove(struct pci_dev
*dev
)
479 struct amd_smbus
*smbus
= pci_get_drvdata(dev
);
481 i2c_del_adapter(&smbus
->adapter
);
482 release_region(smbus
->base
, smbus
->size
);
486 static struct pci_driver amd8111_driver
= {
487 .name
= "amd8111_smbus2",
488 .id_table
= amd8111_ids
,
489 .probe
= amd8111_probe
,
490 .remove
= amd8111_remove
,
493 module_pci_driver(amd8111_driver
);