2 * Copyright (c) 2015 M. Warner Losh <imp@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include "opt_platform.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
41 #include <dev/gpio/gpiobusvar.h>
42 #include <dev/ow/owll.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
48 static struct ofw_compat_data compat_data[] = {
52 OFWBUS_PNP_INFO(compat_data);
53 SIMPLEBUS_PNP_INFO(compat_data);
58 #define OWC_GPIOBUS_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
59 #define OWC_GPIOBUS_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
60 #define OWC_GPIOBUS_LOCK_INIT(_sc) \
61 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
62 "owc_gpiobus", MTX_DEF)
63 #define OWC_GPIOBUS_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
65 struct owc_gpiobus_softc
72 static int owc_gpiobus_probe(device_t);
73 static int owc_gpiobus_attach(device_t);
74 static int owc_gpiobus_detach(device_t);
77 owc_gpiobus_probe(device_t dev)
82 * By default we only bid to attach if specifically added by our parent
83 * (usually via hint.owc_gpiobus.#.at=busname). On FDT systems we bid
84 * as the default driver based on being configured in the FDT data.
86 rv = BUS_PROBE_NOWILDCARD;
89 if (ofw_bus_status_okay(dev) &&
90 ofw_bus_search_compatible(dev, compat_data)->ocd_data)
91 rv = BUS_PROBE_DEFAULT;
94 device_set_desc(dev, "GPIO one-wire bus");
100 owc_gpiobus_attach(device_t dev)
102 struct owc_gpiobus_softc *sc;
105 sc = device_get_softc(dev);
109 /* Try to configure our pin from fdt data on fdt-based systems. */
110 err = gpio_pin_get_by_ofw_idx(dev, ofw_bus_get_node(dev), OW_PIN,
116 * If we didn't get configured by fdt data and our parent is gpiobus,
117 * see if we can be configured by the bus (allows hinted attachment even
118 * on fdt-based systems).
121 strcmp("gpiobus", device_get_name(device_get_parent(dev))) == 0)
122 err = gpio_pin_get_by_child_index(dev, OW_PIN, &sc->sc_pin);
124 /* If we didn't get configured by either method, whine and punt. */
126 device_printf(sc->sc_dev,
127 "cannot acquire gpio pin (config error)\n");
131 OWC_GPIOBUS_LOCK_INIT(sc);
134 * Add the ow bus as a child, but defer probing and attaching it until
135 * interrupts work, because we can't do IO for them until we can read
136 * the system timecounter (which initializes after device attachments).
138 device_add_child(sc->sc_dev, "ow", -1);
139 return (bus_delayed_attach_children(dev));
143 owc_gpiobus_detach(device_t dev)
145 struct owc_gpiobus_softc *sc;
148 sc = device_get_softc(dev);
150 if ((err = device_delete_children(dev)) != 0)
153 gpio_pin_release(sc->sc_pin);
154 OWC_GPIOBUS_LOCK_DESTROY(sc);
160 * In the diagrams below, R is driven by the resistor pullup, M is driven by the
161 * master, and S is driven by the slave / target.
165 * These macros let what why we're doing stuff shine in the code
166 * below, and let the how be confined to here.
168 #define OUTPIN(sc) gpio_pin_setflags((sc)->sc_pin, GPIO_PIN_OUTPUT)
169 #define INPIN(sc) gpio_pin_setflags((sc)->sc_pin, GPIO_PIN_INPUT)
170 #define GETPIN(sc, bp) gpio_pin_is_active((sc)->sc_pin, (bp))
171 #define LOW(sc) gpio_pin_set_active((sc)->sc_pin, false)
174 * WRITE-ONE (see owll_if.m for timings) From Figure 4-1 AN-937
176 * |<---------tSLOT---->|<-tREC->|
177 * High RRRRM | RRRRRRRRRRRR|RRRRRRRRM
180 * Low MMMMMMM | | | MMMMMM...
182 * |<------15us--->| |
183 * |<--------60us---->|
186 owc_gpiobus_write_one(device_t dev, struct ow_timing *t)
188 struct owc_gpiobus_softc *sc;
190 sc = device_get_softc(dev);
199 /* Allow resistor to float line high */
201 DELAY(t->t_slot - t->t_low1 + t->t_rec);
209 * WRITE-ZERO (see owll_if.m for timings) From Figure 4-2 AN-937
211 * |<---------tSLOT------>|<-tREC->|
212 * High RRRRM | | |RRRRRRRM
215 * Low MMMMMMMMMMMMMMMMMMMMMR MMMMMM...
217 * |<------60us--->| |
218 * |<-------tLOW0------>|
221 owc_gpiobus_write_zero(device_t dev, struct ow_timing *t)
223 struct owc_gpiobus_softc *sc;
225 sc = device_get_softc(dev);
234 /* Allow resistor to float line high */
236 DELAY(t->t_slot - t->t_low0 + t->t_rec);
244 * READ-DATA (see owll_if.m for timings) From Figure 4-3 AN-937
246 * |<---------tSLOT------>|<-tREC->|
247 * High RRRRM | rrrrrrrrrrrrrrrRRRRRRRM
250 * Low MMMMMMMSSSSSSSSSSSSSSR MMMMMM...
251 * |<tLOWR>< sample > |
252 * |<------tRDV---->| |
255 * r -- allowed to pull high via the resitor when slave writes a 1-bit
259 owc_gpiobus_read_data(device_t dev, struct ow_timing *t, int *bit)
261 struct owc_gpiobus_softc *sc;
263 sbintime_t then, now;
265 sc = device_get_softc(dev);
269 /* Force low for t_lowr microseconds */
276 * Slave is supposed to hold the line low for t_rdv microseconds for 0
277 * and immediately float it high for a 1. This is measured from the
278 * master's pushing the line low.
284 } while (now - then < (t->t_rdv + 2) * SBT_1US && sample == false);
287 if (now - then < t->t_rdv * SBT_1US)
292 /* Wait out the rest of t_slot */
295 } while (now - then < (t->t_slot + t->t_rec) * SBT_1US);
301 * RESET AND PRESENCE PULSE (see owll_if.m for timings) From Figure 4-4 AN-937
303 * |<---------tRSTH------------>|
304 * High RRRM | | RRRRRRRS | RRRR RRM
307 * Low MMMMMMMM MMMMMM| | | SSSSSSSSSS MMMMMM
308 * |<----tRSTL--->| | |<-tPDL---->|
312 * Note: for Regular Speed operations, tRSTL + tR should be less than 960us to
313 * avoid interfering with other devices on the bus.
315 * Return values in *bit:
316 * -1 = Bus wiring error (stuck low).
317 * 0 = no presence pulse
318 * 1 = presence pulse detected
321 owc_gpiobus_reset_and_presence(device_t dev, struct ow_timing *t, int *bit)
323 struct owc_gpiobus_softc *sc;
326 sc = device_get_softc(dev);
329 * Read the current state of the bus. The steady state of an idle bus is
330 * high. Badly wired buses that are missing the required pull up, or
331 * that have a short circuit to ground cause all kinds of mischief when
332 * we try to read them later. Return EIO if the bus is currently low.
336 if (sample == false) {
348 /* Allow resistor to float line high and then wait for reset pulse */
350 DELAY(t->t_pdh + t->t_pdl / 2);
352 /* Read presence pulse */
358 DELAY(t->t_rsth - (t->t_pdh + t->t_pdl / 2)); /* Timing not critical for this one */
361 * Read the state of the bus after we've waited past the end of the rest
362 * window. It should return to high. If it is low, then we have some
363 * problem and should abort the reset.
366 if (sample == false) {
374 static devclass_t owc_gpiobus_devclass;
376 static device_method_t owc_gpiobus_methods[] = {
377 /* Device interface */
378 DEVMETHOD(device_probe, owc_gpiobus_probe),
379 DEVMETHOD(device_attach, owc_gpiobus_attach),
380 DEVMETHOD(device_detach, owc_gpiobus_detach),
382 DEVMETHOD(owll_write_one, owc_gpiobus_write_one),
383 DEVMETHOD(owll_write_zero, owc_gpiobus_write_zero),
384 DEVMETHOD(owll_read_data, owc_gpiobus_read_data),
385 DEVMETHOD(owll_reset_and_presence, owc_gpiobus_reset_and_presence),
389 static driver_t owc_gpiobus_driver = {
392 sizeof(struct owc_gpiobus_softc),
396 DRIVER_MODULE(owc_gpiobus, simplebus, owc_gpiobus_driver, owc_gpiobus_devclass, 0, 0);
399 DRIVER_MODULE(owc_gpiobus, gpiobus, owc_gpiobus_driver, owc_gpiobus_devclass, 0, 0);
400 MODULE_DEPEND(owc_gpiobus, ow, 1, 1, 1);
401 MODULE_DEPEND(owc_gpiobus, gpiobus, 1, 1, 1);
402 MODULE_VERSION(owc_gpiobus, 1);