pcie-sh7786.c 12 KB

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