diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index 6ad1d66a67fb9..96c7011ab23ac 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -127,6 +127,8 @@ static const char *const modes[] = { /* Unrecognized commands will be replaced with "!CMD" */ #define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321) +#define TPS_CMD_IDLE_POLL_US 10000 + static enum power_supply_property tps6598x_psy_props[] = { POWER_SUPPLY_PROP_USB_TYPE, POWER_SUPPLY_PROP_ONLINE, @@ -354,16 +356,13 @@ static int tps6598x_disconnect(struct tps6598x *tps, u32 status) /* * The sleep's rules for the port controllers' system power state command (the controller is * woken first, the command's end is read at intervals, a failed S3 refuses the sleep, the - * command's limit and its lines) apply on the J714s, J716c, J813 and J815. - * These boards send the command (apple,sleep-ssps) during firmware-driven - * sleep. Other boards retain the original command behavior. + * command's limit and its lines) apply to every controller that sends the command around a + * firmware-driven sleep (apple,sleep-ssps, read into tps->sleep_ssps by tipd_init()). Other + * controllers retain the original command behavior. */ -static bool tipd_sleep_ssps_rules(void) +static bool tipd_sleep_ssps_rules(const struct tps6598x *tps) { - return of_machine_is_compatible("apple,j714s") || - of_machine_is_compatible("apple,j716c") || - of_machine_is_compatible("apple,j813") || - of_machine_is_compatible("apple,j815"); + return tps->sleep_ssps; } /* @@ -415,15 +414,14 @@ static int tps6598x_exec_cmd_gap(struct tps6598x *tps, const char *cmd, return -EINVAL; /* - * Under the sleep's rules a command that has ended has ended, whatever the time - * its last read was made at. Elsewhere a read made past the limit is a timeout, - * as before. + * A command that has ended has ended, whatever the time its last read was made + * at: only a command still running past the limit is a timeout. */ - if ((val || !tipd_sleep_ssps_rules()) && time_is_before_jiffies(timeout)) + if (val && time_is_before_jiffies(timeout)) return -ETIMEDOUT; /* A command with a long limit is not waited for in silence: a line each second */ if (val && cmd_timeout_ms > 1000 && time_is_before_jiffies(note) && - tipd_sleep_ssps_rules()) { + tipd_sleep_ssps_rules(tps)) { dev_info(tps->dev, "command %.4s not done after %u ms (limit %u ms)\n", cmd, jiffies_to_msecs(jiffies - (timeout - msecs_to_jiffies(cmd_timeout_ms))), cmd_timeout_ms); @@ -476,6 +474,56 @@ static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd, out_len, out_data, 1000, 0); } +/** + * tipd_cmd_wait_idle - wait for the controller to end the command it is running + * @tps: the controller + * @timeout_ms: how long to wait + * @busy: if not NULL, set to the command still running at the end, or 0 + * + * The command register holds a command's 4CC until the controller has ended it, and then + * reads 0, or "!CMD" for a command it did not know. That is the controller's only report of a + * command's end. A command that this driver gave up waiting for, or that a reboot cut short, + * keeps running, and the next command written meanwhile is refused (-EBUSY). A read that + * fails is taken as the controller still being busy, as a bus with a busy controller on it can + * fail reads. + * + * Return: 0 once the controller is idle, -ETIMEDOUT if a command is still running at the end, + * or the error of the last read if the register could not be read until then. + */ +int tipd_cmd_wait_idle(struct tps6598x *tps, unsigned int timeout_ms, u32 *busy) +{ + unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); + u32 val = 0; + int ret; + + if (busy) + *busy = 0; + for (;;) { + ret = tps6598x_read32(tps, TPS_REG_CMD1, &val); + if (!ret && (!val || INVALID_CMD(val))) + return 0; + if (time_is_before_jiffies(timeout)) + break; + usleep_range(TPS_CMD_IDLE_POLL_US, TPS_CMD_IDLE_POLL_US + TPS_CMD_IDLE_POLL_US / 4); + } + if (ret) + return ret; + if (busy) + *busy = val; + return -ETIMEDOUT; +} +EXPORT_SYMBOL_GPL(tipd_cmd_wait_idle); + +/* + * Write a command without the check that the controller has ended the last one. Only for a + * reset of a controller whose last command does not end (see tipd_cmd_wait_idle()). + */ +int tipd_cmd_write_busy(struct tps6598x *tps, const char *cmd) +{ + return tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd); +} +EXPORT_SYMBOL_GPL(tipd_cmd_write_busy); + static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role) { const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF"; @@ -1719,17 +1767,12 @@ static int cd321x_prepare(struct tps6598x *tps) cancel_delayed_work_sync(&cd321x->rearm_work); /* As macOS does a second or so before the commit, measured on the same controller. */ - if (pm_suspend_via_firmware() && device_property_read_bool(tps->dev, "apple,sleep-ssps")) { + if (pm_suspend_via_firmware() && tipd_sleep_ssps_rules(tps)) { ktime_t start = ktime_get(); guard(mutex)(&tps->lock); ret = tps->data->switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S3); - /* Without the rules: the lines and their levels as they were */ - if (!tipd_sleep_ssps_rules() && ret) - dev_warn(tps->dev, "didn't take system power state S3: %d\n", ret); - else if (!tipd_sleep_ssps_rules()) - dev_dbg(tps->dev, "system power state S3\n"); - else if (ret) + if (ret) dev_warn(tps->dev, "didn't take system power state S3: %d after %lld ms%s\n", ret, ktime_ms_delta(ktime_get(), start), sleep_s3_refuse ? ": sleep refused" : ""); @@ -1737,7 +1780,7 @@ static int cd321x_prepare(struct tps6598x *tps) dev_info(tps->dev, "system power state S3 in %lld ms\n", ktime_ms_delta(ktime_get(), start)); } - if (ret && sleep_s3_refuse && tipd_sleep_ssps_rules()) + if (ret && sleep_s3_refuse) return -EBUSY; return 0; } @@ -1916,14 +1959,12 @@ static void cd321x_complete(struct tps6598x *tps) cd321x->pm.setup_pending = true; cd321x->reset_by_cut = true; } - if (pm_suspend_via_firmware() && device_property_read_bool(tps->dev, "apple,sleep-ssps")) { + if (pm_suspend_via_firmware() && tipd_sleep_ssps_rules(tps)) { ktime_t start = ktime_get(); int ret = tps->data->switch_power_state(tps, TPS_SYSTEM_POWER_STATE_S0); /* After a sleep or a refused one: never waited for in silence, and never longer */ - if (!tipd_sleep_ssps_rules()) - ret = 0; - else if (ret) + if (ret) dev_warn(tps->dev, "didn't take system power state S0: %d after %lld ms; set up again by its next event\n", ret, ktime_ms_delta(ktime_get(), start)); else @@ -2395,7 +2436,7 @@ static int cd321x_switch_power_state(struct tps6598x *tps, u8 target_state) * Under the sleep's rules the command goes to a controller that has just been sent its * bus's wake command and has answered it, whatever it was doing: woken, read, commanded. */ - if (tps->wake && tipd_sleep_ssps_rules()) { + if (tps->wake && tipd_sleep_ssps_rules(tps)) { ret = tps->wake(tps); if (ret) return ret; @@ -2413,13 +2454,13 @@ static int cd321x_switch_power_state(struct tps6598x *tps, u8 target_state) * the sleep's rules. Under them S0 and S3 have the sleep's own limit and are read with * gaps. */ - if ((target_state == TPS_SYSTEM_POWER_STATE_S0 || tipd_sleep_ssps_rules()) && + if ((target_state == TPS_SYSTEM_POWER_STATE_S0 || tipd_sleep_ssps_rules(tps)) && device_is_compatible(tps->dev, "apple,sn201202x")) ret = tps6598x_exec_cmd_gap(tps, "SSPS", sizeof(target_state), &target_state, 0, NULL, - tipd_sleep_ssps_rules() ? + tipd_sleep_ssps_rules(tps) ? clamp(sleep_ssps_ms, 1000U, 5000U) : 5000, 0, - tipd_sleep_ssps_rules() ? CD321X_SSPS_READ_GAP_US : 0); + tipd_sleep_ssps_rules(tps) ? CD321X_SSPS_READ_GAP_US : 0); else ret = tps6598x_exec_cmd(tps, "SSPS", sizeof(target_state), &target_state, 0, NULL); @@ -2729,6 +2770,21 @@ VISIBLE_IF_KUNIT int cd321x_tbt_watch_late(struct cd321x *cd321x, } EXPORT_SYMBOL_IF_KUNIT(cd321x_tbt_watch_late); +/* + * At shutdown: no worker runs again, so none sends the controller a command. Unlike + * cd321x_stop_work(), the Thunderbolt and display notifiers stay registered: what they queue + * is dropped, and the routes are left as they are. + */ +static void cd321x_shutdown(struct tps6598x *tps) +{ + struct cd321x *cd321x = container_of(tps, struct cd321x, tps); + + disable_work_sync(&cd321x->tbt_attach_work); + disable_delayed_work_sync(&cd321x->resume_work); + disable_delayed_work_sync(&cd321x->update_work); + disable_delayed_work_sync(&cd321x->rearm_work); +} + /* Separate so that the KUnit tests can drive the workers without a port. */ VISIBLE_IF_KUNIT void cd321x_init_work(struct cd321x *cd321x) { @@ -3489,6 +3545,7 @@ int tipd_init(struct tps6598x *tps) return -EPROBE_DEFER; dev_dbg(tps->dev, "Type-C probe start\n"); + tps->sleep_ssps = device_property_read_bool(tps->dev, "apple,sleep-ssps"); tps->reset = devm_gpiod_get_optional(tps->dev, "reset", GPIOD_OUT_LOW); if (IS_ERR(tps->reset)) return dev_err_probe(tps->dev, PTR_ERR(tps->reset), @@ -3785,6 +3842,38 @@ void tipd_usb_wake(struct tps6598x *tps, bool asleep) } EXPORT_SYMBOL_GPL(tipd_usb_wake); +/* + * Leave the controller idle for the next boot. A command the controller is still running when + * the system restarts is still running when the next kernel probes it: on a J714s an LDCM ran + * for more than three seconds after a soft reboot, the bus reported frames that failed their + * parity check meanwhile, and the probe failed. Stop the interrupt and the workers, the only + * senders of commands once user space is gone, and wait under the device lock, so that no + * command of this driver is in flight, for the controller to end the one it runs. The port, its + * mux and its USB role are left as they are. + */ +void tipd_shutdown(struct tps6598x *tps) +{ + u32 busy; + int ret; + + if (tps->irq) + disable_irq(tps->irq); + else + disable_delayed_work_sync(&tps->wq_poll); + if (tps->contaminant_ops) + tps->contaminant_ops->stop(tps); + if (tps->data->shutdown) + tps->data->shutdown(tps); + + scoped_guard(mutex, &tps->lock) + ret = tipd_cmd_wait_idle(tps, TPS_CMD_MAX_MS, &busy); + if (ret == -ETIMEDOUT) + dev_warn(tps->dev, "command %.4s still running at shutdown\n", (char *)&busy); + else if (ret) + dev_warn(tps->dev, "command register unreadable at shutdown: %d\n", ret); +} +EXPORT_SYMBOL_GPL(tipd_shutdown); + int tipd_suspend(struct tps6598x *tps) { if (tps->wakeup) { @@ -3885,6 +3974,7 @@ static const struct tipd_data tipd_cd321x_data = { .connect = cd321x_connect, .prepare = cd321x_prepare, .complete = cd321x_complete, + .shutdown = cd321x_shutdown, }; static const struct tipd_data tipd_tps6598x_data = { @@ -3966,6 +4056,7 @@ static const struct tipd_data tipd_sn201202x_data = { .connect = cd321x_connect, .prepare = cd321x_prepare, .complete = cd321x_complete, + .shutdown = cd321x_shutdown, }; const struct tipd_data *tipd_hw_variants[] = { diff --git a/drivers/usb/typec/tipd/i2c.c b/drivers/usb/typec/tipd/i2c.c index 7e63d2dfc04c7..e1641771e39b8 100644 --- a/drivers/usb/typec/tipd/i2c.c +++ b/drivers/usb/typec/tipd/i2c.c @@ -58,6 +58,11 @@ static void tps6598x_remove_i2c(struct i2c_client *client) tipd_remove(tps); } +static void tps6598x_shutdown_i2c(struct i2c_client *client) +{ + tipd_shutdown(i2c_get_clientdata(client)); +} + /* * The I2C adapter suspends right after its clients and resumes right before * them, and cannot transfer in between. Keep the event handler out of that @@ -122,6 +127,7 @@ static struct i2c_driver tps6598x_i2c_driver = { }, .probe = tps6598x_probe_i2c, .remove = tps6598x_remove_i2c, + .shutdown = tps6598x_shutdown_i2c, .id_table = tps6598x_id, }; module_i2c_driver(tps6598x_i2c_driver); diff --git a/drivers/usb/typec/tipd/spmi.c b/drivers/usb/typec/tipd/spmi.c index 4865a65d9792d..9028ac976b3c2 100644 --- a/drivers/usb/typec/tipd/spmi.c +++ b/drivers/usb/typec/tipd/spmi.c @@ -734,48 +734,31 @@ static bool sn201202x_sleep_event(struct tps6598x *tps) } /* - * On the T6050 boards that sleep (J714s, J716c), the probe and the sleep's SLEEP command print a - * line; on every other machine they don't. A controller without a connector (the MagSafe one, - * hpm5) is probed as any other: it registers its Type-C port and power supply, and with - * apple,sleep-ssps it follows the system's sleep as the USB-C ports' controllers do (woken and - * switched to S3 at the prepare, SLEEP at the suspend, woken at the resume and switched to S0 - * at the complete). + * A controller without a connector (the MagSafe one, hpm5) is probed as any other: it registers + * its Type-C port and power supply, and with apple,sleep-ssps it follows the system's sleep as + * the USB-C ports' controllers do (woken and switched to S3 at the prepare, SLEEP at the + * suspend, woken at the resume and switched to S0 at the complete). */ -static bool sn201202x_sleep_board(void) -{ - return of_machine_is_compatible("apple,j714s") || of_machine_is_compatible("apple,j716c"); -} - -static bool sn201202x_j714s(void) -{ - return of_machine_is_compatible("apple,j714s"); -} /* - * A controller that the previous boot left in the middle of a command can fail its first - * probe on the reads that follow. On the J714s, after a soft reboot that ended a deep-sleep - * test run, the controllers of both USB-C ports ran their LDCM command for more than three - * seconds. Their SPMI buses then reported frames that failed the parity check and commands - * that were not acknowledged, the power status read failed with -EIO, and the probe failed with - * -EINVAL. Both ports, and every USB, Thunderbolt and DisplayPort device on them, stayed gone - * until the next boot. + * A controller that the previous boot left in the middle of a command keeps running it, and + * the first probe fails on the reads that follow: on the J714s, after a soft reboot that ended + * a deep-sleep test run, the controllers of both USB-C ports ran their LDCM command for more + * than three seconds. Their SPMI buses then reported frames that failed the parity check and + * commands that were not acknowledged, the power status read failed with -EIO, and the probe + * failed with -EINVAL. Both ports, and every USB, Thunderbolt and DisplayPort device on them, + * stayed gone until the next boot. * - * On that board a failed probe is tried again after a growing wait. After the third failed try - * the controller's firmware is cold-reset ("Gaid", as the debug link's recovery does), unless - * it is in the debug mode of the boot loader's debug link, and it is tried twice more. Each try - * runs in a devres group of its own, so what a failed try set up is released before the next. - * A controller that is still not up after that defers its probe: the driver core tries it - * again later, and the port is never left failed while its controller is there. + * tipd_shutdown() now leaves the controller idle, but a restart that does not run it (a panic, + * a watchdog, the boot loader) does not. So before the probe sends its first command, the + * command register is read until the controller has ended the command it runs, for at most the + * longest limit this driver gives a command. A controller that has not ended it by then is + * cold-reset once ("Gaid", as the debug link's recovery does), unless it is in the debug mode + * of the boot loader's debug link. */ -#define SN201202X_INIT_TRIES 5 -#define SN201202X_INIT_RESET_AFTER 3 #define SN201202X_RESET_MS 1000 #define SN201202X_RESET_UP_MS 2000 -static const unsigned int sn201202x_init_wait_ms[SN201202X_INIT_TRIES - 1] = { - 200, 500, 0, 1000, -}; - /* 0 with the controller's mode in @mode, or the error of the read */ static int sn201202x_mode(struct tps6598x *tps, char mode[4]) { @@ -786,21 +769,20 @@ static int sn201202x_mode(struct tps6598x *tps, char mode[4]) return memchr_inv(mode, 0, 4) ? 0 : -ENODEV; } +/* A controller whose last command does not end: the command is written over it */ static int sn201202x_cold_reset(struct sn201202x *sn) { struct tps6598x *tps = &sn->cd.tps; char mode[4]; int ms, ret; - sn201202x_wake(sn); ret = sn201202x_mode(tps, mode); /* The boot loader's debug link runs in this mode: it is never reset from here */ if (!ret && !memcmp(mode, "DBMa", 4)) return -EBUSY; - scoped_guard(mutex, &tps->lock) - ret = tps6598x_exec_cmd_tmo(tps, "Gaid", 0, NULL, 0, NULL, 2000, 0); + ret = tipd_cmd_write_busy(tps, "Gaid"); if (ret) - dev_dbg(tps->dev, "reset command: %d\n", ret); + return ret; msleep(SN201202X_RESET_MS); for (ms = 0; ms < SN201202X_RESET_UP_MS; ms += 50) { if (!sn201202x_wake(sn) && !sn201202x_mode(tps, mode) && !memcmp(mode, "APP ", 4)) @@ -813,44 +795,28 @@ static int sn201202x_cold_reset(struct sn201202x *sn) static int sn201202x_init(struct sn201202x *sn) { struct tps6598x *tps = &sn->cd.tps; - unsigned int try; - void *group; + u32 busy; int ret; - if (!sn201202x_j714s()) { - ret = tipd_init(tps); + ret = tipd_cmd_wait_idle(tps, TPS_CMD_MAX_MS, &busy); + if (ret) { + if (ret == -ETIMEDOUT) + dev_warn(tps->dev, "command %.4s from before this boot still running\n", + (char *)&busy); + else + dev_warn(tps->dev, "command register unreadable: %d\n", ret); + ret = sn201202x_cold_reset(sn); + dev_info(tps->dev, "controller reset: %d\n", ret); + if (!ret) + ret = tipd_cmd_wait_idle(tps, TPS_CMD_MAX_MS, NULL); if (ret) - cancel_delayed_work_sync(&sn->ldcm_work); - return ret; + return ret; } - for (try = 0; try < SN201202X_INIT_TRIES; try++) { - group = devres_open_group(tps->dev, NULL, GFP_KERNEL); - if (!group) - return -ENOMEM; - ret = tipd_init(tps); - if (!ret) { - devres_remove_group(tps->dev, group); - if (try) - dev_info(tps->dev, "port controller up at try %u\n", try + 1); - return 0; - } - devres_release_group(tps->dev, group); + ret = tipd_init(tps); + if (ret) cancel_delayed_work_sync(&sn->ldcm_work); - if (ret == -EPROBE_DEFER || ret == -ENOMEM) - return ret; - if (try + 1 == SN201202X_INIT_TRIES) - break; - dev_warn(tps->dev, "probe try %u failed: %d; trying again\n", try + 1, ret); - if (try + 1 == SN201202X_INIT_RESET_AFTER) - dev_info(tps->dev, "controller reset before try %u: %d\n", try + 2, - sn201202x_cold_reset(sn)); - msleep(sn201202x_init_wait_ms[try]); - sn201202x_wake(sn); - } - dev_err(tps->dev, "not up after %u tries and a reset (%d): probe deferred\n", - SN201202X_INIT_TRIES, ret); - return -EPROBE_DEFER; + return ret; } static int sn201202x_probe(struct spmi_device *device) @@ -970,11 +936,15 @@ static int sn201202x_probe(struct spmi_device *device) tps->data->irq_handler(tps->irq, tps); sn201202x_idle_sleep_start(tps); - if (sn201202x_sleep_board()) - dev_info(tps->dev, "port controller ready%s\n", sn->polled ? ", polled" : ""); + dev_info(tps->dev, "port controller ready%s\n", sn->polled ? ", polled" : ""); return 0; } +static void sn201202x_shutdown(struct spmi_device *device) +{ + tipd_shutdown(spmi_device_get_drvdata(device)); +} + static void sn201202x_remove(struct spmi_device *device) { struct tps6598x *tps = spmi_device_get_drvdata(device); @@ -1062,7 +1032,7 @@ VISIBLE_IF_KUNIT int __maybe_unused sn201202x_suspend(struct device *dev) tipd_usb_wake(tps, true); reinit_completion(&sn->sleep_completion); err = spmi_command_sleep(sn->sdev); - if (pm_suspend_via_firmware() && sn201202x_sleep_board()) + if (pm_suspend_via_firmware()) dev_info(dev, "SLEEP: %d\n", err); if (err) goto out_resume; @@ -1155,6 +1125,7 @@ static struct spmi_driver sn201202x_driver = { }, .probe = sn201202x_probe, .remove = sn201202x_remove, + .shutdown = sn201202x_shutdown, }; module_spmi_driver(sn201202x_driver); diff --git a/drivers/usb/typec/tipd/tps6598x.h b/drivers/usb/typec/tipd/tps6598x.h index 42692ced9ad3c..f7a31c493e3cf 100644 --- a/drivers/usb/typec/tipd/tps6598x.h +++ b/drivers/usb/typec/tipd/tps6598x.h @@ -352,6 +352,8 @@ struct tipd_data { int (*connect)(struct tps6598x *tps, u32 status); int (*prepare)(struct tps6598x *tps); void (*complete)(struct tps6598x *tps); + /* Stop what sends the controller commands; tipd_shutdown() */ + void (*shutdown)(struct tps6598x *tps); }; struct tps6598x { @@ -363,6 +365,8 @@ struct tps6598x { int irq; u8 i2c_protocol:1; u8 dbma_at_probe:1; /* Apple "DBMa" debug mode was active at probe */ + /* apple,sleep-ssps: SSPS around a firmware-driven sleep, under the sleep's rules */ + u8 sleep_ssps:1; struct gpio_desc *reset; struct typec_port *port; @@ -526,6 +530,14 @@ int tipd_prepare(struct device *dev); void tipd_complete(struct device *dev); int tipd_suspend(struct tps6598x *tps); int tipd_resume(struct tps6598x *tps); +void tipd_shutdown(struct tps6598x *tps); +/* + * The longest limit this driver gives a command (LDCM; SSPS S0 outside the sleep's rules). A + * command still running after that does not end on its own. + */ +#define TPS_CMD_MAX_MS 5000 +int tipd_cmd_wait_idle(struct tps6598x *tps, unsigned int timeout_ms, u32 *busy); +int tipd_cmd_write_busy(struct tps6598x *tps, const char *cmd); /* Internal to tps6598x-core */ int tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len);