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