11553Srgrimes/*	$NetBSD: iomdiic.c,v 1.12 2022/03/28 12:38:57 riastradh Exp $	*/
250479Speter
31553Srgrimes/*
4160753Syar * Copyright (c) 2003 Wasabi Systems, Inc.
5160753Syar * All rights reserved.
61553Srgrimes *
774816Sru * Written by Jason R. Thorpe for Wasabi Systems, Inc.
81863Swollman *
980029Sobrien * Redistribution and use in source and binary forms, with or without
101553Srgrimes * modification, are permitted provided that the following conditions
11154530Sdelphij * are met:
12157819Sdwmalone * 1. Redistributions of source code must retain the above copyright
1383112Sdd *    notice, this list of conditions and the following disclaimer.
1419618Sjulian * 2. Redistributions in binary form must reproduce the above copyright
15160753Syar *    notice, this list of conditions and the following disclaimer in the
16160753Syar *    documentation and/or other materials provided with the distribution.
17160753Syar * 3. All advertising materials mentioning features or use of this software
18160753Syar *    must display the following acknowledgement:
1980029Sobrien *      This product includes software developed for the NetBSD Project by
2080029Sobrien *      Wasabi Systems, Inc.
2180029Sobrien * 4. The name of Wasabi Systems, Inc. may not be used to endorse
2257084Sluigi *    or promote products derived from this software without specific prior
23160753Syar *    written permission.
2457084Sluigi *
2557084Sluigi * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
2657084Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2780029Sobrien * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
281553Srgrimes * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <sys/param.h>
39#include <sys/device.h>
40#include <sys/device_impl.h>	/* XXX autoconf abuse */
41#include <sys/kernel.h>
42#include <sys/systm.h>
43#include <sys/mutex.h>
44#include <sys/bus.h>
45#include <sys/cpu.h>
46
47#include <arm/iomd/iomdreg.h>
48#include <arm/iomd/iomdvar.h>
49
50#include <dev/i2c/i2cvar.h>
51#include <dev/i2c/i2c_bitbang.h>
52
53#include <arm/iomd/iomdiicvar.h>
54
55struct iomdiic_softc {
56	device_t sc_dev;
57	bus_space_tag_t sc_st;
58	bus_space_handle_t sc_sh;
59
60	struct i2c_controller sc_i2c;
61
62	/*
63	 * The SDA pin is open-drain, so we make it an input by
64	 * writing a 1 to it.
65	 */
66	uint8_t sc_iomd_iocr;
67};
68
69static int	iomdiic_send_start(void *, int);
70static int	iomdiic_send_stop(void *, int);
71static int	iomdiic_initiate_xfer(void *, i2c_addr_t, int);
72static int	iomdiic_read_byte(void *, uint8_t *, int);
73static int	iomdiic_write_byte(void *, uint8_t, int);
74
75#define	IOMDIIC_READ	 *(volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR))
76#define	IOMDIIC_WRITE(x) *(volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR)) = (x)
77
78#define	IOMD_IOCR_SDA		0x01
79#define	IOMD_IOCR_SCL		0x02
80
81static void
82iomdiic_bb_set_bits(void *cookie, uint32_t bits)
83{
84	struct iomdiic_softc *sc = cookie;
85
86	IOMDIIC_WRITE((IOMDIIC_READ & ~(IOMD_IOCR_SDA|IOMD_IOCR_SCL)) |
87	    sc->sc_iomd_iocr | bits);
88}
89
90static void
91iomdiic_bb_set_dir(void *cookie, uint32_t bits)
92{
93	struct iomdiic_softc *sc = cookie;
94
95	sc->sc_iomd_iocr = bits;
96}
97
98static uint32_t
99iomdiic_bb_read_bits(void *cookie)
100{
101
102	return (IOMDIIC_READ);
103}
104
105static const struct i2c_bitbang_ops iomdiic_bbops = {
106	iomdiic_bb_set_bits,
107	iomdiic_bb_set_dir,
108	iomdiic_bb_read_bits,
109	{
110		IOMD_IOCR_SDA,		/* SDA */
111		IOMD_IOCR_SCL,		/* SCL */
112		0,			/* SDA is output */
113		IOMD_IOCR_SDA,		/* SDA is input */
114	}
115};
116
117static int
118iomdiic_match(device_t parent, cfdata_t cf, void *aux)
119{
120	struct iic_attach_args *ia = aux;
121
122	if (strcmp(ia->ia_name, "iic") == 0) return 1;
123	return 0;
124}
125
126static void
127iomdiic_attach(device_t parent, device_t self, void *aux)
128{
129	struct iomdiic_softc *sc = device_private(self);
130	struct i2cbus_attach_args iba;
131
132	aprint_normal("\n");
133
134	sc->sc_dev = self;
135
136	iic_tag_init(&sc->sc_i2c);
137	sc->sc_i2c.ic_cookie = sc;
138	sc->sc_i2c.ic_send_start = iomdiic_send_start;
139	sc->sc_i2c.ic_send_stop = iomdiic_send_stop;
140	sc->sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
141	sc->sc_i2c.ic_read_byte = iomdiic_read_byte;
142	sc->sc_i2c.ic_write_byte = iomdiic_write_byte;
143
144	memset(&iba, 0, sizeof(iba));
145	iba.iba_tag = &sc->sc_i2c;
146	config_found(sc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
147}
148
149CFATTACH_DECL_NEW(iomdiic, sizeof(struct iomdiic_softc),
150    iomdiic_match, iomdiic_attach, NULL, NULL);
151
152i2c_tag_t
153iomdiic_bootstrap_cookie(void)
154{
155	static struct iomdiic_softc sc;
156	static struct device dev;
157
158	/* XXX Yuck. */
159	strcpy(dev.dv_xname, "iomdiicboot");
160
161	sc.sc_dev = &dev;
162
163	iic_tag_init(&sc.sc_i2c);
164	sc.sc_i2c.ic_cookie = &sc;
165	sc.sc_i2c.ic_send_start = iomdiic_send_start;
166	sc.sc_i2c.ic_send_stop = iomdiic_send_stop;
167	sc.sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
168	sc.sc_i2c.ic_read_byte = iomdiic_read_byte;
169	sc.sc_i2c.ic_write_byte = iomdiic_write_byte;
170
171	return ((void *) &sc.sc_i2c);
172}
173
174static int
175iomdiic_send_start(void *cookie, int flags)
176{
177
178	return (i2c_bitbang_send_start(cookie, flags, &iomdiic_bbops));
179}
180
181static int
182iomdiic_send_stop(void *cookie, int flags)
183{
184
185	return (i2c_bitbang_send_stop(cookie, flags, &iomdiic_bbops));
186}
187
188static int
189iomdiic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
190{
191
192	return (i2c_bitbang_initiate_xfer(cookie, addr, flags, &iomdiic_bbops));
193}
194
195static int
196iomdiic_read_byte(void *cookie, uint8_t *bytep, int flags)
197{
198
199	return (i2c_bitbang_read_byte(cookie, bytep, flags, &iomdiic_bbops));
200}
201
202static int
203iomdiic_write_byte(void *cookie, uint8_t byte, int flags)
204{
205
206	return (i2c_bitbang_write_byte(cookie, byte, flags, &iomdiic_bbops));
207}
208