4xx_pcie.c 34 KB

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