2 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>,
3 Philip Edelbrock <phil@netroedge.com>, and Mark D. Studebaker
5 Copyright (C) 2007 - 2012 Jean Delvare <khali@linux-fr.org>
6 Copyright (C) 2010 Intel Corporation,
7 David Woodhouse <dwmw2@infradead.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 Supports the following Intel I/O Controller Hubs (ICH):
28 region SMBus Block proc. block
29 Chip name PCI ID size PEC buffer call read
30 ----------------------------------------------------------------------
31 82801AA (ICH) 0x2413 16 no no no no
32 82801AB (ICH0) 0x2423 16 no no no no
33 82801BA (ICH2) 0x2443 16 no no no no
34 82801CA (ICH3) 0x2483 32 soft no no no
35 82801DB (ICH4) 0x24c3 32 hard yes no no
36 82801E (ICH5) 0x24d3 32 hard yes yes yes
37 6300ESB 0x25a4 32 hard yes yes yes
38 82801F (ICH6) 0x266a 32 hard yes yes yes
39 6310ESB/6320ESB 0x269b 32 hard yes yes yes
40 82801G (ICH7) 0x27da 32 hard yes yes yes
41 82801H (ICH8) 0x283e 32 hard yes yes yes
42 82801I (ICH9) 0x2930 32 hard yes yes yes
43 EP80579 (Tolapai) 0x5032 32 hard yes yes yes
44 ICH10 0x3a30 32 hard yes yes yes
45 ICH10 0x3a60 32 hard yes yes yes
46 5/3400 Series (PCH) 0x3b30 32 hard yes yes yes
47 6 Series (PCH) 0x1c22 32 hard yes yes yes
48 Patsburg (PCH) 0x1d22 32 hard yes yes yes
49 Patsburg (PCH) IDF 0x1d70 32 hard yes yes yes
50 Patsburg (PCH) IDF 0x1d71 32 hard yes yes yes
51 Patsburg (PCH) IDF 0x1d72 32 hard yes yes yes
52 DH89xxCC (PCH) 0x2330 32 hard yes yes yes
53 Panther Point (PCH) 0x1e22 32 hard yes yes yes
54 Lynx Point (PCH) 0x8c22 32 hard yes yes yes
55 Lynx Point-LP (PCH) 0x9c22 32 hard yes yes yes
57 Features supported by this driver:
61 Block process call transaction no
62 I2C block read transaction yes (doesn't use the block buffer)
64 Interrupt processing yes
66 See the file Documentation/i2c/busses/i2c-i801 for details.
69 #include <linux/interrupt.h>
70 #include <linux/module.h>
71 #include <linux/pci.h>
72 #include <linux/kernel.h>
73 #include <linux/stddef.h>
74 #include <linux/delay.h>
75 #include <linux/ioport.h>
76 #include <linux/init.h>
77 #include <linux/i2c.h>
78 #include <linux/acpi.h>
80 #include <linux/dmi.h>
81 #include <linux/slab.h>
82 #include <linux/wait.h>
84 /* I801 SMBus address offsets */
85 #define SMBHSTSTS(p) (0 + (p)->smba)
86 #define SMBHSTCNT(p) (2 + (p)->smba)
87 #define SMBHSTCMD(p) (3 + (p)->smba)
88 #define SMBHSTADD(p) (4 + (p)->smba)
89 #define SMBHSTDAT0(p) (5 + (p)->smba)
90 #define SMBHSTDAT1(p) (6 + (p)->smba)
91 #define SMBBLKDAT(p) (7 + (p)->smba)
92 #define SMBPEC(p) (8 + (p)->smba) /* ICH3 and later */
93 #define SMBAUXSTS(p) (12 + (p)->smba) /* ICH4 and later */
94 #define SMBAUXCTL(p) (13 + (p)->smba) /* ICH4 and later */
96 /* PCI Address Constants */
98 #define SMBPCISTS 0x006
99 #define SMBHSTCFG 0x040
101 /* Host status bits for SMBPCISTS */
102 #define SMBPCISTS_INTS 0x08
104 /* Host configuration bits for SMBHSTCFG */
105 #define SMBHSTCFG_HST_EN 1
106 #define SMBHSTCFG_SMB_SMI_EN 2
107 #define SMBHSTCFG_I2C_EN 4
109 /* Auxiliary control register bits, ICH4+ only */
110 #define SMBAUXCTL_CRC 1
111 #define SMBAUXCTL_E32B 2
114 #define MAX_RETRIES 400
116 /* I801 command constants */
117 #define I801_QUICK 0x00
118 #define I801_BYTE 0x04
119 #define I801_BYTE_DATA 0x08
120 #define I801_WORD_DATA 0x0C
121 #define I801_PROC_CALL 0x10 /* unimplemented */
122 #define I801_BLOCK_DATA 0x14
123 #define I801_I2C_BLOCK_DATA 0x18 /* ICH5 and later */
125 /* I801 Host Control register bits */
126 #define SMBHSTCNT_INTREN 0x01
127 #define SMBHSTCNT_KILL 0x02
128 #define SMBHSTCNT_LAST_BYTE 0x20
129 #define SMBHSTCNT_START 0x40
130 #define SMBHSTCNT_PEC_EN 0x80 /* ICH3 and later */
132 /* I801 Hosts Status register bits */
133 #define SMBHSTSTS_BYTE_DONE 0x80
134 #define SMBHSTSTS_INUSE_STS 0x40
135 #define SMBHSTSTS_SMBALERT_STS 0x20
136 #define SMBHSTSTS_FAILED 0x10
137 #define SMBHSTSTS_BUS_ERR 0x08
138 #define SMBHSTSTS_DEV_ERR 0x04
139 #define SMBHSTSTS_INTR 0x02
140 #define SMBHSTSTS_HOST_BUSY 0x01
142 #define STATUS_ERROR_FLAGS (SMBHSTSTS_FAILED | SMBHSTSTS_BUS_ERR | \
145 #define STATUS_FLAGS (SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INTR | \
148 /* Older devices have their ID defined in <linux/pci_ids.h> */
149 #define PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS 0x1c22
150 #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS 0x1d22
151 /* Patsburg also has three 'Integrated Device Function' SMBus controllers */
152 #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0 0x1d70
153 #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1 0x1d71
154 #define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2 0x1d72
155 #define PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS 0x1e22
156 #define PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS 0x2330
157 #define PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS 0x3b30
158 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS 0x8c22
159 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22
162 struct i2c_adapter adapter
;
164 unsigned char original_hstcfg
;
165 struct pci_dev
*pci_dev
;
166 unsigned int features
;
169 wait_queue_head_t waitq
;
172 /* Command state used by isr for byte-by-byte block transactions */
180 static struct pci_driver i801_driver
;
182 #define FEATURE_SMBUS_PEC (1 << 0)
183 #define FEATURE_BLOCK_BUFFER (1 << 1)
184 #define FEATURE_BLOCK_PROC (1 << 2)
185 #define FEATURE_I2C_BLOCK_READ (1 << 3)
186 #define FEATURE_IRQ (1 << 4)
187 /* Not really a feature, but it's convenient to handle it as such */
188 #define FEATURE_IDF (1 << 15)
190 static const char *i801_feature_names
[] = {
193 "Block process call",
198 static unsigned int disable_features
;
199 module_param(disable_features
, uint
, S_IRUGO
| S_IWUSR
);
200 MODULE_PARM_DESC(disable_features
, "Disable selected driver features");
202 /* Make sure the SMBus host is ready to start transmitting.
203 Return 0 if it is, -EBUSY if it is not. */
204 static int i801_check_pre(struct i801_priv
*priv
)
208 status
= inb_p(SMBHSTSTS(priv
));
209 if (status
& SMBHSTSTS_HOST_BUSY
) {
210 dev_err(&priv
->pci_dev
->dev
, "SMBus is busy, can't use it!\n");
214 status
&= STATUS_FLAGS
;
216 dev_dbg(&priv
->pci_dev
->dev
, "Clearing status flags (%02x)\n",
218 outb_p(status
, SMBHSTSTS(priv
));
219 status
= inb_p(SMBHSTSTS(priv
)) & STATUS_FLAGS
;
221 dev_err(&priv
->pci_dev
->dev
,
222 "Failed clearing status flags (%02x)\n",
232 * Convert the status register to an error code, and clear it.
233 * Note that status only contains the bits we want to clear, not the
234 * actual register value.
236 static int i801_check_post(struct i801_priv
*priv
, int status
)
241 * If the SMBus is still busy, we give up
242 * Note: This timeout condition only happens when using polling
243 * transactions. For interrupt operation, NAK/timeout is indicated by
246 if (unlikely(status
< 0)) {
247 dev_err(&priv
->pci_dev
->dev
, "Transaction timeout\n");
248 /* try to stop the current command */
249 dev_dbg(&priv
->pci_dev
->dev
, "Terminating the current operation\n");
250 outb_p(inb_p(SMBHSTCNT(priv
)) | SMBHSTCNT_KILL
,
252 usleep_range(1000, 2000);
253 outb_p(inb_p(SMBHSTCNT(priv
)) & (~SMBHSTCNT_KILL
),
256 /* Check if it worked */
257 status
= inb_p(SMBHSTSTS(priv
));
258 if ((status
& SMBHSTSTS_HOST_BUSY
) ||
259 !(status
& SMBHSTSTS_FAILED
))
260 dev_err(&priv
->pci_dev
->dev
,
261 "Failed terminating the transaction\n");
262 outb_p(STATUS_FLAGS
, SMBHSTSTS(priv
));
266 if (status
& SMBHSTSTS_FAILED
) {
268 dev_err(&priv
->pci_dev
->dev
, "Transaction failed\n");
270 if (status
& SMBHSTSTS_DEV_ERR
) {
272 dev_dbg(&priv
->pci_dev
->dev
, "No response\n");
274 if (status
& SMBHSTSTS_BUS_ERR
) {
276 dev_dbg(&priv
->pci_dev
->dev
, "Lost arbitration\n");
279 /* Clear status flags except BYTE_DONE, to be cleared by caller */
280 outb_p(status
, SMBHSTSTS(priv
));
285 /* Wait for BUSY being cleared and either INTR or an error flag being set */
286 static int i801_wait_intr(struct i801_priv
*priv
)
291 /* We will always wait for a fraction of a second! */
293 usleep_range(250, 500);
294 status
= inb_p(SMBHSTSTS(priv
));
295 } while (((status
& SMBHSTSTS_HOST_BUSY
) ||
296 !(status
& (STATUS_ERROR_FLAGS
| SMBHSTSTS_INTR
))) &&
297 (timeout
++ < MAX_RETRIES
));
299 if (timeout
> MAX_RETRIES
) {
300 dev_dbg(&priv
->pci_dev
->dev
, "INTR Timeout!\n");
303 return status
& (STATUS_ERROR_FLAGS
| SMBHSTSTS_INTR
);
306 /* Wait for either BYTE_DONE or an error flag being set */
307 static int i801_wait_byte_done(struct i801_priv
*priv
)
312 /* We will always wait for a fraction of a second! */
314 usleep_range(250, 500);
315 status
= inb_p(SMBHSTSTS(priv
));
316 } while (!(status
& (STATUS_ERROR_FLAGS
| SMBHSTSTS_BYTE_DONE
)) &&
317 (timeout
++ < MAX_RETRIES
));
319 if (timeout
> MAX_RETRIES
) {
320 dev_dbg(&priv
->pci_dev
->dev
, "BYTE_DONE Timeout!\n");
323 return status
& STATUS_ERROR_FLAGS
;
326 static int i801_transaction(struct i801_priv
*priv
, int xact
)
331 result
= i801_check_pre(priv
);
335 if (priv
->features
& FEATURE_IRQ
) {
336 outb_p(xact
| SMBHSTCNT_INTREN
| SMBHSTCNT_START
,
338 wait_event(priv
->waitq
, (status
= priv
->status
));
340 return i801_check_post(priv
, status
);
343 /* the current contents of SMBHSTCNT can be overwritten, since PEC,
344 * SMBSCMD are passed in xact */
345 outb_p(xact
| SMBHSTCNT_START
, SMBHSTCNT(priv
));
347 status
= i801_wait_intr(priv
);
348 return i801_check_post(priv
, status
);
351 static int i801_block_transaction_by_block(struct i801_priv
*priv
,
352 union i2c_smbus_data
*data
,
353 char read_write
, int hwpec
)
358 inb_p(SMBHSTCNT(priv
)); /* reset the data buffer index */
360 /* Use 32-byte buffer to process this transaction */
361 if (read_write
== I2C_SMBUS_WRITE
) {
362 len
= data
->block
[0];
363 outb_p(len
, SMBHSTDAT0(priv
));
364 for (i
= 0; i
< len
; i
++)
365 outb_p(data
->block
[i
+1], SMBBLKDAT(priv
));
368 status
= i801_transaction(priv
, I801_BLOCK_DATA
|
369 (hwpec
? SMBHSTCNT_PEC_EN
: 0));
373 if (read_write
== I2C_SMBUS_READ
) {
374 len
= inb_p(SMBHSTDAT0(priv
));
375 if (len
< 1 || len
> I2C_SMBUS_BLOCK_MAX
)
378 data
->block
[0] = len
;
379 for (i
= 0; i
< len
; i
++)
380 data
->block
[i
+ 1] = inb_p(SMBBLKDAT(priv
));
385 static void i801_isr_byte_done(struct i801_priv
*priv
)
388 /* For SMBus block reads, length is received with first byte */
389 if (((priv
->cmd
& 0x1c) == I801_BLOCK_DATA
) &&
390 (priv
->count
== 0)) {
391 priv
->len
= inb_p(SMBHSTDAT0(priv
));
392 if (priv
->len
< 1 || priv
->len
> I2C_SMBUS_BLOCK_MAX
) {
393 dev_err(&priv
->pci_dev
->dev
,
394 "Illegal SMBus block read size %d\n",
397 priv
->len
= I2C_SMBUS_BLOCK_MAX
;
399 dev_dbg(&priv
->pci_dev
->dev
,
400 "SMBus block read size is %d\n",
403 priv
->data
[-1] = priv
->len
;
407 if (priv
->count
< priv
->len
)
408 priv
->data
[priv
->count
++] = inb(SMBBLKDAT(priv
));
410 dev_dbg(&priv
->pci_dev
->dev
,
411 "Discarding extra byte on block read\n");
413 /* Set LAST_BYTE for last byte of read transaction */
414 if (priv
->count
== priv
->len
- 1)
415 outb_p(priv
->cmd
| SMBHSTCNT_LAST_BYTE
,
417 } else if (priv
->count
< priv
->len
- 1) {
418 /* Write next byte, except for IRQ after last byte */
419 outb_p(priv
->data
[++priv
->count
], SMBBLKDAT(priv
));
422 /* Clear BYTE_DONE to continue with next byte */
423 outb_p(SMBHSTSTS_BYTE_DONE
, SMBHSTSTS(priv
));
427 * There are two kinds of interrupts:
429 * 1) i801 signals transaction completion with one of these interrupts:
431 * DEV_ERR - Invalid command, NAK or communication timeout
432 * BUS_ERR - SMI# transaction collision
433 * FAILED - transaction was canceled due to a KILL request
434 * When any of these occur, update ->status and wake up the waitq.
435 * ->status must be cleared before kicking off the next transaction.
437 * 2) For byte-by-byte (I2C read/write) transactions, one BYTE_DONE interrupt
438 * occurs for each byte of a byte-by-byte to prepare the next byte.
440 static irqreturn_t
i801_isr(int irq
, void *dev_id
)
442 struct i801_priv
*priv
= dev_id
;
446 /* Confirm this is our interrupt */
447 pci_read_config_word(priv
->pci_dev
, SMBPCISTS
, &pcists
);
448 if (!(pcists
& SMBPCISTS_INTS
))
451 status
= inb_p(SMBHSTSTS(priv
));
453 dev_dbg(&priv
->pci_dev
->dev
, "irq: status = %02x\n", status
);
455 if (status
& SMBHSTSTS_BYTE_DONE
)
456 i801_isr_byte_done(priv
);
459 * Clear irq sources and report transaction result.
460 * ->status must be cleared before the next transaction is started.
462 status
&= SMBHSTSTS_INTR
| STATUS_ERROR_FLAGS
;
464 outb_p(status
, SMBHSTSTS(priv
));
465 priv
->status
|= status
;
466 wake_up(&priv
->waitq
);
473 * For "byte-by-byte" block transactions:
474 * I2C write uses cmd=I801_BLOCK_DATA, I2C_EN=1
475 * I2C read uses cmd=I801_I2C_BLOCK_DATA
477 static int i801_block_transaction_byte_by_byte(struct i801_priv
*priv
,
478 union i2c_smbus_data
*data
,
479 char read_write
, int command
,
487 result
= i801_check_pre(priv
);
491 len
= data
->block
[0];
493 if (read_write
== I2C_SMBUS_WRITE
) {
494 outb_p(len
, SMBHSTDAT0(priv
));
495 outb_p(data
->block
[1], SMBBLKDAT(priv
));
498 if (command
== I2C_SMBUS_I2C_BLOCK_DATA
&&
499 read_write
== I2C_SMBUS_READ
)
500 smbcmd
= I801_I2C_BLOCK_DATA
;
502 smbcmd
= I801_BLOCK_DATA
;
504 if (priv
->features
& FEATURE_IRQ
) {
505 priv
->is_read
= (read_write
== I2C_SMBUS_READ
);
506 if (len
== 1 && priv
->is_read
)
507 smbcmd
|= SMBHSTCNT_LAST_BYTE
;
508 priv
->cmd
= smbcmd
| SMBHSTCNT_INTREN
;
511 priv
->data
= &data
->block
[1];
513 outb_p(priv
->cmd
| SMBHSTCNT_START
, SMBHSTCNT(priv
));
514 wait_event(priv
->waitq
, (status
= priv
->status
));
516 return i801_check_post(priv
, status
);
519 for (i
= 1; i
<= len
; i
++) {
520 if (i
== len
&& read_write
== I2C_SMBUS_READ
)
521 smbcmd
|= SMBHSTCNT_LAST_BYTE
;
522 outb_p(smbcmd
, SMBHSTCNT(priv
));
525 outb_p(inb(SMBHSTCNT(priv
)) | SMBHSTCNT_START
,
528 status
= i801_wait_byte_done(priv
);
532 if (i
== 1 && read_write
== I2C_SMBUS_READ
533 && command
!= I2C_SMBUS_I2C_BLOCK_DATA
) {
534 len
= inb_p(SMBHSTDAT0(priv
));
535 if (len
< 1 || len
> I2C_SMBUS_BLOCK_MAX
) {
536 dev_err(&priv
->pci_dev
->dev
,
537 "Illegal SMBus block read size %d\n",
540 while (inb_p(SMBHSTSTS(priv
)) &
542 outb_p(SMBHSTSTS_BYTE_DONE
,
544 outb_p(SMBHSTSTS_INTR
, SMBHSTSTS(priv
));
547 data
->block
[0] = len
;
550 /* Retrieve/store value in SMBBLKDAT */
551 if (read_write
== I2C_SMBUS_READ
)
552 data
->block
[i
] = inb_p(SMBBLKDAT(priv
));
553 if (read_write
== I2C_SMBUS_WRITE
&& i
+1 <= len
)
554 outb_p(data
->block
[i
+1], SMBBLKDAT(priv
));
556 /* signals SMBBLKDAT ready */
557 outb_p(SMBHSTSTS_BYTE_DONE
, SMBHSTSTS(priv
));
560 status
= i801_wait_intr(priv
);
562 return i801_check_post(priv
, status
);
565 static int i801_set_block_buffer_mode(struct i801_priv
*priv
)
567 outb_p(inb_p(SMBAUXCTL(priv
)) | SMBAUXCTL_E32B
, SMBAUXCTL(priv
));
568 if ((inb_p(SMBAUXCTL(priv
)) & SMBAUXCTL_E32B
) == 0)
573 /* Block transaction function */
574 static int i801_block_transaction(struct i801_priv
*priv
,
575 union i2c_smbus_data
*data
, char read_write
,
576 int command
, int hwpec
)
581 if (command
== I2C_SMBUS_I2C_BLOCK_DATA
) {
582 if (read_write
== I2C_SMBUS_WRITE
) {
583 /* set I2C_EN bit in configuration register */
584 pci_read_config_byte(priv
->pci_dev
, SMBHSTCFG
, &hostc
);
585 pci_write_config_byte(priv
->pci_dev
, SMBHSTCFG
,
586 hostc
| SMBHSTCFG_I2C_EN
);
587 } else if (!(priv
->features
& FEATURE_I2C_BLOCK_READ
)) {
588 dev_err(&priv
->pci_dev
->dev
,
589 "I2C block read is unsupported!\n");
594 if (read_write
== I2C_SMBUS_WRITE
595 || command
== I2C_SMBUS_I2C_BLOCK_DATA
) {
596 if (data
->block
[0] < 1)
598 if (data
->block
[0] > I2C_SMBUS_BLOCK_MAX
)
599 data
->block
[0] = I2C_SMBUS_BLOCK_MAX
;
601 data
->block
[0] = 32; /* max for SMBus block reads */
604 /* Experience has shown that the block buffer can only be used for
605 SMBus (not I2C) block transactions, even though the datasheet
606 doesn't mention this limitation. */
607 if ((priv
->features
& FEATURE_BLOCK_BUFFER
)
608 && command
!= I2C_SMBUS_I2C_BLOCK_DATA
609 && i801_set_block_buffer_mode(priv
) == 0)
610 result
= i801_block_transaction_by_block(priv
, data
,
613 result
= i801_block_transaction_byte_by_byte(priv
, data
,
617 if (command
== I2C_SMBUS_I2C_BLOCK_DATA
618 && read_write
== I2C_SMBUS_WRITE
) {
619 /* restore saved configuration register value */
620 pci_write_config_byte(priv
->pci_dev
, SMBHSTCFG
, hostc
);
625 /* Return negative errno on error. */
626 static s32
i801_access(struct i2c_adapter
*adap
, u16 addr
,
627 unsigned short flags
, char read_write
, u8 command
,
628 int size
, union i2c_smbus_data
*data
)
633 struct i801_priv
*priv
= i2c_get_adapdata(adap
);
635 hwpec
= (priv
->features
& FEATURE_SMBUS_PEC
) && (flags
& I2C_CLIENT_PEC
)
636 && size
!= I2C_SMBUS_QUICK
637 && size
!= I2C_SMBUS_I2C_BLOCK_DATA
;
640 case I2C_SMBUS_QUICK
:
641 outb_p(((addr
& 0x7f) << 1) | (read_write
& 0x01),
646 outb_p(((addr
& 0x7f) << 1) | (read_write
& 0x01),
648 if (read_write
== I2C_SMBUS_WRITE
)
649 outb_p(command
, SMBHSTCMD(priv
));
652 case I2C_SMBUS_BYTE_DATA
:
653 outb_p(((addr
& 0x7f) << 1) | (read_write
& 0x01),
655 outb_p(command
, SMBHSTCMD(priv
));
656 if (read_write
== I2C_SMBUS_WRITE
)
657 outb_p(data
->byte
, SMBHSTDAT0(priv
));
658 xact
= I801_BYTE_DATA
;
660 case I2C_SMBUS_WORD_DATA
:
661 outb_p(((addr
& 0x7f) << 1) | (read_write
& 0x01),
663 outb_p(command
, SMBHSTCMD(priv
));
664 if (read_write
== I2C_SMBUS_WRITE
) {
665 outb_p(data
->word
& 0xff, SMBHSTDAT0(priv
));
666 outb_p((data
->word
& 0xff00) >> 8, SMBHSTDAT1(priv
));
668 xact
= I801_WORD_DATA
;
670 case I2C_SMBUS_BLOCK_DATA
:
671 outb_p(((addr
& 0x7f) << 1) | (read_write
& 0x01),
673 outb_p(command
, SMBHSTCMD(priv
));
676 case I2C_SMBUS_I2C_BLOCK_DATA
:
677 /* NB: page 240 of ICH5 datasheet shows that the R/#W
678 * bit should be cleared here, even when reading */
679 outb_p((addr
& 0x7f) << 1, SMBHSTADD(priv
));
680 if (read_write
== I2C_SMBUS_READ
) {
681 /* NB: page 240 of ICH5 datasheet also shows
682 * that DATA1 is the cmd field when reading */
683 outb_p(command
, SMBHSTDAT1(priv
));
685 outb_p(command
, SMBHSTCMD(priv
));
689 dev_err(&priv
->pci_dev
->dev
, "Unsupported transaction %d\n",
694 if (hwpec
) /* enable/disable hardware PEC */
695 outb_p(inb_p(SMBAUXCTL(priv
)) | SMBAUXCTL_CRC
, SMBAUXCTL(priv
));
697 outb_p(inb_p(SMBAUXCTL(priv
)) & (~SMBAUXCTL_CRC
),
701 ret
= i801_block_transaction(priv
, data
, read_write
, size
,
704 ret
= i801_transaction(priv
, xact
);
706 /* Some BIOSes don't like it when PEC is enabled at reboot or resume
707 time, so we forcibly disable it after every transaction. Turn off
708 E32B for the same reason. */
710 outb_p(inb_p(SMBAUXCTL(priv
)) &
711 ~(SMBAUXCTL_CRC
| SMBAUXCTL_E32B
), SMBAUXCTL(priv
));
717 if ((read_write
== I2C_SMBUS_WRITE
) || (xact
== I801_QUICK
))
720 switch (xact
& 0x7f) {
721 case I801_BYTE
: /* Result put in SMBHSTDAT0 */
723 data
->byte
= inb_p(SMBHSTDAT0(priv
));
726 data
->word
= inb_p(SMBHSTDAT0(priv
)) +
727 (inb_p(SMBHSTDAT1(priv
)) << 8);
734 static u32
i801_func(struct i2c_adapter
*adapter
)
736 struct i801_priv
*priv
= i2c_get_adapdata(adapter
);
738 return I2C_FUNC_SMBUS_QUICK
| I2C_FUNC_SMBUS_BYTE
|
739 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
|
740 I2C_FUNC_SMBUS_BLOCK_DATA
| I2C_FUNC_SMBUS_WRITE_I2C_BLOCK
|
741 ((priv
->features
& FEATURE_SMBUS_PEC
) ? I2C_FUNC_SMBUS_PEC
: 0) |
742 ((priv
->features
& FEATURE_I2C_BLOCK_READ
) ?
743 I2C_FUNC_SMBUS_READ_I2C_BLOCK
: 0);
746 static const struct i2c_algorithm smbus_algorithm
= {
747 .smbus_xfer
= i801_access
,
748 .functionality
= i801_func
,
751 static DEFINE_PCI_DEVICE_TABLE(i801_ids
) = {
752 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82801AA_3
) },
753 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82801AB_3
) },
754 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82801BA_2
) },
755 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82801CA_3
) },
756 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82801DB_3
) },
757 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82801EB_3
) },
758 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_ESB_4
) },
759 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_ICH6_16
) },
760 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_ICH7_17
) },
761 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_ESB2_17
) },
762 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_ICH8_5
) },
763 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_ICH9_6
) },
764 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_EP80579_1
) },
765 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_ICH10_4
) },
766 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_ICH10_5
) },
767 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS
) },
768 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS
) },
769 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS
) },
770 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0
) },
771 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1
) },
772 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2
) },
773 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS
) },
774 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS
) },
775 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS
) },
776 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS
) },
780 MODULE_DEVICE_TABLE(pci
, i801_ids
);
782 #if defined CONFIG_X86 && defined CONFIG_DMI
783 static unsigned char apanel_addr
;
785 /* Scan the system ROM for the signature "FJKEYINF" */
786 static __init
const void __iomem
*bios_signature(const void __iomem
*bios
)
789 const unsigned char signature
[] = "FJKEYINF";
791 for (offset
= 0; offset
< 0x10000; offset
+= 0x10) {
792 if (check_signature(bios
+ offset
, signature
,
793 sizeof(signature
)-1))
794 return bios
+ offset
;
799 static void __init
input_apanel_init(void)
802 const void __iomem
*p
;
804 bios
= ioremap(0xF0000, 0x10000); /* Can't fail */
805 p
= bios_signature(bios
);
807 /* just use the first address */
808 apanel_addr
= readb(p
+ 8 + 3) >> 1;
813 struct dmi_onboard_device_info
{
816 unsigned short i2c_addr
;
817 const char *i2c_type
;
820 static struct dmi_onboard_device_info __devinitdata dmi_devices
[] = {
821 { "Syleus", DMI_DEV_TYPE_OTHER
, 0x73, "fscsyl" },
822 { "Hermes", DMI_DEV_TYPE_OTHER
, 0x73, "fscher" },
823 { "Hades", DMI_DEV_TYPE_OTHER
, 0x73, "fschds" },
826 static void __devinit
dmi_check_onboard_device(u8 type
, const char *name
,
827 struct i2c_adapter
*adap
)
830 struct i2c_board_info info
;
832 for (i
= 0; i
< ARRAY_SIZE(dmi_devices
); i
++) {
833 /* & ~0x80, ignore enabled/disabled bit */
834 if ((type
& ~0x80) != dmi_devices
[i
].type
)
836 if (strcasecmp(name
, dmi_devices
[i
].name
))
839 memset(&info
, 0, sizeof(struct i2c_board_info
));
840 info
.addr
= dmi_devices
[i
].i2c_addr
;
841 strlcpy(info
.type
, dmi_devices
[i
].i2c_type
, I2C_NAME_SIZE
);
842 i2c_new_device(adap
, &info
);
847 /* We use our own function to check for onboard devices instead of
848 dmi_find_device() as some buggy BIOS's have the devices we are interested
849 in marked as disabled */
850 static void __devinit
dmi_check_onboard_devices(const struct dmi_header
*dm
,
858 count
= (dm
->length
- sizeof(struct dmi_header
)) / 2;
859 for (i
= 0; i
< count
; i
++) {
860 const u8
*d
= (char *)(dm
+ 1) + (i
* 2);
861 const char *name
= ((char *) dm
) + dm
->length
;
868 while (s
> 0 && name
[0]) {
869 name
+= strlen(name
) + 1;
872 if (name
[0] == 0) /* Bogus string reference */
875 dmi_check_onboard_device(type
, name
, adap
);
879 /* Register optional slaves */
880 static void __devinit
i801_probe_optional_slaves(struct i801_priv
*priv
)
882 /* Only register slaves on main SMBus channel */
883 if (priv
->features
& FEATURE_IDF
)
887 struct i2c_board_info info
;
889 memset(&info
, 0, sizeof(struct i2c_board_info
));
890 info
.addr
= apanel_addr
;
891 strlcpy(info
.type
, "fujitsu_apanel", I2C_NAME_SIZE
);
892 i2c_new_device(&priv
->adapter
, &info
);
895 if (dmi_name_in_vendors("FUJITSU"))
896 dmi_walk(dmi_check_onboard_devices
, &priv
->adapter
);
899 static void __init
input_apanel_init(void) {}
900 static void __devinit
i801_probe_optional_slaves(struct i801_priv
*priv
) {}
901 #endif /* CONFIG_X86 && CONFIG_DMI */
903 static int __devinit
i801_probe(struct pci_dev
*dev
,
904 const struct pci_device_id
*id
)
908 struct i801_priv
*priv
;
910 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
914 i2c_set_adapdata(&priv
->adapter
, priv
);
915 priv
->adapter
.owner
= THIS_MODULE
;
916 priv
->adapter
.class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
;
917 priv
->adapter
.algo
= &smbus_algorithm
;
920 switch (dev
->device
) {
921 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF0
:
922 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF1
:
923 case PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2
:
924 priv
->features
|= FEATURE_IDF
;
927 priv
->features
|= FEATURE_I2C_BLOCK_READ
;
929 case PCI_DEVICE_ID_INTEL_82801DB_3
:
930 priv
->features
|= FEATURE_SMBUS_PEC
;
931 priv
->features
|= FEATURE_BLOCK_BUFFER
;
933 case PCI_DEVICE_ID_INTEL_82801CA_3
:
934 case PCI_DEVICE_ID_INTEL_82801BA_2
:
935 case PCI_DEVICE_ID_INTEL_82801AB_3
:
936 case PCI_DEVICE_ID_INTEL_82801AA_3
:
940 /* IRQ processing tested on CougarPoint PCH, ICH5, ICH7-M and ICH10 */
941 if (dev
->device
== PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS
||
942 dev
->device
== PCI_DEVICE_ID_INTEL_82801EB_3
||
943 dev
->device
== PCI_DEVICE_ID_INTEL_ICH7_17
||
944 dev
->device
== PCI_DEVICE_ID_INTEL_ICH8_5
||
945 dev
->device
== PCI_DEVICE_ID_INTEL_ICH9_6
||
946 dev
->device
== PCI_DEVICE_ID_INTEL_ICH10_4
||
947 dev
->device
== PCI_DEVICE_ID_INTEL_ICH10_5
)
948 priv
->features
|= FEATURE_IRQ
;
950 /* Disable features on user request */
951 for (i
= 0; i
< ARRAY_SIZE(i801_feature_names
); i
++) {
952 if (priv
->features
& disable_features
& (1 << i
))
953 dev_notice(&dev
->dev
, "%s disabled by user\n",
954 i801_feature_names
[i
]);
956 priv
->features
&= ~disable_features
;
958 err
= pci_enable_device(dev
);
960 dev_err(&dev
->dev
, "Failed to enable SMBus PCI device (%d)\n",
965 /* Determine the address of the SMBus area */
966 priv
->smba
= pci_resource_start(dev
, SMBBAR
);
968 dev_err(&dev
->dev
, "SMBus base address uninitialized, "
974 err
= acpi_check_resource_conflict(&dev
->resource
[SMBBAR
]);
980 err
= pci_request_region(dev
, SMBBAR
, i801_driver
.name
);
982 dev_err(&dev
->dev
, "Failed to request SMBus region "
983 "0x%lx-0x%Lx\n", priv
->smba
,
984 (unsigned long long)pci_resource_end(dev
, SMBBAR
));
988 pci_read_config_byte(priv
->pci_dev
, SMBHSTCFG
, &temp
);
989 priv
->original_hstcfg
= temp
;
990 temp
&= ~SMBHSTCFG_I2C_EN
; /* SMBus timing */
991 if (!(temp
& SMBHSTCFG_HST_EN
)) {
992 dev_info(&dev
->dev
, "Enabling SMBus device\n");
993 temp
|= SMBHSTCFG_HST_EN
;
995 pci_write_config_byte(priv
->pci_dev
, SMBHSTCFG
, temp
);
997 if (temp
& SMBHSTCFG_SMB_SMI_EN
) {
998 dev_dbg(&dev
->dev
, "SMBus using interrupt SMI#\n");
999 /* Disable SMBus interrupt feature if SMBus using SMI# */
1000 priv
->features
&= ~FEATURE_IRQ
;
1003 /* Clear special mode bits */
1004 if (priv
->features
& (FEATURE_SMBUS_PEC
| FEATURE_BLOCK_BUFFER
))
1005 outb_p(inb_p(SMBAUXCTL(priv
)) &
1006 ~(SMBAUXCTL_CRC
| SMBAUXCTL_E32B
), SMBAUXCTL(priv
));
1008 if (priv
->features
& FEATURE_IRQ
) {
1009 init_waitqueue_head(&priv
->waitq
);
1011 err
= request_irq(dev
->irq
, i801_isr
, IRQF_SHARED
,
1012 i801_driver
.name
, priv
);
1014 dev_err(&dev
->dev
, "Failed to allocate irq %d: %d\n",
1018 dev_info(&dev
->dev
, "SMBus using PCI Interrupt\n");
1021 /* set up the sysfs linkage to our parent device */
1022 priv
->adapter
.dev
.parent
= &dev
->dev
;
1024 /* Retry up to 3 times on lost arbitration */
1025 priv
->adapter
.retries
= 3;
1027 snprintf(priv
->adapter
.name
, sizeof(priv
->adapter
.name
),
1028 "SMBus I801 adapter at %04lx", priv
->smba
);
1029 err
= i2c_add_adapter(&priv
->adapter
);
1031 dev_err(&dev
->dev
, "Failed to add SMBus adapter\n");
1035 i801_probe_optional_slaves(priv
);
1037 pci_set_drvdata(dev
, priv
);
1042 if (priv
->features
& FEATURE_IRQ
)
1043 free_irq(dev
->irq
, priv
);
1045 pci_release_region(dev
, SMBBAR
);
1051 static void __devexit
i801_remove(struct pci_dev
*dev
)
1053 struct i801_priv
*priv
= pci_get_drvdata(dev
);
1055 i2c_del_adapter(&priv
->adapter
);
1056 pci_write_config_byte(dev
, SMBHSTCFG
, priv
->original_hstcfg
);
1058 if (priv
->features
& FEATURE_IRQ
)
1059 free_irq(dev
->irq
, priv
);
1060 pci_release_region(dev
, SMBBAR
);
1062 pci_set_drvdata(dev
, NULL
);
1065 * do not call pci_disable_device(dev) since it can cause hard hangs on
1066 * some systems during power-off (eg. Fujitsu-Siemens Lifebook E8010)
1071 static int i801_suspend(struct pci_dev
*dev
, pm_message_t mesg
)
1073 struct i801_priv
*priv
= pci_get_drvdata(dev
);
1075 pci_save_state(dev
);
1076 pci_write_config_byte(dev
, SMBHSTCFG
, priv
->original_hstcfg
);
1077 pci_set_power_state(dev
, pci_choose_state(dev
, mesg
));
1081 static int i801_resume(struct pci_dev
*dev
)
1083 pci_set_power_state(dev
, PCI_D0
);
1084 pci_restore_state(dev
);
1085 return pci_enable_device(dev
);
1088 #define i801_suspend NULL
1089 #define i801_resume NULL
1092 static struct pci_driver i801_driver
= {
1093 .name
= "i801_smbus",
1094 .id_table
= i801_ids
,
1095 .probe
= i801_probe
,
1096 .remove
= __devexit_p(i801_remove
),
1097 .suspend
= i801_suspend
,
1098 .resume
= i801_resume
,
1101 static int __init
i2c_i801_init(void)
1103 if (dmi_name_in_vendors("FUJITSU"))
1104 input_apanel_init();
1105 return pci_register_driver(&i801_driver
);
1108 static void __exit
i2c_i801_exit(void)
1110 pci_unregister_driver(&i801_driver
);
1113 MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>, "
1114 "Jean Delvare <khali@linux-fr.org>");
1115 MODULE_DESCRIPTION("I801 SMBus driver");
1116 MODULE_LICENSE("GPL");
1118 module_init(i2c_i801_init
);
1119 module_exit(i2c_i801_exit
);