fsl_pci.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * MPC83xx/85xx/86xx PCI/PCIE support routing.
  3. *
  4. * Copyright 2007-2011 Freescale Semiconductor, Inc.
  5. * Copyright 2008-2009 MontaVista Software, Inc.
  6. *
  7. * Initial author: Xianghua Xiao <x.xiao@freescale.com>
  8. * Recode: ZHANG WEI <wei.zhang@freescale.com>
  9. * Rewrite the routing for Frescale PCI and PCI Express
  10. * Roy Zang <tie-fei.zang@freescale.com>
  11. * MPC83xx PCI-Express support:
  12. * Tony Li <tony.li@freescale.com>
  13. * Anton Vorontsov <avorontsov@ru.mvista.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation; either version 2 of the License, or (at your
  18. * option) any later version.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/pci.h>
  22. #include <linux/delay.h>
  23. #include <linux/string.h>
  24. #include <linux/init.h>
  25. #include <linux/bootmem.h>
  26. #include <linux/memblock.h>
  27. #include <linux/log2.h>
  28. #include <linux/slab.h>
  29. #include <asm/io.h>
  30. #include <asm/prom.h>
  31. #include <asm/pci-bridge.h>
  32. #include <asm/machdep.h>
  33. #include <sysdev/fsl_soc.h>
  34. #include <sysdev/fsl_pci.h>
  35. static int fsl_pcie_bus_fixup, is_mpc83xx_pci;
  36. static void __init quirk_fsl_pcie_header(struct pci_dev *dev)
  37. {
  38. u8 progif;
  39. /* if we aren't a PCIe don't bother */
  40. if (!pci_find_capability(dev, PCI_CAP_ID_EXP))
  41. return;
  42. /* if we aren't in host mode don't bother */
  43. pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
  44. if (progif & 0x1)
  45. return;
  46. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  47. fsl_pcie_bus_fixup = 1;
  48. return;
  49. }
  50. static int __init fsl_pcie_check_link(struct pci_controller *hose)
  51. {
  52. u32 val;
  53. early_read_config_dword(hose, 0, 0, PCIE_LTSSM, &val);
  54. if (val < PCIE_LTSSM_L0)
  55. return 1;
  56. return 0;
  57. }
  58. #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
  59. static int __init setup_one_atmu(struct ccsr_pci __iomem *pci,
  60. unsigned int index, const struct resource *res,
  61. resource_size_t offset)
  62. {
  63. resource_size_t pci_addr = res->start - offset;
  64. resource_size_t phys_addr = res->start;
  65. resource_size_t size = resource_size(res);
  66. u32 flags = 0x80044000; /* enable & mem R/W */
  67. unsigned int i;
  68. pr_debug("PCI MEM resource start 0x%016llx, size 0x%016llx.\n",
  69. (u64)res->start, (u64)size);
  70. if (res->flags & IORESOURCE_PREFETCH)
  71. flags |= 0x10000000; /* enable relaxed ordering */
  72. for (i = 0; size > 0; i++) {
  73. unsigned int bits = min(__ilog2(size),
  74. __ffs(pci_addr | phys_addr));
  75. if (index + i >= 5)
  76. return -1;
  77. out_be32(&pci->pow[index + i].potar, pci_addr >> 12);
  78. out_be32(&pci->pow[index + i].potear, (u64)pci_addr >> 44);
  79. out_be32(&pci->pow[index + i].powbar, phys_addr >> 12);
  80. out_be32(&pci->pow[index + i].powar, flags | (bits - 1));
  81. pci_addr += (resource_size_t)1U << bits;
  82. phys_addr += (resource_size_t)1U << bits;
  83. size -= (resource_size_t)1U << bits;
  84. }
  85. return i;
  86. }
  87. /* atmu setup for fsl pci/pcie controller */
  88. static void __init setup_pci_atmu(struct pci_controller *hose,
  89. struct resource *rsrc)
  90. {
  91. struct ccsr_pci __iomem *pci;
  92. int i, j, n, mem_log, win_idx = 3, start_idx = 1, end_idx = 4;
  93. u64 mem, sz, paddr_hi = 0;
  94. u64 paddr_lo = ULLONG_MAX;
  95. u32 pcicsrbar = 0, pcicsrbar_sz;
  96. u32 piwar = PIWAR_EN | PIWAR_PF | PIWAR_TGI_LOCAL |
  97. PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
  98. char *name = hose->dn->full_name;
  99. pr_debug("PCI memory map start 0x%016llx, size 0x%016llx\n",
  100. (u64)rsrc->start, (u64)resource_size(rsrc));
  101. if (of_device_is_compatible(hose->dn, "fsl,qoriq-pcie-v2.2")) {
  102. win_idx = 2;
  103. start_idx = 0;
  104. end_idx = 3;
  105. }
  106. pci = ioremap(rsrc->start, resource_size(rsrc));
  107. if (!pci) {
  108. dev_err(hose->parent, "Unable to map ATMU registers\n");
  109. return;
  110. }
  111. /* Disable all windows (except powar0 since it's ignored) */
  112. for(i = 1; i < 5; i++)
  113. out_be32(&pci->pow[i].powar, 0);
  114. for (i = start_idx; i < end_idx; i++)
  115. out_be32(&pci->piw[i].piwar, 0);
  116. /* Setup outbound MEM window */
  117. for(i = 0, j = 1; i < 3; i++) {
  118. if (!(hose->mem_resources[i].flags & IORESOURCE_MEM))
  119. continue;
  120. paddr_lo = min(paddr_lo, (u64)hose->mem_resources[i].start);
  121. paddr_hi = max(paddr_hi, (u64)hose->mem_resources[i].end);
  122. n = setup_one_atmu(pci, j, &hose->mem_resources[i],
  123. hose->pci_mem_offset);
  124. if (n < 0 || j >= 5) {
  125. pr_err("Ran out of outbound PCI ATMUs for resource %d!\n", i);
  126. hose->mem_resources[i].flags |= IORESOURCE_DISABLED;
  127. } else
  128. j += n;
  129. }
  130. /* Setup outbound IO window */
  131. if (hose->io_resource.flags & IORESOURCE_IO) {
  132. if (j >= 5) {
  133. pr_err("Ran out of outbound PCI ATMUs for IO resource\n");
  134. } else {
  135. pr_debug("PCI IO resource start 0x%016llx, size 0x%016llx, "
  136. "phy base 0x%016llx.\n",
  137. (u64)hose->io_resource.start,
  138. (u64)resource_size(&hose->io_resource),
  139. (u64)hose->io_base_phys);
  140. out_be32(&pci->pow[j].potar, (hose->io_resource.start >> 12));
  141. out_be32(&pci->pow[j].potear, 0);
  142. out_be32(&pci->pow[j].powbar, (hose->io_base_phys >> 12));
  143. /* Enable, IO R/W */
  144. out_be32(&pci->pow[j].powar, 0x80088000
  145. | (__ilog2(hose->io_resource.end
  146. - hose->io_resource.start + 1) - 1));
  147. }
  148. }
  149. /* convert to pci address space */
  150. paddr_hi -= hose->pci_mem_offset;
  151. paddr_lo -= hose->pci_mem_offset;
  152. if (paddr_hi == paddr_lo) {
  153. pr_err("%s: No outbound window space\n", name);
  154. return ;
  155. }
  156. if (paddr_lo == 0) {
  157. pr_err("%s: No space for inbound window\n", name);
  158. return ;
  159. }
  160. /* setup PCSRBAR/PEXCSRBAR */
  161. early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, 0xffffffff);
  162. early_read_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, &pcicsrbar_sz);
  163. pcicsrbar_sz = ~pcicsrbar_sz + 1;
  164. if (paddr_hi < (0x100000000ull - pcicsrbar_sz) ||
  165. (paddr_lo > 0x100000000ull))
  166. pcicsrbar = 0x100000000ull - pcicsrbar_sz;
  167. else
  168. pcicsrbar = (paddr_lo - pcicsrbar_sz) & -pcicsrbar_sz;
  169. early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, pcicsrbar);
  170. paddr_lo = min(paddr_lo, (u64)pcicsrbar);
  171. pr_info("%s: PCICSRBAR @ 0x%x\n", name, pcicsrbar);
  172. /* Setup inbound mem window */
  173. mem = memblock_end_of_DRAM();
  174. sz = min(mem, paddr_lo);
  175. mem_log = __ilog2_u64(sz);
  176. /* PCIe can overmap inbound & outbound since RX & TX are separated */
  177. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  178. /* Size window to exact size if power-of-two or one size up */
  179. if ((1ull << mem_log) != mem) {
  180. if ((1ull << mem_log) > mem)
  181. pr_info("%s: Setting PCI inbound window "
  182. "greater than memory size\n", name);
  183. mem_log++;
  184. }
  185. piwar |= ((mem_log - 1) & PIWAR_SZ_MASK);
  186. /* Setup inbound memory window */
  187. out_be32(&pci->piw[win_idx].pitar, 0x00000000);
  188. out_be32(&pci->piw[win_idx].piwbar, 0x00000000);
  189. out_be32(&pci->piw[win_idx].piwar, piwar);
  190. win_idx--;
  191. hose->dma_window_base_cur = 0x00000000;
  192. hose->dma_window_size = (resource_size_t)sz;
  193. } else {
  194. u64 paddr = 0;
  195. /* Setup inbound memory window */
  196. out_be32(&pci->piw[win_idx].pitar, paddr >> 12);
  197. out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
  198. out_be32(&pci->piw[win_idx].piwar, (piwar | (mem_log - 1)));
  199. win_idx--;
  200. paddr += 1ull << mem_log;
  201. sz -= 1ull << mem_log;
  202. if (sz) {
  203. mem_log = __ilog2_u64(sz);
  204. piwar |= (mem_log - 1);
  205. out_be32(&pci->piw[win_idx].pitar, paddr >> 12);
  206. out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
  207. out_be32(&pci->piw[win_idx].piwar, piwar);
  208. win_idx--;
  209. paddr += 1ull << mem_log;
  210. }
  211. hose->dma_window_base_cur = 0x00000000;
  212. hose->dma_window_size = (resource_size_t)paddr;
  213. }
  214. if (hose->dma_window_size < mem) {
  215. #ifndef CONFIG_SWIOTLB
  216. pr_err("%s: ERROR: Memory size exceeds PCI ATMU ability to "
  217. "map - enable CONFIG_SWIOTLB to avoid dma errors.\n",
  218. name);
  219. #endif
  220. /* adjusting outbound windows could reclaim space in mem map */
  221. if (paddr_hi < 0xffffffffull)
  222. pr_warning("%s: WARNING: Outbound window cfg leaves "
  223. "gaps in memory map. Adjusting the memory map "
  224. "could reduce unnecessary bounce buffering.\n",
  225. name);
  226. pr_info("%s: DMA window size is 0x%llx\n", name,
  227. (u64)hose->dma_window_size);
  228. }
  229. iounmap(pci);
  230. }
  231. static void __init setup_pci_cmd(struct pci_controller *hose)
  232. {
  233. u16 cmd;
  234. int cap_x;
  235. early_read_config_word(hose, 0, 0, PCI_COMMAND, &cmd);
  236. cmd |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY
  237. | PCI_COMMAND_IO;
  238. early_write_config_word(hose, 0, 0, PCI_COMMAND, cmd);
  239. cap_x = early_find_capability(hose, 0, 0, PCI_CAP_ID_PCIX);
  240. if (cap_x) {
  241. int pci_x_cmd = cap_x + PCI_X_CMD;
  242. cmd = PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ
  243. | PCI_X_CMD_ERO | PCI_X_CMD_DPERR_E;
  244. early_write_config_word(hose, 0, 0, pci_x_cmd, cmd);
  245. } else {
  246. early_write_config_byte(hose, 0, 0, PCI_LATENCY_TIMER, 0x80);
  247. }
  248. }
  249. void fsl_pcibios_fixup_bus(struct pci_bus *bus)
  250. {
  251. struct pci_controller *hose = pci_bus_to_host(bus);
  252. int i;
  253. if ((bus->parent == hose->bus) &&
  254. ((fsl_pcie_bus_fixup &&
  255. early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) ||
  256. (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)))
  257. {
  258. for (i = 0; i < 4; ++i) {
  259. struct resource *res = bus->resource[i];
  260. struct resource *par = bus->parent->resource[i];
  261. if (res) {
  262. res->start = 0;
  263. res->end = 0;
  264. res->flags = 0;
  265. }
  266. if (res && par) {
  267. res->start = par->start;
  268. res->end = par->end;
  269. res->flags = par->flags;
  270. }
  271. }
  272. }
  273. }
  274. int __init fsl_add_bridge(struct device_node *dev, int is_primary)
  275. {
  276. int len;
  277. struct pci_controller *hose;
  278. struct resource rsrc;
  279. const int *bus_range;
  280. u8 progif;
  281. if (!of_device_is_available(dev)) {
  282. pr_warning("%s: disabled\n", dev->full_name);
  283. return -ENODEV;
  284. }
  285. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  286. /* Fetch host bridge registers address */
  287. if (of_address_to_resource(dev, 0, &rsrc)) {
  288. printk(KERN_WARNING "Can't get pci register base!");
  289. return -ENOMEM;
  290. }
  291. /* Get bus range if any */
  292. bus_range = of_get_property(dev, "bus-range", &len);
  293. if (bus_range == NULL || len < 2 * sizeof(int))
  294. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  295. " bus 0\n", dev->full_name);
  296. pci_add_flags(PCI_REASSIGN_ALL_BUS);
  297. hose = pcibios_alloc_controller(dev);
  298. if (!hose)
  299. return -ENOMEM;
  300. hose->first_busno = bus_range ? bus_range[0] : 0x0;
  301. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  302. setup_indirect_pci(hose, rsrc.start, rsrc.start + 0x4,
  303. PPC_INDIRECT_TYPE_BIG_ENDIAN);
  304. early_read_config_byte(hose, 0, 0, PCI_CLASS_PROG, &progif);
  305. if ((progif & 1) == 1) {
  306. /* unmap cfg_data & cfg_addr separately if not on same page */
  307. if (((unsigned long)hose->cfg_data & PAGE_MASK) !=
  308. ((unsigned long)hose->cfg_addr & PAGE_MASK))
  309. iounmap(hose->cfg_data);
  310. iounmap(hose->cfg_addr);
  311. pcibios_free_controller(hose);
  312. return 0;
  313. }
  314. setup_pci_cmd(hose);
  315. /* check PCI express link status */
  316. if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
  317. hose->indirect_type |= PPC_INDIRECT_TYPE_EXT_REG |
  318. PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS;
  319. if (fsl_pcie_check_link(hose))
  320. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  321. }
  322. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  323. "Firmware bus number: %d->%d\n",
  324. (unsigned long long)rsrc.start, hose->first_busno,
  325. hose->last_busno);
  326. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  327. hose, hose->cfg_addr, hose->cfg_data);
  328. /* Interpret the "ranges" property */
  329. /* This also maps the I/O region and sets isa_io/mem_base */
  330. pci_process_bridge_OF_ranges(hose, dev, is_primary);
  331. /* Setup PEX window registers */
  332. setup_pci_atmu(hose, &rsrc);
  333. return 0;
  334. }
  335. #endif /* CONFIG_FSL_SOC_BOOKE || CONFIG_PPC_86xx */
  336. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, quirk_fsl_pcie_header);
  337. #if defined(CONFIG_PPC_83xx) || defined(CONFIG_PPC_MPC512x)
  338. struct mpc83xx_pcie_priv {
  339. void __iomem *cfg_type0;
  340. void __iomem *cfg_type1;
  341. u32 dev_base;
  342. };
  343. struct pex_inbound_window {
  344. u32 ar;
  345. u32 tar;
  346. u32 barl;
  347. u32 barh;
  348. };
  349. /*
  350. * With the convention of u-boot, the PCIE outbound window 0 serves
  351. * as configuration transactions outbound.
  352. */
  353. #define PEX_OUTWIN0_BAR 0xCA4
  354. #define PEX_OUTWIN0_TAL 0xCA8
  355. #define PEX_OUTWIN0_TAH 0xCAC
  356. #define PEX_RC_INWIN_BASE 0xE60
  357. #define PEX_RCIWARn_EN 0x1
  358. static int mpc83xx_pcie_exclude_device(struct pci_bus *bus, unsigned int devfn)
  359. {
  360. struct pci_controller *hose = pci_bus_to_host(bus);
  361. if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)
  362. return PCIBIOS_DEVICE_NOT_FOUND;
  363. /*
  364. * Workaround for the HW bug: for Type 0 configure transactions the
  365. * PCI-E controller does not check the device number bits and just
  366. * assumes that the device number bits are 0.
  367. */
  368. if (bus->number == hose->first_busno ||
  369. bus->primary == hose->first_busno) {
  370. if (devfn & 0xf8)
  371. return PCIBIOS_DEVICE_NOT_FOUND;
  372. }
  373. if (ppc_md.pci_exclude_device) {
  374. if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
  375. return PCIBIOS_DEVICE_NOT_FOUND;
  376. }
  377. return PCIBIOS_SUCCESSFUL;
  378. }
  379. static void __iomem *mpc83xx_pcie_remap_cfg(struct pci_bus *bus,
  380. unsigned int devfn, int offset)
  381. {
  382. struct pci_controller *hose = pci_bus_to_host(bus);
  383. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  384. u32 dev_base = bus->number << 24 | devfn << 16;
  385. int ret;
  386. ret = mpc83xx_pcie_exclude_device(bus, devfn);
  387. if (ret)
  388. return NULL;
  389. offset &= 0xfff;
  390. /* Type 0 */
  391. if (bus->number == hose->first_busno)
  392. return pcie->cfg_type0 + offset;
  393. if (pcie->dev_base == dev_base)
  394. goto mapped;
  395. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, dev_base);
  396. pcie->dev_base = dev_base;
  397. mapped:
  398. return pcie->cfg_type1 + offset;
  399. }
  400. static int mpc83xx_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
  401. int offset, int len, u32 *val)
  402. {
  403. void __iomem *cfg_addr;
  404. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  405. if (!cfg_addr)
  406. return PCIBIOS_DEVICE_NOT_FOUND;
  407. switch (len) {
  408. case 1:
  409. *val = in_8(cfg_addr);
  410. break;
  411. case 2:
  412. *val = in_le16(cfg_addr);
  413. break;
  414. default:
  415. *val = in_le32(cfg_addr);
  416. break;
  417. }
  418. return PCIBIOS_SUCCESSFUL;
  419. }
  420. static int mpc83xx_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
  421. int offset, int len, u32 val)
  422. {
  423. struct pci_controller *hose = pci_bus_to_host(bus);
  424. void __iomem *cfg_addr;
  425. cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
  426. if (!cfg_addr)
  427. return PCIBIOS_DEVICE_NOT_FOUND;
  428. /* PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS */
  429. if (offset == PCI_PRIMARY_BUS && bus->number == hose->first_busno)
  430. val &= 0xffffff00;
  431. switch (len) {
  432. case 1:
  433. out_8(cfg_addr, val);
  434. break;
  435. case 2:
  436. out_le16(cfg_addr, val);
  437. break;
  438. default:
  439. out_le32(cfg_addr, val);
  440. break;
  441. }
  442. return PCIBIOS_SUCCESSFUL;
  443. }
  444. static struct pci_ops mpc83xx_pcie_ops = {
  445. .read = mpc83xx_pcie_read_config,
  446. .write = mpc83xx_pcie_write_config,
  447. };
  448. static int __init mpc83xx_pcie_setup(struct pci_controller *hose,
  449. struct resource *reg)
  450. {
  451. struct mpc83xx_pcie_priv *pcie;
  452. u32 cfg_bar;
  453. int ret = -ENOMEM;
  454. pcie = zalloc_maybe_bootmem(sizeof(*pcie), GFP_KERNEL);
  455. if (!pcie)
  456. return ret;
  457. pcie->cfg_type0 = ioremap(reg->start, resource_size(reg));
  458. if (!pcie->cfg_type0)
  459. goto err0;
  460. cfg_bar = in_le32(pcie->cfg_type0 + PEX_OUTWIN0_BAR);
  461. if (!cfg_bar) {
  462. /* PCI-E isn't configured. */
  463. ret = -ENODEV;
  464. goto err1;
  465. }
  466. pcie->cfg_type1 = ioremap(cfg_bar, 0x1000);
  467. if (!pcie->cfg_type1)
  468. goto err1;
  469. WARN_ON(hose->dn->data);
  470. hose->dn->data = pcie;
  471. hose->ops = &mpc83xx_pcie_ops;
  472. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAH, 0);
  473. out_le32(pcie->cfg_type0 + PEX_OUTWIN0_TAL, 0);
  474. if (fsl_pcie_check_link(hose))
  475. hose->indirect_type |= PPC_INDIRECT_TYPE_NO_PCIE_LINK;
  476. return 0;
  477. err1:
  478. iounmap(pcie->cfg_type0);
  479. err0:
  480. kfree(pcie);
  481. return ret;
  482. }
  483. int __init mpc83xx_add_bridge(struct device_node *dev)
  484. {
  485. int ret;
  486. int len;
  487. struct pci_controller *hose;
  488. struct resource rsrc_reg;
  489. struct resource rsrc_cfg;
  490. const int *bus_range;
  491. int primary;
  492. is_mpc83xx_pci = 1;
  493. if (!of_device_is_available(dev)) {
  494. pr_warning("%s: disabled by the firmware.\n",
  495. dev->full_name);
  496. return -ENODEV;
  497. }
  498. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  499. /* Fetch host bridge registers address */
  500. if (of_address_to_resource(dev, 0, &rsrc_reg)) {
  501. printk(KERN_WARNING "Can't get pci register base!\n");
  502. return -ENOMEM;
  503. }
  504. memset(&rsrc_cfg, 0, sizeof(rsrc_cfg));
  505. if (of_address_to_resource(dev, 1, &rsrc_cfg)) {
  506. printk(KERN_WARNING
  507. "No pci config register base in dev tree, "
  508. "using default\n");
  509. /*
  510. * MPC83xx supports up to two host controllers
  511. * one at 0x8500 has config space registers at 0x8300
  512. * one at 0x8600 has config space registers at 0x8380
  513. */
  514. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  515. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8300;
  516. else if ((rsrc_reg.start & 0xfffff) == 0x8600)
  517. rsrc_cfg.start = (rsrc_reg.start & 0xfff00000) + 0x8380;
  518. }
  519. /*
  520. * Controller at offset 0x8500 is primary
  521. */
  522. if ((rsrc_reg.start & 0xfffff) == 0x8500)
  523. primary = 1;
  524. else
  525. primary = 0;
  526. /* Get bus range if any */
  527. bus_range = of_get_property(dev, "bus-range", &len);
  528. if (bus_range == NULL || len < 2 * sizeof(int)) {
  529. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  530. " bus 0\n", dev->full_name);
  531. }
  532. pci_add_flags(PCI_REASSIGN_ALL_BUS);
  533. hose = pcibios_alloc_controller(dev);
  534. if (!hose)
  535. return -ENOMEM;
  536. hose->first_busno = bus_range ? bus_range[0] : 0;
  537. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  538. if (of_device_is_compatible(dev, "fsl,mpc8314-pcie")) {
  539. ret = mpc83xx_pcie_setup(hose, &rsrc_reg);
  540. if (ret)
  541. goto err0;
  542. } else {
  543. setup_indirect_pci(hose, rsrc_cfg.start,
  544. rsrc_cfg.start + 4, 0);
  545. }
  546. printk(KERN_INFO "Found FSL PCI host bridge at 0x%016llx. "
  547. "Firmware bus number: %d->%d\n",
  548. (unsigned long long)rsrc_reg.start, hose->first_busno,
  549. hose->last_busno);
  550. pr_debug(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  551. hose, hose->cfg_addr, hose->cfg_data);
  552. /* Interpret the "ranges" property */
  553. /* This also maps the I/O region and sets isa_io/mem_base */
  554. pci_process_bridge_OF_ranges(hose, dev, primary);
  555. return 0;
  556. err0:
  557. pcibios_free_controller(hose);
  558. return ret;
  559. }
  560. #endif /* CONFIG_PPC_83xx */
  561. u64 fsl_pci_immrbar_base(struct pci_controller *hose)
  562. {
  563. #ifdef CONFIG_PPC_83xx
  564. if (is_mpc83xx_pci) {
  565. struct mpc83xx_pcie_priv *pcie = hose->dn->data;
  566. struct pex_inbound_window *in;
  567. int i;
  568. /* Walk the Root Complex Inbound windows to match IMMR base */
  569. in = pcie->cfg_type0 + PEX_RC_INWIN_BASE;
  570. for (i = 0; i < 4; i++) {
  571. /* not enabled, skip */
  572. if (!in_le32(&in[i].ar) & PEX_RCIWARn_EN)
  573. continue;
  574. if (get_immrbase() == in_le32(&in[i].tar))
  575. return (u64)in_le32(&in[i].barh) << 32 |
  576. in_le32(&in[i].barl);
  577. }
  578. printk(KERN_WARNING "could not find PCI BAR matching IMMR\n");
  579. }
  580. #endif
  581. #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
  582. if (!is_mpc83xx_pci) {
  583. u32 base;
  584. pci_bus_read_config_dword(hose->bus,
  585. PCI_DEVFN(0, 0), PCI_BASE_ADDRESS_0, &base);
  586. return base;
  587. }
  588. #endif
  589. return 0;
  590. }