xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_bsc.c (revision ae99239461decc6806c9ed3c8598afdef1c69e08)
1be9ddf43SLuiz Otavio O Souza /*-
2af3dc4a7SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3af3dc4a7SPedro F. Giffuni  *
4be9ddf43SLuiz Otavio O Souza  * Copyright (c) 2001 Tsubai Masanari.
5be9ddf43SLuiz Otavio O Souza  * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
6be9ddf43SLuiz Otavio O Souza  * Copyright (c) 2013 Luiz Otavio O Souza <loos@freebsd.org>
70bd904edSIan Lepore  * Copyright (c) 2017 Ian Lepore <ian@freebsd.org>
8be9ddf43SLuiz Otavio O Souza  * All rights reserved.
9be9ddf43SLuiz Otavio O Souza  *
10be9ddf43SLuiz Otavio O Souza  * Redistribution and use in source and binary forms, with or without
11be9ddf43SLuiz Otavio O Souza  * modification, are permitted provided that the following conditions
12be9ddf43SLuiz Otavio O Souza  * are met:
13be9ddf43SLuiz Otavio O Souza  * 1. Redistributions of source code must retain the above copyright
14be9ddf43SLuiz Otavio O Souza  *    notice, this list of conditions and the following disclaimer.
15be9ddf43SLuiz Otavio O Souza  * 2. Redistributions in binary form must reproduce the above copyright
16be9ddf43SLuiz Otavio O Souza  *    notice, this list of conditions and the following disclaimer in the
17be9ddf43SLuiz Otavio O Souza  *    documentation and/or other materials provided with the distribution.
18be9ddf43SLuiz Otavio O Souza  *
19be9ddf43SLuiz Otavio O Souza  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20be9ddf43SLuiz Otavio O Souza  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21be9ddf43SLuiz Otavio O Souza  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22be9ddf43SLuiz Otavio O Souza  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23be9ddf43SLuiz Otavio O Souza  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24be9ddf43SLuiz Otavio O Souza  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25be9ddf43SLuiz Otavio O Souza  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26be9ddf43SLuiz Otavio O Souza  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27be9ddf43SLuiz Otavio O Souza  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28be9ddf43SLuiz Otavio O Souza  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29be9ddf43SLuiz Otavio O Souza  * SUCH DAMAGE.
30be9ddf43SLuiz Otavio O Souza  *
31be9ddf43SLuiz Otavio O Souza  */
32be9ddf43SLuiz Otavio O Souza #include <sys/cdefs.h>
33be9ddf43SLuiz Otavio O Souza __FBSDID("$FreeBSD$");
34be9ddf43SLuiz Otavio O Souza 
350bd904edSIan Lepore /*
360bd904edSIan Lepore  * Driver for bcm2835 i2c-compatible two-wire bus, named 'BSC' on this SoC.
370bd904edSIan Lepore  *
380bd904edSIan Lepore  * This controller can only perform complete transfers, it does not provide
390bd904edSIan Lepore  * low-level control over sending start/repeat-start/stop sequences on the bus.
400bd904edSIan Lepore  * In addition, bugs in the silicon make it somewhat difficult to perform a
410bd904edSIan Lepore  * repeat-start, and limit the repeat-start to a read following a write on
420bd904edSIan Lepore  * the same slave device.  (The i2c protocol allows a repeat start to change
430bd904edSIan Lepore  * direction or not, and change slave address or not at any time.)
440bd904edSIan Lepore  *
450bd904edSIan Lepore  * The repeat-start bug and workaround are described in a problem report at
460bd904edSIan Lepore  * https://github.com/raspberrypi/linux/issues/254 with the crucial part being
470bd904edSIan Lepore  * in a comment block from a fragment of a GPU i2c driver, containing this:
480bd904edSIan Lepore  *
490bd904edSIan Lepore  * -----------------------------------------------------------------------------
500bd904edSIan Lepore  * - See i2c.v: The I2C peripheral samples the values for rw_bit and xfer_count
510bd904edSIan Lepore  * - in the IDLE state if start is set.
520bd904edSIan Lepore  * -
530bd904edSIan Lepore  * - We want to generate a ReSTART not a STOP at the end of the TX phase. In
540bd904edSIan Lepore  * - order to do that we must ensure the state machine goes RACK1 -> RACK2 ->
550bd904edSIan Lepore  * - SRSTRT1 (not RACK1 -> RACK2 -> SSTOP1).
560bd904edSIan Lepore  * -
570bd904edSIan Lepore  * - So, in the RACK2 state when (TX) xfer_count==0 we must therefore have
580bd904edSIan Lepore  * - already set, ready to be sampled:
590bd904edSIan Lepore  * -  READ ; rw_bit     <= I2CC bit 0 -- must be "read"
600bd904edSIan Lepore  * -  ST;    start      <= I2CC bit 7 -- must be "Go" in order to not issue STOP
610bd904edSIan Lepore  * -  DLEN;  xfer_count <= I2CDLEN    -- must be equal to our read amount
620bd904edSIan Lepore  * -
630bd904edSIan Lepore  * - The plan to do this is:
640bd904edSIan Lepore  * -  1. Start the sub-address write, but don't let it finish
650bd904edSIan Lepore  * -     (keep xfer_count > 0)
660bd904edSIan Lepore  * -  2. Populate READ, DLEN and ST in preparation for ReSTART read sequence
670bd904edSIan Lepore  * -  3. Let TX finish (write the rest of the data)
680bd904edSIan Lepore  * -  4. Read back data as it arrives
690bd904edSIan Lepore  * -----------------------------------------------------------------------------
700bd904edSIan Lepore  *
710bd904edSIan Lepore  * The transfer function below scans the list of messages passed to it, looking
720bd904edSIan Lepore  * for a read following a write to the same slave.  When it finds that, it
730bd904edSIan Lepore  * starts the write without prefilling the tx fifo, which holds xfer_count>0,
740bd904edSIan Lepore  * then presets the direction, length, and start command for the following read,
750bd904edSIan Lepore  * as described above.  Then the tx fifo is filled and the rest of the transfer
760bd904edSIan Lepore  * proceeds as normal, with the controller automatically supplying a
770bd904edSIan Lepore  * repeat-start on the bus when the write operation finishes.
780bd904edSIan Lepore  *
790bd904edSIan Lepore  * XXX I suspect the controller may be able to do a repeat-start on any
800bd904edSIan Lepore  * write->read or write->write transition, even when the slave addresses differ.
810bd904edSIan Lepore  * It's unclear whether the slave address can be prestaged along with the
820bd904edSIan Lepore  * direction and length while the write xfer_count is being held at zero.  In
830bd904edSIan Lepore  * fact, if it can't do this, then it couldn't be used to read EDID data.
840bd904edSIan Lepore  */
850bd904edSIan Lepore 
86be9ddf43SLuiz Otavio O Souza #include <sys/param.h>
87be9ddf43SLuiz Otavio O Souza #include <sys/systm.h>
88be9ddf43SLuiz Otavio O Souza #include <sys/kernel.h>
89be9ddf43SLuiz Otavio O Souza #include <sys/lock.h>
90be9ddf43SLuiz Otavio O Souza #include <sys/module.h>
91be9ddf43SLuiz Otavio O Souza #include <sys/mutex.h>
92be9ddf43SLuiz Otavio O Souza #include <sys/bus.h>
93be9ddf43SLuiz Otavio O Souza #include <machine/resource.h>
94be9ddf43SLuiz Otavio O Souza #include <machine/bus.h>
95be9ddf43SLuiz Otavio O Souza #include <sys/rman.h>
96be9ddf43SLuiz Otavio O Souza #include <sys/sysctl.h>
97be9ddf43SLuiz Otavio O Souza 
98be9ddf43SLuiz Otavio O Souza #include <dev/iicbus/iicbus.h>
99be9ddf43SLuiz Otavio O Souza #include <dev/iicbus/iiconf.h>
100be9ddf43SLuiz Otavio O Souza #include <dev/ofw/ofw_bus.h>
101be9ddf43SLuiz Otavio O Souza #include <dev/ofw/ofw_bus_subr.h>
102be9ddf43SLuiz Otavio O Souza 
103be9ddf43SLuiz Otavio O Souza #include <arm/broadcom/bcm2835/bcm2835_gpio.h>
104be9ddf43SLuiz Otavio O Souza #include <arm/broadcom/bcm2835/bcm2835_bscreg.h>
105be9ddf43SLuiz Otavio O Souza #include <arm/broadcom/bcm2835/bcm2835_bscvar.h>
106be9ddf43SLuiz Otavio O Souza 
107be9ddf43SLuiz Otavio O Souza #include "iicbus_if.h"
108be9ddf43SLuiz Otavio O Souza 
1099d6eb8bbSOleksandr Tymoshenko static struct ofw_compat_data compat_data[] = {
1109d6eb8bbSOleksandr Tymoshenko 	{"broadcom,bcm2835-bsc",	1},
1119d6eb8bbSOleksandr Tymoshenko 	{"brcm,bcm2708-i2c",		1},
112e9804ab2SOleksandr Tymoshenko 	{"brcm,bcm2835-i2c",		1},
1139d6eb8bbSOleksandr Tymoshenko 	{NULL,				0}
1149d6eb8bbSOleksandr Tymoshenko };
1159d6eb8bbSOleksandr Tymoshenko 
1160bd904edSIan Lepore #define DEVICE_DEBUGF(sc, lvl, fmt, args...) \
1170bd904edSIan Lepore     if ((lvl) <= (sc)->sc_debug) \
1180bd904edSIan Lepore         device_printf((sc)->sc_dev, fmt, ##args)
1190bd904edSIan Lepore 
1200bd904edSIan Lepore #define DEBUGF(sc, lvl, fmt, args...) \
1210bd904edSIan Lepore     if ((lvl) <= (sc)->sc_debug) \
1220bd904edSIan Lepore         printf(fmt, ##args)
1230bd904edSIan Lepore 
124be9ddf43SLuiz Otavio O Souza static void bcm_bsc_intr(void *);
1259e93dfcfSLuiz Otavio O Souza static int bcm_bsc_detach(device_t);
126be9ddf43SLuiz Otavio O Souza 
127be9ddf43SLuiz Otavio O Souza static void
128be9ddf43SLuiz Otavio O Souza bcm_bsc_modifyreg(struct bcm_bsc_softc *sc, uint32_t off, uint32_t mask,
129be9ddf43SLuiz Otavio O Souza 	uint32_t value)
130be9ddf43SLuiz Otavio O Souza {
131be9ddf43SLuiz Otavio O Souza 	uint32_t reg;
132be9ddf43SLuiz Otavio O Souza 
133be9ddf43SLuiz Otavio O Souza 	mtx_assert(&sc->sc_mtx, MA_OWNED);
134be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, off);
135be9ddf43SLuiz Otavio O Souza 	reg &= ~mask;
136be9ddf43SLuiz Otavio O Souza 	reg |= value;
137be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, off, reg);
138be9ddf43SLuiz Otavio O Souza }
139be9ddf43SLuiz Otavio O Souza 
140be9ddf43SLuiz Otavio O Souza static int
141be9ddf43SLuiz Otavio O Souza bcm_bsc_clock_proc(SYSCTL_HANDLER_ARGS)
142be9ddf43SLuiz Otavio O Souza {
143be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
144be9ddf43SLuiz Otavio O Souza 	uint32_t clk;
145be9ddf43SLuiz Otavio O Souza 
146be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
147be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
148be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
149be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
150be9ddf43SLuiz Otavio O Souza 	clk &= 0xffff;
151be9ddf43SLuiz Otavio O Souza 	if (clk == 0)
152be9ddf43SLuiz Otavio O Souza 		clk = 32768;
153be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
154be9ddf43SLuiz Otavio O Souza 
1559e93dfcfSLuiz Otavio O Souza 	return (sysctl_handle_int(oidp, &clk, 0, req));
156be9ddf43SLuiz Otavio O Souza }
157be9ddf43SLuiz Otavio O Souza 
158be9ddf43SLuiz Otavio O Souza static int
159be9ddf43SLuiz Otavio O Souza bcm_bsc_clkt_proc(SYSCTL_HANDLER_ARGS)
160be9ddf43SLuiz Otavio O Souza {
161be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
162be9ddf43SLuiz Otavio O Souza 	uint32_t clkt;
163be9ddf43SLuiz Otavio O Souza 	int error;
164be9ddf43SLuiz Otavio O Souza 
165be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
166be9ddf43SLuiz Otavio O Souza 
167be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
168be9ddf43SLuiz Otavio O Souza 	clkt = BCM_BSC_READ(sc, BCM_BSC_CLKT);
169be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
170be9ddf43SLuiz Otavio O Souza 	clkt &= 0xffff;
171be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &clkt, sizeof(clkt), req);
172be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
173be9ddf43SLuiz Otavio O Souza 		return (error);
174be9ddf43SLuiz Otavio O Souza 
175be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
176be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CLKT, clkt & 0xffff);
177be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
178be9ddf43SLuiz Otavio O Souza 
179be9ddf43SLuiz Otavio O Souza 	return (0);
180be9ddf43SLuiz Otavio O Souza }
181be9ddf43SLuiz Otavio O Souza 
182be9ddf43SLuiz Otavio O Souza static int
183be9ddf43SLuiz Otavio O Souza bcm_bsc_fall_proc(SYSCTL_HANDLER_ARGS)
184be9ddf43SLuiz Otavio O Souza {
185be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
186be9ddf43SLuiz Otavio O Souza 	uint32_t clk, reg;
187be9ddf43SLuiz Otavio O Souza 	int error;
188be9ddf43SLuiz Otavio O Souza 
189be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
190be9ddf43SLuiz Otavio O Souza 
191be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
192be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, BCM_BSC_DELAY);
193be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
194be9ddf43SLuiz Otavio O Souza 	reg >>= 16;
195be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
196be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
197be9ddf43SLuiz Otavio O Souza 		return (error);
198be9ddf43SLuiz Otavio O Souza 
199be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
200be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
201be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
202be9ddf43SLuiz Otavio O Souza 	if (reg > clk / 2)
203be9ddf43SLuiz Otavio O Souza 		reg = clk / 2 - 1;
204be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff0000, reg << 16);
205be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
206be9ddf43SLuiz Otavio O Souza 
207be9ddf43SLuiz Otavio O Souza 	return (0);
208be9ddf43SLuiz Otavio O Souza }
209be9ddf43SLuiz Otavio O Souza 
210be9ddf43SLuiz Otavio O Souza static int
211be9ddf43SLuiz Otavio O Souza bcm_bsc_rise_proc(SYSCTL_HANDLER_ARGS)
212be9ddf43SLuiz Otavio O Souza {
213be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
214be9ddf43SLuiz Otavio O Souza 	uint32_t clk, reg;
215be9ddf43SLuiz Otavio O Souza 	int error;
216be9ddf43SLuiz Otavio O Souza 
217be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
218be9ddf43SLuiz Otavio O Souza 
219be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
220be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, BCM_BSC_DELAY);
221be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
222be9ddf43SLuiz Otavio O Souza 	reg &= 0xffff;
223be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
224be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
225be9ddf43SLuiz Otavio O Souza 		return (error);
226be9ddf43SLuiz Otavio O Souza 
227be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
228be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
229be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
230be9ddf43SLuiz Otavio O Souza 	if (reg > clk / 2)
231be9ddf43SLuiz Otavio O Souza 		reg = clk / 2 - 1;
232be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff, reg);
233be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
234be9ddf43SLuiz Otavio O Souza 
235be9ddf43SLuiz Otavio O Souza 	return (0);
236be9ddf43SLuiz Otavio O Souza }
237be9ddf43SLuiz Otavio O Souza 
238be9ddf43SLuiz Otavio O Souza static void
239be9ddf43SLuiz Otavio O Souza bcm_bsc_sysctl_init(struct bcm_bsc_softc *sc)
240be9ddf43SLuiz Otavio O Souza {
241be9ddf43SLuiz Otavio O Souza 	struct sysctl_ctx_list *ctx;
242be9ddf43SLuiz Otavio O Souza 	struct sysctl_oid *tree_node;
243be9ddf43SLuiz Otavio O Souza 	struct sysctl_oid_list *tree;
244be9ddf43SLuiz Otavio O Souza 
245be9ddf43SLuiz Otavio O Souza 	/*
246be9ddf43SLuiz Otavio O Souza 	 * Add system sysctl tree/handlers.
247be9ddf43SLuiz Otavio O Souza 	 */
248be9ddf43SLuiz Otavio O Souza 	ctx = device_get_sysctl_ctx(sc->sc_dev);
249be9ddf43SLuiz Otavio O Souza 	tree_node = device_get_sysctl_tree(sc->sc_dev);
250be9ddf43SLuiz Otavio O Souza 	tree = SYSCTL_CHILDREN(tree_node);
2519e93dfcfSLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "frequency",
252be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
253be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_clock_proc, "IU", "I2C BUS clock frequency");
254be9ddf43SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clock_stretch",
255be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
256be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_clkt_proc, "IU", "I2C BUS clock stretch timeout");
257be9ddf43SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "fall_edge_delay",
258be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
259be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_fall_proc, "IU", "I2C BUS falling edge delay");
260be9ddf43SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "rise_edge_delay",
261be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
262be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_rise_proc, "IU", "I2C BUS rising edge delay");
2630bd904edSIan Lepore 	SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "debug",
2640bd904edSIan Lepore 	    CTLFLAG_RWTUN, &sc->sc_debug, 0,
2650bd904edSIan Lepore 	    "Enable debug; 1=reads/writes, 2=add starts/stops");
266be9ddf43SLuiz Otavio O Souza }
267be9ddf43SLuiz Otavio O Souza 
268be9ddf43SLuiz Otavio O Souza static void
269be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(struct bcm_bsc_softc *sc)
270be9ddf43SLuiz Otavio O Souza {
271be9ddf43SLuiz Otavio O Souza 
272e50c6241SLuiz Otavio O Souza 	/* Enable the BSC Controller, disable interrupts. */
273e50c6241SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN);
274be9ddf43SLuiz Otavio O Souza 	/* Clear pending interrupts. */
275be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_STATUS, BCM_BSC_STATUS_CLKT |
276be9ddf43SLuiz Otavio O Souza 	    BCM_BSC_STATUS_ERR | BCM_BSC_STATUS_DONE);
277be9ddf43SLuiz Otavio O Souza 	/* Clear the FIFO. */
278be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_CLEAR0,
279be9ddf43SLuiz Otavio O Souza 	    BCM_BSC_CTRL_CLEAR0);
280be9ddf43SLuiz Otavio O Souza }
281be9ddf43SLuiz Otavio O Souza 
282be9ddf43SLuiz Otavio O Souza static int
283be9ddf43SLuiz Otavio O Souza bcm_bsc_probe(device_t dev)
284be9ddf43SLuiz Otavio O Souza {
285be9ddf43SLuiz Otavio O Souza 
286add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
287add35ed5SIan Lepore 		return (ENXIO);
288add35ed5SIan Lepore 
2899d6eb8bbSOleksandr Tymoshenko 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
290be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
291be9ddf43SLuiz Otavio O Souza 
292be9ddf43SLuiz Otavio O Souza 	device_set_desc(dev, "BCM2708/2835 BSC controller");
293be9ddf43SLuiz Otavio O Souza 
294be9ddf43SLuiz Otavio O Souza 	return (BUS_PROBE_DEFAULT);
295be9ddf43SLuiz Otavio O Souza }
296be9ddf43SLuiz Otavio O Souza 
297be9ddf43SLuiz Otavio O Souza static int
298be9ddf43SLuiz Otavio O Souza bcm_bsc_attach(device_t dev)
299be9ddf43SLuiz Otavio O Souza {
300be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
30121cb1342SLuiz Otavio O Souza 	unsigned long start;
302be9ddf43SLuiz Otavio O Souza 	device_t gpio;
30321cb1342SLuiz Otavio O Souza 	int i, rid;
304be9ddf43SLuiz Otavio O Souza 
305be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
306be9ddf43SLuiz Otavio O Souza 	sc->sc_dev = dev;
307be9ddf43SLuiz Otavio O Souza 
308be9ddf43SLuiz Otavio O Souza 	rid = 0;
309be9ddf43SLuiz Otavio O Souza 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
310be9ddf43SLuiz Otavio O Souza 	    RF_ACTIVE);
311be9ddf43SLuiz Otavio O Souza 	if (!sc->sc_mem_res) {
312be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate memory window\n");
313be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
314be9ddf43SLuiz Otavio O Souza 	}
315be9ddf43SLuiz Otavio O Souza 
316be9ddf43SLuiz Otavio O Souza 	sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
317be9ddf43SLuiz Otavio O Souza 	sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
318be9ddf43SLuiz Otavio O Souza 
31921cb1342SLuiz Otavio O Souza 	/* Check the unit we are attaching by its base address. */
32021cb1342SLuiz Otavio O Souza 	start = rman_get_start(sc->sc_mem_res);
32121cb1342SLuiz Otavio O Souza 	for (i = 0; i < nitems(bcm_bsc_pins); i++) {
322b4ae2f3cSLuiz Otavio O Souza 		if (bcm_bsc_pins[i].start == (start & BCM_BSC_BASE_MASK))
32321cb1342SLuiz Otavio O Souza 			break;
32421cb1342SLuiz Otavio O Souza 	}
32521cb1342SLuiz Otavio O Souza 	if (i == nitems(bcm_bsc_pins)) {
32621cb1342SLuiz Otavio O Souza 		device_printf(dev, "only bsc0 and bsc1 are supported\n");
32702a42e68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
32821cb1342SLuiz Otavio O Souza 		return (ENXIO);
32921cb1342SLuiz Otavio O Souza 	}
33021cb1342SLuiz Otavio O Souza 
33121cb1342SLuiz Otavio O Souza 	/*
33221cb1342SLuiz Otavio O Souza 	 * Configure the GPIO pins to ALT0 function to enable BSC control
33321cb1342SLuiz Otavio O Souza 	 * over the pins.
33421cb1342SLuiz Otavio O Souza 	 */
33521cb1342SLuiz Otavio O Souza 	gpio = devclass_get_device(devclass_find("gpio"), 0);
33621cb1342SLuiz Otavio O Souza 	if (!gpio) {
33721cb1342SLuiz Otavio O Souza 		device_printf(dev, "cannot find gpio0\n");
33802a42e68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
33921cb1342SLuiz Otavio O Souza 		return (ENXIO);
34021cb1342SLuiz Otavio O Souza 	}
34121cb1342SLuiz Otavio O Souza 	bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].sda, BCM_GPIO_ALT0);
34221cb1342SLuiz Otavio O Souza 	bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].scl, BCM_GPIO_ALT0);
34321cb1342SLuiz Otavio O Souza 
344be9ddf43SLuiz Otavio O Souza 	rid = 0;
345be9ddf43SLuiz Otavio O Souza 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
346be9ddf43SLuiz Otavio O Souza 	    RF_ACTIVE | RF_SHAREABLE);
347be9ddf43SLuiz Otavio O Souza 	if (!sc->sc_irq_res) {
348be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
349be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate interrupt\n");
350be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
351be9ddf43SLuiz Otavio O Souza 	}
352be9ddf43SLuiz Otavio O Souza 
353be9ddf43SLuiz Otavio O Souza 	/* Hook up our interrupt handler. */
354be9ddf43SLuiz Otavio O Souza 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
355be9ddf43SLuiz Otavio O Souza 	    NULL, bcm_bsc_intr, sc, &sc->sc_intrhand)) {
356be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
357be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
358be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot setup the interrupt handler\n");
359be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
360be9ddf43SLuiz Otavio O Souza 	}
361be9ddf43SLuiz Otavio O Souza 
362be9ddf43SLuiz Otavio O Souza 	mtx_init(&sc->sc_mtx, "bcm_bsc", NULL, MTX_DEF);
363be9ddf43SLuiz Otavio O Souza 
364be9ddf43SLuiz Otavio O Souza 	bcm_bsc_sysctl_init(sc);
365be9ddf43SLuiz Otavio O Souza 
366be9ddf43SLuiz Otavio O Souza 	/* Enable the BSC controller.  Flush the FIFO. */
367be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
368be9ddf43SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
369be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
370be9ddf43SLuiz Otavio O Souza 
3719e93dfcfSLuiz Otavio O Souza 	sc->sc_iicbus = device_add_child(dev, "iicbus", -1);
3729e93dfcfSLuiz Otavio O Souza 	if (sc->sc_iicbus == NULL) {
3739e93dfcfSLuiz Otavio O Souza 		bcm_bsc_detach(dev);
3749e93dfcfSLuiz Otavio O Souza 		return (ENXIO);
3759e93dfcfSLuiz Otavio O Souza 	}
376be9ddf43SLuiz Otavio O Souza 
3771e4042d4SIan Lepore 	/* Probe and attach the iicbus when interrupts are available. */
3781e4042d4SIan Lepore 	config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
3791e4042d4SIan Lepore 
3801e4042d4SIan Lepore 	return (0);
381be9ddf43SLuiz Otavio O Souza }
382be9ddf43SLuiz Otavio O Souza 
383be9ddf43SLuiz Otavio O Souza static int
384be9ddf43SLuiz Otavio O Souza bcm_bsc_detach(device_t dev)
385be9ddf43SLuiz Otavio O Souza {
386be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
387be9ddf43SLuiz Otavio O Souza 
388be9ddf43SLuiz Otavio O Souza 	bus_generic_detach(dev);
389be9ddf43SLuiz Otavio O Souza 
390be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
3910bd904edSIan Lepore 	if (sc->sc_iicbus != NULL)
3920bd904edSIan Lepore 		device_delete_child(dev, sc->sc_iicbus);
393be9ddf43SLuiz Otavio O Souza 	mtx_destroy(&sc->sc_mtx);
394be9ddf43SLuiz Otavio O Souza 	if (sc->sc_intrhand)
395be9ddf43SLuiz Otavio O Souza 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
396be9ddf43SLuiz Otavio O Souza 	if (sc->sc_irq_res)
397be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
398be9ddf43SLuiz Otavio O Souza 	if (sc->sc_mem_res)
399be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
400be9ddf43SLuiz Otavio O Souza 
401be9ddf43SLuiz Otavio O Souza 	return (0);
402be9ddf43SLuiz Otavio O Souza }
403be9ddf43SLuiz Otavio O Souza 
404be9ddf43SLuiz Otavio O Souza static void
4050bd904edSIan Lepore bcm_bsc_empty_rx_fifo(struct bcm_bsc_softc *sc)
4060bd904edSIan Lepore {
4070bd904edSIan Lepore 	uint32_t status;
4080bd904edSIan Lepore 
4090bd904edSIan Lepore 	/* Assumes sc_totlen > 0 and BCM_BSC_STATUS_RXD is asserted on entry. */
4100bd904edSIan Lepore 	do {
4110bd904edSIan Lepore 		if (sc->sc_resid == 0) {
4120bd904edSIan Lepore 			sc->sc_data  = sc->sc_curmsg->buf;
4130bd904edSIan Lepore 			sc->sc_dlen  = sc->sc_curmsg->len;
4140bd904edSIan Lepore 			sc->sc_resid = sc->sc_dlen;
4150bd904edSIan Lepore 			++sc->sc_curmsg;
4160bd904edSIan Lepore 		}
4170bd904edSIan Lepore 		do {
4180bd904edSIan Lepore 			*sc->sc_data = BCM_BSC_READ(sc, BCM_BSC_DATA);
4190bd904edSIan Lepore 			DEBUGF(sc, 1, "0x%02x ", *sc->sc_data);
4200bd904edSIan Lepore 			++sc->sc_data;
4210bd904edSIan Lepore 			--sc->sc_resid;
4220bd904edSIan Lepore 			--sc->sc_totlen;
4230bd904edSIan Lepore 			status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
4240bd904edSIan Lepore 		} while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_RXD));
4250bd904edSIan Lepore 	} while (sc->sc_totlen > 0 && (status & BCM_BSC_STATUS_RXD));
4260bd904edSIan Lepore }
4270bd904edSIan Lepore 
4280bd904edSIan Lepore static void
4290bd904edSIan Lepore bcm_bsc_fill_tx_fifo(struct bcm_bsc_softc *sc)
4300bd904edSIan Lepore {
4310bd904edSIan Lepore 	uint32_t status;
4320bd904edSIan Lepore 
4330bd904edSIan Lepore 	/* Assumes sc_totlen > 0 and BCM_BSC_STATUS_TXD is asserted on entry. */
4340bd904edSIan Lepore 	do {
4350bd904edSIan Lepore 		if (sc->sc_resid == 0) {
4360bd904edSIan Lepore 			sc->sc_data  = sc->sc_curmsg->buf;
4370bd904edSIan Lepore 			sc->sc_dlen  = sc->sc_curmsg->len;
4380bd904edSIan Lepore 			sc->sc_resid = sc->sc_dlen;
4390bd904edSIan Lepore 			++sc->sc_curmsg;
4400bd904edSIan Lepore 		}
4410bd904edSIan Lepore 		do {
4420bd904edSIan Lepore 			BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data);
4430bd904edSIan Lepore 			DEBUGF(sc, 1, "0x%02x ", *sc->sc_data);
4440bd904edSIan Lepore 			++sc->sc_data;
4450bd904edSIan Lepore 			--sc->sc_resid;
4460bd904edSIan Lepore 			--sc->sc_totlen;
4470bd904edSIan Lepore 			status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
4480bd904edSIan Lepore 		} while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_TXD));
4490bd904edSIan Lepore 		/*
4500bd904edSIan Lepore 		 * If a repeat-start was pending and we just hit the end of a tx
4510bd904edSIan Lepore 		 * buffer, see if it's also the end of the writes that preceeded
4520bd904edSIan Lepore 		 * the repeat-start.  If so, log the repeat-start and the start
4530bd904edSIan Lepore 		 * of the following read, and return because we're not writing
4540bd904edSIan Lepore 		 * anymore (and TXD will be true because there's room to write
4550bd904edSIan Lepore 		 * in the fifo).
4560bd904edSIan Lepore 		 */
4570bd904edSIan Lepore 		if (sc->sc_replen > 0 && sc->sc_resid == 0) {
4580bd904edSIan Lepore 			sc->sc_replen -= sc->sc_dlen;
4590bd904edSIan Lepore 			if (sc->sc_replen == 0) {
4600bd904edSIan Lepore 				DEBUGF(sc, 1, " err=0\n");
4610bd904edSIan Lepore 				DEVICE_DEBUGF(sc, 2, "rstart 0x%02x\n",
4620bd904edSIan Lepore 				    sc->sc_curmsg->slave | 0x01);
4630bd904edSIan Lepore 				DEVICE_DEBUGF(sc, 1,
4640bd904edSIan Lepore 				    "read   0x%02x len %d: ",
4650bd904edSIan Lepore 				    sc->sc_curmsg->slave | 0x01,
4660bd904edSIan Lepore 				    sc->sc_totlen);
4670bd904edSIan Lepore 				sc->sc_flags |= BCM_I2C_READ;
4680bd904edSIan Lepore 				return;
4690bd904edSIan Lepore 			}
4700bd904edSIan Lepore 		}
4710bd904edSIan Lepore 	} while (sc->sc_totlen > 0 && (status & BCM_BSC_STATUS_TXD));
4720bd904edSIan Lepore }
4730bd904edSIan Lepore 
4740bd904edSIan Lepore static void
475be9ddf43SLuiz Otavio O Souza bcm_bsc_intr(void *arg)
476be9ddf43SLuiz Otavio O Souza {
477be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
478be9ddf43SLuiz Otavio O Souza 	uint32_t status;
479be9ddf43SLuiz Otavio O Souza 
480be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg;
481be9ddf43SLuiz Otavio O Souza 
482be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
483be9ddf43SLuiz Otavio O Souza 
484be9ddf43SLuiz Otavio O Souza 	/* The I2C interrupt is shared among all the BSC controllers. */
485be9ddf43SLuiz Otavio O Souza 	if ((sc->sc_flags & BCM_I2C_BUSY) == 0) {
486be9ddf43SLuiz Otavio O Souza 		BCM_BSC_UNLOCK(sc);
487be9ddf43SLuiz Otavio O Souza 		return;
488be9ddf43SLuiz Otavio O Souza 	}
489be9ddf43SLuiz Otavio O Souza 
490be9ddf43SLuiz Otavio O Souza 	status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
4910bd904edSIan Lepore 	DEBUGF(sc, 4, " <intrstatus=0x%08x> ", status);
492be9ddf43SLuiz Otavio O Souza 
4930bd904edSIan Lepore 	/* RXD and DONE can assert together, empty fifo before checking done. */
4940bd904edSIan Lepore 	if ((sc->sc_flags & BCM_I2C_READ) && (status & BCM_BSC_STATUS_RXD))
4950bd904edSIan Lepore 		bcm_bsc_empty_rx_fifo(sc);
4960bd904edSIan Lepore 
4970bd904edSIan Lepore 	/* Check for completion. */
4980bd904edSIan Lepore 	if (status & (BCM_BSC_STATUS_ERRBITS | BCM_BSC_STATUS_DONE)) {
4990bd904edSIan Lepore 		sc->sc_flags |= BCM_I2C_DONE;
5000bd904edSIan Lepore 		if (status & BCM_BSC_STATUS_ERRBITS)
501e50c6241SLuiz Otavio O Souza 			sc->sc_flags |= BCM_I2C_ERROR;
502be9ddf43SLuiz Otavio O Souza 		/* Disable interrupts. */
503be9ddf43SLuiz Otavio O Souza 		bcm_bsc_reset(sc);
5040bd904edSIan Lepore 		wakeup(sc);
5050bd904edSIan Lepore 	} else if (!(sc->sc_flags & BCM_I2C_READ)) {
5060bd904edSIan Lepore 		/*
5070bd904edSIan Lepore 		 * Don't check for TXD until after determining whether the
5080bd904edSIan Lepore 		 * transfer is complete; TXD will be asserted along with ERR or
5090bd904edSIan Lepore 		 * DONE if there is room in the fifo.
5100bd904edSIan Lepore 		 */
5110bd904edSIan Lepore 		if (status & BCM_BSC_STATUS_TXD)
5120bd904edSIan Lepore 			bcm_bsc_fill_tx_fifo(sc);
513be9ddf43SLuiz Otavio O Souza 	}
514be9ddf43SLuiz Otavio O Souza 
515be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
516be9ddf43SLuiz Otavio O Souza }
517be9ddf43SLuiz Otavio O Souza 
518be9ddf43SLuiz Otavio O Souza static int
519be9ddf43SLuiz Otavio O Souza bcm_bsc_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
520be9ddf43SLuiz Otavio O Souza {
521be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
5220bd904edSIan Lepore 	struct iic_msg *endmsgs, *nxtmsg;
5230bd904edSIan Lepore 	uint32_t readctl, status;
5240bd904edSIan Lepore 	int err;
5250bd904edSIan Lepore 	uint16_t curlen;
5260bd904edSIan Lepore 	uint8_t curisread, curslave, nxtisread, nxtslave;
527be9ddf43SLuiz Otavio O Souza 
528be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
529be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
530be9ddf43SLuiz Otavio O Souza 
531be9ddf43SLuiz Otavio O Souza 	/* If the controller is busy wait until it is available. */
532be9ddf43SLuiz Otavio O Souza 	while (sc->sc_flags & BCM_I2C_BUSY)
533a141b672SLuiz Otavio O Souza 		mtx_sleep(dev, &sc->sc_mtx, 0, "bscbusw", 0);
534be9ddf43SLuiz Otavio O Souza 
535be9ddf43SLuiz Otavio O Souza 	/* Now we have control over the BSC controller. */
536be9ddf43SLuiz Otavio O Souza 	sc->sc_flags = BCM_I2C_BUSY;
537be9ddf43SLuiz Otavio O Souza 
5380bd904edSIan Lepore 	DEVICE_DEBUGF(sc, 3, "Transfer %d msgs\n", nmsgs);
5390bd904edSIan Lepore 
540be9ddf43SLuiz Otavio O Souza 	/* Clear the FIFO and the pending interrupts. */
541be9ddf43SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
542be9ddf43SLuiz Otavio O Souza 
5430bd904edSIan Lepore 	/*
5440bd904edSIan Lepore 	 * Perform all the transfers requested in the array of msgs.  Note that
5450bd904edSIan Lepore 	 * it is bcm_bsc_empty_rx_fifo() and bcm_bsc_fill_tx_fifo() that advance
5460bd904edSIan Lepore 	 * sc->sc_curmsg through the array of messages, as the data from each
5470bd904edSIan Lepore 	 * message is fully consumed, but it is this loop that notices when we
5480bd904edSIan Lepore 	 * have no more messages to process.
5490bd904edSIan Lepore 	 */
550be9ddf43SLuiz Otavio O Souza 	err = 0;
5510bd904edSIan Lepore 	sc->sc_resid = 0;
5520bd904edSIan Lepore 	sc->sc_curmsg = msgs;
5530bd904edSIan Lepore 	endmsgs = &msgs[nmsgs];
5540bd904edSIan Lepore 	while (sc->sc_curmsg < endmsgs) {
5550bd904edSIan Lepore 		readctl = 0;
5560bd904edSIan Lepore 		curslave = sc->sc_curmsg->slave >> 1;
5570bd904edSIan Lepore 		curisread = sc->sc_curmsg->flags & IIC_M_RD;
5580bd904edSIan Lepore 		sc->sc_replen = 0;
5590bd904edSIan Lepore 		sc->sc_totlen = sc->sc_curmsg->len;
5600bd904edSIan Lepore 		/*
5610bd904edSIan Lepore 		 * Scan for scatter/gather IO (same slave and direction) or
5620bd904edSIan Lepore 		 * repeat-start (read following write for the same slave).
5630bd904edSIan Lepore 		 */
5640bd904edSIan Lepore 		for (nxtmsg = sc->sc_curmsg + 1; nxtmsg < endmsgs; ++nxtmsg) {
5650bd904edSIan Lepore 			nxtslave = nxtmsg->slave >> 1;
5660bd904edSIan Lepore 			if (curslave == nxtslave) {
5670bd904edSIan Lepore 				nxtisread = nxtmsg->flags & IIC_M_RD;
5680bd904edSIan Lepore 				if (curisread == nxtisread) {
5690bd904edSIan Lepore 					/*
5700bd904edSIan Lepore 					 * Same slave and direction, this
5710bd904edSIan Lepore 					 * message will be part of the same
5720bd904edSIan Lepore 					 * transfer as the previous one.
5730bd904edSIan Lepore 					 */
5740bd904edSIan Lepore 					sc->sc_totlen += nxtmsg->len;
5750bd904edSIan Lepore 					continue;
5760bd904edSIan Lepore 				} else if (curisread == IIC_M_WR) {
5770bd904edSIan Lepore 					/*
5780bd904edSIan Lepore 					 * Read after write to same slave means
5790bd904edSIan Lepore 					 * repeat-start, remember how many bytes
5800bd904edSIan Lepore 					 * come before the repeat-start, switch
5810bd904edSIan Lepore 					 * the direction to IIC_M_RD, and gather
5820bd904edSIan Lepore 					 * up following reads to the same slave.
5830bd904edSIan Lepore 					 */
5840bd904edSIan Lepore 					curisread = IIC_M_RD;
5850bd904edSIan Lepore 					sc->sc_replen = sc->sc_totlen;
5860bd904edSIan Lepore 					sc->sc_totlen += nxtmsg->len;
5870bd904edSIan Lepore 					continue;
5880bd904edSIan Lepore 				}
5890bd904edSIan Lepore 			}
5900bd904edSIan Lepore 			break;
5910bd904edSIan Lepore 		}
5920bd904edSIan Lepore 
5930bd904edSIan Lepore 		/*
5940bd904edSIan Lepore 		 * curslave and curisread temporaries from above may refer to
5950bd904edSIan Lepore 		 * the after-repstart msg, reset them to reflect sc_curmsg.
5960bd904edSIan Lepore 		 */
5970bd904edSIan Lepore 		curisread = (sc->sc_curmsg->flags & IIC_M_RD) ? 1 : 0;
5980bd904edSIan Lepore 		curslave = sc->sc_curmsg->slave | curisread;
599be9ddf43SLuiz Otavio O Souza 
600be9ddf43SLuiz Otavio O Souza 		/* Write the slave address. */
6010bd904edSIan Lepore 		BCM_BSC_WRITE(sc, BCM_BSC_SLAVE, curslave >> 1);
602be9ddf43SLuiz Otavio O Souza 
6030bd904edSIan Lepore 		DEVICE_DEBUGF(sc, 2, "start  0x%02x\n", curslave);
604be9ddf43SLuiz Otavio O Souza 
6050bd904edSIan Lepore 		/*
6060bd904edSIan Lepore 		 * Either set up read length and direction variables for a
6070bd904edSIan Lepore 		 * simple transfer or get the hardware started on the first
6080bd904edSIan Lepore 		 * piece of a transfer that involves a repeat-start and set up
6090bd904edSIan Lepore 		 * the read length and direction vars for the second piece.
6100bd904edSIan Lepore 		 */
6110bd904edSIan Lepore 		if (sc->sc_replen == 0) {
6120bd904edSIan Lepore 			DEVICE_DEBUGF(sc, 1, "%-6s 0x%02x len %d: ",
613*ae992394SIan Lepore 			    (curisread) ? "read" : "write", curslave,
6140bd904edSIan Lepore 			    sc->sc_totlen);
6150bd904edSIan Lepore 			curlen = sc->sc_totlen;
6160bd904edSIan Lepore 			if (curisread) {
6170bd904edSIan Lepore 				readctl = BCM_BSC_CTRL_READ;
618be9ddf43SLuiz Otavio O Souza 				sc->sc_flags |= BCM_I2C_READ;
6190bd904edSIan Lepore 			} else {
6200bd904edSIan Lepore 				readctl = 0;
6210bd904edSIan Lepore 				sc->sc_flags &= ~BCM_I2C_READ;
622be9ddf43SLuiz Otavio O Souza 			}
6230bd904edSIan Lepore 		} else {
6240bd904edSIan Lepore 			DEVICE_DEBUGF(sc, 1, "%-6s 0x%02x len %d: ",
625*ae992394SIan Lepore 			    (curisread) ? "read" : "write", curslave,
6260bd904edSIan Lepore 			    sc->sc_replen);
627be9ddf43SLuiz Otavio O Souza 
6280bd904edSIan Lepore 			/*
6290bd904edSIan Lepore 			 * Start the write transfer with an empty fifo and wait
6300bd904edSIan Lepore 			 * for the 'transfer active' status bit to light up;
6310bd904edSIan Lepore 			 * that indicates that the hardware has latched the
6320bd904edSIan Lepore 			 * direction and length for the write, and we can safely
6330bd904edSIan Lepore 			 * reload those registers and issue the start for the
6340bd904edSIan Lepore 			 * following read; interrupts are not enabled here.
6350bd904edSIan Lepore 			 */
6360bd904edSIan Lepore 			BCM_BSC_WRITE(sc, BCM_BSC_DLEN, sc->sc_replen);
637be9ddf43SLuiz Otavio O Souza 			BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN |
6380bd904edSIan Lepore 			    BCM_BSC_CTRL_ST);
6390bd904edSIan Lepore 			do {
6400bd904edSIan Lepore 				status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
6410bd904edSIan Lepore 				if (status & BCM_BSC_STATUS_ERR) {
6420bd904edSIan Lepore 					/* no ACK on slave addr */
6430bd904edSIan Lepore 					err = EIO;
6440bd904edSIan Lepore 					goto xfer_done;
6450bd904edSIan Lepore 				}
6460bd904edSIan Lepore 			} while ((status & BCM_BSC_STATUS_TA) == 0);
6470bd904edSIan Lepore 			/*
6480bd904edSIan Lepore 			 * Set curlen and readctl for the repeat-start read that
6490bd904edSIan Lepore 			 * we need to set up below, but set sc_flags to write,
6500bd904edSIan Lepore 			 * because that is the operation in progress right now.
6510bd904edSIan Lepore 			 */
6520bd904edSIan Lepore 			curlen = sc->sc_totlen - sc->sc_replen;
6530bd904edSIan Lepore 			readctl = BCM_BSC_CTRL_READ;
6540bd904edSIan Lepore 			sc->sc_flags &= ~BCM_I2C_READ;
6550bd904edSIan Lepore 		}
6560bd904edSIan Lepore 
6570bd904edSIan Lepore 		/*
6580bd904edSIan Lepore 		 * Start the transfer with interrupts enabled, then if doing a
6590bd904edSIan Lepore 		 * write, fill the tx fifo.  Not prefilling the fifo until after
6600bd904edSIan Lepore 		 * this start command is the key workaround for making
6610bd904edSIan Lepore 		 * repeat-start work, and it's harmless to do it in this order
6620bd904edSIan Lepore 		 * for a regular write too.
6630bd904edSIan Lepore 		 */
6640bd904edSIan Lepore 		BCM_BSC_WRITE(sc, BCM_BSC_DLEN, curlen);
6650bd904edSIan Lepore 		BCM_BSC_WRITE(sc, BCM_BSC_CTRL, readctl | BCM_BSC_CTRL_I2CEN |
6660bd904edSIan Lepore 		    BCM_BSC_CTRL_ST | BCM_BSC_CTRL_INT_ALL);
6670bd904edSIan Lepore 
6680bd904edSIan Lepore 		if (!(sc->sc_curmsg->flags & IIC_M_RD)) {
6690bd904edSIan Lepore 			bcm_bsc_fill_tx_fifo(sc);
6700bd904edSIan Lepore 		}
671be9ddf43SLuiz Otavio O Souza 
672be9ddf43SLuiz Otavio O Souza 		/* Wait for the transaction to complete. */
6730bd904edSIan Lepore 		while (err == 0 && !(sc->sc_flags & BCM_I2C_DONE)) {
6740bd904edSIan Lepore 			err = mtx_sleep(sc, &sc->sc_mtx, 0, "bsciow", hz);
6750bd904edSIan Lepore 		}
67671b37cb9SLuiz Otavio O Souza 		/* Check for errors. */
6770a39cc71SLuiz Otavio O Souza 		if (err == 0 && (sc->sc_flags & BCM_I2C_ERROR))
678be9ddf43SLuiz Otavio O Souza 			err = EIO;
6790bd904edSIan Lepore xfer_done:
6800bd904edSIan Lepore 		DEBUGF(sc, 1, " err=%d\n", err);
6810bd904edSIan Lepore 		DEVICE_DEBUGF(sc, 2, "stop\n");
68271b37cb9SLuiz Otavio O Souza 		if (err != 0)
683be9ddf43SLuiz Otavio O Souza 			break;
684be9ddf43SLuiz Otavio O Souza 	}
685be9ddf43SLuiz Otavio O Souza 
6860bd904edSIan Lepore 	/* Disable interrupts, clean fifo, etc. */
6870bd904edSIan Lepore 	bcm_bsc_reset(sc);
6880bd904edSIan Lepore 
689be9ddf43SLuiz Otavio O Souza 	/* Clean the controller flags. */
690be9ddf43SLuiz Otavio O Souza 	sc->sc_flags = 0;
691be9ddf43SLuiz Otavio O Souza 
692a141b672SLuiz Otavio O Souza 	/* Wake up the threads waiting for bus. */
693a141b672SLuiz Otavio O Souza 	wakeup(dev);
694a141b672SLuiz Otavio O Souza 
695be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
696be9ddf43SLuiz Otavio O Souza 
697be9ddf43SLuiz Otavio O Souza 	return (err);
698be9ddf43SLuiz Otavio O Souza }
699be9ddf43SLuiz Otavio O Souza 
700e50c6241SLuiz Otavio O Souza static int
701e50c6241SLuiz Otavio O Souza bcm_bsc_iicbus_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
702e50c6241SLuiz Otavio O Souza {
703e50c6241SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
7049e93dfcfSLuiz Otavio O Souza 	uint32_t busfreq;
705e50c6241SLuiz Otavio O Souza 
706e50c6241SLuiz Otavio O Souza 	sc = device_get_softc(dev);
707e50c6241SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
708e50c6241SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
7099e93dfcfSLuiz Otavio O Souza 	if (sc->sc_iicbus == NULL)
7109e93dfcfSLuiz Otavio O Souza 		busfreq = 100000;
7119e93dfcfSLuiz Otavio O Souza 	else
7129e93dfcfSLuiz Otavio O Souza 		busfreq = IICBUS_GET_FREQUENCY(sc->sc_iicbus, speed);
7139e93dfcfSLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CLOCK, BCM_BSC_CORE_CLK / busfreq);
714e50c6241SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
715e50c6241SLuiz Otavio O Souza 
716e50c6241SLuiz Otavio O Souza 	return (IIC_ENOADDR);
717e50c6241SLuiz Otavio O Souza }
718e50c6241SLuiz Otavio O Souza 
719be9ddf43SLuiz Otavio O Souza static phandle_t
720be9ddf43SLuiz Otavio O Souza bcm_bsc_get_node(device_t bus, device_t dev)
721be9ddf43SLuiz Otavio O Souza {
722be9ddf43SLuiz Otavio O Souza 
723be9ddf43SLuiz Otavio O Souza 	/* We only have one child, the I2C bus, which needs our own node. */
724be9ddf43SLuiz Otavio O Souza 	return (ofw_bus_get_node(bus));
725be9ddf43SLuiz Otavio O Souza }
726be9ddf43SLuiz Otavio O Souza 
727be9ddf43SLuiz Otavio O Souza static device_method_t bcm_bsc_methods[] = {
728be9ddf43SLuiz Otavio O Souza 	/* Device interface */
729be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_probe,		bcm_bsc_probe),
730be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_attach,	bcm_bsc_attach),
731be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_detach,	bcm_bsc_detach),
732be9ddf43SLuiz Otavio O Souza 
733be9ddf43SLuiz Otavio O Souza 	/* iicbus interface */
734e50c6241SLuiz Otavio O Souza 	DEVMETHOD(iicbus_reset,		bcm_bsc_iicbus_reset),
735be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(iicbus_callback,	iicbus_null_callback),
736be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(iicbus_transfer,	bcm_bsc_transfer),
737be9ddf43SLuiz Otavio O Souza 
738be9ddf43SLuiz Otavio O Souza 	/* ofw_bus interface */
739be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(ofw_bus_get_node,	bcm_bsc_get_node),
740be9ddf43SLuiz Otavio O Souza 
741be9ddf43SLuiz Otavio O Souza 	DEVMETHOD_END
742be9ddf43SLuiz Otavio O Souza };
743be9ddf43SLuiz Otavio O Souza 
744be9ddf43SLuiz Otavio O Souza static devclass_t bcm_bsc_devclass;
745be9ddf43SLuiz Otavio O Souza 
746be9ddf43SLuiz Otavio O Souza static driver_t bcm_bsc_driver = {
747be9ddf43SLuiz Otavio O Souza 	"iichb",
748be9ddf43SLuiz Otavio O Souza 	bcm_bsc_methods,
749be9ddf43SLuiz Otavio O Souza 	sizeof(struct bcm_bsc_softc),
750be9ddf43SLuiz Otavio O Souza };
751be9ddf43SLuiz Otavio O Souza 
752be9ddf43SLuiz Otavio O Souza DRIVER_MODULE(iicbus, bcm2835_bsc, iicbus_driver, iicbus_devclass, 0, 0);
753be9ddf43SLuiz Otavio O Souza DRIVER_MODULE(bcm2835_bsc, simplebus, bcm_bsc_driver, bcm_bsc_devclass, 0, 0);
754