pcihp_slot.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 1995,2001 Compaq Computer Corporation
  3. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  4. * Copyright (C) 2001 IBM Corp.
  5. * Copyright (C) 2003-2004 Intel Corporation
  6. * (c) Copyright 2009 Hewlett-Packard Development Company, L.P.
  7. *
  8. * All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  18. * NON INFRINGEMENT. See the GNU General Public License for more
  19. * details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/pci.h>
  26. #include <linux/export.h>
  27. #include <linux/pci_hotplug.h>
  28. static struct hpp_type0 pci_default_type0 = {
  29. .revision = 1,
  30. .cache_line_size = 8,
  31. .latency_timer = 0x40,
  32. .enable_serr = 0,
  33. .enable_perr = 0,
  34. };
  35. static void program_hpp_type0(struct pci_dev *dev, struct hpp_type0 *hpp)
  36. {
  37. u16 pci_cmd, pci_bctl;
  38. if (!hpp) {
  39. /*
  40. * Perhaps we *should* use default settings for PCIe, but
  41. * pciehp didn't, so we won't either.
  42. */
  43. if (pci_is_pcie(dev))
  44. return;
  45. dev_info(&dev->dev, "using default PCI settings\n");
  46. hpp = &pci_default_type0;
  47. }
  48. if (hpp->revision > 1) {
  49. dev_warn(&dev->dev,
  50. "PCI settings rev %d not supported; using defaults\n",
  51. hpp->revision);
  52. hpp = &pci_default_type0;
  53. }
  54. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, hpp->cache_line_size);
  55. pci_write_config_byte(dev, PCI_LATENCY_TIMER, hpp->latency_timer);
  56. pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
  57. if (hpp->enable_serr)
  58. pci_cmd |= PCI_COMMAND_SERR;
  59. else
  60. pci_cmd &= ~PCI_COMMAND_SERR;
  61. if (hpp->enable_perr)
  62. pci_cmd |= PCI_COMMAND_PARITY;
  63. else
  64. pci_cmd &= ~PCI_COMMAND_PARITY;
  65. pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
  66. /* Program bridge control value */
  67. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  68. pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
  69. hpp->latency_timer);
  70. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
  71. if (hpp->enable_serr)
  72. pci_bctl |= PCI_BRIDGE_CTL_SERR;
  73. else
  74. pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
  75. if (hpp->enable_perr)
  76. pci_bctl |= PCI_BRIDGE_CTL_PARITY;
  77. else
  78. pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
  79. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
  80. }
  81. }
  82. static void program_hpp_type1(struct pci_dev *dev, struct hpp_type1 *hpp)
  83. {
  84. if (hpp)
  85. dev_warn(&dev->dev, "PCI-X settings not supported\n");
  86. }
  87. static void program_hpp_type2(struct pci_dev *dev, struct hpp_type2 *hpp)
  88. {
  89. int pos;
  90. u16 reg16;
  91. u32 reg32;
  92. if (!hpp)
  93. return;
  94. /* Find PCI Express capability */
  95. pos = pci_pcie_cap(dev);
  96. if (!pos)
  97. return;
  98. if (hpp->revision > 1) {
  99. dev_warn(&dev->dev, "PCIe settings rev %d not supported\n",
  100. hpp->revision);
  101. return;
  102. }
  103. /* Initialize Device Control Register */
  104. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &reg16);
  105. reg16 = (reg16 & hpp->pci_exp_devctl_and) | hpp->pci_exp_devctl_or;
  106. pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, reg16);
  107. /* Initialize Link Control Register */
  108. if (dev->subordinate) {
  109. pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &reg16);
  110. reg16 = (reg16 & hpp->pci_exp_lnkctl_and)
  111. | hpp->pci_exp_lnkctl_or;
  112. pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, reg16);
  113. }
  114. /* Find Advanced Error Reporting Enhanced Capability */
  115. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  116. if (!pos)
  117. return;
  118. /* Initialize Uncorrectable Error Mask Register */
  119. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &reg32);
  120. reg32 = (reg32 & hpp->unc_err_mask_and) | hpp->unc_err_mask_or;
  121. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, reg32);
  122. /* Initialize Uncorrectable Error Severity Register */
  123. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &reg32);
  124. reg32 = (reg32 & hpp->unc_err_sever_and) | hpp->unc_err_sever_or;
  125. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, reg32);
  126. /* Initialize Correctable Error Mask Register */
  127. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg32);
  128. reg32 = (reg32 & hpp->cor_err_mask_and) | hpp->cor_err_mask_or;
  129. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg32);
  130. /* Initialize Advanced Error Capabilities and Control Register */
  131. pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
  132. reg32 = (reg32 & hpp->adv_err_cap_and) | hpp->adv_err_cap_or;
  133. pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
  134. /*
  135. * FIXME: The following two registers are not supported yet.
  136. *
  137. * o Secondary Uncorrectable Error Severity Register
  138. * o Secondary Uncorrectable Error Mask Register
  139. */
  140. }
  141. void pci_configure_slot(struct pci_dev *dev)
  142. {
  143. struct pci_dev *cdev;
  144. struct hotplug_params hpp;
  145. int ret;
  146. if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
  147. (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  148. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
  149. return;
  150. if (dev->bus && dev->bus->self)
  151. pcie_bus_configure_settings(dev->bus,
  152. dev->bus->self->pcie_mpss);
  153. memset(&hpp, 0, sizeof(hpp));
  154. ret = pci_get_hp_params(dev, &hpp);
  155. if (ret)
  156. dev_warn(&dev->dev, "no hotplug settings from platform\n");
  157. program_hpp_type2(dev, hpp.t2);
  158. program_hpp_type1(dev, hpp.t1);
  159. program_hpp_type0(dev, hpp.t0);
  160. if (dev->subordinate) {
  161. list_for_each_entry(cdev, &dev->subordinate->devices,
  162. bus_list)
  163. pci_configure_slot(cdev);
  164. }
  165. }
  166. EXPORT_SYMBOL_GPL(pci_configure_slot);