ppc4xx_pci.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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. * ibm,plb-pciex-460ex
  447. *
  448. * Anything else will be rejected for now as they are all subtly
  449. * different unfortunately.
  450. *
  451. */
  452. #define MAX_PCIE_BUS_MAPPED 0x40
  453. struct ppc4xx_pciex_port
  454. {
  455. struct pci_controller *hose;
  456. struct device_node *node;
  457. unsigned int index;
  458. int endpoint;
  459. int link;
  460. int has_ibpre;
  461. unsigned int sdr_base;
  462. dcr_host_t dcrs;
  463. struct resource cfg_space;
  464. struct resource utl_regs;
  465. void __iomem *utl_base;
  466. };
  467. static struct ppc4xx_pciex_port *ppc4xx_pciex_ports;
  468. static unsigned int ppc4xx_pciex_port_count;
  469. struct ppc4xx_pciex_hwops
  470. {
  471. int (*core_init)(struct device_node *np);
  472. int (*port_init_hw)(struct ppc4xx_pciex_port *port);
  473. int (*setup_utl)(struct ppc4xx_pciex_port *port);
  474. };
  475. static struct ppc4xx_pciex_hwops *ppc4xx_pciex_hwops;
  476. #ifdef CONFIG_44x
  477. /* Check various reset bits of the 440SPe PCIe core */
  478. static int __init ppc440spe_pciex_check_reset(struct device_node *np)
  479. {
  480. u32 valPE0, valPE1, valPE2;
  481. int err = 0;
  482. /* SDR0_PEGPLLLCT1 reset */
  483. if (!(mfdcri(SDR0, PESDR0_PLLLCT1) & 0x01000000)) {
  484. /*
  485. * the PCIe core was probably already initialised
  486. * by firmware - let's re-reset RCSSET regs
  487. *
  488. * -- Shouldn't we also re-reset the whole thing ? -- BenH
  489. */
  490. pr_debug("PCIE: SDR0_PLLLCT1 already reset.\n");
  491. mtdcri(SDR0, PESDR0_440SPE_RCSSET, 0x01010000);
  492. mtdcri(SDR0, PESDR1_440SPE_RCSSET, 0x01010000);
  493. mtdcri(SDR0, PESDR2_440SPE_RCSSET, 0x01010000);
  494. }
  495. valPE0 = mfdcri(SDR0, PESDR0_440SPE_RCSSET);
  496. valPE1 = mfdcri(SDR0, PESDR1_440SPE_RCSSET);
  497. valPE2 = mfdcri(SDR0, PESDR2_440SPE_RCSSET);
  498. /* SDR0_PExRCSSET rstgu */
  499. if (!(valPE0 & 0x01000000) ||
  500. !(valPE1 & 0x01000000) ||
  501. !(valPE2 & 0x01000000)) {
  502. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstgu error\n");
  503. err = -1;
  504. }
  505. /* SDR0_PExRCSSET rstdl */
  506. if (!(valPE0 & 0x00010000) ||
  507. !(valPE1 & 0x00010000) ||
  508. !(valPE2 & 0x00010000)) {
  509. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstdl error\n");
  510. err = -1;
  511. }
  512. /* SDR0_PExRCSSET rstpyn */
  513. if ((valPE0 & 0x00001000) ||
  514. (valPE1 & 0x00001000) ||
  515. (valPE2 & 0x00001000)) {
  516. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstpyn error\n");
  517. err = -1;
  518. }
  519. /* SDR0_PExRCSSET hldplb */
  520. if ((valPE0 & 0x10000000) ||
  521. (valPE1 & 0x10000000) ||
  522. (valPE2 & 0x10000000)) {
  523. printk(KERN_INFO "PCIE: SDR0_PExRCSSET hldplb error\n");
  524. err = -1;
  525. }
  526. /* SDR0_PExRCSSET rdy */
  527. if ((valPE0 & 0x00100000) ||
  528. (valPE1 & 0x00100000) ||
  529. (valPE2 & 0x00100000)) {
  530. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rdy error\n");
  531. err = -1;
  532. }
  533. /* SDR0_PExRCSSET shutdown */
  534. if ((valPE0 & 0x00000100) ||
  535. (valPE1 & 0x00000100) ||
  536. (valPE2 & 0x00000100)) {
  537. printk(KERN_INFO "PCIE: SDR0_PExRCSSET shutdown error\n");
  538. err = -1;
  539. }
  540. return err;
  541. }
  542. /* Global PCIe core initializations for 440SPe core */
  543. static int __init ppc440spe_pciex_core_init(struct device_node *np)
  544. {
  545. int time_out = 20;
  546. /* Set PLL clock receiver to LVPECL */
  547. dcri_clrset(SDR0, PESDR0_PLLLCT1, 0, 1 << 28);
  548. /* Shouldn't we do all the calibration stuff etc... here ? */
  549. if (ppc440spe_pciex_check_reset(np))
  550. return -ENXIO;
  551. if (!(mfdcri(SDR0, PESDR0_PLLLCT2) & 0x10000)) {
  552. printk(KERN_INFO "PCIE: PESDR_PLLCT2 resistance calibration "
  553. "failed (0x%08x)\n",
  554. mfdcri(SDR0, PESDR0_PLLLCT2));
  555. return -1;
  556. }
  557. /* De-assert reset of PCIe PLL, wait for lock */
  558. dcri_clrset(SDR0, PESDR0_PLLLCT1, 1 << 24, 0);
  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. dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET,
  604. (1 << 24) | (1 << 16), 1 << 12);
  605. return 0;
  606. }
  607. static int ppc440speA_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  608. {
  609. return ppc440spe_pciex_init_port_hw(port);
  610. }
  611. static int ppc440speB_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  612. {
  613. int rc = ppc440spe_pciex_init_port_hw(port);
  614. port->has_ibpre = 1;
  615. return rc;
  616. }
  617. static int ppc440speA_pciex_init_utl(struct ppc4xx_pciex_port *port)
  618. {
  619. /* XXX Check what that value means... I hate magic */
  620. dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x68782800);
  621. /*
  622. * Set buffer allocations and then assert VRB and TXE.
  623. */
  624. out_be32(port->utl_base + PEUTL_OUTTR, 0x08000000);
  625. out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
  626. out_be32(port->utl_base + PEUTL_OPDBSZ, 0x10000000);
  627. out_be32(port->utl_base + PEUTL_PBBSZ, 0x53000000);
  628. out_be32(port->utl_base + PEUTL_IPHBSZ, 0x08000000);
  629. out_be32(port->utl_base + PEUTL_IPDBSZ, 0x10000000);
  630. out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
  631. out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
  632. return 0;
  633. }
  634. static int ppc440speB_pciex_init_utl(struct ppc4xx_pciex_port *port)
  635. {
  636. /* Report CRS to the operating system */
  637. out_be32(port->utl_base + PEUTL_PBCTL, 0x08000000);
  638. return 0;
  639. }
  640. static struct ppc4xx_pciex_hwops ppc440speA_pcie_hwops __initdata =
  641. {
  642. .core_init = ppc440spe_pciex_core_init,
  643. .port_init_hw = ppc440speA_pciex_init_port_hw,
  644. .setup_utl = ppc440speA_pciex_init_utl,
  645. };
  646. static struct ppc4xx_pciex_hwops ppc440speB_pcie_hwops __initdata =
  647. {
  648. .core_init = ppc440spe_pciex_core_init,
  649. .port_init_hw = ppc440speB_pciex_init_port_hw,
  650. .setup_utl = ppc440speB_pciex_init_utl,
  651. };
  652. static int __init ppc460ex_pciex_core_init(struct device_node *np)
  653. {
  654. /* Nothing to do, return 2 ports */
  655. return 2;
  656. }
  657. static int ppc460ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  658. {
  659. u32 val;
  660. u32 utlset1;
  661. if (port->endpoint) {
  662. val = PTYPE_LEGACY_ENDPOINT << 20;
  663. utlset1 = 0x20222222;
  664. } else {
  665. val = PTYPE_ROOT_PORT << 20;
  666. utlset1 = 0x21222222;
  667. }
  668. if (port->index == 0) {
  669. val |= LNKW_X1 << 12;
  670. } else {
  671. val |= LNKW_X4 << 12;
  672. utlset1 |= 0x00101101;
  673. }
  674. mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET, val);
  675. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, utlset1);
  676. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01210000);
  677. switch (port->index) {
  678. case 0:
  679. mtdcri(SDR0, PESDR0_460EX_L0CDRCTL, 0x00003230);
  680. mtdcri(SDR0, PESDR0_460EX_L0DRV, 0x00000136);
  681. mtdcri(SDR0, PESDR0_460EX_L0CLK, 0x00000006);
  682. mtdcri(SDR0, PESDR0_460EX_PHY_CTL_RST,0x10000000);
  683. break;
  684. case 1:
  685. mtdcri(SDR0, PESDR1_460EX_L0CDRCTL, 0x00003230);
  686. mtdcri(SDR0, PESDR1_460EX_L1CDRCTL, 0x00003230);
  687. mtdcri(SDR0, PESDR1_460EX_L2CDRCTL, 0x00003230);
  688. mtdcri(SDR0, PESDR1_460EX_L3CDRCTL, 0x00003230);
  689. mtdcri(SDR0, PESDR1_460EX_L0DRV, 0x00000136);
  690. mtdcri(SDR0, PESDR1_460EX_L1DRV, 0x00000136);
  691. mtdcri(SDR0, PESDR1_460EX_L2DRV, 0x00000136);
  692. mtdcri(SDR0, PESDR1_460EX_L3DRV, 0x00000136);
  693. mtdcri(SDR0, PESDR1_460EX_L0CLK, 0x00000006);
  694. mtdcri(SDR0, PESDR1_460EX_L1CLK, 0x00000006);
  695. mtdcri(SDR0, PESDR1_460EX_L2CLK, 0x00000006);
  696. mtdcri(SDR0, PESDR1_460EX_L3CLK, 0x00000006);
  697. mtdcri(SDR0, PESDR1_460EX_PHY_CTL_RST,0x10000000);
  698. break;
  699. }
  700. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
  701. mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) |
  702. (PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTPYN));
  703. /* Poll for PHY reset */
  704. /* XXX FIXME add timeout */
  705. switch (port->index) {
  706. case 0:
  707. while (!(mfdcri(SDR0, PESDR0_460EX_RSTSTA) & 0x1))
  708. udelay(10);
  709. break;
  710. case 1:
  711. while (!(mfdcri(SDR0, PESDR1_460EX_RSTSTA) & 0x1))
  712. udelay(10);
  713. break;
  714. }
  715. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET,
  716. (mfdcri(SDR0, port->sdr_base + PESDRn_RCSSET) &
  717. ~(PESDRx_RCSSET_RSTGU | PESDRx_RCSSET_RSTDL)) |
  718. PESDRx_RCSSET_RSTPYN);
  719. port->has_ibpre = 1;
  720. return 0;
  721. }
  722. static int ppc460ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
  723. {
  724. dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
  725. /*
  726. * Set buffer allocations and then assert VRB and TXE.
  727. */
  728. out_be32(port->utl_base + PEUTL_PBCTL, 0x0800000c);
  729. out_be32(port->utl_base + PEUTL_OUTTR, 0x08000000);
  730. out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
  731. out_be32(port->utl_base + PEUTL_OPDBSZ, 0x04000000);
  732. out_be32(port->utl_base + PEUTL_PBBSZ, 0x00000000);
  733. out_be32(port->utl_base + PEUTL_IPHBSZ, 0x02000000);
  734. out_be32(port->utl_base + PEUTL_IPDBSZ, 0x04000000);
  735. out_be32(port->utl_base + PEUTL_RCIRQEN,0x00f00000);
  736. out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
  737. return 0;
  738. }
  739. static struct ppc4xx_pciex_hwops ppc460ex_pcie_hwops __initdata =
  740. {
  741. .core_init = ppc460ex_pciex_core_init,
  742. .port_init_hw = ppc460ex_pciex_init_port_hw,
  743. .setup_utl = ppc460ex_pciex_init_utl,
  744. };
  745. #endif /* CONFIG_44x */
  746. #ifdef CONFIG_40x
  747. static int __init ppc405ex_pciex_core_init(struct device_node *np)
  748. {
  749. /* Nothing to do, return 2 ports */
  750. return 2;
  751. }
  752. static void ppc405ex_pcie_phy_reset(struct ppc4xx_pciex_port *port)
  753. {
  754. /* Assert the PE0_PHY reset */
  755. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01010000);
  756. msleep(1);
  757. /* deassert the PE0_hotreset */
  758. if (port->endpoint)
  759. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01111000);
  760. else
  761. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x01101000);
  762. /* poll for phy !reset */
  763. /* XXX FIXME add timeout */
  764. while (!(mfdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSTA) & 0x00001000))
  765. ;
  766. /* deassert the PE0_gpl_utl_reset */
  767. mtdcri(SDR0, port->sdr_base + PESDRn_RCSSET, 0x00101000);
  768. }
  769. static int ppc405ex_pciex_init_port_hw(struct ppc4xx_pciex_port *port)
  770. {
  771. u32 val;
  772. if (port->endpoint)
  773. val = PTYPE_LEGACY_ENDPOINT;
  774. else
  775. val = PTYPE_ROOT_PORT;
  776. mtdcri(SDR0, port->sdr_base + PESDRn_DLPSET,
  777. 1 << 24 | val << 20 | LNKW_X1 << 12);
  778. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET1, 0x00000000);
  779. mtdcri(SDR0, port->sdr_base + PESDRn_UTLSET2, 0x01010000);
  780. mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET1, 0x720F0000);
  781. mtdcri(SDR0, port->sdr_base + PESDRn_405EX_PHYSET2, 0x70600003);
  782. /*
  783. * Only reset the PHY when no link is currently established.
  784. * This is for the Atheros PCIe board which has problems to establish
  785. * the link (again) after this PHY reset. All other currently tested
  786. * PCIe boards don't show this problem.
  787. * This has to be re-tested and fixed in a later release!
  788. */
  789. #if 0 /* XXX FIXME: Not resetting the PHY will leave all resources
  790. * configured as done previously by U-Boot. Then Linux will currently
  791. * not reassign them. So the PHY reset is now done always. This will
  792. * lead to problems with the Atheros PCIe board again.
  793. */
  794. val = mfdcri(SDR0, port->sdr_base + PESDRn_LOOP);
  795. if (!(val & 0x00001000))
  796. ppc405ex_pcie_phy_reset(port);
  797. #else
  798. ppc405ex_pcie_phy_reset(port);
  799. #endif
  800. dcr_write(port->dcrs, DCRO_PEGPL_CFG, 0x10000000); /* guarded on */
  801. port->has_ibpre = 1;
  802. return 0;
  803. }
  804. static int ppc405ex_pciex_init_utl(struct ppc4xx_pciex_port *port)
  805. {
  806. dcr_write(port->dcrs, DCRO_PEGPL_SPECIAL, 0x0);
  807. /*
  808. * Set buffer allocations and then assert VRB and TXE.
  809. */
  810. out_be32(port->utl_base + PEUTL_OUTTR, 0x02000000);
  811. out_be32(port->utl_base + PEUTL_INTR, 0x02000000);
  812. out_be32(port->utl_base + PEUTL_OPDBSZ, 0x04000000);
  813. out_be32(port->utl_base + PEUTL_PBBSZ, 0x21000000);
  814. out_be32(port->utl_base + PEUTL_IPHBSZ, 0x02000000);
  815. out_be32(port->utl_base + PEUTL_IPDBSZ, 0x04000000);
  816. out_be32(port->utl_base + PEUTL_RCIRQEN, 0x00f00000);
  817. out_be32(port->utl_base + PEUTL_PCTL, 0x80800066);
  818. out_be32(port->utl_base + PEUTL_PBCTL, 0x08000000);
  819. return 0;
  820. }
  821. static struct ppc4xx_pciex_hwops ppc405ex_pcie_hwops __initdata =
  822. {
  823. .core_init = ppc405ex_pciex_core_init,
  824. .port_init_hw = ppc405ex_pciex_init_port_hw,
  825. .setup_utl = ppc405ex_pciex_init_utl,
  826. };
  827. #endif /* CONFIG_40x */
  828. /* Check that the core has been initied and if not, do it */
  829. static int __init ppc4xx_pciex_check_core_init(struct device_node *np)
  830. {
  831. static int core_init;
  832. int count = -ENODEV;
  833. if (core_init++)
  834. return 0;
  835. #ifdef CONFIG_44x
  836. if (of_device_is_compatible(np, "ibm,plb-pciex-440spe")) {
  837. if (ppc440spe_revA())
  838. ppc4xx_pciex_hwops = &ppc440speA_pcie_hwops;
  839. else
  840. ppc4xx_pciex_hwops = &ppc440speB_pcie_hwops;
  841. }
  842. if (of_device_is_compatible(np, "ibm,plb-pciex-460ex"))
  843. ppc4xx_pciex_hwops = &ppc460ex_pcie_hwops;
  844. #endif /* CONFIG_44x */
  845. #ifdef CONFIG_40x
  846. if (of_device_is_compatible(np, "ibm,plb-pciex-405ex"))
  847. ppc4xx_pciex_hwops = &ppc405ex_pcie_hwops;
  848. #endif
  849. if (ppc4xx_pciex_hwops == NULL) {
  850. printk(KERN_WARNING "PCIE: unknown host type %s\n",
  851. np->full_name);
  852. return -ENODEV;
  853. }
  854. count = ppc4xx_pciex_hwops->core_init(np);
  855. if (count > 0) {
  856. ppc4xx_pciex_ports =
  857. kzalloc(count * sizeof(struct ppc4xx_pciex_port),
  858. GFP_KERNEL);
  859. if (ppc4xx_pciex_ports) {
  860. ppc4xx_pciex_port_count = count;
  861. return 0;
  862. }
  863. printk(KERN_WARNING "PCIE: failed to allocate ports array\n");
  864. return -ENOMEM;
  865. }
  866. return -ENODEV;
  867. }
  868. static void __init ppc4xx_pciex_port_init_mapping(struct ppc4xx_pciex_port *port)
  869. {
  870. /* We map PCI Express configuration based on the reg property */
  871. dcr_write(port->dcrs, DCRO_PEGPL_CFGBAH,
  872. RES_TO_U32_HIGH(port->cfg_space.start));
  873. dcr_write(port->dcrs, DCRO_PEGPL_CFGBAL,
  874. RES_TO_U32_LOW(port->cfg_space.start));
  875. /* XXX FIXME: Use size from reg property. For now, map 512M */
  876. dcr_write(port->dcrs, DCRO_PEGPL_CFGMSK, 0xe0000001);
  877. /* We map UTL registers based on the reg property */
  878. dcr_write(port->dcrs, DCRO_PEGPL_REGBAH,
  879. RES_TO_U32_HIGH(port->utl_regs.start));
  880. dcr_write(port->dcrs, DCRO_PEGPL_REGBAL,
  881. RES_TO_U32_LOW(port->utl_regs.start));
  882. /* XXX FIXME: Use size from reg property */
  883. dcr_write(port->dcrs, DCRO_PEGPL_REGMSK, 0x00007001);
  884. /* Disable all other outbound windows */
  885. dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, 0);
  886. dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, 0);
  887. dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, 0);
  888. dcr_write(port->dcrs, DCRO_PEGPL_MSGMSK, 0);
  889. }
  890. static int __init ppc4xx_pciex_wait_on_sdr(struct ppc4xx_pciex_port *port,
  891. unsigned int sdr_offset,
  892. unsigned int mask,
  893. unsigned int value,
  894. int timeout_ms)
  895. {
  896. u32 val;
  897. while(timeout_ms--) {
  898. val = mfdcri(SDR0, port->sdr_base + sdr_offset);
  899. if ((val & mask) == value) {
  900. pr_debug("PCIE%d: Wait on SDR %x success with tm %d (%08x)\n",
  901. port->index, sdr_offset, timeout_ms, val);
  902. return 0;
  903. }
  904. msleep(1);
  905. }
  906. return -1;
  907. }
  908. static int __init ppc4xx_pciex_port_init(struct ppc4xx_pciex_port *port)
  909. {
  910. int rc = 0;
  911. /* Init HW */
  912. if (ppc4xx_pciex_hwops->port_init_hw)
  913. rc = ppc4xx_pciex_hwops->port_init_hw(port);
  914. if (rc != 0)
  915. return rc;
  916. printk(KERN_INFO "PCIE%d: Checking link...\n",
  917. port->index);
  918. /* Wait for reset to complete */
  919. if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS, 1 << 20, 0, 10)) {
  920. printk(KERN_WARNING "PCIE%d: PGRST failed\n",
  921. port->index);
  922. return -1;
  923. }
  924. /* Check for card presence detect if supported, if not, just wait for
  925. * link unconditionally.
  926. *
  927. * note that we don't fail if there is no link, we just filter out
  928. * config space accesses. That way, it will be easier to implement
  929. * hotplug later on.
  930. */
  931. if (!port->has_ibpre ||
  932. !ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
  933. 1 << 28, 1 << 28, 100)) {
  934. printk(KERN_INFO
  935. "PCIE%d: Device detected, waiting for link...\n",
  936. port->index);
  937. if (ppc4xx_pciex_wait_on_sdr(port, PESDRn_LOOP,
  938. 0x1000, 0x1000, 2000))
  939. printk(KERN_WARNING
  940. "PCIE%d: Link up failed\n", port->index);
  941. else {
  942. printk(KERN_INFO
  943. "PCIE%d: link is up !\n", port->index);
  944. port->link = 1;
  945. }
  946. } else
  947. printk(KERN_INFO "PCIE%d: No device detected.\n", port->index);
  948. /*
  949. * Initialize mapping: disable all regions and configure
  950. * CFG and REG regions based on resources in the device tree
  951. */
  952. ppc4xx_pciex_port_init_mapping(port);
  953. /*
  954. * Map UTL
  955. */
  956. port->utl_base = ioremap(port->utl_regs.start, 0x100);
  957. BUG_ON(port->utl_base == NULL);
  958. /*
  959. * Setup UTL registers --BenH.
  960. */
  961. if (ppc4xx_pciex_hwops->setup_utl)
  962. ppc4xx_pciex_hwops->setup_utl(port);
  963. /*
  964. * Check for VC0 active and assert RDY.
  965. */
  966. if (port->link &&
  967. ppc4xx_pciex_wait_on_sdr(port, PESDRn_RCSSTS,
  968. 1 << 16, 1 << 16, 5000)) {
  969. printk(KERN_INFO "PCIE%d: VC0 not active\n", port->index);
  970. port->link = 0;
  971. }
  972. dcri_clrset(SDR0, port->sdr_base + PESDRn_RCSSET, 0, 1 << 20);
  973. msleep(100);
  974. return 0;
  975. }
  976. static int ppc4xx_pciex_validate_bdf(struct ppc4xx_pciex_port *port,
  977. struct pci_bus *bus,
  978. unsigned int devfn)
  979. {
  980. static int message;
  981. /* Endpoint can not generate upstream(remote) config cycles */
  982. if (port->endpoint && bus->number != port->hose->first_busno)
  983. return PCIBIOS_DEVICE_NOT_FOUND;
  984. /* Check we are within the mapped range */
  985. if (bus->number > port->hose->last_busno) {
  986. if (!message) {
  987. printk(KERN_WARNING "Warning! Probing bus %u"
  988. " out of range !\n", bus->number);
  989. message++;
  990. }
  991. return PCIBIOS_DEVICE_NOT_FOUND;
  992. }
  993. /* The root complex has only one device / function */
  994. if (bus->number == port->hose->first_busno && devfn != 0)
  995. return PCIBIOS_DEVICE_NOT_FOUND;
  996. /* The other side of the RC has only one device as well */
  997. if (bus->number == (port->hose->first_busno + 1) &&
  998. PCI_SLOT(devfn) != 0)
  999. return PCIBIOS_DEVICE_NOT_FOUND;
  1000. /* Check if we have a link */
  1001. if ((bus->number != port->hose->first_busno) && !port->link)
  1002. return PCIBIOS_DEVICE_NOT_FOUND;
  1003. return 0;
  1004. }
  1005. static void __iomem *ppc4xx_pciex_get_config_base(struct ppc4xx_pciex_port *port,
  1006. struct pci_bus *bus,
  1007. unsigned int devfn)
  1008. {
  1009. int relbus;
  1010. /* Remove the casts when we finally remove the stupid volatile
  1011. * in struct pci_controller
  1012. */
  1013. if (bus->number == port->hose->first_busno)
  1014. return (void __iomem *)port->hose->cfg_addr;
  1015. relbus = bus->number - (port->hose->first_busno + 1);
  1016. return (void __iomem *)port->hose->cfg_data +
  1017. ((relbus << 20) | (devfn << 12));
  1018. }
  1019. static int ppc4xx_pciex_read_config(struct pci_bus *bus, unsigned int devfn,
  1020. int offset, int len, u32 *val)
  1021. {
  1022. struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
  1023. struct ppc4xx_pciex_port *port =
  1024. &ppc4xx_pciex_ports[hose->indirect_type];
  1025. void __iomem *addr;
  1026. u32 gpl_cfg;
  1027. BUG_ON(hose != port->hose);
  1028. if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
  1029. return PCIBIOS_DEVICE_NOT_FOUND;
  1030. addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
  1031. /*
  1032. * Reading from configuration space of non-existing device can
  1033. * generate transaction errors. For the read duration we suppress
  1034. * assertion of machine check exceptions to avoid those.
  1035. */
  1036. gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
  1037. dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
  1038. /* Make sure no CRS is recorded */
  1039. out_be32(port->utl_base + PEUTL_RCSTA, 0x00040000);
  1040. switch (len) {
  1041. case 1:
  1042. *val = in_8((u8 *)(addr + offset));
  1043. break;
  1044. case 2:
  1045. *val = in_le16((u16 *)(addr + offset));
  1046. break;
  1047. default:
  1048. *val = in_le32((u32 *)(addr + offset));
  1049. break;
  1050. }
  1051. pr_debug("pcie-config-read: bus=%3d [%3d..%3d] devfn=0x%04x"
  1052. " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
  1053. bus->number, hose->first_busno, hose->last_busno,
  1054. devfn, offset, len, addr + offset, *val);
  1055. /* Check for CRS (440SPe rev B does that for us but heh ..) */
  1056. if (in_be32(port->utl_base + PEUTL_RCSTA) & 0x00040000) {
  1057. pr_debug("Got CRS !\n");
  1058. if (len != 4 || offset != 0)
  1059. return PCIBIOS_DEVICE_NOT_FOUND;
  1060. *val = 0xffff0001;
  1061. }
  1062. dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
  1063. return PCIBIOS_SUCCESSFUL;
  1064. }
  1065. static int ppc4xx_pciex_write_config(struct pci_bus *bus, unsigned int devfn,
  1066. int offset, int len, u32 val)
  1067. {
  1068. struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
  1069. struct ppc4xx_pciex_port *port =
  1070. &ppc4xx_pciex_ports[hose->indirect_type];
  1071. void __iomem *addr;
  1072. u32 gpl_cfg;
  1073. if (ppc4xx_pciex_validate_bdf(port, bus, devfn) != 0)
  1074. return PCIBIOS_DEVICE_NOT_FOUND;
  1075. addr = ppc4xx_pciex_get_config_base(port, bus, devfn);
  1076. /*
  1077. * Reading from configuration space of non-existing device can
  1078. * generate transaction errors. For the read duration we suppress
  1079. * assertion of machine check exceptions to avoid those.
  1080. */
  1081. gpl_cfg = dcr_read(port->dcrs, DCRO_PEGPL_CFG);
  1082. dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg | GPL_DMER_MASK_DISA);
  1083. pr_debug("pcie-config-write: bus=%3d [%3d..%3d] devfn=0x%04x"
  1084. " offset=0x%04x len=%d, addr=0x%p val=0x%08x\n",
  1085. bus->number, hose->first_busno, hose->last_busno,
  1086. devfn, offset, len, addr + offset, val);
  1087. switch (len) {
  1088. case 1:
  1089. out_8((u8 *)(addr + offset), val);
  1090. break;
  1091. case 2:
  1092. out_le16((u16 *)(addr + offset), val);
  1093. break;
  1094. default:
  1095. out_le32((u32 *)(addr + offset), val);
  1096. break;
  1097. }
  1098. dcr_write(port->dcrs, DCRO_PEGPL_CFG, gpl_cfg);
  1099. return PCIBIOS_SUCCESSFUL;
  1100. }
  1101. static struct pci_ops ppc4xx_pciex_pci_ops =
  1102. {
  1103. .read = ppc4xx_pciex_read_config,
  1104. .write = ppc4xx_pciex_write_config,
  1105. };
  1106. static void __init ppc4xx_configure_pciex_POMs(struct ppc4xx_pciex_port *port,
  1107. struct pci_controller *hose,
  1108. void __iomem *mbase)
  1109. {
  1110. u32 lah, lal, pciah, pcial, sa;
  1111. int i, j;
  1112. /* Setup outbound memory windows */
  1113. for (i = j = 0; i < 3; i++) {
  1114. struct resource *res = &hose->mem_resources[i];
  1115. /* we only care about memory windows */
  1116. if (!(res->flags & IORESOURCE_MEM))
  1117. continue;
  1118. if (j > 1) {
  1119. printk(KERN_WARNING "%s: Too many ranges\n",
  1120. port->node->full_name);
  1121. break;
  1122. }
  1123. /* Calculate register values */
  1124. lah = RES_TO_U32_HIGH(res->start);
  1125. lal = RES_TO_U32_LOW(res->start);
  1126. pciah = RES_TO_U32_HIGH(res->start - hose->pci_mem_offset);
  1127. pcial = RES_TO_U32_LOW(res->start - hose->pci_mem_offset);
  1128. sa = res->end + 1 - res->start;
  1129. if (!is_power_of_2(sa) || sa < 0x100000 ||
  1130. sa > 0xffffffffu) {
  1131. printk(KERN_WARNING "%s: Resource out of range\n",
  1132. port->node->full_name);
  1133. continue;
  1134. }
  1135. sa = (0xffffffffu << ilog2(sa)) | 0x1;
  1136. /* Program register values */
  1137. switch (j) {
  1138. case 0:
  1139. out_le32(mbase + PECFG_POM0LAH, pciah);
  1140. out_le32(mbase + PECFG_POM0LAL, pcial);
  1141. dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAH, lah);
  1142. dcr_write(port->dcrs, DCRO_PEGPL_OMR1BAL, lal);
  1143. dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKH, 0x7fffffff);
  1144. dcr_write(port->dcrs, DCRO_PEGPL_OMR1MSKL, sa | 3);
  1145. break;
  1146. case 1:
  1147. out_le32(mbase + PECFG_POM1LAH, pciah);
  1148. out_le32(mbase + PECFG_POM1LAL, pcial);
  1149. dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAH, lah);
  1150. dcr_write(port->dcrs, DCRO_PEGPL_OMR2BAL, lal);
  1151. dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKH, 0x7fffffff);
  1152. dcr_write(port->dcrs, DCRO_PEGPL_OMR2MSKL, sa | 3);
  1153. break;
  1154. }
  1155. j++;
  1156. }
  1157. /* Configure IO, always 64K starting at 0 */
  1158. if (hose->io_resource.flags & IORESOURCE_IO) {
  1159. lah = RES_TO_U32_HIGH(hose->io_base_phys);
  1160. lal = RES_TO_U32_LOW(hose->io_base_phys);
  1161. out_le32(mbase + PECFG_POM2LAH, 0);
  1162. out_le32(mbase + PECFG_POM2LAL, 0);
  1163. dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAH, lah);
  1164. dcr_write(port->dcrs, DCRO_PEGPL_OMR3BAL, lal);
  1165. dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKH, 0x7fffffff);
  1166. dcr_write(port->dcrs, DCRO_PEGPL_OMR3MSKL, 0xffff0000 | 3);
  1167. }
  1168. }
  1169. static void __init ppc4xx_configure_pciex_PIMs(struct ppc4xx_pciex_port *port,
  1170. struct pci_controller *hose,
  1171. void __iomem *mbase,
  1172. struct resource *res)
  1173. {
  1174. resource_size_t size = res->end - res->start + 1;
  1175. u64 sa;
  1176. /* Calculate window size */
  1177. sa = (0xffffffffffffffffull << ilog2(size));;
  1178. if (res->flags & IORESOURCE_PREFETCH)
  1179. sa |= 0x8;
  1180. out_le32(mbase + PECFG_BAR0HMPA, RES_TO_U32_HIGH(sa));
  1181. out_le32(mbase + PECFG_BAR0LMPA, RES_TO_U32_LOW(sa));
  1182. /* The setup of the split looks weird to me ... let's see if it works */
  1183. out_le32(mbase + PECFG_PIM0LAL, 0x00000000);
  1184. out_le32(mbase + PECFG_PIM0LAH, 0x00000000);
  1185. out_le32(mbase + PECFG_PIM1LAL, 0x00000000);
  1186. out_le32(mbase + PECFG_PIM1LAH, 0x00000000);
  1187. out_le32(mbase + PECFG_PIM01SAH, 0xffff0000);
  1188. out_le32(mbase + PECFG_PIM01SAL, 0x00000000);
  1189. /* Enable inbound mapping */
  1190. out_le32(mbase + PECFG_PIMEN, 0x1);
  1191. out_le32(mbase + PCI_BASE_ADDRESS_0, RES_TO_U32_LOW(res->start));
  1192. out_le32(mbase + PCI_BASE_ADDRESS_1, RES_TO_U32_HIGH(res->start));
  1193. /* Enable I/O, Mem, and Busmaster cycles */
  1194. out_le16(mbase + PCI_COMMAND,
  1195. in_le16(mbase + PCI_COMMAND) |
  1196. PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  1197. }
  1198. static void __init ppc4xx_pciex_port_setup_hose(struct ppc4xx_pciex_port *port)
  1199. {
  1200. struct resource dma_window;
  1201. struct pci_controller *hose = NULL;
  1202. const int *bus_range;
  1203. int primary = 0, busses;
  1204. void __iomem *mbase = NULL, *cfg_data = NULL;
  1205. /* XXX FIXME: Handle endpoint mode properly */
  1206. if (port->endpoint) {
  1207. printk(KERN_WARNING "PCIE%d: Port in endpoint mode !\n",
  1208. port->index);
  1209. return;
  1210. }
  1211. /* Check if primary bridge */
  1212. if (of_get_property(port->node, "primary", NULL))
  1213. primary = 1;
  1214. /* Get bus range if any */
  1215. bus_range = of_get_property(port->node, "bus-range", NULL);
  1216. /* Allocate the host controller data structure */
  1217. hose = pcibios_alloc_controller(port->node);
  1218. if (!hose)
  1219. goto fail;
  1220. /* We stick the port number in "indirect_type" so the config space
  1221. * ops can retrieve the port data structure easily
  1222. */
  1223. hose->indirect_type = port->index;
  1224. /* Get bus range */
  1225. hose->first_busno = bus_range ? bus_range[0] : 0x0;
  1226. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  1227. /* Because of how big mapping the config space is (1M per bus), we
  1228. * limit how many busses we support. In the long run, we could replace
  1229. * that with something akin to kmap_atomic instead. We set aside 1 bus
  1230. * for the host itself too.
  1231. */
  1232. busses = hose->last_busno - hose->first_busno; /* This is off by 1 */
  1233. if (busses > MAX_PCIE_BUS_MAPPED) {
  1234. busses = MAX_PCIE_BUS_MAPPED;
  1235. hose->last_busno = hose->first_busno + busses;
  1236. }
  1237. /* We map the external config space in cfg_data and the host config
  1238. * space in cfg_addr. External space is 1M per bus, internal space
  1239. * is 4K
  1240. */
  1241. cfg_data = ioremap(port->cfg_space.start +
  1242. (hose->first_busno + 1) * 0x100000,
  1243. busses * 0x100000);
  1244. mbase = ioremap(port->cfg_space.start + 0x10000000, 0x1000);
  1245. if (cfg_data == NULL || mbase == NULL) {
  1246. printk(KERN_ERR "%s: Can't map config space !",
  1247. port->node->full_name);
  1248. goto fail;
  1249. }
  1250. hose->cfg_data = cfg_data;
  1251. hose->cfg_addr = mbase;
  1252. pr_debug("PCIE %s, bus %d..%d\n", port->node->full_name,
  1253. hose->first_busno, hose->last_busno);
  1254. pr_debug(" config space mapped at: root @0x%p, other @0x%p\n",
  1255. hose->cfg_addr, hose->cfg_data);
  1256. /* Setup config space */
  1257. hose->ops = &ppc4xx_pciex_pci_ops;
  1258. port->hose = hose;
  1259. mbase = (void __iomem *)hose->cfg_addr;
  1260. /*
  1261. * Set bus numbers on our root port
  1262. */
  1263. out_8(mbase + PCI_PRIMARY_BUS, hose->first_busno);
  1264. out_8(mbase + PCI_SECONDARY_BUS, hose->first_busno + 1);
  1265. out_8(mbase + PCI_SUBORDINATE_BUS, hose->last_busno);
  1266. /*
  1267. * OMRs are already reset, also disable PIMs
  1268. */
  1269. out_le32(mbase + PECFG_PIMEN, 0);
  1270. /* Parse outbound mapping resources */
  1271. pci_process_bridge_OF_ranges(hose, port->node, primary);
  1272. /* Parse inbound mapping resources */
  1273. if (ppc4xx_parse_dma_ranges(hose, mbase, &dma_window) != 0)
  1274. goto fail;
  1275. /* Configure outbound ranges POMs */
  1276. ppc4xx_configure_pciex_POMs(port, hose, mbase);
  1277. /* Configure inbound ranges PIMs */
  1278. ppc4xx_configure_pciex_PIMs(port, hose, mbase, &dma_window);
  1279. /* The root complex doesn't show up if we don't set some vendor
  1280. * and device IDs into it. Those are the same bogus one that the
  1281. * initial code in arch/ppc add. We might want to change that.
  1282. */
  1283. out_le16(mbase + 0x200, 0xaaa0 + port->index);
  1284. out_le16(mbase + 0x202, 0xbed0 + port->index);
  1285. /* Set Class Code to PCI-PCI bridge and Revision Id to 1 */
  1286. out_le32(mbase + 0x208, 0x06040001);
  1287. printk(KERN_INFO "PCIE%d: successfully set as root-complex\n",
  1288. port->index);
  1289. return;
  1290. fail:
  1291. if (hose)
  1292. pcibios_free_controller(hose);
  1293. if (cfg_data)
  1294. iounmap(cfg_data);
  1295. if (mbase)
  1296. iounmap(mbase);
  1297. }
  1298. static void __init ppc4xx_probe_pciex_bridge(struct device_node *np)
  1299. {
  1300. struct ppc4xx_pciex_port *port;
  1301. const u32 *pval;
  1302. int portno;
  1303. unsigned int dcrs;
  1304. /* First, proceed to core initialization as we assume there's
  1305. * only one PCIe core in the system
  1306. */
  1307. if (ppc4xx_pciex_check_core_init(np))
  1308. return;
  1309. /* Get the port number from the device-tree */
  1310. pval = of_get_property(np, "port", NULL);
  1311. if (pval == NULL) {
  1312. printk(KERN_ERR "PCIE: Can't find port number for %s\n",
  1313. np->full_name);
  1314. return;
  1315. }
  1316. portno = *pval;
  1317. if (portno >= ppc4xx_pciex_port_count) {
  1318. printk(KERN_ERR "PCIE: port number out of range for %s\n",
  1319. np->full_name);
  1320. return;
  1321. }
  1322. port = &ppc4xx_pciex_ports[portno];
  1323. port->index = portno;
  1324. port->node = of_node_get(np);
  1325. pval = of_get_property(np, "sdr-base", NULL);
  1326. if (pval == NULL) {
  1327. printk(KERN_ERR "PCIE: missing sdr-base for %s\n",
  1328. np->full_name);
  1329. return;
  1330. }
  1331. port->sdr_base = *pval;
  1332. /* XXX Currently, we only support root complex mode */
  1333. port->endpoint = 0;
  1334. /* Fetch config space registers address */
  1335. if (of_address_to_resource(np, 0, &port->cfg_space)) {
  1336. printk(KERN_ERR "%s: Can't get PCI-E config space !",
  1337. np->full_name);
  1338. return;
  1339. }
  1340. /* Fetch host bridge internal registers address */
  1341. if (of_address_to_resource(np, 1, &port->utl_regs)) {
  1342. printk(KERN_ERR "%s: Can't get UTL register base !",
  1343. np->full_name);
  1344. return;
  1345. }
  1346. /* Map DCRs */
  1347. dcrs = dcr_resource_start(np, 0);
  1348. if (dcrs == 0) {
  1349. printk(KERN_ERR "%s: Can't get DCR register base !",
  1350. np->full_name);
  1351. return;
  1352. }
  1353. port->dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
  1354. /* Initialize the port specific registers */
  1355. if (ppc4xx_pciex_port_init(port)) {
  1356. printk(KERN_WARNING "PCIE%d: Port init failed\n", port->index);
  1357. return;
  1358. }
  1359. /* Setup the linux hose data structure */
  1360. ppc4xx_pciex_port_setup_hose(port);
  1361. }
  1362. #endif /* CONFIG_PPC4xx_PCI_EXPRESS */
  1363. static int __init ppc4xx_pci_find_bridges(void)
  1364. {
  1365. struct device_node *np;
  1366. #ifdef CONFIG_PPC4xx_PCI_EXPRESS
  1367. for_each_compatible_node(np, NULL, "ibm,plb-pciex")
  1368. ppc4xx_probe_pciex_bridge(np);
  1369. #endif
  1370. for_each_compatible_node(np, NULL, "ibm,plb-pcix")
  1371. ppc4xx_probe_pcix_bridge(np);
  1372. for_each_compatible_node(np, NULL, "ibm,plb-pci")
  1373. ppc4xx_probe_pci_bridge(np);
  1374. return 0;
  1375. }
  1376. arch_initcall(ppc4xx_pci_find_bridges);