1 /*	$OpenBSD: ds.h,v 1.2 2002/03/14 01:26:54 millert Exp $	*/
2 /*	$NetBSD: ds.h,v 1.1 1997/07/06 22:22:01 is Exp $	*/
3 
4 /*-
5  * Copyright (c) 1997 Ignatios Souvatzis. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the author nor the names of contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Definitions for access to Dallas Semiconductor chips which attach to
34  * the same 1-wire bus as the DS2404 RTC.
35  */
36 
37 #ifndef DALLAS_SEMI_CHIPS_H
38 #define DALLAS_SEMI_CHIPS_H
39 
40 /* Family codes (low byte of the ROM) */
41 
42 #define DS_FAMILY_2404	0x04	/* DS2404 Econoram Time Chip */
43 
44 /*
45  * ROM access codes. These are only available from the 1-wire bus, and one
46  * of them MUST be used before a memory access code is called. If you want
47  * to detect which devices are on the bus, you have to issue the ROM search
48  * function (see data sheet).
49  * If only one device is on the BUS, and you don't want any ROM function,
50  * issue the SKIP function.
51  * READ ROM works only if only one device is on the bus.
52  */
53 
54 #define DS_ROM_MATCH		0x55	/* 55 8-bytes-of-ROM-to-select */
55 #define DS_ROM_SEARCH		0xf0	/* see data sheet */
56 #define DS_ROM_SKIP		0xCC	/* don't do ROM function */
57 #define DS_ROM_READ		0x33	/* 33 -> 8 bytes of ROM */
58 
59 /*
60  * Memory access codes. These are available from the 1- or 3-wire bus, and
61  * but you must use one of the ROM access codes first, if using the 1-wire
62  * bus.
63  *
64  * You can read from any starting address up to the end of the chip, or
65  * abort the read with a reset pulse.
66  * You first write 2-32 bytes beginning some address to the scratchpad.
67  * Starting address and final byte stream length are remembered by the
68  * chip. After reading data and address/length back from the scratchpad,
69  * and verifying the information, you can issue the copy scratchpad command
70  * to copy the written parts of the scratchpad to the corresponding parts
71  * of the implied (in the address) memory page.
72  */
73 
74 #define DS_MEM_WRITE_SCRATCH	0x0f	/* 0F low-ads high-ads data ... */
75 #define DS_MEM_READ_SCRATCH	0xaa	/* AA -> low-ads high-ads end-ofs
76 					 * data ... */
77 #define DS_MEM_COPY_SCRATCH	0x55	/* 55 low-ads high-ads end-ofs */
78 #define DS_MEM_READ_MEMORY	0xf0	/* F0 low-ads high-ads -> data ...*/
79 
80 /*
81  * Hardware handle for access functions
82  */
83 
84 struct ds_handle {
85 	int (*ds_read_bit)(void *);
86 	void (*ds_write_bit)(void *, int);
87 	void (*ds_reset)(void *);
88 	void *ds_hw_handle;
89 };
90 
91 /*
92  * Functions for access to Dallas Semiconductor chips which attach to
93  * the same 1-wire bus as the DS2404 RTC.
94  */
95 
96 static __inline u_int8_t ds_read_byte(struct ds_handle *);
97 static __inline void ds_write_byte(struct ds_handle *, unsigned int);
98 
99 static __inline u_int8_t
ds_read_byte(dsh)100 ds_read_byte(dsh)
101 	struct ds_handle *dsh;
102 {
103 	u_int8_t buf;
104 	int i;
105 
106 	for (i = buf = 0; i < 8; ++i)
107 		buf |= (dsh->ds_read_bit)(dsh->ds_hw_handle) << i;
108 
109 	return buf;
110 }
111 
112 static __inline void
ds_write_byte(dsh,b)113 ds_write_byte(dsh, b)
114 	struct ds_handle *dsh;
115 	unsigned int b;
116 {
117 	int i;
118 
119 	for (i = 0; i < 8; ++i) {
120 		(dsh->ds_write_bit)(dsh->ds_hw_handle, b & 1);
121 		b >>= 1;
122 	}
123 }
124 
125 #endif /* DALLAS_SEMI_CHIPS_H */
126