pcie-sh7786.c 10 KB

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