fixedclock.c revision 1.5
11.5Saymeric/* $NetBSD: fixedclock.c,v 1.5 2018/09/09 07:21:18 aymeric 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.5Saymeric__KERNEL_RCSID(0, "$NetBSD: fixedclock.c,v 1.5 2018/09/09 07:21:18 aymeric 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.5Saymericstatic struct clk *fixedclock_decode(device_t, int, 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.4Sjmcneill	const char *clkname;
931.4Sjmcneill
941.4Sjmcneill	clkname = fdtbus_get_string(phandle, "clock-output-names");
951.4Sjmcneill	if (!clkname)
961.4Sjmcneill		clkname = faa->faa_name;
971.1Sjmcneill
981.1Sjmcneill	sc->sc_dev = self;
991.1Sjmcneill	sc->sc_phandle = phandle;
1001.3Sjmcneill	sc->sc_clkdom.name = device_xname(self);
1011.1Sjmcneill	sc->sc_clkdom.funcs = &fixedclock_clk_funcs;
1021.1Sjmcneill	sc->sc_clkdom.priv = sc;
1031.1Sjmcneill	if (of_getprop_uint32(phandle, "clock-frequency",
1041.1Sjmcneill	    &sc->sc_clk.rate) != 0) {
1051.1Sjmcneill		aprint_error(": couldn't determine frequency\n");
1061.1Sjmcneill		return;
1071.1Sjmcneill	}
1081.1Sjmcneill	sc->sc_clk.base.domain = &sc->sc_clkdom;
1091.4Sjmcneill	sc->sc_clk.base.name = kmem_asprintf("%s", clkname);
1101.3Sjmcneill	clk_attach(&sc->sc_clk.base);
1111.1Sjmcneill
1121.2Sjmcneill	aprint_naive("\n");
1131.4Sjmcneill	aprint_normal(": %u Hz fixed clock (%s)\n", sc->sc_clk.rate, clkname);
1141.1Sjmcneill
1151.1Sjmcneill	fdtbus_register_clock_controller(self, phandle, &fixedclock_fdt_funcs);
1161.1Sjmcneill}
1171.1Sjmcneill
1181.1Sjmcneillstatic struct clk *
1191.5Saymericfixedclock_decode(device_t dev, int cc_phandle, const void *data, size_t len)
1201.1Sjmcneill{
1211.1Sjmcneill	struct fixedclock_softc * const sc = device_private(dev);
1221.1Sjmcneill
1231.1Sjmcneill	/* #clock-cells for a fixed clock is always 0 */
1241.1Sjmcneill	if (len != 0)
1251.1Sjmcneill		return NULL;
1261.1Sjmcneill
1271.1Sjmcneill	return &sc->sc_clk.base;
1281.1Sjmcneill}
1291.1Sjmcneill
1301.1Sjmcneillstatic struct clk *
1311.1Sjmcneillfixedclock_get(void *priv, const char *name)
1321.1Sjmcneill{
1331.1Sjmcneill	struct fixedclock_softc * const sc = priv;
1341.1Sjmcneill
1351.1Sjmcneill	if (strcmp(name, sc->sc_clk.base.name) != 0)
1361.1Sjmcneill		return NULL;
1371.1Sjmcneill
1381.1Sjmcneill	return &sc->sc_clk.base;
1391.1Sjmcneill}
1401.1Sjmcneill
1411.1Sjmcneillstatic void
1421.1Sjmcneillfixedclock_put(void *priv, struct clk *clk)
1431.1Sjmcneill{
1441.1Sjmcneill}
1451.1Sjmcneill
1461.1Sjmcneillstatic u_int
1471.1Sjmcneillfixedclock_get_rate(void *priv, struct clk *clk)
1481.1Sjmcneill{
1491.1Sjmcneill	struct fixedclock_clk *fclk = (struct fixedclock_clk *)clk;
1501.1Sjmcneill
1511.1Sjmcneill	return fclk->rate;
1521.1Sjmcneill}
153