Skip to content
Merged
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
143 changes: 93 additions & 50 deletions drivers/pci/controller/pcie-apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,8 @@ struct apple_pcie {
u32 msi_vector_base;
};

#define MAX_AUX_PERST 3

struct apple_pcie_port {
raw_spinlock_t lock;
raw_spinlock_t irq_lock;
Expand All @@ -762,6 +764,8 @@ struct apple_pcie_port {
u32 saved_intmask;
int sid_map_sz;
struct gpio_desc *perst_gpio;
struct gpio_desc *aux_reset_gpios[MAX_AUX_PERST];
u32 num_aux_resets;
struct gpio_desc *pwren_gpio;
int idx;
/* Conventional port: link up when the sleep began, and found down after it */
Expand Down Expand Up @@ -3110,78 +3114,105 @@ static int apple_pcie_link_pwren(struct apple_pcie *pcie, struct gpio_desc *pwre
return devm_add_action_or_reset(pcie->dev, apple_pcie_unlink_pwren, link);
}

static int apple_pcie_setup_link(struct apple_pcie *pcie,
struct apple_pcie_port *port,
struct device_node *np)
/*
* Acquire the port's PERST# and PWREN GPIOs at probe time and establish the
* PWREN supplier device link before any suspend cycle. Bootloader-trained
* ports pass GPIOD_ASIS so probe never toggles PERST# on a live link; cold
* ports pass GPIOD_OUT_HIGH to assert PERST# before clock setup.
*/
static int apple_pcie_get_port_gpios(struct apple_pcie *pcie,
struct apple_pcie_port *port,
struct device_node *np,
enum gpiod_flags reset_flags)
{
#define MAX_AUX_PERST 3
struct gpio_desc *aux_reset[MAX_AUX_PERST] = { NULL };
struct gpio_desc *reset, *pwren;
u32 num_aux_resets = 0;
struct gpio_desc *reset, *pwren = NULL;
u32 stat;
int ret;

/* Bringing the link up again, after a deep sleep. */
if (port->perst_gpio) {
reset = port->perst_gpio;
pwren = port->pwren_gpio;
goto have_gpios;
}

/*
* Assert PERST# and configure the pin as output.
* On cold ports, assert PERST# and configure the pin as output.
* The Aquantia AQC113 10GB nic used desktop macs is sensitive to
* deasserting it without prior clock setup.
* Observed on M1 Max/Ultra Mac Studios under m1n1's hypervisor.
*/
reset = devm_fwnode_gpiod_get(pcie->dev, of_fwnode_handle(np), "reset",
GPIOD_OUT_HIGH, "PERST#");
if (IS_ERR(reset))
return PTR_ERR(reset);
// HACK: use additional "reset-gpios" until pci-pwrctrl gains PERST# support.
for (u32 idx = 0; idx < MAX_AUX_PERST; idx++) {
aux_reset[idx] = devm_fwnode_gpiod_get_index(pcie->dev,
of_fwnode_handle(np),
"reset", idx + 1,
GPIOD_OUT_HIGH,
"PERST#");
if (IS_ERR(aux_reset[idx])) {
if (PTR_ERR(aux_reset[idx]) == -ENOENT)
break;
else
return PTR_ERR(aux_reset[idx]);
reset_flags, "PERST#");
if (IS_ERR(reset)) {
if (reset_flags == GPIOD_ASIS && PTR_ERR(reset) == -ENOENT)
reset = NULL;
else
return PTR_ERR(reset);
}

if (reset) {
/* HACK: use additional "reset-gpios" until pci-pwrctrl gains PERST# support. */
for (u32 idx = 0; idx < MAX_AUX_PERST; idx++) {
struct gpio_desc *aux;

aux = devm_fwnode_gpiod_get_index(pcie->dev,
of_fwnode_handle(np),
"reset", idx + 1,
reset_flags,
"PERST#");
if (IS_ERR(aux)) {
if (PTR_ERR(aux) == -ENOENT)
break;
return PTR_ERR(aux);
}
port->aux_reset_gpios[idx] = aux;
num_aux_resets++;
}
num_aux_resets++;
if (reset_flags != GPIOD_ASIS)
dev_info(pcie->dev, "Using %u auxiliary PERST#\n",
num_aux_resets);
port->perst_gpio = reset;
port->num_aux_resets = num_aux_resets;
if (!num_aux_resets)
port->perst = reset;
}
dev_info(pcie->dev, "Using %u auxiliary PERST#\n", num_aux_resets);
if (!num_aux_resets)
port->perst = reset;

pwren = devm_fwnode_gpiod_get(pcie->dev, of_fwnode_handle(np), "pwren",
GPIOD_ASIS, "PWREN");
GPIOD_ASIS, "PWREN");
if (IS_ERR(pwren)) {
if (PTR_ERR(pwren) == -ENOENT)
pwren = NULL;
else
return PTR_ERR(pwren);
}
if (!num_aux_resets) {
port->perst_gpio = reset;
port->pwren_gpio = pwren;
}

port->pwren_gpio = pwren;
if (pwren && pcie->hw->s2r_noirq) {
ret = apple_pcie_link_pwren(pcie, pwren);
if (ret)
return ret;
}

have_gpios:
return 0;
}

static int apple_pcie_setup_link(struct apple_pcie *pcie,
struct apple_pcie_port *port,
struct device_node *np)
{
struct gpio_desc *reset = port->perst_gpio;
struct gpio_desc *pwren = port->pwren_gpio;
u32 stat;
int ret;

if (!reset)
return -ENOENT;

rmw_set(PORT_APPCLK_EN, port->base + PORT_APPCLK);

/* Assert PERST# before setting up the clock */
gpiod_set_value_cansleep(reset, 1);
for (u32 idx = 0; idx < num_aux_resets; idx++)
gpiod_set_value_cansleep(aux_reset[idx], 1);
ret = gpiod_direction_output(reset, 1);
if (ret)
return ret;
for (u32 idx = 0; idx < port->num_aux_resets; idx++) {
ret = gpiod_direction_output(port->aux_reset_gpios[idx], 1);
if (ret)
return ret;
}

/* Power-cycle devices which cannot inherit their firmware state. */
if (pcie->hw->pwren_off_ms) {
Expand Down Expand Up @@ -3217,8 +3248,11 @@ static int apple_pcie_setup_link(struct apple_pcie *pcie,
ret = gpiod_set_value_cansleep(reset, 0);
if (ret)
return ret;
for (u32 idx = 0; idx < num_aux_resets; idx++)
gpiod_set_value_cansleep(aux_reset[idx], 0);
for (u32 idx = 0; idx < port->num_aux_resets; idx++) {
ret = gpiod_set_value_cansleep(port->aux_reset_gpios[idx], 0);
if (ret)
return ret;
}

if (pcie->hw->pipe_rst) {
ret = readl_relaxed_poll_timeout(port->base + PORT_T8152_PIPE_RST_STS,
Expand Down Expand Up @@ -3270,7 +3304,7 @@ static int apple_pcie_port_reset_endpoint(struct apple_pcie *pcie,

apple_pcie_port_rmw_clear(port, PORT_LTSSMCTL_START, PORT_LTSSMCTL);
rmw_clear(PORT_PERST_OFF, port->base + pcie->hw->port_perst);
ret = gpiod_set_value_cansleep(port->perst, 1);
ret = gpiod_direction_output(port->perst, 1);
if (ret)
return ret;
usleep_range(100, 200);
Expand Down Expand Up @@ -3500,9 +3534,18 @@ static int apple_pcie_setup_port(struct apple_pcie *pcie,
if (ret)
goto err_teardown;
}
ret = apple_pcie_get_port_gpios(pcie, port, np,
GPIOD_OUT_HIGH);
if (ret)
goto err_teardown;
ret = apple_pcie_setup_link(pcie, port, np);
if (ret)
goto err_teardown;
} else {
ret = apple_pcie_get_port_gpios(pcie, port, np,
GPIOD_ASIS);
if (ret)
goto err_teardown;
}
}

Expand Down Expand Up @@ -8303,7 +8346,7 @@ static int apple_pcie_neo_rail_cycle(struct apple_pcie *pcie, struct apple_pcie_
struct pci_dev *fn;
int i, ret;

ret = gpiod_set_value_cansleep(port->perst_gpio, 1);
ret = gpiod_direction_output(port->perst_gpio, 1);
if (!ret)
ret = gpiod_set_value_cansleep(port->pwren_gpio, 0);
if (ret)
Expand Down Expand Up @@ -8439,7 +8482,7 @@ static int apple_pcie_neo_resume_noirq(struct apple_pcie *pcie)

list_for_each_entry(port, &pcie->ports, entry) {
/* The pin controller just put it back as it was: released, with no clock yet. */
ret = gpiod_set_value_cansleep(port->perst_gpio, 1);
ret = gpiod_direction_output(port->perst_gpio, 1);
if (!ret)
ret = apple_pcie_neo_cold_init(pcie, port);
if (ret) {
Expand Down Expand Up @@ -10138,7 +10181,7 @@ static void apple_pcie_hib_ports_phased(struct apple_pcie *pcie)
continue;
rmw_set(PORT_APPCLK_EN, port->base + PORT_APPCLK);
/* Assert PERST# before setting up the clock */
ret = gpiod_set_value_cansleep(port->perst_gpio, 1);
ret = gpiod_direction_output(port->perst_gpio, 1);
if (!ret && port->pwren_gpio) {
ret = gpiod_set_value_cansleep(port->pwren_gpio, 1);
powered = true;
Expand Down Expand Up @@ -10217,7 +10260,7 @@ static bool apple_pcie_hib_phased_ok(struct apple_pcie *pcie)
if (pcie->hw->pwren_off_ms || pcie->hw->pipe_rst || pcie->hw->retain_clocks)
return false;
list_for_each_entry(port, &pcie->ports, entry)
if (port->hib_cold && !port->perst_gpio)
if (port->hib_cold && (!port->perst_gpio || port->num_aux_resets))
return false;
return true;
}
Expand Down
Loading
Loading