ppc4xx_pci.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  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. res->end + 1 - res->start,
  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 = res->end - res->start + 1;
  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, rsrc_reg.end + 1 - rsrc_reg.start);
  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. res->end + 1 - res->start,
  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 = res->end - res->start + 1;
  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, rsrc_reg.end + 1 - rsrc_reg.start);
  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. };
  553. static struct ppc4xx_pciex_hwops *ppc4xx_pciex_hwops;
  554. #ifdef CONFIG_44x
  555. /* Check various reset bits of the 440SPe PCIe core */
  556. static int __init ppc440spe_pciex_check_reset(struct device_node *np)
  557. {
  558. u32 valPE0, valPE1, valPE2;
  559. int err = 0;
  560. /* SDR0_PEGPLLLCT1 reset */
  561. if (!(mfdcri(SDR0, PESDR0_PLLLCT1) & 0x01000000)) {
  562. /*
  563. * the PCIe core was probably already initialised
  564. * by firmware - let's re-reset RCSSET regs
  565. *
  566. * -- Shouldn't we also re-reset the whole thing ? -- BenH
  567. */
  568. pr_debug("PCIE: SDR0_PLLLCT1 already reset.\n");
  569. mtdcri(SDR0, PESDR0_440SPE_RCSSET, 0x01010000);
  570. mtdcri(SDR0, PESDR1_440SPE_RCSSET, 0x01010000);
  571. mtdcri(SDR0, PESDR2_440SPE_RCSSET, 0x01010000);
  572. }
  573. valPE0 = mfdcri(SDR0, PESDR0_440SPE_RCSSET);
  574. valPE1 = mfdcri(SDR0, PESDR1_440SPE_RCSSET);
  575. valPE2 = mfdcri(SDR0, PESDR2_440SPE_RCSSET);
  576. /* SDR0_PExRCSSET rstgu */
  577. if (!(valPE0 & 0x01000000) ||
  578. !(valPE1 & 0x01000000) ||
  579. !(valPE2 & 0x01000000)) {
  580. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstgu error\n");
  581. err = -1;
  582. }
  583. /* SDR0_PExRCSSET rstdl */
  584. if (!(valPE0 & 0x00010000) ||
  585. !(valPE1 & 0x00010000) ||
  586. !(valPE2 & 0x00010000)) {
  587. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstdl error\n");
  588. err = -1;
  589. }
  590. /* SDR0_PExRCSSET rstpyn */
  591. if ((valPE0 & 0x00001000) ||
  592. (valPE1 & 0x00001000) ||
  593. (valPE2 & 0x00001000)) {
  594. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstpyn error\n");
  595. err = -1;
  596. }
  597. /* SDR0_PExRCSSET hldplb */
  598. if ((valPE0 & 0x10000000) ||
  599. (valPE1 & 0x10000000) ||
  600. (valPE2 & 0x10000000)) {
  601. printk(KERN_INFO "PCIE: SDR0_PExRCSSET hldplb error\n");
  602. err = -1;
  603. }
  604. /* SDR0_PExRCSSET rdy */
  605. if ((valPE0 & 0x00100000) ||
  606. (valPE1 & 0x00100000) ||
  607. (valPE2 & 0x00100000)) {
  608. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rdy error\n");
  609. err = -1;
  610. }
  611. /* SDR0_PExRCSSET shutdown */
  612. if ((valPE0 & 0x00000100) ||
  613. (valPE1 & 0x00000100) ||
  614. (valPE2 & 0x00000100)) {
  615. printk(KERN_INFO "PCIE: SDR0_PExRCSSET shutdown error\n");
  616. err = -1;
  617. }
  618. return err;
  619. }
  620. /* Global PCIe core initializations for 440SPe core */
  621. static int __init ppc440spe_pciex_core_init(struct device_node *np)
  622. {
  623. int time_out = 20;
  624. /* Set PLL clock receiver to LVPECL */
  625. dcri_clrset(SDR0, PESDR0_PLLLCT1, 0, 1 << 28);
  626. /* Shouldn't we do all the calibration stuff etc... here ? */
  627. if (ppc440spe_pciex_check_reset(np))
  628. return -ENXIO;
  629. if (!(mfdcri(SDR0, PESDR0_PLLLCT2) & 0x10000)) {
  630. printk(KERN_INFO "PCIE: PESDR_PLLCT2 resistance calibration "
  631. "failed (0x%08x)\n",
  632. mfdcri(SDR0, PESDR0_PLLLCT2));
  633. return -1;
  634. }
  635. /* De-assert reset of PCIe PLL, wait for lock */
  636. dcri_clrset(SDR0, PESDR0_PLLLCT1, 1 << 24, 0);
  637. udelay(3);
  638. while (time_out) {
  639. if (!(mfdcri(SDR0, PESDR0_PLLLCT3) & 0x10000000)) {
  640. time_out--;
  641. udelay(1);
  642. } else
  643. break;
  644. }
  645. if (!time_out) {
  646. printk(KERN_INFO "PCIE: VCO output not locked\n");
  647. return -1;
  648. }
  649. pr_debug("PCIE initialization OK\n");
  650. return 3;
  651. }
  652. static int ppc440spe_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  653. {
  654. u32 val = 1 << 24;
  655. if (port->endpoint)
  656. val = PTYPE_LEGACY_ENDPOINT << 20;
  657. else
  658. val = PTYPE_ROOT_PORT << 20;
  659. if (port->index == 0)
  660. val |= LNKW_X8 << 12;
  661. else
  662. val |= LNKW_X4 << 12;
  663. mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET, val);
  664. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x20222222);
  665. if (ppc440spe_revA())
  666. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x11000000);
  667. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL0SET1, 0x35000000);
  668. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL1SET1, 0x35000000);
  669. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL2SET1, 0x35000000);
  670. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL3SET1, 0x35000000);
  671. if (port->index == 0) {
  672. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL4SET1,
  673. 0x35000000);
  674. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL5SET1,
  675. 0x35000000);
  676. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL6SET1,
  677. 0x35000000);
  678. mtdcri(SDR0, port->sdr_base + PESDRn_440SPE_HSSL7SET1,
  679. 0x35000000);
  680. }
  681. dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET,
  682. (1 << 24) | (1 << 16), 1 << 12);
  683. return 0;
  684. }
  685. static int ppc440speA_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  686. {
  687. return ppc440spe_pciex_init_port_hw(port);
  688. }
  689. static int ppc440speB_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  690. {
  691. int rc = ppc440spe_pciex_init_port_hw(port);
  692. port->has_ibpre = 1;
  693. return rc;
  694. }
  695. static int ppc440speA_pciex_init_utl(struct ppc4xx_pciex_port *port)
  696. {
  697. /* XXX Check what that value means... I hate magic */
  698. dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x68782800);
  699. /*
  700. * Set buffer allocations and then assert VRB and TXE.
  701. */
  702. out_be32(port->utl_base + PEUTL_OUTTR, 0x08000000);
  703. out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
  704. out_be32(port->utl_base + PEUTL_OPDBSZ, 0x10000000);
  705. out_be32(port->utl_base + PEUTL_PBBSZ, 0x53000000);
  706. out_be32(port->utl_base + PEUTL_IPHBSZ, 0x08000000);
  707. out_be32(port->utl_base + PEUTL_IPDBSZ, 0x10000000);
  708. out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
  709. out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
  710. return 0;
  711. }
  712. static int ppc440speB_pciex_init_utl(struct ppc4xx_pciex_port *port)
  713. {
  714. /* Report CRS to the operating system */
  715. out_be32(port->utl_base + PEUTL_PBCTL, 0x08000000);
  716. return 0;
  717. }
  718. static struct ppc4xx_pciex_hwops ppc440speA_pcie_hwops __initdata =
  719. {
  720. .core_init = ppc440spe_pciex_core_init,
  721. .port_init_hw = ppc440speA_pciex_init_port_hw,
  722. .setup_utl = ppc440speA_pciex_init_utl,
  723. };
  724. static struct ppc4xx_pciex_hwops ppc440speB_pcie_hwops __initdata =
  725. {
  726. .core_init = ppc440spe_pciex_core_init,
  727. .port_init_hw = ppc440speB_pciex_init_port_hw,
  728. .setup_utl = ppc440speB_pciex_init_utl,
  729. };
  730. static int __init ppc460ex_pciex_core_init(struct device_node *np)
  731. {
  732. /* Nothing to do, return 2 ports */
  733. return 2;
  734. }
  735. static int ppc460ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  736. {
  737. u32 val;
  738. u32 utlset1;
  739. if (port->endpoint)
  740. val = PTYPE_LEGACY_ENDPOINT << 20;
  741. else
  742. val = PTYPE_ROOT_PORT << 20;
  743. if (port->index == 0) {
  744. val |= LNKW_X1 << 12;
  745. utlset1 = 0x20000000;
  746. } else {
  747. val |= LNKW_X4 << 12;
  748. utlset1 = 0x20101101;
  749. }
  750. mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET, val);
  751. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, utlset1);
  752. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01210000);
  753. switch (port->index) {
  754. case 0:
  755. mtdcri(SDR0, PESDR0_460EX_L0CDRCTL, 0x00003230);
  756. mtdcri(SDR0, PESDR0_460EX_L0DRV, 0x00000130);
  757. mtdcri(SDR0, PESDR0_460EX_L0CLK, 0x00000006);
  758. mtdcri(SDR0, PESDR0_460EX_PHY_CTL_RST,0x10000000);
  759. break;
  760. case 1:
  761. mtdcri(SDR0, PESDR1_460EX_L0CDRCTL, 0x00003230);
  762. mtdcri(SDR0, PESDR1_460EX_L1CDRCTL, 0x00003230);
  763. mtdcri(SDR0, PESDR1_460EX_L2CDRCTL, 0x00003230);
  764. mtdcri(SDR0, PESDR1_460EX_L3CDRCTL, 0x00003230);
  765. mtdcri(SDR0, PESDR1_460EX_L0DRV, 0x00000130);
  766. mtdcri(SDR0, PESDR1_460EX_L1DRV, 0x00000130);
  767. mtdcri(SDR0, PESDR1_460EX_L2DRV, 0x00000130);
  768. mtdcri(SDR0, PESDR1_460EX_L3DRV, 0x00000130);
  769. mtdcri(SDR0, PESDR1_460EX_L0CLK, 0x00000006);
  770. mtdcri(SDR0, PESDR1_460EX_L1CLK, 0x00000006);
  771. mtdcri(SDR0, PESDR1_460EX_L2CLK, 0x00000006);
  772. mtdcri(SDR0, PESDR1_460EX_L3CLK, 0x00000006);
  773. mtdcri(SDR0, PESDR1_460EX_PHY_CTL_RST,0x10000000);
  774. break;
  775. }
  776. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
  777. mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) |
  778. (PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTPYN));
  779. /* Poll for PHY reset */
  780. /* XXX FIXME add timeout */
  781. switch (port->index) {
  782. case 0:
  783. while (!(mfdcri(SDR0, PESDR0_460EX_RSTSTA) & 0x1))
  784. udelay(10);
  785. break;
  786. case 1:
  787. while (!(mfdcri(SDR0, PESDR1_460EX_RSTSTA) & 0x1))
  788. udelay(10);
  789. break;
  790. }
  791. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
  792. (mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) &
  793. ~(PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTDL)) |
  794. PESDRx_RCSSET_RSTPYN);
  795. port->has_ibpre = 1;
  796. return 0;
  797. }
  798. static int ppc460ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
  799. {
  800. dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
  801. /*
  802. * Set buffer allocations and then assert VRB and TXE.
  803. */
  804. out_be32(port->utl_base + PEUTL_PBCTL, 0x0800000c);
  805. out_be32(port->utl_base + PEUTL_OUTTR, 0x08000000);
  806. out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
  807. out_be32(port->utl_base + PEUTL_OPDBSZ, 0x04000000);
  808. out_be32(port->utl_base + PEUTL_PBBSZ, 0x00000000);
  809. out_be32(port->utl_base + PEUTL_IPHBSZ, 0x02000000);
  810. out_be32(port->utl_base + PEUTL_IPDBSZ, 0x04000000);
  811. out_be32(port->utl_base + PEUTL_RCIRQEN,0x00f00000);
  812. out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
  813. return 0;
  814. }
  815. static struct ppc4xx_pciex_hwops ppc460ex_pcie_hwops __initdata =
  816. {
  817. .core_init = ppc460ex_pciex_core_init,
  818. .port_init_hw = ppc460ex_pciex_init_port_hw,
  819. .setup_utl = ppc460ex_pciex_init_utl,
  820. };
  821. static int __init ppc460sx_pciex_core_init(struct device_node *np)
  822. {
  823. /* HSS drive amplitude */
  824. mtdcri(SDR0, PESDR0_460SX_HSSL0DAMP, 0xB9843211);
  825. mtdcri(SDR0, PESDR0_460SX_HSSL1DAMP, 0xB9843211);
  826. mtdcri(SDR0, PESDR0_460SX_HSSL2DAMP, 0xB9843211);
  827. mtdcri(SDR0, PESDR0_460SX_HSSL3DAMP, 0xB9843211);
  828. mtdcri(SDR0, PESDR0_460SX_HSSL4DAMP, 0xB9843211);
  829. mtdcri(SDR0, PESDR0_460SX_HSSL5DAMP, 0xB9843211);
  830. mtdcri(SDR0, PESDR0_460SX_HSSL6DAMP, 0xB9843211);
  831. mtdcri(SDR0, PESDR0_460SX_HSSL7DAMP, 0xB9843211);
  832. mtdcri(SDR0, PESDR1_460SX_HSSL0DAMP, 0xB9843211);
  833. mtdcri(SDR0, PESDR1_460SX_HSSL1DAMP, 0xB9843211);
  834. mtdcri(SDR0, PESDR1_460SX_HSSL2DAMP, 0xB9843211);
  835. mtdcri(SDR0, PESDR1_460SX_HSSL3DAMP, 0xB9843211);
  836. mtdcri(SDR0, PESDR2_460SX_HSSL0DAMP, 0xB9843211);
  837. mtdcri(SDR0, PESDR2_460SX_HSSL1DAMP, 0xB9843211);
  838. mtdcri(SDR0, PESDR2_460SX_HSSL2DAMP, 0xB9843211);
  839. mtdcri(SDR0, PESDR2_460SX_HSSL3DAMP, 0xB9843211);
  840. /* HSS TX pre-emphasis */
  841. mtdcri(SDR0, PESDR0_460SX_HSSL0COEFA, 0xDCB98987);
  842. mtdcri(SDR0, PESDR0_460SX_HSSL1COEFA, 0xDCB98987);
  843. mtdcri(SDR0, PESDR0_460SX_HSSL2COEFA, 0xDCB98987);
  844. mtdcri(SDR0, PESDR0_460SX_HSSL3COEFA, 0xDCB98987);
  845. mtdcri(SDR0, PESDR0_460SX_HSSL4COEFA, 0xDCB98987);
  846. mtdcri(SDR0, PESDR0_460SX_HSSL5COEFA, 0xDCB98987);
  847. mtdcri(SDR0, PESDR0_460SX_HSSL6COEFA, 0xDCB98987);
  848. mtdcri(SDR0, PESDR0_460SX_HSSL7COEFA, 0xDCB98987);
  849. mtdcri(SDR0, PESDR1_460SX_HSSL0COEFA, 0xDCB98987);
  850. mtdcri(SDR0, PESDR1_460SX_HSSL1COEFA, 0xDCB98987);
  851. mtdcri(SDR0, PESDR1_460SX_HSSL2COEFA, 0xDCB98987);
  852. mtdcri(SDR0, PESDR1_460SX_HSSL3COEFA, 0xDCB98987);
  853. mtdcri(SDR0, PESDR2_460SX_HSSL0COEFA, 0xDCB98987);
  854. mtdcri(SDR0, PESDR2_460SX_HSSL1COEFA, 0xDCB98987);
  855. mtdcri(SDR0, PESDR2_460SX_HSSL2COEFA, 0xDCB98987);
  856. mtdcri(SDR0, PESDR2_460SX_HSSL3COEFA, 0xDCB98987);
  857. /* HSS TX calibration control */
  858. mtdcri(SDR0, PESDR0_460SX_HSSL1CALDRV, 0x22222222);
  859. mtdcri(SDR0, PESDR1_460SX_HSSL1CALDRV, 0x22220000);
  860. mtdcri(SDR0, PESDR2_460SX_HSSL1CALDRV, 0x22220000);
  861. /* HSS TX slew control */
  862. mtdcri(SDR0, PESDR0_460SX_HSSSLEW, 0xFFFFFFFF);
  863. mtdcri(SDR0, PESDR1_460SX_HSSSLEW, 0xFFFF0000);
  864. mtdcri(SDR0, PESDR2_460SX_HSSSLEW, 0xFFFF0000);
  865. udelay(100);
  866. /* De-assert PLLRESET */
  867. dcri_clrset(SDR0, PESDR0_PLLLCT2, 0x00000100, 0);
  868. /* Reset DL, UTL, GPL before configuration */
  869. mtdcri(SDR0, PESDR0_460SX_RCSSET,
  870. PESDRx_RCSSET_RSTDL | PESDRx_RCSSET_RSTGU);
  871. mtdcri(SDR0, PESDR1_460SX_RCSSET,
  872. PESDRx_RCSSET_RSTDL | PESDRx_RCSSET_RSTGU);
  873. mtdcri(SDR0, PESDR2_460SX_RCSSET,
  874. PESDRx_RCSSET_RSTDL | PESDRx_RCSSET_RSTGU);
  875. udelay(100);
  876. /*
  877. * If bifurcation is not enabled, u-boot would have disabled the
  878. * third PCIe port
  879. */
  880. if (((mfdcri(SDR0, PESDR1_460SX_HSSCTLSET) & 0x00000001) ==
  881. 0x00000001)) {
  882. printk(KERN_INFO "PCI: PCIE bifurcation setup successfully.\n");
  883. printk(KERN_INFO "PCI: Total 3 PCIE ports are present\n");
  884. return 3;
  885. }
  886. printk(KERN_INFO "PCI: Total 2 PCIE ports are present\n");
  887. return 2;
  888. }
  889. static int ppc460sx_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  890. {
  891. if (port->endpoint)
  892. dcri_clrset(SDR0, port->sdr_base + PESDRn_UTLSET2,
  893. 0x01000000, 0);
  894. else
  895. dcri_clrset(SDR0, port->sdr_base + PESDRn_UTLSET2,
  896. 0, 0x01000000);
  897. /*Gen-1*/
  898. mtdcri(SDR0, port->sdr_base + PESDRn_460SX_RCEI, 0x08000000);
  899. dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET,
  900. (PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTDL),
  901. PESDRx_RCSSET_RSTPYN);
  902. port->has_ibpre = 1;
  903. return 0;
  904. }
  905. static int ppc460sx_pciex_init_utl(struct ppc4xx_pciex_port *port)
  906. {
  907. /* Max 128 Bytes */
  908. out_be32 (port->utl_base + PEUTL_PBBSZ, 0x00000000);
  909. return 0;
  910. }
  911. static struct ppc4xx_pciex_hwops ppc460sx_pcie_hwops __initdata = {
  912. .core_init = ppc460sx_pciex_core_init,
  913. .port_init_hw = ppc460sx_pciex_init_port_hw,
  914. .setup_utl = ppc460sx_pciex_init_utl,
  915. };
  916. #endif /* CONFIG_44x */
  917. #ifdef CONFIG_40x
  918. static int __init ppc405ex_pciex_core_init(struct device_node *np)
  919. {
  920. /* Nothing to do, return 2 ports */
  921. return 2;
  922. }
  923. static void ppc405ex_pcie_phy_reset(struct ppc4xx_pciex_port *port)
  924. {
  925. /* Assert the PE0_PHY reset */
  926. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01010000);
  927. msleep(1);
  928. /* deassert the PE0_hotreset */
  929. if (port->endpoint)
  930. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01111000);
  931. else
  932. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01101000);
  933. /* poll for phy !reset */
  934. /* XXX FIXME add timeout */
  935. while (!(mfdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSTA) & 0x00001000))
  936. ;
  937. /* deassert the PE0_gpl_utl_reset */
  938. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x00101000);
  939. }
  940. static int ppc405ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  941. {
  942. u32 val;
  943. if (port->endpoint)
  944. val = PTYPE_LEGACY_ENDPOINT;
  945. else
  946. val = PTYPE_ROOT_PORT;
  947. mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET,
  948. 1 << 24 | val << 20 | LNKW_X1 << 12);
  949. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x00000000);
  950. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01010000);
  951. mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET1, 0x720F0000);
  952. mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET2, 0x70600003);
  953. /*
  954. * Only reset the PHY when no link is currently established.
  955. * This is for the Atheros PCIe board which has problems to establish
  956. * the link (again) after this PHY reset. All other currently tested
  957. * PCIe boards don't show this problem.
  958. * This has to be re-tested and fixed in a later release!
  959. */
  960. val = mfdcri(SDR0, port->sdr_base + PESDRn_LOOP);
  961. if (!(val & 0x00001000))
  962. ppc405ex_pcie_phy_reset(port);
  963. dcr_write(port->dcrs, DCRO_PEGPL_CFG, 0x10000000); /* guarded on */
  964. port->has_ibpre = 1;
  965. return 0;
  966. }
  967. static int ppc405ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
  968. {
  969. dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
  970. /*
  971. * Set buffer allocations and then assert VRB and TXE.
  972. */
  973. out_be32(port->utl_base + PEUTL_OUTTR, 0x02000000);
  974. out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
  975. out_be32(port->utl_base + PEUTL_OPDBSZ, 0x04000000);
  976. out_be32(port->utl_base + PEUTL_PBBSZ, 0x21000000);
  977. out_be32(port->utl_base + PEUTL_IPHBSZ, 0x02000000);
  978. out_be32(port->utl_base + PEUTL_IPDBSZ, 0x04000000);
  979. out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
  980. out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
  981. out_be32(port->utl_base + PEUTL_PBCTL, 0x08000000);
  982. return 0;
  983. }
  984. static struct ppc4xx_pciex_hwops ppc405ex_pcie_hwops __initdata =
  985. {
  986. .core_init = ppc405ex_pciex_core_init,
  987. .port_init_hw = ppc405ex_pciex_init_port_hw,
  988. .setup_utl = ppc405ex_pciex_init_utl,
  989. };
  990. #endif /* CONFIG_40x */
  991. /* Check that the core has been initied and if not, do it */
  992. static int __init ppc4xx_pciex_check_core_init(struct device_node *np)
  993. {
  994. static int core_init;
  995. int count = -ENODEV;
  996. if (core_init++)
  997. return 0;
  998. #ifdef CONFIG_44x
  999. if (of_device_is_compatible(np, "ibm,plb-pciex-440spe")) {
  1000. if (ppc440spe_revA())
  1001. ppc4xx_pciex_hwops = &ppc440speA_pcie_hwops;
  1002. else
  1003. ppc4xx_pciex_hwops = &ppc440speB_pcie_hwops;
  1004. }
  1005. if (of_device_is_compatible(np, "ibm,plb-pciex-460ex"))
  1006. ppc4xx_pciex_hwops = &ppc460ex_pcie_hwops;
  1007. if (of_device_is_compatible(np, "ibm,plb-pciex-460sx"))
  1008. ppc4xx_pciex_hwops = &ppc460sx_pcie_hwops;
  1009. #endif /* CONFIG_44x */
  1010. #ifdef CONFIG_40x
  1011. if (of_device_is_compatible(np, "ibm,plb-pciex-405ex"))
  1012. ppc4xx_pciex_hwops = &ppc405ex_pcie_hwops;
  1013. #endif
  1014. if (ppc4xx_pciex_hwops == NULL) {
  1015. printk(KERN_WARNING "PCIE: unknown host type %s\n",
  1016. np->full_name);
  1017. return -ENODEV;
  1018. }
  1019. count = ppc4xx_pciex_hwops->core_init(np);
  1020. if (count > 0) {
  1021. ppc4xx_pciex_ports =
  1022. kzalloc(count * sizeof(struct ppc4xx_pciex_port),
  1023. GFP_KERNEL);
  1024. if (ppc4xx_pciex_ports) {
  1025. ppc4xx_pciex_port_count = count;
  1026. return 0;
  1027. }
  1028. printk(KERN_WARNING "PCIE: failed to allocate ports array\n");
  1029. return -ENOMEM;
  1030. }
  1031. return -ENODEV;
  1032. }
  1033. static void __init ppc4xx_pciex_port_init_mapping(struct ppc4xx_pciex_port *port)
  1034. {
  1035. /* We map PCI Express configuration based on the reg property */
  1036. dcr_write(port->dcrs, DCRO_PEGPL_CFGBAH,
  1037. RES_TO_U32_HIGH(port->cfg_space.start));
  1038. dcr_write(port->dcrs, DCRO_PEGPL_CFGBAL,
  1039. RES_TO_U32_LOW(port->cfg_space.start));
  1040. /* XXX FIXME: Use size from reg property. For now, map 512M */
  1041. dcr_write(port->dcrs, DCRO_PEGPL_CFGMSK, 0xe0000001);
  1042. /* We map UTL registers based on the reg property */
  1043. dcr_write(port->dcrs, DCRO_PEGPL_REGBAH,
  1044. RES_TO_U32_HIGH(port->utl_regs.start));
  1045. dcr_write(port->dcrs, DCRO_PEGPL_REGBAL,
  1046. RES_TO_U32_LOW(port->utl_regs.start));
  1047. /* XXX FIXME: Use size from reg property */
  1048. dcr_write(port->dcrs, DCRO_PEGPL_REGMSK, 0x00007001);
  1049. /* Disable all other outbound windows */
  1050. dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, 0);
  1051. dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, 0);
  1052. dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, 0);
  1053. dcr_write(port->dcrs, DCRO_PEGPL_MSGMSK, 0);
  1054. }
  1055. static int __init ppc4xx_pciex_wait_on_sdr(struct ppc4xx_pciex_port *port,
  1056. unsigned int sdr_offset,
  1057. unsigned int mask,
  1058. unsigned int value,
  1059. int timeout_ms)
  1060. {
  1061. u32 val;
  1062. while(timeout_ms--) {
  1063. val = mfdcri(SDR0, port->sdr_base + sdr_offset);
  1064. if ((val & mask) == value) {
  1065. pr_debug("PCIE%d: Wait on SDR %x success with tm %d (%08x)\n",
  1066. port->index, sdr_offset, timeout_ms, val);
  1067. return 0;
  1068. }
  1069. msleep(1);
  1070. }
  1071. return -1;
  1072. }
  1073. static int __init ppc4xx_pciex_port_init(struct ppc4xx_pciex_port *port)
  1074. {
  1075. int rc = 0;
  1076. /* Init HW */
  1077. if (ppc4xx_pciex_hwops->port_init_hw)
  1078. rc = ppc4xx_pciex_hwops->port_init_hw(port);
  1079. if (rc != 0)
  1080. return rc;
  1081. printk(KERN_INFO "PCIE%d: Checking link...\n",
  1082. port->index);
  1083. /* Wait for reset to complete */
  1084. if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS, 1 << 20, 0, 10)) {
  1085. printk(KERN_WARNING "PCIE%d: PGRST failed\n",
  1086. port->index);
  1087. return -1;
  1088. }
  1089. /* Check for card presence detect if supported, if not, just wait for
  1090. * link unconditionally.
  1091. *
  1092. * note that we don't fail if there is no link, we just filter out
  1093. * config space accesses. That way, it will be easier to implement
  1094. * hotplug later on.
  1095. */
  1096. if (!port->has_ibpre ||
  1097. !ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
  1098. 1 << 28, 1 << 28, 100)) {
  1099. printk(KERN_INFO
  1100. "PCIE%d: Device detected, waiting for link...\n",
  1101. port->index);
  1102. if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
  1103. 0x1000, 0x1000, 2000))
  1104. printk(KERN_WARNING
  1105. "PCIE%d: Link up failed\n", port->index);
  1106. else {
  1107. printk(KERN_INFO
  1108. "PCIE%d: link is up !\n", port->index);
  1109. port->link = 1;
  1110. }
  1111. } else
  1112. printk(KERN_INFO "PCIE%d: No device detected.\n", port->index);
  1113. /*
  1114. * Initialize mapping: disable all regions and configure
  1115. * CFG and REG regions based on resources in the device tree
  1116. */
  1117. ppc4xx_pciex_port_init_mapping(port);
  1118. /*
  1119. * Map UTL
  1120. */
  1121. port->utl_base = ioremap(port->utl_regs.start, 0x100);
  1122. BUG_ON(port->utl_base == NULL);
  1123. /*
  1124. * Setup UTL registers --BenH.
  1125. */
  1126. if (ppc4xx_pciex_hwops->setup_utl)
  1127. ppc4xx_pciex_hwops->setup_utl(port);
  1128. /*
  1129. * Check for VC0 active and assert RDY.
  1130. */
  1131. if (port->link &&
  1132. ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS,
  1133. 1 << 16, 1 << 16, 5000)) {
  1134. printk(KERN_INFO "PCIE%d: VC0 not active\n", port->index);
  1135. port->link = 0;
  1136. }
  1137. dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET, 0, 1 << 20);
  1138. msleep(100);
  1139. return 0;
  1140. }
  1141. static int ppc4xx_pciex_validate_bdf(struct ppc4xx_pciex_port *port,
  1142. struct pci_bus *bus,
  1143. unsigned int devfn)
  1144. {
  1145. static int message;
  1146. /* Endpoint can not generate upstream(remote) config cycles */
  1147. if (port->endpoint && bus->number != port->hose->first_busno)
  1148. return PCIBIOS_DEVICE_NOT_FOUND;
  1149. /* Check we are within the mapped range */
  1150. if (bus->number > port->hose->last_busno) {
  1151. if (!message) {
  1152. printk(KERN_WARNING "Warning! Probing bus %u"
  1153. " out of range !\n", bus->number);
  1154. message++;
  1155. }
  1156. return PCIBIOS_DEVICE_NOT_FOUND;
  1157. }
  1158. /* The root complex has only one device / function */
  1159. if (bus->number == port->hose->first_busno && devfn != 0)
  1160. return PCIBIOS_DEVICE_NOT_FOUND;
  1161. /* The other side of the RC has only one device as well */
  1162. if (bus->number == (port->hose->first_busno + 1) &&
  1163. PCI_SLOT(devfn) != 0)
  1164. return PCIBIOS_DEVICE_NOT_FOUND;
  1165. /* Check if we have a link */
  1166. if ((bus->number != port->hose->first_busno) && !port->link)
  1167. return PCIBIOS_DEVICE_NOT_FOUND;
  1168. return 0;
  1169. }
  1170. static void __iomem *ppc4xx_pciex_get_config_base(struct ppc4xx_pciex_port *port,
  1171. struct pci_bus *bus,
  1172. unsigned int devfn)
  1173. {
  1174. int relbus;
  1175. /* Remove the casts when we finally remove the stupid volatile
  1176. * in struct pci_controller
  1177. */
  1178. if (bus->number == port->hose->first_busno)
  1179. return (void __iomem *)port->hose->cfg_addr;
  1180. relbus = bus->number - (port->hose->first_busno + 1);
  1181. return (void __iomem *)port->hose->cfg_data +
  1182. ((relbus << 20) | (devfn << 12));
  1183. }
  1184. static int ppc4xx_pciex_read_config(struct pci_bus *bus, unsigned int devfn,
  1185. int offset, int len, u32 *val)
  1186. {
  1187. struct pci_controller *hose = pci_bus_to_host(bus);
  1188. struct ppc4xx_pciex_port *port =
  1189. &ppc4xx_pciex_ports[hose->indirect_type];
  1190. void __iomem *addr;
  1191. u32 gpl_cfg;
  1192. BUG_ON(hose != port->hose);
  1193. if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
  1194. return PCIBIOS_DEVICE_NOT_FOUND;
  1195. addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
  1196. /*
  1197. * Reading from configuration space of non-existing device can
  1198. * generate transaction errors. For the read duration we suppress
  1199. * assertion of machine check exceptions to avoid those.
  1200. */
  1201. gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
  1202. dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
  1203. /* Make sure no CRS is recorded */
  1204. out_be32(port->utl_base + PEUTL_RCSTA, 0x00040000);
  1205. switch (len) {
  1206. case 1:
  1207. *val = in_8((u8 *)(addr + offset));
  1208. break;
  1209. case 2:
  1210. *val = in_le16((u16 *)(addr + offset));
  1211. break;
  1212. default:
  1213. *val = in_le32((u32 *)(addr + offset));
  1214. break;
  1215. }
  1216. pr_debug("pcie-config-read: bus=%3d [%3d..%3d] devfn=0x%04x"
  1217. " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
  1218. bus->number, hose->first_busno, hose->last_busno,
  1219. devfn, offset, len, addr + offset, *val);
  1220. /* Check for CRS (440SPe rev B does that for us but heh ..) */
  1221. if (in_be32(port->utl_base + PEUTL_RCSTA) & 0x00040000) {
  1222. pr_debug("Got CRS !\n");
  1223. if (len != 4 || offset != 0)
  1224. return PCIBIOS_DEVICE_NOT_FOUND;
  1225. *val = 0xffff0001;
  1226. }
  1227. dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
  1228. return PCIBIOS_SUCCESSFUL;
  1229. }
  1230. static int ppc4xx_pciex_write_config(struct pci_bus *bus, unsigned int devfn,
  1231. int offset, int len, u32 val)
  1232. {
  1233. struct pci_controller *hose = pci_bus_to_host(bus);
  1234. struct ppc4xx_pciex_port *port =
  1235. &ppc4xx_pciex_ports[hose->indirect_type];
  1236. void __iomem *addr;
  1237. u32 gpl_cfg;
  1238. if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
  1239. return PCIBIOS_DEVICE_NOT_FOUND;
  1240. addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
  1241. /*
  1242. * Reading from configuration space of non-existing device can
  1243. * generate transaction errors. For the read duration we suppress
  1244. * assertion of machine check exceptions to avoid those.
  1245. */
  1246. gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
  1247. dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
  1248. pr_debug("pcie-config-write: bus=%3d [%3d..%3d] devfn=0x%04x"
  1249. " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
  1250. bus->number, hose->first_busno, hose->last_busno,
  1251. devfn, offset, len, addr + offset, val);
  1252. switch (len) {
  1253. case 1:
  1254. out_8((u8 *)(addr + offset), val);
  1255. break;
  1256. case 2:
  1257. out_le16((u16 *)(addr + offset), val);
  1258. break;
  1259. default:
  1260. out_le32((u32 *)(addr + offset), val);
  1261. break;
  1262. }
  1263. dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
  1264. return PCIBIOS_SUCCESSFUL;
  1265. }
  1266. static struct pci_ops ppc4xx_pciex_pci_ops =
  1267. {
  1268. .read = ppc4xx_pciex_read_config,
  1269. .write = ppc4xx_pciex_write_config,
  1270. };
  1271. static int __init ppc4xx_setup_one_pciex_POM(struct ppc4xx_pciex_port *port,
  1272. struct pci_controller *hose,
  1273. void __iomem *mbase,
  1274. u64 plb_addr,
  1275. u64 pci_addr,
  1276. u64 size,
  1277. unsigned int flags,
  1278. int index)
  1279. {
  1280. u32 lah, lal, pciah, pcial, sa;
  1281. if (!is_power_of_2(size) ||
  1282. (index < 2 && size < 0x100000) ||
  1283. (index == 2 && size < 0x100) ||
  1284. (plb_addr & (size - 1)) != 0) {
  1285. printk(KERN_WARNING "%s: Resource out of range\n",
  1286. hose->dn->full_name);
  1287. return -1;
  1288. }
  1289. /* Calculate register values */
  1290. lah = RES_TO_U32_HIGH(plb_addr);
  1291. lal = RES_TO_U32_LOW(plb_addr);
  1292. pciah = RES_TO_U32_HIGH(pci_addr);
  1293. pcial = RES_TO_U32_LOW(pci_addr);
  1294. sa = (0xffffffffu << ilog2(size)) | 0x1;
  1295. /* Program register values */
  1296. switch (index) {
  1297. case 0:
  1298. out_le32(mbase + PECFG_POM0LAH, pciah);
  1299. out_le32(mbase + PECFG_POM0LAL, pcial);
  1300. dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAH, lah);
  1301. dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAL, lal);
  1302. dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKH, 0x7fffffff);
  1303. /* Note that 3 here means enabled | single region */
  1304. dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, sa | 3);
  1305. break;
  1306. case 1:
  1307. out_le32(mbase + PECFG_POM1LAH, pciah);
  1308. out_le32(mbase + PECFG_POM1LAL, pcial);
  1309. dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAH, lah);
  1310. dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAL, lal);
  1311. dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKH, 0x7fffffff);
  1312. /* Note that 3 here means enabled | single region */
  1313. dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, sa | 3);
  1314. break;
  1315. case 2:
  1316. out_le32(mbase + PECFG_POM2LAH, pciah);
  1317. out_le32(mbase + PECFG_POM2LAL, pcial);
  1318. dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAH, lah);
  1319. dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAL, lal);
  1320. dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKH, 0x7fffffff);
  1321. /* Note that 3 here means enabled | IO space !!! */
  1322. dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, sa | 3);
  1323. break;
  1324. }
  1325. return 0;
  1326. }
  1327. static void __init ppc4xx_configure_pciex_POMs(struct ppc4xx_pciex_port *port,
  1328. struct pci_controller *hose,
  1329. void __iomem *mbase)
  1330. {
  1331. int i, j, found_isa_hole = 0;
  1332. /* Setup outbound memory windows */
  1333. for (i = j = 0; i < 3; i++) {
  1334. struct resource *res = &hose->mem_resources[i];
  1335. /* we only care about memory windows */
  1336. if (!(res->flags & IORESOURCE_MEM))
  1337. continue;
  1338. if (j > 1) {
  1339. printk(KERN_WARNING "%s: Too many ranges\n",
  1340. port->node->full_name);
  1341. break;
  1342. }
  1343. /* Configure the resource */
  1344. if (ppc4xx_setup_one_pciex_POM(port, hose, mbase,
  1345. res->start,
  1346. res->start - hose->pci_mem_offset,
  1347. res->end + 1 - res->start,
  1348. res->flags,
  1349. j) == 0) {
  1350. j++;
  1351. /* If the resource PCI address is 0 then we have our
  1352. * ISA memory hole
  1353. */
  1354. if (res->start == hose->pci_mem_offset)
  1355. found_isa_hole = 1;
  1356. }
  1357. }
  1358. /* Handle ISA memory hole if not already covered */
  1359. if (j <= 1 && !found_isa_hole && hose->isa_mem_size)
  1360. if (ppc4xx_setup_one_pciex_POM(port, hose, mbase,
  1361. hose->isa_mem_phys, 0,
  1362. hose->isa_mem_size, 0, j) == 0)
  1363. printk(KERN_INFO "%s: Legacy ISA memory support enabled\n",
  1364. hose->dn->full_name);
  1365. /* Configure IO, always 64K starting at 0. We hard wire it to 64K !
  1366. * Note also that it -has- to be region index 2 on this HW
  1367. */
  1368. if (hose->io_resource.flags & IORESOURCE_IO)
  1369. ppc4xx_setup_one_pciex_POM(port, hose, mbase,
  1370. hose->io_base_phys, 0,
  1371. 0x10000, IORESOURCE_IO, 2);
  1372. }
  1373. static void __init ppc4xx_configure_pciex_PIMs(struct ppc4xx_pciex_port *port,
  1374. struct pci_controller *hose,
  1375. void __iomem *mbase,
  1376. struct resource *res)
  1377. {
  1378. resource_size_t size = res->end - res->start + 1;
  1379. u64 sa;
  1380. if (port->endpoint) {
  1381. resource_size_t ep_addr = 0;
  1382. resource_size_t ep_size = 32 << 20;
  1383. /* Currently we map a fixed 64MByte window to PLB address
  1384. * 0 (SDRAM). This should probably be configurable via a dts
  1385. * property.
  1386. */
  1387. /* Calculate window size */
  1388. sa = (0xffffffffffffffffull << ilog2(ep_size));
  1389. /* Setup BAR0 */
  1390. out_le32(mbase + PECFG_BAR0HMPA, RES_TO_U32_HIGH(sa));
  1391. out_le32(mbase + PECFG_BAR0LMPA, RES_TO_U32_LOW(sa) |
  1392. PCI_BASE_ADDRESS_MEM_TYPE_64);
  1393. /* Disable BAR1 & BAR2 */
  1394. out_le32(mbase + PECFG_BAR1MPA, 0);
  1395. out_le32(mbase + PECFG_BAR2HMPA, 0);
  1396. out_le32(mbase + PECFG_BAR2LMPA, 0);
  1397. out_le32(mbase + PECFG_PIM01SAH, RES_TO_U32_HIGH(sa));
  1398. out_le32(mbase + PECFG_PIM01SAL, RES_TO_U32_LOW(sa));
  1399. out_le32(mbase + PCI_BASE_ADDRESS_0, RES_TO_U32_LOW(ep_addr));
  1400. out_le32(mbase + PCI_BASE_ADDRESS_1, RES_TO_U32_HIGH(ep_addr));
  1401. } else {
  1402. /* Calculate window size */
  1403. sa = (0xffffffffffffffffull << ilog2(size));
  1404. if (res->flags & IORESOURCE_PREFETCH)
  1405. sa |= 0x8;
  1406. out_le32(mbase + PECFG_BAR0HMPA, RES_TO_U32_HIGH(sa));
  1407. out_le32(mbase + PECFG_BAR0LMPA, RES_TO_U32_LOW(sa));
  1408. /* The setup of the split looks weird to me ... let's see
  1409. * if it works
  1410. */
  1411. out_le32(mbase + PECFG_PIM0LAL, 0x00000000);
  1412. out_le32(mbase + PECFG_PIM0LAH, 0x00000000);
  1413. out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
  1414. out_le32(mbase + PECFG_PIM1LAH, 0x00000000);
  1415. out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
  1416. out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
  1417. out_le32(mbase + PCI_BASE_ADDRESS_0, RES_TO_U32_LOW(res->start));
  1418. out_le32(mbase + PCI_BASE_ADDRESS_1, RES_TO_U32_HIGH(res->start));
  1419. }
  1420. /* Enable inbound mapping */
  1421. out_le32(mbase + PECFG_PIMEN, 0x1);
  1422. /* Enable I/O, Mem, and Busmaster cycles */
  1423. out_le16(mbase + PCI_COMMAND,
  1424. in_le16(mbase + PCI_COMMAND) |
  1425. PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  1426. }
  1427. static void __init ppc4xx_pciex_port_setup_hose(struct ppc4xx_pciex_port *port)
  1428. {
  1429. struct resource dma_window;
  1430. struct pci_controller *hose = NULL;
  1431. const int *bus_range;
  1432. int primary = 0, busses;
  1433. void __iomem *mbase = NULL, *cfg_data = NULL;
  1434. const u32 *pval;
  1435. u32 val;
  1436. /* Check if primary bridge */
  1437. if (of_get_property(port->node, "primary", NULL))
  1438. primary = 1;
  1439. /* Get bus range if any */
  1440. bus_range = of_get_property(port->node, "bus-range", NULL);
  1441. /* Allocate the host controller data structure */
  1442. hose = pcibios_alloc_controller(port->node);
  1443. if (!hose)
  1444. goto fail;
  1445. /* We stick the port number in "indirect_type" so the config space
  1446. * ops can retrieve the port data structure easily
  1447. */
  1448. hose->indirect_type = port->index;
  1449. /* Get bus range */
  1450. hose->first_busno = bus_range ? bus_range[0] : 0x0;
  1451. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  1452. /* Because of how big mapping the config space is (1M per bus), we
  1453. * limit how many busses we support. In the long run, we could replace
  1454. * that with something akin to kmap_atomic instead. We set aside 1 bus
  1455. * for the host itself too.
  1456. */
  1457. busses = hose->last_busno - hose->first_busno; /* This is off by 1 */
  1458. if (busses > MAX_PCIE_BUS_MAPPED) {
  1459. busses = MAX_PCIE_BUS_MAPPED;
  1460. hose->last_busno = hose->first_busno + busses;
  1461. }
  1462. if (!port->endpoint) {
  1463. /* Only map the external config space in cfg_data for
  1464. * PCIe root-complexes. External space is 1M per bus
  1465. */
  1466. cfg_data = ioremap(port->cfg_space.start +
  1467. (hose->first_busno + 1) * 0x100000,
  1468. busses * 0x100000);
  1469. if (cfg_data == NULL) {
  1470. printk(KERN_ERR "%s: Can't map external config space !",
  1471. port->node->full_name);
  1472. goto fail;
  1473. }
  1474. hose->cfg_data = cfg_data;
  1475. }
  1476. /* Always map the host config space in cfg_addr.
  1477. * Internal space is 4K
  1478. */
  1479. mbase = ioremap(port->cfg_space.start + 0x10000000, 0x1000);
  1480. if (mbase == NULL) {
  1481. printk(KERN_ERR "%s: Can't map internal config space !",
  1482. port->node->full_name);
  1483. goto fail;
  1484. }
  1485. hose->cfg_addr = mbase;
  1486. pr_debug("PCIE %s, bus %d..%d\n", port->node->full_name,
  1487. hose->first_busno, hose->last_busno);
  1488. pr_debug(" config space mapped at: root @0x%p, other @0x%p\n",
  1489. hose->cfg_addr, hose->cfg_data);
  1490. /* Setup config space */
  1491. hose->ops = &ppc4xx_pciex_pci_ops;
  1492. port->hose = hose;
  1493. mbase = (void __iomem *)hose->cfg_addr;
  1494. if (!port->endpoint) {
  1495. /*
  1496. * Set bus numbers on our root port
  1497. */
  1498. out_8(mbase + PCI_PRIMARY_BUS, hose->first_busno);
  1499. out_8(mbase + PCI_SECONDARY_BUS, hose->first_busno + 1);
  1500. out_8(mbase + PCI_SUBORDINATE_BUS, hose->last_busno);
  1501. }
  1502. /*
  1503. * OMRs are already reset, also disable PIMs
  1504. */
  1505. out_le32(mbase + PECFG_PIMEN, 0);
  1506. /* Parse outbound mapping resources */
  1507. pci_process_bridge_OF_ranges(hose, port->node, primary);
  1508. /* Parse inbound mapping resources */
  1509. if (ppc4xx_parse_dma_ranges(hose, mbase, &dma_window) != 0)
  1510. goto fail;
  1511. /* Configure outbound ranges POMs */
  1512. ppc4xx_configure_pciex_POMs(port, hose, mbase);
  1513. /* Configure inbound ranges PIMs */
  1514. ppc4xx_configure_pciex_PIMs(port, hose, mbase, &dma_window);
  1515. /* The root complex doesn't show up if we don't set some vendor
  1516. * and device IDs into it. The defaults below are the same bogus
  1517. * one that the initial code in arch/ppc had. This can be
  1518. * overwritten by setting the "vendor-id/device-id" properties
  1519. * in the pciex node.
  1520. */
  1521. /* Get the (optional) vendor-/device-id from the device-tree */
  1522. pval = of_get_property(port->node, "vendor-id", NULL);
  1523. if (pval) {
  1524. val = *pval;
  1525. } else {
  1526. if (!port->endpoint)
  1527. val = 0xaaa0 + port->index;
  1528. else
  1529. val = 0xeee0 + port->index;
  1530. }
  1531. out_le16(mbase + 0x200, val);
  1532. pval = of_get_property(port->node, "device-id", NULL);
  1533. if (pval) {
  1534. val = *pval;
  1535. } else {
  1536. if (!port->endpoint)
  1537. val = 0xbed0 + port->index;
  1538. else
  1539. val = 0xfed0 + port->index;
  1540. }
  1541. out_le16(mbase + 0x202, val);
  1542. if (!port->endpoint) {
  1543. /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
  1544. out_le32(mbase + 0x208, 0x06040001);
  1545. printk(KERN_INFO "PCIE%d: successfully set as root-complex\n",
  1546. port->index);
  1547. } else {
  1548. /* Set Class Code to Processor/PPC */
  1549. out_le32(mbase + 0x208, 0x0b200001);
  1550. printk(KERN_INFO "PCIE%d: successfully set as endpoint\n",
  1551. port->index);
  1552. }
  1553. return;
  1554. fail:
  1555. if (hose)
  1556. pcibios_free_controller(hose);
  1557. if (cfg_data)
  1558. iounmap(cfg_data);
  1559. if (mbase)
  1560. iounmap(mbase);
  1561. }
  1562. static void __init ppc4xx_probe_pciex_bridge(struct device_node *np)
  1563. {
  1564. struct ppc4xx_pciex_port *port;
  1565. const u32 *pval;
  1566. int portno;
  1567. unsigned int dcrs;
  1568. const char *val;
  1569. /* First, proceed to core initialization as we assume there's
  1570. * only one PCIe core in the system
  1571. */
  1572. if (ppc4xx_pciex_check_core_init(np))
  1573. return;
  1574. /* Get the port number from the device-tree */
  1575. pval = of_get_property(np, "port", NULL);
  1576. if (pval == NULL) {
  1577. printk(KERN_ERR "PCIE: Can't find port number for %s\n",
  1578. np->full_name);
  1579. return;
  1580. }
  1581. portno = *pval;
  1582. if (portno >= ppc4xx_pciex_port_count) {
  1583. printk(KERN_ERR "PCIE: port number out of range for %s\n",
  1584. np->full_name);
  1585. return;
  1586. }
  1587. port = &ppc4xx_pciex_ports[portno];
  1588. port->index = portno;
  1589. /*
  1590. * Check if device is enabled
  1591. */
  1592. if (!of_device_is_available(np)) {
  1593. printk(KERN_INFO "PCIE%d: Port disabled via device-tree\n", port->index);
  1594. return;
  1595. }
  1596. port->node = of_node_get(np);
  1597. pval = of_get_property(np, "sdr-base", NULL);
  1598. if (pval == NULL) {
  1599. printk(KERN_ERR "PCIE: missing sdr-base for %s\n",
  1600. np->full_name);
  1601. return;
  1602. }
  1603. port->sdr_base = *pval;
  1604. /* Check if device_type property is set to "pci" or "pci-endpoint".
  1605. * Resulting from this setup this PCIe port will be configured
  1606. * as root-complex or as endpoint.
  1607. */
  1608. val = of_get_property(port->node, "device_type", NULL);
  1609. if (!strcmp(val, "pci-endpoint")) {
  1610. port->endpoint = 1;
  1611. } else if (!strcmp(val, "pci")) {
  1612. port->endpoint = 0;
  1613. } else {
  1614. printk(KERN_ERR "PCIE: missing or incorrect device_type for %s\n",
  1615. np->full_name);
  1616. return;
  1617. }
  1618. /* Fetch config space registers address */
  1619. if (of_address_to_resource(np, 0, &port->cfg_space)) {
  1620. printk(KERN_ERR "%s: Can't get PCI-E config space !",
  1621. np->full_name);
  1622. return;
  1623. }
  1624. /* Fetch host bridge internal registers address */
  1625. if (of_address_to_resource(np, 1, &port->utl_regs)) {
  1626. printk(KERN_ERR "%s: Can't get UTL register base !",
  1627. np->full_name);
  1628. return;
  1629. }
  1630. /* Map DCRs */
  1631. dcrs = dcr_resource_start(np, 0);
  1632. if (dcrs == 0) {
  1633. printk(KERN_ERR "%s: Can't get DCR register base !",
  1634. np->full_name);
  1635. return;
  1636. }
  1637. port->dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
  1638. /* Initialize the port specific registers */
  1639. if (ppc4xx_pciex_port_init(port)) {
  1640. printk(KERN_WARNING "PCIE%d: Port init failed\n", port->index);
  1641. return;
  1642. }
  1643. /* Setup the linux hose data structure */
  1644. ppc4xx_pciex_port_setup_hose(port);
  1645. }
  1646. #endif /* CONFIG_PPC4xx_PCI_EXPRESS */
  1647. static int __init ppc4xx_pci_find_bridges(void)
  1648. {
  1649. struct device_node *np;
  1650. ppc_pci_flags |= PPC_PCI_ENABLE_PROC_DOMAINS | PPC_PCI_COMPAT_DOMAIN_0;
  1651. #ifdef CONFIG_PPC4xx_PCI_EXPRESS
  1652. for_each_compatible_node(np, NULL, "ibm,plb-pciex")
  1653. ppc4xx_probe_pciex_bridge(np);
  1654. #endif
  1655. for_each_compatible_node(np, NULL, "ibm,plb-pcix")
  1656. ppc4xx_probe_pcix_bridge(np);
  1657. for_each_compatible_node(np, NULL, "ibm,plb-pci")
  1658. ppc4xx_probe_pci_bridge(np);
  1659. return 0;
  1660. }
  1661. arch_initcall(ppc4xx_pci_find_bridges);