xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_bsc.c (revision 0bd904ed4730e481c5ad0dffc27794ec6ac70cfc)
1be9ddf43SLuiz Otavio O Souza /*-
2be9ddf43SLuiz Otavio O Souza  * Copyright (c) 2001 Tsubai Masanari.
3be9ddf43SLuiz Otavio O Souza  * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
4be9ddf43SLuiz Otavio O Souza  * Copyright (c) 2013 Luiz Otavio O Souza <loos@freebsd.org>
5*0bd904edSIan Lepore  * Copyright (c) 2017 Ian Lepore <ian@freebsd.org>
6be9ddf43SLuiz Otavio O Souza  * All rights reserved.
7be9ddf43SLuiz Otavio O Souza  *
8be9ddf43SLuiz Otavio O Souza  * Redistribution and use in source and binary forms, with or without
9be9ddf43SLuiz Otavio O Souza  * modification, are permitted provided that the following conditions
10be9ddf43SLuiz Otavio O Souza  * are met:
11be9ddf43SLuiz Otavio O Souza  * 1. Redistributions of source code must retain the above copyright
12be9ddf43SLuiz Otavio O Souza  *    notice, this list of conditions and the following disclaimer.
13be9ddf43SLuiz Otavio O Souza  * 2. Redistributions in binary form must reproduce the above copyright
14be9ddf43SLuiz Otavio O Souza  *    notice, this list of conditions and the following disclaimer in the
15be9ddf43SLuiz Otavio O Souza  *    documentation and/or other materials provided with the distribution.
16be9ddf43SLuiz Otavio O Souza  *
17be9ddf43SLuiz Otavio O Souza  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18be9ddf43SLuiz Otavio O Souza  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19be9ddf43SLuiz Otavio O Souza  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20be9ddf43SLuiz Otavio O Souza  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21be9ddf43SLuiz Otavio O Souza  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22be9ddf43SLuiz Otavio O Souza  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23be9ddf43SLuiz Otavio O Souza  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24be9ddf43SLuiz Otavio O Souza  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25be9ddf43SLuiz Otavio O Souza  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26be9ddf43SLuiz Otavio O Souza  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27be9ddf43SLuiz Otavio O Souza  * SUCH DAMAGE.
28be9ddf43SLuiz Otavio O Souza  *
29be9ddf43SLuiz Otavio O Souza  */
30be9ddf43SLuiz Otavio O Souza #include <sys/cdefs.h>
31be9ddf43SLuiz Otavio O Souza __FBSDID("$FreeBSD$");
32be9ddf43SLuiz Otavio O Souza 
33*0bd904edSIan Lepore /*
34*0bd904edSIan Lepore  * Driver for bcm2835 i2c-compatible two-wire bus, named 'BSC' on this SoC.
35*0bd904edSIan Lepore  *
36*0bd904edSIan Lepore  * This controller can only perform complete transfers, it does not provide
37*0bd904edSIan Lepore  * low-level control over sending start/repeat-start/stop sequences on the bus.
38*0bd904edSIan Lepore  * In addition, bugs in the silicon make it somewhat difficult to perform a
39*0bd904edSIan Lepore  * repeat-start, and limit the repeat-start to a read following a write on
40*0bd904edSIan Lepore  * the same slave device.  (The i2c protocol allows a repeat start to change
41*0bd904edSIan Lepore  * direction or not, and change slave address or not at any time.)
42*0bd904edSIan Lepore  *
43*0bd904edSIan Lepore  * The repeat-start bug and workaround are described in a problem report at
44*0bd904edSIan Lepore  * https://github.com/raspberrypi/linux/issues/254 with the crucial part being
45*0bd904edSIan Lepore  * in a comment block from a fragment of a GPU i2c driver, containing this:
46*0bd904edSIan Lepore  *
47*0bd904edSIan Lepore  * -----------------------------------------------------------------------------
48*0bd904edSIan Lepore  * - See i2c.v: The I2C peripheral samples the values for rw_bit and xfer_count
49*0bd904edSIan Lepore  * - in the IDLE state if start is set.
50*0bd904edSIan Lepore  * -
51*0bd904edSIan Lepore  * - We want to generate a ReSTART not a STOP at the end of the TX phase. In
52*0bd904edSIan Lepore  * - order to do that we must ensure the state machine goes RACK1 -> RACK2 ->
53*0bd904edSIan Lepore  * - SRSTRT1 (not RACK1 -> RACK2 -> SSTOP1).
54*0bd904edSIan Lepore  * -
55*0bd904edSIan Lepore  * - So, in the RACK2 state when (TX) xfer_count==0 we must therefore have
56*0bd904edSIan Lepore  * - already set, ready to be sampled:
57*0bd904edSIan Lepore  * -  READ ; rw_bit     <= I2CC bit 0 -- must be "read"
58*0bd904edSIan Lepore  * -  ST;    start      <= I2CC bit 7 -- must be "Go" in order to not issue STOP
59*0bd904edSIan Lepore  * -  DLEN;  xfer_count <= I2CDLEN    -- must be equal to our read amount
60*0bd904edSIan Lepore  * -
61*0bd904edSIan Lepore  * - The plan to do this is:
62*0bd904edSIan Lepore  * -  1. Start the sub-address write, but don't let it finish
63*0bd904edSIan Lepore  * -     (keep xfer_count > 0)
64*0bd904edSIan Lepore  * -  2. Populate READ, DLEN and ST in preparation for ReSTART read sequence
65*0bd904edSIan Lepore  * -  3. Let TX finish (write the rest of the data)
66*0bd904edSIan Lepore  * -  4. Read back data as it arrives
67*0bd904edSIan Lepore  * -----------------------------------------------------------------------------
68*0bd904edSIan Lepore  *
69*0bd904edSIan Lepore  * The transfer function below scans the list of messages passed to it, looking
70*0bd904edSIan Lepore  * for a read following a write to the same slave.  When it finds that, it
71*0bd904edSIan Lepore  * starts the write without prefilling the tx fifo, which holds xfer_count>0,
72*0bd904edSIan Lepore  * then presets the direction, length, and start command for the following read,
73*0bd904edSIan Lepore  * as described above.  Then the tx fifo is filled and the rest of the transfer
74*0bd904edSIan Lepore  * proceeds as normal, with the controller automatically supplying a
75*0bd904edSIan Lepore  * repeat-start on the bus when the write operation finishes.
76*0bd904edSIan Lepore  *
77*0bd904edSIan Lepore  * XXX I suspect the controller may be able to do a repeat-start on any
78*0bd904edSIan Lepore  * write->read or write->write transition, even when the slave addresses differ.
79*0bd904edSIan Lepore  * It's unclear whether the slave address can be prestaged along with the
80*0bd904edSIan Lepore  * direction and length while the write xfer_count is being held at zero.  In
81*0bd904edSIan Lepore  * fact, if it can't do this, then it couldn't be used to read EDID data.
82*0bd904edSIan Lepore  */
83*0bd904edSIan Lepore 
84be9ddf43SLuiz Otavio O Souza #include <sys/param.h>
85be9ddf43SLuiz Otavio O Souza #include <sys/systm.h>
86be9ddf43SLuiz Otavio O Souza #include <sys/kernel.h>
87be9ddf43SLuiz Otavio O Souza #include <sys/lock.h>
88be9ddf43SLuiz Otavio O Souza #include <sys/module.h>
89be9ddf43SLuiz Otavio O Souza #include <sys/mutex.h>
90be9ddf43SLuiz Otavio O Souza #include <sys/bus.h>
91be9ddf43SLuiz Otavio O Souza #include <machine/resource.h>
92be9ddf43SLuiz Otavio O Souza #include <machine/bus.h>
93be9ddf43SLuiz Otavio O Souza #include <sys/rman.h>
94be9ddf43SLuiz Otavio O Souza #include <sys/sysctl.h>
95be9ddf43SLuiz Otavio O Souza 
96be9ddf43SLuiz Otavio O Souza #include <dev/iicbus/iicbus.h>
97be9ddf43SLuiz Otavio O Souza #include <dev/iicbus/iiconf.h>
98be9ddf43SLuiz Otavio O Souza #include <dev/ofw/ofw_bus.h>
99be9ddf43SLuiz Otavio O Souza #include <dev/ofw/ofw_bus_subr.h>
100be9ddf43SLuiz Otavio O Souza 
101be9ddf43SLuiz Otavio O Souza #include <arm/broadcom/bcm2835/bcm2835_gpio.h>
102be9ddf43SLuiz Otavio O Souza #include <arm/broadcom/bcm2835/bcm2835_bscreg.h>
103be9ddf43SLuiz Otavio O Souza #include <arm/broadcom/bcm2835/bcm2835_bscvar.h>
104be9ddf43SLuiz Otavio O Souza 
105be9ddf43SLuiz Otavio O Souza #include "iicbus_if.h"
106be9ddf43SLuiz Otavio O Souza 
1079d6eb8bbSOleksandr Tymoshenko static struct ofw_compat_data compat_data[] = {
1089d6eb8bbSOleksandr Tymoshenko 	{"broadcom,bcm2835-bsc",	1},
1099d6eb8bbSOleksandr Tymoshenko 	{"brcm,bcm2708-i2c",		1},
110e9804ab2SOleksandr Tymoshenko 	{"brcm,bcm2835-i2c",		1},
1119d6eb8bbSOleksandr Tymoshenko 	{NULL,				0}
1129d6eb8bbSOleksandr Tymoshenko };
1139d6eb8bbSOleksandr Tymoshenko 
114*0bd904edSIan Lepore #define DEVICE_DEBUGF(sc, lvl, fmt, args...) \
115*0bd904edSIan Lepore     if ((lvl) <= (sc)->sc_debug) \
116*0bd904edSIan Lepore         device_printf((sc)->sc_dev, fmt, ##args)
117*0bd904edSIan Lepore 
118*0bd904edSIan Lepore #define DEBUGF(sc, lvl, fmt, args...) \
119*0bd904edSIan Lepore     if ((lvl) <= (sc)->sc_debug) \
120*0bd904edSIan Lepore         printf(fmt, ##args)
121*0bd904edSIan Lepore 
122be9ddf43SLuiz Otavio O Souza static void bcm_bsc_intr(void *);
1239e93dfcfSLuiz Otavio O Souza static int bcm_bsc_detach(device_t);
124be9ddf43SLuiz Otavio O Souza 
125be9ddf43SLuiz Otavio O Souza static void
126be9ddf43SLuiz Otavio O Souza bcm_bsc_modifyreg(struct bcm_bsc_softc *sc, uint32_t off, uint32_t mask,
127be9ddf43SLuiz Otavio O Souza 	uint32_t value)
128be9ddf43SLuiz Otavio O Souza {
129be9ddf43SLuiz Otavio O Souza 	uint32_t reg;
130be9ddf43SLuiz Otavio O Souza 
131be9ddf43SLuiz Otavio O Souza 	mtx_assert(&sc->sc_mtx, MA_OWNED);
132be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, off);
133be9ddf43SLuiz Otavio O Souza 	reg &= ~mask;
134be9ddf43SLuiz Otavio O Souza 	reg |= value;
135be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, off, reg);
136be9ddf43SLuiz Otavio O Souza }
137be9ddf43SLuiz Otavio O Souza 
138be9ddf43SLuiz Otavio O Souza static int
139be9ddf43SLuiz Otavio O Souza bcm_bsc_clock_proc(SYSCTL_HANDLER_ARGS)
140be9ddf43SLuiz Otavio O Souza {
141be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
142be9ddf43SLuiz Otavio O Souza 	uint32_t clk;
143be9ddf43SLuiz Otavio O Souza 
144be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
145be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
146be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
147be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
148be9ddf43SLuiz Otavio O Souza 	clk &= 0xffff;
149be9ddf43SLuiz Otavio O Souza 	if (clk == 0)
150be9ddf43SLuiz Otavio O Souza 		clk = 32768;
151be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
152be9ddf43SLuiz Otavio O Souza 
1539e93dfcfSLuiz Otavio O Souza 	return (sysctl_handle_int(oidp, &clk, 0, req));
154be9ddf43SLuiz Otavio O Souza }
155be9ddf43SLuiz Otavio O Souza 
156be9ddf43SLuiz Otavio O Souza static int
157be9ddf43SLuiz Otavio O Souza bcm_bsc_clkt_proc(SYSCTL_HANDLER_ARGS)
158be9ddf43SLuiz Otavio O Souza {
159be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
160be9ddf43SLuiz Otavio O Souza 	uint32_t clkt;
161be9ddf43SLuiz Otavio O Souza 	int error;
162be9ddf43SLuiz Otavio O Souza 
163be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
164be9ddf43SLuiz Otavio O Souza 
165be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
166be9ddf43SLuiz Otavio O Souza 	clkt = BCM_BSC_READ(sc, BCM_BSC_CLKT);
167be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
168be9ddf43SLuiz Otavio O Souza 	clkt &= 0xffff;
169be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &clkt, sizeof(clkt), req);
170be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
171be9ddf43SLuiz Otavio O Souza 		return (error);
172be9ddf43SLuiz Otavio O Souza 
173be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
174be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CLKT, clkt & 0xffff);
175be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
176be9ddf43SLuiz Otavio O Souza 
177be9ddf43SLuiz Otavio O Souza 	return (0);
178be9ddf43SLuiz Otavio O Souza }
179be9ddf43SLuiz Otavio O Souza 
180be9ddf43SLuiz Otavio O Souza static int
181be9ddf43SLuiz Otavio O Souza bcm_bsc_fall_proc(SYSCTL_HANDLER_ARGS)
182be9ddf43SLuiz Otavio O Souza {
183be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
184be9ddf43SLuiz Otavio O Souza 	uint32_t clk, reg;
185be9ddf43SLuiz Otavio O Souza 	int error;
186be9ddf43SLuiz Otavio O Souza 
187be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
188be9ddf43SLuiz Otavio O Souza 
189be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
190be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, BCM_BSC_DELAY);
191be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
192be9ddf43SLuiz Otavio O Souza 	reg >>= 16;
193be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
194be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
195be9ddf43SLuiz Otavio O Souza 		return (error);
196be9ddf43SLuiz Otavio O Souza 
197be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
198be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
199be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
200be9ddf43SLuiz Otavio O Souza 	if (reg > clk / 2)
201be9ddf43SLuiz Otavio O Souza 		reg = clk / 2 - 1;
202be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff0000, reg << 16);
203be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
204be9ddf43SLuiz Otavio O Souza 
205be9ddf43SLuiz Otavio O Souza 	return (0);
206be9ddf43SLuiz Otavio O Souza }
207be9ddf43SLuiz Otavio O Souza 
208be9ddf43SLuiz Otavio O Souza static int
209be9ddf43SLuiz Otavio O Souza bcm_bsc_rise_proc(SYSCTL_HANDLER_ARGS)
210be9ddf43SLuiz Otavio O Souza {
211be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
212be9ddf43SLuiz Otavio O Souza 	uint32_t clk, reg;
213be9ddf43SLuiz Otavio O Souza 	int error;
214be9ddf43SLuiz Otavio O Souza 
215be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
216be9ddf43SLuiz Otavio O Souza 
217be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
218be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, BCM_BSC_DELAY);
219be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
220be9ddf43SLuiz Otavio O Souza 	reg &= 0xffff;
221be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
222be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
223be9ddf43SLuiz Otavio O Souza 		return (error);
224be9ddf43SLuiz Otavio O Souza 
225be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
226be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
227be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
228be9ddf43SLuiz Otavio O Souza 	if (reg > clk / 2)
229be9ddf43SLuiz Otavio O Souza 		reg = clk / 2 - 1;
230be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff, reg);
231be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
232be9ddf43SLuiz Otavio O Souza 
233be9ddf43SLuiz Otavio O Souza 	return (0);
234be9ddf43SLuiz Otavio O Souza }
235be9ddf43SLuiz Otavio O Souza 
236be9ddf43SLuiz Otavio O Souza static void
237be9ddf43SLuiz Otavio O Souza bcm_bsc_sysctl_init(struct bcm_bsc_softc *sc)
238be9ddf43SLuiz Otavio O Souza {
239be9ddf43SLuiz Otavio O Souza 	struct sysctl_ctx_list *ctx;
240be9ddf43SLuiz Otavio O Souza 	struct sysctl_oid *tree_node;
241be9ddf43SLuiz Otavio O Souza 	struct sysctl_oid_list *tree;
242be9ddf43SLuiz Otavio O Souza 
243be9ddf43SLuiz Otavio O Souza 	/*
244be9ddf43SLuiz Otavio O Souza 	 * Add system sysctl tree/handlers.
245be9ddf43SLuiz Otavio O Souza 	 */
246be9ddf43SLuiz Otavio O Souza 	ctx = device_get_sysctl_ctx(sc->sc_dev);
247be9ddf43SLuiz Otavio O Souza 	tree_node = device_get_sysctl_tree(sc->sc_dev);
248be9ddf43SLuiz Otavio O Souza 	tree = SYSCTL_CHILDREN(tree_node);
2499e93dfcfSLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "frequency",
250be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
251be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_clock_proc, "IU", "I2C BUS clock frequency");
252be9ddf43SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clock_stretch",
253be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
254be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_clkt_proc, "IU", "I2C BUS clock stretch timeout");
255be9ddf43SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "fall_edge_delay",
256be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
257be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_fall_proc, "IU", "I2C BUS falling edge delay");
258be9ddf43SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "rise_edge_delay",
259be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
260be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_rise_proc, "IU", "I2C BUS rising edge delay");
261*0bd904edSIan Lepore 	SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "debug",
262*0bd904edSIan Lepore 	    CTLFLAG_RWTUN, &sc->sc_debug, 0,
263*0bd904edSIan Lepore 	    "Enable debug; 1=reads/writes, 2=add starts/stops");
264be9ddf43SLuiz Otavio O Souza }
265be9ddf43SLuiz Otavio O Souza 
266be9ddf43SLuiz Otavio O Souza static void
267be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(struct bcm_bsc_softc *sc)
268be9ddf43SLuiz Otavio O Souza {
269be9ddf43SLuiz Otavio O Souza 
270e50c6241SLuiz Otavio O Souza 	/* Enable the BSC Controller, disable interrupts. */
271e50c6241SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN);
272be9ddf43SLuiz Otavio O Souza 	/* Clear pending interrupts. */
273be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_STATUS, BCM_BSC_STATUS_CLKT |
274be9ddf43SLuiz Otavio O Souza 	    BCM_BSC_STATUS_ERR | BCM_BSC_STATUS_DONE);
275be9ddf43SLuiz Otavio O Souza 	/* Clear the FIFO. */
276be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_CLEAR0,
277be9ddf43SLuiz Otavio O Souza 	    BCM_BSC_CTRL_CLEAR0);
278be9ddf43SLuiz Otavio O Souza }
279be9ddf43SLuiz Otavio O Souza 
280be9ddf43SLuiz Otavio O Souza static int
281be9ddf43SLuiz Otavio O Souza bcm_bsc_probe(device_t dev)
282be9ddf43SLuiz Otavio O Souza {
283be9ddf43SLuiz Otavio O Souza 
284add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
285add35ed5SIan Lepore 		return (ENXIO);
286add35ed5SIan Lepore 
2879d6eb8bbSOleksandr Tymoshenko 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
288be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
289be9ddf43SLuiz Otavio O Souza 
290be9ddf43SLuiz Otavio O Souza 	device_set_desc(dev, "BCM2708/2835 BSC controller");
291be9ddf43SLuiz Otavio O Souza 
292be9ddf43SLuiz Otavio O Souza 	return (BUS_PROBE_DEFAULT);
293be9ddf43SLuiz Otavio O Souza }
294be9ddf43SLuiz Otavio O Souza 
295be9ddf43SLuiz Otavio O Souza static int
296be9ddf43SLuiz Otavio O Souza bcm_bsc_attach(device_t dev)
297be9ddf43SLuiz Otavio O Souza {
298be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
29921cb1342SLuiz Otavio O Souza 	unsigned long start;
300be9ddf43SLuiz Otavio O Souza 	device_t gpio;
30121cb1342SLuiz Otavio O Souza 	int i, rid;
302be9ddf43SLuiz Otavio O Souza 
303be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
304be9ddf43SLuiz Otavio O Souza 	sc->sc_dev = dev;
305be9ddf43SLuiz Otavio O Souza 
306be9ddf43SLuiz Otavio O Souza 	rid = 0;
307be9ddf43SLuiz Otavio O Souza 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
308be9ddf43SLuiz Otavio O Souza 	    RF_ACTIVE);
309be9ddf43SLuiz Otavio O Souza 	if (!sc->sc_mem_res) {
310be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate memory window\n");
311be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
312be9ddf43SLuiz Otavio O Souza 	}
313be9ddf43SLuiz Otavio O Souza 
314be9ddf43SLuiz Otavio O Souza 	sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
315be9ddf43SLuiz Otavio O Souza 	sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
316be9ddf43SLuiz Otavio O Souza 
31721cb1342SLuiz Otavio O Souza 	/* Check the unit we are attaching by its base address. */
31821cb1342SLuiz Otavio O Souza 	start = rman_get_start(sc->sc_mem_res);
31921cb1342SLuiz Otavio O Souza 	for (i = 0; i < nitems(bcm_bsc_pins); i++) {
320b4ae2f3cSLuiz Otavio O Souza 		if (bcm_bsc_pins[i].start == (start & BCM_BSC_BASE_MASK))
32121cb1342SLuiz Otavio O Souza 			break;
32221cb1342SLuiz Otavio O Souza 	}
32321cb1342SLuiz Otavio O Souza 	if (i == nitems(bcm_bsc_pins)) {
32421cb1342SLuiz Otavio O Souza 		device_printf(dev, "only bsc0 and bsc1 are supported\n");
32502a42e68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
32621cb1342SLuiz Otavio O Souza 		return (ENXIO);
32721cb1342SLuiz Otavio O Souza 	}
32821cb1342SLuiz Otavio O Souza 
32921cb1342SLuiz Otavio O Souza 	/*
33021cb1342SLuiz Otavio O Souza 	 * Configure the GPIO pins to ALT0 function to enable BSC control
33121cb1342SLuiz Otavio O Souza 	 * over the pins.
33221cb1342SLuiz Otavio O Souza 	 */
33321cb1342SLuiz Otavio O Souza 	gpio = devclass_get_device(devclass_find("gpio"), 0);
33421cb1342SLuiz Otavio O Souza 	if (!gpio) {
33521cb1342SLuiz Otavio O Souza 		device_printf(dev, "cannot find gpio0\n");
33602a42e68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
33721cb1342SLuiz Otavio O Souza 		return (ENXIO);
33821cb1342SLuiz Otavio O Souza 	}
33921cb1342SLuiz Otavio O Souza 	bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].sda, BCM_GPIO_ALT0);
34021cb1342SLuiz Otavio O Souza 	bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].scl, BCM_GPIO_ALT0);
34121cb1342SLuiz Otavio O Souza 
342be9ddf43SLuiz Otavio O Souza 	rid = 0;
343be9ddf43SLuiz Otavio O Souza 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
344be9ddf43SLuiz Otavio O Souza 	    RF_ACTIVE | RF_SHAREABLE);
345be9ddf43SLuiz Otavio O Souza 	if (!sc->sc_irq_res) {
346be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
347be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate interrupt\n");
348be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
349be9ddf43SLuiz Otavio O Souza 	}
350be9ddf43SLuiz Otavio O Souza 
351be9ddf43SLuiz Otavio O Souza 	/* Hook up our interrupt handler. */
352be9ddf43SLuiz Otavio O Souza 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
353be9ddf43SLuiz Otavio O Souza 	    NULL, bcm_bsc_intr, sc, &sc->sc_intrhand)) {
354be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
355be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
356be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot setup the interrupt handler\n");
357be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
358be9ddf43SLuiz Otavio O Souza 	}
359be9ddf43SLuiz Otavio O Souza 
360be9ddf43SLuiz Otavio O Souza 	mtx_init(&sc->sc_mtx, "bcm_bsc", NULL, MTX_DEF);
361be9ddf43SLuiz Otavio O Souza 
362be9ddf43SLuiz Otavio O Souza 	bcm_bsc_sysctl_init(sc);
363be9ddf43SLuiz Otavio O Souza 
364be9ddf43SLuiz Otavio O Souza 	/* Enable the BSC controller.  Flush the FIFO. */
365be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
366be9ddf43SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
367be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
368be9ddf43SLuiz Otavio O Souza 
3699e93dfcfSLuiz Otavio O Souza 	sc->sc_iicbus = device_add_child(dev, "iicbus", -1);
3709e93dfcfSLuiz Otavio O Souza 	if (sc->sc_iicbus == NULL) {
3719e93dfcfSLuiz Otavio O Souza 		bcm_bsc_detach(dev);
3729e93dfcfSLuiz Otavio O Souza 		return (ENXIO);
3739e93dfcfSLuiz Otavio O Souza 	}
374be9ddf43SLuiz Otavio O Souza 
3751e4042d4SIan Lepore 	/* Probe and attach the iicbus when interrupts are available. */
3761e4042d4SIan Lepore 	config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
3771e4042d4SIan Lepore 
3781e4042d4SIan Lepore 	return (0);
379be9ddf43SLuiz Otavio O Souza }
380be9ddf43SLuiz Otavio O Souza 
381be9ddf43SLuiz Otavio O Souza static int
382be9ddf43SLuiz Otavio O Souza bcm_bsc_detach(device_t dev)
383be9ddf43SLuiz Otavio O Souza {
384be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
385be9ddf43SLuiz Otavio O Souza 
386be9ddf43SLuiz Otavio O Souza 	bus_generic_detach(dev);
387be9ddf43SLuiz Otavio O Souza 
388be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
389*0bd904edSIan Lepore 	if (sc->sc_iicbus != NULL)
390*0bd904edSIan Lepore 		device_delete_child(dev, sc->sc_iicbus);
391be9ddf43SLuiz Otavio O Souza 	mtx_destroy(&sc->sc_mtx);
392be9ddf43SLuiz Otavio O Souza 	if (sc->sc_intrhand)
393be9ddf43SLuiz Otavio O Souza 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
394be9ddf43SLuiz Otavio O Souza 	if (sc->sc_irq_res)
395be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
396be9ddf43SLuiz Otavio O Souza 	if (sc->sc_mem_res)
397be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
398be9ddf43SLuiz Otavio O Souza 
399be9ddf43SLuiz Otavio O Souza 	return (0);
400be9ddf43SLuiz Otavio O Souza }
401be9ddf43SLuiz Otavio O Souza 
402be9ddf43SLuiz Otavio O Souza static void
403*0bd904edSIan Lepore bcm_bsc_empty_rx_fifo(struct bcm_bsc_softc *sc)
404*0bd904edSIan Lepore {
405*0bd904edSIan Lepore 	uint32_t status;
406*0bd904edSIan Lepore 
407*0bd904edSIan Lepore 	/* Assumes sc_totlen > 0 and BCM_BSC_STATUS_RXD is asserted on entry. */
408*0bd904edSIan Lepore 	do {
409*0bd904edSIan Lepore 		if (sc->sc_resid == 0) {
410*0bd904edSIan Lepore 			sc->sc_data  = sc->sc_curmsg->buf;
411*0bd904edSIan Lepore 			sc->sc_dlen  = sc->sc_curmsg->len;
412*0bd904edSIan Lepore 			sc->sc_resid = sc->sc_dlen;
413*0bd904edSIan Lepore 			++sc->sc_curmsg;
414*0bd904edSIan Lepore 		}
415*0bd904edSIan Lepore 		do {
416*0bd904edSIan Lepore 			*sc->sc_data = BCM_BSC_READ(sc, BCM_BSC_DATA);
417*0bd904edSIan Lepore 			DEBUGF(sc, 1, "0x%02x ", *sc->sc_data);
418*0bd904edSIan Lepore 			++sc->sc_data;
419*0bd904edSIan Lepore 			--sc->sc_resid;
420*0bd904edSIan Lepore 			--sc->sc_totlen;
421*0bd904edSIan Lepore 			status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
422*0bd904edSIan Lepore 		} while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_RXD));
423*0bd904edSIan Lepore 	} while (sc->sc_totlen > 0 && (status & BCM_BSC_STATUS_RXD));
424*0bd904edSIan Lepore }
425*0bd904edSIan Lepore 
426*0bd904edSIan Lepore static void
427*0bd904edSIan Lepore bcm_bsc_fill_tx_fifo(struct bcm_bsc_softc *sc)
428*0bd904edSIan Lepore {
429*0bd904edSIan Lepore 	uint32_t status;
430*0bd904edSIan Lepore 
431*0bd904edSIan Lepore 	/* Assumes sc_totlen > 0 and BCM_BSC_STATUS_TXD is asserted on entry. */
432*0bd904edSIan Lepore 	do {
433*0bd904edSIan Lepore 		if (sc->sc_resid == 0) {
434*0bd904edSIan Lepore 			sc->sc_data  = sc->sc_curmsg->buf;
435*0bd904edSIan Lepore 			sc->sc_dlen  = sc->sc_curmsg->len;
436*0bd904edSIan Lepore 			sc->sc_resid = sc->sc_dlen;
437*0bd904edSIan Lepore 			++sc->sc_curmsg;
438*0bd904edSIan Lepore 		}
439*0bd904edSIan Lepore 		do {
440*0bd904edSIan Lepore 			BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data);
441*0bd904edSIan Lepore 			DEBUGF(sc, 1, "0x%02x ", *sc->sc_data);
442*0bd904edSIan Lepore 			++sc->sc_data;
443*0bd904edSIan Lepore 			--sc->sc_resid;
444*0bd904edSIan Lepore 			--sc->sc_totlen;
445*0bd904edSIan Lepore 			status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
446*0bd904edSIan Lepore 		} while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_TXD));
447*0bd904edSIan Lepore 		/*
448*0bd904edSIan Lepore 		 * If a repeat-start was pending and we just hit the end of a tx
449*0bd904edSIan Lepore 		 * buffer, see if it's also the end of the writes that preceeded
450*0bd904edSIan Lepore 		 * the repeat-start.  If so, log the repeat-start and the start
451*0bd904edSIan Lepore 		 * of the following read, and return because we're not writing
452*0bd904edSIan Lepore 		 * anymore (and TXD will be true because there's room to write
453*0bd904edSIan Lepore 		 * in the fifo).
454*0bd904edSIan Lepore 		 */
455*0bd904edSIan Lepore 		if (sc->sc_replen > 0 && sc->sc_resid == 0) {
456*0bd904edSIan Lepore 			sc->sc_replen -= sc->sc_dlen;
457*0bd904edSIan Lepore 			if (sc->sc_replen == 0) {
458*0bd904edSIan Lepore 				DEBUGF(sc, 1, " err=0\n");
459*0bd904edSIan Lepore 				DEVICE_DEBUGF(sc, 2, "rstart 0x%02x\n",
460*0bd904edSIan Lepore 				    sc->sc_curmsg->slave | 0x01);
461*0bd904edSIan Lepore 				DEVICE_DEBUGF(sc, 1,
462*0bd904edSIan Lepore 				    "read   0x%02x len %d: ",
463*0bd904edSIan Lepore 				    sc->sc_curmsg->slave | 0x01,
464*0bd904edSIan Lepore 				    sc->sc_totlen);
465*0bd904edSIan Lepore 				sc->sc_flags |= BCM_I2C_READ;
466*0bd904edSIan Lepore 				return;
467*0bd904edSIan Lepore 			}
468*0bd904edSIan Lepore 		}
469*0bd904edSIan Lepore 	} while (sc->sc_totlen > 0 && (status & BCM_BSC_STATUS_TXD));
470*0bd904edSIan Lepore }
471*0bd904edSIan Lepore 
472*0bd904edSIan Lepore static void
473be9ddf43SLuiz Otavio O Souza bcm_bsc_intr(void *arg)
474be9ddf43SLuiz Otavio O Souza {
475be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
476be9ddf43SLuiz Otavio O Souza 	uint32_t status;
477be9ddf43SLuiz Otavio O Souza 
478be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg;
479be9ddf43SLuiz Otavio O Souza 
480be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
481be9ddf43SLuiz Otavio O Souza 
482be9ddf43SLuiz Otavio O Souza 	/* The I2C interrupt is shared among all the BSC controllers. */
483be9ddf43SLuiz Otavio O Souza 	if ((sc->sc_flags & BCM_I2C_BUSY) == 0) {
484be9ddf43SLuiz Otavio O Souza 		BCM_BSC_UNLOCK(sc);
485be9ddf43SLuiz Otavio O Souza 		return;
486be9ddf43SLuiz Otavio O Souza 	}
487be9ddf43SLuiz Otavio O Souza 
488be9ddf43SLuiz Otavio O Souza 	status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
489*0bd904edSIan Lepore 	DEBUGF(sc, 4, " <intrstatus=0x%08x> ", status);
490be9ddf43SLuiz Otavio O Souza 
491*0bd904edSIan Lepore 	/* RXD and DONE can assert together, empty fifo before checking done. */
492*0bd904edSIan Lepore 	if ((sc->sc_flags & BCM_I2C_READ) && (status & BCM_BSC_STATUS_RXD))
493*0bd904edSIan Lepore 		bcm_bsc_empty_rx_fifo(sc);
494*0bd904edSIan Lepore 
495*0bd904edSIan Lepore 	/* Check for completion. */
496*0bd904edSIan Lepore 	if (status & (BCM_BSC_STATUS_ERRBITS | BCM_BSC_STATUS_DONE)) {
497*0bd904edSIan Lepore 		sc->sc_flags |= BCM_I2C_DONE;
498*0bd904edSIan Lepore 		if (status & BCM_BSC_STATUS_ERRBITS)
499e50c6241SLuiz Otavio O Souza 			sc->sc_flags |= BCM_I2C_ERROR;
500be9ddf43SLuiz Otavio O Souza 		/* Disable interrupts. */
501be9ddf43SLuiz Otavio O Souza 		bcm_bsc_reset(sc);
502*0bd904edSIan Lepore 		wakeup(sc);
503*0bd904edSIan Lepore 	} else if (!(sc->sc_flags & BCM_I2C_READ)) {
504*0bd904edSIan Lepore 		/*
505*0bd904edSIan Lepore 		 * Don't check for TXD until after determining whether the
506*0bd904edSIan Lepore 		 * transfer is complete; TXD will be asserted along with ERR or
507*0bd904edSIan Lepore 		 * DONE if there is room in the fifo.
508*0bd904edSIan Lepore 		 */
509*0bd904edSIan Lepore 		if (status & BCM_BSC_STATUS_TXD)
510*0bd904edSIan Lepore 			bcm_bsc_fill_tx_fifo(sc);
511be9ddf43SLuiz Otavio O Souza 	}
512be9ddf43SLuiz Otavio O Souza 
513be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
514be9ddf43SLuiz Otavio O Souza }
515be9ddf43SLuiz Otavio O Souza 
516be9ddf43SLuiz Otavio O Souza static int
517be9ddf43SLuiz Otavio O Souza bcm_bsc_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
518be9ddf43SLuiz Otavio O Souza {
519be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
520*0bd904edSIan Lepore 	struct iic_msg *endmsgs, *nxtmsg;
521*0bd904edSIan Lepore 	uint32_t readctl, status;
522*0bd904edSIan Lepore 	int err;
523*0bd904edSIan Lepore 	uint16_t curlen;
524*0bd904edSIan Lepore 	uint8_t curisread, curslave, nxtisread, nxtslave;
525be9ddf43SLuiz Otavio O Souza 
526be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
527be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
528be9ddf43SLuiz Otavio O Souza 
529be9ddf43SLuiz Otavio O Souza 	/* If the controller is busy wait until it is available. */
530be9ddf43SLuiz Otavio O Souza 	while (sc->sc_flags & BCM_I2C_BUSY)
531a141b672SLuiz Otavio O Souza 		mtx_sleep(dev, &sc->sc_mtx, 0, "bscbusw", 0);
532be9ddf43SLuiz Otavio O Souza 
533be9ddf43SLuiz Otavio O Souza 	/* Now we have control over the BSC controller. */
534be9ddf43SLuiz Otavio O Souza 	sc->sc_flags = BCM_I2C_BUSY;
535be9ddf43SLuiz Otavio O Souza 
536*0bd904edSIan Lepore 	DEVICE_DEBUGF(sc, 3, "Transfer %d msgs\n", nmsgs);
537*0bd904edSIan Lepore 
538be9ddf43SLuiz Otavio O Souza 	/* Clear the FIFO and the pending interrupts. */
539be9ddf43SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
540be9ddf43SLuiz Otavio O Souza 
541*0bd904edSIan Lepore 	/*
542*0bd904edSIan Lepore 	 * Perform all the transfers requested in the array of msgs.  Note that
543*0bd904edSIan Lepore 	 * it is bcm_bsc_empty_rx_fifo() and bcm_bsc_fill_tx_fifo() that advance
544*0bd904edSIan Lepore 	 * sc->sc_curmsg through the array of messages, as the data from each
545*0bd904edSIan Lepore 	 * message is fully consumed, but it is this loop that notices when we
546*0bd904edSIan Lepore 	 * have no more messages to process.
547*0bd904edSIan Lepore 	 */
548be9ddf43SLuiz Otavio O Souza 	err = 0;
549*0bd904edSIan Lepore 	sc->sc_resid = 0;
550*0bd904edSIan Lepore 	sc->sc_curmsg = msgs;
551*0bd904edSIan Lepore 	endmsgs = &msgs[nmsgs];
552*0bd904edSIan Lepore 	while (sc->sc_curmsg < endmsgs) {
553*0bd904edSIan Lepore 		readctl = 0;
554*0bd904edSIan Lepore 		curslave = sc->sc_curmsg->slave >> 1;
555*0bd904edSIan Lepore 		curisread = sc->sc_curmsg->flags & IIC_M_RD;
556*0bd904edSIan Lepore 		sc->sc_replen = 0;
557*0bd904edSIan Lepore 		sc->sc_totlen = sc->sc_curmsg->len;
558*0bd904edSIan Lepore 		/*
559*0bd904edSIan Lepore 		 * Scan for scatter/gather IO (same slave and direction) or
560*0bd904edSIan Lepore 		 * repeat-start (read following write for the same slave).
561*0bd904edSIan Lepore 		 */
562*0bd904edSIan Lepore 		for (nxtmsg = sc->sc_curmsg + 1; nxtmsg < endmsgs; ++nxtmsg) {
563*0bd904edSIan Lepore 			nxtslave = nxtmsg->slave >> 1;
564*0bd904edSIan Lepore 			if (curslave == nxtslave) {
565*0bd904edSIan Lepore 				nxtisread = nxtmsg->flags & IIC_M_RD;
566*0bd904edSIan Lepore 				if (curisread == nxtisread) {
567*0bd904edSIan Lepore 					/*
568*0bd904edSIan Lepore 					 * Same slave and direction, this
569*0bd904edSIan Lepore 					 * message will be part of the same
570*0bd904edSIan Lepore 					 * transfer as the previous one.
571*0bd904edSIan Lepore 					 */
572*0bd904edSIan Lepore 					sc->sc_totlen += nxtmsg->len;
573*0bd904edSIan Lepore 					continue;
574*0bd904edSIan Lepore 				} else if (curisread == IIC_M_WR) {
575*0bd904edSIan Lepore 					/*
576*0bd904edSIan Lepore 					 * Read after write to same slave means
577*0bd904edSIan Lepore 					 * repeat-start, remember how many bytes
578*0bd904edSIan Lepore 					 * come before the repeat-start, switch
579*0bd904edSIan Lepore 					 * the direction to IIC_M_RD, and gather
580*0bd904edSIan Lepore 					 * up following reads to the same slave.
581*0bd904edSIan Lepore 					 */
582*0bd904edSIan Lepore 					curisread = IIC_M_RD;
583*0bd904edSIan Lepore 					sc->sc_replen = sc->sc_totlen;
584*0bd904edSIan Lepore 					sc->sc_totlen += nxtmsg->len;
585*0bd904edSIan Lepore 					continue;
586*0bd904edSIan Lepore 				}
587*0bd904edSIan Lepore 			}
588*0bd904edSIan Lepore 			break;
589*0bd904edSIan Lepore 		}
590*0bd904edSIan Lepore 
591*0bd904edSIan Lepore 		/*
592*0bd904edSIan Lepore 		 * curslave and curisread temporaries from above may refer to
593*0bd904edSIan Lepore 		 * the after-repstart msg, reset them to reflect sc_curmsg.
594*0bd904edSIan Lepore 		 */
595*0bd904edSIan Lepore 		curisread = (sc->sc_curmsg->flags & IIC_M_RD) ? 1 : 0;
596*0bd904edSIan Lepore 		curslave = sc->sc_curmsg->slave | curisread;
597be9ddf43SLuiz Otavio O Souza 
598be9ddf43SLuiz Otavio O Souza 		/* Write the slave address. */
599*0bd904edSIan Lepore 		BCM_BSC_WRITE(sc, BCM_BSC_SLAVE, curslave >> 1);
600be9ddf43SLuiz Otavio O Souza 
601*0bd904edSIan Lepore 		DEVICE_DEBUGF(sc, 2, "start  0x%02x\n", curslave);
602be9ddf43SLuiz Otavio O Souza 
603*0bd904edSIan Lepore 		/*
604*0bd904edSIan Lepore 		 * Either set up read length and direction variables for a
605*0bd904edSIan Lepore 		 * simple transfer or get the hardware started on the first
606*0bd904edSIan Lepore 		 * piece of a transfer that involves a repeat-start and set up
607*0bd904edSIan Lepore 		 * the read length and direction vars for the second piece.
608*0bd904edSIan Lepore 		 */
609*0bd904edSIan Lepore 		if (sc->sc_replen == 0) {
610*0bd904edSIan Lepore 			DEVICE_DEBUGF(sc, 1, "%-6s 0x%02x len %d: ",
611*0bd904edSIan Lepore 			    (curisread) ? "readctl" : "write", curslave,
612*0bd904edSIan Lepore 			    sc->sc_totlen);
613*0bd904edSIan Lepore 			curlen = sc->sc_totlen;
614*0bd904edSIan Lepore 			if (curisread) {
615*0bd904edSIan Lepore 				readctl = BCM_BSC_CTRL_READ;
616be9ddf43SLuiz Otavio O Souza 				sc->sc_flags |= BCM_I2C_READ;
617*0bd904edSIan Lepore 			} else {
618*0bd904edSIan Lepore 				readctl = 0;
619*0bd904edSIan Lepore 				sc->sc_flags &= ~BCM_I2C_READ;
620be9ddf43SLuiz Otavio O Souza 			}
621*0bd904edSIan Lepore 		} else {
622*0bd904edSIan Lepore 			DEVICE_DEBUGF(sc, 1, "%-6s 0x%02x len %d: ",
623*0bd904edSIan Lepore 			    (curisread) ? "readctl" : "write", curslave,
624*0bd904edSIan Lepore 			    sc->sc_replen);
625be9ddf43SLuiz Otavio O Souza 
626*0bd904edSIan Lepore 			/*
627*0bd904edSIan Lepore 			 * Start the write transfer with an empty fifo and wait
628*0bd904edSIan Lepore 			 * for the 'transfer active' status bit to light up;
629*0bd904edSIan Lepore 			 * that indicates that the hardware has latched the
630*0bd904edSIan Lepore 			 * direction and length for the write, and we can safely
631*0bd904edSIan Lepore 			 * reload those registers and issue the start for the
632*0bd904edSIan Lepore 			 * following read; interrupts are not enabled here.
633*0bd904edSIan Lepore 			 */
634*0bd904edSIan Lepore 			BCM_BSC_WRITE(sc, BCM_BSC_DLEN, sc->sc_replen);
635be9ddf43SLuiz Otavio O Souza 			BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN |
636*0bd904edSIan Lepore 			    BCM_BSC_CTRL_ST);
637*0bd904edSIan Lepore 			do {
638*0bd904edSIan Lepore 				status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
639*0bd904edSIan Lepore 				if (status & BCM_BSC_STATUS_ERR) {
640*0bd904edSIan Lepore 					/* no ACK on slave addr */
641*0bd904edSIan Lepore 					err = EIO;
642*0bd904edSIan Lepore 					goto xfer_done;
643*0bd904edSIan Lepore 				}
644*0bd904edSIan Lepore 			} while ((status & BCM_BSC_STATUS_TA) == 0);
645*0bd904edSIan Lepore 			/*
646*0bd904edSIan Lepore 			 * Set curlen and readctl for the repeat-start read that
647*0bd904edSIan Lepore 			 * we need to set up below, but set sc_flags to write,
648*0bd904edSIan Lepore 			 * because that is the operation in progress right now.
649*0bd904edSIan Lepore 			 */
650*0bd904edSIan Lepore 			curlen = sc->sc_totlen - sc->sc_replen;
651*0bd904edSIan Lepore 			readctl = BCM_BSC_CTRL_READ;
652*0bd904edSIan Lepore 			sc->sc_flags &= ~BCM_I2C_READ;
653*0bd904edSIan Lepore 		}
654*0bd904edSIan Lepore 
655*0bd904edSIan Lepore 		/*
656*0bd904edSIan Lepore 		 * Start the transfer with interrupts enabled, then if doing a
657*0bd904edSIan Lepore 		 * write, fill the tx fifo.  Not prefilling the fifo until after
658*0bd904edSIan Lepore 		 * this start command is the key workaround for making
659*0bd904edSIan Lepore 		 * repeat-start work, and it's harmless to do it in this order
660*0bd904edSIan Lepore 		 * for a regular write too.
661*0bd904edSIan Lepore 		 */
662*0bd904edSIan Lepore 		BCM_BSC_WRITE(sc, BCM_BSC_DLEN, curlen);
663*0bd904edSIan Lepore 		BCM_BSC_WRITE(sc, BCM_BSC_CTRL, readctl | BCM_BSC_CTRL_I2CEN |
664*0bd904edSIan Lepore 		    BCM_BSC_CTRL_ST | BCM_BSC_CTRL_INT_ALL);
665*0bd904edSIan Lepore 
666*0bd904edSIan Lepore 		if (!(sc->sc_curmsg->flags & IIC_M_RD)) {
667*0bd904edSIan Lepore 			bcm_bsc_fill_tx_fifo(sc);
668*0bd904edSIan Lepore 		}
669be9ddf43SLuiz Otavio O Souza 
670be9ddf43SLuiz Otavio O Souza 		/* Wait for the transaction to complete. */
671*0bd904edSIan Lepore 		while (err == 0 && !(sc->sc_flags & BCM_I2C_DONE)) {
672*0bd904edSIan Lepore 			err = mtx_sleep(sc, &sc->sc_mtx, 0, "bsciow", hz);
673*0bd904edSIan Lepore 		}
67471b37cb9SLuiz Otavio O Souza 		/* Check for errors. */
6750a39cc71SLuiz Otavio O Souza 		if (err == 0 && (sc->sc_flags & BCM_I2C_ERROR))
676be9ddf43SLuiz Otavio O Souza 			err = EIO;
677*0bd904edSIan Lepore xfer_done:
678*0bd904edSIan Lepore 		DEBUGF(sc, 1, " err=%d\n", err);
679*0bd904edSIan Lepore 		DEVICE_DEBUGF(sc, 2, "stop\n");
68071b37cb9SLuiz Otavio O Souza 		if (err != 0)
681be9ddf43SLuiz Otavio O Souza 			break;
682be9ddf43SLuiz Otavio O Souza 	}
683be9ddf43SLuiz Otavio O Souza 
684*0bd904edSIan Lepore 	/* Disable interrupts, clean fifo, etc. */
685*0bd904edSIan Lepore 	bcm_bsc_reset(sc);
686*0bd904edSIan Lepore 
687be9ddf43SLuiz Otavio O Souza 	/* Clean the controller flags. */
688be9ddf43SLuiz Otavio O Souza 	sc->sc_flags = 0;
689be9ddf43SLuiz Otavio O Souza 
690a141b672SLuiz Otavio O Souza 	/* Wake up the threads waiting for bus. */
691a141b672SLuiz Otavio O Souza 	wakeup(dev);
692a141b672SLuiz Otavio O Souza 
693be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
694be9ddf43SLuiz Otavio O Souza 
695be9ddf43SLuiz Otavio O Souza 	return (err);
696be9ddf43SLuiz Otavio O Souza }
697be9ddf43SLuiz Otavio O Souza 
698e50c6241SLuiz Otavio O Souza static int
699e50c6241SLuiz Otavio O Souza bcm_bsc_iicbus_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
700e50c6241SLuiz Otavio O Souza {
701e50c6241SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
7029e93dfcfSLuiz Otavio O Souza 	uint32_t busfreq;
703e50c6241SLuiz Otavio O Souza 
704e50c6241SLuiz Otavio O Souza 	sc = device_get_softc(dev);
705e50c6241SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
706e50c6241SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
7079e93dfcfSLuiz Otavio O Souza 	if (sc->sc_iicbus == NULL)
7089e93dfcfSLuiz Otavio O Souza 		busfreq = 100000;
7099e93dfcfSLuiz Otavio O Souza 	else
7109e93dfcfSLuiz Otavio O Souza 		busfreq = IICBUS_GET_FREQUENCY(sc->sc_iicbus, speed);
7119e93dfcfSLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CLOCK, BCM_BSC_CORE_CLK / busfreq);
712e50c6241SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
713e50c6241SLuiz Otavio O Souza 
714e50c6241SLuiz Otavio O Souza 	return (IIC_ENOADDR);
715e50c6241SLuiz Otavio O Souza }
716e50c6241SLuiz Otavio O Souza 
717be9ddf43SLuiz Otavio O Souza static phandle_t
718be9ddf43SLuiz Otavio O Souza bcm_bsc_get_node(device_t bus, device_t dev)
719be9ddf43SLuiz Otavio O Souza {
720be9ddf43SLuiz Otavio O Souza 
721be9ddf43SLuiz Otavio O Souza 	/* We only have one child, the I2C bus, which needs our own node. */
722be9ddf43SLuiz Otavio O Souza 	return (ofw_bus_get_node(bus));
723be9ddf43SLuiz Otavio O Souza }
724be9ddf43SLuiz Otavio O Souza 
725be9ddf43SLuiz Otavio O Souza static device_method_t bcm_bsc_methods[] = {
726be9ddf43SLuiz Otavio O Souza 	/* Device interface */
727be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_probe,		bcm_bsc_probe),
728be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_attach,	bcm_bsc_attach),
729be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_detach,	bcm_bsc_detach),
730be9ddf43SLuiz Otavio O Souza 
731be9ddf43SLuiz Otavio O Souza 	/* iicbus interface */
732e50c6241SLuiz Otavio O Souza 	DEVMETHOD(iicbus_reset,		bcm_bsc_iicbus_reset),
733be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(iicbus_callback,	iicbus_null_callback),
734be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(iicbus_transfer,	bcm_bsc_transfer),
735be9ddf43SLuiz Otavio O Souza 
736be9ddf43SLuiz Otavio O Souza 	/* ofw_bus interface */
737be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(ofw_bus_get_node,	bcm_bsc_get_node),
738be9ddf43SLuiz Otavio O Souza 
739be9ddf43SLuiz Otavio O Souza 	DEVMETHOD_END
740be9ddf43SLuiz Otavio O Souza };
741be9ddf43SLuiz Otavio O Souza 
742be9ddf43SLuiz Otavio O Souza static devclass_t bcm_bsc_devclass;
743be9ddf43SLuiz Otavio O Souza 
744be9ddf43SLuiz Otavio O Souza static driver_t bcm_bsc_driver = {
745be9ddf43SLuiz Otavio O Souza 	"iichb",
746be9ddf43SLuiz Otavio O Souza 	bcm_bsc_methods,
747be9ddf43SLuiz Otavio O Souza 	sizeof(struct bcm_bsc_softc),
748be9ddf43SLuiz Otavio O Souza };
749be9ddf43SLuiz Otavio O Souza 
750be9ddf43SLuiz Otavio O Souza DRIVER_MODULE(iicbus, bcm2835_bsc, iicbus_driver, iicbus_devclass, 0, 0);
751be9ddf43SLuiz Otavio O Souza DRIVER_MODULE(bcm2835_bsc, simplebus, bcm_bsc_driver, bcm_bsc_devclass, 0, 0);
752