ppc4xx_pci.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. /*
  2. * PCI / PCI-X / PCI-Express support for 4xx parts
  3. *
  4. * Copyright 2007 Ben. Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
  5. *
  6. * Most PCI Express code is coming from Stefan Roese implementation for
  7. * arch/ppc in the Denx tree, slightly reworked by me.
  8. *
  9. * Copyright 2007 DENX Software Engineering, Stefan Roese <sr@denx.de>
  10. *
  11. * Some of that comes itself from a previous implementation for 440SPE only
  12. * by Roland Dreier:
  13. *
  14. * Copyright (c) 2005 Cisco Systems. All rights reserved.
  15. * Roland Dreier <rolandd@cisco.com>
  16. *
  17. */
  18. #undef DEBUG
  19. #include <linux/kernel.h>
  20. #include <linux/pci.h>
  21. #include <linux/init.h>
  22. #include <linux/of.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/delay.h>
  25. #include <asm/io.h>
  26. #include <asm/pci-bridge.h>
  27. #include <asm/machdep.h>
  28. #include <asm/dcr.h>
  29. #include <asm/dcr-regs.h>
  30. #include <mm/mmu_decl.h>
  31. #include "ppc4xx_pci.h"
  32. static int dma_offset_set;
  33. #define U64_TO_U32_LOW(val) ((u32)((val) & 0x00000000ffffffffULL))
  34. #define U64_TO_U32_HIGH(val) ((u32)((val) >> 32))
  35. #define RES_TO_U32_LOW(val) \
  36. ((sizeof(resource_size_t) > sizeof(u32)) ? U64_TO_U32_LOW(val) : (val))
  37. #define RES_TO_U32_HIGH(val) \
  38. ((sizeof(resource_size_t) > sizeof(u32)) ? U64_TO_U32_HIGH(val) : (0))
  39. static inline int ppc440spe_revA(void)
  40. {
  41. /* Catch both 440SPe variants, with and without RAID6 support */
  42. if ((mfspr(SPRN_PVR) & 0xffefffff) == 0x53421890)
  43. return 1;
  44. else
  45. return 0;
  46. }
  47. static void fixup_ppc4xx_pci_bridge(struct pci_dev *dev)
  48. {
  49. struct pci_controller *hose;
  50. int i;
  51. if (dev->devfn != 0 || dev->bus->self != NULL)
  52. return;
  53. hose = pci_bus_to_host(dev->bus);
  54. if (hose == NULL)
  55. return;
  56. if (!of_device_is_compatible(hose->dn, "ibm,plb-pciex") &&
  57. !of_device_is_compatible(hose->dn, "ibm,plb-pcix") &&
  58. !of_device_is_compatible(hose->dn, "ibm,plb-pci"))
  59. return;
  60. if (of_device_is_compatible(hose->dn, "ibm,plb440epx-pci") ||
  61. of_device_is_compatible(hose->dn, "ibm,plb440grx-pci")) {
  62. hose->indirect_type |= PPC_INDIRECT_TYPE_BROKEN_MRM;
  63. }
  64. /* Hide the PCI host BARs from the kernel as their content doesn't
  65. * fit well in the resource management
  66. */
  67. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  68. dev->resource[i].start = dev->resource[i].end = 0;
  69. dev->resource[i].flags = 0;
  70. }
  71. printk(KERN_INFO "PCI: Hiding 4xx host bridge resources %s\n",
  72. pci_name(dev));
  73. }
  74. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, fixup_ppc4xx_pci_bridge);
  75. static int __init ppc4xx_parse_dma_ranges(struct pci_controller *hose,
  76. void __iomem *reg,
  77. struct resource *res)
  78. {
  79. u64 size;
  80. const u32 *ranges;
  81. int rlen;
  82. int pna = of_n_addr_cells(hose->dn);
  83. int np = pna + 5;
  84. /* Default */
  85. res->start = 0;
  86. size = 0x80000000;
  87. res->end = size - 1;
  88. res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  89. /* Get dma-ranges property */
  90. ranges = of_get_property(hose->dn, "dma-ranges", &rlen);
  91. if (ranges == NULL)
  92. goto out;
  93. /* Walk it */
  94. while ((rlen -= np * 4) >= 0) {
  95. u32 pci_space = ranges[0];
  96. u64 pci_addr = of_read_number(ranges + 1, 2);
  97. u64 cpu_addr = of_translate_dma_address(hose->dn, ranges + 3);
  98. size = of_read_number(ranges + pna + 3, 2);
  99. ranges += np;
  100. if (cpu_addr == OF_BAD_ADDR || size == 0)
  101. continue;
  102. /* We only care about memory */
  103. if ((pci_space & 0x03000000) != 0x02000000)
  104. continue;
  105. /* We currently only support memory at 0, and pci_addr
  106. * within 32 bits space
  107. */
  108. if (cpu_addr != 0 || pci_addr > 0xffffffff) {
  109. printk(KERN_WARNING "%s: Ignored unsupported dma range"
  110. " 0x%016llx...0x%016llx -> 0x%016llx\n",
  111. hose->dn->full_name,
  112. pci_addr, pci_addr + size - 1, cpu_addr);
  113. continue;
  114. }
  115. /* Check if not prefetchable */
  116. if (!(pci_space & 0x40000000))
  117. res->flags &= ~IORESOURCE_PREFETCH;
  118. /* Use that */
  119. res->start = pci_addr;
  120. /* Beware of 32 bits resources */
  121. if (sizeof(resource_size_t) == sizeof(u32) &&
  122. (pci_addr + size) > 0x100000000ull)
  123. res->end = 0xffffffff;
  124. else
  125. res->end = res->start + size - 1;
  126. break;
  127. }
  128. /* We only support one global DMA offset */
  129. if (dma_offset_set && pci_dram_offset != res->start) {
  130. printk(KERN_ERR "%s: dma-ranges(s) mismatch\n",
  131. hose->dn->full_name);
  132. return -ENXIO;
  133. }
  134. /* Check that we can fit all of memory as we don't support
  135. * DMA bounce buffers
  136. */
  137. if (size < total_memory) {
  138. printk(KERN_ERR "%s: dma-ranges too small "
  139. "(size=%llx total_memory=%llx)\n",
  140. hose->dn->full_name, size, (u64)total_memory);
  141. return -ENXIO;
  142. }
  143. /* Check we are a power of 2 size and that base is a multiple of size*/
  144. if ((size & (size - 1)) != 0 ||
  145. (res->start & (size - 1)) != 0) {
  146. printk(KERN_ERR "%s: dma-ranges unaligned\n",
  147. hose->dn->full_name);
  148. return -ENXIO;
  149. }
  150. /* Check that we are fully contained within 32 bits space */
  151. if (res->end > 0xffffffff) {
  152. printk(KERN_ERR "%s: dma-ranges outside of 32 bits space\n",
  153. hose->dn->full_name);
  154. return -ENXIO;
  155. }
  156. out:
  157. dma_offset_set = 1;
  158. pci_dram_offset = res->start;
  159. printk(KERN_INFO "4xx PCI DMA offset set to 0x%08lx\n",
  160. pci_dram_offset);
  161. return 0;
  162. }
  163. /*
  164. * 4xx PCI 2.x part
  165. */
  166. static int __init ppc4xx_setup_one_pci_PMM(struct pci_controller *hose,
  167. void __iomem *reg,
  168. u64 plb_addr,
  169. u64 pci_addr,
  170. u64 size,
  171. unsigned int flags,
  172. int index)
  173. {
  174. u32 ma, pcila, pciha;
  175. /* Hack warning ! The "old" PCI 2.x cell only let us configure the low
  176. * 32-bit of incoming PLB addresses. The top 4 bits of the 36-bit
  177. * address are actually hard wired to a value that appears to depend
  178. * on the specific SoC. For example, it's 0 on 440EP and 1 on 440EPx.
  179. *
  180. * The trick here is we just crop those top bits and ignore them when
  181. * programming the chip. That means the device-tree has to be right
  182. * for the specific part used (we don't print a warning if it's wrong
  183. * but on the other hand, you'll crash quickly enough), but at least
  184. * this code should work whatever the hard coded value is
  185. */
  186. plb_addr &= 0xffffffffull;
  187. /* Note: Due to the above hack, the test below doesn't actually test
  188. * if you address is above 4G, but it tests that address and
  189. * (address + size) are both contained in the same 4G
  190. */
  191. if ((plb_addr + size) > 0xffffffffull || !is_power_of_2(size) ||
  192. size < 0x1000 || (plb_addr & (size - 1)) != 0) {
  193. printk(KERN_WARNING "%s: Resource out of range\n",
  194. hose->dn->full_name);
  195. return -1;
  196. }
  197. ma = (0xffffffffu << ilog2(size)) | 1;
  198. if (flags & IORESOURCE_PREFETCH)
  199. ma |= 2;
  200. pciha = RES_TO_U32_HIGH(pci_addr);
  201. pcila = RES_TO_U32_LOW(pci_addr);
  202. writel(plb_addr, reg + PCIL0_PMM0LA + (0x10 * index));
  203. writel(pcila, reg + PCIL0_PMM0PCILA + (0x10 * index));
  204. writel(pciha, reg + PCIL0_PMM0PCIHA + (0x10 * index));
  205. writel(ma, reg + PCIL0_PMM0MA + (0x10 * index));
  206. return 0;
  207. }
  208. static void __init ppc4xx_configure_pci_PMMs(struct pci_controller *hose,
  209. void __iomem *reg)
  210. {
  211. int i, j, found_isa_hole = 0;
  212. /* Setup outbound memory windows */
  213. for (i = j = 0; i < 3; i++) {
  214. struct resource *res = &hose->mem_resources[i];
  215. /* we only care about memory windows */
  216. if (!(res->flags & IORESOURCE_MEM))
  217. continue;
  218. if (j > 2) {
  219. printk(KERN_WARNING "%s: Too many ranges\n",
  220. hose->dn->full_name);
  221. break;
  222. }
  223. /* Configure the resource */
  224. if (ppc4xx_setup_one_pci_PMM(hose, reg,
  225. res->start,
  226. res->start - hose->pci_mem_offset,
  227. res->end + 1 - res->start,
  228. res->flags,
  229. j) == 0) {
  230. j++;
  231. /* If the resource PCI address is 0 then we have our
  232. * ISA memory hole
  233. */
  234. if (res->start == hose->pci_mem_offset)
  235. found_isa_hole = 1;
  236. }
  237. }
  238. /* Handle ISA memory hole if not already covered */
  239. if (j <= 2 && !found_isa_hole && hose->isa_mem_size)
  240. if (ppc4xx_setup_one_pci_PMM(hose, reg, hose->isa_mem_phys, 0,
  241. hose->isa_mem_size, 0, j) == 0)
  242. printk(KERN_INFO "%s: Legacy ISA memory support enabled\n",
  243. hose->dn->full_name);
  244. }
  245. static void __init ppc4xx_configure_pci_PTMs(struct pci_controller *hose,
  246. void __iomem *reg,
  247. const struct resource *res)
  248. {
  249. resource_size_t size = res->end - res->start + 1;
  250. u32 sa;
  251. /* Calculate window size */
  252. sa = (0xffffffffu << ilog2(size)) | 1;
  253. sa |= 0x1;
  254. /* RAM is always at 0 local for now */
  255. writel(0, reg + PCIL0_PTM1LA);
  256. writel(sa, reg + PCIL0_PTM1MS);
  257. /* Map on PCI side */
  258. early_write_config_dword(hose, hose->first_busno, 0,
  259. PCI_BASE_ADDRESS_1, res->start);
  260. early_write_config_dword(hose, hose->first_busno, 0,
  261. PCI_BASE_ADDRESS_2, 0x00000000);
  262. early_write_config_word(hose, hose->first_busno, 0,
  263. PCI_COMMAND, 0x0006);
  264. }
  265. static void __init ppc4xx_probe_pci_bridge(struct device_node *np)
  266. {
  267. /* NYI */
  268. struct resource rsrc_cfg;
  269. struct resource rsrc_reg;
  270. struct resource dma_window;
  271. struct pci_controller *hose = NULL;
  272. void __iomem *reg = NULL;
  273. const int *bus_range;
  274. int primary = 0;
  275. /* Check if device is enabled */
  276. if (!of_device_is_available(np)) {
  277. printk(KERN_INFO "%s: Port disabled via device-tree\n",
  278. np->full_name);
  279. return;
  280. }
  281. /* Fetch config space registers address */
  282. if (of_address_to_resource(np, 0, &rsrc_cfg)) {
  283. printk(KERN_ERR "%s: Can't get PCI config register base !",
  284. np->full_name);
  285. return;
  286. }
  287. /* Fetch host bridge internal registers address */
  288. if (of_address_to_resource(np, 3, &rsrc_reg)) {
  289. printk(KERN_ERR "%s: Can't get PCI internal register base !",
  290. np->full_name);
  291. return;
  292. }
  293. /* Check if primary bridge */
  294. if (of_get_property(np, "primary", NULL))
  295. primary = 1;
  296. /* Get bus range if any */
  297. bus_range = of_get_property(np, "bus-range", NULL);
  298. /* Map registers */
  299. reg = ioremap(rsrc_reg.start, rsrc_reg.end + 1 - rsrc_reg.start);
  300. if (reg == NULL) {
  301. printk(KERN_ERR "%s: Can't map registers !", np->full_name);
  302. goto fail;
  303. }
  304. /* Allocate the host controller data structure */
  305. hose = pcibios_alloc_controller(np);
  306. if (!hose)
  307. goto fail;
  308. hose->first_busno = bus_range ? bus_range[0] : 0x0;
  309. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  310. /* Setup config space */
  311. setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 0x4, 0);
  312. /* Disable all windows */
  313. writel(0, reg + PCIL0_PMM0MA);
  314. writel(0, reg + PCIL0_PMM1MA);
  315. writel(0, reg + PCIL0_PMM2MA);
  316. writel(0, reg + PCIL0_PTM1MS);
  317. writel(0, reg + PCIL0_PTM2MS);
  318. /* Parse outbound mapping resources */
  319. pci_process_bridge_OF_ranges(hose, np, primary);
  320. /* Parse inbound mapping resources */
  321. if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0)
  322. goto fail;
  323. /* Configure outbound ranges POMs */
  324. ppc4xx_configure_pci_PMMs(hose, reg);
  325. /* Configure inbound ranges PIMs */
  326. ppc4xx_configure_pci_PTMs(hose, reg, &dma_window);
  327. /* We don't need the registers anymore */
  328. iounmap(reg);
  329. return;
  330. fail:
  331. if (hose)
  332. pcibios_free_controller(hose);
  333. if (reg)
  334. iounmap(reg);
  335. }
  336. /*
  337. * 4xx PCI-X part
  338. */
  339. static int __init ppc4xx_setup_one_pcix_POM(struct pci_controller *hose,
  340. void __iomem *reg,
  341. u64 plb_addr,
  342. u64 pci_addr,
  343. u64 size,
  344. unsigned int flags,
  345. int index)
  346. {
  347. u32 lah, lal, pciah, pcial, sa;
  348. if (!is_power_of_2(size) || size < 0x1000 ||
  349. (plb_addr & (size - 1)) != 0) {
  350. printk(KERN_WARNING "%s: Resource out of range\n",
  351. hose->dn->full_name);
  352. return -1;
  353. }
  354. /* Calculate register values */
  355. lah = RES_TO_U32_HIGH(plb_addr);
  356. lal = RES_TO_U32_LOW(plb_addr);
  357. pciah = RES_TO_U32_HIGH(pci_addr);
  358. pcial = RES_TO_U32_LOW(pci_addr);
  359. sa = (0xffffffffu << ilog2(size)) | 0x1;
  360. /* Program register values */
  361. if (index == 0) {
  362. writel(lah, reg + PCIX0_POM0LAH);
  363. writel(lal, reg + PCIX0_POM0LAL);
  364. writel(pciah, reg + PCIX0_POM0PCIAH);
  365. writel(pcial, reg + PCIX0_POM0PCIAL);
  366. writel(sa, reg + PCIX0_POM0SA);
  367. } else {
  368. writel(lah, reg + PCIX0_POM1LAH);
  369. writel(lal, reg + PCIX0_POM1LAL);
  370. writel(pciah, reg + PCIX0_POM1PCIAH);
  371. writel(pcial, reg + PCIX0_POM1PCIAL);
  372. writel(sa, reg + PCIX0_POM1SA);
  373. }
  374. return 0;
  375. }
  376. static void __init ppc4xx_configure_pcix_POMs(struct pci_controller *hose,
  377. void __iomem *reg)
  378. {
  379. int i, j, found_isa_hole = 0;
  380. /* Setup outbound memory windows */
  381. for (i = j = 0; i < 3; i++) {
  382. struct resource *res = &hose->mem_resources[i];
  383. /* we only care about memory windows */
  384. if (!(res->flags & IORESOURCE_MEM))
  385. continue;
  386. if (j > 1) {
  387. printk(KERN_WARNING "%s: Too many ranges\n",
  388. hose->dn->full_name);
  389. break;
  390. }
  391. /* Configure the resource */
  392. if (ppc4xx_setup_one_pcix_POM(hose, reg,
  393. res->start,
  394. res->start - hose->pci_mem_offset,
  395. res->end + 1 - res->start,
  396. res->flags,
  397. j) == 0) {
  398. j++;
  399. /* If the resource PCI address is 0 then we have our
  400. * ISA memory hole
  401. */
  402. if (res->start == hose->pci_mem_offset)
  403. found_isa_hole = 1;
  404. }
  405. }
  406. /* Handle ISA memory hole if not already covered */
  407. if (j <= 1 && !found_isa_hole && hose->isa_mem_size)
  408. if (ppc4xx_setup_one_pcix_POM(hose, reg, hose->isa_mem_phys, 0,
  409. hose->isa_mem_size, 0, j) == 0)
  410. printk(KERN_INFO "%s: Legacy ISA memory support enabled\n",
  411. hose->dn->full_name);
  412. }
  413. static void __init ppc4xx_configure_pcix_PIMs(struct pci_controller *hose,
  414. void __iomem *reg,
  415. const struct resource *res,
  416. int big_pim,
  417. int enable_msi_hole)
  418. {
  419. resource_size_t size = res->end - res->start + 1;
  420. u32 sa;
  421. /* RAM is always at 0 */
  422. writel(0x00000000, reg + PCIX0_PIM0LAH);
  423. writel(0x00000000, reg + PCIX0_PIM0LAL);
  424. /* Calculate window size */
  425. sa = (0xffffffffu << ilog2(size)) | 1;
  426. sa |= 0x1;
  427. if (res->flags & IORESOURCE_PREFETCH)
  428. sa |= 0x2;
  429. if (enable_msi_hole)
  430. sa |= 0x4;
  431. writel(sa, reg + PCIX0_PIM0SA);
  432. if (big_pim)
  433. writel(0xffffffff, reg + PCIX0_PIM0SAH);
  434. /* Map on PCI side */
  435. writel(0x00000000, reg + PCIX0_BAR0H);
  436. writel(res->start, reg + PCIX0_BAR0L);
  437. writew(0x0006, reg + PCIX0_COMMAND);
  438. }
  439. static void __init ppc4xx_probe_pcix_bridge(struct device_node *np)
  440. {
  441. struct resource rsrc_cfg;
  442. struct resource rsrc_reg;
  443. struct resource dma_window;
  444. struct pci_controller *hose = NULL;
  445. void __iomem *reg = NULL;
  446. const int *bus_range;
  447. int big_pim = 0, msi = 0, primary = 0;
  448. /* Fetch config space registers address */
  449. if (of_address_to_resource(np, 0, &rsrc_cfg)) {
  450. printk(KERN_ERR "%s:Can't get PCI-X config register base !",
  451. np->full_name);
  452. return;
  453. }
  454. /* Fetch host bridge internal registers address */
  455. if (of_address_to_resource(np, 3, &rsrc_reg)) {
  456. printk(KERN_ERR "%s: Can't get PCI-X internal register base !",
  457. np->full_name);
  458. return;
  459. }
  460. /* Check if it supports large PIMs (440GX) */
  461. if (of_get_property(np, "large-inbound-windows", NULL))
  462. big_pim = 1;
  463. /* Check if we should enable MSIs inbound hole */
  464. if (of_get_property(np, "enable-msi-hole", NULL))
  465. msi = 1;
  466. /* Check if primary bridge */
  467. if (of_get_property(np, "primary", NULL))
  468. primary = 1;
  469. /* Get bus range if any */
  470. bus_range = of_get_property(np, "bus-range", NULL);
  471. /* Map registers */
  472. reg = ioremap(rsrc_reg.start, rsrc_reg.end + 1 - rsrc_reg.start);
  473. if (reg == NULL) {
  474. printk(KERN_ERR "%s: Can't map registers !", np->full_name);
  475. goto fail;
  476. }
  477. /* Allocate the host controller data structure */
  478. hose = pcibios_alloc_controller(np);
  479. if (!hose)
  480. goto fail;
  481. hose->first_busno = bus_range ? bus_range[0] : 0x0;
  482. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  483. /* Setup config space */
  484. setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 0x4, 0);
  485. /* Disable all windows */
  486. writel(0, reg + PCIX0_POM0SA);
  487. writel(0, reg + PCIX0_POM1SA);
  488. writel(0, reg + PCIX0_POM2SA);
  489. writel(0, reg + PCIX0_PIM0SA);
  490. writel(0, reg + PCIX0_PIM1SA);
  491. writel(0, reg + PCIX0_PIM2SA);
  492. if (big_pim) {
  493. writel(0, reg + PCIX0_PIM0SAH);
  494. writel(0, reg + PCIX0_PIM2SAH);
  495. }
  496. /* Parse outbound mapping resources */
  497. pci_process_bridge_OF_ranges(hose, np, primary);
  498. /* Parse inbound mapping resources */
  499. if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0)
  500. goto fail;
  501. /* Configure outbound ranges POMs */
  502. ppc4xx_configure_pcix_POMs(hose, reg);
  503. /* Configure inbound ranges PIMs */
  504. ppc4xx_configure_pcix_PIMs(hose, reg, &dma_window, big_pim, msi);
  505. /* We don't need the registers anymore */
  506. iounmap(reg);
  507. return;
  508. fail:
  509. if (hose)
  510. pcibios_free_controller(hose);
  511. if (reg)
  512. iounmap(reg);
  513. }
  514. #ifdef CONFIG_PPC4xx_PCI_EXPRESS
  515. /*
  516. * 4xx PCI-Express part
  517. *
  518. * We support 3 parts currently based on the compatible property:
  519. *
  520. * ibm,plb-pciex-440spe
  521. * ibm,plb-pciex-405ex
  522. * ibm,plb-pciex-460ex
  523. *
  524. * Anything else will be rejected for now as they are all subtly
  525. * different unfortunately.
  526. *
  527. */
  528. #define MAX_PCIE_BUS_MAPPED 0x40
  529. struct ppc4xx_pciex_port
  530. {
  531. struct pci_controller *hose;
  532. struct device_node *node;
  533. unsigned int index;
  534. int endpoint;
  535. int link;
  536. int has_ibpre;
  537. unsigned int sdr_base;
  538. dcr_host_t dcrs;
  539. struct resource cfg_space;
  540. struct resource utl_regs;
  541. void __iomem *utl_base;
  542. };
  543. static struct ppc4xx_pciex_port *ppc4xx_pciex_ports;
  544. static unsigned int ppc4xx_pciex_port_count;
  545. struct ppc4xx_pciex_hwops
  546. {
  547. int (*core_init)(struct device_node *np);
  548. int (*port_init_hw)(struct ppc4xx_pciex_port *port);
  549. int (*setup_utl)(struct ppc4xx_pciex_port *port);
  550. };
  551. static struct ppc4xx_pciex_hwops *ppc4xx_pciex_hwops;
  552. #ifdef CONFIG_44x
  553. /* Check various reset bits of the 440SPe PCIe core */
  554. static int __init ppc440spe_pciex_check_reset(struct device_node *np)
  555. {
  556. u32 valPE0, valPE1, valPE2;
  557. int err = 0;
  558. /* SDR0_PEGPLLLCT1 reset */
  559. if (!(mfdcri(SDR0, PESDR0_PLLLCT1) & 0x01000000)) {
  560. /*
  561. * the PCIe core was probably already initialised
  562. * by firmware - let's re-reset RCSSET regs
  563. *
  564. * -- Shouldn't we also re-reset the whole thing ? -- BenH
  565. */
  566. pr_debug("PCIE: SDR0_PLLLCT1 already reset.\n");
  567. mtdcri(SDR0, PESDR0_440SPE_RCSSET, 0x01010000);
  568. mtdcri(SDR0, PESDR1_440SPE_RCSSET, 0x01010000);
  569. mtdcri(SDR0, PESDR2_440SPE_RCSSET, 0x01010000);
  570. }
  571. valPE0 = mfdcri(SDR0, PESDR0_440SPE_RCSSET);
  572. valPE1 = mfdcri(SDR0, PESDR1_440SPE_RCSSET);
  573. valPE2 = mfdcri(SDR0, PESDR2_440SPE_RCSSET);
  574. /* SDR0_PExRCSSET rstgu */
  575. if (!(valPE0 & 0x01000000) ||
  576. !(valPE1 & 0x01000000) ||
  577. !(valPE2 & 0x01000000)) {
  578. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstgu error\n");
  579. err = -1;
  580. }
  581. /* SDR0_PExRCSSET rstdl */
  582. if (!(valPE0 & 0x00010000) ||
  583. !(valPE1 & 0x00010000) ||
  584. !(valPE2 & 0x00010000)) {
  585. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstdl error\n");
  586. err = -1;
  587. }
  588. /* SDR0_PExRCSSET rstpyn */
  589. if ((valPE0 & 0x00001000) ||
  590. (valPE1 & 0x00001000) ||
  591. (valPE2 & 0x00001000)) {
  592. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstpyn error\n");
  593. err = -1;
  594. }
  595. /* SDR0_PExRCSSET hldplb */
  596. if ((valPE0 & 0x10000000) ||
  597. (valPE1 & 0x10000000) ||
  598. (valPE2 & 0x10000000)) {
  599. printk(KERN_INFO "PCIE: SDR0_PExRCSSET hldplb error\n");
  600. err = -1;
  601. }
  602. /* SDR0_PExRCSSET rdy */
  603. if ((valPE0 & 0x00100000) ||
  604. (valPE1 & 0x00100000) ||
  605. (valPE2 & 0x00100000)) {
  606. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rdy error\n");
  607. err = -1;
  608. }
  609. /* SDR0_PExRCSSET shutdown */
  610. if ((valPE0 & 0x00000100) ||
  611. (valPE1 & 0x00000100) ||
  612. (valPE2 & 0x00000100)) {
  613. printk(KERN_INFO "PCIE: SDR0_PExRCSSET shutdown error\n");
  614. err = -1;
  615. }
  616. return err;
  617. }
  618. /* Global PCIe core initializations for 440SPe core */
  619. static int __init ppc440spe_pciex_core_init(struct device_node *np)
  620. {
  621. int time_out = 20;
  622. /* Set PLL clock receiver to LVPECL */
  623. dcri_clrset(SDR0, PESDR0_PLLLCT1, 0, 1 << 28);
  624. /* Shouldn't we do all the calibration stuff etc... here ? */
  625. if (ppc440spe_pciex_check_reset(np))
  626. return -ENXIO;
  627. if (!(mfdcri(SDR0, PESDR0_PLLLCT2) & 0x10000)) {
  628. printk(KERN_INFO "PCIE: PESDR_PLLCT2 resistance calibration "
  629. "failed (0x%08x)\n",
  630. mfdcri(SDR0, PESDR0_PLLLCT2));
  631. return -1;
  632. }
  633. /* De-assert reset of PCIe PLL, wait for lock */
  634. dcri_clrset(SDR0, PESDR0_PLLLCT1, 1 << 24, 0);
  635. udelay(3);
  636. while (time_out) {
  637. if (!(mfdcri(SDR0, PESDR0_PLLLCT3) & 0x10000000)) {
  638. time_out--;
  639. udelay(1);
  640. } else
  641. break;
  642. }
  643. if (!time_out) {
  644. printk(KERN_INFO "PCIE: VCO output not locked\n");
  645. return -1;
  646. }
  647. pr_debug("PCIE initialization OK\n");
  648. return 3;
  649. }
  650. static int ppc440spe_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  651. {
  652. u32 val = 1 << 24;
  653. if (port->endpoint)
  654. val = PTYPE_LEGACY_ENDPOINT << 20;
  655. else
  656. val = PTYPE_ROOT_PORT << 20;
  657. if (port->index == 0)
  658. val |= LNKW_X8 << 12;
  659. else
  660. val |= LNKW_X4 << 12;
  661. mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET, val);
  662. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x20222222);
  663. if (ppc440spe_revA())
  664. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x11000000);
  665. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL0SET1, 0x35000000);
  666. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL1SET1, 0x35000000);
  667. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL2SET1, 0x35000000);
  668. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL3SET1, 0x35000000);
  669. if (port->index == 0) {
  670. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL4SET1,
  671. 0x35000000);
  672. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL5SET1,
  673. 0x35000000);
  674. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL6SET1,
  675. 0x35000000);
  676. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL7SET1,
  677. 0x35000000);
  678. }
  679. dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET,
  680. (1 << 24) | (1 << 16), 1 << 12);
  681. return 0;
  682. }
  683. static int ppc440speA_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  684. {
  685. return ppc440spe_pciex_init_port_hw(port);
  686. }
  687. static int ppc440speB_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  688. {
  689. int rc = ppc440spe_pciex_init_port_hw(port);
  690. port->has_ibpre = 1;
  691. return rc;
  692. }
  693. static int ppc440speA_pciex_init_utl(struct ppc4xx_pciex_port *port)
  694. {
  695. /* XXX Check what that value means... I hate magic */
  696. dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x68782800);
  697. /*
  698. * Set buffer allocations and then assert VRB and TXE.
  699. */
  700. out_be32(port->utl_base + PEUTL_OUTTR, 0x08000000);
  701. out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
  702. out_be32(port->utl_base + PEUTL_OPDBSZ, 0x10000000);
  703. out_be32(port->utl_base + PEUTL_PBBSZ, 0x53000000);
  704. out_be32(port->utl_base + PEUTL_IPHBSZ, 0x08000000);
  705. out_be32(port->utl_base + PEUTL_IPDBSZ, 0x10000000);
  706. out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
  707. out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
  708. return 0;
  709. }
  710. static int ppc440speB_pciex_init_utl(struct ppc4xx_pciex_port *port)
  711. {
  712. /* Report CRS to the operating system */
  713. out_be32(port->utl_base + PEUTL_PBCTL, 0x08000000);
  714. return 0;
  715. }
  716. static struct ppc4xx_pciex_hwops ppc440speA_pcie_hwops __initdata =
  717. {
  718. .core_init = ppc440spe_pciex_core_init,
  719. .port_init_hw = ppc440speA_pciex_init_port_hw,
  720. .setup_utl = ppc440speA_pciex_init_utl,
  721. };
  722. static struct ppc4xx_pciex_hwops ppc440speB_pcie_hwops __initdata =
  723. {
  724. .core_init = ppc440spe_pciex_core_init,
  725. .port_init_hw = ppc440speB_pciex_init_port_hw,
  726. .setup_utl = ppc440speB_pciex_init_utl,
  727. };
  728. static int __init ppc460ex_pciex_core_init(struct device_node *np)
  729. {
  730. /* Nothing to do, return 2 ports */
  731. return 2;
  732. }
  733. static int ppc460ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  734. {
  735. u32 val;
  736. u32 utlset1;
  737. if (port->endpoint)
  738. val = PTYPE_LEGACY_ENDPOINT << 20;
  739. else
  740. val = PTYPE_ROOT_PORT << 20;
  741. if (port->index == 0) {
  742. val |= LNKW_X1 << 12;
  743. utlset1 = 0x20000000;
  744. } else {
  745. val |= LNKW_X4 << 12;
  746. utlset1 = 0x20101101;
  747. }
  748. mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET, val);
  749. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, utlset1);
  750. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01210000);
  751. switch (port->index) {
  752. case 0:
  753. mtdcri(SDR0, PESDR0_460EX_L0CDRCTL, 0x00003230);
  754. mtdcri(SDR0, PESDR0_460EX_L0DRV, 0x00000130);
  755. mtdcri(SDR0, PESDR0_460EX_L0CLK, 0x00000006);
  756. mtdcri(SDR0, PESDR0_460EX_PHY_CTL_RST,0x10000000);
  757. break;
  758. case 1:
  759. mtdcri(SDR0, PESDR1_460EX_L0CDRCTL, 0x00003230);
  760. mtdcri(SDR0, PESDR1_460EX_L1CDRCTL, 0x00003230);
  761. mtdcri(SDR0, PESDR1_460EX_L2CDRCTL, 0x00003230);
  762. mtdcri(SDR0, PESDR1_460EX_L3CDRCTL, 0x00003230);
  763. mtdcri(SDR0, PESDR1_460EX_L0DRV, 0x00000130);
  764. mtdcri(SDR0, PESDR1_460EX_L1DRV, 0x00000130);
  765. mtdcri(SDR0, PESDR1_460EX_L2DRV, 0x00000130);
  766. mtdcri(SDR0, PESDR1_460EX_L3DRV, 0x00000130);
  767. mtdcri(SDR0, PESDR1_460EX_L0CLK, 0x00000006);
  768. mtdcri(SDR0, PESDR1_460EX_L1CLK, 0x00000006);
  769. mtdcri(SDR0, PESDR1_460EX_L2CLK, 0x00000006);
  770. mtdcri(SDR0, PESDR1_460EX_L3CLK, 0x00000006);
  771. mtdcri(SDR0, PESDR1_460EX_PHY_CTL_RST,0x10000000);
  772. break;
  773. }
  774. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
  775. mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) |
  776. (PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTPYN));
  777. /* Poll for PHY reset */
  778. /* XXX FIXME add timeout */
  779. switch (port->index) {
  780. case 0:
  781. while (!(mfdcri(SDR0, PESDR0_460EX_RSTSTA) & 0x1))
  782. udelay(10);
  783. break;
  784. case 1:
  785. while (!(mfdcri(SDR0, PESDR1_460EX_RSTSTA) & 0x1))
  786. udelay(10);
  787. break;
  788. }
  789. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
  790. (mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) &
  791. ~(PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTDL)) |
  792. PESDRx_RCSSET_RSTPYN);
  793. port->has_ibpre = 1;
  794. return 0;
  795. }
  796. static int ppc460ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
  797. {
  798. dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
  799. /*
  800. * Set buffer allocations and then assert VRB and TXE.
  801. */
  802. out_be32(port->utl_base + PEUTL_PBCTL, 0x0800000c);
  803. out_be32(port->utl_base + PEUTL_OUTTR, 0x08000000);
  804. out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
  805. out_be32(port->utl_base + PEUTL_OPDBSZ, 0x04000000);
  806. out_be32(port->utl_base + PEUTL_PBBSZ, 0x00000000);
  807. out_be32(port->utl_base + PEUTL_IPHBSZ, 0x02000000);
  808. out_be32(port->utl_base + PEUTL_IPDBSZ, 0x04000000);
  809. out_be32(port->utl_base + PEUTL_RCIRQEN,0x00f00000);
  810. out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
  811. return 0;
  812. }
  813. static struct ppc4xx_pciex_hwops ppc460ex_pcie_hwops __initdata =
  814. {
  815. .core_init = ppc460ex_pciex_core_init,
  816. .port_init_hw = ppc460ex_pciex_init_port_hw,
  817. .setup_utl = ppc460ex_pciex_init_utl,
  818. };
  819. #endif /* CONFIG_44x */
  820. #ifdef CONFIG_40x
  821. static int __init ppc405ex_pciex_core_init(struct device_node *np)
  822. {
  823. /* Nothing to do, return 2 ports */
  824. return 2;
  825. }
  826. static void ppc405ex_pcie_phy_reset(struct ppc4xx_pciex_port *port)
  827. {
  828. /* Assert the PE0_PHY reset */
  829. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01010000);
  830. msleep(1);
  831. /* deassert the PE0_hotreset */
  832. if (port->endpoint)
  833. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01111000);
  834. else
  835. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01101000);
  836. /* poll for phy !reset */
  837. /* XXX FIXME add timeout */
  838. while (!(mfdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSTA) & 0x00001000))
  839. ;
  840. /* deassert the PE0_gpl_utl_reset */
  841. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x00101000);
  842. }
  843. static int ppc405ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  844. {
  845. u32 val;
  846. if (port->endpoint)
  847. val = PTYPE_LEGACY_ENDPOINT;
  848. else
  849. val = PTYPE_ROOT_PORT;
  850. mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET,
  851. 1 << 24 | val << 20 | LNKW_X1 << 12);
  852. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x00000000);
  853. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01010000);
  854. mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET1, 0x720F0000);
  855. mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET2, 0x70600003);
  856. /*
  857. * Only reset the PHY when no link is currently established.
  858. * This is for the Atheros PCIe board which has problems to establish
  859. * the link (again) after this PHY reset. All other currently tested
  860. * PCIe boards don't show this problem.
  861. * This has to be re-tested and fixed in a later release!
  862. */
  863. val = mfdcri(SDR0, port->sdr_base + PESDRn_LOOP);
  864. if (!(val & 0x00001000))
  865. ppc405ex_pcie_phy_reset(port);
  866. dcr_write(port->dcrs, DCRO_PEGPL_CFG, 0x10000000); /* guarded on */
  867. port->has_ibpre = 1;
  868. return 0;
  869. }
  870. static int ppc405ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
  871. {
  872. dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
  873. /*
  874. * Set buffer allocations and then assert VRB and TXE.
  875. */
  876. out_be32(port->utl_base + PEUTL_OUTTR, 0x02000000);
  877. out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
  878. out_be32(port->utl_base + PEUTL_OPDBSZ, 0x04000000);
  879. out_be32(port->utl_base + PEUTL_PBBSZ, 0x21000000);
  880. out_be32(port->utl_base + PEUTL_IPHBSZ, 0x02000000);
  881. out_be32(port->utl_base + PEUTL_IPDBSZ, 0x04000000);
  882. out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
  883. out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
  884. out_be32(port->utl_base + PEUTL_PBCTL, 0x08000000);
  885. return 0;
  886. }
  887. static struct ppc4xx_pciex_hwops ppc405ex_pcie_hwops __initdata =
  888. {
  889. .core_init = ppc405ex_pciex_core_init,
  890. .port_init_hw = ppc405ex_pciex_init_port_hw,
  891. .setup_utl = ppc405ex_pciex_init_utl,
  892. };
  893. #endif /* CONFIG_40x */
  894. /* Check that the core has been initied and if not, do it */
  895. static int __init ppc4xx_pciex_check_core_init(struct device_node *np)
  896. {
  897. static int core_init;
  898. int count = -ENODEV;
  899. if (core_init++)
  900. return 0;
  901. #ifdef CONFIG_44x
  902. if (of_device_is_compatible(np, "ibm,plb-pciex-440spe")) {
  903. if (ppc440spe_revA())
  904. ppc4xx_pciex_hwops = &ppc440speA_pcie_hwops;
  905. else
  906. ppc4xx_pciex_hwops = &ppc440speB_pcie_hwops;
  907. }
  908. if (of_device_is_compatible(np, "ibm,plb-pciex-460ex"))
  909. ppc4xx_pciex_hwops = &ppc460ex_pcie_hwops;
  910. #endif /* CONFIG_44x */
  911. #ifdef CONFIG_40x
  912. if (of_device_is_compatible(np, "ibm,plb-pciex-405ex"))
  913. ppc4xx_pciex_hwops = &ppc405ex_pcie_hwops;
  914. #endif
  915. if (ppc4xx_pciex_hwops == NULL) {
  916. printk(KERN_WARNING "PCIE: unknown host type %s\n",
  917. np->full_name);
  918. return -ENODEV;
  919. }
  920. count = ppc4xx_pciex_hwops->core_init(np);
  921. if (count > 0) {
  922. ppc4xx_pciex_ports =
  923. kzalloc(count * sizeof(struct ppc4xx_pciex_port),
  924. GFP_KERNEL);
  925. if (ppc4xx_pciex_ports) {
  926. ppc4xx_pciex_port_count = count;
  927. return 0;
  928. }
  929. printk(KERN_WARNING "PCIE: failed to allocate ports array\n");
  930. return -ENOMEM;
  931. }
  932. return -ENODEV;
  933. }
  934. static void __init ppc4xx_pciex_port_init_mapping(struct ppc4xx_pciex_port *port)
  935. {
  936. /* We map PCI Express configuration based on the reg property */
  937. dcr_write(port->dcrs, DCRO_PEGPL_CFGBAH,
  938. RES_TO_U32_HIGH(port->cfg_space.start));
  939. dcr_write(port->dcrs, DCRO_PEGPL_CFGBAL,
  940. RES_TO_U32_LOW(port->cfg_space.start));
  941. /* XXX FIXME: Use size from reg property. For now, map 512M */
  942. dcr_write(port->dcrs, DCRO_PEGPL_CFGMSK, 0xe0000001);
  943. /* We map UTL registers based on the reg property */
  944. dcr_write(port->dcrs, DCRO_PEGPL_REGBAH,
  945. RES_TO_U32_HIGH(port->utl_regs.start));
  946. dcr_write(port->dcrs, DCRO_PEGPL_REGBAL,
  947. RES_TO_U32_LOW(port->utl_regs.start));
  948. /* XXX FIXME: Use size from reg property */
  949. dcr_write(port->dcrs, DCRO_PEGPL_REGMSK, 0x00007001);
  950. /* Disable all other outbound windows */
  951. dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, 0);
  952. dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, 0);
  953. dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, 0);
  954. dcr_write(port->dcrs, DCRO_PEGPL_MSGMSK, 0);
  955. }
  956. static int __init ppc4xx_pciex_wait_on_sdr(struct ppc4xx_pciex_port *port,
  957. unsigned int sdr_offset,
  958. unsigned int mask,
  959. unsigned int value,
  960. int timeout_ms)
  961. {
  962. u32 val;
  963. while(timeout_ms--) {
  964. val = mfdcri(SDR0, port->sdr_base + sdr_offset);
  965. if ((val & mask) == value) {
  966. pr_debug("PCIE%d: Wait on SDR %x success with tm %d (%08x)\n",
  967. port->index, sdr_offset, timeout_ms, val);
  968. return 0;
  969. }
  970. msleep(1);
  971. }
  972. return -1;
  973. }
  974. static int __init ppc4xx_pciex_port_init(struct ppc4xx_pciex_port *port)
  975. {
  976. int rc = 0;
  977. /* Init HW */
  978. if (ppc4xx_pciex_hwops->port_init_hw)
  979. rc = ppc4xx_pciex_hwops->port_init_hw(port);
  980. if (rc != 0)
  981. return rc;
  982. printk(KERN_INFO "PCIE%d: Checking link...\n",
  983. port->index);
  984. /* Wait for reset to complete */
  985. if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS, 1 << 20, 0, 10)) {
  986. printk(KERN_WARNING "PCIE%d: PGRST failed\n",
  987. port->index);
  988. return -1;
  989. }
  990. /* Check for card presence detect if supported, if not, just wait for
  991. * link unconditionally.
  992. *
  993. * note that we don't fail if there is no link, we just filter out
  994. * config space accesses. That way, it will be easier to implement
  995. * hotplug later on.
  996. */
  997. if (!port->has_ibpre ||
  998. !ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
  999. 1 << 28, 1 << 28, 100)) {
  1000. printk(KERN_INFO
  1001. "PCIE%d: Device detected, waiting for link...\n",
  1002. port->index);
  1003. if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
  1004. 0x1000, 0x1000, 2000))
  1005. printk(KERN_WARNING
  1006. "PCIE%d: Link up failed\n", port->index);
  1007. else {
  1008. printk(KERN_INFO
  1009. "PCIE%d: link is up !\n", port->index);
  1010. port->link = 1;
  1011. }
  1012. } else
  1013. printk(KERN_INFO "PCIE%d: No device detected.\n", port->index);
  1014. /*
  1015. * Initialize mapping: disable all regions and configure
  1016. * CFG and REG regions based on resources in the device tree
  1017. */
  1018. ppc4xx_pciex_port_init_mapping(port);
  1019. /*
  1020. * Map UTL
  1021. */
  1022. port->utl_base = ioremap(port->utl_regs.start, 0x100);
  1023. BUG_ON(port->utl_base == NULL);
  1024. /*
  1025. * Setup UTL registers --BenH.
  1026. */
  1027. if (ppc4xx_pciex_hwops->setup_utl)
  1028. ppc4xx_pciex_hwops->setup_utl(port);
  1029. /*
  1030. * Check for VC0 active and assert RDY.
  1031. */
  1032. if (port->link &&
  1033. ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS,
  1034. 1 << 16, 1 << 16, 5000)) {
  1035. printk(KERN_INFO "PCIE%d: VC0 not active\n", port->index);
  1036. port->link = 0;
  1037. }
  1038. dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET, 0, 1 << 20);
  1039. msleep(100);
  1040. return 0;
  1041. }
  1042. static int ppc4xx_pciex_validate_bdf(struct ppc4xx_pciex_port *port,
  1043. struct pci_bus *bus,
  1044. unsigned int devfn)
  1045. {
  1046. static int message;
  1047. /* Endpoint can not generate upstream(remote) config cycles */
  1048. if (port->endpoint && bus->number != port->hose->first_busno)
  1049. return PCIBIOS_DEVICE_NOT_FOUND;
  1050. /* Check we are within the mapped range */
  1051. if (bus->number > port->hose->last_busno) {
  1052. if (!message) {
  1053. printk(KERN_WARNING "Warning! Probing bus %u"
  1054. " out of range !\n", bus->number);
  1055. message++;
  1056. }
  1057. return PCIBIOS_DEVICE_NOT_FOUND;
  1058. }
  1059. /* The root complex has only one device / function */
  1060. if (bus->number == port->hose->first_busno && devfn != 0)
  1061. return PCIBIOS_DEVICE_NOT_FOUND;
  1062. /* The other side of the RC has only one device as well */
  1063. if (bus->number == (port->hose->first_busno + 1) &&
  1064. PCI_SLOT(devfn) != 0)
  1065. return PCIBIOS_DEVICE_NOT_FOUND;
  1066. /* Check if we have a link */
  1067. if ((bus->number != port->hose->first_busno) && !port->link)
  1068. return PCIBIOS_DEVICE_NOT_FOUND;
  1069. return 0;
  1070. }
  1071. static void __iomem *ppc4xx_pciex_get_config_base(struct ppc4xx_pciex_port *port,
  1072. struct pci_bus *bus,
  1073. unsigned int devfn)
  1074. {
  1075. int relbus;
  1076. /* Remove the casts when we finally remove the stupid volatile
  1077. * in struct pci_controller
  1078. */
  1079. if (bus->number == port->hose->first_busno)
  1080. return (void __iomem *)port->hose->cfg_addr;
  1081. relbus = bus->number - (port->hose->first_busno + 1);
  1082. return (void __iomem *)port->hose->cfg_data +
  1083. ((relbus << 20) | (devfn << 12));
  1084. }
  1085. static int ppc4xx_pciex_read_config(struct pci_bus *bus, unsigned int devfn,
  1086. int offset, int len, u32 *val)
  1087. {
  1088. struct pci_controller *hose = pci_bus_to_host(bus);
  1089. struct ppc4xx_pciex_port *port =
  1090. &ppc4xx_pciex_ports[hose->indirect_type];
  1091. void __iomem *addr;
  1092. u32 gpl_cfg;
  1093. BUG_ON(hose != port->hose);
  1094. if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
  1095. return PCIBIOS_DEVICE_NOT_FOUND;
  1096. addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
  1097. /*
  1098. * Reading from configuration space of non-existing device can
  1099. * generate transaction errors. For the read duration we suppress
  1100. * assertion of machine check exceptions to avoid those.
  1101. */
  1102. gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
  1103. dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
  1104. /* Make sure no CRS is recorded */
  1105. out_be32(port->utl_base + PEUTL_RCSTA, 0x00040000);
  1106. switch (len) {
  1107. case 1:
  1108. *val = in_8((u8 *)(addr + offset));
  1109. break;
  1110. case 2:
  1111. *val = in_le16((u16 *)(addr + offset));
  1112. break;
  1113. default:
  1114. *val = in_le32((u32 *)(addr + offset));
  1115. break;
  1116. }
  1117. pr_debug("pcie-config-read: bus=%3d [%3d..%3d] devfn=0x%04x"
  1118. " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
  1119. bus->number, hose->first_busno, hose->last_busno,
  1120. devfn, offset, len, addr + offset, *val);
  1121. /* Check for CRS (440SPe rev B does that for us but heh ..) */
  1122. if (in_be32(port->utl_base + PEUTL_RCSTA) & 0x00040000) {
  1123. pr_debug("Got CRS !\n");
  1124. if (len != 4 || offset != 0)
  1125. return PCIBIOS_DEVICE_NOT_FOUND;
  1126. *val = 0xffff0001;
  1127. }
  1128. dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
  1129. return PCIBIOS_SUCCESSFUL;
  1130. }
  1131. static int ppc4xx_pciex_write_config(struct pci_bus *bus, unsigned int devfn,
  1132. int offset, int len, u32 val)
  1133. {
  1134. struct pci_controller *hose = pci_bus_to_host(bus);
  1135. struct ppc4xx_pciex_port *port =
  1136. &ppc4xx_pciex_ports[hose->indirect_type];
  1137. void __iomem *addr;
  1138. u32 gpl_cfg;
  1139. if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
  1140. return PCIBIOS_DEVICE_NOT_FOUND;
  1141. addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
  1142. /*
  1143. * Reading from configuration space of non-existing device can
  1144. * generate transaction errors. For the read duration we suppress
  1145. * assertion of machine check exceptions to avoid those.
  1146. */
  1147. gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
  1148. dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
  1149. pr_debug("pcie-config-write: bus=%3d [%3d..%3d] devfn=0x%04x"
  1150. " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
  1151. bus->number, hose->first_busno, hose->last_busno,
  1152. devfn, offset, len, addr + offset, val);
  1153. switch (len) {
  1154. case 1:
  1155. out_8((u8 *)(addr + offset), val);
  1156. break;
  1157. case 2:
  1158. out_le16((u16 *)(addr + offset), val);
  1159. break;
  1160. default:
  1161. out_le32((u32 *)(addr + offset), val);
  1162. break;
  1163. }
  1164. dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
  1165. return PCIBIOS_SUCCESSFUL;
  1166. }
  1167. static struct pci_ops ppc4xx_pciex_pci_ops =
  1168. {
  1169. .read = ppc4xx_pciex_read_config,
  1170. .write = ppc4xx_pciex_write_config,
  1171. };
  1172. static int __init ppc4xx_setup_one_pciex_POM(struct ppc4xx_pciex_port *port,
  1173. struct pci_controller *hose,
  1174. void __iomem *mbase,
  1175. u64 plb_addr,
  1176. u64 pci_addr,
  1177. u64 size,
  1178. unsigned int flags,
  1179. int index)
  1180. {
  1181. u32 lah, lal, pciah, pcial, sa;
  1182. if (!is_power_of_2(size) ||
  1183. (index < 2 && size < 0x100000) ||
  1184. (index == 2 && size < 0x100) ||
  1185. (plb_addr & (size - 1)) != 0) {
  1186. printk(KERN_WARNING "%s: Resource out of range\n",
  1187. hose->dn->full_name);
  1188. return -1;
  1189. }
  1190. /* Calculate register values */
  1191. lah = RES_TO_U32_HIGH(plb_addr);
  1192. lal = RES_TO_U32_LOW(plb_addr);
  1193. pciah = RES_TO_U32_HIGH(pci_addr);
  1194. pcial = RES_TO_U32_LOW(pci_addr);
  1195. sa = (0xffffffffu << ilog2(size)) | 0x1;
  1196. /* Program register values */
  1197. switch (index) {
  1198. case 0:
  1199. out_le32(mbase + PECFG_POM0LAH, pciah);
  1200. out_le32(mbase + PECFG_POM0LAL, pcial);
  1201. dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAH, lah);
  1202. dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAL, lal);
  1203. dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKH, 0x7fffffff);
  1204. /* Note that 3 here means enabled | single region */
  1205. dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, sa | 3);
  1206. break;
  1207. case 1:
  1208. out_le32(mbase + PECFG_POM1LAH, pciah);
  1209. out_le32(mbase + PECFG_POM1LAL, pcial);
  1210. dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAH, lah);
  1211. dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAL, lal);
  1212. dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKH, 0x7fffffff);
  1213. /* Note that 3 here means enabled | single region */
  1214. dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, sa | 3);
  1215. break;
  1216. case 2:
  1217. out_le32(mbase + PECFG_POM2LAH, pciah);
  1218. out_le32(mbase + PECFG_POM2LAL, pcial);
  1219. dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAH, lah);
  1220. dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAL, lal);
  1221. dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKH, 0x7fffffff);
  1222. /* Note that 3 here means enabled | IO space !!! */
  1223. dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, sa | 3);
  1224. break;
  1225. }
  1226. return 0;
  1227. }
  1228. static void __init ppc4xx_configure_pciex_POMs(struct ppc4xx_pciex_port *port,
  1229. struct pci_controller *hose,
  1230. void __iomem *mbase)
  1231. {
  1232. int i, j, found_isa_hole = 0;
  1233. /* Setup outbound memory windows */
  1234. for (i = j = 0; i < 3; i++) {
  1235. struct resource *res = &hose->mem_resources[i];
  1236. /* we only care about memory windows */
  1237. if (!(res->flags & IORESOURCE_MEM))
  1238. continue;
  1239. if (j > 1) {
  1240. printk(KERN_WARNING "%s: Too many ranges\n",
  1241. port->node->full_name);
  1242. break;
  1243. }
  1244. /* Configure the resource */
  1245. if (ppc4xx_setup_one_pciex_POM(port, hose, mbase,
  1246. res->start,
  1247. res->start - hose->pci_mem_offset,
  1248. res->end + 1 - res->start,
  1249. res->flags,
  1250. j) == 0) {
  1251. j++;
  1252. /* If the resource PCI address is 0 then we have our
  1253. * ISA memory hole
  1254. */
  1255. if (res->start == hose->pci_mem_offset)
  1256. found_isa_hole = 1;
  1257. }
  1258. }
  1259. /* Handle ISA memory hole if not already covered */
  1260. if (j <= 1 && !found_isa_hole && hose->isa_mem_size)
  1261. if (ppc4xx_setup_one_pciex_POM(port, hose, mbase,
  1262. hose->isa_mem_phys, 0,
  1263. hose->isa_mem_size, 0, j) == 0)
  1264. printk(KERN_INFO "%s: Legacy ISA memory support enabled\n",
  1265. hose->dn->full_name);
  1266. /* Configure IO, always 64K starting at 0. We hard wire it to 64K !
  1267. * Note also that it -has- to be region index 2 on this HW
  1268. */
  1269. if (hose->io_resource.flags & IORESOURCE_IO)
  1270. ppc4xx_setup_one_pciex_POM(port, hose, mbase,
  1271. hose->io_base_phys, 0,
  1272. 0x10000, IORESOURCE_IO, 2);
  1273. }
  1274. static void __init ppc4xx_configure_pciex_PIMs(struct ppc4xx_pciex_port *port,
  1275. struct pci_controller *hose,
  1276. void __iomem *mbase,
  1277. struct resource *res)
  1278. {
  1279. resource_size_t size = res->end - res->start + 1;
  1280. u64 sa;
  1281. if (port->endpoint) {
  1282. resource_size_t ep_addr = 0;
  1283. resource_size_t ep_size = 32 << 20;
  1284. /* Currently we map a fixed 64MByte window to PLB address
  1285. * 0 (SDRAM). This should probably be configurable via a dts
  1286. * property.
  1287. */
  1288. /* Calculate window size */
  1289. sa = (0xffffffffffffffffull << ilog2(ep_size));
  1290. /* Setup BAR0 */
  1291. out_le32(mbase + PECFG_BAR0HMPA, RES_TO_U32_HIGH(sa));
  1292. out_le32(mbase + PECFG_BAR0LMPA, RES_TO_U32_LOW(sa) |
  1293. PCI_BASE_ADDRESS_MEM_TYPE_64);
  1294. /* Disable BAR1 & BAR2 */
  1295. out_le32(mbase + PECFG_BAR1MPA, 0);
  1296. out_le32(mbase + PECFG_BAR2HMPA, 0);
  1297. out_le32(mbase + PECFG_BAR2LMPA, 0);
  1298. out_le32(mbase + PECFG_PIM01SAH, RES_TO_U32_HIGH(sa));
  1299. out_le32(mbase + PECFG_PIM01SAL, RES_TO_U32_LOW(sa));
  1300. out_le32(mbase + PCI_BASE_ADDRESS_0, RES_TO_U32_LOW(ep_addr));
  1301. out_le32(mbase + PCI_BASE_ADDRESS_1, RES_TO_U32_HIGH(ep_addr));
  1302. } else {
  1303. /* Calculate window size */
  1304. sa = (0xffffffffffffffffull << ilog2(size));
  1305. if (res->flags & IORESOURCE_PREFETCH)
  1306. sa |= 0x8;
  1307. out_le32(mbase + PECFG_BAR0HMPA, RES_TO_U32_HIGH(sa));
  1308. out_le32(mbase + PECFG_BAR0LMPA, RES_TO_U32_LOW(sa));
  1309. /* The setup of the split looks weird to me ... let's see
  1310. * if it works
  1311. */
  1312. out_le32(mbase + PECFG_PIM0LAL, 0x00000000);
  1313. out_le32(mbase + PECFG_PIM0LAH, 0x00000000);
  1314. out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
  1315. out_le32(mbase + PECFG_PIM1LAH, 0x00000000);
  1316. out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
  1317. out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
  1318. out_le32(mbase + PCI_BASE_ADDRESS_0, RES_TO_U32_LOW(res->start));
  1319. out_le32(mbase + PCI_BASE_ADDRESS_1, RES_TO_U32_HIGH(res->start));
  1320. }
  1321. /* Enable inbound mapping */
  1322. out_le32(mbase + PECFG_PIMEN, 0x1);
  1323. /* Enable I/O, Mem, and Busmaster cycles */
  1324. out_le16(mbase + PCI_COMMAND,
  1325. in_le16(mbase + PCI_COMMAND) |
  1326. PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  1327. }
  1328. static void __init ppc4xx_pciex_port_setup_hose(struct ppc4xx_pciex_port *port)
  1329. {
  1330. struct resource dma_window;
  1331. struct pci_controller *hose = NULL;
  1332. const int *bus_range;
  1333. int primary = 0, busses;
  1334. void __iomem *mbase = NULL, *cfg_data = NULL;
  1335. const u32 *pval;
  1336. u32 val;
  1337. /* Check if primary bridge */
  1338. if (of_get_property(port->node, "primary", NULL))
  1339. primary = 1;
  1340. /* Get bus range if any */
  1341. bus_range = of_get_property(port->node, "bus-range", NULL);
  1342. /* Allocate the host controller data structure */
  1343. hose = pcibios_alloc_controller(port->node);
  1344. if (!hose)
  1345. goto fail;
  1346. /* We stick the port number in "indirect_type" so the config space
  1347. * ops can retrieve the port data structure easily
  1348. */
  1349. hose->indirect_type = port->index;
  1350. /* Get bus range */
  1351. hose->first_busno = bus_range ? bus_range[0] : 0x0;
  1352. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  1353. /* Because of how big mapping the config space is (1M per bus), we
  1354. * limit how many busses we support. In the long run, we could replace
  1355. * that with something akin to kmap_atomic instead. We set aside 1 bus
  1356. * for the host itself too.
  1357. */
  1358. busses = hose->last_busno - hose->first_busno; /* This is off by 1 */
  1359. if (busses > MAX_PCIE_BUS_MAPPED) {
  1360. busses = MAX_PCIE_BUS_MAPPED;
  1361. hose->last_busno = hose->first_busno + busses;
  1362. }
  1363. if (!port->endpoint) {
  1364. /* Only map the external config space in cfg_data for
  1365. * PCIe root-complexes. External space is 1M per bus
  1366. */
  1367. cfg_data = ioremap(port->cfg_space.start +
  1368. (hose->first_busno + 1) * 0x100000,
  1369. busses * 0x100000);
  1370. if (cfg_data == NULL) {
  1371. printk(KERN_ERR "%s: Can't map external config space !",
  1372. port->node->full_name);
  1373. goto fail;
  1374. }
  1375. hose->cfg_data = cfg_data;
  1376. }
  1377. /* Always map the host config space in cfg_addr.
  1378. * Internal space is 4K
  1379. */
  1380. mbase = ioremap(port->cfg_space.start + 0x10000000, 0x1000);
  1381. if (mbase == NULL) {
  1382. printk(KERN_ERR "%s: Can't map internal config space !",
  1383. port->node->full_name);
  1384. goto fail;
  1385. }
  1386. hose->cfg_addr = mbase;
  1387. pr_debug("PCIE %s, bus %d..%d\n", port->node->full_name,
  1388. hose->first_busno, hose->last_busno);
  1389. pr_debug(" config space mapped at: root @0x%p, other @0x%p\n",
  1390. hose->cfg_addr, hose->cfg_data);
  1391. /* Setup config space */
  1392. hose->ops = &ppc4xx_pciex_pci_ops;
  1393. port->hose = hose;
  1394. mbase = (void __iomem *)hose->cfg_addr;
  1395. if (!port->endpoint) {
  1396. /*
  1397. * Set bus numbers on our root port
  1398. */
  1399. out_8(mbase + PCI_PRIMARY_BUS, hose->first_busno);
  1400. out_8(mbase + PCI_SECONDARY_BUS, hose->first_busno + 1);
  1401. out_8(mbase + PCI_SUBORDINATE_BUS, hose->last_busno);
  1402. }
  1403. /*
  1404. * OMRs are already reset, also disable PIMs
  1405. */
  1406. out_le32(mbase + PECFG_PIMEN, 0);
  1407. /* Parse outbound mapping resources */
  1408. pci_process_bridge_OF_ranges(hose, port->node, primary);
  1409. /* Parse inbound mapping resources */
  1410. if (ppc4xx_parse_dma_ranges(hose, mbase, &dma_window) != 0)
  1411. goto fail;
  1412. /* Configure outbound ranges POMs */
  1413. ppc4xx_configure_pciex_POMs(port, hose, mbase);
  1414. /* Configure inbound ranges PIMs */
  1415. ppc4xx_configure_pciex_PIMs(port, hose, mbase, &dma_window);
  1416. /* The root complex doesn't show up if we don't set some vendor
  1417. * and device IDs into it. The defaults below are the same bogus
  1418. * one that the initial code in arch/ppc had. This can be
  1419. * overwritten by setting the "vendor-id/device-id" properties
  1420. * in the pciex node.
  1421. */
  1422. /* Get the (optional) vendor-/device-id from the device-tree */
  1423. pval = of_get_property(port->node, "vendor-id", NULL);
  1424. if (pval) {
  1425. val = *pval;
  1426. } else {
  1427. if (!port->endpoint)
  1428. val = 0xaaa0 + port->index;
  1429. else
  1430. val = 0xeee0 + port->index;
  1431. }
  1432. out_le16(mbase + 0x200, val);
  1433. pval = of_get_property(port->node, "device-id", NULL);
  1434. if (pval) {
  1435. val = *pval;
  1436. } else {
  1437. if (!port->endpoint)
  1438. val = 0xbed0 + port->index;
  1439. else
  1440. val = 0xfed0 + port->index;
  1441. }
  1442. out_le16(mbase + 0x202, val);
  1443. if (!port->endpoint) {
  1444. /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
  1445. out_le32(mbase + 0x208, 0x06040001);
  1446. printk(KERN_INFO "PCIE%d: successfully set as root-complex\n",
  1447. port->index);
  1448. } else {
  1449. /* Set Class Code to Processor/PPC */
  1450. out_le32(mbase + 0x208, 0x0b200001);
  1451. printk(KERN_INFO "PCIE%d: successfully set as endpoint\n",
  1452. port->index);
  1453. }
  1454. return;
  1455. fail:
  1456. if (hose)
  1457. pcibios_free_controller(hose);
  1458. if (cfg_data)
  1459. iounmap(cfg_data);
  1460. if (mbase)
  1461. iounmap(mbase);
  1462. }
  1463. static void __init ppc4xx_probe_pciex_bridge(struct device_node *np)
  1464. {
  1465. struct ppc4xx_pciex_port *port;
  1466. const u32 *pval;
  1467. int portno;
  1468. unsigned int dcrs;
  1469. const char *val;
  1470. /* First, proceed to core initialization as we assume there's
  1471. * only one PCIe core in the system
  1472. */
  1473. if (ppc4xx_pciex_check_core_init(np))
  1474. return;
  1475. /* Get the port number from the device-tree */
  1476. pval = of_get_property(np, "port", NULL);
  1477. if (pval == NULL) {
  1478. printk(KERN_ERR "PCIE: Can't find port number for %s\n",
  1479. np->full_name);
  1480. return;
  1481. }
  1482. portno = *pval;
  1483. if (portno >= ppc4xx_pciex_port_count) {
  1484. printk(KERN_ERR "PCIE: port number out of range for %s\n",
  1485. np->full_name);
  1486. return;
  1487. }
  1488. port = &ppc4xx_pciex_ports[portno];
  1489. port->index = portno;
  1490. /*
  1491. * Check if device is enabled
  1492. */
  1493. if (!of_device_is_available(np)) {
  1494. printk(KERN_INFO "PCIE%d: Port disabled via device-tree\n", port->index);
  1495. return;
  1496. }
  1497. port->node = of_node_get(np);
  1498. pval = of_get_property(np, "sdr-base", NULL);
  1499. if (pval == NULL) {
  1500. printk(KERN_ERR "PCIE: missing sdr-base for %s\n",
  1501. np->full_name);
  1502. return;
  1503. }
  1504. port->sdr_base = *pval;
  1505. /* Check if device_type property is set to "pci" or "pci-endpoint".
  1506. * Resulting from this setup this PCIe port will be configured
  1507. * as root-complex or as endpoint.
  1508. */
  1509. val = of_get_property(port->node, "device_type", NULL);
  1510. if (!strcmp(val, "pci-endpoint")) {
  1511. port->endpoint = 1;
  1512. } else if (!strcmp(val, "pci")) {
  1513. port->endpoint = 0;
  1514. } else {
  1515. printk(KERN_ERR "PCIE: missing or incorrect device_type for %s\n",
  1516. np->full_name);
  1517. return;
  1518. }
  1519. /* Fetch config space registers address */
  1520. if (of_address_to_resource(np, 0, &port->cfg_space)) {
  1521. printk(KERN_ERR "%s: Can't get PCI-E config space !",
  1522. np->full_name);
  1523. return;
  1524. }
  1525. /* Fetch host bridge internal registers address */
  1526. if (of_address_to_resource(np, 1, &port->utl_regs)) {
  1527. printk(KERN_ERR "%s: Can't get UTL register base !",
  1528. np->full_name);
  1529. return;
  1530. }
  1531. /* Map DCRs */
  1532. dcrs = dcr_resource_start(np, 0);
  1533. if (dcrs == 0) {
  1534. printk(KERN_ERR "%s: Can't get DCR register base !",
  1535. np->full_name);
  1536. return;
  1537. }
  1538. port->dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
  1539. /* Initialize the port specific registers */
  1540. if (ppc4xx_pciex_port_init(port)) {
  1541. printk(KERN_WARNING "PCIE%d: Port init failed\n", port->index);
  1542. return;
  1543. }
  1544. /* Setup the linux hose data structure */
  1545. ppc4xx_pciex_port_setup_hose(port);
  1546. }
  1547. #endif /* CONFIG_PPC4xx_PCI_EXPRESS */
  1548. static int __init ppc4xx_pci_find_bridges(void)
  1549. {
  1550. struct device_node *np;
  1551. ppc_pci_flags |= PPC_PCI_ENABLE_PROC_DOMAINS | PPC_PCI_COMPAT_DOMAIN_0;
  1552. #ifdef CONFIG_PPC4xx_PCI_EXPRESS
  1553. for_each_compatible_node(np, NULL, "ibm,plb-pciex")
  1554. ppc4xx_probe_pciex_bridge(np);
  1555. #endif
  1556. for_each_compatible_node(np, NULL, "ibm,plb-pcix")
  1557. ppc4xx_probe_pcix_bridge(np);
  1558. for_each_compatible_node(np, NULL, "ibm,plb-pci")
  1559. ppc4xx_probe_pci_bridge(np);
  1560. return 0;
  1561. }
  1562. arch_initcall(ppc4xx_pci_find_bridges);