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