ofw_i2c_subr.c revision 1.2 1 /* $NetBSD: ofw_i2c_subr.c,v 1.2 2025/09/16 11:37:17 thorpej Exp $ */
2
3 /*
4 * Copyright 1998
5 * Digital Equipment Corporation. All rights reserved.
6 *
7 * This software is furnished under license and may be used and
8 * copied only in accordance with the following terms and conditions.
9 * Subject to these conditions, you may download, copy, install,
10 * use, modify and distribute this software in source and/or binary
11 * form. No title or ownership is transferred hereby.
12 *
13 * 1) Any source code used, modified or distributed must reproduce
14 * and retain this copyright notice and list of conditions as
15 * they appear in the source file.
16 *
17 * 2) No right is granted to use any trade name, trademark, or logo of
18 * Digital Equipment Corporation. Neither the "Digital Equipment
19 * Corporation" name nor any trademark or logo of Digital Equipment
20 * Corporation may be used to endorse or promote products derived
21 * from this software without the prior written permission of
22 * Digital Equipment Corporation.
23 *
24 * 3) This software is provided "AS-IS" and any express or implied
25 * warranties, including but not limited to, any implied warranties
26 * of merchantability, fitness for a particular purpose, or
27 * non-infringement are disclaimed. In no event shall DIGITAL be
28 * liable for any damages whatsoever, and in particular, DIGITAL
29 * shall not be liable for special, indirect, consequential, or
30 * incidental damages or damages for lost profits, loss of
31 * revenue or loss of use, whether such damages arise in contract,
32 * negligence, tort, under statute, in equity, at law or otherwise,
33 * even if advised of the possibility of such damage.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ofw_i2c_subr.c,v 1.2 2025/09/16 11:37:17 thorpej Exp $");
38
39 #include <sys/param.h>
40 #include <sys/device.h>
41 #include <sys/kmem.h>
42 #include <sys/systm.h>
43 #include <dev/ofw/openfirm.h>
44 #include <dev/i2c/i2cvar.h>
45
46 /*
47 * Iterate over the subtree of a i2c controller node.
48 * Add all sub-devices into an array as part of the controller's
49 * device properties.
50 * This is used by the i2c bus attach code to do direct configuration.
51 */
52 prop_array_t
53 of_copy_i2c_devs(int ofnode, size_t cell_size, int addr_shift)
54 {
55 int node, len;
56 char name[32];
57 uint64_t reg64;
58 uint32_t reg32;
59 uint64_t addr;
60 prop_array_t array = NULL;
61 prop_dictionary_t dev;
62
63 for (node = OF_child(ofnode); node; node = OF_peer(node)) {
64 if (OF_getprop(node, "name", name, sizeof(name)) <= 0)
65 continue;
66 len = OF_getproplen(node, "reg");
67 addr = 0;
68 if (cell_size == 8 && len >= sizeof(reg64)) {
69 if (OF_getprop(node, "reg", ®64, sizeof(reg64))
70 < sizeof(reg64))
71 continue;
72 addr = be64toh(reg64);
73 /*
74 * The i2c bus number (0 or 1) is encoded in bit 33
75 * of the register, but we encode it in bit 8 of
76 * i2c_addr_t.
77 */
78 if (addr & 0x100000000)
79 addr = (addr & 0xff) | 0x100;
80 } else if (cell_size == 4 && len >= sizeof(reg32)) {
81 if (OF_getprop(node, "reg", ®32, sizeof(reg32))
82 < sizeof(reg32))
83 continue;
84 addr = be32toh(reg32);
85 } else {
86 continue;
87 }
88 addr >>= addr_shift;
89 if (addr == 0) continue;
90
91 if (array == NULL)
92 array = prop_array_create();
93
94 dev = prop_dictionary_create();
95 prop_dictionary_set_string(dev, "name", name);
96 prop_dictionary_set_uint32(dev, "addr", addr);
97 prop_dictionary_set_uint64(dev, "cookie", node);
98 prop_dictionary_set_uint32(dev, "cookietype", I2C_COOKIE_OF);
99 of_to_dataprop(dev, node, "compatible", "compatible");
100 prop_array_add(array, dev);
101 prop_object_release(dev);
102 }
103
104 return array;
105 }
106