1 1.13 thorpej /* $NetBSD: gpiic_opb.c,v 1.13 2025/09/15 13:23:02 thorpej Exp $ */ 2 1.1 scw 3 1.1 scw /* 4 1.1 scw * Copyright 2002, 2003 Wasabi Systems, Inc. 5 1.1 scw * All rights reserved. 6 1.1 scw * 7 1.1 scw * Written by Steve C. Woodford for Wasabi Systems, Inc. 8 1.1 scw * 9 1.1 scw * Redistribution and use in source and binary forms, with or without 10 1.1 scw * modification, are permitted provided that the following conditions 11 1.1 scw * are met: 12 1.1 scw * 1. Redistributions of source code must retain the above copyright 13 1.1 scw * notice, this list of conditions and the following disclaimer. 14 1.1 scw * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 scw * notice, this list of conditions and the following disclaimer in the 16 1.1 scw * documentation and/or other materials provided with the distribution. 17 1.1 scw * 3. All advertising materials mentioning features or use of this software 18 1.1 scw * must display the following acknowledgement: 19 1.1 scw * This product includes software developed for the NetBSD Project by 20 1.1 scw * Wasabi Systems, Inc. 21 1.1 scw * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 1.1 scw * or promote products derived from this software without specific prior 23 1.1 scw * written permission. 24 1.1 scw * 25 1.1 scw * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 1.1 scw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 1.1 scw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 1.1 scw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 1.1 scw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 1.1 scw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 1.1 scw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 1.1 scw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 1.1 scw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 1.1 scw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 1.1 scw * POSSIBILITY OF SUCH DAMAGE. 36 1.1 scw */ 37 1.1 scw 38 1.1 scw #include "locators.h" 39 1.1 scw 40 1.1 scw #include <sys/param.h> 41 1.1 scw #include <sys/systm.h> 42 1.1 scw #include <sys/device.h> 43 1.1 scw #include <sys/errno.h> 44 1.5 ad #include <sys/mutex.h> 45 1.5 ad #include <sys/cpu.h> 46 1.1 scw 47 1.2 scw #include <dev/i2c/i2cvar.h> 48 1.2 scw #include <dev/i2c/i2c_bitbang.h> 49 1.1 scw 50 1.9 matt #include <powerpc/ibm4xx/cpu.h> 51 1.1 scw #include <powerpc/ibm4xx/dev/opbvar.h> 52 1.1 scw #include <powerpc/ibm4xx/dev/gpiicreg.h> 53 1.1 scw 54 1.1 scw struct gpiic_softc { 55 1.8 matt device_t sc_dev; 56 1.1 scw bus_space_tag_t sc_bust; 57 1.1 scw bus_space_handle_t sc_bush; 58 1.1 scw uint8_t sc_txen; 59 1.6 tsutsui uint8_t sc_tx; 60 1.1 scw struct i2c_controller sc_i2c; 61 1.1 scw struct i2c_bitbang_ops sc_bops; 62 1.1 scw }; 63 1.1 scw 64 1.8 matt static int gpiic_match(device_t, cfdata_t, void *); 65 1.8 matt static void gpiic_attach(device_t, device_t, void *); 66 1.1 scw 67 1.8 matt CFATTACH_DECL_NEW(gpiic, sizeof(struct gpiic_softc), 68 1.1 scw gpiic_match, gpiic_attach, NULL, NULL); 69 1.1 scw 70 1.1 scw static int gpiic_send_start(void *, int); 71 1.1 scw static int gpiic_send_stop(void *, int); 72 1.1 scw static int gpiic_initiate_xfer(void *, i2c_addr_t, int); 73 1.1 scw static int gpiic_read_byte(void *, uint8_t *, int); 74 1.1 scw static int gpiic_write_byte(void *, uint8_t, int); 75 1.1 scw static void gpiic_set_dir(void *, uint32_t); 76 1.1 scw static void gpiic_set_bits(void *, uint32_t); 77 1.1 scw static uint32_t gpiic_read_bits(void *); 78 1.1 scw 79 1.1 scw static int 80 1.8 matt gpiic_match(device_t parent, cfdata_t cf, void *args) 81 1.1 scw { 82 1.8 matt struct opb_attach_args * const oaa = args; 83 1.1 scw 84 1.1 scw if (strcmp(oaa->opb_name, cf->cf_name) != 0) 85 1.1 scw return 0; 86 1.1 scw 87 1.1 scw return (1); 88 1.1 scw } 89 1.1 scw 90 1.1 scw static void 91 1.8 matt gpiic_attach(device_t parent, device_t self, void *args) 92 1.1 scw { 93 1.8 matt struct gpiic_softc * const sc = device_private(self); 94 1.8 matt struct opb_attach_args * const oaa = args; 95 1.1 scw 96 1.1 scw aprint_naive(": IIC controller\n"); 97 1.1 scw aprint_normal(": On-Chip IIC controller\n"); 98 1.1 scw 99 1.8 matt sc->sc_dev = self; 100 1.1 scw sc->sc_bust = oaa->opb_bt; 101 1.1 scw 102 1.1 scw bus_space_map(sc->sc_bust, oaa->opb_addr, IIC_NREG, 0, &sc->sc_bush); 103 1.1 scw 104 1.1 scw sc->sc_txen = 0; 105 1.6 tsutsui sc->sc_tx = IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC; 106 1.10 thorpej iic_tag_init(&sc->sc_i2c); 107 1.1 scw sc->sc_i2c.ic_cookie = sc; 108 1.1 scw sc->sc_i2c.ic_send_start = gpiic_send_start; 109 1.1 scw sc->sc_i2c.ic_send_stop = gpiic_send_stop; 110 1.1 scw sc->sc_i2c.ic_initiate_xfer = gpiic_initiate_xfer; 111 1.1 scw sc->sc_i2c.ic_read_byte = gpiic_read_byte; 112 1.1 scw sc->sc_i2c.ic_write_byte = gpiic_write_byte; 113 1.1 scw 114 1.1 scw sc->sc_bops.ibo_set_dir = gpiic_set_dir; 115 1.1 scw sc->sc_bops.ibo_set_bits = gpiic_set_bits; 116 1.1 scw sc->sc_bops.ibo_read_bits = gpiic_read_bits; 117 1.1 scw sc->sc_bops.ibo_bits[I2C_BIT_SDA] = IIC_DIRECTCNTL_SDAC; 118 1.1 scw sc->sc_bops.ibo_bits[I2C_BIT_SCL] = IIC_DIRECTCNTL_SCC; 119 1.1 scw sc->sc_bops.ibo_bits[I2C_BIT_OUTPUT] = 1; 120 1.1 scw sc->sc_bops.ibo_bits[I2C_BIT_INPUT] = 0; 121 1.1 scw 122 1.1 scw /* 123 1.1 scw * Put the controller into Soft Reset. This allows us to 124 1.1 scw * manually bit-bang the I2C clock/data lines. 125 1.1 scw */ 126 1.1 scw bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_XTCNTLSS, 127 1.1 scw IIC_XTCNTLSS_SRST); 128 1.1 scw delay(10); 129 1.1 scw bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL, 130 1.1 scw IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC); 131 1.1 scw 132 1.13 thorpej iicbus_attach(self, &sc->sc_i2c); 133 1.1 scw } 134 1.1 scw 135 1.1 scw static int 136 1.1 scw gpiic_send_start(void *arg, int flags) 137 1.1 scw { 138 1.8 matt struct gpiic_softc * const sc = arg; 139 1.1 scw 140 1.1 scw return (i2c_bitbang_send_start(sc, flags, &sc->sc_bops)); 141 1.1 scw } 142 1.1 scw 143 1.1 scw static int 144 1.1 scw gpiic_send_stop(void *arg, int flags) 145 1.1 scw { 146 1.8 matt struct gpiic_softc * const sc = arg; 147 1.1 scw 148 1.1 scw return (i2c_bitbang_send_stop(sc, flags, &sc->sc_bops)); 149 1.1 scw } 150 1.1 scw 151 1.1 scw static int 152 1.1 scw gpiic_initiate_xfer(void *arg, i2c_addr_t addr, int flags) 153 1.1 scw { 154 1.8 matt struct gpiic_softc * const sc = arg; 155 1.1 scw 156 1.1 scw return (i2c_bitbang_initiate_xfer(sc, addr, flags, &sc->sc_bops)); 157 1.1 scw } 158 1.1 scw 159 1.1 scw static int 160 1.1 scw gpiic_read_byte(void *arg, uint8_t *vp, int flags) 161 1.1 scw { 162 1.8 matt struct gpiic_softc * const sc = arg; 163 1.1 scw 164 1.1 scw return (i2c_bitbang_read_byte(sc, vp, flags, &sc->sc_bops)); 165 1.1 scw } 166 1.1 scw 167 1.1 scw static int 168 1.1 scw gpiic_write_byte(void *arg, uint8_t v, int flags) 169 1.1 scw { 170 1.8 matt struct gpiic_softc * const sc = arg; 171 1.1 scw 172 1.1 scw return (i2c_bitbang_write_byte(sc, v, flags, &sc->sc_bops)); 173 1.1 scw } 174 1.1 scw 175 1.1 scw static void 176 1.1 scw gpiic_set_dir(void *arg, uint32_t bits) 177 1.1 scw { 178 1.8 matt struct gpiic_softc * const sc = arg; 179 1.6 tsutsui uint8_t tx, txen; 180 1.1 scw 181 1.6 tsutsui txen = (uint8_t)bits; 182 1.6 tsutsui if (sc->sc_txen == txen) 183 1.6 tsutsui return; 184 1.6 tsutsui 185 1.6 tsutsui sc->sc_txen = txen; 186 1.6 tsutsui 187 1.6 tsutsui tx = sc->sc_tx; 188 1.6 tsutsui if (sc->sc_txen == 0) 189 1.6 tsutsui tx |= IIC_DIRECTCNTL_SDAC; 190 1.6 tsutsui bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL, tx); 191 1.1 scw } 192 1.1 scw 193 1.1 scw static void 194 1.1 scw gpiic_set_bits(void *arg, uint32_t bits) 195 1.1 scw { 196 1.8 matt struct gpiic_softc * const sc = arg; 197 1.1 scw 198 1.6 tsutsui sc->sc_tx = (uint8_t)bits; 199 1.6 tsutsui if (sc->sc_txen == 0) 200 1.1 scw bits |= IIC_DIRECTCNTL_SDAC; 201 1.1 scw 202 1.1 scw bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL, bits); 203 1.1 scw } 204 1.1 scw 205 1.1 scw static uint32_t 206 1.1 scw gpiic_read_bits(void *arg) 207 1.1 scw { 208 1.8 matt struct gpiic_softc * const sc = arg; 209 1.1 scw uint8_t rv; 210 1.1 scw 211 1.1 scw rv = bus_space_read_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL) << 2; 212 1.1 scw rv &= (IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC); 213 1.1 scw 214 1.1 scw return ((uint32_t)rv); 215 1.1 scw } 216