xref: /netbsd-src/sys/arch/arm/nvidia/tegra_mc.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: tegra_mc.c,v 1.12 2021/01/27 03:10:19 thorpej Exp $ */
2d4fd1143Sjmcneill 
3d4fd1143Sjmcneill /*-
4d4fd1143Sjmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
5d4fd1143Sjmcneill  * All rights reserved.
6d4fd1143Sjmcneill  *
7d4fd1143Sjmcneill  * Redistribution and use in source and binary forms, with or without
8d4fd1143Sjmcneill  * modification, are permitted provided that the following conditions
9d4fd1143Sjmcneill  * are met:
10d4fd1143Sjmcneill  * 1. Redistributions of source code must retain the above copyright
11d4fd1143Sjmcneill  *    notice, this list of conditions and the following disclaimer.
12d4fd1143Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13d4fd1143Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
14d4fd1143Sjmcneill  *    documentation and/or other materials provided with the distribution.
15d4fd1143Sjmcneill  *
16d4fd1143Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17d4fd1143Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18d4fd1143Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19d4fd1143Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20d4fd1143Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21d4fd1143Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22d4fd1143Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23d4fd1143Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24d4fd1143Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25d4fd1143Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26d4fd1143Sjmcneill  * SUCH DAMAGE.
27d4fd1143Sjmcneill  */
28d4fd1143Sjmcneill 
29d4fd1143Sjmcneill #include "locators.h"
30d4fd1143Sjmcneill 
31d4fd1143Sjmcneill #include <sys/cdefs.h>
32*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: tegra_mc.c,v 1.12 2021/01/27 03:10:19 thorpej Exp $");
33d4fd1143Sjmcneill 
34d4fd1143Sjmcneill #include <sys/param.h>
35d4fd1143Sjmcneill #include <sys/bus.h>
36d4fd1143Sjmcneill #include <sys/device.h>
37d4fd1143Sjmcneill #include <sys/intr.h>
38d4fd1143Sjmcneill #include <sys/systm.h>
39d4fd1143Sjmcneill #include <sys/kernel.h>
40d4fd1143Sjmcneill 
41d4fd1143Sjmcneill #include <arm/nvidia/tegra_reg.h>
42d4fd1143Sjmcneill #include <arm/nvidia/tegra_mcreg.h>
43d4fd1143Sjmcneill #include <arm/nvidia/tegra_var.h>
44d4fd1143Sjmcneill 
45d59db8d0Sjmcneill #include <dev/fdt/fdtvar.h>
46d59db8d0Sjmcneill 
47d4fd1143Sjmcneill static int	tegra_mc_match(device_t, cfdata_t, void *);
48d4fd1143Sjmcneill static void	tegra_mc_attach(device_t, device_t, void *);
49d4fd1143Sjmcneill 
5077011a56Sjakllsch static int	tegra_mc_intr(void *);
5177011a56Sjakllsch 
52d4fd1143Sjmcneill struct tegra_mc_softc {
53d4fd1143Sjmcneill 	device_t		sc_dev;
54d4fd1143Sjmcneill 	bus_space_tag_t		sc_bst;
55d4fd1143Sjmcneill 	bus_space_handle_t	sc_bsh;
5677011a56Sjakllsch 	void			*sc_ih;
57d4fd1143Sjmcneill };
58d4fd1143Sjmcneill 
59d4fd1143Sjmcneill static struct tegra_mc_softc *mc_softc = NULL;
60d4fd1143Sjmcneill 
61d4fd1143Sjmcneill CFATTACH_DECL_NEW(tegra_mc, sizeof(struct tegra_mc_softc),
62d4fd1143Sjmcneill 	tegra_mc_match, tegra_mc_attach, NULL, NULL);
63d4fd1143Sjmcneill 
64a5c784b3Sjmcneill #define MC_READ(sc, reg)		\
65a5c784b3Sjmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
66a5c784b3Sjmcneill #define MC_WRITE(sc, reg, val)		\
67a5c784b3Sjmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
68a5c784b3Sjmcneill #define MC_SET_CLEAR(sc, reg, set, clr)	\
69a5c784b3Sjmcneill     tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (set), (clr))
7077011a56Sjakllsch 
71*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
72*6e54367aSthorpej 	{ .compat = "nvidia,tegra124-mc" },
73*6e54367aSthorpej 	DEVICE_COMPAT_EOL
74*6e54367aSthorpej };
75*6e54367aSthorpej 
76d4fd1143Sjmcneill static int
tegra_mc_match(device_t parent,cfdata_t cf,void * aux)77d4fd1143Sjmcneill tegra_mc_match(device_t parent, cfdata_t cf, void *aux)
78d4fd1143Sjmcneill {
79d59db8d0Sjmcneill 	struct fdt_attach_args * const faa = aux;
80d59db8d0Sjmcneill 
81*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
82d4fd1143Sjmcneill }
83d4fd1143Sjmcneill 
84d4fd1143Sjmcneill static void
tegra_mc_attach(device_t parent,device_t self,void * aux)85d4fd1143Sjmcneill tegra_mc_attach(device_t parent, device_t self, void *aux)
86d4fd1143Sjmcneill {
87d4fd1143Sjmcneill 	struct tegra_mc_softc * const sc = device_private(self);
88d59db8d0Sjmcneill 	struct fdt_attach_args * const faa = aux;
89d59db8d0Sjmcneill 	char intrstr[128];
90d59db8d0Sjmcneill 	bus_addr_t addr;
91d59db8d0Sjmcneill 	bus_size_t size;
92d59db8d0Sjmcneill 	int error;
93d59db8d0Sjmcneill 
94d59db8d0Sjmcneill 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
95d59db8d0Sjmcneill 		aprint_error(": couldn't get registers\n");
96d59db8d0Sjmcneill 		return;
97d59db8d0Sjmcneill 	}
98d4fd1143Sjmcneill 
99d4fd1143Sjmcneill 	sc->sc_dev = self;
100d59db8d0Sjmcneill 	sc->sc_bst = faa->faa_bst;
101d59db8d0Sjmcneill 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
102d59db8d0Sjmcneill 	if (error) {
1032e65b46dSskrll 		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr, error);
104d59db8d0Sjmcneill 		return;
105d59db8d0Sjmcneill 	}
106d4fd1143Sjmcneill 
107d4fd1143Sjmcneill 	KASSERT(mc_softc == NULL);
108d4fd1143Sjmcneill 	mc_softc = sc;
109d4fd1143Sjmcneill 
110d4fd1143Sjmcneill 	aprint_naive("\n");
111d4fd1143Sjmcneill 	aprint_normal(": MC\n");
11277011a56Sjakllsch 
113d59db8d0Sjmcneill 	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
114d59db8d0Sjmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
11577011a56Sjakllsch 		return;
11677011a56Sjakllsch 	}
117d59db8d0Sjmcneill 
1183f513eddSjmcneill 	sc->sc_ih = fdtbus_intr_establish_xname(faa->faa_phandle, 0, IPL_VM,
1193f513eddSjmcneill 	    FDT_INTR_MPSAFE, tegra_mc_intr, sc, device_xname(self));
120d59db8d0Sjmcneill 	if (sc->sc_ih == NULL) {
121d59db8d0Sjmcneill 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
122d59db8d0Sjmcneill 		    intrstr);
123d59db8d0Sjmcneill 		return;
124d59db8d0Sjmcneill 	}
125d59db8d0Sjmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
12677011a56Sjakllsch 
127a5c784b3Sjmcneill 	MC_WRITE(sc, MC_INTSTATUS_REG, MC_INT__ALL);
128a5c784b3Sjmcneill 	MC_WRITE(sc, MC_INTMASK_REG, MC_INT__ALL);
12977011a56Sjakllsch }
13077011a56Sjakllsch 
131b36f0941Sjmcneill static int
tegra_mc_intr(void * v)13277011a56Sjakllsch tegra_mc_intr(void *v)
13377011a56Sjakllsch {
13477011a56Sjakllsch 	struct tegra_mc_softc * const sc = v;
13577011a56Sjakllsch 
136a5c784b3Sjmcneill 	const uint32_t status = MC_READ(sc, MC_INTSTATUS_REG);
13777011a56Sjakllsch 
13877011a56Sjakllsch 	if (status == 0) {
13977011a56Sjakllsch 		return 0;
14077011a56Sjakllsch 	}
14177011a56Sjakllsch 
142a5c784b3Sjmcneill 	const uint32_t err_status = MC_READ(sc, MC_ERR_STATUS_REG);
143a5c784b3Sjmcneill 	const uint32_t err_adr = MC_READ(sc, MC_ERR_ADR_REG);
14477011a56Sjakllsch 
14577011a56Sjakllsch 	device_printf(sc->sc_dev, "intrstatus %#x err %#x adr %#x\n",
14677011a56Sjakllsch 	    status, err_status, err_adr);
14777011a56Sjakllsch 
148a5c784b3Sjmcneill 	MC_WRITE(sc, MC_INTSTATUS_REG, status);
14977011a56Sjakllsch 
15077011a56Sjakllsch 	return status;
151d4fd1143Sjmcneill }
152