fsl_pci_init.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. #ifdef CONFIG_PHYS_64BIT
  108. if (sz && (((u64)gd->ram_size) < (1ull << 32)))
  109. printf("Was not able to map all of memory via "
  110. "inbound windows -- %lld remaining\n", sz);
  111. #endif
  112. return r - rgn_base;
  113. }
  114. void fsl_pci_init(struct pci_controller *hose)
  115. {
  116. u16 temp16;
  117. u32 temp32;
  118. int busno = hose->first_busno;
  119. int enabled;
  120. u16 ltssm;
  121. u8 temp8;
  122. int r;
  123. int bridge;
  124. int inbound = 0;
  125. volatile ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *) hose->cfg_addr;
  126. pci_dev_t dev = PCI_BDF(busno,0,0);
  127. /* Initialize ATMU registers based on hose regions and flags */
  128. volatile pot_t *po = &pci->pot[1]; /* skip 0 */
  129. volatile pit_t *pi = &pci->pit[0]; /* ranges from: 3 to 1 */
  130. #ifdef DEBUG
  131. int neg_link_w;
  132. #endif
  133. for (r=0; r<hose->region_count; r++) {
  134. u32 sz = (__ilog2_u64((u64)hose->regions[r].size) - 1);
  135. if (hose->regions[r].flags & PCI_REGION_MEMORY) { /* inbound */
  136. u32 flag = PIWAR_EN | PIWAR_LOCAL |
  137. PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
  138. pi->pitar = (hose->regions[r].phys_start >> 12);
  139. pi->piwbar = (hose->regions[r].bus_start >> 12);
  140. #ifdef CONFIG_SYS_PCI_64BIT
  141. pi->piwbear = (hose->regions[r].bus_start >> 44);
  142. #else
  143. pi->piwbear = 0;
  144. #endif
  145. if (hose->regions[r].flags & PCI_REGION_PREFETCH)
  146. flag |= PIWAR_PF;
  147. pi->piwar = flag | sz;
  148. pi++;
  149. inbound = hose->regions[r].size > 0;
  150. } else { /* Outbound */
  151. po->powbar = (hose->regions[r].phys_start >> 12);
  152. po->potar = (hose->regions[r].bus_start >> 12);
  153. #ifdef CONFIG_SYS_PCI_64BIT
  154. po->potear = (hose->regions[r].bus_start >> 44);
  155. #else
  156. po->potear = 0;
  157. #endif
  158. if (hose->regions[r].flags & PCI_REGION_IO)
  159. po->powar = POWAR_EN | sz |
  160. POWAR_IO_READ | POWAR_IO_WRITE;
  161. else
  162. po->powar = POWAR_EN | sz |
  163. POWAR_MEM_READ | POWAR_MEM_WRITE;
  164. po++;
  165. }
  166. }
  167. pci_register_hose(hose);
  168. pciauto_config_init(hose); /* grab pci_{mem,prefetch,io} */
  169. hose->current_busno = hose->first_busno;
  170. pci->pedr = 0xffffffff; /* Clear any errors */
  171. pci->peer = ~0x20140; /* Enable All Error Interupts except
  172. * - Master abort (pci)
  173. * - Master PERR (pci)
  174. * - ICCA (PCIe)
  175. */
  176. pci_hose_read_config_dword (hose, dev, PCI_DCR, &temp32);
  177. temp32 |= 0xf000e; /* set URR, FER, NFER (but not CER) */
  178. pci_hose_write_config_dword(hose, dev, PCI_DCR, temp32);
  179. pci_hose_read_config_byte (hose, dev, PCI_HEADER_TYPE, &temp8);
  180. bridge = temp8 & PCI_HEADER_TYPE_BRIDGE; /* Bridge, such as pcie */
  181. if ( bridge ) {
  182. pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm);
  183. enabled = ltssm >= PCI_LTSSM_L0;
  184. #ifdef CONFIG_FSL_PCIE_RESET
  185. if (ltssm == 1) {
  186. int i;
  187. debug("....PCIe link error. "
  188. "LTSSM=0x%02x.", ltssm);
  189. pci->pdb_stat |= 0x08000000; /* assert PCIe reset */
  190. temp32 = pci->pdb_stat;
  191. udelay(100);
  192. debug(" Asserting PCIe reset @%x = %x\n",
  193. &pci->pdb_stat, pci->pdb_stat);
  194. pci->pdb_stat &= ~0x08000000; /* clear reset */
  195. asm("sync;isync");
  196. for (i=0; i<100 && ltssm < PCI_LTSSM_L0; i++) {
  197. pci_hose_read_config_word(hose, dev, PCI_LTSSM,
  198. &ltssm);
  199. udelay(1000);
  200. debug("....PCIe link error. "
  201. "LTSSM=0x%02x.\n", ltssm);
  202. }
  203. enabled = ltssm >= PCI_LTSSM_L0;
  204. }
  205. #endif
  206. if (!enabled) {
  207. debug("....PCIE link error. Skipping scan."
  208. "LTSSM=0x%02x\n", ltssm);
  209. hose->last_busno = hose->first_busno;
  210. return;
  211. }
  212. pci->pme_msg_det = 0xffffffff;
  213. pci->pme_msg_int_en = 0xffffffff;
  214. #ifdef DEBUG
  215. pci_hose_read_config_word(hose, dev, PCI_LSR, &temp16);
  216. neg_link_w = (temp16 & 0x3f0 ) >> 4;
  217. printf("...PCIE LTSSM=0x%x, Negotiated link width=%d\n",
  218. ltssm, neg_link_w);
  219. #endif
  220. hose->current_busno++; /* Start scan with secondary */
  221. pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
  222. }
  223. /* Use generic setup_device to initialize standard pci regs,
  224. * but do not allocate any windows since any BAR found (such
  225. * as PCSRBAR) is not in this cpu's memory space.
  226. */
  227. pciauto_setup_device(hose, dev, 0, hose->pci_mem,
  228. hose->pci_prefetch, hose->pci_io);
  229. if (inbound) {
  230. pci_hose_read_config_word(hose, dev, PCI_COMMAND, &temp16);
  231. pci_hose_write_config_word(hose, dev, PCI_COMMAND,
  232. temp16 | PCI_COMMAND_MEMORY);
  233. }
  234. #ifndef CONFIG_PCI_NOSCAN
  235. pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &temp8);
  236. /* Programming Interface (PCI_CLASS_PROG)
  237. * 0 == pci host or pcie root-complex,
  238. * 1 == pci agent or pcie end-point
  239. */
  240. if (!temp8) {
  241. printf(" Scanning PCI bus %02x\n",
  242. hose->current_busno);
  243. hose->last_busno = pci_hose_scan_bus(hose, hose->current_busno);
  244. } else {
  245. debug(" Not scanning PCI bus %02x. PI=%x\n",
  246. hose->current_busno, temp8);
  247. hose->last_busno = hose->current_busno;
  248. }
  249. if ( bridge ) { /* update limit regs and subordinate busno */
  250. pciauto_postscan_setup_bridge(hose, dev, hose->last_busno);
  251. }
  252. #else
  253. hose->last_busno = hose->current_busno;
  254. #endif
  255. /* Clear all error indications */
  256. if (bridge)
  257. pci->pme_msg_det = 0xffffffff;
  258. pci->pedr = 0xffffffff;
  259. pci_hose_read_config_word (hose, dev, PCI_DSR, &temp16);
  260. if (temp16) {
  261. pci_hose_write_config_word(hose, dev,
  262. PCI_DSR, 0xffff);
  263. }
  264. pci_hose_read_config_word (hose, dev, PCI_SEC_STATUS, &temp16);
  265. if (temp16) {
  266. pci_hose_write_config_word(hose, dev, PCI_SEC_STATUS, 0xffff);
  267. }
  268. }
  269. #ifdef CONFIG_OF_BOARD_SETUP
  270. #include <libfdt.h>
  271. #include <fdt_support.h>
  272. void ft_fsl_pci_setup(void *blob, const char *pci_alias,
  273. struct pci_controller *hose)
  274. {
  275. int off = fdt_path_offset(blob, pci_alias);
  276. if (off >= 0) {
  277. u32 bus_range[2];
  278. bus_range[0] = 0;
  279. bus_range[1] = hose->last_busno - hose->first_busno;
  280. fdt_setprop(blob, off, "bus-range", &bus_range[0], 2*4);
  281. fdt_pci_dma_ranges(blob, off, hose);
  282. }
  283. }
  284. #endif