2 * Copyright (c) 2001 Tsubai Masanari.
3 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
4 * Copyright (c) 2013 Luiz Otavio O Souza <loos@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
39 #include <machine/resource.h>
40 #include <machine/bus.h>
42 #include <sys/sysctl.h>
44 #include <dev/iicbus/iicbus.h>
45 #include <dev/iicbus/iiconf.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
49 #include <arm/broadcom/bcm2835/bcm2835_gpio.h>
50 #include <arm/broadcom/bcm2835/bcm2835_bscreg.h>
51 #include <arm/broadcom/bcm2835/bcm2835_bscvar.h>
53 #include "iicbus_if.h"
55 static void bcm_bsc_intr(void *);
58 bcm_bsc_modifyreg(struct bcm_bsc_softc *sc, uint32_t off, uint32_t mask,
63 mtx_assert(&sc->sc_mtx, MA_OWNED);
64 reg = BCM_BSC_READ(sc, off);
67 BCM_BSC_WRITE(sc, off, reg);
71 bcm_bsc_clock_proc(SYSCTL_HANDLER_ARGS)
73 struct bcm_bsc_softc *sc;
77 sc = (struct bcm_bsc_softc *)arg1;
80 clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
85 clk = BCM_BSC_CORE_CLK / clk;
86 error = sysctl_handle_int(oidp, &clk, sizeof(clk), req);
87 if (error != 0 || req->newptr == NULL)
90 clk = BCM_BSC_CORE_CLK / clk;
96 BCM_BSC_WRITE(sc, BCM_BSC_CLOCK, clk);
103 bcm_bsc_clkt_proc(SYSCTL_HANDLER_ARGS)
105 struct bcm_bsc_softc *sc;
109 sc = (struct bcm_bsc_softc *)arg1;
112 clkt = BCM_BSC_READ(sc, BCM_BSC_CLKT);
115 error = sysctl_handle_int(oidp, &clkt, sizeof(clkt), req);
116 if (error != 0 || req->newptr == NULL)
120 BCM_BSC_WRITE(sc, BCM_BSC_CLKT, clkt & 0xffff);
127 bcm_bsc_fall_proc(SYSCTL_HANDLER_ARGS)
129 struct bcm_bsc_softc *sc;
133 sc = (struct bcm_bsc_softc *)arg1;
136 reg = BCM_BSC_READ(sc, BCM_BSC_DELAY);
139 error = sysctl_handle_int(oidp, ®, sizeof(reg), req);
140 if (error != 0 || req->newptr == NULL)
144 clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
145 clk = BCM_BSC_CORE_CLK / clk;
148 bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff0000, reg << 16);
155 bcm_bsc_rise_proc(SYSCTL_HANDLER_ARGS)
157 struct bcm_bsc_softc *sc;
161 sc = (struct bcm_bsc_softc *)arg1;
164 reg = BCM_BSC_READ(sc, BCM_BSC_DELAY);
167 error = sysctl_handle_int(oidp, ®, sizeof(reg), req);
168 if (error != 0 || req->newptr == NULL)
172 clk = BCM_BSC_READ(sc, BCM_BSC_CLOCK);
173 clk = BCM_BSC_CORE_CLK / clk;
176 bcm_bsc_modifyreg(sc, BCM_BSC_DELAY, 0xffff, reg);
183 bcm_bsc_sysctl_init(struct bcm_bsc_softc *sc)
185 struct sysctl_ctx_list *ctx;
186 struct sysctl_oid *tree_node;
187 struct sysctl_oid_list *tree;
190 * Add system sysctl tree/handlers.
192 ctx = device_get_sysctl_ctx(sc->sc_dev);
193 tree_node = device_get_sysctl_tree(sc->sc_dev);
194 tree = SYSCTL_CHILDREN(tree_node);
195 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clock",
196 CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
197 bcm_bsc_clock_proc, "IU", "I2C BUS clock frequency");
198 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clock_stretch",
199 CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
200 bcm_bsc_clkt_proc, "IU", "I2C BUS clock stretch timeout");
201 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "fall_edge_delay",
202 CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
203 bcm_bsc_fall_proc, "IU", "I2C BUS falling edge delay");
204 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "rise_edge_delay",
205 CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
206 bcm_bsc_rise_proc, "IU", "I2C BUS rising edge delay");
210 bcm_bsc_reset(struct bcm_bsc_softc *sc)
213 /* Clear pending interrupts. */
214 BCM_BSC_WRITE(sc, BCM_BSC_STATUS, BCM_BSC_STATUS_CLKT |
215 BCM_BSC_STATUS_ERR | BCM_BSC_STATUS_DONE);
216 /* Clear the FIFO. */
217 bcm_bsc_modifyreg(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_CLEAR0,
218 BCM_BSC_CTRL_CLEAR0);
222 bcm_bsc_probe(device_t dev)
225 if (!ofw_bus_status_okay(dev))
228 if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-bsc"))
231 device_set_desc(dev, "BCM2708/2835 BSC controller");
233 return (BUS_PROBE_DEFAULT);
237 bcm_bsc_attach(device_t dev)
239 struct bcm_bsc_softc *sc;
244 sc = device_get_softc(dev);
248 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
250 if (!sc->sc_mem_res) {
251 device_printf(dev, "cannot allocate memory window\n");
255 sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
256 sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
258 /* Check the unit we are attaching by its base address. */
259 start = rman_get_start(sc->sc_mem_res);
260 for (i = 0; i < nitems(bcm_bsc_pins); i++) {
261 if (bcm_bsc_pins[i].start == start)
264 if (i == nitems(bcm_bsc_pins)) {
265 device_printf(dev, "only bsc0 and bsc1 are supported\n");
270 * Configure the GPIO pins to ALT0 function to enable BSC control
273 gpio = devclass_get_device(devclass_find("gpio"), 0);
275 device_printf(dev, "cannot find gpio0\n");
278 bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].sda, BCM_GPIO_ALT0);
279 bcm_gpio_set_alternate(gpio, bcm_bsc_pins[i].scl, BCM_GPIO_ALT0);
282 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
283 RF_ACTIVE | RF_SHAREABLE);
284 if (!sc->sc_irq_res) {
285 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
286 device_printf(dev, "cannot allocate interrupt\n");
290 /* Hook up our interrupt handler. */
291 if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
292 NULL, bcm_bsc_intr, sc, &sc->sc_intrhand)) {
293 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
294 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
295 device_printf(dev, "cannot setup the interrupt handler\n");
299 mtx_init(&sc->sc_mtx, "bcm_bsc", NULL, MTX_DEF);
301 bcm_bsc_sysctl_init(sc);
303 /* Enable the BSC controller. Flush the FIFO. */
305 BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN);
309 device_add_child(dev, "iicbus", -1);
311 return (bus_generic_attach(dev));
315 bcm_bsc_detach(device_t dev)
317 struct bcm_bsc_softc *sc;
319 bus_generic_detach(dev);
321 sc = device_get_softc(dev);
322 mtx_destroy(&sc->sc_mtx);
324 bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
326 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
328 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
334 bcm_bsc_intr(void *arg)
336 struct bcm_bsc_softc *sc;
339 sc = (struct bcm_bsc_softc *)arg;
343 /* The I2C interrupt is shared among all the BSC controllers. */
344 if ((sc->sc_flags & BCM_I2C_BUSY) == 0) {
349 status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
351 /* Check for errors. */
352 if (status & (BCM_BSC_STATUS_CLKT | BCM_BSC_STATUS_ERR)) {
353 /* Disable interrupts. */
354 BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN);
355 sc->sc_flags |= BCM_I2C_ERROR;
362 if (sc->sc_flags & BCM_I2C_READ) {
363 while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_RXD)) {
364 *sc->sc_data++ = BCM_BSC_READ(sc, BCM_BSC_DATA);
366 status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
369 while (sc->sc_resid > 0 && (status & BCM_BSC_STATUS_TXD)) {
370 BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data++);
372 status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
376 if (status & BCM_BSC_STATUS_DONE) {
377 /* Disable interrupts. */
378 BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN);
387 bcm_bsc_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
389 struct bcm_bsc_softc *sc;
390 uint32_t intr, read, status;
393 sc = device_get_softc(dev);
396 /* If the controller is busy wait until it is available. */
397 while (sc->sc_flags & BCM_I2C_BUSY)
398 mtx_sleep(dev, &sc->sc_mtx, 0, "bcm_bsc", 0);
400 /* Now we have control over the BSC controller. */
401 sc->sc_flags = BCM_I2C_BUSY;
403 /* Clear the FIFO and the pending interrupts. */
407 for (i = 0; i < nmsgs; i++) {
409 /* Write the slave address. */
410 BCM_BSC_WRITE(sc, BCM_BSC_SLAVE, msgs[i].slave >> 1);
412 /* Write the data length. */
413 BCM_BSC_WRITE(sc, BCM_BSC_DLEN, msgs[i].len);
415 sc->sc_data = msgs[i].buf;
416 sc->sc_resid = msgs[i].len;
417 if ((msgs[i].flags & IIC_M_RD) == 0) {
418 /* Fill up the TX FIFO. */
419 status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
420 while (sc->sc_resid > 0 &&
421 (status & BCM_BSC_STATUS_TXD)) {
422 BCM_BSC_WRITE(sc, BCM_BSC_DATA, *sc->sc_data);
425 status = BCM_BSC_READ(sc, BCM_BSC_STATUS);
428 intr = BCM_BSC_CTRL_INTT;
429 sc->sc_flags &= ~BCM_I2C_READ;
431 sc->sc_flags |= BCM_I2C_READ;
432 read = BCM_BSC_CTRL_READ;
433 intr = BCM_BSC_CTRL_INTR;
435 intr |= BCM_BSC_CTRL_INTD;
437 /* Start the transfer. */
438 BCM_BSC_WRITE(sc, BCM_BSC_CTRL, BCM_BSC_CTRL_I2CEN |
439 BCM_BSC_CTRL_ST | read | intr);
441 /* Wait for the transaction to complete. */
442 err = mtx_sleep(dev, &sc->sc_mtx, 0, "bcm_bsc", hz);
444 /* Check if we have a timeout or an I2C error. */
445 if ((sc->sc_flags & BCM_I2C_ERROR) || err == EWOULDBLOCK) {
446 device_printf(sc->sc_dev, "I2C error\n");
452 /* Clean the controller flags. */
461 bcm_bsc_get_node(device_t bus, device_t dev)
464 /* We only have one child, the I2C bus, which needs our own node. */
465 return (ofw_bus_get_node(bus));
468 static device_method_t bcm_bsc_methods[] = {
469 /* Device interface */
470 DEVMETHOD(device_probe, bcm_bsc_probe),
471 DEVMETHOD(device_attach, bcm_bsc_attach),
472 DEVMETHOD(device_detach, bcm_bsc_detach),
474 /* iicbus interface */
475 DEVMETHOD(iicbus_callback, iicbus_null_callback),
476 DEVMETHOD(iicbus_transfer, bcm_bsc_transfer),
478 /* ofw_bus interface */
479 DEVMETHOD(ofw_bus_get_node, bcm_bsc_get_node),
484 static devclass_t bcm_bsc_devclass;
486 static driver_t bcm_bsc_driver = {
489 sizeof(struct bcm_bsc_softc),
492 DRIVER_MODULE(iicbus, bcm2835_bsc, iicbus_driver, iicbus_devclass, 0, 0);
493 DRIVER_MODULE(bcm2835_bsc, simplebus, bcm_bsc_driver, bcm_bsc_devclass, 0, 0);