2 * Copyright (c) 2016 Rubicon Communications, LLC (Netgate)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/sysctl.h>
37 #include <machine/bus.h>
39 #include <arm/ti/am335x/am335x_scm.h>
40 #include <arm/ti/ti_cpuid.h>
41 #include <arm/ti/ti_scm.h>
45 struct am335x_scm_softc {
47 struct sysctl_oid *sc_temp_oid;
51 am335x_scm_temp_sysctl(SYSCTL_HANDLER_ARGS)
55 struct am335x_scm_softc *sc;
59 sc = device_get_softc(dev);
61 /* Read the temperature and convert to Kelvin. */
62 for(i = 50; i > 0; i--) {
63 ti_scm_reg_read_4(SCM_BGAP_CTRL, ®);
64 if ((reg & SCM_BGAP_EOCZ) == 0)
68 if ((reg & SCM_BGAP_EOCZ) == 0) {
70 (reg >> SCM_BGAP_TEMP_SHIFT) & SCM_BGAP_TEMP_MASK;
71 sc->sc_last_temp *= 10;
73 temp = sc->sc_last_temp + TZ_ZEROC;
75 return (sysctl_handle_int(oidp, &temp, 0, req));
79 am335x_scm_identify(driver_t *driver, device_t parent)
84 if (ti_chip() != CHIP_AM335X)
87 /* Make sure we attach only once. */
88 if (device_find_child(parent, "am335x_scm", -1) != NULL)
91 child = device_add_child(parent, "am335x_scm", -1);
93 device_printf(parent, "cannot add ti_scm child\n");
97 am335x_scm_probe(device_t dev)
100 device_set_desc(dev, "AM335x Control Module Extension");
102 return (BUS_PROBE_DEFAULT);
106 am335x_scm_attach(device_t dev)
108 struct am335x_scm_softc *sc;
109 struct sysctl_ctx_list *ctx;
110 struct sysctl_oid_list *tree;
113 /* Reset the digital outputs. */
114 ti_scm_reg_write_4(SCM_BGAP_CTRL, 0);
115 ti_scm_reg_read_4(SCM_BGAP_CTRL, ®);
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, ®);
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);
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");
137 am335x_scm_detach(device_t dev)
139 struct am335x_scm_softc *sc;
141 sc = device_get_softc(dev);
143 /* Remove temperature sysctl. */
144 if (sc->sc_temp_oid != NULL)
145 sysctl_remove_oid(sc->sc_temp_oid, 1, 0);
147 /* Stop the bandgap ADC. */
148 ti_scm_reg_write_4(SCM_BGAP_CTRL, SCM_BGAP_BGOFF);
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),
162 static driver_t am335x_scm_driver = {
165 sizeof(struct am335x_scm_softc),
168 static devclass_t am335x_scm_devclass;
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);