@@ -357,6 +357,7 @@ struct apple_pcie {
357357 void __iomem * intr2axi_base ;
358358 void __iomem * oe_fabric_base ;
359359 void __iomem * early_cfg ;
360+ void __iomem * t6050_phy ; /* shared PHY, mapped by a cold train */
360361 struct pci_config_window * cfg ;
361362 struct apple_tunable * rc_tunable ;
362363 struct apple_tunable * fabric_tunable ;
@@ -1657,7 +1658,8 @@ static int apple_pcie_adopt_link(struct apple_pcie_port *port)
16571658 return ret ;
16581659
16591660 snprintf (name , sizeof (name ), "intr2axi%d" , port -> idx );
1660- port -> intr2axi = devm_platform_ioremap_resource_byname (pdev , name );
1661+ if (!port -> intr2axi )
1662+ port -> intr2axi = devm_platform_ioremap_resource_byname (pdev , name );
16611663 if (IS_ERR (port -> intr2axi ))
16621664 return PTR_ERR (port -> intr2axi );
16631665 /*
@@ -1666,8 +1668,9 @@ static int apple_pcie_adopt_link(struct apple_pcie_port *port)
16661668 * controller owns the pad; the line is taken as the bootloader left it,
16671669 * released, and driven only by the reset.
16681670 */
1669- port -> perst = devm_fwnode_gpiod_get (pcie -> dev , of_fwnode_handle (port -> np ), "reset" ,
1670- GPIOD_ASIS , "PERST#" );
1671+ if (!port -> perst )
1672+ port -> perst = devm_fwnode_gpiod_get (pcie -> dev , of_fwnode_handle (port -> np ),
1673+ "reset" , GPIOD_ASIS , "PERST#" );
16711674 if (IS_ERR (port -> perst )) {
16721675 if (PTR_ERR (port -> perst ) != - ENOENT )
16731676 return PTR_ERR (port -> perst );
@@ -1769,15 +1772,30 @@ static void apple_pcie_t6050_rmw(void __iomem *addr, u32 clear, u32 set)
17691772 writel ((readl (addr ) & ~clear ) | set , addr );
17701773}
17711774
1772- static int apple_pcie_t6050_rc_rmw (struct pci_dev * rp , int where , u32 clear , u32 set )
1775+ /* The root port's config space through the ECAM window: there is no pci_dev before enumeration */
1776+ static void __iomem * apple_pcie_t6050_dbi (struct apple_pcie_port * port )
17731777{
1774- u32 value ;
1775- int ret ;
1778+ return port -> pcie -> cfg -> win + ( PCI_DEVFN ( port -> idx , 0 ) << 12 ) ;
1779+ }
17761780
1777- ret = pci_read_config_dword (rp , where , & value );
1778- if (!ret )
1779- ret = pci_write_config_dword (rp , where , (value & ~clear ) | set );
1780- return pcibios_err_to_errno (ret );
1781+ static void apple_pcie_t6050_dbi_rmw (void __iomem * dbi , int where , u32 clear , u32 set )
1782+ {
1783+ writel ((readl (dbi + where ) & ~clear ) | set , dbi + where );
1784+ }
1785+
1786+ static int apple_pcie_t6050_exp_cap (void __iomem * dbi )
1787+ {
1788+ u8 cap = readb (dbi + PCI_CAPABILITY_LIST );
1789+ int i ;
1790+
1791+ for (i = 0 ; cap && i < 48 ; i ++ ) {
1792+ if (cap < 0x40 || (cap & 3 ))
1793+ return 0 ;
1794+ if (readb (dbi + cap + PCI_CAP_LIST_ID ) == PCI_CAP_ID_EXP )
1795+ return cap ;
1796+ cap = readb (dbi + cap + PCI_CAP_LIST_NEXT );
1797+ }
1798+ return 0 ;
17811799}
17821800
17831801static int apple_pcie_t6050_wait (struct apple_pcie_port * port , void __iomem * base ,
@@ -1798,7 +1816,7 @@ static int apple_pcie_t6050_wait(struct apple_pcie_port *port, void __iomem *bas
17981816 * (offset, 4, mask, value): for the port's registers, or with @rp for the root
17991817 * port's config space. A list it did not copy is skipped.
18001818 */
1801- static int apple_pcie_t6050_tune (struct apple_pcie_port * port , struct pci_dev * rp ,
1819+ static int apple_pcie_t6050_tune (struct apple_pcie_port * port , void __iomem * dbi ,
18021820 const char * name )
18031821{
18041822 u32 cells [64 ];
@@ -1814,18 +1832,11 @@ static int apple_pcie_t6050_tune(struct apple_pcie_port *port, struct pci_dev *r
18141832 return ret ;
18151833 for (i = 0 ; i < count ; i += 4 )
18161834 if (cells [i + 1 ] != 4 || (cells [i ] & 3 ) ||
1817- cells [i ] >= (rp ? PCI_CFG_SPACE_EXP_SIZE : SZ_16K ))
1835+ cells [i ] >= (dbi ? PCI_CFG_SPACE_EXP_SIZE : SZ_16K ))
18181836 return - EINVAL ;
18191837
1820- for (i = 0 ; i < count ; i += 4 ) {
1821- if (!rp ) {
1822- apple_pcie_t6050_rmw (port -> base + cells [i ], cells [i + 2 ], cells [i + 3 ]);
1823- continue ;
1824- }
1825- ret = apple_pcie_t6050_rc_rmw (rp , cells [i ], cells [i + 2 ], cells [i + 3 ]);
1826- if (ret )
1827- return ret ;
1828- }
1838+ for (i = 0 ; i < count ; i += 4 )
1839+ apple_pcie_t6050_rmw ((dbi ?: port -> base ) + cells [i ], cells [i + 2 ], cells [i + 3 ]);
18291840 return 0 ;
18301841}
18311842
@@ -1940,65 +1951,50 @@ static int apple_pcie_t6050_port_enable(struct apple_pcie_port *port)
19401951}
19411952
19421953/* Root complex tunables and a Gen3 x1 link, with the read-only registers opened for it */
1943- static int apple_pcie_t6050_port_configure (struct apple_pcie_port * port , struct pci_dev * rp )
1954+ static int apple_pcie_t6050_port_configure (struct apple_pcie_port * port )
19441955{
19451956 static const char * const shadow [] = {
19461957 "apple,pcie-rc-gen3-shadow-tunables" ,
19471958 "apple,pcie-rc-gen4-shadow-tunables" ,
19481959 "apple,pcie-rc-gen5-shadow-tunables" ,
19491960 };
19501961 static const u32 shadow_sel [] = { 0 , BIT (24 ), 0 };
1951- int cap = pci_pcie_cap ( rp );
1952- u32 value ;
1962+ void __iomem * dbi = apple_pcie_t6050_dbi ( port );
1963+ int cap = apple_pcie_t6050_exp_cap ( dbi ) ;
19531964 u16 ctl2 ;
1954- int ret , err , i ;
1965+ int ret , i ;
19551966
19561967 if (!cap )
19571968 return - EIO ;
1958- ret = apple_pcie_t6050_rc_rmw (rp , RC_T6050_MISC_CONTROL_1 , 0 , RC_T6050_DBI_RO_WR_EN );
1959- if (ret )
1960- return ret ;
1961- ret = apple_pcie_t6050_tune (port , rp , "apple,pcie-rc-tunables" );
1962- if (ret )
1963- goto lock ;
1964- for (i = 0 ; i < ARRAY_SIZE (shadow ); i ++ ) {
1965- ret = apple_pcie_t6050_rc_rmw (rp , RC_T6050_GEN3_RELATED ,
1966- RC_T6050_RATE_SHADOW_SEL , shadow_sel [i ]) ?:
1967- apple_pcie_t6050_tune (port , rp , shadow [i ]);
1968- if (ret )
1969- goto lock ;
1969+ apple_pcie_t6050_dbi_rmw (dbi , RC_T6050_MISC_CONTROL_1 , 0 , RC_T6050_DBI_RO_WR_EN );
1970+ ret = apple_pcie_t6050_tune (port , dbi , "apple,pcie-rc-tunables" );
1971+ for (i = 0 ; !ret && i < ARRAY_SIZE (shadow ); i ++ ) {
1972+ apple_pcie_t6050_dbi_rmw (dbi , RC_T6050_GEN3_RELATED , RC_T6050_RATE_SHADOW_SEL ,
1973+ shadow_sel [i ]);
1974+ ret = apple_pcie_t6050_tune (port , dbi , shadow [i ]);
19701975 }
1971- ret = apple_pcie_t6050_rc_rmw (rp , 0x194 , 0 , BIT (0 ));
1972- if (ret )
1973- goto lock ;
1974-
1975- ret = pcibios_err_to_errno (pci_read_config_dword (rp , cap + PCI_EXP_LNKCAP2 , & value ));
1976- if (ret )
1977- goto lock ;
1978- if (!(value & PCI_EXP_LNKCAP2_SLS_8_0GB )) {
1979- ret = - EIO ;
1980- goto lock ;
1976+ if (!ret ) {
1977+ apple_pcie_t6050_dbi_rmw (dbi , 0x194 , 0 , BIT (0 ));
1978+ if (!(readl (dbi + cap + PCI_EXP_LNKCAP2 ) & PCI_EXP_LNKCAP2_SLS_8_0GB ))
1979+ ret = - EIO ;
1980+ }
1981+ if (!ret ) {
1982+ ctl2 = readw (dbi + cap + PCI_EXP_LNKCTL2 );
1983+ writew ((ctl2 & ~PCI_EXP_LNKCTL2_TLS ) | PCI_EXP_LNKCTL2_TLS_8_0GT ,
1984+ dbi + cap + PCI_EXP_LNKCTL2 );
1985+ apple_pcie_t6050_dbi_rmw (dbi , RC_T6050_PORT_LINK_CTRL , RC_T6050_LINK_MODE ,
1986+ FIELD_PREP (RC_T6050_LINK_MODE , 1 ));
1987+ apple_pcie_t6050_dbi_rmw (dbi , RC_T6050_LINK_WIDTH_SPEED_CTRL , RC_T6050_LINK_WIDTH ,
1988+ FIELD_PREP (RC_T6050_LINK_WIDTH , 1 ));
1989+ apple_pcie_t6050_dbi_rmw (dbi , cap + PCI_EXP_LNKCAP , PCI_EXP_LNKCAP_MLW ,
1990+ FIELD_PREP (PCI_EXP_LNKCAP_MLW , 1 ));
19811991 }
1982- ret = pcibios_err_to_errno (pci_read_config_word (rp , cap + PCI_EXP_LNKCTL2 , & ctl2 )) ?:
1983- pcibios_err_to_errno (pci_write_config_word (rp , cap + PCI_EXP_LNKCTL2 ,
1984- (ctl2 & ~PCI_EXP_LNKCTL2_TLS ) |
1985- PCI_EXP_LNKCTL2_TLS_8_0GT )) ?:
1986- apple_pcie_t6050_rc_rmw (rp , RC_T6050_PORT_LINK_CTRL , RC_T6050_LINK_MODE ,
1987- FIELD_PREP (RC_T6050_LINK_MODE , 1 )) ?:
1988- apple_pcie_t6050_rc_rmw (rp , RC_T6050_LINK_WIDTH_SPEED_CTRL , RC_T6050_LINK_WIDTH ,
1989- FIELD_PREP (RC_T6050_LINK_WIDTH , 1 )) ?:
1990- apple_pcie_t6050_rc_rmw (rp , cap + PCI_EXP_LNKCAP , PCI_EXP_LNKCAP_MLW ,
1991- FIELD_PREP (PCI_EXP_LNKCAP_MLW , 1 ));
1992- lock :
1993- err = apple_pcie_t6050_rc_rmw (rp , RC_T6050_MISC_CONTROL_1 , RC_T6050_DBI_RO_WR_EN , 0 );
1994- ret = ret ?: err ;
1992+ apple_pcie_t6050_dbi_rmw (dbi , RC_T6050_MISC_CONTROL_1 , RC_T6050_DBI_RO_WR_EN , 0 );
19951993 if (ret )
19961994 return ret ;
19971995
1998- ret = apple_pcie_t6050_rc_rmw (rp , cap + PCI_EXP_LNKCTL , 0 , PCI_EXP_LNKCTL_LBMIE ) ?:
1999- apple_pcie_t6050_rc_rmw (rp , RC_T6050_LINK_WIDTH_SPEED_CTRL , 0 , RC_T6050_SPEED_CHANGE );
2000- if (ret )
2001- return ret ;
1996+ apple_pcie_t6050_dbi_rmw (dbi , cap + PCI_EXP_LNKCTL , 0 , PCI_EXP_LNKCTL_LBMIE );
1997+ apple_pcie_t6050_dbi_rmw (dbi , RC_T6050_LINK_WIDTH_SPEED_CTRL , 0 , RC_T6050_SPEED_CHANGE );
20021998 writel (PCIEC_INTR2AXI_ENABLE , port -> intr2axi + PCIEC_INTR2AXI_CTRL );
20031999 return 0 ;
20042000}
@@ -2008,7 +2004,7 @@ static int apple_pcie_t6050_port_restart(struct apple_pcie_port *port, struct pc
20082004{
20092005 return apple_pcie_t6050_port_disable (port , rp ) ?:
20102006 apple_pcie_t6050_port_enable (port ) ?:
2011- apple_pcie_t6050_port_configure (port , rp );
2007+ apple_pcie_t6050_port_configure (port );
20122008}
20132009
20142010/* Lane clocks, PERST# released, then the LTSSM to link up */
@@ -2048,6 +2044,151 @@ static int apple_pcie_t6050_port_train(struct apple_pcie_port *port)
20482044 return 0 ;
20492045}
20502046
2047+ /*
2048+ * A T6050 port the boot loader left untrained (the J714c's N1 on every boot): the shared
2049+ * PCIe PHY clocked and out of reset, the RC running, the endpoint powered (pwren-gpios)
2050+ * with PERST# held, then the port enable, configure and train sequences the root-port
2051+ * reset uses, and the bus numbers a boot loader leaves. The adopt checks then run as for
2052+ * a port trained before Linux. Order and values are m1n1's controller bring-up and the
2053+ * kernel's own T6050 port sequence.
2054+ */
2055+ #define T6050_PHY_CTRL 0x0
2056+ #define T6050_PHY_CTRL_CLK0REQ BIT(0)
2057+ #define T6050_PHY_CTRL_CLK1REQ BIT(1)
2058+ #define T6050_PHY_CTRL_CLK0ACK BIT(2)
2059+ #define T6050_PHY_CTRL_CLK1ACK BIT(3)
2060+ #define T6050_PHY_CTRL_RESET_T8132 BIT(4)
2061+ #define T6050_PHY_CTRL_RESET_T8103 BIT(7)
2062+ #define T6050_PHY_CTRL_LANES GENMASK(9, 8)
2063+ #define T6050_PHY_CFG 0x4
2064+ #define T6050_PHY_STAT 0x8
2065+ #define T6050_PHYCMN_CLK 0x0
2066+ #define T6050_PHYCMN_CLK_MODE GENMASK(1, 0)
2067+ #define CORE_RC_T6050_PRE_RUN 0x54 /* m1n1 writes 0x140 here before RUN */
2068+
2069+ static int apple_pcie_t6050_phy_wait (struct apple_pcie * pcie , u32 mask , u32 value )
2070+ {
2071+ u32 v ;
2072+ int ret ;
2073+
2074+ ret = readl_poll_timeout (pcie -> t6050_phy + T6050_PHY_CTRL , v , (v & mask ) == value ,
2075+ 10 , 50000 );
2076+ if (ret )
2077+ dev_err (pcie -> dev , "shared PHY: %#x, waiting for %#x/%#x\n" , v , value , mask );
2078+ return ret ;
2079+ }
2080+
2081+ static int apple_pcie_t6050_shared_phy_on (struct apple_pcie * pcie )
2082+ {
2083+ struct platform_device * pdev = to_platform_device (pcie -> dev );
2084+ const u32 up = T6050_PHY_CTRL_CLK0ACK | T6050_PHY_CTRL_CLK1ACK | T6050_PHY_CTRL_LANES ;
2085+ void __iomem * phy , * cmn ;
2086+ u32 v ;
2087+ int ret ;
2088+
2089+ if (!pcie -> t6050_phy ) {
2090+ phy = devm_platform_ioremap_resource_byname (pdev , "phy-shared" );
2091+ if (IS_ERR (phy ))
2092+ return PTR_ERR (phy );
2093+ pcie -> t6050_phy = phy ;
2094+ }
2095+ phy = pcie -> t6050_phy ;
2096+ if ((readl (phy + T6050_PHY_CTRL ) & (up | T6050_PHY_CTRL_RESET_T8132 |
2097+ T6050_PHY_CTRL_RESET_T8103 )) == up )
2098+ return 0 ;
2099+ cmn = devm_platform_ioremap_resource_byname (pdev , "phy-common" );
2100+ if (IS_ERR (cmn ))
2101+ return PTR_ERR (cmn );
2102+
2103+ apple_pcie_t6050_rmw (phy + T6050_PHY_CTRL , 0 , T6050_PHY_CTRL_CLK0REQ );
2104+ ret = apple_pcie_t6050_phy_wait (pcie , T6050_PHY_CTRL_CLK0ACK , T6050_PHY_CTRL_CLK0ACK );
2105+ if (ret )
2106+ return ret ;
2107+ apple_pcie_t6050_rmw (phy + T6050_PHY_CTRL , 0 , T6050_PHY_CTRL_CLK1REQ );
2108+ ret = apple_pcie_t6050_phy_wait (pcie , T6050_PHY_CTRL_CLK1ACK , T6050_PHY_CTRL_CLK1ACK );
2109+ if (ret )
2110+ return ret ;
2111+ apple_pcie_t6050_rmw (phy + T6050_PHY_CTRL , T6050_PHY_CTRL_RESET_T8132 , 0 );
2112+ ret = apple_pcie_t6050_phy_wait (pcie , T6050_PHY_CTRL_RESET_T8132 , 0 );
2113+ if (ret )
2114+ return ret ;
2115+ apple_pcie_t6050_rmw (phy + T6050_PHY_CTRL , T6050_PHY_CTRL_RESET_T8103 , 0 );
2116+ ret = apple_pcie_t6050_phy_wait (pcie , T6050_PHY_CTRL_RESET_T8103 , 0 );
2117+ if (ret )
2118+ return ret ;
2119+ apple_pcie_t6050_rmw (phy + T6050_PHY_CFG , 0 , BIT (0 ));
2120+ apple_pcie_t6050_rmw (phy + T6050_PHY_CFG , 0 , BIT (4 ));
2121+ apple_pcie_t6050_rmw (cmn + T6050_PHYCMN_CLK , T6050_PHYCMN_CLK_MODE ,
2122+ FIELD_PREP (T6050_PHYCMN_CLK_MODE , 1 ));
2123+ if (readl_poll_timeout (phy + T6050_PHY_STAT , v , v & BIT (0 ), 10 , 250000 ))
2124+ dev_info (pcie -> dev , "shared PHY: clock status %#x\n" , v );
2125+ apple_pcie_t6050_rmw (phy + T6050_PHY_CTRL , 0 , T6050_PHY_CTRL_LANES );
2126+ return 0 ;
2127+ }
2128+
2129+ static int apple_pcie_t6050_cold_train (struct apple_pcie_port * port )
2130+ {
2131+ struct apple_pcie * pcie = port -> pcie ;
2132+ struct platform_device * pdev = to_platform_device (pcie -> dev );
2133+ struct gpio_desc * pwren ;
2134+ void __iomem * dbi ;
2135+ char name [16 ];
2136+ u32 v , bus ;
2137+ int ret ;
2138+
2139+ bus = pcie -> cfg -> busr .start + 1 + port -> idx ;
2140+ if (bus > pcie -> cfg -> busr .end )
2141+ return - EINVAL ;
2142+ ret = apple_pcie_t6050_shared_phy_on (pcie );
2143+ if (ret )
2144+ return ret ;
2145+ if (!(readl (pcie -> base + CORE_RC_CTL ) & CORE_RC_CTL_RUN )) {
2146+ writel (0x140 , pcie -> base + CORE_RC_T6050_PRE_RUN );
2147+ writel (CORE_RC_CTL_RUN , pcie -> base + CORE_RC_CTL );
2148+ }
2149+ ret = readl_poll_timeout (pcie -> base + CORE_RC_STAT , v , v & CORE_RC_STAT_READY , 10 , 250000 );
2150+ if (ret )
2151+ return ret ;
2152+
2153+ snprintf (name , sizeof (name ), "intr2axi%d" , port -> idx );
2154+ if (!port -> intr2axi )
2155+ port -> intr2axi = devm_platform_ioremap_resource_byname (pdev , name );
2156+ if (IS_ERR (port -> intr2axi ))
2157+ return PTR_ERR (port -> intr2axi );
2158+ if (!port -> perst )
2159+ port -> perst = devm_fwnode_gpiod_get (pcie -> dev , of_fwnode_handle (port -> np ),
2160+ "reset" , GPIOD_ASIS , "PERST#" );
2161+ if (IS_ERR (port -> perst ))
2162+ return PTR_ERR (port -> perst );
2163+ ret = apple_pcie_t6050_perst (port , true);
2164+ if (ret )
2165+ return ret ;
2166+
2167+ /* The endpoint held, then powered: the boot loader leaves it off */
2168+ pwren = devm_fwnode_gpiod_get (pcie -> dev , of_fwnode_handle (port -> np ), "pwren" ,
2169+ GPIOD_OUT_LOW , "PWREN" );
2170+ if (IS_ERR (pwren ))
2171+ return PTR_ERR (pwren );
2172+ msleep (50 );
2173+ ret = gpiod_set_value_cansleep (pwren , 1 );
2174+ if (ret )
2175+ return ret ;
2176+ msleep (150 );
2177+
2178+ ret = apple_pcie_t6050_port_enable (port ) ?:
2179+ apple_pcie_t6050_port_configure (port ) ?:
2180+ apple_pcie_t6050_port_train (port );
2181+ if (ret )
2182+ return ret ;
2183+
2184+ /* Primary bus 0, the port's secondary and subordinate bus, as a boot loader leaves them */
2185+ dbi = apple_pcie_t6050_dbi (port );
2186+ v = readl (dbi + PCI_PRIMARY_BUS ) & 0xff000000 ;
2187+ writel (v | (bus << 8 ) | (bus << 16 ), dbi + PCI_PRIMARY_BUS );
2188+ dev_info (pcie -> dev , "port %d: trained by Linux, the boot loader left it down\n" , port -> idx );
2189+ return 0 ;
2190+ }
2191+
20512192/*
20522193 * Root-port reset of an adopted T6050 port, for an endpoint that must come back on a
20532194 * retrained link (the N1 after its ROM boot): PME_Turn_Off to L2, PERST# asserted, the
@@ -2362,6 +2503,12 @@ static int apple_pcie_setup_port(struct apple_pcie *pcie,
23622503 }
23632504 } else if (pcie -> hw -> adopt_link ) {
23642505 ret = apple_pcie_adopt_link (port );
2506+ /* A port left down that names its endpoint's power line is trained here */
2507+ if (ret == - ENOLINK && of_property_present (np , "pwren-gpios" )) {
2508+ ret = apple_pcie_t6050_cold_train (port );
2509+ if (!ret )
2510+ ret = apple_pcie_adopt_link (port );
2511+ }
23652512 if (ret )
23662513 goto err_teardown ;
23672514 } else {
@@ -3424,8 +3571,7 @@ static int apple_pcie_probe(struct platform_device *pdev)
34243571 /* A root port 00:<n>.0 on bus 0, n a GP port (0-3) */
34253572 if (of_property_read_u32 (child , "reg" , & reg ) || reg & ~0xf800 ||
34263573 (reg >> 11 ) > 3 ||
3427- !of_property_read_bool (child , "apple,link-trained-by-bootloader" ) ||
3428- of_property_present (child , "pwren-gpios" ))
3574+ !of_property_read_bool (child , "apple,link-trained-by-bootloader" ))
34293575 return - EINVAL ;
34303576 /*
34313577 * A port whose endpoint needs a root-port reset names its PERST#
0 commit comments