pcie-sh7786.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * Low-Level PCI Express Support for the SH7786
  3. *
  4. * Copyright (C) 2009 - 2010 Paul Mundt
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/pci.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/io.h>
  14. #include <linux/delay.h>
  15. #include <linux/slab.h>
  16. #include <linux/clk.h>
  17. #include <linux/sh_clk.h>
  18. #include "pcie-sh7786.h"
  19. #include <asm/sizes.h>
  20. struct sh7786_pcie_port {
  21. struct pci_channel *hose;
  22. struct clk *fclk, phy_clk;
  23. unsigned int index;
  24. int endpoint;
  25. int link;
  26. };
  27. static struct sh7786_pcie_port *sh7786_pcie_ports;
  28. static unsigned int nr_ports;
  29. static struct sh7786_pcie_hwops {
  30. int (*core_init)(void);
  31. int (*port_init_hw)(struct sh7786_pcie_port *port);
  32. } *sh7786_pcie_hwops;
  33. static struct resource sh7786_pci0_resources[] = {
  34. {
  35. .name = "PCIe0 IO",
  36. .start = 0xfd000000,
  37. .end = 0xfd000000 + SZ_8M - 1,
  38. .flags = IORESOURCE_IO,
  39. }, {
  40. .name = "PCIe0 MEM 0",
  41. .start = 0xc0000000,
  42. .end = 0xc0000000 + SZ_512M - 1,
  43. .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
  44. }, {
  45. .name = "PCIe0 MEM 1",
  46. .start = 0x10000000,
  47. .end = 0x10000000 + SZ_64M - 1,
  48. .flags = IORESOURCE_MEM,
  49. }, {
  50. .name = "PCIe0 MEM 2",
  51. .start = 0xfe100000,
  52. .end = 0xfe100000 + SZ_1M - 1,
  53. .flags = IORESOURCE_MEM,
  54. },
  55. };
  56. static struct resource sh7786_pci1_resources[] = {
  57. {
  58. .name = "PCIe1 IO",
  59. .start = 0xfd800000,
  60. .end = 0xfd800000 + SZ_8M - 1,
  61. .flags = IORESOURCE_IO,
  62. }, {
  63. .name = "PCIe1 MEM 0",
  64. .start = 0xa0000000,
  65. .end = 0xa0000000 + SZ_512M - 1,
  66. .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
  67. }, {
  68. .name = "PCIe1 MEM 1",
  69. .start = 0x30000000,
  70. .end = 0x30000000 + SZ_256M - 1,
  71. .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
  72. }, {
  73. .name = "PCIe1 MEM 2",
  74. .start = 0xfe300000,
  75. .end = 0xfe300000 + SZ_1M - 1,
  76. .flags = IORESOURCE_MEM,
  77. },
  78. };
  79. static struct resource sh7786_pci2_resources[] = {
  80. {
  81. .name = "PCIe2 IO",
  82. .start = 0xfc800000,
  83. .end = 0xfc800000 + SZ_4M - 1,
  84. .flags = IORESOURCE_IO,
  85. }, {
  86. .name = "PCIe2 MEM 0",
  87. .start = 0x80000000,
  88. .end = 0x80000000 + SZ_512M - 1,
  89. .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
  90. }, {
  91. .name = "PCIe2 MEM 1",
  92. .start = 0x20000000,
  93. .end = 0x20000000 + SZ_256M - 1,
  94. .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
  95. }, {
  96. .name = "PCIe2 MEM 2",
  97. .start = 0xfcd00000,
  98. .end = 0xfcd00000 + SZ_1M - 1,
  99. .flags = IORESOURCE_MEM,
  100. },
  101. };
  102. extern struct pci_ops sh7786_pci_ops;
  103. #define DEFINE_CONTROLLER(start, idx) \
  104. { \
  105. .pci_ops = &sh7786_pci_ops, \
  106. .resources = sh7786_pci##idx##_resources, \
  107. .nr_resources = ARRAY_SIZE(sh7786_pci##idx##_resources), \
  108. .reg_base = start, \
  109. .mem_offset = 0, \
  110. .io_offset = 0, \
  111. }
  112. static struct pci_channel sh7786_pci_channels[] = {
  113. DEFINE_CONTROLLER(0xfe000000, 0),
  114. DEFINE_CONTROLLER(0xfe200000, 1),
  115. DEFINE_CONTROLLER(0xfcc00000, 2),
  116. };
  117. static struct clk fixed_pciexclkp = {
  118. .rate = 100000000, /* 100 MHz reference clock */
  119. };
  120. static void __devinit sh7786_pci_fixup(struct pci_dev *dev)
  121. {
  122. /*
  123. * Prevent enumeration of root complex resources.
  124. */
  125. if (pci_is_root_bus(dev->bus) && dev->devfn == 0) {
  126. int i;
  127. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  128. dev->resource[i].start = 0;
  129. dev->resource[i].end = 0;
  130. dev->resource[i].flags = 0;
  131. }
  132. }
  133. }
  134. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_SH7786,
  135. sh7786_pci_fixup);
  136. static int __init phy_wait_for_ack(struct pci_channel *chan)
  137. {
  138. unsigned int timeout = 100;
  139. while (timeout--) {
  140. if (pci_read_reg(chan, SH4A_PCIEPHYADRR) & (1 << BITS_ACK))
  141. return 0;
  142. udelay(100);
  143. }
  144. return -ETIMEDOUT;
  145. }
  146. static int __init pci_wait_for_irq(struct pci_channel *chan, unsigned int mask)
  147. {
  148. unsigned int timeout = 100;
  149. while (timeout--) {
  150. if ((pci_read_reg(chan, SH4A_PCIEINTR) & mask) == mask)
  151. return 0;
  152. udelay(100);
  153. }
  154. return -ETIMEDOUT;
  155. }
  156. static void __init phy_write_reg(struct pci_channel *chan, unsigned int addr,
  157. unsigned int lane, unsigned int data)
  158. {
  159. unsigned long phyaddr;
  160. phyaddr = (1 << BITS_CMD) + ((lane & 0xf) << BITS_LANE) +
  161. ((addr & 0xff) << BITS_ADR);
  162. /* Set write data */
  163. pci_write_reg(chan, data, SH4A_PCIEPHYDOUTR);
  164. pci_write_reg(chan, phyaddr, SH4A_PCIEPHYADRR);
  165. phy_wait_for_ack(chan);
  166. /* Clear command */
  167. pci_write_reg(chan, 0, SH4A_PCIEPHYDOUTR);
  168. pci_write_reg(chan, 0, SH4A_PCIEPHYADRR);
  169. phy_wait_for_ack(chan);
  170. }
  171. static int __init pcie_clk_init(struct sh7786_pcie_port *port)
  172. {
  173. struct pci_channel *chan = port->hose;
  174. struct clk *clk;
  175. char fclk_name[16];
  176. int ret;
  177. /*
  178. * First register the fixed clock
  179. */
  180. ret = clk_register(&fixed_pciexclkp);
  181. if (unlikely(ret != 0))
  182. return ret;
  183. /*
  184. * Grab the port's function clock, which the PHY clock depends
  185. * on. clock lookups don't help us much at this point, since no
  186. * dev_id is available this early. Lame.
  187. */
  188. snprintf(fclk_name, sizeof(fclk_name), "pcie%d_fck", port->index);
  189. port->fclk = clk_get(NULL, fclk_name);
  190. if (IS_ERR(port->fclk)) {
  191. ret = PTR_ERR(port->fclk);
  192. goto err_fclk;
  193. }
  194. clk_enable(port->fclk);
  195. /*
  196. * And now, set up the PHY clock
  197. */
  198. clk = &port->phy_clk;
  199. memset(clk, 0, sizeof(struct clk));
  200. clk->parent = &fixed_pciexclkp;
  201. clk->enable_reg = (void __iomem *)(chan->reg_base + SH4A_PCIEPHYCTLR);
  202. clk->enable_bit = BITS_CKE;
  203. ret = sh_clk_mstp32_register(clk, 1);
  204. if (unlikely(ret < 0))
  205. goto err_phy;
  206. return 0;
  207. err_phy:
  208. clk_disable(port->fclk);
  209. clk_put(port->fclk);
  210. err_fclk:
  211. clk_unregister(&fixed_pciexclkp);
  212. return ret;
  213. }
  214. static int __init phy_init(struct sh7786_pcie_port *port)
  215. {
  216. struct pci_channel *chan = port->hose;
  217. unsigned int timeout = 100;
  218. clk_enable(&port->phy_clk);
  219. /* Initialize the phy */
  220. phy_write_reg(chan, 0x60, 0xf, 0x004b008b);
  221. phy_write_reg(chan, 0x61, 0xf, 0x00007b41);
  222. phy_write_reg(chan, 0x64, 0xf, 0x00ff4f00);
  223. phy_write_reg(chan, 0x65, 0xf, 0x09070907);
  224. phy_write_reg(chan, 0x66, 0xf, 0x00000010);
  225. phy_write_reg(chan, 0x74, 0xf, 0x0007001c);
  226. phy_write_reg(chan, 0x79, 0xf, 0x01fc000d);
  227. phy_write_reg(chan, 0xb0, 0xf, 0x00000610);
  228. /* Deassert Standby */
  229. phy_write_reg(chan, 0x67, 0x1, 0x00000400);
  230. /* Disable clock */
  231. clk_disable(&port->phy_clk);
  232. while (timeout--) {
  233. if (pci_read_reg(chan, SH4A_PCIEPHYSR))
  234. return 0;
  235. udelay(100);
  236. }
  237. return -ETIMEDOUT;
  238. }
  239. static void __init pcie_reset(struct sh7786_pcie_port *port)
  240. {
  241. struct pci_channel *chan = port->hose;
  242. pci_write_reg(chan, 1, SH4A_PCIESRSTR);
  243. pci_write_reg(chan, 0, SH4A_PCIETCTLR);
  244. pci_write_reg(chan, 0, SH4A_PCIESRSTR);
  245. pci_write_reg(chan, 0, SH4A_PCIETXVC0SR);
  246. }
  247. static int __init pcie_init(struct sh7786_pcie_port *port)
  248. {
  249. struct pci_channel *chan = port->hose;
  250. unsigned int data;
  251. phys_addr_t memphys;
  252. size_t memsize;
  253. int ret, i, win;
  254. /* Begin initialization */
  255. pcie_reset(port);
  256. /*
  257. * Initial header for port config space is type 1, set the device
  258. * class to match. Hardware takes care of propagating the IDSETR
  259. * settings, so there is no need to bother with a quirk.
  260. */
  261. pci_write_reg(chan, PCI_CLASS_BRIDGE_PCI << 16, SH4A_PCIEIDSETR1);
  262. /* Initialize default capabilities. */
  263. data = pci_read_reg(chan, SH4A_PCIEEXPCAP0);
  264. data &= ~(PCI_EXP_FLAGS_TYPE << 16);
  265. if (port->endpoint)
  266. data |= PCI_EXP_TYPE_ENDPOINT << 20;
  267. else
  268. data |= PCI_EXP_TYPE_ROOT_PORT << 20;
  269. data |= PCI_CAP_ID_EXP;
  270. pci_write_reg(chan, data, SH4A_PCIEEXPCAP0);
  271. /* Enable data link layer active state reporting */
  272. pci_write_reg(chan, PCI_EXP_LNKCAP_DLLLARC, SH4A_PCIEEXPCAP3);
  273. /* Enable extended sync and ASPM L0s support */
  274. data = pci_read_reg(chan, SH4A_PCIEEXPCAP4);
  275. data &= ~PCI_EXP_LNKCTL_ASPMC;
  276. data |= PCI_EXP_LNKCTL_ES | 1;
  277. pci_write_reg(chan, data, SH4A_PCIEEXPCAP4);
  278. /* Write out the physical slot number */
  279. data = pci_read_reg(chan, SH4A_PCIEEXPCAP5);
  280. data &= ~PCI_EXP_SLTCAP_PSN;
  281. data |= (port->index + 1) << 19;
  282. pci_write_reg(chan, data, SH4A_PCIEEXPCAP5);
  283. /* Set the completion timer timeout to the maximum 32ms. */
  284. data = pci_read_reg(chan, SH4A_PCIETLCTLR);
  285. data &= ~0x3f00;
  286. data |= 0x32 << 8;
  287. pci_write_reg(chan, data, SH4A_PCIETLCTLR);
  288. /*
  289. * Set fast training sequences to the maximum 255,
  290. * and enable MAC data scrambling.
  291. */
  292. data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
  293. data &= ~PCIEMACCTLR_SCR_DIS;
  294. data |= (0xff << 16);
  295. pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
  296. memphys = __pa(memory_start);
  297. memsize = roundup_pow_of_two(memory_end - memory_start);
  298. /*
  299. * If there's more than 512MB of memory, we need to roll over to
  300. * LAR1/LAMR1.
  301. */
  302. if (memsize > SZ_512M) {
  303. pci_write_reg(chan, memphys + SZ_512M, SH4A_PCIELAR1);
  304. pci_write_reg(chan, ((memsize - SZ_512M) - SZ_256) | 1,
  305. SH4A_PCIELAMR1);
  306. memsize = SZ_512M;
  307. } else {
  308. /*
  309. * Otherwise just zero it out and disable it.
  310. */
  311. pci_write_reg(chan, 0, SH4A_PCIELAR1);
  312. pci_write_reg(chan, 0, SH4A_PCIELAMR1);
  313. }
  314. /*
  315. * LAR0/LAMR0 covers up to the first 512MB, which is enough to
  316. * cover all of lowmem on most platforms.
  317. */
  318. pci_write_reg(chan, memphys, SH4A_PCIELAR0);
  319. pci_write_reg(chan, (memsize - SZ_256) | 1, SH4A_PCIELAMR0);
  320. /* Finish initialization */
  321. data = pci_read_reg(chan, SH4A_PCIETCTLR);
  322. data |= 0x1;
  323. pci_write_reg(chan, data, SH4A_PCIETCTLR);
  324. /* Let things settle down a bit.. */
  325. mdelay(100);
  326. /* Enable DL_Active Interrupt generation */
  327. data = pci_read_reg(chan, SH4A_PCIEDLINTENR);
  328. data |= PCIEDLINTENR_DLL_ACT_ENABLE;
  329. pci_write_reg(chan, data, SH4A_PCIEDLINTENR);
  330. /* Disable MAC data scrambling. */
  331. data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
  332. data |= PCIEMACCTLR_SCR_DIS | (0xff << 16);
  333. pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
  334. /*
  335. * This will timeout if we don't have a link, but we permit the
  336. * port to register anyways in order to support hotplug on future
  337. * hardware.
  338. */
  339. ret = pci_wait_for_irq(chan, MASK_INT_TX_CTRL);
  340. data = pci_read_reg(chan, SH4A_PCIEPCICONF1);
  341. data &= ~(PCI_STATUS_DEVSEL_MASK << 16);
  342. data |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
  343. (PCI_STATUS_CAP_LIST | PCI_STATUS_DEVSEL_FAST) << 16;
  344. pci_write_reg(chan, data, SH4A_PCIEPCICONF1);
  345. pci_write_reg(chan, 0x80888000, SH4A_PCIETXVC0DCTLR);
  346. pci_write_reg(chan, 0x00222000, SH4A_PCIERXVC0DCTLR);
  347. wmb();
  348. if (ret == 0) {
  349. data = pci_read_reg(chan, SH4A_PCIEMACSR);
  350. printk(KERN_NOTICE "PCI: PCIe#%d x%d link detected\n",
  351. port->index, (data >> 20) & 0x3f);
  352. } else
  353. printk(KERN_NOTICE "PCI: PCIe#%d link down\n",
  354. port->index);
  355. for (i = win = 0; i < chan->nr_resources; i++) {
  356. struct resource *res = chan->resources + i;
  357. resource_size_t size;
  358. u32 mask;
  359. /*
  360. * We can't use the 32-bit mode windows in legacy 29-bit
  361. * mode, so just skip them entirely.
  362. */
  363. if ((res->flags & IORESOURCE_MEM_32BIT) && __in_29bit_mode())
  364. continue;
  365. pci_write_reg(chan, 0x00000000, SH4A_PCIEPTCTLR(win));
  366. /*
  367. * The PAMR mask is calculated in units of 256kB, which
  368. * keeps things pretty simple.
  369. */
  370. size = resource_size(res);
  371. mask = (roundup_pow_of_two(size) / SZ_256K) - 1;
  372. pci_write_reg(chan, mask << 18, SH4A_PCIEPAMR(win));
  373. pci_write_reg(chan, upper_32_bits(res->start),
  374. SH4A_PCIEPARH(win));
  375. pci_write_reg(chan, lower_32_bits(res->start),
  376. SH4A_PCIEPARL(win));
  377. mask = MASK_PARE;
  378. if (res->flags & IORESOURCE_IO)
  379. mask |= MASK_SPC;
  380. pci_write_reg(chan, mask, SH4A_PCIEPTCTLR(win));
  381. win++;
  382. }
  383. return 0;
  384. }
  385. int __init pcibios_map_platform_irq(struct pci_dev *pdev, u8 slot, u8 pin)
  386. {
  387. return 71;
  388. }
  389. static int __init sh7786_pcie_core_init(void)
  390. {
  391. /* Return the number of ports */
  392. return test_mode_pin(MODE_PIN12) ? 3 : 2;
  393. }
  394. static int __init sh7786_pcie_init_hw(struct sh7786_pcie_port *port)
  395. {
  396. int ret;
  397. /*
  398. * Check if we are configured in endpoint or root complex mode,
  399. * this is a fixed pin setting that applies to all PCIe ports.
  400. */
  401. port->endpoint = test_mode_pin(MODE_PIN11);
  402. /*
  403. * Setup clocks, needed both for PHY and PCIe registers.
  404. */
  405. ret = pcie_clk_init(port);
  406. if (unlikely(ret < 0))
  407. return ret;
  408. ret = phy_init(port);
  409. if (unlikely(ret < 0))
  410. return ret;
  411. ret = pcie_init(port);
  412. if (unlikely(ret < 0))
  413. return ret;
  414. return register_pci_controller(port->hose);
  415. }
  416. static struct sh7786_pcie_hwops sh7786_65nm_pcie_hwops __initdata = {
  417. .core_init = sh7786_pcie_core_init,
  418. .port_init_hw = sh7786_pcie_init_hw,
  419. };
  420. static int __init sh7786_pcie_init(void)
  421. {
  422. struct clk *platclk;
  423. int ret = 0, i;
  424. printk(KERN_NOTICE "PCI: Starting initialization.\n");
  425. sh7786_pcie_hwops = &sh7786_65nm_pcie_hwops;
  426. nr_ports = sh7786_pcie_hwops->core_init();
  427. BUG_ON(nr_ports > ARRAY_SIZE(sh7786_pci_channels));
  428. if (unlikely(nr_ports == 0))
  429. return -ENODEV;
  430. sh7786_pcie_ports = kzalloc(nr_ports * sizeof(struct sh7786_pcie_port),
  431. GFP_KERNEL);
  432. if (unlikely(!sh7786_pcie_ports))
  433. return -ENOMEM;
  434. /*
  435. * Fetch any optional platform clock associated with this block.
  436. *
  437. * This is a rather nasty hack for boards with spec-mocking FPGAs
  438. * that have a secondary set of clocks outside of the on-chip
  439. * ones that need to be accounted for before there is any chance
  440. * of touching the existing MSTP bits or CPG clocks.
  441. */
  442. platclk = clk_get(NULL, "pcie_plat_clk");
  443. if (IS_ERR(platclk)) {
  444. /* Sane hardware should probably get a WARN_ON.. */
  445. platclk = NULL;
  446. }
  447. clk_enable(platclk);
  448. printk(KERN_NOTICE "PCI: probing %d ports.\n", nr_ports);
  449. for (i = 0; i < nr_ports; i++) {
  450. struct sh7786_pcie_port *port = sh7786_pcie_ports + i;
  451. port->index = i;
  452. port->hose = sh7786_pci_channels + i;
  453. port->hose->io_map_base = port->hose->resources[0].start;
  454. ret |= sh7786_pcie_hwops->port_init_hw(port);
  455. }
  456. if (unlikely(ret)) {
  457. clk_disable(platclk);
  458. clk_put(platclk);
  459. return ret;
  460. }
  461. return 0;
  462. }
  463. arch_initcall(sh7786_pcie_init);