fsl_pci_init.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright 2007 Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * Version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  16. * MA 02111-1307 USA
  17. */
  18. #include <common.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. /*
  21. * PCI/PCIE Controller initialization for mpc85xx/mpc86xx soc's
  22. *
  23. * Initialize controller and call the common driver/pci pci_hose_scan to
  24. * scan for bridges and devices.
  25. *
  26. * Hose fields which need to be pre-initialized by board specific code:
  27. * regions[]
  28. * first_busno
  29. *
  30. * Fields updated:
  31. * last_busno
  32. */
  33. #include <pci.h>
  34. #include <asm/immap_fsl_pci.h>
  35. void pciauto_prescan_setup_bridge(struct pci_controller *hose,
  36. pci_dev_t dev, int sub_bus);
  37. void pciauto_postscan_setup_bridge(struct pci_controller *hose,
  38. pci_dev_t dev, int sub_bus);
  39. void pciauto_config_init(struct pci_controller *hose);
  40. #ifndef CONFIG_SYS_PCI_MEMORY_BUS
  41. #define CONFIG_SYS_PCI_MEMORY_BUS 0
  42. #endif
  43. #ifndef CONFIG_SYS_PCI_MEMORY_PHYS
  44. #define CONFIG_SYS_PCI_MEMORY_PHYS 0
  45. #endif
  46. #if defined(CONFIG_SYS_PCI_64BIT) && !defined(CONFIG_SYS_PCI64_MEMORY_BUS)
  47. #define CONFIG_SYS_PCI64_MEMORY_BUS (64ull*1024*1024*1024)
  48. #endif
  49. int fsl_pci_setup_inbound_windows(struct pci_region *r)
  50. {
  51. struct pci_region *rgn_base = r;
  52. u64 sz = min((u64)gd->ram_size, (1ull << 32) - 1);
  53. phys_addr_t phys_start = CONFIG_SYS_PCI_MEMORY_PHYS;
  54. pci_addr_t bus_start = CONFIG_SYS_PCI_MEMORY_BUS;
  55. pci_size_t pci_sz = 1ull << __ilog2_u64(sz);
  56. debug ("R0 bus_start: %llx phys_start: %llx size: %llx\n",
  57. (u64)bus_start, (u64)phys_start, (u64)pci_sz);
  58. pci_set_region(r++, bus_start, phys_start, pci_sz,
  59. PCI_REGION_MEM | PCI_REGION_MEMORY |
  60. PCI_REGION_PREFETCH);
  61. sz -= pci_sz;
  62. bus_start += pci_sz;
  63. phys_start += pci_sz;
  64. pci_sz = 1ull << __ilog2_u64(sz);
  65. if (sz) {
  66. debug ("R1 bus_start: %llx phys_start: %llx size: %llx\n",
  67. (u64)bus_start, (u64)phys_start, (u64)pci_sz);
  68. pci_set_region(r++, bus_start, phys_start, pci_sz,
  69. PCI_REGION_MEM | PCI_REGION_MEMORY |
  70. PCI_REGION_PREFETCH);
  71. sz -= pci_sz;
  72. bus_start += pci_sz;
  73. phys_start += pci_sz;
  74. }
  75. #if defined(CONFIG_PHYS_64BIT) && defined(CONFIG_SYS_PCI_64BIT)
  76. /*
  77. * On 64-bit capable systems, set up a mapping for all of DRAM
  78. * in high pci address space.
  79. */
  80. pci_sz = 1ull << __ilog2_u64(gd->ram_size);
  81. /* round up to the next largest power of two */
  82. if (gd->ram_size > pci_sz)
  83. pci_sz = 1ull << (__ilog2_u64(gd->ram_size) + 1);
  84. debug ("R64 bus_start: %llx phys_start: %llx size: %llx\n",
  85. (u64)CONFIG_SYS_PCI64_MEMORY_BUS,
  86. (u64)CONFIG_SYS_PCI_MEMORY_PHYS,
  87. (u64)pci_sz);
  88. pci_set_region(r++,
  89. CONFIG_SYS_PCI64_MEMORY_BUS,
  90. CONFIG_SYS_PCI_MEMORY_PHYS,
  91. pci_sz,
  92. PCI_REGION_MEM | PCI_REGION_MEMORY |
  93. PCI_REGION_PREFETCH);
  94. #else
  95. pci_sz = 1ull << __ilog2_u64(sz);
  96. if (sz) {
  97. debug ("R2 bus_start: %llx phys_start: %llx size: %llx\n",
  98. (u64)bus_start, (u64)phys_start, (u64)pci_sz);
  99. pci_set_region(r++, bus_start, phys_start, pci_sz,
  100. PCI_REGION_MEM | PCI_REGION_MEMORY |
  101. PCI_REGION_PREFETCH);
  102. sz -= pci_sz;
  103. bus_start += pci_sz;
  104. phys_start += pci_sz;
  105. }
  106. #endif
  107. if (sz && (((u64)gd->ram_size) < (1ull << 32)))
  108. printf("Was not able to map all of memory via "
  109. "inbound windows -- %lld remaining\n", sz);
  110. return r - rgn_base;
  111. }
  112. void fsl_pci_init(struct pci_controller *hose)
  113. {
  114. u16 temp16;
  115. u32 temp32;
  116. int busno = hose->first_busno;
  117. int enabled;
  118. u16 ltssm;
  119. u8 temp8;
  120. int r;
  121. int bridge;
  122. int inbound = 0;
  123. volatile ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *) hose->cfg_addr;
  124. pci_dev_t dev = PCI_BDF(busno,0,0);
  125. /* Initialize ATMU registers based on hose regions and flags */
  126. volatile pot_t *po = &pci->pot[1]; /* skip 0 */
  127. volatile pit_t *pi = &pci->pit[0]; /* ranges from: 3 to 1 */
  128. #ifdef DEBUG
  129. int neg_link_w;
  130. #endif
  131. for (r=0; r<hose->region_count; r++) {
  132. u32 sz = (__ilog2_u64((u64)hose->regions[r].size) - 1);
  133. if (hose->regions[r].flags & PCI_REGION_MEMORY) { /* inbound */
  134. u32 flag = PIWAR_EN | PIWAR_LOCAL |
  135. PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
  136. pi->pitar = (hose->regions[r].phys_start >> 12);
  137. pi->piwbar = (hose->regions[r].bus_start >> 12);
  138. #ifdef CONFIG_SYS_PCI_64BIT
  139. pi->piwbear = (hose->regions[r].bus_start >> 44);
  140. #else
  141. pi->piwbear = 0;
  142. #endif
  143. if (hose->regions[r].flags & PCI_REGION_PREFETCH)
  144. flag |= PIWAR_PF;
  145. pi->piwar = flag | sz;
  146. pi++;
  147. inbound = hose->regions[r].size > 0;
  148. } else { /* Outbound */
  149. po->powbar = (hose->regions[r].phys_start >> 12);
  150. po->potar = (hose->regions[r].bus_start >> 12);
  151. #ifdef CONFIG_SYS_PCI_64BIT
  152. po->potear = (hose->regions[r].bus_start >> 44);
  153. #else
  154. po->potear = 0;
  155. #endif
  156. if (hose->regions[r].flags & PCI_REGION_IO)
  157. po->powar = POWAR_EN | sz |
  158. POWAR_IO_READ | POWAR_IO_WRITE;
  159. else
  160. po->powar = POWAR_EN | sz |
  161. POWAR_MEM_READ | POWAR_MEM_WRITE;
  162. po++;
  163. }
  164. }
  165. pci_register_hose(hose);
  166. pciauto_config_init(hose); /* grab pci_{mem,prefetch,io} */
  167. hose->current_busno = hose->first_busno;
  168. pci->pedr = 0xffffffff; /* Clear any errors */
  169. pci->peer = ~0x20140; /* Enable All Error Interupts except
  170. * - Master abort (pci)
  171. * - Master PERR (pci)
  172. * - ICCA (PCIe)
  173. */
  174. pci_hose_read_config_dword (hose, dev, PCI_DCR, &temp32);
  175. temp32 |= 0xf000e; /* set URR, FER, NFER (but not CER) */
  176. pci_hose_write_config_dword(hose, dev, PCI_DCR, temp32);
  177. pci_hose_read_config_byte (hose, dev, PCI_HEADER_TYPE, &temp8);
  178. bridge = temp8 & PCI_HEADER_TYPE_BRIDGE; /* Bridge, such as pcie */
  179. if ( bridge ) {
  180. pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm);
  181. enabled = ltssm >= PCI_LTSSM_L0;
  182. #ifdef CONFIG_FSL_PCIE_RESET
  183. if (ltssm == 1) {
  184. int i;
  185. debug("....PCIe link error. "
  186. "LTSSM=0x%02x.", ltssm);
  187. pci->pdb_stat |= 0x08000000; /* assert PCIe reset */
  188. temp32 = pci->pdb_stat;
  189. udelay(100);
  190. debug(" Asserting PCIe reset @%x = %x\n",
  191. &pci->pdb_stat, pci->pdb_stat);
  192. pci->pdb_stat &= ~0x08000000; /* clear reset */
  193. asm("sync;isync");
  194. for (i=0; i<100 && ltssm < PCI_LTSSM_L0; i++) {
  195. pci_hose_read_config_word(hose, dev, PCI_LTSSM,
  196. &ltssm);
  197. udelay(1000);
  198. debug("....PCIe link error. "
  199. "LTSSM=0x%02x.\n", ltssm);
  200. }
  201. enabled = ltssm >= PCI_LTSSM_L0;
  202. }
  203. #endif
  204. if (!enabled) {
  205. debug("....PCIE link error. Skipping scan."
  206. "LTSSM=0x%02x\n", ltssm);
  207. hose->last_busno = hose->first_busno;
  208. return;
  209. }
  210. pci->pme_msg_det = 0xffffffff;
  211. pci->pme_msg_int_en = 0xffffffff;
  212. #ifdef DEBUG
  213. pci_hose_read_config_word(hose, dev, PCI_LSR, &temp16);
  214. neg_link_w = (temp16 & 0x3f0 ) >> 4;
  215. printf("...PCIE LTSSM=0x%x, Negotiated link width=%d\n",
  216. ltssm, neg_link_w);
  217. #endif
  218. hose->current_busno++; /* Start scan with secondary */
  219. pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
  220. }
  221. /* Use generic setup_device to initialize standard pci regs,
  222. * but do not allocate any windows since any BAR found (such
  223. * as PCSRBAR) is not in this cpu's memory space.
  224. */
  225. pciauto_setup_device(hose, dev, 0, hose->pci_mem,
  226. hose->pci_prefetch, hose->pci_io);
  227. if (inbound) {
  228. pci_hose_read_config_word(hose, dev, PCI_COMMAND, &temp16);
  229. pci_hose_write_config_word(hose, dev, PCI_COMMAND,
  230. temp16 | PCI_COMMAND_MEMORY);
  231. }
  232. #ifndef CONFIG_PCI_NOSCAN
  233. pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &temp8);
  234. /* Programming Interface (PCI_CLASS_PROG)
  235. * 0 == pci host or pcie root-complex,
  236. * 1 == pci agent or pcie end-point
  237. */
  238. if (!temp8) {
  239. printf(" Scanning PCI bus %02x\n",
  240. hose->current_busno);
  241. hose->last_busno = pci_hose_scan_bus(hose, hose->current_busno);
  242. } else {
  243. debug(" Not scanning PCI bus %02x. PI=%x\n",
  244. hose->current_busno, temp8);
  245. hose->last_busno = hose->current_busno;
  246. }
  247. if ( bridge ) { /* update limit regs and subordinate busno */
  248. pciauto_postscan_setup_bridge(hose, dev, hose->last_busno);
  249. }
  250. #else
  251. hose->last_busno = hose->current_busno;
  252. #endif
  253. /* Clear all error indications */
  254. if (bridge)
  255. pci->pme_msg_det = 0xffffffff;
  256. pci->pedr = 0xffffffff;
  257. pci_hose_read_config_word (hose, dev, PCI_DSR, &temp16);
  258. if (temp16) {
  259. pci_hose_write_config_word(hose, dev,
  260. PCI_DSR, 0xffff);
  261. }
  262. pci_hose_read_config_word (hose, dev, PCI_SEC_STATUS, &temp16);
  263. if (temp16) {
  264. pci_hose_write_config_word(hose, dev, PCI_SEC_STATUS, 0xffff);
  265. }
  266. }
  267. #ifdef CONFIG_OF_BOARD_SETUP
  268. #include <libfdt.h>
  269. #include <fdt_support.h>
  270. void ft_fsl_pci_setup(void *blob, const char *pci_alias,
  271. struct pci_controller *hose)
  272. {
  273. int off = fdt_path_offset(blob, pci_alias);
  274. if (off >= 0) {
  275. u32 bus_range[2];
  276. bus_range[0] = 0;
  277. bus_range[1] = hose->last_busno - hose->first_busno;
  278. fdt_setprop(blob, off, "bus-range", &bus_range[0], 2*4);
  279. fdt_pci_dma_ranges(blob, off, hose);
  280. }
  281. }
  282. #endif