xref: /freebsd/sys/arm/ti/am335x/am335x_scm.c (revision da5432eda807c4b7232d030d5157d5b417ea4f52)
1 /*-
2  * Copyright (c) 2016 Rubicon Communications, LLC (Netgate)
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 AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, 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 
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/sysctl.h>
34 
35 #include <machine/bus.h>
36 
37 #include <arm/ti/am335x/am335x_scm.h>
38 #include <arm/ti/ti_cpuid.h>
39 #include <arm/ti/ti_scm.h>
40 
41 #include <dev/extres/syscon/syscon.h>
42 #include "syscon_if.h"
43 
44 #define	TZ_ZEROC	2731
45 
46 struct am335x_scm_softc {
47 	int			sc_last_temp;
48 	struct sysctl_oid	*sc_temp_oid;
49 	struct syscon		*syscon;
50 };
51 
52 static int
53 am335x_scm_temp_sysctl(SYSCTL_HANDLER_ARGS)
54 {
55 	device_t dev;
56 	int i, temp;
57 	struct am335x_scm_softc *sc;
58 	uint32_t reg;
59 
60 	dev = (device_t)arg1;
61 	sc = device_get_softc(dev);
62 
63 	/* Read the temperature and convert to Kelvin. */
64 	for(i = 50; i > 0; i--) {
65 		reg = SYSCON_READ_4(sc->syscon, SCM_BGAP_CTRL);
66 		if ((reg & SCM_BGAP_EOCZ) == 0)
67 			break;
68 		DELAY(50);
69 	}
70 	if ((reg & SCM_BGAP_EOCZ) == 0) {
71 		sc->sc_last_temp =
72 		    (reg >> SCM_BGAP_TEMP_SHIFT) & SCM_BGAP_TEMP_MASK;
73 		sc->sc_last_temp *= 10;
74 	}
75 	temp = sc->sc_last_temp + TZ_ZEROC;
76 
77 	return (sysctl_handle_int(oidp, &temp, 0, req));
78 }
79 
80 static void
81 am335x_scm_identify(driver_t *driver, device_t parent)
82 {
83 	device_t child;
84 
85 	/* AM335x only. */
86 	if (ti_chip() != CHIP_AM335X)
87 		return;
88 
89 	/* Make sure we attach only once. */
90 	if (device_find_child(parent, "am335x_scm", -1) != NULL)
91 		return;
92 
93 	child = device_add_child(parent, "am335x_scm", -1);
94 	if (child == NULL)
95 		device_printf(parent, "cannot add ti_scm child\n");
96 }
97 
98 static int
99 am335x_scm_probe(device_t dev)
100 {
101 	/* Just allow the first one */
102 	if (strcmp(device_get_nameunit(dev), "am335x_scm0") != 0)
103 		return (ENXIO);
104 
105 	device_set_desc(dev, "AM335x Control Module Extension");
106 
107 	return (BUS_PROBE_DEFAULT);
108 }
109 
110 static int
111 am335x_scm_attach(device_t dev)
112 {
113 	struct am335x_scm_softc *sc;
114 	struct sysctl_ctx_list *ctx;
115 	struct sysctl_oid_list *tree;
116 	uint32_t reg;
117 	phandle_t opp_table;
118 	int err;
119 
120 	sc = device_get_softc(dev);
121 
122 	/* FIXME: For now; Go and kidnap syscon from opp-table */
123 	opp_table = OF_finddevice("/opp-table");
124 	if (opp_table == -1) {
125 		device_printf(dev, "Cant find /opp-table\n");
126 		return (ENXIO);
127 	}
128 	if (!OF_hasprop(opp_table, "syscon")) {
129 		device_printf(dev, "/opp-table missing syscon property\n");
130 		return (ENXIO);
131 	}
132 	err = syscon_get_by_ofw_property(dev, opp_table, "syscon", &sc->syscon);
133 	if (err) {
134 		device_printf(dev, "Failed to get syscon\n");
135 		return (ENXIO);
136 	}
137 
138 	/* Reset the digital outputs. */
139 	SYSCON_WRITE_4(sc->syscon, SCM_BGAP_CTRL, 0);
140 	reg = SYSCON_READ_4(sc->syscon, SCM_BGAP_CTRL);
141 	DELAY(500);
142 	/* Set continuous mode. */
143 	SYSCON_WRITE_4(sc->syscon, SCM_BGAP_CTRL, SCM_BGAP_CONTCONV);
144 	reg = SYSCON_READ_4(sc->syscon, SCM_BGAP_CTRL);
145 	DELAY(500);
146 	/* Start the ADC conversion. */
147 	reg = SCM_BGAP_CLRZ | SCM_BGAP_CONTCONV | SCM_BGAP_SOC;
148 	SYSCON_WRITE_4(sc->syscon, SCM_BGAP_CTRL, reg);
149 
150 	/* Temperature sysctl. */
151         ctx = device_get_sysctl_ctx(dev);
152 	tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
153 	sc->sc_temp_oid = SYSCTL_ADD_PROC(ctx, tree, OID_AUTO,
154 	    "temperature", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
155 	    dev, 0, am335x_scm_temp_sysctl, "IK", "Current temperature");
156 
157 	return (0);
158 }
159 
160 static int
161 am335x_scm_detach(device_t dev)
162 {
163 	struct am335x_scm_softc *sc;
164 
165 	sc = device_get_softc(dev);
166 
167 	/* Remove temperature sysctl. */
168 	if (sc->sc_temp_oid != NULL)
169 		sysctl_remove_oid(sc->sc_temp_oid, 1, 0);
170 
171 	/* Stop the bandgap ADC. */
172 	SYSCON_WRITE_4(sc->syscon, SCM_BGAP_CTRL, SCM_BGAP_BGOFF);
173 
174 	return (0);
175 }
176 
177 static device_method_t am335x_scm_methods[] = {
178 	DEVMETHOD(device_identify,	am335x_scm_identify),
179 	DEVMETHOD(device_probe,		am335x_scm_probe),
180 	DEVMETHOD(device_attach,	am335x_scm_attach),
181 	DEVMETHOD(device_detach,	am335x_scm_detach),
182 
183 	DEVMETHOD_END
184 };
185 
186 static driver_t am335x_scm_driver = {
187 	"am335x_scm",
188 	am335x_scm_methods,
189 	sizeof(struct am335x_scm_softc),
190 };
191 
192 DRIVER_MODULE(am335x_scm, ti_scm, am335x_scm_driver, 0, 0);
193 MODULE_VERSION(am335x_scm, 1);
194 MODULE_DEPEND(am335x_scm, ti_scm_syscon, 1, 1, 1);
195