fsl_pci_init.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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/fsl_pci.h>
  35. /* Freescale-specific PCI config registers */
  36. #define FSL_PCI_PBFR 0x44
  37. #define FSL_PCIE_CAP_ID 0x4c
  38. #define FSL_PCIE_CFG_RDY 0x4b0
  39. void pciauto_prescan_setup_bridge(struct pci_controller *hose,
  40. pci_dev_t dev, int sub_bus);
  41. void pciauto_postscan_setup_bridge(struct pci_controller *hose,
  42. pci_dev_t dev, int sub_bus);
  43. void pciauto_config_init(struct pci_controller *hose);
  44. #ifndef CONFIG_SYS_PCI_MEMORY_BUS
  45. #define CONFIG_SYS_PCI_MEMORY_BUS 0
  46. #endif
  47. #ifndef CONFIG_SYS_PCI_MEMORY_PHYS
  48. #define CONFIG_SYS_PCI_MEMORY_PHYS 0
  49. #endif
  50. #if defined(CONFIG_SYS_PCI_64BIT) && !defined(CONFIG_SYS_PCI64_MEMORY_BUS)
  51. #define CONFIG_SYS_PCI64_MEMORY_BUS (64ull*1024*1024*1024)
  52. #endif
  53. int fsl_pci_setup_inbound_windows(struct pci_region *r)
  54. {
  55. struct pci_region *rgn_base = r;
  56. u64 sz = min((u64)gd->ram_size, (1ull << 32) - 1);
  57. phys_addr_t phys_start = CONFIG_SYS_PCI_MEMORY_PHYS;
  58. pci_addr_t bus_start = CONFIG_SYS_PCI_MEMORY_BUS;
  59. pci_size_t pci_sz = 1ull << __ilog2_u64(sz);
  60. debug ("R0 bus_start: %llx phys_start: %llx size: %llx\n",
  61. (u64)bus_start, (u64)phys_start, (u64)pci_sz);
  62. pci_set_region(r++, bus_start, phys_start, pci_sz,
  63. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
  64. PCI_REGION_PREFETCH);
  65. sz -= pci_sz;
  66. bus_start += pci_sz;
  67. phys_start += pci_sz;
  68. pci_sz = 1ull << __ilog2_u64(sz);
  69. if (sz) {
  70. debug ("R1 bus_start: %llx phys_start: %llx size: %llx\n",
  71. (u64)bus_start, (u64)phys_start, (u64)pci_sz);
  72. pci_set_region(r++, bus_start, phys_start, pci_sz,
  73. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
  74. PCI_REGION_PREFETCH);
  75. sz -= pci_sz;
  76. bus_start += pci_sz;
  77. phys_start += pci_sz;
  78. }
  79. #if defined(CONFIG_PHYS_64BIT) && defined(CONFIG_SYS_PCI_64BIT)
  80. /*
  81. * On 64-bit capable systems, set up a mapping for all of DRAM
  82. * in high pci address space.
  83. */
  84. pci_sz = 1ull << __ilog2_u64(gd->ram_size);
  85. /* round up to the next largest power of two */
  86. if (gd->ram_size > pci_sz)
  87. pci_sz = 1ull << (__ilog2_u64(gd->ram_size) + 1);
  88. debug ("R64 bus_start: %llx phys_start: %llx size: %llx\n",
  89. (u64)CONFIG_SYS_PCI64_MEMORY_BUS,
  90. (u64)CONFIG_SYS_PCI_MEMORY_PHYS,
  91. (u64)pci_sz);
  92. pci_set_region(r++,
  93. CONFIG_SYS_PCI64_MEMORY_BUS,
  94. CONFIG_SYS_PCI_MEMORY_PHYS,
  95. pci_sz,
  96. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
  97. PCI_REGION_PREFETCH);
  98. #else
  99. pci_sz = 1ull << __ilog2_u64(sz);
  100. if (sz) {
  101. debug ("R2 bus_start: %llx phys_start: %llx size: %llx\n",
  102. (u64)bus_start, (u64)phys_start, (u64)pci_sz);
  103. pci_set_region(r++, bus_start, phys_start, pci_sz,
  104. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY |
  105. PCI_REGION_PREFETCH);
  106. sz -= pci_sz;
  107. bus_start += pci_sz;
  108. phys_start += pci_sz;
  109. }
  110. #endif
  111. #ifdef CONFIG_PHYS_64BIT
  112. if (sz && (((u64)gd->ram_size) < (1ull << 32)))
  113. printf("Was not able to map all of memory via "
  114. "inbound windows -- %lld remaining\n", sz);
  115. #endif
  116. return r - rgn_base;
  117. }
  118. void fsl_pci_init(struct pci_controller *hose)
  119. {
  120. u16 temp16;
  121. u32 temp32;
  122. int busno = hose->first_busno;
  123. int enabled;
  124. u16 ltssm;
  125. u8 temp8;
  126. int r;
  127. int bridge;
  128. int inbound = 0;
  129. volatile ccsr_fsl_pci_t *pci = (ccsr_fsl_pci_t *) hose->cfg_addr;
  130. pci_dev_t dev = PCI_BDF(busno,0,0);
  131. /* Initialize ATMU registers based on hose regions and flags */
  132. volatile pot_t *po = &pci->pot[1]; /* skip 0 */
  133. volatile pit_t *pi = &pci->pit[0]; /* ranges from: 3 to 1 */
  134. #ifdef DEBUG
  135. int neg_link_w;
  136. #endif
  137. for (r=0; r<hose->region_count; r++) {
  138. u32 sz = (__ilog2_u64((u64)hose->regions[r].size) - 1);
  139. if (hose->regions[r].flags & PCI_REGION_SYS_MEMORY) { /* inbound */
  140. u32 flag = PIWAR_EN | PIWAR_LOCAL |
  141. PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
  142. pi->pitar = (hose->regions[r].phys_start >> 12);
  143. pi->piwbar = (hose->regions[r].bus_start >> 12);
  144. #ifdef CONFIG_SYS_PCI_64BIT
  145. pi->piwbear = (hose->regions[r].bus_start >> 44);
  146. #else
  147. pi->piwbear = 0;
  148. #endif
  149. if (hose->regions[r].flags & PCI_REGION_PREFETCH)
  150. flag |= PIWAR_PF;
  151. pi->piwar = flag | sz;
  152. pi++;
  153. inbound = hose->regions[r].size > 0;
  154. } else { /* Outbound */
  155. po->powbar = (hose->regions[r].phys_start >> 12);
  156. po->potar = (hose->regions[r].bus_start >> 12);
  157. #ifdef CONFIG_SYS_PCI_64BIT
  158. po->potear = (hose->regions[r].bus_start >> 44);
  159. #else
  160. po->potear = 0;
  161. #endif
  162. if (hose->regions[r].flags & PCI_REGION_IO)
  163. po->powar = POWAR_EN | sz |
  164. POWAR_IO_READ | POWAR_IO_WRITE;
  165. else
  166. po->powar = POWAR_EN | sz |
  167. POWAR_MEM_READ | POWAR_MEM_WRITE;
  168. po++;
  169. }
  170. }
  171. pci_register_hose(hose);
  172. pciauto_config_init(hose); /* grab pci_{mem,prefetch,io} */
  173. hose->current_busno = hose->first_busno;
  174. pci->pedr = 0xffffffff; /* Clear any errors */
  175. pci->peer = ~0x20140; /* Enable All Error Interupts except
  176. * - Master abort (pci)
  177. * - Master PERR (pci)
  178. * - ICCA (PCIe)
  179. */
  180. pci_hose_read_config_dword (hose, dev, PCI_DCR, &temp32);
  181. temp32 |= 0xf000e; /* set URR, FER, NFER (but not CER) */
  182. pci_hose_write_config_dword(hose, dev, PCI_DCR, temp32);
  183. pci_hose_read_config_byte (hose, dev, PCI_HEADER_TYPE, &temp8);
  184. bridge = temp8 & PCI_HEADER_TYPE_BRIDGE; /* Bridge, such as pcie */
  185. if ( bridge ) {
  186. pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm);
  187. enabled = ltssm >= PCI_LTSSM_L0;
  188. #ifdef CONFIG_FSL_PCIE_RESET
  189. if (ltssm == 1) {
  190. int i;
  191. debug("....PCIe link error. "
  192. "LTSSM=0x%02x.", ltssm);
  193. pci->pdb_stat |= 0x08000000; /* assert PCIe reset */
  194. temp32 = pci->pdb_stat;
  195. udelay(100);
  196. debug(" Asserting PCIe reset @%x = %x\n",
  197. &pci->pdb_stat, pci->pdb_stat);
  198. pci->pdb_stat &= ~0x08000000; /* clear reset */
  199. asm("sync;isync");
  200. for (i=0; i<100 && ltssm < PCI_LTSSM_L0; i++) {
  201. pci_hose_read_config_word(hose, dev, PCI_LTSSM,
  202. &ltssm);
  203. udelay(1000);
  204. debug("....PCIe link error. "
  205. "LTSSM=0x%02x.\n", ltssm);
  206. }
  207. enabled = ltssm >= PCI_LTSSM_L0;
  208. }
  209. #endif
  210. if (!enabled) {
  211. debug("....PCIE link error. Skipping scan."
  212. "LTSSM=0x%02x\n", ltssm);
  213. hose->last_busno = hose->first_busno;
  214. return;
  215. }
  216. pci->pme_msg_det = 0xffffffff;
  217. pci->pme_msg_int_en = 0xffffffff;
  218. #ifdef DEBUG
  219. pci_hose_read_config_word(hose, dev, PCI_LSR, &temp16);
  220. neg_link_w = (temp16 & 0x3f0 ) >> 4;
  221. printf("...PCIE LTSSM=0x%x, Negotiated link width=%d\n",
  222. ltssm, neg_link_w);
  223. #endif
  224. hose->current_busno++; /* Start scan with secondary */
  225. pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
  226. }
  227. /* Use generic setup_device to initialize standard pci regs,
  228. * but do not allocate any windows since any BAR found (such
  229. * as PCSRBAR) is not in this cpu's memory space.
  230. */
  231. pciauto_setup_device(hose, dev, 0, hose->pci_mem,
  232. hose->pci_prefetch, hose->pci_io);
  233. if (inbound) {
  234. pci_hose_read_config_word(hose, dev, PCI_COMMAND, &temp16);
  235. pci_hose_write_config_word(hose, dev, PCI_COMMAND,
  236. temp16 | PCI_COMMAND_MEMORY);
  237. }
  238. #ifndef CONFIG_PCI_NOSCAN
  239. pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &temp8);
  240. /* Programming Interface (PCI_CLASS_PROG)
  241. * 0 == pci host or pcie root-complex,
  242. * 1 == pci agent or pcie end-point
  243. */
  244. if (!temp8) {
  245. printf(" Scanning PCI bus %02x\n",
  246. hose->current_busno);
  247. hose->last_busno = pci_hose_scan_bus(hose, hose->current_busno);
  248. } else {
  249. debug(" Not scanning PCI bus %02x. PI=%x\n",
  250. hose->current_busno, temp8);
  251. hose->last_busno = hose->current_busno;
  252. }
  253. if ( bridge ) { /* update limit regs and subordinate busno */
  254. pciauto_postscan_setup_bridge(hose, dev, hose->last_busno);
  255. }
  256. #else
  257. hose->last_busno = hose->current_busno;
  258. #endif
  259. /* Clear all error indications */
  260. if (bridge)
  261. pci->pme_msg_det = 0xffffffff;
  262. pci->pedr = 0xffffffff;
  263. pci_hose_read_config_word (hose, dev, PCI_DSR, &temp16);
  264. if (temp16) {
  265. pci_hose_write_config_word(hose, dev,
  266. PCI_DSR, 0xffff);
  267. }
  268. pci_hose_read_config_word (hose, dev, PCI_SEC_STATUS, &temp16);
  269. if (temp16) {
  270. pci_hose_write_config_word(hose, dev, PCI_SEC_STATUS, 0xffff);
  271. }
  272. }
  273. /* Enable inbound PCI config cycles for agent/endpoint interface */
  274. void fsl_pci_config_unlock(struct pci_controller *hose)
  275. {
  276. pci_dev_t dev = PCI_BDF(hose->first_busno,0,0);
  277. u8 agent;
  278. u8 pcie_cap;
  279. u16 pbfr;
  280. pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &agent);
  281. if (!agent)
  282. return;
  283. pci_hose_read_config_byte(hose, dev, FSL_PCIE_CAP_ID, &pcie_cap);
  284. if (pcie_cap != 0x0) {
  285. /* PCIe - set CFG_READY bit of Configuration Ready Register */
  286. pci_hose_write_config_byte(hose, dev, FSL_PCIE_CFG_RDY, 0x1);
  287. } else {
  288. /* PCI - clear ACL bit of PBFR */
  289. pci_hose_read_config_word(hose, dev, FSL_PCI_PBFR, &pbfr);
  290. pbfr &= ~0x20;
  291. pci_hose_write_config_word(hose, dev, FSL_PCI_PBFR, pbfr);
  292. }
  293. }
  294. #ifdef CONFIG_OF_BOARD_SETUP
  295. #include <libfdt.h>
  296. #include <fdt_support.h>
  297. void ft_fsl_pci_setup(void *blob, const char *pci_alias,
  298. struct pci_controller *hose)
  299. {
  300. int off = fdt_path_offset(blob, pci_alias);
  301. if (off >= 0) {
  302. u32 bus_range[2];
  303. bus_range[0] = 0;
  304. bus_range[1] = hose->last_busno - hose->first_busno;
  305. fdt_setprop(blob, off, "bus-range", &bus_range[0], 2*4);
  306. fdt_pci_dma_ranges(blob, off, hose);
  307. }
  308. }
  309. #endif