1 1.6 thorpej /* $NetBSD: rtciic.c,v 1.6 2025/09/15 13:23:02 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.6 thorpej __KERNEL_RCSID(0, "$NetBSD: rtciic.c,v 1.6 2025/09/15 13:23:02 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 113 1.1 kiyohara sc->sc_dev = self; 114 1.1 kiyohara 115 1.1 kiyohara aprint_normal("\n"); 116 1.1 kiyohara aprint_naive("\n"); 117 1.1 kiyohara 118 1.1 kiyohara /* Map I/O space(16bit). */ 119 1.1 kiyohara sc->sc_iot = 0; 120 1.1 kiyohara if (bus_space_map(sc->sc_iot, ma->ma_addr1, 2, 0, &sc->sc_ioh)) { 121 1.1 kiyohara aprint_error_dev(self, "can't map registers\n"); 122 1.1 kiyohara return; 123 1.1 kiyohara } 124 1.1 kiyohara sc->sc_rw = RTCIIC_READ(sc) & RTCIIC_RW; 125 1.1 kiyohara 126 1.1 kiyohara /* register with iic */ 127 1.2 thorpej iic_tag_init(&sc->sc_i2c); 128 1.1 kiyohara sc->sc_i2c.ic_cookie = sc; 129 1.1 kiyohara sc->sc_i2c.ic_send_start = rtciic_send_start; 130 1.1 kiyohara sc->sc_i2c.ic_send_stop = rtciic_send_stop; 131 1.1 kiyohara sc->sc_i2c.ic_initiate_xfer = rtciic_initiate_xfer; 132 1.1 kiyohara sc->sc_i2c.ic_read_byte = rtciic_read_byte; 133 1.1 kiyohara sc->sc_i2c.ic_write_byte = rtciic_write_byte; 134 1.1 kiyohara 135 1.1 kiyohara sc->sc_bops.ibo_set_dir = rtciic_set_dir; 136 1.1 kiyohara sc->sc_bops.ibo_set_bits = rtciic_set_bits; 137 1.1 kiyohara sc->sc_bops.ibo_read_bits = rtciic_read_bits; 138 1.1 kiyohara sc->sc_bops.ibo_bits[I2C_BIT_SDA] = RTCIIC_SDAW; 139 1.1 kiyohara sc->sc_bops.ibo_bits[I2C_BIT_SCL] = RTCIIC_SCL; 140 1.1 kiyohara sc->sc_bops.ibo_bits[I2C_BIT_OUTPUT] = 0; 141 1.1 kiyohara sc->sc_bops.ibo_bits[I2C_BIT_INPUT] = RTCIIC_RW; 142 1.1 kiyohara 143 1.6 thorpej iicbus_attach(sc->sc_dev, &sc->sc_i2c); 144 1.1 kiyohara } 145 1.1 kiyohara 146 1.1 kiyohara static int 147 1.1 kiyohara rtciic_send_start(void *arg, int flags) 148 1.1 kiyohara { 149 1.1 kiyohara struct rtciic_softc *sc = arg; 150 1.1 kiyohara 151 1.1 kiyohara return i2c_bitbang_send_start(sc, flags, &sc->sc_bops); 152 1.1 kiyohara } 153 1.1 kiyohara 154 1.1 kiyohara static int 155 1.1 kiyohara rtciic_send_stop(void *arg, int flags) 156 1.1 kiyohara { 157 1.1 kiyohara struct rtciic_softc *sc = arg; 158 1.1 kiyohara 159 1.1 kiyohara return i2c_bitbang_send_stop(sc, flags, &sc->sc_bops); 160 1.1 kiyohara } 161 1.1 kiyohara 162 1.1 kiyohara static int 163 1.1 kiyohara rtciic_initiate_xfer(void *arg, i2c_addr_t addr, int flags) 164 1.1 kiyohara { 165 1.1 kiyohara struct rtciic_softc *sc = arg; 166 1.1 kiyohara 167 1.1 kiyohara return i2c_bitbang_initiate_xfer(sc, addr, flags, &sc->sc_bops); 168 1.1 kiyohara } 169 1.1 kiyohara 170 1.1 kiyohara static int 171 1.1 kiyohara rtciic_read_byte(void *arg, uint8_t *vp, int flags) 172 1.1 kiyohara { 173 1.1 kiyohara struct rtciic_softc *sc = arg; 174 1.1 kiyohara 175 1.1 kiyohara return i2c_bitbang_read_byte(sc, vp, flags, &sc->sc_bops); 176 1.1 kiyohara } 177 1.1 kiyohara 178 1.1 kiyohara static int 179 1.1 kiyohara rtciic_write_byte(void *arg, uint8_t v, int flags) 180 1.1 kiyohara { 181 1.1 kiyohara struct rtciic_softc *sc = arg; 182 1.1 kiyohara 183 1.1 kiyohara return i2c_bitbang_write_byte(sc, v, flags, &sc->sc_bops); 184 1.1 kiyohara } 185 1.1 kiyohara 186 1.1 kiyohara 187 1.1 kiyohara static void 188 1.1 kiyohara rtciic_set_dir(void *arg, uint32_t bits) 189 1.1 kiyohara { 190 1.1 kiyohara struct rtciic_softc *sc = arg; 191 1.1 kiyohara uint16_t reg; 192 1.1 kiyohara 193 1.1 kiyohara DPRINTF(("%s: set dir %s\n", 194 1.1 kiyohara device_xname(sc->sc_dev), (bits & RTCIIC_RW) ? "READ" : "WRITE")); 195 1.1 kiyohara 196 1.1 kiyohara if (sc->sc_rw != (bits & RTCIIC_RW)) { 197 1.1 kiyohara reg = RTCIIC_READ(sc); 198 1.1 kiyohara reg &= ~RTCIIC_RW; 199 1.1 kiyohara reg |= bits; 200 1.1 kiyohara RTCIIC_WRITE(sc, reg); 201 1.1 kiyohara delay(30); 202 1.1 kiyohara sc->sc_rw = bits & RTCIIC_RW; 203 1.1 kiyohara } 204 1.1 kiyohara } 205 1.1 kiyohara 206 1.1 kiyohara static void 207 1.1 kiyohara rtciic_set_bits(void *arg, uint32_t bits) 208 1.1 kiyohara { 209 1.1 kiyohara struct rtciic_softc *sc = arg; 210 1.1 kiyohara 211 1.1 kiyohara DPRINTF(("%s: %s\n", 212 1.1 kiyohara device_xname(sc->sc_dev), 213 1.1 kiyohara (bits == (RTCIIC_SDAW | RTCIIC_SCL)) ? "set SDA/SCL" : 214 1.1 kiyohara ((bits == RTCIIC_SDAW) ? "set SDA" : 215 1.1 kiyohara ((bits == RTCIIC_SCL) ? "set SCL" : "reset")))); 216 1.1 kiyohara 217 1.1 kiyohara if (sc->sc_rw & RTCIIC_RW) { 218 1.1 kiyohara bits &= RTCIIC_SCL; 219 1.1 kiyohara bits |= RTCIIC_RW; 220 1.1 kiyohara } 221 1.1 kiyohara RTCIIC_WRITE(sc, bits); 222 1.1 kiyohara delay(40); 223 1.1 kiyohara } 224 1.1 kiyohara 225 1.1 kiyohara static uint32_t 226 1.1 kiyohara rtciic_read_bits(void *arg) 227 1.1 kiyohara { 228 1.1 kiyohara struct rtciic_softc *sc = arg; 229 1.1 kiyohara uint8_t rv, v; 230 1.1 kiyohara 231 1.1 kiyohara v = RTCIIC_READ(sc); 232 1.1 kiyohara rv = v & RTCIIC_SCL; 233 1.1 kiyohara if (v & RTCIIC_SDAR) 234 1.1 kiyohara rv |= RTCIIC_SDAW; 235 1.1 kiyohara 236 1.1 kiyohara DPRINTF(("%s: read %s\n", 237 1.1 kiyohara device_xname(sc->sc_dev), 238 1.1 kiyohara (rv == (RTCIIC_SDAW | RTCIIC_SCL)) ? "SDA/SCL" : 239 1.1 kiyohara ((rv == RTCIIC_SDAW) ? "SDA" : 240 1.1 kiyohara ((rv == RTCIIC_SCL) ? "SCL" : "no")))); 241 1.1 kiyohara 242 1.1 kiyohara return (uint32_t)rv; 243 1.1 kiyohara } 244