1*c7fb772bSthorpej /* $NetBSD: gpiic_opb.c,v 1.12 2021/08/07 16:19:03 thorpej Exp $ */
22ba76423Sscw
32ba76423Sscw /*
42ba76423Sscw * Copyright 2002, 2003 Wasabi Systems, Inc.
52ba76423Sscw * All rights reserved.
62ba76423Sscw *
72ba76423Sscw * Written by Steve C. Woodford for Wasabi Systems, Inc.
82ba76423Sscw *
92ba76423Sscw * Redistribution and use in source and binary forms, with or without
102ba76423Sscw * modification, are permitted provided that the following conditions
112ba76423Sscw * are met:
122ba76423Sscw * 1. Redistributions of source code must retain the above copyright
132ba76423Sscw * notice, this list of conditions and the following disclaimer.
142ba76423Sscw * 2. Redistributions in binary form must reproduce the above copyright
152ba76423Sscw * notice, this list of conditions and the following disclaimer in the
162ba76423Sscw * documentation and/or other materials provided with the distribution.
172ba76423Sscw * 3. All advertising materials mentioning features or use of this software
182ba76423Sscw * must display the following acknowledgement:
192ba76423Sscw * This product includes software developed for the NetBSD Project by
202ba76423Sscw * Wasabi Systems, Inc.
212ba76423Sscw * 4. The name of Wasabi Systems, Inc. may not be used to endorse
222ba76423Sscw * or promote products derived from this software without specific prior
232ba76423Sscw * written permission.
242ba76423Sscw *
252ba76423Sscw * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
262ba76423Sscw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
272ba76423Sscw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
282ba76423Sscw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
292ba76423Sscw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
302ba76423Sscw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
312ba76423Sscw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
322ba76423Sscw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
332ba76423Sscw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
342ba76423Sscw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
352ba76423Sscw * POSSIBILITY OF SUCH DAMAGE.
362ba76423Sscw */
372ba76423Sscw
382ba76423Sscw #include "locators.h"
392ba76423Sscw
402ba76423Sscw #include <sys/param.h>
412ba76423Sscw #include <sys/systm.h>
422ba76423Sscw #include <sys/device.h>
432ba76423Sscw #include <sys/errno.h>
44065b6ba2Sad #include <sys/mutex.h>
45065b6ba2Sad #include <sys/cpu.h>
462ba76423Sscw
474eea84b7Sscw #include <dev/i2c/i2cvar.h>
484eea84b7Sscw #include <dev/i2c/i2c_bitbang.h>
492ba76423Sscw
501fd2c684Smatt #include <powerpc/ibm4xx/cpu.h>
512ba76423Sscw #include <powerpc/ibm4xx/dev/opbvar.h>
522ba76423Sscw #include <powerpc/ibm4xx/dev/gpiicreg.h>
532ba76423Sscw
542ba76423Sscw struct gpiic_softc {
55036ca983Smatt device_t sc_dev;
562ba76423Sscw bus_space_tag_t sc_bust;
572ba76423Sscw bus_space_handle_t sc_bush;
582ba76423Sscw uint8_t sc_txen;
59ce1887dcStsutsui uint8_t sc_tx;
602ba76423Sscw struct i2c_controller sc_i2c;
612ba76423Sscw struct i2c_bitbang_ops sc_bops;
622ba76423Sscw };
632ba76423Sscw
64036ca983Smatt static int gpiic_match(device_t, cfdata_t, void *);
65036ca983Smatt static void gpiic_attach(device_t, device_t, void *);
662ba76423Sscw
67036ca983Smatt CFATTACH_DECL_NEW(gpiic, sizeof(struct gpiic_softc),
682ba76423Sscw gpiic_match, gpiic_attach, NULL, NULL);
692ba76423Sscw
702ba76423Sscw static int gpiic_send_start(void *, int);
712ba76423Sscw static int gpiic_send_stop(void *, int);
722ba76423Sscw static int gpiic_initiate_xfer(void *, i2c_addr_t, int);
732ba76423Sscw static int gpiic_read_byte(void *, uint8_t *, int);
742ba76423Sscw static int gpiic_write_byte(void *, uint8_t, int);
752ba76423Sscw static void gpiic_set_dir(void *, uint32_t);
762ba76423Sscw static void gpiic_set_bits(void *, uint32_t);
772ba76423Sscw static uint32_t gpiic_read_bits(void *);
782ba76423Sscw
792ba76423Sscw static int
gpiic_match(device_t parent,cfdata_t cf,void * args)80036ca983Smatt gpiic_match(device_t parent, cfdata_t cf, void *args)
812ba76423Sscw {
82036ca983Smatt struct opb_attach_args * const oaa = args;
832ba76423Sscw
842ba76423Sscw if (strcmp(oaa->opb_name, cf->cf_name) != 0)
852ba76423Sscw return 0;
862ba76423Sscw
872ba76423Sscw return (1);
882ba76423Sscw }
892ba76423Sscw
902ba76423Sscw static void
gpiic_attach(device_t parent,device_t self,void * args)91036ca983Smatt gpiic_attach(device_t parent, device_t self, void *args)
922ba76423Sscw {
93036ca983Smatt struct gpiic_softc * const sc = device_private(self);
94036ca983Smatt struct opb_attach_args * const oaa = args;
952ba76423Sscw struct i2cbus_attach_args iba;
962ba76423Sscw
972ba76423Sscw aprint_naive(": IIC controller\n");
982ba76423Sscw aprint_normal(": On-Chip IIC controller\n");
992ba76423Sscw
100036ca983Smatt sc->sc_dev = self;
1012ba76423Sscw sc->sc_bust = oaa->opb_bt;
1022ba76423Sscw
1032ba76423Sscw bus_space_map(sc->sc_bust, oaa->opb_addr, IIC_NREG, 0, &sc->sc_bush);
1042ba76423Sscw
1052ba76423Sscw sc->sc_txen = 0;
106ce1887dcStsutsui sc->sc_tx = IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC;
107601e1783Sthorpej iic_tag_init(&sc->sc_i2c);
1082ba76423Sscw sc->sc_i2c.ic_cookie = sc;
1092ba76423Sscw sc->sc_i2c.ic_send_start = gpiic_send_start;
1102ba76423Sscw sc->sc_i2c.ic_send_stop = gpiic_send_stop;
1112ba76423Sscw sc->sc_i2c.ic_initiate_xfer = gpiic_initiate_xfer;
1122ba76423Sscw sc->sc_i2c.ic_read_byte = gpiic_read_byte;
1132ba76423Sscw sc->sc_i2c.ic_write_byte = gpiic_write_byte;
1142ba76423Sscw
1152ba76423Sscw sc->sc_bops.ibo_set_dir = gpiic_set_dir;
1162ba76423Sscw sc->sc_bops.ibo_set_bits = gpiic_set_bits;
1172ba76423Sscw sc->sc_bops.ibo_read_bits = gpiic_read_bits;
1182ba76423Sscw sc->sc_bops.ibo_bits[I2C_BIT_SDA] = IIC_DIRECTCNTL_SDAC;
1192ba76423Sscw sc->sc_bops.ibo_bits[I2C_BIT_SCL] = IIC_DIRECTCNTL_SCC;
1202ba76423Sscw sc->sc_bops.ibo_bits[I2C_BIT_OUTPUT] = 1;
1212ba76423Sscw sc->sc_bops.ibo_bits[I2C_BIT_INPUT] = 0;
1222ba76423Sscw
1232ba76423Sscw /*
1242ba76423Sscw * Put the controller into Soft Reset. This allows us to
1252ba76423Sscw * manually bit-bang the I2C clock/data lines.
1262ba76423Sscw */
1272ba76423Sscw bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_XTCNTLSS,
1282ba76423Sscw IIC_XTCNTLSS_SRST);
1292ba76423Sscw delay(10);
1302ba76423Sscw bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL,
1312ba76423Sscw IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC);
1322ba76423Sscw
1334ae6ef62Skiyohara memset(&iba, 0, sizeof(iba));
1342ba76423Sscw iba.iba_tag = &sc->sc_i2c;
135*c7fb772bSthorpej config_found(self, &iba, iicbus_print, CFARGS_NONE);
1362ba76423Sscw }
1372ba76423Sscw
1382ba76423Sscw static int
gpiic_send_start(void * arg,int flags)1392ba76423Sscw gpiic_send_start(void *arg, int flags)
1402ba76423Sscw {
141036ca983Smatt struct gpiic_softc * const sc = arg;
1422ba76423Sscw
1432ba76423Sscw return (i2c_bitbang_send_start(sc, flags, &sc->sc_bops));
1442ba76423Sscw }
1452ba76423Sscw
1462ba76423Sscw static int
gpiic_send_stop(void * arg,int flags)1472ba76423Sscw gpiic_send_stop(void *arg, int flags)
1482ba76423Sscw {
149036ca983Smatt struct gpiic_softc * const sc = arg;
1502ba76423Sscw
1512ba76423Sscw return (i2c_bitbang_send_stop(sc, flags, &sc->sc_bops));
1522ba76423Sscw }
1532ba76423Sscw
1542ba76423Sscw static int
gpiic_initiate_xfer(void * arg,i2c_addr_t addr,int flags)1552ba76423Sscw gpiic_initiate_xfer(void *arg, i2c_addr_t addr, int flags)
1562ba76423Sscw {
157036ca983Smatt struct gpiic_softc * const sc = arg;
1582ba76423Sscw
1592ba76423Sscw return (i2c_bitbang_initiate_xfer(sc, addr, flags, &sc->sc_bops));
1602ba76423Sscw }
1612ba76423Sscw
1622ba76423Sscw static int
gpiic_read_byte(void * arg,uint8_t * vp,int flags)1632ba76423Sscw gpiic_read_byte(void *arg, uint8_t *vp, int flags)
1642ba76423Sscw {
165036ca983Smatt struct gpiic_softc * const sc = arg;
1662ba76423Sscw
1672ba76423Sscw return (i2c_bitbang_read_byte(sc, vp, flags, &sc->sc_bops));
1682ba76423Sscw }
1692ba76423Sscw
1702ba76423Sscw static int
gpiic_write_byte(void * arg,uint8_t v,int flags)1712ba76423Sscw gpiic_write_byte(void *arg, uint8_t v, int flags)
1722ba76423Sscw {
173036ca983Smatt struct gpiic_softc * const sc = arg;
1742ba76423Sscw
1752ba76423Sscw return (i2c_bitbang_write_byte(sc, v, flags, &sc->sc_bops));
1762ba76423Sscw }
1772ba76423Sscw
1782ba76423Sscw static void
gpiic_set_dir(void * arg,uint32_t bits)1792ba76423Sscw gpiic_set_dir(void *arg, uint32_t bits)
1802ba76423Sscw {
181036ca983Smatt struct gpiic_softc * const sc = arg;
182ce1887dcStsutsui uint8_t tx, txen;
1832ba76423Sscw
184ce1887dcStsutsui txen = (uint8_t)bits;
185ce1887dcStsutsui if (sc->sc_txen == txen)
186ce1887dcStsutsui return;
187ce1887dcStsutsui
188ce1887dcStsutsui sc->sc_txen = txen;
189ce1887dcStsutsui
190ce1887dcStsutsui tx = sc->sc_tx;
191ce1887dcStsutsui if (sc->sc_txen == 0)
192ce1887dcStsutsui tx |= IIC_DIRECTCNTL_SDAC;
193ce1887dcStsutsui bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL, tx);
1942ba76423Sscw }
1952ba76423Sscw
1962ba76423Sscw static void
gpiic_set_bits(void * arg,uint32_t bits)1972ba76423Sscw gpiic_set_bits(void *arg, uint32_t bits)
1982ba76423Sscw {
199036ca983Smatt struct gpiic_softc * const sc = arg;
2002ba76423Sscw
201ce1887dcStsutsui sc->sc_tx = (uint8_t)bits;
202ce1887dcStsutsui if (sc->sc_txen == 0)
2032ba76423Sscw bits |= IIC_DIRECTCNTL_SDAC;
2042ba76423Sscw
2052ba76423Sscw bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL, bits);
2062ba76423Sscw }
2072ba76423Sscw
2082ba76423Sscw static uint32_t
gpiic_read_bits(void * arg)2092ba76423Sscw gpiic_read_bits(void *arg)
2102ba76423Sscw {
211036ca983Smatt struct gpiic_softc * const sc = arg;
2122ba76423Sscw uint8_t rv;
2132ba76423Sscw
2142ba76423Sscw rv = bus_space_read_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL) << 2;
2152ba76423Sscw rv &= (IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC);
2162ba76423Sscw
2172ba76423Sscw return ((uint32_t)rv);
2182ba76423Sscw }
219