intel_mid_pci.c 9.7 KB

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