xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_bsc.c (revision 7f6897133f88725b4b1f70bf0708f89424ebf3de)
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 
225be9ddf43SLuiz Otavio O Souza 	if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-bsc"))
226be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
227be9ddf43SLuiz Otavio O Souza 
228be9ddf43SLuiz Otavio O Souza 	device_set_desc(dev, "BCM2708/2835 BSC controller");
229be9ddf43SLuiz Otavio O Souza 
230be9ddf43SLuiz Otavio O Souza 	return (BUS_PROBE_DEFAULT);
231be9ddf43SLuiz Otavio O Souza }
232be9ddf43SLuiz Otavio O Souza 
233be9ddf43SLuiz Otavio O Souza static int
234be9ddf43SLuiz Otavio O Souza bcm_bsc_attach(device_t dev)
235be9ddf43SLuiz Otavio O Souza {
236be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
23721cb1342SLuiz Otavio O Souza 	unsigned long start;
238be9ddf43SLuiz Otavio O Souza 	device_t gpio;
23921cb1342SLuiz Otavio O Souza 	int i, rid;
240be9ddf43SLuiz Otavio O Souza 
241be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
242be9ddf43SLuiz Otavio O Souza 	sc->sc_dev = dev;
243be9ddf43SLuiz Otavio O Souza 
244be9ddf43SLuiz Otavio O Souza 	rid = 0;
245be9ddf43SLuiz Otavio O Souza 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
246be9ddf43SLuiz Otavio O Souza 	    RF_ACTIVE);
247be9ddf43SLuiz Otavio O Souza 	if (!sc->sc_mem_res) {
248be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate memory window\n");
249be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
250be9ddf43SLuiz Otavio O Souza 	}
251be9ddf43SLuiz Otavio O Souza 
252be9ddf43SLuiz Otavio O Souza 	sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
253be9ddf43SLuiz Otavio O Souza 	sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
254be9ddf43SLuiz Otavio O Souza 
25521cb1342SLuiz Otavio O Souza 	/* Check the unit we are attaching by its base address. */
25621cb1342SLuiz Otavio O Souza 	start = rman_get_start(sc->sc_mem_res);
25721cb1342SLuiz Otavio O Souza 	for (i = 0; i < nitems(bcm_bsc_pins); i++) {
25821cb1342SLuiz Otavio O Souza 		if (bcm_bsc_pins[i].start == start)
25921cb1342SLuiz Otavio O Souza 			break;
26021cb1342SLuiz Otavio O Souza 	}
26121cb1342SLuiz Otavio O Souza 	if (i == nitems(bcm_bsc_pins)) {
26221cb1342SLuiz Otavio O Souza 		device_printf(dev, "only bsc0 and bsc1 are supported\n");
26321cb1342SLuiz Otavio O Souza 		return (ENXIO);
26421cb1342SLuiz Otavio O Souza 	}
26521cb1342SLuiz Otavio O Souza 
26621cb1342SLuiz Otavio O Souza 	/*
26721cb1342SLuiz Otavio O Souza 	 * Configure the GPIO pins to ALT0 function to enable BSC control
26821cb1342SLuiz Otavio O Souza 	 * over the pins.
26921cb1342SLuiz Otavio O Souza 	 */
27021cb1342SLuiz Otavio O Souza 	gpio = devclass_get_device(devclass_find("gpio"), 0);
27121cb1342SLuiz Otavio O Souza 	if (!gpio) {
27221cb1342SLuiz Otavio O Souza 		device_printf(dev, "cannot find gpio0\n");
27321cb1342SLuiz Otavio O Souza 		return (ENXIO);
27421cb1342SLuiz Otavio O Souza 	}
27521cb1342SLuiz Otavio O Souza 	bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].sda, BCM_GPIO_ALT0);
27621cb1342SLuiz Otavio O Souza 	bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].scl, BCM_GPIO_ALT0);
27721cb1342SLuiz Otavio O Souza 
278be9ddf43SLuiz Otavio O Souza 	rid = 0;
279be9ddf43SLuiz Otavio O Souza 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
280be9ddf43SLuiz Otavio O Souza 	    RF_ACTIVE | RF_SHAREABLE);
281be9ddf43SLuiz Otavio O Souza 	if (!sc->sc_irq_res) {
282be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
283be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate interrupt\n");
284be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
285be9ddf43SLuiz Otavio O Souza 	}
286be9ddf43SLuiz Otavio O Souza 
287be9ddf43SLuiz Otavio O Souza 	/* Hook up our interrupt handler. */
288be9ddf43SLuiz Otavio O Souza 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
289be9ddf43SLuiz Otavio O Souza 	    NULL, bcm_bsc_intr, sc, &sc->sc_intrhand)) {
290be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
291be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
292be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot setup the interrupt handler\n");
293be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
294be9ddf43SLuiz Otavio O Souza 	}
295be9ddf43SLuiz Otavio O Souza 
296be9ddf43SLuiz Otavio O Souza 	mtx_init(&sc->sc_mtx, "bcm_bsc", NULL, MTX_DEF);
297be9ddf43SLuiz Otavio O Souza 
298be9ddf43SLuiz Otavio O Souza 	bcm_bsc_sysctl_init(sc);
299be9ddf43SLuiz Otavio O Souza 
300be9ddf43SLuiz Otavio O Souza 	/* Enable the BSC controller.  Flush the FIFO. */
301be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
302be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN);
303be9ddf43SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
304be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
305be9ddf43SLuiz Otavio O Souza 
306be9ddf43SLuiz Otavio O Souza 	device_add_child(dev, "iicbus", -1);
307be9ddf43SLuiz Otavio O Souza 
308be9ddf43SLuiz Otavio O Souza 	return (bus_generic_attach(dev));
309be9ddf43SLuiz Otavio O Souza }
310be9ddf43SLuiz Otavio O Souza 
311be9ddf43SLuiz Otavio O Souza static int
312be9ddf43SLuiz Otavio O Souza bcm_bsc_detach(device_t dev)
313be9ddf43SLuiz Otavio O Souza {
314be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
315be9ddf43SLuiz Otavio O Souza 
316be9ddf43SLuiz Otavio O Souza 	bus_generic_detach(dev);
317be9ddf43SLuiz Otavio O Souza 
318be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
319be9ddf43SLuiz Otavio O Souza 	mtx_destroy(&sc->sc_mtx);
320be9ddf43SLuiz Otavio O Souza 	if (sc->sc_intrhand)
321be9ddf43SLuiz Otavio O Souza 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
322be9ddf43SLuiz Otavio O Souza 	if (sc->sc_irq_res)
323be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
324be9ddf43SLuiz Otavio O Souza 	if (sc->sc_mem_res)
325be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
326be9ddf43SLuiz Otavio O Souza 
327be9ddf43SLuiz Otavio O Souza 	return (0);
328be9ddf43SLuiz Otavio O Souza }
329be9ddf43SLuiz Otavio O Souza 
330be9ddf43SLuiz Otavio O Souza static void
331be9ddf43SLuiz Otavio O Souza bcm_bsc_intr(void *arg)
332be9ddf43SLuiz Otavio O Souza {
333be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
334be9ddf43SLuiz Otavio O Souza 	uint32_t status;
335be9ddf43SLuiz Otavio O Souza 
336be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg;
337be9ddf43SLuiz Otavio O Souza 
338be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
339be9ddf43SLuiz Otavio O Souza 
340be9ddf43SLuiz Otavio O Souza 	/* The I2C interrupt is shared among all the BSC controllers. */
341be9ddf43SLuiz Otavio O Souza 	if ((sc->sc_flags & BCM_I2C_BUSY) == 0) {
342be9ddf43SLuiz Otavio O Souza 		BCM_BSC_UNLOCK(sc);
343be9ddf43SLuiz Otavio O Souza 		return;
344be9ddf43SLuiz Otavio O Souza 	}
345be9ddf43SLuiz Otavio O Souza 
346be9ddf43SLuiz Otavio O Souza 	status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
347be9ddf43SLuiz Otavio O Souza 
348be9ddf43SLuiz Otavio O Souza 	/* Check for errors. */
349be9ddf43SLuiz Otavio O Souza 	if (status & (BCM_BSC_STATUS_CLKT | BCM_BSC_STATUS_ERR)) {
350be9ddf43SLuiz Otavio O Souza 		/* Disable interrupts. */
351be9ddf43SLuiz Otavio O Souza 		BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN);
352be9ddf43SLuiz Otavio O Souza 		sc->sc_flags |= BCM_I2C_ERROR;
353be9ddf43SLuiz Otavio O Souza 		bcm_bsc_reset(sc);
354be9ddf43SLuiz Otavio O Souza 		wakeup(sc->sc_dev);
355be9ddf43SLuiz Otavio O Souza 		BCM_BSC_UNLOCK(sc);
356be9ddf43SLuiz Otavio O Souza 		return;
357be9ddf43SLuiz Otavio O Souza 	}
358be9ddf43SLuiz Otavio O Souza 
359be9ddf43SLuiz Otavio O Souza 	if (sc->sc_flags & BCM_I2C_READ) {
360be9ddf43SLuiz Otavio O Souza 		while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_RXD)) {
361be9ddf43SLuiz Otavio O Souza 			*sc->sc_data++ = BCM_BSC_READ(sc, BCM_BSC_DATA);
362be9ddf43SLuiz Otavio O Souza 			sc->sc_resid--;
363be9ddf43SLuiz Otavio O Souza 			status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
364be9ddf43SLuiz Otavio O Souza 		}
365be9ddf43SLuiz Otavio O Souza 	} else {
366be9ddf43SLuiz Otavio O Souza 		while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_TXD)) {
367be9ddf43SLuiz Otavio O Souza 			BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data++);
368be9ddf43SLuiz Otavio O Souza 			sc->sc_resid--;
369be9ddf43SLuiz Otavio O Souza 			status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
370be9ddf43SLuiz Otavio O Souza 		}
371be9ddf43SLuiz Otavio O Souza 	}
372be9ddf43SLuiz Otavio O Souza 
373be9ddf43SLuiz Otavio O Souza 	if (status & BCM_BSC_STATUS_DONE) {
374be9ddf43SLuiz Otavio O Souza 		/* Disable interrupts. */
375be9ddf43SLuiz Otavio O Souza 		BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN);
376be9ddf43SLuiz Otavio O Souza 		bcm_bsc_reset(sc);
377be9ddf43SLuiz Otavio O Souza 		wakeup(sc->sc_dev);
378be9ddf43SLuiz Otavio O Souza 	}
379be9ddf43SLuiz Otavio O Souza 
380be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
381be9ddf43SLuiz Otavio O Souza }
382be9ddf43SLuiz Otavio O Souza 
383be9ddf43SLuiz Otavio O Souza static int
384be9ddf43SLuiz Otavio O Souza bcm_bsc_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
385be9ddf43SLuiz Otavio O Souza {
386be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
387be9ddf43SLuiz Otavio O Souza 	uint32_t intr, read, status;
388be9ddf43SLuiz Otavio O Souza 	int i, err;
389be9ddf43SLuiz Otavio O Souza 
390be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
391be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
392be9ddf43SLuiz Otavio O Souza 
393be9ddf43SLuiz Otavio O Souza 	/* If the controller is busy wait until it is available. */
394be9ddf43SLuiz Otavio O Souza 	while (sc->sc_flags & BCM_I2C_BUSY)
395be9ddf43SLuiz Otavio O Souza 		mtx_sleep(dev, &sc->sc_mtx, 0, "bcm_bsc", 0);
396be9ddf43SLuiz Otavio O Souza 
397be9ddf43SLuiz Otavio O Souza 	/* Now we have control over the BSC controller. */
398be9ddf43SLuiz Otavio O Souza 	sc->sc_flags = BCM_I2C_BUSY;
399be9ddf43SLuiz Otavio O Souza 
400be9ddf43SLuiz Otavio O Souza 	/* Clear the FIFO and the pending interrupts. */
401be9ddf43SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
402be9ddf43SLuiz Otavio O Souza 
403be9ddf43SLuiz Otavio O Souza 	err = 0;
404be9ddf43SLuiz Otavio O Souza 	for (i = 0; i < nmsgs; i++) {
405be9ddf43SLuiz Otavio O Souza 
406be9ddf43SLuiz Otavio O Souza 		/* Write the slave address. */
407*7f689713SLuiz Otavio O Souza 		BCM_BSC_WRITE(sc, BCM_BSC_SLAVE, msgs[i].slave);
408be9ddf43SLuiz Otavio O Souza 
409be9ddf43SLuiz Otavio O Souza 		/* Write the data length. */
410be9ddf43SLuiz Otavio O Souza 		BCM_BSC_WRITE(sc, BCM_BSC_DLEN, msgs[i].len);
411be9ddf43SLuiz Otavio O Souza 
412be9ddf43SLuiz Otavio O Souza 		sc->sc_data = msgs[i].buf;
413be9ddf43SLuiz Otavio O Souza 		sc->sc_resid = msgs[i].len;
414be9ddf43SLuiz Otavio O Souza 		if ((msgs[i].flags & IIC_M_RD) == 0) {
415be9ddf43SLuiz Otavio O Souza 			/* Fill up the TX FIFO. */
416be9ddf43SLuiz Otavio O Souza 			status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
417be9ddf43SLuiz Otavio O Souza 			while (sc->sc_resid > 0 &&
418be9ddf43SLuiz Otavio O Souza 			    (status & BCM_BSC_STATUS_TXD)) {
419be9ddf43SLuiz Otavio O Souza 				BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data);
420be9ddf43SLuiz Otavio O Souza 				sc->sc_data++;
421be9ddf43SLuiz Otavio O Souza 				sc->sc_resid--;
422be9ddf43SLuiz Otavio O Souza 				status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
423be9ddf43SLuiz Otavio O Souza 			}
424be9ddf43SLuiz Otavio O Souza 			read = 0;
425be9ddf43SLuiz Otavio O Souza 			intr = BCM_BSC_CTRL_INTT;
426be9ddf43SLuiz Otavio O Souza 			sc->sc_flags &= ~BCM_I2C_READ;
427be9ddf43SLuiz Otavio O Souza 		} else {
428be9ddf43SLuiz Otavio O Souza 			sc->sc_flags |= BCM_I2C_READ;
429be9ddf43SLuiz Otavio O Souza 			read = BCM_BSC_CTRL_READ;
430be9ddf43SLuiz Otavio O Souza 			intr = BCM_BSC_CTRL_INTR;
431be9ddf43SLuiz Otavio O Souza 		}
432be9ddf43SLuiz Otavio O Souza 		intr |= BCM_BSC_CTRL_INTD;
433be9ddf43SLuiz Otavio O Souza 
434be9ddf43SLuiz Otavio O Souza 		/* Start the transfer. */
435be9ddf43SLuiz Otavio O Souza 		BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN |
436be9ddf43SLuiz Otavio O Souza 		    BCM_BSC_CTRL_ST | read | intr);
437be9ddf43SLuiz Otavio O Souza 
438be9ddf43SLuiz Otavio O Souza 		/* Wait for the transaction to complete. */
439be9ddf43SLuiz Otavio O Souza 		err = mtx_sleep(dev, &sc->sc_mtx, 0, "bcm_bsc", hz);
440be9ddf43SLuiz Otavio O Souza 
441be9ddf43SLuiz Otavio O Souza 		/* Check if we have a timeout or an I2C error. */
442be9ddf43SLuiz Otavio O Souza 		if ((sc->sc_flags & BCM_I2C_ERROR) || err == EWOULDBLOCK) {
443be9ddf43SLuiz Otavio O Souza 			device_printf(sc->sc_dev, "I2C error\n");
444be9ddf43SLuiz Otavio O Souza 			err = EIO;
445be9ddf43SLuiz Otavio O Souza 			break;
446be9ddf43SLuiz Otavio O Souza 		}
447be9ddf43SLuiz Otavio O Souza 	}
448be9ddf43SLuiz Otavio O Souza 
449be9ddf43SLuiz Otavio O Souza 	/* Clean the controller flags. */
450be9ddf43SLuiz Otavio O Souza 	sc->sc_flags = 0;
451be9ddf43SLuiz Otavio O Souza 
452be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
453be9ddf43SLuiz Otavio O Souza 
454be9ddf43SLuiz Otavio O Souza 	return (err);
455be9ddf43SLuiz Otavio O Souza }
456be9ddf43SLuiz Otavio O Souza 
457be9ddf43SLuiz Otavio O Souza static phandle_t
458be9ddf43SLuiz Otavio O Souza bcm_bsc_get_node(device_t bus, device_t dev)
459be9ddf43SLuiz Otavio O Souza {
460be9ddf43SLuiz Otavio O Souza 
461be9ddf43SLuiz Otavio O Souza 	/* We only have one child, the I2C bus, which needs our own node. */
462be9ddf43SLuiz Otavio O Souza 	return (ofw_bus_get_node(bus));
463be9ddf43SLuiz Otavio O Souza }
464be9ddf43SLuiz Otavio O Souza 
465be9ddf43SLuiz Otavio O Souza static device_method_t bcm_bsc_methods[] = {
466be9ddf43SLuiz Otavio O Souza 	/* Device interface */
467be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_probe,		bcm_bsc_probe),
468be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_attach,	bcm_bsc_attach),
469be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_detach,	bcm_bsc_detach),
470be9ddf43SLuiz Otavio O Souza 
471be9ddf43SLuiz Otavio O Souza 	/* iicbus interface */
472be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(iicbus_callback,	iicbus_null_callback),
473be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(iicbus_transfer,	bcm_bsc_transfer),
474be9ddf43SLuiz Otavio O Souza 
475be9ddf43SLuiz Otavio O Souza 	/* ofw_bus interface */
476be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(ofw_bus_get_node,	bcm_bsc_get_node),
477be9ddf43SLuiz Otavio O Souza 
478be9ddf43SLuiz Otavio O Souza 	DEVMETHOD_END
479be9ddf43SLuiz Otavio O Souza };
480be9ddf43SLuiz Otavio O Souza 
481be9ddf43SLuiz Otavio O Souza static devclass_t bcm_bsc_devclass;
482be9ddf43SLuiz Otavio O Souza 
483be9ddf43SLuiz Otavio O Souza static driver_t bcm_bsc_driver = {
484be9ddf43SLuiz Otavio O Souza 	"iichb",
485be9ddf43SLuiz Otavio O Souza 	bcm_bsc_methods,
486be9ddf43SLuiz Otavio O Souza 	sizeof(struct bcm_bsc_softc),
487be9ddf43SLuiz Otavio O Souza };
488be9ddf43SLuiz Otavio O Souza 
489be9ddf43SLuiz Otavio O Souza DRIVER_MODULE(iicbus, bcm2835_bsc, iicbus_driver, iicbus_devclass, 0, 0);
490be9ddf43SLuiz Otavio O Souza DRIVER_MODULE(bcm2835_bsc, simplebus, bcm_bsc_driver, bcm_bsc_devclass, 0, 0);
491