PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/mips/pci/pci-lantiq.c

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