ppc440spe_pcie.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Copyright (c) 2005 Cisco Systems. All rights reserved.
  3. * Roland Dreier <rolandd@cisco.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/delay.h>
  12. #include <linux/pci.h>
  13. #include <linux/init.h>
  14. #include <asm/reg.h>
  15. #include <asm/io.h>
  16. #include <asm/ibm44x.h>
  17. #include "ppc440spe_pcie.h"
  18. static int
  19. pcie_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  20. int len, u32 *val)
  21. {
  22. struct pci_controller *hose = bus->sysdata;
  23. if (PCI_SLOT(devfn) != 1)
  24. return PCIBIOS_DEVICE_NOT_FOUND;
  25. offset += devfn << 12;
  26. /*
  27. * Note: the caller has already checked that offset is
  28. * suitably aligned and that len is 1, 2 or 4.
  29. */
  30. switch (len) {
  31. case 1:
  32. *val = in_8(hose->cfg_data + offset);
  33. break;
  34. case 2:
  35. *val = in_le16(hose->cfg_data + offset);
  36. break;
  37. default:
  38. *val = in_le32(hose->cfg_data + offset);
  39. break;
  40. }
  41. if (0) printk("%s: read %x(%d) @ %x\n", __func__, *val, len, offset);
  42. return PCIBIOS_SUCCESSFUL;
  43. }
  44. static int
  45. pcie_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  46. int len, u32 val)
  47. {
  48. struct pci_controller *hose = bus->sysdata;
  49. if (PCI_SLOT(devfn) != 1)
  50. return PCIBIOS_DEVICE_NOT_FOUND;
  51. offset += devfn << 12;
  52. switch (len) {
  53. case 1:
  54. out_8(hose->cfg_data + offset, val);
  55. break;
  56. case 2:
  57. out_le16(hose->cfg_data + offset, val);
  58. break;
  59. default:
  60. out_le32(hose->cfg_data + offset, val);
  61. break;
  62. }
  63. return PCIBIOS_SUCCESSFUL;
  64. }
  65. static struct pci_ops pcie_pci_ops =
  66. {
  67. .read = pcie_read_config,
  68. .write = pcie_write_config
  69. };
  70. enum {
  71. PTYPE_ENDPOINT = 0x0,
  72. PTYPE_LEGACY_ENDPOINT = 0x1,
  73. PTYPE_ROOT_PORT = 0x4,
  74. LNKW_X1 = 0x1,
  75. LNKW_X4 = 0x4,
  76. LNKW_X8 = 0x8
  77. };
  78. static void check_error(void)
  79. {
  80. u32 valPE0, valPE1, valPE2;
  81. /* SDR0_PEGPLLLCT1 reset */
  82. if (!(valPE0 = SDR_READ(PESDR0_PLLLCT1) & 0x01000000)) {
  83. printk(KERN_INFO "PCIE: SDR0_PEGPLLLCT1 reset error 0x%8x\n", valPE0);
  84. }
  85. valPE0 = SDR_READ(PESDR0_RCSSET);
  86. valPE1 = SDR_READ(PESDR1_RCSSET);
  87. valPE2 = SDR_READ(PESDR2_RCSSET);
  88. /* SDR0_PExRCSSET rstgu */
  89. if ( !(valPE0 & 0x01000000) ||
  90. !(valPE1 & 0x01000000) ||
  91. !(valPE2 & 0x01000000)) {
  92. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstgu error\n");
  93. }
  94. /* SDR0_PExRCSSET rstdl */
  95. if ( !(valPE0 & 0x00010000) ||
  96. !(valPE1 & 0x00010000) ||
  97. !(valPE2 & 0x00010000)) {
  98. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstdl error\n");
  99. }
  100. /* SDR0_PExRCSSET rstpyn */
  101. if ( (valPE0 & 0x00001000) ||
  102. (valPE1 & 0x00001000) ||
  103. (valPE2 & 0x00001000)) {
  104. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rstpyn error\n");
  105. }
  106. /* SDR0_PExRCSSET hldplb */
  107. if ( (valPE0 & 0x10000000) ||
  108. (valPE1 & 0x10000000) ||
  109. (valPE2 & 0x10000000)) {
  110. printk(KERN_INFO "PCIE: SDR0_PExRCSSET hldplb error\n");
  111. }
  112. /* SDR0_PExRCSSET rdy */
  113. if ( (valPE0 & 0x00100000) ||
  114. (valPE1 & 0x00100000) ||
  115. (valPE2 & 0x00100000)) {
  116. printk(KERN_INFO "PCIE: SDR0_PExRCSSET rdy error\n");
  117. }
  118. /* SDR0_PExRCSSET shutdown */
  119. if ( (valPE0 & 0x00000100) ||
  120. (valPE1 & 0x00000100) ||
  121. (valPE2 & 0x00000100)) {
  122. printk(KERN_INFO "PCIE: SDR0_PExRCSSET shutdown error\n");
  123. }
  124. }
  125. /*
  126. * Initialize PCI Express core as described in User Manual section 27.12.1
  127. */
  128. int ppc440spe_init_pcie(void)
  129. {
  130. /* Set PLL clock receiver to LVPECL */
  131. SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) | 1 << 28);
  132. check_error();
  133. printk(KERN_INFO "PCIE initialization OK\n");
  134. if (!(SDR_READ(PESDR0_PLLLCT2) & 0x10000))
  135. printk(KERN_INFO "PESDR_PLLCT2 resistance calibration failed (0x%08x)\n",
  136. SDR_READ(PESDR0_PLLLCT2));
  137. /* De-assert reset of PCIe PLL, wait for lock */
  138. SDR_WRITE(PESDR0_PLLLCT1, SDR_READ(PESDR0_PLLLCT1) & ~(1 << 24));
  139. udelay(3);
  140. return 0;
  141. }
  142. int ppc440spe_init_pcie_rootport(int port)
  143. {
  144. static int core_init;
  145. void __iomem *utl_base;
  146. u32 val = 0;
  147. int i;
  148. if (!core_init) {
  149. ++core_init;
  150. i = ppc440spe_init_pcie();
  151. if (i)
  152. return i;
  153. }
  154. /*
  155. * Initialize various parts of the PCI Express core for our port:
  156. *
  157. * - Set as a root port and enable max width
  158. * (PXIE0 -> X8, PCIE1 and PCIE2 -> X4).
  159. * - Set up UTL configuration.
  160. * - Increase SERDES drive strength to levels suggested by AMCC.
  161. * - De-assert RSTPYN, RSTDL and RSTGU.
  162. */
  163. switch (port) {
  164. case 0:
  165. SDR_WRITE(PESDR0_DLPSET, PTYPE_ROOT_PORT << 20 | LNKW_X8 << 12);
  166. SDR_WRITE(PESDR0_UTLSET1, 0x21222222);
  167. SDR_WRITE(PESDR0_UTLSET2, 0x11000000);
  168. SDR_WRITE(PESDR0_HSSL0SET1, 0x35000000);
  169. SDR_WRITE(PESDR0_HSSL1SET1, 0x35000000);
  170. SDR_WRITE(PESDR0_HSSL2SET1, 0x35000000);
  171. SDR_WRITE(PESDR0_HSSL3SET1, 0x35000000);
  172. SDR_WRITE(PESDR0_HSSL4SET1, 0x35000000);
  173. SDR_WRITE(PESDR0_HSSL5SET1, 0x35000000);
  174. SDR_WRITE(PESDR0_HSSL6SET1, 0x35000000);
  175. SDR_WRITE(PESDR0_HSSL7SET1, 0x35000000);
  176. SDR_WRITE(PESDR0_RCSSET,
  177. (SDR_READ(PESDR0_RCSSET) & ~(1 << 24 | 1 << 16)) | 1 << 12);
  178. break;
  179. case 1:
  180. SDR_WRITE(PESDR1_DLPSET, PTYPE_ROOT_PORT << 20 | LNKW_X4 << 12);
  181. SDR_WRITE(PESDR1_UTLSET1, 0x21222222);
  182. SDR_WRITE(PESDR1_UTLSET2, 0x11000000);
  183. SDR_WRITE(PESDR1_HSSL0SET1, 0x35000000);
  184. SDR_WRITE(PESDR1_HSSL1SET1, 0x35000000);
  185. SDR_WRITE(PESDR1_HSSL2SET1, 0x35000000);
  186. SDR_WRITE(PESDR1_HSSL3SET1, 0x35000000);
  187. SDR_WRITE(PESDR1_RCSSET,
  188. (SDR_READ(PESDR1_RCSSET) & ~(1 << 24 | 1 << 16)) | 1 << 12);
  189. break;
  190. case 2:
  191. SDR_WRITE(PESDR2_DLPSET, PTYPE_ROOT_PORT << 20 | LNKW_X4 << 12);
  192. SDR_WRITE(PESDR2_UTLSET1, 0x21222222);
  193. SDR_WRITE(PESDR2_UTLSET2, 0x11000000);
  194. SDR_WRITE(PESDR2_HSSL0SET1, 0x35000000);
  195. SDR_WRITE(PESDR2_HSSL1SET1, 0x35000000);
  196. SDR_WRITE(PESDR2_HSSL2SET1, 0x35000000);
  197. SDR_WRITE(PESDR2_HSSL3SET1, 0x35000000);
  198. SDR_WRITE(PESDR2_RCSSET,
  199. (SDR_READ(PESDR2_RCSSET) & ~(1 << 24 | 1 << 16)) | 1 << 12);
  200. break;
  201. }
  202. mdelay(1000);
  203. switch (port) {
  204. case 0: val = SDR_READ(PESDR0_RCSSTS); break;
  205. case 1: val = SDR_READ(PESDR1_RCSSTS); break;
  206. case 2: val = SDR_READ(PESDR2_RCSSTS); break;
  207. }
  208. if (!(val & (1 << 20)))
  209. printk(KERN_INFO "PCIE%d: PGRST inactive\n", port);
  210. else
  211. printk(KERN_WARNING "PGRST for PCIE%d failed %08x\n", port, val);
  212. switch (port) {
  213. case 0: printk(KERN_INFO "PCIE0: LOOP %08x\n", SDR_READ(PESDR0_LOOP)); break;
  214. case 1: printk(KERN_INFO "PCIE1: LOOP %08x\n", SDR_READ(PESDR1_LOOP)); break;
  215. case 2: printk(KERN_INFO "PCIE2: LOOP %08x\n", SDR_READ(PESDR2_LOOP)); break;
  216. }
  217. /*
  218. * Map UTL registers at 0xc_1000_0n00
  219. */
  220. switch (port) {
  221. case 0:
  222. mtdcr(DCRN_PEGPL_REGBAH(PCIE0), 0x0000000c);
  223. mtdcr(DCRN_PEGPL_REGBAL(PCIE0), 0x10000000);
  224. mtdcr(DCRN_PEGPL_REGMSK(PCIE0), 0x00007001);
  225. mtdcr(DCRN_PEGPL_SPECIAL(PCIE0), 0x68782800);
  226. break;
  227. case 1:
  228. mtdcr(DCRN_PEGPL_REGBAH(PCIE1), 0x0000000c);
  229. mtdcr(DCRN_PEGPL_REGBAL(PCIE1), 0x10001000);
  230. mtdcr(DCRN_PEGPL_REGMSK(PCIE1), 0x00007001);
  231. mtdcr(DCRN_PEGPL_SPECIAL(PCIE1), 0x68782800);
  232. break;
  233. case 2:
  234. mtdcr(DCRN_PEGPL_REGBAH(PCIE2), 0x0000000c);
  235. mtdcr(DCRN_PEGPL_REGBAL(PCIE2), 0x10002000);
  236. mtdcr(DCRN_PEGPL_REGMSK(PCIE2), 0x00007001);
  237. mtdcr(DCRN_PEGPL_SPECIAL(PCIE2), 0x68782800);
  238. }
  239. utl_base = ioremap64(0xc10000000ull + 0x1000 * port, 0x100);
  240. /*
  241. * Set buffer allocations and then assert VRB and TXE.
  242. */
  243. out_be32(utl_base + PEUTL_OUTTR, 0x08000000);
  244. out_be32(utl_base + PEUTL_INTR, 0x02000000);
  245. out_be32(utl_base + PEUTL_OPDBSZ, 0x10000000);
  246. out_be32(utl_base + PEUTL_PBBSZ, 0x53000000);
  247. out_be32(utl_base + PEUTL_IPHBSZ, 0x08000000);
  248. out_be32(utl_base + PEUTL_IPDBSZ, 0x10000000);
  249. out_be32(utl_base + PEUTL_RCIRQEN, 0x00f00000);
  250. out_be32(utl_base + PEUTL_PCTL, 0x80800066);
  251. iounmap(utl_base);
  252. /*
  253. * We map PCI Express configuration access into the 512MB regions
  254. * PCIE0: 0xc_4000_0000
  255. * PCIE1: 0xc_8000_0000
  256. * PCIE2: 0xc_c000_0000
  257. */
  258. switch (port) {
  259. case 0:
  260. mtdcr(DCRN_PEGPL_CFGBAH(PCIE0), 0x0000000c);
  261. mtdcr(DCRN_PEGPL_CFGBAL(PCIE0), 0x40000000);
  262. mtdcr(DCRN_PEGPL_CFGMSK(PCIE0), 0xe0000001); /* 512MB region, valid */
  263. break;
  264. case 1:
  265. mtdcr(DCRN_PEGPL_CFGBAH(PCIE1), 0x0000000c);
  266. mtdcr(DCRN_PEGPL_CFGBAL(PCIE1), 0x80000000);
  267. mtdcr(DCRN_PEGPL_CFGMSK(PCIE1), 0xe0000001); /* 512MB region, valid */
  268. break;
  269. case 2:
  270. mtdcr(DCRN_PEGPL_CFGBAH(PCIE2), 0x0000000c);
  271. mtdcr(DCRN_PEGPL_CFGBAL(PCIE2), 0xc0000000);
  272. mtdcr(DCRN_PEGPL_CFGMSK(PCIE2), 0xe0000001); /* 512MB region, valid */
  273. break;
  274. }
  275. /*
  276. * Check for VC0 active and assert RDY.
  277. */
  278. switch (port) {
  279. case 0:
  280. if (!(SDR_READ(PESDR0_RCSSTS) & (1 << 16)))
  281. printk(KERN_WARNING "PCIE0: VC0 not active\n");
  282. SDR_WRITE(PESDR0_RCSSET, SDR_READ(PESDR0_RCSSET) | 1 << 20);
  283. break;
  284. case 1:
  285. if (!(SDR_READ(PESDR1_RCSSTS) & (1 << 16)))
  286. printk(KERN_WARNING "PCIE0: VC0 not active\n");
  287. SDR_WRITE(PESDR1_RCSSET, SDR_READ(PESDR1_RCSSET) | 1 << 20);
  288. break;
  289. case 2:
  290. if (!(SDR_READ(PESDR2_RCSSTS) & (1 << 16)))
  291. printk(KERN_WARNING "PCIE0: VC0 not active\n");
  292. SDR_WRITE(PESDR2_RCSSET, SDR_READ(PESDR2_RCSSET) | 1 << 20);
  293. break;
  294. }
  295. #if 0
  296. /* Dump all config regs */
  297. for (i = 0x300; i <= 0x320; ++i)
  298. printk("[%04x] 0x%08x\n", i, SDR_READ(i));
  299. for (i = 0x340; i <= 0x353; ++i)
  300. printk("[%04x] 0x%08x\n", i, SDR_READ(i));
  301. for (i = 0x370; i <= 0x383; ++i)
  302. printk("[%04x] 0x%08x\n", i, SDR_READ(i));
  303. for (i = 0x3a0; i <= 0x3a2; ++i)
  304. printk("[%04x] 0x%08x\n", i, SDR_READ(i));
  305. for (i = 0x3c0; i <= 0x3c3; ++i)
  306. printk("[%04x] 0x%08x\n", i, SDR_READ(i));
  307. #endif
  308. mdelay(100);
  309. return 0;
  310. }
  311. void ppc440spe_setup_pcie(struct pci_controller *hose, int port)
  312. {
  313. void __iomem *mbase;
  314. /*
  315. * Map 16MB, which is enough for 4 bits of bus #
  316. */
  317. hose->cfg_data = ioremap64(0xc40000000ull + port * 0x40000000,
  318. 1 << 24);
  319. hose->ops = &pcie_pci_ops;
  320. /*
  321. * Set bus numbers on our root port
  322. */
  323. mbase = ioremap64(0xc50000000ull + port * 0x40000000, 4096);
  324. out_8(mbase + PCI_PRIMARY_BUS, 0);
  325. out_8(mbase + PCI_SECONDARY_BUS, 0);
  326. /*
  327. * Set up outbound translation to hose->mem_space from PLB
  328. * addresses at an offset of 0xd_0000_0000. We set the low
  329. * bits of the mask to 11 to turn off splitting into 8
  330. * subregions and to enable the outbound translation.
  331. */
  332. out_le32(mbase + PECFG_POM0LAH, 0);
  333. out_le32(mbase + PECFG_POM0LAL, hose->mem_space.start);
  334. switch (port) {
  335. case 0:
  336. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE0), 0x0000000d);
  337. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE0), hose->mem_space.start);
  338. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE0), 0x7fffffff);
  339. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE0),
  340. ~(hose->mem_space.end - hose->mem_space.start) | 3);
  341. break;
  342. case 1:
  343. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE1), 0x0000000d);
  344. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE1), hose->mem_space.start);
  345. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE1), 0x7fffffff);
  346. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE1),
  347. ~(hose->mem_space.end - hose->mem_space.start) | 3);
  348. break;
  349. case 2:
  350. mtdcr(DCRN_PEGPL_OMR1BAH(PCIE2), 0x0000000d);
  351. mtdcr(DCRN_PEGPL_OMR1BAL(PCIE2), hose->mem_space.start);
  352. mtdcr(DCRN_PEGPL_OMR1MSKH(PCIE2), 0x7fffffff);
  353. mtdcr(DCRN_PEGPL_OMR1MSKL(PCIE2),
  354. ~(hose->mem_space.end - hose->mem_space.start) | 3);
  355. break;
  356. }
  357. /* Set up 16GB inbound memory window at 0 */
  358. out_le32(mbase + PCI_BASE_ADDRESS_0, 0);
  359. out_le32(mbase + PCI_BASE_ADDRESS_1, 0);
  360. out_le32(mbase + PECFG_BAR0HMPA, 0x7fffffc);
  361. out_le32(mbase + PECFG_BAR0LMPA, 0);
  362. out_le32(mbase + PECFG_PIM0LAL, 0);
  363. out_le32(mbase + PECFG_PIM0LAH, 0);
  364. out_le32(mbase + PECFG_PIMEN, 0x1);
  365. /* Enable I/O, Mem, and Busmaster cycles */
  366. out_le16(mbase + PCI_COMMAND,
  367. in_le16(mbase + PCI_COMMAND) |
  368. PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  369. iounmap(mbase);
  370. }