xref: /netbsd-src/sys/dev/ic/ds.h (revision 1d7f24ead849f206da1037b3b802e9b5f002b711)
1*1d7f24eaSmatt /*	$NetBSD: ds.h,v 1.10 2012/02/12 16:34:11 matt Exp $	*/
23040e924Sis 
33040e924Sis /*-
42c7fa23cSis  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5dc068e0dSis  * All rights reserved.
6dc068e0dSis  *
7dc068e0dSis  * This code is derived from software contributed to The NetBSD Foundation
8dc068e0dSis  * by Ignatios Souvatzis.
93040e924Sis  *
103040e924Sis  * Redistribution and use in source and binary forms, with or without
113040e924Sis  * modification, are permitted provided that the following conditions
123040e924Sis  * are met:
133040e924Sis  * 1. Redistributions of source code must retain the above copyright
143040e924Sis  *    notice, this list of conditions and the following disclaimer.
153040e924Sis  * 2. Redistributions in binary form must reproduce the above copyright
163040e924Sis  *    notice, this list of conditions and the following disclaimer in the
173040e924Sis  *    documentation and/or other materials provided with the distribution.
183040e924Sis  *
19dc068e0dSis  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20dc068e0dSis  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21dc068e0dSis  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22dc068e0dSis  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23dc068e0dSis  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24dc068e0dSis  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25dc068e0dSis  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26dc068e0dSis  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27dc068e0dSis  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28dc068e0dSis  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29dc068e0dSis  * POSSIBILITY OF SUCH DAMAGE.
303040e924Sis  */
313040e924Sis 
323040e924Sis /*
333040e924Sis  * Definitions for access to Dallas Semiconductor chips which attach to
343040e924Sis  * the same 1-wire bus as the DS2404 RTC.
353040e924Sis  */
363040e924Sis 
373040e924Sis #ifndef DALLAS_SEMI_CHIPS_H
383040e924Sis #define DALLAS_SEMI_CHIPS_H
393040e924Sis 
403040e924Sis /* Family codes (low byte of the ROM) */
413040e924Sis 
42a90a7a1aSbjh21 #define DS_FAMILY_2401	0x01	/* DS2401 Silicon Serial Number */
433040e924Sis #define DS_FAMILY_2404	0x04	/* DS2404 Econoram Time Chip */
443040e924Sis 
453040e924Sis /*
463040e924Sis  * ROM access codes. These are only available from the 1-wire bus, and one
473040e924Sis  * of them MUST be used before a memory access code is called. If you want
483040e924Sis  * to detect which devices are on the bus, you have to issue the ROM search
493040e924Sis  * function (see data sheet).
503040e924Sis  * If only one device is on the BUS, and you don't want any ROM function,
513040e924Sis  * issue the SKIP function.
523040e924Sis  * READ ROM works only if only one device is on the bus.
533040e924Sis  */
543040e924Sis 
553040e924Sis #define DS_ROM_MATCH		0x55	/* 55 8-bytes-of-ROM-to-select */
563040e924Sis #define DS_ROM_SEARCH		0xf0	/* see data sheet */
573040e924Sis #define DS_ROM_SKIP		0xCC	/* don't do ROM function */
583040e924Sis #define DS_ROM_READ		0x33	/* 33 -> 8 bytes of ROM */
593040e924Sis 
603040e924Sis /*
613040e924Sis  * Memory access codes. These are available from the 1- or 3-wire bus, and
623040e924Sis  * but you must use one of the ROM access codes first, if using the 1-wire
633040e924Sis  * bus.
643040e924Sis  *
653040e924Sis  * You can read from any starting address up to the end of the chip, or
663040e924Sis  * abort the read with a reset pulse.
673040e924Sis  * You first write 2-32 bytes beginning some address to the scratchpad.
683040e924Sis  * Starting address and final byte stream length are remembered by the
693040e924Sis  * chip. After reading data and address/length back from the scratchpad,
703040e924Sis  * and verifying the information, you can issue the copy scratchpad command
713040e924Sis  * to copy the written parts of the scratchpad to the corresponding parts
723040e924Sis  * of the implied (in the address) memory page.
733040e924Sis  */
743040e924Sis 
753040e924Sis #define DS_MEM_WRITE_SCRATCH	0x0f	/* 0F low-ads high-ads data ... */
763040e924Sis #define DS_MEM_READ_SCRATCH	0xaa	/* AA -> low-ads high-ads end-ofs
773040e924Sis 					 * data ... */
783040e924Sis #define DS_MEM_COPY_SCRATCH	0x55	/* 55 low-ads high-ads end-ofs */
793040e924Sis #define DS_MEM_READ_MEMORY	0xf0	/* F0 low-ads high-ads -> data ...*/
803040e924Sis 
813040e924Sis /*
823040e924Sis  * Hardware handle for access functions
833040e924Sis  */
843040e924Sis 
853040e924Sis struct ds_handle {
8618db93c7Sperry 	int (*ds_read_bit)(void *);
8718db93c7Sperry 	void (*ds_write_bit)(void *, int);
8818db93c7Sperry 	void (*ds_reset)(void *);
893040e924Sis 	void *ds_hw_handle;
903040e924Sis };
913040e924Sis 
923040e924Sis /*
933040e924Sis  * Functions for access to Dallas Semiconductor chips which attach to
943040e924Sis  * the same 1-wire bus as the DS2404 RTC.
953040e924Sis  */
963040e924Sis 
9718db93c7Sperry static u_int8_t ds_read_byte(struct ds_handle *);
9818db93c7Sperry static void ds_write_byte(struct ds_handle *, unsigned int);
993040e924Sis 
100fbae48b9Sperry static __inline u_int8_t
ds_read_byte(struct ds_handle * dsh)101*1d7f24eaSmatt ds_read_byte(struct ds_handle *dsh)
1023040e924Sis {
1033040e924Sis 	u_int8_t buf;
1043040e924Sis 	int i;
1053040e924Sis 
1063040e924Sis 	for (i=buf=0; i<8; ++i)
1073040e924Sis 		buf |= (dsh->ds_read_bit)(dsh->ds_hw_handle) << i;
1083040e924Sis 
1093040e924Sis 	return buf;
1103040e924Sis }
1113040e924Sis 
112fbae48b9Sperry static __inline void
ds_write_byte(struct ds_handle * dsh,unsigned int b)113*1d7f24eaSmatt ds_write_byte(struct ds_handle *dsh, unsigned int b)
1143040e924Sis {
1153040e924Sis 	int i;
1163040e924Sis 
1173040e924Sis 	for (i=0; i<8; ++i) {
1183040e924Sis 		(dsh->ds_write_bit)(dsh->ds_hw_handle, b & 1);
1193040e924Sis 		b >>= 1;
1203040e924Sis 	}
1213040e924Sis }
1223040e924Sis 
1233040e924Sis #endif /* DALLAS_SEMI_CHIPS_H */
124