blob: ac8ef2ce07fb6ffd4dc5a4672a4f109a4a402554 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $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.
4 *
5 * Copyright (C) 2001 David S. Miller (davem@redhat.com)
6 */
7
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>
David S. Miller4b5dff72007-05-13 22:22:13 -070021#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include "bbc_i2c.h"
24
25/* Convert this driver to use i2c bus layer someday... */
26#define I2C_PCF_PIN 0x80
27#define I2C_PCF_ESO 0x40
28#define I2C_PCF_ES1 0x20
29#define I2C_PCF_ES2 0x10
30#define I2C_PCF_ENI 0x08
31#define I2C_PCF_STA 0x04
32#define I2C_PCF_STO 0x02
33#define I2C_PCF_ACK 0x01
34
35#define I2C_PCF_START (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ENI | I2C_PCF_STA | I2C_PCF_ACK)
36#define I2C_PCF_STOP (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_STO | I2C_PCF_ACK)
37#define I2C_PCF_REPSTART ( I2C_PCF_ESO | I2C_PCF_STA | I2C_PCF_ACK)
38#define I2C_PCF_IDLE (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ACK)
39
40#define I2C_PCF_INI 0x40 /* 1 if not initialized */
41#define I2C_PCF_STS 0x20
42#define I2C_PCF_BER 0x10
43#define I2C_PCF_AD0 0x08
44#define I2C_PCF_LRB 0x08
45#define I2C_PCF_AAS 0x04
46#define I2C_PCF_LAB 0x02
47#define I2C_PCF_BB 0x01
48
49/* The BBC devices have two I2C controllers. The first I2C controller
50 * connects mainly to configuration proms (NVRAM, cpu configuration,
51 * dimm types, etc.). Whereas the second I2C controller connects to
52 * environmental control devices such as fans and temperature sensors.
53 * The second controller also connects to the smartcard reader, if present.
54 */
55
56#define NUM_CHILDREN 8
57struct bbc_i2c_bus {
58 struct bbc_i2c_bus *next;
59 int index;
60 spinlock_t lock;
61 void __iomem *i2c_bussel_reg;
62 void __iomem *i2c_control_regs;
63 unsigned char own, clock;
64
65 wait_queue_head_t wq;
66 volatile int waiting;
67
68 struct linux_ebus_device *bus_edev;
69 struct {
70 struct linux_ebus_child *device;
71 int client_claimed;
72 } devs[NUM_CHILDREN];
73};
74
75static struct bbc_i2c_bus *all_bbc_i2c;
76
77struct bbc_i2c_client {
78 struct bbc_i2c_bus *bp;
79 struct linux_ebus_child *echild;
80 int bus;
81 int address;
82};
83
84static int find_device(struct bbc_i2c_bus *bp, struct linux_ebus_child *echild)
85{
86 int i;
87
88 for (i = 0; i < NUM_CHILDREN; i++) {
89 if (bp->devs[i].device == echild) {
90 if (bp->devs[i].client_claimed)
91 return 0;
92 return 1;
93 }
94 }
95 return 0;
96}
97
98static void set_device_claimage(struct bbc_i2c_bus *bp, struct linux_ebus_child *echild, int val)
99{
100 int i;
101
102 for (i = 0; i < NUM_CHILDREN; i++) {
103 if (bp->devs[i].device == echild) {
104 bp->devs[i].client_claimed = val;
105 return;
106 }
107 }
108}
109
110#define claim_device(BP,ECHILD) set_device_claimage(BP,ECHILD,1)
111#define release_device(BP,ECHILD) set_device_claimage(BP,ECHILD,0)
112
113static struct bbc_i2c_bus *find_bus_for_device(struct linux_ebus_child *echild)
114{
115 struct bbc_i2c_bus *bp = all_bbc_i2c;
116
117 while (bp != NULL) {
118 if (find_device(bp, echild) != 0)
119 break;
120 bp = bp->next;
121 }
122
123 return bp;
124}
125
126struct linux_ebus_child *bbc_i2c_getdev(int index)
127{
128 struct bbc_i2c_bus *bp = all_bbc_i2c;
129 struct linux_ebus_child *echild = NULL;
130 int curidx = 0;
131
132 while (bp != NULL) {
133 struct bbc_i2c_bus *next = bp->next;
134 int i;
135
136 for (i = 0; i < NUM_CHILDREN; i++) {
137 if (!(echild = bp->devs[i].device))
138 break;
139 if (curidx == index)
140 goto out;
141 echild = NULL;
142 curidx++;
143 }
144 bp = next;
145 }
146out:
147 if (curidx == index)
148 return echild;
149 return NULL;
150}
151
152struct bbc_i2c_client *bbc_i2c_attach(struct linux_ebus_child *echild)
153{
154 struct bbc_i2c_bus *bp = find_bus_for_device(echild);
155 struct bbc_i2c_client *client;
156
157 if (!bp)
158 return NULL;
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700159 client = kzalloc(sizeof(*client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (!client)
161 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 client->bp = bp;
163 client->echild = echild;
164 client->bus = echild->resource[0].start;
165 client->address = echild->resource[1].start;
166
167 claim_device(bp, echild);
168
169 return client;
170}
171
172void bbc_i2c_detach(struct bbc_i2c_client *client)
173{
174 struct bbc_i2c_bus *bp = client->bp;
175 struct linux_ebus_child *echild = client->echild;
176
177 release_device(bp, echild);
178 kfree(client);
179}
180
181static int wait_for_pin(struct bbc_i2c_bus *bp, u8 *status)
182{
183 DECLARE_WAITQUEUE(wait, current);
184 int limit = 32;
185 int ret = 1;
186
187 bp->waiting = 1;
188 add_wait_queue(&bp->wq, &wait);
189 while (limit-- > 0) {
David S. Miller3b36fb82007-02-26 10:11:35 -0800190 unsigned long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
David S. Miller3b36fb82007-02-26 10:11:35 -0800192 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 ret = 0;
199 break;
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 remove_wait_queue(&bp->wq, &wait);
203 bp->waiting = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 return ret;
206}
207
208int bbc_i2c_writeb(struct bbc_i2c_client *client, unsigned char val, int off)
209{
210 struct bbc_i2c_bus *bp = client->bp;
211 int address = client->address;
212 u8 status;
213 int ret = -1;
214
215 if (bp->i2c_bussel_reg != NULL)
216 writeb(client->bus, bp->i2c_bussel_reg);
217
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;
222
223 writeb(off, bp->i2c_control_regs + 0x1);
224 if (wait_for_pin(bp, &status) ||
225 (status & I2C_PCF_LRB) != 0)
226 goto out;
227
228 writeb(val, bp->i2c_control_regs + 0x1);
229 if (wait_for_pin(bp, &status))
230 goto out;
231
232 ret = 0;
233
234out:
235 writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
236 return ret;
237}
238
239int bbc_i2c_readb(struct bbc_i2c_client *client, unsigned char *byte, int off)
240{
241 struct bbc_i2c_bus *bp = client->bp;
242 unsigned char address = client->address, status;
243 int ret = -1;
244
245 if (bp->i2c_bussel_reg != NULL)
246 writeb(client->bus, bp->i2c_bussel_reg);
247
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;
252
253 writeb(off, bp->i2c_control_regs + 0x1);
254 if (wait_for_pin(bp, &status) ||
255 (status & I2C_PCF_LRB) != 0)
256 goto out;
257
258 writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
259
260 address |= 0x1; /* READ */
261
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;
266
267 /* Set PIN back to one so the device sends the first
268 * byte.
269 */
270 (void) readb(bp->i2c_control_regs + 0x1);
271 if (wait_for_pin(bp, &status))
272 goto out;
273
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;
278
279 ret = 0;
280
281out:
282 writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
283 (void) readb(bp->i2c_control_regs + 0x1);
284
285 return ret;
286}
287
288int bbc_i2c_write_buf(struct bbc_i2c_client *client,
289 char *buf, int len, int off)
290{
291 int ret = 0;
292
293 while (len > 0) {
294 int err = bbc_i2c_writeb(client, *buf, off);
295
296 if (err < 0) {
297 ret = err;
298 break;
299 }
300
301 len--;
302 buf++;
303 off++;
304 }
305 return ret;
306}
307
308int bbc_i2c_read_buf(struct bbc_i2c_client *client,
309 char *buf, int len, int off)
310{
311 int ret = 0;
312
313 while (len > 0) {
314 int err = bbc_i2c_readb(client, buf, off);
315 if (err < 0) {
316 ret = err;
317 break;
318 }
319 len--;
320 buf++;
321 off++;
322 }
323
324 return ret;
325}
326
327EXPORT_SYMBOL(bbc_i2c_getdev);
328EXPORT_SYMBOL(bbc_i2c_attach);
329EXPORT_SYMBOL(bbc_i2c_detach);
330EXPORT_SYMBOL(bbc_i2c_writeb);
331EXPORT_SYMBOL(bbc_i2c_readb);
332EXPORT_SYMBOL(bbc_i2c_write_buf);
333EXPORT_SYMBOL(bbc_i2c_read_buf);
334
David Howells7d12e782006-10-05 14:55:46 +0100335static irqreturn_t bbc_i2c_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct bbc_i2c_bus *bp = dev_id;
338
339 /* PIN going from set to clear is the only event which
340 * makes the i2c assert an interrupt.
341 */
342 if (bp->waiting &&
343 !(readb(bp->i2c_control_regs + 0x0) & I2C_PCF_PIN))
David S. Miller3b36fb82007-02-26 10:11:35 -0800344 wake_up_interruptible(&bp->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 return IRQ_HANDLED;
347}
348
349static void __init reset_one_i2c(struct bbc_i2c_bus *bp)
350{
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);
356}
357
358static int __init attach_one_i2c(struct linux_ebus_device *edev, int index)
359{
Mariusz Kozlowski50aa4852007-07-31 14:04:57 -0700360 struct bbc_i2c_bus *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct linux_ebus_child *echild;
362 int entry;
363
Mariusz Kozlowski50aa4852007-07-31 14:04:57 -0700364 bp = kzalloc(sizeof(*bp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (!bp)
366 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 bp->i2c_control_regs = ioremap(edev->resource[0].start, 0x2);
369 if (!bp->i2c_control_regs)
370 goto fail;
371
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;
376 }
377
378 bp->waiting = 0;
379 init_waitqueue_head(&bp->wq);
380 if (request_irq(edev->irqs[0], bbc_i2c_interrupt,
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700381 IRQF_SHARED, "bbc_i2c", bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 goto fail;
383
384 bp->index = index;
385 bp->bus_edev = edev;
386
387 spin_lock_init(&bp->lock);
388 bp->next = all_bbc_i2c;
389 all_bbc_i2c = bp;
390
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;
397 }
398
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);
403
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);
406
407 reset_one_i2c(bp);
408
409 return 0;
410
411fail:
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;
418}
419
420static int __init bbc_present(void)
421{
422 struct linux_ebus *ebus = NULL;
423 struct linux_ebus_device *edev = NULL;
424
425 for_each_ebus(ebus) {
426 for_each_ebusdev(edev, ebus) {
David S. Miller690c8fd2006-06-22 19:12:03 -0700427 if (!strcmp(edev->prom_node->name, "bbc"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return 1;
429 }
430 }
431 return 0;
432}
433
434extern int bbc_envctrl_init(void);
435extern void bbc_envctrl_cleanup(void);
436static void bbc_i2c_cleanup(void);
437
438static int __init bbc_i2c_init(void)
439{
440 struct linux_ebus *ebus = NULL;
441 struct linux_ebus_device *edev = NULL;
442 int err, index = 0;
443
David S. Millerb5e7ae52006-03-17 13:23:56 -0800444 if ((tlb_type != cheetah && tlb_type != cheetah_plus) ||
445 !bbc_present())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return -ENODEV;
447
448 for_each_ebus(ebus) {
449 for_each_ebusdev(edev, ebus) {
David S. Miller690c8fd2006-06-22 19:12:03 -0700450 if (!strcmp(edev->prom_node->name, "i2c")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (!attach_one_i2c(edev, index))
452 index++;
453 }
454 }
455 }
456
457 if (!index)
458 return -ENODEV;
459
460 err = bbc_envctrl_init();
461 if (err)
462 bbc_i2c_cleanup();
463 return err;
464}
465
466static void bbc_i2c_cleanup(void)
467{
468 struct bbc_i2c_bus *bp = all_bbc_i2c;
469
470 bbc_envctrl_cleanup();
471
472 while (bp != NULL) {
473 struct bbc_i2c_bus *next = bp->next;
474
475 free_irq(bp->bus_edev->irqs[0], bp);
476
477 if (bp->i2c_bussel_reg)
478 iounmap(bp->i2c_bussel_reg);
479 if (bp->i2c_control_regs)
480 iounmap(bp->i2c_control_regs);
481
482 kfree(bp);
483
484 bp = next;
485 }
486 all_bbc_i2c = NULL;
487}
488
489module_init(bbc_i2c_init);
490module_exit(bbc_i2c_cleanup);
David S. Millerb5e7ae52006-03-17 13:23:56 -0800491MODULE_LICENSE("GPL");