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