intel_mid_pci.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Intel MID PCI support
  3. * Copyright (c) 2008 Intel Corporation
  4. * Jesse Barnes <jesse.barnes@intel.com>
  5. *
  6. * Moorestown has an interesting PCI implementation:
  7. * - configuration space is memory mapped (as defined by MCFG)
  8. * - Lincroft devices also have a real, type 1 configuration space
  9. * - Early Lincroft silicon has a type 1 access bug that will cause
  10. * a hang if non-existent devices are accessed
  11. * - some devices have the "fixed BAR" capability, which means
  12. * they can't be relocated or modified; check for that during
  13. * BAR sizing
  14. *
  15. * So, we use the MCFG space for all reads and writes, but also send
  16. * Lincroft writes to type 1 space. But only read/write if the device
  17. * actually exists, otherwise return all 1s for reads and bit bucket
  18. * the writes.
  19. */
  20. #include <linux/sched.h>
  21. #include <linux/pci.h>
  22. #include <linux/ioport.h>
  23. #include <linux/init.h>
  24. #include <linux/dmi.h>
  25. #include <linux/acpi.h>
  26. #include <linux/io.h>
  27. #include <linux/smp.h>
  28. #include <asm/segment.h>
  29. #include <asm/pci_x86.h>
  30. #include <asm/hw_irq.h>
  31. #include <asm/io_apic.h>
  32. #include <asm/intel-mid.h>
  33. #define PCIE_CAP_OFFSET 0x100
  34. /* Fixed BAR fields */
  35. #define PCIE_VNDR_CAP_ID_FIXED_BAR 0x00 /* Fixed BAR (TBD) */
  36. #define PCI_FIXED_BAR_0_SIZE 0x04
  37. #define PCI_FIXED_BAR_1_SIZE 0x08
  38. #define PCI_FIXED_BAR_2_SIZE 0x0c
  39. #define PCI_FIXED_BAR_3_SIZE 0x10
  40. #define PCI_FIXED_BAR_4_SIZE 0x14
  41. #define PCI_FIXED_BAR_5_SIZE 0x1c
  42. static int pci_soc_mode;
  43. /**
  44. * fixed_bar_cap - return the offset of the fixed BAR cap if found
  45. * @bus: PCI bus
  46. * @devfn: device in question
  47. *
  48. * Look for the fixed BAR cap on @bus and @devfn, returning its offset
  49. * if found or 0 otherwise.
  50. */
  51. static int fixed_bar_cap(struct pci_bus *bus, unsigned int devfn)
  52. {
  53. int pos;
  54. u32 pcie_cap = 0, cap_data;
  55. pos = PCIE_CAP_OFFSET;
  56. if (!raw_pci_ext_ops)
  57. return 0;
  58. while (pos) {
  59. if (raw_pci_ext_ops->read(pci_domain_nr(bus), bus->number,
  60. devfn, pos, 4, &pcie_cap))
  61. return 0;
  62. if (PCI_EXT_CAP_ID(pcie_cap) == 0x0000 ||
  63. PCI_EXT_CAP_ID(pcie_cap) == 0xffff)
  64. break;
  65. if (PCI_EXT_CAP_ID(pcie_cap) == PCI_EXT_CAP_ID_VNDR) {
  66. raw_pci_ext_ops->read(pci_domain_nr(bus), bus->number,
  67. devfn, pos + 4, 4, &cap_data);
  68. if ((cap_data & 0xffff) == PCIE_VNDR_CAP_ID_FIXED_BAR)
  69. return pos;
  70. }
  71. pos = PCI_EXT_CAP_NEXT(pcie_cap);
  72. }
  73. return 0;
  74. }
  75. static int pci_device_update_fixed(struct pci_bus *bus, unsigned int devfn,
  76. int reg, int len, u32 val, int offset)
  77. {
  78. u32 size;
  79. unsigned int domain, busnum;
  80. int bar = (reg - PCI_BASE_ADDRESS_0) >> 2;
  81. domain = pci_domain_nr(bus);
  82. busnum = bus->number;
  83. if (val == ~0 && len == 4) {
  84. unsigned long decode;
  85. raw_pci_ext_ops->read(domain, busnum, devfn,
  86. offset + 8 + (bar * 4), 4, &size);
  87. /* Turn the size into a decode pattern for the sizing code */
  88. if (size) {
  89. decode = size - 1;
  90. decode |= decode >> 1;
  91. decode |= decode >> 2;
  92. decode |= decode >> 4;
  93. decode |= decode >> 8;
  94. decode |= decode >> 16;
  95. decode++;
  96. decode = ~(decode - 1);
  97. } else {
  98. decode = 0;
  99. }
  100. /*
  101. * If val is all ones, the core code is trying to size the reg,
  102. * so update the mmconfig space with the real size.
  103. *
  104. * Note: this assumes the fixed size we got is a power of two.
  105. */
  106. return raw_pci_ext_ops->write(domain, busnum, devfn, reg, 4,
  107. decode);
  108. }
  109. /* This is some other kind of BAR write, so just do it. */
  110. return raw_pci_ext_ops->write(domain, busnum, devfn, reg, len, val);
  111. }
  112. /**
  113. * type1_access_ok - check whether to use type 1
  114. * @bus: bus number
  115. * @devfn: device & function in question
  116. *
  117. * If the bus is on a Lincroft chip and it exists, or is not on a Lincroft at
  118. * all, the we can go ahead with any reads & writes. If it's on a Lincroft,
  119. * but doesn't exist, avoid the access altogether to keep the chip from
  120. * hanging.
  121. */
  122. static bool type1_access_ok(unsigned int bus, unsigned int devfn, int reg)
  123. {
  124. /*
  125. * This is a workaround for A0 LNC bug where PCI status register does
  126. * not have new CAP bit set. can not be written by SW either.
  127. *
  128. * PCI header type in real LNC indicates a single function device, this
  129. * will prevent probing other devices under the same function in PCI
  130. * shim. Therefore, use the header type in shim instead.
  131. */
  132. if (reg >= 0x100 || reg == PCI_STATUS || reg == PCI_HEADER_TYPE)
  133. return false;
  134. if (bus == 0 && (devfn == PCI_DEVFN(2, 0)
  135. || devfn == PCI_DEVFN(0, 0)
  136. || devfn == PCI_DEVFN(3, 0)))
  137. return true;
  138. return false; /* Langwell on others */
  139. }
  140. static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
  141. int size, u32 *value)
  142. {
  143. if (type1_access_ok(bus->number, devfn, where))
  144. return pci_direct_conf1.read(pci_domain_nr(bus), bus->number,
  145. devfn, where, size, value);
  146. return raw_pci_ext_ops->read(pci_domain_nr(bus), bus->number,
  147. devfn, where, size, value);
  148. }
  149. static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
  150. int size, u32 value)
  151. {
  152. int offset;
  153. /*
  154. * On MRST, there is no PCI ROM BAR, this will cause a subsequent read
  155. * to ROM BAR return 0 then being ignored.
  156. */
  157. if (where == PCI_ROM_ADDRESS)
  158. return 0;
  159. /*
  160. * Devices with fixed BARs need special handling:
  161. * - BAR sizing code will save, write ~0, read size, restore
  162. * - so writes to fixed BARs need special handling
  163. * - other writes to fixed BAR devices should go through mmconfig
  164. */
  165. offset = fixed_bar_cap(bus, devfn);
  166. if (offset &&
  167. (where >= PCI_BASE_ADDRESS_0 && where <= PCI_BASE_ADDRESS_5)) {
  168. return pci_device_update_fixed(bus, devfn, where, size, value,
  169. offset);
  170. }
  171. /*
  172. * On Moorestown update both real & mmconfig space
  173. * Note: early Lincroft silicon can't handle type 1 accesses to
  174. * non-existent devices, so just eat the write in that case.
  175. */
  176. if (type1_access_ok(bus->number, devfn, where))
  177. return pci_direct_conf1.write(pci_domain_nr(bus), bus->number,
  178. devfn, where, size, value);
  179. return raw_pci_ext_ops->write(pci_domain_nr(bus), bus->number, devfn,
  180. where, size, value);
  181. }
  182. static int intel_mid_pci_irq_enable(struct pci_dev *dev)
  183. {
  184. int polarity;
  185. if (dev->irq_managed && dev->irq > 0)
  186. return 0;
  187. if (intel_mid_identify_cpu() == INTEL_MID_CPU_CHIP_TANGIER)
  188. polarity = 0; /* active high */
  189. else
  190. polarity = 1; /* active low */
  191. /*
  192. * MRST only have IOAPIC, the PCI irq lines are 1:1 mapped to
  193. * IOAPIC RTE entries, so we just enable RTE for the device.
  194. */
  195. if (mp_set_gsi_attr(dev->irq, 1, polarity, dev_to_node(&dev->dev)))
  196. return -EBUSY;
  197. if (mp_map_gsi_to_irq(dev->irq, IOAPIC_MAP_ALLOC) < 0)
  198. return -EBUSY;
  199. dev->irq_managed = 1;
  200. return 0;
  201. }
  202. static void intel_mid_pci_irq_disable(struct pci_dev *dev)
  203. {
  204. if (dev->irq_managed && dev->irq > 0) {
  205. mp_unmap_irq(dev->irq);
  206. dev->irq_managed = 0;
  207. dev->irq = 0;
  208. }
  209. }
  210. struct pci_ops intel_mid_pci_ops = {
  211. .read = pci_read,
  212. .write = pci_write,
  213. };
  214. /**
  215. * intel_mid_pci_init - installs intel_mid_pci_ops
  216. *
  217. * Moorestown has an interesting PCI implementation (see above).
  218. * Called when the early platform detection installs it.
  219. */
  220. int __init intel_mid_pci_init(void)
  221. {
  222. pr_info("Intel MID platform detected, using MID PCI ops\n");
  223. pci_mmcfg_late_init();
  224. pcibios_enable_irq = intel_mid_pci_irq_enable;
  225. pcibios_disable_irq = intel_mid_pci_irq_disable;
  226. pci_root_ops = intel_mid_pci_ops;
  227. pci_soc_mode = 1;
  228. /* Continue with standard init */
  229. return 1;
  230. }
  231. /*
  232. * Langwell devices are not true PCI devices; they are not subject to 10 ms
  233. * d3 to d0 delay required by PCI spec.
  234. */
  235. static void pci_d3delay_fixup(struct pci_dev *dev)
  236. {
  237. /*
  238. * PCI fixups are effectively decided compile time. If we have a dual
  239. * SoC/non-SoC kernel we don't want to mangle d3 on non-SoC devices.
  240. */
  241. if (!pci_soc_mode)
  242. return;
  243. /*
  244. * True PCI devices in Lincroft should allow type 1 access, the rest
  245. * are Langwell fake PCI devices.
  246. */
  247. if (type1_access_ok(dev->bus->number, dev->devfn, PCI_DEVICE_ID))
  248. return;
  249. dev->d3_delay = 0;
  250. }
  251. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_ANY_ID, pci_d3delay_fixup);
  252. static void mrst_power_off_unused_dev(struct pci_dev *dev)
  253. {
  254. pci_set_power_state(dev, PCI_D3hot);
  255. }
  256. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0801, mrst_power_off_unused_dev);
  257. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0809, mrst_power_off_unused_dev);
  258. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x080C, mrst_power_off_unused_dev);
  259. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0812, mrst_power_off_unused_dev);
  260. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0815, mrst_power_off_unused_dev);
  261. /*
  262. * Langwell devices reside at fixed offsets, don't try to move them.
  263. */
  264. static void pci_fixed_bar_fixup(struct pci_dev *dev)
  265. {
  266. unsigned long offset;
  267. u32 size;
  268. int i;
  269. if (!pci_soc_mode)
  270. return;
  271. /* Must have extended configuration space */
  272. if (dev->cfg_size < PCIE_CAP_OFFSET + 4)
  273. return;
  274. /* Fixup the BAR sizes for fixed BAR devices and make them unmoveable */
  275. offset = fixed_bar_cap(dev->bus, dev->devfn);
  276. if (!offset || PCI_DEVFN(2, 0) == dev->devfn ||
  277. PCI_DEVFN(2, 2) == dev->devfn)
  278. return;
  279. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  280. pci_read_config_dword(dev, offset + 8 + (i * 4), &size);
  281. dev->resource[i].end = dev->resource[i].start + size - 1;
  282. dev->resource[i].flags |= IORESOURCE_PCI_FIXED;
  283. }
  284. }
  285. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_ANY_ID, pci_fixed_bar_fixup);