mv64x60_pci.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * PCI bus setup for Marvell mv64360/mv64460 host bridges (Discovery)
  3. *
  4. * Author: Dale Farnsworth <dale@farnsworth.org>
  5. *
  6. * 2007 (c) MontaVista, Software, Inc. This file is licensed under
  7. * the terms of the GNU General Public License version 2. This program
  8. * is licensed "as is" without any warranty of any kind, whether express
  9. * or implied.
  10. */
  11. #include <linux/stddef.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/stat.h>
  15. #include <linux/pci.h>
  16. #include <asm/prom.h>
  17. #include <asm/pci-bridge.h>
  18. #define PCI_HEADER_TYPE_INVALID 0x7f /* Invalid PCI header type */
  19. #ifdef CONFIG_SYSFS
  20. /* 32-bit hex or dec stringified number + '\n' */
  21. #define MV64X60_VAL_LEN_MAX 11
  22. #define MV64X60_PCICFG_CPCI_HOTSWAP 0x68
  23. static ssize_t mv64x60_hs_reg_read(struct file *filp, struct kobject *kobj,
  24. struct bin_attribute *attr, char *buf,
  25. loff_t off, size_t count)
  26. {
  27. struct pci_dev *phb;
  28. u32 v;
  29. if (off > 0)
  30. return 0;
  31. if (count < MV64X60_VAL_LEN_MAX)
  32. return -EINVAL;
  33. phb = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
  34. if (!phb)
  35. return -ENODEV;
  36. pci_read_config_dword(phb, MV64X60_PCICFG_CPCI_HOTSWAP, &v);
  37. pci_dev_put(phb);
  38. return sprintf(buf, "0x%08x\n", v);
  39. }
  40. static ssize_t mv64x60_hs_reg_write(struct file *filp, struct kobject *kobj,
  41. struct bin_attribute *attr, char *buf,
  42. loff_t off, size_t count)
  43. {
  44. struct pci_dev *phb;
  45. u32 v;
  46. if (off > 0)
  47. return 0;
  48. if (count <= 0)
  49. return -EINVAL;
  50. if (sscanf(buf, "%i", &v) != 1)
  51. return -EINVAL;
  52. phb = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
  53. if (!phb)
  54. return -ENODEV;
  55. pci_write_config_dword(phb, MV64X60_PCICFG_CPCI_HOTSWAP, v);
  56. pci_dev_put(phb);
  57. return count;
  58. }
  59. static struct bin_attribute mv64x60_hs_reg_attr = { /* Hotswap register */
  60. .attr = {
  61. .name = "hs_reg",
  62. .mode = S_IRUGO | S_IWUSR,
  63. },
  64. .size = MV64X60_VAL_LEN_MAX,
  65. .read = mv64x60_hs_reg_read,
  66. .write = mv64x60_hs_reg_write,
  67. };
  68. static int __init mv64x60_sysfs_init(void)
  69. {
  70. struct device_node *np;
  71. struct platform_device *pdev;
  72. const unsigned int *prop;
  73. np = of_find_compatible_node(NULL, NULL, "marvell,mv64360");
  74. if (!np)
  75. return 0;
  76. prop = of_get_property(np, "hs_reg_valid", NULL);
  77. of_node_put(np);
  78. pdev = platform_device_register_simple("marvell,mv64360", 0, NULL, 0);
  79. if (IS_ERR(pdev))
  80. return PTR_ERR(pdev);
  81. return sysfs_create_bin_file(&pdev->dev.kobj, &mv64x60_hs_reg_attr);
  82. }
  83. subsys_initcall(mv64x60_sysfs_init);
  84. #endif /* CONFIG_SYSFS */
  85. static void mv64x60_pci_fixup_early(struct pci_dev *dev)
  86. {
  87. /*
  88. * Set the host bridge hdr_type to an invalid value so that
  89. * pci_setup_device() will ignore the host bridge.
  90. */
  91. dev->hdr_type = PCI_HEADER_TYPE_INVALID;
  92. }
  93. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_MARVELL, PCI_DEVICE_ID_MARVELL_MV64360,
  94. mv64x60_pci_fixup_early);
  95. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_MARVELL, PCI_DEVICE_ID_MARVELL_MV64460,
  96. mv64x60_pci_fixup_early);
  97. static int __init mv64x60_add_bridge(struct device_node *dev)
  98. {
  99. int len;
  100. struct pci_controller *hose;
  101. struct resource rsrc;
  102. const int *bus_range;
  103. int primary;
  104. memset(&rsrc, 0, sizeof(rsrc));
  105. /* Fetch host bridge registers address */
  106. if (of_address_to_resource(dev, 0, &rsrc)) {
  107. printk(KERN_ERR "No PCI reg property in device tree\n");
  108. return -ENODEV;
  109. }
  110. /* Get bus range if any */
  111. bus_range = of_get_property(dev, "bus-range", &len);
  112. if (bus_range == NULL || len < 2 * sizeof(int))
  113. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  114. " bus 0\n", dev->full_name);
  115. hose = pcibios_alloc_controller(dev);
  116. if (!hose)
  117. return -ENOMEM;
  118. hose->first_busno = bus_range ? bus_range[0] : 0;
  119. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  120. setup_indirect_pci(hose, rsrc.start, rsrc.start + 4, 0);
  121. hose->self_busno = hose->first_busno;
  122. printk(KERN_INFO "Found MV64x60 PCI host bridge at 0x%016llx. "
  123. "Firmware bus number: %d->%d\n",
  124. (unsigned long long)rsrc.start, hose->first_busno,
  125. hose->last_busno);
  126. /* Interpret the "ranges" property */
  127. /* This also maps the I/O region and sets isa_io/mem_base */
  128. primary = (hose->first_busno == 0);
  129. pci_process_bridge_OF_ranges(hose, dev, primary);
  130. return 0;
  131. }
  132. void __init mv64x60_pci_init(void)
  133. {
  134. struct device_node *np;
  135. for_each_compatible_node(np, "pci", "marvell,mv64360-pci")
  136. mv64x60_add_bridge(np);
  137. }