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