Home | History | Annotate | Line # | Download | only in rpi
      1  1.8   mlelstv /* $NetBSD: rpi_vcmbox.c,v 1.8 2021/03/08 13:53:08 mlelstv 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.8   mlelstv __KERNEL_RCSID(0, "$NetBSD: rpi_vcmbox.c,v 1.8 2021/03/08 13:53:08 mlelstv 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.8   mlelstv #define VCMBOX_INIT_REQUEST(r)		VCPROP_INIT_REQUEST(r)
     69  1.8   mlelstv #define VCMBOX_INIT_TAG(s, t)		VCPROP_INIT_TAG(s, t)
     70  1.1  jmcneill 
     71  1.1  jmcneill struct vcmbox_softc {
     72  1.1  jmcneill 	device_t		sc_dev;
     73  1.1  jmcneill 
     74  1.1  jmcneill 	/* temperature sensor */
     75  1.1  jmcneill 	struct sysmon_envsys	*sc_sme;
     76  1.1  jmcneill #define VCMBOX_SENSOR_TEMP	0
     77  1.1  jmcneill #define VCMBOX_NSENSORS		1
     78  1.1  jmcneill 	envsys_data_t		sc_sensor[VCMBOX_NSENSORS];
     79  1.2  jmcneill 
     80  1.2  jmcneill 	/* cpu frequency scaling */
     81  1.2  jmcneill 	struct sysctllog	*sc_log;
     82  1.2  jmcneill 	uint32_t		sc_cpu_minrate;
     83  1.2  jmcneill 	uint32_t		sc_cpu_maxrate;
     84  1.2  jmcneill 	int			sc_node_target;
     85  1.2  jmcneill 	int			sc_node_current;
     86  1.2  jmcneill 	int			sc_node_min;
     87  1.2  jmcneill 	int			sc_node_max;
     88  1.1  jmcneill };
     89  1.1  jmcneill 
     90  1.1  jmcneill static const char *vcmbox_sensor_name[VCMBOX_NSENSORS] = {
     91  1.1  jmcneill 	"temperature",
     92  1.1  jmcneill };
     93  1.1  jmcneill 
     94  1.1  jmcneill static int vcmbox_sensor_id[VCMBOX_NSENSORS] = {
     95  1.1  jmcneill 	VCPROP_TEMP_SOC,
     96  1.1  jmcneill };
     97  1.1  jmcneill 
     98  1.1  jmcneill static int	vcmbox_match(device_t, cfdata_t, void *);
     99  1.1  jmcneill static void	vcmbox_attach(device_t, device_t, void *);
    100  1.1  jmcneill 
    101  1.1  jmcneill static int	vcmbox_read_temp(struct vcmbox_softc *, uint32_t, int,
    102  1.1  jmcneill 				 uint32_t *);
    103  1.2  jmcneill static int	vcmbox_read_clockrate(struct vcmbox_softc *, uint32_t, int,
    104  1.2  jmcneill 				 uint32_t *);
    105  1.2  jmcneill static int	vcmbox_write_clockrate(struct vcmbox_softc *, uint32_t, int,
    106  1.2  jmcneill 				 uint32_t);
    107  1.2  jmcneill 
    108  1.2  jmcneill static int	vcmbox_cpufreq_init(struct vcmbox_softc *);
    109  1.2  jmcneill static int	vcmbox_cpufreq_sysctl_helper(SYSCTLFN_PROTO);
    110  1.2  jmcneill 
    111  1.2  jmcneill static void	vcmbox_create_sensors(struct vcmbox_softc *);
    112  1.1  jmcneill static void	vcmbox_sensor_get_limits(struct sysmon_envsys *,
    113  1.1  jmcneill 					 envsys_data_t *,
    114  1.1  jmcneill 					 sysmon_envsys_lim_t *, uint32_t *);
    115  1.1  jmcneill static void	vcmbox_sensor_refresh(struct sysmon_envsys *,
    116  1.1  jmcneill 				      envsys_data_t *);
    117  1.1  jmcneill 
    118  1.1  jmcneill CFATTACH_DECL_NEW(vcmbox, sizeof(struct vcmbox_softc),
    119  1.1  jmcneill     vcmbox_match, vcmbox_attach, NULL, NULL);
    120  1.1  jmcneill 
    121  1.1  jmcneill static int
    122  1.1  jmcneill vcmbox_match(device_t parent, cfdata_t match, void *aux)
    123  1.1  jmcneill {
    124  1.1  jmcneill 	return 1;
    125  1.1  jmcneill }
    126  1.1  jmcneill 
    127  1.1  jmcneill static void
    128  1.1  jmcneill vcmbox_attach(device_t parent, device_t self, void *aux)
    129  1.1  jmcneill {
    130  1.1  jmcneill 	struct vcmbox_softc *sc = device_private(self);
    131  1.1  jmcneill 
    132  1.1  jmcneill 	sc->sc_dev = self;
    133  1.1  jmcneill 
    134  1.1  jmcneill 	aprint_naive("\n");
    135  1.1  jmcneill 	aprint_normal("\n");
    136  1.1  jmcneill 
    137  1.2  jmcneill 	vcmbox_cpufreq_init(sc);
    138  1.2  jmcneill 
    139  1.1  jmcneill 	sc->sc_sme = sysmon_envsys_create();
    140  1.1  jmcneill 	sc->sc_sme->sme_cookie = sc;
    141  1.1  jmcneill 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    142  1.1  jmcneill 	sc->sc_sme->sme_refresh = vcmbox_sensor_refresh;
    143  1.1  jmcneill 	sc->sc_sme->sme_get_limits = vcmbox_sensor_get_limits;
    144  1.1  jmcneill 	vcmbox_create_sensors(sc);
    145  1.4   mlelstv 	if (sysmon_envsys_register(sc->sc_sme) == 0)
    146  1.4   mlelstv 		return;
    147  1.4   mlelstv 
    148  1.4   mlelstv 	aprint_error_dev(self, "unable to register with sysmon\n");
    149  1.4   mlelstv 	sysmon_envsys_destroy(sc->sc_sme);
    150  1.1  jmcneill }
    151  1.1  jmcneill 
    152  1.2  jmcneill static int
    153  1.2  jmcneill vcmbox_read_temp(struct vcmbox_softc *sc, uint32_t tag, int id, uint32_t *val)
    154  1.2  jmcneill {
    155  1.2  jmcneill 	struct vcmbox_temp_request vb;
    156  1.2  jmcneill 	uint32_t res;
    157  1.2  jmcneill 	int error;
    158  1.2  jmcneill 
    159  1.2  jmcneill 	VCMBOX_INIT_REQUEST(vb);
    160  1.2  jmcneill 	VCMBOX_INIT_TAG(vb.vbt_temp, tag);
    161  1.7       rin 	vb.vbt_temp.id = htole32(id);
    162  1.2  jmcneill 	error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb, sizeof(vb), &res);
    163  1.2  jmcneill 	if (error)
    164  1.2  jmcneill 		return error;
    165  1.2  jmcneill 	if (!vcprop_buffer_success_p(&vb.vb_hdr) ||
    166  1.2  jmcneill 	    !vcprop_tag_success_p(&vb.vbt_temp.tag)) {
    167  1.2  jmcneill 		return EIO;
    168  1.2  jmcneill 	}
    169  1.7       rin 	*val = le32toh(vb.vbt_temp.value);
    170  1.2  jmcneill 
    171  1.2  jmcneill 	return 0;
    172  1.2  jmcneill }
    173  1.2  jmcneill 
    174  1.2  jmcneill static int
    175  1.2  jmcneill vcmbox_read_clockrate(struct vcmbox_softc *sc, uint32_t tag, int id,
    176  1.2  jmcneill     uint32_t *val)
    177  1.2  jmcneill {
    178  1.2  jmcneill 	struct vcmbox_clockrate_request vb;
    179  1.2  jmcneill 	uint32_t res;
    180  1.2  jmcneill 	int error;
    181  1.2  jmcneill 
    182  1.2  jmcneill 	VCMBOX_INIT_REQUEST(vb);
    183  1.2  jmcneill 	VCMBOX_INIT_TAG(vb.vbt_clockrate, tag);
    184  1.7       rin 	vb.vbt_clockrate.id = htole32(id);
    185  1.2  jmcneill 	error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb, sizeof(vb), &res);
    186  1.2  jmcneill 	if (error)
    187  1.2  jmcneill 		return error;
    188  1.2  jmcneill 	if (!vcprop_buffer_success_p(&vb.vb_hdr) ||
    189  1.2  jmcneill 	    !vcprop_tag_success_p(&vb.vbt_clockrate.tag)) {
    190  1.2  jmcneill 		return EIO;
    191  1.2  jmcneill 	}
    192  1.7       rin 	*val = le32toh(vb.vbt_clockrate.rate);
    193  1.2  jmcneill 
    194  1.2  jmcneill 	return 0;
    195  1.2  jmcneill }
    196  1.2  jmcneill 
    197  1.2  jmcneill static int
    198  1.2  jmcneill vcmbox_write_clockrate(struct vcmbox_softc *sc, uint32_t tag, int id,
    199  1.2  jmcneill     uint32_t val)
    200  1.2  jmcneill {
    201  1.2  jmcneill 	struct vcmbox_clockrate_request vb;
    202  1.2  jmcneill 	uint32_t res;
    203  1.2  jmcneill 	int error;
    204  1.2  jmcneill 
    205  1.2  jmcneill 	VCMBOX_INIT_REQUEST(vb);
    206  1.2  jmcneill 	VCMBOX_INIT_TAG(vb.vbt_clockrate, tag);
    207  1.7       rin 	vb.vbt_clockrate.id = htole32(id);
    208  1.7       rin 	vb.vbt_clockrate.rate = htole32(val);
    209  1.2  jmcneill 	error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb, sizeof(vb), &res);
    210  1.2  jmcneill 	if (error)
    211  1.2  jmcneill 		return error;
    212  1.2  jmcneill 	if (!vcprop_buffer_success_p(&vb.vb_hdr) ||
    213  1.2  jmcneill 	    !vcprop_tag_success_p(&vb.vbt_clockrate.tag)) {
    214  1.2  jmcneill 		return EIO;
    215  1.2  jmcneill 	}
    216  1.2  jmcneill 
    217  1.2  jmcneill 	return 0;
    218  1.2  jmcneill }
    219  1.2  jmcneill 
    220  1.2  jmcneill 
    221  1.2  jmcneill static int
    222  1.2  jmcneill vcmbox_cpufreq_init(struct vcmbox_softc *sc)
    223  1.2  jmcneill {
    224  1.2  jmcneill 	const struct sysctlnode *node, *cpunode, *freqnode;
    225  1.2  jmcneill 	int error;
    226  1.5   mlelstv 	static char available[20];
    227  1.2  jmcneill 
    228  1.2  jmcneill 	error = vcmbox_read_clockrate(sc, VCPROPTAG_GET_MIN_CLOCKRATE,
    229  1.2  jmcneill 	    VCPROP_CLK_ARM, &sc->sc_cpu_minrate);
    230  1.2  jmcneill 	if (error) {
    231  1.2  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't read min clkrate (%d)\n",
    232  1.2  jmcneill 		    error);
    233  1.2  jmcneill 		return error;
    234  1.2  jmcneill 	}
    235  1.2  jmcneill 	error = vcmbox_read_clockrate(sc, VCPROPTAG_GET_MAX_CLOCKRATE,
    236  1.2  jmcneill 	    VCPROP_CLK_ARM, &sc->sc_cpu_maxrate);
    237  1.2  jmcneill 	if (error) {
    238  1.2  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't read max clkrate (%d)\n",
    239  1.2  jmcneill 		    error);
    240  1.2  jmcneill 		return error;
    241  1.2  jmcneill 	}
    242  1.2  jmcneill 
    243  1.2  jmcneill 	error = sysctl_createv(&sc->sc_log, 0, NULL, &node,
    244  1.2  jmcneill 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
    245  1.2  jmcneill 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
    246  1.2  jmcneill 	if (error)
    247  1.2  jmcneill 		goto sysctl_failed;
    248  1.2  jmcneill 	error = sysctl_createv(&sc->sc_log, 0, &node, &cpunode,
    249  1.2  jmcneill 	    0, CTLTYPE_NODE, "cpu", NULL,
    250  1.2  jmcneill 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    251  1.2  jmcneill 	if (error)
    252  1.2  jmcneill 		goto sysctl_failed;
    253  1.2  jmcneill 	error = sysctl_createv(&sc->sc_log, 0, &cpunode, &freqnode,
    254  1.2  jmcneill 	    0, CTLTYPE_NODE, "frequency", NULL,
    255  1.2  jmcneill 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    256  1.2  jmcneill 	if (error)
    257  1.2  jmcneill 		goto sysctl_failed;
    258  1.2  jmcneill 
    259  1.2  jmcneill 	error = sysctl_createv(&sc->sc_log, 0, &freqnode, &node,
    260  1.2  jmcneill 	    CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
    261  1.2  jmcneill 	    vcmbox_cpufreq_sysctl_helper, 0, (void *)sc, 0,
    262  1.3     skrll 	    CTL_CREATE, CTL_EOL);
    263  1.2  jmcneill 	if (error)
    264  1.2  jmcneill 		goto sysctl_failed;
    265  1.2  jmcneill 	sc->sc_node_target = node->sysctl_num;
    266  1.2  jmcneill 
    267  1.2  jmcneill 	error = sysctl_createv(&sc->sc_log, 0, &freqnode, &node,
    268  1.2  jmcneill 	    0, CTLTYPE_INT, "current", NULL,
    269  1.2  jmcneill 	    vcmbox_cpufreq_sysctl_helper, 0, (void *)sc, 0,
    270  1.3     skrll 	    CTL_CREATE, CTL_EOL);
    271  1.2  jmcneill 	if (error)
    272  1.2  jmcneill 		goto sysctl_failed;
    273  1.2  jmcneill 	sc->sc_node_current = node->sysctl_num;
    274  1.2  jmcneill 
    275  1.2  jmcneill 	error = sysctl_createv(&sc->sc_log, 0, &freqnode, &node,
    276  1.2  jmcneill 	    0, CTLTYPE_INT, "min", NULL,
    277  1.2  jmcneill 	    vcmbox_cpufreq_sysctl_helper, 0, (void *)sc, 0,
    278  1.3     skrll 	    CTL_CREATE, CTL_EOL);
    279  1.2  jmcneill 	if (error)
    280  1.2  jmcneill 		goto sysctl_failed;
    281  1.2  jmcneill 	sc->sc_node_min = node->sysctl_num;
    282  1.2  jmcneill 
    283  1.2  jmcneill 	error = sysctl_createv(&sc->sc_log, 0, &freqnode, &node,
    284  1.2  jmcneill 	    0, CTLTYPE_INT, "max", NULL,
    285  1.2  jmcneill 	    vcmbox_cpufreq_sysctl_helper, 0, (void *)sc, 0,
    286  1.3     skrll 	    CTL_CREATE, CTL_EOL);
    287  1.2  jmcneill 	if (error)
    288  1.2  jmcneill 		goto sysctl_failed;
    289  1.2  jmcneill 	sc->sc_node_max = node->sysctl_num;
    290  1.2  jmcneill 
    291  1.5   mlelstv 	snprintf(available, sizeof(available), "%" PRIu32 " %" PRIu32,
    292  1.5   mlelstv 	    RATE2MHZ(sc->sc_cpu_minrate), RATE2MHZ(sc->sc_cpu_maxrate));
    293  1.5   mlelstv 
    294  1.5   mlelstv 	error = sysctl_createv(&sc->sc_log, 0, &freqnode, &node,
    295  1.5   mlelstv 	    CTLFLAG_PERMANENT, CTLTYPE_STRING, "available", NULL,
    296  1.5   mlelstv 	    NULL, 0, available, strlen(available),
    297  1.5   mlelstv 	    CTL_CREATE, CTL_EOL);
    298  1.5   mlelstv 	if (error)
    299  1.5   mlelstv 		goto sysctl_failed;
    300  1.5   mlelstv 
    301  1.2  jmcneill 	return 0;
    302  1.2  jmcneill 
    303  1.2  jmcneill sysctl_failed:
    304  1.2  jmcneill 	aprint_error_dev(sc->sc_dev, "couldn't create sysctl nodes (%d)\n",
    305  1.2  jmcneill 	    error);
    306  1.2  jmcneill 	sysctl_teardown(&sc->sc_log);
    307  1.2  jmcneill 	return error;
    308  1.2  jmcneill }
    309  1.2  jmcneill 
    310  1.2  jmcneill static int
    311  1.2  jmcneill vcmbox_cpufreq_sysctl_helper(SYSCTLFN_ARGS)
    312  1.2  jmcneill {
    313  1.2  jmcneill 	struct sysctlnode node;
    314  1.2  jmcneill 	struct vcmbox_softc *sc;
    315  1.2  jmcneill 	int fq, oldfq = 0, error;
    316  1.2  jmcneill 	uint32_t rate;
    317  1.2  jmcneill 
    318  1.2  jmcneill 	node = *rnode;
    319  1.2  jmcneill 	sc = node.sysctl_data;
    320  1.2  jmcneill 
    321  1.2  jmcneill 	node.sysctl_data = &fq;
    322  1.2  jmcneill 
    323  1.2  jmcneill 	if (rnode->sysctl_num == sc->sc_node_target ||
    324  1.2  jmcneill 	    rnode->sysctl_num == sc->sc_node_current) {
    325  1.2  jmcneill 		error = vcmbox_read_clockrate(sc, VCPROPTAG_GET_CLOCKRATE,
    326  1.2  jmcneill 		    VCPROP_CLK_ARM, &rate);
    327  1.2  jmcneill 		if (error)
    328  1.2  jmcneill 			return error;
    329  1.2  jmcneill 		fq = RATE2MHZ(rate);
    330  1.2  jmcneill 		if (rnode->sysctl_num == sc->sc_node_target)
    331  1.2  jmcneill 			oldfq = fq;
    332  1.2  jmcneill 	} else if (rnode->sysctl_num == sc->sc_node_min) {
    333  1.2  jmcneill 		fq = RATE2MHZ(sc->sc_cpu_minrate);
    334  1.2  jmcneill 	} else if (rnode->sysctl_num == sc->sc_node_max) {
    335  1.2  jmcneill 		fq = RATE2MHZ(sc->sc_cpu_maxrate);
    336  1.2  jmcneill 	} else
    337  1.2  jmcneill 		return EOPNOTSUPP;
    338  1.2  jmcneill 
    339  1.2  jmcneill 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    340  1.2  jmcneill 	if (error || newp == NULL)
    341  1.2  jmcneill 		return error;
    342  1.2  jmcneill 
    343  1.2  jmcneill 	if (fq == oldfq || rnode->sysctl_num != sc->sc_node_target)
    344  1.2  jmcneill 		return 0;
    345  1.2  jmcneill 
    346  1.2  jmcneill 	if (fq < RATE2MHZ(sc->sc_cpu_minrate))
    347  1.2  jmcneill 		fq = RATE2MHZ(sc->sc_cpu_minrate);
    348  1.2  jmcneill 	if (fq > RATE2MHZ(sc->sc_cpu_maxrate))
    349  1.2  jmcneill 		fq = RATE2MHZ(sc->sc_cpu_maxrate);
    350  1.2  jmcneill 
    351  1.2  jmcneill 	return vcmbox_write_clockrate(sc, VCPROPTAG_SET_CLOCKRATE,
    352  1.2  jmcneill 	    VCPROP_CLK_ARM, MHZ2RATE(fq));
    353  1.2  jmcneill }
    354  1.2  jmcneill 
    355  1.1  jmcneill static void
    356  1.1  jmcneill vcmbox_create_sensors(struct vcmbox_softc *sc)
    357  1.1  jmcneill {
    358  1.1  jmcneill 	uint32_t val;
    359  1.1  jmcneill 
    360  1.1  jmcneill 	sc->sc_sensor[VCMBOX_SENSOR_TEMP].sensor = VCMBOX_SENSOR_TEMP;
    361  1.1  jmcneill 	sc->sc_sensor[VCMBOX_SENSOR_TEMP].units = ENVSYS_STEMP;
    362  1.1  jmcneill 	sc->sc_sensor[VCMBOX_SENSOR_TEMP].state = ENVSYS_SINVALID;
    363  1.4   mlelstv 	sc->sc_sensor[VCMBOX_SENSOR_TEMP].flags = ENVSYS_FMONLIMITS |
    364  1.4   mlelstv 						  ENVSYS_FHAS_ENTROPY;
    365  1.1  jmcneill 	strlcpy(sc->sc_sensor[VCMBOX_SENSOR_TEMP].desc,
    366  1.1  jmcneill 	    vcmbox_sensor_name[VCMBOX_SENSOR_TEMP],
    367  1.1  jmcneill 	    sizeof(sc->sc_sensor[VCMBOX_SENSOR_TEMP].desc));
    368  1.1  jmcneill 	if (vcmbox_read_temp(sc, VCPROPTAG_GET_MAX_TEMPERATURE,
    369  1.1  jmcneill 			     vcmbox_sensor_id[VCMBOX_SENSOR_TEMP], &val) == 0) {
    370  1.3     skrll 		sc->sc_sensor[VCMBOX_SENSOR_TEMP].value_max =
    371  1.1  jmcneill 		    val * 1000 + 273150000;
    372  1.1  jmcneill 		sc->sc_sensor[VCMBOX_SENSOR_TEMP].flags |= ENVSYS_FVALID_MAX;
    373  1.1  jmcneill 	}
    374  1.1  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme,
    375  1.1  jmcneill 	    &sc->sc_sensor[VCMBOX_SENSOR_TEMP]);
    376  1.1  jmcneill }
    377  1.1  jmcneill 
    378  1.1  jmcneill static void
    379  1.1  jmcneill vcmbox_sensor_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    380  1.1  jmcneill     sysmon_envsys_lim_t *limits, uint32_t *props)
    381  1.1  jmcneill {
    382  1.1  jmcneill 	struct vcmbox_softc *sc = sme->sme_cookie;
    383  1.1  jmcneill 	uint32_t val;
    384  1.1  jmcneill 
    385  1.1  jmcneill 	*props = 0;
    386  1.1  jmcneill 
    387  1.1  jmcneill 	if (edata->units == ENVSYS_STEMP) {
    388  1.1  jmcneill 		if (vcmbox_read_temp(sc, VCPROPTAG_GET_MAX_TEMPERATURE,
    389  1.1  jmcneill 				     vcmbox_sensor_id[edata->sensor], &val))
    390  1.1  jmcneill 			return;
    391  1.1  jmcneill 		*props = PROP_CRITMAX;
    392  1.1  jmcneill 		limits->sel_critmax = val * 1000 + 273150000;
    393  1.1  jmcneill 	}
    394  1.1  jmcneill }
    395  1.1  jmcneill 
    396  1.1  jmcneill static void
    397  1.1  jmcneill vcmbox_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    398  1.1  jmcneill {
    399  1.1  jmcneill 	struct vcmbox_softc *sc = sme->sme_cookie;
    400  1.1  jmcneill 	uint32_t val;
    401  1.1  jmcneill 
    402  1.1  jmcneill 	edata->state = ENVSYS_SINVALID;
    403  1.1  jmcneill 
    404  1.1  jmcneill 	if (edata->units == ENVSYS_STEMP) {
    405  1.1  jmcneill 		if (vcmbox_read_temp(sc, VCPROPTAG_GET_TEMPERATURE,
    406  1.1  jmcneill 				     vcmbox_sensor_id[edata->sensor], &val))
    407  1.1  jmcneill 			return;
    408  1.1  jmcneill 
    409  1.1  jmcneill 		edata->value_cur = val * 1000 + 273150000;
    410  1.1  jmcneill 		edata->state = ENVSYS_SVALID;
    411  1.1  jmcneill 	}
    412  1.1  jmcneill }
    413