4xx_pcie.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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. static void ppc4xx_setup_utl(u32 port)
  341. {
  342. u32 utl_base;
  343. /*
  344. * Map UTL registers at 0xef4f_n000 (4K 0xfff mask) PEGPLn_REGMSK
  345. */
  346. switch (port) {
  347. case 0:
  348. mtdcr(DCRN_PEGPL_REGBAH(PCIE0), 0x00000000);
  349. mtdcr(DCRN_PEGPL_REGBAL(PCIE0), CFG_PCIE0_UTLBASE);
  350. mtdcr(DCRN_PEGPL_REGMSK(PCIE0), 0xfffffc01); /* 4k region, valid */
  351. mtdcr(DCRN_PEGPL_SPECIAL(PCIE0), 0);
  352. break;
  353. case 1:
  354. mtdcr(DCRN_PEGPL_REGBAH(PCIE1), 0x00000000);
  355. mtdcr(DCRN_PEGPL_REGBAL(PCIE1), CFG_PCIE1_UTLBASE);
  356. mtdcr(DCRN_PEGPL_REGMSK(PCIE1), 0xfffffc01); /* 4k region, valid */
  357. mtdcr(DCRN_PEGPL_SPECIAL(PCIE1), 0);
  358. break;
  359. }
  360. utl_base = (port==0) ? CFG_PCIE0_UTLBASE : CFG_PCIE1_UTLBASE;
  361. /*
  362. * Set buffer allocations and then assert VRB and TXE.
  363. */
  364. out_be32((u32 *)(utl_base + PEUTL_OUTTR), 0x02000000);
  365. out_be32((u32 *)(utl_base + PEUTL_INTR), 0x02000000);
  366. out_be32((u32 *)(utl_base + PEUTL_OPDBSZ), 0x04000000);
  367. out_be32((u32 *)(utl_base + PEUTL_PBBSZ), 0x21000000);
  368. out_be32((u32 *)(utl_base + PEUTL_IPHBSZ), 0x02000000);
  369. out_be32((u32 *)(utl_base + PEUTL_IPDBSZ), 0x04000000);
  370. out_be32((u32 *)(utl_base + PEUTL_RCIRQEN), 0x00f00000);
  371. out_be32((u32 *)(utl_base + PEUTL_PCTL), 0x80800066);
  372. out_be32((u32 *)(utl_base + PEUTL_PBCTL), 0x0800000c);
  373. out_be32((u32 *)(utl_base + PEUTL_RCSTA),
  374. in_be32((u32 *)(utl_base + PEUTL_RCSTA)) | 0x000040000);
  375. }
  376. int ppc4xx_init_pcie(void)
  377. {
  378. /*
  379. * Nothing to do on 405EX
  380. */
  381. return 0;
  382. }
  383. #endif
  384. /*
  385. * Board-specific pcie initialization
  386. * Platform code can reimplement ppc4xx_init_pcie_port_hw() if needed
  387. */
  388. /*
  389. * Initialize various parts of the PCI Express core for our port:
  390. *
  391. * - Set as a root port and enable max width
  392. * (PXIE0 -> X8, PCIE1 and PCIE2 -> X4).
  393. * - Set up UTL configuration.
  394. * - Increase SERDES drive strength to levels suggested by AMCC.
  395. * - De-assert RSTPYN, RSTDL and RSTGU.
  396. *
  397. * NOTICE for 440SPE revB chip: PESDRn_UTLSET2 is not set - we leave it
  398. * with default setting 0x11310000. The register has new fields,
  399. * PESDRn_UTLSET2[LKINE] in particular: clearing it leads to PCIE core
  400. * hang.
  401. */
  402. #if defined(CONFIG_440SPE)
  403. int __ppc4xx_init_pcie_port_hw(int port, int rootport)
  404. {
  405. u32 val = 1 << 24;
  406. u32 utlset1;
  407. if (rootport) {
  408. val = PTYPE_ROOT_PORT << 20;
  409. utlset1 = 0x21222222;
  410. } else {
  411. val = PTYPE_LEGACY_ENDPOINT << 20;
  412. utlset1 = 0x20222222;
  413. }
  414. if (port == 0)
  415. val |= LNKW_X8 << 12;
  416. else
  417. val |= LNKW_X4 << 12;
  418. SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
  419. SDR_WRITE(SDRN_PESDR_UTLSET1(port), utlset1);
  420. if (!ppc440spe_revB())
  421. SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x11000000);
  422. SDR_WRITE(SDRN_PESDR_HSSL0SET1(port), 0x35000000);
  423. SDR_WRITE(SDRN_PESDR_HSSL1SET1(port), 0x35000000);
  424. SDR_WRITE(SDRN_PESDR_HSSL2SET1(port), 0x35000000);
  425. SDR_WRITE(SDRN_PESDR_HSSL3SET1(port), 0x35000000);
  426. if (port == 0) {
  427. SDR_WRITE(PESDR0_HSSL4SET1, 0x35000000);
  428. SDR_WRITE(PESDR0_HSSL5SET1, 0x35000000);
  429. SDR_WRITE(PESDR0_HSSL6SET1, 0x35000000);
  430. SDR_WRITE(PESDR0_HSSL7SET1, 0x35000000);
  431. }
  432. SDR_WRITE(SDRN_PESDR_RCSSET(port), (SDR_READ(SDRN_PESDR_RCSSET(port)) &
  433. ~(1 << 24 | 1 << 16)) | 1 << 12);
  434. return 0;
  435. }
  436. #endif /* CONFIG_440SPE */
  437. #if defined(CONFIG_405EX)
  438. int __ppc4xx_init_pcie_port_hw(int port, int rootport)
  439. {
  440. u32 val;
  441. if (rootport)
  442. val = 0x00401000;
  443. else
  444. val = 0x00101000;
  445. SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
  446. SDR_WRITE(SDRN_PESDR_UTLSET1(port), 0x00000000);
  447. SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x01010000);
  448. SDR_WRITE(SDRN_PESDR_PHYSET1(port), 0x720F0000);
  449. SDR_WRITE(SDRN_PESDR_PHYSET2(port), 0x70600003);
  450. /* Assert the PE0_PHY reset */
  451. SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01010000);
  452. udelay(1000);
  453. /* deassert the PE0_hotreset */
  454. if (is_end_point(port))
  455. SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01111000);
  456. else
  457. SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01101000);
  458. /* poll for phy !reset */
  459. while (!(SDR_READ(SDRN_PESDR_PHYSTA(port)) & 0x00001000))
  460. ;
  461. /* deassert the PE0_gpl_utl_reset */
  462. SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x00101000);
  463. if (port == 0)
  464. mtdcr(DCRN_PEGPL_CFG(PCIE0), 0x10000000); /* guarded on */
  465. else
  466. mtdcr(DCRN_PEGPL_CFG(PCIE1), 0x10000000); /* guarded on */
  467. return 0;
  468. }
  469. #endif /* CONFIG_405EX */
  470. int ppc4xx_init_pcie_port_hw(int port, int rootport)
  471. __attribute__((weak, alias("__ppc4xx_init_pcie_port_hw")));
  472. /*
  473. * We map PCI Express configuration access into the 512MB regions
  474. *
  475. * NOTICE: revB is very strict about PLB real addressess and ranges to
  476. * be mapped for config space; it seems to only work with d_nnnn_nnnn
  477. * range (hangs the core upon config transaction attempts when set
  478. * otherwise) while revA uses c_nnnn_nnnn.
  479. *
  480. * For revA:
  481. * PCIE0: 0xc_4000_0000
  482. * PCIE1: 0xc_8000_0000
  483. * PCIE2: 0xc_c000_0000
  484. *
  485. * For revB:
  486. * PCIE0: 0xd_0000_0000
  487. * PCIE1: 0xd_2000_0000
  488. * PCIE2: 0xd_4000_0000
  489. *
  490. * For 405EX:
  491. * PCIE0: 0xa000_0000
  492. * PCIE1: 0xc000_0000
  493. */
  494. static inline u64 ppc4xx_get_cfgaddr(int port)
  495. {
  496. #if defined(CONFIG_405EX)
  497. if (port == 0)
  498. return (u64)CFG_PCIE0_CFGBASE;
  499. else
  500. return (u64)CFG_PCIE1_CFGBASE;
  501. #endif
  502. #if defined(CONFIG_440SPE)
  503. if (ppc440spe_revB()) {
  504. switch (port) {
  505. default: /* to satisfy compiler */
  506. case 0:
  507. return 0x0000000d00000000ULL;
  508. case 1:
  509. return 0x0000000d20000000ULL;
  510. case 2:
  511. return 0x0000000d40000000ULL;
  512. }
  513. } else {
  514. switch (port) {
  515. default: /* to satisfy compiler */
  516. case 0:
  517. return 0x0000000c40000000ULL;
  518. case 1:
  519. return 0x0000000c80000000ULL;
  520. case 2:
  521. return 0x0000000cc0000000ULL;
  522. }
  523. }
  524. #endif
  525. }
  526. /*
  527. * 4xx boards as end point and root point setup
  528. * and
  529. * testing inbound and out bound windows
  530. *
  531. * 4xx boards can be plugged into another 4xx boards or you can get PCI-E
  532. * cable which can be used to setup loop back from one port to another port.
  533. * Please rememeber that unless there is a endpoint plugged in to root port it
  534. * will not initialize. It is the same in case of endpoint , unless there is
  535. * root port attached it will not initialize.
  536. *
  537. * In this release of software all the PCI-E ports are configured as either
  538. * endpoint or rootpoint.In future we will have support for selective ports
  539. * setup as endpoint and root point in single board.
  540. *
  541. * Once your board came up as root point , you can verify by reading
  542. * /proc/bus/pci/devices. Where you can see the configuration registers
  543. * of end point device attached to the port.
  544. *
  545. * Enpoint cofiguration can be verified by connecting 4xx board to any
  546. * host or another 4xx board. Then try to scan the device. In case of
  547. * linux use "lspci" or appripriate os command.
  548. *
  549. * How do I verify the inbound and out bound windows ? (4xx to 4xx)
  550. * in this configuration inbound and outbound windows are setup to access
  551. * sram memroy area. SRAM is at 0x4 0000 0000 , on PLB bus. This address
  552. * is mapped at 0x90000000. From u-boot prompt write data 0xb000 0000,
  553. * This is waere your POM(PLB out bound memory window) mapped. then
  554. * read the data from other 4xx board's u-boot prompt at address
  555. * 0x9000 0000(SRAM). Data should match.
  556. * In case of inbound , write data to u-boot command prompt at 0xb000 0000
  557. * which is mapped to 0x4 0000 0000. Now on rootpoint yucca u-boot prompt check
  558. * data at 0x9000 0000(SRAM).Data should match.
  559. */
  560. int ppc4xx_init_pcie_port(int port, int rootport)
  561. {
  562. static int core_init;
  563. volatile u32 val = 0;
  564. int attempts;
  565. u64 addr;
  566. u32 low, high;
  567. if (!core_init) {
  568. if (ppc4xx_init_pcie())
  569. return -1;
  570. ++core_init;
  571. }
  572. /*
  573. * Initialize various parts of the PCI Express core for our port
  574. */
  575. ppc4xx_init_pcie_port_hw(port, rootport);
  576. /*
  577. * Notice: the following delay has critical impact on device
  578. * initialization - if too short (<50ms) the link doesn't get up.
  579. */
  580. mdelay(100);
  581. val = SDR_READ(SDRN_PESDR_RCSSTS(port));
  582. if (val & (1 << 20)) {
  583. printf("PCIE%d: PGRST failed %08x\n", port, val);
  584. return -1;
  585. }
  586. /*
  587. * Verify link is up
  588. */
  589. val = SDR_READ(SDRN_PESDR_LOOP(port));
  590. if (!(val & 0x00001000)) {
  591. printf("PCIE%d: link is not up.\n", port);
  592. return -1;
  593. }
  594. /*
  595. * Setup UTL registers - but only on revA!
  596. * We use default settings for revB chip.
  597. */
  598. if (!ppc440spe_revB())
  599. ppc4xx_setup_utl(port);
  600. /*
  601. * We map PCI Express configuration access into the 512MB regions
  602. */
  603. addr = ppc4xx_get_cfgaddr(port);
  604. low = U64_TO_U32_LOW(addr);
  605. high = U64_TO_U32_HIGH(addr);
  606. switch (port) {
  607. case 0:
  608. mtdcr(DCRN_PEGPL_CFGBAH(PCIE0), high);
  609. mtdcr(DCRN_PEGPL_CFGBAL(PCIE0), low);
  610. mtdcr(DCRN_PEGPL_CFGMSK(PCIE0), 0xe0000001); /* 512MB region, valid */
  611. break;
  612. case 1:
  613. mtdcr(DCRN_PEGPL_CFGBAH(PCIE1), high);
  614. mtdcr(DCRN_PEGPL_CFGBAL(PCIE1), low);
  615. mtdcr(DCRN_PEGPL_CFGMSK(PCIE1), 0xe0000001); /* 512MB region, valid */
  616. break;
  617. #if CFG_PCIE_NR_PORTS > 2
  618. case 2:
  619. mtdcr(DCRN_PEGPL_CFGBAH(PCIE2), high);
  620. mtdcr(DCRN_PEGPL_CFGBAL(PCIE2), low);
  621. mtdcr(DCRN_PEGPL_CFGMSK(PCIE2), 0xe0000001); /* 512MB region, valid */
  622. break;
  623. #endif
  624. }
  625. /*
  626. * Check for VC0 active and assert RDY.
  627. */
  628. attempts = 10;
  629. while(!(SDR_READ(SDRN_PESDR_RCSSTS(port)) & (1 << 16))) {
  630. if (!(attempts--)) {
  631. printf("PCIE%d: VC0 not active\n", port);
  632. return -1;
  633. }
  634. mdelay(1000);
  635. }
  636. SDR_WRITE(SDRN_PESDR_RCSSET(port),
  637. SDR_READ(SDRN_PESDR_RCSSET(port)) | 1 << 20);
  638. mdelay(100);
  639. return 0;
  640. }
  641. int ppc4xx_init_pcie_rootport(int port)
  642. {
  643. return ppc4xx_init_pcie_port(port, 1);
  644. }
  645. int ppc4xx_init_pcie_endport(int port)
  646. {
  647. return ppc4xx_init_pcie_port(port, 0);
  648. }
  649. void ppc4xx_setup_pcie_rootpoint(struct pci_controller *hose, int port)
  650. {
  651. volatile void *mbase = NULL;
  652. volatile void *rmbase = NULL;
  653. pci_set_ops(hose,
  654. pcie_read_config_byte,
  655. pcie_read_config_word,
  656. pcie_read_config_dword,
  657. pcie_write_config_byte,
  658. pcie_write_config_word,
  659. pcie_write_config_dword);
  660. switch (port) {
  661. case 0:
  662. mbase = (u32 *)CFG_PCIE0_XCFGBASE;
  663. rmbase = (u32 *)CFG_PCIE0_CFGBASE;
  664. hose->cfg_data = (u8 *)CFG_PCIE0_CFGBASE;
  665. break;
  666. case 1:
  667. mbase = (u32 *)CFG_PCIE1_XCFGBASE;
  668. rmbase = (u32 *)CFG_PCIE1_CFGBASE;
  669. hose->cfg_data = (u8 *)CFG_PCIE1_CFGBASE;
  670. break;
  671. #if CFG_PCIE_NR_PORTS > 2
  672. case 2:
  673. mbase = (u32 *)CFG_PCIE2_XCFGBASE;
  674. rmbase = (u32 *)CFG_PCIE2_CFGBASE;
  675. hose->cfg_data = (u8 *)CFG_PCIE2_CFGBASE;
  676. break;
  677. #endif
  678. }
  679. /*
  680. * Set bus numbers on our root port
  681. */
  682. out_8((u8 *)mbase + PCI_PRIMARY_BUS, 0);
  683. out_8((u8 *)mbase + PCI_SECONDARY_BUS, 1);
  684. out_8((u8 *)mbase + PCI_SUBORDINATE_BUS, 1);
  685. /*
  686. * Set up outbound translation to hose->mem_space from PLB
  687. * addresses at an offset of 0xd_0000_0000. We set the low
  688. * bits of the mask to 11 to turn off splitting into 8
  689. * subregions and to enable the outbound translation.
  690. */
  691. out_le32(mbase + PECFG_POM0LAH, 0x00000000);
  692. out_le32(mbase + PECFG_POM0LAL, CFG_PCIE_MEMBASE +
  693. port * CFG_PCIE_MEMSIZE);
  694. debug("PECFG_POM0LA=%08x.%08x\n", in_le32(mbase + PECFG_POM0LAH),
  695. in_le32(mbase + PECFG_POM0LAL));
  696. switch (port) {
  697. case 0:
  698. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), CFG_PCIE_ADDR_HIGH);
  699. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), CFG_PCIE_MEMBASE +
  700. port * CFG_PCIE_MEMSIZE);
  701. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
  702. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
  703. ~(CFG_PCIE_MEMSIZE - 1) | 3);
  704. debug("0:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
  705. mfdcr(DCRN_PEGPL_OMR1BAH(PCIE0)),
  706. mfdcr(DCRN_PEGPL_OMR1BAL(PCIE0)),
  707. mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE0)),
  708. mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE0)));
  709. break;
  710. case 1:
  711. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), CFG_PCIE_ADDR_HIGH);
  712. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), CFG_PCIE_MEMBASE +
  713. port * CFG_PCIE_MEMSIZE);
  714. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
  715. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
  716. ~(CFG_PCIE_MEMSIZE - 1) | 3);
  717. debug("1:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
  718. mfdcr(DCRN_PEGPL_OMR1BAH(PCIE1)),
  719. mfdcr(DCRN_PEGPL_OMR1BAL(PCIE1)),
  720. mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE1)),
  721. mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE1)));
  722. break;
  723. #if CFG_PCIE_NR_PORTS > 2
  724. case 2:
  725. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), CFG_PCIE_ADDR_HIGH);
  726. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), CFG_PCIE_MEMBASE +
  727. port * CFG_PCIE_MEMSIZE);
  728. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
  729. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
  730. ~(CFG_PCIE_MEMSIZE - 1) | 3);
  731. debug("2:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
  732. mfdcr(DCRN_PEGPL_OMR1BAH(PCIE2)),
  733. mfdcr(DCRN_PEGPL_OMR1BAL(PCIE2)),
  734. mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE2)),
  735. mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE2)));
  736. break;
  737. #endif
  738. }
  739. /* Set up 16GB inbound memory window at 0 */
  740. out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
  741. out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
  742. out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffc);
  743. out_le32(mbase + PECFG_BAR0LMPA, 0);
  744. out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
  745. out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
  746. out_le32(mbase + PECFG_PIM0LAL, 0);
  747. out_le32(mbase + PECFG_PIM0LAH, 0);
  748. out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
  749. out_le32(mbase + PECFG_PIM1LAH, 0x00000004);
  750. out_le32(mbase + PECFG_PIMEN, 0x1);
  751. /* Enable I/O, Mem, and Busmaster cycles */
  752. out_le16((u16 *)(mbase + PCI_COMMAND),
  753. in_le16((u16 *)(mbase + PCI_COMMAND)) |
  754. PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  755. /* Set Device and Vendor Id */
  756. out_le16(mbase + 0x200, 0xaaa0 + port);
  757. out_le16(mbase + 0x202, 0xbed0 + port);
  758. /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
  759. out_le32(mbase + 0x208, 0x06040001);
  760. printf("PCIE%d: successfully set as root-complex\n", port);
  761. }
  762. int ppc4xx_setup_pcie_endpoint(struct pci_controller *hose, int port)
  763. {
  764. volatile void *mbase = NULL;
  765. int attempts = 0;
  766. pci_set_ops(hose,
  767. pcie_read_config_byte,
  768. pcie_read_config_word,
  769. pcie_read_config_dword,
  770. pcie_write_config_byte,
  771. pcie_write_config_word,
  772. pcie_write_config_dword);
  773. switch (port) {
  774. case 0:
  775. mbase = (u32 *)CFG_PCIE0_XCFGBASE;
  776. hose->cfg_data = (u8 *)CFG_PCIE0_CFGBASE;
  777. break;
  778. case 1:
  779. mbase = (u32 *)CFG_PCIE1_XCFGBASE;
  780. hose->cfg_data = (u8 *)CFG_PCIE1_CFGBASE;
  781. break;
  782. #if defined(CFG_PCIE2_CFGBASE)
  783. case 2:
  784. mbase = (u32 *)CFG_PCIE2_XCFGBASE;
  785. hose->cfg_data = (u8 *)CFG_PCIE2_CFGBASE;
  786. break;
  787. #endif
  788. }
  789. /*
  790. * Set up outbound translation to hose->mem_space from PLB
  791. * addresses at an offset of 0xd_0000_0000. We set the low
  792. * bits of the mask to 11 to turn off splitting into 8
  793. * subregions and to enable the outbound translation.
  794. */
  795. out_le32(mbase + PECFG_POM0LAH, 0x00001ff8);
  796. out_le32(mbase + PECFG_POM0LAL, 0x00001000);
  797. switch (port) {
  798. case 0:
  799. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), CFG_PCIE_ADDR_HIGH);
  800. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), CFG_PCIE_MEMBASE +
  801. port * CFG_PCIE_MEMSIZE);
  802. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
  803. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
  804. ~(CFG_PCIE_MEMSIZE - 1) | 3);
  805. break;
  806. case 1:
  807. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), CFG_PCIE_ADDR_HIGH);
  808. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), CFG_PCIE_MEMBASE +
  809. port * CFG_PCIE_MEMSIZE);
  810. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
  811. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
  812. ~(CFG_PCIE_MEMSIZE - 1) | 3);
  813. break;
  814. #if CFG_PCIE_NR_PORTS > 2
  815. case 2:
  816. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), CFG_PCIE_ADDR_HIGH);
  817. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), CFG_PCIE_MEMBASE +
  818. port * CFG_PCIE_MEMSIZE);
  819. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
  820. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
  821. ~(CFG_PCIE_MEMSIZE - 1) | 3);
  822. break;
  823. #endif
  824. }
  825. /* Set up 64MB inbound memory window at 0 */
  826. out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
  827. out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
  828. out_le32(mbase + PECFG_PIM01SAH, 0xffffffff);
  829. out_le32(mbase + PECFG_PIM01SAL, 0xfc000000);
  830. /* Setup BAR0 */
  831. out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffff);
  832. out_le32(mbase + PECFG_BAR0LMPA, 0xfc000000 | PCI_BASE_ADDRESS_MEM_TYPE_64);
  833. /* Disable BAR1 & BAR2 */
  834. out_le32(mbase + PECFG_BAR1MPA, 0);
  835. out_le32(mbase + PECFG_BAR2HMPA, 0);
  836. out_le32(mbase + PECFG_BAR2LMPA, 0);
  837. out_le32(mbase + PECFG_PIM0LAL, U64_TO_U32_LOW(CFG_PCIE_INBOUND_BASE));
  838. out_le32(mbase + PECFG_PIM0LAH, U64_TO_U32_HIGH(CFG_PCIE_INBOUND_BASE));
  839. out_le32(mbase + PECFG_PIMEN, 0x1);
  840. /* Enable I/O, Mem, and Busmaster cycles */
  841. out_le16((u16 *)(mbase + PCI_COMMAND),
  842. in_le16((u16 *)(mbase + PCI_COMMAND)) |
  843. PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  844. out_le16(mbase + 0x200, 0xcaad); /* Setting vendor ID */
  845. out_le16(mbase + 0x202, 0xfeed); /* Setting device ID */
  846. /* Set Class Code to Processor/PPC */
  847. out_le32(mbase + 0x208, 0x0b200001);
  848. attempts = 10;
  849. while(!(SDR_READ(SDRN_PESDR_RCSSTS(port)) & (1 << 8))) {
  850. if (!(attempts--)) {
  851. printf("PCIE%d: BME not active\n", port);
  852. return -1;
  853. }
  854. mdelay(1000);
  855. }
  856. printf("PCIE%d: successfully set as endpoint\n", port);
  857. return 0;
  858. }
  859. #endif /* CONFIG_440SPE && CONFIG_PCI */