Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jean Delvare | 455f332 | 2006-06-12 21:52:02 +0200 | [diff] [blame] | 2 | i2c-i801.c - Part of lm_sensors, Linux kernel modules for hardware |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | monitoring |
| 4 | Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>, |
| 5 | Philip Edelbrock <phil@netroedge.com>, and Mark D. Studebaker |
| 6 | <mdsxyz123@yahoo.com> |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | SUPPORTED DEVICES PCI ID |
| 25 | 82801AA 2413 |
| 26 | 82801AB 2423 |
| 27 | 82801BA 2443 |
| 28 | 82801CA/CAM 2483 |
| 29 | 82801DB 24C3 (HW PEC supported, 32 byte buffer not supported) |
| 30 | 82801EB 24D3 (HW PEC supported, 32 byte buffer not supported) |
| 31 | 6300ESB 25A4 |
| 32 | ICH6 266A |
| 33 | ICH7 27DA |
Jason Gaston | b0a70b5 | 2005-04-16 15:24:45 -0700 | [diff] [blame] | 34 | ESB2 269B |
Jason Gaston | 8254fc4 | 2006-01-09 10:58:08 -0800 | [diff] [blame] | 35 | ICH8 283E |
Jason Gaston | adbc2a1 | 2006-11-22 15:19:12 -0800 | [diff] [blame] | 36 | ICH9 2930 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | This driver supports several versions of Intel's I/O Controller Hubs (ICH). |
| 38 | For SMBus support, they are similar to the PIIX4 and are part |
| 39 | of Intel's '810' and other chipsets. |
Jean Delvare | 455f332 | 2006-06-12 21:52:02 +0200 | [diff] [blame] | 40 | See the file Documentation/i2c/busses/i2c-i801 for details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | I2C Block Read and Process Call are not supported. |
| 42 | */ |
| 43 | |
| 44 | /* Note: we assume there can only be one I801, with one SMBus interface */ |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/module.h> |
| 47 | #include <linux/pci.h> |
| 48 | #include <linux/kernel.h> |
| 49 | #include <linux/stddef.h> |
| 50 | #include <linux/delay.h> |
| 51 | #include <linux/sched.h> |
| 52 | #include <linux/ioport.h> |
| 53 | #include <linux/init.h> |
| 54 | #include <linux/i2c.h> |
| 55 | #include <asm/io.h> |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* I801 SMBus address offsets */ |
| 58 | #define SMBHSTSTS (0 + i801_smba) |
| 59 | #define SMBHSTCNT (2 + i801_smba) |
| 60 | #define SMBHSTCMD (3 + i801_smba) |
| 61 | #define SMBHSTADD (4 + i801_smba) |
| 62 | #define SMBHSTDAT0 (5 + i801_smba) |
| 63 | #define SMBHSTDAT1 (6 + i801_smba) |
| 64 | #define SMBBLKDAT (7 + i801_smba) |
| 65 | #define SMBPEC (8 + i801_smba) /* ICH4 only */ |
| 66 | #define SMBAUXSTS (12 + i801_smba) /* ICH4 only */ |
| 67 | #define SMBAUXCTL (13 + i801_smba) /* ICH4 only */ |
| 68 | |
| 69 | /* PCI Address Constants */ |
Jean Delvare | 6dcc19d | 2006-06-12 21:53:02 +0200 | [diff] [blame] | 70 | #define SMBBAR 4 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #define SMBHSTCFG 0x040 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | /* Host configuration bits for SMBHSTCFG */ |
| 74 | #define SMBHSTCFG_HST_EN 1 |
| 75 | #define SMBHSTCFG_SMB_SMI_EN 2 |
| 76 | #define SMBHSTCFG_I2C_EN 4 |
| 77 | |
| 78 | /* Other settings */ |
| 79 | #define MAX_TIMEOUT 100 |
| 80 | #define ENABLE_INT9 0 /* set to 0x01 to enable - untested */ |
| 81 | |
| 82 | /* I801 command constants */ |
| 83 | #define I801_QUICK 0x00 |
| 84 | #define I801_BYTE 0x04 |
| 85 | #define I801_BYTE_DATA 0x08 |
| 86 | #define I801_WORD_DATA 0x0C |
| 87 | #define I801_PROC_CALL 0x10 /* later chips only, unimplemented */ |
| 88 | #define I801_BLOCK_DATA 0x14 |
| 89 | #define I801_I2C_BLOCK_DATA 0x18 /* unimplemented */ |
| 90 | #define I801_BLOCK_LAST 0x34 |
| 91 | #define I801_I2C_BLOCK_LAST 0x38 /* unimplemented */ |
| 92 | #define I801_START 0x40 |
| 93 | #define I801_PEC_EN 0x80 /* ICH4 only */ |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | static int i801_transaction(void); |
Jean Delvare | 585b316 | 2005-10-26 21:31:15 +0200 | [diff] [blame] | 97 | static int i801_block_transaction(union i2c_smbus_data *data, char read_write, |
| 98 | int command, int hwpec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Jean Delvare | 6dcc19d | 2006-06-12 21:53:02 +0200 | [diff] [blame] | 100 | static unsigned long i801_smba; |
Jean Delvare | d6072f8 | 2005-09-25 16:37:04 +0200 | [diff] [blame] | 101 | static struct pci_driver i801_driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | static struct pci_dev *I801_dev; |
| 103 | static int isich4; |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | static int i801_transaction(void) |
| 106 | { |
| 107 | int temp; |
| 108 | int result = 0; |
| 109 | int timeout = 0; |
| 110 | |
Jean Delvare | 368609c | 2005-07-29 12:15:07 -0700 | [diff] [blame] | 111 | dev_dbg(&I801_dev->dev, "Transaction (pre): CNT=%02x, CMD=%02x, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT), |
| 113 | inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), |
| 114 | inb_p(SMBHSTDAT1)); |
| 115 | |
| 116 | /* Make sure the SMBus host is ready to start transmitting */ |
| 117 | /* 0x1f = Failed, Bus_Err, Dev_Err, Intr, Host_Busy */ |
| 118 | if ((temp = (0x1f & inb_p(SMBHSTSTS))) != 0x00) { |
Jean Delvare | 541e6a0 | 2005-06-23 22:18:08 +0200 | [diff] [blame] | 119 | dev_dbg(&I801_dev->dev, "SMBus busy (%02x). Resetting...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | temp); |
| 121 | outb_p(temp, SMBHSTSTS); |
| 122 | if ((temp = (0x1f & inb_p(SMBHSTSTS))) != 0x00) { |
| 123 | dev_dbg(&I801_dev->dev, "Failed! (%02x)\n", temp); |
| 124 | return -1; |
| 125 | } else { |
Jean Delvare | fcdd96e | 2007-02-13 22:08:59 +0100 | [diff] [blame] | 126 | dev_dbg(&I801_dev->dev, "Successful!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | outb_p(inb(SMBHSTCNT) | I801_START, SMBHSTCNT); |
| 131 | |
| 132 | /* We will always wait for a fraction of a second! */ |
| 133 | do { |
| 134 | msleep(1); |
| 135 | temp = inb_p(SMBHSTSTS); |
| 136 | } while ((temp & 0x01) && (timeout++ < MAX_TIMEOUT)); |
| 137 | |
| 138 | /* If the SMBus is still busy, we give up */ |
| 139 | if (timeout >= MAX_TIMEOUT) { |
| 140 | dev_dbg(&I801_dev->dev, "SMBus Timeout!\n"); |
| 141 | result = -1; |
| 142 | } |
| 143 | |
| 144 | if (temp & 0x10) { |
| 145 | result = -1; |
| 146 | dev_dbg(&I801_dev->dev, "Error: Failed bus transaction\n"); |
| 147 | } |
| 148 | |
| 149 | if (temp & 0x08) { |
| 150 | result = -1; |
| 151 | dev_err(&I801_dev->dev, "Bus collision! SMBus may be locked " |
| 152 | "until next hard reset. (sorry!)\n"); |
| 153 | /* Clock stops and slave is stuck in mid-transmission */ |
| 154 | } |
| 155 | |
| 156 | if (temp & 0x04) { |
| 157 | result = -1; |
| 158 | dev_dbg(&I801_dev->dev, "Error: no response!\n"); |
| 159 | } |
| 160 | |
| 161 | if ((inb_p(SMBHSTSTS) & 0x1f) != 0x00) |
| 162 | outb_p(inb(SMBHSTSTS), SMBHSTSTS); |
| 163 | |
| 164 | if ((temp = (0x1f & inb_p(SMBHSTSTS))) != 0x00) { |
Jean Delvare | 368609c | 2005-07-29 12:15:07 -0700 | [diff] [blame] | 165 | dev_dbg(&I801_dev->dev, "Failed reset at end of transaction " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | "(%02x)\n", temp); |
| 167 | } |
| 168 | dev_dbg(&I801_dev->dev, "Transaction (post): CNT=%02x, CMD=%02x, " |
| 169 | "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT), |
| 170 | inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), |
| 171 | inb_p(SMBHSTDAT1)); |
| 172 | return result; |
| 173 | } |
| 174 | |
| 175 | /* All-inclusive block transaction function */ |
| 176 | static int i801_block_transaction(union i2c_smbus_data *data, char read_write, |
Jean Delvare | 585b316 | 2005-10-26 21:31:15 +0200 | [diff] [blame] | 177 | int command, int hwpec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
| 179 | int i, len; |
| 180 | int smbcmd; |
| 181 | int temp; |
| 182 | int result = 0; |
| 183 | int timeout; |
| 184 | unsigned char hostc, errmask; |
| 185 | |
| 186 | if (command == I2C_SMBUS_I2C_BLOCK_DATA) { |
| 187 | if (read_write == I2C_SMBUS_WRITE) { |
| 188 | /* set I2C_EN bit in configuration register */ |
| 189 | pci_read_config_byte(I801_dev, SMBHSTCFG, &hostc); |
| 190 | pci_write_config_byte(I801_dev, SMBHSTCFG, |
| 191 | hostc | SMBHSTCFG_I2C_EN); |
| 192 | } else { |
| 193 | dev_err(&I801_dev->dev, |
| 194 | "I2C_SMBUS_I2C_BLOCK_READ not DB!\n"); |
| 195 | return -1; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (read_write == I2C_SMBUS_WRITE) { |
| 200 | len = data->block[0]; |
| 201 | if (len < 1) |
| 202 | len = 1; |
| 203 | if (len > 32) |
| 204 | len = 32; |
| 205 | outb_p(len, SMBHSTDAT0); |
| 206 | outb_p(data->block[1], SMBBLKDAT); |
| 207 | } else { |
| 208 | len = 32; /* max for reads */ |
| 209 | } |
| 210 | |
| 211 | if(isich4 && command != I2C_SMBUS_I2C_BLOCK_DATA) { |
| 212 | /* set 32 byte buffer */ |
| 213 | } |
| 214 | |
| 215 | for (i = 1; i <= len; i++) { |
| 216 | if (i == len && read_write == I2C_SMBUS_READ) |
| 217 | smbcmd = I801_BLOCK_LAST; |
| 218 | else |
| 219 | smbcmd = I801_BLOCK_DATA; |
| 220 | outb_p(smbcmd | ENABLE_INT9, SMBHSTCNT); |
| 221 | |
| 222 | dev_dbg(&I801_dev->dev, "Block (pre %d): CNT=%02x, CMD=%02x, " |
| 223 | "ADD=%02x, DAT0=%02x, BLKDAT=%02x\n", i, |
| 224 | inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD), |
| 225 | inb_p(SMBHSTDAT0), inb_p(SMBBLKDAT)); |
| 226 | |
| 227 | /* Make sure the SMBus host is ready to start transmitting */ |
| 228 | temp = inb_p(SMBHSTSTS); |
| 229 | if (i == 1) { |
| 230 | /* Erronenous conditions before transaction: |
| 231 | * Byte_Done, Failed, Bus_Err, Dev_Err, Intr, Host_Busy */ |
| 232 | errmask=0x9f; |
| 233 | } else { |
| 234 | /* Erronenous conditions during transaction: |
| 235 | * Failed, Bus_Err, Dev_Err, Intr */ |
| 236 | errmask=0x1e; |
| 237 | } |
| 238 | if (temp & errmask) { |
| 239 | dev_dbg(&I801_dev->dev, "SMBus busy (%02x). " |
Jean Delvare | 541e6a0 | 2005-06-23 22:18:08 +0200 | [diff] [blame] | 240 | "Resetting...\n", temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | outb_p(temp, SMBHSTSTS); |
| 242 | if (((temp = inb_p(SMBHSTSTS)) & errmask) != 0x00) { |
| 243 | dev_err(&I801_dev->dev, |
| 244 | "Reset failed! (%02x)\n", temp); |
| 245 | result = -1; |
| 246 | goto END; |
| 247 | } |
| 248 | if (i != 1) { |
| 249 | /* if die in middle of block transaction, fail */ |
| 250 | result = -1; |
| 251 | goto END; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if (i == 1) |
| 256 | outb_p(inb(SMBHSTCNT) | I801_START, SMBHSTCNT); |
| 257 | |
| 258 | /* We will always wait for a fraction of a second! */ |
| 259 | timeout = 0; |
| 260 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | msleep(1); |
Jean Delvare | 397e2f6 | 2006-06-12 21:49:36 +0200 | [diff] [blame] | 262 | temp = inb_p(SMBHSTSTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | while ((!(temp & 0x80)) |
| 265 | && (timeout++ < MAX_TIMEOUT)); |
| 266 | |
| 267 | /* If the SMBus is still busy, we give up */ |
| 268 | if (timeout >= MAX_TIMEOUT) { |
| 269 | result = -1; |
| 270 | dev_dbg(&I801_dev->dev, "SMBus Timeout!\n"); |
| 271 | } |
| 272 | |
| 273 | if (temp & 0x10) { |
| 274 | result = -1; |
| 275 | dev_dbg(&I801_dev->dev, |
| 276 | "Error: Failed bus transaction\n"); |
| 277 | } else if (temp & 0x08) { |
| 278 | result = -1; |
| 279 | dev_err(&I801_dev->dev, "Bus collision!\n"); |
| 280 | } else if (temp & 0x04) { |
| 281 | result = -1; |
| 282 | dev_dbg(&I801_dev->dev, "Error: no response!\n"); |
| 283 | } |
| 284 | |
| 285 | if (i == 1 && read_write == I2C_SMBUS_READ) { |
| 286 | len = inb_p(SMBHSTDAT0); |
| 287 | if (len < 1) |
| 288 | len = 1; |
| 289 | if (len > 32) |
| 290 | len = 32; |
| 291 | data->block[0] = len; |
| 292 | } |
| 293 | |
| 294 | /* Retrieve/store value in SMBBLKDAT */ |
| 295 | if (read_write == I2C_SMBUS_READ) |
| 296 | data->block[i] = inb_p(SMBBLKDAT); |
| 297 | if (read_write == I2C_SMBUS_WRITE && i+1 <= len) |
| 298 | outb_p(data->block[i+1], SMBBLKDAT); |
| 299 | if ((temp & 0x9e) != 0x00) |
| 300 | outb_p(temp, SMBHSTSTS); /* signals SMBBLKDAT ready */ |
| 301 | |
| 302 | if ((temp = (0x1e & inb_p(SMBHSTSTS))) != 0x00) { |
| 303 | dev_dbg(&I801_dev->dev, |
| 304 | "Bad status (%02x) at end of transaction\n", |
| 305 | temp); |
| 306 | } |
| 307 | dev_dbg(&I801_dev->dev, "Block (post %d): CNT=%02x, CMD=%02x, " |
| 308 | "ADD=%02x, DAT0=%02x, BLKDAT=%02x\n", i, |
| 309 | inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), inb_p(SMBHSTADD), |
| 310 | inb_p(SMBHSTDAT0), inb_p(SMBBLKDAT)); |
| 311 | |
| 312 | if (result < 0) |
| 313 | goto END; |
| 314 | } |
| 315 | |
Jean Delvare | e8aac4a | 2005-10-26 21:34:42 +0200 | [diff] [blame] | 316 | if (hwpec) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | /* wait for INTR bit as advised by Intel */ |
| 318 | timeout = 0; |
| 319 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | msleep(1); |
Jean Delvare | 397e2f6 | 2006-06-12 21:49:36 +0200 | [diff] [blame] | 321 | temp = inb_p(SMBHSTSTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } while ((!(temp & 0x02)) |
| 323 | && (timeout++ < MAX_TIMEOUT)); |
| 324 | |
| 325 | if (timeout >= MAX_TIMEOUT) { |
| 326 | dev_dbg(&I801_dev->dev, "PEC Timeout!\n"); |
| 327 | } |
| 328 | outb_p(temp, SMBHSTSTS); |
| 329 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | result = 0; |
| 331 | END: |
| 332 | if (command == I2C_SMBUS_I2C_BLOCK_DATA) { |
| 333 | /* restore saved configuration register value */ |
| 334 | pci_write_config_byte(I801_dev, SMBHSTCFG, hostc); |
| 335 | } |
| 336 | return result; |
| 337 | } |
| 338 | |
| 339 | /* Return -1 on error. */ |
| 340 | static s32 i801_access(struct i2c_adapter * adap, u16 addr, |
| 341 | unsigned short flags, char read_write, u8 command, |
| 342 | int size, union i2c_smbus_data * data) |
| 343 | { |
Jean Delvare | e8aac4a | 2005-10-26 21:34:42 +0200 | [diff] [blame] | 344 | int hwpec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | int block = 0; |
| 346 | int ret, xact = 0; |
| 347 | |
Jean Delvare | e8aac4a | 2005-10-26 21:34:42 +0200 | [diff] [blame] | 348 | hwpec = isich4 && (flags & I2C_CLIENT_PEC) |
| 349 | && size != I2C_SMBUS_QUICK |
| 350 | && size != I2C_SMBUS_I2C_BLOCK_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | switch (size) { |
| 353 | case I2C_SMBUS_QUICK: |
| 354 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), |
| 355 | SMBHSTADD); |
| 356 | xact = I801_QUICK; |
| 357 | break; |
| 358 | case I2C_SMBUS_BYTE: |
| 359 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), |
| 360 | SMBHSTADD); |
| 361 | if (read_write == I2C_SMBUS_WRITE) |
| 362 | outb_p(command, SMBHSTCMD); |
| 363 | xact = I801_BYTE; |
| 364 | break; |
| 365 | case I2C_SMBUS_BYTE_DATA: |
| 366 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), |
| 367 | SMBHSTADD); |
| 368 | outb_p(command, SMBHSTCMD); |
| 369 | if (read_write == I2C_SMBUS_WRITE) |
| 370 | outb_p(data->byte, SMBHSTDAT0); |
| 371 | xact = I801_BYTE_DATA; |
| 372 | break; |
| 373 | case I2C_SMBUS_WORD_DATA: |
| 374 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), |
| 375 | SMBHSTADD); |
| 376 | outb_p(command, SMBHSTCMD); |
| 377 | if (read_write == I2C_SMBUS_WRITE) { |
| 378 | outb_p(data->word & 0xff, SMBHSTDAT0); |
| 379 | outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1); |
| 380 | } |
| 381 | xact = I801_WORD_DATA; |
| 382 | break; |
| 383 | case I2C_SMBUS_BLOCK_DATA: |
| 384 | case I2C_SMBUS_I2C_BLOCK_DATA: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), |
| 386 | SMBHSTADD); |
| 387 | outb_p(command, SMBHSTCMD); |
| 388 | block = 1; |
| 389 | break; |
| 390 | case I2C_SMBUS_PROC_CALL: |
| 391 | default: |
| 392 | dev_err(&I801_dev->dev, "Unsupported transaction %d\n", size); |
| 393 | return -1; |
| 394 | } |
| 395 | |
Mark M. Hoffman | 2e3e13f | 2005-11-06 23:04:51 +0100 | [diff] [blame] | 396 | outb_p(hwpec, SMBAUXCTL); /* enable/disable hardware PEC */ |
Jean Delvare | e8aac4a | 2005-10-26 21:34:42 +0200 | [diff] [blame] | 397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | if(block) |
Jean Delvare | 585b316 | 2005-10-26 21:31:15 +0200 | [diff] [blame] | 399 | ret = i801_block_transaction(data, read_write, size, hwpec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | else { |
| 401 | outb_p(xact | ENABLE_INT9, SMBHSTCNT); |
| 402 | ret = i801_transaction(); |
| 403 | } |
| 404 | |
Jean Delvare | c79cfba | 2006-04-20 02:43:18 -0700 | [diff] [blame] | 405 | /* Some BIOSes don't like it when PEC is enabled at reboot or resume |
| 406 | time, so we forcibly disable it after every transaction. */ |
| 407 | if (hwpec) |
| 408 | outb_p(0, SMBAUXCTL); |
| 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | if(block) |
| 411 | return ret; |
| 412 | if(ret) |
| 413 | return -1; |
| 414 | if ((read_write == I2C_SMBUS_WRITE) || (xact == I801_QUICK)) |
| 415 | return 0; |
| 416 | |
| 417 | switch (xact & 0x7f) { |
| 418 | case I801_BYTE: /* Result put in SMBHSTDAT0 */ |
| 419 | case I801_BYTE_DATA: |
| 420 | data->byte = inb_p(SMBHSTDAT0); |
| 421 | break; |
| 422 | case I801_WORD_DATA: |
| 423 | data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8); |
| 424 | break; |
| 425 | } |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | static u32 i801_func(struct i2c_adapter *adapter) |
| 431 | { |
| 432 | return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | |
| 433 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | |
| 434 | I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK |
Jean Delvare | b809554 | 2005-10-26 21:25:04 +0200 | [diff] [blame] | 435 | | (isich4 ? I2C_FUNC_SMBUS_HWPEC_CALC : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 438 | static const struct i2c_algorithm smbus_algorithm = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | .smbus_xfer = i801_access, |
| 440 | .functionality = i801_func, |
| 441 | }; |
| 442 | |
| 443 | static struct i2c_adapter i801_adapter = { |
| 444 | .owner = THIS_MODULE, |
| 445 | .class = I2C_CLASS_HWMON, |
| 446 | .algo = &smbus_algorithm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | }; |
| 448 | |
| 449 | static struct pci_device_id i801_ids[] = { |
| 450 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_3) }, |
| 451 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_3) }, |
| 452 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_2) }, |
| 453 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_3) }, |
| 454 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_3) }, |
| 455 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_3) }, |
| 456 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_4) }, |
| 457 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_16) }, |
| 458 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_17) }, |
Jason Gaston | b0a70b5 | 2005-04-16 15:24:45 -0700 | [diff] [blame] | 459 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_17) }, |
Jason Gaston | 8254fc4 | 2006-01-09 10:58:08 -0800 | [diff] [blame] | 460 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_5) }, |
Jason Gaston | adbc2a1 | 2006-11-22 15:19:12 -0800 | [diff] [blame] | 461 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_6) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { 0, } |
| 463 | }; |
| 464 | |
| 465 | MODULE_DEVICE_TABLE (pci, i801_ids); |
| 466 | |
| 467 | static int __devinit i801_probe(struct pci_dev *dev, const struct pci_device_id *id) |
| 468 | { |
Jean Delvare | 02dd7ae | 2006-06-12 21:53:41 +0200 | [diff] [blame] | 469 | unsigned char temp; |
Jean Delvare | 455f332 | 2006-06-12 21:52:02 +0200 | [diff] [blame] | 470 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Jean Delvare | 02dd7ae | 2006-06-12 21:53:41 +0200 | [diff] [blame] | 472 | I801_dev = dev; |
Jean Delvare | 250d1bd | 2006-12-10 21:21:33 +0100 | [diff] [blame] | 473 | switch (dev->device) { |
| 474 | case PCI_DEVICE_ID_INTEL_82801DB_3: |
| 475 | case PCI_DEVICE_ID_INTEL_82801EB_3: |
| 476 | case PCI_DEVICE_ID_INTEL_ESB_4: |
| 477 | case PCI_DEVICE_ID_INTEL_ICH6_16: |
| 478 | case PCI_DEVICE_ID_INTEL_ICH7_17: |
| 479 | case PCI_DEVICE_ID_INTEL_ESB2_17: |
| 480 | case PCI_DEVICE_ID_INTEL_ICH8_5: |
| 481 | case PCI_DEVICE_ID_INTEL_ICH9_6: |
Jean Delvare | 02dd7ae | 2006-06-12 21:53:41 +0200 | [diff] [blame] | 482 | isich4 = 1; |
Jean Delvare | 250d1bd | 2006-12-10 21:21:33 +0100 | [diff] [blame] | 483 | break; |
| 484 | default: |
Jean Delvare | 02dd7ae | 2006-06-12 21:53:41 +0200 | [diff] [blame] | 485 | isich4 = 0; |
Jean Delvare | 250d1bd | 2006-12-10 21:21:33 +0100 | [diff] [blame] | 486 | } |
Jean Delvare | 02dd7ae | 2006-06-12 21:53:41 +0200 | [diff] [blame] | 487 | |
| 488 | err = pci_enable_device(dev); |
| 489 | if (err) { |
| 490 | dev_err(&dev->dev, "Failed to enable SMBus PCI device (%d)\n", |
| 491 | err); |
| 492 | goto exit; |
| 493 | } |
| 494 | |
| 495 | /* Determine the address of the SMBus area */ |
| 496 | i801_smba = pci_resource_start(dev, SMBBAR); |
| 497 | if (!i801_smba) { |
| 498 | dev_err(&dev->dev, "SMBus base address uninitialized, " |
| 499 | "upgrade BIOS\n"); |
| 500 | err = -ENODEV; |
Daniel Ritz | d6fcb3b | 2006-06-27 18:40:54 +0200 | [diff] [blame] | 501 | goto exit; |
Jean Delvare | 02dd7ae | 2006-06-12 21:53:41 +0200 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | err = pci_request_region(dev, SMBBAR, i801_driver.name); |
| 505 | if (err) { |
| 506 | dev_err(&dev->dev, "Failed to request SMBus region " |
Andrew Morton | 598736c | 2006-06-30 01:56:20 -0700 | [diff] [blame] | 507 | "0x%lx-0x%Lx\n", i801_smba, |
| 508 | (unsigned long long)pci_resource_end(dev, SMBBAR)); |
Daniel Ritz | d6fcb3b | 2006-06-27 18:40:54 +0200 | [diff] [blame] | 509 | goto exit; |
Jean Delvare | 02dd7ae | 2006-06-12 21:53:41 +0200 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | pci_read_config_byte(I801_dev, SMBHSTCFG, &temp); |
| 513 | temp &= ~SMBHSTCFG_I2C_EN; /* SMBus timing */ |
| 514 | if (!(temp & SMBHSTCFG_HST_EN)) { |
| 515 | dev_info(&dev->dev, "Enabling SMBus device\n"); |
| 516 | temp |= SMBHSTCFG_HST_EN; |
| 517 | } |
| 518 | pci_write_config_byte(I801_dev, SMBHSTCFG, temp); |
| 519 | |
| 520 | if (temp & SMBHSTCFG_SMB_SMI_EN) |
| 521 | dev_dbg(&dev->dev, "SMBus using interrupt SMI#\n"); |
| 522 | else |
| 523 | dev_dbg(&dev->dev, "SMBus using PCI Interrupt\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
| 525 | /* set up the driverfs linkage to our parent device */ |
| 526 | i801_adapter.dev.parent = &dev->dev; |
| 527 | |
| 528 | snprintf(i801_adapter.name, I2C_NAME_SIZE, |
Jean Delvare | 6dcc19d | 2006-06-12 21:53:02 +0200 | [diff] [blame] | 529 | "SMBus I801 adapter at %04lx", i801_smba); |
Jean Delvare | 02dd7ae | 2006-06-12 21:53:41 +0200 | [diff] [blame] | 530 | err = i2c_add_adapter(&i801_adapter); |
| 531 | if (err) { |
| 532 | dev_err(&dev->dev, "Failed to add SMBus adapter\n"); |
Daniel Ritz | d6fcb3b | 2006-06-27 18:40:54 +0200 | [diff] [blame] | 533 | goto exit_release; |
Jean Delvare | 02dd7ae | 2006-06-12 21:53:41 +0200 | [diff] [blame] | 534 | } |
Daniel Ritz | d6fcb3b | 2006-06-27 18:40:54 +0200 | [diff] [blame] | 535 | return 0; |
Jean Delvare | 02dd7ae | 2006-06-12 21:53:41 +0200 | [diff] [blame] | 536 | |
Daniel Ritz | d6fcb3b | 2006-06-27 18:40:54 +0200 | [diff] [blame] | 537 | exit_release: |
| 538 | pci_release_region(dev, SMBBAR); |
Jean Delvare | 02dd7ae | 2006-06-12 21:53:41 +0200 | [diff] [blame] | 539 | exit: |
| 540 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static void __devexit i801_remove(struct pci_dev *dev) |
| 544 | { |
| 545 | i2c_del_adapter(&i801_adapter); |
Jean Delvare | 6dcc19d | 2006-06-12 21:53:02 +0200 | [diff] [blame] | 546 | pci_release_region(dev, SMBBAR); |
Daniel Ritz | d6fcb3b | 2006-06-27 18:40:54 +0200 | [diff] [blame] | 547 | /* |
| 548 | * do not call pci_disable_device(dev) since it can cause hard hangs on |
| 549 | * some systems during power-off (eg. Fujitsu-Siemens Lifebook E8010) |
| 550 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | static struct pci_driver i801_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | .name = "i801_smbus", |
| 555 | .id_table = i801_ids, |
| 556 | .probe = i801_probe, |
| 557 | .remove = __devexit_p(i801_remove), |
| 558 | }; |
| 559 | |
| 560 | static int __init i2c_i801_init(void) |
| 561 | { |
| 562 | return pci_register_driver(&i801_driver); |
| 563 | } |
| 564 | |
| 565 | static void __exit i2c_i801_exit(void) |
| 566 | { |
| 567 | pci_unregister_driver(&i801_driver); |
| 568 | } |
| 569 | |
| 570 | MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl>, " |
| 571 | "Philip Edelbrock <phil@netroedge.com>, " |
| 572 | "and Mark D. Studebaker <mdsxyz123@yahoo.com>"); |
| 573 | MODULE_DESCRIPTION("I801 SMBus driver"); |
| 574 | MODULE_LICENSE("GPL"); |
| 575 | |
| 576 | module_init(i2c_i801_init); |
| 577 | module_exit(i2c_i801_exit); |