4xx_pcie.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. /*
  2. * (C) Copyright 2006 - 2008
  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 <common.h>
  27. #include <pci.h>
  28. #include <ppc4xx.h>
  29. #include <asm/processor.h>
  30. #include <asm-ppc/io.h>
  31. #include <asm/errno.h>
  32. #if (defined(CONFIG_440SPE) || defined(CONFIG_405EX) || \
  33. defined(CONFIG_460EX) || defined(CONFIG_460GT)) && \
  34. defined(CONFIG_PCI) && !defined(CONFIG_PCI_DISABLE_PCIE)
  35. #include <asm/4xx_pcie.h>
  36. enum {
  37. PTYPE_ENDPOINT = 0x0,
  38. PTYPE_LEGACY_ENDPOINT = 0x1,
  39. PTYPE_ROOT_PORT = 0x4,
  40. LNKW_X1 = 0x1,
  41. LNKW_X4 = 0x4,
  42. LNKW_X8 = 0x8
  43. };
  44. #if 1 // test-only
  45. int board_pcie_first(void);
  46. int board_pcie_last(void);
  47. static struct pci_controller pcie_hose[CONFIG_SYS_PCIE_NR_PORTS];
  48. /*
  49. * Per default, all cards are present, so we need to check if the
  50. * link comes up.
  51. */
  52. int __board_pcie_card_present(int port)
  53. {
  54. return 1;
  55. }
  56. int board_pcie_card_present(int port)
  57. __attribute__((weak, alias("__board_pcie_card_present")));
  58. /*
  59. * Some boards have runtime detection of the first and last PCIe
  60. * slot used, so let's provide weak default functions for the
  61. * common version.
  62. */
  63. int __board_pcie_first(void)
  64. {
  65. return 0;
  66. }
  67. int board_pcie_first(void)
  68. __attribute__((weak, alias("__board_pcie_first")));
  69. int __board_pcie_last(void)
  70. {
  71. return CONFIG_SYS_PCIE_NR_PORTS - 1;
  72. }
  73. int board_pcie_last(void)
  74. __attribute__((weak, alias("__board_pcie_last")));
  75. void __board_pcie_setup_port(int port, int rootpoint)
  76. {
  77. /* noting in this weak default implementation */
  78. }
  79. void board_pcie_setup_port(int port, int rootpoint)
  80. __attribute__((weak, alias("__board_pcie_setup_port")));
  81. void pcie_setup_hoses(int busno)
  82. {
  83. struct pci_controller *hose;
  84. int i, bus;
  85. int ret = 0;
  86. char *env;
  87. unsigned int delay;
  88. int first = board_pcie_first();
  89. int last = board_pcie_last();
  90. /*
  91. * Assume we're called after the PCI(X) hose(s) are initialized,
  92. * which takes bus ID 0... and therefore start numbering PCIe's
  93. * from the next number.
  94. */
  95. bus = busno;
  96. for (i = first; i <= last; i++) {
  97. /*
  98. * Some boards (e.g. Katmai) can detects via hardware
  99. * if a PCIe card is plugged, so let's check this.
  100. */
  101. if (!board_pcie_card_present(i))
  102. continue;
  103. if (is_end_point(i)) {
  104. board_pcie_setup_port(i, 0);
  105. ret = ppc4xx_init_pcie_endport(i);
  106. } else {
  107. board_pcie_setup_port(i, 1);
  108. ret = ppc4xx_init_pcie_rootport(i);
  109. }
  110. if (ret == -ENODEV)
  111. continue;
  112. if (ret) {
  113. printf("PCIE%d: initialization as %s failed\n", i,
  114. is_end_point(i) ? "endpoint" : "root-complex");
  115. continue;
  116. }
  117. hose = &pcie_hose[i];
  118. hose->first_busno = bus;
  119. hose->last_busno = bus;
  120. hose->current_busno = bus;
  121. /* setup mem resource */
  122. pci_set_region(hose->regions + 0,
  123. CONFIG_SYS_PCIE_MEMBASE + i * CONFIG_SYS_PCIE_MEMSIZE,
  124. CONFIG_SYS_PCIE_MEMBASE + i * CONFIG_SYS_PCIE_MEMSIZE,
  125. CONFIG_SYS_PCIE_MEMSIZE,
  126. PCI_REGION_MEM);
  127. hose->region_count = 1;
  128. pci_register_hose(hose);
  129. if (is_end_point(i)) {
  130. ppc4xx_setup_pcie_endpoint(hose, i);
  131. /*
  132. * Reson for no scanning is endpoint can not generate
  133. * upstream configuration accesses.
  134. */
  135. } else {
  136. ppc4xx_setup_pcie_rootpoint(hose, i);
  137. env = getenv ("pciscandelay");
  138. if (env != NULL) {
  139. delay = simple_strtoul(env, NULL, 10);
  140. if (delay > 5)
  141. printf("Warning, expect noticable delay before "
  142. "PCIe scan due to 'pciscandelay' value!\n");
  143. mdelay(delay * 1000);
  144. }
  145. /*
  146. * Config access can only go down stream
  147. */
  148. hose->last_busno = pci_hose_scan(hose);
  149. bus = hose->last_busno + 1;
  150. }
  151. }
  152. }
  153. #endif
  154. static int validate_endpoint(struct pci_controller *hose)
  155. {
  156. if (hose->cfg_data == (u8 *)CONFIG_SYS_PCIE0_CFGBASE)
  157. return (is_end_point(0));
  158. else if (hose->cfg_data == (u8 *)CONFIG_SYS_PCIE1_CFGBASE)
  159. return (is_end_point(1));
  160. #if CONFIG_SYS_PCIE_NR_PORTS > 2
  161. else if (hose->cfg_data == (u8 *)CONFIG_SYS_PCIE2_CFGBASE)
  162. return (is_end_point(2));
  163. #endif
  164. return 0;
  165. }
  166. static u8* pcie_get_base(struct pci_controller *hose, unsigned int devfn)
  167. {
  168. u8 *base = (u8*)hose->cfg_data;
  169. /* use local configuration space for the first bus */
  170. if (PCI_BUS(devfn) == 0) {
  171. if (hose->cfg_data == (u8*)CONFIG_SYS_PCIE0_CFGBASE)
  172. base = (u8*)CONFIG_SYS_PCIE0_XCFGBASE;
  173. if (hose->cfg_data == (u8*)CONFIG_SYS_PCIE1_CFGBASE)
  174. base = (u8*)CONFIG_SYS_PCIE1_XCFGBASE;
  175. #if CONFIG_SYS_PCIE_NR_PORTS > 2
  176. if (hose->cfg_data == (u8*)CONFIG_SYS_PCIE2_CFGBASE)
  177. base = (u8*)CONFIG_SYS_PCIE2_XCFGBASE;
  178. #endif
  179. }
  180. return base;
  181. }
  182. static void pcie_dmer_disable(void)
  183. {
  184. mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE),
  185. mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE)) | GPL_DMER_MASK_DISA);
  186. mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE),
  187. mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE)) | GPL_DMER_MASK_DISA);
  188. #if CONFIG_SYS_PCIE_NR_PORTS > 2
  189. mtdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE),
  190. mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE)) | GPL_DMER_MASK_DISA);
  191. #endif
  192. }
  193. static void pcie_dmer_enable(void)
  194. {
  195. mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE0_BASE),
  196. mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE0_BASE)) & ~GPL_DMER_MASK_DISA);
  197. mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE1_BASE),
  198. mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE1_BASE)) & ~GPL_DMER_MASK_DISA);
  199. #if CONFIG_SYS_PCIE_NR_PORTS > 2
  200. mtdcr (DCRN_PEGPL_CFG (DCRN_PCIE2_BASE),
  201. mfdcr (DCRN_PEGPL_CFG(DCRN_PCIE2_BASE)) & ~GPL_DMER_MASK_DISA);
  202. #endif
  203. }
  204. static int pcie_read_config(struct pci_controller *hose, unsigned int devfn,
  205. int offset, int len, u32 *val) {
  206. u8 *address;
  207. *val = 0;
  208. if (validate_endpoint(hose))
  209. return 0; /* No upstream config access */
  210. /*
  211. * Bus numbers are relative to hose->first_busno
  212. */
  213. devfn -= PCI_BDF(hose->first_busno, 0, 0);
  214. /*
  215. * NOTICE: configuration space ranges are currenlty mapped only for
  216. * the first 16 buses, so such limit must be imposed. In case more
  217. * buses are required the TLB settings in board/amcc/<board>/init.S
  218. * need to be altered accordingly (one bus takes 1 MB of memory space).
  219. */
  220. if (PCI_BUS(devfn) >= 16)
  221. return 0;
  222. /*
  223. * Only single device/single function is supported for the primary and
  224. * secondary buses of the 440SPe host bridge.
  225. */
  226. if ((!((PCI_FUNC(devfn) == 0) && (PCI_DEV(devfn) == 0))) &&
  227. ((PCI_BUS(devfn) == 0) || (PCI_BUS(devfn) == 1)))
  228. return 0;
  229. address = pcie_get_base(hose, devfn);
  230. offset += devfn << 4;
  231. /*
  232. * Reading from configuration space of non-existing device can
  233. * generate transaction errors. For the read duration we suppress
  234. * assertion of machine check exceptions to avoid those.
  235. */
  236. pcie_dmer_disable ();
  237. debug("%s: cfg_data=%08x offset=%08x\n", __func__, hose->cfg_data, offset);
  238. switch (len) {
  239. case 1:
  240. *val = in_8(hose->cfg_data + offset);
  241. break;
  242. case 2:
  243. *val = in_le16((u16 *)(hose->cfg_data + offset));
  244. break;
  245. default:
  246. *val = in_le32((u32*)(hose->cfg_data + offset));
  247. break;
  248. }
  249. pcie_dmer_enable ();
  250. return 0;
  251. }
  252. static int pcie_write_config(struct pci_controller *hose, unsigned int devfn,
  253. int offset, int len, u32 val) {
  254. u8 *address;
  255. if (validate_endpoint(hose))
  256. return 0; /* No upstream config access */
  257. /*
  258. * Bus numbers are relative to hose->first_busno
  259. */
  260. devfn -= PCI_BDF(hose->first_busno, 0, 0);
  261. /*
  262. * Same constraints as in pcie_read_config().
  263. */
  264. if (PCI_BUS(devfn) >= 16)
  265. return 0;
  266. if ((!((PCI_FUNC(devfn) == 0) && (PCI_DEV(devfn) == 0))) &&
  267. ((PCI_BUS(devfn) == 0) || (PCI_BUS(devfn) == 1)))
  268. return 0;
  269. address = pcie_get_base(hose, devfn);
  270. offset += devfn << 4;
  271. /*
  272. * Suppress MCK exceptions, similar to pcie_read_config()
  273. */
  274. pcie_dmer_disable ();
  275. switch (len) {
  276. case 1:
  277. out_8(hose->cfg_data + offset, val);
  278. break;
  279. case 2:
  280. out_le16((u16 *)(hose->cfg_data + offset), val);
  281. break;
  282. default:
  283. out_le32((u32 *)(hose->cfg_data + offset), val);
  284. break;
  285. }
  286. pcie_dmer_enable ();
  287. return 0;
  288. }
  289. int pcie_read_config_byte(struct pci_controller *hose,pci_dev_t dev,int offset,u8 *val)
  290. {
  291. u32 v;
  292. int rv;
  293. rv = pcie_read_config(hose, dev, offset, 1, &v);
  294. *val = (u8)v;
  295. return rv;
  296. }
  297. int pcie_read_config_word(struct pci_controller *hose,pci_dev_t dev,int offset,u16 *val)
  298. {
  299. u32 v;
  300. int rv;
  301. rv = pcie_read_config(hose, dev, offset, 2, &v);
  302. *val = (u16)v;
  303. return rv;
  304. }
  305. int pcie_read_config_dword(struct pci_controller *hose,pci_dev_t dev,int offset,u32 *val)
  306. {
  307. u32 v;
  308. int rv;
  309. rv = pcie_read_config(hose, dev, offset, 3, &v);
  310. *val = (u32)v;
  311. return rv;
  312. }
  313. int pcie_write_config_byte(struct pci_controller *hose,pci_dev_t dev,int offset,u8 val)
  314. {
  315. return pcie_write_config(hose,(u32)dev,offset,1,val);
  316. }
  317. int pcie_write_config_word(struct pci_controller *hose,pci_dev_t dev,int offset,u16 val)
  318. {
  319. return pcie_write_config(hose,(u32)dev,offset,2,(u32 )val);
  320. }
  321. int pcie_write_config_dword(struct pci_controller *hose,pci_dev_t dev,int offset,u32 val)
  322. {
  323. return pcie_write_config(hose,(u32)dev,offset,3,(u32 )val);
  324. }
  325. #if defined(CONFIG_440SPE)
  326. static void ppc4xx_setup_utl(u32 port) {
  327. volatile void *utl_base = NULL;
  328. /*
  329. * Map UTL registers
  330. */
  331. switch (port) {
  332. case 0:
  333. mtdcr(DCRN_PEGPL_REGBAH(PCIE0), 0x0000000c);
  334. mtdcr(DCRN_PEGPL_REGBAL(PCIE0), 0x20000000);
  335. mtdcr(DCRN_PEGPL_REGMSK(PCIE0), 0x00007001);
  336. mtdcr(DCRN_PEGPL_SPECIAL(PCIE0), 0x68782800);
  337. break;
  338. case 1:
  339. mtdcr(DCRN_PEGPL_REGBAH(PCIE1), 0x0000000c);
  340. mtdcr(DCRN_PEGPL_REGBAL(PCIE1), 0x20001000);
  341. mtdcr(DCRN_PEGPL_REGMSK(PCIE1), 0x00007001);
  342. mtdcr(DCRN_PEGPL_SPECIAL(PCIE1), 0x68782800);
  343. break;
  344. case 2:
  345. mtdcr(DCRN_PEGPL_REGBAH(PCIE2), 0x0000000c);
  346. mtdcr(DCRN_PEGPL_REGBAL(PCIE2), 0x20002000);
  347. mtdcr(DCRN_PEGPL_REGMSK(PCIE2), 0x00007001);
  348. mtdcr(DCRN_PEGPL_SPECIAL(PCIE2), 0x68782800);
  349. break;
  350. }
  351. utl_base = (unsigned int *)(CONFIG_SYS_PCIE_BASE + 0x1000 * port);
  352. /*
  353. * Set buffer allocations and then assert VRB and TXE.
  354. */
  355. out_be32(utl_base + PEUTL_OUTTR, 0x08000000);
  356. out_be32(utl_base + PEUTL_INTR, 0x02000000);
  357. out_be32(utl_base + PEUTL_OPDBSZ, 0x10000000);
  358. out_be32(utl_base + PEUTL_PBBSZ, 0x53000000);
  359. out_be32(utl_base + PEUTL_IPHBSZ, 0x08000000);
  360. out_be32(utl_base + PEUTL_IPDBSZ, 0x10000000);
  361. out_be32(utl_base + PEUTL_RCIRQEN, 0x00f00000);
  362. out_be32(utl_base + PEUTL_PCTL, 0x80800066);
  363. }
  364. static int check_error(void)
  365. {
  366. u32 valPE0, valPE1, valPE2;
  367. int err = 0;
  368. /* SDR0_PEGPLLLCT1 reset */
  369. if (!(valPE0 = SDR_READ(PESDR0_PLLLCT1) & 0x01000000))
  370. printf("PCIE: SDR0_PEGPLLLCT1 reset error 0x%x\n", valPE0);
  371. valPE0 = SDR_READ(PESDR0_RCSSET);
  372. valPE1 = SDR_READ(PESDR1_RCSSET);
  373. valPE2 = SDR_READ(PESDR2_RCSSET);
  374. /* SDR0_PExRCSSET rstgu */
  375. if (!(valPE0 & 0x01000000) ||
  376. !(valPE1 & 0x01000000) ||
  377. !(valPE2 & 0x01000000)) {
  378. printf("PCIE: SDR0_PExRCSSET rstgu error\n");
  379. err = -1;
  380. }
  381. /* SDR0_PExRCSSET rstdl */
  382. if (!(valPE0 & 0x00010000) ||
  383. !(valPE1 & 0x00010000) ||
  384. !(valPE2 & 0x00010000)) {
  385. printf("PCIE: SDR0_PExRCSSET rstdl error\n");
  386. err = -1;
  387. }
  388. /* SDR0_PExRCSSET rstpyn */
  389. if ((valPE0 & 0x00001000) ||
  390. (valPE1 & 0x00001000) ||
  391. (valPE2 & 0x00001000)) {
  392. printf("PCIE: SDR0_PExRCSSET rstpyn error\n");
  393. err = -1;
  394. }
  395. /* SDR0_PExRCSSET hldplb */
  396. if ((valPE0 & 0x10000000) ||
  397. (valPE1 & 0x10000000) ||
  398. (valPE2 & 0x10000000)) {
  399. printf("PCIE: SDR0_PExRCSSET hldplb error\n");
  400. err = -1;
  401. }
  402. /* SDR0_PExRCSSET rdy */
  403. if ((valPE0 & 0x00100000) ||
  404. (valPE1 & 0x00100000) ||
  405. (valPE2 & 0x00100000)) {
  406. printf("PCIE: SDR0_PExRCSSET rdy error\n");
  407. err = -1;
  408. }
  409. /* SDR0_PExRCSSET shutdown */
  410. if ((valPE0 & 0x00000100) ||
  411. (valPE1 & 0x00000100) ||
  412. (valPE2 & 0x00000100)) {
  413. printf("PCIE: SDR0_PExRCSSET shutdown error\n");
  414. err = -1;
  415. }
  416. return err;
  417. }
  418. /*
  419. * Initialize PCI Express core
  420. */
  421. int ppc4xx_init_pcie(void)
  422. {
  423. int time_out = 20;
  424. /* Set PLL clock receiver to LVPECL */
  425. SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) | 1 << 28);
  426. if (check_error()) {
  427. printf("ERROR: failed to set PCIe reference clock receiver --"
  428. "PESDR0_PLLLCT1 = 0x%08x\n", SDR_READ(PESDR0_PLLLCT1));
  429. return -1;
  430. }
  431. /* Did resistance calibration work? */
  432. if (!(SDR_READ(PESDR0_PLLLCT2) & 0x10000)) {
  433. printf("ERROR: PCIe resistance calibration failed --"
  434. "PESDR0_PLLLCT2 = 0x%08x\n", SDR_READ(PESDR0_PLLLCT2));
  435. return -1;
  436. }
  437. /* De-assert reset of PCIe PLL, wait for lock */
  438. SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) & ~(1 << 24));
  439. udelay(300); /* 300 uS is maximum time lock should take */
  440. while (time_out) {
  441. if (!(SDR_READ(PESDR0_PLLLCT3) & 0x10000000)) {
  442. time_out--;
  443. udelay(20); /* Wait 20 uS more if needed */
  444. } else
  445. break;
  446. }
  447. if (!time_out) {
  448. printf("ERROR: PCIe PLL VCO output not locked to ref clock --"
  449. "PESDR0_PLLLCTS=0x%08x\n", SDR_READ(PESDR0_PLLLCT3));
  450. return -1;
  451. }
  452. return 0;
  453. }
  454. #endif
  455. #if defined(CONFIG_460EX) || defined(CONFIG_460GT)
  456. static void ppc4xx_setup_utl(u32 port)
  457. {
  458. volatile void *utl_base = NULL;
  459. /*
  460. * Map UTL registers at 0x0801_n000 (4K 0xfff mask) PEGPLn_REGMSK
  461. */
  462. switch (port) {
  463. case 0:
  464. mtdcr(DCRN_PEGPL_REGBAH(PCIE0), U64_TO_U32_HIGH(CONFIG_SYS_PCIE0_UTLBASE));
  465. mtdcr(DCRN_PEGPL_REGBAL(PCIE0), U64_TO_U32_LOW(CONFIG_SYS_PCIE0_UTLBASE));
  466. mtdcr(DCRN_PEGPL_REGMSK(PCIE0), 0x00007001); /* BAM 11100000=4KB */
  467. mtdcr(DCRN_PEGPL_SPECIAL(PCIE0), 0);
  468. break;
  469. case 1:
  470. mtdcr(DCRN_PEGPL_REGBAH(PCIE1), U64_TO_U32_HIGH(CONFIG_SYS_PCIE0_UTLBASE));
  471. mtdcr(DCRN_PEGPL_REGBAL(PCIE1), U64_TO_U32_LOW(CONFIG_SYS_PCIE0_UTLBASE)
  472. + 0x1000);
  473. mtdcr(DCRN_PEGPL_REGMSK(PCIE1), 0x00007001); /* BAM 11100000=4KB */
  474. mtdcr(DCRN_PEGPL_SPECIAL(PCIE1), 0);
  475. break;
  476. }
  477. utl_base = (unsigned int *)(CONFIG_SYS_PCIE_BASE + 0x1000 * port);
  478. /*
  479. * Set buffer allocations and then assert VRB and TXE.
  480. */
  481. out_be32(utl_base + PEUTL_PBCTL, 0x0800000c); /* PLBME, CRRE */
  482. out_be32(utl_base + PEUTL_OUTTR, 0x08000000);
  483. out_be32(utl_base + PEUTL_INTR, 0x02000000);
  484. out_be32(utl_base + PEUTL_OPDBSZ, 0x04000000); /* OPD = 512 Bytes */
  485. out_be32(utl_base + PEUTL_PBBSZ, 0x00000000); /* Max 512 Bytes */
  486. out_be32(utl_base + PEUTL_IPHBSZ, 0x02000000);
  487. out_be32(utl_base + PEUTL_IPDBSZ, 0x04000000); /* IPD = 512 Bytes */
  488. out_be32(utl_base + PEUTL_RCIRQEN, 0x00f00000);
  489. out_be32(utl_base + PEUTL_PCTL, 0x80800066); /* VRB,TXE,timeout=default */
  490. }
  491. /*
  492. * TODO: double check PCI express SDR based on the latest user manual
  493. * Some registers specified here no longer exist.. has to be
  494. * updated based on the final EAS spec.
  495. */
  496. static int check_error(void)
  497. {
  498. u32 valPE0, valPE1;
  499. int err = 0;
  500. valPE0 = SDR_READ(SDRN_PESDR_RCSSET(0));
  501. valPE1 = SDR_READ(SDRN_PESDR_RCSSET(1));
  502. /* SDR0_PExRCSSET rstgu */
  503. if (!(valPE0 & PESDRx_RCSSET_RSTGU) || !(valPE1 & PESDRx_RCSSET_RSTGU)) {
  504. printf("PCIE: SDR0_PExRCSSET rstgu error\n");
  505. err = -1;
  506. }
  507. /* SDR0_PExRCSSET rstdl */
  508. if (!(valPE0 & PESDRx_RCSSET_RSTDL) || !(valPE1 & PESDRx_RCSSET_RSTDL)) {
  509. printf("PCIE: SDR0_PExRCSSET rstdl error\n");
  510. err = -1;
  511. }
  512. /* SDR0_PExRCSSET rstpyn */
  513. if ((valPE0 & PESDRx_RCSSET_RSTPYN) || (valPE1 & PESDRx_RCSSET_RSTPYN)) {
  514. printf("PCIE: SDR0_PExRCSSET rstpyn error\n");
  515. err = -1;
  516. }
  517. /* SDR0_PExRCSSET hldplb */
  518. if ((valPE0 & PESDRx_RCSSET_HLDPLB) || (valPE1 & PESDRx_RCSSET_HLDPLB)) {
  519. printf("PCIE: SDR0_PExRCSSET hldplb error\n");
  520. err = -1;
  521. }
  522. /* SDR0_PExRCSSET rdy */
  523. if ((valPE0 & PESDRx_RCSSET_RDY) || (valPE1 & PESDRx_RCSSET_RDY)) {
  524. printf("PCIE: SDR0_PExRCSSET rdy error\n");
  525. err = -1;
  526. }
  527. return err;
  528. }
  529. /*
  530. * Initialize PCI Express core as described in User Manual
  531. * TODO: double check PE SDR PLL Register with the updated user manual.
  532. */
  533. int ppc4xx_init_pcie(void)
  534. {
  535. if (check_error())
  536. return -1;
  537. return 0;
  538. }
  539. #endif /* CONFIG_460EX */
  540. #if defined(CONFIG_405EX)
  541. static void ppc4xx_setup_utl(u32 port)
  542. {
  543. u32 utl_base;
  544. /*
  545. * Map UTL registers at 0xef4f_n000 (4K 0xfff mask) PEGPLn_REGMSK
  546. */
  547. switch (port) {
  548. case 0:
  549. mtdcr(DCRN_PEGPL_REGBAH(PCIE0), 0x00000000);
  550. mtdcr(DCRN_PEGPL_REGBAL(PCIE0), CONFIG_SYS_PCIE0_UTLBASE);
  551. mtdcr(DCRN_PEGPL_REGMSK(PCIE0), 0x00007001); /* 4k region, valid */
  552. mtdcr(DCRN_PEGPL_SPECIAL(PCIE0), 0);
  553. break;
  554. case 1:
  555. mtdcr(DCRN_PEGPL_REGBAH(PCIE1), 0x00000000);
  556. mtdcr(DCRN_PEGPL_REGBAL(PCIE1), CONFIG_SYS_PCIE1_UTLBASE);
  557. mtdcr(DCRN_PEGPL_REGMSK(PCIE1), 0x00007001); /* 4k region, valid */
  558. mtdcr(DCRN_PEGPL_SPECIAL(PCIE1), 0);
  559. break;
  560. }
  561. utl_base = (port==0) ? CONFIG_SYS_PCIE0_UTLBASE : CONFIG_SYS_PCIE1_UTLBASE;
  562. /*
  563. * Set buffer allocations and then assert VRB and TXE.
  564. */
  565. out_be32((u32 *)(utl_base + PEUTL_OUTTR), 0x02000000);
  566. out_be32((u32 *)(utl_base + PEUTL_INTR), 0x02000000);
  567. out_be32((u32 *)(utl_base + PEUTL_OPDBSZ), 0x04000000);
  568. out_be32((u32 *)(utl_base + PEUTL_PBBSZ), 0x21000000);
  569. out_be32((u32 *)(utl_base + PEUTL_IPHBSZ), 0x02000000);
  570. out_be32((u32 *)(utl_base + PEUTL_IPDBSZ), 0x04000000);
  571. out_be32((u32 *)(utl_base + PEUTL_RCIRQEN), 0x00f00000);
  572. out_be32((u32 *)(utl_base + PEUTL_PCTL), 0x80800066);
  573. out_be32((u32 *)(utl_base + PEUTL_PBCTL), 0x0800000c);
  574. out_be32((u32 *)(utl_base + PEUTL_RCSTA),
  575. in_be32((u32 *)(utl_base + PEUTL_RCSTA)) | 0x000040000);
  576. }
  577. int ppc4xx_init_pcie(void)
  578. {
  579. /*
  580. * Nothing to do on 405EX
  581. */
  582. return 0;
  583. }
  584. #endif /* CONFIG_405EX */
  585. /*
  586. * Board-specific pcie initialization
  587. * Platform code can reimplement ppc4xx_init_pcie_port_hw() if needed
  588. */
  589. /*
  590. * Initialize various parts of the PCI Express core for our port:
  591. *
  592. * - Set as a root port and enable max width
  593. * (PXIE0 -> X8, PCIE1 and PCIE2 -> X4).
  594. * - Set up UTL configuration.
  595. * - Increase SERDES drive strength to levels suggested by AMCC.
  596. * - De-assert RSTPYN, RSTDL and RSTGU.
  597. *
  598. * NOTICE for 440SPE revB chip: PESDRn_UTLSET2 is not set - we leave it
  599. * with default setting 0x11310000. The register has new fields,
  600. * PESDRn_UTLSET2[LKINE] in particular: clearing it leads to PCIE core
  601. * hang.
  602. */
  603. #if defined(CONFIG_440SPE)
  604. int __ppc4xx_init_pcie_port_hw(int port, int rootport)
  605. {
  606. u32 val = 1 << 24;
  607. u32 utlset1;
  608. if (rootport) {
  609. val = PTYPE_ROOT_PORT << 20;
  610. utlset1 = 0x21222222;
  611. } else {
  612. val = PTYPE_LEGACY_ENDPOINT << 20;
  613. utlset1 = 0x20222222;
  614. }
  615. if (port == 0)
  616. val |= LNKW_X8 << 12;
  617. else
  618. val |= LNKW_X4 << 12;
  619. SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
  620. SDR_WRITE(SDRN_PESDR_UTLSET1(port), utlset1);
  621. if (!ppc440spe_revB())
  622. SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x11000000);
  623. SDR_WRITE(SDRN_PESDR_HSSL0SET1(port), 0x35000000);
  624. SDR_WRITE(SDRN_PESDR_HSSL1SET1(port), 0x35000000);
  625. SDR_WRITE(SDRN_PESDR_HSSL2SET1(port), 0x35000000);
  626. SDR_WRITE(SDRN_PESDR_HSSL3SET1(port), 0x35000000);
  627. if (port == 0) {
  628. SDR_WRITE(PESDR0_HSSL4SET1, 0x35000000);
  629. SDR_WRITE(PESDR0_HSSL5SET1, 0x35000000);
  630. SDR_WRITE(PESDR0_HSSL6SET1, 0x35000000);
  631. SDR_WRITE(PESDR0_HSSL7SET1, 0x35000000);
  632. }
  633. SDR_WRITE(SDRN_PESDR_RCSSET(port), (SDR_READ(SDRN_PESDR_RCSSET(port)) &
  634. ~(1 << 24 | 1 << 16)) | 1 << 12);
  635. return 0;
  636. }
  637. #endif /* CONFIG_440SPE */
  638. #if defined(CONFIG_460EX) || defined(CONFIG_460GT)
  639. int __ppc4xx_init_pcie_port_hw(int port, int rootport)
  640. {
  641. u32 val;
  642. u32 utlset1;
  643. if (rootport)
  644. val = PTYPE_ROOT_PORT << 20;
  645. else
  646. val = PTYPE_LEGACY_ENDPOINT << 20;
  647. if (port == 0) {
  648. val |= LNKW_X1 << 12;
  649. utlset1 = 0x20000000;
  650. } else {
  651. val |= LNKW_X4 << 12;
  652. utlset1 = 0x20101101;
  653. }
  654. SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
  655. SDR_WRITE(SDRN_PESDR_UTLSET1(port), utlset1);
  656. SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x01210000);
  657. switch (port) {
  658. case 0:
  659. SDR_WRITE(PESDR0_L0CDRCTL, 0x00003230);
  660. SDR_WRITE(PESDR0_L0DRV, 0x00000130);
  661. SDR_WRITE(PESDR0_L0CLK, 0x00000006);
  662. SDR_WRITE(PESDR0_PHY_CTL_RST,0x10000000);
  663. break;
  664. case 1:
  665. SDR_WRITE(PESDR1_L0CDRCTL, 0x00003230);
  666. SDR_WRITE(PESDR1_L1CDRCTL, 0x00003230);
  667. SDR_WRITE(PESDR1_L2CDRCTL, 0x00003230);
  668. SDR_WRITE(PESDR1_L3CDRCTL, 0x00003230);
  669. SDR_WRITE(PESDR1_L0DRV, 0x00000130);
  670. SDR_WRITE(PESDR1_L1DRV, 0x00000130);
  671. SDR_WRITE(PESDR1_L2DRV, 0x00000130);
  672. SDR_WRITE(PESDR1_L3DRV, 0x00000130);
  673. SDR_WRITE(PESDR1_L0CLK, 0x00000006);
  674. SDR_WRITE(PESDR1_L1CLK, 0x00000006);
  675. SDR_WRITE(PESDR1_L2CLK, 0x00000006);
  676. SDR_WRITE(PESDR1_L3CLK, 0x00000006);
  677. SDR_WRITE(PESDR1_PHY_CTL_RST,0x10000000);
  678. break;
  679. }
  680. SDR_WRITE(SDRN_PESDR_RCSSET(port), SDR_READ(SDRN_PESDR_RCSSET(port)) |
  681. (PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTPYN));
  682. /* Poll for PHY reset */
  683. switch (port) {
  684. case 0:
  685. while (!(SDR_READ(PESDR0_RSTSTA) & 0x1))
  686. udelay(10);
  687. break;
  688. case 1:
  689. while (!(SDR_READ(PESDR1_RSTSTA) & 0x1))
  690. udelay(10);
  691. break;
  692. }
  693. SDR_WRITE(SDRN_PESDR_RCSSET(port),
  694. (SDR_READ(SDRN_PESDR_RCSSET(port)) &
  695. ~(PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTDL)) |
  696. PESDRx_RCSSET_RSTPYN);
  697. return 0;
  698. }
  699. #endif /* CONFIG_440SPE */
  700. #if defined(CONFIG_405EX)
  701. int __ppc4xx_init_pcie_port_hw(int port, int rootport)
  702. {
  703. u32 val;
  704. if (rootport)
  705. val = 0x00401000;
  706. else
  707. val = 0x00101000;
  708. SDR_WRITE(SDRN_PESDR_DLPSET(port), val);
  709. SDR_WRITE(SDRN_PESDR_UTLSET1(port), 0x00000000);
  710. SDR_WRITE(SDRN_PESDR_UTLSET2(port), 0x01010000);
  711. SDR_WRITE(SDRN_PESDR_PHYSET1(port), 0x720F0000);
  712. SDR_WRITE(SDRN_PESDR_PHYSET2(port), 0x70600003);
  713. /* Assert the PE0_PHY reset */
  714. SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01010000);
  715. udelay(1000);
  716. /* deassert the PE0_hotreset */
  717. if (is_end_point(port))
  718. SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01111000);
  719. else
  720. SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x01101000);
  721. /* poll for phy !reset */
  722. while (!(SDR_READ(SDRN_PESDR_PHYSTA(port)) & 0x00001000))
  723. ;
  724. /* deassert the PE0_gpl_utl_reset */
  725. SDR_WRITE(SDRN_PESDR_RCSSET(port), 0x00101000);
  726. if (port == 0)
  727. mtdcr(DCRN_PEGPL_CFG(PCIE0), 0x10000000); /* guarded on */
  728. else
  729. mtdcr(DCRN_PEGPL_CFG(PCIE1), 0x10000000); /* guarded on */
  730. return 0;
  731. }
  732. #endif /* CONFIG_405EX */
  733. int ppc4xx_init_pcie_port_hw(int port, int rootport)
  734. __attribute__((weak, alias("__ppc4xx_init_pcie_port_hw")));
  735. /*
  736. * We map PCI Express configuration access into the 512MB regions
  737. *
  738. * NOTICE: revB is very strict about PLB real addressess and ranges to
  739. * be mapped for config space; it seems to only work with d_nnnn_nnnn
  740. * range (hangs the core upon config transaction attempts when set
  741. * otherwise) while revA uses c_nnnn_nnnn.
  742. *
  743. * For 440SPe revA:
  744. * PCIE0: 0xc_4000_0000
  745. * PCIE1: 0xc_8000_0000
  746. * PCIE2: 0xc_c000_0000
  747. *
  748. * For 440SPe revB:
  749. * PCIE0: 0xd_0000_0000
  750. * PCIE1: 0xd_2000_0000
  751. * PCIE2: 0xd_4000_0000
  752. *
  753. * For 405EX:
  754. * PCIE0: 0xa000_0000
  755. * PCIE1: 0xc000_0000
  756. *
  757. * For 460EX/GT:
  758. * PCIE0: 0xd_0000_0000
  759. * PCIE1: 0xd_2000_0000
  760. */
  761. static inline u64 ppc4xx_get_cfgaddr(int port)
  762. {
  763. #if defined(CONFIG_405EX)
  764. if (port == 0)
  765. return (u64)CONFIG_SYS_PCIE0_CFGBASE;
  766. else
  767. return (u64)CONFIG_SYS_PCIE1_CFGBASE;
  768. #endif
  769. #if defined(CONFIG_440SPE)
  770. if (ppc440spe_revB()) {
  771. switch (port) {
  772. default: /* to satisfy compiler */
  773. case 0:
  774. return 0x0000000d00000000ULL;
  775. case 1:
  776. return 0x0000000d20000000ULL;
  777. case 2:
  778. return 0x0000000d40000000ULL;
  779. }
  780. } else {
  781. switch (port) {
  782. default: /* to satisfy compiler */
  783. case 0:
  784. return 0x0000000c40000000ULL;
  785. case 1:
  786. return 0x0000000c80000000ULL;
  787. case 2:
  788. return 0x0000000cc0000000ULL;
  789. }
  790. }
  791. #endif
  792. #if defined(CONFIG_460EX) || defined(CONFIG_460GT)
  793. if (port == 0)
  794. return 0x0000000d00000000ULL;
  795. else
  796. return 0x0000000d20000000ULL;
  797. #endif
  798. }
  799. /*
  800. * 4xx boards as end point and root point setup
  801. * and
  802. * testing inbound and out bound windows
  803. *
  804. * 4xx boards can be plugged into another 4xx boards or you can get PCI-E
  805. * cable which can be used to setup loop back from one port to another port.
  806. * Please rememeber that unless there is a endpoint plugged in to root port it
  807. * will not initialize. It is the same in case of endpoint , unless there is
  808. * root port attached it will not initialize.
  809. *
  810. * In this release of software all the PCI-E ports are configured as either
  811. * endpoint or rootpoint.In future we will have support for selective ports
  812. * setup as endpoint and root point in single board.
  813. *
  814. * Once your board came up as root point , you can verify by reading
  815. * /proc/bus/pci/devices. Where you can see the configuration registers
  816. * of end point device attached to the port.
  817. *
  818. * Enpoint cofiguration can be verified by connecting 4xx board to any
  819. * host or another 4xx board. Then try to scan the device. In case of
  820. * linux use "lspci" or appripriate os command.
  821. *
  822. * How do I verify the inbound and out bound windows ? (4xx to 4xx)
  823. * in this configuration inbound and outbound windows are setup to access
  824. * sram memroy area. SRAM is at 0x4 0000 0000 , on PLB bus. This address
  825. * is mapped at 0x90000000. From u-boot prompt write data 0xb000 0000,
  826. * This is waere your POM(PLB out bound memory window) mapped. then
  827. * read the data from other 4xx board's u-boot prompt at address
  828. * 0x9000 0000(SRAM). Data should match.
  829. * In case of inbound , write data to u-boot command prompt at 0xb000 0000
  830. * which is mapped to 0x4 0000 0000. Now on rootpoint yucca u-boot prompt check
  831. * data at 0x9000 0000(SRAM).Data should match.
  832. */
  833. int ppc4xx_init_pcie_port(int port, int rootport)
  834. {
  835. static int core_init;
  836. volatile u32 val = 0;
  837. int attempts;
  838. u64 addr;
  839. u32 low, high;
  840. if (!core_init) {
  841. if (ppc4xx_init_pcie())
  842. return -1;
  843. ++core_init;
  844. }
  845. /*
  846. * Initialize various parts of the PCI Express core for our port
  847. */
  848. ppc4xx_init_pcie_port_hw(port, rootport);
  849. /*
  850. * Notice: the following delay has critical impact on device
  851. * initialization - if too short (<50ms) the link doesn't get up.
  852. */
  853. mdelay(100);
  854. val = SDR_READ(SDRN_PESDR_RCSSTS(port));
  855. if (val & (1 << 20)) {
  856. printf("PCIE%d: PGRST failed %08x\n", port, val);
  857. return -1;
  858. }
  859. /*
  860. * Verify link is up
  861. */
  862. val = SDR_READ(SDRN_PESDR_LOOP(port));
  863. if (!(val & 0x00001000)) {
  864. printf("PCIE%d: link is not up.\n", port);
  865. return -ENODEV;
  866. }
  867. /*
  868. * Setup UTL registers - but only on revA!
  869. * We use default settings for revB chip.
  870. */
  871. if (!ppc440spe_revB())
  872. ppc4xx_setup_utl(port);
  873. /*
  874. * We map PCI Express configuration access into the 512MB regions
  875. */
  876. addr = ppc4xx_get_cfgaddr(port);
  877. low = U64_TO_U32_LOW(addr);
  878. high = U64_TO_U32_HIGH(addr);
  879. switch (port) {
  880. case 0:
  881. mtdcr(DCRN_PEGPL_CFGBAH(PCIE0), high);
  882. mtdcr(DCRN_PEGPL_CFGBAL(PCIE0), low);
  883. mtdcr(DCRN_PEGPL_CFGMSK(PCIE0), 0xe0000001); /* 512MB region, valid */
  884. break;
  885. case 1:
  886. mtdcr(DCRN_PEGPL_CFGBAH(PCIE1), high);
  887. mtdcr(DCRN_PEGPL_CFGBAL(PCIE1), low);
  888. mtdcr(DCRN_PEGPL_CFGMSK(PCIE1), 0xe0000001); /* 512MB region, valid */
  889. break;
  890. #if CONFIG_SYS_PCIE_NR_PORTS > 2
  891. case 2:
  892. mtdcr(DCRN_PEGPL_CFGBAH(PCIE2), high);
  893. mtdcr(DCRN_PEGPL_CFGBAL(PCIE2), low);
  894. mtdcr(DCRN_PEGPL_CFGMSK(PCIE2), 0xe0000001); /* 512MB region, valid */
  895. break;
  896. #endif
  897. }
  898. /*
  899. * Check for VC0 active and assert RDY.
  900. */
  901. attempts = 10;
  902. while(!(SDR_READ(SDRN_PESDR_RCSSTS(port)) & (1 << 16))) {
  903. if (!(attempts--)) {
  904. printf("PCIE%d: VC0 not active\n", port);
  905. return -1;
  906. }
  907. mdelay(1000);
  908. }
  909. SDR_WRITE(SDRN_PESDR_RCSSET(port),
  910. SDR_READ(SDRN_PESDR_RCSSET(port)) | 1 << 20);
  911. mdelay(100);
  912. return 0;
  913. }
  914. int ppc4xx_init_pcie_rootport(int port)
  915. {
  916. return ppc4xx_init_pcie_port(port, 1);
  917. }
  918. int ppc4xx_init_pcie_endport(int port)
  919. {
  920. return ppc4xx_init_pcie_port(port, 0);
  921. }
  922. void ppc4xx_setup_pcie_rootpoint(struct pci_controller *hose, int port)
  923. {
  924. volatile void *mbase = NULL;
  925. volatile void *rmbase = NULL;
  926. pci_set_ops(hose,
  927. pcie_read_config_byte,
  928. pcie_read_config_word,
  929. pcie_read_config_dword,
  930. pcie_write_config_byte,
  931. pcie_write_config_word,
  932. pcie_write_config_dword);
  933. switch (port) {
  934. case 0:
  935. mbase = (u32 *)CONFIG_SYS_PCIE0_XCFGBASE;
  936. rmbase = (u32 *)CONFIG_SYS_PCIE0_CFGBASE;
  937. hose->cfg_data = (u8 *)CONFIG_SYS_PCIE0_CFGBASE;
  938. break;
  939. case 1:
  940. mbase = (u32 *)CONFIG_SYS_PCIE1_XCFGBASE;
  941. rmbase = (u32 *)CONFIG_SYS_PCIE1_CFGBASE;
  942. hose->cfg_data = (u8 *)CONFIG_SYS_PCIE1_CFGBASE;
  943. break;
  944. #if CONFIG_SYS_PCIE_NR_PORTS > 2
  945. case 2:
  946. mbase = (u32 *)CONFIG_SYS_PCIE2_XCFGBASE;
  947. rmbase = (u32 *)CONFIG_SYS_PCIE2_CFGBASE;
  948. hose->cfg_data = (u8 *)CONFIG_SYS_PCIE2_CFGBASE;
  949. break;
  950. #endif
  951. }
  952. /*
  953. * Set bus numbers on our root port
  954. */
  955. out_8((u8 *)mbase + PCI_PRIMARY_BUS, 0);
  956. out_8((u8 *)mbase + PCI_SECONDARY_BUS, 1);
  957. out_8((u8 *)mbase + PCI_SUBORDINATE_BUS, 1);
  958. /*
  959. * Set up outbound translation to hose->mem_space from PLB
  960. * addresses at an offset of 0xd_0000_0000. We set the low
  961. * bits of the mask to 11 to turn off splitting into 8
  962. * subregions and to enable the outbound translation.
  963. */
  964. out_le32(mbase + PECFG_POM0LAH, 0x00000000);
  965. out_le32(mbase + PECFG_POM0LAL, CONFIG_SYS_PCIE_MEMBASE +
  966. port * CONFIG_SYS_PCIE_MEMSIZE);
  967. debug("PECFG_POM0LA=%08x.%08x\n", in_le32(mbase + PECFG_POM0LAH),
  968. in_le32(mbase + PECFG_POM0LAL));
  969. switch (port) {
  970. case 0:
  971. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), CONFIG_SYS_PCIE_ADDR_HIGH);
  972. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), CONFIG_SYS_PCIE_MEMBASE +
  973. port * CONFIG_SYS_PCIE_MEMSIZE);
  974. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
  975. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
  976. ~(CONFIG_SYS_PCIE_MEMSIZE - 1) | 3);
  977. debug("0:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
  978. mfdcr(DCRN_PEGPL_OMR1BAH(PCIE0)),
  979. mfdcr(DCRN_PEGPL_OMR1BAL(PCIE0)),
  980. mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE0)),
  981. mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE0)));
  982. break;
  983. case 1:
  984. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), CONFIG_SYS_PCIE_ADDR_HIGH);
  985. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), CONFIG_SYS_PCIE_MEMBASE +
  986. port * CONFIG_SYS_PCIE_MEMSIZE);
  987. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
  988. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
  989. ~(CONFIG_SYS_PCIE_MEMSIZE - 1) | 3);
  990. debug("1:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
  991. mfdcr(DCRN_PEGPL_OMR1BAH(PCIE1)),
  992. mfdcr(DCRN_PEGPL_OMR1BAL(PCIE1)),
  993. mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE1)),
  994. mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE1)));
  995. break;
  996. #if CONFIG_SYS_PCIE_NR_PORTS > 2
  997. case 2:
  998. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), CONFIG_SYS_PCIE_ADDR_HIGH);
  999. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), CONFIG_SYS_PCIE_MEMBASE +
  1000. port * CONFIG_SYS_PCIE_MEMSIZE);
  1001. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
  1002. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
  1003. ~(CONFIG_SYS_PCIE_MEMSIZE - 1) | 3);
  1004. debug("2:PEGPL_OMR1BA=%08x.%08x MSK=%08x.%08x\n",
  1005. mfdcr(DCRN_PEGPL_OMR1BAH(PCIE2)),
  1006. mfdcr(DCRN_PEGPL_OMR1BAL(PCIE2)),
  1007. mfdcr(DCRN_PEGPL_OMR1MSKH(PCIE2)),
  1008. mfdcr(DCRN_PEGPL_OMR1MSKL(PCIE2)));
  1009. break;
  1010. #endif
  1011. }
  1012. /* Set up 4GB inbound memory window at 0 */
  1013. out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
  1014. out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
  1015. out_le32(mbase + PECFG_BAR0HMPA, 0x7ffffff);
  1016. out_le32(mbase + PECFG_BAR0LMPA, 0);
  1017. out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
  1018. out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
  1019. out_le32(mbase + PECFG_PIM0LAL, 0);
  1020. out_le32(mbase + PECFG_PIM0LAH, 0);
  1021. out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
  1022. out_le32(mbase + PECFG_PIM1LAH, 0x00000004);
  1023. out_le32(mbase + PECFG_PIMEN, 0x1);
  1024. /* Enable I/O, Mem, and Busmaster cycles */
  1025. out_le16((u16 *)(mbase + PCI_COMMAND),
  1026. in_le16((u16 *)(mbase + PCI_COMMAND)) |
  1027. PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  1028. /* Set Device and Vendor Id */
  1029. out_le16(mbase + 0x200, 0xaaa0 + port);
  1030. out_le16(mbase + 0x202, 0xbed0 + port);
  1031. /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
  1032. out_le32(mbase + 0x208, 0x06040001);
  1033. printf("PCIE%d: successfully set as root-complex\n", port);
  1034. }
  1035. int ppc4xx_setup_pcie_endpoint(struct pci_controller *hose, int port)
  1036. {
  1037. volatile void *mbase = NULL;
  1038. int attempts = 0;
  1039. pci_set_ops(hose,
  1040. pcie_read_config_byte,
  1041. pcie_read_config_word,
  1042. pcie_read_config_dword,
  1043. pcie_write_config_byte,
  1044. pcie_write_config_word,
  1045. pcie_write_config_dword);
  1046. switch (port) {
  1047. case 0:
  1048. mbase = (u32 *)CONFIG_SYS_PCIE0_XCFGBASE;
  1049. hose->cfg_data = (u8 *)CONFIG_SYS_PCIE0_CFGBASE;
  1050. break;
  1051. case 1:
  1052. mbase = (u32 *)CONFIG_SYS_PCIE1_XCFGBASE;
  1053. hose->cfg_data = (u8 *)CONFIG_SYS_PCIE1_CFGBASE;
  1054. break;
  1055. #if defined(CONFIG_SYS_PCIE2_CFGBASE)
  1056. case 2:
  1057. mbase = (u32 *)CONFIG_SYS_PCIE2_XCFGBASE;
  1058. hose->cfg_data = (u8 *)CONFIG_SYS_PCIE2_CFGBASE;
  1059. break;
  1060. #endif
  1061. }
  1062. /*
  1063. * Set up outbound translation to hose->mem_space from PLB
  1064. * addresses at an offset of 0xd_0000_0000. We set the low
  1065. * bits of the mask to 11 to turn off splitting into 8
  1066. * subregions and to enable the outbound translation.
  1067. */
  1068. out_le32(mbase + PECFG_POM0LAH, 0x00001ff8);
  1069. out_le32(mbase + PECFG_POM0LAL, 0x00001000);
  1070. switch (port) {
  1071. case 0:
  1072. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), CONFIG_SYS_PCIE_ADDR_HIGH);
  1073. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), CONFIG_SYS_PCIE_MEMBASE +
  1074. port * CONFIG_SYS_PCIE_MEMSIZE);
  1075. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
  1076. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
  1077. ~(CONFIG_SYS_PCIE_MEMSIZE - 1) | 3);
  1078. break;
  1079. case 1:
  1080. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), CONFIG_SYS_PCIE_ADDR_HIGH);
  1081. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), CONFIG_SYS_PCIE_MEMBASE +
  1082. port * CONFIG_SYS_PCIE_MEMSIZE);
  1083. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
  1084. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
  1085. ~(CONFIG_SYS_PCIE_MEMSIZE - 1) | 3);
  1086. break;
  1087. #if CONFIG_SYS_PCIE_NR_PORTS > 2
  1088. case 2:
  1089. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), CONFIG_SYS_PCIE_ADDR_HIGH);
  1090. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), CONFIG_SYS_PCIE_MEMBASE +
  1091. port * CONFIG_SYS_PCIE_MEMSIZE);
  1092. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
  1093. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
  1094. ~(CONFIG_SYS_PCIE_MEMSIZE - 1) | 3);
  1095. break;
  1096. #endif
  1097. }
  1098. /* Set up 64MB inbound memory window at 0 */
  1099. out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
  1100. out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
  1101. out_le32(mbase + PECFG_PIM01SAH, 0xffffffff);
  1102. out_le32(mbase + PECFG_PIM01SAL, 0xfc000000);
  1103. /* Setup BAR0 */
  1104. out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffff);
  1105. out_le32(mbase + PECFG_BAR0LMPA, 0xfc000000 | PCI_BASE_ADDRESS_MEM_TYPE_64);
  1106. /* Disable BAR1 & BAR2 */
  1107. out_le32(mbase + PECFG_BAR1MPA, 0);
  1108. out_le32(mbase + PECFG_BAR2HMPA, 0);
  1109. out_le32(mbase + PECFG_BAR2LMPA, 0);
  1110. out_le32(mbase + PECFG_PIM0LAL, U64_TO_U32_LOW(CONFIG_SYS_PCIE_INBOUND_BASE));
  1111. out_le32(mbase + PECFG_PIM0LAH, U64_TO_U32_HIGH(CONFIG_SYS_PCIE_INBOUND_BASE));
  1112. out_le32(mbase + PECFG_PIMEN, 0x1);
  1113. /* Enable I/O, Mem, and Busmaster cycles */
  1114. out_le16((u16 *)(mbase + PCI_COMMAND),
  1115. in_le16((u16 *)(mbase + PCI_COMMAND)) |
  1116. PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  1117. out_le16(mbase + 0x200, 0xcaad); /* Setting vendor ID */
  1118. out_le16(mbase + 0x202, 0xfeed); /* Setting device ID */
  1119. /* Set Class Code to Processor/PPC */
  1120. out_le32(mbase + 0x208, 0x0b200001);
  1121. attempts = 10;
  1122. while(!(SDR_READ(SDRN_PESDR_RCSSTS(port)) & (1 << 8))) {
  1123. if (!(attempts--)) {
  1124. printf("PCIE%d: BME not active\n", port);
  1125. return -1;
  1126. }
  1127. mdelay(1000);
  1128. }
  1129. printf("PCIE%d: successfully set as endpoint\n", port);
  1130. return 0;
  1131. }
  1132. #endif /* CONFIG_440SPE && CONFIG_PCI */