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