xref: /netbsd-src/sys/arch/mips/ingenic/ingenic_efuse.c (revision 6c39fa1448f7f8f225339264bb2ef5867dea163f)
1*6c39fa14Smacallan /*	$NetBSD: ingenic_efuse.c,v 1.3 2015/10/14 15:44:57 macallan Exp $ */
256acaa06Smacallan 
356acaa06Smacallan /*-
456acaa06Smacallan  * Copyright (c) 2015 Michael Lorenz
556acaa06Smacallan  * All rights reserved.
656acaa06Smacallan  *
756acaa06Smacallan  * Redistribution and use in source and binary forms, with or without
856acaa06Smacallan  * modification, are permitted provided that the following conditions
956acaa06Smacallan  * are met:
1056acaa06Smacallan  * 1. Redistributions of source code must retain the above copyright
1156acaa06Smacallan  *    notice, this list of conditions and the following disclaimer.
1256acaa06Smacallan  * 2. Redistributions in binary form must reproduce the above copyright
1356acaa06Smacallan  *    notice, this list of conditions and the following disclaimer in the
1456acaa06Smacallan  *    documentation and/or other materials provided with the distribution.
1556acaa06Smacallan  *
1656acaa06Smacallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1756acaa06Smacallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1856acaa06Smacallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1956acaa06Smacallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2056acaa06Smacallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2156acaa06Smacallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2256acaa06Smacallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2356acaa06Smacallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2456acaa06Smacallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2556acaa06Smacallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2656acaa06Smacallan  * POSSIBILITY OF SUCH DAMAGE.
2756acaa06Smacallan  */
2856acaa06Smacallan 
29*6c39fa14Smacallan /*
30*6c39fa14Smacallan  * a driver for the 'EFUSE Slave Interface' found on JZ4780
31*6c39fa14Smacallan  * more or less 8kBit of non-volatile storage containing things like MAC
32*6c39fa14Smacallan  * address, various encryption keys, boot code, serial numbers and parameters.
33*6c39fa14Smacallan  * Using it only to get the MAC address for now.
34*6c39fa14Smacallan  */
35*6c39fa14Smacallan 
3656acaa06Smacallan #include <sys/cdefs.h>
37*6c39fa14Smacallan __KERNEL_RCSID(0, "$NetBSD: ingenic_efuse.c,v 1.3 2015/10/14 15:44:57 macallan Exp $");
3856acaa06Smacallan 
3956acaa06Smacallan #include <sys/param.h>
4056acaa06Smacallan #include <sys/systm.h>
4156acaa06Smacallan #include <sys/device.h>
4256acaa06Smacallan #include <sys/mutex.h>
4356acaa06Smacallan #include <sys/bus.h>
4456acaa06Smacallan 
4556acaa06Smacallan #include <mips/ingenic/ingenic_var.h>
4656acaa06Smacallan #include <mips/ingenic/ingenic_regs.h>
4756acaa06Smacallan 
4856acaa06Smacallan #include "opt_ingenic.h"
4956acaa06Smacallan 
5056acaa06Smacallan static int ingenic_efuse_match(device_t, struct cfdata *, void *);
5156acaa06Smacallan static void ingenic_efuse_attach(device_t, device_t, void *);
5256acaa06Smacallan 
5356acaa06Smacallan struct efuse_softc {
5456acaa06Smacallan 	device_t		sc_dev;
5556acaa06Smacallan 	bus_space_tag_t		sc_iot;
5656acaa06Smacallan 	bus_space_handle_t	sc_ioh;
5756acaa06Smacallan 	uint8_t			sc_data[0x20];
5856acaa06Smacallan };
5956acaa06Smacallan 
6056acaa06Smacallan static void ingenic_efuse_read(struct efuse_softc *, int, int, uint8_t *);
6156acaa06Smacallan 
6256acaa06Smacallan void ingenic_set_enaddr(uint8_t *);
6356acaa06Smacallan 
6456acaa06Smacallan CFATTACH_DECL_NEW(ingenic_efuse, sizeof(struct efuse_softc),
6556acaa06Smacallan     ingenic_efuse_match, ingenic_efuse_attach, NULL, NULL);
6656acaa06Smacallan 
6756acaa06Smacallan /* ARGSUSED */
6856acaa06Smacallan static int
ingenic_efuse_match(device_t parent,struct cfdata * match,void * aux)6956acaa06Smacallan ingenic_efuse_match(device_t parent, struct cfdata *match, void *aux)
7056acaa06Smacallan {
7156acaa06Smacallan 	struct apbus_attach_args *aa = aux;
7256acaa06Smacallan 
7356acaa06Smacallan 	if (strcmp(aa->aa_name, "efuse") != 0)
7456acaa06Smacallan 		return 0;
7556acaa06Smacallan 
7656acaa06Smacallan 	return 1;
7756acaa06Smacallan }
7856acaa06Smacallan 
7956acaa06Smacallan /* ARGSUSED */
8056acaa06Smacallan static void
ingenic_efuse_attach(device_t parent,device_t self,void * aux)8156acaa06Smacallan ingenic_efuse_attach(device_t parent, device_t self, void *aux)
8256acaa06Smacallan {
8356acaa06Smacallan 	struct efuse_softc *sc = device_private(self);
8456acaa06Smacallan 	struct apbus_attach_args *aa = aux;
8556acaa06Smacallan 	int error;
8656acaa06Smacallan 
8756acaa06Smacallan 	sc->sc_dev = self;
8856acaa06Smacallan 
8956acaa06Smacallan 	sc->sc_iot = aa->aa_bst;
9056acaa06Smacallan 
9156acaa06Smacallan 	if (aa->aa_addr == 0)
9256acaa06Smacallan 		aa->aa_addr = JZ_EFUSE;
9356acaa06Smacallan 
9456acaa06Smacallan 	error = bus_space_map(aa->aa_bst, aa->aa_addr, 0x30, 0, &sc->sc_ioh);
9556acaa06Smacallan 	if (error) {
9656acaa06Smacallan 		aprint_error_dev(self,
9756acaa06Smacallan 		    "can't map registers for %s: %d\n", aa->aa_name, error);
9856acaa06Smacallan 		return;
9956acaa06Smacallan 	}
10056acaa06Smacallan 
10156acaa06Smacallan 	aprint_naive(": Ingenic EFUSE Slave Interface\n");
10256acaa06Smacallan 	aprint_normal(": Ingenic EFUSE Slave Interface\n");
10356acaa06Smacallan 	ingenic_efuse_read(sc, 8, 0x20, sc->sc_data);
10456acaa06Smacallan 	ingenic_set_enaddr(&sc->sc_data[0x1a]);
10556acaa06Smacallan #ifdef INGENIC_DEBUG
10656acaa06Smacallan 	{
107eaf48229Smacallan 		int i, j;
10856acaa06Smacallan 		for (i = 0; i < 0x20; i += 8) {
10956acaa06Smacallan 			printf("%02x:", i);
11056acaa06Smacallan 			for (j = 0; j < 8; j++)
11156acaa06Smacallan 				printf(" %02x", sc->sc_data[i + j]);
11256acaa06Smacallan 			printf("\n");
11356acaa06Smacallan 		}
11456acaa06Smacallan 	}
11556acaa06Smacallan #endif
11656acaa06Smacallan }
11756acaa06Smacallan 
11856acaa06Smacallan static void
ingenic_efuse_read(struct efuse_softc * sc,int addr,int len,uint8_t * buf)11956acaa06Smacallan ingenic_efuse_read(struct efuse_softc *sc, int addr, int len, uint8_t *buf)
12056acaa06Smacallan {
12156acaa06Smacallan 	uint32_t abuf;
12256acaa06Smacallan 	int i;
12356acaa06Smacallan 
124*6c39fa14Smacallan 	/* default, just in case */
12556acaa06Smacallan 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, JZ_EFUCFG, 0x00040000);
12656acaa06Smacallan 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, JZ_EFUCTRL,
12756acaa06Smacallan 		JZ_EFUSE_READ |
12856acaa06Smacallan 		(addr << JZ_EFUSE_ADDR_SHIFT) |
12956acaa06Smacallan 		((len - 1) << JZ_EFUSE_SIZE_SHIFT));
13056acaa06Smacallan 	do {} while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, JZ_EFUSTATE) &
13156acaa06Smacallan 		JZ_EFUSE_RD_DONE) == 0);
13256acaa06Smacallan 	for (i = 0; i < len; i += 4) {
13356acaa06Smacallan 		abuf = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
13456acaa06Smacallan 		    JZ_EFUDATA0 + i);
13556acaa06Smacallan 		memcpy(buf, &abuf, 4);
13656acaa06Smacallan 		buf += 4;
13756acaa06Smacallan 	}
13856acaa06Smacallan }
139