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