11.2Sthorpej/* $NetBSD: sunxi_resets.c,v 1.2 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.2Sthorpej__KERNEL_RCSID(0, "$NetBSD: sunxi_resets.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $");
311.1Sjmcneill
321.1Sjmcneill#include <sys/param.h>
331.1Sjmcneill#include <sys/bus.h>
341.1Sjmcneill#include <sys/cpu.h>
351.1Sjmcneill#include <sys/device.h>
361.1Sjmcneill
371.1Sjmcneill#include <dev/fdt/fdtvar.h>
381.1Sjmcneill
391.1Sjmcneill#include <dev/clk/clk_backend.h>
401.1Sjmcneill
411.1Sjmcneill#define	RESET_REG(index)	(((index) / 32) * 4)
421.1Sjmcneill#define	RESET_MASK(index)	__BIT((index) % 32)
431.1Sjmcneill
441.2Sthorpejstatic const struct device_compatible_entry compat_data[] = {
451.2Sthorpej	{ .compat = "allwinner,sun6i-a31-clock-reset" },
461.2Sthorpej	DEVICE_COMPAT_EOL
471.1Sjmcneill};
481.1Sjmcneill
491.1Sjmcneillstruct sunxi_resets_softc {
501.1Sjmcneill	device_t		sc_dev;
511.1Sjmcneill	bus_space_tag_t		sc_bst;
521.1Sjmcneill	bus_space_handle_t	sc_bsh;
531.1Sjmcneill};
541.1Sjmcneill
551.1Sjmcneill#define	RESET_READ(sc, reg)		\
561.1Sjmcneill	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
571.1Sjmcneill#define	RESET_WRITE(sc, reg, val)	\
581.1Sjmcneill	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
591.1Sjmcneill
601.1Sjmcneillstatic void *
611.1Sjmcneillsunxi_resets_acquire(device_t dev, const void *data, size_t len)
621.1Sjmcneill{
631.1Sjmcneill	if (len != 4)
641.1Sjmcneill		return NULL;
651.1Sjmcneill
661.1Sjmcneill	/* Specifier is an index. Just return it. */
671.1Sjmcneill	return (void *)(uintptr_t)be32dec(data);
681.1Sjmcneill}
691.1Sjmcneill
701.1Sjmcneillstatic void
711.1Sjmcneillsunxi_resets_release(device_t dev, void *priv)
721.1Sjmcneill{
731.1Sjmcneill}
741.1Sjmcneill
751.1Sjmcneillstatic int
761.1Sjmcneillsunxi_resets_assert(device_t dev, void *priv)
771.1Sjmcneill{
781.1Sjmcneill	struct sunxi_resets_softc * const sc = device_private(dev);
791.1Sjmcneill	const uintptr_t index = (uintptr_t)priv;
801.1Sjmcneill
811.1Sjmcneill	const bus_size_t reset_reg = RESET_REG(index);
821.1Sjmcneill	const uint32_t reset_mask = RESET_MASK(index);
831.1Sjmcneill
841.1Sjmcneill	const uint32_t val = RESET_READ(sc, reset_reg);
851.1Sjmcneill	RESET_WRITE(sc, reset_reg, val & ~reset_mask);
861.1Sjmcneill
871.1Sjmcneill	return 0;
881.1Sjmcneill}
891.1Sjmcneill
901.1Sjmcneillstatic int
911.1Sjmcneillsunxi_resets_deassert(device_t dev, void *priv)
921.1Sjmcneill{
931.1Sjmcneill	struct sunxi_resets_softc * const sc = device_private(dev);
941.1Sjmcneill	const uintptr_t index = (uintptr_t)priv;
951.1Sjmcneill
961.1Sjmcneill	const bus_size_t reset_reg = RESET_REG(index);
971.1Sjmcneill	const uint32_t reset_mask = RESET_MASK(index);
981.1Sjmcneill
991.1Sjmcneill	const uint32_t val = RESET_READ(sc, reset_reg);
1001.1Sjmcneill	RESET_WRITE(sc, reset_reg, val | reset_mask);
1011.1Sjmcneill
1021.1Sjmcneill	return 0;
1031.1Sjmcneill}
1041.1Sjmcneill
1051.1Sjmcneillstatic const struct fdtbus_reset_controller_func sunxi_fdtreset_funcs = {
1061.1Sjmcneill	.acquire = sunxi_resets_acquire,
1071.1Sjmcneill	.release = sunxi_resets_release,
1081.1Sjmcneill	.reset_assert = sunxi_resets_assert,
1091.1Sjmcneill	.reset_deassert = sunxi_resets_deassert,
1101.1Sjmcneill};
1111.1Sjmcneill
1121.1Sjmcneillstatic int
1131.1Sjmcneillsunxi_resets_match(device_t parent, cfdata_t cf, void *aux)
1141.1Sjmcneill{
1151.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1161.1Sjmcneill
1171.2Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
1181.1Sjmcneill}
1191.1Sjmcneill
1201.1Sjmcneillstatic void
1211.1Sjmcneillsunxi_resets_attach(device_t parent, device_t self, void *aux)
1221.1Sjmcneill{
1231.1Sjmcneill	struct sunxi_resets_softc * const sc = device_private(self);
1241.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1251.1Sjmcneill	const int phandle = faa->faa_phandle;
1261.1Sjmcneill	bus_addr_t addr;
1271.1Sjmcneill	bus_size_t size;
1281.1Sjmcneill
1291.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1301.1Sjmcneill		aprint_error(": couldn't get registers\n");
1311.1Sjmcneill		return;
1321.1Sjmcneill	}
1331.1Sjmcneill
1341.1Sjmcneill	sc->sc_dev = self;
1351.1Sjmcneill	sc->sc_bst = faa->faa_bst;
1361.1Sjmcneill	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
1371.1Sjmcneill		aprint_error(": couldn't map registers\n");
1381.1Sjmcneill		return;
1391.1Sjmcneill	}
1401.1Sjmcneill
1411.1Sjmcneill	aprint_naive("\n");
1421.1Sjmcneill	aprint_normal("\n");
1431.1Sjmcneill
1441.1Sjmcneill	fdtbus_register_reset_controller(sc->sc_dev, phandle,
1451.1Sjmcneill	    &sunxi_fdtreset_funcs);
1461.1Sjmcneill}
1471.1Sjmcneill
1481.1SjmcneillCFATTACH_DECL_NEW(sunxi_resets, sizeof(struct sunxi_resets_softc),
1491.1Sjmcneill    sunxi_resets_match, sunxi_resets_attach, NULL, NULL);
150