ppc440spe_pcie.c 11 KB

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