xref: /netbsd-src/sys/arch/arm/iomd/iomdiic.c (revision b6c8c37ff61c4264acd6dfb4f74f0eac3cfbf53f)
1*b6c8c37fSriastradh /*	$NetBSD: iomdiic.c,v 1.12 2022/03/28 12:38:57 riastradh Exp $	*/
22c0d381bSthorpej 
32c0d381bSthorpej /*
42c0d381bSthorpej  * Copyright (c) 2003 Wasabi Systems, Inc.
52c0d381bSthorpej  * All rights reserved.
62c0d381bSthorpej  *
72c0d381bSthorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
82c0d381bSthorpej  *
92c0d381bSthorpej  * Redistribution and use in source and binary forms, with or without
102c0d381bSthorpej  * modification, are permitted provided that the following conditions
112c0d381bSthorpej  * are met:
122c0d381bSthorpej  * 1. Redistributions of source code must retain the above copyright
132c0d381bSthorpej  *    notice, this list of conditions and the following disclaimer.
142c0d381bSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
152c0d381bSthorpej  *    notice, this list of conditions and the following disclaimer in the
162c0d381bSthorpej  *    documentation and/or other materials provided with the distribution.
172c0d381bSthorpej  * 3. All advertising materials mentioning features or use of this software
182c0d381bSthorpej  *    must display the following acknowledgement:
192c0d381bSthorpej  *      This product includes software developed for the NetBSD Project by
202c0d381bSthorpej  *      Wasabi Systems, Inc.
212c0d381bSthorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
222c0d381bSthorpej  *    or promote products derived from this software without specific prior
232c0d381bSthorpej  *    written permission.
242c0d381bSthorpej  *
252c0d381bSthorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
262c0d381bSthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
272c0d381bSthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
282c0d381bSthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
292c0d381bSthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
302c0d381bSthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
312c0d381bSthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
322c0d381bSthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
332c0d381bSthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
342c0d381bSthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
352c0d381bSthorpej  * POSSIBILITY OF SUCH DAMAGE.
362c0d381bSthorpej  */
372c0d381bSthorpej 
382c0d381bSthorpej #include <sys/param.h>
392c0d381bSthorpej #include <sys/device.h>
40*b6c8c37fSriastradh #include <sys/device_impl.h>	/* XXX autoconf abuse */
412c0d381bSthorpej #include <sys/kernel.h>
422c0d381bSthorpej #include <sys/systm.h>
43065b6ba2Sad #include <sys/mutex.h>
44065b6ba2Sad #include <sys/bus.h>
45065b6ba2Sad #include <sys/cpu.h>
462c0d381bSthorpej 
472c0d381bSthorpej #include <arm/iomd/iomdreg.h>
482c0d381bSthorpej #include <arm/iomd/iomdvar.h>
492c0d381bSthorpej 
502c0d381bSthorpej #include <dev/i2c/i2cvar.h>
512c0d381bSthorpej #include <dev/i2c/i2c_bitbang.h>
522c0d381bSthorpej 
532c0d381bSthorpej #include <arm/iomd/iomdiicvar.h>
542c0d381bSthorpej 
552c0d381bSthorpej struct iomdiic_softc {
56e0f866efSskrll 	device_t sc_dev;
572c0d381bSthorpej 	bus_space_tag_t sc_st;
582c0d381bSthorpej 	bus_space_handle_t sc_sh;
592c0d381bSthorpej 
602c0d381bSthorpej 	struct i2c_controller sc_i2c;
612c0d381bSthorpej 
622c0d381bSthorpej 	/*
632c0d381bSthorpej 	 * The SDA pin is open-drain, so we make it an input by
642c0d381bSthorpej 	 * writing a 1 to it.
652c0d381bSthorpej 	 */
662c0d381bSthorpej 	uint8_t sc_iomd_iocr;
672c0d381bSthorpej };
682c0d381bSthorpej 
692c0d381bSthorpej static int	iomdiic_send_start(void *, int);
702c0d381bSthorpej static int	iomdiic_send_stop(void *, int);
712c0d381bSthorpej static int	iomdiic_initiate_xfer(void *, i2c_addr_t, int);
722c0d381bSthorpej static int	iomdiic_read_byte(void *, uint8_t *, int);
732c0d381bSthorpej static int	iomdiic_write_byte(void *, uint8_t, int);
742c0d381bSthorpej 
755f1c88d7Sperry #define	IOMDIIC_READ	 *(volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR))
765f1c88d7Sperry #define	IOMDIIC_WRITE(x) *(volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR)) = (x)
772c0d381bSthorpej 
782c0d381bSthorpej #define	IOMD_IOCR_SDA		0x01
792c0d381bSthorpej #define	IOMD_IOCR_SCL		0x02
802c0d381bSthorpej 
812c0d381bSthorpej static void
iomdiic_bb_set_bits(void * cookie,uint32_t bits)822c0d381bSthorpej iomdiic_bb_set_bits(void *cookie, uint32_t bits)
832c0d381bSthorpej {
842c0d381bSthorpej 	struct iomdiic_softc *sc = cookie;
852c0d381bSthorpej 
862c0d381bSthorpej 	IOMDIIC_WRITE((IOMDIIC_READ & ~(IOMD_IOCR_SDA|IOMD_IOCR_SCL)) |
872c0d381bSthorpej 	    sc->sc_iomd_iocr | bits);
882c0d381bSthorpej }
892c0d381bSthorpej 
902c0d381bSthorpej static void
iomdiic_bb_set_dir(void * cookie,uint32_t bits)912c0d381bSthorpej iomdiic_bb_set_dir(void *cookie, uint32_t bits)
922c0d381bSthorpej {
932c0d381bSthorpej 	struct iomdiic_softc *sc = cookie;
942c0d381bSthorpej 
952c0d381bSthorpej 	sc->sc_iomd_iocr = bits;
962c0d381bSthorpej }
972c0d381bSthorpej 
982c0d381bSthorpej static uint32_t
iomdiic_bb_read_bits(void * cookie)992c0d381bSthorpej iomdiic_bb_read_bits(void *cookie)
1002c0d381bSthorpej {
1012c0d381bSthorpej 
1022c0d381bSthorpej 	return (IOMDIIC_READ);
1032c0d381bSthorpej }
1042c0d381bSthorpej 
1052c0d381bSthorpej static const struct i2c_bitbang_ops iomdiic_bbops = {
1062c0d381bSthorpej 	iomdiic_bb_set_bits,
1072c0d381bSthorpej 	iomdiic_bb_set_dir,
1082c0d381bSthorpej 	iomdiic_bb_read_bits,
1092c0d381bSthorpej 	{
1102c0d381bSthorpej 		IOMD_IOCR_SDA,		/* SDA */
1112c0d381bSthorpej 		IOMD_IOCR_SCL,		/* SCL */
1122c0d381bSthorpej 		0,			/* SDA is output */
1132c0d381bSthorpej 		IOMD_IOCR_SDA,		/* SDA is input */
1142c0d381bSthorpej 	}
1152c0d381bSthorpej };
1162c0d381bSthorpej 
1172c0d381bSthorpej static int
iomdiic_match(device_t parent,cfdata_t cf,void * aux)118e0f866efSskrll iomdiic_match(device_t parent, cfdata_t cf, void *aux)
1192c0d381bSthorpej {
1204e923603Sreinoud 	struct iic_attach_args *ia = aux;
1212c0d381bSthorpej 
1224e923603Sreinoud 	if (strcmp(ia->ia_name, "iic") == 0) return 1;
1234e923603Sreinoud 	return 0;
1242c0d381bSthorpej }
1252c0d381bSthorpej 
1262c0d381bSthorpej static void
iomdiic_attach(device_t parent,device_t self,void * aux)127e0f866efSskrll iomdiic_attach(device_t parent, device_t self, void *aux)
1282c0d381bSthorpej {
129e0f866efSskrll 	struct iomdiic_softc *sc = device_private(self);
1302c0d381bSthorpej 	struct i2cbus_attach_args iba;
1312c0d381bSthorpej 
132e0f866efSskrll 	aprint_normal("\n");
133e0f866efSskrll 
134e0f866efSskrll 	sc->sc_dev = self;
1352c0d381bSthorpej 
136601e1783Sthorpej 	iic_tag_init(&sc->sc_i2c);
1372c0d381bSthorpej 	sc->sc_i2c.ic_cookie = sc;
1382c0d381bSthorpej 	sc->sc_i2c.ic_send_start = iomdiic_send_start;
1392c0d381bSthorpej 	sc->sc_i2c.ic_send_stop = iomdiic_send_stop;
1402c0d381bSthorpej 	sc->sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
1412c0d381bSthorpej 	sc->sc_i2c.ic_read_byte = iomdiic_read_byte;
1422c0d381bSthorpej 	sc->sc_i2c.ic_write_byte = iomdiic_write_byte;
1432c0d381bSthorpej 
1442f02870fSchs 	memset(&iba, 0, sizeof(iba));
1452c0d381bSthorpej 	iba.iba_tag = &sc->sc_i2c;
146c7fb772bSthorpej 	config_found(sc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
1472c0d381bSthorpej }
1482c0d381bSthorpej 
149e0f866efSskrll CFATTACH_DECL_NEW(iomdiic, sizeof(struct iomdiic_softc),
1502c0d381bSthorpej     iomdiic_match, iomdiic_attach, NULL, NULL);
1512c0d381bSthorpej 
1522c0d381bSthorpej i2c_tag_t
iomdiic_bootstrap_cookie(void)1532c0d381bSthorpej iomdiic_bootstrap_cookie(void)
1542c0d381bSthorpej {
1552c0d381bSthorpej 	static struct iomdiic_softc sc;
156e0f866efSskrll 	static struct device dev;
1572c0d381bSthorpej 
1582c0d381bSthorpej 	/* XXX Yuck. */
159e0f866efSskrll 	strcpy(dev.dv_xname, "iomdiicboot");
1602c0d381bSthorpej 
161e0f866efSskrll 	sc.sc_dev = &dev;
162601e1783Sthorpej 
163601e1783Sthorpej 	iic_tag_init(&sc.sc_i2c);
1642c0d381bSthorpej 	sc.sc_i2c.ic_cookie = &sc;
1652c0d381bSthorpej 	sc.sc_i2c.ic_send_start = iomdiic_send_start;
1662c0d381bSthorpej 	sc.sc_i2c.ic_send_stop = iomdiic_send_stop;
1672c0d381bSthorpej 	sc.sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
1682c0d381bSthorpej 	sc.sc_i2c.ic_read_byte = iomdiic_read_byte;
1692c0d381bSthorpej 	sc.sc_i2c.ic_write_byte = iomdiic_write_byte;
1702c0d381bSthorpej 
1712c0d381bSthorpej 	return ((void *) &sc.sc_i2c);
1722c0d381bSthorpej }
1732c0d381bSthorpej 
1742c0d381bSthorpej static int
iomdiic_send_start(void * cookie,int flags)1752c0d381bSthorpej iomdiic_send_start(void *cookie, int flags)
1762c0d381bSthorpej {
1772c0d381bSthorpej 
1782c0d381bSthorpej 	return (i2c_bitbang_send_start(cookie, flags, &iomdiic_bbops));
1792c0d381bSthorpej }
1802c0d381bSthorpej 
1812c0d381bSthorpej static int
iomdiic_send_stop(void * cookie,int flags)1822c0d381bSthorpej iomdiic_send_stop(void *cookie, int flags)
1832c0d381bSthorpej {
1842c0d381bSthorpej 
1852c0d381bSthorpej 	return (i2c_bitbang_send_stop(cookie, flags, &iomdiic_bbops));
1862c0d381bSthorpej }
1872c0d381bSthorpej 
1882c0d381bSthorpej static int
iomdiic_initiate_xfer(void * cookie,i2c_addr_t addr,int flags)1892c0d381bSthorpej iomdiic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
1902c0d381bSthorpej {
1912c0d381bSthorpej 
1922c0d381bSthorpej 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags, &iomdiic_bbops));
1932c0d381bSthorpej }
1942c0d381bSthorpej 
1952c0d381bSthorpej static int
iomdiic_read_byte(void * cookie,uint8_t * bytep,int flags)1962c0d381bSthorpej iomdiic_read_byte(void *cookie, uint8_t *bytep, int flags)
1972c0d381bSthorpej {
1982c0d381bSthorpej 
1992c0d381bSthorpej 	return (i2c_bitbang_read_byte(cookie, bytep, flags, &iomdiic_bbops));
2002c0d381bSthorpej }
2012c0d381bSthorpej 
2022c0d381bSthorpej static int
iomdiic_write_byte(void * cookie,uint8_t byte,int flags)2032c0d381bSthorpej iomdiic_write_byte(void *cookie, uint8_t byte, int flags)
2042c0d381bSthorpej {
2052c0d381bSthorpej 
2062c0d381bSthorpej 	return (i2c_bitbang_write_byte(cookie, byte, flags, &iomdiic_bbops));
2072c0d381bSthorpej }
208