oweeprom.c revision 1.1.2.2 1 1.1.2.2 bouyer /* $NetBSD: oweeprom.c,v 1.1.2.2 2020/04/20 11:29:04 bouyer Exp $ */
2 1.1.2.2 bouyer
3 1.1.2.2 bouyer /*-
4 1.1.2.2 bouyer * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.1.2.2 bouyer * All rights reserved.
6 1.1.2.2 bouyer *
7 1.1.2.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 bouyer * by Michael Lorenz.
9 1.1.2.2 bouyer *
10 1.1.2.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 bouyer * modification, are permitted provided that the following conditions
12 1.1.2.2 bouyer * are met:
13 1.1.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.1.2.2 bouyer *
19 1.1.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.2 bouyer */
31 1.1.2.2 bouyer
32 1.1.2.2 bouyer /*
33 1.1.2.2 bouyer * 1-Wire EEPROM family type device driver.
34 1.1.2.2 bouyer */
35 1.1.2.2 bouyer
36 1.1.2.2 bouyer #include <sys/cdefs.h>
37 1.1.2.2 bouyer __KERNEL_RCSID(0, "$NetBSD: oweeprom.c,v 1.1.2.2 2020/04/20 11:29:04 bouyer Exp $");
38 1.1.2.2 bouyer
39 1.1.2.2 bouyer #include <sys/param.h>
40 1.1.2.2 bouyer #include <sys/systm.h>
41 1.1.2.2 bouyer #include <sys/device.h>
42 1.1.2.2 bouyer #include <sys/kernel.h>
43 1.1.2.2 bouyer #include <sys/proc.h>
44 1.1.2.2 bouyer
45 1.1.2.2 bouyer #include <dev/onewire/onewiredevs.h>
46 1.1.2.2 bouyer #include <dev/onewire/onewirereg.h>
47 1.1.2.2 bouyer #include <dev/onewire/onewirevar.h>
48 1.1.2.2 bouyer
49 1.1.2.2 bouyer #define EEPROM_CMD_WRITE_SCRATCHPAD 0x0f
50 1.1.2.2 bouyer #define EEPROM_CMD_READ_SCRATCHPAD 0xaa
51 1.1.2.2 bouyer #define EEPROM_CMD_COPY_SCRATCHPAD 0x55
52 1.1.2.2 bouyer #define EEPROM_CMD_READ_MEMORY 0xf0
53 1.1.2.2 bouyer #define EEPROM_CMD_WRITE_APPREG 0x99
54 1.1.2.2 bouyer #define EEPROM_CMD_READ_STATUS 0x66
55 1.1.2.2 bouyer #define EEPROM_CMD_READ_APPREG 0xc3
56 1.1.2.2 bouyer #define EEPROM_CMD_COPY_LOCK_APPREG 0x5a
57 1.1.2.2 bouyer
58 1.1.2.2 bouyer struct oweeprom_softc {
59 1.1.2.2 bouyer device_t sc_dev;
60 1.1.2.2 bouyer void *sc_onewire;
61 1.1.2.2 bouyer u_int64_t sc_rom;
62 1.1.2.2 bouyer };
63 1.1.2.2 bouyer
64 1.1.2.2 bouyer static int oweeprom_match(device_t, cfdata_t, void *);
65 1.1.2.2 bouyer static void oweeprom_attach(device_t, device_t, void *);
66 1.1.2.2 bouyer
67 1.1.2.2 bouyer CFATTACH_DECL_NEW(oweeprom, sizeof(struct oweeprom_softc),
68 1.1.2.2 bouyer oweeprom_match, oweeprom_attach, NULL, NULL);
69 1.1.2.2 bouyer
70 1.1.2.2 bouyer static const struct onewire_matchfam oweeprom_fams[] = {
71 1.1.2.2 bouyer { ONEWIRE_FAMILY_DS2430 },
72 1.1.2.2 bouyer };
73 1.1.2.2 bouyer
74 1.1.2.2 bouyer static int
75 1.1.2.2 bouyer oweeprom_match(device_t parent, cfdata_t match, void *aux)
76 1.1.2.2 bouyer {
77 1.1.2.2 bouyer return (onewire_matchbyfam(aux, oweeprom_fams,
78 1.1.2.2 bouyer __arraycount(oweeprom_fams)));
79 1.1.2.2 bouyer }
80 1.1.2.2 bouyer
81 1.1.2.2 bouyer static void
82 1.1.2.2 bouyer oweeprom_attach(device_t parent, device_t self, void *aux)
83 1.1.2.2 bouyer {
84 1.1.2.2 bouyer struct oweeprom_softc *sc = device_private(self);
85 1.1.2.2 bouyer struct onewire_attach_args *oa = aux;
86 1.1.2.2 bouyer int i, j;
87 1.1.2.2 bouyer uint8_t data[32];
88 1.1.2.2 bouyer
89 1.1.2.2 bouyer aprint_naive("\n");
90 1.1.2.2 bouyer
91 1.1.2.2 bouyer sc->sc_dev = self;
92 1.1.2.2 bouyer sc->sc_onewire = oa->oa_onewire;
93 1.1.2.2 bouyer sc->sc_rom = oa->oa_rom;
94 1.1.2.2 bouyer
95 1.1.2.2 bouyer aprint_normal("\n");
96 1.1.2.2 bouyer
97 1.1.2.2 bouyer onewire_lock(sc->sc_onewire);
98 1.1.2.2 bouyer if (onewire_reset(sc->sc_onewire) != 0) {
99 1.1.2.2 bouyer printf("reset failrd\n");
100 1.1.2.2 bouyer return;
101 1.1.2.2 bouyer }
102 1.1.2.2 bouyer
103 1.1.2.2 bouyer onewire_matchrom(sc->sc_onewire, sc->sc_rom);
104 1.1.2.2 bouyer onewire_write_byte(sc->sc_onewire, EEPROM_CMD_READ_MEMORY);
105 1.1.2.2 bouyer onewire_write_byte(sc->sc_onewire, 0);
106 1.1.2.2 bouyer onewire_read_block(sc->sc_onewire, data, 32);
107 1.1.2.2 bouyer printf("EEPROM\n");
108 1.1.2.2 bouyer for (i = 0; i < 32; i += 8) {
109 1.1.2.2 bouyer printf("%02x:", i);
110 1.1.2.2 bouyer for (j = 0; j < 8; j++)
111 1.1.2.2 bouyer printf(" %02x", data[i + j]);
112 1.1.2.2 bouyer printf("\n");
113 1.1.2.2 bouyer }
114 1.1.2.2 bouyer
115 1.1.2.2 bouyer if (onewire_reset(sc->sc_onewire) != 0) {
116 1.1.2.2 bouyer printf("reset failrd\n");
117 1.1.2.2 bouyer return;
118 1.1.2.2 bouyer }
119 1.1.2.2 bouyer
120 1.1.2.2 bouyer onewire_matchrom(sc->sc_onewire, sc->sc_rom);
121 1.1.2.2 bouyer onewire_write_byte(sc->sc_onewire, EEPROM_CMD_READ_APPREG);
122 1.1.2.2 bouyer onewire_write_byte(sc->sc_onewire, 0);
123 1.1.2.2 bouyer onewire_read_block(sc->sc_onewire, data, 8);
124 1.1.2.2 bouyer printf("Application register\n");
125 1.1.2.2 bouyer for (j = 0; j < 8; j++)
126 1.1.2.2 bouyer printf(" %02x", data[j]);
127 1.1.2.2 bouyer printf("\n");
128 1.1.2.2 bouyer
129 1.1.2.2 bouyer if (onewire_reset(sc->sc_onewire) != 0) {
130 1.1.2.2 bouyer printf("reset failrd\n");
131 1.1.2.2 bouyer return;
132 1.1.2.2 bouyer }
133 1.1.2.2 bouyer
134 1.1.2.2 bouyer onewire_matchrom(sc->sc_onewire, sc->sc_rom);
135 1.1.2.2 bouyer onewire_write_byte(sc->sc_onewire, EEPROM_CMD_READ_STATUS);
136 1.1.2.2 bouyer onewire_write_byte(sc->sc_onewire, 0);
137 1.1.2.2 bouyer onewire_read_block(sc->sc_onewire, data, 1);
138 1.1.2.2 bouyer printf("Status register %02x\n", data[0]);
139 1.1.2.2 bouyer
140 1.1.2.2 bouyer onewire_unlock(sc->sc_onewire);
141 1.1.2.2 bouyer }
142