pcie-sh7786.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Low-Level PCI Express Support for the SH7786
  3. *
  4. * Copyright (C) 2009 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_pci_32bit_mem_resources[] = {
  30. {
  31. .name = "pci0_mem",
  32. .start = SH4A_PCIMEM_BASEA,
  33. .end = SH4A_PCIMEM_BASEA + SZ_64M - 1,
  34. .flags = IORESOURCE_MEM,
  35. }, {
  36. .name = "pci1_mem",
  37. .start = SH4A_PCIMEM_BASEA1,
  38. .end = SH4A_PCIMEM_BASEA1 + SZ_64M - 1,
  39. .flags = IORESOURCE_MEM,
  40. }, {
  41. .name = "pci2_mem",
  42. .start = SH4A_PCIMEM_BASEA2,
  43. .end = SH4A_PCIMEM_BASEA2 + SZ_64M - 1,
  44. .flags = IORESOURCE_MEM,
  45. },
  46. };
  47. static struct resource sh7786_pci_29bit_mem_resource = {
  48. .start = SH4A_PCIMEM_BASE,
  49. .end = SH4A_PCIMEM_BASE + SZ_64M - 1,
  50. .flags = IORESOURCE_MEM,
  51. };
  52. static struct resource sh7786_pci_io_resources[] = {
  53. {
  54. .name = "pci0_io",
  55. .start = SH4A_PCIIO_BASE,
  56. .end = SH4A_PCIIO_BASE + SZ_8M - 1,
  57. .flags = IORESOURCE_IO,
  58. }, {
  59. .name = "pci1_io",
  60. .start = SH4A_PCIIO_BASE1,
  61. .end = SH4A_PCIIO_BASE1 + SZ_8M - 1,
  62. .flags = IORESOURCE_IO,
  63. }, {
  64. .name = "pci2_io",
  65. .start = SH4A_PCIIO_BASE2,
  66. .end = SH4A_PCIIO_BASE2 + SZ_4M - 1,
  67. .flags = IORESOURCE_IO,
  68. },
  69. };
  70. extern struct pci_ops sh7786_pci_ops;
  71. #define DEFINE_CONTROLLER(start, idx) \
  72. { \
  73. .pci_ops = &sh7786_pci_ops, \
  74. .reg_base = start, \
  75. /* mem_resource filled in at probe time */ \
  76. .mem_offset = 0, \
  77. .io_resource = &sh7786_pci_io_resources[idx], \
  78. .io_offset = 0, \
  79. }
  80. static struct pci_channel sh7786_pci_channels[] = {
  81. DEFINE_CONTROLLER(0xfe000000, 0),
  82. DEFINE_CONTROLLER(0xfe200000, 1),
  83. DEFINE_CONTROLLER(0xfcc00000, 2),
  84. };
  85. static int phy_wait_for_ack(struct pci_channel *chan)
  86. {
  87. unsigned int timeout = 100;
  88. while (timeout--) {
  89. if (pci_read_reg(chan, SH4A_PCIEPHYADRR) & (1 << BITS_ACK))
  90. return 0;
  91. udelay(100);
  92. }
  93. return -ETIMEDOUT;
  94. }
  95. static int pci_wait_for_irq(struct pci_channel *chan, unsigned int mask)
  96. {
  97. unsigned int timeout = 100;
  98. while (timeout--) {
  99. if ((pci_read_reg(chan, SH4A_PCIEINTR) & mask) == mask)
  100. return 0;
  101. udelay(100);
  102. }
  103. return -ETIMEDOUT;
  104. }
  105. static void phy_write_reg(struct pci_channel *chan, unsigned int addr,
  106. unsigned int lane, unsigned int data)
  107. {
  108. unsigned long phyaddr, ctrl;
  109. phyaddr = (1 << BITS_CMD) + ((lane & 0xf) << BITS_LANE) +
  110. ((addr & 0xff) << BITS_ADR);
  111. /* Enable clock */
  112. ctrl = pci_read_reg(chan, SH4A_PCIEPHYCTLR);
  113. ctrl |= (1 << BITS_CKE);
  114. pci_write_reg(chan, ctrl, SH4A_PCIEPHYCTLR);
  115. /* Set write data */
  116. pci_write_reg(chan, data, SH4A_PCIEPHYDOUTR);
  117. pci_write_reg(chan, phyaddr, SH4A_PCIEPHYADRR);
  118. phy_wait_for_ack(chan);
  119. /* Clear command */
  120. pci_write_reg(chan, 0, SH4A_PCIEPHYADRR);
  121. phy_wait_for_ack(chan);
  122. /* Disable clock */
  123. ctrl = pci_read_reg(chan, SH4A_PCIEPHYCTLR);
  124. ctrl &= ~(1 << BITS_CKE);
  125. pci_write_reg(chan, ctrl, SH4A_PCIEPHYCTLR);
  126. }
  127. static int phy_init(struct pci_channel *chan)
  128. {
  129. unsigned int timeout = 100;
  130. /* Initialize the phy */
  131. phy_write_reg(chan, 0x60, 0xf, 0x004b008b);
  132. phy_write_reg(chan, 0x61, 0xf, 0x00007b41);
  133. phy_write_reg(chan, 0x64, 0xf, 0x00ff4f00);
  134. phy_write_reg(chan, 0x65, 0xf, 0x09070907);
  135. phy_write_reg(chan, 0x66, 0xf, 0x00000010);
  136. phy_write_reg(chan, 0x74, 0xf, 0x0007001c);
  137. phy_write_reg(chan, 0x79, 0xf, 0x01fc000d);
  138. /* Deassert Standby */
  139. phy_write_reg(chan, 0x67, 0xf, 0x00000400);
  140. while (timeout--) {
  141. if (pci_read_reg(chan, SH4A_PCIEPHYSR))
  142. return 0;
  143. udelay(100);
  144. }
  145. return -ETIMEDOUT;
  146. }
  147. static int pcie_init(struct sh7786_pcie_port *port)
  148. {
  149. struct pci_channel *chan = port->hose;
  150. unsigned int data;
  151. int ret;
  152. /* Begin initialization */
  153. pci_write_reg(chan, 0, SH4A_PCIETCTLR);
  154. /* Initialize as type1. */
  155. data = pci_read_reg(chan, SH4A_PCIEPCICONF3);
  156. data &= ~(0x7f << 16);
  157. data |= PCI_HEADER_TYPE_BRIDGE << 16;
  158. pci_write_reg(chan, data, SH4A_PCIEPCICONF3);
  159. /* Initialize default capabilities. */
  160. data = pci_read_reg(chan, SH4A_PCIEEXPCAP0);
  161. data &= ~(PCI_EXP_FLAGS_TYPE << 16);
  162. if (port->endpoint)
  163. data |= PCI_EXP_TYPE_ENDPOINT << 20;
  164. else
  165. data |= PCI_EXP_TYPE_ROOT_PORT << 20;
  166. data |= PCI_CAP_ID_EXP;
  167. pci_write_reg(chan, data, SH4A_PCIEEXPCAP0);
  168. /* Enable x4 link width and extended sync. */
  169. data = pci_read_reg(chan, SH4A_PCIEEXPCAP4);
  170. data &= ~(PCI_EXP_LNKSTA_NLW << 16);
  171. data |= (1 << 22) | PCI_EXP_LNKCTL_ES;
  172. pci_write_reg(chan, data, SH4A_PCIEEXPCAP4);
  173. /* Set the completion timer timeout to the maximum 32ms. */
  174. data = pci_read_reg(chan, SH4A_PCIETLCTLR);
  175. data &= ~0xffff;
  176. data |= 0x32 << 8;
  177. pci_write_reg(chan, data, SH4A_PCIETLCTLR);
  178. /*
  179. * Set fast training sequences to the maximum 255,
  180. * and enable MAC data scrambling.
  181. */
  182. data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
  183. data &= ~PCIEMACCTLR_SCR_DIS;
  184. data |= (0xff << 16);
  185. pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
  186. /* Finish initialization */
  187. data = pci_read_reg(chan, SH4A_PCIETCTLR);
  188. data |= 0x1;
  189. pci_write_reg(chan, data, SH4A_PCIETCTLR);
  190. /* Enable DL_Active Interrupt generation */
  191. data = pci_read_reg(chan, SH4A_PCIEDLINTENR);
  192. data |= PCIEDLINTENR_DLL_ACT_ENABLE;
  193. pci_write_reg(chan, data, SH4A_PCIEDLINTENR);
  194. /* Disable MAC data scrambling. */
  195. data = pci_read_reg(chan, SH4A_PCIEMACCTLR);
  196. data |= PCIEMACCTLR_SCR_DIS | (0xff << 16);
  197. pci_write_reg(chan, data, SH4A_PCIEMACCTLR);
  198. ret = pci_wait_for_irq(chan, MASK_INT_TX_CTRL);
  199. if (unlikely(ret != 0))
  200. return -ENODEV;
  201. pci_write_reg(chan, 0x00100007, SH4A_PCIEPCICONF1);
  202. pci_write_reg(chan, 0x80888000, SH4A_PCIETXVC0DCTLR);
  203. pci_write_reg(chan, 0x00222000, SH4A_PCIERXVC0DCTLR);
  204. pci_write_reg(chan, 0x000050A0, SH4A_PCIEEXPCAP2);
  205. wmb();
  206. data = pci_read_reg(chan, SH4A_PCIEMACSR);
  207. printk(KERN_NOTICE "PCI: PCIe#%d link width %d\n",
  208. port->index, (data >> 20) & 0x3f);
  209. pci_write_reg(chan, 0x007c0000, SH4A_PCIEPAMR0);
  210. pci_write_reg(chan, 0x00000000, SH4A_PCIEPARH0);
  211. pci_write_reg(chan, 0x00000000, SH4A_PCIEPARL0);
  212. pci_write_reg(chan, 0x80000100, SH4A_PCIEPTCTLR0);
  213. pci_write_reg(chan, 0x03fc0000, SH4A_PCIEPAMR2);
  214. pci_write_reg(chan, 0x00000000, SH4A_PCIEPARH2);
  215. pci_write_reg(chan, 0x00000000, SH4A_PCIEPARL2);
  216. pci_write_reg(chan, 0x80000000, SH4A_PCIEPTCTLR2);
  217. return 0;
  218. }
  219. int __init pcibios_map_platform_irq(struct pci_dev *pdev, u8 slot, u8 pin)
  220. {
  221. return 71;
  222. }
  223. static int sh7786_pcie_core_init(void)
  224. {
  225. /* Return the number of ports */
  226. return test_mode_pin(MODE_PIN12) ? 3 : 2;
  227. }
  228. static int __devinit sh7786_pcie_init_hw(struct sh7786_pcie_port *port)
  229. {
  230. int ret;
  231. ret = phy_init(port->hose);
  232. if (unlikely(ret < 0))
  233. return ret;
  234. /*
  235. * Check if we are configured in endpoint or root complex mode,
  236. * this is a fixed pin setting that applies to all PCIe ports.
  237. */
  238. port->endpoint = test_mode_pin(MODE_PIN11);
  239. ret = pcie_init(port);
  240. if (unlikely(ret < 0))
  241. return ret;
  242. register_pci_controller(port->hose);
  243. return 0;
  244. }
  245. static struct sh7786_pcie_hwops sh7786_65nm_pcie_hwops __initdata = {
  246. .core_init = sh7786_pcie_core_init,
  247. .port_init_hw = sh7786_pcie_init_hw,
  248. };
  249. static int __init sh7786_pcie_init(void)
  250. {
  251. int ret = 0, i;
  252. printk(KERN_NOTICE "PCI: Starting intialization.\n");
  253. sh7786_pcie_hwops = &sh7786_65nm_pcie_hwops;
  254. nr_ports = sh7786_pcie_hwops->core_init();
  255. BUG_ON(nr_ports > ARRAY_SIZE(sh7786_pci_channels));
  256. if (unlikely(nr_ports == 0))
  257. return -ENODEV;
  258. sh7786_pcie_ports = kzalloc(nr_ports * sizeof(struct sh7786_pcie_port),
  259. GFP_KERNEL);
  260. if (unlikely(!sh7786_pcie_ports))
  261. return -ENOMEM;
  262. printk(KERN_NOTICE "PCI: probing %d ports.\n", nr_ports);
  263. for (i = 0; i < nr_ports; i++) {
  264. struct sh7786_pcie_port *port = sh7786_pcie_ports + i;
  265. port->index = i;
  266. port->hose = sh7786_pci_channels + i;
  267. port->hose->io_map_base = port->hose->io_resource->start;
  268. /*
  269. * Check if we are booting in 29 or 32-bit mode
  270. *
  271. * 32-bit mode provides each controller with its own
  272. * memory window, while 29-bit mode uses a shared one.
  273. */
  274. port->hose->mem_resource = test_mode_pin(MODE_PIN10) ?
  275. &sh7786_pci_32bit_mem_resources[i] :
  276. &sh7786_pci_29bit_mem_resource;
  277. ret |= sh7786_pcie_hwops->port_init_hw(port);
  278. }
  279. if (unlikely(ret))
  280. return ret;
  281. return 0;
  282. }
  283. arch_initcall(sh7786_pcie_init);