fixedclock.c revision 1.1
11.1Sjmcneill/* $NetBSD: fixedclock.c,v 1.1 2017/04/16 12:29:20 jmcneill Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2017 Jared D. McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
141.1Sjmcneill *    documentation and/or other materials provided with the distribution.
151.1Sjmcneill *
161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sjmcneill * SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include <sys/cdefs.h>
301.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: fixedclock.c,v 1.1 2017/04/16 12:29:20 jmcneill Exp $");
311.1Sjmcneill
321.1Sjmcneill#include <sys/param.h>
331.1Sjmcneill#include <sys/systm.h>
341.1Sjmcneill#include <sys/device.h>
351.1Sjmcneill#include <sys/kmem.h>
361.1Sjmcneill#include <sys/bus.h>
371.1Sjmcneill
381.1Sjmcneill#include <dev/clk/clk_backend.h>
391.1Sjmcneill
401.1Sjmcneill#include <dev/fdt/fdtvar.h>
411.1Sjmcneill
421.1Sjmcneillstatic int	fixedclock_match(device_t, cfdata_t, void *);
431.1Sjmcneillstatic void	fixedclock_attach(device_t, device_t, void *);
441.1Sjmcneill
451.1Sjmcneillstatic struct clk *fixedclock_decode(device_t, const void *, size_t);
461.1Sjmcneill
471.1Sjmcneillstatic const struct fdtbus_clock_controller_func fixedclock_fdt_funcs = {
481.1Sjmcneill	.decode = fixedclock_decode
491.1Sjmcneill};
501.1Sjmcneill
511.1Sjmcneillstatic struct clk *fixedclock_get(void *, const char *);
521.1Sjmcneillstatic void	fixedclock_put(void *, struct clk *);
531.1Sjmcneillstatic u_int	fixedclock_get_rate(void *, struct clk *);
541.1Sjmcneill
551.1Sjmcneillstatic const struct clk_funcs fixedclock_clk_funcs = {
561.1Sjmcneill	.get = fixedclock_get,
571.1Sjmcneill	.put = fixedclock_put,
581.1Sjmcneill	.get_rate = fixedclock_get_rate,
591.1Sjmcneill};
601.1Sjmcneill
611.1Sjmcneillstruct fixedclock_clk {
621.1Sjmcneill	struct clk	base;
631.1Sjmcneill	u_int		rate;
641.1Sjmcneill};
651.1Sjmcneill
661.1Sjmcneillstruct fixedclock_softc {
671.1Sjmcneill	device_t	sc_dev;
681.1Sjmcneill	int		sc_phandle;
691.1Sjmcneill
701.1Sjmcneill	struct clk_domain sc_clkdom;
711.1Sjmcneill	struct fixedclock_clk sc_clk;
721.1Sjmcneill};
731.1Sjmcneill
741.1SjmcneillCFATTACH_DECL_NEW(fclock, sizeof(struct fixedclock_softc),
751.1Sjmcneill    fixedclock_match, fixedclock_attach, NULL, NULL);
761.1Sjmcneill
771.1Sjmcneillstatic int
781.1Sjmcneillfixedclock_match(device_t parent, cfdata_t cf, void *aux)
791.1Sjmcneill{
801.1Sjmcneill	const char * const compatible[] = { "fixed-clock", NULL };
811.1Sjmcneill	const struct fdt_attach_args *faa = aux;
821.1Sjmcneill
831.1Sjmcneill	return of_match_compatible(faa->faa_phandle, compatible);
841.1Sjmcneill}
851.1Sjmcneill
861.1Sjmcneillstatic void
871.1Sjmcneillfixedclock_attach(device_t parent, device_t self, void *aux)
881.1Sjmcneill{
891.1Sjmcneill	struct fixedclock_softc * const sc = device_private(self);
901.1Sjmcneill	const struct fdt_attach_args *faa = aux;
911.1Sjmcneill	const int phandle = faa->faa_phandle;
921.1Sjmcneill
931.1Sjmcneill	sc->sc_dev = self;
941.1Sjmcneill	sc->sc_phandle = phandle;
951.1Sjmcneill	sc->sc_clkdom.funcs = &fixedclock_clk_funcs;
961.1Sjmcneill	sc->sc_clkdom.priv = sc;
971.1Sjmcneill	if (of_getprop_uint32(phandle, "clock-frequency",
981.1Sjmcneill	    &sc->sc_clk.rate) != 0) {
991.1Sjmcneill		aprint_error(": couldn't determine frequency\n");
1001.1Sjmcneill		return;
1011.1Sjmcneill	}
1021.1Sjmcneill	sc->sc_clk.base.domain = &sc->sc_clkdom;
1031.1Sjmcneill	sc->sc_clk.base.name = kmem_asprintf("%s", faa->faa_name);
1041.1Sjmcneill
1051.1Sjmcneill	aprint_naive(": %u Hz fixed clock (%s)\n", sc->sc_clk.rate,
1061.1Sjmcneill	    sc->sc_clk.base.name);
1071.1Sjmcneill
1081.1Sjmcneill	fdtbus_register_clock_controller(self, phandle, &fixedclock_fdt_funcs);
1091.1Sjmcneill}
1101.1Sjmcneill
1111.1Sjmcneillstatic struct clk *
1121.1Sjmcneillfixedclock_decode(device_t dev, const void *data, size_t len)
1131.1Sjmcneill{
1141.1Sjmcneill	struct fixedclock_softc * const sc = device_private(dev);
1151.1Sjmcneill
1161.1Sjmcneill	/* #clock-cells for a fixed clock is always 0 */
1171.1Sjmcneill	if (len != 0)
1181.1Sjmcneill		return NULL;
1191.1Sjmcneill
1201.1Sjmcneill	return &sc->sc_clk.base;
1211.1Sjmcneill}
1221.1Sjmcneill
1231.1Sjmcneillstatic struct clk *
1241.1Sjmcneillfixedclock_get(void *priv, const char *name)
1251.1Sjmcneill{
1261.1Sjmcneill	struct fixedclock_softc * const sc = priv;
1271.1Sjmcneill
1281.1Sjmcneill	if (strcmp(name, sc->sc_clk.base.name) != 0)
1291.1Sjmcneill		return NULL;
1301.1Sjmcneill
1311.1Sjmcneill	return &sc->sc_clk.base;
1321.1Sjmcneill}
1331.1Sjmcneill
1341.1Sjmcneillstatic void
1351.1Sjmcneillfixedclock_put(void *priv, struct clk *clk)
1361.1Sjmcneill{
1371.1Sjmcneill}
1381.1Sjmcneill
1391.1Sjmcneillstatic u_int
1401.1Sjmcneillfixedclock_get_rate(void *priv, struct clk *clk)
1411.1Sjmcneill{
1421.1Sjmcneill	struct fixedclock_clk *fclk = (struct fixedclock_clk *)clk;
1431.1Sjmcneill
1441.1Sjmcneill	return fclk->rate;
1451.1Sjmcneill}
146