fsl_pci_init.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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, u32 cfg_addr, u32 cfg_data)
  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 *)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. pci_setup_indirect(hose, cfg_addr, cfg_data);
  138. for (r=0; r<hose->region_count; r++) {
  139. u32 sz = (__ilog2_u64((u64)hose->regions[r].size) - 1);
  140. if (hose->regions[r].flags & PCI_REGION_SYS_MEMORY) { /* inbound */
  141. u32 flag = PIWAR_EN | PIWAR_LOCAL |
  142. PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
  143. pi->pitar = (hose->regions[r].phys_start >> 12);
  144. pi->piwbar = (hose->regions[r].bus_start >> 12);
  145. #ifdef CONFIG_SYS_PCI_64BIT
  146. pi->piwbear = (hose->regions[r].bus_start >> 44);
  147. #else
  148. pi->piwbear = 0;
  149. #endif
  150. if (hose->regions[r].flags & PCI_REGION_PREFETCH)
  151. flag |= PIWAR_PF;
  152. pi->piwar = flag | sz;
  153. pi++;
  154. inbound = hose->regions[r].size > 0;
  155. } else { /* Outbound */
  156. po->powbar = (hose->regions[r].phys_start >> 12);
  157. po->potar = (hose->regions[r].bus_start >> 12);
  158. #ifdef CONFIG_SYS_PCI_64BIT
  159. po->potear = (hose->regions[r].bus_start >> 44);
  160. #else
  161. po->potear = 0;
  162. #endif
  163. if (hose->regions[r].flags & PCI_REGION_IO)
  164. po->powar = POWAR_EN | sz |
  165. POWAR_IO_READ | POWAR_IO_WRITE;
  166. else
  167. po->powar = POWAR_EN | sz |
  168. POWAR_MEM_READ | POWAR_MEM_WRITE;
  169. po++;
  170. }
  171. }
  172. pci_register_hose(hose);
  173. pciauto_config_init(hose); /* grab pci_{mem,prefetch,io} */
  174. hose->current_busno = hose->first_busno;
  175. pci->pedr = 0xffffffff; /* Clear any errors */
  176. pci->peer = ~0x20140; /* Enable All Error Interupts except
  177. * - Master abort (pci)
  178. * - Master PERR (pci)
  179. * - ICCA (PCIe)
  180. */
  181. pci_hose_read_config_dword (hose, dev, PCI_DCR, &temp32);
  182. temp32 |= 0xf000e; /* set URR, FER, NFER (but not CER) */
  183. pci_hose_write_config_dword(hose, dev, PCI_DCR, temp32);
  184. pci_hose_read_config_byte (hose, dev, PCI_HEADER_TYPE, &temp8);
  185. bridge = temp8 & PCI_HEADER_TYPE_BRIDGE; /* Bridge, such as pcie */
  186. if ( bridge ) {
  187. pci_hose_read_config_word(hose, dev, PCI_LTSSM, &ltssm);
  188. enabled = ltssm >= PCI_LTSSM_L0;
  189. #ifdef CONFIG_FSL_PCIE_RESET
  190. if (ltssm == 1) {
  191. int i;
  192. debug("....PCIe link error. "
  193. "LTSSM=0x%02x.", ltssm);
  194. pci->pdb_stat |= 0x08000000; /* assert PCIe reset */
  195. temp32 = pci->pdb_stat;
  196. udelay(100);
  197. debug(" Asserting PCIe reset @%x = %x\n",
  198. &pci->pdb_stat, pci->pdb_stat);
  199. pci->pdb_stat &= ~0x08000000; /* clear reset */
  200. asm("sync;isync");
  201. for (i=0; i<100 && ltssm < PCI_LTSSM_L0; i++) {
  202. pci_hose_read_config_word(hose, dev, PCI_LTSSM,
  203. &ltssm);
  204. udelay(1000);
  205. debug("....PCIe link error. "
  206. "LTSSM=0x%02x.\n", ltssm);
  207. }
  208. enabled = ltssm >= PCI_LTSSM_L0;
  209. }
  210. #endif
  211. if (!enabled) {
  212. debug("....PCIE link error. Skipping scan."
  213. "LTSSM=0x%02x\n", ltssm);
  214. hose->last_busno = hose->first_busno;
  215. return;
  216. }
  217. pci->pme_msg_det = 0xffffffff;
  218. pci->pme_msg_int_en = 0xffffffff;
  219. #ifdef DEBUG
  220. pci_hose_read_config_word(hose, dev, PCI_LSR, &temp16);
  221. neg_link_w = (temp16 & 0x3f0 ) >> 4;
  222. printf("...PCIE LTSSM=0x%x, Negotiated link width=%d\n",
  223. ltssm, neg_link_w);
  224. #endif
  225. hose->current_busno++; /* Start scan with secondary */
  226. pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
  227. }
  228. /* Use generic setup_device to initialize standard pci regs,
  229. * but do not allocate any windows since any BAR found (such
  230. * as PCSRBAR) is not in this cpu's memory space.
  231. */
  232. pciauto_setup_device(hose, dev, 0, hose->pci_mem,
  233. hose->pci_prefetch, hose->pci_io);
  234. if (inbound) {
  235. pci_hose_read_config_word(hose, dev, PCI_COMMAND, &temp16);
  236. pci_hose_write_config_word(hose, dev, PCI_COMMAND,
  237. temp16 | PCI_COMMAND_MEMORY);
  238. }
  239. #ifndef CONFIG_PCI_NOSCAN
  240. pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &temp8);
  241. /* Programming Interface (PCI_CLASS_PROG)
  242. * 0 == pci host or pcie root-complex,
  243. * 1 == pci agent or pcie end-point
  244. */
  245. if (!temp8) {
  246. printf(" Scanning PCI bus %02x\n",
  247. hose->current_busno);
  248. hose->last_busno = pci_hose_scan_bus(hose, hose->current_busno);
  249. } else {
  250. debug(" Not scanning PCI bus %02x. PI=%x\n",
  251. hose->current_busno, temp8);
  252. hose->last_busno = hose->current_busno;
  253. }
  254. if ( bridge ) { /* update limit regs and subordinate busno */
  255. pciauto_postscan_setup_bridge(hose, dev, hose->last_busno);
  256. }
  257. #else
  258. hose->last_busno = hose->current_busno;
  259. #endif
  260. /* Clear all error indications */
  261. if (bridge)
  262. pci->pme_msg_det = 0xffffffff;
  263. pci->pedr = 0xffffffff;
  264. pci_hose_read_config_word (hose, dev, PCI_DSR, &temp16);
  265. if (temp16) {
  266. pci_hose_write_config_word(hose, dev,
  267. PCI_DSR, 0xffff);
  268. }
  269. pci_hose_read_config_word (hose, dev, PCI_SEC_STATUS, &temp16);
  270. if (temp16) {
  271. pci_hose_write_config_word(hose, dev, PCI_SEC_STATUS, 0xffff);
  272. }
  273. }
  274. /* Enable inbound PCI config cycles for agent/endpoint interface */
  275. void fsl_pci_config_unlock(struct pci_controller *hose)
  276. {
  277. pci_dev_t dev = PCI_BDF(hose->first_busno,0,0);
  278. u8 agent;
  279. u8 pcie_cap;
  280. u16 pbfr;
  281. pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &agent);
  282. if (!agent)
  283. return;
  284. pci_hose_read_config_byte(hose, dev, FSL_PCIE_CAP_ID, &pcie_cap);
  285. if (pcie_cap != 0x0) {
  286. /* PCIe - set CFG_READY bit of Configuration Ready Register */
  287. pci_hose_write_config_byte(hose, dev, FSL_PCIE_CFG_RDY, 0x1);
  288. } else {
  289. /* PCI - clear ACL bit of PBFR */
  290. pci_hose_read_config_word(hose, dev, FSL_PCI_PBFR, &pbfr);
  291. pbfr &= ~0x20;
  292. pci_hose_write_config_word(hose, dev, FSL_PCI_PBFR, pbfr);
  293. }
  294. }
  295. #ifdef CONFIG_OF_BOARD_SETUP
  296. #include <libfdt.h>
  297. #include <fdt_support.h>
  298. void ft_fsl_pci_setup(void *blob, const char *pci_alias,
  299. struct pci_controller *hose)
  300. {
  301. int off = fdt_path_offset(blob, pci_alias);
  302. if (off >= 0) {
  303. u32 bus_range[2];
  304. bus_range[0] = 0;
  305. bus_range[1] = hose->last_busno - hose->first_busno;
  306. fdt_setprop(blob, off, "bus-range", &bus_range[0], 2*4);
  307. fdt_pci_dma_ranges(blob, off, hose);
  308. }
  309. }
  310. #endif