pcie-sh7786.c 15 KB

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