summaryrefslogtreecommitdiffstats
path: root/arch/powerpc/platforms/powermac/pmac_low_i2c.c
blob: f3f39e8e337a632eda28e2103fef2042ae7901db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
 *  arch/ppc/platforms/pmac_low_i2c.c
 *
 *  Copyright (C) 2003 Ben. Herrenschmidt (benh@kernel.crashing.org)
 *
 *  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.
 *
 *  This file contains some low-level i2c access routines that
 *  need to be used by various bits of the PowerMac platform code
 *  at times where the real asynchronous & interrupt driven driver
 *  cannot be used. The API borrows some semantics from the darwin
 *  driver in order to ease the implementation of the platform
 *  properties parser
 */

#undef DEBUG

#include <linux/config.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/adb.h>
#include <linux/pmu.h>
#include <asm/keylargo.h>
#include <asm/uninorth.h>
#include <asm/io.h>
#include <asm/prom.h>
#include <asm/machdep.h>
#include <asm/pmac_low_i2c.h>

#define MAX_LOW_I2C_HOST	4

#ifdef DEBUG
#define DBG(x...) do {\
		printk(KERN_DEBUG "KW:" x);	\
	} while(0)
#else
#define DBG(x...)
#endif

struct low_i2c_host;

typedef int (*low_i2c_func_t)(struct low_i2c_host *host, u8 addr, u8 sub, u8 *data, int len);

struct low_i2c_host
{
	struct device_node	*np;		/* OF device node */
	struct semaphore	mutex;		/* Access mutex for use by i2c-keywest */
	low_i2c_func_t		func;		/* Access function */
	unsigned int		is_open : 1;	/* Poor man's access control */
	int			mode;		/* Current mode */
	int			channel;	/* Current channel */
	int			num_channels;	/* Number of channels */
	void __iomem		*base;		/* For keywest-i2c, base address */
	int			bsteps;		/* And register stepping */
	int			speed;		/* And speed */
};

static struct low_i2c_host	low_i2c_hosts[MAX_LOW_I2C_HOST];

/* No locking is necessary on allocation, we are running way before
 * anything can race with us
 */
static struct low_i2c_host *find_low_i2c_host(struct device_node *np)
{
	int i;

	for (i = 0; i < MAX_LOW_I2C_HOST; i++)
		if (low_i2c_hosts[i].np == np)
			return &low_i2c_hosts[i];
	return NULL;
}

/*
 *
 * i2c-keywest implementation (UniNorth, U2, U3, Keylargo's)
 *
 */

/*
 * Keywest i2c definitions borrowed from drivers/i2c/i2c-keywest.h,
 * should be moved somewhere in include/asm-ppc/
 */
/* Register indices */
typedef enum {
	reg_mode = 0,
	reg_control,
	reg_status,
	reg_isr,
	reg_ier,
	reg_addr,
	reg_subaddr,
	reg_data
} reg_t;


/* Mode register */
#define KW_I2C_MODE_100KHZ	0x00
#define KW_I2C_MODE_50KHZ	0x01
#define KW_I2C_MODE_25KHZ	0x02
#define KW_I2C_MODE_DUMB	0x00
#define KW_I2C_MODE_STANDARD	0x04
#define KW_I2C_MODE_STANDARDSUB	0x08
#define KW_I2C_MODE_COMBINED	0x0C
#define KW_I2C_MODE_MODE_MASK	0x0C
#define KW_I2C_MODE_CHAN_MASK	0xF0

/* Control register */
#define KW_I2C_CTL_AAK		0x01
#define KW_I2C_CTL_XADDR	0x02
#define KW_I2C_CTL_STOP		0x04
#define KW_I2C_CTL_START	0x08

/* Status register */
#define KW_I2C_STAT_BUSY	0x01
#define KW_I2C_STAT_LAST_AAK	0x02
#define KW_I2C_STAT_LAST_RW	0x04
#define KW_I2C_STAT_SDA		0x08
#define KW_I2C_STAT_SCL		0x10

/* IER & ISR registers */
#define KW_I2C_IRQ_DATA		0x01
#define KW_I2C_IRQ_ADDR		0x02
#define KW_I2C_IRQ_STOP		0x04
#define KW_I2C_IRQ_START	0x08
#define KW_I2C_IRQ_MASK		0x0F

/* State machine states */
enum {
	state_idle,
	state_addr,
	state_read,
	state_write,
	state_stop,
	state_dead
};

#define WRONG_STATE(name) do {\
		printk(KERN_DEBUG "KW: wrong state. Got %s, state: %s (isr: %02x)\n", \
		       name, __kw_state_names[state], isr); \
	} while(0)

static const char *__kw_state_names[] = {
	"state_idle",
	"state_addr",
	"state_read",
	"state_write",
	"state_stop",
	"state_dead"
};

static inline u8 __kw_read_reg(struct low_i2c_host *host, reg_t reg)
{
	return readb(host->base + (((unsigned int)reg) << host->bsteps));
}

static inline void __kw_write_reg(struct low_i2c_host *host, reg_t reg, u8 val)
{
	writeb(val, host->base + (((unsigned)reg) << host->bsteps));
	(void)__kw_read_reg(host, reg_subaddr);
}

#define kw_write_reg(reg, val)	__kw_write_reg(host, reg, val) 
#define kw_read_reg(reg)	__kw_read_reg(host, reg) 


/* Don't schedule, the g5 fan controller is too
 * timing sensitive
 */
static u8 kw_wait_interrupt(struct low_i2c_host* host)
{
	int i, j;
	u8 isr;
	
	for (i = 0; i < 100000; i++) {
		isr = kw_read_reg(reg_isr) & KW_I2C_IRQ_MASK;
		if (isr != 0)
			return isr;

		/* This code is used with the timebase frozen, we cannot rely
		 * on udelay ! For now, just use a bogus loop
		 */
		for (j = 1; j < 10000; j++)
			mb();
	}
	return isr;
}

static int kw_handle_interrupt(struct low_i2c_host *host, int state, int rw, int *rc, u8 **data, int *len, u8 isr)
{
	u8 ack;

	DBG("kw_handle_interrupt(%s, isr: %x)\n", __kw_state_names[state], isr);

	if (isr == 0) {
		if (state != state_stop) {
			DBG("KW: Timeout !\n");
			*rc = -EIO;
			goto stop;
		}
		if (state == state_stop) {
			ack = kw_read_reg(reg_status);
			if (!(ack & KW_I2C_STAT_BUSY)) {
				state = state_idle;
				kw_write_reg(reg_ier, 0x00);
			}
		}
		return state;
	}

	if (isr & KW_I2C_IRQ_ADDR) {
		ack = kw_read_reg(reg_status);
		if (state != state_addr) {
			kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);
			WRONG_STATE("KW_I2C_IRQ_ADDR"); 
			*rc = -EIO;
			goto stop;
		}
		if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {			
			*rc = -ENODEV;
			DBG("KW: NAK on address\n");
			return state_stop;		     
		} else {
			if (rw) {
				state = state_read;
				if (*len > 1)
					kw_write_reg(reg_control, KW_I2C_CTL_AAK);
			} else {
				state = state_write;
				kw_write_reg(reg_data, **data);
				(*data)++; (*len)--;
			}
		}
		kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);
	}

	if (isr & KW_I2C_IRQ_DATA) {
		if (state == state_read) {
			**data = kw_read_reg(reg_data);
			(*data)++; (*len)--;
			kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
			if ((*len) == 0)
				state = state_stop;
			else if ((*len) == 1)
				kw_write_reg(reg_control, 0);
		} else if (state == state_write) {
			ack = kw_read_reg(reg_status);
			if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
				DBG("KW: nack on data write\n");
				*rc = -EIO;
				goto stop;
			} else if (*len) {
				kw_write_reg(reg_data, **data);
				(*data)++; (*len)--;
			} else {
				kw_write_reg(reg_control, KW_I2C_CTL_STOP);
				state = state_stop;
				*rc = 0;
			}
			kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
		} else {
			kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
			WRONG_STATE("KW_I2C_IRQ_DATA"); 
			if (state != state_stop) {
				*rc = -EIO;
				goto stop;
			}
		}
	}

	if (isr & KW_I2C_IRQ_STOP) {
		kw_write_reg(reg_isr, KW_I2C_IRQ_STOP);
		if (state != state_stop) {
			WRONG_STATE("KW_I2C_IRQ_STOP");
			*rc = -EIO;
		}
		return state_idle;
	}

	if (isr & KW_I2C_IRQ_START)
		kw_write_reg(reg_isr, KW_I2C_IRQ_START);

	return state;

 stop:
	kw_write_reg(reg_control, KW_I2C_CTL_STOP);	
	return state_stop;
}

static int keywest_low_i2c_func(struct low_i2c_host *host, u8 addr, u8 subaddr, u8 *data, int len)
{
	u8 mode_reg = host->speed;
	int state = state_addr;
	int rc = 0;

	/* Setup mode & subaddress if any */
	switch(host->mode) {
	case pmac_low_i2c_mode_dumb:
		printk(KERN_ERR "low_i2c: Dumb mode not supported !\n");
		return -EINVAL;
	case pmac_low_i2c_mode_std:
		mode_reg |= KW_I2C_MODE_STANDARD;
		break;
	case pmac_low_i2c_mode_stdsub:
		mode_reg |= KW_I2C_MODE_STANDARDSUB;
		break;
	case pmac_low_i2c_mode_combined:
		mode_reg |= KW_I2C_MODE_COMBINED;
		break;
	}

	/* Setup channel & clear pending irqs */
	kw_write_reg(reg_isr, kw_read_reg(reg_isr));
	kw_write_reg(reg_mode, mode_reg | (host->channel << 4));
	kw_write_reg(reg_status, 0);

	/* Set up address and r/w bit */
	kw_write_reg(reg_addr, addr);

	/* Set up the sub address */
	if ((mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB
	    || (mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)
		kw_write_reg(reg_subaddr, subaddr);

	/* Start sending address & disable interrupt*/
	kw_write_reg(reg_ier, 0 /*KW_I2C_IRQ_MASK*/);
	kw_write_reg(reg_control, KW_I2C_CTL_XADDR);

	/* State machine, to turn into an interrupt handler */
	while(state != state_idle) {
		u8 isr = kw_wait_interrupt(host);
		state = kw_handle_interrupt(host, state, addr & 1, &rc, &data, &len, isr);
	}

	return rc;
}

static void keywest_low_i2c_add(struct device_node *np)
{
	struct low_i2c_host	*host = find_low_i2c_host(NULL);
	u32			*psteps, *prate, steps, aoffset = 0;
	struct device_node	*parent;

	if (host == NULL) {
		printk(KERN_ERR "low_i2c: Can't allocate host for %s\n",
		       np->full_name);
		return;
	}
	memset(host, 0, sizeof(*host));

	init_MUTEX(&host->mutex);
	host->np = of_node_get(np);	
	psteps = (u32 *)get_property(np, "AAPL,address-step", NULL);
	steps = psteps ? (*psteps) : 0x10;
	for (host->bsteps = 0; (steps & 0x01) == 0; host->bsteps++)
		steps >>= 1;
	parent = of_get_parent(np);
	host->num_channels = 1;
	if (parent && parent->name[0] == 'u') {
		host->num_channels = 2;
		aoffset = 3;
	}
	/* Select interface rate */
	host->speed = KW_I2C_MODE_100KHZ;
	prate = (u32 *)get_property(np, "AAPL,i2c-rate", NULL);
	if (prate) switch(*prate) {
	case 100:
		host->speed = KW_I2C_MODE_100KHZ;
		break;
	case 50:
		host->speed = KW_I2C_MODE_50KHZ;
		break;
	case 25:
		host->speed = KW_I2C_MODE_25KHZ;
		break;
	}	

	host->mode = pmac_low_i2c_mode_std;
	host->base = ioremap(np->addrs[0].address + aoffset,
						np->addrs[0].size);
	host->func = keywest_low_i2c_func;
}

/*
 *
 * PMU implementation
 *
 */


#ifdef CONFIG_ADB_PMU

static int pmu_low_i2c_func(struct low_i2c_host *host, u8 addr, u8 sub, u8 *data, int len)
{
	// TODO
	return -ENODEV;
}

static void pmu_low_i2c_add(struct device_node *np)
{
	struct low_i2c_host	*host = find_low_i2c_host(NULL);

	if (host == NULL) {
		printk(KERN_ERR "low_i2c: Can't allocate host for %s\n",
		       np->full_name);
		return;
	}
	memset(host, 0, sizeof(*host));

	init_MUTEX(&host->mutex);
	host->np = of_node_get(np);	
	host->num_channels = 3;
	host->mode = pmac_low_i2c_mode_std;
	host->func = pmu_low_i2c_func;
}

#endif /* CONFIG_ADB_PMU */

void __init pmac_init_low_i2c(void)
{
	struct device_node *np;

	/* Probe keywest-i2c busses */
	np = of_find_compatible_node(NULL, "i2c", "keywest-i2c");
	while(np) {
		keywest_low_i2c_add(np);
		np = of_find_compatible_node(np, "i2c", "keywest-i2c");
	}

#ifdef CONFIG_ADB_PMU
	/* Probe PMU busses */
	np = of_find_node_by_name(NULL, "via-pmu");
	if (np)
		pmu_low_i2c_add(np);
#endif /* CONFIG_ADB_PMU */

	/* TODO: Add CUDA support as well */
}

int pmac_low_i2c_lock(struct device_node *np)
{
	struct low_i2c_host *host = find_low_i2c_host(np);

	if (!host)
		return -ENODEV;
	down(&host->mutex);
	return 0;
}
EXPORT_SYMBOL(pmac_low_i2c_lock);

int pmac_low_i2c_unlock(struct device_node *np)
{
	struct low_i2c_host *host = find_low_i2c_host(np);

	if (!host)
		return -ENODEV;
	up(&host->mutex);
	return 0;
}
EXPORT_SYMBOL(pmac_low_i2c_unlock);


int pmac_low_i2c_open(struct device_node *np, int channel)
{
	struct low_i2c_host *host = find_low_i2c_host(np);

	if (!host)
		return -ENODEV;

	if (channel >= host->num_channels)
		return -EINVAL;

	down(&host->mutex);
	host->is_open = 1;
	host->channel = channel;

	return 0;
}
EXPORT_SYMBOL(pmac_low_i2c_open);

int pmac_low_i2c_close(struct device_node *np)
{
	struct low_i2c_host *host = find_low_i2c_host(np);

	if (!host)
		return -ENODEV;

	host->is_open = 0;
	up(&host->mutex);

	return 0;
}
EXPORT_SYMBOL(pmac_low_i2c_close);

int pmac_low_i2c_setmode(struct device_node *np, int mode)
{
	struct low_i2c_host *host = find_low_i2c_host(np);

	if (!host)
		return -ENODEV;
	WARN_ON(!host->is_open);
	host->mode = mode;

	return 0;
}
EXPORT_SYMBOL(pmac_low_i2c_setmode);

int pmac_low_i2c_xfer(struct device_node *np, u8 addrdir, u8 subaddr, u8 *data, int len)
{
	struct low_i2c_host *host = find_low_i2c_host(np);

	if (!host)
		return -ENODEV;
	WARN_ON(!host->is_open);

	return host->func(host, addrdir, subaddr, data, len);
}
EXPORT_SYMBOL(pmac_low_i2c_xfer);

OpenPOWER on IntegriCloud