xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_bsc.c (revision 1e4042d44e3f334027a5223ef0c445673254c879)
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 
559d6eb8bbSOleksandr Tymoshenko static struct ofw_compat_data compat_data[] = {
569d6eb8bbSOleksandr Tymoshenko 	{"broadcom,bcm2835-bsc",	1},
579d6eb8bbSOleksandr Tymoshenko 	{"brcm,bcm2708-i2c",		1},
58e9804ab2SOleksandr Tymoshenko 	{"brcm,bcm2835-i2c",		1},
599d6eb8bbSOleksandr Tymoshenko 	{NULL,				0}
609d6eb8bbSOleksandr Tymoshenko };
619d6eb8bbSOleksandr Tymoshenko 
62be9ddf43SLuiz Otavio O Souza static void bcm_bsc_intr(void *);
639e93dfcfSLuiz Otavio O Souza static int bcm_bsc_detach(device_t);
64be9ddf43SLuiz Otavio O Souza 
65be9ddf43SLuiz Otavio O Souza static void
66be9ddf43SLuiz Otavio O Souza bcm_bsc_modifyreg(struct bcm_bsc_softc *sc, uint32_t off, uint32_t mask,
67be9ddf43SLuiz Otavio O Souza 	uint32_t value)
68be9ddf43SLuiz Otavio O Souza {
69be9ddf43SLuiz Otavio O Souza 	uint32_t reg;
70be9ddf43SLuiz Otavio O Souza 
71be9ddf43SLuiz Otavio O Souza 	mtx_assert(&sc->sc_mtx, MA_OWNED);
72be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, off);
73be9ddf43SLuiz Otavio O Souza 	reg &= ~mask;
74be9ddf43SLuiz Otavio O Souza 	reg |= value;
75be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, off, reg);
76be9ddf43SLuiz Otavio O Souza }
77be9ddf43SLuiz Otavio O Souza 
78be9ddf43SLuiz Otavio O Souza static int
79be9ddf43SLuiz Otavio O Souza bcm_bsc_clock_proc(SYSCTL_HANDLER_ARGS)
80be9ddf43SLuiz Otavio O Souza {
81be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
82be9ddf43SLuiz Otavio O Souza 	uint32_t clk;
83be9ddf43SLuiz Otavio O Souza 
84be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
85be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
86be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
87be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
88be9ddf43SLuiz Otavio O Souza 	clk &= 0xffff;
89be9ddf43SLuiz Otavio O Souza 	if (clk == 0)
90be9ddf43SLuiz Otavio O Souza 		clk = 32768;
91be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
92be9ddf43SLuiz Otavio O Souza 
939e93dfcfSLuiz Otavio O Souza 	return (sysctl_handle_int(oidp, &clk, 0, req));
94be9ddf43SLuiz Otavio O Souza }
95be9ddf43SLuiz Otavio O Souza 
96be9ddf43SLuiz Otavio O Souza static int
97be9ddf43SLuiz Otavio O Souza bcm_bsc_clkt_proc(SYSCTL_HANDLER_ARGS)
98be9ddf43SLuiz Otavio O Souza {
99be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
100be9ddf43SLuiz Otavio O Souza 	uint32_t clkt;
101be9ddf43SLuiz Otavio O Souza 	int error;
102be9ddf43SLuiz Otavio O Souza 
103be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
104be9ddf43SLuiz Otavio O Souza 
105be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
106be9ddf43SLuiz Otavio O Souza 	clkt = BCM_BSC_READ(sc, BCM_BSC_CLKT);
107be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
108be9ddf43SLuiz Otavio O Souza 	clkt &= 0xffff;
109be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &clkt, sizeof(clkt), req);
110be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
111be9ddf43SLuiz Otavio O Souza 		return (error);
112be9ddf43SLuiz Otavio O Souza 
113be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
114be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CLKT, clkt & 0xffff);
115be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
116be9ddf43SLuiz Otavio O Souza 
117be9ddf43SLuiz Otavio O Souza 	return (0);
118be9ddf43SLuiz Otavio O Souza }
119be9ddf43SLuiz Otavio O Souza 
120be9ddf43SLuiz Otavio O Souza static int
121be9ddf43SLuiz Otavio O Souza bcm_bsc_fall_proc(SYSCTL_HANDLER_ARGS)
122be9ddf43SLuiz Otavio O Souza {
123be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
124be9ddf43SLuiz Otavio O Souza 	uint32_t clk, reg;
125be9ddf43SLuiz Otavio O Souza 	int error;
126be9ddf43SLuiz Otavio O Souza 
127be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
128be9ddf43SLuiz Otavio O Souza 
129be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
130be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, BCM_BSC_DELAY);
131be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
132be9ddf43SLuiz Otavio O Souza 	reg >>= 16;
133be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
134be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
135be9ddf43SLuiz Otavio O Souza 		return (error);
136be9ddf43SLuiz Otavio O Souza 
137be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
138be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
139be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
140be9ddf43SLuiz Otavio O Souza 	if (reg > clk / 2)
141be9ddf43SLuiz Otavio O Souza 		reg = clk / 2 - 1;
142be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff0000, reg << 16);
143be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
144be9ddf43SLuiz Otavio O Souza 
145be9ddf43SLuiz Otavio O Souza 	return (0);
146be9ddf43SLuiz Otavio O Souza }
147be9ddf43SLuiz Otavio O Souza 
148be9ddf43SLuiz Otavio O Souza static int
149be9ddf43SLuiz Otavio O Souza bcm_bsc_rise_proc(SYSCTL_HANDLER_ARGS)
150be9ddf43SLuiz Otavio O Souza {
151be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
152be9ddf43SLuiz Otavio O Souza 	uint32_t clk, reg;
153be9ddf43SLuiz Otavio O Souza 	int error;
154be9ddf43SLuiz Otavio O Souza 
155be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg1;
156be9ddf43SLuiz Otavio O Souza 
157be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
158be9ddf43SLuiz Otavio O Souza 	reg = BCM_BSC_READ(sc, BCM_BSC_DELAY);
159be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
160be9ddf43SLuiz Otavio O Souza 	reg &= 0xffff;
161be9ddf43SLuiz Otavio O Souza 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
162be9ddf43SLuiz Otavio O Souza 	if (error != 0 || req->newptr == NULL)
163be9ddf43SLuiz Otavio O Souza 		return (error);
164be9ddf43SLuiz Otavio O Souza 
165be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
166be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
167be9ddf43SLuiz Otavio O Souza 	clk = BCM_BSC_CORE_CLK / clk;
168be9ddf43SLuiz Otavio O Souza 	if (reg > clk / 2)
169be9ddf43SLuiz Otavio O Souza 		reg = clk / 2 - 1;
170be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff, reg);
171be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
172be9ddf43SLuiz Otavio O Souza 
173be9ddf43SLuiz Otavio O Souza 	return (0);
174be9ddf43SLuiz Otavio O Souza }
175be9ddf43SLuiz Otavio O Souza 
176be9ddf43SLuiz Otavio O Souza static void
177be9ddf43SLuiz Otavio O Souza bcm_bsc_sysctl_init(struct bcm_bsc_softc *sc)
178be9ddf43SLuiz Otavio O Souza {
179be9ddf43SLuiz Otavio O Souza 	struct sysctl_ctx_list *ctx;
180be9ddf43SLuiz Otavio O Souza 	struct sysctl_oid *tree_node;
181be9ddf43SLuiz Otavio O Souza 	struct sysctl_oid_list *tree;
182be9ddf43SLuiz Otavio O Souza 
183be9ddf43SLuiz Otavio O Souza 	/*
184be9ddf43SLuiz Otavio O Souza 	 * Add system sysctl tree/handlers.
185be9ddf43SLuiz Otavio O Souza 	 */
186be9ddf43SLuiz Otavio O Souza 	ctx = device_get_sysctl_ctx(sc->sc_dev);
187be9ddf43SLuiz Otavio O Souza 	tree_node = device_get_sysctl_tree(sc->sc_dev);
188be9ddf43SLuiz Otavio O Souza 	tree = SYSCTL_CHILDREN(tree_node);
1899e93dfcfSLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "frequency",
190be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
191be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_clock_proc, "IU", "I2C BUS clock frequency");
192be9ddf43SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clock_stretch",
193be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
194be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_clkt_proc, "IU", "I2C BUS clock stretch timeout");
195be9ddf43SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "fall_edge_delay",
196be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
197be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_fall_proc, "IU", "I2C BUS falling edge delay");
198be9ddf43SLuiz Otavio O Souza 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "rise_edge_delay",
199be9ddf43SLuiz Otavio O Souza 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
200be9ddf43SLuiz Otavio O Souza 	    bcm_bsc_rise_proc, "IU", "I2C BUS rising edge delay");
201be9ddf43SLuiz Otavio O Souza }
202be9ddf43SLuiz Otavio O Souza 
203be9ddf43SLuiz Otavio O Souza static void
204be9ddf43SLuiz Otavio O Souza bcm_bsc_reset(struct bcm_bsc_softc *sc)
205be9ddf43SLuiz Otavio O Souza {
206be9ddf43SLuiz Otavio O Souza 
207e50c6241SLuiz Otavio O Souza 	/* Enable the BSC Controller, disable interrupts. */
208e50c6241SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN);
209be9ddf43SLuiz Otavio O Souza 	/* Clear pending interrupts. */
210be9ddf43SLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_STATUS, BCM_BSC_STATUS_CLKT |
211be9ddf43SLuiz Otavio O Souza 	    BCM_BSC_STATUS_ERR | BCM_BSC_STATUS_DONE);
212be9ddf43SLuiz Otavio O Souza 	/* Clear the FIFO. */
213be9ddf43SLuiz Otavio O Souza 	bcm_bsc_modifyreg(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_CLEAR0,
214be9ddf43SLuiz Otavio O Souza 	    BCM_BSC_CTRL_CLEAR0);
215be9ddf43SLuiz Otavio O Souza }
216be9ddf43SLuiz Otavio O Souza 
217be9ddf43SLuiz Otavio O Souza static int
218be9ddf43SLuiz Otavio O Souza bcm_bsc_probe(device_t dev)
219be9ddf43SLuiz Otavio O Souza {
220be9ddf43SLuiz Otavio O Souza 
221add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
222add35ed5SIan Lepore 		return (ENXIO);
223add35ed5SIan Lepore 
2249d6eb8bbSOleksandr Tymoshenko 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
225be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
226be9ddf43SLuiz Otavio O Souza 
227be9ddf43SLuiz Otavio O Souza 	device_set_desc(dev, "BCM2708/2835 BSC controller");
228be9ddf43SLuiz Otavio O Souza 
229be9ddf43SLuiz Otavio O Souza 	return (BUS_PROBE_DEFAULT);
230be9ddf43SLuiz Otavio O Souza }
231be9ddf43SLuiz Otavio O Souza 
232be9ddf43SLuiz Otavio O Souza static int
233be9ddf43SLuiz Otavio O Souza bcm_bsc_attach(device_t dev)
234be9ddf43SLuiz Otavio O Souza {
235be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
23621cb1342SLuiz Otavio O Souza 	unsigned long start;
237be9ddf43SLuiz Otavio O Souza 	device_t gpio;
23821cb1342SLuiz Otavio O Souza 	int i, rid;
239be9ddf43SLuiz Otavio O Souza 
240be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
241be9ddf43SLuiz Otavio O Souza 	sc->sc_dev = dev;
242be9ddf43SLuiz Otavio O Souza 
243be9ddf43SLuiz Otavio O Souza 	rid = 0;
244be9ddf43SLuiz Otavio O Souza 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
245be9ddf43SLuiz Otavio O Souza 	    RF_ACTIVE);
246be9ddf43SLuiz Otavio O Souza 	if (!sc->sc_mem_res) {
247be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate memory window\n");
248be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
249be9ddf43SLuiz Otavio O Souza 	}
250be9ddf43SLuiz Otavio O Souza 
251be9ddf43SLuiz Otavio O Souza 	sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
252be9ddf43SLuiz Otavio O Souza 	sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
253be9ddf43SLuiz Otavio O Souza 
25421cb1342SLuiz Otavio O Souza 	/* Check the unit we are attaching by its base address. */
25521cb1342SLuiz Otavio O Souza 	start = rman_get_start(sc->sc_mem_res);
25621cb1342SLuiz Otavio O Souza 	for (i = 0; i < nitems(bcm_bsc_pins); i++) {
257b4ae2f3cSLuiz Otavio O Souza 		if (bcm_bsc_pins[i].start == (start & BCM_BSC_BASE_MASK))
25821cb1342SLuiz Otavio O Souza 			break;
25921cb1342SLuiz Otavio O Souza 	}
26021cb1342SLuiz Otavio O Souza 	if (i == nitems(bcm_bsc_pins)) {
26121cb1342SLuiz Otavio O Souza 		device_printf(dev, "only bsc0 and bsc1 are supported\n");
26202a42e68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
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");
27302a42e68SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
27421cb1342SLuiz Otavio O Souza 		return (ENXIO);
27521cb1342SLuiz Otavio O Souza 	}
27621cb1342SLuiz Otavio O Souza 	bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].sda, BCM_GPIO_ALT0);
27721cb1342SLuiz Otavio O Souza 	bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].scl, BCM_GPIO_ALT0);
27821cb1342SLuiz Otavio O Souza 
279be9ddf43SLuiz Otavio O Souza 	rid = 0;
280be9ddf43SLuiz Otavio O Souza 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
281be9ddf43SLuiz Otavio O Souza 	    RF_ACTIVE | RF_SHAREABLE);
282be9ddf43SLuiz Otavio O Souza 	if (!sc->sc_irq_res) {
283be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
284be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot allocate interrupt\n");
285be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
286be9ddf43SLuiz Otavio O Souza 	}
287be9ddf43SLuiz Otavio O Souza 
288be9ddf43SLuiz Otavio O Souza 	/* Hook up our interrupt handler. */
289be9ddf43SLuiz Otavio O Souza 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
290be9ddf43SLuiz Otavio O Souza 	    NULL, bcm_bsc_intr, sc, &sc->sc_intrhand)) {
291be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
292be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
293be9ddf43SLuiz Otavio O Souza 		device_printf(dev, "cannot setup the interrupt handler\n");
294be9ddf43SLuiz Otavio O Souza 		return (ENXIO);
295be9ddf43SLuiz Otavio O Souza 	}
296be9ddf43SLuiz Otavio O Souza 
297be9ddf43SLuiz Otavio O Souza 	mtx_init(&sc->sc_mtx, "bcm_bsc", NULL, MTX_DEF);
298be9ddf43SLuiz Otavio O Souza 
299be9ddf43SLuiz Otavio O Souza 	bcm_bsc_sysctl_init(sc);
300be9ddf43SLuiz Otavio O Souza 
301be9ddf43SLuiz Otavio O Souza 	/* Enable the BSC controller.  Flush the FIFO. */
302be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
303be9ddf43SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
304be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
305be9ddf43SLuiz Otavio O Souza 
3069e93dfcfSLuiz Otavio O Souza 	sc->sc_iicbus = device_add_child(dev, "iicbus", -1);
3079e93dfcfSLuiz Otavio O Souza 	if (sc->sc_iicbus == NULL) {
3089e93dfcfSLuiz Otavio O Souza 		bcm_bsc_detach(dev);
3099e93dfcfSLuiz Otavio O Souza 		return (ENXIO);
3109e93dfcfSLuiz Otavio O Souza 	}
311be9ddf43SLuiz Otavio O Souza 
312*1e4042d4SIan Lepore 	/* Probe and attach the iicbus when interrupts are available. */
313*1e4042d4SIan Lepore 	config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
314*1e4042d4SIan Lepore 
315*1e4042d4SIan Lepore 	return (0);
316be9ddf43SLuiz Otavio O Souza }
317be9ddf43SLuiz Otavio O Souza 
318be9ddf43SLuiz Otavio O Souza static int
319be9ddf43SLuiz Otavio O Souza bcm_bsc_detach(device_t dev)
320be9ddf43SLuiz Otavio O Souza {
321be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
322be9ddf43SLuiz Otavio O Souza 
323be9ddf43SLuiz Otavio O Souza 	bus_generic_detach(dev);
324be9ddf43SLuiz Otavio O Souza 
325be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
326be9ddf43SLuiz Otavio O Souza 	mtx_destroy(&sc->sc_mtx);
327be9ddf43SLuiz Otavio O Souza 	if (sc->sc_intrhand)
328be9ddf43SLuiz Otavio O Souza 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
329be9ddf43SLuiz Otavio O Souza 	if (sc->sc_irq_res)
330be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
331be9ddf43SLuiz Otavio O Souza 	if (sc->sc_mem_res)
332be9ddf43SLuiz Otavio O Souza 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
333be9ddf43SLuiz Otavio O Souza 
334be9ddf43SLuiz Otavio O Souza 	return (0);
335be9ddf43SLuiz Otavio O Souza }
336be9ddf43SLuiz Otavio O Souza 
337be9ddf43SLuiz Otavio O Souza static void
338be9ddf43SLuiz Otavio O Souza bcm_bsc_intr(void *arg)
339be9ddf43SLuiz Otavio O Souza {
340be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
341be9ddf43SLuiz Otavio O Souza 	uint32_t status;
342be9ddf43SLuiz Otavio O Souza 
343be9ddf43SLuiz Otavio O Souza 	sc = (struct bcm_bsc_softc *)arg;
344be9ddf43SLuiz Otavio O Souza 
345be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
346be9ddf43SLuiz Otavio O Souza 
347be9ddf43SLuiz Otavio O Souza 	/* The I2C interrupt is shared among all the BSC controllers. */
348be9ddf43SLuiz Otavio O Souza 	if ((sc->sc_flags & BCM_I2C_BUSY) == 0) {
349be9ddf43SLuiz Otavio O Souza 		BCM_BSC_UNLOCK(sc);
350be9ddf43SLuiz Otavio O Souza 		return;
351be9ddf43SLuiz Otavio O Souza 	}
352be9ddf43SLuiz Otavio O Souza 
353be9ddf43SLuiz Otavio O Souza 	status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
354be9ddf43SLuiz Otavio O Souza 
355be9ddf43SLuiz Otavio O Souza 	/* Check for errors. */
356be9ddf43SLuiz Otavio O Souza 	if (status & (BCM_BSC_STATUS_CLKT | BCM_BSC_STATUS_ERR)) {
357be9ddf43SLuiz Otavio O Souza 		/* Disable interrupts. */
358be9ddf43SLuiz Otavio O Souza 		bcm_bsc_reset(sc);
359e50c6241SLuiz Otavio O Souza 		sc->sc_flags |= BCM_I2C_ERROR;
360be9ddf43SLuiz Otavio O Souza 		wakeup(sc->sc_dev);
361be9ddf43SLuiz Otavio O Souza 		BCM_BSC_UNLOCK(sc);
362be9ddf43SLuiz Otavio O Souza 		return;
363be9ddf43SLuiz Otavio O Souza 	}
364be9ddf43SLuiz Otavio O Souza 
365be9ddf43SLuiz Otavio O Souza 	if (sc->sc_flags & BCM_I2C_READ) {
366be9ddf43SLuiz Otavio O Souza 		while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_RXD)) {
367be9ddf43SLuiz Otavio O Souza 			*sc->sc_data++ = BCM_BSC_READ(sc, BCM_BSC_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 	} else {
372be9ddf43SLuiz Otavio O Souza 		while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_TXD)) {
373be9ddf43SLuiz Otavio O Souza 			BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data++);
374be9ddf43SLuiz Otavio O Souza 			sc->sc_resid--;
375be9ddf43SLuiz Otavio O Souza 			status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
376be9ddf43SLuiz Otavio O Souza 		}
377be9ddf43SLuiz Otavio O Souza 	}
378be9ddf43SLuiz Otavio O Souza 
379be9ddf43SLuiz Otavio O Souza 	if (status & BCM_BSC_STATUS_DONE) {
380be9ddf43SLuiz Otavio O Souza 		/* Disable interrupts. */
381be9ddf43SLuiz Otavio O Souza 		bcm_bsc_reset(sc);
382be9ddf43SLuiz Otavio O Souza 		wakeup(sc->sc_dev);
383be9ddf43SLuiz Otavio O Souza 	}
384be9ddf43SLuiz Otavio O Souza 
385be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
386be9ddf43SLuiz Otavio O Souza }
387be9ddf43SLuiz Otavio O Souza 
388be9ddf43SLuiz Otavio O Souza static int
389be9ddf43SLuiz Otavio O Souza bcm_bsc_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
390be9ddf43SLuiz Otavio O Souza {
391be9ddf43SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
392be9ddf43SLuiz Otavio O Souza 	uint32_t intr, read, status;
393be9ddf43SLuiz Otavio O Souza 	int i, err;
394be9ddf43SLuiz Otavio O Souza 
395be9ddf43SLuiz Otavio O Souza 	sc = device_get_softc(dev);
396be9ddf43SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
397be9ddf43SLuiz Otavio O Souza 
398be9ddf43SLuiz Otavio O Souza 	/* If the controller is busy wait until it is available. */
399be9ddf43SLuiz Otavio O Souza 	while (sc->sc_flags & BCM_I2C_BUSY)
400a141b672SLuiz Otavio O Souza 		mtx_sleep(dev, &sc->sc_mtx, 0, "bscbusw", 0);
401be9ddf43SLuiz Otavio O Souza 
402be9ddf43SLuiz Otavio O Souza 	/* Now we have control over the BSC controller. */
403be9ddf43SLuiz Otavio O Souza 	sc->sc_flags = BCM_I2C_BUSY;
404be9ddf43SLuiz Otavio O Souza 
405be9ddf43SLuiz Otavio O Souza 	/* Clear the FIFO and the pending interrupts. */
406be9ddf43SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
407be9ddf43SLuiz Otavio O Souza 
408be9ddf43SLuiz Otavio O Souza 	err = 0;
409be9ddf43SLuiz Otavio O Souza 	for (i = 0; i < nmsgs; i++) {
410be9ddf43SLuiz Otavio O Souza 
411be9ddf43SLuiz Otavio O Souza 		/* Write the slave address. */
412d6cf3c63SLuiz Otavio O Souza 		BCM_BSC_WRITE(sc, BCM_BSC_SLAVE, msgs[i].slave >> 1);
413be9ddf43SLuiz Otavio O Souza 
414be9ddf43SLuiz Otavio O Souza 		/* Write the data length. */
415be9ddf43SLuiz Otavio O Souza 		BCM_BSC_WRITE(sc, BCM_BSC_DLEN, msgs[i].len);
416be9ddf43SLuiz Otavio O Souza 
417be9ddf43SLuiz Otavio O Souza 		sc->sc_data = msgs[i].buf;
418be9ddf43SLuiz Otavio O Souza 		sc->sc_resid = msgs[i].len;
419be9ddf43SLuiz Otavio O Souza 		if ((msgs[i].flags & IIC_M_RD) == 0) {
420be9ddf43SLuiz Otavio O Souza 			/* Fill up the TX FIFO. */
421be9ddf43SLuiz Otavio O Souza 			status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
422be9ddf43SLuiz Otavio O Souza 			while (sc->sc_resid > 0 &&
423be9ddf43SLuiz Otavio O Souza 			    (status & BCM_BSC_STATUS_TXD)) {
424be9ddf43SLuiz Otavio O Souza 				BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data);
425be9ddf43SLuiz Otavio O Souza 				sc->sc_data++;
426be9ddf43SLuiz Otavio O Souza 				sc->sc_resid--;
427be9ddf43SLuiz Otavio O Souza 				status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
428be9ddf43SLuiz Otavio O Souza 			}
429be9ddf43SLuiz Otavio O Souza 			read = 0;
430be9ddf43SLuiz Otavio O Souza 			intr = BCM_BSC_CTRL_INTT;
431be9ddf43SLuiz Otavio O Souza 			sc->sc_flags &= ~BCM_I2C_READ;
432be9ddf43SLuiz Otavio O Souza 		} else {
433be9ddf43SLuiz Otavio O Souza 			sc->sc_flags |= BCM_I2C_READ;
434be9ddf43SLuiz Otavio O Souza 			read = BCM_BSC_CTRL_READ;
435be9ddf43SLuiz Otavio O Souza 			intr = BCM_BSC_CTRL_INTR;
436be9ddf43SLuiz Otavio O Souza 		}
437be9ddf43SLuiz Otavio O Souza 		intr |= BCM_BSC_CTRL_INTD;
438be9ddf43SLuiz Otavio O Souza 
439be9ddf43SLuiz Otavio O Souza 		/* Start the transfer. */
440be9ddf43SLuiz Otavio O Souza 		BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN |
441be9ddf43SLuiz Otavio O Souza 		    BCM_BSC_CTRL_ST | read | intr);
442be9ddf43SLuiz Otavio O Souza 
443be9ddf43SLuiz Otavio O Souza 		/* Wait for the transaction to complete. */
444a141b672SLuiz Otavio O Souza 		err = mtx_sleep(dev, &sc->sc_mtx, 0, "bsciow", hz);
445be9ddf43SLuiz Otavio O Souza 
44671b37cb9SLuiz Otavio O Souza 		/* Check for errors. */
4470a39cc71SLuiz Otavio O Souza 		if (err == 0 && (sc->sc_flags & BCM_I2C_ERROR))
448be9ddf43SLuiz Otavio O Souza 			err = EIO;
44971b37cb9SLuiz Otavio O Souza 		if (err != 0)
450be9ddf43SLuiz Otavio O Souza 			break;
451be9ddf43SLuiz Otavio O Souza 	}
452be9ddf43SLuiz Otavio O Souza 
453be9ddf43SLuiz Otavio O Souza 	/* Clean the controller flags. */
454be9ddf43SLuiz Otavio O Souza 	sc->sc_flags = 0;
455be9ddf43SLuiz Otavio O Souza 
456a141b672SLuiz Otavio O Souza 	/* Wake up the threads waiting for bus. */
457a141b672SLuiz Otavio O Souza 	wakeup(dev);
458a141b672SLuiz Otavio O Souza 
459be9ddf43SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
460be9ddf43SLuiz Otavio O Souza 
461be9ddf43SLuiz Otavio O Souza 	return (err);
462be9ddf43SLuiz Otavio O Souza }
463be9ddf43SLuiz Otavio O Souza 
464e50c6241SLuiz Otavio O Souza static int
465e50c6241SLuiz Otavio O Souza bcm_bsc_iicbus_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
466e50c6241SLuiz Otavio O Souza {
467e50c6241SLuiz Otavio O Souza 	struct bcm_bsc_softc *sc;
4689e93dfcfSLuiz Otavio O Souza 	uint32_t busfreq;
469e50c6241SLuiz Otavio O Souza 
470e50c6241SLuiz Otavio O Souza 	sc = device_get_softc(dev);
471e50c6241SLuiz Otavio O Souza 	BCM_BSC_LOCK(sc);
472e50c6241SLuiz Otavio O Souza 	bcm_bsc_reset(sc);
4739e93dfcfSLuiz Otavio O Souza 	if (sc->sc_iicbus == NULL)
4749e93dfcfSLuiz Otavio O Souza 		busfreq = 100000;
4759e93dfcfSLuiz Otavio O Souza 	else
4769e93dfcfSLuiz Otavio O Souza 		busfreq = IICBUS_GET_FREQUENCY(sc->sc_iicbus, speed);
4779e93dfcfSLuiz Otavio O Souza 	BCM_BSC_WRITE(sc, BCM_BSC_CLOCK, BCM_BSC_CORE_CLK / busfreq);
478e50c6241SLuiz Otavio O Souza 	BCM_BSC_UNLOCK(sc);
479e50c6241SLuiz Otavio O Souza 
480e50c6241SLuiz Otavio O Souza 	return (IIC_ENOADDR);
481e50c6241SLuiz Otavio O Souza }
482e50c6241SLuiz Otavio O Souza 
483be9ddf43SLuiz Otavio O Souza static phandle_t
484be9ddf43SLuiz Otavio O Souza bcm_bsc_get_node(device_t bus, device_t dev)
485be9ddf43SLuiz Otavio O Souza {
486be9ddf43SLuiz Otavio O Souza 
487be9ddf43SLuiz Otavio O Souza 	/* We only have one child, the I2C bus, which needs our own node. */
488be9ddf43SLuiz Otavio O Souza 	return (ofw_bus_get_node(bus));
489be9ddf43SLuiz Otavio O Souza }
490be9ddf43SLuiz Otavio O Souza 
491be9ddf43SLuiz Otavio O Souza static device_method_t bcm_bsc_methods[] = {
492be9ddf43SLuiz Otavio O Souza 	/* Device interface */
493be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_probe,		bcm_bsc_probe),
494be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_attach,	bcm_bsc_attach),
495be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(device_detach,	bcm_bsc_detach),
496be9ddf43SLuiz Otavio O Souza 
497be9ddf43SLuiz Otavio O Souza 	/* iicbus interface */
498e50c6241SLuiz Otavio O Souza 	DEVMETHOD(iicbus_reset,		bcm_bsc_iicbus_reset),
499be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(iicbus_callback,	iicbus_null_callback),
500be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(iicbus_transfer,	bcm_bsc_transfer),
501be9ddf43SLuiz Otavio O Souza 
502be9ddf43SLuiz Otavio O Souza 	/* ofw_bus interface */
503be9ddf43SLuiz Otavio O Souza 	DEVMETHOD(ofw_bus_get_node,	bcm_bsc_get_node),
504be9ddf43SLuiz Otavio O Souza 
505be9ddf43SLuiz Otavio O Souza 	DEVMETHOD_END
506be9ddf43SLuiz Otavio O Souza };
507be9ddf43SLuiz Otavio O Souza 
508be9ddf43SLuiz Otavio O Souza static devclass_t bcm_bsc_devclass;
509be9ddf43SLuiz Otavio O Souza 
510be9ddf43SLuiz Otavio O Souza static driver_t bcm_bsc_driver = {
511be9ddf43SLuiz Otavio O Souza 	"iichb",
512be9ddf43SLuiz Otavio O Souza 	bcm_bsc_methods,
513be9ddf43SLuiz Otavio O Souza 	sizeof(struct bcm_bsc_softc),
514be9ddf43SLuiz Otavio O Souza };
515be9ddf43SLuiz Otavio O Souza 
516be9ddf43SLuiz Otavio O Souza DRIVER_MODULE(iicbus, bcm2835_bsc, iicbus_driver, iicbus_devclass, 0, 0);
517be9ddf43SLuiz Otavio O Souza DRIVER_MODULE(bcm2835_bsc, simplebus, bcm_bsc_driver, bcm_bsc_devclass, 0, 0);
518