pcie.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (C) 2007-2009 Freescale Semiconductor, Inc.
  3. * Copyright (C) 2008-2009 MontaVista Software, Inc.
  4. *
  5. * Authors: Tony Li <tony.li@freescale.com>
  6. * Anton Vorontsov <avorontsov@ru.mvista.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <pci.h>
  25. #include <mpc83xx.h>
  26. #include <asm/io.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. #define PCIE_MAX_BUSES 2
  29. #ifdef CONFIG_83XX_GENERIC_PCIE_REGISTER_HOSES
  30. static int mpc83xx_pcie_remap_cfg(struct pci_controller *hose, pci_dev_t dev)
  31. {
  32. int bus = PCI_BUS(dev) - hose->first_busno;
  33. immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  34. pex83xx_t *pex = &immr->pciexp[bus];
  35. struct pex_outbound_window *out_win = &pex->bridge.pex_outbound_win[0];
  36. u8 devfn = PCI_DEV(dev) << 3 | PCI_FUNC(dev);
  37. u32 dev_base = bus << 24 | devfn << 16;
  38. if (hose->indirect_type == INDIRECT_TYPE_NO_PCIE_LINK)
  39. return -1;
  40. /*
  41. * Workaround for the HW bug: for Type 0 configure transactions the
  42. * PCI-E controller does not check the device number bits and just
  43. * assumes that the device number bits are 0.
  44. */
  45. if (devfn & 0xf8)
  46. return -1;
  47. out_le32(&out_win->tarl, dev_base);
  48. return 0;
  49. }
  50. #define cfg_read(val, addr, type, op) \
  51. do { *val = op((type)(addr)); } while (0)
  52. #define cfg_write(val, addr, type, op) \
  53. do { op((type *)(addr), (val)); } while (0)
  54. #define PCIE_OP(rw, size, type, op) \
  55. static int pcie_##rw##_config_##size(struct pci_controller *hose, \
  56. pci_dev_t dev, int offset, \
  57. type val) \
  58. { \
  59. int ret; \
  60. \
  61. ret = mpc83xx_pcie_remap_cfg(hose, dev); \
  62. if (ret) \
  63. return ret; \
  64. cfg_##rw(val, (void *)hose->cfg_addr + offset, type, op); \
  65. return 0; \
  66. }
  67. PCIE_OP(read, byte, u8 *, in_8)
  68. PCIE_OP(read, word, u16 *, in_le16)
  69. PCIE_OP(read, dword, u32 *, in_le32)
  70. PCIE_OP(write, byte, u8, out_8)
  71. PCIE_OP(write, word, u16, out_le16)
  72. PCIE_OP(write, dword, u32, out_le32)
  73. static void mpc83xx_pcie_register_hose(int bus, struct pci_region *reg,
  74. u8 link)
  75. {
  76. extern void disable_addr_trans(void); /* start.S */
  77. static struct pci_controller pcie_hose[PCIE_MAX_BUSES];
  78. static int max_bus;
  79. struct pci_controller *hose = &pcie_hose[bus];
  80. int i;
  81. /*
  82. * There are no spare BATs to remap all PCI-E windows for U-Boot, so
  83. * disable translations. In general, this is not great solution, and
  84. * that's why we don't register PCI-E hoses by default.
  85. */
  86. disable_addr_trans();
  87. for (i = 0; i < 2; i++, reg++) {
  88. if (reg->size == 0)
  89. break;
  90. hose->regions[i] = *reg;
  91. hose->region_count++;
  92. }
  93. i = hose->region_count++;
  94. hose->regions[i].bus_start = 0;
  95. hose->regions[i].phys_start = 0;
  96. hose->regions[i].size = gd->ram_size;
  97. hose->regions[i].flags = PCI_REGION_MEM | PCI_REGION_SYS_MEMORY;
  98. i = hose->region_count++;
  99. hose->regions[i].bus_start = CONFIG_SYS_IMMR;
  100. hose->regions[i].phys_start = CONFIG_SYS_IMMR;
  101. hose->regions[i].size = 0x100000;
  102. hose->regions[i].flags = PCI_REGION_MEM | PCI_REGION_SYS_MEMORY;
  103. hose->first_busno = max_bus;
  104. hose->last_busno = 0xff;
  105. if (bus == 0)
  106. hose->cfg_addr = (unsigned int *)CONFIG_SYS_PCIE1_CFG_BASE;
  107. else
  108. hose->cfg_addr = (unsigned int *)CONFIG_SYS_PCIE2_CFG_BASE;
  109. pci_set_ops(hose,
  110. pcie_read_config_byte,
  111. pcie_read_config_word,
  112. pcie_read_config_dword,
  113. pcie_write_config_byte,
  114. pcie_write_config_word,
  115. pcie_write_config_dword);
  116. if (!link)
  117. hose->indirect_type = INDIRECT_TYPE_NO_PCIE_LINK;
  118. pci_register_hose(hose);
  119. #ifdef CONFIG_PCI_SCAN_SHOW
  120. printf("PCI: Bus Dev VenId DevId Class Int\n");
  121. #endif
  122. /*
  123. * Hose scan.
  124. */
  125. hose->last_busno = pci_hose_scan(hose);
  126. max_bus = hose->last_busno + 1;
  127. }
  128. #else
  129. static void mpc83xx_pcie_register_hose(int bus, struct pci_region *reg,
  130. u8 link) {}
  131. #endif /* CONFIG_83XX_GENERIC_PCIE_REGISTER_HOSES */
  132. static void mpc83xx_pcie_init_bus(int bus, struct pci_region *reg)
  133. {
  134. immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  135. pex83xx_t *pex = &immr->pciexp[bus];
  136. struct pex_outbound_window *out_win;
  137. struct pex_inbound_window *in_win;
  138. void *hose_cfg_base;
  139. unsigned int ram_sz;
  140. unsigned int barl;
  141. unsigned int tar;
  142. u16 reg16;
  143. int i;
  144. /* Enable pex csb bridge inbound & outbound transactions */
  145. out_le32(&pex->bridge.pex_csb_ctrl,
  146. in_le32(&pex->bridge.pex_csb_ctrl) | PEX_CSB_CTRL_OBPIOE |
  147. PEX_CSB_CTRL_IBPIOE);
  148. /* Enable bridge outbound */
  149. out_le32(&pex->bridge.pex_csb_obctrl, PEX_CSB_OBCTRL_PIOE |
  150. PEX_CSB_OBCTRL_MEMWE | PEX_CSB_OBCTRL_IOWE |
  151. PEX_CSB_OBCTRL_CFGWE);
  152. out_win = &pex->bridge.pex_outbound_win[0];
  153. if (bus) {
  154. out_le32(&out_win->ar, PEX_OWAR_EN | PEX_OWAR_TYPE_CFG |
  155. CONFIG_SYS_PCIE2_CFG_SIZE);
  156. out_le32(&out_win->bar, CONFIG_SYS_PCIE2_CFG_BASE);
  157. } else {
  158. out_le32(&out_win->ar, PEX_OWAR_EN | PEX_OWAR_TYPE_CFG |
  159. CONFIG_SYS_PCIE1_CFG_SIZE);
  160. out_le32(&out_win->bar, CONFIG_SYS_PCIE1_CFG_BASE);
  161. }
  162. out_le32(&out_win->tarl, 0);
  163. out_le32(&out_win->tarh, 0);
  164. for (i = 0; i < 2; i++, reg++) {
  165. u32 ar;
  166. if (reg->size == 0)
  167. break;
  168. out_win = &pex->bridge.pex_outbound_win[i + 1];
  169. out_le32(&out_win->bar, reg->phys_start);
  170. out_le32(&out_win->tarl, reg->bus_start);
  171. out_le32(&out_win->tarh, 0);
  172. ar = PEX_OWAR_EN | (reg->size & PEX_OWAR_SIZE);
  173. if (reg->flags & PCI_REGION_IO)
  174. ar |= PEX_OWAR_TYPE_IO;
  175. else
  176. ar |= PEX_OWAR_TYPE_MEM;
  177. out_le32(&out_win->ar, ar);
  178. }
  179. out_le32(&pex->bridge.pex_csb_ibctrl, PEX_CSB_IBCTRL_PIOE);
  180. ram_sz = gd->ram_size;
  181. barl = 0;
  182. tar = 0;
  183. i = 0;
  184. while (ram_sz > 0) {
  185. in_win = &pex->bridge.pex_inbound_win[i];
  186. out_le32(&in_win->barl, barl);
  187. out_le32(&in_win->barh, 0x0);
  188. out_le32(&in_win->tar, tar);
  189. if (ram_sz >= 0x10000000) {
  190. /* The maxium windows size is 256M */
  191. out_le32(&in_win->ar, PEX_IWAR_EN | PEX_IWAR_NSOV |
  192. PEX_IWAR_TYPE_PF | 0x0FFFF000);
  193. barl += 0x10000000;
  194. tar += 0x10000000;
  195. ram_sz -= 0x10000000;
  196. } else {
  197. /* The UM is not clear here.
  198. * So, round up to even Mb boundary */
  199. ram_sz = ram_sz >> (20 +
  200. ((ram_sz & 0xFFFFF) ? 1 : 0));
  201. if (!(ram_sz % 2))
  202. ram_sz -= 1;
  203. out_le32(&in_win->ar, PEX_IWAR_EN | PEX_IWAR_NSOV |
  204. PEX_IWAR_TYPE_PF | (ram_sz << 20) | 0xFF000);
  205. ram_sz = 0;
  206. }
  207. i++;
  208. }
  209. in_win = &pex->bridge.pex_inbound_win[i];
  210. out_le32(&in_win->barl, CONFIG_SYS_IMMR);
  211. out_le32(&in_win->barh, 0);
  212. out_le32(&in_win->tar, CONFIG_SYS_IMMR);
  213. out_le32(&in_win->ar, PEX_IWAR_EN |
  214. PEX_IWAR_TYPE_NO_PF | PEX_IWAR_SIZE_1M);
  215. /* Enable the host virtual INTX interrupts */
  216. out_le32(&pex->bridge.pex_int_axi_misc_enb,
  217. in_le32(&pex->bridge.pex_int_axi_misc_enb) | 0x1E0);
  218. /* Hose configure header is memory-mapped */
  219. hose_cfg_base = (void *)pex;
  220. get_clocks();
  221. /* Configure the PCIE controller core clock ratio */
  222. out_le32(hose_cfg_base + PEX_GCLK_RATIO,
  223. (((bus ? gd->pciexp2_clk : gd->pciexp1_clk) / 1000000) * 16)
  224. / 333);
  225. udelay(1000000);
  226. /* Do Type 1 bridge configuration */
  227. out_8(hose_cfg_base + PCI_PRIMARY_BUS, 0);
  228. out_8(hose_cfg_base + PCI_SECONDARY_BUS, 1);
  229. out_8(hose_cfg_base + PCI_SUBORDINATE_BUS, 255);
  230. /*
  231. * Write to Command register
  232. */
  233. reg16 = in_le16(hose_cfg_base + PCI_COMMAND);
  234. reg16 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO |
  235. PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  236. out_le16(hose_cfg_base + PCI_COMMAND, reg16);
  237. /*
  238. * Clear non-reserved bits in status register.
  239. */
  240. out_le16(hose_cfg_base + PCI_STATUS, 0xffff);
  241. out_8(hose_cfg_base + PCI_LATENCY_TIMER, 0x80);
  242. out_8(hose_cfg_base + PCI_CACHE_LINE_SIZE, 0x08);
  243. printf("PCIE%d: ", bus);
  244. reg16 = in_le16(hose_cfg_base + PCI_LTSSM);
  245. if (reg16 >= PCI_LTSSM_L0)
  246. printf("link\n");
  247. else
  248. printf("No link\n");
  249. mpc83xx_pcie_register_hose(bus, reg, reg16 >= PCI_LTSSM_L0);
  250. }
  251. /*
  252. * The caller must have already set SCCR, SERDES and the PCIE_LAW BARs
  253. * must have been set to cover all of the requested regions.
  254. */
  255. void mpc83xx_pcie_init(int num_buses, struct pci_region **reg, int warmboot)
  256. {
  257. int i;
  258. /*
  259. * Release PCI RST Output signal.
  260. * Power on to RST high must be at least 100 ms as per PCI spec.
  261. * On warm boots only 1 ms is required.
  262. */
  263. udelay(warmboot ? 1000 : 100000);
  264. for (i = 0; i < num_buses; i++)
  265. mpc83xx_pcie_init_bus(i, reg[i]);
  266. }