pci.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 inline int is_root_port(int busno, int devfn)
  47. {
  48. return ((busno == 0) && (PCI_FUNC(devfn) < 4) &&
  49. ((PCI_SLOT(devfn) == 16) || (PCI_SLOT(devfn) == 17)));
  50. }
  51. static inline int is_5945_reg(int reg)
  52. {
  53. return (((reg >= 0x18) && (reg < 0x34)) ||
  54. ((reg >= 0x158) && (reg < 0x178)));
  55. }
  56. static int workaround_5945(struct pci_bus *bus, unsigned int devfn,
  57. int offset, int len, u32 *val)
  58. {
  59. struct pci_controller *hose;
  60. void volatile __iomem *addr, *dummy;
  61. int byte;
  62. u32 tmp;
  63. if (!is_root_port(bus->number, devfn) || !is_5945_reg(offset))
  64. return 0;
  65. hose = pci_bus_to_host(bus);
  66. addr = pa_pxp_cfg_addr(hose, bus->number, devfn, offset & ~0x3);
  67. byte = offset & 0x3;
  68. /* Workaround bug 5945: write 0 to a dummy register before reading,
  69. * and write back what we read. We must read/write the full 32-bit
  70. * contents so we need to shift and mask by hand.
  71. */
  72. dummy = pa_pxp_cfg_addr(hose, bus->number, devfn, 0x10);
  73. out_le32(dummy, 0);
  74. tmp = in_le32(addr);
  75. out_le32(addr, tmp);
  76. switch (len) {
  77. case 1:
  78. *val = (tmp >> (8*byte)) & 0xff;
  79. break;
  80. case 2:
  81. if (byte == 0)
  82. *val = tmp & 0xffff;
  83. else
  84. *val = (tmp >> 16) & 0xffff;
  85. break;
  86. default:
  87. *val = tmp;
  88. break;
  89. }
  90. return 1;
  91. }
  92. static int pa_pxp_read_config(struct pci_bus *bus, unsigned int devfn,
  93. int offset, int len, u32 *val)
  94. {
  95. struct pci_controller *hose;
  96. void volatile __iomem *addr;
  97. hose = pci_bus_to_host(bus);
  98. if (!hose)
  99. return PCIBIOS_DEVICE_NOT_FOUND;
  100. if (!pa_pxp_offset_valid(bus->number, devfn, offset))
  101. return PCIBIOS_BAD_REGISTER_NUMBER;
  102. if (workaround_5945(bus, devfn, offset, len, val))
  103. return PCIBIOS_SUCCESSFUL;
  104. addr = pa_pxp_cfg_addr(hose, bus->number, devfn, offset);
  105. /*
  106. * Note: the caller has already checked that offset is
  107. * suitably aligned and that len is 1, 2 or 4.
  108. */
  109. switch (len) {
  110. case 1:
  111. *val = in_8(addr);
  112. break;
  113. case 2:
  114. *val = in_le16(addr);
  115. break;
  116. default:
  117. *val = in_le32(addr);
  118. break;
  119. }
  120. return PCIBIOS_SUCCESSFUL;
  121. }
  122. static int pa_pxp_write_config(struct pci_bus *bus, unsigned int devfn,
  123. int offset, int len, u32 val)
  124. {
  125. struct pci_controller *hose;
  126. void volatile __iomem *addr;
  127. hose = pci_bus_to_host(bus);
  128. if (!hose)
  129. return PCIBIOS_DEVICE_NOT_FOUND;
  130. if (!pa_pxp_offset_valid(bus->number, devfn, offset))
  131. return PCIBIOS_BAD_REGISTER_NUMBER;
  132. addr = pa_pxp_cfg_addr(hose, bus->number, devfn, offset);
  133. /*
  134. * Note: the caller has already checked that offset is
  135. * suitably aligned and that len is 1, 2 or 4.
  136. */
  137. switch (len) {
  138. case 1:
  139. out_8(addr, val);
  140. break;
  141. case 2:
  142. out_le16(addr, val);
  143. break;
  144. default:
  145. out_le32(addr, val);
  146. break;
  147. }
  148. return PCIBIOS_SUCCESSFUL;
  149. }
  150. static struct pci_ops pa_pxp_ops = {
  151. .read = pa_pxp_read_config,
  152. .write = pa_pxp_write_config,
  153. };
  154. static void __init setup_pa_pxp(struct pci_controller *hose)
  155. {
  156. hose->ops = &pa_pxp_ops;
  157. hose->cfg_data = ioremap(0xe0000000, 0x10000000);
  158. }
  159. static int __init pas_add_bridge(struct device_node *dev)
  160. {
  161. struct pci_controller *hose;
  162. pr_debug("Adding PCI host bridge %s\n", dev->full_name);
  163. hose = pcibios_alloc_controller(dev);
  164. if (!hose)
  165. return -ENOMEM;
  166. hose->first_busno = 0;
  167. hose->last_busno = 0xff;
  168. setup_pa_pxp(hose);
  169. printk(KERN_INFO "Found PA-PXP PCI host bridge.\n");
  170. /* Interpret the "ranges" property */
  171. pci_process_bridge_OF_ranges(hose, dev, 1);
  172. return 0;
  173. }
  174. void __init pas_pci_init(void)
  175. {
  176. struct device_node *np, *root;
  177. root = of_find_node_by_path("/");
  178. if (!root) {
  179. printk(KERN_CRIT "pas_pci_init: can't find root "
  180. "of device tree\n");
  181. return;
  182. }
  183. for (np = NULL; (np = of_get_next_child(root, np)) != NULL;)
  184. if (np->name && !strcmp(np->name, "pxp") && !pas_add_bridge(np))
  185. of_node_get(np);
  186. of_node_put(root);
  187. /* Setup the linkage between OF nodes and PHBs */
  188. pci_devs_phb_init();
  189. /* Use the common resource allocation mechanism */
  190. pci_probe_only = 1;
  191. }
  192. void __iomem *pasemi_pci_getcfgaddr(struct pci_dev *dev, int offset)
  193. {
  194. struct pci_controller *hose;
  195. hose = pci_bus_to_host(dev->bus);
  196. return (void __iomem *)pa_pxp_cfg_addr(hose, dev->bus->number, dev->devfn, offset);
  197. }