1 // SPDX-License-Identifier: GPL-2.0-only
2 /* bbc_i2c.c: I2C low-level driver for BBC device on UltraSPARC-III
5 * Copyright (C) 2001, 2008 David S. Miller (davem@davemloft.net)
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/interrupt.h>
17 #include <linux/of_device.h>
23 /* Convert this driver to use i2c bus layer someday... */
24 #define I2C_PCF_PIN 0x80
25 #define I2C_PCF_ESO 0x40
26 #define I2C_PCF_ES1 0x20
27 #define I2C_PCF_ES2 0x10
28 #define I2C_PCF_ENI 0x08
29 #define I2C_PCF_STA 0x04
30 #define I2C_PCF_STO 0x02
31 #define I2C_PCF_ACK 0x01
33 #define I2C_PCF_START (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ENI | I2C_PCF_STA | I2C_PCF_ACK)
34 #define I2C_PCF_STOP (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_STO | I2C_PCF_ACK)
35 #define I2C_PCF_REPSTART ( I2C_PCF_ESO | I2C_PCF_STA | I2C_PCF_ACK)
36 #define I2C_PCF_IDLE (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ACK)
38 #define I2C_PCF_INI 0x40 /* 1 if not initialized */
39 #define I2C_PCF_STS 0x20
40 #define I2C_PCF_BER 0x10
41 #define I2C_PCF_AD0 0x08
42 #define I2C_PCF_LRB 0x08
43 #define I2C_PCF_AAS 0x04
44 #define I2C_PCF_LAB 0x02
45 #define I2C_PCF_BB 0x01
47 /* The BBC devices have two I2C controllers. The first I2C controller
48 * connects mainly to configuration proms (NVRAM, cpu configuration,
49 * dimm types, etc.). Whereas the second I2C controller connects to
50 * environmental control devices such as fans and temperature sensors.
51 * The second controller also connects to the smartcard reader, if present.
54 static void set_device_claimage(struct bbc_i2c_bus
*bp
, struct platform_device
*op
, int val
)
58 for (i
= 0; i
< NUM_CHILDREN
; i
++) {
59 if (bp
->devs
[i
].device
== op
) {
60 bp
->devs
[i
].client_claimed
= val
;
66 #define claim_device(BP,ECHILD) set_device_claimage(BP,ECHILD,1)
67 #define release_device(BP,ECHILD) set_device_claimage(BP,ECHILD,0)
69 struct platform_device
*bbc_i2c_getdev(struct bbc_i2c_bus
*bp
, int index
)
71 struct platform_device
*op
= NULL
;
74 for (i
= 0; i
< NUM_CHILDREN
; i
++) {
75 if (!(op
= bp
->devs
[i
].device
))
89 struct bbc_i2c_client
*bbc_i2c_attach(struct bbc_i2c_bus
*bp
, struct platform_device
*op
)
91 struct bbc_i2c_client
*client
;
94 client
= kzalloc(sizeof(*client
), GFP_KERNEL
);
100 reg
= of_get_property(op
->dev
.of_node
, "reg", NULL
);
106 client
->bus
= reg
[0];
107 client
->address
= reg
[1];
109 claim_device(bp
, op
);
114 void bbc_i2c_detach(struct bbc_i2c_client
*client
)
116 struct bbc_i2c_bus
*bp
= client
->bp
;
117 struct platform_device
*op
= client
->op
;
119 release_device(bp
, op
);
123 static int wait_for_pin(struct bbc_i2c_bus
*bp
, u8
*status
)
125 DECLARE_WAITQUEUE(wait
, current
);
130 add_wait_queue(&bp
->wq
, &wait
);
131 while (limit
-- > 0) {
134 val
= wait_event_interruptible_timeout(
136 (((*status
= readb(bp
->i2c_control_regs
+ 0))
137 & I2C_PCF_PIN
) == 0),
138 msecs_to_jiffies(250));
144 remove_wait_queue(&bp
->wq
, &wait
);
150 int bbc_i2c_writeb(struct bbc_i2c_client
*client
, unsigned char val
, int off
)
152 struct bbc_i2c_bus
*bp
= client
->bp
;
153 int address
= client
->address
;
157 if (bp
->i2c_bussel_reg
!= NULL
)
158 writeb(client
->bus
, bp
->i2c_bussel_reg
);
160 writeb(address
, bp
->i2c_control_regs
+ 0x1);
161 writeb(I2C_PCF_START
, bp
->i2c_control_regs
+ 0x0);
162 if (wait_for_pin(bp
, &status
))
165 writeb(off
, bp
->i2c_control_regs
+ 0x1);
166 if (wait_for_pin(bp
, &status
) ||
167 (status
& I2C_PCF_LRB
) != 0)
170 writeb(val
, bp
->i2c_control_regs
+ 0x1);
171 if (wait_for_pin(bp
, &status
))
177 writeb(I2C_PCF_STOP
, bp
->i2c_control_regs
+ 0x0);
181 int bbc_i2c_readb(struct bbc_i2c_client
*client
, unsigned char *byte
, int off
)
183 struct bbc_i2c_bus
*bp
= client
->bp
;
184 unsigned char address
= client
->address
, status
;
187 if (bp
->i2c_bussel_reg
!= NULL
)
188 writeb(client
->bus
, bp
->i2c_bussel_reg
);
190 writeb(address
, bp
->i2c_control_regs
+ 0x1);
191 writeb(I2C_PCF_START
, bp
->i2c_control_regs
+ 0x0);
192 if (wait_for_pin(bp
, &status
))
195 writeb(off
, bp
->i2c_control_regs
+ 0x1);
196 if (wait_for_pin(bp
, &status
) ||
197 (status
& I2C_PCF_LRB
) != 0)
200 writeb(I2C_PCF_STOP
, bp
->i2c_control_regs
+ 0x0);
202 address
|= 0x1; /* READ */
204 writeb(address
, bp
->i2c_control_regs
+ 0x1);
205 writeb(I2C_PCF_START
, bp
->i2c_control_regs
+ 0x0);
206 if (wait_for_pin(bp
, &status
))
209 /* Set PIN back to one so the device sends the first
212 (void) readb(bp
->i2c_control_regs
+ 0x1);
213 if (wait_for_pin(bp
, &status
))
216 writeb(I2C_PCF_ESO
| I2C_PCF_ENI
, bp
->i2c_control_regs
+ 0x0);
217 *byte
= readb(bp
->i2c_control_regs
+ 0x1);
218 if (wait_for_pin(bp
, &status
))
224 writeb(I2C_PCF_STOP
, bp
->i2c_control_regs
+ 0x0);
225 (void) readb(bp
->i2c_control_regs
+ 0x1);
230 int bbc_i2c_write_buf(struct bbc_i2c_client
*client
,
231 char *buf
, int len
, int off
)
236 ret
= bbc_i2c_writeb(client
, *buf
, off
);
246 int bbc_i2c_read_buf(struct bbc_i2c_client
*client
,
247 char *buf
, int len
, int off
)
252 ret
= bbc_i2c_readb(client
, buf
, off
);
263 EXPORT_SYMBOL(bbc_i2c_getdev
);
264 EXPORT_SYMBOL(bbc_i2c_attach
);
265 EXPORT_SYMBOL(bbc_i2c_detach
);
266 EXPORT_SYMBOL(bbc_i2c_writeb
);
267 EXPORT_SYMBOL(bbc_i2c_readb
);
268 EXPORT_SYMBOL(bbc_i2c_write_buf
);
269 EXPORT_SYMBOL(bbc_i2c_read_buf
);
271 static irqreturn_t
bbc_i2c_interrupt(int irq
, void *dev_id
)
273 struct bbc_i2c_bus
*bp
= dev_id
;
275 /* PIN going from set to clear is the only event which
276 * makes the i2c assert an interrupt.
279 !(readb(bp
->i2c_control_regs
+ 0x0) & I2C_PCF_PIN
))
280 wake_up_interruptible(&bp
->wq
);
285 static void reset_one_i2c(struct bbc_i2c_bus
*bp
)
287 writeb(I2C_PCF_PIN
, bp
->i2c_control_regs
+ 0x0);
288 writeb(bp
->own
, bp
->i2c_control_regs
+ 0x1);
289 writeb(I2C_PCF_PIN
| I2C_PCF_ES1
, bp
->i2c_control_regs
+ 0x0);
290 writeb(bp
->clock
, bp
->i2c_control_regs
+ 0x1);
291 writeb(I2C_PCF_IDLE
, bp
->i2c_control_regs
+ 0x0);
294 static struct bbc_i2c_bus
* attach_one_i2c(struct platform_device
*op
, int index
)
296 struct bbc_i2c_bus
*bp
;
297 struct device_node
*dp
;
300 bp
= kzalloc(sizeof(*bp
), GFP_KERNEL
);
304 INIT_LIST_HEAD(&bp
->temps
);
305 INIT_LIST_HEAD(&bp
->fans
);
307 bp
->i2c_control_regs
= of_ioremap(&op
->resource
[0], 0, 0x2, "bbc_i2c_regs");
308 if (!bp
->i2c_control_regs
)
311 if (op
->num_resources
== 2) {
312 bp
->i2c_bussel_reg
= of_ioremap(&op
->resource
[1], 0, 0x1, "bbc_i2c_bussel");
313 if (!bp
->i2c_bussel_reg
)
318 init_waitqueue_head(&bp
->wq
);
319 if (request_irq(op
->archdata
.irqs
[0], bbc_i2c_interrupt
,
320 IRQF_SHARED
, "bbc_i2c", bp
))
326 spin_lock_init(&bp
->lock
);
329 for (dp
= op
->dev
.of_node
->child
;
331 dp
= dp
->sibling
, entry
++) {
332 struct platform_device
*child_op
;
334 child_op
= of_find_device_by_node(dp
);
335 bp
->devs
[entry
].device
= child_op
;
336 bp
->devs
[entry
].client_claimed
= 0;
339 writeb(I2C_PCF_PIN
, bp
->i2c_control_regs
+ 0x0);
340 bp
->own
= readb(bp
->i2c_control_regs
+ 0x01);
341 writeb(I2C_PCF_PIN
| I2C_PCF_ES1
, bp
->i2c_control_regs
+ 0x0);
342 bp
->clock
= readb(bp
->i2c_control_regs
+ 0x01);
344 printk(KERN_INFO
"i2c-%d: Regs at %p, %d devices, own %02x, clock %02x.\n",
345 bp
->index
, bp
->i2c_control_regs
, entry
, bp
->own
, bp
->clock
);
352 if (bp
->i2c_bussel_reg
)
353 of_iounmap(&op
->resource
[1], bp
->i2c_bussel_reg
, 1);
354 if (bp
->i2c_control_regs
)
355 of_iounmap(&op
->resource
[0], bp
->i2c_control_regs
, 2);
360 extern int bbc_envctrl_init(struct bbc_i2c_bus
*bp
);
361 extern void bbc_envctrl_cleanup(struct bbc_i2c_bus
*bp
);
363 static int bbc_i2c_probe(struct platform_device
*op
)
365 struct bbc_i2c_bus
*bp
;
368 bp
= attach_one_i2c(op
, index
);
372 err
= bbc_envctrl_init(bp
);
374 free_irq(op
->archdata
.irqs
[0], bp
);
375 if (bp
->i2c_bussel_reg
)
376 of_iounmap(&op
->resource
[0], bp
->i2c_bussel_reg
, 1);
377 if (bp
->i2c_control_regs
)
378 of_iounmap(&op
->resource
[1], bp
->i2c_control_regs
, 2);
381 dev_set_drvdata(&op
->dev
, bp
);
387 static int bbc_i2c_remove(struct platform_device
*op
)
389 struct bbc_i2c_bus
*bp
= dev_get_drvdata(&op
->dev
);
391 bbc_envctrl_cleanup(bp
);
393 free_irq(op
->archdata
.irqs
[0], bp
);
395 if (bp
->i2c_bussel_reg
)
396 of_iounmap(&op
->resource
[0], bp
->i2c_bussel_reg
, 1);
397 if (bp
->i2c_control_regs
)
398 of_iounmap(&op
->resource
[1], bp
->i2c_control_regs
, 2);
405 static const struct of_device_id bbc_i2c_match
[] = {
408 .compatible
= "SUNW,bbc-i2c",
412 MODULE_DEVICE_TABLE(of
, bbc_i2c_match
);
414 static struct platform_driver bbc_i2c_driver
= {
417 .of_match_table
= bbc_i2c_match
,
419 .probe
= bbc_i2c_probe
,
420 .remove
= bbc_i2c_remove
,
423 module_platform_driver(bbc_i2c_driver
);
425 MODULE_LICENSE("GPL");