Skip to content
Closed
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions drivers/mfd/macsmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -683,11 +683,14 @@ MODULE_DEVICE_TABLE(of, apple_smc_of_match);
/* The gaps macOS leaves before the AP idle notice and after it. */
#define SMC_SLEEP_ARM_MS 6
#define SMC_SLEEP_IDLE_MS 30
/* The shortest gap macOS left between slpw and slpa, in two traced sleeps. */
#define SMC_SLEEP_WARN_MS 770
/*
* Between a late warning (a sleep that cut the debug link) and slpa. macOS's shortest gap
* was 770 ms; the warning at .prepare leaves 150-500 ms, and those sleeps run to the alarm.
*/
#define SMC_SLEEP_WARN_MS 200

/* After the debug link's cut. Its port and charger events keep coming for over a second. */
#define SMC_OUTBOX_QUIET_MS 500
/* After the debug link's cut. No message came in it in any logged sleep. */
#define SMC_OUTBOX_QUIET_MS 100
#define SMC_OUTBOX_SETTLE_MS 5000

static int apple_smc_ap_power(struct apple_smc *smc, bool on)
Expand Down
40 changes: 27 additions & 13 deletions drivers/soc/apple/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/irqchip/irq-apple-aic.h>
#include <linux/ktime.h>
#include <linux/moduleparam.h>
#include <linux/mutex.h>
#include <linux/of.h>
Expand Down Expand Up @@ -162,7 +163,9 @@ struct aura_s2r_mailbox {
#define HPM_CMD_SSPS 0x53505353
#define HPM_CMD_COLD_RESET 0x64696147
#define HPM_SSPS_S3 3
#define HPM_RESET_MS 1000
/* The first look after the reset, and how long a contract that never seemed to go is trusted to have gone. */
#define HPM_RESET_FIRST_MS 50
#define HPM_RESET_FLOOR_MS 1000
#define HPM_PAGE_POWER_STATUS 0x3f
#define HPM_POWER_CONNECTED BIT(0)
#define HPM_POWER_TYPEC_MODE GENMASK(3, 2)
Expand Down Expand Up @@ -1837,26 +1840,38 @@ static int sleep_hpm_sleep(struct apple_sleep *s)
* Left to finish across the commit, that sometimes failed: neither side had a contract or
* saw the other, and no VDM got through until a replug. Returns the ms it took.
*/
static int sleep_hpm_wait_contract(struct apple_sleep *s)
static int sleep_hpm_wait_contract(struct apple_sleep *s, int *gone_ms)
{
bool wake = true;
ktime_t start = ktime_get();
bool wake = true, up;
u32 val = 0;
int ms, ret;

for (ms = 0; ms < HPM_CONTRACT_MS; ms += 10) {
/*
* It reads back as before for a moment after the reset. Count it as back once it was
* seen gone (no contract, or no answer while the controller restarts), or after the
* floor if it never seemed to go.
*/
*gone_ms = -1;
mdelay(HPM_RESET_FIRST_MS);
for (;;) {
ms = ktime_ms_delta(ktime_get(), start);
if (ms >= HPM_CONTRACT_MS)
return -ETIMEDOUT;
ret = wake ? sleep_hpm_cmd(s, SPMI_OP_WAKEUP, 0, NULL, NULL) : 0;
if (!ret)
ret = sleep_hpm_select(s, HPM_PAGE_POWER_STATUS);
if (!ret)
ret = sleep_hpm_cmd(s, SPMI_OP_EXT_READ(4), HPM_REG_DATA, NULL, &val);
if (!ret && (val & HPM_POWER_CONNECTED) &&
FIELD_GET(HPM_POWER_TYPEC_MODE, val) == HPM_POWER_TYPEC_PD)
up = !ret && (val & HPM_POWER_CONNECTED) &&
FIELD_GET(HPM_POWER_TYPEC_MODE, val) == HPM_POWER_TYPEC_PD;
if (!up && *gone_ms < 0)
*gone_ms = ms;
if (up && (*gone_ms >= 0 || ms >= HPM_RESET_FLOOR_MS))
return ms;
wake = ret;
mdelay(10);
}

return -ETIMEDOUT;
}

/* A live debug link holds the debug switch domain on. */
Expand All @@ -1877,7 +1892,7 @@ static bool apple_sleep_usb_down(struct apple_sleep *s)
{
u32 cmd = HPM_CMD_COLD_RESET;
u32 enable;
int i, ret, ms;
int i, ret, ms, gone;

if (s->usb_down_ran)
return false;
Expand All @@ -1899,11 +1914,10 @@ static bool apple_sleep_usb_down(struct apple_sleep *s)
if (ret)
break;
WRITE_ONCE(sleep_debug_link_cut, true);
mdelay(HPM_RESET_MS);
ms = sleep_hpm_wait_contract(s);
ms = sleep_hpm_wait_contract(s, &gone);
if (ms >= 0) {
dev_info(s->dev, "debug link: contract back %d ms after reset %d\n",
HPM_RESET_MS + ms, i);
dev_info(s->dev, "debug link: contract back %d ms after reset %d (gone at %d ms)\n",
ms, i, gone);
break;
}
dev_warn(s->dev, "debug link: no contract with the partner after reset %d\n", i);
Expand Down
Loading