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, ®, 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, ®, 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
3455b56413dSWarner Losh sc->sc_iicbus = device_add_child(dev, "iicbus", DEVICE_UNIT_ANY);
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. */
352*34f5de82SJohn Baldwin bus_delayed_attach_children(dev);
353*34f5de82SJohn Baldwin return (0);
354be9ddf43SLuiz Otavio O Souza }
355be9ddf43SLuiz Otavio O Souza
356be9ddf43SLuiz Otavio O Souza static int
bcm_bsc_detach(device_t dev)357be9ddf43SLuiz Otavio O Souza bcm_bsc_detach(device_t dev)
358be9ddf43SLuiz Otavio O Souza {
359be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc;
360be9ddf43SLuiz Otavio O Souza
361be9ddf43SLuiz Otavio O Souza bus_generic_detach(dev);
362be9ddf43SLuiz Otavio O Souza
363be9ddf43SLuiz Otavio O Souza sc = device_get_softc(dev);
3640bd904edSIan Lepore if (sc->sc_iicbus != NULL)
3650bd904edSIan Lepore device_delete_child(dev, sc->sc_iicbus);
366be9ddf43SLuiz Otavio O Souza mtx_destroy(&sc->sc_mtx);
367be9ddf43SLuiz Otavio O Souza if (sc->sc_intrhand)
368be9ddf43SLuiz Otavio O Souza bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
369be9ddf43SLuiz Otavio O Souza if (sc->sc_irq_res)
370be9ddf43SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
371be9ddf43SLuiz Otavio O Souza if (sc->sc_mem_res)
372be9ddf43SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
373be9ddf43SLuiz Otavio O Souza
374be9ddf43SLuiz Otavio O Souza return (0);
375be9ddf43SLuiz Otavio O Souza }
376be9ddf43SLuiz Otavio O Souza
377be9ddf43SLuiz Otavio O Souza static void
bcm_bsc_empty_rx_fifo(struct bcm_bsc_softc * sc)3780bd904edSIan Lepore bcm_bsc_empty_rx_fifo(struct bcm_bsc_softc *sc)
3790bd904edSIan Lepore {
3800bd904edSIan Lepore uint32_t status;
3810bd904edSIan Lepore
3820bd904edSIan Lepore /* Assumes sc_totlen > 0 and BCM_BSC_STATUS_RXD is asserted on entry. */
3830bd904edSIan Lepore do {
3840bd904edSIan Lepore if (sc->sc_resid == 0) {
3850bd904edSIan Lepore sc->sc_data = sc->sc_curmsg->buf;
3860bd904edSIan Lepore sc->sc_dlen = sc->sc_curmsg->len;
3870bd904edSIan Lepore sc->sc_resid = sc->sc_dlen;
3880bd904edSIan Lepore ++sc->sc_curmsg;
3890bd904edSIan Lepore }
3900bd904edSIan Lepore do {
3910bd904edSIan Lepore *sc->sc_data = BCM_BSC_READ(sc, BCM_BSC_DATA);
3920bd904edSIan Lepore DEBUGF(sc, 1, "0x%02x ", *sc->sc_data);
3930bd904edSIan Lepore ++sc->sc_data;
3940bd904edSIan Lepore --sc->sc_resid;
3950bd904edSIan Lepore --sc->sc_totlen;
3960bd904edSIan Lepore status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
3970bd904edSIan Lepore } while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_RXD));
3980bd904edSIan Lepore } while (sc->sc_totlen > 0 && (status & BCM_BSC_STATUS_RXD));
3990bd904edSIan Lepore }
4000bd904edSIan Lepore
4010bd904edSIan Lepore static void
bcm_bsc_fill_tx_fifo(struct bcm_bsc_softc * sc)4020bd904edSIan Lepore bcm_bsc_fill_tx_fifo(struct bcm_bsc_softc *sc)
4030bd904edSIan Lepore {
4040bd904edSIan Lepore uint32_t status;
4050bd904edSIan Lepore
4060bd904edSIan Lepore /* Assumes sc_totlen > 0 and BCM_BSC_STATUS_TXD is asserted on entry. */
4070bd904edSIan Lepore do {
4080bd904edSIan Lepore if (sc->sc_resid == 0) {
4090bd904edSIan Lepore sc->sc_data = sc->sc_curmsg->buf;
4100bd904edSIan Lepore sc->sc_dlen = sc->sc_curmsg->len;
4110bd904edSIan Lepore sc->sc_resid = sc->sc_dlen;
4120bd904edSIan Lepore ++sc->sc_curmsg;
4130bd904edSIan Lepore }
4140bd904edSIan Lepore do {
4150bd904edSIan Lepore BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data);
4160bd904edSIan Lepore DEBUGF(sc, 1, "0x%02x ", *sc->sc_data);
4170bd904edSIan Lepore ++sc->sc_data;
4180bd904edSIan Lepore --sc->sc_resid;
4190bd904edSIan Lepore --sc->sc_totlen;
4200bd904edSIan Lepore status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
4210bd904edSIan Lepore } while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_TXD));
4220bd904edSIan Lepore /*
4230bd904edSIan Lepore * If a repeat-start was pending and we just hit the end of a tx
4240bd904edSIan Lepore * buffer, see if it's also the end of the writes that preceeded
4250bd904edSIan Lepore * the repeat-start. If so, log the repeat-start and the start
4260bd904edSIan Lepore * of the following read, and return because we're not writing
4270bd904edSIan Lepore * anymore (and TXD will be true because there's room to write
4280bd904edSIan Lepore * in the fifo).
4290bd904edSIan Lepore */
4300bd904edSIan Lepore if (sc->sc_replen > 0 && sc->sc_resid == 0) {
4310bd904edSIan Lepore sc->sc_replen -= sc->sc_dlen;
4320bd904edSIan Lepore if (sc->sc_replen == 0) {
4330bd904edSIan Lepore DEBUGF(sc, 1, " err=0\n");
4340bd904edSIan Lepore DEVICE_DEBUGF(sc, 2, "rstart 0x%02x\n",
4350bd904edSIan Lepore sc->sc_curmsg->slave | 0x01);
4360bd904edSIan Lepore DEVICE_DEBUGF(sc, 1,
4370bd904edSIan Lepore "read 0x%02x len %d: ",
4380bd904edSIan Lepore sc->sc_curmsg->slave | 0x01,
4390bd904edSIan Lepore sc->sc_totlen);
4400bd904edSIan Lepore sc->sc_flags |= BCM_I2C_READ;
4410bd904edSIan Lepore return;
4420bd904edSIan Lepore }
4430bd904edSIan Lepore }
4440bd904edSIan Lepore } while (sc->sc_totlen > 0 && (status & BCM_BSC_STATUS_TXD));
4450bd904edSIan Lepore }
4460bd904edSIan Lepore
4470bd904edSIan Lepore static void
bcm_bsc_intr(void * arg)448be9ddf43SLuiz Otavio O Souza bcm_bsc_intr(void *arg)
449be9ddf43SLuiz Otavio O Souza {
450be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc;
451be9ddf43SLuiz Otavio O Souza uint32_t status;
452be9ddf43SLuiz Otavio O Souza
453be9ddf43SLuiz Otavio O Souza sc = (struct bcm_bsc_softc *)arg;
454be9ddf43SLuiz Otavio O Souza
455be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc);
456be9ddf43SLuiz Otavio O Souza
457be9ddf43SLuiz Otavio O Souza /* The I2C interrupt is shared among all the BSC controllers. */
458be9ddf43SLuiz Otavio O Souza if ((sc->sc_flags & BCM_I2C_BUSY) == 0) {
459be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc);
460be9ddf43SLuiz Otavio O Souza return;
461be9ddf43SLuiz Otavio O Souza }
462be9ddf43SLuiz Otavio O Souza
463be9ddf43SLuiz Otavio O Souza status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
4640bd904edSIan Lepore DEBUGF(sc, 4, " <intrstatus=0x%08x> ", status);
465be9ddf43SLuiz Otavio O Souza
4660bd904edSIan Lepore /* RXD and DONE can assert together, empty fifo before checking done. */
4670bd904edSIan Lepore if ((sc->sc_flags & BCM_I2C_READ) && (status & BCM_BSC_STATUS_RXD))
4680bd904edSIan Lepore bcm_bsc_empty_rx_fifo(sc);
4690bd904edSIan Lepore
4700bd904edSIan Lepore /* Check for completion. */
4710bd904edSIan Lepore if (status & (BCM_BSC_STATUS_ERRBITS | BCM_BSC_STATUS_DONE)) {
4720bd904edSIan Lepore sc->sc_flags |= BCM_I2C_DONE;
4730bd904edSIan Lepore if (status & BCM_BSC_STATUS_ERRBITS)
474e50c6241SLuiz Otavio O Souza sc->sc_flags |= BCM_I2C_ERROR;
475be9ddf43SLuiz Otavio O Souza /* Disable interrupts. */
476be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(sc);
4770bd904edSIan Lepore wakeup(sc);
4780bd904edSIan Lepore } else if (!(sc->sc_flags & BCM_I2C_READ)) {
4790bd904edSIan Lepore /*
4800bd904edSIan Lepore * Don't check for TXD until after determining whether the
4810bd904edSIan Lepore * transfer is complete; TXD will be asserted along with ERR or
4820bd904edSIan Lepore * DONE if there is room in the fifo.
4830bd904edSIan Lepore */
484b5496277SIan Lepore if ((status & BCM_BSC_STATUS_TXD) && sc->sc_totlen > 0)
4850bd904edSIan Lepore bcm_bsc_fill_tx_fifo(sc);
486be9ddf43SLuiz Otavio O Souza }
487be9ddf43SLuiz Otavio O Souza
488be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc);
489be9ddf43SLuiz Otavio O Souza }
490be9ddf43SLuiz Otavio O Souza
491be9ddf43SLuiz Otavio O Souza static int
bcm_bsc_transfer(device_t dev,struct iic_msg * msgs,uint32_t nmsgs)492be9ddf43SLuiz Otavio O Souza bcm_bsc_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
493be9ddf43SLuiz Otavio O Souza {
494be9ddf43SLuiz Otavio O Souza struct bcm_bsc_softc *sc;
4950bd904edSIan Lepore struct iic_msg *endmsgs, *nxtmsg;
4960bd904edSIan Lepore uint32_t readctl, status;
4970bd904edSIan Lepore int err;
4980bd904edSIan Lepore uint16_t curlen;
4990bd904edSIan Lepore uint8_t curisread, curslave, nxtisread, nxtslave;
500be9ddf43SLuiz Otavio O Souza
501be9ddf43SLuiz Otavio O Souza sc = device_get_softc(dev);
502be9ddf43SLuiz Otavio O Souza BCM_BSC_LOCK(sc);
503be9ddf43SLuiz Otavio O Souza
504be9ddf43SLuiz Otavio O Souza /* If the controller is busy wait until it is available. */
505be9ddf43SLuiz Otavio O Souza while (sc->sc_flags & BCM_I2C_BUSY)
506a141b672SLuiz Otavio O Souza mtx_sleep(dev, &sc->sc_mtx, 0, "bscbusw", 0);
507be9ddf43SLuiz Otavio O Souza
508be9ddf43SLuiz Otavio O Souza /* Now we have control over the BSC controller. */
509be9ddf43SLuiz Otavio O Souza sc->sc_flags = BCM_I2C_BUSY;
510be9ddf43SLuiz Otavio O Souza
5110bd904edSIan Lepore DEVICE_DEBUGF(sc, 3, "Transfer %d msgs\n", nmsgs);
5120bd904edSIan Lepore
513be9ddf43SLuiz Otavio O Souza /* Clear the FIFO and the pending interrupts. */
514be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(sc);
515be9ddf43SLuiz Otavio O Souza
5160bd904edSIan Lepore /*
5170bd904edSIan Lepore * Perform all the transfers requested in the array of msgs. Note that
5180bd904edSIan Lepore * it is bcm_bsc_empty_rx_fifo() and bcm_bsc_fill_tx_fifo() that advance
5190bd904edSIan Lepore * sc->sc_curmsg through the array of messages, as the data from each
5200bd904edSIan Lepore * message is fully consumed, but it is this loop that notices when we
5210bd904edSIan Lepore * have no more messages to process.
5220bd904edSIan Lepore */
523be9ddf43SLuiz Otavio O Souza err = 0;
5240bd904edSIan Lepore sc->sc_resid = 0;
5250bd904edSIan Lepore sc->sc_curmsg = msgs;
5260bd904edSIan Lepore endmsgs = &msgs[nmsgs];
5270bd904edSIan Lepore while (sc->sc_curmsg < endmsgs) {
5280bd904edSIan Lepore readctl = 0;
5290bd904edSIan Lepore curslave = sc->sc_curmsg->slave >> 1;
5300bd904edSIan Lepore curisread = sc->sc_curmsg->flags & IIC_M_RD;
5310bd904edSIan Lepore sc->sc_replen = 0;
5320bd904edSIan Lepore sc->sc_totlen = sc->sc_curmsg->len;
5330bd904edSIan Lepore /*
5340bd904edSIan Lepore * Scan for scatter/gather IO (same slave and direction) or
5350bd904edSIan Lepore * repeat-start (read following write for the same slave).
5360bd904edSIan Lepore */
5370bd904edSIan Lepore for (nxtmsg = sc->sc_curmsg + 1; nxtmsg < endmsgs; ++nxtmsg) {
5380bd904edSIan Lepore nxtslave = nxtmsg->slave >> 1;
5390bd904edSIan Lepore if (curslave == nxtslave) {
5400bd904edSIan Lepore nxtisread = nxtmsg->flags & IIC_M_RD;
5410bd904edSIan Lepore if (curisread == nxtisread) {
5420bd904edSIan Lepore /*
5430bd904edSIan Lepore * Same slave and direction, this
5440bd904edSIan Lepore * message will be part of the same
5450bd904edSIan Lepore * transfer as the previous one.
5460bd904edSIan Lepore */
5470bd904edSIan Lepore sc->sc_totlen += nxtmsg->len;
5480bd904edSIan Lepore continue;
5490bd904edSIan Lepore } else if (curisread == IIC_M_WR) {
5500bd904edSIan Lepore /*
5510bd904edSIan Lepore * Read after write to same slave means
5520bd904edSIan Lepore * repeat-start, remember how many bytes
5530bd904edSIan Lepore * come before the repeat-start, switch
5540bd904edSIan Lepore * the direction to IIC_M_RD, and gather
5550bd904edSIan Lepore * up following reads to the same slave.
5560bd904edSIan Lepore */
5570bd904edSIan Lepore curisread = IIC_M_RD;
5580bd904edSIan Lepore sc->sc_replen = sc->sc_totlen;
5590bd904edSIan Lepore sc->sc_totlen += nxtmsg->len;
5600bd904edSIan Lepore continue;
5610bd904edSIan Lepore }
5620bd904edSIan Lepore }
5630bd904edSIan Lepore break;
5640bd904edSIan Lepore }
5650bd904edSIan Lepore
5660bd904edSIan Lepore /*
5670bd904edSIan Lepore * curslave and curisread temporaries from above may refer to
5680bd904edSIan Lepore * the after-repstart msg, reset them to reflect sc_curmsg.
5690bd904edSIan Lepore */
5700bd904edSIan Lepore curisread = (sc->sc_curmsg->flags & IIC_M_RD) ? 1 : 0;
5710bd904edSIan Lepore curslave = sc->sc_curmsg->slave | curisread;
572be9ddf43SLuiz Otavio O Souza
573be9ddf43SLuiz Otavio O Souza /* Write the slave address. */
5740bd904edSIan Lepore BCM_BSC_WRITE(sc, BCM_BSC_SLAVE, curslave >> 1);
575be9ddf43SLuiz Otavio O Souza
5760bd904edSIan Lepore DEVICE_DEBUGF(sc, 2, "start 0x%02x\n", curslave);
577be9ddf43SLuiz Otavio O Souza
5780bd904edSIan Lepore /*
5790bd904edSIan Lepore * Either set up read length and direction variables for a
5800bd904edSIan Lepore * simple transfer or get the hardware started on the first
5810bd904edSIan Lepore * piece of a transfer that involves a repeat-start and set up
5820bd904edSIan Lepore * the read length and direction vars for the second piece.
5830bd904edSIan Lepore */
5840bd904edSIan Lepore if (sc->sc_replen == 0) {
5850bd904edSIan Lepore DEVICE_DEBUGF(sc, 1, "%-6s 0x%02x len %d: ",
586ae992394SIan Lepore (curisread) ? "read" : "write", curslave,
5870bd904edSIan Lepore sc->sc_totlen);
5880bd904edSIan Lepore curlen = sc->sc_totlen;
5890bd904edSIan Lepore if (curisread) {
5900bd904edSIan Lepore readctl = BCM_BSC_CTRL_READ;
591be9ddf43SLuiz Otavio O Souza sc->sc_flags |= BCM_I2C_READ;
5920bd904edSIan Lepore } else {
5930bd904edSIan Lepore readctl = 0;
5940bd904edSIan Lepore sc->sc_flags &= ~BCM_I2C_READ;
595be9ddf43SLuiz Otavio O Souza }
5960bd904edSIan Lepore } else {
5970bd904edSIan Lepore DEVICE_DEBUGF(sc, 1, "%-6s 0x%02x len %d: ",
598ae992394SIan Lepore (curisread) ? "read" : "write", curslave,
5990bd904edSIan Lepore sc->sc_replen);
600be9ddf43SLuiz Otavio O Souza
6010bd904edSIan Lepore /*
6020bd904edSIan Lepore * Start the write transfer with an empty fifo and wait
6030bd904edSIan Lepore * for the 'transfer active' status bit to light up;
6040bd904edSIan Lepore * that indicates that the hardware has latched the
6050bd904edSIan Lepore * direction and length for the write, and we can safely
6060bd904edSIan Lepore * reload those registers and issue the start for the
6070bd904edSIan Lepore * following read; interrupts are not enabled here.
6080bd904edSIan Lepore */
6090bd904edSIan Lepore BCM_BSC_WRITE(sc, BCM_BSC_DLEN, sc->sc_replen);
610be9ddf43SLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN |
6110bd904edSIan Lepore BCM_BSC_CTRL_ST);
6120bd904edSIan Lepore do {
6130bd904edSIan Lepore status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
6140bd904edSIan Lepore if (status & BCM_BSC_STATUS_ERR) {
6150bd904edSIan Lepore /* no ACK on slave addr */
6160bd904edSIan Lepore err = EIO;
6170bd904edSIan Lepore goto xfer_done;
6180bd904edSIan Lepore }
6190bd904edSIan Lepore } while ((status & BCM_BSC_STATUS_TA) == 0);
6200bd904edSIan Lepore /*
6210bd904edSIan Lepore * Set curlen and readctl for the repeat-start read that
6220bd904edSIan Lepore * we need to set up below, but set sc_flags to write,
6230bd904edSIan Lepore * because that is the operation in progress right now.
6240bd904edSIan Lepore */
6250bd904edSIan Lepore curlen = sc->sc_totlen - sc->sc_replen;
6260bd904edSIan Lepore readctl = BCM_BSC_CTRL_READ;
6270bd904edSIan Lepore sc->sc_flags &= ~BCM_I2C_READ;
6280bd904edSIan Lepore }
6290bd904edSIan Lepore
6300bd904edSIan Lepore /*
6310bd904edSIan Lepore * Start the transfer with interrupts enabled, then if doing a
6320bd904edSIan Lepore * write, fill the tx fifo. Not prefilling the fifo until after
6330bd904edSIan Lepore * this start command is the key workaround for making
6340bd904edSIan Lepore * repeat-start work, and it's harmless to do it in this order
6350bd904edSIan Lepore * for a regular write too.
6360bd904edSIan Lepore */
6370bd904edSIan Lepore BCM_BSC_WRITE(sc, BCM_BSC_DLEN, curlen);
6380bd904edSIan Lepore BCM_BSC_WRITE(sc, BCM_BSC_CTRL, readctl | BCM_BSC_CTRL_I2CEN |
6390bd904edSIan Lepore BCM_BSC_CTRL_ST | BCM_BSC_CTRL_INT_ALL);
6400bd904edSIan Lepore
6410bd904edSIan Lepore if (!(sc->sc_curmsg->flags & IIC_M_RD)) {
6420bd904edSIan Lepore bcm_bsc_fill_tx_fifo(sc);
6430bd904edSIan Lepore }
644be9ddf43SLuiz Otavio O Souza
645be9ddf43SLuiz Otavio O Souza /* Wait for the transaction to complete. */
6460bd904edSIan Lepore while (err == 0 && !(sc->sc_flags & BCM_I2C_DONE)) {
6470bd904edSIan Lepore err = mtx_sleep(sc, &sc->sc_mtx, 0, "bsciow", hz);
6480bd904edSIan Lepore }
64971b37cb9SLuiz Otavio O Souza /* Check for errors. */
6500a39cc71SLuiz Otavio O Souza if (err == 0 && (sc->sc_flags & BCM_I2C_ERROR))
651be9ddf43SLuiz Otavio O Souza err = EIO;
6520bd904edSIan Lepore xfer_done:
6530bd904edSIan Lepore DEBUGF(sc, 1, " err=%d\n", err);
6540bd904edSIan Lepore DEVICE_DEBUGF(sc, 2, "stop\n");
65571b37cb9SLuiz Otavio O Souza if (err != 0)
656be9ddf43SLuiz Otavio O Souza break;
657be9ddf43SLuiz Otavio O Souza }
658be9ddf43SLuiz Otavio O Souza
6590bd904edSIan Lepore /* Disable interrupts, clean fifo, etc. */
6600bd904edSIan Lepore bcm_bsc_reset(sc);
6610bd904edSIan Lepore
662be9ddf43SLuiz Otavio O Souza /* Clean the controller flags. */
663be9ddf43SLuiz Otavio O Souza sc->sc_flags = 0;
664be9ddf43SLuiz Otavio O Souza
665a141b672SLuiz Otavio O Souza /* Wake up the threads waiting for bus. */
666a141b672SLuiz Otavio O Souza wakeup(dev);
667a141b672SLuiz Otavio O Souza
668be9ddf43SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc);
669be9ddf43SLuiz Otavio O Souza
670be9ddf43SLuiz Otavio O Souza return (err);
671be9ddf43SLuiz Otavio O Souza }
672be9ddf43SLuiz Otavio O Souza
673e50c6241SLuiz Otavio O Souza static int
bcm_bsc_iicbus_reset(device_t dev,u_char speed,u_char addr,u_char * oldaddr)674e50c6241SLuiz Otavio O Souza bcm_bsc_iicbus_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
675e50c6241SLuiz Otavio O Souza {
676e50c6241SLuiz Otavio O Souza struct bcm_bsc_softc *sc;
6779e93dfcfSLuiz Otavio O Souza uint32_t busfreq;
678e50c6241SLuiz Otavio O Souza
679e50c6241SLuiz Otavio O Souza sc = device_get_softc(dev);
680e50c6241SLuiz Otavio O Souza BCM_BSC_LOCK(sc);
681e50c6241SLuiz Otavio O Souza bcm_bsc_reset(sc);
6829e93dfcfSLuiz Otavio O Souza if (sc->sc_iicbus == NULL)
6839e93dfcfSLuiz Otavio O Souza busfreq = 100000;
6849e93dfcfSLuiz Otavio O Souza else
6859e93dfcfSLuiz Otavio O Souza busfreq = IICBUS_GET_FREQUENCY(sc->sc_iicbus, speed);
6869e93dfcfSLuiz Otavio O Souza BCM_BSC_WRITE(sc, BCM_BSC_CLOCK, BCM_BSC_CORE_CLK / busfreq);
687e50c6241SLuiz Otavio O Souza BCM_BSC_UNLOCK(sc);
688e50c6241SLuiz Otavio O Souza
689e50c6241SLuiz Otavio O Souza return (IIC_ENOADDR);
690e50c6241SLuiz Otavio O Souza }
691e50c6241SLuiz Otavio O Souza
692be9ddf43SLuiz Otavio O Souza static phandle_t
bcm_bsc_get_node(device_t bus,device_t dev)693be9ddf43SLuiz Otavio O Souza bcm_bsc_get_node(device_t bus, device_t dev)
694be9ddf43SLuiz Otavio O Souza {
695be9ddf43SLuiz Otavio O Souza
696be9ddf43SLuiz Otavio O Souza /* We only have one child, the I2C bus, which needs our own node. */
697be9ddf43SLuiz Otavio O Souza return (ofw_bus_get_node(bus));
698be9ddf43SLuiz Otavio O Souza }
699be9ddf43SLuiz Otavio O Souza
700be9ddf43SLuiz Otavio O Souza static device_method_t bcm_bsc_methods[] = {
701be9ddf43SLuiz Otavio O Souza /* Device interface */
702be9ddf43SLuiz Otavio O Souza DEVMETHOD(device_probe, bcm_bsc_probe),
703be9ddf43SLuiz Otavio O Souza DEVMETHOD(device_attach, bcm_bsc_attach),
704be9ddf43SLuiz Otavio O Souza DEVMETHOD(device_detach, bcm_bsc_detach),
705be9ddf43SLuiz Otavio O Souza
706be9ddf43SLuiz Otavio O Souza /* iicbus interface */
707e50c6241SLuiz Otavio O Souza DEVMETHOD(iicbus_reset, bcm_bsc_iicbus_reset),
708be9ddf43SLuiz Otavio O Souza DEVMETHOD(iicbus_callback, iicbus_null_callback),
709be9ddf43SLuiz Otavio O Souza DEVMETHOD(iicbus_transfer, bcm_bsc_transfer),
710be9ddf43SLuiz Otavio O Souza
711be9ddf43SLuiz Otavio O Souza /* ofw_bus interface */
712be9ddf43SLuiz Otavio O Souza DEVMETHOD(ofw_bus_get_node, bcm_bsc_get_node),
713be9ddf43SLuiz Otavio O Souza
714be9ddf43SLuiz Otavio O Souza DEVMETHOD_END
715be9ddf43SLuiz Otavio O Souza };
716be9ddf43SLuiz Otavio O Souza
717be9ddf43SLuiz Otavio O Souza static driver_t bcm_bsc_driver = {
718be9ddf43SLuiz Otavio O Souza "iichb",
719be9ddf43SLuiz Otavio O Souza bcm_bsc_methods,
720be9ddf43SLuiz Otavio O Souza sizeof(struct bcm_bsc_softc),
721be9ddf43SLuiz Otavio O Souza };
722be9ddf43SLuiz Otavio O Souza
723676ea8e1SJohn Baldwin DRIVER_MODULE(iicbus, bcm2835_bsc, iicbus_driver, 0, 0);
72482d4dc06SJohn Baldwin DRIVER_MODULE(bcm2835_bsc, simplebus, bcm_bsc_driver, 0, 0);
725