xref: /netbsd-src/sys/dev/onewire/oweeprom.c (revision 93fd4b2a993df9ec9701c85a72e7c5614487f014)
1 /*	$NetBSD: oweeprom.c,v 1.1 2020/04/14 13:35:24 macallan Exp $	*/
2 
3 /*-
4  * Copyright (c) 2020 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Michael Lorenz.
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  * 1-Wire EEPROM family type device driver.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: oweeprom.c,v 1.1 2020/04/14 13:35:24 macallan Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/kernel.h>
43 #include <sys/proc.h>
44 
45 #include <dev/onewire/onewiredevs.h>
46 #include <dev/onewire/onewirereg.h>
47 #include <dev/onewire/onewirevar.h>
48 
49 #define EEPROM_CMD_WRITE_SCRATCHPAD	0x0f
50 #define EEPROM_CMD_READ_SCRATCHPAD	0xaa
51 #define EEPROM_CMD_COPY_SCRATCHPAD	0x55
52 #define EEPROM_CMD_READ_MEMORY		0xf0
53 #define EEPROM_CMD_WRITE_APPREG		0x99
54 #define EEPROM_CMD_READ_STATUS		0x66
55 #define EEPROM_CMD_READ_APPREG		0xc3
56 #define EEPROM_CMD_COPY_LOCK_APPREG	0x5a
57 
58 struct oweeprom_softc {
59 	device_t			sc_dev;
60 	void				*sc_onewire;
61 	u_int64_t			sc_rom;
62 };
63 
64 static int	oweeprom_match(device_t, cfdata_t, void *);
65 static void	oweeprom_attach(device_t, device_t, void *);
66 
67 CFATTACH_DECL_NEW(oweeprom, sizeof(struct oweeprom_softc),
68 	oweeprom_match, oweeprom_attach, NULL, NULL);
69 
70 static const struct onewire_matchfam oweeprom_fams[] = {
71 	{ ONEWIRE_FAMILY_DS2430 },
72 };
73 
74 static int
oweeprom_match(device_t parent,cfdata_t match,void * aux)75 oweeprom_match(device_t parent, cfdata_t match, void *aux)
76 {
77 	return (onewire_matchbyfam(aux, oweeprom_fams,
78 	    __arraycount(oweeprom_fams)));
79 }
80 
81 static void
oweeprom_attach(device_t parent,device_t self,void * aux)82 oweeprom_attach(device_t parent, device_t self, void *aux)
83 {
84 	struct oweeprom_softc *sc = device_private(self);
85 	struct onewire_attach_args *oa = aux;
86 	int i, j;
87 	uint8_t data[32];
88 
89 	aprint_naive("\n");
90 
91 	sc->sc_dev = self;
92 	sc->sc_onewire = oa->oa_onewire;
93 	sc->sc_rom = oa->oa_rom;
94 
95 	aprint_normal("\n");
96 
97 	onewire_lock(sc->sc_onewire);
98 	if (onewire_reset(sc->sc_onewire) != 0) {
99 		printf("reset failrd\n");
100 		return;
101 	}
102 
103 	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
104 	onewire_write_byte(sc->sc_onewire, EEPROM_CMD_READ_MEMORY);
105 	onewire_write_byte(sc->sc_onewire, 0);
106 	onewire_read_block(sc->sc_onewire, data, 32);
107 	printf("EEPROM\n");
108 	for (i = 0; i < 32; i += 8) {
109 		printf("%02x:", i);
110 		for (j = 0; j < 8; j++)
111 			printf(" %02x", data[i + j]);
112 		printf("\n");
113 	}
114 
115 	if (onewire_reset(sc->sc_onewire) != 0) {
116 		printf("reset failrd\n");
117 		return;
118 	}
119 
120 	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
121 	onewire_write_byte(sc->sc_onewire, EEPROM_CMD_READ_APPREG);
122 	onewire_write_byte(sc->sc_onewire, 0);
123 	onewire_read_block(sc->sc_onewire, data, 8);
124 	printf("Application register\n");
125 	for (j = 0; j < 8; j++)
126 		printf(" %02x", data[j]);
127 	printf("\n");
128 
129 	if (onewire_reset(sc->sc_onewire) != 0) {
130 		printf("reset failrd\n");
131 		return;
132 	}
133 
134 	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
135 	onewire_write_byte(sc->sc_onewire, EEPROM_CMD_READ_STATUS);
136 	onewire_write_byte(sc->sc_onewire, 0);
137 	onewire_read_block(sc->sc_onewire, data, 1);
138 	printf("Status register %02x\n", data[0]);
139 
140 	onewire_unlock(sc->sc_onewire);
141 }
142