xref: /openbsd/sys/dev/onewire/onewire_bitbang.c (revision 274d7c50)
1 /*	$OpenBSD: onewire_bitbang.c,v 1.2 2010/07/02 03:13:42 tedu Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * 1-Wire bus bit-banging routines.
21  */
22 
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/device.h>
26 
27 #include <dev/onewire/onewirevar.h>
28 
29 int
30 onewire_bb_reset(const struct onewire_bbops *ops, void *arg)
31 {
32 	int s, rv, i;
33 
34 	s = splhigh();
35 	ops->bb_tx(arg);
36 	ops->bb_set(arg, 0);
37 	DELAY(480);
38 	ops->bb_set(arg, 1);
39 	ops->bb_rx(arg);
40 	DELAY(30);
41 	for (i = 0; i < 6; i++) {
42 		if ((rv = ops->bb_get(arg)) == 0)
43 			break;
44 		DELAY(20);
45 	}
46 	DELAY(450);
47 	splx(s);
48 
49 	return (rv);
50 }
51 
52 int
53 onewire_bb_bit(const struct onewire_bbops *ops, void *arg, int value)
54 {
55 	int s, rv, i;
56 
57 	s = splhigh();
58 	ops->bb_tx(arg);
59 	ops->bb_set(arg, 0);
60 	DELAY(2);
61 	rv = 0;
62 	if (value) {
63 		ops->bb_set(arg, 1);
64 		ops->bb_rx(arg);
65 		for (i = 0; i < 15; i++) {
66 			if ((rv = ops->bb_get(arg)) == 0)
67 				break;
68 			DELAY(2);
69 		}
70 		ops->bb_tx(arg);
71 	}
72 	DELAY(60);
73 	ops->bb_set(arg, 1);
74 	DELAY(5);
75 	splx(s);
76 
77 	return (rv);
78 }
79