Home | History | Annotate | Line # | Download | only in sociox
sni_i2c.c revision 1.4
      1 /*	$NetBSD: sni_i2c.c,v 1.4 2020/03/25 22:11:00 nisimura 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.4 2020/03/25 22:11:00 nisimura 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 	device_t		sc_i2cdev;
     65 	bus_space_tag_t		sc_iot;
     66 	bus_space_handle_t	sc_ioh;
     67 	bus_addr_t		sc_iob;
     68 	bus_size_t		sc_ios;
     69 	void			*sc_ih;
     70 	kmutex_t		sc_lock;
     71 	kmutex_t		sc_mtx;
     72 	kcondvar_t		sc_cv;
     73 	int			sc_opflags;
     74 	bool			sc_busy;
     75 	int			sc_phandle;
     76 };
     77 
     78 CFATTACH_DECL_NEW(sniiic_fdt, sizeof(struct sniiic_softc),
     79     sniiic_fdt_match, sniiic_fdt_attach, NULL, NULL);
     80 
     81 CFATTACH_DECL_NEW(sniiic_acpi, sizeof(struct sniiic_softc),
     82     sniiic_acpi_match, sniiic_acpi_attach, NULL, NULL);
     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 	_Bool disable;
    127 
    128 	prop_dictionary_get_bool(dict, "disable", &disable);
    129 	if (disable) {
    130 		aprint_naive(": disabled\n");
    131 		aprint_normal(": disabled\n");
    132 		return;
    133 	}
    134 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0
    135 	    || bus_space_map(faa->faa_bst, addr, size, 0, &ioh) != 0) {
    136 		aprint_error(": unable to map device\n");
    137 		return;
    138 	}
    139 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    140 		aprint_error(": failed to decode interrupt\n");
    141 		goto fail;
    142 	}
    143 	sc->sc_ih = fdtbus_intr_establish(phandle,
    144 			0, IPL_BIO, 0, sni_i2c_intr, sc);
    145 	if (sc->sc_ih == NULL) {
    146 		aprint_error_dev(self, "couldn't establish interrupt\n");
    147 		goto fail;
    148 	}
    149 
    150 	aprint_naive("\n");
    151 	aprint_normal_dev(self, ": Socionext I2C controller\n");
    152 	aprint_normal_dev(self, ": interrupting on %s\n", intrstr);
    153 
    154 	sc->sc_dev = self;
    155 	sc->sc_iot = faa->faa_bst;
    156 	sc->sc_ioh = ioh;
    157 	sc->sc_iob = addr;
    158 	sc->sc_ios = size;
    159 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    160 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_BIO);
    161 	cv_init(&sc->sc_cv, device_xname(self));
    162 
    163 	iic_tag_init(&sc->sc_ic);
    164 	sc->sc_ic.ic_cookie = sc;
    165 	sc->sc_ic.ic_acquire_bus = sni_i2c_acquire_bus;
    166 	sc->sc_ic.ic_release_bus = sni_i2c_release_bus;
    167 	sc->sc_ic.ic_exec = sni_i2c_exec;
    168 
    169 	fdtbus_register_i2c_controller(self, phandle, &sni_i2c_funcs);
    170 #if 0
    171 	fdtbus_attach_i2cbus(self, phandle, &sc->sc_ic, iicbus_print);
    172 #endif
    173 	return;
    174  fail:
    175 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    176 	return;
    177 }
    178 
    179 static int
    180 sniiic_acpi_match(device_t parent, struct cfdata *match, void *aux)
    181 {
    182 	static const char * compatible[] = {
    183 		"SCX0003",
    184 		NULL
    185 	};
    186 	struct acpi_attach_args *aa = aux;
    187 
    188 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    189 		return 0;
    190 	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
    191 }
    192 
    193 static void
    194 sniiic_acpi_attach(device_t parent, device_t self, void *aux)
    195 {
    196 	struct sniiic_softc * const sc = device_private(self);
    197 	struct acpi_attach_args *aa = aux;
    198 	bus_space_handle_t ioh;
    199 	struct i2cbus_attach_args iba;
    200 	struct acpi_resources res;
    201 	struct acpi_mem *mem;
    202 	struct acpi_irq *irq;
    203 	ACPI_STATUS rv;
    204 
    205 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    206 	    &res, &acpi_resource_parse_ops_default);
    207 	if (ACPI_FAILURE(rv))
    208 		return;
    209 	mem = acpi_res_mem(&res, 0);
    210 	irq = acpi_res_irq(&res, 0);
    211 	if (mem == NULL || irq == NULL || mem->ar_length == 0) {
    212 		aprint_error(": incomplete resources\n");
    213 		return;
    214 	}
    215 	if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0,
    216 	    &ioh)) {
    217 		aprint_error(": couldn't map registers\n");
    218 		return;
    219 	}
    220 	sc->sc_ih = acpi_intr_establish(self,
    221 	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
    222 	    IPL_BIO, false, sni_i2c_intr, sc, device_xname(self));
    223 	if (sc->sc_ih == NULL) {
    224 		aprint_error_dev(self, "couldn't establish interrupt\n");
    225 		goto fail;
    226 	}
    227 
    228 	aprint_naive("\n");
    229 	aprint_normal_dev(self, ": Socionext I2C controller\n");
    230 
    231 	sc->sc_dev = self;
    232 	sc->sc_iot = aa->aa_memt;
    233 	sc->sc_ioh = ioh;
    234 	sc->sc_iob = mem->ar_base;
    235 	sc->sc_ios = mem->ar_length;
    236 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    237 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NET);
    238 	cv_init(&sc->sc_cv, device_xname(self));
    239 
    240 	iic_tag_init(&sc->sc_ic);
    241 	sc->sc_ic.ic_cookie = sc;
    242 	sc->sc_ic.ic_acquire_bus = sni_i2c_acquire_bus;
    243 	sc->sc_ic.ic_release_bus = sni_i2c_release_bus;
    244 	sc->sc_ic.ic_exec = sni_i2c_exec;
    245 
    246 	memset(&iba, 0, sizeof(iba));
    247 	iba.iba_tag = &sc->sc_ic;
    248 #if 0
    249 	(void) config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
    250 #endif
    251 
    252 	acpi_resource_cleanup(&res);
    253 
    254 	return;
    255  fail:
    256 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    257 	acpi_resource_cleanup(&res);
    258 	return;
    259 }
    260 
    261 static int
    262 sni_i2c_acquire_bus(void *opaque, int flags)
    263 {
    264 	struct sniiic_softc *sc = opaque;
    265 
    266 	mutex_enter(&sc->sc_lock);
    267 	while (sc->sc_busy)
    268 		cv_wait(&sc->sc_cv, &sc->sc_lock);
    269 	sc->sc_busy = true;
    270 	mutex_exit(&sc->sc_lock);
    271 
    272 	return 0;
    273 }
    274 
    275 static void
    276 sni_i2c_release_bus(void *opaque, int flags)
    277 {
    278 	struct sniiic_softc *sc = opaque;
    279 
    280 	mutex_enter(&sc->sc_lock);
    281 	sc->sc_busy = false;
    282 	cv_broadcast(&sc->sc_cv);
    283 	mutex_exit(&sc->sc_lock);
    284 }
    285 
    286 static int
    287 sni_i2c_exec(void *opaque, i2c_op_t op, i2c_addr_t addr,
    288     const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
    289 {
    290 	struct sniiic_softc *sc = opaque;
    291 	int err;
    292 #if 0
    293 	printf("%s: exec op: %d addr: 0x%x cmdlen: %d len: %d flags 0x%x\n",
    294 	    device_xname(sc->sc_dev), op, addr, (int)cmdlen, (int)len, flags);
    295 #endif
    296 
    297 	err = 0;
    298 	/* AAA */
    299 	goto done;
    300  done:
    301 	if (err)
    302 		sni_i2c_reset(sc);
    303 	sni_i2c_flush(sc);
    304 	return err;
    305 }
    306 
    307 static int
    308 sni_i2c_intr(void *arg)
    309 {
    310 	struct sniiic_softc * const sc = arg;
    311 	uint32_t stat = 0;
    312 
    313 	(void)stat;
    314 	mutex_enter(&sc->sc_mtx);
    315 	cv_broadcast(&sc->sc_cv);
    316 	mutex_exit(&sc->sc_mtx);
    317 	return 1;
    318 }
    319 
    320 static void
    321 sni_i2c_reset(struct sniiic_softc *sc)
    322 {
    323 	/* AAA */
    324 }
    325 
    326 static void
    327 sni_i2c_flush(struct sniiic_softc *sc)
    328 {
    329 	/* AAA */
    330 }
    331 
    332 static i2c_tag_t
    333 sni_i2c_get_tag(device_t dev)
    334 {
    335 	struct sniiic_softc * const sc = device_private(dev);
    336 
    337 	return &sc->sc_ic;
    338 }
    339