11.4Sthorpej/* $NetBSD: meson_resets.c,v 1.4 2021/01/27 03:10:18 thorpej Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2017-2019 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.4Sthorpej__KERNEL_RCSID(0, "$NetBSD: meson_resets.c,v 1.4 2021/01/27 03:10:18 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.1Sjmcneill#define	LEVEL_OFFSET		0x7c
451.1Sjmcneill
461.4Sthorpejstatic const struct device_compatible_entry compat_data[] = {
471.4Sthorpej	{ .compat = "amlogic,meson8b-reset" },
481.4Sthorpej	{ .compat = "amlogic,meson-axg-reset" },
491.4Sthorpej	{ .compat = "amlogic,meson-gxbb-reset" },
501.4Sthorpej	DEVICE_COMPAT_EOL
511.1Sjmcneill};
521.1Sjmcneill
531.1Sjmcneillstruct meson_resets_softc {
541.1Sjmcneill	device_t		sc_dev;
551.1Sjmcneill	bus_space_tag_t		sc_bst;
561.1Sjmcneill	bus_space_handle_t	sc_bsh;
571.1Sjmcneill};
581.1Sjmcneill
591.1Sjmcneill#define	RESET_READ(sc, reg)		\
601.1Sjmcneill	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
611.1Sjmcneill#define	RESET_WRITE(sc, reg, val)	\
621.1Sjmcneill	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
631.1Sjmcneill
641.1Sjmcneillstatic void *
651.1Sjmcneillmeson_resets_acquire(device_t dev, const void *data, size_t len)
661.1Sjmcneill{
671.1Sjmcneill	if (len != 4)
681.1Sjmcneill		return NULL;
691.1Sjmcneill
701.1Sjmcneill	/* Specifier is an index. Just return it. */
711.1Sjmcneill	return (void *)(uintptr_t)be32dec(data);
721.1Sjmcneill}
731.1Sjmcneill
741.1Sjmcneillstatic void
751.1Sjmcneillmeson_resets_release(device_t dev, void *priv)
761.1Sjmcneill{
771.1Sjmcneill}
781.1Sjmcneill
791.1Sjmcneillstatic int
801.1Sjmcneillmeson_resets_assert(device_t dev, void *priv)
811.1Sjmcneill{
821.1Sjmcneill	struct meson_resets_softc * const sc = device_private(dev);
831.1Sjmcneill	const uintptr_t index = (uintptr_t)priv;
841.1Sjmcneill
851.1Sjmcneill	const bus_size_t reset_reg = RESET_REG(index) + LEVEL_OFFSET;
861.1Sjmcneill	const uint32_t reset_mask = RESET_MASK(index);
871.1Sjmcneill
881.1Sjmcneill	const uint32_t val = RESET_READ(sc, reset_reg);
891.1Sjmcneill	RESET_WRITE(sc, reset_reg, val & ~reset_mask);
901.1Sjmcneill
911.1Sjmcneill	return 0;
921.1Sjmcneill}
931.1Sjmcneill
941.1Sjmcneillstatic int
951.1Sjmcneillmeson_resets_deassert(device_t dev, void *priv)
961.1Sjmcneill{
971.1Sjmcneill	struct meson_resets_softc * const sc = device_private(dev);
981.1Sjmcneill	const uintptr_t index = (uintptr_t)priv;
991.1Sjmcneill
1001.1Sjmcneill	const bus_size_t reset_reg = RESET_REG(index) + LEVEL_OFFSET;
1011.1Sjmcneill	const uint32_t reset_mask = RESET_MASK(index);
1021.1Sjmcneill
1031.1Sjmcneill	const uint32_t val = RESET_READ(sc, reset_reg);
1041.1Sjmcneill	RESET_WRITE(sc, reset_reg, val | reset_mask);
1051.1Sjmcneill
1061.1Sjmcneill	return 0;
1071.1Sjmcneill}
1081.1Sjmcneill
1091.1Sjmcneillstatic const struct fdtbus_reset_controller_func meson_fdtreset_funcs = {
1101.1Sjmcneill	.acquire = meson_resets_acquire,
1111.1Sjmcneill	.release = meson_resets_release,
1121.1Sjmcneill	.reset_assert = meson_resets_assert,
1131.1Sjmcneill	.reset_deassert = meson_resets_deassert,
1141.1Sjmcneill};
1151.1Sjmcneill
1161.1Sjmcneillstatic int
1171.1Sjmcneillmeson_resets_match(device_t parent, cfdata_t cf, void *aux)
1181.1Sjmcneill{
1191.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1201.1Sjmcneill
1211.4Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
1221.1Sjmcneill}
1231.1Sjmcneill
1241.1Sjmcneillstatic void
1251.1Sjmcneillmeson_resets_attach(device_t parent, device_t self, void *aux)
1261.1Sjmcneill{
1271.1Sjmcneill	struct meson_resets_softc * const sc = device_private(self);
1281.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1291.1Sjmcneill	const int phandle = faa->faa_phandle;
1301.1Sjmcneill	bus_addr_t addr;
1311.1Sjmcneill	bus_size_t size;
1321.1Sjmcneill
1331.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1341.1Sjmcneill		aprint_error(": couldn't get registers\n");
1351.1Sjmcneill		return;
1361.1Sjmcneill	}
1371.1Sjmcneill
1381.1Sjmcneill	sc->sc_dev = self;
1391.1Sjmcneill	sc->sc_bst = faa->faa_bst;
1401.1Sjmcneill	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
1411.1Sjmcneill		aprint_error(": couldn't map registers\n");
1421.1Sjmcneill		return;
1431.1Sjmcneill	}
1441.1Sjmcneill
1451.1Sjmcneill	aprint_naive("\n");
1461.1Sjmcneill	aprint_normal("\n");
1471.1Sjmcneill
1481.1Sjmcneill	fdtbus_register_reset_controller(sc->sc_dev, phandle,
1491.1Sjmcneill	    &meson_fdtreset_funcs);
1501.1Sjmcneill}
1511.1Sjmcneill
1521.1SjmcneillCFATTACH_DECL_NEW(meson_resets, sizeof(struct meson_resets_softc),
1531.1Sjmcneill    meson_resets_match, meson_resets_attach, NULL, NULL);
154