Home | History | Annotate | Line # | Download | only in dev
jbus-i2c.c revision 1.7.2.1
      1  1.7.2.1   thorpej /*	$NetBSD: jbus-i2c.c,v 1.7.2.1 2021/08/09 00:30:08 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.7.2.1   thorpej __KERNEL_RCSID(0, "$NetBSD: jbus-i2c.c,v 1.7.2.1 2021/08/09 00:30:08 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 };
     83      1.1  macallan 
     84      1.1  macallan CFATTACH_DECL_NEW(jbusi2c, sizeof(struct jbusi2c_softc),
     85      1.1  macallan     jbusi2c_match, jbusi2c_attach, NULL, NULL);
     86      1.1  macallan 
     87      1.2  macallan /* schizo GPIO registers */
     88      1.2  macallan #define DATA	0
     89      1.2  macallan #define DIR	8
     90      1.1  macallan 
     91      1.1  macallan int
     92      1.1  macallan jbusi2c_match(device_t parent, cfdata_t match, void *aux)
     93      1.1  macallan {
     94      1.1  macallan 	struct mainbus_attach_args *ma = aux;
     95      1.1  macallan 	char *str;
     96      1.1  macallan 
     97      1.1  macallan 	if (strcmp(ma->ma_name, "i2c") != 0)
     98      1.1  macallan 		return (0);
     99      1.1  macallan 
    100      1.1  macallan 	str = prom_getpropstring(ma->ma_node, "compatible");
    101      1.1  macallan 	if (strcmp(str, "jbus-i2c") == 0)
    102      1.1  macallan 		return (1);
    103      1.1  macallan 
    104      1.1  macallan 	return (0);
    105      1.1  macallan }
    106      1.1  macallan 
    107      1.1  macallan void
    108      1.1  macallan jbusi2c_attach(device_t parent, device_t self, void *aux)
    109      1.1  macallan {
    110      1.1  macallan 	struct jbusi2c_softc *sc = device_private(self);
    111      1.1  macallan 	struct mainbus_attach_args *ma = aux;
    112  1.7.2.1   thorpej 	struct i2cbus_attach_args iba;
    113      1.1  macallan 
    114      1.3    martin 	aprint_normal(": addr %" PRIx64 "\n", ma->ma_reg[0].ur_paddr);
    115      1.1  macallan 
    116      1.1  macallan 	sc->sc_dev = self;
    117      1.2  macallan 	sc->sc_bustag = ma->ma_bustag;
    118      1.1  macallan 
    119      1.2  macallan 	if (bus_space_map(sc->sc_bustag, ma->ma_reg[0].ur_paddr, 16, 0,
    120      1.2  macallan 	    &sc->sc_regh)) {
    121      1.2  macallan 		aprint_error(": failed to map registers\n");
    122      1.2  macallan 		return;
    123      1.2  macallan 	}
    124      1.2  macallan 
    125      1.4   thorpej 	iic_tag_init(&sc->sc_i2c);
    126      1.2  macallan 	sc->sc_i2c.ic_cookie = sc;
    127      1.1  macallan 	sc->sc_i2c.ic_send_start = jbusi2c_i2c_send_start;
    128      1.1  macallan 	sc->sc_i2c.ic_send_stop = jbusi2c_i2c_send_stop;
    129      1.1  macallan 	sc->sc_i2c.ic_initiate_xfer = jbusi2c_i2c_initiate_xfer;
    130      1.1  macallan 	sc->sc_i2c.ic_read_byte = jbusi2c_i2c_read_byte;
    131      1.1  macallan 	sc->sc_i2c.ic_write_byte = jbusi2c_i2c_write_byte;
    132      1.1  macallan 
    133      1.1  macallan 	memset(&iba, 0, sizeof(iba));
    134      1.1  macallan 	iba.iba_tag = &sc->sc_i2c;
    135  1.7.2.1   thorpej 	config_found(sc->sc_dev, &iba, iicbus_print,
    136  1.7.2.1   thorpej 	    CFARGS(.devhandle = device_handle(self)));
    137      1.1  macallan }
    138      1.1  macallan 
    139      1.2  macallan static inline void
    140      1.2  macallan jbusi2c_write(struct jbusi2c_softc *sc, int reg, uint64_t bits)
    141      1.2  macallan {
    142      1.2  macallan 	bus_space_write_8(sc->sc_bustag, sc->sc_regh, reg, bits);
    143      1.2  macallan }
    144      1.2  macallan 
    145      1.2  macallan static inline uint64_t
    146      1.2  macallan jbusi2c_read(struct jbusi2c_softc *sc, int reg)
    147      1.2  macallan {
    148      1.2  macallan 	return bus_space_read_8(sc->sc_bustag, sc->sc_regh, reg);
    149      1.2  macallan }
    150      1.2  macallan 
    151      1.1  macallan /* I2C bitbanging */
    152      1.1  macallan static void
    153      1.1  macallan jbusi2c_i2cbb_set_bits(void *cookie, uint32_t bits)
    154      1.1  macallan {
    155      1.2  macallan 	struct jbusi2c_softc *sc = cookie;
    156      1.1  macallan 
    157      1.2  macallan 	jbusi2c_write(sc, DATA, bits);
    158      1.1  macallan }
    159      1.1  macallan 
    160      1.1  macallan static void
    161      1.1  macallan jbusi2c_i2cbb_set_dir(void *cookie, uint32_t dir)
    162      1.1  macallan {
    163      1.2  macallan 	struct jbusi2c_softc *sc = cookie;
    164      1.1  macallan 
    165      1.2  macallan 	jbusi2c_write(sc, DIR, dir);
    166      1.1  macallan }
    167      1.1  macallan 
    168      1.1  macallan static uint32_t
    169      1.1  macallan jbusi2c_i2cbb_read(void *cookie)
    170      1.1  macallan {
    171      1.2  macallan 	struct jbusi2c_softc *sc = cookie;
    172      1.1  macallan 
    173      1.2  macallan 	return jbusi2c_read(sc, DATA);
    174      1.1  macallan }
    175      1.1  macallan 
    176      1.1  macallan /* higher level I2C stuff */
    177      1.1  macallan static int
    178      1.1  macallan jbusi2c_i2c_send_start(void *cookie, int flags)
    179      1.1  macallan {
    180      1.1  macallan 
    181      1.1  macallan 	return i2c_bitbang_send_start(cookie, flags, &jbusi2c_i2cbb_ops);
    182      1.1  macallan }
    183      1.1  macallan 
    184      1.1  macallan static int
    185      1.1  macallan jbusi2c_i2c_send_stop(void *cookie, int flags)
    186      1.1  macallan {
    187      1.1  macallan 
    188      1.1  macallan 	return i2c_bitbang_send_stop(cookie, flags, &jbusi2c_i2cbb_ops);
    189      1.1  macallan }
    190      1.1  macallan 
    191      1.1  macallan static int
    192      1.1  macallan jbusi2c_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    193      1.1  macallan {
    194      1.1  macallan 
    195      1.1  macallan 	return i2c_bitbang_initiate_xfer(cookie, addr, flags,
    196      1.1  macallan 	    &jbusi2c_i2cbb_ops);
    197      1.1  macallan }
    198      1.1  macallan 
    199      1.1  macallan static int
    200      1.1  macallan jbusi2c_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
    201      1.1  macallan {
    202      1.1  macallan 
    203      1.1  macallan 	return i2c_bitbang_read_byte(cookie, valp, flags, &jbusi2c_i2cbb_ops);
    204      1.1  macallan }
    205      1.1  macallan 
    206      1.1  macallan static int
    207      1.1  macallan jbusi2c_i2c_write_byte(void *cookie, uint8_t val, int flags)
    208      1.1  macallan {
    209      1.1  macallan 
    210      1.1  macallan 	return i2c_bitbang_write_byte(cookie, val, flags, &jbusi2c_i2cbb_ops);
    211      1.1  macallan }
    212