ppc4xx_pci.c 40 KB

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