pci-lantiq.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/pci.h>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/delay.h>
  13. #include <linux/mm.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/module.h>
  16. #include <linux/clk.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_pci.h>
  21. #include <asm/gpio.h>
  22. #include <asm/addrspace.h>
  23. #include <lantiq_soc.h>
  24. #include <lantiq_irq.h>
  25. #include "pci-lantiq.h"
  26. #define PCI_CR_FCI_ADDR_MAP0 0x00C0
  27. #define PCI_CR_FCI_ADDR_MAP1 0x00C4
  28. #define PCI_CR_FCI_ADDR_MAP2 0x00C8
  29. #define PCI_CR_FCI_ADDR_MAP3 0x00CC
  30. #define PCI_CR_FCI_ADDR_MAP4 0x00D0
  31. #define PCI_CR_FCI_ADDR_MAP5 0x00D4
  32. #define PCI_CR_FCI_ADDR_MAP6 0x00D8
  33. #define PCI_CR_FCI_ADDR_MAP7 0x00DC
  34. #define PCI_CR_CLK_CTRL 0x0000
  35. #define PCI_CR_PCI_MOD 0x0030
  36. #define PCI_CR_PC_ARB 0x0080
  37. #define PCI_CR_FCI_ADDR_MAP11hg 0x00E4
  38. #define PCI_CR_BAR11MASK 0x0044
  39. #define PCI_CR_BAR12MASK 0x0048
  40. #define PCI_CR_BAR13MASK 0x004C
  41. #define PCI_CS_BASE_ADDR1 0x0010
  42. #define PCI_CR_PCI_ADDR_MAP11 0x0064
  43. #define PCI_CR_FCI_BURST_LENGTH 0x00E8
  44. #define PCI_CR_PCI_EOI 0x002C
  45. #define PCI_CS_STS_CMD 0x0004
  46. #define PCI_MASTER0_REQ_MASK_2BITS 8
  47. #define PCI_MASTER1_REQ_MASK_2BITS 10
  48. #define PCI_MASTER2_REQ_MASK_2BITS 12
  49. #define INTERNAL_ARB_ENABLE_BIT 0
  50. #define LTQ_CGU_IFCCR 0x0018
  51. #define LTQ_CGU_PCICR 0x0034
  52. #define ltq_pci_w32(x, y) ltq_w32((x), ltq_pci_membase + (y))
  53. #define ltq_pci_r32(x) ltq_r32(ltq_pci_membase + (x))
  54. #define ltq_pci_cfg_w32(x, y) ltq_w32((x), ltq_pci_mapped_cfg + (y))
  55. #define ltq_pci_cfg_r32(x) ltq_r32(ltq_pci_mapped_cfg + (x))
  56. __iomem void *ltq_pci_mapped_cfg;
  57. static __iomem void *ltq_pci_membase;
  58. static int reset_gpio;
  59. static struct clk *clk_pci, *clk_external;
  60. static struct resource pci_io_resource;
  61. static struct resource pci_mem_resource;
  62. static struct pci_ops pci_ops = {
  63. .read = ltq_pci_read_config_dword,
  64. .write = ltq_pci_write_config_dword
  65. };
  66. static struct pci_controller pci_controller = {
  67. .pci_ops = &pci_ops,
  68. .mem_resource = &pci_mem_resource,
  69. .mem_offset = 0x00000000UL,
  70. .io_resource = &pci_io_resource,
  71. .io_offset = 0x00000000UL,
  72. };
  73. static inline u32 ltq_calc_bar11mask(void)
  74. {
  75. u32 mem, bar11mask;
  76. /* BAR11MASK value depends on available memory on system. */
  77. mem = get_num_physpages() * PAGE_SIZE;
  78. bar11mask = (0x0ffffff0 & ~((1 << (fls(mem) - 1)) - 1)) | 8;
  79. return bar11mask;
  80. }
  81. static int ltq_pci_startup(struct platform_device *pdev)
  82. {
  83. struct device_node *node = pdev->dev.of_node;
  84. const __be32 *req_mask, *bus_clk;
  85. u32 temp_buffer;
  86. /* get our clocks */
  87. clk_pci = clk_get(&pdev->dev, NULL);
  88. if (IS_ERR(clk_pci)) {
  89. dev_err(&pdev->dev, "failed to get pci clock\n");
  90. return PTR_ERR(clk_pci);
  91. }
  92. clk_external = clk_get(&pdev->dev, "external");
  93. if (IS_ERR(clk_external)) {
  94. clk_put(clk_pci);
  95. dev_err(&pdev->dev, "failed to get external pci clock\n");
  96. return PTR_ERR(clk_external);
  97. }
  98. /* read the bus speed that we want */
  99. bus_clk = of_get_property(node, "lantiq,bus-clock", NULL);
  100. if (bus_clk)
  101. clk_set_rate(clk_pci, *bus_clk);
  102. /* and enable the clocks */
  103. clk_enable(clk_pci);
  104. if (of_find_property(node, "lantiq,external-clock", NULL))
  105. clk_enable(clk_external);
  106. else
  107. clk_disable(clk_external);
  108. /* setup reset gpio used by pci */
  109. reset_gpio = of_get_named_gpio(node, "gpio-reset", 0);
  110. if (gpio_is_valid(reset_gpio)) {
  111. int ret = devm_gpio_request(&pdev->dev,
  112. reset_gpio, "pci-reset");
  113. if (ret) {
  114. dev_err(&pdev->dev,
  115. "failed to request gpio %d\n", reset_gpio);
  116. return ret;
  117. }
  118. gpio_direction_output(reset_gpio, 1);
  119. }
  120. /* enable auto-switching between PCI and EBU */
  121. ltq_pci_w32(0xa, PCI_CR_CLK_CTRL);
  122. /* busy, i.e. configuration is not done, PCI access has to be retried */
  123. ltq_pci_w32(ltq_pci_r32(PCI_CR_PCI_MOD) & ~(1 << 24), PCI_CR_PCI_MOD);
  124. wmb();
  125. /* BUS Master/IO/MEM access */
  126. ltq_pci_cfg_w32(ltq_pci_cfg_r32(PCI_CS_STS_CMD) | 7, PCI_CS_STS_CMD);
  127. /* enable external 2 PCI masters */
  128. temp_buffer = ltq_pci_r32(PCI_CR_PC_ARB);
  129. /* setup the request mask */
  130. req_mask = of_get_property(node, "req-mask", NULL);
  131. if (req_mask)
  132. temp_buffer &= ~((*req_mask & 0xf) << 16);
  133. else
  134. temp_buffer &= ~0xf0000;
  135. /* enable internal arbiter */
  136. temp_buffer |= (1 << INTERNAL_ARB_ENABLE_BIT);
  137. /* enable internal PCI master reqest */
  138. temp_buffer &= (~(3 << PCI_MASTER0_REQ_MASK_2BITS));
  139. /* enable EBU request */
  140. temp_buffer &= (~(3 << PCI_MASTER1_REQ_MASK_2BITS));
  141. /* enable all external masters request */
  142. temp_buffer &= (~(3 << PCI_MASTER2_REQ_MASK_2BITS));
  143. ltq_pci_w32(temp_buffer, PCI_CR_PC_ARB);
  144. wmb();
  145. /* setup BAR memory regions */
  146. ltq_pci_w32(0x18000000, PCI_CR_FCI_ADDR_MAP0);
  147. ltq_pci_w32(0x18400000, PCI_CR_FCI_ADDR_MAP1);
  148. ltq_pci_w32(0x18800000, PCI_CR_FCI_ADDR_MAP2);
  149. ltq_pci_w32(0x18c00000, PCI_CR_FCI_ADDR_MAP3);
  150. ltq_pci_w32(0x19000000, PCI_CR_FCI_ADDR_MAP4);
  151. ltq_pci_w32(0x19400000, PCI_CR_FCI_ADDR_MAP5);
  152. ltq_pci_w32(0x19800000, PCI_CR_FCI_ADDR_MAP6);
  153. ltq_pci_w32(0x19c00000, PCI_CR_FCI_ADDR_MAP7);
  154. ltq_pci_w32(0x1ae00000, PCI_CR_FCI_ADDR_MAP11hg);
  155. ltq_pci_w32(ltq_calc_bar11mask(), PCI_CR_BAR11MASK);
  156. ltq_pci_w32(0, PCI_CR_PCI_ADDR_MAP11);
  157. ltq_pci_w32(0, PCI_CS_BASE_ADDR1);
  158. /* both TX and RX endian swap are enabled */
  159. ltq_pci_w32(ltq_pci_r32(PCI_CR_PCI_EOI) | 3, PCI_CR_PCI_EOI);
  160. wmb();
  161. ltq_pci_w32(ltq_pci_r32(PCI_CR_BAR12MASK) | 0x80000000,
  162. PCI_CR_BAR12MASK);
  163. ltq_pci_w32(ltq_pci_r32(PCI_CR_BAR13MASK) | 0x80000000,
  164. PCI_CR_BAR13MASK);
  165. /*use 8 dw burst length */
  166. ltq_pci_w32(0x303, PCI_CR_FCI_BURST_LENGTH);
  167. ltq_pci_w32(ltq_pci_r32(PCI_CR_PCI_MOD) | (1 << 24), PCI_CR_PCI_MOD);
  168. wmb();
  169. /* setup irq line */
  170. ltq_ebu_w32(ltq_ebu_r32(LTQ_EBU_PCC_CON) | 0xc, LTQ_EBU_PCC_CON);
  171. ltq_ebu_w32(ltq_ebu_r32(LTQ_EBU_PCC_IEN) | 0x10, LTQ_EBU_PCC_IEN);
  172. /* toggle reset pin */
  173. if (gpio_is_valid(reset_gpio)) {
  174. __gpio_set_value(reset_gpio, 0);
  175. wmb();
  176. mdelay(1);
  177. __gpio_set_value(reset_gpio, 1);
  178. }
  179. return 0;
  180. }
  181. static int ltq_pci_probe(struct platform_device *pdev)
  182. {
  183. struct resource *res_cfg, *res_bridge;
  184. pci_clear_flags(PCI_PROBE_ONLY);
  185. res_bridge = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  186. ltq_pci_membase = devm_ioremap_resource(&pdev->dev, res_bridge);
  187. if (IS_ERR(ltq_pci_membase))
  188. return PTR_ERR(ltq_pci_membase);
  189. res_cfg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  190. ltq_pci_mapped_cfg = devm_ioremap_resource(&pdev->dev, res_cfg);
  191. if (IS_ERR(ltq_pci_mapped_cfg))
  192. return PTR_ERR(ltq_pci_mapped_cfg);
  193. ltq_pci_startup(pdev);
  194. pci_load_of_ranges(&pci_controller, pdev->dev.of_node);
  195. register_pci_controller(&pci_controller);
  196. return 0;
  197. }
  198. static const struct of_device_id ltq_pci_match[] = {
  199. { .compatible = "lantiq,pci-xway" },
  200. {},
  201. };
  202. MODULE_DEVICE_TABLE(of, ltq_pci_match);
  203. static struct platform_driver ltq_pci_driver = {
  204. .probe = ltq_pci_probe,
  205. .driver = {
  206. .name = "pci-xway",
  207. .of_match_table = ltq_pci_match,
  208. },
  209. };
  210. int __init pcibios_init(void)
  211. {
  212. int ret = platform_driver_register(&ltq_pci_driver);
  213. if (ret)
  214. pr_info("pci-xway: Error registering platform driver!");
  215. return ret;
  216. }
  217. arch_initcall(pcibios_init);