11.3Sthorpej/* $NetBSD: sun9i_a80_mmcclk.c,v 1.3 2021/01/27 03:10:20 thorpej Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
141.1Sjmcneill *    documentation and/or other materials provided with the distribution.
151.1Sjmcneill *
161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sjmcneill * SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include <sys/cdefs.h>
301.1Sjmcneill
311.3Sthorpej__KERNEL_RCSID(1, "$NetBSD: sun9i_a80_mmcclk.c,v 1.3 2021/01/27 03:10:20 thorpej Exp $");
321.1Sjmcneill
331.1Sjmcneill#include <sys/param.h>
341.1Sjmcneill#include <sys/bus.h>
351.1Sjmcneill#include <sys/device.h>
361.1Sjmcneill#include <sys/systm.h>
371.1Sjmcneill
381.1Sjmcneill#include <dev/fdt/fdtvar.h>
391.1Sjmcneill
401.1Sjmcneill#include <arm/sunxi/sunxi_ccu.h>
411.1Sjmcneill
421.1Sjmcneill#define	SDC_COMM(port)	(0x04 * (port))
431.1Sjmcneill
441.1Sjmcneillstatic int sun9i_a80_mmcclk_match(device_t, cfdata_t, void *);
451.1Sjmcneillstatic void sun9i_a80_mmcclk_attach(device_t, device_t, void *);
461.1Sjmcneill
471.3Sthorpejstatic const struct device_compatible_entry compat_data[] = {
481.3Sthorpej	{ .compat = "allwinner,sun9i-a80-mmc-config-clk" },
491.3Sthorpej	DEVICE_COMPAT_EOL
501.1Sjmcneill};
511.1Sjmcneill
521.1SjmcneillCFATTACH_DECL_NEW(sunxi_a80_mmcclk, sizeof(struct sunxi_ccu_softc),
531.1Sjmcneill	sun9i_a80_mmcclk_match, sun9i_a80_mmcclk_attach, NULL, NULL);
541.1Sjmcneill
551.1Sjmcneillstatic struct sunxi_ccu_reset sun9i_a80_mmcclk_resets[] = {
561.1Sjmcneill	SUNXI_CCU_RESET(0, SDC_COMM(0), 18),
571.1Sjmcneill	SUNXI_CCU_RESET(1, SDC_COMM(1), 18),
581.1Sjmcneill	SUNXI_CCU_RESET(2, SDC_COMM(2), 18),
591.1Sjmcneill	SUNXI_CCU_RESET(3, SDC_COMM(3), 18),
601.1Sjmcneill};
611.1Sjmcneill
621.1Sjmcneillstatic struct sunxi_ccu_clk sun9i_a80_mmcclk_clks[] = {
631.1Sjmcneill	SUNXI_CCU_GATE(0, "mmc0_config", "ahb", SDC_COMM(0), 16),
641.1Sjmcneill	SUNXI_CCU_GATE(1, "mmc1_config", "ahb", SDC_COMM(1), 16),
651.1Sjmcneill	SUNXI_CCU_GATE(2, "mmc2_config", "ahb", SDC_COMM(2), 16),
661.1Sjmcneill	SUNXI_CCU_GATE(3, "mmc3_config", "ahb", SDC_COMM(3), 16),
671.1Sjmcneill};
681.1Sjmcneill
691.1Sjmcneillstatic int
701.1Sjmcneillsun9i_a80_mmcclk_match(device_t parent, cfdata_t cf, void *aux)
711.1Sjmcneill{
721.1Sjmcneill	struct fdt_attach_args * const faa = aux;
731.1Sjmcneill
741.3Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
751.1Sjmcneill}
761.1Sjmcneill
771.1Sjmcneillstatic void
781.1Sjmcneillsun9i_a80_mmcclk_attach(device_t parent, device_t self, void *aux)
791.1Sjmcneill{
801.1Sjmcneill	struct sunxi_ccu_softc * const sc = device_private(self);
811.1Sjmcneill	struct fdt_attach_args * const faa = aux;
821.2Sjmcneill	const int phandle = faa->faa_phandle;
831.2Sjmcneill	struct fdtbus_reset *rst;
841.2Sjmcneill	struct clk *clk;
851.1Sjmcneill
861.1Sjmcneill	sc->sc_dev = self;
871.1Sjmcneill	sc->sc_phandle = faa->faa_phandle;
881.1Sjmcneill	sc->sc_bst = faa->faa_bst;
891.1Sjmcneill
901.1Sjmcneill	sc->sc_resets = sun9i_a80_mmcclk_resets;
911.1Sjmcneill	sc->sc_nresets = __arraycount(sun9i_a80_mmcclk_resets);
921.1Sjmcneill
931.1Sjmcneill	sc->sc_clks = sun9i_a80_mmcclk_clks;
941.1Sjmcneill	sc->sc_nclks = __arraycount(sun9i_a80_mmcclk_clks);
951.1Sjmcneill
961.2Sjmcneill	clk = fdtbus_clock_get(phandle, "ahb");
971.2Sjmcneill	if (clk == NULL || clk_enable(clk) != 0) {
981.2Sjmcneill		aprint_error(": couldn't enable clock\n");
991.2Sjmcneill		return;
1001.2Sjmcneill	}
1011.2Sjmcneill	rst = fdtbus_reset_get(phandle, "ahb");
1021.2Sjmcneill	if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
1031.2Sjmcneill		aprint_error(": couldn't de-assert reset\n");
1041.2Sjmcneill		return;
1051.2Sjmcneill	}
1061.2Sjmcneill
1071.1Sjmcneill	if (sunxi_ccu_attach(sc) != 0)
1081.1Sjmcneill		return;
1091.1Sjmcneill
1101.1Sjmcneill	aprint_naive("\n");
1111.1Sjmcneill	aprint_normal(": A80 SD/MMC-COMM\n");
1121.1Sjmcneill
1131.1Sjmcneill	sunxi_ccu_print(sc);
1141.1Sjmcneill}
115