2019-05-29 14:17:58 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2015-08-06 10:37:42 +00:00
|
|
|
/*
|
2018-04-09 08:41:44 +00:00
|
|
|
* Copyright (c) 2015, 2017-2018, The Linux Foundation. All rights reserved.
|
2015-08-06 10:37:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/jiffies.h>
|
|
|
|
#include <linux/kernel.h>
|
2015-12-01 16:12:12 +00:00
|
|
|
#include <linux/ktime.h>
|
2015-08-06 10:37:42 +00:00
|
|
|
#include <linux/pm_domain.h>
|
|
|
|
#include <linux/regmap.h>
|
2015-08-06 10:37:45 +00:00
|
|
|
#include <linux/reset-controller.h>
|
2015-08-06 10:37:42 +00:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include "gdsc.h"
|
|
|
|
|
|
|
|
#define PWR_ON_MASK BIT(31)
|
|
|
|
#define EN_REST_WAIT_MASK GENMASK_ULL(23, 20)
|
|
|
|
#define EN_FEW_WAIT_MASK GENMASK_ULL(19, 16)
|
|
|
|
#define CLK_DIS_WAIT_MASK GENMASK_ULL(15, 12)
|
|
|
|
#define SW_OVERRIDE_MASK BIT(2)
|
|
|
|
#define HW_CONTROL_MASK BIT(1)
|
|
|
|
#define SW_COLLAPSE_MASK BIT(0)
|
2016-10-20 09:38:06 +00:00
|
|
|
#define GMEM_CLAMP_IO_MASK BIT(0)
|
2018-04-09 08:41:44 +00:00
|
|
|
#define GMEM_RESET_MASK BIT(4)
|
2015-08-06 10:37:42 +00:00
|
|
|
|
2018-05-01 05:03:33 +00:00
|
|
|
/* CFG_GDSCR */
|
|
|
|
#define GDSC_POWER_UP_COMPLETE BIT(16)
|
|
|
|
#define GDSC_POWER_DOWN_COMPLETE BIT(15)
|
|
|
|
#define CFG_GDSCR_OFFSET 0x4
|
2015-08-06 10:37:42 +00:00
|
|
|
|
|
|
|
/* Wait 2^n CXO cycles between all states. Here, n=2 (4 cycles). */
|
|
|
|
#define EN_REST_WAIT_VAL (0x2 << 20)
|
|
|
|
#define EN_FEW_WAIT_VAL (0x8 << 16)
|
|
|
|
#define CLK_DIS_WAIT_VAL (0x2 << 12)
|
|
|
|
|
2015-08-06 10:37:44 +00:00
|
|
|
#define RETAIN_MEM BIT(14)
|
|
|
|
#define RETAIN_PERIPH BIT(13)
|
|
|
|
|
2018-04-09 08:41:45 +00:00
|
|
|
#define TIMEOUT_US 500
|
2015-08-06 10:37:42 +00:00
|
|
|
|
|
|
|
#define domain_to_gdsc(domain) container_of(domain, struct gdsc, pd)
|
|
|
|
|
2018-05-02 06:58:47 +00:00
|
|
|
enum gdsc_status {
|
|
|
|
GDSC_OFF,
|
|
|
|
GDSC_ON
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Returns 1 if GDSC status is status, 0 if not, and < 0 on error */
|
|
|
|
static int gdsc_check_status(struct gdsc *sc, enum gdsc_status status)
|
2015-08-06 10:37:42 +00:00
|
|
|
{
|
2018-05-01 05:03:33 +00:00
|
|
|
unsigned int reg;
|
2015-08-06 10:37:42 +00:00
|
|
|
u32 val;
|
|
|
|
int ret;
|
|
|
|
|
2018-05-01 05:03:33 +00:00
|
|
|
if (sc->flags & POLL_CFG_GDSCR)
|
|
|
|
reg = sc->gdscr + CFG_GDSCR_OFFSET;
|
2018-05-02 06:58:47 +00:00
|
|
|
else if (sc->gds_hw_ctrl)
|
|
|
|
reg = sc->gds_hw_ctrl;
|
2018-05-01 05:03:33 +00:00
|
|
|
else
|
2018-05-02 06:58:47 +00:00
|
|
|
reg = sc->gdscr;
|
2018-05-01 05:03:33 +00:00
|
|
|
|
2015-12-01 16:12:12 +00:00
|
|
|
ret = regmap_read(sc->regmap, reg, &val);
|
2015-08-06 10:37:42 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-05-01 05:03:33 +00:00
|
|
|
if (sc->flags & POLL_CFG_GDSCR) {
|
2018-05-02 06:58:47 +00:00
|
|
|
switch (status) {
|
|
|
|
case GDSC_ON:
|
2018-05-01 05:03:33 +00:00
|
|
|
return !!(val & GDSC_POWER_UP_COMPLETE);
|
2018-05-02 06:58:47 +00:00
|
|
|
case GDSC_OFF:
|
|
|
|
return !!(val & GDSC_POWER_DOWN_COMPLETE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case GDSC_ON:
|
|
|
|
return !!(val & PWR_ON_MASK);
|
|
|
|
case GDSC_OFF:
|
|
|
|
return !(val & PWR_ON_MASK);
|
2018-05-01 05:03:33 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 06:58:47 +00:00
|
|
|
return -EINVAL;
|
2015-08-06 10:37:42 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 12:28:26 +00:00
|
|
|
static int gdsc_hwctrl(struct gdsc *sc, bool en)
|
|
|
|
{
|
|
|
|
u32 val = en ? HW_CONTROL_MASK : 0;
|
|
|
|
|
|
|
|
return regmap_update_bits(sc->regmap, sc->gdscr, HW_CONTROL_MASK, val);
|
|
|
|
}
|
|
|
|
|
2018-05-02 06:58:47 +00:00
|
|
|
static int gdsc_poll_status(struct gdsc *sc, enum gdsc_status status)
|
2017-01-23 04:26:21 +00:00
|
|
|
{
|
|
|
|
ktime_t start;
|
|
|
|
|
|
|
|
start = ktime_get();
|
|
|
|
do {
|
2018-05-02 06:58:47 +00:00
|
|
|
if (gdsc_check_status(sc, status))
|
2017-01-23 04:26:21 +00:00
|
|
|
return 0;
|
|
|
|
} while (ktime_us_delta(ktime_get(), start) < TIMEOUT_US);
|
|
|
|
|
2018-05-02 06:58:47 +00:00
|
|
|
if (gdsc_check_status(sc, status))
|
2017-01-23 04:26:21 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
2018-05-02 06:58:47 +00:00
|
|
|
static int gdsc_toggle_logic(struct gdsc *sc, enum gdsc_status status)
|
2015-08-06 10:37:42 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2018-05-02 06:58:47 +00:00
|
|
|
u32 val = (status == GDSC_ON) ? 0 : SW_COLLAPSE_MASK;
|
2015-08-06 10:37:42 +00:00
|
|
|
|
|
|
|
ret = regmap_update_bits(sc->regmap, sc->gdscr, SW_COLLAPSE_MASK, val);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-12-01 16:12:13 +00:00
|
|
|
/* If disabling votable gdscs, don't poll on status */
|
2018-05-02 06:58:47 +00:00
|
|
|
if ((sc->flags & VOTABLE) && status == GDSC_OFF) {
|
2015-12-01 16:12:13 +00:00
|
|
|
/*
|
|
|
|
* Add a short delay here to ensure that an enable
|
|
|
|
* right after it was disabled does not put it in an
|
|
|
|
* unknown state
|
|
|
|
*/
|
|
|
|
udelay(TIMEOUT_US);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-01 16:12:12 +00:00
|
|
|
if (sc->gds_hw_ctrl) {
|
|
|
|
/*
|
|
|
|
* The gds hw controller asserts/de-asserts the status bit soon
|
|
|
|
* after it receives a power on/off request from a master.
|
|
|
|
* The controller then takes around 8 xo cycles to start its
|
|
|
|
* internal state machine and update the status bit. During
|
|
|
|
* this time, the status bit does not reflect the true status
|
|
|
|
* of the core.
|
|
|
|
* Add a delay of 1 us between writing to the SW_COLLAPSE bit
|
|
|
|
* and polling the status bit.
|
|
|
|
*/
|
|
|
|
udelay(1);
|
|
|
|
}
|
2015-08-06 10:37:42 +00:00
|
|
|
|
2019-05-04 00:17:36 +00:00
|
|
|
ret = gdsc_poll_status(sc, status);
|
|
|
|
WARN(ret, "%s status stuck at 'o%s'", sc->pd.name, status ? "ff" : "n");
|
|
|
|
return ret;
|
2015-08-06 10:37:42 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 10:37:45 +00:00
|
|
|
static inline int gdsc_deassert_reset(struct gdsc *sc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < sc->reset_count; i++)
|
|
|
|
sc->rcdev->ops->deassert(sc->rcdev, sc->resets[i]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int gdsc_assert_reset(struct gdsc *sc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < sc->reset_count; i++)
|
|
|
|
sc->rcdev->ops->assert(sc->rcdev, sc->resets[i]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-06 10:37:44 +00:00
|
|
|
static inline void gdsc_force_mem_on(struct gdsc *sc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u32 mask = RETAIN_MEM | RETAIN_PERIPH;
|
|
|
|
|
|
|
|
for (i = 0; i < sc->cxc_count; i++)
|
|
|
|
regmap_update_bits(sc->regmap, sc->cxcs[i], mask, mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void gdsc_clear_mem_on(struct gdsc *sc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u32 mask = RETAIN_MEM | RETAIN_PERIPH;
|
|
|
|
|
|
|
|
for (i = 0; i < sc->cxc_count; i++)
|
|
|
|
regmap_update_bits(sc->regmap, sc->cxcs[i], mask, 0);
|
|
|
|
}
|
|
|
|
|
2016-10-20 09:38:06 +00:00
|
|
|
static inline void gdsc_deassert_clamp_io(struct gdsc *sc)
|
|
|
|
{
|
|
|
|
regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
|
|
|
|
GMEM_CLAMP_IO_MASK, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void gdsc_assert_clamp_io(struct gdsc *sc)
|
|
|
|
{
|
|
|
|
regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
|
|
|
|
GMEM_CLAMP_IO_MASK, 1);
|
|
|
|
}
|
|
|
|
|
2018-04-09 08:41:44 +00:00
|
|
|
static inline void gdsc_assert_reset_aon(struct gdsc *sc)
|
|
|
|
{
|
|
|
|
regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
|
|
|
|
GMEM_RESET_MASK, 1);
|
|
|
|
udelay(1);
|
|
|
|
regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
|
|
|
|
GMEM_RESET_MASK, 0);
|
|
|
|
}
|
2015-08-06 10:37:42 +00:00
|
|
|
static int gdsc_enable(struct generic_pm_domain *domain)
|
|
|
|
{
|
|
|
|
struct gdsc *sc = domain_to_gdsc(domain);
|
|
|
|
int ret;
|
|
|
|
|
2015-08-06 10:37:45 +00:00
|
|
|
if (sc->pwrsts == PWRSTS_ON)
|
|
|
|
return gdsc_deassert_reset(sc);
|
|
|
|
|
2018-04-09 08:41:44 +00:00
|
|
|
if (sc->flags & SW_RESET) {
|
|
|
|
gdsc_assert_reset(sc);
|
|
|
|
udelay(1);
|
|
|
|
gdsc_deassert_reset(sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sc->flags & CLAMP_IO) {
|
|
|
|
if (sc->flags & AON_RESET)
|
|
|
|
gdsc_assert_reset_aon(sc);
|
2016-10-20 09:38:06 +00:00
|
|
|
gdsc_deassert_clamp_io(sc);
|
2018-04-09 08:41:44 +00:00
|
|
|
}
|
2016-10-20 09:38:06 +00:00
|
|
|
|
2018-05-02 06:58:47 +00:00
|
|
|
ret = gdsc_toggle_logic(sc, GDSC_ON);
|
2015-08-06 10:37:42 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-08-06 10:37:44 +00:00
|
|
|
|
|
|
|
if (sc->pwrsts & PWRSTS_OFF)
|
|
|
|
gdsc_force_mem_on(sc);
|
|
|
|
|
2015-08-06 10:37:42 +00:00
|
|
|
/*
|
|
|
|
* If clocks to this power domain were already on, they will take an
|
|
|
|
* additional 4 clock cycles to re-enable after the power domain is
|
|
|
|
* enabled. Delay to account for this. A delay is also needed to ensure
|
|
|
|
* clocks are not enabled within 400ns of enabling power to the
|
|
|
|
* memories.
|
|
|
|
*/
|
|
|
|
udelay(1);
|
|
|
|
|
2016-11-18 12:28:26 +00:00
|
|
|
/* Turn on HW trigger mode if supported */
|
2017-01-23 04:26:21 +00:00
|
|
|
if (sc->flags & HW_CTRL) {
|
|
|
|
ret = gdsc_hwctrl(sc, true);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
/*
|
|
|
|
* Wait for the GDSC to go through a power down and
|
|
|
|
* up cycle. In case a firmware ends up polling status
|
|
|
|
* bits for the gdsc, it might read an 'on' status before
|
|
|
|
* the GDSC can finish the power cycle.
|
|
|
|
* We wait 1us before returning to ensure the firmware
|
|
|
|
* can't immediately poll the status bits.
|
|
|
|
*/
|
|
|
|
udelay(1);
|
|
|
|
}
|
2016-11-18 12:28:26 +00:00
|
|
|
|
2015-08-06 10:37:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gdsc_disable(struct generic_pm_domain *domain)
|
|
|
|
{
|
|
|
|
struct gdsc *sc = domain_to_gdsc(domain);
|
2016-10-20 09:38:06 +00:00
|
|
|
int ret;
|
2015-08-06 10:37:42 +00:00
|
|
|
|
2015-08-06 10:37:45 +00:00
|
|
|
if (sc->pwrsts == PWRSTS_ON)
|
|
|
|
return gdsc_assert_reset(sc);
|
|
|
|
|
2016-11-18 12:28:26 +00:00
|
|
|
/* Turn off HW trigger mode if supported */
|
|
|
|
if (sc->flags & HW_CTRL) {
|
|
|
|
ret = gdsc_hwctrl(sc, false);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2017-01-23 04:26:21 +00:00
|
|
|
/*
|
|
|
|
* Wait for the GDSC to go through a power down and
|
|
|
|
* up cycle. In case we end up polling status
|
|
|
|
* bits for the gdsc before the power cycle is completed
|
|
|
|
* it might read an 'on' status wrongly.
|
|
|
|
*/
|
|
|
|
udelay(1);
|
|
|
|
|
2018-05-02 06:58:47 +00:00
|
|
|
ret = gdsc_poll_status(sc, GDSC_ON);
|
2017-01-23 04:26:21 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-11-18 12:28:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 10:37:44 +00:00
|
|
|
if (sc->pwrsts & PWRSTS_OFF)
|
|
|
|
gdsc_clear_mem_on(sc);
|
|
|
|
|
2018-05-02 06:58:47 +00:00
|
|
|
ret = gdsc_toggle_logic(sc, GDSC_OFF);
|
2016-10-20 09:38:06 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (sc->flags & CLAMP_IO)
|
|
|
|
gdsc_assert_clamp_io(sc);
|
|
|
|
|
|
|
|
return 0;
|
2015-08-06 10:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gdsc_init(struct gdsc *sc)
|
|
|
|
{
|
|
|
|
u32 mask, val;
|
|
|
|
int on, ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable HW trigger: collapse/restore occur based on registers writes.
|
|
|
|
* Disable SW override: Use hardware state-machine for sequencing.
|
|
|
|
* Configure wait time between states.
|
|
|
|
*/
|
|
|
|
mask = HW_CONTROL_MASK | SW_OVERRIDE_MASK |
|
|
|
|
EN_REST_WAIT_MASK | EN_FEW_WAIT_MASK | CLK_DIS_WAIT_MASK;
|
|
|
|
val = EN_REST_WAIT_VAL | EN_FEW_WAIT_VAL | CLK_DIS_WAIT_VAL;
|
|
|
|
ret = regmap_update_bits(sc->regmap, sc->gdscr, mask, val);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-08-06 10:37:45 +00:00
|
|
|
/* Force gdsc ON if only ON state is supported */
|
|
|
|
if (sc->pwrsts == PWRSTS_ON) {
|
2018-05-02 06:58:47 +00:00
|
|
|
ret = gdsc_toggle_logic(sc, GDSC_ON);
|
2015-08-06 10:37:45 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-02 06:58:47 +00:00
|
|
|
on = gdsc_check_status(sc, GDSC_ON);
|
2015-08-06 10:37:42 +00:00
|
|
|
if (on < 0)
|
|
|
|
return on;
|
|
|
|
|
2015-12-01 16:12:13 +00:00
|
|
|
/*
|
|
|
|
* Votable GDSCs can be ON due to Vote from other masters.
|
|
|
|
* If a Votable GDSC is ON, make sure we have a Vote.
|
|
|
|
*/
|
|
|
|
if ((sc->flags & VOTABLE) && on)
|
|
|
|
gdsc_enable(&sc->pd);
|
|
|
|
|
2018-03-23 08:26:14 +00:00
|
|
|
/* If ALWAYS_ON GDSCs are not ON, turn them ON */
|
|
|
|
if (sc->flags & ALWAYS_ON) {
|
|
|
|
if (!on)
|
|
|
|
gdsc_enable(&sc->pd);
|
|
|
|
on = true;
|
|
|
|
sc->pd.flags |= GENPD_FLAG_ALWAYS_ON;
|
|
|
|
}
|
|
|
|
|
2015-08-06 10:37:44 +00:00
|
|
|
if (on || (sc->pwrsts & PWRSTS_RET))
|
|
|
|
gdsc_force_mem_on(sc);
|
|
|
|
else
|
|
|
|
gdsc_clear_mem_on(sc);
|
|
|
|
|
2018-11-26 17:20:31 +00:00
|
|
|
if (!sc->pd.power_off)
|
|
|
|
sc->pd.power_off = gdsc_disable;
|
|
|
|
if (!sc->pd.power_on)
|
|
|
|
sc->pd.power_on = gdsc_enable;
|
2015-08-06 10:37:42 +00:00
|
|
|
pm_genpd_init(&sc->pd, NULL, !on);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-01 16:12:11 +00:00
|
|
|
int gdsc_register(struct gdsc_desc *desc,
|
2015-08-06 10:37:45 +00:00
|
|
|
struct reset_controller_dev *rcdev, struct regmap *regmap)
|
2015-08-06 10:37:42 +00:00
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
struct genpd_onecell_data *data;
|
2015-12-01 16:12:11 +00:00
|
|
|
struct device *dev = desc->dev;
|
|
|
|
struct gdsc **scs = desc->scs;
|
|
|
|
size_t num = desc->num;
|
2015-08-06 10:37:42 +00:00
|
|
|
|
|
|
|
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
|
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
data->domains = devm_kcalloc(dev, num, sizeof(*data->domains),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!data->domains)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
data->num_domains = num;
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
if (!scs[i])
|
|
|
|
continue;
|
|
|
|
scs[i]->regmap = regmap;
|
2015-08-06 10:37:45 +00:00
|
|
|
scs[i]->rcdev = rcdev;
|
2015-08-06 10:37:42 +00:00
|
|
|
ret = gdsc_init(scs[i]);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
data->domains[i] = &scs[i]->pd;
|
|
|
|
}
|
|
|
|
|
2015-12-01 16:12:11 +00:00
|
|
|
/* Add subdomains */
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
if (!scs[i])
|
|
|
|
continue;
|
|
|
|
if (scs[i]->parent)
|
|
|
|
pm_genpd_add_subdomain(scs[i]->parent, &scs[i]->pd);
|
|
|
|
}
|
|
|
|
|
2015-08-06 10:37:42 +00:00
|
|
|
return of_genpd_add_provider_onecell(dev->of_node, data);
|
|
|
|
}
|
|
|
|
|
2015-12-01 16:12:11 +00:00
|
|
|
void gdsc_unregister(struct gdsc_desc *desc)
|
2015-08-06 10:37:42 +00:00
|
|
|
{
|
2015-12-01 16:12:11 +00:00
|
|
|
int i;
|
|
|
|
struct device *dev = desc->dev;
|
|
|
|
struct gdsc **scs = desc->scs;
|
|
|
|
size_t num = desc->num;
|
|
|
|
|
|
|
|
/* Remove subdomains */
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
if (!scs[i])
|
|
|
|
continue;
|
|
|
|
if (scs[i]->parent)
|
|
|
|
pm_genpd_remove_subdomain(scs[i]->parent, &scs[i]->pd);
|
|
|
|
}
|
2015-08-06 10:37:42 +00:00
|
|
|
of_genpd_del_provider(dev->of_node);
|
|
|
|
}
|