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