4xx_pcie.c 35 KB

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