pcie-sh7786.c 15 KB

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