Home | History | Annotate | Line # | Download | only in rpi
rpi_vcmbox.c revision 1.7
      1  1.7       rin /* $NetBSD: rpi_vcmbox.c,v 1.7 2020/12/01 04:14:31 rin Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2013 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill /*
     30  1.1  jmcneill  * Raspberry Pi VC Mailbox Interface
     31  1.1  jmcneill  */
     32  1.1  jmcneill 
     33  1.1  jmcneill #include <sys/cdefs.h>
     34  1.7       rin __KERNEL_RCSID(0, "$NetBSD: rpi_vcmbox.c,v 1.7 2020/12/01 04:14:31 rin Exp $");
     35  1.1  jmcneill 
     36  1.1  jmcneill #include <sys/param.h>
     37  1.1  jmcneill #include <sys/types.h>
     38  1.6       rin #include <sys/bus.h>
     39  1.6       rin #include <sys/conf.h>
     40  1.1  jmcneill #include <sys/device.h>
     41  1.7       rin #include <sys/endian.h>
     42  1.1  jmcneill #include <sys/kmem.h>
     43  1.6       rin #include <sys/systm.h>
     44  1.2  jmcneill #include <sys/sysctl.h>
     45  1.1  jmcneill 
     46  1.1  jmcneill #include <dev/sysmon/sysmonvar.h>
     47  1.1  jmcneill 
     48  1.1  jmcneill #include <arm/broadcom/bcm2835_mbox.h>
     49  1.1  jmcneill 
     50  1.1  jmcneill #include <evbarm/rpi/vcio.h>
     51  1.1  jmcneill #include <evbarm/rpi/vcprop.h>
     52  1.1  jmcneill 
     53  1.1  jmcneill struct vcmbox_temp_request {
     54  1.1  jmcneill 	struct vcprop_buffer_hdr	vb_hdr;
     55  1.1  jmcneill 	struct vcprop_tag_temperature	vbt_temp;
     56  1.1  jmcneill 	struct vcprop_tag end;
     57  1.1  jmcneill } __packed;
     58  1.1  jmcneill 
     59  1.2  jmcneill struct vcmbox_clockrate_request {
     60  1.2  jmcneill 	struct vcprop_buffer_hdr	vb_hdr;
     61  1.2  jmcneill 	struct vcprop_tag_clockrate	vbt_clockrate;
     62  1.2  jmcneill 	struct vcprop_tag end;
     63  1.2  jmcneill } __packed;
     64  1.2  jmcneill 
     65  1.2  jmcneill #define RATE2MHZ(rate)	((rate) / 1000000)
     66  1.2  jmcneill #define MHZ2RATE(mhz)	((mhz) * 1000000)
     67  1.2  jmcneill 
     68  1.1  jmcneill #define VCMBOX_INIT_REQUEST(req)					\
     69  1.1  jmcneill 	do {								\
     70  1.1  jmcneill 		memset(&(req), 0, sizeof((req)));			\
     71  1.7       rin 		(req).vb_hdr.vpb_len = htole32(sizeof((req)));		\
     72  1.7       rin 		(req).vb_hdr.vpb_rcode = htole32(VCPROP_PROCESS_REQUEST);\
     73  1.7       rin 		(req).end.vpt_tag = htole32(VCPROPTAG_NULL);		\
     74  1.1  jmcneill 	} while (0)
     75  1.1  jmcneill #define VCMBOX_INIT_TAG(s, t)						\
     76  1.1  jmcneill 	do {								\
     77  1.7       rin 		(s).tag.vpt_tag = htole32(t);				\
     78  1.7       rin 		(s).tag.vpt_rcode = htole32(VCPROPTAG_REQUEST);		\
     79  1.7       rin 		(s).tag.vpt_len = htole32(VCPROPTAG_LEN(s));		\
     80  1.1  jmcneill 	} while (0)
     81  1.1  jmcneill 
     82  1.1  jmcneill struct vcmbox_softc {
     83  1.1  jmcneill 	device_t		sc_dev;
     84  1.1  jmcneill 
     85  1.1  jmcneill 	/* temperature sensor */
     86  1.1  jmcneill 	struct sysmon_envsys	*sc_sme;
     87  1.1  jmcneill #define VCMBOX_SENSOR_TEMP	0
     88  1.1  jmcneill #define VCMBOX_NSENSORS		1
     89  1.1  jmcneill 	envsys_data_t		sc_sensor[VCMBOX_NSENSORS];
     90  1.2  jmcneill 
     91  1.2  jmcneill 	/* cpu frequency scaling */
     92  1.2  jmcneill 	struct sysctllog	*sc_log;
     93  1.2  jmcneill 	uint32_t		sc_cpu_minrate;
     94  1.2  jmcneill 	uint32_t		sc_cpu_maxrate;
     95  1.2  jmcneill 	int			sc_node_target;
     96  1.2  jmcneill 	int			sc_node_current;
     97  1.2  jmcneill 	int			sc_node_min;
     98  1.2  jmcneill 	int			sc_node_max;
     99  1.1  jmcneill };
    100  1.1  jmcneill 
    101  1.1  jmcneill static const char *vcmbox_sensor_name[VCMBOX_NSENSORS] = {
    102  1.1  jmcneill 	"temperature",
    103  1.1  jmcneill };
    104  1.1  jmcneill 
    105  1.1  jmcneill static int vcmbox_sensor_id[VCMBOX_NSENSORS] = {
    106  1.1  jmcneill 	VCPROP_TEMP_SOC,
    107  1.1  jmcneill };
    108  1.1  jmcneill 
    109  1.1  jmcneill static int	vcmbox_match(device_t, cfdata_t, void *);
    110  1.1  jmcneill static void	vcmbox_attach(device_t, device_t, void *);
    111  1.1  jmcneill 
    112  1.1  jmcneill static int	vcmbox_read_temp(struct vcmbox_softc *, uint32_t, int,
    113  1.1  jmcneill 				 uint32_t *);
    114  1.2  jmcneill static int	vcmbox_read_clockrate(struct vcmbox_softc *, uint32_t, int,
    115  1.2  jmcneill 				 uint32_t *);
    116  1.2  jmcneill static int	vcmbox_write_clockrate(struct vcmbox_softc *, uint32_t, int,
    117  1.2  jmcneill 				 uint32_t);
    118  1.2  jmcneill 
    119  1.2  jmcneill static int	vcmbox_cpufreq_init(struct vcmbox_softc *);
    120  1.2  jmcneill static int	vcmbox_cpufreq_sysctl_helper(SYSCTLFN_PROTO);
    121  1.2  jmcneill 
    122  1.2  jmcneill static void	vcmbox_create_sensors(struct vcmbox_softc *);
    123  1.1  jmcneill static void	vcmbox_sensor_get_limits(struct sysmon_envsys *,
    124  1.1  jmcneill 					 envsys_data_t *,
    125  1.1  jmcneill 					 sysmon_envsys_lim_t *, uint32_t *);
    126  1.1  jmcneill static void	vcmbox_sensor_refresh(struct sysmon_envsys *,
    127  1.1  jmcneill 				      envsys_data_t *);
    128  1.1  jmcneill 
    129  1.1  jmcneill CFATTACH_DECL_NEW(vcmbox, sizeof(struct vcmbox_softc),
    130  1.1  jmcneill     vcmbox_match, vcmbox_attach, NULL, NULL);
    131  1.1  jmcneill 
    132  1.1  jmcneill static int
    133  1.1  jmcneill vcmbox_match(device_t parent, cfdata_t match, void *aux)
    134  1.1  jmcneill {
    135  1.1  jmcneill 	return 1;
    136  1.1  jmcneill }
    137  1.1  jmcneill 
    138  1.1  jmcneill static void
    139  1.1  jmcneill vcmbox_attach(device_t parent, device_t self, void *aux)
    140  1.1  jmcneill {
    141  1.1  jmcneill 	struct vcmbox_softc *sc = device_private(self);
    142  1.1  jmcneill 
    143  1.1  jmcneill 	sc->sc_dev = self;
    144  1.1  jmcneill 
    145  1.1  jmcneill 	aprint_naive("\n");
    146  1.1  jmcneill 	aprint_normal("\n");
    147  1.1  jmcneill 
    148  1.2  jmcneill 	vcmbox_cpufreq_init(sc);
    149  1.2  jmcneill 
    150  1.1  jmcneill 	sc->sc_sme = sysmon_envsys_create();
    151  1.1  jmcneill 	sc->sc_sme->sme_cookie = sc;
    152  1.1  jmcneill 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    153  1.1  jmcneill 	sc->sc_sme->sme_refresh = vcmbox_sensor_refresh;
    154  1.1  jmcneill 	sc->sc_sme->sme_get_limits = vcmbox_sensor_get_limits;
    155  1.1  jmcneill 	vcmbox_create_sensors(sc);
    156  1.4   mlelstv 	if (sysmon_envsys_register(sc->sc_sme) == 0)
    157  1.4   mlelstv 		return;
    158  1.4   mlelstv 
    159  1.4   mlelstv 	aprint_error_dev(self, "unable to register with sysmon\n");
    160  1.4   mlelstv 	sysmon_envsys_destroy(sc->sc_sme);
    161  1.1  jmcneill }
    162  1.1  jmcneill 
    163  1.2  jmcneill static int
    164  1.2  jmcneill vcmbox_read_temp(struct vcmbox_softc *sc, uint32_t tag, int id, uint32_t *val)
    165  1.2  jmcneill {
    166  1.2  jmcneill 	struct vcmbox_temp_request vb;
    167  1.2  jmcneill 	uint32_t res;
    168  1.2  jmcneill 	int error;
    169  1.2  jmcneill 
    170  1.2  jmcneill 	VCMBOX_INIT_REQUEST(vb);
    171  1.2  jmcneill 	VCMBOX_INIT_TAG(vb.vbt_temp, tag);
    172  1.7       rin 	vb.vbt_temp.id = htole32(id);
    173  1.2  jmcneill 	error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb, sizeof(vb), &res);
    174  1.2  jmcneill 	if (error)
    175  1.2  jmcneill 		return error;
    176  1.2  jmcneill 	if (!vcprop_buffer_success_p(&vb.vb_hdr) ||
    177  1.2  jmcneill 	    !vcprop_tag_success_p(&vb.vbt_temp.tag)) {
    178  1.2  jmcneill 		return EIO;
    179  1.2  jmcneill 	}
    180  1.7       rin 	*val = le32toh(vb.vbt_temp.value);
    181  1.2  jmcneill 
    182  1.2  jmcneill 	return 0;
    183  1.2  jmcneill }
    184  1.2  jmcneill 
    185  1.2  jmcneill static int
    186  1.2  jmcneill vcmbox_read_clockrate(struct vcmbox_softc *sc, uint32_t tag, int id,
    187  1.2  jmcneill     uint32_t *val)
    188  1.2  jmcneill {
    189  1.2  jmcneill 	struct vcmbox_clockrate_request vb;
    190  1.2  jmcneill 	uint32_t res;
    191  1.2  jmcneill 	int error;
    192  1.2  jmcneill 
    193  1.2  jmcneill 	VCMBOX_INIT_REQUEST(vb);
    194  1.2  jmcneill 	VCMBOX_INIT_TAG(vb.vbt_clockrate, tag);
    195  1.7       rin 	vb.vbt_clockrate.id = htole32(id);
    196  1.2  jmcneill 	error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb, sizeof(vb), &res);
    197  1.2  jmcneill 	if (error)
    198  1.2  jmcneill 		return error;
    199  1.2  jmcneill 	if (!vcprop_buffer_success_p(&vb.vb_hdr) ||
    200  1.2  jmcneill 	    !vcprop_tag_success_p(&vb.vbt_clockrate.tag)) {
    201  1.2  jmcneill 		return EIO;
    202  1.2  jmcneill 	}
    203  1.7       rin 	*val = le32toh(vb.vbt_clockrate.rate);
    204  1.2  jmcneill 
    205  1.2  jmcneill 	return 0;
    206  1.2  jmcneill }
    207  1.2  jmcneill 
    208  1.2  jmcneill static int
    209  1.2  jmcneill vcmbox_write_clockrate(struct vcmbox_softc *sc, uint32_t tag, int id,
    210  1.2  jmcneill     uint32_t val)
    211  1.2  jmcneill {
    212  1.2  jmcneill 	struct vcmbox_clockrate_request vb;
    213  1.2  jmcneill 	uint32_t res;
    214  1.2  jmcneill 	int error;
    215  1.2  jmcneill 
    216  1.2  jmcneill 	VCMBOX_INIT_REQUEST(vb);
    217  1.2  jmcneill 	VCMBOX_INIT_TAG(vb.vbt_clockrate, tag);
    218  1.7       rin 	vb.vbt_clockrate.id = htole32(id);
    219  1.7       rin 	vb.vbt_clockrate.rate = htole32(val);
    220  1.2  jmcneill 	error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb, sizeof(vb), &res);
    221  1.2  jmcneill 	if (error)
    222  1.2  jmcneill 		return error;
    223  1.2  jmcneill 	if (!vcprop_buffer_success_p(&vb.vb_hdr) ||
    224  1.2  jmcneill 	    !vcprop_tag_success_p(&vb.vbt_clockrate.tag)) {
    225  1.2  jmcneill 		return EIO;
    226  1.2  jmcneill 	}
    227  1.2  jmcneill 
    228  1.2  jmcneill 	return 0;
    229  1.2  jmcneill }
    230  1.2  jmcneill 
    231  1.2  jmcneill 
    232  1.2  jmcneill static int
    233  1.2  jmcneill vcmbox_cpufreq_init(struct vcmbox_softc *sc)
    234  1.2  jmcneill {
    235  1.2  jmcneill 	const struct sysctlnode *node, *cpunode, *freqnode;
    236  1.2  jmcneill 	int error;
    237  1.5   mlelstv 	static char available[20];
    238  1.2  jmcneill 
    239  1.2  jmcneill 	error = vcmbox_read_clockrate(sc, VCPROPTAG_GET_MIN_CLOCKRATE,
    240  1.2  jmcneill 	    VCPROP_CLK_ARM, &sc->sc_cpu_minrate);
    241  1.2  jmcneill 	if (error) {
    242  1.2  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't read min clkrate (%d)\n",
    243  1.2  jmcneill 		    error);
    244  1.2  jmcneill 		return error;
    245  1.2  jmcneill 	}
    246  1.2  jmcneill 	error = vcmbox_read_clockrate(sc, VCPROPTAG_GET_MAX_CLOCKRATE,
    247  1.2  jmcneill 	    VCPROP_CLK_ARM, &sc->sc_cpu_maxrate);
    248  1.2  jmcneill 	if (error) {
    249  1.2  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't read max clkrate (%d)\n",
    250  1.2  jmcneill 		    error);
    251  1.2  jmcneill 		return error;
    252  1.2  jmcneill 	}
    253  1.2  jmcneill 
    254  1.2  jmcneill 	error = sysctl_createv(&sc->sc_log, 0, NULL, &node,
    255  1.2  jmcneill 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
    256  1.2  jmcneill 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
    257  1.2  jmcneill 	if (error)
    258  1.2  jmcneill 		goto sysctl_failed;
    259  1.2  jmcneill 	error = sysctl_createv(&sc->sc_log, 0, &node, &cpunode,
    260  1.2  jmcneill 	    0, CTLTYPE_NODE, "cpu", NULL,
    261  1.2  jmcneill 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    262  1.2  jmcneill 	if (error)
    263  1.2  jmcneill 		goto sysctl_failed;
    264  1.2  jmcneill 	error = sysctl_createv(&sc->sc_log, 0, &cpunode, &freqnode,
    265  1.2  jmcneill 	    0, CTLTYPE_NODE, "frequency", NULL,
    266  1.2  jmcneill 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    267  1.2  jmcneill 	if (error)
    268  1.2  jmcneill 		goto sysctl_failed;
    269  1.2  jmcneill 
    270  1.2  jmcneill 	error = sysctl_createv(&sc->sc_log, 0, &freqnode, &node,
    271  1.2  jmcneill 	    CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
    272  1.2  jmcneill 	    vcmbox_cpufreq_sysctl_helper, 0, (void *)sc, 0,
    273  1.3     skrll 	    CTL_CREATE, CTL_EOL);
    274  1.2  jmcneill 	if (error)
    275  1.2  jmcneill 		goto sysctl_failed;
    276  1.2  jmcneill 	sc->sc_node_target = node->sysctl_num;
    277  1.2  jmcneill 
    278  1.2  jmcneill 	error = sysctl_createv(&sc->sc_log, 0, &freqnode, &node,
    279  1.2  jmcneill 	    0, CTLTYPE_INT, "current", NULL,
    280  1.2  jmcneill 	    vcmbox_cpufreq_sysctl_helper, 0, (void *)sc, 0,
    281  1.3     skrll 	    CTL_CREATE, CTL_EOL);
    282  1.2  jmcneill 	if (error)
    283  1.2  jmcneill 		goto sysctl_failed;
    284  1.2  jmcneill 	sc->sc_node_current = node->sysctl_num;
    285  1.2  jmcneill 
    286  1.2  jmcneill 	error = sysctl_createv(&sc->sc_log, 0, &freqnode, &node,
    287  1.2  jmcneill 	    0, CTLTYPE_INT, "min", NULL,
    288  1.2  jmcneill 	    vcmbox_cpufreq_sysctl_helper, 0, (void *)sc, 0,
    289  1.3     skrll 	    CTL_CREATE, CTL_EOL);
    290  1.2  jmcneill 	if (error)
    291  1.2  jmcneill 		goto sysctl_failed;
    292  1.2  jmcneill 	sc->sc_node_min = node->sysctl_num;
    293  1.2  jmcneill 
    294  1.2  jmcneill 	error = sysctl_createv(&sc->sc_log, 0, &freqnode, &node,
    295  1.2  jmcneill 	    0, CTLTYPE_INT, "max", NULL,
    296  1.2  jmcneill 	    vcmbox_cpufreq_sysctl_helper, 0, (void *)sc, 0,
    297  1.3     skrll 	    CTL_CREATE, CTL_EOL);
    298  1.2  jmcneill 	if (error)
    299  1.2  jmcneill 		goto sysctl_failed;
    300  1.2  jmcneill 	sc->sc_node_max = node->sysctl_num;
    301  1.2  jmcneill 
    302  1.5   mlelstv 	snprintf(available, sizeof(available), "%" PRIu32 " %" PRIu32,
    303  1.5   mlelstv 	    RATE2MHZ(sc->sc_cpu_minrate), RATE2MHZ(sc->sc_cpu_maxrate));
    304  1.5   mlelstv 
    305  1.5   mlelstv 	error = sysctl_createv(&sc->sc_log, 0, &freqnode, &node,
    306  1.5   mlelstv 	    CTLFLAG_PERMANENT, CTLTYPE_STRING, "available", NULL,
    307  1.5   mlelstv 	    NULL, 0, available, strlen(available),
    308  1.5   mlelstv 	    CTL_CREATE, CTL_EOL);
    309  1.5   mlelstv 	if (error)
    310  1.5   mlelstv 		goto sysctl_failed;
    311  1.5   mlelstv 
    312  1.2  jmcneill 	return 0;
    313  1.2  jmcneill 
    314  1.2  jmcneill sysctl_failed:
    315  1.2  jmcneill 	aprint_error_dev(sc->sc_dev, "couldn't create sysctl nodes (%d)\n",
    316  1.2  jmcneill 	    error);
    317  1.2  jmcneill 	sysctl_teardown(&sc->sc_log);
    318  1.2  jmcneill 	return error;
    319  1.2  jmcneill }
    320  1.2  jmcneill 
    321  1.2  jmcneill static int
    322  1.2  jmcneill vcmbox_cpufreq_sysctl_helper(SYSCTLFN_ARGS)
    323  1.2  jmcneill {
    324  1.2  jmcneill 	struct sysctlnode node;
    325  1.2  jmcneill 	struct vcmbox_softc *sc;
    326  1.2  jmcneill 	int fq, oldfq = 0, error;
    327  1.2  jmcneill 	uint32_t rate;
    328  1.2  jmcneill 
    329  1.2  jmcneill 	node = *rnode;
    330  1.2  jmcneill 	sc = node.sysctl_data;
    331  1.2  jmcneill 
    332  1.2  jmcneill 	node.sysctl_data = &fq;
    333  1.2  jmcneill 
    334  1.2  jmcneill 	if (rnode->sysctl_num == sc->sc_node_target ||
    335  1.2  jmcneill 	    rnode->sysctl_num == sc->sc_node_current) {
    336  1.2  jmcneill 		error = vcmbox_read_clockrate(sc, VCPROPTAG_GET_CLOCKRATE,
    337  1.2  jmcneill 		    VCPROP_CLK_ARM, &rate);
    338  1.2  jmcneill 		if (error)
    339  1.2  jmcneill 			return error;
    340  1.2  jmcneill 		fq = RATE2MHZ(rate);
    341  1.2  jmcneill 		if (rnode->sysctl_num == sc->sc_node_target)
    342  1.2  jmcneill 			oldfq = fq;
    343  1.2  jmcneill 	} else if (rnode->sysctl_num == sc->sc_node_min) {
    344  1.2  jmcneill 		fq = RATE2MHZ(sc->sc_cpu_minrate);
    345  1.2  jmcneill 	} else if (rnode->sysctl_num == sc->sc_node_max) {
    346  1.2  jmcneill 		fq = RATE2MHZ(sc->sc_cpu_maxrate);
    347  1.2  jmcneill 	} else
    348  1.2  jmcneill 		return EOPNOTSUPP;
    349  1.2  jmcneill 
    350  1.2  jmcneill 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    351  1.2  jmcneill 	if (error || newp == NULL)
    352  1.2  jmcneill 		return error;
    353  1.2  jmcneill 
    354  1.2  jmcneill 	if (fq == oldfq || rnode->sysctl_num != sc->sc_node_target)
    355  1.2  jmcneill 		return 0;
    356  1.2  jmcneill 
    357  1.2  jmcneill 	if (fq < RATE2MHZ(sc->sc_cpu_minrate))
    358  1.2  jmcneill 		fq = RATE2MHZ(sc->sc_cpu_minrate);
    359  1.2  jmcneill 	if (fq > RATE2MHZ(sc->sc_cpu_maxrate))
    360  1.2  jmcneill 		fq = RATE2MHZ(sc->sc_cpu_maxrate);
    361  1.2  jmcneill 
    362  1.2  jmcneill 	return vcmbox_write_clockrate(sc, VCPROPTAG_SET_CLOCKRATE,
    363  1.2  jmcneill 	    VCPROP_CLK_ARM, MHZ2RATE(fq));
    364  1.2  jmcneill }
    365  1.2  jmcneill 
    366  1.1  jmcneill static void
    367  1.1  jmcneill vcmbox_create_sensors(struct vcmbox_softc *sc)
    368  1.1  jmcneill {
    369  1.1  jmcneill 	uint32_t val;
    370  1.1  jmcneill 
    371  1.1  jmcneill 	sc->sc_sensor[VCMBOX_SENSOR_TEMP].sensor = VCMBOX_SENSOR_TEMP;
    372  1.1  jmcneill 	sc->sc_sensor[VCMBOX_SENSOR_TEMP].units = ENVSYS_STEMP;
    373  1.1  jmcneill 	sc->sc_sensor[VCMBOX_SENSOR_TEMP].state = ENVSYS_SINVALID;
    374  1.4   mlelstv 	sc->sc_sensor[VCMBOX_SENSOR_TEMP].flags = ENVSYS_FMONLIMITS |
    375  1.4   mlelstv 						  ENVSYS_FHAS_ENTROPY;
    376  1.1  jmcneill 	strlcpy(sc->sc_sensor[VCMBOX_SENSOR_TEMP].desc,
    377  1.1  jmcneill 	    vcmbox_sensor_name[VCMBOX_SENSOR_TEMP],
    378  1.1  jmcneill 	    sizeof(sc->sc_sensor[VCMBOX_SENSOR_TEMP].desc));
    379  1.1  jmcneill 	if (vcmbox_read_temp(sc, VCPROPTAG_GET_MAX_TEMPERATURE,
    380  1.1  jmcneill 			     vcmbox_sensor_id[VCMBOX_SENSOR_TEMP], &val) == 0) {
    381  1.3     skrll 		sc->sc_sensor[VCMBOX_SENSOR_TEMP].value_max =
    382  1.1  jmcneill 		    val * 1000 + 273150000;
    383  1.1  jmcneill 		sc->sc_sensor[VCMBOX_SENSOR_TEMP].flags |= ENVSYS_FVALID_MAX;
    384  1.1  jmcneill 	}
    385  1.1  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme,
    386  1.1  jmcneill 	    &sc->sc_sensor[VCMBOX_SENSOR_TEMP]);
    387  1.1  jmcneill }
    388  1.1  jmcneill 
    389  1.1  jmcneill static void
    390  1.1  jmcneill vcmbox_sensor_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    391  1.1  jmcneill     sysmon_envsys_lim_t *limits, uint32_t *props)
    392  1.1  jmcneill {
    393  1.1  jmcneill 	struct vcmbox_softc *sc = sme->sme_cookie;
    394  1.1  jmcneill 	uint32_t val;
    395  1.1  jmcneill 
    396  1.1  jmcneill 	*props = 0;
    397  1.1  jmcneill 
    398  1.1  jmcneill 	if (edata->units == ENVSYS_STEMP) {
    399  1.1  jmcneill 		if (vcmbox_read_temp(sc, VCPROPTAG_GET_MAX_TEMPERATURE,
    400  1.1  jmcneill 				     vcmbox_sensor_id[edata->sensor], &val))
    401  1.1  jmcneill 			return;
    402  1.1  jmcneill 		*props = PROP_CRITMAX;
    403  1.1  jmcneill 		limits->sel_critmax = val * 1000 + 273150000;
    404  1.1  jmcneill 	}
    405  1.1  jmcneill }
    406  1.1  jmcneill 
    407  1.1  jmcneill static void
    408  1.1  jmcneill vcmbox_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    409  1.1  jmcneill {
    410  1.1  jmcneill 	struct vcmbox_softc *sc = sme->sme_cookie;
    411  1.1  jmcneill 	uint32_t val;
    412  1.1  jmcneill 
    413  1.1  jmcneill 	edata->state = ENVSYS_SINVALID;
    414  1.1  jmcneill 
    415  1.1  jmcneill 	if (edata->units == ENVSYS_STEMP) {
    416  1.1  jmcneill 		if (vcmbox_read_temp(sc, VCPROPTAG_GET_TEMPERATURE,
    417  1.1  jmcneill 				     vcmbox_sensor_id[edata->sensor], &val))
    418  1.1  jmcneill 			return;
    419  1.1  jmcneill 
    420  1.1  jmcneill 		edata->value_cur = val * 1000 + 273150000;
    421  1.1  jmcneill 		edata->state = ENVSYS_SVALID;
    422  1.1  jmcneill 	}
    423  1.1  jmcneill }
    424