Home | History | Annotate | Line # | Download | only in rockchip
      1  1.10       ryo /* $NetBSD: rk_cru.c,v 1.10 2022/09/18 21:33:57 ryo Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  jmcneill  * SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill #include "opt_soc.h"
     30   1.8     skrll #include "opt_console.h"
     31   1.1  jmcneill 
     32   1.1  jmcneill #include <sys/cdefs.h>
     33  1.10       ryo __KERNEL_RCSID(0, "$NetBSD: rk_cru.c,v 1.10 2022/09/18 21:33:57 ryo Exp $");
     34   1.1  jmcneill 
     35   1.1  jmcneill #include <sys/param.h>
     36   1.1  jmcneill #include <sys/bus.h>
     37   1.1  jmcneill #include <sys/cpu.h>
     38   1.1  jmcneill #include <sys/device.h>
     39   1.1  jmcneill 
     40   1.1  jmcneill #include <dev/fdt/fdtvar.h>
     41   1.1  jmcneill 
     42   1.1  jmcneill #include <dev/clk/clk_backend.h>
     43   1.1  jmcneill 
     44   1.1  jmcneill #include <arm/rockchip/rk_cru.h>
     45   1.1  jmcneill 
     46   1.1  jmcneill static void *
     47   1.1  jmcneill rk_cru_reset_acquire(device_t dev, const void *data, size_t len)
     48   1.1  jmcneill {
     49   1.1  jmcneill 	if (len != 4)
     50   1.1  jmcneill 		return NULL;
     51   1.1  jmcneill 
     52   1.1  jmcneill 	return (void *)(uintptr_t)be32dec(data);
     53   1.1  jmcneill }
     54   1.1  jmcneill 
     55   1.1  jmcneill static void
     56   1.1  jmcneill rk_cru_reset_release(device_t dev, void *priv)
     57   1.1  jmcneill {
     58   1.1  jmcneill }
     59   1.1  jmcneill 
     60   1.1  jmcneill static int
     61   1.1  jmcneill rk_cru_reset_assert(device_t dev, void *priv)
     62   1.1  jmcneill {
     63   1.1  jmcneill 	struct rk_cru_softc * const sc = device_private(dev);
     64   1.1  jmcneill 	const uintptr_t reset_id = (uintptr_t)priv;
     65   1.5  jmcneill 	const bus_size_t reg = sc->sc_softrst_base + (reset_id / 16) * 4;
     66   1.2  jmcneill 	const u_int shift = reset_id % 16;
     67   1.1  jmcneill 
     68   1.2  jmcneill 	CRU_WRITE(sc, reg, (1 << (shift + 16)) | (1 << shift));
     69   1.1  jmcneill 
     70   1.1  jmcneill 	return 0;
     71   1.1  jmcneill }
     72   1.1  jmcneill 
     73   1.1  jmcneill static int
     74   1.1  jmcneill rk_cru_reset_deassert(device_t dev, void *priv)
     75   1.1  jmcneill {
     76   1.1  jmcneill 	struct rk_cru_softc * const sc = device_private(dev);
     77   1.1  jmcneill 	const uintptr_t reset_id = (uintptr_t)priv;
     78   1.5  jmcneill 	const bus_size_t reg = sc->sc_softrst_base + (reset_id / 16) * 4;
     79   1.2  jmcneill 	const u_int shift = reset_id % 16;
     80   1.1  jmcneill 
     81   1.2  jmcneill 	CRU_WRITE(sc, reg, (1 << (shift + 16)) | (0 << shift));
     82   1.1  jmcneill 
     83   1.1  jmcneill 	return 0;
     84   1.1  jmcneill }
     85   1.1  jmcneill 
     86   1.1  jmcneill static const struct fdtbus_reset_controller_func rk_cru_fdtreset_funcs = {
     87   1.1  jmcneill 	.acquire = rk_cru_reset_acquire,
     88   1.1  jmcneill 	.release = rk_cru_reset_release,
     89   1.1  jmcneill 	.reset_assert = rk_cru_reset_assert,
     90   1.1  jmcneill 	.reset_deassert = rk_cru_reset_deassert,
     91   1.1  jmcneill };
     92   1.1  jmcneill 
     93   1.1  jmcneill static struct clk *
     94   1.7   aymeric rk_cru_clock_decode(device_t dev, int cc_phandle, const void *data, size_t len)
     95   1.1  jmcneill {
     96   1.1  jmcneill 	struct rk_cru_softc * const sc = device_private(dev);
     97   1.1  jmcneill 	struct rk_cru_clk *clk;
     98   1.1  jmcneill 
     99   1.1  jmcneill 	if (len != 4)
    100   1.1  jmcneill 		return NULL;
    101   1.1  jmcneill 
    102   1.1  jmcneill 	const u_int clock_id = be32dec(data);
    103   1.1  jmcneill 
    104   1.1  jmcneill 	for (int i = 0; i < sc->sc_nclks; i++) {
    105   1.1  jmcneill 		clk = &sc->sc_clks[i];
    106   1.1  jmcneill 		if (clk->id == clock_id)
    107   1.1  jmcneill 			return &clk->base;
    108   1.1  jmcneill 	}
    109   1.1  jmcneill 
    110   1.1  jmcneill 	return NULL;
    111   1.1  jmcneill }
    112   1.1  jmcneill 
    113   1.1  jmcneill static const struct fdtbus_clock_controller_func rk_cru_fdtclock_funcs = {
    114   1.1  jmcneill 	.decode = rk_cru_clock_decode,
    115   1.1  jmcneill };
    116   1.1  jmcneill 
    117   1.1  jmcneill static struct clk *
    118   1.1  jmcneill rk_cru_clock_get(void *priv, const char *name)
    119   1.1  jmcneill {
    120   1.1  jmcneill 	struct rk_cru_softc * const sc = priv;
    121   1.1  jmcneill 	struct rk_cru_clk *clk;
    122   1.1  jmcneill 
    123   1.1  jmcneill 	clk = rk_cru_clock_find(sc, name);
    124   1.1  jmcneill 	if (clk == NULL)
    125   1.1  jmcneill 		return NULL;
    126   1.1  jmcneill 
    127   1.1  jmcneill 	return &clk->base;
    128   1.1  jmcneill }
    129   1.1  jmcneill 
    130   1.1  jmcneill static void
    131   1.1  jmcneill rk_cru_clock_put(void *priv, struct clk *clk)
    132   1.1  jmcneill {
    133   1.1  jmcneill }
    134   1.1  jmcneill 
    135   1.1  jmcneill static u_int
    136   1.1  jmcneill rk_cru_clock_get_rate(void *priv, struct clk *clkp)
    137   1.1  jmcneill {
    138   1.1  jmcneill 	struct rk_cru_softc * const sc = priv;
    139   1.1  jmcneill 	struct rk_cru_clk *clk = (struct rk_cru_clk *)clkp;
    140   1.1  jmcneill 	struct clk *clkp_parent;
    141   1.1  jmcneill 
    142   1.1  jmcneill 	if (clk->get_rate)
    143   1.1  jmcneill 		return clk->get_rate(sc, clk);
    144   1.1  jmcneill 
    145   1.1  jmcneill 	clkp_parent = clk_get_parent(clkp);
    146   1.1  jmcneill 	if (clkp_parent == NULL) {
    147  1.10       ryo 		aprint_debug("%s: no parent for %s\n", __func__,
    148  1.10       ryo 		    clk->base.name);
    149   1.1  jmcneill 		return 0;
    150   1.1  jmcneill 	}
    151   1.1  jmcneill 
    152   1.1  jmcneill 	return clk_get_rate(clkp_parent);
    153   1.1  jmcneill }
    154   1.1  jmcneill 
    155   1.1  jmcneill static int
    156   1.1  jmcneill rk_cru_clock_set_rate(void *priv, struct clk *clkp, u_int rate)
    157   1.1  jmcneill {
    158   1.1  jmcneill 	struct rk_cru_softc * const sc = priv;
    159   1.1  jmcneill 	struct rk_cru_clk *clk = (struct rk_cru_clk *)clkp;
    160   1.1  jmcneill 	struct clk *clkp_parent;
    161   1.1  jmcneill 
    162   1.1  jmcneill 	if (clkp->flags & CLK_SET_RATE_PARENT) {
    163   1.1  jmcneill 		clkp_parent = clk_get_parent(clkp);
    164   1.1  jmcneill 		if (clkp_parent == NULL) {
    165  1.10       ryo 			aprint_error("%s: no parent for %s\n", __func__,
    166  1.10       ryo 			    clk->base.name);
    167   1.1  jmcneill 			return ENXIO;
    168   1.1  jmcneill 		}
    169   1.1  jmcneill 		return clk_set_rate(clkp_parent, rate);
    170   1.1  jmcneill 	}
    171   1.1  jmcneill 
    172   1.1  jmcneill 	if (clk->set_rate)
    173   1.1  jmcneill 		return clk->set_rate(sc, clk, rate);
    174   1.1  jmcneill 
    175   1.1  jmcneill 	return ENXIO;
    176   1.1  jmcneill }
    177   1.1  jmcneill 
    178   1.1  jmcneill static u_int
    179   1.1  jmcneill rk_cru_clock_round_rate(void *priv, struct clk *clkp, u_int rate)
    180   1.1  jmcneill {
    181   1.1  jmcneill 	struct rk_cru_softc * const sc = priv;
    182   1.1  jmcneill 	struct rk_cru_clk *clk = (struct rk_cru_clk *)clkp;
    183   1.1  jmcneill 	struct clk *clkp_parent;
    184   1.1  jmcneill 
    185   1.1  jmcneill 	if (clkp->flags & CLK_SET_RATE_PARENT) {
    186   1.1  jmcneill 		clkp_parent = clk_get_parent(clkp);
    187   1.1  jmcneill 		if (clkp_parent == NULL) {
    188  1.10       ryo 			aprint_error("%s: no parent for %s\n", __func__,
    189  1.10       ryo 			    clk->base.name);
    190   1.1  jmcneill 			return 0;
    191   1.1  jmcneill 		}
    192   1.1  jmcneill 		return clk_round_rate(clkp_parent, rate);
    193   1.1  jmcneill 	}
    194   1.1  jmcneill 
    195   1.1  jmcneill 	if (clk->round_rate)
    196   1.1  jmcneill 		return clk->round_rate(sc, clk, rate);
    197   1.1  jmcneill 
    198   1.1  jmcneill 	return 0;
    199   1.1  jmcneill }
    200   1.1  jmcneill 
    201   1.1  jmcneill static int
    202   1.1  jmcneill rk_cru_clock_enable(void *priv, struct clk *clkp)
    203   1.1  jmcneill {
    204   1.1  jmcneill 	struct rk_cru_softc * const sc = priv;
    205   1.1  jmcneill 	struct rk_cru_clk *clk = (struct rk_cru_clk *)clkp;
    206   1.1  jmcneill 	struct clk *clkp_parent;
    207   1.1  jmcneill 	int error = 0;
    208   1.1  jmcneill 
    209   1.1  jmcneill 	clkp_parent = clk_get_parent(clkp);
    210   1.1  jmcneill 	if (clkp_parent != NULL) {
    211   1.1  jmcneill 		error = clk_enable(clkp_parent);
    212   1.1  jmcneill 		if (error != 0)
    213   1.1  jmcneill 			return error;
    214   1.1  jmcneill 	}
    215   1.1  jmcneill 
    216   1.1  jmcneill 	if (clk->enable)
    217   1.1  jmcneill 		error = clk->enable(sc, clk, 1);
    218   1.1  jmcneill 
    219   1.1  jmcneill 	return error;
    220   1.1  jmcneill }
    221   1.1  jmcneill 
    222   1.1  jmcneill static int
    223   1.1  jmcneill rk_cru_clock_disable(void *priv, struct clk *clkp)
    224   1.1  jmcneill {
    225   1.1  jmcneill 	struct rk_cru_softc * const sc = priv;
    226   1.1  jmcneill 	struct rk_cru_clk *clk = (struct rk_cru_clk *)clkp;
    227   1.1  jmcneill 	int error = EINVAL;
    228   1.1  jmcneill 
    229   1.1  jmcneill 	if (clk->enable)
    230   1.1  jmcneill 		error = clk->enable(sc, clk, 0);
    231   1.1  jmcneill 
    232   1.1  jmcneill 	return error;
    233   1.1  jmcneill }
    234   1.1  jmcneill 
    235   1.1  jmcneill static int
    236  1.10       ryo rk_cru_clock_set_parent(void *priv, struct clk *clkp, struct clk *clkp_parent)
    237   1.1  jmcneill {
    238   1.1  jmcneill 	struct rk_cru_softc * const sc = priv;
    239   1.1  jmcneill 	struct rk_cru_clk *clk = (struct rk_cru_clk *)clkp;
    240   1.1  jmcneill 
    241   1.1  jmcneill 	if (clk->set_parent == NULL)
    242   1.1  jmcneill 		return EINVAL;
    243   1.1  jmcneill 
    244   1.1  jmcneill 	return clk->set_parent(sc, clk, clkp_parent->name);
    245   1.1  jmcneill }
    246   1.1  jmcneill 
    247   1.1  jmcneill static struct clk *
    248   1.1  jmcneill rk_cru_clock_get_parent(void *priv, struct clk *clkp)
    249   1.1  jmcneill {
    250   1.1  jmcneill 	struct rk_cru_softc * const sc = priv;
    251   1.1  jmcneill 	struct rk_cru_clk *clk = (struct rk_cru_clk *)clkp;
    252   1.1  jmcneill 	struct rk_cru_clk *clk_parent;
    253   1.1  jmcneill 	const char *parent;
    254   1.1  jmcneill 
    255   1.1  jmcneill 	if (clk->get_parent == NULL)
    256   1.1  jmcneill 		return NULL;
    257   1.1  jmcneill 
    258   1.1  jmcneill 	parent = clk->get_parent(sc, clk);
    259   1.1  jmcneill 	if (parent == NULL)
    260   1.1  jmcneill 		return NULL;
    261   1.1  jmcneill 
    262   1.1  jmcneill 	clk_parent = rk_cru_clock_find(sc, parent);
    263   1.1  jmcneill 	if (clk_parent != NULL)
    264   1.1  jmcneill 		return &clk_parent->base;
    265   1.1  jmcneill 
    266   1.1  jmcneill 	/* No parent in this domain, try FDT */
    267   1.1  jmcneill 	return fdtbus_clock_byname(parent);
    268   1.1  jmcneill }
    269   1.1  jmcneill 
    270   1.1  jmcneill static const struct clk_funcs rk_cru_clock_funcs = {
    271   1.1  jmcneill 	.get = rk_cru_clock_get,
    272   1.1  jmcneill 	.put = rk_cru_clock_put,
    273   1.1  jmcneill 	.get_rate = rk_cru_clock_get_rate,
    274   1.1  jmcneill 	.set_rate = rk_cru_clock_set_rate,
    275   1.1  jmcneill 	.round_rate = rk_cru_clock_round_rate,
    276   1.1  jmcneill 	.enable = rk_cru_clock_enable,
    277   1.1  jmcneill 	.disable = rk_cru_clock_disable,
    278   1.1  jmcneill 	.set_parent = rk_cru_clock_set_parent,
    279   1.1  jmcneill 	.get_parent = rk_cru_clock_get_parent,
    280   1.1  jmcneill };
    281   1.1  jmcneill 
    282   1.1  jmcneill struct rk_cru_clk *
    283   1.1  jmcneill rk_cru_clock_find(struct rk_cru_softc *sc, const char *name)
    284   1.1  jmcneill {
    285   1.1  jmcneill 	for (int i = 0; i < sc->sc_nclks; i++) {
    286   1.1  jmcneill 		if (sc->sc_clks[i].base.name == NULL)
    287   1.1  jmcneill 			continue;
    288   1.1  jmcneill 		if (strcmp(sc->sc_clks[i].base.name, name) == 0)
    289   1.1  jmcneill 			return &sc->sc_clks[i];
    290   1.1  jmcneill 	}
    291   1.1  jmcneill 
    292   1.1  jmcneill 	return NULL;
    293   1.1  jmcneill }
    294   1.1  jmcneill 
    295   1.1  jmcneill int
    296   1.1  jmcneill rk_cru_attach(struct rk_cru_softc *sc)
    297   1.1  jmcneill {
    298   1.1  jmcneill 	bus_addr_t addr;
    299   1.1  jmcneill 	bus_size_t size;
    300   1.1  jmcneill 	int i;
    301   1.1  jmcneill 
    302   1.1  jmcneill 	if (of_hasprop(sc->sc_phandle, "rockchip,grf")) {
    303  1.10       ryo 		sc->sc_grf = fdtbus_syscon_acquire(sc->sc_phandle,
    304  1.10       ryo 		    "rockchip,grf");
    305   1.4  jmcneill 		if (sc->sc_grf == NULL) {
    306   1.4  jmcneill 			aprint_error(": couldn't get grf syscon\n");
    307   1.1  jmcneill 			return ENXIO;
    308   1.1  jmcneill 		}
    309   1.1  jmcneill 	}
    310   1.1  jmcneill 
    311   1.1  jmcneill 	if (fdtbus_get_reg(sc->sc_phandle, 0, &addr, &size) != 0) {
    312   1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    313   1.1  jmcneill 		return ENXIO;
    314   1.1  jmcneill 	}
    315   1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    316   1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    317   1.1  jmcneill 		return ENXIO;
    318   1.1  jmcneill 	}
    319   1.1  jmcneill 
    320   1.1  jmcneill 	sc->sc_clkdom.name = device_xname(sc->sc_dev);
    321   1.1  jmcneill 	sc->sc_clkdom.funcs = &rk_cru_clock_funcs;
    322   1.1  jmcneill 	sc->sc_clkdom.priv = sc;
    323   1.1  jmcneill 	for (i = 0; i < sc->sc_nclks; i++) {
    324   1.1  jmcneill 		sc->sc_clks[i].base.domain = &sc->sc_clkdom;
    325   1.1  jmcneill 		clk_attach(&sc->sc_clks[i].base);
    326   1.1  jmcneill 	}
    327   1.1  jmcneill 
    328   1.1  jmcneill 	fdtbus_register_clock_controller(sc->sc_dev, sc->sc_phandle,
    329   1.1  jmcneill 	    &rk_cru_fdtclock_funcs);
    330   1.1  jmcneill 
    331   1.1  jmcneill 	fdtbus_register_reset_controller(sc->sc_dev, sc->sc_phandle,
    332   1.1  jmcneill 	    &rk_cru_fdtreset_funcs);
    333   1.1  jmcneill 
    334   1.1  jmcneill 	return 0;
    335   1.1  jmcneill }
    336   1.1  jmcneill 
    337   1.1  jmcneill void
    338   1.1  jmcneill rk_cru_print(struct rk_cru_softc *sc)
    339   1.1  jmcneill {
    340   1.1  jmcneill 	struct rk_cru_clk *clk;
    341   1.1  jmcneill 	struct clk *clkp_parent;
    342   1.1  jmcneill 	const char *type;
    343   1.1  jmcneill 	int i;
    344   1.1  jmcneill 
    345   1.1  jmcneill 	for (i = 0; i < sc->sc_nclks; i++) {
    346   1.1  jmcneill 		clk = &sc->sc_clks[i];
    347   1.1  jmcneill 		if (clk->type == RK_CRU_UNKNOWN)
    348   1.1  jmcneill 			continue;
    349   1.1  jmcneill 
    350   1.1  jmcneill 		clkp_parent = clk_get_parent(&clk->base);
    351   1.1  jmcneill 
    352   1.1  jmcneill 		switch (clk->type) {
    353   1.1  jmcneill 		case RK_CRU_PLL:		type = "pll"; break;
    354   1.1  jmcneill 		case RK_CRU_ARM:		type = "arm"; break;
    355   1.1  jmcneill 		case RK_CRU_COMPOSITE:		type = "comp"; break;
    356   1.1  jmcneill 		case RK_CRU_GATE:		type = "gate"; break;
    357   1.1  jmcneill 		case RK_CRU_MUX:		type = "mux"; break;
    358   1.1  jmcneill 		default:			type = "???"; break;
    359   1.1  jmcneill 		}
    360   1.1  jmcneill 
    361  1.10       ryo 		aprint_debug_dev(sc->sc_dev,
    362   1.1  jmcneill 		    "%3d %-14s %2s %-14s %-7s ",
    363   1.1  jmcneill 		    clk->id,
    364  1.10       ryo 		    clk->base.name,
    365  1.10       ryo 		    clkp_parent ? "<-" : "",
    366  1.10       ryo 		    clkp_parent ? clkp_parent->name : "",
    367  1.10       ryo 		    type);
    368   1.6  jmcneill 		aprint_debug("%10d Hz\n", clk_get_rate(&clk->base));
    369   1.1  jmcneill 	}
    370   1.1  jmcneill }
    371