blob: 0e18fe8460103e26eaac8bc68d6dbbe873079c77 [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>
19#include <asm/io.h>
20
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR ("Vojtech Pavlik <vojtech@suse.cz>");
23MODULE_DESCRIPTION("AMD8111 SMBus 2.0 driver");
24
25struct amd_smbus {
26 struct pci_dev *dev;
27 struct i2c_adapter adapter;
28 int base;
29 int size;
30};
31
Jean Delvared6072f82005-09-25 16:37:04 +020032static struct pci_driver amd8111_driver;
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
35 * AMD PCI control registers definitions.
36 */
37
38#define AMD_PCI_MISC 0x48
39
40#define AMD_PCI_MISC_SCI 0x04 /* deliver SCI */
41#define AMD_PCI_MISC_INT 0x02 /* deliver PCI IRQ */
42#define AMD_PCI_MISC_SPEEDUP 0x01 /* 16x clock speedup */
43
44/*
45 * ACPI 2.0 chapter 13 PCI interface definitions.
46 */
47
48#define AMD_EC_DATA 0x00 /* data register */
49#define AMD_EC_SC 0x04 /* status of controller */
50#define AMD_EC_CMD 0x04 /* command register */
51#define AMD_EC_ICR 0x08 /* interrupt control register */
52
53#define AMD_EC_SC_SMI 0x04 /* smi event pending */
54#define AMD_EC_SC_SCI 0x02 /* sci event pending */
55#define AMD_EC_SC_BURST 0x01 /* burst mode enabled */
56#define AMD_EC_SC_CMD 0x08 /* byte in data reg is command */
57#define AMD_EC_SC_IBF 0x02 /* data ready for embedded controller */
58#define AMD_EC_SC_OBF 0x01 /* data ready for host */
59
60#define AMD_EC_CMD_RD 0x80 /* read EC */
61#define AMD_EC_CMD_WR 0x81 /* write EC */
62#define AMD_EC_CMD_BE 0x82 /* enable burst mode */
63#define AMD_EC_CMD_BD 0x83 /* disable burst mode */
64#define AMD_EC_CMD_QR 0x84 /* query EC */
65
66/*
67 * ACPI 2.0 chapter 13 access of registers of the EC
68 */
69
70static unsigned int amd_ec_wait_write(struct amd_smbus *smbus)
71{
72 int timeout = 500;
73
74 while (timeout-- && (inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_IBF))
75 udelay(1);
76
77 if (!timeout) {
Jean Delvare88b9e752007-02-13 22:09:02 +010078 dev_warn(&smbus->dev->dev,
79 "Timeout while waiting for IBF to clear\n");
David Brownell97140342008-07-14 22:38:25 +020080 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82
83 return 0;
84}
85
86static unsigned int amd_ec_wait_read(struct amd_smbus *smbus)
87{
88 int timeout = 500;
89
90 while (timeout-- && (~inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_OBF))
91 udelay(1);
92
93 if (!timeout) {
Jean Delvare88b9e752007-02-13 22:09:02 +010094 dev_warn(&smbus->dev->dev,
95 "Timeout while waiting for OBF to set\n");
David Brownell97140342008-07-14 22:38:25 +020096 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98
99 return 0;
100}
101
Jean Delvare88b9e752007-02-13 22:09:02 +0100102static unsigned int amd_ec_read(struct amd_smbus *smbus, unsigned char address,
103 unsigned char *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
David Brownell97140342008-07-14 22:38:25 +0200105 int status;
106
107 status = amd_ec_wait_write(smbus);
108 if (status)
109 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 outb(AMD_EC_CMD_RD, smbus->base + AMD_EC_CMD);
111
David Brownell97140342008-07-14 22:38:25 +0200112 status = amd_ec_wait_write(smbus);
113 if (status)
114 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 outb(address, smbus->base + AMD_EC_DATA);
116
David Brownell97140342008-07-14 22:38:25 +0200117 status = amd_ec_wait_read(smbus);
118 if (status)
119 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 *data = inb(smbus->base + AMD_EC_DATA);
121
122 return 0;
123}
124
Jean Delvare88b9e752007-02-13 22:09:02 +0100125static unsigned int amd_ec_write(struct amd_smbus *smbus, unsigned char address,
126 unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
David Brownell97140342008-07-14 22:38:25 +0200128 int status;
129
130 status = amd_ec_wait_write(smbus);
131 if (status)
132 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 outb(AMD_EC_CMD_WR, smbus->base + AMD_EC_CMD);
134
David Brownell97140342008-07-14 22:38:25 +0200135 status = amd_ec_wait_write(smbus);
136 if (status)
137 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 outb(address, smbus->base + AMD_EC_DATA);
139
David Brownell97140342008-07-14 22:38:25 +0200140 status = amd_ec_wait_write(smbus);
141 if (status)
142 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 outb(data, smbus->base + AMD_EC_DATA);
144
145 return 0;
146}
147
148/*
149 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
150 */
151
152#define AMD_SMB_PRTCL 0x00 /* protocol, PEC */
153#define AMD_SMB_STS 0x01 /* status */
154#define AMD_SMB_ADDR 0x02 /* address */
155#define AMD_SMB_CMD 0x03 /* command */
156#define AMD_SMB_DATA 0x04 /* 32 data registers */
157#define AMD_SMB_BCNT 0x24 /* number of data bytes */
158#define AMD_SMB_ALRM_A 0x25 /* alarm address */
159#define AMD_SMB_ALRM_D 0x26 /* 2 bytes alarm data */
160
161#define AMD_SMB_STS_DONE 0x80
162#define AMD_SMB_STS_ALRM 0x40
163#define AMD_SMB_STS_RES 0x20
164#define AMD_SMB_STS_STATUS 0x1f
165
166#define AMD_SMB_STATUS_OK 0x00
167#define AMD_SMB_STATUS_FAIL 0x07
168#define AMD_SMB_STATUS_DNAK 0x10
169#define AMD_SMB_STATUS_DERR 0x11
170#define AMD_SMB_STATUS_CMD_DENY 0x12
171#define AMD_SMB_STATUS_UNKNOWN 0x13
172#define AMD_SMB_STATUS_ACC_DENY 0x17
173#define AMD_SMB_STATUS_TIMEOUT 0x18
174#define AMD_SMB_STATUS_NOTSUP 0x19
175#define AMD_SMB_STATUS_BUSY 0x1A
176#define AMD_SMB_STATUS_PEC 0x1F
177
178#define AMD_SMB_PRTCL_WRITE 0x00
179#define AMD_SMB_PRTCL_READ 0x01
180#define AMD_SMB_PRTCL_QUICK 0x02
181#define AMD_SMB_PRTCL_BYTE 0x04
182#define AMD_SMB_PRTCL_BYTE_DATA 0x06
183#define AMD_SMB_PRTCL_WORD_DATA 0x08
184#define AMD_SMB_PRTCL_BLOCK_DATA 0x0a
185#define AMD_SMB_PRTCL_PROC_CALL 0x0c
186#define AMD_SMB_PRTCL_BLOCK_PROC_CALL 0x0d
187#define AMD_SMB_PRTCL_I2C_BLOCK_DATA 0x4a
188#define AMD_SMB_PRTCL_PEC 0x80
189
190
Jean Delvare88b9e752007-02-13 22:09:02 +0100191static s32 amd8111_access(struct i2c_adapter * adap, u16 addr,
192 unsigned short flags, char read_write, u8 command, int size,
193 union i2c_smbus_data * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 struct amd_smbus *smbus = adap->algo_data;
196 unsigned char protocol, len, pec, temp[2];
197 int i;
198
Jean Delvare88b9e752007-02-13 22:09:02 +0100199 protocol = (read_write == I2C_SMBUS_READ) ? AMD_SMB_PRTCL_READ
200 : AMD_SMB_PRTCL_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 pec = (flags & I2C_CLIENT_PEC) ? AMD_SMB_PRTCL_PEC : 0;
202
203 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 case I2C_SMBUS_QUICK:
205 protocol |= AMD_SMB_PRTCL_QUICK;
206 read_write = I2C_SMBUS_WRITE;
207 break;
208
209 case I2C_SMBUS_BYTE:
210 if (read_write == I2C_SMBUS_WRITE)
211 amd_ec_write(smbus, AMD_SMB_CMD, command);
212 protocol |= AMD_SMB_PRTCL_BYTE;
213 break;
214
215 case I2C_SMBUS_BYTE_DATA:
216 amd_ec_write(smbus, AMD_SMB_CMD, command);
217 if (read_write == I2C_SMBUS_WRITE)
218 amd_ec_write(smbus, AMD_SMB_DATA, data->byte);
219 protocol |= AMD_SMB_PRTCL_BYTE_DATA;
220 break;
221
222 case I2C_SMBUS_WORD_DATA:
223 amd_ec_write(smbus, AMD_SMB_CMD, command);
224 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare88b9e752007-02-13 22:09:02 +0100225 amd_ec_write(smbus, AMD_SMB_DATA,
226 data->word & 0xff);
227 amd_ec_write(smbus, AMD_SMB_DATA + 1,
228 data->word >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230 protocol |= AMD_SMB_PRTCL_WORD_DATA | pec;
231 break;
232
233 case I2C_SMBUS_BLOCK_DATA:
234 amd_ec_write(smbus, AMD_SMB_CMD, command);
235 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare88b9e752007-02-13 22:09:02 +0100236 len = min_t(u8, data->block[0],
237 I2C_SMBUS_BLOCK_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 amd_ec_write(smbus, AMD_SMB_BCNT, len);
239 for (i = 0; i < len; i++)
Jean Delvare88b9e752007-02-13 22:09:02 +0100240 amd_ec_write(smbus, AMD_SMB_DATA + i,
241 data->block[i + 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243 protocol |= AMD_SMB_PRTCL_BLOCK_DATA | pec;
244 break;
245
246 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare88b9e752007-02-13 22:09:02 +0100247 len = min_t(u8, data->block[0],
248 I2C_SMBUS_BLOCK_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 amd_ec_write(smbus, AMD_SMB_CMD, command);
250 amd_ec_write(smbus, AMD_SMB_BCNT, len);
251 if (read_write == I2C_SMBUS_WRITE)
252 for (i = 0; i < len; i++)
Jean Delvare88b9e752007-02-13 22:09:02 +0100253 amd_ec_write(smbus, AMD_SMB_DATA + i,
254 data->block[i + 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 protocol |= AMD_SMB_PRTCL_I2C_BLOCK_DATA;
256 break;
257
258 case I2C_SMBUS_PROC_CALL:
259 amd_ec_write(smbus, AMD_SMB_CMD, command);
Jean Delvare88b9e752007-02-13 22:09:02 +0100260 amd_ec_write(smbus, AMD_SMB_DATA, data->word & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 amd_ec_write(smbus, AMD_SMB_DATA + 1, data->word >> 8);
262 protocol = AMD_SMB_PRTCL_PROC_CALL | pec;
263 read_write = I2C_SMBUS_READ;
264 break;
265
266 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare58791fd2007-03-22 19:49:00 +0100267 len = min_t(u8, data->block[0],
268 I2C_SMBUS_BLOCK_MAX - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 amd_ec_write(smbus, AMD_SMB_CMD, command);
270 amd_ec_write(smbus, AMD_SMB_BCNT, len);
271 for (i = 0; i < len; i++)
Jean Delvare88b9e752007-02-13 22:09:02 +0100272 amd_ec_write(smbus, AMD_SMB_DATA + i,
273 data->block[i + 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 protocol = AMD_SMB_PRTCL_BLOCK_PROC_CALL | pec;
275 read_write = I2C_SMBUS_READ;
276 break;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 default:
279 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
David Brownell97140342008-07-14 22:38:25 +0200280 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
283 amd_ec_write(smbus, AMD_SMB_ADDR, addr << 1);
284 amd_ec_write(smbus, AMD_SMB_PRTCL, protocol);
285
David Brownell97140342008-07-14 22:38:25 +0200286 /* FIXME this discards status from ec_read(); so temp[0] will
287 * hold stack garbage ... the rest of this routine will act
288 * nonsensically. Ignored ec_write() status might explain
289 * some such failures...
290 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
292
293 if (~temp[0] & AMD_SMB_STS_DONE) {
294 udelay(500);
295 amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
296 }
297
298 if (~temp[0] & AMD_SMB_STS_DONE) {
299 msleep(1);
300 amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
301 }
302
303 if ((~temp[0] & AMD_SMB_STS_DONE) || (temp[0] & AMD_SMB_STS_STATUS))
David Brownell97140342008-07-14 22:38:25 +0200304 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if (read_write == I2C_SMBUS_WRITE)
307 return 0;
308
309 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 case I2C_SMBUS_BYTE:
311 case I2C_SMBUS_BYTE_DATA:
312 amd_ec_read(smbus, AMD_SMB_DATA, &data->byte);
313 break;
314
315 case I2C_SMBUS_WORD_DATA:
316 case I2C_SMBUS_PROC_CALL:
317 amd_ec_read(smbus, AMD_SMB_DATA, temp + 0);
318 amd_ec_read(smbus, AMD_SMB_DATA + 1, temp + 1);
319 data->word = (temp[1] << 8) | temp[0];
320 break;
321
322 case I2C_SMBUS_BLOCK_DATA:
323 case I2C_SMBUS_BLOCK_PROC_CALL:
324 amd_ec_read(smbus, AMD_SMB_BCNT, &len);
Jean Delvare88b9e752007-02-13 22:09:02 +0100325 len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 case I2C_SMBUS_I2C_BLOCK_DATA:
327 for (i = 0; i < len; i++)
Jean Delvare88b9e752007-02-13 22:09:02 +0100328 amd_ec_read(smbus, AMD_SMB_DATA + i,
329 data->block + i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 data->block[0] = len;
331 break;
332 }
333
334 return 0;
335}
336
337
338static u32 amd8111_func(struct i2c_adapter *adapter)
339{
Jean Delvare88b9e752007-02-13 22:09:02 +0100340 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
341 I2C_FUNC_SMBUS_BYTE_DATA |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA |
343 I2C_FUNC_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
David Brownell6662cbb2007-10-13 23:56:33 +0200344 I2C_FUNC_SMBUS_I2C_BLOCK | I2C_FUNC_SMBUS_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Jean Delvare8f9082c2006-09-03 22:39:46 +0200347static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 .smbus_xfer = amd8111_access,
349 .functionality = amd8111_func,
350};
351
352
353static struct pci_device_id amd8111_ids[] = {
354 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_SMBUS2) },
355 { 0, }
356};
357
358MODULE_DEVICE_TABLE (pci, amd8111_ids);
359
Jean Delvare88b9e752007-02-13 22:09:02 +0100360static int __devinit amd8111_probe(struct pci_dev *dev,
361 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct amd_smbus *smbus;
Jean Delvare88b9e752007-02-13 22:09:02 +0100364 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Jean Delvare88b9e752007-02-13 22:09:02 +0100366 if (!(pci_resource_flags(dev, 0) & IORESOURCE_IO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return -ENODEV;
368
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200369 smbus = kzalloc(sizeof(struct amd_smbus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (!smbus)
371 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 smbus->dev = dev;
374 smbus->base = pci_resource_start(dev, 0);
375 smbus->size = pci_resource_len(dev, 0);
376
Jean Delvare88b9e752007-02-13 22:09:02 +0100377 if (!request_region(smbus->base, smbus->size, amd8111_driver.name)) {
378 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 goto out_kfree;
Jean Delvare88b9e752007-02-13 22:09:02 +0100380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 smbus->adapter.owner = THIS_MODULE;
David Brownell2096b952007-05-01 23:26:28 +0200383 snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 "SMBus2 AMD8111 adapter at %04x", smbus->base);
Stephen Hemminger9ace5552007-02-13 22:09:01 +0100385 smbus->adapter.id = I2C_HW_SMBUS_AMD8111;
Jean Delvare3401b2f2008-07-14 22:38:29 +0200386 smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 smbus->adapter.algo = &smbus_algorithm;
388 smbus->adapter.algo_data = smbus;
389
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100390 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 smbus->adapter.dev.parent = &dev->dev;
392
Jean Delvare88b9e752007-02-13 22:09:02 +0100393 pci_write_config_dword(smbus->dev, AMD_PCI_MISC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 error = i2c_add_adapter(&smbus->adapter);
395 if (error)
396 goto out_release_region;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 pci_set_drvdata(dev, smbus);
399 return 0;
400
401 out_release_region:
402 release_region(smbus->base, smbus->size);
403 out_kfree:
404 kfree(smbus);
Jean Delvare88b9e752007-02-13 22:09:02 +0100405 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static void __devexit amd8111_remove(struct pci_dev *dev)
409{
410 struct amd_smbus *smbus = pci_get_drvdata(dev);
411
412 i2c_del_adapter(&smbus->adapter);
413 release_region(smbus->base, smbus->size);
414 kfree(smbus);
415}
416
417static struct pci_driver amd8111_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 .name = "amd8111_smbus2",
419 .id_table = amd8111_ids,
420 .probe = amd8111_probe,
421 .remove = __devexit_p(amd8111_remove),
422};
423
424static int __init i2c_amd8111_init(void)
425{
426 return pci_register_driver(&amd8111_driver);
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static void __exit i2c_amd8111_exit(void)
430{
431 pci_unregister_driver(&amd8111_driver);
432}
433
434module_init(i2c_amd8111_init);
435module_exit(i2c_amd8111_exit);