Home | History | Annotate | Line # | Download | only in dev
jbus-i2c.c revision 1.5.4.1
      1  1.5.4.1   thorpej /*	$NetBSD: jbus-i2c.c,v 1.5.4.1 2021/03/23 07:14:51 thorpej Exp $	*/
      2      1.1  macallan 
      3      1.1  macallan /*
      4      1.1  macallan  * Copyright (c) 2018 Michael Lorenz
      5      1.1  macallan  * All rights reserved.
      6      1.1  macallan  *
      7      1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8      1.1  macallan  * modification, are permitted provided that the following conditions
      9      1.1  macallan  * are met:
     10      1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11      1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12      1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15      1.1  macallan  *
     16      1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17      1.1  macallan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18      1.1  macallan  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19      1.1  macallan  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20      1.1  macallan  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21      1.1  macallan  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22      1.1  macallan  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23      1.1  macallan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24      1.1  macallan  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     25      1.1  macallan  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26      1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     27      1.1  macallan  */
     28      1.1  macallan 
     29      1.1  macallan #include <sys/cdefs.h>
     30  1.5.4.1   thorpej __KERNEL_RCSID(0, "$NetBSD: jbus-i2c.c,v 1.5.4.1 2021/03/23 07:14:51 thorpej Exp $");
     31      1.1  macallan 
     32      1.1  macallan #include <sys/param.h>
     33      1.1  macallan #include <sys/device.h>
     34      1.1  macallan #include <sys/errno.h>
     35      1.1  macallan 
     36      1.1  macallan #include <sys/bus.h>
     37      1.1  macallan #include <machine/autoconf.h>
     38      1.1  macallan 
     39      1.1  macallan #include <dev/i2c/i2cvar.h>
     40      1.1  macallan #include <dev/i2c/i2c_bitbang.h>
     41      1.1  macallan 
     42      1.1  macallan #include <machine/openfirm.h>
     43      1.1  macallan 
     44      1.2  macallan #ifdef JBUSI2C_DEBUG
     45      1.1  macallan #define DPRINTF printf
     46      1.2  macallan #else
     47      1.2  macallan #define DPRINTF if (0) printf
     48      1.2  macallan #endif
     49      1.1  macallan 
     50      1.1  macallan /* I2C glue */
     51      1.1  macallan static int jbusi2c_i2c_send_start(void *, int);
     52      1.1  macallan static int jbusi2c_i2c_send_stop(void *, int);
     53      1.1  macallan static int jbusi2c_i2c_initiate_xfer(void *, i2c_addr_t, int);
     54      1.1  macallan static int jbusi2c_i2c_read_byte(void *, uint8_t *, int);
     55      1.1  macallan static int jbusi2c_i2c_write_byte(void *, uint8_t, int);
     56      1.1  macallan 
     57      1.1  macallan /* I2C bitbang glue */
     58      1.1  macallan static void jbusi2c_i2cbb_set_bits(void *, uint32_t);
     59      1.1  macallan static void jbusi2c_i2cbb_set_dir(void *, uint32_t);
     60      1.1  macallan static uint32_t jbusi2c_i2cbb_read(void *);
     61      1.1  macallan 
     62      1.1  macallan static const struct i2c_bitbang_ops jbusi2c_i2cbb_ops = {
     63      1.1  macallan 	jbusi2c_i2cbb_set_bits,
     64      1.1  macallan 	jbusi2c_i2cbb_set_dir,
     65      1.1  macallan 	jbusi2c_i2cbb_read,
     66      1.1  macallan 	{
     67      1.1  macallan 		2,	/* bit 1 is data */
     68      1.1  macallan 		1,	/* bit 0 is clock */
     69      1.1  macallan 		3,	/* direction register for both out */
     70      1.1  macallan 		1	/* data in, clock out */
     71      1.1  macallan 	}
     72      1.1  macallan };
     73      1.1  macallan 
     74      1.1  macallan static	int	jbusi2c_match(device_t, cfdata_t, void *);
     75      1.1  macallan static	void	jbusi2c_attach(device_t, device_t, void *);
     76      1.1  macallan 
     77      1.1  macallan struct jbusi2c_softc {
     78      1.1  macallan 	device_t sc_dev;
     79      1.1  macallan 	struct i2c_controller sc_i2c;
     80      1.2  macallan 	bus_space_tag_t sc_bustag;
     81      1.2  macallan 	bus_space_handle_t sc_regh;
     82      1.1  macallan 	int sc_node;
     83      1.1  macallan };
     84      1.1  macallan 
     85      1.1  macallan static void jbusi2c_setup_i2c(struct jbusi2c_softc *);
     86      1.1  macallan 
     87      1.1  macallan CFATTACH_DECL_NEW(jbusi2c, sizeof(struct jbusi2c_softc),
     88      1.1  macallan     jbusi2c_match, jbusi2c_attach, NULL, NULL);
     89      1.1  macallan 
     90      1.2  macallan /* schizo GPIO registers */
     91      1.2  macallan #define DATA	0
     92      1.2  macallan #define DIR	8
     93      1.1  macallan 
     94      1.1  macallan int
     95      1.1  macallan jbusi2c_match(device_t parent, cfdata_t match, void *aux)
     96      1.1  macallan {
     97      1.1  macallan 	struct mainbus_attach_args *ma = aux;
     98      1.1  macallan 	char *str;
     99      1.1  macallan 
    100      1.1  macallan 	if (strcmp(ma->ma_name, "i2c") != 0)
    101      1.1  macallan 		return (0);
    102      1.1  macallan 
    103      1.1  macallan 	str = prom_getpropstring(ma->ma_node, "compatible");
    104      1.1  macallan 	if (strcmp(str, "jbus-i2c") == 0)
    105      1.1  macallan 		return (1);
    106      1.1  macallan 
    107      1.1  macallan 	return (0);
    108      1.1  macallan }
    109      1.1  macallan 
    110      1.1  macallan void
    111      1.1  macallan jbusi2c_attach(device_t parent, device_t self, void *aux)
    112      1.1  macallan {
    113      1.1  macallan 	struct jbusi2c_softc *sc = device_private(self);
    114      1.1  macallan 	struct mainbus_attach_args *ma = aux;
    115      1.1  macallan 
    116      1.3    martin 	aprint_normal(": addr %" PRIx64 "\n", ma->ma_reg[0].ur_paddr);
    117      1.1  macallan 
    118      1.1  macallan 	sc->sc_dev = self;
    119      1.1  macallan 	sc->sc_node = ma->ma_node;
    120      1.2  macallan 	sc->sc_bustag = ma->ma_bustag;
    121      1.1  macallan 
    122      1.2  macallan 	if (bus_space_map(sc->sc_bustag, ma->ma_reg[0].ur_paddr, 16, 0,
    123      1.2  macallan 	    &sc->sc_regh)) {
    124      1.2  macallan 		aprint_error(": failed to map registers\n");
    125      1.2  macallan 		return;
    126      1.2  macallan 	}
    127      1.2  macallan 
    128      1.2  macallan 	jbusi2c_setup_i2c(sc);
    129      1.1  macallan }
    130      1.1  macallan 
    131      1.1  macallan 
    132      1.1  macallan 
    133      1.1  macallan static void
    134      1.1  macallan jbusi2c_setup_i2c(struct jbusi2c_softc *sc)
    135      1.1  macallan {
    136      1.1  macallan 	struct i2cbus_attach_args iba;
    137      1.1  macallan 	prop_array_t cfg;
    138      1.1  macallan 	prop_dictionary_t dev;
    139      1.1  macallan 	prop_dictionary_t dict = device_properties(sc->sc_dev);
    140      1.2  macallan 	int devs, regs[2], addr;
    141      1.1  macallan 	char name[64], compat[256];
    142      1.1  macallan 
    143      1.4   thorpej 	iic_tag_init(&sc->sc_i2c);
    144      1.2  macallan 	sc->sc_i2c.ic_cookie = sc;
    145      1.1  macallan 	sc->sc_i2c.ic_send_start = jbusi2c_i2c_send_start;
    146      1.1  macallan 	sc->sc_i2c.ic_send_stop = jbusi2c_i2c_send_stop;
    147      1.1  macallan 	sc->sc_i2c.ic_initiate_xfer = jbusi2c_i2c_initiate_xfer;
    148      1.1  macallan 	sc->sc_i2c.ic_read_byte = jbusi2c_i2c_read_byte;
    149      1.1  macallan 	sc->sc_i2c.ic_write_byte = jbusi2c_i2c_write_byte;
    150      1.1  macallan 
    151      1.1  macallan 	/* round up i2c devices */
    152      1.1  macallan 	devs = OF_child(sc->sc_node);
    153      1.1  macallan 	cfg = prop_array_create();
    154      1.1  macallan 	prop_dictionary_set(dict, "i2c-child-devices", cfg);
    155      1.1  macallan 	prop_object_release(cfg);
    156      1.1  macallan 	while (devs != 0) {
    157      1.1  macallan 		if (OF_getprop(devs, "name", name, 256) <= 0)
    158      1.1  macallan 			goto skip;
    159      1.1  macallan 		memset(compat, 0, sizeof(compat));
    160      1.1  macallan 		if (OF_getprop(devs, "compatible",
    161      1.1  macallan 		    compat, 255) <= 0)
    162      1.1  macallan 			goto skip;
    163      1.1  macallan 		if (OF_getprop(devs, "reg", regs, 8) <= 0)
    164      1.1  macallan 			goto skip;
    165      1.1  macallan 		if (regs[0] != 0) goto skip;
    166      1.1  macallan 		addr = (regs[1] & 0xff) >> 1;
    167      1.1  macallan 		DPRINTF("-> %s@%d,%x\n", name, regs[0], addr);
    168      1.1  macallan 		dev = prop_dictionary_create();
    169      1.5   thorpej 		prop_dictionary_set_string(dev, "name", name);
    170      1.5   thorpej 		prop_dictionary_set_data(dev, "compatible", compat,
    171      1.5   thorpej 		    strlen(compat)+1);
    172      1.1  macallan 		prop_dictionary_set_uint32(dev, "addr", addr);
    173      1.1  macallan 		prop_dictionary_set_uint64(dev, "cookie", devs);
    174      1.1  macallan 		prop_array_add(cfg, dev);
    175      1.1  macallan 		prop_object_release(dev);
    176      1.1  macallan 	skip:
    177      1.1  macallan 		devs = OF_peer(devs);
    178      1.1  macallan 	}
    179      1.1  macallan 	memset(&iba, 0, sizeof(iba));
    180      1.1  macallan 	iba.iba_tag = &sc->sc_i2c;
    181  1.5.4.1   thorpej 	config_found(sc->sc_dev, &iba, iicbus_print, CFARG_EOL);
    182      1.1  macallan }
    183      1.1  macallan 
    184      1.2  macallan static inline void
    185      1.2  macallan jbusi2c_write(struct jbusi2c_softc *sc, int reg, uint64_t bits)
    186      1.2  macallan {
    187      1.2  macallan 	bus_space_write_8(sc->sc_bustag, sc->sc_regh, reg, bits);
    188      1.2  macallan }
    189      1.2  macallan 
    190      1.2  macallan static inline uint64_t
    191      1.2  macallan jbusi2c_read(struct jbusi2c_softc *sc, int reg)
    192      1.2  macallan {
    193      1.2  macallan 	return bus_space_read_8(sc->sc_bustag, sc->sc_regh, reg);
    194      1.2  macallan }
    195      1.2  macallan 
    196      1.1  macallan /* I2C bitbanging */
    197      1.1  macallan static void
    198      1.1  macallan jbusi2c_i2cbb_set_bits(void *cookie, uint32_t bits)
    199      1.1  macallan {
    200      1.2  macallan 	struct jbusi2c_softc *sc = cookie;
    201      1.1  macallan 
    202      1.2  macallan 	jbusi2c_write(sc, DATA, bits);
    203      1.1  macallan }
    204      1.1  macallan 
    205      1.1  macallan static void
    206      1.1  macallan jbusi2c_i2cbb_set_dir(void *cookie, uint32_t dir)
    207      1.1  macallan {
    208      1.2  macallan 	struct jbusi2c_softc *sc = cookie;
    209      1.1  macallan 
    210      1.2  macallan 	jbusi2c_write(sc, DIR, dir);
    211      1.1  macallan }
    212      1.1  macallan 
    213      1.1  macallan static uint32_t
    214      1.1  macallan jbusi2c_i2cbb_read(void *cookie)
    215      1.1  macallan {
    216      1.2  macallan 	struct jbusi2c_softc *sc = cookie;
    217      1.1  macallan 
    218      1.2  macallan 	return jbusi2c_read(sc, DATA);
    219      1.1  macallan }
    220      1.1  macallan 
    221      1.1  macallan /* higher level I2C stuff */
    222      1.1  macallan static int
    223      1.1  macallan jbusi2c_i2c_send_start(void *cookie, int flags)
    224      1.1  macallan {
    225      1.1  macallan 
    226      1.1  macallan 	return i2c_bitbang_send_start(cookie, flags, &jbusi2c_i2cbb_ops);
    227      1.1  macallan }
    228      1.1  macallan 
    229      1.1  macallan static int
    230      1.1  macallan jbusi2c_i2c_send_stop(void *cookie, int flags)
    231      1.1  macallan {
    232      1.1  macallan 
    233      1.1  macallan 	return i2c_bitbang_send_stop(cookie, flags, &jbusi2c_i2cbb_ops);
    234      1.1  macallan }
    235      1.1  macallan 
    236      1.1  macallan static int
    237      1.1  macallan jbusi2c_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    238      1.1  macallan {
    239      1.1  macallan 
    240      1.1  macallan 	return i2c_bitbang_initiate_xfer(cookie, addr, flags,
    241      1.1  macallan 	    &jbusi2c_i2cbb_ops);
    242      1.1  macallan }
    243      1.1  macallan 
    244      1.1  macallan static int
    245      1.1  macallan jbusi2c_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
    246      1.1  macallan {
    247      1.1  macallan 
    248      1.1  macallan 	return i2c_bitbang_read_byte(cookie, valp, flags, &jbusi2c_i2cbb_ops);
    249      1.1  macallan }
    250      1.1  macallan 
    251      1.1  macallan static int
    252      1.1  macallan jbusi2c_i2c_write_byte(void *cookie, uint8_t val, int flags)
    253      1.1  macallan {
    254      1.1  macallan 
    255      1.1  macallan 	return i2c_bitbang_write_byte(cookie, val, flags, &jbusi2c_i2cbb_ops);
    256      1.1  macallan }
    257