ingenic_efuse.c revision 1.2 1 /* $NetBSD: ingenic_efuse.c,v 1.2 2015/10/08 18:20:31 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 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ingenic_efuse.c,v 1.2 2015/10/08 18:20:31 macallan Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/mutex.h>
36 #include <sys/bus.h>
37
38 #include <mips/ingenic/ingenic_var.h>
39 #include <mips/ingenic/ingenic_regs.h>
40
41 #include "opt_ingenic.h"
42
43 static int ingenic_efuse_match(device_t, struct cfdata *, void *);
44 static void ingenic_efuse_attach(device_t, device_t, void *);
45
46 struct efuse_softc {
47 device_t sc_dev;
48 bus_space_tag_t sc_iot;
49 bus_space_handle_t sc_ioh;
50 uint8_t sc_data[0x20];
51 };
52
53 static void ingenic_efuse_read(struct efuse_softc *, int, int, uint8_t *);
54
55 void ingenic_set_enaddr(uint8_t *);
56
57 CFATTACH_DECL_NEW(ingenic_efuse, sizeof(struct efuse_softc),
58 ingenic_efuse_match, ingenic_efuse_attach, NULL, NULL);
59
60 /* ARGSUSED */
61 static int
62 ingenic_efuse_match(device_t parent, struct cfdata *match, void *aux)
63 {
64 struct apbus_attach_args *aa = aux;
65
66 if (strcmp(aa->aa_name, "efuse") != 0)
67 return 0;
68
69 return 1;
70 }
71
72 /* ARGSUSED */
73 static void
74 ingenic_efuse_attach(device_t parent, device_t self, void *aux)
75 {
76 struct efuse_softc *sc = device_private(self);
77 struct apbus_attach_args *aa = aux;
78 int error;
79
80 sc->sc_dev = self;
81
82 sc->sc_iot = aa->aa_bst;
83
84 if (aa->aa_addr == 0)
85 aa->aa_addr = JZ_EFUSE;
86
87 error = bus_space_map(aa->aa_bst, aa->aa_addr, 0x30, 0, &sc->sc_ioh);
88 if (error) {
89 aprint_error_dev(self,
90 "can't map registers for %s: %d\n", aa->aa_name, error);
91 return;
92 }
93
94 aprint_naive(": Ingenic EFUSE Slave Interface\n");
95 aprint_normal(": Ingenic EFUSE Slave Interface\n");
96 ingenic_efuse_read(sc, 8, 0x20, sc->sc_data);
97 ingenic_set_enaddr(&sc->sc_data[0x1a]);
98 #ifdef INGENIC_DEBUG
99 {
100 int i, j;
101 for (i = 0; i < 0x20; i += 8) {
102 printf("%02x:", i);
103 for (j = 0; j < 8; j++)
104 printf(" %02x", sc->sc_data[i + j]);
105 printf("\n");
106 }
107 }
108 #endif
109 }
110
111 static void
112 ingenic_efuse_read(struct efuse_softc *sc, int addr, int len, uint8_t *buf)
113 {
114 uint32_t abuf;
115 int i;
116
117 bus_space_write_4(sc->sc_iot, sc->sc_ioh, JZ_EFUCFG, 0x00040000);
118 bus_space_write_4(sc->sc_iot, sc->sc_ioh, JZ_EFUCTRL,
119 JZ_EFUSE_READ |
120 (addr << JZ_EFUSE_ADDR_SHIFT) |
121 ((len - 1) << JZ_EFUSE_SIZE_SHIFT));
122 do {} while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, JZ_EFUSTATE) &
123 JZ_EFUSE_RD_DONE) == 0);
124 for (i = 0; i < len; i += 4) {
125 abuf = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
126 JZ_EFUDATA0 + i);
127 memcpy(buf, &abuf, 4);
128 buf += 4;
129 }
130 }