pci.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2006 PA Semi, Inc
  3. *
  4. * Authors: Kip Walker, PA Semi
  5. * Olof Johansson, PA Semi
  6. *
  7. * Maintained by: Olof Johansson <olof@lixom.net>
  8. *
  9. * Based on arch/powerpc/platforms/maple/pci.c
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/pci.h>
  26. #include <asm/pci-bridge.h>
  27. #include <asm/machdep.h>
  28. #include <asm/ppc-pci.h>
  29. #define PA_PXP_CFA(bus, devfn, off) (((bus) << 20) | ((devfn) << 12) | (off))
  30. static inline int pa_pxp_offset_valid(u8 bus, u8 devfn, int offset)
  31. {
  32. /* Device 0 Function 0 is special: It's config space spans function 1 as
  33. * well, so allow larger offset. It's really a two-function device but the
  34. * second function does not probe.
  35. */
  36. if (bus == 0 && devfn == 0)
  37. return offset < 8192;
  38. else
  39. return offset < 4096;
  40. }
  41. static void volatile __iomem *pa_pxp_cfg_addr(struct pci_controller *hose,
  42. u8 bus, u8 devfn, int offset)
  43. {
  44. return hose->cfg_data + PA_PXP_CFA(bus, devfn, offset);
  45. }
  46. static int pa_pxp_read_config(struct pci_bus *bus, unsigned int devfn,
  47. int offset, int len, u32 *val)
  48. {
  49. struct pci_controller *hose;
  50. void volatile __iomem *addr;
  51. hose = pci_bus_to_host(bus);
  52. if (!hose)
  53. return PCIBIOS_DEVICE_NOT_FOUND;
  54. if (!pa_pxp_offset_valid(bus->number, devfn, offset))
  55. return PCIBIOS_BAD_REGISTER_NUMBER;
  56. addr = pa_pxp_cfg_addr(hose, bus->number, devfn, offset);
  57. /*
  58. * Note: the caller has already checked that offset is
  59. * suitably aligned and that len is 1, 2 or 4.
  60. */
  61. switch (len) {
  62. case 1:
  63. *val = in_8(addr);
  64. break;
  65. case 2:
  66. *val = in_le16(addr);
  67. break;
  68. default:
  69. *val = in_le32(addr);
  70. break;
  71. }
  72. return PCIBIOS_SUCCESSFUL;
  73. }
  74. static int pa_pxp_write_config(struct pci_bus *bus, unsigned int devfn,
  75. int offset, int len, u32 val)
  76. {
  77. struct pci_controller *hose;
  78. void volatile __iomem *addr;
  79. hose = pci_bus_to_host(bus);
  80. if (!hose)
  81. return PCIBIOS_DEVICE_NOT_FOUND;
  82. if (!pa_pxp_offset_valid(bus->number, devfn, offset))
  83. return PCIBIOS_BAD_REGISTER_NUMBER;
  84. addr = pa_pxp_cfg_addr(hose, bus->number, devfn, offset);
  85. /*
  86. * Note: the caller has already checked that offset is
  87. * suitably aligned and that len is 1, 2 or 4.
  88. */
  89. switch (len) {
  90. case 1:
  91. out_8(addr, val);
  92. (void) in_8(addr);
  93. break;
  94. case 2:
  95. out_le16(addr, val);
  96. (void) in_le16(addr);
  97. break;
  98. default:
  99. out_le32(addr, val);
  100. (void) in_le32(addr);
  101. break;
  102. }
  103. return PCIBIOS_SUCCESSFUL;
  104. }
  105. static struct pci_ops pa_pxp_ops = {
  106. pa_pxp_read_config,
  107. pa_pxp_write_config,
  108. };
  109. static void __init setup_pa_pxp(struct pci_controller *hose)
  110. {
  111. hose->ops = &pa_pxp_ops;
  112. hose->cfg_data = ioremap(0xe0000000, 0x10000000);
  113. }
  114. static int __init add_bridge(struct device_node *dev)
  115. {
  116. struct pci_controller *hose;
  117. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  118. hose = pcibios_alloc_controller(dev);
  119. if (!hose)
  120. return -ENOMEM;
  121. hose->first_busno = 0;
  122. hose->last_busno = 0xff;
  123. setup_pa_pxp(hose);
  124. printk(KERN_INFO "Found PA-PXP PCI host bridge.\n");
  125. /* Interpret the "ranges" property */
  126. /* This also maps the I/O region and sets isa_io/mem_base */
  127. pci_process_bridge_OF_ranges(hose, dev, 1);
  128. pci_setup_phb_io(hose, 1);
  129. return 0;
  130. }
  131. static void __init pas_fixup_phb_resources(void)
  132. {
  133. struct pci_controller *hose, *tmp;
  134. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  135. unsigned long offset = (unsigned long)hose->io_base_virt - pci_io_base;
  136. hose->io_resource.start += offset;
  137. hose->io_resource.end += offset;
  138. printk(KERN_INFO "PCI Host %d, io start: %lx; io end: %lx\n",
  139. hose->global_number,
  140. hose->io_resource.start, hose->io_resource.end);
  141. }
  142. }
  143. void __init pas_pci_init(void)
  144. {
  145. struct device_node *np, *root;
  146. root = of_find_node_by_path("/");
  147. if (!root) {
  148. printk(KERN_CRIT "pas_pci_init: can't find root "
  149. "of device tree\n");
  150. return;
  151. }
  152. for (np = NULL; (np = of_get_next_child(root, np)) != NULL;)
  153. if (np->name && !strcmp(np->name, "pxp") && !add_bridge(np))
  154. of_node_get(np);
  155. of_node_put(root);
  156. pas_fixup_phb_resources();
  157. /* Setup the linkage between OF nodes and PHBs */
  158. pci_devs_phb_init();
  159. /* Use the common resource allocation mechanism */
  160. pci_probe_only = 1;
  161. }