ppc4xx_pci.c 54 KB

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