Path: blob/main/sys/arm/nvidia/tegra124/tegra124_pmc.c
39507 views
/*-1* Copyright (c) 2016 Michal Meloun <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/param.h>27#include <sys/bus.h>28#include <sys/kernel.h>29#include <sys/lock.h>30#include <sys/malloc.h>31#include <sys/module.h>32#include <sys/mutex.h>33#include <sys/rman.h>34#include <sys/systm.h>3536#include <machine/bus.h>3738#include <dev/clk/clk.h>39#include <dev/hwreset/hwreset.h>40#include <dev/ofw/ofw_bus.h>41#include <dev/ofw/ofw_bus_subr.h>4243#include <arm/nvidia/tegra_pmc.h>4445#define PMC_CNTRL 0x00046#define PMC_CNTRL_CPUPWRGOOD_SEL_MASK (0x3 << 20)47#define PMC_CNTRL_CPUPWRGOOD_SEL_SHIFT 2048#define PMC_CNTRL_CPUPWRGOOD_EN (1 << 19)49#define PMC_CNTRL_FUSE_OVERRIDE (1 << 18)50#define PMC_CNTRL_INTR_POLARITY (1 << 17)51#define PMC_CNTRL_CPU_PWRREQ_OE (1 << 16)52#define PMC_CNTRL_CPU_PWRREQ_POLARITY (1 << 15)53#define PMC_CNTRL_SIDE_EFFECT_LP0 (1 << 14)54#define PMC_CNTRL_AOINIT (1 << 13)55#define PMC_CNTRL_PWRGATE_DIS (1 << 12)56#define PMC_CNTRL_SYSCLK_OE (1 << 11)57#define PMC_CNTRL_SYSCLK_POLARITY (1 << 10)58#define PMC_CNTRL_PWRREQ_OE (1 << 9)59#define PMC_CNTRL_PWRREQ_POLARITY (1 << 8)60#define PMC_CNTRL_BLINK_EN (1 << 7)61#define PMC_CNTRL_GLITCHDET_DIS (1 << 6)62#define PMC_CNTRL_LATCHWAKE_EN (1 << 5)63#define PMC_CNTRL_MAIN_RST (1 << 4)64#define PMC_CNTRL_KBC_RST (1 << 3)65#define PMC_CNTRL_RTC_RST (1 << 2)66#define PMC_CNTRL_RTC_CLK_DIS (1 << 1)67#define PMC_CNTRL_KBC_CLK_DIS (1 << 0)6869#define PMC_DPD_SAMPLE 0x0207071#define PMC_CLAMP_STATUS 0x02C72#define PMC_CLAMP_STATUS_PARTID(x) (1 << ((x) & 0x1F))7374#define PMC_PWRGATE_TOGGLE 0x03075#define PMC_PWRGATE_TOGGLE_START (1 << 8)76#define PMC_PWRGATE_TOGGLE_PARTID(x) (((x) & 0x1F) << 0)7778#define PMC_REMOVE_CLAMPING_CMD 0x03479#define PMC_REMOVE_CLAMPING_CMD_PARTID(x) (1 << ((x) & 0x1F))8081#define PMC_PWRGATE_STATUS 0x03882#define PMC_PWRGATE_STATUS_PARTID(x) (1 << ((x) & 0x1F))8384#define PMC_SCRATCH0 0x05085#define PMC_SCRATCH0_MODE_RECOVERY (1 << 31)86#define PMC_SCRATCH0_MODE_BOOTLOADER (1 << 30)87#define PMC_SCRATCH0_MODE_RCM (1 << 1)88#define PMC_SCRATCH0_MODE_MASK (PMC_SCRATCH0_MODE_RECOVERY | \89PMC_SCRATCH0_MODE_BOOTLOADER | \90PMC_SCRATCH0_MODE_RCM)9192#define PMC_CPUPWRGOOD_TIMER 0x0c893#define PMC_CPUPWROFF_TIMER 0x0cc9495#define PMC_SCRATCH41 0x1409697#define PMC_SENSOR_CTRL 0x1b098#define PMC_SENSOR_CTRL_BLOCK_SCRATCH_WRITE (1 << 2)99#define PMC_SENSOR_CTRL_ENABLE_RST (1 << 1)100#define PMC_SENSOR_CTRL_ENABLE_PG (1 << 0)101102#define PMC_IO_DPD_REQ 0x1b8103#define PMC_IO_DPD_REQ_CODE_IDLE (0 << 30)104#define PMC_IO_DPD_REQ_CODE_OFF (1 << 30)105#define PMC_IO_DPD_REQ_CODE_ON (2 << 30)106#define PMC_IO_DPD_REQ_CODE_MASK (3 << 30)107108#define PMC_IO_DPD_STATUS 0x1bc109#define PMC_IO_DPD_STATUS_HDMI (1 << 28)110#define PMC_IO_DPD2_REQ 0x1c0111#define PMC_IO_DPD2_STATUS 0x1c4112#define PMC_IO_DPD2_STATUS_HV (1 << 6)113#define PMC_SEL_DPD_TIM 0x1c8114115#define PMC_SCRATCH54 0x258116#define PMC_SCRATCH54_DATA_SHIFT 8117#define PMC_SCRATCH54_ADDR_SHIFT 0118119#define PMC_SCRATCH55 0x25c120#define PMC_SCRATCH55_RST_ENABLE (1 << 31)121#define PMC_SCRATCH55_CNTRL_TYPE (1 << 30)122#define PMC_SCRATCH55_CNTRL_ID_SHIFT 27123#define PMC_SCRATCH55_CNTRL_ID_MASK 0x07124#define PMC_SCRATCH55_PINMUX_SHIFT 24125#define PMC_SCRATCH55_PINMUX_MASK 0x07126#define PMC_SCRATCH55_CHECKSUM_SHIFT 16127#define PMC_SCRATCH55_CHECKSUM_MASK 0xFF128#define PMC_SCRATCH55_16BITOP (1 << 15)129#define PMC_SCRATCH55_I2CSLV1_SHIFT 0130#define PMC_SCRATCH55_I2CSLV1_MASK 0x7F131132#define PMC_GPU_RG_CNTRL 0x2d4133134#define WR4(_sc, _r, _v) bus_write_4((_sc)->mem_res, (_r), (_v))135#define RD4(_sc, _r) bus_read_4((_sc)->mem_res, (_r))136137#define PMC_LOCK(_sc) mtx_lock(&(_sc)->mtx)138#define PMC_UNLOCK(_sc) mtx_unlock(&(_sc)->mtx)139#define PMC_LOCK_INIT(_sc) mtx_init(&(_sc)->mtx, \140device_get_nameunit(_sc->dev), "tegra124_pmc", MTX_DEF)141#define PMC_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx);142#define PMC_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_OWNED);143#define PMC_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_NOTOWNED);144145struct tegra124_pmc_softc {146device_t dev;147struct resource *mem_res;148clk_t clk;149struct mtx mtx;150151uint32_t rate;152enum tegra_suspend_mode suspend_mode;153uint32_t cpu_good_time;154uint32_t cpu_off_time;155uint32_t core_osc_time;156uint32_t core_pmu_time;157uint32_t core_off_time;158int corereq_high;159int sysclkreq_high;160int combined_req;161int cpu_pwr_good_en;162uint32_t lp0_vec_phys;163uint32_t lp0_vec_size;164};165166static struct ofw_compat_data compat_data[] = {167{"nvidia,tegra124-pmc", 1},168{NULL, 0},169};170171static struct tegra124_pmc_softc *pmc_sc;172173static inline struct tegra124_pmc_softc *174tegra124_pmc_get_sc(void)175{176if (pmc_sc == NULL)177panic("To early call to Tegra PMC driver.\n");178return (pmc_sc);179}180181static int182tegra124_pmc_set_powergate(struct tegra124_pmc_softc *sc,183enum tegra_powergate_id id, int ena)184{185uint32_t reg;186int i;187188PMC_LOCK(sc);189190reg = RD4(sc, PMC_PWRGATE_STATUS) & PMC_PWRGATE_STATUS_PARTID(id);191if (((reg != 0) && ena) || ((reg == 0) && !ena)) {192PMC_UNLOCK(sc);193return (0);194}195196for (i = 100; i > 0; i--) {197reg = RD4(sc, PMC_PWRGATE_TOGGLE);198if ((reg & PMC_PWRGATE_TOGGLE_START) == 0)199break;200DELAY(1);201}202if (i <= 0)203device_printf(sc->dev,204"Timeout when waiting for TOGGLE_START\n");205206WR4(sc, PMC_PWRGATE_TOGGLE,207PMC_PWRGATE_TOGGLE_START | PMC_PWRGATE_TOGGLE_PARTID(id));208209for (i = 100; i > 0; i--) {210reg = RD4(sc, PMC_PWRGATE_TOGGLE);211if ((reg & PMC_PWRGATE_TOGGLE_START) == 0)212break;213DELAY(1);214}215if (i <= 0)216device_printf(sc->dev,217"Timeout when waiting for TOGGLE_START\n");218PMC_UNLOCK(sc);219return (0);220}221222int223tegra_powergate_remove_clamping(enum tegra_powergate_id id)224{225struct tegra124_pmc_softc *sc;226uint32_t reg;227enum tegra_powergate_id swid;228int i;229230sc = tegra124_pmc_get_sc();231232if (id == TEGRA_POWERGATE_3D) {233WR4(sc, PMC_GPU_RG_CNTRL, 0);234return (0);235}236237reg = RD4(sc, PMC_PWRGATE_STATUS);238if ((reg & PMC_PWRGATE_STATUS_PARTID(id)) == 0)239panic("Attempt to remove clamping for unpowered partition.\n");240241if (id == TEGRA_POWERGATE_PCX)242swid = TEGRA_POWERGATE_VDE;243else if (id == TEGRA_POWERGATE_VDE)244swid = TEGRA_POWERGATE_PCX;245else246swid = id;247WR4(sc, PMC_REMOVE_CLAMPING_CMD, PMC_REMOVE_CLAMPING_CMD_PARTID(swid));248249for (i = 100; i > 0; i--) {250reg = RD4(sc, PMC_REMOVE_CLAMPING_CMD);251if ((reg & PMC_REMOVE_CLAMPING_CMD_PARTID(swid)) == 0)252break;253DELAY(1);254}255if (i <= 0)256device_printf(sc->dev, "Timeout when remove clamping\n");257258reg = RD4(sc, PMC_CLAMP_STATUS);259if ((reg & PMC_CLAMP_STATUS_PARTID(id)) != 0)260panic("Cannot remove clamping\n");261262return (0);263}264265int266tegra_powergate_is_powered(enum tegra_powergate_id id)267{268struct tegra124_pmc_softc *sc;269uint32_t reg;270271sc = tegra124_pmc_get_sc();272273reg = RD4(sc, PMC_PWRGATE_STATUS);274return ((reg & PMC_PWRGATE_STATUS_PARTID(id)) ? 1 : 0);275}276277int278tegra_powergate_power_on(enum tegra_powergate_id id)279{280struct tegra124_pmc_softc *sc;281int rv, i;282283sc = tegra124_pmc_get_sc();284285rv = tegra124_pmc_set_powergate(sc, id, 1);286if (rv != 0) {287device_printf(sc->dev, "Cannot set powergate: %d\n", id);288return (rv);289}290291for (i = 100; i > 0; i--) {292if (tegra_powergate_is_powered(id))293break;294DELAY(1);295}296if (i <= 0)297device_printf(sc->dev, "Timeout when waiting on power up\n");298299return (rv);300}301302int303tegra_powergate_power_off(enum tegra_powergate_id id)304{305struct tegra124_pmc_softc *sc;306int rv, i;307308sc = tegra124_pmc_get_sc();309310rv = tegra124_pmc_set_powergate(sc, id, 0);311if (rv != 0) {312device_printf(sc->dev, "Cannot set powergate: %d\n", id);313return (rv);314}315for (i = 100; i > 0; i--) {316if (!tegra_powergate_is_powered(id))317break;318DELAY(1);319}320if (i <= 0)321device_printf(sc->dev, "Timeout when waiting on power off\n");322323return (rv);324}325326int327tegra_powergate_sequence_power_up(enum tegra_powergate_id id, clk_t clk,328hwreset_t rst)329{330struct tegra124_pmc_softc *sc;331int rv;332333sc = tegra124_pmc_get_sc();334335rv = hwreset_assert(rst);336if (rv != 0) {337device_printf(sc->dev, "Cannot assert reset\n");338return (rv);339}340341rv = clk_stop(clk);342if (rv != 0) {343device_printf(sc->dev, "Cannot stop clock\n");344goto clk_fail;345}346347rv = tegra_powergate_power_on(id);348if (rv != 0) {349device_printf(sc->dev, "Cannot power on powergate\n");350goto clk_fail;351}352353rv = clk_enable(clk);354if (rv != 0) {355device_printf(sc->dev, "Cannot enable clock\n");356goto clk_fail;357}358DELAY(20);359360rv = tegra_powergate_remove_clamping(id);361if (rv != 0) {362device_printf(sc->dev, "Cannot remove clamping\n");363goto fail;364}365rv = hwreset_deassert(rst);366if (rv != 0) {367device_printf(sc->dev, "Cannot unreset reset\n");368goto fail;369}370return 0;371372fail:373clk_disable(clk);374clk_fail:375hwreset_assert(rst);376tegra_powergate_power_off(id);377return (rv);378}379380static int381tegra124_pmc_parse_fdt(struct tegra124_pmc_softc *sc, phandle_t node)382{383int rv;384uint32_t tmp;385uint32_t tmparr[2];386387rv = OF_getencprop(node, "nvidia,suspend-mode", &tmp, sizeof(tmp));388if (rv > 0) {389switch (tmp) {390case 0:391sc->suspend_mode = TEGRA_SUSPEND_LP0;392break;393394case 1:395sc->suspend_mode = TEGRA_SUSPEND_LP1;396break;397398case 2:399sc->suspend_mode = TEGRA_SUSPEND_LP2;400break;401402default:403sc->suspend_mode = TEGRA_SUSPEND_NONE;404break;405}406}407408rv = OF_getencprop(node, "nvidia,cpu-pwr-good-time", &tmp, sizeof(tmp));409if (rv > 0) {410sc->cpu_good_time = tmp;411sc->suspend_mode = TEGRA_SUSPEND_NONE;412}413414rv = OF_getencprop(node, "nvidia,cpu-pwr-off-time", &tmp, sizeof(tmp));415if (rv > 0) {416sc->cpu_off_time = tmp;417sc->suspend_mode = TEGRA_SUSPEND_NONE;418}419420rv = OF_getencprop(node, "nvidia,core-pwr-good-time", tmparr,421sizeof(tmparr));422if (rv == sizeof(tmparr)) {423sc->core_osc_time = tmparr[0];424sc->core_pmu_time = tmparr[1];425sc->suspend_mode = TEGRA_SUSPEND_NONE;426}427428rv = OF_getencprop(node, "nvidia,core-pwr-off-time", &tmp, sizeof(tmp));429if (rv > 0) {430sc->core_off_time = tmp;431sc->suspend_mode = TEGRA_SUSPEND_NONE;432}433434sc->corereq_high =435OF_hasprop(node, "nvidia,core-power-req-active-high");436sc->sysclkreq_high =437OF_hasprop(node, "nvidia,sys-clock-req-active-high");438sc->combined_req =439OF_hasprop(node, "nvidia,combined-power-req");440sc->cpu_pwr_good_en =441OF_hasprop(node, "nvidia,cpu-pwr-good-en");442443rv = OF_getencprop(node, "nvidia,lp0-vec", tmparr, sizeof(tmparr));444if (rv == sizeof(tmparr)) {445sc->lp0_vec_phys = tmparr[0];446sc->core_pmu_time = tmparr[1];447sc->lp0_vec_size = TEGRA_SUSPEND_NONE;448if (sc->suspend_mode == TEGRA_SUSPEND_LP0)449sc->suspend_mode = TEGRA_SUSPEND_LP1;450}451return 0;452}453454static int455tegra124_pmc_probe(device_t dev)456{457458if (!ofw_bus_status_okay(dev))459return (ENXIO);460461if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)462return (ENXIO);463464device_set_desc(dev, "Tegra PMC");465return (BUS_PROBE_DEFAULT);466}467468static int469tegra124_pmc_detach(device_t dev)470{471472/* This device is always present. */473return (EBUSY);474}475476static int477tegra124_pmc_attach(device_t dev)478{479struct tegra124_pmc_softc *sc;480int rid, rv;481uint32_t reg;482phandle_t node;483484sc = device_get_softc(dev);485sc->dev = dev;486node = ofw_bus_get_node(dev);487488rv = tegra124_pmc_parse_fdt(sc, node);489if (rv != 0) {490device_printf(sc->dev, "Cannot parse FDT data\n");491return (rv);492}493494rv = clk_get_by_ofw_name(sc->dev, 0, "pclk", &sc->clk);495if (rv != 0) {496device_printf(sc->dev, "Cannot get \"pclk\" clock\n");497return (ENXIO);498}499500rid = 0;501sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,502RF_ACTIVE);503if (sc->mem_res == NULL) {504device_printf(dev, "Cannot allocate memory resources\n");505return (ENXIO);506}507508PMC_LOCK_INIT(sc);509510/* Enable CPU power request. */511reg = RD4(sc, PMC_CNTRL);512reg |= PMC_CNTRL_CPU_PWRREQ_OE;513WR4(sc, PMC_CNTRL, reg);514515/* Set sysclk output polarity */516reg = RD4(sc, PMC_CNTRL);517if (sc->sysclkreq_high)518reg &= ~PMC_CNTRL_SYSCLK_POLARITY;519else520reg |= PMC_CNTRL_SYSCLK_POLARITY;521WR4(sc, PMC_CNTRL, reg);522523/* Enable sysclk request. */524reg = RD4(sc, PMC_CNTRL);525reg |= PMC_CNTRL_SYSCLK_OE;526WR4(sc, PMC_CNTRL, reg);527528/*529* Remove HDMI from deep power down mode.530* XXX mote this to HDMI driver531*/532reg = RD4(sc, PMC_IO_DPD_STATUS);533reg &= ~ PMC_IO_DPD_STATUS_HDMI;534WR4(sc, PMC_IO_DPD_STATUS, reg);535536reg = RD4(sc, PMC_IO_DPD2_STATUS);537reg &= ~ PMC_IO_DPD2_STATUS_HV;538WR4(sc, PMC_IO_DPD2_STATUS, reg);539540if (pmc_sc != NULL)541panic("tegra124_pmc: double driver attach");542pmc_sc = sc;543return (0);544}545546static device_method_t tegra124_pmc_methods[] = {547/* Device interface */548DEVMETHOD(device_probe, tegra124_pmc_probe),549DEVMETHOD(device_attach, tegra124_pmc_attach),550DEVMETHOD(device_detach, tegra124_pmc_detach),551552DEVMETHOD_END553};554555static DEFINE_CLASS_0(pmc, tegra124_pmc_driver, tegra124_pmc_methods,556sizeof(struct tegra124_pmc_softc));557EARLY_DRIVER_MODULE(tegra124_pmc, simplebus, tegra124_pmc_driver, NULL, NULL,55870);559560561