i2c-amd8111.c 11.1 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * SMBus 2.0 driver for AMD-8111 IO-Hub.
 *
 * Copyright (c) 2002 Vojtech Pavlik
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation version 2.
 */

#include <linux/module.h>
#include <linux/pci.h>
#include <linux/kernel.h>
#include <linux/stddef.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/i2c.h>
#include <linux/delay.h>
19
#include <linux/acpi.h>
Linus Torvalds's avatar
Linus Torvalds committed
20 21 22 23 24 25 26 27 28 29 30 31 32
#include <asm/io.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR ("Vojtech Pavlik <vojtech@suse.cz>");
MODULE_DESCRIPTION("AMD8111 SMBus 2.0 driver");

struct amd_smbus {
	struct pci_dev *dev;
	struct i2c_adapter adapter;
	int base;
	int size;
};

33 34
static struct pci_driver amd8111_driver;

Linus Torvalds's avatar
Linus Torvalds committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/*
 * AMD PCI control registers definitions.
 */

#define AMD_PCI_MISC	0x48

#define AMD_PCI_MISC_SCI	0x04	/* deliver SCI */
#define AMD_PCI_MISC_INT	0x02	/* deliver PCI IRQ */
#define AMD_PCI_MISC_SPEEDUP	0x01	/* 16x clock speedup */

/*
 * ACPI 2.0 chapter 13 PCI interface definitions.
 */

#define AMD_EC_DATA	0x00	/* data register */
#define AMD_EC_SC	0x04	/* status of controller */
#define AMD_EC_CMD	0x04	/* command register */
#define AMD_EC_ICR	0x08	/* interrupt control register */

#define AMD_EC_SC_SMI	0x04	/* smi event pending */
#define AMD_EC_SC_SCI	0x02	/* sci event pending */
#define AMD_EC_SC_BURST	0x01	/* burst mode enabled */
#define AMD_EC_SC_CMD	0x08	/* byte in data reg is command */
#define AMD_EC_SC_IBF	0x02	/* data ready for embedded controller */
#define AMD_EC_SC_OBF	0x01	/* data ready for host */

#define AMD_EC_CMD_RD	0x80	/* read EC */
#define AMD_EC_CMD_WR	0x81	/* write EC */
#define AMD_EC_CMD_BE	0x82	/* enable burst mode */
#define AMD_EC_CMD_BD	0x83	/* disable burst mode */
#define AMD_EC_CMD_QR	0x84	/* query EC */

/*
 * ACPI 2.0 chapter 13 access of registers of the EC
 */

static unsigned int amd_ec_wait_write(struct amd_smbus *smbus)
{
	int timeout = 500;

Roel Kluin's avatar
Roel Kluin committed
75
	while ((inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_IBF) && --timeout)
Linus Torvalds's avatar
Linus Torvalds committed
76 77 78
		udelay(1);

	if (!timeout) {
Jean Delvare's avatar
Jean Delvare committed
79 80
		dev_warn(&smbus->dev->dev,
			 "Timeout while waiting for IBF to clear\n");
81
		return -ETIMEDOUT;
Linus Torvalds's avatar
Linus Torvalds committed
82 83 84 85 86 87 88 89 90
	}

	return 0;
}

static unsigned int amd_ec_wait_read(struct amd_smbus *smbus)
{
	int timeout = 500;

Roel Kluin's avatar
Roel Kluin committed
91
	while ((~inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_OBF) && --timeout)
Linus Torvalds's avatar
Linus Torvalds committed
92 93 94
		udelay(1);

	if (!timeout) {
Jean Delvare's avatar
Jean Delvare committed
95 96
		dev_warn(&smbus->dev->dev,
			 "Timeout while waiting for OBF to set\n");
97
		return -ETIMEDOUT;
Linus Torvalds's avatar
Linus Torvalds committed
98 99 100 101 102
	}

	return 0;
}

Jean Delvare's avatar
Jean Delvare committed
103 104
static unsigned int amd_ec_read(struct amd_smbus *smbus, unsigned char address,
		unsigned char *data)
Linus Torvalds's avatar
Linus Torvalds committed
105
{
106 107 108 109 110
	int status;

	status = amd_ec_wait_write(smbus);
	if (status)
		return status;
Linus Torvalds's avatar
Linus Torvalds committed
111 112
	outb(AMD_EC_CMD_RD, smbus->base + AMD_EC_CMD);

113 114 115
	status = amd_ec_wait_write(smbus);
	if (status)
		return status;
Linus Torvalds's avatar
Linus Torvalds committed
116 117
	outb(address, smbus->base + AMD_EC_DATA);

118 119 120
	status = amd_ec_wait_read(smbus);
	if (status)
		return status;
Linus Torvalds's avatar
Linus Torvalds committed
121 122 123 124 125
	*data = inb(smbus->base + AMD_EC_DATA);

	return 0;
}

Jean Delvare's avatar
Jean Delvare committed
126 127
static unsigned int amd_ec_write(struct amd_smbus *smbus, unsigned char address,
		unsigned char data)
Linus Torvalds's avatar
Linus Torvalds committed
128
{
129 130 131 132 133
	int status;

	status = amd_ec_wait_write(smbus);
	if (status)
		return status;
Linus Torvalds's avatar
Linus Torvalds committed
134 135
	outb(AMD_EC_CMD_WR, smbus->base + AMD_EC_CMD);

136 137 138
	status = amd_ec_wait_write(smbus);
	if (status)
		return status;
Linus Torvalds's avatar
Linus Torvalds committed
139 140
	outb(address, smbus->base + AMD_EC_DATA);

141 142 143
	status = amd_ec_wait_write(smbus);
	if (status)
		return status;
Linus Torvalds's avatar
Linus Torvalds committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	outb(data, smbus->base + AMD_EC_DATA);

	return 0;
}

/*
 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
 */

#define AMD_SMB_PRTCL	0x00	/* protocol, PEC */
#define AMD_SMB_STS	0x01	/* status */
#define AMD_SMB_ADDR	0x02	/* address */
#define AMD_SMB_CMD	0x03	/* command */
#define AMD_SMB_DATA	0x04	/* 32 data registers */
#define AMD_SMB_BCNT	0x24	/* number of data bytes */
#define AMD_SMB_ALRM_A	0x25	/* alarm address */
#define AMD_SMB_ALRM_D	0x26	/* 2 bytes alarm data */

#define AMD_SMB_STS_DONE	0x80
#define AMD_SMB_STS_ALRM	0x40
#define AMD_SMB_STS_RES		0x20
#define AMD_SMB_STS_STATUS	0x1f

#define AMD_SMB_STATUS_OK	0x00
#define AMD_SMB_STATUS_FAIL	0x07
#define AMD_SMB_STATUS_DNAK	0x10
#define AMD_SMB_STATUS_DERR	0x11
#define AMD_SMB_STATUS_CMD_DENY	0x12
#define AMD_SMB_STATUS_UNKNOWN	0x13
#define AMD_SMB_STATUS_ACC_DENY	0x17
#define AMD_SMB_STATUS_TIMEOUT	0x18
#define AMD_SMB_STATUS_NOTSUP	0x19
#define AMD_SMB_STATUS_BUSY	0x1A
#define AMD_SMB_STATUS_PEC	0x1F

#define AMD_SMB_PRTCL_WRITE		0x00
#define AMD_SMB_PRTCL_READ		0x01
#define AMD_SMB_PRTCL_QUICK		0x02
#define AMD_SMB_PRTCL_BYTE		0x04
#define AMD_SMB_PRTCL_BYTE_DATA		0x06
#define AMD_SMB_PRTCL_WORD_DATA		0x08
#define AMD_SMB_PRTCL_BLOCK_DATA	0x0a
#define AMD_SMB_PRTCL_PROC_CALL		0x0c
#define AMD_SMB_PRTCL_BLOCK_PROC_CALL	0x0d
#define AMD_SMB_PRTCL_I2C_BLOCK_DATA	0x4a
#define AMD_SMB_PRTCL_PEC		0x80


Jean Delvare's avatar
Jean Delvare committed
192 193 194
static s32 amd8111_access(struct i2c_adapter * adap, u16 addr,
		unsigned short flags, char read_write, u8 command, int size,
		union i2c_smbus_data * data)
Linus Torvalds's avatar
Linus Torvalds committed
195 196 197 198 199
{
	struct amd_smbus *smbus = adap->algo_data;
	unsigned char protocol, len, pec, temp[2];
	int i;

Jean Delvare's avatar
Jean Delvare committed
200 201
	protocol = (read_write == I2C_SMBUS_READ) ? AMD_SMB_PRTCL_READ
						  : AMD_SMB_PRTCL_WRITE;
Linus Torvalds's avatar
Linus Torvalds committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	pec = (flags & I2C_CLIENT_PEC) ? AMD_SMB_PRTCL_PEC : 0;

	switch (size) {
		case I2C_SMBUS_QUICK:
			protocol |= AMD_SMB_PRTCL_QUICK;
			read_write = I2C_SMBUS_WRITE;
			break;

		case I2C_SMBUS_BYTE:
			if (read_write == I2C_SMBUS_WRITE)
				amd_ec_write(smbus, AMD_SMB_CMD, command);
			protocol |= AMD_SMB_PRTCL_BYTE;
			break;

		case I2C_SMBUS_BYTE_DATA:
			amd_ec_write(smbus, AMD_SMB_CMD, command);
			if (read_write == I2C_SMBUS_WRITE)
				amd_ec_write(smbus, AMD_SMB_DATA, data->byte);
			protocol |= AMD_SMB_PRTCL_BYTE_DATA;
			break;

		case I2C_SMBUS_WORD_DATA:
			amd_ec_write(smbus, AMD_SMB_CMD, command);
			if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare's avatar
Jean Delvare committed
226 227 228 229
				amd_ec_write(smbus, AMD_SMB_DATA,
					     data->word & 0xff);
				amd_ec_write(smbus, AMD_SMB_DATA + 1,
					     data->word >> 8);
Linus Torvalds's avatar
Linus Torvalds committed
230 231 232 233 234 235 236
			}
			protocol |= AMD_SMB_PRTCL_WORD_DATA | pec;
			break;

		case I2C_SMBUS_BLOCK_DATA:
			amd_ec_write(smbus, AMD_SMB_CMD, command);
			if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare's avatar
Jean Delvare committed
237 238
				len = min_t(u8, data->block[0],
					    I2C_SMBUS_BLOCK_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
239 240
				amd_ec_write(smbus, AMD_SMB_BCNT, len);
				for (i = 0; i < len; i++)
Jean Delvare's avatar
Jean Delvare committed
241 242
					amd_ec_write(smbus, AMD_SMB_DATA + i,
						     data->block[i + 1]);
Linus Torvalds's avatar
Linus Torvalds committed
243 244 245 246 247
			}
			protocol |= AMD_SMB_PRTCL_BLOCK_DATA | pec;
			break;

		case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare's avatar
Jean Delvare committed
248 249
			len = min_t(u8, data->block[0],
				    I2C_SMBUS_BLOCK_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
250 251 252 253
			amd_ec_write(smbus, AMD_SMB_CMD, command);
			amd_ec_write(smbus, AMD_SMB_BCNT, len);
			if (read_write == I2C_SMBUS_WRITE)
				for (i = 0; i < len; i++)
Jean Delvare's avatar
Jean Delvare committed
254 255
					amd_ec_write(smbus, AMD_SMB_DATA + i,
						     data->block[i + 1]);
Linus Torvalds's avatar
Linus Torvalds committed
256 257 258 259 260
			protocol |= AMD_SMB_PRTCL_I2C_BLOCK_DATA;
			break;

		case I2C_SMBUS_PROC_CALL:
			amd_ec_write(smbus, AMD_SMB_CMD, command);
Jean Delvare's avatar
Jean Delvare committed
261
			amd_ec_write(smbus, AMD_SMB_DATA, data->word & 0xff);
Linus Torvalds's avatar
Linus Torvalds committed
262 263 264 265 266 267
			amd_ec_write(smbus, AMD_SMB_DATA + 1, data->word >> 8);
			protocol = AMD_SMB_PRTCL_PROC_CALL | pec;
			read_write = I2C_SMBUS_READ;
			break;

		case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare's avatar
Jean Delvare committed
268 269
			len = min_t(u8, data->block[0],
				    I2C_SMBUS_BLOCK_MAX - 1);
Linus Torvalds's avatar
Linus Torvalds committed
270 271 272
			amd_ec_write(smbus, AMD_SMB_CMD, command);
			amd_ec_write(smbus, AMD_SMB_BCNT, len);
			for (i = 0; i < len; i++)
Jean Delvare's avatar
Jean Delvare committed
273 274
				amd_ec_write(smbus, AMD_SMB_DATA + i,
					     data->block[i + 1]);
Linus Torvalds's avatar
Linus Torvalds committed
275 276 277 278 279 280
			protocol = AMD_SMB_PRTCL_BLOCK_PROC_CALL | pec;
			read_write = I2C_SMBUS_READ;
			break;

		default:
			dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
281
			return -EOPNOTSUPP;
Linus Torvalds's avatar
Linus Torvalds committed
282 283 284 285 286
	}

	amd_ec_write(smbus, AMD_SMB_ADDR, addr << 1);
	amd_ec_write(smbus, AMD_SMB_PRTCL, protocol);

287 288 289 290 291
	/* FIXME this discards status from ec_read(); so temp[0] will
	 * hold stack garbage ... the rest of this routine will act
	 * nonsensically.  Ignored ec_write() status might explain
	 * some such failures...
	 */
Linus Torvalds's avatar
Linus Torvalds committed
292 293 294 295 296 297 298 299 300 301 302 303 304
	amd_ec_read(smbus, AMD_SMB_STS, temp + 0);

	if (~temp[0] & AMD_SMB_STS_DONE) {
		udelay(500);
		amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
	}

	if (~temp[0] & AMD_SMB_STS_DONE) {
		msleep(1);
		amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
	}

	if ((~temp[0] & AMD_SMB_STS_DONE) || (temp[0] & AMD_SMB_STS_STATUS))
305
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

	if (read_write == I2C_SMBUS_WRITE)
		return 0;

	switch (size) {
		case I2C_SMBUS_BYTE:
		case I2C_SMBUS_BYTE_DATA:
			amd_ec_read(smbus, AMD_SMB_DATA, &data->byte);
			break;

		case I2C_SMBUS_WORD_DATA:
		case I2C_SMBUS_PROC_CALL:
			amd_ec_read(smbus, AMD_SMB_DATA, temp + 0);
			amd_ec_read(smbus, AMD_SMB_DATA + 1, temp + 1);
			data->word = (temp[1] << 8) | temp[0];
			break;

		case I2C_SMBUS_BLOCK_DATA:
		case I2C_SMBUS_BLOCK_PROC_CALL:
			amd_ec_read(smbus, AMD_SMB_BCNT, &len);
Jean Delvare's avatar
Jean Delvare committed
326
			len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX);
Linus Torvalds's avatar
Linus Torvalds committed
327 328
		case I2C_SMBUS_I2C_BLOCK_DATA:
			for (i = 0; i < len; i++)
Jean Delvare's avatar
Jean Delvare committed
329 330
				amd_ec_read(smbus, AMD_SMB_DATA + i,
					    data->block + i + 1);
Linus Torvalds's avatar
Linus Torvalds committed
331 332 333 334 335 336 337 338 339 340
			data->block[0] = len;
			break;
	}

	return 0;
}


static u32 amd8111_func(struct i2c_adapter *adapter)
{
Jean Delvare's avatar
Jean Delvare committed
341 342
	return	I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
		I2C_FUNC_SMBUS_BYTE_DATA |
Linus Torvalds's avatar
Linus Torvalds committed
343 344
		I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA |
		I2C_FUNC_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
345
		I2C_FUNC_SMBUS_I2C_BLOCK | I2C_FUNC_SMBUS_PEC;
Linus Torvalds's avatar
Linus Torvalds committed
346 347
}

348
static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds's avatar
Linus Torvalds committed
349 350 351 352 353 354 355 356 357 358 359 360
	.smbus_xfer = amd8111_access,
	.functionality = amd8111_func,
};


static struct pci_device_id amd8111_ids[] = {
	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_SMBUS2) },
	{ 0, }
};

MODULE_DEVICE_TABLE (pci, amd8111_ids);

Jean Delvare's avatar
Jean Delvare committed
361 362
static int __devinit amd8111_probe(struct pci_dev *dev,
		const struct pci_device_id *id)
Linus Torvalds's avatar
Linus Torvalds committed
363 364
{
	struct amd_smbus *smbus;
Jean Delvare's avatar
Jean Delvare committed
365
	int error;
Linus Torvalds's avatar
Linus Torvalds committed
366

Jean Delvare's avatar
Jean Delvare committed
367
	if (!(pci_resource_flags(dev, 0) & IORESOURCE_IO))
Linus Torvalds's avatar
Linus Torvalds committed
368 369
		return -ENODEV;

370
	smbus = kzalloc(sizeof(struct amd_smbus), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
371 372 373 374 375 376 377
	if (!smbus)
		return -ENOMEM;

	smbus->dev = dev;
	smbus->base = pci_resource_start(dev, 0);
	smbus->size = pci_resource_len(dev, 0);

378 379 380 381
	error = acpi_check_resource_conflict(&dev->resource[0]);
	if (error)
		goto out_kfree;

Jean Delvare's avatar
Jean Delvare committed
382 383
	if (!request_region(smbus->base, smbus->size, amd8111_driver.name)) {
		error = -EBUSY;
Linus Torvalds's avatar
Linus Torvalds committed
384
		goto out_kfree;
Jean Delvare's avatar
Jean Delvare committed
385
	}
Linus Torvalds's avatar
Linus Torvalds committed
386 387

	smbus->adapter.owner = THIS_MODULE;
David Brownell's avatar
David Brownell committed
388
	snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
Linus Torvalds's avatar
Linus Torvalds committed
389
		"SMBus2 AMD8111 adapter at %04x", smbus->base);
390
	smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Linus Torvalds's avatar
Linus Torvalds committed
391 392 393
	smbus->adapter.algo = &smbus_algorithm;
	smbus->adapter.algo_data = smbus;

394
	/* set up the sysfs linkage to our parent device */
Linus Torvalds's avatar
Linus Torvalds committed
395 396
	smbus->adapter.dev.parent = &dev->dev;

Jean Delvare's avatar
Jean Delvare committed
397
	pci_write_config_dword(smbus->dev, AMD_PCI_MISC, 0);
Linus Torvalds's avatar
Linus Torvalds committed
398 399 400 401 402 403 404 405 406 407 408
	error = i2c_add_adapter(&smbus->adapter);
	if (error)
		goto out_release_region;

	pci_set_drvdata(dev, smbus);
	return 0;

 out_release_region:
	release_region(smbus->base, smbus->size);
 out_kfree:
	kfree(smbus);
Jean Delvare's avatar
Jean Delvare committed
409
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
}

static void __devexit amd8111_remove(struct pci_dev *dev)
{
	struct amd_smbus *smbus = pci_get_drvdata(dev);

	i2c_del_adapter(&smbus->adapter);
	release_region(smbus->base, smbus->size);
	kfree(smbus);
}

static struct pci_driver amd8111_driver = {
	.name		= "amd8111_smbus2",
	.id_table	= amd8111_ids,
	.probe		= amd8111_probe,
	.remove		= __devexit_p(amd8111_remove),
};

static int __init i2c_amd8111_init(void)
{
	return pci_register_driver(&amd8111_driver);
}

static void __exit i2c_amd8111_exit(void)
{
	pci_unregister_driver(&amd8111_driver);
}

module_init(i2c_amd8111_init);
module_exit(i2c_amd8111_exit);