pci_tegra.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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: %pa-%pa\n", &pcie->io.start, &pcie->io.end);
  363. debug(" non-prefetchable memory: %pa-%pa\n", &pcie->mem.start,
  364. &pcie->mem.end);
  365. debug(" prefetchable memory: %pa-%pa\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. int err;
  375. err = fdtdec_get_int(fdt, node, "nvidia,num-lanes", 0);
  376. if (err < 0) {
  377. error("failed to parse \"nvidia,num-lanes\" property");
  378. return err;
  379. }
  380. *lanes = err;
  381. err = fdtdec_get_pci_addr(fdt, node, 0, "reg", &addr);
  382. if (err < 0) {
  383. error("failed to parse \"reg\" property");
  384. return err;
  385. }
  386. *index = PCI_DEV(addr.phys_hi) - 1;
  387. return 0;
  388. }
  389. static int tegra_pcie_parse_dt(const void *fdt, int node,
  390. struct tegra_pcie *pcie)
  391. {
  392. int err, subnode;
  393. u32 lanes = 0;
  394. err = fdt_get_named_resource(fdt, node, "reg", "reg-names", "pads",
  395. &pcie->pads);
  396. if (err < 0) {
  397. error("resource \"pads\" not found");
  398. return err;
  399. }
  400. err = fdt_get_named_resource(fdt, node, "reg", "reg-names", "afi",
  401. &pcie->afi);
  402. if (err < 0) {
  403. error("resource \"afi\" not found");
  404. return err;
  405. }
  406. err = fdt_get_named_resource(fdt, node, "reg", "reg-names", "cs",
  407. &pcie->cs);
  408. if (err < 0) {
  409. error("resource \"cs\" not found");
  410. return err;
  411. }
  412. pcie->phy = tegra_xusb_phy_get(TEGRA_XUSB_PADCTL_PCIE);
  413. if (pcie->phy) {
  414. err = tegra_xusb_phy_prepare(pcie->phy);
  415. if (err < 0) {
  416. error("failed to prepare PHY: %d", err);
  417. return err;
  418. }
  419. }
  420. err = tegra_pcie_parse_dt_ranges(fdt, node, pcie);
  421. if (err < 0) {
  422. error("failed to parse \"ranges\" property");
  423. return err;
  424. }
  425. fdt_for_each_subnode(fdt, subnode, node) {
  426. unsigned int index = 0, num_lanes = 0;
  427. struct tegra_pcie_port *port;
  428. err = tegra_pcie_parse_port_info(fdt, subnode, &index,
  429. &num_lanes);
  430. if (err < 0) {
  431. error("failed to obtain root port info");
  432. continue;
  433. }
  434. lanes |= num_lanes << (index << 3);
  435. if (!fdtdec_get_is_enabled(fdt, subnode))
  436. continue;
  437. port = malloc(sizeof(*port));
  438. if (!port)
  439. continue;
  440. memset(port, 0, sizeof(*port));
  441. port->num_lanes = num_lanes;
  442. port->index = index;
  443. err = tegra_pcie_port_parse_dt(fdt, subnode, port);
  444. if (err < 0) {
  445. free(port);
  446. continue;
  447. }
  448. list_add_tail(&port->list, &pcie->ports);
  449. port->pcie = pcie;
  450. }
  451. err = tegra_pcie_get_xbar_config(fdt, node, lanes, &pcie->xbar);
  452. if (err < 0) {
  453. error("invalid lane configuration");
  454. return err;
  455. }
  456. return 0;
  457. }
  458. int __weak tegra_pcie_board_init(void)
  459. {
  460. return 0;
  461. }
  462. static int tegra_pcie_power_on(struct tegra_pcie *pcie)
  463. {
  464. const struct tegra_pcie_soc *soc = pcie->soc;
  465. unsigned long value;
  466. int err;
  467. /* reset PCIEXCLK logic, AFI controller and PCIe controller */
  468. reset_set_enable(PERIPH_ID_PCIEXCLK, 1);
  469. reset_set_enable(PERIPH_ID_AFI, 1);
  470. reset_set_enable(PERIPH_ID_PCIE, 1);
  471. err = tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
  472. if (err < 0) {
  473. error("failed to power off PCIe partition: %d", err);
  474. return err;
  475. }
  476. tegra_pcie_board_init();
  477. err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
  478. PERIPH_ID_PCIE);
  479. if (err < 0) {
  480. error("failed to power up PCIe partition: %d", err);
  481. return err;
  482. }
  483. /* take AFI controller out of reset */
  484. reset_set_enable(PERIPH_ID_AFI, 0);
  485. /* enable AFI clock */
  486. clock_enable(PERIPH_ID_AFI);
  487. if (soc->has_cml_clk) {
  488. /* enable CML clock */
  489. value = readl(NV_PA_CLK_RST_BASE + 0x48c);
  490. value |= (1 << 0);
  491. value &= ~(1 << 1);
  492. writel(value, NV_PA_CLK_RST_BASE + 0x48c);
  493. }
  494. err = tegra_plle_enable();
  495. if (err < 0) {
  496. error("failed to enable PLLE: %d\n", err);
  497. return err;
  498. }
  499. return 0;
  500. }
  501. static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
  502. {
  503. const struct tegra_pcie_soc *soc = pcie->soc;
  504. unsigned long start = get_timer(0);
  505. u32 value;
  506. while (get_timer(start) < timeout) {
  507. value = pads_readl(pcie, soc->pads_pll_ctl);
  508. if (value & PADS_PLL_CTL_LOCKDET)
  509. return 0;
  510. }
  511. return -ETIMEDOUT;
  512. }
  513. static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
  514. {
  515. const struct tegra_pcie_soc *soc = pcie->soc;
  516. u32 value;
  517. int err;
  518. /* initialize internal PHY, enable up to 16 PCIe lanes */
  519. pads_writel(pcie, 0, PADS_CTL_SEL);
  520. /* override IDDQ to 1 on all 4 lanes */
  521. value = pads_readl(pcie, PADS_CTL);
  522. value |= PADS_CTL_IDDQ_1L;
  523. pads_writel(pcie, value, PADS_CTL);
  524. /*
  525. * Set up PHY PLL inputs select PLLE output as refclock, set TX
  526. * ref sel to div10 (not div5).
  527. */
  528. value = pads_readl(pcie, soc->pads_pll_ctl);
  529. value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
  530. value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
  531. pads_writel(pcie, value, soc->pads_pll_ctl);
  532. /* reset PLL */
  533. value = pads_readl(pcie, soc->pads_pll_ctl);
  534. value &= ~PADS_PLL_CTL_RST_B4SM;
  535. pads_writel(pcie, value, soc->pads_pll_ctl);
  536. udelay(20);
  537. /* take PLL out of reset */
  538. value = pads_readl(pcie, soc->pads_pll_ctl);
  539. value |= PADS_PLL_CTL_RST_B4SM;
  540. pads_writel(pcie, value, soc->pads_pll_ctl);
  541. /* configure the reference clock driver */
  542. value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
  543. pads_writel(pcie, value, PADS_REFCLK_CFG0);
  544. if (soc->num_ports > 2)
  545. pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
  546. /* wait for the PLL to lock */
  547. err = tegra_pcie_pll_wait(pcie, 500);
  548. if (err < 0) {
  549. error("PLL failed to lock: %d", err);
  550. return err;
  551. }
  552. /* turn off IDDQ override */
  553. value = pads_readl(pcie, PADS_CTL);
  554. value &= ~PADS_CTL_IDDQ_1L;
  555. pads_writel(pcie, value, PADS_CTL);
  556. /* enable TX/RX data */
  557. value = pads_readl(pcie, PADS_CTL);
  558. value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
  559. pads_writel(pcie, value, PADS_CTL);
  560. return 0;
  561. }
  562. static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
  563. {
  564. const struct tegra_pcie_soc *soc = pcie->soc;
  565. struct tegra_pcie_port *port;
  566. u32 value;
  567. int err;
  568. if (pcie->phy) {
  569. value = afi_readl(pcie, AFI_PLLE_CONTROL);
  570. value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
  571. value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
  572. afi_writel(pcie, value, AFI_PLLE_CONTROL);
  573. }
  574. if (soc->has_pex_bias_ctrl)
  575. afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
  576. value = afi_readl(pcie, AFI_PCIE_CONFIG);
  577. value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
  578. value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar;
  579. list_for_each_entry(port, &pcie->ports, list)
  580. value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
  581. afi_writel(pcie, value, AFI_PCIE_CONFIG);
  582. value = afi_readl(pcie, AFI_FUSE);
  583. if (soc->has_gen2)
  584. value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
  585. else
  586. value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
  587. afi_writel(pcie, value, AFI_FUSE);
  588. if (pcie->phy)
  589. err = tegra_xusb_phy_enable(pcie->phy);
  590. else
  591. err = tegra_pcie_phy_enable(pcie);
  592. if (err < 0) {
  593. error("failed to power on PHY: %d\n", err);
  594. return err;
  595. }
  596. /* take the PCIEXCLK logic out of reset */
  597. reset_set_enable(PERIPH_ID_PCIEXCLK, 0);
  598. /* finally enable PCIe */
  599. value = afi_readl(pcie, AFI_CONFIGURATION);
  600. value |= AFI_CONFIGURATION_EN_FPCI;
  601. afi_writel(pcie, value, AFI_CONFIGURATION);
  602. /* disable all interrupts */
  603. afi_writel(pcie, 0, AFI_AFI_INTR_ENABLE);
  604. afi_writel(pcie, 0, AFI_SM_INTR_ENABLE);
  605. afi_writel(pcie, 0, AFI_INTR_MASK);
  606. afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
  607. return 0;
  608. }
  609. static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
  610. {
  611. unsigned long fpci, axi, size;
  612. /* BAR 0: type 1 extended configuration space */
  613. fpci = 0xfe100000;
  614. size = fdt_resource_size(&pcie->cs);
  615. axi = pcie->cs.start;
  616. afi_writel(pcie, axi, AFI_AXI_BAR0_START);
  617. afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
  618. afi_writel(pcie, fpci, AFI_FPCI_BAR0);
  619. /* BAR 1: downstream I/O */
  620. fpci = 0xfdfc0000;
  621. size = fdt_resource_size(&pcie->io);
  622. axi = pcie->io.start;
  623. afi_writel(pcie, axi, AFI_AXI_BAR1_START);
  624. afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
  625. afi_writel(pcie, fpci, AFI_FPCI_BAR1);
  626. /* BAR 2: prefetchable memory */
  627. fpci = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
  628. size = fdt_resource_size(&pcie->prefetch);
  629. axi = pcie->prefetch.start;
  630. afi_writel(pcie, axi, AFI_AXI_BAR2_START);
  631. afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
  632. afi_writel(pcie, fpci, AFI_FPCI_BAR2);
  633. /* BAR 3: non-prefetchable memory */
  634. fpci = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
  635. size = fdt_resource_size(&pcie->mem);
  636. axi = pcie->mem.start;
  637. afi_writel(pcie, axi, AFI_AXI_BAR3_START);
  638. afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
  639. afi_writel(pcie, fpci, AFI_FPCI_BAR3);
  640. /* NULL out the remaining BARs as they are not used */
  641. afi_writel(pcie, 0, AFI_AXI_BAR4_START);
  642. afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
  643. afi_writel(pcie, 0, AFI_FPCI_BAR4);
  644. afi_writel(pcie, 0, AFI_AXI_BAR5_START);
  645. afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
  646. afi_writel(pcie, 0, AFI_FPCI_BAR5);
  647. /* map all upstream transactions as uncached */
  648. afi_writel(pcie, NV_PA_SDRAM_BASE, AFI_CACHE_BAR0_ST);
  649. afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
  650. afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
  651. afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
  652. /* MSI translations are setup only when needed */
  653. afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
  654. afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
  655. afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
  656. afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
  657. }
  658. static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
  659. {
  660. unsigned long ret = 0;
  661. switch (port->index) {
  662. case 0:
  663. ret = AFI_PEX0_CTRL;
  664. break;
  665. case 1:
  666. ret = AFI_PEX1_CTRL;
  667. break;
  668. case 2:
  669. ret = AFI_PEX2_CTRL;
  670. break;
  671. }
  672. return ret;
  673. }
  674. static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
  675. {
  676. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  677. unsigned long value;
  678. /* pulse reset signel */
  679. value = afi_readl(port->pcie, ctrl);
  680. value &= ~AFI_PEX_CTRL_RST;
  681. afi_writel(port->pcie, value, ctrl);
  682. udelay(2000);
  683. value = afi_readl(port->pcie, ctrl);
  684. value |= AFI_PEX_CTRL_RST;
  685. afi_writel(port->pcie, value, ctrl);
  686. }
  687. static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
  688. {
  689. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  690. unsigned long value;
  691. /* enable reference clock */
  692. value = afi_readl(port->pcie, ctrl);
  693. value |= AFI_PEX_CTRL_REFCLK_EN;
  694. if (port->pcie->soc->has_pex_clkreq_en)
  695. value |= AFI_PEX_CTRL_CLKREQ_EN;
  696. value |= AFI_PEX_CTRL_OVERRIDE_EN;
  697. afi_writel(port->pcie, value, ctrl);
  698. tegra_pcie_port_reset(port);
  699. }
  700. static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
  701. {
  702. unsigned int retries = 3;
  703. unsigned long value;
  704. value = rp_readl(port, RP_PRIV_MISC);
  705. value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
  706. value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
  707. rp_writel(port, value, RP_PRIV_MISC);
  708. do {
  709. unsigned int timeout = 200;
  710. do {
  711. value = rp_readl(port, RP_VEND_XP);
  712. if (value & RP_VEND_XP_DL_UP)
  713. break;
  714. udelay(2000);
  715. } while (--timeout);
  716. if (!timeout) {
  717. debug("link %u down, retrying\n", port->index);
  718. goto retry;
  719. }
  720. timeout = 200;
  721. do {
  722. value = rp_readl(port, RP_LINK_CONTROL_STATUS);
  723. if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
  724. return true;
  725. udelay(2000);
  726. } while (--timeout);
  727. retry:
  728. tegra_pcie_port_reset(port);
  729. } while (--retries);
  730. return false;
  731. }
  732. static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
  733. {
  734. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  735. unsigned long value;
  736. /* assert port reset */
  737. value = afi_readl(port->pcie, ctrl);
  738. value &= ~AFI_PEX_CTRL_RST;
  739. afi_writel(port->pcie, value, ctrl);
  740. /* disable reference clock */
  741. value = afi_readl(port->pcie, ctrl);
  742. value &= ~AFI_PEX_CTRL_REFCLK_EN;
  743. afi_writel(port->pcie, value, ctrl);
  744. }
  745. static void tegra_pcie_port_free(struct tegra_pcie_port *port)
  746. {
  747. list_del(&port->list);
  748. free(port);
  749. }
  750. static int tegra_pcie_enable(struct tegra_pcie *pcie)
  751. {
  752. struct tegra_pcie_port *port, *tmp;
  753. list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
  754. debug("probing port %u, using %u lanes\n", port->index,
  755. port->num_lanes);
  756. tegra_pcie_port_enable(port);
  757. if (tegra_pcie_port_check_link(port))
  758. continue;
  759. debug("link %u down, ignoring\n", port->index);
  760. tegra_pcie_port_disable(port);
  761. tegra_pcie_port_free(port);
  762. }
  763. return 0;
  764. }
  765. static const struct tegra_pcie_soc tegra20_pcie_soc = {
  766. .num_ports = 2,
  767. .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
  768. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
  769. .has_pex_clkreq_en = false,
  770. .has_pex_bias_ctrl = false,
  771. .has_cml_clk = false,
  772. .has_gen2 = false,
  773. };
  774. static const struct tegra_pcie_soc tegra30_pcie_soc = {
  775. .num_ports = 3,
  776. .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
  777. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
  778. .has_pex_clkreq_en = true,
  779. .has_pex_bias_ctrl = true,
  780. .has_cml_clk = true,
  781. .has_gen2 = false,
  782. };
  783. static const struct tegra_pcie_soc tegra124_pcie_soc = {
  784. .num_ports = 2,
  785. .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
  786. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
  787. .has_pex_clkreq_en = true,
  788. .has_pex_bias_ctrl = true,
  789. .has_cml_clk = true,
  790. .has_gen2 = true,
  791. };
  792. static int process_nodes(const void *fdt, int nodes[], unsigned int count)
  793. {
  794. unsigned int i;
  795. for (i = 0; i < count; i++) {
  796. const struct tegra_pcie_soc *soc;
  797. struct tegra_pcie *pcie;
  798. enum fdt_compat_id id;
  799. int err;
  800. if (!fdtdec_get_is_enabled(fdt, nodes[i]))
  801. continue;
  802. id = fdtdec_lookup(fdt, nodes[i]);
  803. switch (id) {
  804. case COMPAT_NVIDIA_TEGRA20_PCIE:
  805. soc = &tegra20_pcie_soc;
  806. break;
  807. case COMPAT_NVIDIA_TEGRA30_PCIE:
  808. soc = &tegra30_pcie_soc;
  809. break;
  810. case COMPAT_NVIDIA_TEGRA124_PCIE:
  811. soc = &tegra124_pcie_soc;
  812. break;
  813. default:
  814. error("unsupported compatible: %s",
  815. fdtdec_get_compatible(id));
  816. continue;
  817. }
  818. pcie = malloc(sizeof(*pcie));
  819. if (!pcie) {
  820. error("failed to allocate controller");
  821. continue;
  822. }
  823. memset(pcie, 0, sizeof(*pcie));
  824. pcie->soc = soc;
  825. INIT_LIST_HEAD(&pcie->ports);
  826. err = tegra_pcie_parse_dt(fdt, nodes[i], pcie);
  827. if (err < 0) {
  828. free(pcie);
  829. continue;
  830. }
  831. err = tegra_pcie_power_on(pcie);
  832. if (err < 0) {
  833. error("failed to power on");
  834. continue;
  835. }
  836. err = tegra_pcie_enable_controller(pcie);
  837. if (err < 0) {
  838. error("failed to enable controller");
  839. continue;
  840. }
  841. tegra_pcie_setup_translations(pcie);
  842. err = tegra_pcie_enable(pcie);
  843. if (err < 0) {
  844. error("failed to enable PCIe");
  845. continue;
  846. }
  847. pcie->hose.first_busno = 0;
  848. pcie->hose.current_busno = 0;
  849. pcie->hose.last_busno = 0;
  850. pci_set_region(&pcie->hose.regions[0], NV_PA_SDRAM_BASE,
  851. NV_PA_SDRAM_BASE, gd->ram_size,
  852. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  853. pci_set_region(&pcie->hose.regions[1], pcie->io.start,
  854. pcie->io.start, fdt_resource_size(&pcie->io),
  855. PCI_REGION_IO);
  856. pci_set_region(&pcie->hose.regions[2], pcie->mem.start,
  857. pcie->mem.start, fdt_resource_size(&pcie->mem),
  858. PCI_REGION_MEM);
  859. pci_set_region(&pcie->hose.regions[3], pcie->prefetch.start,
  860. pcie->prefetch.start,
  861. fdt_resource_size(&pcie->prefetch),
  862. PCI_REGION_MEM | PCI_REGION_PREFETCH);
  863. pcie->hose.region_count = 4;
  864. pci_set_ops(&pcie->hose,
  865. pci_hose_read_config_byte_via_dword,
  866. pci_hose_read_config_word_via_dword,
  867. tegra_pcie_read_conf,
  868. pci_hose_write_config_byte_via_dword,
  869. pci_hose_write_config_word_via_dword,
  870. tegra_pcie_write_conf);
  871. pci_register_hose(&pcie->hose);
  872. #ifdef CONFIG_PCI_SCAN_SHOW
  873. printf("PCI: Enumerating devices...\n");
  874. printf("---------------------------------------\n");
  875. printf(" Device ID Description\n");
  876. printf(" ------ -- -----------\n");
  877. #endif
  878. pcie->hose.last_busno = pci_hose_scan(&pcie->hose);
  879. }
  880. return 0;
  881. }
  882. void pci_init_board(void)
  883. {
  884. const void *fdt = gd->fdt_blob;
  885. int count, nodes[1];
  886. count = fdtdec_find_aliases_for_id(fdt, "pcie-controller",
  887. COMPAT_NVIDIA_TEGRA124_PCIE,
  888. nodes, ARRAY_SIZE(nodes));
  889. if (process_nodes(fdt, nodes, count))
  890. return;
  891. count = fdtdec_find_aliases_for_id(fdt, "pcie-controller",
  892. COMPAT_NVIDIA_TEGRA30_PCIE,
  893. nodes, ARRAY_SIZE(nodes));
  894. if (process_nodes(fdt, nodes, count))
  895. return;
  896. count = fdtdec_find_aliases_for_id(fdt, "pcie-controller",
  897. COMPAT_NVIDIA_TEGRA20_PCIE,
  898. nodes, ARRAY_SIZE(nodes));
  899. if (process_nodes(fdt, nodes, count))
  900. return;
  901. }
  902. int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
  903. {
  904. if (PCI_BUS(dev) != 0 && PCI_DEV(dev) > 0)
  905. return 1;
  906. return 0;
  907. }