scx200_acb.c 13.7 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3 4
    Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>

    National Semiconductor SCx200 ACCESS.bus support
5
    Also supports the AMD CS5535 and AMD CS5536
6

L
Linus Torvalds 已提交
7 8 9
    Based on i2c-keywest.c which is:
        Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
        Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
10

L
Linus Torvalds 已提交
11 12 13 14
    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; either version 2 of the
    License, or (at your option) any later version.
15

L
Linus Torvalds 已提交
16 17 18 19
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.
20

L
Linus Torvalds 已提交
21 22 23 24 25
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

26 27
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
28 29 30 31 32 33
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/i2c.h>
#include <linux/pci.h>
34
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
35
#include <linux/delay.h>
36
#include <linux/mutex.h>
37
#include <linux/slab.h>
38
#include <linux/io.h>
L
Linus Torvalds 已提交
39 40 41 42 43

#include <linux/scx200.h>

MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
44
MODULE_ALIAS("platform:cs5535-smb");
L
Linus Torvalds 已提交
45 46 47 48 49 50 51
MODULE_LICENSE("GPL");

#define MAX_DEVICES 4
static int base[MAX_DEVICES] = { 0x820, 0x840 };
module_param_array(base, int, NULL, 0);
MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");

52
#define POLL_TIMEOUT	(HZ/5)
L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

enum scx200_acb_state {
	state_idle,
	state_address,
	state_command,
	state_repeat_start,
	state_quick,
	state_read,
	state_write,
};

static const char *scx200_acb_state_name[] = {
	"idle",
	"address",
	"command",
	"repeat_start",
	"quick",
	"read",
	"write",
};

/* Physical interface */
75
struct scx200_acb_iface {
L
Linus Torvalds 已提交
76 77 78
	struct scx200_acb_iface *next;
	struct i2c_adapter adapter;
	unsigned base;
79
	struct mutex mutex;
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

	/* State machine data */
	enum scx200_acb_state state;
	int result;
	u8 address_byte;
	u8 command;
	u8 *ptr;
	char needs_reset;
	unsigned len;
};

/* Register Definitions */
#define ACBSDA		(iface->base + 0)
#define ACBST		(iface->base + 1)
#define    ACBST_SDAST		0x40 /* SDA Status */
95
#define    ACBST_BER		0x20
L
Linus Torvalds 已提交
96 97 98 99 100 101 102 103
#define    ACBST_NEGACK		0x10 /* Negative Acknowledge */
#define    ACBST_STASTR		0x08 /* Stall After Start */
#define    ACBST_MASTER		0x02
#define ACBCST		(iface->base + 2)
#define    ACBCST_BB		0x02
#define ACBCTL1		(iface->base + 3)
#define    ACBCTL1_STASTRE	0x80
#define    ACBCTL1_NMINTE	0x40
104 105 106
#define    ACBCTL1_ACK		0x10
#define    ACBCTL1_STOP		0x02
#define    ACBCTL1_START	0x01
L
Linus Torvalds 已提交
107 108 109 110 111 112 113 114 115 116
#define ACBADDR		(iface->base + 4)
#define ACBCTL2		(iface->base + 5)
#define    ACBCTL2_ENABLE	0x01

/************************************************************************/

static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
{
	const char *errmsg;

117 118
	dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n",
		scx200_acb_state_name[iface->state], status);
L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126 127

	if (status & ACBST_BER) {
		errmsg = "bus error";
		goto error;
	}
	if (!(status & ACBST_MASTER)) {
		errmsg = "not master";
		goto error;
	}
128 129 130 131 132 133 134 135 136
	if (status & ACBST_NEGACK) {
		dev_dbg(&iface->adapter.dev, "negative ack in state %s\n",
			scx200_acb_state_name[iface->state]);

		iface->state = state_idle;
		iface->result = -ENXIO;

		outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
		outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
137 138 139

		/* Reset the status register */
		outb(0, ACBST);
140 141
		return;
	}
L
Linus Torvalds 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

	switch (iface->state) {
	case state_idle:
		dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
		break;

	case state_address:
		/* Do a pointer write first */
		outb(iface->address_byte & ~1, ACBSDA);

		iface->state = state_command;
		break;

	case state_command:
		outb(iface->command, ACBSDA);

		if (iface->address_byte & 1)
			iface->state = state_repeat_start;
		else
			iface->state = state_write;
		break;

	case state_repeat_start:
		outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
		/* fallthrough */
167

L
Linus Torvalds 已提交
168 169
	case state_quick:
		if (iface->address_byte & 1) {
170
			if (iface->len == 1)
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184
				outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
			else
				outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
			outb(iface->address_byte, ACBSDA);

			iface->state = state_read;
		} else {
			outb(iface->address_byte, ACBSDA);

			iface->state = state_write;
		}
		break;

	case state_read:
185 186
		/* Set ACK if _next_ byte will be the last one */
		if (iface->len == 2)
L
Linus Torvalds 已提交
187 188 189 190
			outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
		else
			outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);

191
		if (iface->len == 1) {
L
Linus Torvalds 已提交
192 193 194 195 196
			iface->result = 0;
			iface->state = state_idle;
			outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
		}

197 198 199
		*iface->ptr++ = inb(ACBSDA);
		--iface->len;

L
Linus Torvalds 已提交
200 201 202 203 204 205 206 207 208
		break;

	case state_write:
		if (iface->len == 0) {
			iface->result = 0;
			iface->state = state_idle;
			outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
			break;
		}
209

L
Linus Torvalds 已提交
210 211
		outb(*iface->ptr++, ACBSDA);
		--iface->len;
212

L
Linus Torvalds 已提交
213 214 215 216 217 218
		break;
	}

	return;

 error:
219 220 221 222
	dev_err(&iface->adapter.dev,
		"%s in state %s (addr=0x%02x, len=%d, status=0x%02x)\n", errmsg,
		scx200_acb_state_name[iface->state], iface->address_byte,
		iface->len, status);
L
Linus Torvalds 已提交
223 224 225 226 227 228 229 230

	iface->state = state_idle;
	iface->result = -EIO;
	iface->needs_reset = 1;
}

static void scx200_acb_poll(struct scx200_acb_iface *iface)
{
231
	u8 status;
L
Linus Torvalds 已提交
232 233 234
	unsigned long timeout;

	timeout = jiffies + POLL_TIMEOUT;
235
	while (1) {
L
Linus Torvalds 已提交
236
		status = inb(ACBST);
237 238 239 240

		/* Reset the status register to avoid the hang */
		outb(0, ACBST);

L
Linus Torvalds 已提交
241 242 243 244
		if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
			scx200_acb_machine(iface, status);
			return;
		}
245 246 247 248
		if (time_after(jiffies, timeout))
			break;
		cpu_relax();
		cond_resched();
L
Linus Torvalds 已提交
249 250
	}

251 252 253 254 255 256
	dev_err(&iface->adapter.dev, "timeout in state %s\n",
		scx200_acb_state_name[iface->state]);

	iface->state = state_idle;
	iface->result = -EIO;
	iface->needs_reset = 1;
L
Linus Torvalds 已提交
257 258 259 260 261
}

static void scx200_acb_reset(struct scx200_acb_iface *iface)
{
	/* Disable the ACCESS.bus device and Configure the SCL
262
	   frequency: 16 clock cycles */
L
Linus Torvalds 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	outb(0x70, ACBCTL2);
	/* Polling mode */
	outb(0, ACBCTL1);
	/* Disable slave address */
	outb(0, ACBADDR);
	/* Enable the ACCESS.bus device */
	outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
	/* Free STALL after START */
	outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1);
	/* Send a STOP */
	outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
	/* Clear BER, NEGACK and STASTR bits */
	outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
	/* Clear BB bit */
	outb(inb(ACBCST) | ACBCST_BB, ACBCST);
}

static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
281 282 283
				 u16 address, unsigned short flags,
				 char rw, u8 command, int size,
				 union i2c_smbus_data *data)
L
Linus Torvalds 已提交
284 285 286 287 288 289 290 291 292
{
	struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
	int len;
	u8 *buffer;
	u16 cur_word;
	int rc;

	switch (size) {
	case I2C_SMBUS_QUICK:
293 294 295 296
		len = 0;
		buffer = NULL;
		break;

L
Linus Torvalds 已提交
297
	case I2C_SMBUS_BYTE:
298 299
		len = 1;
		buffer = rw ? &data->byte : &command;
300 301
		break;

L
Linus Torvalds 已提交
302
	case I2C_SMBUS_BYTE_DATA:
303 304 305 306
		len = 1;
		buffer = &data->byte;
		break;

L
Linus Torvalds 已提交
307 308
	case I2C_SMBUS_WORD_DATA:
		len = 2;
309 310
		cur_word = cpu_to_le16(data->word);
		buffer = (u8 *)&cur_word;
L
Linus Torvalds 已提交
311
		break;
312

313
	case I2C_SMBUS_I2C_BLOCK_DATA:
314
		len = data->block[0];
315 316
		if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
			return -EINVAL;
317
		buffer = &data->block[1];
L
Linus Torvalds 已提交
318
		break;
319

L
Linus Torvalds 已提交
320
	default:
321
		return -EINVAL;
L
Linus Torvalds 已提交
322 323
	}

324 325 326
	dev_dbg(&adapter->dev,
		"size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
		size, address, command, len, rw);
L
Linus Torvalds 已提交
327 328

	if (!len && rw == I2C_SMBUS_READ) {
329
		dev_dbg(&adapter->dev, "zero length read\n");
L
Linus Torvalds 已提交
330 331 332
		return -EINVAL;
	}

333
	mutex_lock(&iface->mutex);
L
Linus Torvalds 已提交
334

335
	iface->address_byte = (address << 1) | rw;
L
Linus Torvalds 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	iface->command = command;
	iface->ptr = buffer;
	iface->len = len;
	iface->result = -EINVAL;
	iface->needs_reset = 0;

	outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);

	if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
		iface->state = state_quick;
	else
		iface->state = state_address;

	while (iface->state != state_idle)
		scx200_acb_poll(iface);

	if (iface->needs_reset)
		scx200_acb_reset(iface);

	rc = iface->result;

357
	mutex_unlock(&iface->mutex);
L
Linus Torvalds 已提交
358 359

	if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
360
		data->word = le16_to_cpu(cur_word);
L
Linus Torvalds 已提交
361 362

#ifdef DEBUG
363
	dev_dbg(&adapter->dev, "transfer done, result: %d", rc);
L
Linus Torvalds 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	if (buffer) {
		int i;
		printk(" data:");
		for (i = 0; i < len; ++i)
			printk(" %02x", buffer[i]);
	}
	printk("\n");
#endif

	return rc;
}

static u32 scx200_acb_func(struct i2c_adapter *adapter)
{
	return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
	       I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
380
	       I2C_FUNC_SMBUS_I2C_BLOCK;
L
Linus Torvalds 已提交
381 382 383
}

/* For now, we only handle combined mode (smbus) */
384
static const struct i2c_algorithm scx200_acb_algorithm = {
L
Linus Torvalds 已提交
385 386 387 388 389
	.smbus_xfer	= scx200_acb_smbus_xfer,
	.functionality	= scx200_acb_func,
};

static struct scx200_acb_iface *scx200_acb_list;
390
static DEFINE_MUTEX(scx200_acb_list_mutex);
L
Linus Torvalds 已提交
391

392
static int scx200_acb_probe(struct scx200_acb_iface *iface)
L
Linus Torvalds 已提交
393 394 395 396
{
	u8 val;

	/* Disable the ACCESS.bus device and Configure the SCL
397
	   frequency: 16 clock cycles */
L
Linus Torvalds 已提交
398 399 400
	outb(0x70, ACBCTL2);

	if (inb(ACBCTL2) != 0x70) {
401
		pr_debug("ACBCTL2 readback failed\n");
L
Linus Torvalds 已提交
402 403 404 405 406 407 408
		return -ENXIO;
	}

	outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);

	val = inb(ACBCTL1);
	if (val) {
409
		pr_debug("disabled, but ACBCTL1=0x%02x\n", val);
L
Linus Torvalds 已提交
410 411 412 413 414 415 416 417 418
		return -ENXIO;
	}

	outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);

	outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);

	val = inb(ACBCTL1);
	if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
419 420
		pr_debug("enabled, but NMINTE won't be set, ACBCTL1=0x%02x\n",
			 val);
L
Linus Torvalds 已提交
421 422 423 424 425 426
		return -ENXIO;
	}

	return 0;
}

427
static struct scx200_acb_iface *scx200_create_iface(const char *text,
428
		struct device *dev, int index)
L
Linus Torvalds 已提交
429 430 431 432
{
	struct scx200_acb_iface *iface;
	struct i2c_adapter *adapter;

433
	iface = kzalloc(sizeof(*iface), GFP_KERNEL);
L
Linus Torvalds 已提交
434
	if (!iface) {
435
		pr_err("can't allocate memory\n");
436
		return NULL;
L
Linus Torvalds 已提交
437 438 439 440
	}

	adapter = &iface->adapter;
	i2c_set_adapdata(adapter, iface);
D
David Brownell 已提交
441
	snprintf(adapter->name, sizeof(adapter->name), "%s ACB%d", text, index);
L
Linus Torvalds 已提交
442 443
	adapter->owner = THIS_MODULE;
	adapter->algo = &scx200_acb_algorithm;
444
	adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
445
	adapter->dev.parent = dev;
L
Linus Torvalds 已提交
446

447
	mutex_init(&iface->mutex);
L
Linus Torvalds 已提交
448

449 450 451
	return iface;
}

452
static int scx200_acb_create(struct scx200_acb_iface *iface)
453 454 455 456 457
{
	struct i2c_adapter *adapter;
	int rc;

	adapter = &iface->adapter;
L
Linus Torvalds 已提交
458 459 460

	rc = scx200_acb_probe(iface);
	if (rc) {
461
		pr_warn("probe failed\n");
462
		return rc;
L
Linus Torvalds 已提交
463 464 465 466 467
	}

	scx200_acb_reset(iface);

	if (i2c_add_adapter(adapter) < 0) {
468
		pr_err("failed to register\n");
469
		return -ENODEV;
L
Linus Torvalds 已提交
470 471
	}

472 473 474 475 476 477 478
	if (!adapter->dev.parent) {
		/* If there's no dev, we're tracking (ISA) ifaces manually */
		mutex_lock(&scx200_acb_list_mutex);
		iface->next = scx200_acb_list;
		scx200_acb_list = iface;
		mutex_unlock(&scx200_acb_list_mutex);
	}
L
Linus Torvalds 已提交
479 480

	return 0;
481
}
L
Linus Torvalds 已提交
482

483
static struct scx200_acb_iface *scx200_create_dev(const char *text,
484
		unsigned long base, int index, struct device *dev)
485 486 487 488
{
	struct scx200_acb_iface *iface;
	int rc;

489
	iface = scx200_create_iface(text, dev, index);
490 491

	if (iface == NULL)
492
		return NULL;
493

494
	if (!request_region(base, 8, iface->adapter.name)) {
495
		pr_err("can't allocate io 0x%lx-0x%lx\n", base, base + 8 - 1);
496 497 498
		goto errout_free;
	}

499
	iface->base = base;
500 501 502
	rc = scx200_acb_create(iface);

	if (rc == 0)
503
		return iface;
504

505
	release_region(base, 8);
506 507
 errout_free:
	kfree(iface);
508
	return NULL;
L
Linus Torvalds 已提交
509 510
}

511
static int scx200_probe(struct platform_device *pdev)
512 513
{
	struct scx200_acb_iface *iface;
514
	struct resource *res;
515

516 517 518 519
	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
	if (!res) {
		dev_err(&pdev->dev, "can't fetch device resource info\n");
		return -ENODEV;
520 521
	}

522 523 524
	iface = scx200_create_dev("CS5535", res->start, 0, &pdev->dev);
	if (!iface)
		return -EIO;
525

526 527 528
	dev_info(&pdev->dev, "SCx200 device '%s' registered\n",
			iface->adapter.name);
	platform_set_drvdata(pdev, iface);
L
Linus Torvalds 已提交
529

530
	return 0;
531 532
}

533
static void scx200_cleanup_iface(struct scx200_acb_iface *iface)
534 535 536 537 538
{
	i2c_del_adapter(&iface->adapter);
	release_region(iface->base, 8);
	kfree(iface);
}
539

540
static int scx200_remove(struct platform_device *pdev)
541
{
542
	struct scx200_acb_iface *iface;
543

544 545 546
	iface = platform_get_drvdata(pdev);
	platform_set_drvdata(pdev, NULL);
	scx200_cleanup_iface(iface);
547

548 549
	return 0;
}
550

551
static struct platform_driver scx200_pci_driver = {
552 553 554 555 556
	.driver = {
		.name = "cs5535-smb",
		.owner = THIS_MODULE,
	},
	.probe = scx200_probe,
557
	.remove = scx200_remove,
558
};
559

560
static DEFINE_PCI_DEVICE_TABLE(scx200_isa) = {
561 562 563 564
	{ PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
	{ PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
	{ 0, }
};
565

566 567 568
static __init void scx200_scan_isa(void)
{
	int i;
569

570 571
	if (!pci_dev_present(scx200_isa))
		return;
572

573 574 575
	for (i = 0; i < MAX_DEVICES; ++i) {
		if (base[i] == 0)
			continue;
576

577 578
		/* XXX: should we care about failures? */
		scx200_create_dev("SCx200", base[i], i, NULL);
579 580 581
	}
}

L
Linus Torvalds 已提交
582 583
static int __init scx200_acb_init(void)
{
584
	pr_debug("NatSemi SCx200 ACCESS.bus Driver\n");
L
Linus Torvalds 已提交
585

586 587
	/* First scan for ISA-based devices */
	scx200_scan_isa();	/* XXX: should we care about errors? */
L
Linus Torvalds 已提交
588

589 590 591
	/* If at least one bus was created, init must succeed */
	if (scx200_acb_list)
		return 0;
592 593

	/* No ISA devices; register the platform driver for PCI-based devices */
594
	return platform_driver_register(&scx200_pci_driver);
L
Linus Torvalds 已提交
595 596 597 598 599
}

static void __exit scx200_acb_cleanup(void)
{
	struct scx200_acb_iface *iface;
600

601
	platform_driver_unregister(&scx200_pci_driver);
602

603
	mutex_lock(&scx200_acb_list_mutex);
L
Linus Torvalds 已提交
604 605
	while ((iface = scx200_acb_list) != NULL) {
		scx200_acb_list = iface->next;
606
		mutex_unlock(&scx200_acb_list_mutex);
L
Linus Torvalds 已提交
607

608
		scx200_cleanup_iface(iface);
609

610
		mutex_lock(&scx200_acb_list_mutex);
L
Linus Torvalds 已提交
611
	}
612
	mutex_unlock(&scx200_acb_list_mutex);
L
Linus Torvalds 已提交
613 614 615 616
}

module_init(scx200_acb_init);
module_exit(scx200_acb_cleanup);