1 /* $NetBSD: bcm2835_cprman.c,v 1.1 2017/12/10 21:38:26 skrll Exp $ */ 2 3 /*- 4 * Copyright (c) 2017 Jared D. McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: bcm2835_cprman.c,v 1.1 2017/12/10 21:38:26 skrll Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/device.h> 35 #include <sys/kmem.h> 36 #include <sys/bus.h> 37 38 #include <dev/clk/clk_backend.h> 39 40 #include <dev/fdt/fdtvar.h> 41 42 #include <arm/broadcom/bcm2835var.h> 43 44 enum { 45 CPRMAN_CLOCK_TIMER = 17, 46 CPRMAN_CLOCK_UART = 19, 47 CPRMAN_CLOCK_VPU = 20, 48 CPRMAN_CLOCK_V3D = 21, 49 CPRMAN_CLOCK_ISP = 22, 50 CPRMAN_CLOCK_H264 = 23, 51 CPRMAN_CLOCK_VEC = 24, 52 CPRMAN_CLOCK_HSM = 25, 53 CPRMAN_CLOCK_SDRAM = 26, 54 CPRMAN_CLOCK_TSENS = 27, 55 CPRMAN_CLOCK_EMMC = 28, 56 CPRMAN_CLOCK_PERIIMAGE = 29, 57 CPRMAN_CLOCK_PWM = 30, 58 CPRMAN_CLOCK_PCM = 31, 59 CPRMAN_NCLOCK 60 }; 61 62 struct cprman_clk { 63 struct clk base; 64 u_int id; 65 }; 66 67 struct cprman_softc { 68 device_t sc_dev; 69 int sc_phandle; 70 71 struct clk_domain sc_clkdom; 72 struct cprman_clk sc_clk[CPRMAN_NCLOCK]; 73 }; 74 75 76 static struct clk * 77 cprman_decode(device_t dev, const void *data, size_t len) 78 { 79 struct cprman_softc * const sc = device_private(dev); 80 struct cprman_clk *clk; 81 const u_int *spec = data; 82 u_int id; 83 84 if (len != 4) 85 return NULL; 86 87 id = be32toh(spec[0]); 88 89 if (id >= CPRMAN_NCLOCK) 90 return NULL; 91 clk = &sc->sc_clk[id]; 92 if (clk->base.name == NULL) 93 return NULL; 94 95 return &clk->base; 96 } 97 98 static const struct fdtbus_clock_controller_func cprman_fdt_funcs = { 99 .decode = cprman_decode 100 }; 101 102 static struct clk * 103 cprman_get(void *priv, const char *name) 104 { 105 struct cprman_softc * const sc = priv; 106 u_int n; 107 108 for (n = 0; n < __arraycount(sc->sc_clk); n++) { 109 if (sc->sc_clk[n].base.name == NULL) 110 continue; 111 if (strcmp(sc->sc_clk[n].base.name, name) == 0) 112 return &sc->sc_clk[n].base; 113 } 114 115 return NULL; 116 } 117 118 static void 119 cprman_put(void *priv, struct clk *clk) 120 { 121 } 122 123 static u_int 124 cprman_get_rate(void *priv, struct clk *baseclk) 125 { 126 //struct cprman_softc * const sc = priv; 127 struct cprman_clk *clk = container_of(baseclk, struct cprman_clk, base); 128 129 switch (clk->id) { 130 case CPRMAN_CLOCK_UART: 131 return bcm283x_clk_get_rate_uart(); 132 case CPRMAN_CLOCK_VPU: 133 return bcm283x_clk_get_rate_vpu(); 134 case CPRMAN_CLOCK_EMMC: 135 return bcm283x_clk_get_rate_emmc(); 136 default: 137 panic("unsupported clock id %d\n", clk->id); 138 } 139 } 140 141 static const struct clk_funcs cprman_clk_funcs = { 142 .get = cprman_get, 143 .put = cprman_put, 144 .get_rate = cprman_get_rate, 145 }; 146 147 static void 148 cprman_add_clock(struct cprman_softc *sc, u_int id, const char *name) 149 { 150 sc->sc_clk[id].base.domain = &sc->sc_clkdom; 151 sc->sc_clk[id].base.name = name; 152 sc->sc_clk[id].id = id; 153 } 154 155 static int 156 cprman_match(device_t parent, cfdata_t cf, void *aux) 157 { 158 const char * const compatible[] = { "brcm,bcm2835-cprman", NULL }; 159 const struct fdt_attach_args *faa = aux; 160 161 return of_match_compatible(faa->faa_phandle, compatible); 162 } 163 164 static void 165 cprman_attach(device_t parent, device_t self, void *aux) 166 { 167 struct cprman_softc * const sc = device_private(self); 168 const struct fdt_attach_args *faa = aux; 169 const int phandle = faa->faa_phandle; 170 171 sc->sc_dev = self; 172 sc->sc_phandle = phandle; 173 sc->sc_clkdom.funcs = &cprman_clk_funcs; 174 sc->sc_clkdom.priv = sc; 175 176 cprman_add_clock(sc, CPRMAN_CLOCK_UART, "uart"); 177 cprman_add_clock(sc, CPRMAN_CLOCK_VPU, "vpu"); 178 cprman_add_clock(sc, CPRMAN_CLOCK_EMMC, "emmc"); 179 180 aprint_naive("\n"); 181 aprint_normal(": BCM283x Clock Controller\n"); 182 183 fdtbus_register_clock_controller(self, phandle, &cprman_fdt_funcs); 184 } 185 186 CFATTACH_DECL_NEW(bcmcprman_fdt, sizeof(struct cprman_softc), 187 cprman_match, cprman_attach, NULL, NULL); 188