Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 180 additions & 17 deletions drivers/soc/apple/t6041-sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

#include <clocksource/arm_arch_timer.h>
#include <dt-bindings/gpio/gpio.h>
#include <linux/bitfield.h>
#include <linux/bitmap.h>
#include <linux/build_bug.h>
Expand Down Expand Up @@ -89,6 +90,34 @@

#define AOP_PS_WORDS 44

#define T6041_GP_PORTS 4
#define APCIE_PORT_L2 0x088
#define APCIE_PORT_L2_ENTER 0x11
#define APCIE_PORT_L2_BUSY BIT(0)
#define APCIE_PORT_INTSTAT 0x100
#define APCIE_PORT_INTMSK 0x104
#define APCIE_PORT_INT_LINK GENMASK(7, 4)
#define APCIE_PORT_PERST_GUARD 0x13c
#define APCIE_PORT_LINKSTS 0x208
#define APCIE_PORT_LINKSTS_UP BIT(0)
#define APCIE_PORT_APPCLK 0x800
#define APCIE_PORT_APPCLK_EN BIT(0)
#define APCIE_PORT_APPCLK_CGDIS BIT(8)
#define APCIE_PORT_STATUS 0x804
#define APCIE_PORT_STATUS_READY BIT(0)
#define APCIE_PORT_PERST 0x82c
#define APCIE_PHY_CLK0REQ BIT(0)
#define APCIE_PHY_CLK1REQ BIT(1)
#define APCIE_PHY_CLK0ACK BIT(2)
#define APCIE_PHY_CLK1ACK BIT(3)
#define APCIE_PHY_RESET BIT(4)
#define APCIE_PHY_EN0 BIT(9)
#define APCIE_PHY_EN1 BIT(10)
#define APCIE_PHY_LINK BIT(31)
#define APCIE_PORT_STOP_US 5000
#define APCIE_PERST_HOLD_US 1000
#define GPIO_PIN_DATA BIT(0)

#define WAKE_WORDS 9
#define WAKE_LAST_MASK 0x00003fffU
#define WAKE_MAX_ID (32 * (WAKE_WORDS - 1) + 14)
Expand Down Expand Up @@ -192,6 +221,7 @@ struct t6041_sleep {
u32 cpm_off_late;
unsigned int spmi_late;
unsigned int ps_late;
unsigned int gp_stop_late;
unsigned int gp_late;
/* Saved as the commit starts, put back by t6041_sleep_return(). */
u64 cntkctl;
Expand All @@ -203,16 +233,23 @@ struct t6041_sleep {
u32 pre_mask[2];
u32 pre_wake[WAKE_WORDS];
u32 pre_votes[5];
u32 pre_disp_cluster[2];
/* Each AMCC plane's cache ways (+0x2800) and status (+0x2804). */
u32 pre_ways[4 * AMCC_MAX_GROUPS];
u32 pre_cache[4 * AMCC_MAX_GROUPS];
bool pre_a0;
/* GP PCIe ports 0/1: port block, PHY word, and saved state. */
void __iomem *gp_port[2];
void __iomem *gp_phy[2];
u32 gp_phy_pre[2];
u32 gp_app_pre[2];
u32 gp_perst_pre[2];
/*
* GP PCIe ports: port block, PHY word, and saved state. Ports 0/1 are
* required; 2/3 are restored when described and set up.
*/
void __iomem *gp_port[T6041_GP_PORTS];
void __iomem *gp_phy[T6041_GP_PORTS];
int gp_perst[T6041_GP_PORTS];
bool gp_perst_low[T6041_GP_PORTS];
u32 gp_phy_pre[T6041_GP_PORTS];
u32 gp_app_pre[T6041_GP_PORTS];
u32 gp_perst_pre[T6041_GP_PORTS];
unsigned int gp_up;
bool gp_saved;
/* ANS's storage PCIe block and bootloader-supplied tunables (apple,apcie). */
void __iomem *ap_rc;
Expand Down Expand Up @@ -674,13 +711,81 @@ static void t6041_ptd_soc_dev_pulse(struct t6041_sleep *s)
writeq(0x800ULL, s->ptd + PTD_SOC_DEV_REQ_WRITE);
}

/*
* Stop every active GP PCIe port before the PCIe block loses power (in the
* same order as sleep-t6040.h): link into L2, endpoint PERST# asserted, PHY
* clocks and port application clock off. Keep PERST# asserted in gpio_pre so
* wake's GPIO restore never deasserts it before refclk is back.
*/
static void t6041_gp_ports_stop(struct t6041_sleep *s)
{
unsigned int i;
u32 v;

if (!s->gp_saved ||
FIELD_GET(PS_ACTUAL, readl(s->ps_apcie[T6041_APCIE_PHY_SW])) != 0xf)
return;

for (i = 0; i < T6041_GP_PORTS; i++) {
void __iomem *port = s->gp_port[i];
void __iomem *phy = s->gp_phy[i];

if (!(s->gp_up & BIT(i)) ||
!(s->gp_app_pre[i] & APCIE_PORT_APPCLK_EN))
continue;

writel(readl(port + APCIE_PORT_INTMSK) | APCIE_PORT_INT_LINK,
port + APCIE_PORT_INTMSK);
writel(APCIE_PORT_INT_LINK, port + APCIE_PORT_INTSTAT);
writel(readl(port + APCIE_PORT_APPCLK) | APCIE_PORT_APPCLK_CGDIS,
port + APCIE_PORT_APPCLK);
if (readl(port + APCIE_PORT_LINKSTS) & APCIE_PORT_LINKSTS_UP) {
writel(APCIE_PORT_L2_ENTER, port + APCIE_PORT_L2);
if (t6041_poll_until_us(port + APCIE_PORT_L2, v,
!(v & APCIE_PORT_L2_BUSY),
APCIE_PORT_STOP_US))
s->gp_stop_late |= BIT(i);
}
if (s->gp_perst[i] >= 0) {
void __iomem *reg = s->gpio + 4 * s->gp_perst[i];

v = readl(reg);
v = s->gp_perst_low[i] ? (v & ~GPIO_PIN_DATA) :
(v | GPIO_PIN_DATA);
writel(v, reg);
if (!test_bit(s->gp_perst[i], s->gpio_skip))
s->gpio_pre[s->gp_perst[i]] = v;
}
writel(readl(phy) & ~APCIE_PHY_LINK, phy);
t6041_spin_us(APCIE_PERST_HOLD_US);
writel(readl(port + APCIE_PORT_PERST_GUARD) | BIT(8),
port + APCIE_PORT_PERST_GUARD);
writel(0, port + APCIE_PORT_PERST);
writel(readl(port + APCIE_PORT_PERST_GUARD) & ~BIT(8),
port + APCIE_PORT_PERST_GUARD);
writel(readl(phy) & ~APCIE_PHY_EN1, phy);
writel(readl(phy) & ~APCIE_PHY_EN0, phy);
writel(readl(phy) | APCIE_PHY_RESET, phy);
writel(readl(phy) & ~APCIE_PHY_CLK0REQ, phy);
if (t6041_poll_until_us(phy, v, !(v & APCIE_PHY_CLK0ACK),
APCIE_PORT_STOP_US))
s->gp_stop_late |= BIT(i);
writel(readl(phy) & ~APCIE_PHY_CLK1REQ, phy);
writel(readl(port + APCIE_PORT_APPCLK) & ~APCIE_PORT_APPCLK_EN,
port + APCIE_PORT_APPCLK);
writel(readl(port + APCIE_PORT_INTSTAT),
port + APCIE_PORT_INTSTAT);
}
}

static int t6041_sleep_commit(unsigned long unused)
{
struct t6041_sleep *s = t6041_sleep;
unsigned int i, cpu;
u32 v;

t6041_save(s);
t6041_gp_ports_stop(s);
s->stop_ret = spin_table_stop_parked();
t6041_ave_off(s);
/* While every AMCC slice is still on: a clock-gated plane faults. */
Expand Down Expand Up @@ -895,6 +1000,8 @@ static void t6041_save(struct t6041_sleep *s)
}
for (i = 0; i < ARRAY_SIZE(t6041_pmc_vote_off); i++)
s->pre_votes[i] = readl(s->pmc + t6041_pmc_vote_off[i]);
s->pre_disp_cluster[0] = readl(s->cluster + 0x0c);
s->pre_disp_cluster[1] = readl(s->cluster + 0x10);
s->cntkctl = read_sysreg(cntkctl_el1);
s->siq_cfg = read_sysreg_s(SYS_APL_SIQ_CFG_EL1);
for (i = 0; i < s->n_restore; i++)
Expand Down Expand Up @@ -923,10 +1030,16 @@ static void t6041_save(struct t6041_sleep *s)
s->gp_saved = s->gp_port[0] && s->gp_port[1] && s->gp_phy[0] && s->gp_phy[1] &&
FIELD_GET(PS_ACTUAL, readl(s->ps_apcie[T6041_APCIE_GP])) == 0xf &&
FIELD_GET(PS_ACTUAL, readl(s->ps_apcie[T6041_APCIE_SYS_GP])) == 0xf;
for (i = 0; i < 2 && s->gp_saved; i++) {
s->gp_up = 0;
for (i = 0; i < T6041_GP_PORTS && s->gp_saved; i++) {
if (!s->gp_port[i] || !s->gp_phy[i])
continue;
s->gp_phy_pre[i] = readl(s->gp_phy[i]);
s->gp_app_pre[i] = readl(s->gp_port[i] + 0x800);
s->gp_perst_pre[i] = readl(s->gp_port[i] + 0x82c);
/* The PCIe driver requests a port's refclk when it sets the port up. */
if (s->gp_phy_pre[i] & BIT(0))
s->gp_up |= BIT(i);
}
}

Expand Down Expand Up @@ -1056,21 +1169,22 @@ static int t6041_apcie_raise(struct t6041_sleep *s)
}

/*
* Restore GP PCIe port 0/1 reference and app clocks after deep sleep so their
* DART blocks do not raise SErrors on resume. Returns a mask of late ports.
* Restore the reference and app clocks of the GP PCIe ports that were set up
* before the sleep, so their DART blocks do not raise SErrors on resume.
* Returns a mask of late ports.
*/
static unsigned int t6041_gp_pcie_clk(struct t6041_sleep *s)
{
unsigned int i, late = 0;
u32 v;

for (i = 0; i < 2; i++) {
for (i = 0; i < T6041_GP_PORTS; i++) {
void __iomem *phy = s->gp_phy[i];
void __iomem *port = s->gp_port[i];
u32 cfg = s->gp_phy_pre[i];
u32 app = s->gp_app_pre[i];

if (!s->gp_saved ||
if (!s->gp_saved || !(s->gp_up & BIT(i)) ||
FIELD_GET(PS_ACTUAL, readl(s->ps_apcie[T6041_APCIE_GP])) != 0xf ||
FIELD_GET(PS_ACTUAL, readl(s->ps_apcie[T6041_APCIE_SYS_GP])) != 0xf)
continue;
Expand All @@ -1086,7 +1200,8 @@ static unsigned int t6041_gp_pcie_clk(struct t6041_sleep *s)
udelay(1);
}
writel(readl(phy) | (cfg & (BIT(9) | BIT(10))), phy);
writel(readl(phy) | (cfg & BIT(31)), phy);
if (s->gp_perst[i] < 0)
writel(readl(phy) | (cfg & BIT(31)), phy);
writel((readl(port + 0x800) & ~(BIT(0) | BIT(8))) | (app & (BIT(0) | BIT(8))),
port + 0x800);
writel(readl(port + 0x13c) | BIT(8), port + 0x13c);
Expand Down Expand Up @@ -1175,6 +1290,15 @@ static void t6041_sleep_return(struct t6041_sleep *s)
writel(0xf, s->cluster + 0x0);
writel(0xf, s->cluster + 0x4);
writel(0xf, s->cluster + 0x8);
/* Restore DISP_INT (+0x0c) and DISP_EXT (+0x10, DISPEXT_RAIL_V) complexes. */
for (i = 0; i < ARRAY_SIZE(s->pre_disp_cluster); i++) {
u32 on = s->pre_disp_cluster[i] & 0xf;

if (!on)
continue;
v = readl(s->cluster + 0x0c + 4 * i);
writel((v & ~0xfU) | on, s->cluster + 0x0c + 4 * i);
}
/* The two wake gates, then the CPM voters. */
writel(readl(s->pwrgate + 0x000) | BIT(31), s->pwrgate + 0x000);
writel(readl(s->pwrgate + 0x040) | BIT(31), s->pwrgate + 0x040);
Expand Down Expand Up @@ -1284,6 +1408,7 @@ static int t6041_sleep_enter(suspend_state_t state)
s->cpm_off_late = 0;
s->spmi_late = 0;
s->ps_late = 0;
s->gp_stop_late = 0;
s->gp_late = 0;
/* macOS stops the PMP before its last steps; past this a refusal can't go back. */
ret = apple_sleep_coprocs_stop_all();
Expand Down Expand Up @@ -1329,6 +1454,8 @@ static void t6041_sleep_end(void)
dev_dbg(s->dev, "%u power domains took more than 3 ms to come back\n", s->ps_late);
if (s->apcie_ret)
dev_warn(s->dev, "storage link raise timed out at step %d\n", s->apcie_ret);
if (s->gp_stop_late)
dev_warn(s->dev, "GP PCIe ports %#x stop timed out\n", s->gp_stop_late);
if (s->gp_late)
dev_warn(s->dev, "GP PCIe ports %#x didn't come back\n", s->gp_late);
}
Expand Down Expand Up @@ -1982,10 +2109,23 @@ static void __iomem *t6041_apcie_block(struct device *dev, struct device_node *n
return IS_ERR(base) ? NULL : base;
}

static int t6041_gp_child_port(struct device_node *child)
{
u32 reg;

if (of_property_read_u32_index(child, "reg", 0, &reg))
return -1;
return (reg >> 11) & 0x1f;
}

static void t6041_map_apcie(struct t6041_sleep *s)
{
struct device *dev = s->dev;
struct device_node *np;
struct device_node *np, *pin_np, *child;
unsigned int i;

for (i = 0; i < T6041_GP_PORTS; i++)
s->gp_perst[i] = -1;

np = of_parse_phandle(dev->of_node, "apple,apcie", 0);
if (!np)
Expand All @@ -2004,10 +2144,33 @@ static void t6041_map_apcie(struct t6041_sleep *s)
"apple,tunable-phy-ip-pll", &s->tun_phy_ip_pll);
s->tun_phy_ip_lanes = t6041_apcie_tunable(dev, np, "phy-ip",
"apple,tunable-phy-ip-auspma");
s->gp_port[0] = t6041_apcie_block(dev, np, "port0", 0x830, NULL, NULL);
s->gp_port[1] = t6041_apcie_block(dev, np, "port1", 0x830, NULL, NULL);
s->gp_phy[0] = t6041_apcie_block(dev, np, "phy0", 0x4, NULL, NULL);
s->gp_phy[1] = t6041_apcie_block(dev, np, "phy1", 0x4, NULL, NULL);
for (i = 0; i < T6041_GP_PORTS; i++) {
char name[8];

snprintf(name, sizeof(name), "port%u", i);
s->gp_port[i] = t6041_apcie_block(dev, np, name, 0x830, NULL, NULL);
snprintf(name, sizeof(name), "phy%u", i);
s->gp_phy[i] = t6041_apcie_block(dev, np, name, 0x4, NULL, NULL);
}
pin_np = of_parse_phandle(dev->of_node, "apple,pinctrl", 0);
if (pin_np && of_device_is_available(np)) {
for_each_available_child_of_node(np, child) {
struct of_phandle_args args;
int p = t6041_gp_child_port(child);

if (p < 0 || p >= T6041_GP_PORTS ||
of_parse_phandle_with_args(child, "reset-gpios",
"#gpio-cells", 0, &args))
continue;
if (args.np == pin_np && args.args_count >= 2 &&
args.args[0] < s->npins) {
s->gp_perst[p] = args.args[0];
s->gp_perst_low[p] = !!(args.args[1] & GPIO_ACTIVE_LOW);
}
of_node_put(args.np);
}
}
of_node_put(pin_np);
of_node_put(np);

if (s->ap_axi)
Expand Down
Loading