ppc4xx_pci.c 49 KB

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