Home | History | Annotate | Line # | Download | only in sunxi
sunxi_de2_ccu.c revision 1.3.4.2
      1  1.3.4.2  christos /* $NetBSD: sunxi_de2_ccu.c,v 1.3.4.2 2019/06/10 22:05:56 christos Exp $ */
      2  1.3.4.2  christos 
      3  1.3.4.2  christos /*-
      4  1.3.4.2  christos  * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.3.4.2  christos  * All rights reserved.
      6  1.3.4.2  christos  *
      7  1.3.4.2  christos  * Redistribution and use in source and binary forms, with or without
      8  1.3.4.2  christos  * modification, are permitted provided that the following conditions
      9  1.3.4.2  christos  * are met:
     10  1.3.4.2  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.3.4.2  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.3.4.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.3.4.2  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.3.4.2  christos  *    documentation and/or other materials provided with the distribution.
     15  1.3.4.2  christos  *
     16  1.3.4.2  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.3.4.2  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.3.4.2  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.3.4.2  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.3.4.2  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.3.4.2  christos  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.3.4.2  christos  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.3.4.2  christos  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.3.4.2  christos  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.3.4.2  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.3.4.2  christos  * SUCH DAMAGE.
     27  1.3.4.2  christos  */
     28  1.3.4.2  christos 
     29  1.3.4.2  christos #include <sys/cdefs.h>
     30  1.3.4.2  christos 
     31  1.3.4.2  christos __KERNEL_RCSID(1, "$NetBSD: sunxi_de2_ccu.c,v 1.3.4.2 2019/06/10 22:05:56 christos Exp $");
     32  1.3.4.2  christos 
     33  1.3.4.2  christos #include <sys/param.h>
     34  1.3.4.2  christos #include <sys/bus.h>
     35  1.3.4.2  christos #include <sys/device.h>
     36  1.3.4.2  christos #include <sys/systm.h>
     37  1.3.4.2  christos 
     38  1.3.4.2  christos #include <dev/fdt/fdtvar.h>
     39  1.3.4.2  christos 
     40  1.3.4.2  christos #include <arm/sunxi/sunxi_ccu.h>
     41  1.3.4.2  christos #include <arm/sunxi/sunxi_de2_ccu.h>
     42  1.3.4.2  christos 
     43  1.3.4.2  christos static int sunxi_de2_ccu_match(device_t, cfdata_t, void *);
     44  1.3.4.2  christos static void sunxi_de2_ccu_attach(device_t, device_t, void *);
     45  1.3.4.2  christos 
     46  1.3.4.2  christos CFATTACH_DECL_NEW(sunxi_de2ccu, sizeof(struct sunxi_ccu_softc),
     47  1.3.4.2  christos 	sunxi_de2_ccu_match, sunxi_de2_ccu_attach, NULL, NULL);
     48  1.3.4.2  christos 
     49  1.3.4.2  christos static struct sunxi_ccu_reset sun8i_h3_de2_ccu_resets[] = {
     50  1.3.4.2  christos 	SUNXI_CCU_RESET(DE2_RST_MIXER0, 0x08, 0),
     51  1.3.4.2  christos 	SUNXI_CCU_RESET(DE2_RST_WB, 0x08, 2),
     52  1.3.4.2  christos };
     53  1.3.4.2  christos 
     54  1.3.4.2  christos static struct sunxi_ccu_reset sun50i_a64_de2_ccu_resets[] = {
     55  1.3.4.2  christos 	SUNXI_CCU_RESET(DE2_RST_MIXER0, 0x08, 0),
     56  1.3.4.2  christos 	SUNXI_CCU_RESET(DE2_RST_MIXER1, 0x08, 1),
     57  1.3.4.2  christos 	SUNXI_CCU_RESET(DE2_RST_WB, 0x08, 2),
     58  1.3.4.2  christos };
     59  1.3.4.2  christos 
     60  1.3.4.2  christos static const char *mod_parents[] = { "mod" };
     61  1.3.4.2  christos 
     62  1.3.4.2  christos static struct sunxi_ccu_clk sun8i_h3_de2_ccu_clks[] = {
     63  1.3.4.2  christos 	SUNXI_CCU_GATE(DE2_CLK_BUS_MIXER0, "bus-mixer0", "bus", 0x04, 0),
     64  1.3.4.2  christos 	SUNXI_CCU_GATE(DE2_CLK_BUS_MIXER1, "bus-mixer1", "bus", 0x04, 1),
     65  1.3.4.2  christos 	SUNXI_CCU_GATE(DE2_CLK_BUS_WB, "bus-wb", "bus", 0x04, 2),
     66  1.3.4.2  christos 
     67  1.3.4.2  christos 	SUNXI_CCU_DIV(DE2_CLK_MIXER0_DIV, "mixer0-div", mod_parents,
     68  1.3.4.2  christos 	    0x0c, __BITS(3,0), 0, SUNXI_CCU_DIV_SET_RATE_PARENT),
     69  1.3.4.2  christos 	SUNXI_CCU_DIV(DE2_CLK_MIXER1_DIV, "mixer1-div", mod_parents,
     70  1.3.4.2  christos 	    0x0c, __BITS(7,4), 0, SUNXI_CCU_DIV_SET_RATE_PARENT),
     71  1.3.4.2  christos 	SUNXI_CCU_DIV(DE2_CLK_WB_DIV, "wb-div", mod_parents,
     72  1.3.4.2  christos 	    0x0c, __BITS(11,8), 0, SUNXI_CCU_DIV_SET_RATE_PARENT),
     73  1.3.4.2  christos 
     74  1.3.4.2  christos 	SUNXI_CCU_GATE(DE2_CLK_MIXER0, "mixer0", "mixer0-div", 0x00, 0),
     75  1.3.4.2  christos 	SUNXI_CCU_GATE(DE2_CLK_MIXER1, "mixer1", "mixer1-div", 0x00, 1),
     76  1.3.4.2  christos 	SUNXI_CCU_GATE(DE2_CLK_WB, "wb", "wb-div", 0x00, 2),
     77  1.3.4.2  christos };
     78  1.3.4.2  christos 
     79  1.3.4.2  christos struct sunxi_de2_ccu_config {
     80  1.3.4.2  christos 	struct sunxi_ccu_reset	*resets;
     81  1.3.4.2  christos 	u_int			nresets;
     82  1.3.4.2  christos 	struct sunxi_ccu_clk	*clks;
     83  1.3.4.2  christos 	u_int			nclks;
     84  1.3.4.2  christos };
     85  1.3.4.2  christos 
     86  1.3.4.2  christos static const struct sunxi_de2_ccu_config sun8i_h3_de2_config = {
     87  1.3.4.2  christos 	.resets = sun8i_h3_de2_ccu_resets,
     88  1.3.4.2  christos 	.nresets = __arraycount(sun8i_h3_de2_ccu_resets),
     89  1.3.4.2  christos 	.clks = sun8i_h3_de2_ccu_clks,
     90  1.3.4.2  christos 	.nclks = __arraycount(sun8i_h3_de2_ccu_clks),
     91  1.3.4.2  christos };
     92  1.3.4.2  christos 
     93  1.3.4.2  christos static const struct sunxi_de2_ccu_config sun50i_a64_de2_config = {
     94  1.3.4.2  christos 	.resets = sun50i_a64_de2_ccu_resets,
     95  1.3.4.2  christos 	.nresets = __arraycount(sun50i_a64_de2_ccu_resets),
     96  1.3.4.2  christos 	.clks = sun8i_h3_de2_ccu_clks,
     97  1.3.4.2  christos 	.nclks = __arraycount(sun8i_h3_de2_ccu_clks),
     98  1.3.4.2  christos };
     99  1.3.4.2  christos 
    100  1.3.4.2  christos static const struct of_compat_data compat_data[] = {
    101  1.3.4.2  christos 	{ "allwinner,sun8i-h3-de2-clk",		(uintptr_t)&sun8i_h3_de2_config },
    102  1.3.4.2  christos 	{ "allwinner,sun50i-a64-de2-clk",	(uintptr_t)&sun50i_a64_de2_config },
    103  1.3.4.2  christos 	{ "allwinner,sun50i-h5-de2-clk",	(uintptr_t)&sun50i_a64_de2_config },
    104  1.3.4.2  christos 	{ NULL }
    105  1.3.4.2  christos };
    106  1.3.4.2  christos 
    107  1.3.4.2  christos static int
    108  1.3.4.2  christos sunxi_de2_ccu_match(device_t parent, cfdata_t cf, void *aux)
    109  1.3.4.2  christos {
    110  1.3.4.2  christos 	struct fdt_attach_args * const faa = aux;
    111  1.3.4.2  christos 
    112  1.3.4.2  christos 	return of_match_compat_data(faa->faa_phandle, compat_data);
    113  1.3.4.2  christos }
    114  1.3.4.2  christos 
    115  1.3.4.2  christos static void
    116  1.3.4.2  christos sunxi_de2_ccu_attach(device_t parent, device_t self, void *aux)
    117  1.3.4.2  christos {
    118  1.3.4.2  christos 	struct sunxi_ccu_softc * const sc = device_private(self);
    119  1.3.4.2  christos 	struct fdt_attach_args * const faa = aux;
    120  1.3.4.2  christos 	const int phandle = faa->faa_phandle;
    121  1.3.4.2  christos 	const struct sunxi_de2_ccu_config *conf;
    122  1.3.4.2  christos 	struct clk *clk_bus, *clk_mod;
    123  1.3.4.2  christos 	struct fdtbus_reset *rst;
    124  1.3.4.2  christos 
    125  1.3.4.2  christos 	sc->sc_dev = self;
    126  1.3.4.2  christos 	sc->sc_phandle = phandle;
    127  1.3.4.2  christos 	sc->sc_bst = faa->faa_bst;
    128  1.3.4.2  christos 
    129  1.3.4.2  christos 	conf = (void *)of_search_compatible(phandle, compat_data)->data;
    130  1.3.4.2  christos 
    131  1.3.4.2  christos 	sc->sc_resets = conf->resets;
    132  1.3.4.2  christos 	sc->sc_nresets = conf->nresets;
    133  1.3.4.2  christos 	sc->sc_clks = conf->clks;
    134  1.3.4.2  christos 	sc->sc_nclks = conf->nclks;
    135  1.3.4.2  christos 
    136  1.3.4.2  christos 	clk_bus = fdtbus_clock_get(phandle, "bus");
    137  1.3.4.2  christos 	if (clk_bus == NULL || clk_enable(clk_bus) != 0) {
    138  1.3.4.2  christos 		aprint_error(": couldn't enable bus clock\n");
    139  1.3.4.2  christos 		return;
    140  1.3.4.2  christos 	}
    141  1.3.4.2  christos 	clk_mod = fdtbus_clock_get(phandle, "mod");
    142  1.3.4.2  christos 	if (clk_mod == NULL || clk_enable(clk_mod) != 0) {
    143  1.3.4.2  christos 		aprint_error(": couldn't enable mod clock\n");
    144  1.3.4.2  christos 		return;
    145  1.3.4.2  christos 	}
    146  1.3.4.2  christos 	rst = fdtbus_reset_get_index(phandle, 0);
    147  1.3.4.2  christos 	if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
    148  1.3.4.2  christos 		aprint_error(": couldn't de-assert reset\n");
    149  1.3.4.2  christos 		return;
    150  1.3.4.2  christos 	}
    151  1.3.4.2  christos 
    152  1.3.4.2  christos 	if (sunxi_ccu_attach(sc) != 0)
    153  1.3.4.2  christos 		return;
    154  1.3.4.2  christos 
    155  1.3.4.2  christos 	aprint_naive("\n");
    156  1.3.4.2  christos 	aprint_normal(": DE2 CCU\n");
    157  1.3.4.2  christos 
    158  1.3.4.2  christos 	sunxi_ccu_print(sc);
    159  1.3.4.2  christos }
    160