pcie_indirect.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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, int offset,
  27. int len,u32 *val)
  28. {
  29. int bus = PCI_BUS(dev);
  30. volatile unsigned char *cfg_data;
  31. u32 temp;
  32. PEX_FIX;
  33. if( bus == 0xff) {
  34. PCI_CFG_OUT(hose->cfg_addr, dev | (offset & 0xfc) | 0x80000001);
  35. }else {
  36. PCI_CFG_OUT(hose->cfg_addr, dev | (offset & 0xfc) | 0x80000000);
  37. }
  38. /*
  39. * Note: the caller has already checked that offset is
  40. * suitably aligned and that len is 1, 2 or 4.
  41. */
  42. /* ERRATA PCI-Ex 12 - Configuration Address/Data Alignment */
  43. cfg_data = hose->cfg_data;
  44. PEX_FIX;
  45. temp = in_le32((u32 *)cfg_data);
  46. switch (len) {
  47. case 1:
  48. *val = (temp >> (((offset & 3))*8)) & 0xff;
  49. break;
  50. case 2:
  51. *val = (temp >> (((offset & 3))*8)) & 0xffff;
  52. break;
  53. default:
  54. *val = temp;
  55. break;
  56. }
  57. return 0;
  58. }
  59. static int
  60. indirect_write_config_pcie(struct pci_controller *hose,
  61. pci_dev_t dev,
  62. int offset,
  63. int len,
  64. u32 val)
  65. {
  66. int bus = PCI_BUS(dev);
  67. volatile unsigned char *cfg_data;
  68. u32 temp;
  69. PEX_FIX;
  70. if( bus == 0xff) {
  71. PCI_CFG_OUT(hose->cfg_addr, dev | (offset & 0xfc) | 0x80000001);
  72. }else {
  73. PCI_CFG_OUT(hose->cfg_addr, dev | (offset & 0xfc) | 0x80000000);
  74. }
  75. /*
  76. * Note: the caller has already checked that offset is
  77. * suitably aligned and that len is 1, 2 or 4.
  78. */
  79. /* ERRATA PCI-Ex 12 - Configuration Address/Data Alignment */
  80. cfg_data = hose->cfg_data;
  81. switch (len) {
  82. case 1:
  83. PEX_FIX;
  84. temp = in_le32((u32 *)cfg_data);
  85. temp = (temp & ~(0xff << ((offset & 3) * 8))) |
  86. (val << ((offset & 3) * 8));
  87. PEX_FIX;
  88. out_le32((u32 *)cfg_data, temp);
  89. break;
  90. case 2:
  91. PEX_FIX;
  92. temp = in_le32((u32 *)cfg_data);
  93. temp = (temp & ~(0xffff << ((offset & 3) * 8)));
  94. temp |= (val << ((offset & 3) * 8)) ;
  95. PEX_FIX;
  96. out_le32((u32 *)cfg_data, temp);
  97. break;
  98. default:
  99. PEX_FIX;
  100. out_le32((u32 *)cfg_data, val);
  101. break;
  102. }
  103. PEX_FIX;
  104. return 0;
  105. }
  106. static int
  107. indirect_read_config_byte_pcie(struct pci_controller *hose,
  108. pci_dev_t dev,
  109. int offset,
  110. u8 *val)
  111. {
  112. u32 val32;
  113. indirect_read_config_pcie(hose,dev,offset,1,&val32);
  114. *val = (u8)val32;
  115. return 0;
  116. }
  117. static int
  118. indirect_read_config_word_pcie(struct pci_controller *hose,
  119. pci_dev_t dev,
  120. int offset,
  121. u16 *val)
  122. {
  123. u32 val32;
  124. indirect_read_config_pcie(hose,dev,offset,2,&val32);
  125. *val = (u16)val32;
  126. return 0;
  127. }
  128. static int
  129. indirect_read_config_dword_pcie(struct pci_controller *hose,
  130. pci_dev_t dev,
  131. int offset,
  132. u32 *val)
  133. {
  134. return indirect_read_config_pcie(hose,dev, offset,4,val);
  135. }
  136. static int
  137. indirect_write_config_byte_pcie(struct pci_controller *hose,
  138. pci_dev_t dev,
  139. int offset,
  140. u8 val)
  141. {
  142. return indirect_write_config_pcie(hose,dev, offset,1,(u32)val);
  143. }
  144. static int
  145. indirect_write_config_word_pcie(struct pci_controller *hose,
  146. pci_dev_t dev,
  147. int offset,
  148. unsigned short val)
  149. {
  150. return indirect_write_config_pcie(hose,dev, offset,2,(u32)val);
  151. }
  152. static int
  153. indirect_write_config_dword_pcie(struct pci_controller *hose,
  154. pci_dev_t dev,
  155. int offset,
  156. u32 val)
  157. {
  158. return indirect_write_config_pcie(hose,dev, offset,4,val);
  159. }
  160. void
  161. pcie_setup_indirect(struct pci_controller* hose,
  162. u32 cfg_addr,
  163. u32 cfg_data)
  164. {
  165. pci_set_ops(hose,
  166. indirect_read_config_byte_pcie,
  167. indirect_read_config_word_pcie,
  168. indirect_read_config_dword_pcie,
  169. indirect_write_config_byte_pcie,
  170. indirect_write_config_word_pcie,
  171. indirect_write_config_dword_pcie);
  172. hose->cfg_addr = (unsigned int *) cfg_addr;
  173. hose->cfg_data = (unsigned char *) cfg_data;
  174. }
  175. #endif /* CONFIG_PCI */