2 * arch/ppc/platforms/pmac_low_i2c.c
4 * Copyright (C) 2003 Ben. Herrenschmidt (benh@kernel.crashing.org)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * This file contains some low-level i2c access routines that
12 * need to be used by various bits of the PowerMac platform code
13 * at times where the real asynchronous & interrupt driven driver
14 * cannot be used. The API borrows some semantics from the darwin
15 * driver in order to ease the implementation of the platform
19 #include <linux/config.h>
20 #include <linux/types.h>
21 #include <linux/delay.h>
22 #include <linux/sched.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/adb.h>
26 #include <linux/pmu.h>
27 #include <asm/keylargo.h>
28 #include <asm/uninorth.h>
31 #include <asm/machdep.h>
32 #include <asm/pmac_low_i2c.h>
34 #define MAX_LOW_I2C_HOST 4
37 #define DBG(x...) do {\
38 printk(KERN_DEBUG "KW:" x); \
46 typedef int (*low_i2c_func_t)(struct low_i2c_host *host, u8 addr, u8 sub, u8 *data, int len);
50 struct device_node *np; /* OF device node */
51 struct semaphore mutex; /* Access mutex for use by i2c-keywest */
52 low_i2c_func_t func; /* Access function */
53 int is_open : 1; /* Poor man's access control */
54 int mode; /* Current mode */
55 int channel; /* Current channel */
56 int num_channels; /* Number of channels */
57 unsigned long base; /* For keywest-i2c, base address */
58 int bsteps; /* And register stepping */
59 int speed; /* And speed */
62 static struct low_i2c_host low_i2c_hosts[MAX_LOW_I2C_HOST];
64 /* No locking is necessary on allocation, we are running way before
65 * anything can race with us
67 static struct low_i2c_host *find_low_i2c_host(struct device_node *np)
71 for (i = 0; i < MAX_LOW_I2C_HOST; i++)
72 if (low_i2c_hosts[i].np == np)
73 return &low_i2c_hosts[i];
79 * i2c-keywest implementation (UniNorth, U2, U3, Keylargo's)
84 * Keywest i2c definitions borrowed from drivers/i2c/i2c-keywest.h,
85 * should be moved somewhere in include/asm-ppc/
87 /* Register indices */
101 #define KW_I2C_MODE_100KHZ 0x00
102 #define KW_I2C_MODE_50KHZ 0x01
103 #define KW_I2C_MODE_25KHZ 0x02
104 #define KW_I2C_MODE_DUMB 0x00
105 #define KW_I2C_MODE_STANDARD 0x04
106 #define KW_I2C_MODE_STANDARDSUB 0x08
107 #define KW_I2C_MODE_COMBINED 0x0C
108 #define KW_I2C_MODE_MODE_MASK 0x0C
109 #define KW_I2C_MODE_CHAN_MASK 0xF0
111 /* Control register */
112 #define KW_I2C_CTL_AAK 0x01
113 #define KW_I2C_CTL_XADDR 0x02
114 #define KW_I2C_CTL_STOP 0x04
115 #define KW_I2C_CTL_START 0x08
117 /* Status register */
118 #define KW_I2C_STAT_BUSY 0x01
119 #define KW_I2C_STAT_LAST_AAK 0x02
120 #define KW_I2C_STAT_LAST_RW 0x04
121 #define KW_I2C_STAT_SDA 0x08
122 #define KW_I2C_STAT_SCL 0x10
124 /* IER & ISR registers */
125 #define KW_I2C_IRQ_DATA 0x01
126 #define KW_I2C_IRQ_ADDR 0x02
127 #define KW_I2C_IRQ_STOP 0x04
128 #define KW_I2C_IRQ_START 0x08
129 #define KW_I2C_IRQ_MASK 0x0F
131 /* State machine states */
141 #define WRONG_STATE(name) do {\
142 printk(KERN_DEBUG "KW: wrong state. Got %s, state: %s (isr: %02x)\n", \
143 name, __kw_state_names[state], isr); \
146 static const char *__kw_state_names[] = {
155 static inline u8 __kw_read_reg(struct low_i2c_host *host, reg_t reg)
157 return in_8(((volatile u8 *)host->base)
158 + (((unsigned)reg) << host->bsteps));
161 static inline void __kw_write_reg(struct low_i2c_host *host, reg_t reg, u8 val)
163 out_8(((volatile u8 *)host->base)
164 + (((unsigned)reg) << host->bsteps), val);
165 (void)__kw_read_reg(host, reg_subaddr);
168 #define kw_write_reg(reg, val) __kw_write_reg(host, reg, val)
169 #define kw_read_reg(reg) __kw_read_reg(host, reg)
172 /* Don't schedule, the g5 fan controller is too
175 static u8 kw_wait_interrupt(struct low_i2c_host* host)
180 for (i = 0; i < 200000; i++) {
181 isr = kw_read_reg(reg_isr) & KW_I2C_IRQ_MASK;
189 static int kw_handle_interrupt(struct low_i2c_host *host, int state, int rw, int *rc, u8 **data, int *len, u8 isr)
194 if (state != state_stop) {
195 DBG("KW: Timeout !\n");
199 if (state == state_stop) {
200 ack = kw_read_reg(reg_status);
201 if (!(ack & KW_I2C_STAT_BUSY)) {
203 kw_write_reg(reg_ier, 0x00);
209 if (isr & KW_I2C_IRQ_ADDR) {
210 ack = kw_read_reg(reg_status);
211 if (state != state_addr) {
212 kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);
213 WRONG_STATE("KW_I2C_IRQ_ADDR");
217 if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
219 DBG("KW: NAK on address\n");
225 kw_write_reg(reg_control, KW_I2C_CTL_AAK);
228 kw_write_reg(reg_data, **data);
232 kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);
235 if (isr & KW_I2C_IRQ_DATA) {
236 if (state == state_read) {
237 **data = kw_read_reg(reg_data);
239 kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
242 else if ((*len) == 1)
243 kw_write_reg(reg_control, 0);
244 } else if (state == state_write) {
245 ack = kw_read_reg(reg_status);
246 if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
247 DBG("KW: nack on data write\n");
251 kw_write_reg(reg_data, **data);
254 kw_write_reg(reg_control, KW_I2C_CTL_STOP);
258 kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
260 kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
261 WRONG_STATE("KW_I2C_IRQ_DATA");
262 if (state != state_stop) {
269 if (isr & KW_I2C_IRQ_STOP) {
270 kw_write_reg(reg_isr, KW_I2C_IRQ_STOP);
271 if (state != state_stop) {
272 WRONG_STATE("KW_I2C_IRQ_STOP");
278 if (isr & KW_I2C_IRQ_START)
279 kw_write_reg(reg_isr, KW_I2C_IRQ_START);
284 kw_write_reg(reg_control, KW_I2C_CTL_STOP);
288 static int keywest_low_i2c_func(struct low_i2c_host *host, u8 addr, u8 subaddr, u8 *data, int len)
290 u8 mode_reg = host->speed;
291 int state = state_addr;
294 /* Setup mode & subaddress if any */
296 case pmac_low_i2c_mode_dumb:
297 printk(KERN_ERR "low_i2c: Dumb mode not supported !\n");
299 case pmac_low_i2c_mode_std:
300 mode_reg |= KW_I2C_MODE_STANDARD;
302 case pmac_low_i2c_mode_stdsub:
303 mode_reg |= KW_I2C_MODE_STANDARDSUB;
304 kw_write_reg(reg_subaddr, subaddr);
306 case pmac_low_i2c_mode_combined:
307 mode_reg |= KW_I2C_MODE_COMBINED;
308 kw_write_reg(reg_subaddr, subaddr);
312 /* Setup channel & clear pending irqs */
313 kw_write_reg(reg_isr, kw_read_reg(reg_isr));
314 kw_write_reg(reg_mode, mode_reg | (host->channel << 4));
315 kw_write_reg(reg_status, 0);
317 /* Set up address and r/w bit */
318 kw_write_reg(reg_addr, addr);
320 /* Start sending address & disable interrupt*/
321 kw_write_reg(reg_ier, 0 /*KW_I2C_IRQ_MASK*/);
322 kw_write_reg(reg_control, KW_I2C_CTL_XADDR);
324 /* State machine, to turn into an interrupt handler */
325 while(state != state_idle) {
326 u8 isr = kw_wait_interrupt(host);
327 state = kw_handle_interrupt(host, state, addr & 1, &rc, &data, &len, isr);
333 static void keywest_low_i2c_add(struct device_node *np)
335 struct low_i2c_host *host = find_low_i2c_host(NULL);
336 unsigned long *psteps, *prate, steps, aoffset = 0;
337 struct device_node *parent;
340 printk(KERN_ERR "low_i2c: Can't allocate host for %s\n",
344 memset(host, 0, sizeof(*host));
346 init_MUTEX(&host->mutex);
347 host->np = of_node_get(np);
348 psteps = (unsigned long *)get_property(np, "AAPL,address-step", NULL);
349 steps = psteps ? (*psteps) : 0x10;
350 for (host->bsteps = 0; (steps & 0x01) == 0; host->bsteps++)
352 parent = of_get_parent(np);
353 host->num_channels = 1;
354 if (parent && parent->name[0] == 'u') {
355 host->num_channels = 2;
358 /* Select interface rate */
359 host->speed = KW_I2C_MODE_100KHZ;
360 prate = (unsigned long *)get_property(np, "AAPL,i2c-rate", NULL);
361 if (prate) switch(*prate) {
363 host->speed = KW_I2C_MODE_100KHZ;
366 host->speed = KW_I2C_MODE_50KHZ;
369 host->speed = KW_I2C_MODE_25KHZ;
372 host->mode = pmac_low_i2c_mode_std;
373 host->base = (unsigned long)ioremap(np->addrs[0].address + aoffset,
375 host->func = keywest_low_i2c_func;
385 #ifdef CONFIG_ADB_PMU
387 static int pmu_low_i2c_func(struct low_i2c_host *host, u8 addr, u8 sub, u8 *data, int len)
393 static void pmu_low_i2c_add(struct device_node *np)
395 struct low_i2c_host *host = find_low_i2c_host(NULL);
398 printk(KERN_ERR "low_i2c: Can't allocate host for %s\n",
402 memset(host, 0, sizeof(*host));
404 init_MUTEX(&host->mutex);
405 host->np = of_node_get(np);
406 host->num_channels = 3;
407 host->mode = pmac_low_i2c_mode_std;
408 host->func = pmu_low_i2c_func;
411 #endif /* CONFIG_ADB_PMU */
413 void __init pmac_init_low_i2c(void)
415 struct device_node *np;
417 /* Probe keywest-i2c busses */
418 np = of_find_compatible_node(NULL, "i2c", "keywest-i2c");
420 keywest_low_i2c_add(np);
421 np = of_find_compatible_node(np, "i2c", "keywest-i2c");
424 #ifdef CONFIG_ADB_PMU
425 /* Probe PMU busses */
426 np = of_find_node_by_name(NULL, "via-pmu");
429 #endif /* CONFIG_ADB_PMU */
431 /* TODO: Add CUDA support as well */
434 int pmac_low_i2c_lock(struct device_node *np)
436 struct low_i2c_host *host = find_low_i2c_host(np);
443 EXPORT_SYMBOL(pmac_low_i2c_lock);
445 int pmac_low_i2c_unlock(struct device_node *np)
447 struct low_i2c_host *host = find_low_i2c_host(np);
454 EXPORT_SYMBOL(pmac_low_i2c_unlock);
457 int pmac_low_i2c_open(struct device_node *np, int channel)
459 struct low_i2c_host *host = find_low_i2c_host(np);
464 if (channel >= host->num_channels)
469 host->channel = channel;
473 EXPORT_SYMBOL(pmac_low_i2c_open);
475 int pmac_low_i2c_close(struct device_node *np)
477 struct low_i2c_host *host = find_low_i2c_host(np);
487 EXPORT_SYMBOL(pmac_low_i2c_close);
489 int pmac_low_i2c_setmode(struct device_node *np, int mode)
491 struct low_i2c_host *host = find_low_i2c_host(np);
495 WARN_ON(!host->is_open);
500 EXPORT_SYMBOL(pmac_low_i2c_setmode);
502 int pmac_low_i2c_xfer(struct device_node *np, u8 addrdir, u8 subaddr, u8 *data, int len)
504 struct low_i2c_host *host = find_low_i2c_host(np);
508 WARN_ON(!host->is_open);
510 return host->func(host, addrdir, subaddr, data, len);
512 EXPORT_SYMBOL(pmac_low_i2c_xfer);