pci_mpc85xx.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) Copyright 2019
  4. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  5. *
  6. */
  7. #include <common.h>
  8. #include <asm/bitops.h>
  9. #include <pci.h>
  10. #include <dm.h>
  11. #include <asm/fsl_law.h>
  12. struct mpc85xx_pci_priv {
  13. void __iomem *cfg_addr;
  14. void __iomem *cfg_data;
  15. };
  16. static int mpc85xx_pci_dm_read_config(const struct udevice *dev, pci_dev_t bdf,
  17. uint offset, ulong *value,
  18. enum pci_size_t size)
  19. {
  20. struct mpc85xx_pci_priv *priv = dev_get_priv(dev);
  21. u32 addr;
  22. if (offset > 0xff) {
  23. *value = pci_get_ff(size);
  24. return 0;
  25. }
  26. /* Skip mpc85xx PCI controller's ATMU inbound registers */
  27. if (PCI_BUS(bdf) == 0 && PCI_DEV(bdf) == 0 && PCI_FUNC(bdf) == 0 &&
  28. (offset & ~3) >= PCI_BASE_ADDRESS_0 && (offset & ~3) <= PCI_BASE_ADDRESS_5) {
  29. *value = 0;
  30. return 0;
  31. }
  32. addr = PCI_CONF1_ADDRESS(PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), offset);
  33. out_be32(priv->cfg_addr, addr);
  34. sync();
  35. switch (size) {
  36. case PCI_SIZE_8:
  37. *value = in_8(priv->cfg_data + (offset & 3));
  38. break;
  39. case PCI_SIZE_16:
  40. *value = in_le16(priv->cfg_data + (offset & 2));
  41. break;
  42. case PCI_SIZE_32:
  43. *value = in_le32(priv->cfg_data);
  44. break;
  45. }
  46. return 0;
  47. }
  48. static int mpc85xx_pci_dm_write_config(struct udevice *dev, pci_dev_t bdf,
  49. uint offset, ulong value,
  50. enum pci_size_t size)
  51. {
  52. struct mpc85xx_pci_priv *priv = dev_get_priv(dev);
  53. u32 addr;
  54. if (offset > 0xff)
  55. return 0;
  56. /* Skip mpc85xx PCI controller's ATMU inbound registers */
  57. if (PCI_BUS(bdf) == 0 && PCI_DEV(bdf) == 0 && PCI_FUNC(bdf) == 0 &&
  58. (offset & ~3) >= PCI_BASE_ADDRESS_0 && (offset & ~3) <= PCI_BASE_ADDRESS_5)
  59. return 0;
  60. addr = PCI_CONF1_ADDRESS(PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), offset);
  61. out_be32(priv->cfg_addr, addr);
  62. sync();
  63. switch (size) {
  64. case PCI_SIZE_8:
  65. out_8(priv->cfg_data + (offset & 3), value);
  66. break;
  67. case PCI_SIZE_16:
  68. out_le16(priv->cfg_data + (offset & 2), value);
  69. break;
  70. case PCI_SIZE_32:
  71. out_le32(priv->cfg_data, value);
  72. break;
  73. }
  74. sync();
  75. return 0;
  76. }
  77. #ifdef CONFIG_FSL_LAW
  78. static int
  79. mpc85xx_pci_dm_setup_laws(struct pci_region *io, struct pci_region *mem,
  80. struct pci_region *pre)
  81. {
  82. /*
  83. * Unfortunately we have defines for this addresse,
  84. * as we have to setup the TLB, and at this stage
  85. * we have no access to DT ... may we check here
  86. * if the value in the define is the same ?
  87. */
  88. if (mem)
  89. set_next_law(mem->phys_start, law_size_bits(mem->size),
  90. LAW_TRGT_IF_PCI);
  91. if (io)
  92. set_next_law(io->phys_start, law_size_bits(io->size),
  93. LAW_TRGT_IF_PCI);
  94. if (pre)
  95. set_next_law(pre->phys_start, law_size_bits(pre->size),
  96. LAW_TRGT_IF_PCI);
  97. return 0;
  98. }
  99. #endif
  100. static int mpc85xx_pci_dm_probe(struct udevice *dev)
  101. {
  102. struct mpc85xx_pci_priv *priv = dev_get_priv(dev);
  103. struct pci_region *io;
  104. struct pci_region *mem;
  105. struct pci_region *pre;
  106. int count;
  107. ccsr_pcix_t *pcix;
  108. count = pci_get_regions(dev, &io, &mem, &pre);
  109. if (count != 2) {
  110. printf("%s: wrong count of regions %d only 2 allowed\n",
  111. __func__, count);
  112. return -EINVAL;
  113. }
  114. #ifdef CONFIG_FSL_LAW
  115. mpc85xx_pci_dm_setup_laws(io, mem, pre);
  116. #endif
  117. pcix = priv->cfg_addr;
  118. /* BAR 1: memory */
  119. out_be32(&pcix->potar1, mem->bus_start >> 12);
  120. out_be32(&pcix->potear1, (u64)mem->bus_start >> 44);
  121. out_be32(&pcix->powbar1, mem->phys_start >> 12);
  122. out_be32(&pcix->powbear1, (u64)mem->phys_start >> 44);
  123. out_be32(&pcix->powar1, (POWAR_EN | POWAR_MEM_READ |
  124. POWAR_MEM_WRITE | (__ilog2(mem->size) - 1)));
  125. /* BAR 1: IO */
  126. out_be32(&pcix->potar2, io->bus_start >> 12);
  127. out_be32(&pcix->potear2, (u64)io->bus_start >> 44);
  128. out_be32(&pcix->powbar2, io->phys_start >> 12);
  129. out_be32(&pcix->powbear2, (u64)io->phys_start >> 44);
  130. out_be32(&pcix->powar2, (POWAR_EN | POWAR_IO_READ |
  131. POWAR_IO_WRITE | (__ilog2(io->size) - 1)));
  132. out_be32(&pcix->pitar1, 0);
  133. out_be32(&pcix->piwbar1, 0);
  134. out_be32(&pcix->piwar1, (PIWAR_EN | PIWAR_PF | PIWAR_LOCAL |
  135. PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP | PIWAR_MEM_2G));
  136. out_be32(&pcix->powar3, 0);
  137. out_be32(&pcix->powar4, 0);
  138. out_be32(&pcix->piwar2, 0);
  139. out_be32(&pcix->piwar3, 0);
  140. return 0;
  141. }
  142. static int mpc85xx_pci_dm_remove(struct udevice *dev)
  143. {
  144. return 0;
  145. }
  146. static int mpc85xx_pci_of_to_plat(struct udevice *dev)
  147. {
  148. struct mpc85xx_pci_priv *priv = dev_get_priv(dev);
  149. fdt_addr_t addr;
  150. addr = devfdt_get_addr_index(dev, 0);
  151. if (addr == FDT_ADDR_T_NONE)
  152. return -EINVAL;
  153. priv->cfg_addr = (void __iomem *)map_physmem(addr, 0, MAP_NOCACHE);
  154. priv->cfg_data = (void __iomem *)((ulong)priv->cfg_addr + 4);
  155. return 0;
  156. }
  157. static const struct dm_pci_ops mpc85xx_pci_ops = {
  158. .read_config = mpc85xx_pci_dm_read_config,
  159. .write_config = mpc85xx_pci_dm_write_config,
  160. };
  161. static const struct udevice_id mpc85xx_pci_ids[] = {
  162. { .compatible = "fsl,mpc8540-pci" },
  163. { }
  164. };
  165. U_BOOT_DRIVER(mpc85xx_pci) = {
  166. .name = "mpc85xx_pci",
  167. .id = UCLASS_PCI,
  168. .of_match = mpc85xx_pci_ids,
  169. .ops = &mpc85xx_pci_ops,
  170. .probe = mpc85xx_pci_dm_probe,
  171. .remove = mpc85xx_pci_dm_remove,
  172. .of_to_plat = mpc85xx_pci_of_to_plat,
  173. .priv_auto = sizeof(struct mpc85xx_pci_priv),
  174. };