rtas_pci.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. #ifdef CONFIG_EEH
  63. if (pdn->edev && pdn->edev->pe &&
  64. (pdn->edev->pe->state & EEH_PE_CFG_BLOCKED))
  65. return PCIBIOS_SET_FAILED;
  66. #endif
  67. addr = rtas_config_addr(pdn->busno, pdn->devfn, where);
  68. buid = pdn->phb->buid;
  69. if (buid) {
  70. ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval,
  71. addr, BUID_HI(buid), BUID_LO(buid), size);
  72. } else {
  73. ret = rtas_call(read_pci_config, 2, 2, &returnval, addr, size);
  74. }
  75. *val = returnval;
  76. if (ret)
  77. return PCIBIOS_DEVICE_NOT_FOUND;
  78. return PCIBIOS_SUCCESSFUL;
  79. }
  80. static int rtas_pci_read_config(struct pci_bus *bus,
  81. unsigned int devfn,
  82. int where, int size, u32 *val)
  83. {
  84. struct device_node *busdn, *dn;
  85. struct pci_dn *pdn;
  86. bool found = false;
  87. int ret;
  88. /* Search only direct children of the bus */
  89. *val = 0xFFFFFFFF;
  90. busdn = pci_bus_to_OF_node(bus);
  91. for (dn = busdn->child; dn; dn = dn->sibling) {
  92. pdn = PCI_DN(dn);
  93. if (pdn && pdn->devfn == devfn
  94. && of_device_is_available(dn)) {
  95. found = true;
  96. break;
  97. }
  98. }
  99. if (!found)
  100. return PCIBIOS_DEVICE_NOT_FOUND;
  101. ret = rtas_read_config(pdn, where, size, val);
  102. if (*val == EEH_IO_ERROR_VALUE(size) &&
  103. eeh_dev_check_failure(of_node_to_eeh_dev(dn)))
  104. return PCIBIOS_DEVICE_NOT_FOUND;
  105. return ret;
  106. }
  107. int rtas_write_config(struct pci_dn *pdn, int where, int size, u32 val)
  108. {
  109. unsigned long buid, addr;
  110. int ret;
  111. if (!pdn)
  112. return PCIBIOS_DEVICE_NOT_FOUND;
  113. if (!config_access_valid(pdn, where))
  114. return PCIBIOS_BAD_REGISTER_NUMBER;
  115. #ifdef CONFIG_EEH
  116. if (pdn->edev && pdn->edev->pe &&
  117. (pdn->edev->pe->state & EEH_PE_CFG_BLOCKED))
  118. return PCIBIOS_SET_FAILED;
  119. #endif
  120. addr = rtas_config_addr(pdn->busno, pdn->devfn, where);
  121. buid = pdn->phb->buid;
  122. if (buid) {
  123. ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr,
  124. BUID_HI(buid), BUID_LO(buid), size, (ulong) val);
  125. } else {
  126. ret = rtas_call(write_pci_config, 3, 1, NULL, addr, size, (ulong)val);
  127. }
  128. if (ret)
  129. return PCIBIOS_DEVICE_NOT_FOUND;
  130. return PCIBIOS_SUCCESSFUL;
  131. }
  132. static int rtas_pci_write_config(struct pci_bus *bus,
  133. unsigned int devfn,
  134. int where, int size, u32 val)
  135. {
  136. struct device_node *busdn, *dn;
  137. struct pci_dn *pdn;
  138. bool found = false;
  139. /* Search only direct children of the bus */
  140. busdn = pci_bus_to_OF_node(bus);
  141. for (dn = busdn->child; dn; dn = dn->sibling) {
  142. pdn = PCI_DN(dn);
  143. if (pdn && pdn->devfn == devfn
  144. && of_device_is_available(dn)) {
  145. found = true;
  146. break;
  147. }
  148. }
  149. if (!found)
  150. return PCIBIOS_DEVICE_NOT_FOUND;
  151. return rtas_write_config(pdn, where, size, val);
  152. }
  153. static struct pci_ops rtas_pci_ops = {
  154. .read = rtas_pci_read_config,
  155. .write = rtas_pci_write_config,
  156. };
  157. static int is_python(struct device_node *dev)
  158. {
  159. const char *model = of_get_property(dev, "model", NULL);
  160. if (model && strstr(model, "Python"))
  161. return 1;
  162. return 0;
  163. }
  164. static void python_countermeasures(struct device_node *dev)
  165. {
  166. struct resource registers;
  167. void __iomem *chip_regs;
  168. volatile u32 val;
  169. if (of_address_to_resource(dev, 0, &registers)) {
  170. printk(KERN_ERR "Can't get address for Python workarounds !\n");
  171. return;
  172. }
  173. /* Python's register file is 1 MB in size. */
  174. chip_regs = ioremap(registers.start & ~(0xfffffUL), 0x100000);
  175. /*
  176. * Firmware doesn't always clear this bit which is critical
  177. * for good performance - Anton
  178. */
  179. #define PRG_CL_RESET_VALID 0x00010000
  180. val = in_be32(chip_regs + 0xf6030);
  181. if (val & PRG_CL_RESET_VALID) {
  182. printk(KERN_INFO "Python workaround: ");
  183. val &= ~PRG_CL_RESET_VALID;
  184. out_be32(chip_regs + 0xf6030, val);
  185. /*
  186. * We must read it back for changes to
  187. * take effect
  188. */
  189. val = in_be32(chip_regs + 0xf6030);
  190. printk("reg0: %x\n", val);
  191. }
  192. iounmap(chip_regs);
  193. }
  194. void __init init_pci_config_tokens(void)
  195. {
  196. read_pci_config = rtas_token("read-pci-config");
  197. write_pci_config = rtas_token("write-pci-config");
  198. ibm_read_pci_config = rtas_token("ibm,read-pci-config");
  199. ibm_write_pci_config = rtas_token("ibm,write-pci-config");
  200. }
  201. unsigned long get_phb_buid(struct device_node *phb)
  202. {
  203. struct resource r;
  204. if (ibm_read_pci_config == -1)
  205. return 0;
  206. if (of_address_to_resource(phb, 0, &r))
  207. return 0;
  208. return r.start;
  209. }
  210. static int phb_set_bus_ranges(struct device_node *dev,
  211. struct pci_controller *phb)
  212. {
  213. const __be32 *bus_range;
  214. unsigned int len;
  215. bus_range = of_get_property(dev, "bus-range", &len);
  216. if (bus_range == NULL || len < 2 * sizeof(int)) {
  217. return 1;
  218. }
  219. phb->first_busno = be32_to_cpu(bus_range[0]);
  220. phb->last_busno = be32_to_cpu(bus_range[1]);
  221. return 0;
  222. }
  223. int rtas_setup_phb(struct pci_controller *phb)
  224. {
  225. struct device_node *dev = phb->dn;
  226. if (is_python(dev))
  227. python_countermeasures(dev);
  228. if (phb_set_bus_ranges(dev, phb))
  229. return 1;
  230. phb->ops = &rtas_pci_ops;
  231. phb->buid = get_phb_buid(dev);
  232. return 0;
  233. }
  234. void __init find_and_init_phbs(void)
  235. {
  236. struct device_node *node;
  237. struct pci_controller *phb;
  238. struct device_node *root = of_find_node_by_path("/");
  239. for_each_child_of_node(root, node) {
  240. if (node->type == NULL || (strcmp(node->type, "pci") != 0 &&
  241. strcmp(node->type, "pciex") != 0))
  242. continue;
  243. phb = pcibios_alloc_controller(node);
  244. if (!phb)
  245. continue;
  246. rtas_setup_phb(phb);
  247. pci_process_bridge_OF_ranges(phb, node, 0);
  248. isa_bridge_find_early(phb);
  249. }
  250. of_node_put(root);
  251. pci_devs_phb_init();
  252. /*
  253. * PCI_PROBE_ONLY and PCI_REASSIGN_ALL_BUS can be set via properties
  254. * in chosen.
  255. */
  256. if (of_chosen) {
  257. const int *prop;
  258. prop = of_get_property(of_chosen,
  259. "linux,pci-probe-only", NULL);
  260. if (prop) {
  261. if (*prop)
  262. pci_add_flags(PCI_PROBE_ONLY);
  263. else
  264. pci_clear_flags(PCI_PROBE_ONLY);
  265. }
  266. #ifdef CONFIG_PPC32 /* Will be made generic soon */
  267. prop = of_get_property(of_chosen,
  268. "linux,pci-assign-all-buses", NULL);
  269. if (prop && *prop)
  270. pci_add_flags(PCI_REASSIGN_ALL_BUS);
  271. #endif /* CONFIG_PPC32 */
  272. }
  273. }