pcie-sh7786.c 14 KB

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