ofw_i2c_subr.c revision 1.1.16.3 1 1.1.16.3 thorpej /* $NetBSD: ofw_i2c_subr.c,v 1.1.16.3 2021/09/11 12:44:49 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1.16.1 thorpej * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 1.1.16.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1.16.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1.16.1 thorpej * modification, are permitted provided that the following conditions
9 1.1.16.1 thorpej * are met:
10 1.1.16.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1.16.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1.16.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.16.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1.16.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej *
16 1.1.16.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.16.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.16.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.16.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.16.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.16.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.16.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.16.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.16.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.16.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.16.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
27 1.1 thorpej */
28 1.1 thorpej
29 1.1 thorpej #include <sys/cdefs.h>
30 1.1.16.3 thorpej __KERNEL_RCSID(0, "$NetBSD: ofw_i2c_subr.c,v 1.1.16.3 2021/09/11 12:44:49 thorpej Exp $");
31 1.1 thorpej
32 1.1 thorpej #include <sys/param.h>
33 1.1 thorpej #include <sys/device.h>
34 1.1.16.1 thorpej #include <sys/endian.h>
35 1.1 thorpej #include <sys/kmem.h>
36 1.1 thorpej #include <sys/systm.h>
37 1.1 thorpej #include <dev/ofw/openfirm.h>
38 1.1 thorpej #include <dev/i2c/i2cvar.h>
39 1.1 thorpej
40 1.1.16.1 thorpej #ifdef __HAVE_OPENFIRMWARE_VARIANT_AAPL
41 1.1.16.1 thorpej /*
42 1.1.16.1 thorpej * Apple OpenFirmware implementations have the i2c device
43 1.1.16.1 thorpej * address shifted left 1 bit to account for the r/w bit
44 1.1.16.1 thorpej * on the wire. We also want to look at only the least-
45 1.1.16.1 thorpej * significant 8 bits of the address cell.
46 1.1.16.1 thorpej */
47 1.1.16.1 thorpej #define OFW_I2C_ADDRESS_MASK __BITS(0,7)
48 1.1.16.1 thorpej #define OFW_I2C_ADDRESS_SHIFT 1
49 1.1.16.1 thorpej
50 1.1.16.1 thorpej /*
51 1.1.16.1 thorpej * Some of Apple's older OpenFirmware implementations are rife with
52 1.1.16.1 thorpej * nodes lacking "compatible" properties.
53 1.1.16.1 thorpej */
54 1.1.16.1 thorpej #define OFW_I2C_ALLOW_MISSING_COMPATIBLE_PROPERTY
55 1.1.16.1 thorpej #endif /* __HAVE_OPENFIRMWARE_VARIANT_AAPL */
56 1.1.16.1 thorpej
57 1.1.16.1 thorpej #ifdef __HAVE_OPENFIRMWARE_VARIANT_SUNW
58 1.1.16.1 thorpej /*
59 1.1.16.1 thorpej * Sun OpenFirmware implementations use 2 cells for the
60 1.1.16.1 thorpej * i2c device "reg" property, the first containing the
61 1.1.16.1 thorpej * channel number, the second containing the i2c device
62 1.1.16.1 thorpej * address shifted left 1 bit to account for the r/w bit
63 1.1.16.1 thorpej * on the wire.
64 1.1.16.1 thorpej */
65 1.1.16.1 thorpej #define OFW_I2C_REG_NCELLS 2
66 1.1.16.1 thorpej #define OFW_I2C_REG_CHANNEL 0
67 1.1.16.1 thorpej #define OFW_I2C_REG_ADDRESS 1
68 1.1.16.1 thorpej #define OFW_I2C_ADDRESS_SHIFT 1
69 1.1.16.1 thorpej #endif /* __HAVE_OPENFIRMWARE_VARIANT_SUNW */
70 1.1.16.1 thorpej
71 1.1.16.1 thorpej #ifndef OFW_I2C_REG_NCELLS
72 1.1.16.1 thorpej #define OFW_I2C_REG_NCELLS 1
73 1.1.16.1 thorpej #endif
74 1.1.16.1 thorpej
75 1.1.16.1 thorpej #ifndef OFW_I2C_REG_ADDRESS
76 1.1.16.1 thorpej #define OFW_I2C_REG_ADDRESS 0
77 1.1.16.1 thorpej #endif
78 1.1.16.1 thorpej
79 1.1.16.1 thorpej /* No default for OFW_I2C_REG_CHANNEL. */
80 1.1.16.1 thorpej
81 1.1.16.1 thorpej #ifndef OFW_I2C_ADDRESS_MASK
82 1.1.16.1 thorpej #define OFW_I2C_ADDRESS_MASK __BITS(0,31)
83 1.1.16.1 thorpej #endif
84 1.1.16.1 thorpej
85 1.1.16.1 thorpej #ifndef OFW_I2C_ADDRESS_SHIFT
86 1.1.16.1 thorpej #define OFW_I2C_ADDRESS_SHIFT 0
87 1.1.16.1 thorpej #endif
88 1.1.16.1 thorpej
89 1.1.16.1 thorpej static bool
90 1.1.16.1 thorpej of_i2c_get_address(i2c_tag_t tag, int node, uint32_t *addrp)
91 1.1.16.1 thorpej {
92 1.1.16.1 thorpej uint32_t reg[OFW_I2C_REG_NCELLS];
93 1.1.16.1 thorpej uint32_t addr;
94 1.1.16.1 thorpej #ifdef OFW_I2C_REG_CHANNEL
95 1.1.16.1 thorpej uint32_t channel;
96 1.1.16.1 thorpej #endif
97 1.1.16.1 thorpej
98 1.1.16.1 thorpej if (OF_getprop(node, "reg", reg, sizeof(reg)) != sizeof(reg)) {
99 1.1.16.1 thorpej /*
100 1.1.16.1 thorpej * "reg" property is malformed; reject the device.
101 1.1.16.1 thorpej */
102 1.1.16.1 thorpej return false;
103 1.1.16.1 thorpej }
104 1.1.16.1 thorpej
105 1.1.16.1 thorpej addr = be32toh(reg[OFW_I2C_REG_ADDRESS]);
106 1.1.16.1 thorpej addr = (addr & OFW_I2C_ADDRESS_MASK) >> OFW_I2C_ADDRESS_SHIFT;
107 1.1.16.1 thorpej
108 1.1.16.1 thorpej #ifdef OFW_I2C_REG_CHANNEL
109 1.1.16.1 thorpej /*
110 1.1.16.1 thorpej * If the channel in the "reg" property does not match,
111 1.1.16.1 thorpej * reject the device.
112 1.1.16.1 thorpej */
113 1.1.16.1 thorpej channel = be32toh(reg[OFW_I2C_REG_CHANNEL]);
114 1.1.16.1 thorpej if (channel != tag->ic_channel) {
115 1.1.16.1 thorpej return false;
116 1.1.16.1 thorpej }
117 1.1.16.1 thorpej #endif
118 1.1.16.1 thorpej
119 1.1.16.1 thorpej *addrp = addr;
120 1.1.16.1 thorpej return true;
121 1.1.16.1 thorpej }
122 1.1.16.1 thorpej
123 1.1 thorpej /*
124 1.1.16.1 thorpej * This follows the Device Tree bindings for i2c, which for the most part
125 1.1.16.1 thorpej * work for the classical OpenFirmware implementations, as well. There are
126 1.1.16.1 thorpej * some quirks do deal with for different OpenFirmware implementations, which
127 1.1.16.1 thorpej * are mainly in how the "reg" property is interpreted.
128 1.1 thorpej */
129 1.1.16.1 thorpej static int
130 1.1.16.1 thorpej of_i2c_enumerate_devices(device_t dev, devhandle_t call_handle, void *v)
131 1.1 thorpej {
132 1.1.16.1 thorpej struct i2c_enumerate_devices_args *args = v;
133 1.1.16.1 thorpej int i2c_node, node;
134 1.1.16.1 thorpej char name[32], compat_buf[32];
135 1.1.16.1 thorpej uint32_t addr;
136 1.1.16.1 thorpej char *clist;
137 1.1.16.1 thorpej int clist_size;
138 1.1.16.1 thorpej bool cbrv;
139 1.1 thorpej
140 1.1.16.1 thorpej i2c_node = devhandle_to_of(call_handle);
141 1.1.16.1 thorpej
142 1.1.16.3 thorpej /*
143 1.1.16.3 thorpej * The Device Tree bindings state that if a controller has a
144 1.1.16.3 thorpej * child node named "i2c-bus", then that is the node beneath
145 1.1.16.3 thorpej * which the child devices are populated.
146 1.1.16.3 thorpej */
147 1.1.16.3 thorpej for (node = OF_child(i2c_node); node != 0; node = OF_peer(node)) {
148 1.1.16.3 thorpej if (OF_getprop(node, "name", name, sizeof(name)) <= 0) {
149 1.1.16.3 thorpej continue;
150 1.1.16.3 thorpej }
151 1.1.16.3 thorpej if (strcmp(name, "i2c-bus") == 0) {
152 1.1.16.3 thorpej i2c_node = node;
153 1.1.16.3 thorpej break;
154 1.1.16.3 thorpej }
155 1.1.16.3 thorpej }
156 1.1.16.3 thorpej
157 1.1.16.1 thorpej for (node = OF_child(i2c_node); node != 0; node = OF_peer(node)) {
158 1.1.16.1 thorpej if (OF_getprop(node, "name", name, sizeof(name)) <= 0) {
159 1.1 thorpej continue;
160 1.1.16.1 thorpej }
161 1.1.16.1 thorpej if (!of_i2c_get_address(args->ia->ia_tag, node, &addr)) {
162 1.1 thorpej continue;
163 1.1 thorpej }
164 1.1 thorpej
165 1.1.16.1 thorpej clist_size = OF_getproplen(node, "compatible");
166 1.1.16.1 thorpej if (clist_size <= 0) {
167 1.1.16.1 thorpej #ifndef OFW_I2C_ALLOW_MISSING_COMPATIBLE_PROPERTY
168 1.1.16.1 thorpej continue;
169 1.1.16.1 thorpej #else
170 1.1.16.1 thorpej clist_size = 0;
171 1.1.16.1 thorpej #endif
172 1.1.16.1 thorpej }
173 1.1.16.1 thorpej clist = kmem_tmpbuf_alloc(clist_size,
174 1.1.16.1 thorpej compat_buf, sizeof(compat_buf), KM_SLEEP);
175 1.1.16.1 thorpej if (OF_getprop(node, "compatible", clist, clist_size) <
176 1.1.16.1 thorpej clist_size) {
177 1.1.16.1 thorpej kmem_tmpbuf_free(clist, clist_size, compat_buf);
178 1.1.16.1 thorpej continue;
179 1.1.16.1 thorpej }
180 1.1 thorpej
181 1.1.16.1 thorpej args->ia->ia_addr = (i2c_addr_t)addr;
182 1.1.16.1 thorpej args->ia->ia_name = name;
183 1.1.16.1 thorpej args->ia->ia_clist = clist;
184 1.1.16.1 thorpej args->ia->ia_clist_size = clist_size;
185 1.1.16.1 thorpej args->ia->ia_devhandle = devhandle_from_of(node);
186 1.1.16.1 thorpej
187 1.1.16.1 thorpej cbrv = args->callback(dev, args);
188 1.1 thorpej
189 1.1.16.1 thorpej kmem_tmpbuf_free(clist, clist_size, compat_buf);
190 1.1.16.1 thorpej
191 1.1.16.1 thorpej if (!cbrv) {
192 1.1.16.1 thorpej break;
193 1.1.16.1 thorpej }
194 1.1 thorpej }
195 1.1.16.1 thorpej
196 1.1.16.1 thorpej return 0;
197 1.1 thorpej }
198 1.1.16.1 thorpej OF_DEVICE_CALL_REGISTER("i2c-enumerate-devices", of_i2c_enumerate_devices);
199