pcie-sh7786.c 13 KB

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