Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_cm.c revision 1.1
      1  1.1  mlelstv /*	$NetBSD: bcm2835_cm.c,v 1.1 2015/11/21 07:41:29 mlelstv Exp $ */
      2  1.1  mlelstv 
      3  1.1  mlelstv /*-
      4  1.1  mlelstv  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5  1.1  mlelstv  * All rights reserved.
      6  1.1  mlelstv  *
      7  1.1  mlelstv  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  mlelstv  * by Michael van Elst
      9  1.1  mlelstv  *
     10  1.1  mlelstv  * Redistribution and use in source and binary forms, with or without
     11  1.1  mlelstv  * modification, are permitted provided that the following conditions
     12  1.1  mlelstv  * are met:
     13  1.1  mlelstv  * 1. Redistributions of source code must retain the above copyright
     14  1.1  mlelstv  *    notice, this list of conditions and the following disclaimer.
     15  1.1  mlelstv  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  mlelstv  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  mlelstv  *    documentation and/or other materials provided with the distribution.
     18  1.1  mlelstv  *
     19  1.1  mlelstv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  mlelstv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  mlelstv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  mlelstv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  mlelstv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  mlelstv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  mlelstv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  mlelstv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  mlelstv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  mlelstv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  mlelstv  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  mlelstv  */
     31  1.1  mlelstv 
     32  1.1  mlelstv /*
     33  1.1  mlelstv  * Driver for BCM2835 Clock Manager
     34  1.1  mlelstv  */
     35  1.1  mlelstv 
     36  1.1  mlelstv #include <sys/cdefs.h>
     37  1.1  mlelstv __KERNEL_RCSID(0, "$NetBSD: bcm2835_cm.c,v 1.1 2015/11/21 07:41:29 mlelstv Exp $");
     38  1.1  mlelstv 
     39  1.1  mlelstv #include <sys/param.h>
     40  1.1  mlelstv #include <sys/systm.h>
     41  1.1  mlelstv #include <sys/device.h>
     42  1.1  mlelstv #include <sys/kernel.h>
     43  1.1  mlelstv #include <sys/bus.h>
     44  1.1  mlelstv 
     45  1.1  mlelstv #include <arm/broadcom/bcm2835reg.h>
     46  1.1  mlelstv #include <arm/broadcom/bcm_amba.h>
     47  1.1  mlelstv 
     48  1.1  mlelstv #include <arm/broadcom/bcm2835_cm.h>
     49  1.1  mlelstv 
     50  1.1  mlelstv struct bcm2835cm_softc {
     51  1.1  mlelstv 	device_t		sc_dev;
     52  1.1  mlelstv 
     53  1.1  mlelstv 	bus_space_tag_t		sc_iot;
     54  1.1  mlelstv 	bus_space_handle_t	sc_ioh;
     55  1.1  mlelstv };
     56  1.1  mlelstv 
     57  1.1  mlelstv #define CM_WRITE(sc, reg, val) \
     58  1.1  mlelstv 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
     59  1.1  mlelstv #define CM_READ(sc, reg) \
     60  1.1  mlelstv 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
     61  1.1  mlelstv 
     62  1.1  mlelstv static int bcmcm_match(device_t, cfdata_t, void *);
     63  1.1  mlelstv static void bcmcm_attach(device_t, device_t, void *);
     64  1.1  mlelstv static int bcmcm_wait(struct bcm2835cm_softc *, int, int);
     65  1.1  mlelstv 
     66  1.1  mlelstv CFATTACH_DECL_NEW(bcmcm_amba, sizeof(struct bcm2835cm_softc),
     67  1.1  mlelstv     bcmcm_match, bcmcm_attach, NULL, NULL);
     68  1.1  mlelstv 
     69  1.1  mlelstv /* ARGSUSED */
     70  1.1  mlelstv static int
     71  1.1  mlelstv bcmcm_match(device_t parent, cfdata_t match, void *aux)
     72  1.1  mlelstv {
     73  1.1  mlelstv 	struct amba_attach_args *aaa = aux;
     74  1.1  mlelstv 
     75  1.1  mlelstv 	if (strcmp(aaa->aaa_name, "bcmcm") != 0)
     76  1.1  mlelstv 		return 0;
     77  1.1  mlelstv 
     78  1.1  mlelstv 	if (aaa->aaa_addr != BCM2835_CM_BASE)
     79  1.1  mlelstv 		return 0;
     80  1.1  mlelstv 
     81  1.1  mlelstv 	return 1;
     82  1.1  mlelstv }
     83  1.1  mlelstv 
     84  1.1  mlelstv static void
     85  1.1  mlelstv bcmcm_attach(device_t parent, device_t self, void *aux)
     86  1.1  mlelstv {
     87  1.1  mlelstv 	struct bcm2835cm_softc *sc = device_private(self);
     88  1.1  mlelstv  	struct amba_attach_args *aaa = aux;
     89  1.1  mlelstv 
     90  1.1  mlelstv 	aprint_naive("\n");
     91  1.1  mlelstv 	aprint_normal(": CM\n");
     92  1.1  mlelstv 
     93  1.1  mlelstv 	sc->sc_dev = self;
     94  1.1  mlelstv 	sc->sc_iot = aaa->aaa_iot;
     95  1.1  mlelstv 
     96  1.1  mlelstv 	if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, BCM2835_CM_SIZE, 0,
     97  1.1  mlelstv 	    &sc->sc_ioh)) {
     98  1.1  mlelstv 		aprint_error_dev(sc->sc_dev, "unable to map device\n");
     99  1.1  mlelstv 		goto fail0;
    100  1.1  mlelstv 	}
    101  1.1  mlelstv 
    102  1.1  mlelstv 	/* Success!  */
    103  1.1  mlelstv 
    104  1.1  mlelstv fail0:	return;
    105  1.1  mlelstv }
    106  1.1  mlelstv 
    107  1.1  mlelstv static int
    108  1.1  mlelstv bcmcm_wait(struct bcm2835cm_softc *sc, int ctlreg, int onoff)
    109  1.1  mlelstv {
    110  1.1  mlelstv 	int i;
    111  1.1  mlelstv 	uint32_t r;
    112  1.1  mlelstv 
    113  1.1  mlelstv 	for (i=0; i<100; ++i) {
    114  1.1  mlelstv 		r = CM_READ(sc, ctlreg);
    115  1.1  mlelstv 		if (((r & CM_CTL_BUSY) != 0) == onoff)
    116  1.1  mlelstv 			break;
    117  1.1  mlelstv 		delay(10);
    118  1.1  mlelstv 	}
    119  1.1  mlelstv 	if (i >= 100) {
    120  1.1  mlelstv 		device_printf(sc->sc_dev, "busy (addr=%#x)\n", ctlreg);
    121  1.1  mlelstv 		return EIO;
    122  1.1  mlelstv 	}
    123  1.1  mlelstv 
    124  1.1  mlelstv 	return 0;
    125  1.1  mlelstv }
    126  1.1  mlelstv 
    127  1.1  mlelstv int
    128  1.1  mlelstv bcm_cm_set(enum bcm_cm_clock clk, uint32_t ctl, uint32_t div)
    129  1.1  mlelstv {
    130  1.1  mlelstv 	struct bcm2835cm_softc *sc;
    131  1.1  mlelstv 	device_t dev;
    132  1.1  mlelstv 	int ctlreg, divreg;
    133  1.1  mlelstv 	uint32_t r;
    134  1.1  mlelstv 
    135  1.1  mlelstv 	dev = device_find_by_driver_unit("bcmcm", 0);
    136  1.1  mlelstv 	if (dev == NULL)
    137  1.1  mlelstv 		return ENXIO;
    138  1.1  mlelstv 	sc = device_private(dev);
    139  1.1  mlelstv 
    140  1.1  mlelstv 	switch (clk) {
    141  1.1  mlelstv 	case BCM_CM_GP0:
    142  1.1  mlelstv 		ctlreg = CM_GP0CTL;
    143  1.1  mlelstv 		divreg = CM_GP0DIV;
    144  1.1  mlelstv 		break;
    145  1.1  mlelstv 	case BCM_CM_GP1:
    146  1.1  mlelstv 		ctlreg = CM_GP1CTL;
    147  1.1  mlelstv 		divreg = CM_GP1DIV;
    148  1.1  mlelstv 		break;
    149  1.1  mlelstv 	case BCM_CM_GP2:
    150  1.1  mlelstv 		ctlreg = CM_GP2CTL;
    151  1.1  mlelstv 		divreg = CM_GP2DIV;
    152  1.1  mlelstv 		break;
    153  1.1  mlelstv 	case BCM_CM_PCM:
    154  1.1  mlelstv 		ctlreg = CM_PCMCTL;
    155  1.1  mlelstv 		divreg = CM_PCMDIV;
    156  1.1  mlelstv 		break;
    157  1.1  mlelstv 	case BCM_CM_PWM:
    158  1.1  mlelstv 		ctlreg = CM_PWMCTL;
    159  1.1  mlelstv 		divreg = CM_PWMDIV;
    160  1.1  mlelstv 		break;
    161  1.1  mlelstv 	default:
    162  1.1  mlelstv 		return EINVAL;
    163  1.1  mlelstv 	}
    164  1.1  mlelstv 
    165  1.1  mlelstv 	ctl &= ~CM_CTL_PASSWD;
    166  1.1  mlelstv 	ctl |= __SHIFTIN(CM_PASSWD, CM_CTL_PASSWD);
    167  1.1  mlelstv 	div &= ~CM_DIV_PASSWD;
    168  1.1  mlelstv 	div |= __SHIFTIN(CM_PASSWD, CM_DIV_PASSWD);
    169  1.1  mlelstv 
    170  1.1  mlelstv 	/* if clock is running, turn it off and wait for
    171  1.1  mlelstv 	 * the cycle to end
    172  1.1  mlelstv 	 */
    173  1.1  mlelstv 	r = CM_READ(sc, ctlreg);
    174  1.1  mlelstv 	if (r & CM_CTL_ENAB) {
    175  1.1  mlelstv 		r &= ~CM_CTL_PASSWD;
    176  1.1  mlelstv 		r |= __SHIFTIN(CM_PASSWD, CM_CTL_PASSWD);
    177  1.1  mlelstv 		r &= ~CM_CTL_ENAB;
    178  1.1  mlelstv 		CM_WRITE(sc, ctlreg, r);
    179  1.1  mlelstv 	}
    180  1.1  mlelstv 
    181  1.1  mlelstv 	bcmcm_wait(sc, ctlreg, 0);
    182  1.1  mlelstv 
    183  1.1  mlelstv 	/* configure new divider, mode, don't enable */
    184  1.1  mlelstv 	CM_WRITE(sc, divreg, div);
    185  1.1  mlelstv 	CM_WRITE(sc, ctlreg, ctl & ~CM_CTL_ENAB);
    186  1.1  mlelstv 
    187  1.1  mlelstv 	/* enable it */
    188  1.1  mlelstv 	if (ctl & CM_CTL_ENAB) {
    189  1.1  mlelstv 		CM_WRITE(sc, ctlreg, ctl);
    190  1.1  mlelstv 		return bcmcm_wait(sc, ctlreg, 1);
    191  1.1  mlelstv 	}
    192  1.1  mlelstv 
    193  1.1  mlelstv 	return 0;
    194  1.1  mlelstv }
    195  1.1  mlelstv 
    196  1.1  mlelstv int
    197  1.1  mlelstv bcm_cm_get(enum bcm_cm_clock clk, uint32_t *ctlp, uint32_t *divp)
    198  1.1  mlelstv {
    199  1.1  mlelstv 	struct bcm2835cm_softc *sc;
    200  1.1  mlelstv 	device_t dev;
    201  1.1  mlelstv 	int ctlreg, divreg;
    202  1.1  mlelstv 
    203  1.1  mlelstv 	dev = device_find_by_driver_unit("bcmcm", 0);
    204  1.1  mlelstv 	if (dev == NULL)
    205  1.1  mlelstv 		return ENXIO;
    206  1.1  mlelstv 	sc = device_private(dev);
    207  1.1  mlelstv 
    208  1.1  mlelstv 	switch (clk) {
    209  1.1  mlelstv 	case BCM_CM_GP0:
    210  1.1  mlelstv 		ctlreg = CM_GP0CTL;
    211  1.1  mlelstv 		divreg = CM_GP0DIV;
    212  1.1  mlelstv 		break;
    213  1.1  mlelstv 	case BCM_CM_GP1:
    214  1.1  mlelstv 		ctlreg = CM_GP1CTL;
    215  1.1  mlelstv 		divreg = CM_GP1DIV;
    216  1.1  mlelstv 		break;
    217  1.1  mlelstv 	case BCM_CM_GP2:
    218  1.1  mlelstv 		ctlreg = CM_GP2CTL;
    219  1.1  mlelstv 		divreg = CM_GP2DIV;
    220  1.1  mlelstv 		break;
    221  1.1  mlelstv 	case BCM_CM_PCM:
    222  1.1  mlelstv 		ctlreg = CM_PCMCTL;
    223  1.1  mlelstv 		divreg = CM_PCMDIV;
    224  1.1  mlelstv 		break;
    225  1.1  mlelstv 	case BCM_CM_PWM:
    226  1.1  mlelstv 		ctlreg = CM_PWMCTL;
    227  1.1  mlelstv 		divreg = CM_PWMDIV;
    228  1.1  mlelstv 		break;
    229  1.1  mlelstv 	default:
    230  1.1  mlelstv 		return EINVAL;
    231  1.1  mlelstv 	}
    232  1.1  mlelstv 
    233  1.1  mlelstv 	if (ctlp != NULL)
    234  1.1  mlelstv 		*ctlp = CM_READ(sc, ctlreg);
    235  1.1  mlelstv 	if (divp != NULL)
    236  1.1  mlelstv 		*divp = CM_READ(sc, divreg);
    237  1.1  mlelstv 
    238  1.1  mlelstv 	return 0;
    239  1.1  mlelstv }
    240