4xx_pcie.c 25 KB

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