11.6Sthorpej/* $NetBSD: fixedclock.c,v 1.6 2021/01/27 03:10:21 thorpej 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.6Sthorpej__KERNEL_RCSID(0, "$NetBSD: fixedclock.c,v 1.6 2021/01/27 03:10:21 thorpej 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.6Sthorpejstatic const struct device_compatible_entry compat_data[] = {
781.6Sthorpej	{ .compat = "fixed-clock" },
791.6Sthorpej	DEVICE_COMPAT_EOL
801.6Sthorpej};
811.6Sthorpej
821.1Sjmcneillstatic int
831.1Sjmcneillfixedclock_match(device_t parent, cfdata_t cf, void *aux)
841.1Sjmcneill{
851.1Sjmcneill	const struct fdt_attach_args *faa = aux;
861.1Sjmcneill
871.6Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
881.1Sjmcneill}
891.1Sjmcneill
901.1Sjmcneillstatic void
911.1Sjmcneillfixedclock_attach(device_t parent, device_t self, void *aux)
921.1Sjmcneill{
931.1Sjmcneill	struct fixedclock_softc * const sc = device_private(self);
941.1Sjmcneill	const struct fdt_attach_args *faa = aux;
951.1Sjmcneill	const int phandle = faa->faa_phandle;
961.4Sjmcneill	const char *clkname;
971.4Sjmcneill
981.4Sjmcneill	clkname = fdtbus_get_string(phandle, "clock-output-names");
991.4Sjmcneill	if (!clkname)
1001.4Sjmcneill		clkname = faa->faa_name;
1011.1Sjmcneill
1021.1Sjmcneill	sc->sc_dev = self;
1031.1Sjmcneill	sc->sc_phandle = phandle;
1041.3Sjmcneill	sc->sc_clkdom.name = device_xname(self);
1051.1Sjmcneill	sc->sc_clkdom.funcs = &fixedclock_clk_funcs;
1061.1Sjmcneill	sc->sc_clkdom.priv = sc;
1071.1Sjmcneill	if (of_getprop_uint32(phandle, "clock-frequency",
1081.1Sjmcneill	    &sc->sc_clk.rate) != 0) {
1091.1Sjmcneill		aprint_error(": couldn't determine frequency\n");
1101.1Sjmcneill		return;
1111.1Sjmcneill	}
1121.1Sjmcneill	sc->sc_clk.base.domain = &sc->sc_clkdom;
1131.4Sjmcneill	sc->sc_clk.base.name = kmem_asprintf("%s", clkname);
1141.3Sjmcneill	clk_attach(&sc->sc_clk.base);
1151.1Sjmcneill
1161.2Sjmcneill	aprint_naive("\n");
1171.4Sjmcneill	aprint_normal(": %u Hz fixed clock (%s)\n", sc->sc_clk.rate, clkname);
1181.1Sjmcneill
1191.1Sjmcneill	fdtbus_register_clock_controller(self, phandle, &fixedclock_fdt_funcs);
1201.1Sjmcneill}
1211.1Sjmcneill
1221.1Sjmcneillstatic struct clk *
1231.5Saymericfixedclock_decode(device_t dev, int cc_phandle, const void *data, size_t len)
1241.1Sjmcneill{
1251.1Sjmcneill	struct fixedclock_softc * const sc = device_private(dev);
1261.1Sjmcneill
1271.1Sjmcneill	/* #clock-cells for a fixed clock is always 0 */
1281.1Sjmcneill	if (len != 0)
1291.1Sjmcneill		return NULL;
1301.1Sjmcneill
1311.1Sjmcneill	return &sc->sc_clk.base;
1321.1Sjmcneill}
1331.1Sjmcneill
1341.1Sjmcneillstatic struct clk *
1351.1Sjmcneillfixedclock_get(void *priv, const char *name)
1361.1Sjmcneill{
1371.1Sjmcneill	struct fixedclock_softc * const sc = priv;
1381.1Sjmcneill
1391.1Sjmcneill	if (strcmp(name, sc->sc_clk.base.name) != 0)
1401.1Sjmcneill		return NULL;
1411.1Sjmcneill
1421.1Sjmcneill	return &sc->sc_clk.base;
1431.1Sjmcneill}
1441.1Sjmcneill
1451.1Sjmcneillstatic void
1461.1Sjmcneillfixedclock_put(void *priv, struct clk *clk)
1471.1Sjmcneill{
1481.1Sjmcneill}
1491.1Sjmcneill
1501.1Sjmcneillstatic u_int
1511.1Sjmcneillfixedclock_get_rate(void *priv, struct clk *clk)
1521.1Sjmcneill{
1531.1Sjmcneill	struct fixedclock_clk *fclk = (struct fixedclock_clk *)clk;
1541.1Sjmcneill
1551.1Sjmcneill	return fclk->rate;
1561.1Sjmcneill}
157