Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * SMBus 2.0 driver for AMD-8111 IO-Hub. |
| 3 | * |
| 4 | * Copyright (c) 2002 Vojtech Pavlik |
| 5 | * |
| 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. |
| 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/pci.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/stddef.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/ioport.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/i2c.h> |
| 18 | #include <linux/delay.h> |
Jean Delvare | 54fb4a05 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 19 | #include <linux/acpi.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
H Hartley Sweeten | 2178218 | 2010-05-21 18:41:01 +0200 | [diff] [blame] | 21 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | MODULE_LICENSE("GPL"); |
| 24 | MODULE_AUTHOR ("Vojtech Pavlik <vojtech@suse.cz>"); |
| 25 | MODULE_DESCRIPTION("AMD8111 SMBus 2.0 driver"); |
| 26 | |
| 27 | struct amd_smbus { |
| 28 | struct pci_dev *dev; |
| 29 | struct i2c_adapter adapter; |
| 30 | int base; |
| 31 | int size; |
| 32 | }; |
| 33 | |
Jean Delvare | d6072f8 | 2005-09-25 16:37:04 +0200 | [diff] [blame] | 34 | static struct pci_driver amd8111_driver; |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | /* |
| 37 | * AMD PCI control registers definitions. |
| 38 | */ |
| 39 | |
| 40 | #define AMD_PCI_MISC 0x48 |
| 41 | |
| 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 */ |
| 45 | |
| 46 | /* |
| 47 | * ACPI 2.0 chapter 13 PCI interface definitions. |
| 48 | */ |
| 49 | |
| 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 */ |
| 54 | |
| 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 */ |
| 61 | |
| 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 */ |
| 67 | |
| 68 | /* |
| 69 | * ACPI 2.0 chapter 13 access of registers of the EC |
| 70 | */ |
| 71 | |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 72 | static int amd_ec_wait_write(struct amd_smbus *smbus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
| 74 | int timeout = 500; |
| 75 | |
Roel Kluin | a746b57 | 2009-02-24 19:19:48 +0100 | [diff] [blame] | 76 | while ((inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_IBF) && --timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | udelay(1); |
| 78 | |
| 79 | if (!timeout) { |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 80 | dev_warn(&smbus->dev->dev, |
| 81 | "Timeout while waiting for IBF to clear\n"); |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 82 | return -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 88 | static int amd_ec_wait_read(struct amd_smbus *smbus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
| 90 | int timeout = 500; |
| 91 | |
Roel Kluin | a746b57 | 2009-02-24 19:19:48 +0100 | [diff] [blame] | 92 | while ((~inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_OBF) && --timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | udelay(1); |
| 94 | |
| 95 | if (!timeout) { |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 96 | dev_warn(&smbus->dev->dev, |
| 97 | "Timeout while waiting for OBF to set\n"); |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 98 | return -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 104 | static int amd_ec_read(struct amd_smbus *smbus, unsigned char address, |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 105 | unsigned char *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 107 | int status; |
| 108 | |
| 109 | status = amd_ec_wait_write(smbus); |
| 110 | if (status) |
| 111 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | outb(AMD_EC_CMD_RD, smbus->base + AMD_EC_CMD); |
| 113 | |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 114 | status = amd_ec_wait_write(smbus); |
| 115 | if (status) |
| 116 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | outb(address, smbus->base + AMD_EC_DATA); |
| 118 | |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 119 | status = amd_ec_wait_read(smbus); |
| 120 | if (status) |
| 121 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | *data = inb(smbus->base + AMD_EC_DATA); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 127 | static int amd_ec_write(struct amd_smbus *smbus, unsigned char address, |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 128 | unsigned char data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 130 | int status; |
| 131 | |
| 132 | status = amd_ec_wait_write(smbus); |
| 133 | if (status) |
| 134 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | outb(AMD_EC_CMD_WR, smbus->base + AMD_EC_CMD); |
| 136 | |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 137 | status = amd_ec_wait_write(smbus); |
| 138 | if (status) |
| 139 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | outb(address, smbus->base + AMD_EC_DATA); |
| 141 | |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 142 | status = amd_ec_wait_write(smbus); |
| 143 | if (status) |
| 144 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | outb(data, smbus->base + AMD_EC_DATA); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * ACPI 2.0 chapter 13 SMBus 2.0 EC register model |
| 152 | */ |
| 153 | |
| 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 */ |
| 162 | |
| 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 |
| 167 | |
| 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 |
| 179 | |
| 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 |
| 191 | |
| 192 | |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
| 197 | struct amd_smbus *smbus = adap->algo_data; |
| 198 | unsigned char protocol, len, pec, temp[2]; |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 199 | int i, status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 201 | protocol = (read_write == I2C_SMBUS_READ) ? AMD_SMB_PRTCL_READ |
| 202 | : AMD_SMB_PRTCL_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | pec = (flags & I2C_CLIENT_PEC) ? AMD_SMB_PRTCL_PEC : 0; |
| 204 | |
| 205 | switch (size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | case I2C_SMBUS_QUICK: |
| 207 | protocol |= AMD_SMB_PRTCL_QUICK; |
| 208 | read_write = I2C_SMBUS_WRITE; |
| 209 | break; |
| 210 | |
| 211 | case I2C_SMBUS_BYTE: |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 212 | if (read_write == I2C_SMBUS_WRITE) { |
| 213 | status = amd_ec_write(smbus, AMD_SMB_CMD, |
| 214 | command); |
| 215 | if (status) |
| 216 | return status; |
| 217 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | protocol |= AMD_SMB_PRTCL_BYTE; |
| 219 | break; |
| 220 | |
| 221 | case I2C_SMBUS_BYTE_DATA: |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 222 | status = amd_ec_write(smbus, AMD_SMB_CMD, command); |
| 223 | if (status) |
| 224 | return status; |
| 225 | if (read_write == I2C_SMBUS_WRITE) { |
| 226 | status = amd_ec_write(smbus, AMD_SMB_DATA, |
| 227 | data->byte); |
| 228 | if (status) |
| 229 | return status; |
| 230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | protocol |= AMD_SMB_PRTCL_BYTE_DATA; |
| 232 | break; |
| 233 | |
| 234 | case I2C_SMBUS_WORD_DATA: |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 235 | status = amd_ec_write(smbus, AMD_SMB_CMD, command); |
| 236 | if (status) |
| 237 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | if (read_write == I2C_SMBUS_WRITE) { |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 239 | status = amd_ec_write(smbus, AMD_SMB_DATA, |
| 240 | data->word & 0xff); |
| 241 | if (status) |
| 242 | return status; |
| 243 | status = amd_ec_write(smbus, AMD_SMB_DATA + 1, |
| 244 | data->word >> 8); |
| 245 | if (status) |
| 246 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | protocol |= AMD_SMB_PRTCL_WORD_DATA | pec; |
| 249 | break; |
| 250 | |
| 251 | case I2C_SMBUS_BLOCK_DATA: |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 252 | status = amd_ec_write(smbus, AMD_SMB_CMD, command); |
| 253 | if (status) |
| 254 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | if (read_write == I2C_SMBUS_WRITE) { |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 256 | len = min_t(u8, data->block[0], |
| 257 | I2C_SMBUS_BLOCK_MAX); |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 258 | status = amd_ec_write(smbus, AMD_SMB_BCNT, len); |
| 259 | if (status) |
| 260 | return status; |
| 261 | for (i = 0; i < len; i++) { |
| 262 | status = |
| 263 | amd_ec_write(smbus, AMD_SMB_DATA + i, |
| 264 | data->block[i + 1]); |
| 265 | if (status) |
| 266 | return status; |
| 267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | protocol |= AMD_SMB_PRTCL_BLOCK_DATA | pec; |
| 270 | break; |
| 271 | |
| 272 | case I2C_SMBUS_I2C_BLOCK_DATA: |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 273 | len = min_t(u8, data->block[0], |
| 274 | I2C_SMBUS_BLOCK_MAX); |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 275 | status = amd_ec_write(smbus, AMD_SMB_CMD, command); |
| 276 | if (status) |
| 277 | return status; |
| 278 | status = amd_ec_write(smbus, AMD_SMB_BCNT, len); |
| 279 | if (status) |
| 280 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | if (read_write == I2C_SMBUS_WRITE) |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 282 | for (i = 0; i < len; i++) { |
| 283 | status = |
| 284 | amd_ec_write(smbus, AMD_SMB_DATA + i, |
| 285 | data->block[i + 1]); |
| 286 | if (status) |
| 287 | return status; |
| 288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | protocol |= AMD_SMB_PRTCL_I2C_BLOCK_DATA; |
| 290 | break; |
| 291 | |
| 292 | case I2C_SMBUS_PROC_CALL: |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 293 | status = amd_ec_write(smbus, AMD_SMB_CMD, command); |
| 294 | if (status) |
| 295 | return status; |
| 296 | status = amd_ec_write(smbus, AMD_SMB_DATA, |
| 297 | data->word & 0xff); |
| 298 | if (status) |
| 299 | return status; |
| 300 | status = amd_ec_write(smbus, AMD_SMB_DATA + 1, |
| 301 | data->word >> 8); |
| 302 | if (status) |
| 303 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | protocol = AMD_SMB_PRTCL_PROC_CALL | pec; |
| 305 | read_write = I2C_SMBUS_READ; |
| 306 | break; |
| 307 | |
| 308 | case I2C_SMBUS_BLOCK_PROC_CALL: |
Jean Delvare | 58791fd | 2007-03-22 19:49:00 +0100 | [diff] [blame] | 309 | len = min_t(u8, data->block[0], |
| 310 | I2C_SMBUS_BLOCK_MAX - 1); |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 311 | status = amd_ec_write(smbus, AMD_SMB_CMD, command); |
| 312 | if (status) |
| 313 | return status; |
| 314 | status = amd_ec_write(smbus, AMD_SMB_BCNT, len); |
| 315 | if (status) |
| 316 | return status; |
| 317 | for (i = 0; i < len; i++) { |
| 318 | status = amd_ec_write(smbus, AMD_SMB_DATA + i, |
| 319 | data->block[i + 1]); |
| 320 | if (status) |
| 321 | return status; |
| 322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | protocol = AMD_SMB_PRTCL_BLOCK_PROC_CALL | pec; |
| 324 | read_write = I2C_SMBUS_READ; |
| 325 | break; |
| 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | default: |
| 328 | dev_warn(&adap->dev, "Unsupported transaction %d\n", size); |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 329 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 332 | status = amd_ec_write(smbus, AMD_SMB_ADDR, addr << 1); |
| 333 | if (status) |
| 334 | return status; |
| 335 | status = amd_ec_write(smbus, AMD_SMB_PRTCL, protocol); |
| 336 | if (status) |
| 337 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 339 | status = amd_ec_read(smbus, AMD_SMB_STS, temp + 0); |
| 340 | if (status) |
| 341 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | if (~temp[0] & AMD_SMB_STS_DONE) { |
| 344 | udelay(500); |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 345 | status = amd_ec_read(smbus, AMD_SMB_STS, temp + 0); |
| 346 | if (status) |
| 347 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | if (~temp[0] & AMD_SMB_STS_DONE) { |
| 351 | msleep(1); |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 352 | status = amd_ec_read(smbus, AMD_SMB_STS, temp + 0); |
| 353 | if (status) |
| 354 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | if ((~temp[0] & AMD_SMB_STS_DONE) || (temp[0] & AMD_SMB_STS_STATUS)) |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 358 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | if (read_write == I2C_SMBUS_WRITE) |
| 361 | return 0; |
| 362 | |
| 363 | switch (size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | case I2C_SMBUS_BYTE: |
| 365 | case I2C_SMBUS_BYTE_DATA: |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 366 | status = amd_ec_read(smbus, AMD_SMB_DATA, &data->byte); |
| 367 | if (status) |
| 368 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | break; |
| 370 | |
| 371 | case I2C_SMBUS_WORD_DATA: |
| 372 | case I2C_SMBUS_PROC_CALL: |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 373 | status = amd_ec_read(smbus, AMD_SMB_DATA, temp + 0); |
| 374 | if (status) |
| 375 | return status; |
| 376 | status = amd_ec_read(smbus, AMD_SMB_DATA + 1, temp + 1); |
| 377 | if (status) |
| 378 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | data->word = (temp[1] << 8) | temp[0]; |
| 380 | break; |
| 381 | |
| 382 | case I2C_SMBUS_BLOCK_DATA: |
| 383 | case I2C_SMBUS_BLOCK_PROC_CALL: |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 384 | status = amd_ec_read(smbus, AMD_SMB_BCNT, &len); |
| 385 | if (status) |
| 386 | return status; |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 387 | len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | case I2C_SMBUS_I2C_BLOCK_DATA: |
Julia Lawall | 9cb2c27 | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 389 | for (i = 0; i < len; i++) { |
| 390 | status = amd_ec_read(smbus, AMD_SMB_DATA + i, |
| 391 | data->block + i + 1); |
| 392 | if (status) |
| 393 | return status; |
| 394 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | data->block[0] = len; |
| 396 | break; |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | static u32 amd8111_func(struct i2c_adapter *adapter) |
| 404 | { |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 405 | return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | |
| 406 | I2C_FUNC_SMBUS_BYTE_DATA | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA | |
| 408 | I2C_FUNC_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_PROC_CALL | |
David Brownell | 6662cbb | 2007-10-13 23:56:33 +0200 | [diff] [blame] | 409 | I2C_FUNC_SMBUS_I2C_BLOCK | I2C_FUNC_SMBUS_PEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 412 | static const struct i2c_algorithm smbus_algorithm = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | .smbus_xfer = amd8111_access, |
| 414 | .functionality = amd8111_func, |
| 415 | }; |
| 416 | |
| 417 | |
Márton Németh | 4111ecd | 2010-03-02 12:23:37 +0100 | [diff] [blame] | 418 | static const struct pci_device_id amd8111_ids[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_SMBUS2) }, |
| 420 | { 0, } |
| 421 | }; |
| 422 | |
| 423 | MODULE_DEVICE_TABLE (pci, amd8111_ids); |
| 424 | |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 425 | static int __devinit amd8111_probe(struct pci_dev *dev, |
| 426 | const struct pci_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
| 428 | struct amd_smbus *smbus; |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 429 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 431 | if (!(pci_resource_flags(dev, 0) & IORESOURCE_IO)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return -ENODEV; |
| 433 | |
Deepak Saxena | 5263ebb | 2005-10-17 23:09:43 +0200 | [diff] [blame] | 434 | smbus = kzalloc(sizeof(struct amd_smbus), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | if (!smbus) |
| 436 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
| 438 | smbus->dev = dev; |
| 439 | smbus->base = pci_resource_start(dev, 0); |
| 440 | smbus->size = pci_resource_len(dev, 0); |
| 441 | |
Jean Delvare | 54fb4a05 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 442 | error = acpi_check_resource_conflict(&dev->resource[0]); |
Jean Delvare | 18669ea | 2009-10-04 22:53:45 +0200 | [diff] [blame] | 443 | if (error) { |
| 444 | error = -ENODEV; |
Jean Delvare | 54fb4a05 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 445 | goto out_kfree; |
Jean Delvare | 18669ea | 2009-10-04 22:53:45 +0200 | [diff] [blame] | 446 | } |
Jean Delvare | 54fb4a05 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 447 | |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 448 | if (!request_region(smbus->base, smbus->size, amd8111_driver.name)) { |
| 449 | error = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | goto out_kfree; |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 451 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
| 453 | smbus->adapter.owner = THIS_MODULE; |
David Brownell | 2096b95 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 454 | snprintf(smbus->adapter.name, sizeof(smbus->adapter.name), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | "SMBus2 AMD8111 adapter at %04x", smbus->base); |
Jean Delvare | 3401b2f | 2008-07-14 22:38:29 +0200 | [diff] [blame] | 456 | smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | smbus->adapter.algo = &smbus_algorithm; |
| 458 | smbus->adapter.algo_data = smbus; |
| 459 | |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 460 | /* set up the sysfs linkage to our parent device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | smbus->adapter.dev.parent = &dev->dev; |
| 462 | |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 463 | pci_write_config_dword(smbus->dev, AMD_PCI_MISC, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | error = i2c_add_adapter(&smbus->adapter); |
| 465 | if (error) |
| 466 | goto out_release_region; |
| 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | pci_set_drvdata(dev, smbus); |
| 469 | return 0; |
| 470 | |
| 471 | out_release_region: |
| 472 | release_region(smbus->base, smbus->size); |
| 473 | out_kfree: |
| 474 | kfree(smbus); |
Jean Delvare | 88b9e75 | 2007-02-13 22:09:02 +0100 | [diff] [blame] | 475 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | static void __devexit amd8111_remove(struct pci_dev *dev) |
| 479 | { |
| 480 | struct amd_smbus *smbus = pci_get_drvdata(dev); |
| 481 | |
| 482 | i2c_del_adapter(&smbus->adapter); |
| 483 | release_region(smbus->base, smbus->size); |
| 484 | kfree(smbus); |
| 485 | } |
| 486 | |
| 487 | static struct pci_driver amd8111_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | .name = "amd8111_smbus2", |
| 489 | .id_table = amd8111_ids, |
| 490 | .probe = amd8111_probe, |
| 491 | .remove = __devexit_p(amd8111_remove), |
| 492 | }; |
| 493 | |
| 494 | static int __init i2c_amd8111_init(void) |
| 495 | { |
| 496 | return pci_register_driver(&amd8111_driver); |
| 497 | } |
| 498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | static void __exit i2c_amd8111_exit(void) |
| 500 | { |
| 501 | pci_unregister_driver(&amd8111_driver); |
| 502 | } |
| 503 | |
| 504 | module_init(i2c_amd8111_init); |
| 505 | module_exit(i2c_amd8111_exit); |