am335x_scm.c revision 310856
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: stable/11/sys/arm/ti/am335x/am335x_scm.c 310856 2016-12-30 20:28:07Z loos $");
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
45struct am335x_scm_softc {
46	int			sc_last_temp;
47	struct sysctl_oid	*sc_temp_oid;
48};
49
50static int
51am335x_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
78static void
79am335x_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
96static int
97am335x_scm_probe(device_t dev)
98{
99
100	device_set_desc(dev, "AM335x Control Module Extension");
101
102	return (BUS_PROBE_DEFAULT);
103}
104
105static int
106am335x_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	/* Set ADC to continous mode, clear output reset. */
114	reg = SCM_BGAP_CLRZ | SCM_BGAP_CONTCONV;
115	ti_scm_reg_write_4(SCM_BGAP_CTRL, reg);
116	/* Flush write. */
117	ti_scm_reg_read_4(SCM_BGAP_CTRL, &reg);
118	/* Start the ADC conversion. */
119	reg = SCM_BGAP_CLRZ | SCM_BGAP_CONTCONV | SCM_BGAP_SOC;
120	ti_scm_reg_write_4(SCM_BGAP_CTRL, reg);
121
122	/* Temperature sysctl. */
123	sc = device_get_softc(dev);
124        ctx = device_get_sysctl_ctx(dev);
125	tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
126	sc->sc_temp_oid = SYSCTL_ADD_PROC(ctx, tree, OID_AUTO,
127	    "temperature", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
128	    dev, 0, am335x_scm_temp_sysctl, "IK", "Current temperature");
129
130	return (0);
131}
132
133static int
134am335x_scm_detach(device_t dev)
135{
136	struct am335x_scm_softc *sc;
137
138	sc = device_get_softc(dev);
139
140	/* Remove temperature sysctl. */
141	if (sc->sc_temp_oid != NULL)
142		sysctl_remove_oid(sc->sc_temp_oid, 1, 0);
143
144	/* Stop the bandgap ADC. */
145	ti_scm_reg_write_4(SCM_BGAP_CTRL, SCM_BGAP_BGOFF);
146
147	return (0);
148}
149
150static device_method_t am335x_scm_methods[] = {
151	DEVMETHOD(device_identify,	am335x_scm_identify),
152	DEVMETHOD(device_probe,		am335x_scm_probe),
153	DEVMETHOD(device_attach,	am335x_scm_attach),
154	DEVMETHOD(device_detach,	am335x_scm_detach),
155
156	DEVMETHOD_END
157};
158
159static driver_t am335x_scm_driver = {
160	"am335x_scm",
161	am335x_scm_methods,
162	sizeof(struct am335x_scm_softc),
163};
164
165static devclass_t am335x_scm_devclass;
166
167DRIVER_MODULE(am335x_scm, ti_scm, am335x_scm_driver, am335x_scm_devclass, 0, 0);
168MODULE_VERSION(am335x_scm, 1);
169MODULE_DEPEND(am335x_scm, ti_scm, 1, 1, 1);
170