pci.c 8.8 KB

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