pci_tegra.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. /*
  2. * Copyright (c) 2010, CompuLab, Ltd.
  3. * Author: Mike Rapoport <mike@compulab.co.il>
  4. *
  5. * Based on NVIDIA PCIe driver
  6. * Copyright (c) 2008-2009, NVIDIA Corporation.
  7. *
  8. * Copyright (c) 2013-2014, NVIDIA Corporation.
  9. *
  10. * SPDX-License-Identifier: GPL-2.0
  11. */
  12. #define pr_fmt(fmt) "tegra-pcie: " fmt
  13. #include <common.h>
  14. #include <dm.h>
  15. #include <errno.h>
  16. #include <fdtdec.h>
  17. #include <malloc.h>
  18. #include <pci.h>
  19. #include <asm/io.h>
  20. #include <asm/gpio.h>
  21. #include <asm/arch/clock.h>
  22. #include <asm/arch/powergate.h>
  23. #include <asm/arch-tegra/xusb-padctl.h>
  24. #include <linux/list.h>
  25. #include <dt-bindings/pinctrl/pinctrl-tegra-xusb.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. #define AFI_AXI_BAR0_SZ 0x00
  28. #define AFI_AXI_BAR1_SZ 0x04
  29. #define AFI_AXI_BAR2_SZ 0x08
  30. #define AFI_AXI_BAR3_SZ 0x0c
  31. #define AFI_AXI_BAR4_SZ 0x10
  32. #define AFI_AXI_BAR5_SZ 0x14
  33. #define AFI_AXI_BAR0_START 0x18
  34. #define AFI_AXI_BAR1_START 0x1c
  35. #define AFI_AXI_BAR2_START 0x20
  36. #define AFI_AXI_BAR3_START 0x24
  37. #define AFI_AXI_BAR4_START 0x28
  38. #define AFI_AXI_BAR5_START 0x2c
  39. #define AFI_FPCI_BAR0 0x30
  40. #define AFI_FPCI_BAR1 0x34
  41. #define AFI_FPCI_BAR2 0x38
  42. #define AFI_FPCI_BAR3 0x3c
  43. #define AFI_FPCI_BAR4 0x40
  44. #define AFI_FPCI_BAR5 0x44
  45. #define AFI_CACHE_BAR0_SZ 0x48
  46. #define AFI_CACHE_BAR0_ST 0x4c
  47. #define AFI_CACHE_BAR1_SZ 0x50
  48. #define AFI_CACHE_BAR1_ST 0x54
  49. #define AFI_MSI_BAR_SZ 0x60
  50. #define AFI_MSI_FPCI_BAR_ST 0x64
  51. #define AFI_MSI_AXI_BAR_ST 0x68
  52. #define AFI_CONFIGURATION 0xac
  53. #define AFI_CONFIGURATION_EN_FPCI (1 << 0)
  54. #define AFI_FPCI_ERROR_MASKS 0xb0
  55. #define AFI_INTR_MASK 0xb4
  56. #define AFI_INTR_MASK_INT_MASK (1 << 0)
  57. #define AFI_INTR_MASK_MSI_MASK (1 << 8)
  58. #define AFI_SM_INTR_ENABLE 0xc4
  59. #define AFI_SM_INTR_INTA_ASSERT (1 << 0)
  60. #define AFI_SM_INTR_INTB_ASSERT (1 << 1)
  61. #define AFI_SM_INTR_INTC_ASSERT (1 << 2)
  62. #define AFI_SM_INTR_INTD_ASSERT (1 << 3)
  63. #define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
  64. #define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
  65. #define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
  66. #define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
  67. #define AFI_AFI_INTR_ENABLE 0xc8
  68. #define AFI_INTR_EN_INI_SLVERR (1 << 0)
  69. #define AFI_INTR_EN_INI_DECERR (1 << 1)
  70. #define AFI_INTR_EN_TGT_SLVERR (1 << 2)
  71. #define AFI_INTR_EN_TGT_DECERR (1 << 3)
  72. #define AFI_INTR_EN_TGT_WRERR (1 << 4)
  73. #define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
  74. #define AFI_INTR_EN_AXI_DECERR (1 << 6)
  75. #define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
  76. #define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
  77. #define AFI_PCIE_CONFIG 0x0f8
  78. #define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
  79. #define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
  80. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
  81. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
  82. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
  83. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1 (0x0 << 20)
  84. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
  85. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
  86. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1 (0x1 << 20)
  87. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
  88. #define AFI_FUSE 0x104
  89. #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
  90. #define AFI_PEX0_CTRL 0x110
  91. #define AFI_PEX1_CTRL 0x118
  92. #define AFI_PEX2_CTRL 0x128
  93. #define AFI_PEX_CTRL_RST (1 << 0)
  94. #define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
  95. #define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
  96. #define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4)
  97. #define AFI_PLLE_CONTROL 0x160
  98. #define AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
  99. #define AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
  100. #define AFI_PEXBIAS_CTRL_0 0x168
  101. #define PADS_CTL_SEL 0x0000009C
  102. #define PADS_CTL 0x000000A0
  103. #define PADS_CTL_IDDQ_1L (1 << 0)
  104. #define PADS_CTL_TX_DATA_EN_1L (1 << 6)
  105. #define PADS_CTL_RX_DATA_EN_1L (1 << 10)
  106. #define PADS_PLL_CTL_TEGRA20 0x000000B8
  107. #define PADS_PLL_CTL_TEGRA30 0x000000B4
  108. #define PADS_PLL_CTL_RST_B4SM (0x1 << 1)
  109. #define PADS_PLL_CTL_LOCKDET (0x1 << 8)
  110. #define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
  111. #define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0x0 << 16)
  112. #define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (0x1 << 16)
  113. #define PADS_PLL_CTL_REFCLK_EXTERNAL (0x2 << 16)
  114. #define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
  115. #define PADS_PLL_CTL_TXCLKREF_DIV10 (0x0 << 20)
  116. #define PADS_PLL_CTL_TXCLKREF_DIV5 (0x1 << 20)
  117. #define PADS_PLL_CTL_TXCLKREF_BUF_EN (0x1 << 22)
  118. #define PADS_REFCLK_CFG0 0x000000C8
  119. #define PADS_REFCLK_CFG1 0x000000CC
  120. /*
  121. * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
  122. * entries, one entry per PCIe port. These field definitions and desired
  123. * values aren't in the TRM, but do come from NVIDIA.
  124. */
  125. #define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
  126. #define PADS_REFCLK_CFG_E_TERM_SHIFT 7
  127. #define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
  128. #define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
  129. /* Default value provided by HW engineering is 0xfa5c */
  130. #define PADS_REFCLK_CFG_VALUE \
  131. ( \
  132. (0x17 << PADS_REFCLK_CFG_TERM_SHIFT) | \
  133. (0 << PADS_REFCLK_CFG_E_TERM_SHIFT) | \
  134. (0xa << PADS_REFCLK_CFG_PREDI_SHIFT) | \
  135. (0xf << PADS_REFCLK_CFG_DRVI_SHIFT) \
  136. )
  137. #define RP_VEND_XP 0x00000F00
  138. #define RP_VEND_XP_DL_UP (1 << 30)
  139. #define RP_VEND_CTL2 0x00000FA8
  140. #define RP_VEND_CTL2_PCA_ENABLE (1 << 7)
  141. #define RP_PRIV_MISC 0x00000FE0
  142. #define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xE << 0)
  143. #define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xF << 0)
  144. #define RP_LINK_CONTROL_STATUS 0x00000090
  145. #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
  146. #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
  147. enum tegra_pci_id {
  148. TEGRA20_PCIE,
  149. TEGRA30_PCIE,
  150. TEGRA124_PCIE,
  151. TEGRA210_PCIE,
  152. };
  153. struct tegra_pcie_port {
  154. struct tegra_pcie *pcie;
  155. struct fdt_resource regs;
  156. unsigned int num_lanes;
  157. unsigned int index;
  158. struct list_head list;
  159. };
  160. struct tegra_pcie_soc {
  161. unsigned int num_ports;
  162. unsigned long pads_pll_ctl;
  163. unsigned long tx_ref_sel;
  164. bool has_pex_clkreq_en;
  165. bool has_pex_bias_ctrl;
  166. bool has_cml_clk;
  167. bool has_gen2;
  168. bool force_pca_enable;
  169. };
  170. struct tegra_pcie {
  171. struct pci_controller hose;
  172. struct fdt_resource pads;
  173. struct fdt_resource afi;
  174. struct fdt_resource cs;
  175. struct list_head ports;
  176. unsigned long xbar;
  177. const struct tegra_pcie_soc *soc;
  178. struct tegra_xusb_phy *phy;
  179. };
  180. static void afi_writel(struct tegra_pcie *pcie, unsigned long value,
  181. unsigned long offset)
  182. {
  183. writel(value, pcie->afi.start + offset);
  184. }
  185. static unsigned long afi_readl(struct tegra_pcie *pcie, unsigned long offset)
  186. {
  187. return readl(pcie->afi.start + offset);
  188. }
  189. static void pads_writel(struct tegra_pcie *pcie, unsigned long value,
  190. unsigned long offset)
  191. {
  192. writel(value, pcie->pads.start + offset);
  193. }
  194. static unsigned long pads_readl(struct tegra_pcie *pcie, unsigned long offset)
  195. {
  196. return readl(pcie->pads.start + offset);
  197. }
  198. static unsigned long rp_readl(struct tegra_pcie_port *port,
  199. unsigned long offset)
  200. {
  201. return readl(port->regs.start + offset);
  202. }
  203. static void rp_writel(struct tegra_pcie_port *port, unsigned long value,
  204. unsigned long offset)
  205. {
  206. writel(value, port->regs.start + offset);
  207. }
  208. static unsigned long tegra_pcie_conf_offset(pci_dev_t bdf, int where)
  209. {
  210. return ((where & 0xf00) << 16) | (PCI_BUS(bdf) << 16) |
  211. (PCI_DEV(bdf) << 11) | (PCI_FUNC(bdf) << 8) |
  212. (where & 0xfc);
  213. }
  214. static int tegra_pcie_conf_address(struct tegra_pcie *pcie, pci_dev_t bdf,
  215. int where, unsigned long *address)
  216. {
  217. unsigned int bus = PCI_BUS(bdf);
  218. if (bus == 0) {
  219. unsigned int dev = PCI_DEV(bdf);
  220. struct tegra_pcie_port *port;
  221. list_for_each_entry(port, &pcie->ports, list) {
  222. if (port->index + 1 == dev) {
  223. *address = port->regs.start + (where & ~3);
  224. return 0;
  225. }
  226. }
  227. } else {
  228. *address = pcie->cs.start + tegra_pcie_conf_offset(bdf, where);
  229. return 0;
  230. }
  231. return -EFAULT;
  232. }
  233. static int pci_tegra_read_config(struct udevice *bus, pci_dev_t bdf,
  234. uint offset, ulong *valuep,
  235. enum pci_size_t size)
  236. {
  237. struct tegra_pcie *pcie = dev_get_priv(bus);
  238. unsigned long address, value;
  239. int err;
  240. err = tegra_pcie_conf_address(pcie, bdf, offset, &address);
  241. if (err < 0) {
  242. value = 0xffffffff;
  243. goto done;
  244. }
  245. value = readl(address);
  246. /* fixup root port class */
  247. if (PCI_BUS(bdf) == 0) {
  248. if (offset == PCI_CLASS_REVISION) {
  249. value &= ~0x00ff0000;
  250. value |= PCI_CLASS_BRIDGE_PCI << 16;
  251. }
  252. }
  253. done:
  254. *valuep = pci_conv_32_to_size(value, offset, size);
  255. return 0;
  256. }
  257. static int pci_tegra_write_config(struct udevice *bus, pci_dev_t bdf,
  258. uint offset, ulong value,
  259. enum pci_size_t size)
  260. {
  261. struct tegra_pcie *pcie = dev_get_priv(bus);
  262. unsigned long address;
  263. ulong old;
  264. int err;
  265. err = tegra_pcie_conf_address(pcie, bdf, offset, &address);
  266. if (err < 0)
  267. return 0;
  268. old = readl(address);
  269. value = pci_conv_size_to_32(old, value, offset, size);
  270. writel(value, address);
  271. return 0;
  272. }
  273. static int tegra_pcie_port_parse_dt(const void *fdt, int node,
  274. struct tegra_pcie_port *port)
  275. {
  276. const u32 *addr;
  277. int len;
  278. addr = fdt_getprop(fdt, node, "assigned-addresses", &len);
  279. if (!addr) {
  280. error("property \"assigned-addresses\" not found");
  281. return -FDT_ERR_NOTFOUND;
  282. }
  283. port->regs.start = fdt32_to_cpu(addr[2]);
  284. port->regs.end = port->regs.start + fdt32_to_cpu(addr[4]);
  285. return 0;
  286. }
  287. static int tegra_pcie_get_xbar_config(const void *fdt, int node, u32 lanes,
  288. enum tegra_pci_id id, unsigned long *xbar)
  289. {
  290. switch (id) {
  291. case TEGRA20_PCIE:
  292. switch (lanes) {
  293. case 0x00000004:
  294. debug("single-mode configuration\n");
  295. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
  296. return 0;
  297. case 0x00000202:
  298. debug("dual-mode configuration\n");
  299. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
  300. return 0;
  301. }
  302. break;
  303. case TEGRA30_PCIE:
  304. switch (lanes) {
  305. case 0x00000204:
  306. debug("4x1, 2x1 configuration\n");
  307. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
  308. return 0;
  309. case 0x00020202:
  310. debug("2x3 configuration\n");
  311. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
  312. return 0;
  313. case 0x00010104:
  314. debug("4x1, 1x2 configuration\n");
  315. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
  316. return 0;
  317. }
  318. break;
  319. case TEGRA124_PCIE:
  320. case TEGRA210_PCIE:
  321. switch (lanes) {
  322. case 0x0000104:
  323. debug("4x1, 1x1 configuration\n");
  324. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
  325. return 0;
  326. case 0x0000102:
  327. debug("2x1, 1x1 configuration\n");
  328. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
  329. return 0;
  330. }
  331. break;
  332. default:
  333. break;
  334. }
  335. return -FDT_ERR_NOTFOUND;
  336. }
  337. static int tegra_pcie_parse_port_info(const void *fdt, int node,
  338. unsigned int *index,
  339. unsigned int *lanes)
  340. {
  341. struct fdt_pci_addr addr;
  342. int err;
  343. err = fdtdec_get_int(fdt, node, "nvidia,num-lanes", 0);
  344. if (err < 0) {
  345. error("failed to parse \"nvidia,num-lanes\" property");
  346. return err;
  347. }
  348. *lanes = err;
  349. err = fdtdec_get_pci_addr(fdt, node, 0, "reg", &addr);
  350. if (err < 0) {
  351. error("failed to parse \"reg\" property");
  352. return err;
  353. }
  354. *index = PCI_DEV(addr.phys_hi) - 1;
  355. return 0;
  356. }
  357. int __weak tegra_pcie_board_init(void)
  358. {
  359. return 0;
  360. }
  361. static int tegra_pcie_parse_dt(const void *fdt, int node, enum tegra_pci_id id,
  362. struct tegra_pcie *pcie)
  363. {
  364. int err, subnode;
  365. u32 lanes = 0;
  366. err = fdt_get_named_resource(fdt, node, "reg", "reg-names", "pads",
  367. &pcie->pads);
  368. if (err < 0) {
  369. error("resource \"pads\" not found");
  370. return err;
  371. }
  372. err = fdt_get_named_resource(fdt, node, "reg", "reg-names", "afi",
  373. &pcie->afi);
  374. if (err < 0) {
  375. error("resource \"afi\" not found");
  376. return err;
  377. }
  378. err = fdt_get_named_resource(fdt, node, "reg", "reg-names", "cs",
  379. &pcie->cs);
  380. if (err < 0) {
  381. error("resource \"cs\" not found");
  382. return err;
  383. }
  384. err = tegra_pcie_board_init();
  385. if (err < 0) {
  386. error("tegra_pcie_board_init() failed: err=%d", err);
  387. return err;
  388. }
  389. pcie->phy = tegra_xusb_phy_get(TEGRA_XUSB_PADCTL_PCIE);
  390. if (pcie->phy) {
  391. err = tegra_xusb_phy_prepare(pcie->phy);
  392. if (err < 0) {
  393. error("failed to prepare PHY: %d", err);
  394. return err;
  395. }
  396. }
  397. fdt_for_each_subnode(fdt, subnode, node) {
  398. unsigned int index = 0, num_lanes = 0;
  399. struct tegra_pcie_port *port;
  400. err = tegra_pcie_parse_port_info(fdt, subnode, &index,
  401. &num_lanes);
  402. if (err < 0) {
  403. error("failed to obtain root port info");
  404. continue;
  405. }
  406. lanes |= num_lanes << (index << 3);
  407. if (!fdtdec_get_is_enabled(fdt, subnode))
  408. continue;
  409. port = malloc(sizeof(*port));
  410. if (!port)
  411. continue;
  412. memset(port, 0, sizeof(*port));
  413. port->num_lanes = num_lanes;
  414. port->index = index;
  415. err = tegra_pcie_port_parse_dt(fdt, subnode, port);
  416. if (err < 0) {
  417. free(port);
  418. continue;
  419. }
  420. list_add_tail(&port->list, &pcie->ports);
  421. port->pcie = pcie;
  422. }
  423. err = tegra_pcie_get_xbar_config(fdt, node, lanes, id, &pcie->xbar);
  424. if (err < 0) {
  425. error("invalid lane configuration");
  426. return err;
  427. }
  428. return 0;
  429. }
  430. static int tegra_pcie_power_on(struct tegra_pcie *pcie)
  431. {
  432. const struct tegra_pcie_soc *soc = pcie->soc;
  433. unsigned long value;
  434. int err;
  435. /* reset PCIEXCLK logic, AFI controller and PCIe controller */
  436. reset_set_enable(PERIPH_ID_PCIEXCLK, 1);
  437. reset_set_enable(PERIPH_ID_AFI, 1);
  438. reset_set_enable(PERIPH_ID_PCIE, 1);
  439. err = tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
  440. if (err < 0) {
  441. error("failed to power off PCIe partition: %d", err);
  442. return err;
  443. }
  444. err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
  445. PERIPH_ID_PCIE);
  446. if (err < 0) {
  447. error("failed to power up PCIe partition: %d", err);
  448. return err;
  449. }
  450. /* take AFI controller out of reset */
  451. reset_set_enable(PERIPH_ID_AFI, 0);
  452. /* enable AFI clock */
  453. clock_enable(PERIPH_ID_AFI);
  454. if (soc->has_cml_clk) {
  455. /* enable CML clock */
  456. value = readl(NV_PA_CLK_RST_BASE + 0x48c);
  457. value |= (1 << 0);
  458. value &= ~(1 << 1);
  459. writel(value, NV_PA_CLK_RST_BASE + 0x48c);
  460. }
  461. err = tegra_plle_enable();
  462. if (err < 0) {
  463. error("failed to enable PLLE: %d\n", err);
  464. return err;
  465. }
  466. return 0;
  467. }
  468. static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
  469. {
  470. const struct tegra_pcie_soc *soc = pcie->soc;
  471. unsigned long start = get_timer(0);
  472. u32 value;
  473. while (get_timer(start) < timeout) {
  474. value = pads_readl(pcie, soc->pads_pll_ctl);
  475. if (value & PADS_PLL_CTL_LOCKDET)
  476. return 0;
  477. }
  478. return -ETIMEDOUT;
  479. }
  480. static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
  481. {
  482. const struct tegra_pcie_soc *soc = pcie->soc;
  483. u32 value;
  484. int err;
  485. /* initialize internal PHY, enable up to 16 PCIe lanes */
  486. pads_writel(pcie, 0, PADS_CTL_SEL);
  487. /* override IDDQ to 1 on all 4 lanes */
  488. value = pads_readl(pcie, PADS_CTL);
  489. value |= PADS_CTL_IDDQ_1L;
  490. pads_writel(pcie, value, PADS_CTL);
  491. /*
  492. * Set up PHY PLL inputs select PLLE output as refclock, set TX
  493. * ref sel to div10 (not div5).
  494. */
  495. value = pads_readl(pcie, soc->pads_pll_ctl);
  496. value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
  497. value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
  498. pads_writel(pcie, value, soc->pads_pll_ctl);
  499. /* reset PLL */
  500. value = pads_readl(pcie, soc->pads_pll_ctl);
  501. value &= ~PADS_PLL_CTL_RST_B4SM;
  502. pads_writel(pcie, value, soc->pads_pll_ctl);
  503. udelay(20);
  504. /* take PLL out of reset */
  505. value = pads_readl(pcie, soc->pads_pll_ctl);
  506. value |= PADS_PLL_CTL_RST_B4SM;
  507. pads_writel(pcie, value, soc->pads_pll_ctl);
  508. /* configure the reference clock driver */
  509. value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
  510. pads_writel(pcie, value, PADS_REFCLK_CFG0);
  511. if (soc->num_ports > 2)
  512. pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
  513. /* wait for the PLL to lock */
  514. err = tegra_pcie_pll_wait(pcie, 500);
  515. if (err < 0) {
  516. error("PLL failed to lock: %d", err);
  517. return err;
  518. }
  519. /* turn off IDDQ override */
  520. value = pads_readl(pcie, PADS_CTL);
  521. value &= ~PADS_CTL_IDDQ_1L;
  522. pads_writel(pcie, value, PADS_CTL);
  523. /* enable TX/RX data */
  524. value = pads_readl(pcie, PADS_CTL);
  525. value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
  526. pads_writel(pcie, value, PADS_CTL);
  527. return 0;
  528. }
  529. static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
  530. {
  531. const struct tegra_pcie_soc *soc = pcie->soc;
  532. struct tegra_pcie_port *port;
  533. u32 value;
  534. int err;
  535. if (pcie->phy) {
  536. value = afi_readl(pcie, AFI_PLLE_CONTROL);
  537. value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
  538. value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
  539. afi_writel(pcie, value, AFI_PLLE_CONTROL);
  540. }
  541. if (soc->has_pex_bias_ctrl)
  542. afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
  543. value = afi_readl(pcie, AFI_PCIE_CONFIG);
  544. value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
  545. value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar;
  546. list_for_each_entry(port, &pcie->ports, list)
  547. value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
  548. afi_writel(pcie, value, AFI_PCIE_CONFIG);
  549. value = afi_readl(pcie, AFI_FUSE);
  550. if (soc->has_gen2)
  551. value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
  552. else
  553. value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
  554. afi_writel(pcie, value, AFI_FUSE);
  555. if (pcie->phy)
  556. err = tegra_xusb_phy_enable(pcie->phy);
  557. else
  558. err = tegra_pcie_phy_enable(pcie);
  559. if (err < 0) {
  560. error("failed to power on PHY: %d\n", err);
  561. return err;
  562. }
  563. /* take the PCIEXCLK logic out of reset */
  564. reset_set_enable(PERIPH_ID_PCIEXCLK, 0);
  565. /* finally enable PCIe */
  566. value = afi_readl(pcie, AFI_CONFIGURATION);
  567. value |= AFI_CONFIGURATION_EN_FPCI;
  568. afi_writel(pcie, value, AFI_CONFIGURATION);
  569. /* disable all interrupts */
  570. afi_writel(pcie, 0, AFI_AFI_INTR_ENABLE);
  571. afi_writel(pcie, 0, AFI_SM_INTR_ENABLE);
  572. afi_writel(pcie, 0, AFI_INTR_MASK);
  573. afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
  574. return 0;
  575. }
  576. static int tegra_pcie_setup_translations(struct udevice *bus)
  577. {
  578. struct tegra_pcie *pcie = dev_get_priv(bus);
  579. unsigned long fpci, axi, size;
  580. struct pci_region *io, *mem, *pref;
  581. int count;
  582. /* BAR 0: type 1 extended configuration space */
  583. fpci = 0xfe100000;
  584. size = fdt_resource_size(&pcie->cs);
  585. axi = pcie->cs.start;
  586. afi_writel(pcie, axi, AFI_AXI_BAR0_START);
  587. afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
  588. afi_writel(pcie, fpci, AFI_FPCI_BAR0);
  589. count = pci_get_regions(bus, &io, &mem, &pref);
  590. if (count != 3)
  591. return -EINVAL;
  592. /* BAR 1: downstream I/O */
  593. fpci = 0xfdfc0000;
  594. size = io->size;
  595. axi = io->phys_start;
  596. afi_writel(pcie, axi, AFI_AXI_BAR1_START);
  597. afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
  598. afi_writel(pcie, fpci, AFI_FPCI_BAR1);
  599. /* BAR 2: prefetchable memory */
  600. fpci = (((pref->phys_start >> 12) & 0x0fffffff) << 4) | 0x1;
  601. size = pref->size;
  602. axi = pref->phys_start;
  603. afi_writel(pcie, axi, AFI_AXI_BAR2_START);
  604. afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
  605. afi_writel(pcie, fpci, AFI_FPCI_BAR2);
  606. /* BAR 3: non-prefetchable memory */
  607. fpci = (((mem->phys_start >> 12) & 0x0fffffff) << 4) | 0x1;
  608. size = mem->size;
  609. axi = mem->phys_start;
  610. afi_writel(pcie, axi, AFI_AXI_BAR3_START);
  611. afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
  612. afi_writel(pcie, fpci, AFI_FPCI_BAR3);
  613. /* NULL out the remaining BARs as they are not used */
  614. afi_writel(pcie, 0, AFI_AXI_BAR4_START);
  615. afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
  616. afi_writel(pcie, 0, AFI_FPCI_BAR4);
  617. afi_writel(pcie, 0, AFI_AXI_BAR5_START);
  618. afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
  619. afi_writel(pcie, 0, AFI_FPCI_BAR5);
  620. /* map all upstream transactions as uncached */
  621. afi_writel(pcie, NV_PA_SDRAM_BASE, AFI_CACHE_BAR0_ST);
  622. afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
  623. afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
  624. afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
  625. /* MSI translations are setup only when needed */
  626. afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
  627. afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
  628. afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
  629. afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
  630. return 0;
  631. }
  632. static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
  633. {
  634. unsigned long ret = 0;
  635. switch (port->index) {
  636. case 0:
  637. ret = AFI_PEX0_CTRL;
  638. break;
  639. case 1:
  640. ret = AFI_PEX1_CTRL;
  641. break;
  642. case 2:
  643. ret = AFI_PEX2_CTRL;
  644. break;
  645. }
  646. return ret;
  647. }
  648. static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
  649. {
  650. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  651. unsigned long value;
  652. /* pulse reset signel */
  653. value = afi_readl(port->pcie, ctrl);
  654. value &= ~AFI_PEX_CTRL_RST;
  655. afi_writel(port->pcie, value, ctrl);
  656. udelay(2000);
  657. value = afi_readl(port->pcie, ctrl);
  658. value |= AFI_PEX_CTRL_RST;
  659. afi_writel(port->pcie, value, ctrl);
  660. }
  661. static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
  662. {
  663. const struct tegra_pcie_soc *soc = port->pcie->soc;
  664. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  665. unsigned long value;
  666. /* enable reference clock */
  667. value = afi_readl(port->pcie, ctrl);
  668. value |= AFI_PEX_CTRL_REFCLK_EN;
  669. if (port->pcie->soc->has_pex_clkreq_en)
  670. value |= AFI_PEX_CTRL_CLKREQ_EN;
  671. value |= AFI_PEX_CTRL_OVERRIDE_EN;
  672. afi_writel(port->pcie, value, ctrl);
  673. tegra_pcie_port_reset(port);
  674. if (soc->force_pca_enable) {
  675. value = rp_readl(port, RP_VEND_CTL2);
  676. value |= RP_VEND_CTL2_PCA_ENABLE;
  677. rp_writel(port, value, RP_VEND_CTL2);
  678. }
  679. }
  680. static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
  681. {
  682. unsigned int retries = 3;
  683. unsigned long value;
  684. value = rp_readl(port, RP_PRIV_MISC);
  685. value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
  686. value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
  687. rp_writel(port, value, RP_PRIV_MISC);
  688. do {
  689. unsigned int timeout = 200;
  690. do {
  691. value = rp_readl(port, RP_VEND_XP);
  692. if (value & RP_VEND_XP_DL_UP)
  693. break;
  694. udelay(2000);
  695. } while (--timeout);
  696. if (!timeout) {
  697. debug("link %u down, retrying\n", port->index);
  698. goto retry;
  699. }
  700. timeout = 200;
  701. do {
  702. value = rp_readl(port, RP_LINK_CONTROL_STATUS);
  703. if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
  704. return true;
  705. udelay(2000);
  706. } while (--timeout);
  707. retry:
  708. tegra_pcie_port_reset(port);
  709. } while (--retries);
  710. return false;
  711. }
  712. static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
  713. {
  714. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  715. unsigned long value;
  716. /* assert port reset */
  717. value = afi_readl(port->pcie, ctrl);
  718. value &= ~AFI_PEX_CTRL_RST;
  719. afi_writel(port->pcie, value, ctrl);
  720. /* disable reference clock */
  721. value = afi_readl(port->pcie, ctrl);
  722. value &= ~AFI_PEX_CTRL_REFCLK_EN;
  723. afi_writel(port->pcie, value, ctrl);
  724. }
  725. static void tegra_pcie_port_free(struct tegra_pcie_port *port)
  726. {
  727. list_del(&port->list);
  728. free(port);
  729. }
  730. static int tegra_pcie_enable(struct tegra_pcie *pcie)
  731. {
  732. struct tegra_pcie_port *port, *tmp;
  733. list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
  734. debug("probing port %u, using %u lanes\n", port->index,
  735. port->num_lanes);
  736. tegra_pcie_port_enable(port);
  737. if (tegra_pcie_port_check_link(port))
  738. continue;
  739. debug("link %u down, ignoring\n", port->index);
  740. tegra_pcie_port_disable(port);
  741. tegra_pcie_port_free(port);
  742. }
  743. return 0;
  744. }
  745. static const struct tegra_pcie_soc pci_tegra_soc[] = {
  746. [TEGRA20_PCIE] = {
  747. .num_ports = 2,
  748. .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
  749. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
  750. .has_pex_clkreq_en = false,
  751. .has_pex_bias_ctrl = false,
  752. .has_cml_clk = false,
  753. .has_gen2 = false,
  754. },
  755. [TEGRA30_PCIE] = {
  756. .num_ports = 3,
  757. .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
  758. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
  759. .has_pex_clkreq_en = true,
  760. .has_pex_bias_ctrl = true,
  761. .has_cml_clk = true,
  762. .has_gen2 = false,
  763. },
  764. [TEGRA124_PCIE] = {
  765. .num_ports = 2,
  766. .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
  767. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
  768. .has_pex_clkreq_en = true,
  769. .has_pex_bias_ctrl = true,
  770. .has_cml_clk = true,
  771. .has_gen2 = true,
  772. },
  773. [TEGRA210_PCIE] = {
  774. .num_ports = 2,
  775. .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
  776. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
  777. .has_pex_clkreq_en = true,
  778. .has_pex_bias_ctrl = true,
  779. .has_cml_clk = true,
  780. .has_gen2 = true,
  781. .force_pca_enable = true,
  782. }
  783. };
  784. static int pci_tegra_ofdata_to_platdata(struct udevice *dev)
  785. {
  786. struct tegra_pcie *pcie = dev_get_priv(dev);
  787. enum tegra_pci_id id;
  788. id = dev_get_driver_data(dev);
  789. pcie->soc = &pci_tegra_soc[id];
  790. INIT_LIST_HEAD(&pcie->ports);
  791. if (tegra_pcie_parse_dt(gd->fdt_blob, dev->of_offset, id, pcie))
  792. return -EINVAL;
  793. return 0;
  794. }
  795. static int pci_tegra_probe(struct udevice *dev)
  796. {
  797. struct tegra_pcie *pcie = dev_get_priv(dev);
  798. int err;
  799. err = tegra_pcie_power_on(pcie);
  800. if (err < 0) {
  801. error("failed to power on");
  802. return err;
  803. }
  804. err = tegra_pcie_enable_controller(pcie);
  805. if (err < 0) {
  806. error("failed to enable controller");
  807. return err;
  808. }
  809. err = tegra_pcie_setup_translations(dev);
  810. if (err < 0) {
  811. error("failed to decode ranges");
  812. return err;
  813. }
  814. err = tegra_pcie_enable(pcie);
  815. if (err < 0) {
  816. error("failed to enable PCIe");
  817. return err;
  818. }
  819. return 0;
  820. }
  821. static const struct dm_pci_ops pci_tegra_ops = {
  822. .read_config = pci_tegra_read_config,
  823. .write_config = pci_tegra_write_config,
  824. };
  825. static const struct udevice_id pci_tegra_ids[] = {
  826. { .compatible = "nvidia,tegra20-pcie", .data = TEGRA20_PCIE },
  827. { .compatible = "nvidia,tegra30-pcie", .data = TEGRA30_PCIE },
  828. { .compatible = "nvidia,tegra124-pcie", .data = TEGRA124_PCIE },
  829. { .compatible = "nvidia,tegra210-pcie", .data = TEGRA210_PCIE },
  830. { }
  831. };
  832. U_BOOT_DRIVER(pci_tegra) = {
  833. .name = "pci_tegra",
  834. .id = UCLASS_PCI,
  835. .of_match = pci_tegra_ids,
  836. .ops = &pci_tegra_ops,
  837. .ofdata_to_platdata = pci_tegra_ofdata_to_platdata,
  838. .probe = pci_tegra_probe,
  839. .priv_auto_alloc_size = sizeof(struct tegra_pcie),
  840. };
  841. int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
  842. {
  843. if (PCI_BUS(dev) != 0 && PCI_DEV(dev) > 0)
  844. return 1;
  845. return 0;
  846. }