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