Home | History | Annotate | Line # | Download | only in broadcom
      1  1.3  thorpej /*	$NetBSD: bcm2835_cm.c,v 1.3 2021/01/27 03:10:19 thorpej 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.3  thorpej __KERNEL_RCSID(0, "$NetBSD: bcm2835_cm.c,v 1.3 2021/01/27 03:10:19 thorpej 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.2    skrll #include <arm/broadcom/bcm2835_cm.h>
     47  1.2    skrll 
     48  1.2    skrll #include <dev/fdt/fdtvar.h>
     49  1.1  mlelstv 
     50  1.2    skrll #include <arm/fdt/arm_fdtvar.h>
     51  1.1  mlelstv 
     52  1.1  mlelstv struct bcm2835cm_softc {
     53  1.1  mlelstv 	device_t		sc_dev;
     54  1.1  mlelstv 
     55  1.1  mlelstv 	bus_space_tag_t		sc_iot;
     56  1.1  mlelstv 	bus_space_handle_t	sc_ioh;
     57  1.1  mlelstv };
     58  1.1  mlelstv 
     59  1.1  mlelstv #define CM_WRITE(sc, reg, val) \
     60  1.1  mlelstv 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
     61  1.1  mlelstv #define CM_READ(sc, reg) \
     62  1.1  mlelstv 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
     63  1.1  mlelstv 
     64  1.1  mlelstv static int bcmcm_match(device_t, cfdata_t, void *);
     65  1.1  mlelstv static void bcmcm_attach(device_t, device_t, void *);
     66  1.1  mlelstv static int bcmcm_wait(struct bcm2835cm_softc *, int, int);
     67  1.1  mlelstv 
     68  1.2    skrll CFATTACH_DECL_NEW(bcmcm_fdt, sizeof(struct bcm2835cm_softc),
     69  1.1  mlelstv     bcmcm_match, bcmcm_attach, NULL, NULL);
     70  1.1  mlelstv 
     71  1.3  thorpej static const struct device_compatible_entry compat_data[] = {
     72  1.3  thorpej 	{ .compat = "brcm,bcm2835-cprman" },
     73  1.3  thorpej 	DEVICE_COMPAT_EOL
     74  1.3  thorpej };
     75  1.3  thorpej 
     76  1.1  mlelstv /* ARGSUSED */
     77  1.1  mlelstv static int
     78  1.1  mlelstv bcmcm_match(device_t parent, cfdata_t match, void *aux)
     79  1.1  mlelstv {
     80  1.2    skrll 	struct fdt_attach_args * const faa = aux;
     81  1.1  mlelstv 
     82  1.3  thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
     83  1.1  mlelstv }
     84  1.1  mlelstv 
     85  1.1  mlelstv static void
     86  1.1  mlelstv bcmcm_attach(device_t parent, device_t self, void *aux)
     87  1.1  mlelstv {
     88  1.1  mlelstv 	struct bcm2835cm_softc *sc = device_private(self);
     89  1.2    skrll 	struct fdt_attach_args * const faa = aux;
     90  1.1  mlelstv 
     91  1.1  mlelstv 	aprint_naive("\n");
     92  1.1  mlelstv 	aprint_normal(": CM\n");
     93  1.1  mlelstv 
     94  1.1  mlelstv 	sc->sc_dev = self;
     95  1.2    skrll 	sc->sc_iot = faa->faa_bst;
     96  1.2    skrll 	const int phandle = faa->faa_phandle;
     97  1.2    skrll 
     98  1.2    skrll 	bus_addr_t addr;
     99  1.2    skrll 	bus_size_t size;
    100  1.2    skrll 
    101  1.2    skrll 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    102  1.2    skrll 		aprint_error(": missing 'reg' property\n");
    103  1.2    skrll 		return;
    104  1.2    skrll 	}
    105  1.1  mlelstv 
    106  1.2    skrll 	if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_ioh)) {
    107  1.1  mlelstv 		aprint_error_dev(sc->sc_dev, "unable to map device\n");
    108  1.2    skrll 		return;
    109  1.1  mlelstv 	}
    110  1.1  mlelstv 
    111  1.1  mlelstv 	/* Success!  */
    112  1.1  mlelstv 
    113  1.2    skrll 	return;
    114  1.1  mlelstv }
    115  1.1  mlelstv 
    116  1.1  mlelstv static int
    117  1.1  mlelstv bcmcm_wait(struct bcm2835cm_softc *sc, int ctlreg, int onoff)
    118  1.1  mlelstv {
    119  1.1  mlelstv 	int i;
    120  1.1  mlelstv 	uint32_t r;
    121  1.1  mlelstv 
    122  1.1  mlelstv 	for (i=0; i<100; ++i) {
    123  1.1  mlelstv 		r = CM_READ(sc, ctlreg);
    124  1.1  mlelstv 		if (((r & CM_CTL_BUSY) != 0) == onoff)
    125  1.1  mlelstv 			break;
    126  1.1  mlelstv 		delay(10);
    127  1.1  mlelstv 	}
    128  1.1  mlelstv 	if (i >= 100) {
    129  1.1  mlelstv 		device_printf(sc->sc_dev, "busy (addr=%#x)\n", ctlreg);
    130  1.1  mlelstv 		return EIO;
    131  1.1  mlelstv 	}
    132  1.1  mlelstv 
    133  1.1  mlelstv 	return 0;
    134  1.1  mlelstv }
    135  1.1  mlelstv 
    136  1.1  mlelstv int
    137  1.1  mlelstv bcm_cm_set(enum bcm_cm_clock clk, uint32_t ctl, uint32_t div)
    138  1.1  mlelstv {
    139  1.1  mlelstv 	struct bcm2835cm_softc *sc;
    140  1.1  mlelstv 	device_t dev;
    141  1.1  mlelstv 	int ctlreg, divreg;
    142  1.1  mlelstv 	uint32_t r;
    143  1.1  mlelstv 
    144  1.1  mlelstv 	dev = device_find_by_driver_unit("bcmcm", 0);
    145  1.1  mlelstv 	if (dev == NULL)
    146  1.1  mlelstv 		return ENXIO;
    147  1.1  mlelstv 	sc = device_private(dev);
    148  1.1  mlelstv 
    149  1.1  mlelstv 	switch (clk) {
    150  1.1  mlelstv 	case BCM_CM_GP0:
    151  1.1  mlelstv 		ctlreg = CM_GP0CTL;
    152  1.1  mlelstv 		divreg = CM_GP0DIV;
    153  1.1  mlelstv 		break;
    154  1.1  mlelstv 	case BCM_CM_GP1:
    155  1.1  mlelstv 		ctlreg = CM_GP1CTL;
    156  1.1  mlelstv 		divreg = CM_GP1DIV;
    157  1.1  mlelstv 		break;
    158  1.1  mlelstv 	case BCM_CM_GP2:
    159  1.1  mlelstv 		ctlreg = CM_GP2CTL;
    160  1.1  mlelstv 		divreg = CM_GP2DIV;
    161  1.1  mlelstv 		break;
    162  1.1  mlelstv 	case BCM_CM_PCM:
    163  1.1  mlelstv 		ctlreg = CM_PCMCTL;
    164  1.1  mlelstv 		divreg = CM_PCMDIV;
    165  1.1  mlelstv 		break;
    166  1.1  mlelstv 	case BCM_CM_PWM:
    167  1.1  mlelstv 		ctlreg = CM_PWMCTL;
    168  1.1  mlelstv 		divreg = CM_PWMDIV;
    169  1.1  mlelstv 		break;
    170  1.1  mlelstv 	default:
    171  1.1  mlelstv 		return EINVAL;
    172  1.1  mlelstv 	}
    173  1.1  mlelstv 
    174  1.1  mlelstv 	ctl &= ~CM_CTL_PASSWD;
    175  1.1  mlelstv 	ctl |= __SHIFTIN(CM_PASSWD, CM_CTL_PASSWD);
    176  1.1  mlelstv 	div &= ~CM_DIV_PASSWD;
    177  1.1  mlelstv 	div |= __SHIFTIN(CM_PASSWD, CM_DIV_PASSWD);
    178  1.1  mlelstv 
    179  1.2    skrll 	/*
    180  1.2    skrll 	 * if clock is running, turn it off and wait for
    181  1.1  mlelstv 	 * the cycle to end
    182  1.1  mlelstv 	 */
    183  1.1  mlelstv 	r = CM_READ(sc, ctlreg);
    184  1.1  mlelstv 	if (r & CM_CTL_ENAB) {
    185  1.1  mlelstv 		r &= ~CM_CTL_PASSWD;
    186  1.1  mlelstv 		r |= __SHIFTIN(CM_PASSWD, CM_CTL_PASSWD);
    187  1.1  mlelstv 		r &= ~CM_CTL_ENAB;
    188  1.1  mlelstv 		CM_WRITE(sc, ctlreg, r);
    189  1.1  mlelstv 	}
    190  1.1  mlelstv 
    191  1.1  mlelstv 	bcmcm_wait(sc, ctlreg, 0);
    192  1.1  mlelstv 
    193  1.1  mlelstv 	/* configure new divider, mode, don't enable */
    194  1.1  mlelstv 	CM_WRITE(sc, divreg, div);
    195  1.1  mlelstv 	CM_WRITE(sc, ctlreg, ctl & ~CM_CTL_ENAB);
    196  1.1  mlelstv 
    197  1.1  mlelstv 	/* enable it */
    198  1.1  mlelstv 	if (ctl & CM_CTL_ENAB) {
    199  1.1  mlelstv 		CM_WRITE(sc, ctlreg, ctl);
    200  1.1  mlelstv 		return bcmcm_wait(sc, ctlreg, 1);
    201  1.1  mlelstv 	}
    202  1.1  mlelstv 
    203  1.1  mlelstv 	return 0;
    204  1.1  mlelstv }
    205  1.1  mlelstv 
    206  1.1  mlelstv int
    207  1.1  mlelstv bcm_cm_get(enum bcm_cm_clock clk, uint32_t *ctlp, uint32_t *divp)
    208  1.1  mlelstv {
    209  1.1  mlelstv 	struct bcm2835cm_softc *sc;
    210  1.1  mlelstv 	device_t dev;
    211  1.1  mlelstv 	int ctlreg, divreg;
    212  1.1  mlelstv 
    213  1.1  mlelstv 	dev = device_find_by_driver_unit("bcmcm", 0);
    214  1.1  mlelstv 	if (dev == NULL)
    215  1.1  mlelstv 		return ENXIO;
    216  1.1  mlelstv 	sc = device_private(dev);
    217  1.1  mlelstv 
    218  1.1  mlelstv 	switch (clk) {
    219  1.1  mlelstv 	case BCM_CM_GP0:
    220  1.1  mlelstv 		ctlreg = CM_GP0CTL;
    221  1.1  mlelstv 		divreg = CM_GP0DIV;
    222  1.1  mlelstv 		break;
    223  1.1  mlelstv 	case BCM_CM_GP1:
    224  1.1  mlelstv 		ctlreg = CM_GP1CTL;
    225  1.1  mlelstv 		divreg = CM_GP1DIV;
    226  1.1  mlelstv 		break;
    227  1.1  mlelstv 	case BCM_CM_GP2:
    228  1.1  mlelstv 		ctlreg = CM_GP2CTL;
    229  1.1  mlelstv 		divreg = CM_GP2DIV;
    230  1.1  mlelstv 		break;
    231  1.1  mlelstv 	case BCM_CM_PCM:
    232  1.1  mlelstv 		ctlreg = CM_PCMCTL;
    233  1.1  mlelstv 		divreg = CM_PCMDIV;
    234  1.1  mlelstv 		break;
    235  1.1  mlelstv 	case BCM_CM_PWM:
    236  1.1  mlelstv 		ctlreg = CM_PWMCTL;
    237  1.1  mlelstv 		divreg = CM_PWMDIV;
    238  1.1  mlelstv 		break;
    239  1.1  mlelstv 	default:
    240  1.1  mlelstv 		return EINVAL;
    241  1.1  mlelstv 	}
    242  1.1  mlelstv 
    243  1.1  mlelstv 	if (ctlp != NULL)
    244  1.1  mlelstv 		*ctlp = CM_READ(sc, ctlreg);
    245  1.1  mlelstv 	if (divp != NULL)
    246  1.1  mlelstv 		*divp = CM_READ(sc, divreg);
    247  1.1  mlelstv 
    248  1.1  mlelstv 	return 0;
    249  1.1  mlelstv }
    250