pcie_indirect.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Support for indirect PCI bridges.
  3. *
  4. * Copyright (c) Freescale Semiconductor, Inc.
  5. * 2006. All rights reserved.
  6. *
  7. * Jason Jin <Jason.jin@freescale.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * partly derived from
  15. * arch/powerpc/platforms/86xx/mpc86xx_pcie.c
  16. */
  17. #include <common.h>
  18. #ifdef CONFIG_PCI
  19. #include <asm/processor.h>
  20. #include <asm/io.h>
  21. #include <pci.h>
  22. #define PCI_CFG_OUT out_be32
  23. #define PEX_FIX out_be32(hose->cfg_addr+0x4, 0x0400ffff)
  24. static int
  25. indirect_read_config_pcie(struct pci_controller *hose,
  26. pci_dev_t dev,
  27. int offset,
  28. int len,
  29. u32 *val)
  30. {
  31. int bus = PCI_BUS(dev);
  32. volatile unsigned char *cfg_data;
  33. u32 temp;
  34. PEX_FIX;
  35. if (bus == 0xff) {
  36. PCI_CFG_OUT(hose->cfg_addr,
  37. dev | (offset & 0xfc) | 0x80000001);
  38. } else {
  39. PCI_CFG_OUT(hose->cfg_addr,
  40. dev | (offset & 0xfc) | 0x80000000);
  41. }
  42. /*
  43. * Note: the caller has already checked that offset is
  44. * suitably aligned and that len is 1, 2 or 4.
  45. */
  46. /* ERRATA PCI-Ex 12 - Configuration Address/Data Alignment */
  47. cfg_data = hose->cfg_data;
  48. PEX_FIX;
  49. temp = in_le32((u32 *) cfg_data);
  50. switch (len) {
  51. case 1:
  52. *val = (temp >> (((offset & 3)) * 8)) & 0xff;
  53. break;
  54. case 2:
  55. *val = (temp >> (((offset & 3)) * 8)) & 0xffff;
  56. break;
  57. default:
  58. *val = temp;
  59. break;
  60. }
  61. return 0;
  62. }
  63. static int
  64. indirect_write_config_pcie(struct pci_controller *hose,
  65. pci_dev_t dev,
  66. int offset,
  67. int len,
  68. u32 val)
  69. {
  70. int bus = PCI_BUS(dev);
  71. volatile unsigned char *cfg_data;
  72. u32 temp;
  73. PEX_FIX;
  74. if (bus == 0xff) {
  75. PCI_CFG_OUT(hose->cfg_addr,
  76. dev | (offset & 0xfc) | 0x80000001);
  77. } else {
  78. PCI_CFG_OUT(hose->cfg_addr,
  79. dev | (offset & 0xfc) | 0x80000000);
  80. }
  81. /*
  82. * Note: the caller has already checked that offset is
  83. * suitably aligned and that len is 1, 2 or 4.
  84. */
  85. /* ERRATA PCI-Ex 12 - Configuration Address/Data Alignment */
  86. cfg_data = hose->cfg_data;
  87. switch (len) {
  88. case 1:
  89. PEX_FIX;
  90. temp = in_le32((u32 *) cfg_data);
  91. temp = (temp & ~(0xff << ((offset & 3) * 8))) |
  92. (val << ((offset & 3) * 8));
  93. PEX_FIX;
  94. out_le32((u32 *) cfg_data, temp);
  95. break;
  96. case 2:
  97. PEX_FIX;
  98. temp = in_le32((u32 *) cfg_data);
  99. temp = (temp & ~(0xffff << ((offset & 3) * 8)));
  100. temp |= (val << ((offset & 3) * 8));
  101. PEX_FIX;
  102. out_le32((u32 *) cfg_data, temp);
  103. break;
  104. default:
  105. PEX_FIX;
  106. out_le32((u32 *) cfg_data, val);
  107. break;
  108. }
  109. PEX_FIX;
  110. return 0;
  111. }
  112. static int
  113. indirect_read_config_byte_pcie(struct pci_controller *hose,
  114. pci_dev_t dev,
  115. int offset,
  116. u8 *val)
  117. {
  118. u32 val32;
  119. indirect_read_config_pcie(hose, dev, offset, 1, &val32);
  120. *val = (u8) val32;
  121. return 0;
  122. }
  123. static int
  124. indirect_read_config_word_pcie(struct pci_controller *hose,
  125. pci_dev_t dev,
  126. int offset,
  127. u16 *val)
  128. {
  129. u32 val32;
  130. indirect_read_config_pcie(hose, dev, offset, 2, &val32);
  131. *val = (u16) val32;
  132. return 0;
  133. }
  134. static int
  135. indirect_read_config_dword_pcie(struct pci_controller *hose,
  136. pci_dev_t dev,
  137. int offset,
  138. u32 *val)
  139. {
  140. return indirect_read_config_pcie(hose, dev, offset, 4, val);
  141. }
  142. static int
  143. indirect_write_config_byte_pcie(struct pci_controller *hose,
  144. pci_dev_t dev,
  145. int offset,
  146. u8 val)
  147. {
  148. return indirect_write_config_pcie(hose, dev, offset, 1, (u32) val);
  149. }
  150. static int
  151. indirect_write_config_word_pcie(struct pci_controller *hose,
  152. pci_dev_t dev,
  153. int offset,
  154. unsigned short val)
  155. {
  156. return indirect_write_config_pcie(hose, dev, offset, 2, (u32) val);
  157. }
  158. static int
  159. indirect_write_config_dword_pcie(struct pci_controller *hose,
  160. pci_dev_t dev,
  161. int offset,
  162. u32 val)
  163. {
  164. return indirect_write_config_pcie(hose, dev, offset, 4, val);
  165. }
  166. void
  167. pcie_setup_indirect(struct pci_controller *hose, u32 cfg_addr, u32 cfg_data)
  168. {
  169. pci_set_ops(hose,
  170. indirect_read_config_byte_pcie,
  171. indirect_read_config_word_pcie,
  172. indirect_read_config_dword_pcie,
  173. indirect_write_config_byte_pcie,
  174. indirect_write_config_word_pcie,
  175. indirect_write_config_dword_pcie);
  176. hose->cfg_addr = (unsigned int *)cfg_addr;
  177. hose->cfg_data = (unsigned char *)cfg_data;
  178. }
  179. #endif /* CONFIG_PCI */