xref: /freebsd/sys/arm/ti/am335x/am335x_scm.c (revision 076ad2f836d5f49dc1375f1677335a48fe0d4b82)
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 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/sysctl.h>
36 
37 #include <machine/bus.h>
38 
39 #include <arm/ti/am335x/am335x_scm.h>
40 #include <arm/ti/ti_cpuid.h>
41 #include <arm/ti/ti_scm.h>
42 
43 #define	TZ_ZEROC	2731
44 
45 struct am335x_scm_softc {
46 	int			sc_last_temp;
47 	struct sysctl_oid	*sc_temp_oid;
48 };
49 
50 static int
51 am335x_scm_temp_sysctl(SYSCTL_HANDLER_ARGS)
52 {
53 	device_t dev;
54 	int i, temp;
55 	struct am335x_scm_softc *sc;
56 	uint32_t reg;
57 
58 	dev = (device_t)arg1;
59 	sc = device_get_softc(dev);
60 
61 	/* Read the temperature and convert to Kelvin. */
62 	for(i = 50; i > 0; i--) {
63 		ti_scm_reg_read_4(SCM_BGAP_CTRL, &reg);
64 		if ((reg & SCM_BGAP_EOCZ) == 0)
65 			break;
66 		DELAY(50);
67 	}
68 	if ((reg & SCM_BGAP_EOCZ) == 0) {
69 		sc->sc_last_temp =
70 		    (reg >> SCM_BGAP_TEMP_SHIFT) & SCM_BGAP_TEMP_MASK;
71 		sc->sc_last_temp *= 10;
72 	}
73 	temp = sc->sc_last_temp + TZ_ZEROC;
74 
75 	return (sysctl_handle_int(oidp, &temp, 0, req));
76 }
77 
78 static void
79 am335x_scm_identify(driver_t *driver, device_t parent)
80 {
81 	device_t child;
82 
83 	/* AM335x only. */
84 	if (ti_chip() != CHIP_AM335X)
85 		return;
86 
87 	/* Make sure we attach only once. */
88 	if (device_find_child(parent, "am335x_scm", -1) != NULL)
89 		return;
90 
91 	child = device_add_child(parent, "am335x_scm", -1);
92 	if (child == NULL)
93 		device_printf(parent, "cannot add ti_scm child\n");
94 }
95 
96 static int
97 am335x_scm_probe(device_t dev)
98 {
99 
100 	device_set_desc(dev, "AM335x Control Module Extension");
101 
102 	return (BUS_PROBE_DEFAULT);
103 }
104 
105 static int
106 am335x_scm_attach(device_t dev)
107 {
108 	struct am335x_scm_softc *sc;
109 	struct sysctl_ctx_list *ctx;
110 	struct sysctl_oid_list *tree;
111 	uint32_t reg;
112 
113 	/* Reset the digital outputs. */
114 	ti_scm_reg_write_4(SCM_BGAP_CTRL, 0);
115 	ti_scm_reg_read_4(SCM_BGAP_CTRL, &reg);
116 	DELAY(500);
117 	/* Set continous mode. */
118 	ti_scm_reg_write_4(SCM_BGAP_CTRL, SCM_BGAP_CONTCONV);
119 	ti_scm_reg_read_4(SCM_BGAP_CTRL, &reg);
120 	DELAY(500);
121 	/* Start the ADC conversion. */
122 	reg = SCM_BGAP_CLRZ | SCM_BGAP_CONTCONV | SCM_BGAP_SOC;
123 	ti_scm_reg_write_4(SCM_BGAP_CTRL, reg);
124 
125 	/* Temperature sysctl. */
126 	sc = device_get_softc(dev);
127         ctx = device_get_sysctl_ctx(dev);
128 	tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
129 	sc->sc_temp_oid = SYSCTL_ADD_PROC(ctx, tree, OID_AUTO,
130 	    "temperature", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
131 	    dev, 0, am335x_scm_temp_sysctl, "IK", "Current temperature");
132 
133 	return (0);
134 }
135 
136 static int
137 am335x_scm_detach(device_t dev)
138 {
139 	struct am335x_scm_softc *sc;
140 
141 	sc = device_get_softc(dev);
142 
143 	/* Remove temperature sysctl. */
144 	if (sc->sc_temp_oid != NULL)
145 		sysctl_remove_oid(sc->sc_temp_oid, 1, 0);
146 
147 	/* Stop the bandgap ADC. */
148 	ti_scm_reg_write_4(SCM_BGAP_CTRL, SCM_BGAP_BGOFF);
149 
150 	return (0);
151 }
152 
153 static device_method_t am335x_scm_methods[] = {
154 	DEVMETHOD(device_identify,	am335x_scm_identify),
155 	DEVMETHOD(device_probe,		am335x_scm_probe),
156 	DEVMETHOD(device_attach,	am335x_scm_attach),
157 	DEVMETHOD(device_detach,	am335x_scm_detach),
158 
159 	DEVMETHOD_END
160 };
161 
162 static driver_t am335x_scm_driver = {
163 	"am335x_scm",
164 	am335x_scm_methods,
165 	sizeof(struct am335x_scm_softc),
166 };
167 
168 static devclass_t am335x_scm_devclass;
169 
170 DRIVER_MODULE(am335x_scm, ti_scm, am335x_scm_driver, am335x_scm_devclass, 0, 0);
171 MODULE_VERSION(am335x_scm, 1);
172 MODULE_DEPEND(am335x_scm, ti_scm, 1, 1, 1);
173