pcie.c 9.3 KB

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