pcie-tango.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <linux/pci-ecam.h>
  2. #include <linux/delay.h>
  3. #include <linux/of.h>
  4. #define SMP8759_MUX 0x48
  5. #define SMP8759_TEST_OUT 0x74
  6. struct tango_pcie {
  7. void __iomem *base;
  8. };
  9. static int smp8759_config_read(struct pci_bus *bus, unsigned int devfn,
  10. int where, int size, u32 *val)
  11. {
  12. struct pci_config_window *cfg = bus->sysdata;
  13. struct tango_pcie *pcie = dev_get_drvdata(cfg->parent);
  14. int ret;
  15. /* Reads in configuration space outside devfn 0 return garbage */
  16. if (devfn != 0)
  17. return PCIBIOS_FUNC_NOT_SUPPORTED;
  18. /*
  19. * PCI config and MMIO accesses are muxed. Linux doesn't have a
  20. * mutual exclusion mechanism for config vs. MMIO accesses, so
  21. * concurrent accesses may cause corruption.
  22. */
  23. writel_relaxed(1, pcie->base + SMP8759_MUX);
  24. ret = pci_generic_config_read(bus, devfn, where, size, val);
  25. writel_relaxed(0, pcie->base + SMP8759_MUX);
  26. return ret;
  27. }
  28. static int smp8759_config_write(struct pci_bus *bus, unsigned int devfn,
  29. int where, int size, u32 val)
  30. {
  31. struct pci_config_window *cfg = bus->sysdata;
  32. struct tango_pcie *pcie = dev_get_drvdata(cfg->parent);
  33. int ret;
  34. writel_relaxed(1, pcie->base + SMP8759_MUX);
  35. ret = pci_generic_config_write(bus, devfn, where, size, val);
  36. writel_relaxed(0, pcie->base + SMP8759_MUX);
  37. return ret;
  38. }
  39. static struct pci_ecam_ops smp8759_ecam_ops = {
  40. .bus_shift = 20,
  41. .pci_ops = {
  42. .map_bus = pci_ecam_map_bus,
  43. .read = smp8759_config_read,
  44. .write = smp8759_config_write,
  45. }
  46. };
  47. static int tango_pcie_link_up(struct tango_pcie *pcie)
  48. {
  49. void __iomem *test_out = pcie->base + SMP8759_TEST_OUT;
  50. int i;
  51. writel_relaxed(16, test_out);
  52. for (i = 0; i < 10; ++i) {
  53. u32 ltssm_state = readl_relaxed(test_out) >> 8;
  54. if ((ltssm_state & 0x1f) == 0xf) /* L0 */
  55. return 1;
  56. usleep_range(3000, 4000);
  57. }
  58. return 0;
  59. }
  60. static int tango_pcie_probe(struct platform_device *pdev)
  61. {
  62. struct device *dev = &pdev->dev;
  63. struct tango_pcie *pcie;
  64. struct resource *res;
  65. int ret;
  66. dev_warn(dev, "simultaneous PCI config and MMIO accesses may cause data corruption\n");
  67. add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
  68. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  69. if (!pcie)
  70. return -ENOMEM;
  71. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  72. pcie->base = devm_ioremap_resource(dev, res);
  73. if (IS_ERR(pcie->base))
  74. return PTR_ERR(pcie->base);
  75. platform_set_drvdata(pdev, pcie);
  76. if (!tango_pcie_link_up(pcie))
  77. return -ENODEV;
  78. return pci_host_common_probe(pdev, &smp8759_ecam_ops);
  79. }
  80. static const struct of_device_id tango_pcie_ids[] = {
  81. { .compatible = "sigma,smp8759-pcie" },
  82. { },
  83. };
  84. static struct platform_driver tango_pcie_driver = {
  85. .probe = tango_pcie_probe,
  86. .driver = {
  87. .name = KBUILD_MODNAME,
  88. .of_match_table = tango_pcie_ids,
  89. .suppress_bind_attrs = true,
  90. },
  91. };
  92. builtin_platform_driver(tango_pcie_driver);
  93. /*
  94. * The root complex advertises the wrong device class.
  95. * Header Type 1 is for PCI-to-PCI bridges.
  96. */
  97. static void tango_fixup_class(struct pci_dev *dev)
  98. {
  99. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  100. }
  101. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SIGMA, 0x0024, tango_fixup_class);
  102. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SIGMA, 0x0028, tango_fixup_class);
  103. /*
  104. * The root complex exposes a "fake" BAR, which is used to filter
  105. * bus-to-system accesses. Only accesses within the range defined by this
  106. * BAR are forwarded to the host, others are ignored.
  107. *
  108. * By default, the DMA framework expects an identity mapping, and DRAM0 is
  109. * mapped at 0x80000000.
  110. */
  111. static void tango_fixup_bar(struct pci_dev *dev)
  112. {
  113. dev->non_compliant_bars = true;
  114. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, 0x80000000);
  115. }
  116. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SIGMA, 0x0024, tango_fixup_bar);
  117. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SIGMA, 0x0028, tango_fixup_bar);