fsl_pci_init.c 10 KB

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