pcie_apple.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host bridge driver for Apple system-on-chips.
  4. *
  5. * The HW is ECAM compliant.
  6. *
  7. * Initialization requires enabling power and clocks, along with a
  8. * number of register pokes.
  9. *
  10. * Copyright (C) 2021 Alyssa Rosenzweig <alyssa@rosenzweig.io>
  11. * Copyright (C) 2021 Google LLC
  12. * Copyright (C) 2021 Corellium LLC
  13. * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
  14. *
  15. * Author: Alyssa Rosenzweig <alyssa@rosenzweig.io>
  16. * Author: Marc Zyngier <maz@kernel.org>
  17. */
  18. #include <common.h>
  19. #include <dm.h>
  20. #include <dm/device_compat.h>
  21. #include <dm/devres.h>
  22. #include <mapmem.h>
  23. #include <pci.h>
  24. #include <asm/io.h>
  25. #include <asm-generic/gpio.h>
  26. #include <linux/delay.h>
  27. #include <linux/iopoll.h>
  28. #define CORE_RC_PHYIF_CTL 0x00024
  29. #define CORE_RC_PHYIF_CTL_RUN BIT(0)
  30. #define CORE_RC_PHYIF_STAT 0x00028
  31. #define CORE_RC_PHYIF_STAT_REFCLK BIT(4)
  32. #define CORE_RC_CTL 0x00050
  33. #define CORE_RC_CTL_RUN BIT(0)
  34. #define CORE_RC_STAT 0x00058
  35. #define CORE_RC_STAT_READY BIT(0)
  36. #define CORE_FABRIC_STAT 0x04000
  37. #define CORE_FABRIC_STAT_MASK 0x001F001F
  38. #define CORE_PHY_DEFAULT_BASE(port) (0x84000 + 0x4000 * (port))
  39. #define PHY_LANE_CFG 0x00000
  40. #define PHY_LANE_CFG_REFCLK0REQ BIT(0)
  41. #define PHY_LANE_CFG_REFCLK1REQ BIT(1)
  42. #define PHY_LANE_CFG_REFCLK0ACK BIT(2)
  43. #define PHY_LANE_CFG_REFCLK1ACK BIT(3)
  44. #define PHY_LANE_CFG_REFCLKEN (BIT(9) | BIT(10))
  45. #define PHY_LANE_CFG_REFCLKCGEN (BIT(30) | BIT(31))
  46. #define PHY_LANE_CTL 0x00004
  47. #define PHY_LANE_CTL_CFGACC BIT(15)
  48. #define PORT_LTSSMCTL 0x00080
  49. #define PORT_LTSSMCTL_START BIT(0)
  50. #define PORT_INTSTAT 0x00100
  51. #define PORT_INT_TUNNEL_ERR 31
  52. #define PORT_INT_CPL_TIMEOUT 23
  53. #define PORT_INT_RID2SID_MAPERR 22
  54. #define PORT_INT_CPL_ABORT 21
  55. #define PORT_INT_MSI_BAD_DATA 19
  56. #define PORT_INT_MSI_ERR 18
  57. #define PORT_INT_REQADDR_GT32 17
  58. #define PORT_INT_AF_TIMEOUT 15
  59. #define PORT_INT_LINK_DOWN 14
  60. #define PORT_INT_LINK_UP 12
  61. #define PORT_INT_LINK_BWMGMT 11
  62. #define PORT_INT_AER_MASK (15 << 4)
  63. #define PORT_INT_PORT_ERR 4
  64. #define PORT_INT_INTx(i) i
  65. #define PORT_INT_INTx_MASK 15
  66. #define PORT_INTMSK 0x00104
  67. #define PORT_INTMSKSET 0x00108
  68. #define PORT_INTMSKCLR 0x0010c
  69. #define PORT_MSICFG 0x00124
  70. #define PORT_MSICFG_EN BIT(0)
  71. #define PORT_MSICFG_L2MSINUM_SHIFT 4
  72. #define PORT_MSIBASE 0x00128
  73. #define PORT_MSIBASE_1_SHIFT 16
  74. #define PORT_MSIADDR 0x00168
  75. #define PORT_LINKSTS 0x00208
  76. #define PORT_LINKSTS_UP BIT(0)
  77. #define PORT_LINKSTS_BUSY BIT(2)
  78. #define PORT_LINKCMDSTS 0x00210
  79. #define PORT_OUTS_NPREQS 0x00284
  80. #define PORT_OUTS_NPREQS_REQ BIT(24)
  81. #define PORT_OUTS_NPREQS_CPL BIT(16)
  82. #define PORT_RXWR_FIFO 0x00288
  83. #define PORT_RXWR_FIFO_HDR GENMASK(15, 10)
  84. #define PORT_RXWR_FIFO_DATA GENMASK(9, 0)
  85. #define PORT_RXRD_FIFO 0x0028C
  86. #define PORT_RXRD_FIFO_REQ GENMASK(6, 0)
  87. #define PORT_OUTS_CPLS 0x00290
  88. #define PORT_OUTS_CPLS_SHRD GENMASK(14, 8)
  89. #define PORT_OUTS_CPLS_WAIT GENMASK(6, 0)
  90. #define PORT_APPCLK 0x00800
  91. #define PORT_APPCLK_EN BIT(0)
  92. #define PORT_APPCLK_CGDIS BIT(8)
  93. #define PORT_STATUS 0x00804
  94. #define PORT_STATUS_READY BIT(0)
  95. #define PORT_REFCLK 0x00810
  96. #define PORT_REFCLK_EN BIT(0)
  97. #define PORT_REFCLK_CGDIS BIT(8)
  98. #define PORT_PERST 0x00814
  99. #define PORT_PERST_OFF BIT(0)
  100. #define PORT_RID2SID(i16) (0x00828 + 4 * (i16))
  101. #define PORT_RID2SID_VALID BIT(31)
  102. #define PORT_RID2SID_SID_SHIFT 16
  103. #define PORT_RID2SID_BUS_SHIFT 8
  104. #define PORT_RID2SID_DEV_SHIFT 3
  105. #define PORT_RID2SID_FUNC_SHIFT 0
  106. #define PORT_OUTS_PREQS_HDR 0x00980
  107. #define PORT_OUTS_PREQS_HDR_MASK GENMASK(9, 0)
  108. #define PORT_OUTS_PREQS_DATA 0x00984
  109. #define PORT_OUTS_PREQS_DATA_MASK GENMASK(15, 0)
  110. #define PORT_TUNCTRL 0x00988
  111. #define PORT_TUNCTRL_PERST_ON BIT(0)
  112. #define PORT_TUNCTRL_PERST_ACK_REQ BIT(1)
  113. #define PORT_TUNSTAT 0x0098c
  114. #define PORT_TUNSTAT_PERST_ON BIT(0)
  115. #define PORT_TUNSTAT_PERST_ACK_PEND BIT(1)
  116. #define PORT_PREFMEM_ENABLE 0x00994
  117. struct reg_info {
  118. u32 phy_lane_ctl;
  119. u32 port_refclk;
  120. u32 port_perst;
  121. };
  122. const struct reg_info t8103_hw = {
  123. .phy_lane_ctl = PHY_LANE_CTL,
  124. .port_refclk = PORT_REFCLK,
  125. .port_perst = PORT_PERST,
  126. };
  127. #define PORT_T602X_PERST 0x082c
  128. const struct reg_info t602x_hw = {
  129. .phy_lane_ctl = 0,
  130. .port_refclk = 0,
  131. .port_perst = PORT_T602X_PERST,
  132. };
  133. struct apple_pcie_priv {
  134. struct udevice *dev;
  135. void __iomem *base;
  136. void __iomem *cfg_base;
  137. struct list_head ports;
  138. const struct reg_info *hw;
  139. };
  140. struct apple_pcie_port {
  141. struct apple_pcie_priv *pcie;
  142. struct gpio_desc reset;
  143. ofnode np;
  144. void __iomem *base;
  145. void __iomem *phy;
  146. struct list_head entry;
  147. int idx;
  148. };
  149. static void rmw_set(u32 set, void __iomem *addr)
  150. {
  151. writel_relaxed(readl_relaxed(addr) | set, addr);
  152. }
  153. static void rmw_clear(u32 clr, void __iomem *addr)
  154. {
  155. writel_relaxed(readl_relaxed(addr) & ~clr, addr);
  156. }
  157. static int apple_pcie_config_address(const struct udevice *bus,
  158. pci_dev_t bdf, uint offset,
  159. void **paddress)
  160. {
  161. struct apple_pcie_priv *pcie = dev_get_priv(bus);
  162. void *addr;
  163. addr = pcie->cfg_base;
  164. addr += PCIE_ECAM_OFFSET(PCI_BUS(bdf), PCI_DEV(bdf),
  165. PCI_FUNC(bdf), offset);
  166. *paddress = addr;
  167. return 0;
  168. }
  169. static int apple_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
  170. uint offset, ulong *valuep,
  171. enum pci_size_t size)
  172. {
  173. int ret;
  174. ret = pci_generic_mmap_read_config(bus, apple_pcie_config_address,
  175. bdf, offset, valuep, size);
  176. return ret;
  177. }
  178. static int apple_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
  179. uint offset, ulong value,
  180. enum pci_size_t size)
  181. {
  182. return pci_generic_mmap_write_config(bus, apple_pcie_config_address,
  183. bdf, offset, value, size);
  184. }
  185. static const struct dm_pci_ops apple_pcie_ops = {
  186. .read_config = apple_pcie_read_config,
  187. .write_config = apple_pcie_write_config,
  188. };
  189. static int apple_pcie_setup_refclk(struct apple_pcie_priv *pcie,
  190. struct apple_pcie_port *port)
  191. {
  192. u32 stat;
  193. int res;
  194. if (pcie->hw->phy_lane_ctl)
  195. rmw_set(PHY_LANE_CTL_CFGACC, port->phy + pcie->hw->phy_lane_ctl);
  196. rmw_set(PHY_LANE_CFG_REFCLK0REQ, port->phy + PHY_LANE_CFG);
  197. res = readl_poll_sleep_timeout(port->phy + PHY_LANE_CFG,
  198. stat, stat & PHY_LANE_CFG_REFCLK0ACK,
  199. 100, 50000);
  200. if (res < 0)
  201. return res;
  202. rmw_set(PHY_LANE_CFG_REFCLK1REQ, port->phy + PHY_LANE_CFG);
  203. res = readl_poll_sleep_timeout(port->phy + PHY_LANE_CFG,
  204. stat, stat & PHY_LANE_CFG_REFCLK1ACK,
  205. 100, 50000);
  206. if (res < 0)
  207. return res;
  208. if (pcie->hw->phy_lane_ctl)
  209. rmw_clear(PHY_LANE_CTL_CFGACC, port->phy + pcie->hw->phy_lane_ctl);
  210. rmw_set(PHY_LANE_CFG_REFCLKEN, port->phy + PHY_LANE_CFG);
  211. if (pcie->hw->port_refclk)
  212. rmw_set(PORT_REFCLK_EN, port->base + pcie->hw->port_refclk);
  213. return 0;
  214. }
  215. static int apple_pcie_setup_port(struct apple_pcie_priv *pcie, ofnode np)
  216. {
  217. struct apple_pcie_port *port;
  218. struct gpio_desc reset;
  219. fdt_addr_t addr;
  220. u32 stat, idx;
  221. int ret;
  222. char name[16];
  223. ret = gpio_request_by_name_nodev(np, "reset-gpios", 0, &reset, 0);
  224. if (ret)
  225. return ret;
  226. port = devm_kzalloc(pcie->dev, sizeof(*port), GFP_KERNEL);
  227. if (!port)
  228. return -ENOMEM;
  229. ret = ofnode_read_u32_index(np, "reg", 0, &idx);
  230. if (ret)
  231. return ret;
  232. /* Use the first reg entry to work out the port index */
  233. port->idx = idx >> 11;
  234. port->pcie = pcie;
  235. port->reset = reset;
  236. port->np = np;
  237. snprintf(name, sizeof(name), "port%d", port->idx);
  238. addr = dev_read_addr_name(pcie->dev, name);
  239. if (addr == FDT_ADDR_T_NONE)
  240. addr = dev_read_addr_index(pcie->dev, port->idx + 2);
  241. if (addr == FDT_ADDR_T_NONE)
  242. return -EINVAL;
  243. port->base = map_sysmem(addr, 0);
  244. snprintf(name, sizeof(name), "phy%d", port->idx);
  245. addr = dev_read_addr_name(pcie->dev, name);
  246. if (addr == FDT_ADDR_T_NONE)
  247. port->phy = pcie->base + CORE_PHY_DEFAULT_BASE(port->idx);
  248. else
  249. port->phy = map_sysmem(addr, 0);
  250. rmw_set(PORT_APPCLK_EN, port->base + PORT_APPCLK);
  251. /* Assert PERST# before setting up the clock */
  252. dm_gpio_set_value(&reset, 1);
  253. ret = apple_pcie_setup_refclk(pcie, port);
  254. if (ret < 0)
  255. return ret;
  256. /* The minimal Tperst-clk value is 100us (PCIe CEM r5.0, 2.9.2) */
  257. udelay(100);
  258. /* Deassert PERST# */
  259. rmw_set(PORT_PERST_OFF, port->base + pcie->hw->port_perst);
  260. dm_gpio_set_value(&reset, 0);
  261. /* Wait for 100ms after PERST# deassertion (PCIe r5.0, 6.6.1) */
  262. udelay(100 * 1000);
  263. ret = readl_poll_sleep_timeout(port->base + PORT_STATUS, stat,
  264. stat & PORT_STATUS_READY, 100, 250000);
  265. if (ret < 0) {
  266. dev_err(pcie->dev, "port %d ready wait timeout\n", port->idx);
  267. return ret;
  268. }
  269. list_add_tail(&port->entry, &pcie->ports);
  270. writel_relaxed(PORT_LTSSMCTL_START, port->base + PORT_LTSSMCTL);
  271. /*
  272. * Deliberately ignore the link not coming up as connected
  273. * devices (e.g. the WiFi controller) may not be powerd up.
  274. */
  275. readl_poll_sleep_timeout(port->base + PORT_LINKSTS, stat,
  276. (stat & PORT_LINKSTS_UP), 100, 100000);
  277. if (pcie->hw->port_refclk)
  278. rmw_clear(PORT_REFCLK_CGDIS, port->base + PORT_REFCLK);
  279. else
  280. rmw_set(PHY_LANE_CFG_REFCLKCGEN, port->phy + PHY_LANE_CFG);
  281. rmw_clear(PORT_APPCLK_CGDIS, port->base + PORT_APPCLK);
  282. return 0;
  283. }
  284. static int apple_pcie_probe(struct udevice *dev)
  285. {
  286. struct apple_pcie_priv *pcie = dev_get_priv(dev);
  287. fdt_addr_t addr;
  288. ofnode of_port;
  289. int i, ret;
  290. pcie->hw = (struct reg_info *)dev_get_driver_data(dev);
  291. pcie->dev = dev;
  292. addr = dev_read_addr_index(dev, 0);
  293. if (addr == FDT_ADDR_T_NONE)
  294. return -EINVAL;
  295. pcie->cfg_base = map_sysmem(addr, 0);
  296. addr = dev_read_addr_index(dev, 1);
  297. if (addr == FDT_ADDR_T_NONE)
  298. return -EINVAL;
  299. pcie->base = map_sysmem(addr, 0);
  300. INIT_LIST_HEAD(&pcie->ports);
  301. for (of_port = ofnode_first_subnode(dev_ofnode(dev));
  302. ofnode_valid(of_port);
  303. of_port = ofnode_next_subnode(of_port)) {
  304. if (!ofnode_is_enabled(of_port))
  305. continue;
  306. ret = apple_pcie_setup_port(pcie, of_port);
  307. if (ret) {
  308. dev_err(pcie->dev, "Port %d setup fail: %d\n", i, ret);
  309. return ret;
  310. }
  311. }
  312. return 0;
  313. }
  314. static int apple_pcie_remove(struct udevice *dev)
  315. {
  316. struct apple_pcie_priv *pcie = dev_get_priv(dev);
  317. struct apple_pcie_port *port, *tmp;
  318. list_for_each_entry_safe(port, tmp, &pcie->ports, entry) {
  319. gpio_free_list_nodev(&port->reset, 1);
  320. free(port);
  321. }
  322. return 0;
  323. }
  324. static const struct udevice_id apple_pcie_of_match[] = {
  325. { .compatible = "apple,t6020-pcie", .data = (ulong)&t602x_hw },
  326. { .compatible = "apple,pcie", .data = (ulong)&t8103_hw },
  327. { /* sentinel */ }
  328. };
  329. U_BOOT_DRIVER(apple_pcie) = {
  330. .name = "apple_pcie",
  331. .id = UCLASS_PCI,
  332. .of_match = apple_pcie_of_match,
  333. .probe = apple_pcie_probe,
  334. .remove = apple_pcie_remove,
  335. .priv_auto = sizeof(struct apple_pcie_priv),
  336. .ops = &apple_pcie_ops,
  337. };