summaryrefslogtreecommitdiff
path: root/sys/dev/onewire/oweeprom.c
blob: d4083e1f8218e326e3eec0020c0d91c9d2e374ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*	$NetBSD: oweeprom.c,v 1.1 2020/04/14 13:35:24 macallan Exp $	*/

/*-
 * Copyright (c) 2020 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Michael Lorenz.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * 1-Wire EEPROM family type device driver.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: oweeprom.c,v 1.1 2020/04/14 13:35:24 macallan Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/proc.h>

#include <dev/onewire/onewiredevs.h>
#include <dev/onewire/onewirereg.h>
#include <dev/onewire/onewirevar.h>

#define EEPROM_CMD_WRITE_SCRATCHPAD	0x0f
#define EEPROM_CMD_READ_SCRATCHPAD	0xaa
#define EEPROM_CMD_COPY_SCRATCHPAD	0x55
#define EEPROM_CMD_READ_MEMORY		0xf0
#define EEPROM_CMD_WRITE_APPREG		0x99
#define EEPROM_CMD_READ_STATUS		0x66
#define EEPROM_CMD_READ_APPREG		0xc3
#define EEPROM_CMD_COPY_LOCK_APPREG	0x5a

struct oweeprom_softc {
	device_t			sc_dev;
	void				*sc_onewire;
	u_int64_t			sc_rom;
};

static int	oweeprom_match(device_t, cfdata_t, void *);
static void	oweeprom_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(oweeprom, sizeof(struct oweeprom_softc),
	oweeprom_match, oweeprom_attach, NULL, NULL);

static const struct onewire_matchfam oweeprom_fams[] = {
	{ ONEWIRE_FAMILY_DS2430 },
};

static int
oweeprom_match(device_t parent, cfdata_t match, void *aux)
{
	return (onewire_matchbyfam(aux, oweeprom_fams,
	    __arraycount(oweeprom_fams)));
}

static void
oweeprom_attach(device_t parent, device_t self, void *aux)
{
	struct oweeprom_softc *sc = device_private(self);
	struct onewire_attach_args *oa = aux;
	int i, j;
	uint8_t data[32];

	aprint_naive("\n");

	sc->sc_dev = self;
	sc->sc_onewire = oa->oa_onewire;
	sc->sc_rom = oa->oa_rom;

	aprint_normal("\n");

	onewire_lock(sc->sc_onewire);
	if (onewire_reset(sc->sc_onewire) != 0) {
		printf("reset failrd\n");
		return;
	}

	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
	onewire_write_byte(sc->sc_onewire, EEPROM_CMD_READ_MEMORY);
	onewire_write_byte(sc->sc_onewire, 0);
	onewire_read_block(sc->sc_onewire, data, 32);
	printf("EEPROM\n");
	for (i = 0; i < 32; i += 8) {
		printf("%02x:", i);
		for (j = 0; j < 8; j++)
			printf(" %02x", data[i + j]);
		printf("\n");
	}

	if (onewire_reset(sc->sc_onewire) != 0) {
		printf("reset failrd\n");
		return;
	}

	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
	onewire_write_byte(sc->sc_onewire, EEPROM_CMD_READ_APPREG);
	onewire_write_byte(sc->sc_onewire, 0);
	onewire_read_block(sc->sc_onewire, data, 8);
	printf("Application register\n");
	for (j = 0; j < 8; j++)
		printf(" %02x", data[j]);
	printf("\n");

	if (onewire_reset(sc->sc_onewire) != 0) {
		printf("reset failrd\n");
		return;
	}

	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
	onewire_write_byte(sc->sc_onewire, EEPROM_CMD_READ_STATUS);
	onewire_write_byte(sc->sc_onewire, 0);
	onewire_read_block(sc->sc_onewire, data, 1);
	printf("Status register %02x\n", data[0]);

	onewire_unlock(sc->sc_onewire);
}