ppc4xx_pci.c 58 KB

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