Home | History | Annotate | Line # | Download | only in ingenic
      1  1.3  macallan /*	$NetBSD: ingenic_efuse.c,v 1.3 2015/10/14 15:44:57 macallan Exp $ */
      2  1.1  macallan 
      3  1.1  macallan /*-
      4  1.1  macallan  * Copyright (c) 2015 Michael Lorenz
      5  1.1  macallan  * All rights reserved.
      6  1.1  macallan  *
      7  1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8  1.1  macallan  * modification, are permitted provided that the following conditions
      9  1.1  macallan  * are met:
     10  1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11  1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12  1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15  1.1  macallan  *
     16  1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  macallan  */
     28  1.1  macallan 
     29  1.3  macallan /*
     30  1.3  macallan  * a driver for the 'EFUSE Slave Interface' found on JZ4780
     31  1.3  macallan  * more or less 8kBit of non-volatile storage containing things like MAC
     32  1.3  macallan  * address, various encryption keys, boot code, serial numbers and parameters.
     33  1.3  macallan  * Using it only to get the MAC address for now.
     34  1.3  macallan  */
     35  1.3  macallan 
     36  1.1  macallan #include <sys/cdefs.h>
     37  1.3  macallan __KERNEL_RCSID(0, "$NetBSD: ingenic_efuse.c,v 1.3 2015/10/14 15:44:57 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/mutex.h>
     43  1.1  macallan #include <sys/bus.h>
     44  1.1  macallan 
     45  1.1  macallan #include <mips/ingenic/ingenic_var.h>
     46  1.1  macallan #include <mips/ingenic/ingenic_regs.h>
     47  1.1  macallan 
     48  1.1  macallan #include "opt_ingenic.h"
     49  1.1  macallan 
     50  1.1  macallan static int ingenic_efuse_match(device_t, struct cfdata *, void *);
     51  1.1  macallan static void ingenic_efuse_attach(device_t, device_t, void *);
     52  1.1  macallan 
     53  1.1  macallan struct efuse_softc {
     54  1.1  macallan 	device_t		sc_dev;
     55  1.1  macallan 	bus_space_tag_t		sc_iot;
     56  1.1  macallan 	bus_space_handle_t	sc_ioh;
     57  1.1  macallan 	uint8_t			sc_data[0x20];
     58  1.1  macallan };
     59  1.1  macallan 
     60  1.1  macallan static void ingenic_efuse_read(struct efuse_softc *, int, int, uint8_t *);
     61  1.1  macallan 
     62  1.1  macallan void ingenic_set_enaddr(uint8_t *);
     63  1.1  macallan 
     64  1.1  macallan CFATTACH_DECL_NEW(ingenic_efuse, sizeof(struct efuse_softc),
     65  1.1  macallan     ingenic_efuse_match, ingenic_efuse_attach, NULL, NULL);
     66  1.1  macallan 
     67  1.1  macallan /* ARGSUSED */
     68  1.1  macallan static int
     69  1.1  macallan ingenic_efuse_match(device_t parent, struct cfdata *match, void *aux)
     70  1.1  macallan {
     71  1.1  macallan 	struct apbus_attach_args *aa = aux;
     72  1.1  macallan 
     73  1.1  macallan 	if (strcmp(aa->aa_name, "efuse") != 0)
     74  1.1  macallan 		return 0;
     75  1.1  macallan 
     76  1.1  macallan 	return 1;
     77  1.1  macallan }
     78  1.1  macallan 
     79  1.1  macallan /* ARGSUSED */
     80  1.1  macallan static void
     81  1.1  macallan ingenic_efuse_attach(device_t parent, device_t self, void *aux)
     82  1.1  macallan {
     83  1.1  macallan 	struct efuse_softc *sc = device_private(self);
     84  1.1  macallan 	struct apbus_attach_args *aa = aux;
     85  1.1  macallan 	int error;
     86  1.1  macallan 
     87  1.1  macallan 	sc->sc_dev = self;
     88  1.1  macallan 
     89  1.1  macallan 	sc->sc_iot = aa->aa_bst;
     90  1.1  macallan 
     91  1.1  macallan 	if (aa->aa_addr == 0)
     92  1.1  macallan 		aa->aa_addr = JZ_EFUSE;
     93  1.1  macallan 
     94  1.1  macallan 	error = bus_space_map(aa->aa_bst, aa->aa_addr, 0x30, 0, &sc->sc_ioh);
     95  1.1  macallan 	if (error) {
     96  1.1  macallan 		aprint_error_dev(self,
     97  1.1  macallan 		    "can't map registers for %s: %d\n", aa->aa_name, error);
     98  1.1  macallan 		return;
     99  1.1  macallan 	}
    100  1.1  macallan 
    101  1.1  macallan 	aprint_naive(": Ingenic EFUSE Slave Interface\n");
    102  1.1  macallan 	aprint_normal(": Ingenic EFUSE Slave Interface\n");
    103  1.1  macallan 	ingenic_efuse_read(sc, 8, 0x20, sc->sc_data);
    104  1.1  macallan 	ingenic_set_enaddr(&sc->sc_data[0x1a]);
    105  1.1  macallan #ifdef INGENIC_DEBUG
    106  1.1  macallan 	{
    107  1.2  macallan 		int i, j;
    108  1.1  macallan 		for (i = 0; i < 0x20; 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", sc->sc_data[i + j]);
    112  1.1  macallan 			printf("\n");
    113  1.1  macallan 		}
    114  1.1  macallan 	}
    115  1.1  macallan #endif
    116  1.1  macallan }
    117  1.1  macallan 
    118  1.1  macallan static void
    119  1.1  macallan ingenic_efuse_read(struct efuse_softc *sc, int addr, int len, uint8_t *buf)
    120  1.1  macallan {
    121  1.1  macallan 	uint32_t abuf;
    122  1.1  macallan 	int i;
    123  1.1  macallan 
    124  1.3  macallan 	/* default, just in case */
    125  1.1  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, JZ_EFUCFG, 0x00040000);
    126  1.1  macallan 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, JZ_EFUCTRL,
    127  1.1  macallan 		JZ_EFUSE_READ |
    128  1.1  macallan 		(addr << JZ_EFUSE_ADDR_SHIFT) |
    129  1.1  macallan 		((len - 1) << JZ_EFUSE_SIZE_SHIFT));
    130  1.1  macallan 	do {} while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, JZ_EFUSTATE) &
    131  1.1  macallan 		JZ_EFUSE_RD_DONE) == 0);
    132  1.1  macallan 	for (i = 0; i < len; i += 4) {
    133  1.1  macallan 		abuf = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    134  1.1  macallan 		    JZ_EFUDATA0 + i);
    135  1.1  macallan 		memcpy(buf, &abuf, 4);
    136  1.1  macallan 		buf += 4;
    137  1.1  macallan 	}
    138  1.3  macallan }
    139