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 void __iomem
* 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(host
->base
+ (((unsigned)reg
) << host
->bsteps
));
160 static inline void __kw_write_reg(struct low_i2c_host
*host
, reg_t reg
, u8 val
)
162 out_8(host
->base
+ (((unsigned)reg
) << host
->bsteps
), val
);
163 (void)__kw_read_reg(host
, reg_subaddr
);
166 #define kw_write_reg(reg, val) __kw_write_reg(host, reg, val)
167 #define kw_read_reg(reg) __kw_read_reg(host, reg)
170 /* Don't schedule, the g5 fan controller is too
173 static u8
kw_wait_interrupt(struct low_i2c_host
* host
)
178 for (i
= 0; i
< 200000; i
++) {
179 isr
= kw_read_reg(reg_isr
) & KW_I2C_IRQ_MASK
;
187 static int kw_handle_interrupt(struct low_i2c_host
*host
, int state
, int rw
, int *rc
, u8
**data
, int *len
, u8 isr
)
192 if (state
!= state_stop
) {
193 DBG("KW: Timeout !\n");
197 if (state
== state_stop
) {
198 ack
= kw_read_reg(reg_status
);
199 if (!(ack
& KW_I2C_STAT_BUSY
)) {
201 kw_write_reg(reg_ier
, 0x00);
207 if (isr
& KW_I2C_IRQ_ADDR
) {
208 ack
= kw_read_reg(reg_status
);
209 if (state
!= state_addr
) {
210 kw_write_reg(reg_isr
, KW_I2C_IRQ_ADDR
);
211 WRONG_STATE("KW_I2C_IRQ_ADDR");
215 if ((ack
& KW_I2C_STAT_LAST_AAK
) == 0) {
217 DBG("KW: NAK on address\n");
223 kw_write_reg(reg_control
, KW_I2C_CTL_AAK
);
226 kw_write_reg(reg_data
, **data
);
230 kw_write_reg(reg_isr
, KW_I2C_IRQ_ADDR
);
233 if (isr
& KW_I2C_IRQ_DATA
) {
234 if (state
== state_read
) {
235 **data
= kw_read_reg(reg_data
);
237 kw_write_reg(reg_isr
, KW_I2C_IRQ_DATA
);
240 else if ((*len
) == 1)
241 kw_write_reg(reg_control
, 0);
242 } else if (state
== state_write
) {
243 ack
= kw_read_reg(reg_status
);
244 if ((ack
& KW_I2C_STAT_LAST_AAK
) == 0) {
245 DBG("KW: nack on data write\n");
249 kw_write_reg(reg_data
, **data
);
252 kw_write_reg(reg_control
, KW_I2C_CTL_STOP
);
256 kw_write_reg(reg_isr
, KW_I2C_IRQ_DATA
);
258 kw_write_reg(reg_isr
, KW_I2C_IRQ_DATA
);
259 WRONG_STATE("KW_I2C_IRQ_DATA");
260 if (state
!= state_stop
) {
267 if (isr
& KW_I2C_IRQ_STOP
) {
268 kw_write_reg(reg_isr
, KW_I2C_IRQ_STOP
);
269 if (state
!= state_stop
) {
270 WRONG_STATE("KW_I2C_IRQ_STOP");
276 if (isr
& KW_I2C_IRQ_START
)
277 kw_write_reg(reg_isr
, KW_I2C_IRQ_START
);
282 kw_write_reg(reg_control
, KW_I2C_CTL_STOP
);
286 static int keywest_low_i2c_func(struct low_i2c_host
*host
, u8 addr
, u8 subaddr
, u8
*data
, int len
)
288 u8 mode_reg
= host
->speed
;
289 int state
= state_addr
;
292 /* Setup mode & subaddress if any */
294 case pmac_low_i2c_mode_dumb
:
295 printk(KERN_ERR
"low_i2c: Dumb mode not supported !\n");
297 case pmac_low_i2c_mode_std
:
298 mode_reg
|= KW_I2C_MODE_STANDARD
;
300 case pmac_low_i2c_mode_stdsub
:
301 mode_reg
|= KW_I2C_MODE_STANDARDSUB
;
302 kw_write_reg(reg_subaddr
, subaddr
);
304 case pmac_low_i2c_mode_combined
:
305 mode_reg
|= KW_I2C_MODE_COMBINED
;
306 kw_write_reg(reg_subaddr
, subaddr
);
310 /* Setup channel & clear pending irqs */
311 kw_write_reg(reg_isr
, kw_read_reg(reg_isr
));
312 kw_write_reg(reg_mode
, mode_reg
| (host
->channel
<< 4));
313 kw_write_reg(reg_status
, 0);
315 /* Set up address and r/w bit */
316 kw_write_reg(reg_addr
, addr
);
318 /* Start sending address & disable interrupt*/
319 kw_write_reg(reg_ier
, 0 /*KW_I2C_IRQ_MASK*/);
320 kw_write_reg(reg_control
, KW_I2C_CTL_XADDR
);
322 /* State machine, to turn into an interrupt handler */
323 while(state
!= state_idle
) {
324 u8 isr
= kw_wait_interrupt(host
);
325 state
= kw_handle_interrupt(host
, state
, addr
& 1, &rc
, &data
, &len
, isr
);
331 static void keywest_low_i2c_add(struct device_node
*np
)
333 struct low_i2c_host
*host
= find_low_i2c_host(NULL
);
334 unsigned long *psteps
, *prate
, steps
, aoffset
= 0;
335 struct device_node
*parent
;
338 printk(KERN_ERR
"low_i2c: Can't allocate host for %s\n",
342 memset(host
, 0, sizeof(*host
));
344 init_MUTEX(&host
->mutex
);
345 host
->np
= of_node_get(np
);
346 psteps
= (unsigned long *)get_property(np
, "AAPL,address-step", NULL
);
347 steps
= psteps
? (*psteps
) : 0x10;
348 for (host
->bsteps
= 0; (steps
& 0x01) == 0; host
->bsteps
++)
350 parent
= of_get_parent(np
);
351 host
->num_channels
= 1;
352 if (parent
&& parent
->name
[0] == 'u') {
353 host
->num_channels
= 2;
356 /* Select interface rate */
357 host
->speed
= KW_I2C_MODE_100KHZ
;
358 prate
= (unsigned long *)get_property(np
, "AAPL,i2c-rate", NULL
);
359 if (prate
) switch(*prate
) {
361 host
->speed
= KW_I2C_MODE_100KHZ
;
364 host
->speed
= KW_I2C_MODE_50KHZ
;
367 host
->speed
= KW_I2C_MODE_25KHZ
;
370 host
->mode
= pmac_low_i2c_mode_std
;
371 host
->base
= ioremap(np
->addrs
[0].address
+ aoffset
,
373 host
->func
= keywest_low_i2c_func
;
383 #ifdef CONFIG_ADB_PMU
385 static int pmu_low_i2c_func(struct low_i2c_host
*host
, u8 addr
, u8 sub
, u8
*data
, int len
)
391 static void pmu_low_i2c_add(struct device_node
*np
)
393 struct low_i2c_host
*host
= find_low_i2c_host(NULL
);
396 printk(KERN_ERR
"low_i2c: Can't allocate host for %s\n",
400 memset(host
, 0, sizeof(*host
));
402 init_MUTEX(&host
->mutex
);
403 host
->np
= of_node_get(np
);
404 host
->num_channels
= 3;
405 host
->mode
= pmac_low_i2c_mode_std
;
406 host
->func
= pmu_low_i2c_func
;
409 #endif /* CONFIG_ADB_PMU */
411 void __init
pmac_init_low_i2c(void)
413 struct device_node
*np
;
415 /* Probe keywest-i2c busses */
416 np
= of_find_compatible_node(NULL
, "i2c", "keywest-i2c");
418 keywest_low_i2c_add(np
);
419 np
= of_find_compatible_node(np
, "i2c", "keywest-i2c");
422 #ifdef CONFIG_ADB_PMU
423 /* Probe PMU busses */
424 np
= of_find_node_by_name(NULL
, "via-pmu");
427 #endif /* CONFIG_ADB_PMU */
429 /* TODO: Add CUDA support as well */
432 int pmac_low_i2c_lock(struct device_node
*np
)
434 struct low_i2c_host
*host
= find_low_i2c_host(np
);
441 EXPORT_SYMBOL(pmac_low_i2c_lock
);
443 int pmac_low_i2c_unlock(struct device_node
*np
)
445 struct low_i2c_host
*host
= find_low_i2c_host(np
);
452 EXPORT_SYMBOL(pmac_low_i2c_unlock
);
455 int pmac_low_i2c_open(struct device_node
*np
, int channel
)
457 struct low_i2c_host
*host
= find_low_i2c_host(np
);
462 if (channel
>= host
->num_channels
)
467 host
->channel
= channel
;
471 EXPORT_SYMBOL(pmac_low_i2c_open
);
473 int pmac_low_i2c_close(struct device_node
*np
)
475 struct low_i2c_host
*host
= find_low_i2c_host(np
);
485 EXPORT_SYMBOL(pmac_low_i2c_close
);
487 int pmac_low_i2c_setmode(struct device_node
*np
, int mode
)
489 struct low_i2c_host
*host
= find_low_i2c_host(np
);
493 WARN_ON(!host
->is_open
);
498 EXPORT_SYMBOL(pmac_low_i2c_setmode
);
500 int pmac_low_i2c_xfer(struct device_node
*np
, u8 addrdir
, u8 subaddr
, u8
*data
, int len
)
502 struct low_i2c_host
*host
= find_low_i2c_host(np
);
506 WARN_ON(!host
->is_open
);
508 return host
->func(host
, addrdir
, subaddr
, data
, len
);
510 EXPORT_SYMBOL(pmac_low_i2c_xfer
);