2 * Copyright (c) 2015 M. Warner Losh <imp@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_platform.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
43 #include <dev/fdt/fdt_common.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
48 #include <dev/gpio/gpiobusvar.h>
49 #include "gpiobus_if.h"
51 #include <dev/ow/owll.h>
55 #define OWC_GPIOBUS_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
56 #define OWC_GPIOBUS_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
57 #define OWC_GPIOBUS_LOCK_INIT(_sc) \
58 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
59 "owc_gpiobus", MTX_DEF)
60 #define OWC_GPIOBUS_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
62 struct owc_gpiobus_softc
69 static int owc_gpiobus_probe(device_t);
70 static int owc_gpiobus_attach(device_t);
71 static int owc_gpiobus_detach(device_t);
75 owc_gpiobus_identify(driver_t *driver, device_t bus)
80 * Find all the 1-wire bus pseudo-nodes that are
81 * at the top level of the FDT. Would be nice to
82 * somehow preserve the node name of these busses,
83 * but there's no good place to put it. The driver's
84 * name is used for the device name, and the 1-wire
85 * bus overwrites the description.
87 root = OF_finddevice("/");
90 for (w1 = OF_child(root); w1 != 0; w1 = OF_peer(w1)) {
91 if (!fdt_is_compatible_strict(w1, "w1-gpio"))
93 if (!OF_hasprop(w1, "gpios"))
95 ofw_gpiobus_add_fdt_child(bus, driver->name, w1);
101 owc_gpiobus_probe(device_t dev)
104 if (!ofw_bus_status_okay(dev))
107 if (ofw_bus_is_compatible(dev, "w1-gpio")) {
108 device_set_desc(dev, "FDT GPIO attached one-wire bus");
109 return (BUS_PROBE_DEFAULT);
114 device_set_desc(dev, "GPIO attached one-wire bus");
120 owc_gpiobus_attach(device_t dev)
122 struct owc_gpiobus_softc *sc;
126 sc = device_get_softc(dev);
128 sc->sc_busdev = device_get_parent(dev);
129 OWC_GPIOBUS_LOCK_INIT(sc);
131 if (device_get_children(dev, &kids, &nkid) == 0)
134 device_add_child(dev, "ow", -1);
135 bus_generic_attach(dev);
141 owc_gpiobus_detach(device_t dev)
143 struct owc_gpiobus_softc *sc;
145 sc = device_get_softc(dev);
146 OWC_GPIOBUS_LOCK_DESTROY(sc);
147 bus_generic_detach(dev);
152 * In the diagrams below, R is driven by the resistor pullup, M is driven by the
153 * master, and S is driven by the slave / target.
157 * These macros let what why we're doing stuff shine in the code
158 * below, and let the how be confined to here.
160 #define GETBUS(sc) GPIOBUS_ACQUIRE_BUS((sc)->sc_busdev, \
161 (sc)->sc_dev, GPIOBUS_WAIT)
162 #define RELBUS(sc) GPIOBUS_RELEASE_BUS((sc)->sc_busdev, \
164 #define OUTPIN(sc) GPIOBUS_PIN_SETFLAGS((sc)->sc_busdev, \
165 (sc)->sc_dev, OW_PIN, GPIO_PIN_OUTPUT)
166 #define INPIN(sc) GPIOBUS_PIN_SETFLAGS((sc)->sc_busdev, \
167 (sc)->sc_dev, OW_PIN, GPIO_PIN_INPUT)
168 #define GETPIN(sc, bit) GPIOBUS_PIN_GET((sc)->sc_busdev, \
169 (sc)->sc_dev, OW_PIN, bit)
170 #define LOW(sc) GPIOBUS_PIN_SET((sc)->sc_busdev, \
171 (sc)->sc_dev, OW_PIN, GPIO_PIN_LOW)
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;
191 sc = device_get_softc(dev);
203 /* Allow resistor to float line high */
205 DELAY(t->t_slot - t->t_low1 + t->t_rec);
215 * WRITE-ZERO (see owll_if.m for timings) From Figure 4-2 AN-937
217 * |<---------tSLOT------>|<-tREC->|
218 * High RRRRM | | |RRRRRRRM
221 * Low MMMMMMMMMMMMMMMMMMMMMR MMMMMM...
223 * |<------60us--->| |
224 * |<-------tLOW0------>|
227 owc_gpiobus_write_zero(device_t dev, struct ow_timing *t)
229 struct owc_gpiobus_softc *sc;
232 sc = device_get_softc(dev);
244 /* Allow resistor to float line high */
246 DELAY(t->t_slot - t->t_low0 + t->t_rec);
256 * READ-DATA (see owll_if.m for timings) From Figure 4-3 AN-937
258 * |<---------tSLOT------>|<-tREC->|
259 * High RRRRM | rrrrrrrrrrrrrrrRRRRRRRM
262 * Low MMMMMMMSSSSSSSSSSSSSSR MMMMMM...
263 * |<tLOWR>< sample > |
264 * |<------tRDV---->| |
267 * r -- allowed to pull high via the resitor when slave writes a 1-bit
271 owc_gpiobus_read_data(device_t dev, struct ow_timing *t, int *bit)
273 struct owc_gpiobus_softc *sc;
275 sbintime_t then, now;
277 sc = device_get_softc(dev);
282 /* Force low for t_lowr microseconds */
289 * Slave is supposed to hold the line low for t_rdv microseconds for 0
290 * and immediately float it high for a 1. This is measured from the
291 * master's pushing the line low.
298 } while ((now - then) / SBT_1US < t->t_rdv + 2 && sample == 0);
301 if ((now - then) / SBT_1NS < t->t_rdv * 1000)
306 /* Wait out the rest of t_slot */
309 } while ((now - then) / SBT_1US < t->t_slot);
317 * RESET AND PRESENCE PULSE (see owll_if.m for timings) From Figure 4-4 AN-937
319 * |<---------tRSTH------------>|
320 * High RRRM | | RRRRRRRS | RRRR RRM
323 * Low MMMMMMMM MMMMMM| | | SSSSSSSSSS MMMMMM
324 * |<----tRSTL--->| | |<-tPDL---->|
328 * Note: for Regular Speed operations, tRSTL + tR should be less than 960us to
329 * avoid interferring with other devices on the bus
332 owc_gpiobus_reset_and_presence(device_t dev, struct ow_timing *t, int *bit)
334 struct owc_gpiobus_softc *sc;
338 sc = device_get_softc(dev);
345 * Read the current state of the bus. The steady state of an idle bus is
346 * high. Badly wired buses that are missing the required pull up, or
347 * that have a short circuit to ground cause all kinds of mischief when
348 * we try to read them later. Return EIO and release the bus if the bus
366 /* Allow resistor to float line high and then wait for reset pulse */
368 DELAY(t->t_pdh + t->t_pdl / 2);
370 /* Read presence pulse */
376 DELAY(t->t_rsth - (t->t_pdh + t->t_pdl / 2)); /* Timing not critical for this one */
379 * Read the state of the bus after we've waited past the end of the rest
380 * window. It should return to high. If it is low, then we have some
381 * problem and should abort the reset.
395 static devclass_t owc_gpiobus_devclass;
397 static device_method_t owc_gpiobus_methods[] = {
398 /* Device interface */
400 DEVMETHOD(device_identify, owc_gpiobus_identify),
402 DEVMETHOD(device_probe, owc_gpiobus_probe),
403 DEVMETHOD(device_attach, owc_gpiobus_attach),
404 DEVMETHOD(device_detach, owc_gpiobus_detach),
406 DEVMETHOD(owll_write_one, owc_gpiobus_write_one),
407 DEVMETHOD(owll_write_zero, owc_gpiobus_write_zero),
408 DEVMETHOD(owll_read_data, owc_gpiobus_read_data),
409 DEVMETHOD(owll_reset_and_presence, owc_gpiobus_reset_and_presence),
413 static driver_t owc_gpiobus_driver = {
416 sizeof(struct owc_gpiobus_softc),
419 DRIVER_MODULE(owc_gpiobus_fdt, gpiobus, owc_gpiobus_driver, owc_gpiobus_devclass, 0, 0);
420 MODULE_DEPEND(owc_gpiobus_fdt, ow, 1, 1, 1);