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