ppc4xx_pci.c 41 KB

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