Linux 2.6.20.7
[linux/fpc-iii.git] / drivers / sbus / char / bbc_i2c.c
blob8410587348f1dcc5084cafc0df55937c0279e193
1 /* $Id: bbc_i2c.c,v 1.2 2001/04/02 09:59:08 davem Exp $
2 * bbc_i2c.c: I2C low-level driver for BBC device on UltraSPARC-III
3 * platforms.
5 * Copyright (C) 2001 David S. Miller (davem@redhat.com)
6 */
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11 #include <linux/slab.h>
12 #include <linux/sched.h>
13 #include <linux/wait.h>
14 #include <linux/delay.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <asm/oplib.h>
18 #include <asm/ebus.h>
19 #include <asm/spitfire.h>
20 #include <asm/bbc.h>
22 #include "bbc_i2c.h"
24 /* Convert this driver to use i2c bus layer someday... */
25 #define I2C_PCF_PIN 0x80
26 #define I2C_PCF_ESO 0x40
27 #define I2C_PCF_ES1 0x20
28 #define I2C_PCF_ES2 0x10
29 #define I2C_PCF_ENI 0x08
30 #define I2C_PCF_STA 0x04
31 #define I2C_PCF_STO 0x02
32 #define I2C_PCF_ACK 0x01
34 #define I2C_PCF_START (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ENI | I2C_PCF_STA | I2C_PCF_ACK)
35 #define I2C_PCF_STOP (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_STO | I2C_PCF_ACK)
36 #define I2C_PCF_REPSTART ( I2C_PCF_ESO | I2C_PCF_STA | I2C_PCF_ACK)
37 #define I2C_PCF_IDLE (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ACK)
39 #define I2C_PCF_INI 0x40 /* 1 if not initialized */
40 #define I2C_PCF_STS 0x20
41 #define I2C_PCF_BER 0x10
42 #define I2C_PCF_AD0 0x08
43 #define I2C_PCF_LRB 0x08
44 #define I2C_PCF_AAS 0x04
45 #define I2C_PCF_LAB 0x02
46 #define I2C_PCF_BB 0x01
48 /* The BBC devices have two I2C controllers. The first I2C controller
49 * connects mainly to configuration proms (NVRAM, cpu configuration,
50 * dimm types, etc.). Whereas the second I2C controller connects to
51 * environmental control devices such as fans and temperature sensors.
52 * The second controller also connects to the smartcard reader, if present.
55 #define NUM_CHILDREN 8
56 struct bbc_i2c_bus {
57 struct bbc_i2c_bus *next;
58 int index;
59 spinlock_t lock;
60 void __iomem *i2c_bussel_reg;
61 void __iomem *i2c_control_regs;
62 unsigned char own, clock;
64 wait_queue_head_t wq;
65 volatile int waiting;
67 struct linux_ebus_device *bus_edev;
68 struct {
69 struct linux_ebus_child *device;
70 int client_claimed;
71 } devs[NUM_CHILDREN];
74 static struct bbc_i2c_bus *all_bbc_i2c;
76 struct bbc_i2c_client {
77 struct bbc_i2c_bus *bp;
78 struct linux_ebus_child *echild;
79 int bus;
80 int address;
83 static int find_device(struct bbc_i2c_bus *bp, struct linux_ebus_child *echild)
85 int i;
87 for (i = 0; i < NUM_CHILDREN; i++) {
88 if (bp->devs[i].device == echild) {
89 if (bp->devs[i].client_claimed)
90 return 0;
91 return 1;
94 return 0;
97 static void set_device_claimage(struct bbc_i2c_bus *bp, struct linux_ebus_child *echild, int val)
99 int i;
101 for (i = 0; i < NUM_CHILDREN; i++) {
102 if (bp->devs[i].device == echild) {
103 bp->devs[i].client_claimed = val;
104 return;
109 #define claim_device(BP,ECHILD) set_device_claimage(BP,ECHILD,1)
110 #define release_device(BP,ECHILD) set_device_claimage(BP,ECHILD,0)
112 static struct bbc_i2c_bus *find_bus_for_device(struct linux_ebus_child *echild)
114 struct bbc_i2c_bus *bp = all_bbc_i2c;
116 while (bp != NULL) {
117 if (find_device(bp, echild) != 0)
118 break;
119 bp = bp->next;
122 return bp;
125 struct linux_ebus_child *bbc_i2c_getdev(int index)
127 struct bbc_i2c_bus *bp = all_bbc_i2c;
128 struct linux_ebus_child *echild = NULL;
129 int curidx = 0;
131 while (bp != NULL) {
132 struct bbc_i2c_bus *next = bp->next;
133 int i;
135 for (i = 0; i < NUM_CHILDREN; i++) {
136 if (!(echild = bp->devs[i].device))
137 break;
138 if (curidx == index)
139 goto out;
140 echild = NULL;
141 curidx++;
143 bp = next;
145 out:
146 if (curidx == index)
147 return echild;
148 return NULL;
151 struct bbc_i2c_client *bbc_i2c_attach(struct linux_ebus_child *echild)
153 struct bbc_i2c_bus *bp = find_bus_for_device(echild);
154 struct bbc_i2c_client *client;
156 if (!bp)
157 return NULL;
158 client = kmalloc(sizeof(*client), GFP_KERNEL);
159 if (!client)
160 return NULL;
161 memset(client, 0, sizeof(*client));
162 client->bp = bp;
163 client->echild = echild;
164 client->bus = echild->resource[0].start;
165 client->address = echild->resource[1].start;
167 claim_device(bp, echild);
169 return client;
172 void bbc_i2c_detach(struct bbc_i2c_client *client)
174 struct bbc_i2c_bus *bp = client->bp;
175 struct linux_ebus_child *echild = client->echild;
177 release_device(bp, echild);
178 kfree(client);
181 static int wait_for_pin(struct bbc_i2c_bus *bp, u8 *status)
183 DECLARE_WAITQUEUE(wait, current);
184 int limit = 32;
185 int ret = 1;
187 bp->waiting = 1;
188 add_wait_queue(&bp->wq, &wait);
189 while (limit-- > 0) {
190 unsigned long val;
192 val = wait_event_interruptible_timeout(
193 bp->wq,
194 (((*status = readb(bp->i2c_control_regs + 0))
195 & I2C_PCF_PIN) == 0),
196 msecs_to_jiffies(250));
197 if (val > 0) {
198 ret = 0;
199 break;
202 remove_wait_queue(&bp->wq, &wait);
203 bp->waiting = 0;
205 return ret;
208 int bbc_i2c_writeb(struct bbc_i2c_client *client, unsigned char val, int off)
210 struct bbc_i2c_bus *bp = client->bp;
211 int address = client->address;
212 u8 status;
213 int ret = -1;
215 if (bp->i2c_bussel_reg != NULL)
216 writeb(client->bus, bp->i2c_bussel_reg);
218 writeb(address, bp->i2c_control_regs + 0x1);
219 writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
220 if (wait_for_pin(bp, &status))
221 goto out;
223 writeb(off, bp->i2c_control_regs + 0x1);
224 if (wait_for_pin(bp, &status) ||
225 (status & I2C_PCF_LRB) != 0)
226 goto out;
228 writeb(val, bp->i2c_control_regs + 0x1);
229 if (wait_for_pin(bp, &status))
230 goto out;
232 ret = 0;
234 out:
235 writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
236 return ret;
239 int bbc_i2c_readb(struct bbc_i2c_client *client, unsigned char *byte, int off)
241 struct bbc_i2c_bus *bp = client->bp;
242 unsigned char address = client->address, status;
243 int ret = -1;
245 if (bp->i2c_bussel_reg != NULL)
246 writeb(client->bus, bp->i2c_bussel_reg);
248 writeb(address, bp->i2c_control_regs + 0x1);
249 writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
250 if (wait_for_pin(bp, &status))
251 goto out;
253 writeb(off, bp->i2c_control_regs + 0x1);
254 if (wait_for_pin(bp, &status) ||
255 (status & I2C_PCF_LRB) != 0)
256 goto out;
258 writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
260 address |= 0x1; /* READ */
262 writeb(address, bp->i2c_control_regs + 0x1);
263 writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
264 if (wait_for_pin(bp, &status))
265 goto out;
267 /* Set PIN back to one so the device sends the first
268 * byte.
270 (void) readb(bp->i2c_control_regs + 0x1);
271 if (wait_for_pin(bp, &status))
272 goto out;
274 writeb(I2C_PCF_ESO | I2C_PCF_ENI, bp->i2c_control_regs + 0x0);
275 *byte = readb(bp->i2c_control_regs + 0x1);
276 if (wait_for_pin(bp, &status))
277 goto out;
279 ret = 0;
281 out:
282 writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
283 (void) readb(bp->i2c_control_regs + 0x1);
285 return ret;
288 int bbc_i2c_write_buf(struct bbc_i2c_client *client,
289 char *buf, int len, int off)
291 int ret = 0;
293 while (len > 0) {
294 int err = bbc_i2c_writeb(client, *buf, off);
296 if (err < 0) {
297 ret = err;
298 break;
301 len--;
302 buf++;
303 off++;
305 return ret;
308 int bbc_i2c_read_buf(struct bbc_i2c_client *client,
309 char *buf, int len, int off)
311 int ret = 0;
313 while (len > 0) {
314 int err = bbc_i2c_readb(client, buf, off);
315 if (err < 0) {
316 ret = err;
317 break;
319 len--;
320 buf++;
321 off++;
324 return ret;
327 EXPORT_SYMBOL(bbc_i2c_getdev);
328 EXPORT_SYMBOL(bbc_i2c_attach);
329 EXPORT_SYMBOL(bbc_i2c_detach);
330 EXPORT_SYMBOL(bbc_i2c_writeb);
331 EXPORT_SYMBOL(bbc_i2c_readb);
332 EXPORT_SYMBOL(bbc_i2c_write_buf);
333 EXPORT_SYMBOL(bbc_i2c_read_buf);
335 static irqreturn_t bbc_i2c_interrupt(int irq, void *dev_id)
337 struct bbc_i2c_bus *bp = dev_id;
339 /* PIN going from set to clear is the only event which
340 * makes the i2c assert an interrupt.
342 if (bp->waiting &&
343 !(readb(bp->i2c_control_regs + 0x0) & I2C_PCF_PIN))
344 wake_up_interruptible(&bp->wq);
346 return IRQ_HANDLED;
349 static void __init reset_one_i2c(struct bbc_i2c_bus *bp)
351 writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
352 writeb(bp->own, bp->i2c_control_regs + 0x1);
353 writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
354 writeb(bp->clock, bp->i2c_control_regs + 0x1);
355 writeb(I2C_PCF_IDLE, bp->i2c_control_regs + 0x0);
358 static int __init attach_one_i2c(struct linux_ebus_device *edev, int index)
360 struct bbc_i2c_bus *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
361 struct linux_ebus_child *echild;
362 int entry;
364 if (!bp)
365 return -ENOMEM;
366 memset(bp, 0, sizeof(*bp));
368 bp->i2c_control_regs = ioremap(edev->resource[0].start, 0x2);
369 if (!bp->i2c_control_regs)
370 goto fail;
372 if (edev->num_addrs == 2) {
373 bp->i2c_bussel_reg = ioremap(edev->resource[1].start, 0x1);
374 if (!bp->i2c_bussel_reg)
375 goto fail;
378 bp->waiting = 0;
379 init_waitqueue_head(&bp->wq);
380 if (request_irq(edev->irqs[0], bbc_i2c_interrupt,
381 IRQF_SHARED, "bbc_i2c", bp))
382 goto fail;
384 bp->index = index;
385 bp->bus_edev = edev;
387 spin_lock_init(&bp->lock);
388 bp->next = all_bbc_i2c;
389 all_bbc_i2c = bp;
391 entry = 0;
392 for (echild = edev->children;
393 echild && entry < 8;
394 echild = echild->next, entry++) {
395 bp->devs[entry].device = echild;
396 bp->devs[entry].client_claimed = 0;
399 writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
400 bp->own = readb(bp->i2c_control_regs + 0x01);
401 writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
402 bp->clock = readb(bp->i2c_control_regs + 0x01);
404 printk(KERN_INFO "i2c-%d: Regs at %p, %d devices, own %02x, clock %02x.\n",
405 bp->index, bp->i2c_control_regs, entry, bp->own, bp->clock);
407 reset_one_i2c(bp);
409 return 0;
411 fail:
412 if (bp->i2c_bussel_reg)
413 iounmap(bp->i2c_bussel_reg);
414 if (bp->i2c_control_regs)
415 iounmap(bp->i2c_control_regs);
416 kfree(bp);
417 return -EINVAL;
420 static int __init bbc_present(void)
422 struct linux_ebus *ebus = NULL;
423 struct linux_ebus_device *edev = NULL;
425 for_each_ebus(ebus) {
426 for_each_ebusdev(edev, ebus) {
427 if (!strcmp(edev->prom_node->name, "bbc"))
428 return 1;
431 return 0;
434 extern int bbc_envctrl_init(void);
435 extern void bbc_envctrl_cleanup(void);
436 static void bbc_i2c_cleanup(void);
438 static int __init bbc_i2c_init(void)
440 struct linux_ebus *ebus = NULL;
441 struct linux_ebus_device *edev = NULL;
442 int err, index = 0;
444 if ((tlb_type != cheetah && tlb_type != cheetah_plus) ||
445 !bbc_present())
446 return -ENODEV;
448 for_each_ebus(ebus) {
449 for_each_ebusdev(edev, ebus) {
450 if (!strcmp(edev->prom_node->name, "i2c")) {
451 if (!attach_one_i2c(edev, index))
452 index++;
457 if (!index)
458 return -ENODEV;
460 err = bbc_envctrl_init();
461 if (err)
462 bbc_i2c_cleanup();
463 return err;
466 static void bbc_i2c_cleanup(void)
468 struct bbc_i2c_bus *bp = all_bbc_i2c;
470 bbc_envctrl_cleanup();
472 while (bp != NULL) {
473 struct bbc_i2c_bus *next = bp->next;
475 free_irq(bp->bus_edev->irqs[0], bp);
477 if (bp->i2c_bussel_reg)
478 iounmap(bp->i2c_bussel_reg);
479 if (bp->i2c_control_regs)
480 iounmap(bp->i2c_control_regs);
482 kfree(bp);
484 bp = next;
486 all_bbc_i2c = NULL;
489 module_init(bbc_i2c_init);
490 module_exit(bbc_i2c_cleanup);
491 MODULE_LICENSE("GPL");