sni_i2c.c revision 1.12.2.1 1 /* $NetBSD: sni_i2c.c,v 1.12.2.1 2021/08/09 00:30:07 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tohru Nishimura.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Socionext SC2A11 SynQuacer I2C driver
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: sni_i2c.c,v 1.12.2.1 2021/08/09 00:30:07 thorpej Exp $");
38
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/intr.h>
42 #include <sys/device.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/errno.h>
46 #include <sys/kernel.h>
47 #include <sys/systm.h>
48
49 #include <dev/i2c/i2cvar.h>
50
51 #include <dev/fdt/fdtvar.h>
52 #include <dev/acpi/acpireg.h>
53 #include <dev/acpi/acpivar.h>
54 #include <dev/acpi/acpi_intr.h>
55
56 static int sniiic_fdt_match(device_t, struct cfdata *, void *);
57 static void sniiic_fdt_attach(device_t, device_t, void *);
58 static int sniiic_acpi_match(device_t, struct cfdata *, void *);
59 static void sniiic_acpi_attach(device_t, device_t, void *);
60
61 struct sniiic_softc {
62 device_t sc_dev;
63 struct i2c_controller sc_ic;
64 bus_space_tag_t sc_iot;
65 bus_space_handle_t sc_ioh;
66 bus_addr_t sc_iob;
67 bus_size_t sc_ios;
68 void *sc_ih;
69 kmutex_t sc_lock;
70 kmutex_t sc_mtx;
71 kcondvar_t sc_cv;
72 volatile bool sc_busy;
73 int sc_phandle;
74 };
75
76 CFATTACH_DECL_NEW(sniiic_fdt, sizeof(struct sniiic_softc),
77 sniiic_fdt_match, sniiic_fdt_attach, NULL, NULL);
78
79 CFATTACH_DECL_NEW(sniiic_acpi, sizeof(struct sniiic_softc),
80 sniiic_acpi_match, sniiic_acpi_attach, NULL, NULL);
81
82 void sni_i2c_common_i(struct sniiic_softc *);
83
84 static int sni_i2c_acquire_bus(void *, int);
85 static void sni_i2c_release_bus(void *, int);
86 static int sni_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
87 size_t, void *, size_t, int);
88
89 static int sni_i2c_intr(void *);
90 static void sni_i2c_reset(struct sniiic_softc *);
91 static void sni_i2c_flush(struct sniiic_softc *);
92
93 #define I2C_READ(sc, reg) \
94 bus_space_read_4((sc)->sc_ioh,(sc)->sc_ioh,(reg))
95 #define I2C_WRITE(sc, reg, val) \
96 bus_space_write_4((sc)->sc_ioh,(sc)->sc_ioh,(reg),(val))
97
98 static const struct device_compatible_entry compat_data[] = {
99 { .compat = "socionext,synquacer-i2c" },
100 DEVICE_COMPAT_EOL
101 };
102
103 static int
104 sniiic_fdt_match(device_t parent, struct cfdata *match, void *aux)
105 {
106 struct fdt_attach_args * const faa = aux;
107
108 return of_compatible_match(faa->faa_phandle, compat_data);
109 }
110
111 static void
112 sniiic_fdt_attach(device_t parent, device_t self, void *aux)
113 {
114 struct sniiic_softc * const sc = device_private(self);
115 struct fdt_attach_args * const faa = aux;
116 const int phandle = faa->faa_phandle;
117 bus_space_handle_t ioh;
118 bus_addr_t addr;
119 bus_size_t size;
120 char intrstr[128];
121
122 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0
123 || bus_space_map(faa->faa_bst, addr, size, 0, &ioh) != 0) {
124 aprint_error(": unable to map device\n");
125 return;
126 }
127 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
128 aprint_error(": failed to decode interrupt\n");
129 goto fail;
130 }
131 sc->sc_ih = fdtbus_intr_establish(phandle,
132 0, IPL_BIO, 0, sni_i2c_intr, sc);
133 if (sc->sc_ih == NULL) {
134 aprint_error_dev(self, "couldn't establish interrupt\n");
135 goto fail;
136 }
137
138 aprint_naive("\n");
139 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
140
141 sc->sc_dev = self;
142 sc->sc_iot = faa->faa_bst;
143 sc->sc_ioh = ioh;
144 sc->sc_iob = addr;
145 sc->sc_ios = size;
146
147 sni_i2c_common_i(sc);
148
149 fdtbus_register_i2c_controller(&sc->sc_ic, phandle);
150 #if 0
151 struct i2cbus_attach_args iba = {
152 .iba_tag = &sc->sc_i2c,
153 };
154 config_found(self, &iba, iicbus_print,
155 CFARGS(.devhandle = device_handle(self)));
156 #endif
157 return;
158 fail:
159 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
160 return;
161 }
162
163 static int
164 sniiic_acpi_match(device_t parent, struct cfdata *match, void *aux)
165 {
166 static const char * compatible[] = {
167 "SCX0003",
168 NULL
169 };
170 struct acpi_attach_args *aa = aux;
171
172 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
173 return 0;
174 return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
175 }
176
177 static void
178 sniiic_acpi_attach(device_t parent, device_t self, void *aux)
179 {
180 struct sniiic_softc * const sc = device_private(self);
181 struct acpi_attach_args *aa = aux;
182 bus_space_handle_t ioh;
183 struct i2cbus_attach_args iba;
184 struct acpi_resources res;
185 struct acpi_mem *mem;
186 struct acpi_irq *irq;
187 ACPI_STATUS rv;
188
189 rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
190 &res, &acpi_resource_parse_ops_default);
191 if (ACPI_FAILURE(rv))
192 return;
193 mem = acpi_res_mem(&res, 0);
194 irq = acpi_res_irq(&res, 0);
195 if (mem == NULL || irq == NULL || mem->ar_length == 0) {
196 aprint_error(": incomplete resources\n");
197 return;
198 }
199 if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0,
200 &ioh)) {
201 aprint_error(": couldn't map registers\n");
202 return;
203 }
204 sc->sc_ih = acpi_intr_establish(self,
205 (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
206 IPL_BIO, false, sni_i2c_intr, sc, device_xname(self));
207 if (sc->sc_ih == NULL) {
208 aprint_error_dev(self, "couldn't establish interrupt\n");
209 goto fail;
210 }
211
212 aprint_naive("\n");
213
214 sc->sc_dev = self;
215 sc->sc_iot = aa->aa_memt;
216 sc->sc_ioh = ioh;
217 sc->sc_iob = mem->ar_base;
218 sc->sc_ios = mem->ar_length;
219
220 sni_i2c_common_i(sc);
221
222 memset(&iba, 0, sizeof(iba));
223 iba.iba_tag = &sc->sc_ic;
224 #if 0
225 config_found(sc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
226 #endif
227
228 acpi_resource_cleanup(&res);
229
230 return;
231 fail:
232 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
233 acpi_resource_cleanup(&res);
234 return;
235 }
236
237 void
238 sni_i2c_common_i(struct sniiic_softc *sc)
239 {
240
241 aprint_normal_dev(sc->sc_dev, "Socionext I2C controller\n");
242
243 iic_tag_init(&sc->sc_ic);
244 sc->sc_ic.ic_cookie = sc;
245 sc->sc_ic.ic_acquire_bus = sni_i2c_acquire_bus;
246 sc->sc_ic.ic_release_bus = sni_i2c_release_bus;
247 sc->sc_ic.ic_exec = sni_i2c_exec;
248 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
249 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_BIO);
250 cv_init(&sc->sc_cv, device_xname(sc->sc_dev));
251
252 /* no attach here */
253 }
254
255 static int
256 sni_i2c_acquire_bus(void *opaque, int flags)
257 {
258 struct sniiic_softc *sc = opaque;
259
260 mutex_enter(&sc->sc_lock);
261 while (sc->sc_busy)
262 cv_wait(&sc->sc_cv, &sc->sc_lock);
263 sc->sc_busy = true;
264 mutex_exit(&sc->sc_lock);
265
266 return 0;
267 }
268
269 static void
270 sni_i2c_release_bus(void *opaque, int flags)
271 {
272 struct sniiic_softc *sc = opaque;
273
274 mutex_enter(&sc->sc_lock);
275 sc->sc_busy = false;
276 cv_broadcast(&sc->sc_cv);
277 mutex_exit(&sc->sc_lock);
278 }
279
280 static int
281 sni_i2c_exec(void *opaque, i2c_op_t op, i2c_addr_t addr,
282 const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
283 {
284 struct sniiic_softc *sc = opaque;
285 int err;
286 #if 0
287 printf("%s: exec op: %d addr: 0x%x cmdlen: %d len: %d flags 0x%x\n",
288 device_xname(sc->sc_dev), op, addr, (int)cmdlen, (int)len, flags);
289 #endif
290
291 err = 0;
292 /* AAA */
293 goto done;
294 done:
295 if (err)
296 sni_i2c_reset(sc);
297 sni_i2c_flush(sc);
298 return err;
299 }
300
301 static int
302 sni_i2c_intr(void *arg)
303 {
304 struct sniiic_softc * const sc = arg;
305 uint32_t stat = 0;
306
307 (void)stat;
308 mutex_enter(&sc->sc_mtx);
309 cv_broadcast(&sc->sc_cv);
310 mutex_exit(&sc->sc_mtx);
311 return 1;
312 }
313
314 static void
315 sni_i2c_reset(struct sniiic_softc *sc)
316 {
317 /* AAA */
318 }
319
320 static void
321 sni_i2c_flush(struct sniiic_softc *sc)
322 {
323 /* AAA */
324 }
325