4xx_pcie.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /*
  2. * (C) Copyright 2006 - 2007
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Copyright (c) 2005 Cisco Systems. All rights reserved.
  6. * Roland Dreier <rolandd@cisco.com>
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. */
  22. /* define DEBUG for debugging output (obviously ;-)) */
  23. #if 0
  24. #define DEBUG
  25. #endif
  26. #include <asm/processor.h>
  27. #include <asm-ppc/io.h>
  28. #include <ppc4xx.h>
  29. #include <common.h>
  30. #include <pci.h>
  31. #if (defined(CONFIG_440SPE) || defined(CONFIG_405EX)) && \
  32. defined(CONFIG_PCI)
  33. #include <asm/4xx_pcie.h>
  34. enum {
  35. PTYPE_ENDPOINT = 0x0,
  36. PTYPE_LEGACY_ENDPOINT = 0x1,
  37. PTYPE_ROOT_PORT = 0x4,
  38. LNKW_X1 = 0x1,
  39. LNKW_X4 = 0x4,
  40. LNKW_X8 = 0x8
  41. };
  42. static u8* pcie_get_base(struct pci_controller *hose, unsigned int devfn)
  43. {
  44. u8 *base = (u8*)hose->cfg_data;
  45. /* use local configuration space for the first bus */
  46. if (PCI_BUS(devfn) == 0) {
  47. if (hose->cfg_data == (u8*)CFG_PCIE0_CFGBASE)
  48. base = (u8*)CFG_PCIE0_XCFGBASE;
  49. if (hose->cfg_data == (u8*)CFG_PCIE1_CFGBASE)
  50. base = (u8*)CFG_PCIE1_XCFGBASE;
  51. #if CFG_PCIE_NR_PORTS > 2
  52. if (hose->cfg_data == (u8*)CFG_PCIE2_CFGBASE)
  53. base = (u8*)CFG_PCIE2_XCFGBASE;
  54. #endif
  55. }
  56. return base;
  57. }
  58. static void pcie_dmer_disable(void)
  59. {
  60. mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE),
  61. mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE)) | GPL_DMER_MASK_DISA);
  62. mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE),
  63. mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE)) | GPL_DMER_MASK_DISA);
  64. #if CFG_PCIE_NR_PORTS > 2
  65. mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE),
  66. mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE)) | GPL_DMER_MASK_DISA);
  67. #endif
  68. }
  69. static void pcie_dmer_enable(void)
  70. {
  71. mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE0_BASE),
  72. mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE)) & ~GPL_DMER_MASK_DISA);
  73. mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE1_BASE),
  74. mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE)) & ~GPL_DMER_MASK_DISA);
  75. #if CFG_PCIE_NR_PORTS > 2
  76. mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE2_BASE),
  77. mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE)) & ~GPL_DMER_MASK_DISA);
  78. #endif
  79. }
  80. static int pcie_read_config(struct pci_controller *hose, unsigned int devfn,
  81. int offset, int len, u32 *val) {
  82. u8 *address;
  83. *val = 0;
  84. /*
  85. * Bus numbers are relative to hose->first_busno
  86. */
  87. devfn -= PCI_BDF(hose->first_busno, 0, 0);
  88. /*
  89. * NOTICE: configuration space ranges are currenlty mapped only for
  90. * the first 16 buses, so such limit must be imposed. In case more
  91. * buses are required the TLB settings in board/amcc/<board>/init.S
  92. * need to be altered accordingly (one bus takes 1 MB of memory space).
  93. */
  94. if (PCI_BUS(devfn) >= 16)
  95. return 0;
  96. /*
  97. * Only single device/single function is supported for the primary and
  98. * secondary buses of the 440SPe host bridge.
  99. */
  100. if ((!((PCI_FUNC(devfn) == 0) && (PCI_DEV(devfn) == 0))) &&
  101. ((PCI_BUS(devfn) == 0) || (PCI_BUS(devfn) == 1)))
  102. return 0;
  103. address = pcie_get_base(hose, devfn);
  104. offset += devfn << 4;
  105. /*
  106. * Reading from configuration space of non-existing device can
  107. * generate transaction errors. For the read duration we suppress
  108. * assertion of machine check exceptions to avoid those.
  109. */
  110. pcie_dmer_disable ();
  111. debug("%s: cfg_data=%08x offset=%08x\n", __func__, hose->cfg_data, offset);
  112. switch (len) {
  113. case 1:
  114. *val = in_8(hose->cfg_data + offset);
  115. break;
  116. case 2:
  117. *val = in_le16((u16 *)(hose->cfg_data + offset));
  118. break;
  119. default:
  120. *val = in_le32((u32*)(hose->cfg_data + offset));
  121. break;
  122. }
  123. pcie_dmer_enable ();
  124. return 0;
  125. }
  126. static int pcie_write_config(struct pci_controller *hose, unsigned int devfn,
  127. int offset, int len, u32 val) {
  128. u8 *address;
  129. /*
  130. * Bus numbers are relative to hose->first_busno
  131. */
  132. devfn -= PCI_BDF(hose->first_busno, 0, 0);
  133. /*
  134. * Same constraints as in pcie_read_config().
  135. */
  136. if (PCI_BUS(devfn) >= 16)
  137. return 0;
  138. if ((!((PCI_FUNC(devfn) == 0) && (PCI_DEV(devfn) == 0))) &&
  139. ((PCI_BUS(devfn) == 0) || (PCI_BUS(devfn) == 1)))
  140. return 0;
  141. address = pcie_get_base(hose, devfn);
  142. offset += devfn << 4;
  143. /*
  144. * Suppress MCK exceptions, similar to pcie_read_config()
  145. */
  146. pcie_dmer_disable ();
  147. switch (len) {
  148. case 1:
  149. out_8(hose->cfg_data + offset, val);
  150. break;
  151. case 2:
  152. out_le16((u16 *)(hose->cfg_data + offset), val);
  153. break;
  154. default:
  155. out_le32((u32 *)(hose->cfg_data + offset), val);
  156. break;
  157. }
  158. pcie_dmer_enable ();
  159. return 0;
  160. }
  161. int pcie_read_config_byte(struct pci_controller *hose,pci_dev_t dev,int offset,u8 *val)
  162. {
  163. u32 v;
  164. int rv;
  165. rv = pcie_read_config(hose, dev, offset, 1, &v);
  166. *val = (u8)v;
  167. return rv;
  168. }
  169. int pcie_read_config_word(struct pci_controller *hose,pci_dev_t dev,int offset,u16 *val)
  170. {
  171. u32 v;
  172. int rv;
  173. rv = pcie_read_config(hose, dev, offset, 2, &v);
  174. *val = (u16)v;
  175. return rv;
  176. }
  177. int pcie_read_config_dword(struct pci_controller *hose,pci_dev_t dev,int offset,u32 *val)
  178. {
  179. u32 v;
  180. int rv;
  181. rv = pcie_read_config(hose, dev, offset, 3, &v);
  182. *val = (u32)v;
  183. return rv;
  184. }
  185. int pcie_write_config_byte(struct pci_controller *hose,pci_dev_t dev,int offset,u8 val)
  186. {
  187. return pcie_write_config(hose,(u32)dev,offset,1,val);
  188. }
  189. int pcie_write_config_word(struct pci_controller *hose,pci_dev_t dev,int offset,u16 val)
  190. {
  191. return pcie_write_config(hose,(u32)dev,offset,2,(u32 )val);
  192. }
  193. int pcie_write_config_dword(struct pci_controller *hose,pci_dev_t dev,int offset,u32 val)
  194. {
  195. return pcie_write_config(hose,(u32)dev,offset,3,(u32 )val);
  196. }
  197. #if defined(CONFIG_440SPE)
  198. static void ppc4xx_setup_utl(u32 port) {
  199. volatile void *utl_base = NULL;
  200. /*
  201. * Map UTL registers
  202. */
  203. switch (port) {
  204. case 0:
  205. mtdcr(DCRN_PEGPL_REGBAH(PCIE0), 0x0000000c);
  206. mtdcr(DCRN_PEGPL_REGBAL(PCIE0), 0x20000000);
  207. mtdcr(DCRN_PEGPL_REGMSK(PCIE0), 0x00007001);
  208. mtdcr(DCRN_PEGPL_SPECIAL(PCIE0), 0x68782800);
  209. break;
  210. case 1:
  211. mtdcr(DCRN_PEGPL_REGBAH(PCIE1), 0x0000000c);
  212. mtdcr(DCRN_PEGPL_REGBAL(PCIE1), 0x20001000);
  213. mtdcr(DCRN_PEGPL_REGMSK(PCIE1), 0x00007001);
  214. mtdcr(DCRN_PEGPL_SPECIAL(PCIE1), 0x68782800);
  215. break;
  216. case 2:
  217. mtdcr(DCRN_PEGPL_REGBAH(PCIE2), 0x0000000c);
  218. mtdcr(DCRN_PEGPL_REGBAL(PCIE2), 0x20002000);
  219. mtdcr(DCRN_PEGPL_REGMSK(PCIE2), 0x00007001);
  220. mtdcr(DCRN_PEGPL_SPECIAL(PCIE2), 0x68782800);
  221. break;
  222. }
  223. utl_base = (unsigned int *)(CFG_PCIE_BASE + 0x1000 * port);
  224. /*
  225. * Set buffer allocations and then assert VRB and TXE.
  226. */
  227. out_be32(utl_base + PEUTL_OUTTR, 0x08000000);
  228. out_be32(utl_base + PEUTL_INTR, 0x02000000);
  229. out_be32(utl_base + PEUTL_OPDBSZ, 0x10000000);
  230. out_be32(utl_base + PEUTL_PBBSZ, 0x53000000);
  231. out_be32(utl_base + PEUTL_IPHBSZ, 0x08000000);
  232. out_be32(utl_base + PEUTL_IPDBSZ, 0x10000000);
  233. out_be32(utl_base + PEUTL_RCIRQEN, 0x00f00000);
  234. out_be32(utl_base + PEUTL_PCTL, 0x80800066);
  235. }
  236. static int check_error(void)
  237. {
  238. u32 valPE0, valPE1, valPE2;
  239. int err = 0;
  240. /* SDR0_PEGPLLLCT1 reset */
  241. if (!(valPE0 = SDR_READ(PESDR0_PLLLCT1) & 0x01000000)) {
  242. printf("PCIE: SDR0_PEGPLLLCT1 reset error 0x%x\n", valPE0);
  243. }
  244. valPE0 = SDR_READ(PESDR0_RCSSET);
  245. valPE1 = SDR_READ(PESDR1_RCSSET);
  246. valPE2 = SDR_READ(PESDR2_RCSSET);
  247. /* SDR0_PExRCSSET rstgu */
  248. if (!(valPE0 & 0x01000000) ||
  249. !(valPE1 & 0x01000000) ||
  250. !(valPE2 & 0x01000000)) {
  251. printf("PCIE: SDR0_PExRCSSET rstgu error\n");
  252. err = -1;
  253. }
  254. /* SDR0_PExRCSSET rstdl */
  255. if (!(valPE0 & 0x00010000) ||
  256. !(valPE1 & 0x00010000) ||
  257. !(valPE2 & 0x00010000)) {
  258. printf("PCIE: SDR0_PExRCSSET rstdl error\n");
  259. err = -1;
  260. }
  261. /* SDR0_PExRCSSET rstpyn */
  262. if ((valPE0 & 0x00001000) ||
  263. (valPE1 & 0x00001000) ||
  264. (valPE2 & 0x00001000)) {
  265. printf("PCIE: SDR0_PExRCSSET rstpyn error\n");
  266. err = -1;
  267. }
  268. /* SDR0_PExRCSSET hldplb */
  269. if ((valPE0 & 0x10000000) ||
  270. (valPE1 & 0x10000000) ||
  271. (valPE2 & 0x10000000)) {
  272. printf("PCIE: SDR0_PExRCSSET hldplb error\n");
  273. err = -1;
  274. }
  275. /* SDR0_PExRCSSET rdy */
  276. if ((valPE0 & 0x00100000) ||
  277. (valPE1 & 0x00100000) ||
  278. (valPE2 & 0x00100000)) {
  279. printf("PCIE: SDR0_PExRCSSET rdy error\n");
  280. err = -1;
  281. }
  282. /* SDR0_PExRCSSET shutdown */
  283. if ((valPE0 & 0x00000100) ||
  284. (valPE1 & 0x00000100) ||
  285. (valPE2 & 0x00000100)) {
  286. printf("PCIE: SDR0_PExRCSSET shutdown error\n");
  287. err = -1;
  288. }
  289. return err;
  290. }
  291. /*
  292. * Initialize PCI Express core
  293. */
  294. int ppc4xx_init_pcie(void)
  295. {
  296. int time_out = 20;
  297. /* Set PLL clock receiver to LVPECL */
  298. SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) | 1 << 28);
  299. if (check_error())
  300. return -1;
  301. if (!(SDR_READ(PESDR0_PLLLCT2) & 0x10000))
  302. {
  303. printf("PCIE: PESDR_PLLCT2 resistance calibration failed (0x%08x)\n",
  304. SDR_READ(PESDR0_PLLLCT2));
  305. return -1;
  306. }
  307. /* De-assert reset of PCIe PLL, wait for lock */
  308. SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) & ~(1 << 24));
  309. udelay(3);
  310. while (time_out) {
  311. if (!(SDR_READ(PESDR0_PLLLCT3) & 0x10000000)) {
  312. time_out--;
  313. udelay(1);
  314. } else
  315. break;
  316. }
  317. if (!time_out) {
  318. printf("PCIE: VCO output not locked\n");
  319. return -1;
  320. }
  321. return 0;
  322. }
  323. #else
  324. int ppc4xx_init_pcie(void)
  325. {
  326. /*
  327. * Nothing to do on 405EX
  328. */
  329. return 0;
  330. }
  331. #endif
  332. /*
  333. * Board-specific pcie initialization
  334. * Platform code can reimplement ppc4xx_init_pcie_port_hw() if needed
  335. */
  336. /*
  337. * Initialize various parts of the PCI Express core for our port:
  338. *
  339. * - Set as a root port and enable max width
  340. * (PXIE0 -> X8, PCIE1 and PCIE2 -> X4).
  341. * - Set up UTL configuration.
  342. * - Increase SERDES drive strength to levels suggested by AMCC.
  343. * - De-assert RSTPYN, RSTDL and RSTGU.
  344. *
  345. * NOTICE for 440SPE revB chip: PESDRn_UTLSET2 is not set - we leave it
  346. * with default setting 0x11310000. The register has new fields,
  347. * PESDRn_UTLSET2[LKINE] in particular: clearing it leads to PCIE core
  348. * hang.
  349. */
  350. #if defined(CONFIG_440SPE)
  351. int __ppc4xx_init_pcie_port_hw(int port, int rootport)
  352. {
  353. u32 val = 1 << 24;
  354. u32 utlset1;
  355. if (rootport) {
  356. val = PTYPE_ROOT_PORT << 20;
  357. utlset1 = 0x21222222;
  358. } else {
  359. val = PTYPE_LEGACY_ENDPOINT << 20;
  360. utlset1 = 0x20222222;
  361. }
  362. if (port == 0)
  363. val |= LNKW_X8 << 12;
  364. else
  365. val |= LNKW_X4 << 12;
  366. SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
  367. SDR_WRITE(SDRN_PESDR_UTLSET1(port), utlset1);
  368. if (!ppc440spe_revB())
  369. SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x11000000);
  370. SDR_WRITE(SDRN_PESDR_HSSL0SET1(port), 0x35000000);
  371. SDR_WRITE(SDRN_PESDR_HSSL1SET1(port), 0x35000000);
  372. SDR_WRITE(SDRN_PESDR_HSSL2SET1(port), 0x35000000);
  373. SDR_WRITE(SDRN_PESDR_HSSL3SET1(port), 0x35000000);
  374. if (port == 0) {
  375. SDR_WRITE(PESDR0_HSSL4SET1, 0x35000000);
  376. SDR_WRITE(PESDR0_HSSL5SET1, 0x35000000);
  377. SDR_WRITE(PESDR0_HSSL6SET1, 0x35000000);
  378. SDR_WRITE(PESDR0_HSSL7SET1, 0x35000000);
  379. }
  380. SDR_WRITE(SDRN_PESDR_RCSSET(port), (SDR_READ(SDRN_PESDR_RCSSET(port)) &
  381. ~(1 << 24 | 1 << 16)) | 1 << 12);
  382. return 0;
  383. }
  384. #endif /* CONFIG_440SPE */
  385. #if defined(CONFIG_405EX)
  386. int __ppc4xx_init_pcie_port_hw(int port, int rootport)
  387. {
  388. u32 val;
  389. /*
  390. * test-only:
  391. * This needs some testing and perhaps changes for
  392. * endpoint configuration. Probably no PHY reset at all, etc.
  393. * sr, 2007-10-03
  394. */
  395. if (rootport)
  396. val = 0x00401000;
  397. else
  398. val = 0x00101000;
  399. SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
  400. SDR_WRITE(SDRN_PESDR_UTLSET1(port), 0x20222222);
  401. SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x01110000);
  402. SDR_WRITE(SDRN_PESDR_PHYSET1(port), 0x720F0000);
  403. SDR_WRITE(SDRN_PESDR_PHYSET2(port), 0x70600003);
  404. /* Assert the PE0_PHY reset */
  405. SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01010000);
  406. udelay(1000);
  407. /* deassert the PE0_hotreset */
  408. SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01101000);
  409. /* poll for phy !reset */
  410. while (!(SDR_READ(SDRN_PESDR_PHYSTA(port)) & 0x00001000))
  411. ;
  412. /* deassert the PE0_gpl_utl_reset */
  413. SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x00101000);
  414. if (port == 0)
  415. mtdcr(DCRN_PEGPL_CFG(PCIE0), 0x10000000); /* guarded on */
  416. else
  417. mtdcr(DCRN_PEGPL_CFG(PCIE1), 0x10000000); /* guarded on */
  418. return 0;
  419. }
  420. #endif /* CONFIG_405EX */
  421. int ppc4xx_init_pcie_port_hw(int port, int rootport)
  422. __attribute__((weak, alias("__ppc4xx_init_pcie_port_hw")));
  423. /*
  424. * We map PCI Express configuration access into the 512MB regions
  425. *
  426. * NOTICE: revB is very strict about PLB real addressess and ranges to
  427. * be mapped for config space; it seems to only work with d_nnnn_nnnn
  428. * range (hangs the core upon config transaction attempts when set
  429. * otherwise) while revA uses c_nnnn_nnnn.
  430. *
  431. * For revA:
  432. * PCIE0: 0xc_4000_0000
  433. * PCIE1: 0xc_8000_0000
  434. * PCIE2: 0xc_c000_0000
  435. *
  436. * For revB:
  437. * PCIE0: 0xd_0000_0000
  438. * PCIE1: 0xd_2000_0000
  439. * PCIE2: 0xd_4000_0000
  440. *
  441. * For 405EX:
  442. * PCIE0: 0xa000_0000
  443. * PCIE1: 0xc000_0000
  444. */
  445. static inline u64 ppc4xx_get_cfgaddr(int port)
  446. {
  447. #if defined(CONFIG_405EX)
  448. if (port == 0)
  449. return (u64)CFG_PCIE0_CFGBASE;
  450. else
  451. return (u64)CFG_PCIE1_CFGBASE;
  452. #endif
  453. #if defined(CONFIG_440SPE)
  454. if (ppc440spe_revB()) {
  455. switch (port) {
  456. default: /* to satisfy compiler */
  457. case 0:
  458. return 0x0000000d00000000ULL;
  459. case 1:
  460. return 0x0000000d20000000ULL;
  461. case 2:
  462. return 0x0000000d40000000ULL;
  463. }
  464. } else {
  465. switch (port) {
  466. default: /* to satisfy compiler */
  467. case 0:
  468. return 0x0000000c40000000ULL;
  469. case 1:
  470. return 0x0000000c80000000ULL;
  471. case 2:
  472. return 0x0000000cc0000000ULL;
  473. }
  474. }
  475. #endif
  476. }
  477. /*
  478. * 4xx boards as end point and root point setup
  479. * and
  480. * testing inbound and out bound windows
  481. *
  482. * 4xx boards can be plugged into another 4xx boards or you can get PCI-E
  483. * cable which can be used to setup loop back from one port to another port.
  484. * Please rememeber that unless there is a endpoint plugged in to root port it
  485. * will not initialize. It is the same in case of endpoint , unless there is
  486. * root port attached it will not initialize.
  487. *
  488. * In this release of software all the PCI-E ports are configured as either
  489. * endpoint or rootpoint.In future we will have support for selective ports
  490. * setup as endpoint and root point in single board.
  491. *
  492. * Once your board came up as root point , you can verify by reading
  493. * /proc/bus/pci/devices. Where you can see the configuration registers
  494. * of end point device attached to the port.
  495. *
  496. * Enpoint cofiguration can be verified by connecting 4xx board to any
  497. * host or another 4xx board. Then try to scan the device. In case of
  498. * linux use "lspci" or appripriate os command.
  499. *
  500. * How do I verify the inbound and out bound windows ? (4xx to 4xx)
  501. * in this configuration inbound and outbound windows are setup to access
  502. * sram memroy area. SRAM is at 0x4 0000 0000 , on PLB bus. This address
  503. * is mapped at 0x90000000. From u-boot prompt write data 0xb000 0000,
  504. * This is waere your POM(PLB out bound memory window) mapped. then
  505. * read the data from other 4xx board's u-boot prompt at address
  506. * 0x9000 0000(SRAM). Data should match.
  507. * In case of inbound , write data to u-boot command prompt at 0xb000 0000
  508. * which is mapped to 0x4 0000 0000. Now on rootpoint yucca u-boot prompt check
  509. * data at 0x9000 0000(SRAM).Data should match.
  510. */
  511. int ppc4xx_init_pcie_port(int port, int rootport)
  512. {
  513. static int core_init;
  514. volatile u32 val = 0;
  515. int attempts;
  516. u64 addr;
  517. u32 low, high;
  518. if (!core_init) {
  519. ++core_init;
  520. if (ppc4xx_init_pcie())
  521. return -1;
  522. }
  523. /*
  524. * Initialize various parts of the PCI Express core for our port
  525. */
  526. ppc4xx_init_pcie_port_hw(port, rootport);
  527. /*
  528. * Notice: the following delay has critical impact on device
  529. * initialization - if too short (<50ms) the link doesn't get up.
  530. */
  531. mdelay(100);
  532. val = SDR_READ(SDRN_PESDR_RCSSTS(port));
  533. if (val & (1 << 20)) {
  534. printf("PCIE%d: PGRST failed %08x\n", port, val);
  535. return -1;
  536. }
  537. /*
  538. * Verify link is up
  539. */
  540. val = SDR_READ(SDRN_PESDR_LOOP(port));
  541. if (!(val & 0x00001000)) {
  542. printf("PCIE%d: link is not up.\n", port);
  543. return -1;
  544. }
  545. #if defined(CONFIG_440SPE)
  546. /*
  547. * Setup UTL registers - but only on revA!
  548. * We use default settings for revB chip.
  549. */
  550. if (!ppc440spe_revB())
  551. ppc4xx_setup_utl(port);
  552. #endif
  553. /*
  554. * We map PCI Express configuration access into the 512MB regions
  555. */
  556. addr = ppc4xx_get_cfgaddr(port);
  557. low = U64_TO_U32_LOW(addr);
  558. high = U64_TO_U32_HIGH(addr);
  559. switch (port) {
  560. case 0:
  561. mtdcr(DCRN_PEGPL_CFGBAH(PCIE0), high);
  562. mtdcr(DCRN_PEGPL_CFGBAL(PCIE0), low);
  563. mtdcr(DCRN_PEGPL_CFGMSK(PCIE0), 0xe0000001); /* 512MB region, valid */
  564. break;
  565. case 1:
  566. mtdcr(DCRN_PEGPL_CFGBAH(PCIE1), high);
  567. mtdcr(DCRN_PEGPL_CFGBAL(PCIE1), low);
  568. mtdcr(DCRN_PEGPL_CFGMSK(PCIE1), 0xe0000001); /* 512MB region, valid */
  569. break;
  570. #if CFG_PCIE_NR_PORTS > 2
  571. case 2:
  572. mtdcr(DCRN_PEGPL_CFGBAH(PCIE2), high);
  573. mtdcr(DCRN_PEGPL_CFGBAL(PCIE2), low);
  574. mtdcr(DCRN_PEGPL_CFGMSK(PCIE2), 0xe0000001); /* 512MB region, valid */
  575. break;
  576. #endif
  577. }
  578. /*
  579. * Check for VC0 active and assert RDY.
  580. */
  581. attempts = 10;
  582. while(!(SDR_READ(SDRN_PESDR_RCSSTS(port)) & (1 << 16))) {
  583. if (!(attempts--)) {
  584. printf("PCIE%d: VC0 not active\n", port);
  585. return -1;
  586. }
  587. mdelay(1000);
  588. }
  589. SDR_WRITE(SDRN_PESDR_RCSSET(port),
  590. SDR_READ(SDRN_PESDR_RCSSET(port)) | 1 << 20);
  591. mdelay(100);
  592. return 0;
  593. }
  594. int ppc4xx_init_pcie_rootport(int port)
  595. {
  596. return ppc4xx_init_pcie_port(port, 1);
  597. }
  598. int ppc4xx_init_pcie_endport(int port)
  599. {
  600. return ppc4xx_init_pcie_port(port, 0);
  601. }
  602. void ppc4xx_setup_pcie_rootpoint(struct pci_controller *hose, int port)
  603. {
  604. volatile void *mbase = NULL;
  605. volatile void *rmbase = NULL;
  606. pci_set_ops(hose,
  607. pcie_read_config_byte,
  608. pcie_read_config_word,
  609. pcie_read_config_dword,
  610. pcie_write_config_byte,
  611. pcie_write_config_word,
  612. pcie_write_config_dword);
  613. switch (port) {
  614. case 0:
  615. mbase = (u32 *)CFG_PCIE0_XCFGBASE;
  616. rmbase = (u32 *)CFG_PCIE0_CFGBASE;
  617. hose->cfg_data = (u8 *)CFG_PCIE0_CFGBASE;
  618. break;
  619. case 1:
  620. mbase = (u32 *)CFG_PCIE1_XCFGBASE;
  621. rmbase = (u32 *)CFG_PCIE1_CFGBASE;
  622. hose->cfg_data = (u8 *)CFG_PCIE1_CFGBASE;
  623. break;
  624. #if CFG_PCIE_NR_PORTS > 2
  625. case 2:
  626. mbase = (u32 *)CFG_PCIE2_XCFGBASE;
  627. rmbase = (u32 *)CFG_PCIE2_CFGBASE;
  628. hose->cfg_data = (u8 *)CFG_PCIE2_CFGBASE;
  629. break;
  630. #endif
  631. }
  632. /*
  633. * Set bus numbers on our root port
  634. */
  635. out_8((u8 *)mbase + PCI_PRIMARY_BUS, 0);
  636. out_8((u8 *)mbase + PCI_SECONDARY_BUS, 1);
  637. out_8((u8 *)mbase + PCI_SUBORDINATE_BUS, 1);
  638. /*
  639. * Set up outbound translation to hose->mem_space from PLB
  640. * addresses at an offset of 0xd_0000_0000. We set the low
  641. * bits of the mask to 11 to turn off splitting into 8
  642. * subregions and to enable the outbound translation.
  643. */
  644. out_le32(mbase + PECFG_POM0LAH, 0x00000000);
  645. out_le32(mbase + PECFG_POM0LAL, CFG_PCIE_MEMBASE +
  646. port * CFG_PCIE_MEMSIZE);
  647. debug("PECFG_POM0LA=%08x.%08x\n", in_le32(mbase + PECFG_POM0LAH),
  648. in_le32(mbase + PECFG_POM0LAL));
  649. switch (port) {
  650. case 0:
  651. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), CFG_PCIE_ADDR_HIGH);
  652. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), CFG_PCIE_MEMBASE +
  653. port * CFG_PCIE_MEMSIZE);
  654. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
  655. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
  656. ~(CFG_PCIE_MEMSIZE - 1) | 3);
  657. debug("0:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
  658. mfdcr(DCRN_PEGPL_OMR1BAH(PCIE0)),
  659. mfdcr(DCRN_PEGPL_OMR1BAL(PCIE0)),
  660. mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE0)),
  661. mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE0)));
  662. break;
  663. case 1:
  664. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), CFG_PCIE_ADDR_HIGH);
  665. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), CFG_PCIE_MEMBASE +
  666. port * CFG_PCIE_MEMSIZE);
  667. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
  668. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
  669. ~(CFG_PCIE_MEMSIZE - 1) | 3);
  670. debug("1:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
  671. mfdcr(DCRN_PEGPL_OMR1BAH(PCIE1)),
  672. mfdcr(DCRN_PEGPL_OMR1BAL(PCIE1)),
  673. mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE1)),
  674. mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE1)));
  675. break;
  676. #if CFG_PCIE_NR_PORTS > 2
  677. case 2:
  678. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), CFG_PCIE_ADDR_HIGH);
  679. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), CFG_PCIE_MEMBASE +
  680. port * CFG_PCIE_MEMSIZE);
  681. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
  682. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
  683. ~(CFG_PCIE_MEMSIZE - 1) | 3);
  684. debug("2:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
  685. mfdcr(DCRN_PEGPL_OMR1BAH(PCIE2)),
  686. mfdcr(DCRN_PEGPL_OMR1BAL(PCIE2)),
  687. mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE2)),
  688. mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE2)));
  689. break;
  690. #endif
  691. }
  692. /* Set up 16GB inbound memory window at 0 */
  693. out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
  694. out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
  695. out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffc);
  696. out_le32(mbase + PECFG_BAR0LMPA, 0);
  697. out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
  698. out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
  699. out_le32(mbase + PECFG_PIM0LAL, 0);
  700. out_le32(mbase + PECFG_PIM0LAH, 0);
  701. out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
  702. out_le32(mbase + PECFG_PIM1LAH, 0x00000004);
  703. out_le32(mbase + PECFG_PIMEN, 0x1);
  704. /* Enable I/O, Mem, and Busmaster cycles */
  705. out_le16((u16 *)(mbase + PCI_COMMAND),
  706. in_le16((u16 *)(mbase + PCI_COMMAND)) |
  707. PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  708. /* Set Device and Vendor Id */
  709. out_le16(mbase + 0x200, 0xaaa0 + port);
  710. out_le16(mbase + 0x202, 0xbed0 + port);
  711. /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
  712. out_le32(mbase + 0x208, 0x06040001);
  713. printf("PCIE%d: successfully set as root-complex\n", port);
  714. }
  715. int ppc4xx_setup_pcie_endpoint(struct pci_controller *hose, int port)
  716. {
  717. volatile void *mbase = NULL;
  718. int attempts = 0;
  719. pci_set_ops(hose,
  720. pcie_read_config_byte,
  721. pcie_read_config_word,
  722. pcie_read_config_dword,
  723. pcie_write_config_byte,
  724. pcie_write_config_word,
  725. pcie_write_config_dword);
  726. switch (port) {
  727. case 0:
  728. mbase = (u32 *)CFG_PCIE0_XCFGBASE;
  729. hose->cfg_data = (u8 *)CFG_PCIE0_CFGBASE;
  730. break;
  731. case 1:
  732. mbase = (u32 *)CFG_PCIE1_XCFGBASE;
  733. hose->cfg_data = (u8 *)CFG_PCIE1_CFGBASE;
  734. break;
  735. #if defined(CFG_PCIE2_CFGBASE)
  736. case 2:
  737. mbase = (u32 *)CFG_PCIE2_XCFGBASE;
  738. hose->cfg_data = (u8 *)CFG_PCIE2_CFGBASE;
  739. break;
  740. #endif
  741. }
  742. /*
  743. * Set up outbound translation to hose->mem_space from PLB
  744. * addresses at an offset of 0xd_0000_0000. We set the low
  745. * bits of the mask to 11 to turn off splitting into 8
  746. * subregions and to enable the outbound translation.
  747. */
  748. out_le32(mbase + PECFG_POM0LAH, 0x00001ff8);
  749. out_le32(mbase + PECFG_POM0LAL, 0x00001000);
  750. switch (port) {
  751. case 0:
  752. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), CFG_PCIE_ADDR_HIGH);
  753. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), CFG_PCIE_MEMBASE +
  754. port * CFG_PCIE_MEMSIZE);
  755. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
  756. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
  757. ~(CFG_PCIE_MEMSIZE - 1) | 3);
  758. break;
  759. case 1:
  760. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), CFG_PCIE_ADDR_HIGH);
  761. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), CFG_PCIE_MEMBASE +
  762. port * CFG_PCIE_MEMSIZE);
  763. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
  764. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
  765. ~(CFG_PCIE_MEMSIZE - 1) | 3);
  766. break;
  767. #if CFG_PCIE_NR_PORTS > 2
  768. case 2:
  769. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), CFG_PCIE_ADDR_HIGH);
  770. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), CFG_PCIE_MEMBASE +
  771. port * CFG_PCIE_MEMSIZE);
  772. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
  773. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
  774. ~(CFG_PCIE_MEMSIZE - 1) | 3);
  775. break;
  776. #endif
  777. }
  778. /* Set up 16GB inbound memory window at 0 */
  779. out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
  780. out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
  781. out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffc);
  782. out_le32(mbase + PECFG_BAR0LMPA, 0);
  783. out_le32(mbase + PECFG_PIM0LAL, U64_TO_U32_LOW(CFG_PCIE_INBOUND_BASE));
  784. out_le32(mbase + PECFG_PIM0LAH, U64_TO_U32_HIGH(CFG_PCIE_INBOUND_BASE));
  785. out_le32(mbase + PECFG_PIMEN, 0x1);
  786. /* Enable I/O, Mem, and Busmaster cycles */
  787. out_le16((u16 *)(mbase + PCI_COMMAND),
  788. in_le16((u16 *)(mbase + PCI_COMMAND)) |
  789. PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  790. out_le16(mbase + 0x200, 0xcaad); /* Setting vendor ID */
  791. out_le16(mbase + 0x202, 0xfeed); /* Setting device ID */
  792. attempts = 10;
  793. while(!(SDR_READ(SDRN_PESDR_RCSSTS(port)) & (1 << 8))) {
  794. if (!(attempts--)) {
  795. printf("PCIE%d: BME not active\n", port);
  796. return -1;
  797. }
  798. mdelay(1000);
  799. }
  800. printf("PCIE%d: successfully set as endpoint\n", port);
  801. return 0;
  802. }
  803. #endif /* CONFIG_440SPE && CONFIG_PCI */