pci.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * MPC86XX pci setup code
  3. *
  4. * Recode: ZHANG WEI <wei.zhang@freescale.com>
  5. * Initial author: Xianghua Xiao <x.xiao@freescale.com>
  6. *
  7. * Copyright 2006 Freescale Semiconductor Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/pci.h>
  18. #include <linux/serial.h>
  19. #include <asm/system.h>
  20. #include <asm/atomic.h>
  21. #include <asm/io.h>
  22. #include <asm/prom.h>
  23. #include <asm/pci-bridge.h>
  24. #include <sysdev/fsl_soc.h>
  25. #include <sysdev/fsl_pcie.h>
  26. #include "mpc86xx.h"
  27. #undef DEBUG
  28. #ifdef DEBUG
  29. #define DBG(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ## args)
  30. #else
  31. #define DBG(fmt, args...)
  32. #endif
  33. struct pcie_outbound_window_regs {
  34. uint pexotar; /* 0x.0 - PCI Express outbound translation address register */
  35. uint pexotear; /* 0x.4 - PCI Express outbound translation extended address register */
  36. uint pexowbar; /* 0x.8 - PCI Express outbound window base address register */
  37. char res1[4];
  38. uint pexowar; /* 0x.10 - PCI Express outbound window attributes register */
  39. char res2[12];
  40. };
  41. struct pcie_inbound_window_regs {
  42. uint pexitar; /* 0x.0 - PCI Express inbound translation address register */
  43. char res1[4];
  44. uint pexiwbar; /* 0x.8 - PCI Express inbound window base address register */
  45. uint pexiwbear; /* 0x.c - PCI Express inbound window base extended address register */
  46. uint pexiwar; /* 0x.10 - PCI Express inbound window attributes register */
  47. char res2[12];
  48. };
  49. static void __init setup_pcie_atmu(struct pci_controller *hose, struct resource *rsrc)
  50. {
  51. volatile struct ccsr_pex *pcie;
  52. volatile struct pcie_outbound_window_regs *pcieow;
  53. volatile struct pcie_inbound_window_regs *pcieiw;
  54. int i = 0;
  55. DBG("PCIE memory map start 0x%x, size 0x%x\n", rsrc->start,
  56. rsrc->end - rsrc->start + 1);
  57. pcie = ioremap(rsrc->start, rsrc->end - rsrc->start + 1);
  58. /* Disable all windows (except pexowar0 since its ignored) */
  59. pcie->pexowar1 = 0;
  60. pcie->pexowar2 = 0;
  61. pcie->pexowar3 = 0;
  62. pcie->pexowar4 = 0;
  63. pcie->pexiwar1 = 0;
  64. pcie->pexiwar2 = 0;
  65. pcie->pexiwar3 = 0;
  66. pcieow = (struct pcie_outbound_window_regs *)&pcie->pexotar1;
  67. pcieiw = (struct pcie_inbound_window_regs *)&pcie->pexitar1;
  68. /* Setup outbound MEM window */
  69. for(i = 0; i < 3; i++)
  70. if (hose->mem_resources[i].flags & IORESOURCE_MEM){
  71. DBG("PCIE MEM resource start 0x%08x, size 0x%08x.\n",
  72. hose->mem_resources[i].start,
  73. hose->mem_resources[i].end
  74. - hose->mem_resources[i].start + 1);
  75. pcieow->pexotar = (hose->mem_resources[i].start) >> 12
  76. & 0x000fffff;
  77. pcieow->pexotear = 0;
  78. pcieow->pexowbar = (hose->mem_resources[i].start) >> 12
  79. & 0x000fffff;
  80. /* Enable, Mem R/W */
  81. pcieow->pexowar = 0x80044000 |
  82. (__ilog2(hose->mem_resources[i].end
  83. - hose->mem_resources[i].start + 1)
  84. - 1);
  85. pcieow++;
  86. }
  87. /* Setup outbound IO window */
  88. if (hose->io_resource.flags & IORESOURCE_IO){
  89. DBG("PCIE IO resource start 0x%08x, size 0x%08x, phy base 0x%08x.\n",
  90. hose->io_resource.start,
  91. hose->io_resource.end - hose->io_resource.start + 1,
  92. hose->io_base_phys);
  93. pcieow->pexotar = (hose->io_resource.start) >> 12 & 0x000fffff;
  94. pcieow->pexotear = 0;
  95. pcieow->pexowbar = (hose->io_base_phys) >> 12 & 0x000fffff;
  96. /* Enable, IO R/W */
  97. pcieow->pexowar = 0x80088000 | (__ilog2(hose->io_resource.end
  98. - hose->io_resource.start + 1) - 1);
  99. }
  100. /* Setup 2G inbound Memory Window @ 0 */
  101. pcieiw->pexitar = 0x00000000;
  102. pcieiw->pexiwbar = 0x00000000;
  103. /* Enable, Prefetch, Local Mem, Snoop R/W, 2G */
  104. pcieiw->pexiwar = 0xa0f5501e;
  105. }
  106. static void __init
  107. mpc86xx_setup_pcie(struct pci_controller *hose, u32 pcie_offset, u32 pcie_size)
  108. {
  109. u16 cmd;
  110. DBG("PCIE host controller register offset 0x%08x, size 0x%08x.\n",
  111. pcie_offset, pcie_size);
  112. early_read_config_word(hose, 0, 0, PCI_COMMAND, &cmd);
  113. cmd |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY
  114. | PCI_COMMAND_IO;
  115. early_write_config_word(hose, 0, 0, PCI_COMMAND, cmd);
  116. early_write_config_byte(hose, 0, 0, PCI_LATENCY_TIMER, 0x80);
  117. }
  118. static void __devinit quirk_fsl_pcie_transparent(struct pci_dev *dev)
  119. {
  120. struct resource *res;
  121. int i, res_idx = PCI_BRIDGE_RESOURCES;
  122. struct pci_controller *hose;
  123. /*
  124. * Make the bridge be transparent.
  125. */
  126. dev->transparent = 1;
  127. hose = pci_bus_to_host(dev->bus);
  128. if (!hose) {
  129. printk(KERN_ERR "Can't find hose for bus %d\n",
  130. dev->bus->number);
  131. return;
  132. }
  133. if (hose->io_resource.flags) {
  134. res = &dev->resource[res_idx++];
  135. res->start = hose->io_resource.start;
  136. res->end = hose->io_resource.end;
  137. res->flags = hose->io_resource.flags;
  138. }
  139. for (i = 0; i < 3; i++) {
  140. res = &dev->resource[res_idx + i];
  141. res->start = hose->mem_resources[i].start;
  142. res->end = hose->mem_resources[i].end;
  143. res->flags = hose->mem_resources[i].flags;
  144. }
  145. }
  146. DECLARE_PCI_FIXUP_EARLY(0x1957, 0x7010, quirk_fsl_pcie_transparent);
  147. DECLARE_PCI_FIXUP_EARLY(0x1957, 0x7011, quirk_fsl_pcie_transparent);
  148. #define PCIE_LTSSM 0x404 /* PCIe Link Training and Status */
  149. #define PCIE_LTSSM_L0 0x16 /* L0 state */
  150. int __init mpc86xx_add_bridge(struct device_node *dev)
  151. {
  152. int len;
  153. struct pci_controller *hose;
  154. struct resource rsrc;
  155. const int *bus_range;
  156. int has_address = 0;
  157. int primary = 0;
  158. u16 val;
  159. DBG("Adding PCIE host bridge %s\n", dev->full_name);
  160. /* Fetch host bridge registers address */
  161. has_address = (of_address_to_resource(dev, 0, &rsrc) == 0);
  162. /* Get bus range if any */
  163. bus_range = of_get_property(dev, "bus-range", &len);
  164. if (bus_range == NULL || len < 2 * sizeof(int))
  165. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  166. " bus 0\n", dev->full_name);
  167. pci_assign_all_buses = 1;
  168. hose = pcibios_alloc_controller(dev);
  169. if (!hose)
  170. return -ENOMEM;
  171. hose->indirect_type = PPC_INDIRECT_TYPE_EXT_REG |
  172. PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS;
  173. hose->first_busno = bus_range ? bus_range[0] : 0x0;
  174. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  175. setup_indirect_pci(hose, rsrc.start, rsrc.start + 0x4);
  176. /* Probe the hose link training status */
  177. early_read_config_word(hose, 0, 0, PCIE_LTSSM, &val);
  178. if (val < PCIE_LTSSM_L0)
  179. return -ENXIO;
  180. /* Setup the PCIE host controller. */
  181. mpc86xx_setup_pcie(hose, rsrc.start, rsrc.end - rsrc.start + 1);
  182. if ((rsrc.start & 0xfffff) == 0x8000)
  183. primary = 1;
  184. printk(KERN_INFO "Found MPC86xx PCIE host bridge at 0x%08lx. "
  185. "Firmware bus number: %d->%d\n",
  186. (unsigned long) rsrc.start,
  187. hose->first_busno, hose->last_busno);
  188. DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  189. hose, hose->cfg_addr, hose->cfg_data);
  190. /* Interpret the "ranges" property */
  191. /* This also maps the I/O region and sets isa_io/mem_base */
  192. pci_process_bridge_OF_ranges(hose, dev, primary);
  193. /* Setup PEX window registers */
  194. setup_pcie_atmu(hose, &rsrc);
  195. return 0;
  196. }