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/pci.h>
  15. #include <asm/prom.h>
  16. #include <asm/pci-bridge.h>
  17. #define PCI_HEADER_TYPE_INVALID 0x7f /* Invalid PCI header type */
  18. #ifdef CONFIG_SYSFS
  19. /* 32-bit hex or dec stringified number + '\n' */
  20. #define MV64X60_VAL_LEN_MAX 11
  21. #define MV64X60_PCICFG_CPCI_HOTSWAP 0x68
  22. static ssize_t mv64x60_hs_reg_read(struct kobject *kobj, char *buf, loff_t off,
  23. size_t count)
  24. {
  25. struct pci_dev *phb;
  26. u32 v;
  27. if (off > 0)
  28. return 0;
  29. if (count < MV64X60_VAL_LEN_MAX)
  30. return -EINVAL;
  31. phb = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
  32. if (!phb)
  33. return -ENODEV;
  34. pci_read_config_dword(phb, MV64X60_PCICFG_CPCI_HOTSWAP, &v);
  35. pci_dev_put(phb);
  36. return sprintf(buf, "0x%08x\n", v);
  37. }
  38. static ssize_t mv64x60_hs_reg_write(struct kobject *kobj, char *buf, loff_t off,
  39. size_t count)
  40. {
  41. struct pci_dev *phb;
  42. u32 v;
  43. if (off > 0)
  44. return 0;
  45. if (count <= 0)
  46. return -EINVAL;
  47. if (sscanf(buf, "%i", &v) != 1)
  48. return -EINVAL;
  49. phb = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
  50. if (!phb)
  51. return -ENODEV;
  52. pci_write_config_dword(phb, MV64X60_PCICFG_CPCI_HOTSWAP, v);
  53. pci_dev_put(phb);
  54. return count;
  55. }
  56. static struct bin_attribute mv64x60_hs_reg_attr = { /* Hotswap register */
  57. .attr = {
  58. .name = "hs_reg",
  59. .mode = S_IRUGO | S_IWUSR,
  60. .owner = THIS_MODULE,
  61. },
  62. .size = MV64X60_VAL_LEN_MAX,
  63. .read = mv64x60_hs_reg_read,
  64. .write = mv64x60_hs_reg_write,
  65. };
  66. static int __init mv64x60_sysfs_init(void)
  67. {
  68. struct device_node *np;
  69. struct platform_device *pdev;
  70. const unsigned int *prop;
  71. np = of_find_compatible_node(NULL, NULL, "marvell,mv64x60");
  72. if (!np)
  73. return 0;
  74. prop = of_get_property(np, "hs_reg_valid", NULL);
  75. of_node_put(np);
  76. pdev = platform_device_register_simple("marvell,mv64x60", 0, NULL, 0);
  77. if (IS_ERR(pdev))
  78. return PTR_ERR(pdev);
  79. return sysfs_create_bin_file(&pdev->dev.kobj, &mv64x60_hs_reg_attr);
  80. }
  81. subsys_initcall(mv64x60_sysfs_init);
  82. #endif /* CONFIG_SYSFS */
  83. static void __init mv64x60_pci_fixup_early(struct pci_dev *dev)
  84. {
  85. /*
  86. * Set the host bridge hdr_type to an invalid value so that
  87. * pci_setup_device() will ignore the host bridge.
  88. */
  89. dev->hdr_type = PCI_HEADER_TYPE_INVALID;
  90. }
  91. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_MARVELL, PCI_DEVICE_ID_MARVELL_MV64360,
  92. mv64x60_pci_fixup_early);
  93. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_MARVELL, PCI_DEVICE_ID_MARVELL_MV64460,
  94. mv64x60_pci_fixup_early);
  95. static int __init mv64x60_add_bridge(struct device_node *dev)
  96. {
  97. int len;
  98. struct pci_controller *hose;
  99. struct resource rsrc;
  100. const int *bus_range;
  101. int primary;
  102. memset(&rsrc, 0, sizeof(rsrc));
  103. /* Fetch host bridge registers address */
  104. if (of_address_to_resource(dev, 0, &rsrc)) {
  105. printk(KERN_ERR "No PCI reg property in device tree\n");
  106. return -ENODEV;
  107. }
  108. /* Get bus range if any */
  109. bus_range = of_get_property(dev, "bus-range", &len);
  110. if (bus_range == NULL || len < 2 * sizeof(int))
  111. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  112. " bus 0\n", dev->full_name);
  113. hose = pcibios_alloc_controller();
  114. if (!hose)
  115. return -ENOMEM;
  116. hose->arch_data = dev;
  117. hose->set_cfg_type = 1;
  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);
  121. hose->bus_offset = 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 = NULL;
  135. while ((np = of_find_compatible_node(np, "pci", "marvell,mv64x60-pci")))
  136. mv64x60_add_bridge(np);
  137. }