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