pci-versatile.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 2004 Koninklijke Philips Electronics NV
  3. *
  4. * Conversion to platform driver and DT:
  5. * Copyright 2014 Linaro Ltd.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * 14/04/2005 Initial version, colin.king@philips.com
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_pci.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/pci.h>
  24. #include <linux/platform_device.h>
  25. static void __iomem *versatile_pci_base;
  26. static void __iomem *versatile_cfg_base[2];
  27. #define PCI_IMAP(m) (versatile_pci_base + ((m) * 4))
  28. #define PCI_SMAP(m) (versatile_pci_base + 0x14 + ((m) * 4))
  29. #define PCI_SELFID (versatile_pci_base + 0xc)
  30. #define VP_PCI_DEVICE_ID 0x030010ee
  31. #define VP_PCI_CLASS_ID 0x0b400000
  32. static u32 pci_slot_ignore;
  33. static int __init versatile_pci_slot_ignore(char *str)
  34. {
  35. int retval;
  36. int slot;
  37. while ((retval = get_option(&str, &slot))) {
  38. if ((slot < 0) || (slot > 31))
  39. pr_err("Illegal slot value: %d\n", slot);
  40. else
  41. pci_slot_ignore |= (1 << slot);
  42. }
  43. return 1;
  44. }
  45. __setup("pci_slot_ignore=", versatile_pci_slot_ignore);
  46. static void __iomem *versatile_map_bus(struct pci_bus *bus,
  47. unsigned int devfn, int offset)
  48. {
  49. unsigned int busnr = bus->number;
  50. if (pci_slot_ignore & (1 << PCI_SLOT(devfn)))
  51. return NULL;
  52. return versatile_cfg_base[1] + ((busnr << 16) | (devfn << 8) | offset);
  53. }
  54. static struct pci_ops pci_versatile_ops = {
  55. .map_bus = versatile_map_bus,
  56. .read = pci_generic_config_read32,
  57. .write = pci_generic_config_write,
  58. };
  59. static int versatile_pci_parse_request_of_pci_ranges(struct device *dev,
  60. struct list_head *res)
  61. {
  62. int err, mem = 1, res_valid = 0;
  63. struct device_node *np = dev->of_node;
  64. resource_size_t iobase;
  65. struct resource_entry *win;
  66. err = of_pci_get_host_bridge_resources(np, 0, 0xff, res, &iobase);
  67. if (err)
  68. return err;
  69. resource_list_for_each_entry(win, res) {
  70. struct resource *parent, *res = win->res;
  71. switch (resource_type(res)) {
  72. case IORESOURCE_IO:
  73. parent = &ioport_resource;
  74. err = pci_remap_iospace(res, iobase);
  75. if (err) {
  76. dev_warn(dev, "error %d: failed to map resource %pR\n",
  77. err, res);
  78. continue;
  79. }
  80. break;
  81. case IORESOURCE_MEM:
  82. parent = &iomem_resource;
  83. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  84. writel(res->start >> 28, PCI_IMAP(mem));
  85. writel(PHYS_OFFSET >> 28, PCI_SMAP(mem));
  86. mem++;
  87. break;
  88. case IORESOURCE_BUS:
  89. default:
  90. continue;
  91. }
  92. err = devm_request_resource(dev, parent, res);
  93. if (err)
  94. goto out_release_res;
  95. }
  96. if (!res_valid) {
  97. dev_err(dev, "non-prefetchable memory resource required\n");
  98. err = -EINVAL;
  99. goto out_release_res;
  100. }
  101. return 0;
  102. out_release_res:
  103. pci_free_resource_list(res);
  104. return err;
  105. }
  106. /* Unused, temporary to satisfy ARM arch code */
  107. struct pci_sys_data sys;
  108. static int versatile_pci_probe(struct platform_device *pdev)
  109. {
  110. struct resource *res;
  111. int ret, i, myslot = -1;
  112. u32 val;
  113. void __iomem *local_pci_cfg_base;
  114. struct pci_bus *bus;
  115. LIST_HEAD(pci_res);
  116. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  117. versatile_pci_base = devm_ioremap_resource(&pdev->dev, res);
  118. if (IS_ERR(versatile_pci_base))
  119. return PTR_ERR(versatile_pci_base);
  120. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  121. versatile_cfg_base[0] = devm_ioremap_resource(&pdev->dev, res);
  122. if (IS_ERR(versatile_cfg_base[0]))
  123. return PTR_ERR(versatile_cfg_base[0]);
  124. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  125. versatile_cfg_base[1] = devm_ioremap_resource(&pdev->dev, res);
  126. if (IS_ERR(versatile_cfg_base[1]))
  127. return PTR_ERR(versatile_cfg_base[1]);
  128. ret = versatile_pci_parse_request_of_pci_ranges(&pdev->dev, &pci_res);
  129. if (ret)
  130. return ret;
  131. /*
  132. * We need to discover the PCI core first to configure itself
  133. * before the main PCI probing is performed
  134. */
  135. for (i = 0; i < 32; i++) {
  136. if ((readl(versatile_cfg_base[0] + (i << 11) + PCI_VENDOR_ID) == VP_PCI_DEVICE_ID) &&
  137. (readl(versatile_cfg_base[0] + (i << 11) + PCI_CLASS_REVISION) == VP_PCI_CLASS_ID)) {
  138. myslot = i;
  139. break;
  140. }
  141. }
  142. if (myslot == -1) {
  143. dev_err(&pdev->dev, "Cannot find PCI core!\n");
  144. return -EIO;
  145. }
  146. /*
  147. * Do not to map Versatile FPGA PCI device into memory space
  148. */
  149. pci_slot_ignore |= (1 << myslot);
  150. dev_info(&pdev->dev, "PCI core found (slot %d)\n", myslot);
  151. writel(myslot, PCI_SELFID);
  152. local_pci_cfg_base = versatile_cfg_base[1] + (myslot << 11);
  153. val = readl(local_pci_cfg_base + PCI_COMMAND);
  154. val |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE;
  155. writel(val, local_pci_cfg_base + PCI_COMMAND);
  156. /*
  157. * Configure the PCI inbound memory windows to be 1:1 mapped to SDRAM
  158. */
  159. writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_0);
  160. writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_1);
  161. writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_2);
  162. /*
  163. * For many years the kernel and QEMU were symbiotically buggy
  164. * in that they both assumed the same broken IRQ mapping.
  165. * QEMU therefore attempts to auto-detect old broken kernels
  166. * so that they still work on newer QEMU as they did on old
  167. * QEMU. Since we now use the correct (ie matching-hardware)
  168. * IRQ mapping we write a definitely different value to a
  169. * PCI_INTERRUPT_LINE register to tell QEMU that we expect
  170. * real hardware behaviour and it need not be backwards
  171. * compatible for us. This write is harmless on real hardware.
  172. */
  173. writel(0, versatile_cfg_base[0] + PCI_INTERRUPT_LINE);
  174. pci_add_flags(PCI_ENABLE_PROC_DOMAINS);
  175. pci_add_flags(PCI_REASSIGN_ALL_BUS | PCI_REASSIGN_ALL_RSRC);
  176. bus = pci_scan_root_bus(&pdev->dev, 0, &pci_versatile_ops, &sys, &pci_res);
  177. if (!bus)
  178. return -ENOMEM;
  179. pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
  180. pci_assign_unassigned_bus_resources(bus);
  181. pci_bus_add_devices(bus);
  182. return 0;
  183. }
  184. static const struct of_device_id versatile_pci_of_match[] = {
  185. { .compatible = "arm,versatile-pci", },
  186. { },
  187. };
  188. MODULE_DEVICE_TABLE(of, versatile_pci_of_match);
  189. static struct platform_driver versatile_pci_driver = {
  190. .driver = {
  191. .name = "versatile-pci",
  192. .of_match_table = versatile_pci_of_match,
  193. },
  194. .probe = versatile_pci_probe,
  195. };
  196. module_platform_driver(versatile_pci_driver);
  197. MODULE_DESCRIPTION("Versatile PCI driver");
  198. MODULE_LICENSE("GPL v2");