ofw_i2c_subr.c revision 1.1.6.7 1 /* $NetBSD: ofw_i2c_subr.c,v 1.1.6.7 2021/05/14 03:05:52 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Copyright 1998
31 * Digital Equipment Corporation. All rights reserved.
32 *
33 * This software is furnished under license and may be used and
34 * copied only in accordance with the following terms and conditions.
35 * Subject to these conditions, you may download, copy, install,
36 * use, modify and distribute this software in source and/or binary
37 * form. No title or ownership is transferred hereby.
38 *
39 * 1) Any source code used, modified or distributed must reproduce
40 * and retain this copyright notice and list of conditions as
41 * they appear in the source file.
42 *
43 * 2) No right is granted to use any trade name, trademark, or logo of
44 * Digital Equipment Corporation. Neither the "Digital Equipment
45 * Corporation" name nor any trademark or logo of Digital Equipment
46 * Corporation may be used to endorse or promote products derived
47 * from this software without the prior written permission of
48 * Digital Equipment Corporation.
49 *
50 * 3) This software is provided "AS-IS" and any express or implied
51 * warranties, including but not limited to, any implied warranties
52 * of merchantability, fitness for a particular purpose, or
53 * non-infringement are disclaimed. In no event shall DIGITAL be
54 * liable for any damages whatsoever, and in particular, DIGITAL
55 * shall not be liable for special, indirect, consequential, or
56 * incidental damages or damages for lost profits, loss of
57 * revenue or loss of use, whether such damages arise in contract,
58 * negligence, tort, under statute, in equity, at law or otherwise,
59 * even if advised of the possibility of such damage.
60 */
61
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: ofw_i2c_subr.c,v 1.1.6.7 2021/05/14 03:05:52 thorpej Exp $");
64
65 #include <sys/param.h>
66 #include <sys/device.h>
67 #include <sys/endian.h>
68 #include <sys/kmem.h>
69 #include <sys/systm.h>
70 #include <dev/ofw/openfirm.h>
71 #include <dev/i2c/i2cvar.h>
72
73 #ifdef __HAVE_OPENFIRMWARE_VARIANT_AAPL
74 /*
75 * Apple OpenFirmware implementations have the i2c device
76 * address shifted left 1 bit to account for the r/w bit
77 * on the wire. We also want to look at only the least-
78 * significant 8 bits of the address cell.
79 */
80 #define OFW_I2C_ADDRESS_MASK __BITS(0,7)
81 #define OFW_I2C_ADDRESS_SHIFT 1
82
83 /*
84 * Some of Apple's older OpenFirmware implementations are rife with
85 * nodes lacking "compatible" properties.
86 */
87 #define OFW_I2C_ALLOW_MISSING_COMPATIBLE_PROPERTY
88 #endif /* __HAVE_OPENFIRMWARE_VARIANT_AAPL */
89
90 #ifdef __HAVE_OPENFIRMWARE_VARIANT_SUNW
91 /*
92 * Sun OpenFirmware implementations use 2 cells for the
93 * i2c device "reg" property, the first containing the
94 * channel number, the second containing the i2c device
95 * address shifted left 1 bit to account for the r/w bit
96 * on the wire.
97 */
98 #define OFW_I2C_REG_NCELLS 2
99 #define OFW_I2C_REG_CHANNEL 0
100 #define OFW_I2C_REG_ADDRESS 1
101 #define OFW_I2C_ADDRESS_SHIFT 1
102 #endif /* __HAVE_OPENFIRMWARE_VARIANT_SUNW */
103
104 #ifndef OFW_I2C_REG_NCELLS
105 #define OFW_I2C_REG_NCELLS 1
106 #endif
107
108 #ifndef OFW_I2C_REG_ADDRESS
109 #define OFW_I2C_REG_ADDRESS 0
110 #endif
111
112 /* No default for OFW_I2C_REG_CHANNEL. */
113
114 #ifndef OFW_I2C_ADDRESS_MASK
115 #define OFW_I2C_ADDRESS_MASK __BITS(0,31)
116 #endif
117
118 #ifndef OFW_I2C_ADDRESS_SHIFT
119 #define OFW_I2C_ADDRESS_SHIFT 0
120 #endif
121
122 static bool
123 of_i2c_get_address(i2c_tag_t tag, int node, uint32_t *addrp)
124 {
125 uint32_t reg[OFW_I2C_REG_NCELLS];
126 uint32_t addr;
127 #ifdef OFW_I2C_REG_CHANNEL
128 uint32_t channel;
129 #endif
130
131 if (OF_getprop(node, "reg", reg, sizeof(reg)) != sizeof(reg)) {
132 /*
133 * "reg" property is malformed; reject the device.
134 */
135 return false;
136 }
137
138 addr = be32toh(reg[OFW_I2C_REG_ADDRESS]);
139 addr = (addr & OFW_I2C_ADDRESS_MASK) >> OFW_I2C_ADDRESS_SHIFT;
140
141 #ifdef OFW_I2C_REG_CHANNEL
142 /*
143 * If the channel in the "reg" property does not match,
144 * reject the device.
145 */
146 channel = be32toh(reg[OFW_I2C_REG_CHANNEL]);
147 if (channel != tag->ic_channel) {
148 return false;
149 }
150 #endif
151
152 *addrp = addr;
153 return true;
154 }
155
156 /*
157 * This follows the Device Tree bindings for i2c, which for the most part
158 * work for the classical OpenFirmware implementations, as well. There are
159 * some quirks do deal with for different OpenFirmware implementations, which
160 * are mainly in how the "reg" property is interpreted.
161 */
162 static int
163 of_i2c_enumerate_devices(device_t dev, devhandle_t call_handle, void *v)
164 {
165 struct i2c_enumerate_devices_args *args = v;
166 int i2c_node, node;
167 char name[32], compat_buf[32];
168 prop_dictionary_t props;
169 uint32_t addr;
170 char *clist;
171 int clist_size;
172 bool cbrv;
173
174 i2c_node = devhandle_to_of(call_handle);
175
176 for (node = OF_child(i2c_node); node != 0; node = OF_peer(node)) {
177 if (OF_getprop(node, "name", name, sizeof(name)) <= 0) {
178 continue;
179 }
180 if (!of_i2c_get_address(args->ia->ia_tag, node, &addr)) {
181 continue;
182 }
183
184 clist_size = OF_getproplen(node, "compatible");
185 #ifndef OFW_I2C_ALLOW_MISSING_COMPATIBLE_PROPERTY
186 if (clist_size <= 0) {
187 continue;
188 }
189 #endif
190 clist = kmem_tmpbuf_alloc(clist_size,
191 compat_buf, sizeof(compat_buf), KM_SLEEP);
192 if (OF_getprop(node, "compatible", clist, clist_size) <
193 clist_size) {
194 kmem_tmpbuf_free(clist, clist_size, compat_buf);
195 continue;
196 }
197 props = prop_dictionary_create();
198
199 args->ia->ia_addr = (i2c_addr_t)addr;
200 args->ia->ia_name = name;
201 args->ia->ia_clist = clist;
202 args->ia->ia_clist_size = clist_size;
203 args->ia->ia_prop = props;
204 args->ia->ia_devhandle = devhandle_from_of(node);
205
206 cbrv = args->callback(dev, args);
207
208 prop_object_release(props);
209 kmem_tmpbuf_free(clist, clist_size, compat_buf);
210
211 if (!cbrv) {
212 break;
213 }
214 }
215
216 return 0;
217 }
218 OF_DEVICE_CALL_REGISTER("i2c-enumerate-devices", of_i2c_enumerate_devices);
219