pci.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * linux/arch/arm/mach-versatile/pci.c
  3. *
  4. * (C) Copyright Koninklijke Philips Electronics NV 2004. All rights reserved.
  5. * You can redistribute and/or modify this software under the terms of version 2
  6. * of the GNU General Public License as published by the Free Software Foundation.
  7. * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
  8. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. * General Public License for more details.
  10. * Koninklijke Philips Electronics nor its subsidiaries is obligated to provide any support for this software.
  11. *
  12. * ARM Versatile PCI driver.
  13. *
  14. * 14/04/2005 Initial version, colin.king@philips.com
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/pci.h>
  19. #include <linux/slab.h>
  20. #include <linux/ioport.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/init.h>
  24. #include <linux/io.h>
  25. #include <mach/hardware.h>
  26. #include <asm/irq.h>
  27. #include <asm/system.h>
  28. #include <asm/mach/pci.h>
  29. /*
  30. * these spaces are mapped using the following base registers:
  31. *
  32. * Usage Local Bus Memory Base/Map registers used
  33. *
  34. * Mem 50000000 - 5FFFFFFF LB_BASE0/LB_MAP0, non prefetch
  35. * Mem 60000000 - 6FFFFFFF LB_BASE1/LB_MAP1, prefetch
  36. * IO 44000000 - 4FFFFFFF LB_BASE2/LB_MAP2, IO
  37. * Cfg 42000000 - 42FFFFFF PCI config
  38. *
  39. */
  40. #define __IO_ADDRESS(n) ((void __iomem *)(unsigned long)IO_ADDRESS(n))
  41. #define SYS_PCICTL __IO_ADDRESS(VERSATILE_SYS_PCICTL)
  42. #define PCI_IMAP0 __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x0)
  43. #define PCI_IMAP1 __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x4)
  44. #define PCI_IMAP2 __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x8)
  45. #define PCI_SMAP0 __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x10)
  46. #define PCI_SMAP1 __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x14)
  47. #define PCI_SMAP2 __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x18)
  48. #define PCI_SELFID __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0xc)
  49. #define DEVICE_ID_OFFSET 0x00
  50. #define CSR_OFFSET 0x04
  51. #define CLASS_ID_OFFSET 0x08
  52. #define VP_PCI_DEVICE_ID 0x030010ee
  53. #define VP_PCI_CLASS_ID 0x0b400000
  54. static unsigned long pci_slot_ignore = 0;
  55. static int __init versatile_pci_slot_ignore(char *str)
  56. {
  57. int retval;
  58. int slot;
  59. while ((retval = get_option(&str,&slot))) {
  60. if ((slot < 0) || (slot > 31)) {
  61. printk("Illegal slot value: %d\n",slot);
  62. } else {
  63. pci_slot_ignore |= (1 << slot);
  64. }
  65. }
  66. return 1;
  67. }
  68. __setup("pci_slot_ignore=", versatile_pci_slot_ignore);
  69. static void __iomem *__pci_addr(struct pci_bus *bus,
  70. unsigned int devfn, int offset)
  71. {
  72. unsigned int busnr = bus->number;
  73. /*
  74. * Trap out illegal values
  75. */
  76. if (offset > 255)
  77. BUG();
  78. if (busnr > 255)
  79. BUG();
  80. if (devfn > 255)
  81. BUG();
  82. return VERSATILE_PCI_CFG_VIRT_BASE + ((busnr << 16) |
  83. (PCI_SLOT(devfn) << 11) | (PCI_FUNC(devfn) << 8) | offset);
  84. }
  85. static int versatile_read_config(struct pci_bus *bus, unsigned int devfn, int where,
  86. int size, u32 *val)
  87. {
  88. void __iomem *addr = __pci_addr(bus, devfn, where & ~3);
  89. u32 v;
  90. int slot = PCI_SLOT(devfn);
  91. if (pci_slot_ignore & (1 << slot)) {
  92. /* Ignore this slot */
  93. switch (size) {
  94. case 1:
  95. v = 0xff;
  96. break;
  97. case 2:
  98. v = 0xffff;
  99. break;
  100. default:
  101. v = 0xffffffff;
  102. }
  103. } else {
  104. switch (size) {
  105. case 1:
  106. v = __raw_readl(addr);
  107. if (where & 2) v >>= 16;
  108. if (where & 1) v >>= 8;
  109. v &= 0xff;
  110. break;
  111. case 2:
  112. v = __raw_readl(addr);
  113. if (where & 2) v >>= 16;
  114. v &= 0xffff;
  115. break;
  116. default:
  117. v = __raw_readl(addr);
  118. break;
  119. }
  120. }
  121. *val = v;
  122. return PCIBIOS_SUCCESSFUL;
  123. }
  124. static int versatile_write_config(struct pci_bus *bus, unsigned int devfn, int where,
  125. int size, u32 val)
  126. {
  127. void __iomem *addr = __pci_addr(bus, devfn, where);
  128. int slot = PCI_SLOT(devfn);
  129. if (pci_slot_ignore & (1 << slot)) {
  130. return PCIBIOS_SUCCESSFUL;
  131. }
  132. switch (size) {
  133. case 1:
  134. __raw_writeb((u8)val, addr);
  135. break;
  136. case 2:
  137. __raw_writew((u16)val, addr);
  138. break;
  139. case 4:
  140. __raw_writel(val, addr);
  141. break;
  142. }
  143. return PCIBIOS_SUCCESSFUL;
  144. }
  145. static struct pci_ops pci_versatile_ops = {
  146. .read = versatile_read_config,
  147. .write = versatile_write_config,
  148. };
  149. static struct resource io_mem = {
  150. .name = "PCI I/O space",
  151. .start = VERSATILE_PCI_MEM_BASE0,
  152. .end = VERSATILE_PCI_MEM_BASE0+VERSATILE_PCI_MEM_BASE0_SIZE-1,
  153. .flags = IORESOURCE_IO,
  154. };
  155. static struct resource non_mem = {
  156. .name = "PCI non-prefetchable",
  157. .start = VERSATILE_PCI_MEM_BASE1,
  158. .end = VERSATILE_PCI_MEM_BASE1+VERSATILE_PCI_MEM_BASE1_SIZE-1,
  159. .flags = IORESOURCE_MEM,
  160. };
  161. static struct resource pre_mem = {
  162. .name = "PCI prefetchable",
  163. .start = VERSATILE_PCI_MEM_BASE2,
  164. .end = VERSATILE_PCI_MEM_BASE2+VERSATILE_PCI_MEM_BASE2_SIZE-1,
  165. .flags = IORESOURCE_MEM | IORESOURCE_PREFETCH,
  166. };
  167. static int __init pci_versatile_setup_resources(struct resource **resource)
  168. {
  169. int ret = 0;
  170. ret = request_resource(&iomem_resource, &io_mem);
  171. if (ret) {
  172. printk(KERN_ERR "PCI: unable to allocate I/O "
  173. "memory region (%d)\n", ret);
  174. goto out;
  175. }
  176. ret = request_resource(&iomem_resource, &non_mem);
  177. if (ret) {
  178. printk(KERN_ERR "PCI: unable to allocate non-prefetchable "
  179. "memory region (%d)\n", ret);
  180. goto release_io_mem;
  181. }
  182. ret = request_resource(&iomem_resource, &pre_mem);
  183. if (ret) {
  184. printk(KERN_ERR "PCI: unable to allocate prefetchable "
  185. "memory region (%d)\n", ret);
  186. goto release_non_mem;
  187. }
  188. /*
  189. * bus->resource[0] is the IO resource for this bus
  190. * bus->resource[1] is the mem resource for this bus
  191. * bus->resource[2] is the prefetch mem resource for this bus
  192. */
  193. resource[0] = &io_mem;
  194. resource[1] = &non_mem;
  195. resource[2] = &pre_mem;
  196. goto out;
  197. release_non_mem:
  198. release_resource(&non_mem);
  199. release_io_mem:
  200. release_resource(&io_mem);
  201. out:
  202. return ret;
  203. }
  204. int __init pci_versatile_setup(int nr, struct pci_sys_data *sys)
  205. {
  206. int ret = 0;
  207. int i;
  208. int myslot = -1;
  209. unsigned long val;
  210. void __iomem *local_pci_cfg_base;
  211. val = __raw_readl(SYS_PCICTL);
  212. if (!(val & 1)) {
  213. printk("Not plugged into PCI backplane!\n");
  214. ret = -EIO;
  215. goto out;
  216. }
  217. if (nr == 0) {
  218. sys->mem_offset = 0;
  219. ret = pci_versatile_setup_resources(sys->resource);
  220. if (ret < 0) {
  221. printk("pci_versatile_setup: resources... oops?\n");
  222. goto out;
  223. }
  224. } else {
  225. printk("pci_versatile_setup: resources... nr == 0??\n");
  226. goto out;
  227. }
  228. /*
  229. * We need to discover the PCI core first to configure itself
  230. * before the main PCI probing is performed
  231. */
  232. for (i=0; i<32; i++)
  233. if ((__raw_readl(VERSATILE_PCI_VIRT_BASE+(i<<11)+DEVICE_ID_OFFSET) == VP_PCI_DEVICE_ID) &&
  234. (__raw_readl(VERSATILE_PCI_VIRT_BASE+(i<<11)+CLASS_ID_OFFSET) == VP_PCI_CLASS_ID)) {
  235. myslot = i;
  236. break;
  237. }
  238. if (myslot == -1) {
  239. printk("Cannot find PCI core!\n");
  240. ret = -EIO;
  241. goto out;
  242. }
  243. printk("PCI core found (slot %d)\n",myslot);
  244. __raw_writel(myslot, PCI_SELFID);
  245. local_pci_cfg_base = VERSATILE_PCI_CFG_VIRT_BASE + (myslot << 11);
  246. val = __raw_readl(local_pci_cfg_base + CSR_OFFSET);
  247. val |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE;
  248. __raw_writel(val, local_pci_cfg_base + CSR_OFFSET);
  249. /*
  250. * Configure the PCI inbound memory windows to be 1:1 mapped to SDRAM
  251. */
  252. __raw_writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_0);
  253. __raw_writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_1);
  254. __raw_writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_2);
  255. /*
  256. * Do not to map Versatile FPGA PCI device into memory space
  257. */
  258. pci_slot_ignore |= (1 << myslot);
  259. ret = 1;
  260. out:
  261. return ret;
  262. }
  263. struct pci_bus *pci_versatile_scan_bus(int nr, struct pci_sys_data *sys)
  264. {
  265. return pci_scan_bus(sys->busnr, &pci_versatile_ops, sys);
  266. }
  267. void __init pci_versatile_preinit(void)
  268. {
  269. __raw_writel(VERSATILE_PCI_MEM_BASE0 >> 28, PCI_IMAP0);
  270. __raw_writel(VERSATILE_PCI_MEM_BASE1 >> 28, PCI_IMAP1);
  271. __raw_writel(VERSATILE_PCI_MEM_BASE2 >> 28, PCI_IMAP2);
  272. __raw_writel(PHYS_OFFSET >> 28, PCI_SMAP0);
  273. __raw_writel(PHYS_OFFSET >> 28, PCI_SMAP1);
  274. __raw_writel(PHYS_OFFSET >> 28, PCI_SMAP2);
  275. __raw_writel(1, SYS_PCICTL);
  276. }
  277. /*
  278. * map the specified device/slot/pin to an IRQ. Different backplanes may need to modify this.
  279. */
  280. static int __init versatile_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
  281. {
  282. int irq;
  283. int devslot = PCI_SLOT(dev->devfn);
  284. /* slot, pin, irq
  285. * 24 1 27
  286. * 25 1 28
  287. * 26 1 29
  288. * 27 1 30
  289. */
  290. irq = 27 + ((slot + pin - 1) & 3);
  291. printk("PCI map irq: slot %d, pin %d, devslot %d, irq: %d\n",slot,pin,devslot,irq);
  292. return irq;
  293. }
  294. static struct hw_pci versatile_pci __initdata = {
  295. .swizzle = NULL,
  296. .map_irq = versatile_map_irq,
  297. .nr_controllers = 1,
  298. .setup = pci_versatile_setup,
  299. .scan = pci_versatile_scan_bus,
  300. .preinit = pci_versatile_preinit,
  301. };
  302. static int __init versatile_pci_init(void)
  303. {
  304. pci_common_init(&versatile_pci);
  305. return 0;
  306. }
  307. subsys_initcall(versatile_pci_init);