fixedclock.c revision 1.6
1/* $NetBSD: fixedclock.c,v 1.6 2021/01/27 03:10:21 thorpej 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: fixedclock.c,v 1.6 2021/01/27 03:10:21 thorpej 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 42static int fixedclock_match(device_t, cfdata_t, void *); 43static void fixedclock_attach(device_t, device_t, void *); 44 45static struct clk *fixedclock_decode(device_t, int, const void *, size_t); 46 47static const struct fdtbus_clock_controller_func fixedclock_fdt_funcs = { 48 .decode = fixedclock_decode 49}; 50 51static struct clk *fixedclock_get(void *, const char *); 52static void fixedclock_put(void *, struct clk *); 53static u_int fixedclock_get_rate(void *, struct clk *); 54 55static const struct clk_funcs fixedclock_clk_funcs = { 56 .get = fixedclock_get, 57 .put = fixedclock_put, 58 .get_rate = fixedclock_get_rate, 59}; 60 61struct fixedclock_clk { 62 struct clk base; 63 u_int rate; 64}; 65 66struct fixedclock_softc { 67 device_t sc_dev; 68 int sc_phandle; 69 70 struct clk_domain sc_clkdom; 71 struct fixedclock_clk sc_clk; 72}; 73 74CFATTACH_DECL_NEW(fclock, sizeof(struct fixedclock_softc), 75 fixedclock_match, fixedclock_attach, NULL, NULL); 76 77static const struct device_compatible_entry compat_data[] = { 78 { .compat = "fixed-clock" }, 79 DEVICE_COMPAT_EOL 80}; 81 82static int 83fixedclock_match(device_t parent, cfdata_t cf, void *aux) 84{ 85 const struct fdt_attach_args *faa = aux; 86 87 return of_compatible_match(faa->faa_phandle, compat_data); 88} 89 90static void 91fixedclock_attach(device_t parent, device_t self, void *aux) 92{ 93 struct fixedclock_softc * const sc = device_private(self); 94 const struct fdt_attach_args *faa = aux; 95 const int phandle = faa->faa_phandle; 96 const char *clkname; 97 98 clkname = fdtbus_get_string(phandle, "clock-output-names"); 99 if (!clkname) 100 clkname = faa->faa_name; 101 102 sc->sc_dev = self; 103 sc->sc_phandle = phandle; 104 sc->sc_clkdom.name = device_xname(self); 105 sc->sc_clkdom.funcs = &fixedclock_clk_funcs; 106 sc->sc_clkdom.priv = sc; 107 if (of_getprop_uint32(phandle, "clock-frequency", 108 &sc->sc_clk.rate) != 0) { 109 aprint_error(": couldn't determine frequency\n"); 110 return; 111 } 112 sc->sc_clk.base.domain = &sc->sc_clkdom; 113 sc->sc_clk.base.name = kmem_asprintf("%s", clkname); 114 clk_attach(&sc->sc_clk.base); 115 116 aprint_naive("\n"); 117 aprint_normal(": %u Hz fixed clock (%s)\n", sc->sc_clk.rate, clkname); 118 119 fdtbus_register_clock_controller(self, phandle, &fixedclock_fdt_funcs); 120} 121 122static struct clk * 123fixedclock_decode(device_t dev, int cc_phandle, const void *data, size_t len) 124{ 125 struct fixedclock_softc * const sc = device_private(dev); 126 127 /* #clock-cells for a fixed clock is always 0 */ 128 if (len != 0) 129 return NULL; 130 131 return &sc->sc_clk.base; 132} 133 134static struct clk * 135fixedclock_get(void *priv, const char *name) 136{ 137 struct fixedclock_softc * const sc = priv; 138 139 if (strcmp(name, sc->sc_clk.base.name) != 0) 140 return NULL; 141 142 return &sc->sc_clk.base; 143} 144 145static void 146fixedclock_put(void *priv, struct clk *clk) 147{ 148} 149 150static u_int 151fixedclock_get_rate(void *priv, struct clk *clk) 152{ 153 struct fixedclock_clk *fclk = (struct fixedclock_clk *)clk; 154 155 return fclk->rate; 156} 157