1 /* $NetBSD: rtciic.c,v 1.1 2011/02/19 10:46:28 kiyohara 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.1 2011/02/19 10:46:28 kiyohara 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) /* recived 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_acquire_bus(void *, int); 76 static void rtciic_release_bus(void *, int); 77 static int rtciic_send_start(void *, int); 78 static int rtciic_send_stop(void *, int); 79 static int rtciic_initiate_xfer(void *, i2c_addr_t, int); 80 static int rtciic_read_byte(void *, uint8_t *, int); 81 static int rtciic_write_byte(void *, uint8_t, int); 82 83 static void rtciic_set_dir(void *, uint32_t); 84 static void rtciic_set_bits(void *, uint32_t); 85 static uint32_t rtciic_read_bits(void *); 86 87 CFATTACH_DECL_NEW(rtciic, sizeof(struct rtciic_softc), 88 rtciic_match, rtciic_attach, NULL, NULL); 89 90 static int 91 rtciic_match(device_t parent, cfdata_t match, void *aux) 92 { 93 struct mainbus_attach_args *ma = aux; 94 95 if (strcmp(ma->ma_name, match->cf_name) != 0) 96 return 0; 97 98 /* Disallow wildcarded values. */ 99 if (ma->ma_addr1 == MAINBUSCF_ADDR1_DEFAULT) 100 return 0; 101 102 /* no irq */ 103 if (ma->ma_irq1 != MAINBUSCF_IRQ1_DEFAULT) 104 return 0; 105 106 return 1; 107 } 108 109 void 110 rtciic_attach(device_t parent, device_t self, void *aux) 111 { 112 struct rtciic_softc *sc = device_private(self); 113 struct mainbus_attach_args *ma = aux; 114 struct i2cbus_attach_args iba; 115 116 sc->sc_dev = self; 117 118 aprint_normal("\n"); 119 aprint_naive("\n"); 120 121 /* Map I/O space(16bit). */ 122 sc->sc_iot = 0; 123 if (bus_space_map(sc->sc_iot, ma->ma_addr1, 2, 0, &sc->sc_ioh)) { 124 aprint_error_dev(self, "can't map registers\n"); 125 return; 126 } 127 sc->sc_rw = RTCIIC_READ(sc) & RTCIIC_RW; 128 129 /* register with iic */ 130 sc->sc_i2c.ic_cookie = sc; 131 sc->sc_i2c.ic_acquire_bus = rtciic_acquire_bus; 132 sc->sc_i2c.ic_release_bus = rtciic_release_bus; 133 sc->sc_i2c.ic_exec = NULL; 134 sc->sc_i2c.ic_send_start = rtciic_send_start; 135 sc->sc_i2c.ic_send_stop = rtciic_send_stop; 136 sc->sc_i2c.ic_initiate_xfer = rtciic_initiate_xfer; 137 sc->sc_i2c.ic_read_byte = rtciic_read_byte; 138 sc->sc_i2c.ic_write_byte = rtciic_write_byte; 139 140 sc->sc_bops.ibo_set_dir = rtciic_set_dir; 141 sc->sc_bops.ibo_set_bits = rtciic_set_bits; 142 sc->sc_bops.ibo_read_bits = rtciic_read_bits; 143 sc->sc_bops.ibo_bits[I2C_BIT_SDA] = RTCIIC_SDAW; 144 sc->sc_bops.ibo_bits[I2C_BIT_SCL] = RTCIIC_SCL; 145 sc->sc_bops.ibo_bits[I2C_BIT_OUTPUT] = 0; 146 sc->sc_bops.ibo_bits[I2C_BIT_INPUT] = RTCIIC_RW; 147 148 memset(&iba, 0, sizeof(iba)); 149 iba.iba_tag = &sc->sc_i2c; 150 (void) config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print); 151 } 152 153 154 static int 155 rtciic_acquire_bus(void *cookie, int flags) 156 { 157 158 return 0; 159 } 160 161 static void 162 rtciic_release_bus(void *cookie, int flags) 163 { 164 /* nothing */ 165 } 166 167 static int 168 rtciic_send_start(void *arg, int flags) 169 { 170 struct rtciic_softc *sc = arg; 171 172 return i2c_bitbang_send_start(sc, flags, &sc->sc_bops); 173 } 174 175 static int 176 rtciic_send_stop(void *arg, int flags) 177 { 178 struct rtciic_softc *sc = arg; 179 180 return i2c_bitbang_send_stop(sc, flags, &sc->sc_bops); 181 } 182 183 static int 184 rtciic_initiate_xfer(void *arg, i2c_addr_t addr, int flags) 185 { 186 struct rtciic_softc *sc = arg; 187 188 return i2c_bitbang_initiate_xfer(sc, addr, flags, &sc->sc_bops); 189 } 190 191 static int 192 rtciic_read_byte(void *arg, uint8_t *vp, int flags) 193 { 194 struct rtciic_softc *sc = arg; 195 196 return i2c_bitbang_read_byte(sc, vp, flags, &sc->sc_bops); 197 } 198 199 static int 200 rtciic_write_byte(void *arg, uint8_t v, int flags) 201 { 202 struct rtciic_softc *sc = arg; 203 204 return i2c_bitbang_write_byte(sc, v, flags, &sc->sc_bops); 205 } 206 207 208 static void 209 rtciic_set_dir(void *arg, uint32_t bits) 210 { 211 struct rtciic_softc *sc = arg; 212 uint16_t reg; 213 214 DPRINTF(("%s: set dir %s\n", 215 device_xname(sc->sc_dev), (bits & RTCIIC_RW) ? "READ" : "WRITE")); 216 217 if (sc->sc_rw != (bits & RTCIIC_RW)) { 218 reg = RTCIIC_READ(sc); 219 reg &= ~RTCIIC_RW; 220 reg |= bits; 221 RTCIIC_WRITE(sc, reg); 222 delay(30); 223 sc->sc_rw = bits & RTCIIC_RW; 224 } 225 } 226 227 static void 228 rtciic_set_bits(void *arg, uint32_t bits) 229 { 230 struct rtciic_softc *sc = arg; 231 232 DPRINTF(("%s: %s\n", 233 device_xname(sc->sc_dev), 234 (bits == (RTCIIC_SDAW | RTCIIC_SCL)) ? "set SDA/SCL" : 235 ((bits == RTCIIC_SDAW) ? "set SDA" : 236 ((bits == RTCIIC_SCL) ? "set SCL" : "reset")))); 237 238 if (sc->sc_rw & RTCIIC_RW) { 239 bits &= RTCIIC_SCL; 240 bits |= RTCIIC_RW; 241 } 242 RTCIIC_WRITE(sc, bits); 243 delay(40); 244 } 245 246 static uint32_t 247 rtciic_read_bits(void *arg) 248 { 249 struct rtciic_softc *sc = arg; 250 uint8_t rv, v; 251 252 v = RTCIIC_READ(sc); 253 rv = v & RTCIIC_SCL; 254 if (v & RTCIIC_SDAR) 255 rv |= RTCIIC_SDAW; 256 257 DPRINTF(("%s: read %s\n", 258 device_xname(sc->sc_dev), 259 (rv == (RTCIIC_SDAW | RTCIIC_SCL)) ? "SDA/SCL" : 260 ((rv == RTCIIC_SDAW) ? "SDA" : 261 ((rv == RTCIIC_SCL) ? "SCL" : "no")))); 262 263 return (uint32_t)rv; 264 } 265