pci_tegra.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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 DEBUG
  13. #define pr_fmt(fmt) "tegra-pcie: " fmt
  14. #include <common.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_PRIV_MISC 0x00000FE0
  140. #define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xE << 0)
  141. #define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xF << 0)
  142. #define RP_LINK_CONTROL_STATUS 0x00000090
  143. #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
  144. #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
  145. struct tegra_pcie;
  146. struct tegra_pcie_port {
  147. struct tegra_pcie *pcie;
  148. struct fdt_resource regs;
  149. unsigned int num_lanes;
  150. unsigned int index;
  151. struct list_head list;
  152. };
  153. struct tegra_pcie_soc {
  154. unsigned int num_ports;
  155. unsigned long pads_pll_ctl;
  156. unsigned long tx_ref_sel;
  157. bool has_pex_clkreq_en;
  158. bool has_pex_bias_ctrl;
  159. bool has_cml_clk;
  160. bool has_gen2;
  161. };
  162. struct tegra_pcie {
  163. struct pci_controller hose;
  164. struct fdt_resource pads;
  165. struct fdt_resource afi;
  166. struct fdt_resource cs;
  167. struct fdt_resource prefetch;
  168. struct fdt_resource mem;
  169. struct fdt_resource io;
  170. struct list_head ports;
  171. unsigned long xbar;
  172. const struct tegra_pcie_soc *soc;
  173. struct tegra_xusb_phy *phy;
  174. };
  175. static inline struct tegra_pcie *to_tegra_pcie(struct pci_controller *hose)
  176. {
  177. return container_of(hose, struct tegra_pcie, hose);
  178. }
  179. static void afi_writel(struct tegra_pcie *pcie, unsigned long value,
  180. unsigned long offset)
  181. {
  182. writel(value, pcie->afi.start + offset);
  183. }
  184. static unsigned long afi_readl(struct tegra_pcie *pcie, unsigned long offset)
  185. {
  186. return readl(pcie->afi.start + offset);
  187. }
  188. static void pads_writel(struct tegra_pcie *pcie, unsigned long value,
  189. unsigned long offset)
  190. {
  191. writel(value, pcie->pads.start + offset);
  192. }
  193. static unsigned long pads_readl(struct tegra_pcie *pcie, unsigned long offset)
  194. {
  195. return readl(pcie->pads.start + offset);
  196. }
  197. static unsigned long rp_readl(struct tegra_pcie_port *port,
  198. unsigned long offset)
  199. {
  200. return readl(port->regs.start + offset);
  201. }
  202. static void rp_writel(struct tegra_pcie_port *port, unsigned long value,
  203. unsigned long offset)
  204. {
  205. writel(value, port->regs.start + offset);
  206. }
  207. static unsigned long tegra_pcie_conf_offset(pci_dev_t bdf, int where)
  208. {
  209. return ((where & 0xf00) << 16) | (PCI_BUS(bdf) << 16) |
  210. (PCI_DEV(bdf) << 11) | (PCI_FUNC(bdf) << 8) |
  211. (where & 0xfc);
  212. }
  213. static int tegra_pcie_conf_address(struct tegra_pcie *pcie, pci_dev_t bdf,
  214. int where, unsigned long *address)
  215. {
  216. unsigned int bus = PCI_BUS(bdf);
  217. if (bus == 0) {
  218. unsigned int dev = PCI_DEV(bdf);
  219. struct tegra_pcie_port *port;
  220. list_for_each_entry(port, &pcie->ports, list) {
  221. if (port->index + 1 == dev) {
  222. *address = port->regs.start + (where & ~3);
  223. return 0;
  224. }
  225. }
  226. } else {
  227. *address = pcie->cs.start + tegra_pcie_conf_offset(bdf, where);
  228. return 0;
  229. }
  230. return -1;
  231. }
  232. static int tegra_pcie_read_conf(struct pci_controller *hose, pci_dev_t bdf,
  233. int where, u32 *value)
  234. {
  235. struct tegra_pcie *pcie = to_tegra_pcie(hose);
  236. unsigned long address;
  237. int err;
  238. err = tegra_pcie_conf_address(pcie, bdf, where, &address);
  239. if (err < 0) {
  240. *value = 0xffffffff;
  241. return 1;
  242. }
  243. *value = readl(address);
  244. /* fixup root port class */
  245. if (PCI_BUS(bdf) == 0) {
  246. if (where == PCI_CLASS_REVISION) {
  247. *value &= ~0x00ff0000;
  248. *value |= PCI_CLASS_BRIDGE_PCI << 16;
  249. }
  250. }
  251. return 0;
  252. }
  253. static int tegra_pcie_write_conf(struct pci_controller *hose, pci_dev_t bdf,
  254. int where, u32 value)
  255. {
  256. struct tegra_pcie *pcie = to_tegra_pcie(hose);
  257. unsigned long address;
  258. int err;
  259. err = tegra_pcie_conf_address(pcie, bdf, where, &address);
  260. if (err < 0)
  261. return 1;
  262. writel(value, address);
  263. return 0;
  264. }
  265. static int tegra_pcie_port_parse_dt(const void *fdt, int node,
  266. struct tegra_pcie_port *port)
  267. {
  268. const u32 *addr;
  269. int len;
  270. addr = fdt_getprop(fdt, node, "assigned-addresses", &len);
  271. if (!addr) {
  272. error("property \"assigned-addresses\" not found");
  273. return -FDT_ERR_NOTFOUND;
  274. }
  275. port->regs.start = fdt32_to_cpu(addr[2]);
  276. port->regs.end = port->regs.start + fdt32_to_cpu(addr[4]);
  277. return 0;
  278. }
  279. static int tegra_pcie_get_xbar_config(const void *fdt, int node, u32 lanes,
  280. unsigned long *xbar)
  281. {
  282. enum fdt_compat_id id = fdtdec_lookup(fdt, node);
  283. switch (id) {
  284. case COMPAT_NVIDIA_TEGRA20_PCIE:
  285. switch (lanes) {
  286. case 0x00000004:
  287. debug("single-mode configuration\n");
  288. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
  289. return 0;
  290. case 0x00000202:
  291. debug("dual-mode configuration\n");
  292. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
  293. return 0;
  294. }
  295. break;
  296. case COMPAT_NVIDIA_TEGRA30_PCIE:
  297. switch (lanes) {
  298. case 0x00000204:
  299. debug("4x1, 2x1 configuration\n");
  300. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
  301. return 0;
  302. case 0x00020202:
  303. debug("2x3 configuration\n");
  304. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
  305. return 0;
  306. case 0x00010104:
  307. debug("4x1, 1x2 configuration\n");
  308. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
  309. return 0;
  310. }
  311. break;
  312. case COMPAT_NVIDIA_TEGRA124_PCIE:
  313. switch (lanes) {
  314. case 0x0000104:
  315. debug("4x1, 1x1 configuration\n");
  316. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
  317. return 0;
  318. case 0x0000102:
  319. debug("2x1, 1x1 configuration\n");
  320. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
  321. return 0;
  322. }
  323. break;
  324. default:
  325. break;
  326. }
  327. return -FDT_ERR_NOTFOUND;
  328. }
  329. static int tegra_pcie_parse_dt_ranges(const void *fdt, int node,
  330. struct tegra_pcie *pcie)
  331. {
  332. const u32 *ptr, *end;
  333. int len;
  334. ptr = fdt_getprop(fdt, node, "ranges", &len);
  335. if (!ptr) {
  336. error("missing \"ranges\" property");
  337. return -FDT_ERR_NOTFOUND;
  338. }
  339. end = ptr + len / 4;
  340. while (ptr < end) {
  341. struct fdt_resource *res = NULL;
  342. u32 space = fdt32_to_cpu(*ptr);
  343. switch ((space >> 24) & 0x3) {
  344. case 0x01:
  345. res = &pcie->io;
  346. break;
  347. case 0x02: /* 32 bit */
  348. case 0x03: /* 64 bit */
  349. if (space & (1 << 30))
  350. res = &pcie->prefetch;
  351. else
  352. res = &pcie->mem;
  353. break;
  354. }
  355. if (res) {
  356. res->start = fdt32_to_cpu(ptr[3]);
  357. res->end = res->start + fdt32_to_cpu(ptr[5]);
  358. }
  359. ptr += 3 + 1 + 2;
  360. }
  361. debug("PCI regions:\n");
  362. debug(" I/O: %#x-%#x\n", pcie->io.start, pcie->io.end);
  363. debug(" non-prefetchable memory: %#x-%#x\n", pcie->mem.start,
  364. pcie->mem.end);
  365. debug(" prefetchable memory: %#x-%#x\n", pcie->prefetch.start,
  366. pcie->prefetch.end);
  367. return 0;
  368. }
  369. static int tegra_pcie_parse_port_info(const void *fdt, int node,
  370. unsigned int *index,
  371. unsigned int *lanes)
  372. {
  373. struct fdt_pci_addr addr;
  374. pci_dev_t bdf;
  375. int err;
  376. err = fdtdec_get_int(fdt, node, "nvidia,num-lanes", 0);
  377. if (err < 0) {
  378. error("failed to parse \"nvidia,num-lanes\" property");
  379. return err;
  380. }
  381. *lanes = err;
  382. err = fdtdec_get_pci_bdf(fdt, node, &addr, &bdf);
  383. if (err < 0) {
  384. error("failed to parse \"reg\" property");
  385. return err;
  386. }
  387. *index = PCI_DEV(bdf) - 1;
  388. return 0;
  389. }
  390. static int tegra_pcie_parse_dt(const void *fdt, int node,
  391. struct tegra_pcie *pcie)
  392. {
  393. int err, subnode;
  394. u32 lanes = 0;
  395. err = fdt_get_named_resource(fdt, node, "reg", "reg-names", "pads",
  396. &pcie->pads);
  397. if (err < 0) {
  398. error("resource \"pads\" not found");
  399. return err;
  400. }
  401. err = fdt_get_named_resource(fdt, node, "reg", "reg-names", "afi",
  402. &pcie->afi);
  403. if (err < 0) {
  404. error("resource \"afi\" not found");
  405. return err;
  406. }
  407. err = fdt_get_named_resource(fdt, node, "reg", "reg-names", "cs",
  408. &pcie->cs);
  409. if (err < 0) {
  410. error("resource \"cs\" not found");
  411. return err;
  412. }
  413. pcie->phy = tegra_xusb_phy_get(TEGRA_XUSB_PADCTL_PCIE);
  414. if (pcie->phy) {
  415. err = tegra_xusb_phy_prepare(pcie->phy);
  416. if (err < 0) {
  417. error("failed to prepare PHY: %d", err);
  418. return err;
  419. }
  420. }
  421. err = tegra_pcie_parse_dt_ranges(fdt, node, pcie);
  422. if (err < 0) {
  423. error("failed to parse \"ranges\" property");
  424. return err;
  425. }
  426. fdt_for_each_subnode(fdt, subnode, node) {
  427. unsigned int index = 0, num_lanes = 0;
  428. struct tegra_pcie_port *port;
  429. err = tegra_pcie_parse_port_info(fdt, subnode, &index,
  430. &num_lanes);
  431. if (err < 0) {
  432. error("failed to obtain root port info");
  433. continue;
  434. }
  435. lanes |= num_lanes << (index << 3);
  436. if (!fdtdec_get_is_enabled(fdt, subnode))
  437. continue;
  438. port = malloc(sizeof(*port));
  439. if (!port)
  440. continue;
  441. memset(port, 0, sizeof(*port));
  442. port->num_lanes = num_lanes;
  443. port->index = index;
  444. err = tegra_pcie_port_parse_dt(fdt, subnode, port);
  445. if (err < 0) {
  446. free(port);
  447. continue;
  448. }
  449. list_add_tail(&port->list, &pcie->ports);
  450. port->pcie = pcie;
  451. }
  452. err = tegra_pcie_get_xbar_config(fdt, node, lanes, &pcie->xbar);
  453. if (err < 0) {
  454. error("invalid lane configuration");
  455. return err;
  456. }
  457. return 0;
  458. }
  459. int __weak tegra_pcie_board_init(void)
  460. {
  461. return 0;
  462. }
  463. static int tegra_pcie_power_on(struct tegra_pcie *pcie)
  464. {
  465. const struct tegra_pcie_soc *soc = pcie->soc;
  466. unsigned long value;
  467. int err;
  468. /* reset PCIEXCLK logic, AFI controller and PCIe controller */
  469. reset_set_enable(PERIPH_ID_PCIEXCLK, 1);
  470. reset_set_enable(PERIPH_ID_AFI, 1);
  471. reset_set_enable(PERIPH_ID_PCIE, 1);
  472. err = tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
  473. if (err < 0) {
  474. error("failed to power off PCIe partition: %d", err);
  475. return err;
  476. }
  477. tegra_pcie_board_init();
  478. err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
  479. PERIPH_ID_PCIE);
  480. if (err < 0) {
  481. error("failed to power up PCIe partition: %d", err);
  482. return err;
  483. }
  484. /* take AFI controller out of reset */
  485. reset_set_enable(PERIPH_ID_AFI, 0);
  486. /* enable AFI clock */
  487. clock_enable(PERIPH_ID_AFI);
  488. if (soc->has_cml_clk) {
  489. /* enable CML clock */
  490. value = readl(NV_PA_CLK_RST_BASE + 0x48c);
  491. value |= (1 << 0);
  492. value &= ~(1 << 1);
  493. writel(value, NV_PA_CLK_RST_BASE + 0x48c);
  494. }
  495. err = tegra_plle_enable();
  496. if (err < 0) {
  497. error("failed to enable PLLE: %d\n", err);
  498. return err;
  499. }
  500. return 0;
  501. }
  502. static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
  503. {
  504. const struct tegra_pcie_soc *soc = pcie->soc;
  505. unsigned long start = get_timer(0);
  506. u32 value;
  507. while (get_timer(start) < timeout) {
  508. value = pads_readl(pcie, soc->pads_pll_ctl);
  509. if (value & PADS_PLL_CTL_LOCKDET)
  510. return 0;
  511. }
  512. return -ETIMEDOUT;
  513. }
  514. static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
  515. {
  516. const struct tegra_pcie_soc *soc = pcie->soc;
  517. u32 value;
  518. int err;
  519. /* initialize internal PHY, enable up to 16 PCIe lanes */
  520. pads_writel(pcie, 0, PADS_CTL_SEL);
  521. /* override IDDQ to 1 on all 4 lanes */
  522. value = pads_readl(pcie, PADS_CTL);
  523. value |= PADS_CTL_IDDQ_1L;
  524. pads_writel(pcie, value, PADS_CTL);
  525. /*
  526. * Set up PHY PLL inputs select PLLE output as refclock, set TX
  527. * ref sel to div10 (not div5).
  528. */
  529. value = pads_readl(pcie, soc->pads_pll_ctl);
  530. value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
  531. value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
  532. pads_writel(pcie, value, soc->pads_pll_ctl);
  533. /* reset PLL */
  534. value = pads_readl(pcie, soc->pads_pll_ctl);
  535. value &= ~PADS_PLL_CTL_RST_B4SM;
  536. pads_writel(pcie, value, soc->pads_pll_ctl);
  537. udelay(20);
  538. /* take PLL out of reset */
  539. value = pads_readl(pcie, soc->pads_pll_ctl);
  540. value |= PADS_PLL_CTL_RST_B4SM;
  541. pads_writel(pcie, value, soc->pads_pll_ctl);
  542. /* configure the reference clock driver */
  543. value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
  544. pads_writel(pcie, value, PADS_REFCLK_CFG0);
  545. if (soc->num_ports > 2)
  546. pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
  547. /* wait for the PLL to lock */
  548. err = tegra_pcie_pll_wait(pcie, 500);
  549. if (err < 0) {
  550. error("PLL failed to lock: %d", err);
  551. return err;
  552. }
  553. /* turn off IDDQ override */
  554. value = pads_readl(pcie, PADS_CTL);
  555. value &= ~PADS_CTL_IDDQ_1L;
  556. pads_writel(pcie, value, PADS_CTL);
  557. /* enable TX/RX data */
  558. value = pads_readl(pcie, PADS_CTL);
  559. value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
  560. pads_writel(pcie, value, PADS_CTL);
  561. return 0;
  562. }
  563. static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
  564. {
  565. const struct tegra_pcie_soc *soc = pcie->soc;
  566. struct tegra_pcie_port *port;
  567. u32 value;
  568. int err;
  569. if (pcie->phy) {
  570. value = afi_readl(pcie, AFI_PLLE_CONTROL);
  571. value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
  572. value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
  573. afi_writel(pcie, value, AFI_PLLE_CONTROL);
  574. }
  575. if (soc->has_pex_bias_ctrl)
  576. afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
  577. value = afi_readl(pcie, AFI_PCIE_CONFIG);
  578. value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
  579. value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar;
  580. list_for_each_entry(port, &pcie->ports, list)
  581. value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
  582. afi_writel(pcie, value, AFI_PCIE_CONFIG);
  583. value = afi_readl(pcie, AFI_FUSE);
  584. if (soc->has_gen2)
  585. value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
  586. else
  587. value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
  588. afi_writel(pcie, value, AFI_FUSE);
  589. if (pcie->phy)
  590. err = tegra_xusb_phy_enable(pcie->phy);
  591. else
  592. err = tegra_pcie_phy_enable(pcie);
  593. if (err < 0) {
  594. error("failed to power on PHY: %d\n", err);
  595. return err;
  596. }
  597. /* take the PCIEXCLK logic out of reset */
  598. reset_set_enable(PERIPH_ID_PCIEXCLK, 0);
  599. /* finally enable PCIe */
  600. value = afi_readl(pcie, AFI_CONFIGURATION);
  601. value |= AFI_CONFIGURATION_EN_FPCI;
  602. afi_writel(pcie, value, AFI_CONFIGURATION);
  603. /* disable all interrupts */
  604. afi_writel(pcie, 0, AFI_AFI_INTR_ENABLE);
  605. afi_writel(pcie, 0, AFI_SM_INTR_ENABLE);
  606. afi_writel(pcie, 0, AFI_INTR_MASK);
  607. afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
  608. return 0;
  609. }
  610. static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
  611. {
  612. unsigned long fpci, axi, size;
  613. /* BAR 0: type 1 extended configuration space */
  614. fpci = 0xfe100000;
  615. size = fdt_resource_size(&pcie->cs);
  616. axi = pcie->cs.start;
  617. afi_writel(pcie, axi, AFI_AXI_BAR0_START);
  618. afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
  619. afi_writel(pcie, fpci, AFI_FPCI_BAR0);
  620. /* BAR 1: downstream I/O */
  621. fpci = 0xfdfc0000;
  622. size = fdt_resource_size(&pcie->io);
  623. axi = pcie->io.start;
  624. afi_writel(pcie, axi, AFI_AXI_BAR1_START);
  625. afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
  626. afi_writel(pcie, fpci, AFI_FPCI_BAR1);
  627. /* BAR 2: prefetchable memory */
  628. fpci = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
  629. size = fdt_resource_size(&pcie->prefetch);
  630. axi = pcie->prefetch.start;
  631. afi_writel(pcie, axi, AFI_AXI_BAR2_START);
  632. afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
  633. afi_writel(pcie, fpci, AFI_FPCI_BAR2);
  634. /* BAR 3: non-prefetchable memory */
  635. fpci = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
  636. size = fdt_resource_size(&pcie->mem);
  637. axi = pcie->mem.start;
  638. afi_writel(pcie, axi, AFI_AXI_BAR3_START);
  639. afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
  640. afi_writel(pcie, fpci, AFI_FPCI_BAR3);
  641. /* NULL out the remaining BARs as they are not used */
  642. afi_writel(pcie, 0, AFI_AXI_BAR4_START);
  643. afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
  644. afi_writel(pcie, 0, AFI_FPCI_BAR4);
  645. afi_writel(pcie, 0, AFI_AXI_BAR5_START);
  646. afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
  647. afi_writel(pcie, 0, AFI_FPCI_BAR5);
  648. /* map all upstream transactions as uncached */
  649. afi_writel(pcie, NV_PA_SDRAM_BASE, AFI_CACHE_BAR0_ST);
  650. afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
  651. afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
  652. afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
  653. /* MSI translations are setup only when needed */
  654. afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
  655. afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
  656. afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
  657. afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
  658. }
  659. static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
  660. {
  661. unsigned long ret = 0;
  662. switch (port->index) {
  663. case 0:
  664. ret = AFI_PEX0_CTRL;
  665. break;
  666. case 1:
  667. ret = AFI_PEX1_CTRL;
  668. break;
  669. case 2:
  670. ret = AFI_PEX2_CTRL;
  671. break;
  672. }
  673. return ret;
  674. }
  675. static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
  676. {
  677. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  678. unsigned long value;
  679. /* pulse reset signel */
  680. value = afi_readl(port->pcie, ctrl);
  681. value &= ~AFI_PEX_CTRL_RST;
  682. afi_writel(port->pcie, value, ctrl);
  683. udelay(2000);
  684. value = afi_readl(port->pcie, ctrl);
  685. value |= AFI_PEX_CTRL_RST;
  686. afi_writel(port->pcie, value, ctrl);
  687. }
  688. static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
  689. {
  690. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  691. unsigned long value;
  692. /* enable reference clock */
  693. value = afi_readl(port->pcie, ctrl);
  694. value |= AFI_PEX_CTRL_REFCLK_EN;
  695. if (port->pcie->soc->has_pex_clkreq_en)
  696. value |= AFI_PEX_CTRL_CLKREQ_EN;
  697. value |= AFI_PEX_CTRL_OVERRIDE_EN;
  698. afi_writel(port->pcie, value, ctrl);
  699. tegra_pcie_port_reset(port);
  700. }
  701. static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
  702. {
  703. unsigned int retries = 3;
  704. unsigned long value;
  705. value = rp_readl(port, RP_PRIV_MISC);
  706. value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
  707. value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
  708. rp_writel(port, value, RP_PRIV_MISC);
  709. do {
  710. unsigned int timeout = 200;
  711. do {
  712. value = rp_readl(port, RP_VEND_XP);
  713. if (value & RP_VEND_XP_DL_UP)
  714. break;
  715. udelay(2000);
  716. } while (--timeout);
  717. if (!timeout) {
  718. debug("link %u down, retrying\n", port->index);
  719. goto retry;
  720. }
  721. timeout = 200;
  722. do {
  723. value = rp_readl(port, RP_LINK_CONTROL_STATUS);
  724. if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
  725. return true;
  726. udelay(2000);
  727. } while (--timeout);
  728. retry:
  729. tegra_pcie_port_reset(port);
  730. } while (--retries);
  731. return false;
  732. }
  733. static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
  734. {
  735. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  736. unsigned long value;
  737. /* assert port reset */
  738. value = afi_readl(port->pcie, ctrl);
  739. value &= ~AFI_PEX_CTRL_RST;
  740. afi_writel(port->pcie, value, ctrl);
  741. /* disable reference clock */
  742. value = afi_readl(port->pcie, ctrl);
  743. value &= ~AFI_PEX_CTRL_REFCLK_EN;
  744. afi_writel(port->pcie, value, ctrl);
  745. }
  746. static void tegra_pcie_port_free(struct tegra_pcie_port *port)
  747. {
  748. list_del(&port->list);
  749. free(port);
  750. }
  751. static int tegra_pcie_enable(struct tegra_pcie *pcie)
  752. {
  753. struct tegra_pcie_port *port, *tmp;
  754. list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
  755. debug("probing port %u, using %u lanes\n", port->index,
  756. port->num_lanes);
  757. tegra_pcie_port_enable(port);
  758. if (tegra_pcie_port_check_link(port))
  759. continue;
  760. debug("link %u down, ignoring\n", port->index);
  761. tegra_pcie_port_disable(port);
  762. tegra_pcie_port_free(port);
  763. }
  764. return 0;
  765. }
  766. static const struct tegra_pcie_soc tegra20_pcie_soc = {
  767. .num_ports = 2,
  768. .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
  769. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
  770. .has_pex_clkreq_en = false,
  771. .has_pex_bias_ctrl = false,
  772. .has_cml_clk = false,
  773. .has_gen2 = false,
  774. };
  775. static const struct tegra_pcie_soc tegra30_pcie_soc = {
  776. .num_ports = 3,
  777. .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
  778. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
  779. .has_pex_clkreq_en = true,
  780. .has_pex_bias_ctrl = true,
  781. .has_cml_clk = true,
  782. .has_gen2 = false,
  783. };
  784. static const struct tegra_pcie_soc tegra124_pcie_soc = {
  785. .num_ports = 2,
  786. .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
  787. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
  788. .has_pex_clkreq_en = true,
  789. .has_pex_bias_ctrl = true,
  790. .has_cml_clk = true,
  791. .has_gen2 = true,
  792. };
  793. static int process_nodes(const void *fdt, int nodes[], unsigned int count)
  794. {
  795. unsigned int i;
  796. for (i = 0; i < count; i++) {
  797. const struct tegra_pcie_soc *soc;
  798. struct tegra_pcie *pcie;
  799. enum fdt_compat_id id;
  800. int err;
  801. if (!fdtdec_get_is_enabled(fdt, nodes[i]))
  802. continue;
  803. id = fdtdec_lookup(fdt, nodes[i]);
  804. switch (id) {
  805. case COMPAT_NVIDIA_TEGRA20_PCIE:
  806. soc = &tegra20_pcie_soc;
  807. break;
  808. case COMPAT_NVIDIA_TEGRA30_PCIE:
  809. soc = &tegra30_pcie_soc;
  810. break;
  811. case COMPAT_NVIDIA_TEGRA124_PCIE:
  812. soc = &tegra124_pcie_soc;
  813. break;
  814. default:
  815. error("unsupported compatible: %s",
  816. fdtdec_get_compatible(id));
  817. continue;
  818. }
  819. pcie = malloc(sizeof(*pcie));
  820. if (!pcie) {
  821. error("failed to allocate controller");
  822. continue;
  823. }
  824. memset(pcie, 0, sizeof(*pcie));
  825. pcie->soc = soc;
  826. INIT_LIST_HEAD(&pcie->ports);
  827. err = tegra_pcie_parse_dt(fdt, nodes[i], pcie);
  828. if (err < 0) {
  829. free(pcie);
  830. continue;
  831. }
  832. err = tegra_pcie_power_on(pcie);
  833. if (err < 0) {
  834. error("failed to power on");
  835. continue;
  836. }
  837. err = tegra_pcie_enable_controller(pcie);
  838. if (err < 0) {
  839. error("failed to enable controller");
  840. continue;
  841. }
  842. tegra_pcie_setup_translations(pcie);
  843. err = tegra_pcie_enable(pcie);
  844. if (err < 0) {
  845. error("failed to enable PCIe");
  846. continue;
  847. }
  848. pcie->hose.first_busno = 0;
  849. pcie->hose.current_busno = 0;
  850. pcie->hose.last_busno = 0;
  851. pci_set_region(&pcie->hose.regions[0], NV_PA_SDRAM_BASE,
  852. NV_PA_SDRAM_BASE, gd->ram_size,
  853. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  854. pci_set_region(&pcie->hose.regions[1], pcie->io.start,
  855. pcie->io.start, fdt_resource_size(&pcie->io),
  856. PCI_REGION_IO);
  857. pci_set_region(&pcie->hose.regions[2], pcie->mem.start,
  858. pcie->mem.start, fdt_resource_size(&pcie->mem),
  859. PCI_REGION_MEM);
  860. pci_set_region(&pcie->hose.regions[3], pcie->prefetch.start,
  861. pcie->prefetch.start,
  862. fdt_resource_size(&pcie->prefetch),
  863. PCI_REGION_MEM | PCI_REGION_PREFETCH);
  864. pcie->hose.region_count = 4;
  865. pci_set_ops(&pcie->hose,
  866. pci_hose_read_config_byte_via_dword,
  867. pci_hose_read_config_word_via_dword,
  868. tegra_pcie_read_conf,
  869. pci_hose_write_config_byte_via_dword,
  870. pci_hose_write_config_word_via_dword,
  871. tegra_pcie_write_conf);
  872. pci_register_hose(&pcie->hose);
  873. #ifdef CONFIG_PCI_SCAN_SHOW
  874. printf("PCI: Enumerating devices...\n");
  875. printf("---------------------------------------\n");
  876. printf(" Device ID Description\n");
  877. printf(" ------ -- -----------\n");
  878. #endif
  879. pcie->hose.last_busno = pci_hose_scan(&pcie->hose);
  880. }
  881. return 0;
  882. }
  883. void pci_init_board(void)
  884. {
  885. const void *fdt = gd->fdt_blob;
  886. int count, nodes[1];
  887. count = fdtdec_find_aliases_for_id(fdt, "pcie-controller",
  888. COMPAT_NVIDIA_TEGRA124_PCIE,
  889. nodes, ARRAY_SIZE(nodes));
  890. if (process_nodes(fdt, nodes, count))
  891. return;
  892. count = fdtdec_find_aliases_for_id(fdt, "pcie-controller",
  893. COMPAT_NVIDIA_TEGRA30_PCIE,
  894. nodes, ARRAY_SIZE(nodes));
  895. if (process_nodes(fdt, nodes, count))
  896. return;
  897. count = fdtdec_find_aliases_for_id(fdt, "pcie-controller",
  898. COMPAT_NVIDIA_TEGRA20_PCIE,
  899. nodes, ARRAY_SIZE(nodes));
  900. if (process_nodes(fdt, nodes, count))
  901. return;
  902. }
  903. int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
  904. {
  905. if (PCI_BUS(dev) != 0 && PCI_DEV(dev) > 0)
  906. return 1;
  907. return 0;
  908. }