pcie.c 9.0 KB

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