sni_i2c.c revision 1.9 1 /* $NetBSD: sni_i2c.c,v 1.9 2020/12/23 16:02:11 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.9 2020/12/23 16:02:11 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 int
99 sniiic_fdt_match(device_t parent, struct cfdata *match, void *aux)
100 {
101 static const char * compatible[] = {
102 "socionext,synquacer-i2c",
103 NULL
104 };
105 struct fdt_attach_args * const faa = aux;
106
107 return of_match_compatible(faa->faa_phandle, compatible);
108 }
109
110 static void
111 sniiic_fdt_attach(device_t parent, device_t self, void *aux)
112 {
113 struct sniiic_softc * const sc = device_private(self);
114 struct fdt_attach_args * const faa = aux;
115 const int phandle = faa->faa_phandle;
116 bus_space_handle_t ioh;
117 bus_addr_t addr;
118 bus_size_t size;
119 char intrstr[128];
120
121 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0
122 || bus_space_map(faa->faa_bst, addr, size, 0, &ioh) != 0) {
123 aprint_error(": unable to map device\n");
124 return;
125 }
126 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
127 aprint_error(": failed to decode interrupt\n");
128 goto fail;
129 }
130 sc->sc_ih = fdtbus_intr_establish(phandle,
131 0, IPL_BIO, 0, sni_i2c_intr, sc);
132 if (sc->sc_ih == NULL) {
133 aprint_error_dev(self, "couldn't establish interrupt\n");
134 goto fail;
135 }
136
137 aprint_naive("\n");
138 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
139
140 sc->sc_dev = self;
141 sc->sc_iot = faa->faa_bst;
142 sc->sc_ioh = ioh;
143 sc->sc_iob = addr;
144 sc->sc_ios = size;
145
146 sni_i2c_common_i(sc);
147
148 fdtbus_register_i2c_controller(&sc->sc_ic, phandle);
149 #if 0
150 fdtbus_attach_i2cbus(self, phandle, &sc->sc_ic, iicbus_print);
151 #endif
152 return;
153 fail:
154 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
155 return;
156 }
157
158 static int
159 sniiic_acpi_match(device_t parent, struct cfdata *match, void *aux)
160 {
161 static const char * compatible[] = {
162 "SCX0003",
163 NULL
164 };
165 struct acpi_attach_args *aa = aux;
166
167 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
168 return 0;
169 return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
170 }
171
172 static void
173 sniiic_acpi_attach(device_t parent, device_t self, void *aux)
174 {
175 struct sniiic_softc * const sc = device_private(self);
176 struct acpi_attach_args *aa = aux;
177 bus_space_handle_t ioh;
178 struct i2cbus_attach_args iba;
179 struct acpi_resources res;
180 struct acpi_mem *mem;
181 struct acpi_irq *irq;
182 ACPI_STATUS rv;
183
184 rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
185 &res, &acpi_resource_parse_ops_default);
186 if (ACPI_FAILURE(rv))
187 return;
188 mem = acpi_res_mem(&res, 0);
189 irq = acpi_res_irq(&res, 0);
190 if (mem == NULL || irq == NULL || mem->ar_length == 0) {
191 aprint_error(": incomplete resources\n");
192 return;
193 }
194 if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0,
195 &ioh)) {
196 aprint_error(": couldn't map registers\n");
197 return;
198 }
199 sc->sc_ih = acpi_intr_establish(self,
200 (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
201 IPL_BIO, false, sni_i2c_intr, sc, device_xname(self));
202 if (sc->sc_ih == NULL) {
203 aprint_error_dev(self, "couldn't establish interrupt\n");
204 goto fail;
205 }
206
207 aprint_naive("\n");
208
209 sc->sc_dev = self;
210 sc->sc_iot = aa->aa_memt;
211 sc->sc_ioh = ioh;
212 sc->sc_iob = mem->ar_base;
213 sc->sc_ios = mem->ar_length;
214
215 sni_i2c_common_i(sc);
216
217 memset(&iba, 0, sizeof(iba));
218 iba.iba_tag = &sc->sc_ic;
219 #if 0
220 (void) config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
221 #endif
222
223 acpi_resource_cleanup(&res);
224
225 return;
226 fail:
227 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
228 acpi_resource_cleanup(&res);
229 return;
230 }
231
232 void
233 sni_i2c_common_i(struct sniiic_softc *sc)
234 {
235
236 aprint_normal_dev(sc->sc_dev, "Socionext I2C controller\n");
237
238 iic_tag_init(&sc->sc_ic);
239 sc->sc_ic.ic_cookie = sc;
240 sc->sc_ic.ic_acquire_bus = sni_i2c_acquire_bus;
241 sc->sc_ic.ic_release_bus = sni_i2c_release_bus;
242 sc->sc_ic.ic_exec = sni_i2c_exec;
243 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
244 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_BIO);
245 cv_init(&sc->sc_cv, device_xname(sc->sc_dev));
246
247 /* no attach here */
248 }
249
250 static int
251 sni_i2c_acquire_bus(void *opaque, int flags)
252 {
253 struct sniiic_softc *sc = opaque;
254
255 mutex_enter(&sc->sc_lock);
256 while (sc->sc_busy)
257 cv_wait(&sc->sc_cv, &sc->sc_lock);
258 sc->sc_busy = true;
259 mutex_exit(&sc->sc_lock);
260
261 return 0;
262 }
263
264 static void
265 sni_i2c_release_bus(void *opaque, int flags)
266 {
267 struct sniiic_softc *sc = opaque;
268
269 mutex_enter(&sc->sc_lock);
270 sc->sc_busy = false;
271 cv_broadcast(&sc->sc_cv);
272 mutex_exit(&sc->sc_lock);
273 }
274
275 static int
276 sni_i2c_exec(void *opaque, i2c_op_t op, i2c_addr_t addr,
277 const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
278 {
279 struct sniiic_softc *sc = opaque;
280 int err;
281 #if 0
282 printf("%s: exec op: %d addr: 0x%x cmdlen: %d len: %d flags 0x%x\n",
283 device_xname(sc->sc_dev), op, addr, (int)cmdlen, (int)len, flags);
284 #endif
285
286 err = 0;
287 /* AAA */
288 goto done;
289 done:
290 if (err)
291 sni_i2c_reset(sc);
292 sni_i2c_flush(sc);
293 return err;
294 }
295
296 static int
297 sni_i2c_intr(void *arg)
298 {
299 struct sniiic_softc * const sc = arg;
300 uint32_t stat = 0;
301
302 (void)stat;
303 mutex_enter(&sc->sc_mtx);
304 cv_broadcast(&sc->sc_cv);
305 mutex_exit(&sc->sc_mtx);
306 return 1;
307 }
308
309 static void
310 sni_i2c_reset(struct sniiic_softc *sc)
311 {
312 /* AAA */
313 }
314
315 static void
316 sni_i2c_flush(struct sniiic_softc *sc)
317 {
318 /* AAA */
319 }
320