4xx_pcie.c 35 KB

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