pci-lantiq.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/export.h>
  16. #include <linux/platform_device.h>
  17. #include <asm/pci.h>
  18. #include <asm/gpio.h>
  19. #include <asm/addrspace.h>
  20. #include <lantiq_soc.h>
  21. #include <lantiq_irq.h>
  22. #include <lantiq_platform.h>
  23. #include "pci-lantiq.h"
  24. #define LTQ_PCI_CFG_BASE 0x17000000
  25. #define LTQ_PCI_CFG_SIZE 0x00008000
  26. #define LTQ_PCI_MEM_BASE 0x18000000
  27. #define LTQ_PCI_MEM_SIZE 0x02000000
  28. #define LTQ_PCI_IO_BASE 0x1AE00000
  29. #define LTQ_PCI_IO_SIZE 0x00200000
  30. #define PCI_CR_FCI_ADDR_MAP0 0x00C0
  31. #define PCI_CR_FCI_ADDR_MAP1 0x00C4
  32. #define PCI_CR_FCI_ADDR_MAP2 0x00C8
  33. #define PCI_CR_FCI_ADDR_MAP3 0x00CC
  34. #define PCI_CR_FCI_ADDR_MAP4 0x00D0
  35. #define PCI_CR_FCI_ADDR_MAP5 0x00D4
  36. #define PCI_CR_FCI_ADDR_MAP6 0x00D8
  37. #define PCI_CR_FCI_ADDR_MAP7 0x00DC
  38. #define PCI_CR_CLK_CTRL 0x0000
  39. #define PCI_CR_PCI_MOD 0x0030
  40. #define PCI_CR_PC_ARB 0x0080
  41. #define PCI_CR_FCI_ADDR_MAP11hg 0x00E4
  42. #define PCI_CR_BAR11MASK 0x0044
  43. #define PCI_CR_BAR12MASK 0x0048
  44. #define PCI_CR_BAR13MASK 0x004C
  45. #define PCI_CS_BASE_ADDR1 0x0010
  46. #define PCI_CR_PCI_ADDR_MAP11 0x0064
  47. #define PCI_CR_FCI_BURST_LENGTH 0x00E8
  48. #define PCI_CR_PCI_EOI 0x002C
  49. #define PCI_CS_STS_CMD 0x0004
  50. #define PCI_MASTER0_REQ_MASK_2BITS 8
  51. #define PCI_MASTER1_REQ_MASK_2BITS 10
  52. #define PCI_MASTER2_REQ_MASK_2BITS 12
  53. #define INTERNAL_ARB_ENABLE_BIT 0
  54. #define LTQ_CGU_IFCCR 0x0018
  55. #define LTQ_CGU_PCICR 0x0034
  56. #define ltq_pci_w32(x, y) ltq_w32((x), ltq_pci_membase + (y))
  57. #define ltq_pci_r32(x) ltq_r32(ltq_pci_membase + (x))
  58. #define ltq_pci_cfg_w32(x, y) ltq_w32((x), ltq_pci_mapped_cfg + (y))
  59. #define ltq_pci_cfg_r32(x) ltq_r32(ltq_pci_mapped_cfg + (x))
  60. __iomem void *ltq_pci_mapped_cfg;
  61. static __iomem void *ltq_pci_membase;
  62. int (*ltqpci_plat_dev_init)(struct pci_dev *dev) = NULL;
  63. /* Since the PCI REQ pins can be reused for other functionality, make it
  64. possible to exclude those from interpretation by the PCI controller */
  65. static int ltq_pci_req_mask = 0xf;
  66. static int *ltq_pci_irq_map;
  67. struct pci_ops ltq_pci_ops = {
  68. .read = ltq_pci_read_config_dword,
  69. .write = ltq_pci_write_config_dword
  70. };
  71. static struct resource pci_io_resource = {
  72. .name = "pci io space",
  73. .start = LTQ_PCI_IO_BASE,
  74. .end = LTQ_PCI_IO_BASE + LTQ_PCI_IO_SIZE - 1,
  75. .flags = IORESOURCE_IO
  76. };
  77. static struct resource pci_mem_resource = {
  78. .name = "pci memory space",
  79. .start = LTQ_PCI_MEM_BASE,
  80. .end = LTQ_PCI_MEM_BASE + LTQ_PCI_MEM_SIZE - 1,
  81. .flags = IORESOURCE_MEM
  82. };
  83. static struct pci_controller ltq_pci_controller = {
  84. .pci_ops = &ltq_pci_ops,
  85. .mem_resource = &pci_mem_resource,
  86. .mem_offset = 0x00000000UL,
  87. .io_resource = &pci_io_resource,
  88. .io_offset = 0x00000000UL,
  89. };
  90. int pcibios_plat_dev_init(struct pci_dev *dev)
  91. {
  92. if (ltqpci_plat_dev_init)
  93. return ltqpci_plat_dev_init(dev);
  94. return 0;
  95. }
  96. static u32 ltq_calc_bar11mask(void)
  97. {
  98. u32 mem, bar11mask;
  99. /* BAR11MASK value depends on available memory on system. */
  100. mem = num_physpages * PAGE_SIZE;
  101. bar11mask = (0x0ffffff0 & ~((1 << (fls(mem) - 1)) - 1)) | 8;
  102. return bar11mask;
  103. }
  104. static int __devinit ltq_pci_startup(struct ltq_pci_data *conf)
  105. {
  106. u32 temp_buffer;
  107. /* set clock to 33Mhz */
  108. if (ltq_is_ar9()) {
  109. ltq_cgu_w32(ltq_cgu_r32(LTQ_CGU_IFCCR) & ~0x1f00000, LTQ_CGU_IFCCR);
  110. ltq_cgu_w32(ltq_cgu_r32(LTQ_CGU_IFCCR) | 0xe00000, LTQ_CGU_IFCCR);
  111. } else {
  112. ltq_cgu_w32(ltq_cgu_r32(LTQ_CGU_IFCCR) & ~0xf00000, LTQ_CGU_IFCCR);
  113. ltq_cgu_w32(ltq_cgu_r32(LTQ_CGU_IFCCR) | 0x800000, LTQ_CGU_IFCCR);
  114. }
  115. /* external or internal clock ? */
  116. if (conf->clock) {
  117. ltq_cgu_w32(ltq_cgu_r32(LTQ_CGU_IFCCR) & ~(1 << 16),
  118. LTQ_CGU_IFCCR);
  119. ltq_cgu_w32((1 << 30), LTQ_CGU_PCICR);
  120. } else {
  121. ltq_cgu_w32(ltq_cgu_r32(LTQ_CGU_IFCCR) | (1 << 16),
  122. LTQ_CGU_IFCCR);
  123. ltq_cgu_w32((1 << 31) | (1 << 30), LTQ_CGU_PCICR);
  124. }
  125. /* setup pci clock and gpis used by pci */
  126. gpio_request(21, "pci-reset");
  127. /* enable auto-switching between PCI and EBU */
  128. ltq_pci_w32(0xa, PCI_CR_CLK_CTRL);
  129. /* busy, i.e. configuration is not done, PCI access has to be retried */
  130. ltq_pci_w32(ltq_pci_r32(PCI_CR_PCI_MOD) & ~(1 << 24), PCI_CR_PCI_MOD);
  131. wmb();
  132. /* BUS Master/IO/MEM access */
  133. ltq_pci_cfg_w32(ltq_pci_cfg_r32(PCI_CS_STS_CMD) | 7, PCI_CS_STS_CMD);
  134. /* enable external 2 PCI masters */
  135. temp_buffer = ltq_pci_r32(PCI_CR_PC_ARB);
  136. temp_buffer &= (~(ltq_pci_req_mask << 16));
  137. /* enable internal arbiter */
  138. temp_buffer |= (1 << INTERNAL_ARB_ENABLE_BIT);
  139. /* enable internal PCI master reqest */
  140. temp_buffer &= (~(3 << PCI_MASTER0_REQ_MASK_2BITS));
  141. /* enable EBU request */
  142. temp_buffer &= (~(3 << PCI_MASTER1_REQ_MASK_2BITS));
  143. /* enable all external masters request */
  144. temp_buffer &= (~(3 << PCI_MASTER2_REQ_MASK_2BITS));
  145. ltq_pci_w32(temp_buffer, PCI_CR_PC_ARB);
  146. wmb();
  147. /* setup BAR memory regions */
  148. ltq_pci_w32(0x18000000, PCI_CR_FCI_ADDR_MAP0);
  149. ltq_pci_w32(0x18400000, PCI_CR_FCI_ADDR_MAP1);
  150. ltq_pci_w32(0x18800000, PCI_CR_FCI_ADDR_MAP2);
  151. ltq_pci_w32(0x18c00000, PCI_CR_FCI_ADDR_MAP3);
  152. ltq_pci_w32(0x19000000, PCI_CR_FCI_ADDR_MAP4);
  153. ltq_pci_w32(0x19400000, PCI_CR_FCI_ADDR_MAP5);
  154. ltq_pci_w32(0x19800000, PCI_CR_FCI_ADDR_MAP6);
  155. ltq_pci_w32(0x19c00000, PCI_CR_FCI_ADDR_MAP7);
  156. ltq_pci_w32(0x1ae00000, PCI_CR_FCI_ADDR_MAP11hg);
  157. ltq_pci_w32(ltq_calc_bar11mask(), PCI_CR_BAR11MASK);
  158. ltq_pci_w32(0, PCI_CR_PCI_ADDR_MAP11);
  159. ltq_pci_w32(0, PCI_CS_BASE_ADDR1);
  160. /* both TX and RX endian swap are enabled */
  161. ltq_pci_w32(ltq_pci_r32(PCI_CR_PCI_EOI) | 3, PCI_CR_PCI_EOI);
  162. wmb();
  163. ltq_pci_w32(ltq_pci_r32(PCI_CR_BAR12MASK) | 0x80000000,
  164. PCI_CR_BAR12MASK);
  165. ltq_pci_w32(ltq_pci_r32(PCI_CR_BAR13MASK) | 0x80000000,
  166. PCI_CR_BAR13MASK);
  167. /*use 8 dw burst length */
  168. ltq_pci_w32(0x303, PCI_CR_FCI_BURST_LENGTH);
  169. ltq_pci_w32(ltq_pci_r32(PCI_CR_PCI_MOD) | (1 << 24), PCI_CR_PCI_MOD);
  170. wmb();
  171. /* setup irq line */
  172. ltq_ebu_w32(ltq_ebu_r32(LTQ_EBU_PCC_CON) | 0xc, LTQ_EBU_PCC_CON);
  173. ltq_ebu_w32(ltq_ebu_r32(LTQ_EBU_PCC_IEN) | 0x10, LTQ_EBU_PCC_IEN);
  174. /* toggle reset pin */
  175. __gpio_set_value(21, 0);
  176. wmb();
  177. mdelay(1);
  178. __gpio_set_value(21, 1);
  179. return 0;
  180. }
  181. int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  182. {
  183. if (ltq_pci_irq_map[slot])
  184. return ltq_pci_irq_map[slot];
  185. printk(KERN_ERR "lq_pci: trying to map irq for unknown slot %d\n",
  186. slot);
  187. return 0;
  188. }
  189. static int __devinit ltq_pci_probe(struct platform_device *pdev)
  190. {
  191. struct ltq_pci_data *ltq_pci_data =
  192. (struct ltq_pci_data *) pdev->dev.platform_data;
  193. pci_clear_flags(PCI_PROBE_ONLY);
  194. ltq_pci_irq_map = ltq_pci_data->irq;
  195. ltq_pci_membase = ioremap_nocache(PCI_CR_BASE_ADDR, PCI_CR_SIZE);
  196. ltq_pci_mapped_cfg =
  197. ioremap_nocache(LTQ_PCI_CFG_BASE, LTQ_PCI_CFG_BASE);
  198. ltq_pci_controller.io_map_base =
  199. (unsigned long)ioremap(LTQ_PCI_IO_BASE, LTQ_PCI_IO_SIZE - 1);
  200. ltq_pci_startup(ltq_pci_data);
  201. register_pci_controller(&ltq_pci_controller);
  202. return 0;
  203. }
  204. static struct platform_driver
  205. ltq_pci_driver = {
  206. .probe = ltq_pci_probe,
  207. .driver = {
  208. .name = "ltq_pci",
  209. .owner = THIS_MODULE,
  210. },
  211. };
  212. int __init pcibios_init(void)
  213. {
  214. int ret = platform_driver_register(&ltq_pci_driver);
  215. if (ret)
  216. printk(KERN_INFO "ltq_pci: Error registering platfom driver!");
  217. return ret;
  218. }
  219. arch_initcall(pcibios_init);