ppc4xx_pci.c 43 KB

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