xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_bsc.c (revision 71b37cb9ae53ee9dbd1a9fd90dc964be29c6774e)
1be9ddf43SLuiz Otavio O Souza /*-
2be9ddf43SLuiz Otavio O Souza  * Copyright (c) 2001 Tsubai Masanari.
3be9ddf43SLuiz Otavio O Souza  * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
4be9ddf43SLuiz Otavio O Souza  * Copyright (c) 2013 Luiz Otavio O Souza <loos@freebsd.org>
5be9ddf43SLuiz Otavio O Souza  * All rights reserved.
6be9ddf43SLuiz Otavio O Souza  *
7be9ddf43SLuiz Otavio O Souza  * Redistribution and use in source and binary forms, with or without
8be9ddf43SLuiz Otavio O Souza  * modification, are permitted provided that the following conditions
9be9ddf43SLuiz Otavio O Souza  * are met:
10be9ddf43SLuiz Otavio O Souza  * 1. Redistributions of source code must retain the above copyright
11be9ddf43SLuiz Otavio O Souza  *    notice, this list of conditions and the following disclaimer.
12be9ddf43SLuiz Otavio O Souza  * 2. Redistributions in binary form must reproduce the above copyright
13be9ddf43SLuiz Otavio O Souza  *    notice, this list of conditions and the following disclaimer in the
14be9ddf43SLuiz Otavio O Souza  *    documentation and/or other materials provided with the distribution.
15be9ddf43SLuiz Otavio O Souza  *
16be9ddf43SLuiz Otavio O Souza  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17be9ddf43SLuiz Otavio O Souza  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18be9ddf43SLuiz Otavio O Souza  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19be9ddf43SLuiz Otavio O Souza  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20be9ddf43SLuiz Otavio O Souza  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21be9ddf43SLuiz Otavio O Souza  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22be9ddf43SLuiz Otavio O Souza  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23be9ddf43SLuiz Otavio O Souza  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24be9ddf43SLuiz Otavio O Souza  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25be9ddf43SLuiz Otavio O Souza  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26be9ddf43SLuiz Otavio O Souza  * SUCH DAMAGE.
27be9ddf43SLuiz Otavio O Souza  *
28be9ddf43SLuiz Otavio O Souza  */
29be9ddf43SLuiz Otavio O Souza #include <sys/cdefs.h>
30be9ddf43SLuiz Otavio O Souza __FBSDID("$FreeBSD$");
31be9ddf43SLuiz Otavio O Souza 
32be9ddf43SLuiz Otavio O Souza #include <sys/param.h>
33be9ddf43SLuiz Otavio O Souza #include <sys/systm.h>
34be9ddf43SLuiz Otavio O Souza #include <sys/kernel.h>
35be9ddf43SLuiz Otavio O Souza #include <sys/lock.h>
36be9ddf43SLuiz Otavio O Souza #include <sys/module.h>
37be9ddf43SLuiz Otavio O Souza #include <sys/mutex.h>
38be9ddf43SLuiz Otavio O Souza #include <sys/bus.h>
39be9ddf43SLuiz Otavio O Souza #include <machine/resource.h>
40be9ddf43SLuiz Otavio O Souza #include <machine/bus.h>
41be9ddf43SLuiz Otavio O Souza #include <sys/rman.h>
42be9ddf43SLuiz Otavio O Souza #include <sys/sysctl.h>
43be9ddf43SLuiz Otavio O Souza 
44be9ddf43SLuiz Otavio O Souza #include <dev/iicbus/iicbus.h>
45be9ddf43SLuiz Otavio O Souza #include <dev/iicbus/iiconf.h>
46be9ddf43SLuiz Otavio O Souza #include <dev/ofw/ofw_bus.h>
47be9ddf43SLuiz Otavio O Souza #include <dev/ofw/ofw_bus_subr.h>
48be9ddf43SLuiz Otavio O Souza 
49be9ddf43SLuiz Otavio O Souza #include <arm/broadcom/bcm2835/bcm2835_gpio.h>
50be9ddf43SLuiz Otavio O Souza #include <arm/broadcom/bcm2835/bcm2835_bscreg.h>
51be9ddf43SLuiz Otavio O Souza #include <arm/broadcom/bcm2835/bcm2835_bscvar.h>
52be9ddf43SLuiz Otavio O Souza 
53be9ddf43SLuiz Otavio O Souza #include "iicbus_if.h"
54be9ddf43SLuiz Otavio O Souza 
55be9ddf43SLuiz Otavio O Souza static void bcm_bsc_intr(void *);
56be9ddf43SLuiz Otavio O Souza 
57be9ddf43SLuiz Otavio O Souza static void
58be9ddf43SLuiz Otavio O Souza bcm_bsc_modifyreg(struct bcm_bsc_softc *sc, uint32_t off, uint32_t mask,
59be9ddf43SLuiz Otavio O Souza 	uint32_t value)
60be9ddf43SLuiz Otavio O Souza {
61be9ddf43SLuiz Otavio O Souza 	uint32_t reg;
62be9ddf43SLuiz Otavio O Souza 
63be9ddf43SLuiz Otavio O Souza 	mtx_assert(&sc->sc_mtx, MA_OWNED);
64be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, off);
65be9ddf43SLuiz Otavio O Souza 	reg &= ~mask;
66be9ddf43SLuiz Otavio O Souza 	reg |= value;
67be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, off, reg);
68be9ddf43SLuiz Otavio O Souza }
69be9ddf43SLuiz Otavio O Souza 
70be9ddf43SLuiz Otavio O Souza static int
71be9ddf43SLuiz Otavio O Souza bcm_bsc_clock_proc(SYSCTL_HANDLER_ARGS)
72be9ddf43SLuiz Otavio O Souza {
73be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
74be9ddf43SLuiz Otavio O Souza 	uint32_t clk;
75be9ddf43SLuiz Otavio O Souza 	int error;
76be9ddf43SLuiz Otavio O Souza 
77be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
78be9ddf43SLuiz Otavio O Souza 
79be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
80be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
81be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
82be9ddf43SLuiz Otavio O Souza 	clk &= 0xffff;
83be9ddf43SLuiz Otavio O Souza 	if (clk == 0)
84be9ddf43SLuiz Otavio O Souza 		clk = 32768;
85be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
86be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &clk, sizeof(clk), req);
87be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
88be9ddf43SLuiz Otavio O Souza 		return (error);
89be9ddf43SLuiz Otavio O Souza 
90be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
91be9ddf43SLuiz Otavio O Souza 	if (clk % 2)
92be9ddf43SLuiz Otavio O Souza 		clk--;
93be9ddf43SLuiz Otavio O Souza 	if (clk > 0xffff)
94be9ddf43SLuiz Otavio O Souza 		clk = 0xffff;
95be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
96be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CLOCK, clk);
97be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
98be9ddf43SLuiz Otavio O Souza 
99be9ddf43SLuiz Otavio O Souza 	return (0);
100be9ddf43SLuiz Otavio O Souza }
101be9ddf43SLuiz Otavio O Souza 
102be9ddf43SLuiz Otavio O Souza static int
103be9ddf43SLuiz Otavio O Souza bcm_bsc_clkt_proc(SYSCTL_HANDLER_ARGS)
104be9ddf43SLuiz Otavio O Souza {
105be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
106be9ddf43SLuiz Otavio O Souza 	uint32_t clkt;
107be9ddf43SLuiz Otavio O Souza 	int error;
108be9ddf43SLuiz Otavio O Souza 
109be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
110be9ddf43SLuiz Otavio O Souza 
111be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
112be9ddf43SLuiz Otavio O Souza 	clkt = BCM_BSC_READ(sc, BCM_BSC_CLKT);
113be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
114be9ddf43SLuiz Otavio O Souza 	clkt &= 0xffff;
115be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &clkt, sizeof(clkt), req);
116be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
117be9ddf43SLuiz Otavio O Souza 		return (error);
118be9ddf43SLuiz Otavio O Souza 
119be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
120be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CLKT, clkt & 0xffff);
121be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
122be9ddf43SLuiz Otavio O Souza 
123be9ddf43SLuiz Otavio O Souza 	return (0);
124be9ddf43SLuiz Otavio O Souza }
125be9ddf43SLuiz Otavio O Souza 
126be9ddf43SLuiz Otavio O Souza static int
127be9ddf43SLuiz Otavio O Souza bcm_bsc_fall_proc(SYSCTL_HANDLER_ARGS)
128be9ddf43SLuiz Otavio O Souza {
129be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
130be9ddf43SLuiz Otavio O Souza 	uint32_t clk, reg;
131be9ddf43SLuiz Otavio O Souza 	int error;
132be9ddf43SLuiz Otavio O Souza 
133be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
134be9ddf43SLuiz Otavio O Souza 
135be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
136be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, BCM_BSC_DELAY);
137be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
138be9ddf43SLuiz Otavio O Souza 	reg >>= 16;
139be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
140be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
141be9ddf43SLuiz Otavio O Souza 		return (error);
142be9ddf43SLuiz Otavio O Souza 
143be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
144be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
145be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
146be9ddf43SLuiz Otavio O Souza 	if (reg > clk / 2)
147be9ddf43SLuiz Otavio O Souza 		reg = clk / 2 - 1;
148be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff0000, reg << 16);
149be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
150be9ddf43SLuiz Otavio O Souza 
151be9ddf43SLuiz Otavio O Souza 	return (0);
152be9ddf43SLuiz Otavio O Souza }
153be9ddf43SLuiz Otavio O Souza 
154be9ddf43SLuiz Otavio O Souza static int
155be9ddf43SLuiz Otavio O Souza bcm_bsc_rise_proc(SYSCTL_HANDLER_ARGS)
156be9ddf43SLuiz Otavio O Souza {
157be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
158be9ddf43SLuiz Otavio O Souza 	uint32_t clk, reg;
159be9ddf43SLuiz Otavio O Souza 	int error;
160be9ddf43SLuiz Otavio O Souza 
161be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
162be9ddf43SLuiz Otavio O Souza 
163be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
164be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, BCM_BSC_DELAY);
165be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
166be9ddf43SLuiz Otavio O Souza 	reg &= 0xffff;
167be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
168be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
169be9ddf43SLuiz Otavio O Souza 		return (error);
170be9ddf43SLuiz Otavio O Souza 
171be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
172be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
173be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
174be9ddf43SLuiz Otavio O Souza 	if (reg > clk / 2)
175be9ddf43SLuiz Otavio O Souza 		reg = clk / 2 - 1;
176be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff, reg);
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 void
183be9ddf43SLuiz Otavio O Souza bcm_bsc_sysctl_init(struct bcm_bsc_softc *sc)
184be9ddf43SLuiz Otavio O Souza {
185be9ddf43SLuiz Otavio O Souza 	struct sysctl_ctx_list *ctx;
186be9ddf43SLuiz Otavio O Souza 	struct sysctl_oid *tree_node;
187be9ddf43SLuiz Otavio O Souza 	struct sysctl_oid_list *tree;
188be9ddf43SLuiz Otavio O Souza 
189be9ddf43SLuiz Otavio O Souza 	/*
190be9ddf43SLuiz Otavio O Souza 	 * Add system sysctl tree/handlers.
191be9ddf43SLuiz Otavio O Souza 	 */
192be9ddf43SLuiz Otavio O Souza 	ctx = device_get_sysctl_ctx(sc->sc_dev);
193be9ddf43SLuiz Otavio O Souza 	tree_node = device_get_sysctl_tree(sc->sc_dev);
194be9ddf43SLuiz Otavio O Souza 	tree = SYSCTL_CHILDREN(tree_node);
195be9ddf43SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clock",
196be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
197be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_clock_proc, "IU", "I2C BUS clock frequency");
198be9ddf43SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clock_stretch",
199be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
200be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_clkt_proc, "IU", "I2C BUS clock stretch timeout");
201be9ddf43SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "fall_edge_delay",
202be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
203be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_fall_proc, "IU", "I2C BUS falling edge delay");
204be9ddf43SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "rise_edge_delay",
205be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
206be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_rise_proc, "IU", "I2C BUS rising edge delay");
207be9ddf43SLuiz Otavio O Souza }
208be9ddf43SLuiz Otavio O Souza 
209be9ddf43SLuiz Otavio O Souza static void
210be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(struct bcm_bsc_softc *sc)
211be9ddf43SLuiz Otavio O Souza {
212be9ddf43SLuiz Otavio O Souza 
213be9ddf43SLuiz Otavio O Souza 	/* Clear pending interrupts. */
214be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_STATUS, BCM_BSC_STATUS_CLKT |
215be9ddf43SLuiz Otavio O Souza 	    BCM_BSC_STATUS_ERR | BCM_BSC_STATUS_DONE);
216be9ddf43SLuiz Otavio O Souza 	/* Clear the FIFO. */
217be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_CLEAR0,
218be9ddf43SLuiz Otavio O Souza 	    BCM_BSC_CTRL_CLEAR0);
219be9ddf43SLuiz Otavio O Souza }
220be9ddf43SLuiz Otavio O Souza 
221be9ddf43SLuiz Otavio O Souza static int
222be9ddf43SLuiz Otavio O Souza bcm_bsc_probe(device_t dev)
223be9ddf43SLuiz Otavio O Souza {
224be9ddf43SLuiz Otavio O Souza 
225add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
226add35ed5SIan Lepore 		return (ENXIO);
227add35ed5SIan Lepore 
228be9ddf43SLuiz Otavio O Souza 	if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-bsc"))
229be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
230be9ddf43SLuiz Otavio O Souza 
231be9ddf43SLuiz Otavio O Souza 	device_set_desc(dev, "BCM2708/2835 BSC controller");
232be9ddf43SLuiz Otavio O Souza 
233be9ddf43SLuiz Otavio O Souza 	return (BUS_PROBE_DEFAULT);
234be9ddf43SLuiz Otavio O Souza }
235be9ddf43SLuiz Otavio O Souza 
236be9ddf43SLuiz Otavio O Souza static int
237be9ddf43SLuiz Otavio O Souza bcm_bsc_attach(device_t dev)
238be9ddf43SLuiz Otavio O Souza {
239be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
24021cb1342SLuiz Otavio O Souza 	unsigned long start;
241be9ddf43SLuiz Otavio O Souza 	device_t gpio;
24221cb1342SLuiz Otavio O Souza 	int i, rid;
243be9ddf43SLuiz Otavio O Souza 
244be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
245be9ddf43SLuiz Otavio O Souza 	sc->sc_dev = dev;
246be9ddf43SLuiz Otavio O Souza 
247be9ddf43SLuiz Otavio O Souza 	rid = 0;
248be9ddf43SLuiz Otavio O Souza 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
249be9ddf43SLuiz Otavio O Souza 	    RF_ACTIVE);
250be9ddf43SLuiz Otavio O Souza 	if (!sc->sc_mem_res) {
251be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate memory window\n");
252be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
253be9ddf43SLuiz Otavio O Souza 	}
254be9ddf43SLuiz Otavio O Souza 
255be9ddf43SLuiz Otavio O Souza 	sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
256be9ddf43SLuiz Otavio O Souza 	sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
257be9ddf43SLuiz Otavio O Souza 
25821cb1342SLuiz Otavio O Souza 	/* Check the unit we are attaching by its base address. */
25921cb1342SLuiz Otavio O Souza 	start = rman_get_start(sc->sc_mem_res);
26021cb1342SLuiz Otavio O Souza 	for (i = 0; i < nitems(bcm_bsc_pins); i++) {
26121cb1342SLuiz Otavio O Souza 		if (bcm_bsc_pins[i].start == start)
26221cb1342SLuiz Otavio O Souza 			break;
26321cb1342SLuiz Otavio O Souza 	}
26421cb1342SLuiz Otavio O Souza 	if (i == nitems(bcm_bsc_pins)) {
26521cb1342SLuiz Otavio O Souza 		device_printf(dev, "only bsc0 and bsc1 are supported\n");
26621cb1342SLuiz Otavio O Souza 		return (ENXIO);
26721cb1342SLuiz Otavio O Souza 	}
26821cb1342SLuiz Otavio O Souza 
26921cb1342SLuiz Otavio O Souza 	/*
27021cb1342SLuiz Otavio O Souza 	 * Configure the GPIO pins to ALT0 function to enable BSC control
27121cb1342SLuiz Otavio O Souza 	 * over the pins.
27221cb1342SLuiz Otavio O Souza 	 */
27321cb1342SLuiz Otavio O Souza 	gpio = devclass_get_device(devclass_find("gpio"), 0);
27421cb1342SLuiz Otavio O Souza 	if (!gpio) {
27521cb1342SLuiz Otavio O Souza 		device_printf(dev, "cannot find gpio0\n");
27621cb1342SLuiz Otavio O Souza 		return (ENXIO);
27721cb1342SLuiz Otavio O Souza 	}
27821cb1342SLuiz Otavio O Souza 	bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].sda, BCM_GPIO_ALT0);
27921cb1342SLuiz Otavio O Souza 	bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].scl, BCM_GPIO_ALT0);
28021cb1342SLuiz Otavio O Souza 
281be9ddf43SLuiz Otavio O Souza 	rid = 0;
282be9ddf43SLuiz Otavio O Souza 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
283be9ddf43SLuiz Otavio O Souza 	    RF_ACTIVE | RF_SHAREABLE);
284be9ddf43SLuiz Otavio O Souza 	if (!sc->sc_irq_res) {
285be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
286be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate interrupt\n");
287be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
288be9ddf43SLuiz Otavio O Souza 	}
289be9ddf43SLuiz Otavio O Souza 
290be9ddf43SLuiz Otavio O Souza 	/* Hook up our interrupt handler. */
291be9ddf43SLuiz Otavio O Souza 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
292be9ddf43SLuiz Otavio O Souza 	    NULL, bcm_bsc_intr, sc, &sc->sc_intrhand)) {
293be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
294be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
295be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot setup the interrupt handler\n");
296be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
297be9ddf43SLuiz Otavio O Souza 	}
298be9ddf43SLuiz Otavio O Souza 
299be9ddf43SLuiz Otavio O Souza 	mtx_init(&sc->sc_mtx, "bcm_bsc", NULL, MTX_DEF);
300be9ddf43SLuiz Otavio O Souza 
301be9ddf43SLuiz Otavio O Souza 	bcm_bsc_sysctl_init(sc);
302be9ddf43SLuiz Otavio O Souza 
303be9ddf43SLuiz Otavio O Souza 	/* Enable the BSC controller.  Flush the FIFO. */
304be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
305be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN);
306be9ddf43SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
307be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
308be9ddf43SLuiz Otavio O Souza 
309be9ddf43SLuiz Otavio O Souza 	device_add_child(dev, "iicbus", -1);
310be9ddf43SLuiz Otavio O Souza 
311be9ddf43SLuiz Otavio O Souza 	return (bus_generic_attach(dev));
312be9ddf43SLuiz Otavio O Souza }
313be9ddf43SLuiz Otavio O Souza 
314be9ddf43SLuiz Otavio O Souza static int
315be9ddf43SLuiz Otavio O Souza bcm_bsc_detach(device_t dev)
316be9ddf43SLuiz Otavio O Souza {
317be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
318be9ddf43SLuiz Otavio O Souza 
319be9ddf43SLuiz Otavio O Souza 	bus_generic_detach(dev);
320be9ddf43SLuiz Otavio O Souza 
321be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
322be9ddf43SLuiz Otavio O Souza 	mtx_destroy(&sc->sc_mtx);
323be9ddf43SLuiz Otavio O Souza 	if (sc->sc_intrhand)
324be9ddf43SLuiz Otavio O Souza 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
325be9ddf43SLuiz Otavio O Souza 	if (sc->sc_irq_res)
326be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
327be9ddf43SLuiz Otavio O Souza 	if (sc->sc_mem_res)
328be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
329be9ddf43SLuiz Otavio O Souza 
330be9ddf43SLuiz Otavio O Souza 	return (0);
331be9ddf43SLuiz Otavio O Souza }
332be9ddf43SLuiz Otavio O Souza 
333be9ddf43SLuiz Otavio O Souza static void
334be9ddf43SLuiz Otavio O Souza bcm_bsc_intr(void *arg)
335be9ddf43SLuiz Otavio O Souza {
336be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
337be9ddf43SLuiz Otavio O Souza 	uint32_t status;
338be9ddf43SLuiz Otavio O Souza 
339be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg;
340be9ddf43SLuiz Otavio O Souza 
341be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
342be9ddf43SLuiz Otavio O Souza 
343be9ddf43SLuiz Otavio O Souza 	/* The I2C interrupt is shared among all the BSC controllers. */
344be9ddf43SLuiz Otavio O Souza 	if ((sc->sc_flags & BCM_I2C_BUSY) == 0) {
345be9ddf43SLuiz Otavio O Souza 		BCM_BSC_UNLOCK(sc);
346be9ddf43SLuiz Otavio O Souza 		return;
347be9ddf43SLuiz Otavio O Souza 	}
348be9ddf43SLuiz Otavio O Souza 
349be9ddf43SLuiz Otavio O Souza 	status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
350be9ddf43SLuiz Otavio O Souza 
351be9ddf43SLuiz Otavio O Souza 	/* Check for errors. */
352be9ddf43SLuiz Otavio O Souza 	if (status & (BCM_BSC_STATUS_CLKT | BCM_BSC_STATUS_ERR)) {
353be9ddf43SLuiz Otavio O Souza 		/* Disable interrupts. */
354be9ddf43SLuiz Otavio O Souza 		BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN);
355be9ddf43SLuiz Otavio O Souza 		sc->sc_flags |= BCM_I2C_ERROR;
356be9ddf43SLuiz Otavio O Souza 		bcm_bsc_reset(sc);
357be9ddf43SLuiz Otavio O Souza 		wakeup(sc->sc_dev);
358be9ddf43SLuiz Otavio O Souza 		BCM_BSC_UNLOCK(sc);
359be9ddf43SLuiz Otavio O Souza 		return;
360be9ddf43SLuiz Otavio O Souza 	}
361be9ddf43SLuiz Otavio O Souza 
362be9ddf43SLuiz Otavio O Souza 	if (sc->sc_flags & BCM_I2C_READ) {
363be9ddf43SLuiz Otavio O Souza 		while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_RXD)) {
364be9ddf43SLuiz Otavio O Souza 			*sc->sc_data++ = BCM_BSC_READ(sc, BCM_BSC_DATA);
365be9ddf43SLuiz Otavio O Souza 			sc->sc_resid--;
366be9ddf43SLuiz Otavio O Souza 			status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
367be9ddf43SLuiz Otavio O Souza 		}
368be9ddf43SLuiz Otavio O Souza 	} else {
369be9ddf43SLuiz Otavio O Souza 		while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_TXD)) {
370be9ddf43SLuiz Otavio O Souza 			BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data++);
371be9ddf43SLuiz Otavio O Souza 			sc->sc_resid--;
372be9ddf43SLuiz Otavio O Souza 			status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
373be9ddf43SLuiz Otavio O Souza 		}
374be9ddf43SLuiz Otavio O Souza 	}
375be9ddf43SLuiz Otavio O Souza 
376be9ddf43SLuiz Otavio O Souza 	if (status & BCM_BSC_STATUS_DONE) {
377be9ddf43SLuiz Otavio O Souza 		/* Disable interrupts. */
378be9ddf43SLuiz Otavio O Souza 		BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN);
379be9ddf43SLuiz Otavio O Souza 		bcm_bsc_reset(sc);
380be9ddf43SLuiz Otavio O Souza 		wakeup(sc->sc_dev);
381be9ddf43SLuiz Otavio O Souza 	}
382be9ddf43SLuiz Otavio O Souza 
383be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
384be9ddf43SLuiz Otavio O Souza }
385be9ddf43SLuiz Otavio O Souza 
386be9ddf43SLuiz Otavio O Souza static int
387be9ddf43SLuiz Otavio O Souza bcm_bsc_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
388be9ddf43SLuiz Otavio O Souza {
389be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
390be9ddf43SLuiz Otavio O Souza 	uint32_t intr, read, status;
391be9ddf43SLuiz Otavio O Souza 	int i, err;
392be9ddf43SLuiz Otavio O Souza 
393be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
394be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
395be9ddf43SLuiz Otavio O Souza 
396be9ddf43SLuiz Otavio O Souza 	/* If the controller is busy wait until it is available. */
397be9ddf43SLuiz Otavio O Souza 	while (sc->sc_flags & BCM_I2C_BUSY)
398a141b672SLuiz Otavio O Souza 		mtx_sleep(dev, &sc->sc_mtx, 0, "bscbusw", 0);
399be9ddf43SLuiz Otavio O Souza 
400be9ddf43SLuiz Otavio O Souza 	/* Now we have control over the BSC controller. */
401be9ddf43SLuiz Otavio O Souza 	sc->sc_flags = BCM_I2C_BUSY;
402be9ddf43SLuiz Otavio O Souza 
403be9ddf43SLuiz Otavio O Souza 	/* Clear the FIFO and the pending interrupts. */
404be9ddf43SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
405be9ddf43SLuiz Otavio O Souza 
406be9ddf43SLuiz Otavio O Souza 	err = 0;
407be9ddf43SLuiz Otavio O Souza 	for (i = 0; i < nmsgs; i++) {
408be9ddf43SLuiz Otavio O Souza 
409be9ddf43SLuiz Otavio O Souza 		/* Write the slave address. */
410d6cf3c63SLuiz Otavio O Souza 		BCM_BSC_WRITE(sc, BCM_BSC_SLAVE, msgs[i].slave >> 1);
411be9ddf43SLuiz Otavio O Souza 
412be9ddf43SLuiz Otavio O Souza 		/* Write the data length. */
413be9ddf43SLuiz Otavio O Souza 		BCM_BSC_WRITE(sc, BCM_BSC_DLEN, msgs[i].len);
414be9ddf43SLuiz Otavio O Souza 
415be9ddf43SLuiz Otavio O Souza 		sc->sc_data = msgs[i].buf;
416be9ddf43SLuiz Otavio O Souza 		sc->sc_resid = msgs[i].len;
417be9ddf43SLuiz Otavio O Souza 		if ((msgs[i].flags & IIC_M_RD) == 0) {
418be9ddf43SLuiz Otavio O Souza 			/* Fill up the TX FIFO. */
419be9ddf43SLuiz Otavio O Souza 			status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
420be9ddf43SLuiz Otavio O Souza 			while (sc->sc_resid > 0 &&
421be9ddf43SLuiz Otavio O Souza 			    (status & BCM_BSC_STATUS_TXD)) {
422be9ddf43SLuiz Otavio O Souza 				BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data);
423be9ddf43SLuiz Otavio O Souza 				sc->sc_data++;
424be9ddf43SLuiz Otavio O Souza 				sc->sc_resid--;
425be9ddf43SLuiz Otavio O Souza 				status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
426be9ddf43SLuiz Otavio O Souza 			}
427be9ddf43SLuiz Otavio O Souza 			read = 0;
428be9ddf43SLuiz Otavio O Souza 			intr = BCM_BSC_CTRL_INTT;
429be9ddf43SLuiz Otavio O Souza 			sc->sc_flags &= ~BCM_I2C_READ;
430be9ddf43SLuiz Otavio O Souza 		} else {
431be9ddf43SLuiz Otavio O Souza 			sc->sc_flags |= BCM_I2C_READ;
432be9ddf43SLuiz Otavio O Souza 			read = BCM_BSC_CTRL_READ;
433be9ddf43SLuiz Otavio O Souza 			intr = BCM_BSC_CTRL_INTR;
434be9ddf43SLuiz Otavio O Souza 		}
435be9ddf43SLuiz Otavio O Souza 		intr |= BCM_BSC_CTRL_INTD;
436be9ddf43SLuiz Otavio O Souza 
437be9ddf43SLuiz Otavio O Souza 		/* Start the transfer. */
438be9ddf43SLuiz Otavio O Souza 		BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN |
439be9ddf43SLuiz Otavio O Souza 		    BCM_BSC_CTRL_ST | read | intr);
440be9ddf43SLuiz Otavio O Souza 
441be9ddf43SLuiz Otavio O Souza 		/* Wait for the transaction to complete. */
442a141b672SLuiz Otavio O Souza 		err = mtx_sleep(dev, &sc->sc_mtx, 0, "bsciow", hz);
443be9ddf43SLuiz Otavio O Souza 
444*71b37cb9SLuiz Otavio O Souza 		/* Check for errors. */
445*71b37cb9SLuiz Otavio O Souza 		if (err != 0 && (sc->sc_flags & BCM_I2C_ERROR))
446be9ddf43SLuiz Otavio O Souza 			err = EIO;
447*71b37cb9SLuiz Otavio O Souza 		if (err != 0)
448be9ddf43SLuiz Otavio O Souza 			break;
449be9ddf43SLuiz Otavio O Souza 	}
450be9ddf43SLuiz Otavio O Souza 
451be9ddf43SLuiz Otavio O Souza 	/* Clean the controller flags. */
452be9ddf43SLuiz Otavio O Souza 	sc->sc_flags = 0;
453be9ddf43SLuiz Otavio O Souza 
454a141b672SLuiz Otavio O Souza 	/* Wake up the threads waiting for bus. */
455a141b672SLuiz Otavio O Souza 	wakeup(dev);
456a141b672SLuiz Otavio O Souza 
457be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
458be9ddf43SLuiz Otavio O Souza 
459be9ddf43SLuiz Otavio O Souza 	return (err);
460be9ddf43SLuiz Otavio O Souza }
461be9ddf43SLuiz Otavio O Souza 
462be9ddf43SLuiz Otavio O Souza static phandle_t
463be9ddf43SLuiz Otavio O Souza bcm_bsc_get_node(device_t bus, device_t dev)
464be9ddf43SLuiz Otavio O Souza {
465be9ddf43SLuiz Otavio O Souza 
466be9ddf43SLuiz Otavio O Souza 	/* We only have one child, the I2C bus, which needs our own node. */
467be9ddf43SLuiz Otavio O Souza 	return (ofw_bus_get_node(bus));
468be9ddf43SLuiz Otavio O Souza }
469be9ddf43SLuiz Otavio O Souza 
470be9ddf43SLuiz Otavio O Souza static device_method_t bcm_bsc_methods[] = {
471be9ddf43SLuiz Otavio O Souza 	/* Device interface */
472be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_probe,		bcm_bsc_probe),
473be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_attach,	bcm_bsc_attach),
474be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_detach,	bcm_bsc_detach),
475be9ddf43SLuiz Otavio O Souza 
476be9ddf43SLuiz Otavio O Souza 	/* iicbus interface */
477be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(iicbus_callback,	iicbus_null_callback),
478be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(iicbus_transfer,	bcm_bsc_transfer),
479be9ddf43SLuiz Otavio O Souza 
480be9ddf43SLuiz Otavio O Souza 	/* ofw_bus interface */
481be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(ofw_bus_get_node,	bcm_bsc_get_node),
482be9ddf43SLuiz Otavio O Souza 
483be9ddf43SLuiz Otavio O Souza 	DEVMETHOD_END
484be9ddf43SLuiz Otavio O Souza };
485be9ddf43SLuiz Otavio O Souza 
486be9ddf43SLuiz Otavio O Souza static devclass_t bcm_bsc_devclass;
487be9ddf43SLuiz Otavio O Souza 
488be9ddf43SLuiz Otavio O Souza static driver_t bcm_bsc_driver = {
489be9ddf43SLuiz Otavio O Souza 	"iichb",
490be9ddf43SLuiz Otavio O Souza 	bcm_bsc_methods,
491be9ddf43SLuiz Otavio O Souza 	sizeof(struct bcm_bsc_softc),
492be9ddf43SLuiz Otavio O Souza };
493be9ddf43SLuiz Otavio O Souza 
494be9ddf43SLuiz Otavio O Souza DRIVER_MODULE(iicbus, bcm2835_bsc, iicbus_driver, iicbus_devclass, 0, 0);
495be9ddf43SLuiz Otavio O Souza DRIVER_MODULE(bcm2835_bsc, simplebus, bcm_bsc_driver, bcm_bsc_devclass, 0, 0);
496