pcie-sh7786.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. },
  51. };
  52. static struct resource sh7786_pci1_resources[] = {
  53. {
  54. .name = "PCIe1 IO",
  55. .start = 0xfd800000,
  56. .end = 0xfd800000 + SZ_8M - 1,
  57. .flags = IORESOURCE_IO,
  58. }, {
  59. .name = "PCIe1 MEM 0",
  60. .start = 0xa0000000,
  61. .end = 0xa0000000 + SZ_512M - 1,
  62. .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
  63. }, {
  64. .name = "PCIe1 MEM 1",
  65. .start = 0x30000000,
  66. .end = 0x30000000 + SZ_256M - 1,
  67. .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
  68. }, {
  69. .name = "PCIe1 MEM 2",
  70. .start = 0xfe300000,
  71. .end = 0xfe300000 + SZ_1M - 1,
  72. },
  73. };
  74. static struct resource sh7786_pci2_resources[] = {
  75. {
  76. .name = "PCIe2 IO",
  77. .start = 0xfc800000,
  78. .end = 0xfc800000 + SZ_4M - 1,
  79. }, {
  80. .name = "PCIe2 MEM 0",
  81. .start = 0x80000000,
  82. .end = 0x80000000 + SZ_512M - 1,
  83. .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
  84. }, {
  85. .name = "PCIe2 MEM 1",
  86. .start = 0x20000000,
  87. .end = 0x20000000 + SZ_256M - 1,
  88. .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
  89. }, {
  90. .name = "PCIe2 MEM 2",
  91. .start = 0xfcd00000,
  92. .end = 0xfcd00000 + SZ_1M - 1,
  93. },
  94. };
  95. extern struct pci_ops sh7786_pci_ops;
  96. #define DEFINE_CONTROLLER(start, idx) \
  97. { \
  98. .pci_ops = &sh7786_pci_ops, \
  99. .resources = sh7786_pci##idx##_resources, \
  100. .nr_resources = ARRAY_SIZE(sh7786_pci##idx##_resources), \
  101. .reg_base = start, \
  102. .mem_offset = 0, \
  103. .io_offset = 0, \
  104. }
  105. static struct pci_channel sh7786_pci_channels[] = {
  106. DEFINE_CONTROLLER(0xfe000000, 0),
  107. DEFINE_CONTROLLER(0xfe200000, 1),
  108. DEFINE_CONTROLLER(0xfcc00000, 2),
  109. };
  110. static int phy_wait_for_ack(struct pci_channel *chan)
  111. {
  112. unsigned int timeout = 100;
  113. while (timeout--) {
  114. if (pci_read_reg(chan, SH4A_PCIEPHYADRR) & (1 << BITS_ACK))
  115. return 0;
  116. udelay(100);
  117. }
  118. return -ETIMEDOUT;
  119. }
  120. static int pci_wait_for_irq(struct pci_channel *chan, unsigned int mask)
  121. {
  122. unsigned int timeout = 100;
  123. while (timeout--) {
  124. if ((pci_read_reg(chan, SH4A_PCIEINTR) & mask) == mask)
  125. return 0;
  126. udelay(100);
  127. }
  128. return -ETIMEDOUT;
  129. }
  130. static void phy_write_reg(struct pci_channel *chan, unsigned int addr,
  131. unsigned int lane, unsigned int data)
  132. {
  133. unsigned long phyaddr, ctrl;
  134. phyaddr = (1 << BITS_CMD) + ((lane & 0xf) << BITS_LANE) +
  135. ((addr & 0xff) << BITS_ADR);
  136. /* Enable clock */
  137. ctrl = pci_read_reg(chan, SH4A_PCIEPHYCTLR);
  138. ctrl |= (1 << BITS_CKE);
  139. pci_write_reg(chan, ctrl, SH4A_PCIEPHYCTLR);
  140. /* Set write data */
  141. pci_write_reg(chan, data, SH4A_PCIEPHYDOUTR);
  142. pci_write_reg(chan, phyaddr, SH4A_PCIEPHYADRR);
  143. phy_wait_for_ack(chan);
  144. /* Clear command */
  145. pci_write_reg(chan, 0, SH4A_PCIEPHYADRR);
  146. phy_wait_for_ack(chan);
  147. /* Disable clock */
  148. ctrl = pci_read_reg(chan, SH4A_PCIEPHYCTLR);
  149. ctrl &= ~(1 << BITS_CKE);
  150. pci_write_reg(chan, ctrl, SH4A_PCIEPHYCTLR);
  151. }
  152. static int phy_init(struct pci_channel *chan)
  153. {
  154. unsigned int timeout = 100;
  155. /* Initialize the phy */
  156. phy_write_reg(chan, 0x60, 0xf, 0x004b008b);
  157. phy_write_reg(chan, 0x61, 0xf, 0x00007b41);
  158. phy_write_reg(chan, 0x64, 0xf, 0x00ff4f00);
  159. phy_write_reg(chan, 0x65, 0xf, 0x09070907);
  160. phy_write_reg(chan, 0x66, 0xf, 0x00000010);
  161. phy_write_reg(chan, 0x74, 0xf, 0x0007001c);
  162. phy_write_reg(chan, 0x79, 0xf, 0x01fc000d);
  163. /* Deassert Standby */
  164. phy_write_reg(chan, 0x67, 0xf, 0x00000400);
  165. while (timeout--) {
  166. if (pci_read_reg(chan, SH4A_PCIEPHYSR))
  167. return 0;
  168. udelay(100);
  169. }
  170. return -ETIMEDOUT;
  171. }
  172. static int pcie_init(struct sh7786_pcie_port *port)
  173. {
  174. struct pci_channel *chan = port->hose;
  175. unsigned int data;
  176. phys_addr_t memphys;
  177. size_t memsize;
  178. int ret, i;
  179. /* Begin initialization */
  180. pci_write_reg(chan, 0, SH4A_PCIETCTLR);
  181. /* Initialize as type1. */
  182. data = pci_read_reg(chan, SH4A_PCIEPCICONF3);
  183. data &= ~(0x7f << 16);
  184. data |= PCI_HEADER_TYPE_BRIDGE << 16;
  185. pci_write_reg(chan, data, SH4A_PCIEPCICONF3);
  186. /* Initialize default capabilities. */
  187. data = pci_read_reg(chan, SH4A_PCIEEXPCAP0);
  188. data &= ~(PCI_EXP_FLAGS_TYPE << 16);
  189. if (port->endpoint)
  190. data |= PCI_EXP_TYPE_ENDPOINT << 20;
  191. else
  192. data |= PCI_EXP_TYPE_ROOT_PORT << 20;
  193. data |= PCI_CAP_ID_EXP;
  194. pci_write_reg(chan, data, SH4A_PCIEEXPCAP0);
  195. /* Enable data link layer active state reporting */
  196. pci_write_reg(chan, PCI_EXP_LNKCAP_DLLLARC, SH4A_PCIEEXPCAP3);
  197. /* Enable extended sync and ASPM L0s support */
  198. data = pci_read_reg(chan, SH4A_PCIEEXPCAP4);
  199. data &= ~PCI_EXP_LNKCTL_ASPMC;
  200. data |= PCI_EXP_LNKCTL_ES | 1;
  201. pci_write_reg(chan, data, SH4A_PCIEEXPCAP4);
  202. /* Write out the physical slot number */
  203. data = pci_read_reg(chan, SH4A_PCIEEXPCAP5);
  204. data &= ~PCI_EXP_SLTCAP_PSN;
  205. data |= (port->index + 1) << 19;
  206. pci_write_reg(chan, data, SH4A_PCIEEXPCAP5);
  207. /* Set the completion timer timeout to the maximum 32ms. */
  208. data = pci_read_reg(chan, SH4A_PCIETLCTLR);
  209. data &= ~0x3f00;
  210. data |= 0x32 << 8;
  211. pci_write_reg(chan, data, SH4A_PCIETLCTLR);
  212. /*
  213. * Set fast training sequences to the maximum 255,
  214. * and enable MAC data scrambling.
  215. */
  216. data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
  217. data &= ~PCIEMACCTLR_SCR_DIS;
  218. data |= (0xff << 16);
  219. pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
  220. memphys = __pa(memory_start);
  221. memsize = roundup_pow_of_two(memory_end - memory_start);
  222. /*
  223. * If there's more than 512MB of memory, we need to roll over to
  224. * LAR1/LAMR1.
  225. */
  226. if (memsize > SZ_512M) {
  227. __raw_writel(memphys + SZ_512M, chan->reg_base + SH4A_PCIELAR1);
  228. __raw_writel(((memsize - SZ_512M) - SZ_256) | 1,
  229. chan->reg_base + SH4A_PCIELAMR1);
  230. memsize = SZ_512M;
  231. } else {
  232. /*
  233. * Otherwise just zero it out and disable it.
  234. */
  235. __raw_writel(0, chan->reg_base + SH4A_PCIELAR1);
  236. __raw_writel(0, chan->reg_base + SH4A_PCIELAMR1);
  237. }
  238. /*
  239. * LAR0/LAMR0 covers up to the first 512MB, which is enough to
  240. * cover all of lowmem on most platforms.
  241. */
  242. __raw_writel(memphys, chan->reg_base + SH4A_PCIELAR0);
  243. __raw_writel((memsize - SZ_256) | 1, chan->reg_base + SH4A_PCIELAMR0);
  244. /* Finish initialization */
  245. data = pci_read_reg(chan, SH4A_PCIETCTLR);
  246. data |= 0x1;
  247. pci_write_reg(chan, data, SH4A_PCIETCTLR);
  248. /* Enable DL_Active Interrupt generation */
  249. data = pci_read_reg(chan, SH4A_PCIEDLINTENR);
  250. data |= PCIEDLINTENR_DLL_ACT_ENABLE;
  251. pci_write_reg(chan, data, SH4A_PCIEDLINTENR);
  252. /* Disable MAC data scrambling. */
  253. data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
  254. data |= PCIEMACCTLR_SCR_DIS | (0xff << 16);
  255. pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
  256. ret = pci_wait_for_irq(chan, MASK_INT_TX_CTRL);
  257. if (unlikely(ret != 0))
  258. return -ENODEV;
  259. data = pci_read_reg(chan, SH4A_PCIEPCICONF1);
  260. data &= ~(PCI_STATUS_DEVSEL_MASK << 16);
  261. data |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
  262. (PCI_STATUS_CAP_LIST | PCI_STATUS_DEVSEL_FAST) << 16;
  263. pci_write_reg(chan, data, SH4A_PCIEPCICONF1);
  264. pci_write_reg(chan, 0x80888000, SH4A_PCIETXVC0DCTLR);
  265. pci_write_reg(chan, 0x00222000, SH4A_PCIERXVC0DCTLR);
  266. wmb();
  267. data = pci_read_reg(chan, SH4A_PCIEMACSR);
  268. printk(KERN_NOTICE "PCI: PCIe#%d link width %d\n",
  269. port->index, (data >> 20) & 0x3f);
  270. for (i = 0; i < chan->nr_resources; i++) {
  271. struct resource *res = chan->resources + i;
  272. resource_size_t size;
  273. u32 enable_mask;
  274. pci_write_reg(chan, 0x00000000, SH4A_PCIEPTCTLR(i));
  275. size = resource_size(res);
  276. /*
  277. * The PAMR mask is calculated in units of 256kB, which
  278. * keeps things pretty simple.
  279. */
  280. __raw_writel(((roundup_pow_of_two(size) / SZ_256K) - 1) << 18,
  281. chan->reg_base + SH4A_PCIEPAMR(i));
  282. pci_write_reg(chan, 0x00000000, SH4A_PCIEPARH(i));
  283. pci_write_reg(chan, 0x00000000, SH4A_PCIEPARL(i));
  284. enable_mask = MASK_PARE;
  285. if (res->flags & IORESOURCE_IO)
  286. enable_mask |= MASK_SPC;
  287. pci_write_reg(chan, enable_mask, SH4A_PCIEPTCTLR(i));
  288. }
  289. return 0;
  290. }
  291. int __init pcibios_map_platform_irq(struct pci_dev *pdev, u8 slot, u8 pin)
  292. {
  293. return 71;
  294. }
  295. static int sh7786_pcie_core_init(void)
  296. {
  297. /* Return the number of ports */
  298. return test_mode_pin(MODE_PIN12) ? 3 : 2;
  299. }
  300. static int __devinit sh7786_pcie_init_hw(struct sh7786_pcie_port *port)
  301. {
  302. int ret;
  303. ret = phy_init(port->hose);
  304. if (unlikely(ret < 0))
  305. return ret;
  306. /*
  307. * Check if we are configured in endpoint or root complex mode,
  308. * this is a fixed pin setting that applies to all PCIe ports.
  309. */
  310. port->endpoint = test_mode_pin(MODE_PIN11);
  311. ret = pcie_init(port);
  312. if (unlikely(ret < 0))
  313. return ret;
  314. return register_pci_controller(port->hose);
  315. }
  316. static struct sh7786_pcie_hwops sh7786_65nm_pcie_hwops __initdata = {
  317. .core_init = sh7786_pcie_core_init,
  318. .port_init_hw = sh7786_pcie_init_hw,
  319. };
  320. static int __init sh7786_pcie_init(void)
  321. {
  322. int ret = 0, i;
  323. printk(KERN_NOTICE "PCI: Starting intialization.\n");
  324. sh7786_pcie_hwops = &sh7786_65nm_pcie_hwops;
  325. nr_ports = sh7786_pcie_hwops->core_init();
  326. BUG_ON(nr_ports > ARRAY_SIZE(sh7786_pci_channels));
  327. if (unlikely(nr_ports == 0))
  328. return -ENODEV;
  329. sh7786_pcie_ports = kzalloc(nr_ports * sizeof(struct sh7786_pcie_port),
  330. GFP_KERNEL);
  331. if (unlikely(!sh7786_pcie_ports))
  332. return -ENOMEM;
  333. printk(KERN_NOTICE "PCI: probing %d ports.\n", nr_ports);
  334. for (i = 0; i < nr_ports; i++) {
  335. struct sh7786_pcie_port *port = sh7786_pcie_ports + i;
  336. port->index = i;
  337. port->hose = sh7786_pci_channels + i;
  338. port->hose->io_map_base = port->hose->resources[0].start;
  339. ret |= sh7786_pcie_hwops->port_init_hw(port);
  340. }
  341. if (unlikely(ret))
  342. return ret;
  343. return 0;
  344. }
  345. arch_initcall(sh7786_pcie_init);