rtas_pci.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 <linux/bootmem.h>
  29. #include <asm/io.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/irq.h>
  32. #include <asm/prom.h>
  33. #include <asm/machdep.h>
  34. #include <asm/pci-bridge.h>
  35. #include <asm/iommu.h>
  36. #include <asm/rtas.h>
  37. #include <asm/mpic.h>
  38. #include <asm/ppc-pci.h>
  39. #include <asm/eeh.h>
  40. /* RTAS tokens */
  41. static int read_pci_config;
  42. static int write_pci_config;
  43. static int ibm_read_pci_config;
  44. static int ibm_write_pci_config;
  45. static inline int config_access_valid(struct pci_dn *dn, int where)
  46. {
  47. if (where < 256)
  48. return 1;
  49. if (where < 4096 && dn->pci_ext_config_space)
  50. return 1;
  51. return 0;
  52. }
  53. int rtas_read_config(struct pci_dn *pdn, int where, int size, u32 *val)
  54. {
  55. int returnval = -1;
  56. unsigned long buid, addr;
  57. int ret;
  58. if (!pdn)
  59. return PCIBIOS_DEVICE_NOT_FOUND;
  60. if (!config_access_valid(pdn, where))
  61. return PCIBIOS_BAD_REGISTER_NUMBER;
  62. addr = rtas_config_addr(pdn->busno, pdn->devfn, where);
  63. buid = pdn->phb->buid;
  64. if (buid) {
  65. ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval,
  66. addr, BUID_HI(buid), BUID_LO(buid), size);
  67. } else {
  68. ret = rtas_call(read_pci_config, 2, 2, &returnval, addr, size);
  69. }
  70. *val = returnval;
  71. if (ret)
  72. return PCIBIOS_DEVICE_NOT_FOUND;
  73. if (returnval == EEH_IO_ERROR_VALUE(size) &&
  74. eeh_dn_check_failure (pdn->node, NULL))
  75. return PCIBIOS_DEVICE_NOT_FOUND;
  76. return PCIBIOS_SUCCESSFUL;
  77. }
  78. static int rtas_pci_read_config(struct pci_bus *bus,
  79. unsigned int devfn,
  80. int where, int size, u32 *val)
  81. {
  82. struct device_node *busdn, *dn;
  83. if (bus->self)
  84. busdn = pci_device_to_OF_node(bus->self);
  85. else
  86. busdn = bus->sysdata; /* must be a phb */
  87. /* Search only direct children of the bus */
  88. for (dn = busdn->child; dn; dn = dn->sibling) {
  89. struct pci_dn *pdn = PCI_DN(dn);
  90. if (pdn && pdn->devfn == devfn
  91. && of_device_is_available(dn))
  92. return rtas_read_config(pdn, where, size, val);
  93. }
  94. return PCIBIOS_DEVICE_NOT_FOUND;
  95. }
  96. int rtas_write_config(struct pci_dn *pdn, int where, int size, u32 val)
  97. {
  98. unsigned long buid, addr;
  99. int ret;
  100. if (!pdn)
  101. return PCIBIOS_DEVICE_NOT_FOUND;
  102. if (!config_access_valid(pdn, where))
  103. return PCIBIOS_BAD_REGISTER_NUMBER;
  104. addr = rtas_config_addr(pdn->busno, pdn->devfn, where);
  105. buid = pdn->phb->buid;
  106. if (buid) {
  107. ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr,
  108. BUID_HI(buid), BUID_LO(buid), size, (ulong) val);
  109. } else {
  110. ret = rtas_call(write_pci_config, 3, 1, NULL, addr, size, (ulong)val);
  111. }
  112. if (ret)
  113. return PCIBIOS_DEVICE_NOT_FOUND;
  114. return PCIBIOS_SUCCESSFUL;
  115. }
  116. static int rtas_pci_write_config(struct pci_bus *bus,
  117. unsigned int devfn,
  118. int where, int size, u32 val)
  119. {
  120. struct device_node *busdn, *dn;
  121. if (bus->self)
  122. busdn = pci_device_to_OF_node(bus->self);
  123. else
  124. busdn = bus->sysdata; /* must be a phb */
  125. /* Search only direct children of the bus */
  126. for (dn = busdn->child; dn; dn = dn->sibling) {
  127. struct pci_dn *pdn = PCI_DN(dn);
  128. if (pdn && pdn->devfn == devfn
  129. && of_device_is_available(dn))
  130. return rtas_write_config(pdn, where, size, val);
  131. }
  132. return PCIBIOS_DEVICE_NOT_FOUND;
  133. }
  134. static struct pci_ops rtas_pci_ops = {
  135. .read = rtas_pci_read_config,
  136. .write = rtas_pci_write_config,
  137. };
  138. static int is_python(struct device_node *dev)
  139. {
  140. const char *model = of_get_property(dev, "model", NULL);
  141. if (model && strstr(model, "Python"))
  142. return 1;
  143. return 0;
  144. }
  145. static void python_countermeasures(struct device_node *dev)
  146. {
  147. struct resource registers;
  148. void __iomem *chip_regs;
  149. volatile u32 val;
  150. if (of_address_to_resource(dev, 0, &registers)) {
  151. printk(KERN_ERR "Can't get address for Python workarounds !\n");
  152. return;
  153. }
  154. /* Python's register file is 1 MB in size. */
  155. chip_regs = ioremap(registers.start & ~(0xfffffUL), 0x100000);
  156. /*
  157. * Firmware doesn't always clear this bit which is critical
  158. * for good performance - Anton
  159. */
  160. #define PRG_CL_RESET_VALID 0x00010000
  161. val = in_be32(chip_regs + 0xf6030);
  162. if (val & PRG_CL_RESET_VALID) {
  163. printk(KERN_INFO "Python workaround: ");
  164. val &= ~PRG_CL_RESET_VALID;
  165. out_be32(chip_regs + 0xf6030, val);
  166. /*
  167. * We must read it back for changes to
  168. * take effect
  169. */
  170. val = in_be32(chip_regs + 0xf6030);
  171. printk("reg0: %x\n", val);
  172. }
  173. iounmap(chip_regs);
  174. }
  175. void __init init_pci_config_tokens (void)
  176. {
  177. read_pci_config = rtas_token("read-pci-config");
  178. write_pci_config = rtas_token("write-pci-config");
  179. ibm_read_pci_config = rtas_token("ibm,read-pci-config");
  180. ibm_write_pci_config = rtas_token("ibm,write-pci-config");
  181. }
  182. unsigned long __devinit get_phb_buid (struct device_node *phb)
  183. {
  184. struct resource r;
  185. if (ibm_read_pci_config == -1)
  186. return 0;
  187. if (of_address_to_resource(phb, 0, &r))
  188. return 0;
  189. return r.start;
  190. }
  191. static int phb_set_bus_ranges(struct device_node *dev,
  192. struct pci_controller *phb)
  193. {
  194. const int *bus_range;
  195. unsigned int len;
  196. bus_range = of_get_property(dev, "bus-range", &len);
  197. if (bus_range == NULL || len < 2 * sizeof(int)) {
  198. return 1;
  199. }
  200. phb->first_busno = bus_range[0];
  201. phb->last_busno = bus_range[1];
  202. return 0;
  203. }
  204. int __devinit rtas_setup_phb(struct pci_controller *phb)
  205. {
  206. struct device_node *dev = phb->dn;
  207. if (is_python(dev))
  208. python_countermeasures(dev);
  209. if (phb_set_bus_ranges(dev, phb))
  210. return 1;
  211. phb->ops = &rtas_pci_ops;
  212. phb->buid = get_phb_buid(dev);
  213. return 0;
  214. }
  215. void __init find_and_init_phbs(void)
  216. {
  217. struct device_node *node;
  218. struct pci_controller *phb;
  219. struct device_node *root = of_find_node_by_path("/");
  220. for_each_child_of_node(root, node) {
  221. if (node->type == NULL || (strcmp(node->type, "pci") != 0 &&
  222. strcmp(node->type, "pciex") != 0))
  223. continue;
  224. phb = pcibios_alloc_controller(node);
  225. if (!phb)
  226. continue;
  227. rtas_setup_phb(phb);
  228. pci_process_bridge_OF_ranges(phb, node, 0);
  229. isa_bridge_find_early(phb);
  230. }
  231. of_node_put(root);
  232. pci_devs_phb_init();
  233. /*
  234. * pci_probe_only and pci_assign_all_buses can be set via properties
  235. * in chosen.
  236. */
  237. if (of_chosen) {
  238. const int *prop;
  239. prop = of_get_property(of_chosen,
  240. "linux,pci-probe-only", NULL);
  241. if (prop)
  242. pci_probe_only = *prop;
  243. #ifdef CONFIG_PPC32 /* Will be made generic soon */
  244. prop = of_get_property(of_chosen,
  245. "linux,pci-assign-all-buses", NULL);
  246. if (prop && *prop)
  247. ppc_pci_flags |= PPC_PCI_REASSIGN_ALL_BUS;
  248. #endif /* CONFIG_PPC32 */
  249. }
  250. }