]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/allwinner/aw_sid.c
Import bhyve_graphics into CURRENT. Thanks to all who tested
[FreeBSD/FreeBSD.git] / sys / arm / allwinner / aw_sid.c
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 /*
30  * Allwinner secure ID controller
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/rman.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <machine/bus.h>
43
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46
47 #include <arm/allwinner/aw_sid.h>
48
49 #define SID_SRAM                0x200
50 #define SID_THERMAL_CALIB0      (SID_SRAM + 0x34)
51 #define SID_THERMAL_CALIB1      (SID_SRAM + 0x38)
52
53 static struct ofw_compat_data compat_data[] = {
54         { "allwinner,sun8i-a83t-sid",           1 },
55         { NULL,                                 0 }
56 };
57
58 struct aw_sid_softc {
59         struct resource         *res;
60 };
61
62 static struct aw_sid_softc *aw_sid_sc;
63
64 static struct resource_spec aw_sid_spec[] = {
65         { SYS_RES_MEMORY,       0,      RF_ACTIVE },
66         { -1, 0 }
67 };
68
69 #define RD4(sc, reg)            bus_read_4((sc)->res, (reg))
70 #define WR4(sc, reg, val)       bus_write_4((sc)->res, (reg), (val))
71
72 static int
73 aw_sid_probe(device_t dev)
74 {
75         if (!ofw_bus_status_okay(dev))
76                 return (ENXIO);
77
78         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
79                 return (ENXIO);
80
81         device_set_desc(dev, "Allwinner Secure ID Controller");
82         return (BUS_PROBE_DEFAULT);
83 }
84
85 static int
86 aw_sid_attach(device_t dev)
87 {
88         struct aw_sid_softc *sc;
89
90         sc = device_get_softc(dev);
91
92         if (bus_alloc_resources(dev, aw_sid_spec, &sc->res) != 0) {
93                 device_printf(dev, "cannot allocate resources for device\n");
94                 return (ENXIO);
95         }
96
97         aw_sid_sc = sc;
98
99         return (0);
100 }
101
102 int
103 aw_sid_read_tscalib(uint32_t *calib0, uint32_t *calib1)
104 {
105         struct aw_sid_softc *sc;
106
107         sc = aw_sid_sc;
108         if (sc == NULL)
109                 return (ENXIO);
110
111         *calib0 = RD4(sc, SID_THERMAL_CALIB0);
112         *calib1 = RD4(sc, SID_THERMAL_CALIB1);
113
114         return (0);
115 }
116
117 static device_method_t aw_sid_methods[] = {
118         /* Device interface */
119         DEVMETHOD(device_probe,         aw_sid_probe),
120         DEVMETHOD(device_attach,        aw_sid_attach),
121
122         DEVMETHOD_END
123 };
124
125 static driver_t aw_sid_driver = {
126         "aw_sid",
127         aw_sid_methods,
128         sizeof(struct aw_sid_softc),
129 };
130
131 static devclass_t aw_sid_devclass;
132
133 EARLY_DRIVER_MODULE(aw_sid, simplebus, aw_sid_driver, aw_sid_devclass, 0, 0,
134     BUS_PASS_RESOURCE + BUS_PASS_ORDER_FIRST);
135 MODULE_VERSION(aw_sid, 1);