bbc_i2c.c 9.6 KB
Newer Older
1
/* bbc_i2c.c: I2C low-level driver for BBC device on UltraSPARC-III
L
Linus Torvalds 已提交
2 3
 *            platforms.
 *
4
 * Copyright (C) 2001, 2008 David S. Miller (davem@davemloft.net)
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/interrupt.h>
16 17
#include <linux/of.h>
#include <linux/of_device.h>
L
Linus Torvalds 已提交
18
#include <asm/bbc.h>
19
#include <asm/io.h>
L
Linus Torvalds 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

#include "bbc_i2c.h"

/* Convert this driver to use i2c bus layer someday... */
#define I2C_PCF_PIN	0x80
#define I2C_PCF_ESO	0x40
#define I2C_PCF_ES1	0x20
#define I2C_PCF_ES2	0x10
#define I2C_PCF_ENI	0x08
#define I2C_PCF_STA	0x04
#define I2C_PCF_STO	0x02
#define I2C_PCF_ACK	0x01

#define I2C_PCF_START    (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ENI | I2C_PCF_STA | I2C_PCF_ACK)
#define I2C_PCF_STOP     (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_STO | I2C_PCF_ACK)
#define I2C_PCF_REPSTART (              I2C_PCF_ESO | I2C_PCF_STA | I2C_PCF_ACK)
#define I2C_PCF_IDLE     (I2C_PCF_PIN | I2C_PCF_ESO               | I2C_PCF_ACK)

#define I2C_PCF_INI 0x40   /* 1 if not initialized */
#define I2C_PCF_STS 0x20
#define I2C_PCF_BER 0x10
#define I2C_PCF_AD0 0x08
#define I2C_PCF_LRB 0x08
#define I2C_PCF_AAS 0x04
#define I2C_PCF_LAB 0x02
#define I2C_PCF_BB  0x01

/* The BBC devices have two I2C controllers.  The first I2C controller
 * connects mainly to configuration proms (NVRAM, cpu configuration,
 * dimm types, etc.).  Whereas the second I2C controller connects to
 * environmental control devices such as fans and temperature sensors.
 * The second controller also connects to the smartcard reader, if present.
 */

54
static void set_device_claimage(struct bbc_i2c_bus *bp, struct of_device *op, int val)
L
Linus Torvalds 已提交
55 56 57 58
{
	int i;

	for (i = 0; i < NUM_CHILDREN; i++) {
59
		if (bp->devs[i].device == op) {
L
Linus Torvalds 已提交
60 61 62 63 64 65 66 67 68
			bp->devs[i].client_claimed = val;
			return;
		}
	}
}

#define claim_device(BP,ECHILD)		set_device_claimage(BP,ECHILD,1)
#define release_device(BP,ECHILD)	set_device_claimage(BP,ECHILD,0)

69
struct of_device *bbc_i2c_getdev(struct bbc_i2c_bus *bp, int index)
L
Linus Torvalds 已提交
70
{
71 72
	struct of_device *op = NULL;
	int curidx = 0, i;
L
Linus Torvalds 已提交
73

74 75
	for (i = 0; i < NUM_CHILDREN; i++) {
		if (!(op = bp->devs[i].device))
L
Linus Torvalds 已提交
76
			break;
77 78 79 80
		if (curidx == index)
			goto out;
		op = NULL;
		curidx++;
L
Linus Torvalds 已提交
81 82 83 84
	}

out:
	if (curidx == index)
85
		return op;
L
Linus Torvalds 已提交
86 87 88
	return NULL;
}

89
struct bbc_i2c_client *bbc_i2c_attach(struct bbc_i2c_bus *bp, struct of_device *op)
L
Linus Torvalds 已提交
90 91
{
	struct bbc_i2c_client *client;
92
	const u32 *reg;
L
Linus Torvalds 已提交
93

94
	client = kzalloc(sizeof(*client), GFP_KERNEL);
L
Linus Torvalds 已提交
95 96 97
	if (!client)
		return NULL;
	client->bp = bp;
98 99 100 101 102 103 104
	client->op = op;

	reg = of_get_property(op->node, "reg", NULL);
	if (!reg) {
		kfree(client);
		return NULL;
	}
L
Linus Torvalds 已提交
105

106 107 108 109
	client->bus = reg[0];
	client->address = reg[1];

	claim_device(bp, op);
L
Linus Torvalds 已提交
110 111 112 113 114 115 116

	return client;
}

void bbc_i2c_detach(struct bbc_i2c_client *client)
{
	struct bbc_i2c_bus *bp = client->bp;
117
	struct of_device *op = client->op;
L
Linus Torvalds 已提交
118

119
	release_device(bp, op);
L
Linus Torvalds 已提交
120 121 122 123 124 125 126 127 128 129 130 131
	kfree(client);
}

static int wait_for_pin(struct bbc_i2c_bus *bp, u8 *status)
{
	DECLARE_WAITQUEUE(wait, current);
	int limit = 32;
	int ret = 1;

	bp->waiting = 1;
	add_wait_queue(&bp->wq, &wait);
	while (limit-- > 0) {
132 133 134 135 136 137 138 139
		unsigned long val;

		val = wait_event_interruptible_timeout(
				bp->wq,
				(((*status = readb(bp->i2c_control_regs + 0))
				  & I2C_PCF_PIN) == 0),
				msecs_to_jiffies(250));
		if (val > 0) {
L
Linus Torvalds 已提交
140 141 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 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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
			ret = 0;
			break;
		}
	}
	remove_wait_queue(&bp->wq, &wait);
	bp->waiting = 0;

	return ret;
}

int bbc_i2c_writeb(struct bbc_i2c_client *client, unsigned char val, int off)
{
	struct bbc_i2c_bus *bp = client->bp;
	int address = client->address;
	u8 status;
	int ret = -1;

	if (bp->i2c_bussel_reg != NULL)
		writeb(client->bus, bp->i2c_bussel_reg);

	writeb(address, bp->i2c_control_regs + 0x1);
	writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
	if (wait_for_pin(bp, &status))
		goto out;

	writeb(off, bp->i2c_control_regs + 0x1);
	if (wait_for_pin(bp, &status) ||
	    (status & I2C_PCF_LRB) != 0)
		goto out;

	writeb(val, bp->i2c_control_regs + 0x1);
	if (wait_for_pin(bp, &status))
		goto out;

	ret = 0;

out:
	writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
	return ret;
}

int bbc_i2c_readb(struct bbc_i2c_client *client, unsigned char *byte, int off)
{
	struct bbc_i2c_bus *bp = client->bp;
	unsigned char address = client->address, status;
	int ret = -1;

	if (bp->i2c_bussel_reg != NULL)
		writeb(client->bus, bp->i2c_bussel_reg);

	writeb(address, bp->i2c_control_regs + 0x1);
	writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
	if (wait_for_pin(bp, &status))
		goto out;

	writeb(off, bp->i2c_control_regs + 0x1);
	if (wait_for_pin(bp, &status) ||
	    (status & I2C_PCF_LRB) != 0)
		goto out;

	writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);

	address |= 0x1; /* READ */

	writeb(address, bp->i2c_control_regs + 0x1);
	writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
	if (wait_for_pin(bp, &status))
		goto out;

	/* Set PIN back to one so the device sends the first
	 * byte.
	 */
	(void) readb(bp->i2c_control_regs + 0x1);
	if (wait_for_pin(bp, &status))
		goto out;

	writeb(I2C_PCF_ESO | I2C_PCF_ENI, bp->i2c_control_regs + 0x0);
	*byte = readb(bp->i2c_control_regs + 0x1);
	if (wait_for_pin(bp, &status))
		goto out;

	ret = 0;

out:
	writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
	(void) readb(bp->i2c_control_regs + 0x1);

	return ret;
}

int bbc_i2c_write_buf(struct bbc_i2c_client *client,
		      char *buf, int len, int off)
{
	int ret = 0;

	while (len > 0) {
		int err = bbc_i2c_writeb(client, *buf, off);

		if (err < 0) {
			ret = err;
			break;
		}

		len--;
		buf++;
		off++;
	}
	return ret;
}

int bbc_i2c_read_buf(struct bbc_i2c_client *client,
		     char *buf, int len, int off)
{
	int ret = 0;

	while (len > 0) {
		int err = bbc_i2c_readb(client, buf, off);
		if (err < 0) {
			ret = err;
			break;
		}
		len--;
		buf++;
		off++;
	}

	return ret;
}

EXPORT_SYMBOL(bbc_i2c_getdev);
EXPORT_SYMBOL(bbc_i2c_attach);
EXPORT_SYMBOL(bbc_i2c_detach);
EXPORT_SYMBOL(bbc_i2c_writeb);
EXPORT_SYMBOL(bbc_i2c_readb);
EXPORT_SYMBOL(bbc_i2c_write_buf);
EXPORT_SYMBOL(bbc_i2c_read_buf);

277
static irqreturn_t bbc_i2c_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
278 279 280 281 282 283 284 285
{
	struct bbc_i2c_bus *bp = dev_id;

	/* PIN going from set to clear is the only event which
	 * makes the i2c assert an interrupt.
	 */
	if (bp->waiting &&
	    !(readb(bp->i2c_control_regs + 0x0) & I2C_PCF_PIN))
286
		wake_up_interruptible(&bp->wq);
L
Linus Torvalds 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299

	return IRQ_HANDLED;
}

static void __init reset_one_i2c(struct bbc_i2c_bus *bp)
{
	writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
	writeb(bp->own, bp->i2c_control_regs + 0x1);
	writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
	writeb(bp->clock, bp->i2c_control_regs + 0x1);
	writeb(I2C_PCF_IDLE, bp->i2c_control_regs + 0x0);
}

300
static struct bbc_i2c_bus * __init attach_one_i2c(struct of_device *op, int index)
L
Linus Torvalds 已提交
301
{
302
	struct bbc_i2c_bus *bp;
303
	struct device_node *dp;
L
Linus Torvalds 已提交
304 305
	int entry;

306
	bp = kzalloc(sizeof(*bp), GFP_KERNEL);
L
Linus Torvalds 已提交
307
	if (!bp)
308
		return NULL;
L
Linus Torvalds 已提交
309

310
	bp->i2c_control_regs = of_ioremap(&op->resource[0], 0, 0x2, "bbc_i2c_regs");
L
Linus Torvalds 已提交
311 312 313
	if (!bp->i2c_control_regs)
		goto fail;

314 315 316
	bp->i2c_bussel_reg = of_ioremap(&op->resource[1], 0, 0x1, "bbc_i2c_bussel");
	if (!bp->i2c_bussel_reg)
		goto fail;
L
Linus Torvalds 已提交
317 318 319

	bp->waiting = 0;
	init_waitqueue_head(&bp->wq);
320
	if (request_irq(op->irqs[0], bbc_i2c_interrupt,
321
			IRQF_SHARED, "bbc_i2c", bp))
L
Linus Torvalds 已提交
322 323 324
		goto fail;

	bp->index = index;
325
	bp->op = op;
L
Linus Torvalds 已提交
326 327 328 329

	spin_lock_init(&bp->lock);

	entry = 0;
330 331 332 333 334 335 336
	for (dp = op->node->child;
	     dp && entry < 8;
	     dp = dp->sibling, entry++) {
		struct of_device *child_op;

		child_op = of_find_device_by_node(dp);
		bp->devs[entry].device = child_op;
L
Linus Torvalds 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349
		bp->devs[entry].client_claimed = 0;
	}

	writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
	bp->own = readb(bp->i2c_control_regs + 0x01);
	writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
	bp->clock = readb(bp->i2c_control_regs + 0x01);

	printk(KERN_INFO "i2c-%d: Regs at %p, %d devices, own %02x, clock %02x.\n",
	       bp->index, bp->i2c_control_regs, entry, bp->own, bp->clock);

	reset_one_i2c(bp);

350
	return bp;
L
Linus Torvalds 已提交
351 352 353

fail:
	if (bp->i2c_bussel_reg)
354
		of_iounmap(&op->resource[1], bp->i2c_bussel_reg, 1);
L
Linus Torvalds 已提交
355
	if (bp->i2c_control_regs)
356
		of_iounmap(&op->resource[0], bp->i2c_control_regs, 2);
L
Linus Torvalds 已提交
357
	kfree(bp);
358
	return NULL;
L
Linus Torvalds 已提交
359 360
}

361 362
extern int bbc_envctrl_init(struct bbc_i2c_bus *bp);
extern void bbc_envctrl_cleanup(struct bbc_i2c_bus *bp);
L
Linus Torvalds 已提交
363

A
Al Viro 已提交
364
static int __init bbc_i2c_probe(struct of_device *op,
365
				   const struct of_device_id *match)
L
Linus Torvalds 已提交
366
{
367
	struct bbc_i2c_bus *bp;
L
Linus Torvalds 已提交
368 369
	int err, index = 0;

370 371 372
	bp = attach_one_i2c(op, index);
	if (!bp)
		return -EINVAL;
L
Linus Torvalds 已提交
373

374 375 376 377 378 379 380 381 382 383
	err = bbc_envctrl_init(bp);
	if (err) {
		free_irq(op->irqs[0], bp);
		if (bp->i2c_bussel_reg)
			of_iounmap(&op->resource[0], bp->i2c_bussel_reg, 1);
		if (bp->i2c_control_regs)
			of_iounmap(&op->resource[1], bp->i2c_control_regs, 2);
		kfree(bp);
	} else {
		dev_set_drvdata(&op->dev, bp);
L
Linus Torvalds 已提交
384 385 386 387 388
	}

	return err;
}

A
Al Viro 已提交
389
static int __exit bbc_i2c_remove(struct of_device *op)
L
Linus Torvalds 已提交
390
{
391 392 393 394 395
	struct bbc_i2c_bus *bp = dev_get_drvdata(&op->dev);

	bbc_envctrl_cleanup(bp);

	free_irq(op->irqs[0], bp);
L
Linus Torvalds 已提交
396

397 398 399 400
	if (bp->i2c_bussel_reg)
		of_iounmap(&op->resource[0], bp->i2c_bussel_reg, 1);
	if (bp->i2c_control_regs)
		of_iounmap(&op->resource[1], bp->i2c_control_regs, 2);
L
Linus Torvalds 已提交
401

402
	kfree(bp);
L
Linus Torvalds 已提交
403

404 405
	return 0;
}
L
Linus Torvalds 已提交
406

407
static const struct of_device_id bbc_i2c_match[] = {
408 409 410 411 412 413 414
	{
		.name = "i2c",
		.compatible = "SUNW,bbc-i2c",
	},
	{},
};
MODULE_DEVICE_TABLE(of, bbc_i2c_match);
L
Linus Torvalds 已提交
415

416 417 418 419
static struct of_platform_driver bbc_i2c_driver = {
	.name		= "bbc_i2c",
	.match_table	= bbc_i2c_match,
	.probe		= bbc_i2c_probe,
A
Al Viro 已提交
420
	.remove		= __exit_p(bbc_i2c_remove),
421
};
L
Linus Torvalds 已提交
422

423 424 425 426 427 428 429 430
static int __init bbc_i2c_init(void)
{
	return of_register_driver(&bbc_i2c_driver, &of_bus_type);
}

static void __exit bbc_i2c_exit(void)
{
	of_unregister_driver(&bbc_i2c_driver);
L
Linus Torvalds 已提交
431 432 433
}

module_init(bbc_i2c_init);
434 435
module_exit(bbc_i2c_exit);

436
MODULE_LICENSE("GPL");