ppc4xx_pci.c 56 KB

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