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