1*6e54367aSthorpej /* $NetBSD: sunxi_gmacclk.c,v 1.3 2021/01/27 03:10:20 thorpej Exp $ */
26c2b6ecfSjmcneill
36c2b6ecfSjmcneill /*-
46c2b6ecfSjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
56c2b6ecfSjmcneill * All rights reserved.
66c2b6ecfSjmcneill *
76c2b6ecfSjmcneill * Redistribution and use in source and binary forms, with or without
86c2b6ecfSjmcneill * modification, are permitted provided that the following conditions
96c2b6ecfSjmcneill * are met:
106c2b6ecfSjmcneill * 1. Redistributions of source code must retain the above copyright
116c2b6ecfSjmcneill * notice, this list of conditions and the following disclaimer.
126c2b6ecfSjmcneill * 2. Redistributions in binary form must reproduce the above copyright
136c2b6ecfSjmcneill * notice, this list of conditions and the following disclaimer in the
146c2b6ecfSjmcneill * documentation and/or other materials provided with the distribution.
156c2b6ecfSjmcneill *
166c2b6ecfSjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
176c2b6ecfSjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
186c2b6ecfSjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
196c2b6ecfSjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
206c2b6ecfSjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
216c2b6ecfSjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
226c2b6ecfSjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
236c2b6ecfSjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
246c2b6ecfSjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
256c2b6ecfSjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
266c2b6ecfSjmcneill * SUCH DAMAGE.
276c2b6ecfSjmcneill */
286c2b6ecfSjmcneill
296c2b6ecfSjmcneill #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: sunxi_gmacclk.c,v 1.3 2021/01/27 03:10:20 thorpej Exp $");
316c2b6ecfSjmcneill
326c2b6ecfSjmcneill #include <sys/param.h>
336c2b6ecfSjmcneill #include <sys/systm.h>
346c2b6ecfSjmcneill #include <sys/device.h>
356c2b6ecfSjmcneill #include <sys/kmem.h>
366c2b6ecfSjmcneill #include <sys/bus.h>
376c2b6ecfSjmcneill
386c2b6ecfSjmcneill #include <dev/clk/clk_backend.h>
396c2b6ecfSjmcneill
406c2b6ecfSjmcneill #include <dev/fdt/fdtvar.h>
416c2b6ecfSjmcneill
426c2b6ecfSjmcneill #define GMAC_CLK_PIT __BIT(2)
436c2b6ecfSjmcneill #define GMAC_CLK_PIT_MII 0
446c2b6ecfSjmcneill #define GMAC_CLK_PIT_RGMII 1
456c2b6ecfSjmcneill #define GMAC_CLK_SRC __BITS(1,0)
466c2b6ecfSjmcneill #define GMAC_CLK_SRC_MII 0
476c2b6ecfSjmcneill #define GMAC_CLK_SRC_EXT_RGMII 1
486c2b6ecfSjmcneill #define GMAC_CLK_SRC_RGMII 2
496c2b6ecfSjmcneill
506c2b6ecfSjmcneill static int sunxi_gmacclk_match(device_t, cfdata_t, void *);
516c2b6ecfSjmcneill static void sunxi_gmacclk_attach(device_t, device_t, void *);
526c2b6ecfSjmcneill
5351b425efSaymeric static struct clk *sunxi_gmacclk_decode(device_t, int, const void *, size_t);
546c2b6ecfSjmcneill
556c2b6ecfSjmcneill static const struct fdtbus_clock_controller_func sunxi_gmacclk_fdt_funcs = {
566c2b6ecfSjmcneill .decode = sunxi_gmacclk_decode
576c2b6ecfSjmcneill };
586c2b6ecfSjmcneill
596c2b6ecfSjmcneill static struct clk *sunxi_gmacclk_get(void *, const char *);
606c2b6ecfSjmcneill static void sunxi_gmacclk_put(void *, struct clk *);
616c2b6ecfSjmcneill static int sunxi_gmacclk_set_rate(void *, struct clk *, u_int);
626c2b6ecfSjmcneill static u_int sunxi_gmacclk_get_rate(void *, struct clk *);
636c2b6ecfSjmcneill static struct clk *sunxi_gmacclk_get_parent(void *, struct clk *);
646c2b6ecfSjmcneill
656c2b6ecfSjmcneill static const struct clk_funcs sunxi_gmacclk_clk_funcs = {
666c2b6ecfSjmcneill .get = sunxi_gmacclk_get,
676c2b6ecfSjmcneill .put = sunxi_gmacclk_put,
686c2b6ecfSjmcneill .set_rate = sunxi_gmacclk_set_rate,
696c2b6ecfSjmcneill .get_rate = sunxi_gmacclk_get_rate,
706c2b6ecfSjmcneill .get_parent = sunxi_gmacclk_get_parent,
716c2b6ecfSjmcneill };
726c2b6ecfSjmcneill
736c2b6ecfSjmcneill struct sunxi_gmacclk_softc {
746c2b6ecfSjmcneill device_t sc_dev;
756c2b6ecfSjmcneill int sc_phandle;
766c2b6ecfSjmcneill bus_space_tag_t sc_bst;
776c2b6ecfSjmcneill bus_space_handle_t sc_bsh;
786c2b6ecfSjmcneill
796c2b6ecfSjmcneill struct clk_domain sc_clkdom;
806c2b6ecfSjmcneill struct clk sc_clk;
816c2b6ecfSjmcneill struct clk *sc_parent[2];
826c2b6ecfSjmcneill };
836c2b6ecfSjmcneill
846c2b6ecfSjmcneill #define RD4(sc, reg) \
856c2b6ecfSjmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
866c2b6ecfSjmcneill #define WR4(sc, reg, val) \
876c2b6ecfSjmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
886c2b6ecfSjmcneill
896c2b6ecfSjmcneill CFATTACH_DECL_NEW(sunxi_gmacclk, sizeof(struct sunxi_gmacclk_softc),
906c2b6ecfSjmcneill sunxi_gmacclk_match, sunxi_gmacclk_attach, NULL, NULL);
916c2b6ecfSjmcneill
92*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
93*6e54367aSthorpej { .compat = "allwinner,sun7i-a20-gmac-clk" },
94*6e54367aSthorpej DEVICE_COMPAT_EOL
95*6e54367aSthorpej };
96*6e54367aSthorpej
976c2b6ecfSjmcneill static int
sunxi_gmacclk_match(device_t parent,cfdata_t cf,void * aux)986c2b6ecfSjmcneill sunxi_gmacclk_match(device_t parent, cfdata_t cf, void *aux)
996c2b6ecfSjmcneill {
1006c2b6ecfSjmcneill const struct fdt_attach_args *faa = aux;
1016c2b6ecfSjmcneill
102*6e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
1036c2b6ecfSjmcneill }
1046c2b6ecfSjmcneill
1056c2b6ecfSjmcneill static void
sunxi_gmacclk_attach(device_t parent,device_t self,void * aux)1066c2b6ecfSjmcneill sunxi_gmacclk_attach(device_t parent, device_t self, void *aux)
1076c2b6ecfSjmcneill {
1086c2b6ecfSjmcneill struct sunxi_gmacclk_softc * const sc = device_private(self);
1096c2b6ecfSjmcneill const struct fdt_attach_args *faa = aux;
1106c2b6ecfSjmcneill const int phandle = faa->faa_phandle;
1116c2b6ecfSjmcneill bus_addr_t addr;
1126c2b6ecfSjmcneill bus_size_t size;
1136c2b6ecfSjmcneill
1146c2b6ecfSjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1156c2b6ecfSjmcneill aprint_error(": couldn't get registers\n");
1166c2b6ecfSjmcneill return;
1176c2b6ecfSjmcneill }
1186c2b6ecfSjmcneill
1196c2b6ecfSjmcneill sc->sc_dev = self;
1206c2b6ecfSjmcneill sc->sc_phandle = phandle;
1216c2b6ecfSjmcneill sc->sc_bst = faa->faa_bst;
1226c2b6ecfSjmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
1236c2b6ecfSjmcneill aprint_error(": couldn't map registers\n");
1246c2b6ecfSjmcneill return;
1256c2b6ecfSjmcneill }
1266c2b6ecfSjmcneill sc->sc_parent[0] = fdtbus_clock_get_index(phandle, 0);
1276c2b6ecfSjmcneill sc->sc_parent[1] = fdtbus_clock_get_index(phandle, 1);
1286c2b6ecfSjmcneill if (sc->sc_parent[0] == NULL || sc->sc_parent[1] == NULL) {
1296c2b6ecfSjmcneill aprint_error(": couldn't get parent clocks\n");
1306c2b6ecfSjmcneill return;
1316c2b6ecfSjmcneill }
1326c2b6ecfSjmcneill
1336c2b6ecfSjmcneill sc->sc_clkdom.funcs = &sunxi_gmacclk_clk_funcs;
1346c2b6ecfSjmcneill sc->sc_clkdom.priv = sc;
1356c2b6ecfSjmcneill
1366c2b6ecfSjmcneill sc->sc_clk.domain = &sc->sc_clkdom;
1376c2b6ecfSjmcneill sc->sc_clk.name = kmem_asprintf("%s", faa->faa_name);
1386c2b6ecfSjmcneill
1396c2b6ecfSjmcneill aprint_naive("\n");
1406c2b6ecfSjmcneill aprint_normal(": GMAC MII/RGMII clock mux\n");
1416c2b6ecfSjmcneill
1426c2b6ecfSjmcneill fdtbus_register_clock_controller(self, phandle, &sunxi_gmacclk_fdt_funcs);
1436c2b6ecfSjmcneill }
1446c2b6ecfSjmcneill
1456c2b6ecfSjmcneill static struct clk *
sunxi_gmacclk_decode(device_t dev,int cc_phandle,const void * data,size_t len)14651b425efSaymeric sunxi_gmacclk_decode(device_t dev, int cc_phandle, const void *data,
14751b425efSaymeric size_t len)
1486c2b6ecfSjmcneill {
1496c2b6ecfSjmcneill struct sunxi_gmacclk_softc * const sc = device_private(dev);
1506c2b6ecfSjmcneill
1516c2b6ecfSjmcneill if (len != 0)
1526c2b6ecfSjmcneill return NULL;
1536c2b6ecfSjmcneill
1546c2b6ecfSjmcneill return &sc->sc_clk;
1556c2b6ecfSjmcneill }
1566c2b6ecfSjmcneill
1576c2b6ecfSjmcneill static struct clk *
sunxi_gmacclk_get(void * priv,const char * name)1586c2b6ecfSjmcneill sunxi_gmacclk_get(void *priv, const char *name)
1596c2b6ecfSjmcneill {
1606c2b6ecfSjmcneill struct sunxi_gmacclk_softc * const sc = priv;
1616c2b6ecfSjmcneill
1626c2b6ecfSjmcneill if (strcmp(name, sc->sc_clk.name) != 0)
1636c2b6ecfSjmcneill return NULL;
1646c2b6ecfSjmcneill
1656c2b6ecfSjmcneill return &sc->sc_clk;
1666c2b6ecfSjmcneill }
1676c2b6ecfSjmcneill
1686c2b6ecfSjmcneill static void
sunxi_gmacclk_put(void * priv,struct clk * clk)1696c2b6ecfSjmcneill sunxi_gmacclk_put(void *priv, struct clk *clk)
1706c2b6ecfSjmcneill {
1716c2b6ecfSjmcneill }
1726c2b6ecfSjmcneill
1736c2b6ecfSjmcneill static int
sunxi_gmacclk_set_rate(void * priv,struct clk * clk,u_int rate)1746c2b6ecfSjmcneill sunxi_gmacclk_set_rate(void *priv, struct clk *clk, u_int rate)
1756c2b6ecfSjmcneill {
1766c2b6ecfSjmcneill struct sunxi_gmacclk_softc * const sc = priv;
1776c2b6ecfSjmcneill uint32_t val;
1786c2b6ecfSjmcneill
1796c2b6ecfSjmcneill val = RD4(sc, 0);
1806c2b6ecfSjmcneill val &= ~(GMAC_CLK_PIT|GMAC_CLK_SRC);
1816c2b6ecfSjmcneill if (rate == clk_get_rate(sc->sc_parent[GMAC_CLK_PIT_MII])) {
1826c2b6ecfSjmcneill /* MII clock */
1836c2b6ecfSjmcneill val |= __SHIFTIN(GMAC_CLK_PIT_MII, GMAC_CLK_PIT);
1846c2b6ecfSjmcneill val |= __SHIFTIN(GMAC_CLK_SRC_MII, GMAC_CLK_SRC);
1856c2b6ecfSjmcneill } else if (rate == clk_get_rate(sc->sc_parent[GMAC_CLK_PIT_RGMII])) {
1866c2b6ecfSjmcneill /* RGMII clock */
1876c2b6ecfSjmcneill val |= __SHIFTIN(GMAC_CLK_PIT_RGMII, GMAC_CLK_PIT);
1886c2b6ecfSjmcneill val |= __SHIFTIN(GMAC_CLK_SRC_RGMII, GMAC_CLK_SRC);
1896c2b6ecfSjmcneill } else {
1906c2b6ecfSjmcneill return ENXIO;
1916c2b6ecfSjmcneill }
1926c2b6ecfSjmcneill WR4(sc, 0, val);
1936c2b6ecfSjmcneill
1946c2b6ecfSjmcneill return 0;
1956c2b6ecfSjmcneill }
1966c2b6ecfSjmcneill
1976c2b6ecfSjmcneill static u_int
sunxi_gmacclk_get_rate(void * priv,struct clk * clk)1986c2b6ecfSjmcneill sunxi_gmacclk_get_rate(void *priv, struct clk *clk)
1996c2b6ecfSjmcneill {
2006c2b6ecfSjmcneill struct clk *clk_parent = clk_get_parent(clk);
2016c2b6ecfSjmcneill
2026c2b6ecfSjmcneill return clk_get_rate(clk_parent);
2036c2b6ecfSjmcneill }
2046c2b6ecfSjmcneill
2056c2b6ecfSjmcneill static struct clk *
sunxi_gmacclk_get_parent(void * priv,struct clk * clk)2066c2b6ecfSjmcneill sunxi_gmacclk_get_parent(void *priv, struct clk *clk)
2076c2b6ecfSjmcneill {
2086c2b6ecfSjmcneill struct sunxi_gmacclk_softc * const sc = priv;
2096c2b6ecfSjmcneill uint32_t val;
2106c2b6ecfSjmcneill u_int sel;
2116c2b6ecfSjmcneill
2126c2b6ecfSjmcneill val = RD4(sc, 0);
2136c2b6ecfSjmcneill sel = __SHIFTOUT(val, GMAC_CLK_PIT);
2146c2b6ecfSjmcneill
2156c2b6ecfSjmcneill return sc->sc_parent[sel];
2166c2b6ecfSjmcneill }
217