pci.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Support for Gemini PCI Controller
  3. *
  4. * Copyright (C) 2009 Janos Laube <janos.dev@gmail.com>
  5. * Copyright (C) 2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  6. *
  7. * based on SL2312 PCI controller code
  8. * Storlink (C) 2003
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/pci.h>
  17. #include <linux/irq.h>
  18. #include <linux/gpio.h>
  19. #include <asm/mach/pci.h>
  20. #include <mach/irqs.h>
  21. #include <mach/hardware.h>
  22. #define GEMINI_PCI_IOSIZE_1M 0x0000
  23. #define GEMINI_PCI_PMC 0x40
  24. #define GEMINI_PCI_PMCSR 0x44
  25. #define GEMINI_PCI_CTRL1 0x48
  26. #define GEMINI_PCI_CTRL2 0x4C
  27. #define GEMINI_PCI_MEM1_BASE_SIZE 0x50
  28. #define GEMINI_PCI_MEM2_BASE_SIZE 0x54
  29. #define GEMINI_PCI_MEM3_BASE_SIZE 0x58
  30. #define PCI_CTRL2_INTSTS_OFFSET 28
  31. #define PCI_CTRL2_INTMASK_OFFSET 22
  32. #define GEMINI_PCI_DMA_MASK 0xFFF00000
  33. #define GEMINI_PCI_DMA_MEM1_BASE 0x00000000
  34. #define GEMINI_PCI_DMA_MEM2_BASE 0x00000000
  35. #define GEMINI_PCI_DMA_MEM3_BASE 0x00000000
  36. #define GEMINI_PCI_DMA_MEM1_SIZE 7
  37. #define GEMINI_PCI_DMA_MEM2_SIZE 6
  38. #define GEMINI_PCI_DMA_MEM3_SIZE 6
  39. #define PCI_CONF_ENABLE (1 << 31)
  40. #define PCI_CONF_WHERE(r) ((r) & 0xFC)
  41. #define PCI_CONF_BUS(b) (((b) & 0xFF) << 16)
  42. #define PCI_CONF_DEVICE(d) (((d) & 0x1F) << 11)
  43. #define PCI_CONF_FUNCTION(f) (((f) & 0x07) << 8)
  44. #define PCI_IOSIZE_REG (IO_ADDRESS(GEMINI_PCI_IO_BASE))
  45. #define PCI_PROT_REG (IO_ADDRESS(GEMINI_PCI_IO_BASE) + 0x04)
  46. #define PCI_CTRL_REG (IO_ADDRESS(GEMINI_PCI_IO_BASE) + 0x08)
  47. #define PCI_SOFTRST_REG (IO_ADDRESS(GEMINI_PCI_IO_BASE) + 0x10)
  48. #define PCI_CONFIG_REG (IO_ADDRESS(GEMINI_PCI_IO_BASE) + 0x28)
  49. #define PCI_DATA_REG (IO_ADDRESS(GEMINI_PCI_IO_BASE) + 0x2C)
  50. static DEFINE_SPINLOCK(gemini_pci_lock);
  51. static int gemini_pci_read_config(struct pci_bus* bus, unsigned int fn,
  52. int config, int size, u32* value)
  53. {
  54. unsigned long irq_flags;
  55. spin_lock_irqsave(&gemini_pci_lock, irq_flags);
  56. __raw_writel(PCI_CONF_BUS(bus->number) |
  57. PCI_CONF_DEVICE(PCI_SLOT(fn)) |
  58. PCI_CONF_FUNCTION(PCI_FUNC(fn)) |
  59. PCI_CONF_WHERE(config) |
  60. PCI_CONF_ENABLE,
  61. PCI_CONFIG_REG);
  62. *value = __raw_readl(PCI_DATA_REG);
  63. if (size == 1)
  64. *value = (*value >> (8 * (config & 3))) & 0xFF;
  65. else if (size == 2)
  66. *value = (*value >> (8 * (config & 3))) & 0xFFFF;
  67. spin_unlock_irqrestore(&gemini_pci_lock, irq_flags);
  68. dev_dbg(&bus->dev,
  69. "[read] slt: %.2d, fnc: %d, cnf: 0x%.2X, val (%d bytes): 0x%.8X\n",
  70. PCI_SLOT(fn), PCI_FUNC(fn), config, size, *value);
  71. return PCIBIOS_SUCCESSFUL;
  72. }
  73. static int gemini_pci_write_config(struct pci_bus* bus, unsigned int fn,
  74. int config, int size, u32 value)
  75. {
  76. unsigned long irq_flags = 0;
  77. int ret = PCIBIOS_SUCCESSFUL;
  78. dev_dbg(&bus->dev,
  79. "[write] slt: %.2d, fnc: %d, cnf: 0x%.2X, val (%d bytes): 0x%.8X\n",
  80. PCI_SLOT(fn), PCI_FUNC(fn), config, size, value);
  81. spin_lock_irqsave(&gemini_pci_lock, irq_flags);
  82. __raw_writel(PCI_CONF_BUS(bus->number) |
  83. PCI_CONF_DEVICE(PCI_SLOT(fn)) |
  84. PCI_CONF_FUNCTION(PCI_FUNC(fn)) |
  85. PCI_CONF_WHERE(config) |
  86. PCI_CONF_ENABLE,
  87. PCI_CONFIG_REG);
  88. switch(size) {
  89. case 4:
  90. __raw_writel(value, PCI_DATA_REG);
  91. break;
  92. case 2:
  93. __raw_writew(value, PCI_DATA_REG + (config & 3));
  94. break;
  95. case 1:
  96. __raw_writeb(value, PCI_DATA_REG + (config & 3));
  97. break;
  98. default:
  99. ret = PCIBIOS_BAD_REGISTER_NUMBER;
  100. }
  101. spin_unlock_irqrestore(&gemini_pci_lock, irq_flags);
  102. return ret;
  103. }
  104. static struct pci_ops gemini_pci_ops = {
  105. .read = gemini_pci_read_config,
  106. .write = gemini_pci_write_config,
  107. };
  108. static struct resource gemini_pci_resource_io = {
  109. .name = "PCI I/O Space",
  110. .start = GEMINI_PCI_IO_BASE,
  111. .end = GEMINI_PCI_IO_BASE + SZ_1M - 1,
  112. .flags = IORESOURCE_IO,
  113. };
  114. static struct resource gemini_pci_resource_mem = {
  115. .name = "PCI Memory Space",
  116. .start = GEMINI_PCI_MEM_BASE,
  117. .end = GEMINI_PCI_MEM_BASE + SZ_128M - 1,
  118. .flags = IORESOURCE_MEM,
  119. };
  120. static int __init gemini_pci_request_resources(struct pci_sys_data *sys)
  121. {
  122. if (request_resource(&ioport_resource, &gemini_pci_resource_io))
  123. goto bad_resources;
  124. if (request_resource(&iomem_resource, &gemini_pci_resource_mem))
  125. goto bad_resources;
  126. pci_add_resource(&sys->resources, &gemini_pci_resource_io);
  127. pci_add_resource(&sys->resources, &gemini_pci_resource_mem);
  128. return 0;
  129. bad_resources:
  130. pr_err("Gemini PCI: request_resource() failed. "
  131. "Abort PCI bus enumeration.\n");
  132. return -1;
  133. }
  134. static int __init gemini_pci_setup(int nr, struct pci_sys_data *sys)
  135. {
  136. unsigned int cmd;
  137. pcibios_min_io = 0x100;
  138. pcibios_min_mem = 0;
  139. if ((nr > 0) || gemini_pci_request_resources(sys))
  140. return 0;
  141. /* setup I/O space to 1MB size */
  142. __raw_writel(GEMINI_PCI_IOSIZE_1M, PCI_IOSIZE_REG);
  143. /* setup hostbridge */
  144. cmd = __raw_readl(PCI_CTRL_REG);
  145. cmd |= PCI_COMMAND_IO;
  146. cmd |= PCI_COMMAND_MEMORY;
  147. cmd |= PCI_COMMAND_MASTER;
  148. __raw_writel(cmd, PCI_CTRL_REG);
  149. return 1;
  150. }
  151. static struct pci_bus* __init gemini_pci_scan_bus(int nr, struct pci_sys_data* sys)
  152. {
  153. unsigned int reg = 0;
  154. struct pci_bus* bus = 0;
  155. bus = pci_scan_bus(nr, &gemini_pci_ops, sys);
  156. if (bus) {
  157. dev_dbg(&bus->dev, "setting up PCI DMA\n");
  158. reg = (GEMINI_PCI_DMA_MEM1_BASE & GEMINI_PCI_DMA_MASK)
  159. | (GEMINI_PCI_DMA_MEM1_SIZE << 16);
  160. gemini_pci_write_config(bus, 0, GEMINI_PCI_MEM1_BASE_SIZE, 4, reg);
  161. reg = (GEMINI_PCI_DMA_MEM2_BASE & GEMINI_PCI_DMA_MASK)
  162. | (GEMINI_PCI_DMA_MEM2_SIZE << 16);
  163. gemini_pci_write_config(bus, 0, GEMINI_PCI_MEM2_BASE_SIZE, 4, reg);
  164. reg = (GEMINI_PCI_DMA_MEM3_BASE & GEMINI_PCI_DMA_MASK)
  165. | (GEMINI_PCI_DMA_MEM3_SIZE << 16);
  166. gemini_pci_write_config(bus, 0, GEMINI_PCI_MEM3_BASE_SIZE, 4, reg);
  167. }
  168. return bus;
  169. }
  170. /* Should work with all boards based on original Storlink EVB */
  171. static int __init gemini_pci_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  172. {
  173. if (slot < 9 || slot > 12)
  174. return -1;
  175. return PCI_IRQ_BASE + (((slot - 9) + (pin - 1)) & 0x3);
  176. }
  177. static struct hw_pci gemini_hw_pci __initdata = {
  178. .nr_controllers = 1,
  179. .setup = gemini_pci_setup,
  180. .scan = gemini_pci_scan_bus,
  181. .map_irq = gemini_pci_map_irq,
  182. };
  183. /* we need this for muxed PCI interrupts handling */
  184. static struct pci_bus bogus_pci_bus;
  185. static void gemini_pci_ack_irq(struct irq_data *d)
  186. {
  187. unsigned int irq = d->irq;
  188. unsigned int reg;
  189. gemini_pci_read_config(&bogus_pci_bus, 0, GEMINI_PCI_CTRL2, 4, &reg);
  190. reg &= ~(0xF << PCI_CTRL2_INTSTS_OFFSET);
  191. reg |= 1 << (irq - PCI_IRQ_BASE + PCI_CTRL2_INTSTS_OFFSET);
  192. gemini_pci_write_config(&bogus_pci_bus, 0, GEMINI_PCI_CTRL2, 4, reg);
  193. }
  194. static void gemini_pci_mask_irq(struct irq_data *d)
  195. {
  196. unsigned int irq = d->irq;
  197. unsigned int reg;
  198. gemini_pci_read_config(&bogus_pci_bus, 0, GEMINI_PCI_CTRL2, 4, &reg);
  199. reg &= ~((0xF << PCI_CTRL2_INTSTS_OFFSET)
  200. | (1 << (irq - PCI_IRQ_BASE + PCI_CTRL2_INTMASK_OFFSET)));
  201. gemini_pci_write_config(&bogus_pci_bus, 0, GEMINI_PCI_CTRL2, 4, reg);
  202. }
  203. static void gemini_pci_unmask_irq(struct irq_data *d)
  204. {
  205. unsigned int irq = d->irq;
  206. unsigned int reg;
  207. gemini_pci_read_config(&bogus_pci_bus, 0, GEMINI_PCI_CTRL2, 4, &reg);
  208. reg &= ~(0xF << PCI_CTRL2_INTSTS_OFFSET);
  209. reg |= 1 << (irq - PCI_IRQ_BASE + PCI_CTRL2_INTMASK_OFFSET);
  210. gemini_pci_write_config(&bogus_pci_bus, 0, GEMINI_PCI_CTRL2, 4, reg);
  211. }
  212. static void gemini_pci_irq_handler(struct irq_desc *desc)
  213. {
  214. unsigned int pci_irq_no, irq_stat, reg, i;
  215. gemini_pci_read_config(&bogus_pci_bus, 0, GEMINI_PCI_CTRL2, 4, &reg);
  216. irq_stat = reg >> PCI_CTRL2_INTSTS_OFFSET;
  217. for (i = 0; i < 4; i++) {
  218. if ((irq_stat & (1 << i)) == 0)
  219. continue;
  220. pci_irq_no = PCI_IRQ_BASE + i;
  221. BUG_ON(!(irq_desc[pci_irq_no].handle_irq));
  222. irq_desc[pci_irq_no].handle_irq(&irq_desc[pci_irq_no]);
  223. }
  224. }
  225. static struct irq_chip gemini_pci_irq_chip = {
  226. .name = "PCI",
  227. .irq_ack = gemini_pci_ack_irq,
  228. .irq_mask = gemini_pci_mask_irq,
  229. .irq_unmask = gemini_pci_unmask_irq,
  230. };
  231. static int __init gemini_pci_init(void)
  232. {
  233. int i;
  234. for (i = 72; i <= 95; i++)
  235. gpio_request(i, "PCI");
  236. /* initialize our bogus bus */
  237. dev_set_name(&bogus_pci_bus.dev, "PCI IRQ handler");
  238. bogus_pci_bus.number = 0;
  239. /* mask and clear all interrupts */
  240. gemini_pci_write_config(&bogus_pci_bus, 0, GEMINI_PCI_CTRL2 + 2, 2,
  241. 0xF000);
  242. for (i = PCI_IRQ_BASE; i < PCI_IRQ_BASE + 4; i++) {
  243. irq_set_chip_and_handler(i, &gemini_pci_irq_chip,
  244. handle_level_irq);
  245. }
  246. irq_set_chained_handler(IRQ_PCI, gemini_pci_irq_handler);
  247. pci_common_init(&gemini_hw_pci);
  248. return 0;
  249. }
  250. subsys_initcall(gemini_pci_init);