Home | History | Annotate | Line # | Download | only in dev
rtciic.c revision 1.3.10.1
      1  1.3.10.1   thorpej /*	$NetBSD: rtciic.c,v 1.3.10.1 2021/03/23 07:14:49 thorpej Exp $	*/
      2       1.1  kiyohara /*
      3       1.1  kiyohara  * Copyright (c) 2011 KIYOHARA Takashi
      4       1.1  kiyohara  * All rights reserved.
      5       1.1  kiyohara  *
      6       1.1  kiyohara  * Redistribution and use in source and binary forms, with or without
      7       1.1  kiyohara  * modification, are permitted provided that the following conditions
      8       1.1  kiyohara  * are met:
      9       1.1  kiyohara  * 1. Redistributions of source code must retain the above copyright
     10       1.1  kiyohara  *    notice, this list of conditions and the following disclaimer.
     11       1.1  kiyohara  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1  kiyohara  *    notice, this list of conditions and the following disclaimer in the
     13       1.1  kiyohara  *    documentation and/or other materials provided with the distribution.
     14       1.1  kiyohara  *
     15       1.1  kiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16       1.1  kiyohara  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17       1.1  kiyohara  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18       1.1  kiyohara  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19       1.1  kiyohara  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20       1.1  kiyohara  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21       1.1  kiyohara  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22       1.1  kiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23       1.1  kiyohara  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24       1.1  kiyohara  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25       1.1  kiyohara  * POSSIBILITY OF SUCH DAMAGE.
     26       1.1  kiyohara  *
     27       1.1  kiyohara  */
     28       1.1  kiyohara #include <sys/cdefs.h>
     29  1.3.10.1   thorpej __KERNEL_RCSID(0, "$NetBSD: rtciic.c,v 1.3.10.1 2021/03/23 07:14:49 thorpej Exp $");
     30       1.1  kiyohara 
     31       1.1  kiyohara #include <sys/param.h>
     32       1.1  kiyohara #include <sys/bus.h>
     33       1.1  kiyohara #include <sys/device.h>
     34       1.1  kiyohara #include <sys/errno.h>
     35       1.1  kiyohara 
     36       1.1  kiyohara #include <machine/autoconf.h>
     37       1.1  kiyohara 
     38       1.1  kiyohara #include <dev/i2c/i2cvar.h>
     39       1.1  kiyohara #include <dev/i2c/i2c_bitbang.h>
     40       1.1  kiyohara 
     41       1.1  kiyohara #include "locators.h"
     42       1.1  kiyohara 
     43       1.1  kiyohara #ifdef RTCIIC_DEBUG
     44       1.1  kiyohara #define DPRINTF(x) printf x
     45       1.1  kiyohara #else
     46       1.1  kiyohara #define DPRINTF(x)
     47       1.1  kiyohara #endif
     48       1.1  kiyohara 
     49       1.3   msaitoh #define RTCIIC_SDAR	(1 << 3)	/* received serial data */
     50       1.1  kiyohara #define RTCIIC_SDAW	(1 << 2)	/* sended serial data */
     51       1.1  kiyohara #define RTCIIC_SCL	(1 << 1)	/* serial clock */
     52       1.1  kiyohara #define RTCIIC_RW	(1 << 0)	/* data direction (0:write, 1:read) */
     53       1.1  kiyohara 
     54       1.1  kiyohara /* This device is Big Endian */
     55       1.1  kiyohara #define RTCIIC_READ(sc) \
     56       1.1  kiyohara 	bswap16(bus_space_read_2((sc)->sc_iot, (sc)->sc_ioh, 0))
     57       1.1  kiyohara #define RTCIIC_WRITE(sc, val) \
     58       1.1  kiyohara 	bus_space_write_2((sc)->sc_iot, (sc)->sc_ioh, 0, bswap16(val))
     59       1.1  kiyohara 
     60       1.1  kiyohara struct rtciic_softc {
     61       1.1  kiyohara 	device_t sc_dev;
     62       1.1  kiyohara 
     63       1.1  kiyohara 	bus_space_tag_t sc_iot;
     64       1.1  kiyohara 	bus_space_handle_t sc_ioh;
     65       1.1  kiyohara 
     66       1.1  kiyohara 	struct i2c_controller sc_i2c;
     67       1.1  kiyohara 	struct i2c_bitbang_ops sc_bops;
     68       1.1  kiyohara 
     69       1.1  kiyohara 	int sc_rw;
     70       1.1  kiyohara };
     71       1.1  kiyohara 
     72       1.1  kiyohara static int rtciic_match(device_t, cfdata_t , void *);
     73       1.1  kiyohara static void rtciic_attach(device_t, device_t, void *);
     74       1.1  kiyohara 
     75       1.1  kiyohara static int rtciic_send_start(void *, int);
     76       1.1  kiyohara static int rtciic_send_stop(void *, int);
     77       1.1  kiyohara static int rtciic_initiate_xfer(void *, i2c_addr_t, int);
     78       1.1  kiyohara static int rtciic_read_byte(void *, uint8_t *, int);
     79       1.1  kiyohara static int rtciic_write_byte(void *, uint8_t, int);
     80       1.1  kiyohara 
     81       1.1  kiyohara static void rtciic_set_dir(void *, uint32_t);
     82       1.1  kiyohara static void rtciic_set_bits(void *, uint32_t);
     83       1.1  kiyohara static uint32_t rtciic_read_bits(void *);
     84       1.1  kiyohara 
     85       1.1  kiyohara CFATTACH_DECL_NEW(rtciic, sizeof(struct rtciic_softc),
     86       1.1  kiyohara     rtciic_match, rtciic_attach, NULL, NULL);
     87       1.1  kiyohara 
     88       1.1  kiyohara static int
     89       1.1  kiyohara rtciic_match(device_t parent, cfdata_t match, void *aux)
     90       1.1  kiyohara {
     91       1.1  kiyohara 	struct mainbus_attach_args *ma = aux;
     92       1.1  kiyohara 
     93       1.1  kiyohara 	if (strcmp(ma->ma_name, match->cf_name) != 0)
     94       1.1  kiyohara 		return 0;
     95       1.1  kiyohara 
     96       1.1  kiyohara 	/* Disallow wildcarded values. */
     97       1.1  kiyohara 	if (ma->ma_addr1 == MAINBUSCF_ADDR1_DEFAULT)
     98       1.1  kiyohara 		return 0;
     99       1.1  kiyohara 
    100       1.1  kiyohara 	/* no irq */
    101       1.1  kiyohara 	if (ma->ma_irq1 != MAINBUSCF_IRQ1_DEFAULT)
    102       1.1  kiyohara 		return 0;
    103       1.1  kiyohara 
    104       1.1  kiyohara 	return 1;
    105       1.1  kiyohara }
    106       1.1  kiyohara 
    107       1.1  kiyohara void
    108       1.1  kiyohara rtciic_attach(device_t parent, device_t self, void *aux)
    109       1.1  kiyohara {
    110       1.1  kiyohara 	struct rtciic_softc *sc = device_private(self);
    111       1.1  kiyohara 	struct mainbus_attach_args *ma = aux;
    112       1.1  kiyohara 	struct i2cbus_attach_args iba;
    113       1.1  kiyohara 
    114       1.1  kiyohara 	sc->sc_dev = self;
    115       1.1  kiyohara 
    116       1.1  kiyohara 	aprint_normal("\n");
    117       1.1  kiyohara 	aprint_naive("\n");
    118       1.1  kiyohara 
    119       1.1  kiyohara 	/* Map I/O space(16bit). */
    120       1.1  kiyohara 	sc->sc_iot = 0;
    121       1.1  kiyohara 	if (bus_space_map(sc->sc_iot, ma->ma_addr1, 2, 0, &sc->sc_ioh)) {
    122       1.1  kiyohara 		aprint_error_dev(self, "can't map registers\n");
    123       1.1  kiyohara 		return;
    124       1.1  kiyohara 	}
    125       1.1  kiyohara 	sc->sc_rw = RTCIIC_READ(sc) & RTCIIC_RW;
    126       1.1  kiyohara 
    127       1.1  kiyohara 	/* register with iic */
    128       1.2   thorpej 	iic_tag_init(&sc->sc_i2c);
    129       1.1  kiyohara 	sc->sc_i2c.ic_cookie = sc;
    130       1.1  kiyohara 	sc->sc_i2c.ic_send_start = rtciic_send_start;
    131       1.1  kiyohara 	sc->sc_i2c.ic_send_stop = rtciic_send_stop;
    132       1.1  kiyohara 	sc->sc_i2c.ic_initiate_xfer = rtciic_initiate_xfer;
    133       1.1  kiyohara 	sc->sc_i2c.ic_read_byte = rtciic_read_byte;
    134       1.1  kiyohara 	sc->sc_i2c.ic_write_byte = rtciic_write_byte;
    135       1.1  kiyohara 
    136       1.1  kiyohara 	sc->sc_bops.ibo_set_dir = rtciic_set_dir;
    137       1.1  kiyohara 	sc->sc_bops.ibo_set_bits = rtciic_set_bits;
    138       1.1  kiyohara 	sc->sc_bops.ibo_read_bits = rtciic_read_bits;
    139       1.1  kiyohara 	sc->sc_bops.ibo_bits[I2C_BIT_SDA] = RTCIIC_SDAW;
    140       1.1  kiyohara 	sc->sc_bops.ibo_bits[I2C_BIT_SCL] = RTCIIC_SCL;
    141       1.1  kiyohara 	sc->sc_bops.ibo_bits[I2C_BIT_OUTPUT] = 0;
    142       1.1  kiyohara 	sc->sc_bops.ibo_bits[I2C_BIT_INPUT] = RTCIIC_RW;
    143       1.1  kiyohara 
    144       1.1  kiyohara 	memset(&iba, 0, sizeof(iba));
    145       1.1  kiyohara 	iba.iba_tag = &sc->sc_i2c;
    146  1.3.10.1   thorpej 	config_found(sc->sc_dev, &iba, iicbus_print, CFARG_EOL);
    147       1.1  kiyohara }
    148       1.1  kiyohara 
    149       1.1  kiyohara static int
    150       1.1  kiyohara rtciic_send_start(void *arg, int flags)
    151       1.1  kiyohara {
    152       1.1  kiyohara 	struct rtciic_softc *sc = arg;
    153       1.1  kiyohara 
    154       1.1  kiyohara 	return i2c_bitbang_send_start(sc, flags, &sc->sc_bops);
    155       1.1  kiyohara }
    156       1.1  kiyohara 
    157       1.1  kiyohara static int
    158       1.1  kiyohara rtciic_send_stop(void *arg, int flags)
    159       1.1  kiyohara {
    160       1.1  kiyohara 	struct rtciic_softc *sc = arg;
    161       1.1  kiyohara 
    162       1.1  kiyohara 	return i2c_bitbang_send_stop(sc, flags, &sc->sc_bops);
    163       1.1  kiyohara }
    164       1.1  kiyohara 
    165       1.1  kiyohara static int
    166       1.1  kiyohara rtciic_initiate_xfer(void *arg, i2c_addr_t addr, int flags)
    167       1.1  kiyohara {
    168       1.1  kiyohara 	struct rtciic_softc *sc = arg;
    169       1.1  kiyohara 
    170       1.1  kiyohara 	return i2c_bitbang_initiate_xfer(sc, addr, flags, &sc->sc_bops);
    171       1.1  kiyohara }
    172       1.1  kiyohara 
    173       1.1  kiyohara static int
    174       1.1  kiyohara rtciic_read_byte(void *arg, uint8_t *vp, int flags)
    175       1.1  kiyohara {
    176       1.1  kiyohara 	struct rtciic_softc *sc = arg;
    177       1.1  kiyohara 
    178       1.1  kiyohara 	return i2c_bitbang_read_byte(sc, vp, flags, &sc->sc_bops);
    179       1.1  kiyohara }
    180       1.1  kiyohara 
    181       1.1  kiyohara static int
    182       1.1  kiyohara rtciic_write_byte(void *arg, uint8_t v, int flags)
    183       1.1  kiyohara {
    184       1.1  kiyohara 	struct rtciic_softc *sc = arg;
    185       1.1  kiyohara 
    186       1.1  kiyohara 	return i2c_bitbang_write_byte(sc, v, flags, &sc->sc_bops);
    187       1.1  kiyohara }
    188       1.1  kiyohara 
    189       1.1  kiyohara 
    190       1.1  kiyohara static void
    191       1.1  kiyohara rtciic_set_dir(void *arg, uint32_t bits)
    192       1.1  kiyohara {
    193       1.1  kiyohara 	struct rtciic_softc *sc = arg;
    194       1.1  kiyohara 	uint16_t reg;
    195       1.1  kiyohara 
    196       1.1  kiyohara 	DPRINTF(("%s: set dir %s\n",
    197       1.1  kiyohara 	    device_xname(sc->sc_dev), (bits & RTCIIC_RW) ? "READ" : "WRITE"));
    198       1.1  kiyohara 
    199       1.1  kiyohara 	if (sc->sc_rw != (bits & RTCIIC_RW)) {
    200       1.1  kiyohara 		reg = RTCIIC_READ(sc);
    201       1.1  kiyohara 		reg &= ~RTCIIC_RW;
    202       1.1  kiyohara 		reg |= bits;
    203       1.1  kiyohara 		RTCIIC_WRITE(sc, reg);
    204       1.1  kiyohara 		delay(30);
    205       1.1  kiyohara 		sc->sc_rw = bits & RTCIIC_RW;
    206       1.1  kiyohara 	}
    207       1.1  kiyohara }
    208       1.1  kiyohara 
    209       1.1  kiyohara static void
    210       1.1  kiyohara rtciic_set_bits(void *arg, uint32_t bits)
    211       1.1  kiyohara {
    212       1.1  kiyohara 	struct rtciic_softc *sc = arg;
    213       1.1  kiyohara 
    214       1.1  kiyohara 	DPRINTF(("%s: %s\n",
    215       1.1  kiyohara 	    device_xname(sc->sc_dev),
    216       1.1  kiyohara 	    (bits == (RTCIIC_SDAW | RTCIIC_SCL)) ? "set SDA/SCL" :
    217       1.1  kiyohara 	    ((bits == RTCIIC_SDAW) ? "set SDA" :
    218       1.1  kiyohara 	    ((bits == RTCIIC_SCL) ? "set SCL" : "reset"))));
    219       1.1  kiyohara 
    220       1.1  kiyohara 	if (sc->sc_rw & RTCIIC_RW) {
    221       1.1  kiyohara 		bits &= RTCIIC_SCL;
    222       1.1  kiyohara 		bits |= RTCIIC_RW;
    223       1.1  kiyohara 	}
    224       1.1  kiyohara 	RTCIIC_WRITE(sc, bits);
    225       1.1  kiyohara 	delay(40);
    226       1.1  kiyohara }
    227       1.1  kiyohara 
    228       1.1  kiyohara static uint32_t
    229       1.1  kiyohara rtciic_read_bits(void *arg)
    230       1.1  kiyohara {
    231       1.1  kiyohara 	struct rtciic_softc *sc = arg;
    232       1.1  kiyohara 	uint8_t rv, v;
    233       1.1  kiyohara 
    234       1.1  kiyohara 	v = RTCIIC_READ(sc);
    235       1.1  kiyohara 	rv = v & RTCIIC_SCL;
    236       1.1  kiyohara 	if (v & RTCIIC_SDAR)
    237       1.1  kiyohara 		rv |= RTCIIC_SDAW;
    238       1.1  kiyohara 
    239       1.1  kiyohara 	DPRINTF(("%s: read %s\n",
    240       1.1  kiyohara 	    device_xname(sc->sc_dev),
    241       1.1  kiyohara 	    (rv == (RTCIIC_SDAW | RTCIIC_SCL)) ? "SDA/SCL" :
    242       1.1  kiyohara 	    ((rv == RTCIIC_SDAW) ? "SDA" :
    243       1.1  kiyohara 	    ((rv == RTCIIC_SCL) ? "SCL" : "no"))));
    244       1.1  kiyohara 
    245       1.1  kiyohara 	return (uint32_t)rv;
    246       1.1  kiyohara }
    247