xref: /netbsd-src/sys/arch/arm/sunxi/sun50i_a64_r_ccu.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: sun50i_a64_r_ccu.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $ */
247219325Sjmcneill 
347219325Sjmcneill /*-
447219325Sjmcneill  * Copyright (c) 2017-2018 Jared McNeill <jmcneill@invisible.ca>
547219325Sjmcneill  * All rights reserved.
647219325Sjmcneill  *
747219325Sjmcneill  * Redistribution and use in source and binary forms, with or without
847219325Sjmcneill  * modification, are permitted provided that the following conditions
947219325Sjmcneill  * are met:
1047219325Sjmcneill  * 1. Redistributions of source code must retain the above copyright
1147219325Sjmcneill  *    notice, this list of conditions and the following disclaimer.
1247219325Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
1347219325Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
1447219325Sjmcneill  *    documentation and/or other materials provided with the distribution.
1547219325Sjmcneill  *
1647219325Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1747219325Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1847219325Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1947219325Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2047219325Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2147219325Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2247219325Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2347219325Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2447219325Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2547219325Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2647219325Sjmcneill  * SUCH DAMAGE.
2747219325Sjmcneill  */
2847219325Sjmcneill 
2947219325Sjmcneill #include <sys/cdefs.h>
3047219325Sjmcneill 
31*6e54367aSthorpej __KERNEL_RCSID(1, "$NetBSD: sun50i_a64_r_ccu.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $");
3247219325Sjmcneill 
3347219325Sjmcneill #include <sys/param.h>
3447219325Sjmcneill #include <sys/bus.h>
3547219325Sjmcneill #include <sys/device.h>
3647219325Sjmcneill #include <sys/systm.h>
3747219325Sjmcneill 
3847219325Sjmcneill #include <dev/fdt/fdtvar.h>
3947219325Sjmcneill 
4047219325Sjmcneill #include <arm/sunxi/sunxi_ccu.h>
4147219325Sjmcneill #include <arm/sunxi/sun50i_a64_r_ccu.h>
4247219325Sjmcneill 
4347219325Sjmcneill #define	AR100_CFG_REG		0x00
4447219325Sjmcneill #define	APB0_CFG_REG		0x0c
4547219325Sjmcneill #define	APB0_GATE_REG		0x28
4647219325Sjmcneill #define	IR_CFG_REG		0x54
4747219325Sjmcneill #define	APB0_RESET_REG		0xb0
4847219325Sjmcneill 
4947219325Sjmcneill static int sun50i_a64_r_ccu_match(device_t, cfdata_t, void *);
5047219325Sjmcneill static void sun50i_a64_r_ccu_attach(device_t, device_t, void *);
5147219325Sjmcneill 
52*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
53*6e54367aSthorpej 	{ .compat = "allwinner,sun50i-a64-r-ccu" },
54*6e54367aSthorpej 	DEVICE_COMPAT_EOL
5547219325Sjmcneill };
5647219325Sjmcneill 
5747219325Sjmcneill CFATTACH_DECL_NEW(sunxi_a64_r_ccu, sizeof(struct sunxi_ccu_softc),
5847219325Sjmcneill 	sun50i_a64_r_ccu_match, sun50i_a64_r_ccu_attach, NULL, NULL);
5947219325Sjmcneill 
6047219325Sjmcneill static struct sunxi_ccu_reset sun50i_a64_r_ccu_resets[] = {
6147219325Sjmcneill 	SUNXI_CCU_RESET(A64_R_RST_APB0_IR, APB0_RESET_REG, 1),
6247219325Sjmcneill 	SUNXI_CCU_RESET(A64_R_RST_APB0_TIMER, APB0_RESET_REG, 2),
6347219325Sjmcneill 	SUNXI_CCU_RESET(A64_R_RST_APB0_RSB, APB0_RESET_REG, 3),
6447219325Sjmcneill 	SUNXI_CCU_RESET(A64_R_RST_APB0_UART, APB0_RESET_REG, 4),
6547219325Sjmcneill 	SUNXI_CCU_RESET(A64_R_RST_APB0_I2C, APB0_RESET_REG, 6),
6647219325Sjmcneill };
6747219325Sjmcneill 
6847219325Sjmcneill static const char *ar100_parents[] = { "losc", "hosc", "pll_periph0", "losc" };
6947219325Sjmcneill static const char *apb0_parents[] = { "ahb0" };
7047219325Sjmcneill static const char *mod_parents[] = { "losc", "hosc" };
7147219325Sjmcneill 
7247219325Sjmcneill static struct sunxi_ccu_clk sun50i_a64_r_ccu_clks[] = {
7347219325Sjmcneill 	SUNXI_CCU_PREDIV(A64_R_CLK_AR100, "ar100", ar100_parents,
7447219325Sjmcneill 	    AR100_CFG_REG,	/* reg */
7547219325Sjmcneill 	    __BITS(12,8),	/* prediv */
7647219325Sjmcneill 	    __BIT(2),		/* prediv_sel */
7747219325Sjmcneill 	    __BITS(5,4),	/* div */
7847219325Sjmcneill 	    __BITS(17,16),	/* sel */
7947219325Sjmcneill 	    SUNXI_CCU_PREDIV_POWER_OF_TWO),
8047219325Sjmcneill 
8147219325Sjmcneill 	SUNXI_CCU_FIXED_FACTOR(A64_R_CLK_AHB0, "ahb0", "ar100", 1, 1),
8247219325Sjmcneill 
8347219325Sjmcneill 	SUNXI_CCU_DIV(A64_R_CLK_APB0, "apb0", apb0_parents,
8447219325Sjmcneill 	    APB0_CFG_REG,	/* reg */
8547219325Sjmcneill 	    __BITS(1,0),	/* div */
8647219325Sjmcneill 	    0,			/* sel */
8747219325Sjmcneill 	    SUNXI_CCU_DIV_POWER_OF_TWO),
8847219325Sjmcneill 
8947219325Sjmcneill 	SUNXI_CCU_GATE(A64_R_CLK_APB0_PIO, "apb0-pio", "apb0",
9047219325Sjmcneill 	    APB0_GATE_REG, 0),
9147219325Sjmcneill 	SUNXI_CCU_GATE(A64_R_CLK_APB0_IR, "apb0-ir", "apb0",
9247219325Sjmcneill 	    APB0_GATE_REG, 1),
9347219325Sjmcneill 	SUNXI_CCU_GATE(A64_R_CLK_APB0_TIMER, "apb0-timer", "apb0",
9447219325Sjmcneill 	    APB0_GATE_REG, 2),
9547219325Sjmcneill 	SUNXI_CCU_GATE(A64_R_CLK_APB0_RSB, "apb0-rsb", "apb0",
9647219325Sjmcneill 	    APB0_GATE_REG, 3),
9747219325Sjmcneill 	SUNXI_CCU_GATE(A64_R_CLK_APB0_UART, "apb0-uart", "apb0",
9847219325Sjmcneill 	    APB0_GATE_REG, 4),
9947219325Sjmcneill 	SUNXI_CCU_GATE(A64_R_CLK_APB0_I2C, "apb0-i2c", "apb0",
10047219325Sjmcneill 	    APB0_GATE_REG, 6),
10147219325Sjmcneill 	SUNXI_CCU_GATE(A64_R_CLK_APB0_TWD, "apb0-twd", "apb0",
10247219325Sjmcneill 	    APB0_GATE_REG, 7),
10347219325Sjmcneill 	SUNXI_CCU_NM(A64_R_CLK_IR, "ir", mod_parents,
10447219325Sjmcneill 	    IR_CFG_REG,		/* reg */
10547219325Sjmcneill 	    __BITS(3,0),	/* n */
10647219325Sjmcneill 	    __BITS(17,16),	/* m */
10747219325Sjmcneill 	    __BITS(25,24),	/* sel */
10847219325Sjmcneill 	    __BIT(31),		/* enable */
10947219325Sjmcneill 	    0)
11047219325Sjmcneill };
11147219325Sjmcneill 
11247219325Sjmcneill static int
sun50i_a64_r_ccu_match(device_t parent,cfdata_t cf,void * aux)11347219325Sjmcneill sun50i_a64_r_ccu_match(device_t parent, cfdata_t cf, void *aux)
11447219325Sjmcneill {
11547219325Sjmcneill 	struct fdt_attach_args * const faa = aux;
11647219325Sjmcneill 
117*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
11847219325Sjmcneill }
11947219325Sjmcneill 
12047219325Sjmcneill static void
sun50i_a64_r_ccu_attach(device_t parent,device_t self,void * aux)12147219325Sjmcneill sun50i_a64_r_ccu_attach(device_t parent, device_t self, void *aux)
12247219325Sjmcneill {
12347219325Sjmcneill 	struct sunxi_ccu_softc * const sc = device_private(self);
12447219325Sjmcneill 	struct fdt_attach_args * const faa = aux;
12547219325Sjmcneill 
12647219325Sjmcneill 	sc->sc_dev = self;
12747219325Sjmcneill 	sc->sc_phandle = faa->faa_phandle;
12847219325Sjmcneill 	sc->sc_bst = faa->faa_bst;
12947219325Sjmcneill 
13047219325Sjmcneill 	sc->sc_resets = sun50i_a64_r_ccu_resets;
13147219325Sjmcneill 	sc->sc_nresets = __arraycount(sun50i_a64_r_ccu_resets);
13247219325Sjmcneill 
13347219325Sjmcneill 	sc->sc_clks = sun50i_a64_r_ccu_clks;
13447219325Sjmcneill 	sc->sc_nclks = __arraycount(sun50i_a64_r_ccu_clks);
13547219325Sjmcneill 
13647219325Sjmcneill 	if (sunxi_ccu_attach(sc) != 0)
13747219325Sjmcneill 		return;
13847219325Sjmcneill 
13947219325Sjmcneill 	aprint_naive("\n");
14047219325Sjmcneill 	aprint_normal(": A64 PRCM CCU\n");
14147219325Sjmcneill 
14247219325Sjmcneill 	sunxi_ccu_print(sc);
14347219325Sjmcneill }
144