ppc4xx_pci.c 41 KB

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