intel_mid_pci.c 9.9 KB

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