pci.c 5.6 KB

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