rtas_pci.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
  3. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  4. *
  5. * RTAS specific routines for PCI.
  6. *
  7. * Based on code from pci.c, chrp_pci.c and pSeries_pci.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/threads.h>
  25. #include <linux/pci.h>
  26. #include <linux/string.h>
  27. #include <linux/init.h>
  28. #include <asm/io.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/irq.h>
  31. #include <asm/prom.h>
  32. #include <asm/machdep.h>
  33. #include <asm/pci-bridge.h>
  34. #include <asm/iommu.h>
  35. #include <asm/rtas.h>
  36. #include <asm/mpic.h>
  37. #include <asm/ppc-pci.h>
  38. #include <asm/eeh.h>
  39. /* RTAS tokens */
  40. static int read_pci_config;
  41. static int write_pci_config;
  42. static int ibm_read_pci_config;
  43. static int ibm_write_pci_config;
  44. static inline int config_access_valid(struct pci_dn *dn, int where)
  45. {
  46. if (where < 256)
  47. return 1;
  48. if (where < 4096 && dn->pci_ext_config_space)
  49. return 1;
  50. return 0;
  51. }
  52. int rtas_read_config(struct pci_dn *pdn, int where, int size, u32 *val)
  53. {
  54. int returnval = -1;
  55. unsigned long buid, addr;
  56. int ret;
  57. if (!pdn)
  58. return PCIBIOS_DEVICE_NOT_FOUND;
  59. if (!config_access_valid(pdn, where))
  60. return PCIBIOS_BAD_REGISTER_NUMBER;
  61. #ifdef CONFIG_EEH
  62. if (pdn->edev && pdn->edev->pe &&
  63. (pdn->edev->pe->state & EEH_PE_CFG_BLOCKED))
  64. return PCIBIOS_SET_FAILED;
  65. #endif
  66. addr = rtas_config_addr(pdn->busno, pdn->devfn, where);
  67. buid = pdn->phb->buid;
  68. if (buid) {
  69. ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval,
  70. addr, BUID_HI(buid), BUID_LO(buid), size);
  71. } else {
  72. ret = rtas_call(read_pci_config, 2, 2, &returnval, addr, size);
  73. }
  74. *val = returnval;
  75. if (ret)
  76. return PCIBIOS_DEVICE_NOT_FOUND;
  77. return PCIBIOS_SUCCESSFUL;
  78. }
  79. static int rtas_pci_read_config(struct pci_bus *bus,
  80. unsigned int devfn,
  81. int where, int size, u32 *val)
  82. {
  83. struct device_node *busdn, *dn;
  84. struct pci_dn *pdn;
  85. bool found = false;
  86. int ret;
  87. /* Search only direct children of the bus */
  88. *val = 0xFFFFFFFF;
  89. busdn = pci_bus_to_OF_node(bus);
  90. for (dn = busdn->child; dn; dn = dn->sibling) {
  91. pdn = PCI_DN(dn);
  92. if (pdn && pdn->devfn == devfn
  93. && of_device_is_available(dn)) {
  94. found = true;
  95. break;
  96. }
  97. }
  98. if (!found)
  99. return PCIBIOS_DEVICE_NOT_FOUND;
  100. ret = rtas_read_config(pdn, where, size, val);
  101. if (*val == EEH_IO_ERROR_VALUE(size) &&
  102. eeh_dev_check_failure(pdn_to_eeh_dev(pdn)))
  103. return PCIBIOS_DEVICE_NOT_FOUND;
  104. return ret;
  105. }
  106. int rtas_write_config(struct pci_dn *pdn, int where, int size, u32 val)
  107. {
  108. unsigned long buid, addr;
  109. int ret;
  110. if (!pdn)
  111. return PCIBIOS_DEVICE_NOT_FOUND;
  112. if (!config_access_valid(pdn, where))
  113. return PCIBIOS_BAD_REGISTER_NUMBER;
  114. #ifdef CONFIG_EEH
  115. if (pdn->edev && pdn->edev->pe &&
  116. (pdn->edev->pe->state & EEH_PE_CFG_BLOCKED))
  117. return PCIBIOS_SET_FAILED;
  118. #endif
  119. addr = rtas_config_addr(pdn->busno, pdn->devfn, where);
  120. buid = pdn->phb->buid;
  121. if (buid) {
  122. ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr,
  123. BUID_HI(buid), BUID_LO(buid), size, (ulong) val);
  124. } else {
  125. ret = rtas_call(write_pci_config, 3, 1, NULL, addr, size, (ulong)val);
  126. }
  127. if (ret)
  128. return PCIBIOS_DEVICE_NOT_FOUND;
  129. return PCIBIOS_SUCCESSFUL;
  130. }
  131. static int rtas_pci_write_config(struct pci_bus *bus,
  132. unsigned int devfn,
  133. int where, int size, u32 val)
  134. {
  135. struct device_node *busdn, *dn;
  136. struct pci_dn *pdn;
  137. bool found = false;
  138. /* Search only direct children of the bus */
  139. busdn = pci_bus_to_OF_node(bus);
  140. for (dn = busdn->child; dn; dn = dn->sibling) {
  141. pdn = PCI_DN(dn);
  142. if (pdn && pdn->devfn == devfn
  143. && of_device_is_available(dn)) {
  144. found = true;
  145. break;
  146. }
  147. }
  148. if (!found)
  149. return PCIBIOS_DEVICE_NOT_FOUND;
  150. return rtas_write_config(pdn, where, size, val);
  151. }
  152. static struct pci_ops rtas_pci_ops = {
  153. .read = rtas_pci_read_config,
  154. .write = rtas_pci_write_config,
  155. };
  156. static int is_python(struct device_node *dev)
  157. {
  158. const char *model = of_get_property(dev, "model", NULL);
  159. if (model && strstr(model, "Python"))
  160. return 1;
  161. return 0;
  162. }
  163. static void python_countermeasures(struct device_node *dev)
  164. {
  165. struct resource registers;
  166. void __iomem *chip_regs;
  167. volatile u32 val;
  168. if (of_address_to_resource(dev, 0, &registers)) {
  169. printk(KERN_ERR "Can't get address for Python workarounds !\n");
  170. return;
  171. }
  172. /* Python's register file is 1 MB in size. */
  173. chip_regs = ioremap(registers.start & ~(0xfffffUL), 0x100000);
  174. /*
  175. * Firmware doesn't always clear this bit which is critical
  176. * for good performance - Anton
  177. */
  178. #define PRG_CL_RESET_VALID 0x00010000
  179. val = in_be32(chip_regs + 0xf6030);
  180. if (val & PRG_CL_RESET_VALID) {
  181. printk(KERN_INFO "Python workaround: ");
  182. val &= ~PRG_CL_RESET_VALID;
  183. out_be32(chip_regs + 0xf6030, val);
  184. /*
  185. * We must read it back for changes to
  186. * take effect
  187. */
  188. val = in_be32(chip_regs + 0xf6030);
  189. printk("reg0: %x\n", val);
  190. }
  191. iounmap(chip_regs);
  192. }
  193. void __init init_pci_config_tokens(void)
  194. {
  195. read_pci_config = rtas_token("read-pci-config");
  196. write_pci_config = rtas_token("write-pci-config");
  197. ibm_read_pci_config = rtas_token("ibm,read-pci-config");
  198. ibm_write_pci_config = rtas_token("ibm,write-pci-config");
  199. }
  200. unsigned long get_phb_buid(struct device_node *phb)
  201. {
  202. struct resource r;
  203. if (ibm_read_pci_config == -1)
  204. return 0;
  205. if (of_address_to_resource(phb, 0, &r))
  206. return 0;
  207. return r.start;
  208. }
  209. static int phb_set_bus_ranges(struct device_node *dev,
  210. struct pci_controller *phb)
  211. {
  212. const __be32 *bus_range;
  213. unsigned int len;
  214. bus_range = of_get_property(dev, "bus-range", &len);
  215. if (bus_range == NULL || len < 2 * sizeof(int)) {
  216. return 1;
  217. }
  218. phb->first_busno = be32_to_cpu(bus_range[0]);
  219. phb->last_busno = be32_to_cpu(bus_range[1]);
  220. return 0;
  221. }
  222. int rtas_setup_phb(struct pci_controller *phb)
  223. {
  224. struct device_node *dev = phb->dn;
  225. if (is_python(dev))
  226. python_countermeasures(dev);
  227. if (phb_set_bus_ranges(dev, phb))
  228. return 1;
  229. phb->ops = &rtas_pci_ops;
  230. phb->buid = get_phb_buid(dev);
  231. return 0;
  232. }