blob: dc7ea32b69a8f64c3a09725cd54748fb8c95b9c8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>,
3 Philip Edelbrock <phil@netroedge.com>, and Mark D. Studebaker
4 <mdsxyz123@yahoo.com>
Jean Delvarecf898dc2008-07-14 22:38:33 +02005 Copyright (C) 2007, 2008 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22/*
Jean Delvareae7b0492008-01-27 18:14:49 +010023 Supports the following Intel I/O Controller Hubs (ICH):
24
25 I/O Block I2C
26 region SMBus Block proc. block
27 Chip name PCI ID size PEC buffer call read
28 ----------------------------------------------------------------------
29 82801AA (ICH) 0x2413 16 no no no no
30 82801AB (ICH0) 0x2423 16 no no no no
31 82801BA (ICH2) 0x2443 16 no no no no
32 82801CA (ICH3) 0x2483 32 soft no no no
33 82801DB (ICH4) 0x24c3 32 hard yes no no
34 82801E (ICH5) 0x24d3 32 hard yes yes yes
35 6300ESB 0x25a4 32 hard yes yes yes
36 82801F (ICH6) 0x266a 32 hard yes yes yes
37 6310ESB/6320ESB 0x269b 32 hard yes yes yes
38 82801G (ICH7) 0x27da 32 hard yes yes yes
39 82801H (ICH8) 0x283e 32 hard yes yes yes
40 82801I (ICH9) 0x2930 32 hard yes yes yes
Gaston, Jason Dd28dc712008-02-24 20:03:42 +010041 Tolapai 0x5032 32 hard yes yes yes
42 ICH10 0x3a30 32 hard yes yes yes
43 ICH10 0x3a60 32 hard yes yes yes
Jean Delvareae7b0492008-01-27 18:14:49 +010044
45 Features supported by this driver:
46 Software PEC no
47 Hardware PEC yes
48 Block buffer yes
49 Block process call transaction no
Jean Delvare63420642008-01-27 18:14:50 +010050 I2C block read transaction yes (doesn't use the block buffer)
Jean Delvareae7b0492008-01-27 18:14:49 +010051
52 See the file Documentation/i2c/busses/i2c-i801 for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070053*/
54
55/* Note: we assume there can only be one I801, with one SMBus interface */
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/module.h>
58#include <linux/pci.h>
59#include <linux/kernel.h>
60#include <linux/stddef.h>
61#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/ioport.h>
63#include <linux/init.h>
64#include <linux/i2c.h>
Jean Delvare54fb4a052008-07-14 22:38:33 +020065#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <asm/io.h>
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* I801 SMBus address offsets */
69#define SMBHSTSTS (0 + i801_smba)
70#define SMBHSTCNT (2 + i801_smba)
71#define SMBHSTCMD (3 + i801_smba)
72#define SMBHSTADD (4 + i801_smba)
73#define SMBHSTDAT0 (5 + i801_smba)
74#define SMBHSTDAT1 (6 + i801_smba)
75#define SMBBLKDAT (7 + i801_smba)
Jean Delvareae7b0492008-01-27 18:14:49 +010076#define SMBPEC (8 + i801_smba) /* ICH3 and later */
77#define SMBAUXSTS (12 + i801_smba) /* ICH4 and later */
78#define SMBAUXCTL (13 + i801_smba) /* ICH4 and later */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* PCI Address Constants */
Jean Delvare6dcc19d2006-06-12 21:53:02 +020081#define SMBBAR 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define SMBHSTCFG 0x040
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/* Host configuration bits for SMBHSTCFG */
85#define SMBHSTCFG_HST_EN 1
86#define SMBHSTCFG_SMB_SMI_EN 2
87#define SMBHSTCFG_I2C_EN 4
88
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +020089/* Auxillary control register bits, ICH4+ only */
90#define SMBAUXCTL_CRC 1
91#define SMBAUXCTL_E32B 2
92
93/* kill bit for SMBHSTCNT */
94#define SMBHSTCNT_KILL 2
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/* Other settings */
97#define MAX_TIMEOUT 100
98#define ENABLE_INT9 0 /* set to 0x01 to enable - untested */
99
100/* I801 command constants */
101#define I801_QUICK 0x00
102#define I801_BYTE 0x04
103#define I801_BYTE_DATA 0x08
104#define I801_WORD_DATA 0x0C
Jean Delvareae7b0492008-01-27 18:14:49 +0100105#define I801_PROC_CALL 0x10 /* unimplemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define I801_BLOCK_DATA 0x14
Jean Delvare63420642008-01-27 18:14:50 +0100107#define I801_I2C_BLOCK_DATA 0x18 /* ICH5 and later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define I801_BLOCK_LAST 0x34
Jean Delvare63420642008-01-27 18:14:50 +0100109#define I801_I2C_BLOCK_LAST 0x38 /* ICH5 and later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define I801_START 0x40
Jean Delvareae7b0492008-01-27 18:14:49 +0100111#define I801_PEC_EN 0x80 /* ICH3 and later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200113/* I801 Hosts Status register bits */
114#define SMBHSTSTS_BYTE_DONE 0x80
115#define SMBHSTSTS_INUSE_STS 0x40
116#define SMBHSTSTS_SMBALERT_STS 0x20
117#define SMBHSTSTS_FAILED 0x10
118#define SMBHSTSTS_BUS_ERR 0x08
119#define SMBHSTSTS_DEV_ERR 0x04
120#define SMBHSTSTS_INTR 0x02
121#define SMBHSTSTS_HOST_BUSY 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Jean Delvarecf898dc2008-07-14 22:38:33 +0200123#define STATUS_FLAGS (SMBHSTSTS_BYTE_DONE | SMBHSTSTS_FAILED | \
124 SMBHSTSTS_BUS_ERR | SMBHSTSTS_DEV_ERR | \
125 SMBHSTSTS_INTR)
126
Jean Delvare6dcc19d2006-06-12 21:53:02 +0200127static unsigned long i801_smba;
Jean Delvarea5aaea32007-03-22 19:49:01 +0100128static unsigned char i801_original_hstcfg;
Jean Delvared6072f82005-09-25 16:37:04 +0200129static struct pci_driver i801_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static struct pci_dev *I801_dev;
Jean Delvare369f6f42008-01-27 18:14:50 +0100131
132#define FEATURE_SMBUS_PEC (1 << 0)
133#define FEATURE_BLOCK_BUFFER (1 << 1)
134#define FEATURE_BLOCK_PROC (1 << 2)
135#define FEATURE_I2C_BLOCK_READ (1 << 3)
136static unsigned int i801_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jean Delvarecf898dc2008-07-14 22:38:33 +0200138/* Make sure the SMBus host is ready to start transmitting.
139 Return 0 if it is, -EBUSY if it is not. */
140static int i801_check_pre(void)
141{
142 int status;
143
144 status = inb_p(SMBHSTSTS);
145 if (status & SMBHSTSTS_HOST_BUSY) {
146 dev_err(&I801_dev->dev, "SMBus is busy, can't use it!\n");
147 return -EBUSY;
148 }
149
150 status &= STATUS_FLAGS;
151 if (status) {
152 dev_dbg(&I801_dev->dev, "Clearing status flags (%02x)\n",
153 status);
154 outb_p(status, SMBHSTSTS);
155 status = inb_p(SMBHSTSTS) & STATUS_FLAGS;
156 if (status) {
157 dev_err(&I801_dev->dev,
158 "Failed clearing status flags (%02x)\n",
159 status);
160 return -EBUSY;
161 }
162 }
163
164 return 0;
165}
166
167/* Convert the status register to an error code, and clear it. */
168static int i801_check_post(int status, int timeout)
169{
170 int result = 0;
171
172 /* If the SMBus is still busy, we give up */
173 if (timeout) {
174 dev_err(&I801_dev->dev, "Transaction timeout\n");
175 /* try to stop the current command */
176 dev_dbg(&I801_dev->dev, "Terminating the current operation\n");
177 outb_p(inb_p(SMBHSTCNT) | SMBHSTCNT_KILL, SMBHSTCNT);
178 msleep(1);
179 outb_p(inb_p(SMBHSTCNT) & (~SMBHSTCNT_KILL), SMBHSTCNT);
180
181 /* Check if it worked */
182 status = inb_p(SMBHSTSTS);
183 if ((status & SMBHSTSTS_HOST_BUSY) ||
184 !(status & SMBHSTSTS_FAILED))
185 dev_err(&I801_dev->dev,
186 "Failed terminating the transaction\n");
187 outb_p(STATUS_FLAGS, SMBHSTSTS);
188 return -ETIMEDOUT;
189 }
190
191 if (status & SMBHSTSTS_FAILED) {
192 result = -EIO;
193 dev_err(&I801_dev->dev, "Transaction failed\n");
194 }
195 if (status & SMBHSTSTS_DEV_ERR) {
196 result = -ENXIO;
197 dev_dbg(&I801_dev->dev, "No response\n");
198 }
199 if (status & SMBHSTSTS_BUS_ERR) {
200 result = -EAGAIN;
201 dev_dbg(&I801_dev->dev, "Lost arbitration\n");
202 }
203
204 if (result) {
205 /* Clear error flags */
206 outb_p(status & STATUS_FLAGS, SMBHSTSTS);
207 status = inb_p(SMBHSTSTS) & STATUS_FLAGS;
208 if (status) {
209 dev_warn(&I801_dev->dev, "Failed clearing status "
210 "flags at end of transaction (%02x)\n",
211 status);
212 }
213 }
214
215 return result;
216}
217
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200218static int i801_transaction(int xact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Jean Delvare2b738092008-07-14 22:38:32 +0200220 int status;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200221 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int timeout = 0;
223
Jean Delvarecf898dc2008-07-14 22:38:33 +0200224 result = i801_check_pre();
225 if (result < 0)
226 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200228 /* the current contents of SMBHSTCNT can be overwritten, since PEC,
229 * INTREN, SMBSCMD are passed in xact */
230 outb_p(xact | I801_START, SMBHSTCNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* We will always wait for a fraction of a second! */
233 do {
234 msleep(1);
Jean Delvare2b738092008-07-14 22:38:32 +0200235 status = inb_p(SMBHSTSTS);
236 } while ((status & SMBHSTSTS_HOST_BUSY) && (timeout++ < MAX_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Jean Delvarecf898dc2008-07-14 22:38:33 +0200238 result = i801_check_post(status, timeout >= MAX_TIMEOUT);
239 if (result < 0)
240 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Jean Delvarecf898dc2008-07-14 22:38:33 +0200242 outb_p(SMBHSTSTS_INTR, SMBHSTSTS);
243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200246/* wait for INTR bit as advised by Intel */
247static void i801_wait_hwpec(void)
248{
249 int timeout = 0;
Jean Delvare2b738092008-07-14 22:38:32 +0200250 int status;
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200251
252 do {
253 msleep(1);
Jean Delvare2b738092008-07-14 22:38:32 +0200254 status = inb_p(SMBHSTSTS);
255 } while ((!(status & SMBHSTSTS_INTR))
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200256 && (timeout++ < MAX_TIMEOUT));
257
258 if (timeout >= MAX_TIMEOUT) {
259 dev_dbg(&I801_dev->dev, "PEC Timeout!\n");
260 }
Jean Delvare2b738092008-07-14 22:38:32 +0200261 outb_p(status, SMBHSTSTS);
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200262}
263
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200264static int i801_block_transaction_by_block(union i2c_smbus_data *data,
265 char read_write, int hwpec)
266{
267 int i, len;
David Brownell97140342008-07-14 22:38:25 +0200268 int status;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200269
270 inb_p(SMBHSTCNT); /* reset the data buffer index */
271
272 /* Use 32-byte buffer to process this transaction */
273 if (read_write == I2C_SMBUS_WRITE) {
274 len = data->block[0];
275 outb_p(len, SMBHSTDAT0);
276 for (i = 0; i < len; i++)
277 outb_p(data->block[i+1], SMBBLKDAT);
278 }
279
David Brownell97140342008-07-14 22:38:25 +0200280 status = i801_transaction(I801_BLOCK_DATA | ENABLE_INT9 |
281 I801_PEC_EN * hwpec);
282 if (status)
283 return status;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200284
285 if (read_write == I2C_SMBUS_READ) {
286 len = inb_p(SMBHSTDAT0);
287 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX)
David Brownell97140342008-07-14 22:38:25 +0200288 return -EPROTO;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200289
290 data->block[0] = len;
291 for (i = 0; i < len; i++)
292 data->block[i + 1] = inb_p(SMBBLKDAT);
293 }
294 return 0;
295}
296
297static int i801_block_transaction_byte_by_byte(union i2c_smbus_data *data,
Jean Delvare63420642008-01-27 18:14:50 +0100298 char read_write, int command,
299 int hwpec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 int i, len;
302 int smbcmd;
Jean Delvare2b738092008-07-14 22:38:32 +0200303 int status;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200304 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 int timeout;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200306
307 result = i801_check_pre();
308 if (result < 0)
309 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200311 len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 if (read_write == I2C_SMBUS_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 outb_p(len, SMBHSTDAT0);
315 outb_p(data->block[1], SMBBLKDAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317
318 for (i = 1; i <= len; i++) {
Jean Delvare63420642008-01-27 18:14:50 +0100319 if (i == len && read_write == I2C_SMBUS_READ) {
320 if (command == I2C_SMBUS_I2C_BLOCK_DATA)
321 smbcmd = I801_I2C_BLOCK_LAST;
322 else
323 smbcmd = I801_BLOCK_LAST;
324 } else {
325 if (command == I2C_SMBUS_I2C_BLOCK_DATA
326 && read_write == I2C_SMBUS_READ)
327 smbcmd = I801_I2C_BLOCK_DATA;
328 else
329 smbcmd = I801_BLOCK_DATA;
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 outb_p(smbcmd | ENABLE_INT9, SMBHSTCNT);
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (i == 1)
334 outb_p(inb(SMBHSTCNT) | I801_START, SMBHSTCNT);
335
336 /* We will always wait for a fraction of a second! */
337 timeout = 0;
338 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 msleep(1);
Jean Delvare2b738092008-07-14 22:38:32 +0200340 status = inb_p(SMBHSTSTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Jean Delvare2b738092008-07-14 22:38:32 +0200342 while ((!(status & SMBHSTSTS_BYTE_DONE))
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200343 && (timeout++ < MAX_TIMEOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Jean Delvarecf898dc2008-07-14 22:38:33 +0200345 result = i801_check_post(status, timeout >= MAX_TIMEOUT);
346 if (result < 0)
347 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Jean Delvare63420642008-01-27 18:14:50 +0100349 if (i == 1 && read_write == I2C_SMBUS_READ
350 && command != I2C_SMBUS_I2C_BLOCK_DATA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 len = inb_p(SMBHSTDAT0);
Jean Delvarecf898dc2008-07-14 22:38:33 +0200352 if (len < 1 || len > I2C_SMBUS_BLOCK_MAX) {
353 dev_err(&I801_dev->dev,
354 "Illegal SMBus block read size %d\n",
355 len);
356 /* Recover */
357 while (inb_p(SMBHSTSTS) & SMBHSTSTS_HOST_BUSY)
358 outb_p(SMBHSTSTS_BYTE_DONE, SMBHSTSTS);
359 outb_p(SMBHSTSTS_INTR, SMBHSTSTS);
David Brownell97140342008-07-14 22:38:25 +0200360 return -EPROTO;
Jean Delvarecf898dc2008-07-14 22:38:33 +0200361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 data->block[0] = len;
363 }
364
365 /* Retrieve/store value in SMBBLKDAT */
366 if (read_write == I2C_SMBUS_READ)
367 data->block[i] = inb_p(SMBBLKDAT);
368 if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
369 outb_p(data->block[i+1], SMBBLKDAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Jean Delvarecf898dc2008-07-14 22:38:33 +0200371 /* signals SMBBLKDAT ready */
372 outb_p(SMBHSTSTS_BYTE_DONE | SMBHSTSTS_INTR, SMBHSTSTS);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200373 }
Jean Delvarecf898dc2008-07-14 22:38:33 +0200374
375 return 0;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200376}
377
378static int i801_set_block_buffer_mode(void)
379{
380 outb_p(inb_p(SMBAUXCTL) | SMBAUXCTL_E32B, SMBAUXCTL);
381 if ((inb_p(SMBAUXCTL) & SMBAUXCTL_E32B) == 0)
David Brownell97140342008-07-14 22:38:25 +0200382 return -EIO;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200383 return 0;
384}
385
386/* Block transaction function */
387static int i801_block_transaction(union i2c_smbus_data *data, char read_write,
388 int command, int hwpec)
389{
390 int result = 0;
391 unsigned char hostc;
392
393 if (command == I2C_SMBUS_I2C_BLOCK_DATA) {
394 if (read_write == I2C_SMBUS_WRITE) {
395 /* set I2C_EN bit in configuration register */
396 pci_read_config_byte(I801_dev, SMBHSTCFG, &hostc);
397 pci_write_config_byte(I801_dev, SMBHSTCFG,
398 hostc | SMBHSTCFG_I2C_EN);
Jean Delvare63420642008-01-27 18:14:50 +0100399 } else if (!(i801_features & FEATURE_I2C_BLOCK_READ)) {
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200400 dev_err(&I801_dev->dev,
Jean Delvare63420642008-01-27 18:14:50 +0100401 "I2C block read is unsupported!\n");
David Brownell97140342008-07-14 22:38:25 +0200402 return -EOPNOTSUPP;
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
Jean Delvare63420642008-01-27 18:14:50 +0100406 if (read_write == I2C_SMBUS_WRITE
407 || command == I2C_SMBUS_I2C_BLOCK_DATA) {
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200408 if (data->block[0] < 1)
409 data->block[0] = 1;
410 if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
411 data->block[0] = I2C_SMBUS_BLOCK_MAX;
412 } else {
Jean Delvare63420642008-01-27 18:14:50 +0100413 data->block[0] = 32; /* max for SMBus block reads */
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200414 }
415
Jean Delvare369f6f42008-01-27 18:14:50 +0100416 if ((i801_features & FEATURE_BLOCK_BUFFER)
Jean Delvare63420642008-01-27 18:14:50 +0100417 && !(command == I2C_SMBUS_I2C_BLOCK_DATA
418 && read_write == I2C_SMBUS_READ)
Jean Delvare369f6f42008-01-27 18:14:50 +0100419 && i801_set_block_buffer_mode() == 0)
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200420 result = i801_block_transaction_by_block(data, read_write,
421 hwpec);
422 else
423 result = i801_block_transaction_byte_by_byte(data, read_write,
Jean Delvare63420642008-01-27 18:14:50 +0100424 command, hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200425
426 if (result == 0 && hwpec)
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200427 i801_wait_hwpec();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Jean Delvare63420642008-01-27 18:14:50 +0100429 if (command == I2C_SMBUS_I2C_BLOCK_DATA
430 && read_write == I2C_SMBUS_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* restore saved configuration register value */
432 pci_write_config_byte(I801_dev, SMBHSTCFG, hostc);
433 }
434 return result;
435}
436
David Brownell97140342008-07-14 22:38:25 +0200437/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438static s32 i801_access(struct i2c_adapter * adap, u16 addr,
439 unsigned short flags, char read_write, u8 command,
440 int size, union i2c_smbus_data * data)
441{
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200442 int hwpec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 int block = 0;
444 int ret, xact = 0;
445
Jean Delvare369f6f42008-01-27 18:14:50 +0100446 hwpec = (i801_features & FEATURE_SMBUS_PEC) && (flags & I2C_CLIENT_PEC)
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200447 && size != I2C_SMBUS_QUICK
448 && size != I2C_SMBUS_I2C_BLOCK_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 switch (size) {
451 case I2C_SMBUS_QUICK:
452 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
453 SMBHSTADD);
454 xact = I801_QUICK;
455 break;
456 case I2C_SMBUS_BYTE:
457 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
458 SMBHSTADD);
459 if (read_write == I2C_SMBUS_WRITE)
460 outb_p(command, SMBHSTCMD);
461 xact = I801_BYTE;
462 break;
463 case I2C_SMBUS_BYTE_DATA:
464 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
465 SMBHSTADD);
466 outb_p(command, SMBHSTCMD);
467 if (read_write == I2C_SMBUS_WRITE)
468 outb_p(data->byte, SMBHSTDAT0);
469 xact = I801_BYTE_DATA;
470 break;
471 case I2C_SMBUS_WORD_DATA:
472 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
473 SMBHSTADD);
474 outb_p(command, SMBHSTCMD);
475 if (read_write == I2C_SMBUS_WRITE) {
476 outb_p(data->word & 0xff, SMBHSTDAT0);
477 outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
478 }
479 xact = I801_WORD_DATA;
480 break;
481 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
483 SMBHSTADD);
484 outb_p(command, SMBHSTCMD);
485 block = 1;
486 break;
Jean Delvare63420642008-01-27 18:14:50 +0100487 case I2C_SMBUS_I2C_BLOCK_DATA:
488 /* NB: page 240 of ICH5 datasheet shows that the R/#W
489 * bit should be cleared here, even when reading */
490 outb_p((addr & 0x7f) << 1, SMBHSTADD);
491 if (read_write == I2C_SMBUS_READ) {
492 /* NB: page 240 of ICH5 datasheet also shows
493 * that DATA1 is the cmd field when reading */
494 outb_p(command, SMBHSTDAT1);
495 } else
496 outb_p(command, SMBHSTCMD);
497 block = 1;
498 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 default:
500 dev_err(&I801_dev->dev, "Unsupported transaction %d\n", size);
David Brownell97140342008-07-14 22:38:25 +0200501 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503
Oleg Ryjkovca8b9e32007-07-12 14:12:31 +0200504 if (hwpec) /* enable/disable hardware PEC */
505 outb_p(inb_p(SMBAUXCTL) | SMBAUXCTL_CRC, SMBAUXCTL);
506 else
507 outb_p(inb_p(SMBAUXCTL) & (~SMBAUXCTL_CRC), SMBAUXCTL);
Jean Delvaree8aac4a2005-10-26 21:34:42 +0200508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if(block)
Jean Delvare585b3162005-10-26 21:31:15 +0200510 ret = i801_block_transaction(data, read_write, size, hwpec);
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200511 else
512 ret = i801_transaction(xact | ENABLE_INT9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Jean Delvarec79cfba2006-04-20 02:43:18 -0700514 /* Some BIOSes don't like it when PEC is enabled at reboot or resume
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200515 time, so we forcibly disable it after every transaction. Turn off
516 E32B for the same reason. */
Jean Delvarea0921b62008-01-27 18:14:50 +0100517 if (hwpec || block)
Oleg Ryjkov7edcb9a2007-07-12 14:12:31 +0200518 outb_p(inb_p(SMBAUXCTL) & ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B),
519 SMBAUXCTL);
Jean Delvarec79cfba2006-04-20 02:43:18 -0700520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if(block)
522 return ret;
523 if(ret)
David Brownell97140342008-07-14 22:38:25 +0200524 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if ((read_write == I2C_SMBUS_WRITE) || (xact == I801_QUICK))
526 return 0;
527
528 switch (xact & 0x7f) {
529 case I801_BYTE: /* Result put in SMBHSTDAT0 */
530 case I801_BYTE_DATA:
531 data->byte = inb_p(SMBHSTDAT0);
532 break;
533 case I801_WORD_DATA:
534 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
535 break;
536 }
537 return 0;
538}
539
540
541static u32 i801_func(struct i2c_adapter *adapter)
542{
543 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
Jean Delvare369f6f42008-01-27 18:14:50 +0100544 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
545 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK |
Jean Delvare63420642008-01-27 18:14:50 +0100546 ((i801_features & FEATURE_SMBUS_PEC) ? I2C_FUNC_SMBUS_PEC : 0) |
547 ((i801_features & FEATURE_I2C_BLOCK_READ) ?
548 I2C_FUNC_SMBUS_READ_I2C_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Jean Delvare8f9082c2006-09-03 22:39:46 +0200551static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 .smbus_xfer = i801_access,
553 .functionality = i801_func,
554};
555
556static struct i2c_adapter i801_adapter = {
557 .owner = THIS_MODULE,
Stephen Hemminger9ace5552007-02-13 22:09:01 +0100558 .id = I2C_HW_SMBUS_I801,
Jean Delvare3401b2f2008-07-14 22:38:29 +0200559 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 .algo = &smbus_algorithm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561};
562
563static struct pci_device_id i801_ids[] = {
564 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_3) },
565 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_3) },
566 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_2) },
567 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_3) },
568 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_3) },
569 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_3) },
570 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_4) },
571 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_16) },
572 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_17) },
Jason Gastonb0a70b52005-04-16 15:24:45 -0700573 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_17) },
Jason Gaston8254fc42006-01-09 10:58:08 -0800574 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_5) },
Jason Gastonadbc2a12006-11-22 15:19:12 -0800575 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_6) },
Jason Gastone07bc672007-10-13 23:56:31 +0200576 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_TOLAPAI_1) },
Gaston, Jason Dd28dc712008-02-24 20:03:42 +0100577 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_4) },
578 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10_5) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 { 0, }
580};
581
582MODULE_DEVICE_TABLE (pci, i801_ids);
583
584static int __devinit i801_probe(struct pci_dev *dev, const struct pci_device_id *id)
585{
Jean Delvare02dd7ae2006-06-12 21:53:41 +0200586 unsigned char temp;
Jean Delvare455f3322006-06-12 21:52:02 +0200587 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Jean Delvare02dd7ae2006-06-12 21:53:41 +0200589 I801_dev = dev;
Jean Delvare369f6f42008-01-27 18:14:50 +0100590 i801_features = 0;
Jean Delvare250d1bd2006-12-10 21:21:33 +0100591 switch (dev->device) {
Jean Delvare250d1bd2006-12-10 21:21:33 +0100592 case PCI_DEVICE_ID_INTEL_82801EB_3:
593 case PCI_DEVICE_ID_INTEL_ESB_4:
594 case PCI_DEVICE_ID_INTEL_ICH6_16:
595 case PCI_DEVICE_ID_INTEL_ICH7_17:
596 case PCI_DEVICE_ID_INTEL_ESB2_17:
597 case PCI_DEVICE_ID_INTEL_ICH8_5:
598 case PCI_DEVICE_ID_INTEL_ICH9_6:
Gaston, Jason Dd28dc712008-02-24 20:03:42 +0100599 case PCI_DEVICE_ID_INTEL_TOLAPAI_1:
600 case PCI_DEVICE_ID_INTEL_ICH10_4:
601 case PCI_DEVICE_ID_INTEL_ICH10_5:
Jean Delvare63420642008-01-27 18:14:50 +0100602 i801_features |= FEATURE_I2C_BLOCK_READ;
603 /* fall through */
604 case PCI_DEVICE_ID_INTEL_82801DB_3:
Jean Delvare369f6f42008-01-27 18:14:50 +0100605 i801_features |= FEATURE_SMBUS_PEC;
606 i801_features |= FEATURE_BLOCK_BUFFER;
Jean Delvare250d1bd2006-12-10 21:21:33 +0100607 break;
Jean Delvare250d1bd2006-12-10 21:21:33 +0100608 }
Jean Delvare02dd7ae2006-06-12 21:53:41 +0200609
610 err = pci_enable_device(dev);
611 if (err) {
612 dev_err(&dev->dev, "Failed to enable SMBus PCI device (%d)\n",
613 err);
614 goto exit;
615 }
616
617 /* Determine the address of the SMBus area */
618 i801_smba = pci_resource_start(dev, SMBBAR);
619 if (!i801_smba) {
620 dev_err(&dev->dev, "SMBus base address uninitialized, "
621 "upgrade BIOS\n");
622 err = -ENODEV;
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +0200623 goto exit;
Jean Delvare02dd7ae2006-06-12 21:53:41 +0200624 }
625
Jean Delvare54fb4a052008-07-14 22:38:33 +0200626 err = acpi_check_resource_conflict(&dev->resource[SMBBAR]);
627 if (err)
628 goto exit;
629
Jean Delvare02dd7ae2006-06-12 21:53:41 +0200630 err = pci_request_region(dev, SMBBAR, i801_driver.name);
631 if (err) {
632 dev_err(&dev->dev, "Failed to request SMBus region "
Andrew Morton598736c2006-06-30 01:56:20 -0700633 "0x%lx-0x%Lx\n", i801_smba,
634 (unsigned long long)pci_resource_end(dev, SMBBAR));
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +0200635 goto exit;
Jean Delvare02dd7ae2006-06-12 21:53:41 +0200636 }
637
638 pci_read_config_byte(I801_dev, SMBHSTCFG, &temp);
Jean Delvarea5aaea32007-03-22 19:49:01 +0100639 i801_original_hstcfg = temp;
Jean Delvare02dd7ae2006-06-12 21:53:41 +0200640 temp &= ~SMBHSTCFG_I2C_EN; /* SMBus timing */
641 if (!(temp & SMBHSTCFG_HST_EN)) {
642 dev_info(&dev->dev, "Enabling SMBus device\n");
643 temp |= SMBHSTCFG_HST_EN;
644 }
645 pci_write_config_byte(I801_dev, SMBHSTCFG, temp);
646
647 if (temp & SMBHSTCFG_SMB_SMI_EN)
648 dev_dbg(&dev->dev, "SMBus using interrupt SMI#\n");
649 else
650 dev_dbg(&dev->dev, "SMBus using PCI Interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Jean Delvarea0921b62008-01-27 18:14:50 +0100652 /* Clear special mode bits */
653 if (i801_features & (FEATURE_SMBUS_PEC | FEATURE_BLOCK_BUFFER))
654 outb_p(inb_p(SMBAUXCTL) & ~(SMBAUXCTL_CRC | SMBAUXCTL_E32B),
655 SMBAUXCTL);
656
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100657 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 i801_adapter.dev.parent = &dev->dev;
659
David Brownell2096b952007-05-01 23:26:28 +0200660 snprintf(i801_adapter.name, sizeof(i801_adapter.name),
Jean Delvare6dcc19d2006-06-12 21:53:02 +0200661 "SMBus I801 adapter at %04lx", i801_smba);
Jean Delvare02dd7ae2006-06-12 21:53:41 +0200662 err = i2c_add_adapter(&i801_adapter);
663 if (err) {
664 dev_err(&dev->dev, "Failed to add SMBus adapter\n");
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +0200665 goto exit_release;
Jean Delvare02dd7ae2006-06-12 21:53:41 +0200666 }
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +0200667 return 0;
Jean Delvare02dd7ae2006-06-12 21:53:41 +0200668
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +0200669exit_release:
670 pci_release_region(dev, SMBBAR);
Jean Delvare02dd7ae2006-06-12 21:53:41 +0200671exit:
672 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675static void __devexit i801_remove(struct pci_dev *dev)
676{
677 i2c_del_adapter(&i801_adapter);
Jean Delvarea5aaea32007-03-22 19:49:01 +0100678 pci_write_config_byte(I801_dev, SMBHSTCFG, i801_original_hstcfg);
Jean Delvare6dcc19d2006-06-12 21:53:02 +0200679 pci_release_region(dev, SMBBAR);
Daniel Ritzd6fcb3b2006-06-27 18:40:54 +0200680 /*
681 * do not call pci_disable_device(dev) since it can cause hard hangs on
682 * some systems during power-off (eg. Fujitsu-Siemens Lifebook E8010)
683 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Jean Delvarea5aaea32007-03-22 19:49:01 +0100686#ifdef CONFIG_PM
687static int i801_suspend(struct pci_dev *dev, pm_message_t mesg)
688{
689 pci_save_state(dev);
690 pci_write_config_byte(dev, SMBHSTCFG, i801_original_hstcfg);
691 pci_set_power_state(dev, pci_choose_state(dev, mesg));
692 return 0;
693}
694
695static int i801_resume(struct pci_dev *dev)
696{
697 pci_set_power_state(dev, PCI_D0);
698 pci_restore_state(dev);
699 return pci_enable_device(dev);
700}
701#else
702#define i801_suspend NULL
703#define i801_resume NULL
704#endif
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706static struct pci_driver i801_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 .name = "i801_smbus",
708 .id_table = i801_ids,
709 .probe = i801_probe,
710 .remove = __devexit_p(i801_remove),
Jean Delvarea5aaea32007-03-22 19:49:01 +0100711 .suspend = i801_suspend,
712 .resume = i801_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713};
714
715static int __init i2c_i801_init(void)
716{
717 return pci_register_driver(&i801_driver);
718}
719
720static void __exit i2c_i801_exit(void)
721{
722 pci_unregister_driver(&i801_driver);
723}
724
Jean Delvare63420642008-01-27 18:14:50 +0100725MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>, "
726 "Jean Delvare <khali@linux-fr.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727MODULE_DESCRIPTION("I801 SMBus driver");
728MODULE_LICENSE("GPL");
729
730module_init(i2c_i801_init);
731module_exit(i2c_i801_exit);