Home | History | Annotate | Line # | Download | only in sunxi
      1  1.8     skrll /* $NetBSD: sunxi_de2_ccu.c,v 1.8 2022/06/28 05:19:03 skrll Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2019 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 <sys/cdefs.h>
     30  1.1  jmcneill 
     31  1.8     skrll __KERNEL_RCSID(1, "$NetBSD: sunxi_de2_ccu.c,v 1.8 2022/06/28 05:19:03 skrll Exp $");
     32  1.1  jmcneill 
     33  1.1  jmcneill #include <sys/param.h>
     34  1.1  jmcneill #include <sys/bus.h>
     35  1.1  jmcneill #include <sys/device.h>
     36  1.1  jmcneill #include <sys/systm.h>
     37  1.1  jmcneill 
     38  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     39  1.1  jmcneill 
     40  1.1  jmcneill #include <arm/sunxi/sunxi_ccu.h>
     41  1.1  jmcneill #include <arm/sunxi/sunxi_de2_ccu.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill static int sunxi_de2_ccu_match(device_t, cfdata_t, void *);
     44  1.1  jmcneill static void sunxi_de2_ccu_attach(device_t, device_t, void *);
     45  1.1  jmcneill 
     46  1.1  jmcneill CFATTACH_DECL_NEW(sunxi_de2ccu, sizeof(struct sunxi_ccu_softc),
     47  1.1  jmcneill 	sunxi_de2_ccu_match, sunxi_de2_ccu_attach, NULL, NULL);
     48  1.1  jmcneill 
     49  1.3  jmcneill static struct sunxi_ccu_reset sun8i_h3_de2_ccu_resets[] = {
     50  1.3  jmcneill 	SUNXI_CCU_RESET(DE2_RST_MIXER0, 0x08, 0),
     51  1.3  jmcneill 	SUNXI_CCU_RESET(DE2_RST_WB, 0x08, 2),
     52  1.3  jmcneill };
     53  1.3  jmcneill 
     54  1.1  jmcneill static struct sunxi_ccu_reset sun50i_a64_de2_ccu_resets[] = {
     55  1.1  jmcneill 	SUNXI_CCU_RESET(DE2_RST_MIXER0, 0x08, 0),
     56  1.1  jmcneill 	SUNXI_CCU_RESET(DE2_RST_MIXER1, 0x08, 1),
     57  1.1  jmcneill 	SUNXI_CCU_RESET(DE2_RST_WB, 0x08, 2),
     58  1.1  jmcneill };
     59  1.1  jmcneill 
     60  1.1  jmcneill static const char *mod_parents[] = { "mod" };
     61  1.1  jmcneill 
     62  1.3  jmcneill static struct sunxi_ccu_clk sun8i_h3_de2_ccu_clks[] = {
     63  1.1  jmcneill 	SUNXI_CCU_GATE(DE2_CLK_BUS_MIXER0, "bus-mixer0", "bus", 0x04, 0),
     64  1.1  jmcneill 	SUNXI_CCU_GATE(DE2_CLK_BUS_MIXER1, "bus-mixer1", "bus", 0x04, 1),
     65  1.1  jmcneill 	SUNXI_CCU_GATE(DE2_CLK_BUS_WB, "bus-wb", "bus", 0x04, 2),
     66  1.1  jmcneill 
     67  1.1  jmcneill 	SUNXI_CCU_DIV(DE2_CLK_MIXER0_DIV, "mixer0-div", mod_parents,
     68  1.1  jmcneill 	    0x0c, __BITS(3,0), 0, SUNXI_CCU_DIV_SET_RATE_PARENT),
     69  1.1  jmcneill 	SUNXI_CCU_DIV(DE2_CLK_MIXER1_DIV, "mixer1-div", mod_parents,
     70  1.1  jmcneill 	    0x0c, __BITS(7,4), 0, SUNXI_CCU_DIV_SET_RATE_PARENT),
     71  1.1  jmcneill 	SUNXI_CCU_DIV(DE2_CLK_WB_DIV, "wb-div", mod_parents,
     72  1.1  jmcneill 	    0x0c, __BITS(11,8), 0, SUNXI_CCU_DIV_SET_RATE_PARENT),
     73  1.1  jmcneill 
     74  1.1  jmcneill 	SUNXI_CCU_GATE(DE2_CLK_MIXER0, "mixer0", "mixer0-div", 0x00, 0),
     75  1.1  jmcneill 	SUNXI_CCU_GATE(DE2_CLK_MIXER1, "mixer1", "mixer1-div", 0x00, 1),
     76  1.1  jmcneill 	SUNXI_CCU_GATE(DE2_CLK_WB, "wb", "wb-div", 0x00, 2),
     77  1.1  jmcneill };
     78  1.1  jmcneill 
     79  1.1  jmcneill struct sunxi_de2_ccu_config {
     80  1.1  jmcneill 	struct sunxi_ccu_reset	*resets;
     81  1.1  jmcneill 	u_int			nresets;
     82  1.1  jmcneill 	struct sunxi_ccu_clk	*clks;
     83  1.1  jmcneill 	u_int			nclks;
     84  1.1  jmcneill };
     85  1.1  jmcneill 
     86  1.3  jmcneill static const struct sunxi_de2_ccu_config sun8i_h3_de2_config = {
     87  1.3  jmcneill 	.resets = sun8i_h3_de2_ccu_resets,
     88  1.3  jmcneill 	.nresets = __arraycount(sun8i_h3_de2_ccu_resets),
     89  1.3  jmcneill 	.clks = sun8i_h3_de2_ccu_clks,
     90  1.3  jmcneill 	.nclks = __arraycount(sun8i_h3_de2_ccu_clks),
     91  1.3  jmcneill };
     92  1.3  jmcneill 
     93  1.1  jmcneill static const struct sunxi_de2_ccu_config sun50i_a64_de2_config = {
     94  1.1  jmcneill 	.resets = sun50i_a64_de2_ccu_resets,
     95  1.1  jmcneill 	.nresets = __arraycount(sun50i_a64_de2_ccu_resets),
     96  1.3  jmcneill 	.clks = sun8i_h3_de2_ccu_clks,
     97  1.3  jmcneill 	.nclks = __arraycount(sun8i_h3_de2_ccu_clks),
     98  1.1  jmcneill };
     99  1.1  jmcneill 
    100  1.4   thorpej static const struct device_compatible_entry compat_data[] = {
    101  1.4   thorpej 	{ .compat = "allwinner,sun8i-h3-de2-clk",
    102  1.4   thorpej 	  .data = &sun8i_h3_de2_config },
    103  1.8     skrll 	{ .compat = "allwinner,sun8i-v3s-de2-clk",
    104  1.8     skrll 	  .data = &sun8i_h3_de2_config },
    105  1.4   thorpej 	{ .compat = "allwinner,sun50i-a64-de2-clk",
    106  1.4   thorpej 	  .data = &sun50i_a64_de2_config },
    107  1.4   thorpej 	{ .compat = "allwinner,sun50i-h5-de2-clk",
    108  1.4   thorpej 	  .data = &sun50i_a64_de2_config },
    109  1.4   thorpej 
    110  1.6   thorpej 	DEVICE_COMPAT_EOL
    111  1.1  jmcneill };
    112  1.1  jmcneill 
    113  1.1  jmcneill static int
    114  1.1  jmcneill sunxi_de2_ccu_match(device_t parent, cfdata_t cf, void *aux)
    115  1.1  jmcneill {
    116  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    117  1.1  jmcneill 
    118  1.7   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    119  1.1  jmcneill }
    120  1.1  jmcneill 
    121  1.1  jmcneill static void
    122  1.1  jmcneill sunxi_de2_ccu_attach(device_t parent, device_t self, void *aux)
    123  1.1  jmcneill {
    124  1.1  jmcneill 	struct sunxi_ccu_softc * const sc = device_private(self);
    125  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    126  1.2  jmcneill 	const int phandle = faa->faa_phandle;
    127  1.1  jmcneill 	const struct sunxi_de2_ccu_config *conf;
    128  1.2  jmcneill 	struct clk *clk_bus, *clk_mod;
    129  1.2  jmcneill 	struct fdtbus_reset *rst;
    130  1.1  jmcneill 
    131  1.1  jmcneill 	sc->sc_dev = self;
    132  1.2  jmcneill 	sc->sc_phandle = phandle;
    133  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    134  1.1  jmcneill 
    135  1.7   thorpej 	conf = of_compatible_lookup(phandle, compat_data)->data;
    136  1.1  jmcneill 
    137  1.1  jmcneill 	sc->sc_resets = conf->resets;
    138  1.1  jmcneill 	sc->sc_nresets = conf->nresets;
    139  1.1  jmcneill 	sc->sc_clks = conf->clks;
    140  1.1  jmcneill 	sc->sc_nclks = conf->nclks;
    141  1.1  jmcneill 
    142  1.2  jmcneill 	clk_bus = fdtbus_clock_get(phandle, "bus");
    143  1.2  jmcneill 	if (clk_bus == NULL || clk_enable(clk_bus) != 0) {
    144  1.2  jmcneill 		aprint_error(": couldn't enable bus clock\n");
    145  1.2  jmcneill 		return;
    146  1.2  jmcneill 	}
    147  1.2  jmcneill 	clk_mod = fdtbus_clock_get(phandle, "mod");
    148  1.2  jmcneill 	if (clk_mod == NULL || clk_enable(clk_mod) != 0) {
    149  1.2  jmcneill 		aprint_error(": couldn't enable mod clock\n");
    150  1.2  jmcneill 		return;
    151  1.2  jmcneill 	}
    152  1.2  jmcneill 	rst = fdtbus_reset_get_index(phandle, 0);
    153  1.2  jmcneill 	if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
    154  1.2  jmcneill 		aprint_error(": couldn't de-assert reset\n");
    155  1.2  jmcneill 		return;
    156  1.2  jmcneill 	}
    157  1.2  jmcneill 
    158  1.1  jmcneill 	if (sunxi_ccu_attach(sc) != 0)
    159  1.1  jmcneill 		return;
    160  1.1  jmcneill 
    161  1.1  jmcneill 	aprint_naive("\n");
    162  1.1  jmcneill 	aprint_normal(": DE2 CCU\n");
    163  1.1  jmcneill 
    164  1.1  jmcneill 	sunxi_ccu_print(sc);
    165  1.1  jmcneill }
    166