pci.c 8.3 KB

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