pcie.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 cfg_read_err(val) do { *val = -1; } while (0)
  55. #define cfg_write_err(val) do { } while (0)
  56. #define PCIE_OP(rw, size, type, op) \
  57. static int pcie_##rw##_config_##size(struct pci_controller *hose, \
  58. pci_dev_t dev, int offset, \
  59. type val) \
  60. { \
  61. int ret; \
  62. \
  63. ret = mpc83xx_pcie_remap_cfg(hose, dev); \
  64. if (ret) { \
  65. cfg_##rw##_err(val); \
  66. return ret; \
  67. } \
  68. cfg_##rw(val, (void *)hose->cfg_addr + offset, type, op); \
  69. return 0; \
  70. }
  71. PCIE_OP(read, byte, u8 *, in_8)
  72. PCIE_OP(read, word, u16 *, in_le16)
  73. PCIE_OP(read, dword, u32 *, in_le32)
  74. PCIE_OP(write, byte, u8, out_8)
  75. PCIE_OP(write, word, u16, out_le16)
  76. PCIE_OP(write, dword, u32, out_le32)
  77. static void mpc83xx_pcie_register_hose(int bus, struct pci_region *reg,
  78. u8 link)
  79. {
  80. extern void disable_addr_trans(void); /* start.S */
  81. static struct pci_controller pcie_hose[PCIE_MAX_BUSES];
  82. struct pci_controller *hose = &pcie_hose[bus];
  83. int i;
  84. /*
  85. * There are no spare BATs to remap all PCI-E windows for U-Boot, so
  86. * disable translations. In general, this is not great solution, and
  87. * that's why we don't register PCI-E hoses by default.
  88. */
  89. disable_addr_trans();
  90. for (i = 0; i < 2; i++, reg++) {
  91. if (reg->size == 0)
  92. break;
  93. hose->regions[i] = *reg;
  94. hose->region_count++;
  95. }
  96. i = hose->region_count++;
  97. hose->regions[i].bus_start = 0;
  98. hose->regions[i].phys_start = 0;
  99. hose->regions[i].size = gd->ram_size;
  100. hose->regions[i].flags = PCI_REGION_MEM | PCI_REGION_SYS_MEMORY;
  101. i = hose->region_count++;
  102. hose->regions[i].bus_start = CONFIG_SYS_IMMR;
  103. hose->regions[i].phys_start = CONFIG_SYS_IMMR;
  104. hose->regions[i].size = 0x100000;
  105. hose->regions[i].flags = PCI_REGION_MEM | PCI_REGION_SYS_MEMORY;
  106. hose->first_busno = pci_last_busno() + 1;
  107. hose->last_busno = 0xff;
  108. if (bus == 0)
  109. hose->cfg_addr = (unsigned int *)CONFIG_SYS_PCIE1_CFG_BASE;
  110. else
  111. hose->cfg_addr = (unsigned int *)CONFIG_SYS_PCIE2_CFG_BASE;
  112. pci_set_ops(hose,
  113. pcie_read_config_byte,
  114. pcie_read_config_word,
  115. pcie_read_config_dword,
  116. pcie_write_config_byte,
  117. pcie_write_config_word,
  118. pcie_write_config_dword);
  119. if (!link)
  120. hose->indirect_type = INDIRECT_TYPE_NO_PCIE_LINK;
  121. pci_register_hose(hose);
  122. #ifdef CONFIG_PCI_SCAN_SHOW
  123. printf("PCI: Bus Dev VenId DevId Class Int\n");
  124. #endif
  125. /*
  126. * Hose scan.
  127. */
  128. hose->last_busno = pci_hose_scan(hose);
  129. }
  130. #else
  131. static void mpc83xx_pcie_register_hose(int bus, struct pci_region *reg,
  132. u8 link) {}
  133. #endif /* CONFIG_83XX_GENERIC_PCIE_REGISTER_HOSES */
  134. static void mpc83xx_pcie_init_bus(int bus, struct pci_region *reg)
  135. {
  136. immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  137. pex83xx_t *pex = &immr->pciexp[bus];
  138. struct pex_outbound_window *out_win;
  139. struct pex_inbound_window *in_win;
  140. void *hose_cfg_base;
  141. unsigned int ram_sz;
  142. unsigned int barl;
  143. unsigned int tar;
  144. u16 reg16;
  145. int i;
  146. /* Enable pex csb bridge inbound & outbound transactions */
  147. out_le32(&pex->bridge.pex_csb_ctrl,
  148. in_le32(&pex->bridge.pex_csb_ctrl) | PEX_CSB_CTRL_OBPIOE |
  149. PEX_CSB_CTRL_IBPIOE);
  150. /* Enable bridge outbound */
  151. out_le32(&pex->bridge.pex_csb_obctrl, PEX_CSB_OBCTRL_PIOE |
  152. PEX_CSB_OBCTRL_MEMWE | PEX_CSB_OBCTRL_IOWE |
  153. PEX_CSB_OBCTRL_CFGWE);
  154. out_win = &pex->bridge.pex_outbound_win[0];
  155. if (bus) {
  156. out_le32(&out_win->ar, PEX_OWAR_EN | PEX_OWAR_TYPE_CFG |
  157. CONFIG_SYS_PCIE2_CFG_SIZE);
  158. out_le32(&out_win->bar, CONFIG_SYS_PCIE2_CFG_BASE);
  159. } else {
  160. out_le32(&out_win->ar, PEX_OWAR_EN | PEX_OWAR_TYPE_CFG |
  161. CONFIG_SYS_PCIE1_CFG_SIZE);
  162. out_le32(&out_win->bar, CONFIG_SYS_PCIE1_CFG_BASE);
  163. }
  164. out_le32(&out_win->tarl, 0);
  165. out_le32(&out_win->tarh, 0);
  166. for (i = 0; i < 2; i++, reg++) {
  167. u32 ar;
  168. if (reg->size == 0)
  169. break;
  170. out_win = &pex->bridge.pex_outbound_win[i + 1];
  171. out_le32(&out_win->bar, reg->phys_start);
  172. out_le32(&out_win->tarl, reg->bus_start);
  173. out_le32(&out_win->tarh, 0);
  174. ar = PEX_OWAR_EN | (reg->size & PEX_OWAR_SIZE);
  175. if (reg->flags & PCI_REGION_IO)
  176. ar |= PEX_OWAR_TYPE_IO;
  177. else
  178. ar |= PEX_OWAR_TYPE_MEM;
  179. out_le32(&out_win->ar, ar);
  180. }
  181. out_le32(&pex->bridge.pex_csb_ibctrl, PEX_CSB_IBCTRL_PIOE);
  182. ram_sz = gd->ram_size;
  183. barl = 0;
  184. tar = 0;
  185. i = 0;
  186. while (ram_sz > 0) {
  187. in_win = &pex->bridge.pex_inbound_win[i];
  188. out_le32(&in_win->barl, barl);
  189. out_le32(&in_win->barh, 0x0);
  190. out_le32(&in_win->tar, tar);
  191. if (ram_sz >= 0x10000000) {
  192. /* The maxium windows size is 256M */
  193. out_le32(&in_win->ar, PEX_IWAR_EN | PEX_IWAR_NSOV |
  194. PEX_IWAR_TYPE_PF | 0x0FFFF000);
  195. barl += 0x10000000;
  196. tar += 0x10000000;
  197. ram_sz -= 0x10000000;
  198. } else {
  199. /* The UM is not clear here.
  200. * So, round up to even Mb boundary */
  201. ram_sz = ram_sz >> (20 +
  202. ((ram_sz & 0xFFFFF) ? 1 : 0));
  203. if (!(ram_sz % 2))
  204. ram_sz -= 1;
  205. out_le32(&in_win->ar, PEX_IWAR_EN | PEX_IWAR_NSOV |
  206. PEX_IWAR_TYPE_PF | (ram_sz << 20) | 0xFF000);
  207. ram_sz = 0;
  208. }
  209. i++;
  210. }
  211. in_win = &pex->bridge.pex_inbound_win[i];
  212. out_le32(&in_win->barl, CONFIG_SYS_IMMR);
  213. out_le32(&in_win->barh, 0);
  214. out_le32(&in_win->tar, CONFIG_SYS_IMMR);
  215. out_le32(&in_win->ar, PEX_IWAR_EN |
  216. PEX_IWAR_TYPE_NO_PF | PEX_IWAR_SIZE_1M);
  217. /* Enable the host virtual INTX interrupts */
  218. out_le32(&pex->bridge.pex_int_axi_misc_enb,
  219. in_le32(&pex->bridge.pex_int_axi_misc_enb) | 0x1E0);
  220. /* Hose configure header is memory-mapped */
  221. hose_cfg_base = (void *)pex;
  222. get_clocks();
  223. /* Configure the PCIE controller core clock ratio */
  224. out_le32(hose_cfg_base + PEX_GCLK_RATIO,
  225. (((bus ? gd->pciexp2_clk : gd->pciexp1_clk) / 1000000) * 16)
  226. / 333);
  227. udelay(1000000);
  228. /* Do Type 1 bridge configuration */
  229. out_8(hose_cfg_base + PCI_PRIMARY_BUS, 0);
  230. out_8(hose_cfg_base + PCI_SECONDARY_BUS, 1);
  231. out_8(hose_cfg_base + PCI_SUBORDINATE_BUS, 255);
  232. /*
  233. * Write to Command register
  234. */
  235. reg16 = in_le16(hose_cfg_base + PCI_COMMAND);
  236. reg16 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO |
  237. PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  238. out_le16(hose_cfg_base + PCI_COMMAND, reg16);
  239. /*
  240. * Clear non-reserved bits in status register.
  241. */
  242. out_le16(hose_cfg_base + PCI_STATUS, 0xffff);
  243. out_8(hose_cfg_base + PCI_LATENCY_TIMER, 0x80);
  244. out_8(hose_cfg_base + PCI_CACHE_LINE_SIZE, 0x08);
  245. printf("PCIE%d: ", bus);
  246. reg16 = in_le16(hose_cfg_base + PCI_LTSSM);
  247. if (reg16 >= PCI_LTSSM_L0)
  248. printf("link\n");
  249. else
  250. printf("No link\n");
  251. mpc83xx_pcie_register_hose(bus, reg, reg16 >= PCI_LTSSM_L0);
  252. }
  253. /*
  254. * The caller must have already set SCCR, SERDES and the PCIE_LAW BARs
  255. * must have been set to cover all of the requested regions.
  256. */
  257. void mpc83xx_pcie_init(int num_buses, struct pci_region **reg, int warmboot)
  258. {
  259. int i;
  260. /*
  261. * Release PCI RST Output signal.
  262. * Power on to RST high must be at least 100 ms as per PCI spec.
  263. * On warm boots only 1 ms is required.
  264. */
  265. udelay(warmboot ? 1000 : 100000);
  266. for (i = 0; i < num_buses; i++)
  267. mpc83xx_pcie_init_bus(i, reg[i]);
  268. }