pci.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * pci.c -- PCI bus support for ColdFire processors
  3. *
  4. * (C) Copyright 2012, Greg Ungerer <gerg@uclinux.com>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/io.h>
  17. #include <linux/pci.h>
  18. #include <linux/delay.h>
  19. #include <asm/coldfire.h>
  20. #include <asm/mcfsim.h>
  21. #include <asm/m54xxpci.h>
  22. /*
  23. * Memory and IO mappings. We use a 1:1 mapping for local host memory to
  24. * PCI bus memory (no reason not to really). IO space doesn't matter, we
  25. * always use access functions for that. The device configuration space is
  26. * mapped over the IO map space when we enable it in the PCICAR register.
  27. */
  28. #define PCI_MEM_PA 0xf0000000 /* Host physical address */
  29. #define PCI_MEM_BA 0xf0000000 /* Bus physical address */
  30. #define PCI_MEM_SIZE 0x08000000 /* 128 MB */
  31. #define PCI_MEM_MASK (PCI_MEM_SIZE - 1)
  32. #define PCI_IO_PA 0xf8000000 /* Host physical address */
  33. #define PCI_IO_BA 0x00000000 /* Bus physical address */
  34. #define PCI_IO_SIZE 0x00010000 /* 64k */
  35. #define PCI_IO_MASK (PCI_IO_SIZE - 1)
  36. static struct pci_bus *rootbus;
  37. static unsigned long iospace;
  38. /*
  39. * We need to be carefull probing on bus 0 (directly connected to host
  40. * bridge). We should only access the well defined possible devices in
  41. * use, ignore aliases and the like.
  42. */
  43. static unsigned char mcf_host_slot2sid[32] = {
  44. 0, 0, 0, 0, 0, 0, 0, 0,
  45. 0, 0, 0, 0, 0, 0, 0, 0,
  46. 0, 1, 2, 0, 3, 4, 0, 0,
  47. 0, 0, 0, 0, 0, 0, 0, 0,
  48. };
  49. static unsigned char mcf_host_irq[] = {
  50. 0, 69, 69, 71, 71,
  51. };
  52. static inline void syncio(void)
  53. {
  54. /* The ColdFire "nop" instruction waits for all bus IO to complete */
  55. __asm__ __volatile__ ("nop");
  56. }
  57. /*
  58. * Configuration space access functions. Configuration space access is
  59. * through the IO mapping window, enabling it via the PCICAR register.
  60. */
  61. static unsigned long mcf_mk_pcicar(int bus, unsigned int devfn, int where)
  62. {
  63. return (bus << PCICAR_BUSN) | (devfn << PCICAR_DEVFNN) | (where & 0xfc);
  64. }
  65. static int mcf_pci_readconfig(struct pci_bus *bus, unsigned int devfn,
  66. int where, int size, u32 *value)
  67. {
  68. unsigned long addr;
  69. *value = 0xffffffff;
  70. if (bus->number == 0) {
  71. if (mcf_host_slot2sid[PCI_SLOT(devfn)] == 0)
  72. return PCIBIOS_SUCCESSFUL;
  73. }
  74. syncio();
  75. addr = mcf_mk_pcicar(bus->number, devfn, where);
  76. __raw_writel(PCICAR_E | addr, PCICAR);
  77. addr = iospace + (where & 0x3);
  78. switch (size) {
  79. case 1:
  80. *value = __raw_readb(addr);
  81. break;
  82. case 2:
  83. *value = le16_to_cpu(__raw_readw(addr));
  84. break;
  85. default:
  86. *value = le32_to_cpu(__raw_readl(addr));
  87. break;
  88. }
  89. syncio();
  90. __raw_writel(0, PCICAR);
  91. return PCIBIOS_SUCCESSFUL;
  92. }
  93. static int mcf_pci_writeconfig(struct pci_bus *bus, unsigned int devfn,
  94. int where, int size, u32 value)
  95. {
  96. unsigned long addr;
  97. if (bus->number == 0) {
  98. if (mcf_host_slot2sid[PCI_SLOT(devfn)] == 0)
  99. return PCIBIOS_SUCCESSFUL;
  100. }
  101. syncio();
  102. addr = mcf_mk_pcicar(bus->number, devfn, where);
  103. __raw_writel(PCICAR_E | addr, PCICAR);
  104. addr = iospace + (where & 0x3);
  105. switch (size) {
  106. case 1:
  107. __raw_writeb(value, addr);
  108. break;
  109. case 2:
  110. __raw_writew(cpu_to_le16(value), addr);
  111. break;
  112. default:
  113. __raw_writel(cpu_to_le32(value), addr);
  114. break;
  115. }
  116. syncio();
  117. __raw_writel(0, PCICAR);
  118. return PCIBIOS_SUCCESSFUL;
  119. }
  120. static struct pci_ops mcf_pci_ops = {
  121. .read = mcf_pci_readconfig,
  122. .write = mcf_pci_writeconfig,
  123. };
  124. /*
  125. * IO address space access functions. Pretty strait forward, these are
  126. * directly mapped in to the IO mapping window. And that is mapped into
  127. * virtual address space.
  128. */
  129. u8 mcf_pci_inb(u32 addr)
  130. {
  131. return __raw_readb(iospace + (addr & PCI_IO_MASK));
  132. }
  133. EXPORT_SYMBOL(mcf_pci_inb);
  134. u16 mcf_pci_inw(u32 addr)
  135. {
  136. return le16_to_cpu(__raw_readw(iospace + (addr & PCI_IO_MASK)));
  137. }
  138. EXPORT_SYMBOL(mcf_pci_inw);
  139. u32 mcf_pci_inl(u32 addr)
  140. {
  141. return le32_to_cpu(__raw_readl(iospace + (addr & PCI_IO_MASK)));
  142. }
  143. EXPORT_SYMBOL(mcf_pci_inl);
  144. void mcf_pci_insb(u32 addr, u8 *buf, u32 len)
  145. {
  146. for (; len; len--)
  147. *buf++ = mcf_pci_inb(addr);
  148. }
  149. EXPORT_SYMBOL(mcf_pci_insb);
  150. void mcf_pci_insw(u32 addr, u16 *buf, u32 len)
  151. {
  152. for (; len; len--)
  153. *buf++ = mcf_pci_inw(addr);
  154. }
  155. EXPORT_SYMBOL(mcf_pci_insw);
  156. void mcf_pci_insl(u32 addr, u32 *buf, u32 len)
  157. {
  158. for (; len; len--)
  159. *buf++ = mcf_pci_inl(addr);
  160. }
  161. EXPORT_SYMBOL(mcf_pci_insl);
  162. void mcf_pci_outb(u8 v, u32 addr)
  163. {
  164. __raw_writeb(v, iospace + (addr & PCI_IO_MASK));
  165. }
  166. EXPORT_SYMBOL(mcf_pci_outb);
  167. void mcf_pci_outw(u16 v, u32 addr)
  168. {
  169. __raw_writew(cpu_to_le16(v), iospace + (addr & PCI_IO_MASK));
  170. }
  171. EXPORT_SYMBOL(mcf_pci_outw);
  172. void mcf_pci_outl(u32 v, u32 addr)
  173. {
  174. __raw_writel(cpu_to_le32(v), iospace + (addr & PCI_IO_MASK));
  175. }
  176. EXPORT_SYMBOL(mcf_pci_outl);
  177. void mcf_pci_outsb(u32 addr, const u8 *buf, u32 len)
  178. {
  179. for (; len; len--)
  180. mcf_pci_outb(*buf++, addr);
  181. }
  182. EXPORT_SYMBOL(mcf_pci_outsb);
  183. void mcf_pci_outsw(u32 addr, const u16 *buf, u32 len)
  184. {
  185. for (; len; len--)
  186. mcf_pci_outw(*buf++, addr);
  187. }
  188. EXPORT_SYMBOL(mcf_pci_outsw);
  189. void mcf_pci_outsl(u32 addr, const u32 *buf, u32 len)
  190. {
  191. for (; len; len--)
  192. mcf_pci_outl(*buf++, addr);
  193. }
  194. EXPORT_SYMBOL(mcf_pci_outsl);
  195. /*
  196. * Initialize the PCI bus registers, and scan the bus.
  197. */
  198. static struct resource mcf_pci_mem = {
  199. .name = "PCI Memory space",
  200. .start = PCI_MEM_PA,
  201. .end = PCI_MEM_PA + PCI_MEM_SIZE - 1,
  202. .flags = IORESOURCE_MEM,
  203. };
  204. static struct resource mcf_pci_io = {
  205. .name = "PCI IO space",
  206. .start = 0x400,
  207. .end = 0x10000 - 1,
  208. .flags = IORESOURCE_IO,
  209. };
  210. /*
  211. * Interrupt mapping and setting.
  212. */
  213. static int mcf_pci_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  214. {
  215. int sid;
  216. sid = mcf_host_slot2sid[slot];
  217. if (sid)
  218. return mcf_host_irq[sid];
  219. return 0;
  220. }
  221. static int __init mcf_pci_init(void)
  222. {
  223. pr_info("ColdFire: PCI bus initialization...\n");
  224. /* Reset the external PCI bus */
  225. __raw_writel(PCIGSCR_RESET, PCIGSCR);
  226. __raw_writel(0, PCITCR);
  227. request_resource(&iomem_resource, &mcf_pci_mem);
  228. request_resource(&iomem_resource, &mcf_pci_io);
  229. /* Configure PCI arbiter */
  230. __raw_writel(PACR_INTMPRI | PACR_INTMINTE | PACR_EXTMPRI(0x1f) |
  231. PACR_EXTMINTE(0x1f), PACR);
  232. /* Set required multi-function pins for PCI bus use */
  233. __raw_writew(0x3ff, MCFGPIO_PAR_PCIBG);
  234. __raw_writew(0x3ff, MCFGPIO_PAR_PCIBR);
  235. /* Set up config space for local host bus controller */
  236. __raw_writel(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
  237. PCI_COMMAND_INVALIDATE, PCISCR);
  238. __raw_writel(PCICR1_LT(32) | PCICR1_CL(8), PCICR1);
  239. __raw_writel(0, PCICR2);
  240. /*
  241. * Set up the initiator windows for memory and IO mapping.
  242. * These give the CPU bus access onto the PCI bus. One for each of
  243. * PCI memory and IO address spaces.
  244. */
  245. __raw_writel(WXBTAR(PCI_MEM_PA, PCI_MEM_BA, PCI_MEM_SIZE),
  246. PCIIW0BTAR);
  247. __raw_writel(WXBTAR(PCI_IO_PA, PCI_IO_BA, PCI_IO_SIZE),
  248. PCIIW1BTAR);
  249. __raw_writel(PCIIWCR_W0_MEM /*| PCIIWCR_W0_MRDL*/ | PCIIWCR_W0_E |
  250. PCIIWCR_W1_IO | PCIIWCR_W1_E, PCIIWCR);
  251. /*
  252. * Set up the target windows for access from the PCI bus back to the
  253. * CPU bus. All we need is access to system RAM (for mastering).
  254. */
  255. __raw_writel(CONFIG_RAMBASE, PCIBAR1);
  256. __raw_writel(CONFIG_RAMBASE | PCITBATR1_E, PCITBATR1);
  257. /* Keep a virtual mapping to IO/config space active */
  258. iospace = (unsigned long) ioremap(PCI_IO_PA, PCI_IO_SIZE);
  259. if (iospace == 0)
  260. return -ENODEV;
  261. pr_info("Coldfire: PCI IO/config window mapped to 0x%x\n",
  262. (u32) iospace);
  263. /* Turn of PCI reset, and wait for devices to settle */
  264. __raw_writel(0, PCIGSCR);
  265. set_current_state(TASK_UNINTERRUPTIBLE);
  266. schedule_timeout(msecs_to_jiffies(200));
  267. rootbus = pci_scan_bus(0, &mcf_pci_ops, NULL);
  268. if (!rootbus)
  269. return -ENODEV;
  270. rootbus->resource[0] = &mcf_pci_io;
  271. rootbus->resource[1] = &mcf_pci_mem;
  272. pci_fixup_irqs(pci_common_swizzle, mcf_pci_map_irq);
  273. pci_bus_size_bridges(rootbus);
  274. pci_bus_assign_resources(rootbus);
  275. pci_bus_add_devices(rootbus);
  276. return 0;
  277. }
  278. subsys_initcall(mcf_pci_init);