ppc4xx_pci.c 41 KB

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