Home | History | Annotate | Line # | Download | only in rpi
rpi_vcmbox.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: rpi_vcmbox.c,v 1.1 2013/01/07 20:19:33 jmcneill 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.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: rpi_vcmbox.c,v 1.1 2013/01/07 20:19:33 jmcneill Exp $");
     35  1.1  jmcneill 
     36  1.1  jmcneill #include <sys/param.h>
     37  1.1  jmcneill #include <sys/types.h>
     38  1.1  jmcneill #include <sys/systm.h>
     39  1.1  jmcneill #include <sys/device.h>
     40  1.1  jmcneill #include <sys/conf.h>
     41  1.1  jmcneill #include <sys/bus.h>
     42  1.1  jmcneill #include <sys/kmem.h>
     43  1.1  jmcneill 
     44  1.1  jmcneill #include <dev/sysmon/sysmonvar.h>
     45  1.1  jmcneill 
     46  1.1  jmcneill #include <arm/broadcom/bcm2835_mbox.h>
     47  1.1  jmcneill 
     48  1.1  jmcneill #include <evbarm/rpi/vcio.h>
     49  1.1  jmcneill #include <evbarm/rpi/vcprop.h>
     50  1.1  jmcneill 
     51  1.1  jmcneill struct vcmbox_temp_request {
     52  1.1  jmcneill 	struct vcprop_buffer_hdr	vb_hdr;
     53  1.1  jmcneill 	struct vcprop_tag_temperature	vbt_temp;
     54  1.1  jmcneill 	struct vcprop_tag end;
     55  1.1  jmcneill } __packed;
     56  1.1  jmcneill 
     57  1.1  jmcneill #define VCMBOX_INIT_REQUEST(req)					\
     58  1.1  jmcneill 	do {								\
     59  1.1  jmcneill 		memset(&(req), 0, sizeof((req)));			\
     60  1.1  jmcneill 		(req).vb_hdr.vpb_len = sizeof((req));			\
     61  1.1  jmcneill 		(req).vb_hdr.vpb_rcode = VCPROP_PROCESS_REQUEST;	\
     62  1.1  jmcneill 		(req).end.vpt_tag = VCPROPTAG_NULL;			\
     63  1.1  jmcneill 	} while (0)
     64  1.1  jmcneill #define VCMBOX_INIT_TAG(s, t)						\
     65  1.1  jmcneill 	do {								\
     66  1.1  jmcneill 		(s).tag.vpt_tag = (t);					\
     67  1.1  jmcneill 		(s).tag.vpt_rcode = VCPROPTAG_REQUEST;			\
     68  1.1  jmcneill 		(s).tag.vpt_len = VCPROPTAG_LEN(s);			\
     69  1.1  jmcneill 	} while (0)
     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.1  jmcneill };
     80  1.1  jmcneill 
     81  1.1  jmcneill static const char *vcmbox_sensor_name[VCMBOX_NSENSORS] = {
     82  1.1  jmcneill 	"temperature",
     83  1.1  jmcneill };
     84  1.1  jmcneill 
     85  1.1  jmcneill static int vcmbox_sensor_id[VCMBOX_NSENSORS] = {
     86  1.1  jmcneill 	VCPROP_TEMP_SOC,
     87  1.1  jmcneill };
     88  1.1  jmcneill 
     89  1.1  jmcneill static int	vcmbox_match(device_t, cfdata_t, void *);
     90  1.1  jmcneill static void	vcmbox_attach(device_t, device_t, void *);
     91  1.1  jmcneill 
     92  1.1  jmcneill static void	vcmbox_create_sensors(struct vcmbox_softc *);
     93  1.1  jmcneill static int	vcmbox_read_temp(struct vcmbox_softc *, uint32_t, int,
     94  1.1  jmcneill 				 uint32_t *);
     95  1.1  jmcneill static void	vcmbox_sensor_get_limits(struct sysmon_envsys *,
     96  1.1  jmcneill 					 envsys_data_t *,
     97  1.1  jmcneill 					 sysmon_envsys_lim_t *, uint32_t *);
     98  1.1  jmcneill static void	vcmbox_sensor_refresh(struct sysmon_envsys *,
     99  1.1  jmcneill 				      envsys_data_t *);
    100  1.1  jmcneill 
    101  1.1  jmcneill CFATTACH_DECL_NEW(vcmbox, sizeof(struct vcmbox_softc),
    102  1.1  jmcneill     vcmbox_match, vcmbox_attach, NULL, NULL);
    103  1.1  jmcneill 
    104  1.1  jmcneill static int
    105  1.1  jmcneill vcmbox_match(device_t parent, cfdata_t match, void *aux)
    106  1.1  jmcneill {
    107  1.1  jmcneill 	return 1;
    108  1.1  jmcneill }
    109  1.1  jmcneill 
    110  1.1  jmcneill static void
    111  1.1  jmcneill vcmbox_attach(device_t parent, device_t self, void *aux)
    112  1.1  jmcneill {
    113  1.1  jmcneill 	struct vcmbox_softc *sc = device_private(self);
    114  1.1  jmcneill 
    115  1.1  jmcneill 	sc->sc_dev = self;
    116  1.1  jmcneill 
    117  1.1  jmcneill 	aprint_naive("\n");
    118  1.1  jmcneill 	aprint_normal("\n");
    119  1.1  jmcneill 
    120  1.1  jmcneill 	sc->sc_sme = sysmon_envsys_create();
    121  1.1  jmcneill 	sc->sc_sme->sme_cookie = sc;
    122  1.1  jmcneill 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    123  1.1  jmcneill 	sc->sc_sme->sme_refresh = vcmbox_sensor_refresh;
    124  1.1  jmcneill 	sc->sc_sme->sme_get_limits = vcmbox_sensor_get_limits;
    125  1.1  jmcneill 	vcmbox_create_sensors(sc);
    126  1.1  jmcneill 	sysmon_envsys_register(sc->sc_sme);
    127  1.1  jmcneill }
    128  1.1  jmcneill 
    129  1.1  jmcneill static void
    130  1.1  jmcneill vcmbox_create_sensors(struct vcmbox_softc *sc)
    131  1.1  jmcneill {
    132  1.1  jmcneill 	uint32_t val;
    133  1.1  jmcneill 
    134  1.1  jmcneill 	sc->sc_sensor[VCMBOX_SENSOR_TEMP].sensor = VCMBOX_SENSOR_TEMP;
    135  1.1  jmcneill 	sc->sc_sensor[VCMBOX_SENSOR_TEMP].units = ENVSYS_STEMP;
    136  1.1  jmcneill 	sc->sc_sensor[VCMBOX_SENSOR_TEMP].state = ENVSYS_SINVALID;
    137  1.1  jmcneill 	sc->sc_sensor[VCMBOX_SENSOR_TEMP].flags = ENVSYS_FHAS_ENTROPY;
    138  1.1  jmcneill 	strlcpy(sc->sc_sensor[VCMBOX_SENSOR_TEMP].desc,
    139  1.1  jmcneill 	    vcmbox_sensor_name[VCMBOX_SENSOR_TEMP],
    140  1.1  jmcneill 	    sizeof(sc->sc_sensor[VCMBOX_SENSOR_TEMP].desc));
    141  1.1  jmcneill 	if (vcmbox_read_temp(sc, VCPROPTAG_GET_MAX_TEMPERATURE,
    142  1.1  jmcneill 			     vcmbox_sensor_id[VCMBOX_SENSOR_TEMP], &val) == 0) {
    143  1.1  jmcneill 		sc->sc_sensor[VCMBOX_SENSOR_TEMP].value_max =
    144  1.1  jmcneill 		    val * 1000 + 273150000;
    145  1.1  jmcneill 		sc->sc_sensor[VCMBOX_SENSOR_TEMP].flags |= ENVSYS_FVALID_MAX;
    146  1.1  jmcneill 	}
    147  1.1  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme,
    148  1.1  jmcneill 	    &sc->sc_sensor[VCMBOX_SENSOR_TEMP]);
    149  1.1  jmcneill }
    150  1.1  jmcneill 
    151  1.1  jmcneill static int
    152  1.1  jmcneill vcmbox_read_temp(struct vcmbox_softc *sc, uint32_t tag, int id, uint32_t *val)
    153  1.1  jmcneill {
    154  1.1  jmcneill 	struct vcmbox_temp_request vb;
    155  1.1  jmcneill 	uint32_t res;
    156  1.1  jmcneill 	int error;
    157  1.1  jmcneill 
    158  1.1  jmcneill 	VCMBOX_INIT_REQUEST(vb);
    159  1.1  jmcneill 	VCMBOX_INIT_TAG(vb.vbt_temp, tag);
    160  1.1  jmcneill 	vb.vbt_temp.id = id;
    161  1.1  jmcneill 	error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb, sizeof(vb), &res);
    162  1.1  jmcneill 	if (error)
    163  1.1  jmcneill 		return error;
    164  1.1  jmcneill 	if (!vcprop_buffer_success_p(&vb.vb_hdr) ||
    165  1.1  jmcneill 	    !vcprop_tag_success_p(&vb.vbt_temp.tag)) {
    166  1.1  jmcneill 		return EIO;
    167  1.1  jmcneill 	}
    168  1.1  jmcneill 	*val = vb.vbt_temp.value;
    169  1.1  jmcneill 
    170  1.1  jmcneill 	return 0;
    171  1.1  jmcneill }
    172  1.1  jmcneill 
    173  1.1  jmcneill static void
    174  1.1  jmcneill vcmbox_sensor_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    175  1.1  jmcneill     sysmon_envsys_lim_t *limits, uint32_t *props)
    176  1.1  jmcneill {
    177  1.1  jmcneill 	struct vcmbox_softc *sc = sme->sme_cookie;
    178  1.1  jmcneill 	uint32_t val;
    179  1.1  jmcneill 
    180  1.1  jmcneill 	*props = 0;
    181  1.1  jmcneill 
    182  1.1  jmcneill 	if (edata->units == ENVSYS_STEMP) {
    183  1.1  jmcneill 		if (vcmbox_read_temp(sc, VCPROPTAG_GET_MAX_TEMPERATURE,
    184  1.1  jmcneill 				     vcmbox_sensor_id[edata->sensor], &val))
    185  1.1  jmcneill 			return;
    186  1.1  jmcneill 		*props = PROP_CRITMAX;
    187  1.1  jmcneill 		limits->sel_critmax = val * 1000 + 273150000;
    188  1.1  jmcneill 	}
    189  1.1  jmcneill }
    190  1.1  jmcneill 
    191  1.1  jmcneill static void
    192  1.1  jmcneill vcmbox_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    193  1.1  jmcneill {
    194  1.1  jmcneill 	struct vcmbox_softc *sc = sme->sme_cookie;
    195  1.1  jmcneill 	uint32_t val;
    196  1.1  jmcneill 
    197  1.1  jmcneill 	edata->state = ENVSYS_SINVALID;
    198  1.1  jmcneill 
    199  1.1  jmcneill 	if (edata->units == ENVSYS_STEMP) {
    200  1.1  jmcneill 		if (vcmbox_read_temp(sc, VCPROPTAG_GET_TEMPERATURE,
    201  1.1  jmcneill 				     vcmbox_sensor_id[edata->sensor], &val))
    202  1.1  jmcneill 			return;
    203  1.1  jmcneill 
    204  1.1  jmcneill 		edata->value_cur = val * 1000 + 273150000;
    205  1.1  jmcneill 		edata->state = ENVSYS_SVALID;
    206  1.1  jmcneill 	}
    207  1.1  jmcneill }
    208