Home | History | Annotate | Line # | Download | only in sociox
sni_i2c.c revision 1.10
      1 /*	$NetBSD: sni_i2c.c,v 1.10 2021/01/27 03:10:19 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.10 2021/01/27 03:10:19 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 	fdtbus_attach_i2cbus(self, phandle, &sc->sc_ic, iicbus_print);
    152 #endif
    153 	return;
    154  fail:
    155 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    156 	return;
    157 }
    158 
    159 static int
    160 sniiic_acpi_match(device_t parent, struct cfdata *match, void *aux)
    161 {
    162 	static const char * compatible[] = {
    163 		"SCX0003",
    164 		NULL
    165 	};
    166 	struct acpi_attach_args *aa = aux;
    167 
    168 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    169 		return 0;
    170 	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
    171 }
    172 
    173 static void
    174 sniiic_acpi_attach(device_t parent, device_t self, void *aux)
    175 {
    176 	struct sniiic_softc * const sc = device_private(self);
    177 	struct acpi_attach_args *aa = aux;
    178 	bus_space_handle_t ioh;
    179 	struct i2cbus_attach_args iba;
    180 	struct acpi_resources res;
    181 	struct acpi_mem *mem;
    182 	struct acpi_irq *irq;
    183 	ACPI_STATUS rv;
    184 
    185 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    186 	    &res, &acpi_resource_parse_ops_default);
    187 	if (ACPI_FAILURE(rv))
    188 		return;
    189 	mem = acpi_res_mem(&res, 0);
    190 	irq = acpi_res_irq(&res, 0);
    191 	if (mem == NULL || irq == NULL || mem->ar_length == 0) {
    192 		aprint_error(": incomplete resources\n");
    193 		return;
    194 	}
    195 	if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0,
    196 	    &ioh)) {
    197 		aprint_error(": couldn't map registers\n");
    198 		return;
    199 	}
    200 	sc->sc_ih = acpi_intr_establish(self,
    201 	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
    202 	    IPL_BIO, false, sni_i2c_intr, sc, device_xname(self));
    203 	if (sc->sc_ih == NULL) {
    204 		aprint_error_dev(self, "couldn't establish interrupt\n");
    205 		goto fail;
    206 	}
    207 
    208 	aprint_naive("\n");
    209 
    210 	sc->sc_dev = self;
    211 	sc->sc_iot = aa->aa_memt;
    212 	sc->sc_ioh = ioh;
    213 	sc->sc_iob = mem->ar_base;
    214 	sc->sc_ios = mem->ar_length;
    215 
    216 	sni_i2c_common_i(sc);
    217 
    218 	memset(&iba, 0, sizeof(iba));
    219 	iba.iba_tag = &sc->sc_ic;
    220 #if 0
    221 	(void) config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
    222 #endif
    223 
    224 	acpi_resource_cleanup(&res);
    225 
    226 	return;
    227  fail:
    228 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    229 	acpi_resource_cleanup(&res);
    230 	return;
    231 }
    232 
    233 void
    234 sni_i2c_common_i(struct sniiic_softc *sc)
    235 {
    236 
    237 	aprint_normal_dev(sc->sc_dev, "Socionext I2C controller\n");
    238 
    239 	iic_tag_init(&sc->sc_ic);
    240 	sc->sc_ic.ic_cookie = sc;
    241 	sc->sc_ic.ic_acquire_bus = sni_i2c_acquire_bus;
    242 	sc->sc_ic.ic_release_bus = sni_i2c_release_bus;
    243 	sc->sc_ic.ic_exec = sni_i2c_exec;
    244 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    245 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_BIO);
    246 	cv_init(&sc->sc_cv, device_xname(sc->sc_dev));
    247 
    248 	/* no attach here */
    249 }
    250 
    251 static int
    252 sni_i2c_acquire_bus(void *opaque, int flags)
    253 {
    254 	struct sniiic_softc *sc = opaque;
    255 
    256 	mutex_enter(&sc->sc_lock);
    257 	while (sc->sc_busy)
    258 		cv_wait(&sc->sc_cv, &sc->sc_lock);
    259 	sc->sc_busy = true;
    260 	mutex_exit(&sc->sc_lock);
    261 
    262 	return 0;
    263 }
    264 
    265 static void
    266 sni_i2c_release_bus(void *opaque, int flags)
    267 {
    268 	struct sniiic_softc *sc = opaque;
    269 
    270 	mutex_enter(&sc->sc_lock);
    271 	sc->sc_busy = false;
    272 	cv_broadcast(&sc->sc_cv);
    273 	mutex_exit(&sc->sc_lock);
    274 }
    275 
    276 static int
    277 sni_i2c_exec(void *opaque, i2c_op_t op, i2c_addr_t addr,
    278     const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
    279 {
    280 	struct sniiic_softc *sc = opaque;
    281 	int err;
    282 #if 0
    283 	printf("%s: exec op: %d addr: 0x%x cmdlen: %d len: %d flags 0x%x\n",
    284 	    device_xname(sc->sc_dev), op, addr, (int)cmdlen, (int)len, flags);
    285 #endif
    286 
    287 	err = 0;
    288 	/* AAA */
    289 	goto done;
    290  done:
    291 	if (err)
    292 		sni_i2c_reset(sc);
    293 	sni_i2c_flush(sc);
    294 	return err;
    295 }
    296 
    297 static int
    298 sni_i2c_intr(void *arg)
    299 {
    300 	struct sniiic_softc * const sc = arg;
    301 	uint32_t stat = 0;
    302 
    303 	(void)stat;
    304 	mutex_enter(&sc->sc_mtx);
    305 	cv_broadcast(&sc->sc_cv);
    306 	mutex_exit(&sc->sc_mtx);
    307 	return 1;
    308 }
    309 
    310 static void
    311 sni_i2c_reset(struct sniiic_softc *sc)
    312 {
    313 	/* AAA */
    314 }
    315 
    316 static void
    317 sni_i2c_flush(struct sniiic_softc *sc)
    318 {
    319 	/* AAA */
    320 }
    321