blob: 5b4ad86ca166f2bdcec9e63f8fa02947f2bfd83c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/kernel.h>
14#include <linux/stddef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/ioport.h>
16#include <linux/init.h>
17#include <linux/i2c.h>
18#include <linux/delay.h>
Jean Delvare54fb4a052008-07-14 22:38:33 +020019#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/io.h>
21
22MODULE_LICENSE("GPL");
23MODULE_AUTHOR ("Vojtech Pavlik <vojtech@suse.cz>");
24MODULE_DESCRIPTION("AMD8111 SMBus 2.0 driver");
25
26struct amd_smbus {
27 struct pci_dev *dev;
28 struct i2c_adapter adapter;
29 int base;
30 int size;
31};
32
Jean Delvared6072f82005-09-25 16:37:04 +020033static struct pci_driver amd8111_driver;
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
36 * AMD PCI control registers definitions.
37 */
38
39#define AMD_PCI_MISC 0x48
40
41#define AMD_PCI_MISC_SCI 0x04 /* deliver SCI */
42#define AMD_PCI_MISC_INT 0x02 /* deliver PCI IRQ */
43#define AMD_PCI_MISC_SPEEDUP 0x01 /* 16x clock speedup */
44
45/*
46 * ACPI 2.0 chapter 13 PCI interface definitions.
47 */
48
49#define AMD_EC_DATA 0x00 /* data register */
50#define AMD_EC_SC 0x04 /* status of controller */
51#define AMD_EC_CMD 0x04 /* command register */
52#define AMD_EC_ICR 0x08 /* interrupt control register */
53
54#define AMD_EC_SC_SMI 0x04 /* smi event pending */
55#define AMD_EC_SC_SCI 0x02 /* sci event pending */
56#define AMD_EC_SC_BURST 0x01 /* burst mode enabled */
57#define AMD_EC_SC_CMD 0x08 /* byte in data reg is command */
58#define AMD_EC_SC_IBF 0x02 /* data ready for embedded controller */
59#define AMD_EC_SC_OBF 0x01 /* data ready for host */
60
61#define AMD_EC_CMD_RD 0x80 /* read EC */
62#define AMD_EC_CMD_WR 0x81 /* write EC */
63#define AMD_EC_CMD_BE 0x82 /* enable burst mode */
64#define AMD_EC_CMD_BD 0x83 /* disable burst mode */
65#define AMD_EC_CMD_QR 0x84 /* query EC */
66
67/*
68 * ACPI 2.0 chapter 13 access of registers of the EC
69 */
70
71static unsigned int amd_ec_wait_write(struct amd_smbus *smbus)
72{
73 int timeout = 500;
74
Roel Kluina746b572009-02-24 19:19:48 +010075 while ((inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_IBF) && --timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 udelay(1);
77
78 if (!timeout) {
Jean Delvare88b9e752007-02-13 22:09:02 +010079 dev_warn(&smbus->dev->dev,
80 "Timeout while waiting for IBF to clear\n");
David Brownell97140342008-07-14 22:38:25 +020081 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83
84 return 0;
85}
86
87static unsigned int amd_ec_wait_read(struct amd_smbus *smbus)
88{
89 int timeout = 500;
90
Roel Kluina746b572009-02-24 19:19:48 +010091 while ((~inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_OBF) && --timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 udelay(1);
93
94 if (!timeout) {
Jean Delvare88b9e752007-02-13 22:09:02 +010095 dev_warn(&smbus->dev->dev,
96 "Timeout while waiting for OBF to set\n");
David Brownell97140342008-07-14 22:38:25 +020097 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99
100 return 0;
101}
102
Jean Delvare88b9e752007-02-13 22:09:02 +0100103static unsigned int amd_ec_read(struct amd_smbus *smbus, unsigned char address,
104 unsigned char *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
David Brownell97140342008-07-14 22:38:25 +0200106 int status;
107
108 status = amd_ec_wait_write(smbus);
109 if (status)
110 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 outb(AMD_EC_CMD_RD, smbus->base + AMD_EC_CMD);
112
David Brownell97140342008-07-14 22:38:25 +0200113 status = amd_ec_wait_write(smbus);
114 if (status)
115 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 outb(address, smbus->base + AMD_EC_DATA);
117
David Brownell97140342008-07-14 22:38:25 +0200118 status = amd_ec_wait_read(smbus);
119 if (status)
120 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 *data = inb(smbus->base + AMD_EC_DATA);
122
123 return 0;
124}
125
Jean Delvare88b9e752007-02-13 22:09:02 +0100126static unsigned int amd_ec_write(struct amd_smbus *smbus, unsigned char address,
127 unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
David Brownell97140342008-07-14 22:38:25 +0200129 int status;
130
131 status = amd_ec_wait_write(smbus);
132 if (status)
133 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 outb(AMD_EC_CMD_WR, smbus->base + AMD_EC_CMD);
135
David Brownell97140342008-07-14 22:38:25 +0200136 status = amd_ec_wait_write(smbus);
137 if (status)
138 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 outb(address, smbus->base + AMD_EC_DATA);
140
David Brownell97140342008-07-14 22:38:25 +0200141 status = amd_ec_wait_write(smbus);
142 if (status)
143 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 outb(data, smbus->base + AMD_EC_DATA);
145
146 return 0;
147}
148
149/*
150 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
151 */
152
153#define AMD_SMB_PRTCL 0x00 /* protocol, PEC */
154#define AMD_SMB_STS 0x01 /* status */
155#define AMD_SMB_ADDR 0x02 /* address */
156#define AMD_SMB_CMD 0x03 /* command */
157#define AMD_SMB_DATA 0x04 /* 32 data registers */
158#define AMD_SMB_BCNT 0x24 /* number of data bytes */
159#define AMD_SMB_ALRM_A 0x25 /* alarm address */
160#define AMD_SMB_ALRM_D 0x26 /* 2 bytes alarm data */
161
162#define AMD_SMB_STS_DONE 0x80
163#define AMD_SMB_STS_ALRM 0x40
164#define AMD_SMB_STS_RES 0x20
165#define AMD_SMB_STS_STATUS 0x1f
166
167#define AMD_SMB_STATUS_OK 0x00
168#define AMD_SMB_STATUS_FAIL 0x07
169#define AMD_SMB_STATUS_DNAK 0x10
170#define AMD_SMB_STATUS_DERR 0x11
171#define AMD_SMB_STATUS_CMD_DENY 0x12
172#define AMD_SMB_STATUS_UNKNOWN 0x13
173#define AMD_SMB_STATUS_ACC_DENY 0x17
174#define AMD_SMB_STATUS_TIMEOUT 0x18
175#define AMD_SMB_STATUS_NOTSUP 0x19
176#define AMD_SMB_STATUS_BUSY 0x1A
177#define AMD_SMB_STATUS_PEC 0x1F
178
179#define AMD_SMB_PRTCL_WRITE 0x00
180#define AMD_SMB_PRTCL_READ 0x01
181#define AMD_SMB_PRTCL_QUICK 0x02
182#define AMD_SMB_PRTCL_BYTE 0x04
183#define AMD_SMB_PRTCL_BYTE_DATA 0x06
184#define AMD_SMB_PRTCL_WORD_DATA 0x08
185#define AMD_SMB_PRTCL_BLOCK_DATA 0x0a
186#define AMD_SMB_PRTCL_PROC_CALL 0x0c
187#define AMD_SMB_PRTCL_BLOCK_PROC_CALL 0x0d
188#define AMD_SMB_PRTCL_I2C_BLOCK_DATA 0x4a
189#define AMD_SMB_PRTCL_PEC 0x80
190
191
Jean Delvare88b9e752007-02-13 22:09:02 +0100192static s32 amd8111_access(struct i2c_adapter * adap, u16 addr,
193 unsigned short flags, char read_write, u8 command, int size,
194 union i2c_smbus_data * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 struct amd_smbus *smbus = adap->algo_data;
197 unsigned char protocol, len, pec, temp[2];
198 int i;
199
Jean Delvare88b9e752007-02-13 22:09:02 +0100200 protocol = (read_write == I2C_SMBUS_READ) ? AMD_SMB_PRTCL_READ
201 : AMD_SMB_PRTCL_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 pec = (flags & I2C_CLIENT_PEC) ? AMD_SMB_PRTCL_PEC : 0;
203
204 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 case I2C_SMBUS_QUICK:
206 protocol |= AMD_SMB_PRTCL_QUICK;
207 read_write = I2C_SMBUS_WRITE;
208 break;
209
210 case I2C_SMBUS_BYTE:
211 if (read_write == I2C_SMBUS_WRITE)
212 amd_ec_write(smbus, AMD_SMB_CMD, command);
213 protocol |= AMD_SMB_PRTCL_BYTE;
214 break;
215
216 case I2C_SMBUS_BYTE_DATA:
217 amd_ec_write(smbus, AMD_SMB_CMD, command);
218 if (read_write == I2C_SMBUS_WRITE)
219 amd_ec_write(smbus, AMD_SMB_DATA, data->byte);
220 protocol |= AMD_SMB_PRTCL_BYTE_DATA;
221 break;
222
223 case I2C_SMBUS_WORD_DATA:
224 amd_ec_write(smbus, AMD_SMB_CMD, command);
225 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare88b9e752007-02-13 22:09:02 +0100226 amd_ec_write(smbus, AMD_SMB_DATA,
227 data->word & 0xff);
228 amd_ec_write(smbus, AMD_SMB_DATA + 1,
229 data->word >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 protocol |= AMD_SMB_PRTCL_WORD_DATA | pec;
232 break;
233
234 case I2C_SMBUS_BLOCK_DATA:
235 amd_ec_write(smbus, AMD_SMB_CMD, command);
236 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare88b9e752007-02-13 22:09:02 +0100237 len = min_t(u8, data->block[0],
238 I2C_SMBUS_BLOCK_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 amd_ec_write(smbus, AMD_SMB_BCNT, len);
240 for (i = 0; i < len; i++)
Jean Delvare88b9e752007-02-13 22:09:02 +0100241 amd_ec_write(smbus, AMD_SMB_DATA + i,
242 data->block[i + 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 protocol |= AMD_SMB_PRTCL_BLOCK_DATA | pec;
245 break;
246
247 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare88b9e752007-02-13 22:09:02 +0100248 len = min_t(u8, data->block[0],
249 I2C_SMBUS_BLOCK_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 amd_ec_write(smbus, AMD_SMB_CMD, command);
251 amd_ec_write(smbus, AMD_SMB_BCNT, len);
252 if (read_write == I2C_SMBUS_WRITE)
253 for (i = 0; i < len; i++)
Jean Delvare88b9e752007-02-13 22:09:02 +0100254 amd_ec_write(smbus, AMD_SMB_DATA + i,
255 data->block[i + 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 protocol |= AMD_SMB_PRTCL_I2C_BLOCK_DATA;
257 break;
258
259 case I2C_SMBUS_PROC_CALL:
260 amd_ec_write(smbus, AMD_SMB_CMD, command);
Jean Delvare88b9e752007-02-13 22:09:02 +0100261 amd_ec_write(smbus, AMD_SMB_DATA, data->word & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 amd_ec_write(smbus, AMD_SMB_DATA + 1, data->word >> 8);
263 protocol = AMD_SMB_PRTCL_PROC_CALL | pec;
264 read_write = I2C_SMBUS_READ;
265 break;
266
267 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare58791fd2007-03-22 19:49:00 +0100268 len = min_t(u8, data->block[0],
269 I2C_SMBUS_BLOCK_MAX - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 amd_ec_write(smbus, AMD_SMB_CMD, command);
271 amd_ec_write(smbus, AMD_SMB_BCNT, len);
272 for (i = 0; i < len; i++)
Jean Delvare88b9e752007-02-13 22:09:02 +0100273 amd_ec_write(smbus, AMD_SMB_DATA + i,
274 data->block[i + 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 protocol = AMD_SMB_PRTCL_BLOCK_PROC_CALL | pec;
276 read_write = I2C_SMBUS_READ;
277 break;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 default:
280 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
David Brownell97140342008-07-14 22:38:25 +0200281 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
284 amd_ec_write(smbus, AMD_SMB_ADDR, addr << 1);
285 amd_ec_write(smbus, AMD_SMB_PRTCL, protocol);
286
David Brownell97140342008-07-14 22:38:25 +0200287 /* FIXME this discards status from ec_read(); so temp[0] will
288 * hold stack garbage ... the rest of this routine will act
289 * nonsensically. Ignored ec_write() status might explain
290 * some such failures...
291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
293
294 if (~temp[0] & AMD_SMB_STS_DONE) {
295 udelay(500);
296 amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
297 }
298
299 if (~temp[0] & AMD_SMB_STS_DONE) {
300 msleep(1);
301 amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
302 }
303
304 if ((~temp[0] & AMD_SMB_STS_DONE) || (temp[0] & AMD_SMB_STS_STATUS))
David Brownell97140342008-07-14 22:38:25 +0200305 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 if (read_write == I2C_SMBUS_WRITE)
308 return 0;
309
310 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 case I2C_SMBUS_BYTE:
312 case I2C_SMBUS_BYTE_DATA:
313 amd_ec_read(smbus, AMD_SMB_DATA, &data->byte);
314 break;
315
316 case I2C_SMBUS_WORD_DATA:
317 case I2C_SMBUS_PROC_CALL:
318 amd_ec_read(smbus, AMD_SMB_DATA, temp + 0);
319 amd_ec_read(smbus, AMD_SMB_DATA + 1, temp + 1);
320 data->word = (temp[1] << 8) | temp[0];
321 break;
322
323 case I2C_SMBUS_BLOCK_DATA:
324 case I2C_SMBUS_BLOCK_PROC_CALL:
325 amd_ec_read(smbus, AMD_SMB_BCNT, &len);
Jean Delvare88b9e752007-02-13 22:09:02 +0100326 len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 case I2C_SMBUS_I2C_BLOCK_DATA:
328 for (i = 0; i < len; i++)
Jean Delvare88b9e752007-02-13 22:09:02 +0100329 amd_ec_read(smbus, AMD_SMB_DATA + i,
330 data->block + i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 data->block[0] = len;
332 break;
333 }
334
335 return 0;
336}
337
338
339static u32 amd8111_func(struct i2c_adapter *adapter)
340{
Jean Delvare88b9e752007-02-13 22:09:02 +0100341 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
342 I2C_FUNC_SMBUS_BYTE_DATA |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA |
344 I2C_FUNC_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
David Brownell6662cbb2007-10-13 23:56:33 +0200345 I2C_FUNC_SMBUS_I2C_BLOCK | I2C_FUNC_SMBUS_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Jean Delvare8f9082c2006-09-03 22:39:46 +0200348static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 .smbus_xfer = amd8111_access,
350 .functionality = amd8111_func,
351};
352
353
354static struct pci_device_id amd8111_ids[] = {
355 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_SMBUS2) },
356 { 0, }
357};
358
359MODULE_DEVICE_TABLE (pci, amd8111_ids);
360
Jean Delvare88b9e752007-02-13 22:09:02 +0100361static int __devinit amd8111_probe(struct pci_dev *dev,
362 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct amd_smbus *smbus;
Jean Delvare88b9e752007-02-13 22:09:02 +0100365 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Jean Delvare88b9e752007-02-13 22:09:02 +0100367 if (!(pci_resource_flags(dev, 0) & IORESOURCE_IO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return -ENODEV;
369
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200370 smbus = kzalloc(sizeof(struct amd_smbus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (!smbus)
372 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 smbus->dev = dev;
375 smbus->base = pci_resource_start(dev, 0);
376 smbus->size = pci_resource_len(dev, 0);
377
Jean Delvare54fb4a052008-07-14 22:38:33 +0200378 error = acpi_check_resource_conflict(&dev->resource[0]);
Jean Delvare18669ea2009-10-04 22:53:45 +0200379 if (error) {
380 error = -ENODEV;
Jean Delvare54fb4a052008-07-14 22:38:33 +0200381 goto out_kfree;
Jean Delvare18669ea2009-10-04 22:53:45 +0200382 }
Jean Delvare54fb4a052008-07-14 22:38:33 +0200383
Jean Delvare88b9e752007-02-13 22:09:02 +0100384 if (!request_region(smbus->base, smbus->size, amd8111_driver.name)) {
385 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 goto out_kfree;
Jean Delvare88b9e752007-02-13 22:09:02 +0100387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 smbus->adapter.owner = THIS_MODULE;
David Brownell2096b952007-05-01 23:26:28 +0200390 snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 "SMBus2 AMD8111 adapter at %04x", smbus->base);
Jean Delvare3401b2f2008-07-14 22:38:29 +0200392 smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 smbus->adapter.algo = &smbus_algorithm;
394 smbus->adapter.algo_data = smbus;
395
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100396 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 smbus->adapter.dev.parent = &dev->dev;
398
Jean Delvare88b9e752007-02-13 22:09:02 +0100399 pci_write_config_dword(smbus->dev, AMD_PCI_MISC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 error = i2c_add_adapter(&smbus->adapter);
401 if (error)
402 goto out_release_region;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 pci_set_drvdata(dev, smbus);
405 return 0;
406
407 out_release_region:
408 release_region(smbus->base, smbus->size);
409 out_kfree:
410 kfree(smbus);
Jean Delvare88b9e752007-02-13 22:09:02 +0100411 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414static void __devexit amd8111_remove(struct pci_dev *dev)
415{
416 struct amd_smbus *smbus = pci_get_drvdata(dev);
417
418 i2c_del_adapter(&smbus->adapter);
419 release_region(smbus->base, smbus->size);
420 kfree(smbus);
421}
422
423static struct pci_driver amd8111_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 .name = "amd8111_smbus2",
425 .id_table = amd8111_ids,
426 .probe = amd8111_probe,
427 .remove = __devexit_p(amd8111_remove),
428};
429
430static int __init i2c_amd8111_init(void)
431{
432 return pci_register_driver(&amd8111_driver);
433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435static void __exit i2c_amd8111_exit(void)
436{
437 pci_unregister_driver(&amd8111_driver);
438}
439
440module_init(i2c_amd8111_init);
441module_exit(i2c_amd8111_exit);