xref: /freebsd/sys/dev/ow/owc_gpiobus.c (revision 685dc743)
1ae1f3df4SWarner Losh /*-
2f86e6000SWarner Losh  * Copyright (c) 2015 M. Warner Losh <imp@FreeBSD.org>
3ae1f3df4SWarner Losh  *
4ae1f3df4SWarner Losh  * Redistribution and use in source and binary forms, with or without
5ae1f3df4SWarner Losh  * modification, are permitted provided that the following conditions
6ae1f3df4SWarner Losh  * are met:
7ae1f3df4SWarner Losh  * 1. Redistributions of source code must retain the above copyright
8ae1f3df4SWarner Losh  *    notice, this list of conditions and the following disclaimer.
9ae1f3df4SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
10ae1f3df4SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
11ae1f3df4SWarner Losh  *    documentation and/or other materials provided with the distribution.
12ae1f3df4SWarner Losh  *
13ae1f3df4SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14ae1f3df4SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15ae1f3df4SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16ae1f3df4SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17ae1f3df4SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18ae1f3df4SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19ae1f3df4SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20ae1f3df4SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21ae1f3df4SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22ae1f3df4SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23ae1f3df4SWarner Losh  * SUCH DAMAGE.
24ae1f3df4SWarner Losh  */
25ae1f3df4SWarner Losh 
26ae1f3df4SWarner Losh #include <sys/cdefs.h>
27ae1f3df4SWarner Losh #include "opt_platform.h"
28ae1f3df4SWarner Losh 
29ae1f3df4SWarner Losh #include <sys/param.h>
30ae1f3df4SWarner Losh #include <sys/systm.h>
31ae1f3df4SWarner Losh #include <sys/bus.h>
32ae1f3df4SWarner Losh #include <sys/gpio.h>
33ae1f3df4SWarner Losh #include <sys/kernel.h>
34ae1f3df4SWarner Losh #include <sys/lock.h>
35ae1f3df4SWarner Losh #include <sys/malloc.h>
36ae1f3df4SWarner Losh #include <sys/module.h>
37ae1f3df4SWarner Losh #include <sys/mutex.h>
38ae1f3df4SWarner Losh 
39c7b0edf2SIan Lepore #include <dev/gpio/gpiobusvar.h>
40c7b0edf2SIan Lepore #include <dev/ow/owll.h>
41c7b0edf2SIan Lepore 
42ae1f3df4SWarner Losh #ifdef FDT
43ae1f3df4SWarner Losh #include <dev/ofw/ofw_bus.h>
44ae1f3df4SWarner Losh #include <dev/ofw/ofw_bus_subr.h>
45ae1f3df4SWarner Losh 
46c7b0edf2SIan Lepore static struct ofw_compat_data compat_data[] = {
47c7b0edf2SIan Lepore 	{"w1-gpio",  true},
48c7b0edf2SIan Lepore 	{NULL,       false}
49c7b0edf2SIan Lepore };
50519b64e2SMark Johnston OFWBUS_PNP_INFO(compat_data);
51519b64e2SMark Johnston SIMPLEBUS_PNP_INFO(compat_data);
52c7b0edf2SIan Lepore #endif /* FDT */
53ae1f3df4SWarner Losh 
54ae1f3df4SWarner Losh #define	OW_PIN		0
55ae1f3df4SWarner Losh 
56ae1f3df4SWarner Losh #define OWC_GPIOBUS_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
57ae1f3df4SWarner Losh #define	OWC_GPIOBUS_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
58ae1f3df4SWarner Losh #define OWC_GPIOBUS_LOCK_INIT(_sc) \
59ae1f3df4SWarner Losh 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
60ae1f3df4SWarner Losh 	    "owc_gpiobus", MTX_DEF)
61ae1f3df4SWarner Losh #define OWC_GPIOBUS_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
62ae1f3df4SWarner Losh 
63ae1f3df4SWarner Losh struct owc_gpiobus_softc
64ae1f3df4SWarner Losh {
65ae1f3df4SWarner Losh 	device_t	sc_dev;
66c7b0edf2SIan Lepore 	gpio_pin_t	sc_pin;
67ae1f3df4SWarner Losh 	struct mtx	sc_mtx;
68ae1f3df4SWarner Losh };
69ae1f3df4SWarner Losh 
70ae1f3df4SWarner Losh static int owc_gpiobus_probe(device_t);
71ae1f3df4SWarner Losh static int owc_gpiobus_attach(device_t);
72ae1f3df4SWarner Losh static int owc_gpiobus_detach(device_t);
73ae1f3df4SWarner Losh 
74ae1f3df4SWarner Losh static int
owc_gpiobus_probe(device_t dev)75ae1f3df4SWarner Losh owc_gpiobus_probe(device_t dev)
76ae1f3df4SWarner Losh {
77c7b0edf2SIan Lepore 	int rv;
78c7b0edf2SIan Lepore 
79c7b0edf2SIan Lepore 	/*
80c7b0edf2SIan Lepore 	 * By default we only bid to attach if specifically added by our parent
81c7b0edf2SIan Lepore 	 * (usually via hint.owc_gpiobus.#.at=busname).  On FDT systems we bid
82c7b0edf2SIan Lepore 	 * as the default driver based on being configured in the FDT data.
83c7b0edf2SIan Lepore 	 */
84c7b0edf2SIan Lepore 	rv = BUS_PROBE_NOWILDCARD;
85c7b0edf2SIan Lepore 
86ae1f3df4SWarner Losh #ifdef FDT
87c7b0edf2SIan Lepore 	if (ofw_bus_status_okay(dev) &&
88c7b0edf2SIan Lepore 	    ofw_bus_search_compatible(dev, compat_data)->ocd_data)
89c7b0edf2SIan Lepore 		rv = BUS_PROBE_DEFAULT;
90ae1f3df4SWarner Losh #endif
91c7b0edf2SIan Lepore 
92c7b0edf2SIan Lepore 	device_set_desc(dev, "GPIO one-wire bus");
93c7b0edf2SIan Lepore 
94c7b0edf2SIan Lepore 	return (rv);
95ae1f3df4SWarner Losh }
96ae1f3df4SWarner Losh 
97ae1f3df4SWarner Losh static int
owc_gpiobus_attach(device_t dev)98ae1f3df4SWarner Losh owc_gpiobus_attach(device_t dev)
99ae1f3df4SWarner Losh {
100ae1f3df4SWarner Losh 	struct owc_gpiobus_softc *sc;
101c7b0edf2SIan Lepore 	int err;
102ae1f3df4SWarner Losh 
103ae1f3df4SWarner Losh 	sc = device_get_softc(dev);
104ae1f3df4SWarner Losh 	sc->sc_dev = dev;
105c7b0edf2SIan Lepore 
106c7b0edf2SIan Lepore #ifdef FDT
107c7b0edf2SIan Lepore 	/* Try to configure our pin from fdt data on fdt-based systems. */
108c7b0edf2SIan Lepore 	err = gpio_pin_get_by_ofw_idx(dev, ofw_bus_get_node(dev), OW_PIN,
109c7b0edf2SIan Lepore 	    &sc->sc_pin);
110c7b0edf2SIan Lepore #else
111c7b0edf2SIan Lepore 	err = ENOENT;
112c7b0edf2SIan Lepore #endif
113c7b0edf2SIan Lepore 	/*
114c7b0edf2SIan Lepore 	 * If we didn't get configured by fdt data and our parent is gpiobus,
115c7b0edf2SIan Lepore 	 * see if we can be configured by the bus (allows hinted attachment even
116c7b0edf2SIan Lepore 	 * on fdt-based systems).
117c7b0edf2SIan Lepore 	 */
118c7b0edf2SIan Lepore 	if (err != 0 &&
119c7b0edf2SIan Lepore 	    strcmp("gpiobus", device_get_name(device_get_parent(dev))) == 0)
120c7b0edf2SIan Lepore 		err = gpio_pin_get_by_child_index(dev, OW_PIN, &sc->sc_pin);
121c7b0edf2SIan Lepore 
122c7b0edf2SIan Lepore 	/* If we didn't get configured by either method, whine and punt. */
123c7b0edf2SIan Lepore 	if (err != 0) {
124c7b0edf2SIan Lepore 		device_printf(sc->sc_dev,
125c7b0edf2SIan Lepore 		    "cannot acquire gpio pin (config error)\n");
126c7b0edf2SIan Lepore 		return (err);
127c7b0edf2SIan Lepore 	}
128c7b0edf2SIan Lepore 
129ae1f3df4SWarner Losh 	OWC_GPIOBUS_LOCK_INIT(sc);
130c7b0edf2SIan Lepore 
131c7b0edf2SIan Lepore 	/*
132c7b0edf2SIan Lepore 	 * Add the ow bus as a child, but defer probing and attaching it until
133c7b0edf2SIan Lepore 	 * interrupts work, because we can't do IO for them until we can read
134c7b0edf2SIan Lepore 	 * the system timecounter (which initializes after device attachments).
135c7b0edf2SIan Lepore 	 */
136c7b0edf2SIan Lepore 	device_add_child(sc->sc_dev, "ow", -1);
1379f07ef76SWarner Losh 	return (bus_delayed_attach_children(dev));
138ae1f3df4SWarner Losh }
139ae1f3df4SWarner Losh 
140ae1f3df4SWarner Losh static int
owc_gpiobus_detach(device_t dev)141ae1f3df4SWarner Losh owc_gpiobus_detach(device_t dev)
142ae1f3df4SWarner Losh {
143ae1f3df4SWarner Losh 	struct owc_gpiobus_softc *sc;
144c7b0edf2SIan Lepore 	int err;
145ae1f3df4SWarner Losh 
146ae1f3df4SWarner Losh 	sc = device_get_softc(dev);
147c7b0edf2SIan Lepore 
148c7b0edf2SIan Lepore 	if ((err = device_delete_children(dev)) != 0)
149c7b0edf2SIan Lepore 		return (err);
150c7b0edf2SIan Lepore 
151c7b0edf2SIan Lepore 	gpio_pin_release(sc->sc_pin);
152ae1f3df4SWarner Losh 	OWC_GPIOBUS_LOCK_DESTROY(sc);
153c7b0edf2SIan Lepore 
154ae1f3df4SWarner Losh 	return (0);
155ae1f3df4SWarner Losh }
156ae1f3df4SWarner Losh 
157ae1f3df4SWarner Losh /*
158ae1f3df4SWarner Losh  * In the diagrams below, R is driven by the resistor pullup, M is driven by the
159ae1f3df4SWarner Losh  * master, and S is driven by the slave / target.
160ae1f3df4SWarner Losh  */
161ae1f3df4SWarner Losh 
162ae1f3df4SWarner Losh /*
163ae1f3df4SWarner Losh  * These macros let what why we're doing stuff shine in the code
164ae1f3df4SWarner Losh  * below, and let the how be confined to here.
165ae1f3df4SWarner Losh  */
166c7b0edf2SIan Lepore #define OUTPIN(sc)	gpio_pin_setflags((sc)->sc_pin, GPIO_PIN_OUTPUT)
167c7b0edf2SIan Lepore #define INPIN(sc)	gpio_pin_setflags((sc)->sc_pin, GPIO_PIN_INPUT)
168c7b0edf2SIan Lepore #define GETPIN(sc, bp)	gpio_pin_is_active((sc)->sc_pin, (bp))
169c7b0edf2SIan Lepore #define LOW(sc)		gpio_pin_set_active((sc)->sc_pin, false)
170ae1f3df4SWarner Losh 
171ae1f3df4SWarner Losh /*
172ae1f3df4SWarner Losh  * WRITE-ONE (see owll_if.m for timings) From Figure 4-1 AN-937
173ae1f3df4SWarner Losh  *
174ae1f3df4SWarner Losh  *		       |<---------tSLOT---->|<-tREC->|
175ae1f3df4SWarner Losh  *	High	RRRRM  | 	RRRRRRRRRRRR|RRRRRRRRM
176ae1f3df4SWarner Losh  *		     M |       R |     |  |	      M
177ae1f3df4SWarner Losh  *		      M|      R	 |     |  |	       M
178ae1f3df4SWarner Losh  *	Low	       MMMMMMM	 |     |  |    	        MMMMMM...
179ae1f3df4SWarner Losh  *		       |<-tLOW1->|     |  |
180ae1f3df4SWarner Losh  *		       |<------15us--->|  |
181ae1f3df4SWarner Losh  *                     |<--------60us---->|
182ae1f3df4SWarner Losh  */
183ae1f3df4SWarner Losh static int
owc_gpiobus_write_one(device_t dev,struct ow_timing * t)184ae1f3df4SWarner Losh owc_gpiobus_write_one(device_t dev, struct ow_timing *t)
185ae1f3df4SWarner Losh {
186ae1f3df4SWarner Losh 	struct owc_gpiobus_softc *sc;
187ae1f3df4SWarner Losh 
188ae1f3df4SWarner Losh 	sc = device_get_softc(dev);
189ae1f3df4SWarner Losh 
190ae1f3df4SWarner Losh 	critical_enter();
191ae1f3df4SWarner Losh 
192ae1f3df4SWarner Losh 	/* Force low */
193ae1f3df4SWarner Losh 	OUTPIN(sc);
194ae1f3df4SWarner Losh 	LOW(sc);
195ae1f3df4SWarner Losh 	DELAY(t->t_low1);
196ae1f3df4SWarner Losh 
197ae1f3df4SWarner Losh 	/* Allow resistor to float line high */
198ae1f3df4SWarner Losh 	INPIN(sc);
199ae1f3df4SWarner Losh 	DELAY(t->t_slot - t->t_low1 + t->t_rec);
200ae1f3df4SWarner Losh 
201ae1f3df4SWarner Losh 	critical_exit();
202ae1f3df4SWarner Losh 
20301f1fff0SAndriy Gapon 	return (0);
204ae1f3df4SWarner Losh }
205ae1f3df4SWarner Losh 
206ae1f3df4SWarner Losh /*
207ae1f3df4SWarner Losh  * WRITE-ZERO (see owll_if.m for timings) From Figure 4-2 AN-937
208ae1f3df4SWarner Losh  *
209ae1f3df4SWarner Losh  *		       |<---------tSLOT------>|<-tREC->|
210ae1f3df4SWarner Losh  *	High	RRRRM  | 	            | |RRRRRRRM
211ae1f3df4SWarner Losh  *		     M |                    | R	       M
212ae1f3df4SWarner Losh  *		      M|       	 |     |    |R 	        M
213ae1f3df4SWarner Losh  *	Low	       MMMMMMMMMMMMMMMMMMMMMR  	         MMMMMM...
214ae1f3df4SWarner Losh  *     	       	       |<--15us->|     |    |
215ae1f3df4SWarner Losh  *     	       	       |<------60us--->|    |
216ae1f3df4SWarner Losh  *                     |<-------tLOW0------>|
217ae1f3df4SWarner Losh  */
218ae1f3df4SWarner Losh static int
owc_gpiobus_write_zero(device_t dev,struct ow_timing * t)219ae1f3df4SWarner Losh owc_gpiobus_write_zero(device_t dev, struct ow_timing *t)
220ae1f3df4SWarner Losh {
221ae1f3df4SWarner Losh 	struct owc_gpiobus_softc *sc;
222ae1f3df4SWarner Losh 
223ae1f3df4SWarner Losh 	sc = device_get_softc(dev);
224ae1f3df4SWarner Losh 
225ae1f3df4SWarner Losh 	critical_enter();
226ae1f3df4SWarner Losh 
227ae1f3df4SWarner Losh 	/* Force low */
228ae1f3df4SWarner Losh 	OUTPIN(sc);
229ae1f3df4SWarner Losh 	LOW(sc);
230ae1f3df4SWarner Losh 	DELAY(t->t_low0);
231ae1f3df4SWarner Losh 
232ae1f3df4SWarner Losh 	/* Allow resistor to float line high */
233ae1f3df4SWarner Losh 	INPIN(sc);
234ae1f3df4SWarner Losh 	DELAY(t->t_slot - t->t_low0 + t->t_rec);
235ae1f3df4SWarner Losh 
236ae1f3df4SWarner Losh 	critical_exit();
237ae1f3df4SWarner Losh 
23801f1fff0SAndriy Gapon 	return (0);
239ae1f3df4SWarner Losh }
240ae1f3df4SWarner Losh 
241ae1f3df4SWarner Losh /*
242ae1f3df4SWarner Losh  * READ-DATA (see owll_if.m for timings) From Figure 4-3 AN-937
243ae1f3df4SWarner Losh  *
244ae1f3df4SWarner Losh  *		       |<---------tSLOT------>|<-tREC->|
245ae1f3df4SWarner Losh  *	High	RRRRM  |        rrrrrrrrrrrrrrrRRRRRRRM
246ae1f3df4SWarner Losh  *		     M |       r            | R	       M
247ae1f3df4SWarner Losh  *		      M|      r	        |   |R 	        M
248ae1f3df4SWarner Losh  *	Low	       MMMMMMMSSSSSSSSSSSSSSR  	         MMMMMM...
249ae1f3df4SWarner Losh  *     	       	       |<tLOWR>< sample	>   |
250ae1f3df4SWarner Losh  *     	       	       |<------tRDV---->|   |
251ae1f3df4SWarner Losh  *                                    ->|   |<-tRELEASE
252ae1f3df4SWarner Losh  *
253ae1f3df4SWarner Losh  * r -- allowed to pull high via the resitor when slave writes a 1-bit
254ae1f3df4SWarner Losh  *
255ae1f3df4SWarner Losh  */
256ae1f3df4SWarner Losh static int
owc_gpiobus_read_data(device_t dev,struct ow_timing * t,int * bit)257ae1f3df4SWarner Losh owc_gpiobus_read_data(device_t dev, struct ow_timing *t, int *bit)
258ae1f3df4SWarner Losh {
259ae1f3df4SWarner Losh 	struct owc_gpiobus_softc *sc;
260c7b0edf2SIan Lepore 	bool sample;
261ae1f3df4SWarner Losh 	sbintime_t then, now;
262ae1f3df4SWarner Losh 
263ae1f3df4SWarner Losh 	sc = device_get_softc(dev);
264ae1f3df4SWarner Losh 
265c1750b82SAndriy Gapon 	critical_enter();
266c1750b82SAndriy Gapon 
267ae1f3df4SWarner Losh 	/* Force low for t_lowr microseconds */
268ae1f3df4SWarner Losh 	then = sbinuptime();
269ae1f3df4SWarner Losh 	OUTPIN(sc);
270ae1f3df4SWarner Losh 	LOW(sc);
271ae1f3df4SWarner Losh 	DELAY(t->t_lowr);
272ae1f3df4SWarner Losh 
273ae1f3df4SWarner Losh 	/*
274ae1f3df4SWarner Losh 	 * Slave is supposed to hold the line low for t_rdv microseconds for 0
275ae1f3df4SWarner Losh 	 * and immediately float it high for a 1. This is measured from the
276ae1f3df4SWarner Losh 	 * master's pushing the line low.
277ae1f3df4SWarner Losh 	 */
278ae1f3df4SWarner Losh 	INPIN(sc);
279ae1f3df4SWarner Losh 	do {
280ae1f3df4SWarner Losh 		now = sbinuptime();
281ae1f3df4SWarner Losh 		GETPIN(sc, &sample);
282c7b0edf2SIan Lepore 	} while (now - then < (t->t_rdv + 2) * SBT_1US && sample == false);
283ae1f3df4SWarner Losh 	critical_exit();
284ae1f3df4SWarner Losh 
285bb7b803bSAndriy Gapon 	if (now - then < t->t_rdv * SBT_1US)
286ae1f3df4SWarner Losh 		*bit = 1;
287ae1f3df4SWarner Losh 	else
288ae1f3df4SWarner Losh 		*bit = 0;
289ae1f3df4SWarner Losh 
290ae1f3df4SWarner Losh 	/* Wait out the rest of t_slot */
291ae1f3df4SWarner Losh 	do {
292ae1f3df4SWarner Losh 		now = sbinuptime();
293b8c776baSAndriy Gapon 	} while (now - then < (t->t_slot + t->t_rec) * SBT_1US);
294ae1f3df4SWarner Losh 
295c7b0edf2SIan Lepore 	return (0);
296ae1f3df4SWarner Losh }
297ae1f3df4SWarner Losh 
298ae1f3df4SWarner Losh /*
299ae1f3df4SWarner Losh  * RESET AND PRESENCE PULSE (see owll_if.m for timings) From Figure 4-4 AN-937
300ae1f3df4SWarner Losh  *
301ae1f3df4SWarner Losh  *				    |<---------tRSTH------------>|
302ae1f3df4SWarner Losh  *	High RRRM  |		  | RRRRRRRS	       |  RRRR RRM
303ae1f3df4SWarner Losh  *		 M |		  |R|  	   |S  	       | R	  M
304ae1f3df4SWarner Losh  *		  M|		  R |	   | S	       |R	   M
305ae1f3df4SWarner Losh  *	Low	   MMMMMMMM MMMMMM| |	   |  SSSSSSSSSS	    MMMMMM
306ae1f3df4SWarner Losh  *     	       	   |<----tRSTL--->| |  	   |<-tPDL---->|
307ae1f3df4SWarner Losh  *		   |   	       	->| |<-tR  |	       |
308ae1f3df4SWarner Losh  *				    |<tPDH>|
309ae1f3df4SWarner Losh  *
310ae1f3df4SWarner Losh  * Note: for Regular Speed operations, tRSTL + tR should be less than 960us to
311c7b0edf2SIan Lepore  * avoid interfering with other devices on the bus.
312c7b0edf2SIan Lepore  *
313c7b0edf2SIan Lepore  * Return values in *bit:
314c7b0edf2SIan Lepore  *  -1 = Bus wiring error (stuck low).
315c7b0edf2SIan Lepore  *   0 = no presence pulse
316c7b0edf2SIan Lepore  *   1 = presence pulse detected
317ae1f3df4SWarner Losh  */
318ae1f3df4SWarner Losh static int
owc_gpiobus_reset_and_presence(device_t dev,struct ow_timing * t,int * bit)319ae1f3df4SWarner Losh owc_gpiobus_reset_and_presence(device_t dev, struct ow_timing *t, int *bit)
320ae1f3df4SWarner Losh {
321ae1f3df4SWarner Losh 	struct owc_gpiobus_softc *sc;
322c7b0edf2SIan Lepore 	bool sample;
323ae1f3df4SWarner Losh 
324ae1f3df4SWarner Losh 	sc = device_get_softc(dev);
325ae1f3df4SWarner Losh 
326ae1f3df4SWarner Losh 	/*
327ae1f3df4SWarner Losh 	 * Read the current state of the bus. The steady state of an idle bus is
328ae1f3df4SWarner Losh 	 * high. Badly wired buses that are missing the required pull up, or
329ae1f3df4SWarner Losh 	 * that have a short circuit to ground cause all kinds of mischief when
330c7b0edf2SIan Lepore 	 * we try to read them later. Return EIO if the bus is currently low.
331ae1f3df4SWarner Losh 	 */
332ae1f3df4SWarner Losh 	INPIN(sc);
333c7b0edf2SIan Lepore 	GETPIN(sc, &sample);
334c7b0edf2SIan Lepore 	if (sample == false) {
335ae1f3df4SWarner Losh 		*bit = -1;
33601f1fff0SAndriy Gapon 		return (EIO);
337ae1f3df4SWarner Losh 	}
338ae1f3df4SWarner Losh 
339ae1f3df4SWarner Losh 	critical_enter();
340ae1f3df4SWarner Losh 
341ae1f3df4SWarner Losh 	/* Force low */
342ae1f3df4SWarner Losh 	OUTPIN(sc);
343ae1f3df4SWarner Losh 	LOW(sc);
344ae1f3df4SWarner Losh 	DELAY(t->t_rstl);
345ae1f3df4SWarner Losh 
346ae1f3df4SWarner Losh 	/* Allow resistor to float line high and then wait for reset pulse */
347ae1f3df4SWarner Losh 	INPIN(sc);
348ae1f3df4SWarner Losh 	DELAY(t->t_pdh + t->t_pdl / 2);
349ae1f3df4SWarner Losh 
350ae1f3df4SWarner Losh 	/* Read presence pulse  */
351c7b0edf2SIan Lepore 	GETPIN(sc, &sample);
352c7b0edf2SIan Lepore 	*bit = sample;
353ae1f3df4SWarner Losh 
354ae1f3df4SWarner Losh 	critical_exit();
355ae1f3df4SWarner Losh 
356ae1f3df4SWarner Losh 	DELAY(t->t_rsth - (t->t_pdh + t->t_pdl / 2));	/* Timing not critical for this one */
357ae1f3df4SWarner Losh 
358ae1f3df4SWarner Losh 	/*
359ae1f3df4SWarner Losh 	 * Read the state of the bus after we've waited past the end of the rest
360ae1f3df4SWarner Losh 	 * window. It should return to high. If it is low, then we have some
361ae1f3df4SWarner Losh 	 * problem and should abort the reset.
362ae1f3df4SWarner Losh 	 */
363c7b0edf2SIan Lepore 	GETPIN(sc, &sample);
364c7b0edf2SIan Lepore 	if (sample == false) {
365ae1f3df4SWarner Losh 		*bit = -1;
36601f1fff0SAndriy Gapon 		return (EIO);
367ae1f3df4SWarner Losh 	}
368ae1f3df4SWarner Losh 
36901f1fff0SAndriy Gapon 	return (0);
370ae1f3df4SWarner Losh }
371ae1f3df4SWarner Losh 
372ae1f3df4SWarner Losh static device_method_t owc_gpiobus_methods[] = {
373ae1f3df4SWarner Losh 	/* Device interface */
374ae1f3df4SWarner Losh 	DEVMETHOD(device_probe,		owc_gpiobus_probe),
375ae1f3df4SWarner Losh 	DEVMETHOD(device_attach,	owc_gpiobus_attach),
376ae1f3df4SWarner Losh 	DEVMETHOD(device_detach,	owc_gpiobus_detach),
377ae1f3df4SWarner Losh 
378ae1f3df4SWarner Losh 	DEVMETHOD(owll_write_one,	owc_gpiobus_write_one),
379ae1f3df4SWarner Losh 	DEVMETHOD(owll_write_zero,	owc_gpiobus_write_zero),
380ae1f3df4SWarner Losh 	DEVMETHOD(owll_read_data,	owc_gpiobus_read_data),
381ae1f3df4SWarner Losh 	DEVMETHOD(owll_reset_and_presence,	owc_gpiobus_reset_and_presence),
382ae1f3df4SWarner Losh 	{ 0, 0 }
383ae1f3df4SWarner Losh };
384ae1f3df4SWarner Losh 
385ae1f3df4SWarner Losh static driver_t owc_gpiobus_driver = {
386ae1f3df4SWarner Losh 	"owc",
387ae1f3df4SWarner Losh 	owc_gpiobus_methods,
388ae1f3df4SWarner Losh 	sizeof(struct owc_gpiobus_softc),
389ae1f3df4SWarner Losh };
390ae1f3df4SWarner Losh 
391c7b0edf2SIan Lepore #ifdef FDT
392ffd39155SJohn Baldwin DRIVER_MODULE(owc_gpiobus, simplebus, owc_gpiobus_driver, 0, 0);
393c7b0edf2SIan Lepore #endif
394c7b0edf2SIan Lepore 
395ffd39155SJohn Baldwin DRIVER_MODULE(owc_gpiobus, gpiobus, owc_gpiobus_driver, 0, 0);
396b66ed8eeSAndriy Gapon MODULE_DEPEND(owc_gpiobus, ow, 1, 1, 1);
397926c3367SAndriy Gapon MODULE_DEPEND(owc_gpiobus, gpiobus, 1, 1, 1);
398926c3367SAndriy Gapon MODULE_VERSION(owc_gpiobus, 1);
399