xref: /netbsd-src/sys/arch/arm/nvidia/tegra_pmc.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: tegra_pmc.c,v 1.16 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 <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: tegra_pmc.c,v 1.16 2021/01/27 03:10:19 thorpej Exp $");
31d4fd1143Sjmcneill 
32d4fd1143Sjmcneill #include <sys/param.h>
33d4fd1143Sjmcneill #include <sys/bus.h>
34d4fd1143Sjmcneill #include <sys/device.h>
35d4fd1143Sjmcneill #include <sys/intr.h>
36d4fd1143Sjmcneill #include <sys/systm.h>
37d4fd1143Sjmcneill #include <sys/kernel.h>
38d4fd1143Sjmcneill 
39d4fd1143Sjmcneill #include <arm/nvidia/tegra_reg.h>
40d4fd1143Sjmcneill #include <arm/nvidia/tegra_pmcreg.h>
41d4fd1143Sjmcneill #include <arm/nvidia/tegra_var.h>
42d4fd1143Sjmcneill 
43d59db8d0Sjmcneill #include <dev/fdt/fdtvar.h>
44d59db8d0Sjmcneill 
45d4fd1143Sjmcneill static int	tegra_pmc_match(device_t, cfdata_t, void *);
46d4fd1143Sjmcneill static void	tegra_pmc_attach(device_t, device_t, void *);
47d4fd1143Sjmcneill 
48d4fd1143Sjmcneill struct tegra_pmc_softc {
49d4fd1143Sjmcneill 	device_t		sc_dev;
50d4fd1143Sjmcneill 	bus_space_tag_t		sc_bst;
51d4fd1143Sjmcneill 	bus_space_handle_t	sc_bsh;
52d4fd1143Sjmcneill };
53d4fd1143Sjmcneill 
54d4fd1143Sjmcneill static struct tegra_pmc_softc *pmc_softc = NULL;
55d4fd1143Sjmcneill 
56d4fd1143Sjmcneill CFATTACH_DECL_NEW(tegra_pmc, sizeof(struct tegra_pmc_softc),
57d4fd1143Sjmcneill 	tegra_pmc_match, tegra_pmc_attach, NULL, NULL);
58d4fd1143Sjmcneill 
59*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
60*6e54367aSthorpej 	{ .compat = "nvidia,tegra210-pmc" },
61*6e54367aSthorpej 	{ .compat = "nvidia,tegra124-pmc" },
62*6e54367aSthorpej 	DEVICE_COMPAT_EOL
63*6e54367aSthorpej };
64*6e54367aSthorpej 
65d4fd1143Sjmcneill static int
tegra_pmc_match(device_t parent,cfdata_t cf,void * aux)66d4fd1143Sjmcneill tegra_pmc_match(device_t parent, cfdata_t cf, void *aux)
67d4fd1143Sjmcneill {
68d59db8d0Sjmcneill 	struct fdt_attach_args * const faa = aux;
69d59db8d0Sjmcneill 
70*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
71d4fd1143Sjmcneill }
72d4fd1143Sjmcneill 
73d4fd1143Sjmcneill static void
tegra_pmc_attach(device_t parent,device_t self,void * aux)74d4fd1143Sjmcneill tegra_pmc_attach(device_t parent, device_t self, void *aux)
75d4fd1143Sjmcneill {
76d4fd1143Sjmcneill 	struct tegra_pmc_softc * const sc = device_private(self);
77d59db8d0Sjmcneill 	struct fdt_attach_args * const faa = aux;
78d59db8d0Sjmcneill 	bus_addr_t addr;
79d59db8d0Sjmcneill 	bus_size_t size;
80d59db8d0Sjmcneill 	int error;
81d59db8d0Sjmcneill 
82d59db8d0Sjmcneill 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
83d59db8d0Sjmcneill 		aprint_error(": couldn't get registers\n");
84d59db8d0Sjmcneill 		return;
85d59db8d0Sjmcneill 	}
86d4fd1143Sjmcneill 
87d4fd1143Sjmcneill 	sc->sc_dev = self;
88d59db8d0Sjmcneill 	sc->sc_bst = faa->faa_bst;
89d59db8d0Sjmcneill 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
90d59db8d0Sjmcneill 	if (error) {
912e65b46dSskrll 		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr, error);
92d59db8d0Sjmcneill 		return;
93d59db8d0Sjmcneill 	}
94d4fd1143Sjmcneill 
95d4fd1143Sjmcneill 	KASSERT(pmc_softc == NULL);
96d4fd1143Sjmcneill 	pmc_softc = sc;
97d4fd1143Sjmcneill 
98d4fd1143Sjmcneill 	aprint_naive("\n");
99d4fd1143Sjmcneill 	aprint_normal(": PMC\n");
100d4fd1143Sjmcneill }
101d4fd1143Sjmcneill 
10219d924bcSjmcneill static void
tegra_pmc_get_bs(bus_space_tag_t * pbst,bus_space_handle_t * pbsh)10319d924bcSjmcneill tegra_pmc_get_bs(bus_space_tag_t *pbst, bus_space_handle_t *pbsh)
10419d924bcSjmcneill {
10519d924bcSjmcneill 	if (pmc_softc) {
10619d924bcSjmcneill 		*pbst = pmc_softc->sc_bst;
10719d924bcSjmcneill 		*pbsh = pmc_softc->sc_bsh;
10819d924bcSjmcneill 	} else {
109fe33aa27Sryo 		extern struct bus_space arm_generic_bs_tag;
110fe33aa27Sryo 
111fe33aa27Sryo 		*pbst = &arm_generic_bs_tag;
112fe33aa27Sryo 
11319d924bcSjmcneill 		bus_space_subregion(*pbst, tegra_apb_bsh,
11419d924bcSjmcneill 		    TEGRA_PMC_OFFSET, TEGRA_PMC_SIZE, pbsh);
11519d924bcSjmcneill 	}
11619d924bcSjmcneill }
11719d924bcSjmcneill 
118d4fd1143Sjmcneill void
tegra_pmc_reset(void)119d4fd1143Sjmcneill tegra_pmc_reset(void)
120d4fd1143Sjmcneill {
121d4fd1143Sjmcneill 	bus_space_tag_t bst;
122d4fd1143Sjmcneill 	bus_space_handle_t bsh;
123d4fd1143Sjmcneill 	uint32_t cntrl;
124d4fd1143Sjmcneill 
12519d924bcSjmcneill 	tegra_pmc_get_bs(&bst, &bsh);
126d4fd1143Sjmcneill 
127d4fd1143Sjmcneill 	cntrl = bus_space_read_4(bst, bsh, PMC_CNTRL_0_REG);
128d4fd1143Sjmcneill 	cntrl |= PMC_CNTRL_0_MAIN_RST;
129d4fd1143Sjmcneill 	bus_space_write_4(bst, bsh, PMC_CNTRL_0_REG, cntrl);
130d4fd1143Sjmcneill 
131d4fd1143Sjmcneill 	for (;;) {
132d4fd1143Sjmcneill 		__asm("wfi");
133d4fd1143Sjmcneill 	}
134d4fd1143Sjmcneill }
13519d924bcSjmcneill 
13619d924bcSjmcneill void
tegra_pmc_power(u_int partid,bool enable)13719d924bcSjmcneill tegra_pmc_power(u_int partid, bool enable)
13819d924bcSjmcneill {
13919d924bcSjmcneill 	bus_space_tag_t bst;
14019d924bcSjmcneill 	bus_space_handle_t bsh;
14138339827Sjmcneill 	uint32_t status, toggle;
14219d924bcSjmcneill 	bool state;
14338339827Sjmcneill 	int retry = 10000;
14419d924bcSjmcneill 
14519d924bcSjmcneill 	tegra_pmc_get_bs(&bst, &bsh);
14619d924bcSjmcneill 
14719d924bcSjmcneill 	status = bus_space_read_4(bst, bsh, PMC_PWRGATE_STATUS_0_REG);
14819d924bcSjmcneill 	state = !!(status & __BIT(partid));
14919d924bcSjmcneill 	if (state == enable)
15019d924bcSjmcneill 		return;
15119d924bcSjmcneill 
15238339827Sjmcneill 	while (--retry > 0) {
15338339827Sjmcneill 		toggle = bus_space_read_4(bst, bsh, PMC_PWRGATE_TOGGLE_0_REG);
15438339827Sjmcneill 		if ((toggle & PMC_PWRGATE_TOGGLE_0_START) == 0)
15538339827Sjmcneill 			break;
15638339827Sjmcneill 		delay(1);
15738339827Sjmcneill 	}
15838339827Sjmcneill 	if (retry == 0) {
15938339827Sjmcneill 		printf("ERROR: Couldn't enable PMC partition %#x\n", partid);
16038339827Sjmcneill 		return;
16138339827Sjmcneill 	}
16238339827Sjmcneill 
16319d924bcSjmcneill 	bus_space_write_4(bst, bsh, PMC_PWRGATE_TOGGLE_0_REG,
16419d924bcSjmcneill 	    __SHIFTIN(partid, PMC_PWRGATE_TOGGLE_0_PARTID) |
16519d924bcSjmcneill 	    PMC_PWRGATE_TOGGLE_0_START);
16619d924bcSjmcneill }
167b43705e1Sjmcneill 
168b43705e1Sjmcneill void
tegra_pmc_remove_clamping(u_int partid)169b43705e1Sjmcneill tegra_pmc_remove_clamping(u_int partid)
170b43705e1Sjmcneill {
171b43705e1Sjmcneill 	bus_space_tag_t bst;
172b43705e1Sjmcneill 	bus_space_handle_t bsh;
173b43705e1Sjmcneill 
174b43705e1Sjmcneill 	tegra_pmc_get_bs(&bst, &bsh);
175b43705e1Sjmcneill 
176ea56b024Sjmcneill 	if (partid == PMC_PARTID_TD) {
17789c48b02Sjmcneill 		/*
178ea56b024Sjmcneill 		 * On Tegra124 and later, the GPU power clamping is
179ea56b024Sjmcneill 		 * controlled by a separate register
18089c48b02Sjmcneill 		 */
18189c48b02Sjmcneill 		bus_space_write_4(bst, bsh, PMC_GPU_RG_CNTRL_REG, 0);
18289c48b02Sjmcneill 		return;
18389c48b02Sjmcneill 	}
18489c48b02Sjmcneill 
185b43705e1Sjmcneill 	bus_space_write_4(bst, bsh, PMC_REMOVE_CLAMPING_CMD_0_REG,
186b43705e1Sjmcneill 	    __BIT(partid));
187b43705e1Sjmcneill }
188b1b62c57Sjmcneill 
189b1b62c57Sjmcneill void
tegra_pmc_hdmi_enable(void)190b1b62c57Sjmcneill tegra_pmc_hdmi_enable(void)
191b1b62c57Sjmcneill {
192b1b62c57Sjmcneill 	bus_space_tag_t bst;
193b1b62c57Sjmcneill 	bus_space_handle_t bsh;
194b1b62c57Sjmcneill 
195b1b62c57Sjmcneill 	tegra_pmc_get_bs(&bst, &bsh);
196b1b62c57Sjmcneill 
197b1b62c57Sjmcneill 	tegra_reg_set_clear(bst, bsh, PMC_IO_DPD_STATUS_REG,
198b1b62c57Sjmcneill 	    0, PMC_IO_DPD_STATUS_HDMI);
199b1b62c57Sjmcneill 	tegra_reg_set_clear(bst, bsh, PMC_IO_DPD2_STATUS_REG,
200b1b62c57Sjmcneill 	    0, PMC_IO_DPD2_STATUS_HV);
201b1b62c57Sjmcneill }
202