pci-lantiq.c 8.2 KB

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