pci.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) Freescale Semiconductor, Inc. 2007
  4. *
  5. * Author: Scott Wood <scottwood@freescale.com>,
  6. * with some bits from older board-specific PCI initialization.
  7. */
  8. #include <common.h>
  9. #include <pci.h>
  10. #if defined(CONFIG_OF_LIBFDT)
  11. #include <linux/libfdt.h>
  12. #include <fdt_support.h>
  13. #endif
  14. #include <asm/mpc8349_pci.h>
  15. #define MAX_BUSES 2
  16. DECLARE_GLOBAL_DATA_PTR;
  17. static struct pci_controller pci_hose[MAX_BUSES];
  18. static int pci_num_buses;
  19. static void pci_init_bus(int bus, struct pci_region *reg)
  20. {
  21. volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR;
  22. volatile pot83xx_t *pot = immr->ios.pot;
  23. volatile pcictrl83xx_t *pci_ctrl = &immr->pci_ctrl[bus];
  24. struct pci_controller *hose = &pci_hose[bus];
  25. u32 dev;
  26. u16 reg16;
  27. int i;
  28. if (bus == 1)
  29. pot += 3;
  30. /* Setup outbound translation windows */
  31. for (i = 0; i < 3; i++, reg++, pot++) {
  32. if (reg->size == 0)
  33. break;
  34. hose->regions[i] = *reg;
  35. hose->region_count++;
  36. pot->potar = reg->bus_start >> 12;
  37. pot->pobar = reg->phys_start >> 12;
  38. pot->pocmr = ~(reg->size - 1) >> 12;
  39. if (reg->flags & PCI_REGION_IO)
  40. pot->pocmr |= POCMR_IO;
  41. #ifdef CONFIG_83XX_PCI_STREAMING
  42. else if (reg->flags & PCI_REGION_PREFETCH)
  43. pot->pocmr |= POCMR_SE;
  44. #endif
  45. if (bus == 1)
  46. pot->pocmr |= POCMR_DST;
  47. pot->pocmr |= POCMR_EN;
  48. }
  49. /* Point inbound translation at RAM */
  50. pci_ctrl->pitar1 = 0;
  51. pci_ctrl->pibar1 = 0;
  52. pci_ctrl->piebar1 = 0;
  53. pci_ctrl->piwar1 = PIWAR_EN | PIWAR_PF | PIWAR_RTT_SNOOP |
  54. PIWAR_WTT_SNOOP | (__ilog2(gd->ram_size - 1));
  55. i = hose->region_count++;
  56. hose->regions[i].bus_start = 0;
  57. hose->regions[i].phys_start = 0;
  58. hose->regions[i].size = gd->ram_size;
  59. hose->regions[i].flags = PCI_REGION_MEM | PCI_REGION_SYS_MEMORY;
  60. hose->first_busno = pci_last_busno() + 1;
  61. hose->last_busno = 0xff;
  62. pci_setup_indirect(hose, CONFIG_SYS_IMMR + 0x8300 + bus * 0x80,
  63. CONFIG_SYS_IMMR + 0x8304 + bus * 0x80);
  64. pci_register_hose(hose);
  65. /*
  66. * Write to Command register
  67. */
  68. reg16 = 0xff;
  69. dev = PCI_BDF(hose->first_busno, 0, 0);
  70. pci_hose_read_config_word(hose, dev, PCI_COMMAND, &reg16);
  71. reg16 |= PCI_COMMAND_SERR | PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
  72. pci_hose_write_config_word(hose, dev, PCI_COMMAND, reg16);
  73. /*
  74. * Clear non-reserved bits in status register.
  75. */
  76. pci_hose_write_config_word(hose, dev, PCI_STATUS, 0xffff);
  77. pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
  78. pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
  79. #ifdef CONFIG_PCI_SCAN_SHOW
  80. printf("PCI: Bus Dev VenId DevId Class Int\n");
  81. #endif
  82. #ifndef CONFIG_PCISLAVE
  83. /*
  84. * Hose scan.
  85. */
  86. hose->last_busno = pci_hose_scan(hose);
  87. #endif
  88. }
  89. /*
  90. * The caller must have already set OCCR, and the PCI_LAW BARs
  91. * must have been set to cover all of the requested regions.
  92. *
  93. * If fewer than three regions are requested, then the region
  94. * list is terminated with a region of size 0.
  95. */
  96. void mpc83xx_pci_init(int num_buses, struct pci_region **reg)
  97. {
  98. volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR;
  99. int i;
  100. if (num_buses > MAX_BUSES) {
  101. printf("%d PCI buses requested, %d supported\n",
  102. num_buses, MAX_BUSES);
  103. num_buses = MAX_BUSES;
  104. }
  105. pci_num_buses = num_buses;
  106. /*
  107. * Release PCI RST Output signal.
  108. * Power on to RST high must be at least 100 ms as per PCI spec.
  109. * On warm boots only 1 ms is required, but we play it safe.
  110. */
  111. udelay(100000);
  112. for (i = 0; i < num_buses; i++)
  113. immr->pci_ctrl[i].gcr = 1;
  114. /*
  115. * RST high to first config access must be at least 2^25 cycles
  116. * as per PCI spec. This could be cut in half if we know we're
  117. * running at 66MHz. This could be insufficiently long if we're
  118. * running the PCI bus at significantly less than 33MHz.
  119. */
  120. udelay(1020000);
  121. for (i = 0; i < num_buses; i++)
  122. pci_init_bus(i, reg[i]);
  123. }
  124. #ifdef CONFIG_PCISLAVE
  125. #define PCI_FUNCTION_CONFIG 0x44
  126. #define PCI_FUNCTION_CFG_LOCK 0x20
  127. /*
  128. * Unlock the configuration bit so that the host system can begin booting
  129. *
  130. * This should be used after you have:
  131. * 1) Called mpc83xx_pci_init()
  132. * 2) Set up your inbound translation windows to the appropriate size
  133. */
  134. void mpc83xx_pcislave_unlock(int bus)
  135. {
  136. struct pci_controller *hose = &pci_hose[bus];
  137. u32 dev;
  138. u16 reg16;
  139. /* Unlock configuration lock in PCI function configuration register */
  140. dev = PCI_BDF(hose->first_busno, 0, 0);
  141. pci_hose_read_config_word (hose, dev, PCI_FUNCTION_CONFIG, &reg16);
  142. reg16 &= ~(PCI_FUNCTION_CFG_LOCK);
  143. pci_hose_write_config_word (hose, dev, PCI_FUNCTION_CONFIG, reg16);
  144. /* The configuration bit is now unlocked, so we can scan the bus */
  145. hose->last_busno = pci_hose_scan(hose);
  146. }
  147. #endif
  148. #if defined(CONFIG_OF_LIBFDT)
  149. void ft_pci_setup(void *blob, bd_t *bd)
  150. {
  151. int nodeoffset;
  152. int tmp[2];
  153. const char *path;
  154. if (pci_num_buses < 1)
  155. return;
  156. nodeoffset = fdt_path_offset(blob, "/aliases");
  157. if (nodeoffset >= 0) {
  158. path = fdt_getprop(blob, nodeoffset, "pci0", NULL);
  159. if (path) {
  160. tmp[0] = cpu_to_be32(pci_hose[0].first_busno);
  161. tmp[1] = cpu_to_be32(pci_hose[0].last_busno);
  162. do_fixup_by_path(blob, path, "bus-range",
  163. &tmp, sizeof(tmp), 1);
  164. tmp[0] = cpu_to_be32(gd->pci_clk);
  165. do_fixup_by_path(blob, path, "clock-frequency",
  166. &tmp, sizeof(tmp[0]), 1);
  167. }
  168. if (pci_num_buses < 2)
  169. return;
  170. path = fdt_getprop(blob, nodeoffset, "pci1", NULL);
  171. if (path) {
  172. tmp[0] = cpu_to_be32(pci_hose[1].first_busno);
  173. tmp[1] = cpu_to_be32(pci_hose[1].last_busno);
  174. do_fixup_by_path(blob, path, "bus-range",
  175. &tmp, sizeof(tmp), 1);
  176. tmp[0] = cpu_to_be32(gd->pci_clk);
  177. do_fixup_by_path(blob, path, "clock-frequency",
  178. &tmp, sizeof(tmp[0]), 1);
  179. }
  180. }
  181. }
  182. #endif /* CONFIG_OF_LIBFDT */