xref: /freebsd-src/sys/dev/ow/owc_gpiobus.c (revision 3ddaf8200bc90b1410755ebac7b5c979ea90a2f6)
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
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
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 	 */
1365b56413dSWarner Losh 	device_add_child(sc->sc_dev, "ow", DEVICE_UNIT_ANY);
13734f5de82SJohn Baldwin 	bus_delayed_attach_children(dev);
13834f5de82SJohn Baldwin 	return (0);
139ae1f3df4SWarner Losh }
140ae1f3df4SWarner Losh 
141ae1f3df4SWarner Losh static int
142ae1f3df4SWarner Losh owc_gpiobus_detach(device_t dev)
143ae1f3df4SWarner Losh {
144ae1f3df4SWarner Losh 	struct owc_gpiobus_softc *sc;
145c7b0edf2SIan Lepore 	int err;
146ae1f3df4SWarner Losh 
147ae1f3df4SWarner Losh 	sc = device_get_softc(dev);
148c7b0edf2SIan Lepore 
149*3ddaf820SJohn Baldwin 	if ((err = bus_generic_detach(dev)) != 0)
150c7b0edf2SIan Lepore 		return (err);
151c7b0edf2SIan Lepore 
152c7b0edf2SIan Lepore 	gpio_pin_release(sc->sc_pin);
153ae1f3df4SWarner Losh 	OWC_GPIOBUS_LOCK_DESTROY(sc);
154c7b0edf2SIan Lepore 
155ae1f3df4SWarner Losh 	return (0);
156ae1f3df4SWarner Losh }
157ae1f3df4SWarner Losh 
158ae1f3df4SWarner Losh /*
159ae1f3df4SWarner Losh  * In the diagrams below, R is driven by the resistor pullup, M is driven by the
160ae1f3df4SWarner Losh  * master, and S is driven by the slave / target.
161ae1f3df4SWarner Losh  */
162ae1f3df4SWarner Losh 
163ae1f3df4SWarner Losh /*
164ae1f3df4SWarner Losh  * These macros let what why we're doing stuff shine in the code
165ae1f3df4SWarner Losh  * below, and let the how be confined to here.
166ae1f3df4SWarner Losh  */
167c7b0edf2SIan Lepore #define OUTPIN(sc)	gpio_pin_setflags((sc)->sc_pin, GPIO_PIN_OUTPUT)
168c7b0edf2SIan Lepore #define INPIN(sc)	gpio_pin_setflags((sc)->sc_pin, GPIO_PIN_INPUT)
169c7b0edf2SIan Lepore #define GETPIN(sc, bp)	gpio_pin_is_active((sc)->sc_pin, (bp))
170c7b0edf2SIan Lepore #define LOW(sc)		gpio_pin_set_active((sc)->sc_pin, false)
171ae1f3df4SWarner Losh 
172ae1f3df4SWarner Losh /*
173ae1f3df4SWarner Losh  * WRITE-ONE (see owll_if.m for timings) From Figure 4-1 AN-937
174ae1f3df4SWarner Losh  *
175ae1f3df4SWarner Losh  *		       |<---------tSLOT---->|<-tREC->|
176ae1f3df4SWarner Losh  *	High	RRRRM  | 	RRRRRRRRRRRR|RRRRRRRRM
177ae1f3df4SWarner Losh  *		     M |       R |     |  |	      M
178ae1f3df4SWarner Losh  *		      M|      R	 |     |  |	       M
179ae1f3df4SWarner Losh  *	Low	       MMMMMMM	 |     |  |    	        MMMMMM...
180ae1f3df4SWarner Losh  *		       |<-tLOW1->|     |  |
181ae1f3df4SWarner Losh  *		       |<------15us--->|  |
182ae1f3df4SWarner Losh  *                     |<--------60us---->|
183ae1f3df4SWarner Losh  */
184ae1f3df4SWarner Losh static int
185ae1f3df4SWarner Losh owc_gpiobus_write_one(device_t dev, struct ow_timing *t)
186ae1f3df4SWarner Losh {
187ae1f3df4SWarner Losh 	struct owc_gpiobus_softc *sc;
188ae1f3df4SWarner Losh 
189ae1f3df4SWarner Losh 	sc = device_get_softc(dev);
190ae1f3df4SWarner Losh 
191ae1f3df4SWarner Losh 	critical_enter();
192ae1f3df4SWarner Losh 
193ae1f3df4SWarner Losh 	/* Force low */
194ae1f3df4SWarner Losh 	OUTPIN(sc);
195ae1f3df4SWarner Losh 	LOW(sc);
196ae1f3df4SWarner Losh 	DELAY(t->t_low1);
197ae1f3df4SWarner Losh 
198ae1f3df4SWarner Losh 	/* Allow resistor to float line high */
199ae1f3df4SWarner Losh 	INPIN(sc);
200ae1f3df4SWarner Losh 	DELAY(t->t_slot - t->t_low1 + t->t_rec);
201ae1f3df4SWarner Losh 
202ae1f3df4SWarner Losh 	critical_exit();
203ae1f3df4SWarner Losh 
20401f1fff0SAndriy Gapon 	return (0);
205ae1f3df4SWarner Losh }
206ae1f3df4SWarner Losh 
207ae1f3df4SWarner Losh /*
208ae1f3df4SWarner Losh  * WRITE-ZERO (see owll_if.m for timings) From Figure 4-2 AN-937
209ae1f3df4SWarner Losh  *
210ae1f3df4SWarner Losh  *		       |<---------tSLOT------>|<-tREC->|
211ae1f3df4SWarner Losh  *	High	RRRRM  | 	            | |RRRRRRRM
212ae1f3df4SWarner Losh  *		     M |                    | R	       M
213ae1f3df4SWarner Losh  *		      M|       	 |     |    |R 	        M
214ae1f3df4SWarner Losh  *	Low	       MMMMMMMMMMMMMMMMMMMMMR  	         MMMMMM...
215ae1f3df4SWarner Losh  *     	       	       |<--15us->|     |    |
216ae1f3df4SWarner Losh  *     	       	       |<------60us--->|    |
217ae1f3df4SWarner Losh  *                     |<-------tLOW0------>|
218ae1f3df4SWarner Losh  */
219ae1f3df4SWarner Losh static int
220ae1f3df4SWarner Losh owc_gpiobus_write_zero(device_t dev, struct ow_timing *t)
221ae1f3df4SWarner Losh {
222ae1f3df4SWarner Losh 	struct owc_gpiobus_softc *sc;
223ae1f3df4SWarner Losh 
224ae1f3df4SWarner Losh 	sc = device_get_softc(dev);
225ae1f3df4SWarner Losh 
226ae1f3df4SWarner Losh 	critical_enter();
227ae1f3df4SWarner Losh 
228ae1f3df4SWarner Losh 	/* Force low */
229ae1f3df4SWarner Losh 	OUTPIN(sc);
230ae1f3df4SWarner Losh 	LOW(sc);
231ae1f3df4SWarner Losh 	DELAY(t->t_low0);
232ae1f3df4SWarner Losh 
233ae1f3df4SWarner Losh 	/* Allow resistor to float line high */
234ae1f3df4SWarner Losh 	INPIN(sc);
235ae1f3df4SWarner Losh 	DELAY(t->t_slot - t->t_low0 + t->t_rec);
236ae1f3df4SWarner Losh 
237ae1f3df4SWarner Losh 	critical_exit();
238ae1f3df4SWarner Losh 
23901f1fff0SAndriy Gapon 	return (0);
240ae1f3df4SWarner Losh }
241ae1f3df4SWarner Losh 
242ae1f3df4SWarner Losh /*
243ae1f3df4SWarner Losh  * READ-DATA (see owll_if.m for timings) From Figure 4-3 AN-937
244ae1f3df4SWarner Losh  *
245ae1f3df4SWarner Losh  *		       |<---------tSLOT------>|<-tREC->|
246ae1f3df4SWarner Losh  *	High	RRRRM  |        rrrrrrrrrrrrrrrRRRRRRRM
247ae1f3df4SWarner Losh  *		     M |       r            | R	       M
248ae1f3df4SWarner Losh  *		      M|      r	        |   |R 	        M
249ae1f3df4SWarner Losh  *	Low	       MMMMMMMSSSSSSSSSSSSSSR  	         MMMMMM...
250ae1f3df4SWarner Losh  *     	       	       |<tLOWR>< sample	>   |
251ae1f3df4SWarner Losh  *     	       	       |<------tRDV---->|   |
252ae1f3df4SWarner Losh  *                                    ->|   |<-tRELEASE
253ae1f3df4SWarner Losh  *
254ae1f3df4SWarner Losh  * r -- allowed to pull high via the resitor when slave writes a 1-bit
255ae1f3df4SWarner Losh  *
256ae1f3df4SWarner Losh  */
257ae1f3df4SWarner Losh static int
258ae1f3df4SWarner Losh owc_gpiobus_read_data(device_t dev, struct ow_timing *t, int *bit)
259ae1f3df4SWarner Losh {
260ae1f3df4SWarner Losh 	struct owc_gpiobus_softc *sc;
261c7b0edf2SIan Lepore 	bool sample;
262ae1f3df4SWarner Losh 	sbintime_t then, now;
263ae1f3df4SWarner Losh 
264ae1f3df4SWarner Losh 	sc = device_get_softc(dev);
265ae1f3df4SWarner Losh 
266c1750b82SAndriy Gapon 	critical_enter();
267c1750b82SAndriy Gapon 
268ae1f3df4SWarner Losh 	/* Force low for t_lowr microseconds */
269ae1f3df4SWarner Losh 	then = sbinuptime();
270ae1f3df4SWarner Losh 	OUTPIN(sc);
271ae1f3df4SWarner Losh 	LOW(sc);
272ae1f3df4SWarner Losh 	DELAY(t->t_lowr);
273ae1f3df4SWarner Losh 
274ae1f3df4SWarner Losh 	/*
275ae1f3df4SWarner Losh 	 * Slave is supposed to hold the line low for t_rdv microseconds for 0
276ae1f3df4SWarner Losh 	 * and immediately float it high for a 1. This is measured from the
277ae1f3df4SWarner Losh 	 * master's pushing the line low.
278ae1f3df4SWarner Losh 	 */
279ae1f3df4SWarner Losh 	INPIN(sc);
280ae1f3df4SWarner Losh 	do {
281ae1f3df4SWarner Losh 		now = sbinuptime();
282ae1f3df4SWarner Losh 		GETPIN(sc, &sample);
283c7b0edf2SIan Lepore 	} while (now - then < (t->t_rdv + 2) * SBT_1US && sample == false);
284ae1f3df4SWarner Losh 	critical_exit();
285ae1f3df4SWarner Losh 
286bb7b803bSAndriy Gapon 	if (now - then < t->t_rdv * SBT_1US)
287ae1f3df4SWarner Losh 		*bit = 1;
288ae1f3df4SWarner Losh 	else
289ae1f3df4SWarner Losh 		*bit = 0;
290ae1f3df4SWarner Losh 
291ae1f3df4SWarner Losh 	/* Wait out the rest of t_slot */
292ae1f3df4SWarner Losh 	do {
293ae1f3df4SWarner Losh 		now = sbinuptime();
294b8c776baSAndriy Gapon 	} while (now - then < (t->t_slot + t->t_rec) * SBT_1US);
295ae1f3df4SWarner Losh 
296c7b0edf2SIan Lepore 	return (0);
297ae1f3df4SWarner Losh }
298ae1f3df4SWarner Losh 
299ae1f3df4SWarner Losh /*
300ae1f3df4SWarner Losh  * RESET AND PRESENCE PULSE (see owll_if.m for timings) From Figure 4-4 AN-937
301ae1f3df4SWarner Losh  *
302ae1f3df4SWarner Losh  *				    |<---------tRSTH------------>|
303ae1f3df4SWarner Losh  *	High RRRM  |		  | RRRRRRRS	       |  RRRR RRM
304ae1f3df4SWarner Losh  *		 M |		  |R|  	   |S  	       | R	  M
305ae1f3df4SWarner Losh  *		  M|		  R |	   | S	       |R	   M
306ae1f3df4SWarner Losh  *	Low	   MMMMMMMM MMMMMM| |	   |  SSSSSSSSSS	    MMMMMM
307ae1f3df4SWarner Losh  *     	       	   |<----tRSTL--->| |  	   |<-tPDL---->|
308ae1f3df4SWarner Losh  *		   |   	       	->| |<-tR  |	       |
309ae1f3df4SWarner Losh  *				    |<tPDH>|
310ae1f3df4SWarner Losh  *
311ae1f3df4SWarner Losh  * Note: for Regular Speed operations, tRSTL + tR should be less than 960us to
312c7b0edf2SIan Lepore  * avoid interfering with other devices on the bus.
313c7b0edf2SIan Lepore  *
314c7b0edf2SIan Lepore  * Return values in *bit:
315c7b0edf2SIan Lepore  *  -1 = Bus wiring error (stuck low).
316c7b0edf2SIan Lepore  *   0 = no presence pulse
317c7b0edf2SIan Lepore  *   1 = presence pulse detected
318ae1f3df4SWarner Losh  */
319ae1f3df4SWarner Losh static int
320ae1f3df4SWarner Losh owc_gpiobus_reset_and_presence(device_t dev, struct ow_timing *t, int *bit)
321ae1f3df4SWarner Losh {
322ae1f3df4SWarner Losh 	struct owc_gpiobus_softc *sc;
323c7b0edf2SIan Lepore 	bool sample;
324ae1f3df4SWarner Losh 
325ae1f3df4SWarner Losh 	sc = device_get_softc(dev);
326ae1f3df4SWarner Losh 
327ae1f3df4SWarner Losh 	/*
328ae1f3df4SWarner Losh 	 * Read the current state of the bus. The steady state of an idle bus is
329ae1f3df4SWarner Losh 	 * high. Badly wired buses that are missing the required pull up, or
330ae1f3df4SWarner Losh 	 * that have a short circuit to ground cause all kinds of mischief when
331c7b0edf2SIan Lepore 	 * we try to read them later. Return EIO if the bus is currently low.
332ae1f3df4SWarner Losh 	 */
333ae1f3df4SWarner Losh 	INPIN(sc);
334c7b0edf2SIan Lepore 	GETPIN(sc, &sample);
335c7b0edf2SIan Lepore 	if (sample == false) {
336ae1f3df4SWarner Losh 		*bit = -1;
33701f1fff0SAndriy Gapon 		return (EIO);
338ae1f3df4SWarner Losh 	}
339ae1f3df4SWarner Losh 
340ae1f3df4SWarner Losh 	critical_enter();
341ae1f3df4SWarner Losh 
342ae1f3df4SWarner Losh 	/* Force low */
343ae1f3df4SWarner Losh 	OUTPIN(sc);
344ae1f3df4SWarner Losh 	LOW(sc);
345ae1f3df4SWarner Losh 	DELAY(t->t_rstl);
346ae1f3df4SWarner Losh 
347ae1f3df4SWarner Losh 	/* Allow resistor to float line high and then wait for reset pulse */
348ae1f3df4SWarner Losh 	INPIN(sc);
349ae1f3df4SWarner Losh 	DELAY(t->t_pdh + t->t_pdl / 2);
350ae1f3df4SWarner Losh 
351ae1f3df4SWarner Losh 	/* Read presence pulse  */
352c7b0edf2SIan Lepore 	GETPIN(sc, &sample);
353c7b0edf2SIan Lepore 	*bit = sample;
354ae1f3df4SWarner Losh 
355ae1f3df4SWarner Losh 	critical_exit();
356ae1f3df4SWarner Losh 
357ae1f3df4SWarner Losh 	DELAY(t->t_rsth - (t->t_pdh + t->t_pdl / 2));	/* Timing not critical for this one */
358ae1f3df4SWarner Losh 
359ae1f3df4SWarner Losh 	/*
360ae1f3df4SWarner Losh 	 * Read the state of the bus after we've waited past the end of the rest
361ae1f3df4SWarner Losh 	 * window. It should return to high. If it is low, then we have some
362ae1f3df4SWarner Losh 	 * problem and should abort the reset.
363ae1f3df4SWarner Losh 	 */
364c7b0edf2SIan Lepore 	GETPIN(sc, &sample);
365c7b0edf2SIan Lepore 	if (sample == false) {
366ae1f3df4SWarner Losh 		*bit = -1;
36701f1fff0SAndriy Gapon 		return (EIO);
368ae1f3df4SWarner Losh 	}
369ae1f3df4SWarner Losh 
37001f1fff0SAndriy Gapon 	return (0);
371ae1f3df4SWarner Losh }
372ae1f3df4SWarner Losh 
373ae1f3df4SWarner Losh static device_method_t owc_gpiobus_methods[] = {
374ae1f3df4SWarner Losh 	/* Device interface */
375ae1f3df4SWarner Losh 	DEVMETHOD(device_probe,		owc_gpiobus_probe),
376ae1f3df4SWarner Losh 	DEVMETHOD(device_attach,	owc_gpiobus_attach),
377ae1f3df4SWarner Losh 	DEVMETHOD(device_detach,	owc_gpiobus_detach),
378ae1f3df4SWarner Losh 
379ae1f3df4SWarner Losh 	DEVMETHOD(owll_write_one,	owc_gpiobus_write_one),
380ae1f3df4SWarner Losh 	DEVMETHOD(owll_write_zero,	owc_gpiobus_write_zero),
381ae1f3df4SWarner Losh 	DEVMETHOD(owll_read_data,	owc_gpiobus_read_data),
382ae1f3df4SWarner Losh 	DEVMETHOD(owll_reset_and_presence,	owc_gpiobus_reset_and_presence),
383ae1f3df4SWarner Losh 	{ 0, 0 }
384ae1f3df4SWarner Losh };
385ae1f3df4SWarner Losh 
386ae1f3df4SWarner Losh static driver_t owc_gpiobus_driver = {
387ae1f3df4SWarner Losh 	"owc",
388ae1f3df4SWarner Losh 	owc_gpiobus_methods,
389ae1f3df4SWarner Losh 	sizeof(struct owc_gpiobus_softc),
390ae1f3df4SWarner Losh };
391ae1f3df4SWarner Losh 
392c7b0edf2SIan Lepore #ifdef FDT
393ffd39155SJohn Baldwin DRIVER_MODULE(owc_gpiobus, simplebus, owc_gpiobus_driver, 0, 0);
394c7b0edf2SIan Lepore #endif
395c7b0edf2SIan Lepore 
396ffd39155SJohn Baldwin DRIVER_MODULE(owc_gpiobus, gpiobus, owc_gpiobus_driver, 0, 0);
397b66ed8eeSAndriy Gapon MODULE_DEPEND(owc_gpiobus, ow, 1, 1, 1);
398926c3367SAndriy Gapon MODULE_DEPEND(owc_gpiobus, gpiobus, 1, 1, 1);
399926c3367SAndriy Gapon MODULE_VERSION(owc_gpiobus, 1);
400