xref: /netbsd-src/sys/arch/arm/sunxi/sun9i_a80_mmcclk.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1 /* $NetBSD: sun9i_a80_mmcclk.c,v 1.3 2021/01/27 03:10:20 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 
31 __KERNEL_RCSID(1, "$NetBSD: sun9i_a80_mmcclk.c,v 1.3 2021/01/27 03:10:20 thorpej Exp $");
32 
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/device.h>
36 #include <sys/systm.h>
37 
38 #include <dev/fdt/fdtvar.h>
39 
40 #include <arm/sunxi/sunxi_ccu.h>
41 
42 #define	SDC_COMM(port)	(0x04 * (port))
43 
44 static int sun9i_a80_mmcclk_match(device_t, cfdata_t, void *);
45 static void sun9i_a80_mmcclk_attach(device_t, device_t, void *);
46 
47 static const struct device_compatible_entry compat_data[] = {
48 	{ .compat = "allwinner,sun9i-a80-mmc-config-clk" },
49 	DEVICE_COMPAT_EOL
50 };
51 
52 CFATTACH_DECL_NEW(sunxi_a80_mmcclk, sizeof(struct sunxi_ccu_softc),
53 	sun9i_a80_mmcclk_match, sun9i_a80_mmcclk_attach, NULL, NULL);
54 
55 static struct sunxi_ccu_reset sun9i_a80_mmcclk_resets[] = {
56 	SUNXI_CCU_RESET(0, SDC_COMM(0), 18),
57 	SUNXI_CCU_RESET(1, SDC_COMM(1), 18),
58 	SUNXI_CCU_RESET(2, SDC_COMM(2), 18),
59 	SUNXI_CCU_RESET(3, SDC_COMM(3), 18),
60 };
61 
62 static struct sunxi_ccu_clk sun9i_a80_mmcclk_clks[] = {
63 	SUNXI_CCU_GATE(0, "mmc0_config", "ahb", SDC_COMM(0), 16),
64 	SUNXI_CCU_GATE(1, "mmc1_config", "ahb", SDC_COMM(1), 16),
65 	SUNXI_CCU_GATE(2, "mmc2_config", "ahb", SDC_COMM(2), 16),
66 	SUNXI_CCU_GATE(3, "mmc3_config", "ahb", SDC_COMM(3), 16),
67 };
68 
69 static int
sun9i_a80_mmcclk_match(device_t parent,cfdata_t cf,void * aux)70 sun9i_a80_mmcclk_match(device_t parent, cfdata_t cf, void *aux)
71 {
72 	struct fdt_attach_args * const faa = aux;
73 
74 	return of_compatible_match(faa->faa_phandle, compat_data);
75 }
76 
77 static void
sun9i_a80_mmcclk_attach(device_t parent,device_t self,void * aux)78 sun9i_a80_mmcclk_attach(device_t parent, device_t self, void *aux)
79 {
80 	struct sunxi_ccu_softc * const sc = device_private(self);
81 	struct fdt_attach_args * const faa = aux;
82 	const int phandle = faa->faa_phandle;
83 	struct fdtbus_reset *rst;
84 	struct clk *clk;
85 
86 	sc->sc_dev = self;
87 	sc->sc_phandle = faa->faa_phandle;
88 	sc->sc_bst = faa->faa_bst;
89 
90 	sc->sc_resets = sun9i_a80_mmcclk_resets;
91 	sc->sc_nresets = __arraycount(sun9i_a80_mmcclk_resets);
92 
93 	sc->sc_clks = sun9i_a80_mmcclk_clks;
94 	sc->sc_nclks = __arraycount(sun9i_a80_mmcclk_clks);
95 
96 	clk = fdtbus_clock_get(phandle, "ahb");
97 	if (clk == NULL || clk_enable(clk) != 0) {
98 		aprint_error(": couldn't enable clock\n");
99 		return;
100 	}
101 	rst = fdtbus_reset_get(phandle, "ahb");
102 	if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
103 		aprint_error(": couldn't de-assert reset\n");
104 		return;
105 	}
106 
107 	if (sunxi_ccu_attach(sc) != 0)
108 		return;
109 
110 	aprint_naive("\n");
111 	aprint_normal(": A80 SD/MMC-COMM\n");
112 
113 	sunxi_ccu_print(sc);
114 }
115