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