xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_bsc.c (revision 685dc743)
1be9ddf43SLuiz Otavio O Souza /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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>
330bd904edSIan Lepore /*
340bd904edSIan Lepore  * Driver for bcm2835 i2c-compatible two-wire bus, named 'BSC' on this SoC.
350bd904edSIan Lepore  *
360bd904edSIan Lepore  * This controller can only perform complete transfers, it does not provide
370bd904edSIan Lepore  * low-level control over sending start/repeat-start/stop sequences on the bus.
380bd904edSIan Lepore  * In addition, bugs in the silicon make it somewhat difficult to perform a
390bd904edSIan Lepore  * repeat-start, and limit the repeat-start to a read following a write on
400bd904edSIan Lepore  * the same slave device.  (The i2c protocol allows a repeat start to change
410bd904edSIan Lepore  * direction or not, and change slave address or not at any time.)
420bd904edSIan Lepore  *
430bd904edSIan Lepore  * The repeat-start bug and workaround are described in a problem report at
440bd904edSIan Lepore  * https://github.com/raspberrypi/linux/issues/254 with the crucial part being
450bd904edSIan Lepore  * in a comment block from a fragment of a GPU i2c driver, containing this:
460bd904edSIan Lepore  *
470bd904edSIan Lepore  * -----------------------------------------------------------------------------
480bd904edSIan Lepore  * - See i2c.v: The I2C peripheral samples the values for rw_bit and xfer_count
490bd904edSIan Lepore  * - in the IDLE state if start is set.
500bd904edSIan Lepore  * -
510bd904edSIan Lepore  * - We want to generate a ReSTART not a STOP at the end of the TX phase. In
520bd904edSIan Lepore  * - order to do that we must ensure the state machine goes RACK1 -> RACK2 ->
530bd904edSIan Lepore  * - SRSTRT1 (not RACK1 -> RACK2 -> SSTOP1).
540bd904edSIan Lepore  * -
550bd904edSIan Lepore  * - So, in the RACK2 state when (TX) xfer_count==0 we must therefore have
560bd904edSIan Lepore  * - already set, ready to be sampled:
570bd904edSIan Lepore  * -  READ ; rw_bit     <= I2CC bit 0 -- must be "read"
580bd904edSIan Lepore  * -  ST;    start      <= I2CC bit 7 -- must be "Go" in order to not issue STOP
590bd904edSIan Lepore  * -  DLEN;  xfer_count <= I2CDLEN    -- must be equal to our read amount
600bd904edSIan Lepore  * -
610bd904edSIan Lepore  * - The plan to do this is:
620bd904edSIan Lepore  * -  1. Start the sub-address write, but don't let it finish
630bd904edSIan Lepore  * -     (keep xfer_count > 0)
640bd904edSIan Lepore  * -  2. Populate READ, DLEN and ST in preparation for ReSTART read sequence
650bd904edSIan Lepore  * -  3. Let TX finish (write the rest of the data)
660bd904edSIan Lepore  * -  4. Read back data as it arrives
670bd904edSIan Lepore  * -----------------------------------------------------------------------------
680bd904edSIan Lepore  *
690bd904edSIan Lepore  * The transfer function below scans the list of messages passed to it, looking
700bd904edSIan Lepore  * for a read following a write to the same slave.  When it finds that, it
710bd904edSIan Lepore  * starts the write without prefilling the tx fifo, which holds xfer_count>0,
720bd904edSIan Lepore  * then presets the direction, length, and start command for the following read,
730bd904edSIan Lepore  * as described above.  Then the tx fifo is filled and the rest of the transfer
740bd904edSIan Lepore  * proceeds as normal, with the controller automatically supplying a
750bd904edSIan Lepore  * repeat-start on the bus when the write operation finishes.
760bd904edSIan Lepore  *
770bd904edSIan Lepore  * XXX I suspect the controller may be able to do a repeat-start on any
780bd904edSIan Lepore  * write->read or write->write transition, even when the slave addresses differ.
790bd904edSIan Lepore  * It's unclear whether the slave address can be prestaged along with the
800bd904edSIan Lepore  * direction and length while the write xfer_count is being held at zero.  In
810bd904edSIan Lepore  * fact, if it can't do this, then it couldn't be used to read EDID data.
820bd904edSIan Lepore  */
830bd904edSIan 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_bscreg.h>
102be9ddf43SLuiz Otavio O Souza #include <arm/broadcom/bcm2835/bcm2835_bscvar.h>
103be9ddf43SLuiz Otavio O Souza 
104be9ddf43SLuiz Otavio O Souza #include "iicbus_if.h"
105be9ddf43SLuiz Otavio O Souza 
1069d6eb8bbSOleksandr Tymoshenko static struct ofw_compat_data compat_data[] = {
1079d6eb8bbSOleksandr Tymoshenko 	{"broadcom,bcm2835-bsc",	1},
1089d6eb8bbSOleksandr Tymoshenko 	{"brcm,bcm2708-i2c",		1},
109e9804ab2SOleksandr Tymoshenko 	{"brcm,bcm2835-i2c",		1},
1109d6eb8bbSOleksandr Tymoshenko 	{NULL,				0}
1119d6eb8bbSOleksandr Tymoshenko };
1129d6eb8bbSOleksandr Tymoshenko 
1130bd904edSIan Lepore #define DEVICE_DEBUGF(sc, lvl, fmt, args...) \
1140bd904edSIan Lepore     if ((lvl) <= (sc)->sc_debug) \
1150bd904edSIan Lepore         device_printf((sc)->sc_dev, fmt, ##args)
1160bd904edSIan Lepore 
1170bd904edSIan Lepore #define DEBUGF(sc, lvl, fmt, args...) \
1180bd904edSIan Lepore     if ((lvl) <= (sc)->sc_debug) \
1190bd904edSIan Lepore         printf(fmt, ##args)
1200bd904edSIan Lepore 
121be9ddf43SLuiz Otavio O Souza static void bcm_bsc_intr(void *);
1229e93dfcfSLuiz Otavio O Souza static int bcm_bsc_detach(device_t);
123be9ddf43SLuiz Otavio O Souza 
124be9ddf43SLuiz Otavio O Souza static void
bcm_bsc_modifyreg(struct bcm_bsc_softc * sc,uint32_t off,uint32_t mask,uint32_t value)125be9ddf43SLuiz Otavio O Souza bcm_bsc_modifyreg(struct bcm_bsc_softc *sc, uint32_t off, uint32_t mask,
126be9ddf43SLuiz Otavio O Souza 	uint32_t value)
127be9ddf43SLuiz Otavio O Souza {
128be9ddf43SLuiz Otavio O Souza 	uint32_t reg;
129be9ddf43SLuiz Otavio O Souza 
130be9ddf43SLuiz Otavio O Souza 	mtx_assert(&sc->sc_mtx, MA_OWNED);
131be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, off);
132be9ddf43SLuiz Otavio O Souza 	reg &= ~mask;
133be9ddf43SLuiz Otavio O Souza 	reg |= value;
134be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, off, reg);
135be9ddf43SLuiz Otavio O Souza }
136be9ddf43SLuiz Otavio O Souza 
137be9ddf43SLuiz Otavio O Souza static int
bcm_bsc_clock_proc(SYSCTL_HANDLER_ARGS)138be9ddf43SLuiz Otavio O Souza bcm_bsc_clock_proc(SYSCTL_HANDLER_ARGS)
139be9ddf43SLuiz Otavio O Souza {
140be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
141be9ddf43SLuiz Otavio O Souza 	uint32_t clk;
142be9ddf43SLuiz Otavio O Souza 
143be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
144be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
145be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
146be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
147be9ddf43SLuiz Otavio O Souza 	clk &= 0xffff;
148be9ddf43SLuiz Otavio O Souza 	if (clk == 0)
149be9ddf43SLuiz Otavio O Souza 		clk = 32768;
150be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
151be9ddf43SLuiz Otavio O Souza 
1529e93dfcfSLuiz Otavio O Souza 	return (sysctl_handle_int(oidp, &clk, 0, req));
153be9ddf43SLuiz Otavio O Souza }
154be9ddf43SLuiz Otavio O Souza 
155be9ddf43SLuiz Otavio O Souza static int
bcm_bsc_clkt_proc(SYSCTL_HANDLER_ARGS)156be9ddf43SLuiz Otavio O Souza bcm_bsc_clkt_proc(SYSCTL_HANDLER_ARGS)
157be9ddf43SLuiz Otavio O Souza {
158be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
159be9ddf43SLuiz Otavio O Souza 	uint32_t clkt;
160be9ddf43SLuiz Otavio O Souza 	int error;
161be9ddf43SLuiz Otavio O Souza 
162be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
163be9ddf43SLuiz Otavio O Souza 
164be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
165be9ddf43SLuiz Otavio O Souza 	clkt = BCM_BSC_READ(sc, BCM_BSC_CLKT);
166be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
167be9ddf43SLuiz Otavio O Souza 	clkt &= 0xffff;
168be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &clkt, sizeof(clkt), req);
169be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
170be9ddf43SLuiz Otavio O Souza 		return (error);
171be9ddf43SLuiz Otavio O Souza 
172be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
173be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CLKT, clkt & 0xffff);
174be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
175be9ddf43SLuiz Otavio O Souza 
176be9ddf43SLuiz Otavio O Souza 	return (0);
177be9ddf43SLuiz Otavio O Souza }
178be9ddf43SLuiz Otavio O Souza 
179be9ddf43SLuiz Otavio O Souza static int
bcm_bsc_fall_proc(SYSCTL_HANDLER_ARGS)180be9ddf43SLuiz Otavio O Souza bcm_bsc_fall_proc(SYSCTL_HANDLER_ARGS)
181be9ddf43SLuiz Otavio O Souza {
182be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
183be9ddf43SLuiz Otavio O Souza 	uint32_t clk, reg;
184be9ddf43SLuiz Otavio O Souza 	int error;
185be9ddf43SLuiz Otavio O Souza 
186be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
187be9ddf43SLuiz Otavio O Souza 
188be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
189be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, BCM_BSC_DELAY);
190be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
191be9ddf43SLuiz Otavio O Souza 	reg >>= 16;
192be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
193be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
194be9ddf43SLuiz Otavio O Souza 		return (error);
195be9ddf43SLuiz Otavio O Souza 
196be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
197be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
198be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
199be9ddf43SLuiz Otavio O Souza 	if (reg > clk / 2)
200be9ddf43SLuiz Otavio O Souza 		reg = clk / 2 - 1;
201be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff0000, reg << 16);
202be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
203be9ddf43SLuiz Otavio O Souza 
204be9ddf43SLuiz Otavio O Souza 	return (0);
205be9ddf43SLuiz Otavio O Souza }
206be9ddf43SLuiz Otavio O Souza 
207be9ddf43SLuiz Otavio O Souza static int
bcm_bsc_rise_proc(SYSCTL_HANDLER_ARGS)208be9ddf43SLuiz Otavio O Souza bcm_bsc_rise_proc(SYSCTL_HANDLER_ARGS)
209be9ddf43SLuiz Otavio O Souza {
210be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
211be9ddf43SLuiz Otavio O Souza 	uint32_t clk, reg;
212be9ddf43SLuiz Otavio O Souza 	int error;
213be9ddf43SLuiz Otavio O Souza 
214be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
215be9ddf43SLuiz Otavio O Souza 
216be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
217be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, BCM_BSC_DELAY);
218be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
219be9ddf43SLuiz Otavio O Souza 	reg &= 0xffff;
220be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
221be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
222be9ddf43SLuiz Otavio O Souza 		return (error);
223be9ddf43SLuiz Otavio O Souza 
224be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
225be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
226be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
227be9ddf43SLuiz Otavio O Souza 	if (reg > clk / 2)
228be9ddf43SLuiz Otavio O Souza 		reg = clk / 2 - 1;
229be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff, reg);
230be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
231be9ddf43SLuiz Otavio O Souza 
232be9ddf43SLuiz Otavio O Souza 	return (0);
233be9ddf43SLuiz Otavio O Souza }
234be9ddf43SLuiz Otavio O Souza 
235be9ddf43SLuiz Otavio O Souza static void
bcm_bsc_sysctl_init(struct bcm_bsc_softc * sc)236be9ddf43SLuiz Otavio O Souza bcm_bsc_sysctl_init(struct bcm_bsc_softc *sc)
237be9ddf43SLuiz Otavio O Souza {
238be9ddf43SLuiz Otavio O Souza 	struct sysctl_ctx_list *ctx;
239be9ddf43SLuiz Otavio O Souza 	struct sysctl_oid *tree_node;
240be9ddf43SLuiz Otavio O Souza 	struct sysctl_oid_list *tree;
241be9ddf43SLuiz Otavio O Souza 
242be9ddf43SLuiz Otavio O Souza 	/*
243be9ddf43SLuiz Otavio O Souza 	 * Add system sysctl tree/handlers.
244be9ddf43SLuiz Otavio O Souza 	 */
245be9ddf43SLuiz Otavio O Souza 	ctx = device_get_sysctl_ctx(sc->sc_dev);
246be9ddf43SLuiz Otavio O Souza 	tree_node = device_get_sysctl_tree(sc->sc_dev);
247be9ddf43SLuiz Otavio O Souza 	tree = SYSCTL_CHILDREN(tree_node);
2489e93dfcfSLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "frequency",
2497029da5cSPawel Biernacki 	    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,
2507029da5cSPawel Biernacki 	    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",
2537029da5cSPawel Biernacki 	    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,
2547029da5cSPawel Biernacki 	    sc, sizeof(*sc),
255be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_clkt_proc, "IU", "I2C BUS clock stretch timeout");
256be9ddf43SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "fall_edge_delay",
2577029da5cSPawel Biernacki 	    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,
2587029da5cSPawel Biernacki 	    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",
2617029da5cSPawel Biernacki 	    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT,
2627029da5cSPawel Biernacki 	    sc, sizeof(*sc),
263be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_rise_proc, "IU", "I2C BUS rising edge delay");
2640bd904edSIan Lepore 	SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "debug",
2650bd904edSIan Lepore 	    CTLFLAG_RWTUN, &sc->sc_debug, 0,
2660bd904edSIan Lepore 	    "Enable debug; 1=reads/writes, 2=add starts/stops");
267be9ddf43SLuiz Otavio O Souza }
268be9ddf43SLuiz Otavio O Souza 
269be9ddf43SLuiz Otavio O Souza static void
bcm_bsc_reset(struct bcm_bsc_softc * sc)270be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(struct bcm_bsc_softc *sc)
271be9ddf43SLuiz Otavio O Souza {
272be9ddf43SLuiz Otavio O Souza 
273e50c6241SLuiz Otavio O Souza 	/* Enable the BSC Controller, disable interrupts. */
274e50c6241SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN);
275be9ddf43SLuiz Otavio O Souza 	/* Clear pending interrupts. */
276be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_STATUS, BCM_BSC_STATUS_CLKT |
277be9ddf43SLuiz Otavio O Souza 	    BCM_BSC_STATUS_ERR | BCM_BSC_STATUS_DONE);
278be9ddf43SLuiz Otavio O Souza 	/* Clear the FIFO. */
279be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_CLEAR0,
280be9ddf43SLuiz Otavio O Souza 	    BCM_BSC_CTRL_CLEAR0);
281be9ddf43SLuiz Otavio O Souza }
282be9ddf43SLuiz Otavio O Souza 
283be9ddf43SLuiz Otavio O Souza static int
bcm_bsc_probe(device_t dev)284be9ddf43SLuiz Otavio O Souza bcm_bsc_probe(device_t dev)
285be9ddf43SLuiz Otavio O Souza {
286be9ddf43SLuiz Otavio O Souza 
287add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
288add35ed5SIan Lepore 		return (ENXIO);
289add35ed5SIan Lepore 
2909d6eb8bbSOleksandr Tymoshenko 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
291be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
292be9ddf43SLuiz Otavio O Souza 
293be9ddf43SLuiz Otavio O Souza 	device_set_desc(dev, "BCM2708/2835 BSC controller");
294be9ddf43SLuiz Otavio O Souza 
295be9ddf43SLuiz Otavio O Souza 	return (BUS_PROBE_DEFAULT);
296be9ddf43SLuiz Otavio O Souza }
297be9ddf43SLuiz Otavio O Souza 
298be9ddf43SLuiz Otavio O Souza static int
bcm_bsc_attach(device_t dev)299be9ddf43SLuiz Otavio O Souza bcm_bsc_attach(device_t dev)
300be9ddf43SLuiz Otavio O Souza {
301be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
30291cc58afSOleksandr Tymoshenko 	int rid;
303be9ddf43SLuiz Otavio O Souza 
304be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
305be9ddf43SLuiz Otavio O Souza 	sc->sc_dev = dev;
306be9ddf43SLuiz Otavio O Souza 
307be9ddf43SLuiz Otavio O Souza 	rid = 0;
308be9ddf43SLuiz Otavio O Souza 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
309be9ddf43SLuiz Otavio O Souza 	    RF_ACTIVE);
310be9ddf43SLuiz Otavio O Souza 	if (!sc->sc_mem_res) {
311be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate memory window\n");
312be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
313be9ddf43SLuiz Otavio O Souza 	}
314be9ddf43SLuiz Otavio O Souza 
315be9ddf43SLuiz Otavio O Souza 	sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
316be9ddf43SLuiz Otavio O Souza 	sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
317be9ddf43SLuiz Otavio O Souza 
318be9ddf43SLuiz Otavio O Souza 	rid = 0;
319be9ddf43SLuiz Otavio O Souza 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
320be9ddf43SLuiz Otavio O Souza 	    RF_ACTIVE | RF_SHAREABLE);
321be9ddf43SLuiz Otavio O Souza 	if (!sc->sc_irq_res) {
322be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
323be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate interrupt\n");
324be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
325be9ddf43SLuiz Otavio O Souza 	}
326be9ddf43SLuiz Otavio O Souza 
327be9ddf43SLuiz Otavio O Souza 	/* Hook up our interrupt handler. */
328be9ddf43SLuiz Otavio O Souza 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
329be9ddf43SLuiz Otavio O Souza 	    NULL, bcm_bsc_intr, sc, &sc->sc_intrhand)) {
330be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
331be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
332be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot setup the interrupt handler\n");
333be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
334be9ddf43SLuiz Otavio O Souza 	}
335be9ddf43SLuiz Otavio O Souza 
336be9ddf43SLuiz Otavio O Souza 	mtx_init(&sc->sc_mtx, "bcm_bsc", NULL, MTX_DEF);
337be9ddf43SLuiz Otavio O Souza 
338be9ddf43SLuiz Otavio O Souza 	bcm_bsc_sysctl_init(sc);
339be9ddf43SLuiz Otavio O Souza 
340be9ddf43SLuiz Otavio O Souza 	/* Enable the BSC controller.  Flush the FIFO. */
341be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
342be9ddf43SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
343be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
344be9ddf43SLuiz Otavio O Souza 
3459e93dfcfSLuiz Otavio O Souza 	sc->sc_iicbus = device_add_child(dev, "iicbus", -1);
3469e93dfcfSLuiz Otavio O Souza 	if (sc->sc_iicbus == NULL) {
3479e93dfcfSLuiz Otavio O Souza 		bcm_bsc_detach(dev);
3489e93dfcfSLuiz Otavio O Souza 		return (ENXIO);
3499e93dfcfSLuiz Otavio O Souza 	}
350be9ddf43SLuiz Otavio O Souza 
3511e4042d4SIan Lepore 	/* Probe and attach the iicbus when interrupts are available. */
3529f07ef76SWarner Losh 	return (bus_delayed_attach_children(dev));
353be9ddf43SLuiz Otavio O Souza }
354be9ddf43SLuiz Otavio O Souza 
355be9ddf43SLuiz Otavio O Souza static int
bcm_bsc_detach(device_t dev)356be9ddf43SLuiz Otavio O Souza bcm_bsc_detach(device_t dev)
357be9ddf43SLuiz Otavio O Souza {
358be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
359be9ddf43SLuiz Otavio O Souza 
360be9ddf43SLuiz Otavio O Souza 	bus_generic_detach(dev);
361be9ddf43SLuiz Otavio O Souza 
362be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
3630bd904edSIan Lepore 	if (sc->sc_iicbus != NULL)
3640bd904edSIan Lepore 		device_delete_child(dev, sc->sc_iicbus);
365be9ddf43SLuiz Otavio O Souza 	mtx_destroy(&sc->sc_mtx);
366be9ddf43SLuiz Otavio O Souza 	if (sc->sc_intrhand)
367be9ddf43SLuiz Otavio O Souza 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
368be9ddf43SLuiz Otavio O Souza 	if (sc->sc_irq_res)
369be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
370be9ddf43SLuiz Otavio O Souza 	if (sc->sc_mem_res)
371be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
372be9ddf43SLuiz Otavio O Souza 
373be9ddf43SLuiz Otavio O Souza 	return (0);
374be9ddf43SLuiz Otavio O Souza }
375be9ddf43SLuiz Otavio O Souza 
376be9ddf43SLuiz Otavio O Souza static void
bcm_bsc_empty_rx_fifo(struct bcm_bsc_softc * sc)3770bd904edSIan Lepore bcm_bsc_empty_rx_fifo(struct bcm_bsc_softc *sc)
3780bd904edSIan Lepore {
3790bd904edSIan Lepore 	uint32_t status;
3800bd904edSIan Lepore 
3810bd904edSIan Lepore 	/* Assumes sc_totlen > 0 and BCM_BSC_STATUS_RXD is asserted on entry. */
3820bd904edSIan Lepore 	do {
3830bd904edSIan Lepore 		if (sc->sc_resid == 0) {
3840bd904edSIan Lepore 			sc->sc_data  = sc->sc_curmsg->buf;
3850bd904edSIan Lepore 			sc->sc_dlen  = sc->sc_curmsg->len;
3860bd904edSIan Lepore 			sc->sc_resid = sc->sc_dlen;
3870bd904edSIan Lepore 			++sc->sc_curmsg;
3880bd904edSIan Lepore 		}
3890bd904edSIan Lepore 		do {
3900bd904edSIan Lepore 			*sc->sc_data = BCM_BSC_READ(sc, BCM_BSC_DATA);
3910bd904edSIan Lepore 			DEBUGF(sc, 1, "0x%02x ", *sc->sc_data);
3920bd904edSIan Lepore 			++sc->sc_data;
3930bd904edSIan Lepore 			--sc->sc_resid;
3940bd904edSIan Lepore 			--sc->sc_totlen;
3950bd904edSIan Lepore 			status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
3960bd904edSIan Lepore 		} while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_RXD));
3970bd904edSIan Lepore 	} while (sc->sc_totlen > 0 && (status & BCM_BSC_STATUS_RXD));
3980bd904edSIan Lepore }
3990bd904edSIan Lepore 
4000bd904edSIan Lepore static void
bcm_bsc_fill_tx_fifo(struct bcm_bsc_softc * sc)4010bd904edSIan Lepore bcm_bsc_fill_tx_fifo(struct bcm_bsc_softc *sc)
4020bd904edSIan Lepore {
4030bd904edSIan Lepore 	uint32_t status;
4040bd904edSIan Lepore 
4050bd904edSIan Lepore 	/* Assumes sc_totlen > 0 and BCM_BSC_STATUS_TXD is asserted on entry. */
4060bd904edSIan Lepore 	do {
4070bd904edSIan Lepore 		if (sc->sc_resid == 0) {
4080bd904edSIan Lepore 			sc->sc_data  = sc->sc_curmsg->buf;
4090bd904edSIan Lepore 			sc->sc_dlen  = sc->sc_curmsg->len;
4100bd904edSIan Lepore 			sc->sc_resid = sc->sc_dlen;
4110bd904edSIan Lepore 			++sc->sc_curmsg;
4120bd904edSIan Lepore 		}
4130bd904edSIan Lepore 		do {
4140bd904edSIan Lepore 			BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data);
4150bd904edSIan Lepore 			DEBUGF(sc, 1, "0x%02x ", *sc->sc_data);
4160bd904edSIan Lepore 			++sc->sc_data;
4170bd904edSIan Lepore 			--sc->sc_resid;
4180bd904edSIan Lepore 			--sc->sc_totlen;
4190bd904edSIan Lepore 			status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
4200bd904edSIan Lepore 		} while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_TXD));
4210bd904edSIan Lepore 		/*
4220bd904edSIan Lepore 		 * If a repeat-start was pending and we just hit the end of a tx
4230bd904edSIan Lepore 		 * buffer, see if it's also the end of the writes that preceeded
4240bd904edSIan Lepore 		 * the repeat-start.  If so, log the repeat-start and the start
4250bd904edSIan Lepore 		 * of the following read, and return because we're not writing
4260bd904edSIan Lepore 		 * anymore (and TXD will be true because there's room to write
4270bd904edSIan Lepore 		 * in the fifo).
4280bd904edSIan Lepore 		 */
4290bd904edSIan Lepore 		if (sc->sc_replen > 0 && sc->sc_resid == 0) {
4300bd904edSIan Lepore 			sc->sc_replen -= sc->sc_dlen;
4310bd904edSIan Lepore 			if (sc->sc_replen == 0) {
4320bd904edSIan Lepore 				DEBUGF(sc, 1, " err=0\n");
4330bd904edSIan Lepore 				DEVICE_DEBUGF(sc, 2, "rstart 0x%02x\n",
4340bd904edSIan Lepore 				    sc->sc_curmsg->slave | 0x01);
4350bd904edSIan Lepore 				DEVICE_DEBUGF(sc, 1,
4360bd904edSIan Lepore 				    "read   0x%02x len %d: ",
4370bd904edSIan Lepore 				    sc->sc_curmsg->slave | 0x01,
4380bd904edSIan Lepore 				    sc->sc_totlen);
4390bd904edSIan Lepore 				sc->sc_flags |= BCM_I2C_READ;
4400bd904edSIan Lepore 				return;
4410bd904edSIan Lepore 			}
4420bd904edSIan Lepore 		}
4430bd904edSIan Lepore 	} while (sc->sc_totlen > 0 && (status & BCM_BSC_STATUS_TXD));
4440bd904edSIan Lepore }
4450bd904edSIan Lepore 
4460bd904edSIan Lepore static void
bcm_bsc_intr(void * arg)447be9ddf43SLuiz Otavio O Souza bcm_bsc_intr(void *arg)
448be9ddf43SLuiz Otavio O Souza {
449be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
450be9ddf43SLuiz Otavio O Souza 	uint32_t status;
451be9ddf43SLuiz Otavio O Souza 
452be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg;
453be9ddf43SLuiz Otavio O Souza 
454be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
455be9ddf43SLuiz Otavio O Souza 
456be9ddf43SLuiz Otavio O Souza 	/* The I2C interrupt is shared among all the BSC controllers. */
457be9ddf43SLuiz Otavio O Souza 	if ((sc->sc_flags & BCM_I2C_BUSY) == 0) {
458be9ddf43SLuiz Otavio O Souza 		BCM_BSC_UNLOCK(sc);
459be9ddf43SLuiz Otavio O Souza 		return;
460be9ddf43SLuiz Otavio O Souza 	}
461be9ddf43SLuiz Otavio O Souza 
462be9ddf43SLuiz Otavio O Souza 	status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
4630bd904edSIan Lepore 	DEBUGF(sc, 4, " <intrstatus=0x%08x> ", status);
464be9ddf43SLuiz Otavio O Souza 
4650bd904edSIan Lepore 	/* RXD and DONE can assert together, empty fifo before checking done. */
4660bd904edSIan Lepore 	if ((sc->sc_flags & BCM_I2C_READ) && (status & BCM_BSC_STATUS_RXD))
4670bd904edSIan Lepore 		bcm_bsc_empty_rx_fifo(sc);
4680bd904edSIan Lepore 
4690bd904edSIan Lepore 	/* Check for completion. */
4700bd904edSIan Lepore 	if (status & (BCM_BSC_STATUS_ERRBITS | BCM_BSC_STATUS_DONE)) {
4710bd904edSIan Lepore 		sc->sc_flags |= BCM_I2C_DONE;
4720bd904edSIan Lepore 		if (status & BCM_BSC_STATUS_ERRBITS)
473e50c6241SLuiz Otavio O Souza 			sc->sc_flags |= BCM_I2C_ERROR;
474be9ddf43SLuiz Otavio O Souza 		/* Disable interrupts. */
475be9ddf43SLuiz Otavio O Souza 		bcm_bsc_reset(sc);
4760bd904edSIan Lepore 		wakeup(sc);
4770bd904edSIan Lepore 	} else if (!(sc->sc_flags & BCM_I2C_READ)) {
4780bd904edSIan Lepore 		/*
4790bd904edSIan Lepore 		 * Don't check for TXD until after determining whether the
4800bd904edSIan Lepore 		 * transfer is complete; TXD will be asserted along with ERR or
4810bd904edSIan Lepore 		 * DONE if there is room in the fifo.
4820bd904edSIan Lepore 		 */
483b5496277SIan Lepore 		if ((status & BCM_BSC_STATUS_TXD) && sc->sc_totlen > 0)
4840bd904edSIan Lepore 			bcm_bsc_fill_tx_fifo(sc);
485be9ddf43SLuiz Otavio O Souza 	}
486be9ddf43SLuiz Otavio O Souza 
487be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
488be9ddf43SLuiz Otavio O Souza }
489be9ddf43SLuiz Otavio O Souza 
490be9ddf43SLuiz Otavio O Souza static int
bcm_bsc_transfer(device_t dev,struct iic_msg * msgs,uint32_t nmsgs)491be9ddf43SLuiz Otavio O Souza bcm_bsc_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
492be9ddf43SLuiz Otavio O Souza {
493be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
4940bd904edSIan Lepore 	struct iic_msg *endmsgs, *nxtmsg;
4950bd904edSIan Lepore 	uint32_t readctl, status;
4960bd904edSIan Lepore 	int err;
4970bd904edSIan Lepore 	uint16_t curlen;
4980bd904edSIan Lepore 	uint8_t curisread, curslave, nxtisread, nxtslave;
499be9ddf43SLuiz Otavio O Souza 
500be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
501be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
502be9ddf43SLuiz Otavio O Souza 
503be9ddf43SLuiz Otavio O Souza 	/* If the controller is busy wait until it is available. */
504be9ddf43SLuiz Otavio O Souza 	while (sc->sc_flags & BCM_I2C_BUSY)
505a141b672SLuiz Otavio O Souza 		mtx_sleep(dev, &sc->sc_mtx, 0, "bscbusw", 0);
506be9ddf43SLuiz Otavio O Souza 
507be9ddf43SLuiz Otavio O Souza 	/* Now we have control over the BSC controller. */
508be9ddf43SLuiz Otavio O Souza 	sc->sc_flags = BCM_I2C_BUSY;
509be9ddf43SLuiz Otavio O Souza 
5100bd904edSIan Lepore 	DEVICE_DEBUGF(sc, 3, "Transfer %d msgs\n", nmsgs);
5110bd904edSIan Lepore 
512be9ddf43SLuiz Otavio O Souza 	/* Clear the FIFO and the pending interrupts. */
513be9ddf43SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
514be9ddf43SLuiz Otavio O Souza 
5150bd904edSIan Lepore 	/*
5160bd904edSIan Lepore 	 * Perform all the transfers requested in the array of msgs.  Note that
5170bd904edSIan Lepore 	 * it is bcm_bsc_empty_rx_fifo() and bcm_bsc_fill_tx_fifo() that advance
5180bd904edSIan Lepore 	 * sc->sc_curmsg through the array of messages, as the data from each
5190bd904edSIan Lepore 	 * message is fully consumed, but it is this loop that notices when we
5200bd904edSIan Lepore 	 * have no more messages to process.
5210bd904edSIan Lepore 	 */
522be9ddf43SLuiz Otavio O Souza 	err = 0;
5230bd904edSIan Lepore 	sc->sc_resid = 0;
5240bd904edSIan Lepore 	sc->sc_curmsg = msgs;
5250bd904edSIan Lepore 	endmsgs = &msgs[nmsgs];
5260bd904edSIan Lepore 	while (sc->sc_curmsg < endmsgs) {
5270bd904edSIan Lepore 		readctl = 0;
5280bd904edSIan Lepore 		curslave = sc->sc_curmsg->slave >> 1;
5290bd904edSIan Lepore 		curisread = sc->sc_curmsg->flags & IIC_M_RD;
5300bd904edSIan Lepore 		sc->sc_replen = 0;
5310bd904edSIan Lepore 		sc->sc_totlen = sc->sc_curmsg->len;
5320bd904edSIan Lepore 		/*
5330bd904edSIan Lepore 		 * Scan for scatter/gather IO (same slave and direction) or
5340bd904edSIan Lepore 		 * repeat-start (read following write for the same slave).
5350bd904edSIan Lepore 		 */
5360bd904edSIan Lepore 		for (nxtmsg = sc->sc_curmsg + 1; nxtmsg < endmsgs; ++nxtmsg) {
5370bd904edSIan Lepore 			nxtslave = nxtmsg->slave >> 1;
5380bd904edSIan Lepore 			if (curslave == nxtslave) {
5390bd904edSIan Lepore 				nxtisread = nxtmsg->flags & IIC_M_RD;
5400bd904edSIan Lepore 				if (curisread == nxtisread) {
5410bd904edSIan Lepore 					/*
5420bd904edSIan Lepore 					 * Same slave and direction, this
5430bd904edSIan Lepore 					 * message will be part of the same
5440bd904edSIan Lepore 					 * transfer as the previous one.
5450bd904edSIan Lepore 					 */
5460bd904edSIan Lepore 					sc->sc_totlen += nxtmsg->len;
5470bd904edSIan Lepore 					continue;
5480bd904edSIan Lepore 				} else if (curisread == IIC_M_WR) {
5490bd904edSIan Lepore 					/*
5500bd904edSIan Lepore 					 * Read after write to same slave means
5510bd904edSIan Lepore 					 * repeat-start, remember how many bytes
5520bd904edSIan Lepore 					 * come before the repeat-start, switch
5530bd904edSIan Lepore 					 * the direction to IIC_M_RD, and gather
5540bd904edSIan Lepore 					 * up following reads to the same slave.
5550bd904edSIan Lepore 					 */
5560bd904edSIan Lepore 					curisread = IIC_M_RD;
5570bd904edSIan Lepore 					sc->sc_replen = sc->sc_totlen;
5580bd904edSIan Lepore 					sc->sc_totlen += nxtmsg->len;
5590bd904edSIan Lepore 					continue;
5600bd904edSIan Lepore 				}
5610bd904edSIan Lepore 			}
5620bd904edSIan Lepore 			break;
5630bd904edSIan Lepore 		}
5640bd904edSIan Lepore 
5650bd904edSIan Lepore 		/*
5660bd904edSIan Lepore 		 * curslave and curisread temporaries from above may refer to
5670bd904edSIan Lepore 		 * the after-repstart msg, reset them to reflect sc_curmsg.
5680bd904edSIan Lepore 		 */
5690bd904edSIan Lepore 		curisread = (sc->sc_curmsg->flags & IIC_M_RD) ? 1 : 0;
5700bd904edSIan Lepore 		curslave = sc->sc_curmsg->slave | curisread;
571be9ddf43SLuiz Otavio O Souza 
572be9ddf43SLuiz Otavio O Souza 		/* Write the slave address. */
5730bd904edSIan Lepore 		BCM_BSC_WRITE(sc, BCM_BSC_SLAVE, curslave >> 1);
574be9ddf43SLuiz Otavio O Souza 
5750bd904edSIan Lepore 		DEVICE_DEBUGF(sc, 2, "start  0x%02x\n", curslave);
576be9ddf43SLuiz Otavio O Souza 
5770bd904edSIan Lepore 		/*
5780bd904edSIan Lepore 		 * Either set up read length and direction variables for a
5790bd904edSIan Lepore 		 * simple transfer or get the hardware started on the first
5800bd904edSIan Lepore 		 * piece of a transfer that involves a repeat-start and set up
5810bd904edSIan Lepore 		 * the read length and direction vars for the second piece.
5820bd904edSIan Lepore 		 */
5830bd904edSIan Lepore 		if (sc->sc_replen == 0) {
5840bd904edSIan Lepore 			DEVICE_DEBUGF(sc, 1, "%-6s 0x%02x len %d: ",
585ae992394SIan Lepore 			    (curisread) ? "read" : "write", curslave,
5860bd904edSIan Lepore 			    sc->sc_totlen);
5870bd904edSIan Lepore 			curlen = sc->sc_totlen;
5880bd904edSIan Lepore 			if (curisread) {
5890bd904edSIan Lepore 				readctl = BCM_BSC_CTRL_READ;
590be9ddf43SLuiz Otavio O Souza 				sc->sc_flags |= BCM_I2C_READ;
5910bd904edSIan Lepore 			} else {
5920bd904edSIan Lepore 				readctl = 0;
5930bd904edSIan Lepore 				sc->sc_flags &= ~BCM_I2C_READ;
594be9ddf43SLuiz Otavio O Souza 			}
5950bd904edSIan Lepore 		} else {
5960bd904edSIan Lepore 			DEVICE_DEBUGF(sc, 1, "%-6s 0x%02x len %d: ",
597ae992394SIan Lepore 			    (curisread) ? "read" : "write", curslave,
5980bd904edSIan Lepore 			    sc->sc_replen);
599be9ddf43SLuiz Otavio O Souza 
6000bd904edSIan Lepore 			/*
6010bd904edSIan Lepore 			 * Start the write transfer with an empty fifo and wait
6020bd904edSIan Lepore 			 * for the 'transfer active' status bit to light up;
6030bd904edSIan Lepore 			 * that indicates that the hardware has latched the
6040bd904edSIan Lepore 			 * direction and length for the write, and we can safely
6050bd904edSIan Lepore 			 * reload those registers and issue the start for the
6060bd904edSIan Lepore 			 * following read; interrupts are not enabled here.
6070bd904edSIan Lepore 			 */
6080bd904edSIan Lepore 			BCM_BSC_WRITE(sc, BCM_BSC_DLEN, sc->sc_replen);
609be9ddf43SLuiz Otavio O Souza 			BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN |
6100bd904edSIan Lepore 			    BCM_BSC_CTRL_ST);
6110bd904edSIan Lepore 			do {
6120bd904edSIan Lepore 				status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
6130bd904edSIan Lepore 				if (status & BCM_BSC_STATUS_ERR) {
6140bd904edSIan Lepore 					/* no ACK on slave addr */
6150bd904edSIan Lepore 					err = EIO;
6160bd904edSIan Lepore 					goto xfer_done;
6170bd904edSIan Lepore 				}
6180bd904edSIan Lepore 			} while ((status & BCM_BSC_STATUS_TA) == 0);
6190bd904edSIan Lepore 			/*
6200bd904edSIan Lepore 			 * Set curlen and readctl for the repeat-start read that
6210bd904edSIan Lepore 			 * we need to set up below, but set sc_flags to write,
6220bd904edSIan Lepore 			 * because that is the operation in progress right now.
6230bd904edSIan Lepore 			 */
6240bd904edSIan Lepore 			curlen = sc->sc_totlen - sc->sc_replen;
6250bd904edSIan Lepore 			readctl = BCM_BSC_CTRL_READ;
6260bd904edSIan Lepore 			sc->sc_flags &= ~BCM_I2C_READ;
6270bd904edSIan Lepore 		}
6280bd904edSIan Lepore 
6290bd904edSIan Lepore 		/*
6300bd904edSIan Lepore 		 * Start the transfer with interrupts enabled, then if doing a
6310bd904edSIan Lepore 		 * write, fill the tx fifo.  Not prefilling the fifo until after
6320bd904edSIan Lepore 		 * this start command is the key workaround for making
6330bd904edSIan Lepore 		 * repeat-start work, and it's harmless to do it in this order
6340bd904edSIan Lepore 		 * for a regular write too.
6350bd904edSIan Lepore 		 */
6360bd904edSIan Lepore 		BCM_BSC_WRITE(sc, BCM_BSC_DLEN, curlen);
6370bd904edSIan Lepore 		BCM_BSC_WRITE(sc, BCM_BSC_CTRL, readctl | BCM_BSC_CTRL_I2CEN |
6380bd904edSIan Lepore 		    BCM_BSC_CTRL_ST | BCM_BSC_CTRL_INT_ALL);
6390bd904edSIan Lepore 
6400bd904edSIan Lepore 		if (!(sc->sc_curmsg->flags & IIC_M_RD)) {
6410bd904edSIan Lepore 			bcm_bsc_fill_tx_fifo(sc);
6420bd904edSIan Lepore 		}
643be9ddf43SLuiz Otavio O Souza 
644be9ddf43SLuiz Otavio O Souza 		/* Wait for the transaction to complete. */
6450bd904edSIan Lepore 		while (err == 0 && !(sc->sc_flags & BCM_I2C_DONE)) {
6460bd904edSIan Lepore 			err = mtx_sleep(sc, &sc->sc_mtx, 0, "bsciow", hz);
6470bd904edSIan Lepore 		}
64871b37cb9SLuiz Otavio O Souza 		/* Check for errors. */
6490a39cc71SLuiz Otavio O Souza 		if (err == 0 && (sc->sc_flags & BCM_I2C_ERROR))
650be9ddf43SLuiz Otavio O Souza 			err = EIO;
6510bd904edSIan Lepore xfer_done:
6520bd904edSIan Lepore 		DEBUGF(sc, 1, " err=%d\n", err);
6530bd904edSIan Lepore 		DEVICE_DEBUGF(sc, 2, "stop\n");
65471b37cb9SLuiz Otavio O Souza 		if (err != 0)
655be9ddf43SLuiz Otavio O Souza 			break;
656be9ddf43SLuiz Otavio O Souza 	}
657be9ddf43SLuiz Otavio O Souza 
6580bd904edSIan Lepore 	/* Disable interrupts, clean fifo, etc. */
6590bd904edSIan Lepore 	bcm_bsc_reset(sc);
6600bd904edSIan Lepore 
661be9ddf43SLuiz Otavio O Souza 	/* Clean the controller flags. */
662be9ddf43SLuiz Otavio O Souza 	sc->sc_flags = 0;
663be9ddf43SLuiz Otavio O Souza 
664a141b672SLuiz Otavio O Souza 	/* Wake up the threads waiting for bus. */
665a141b672SLuiz Otavio O Souza 	wakeup(dev);
666a141b672SLuiz Otavio O Souza 
667be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
668be9ddf43SLuiz Otavio O Souza 
669be9ddf43SLuiz Otavio O Souza 	return (err);
670be9ddf43SLuiz Otavio O Souza }
671be9ddf43SLuiz Otavio O Souza 
672e50c6241SLuiz Otavio O Souza static int
bcm_bsc_iicbus_reset(device_t dev,u_char speed,u_char addr,u_char * oldaddr)673e50c6241SLuiz Otavio O Souza bcm_bsc_iicbus_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
674e50c6241SLuiz Otavio O Souza {
675e50c6241SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
6769e93dfcfSLuiz Otavio O Souza 	uint32_t busfreq;
677e50c6241SLuiz Otavio O Souza 
678e50c6241SLuiz Otavio O Souza 	sc = device_get_softc(dev);
679e50c6241SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
680e50c6241SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
6819e93dfcfSLuiz Otavio O Souza 	if (sc->sc_iicbus == NULL)
6829e93dfcfSLuiz Otavio O Souza 		busfreq = 100000;
6839e93dfcfSLuiz Otavio O Souza 	else
6849e93dfcfSLuiz Otavio O Souza 		busfreq = IICBUS_GET_FREQUENCY(sc->sc_iicbus, speed);
6859e93dfcfSLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CLOCK, BCM_BSC_CORE_CLK / busfreq);
686e50c6241SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
687e50c6241SLuiz Otavio O Souza 
688e50c6241SLuiz Otavio O Souza 	return (IIC_ENOADDR);
689e50c6241SLuiz Otavio O Souza }
690e50c6241SLuiz Otavio O Souza 
691be9ddf43SLuiz Otavio O Souza static phandle_t
bcm_bsc_get_node(device_t bus,device_t dev)692be9ddf43SLuiz Otavio O Souza bcm_bsc_get_node(device_t bus, device_t dev)
693be9ddf43SLuiz Otavio O Souza {
694be9ddf43SLuiz Otavio O Souza 
695be9ddf43SLuiz Otavio O Souza 	/* We only have one child, the I2C bus, which needs our own node. */
696be9ddf43SLuiz Otavio O Souza 	return (ofw_bus_get_node(bus));
697be9ddf43SLuiz Otavio O Souza }
698be9ddf43SLuiz Otavio O Souza 
699be9ddf43SLuiz Otavio O Souza static device_method_t bcm_bsc_methods[] = {
700be9ddf43SLuiz Otavio O Souza 	/* Device interface */
701be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_probe,		bcm_bsc_probe),
702be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_attach,	bcm_bsc_attach),
703be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_detach,	bcm_bsc_detach),
704be9ddf43SLuiz Otavio O Souza 
705be9ddf43SLuiz Otavio O Souza 	/* iicbus interface */
706e50c6241SLuiz Otavio O Souza 	DEVMETHOD(iicbus_reset,		bcm_bsc_iicbus_reset),
707be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(iicbus_callback,	iicbus_null_callback),
708be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(iicbus_transfer,	bcm_bsc_transfer),
709be9ddf43SLuiz Otavio O Souza 
710be9ddf43SLuiz Otavio O Souza 	/* ofw_bus interface */
711be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(ofw_bus_get_node,	bcm_bsc_get_node),
712be9ddf43SLuiz Otavio O Souza 
713be9ddf43SLuiz Otavio O Souza 	DEVMETHOD_END
714be9ddf43SLuiz Otavio O Souza };
715be9ddf43SLuiz Otavio O Souza 
716be9ddf43SLuiz Otavio O Souza static driver_t bcm_bsc_driver = {
717be9ddf43SLuiz Otavio O Souza 	"iichb",
718be9ddf43SLuiz Otavio O Souza 	bcm_bsc_methods,
719be9ddf43SLuiz Otavio O Souza 	sizeof(struct bcm_bsc_softc),
720be9ddf43SLuiz Otavio O Souza };
721be9ddf43SLuiz Otavio O Souza 
722676ea8e1SJohn Baldwin DRIVER_MODULE(iicbus, bcm2835_bsc, iicbus_driver, 0, 0);
72382d4dc06SJohn Baldwin DRIVER_MODULE(bcm2835_bsc, simplebus, bcm_bsc_driver, 0, 0);
724