leon_pci.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * leon_pci.c: LEON Host PCI support
  3. *
  4. * Copyright (C) 2011 Aeroflex Gaisler AB, Daniel Hellstrom
  5. *
  6. * Code is partially derived from pcic.c
  7. */
  8. #include <linux/of_device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/pci.h>
  11. #include <asm/leon.h>
  12. #include <asm/leon_pci.h>
  13. /* The LEON architecture does not rely on a BIOS or bootloader to setup
  14. * PCI for us. The Linux generic routines are used to setup resources,
  15. * reset values of confuration-space registers settings ae preseved.
  16. */
  17. void leon_pci_init(struct platform_device *ofdev, struct leon_pci_info *info)
  18. {
  19. struct pci_bus *root_bus;
  20. root_bus = pci_scan_bus_parented(&ofdev->dev, 0, info->ops, info);
  21. if (root_bus) {
  22. root_bus->resource[0] = &info->io_space;
  23. root_bus->resource[1] = &info->mem_space;
  24. root_bus->resource[2] = NULL;
  25. /* Init all PCI devices into PCI tree */
  26. pci_bus_add_devices(root_bus);
  27. /* Setup IRQs of all devices using custom routines */
  28. pci_fixup_irqs(pci_common_swizzle, info->map_irq);
  29. /* Assign devices with resources */
  30. pci_assign_unassigned_resources();
  31. }
  32. }
  33. /* PCI Memory and Prefetchable Memory is direct-mapped. However I/O Space is
  34. * accessed through a Window which is translated to low 64KB in PCI space, the
  35. * first 4KB is not used so 60KB is available.
  36. *
  37. * This function is used by generic code to translate resource addresses into
  38. * PCI addresses.
  39. */
  40. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  41. struct resource *res)
  42. {
  43. struct leon_pci_info *info = dev->bus->sysdata;
  44. region->start = res->start;
  45. region->end = res->end;
  46. if (res->flags & IORESOURCE_IO) {
  47. region->start -= (info->io_space.start - 0x1000);
  48. region->end -= (info->io_space.start - 0x1000);
  49. }
  50. }
  51. EXPORT_SYMBOL(pcibios_resource_to_bus);
  52. /* see pcibios_resource_to_bus() comment */
  53. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  54. struct pci_bus_region *region)
  55. {
  56. struct leon_pci_info *info = dev->bus->sysdata;
  57. res->start = region->start;
  58. res->end = region->end;
  59. if (res->flags & IORESOURCE_IO) {
  60. res->start += (info->io_space.start - 0x1000);
  61. res->end += (info->io_space.start - 0x1000);
  62. }
  63. }
  64. EXPORT_SYMBOL(pcibios_bus_to_resource);
  65. void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
  66. {
  67. struct leon_pci_info *info = pbus->sysdata;
  68. struct pci_dev *dev;
  69. int i, has_io, has_mem;
  70. u16 cmd;
  71. /* Generic PCI bus probing sets these to point at
  72. * &io{port,mem}_resouce which is wrong for us.
  73. */
  74. if (pbus->self == NULL) {
  75. pbus->resource[0] = &info->io_space;
  76. pbus->resource[1] = &info->mem_space;
  77. pbus->resource[2] = NULL;
  78. }
  79. list_for_each_entry(dev, &pbus->devices, bus_list) {
  80. /*
  81. * We can not rely on that the bootloader has enabled I/O
  82. * or memory access to PCI devices. Instead we enable it here
  83. * if the device has BARs of respective type.
  84. */
  85. has_io = has_mem = 0;
  86. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  87. unsigned long f = dev->resource[i].flags;
  88. if (f & IORESOURCE_IO)
  89. has_io = 1;
  90. else if (f & IORESOURCE_MEM)
  91. has_mem = 1;
  92. }
  93. /* ROM BARs are mapped into 32-bit memory space */
  94. if (dev->resource[PCI_ROM_RESOURCE].end != 0) {
  95. dev->resource[PCI_ROM_RESOURCE].flags |=
  96. IORESOURCE_ROM_ENABLE;
  97. has_mem = 1;
  98. }
  99. pci_bus_read_config_word(pbus, dev->devfn, PCI_COMMAND, &cmd);
  100. if (has_io && !(cmd & PCI_COMMAND_IO)) {
  101. #ifdef CONFIG_PCI_DEBUG
  102. printk(KERN_INFO "LEONPCI: Enabling I/O for dev %s\n",
  103. pci_name(dev));
  104. #endif
  105. cmd |= PCI_COMMAND_IO;
  106. pci_bus_write_config_word(pbus, dev->devfn, PCI_COMMAND,
  107. cmd);
  108. }
  109. if (has_mem && !(cmd & PCI_COMMAND_MEMORY)) {
  110. #ifdef CONFIG_PCI_DEBUG
  111. printk(KERN_INFO "LEONPCI: Enabling MEMORY for dev"
  112. "%s\n", pci_name(dev));
  113. #endif
  114. cmd |= PCI_COMMAND_MEMORY;
  115. pci_bus_write_config_word(pbus, dev->devfn, PCI_COMMAND,
  116. cmd);
  117. }
  118. }
  119. }
  120. /*
  121. * Other archs parse arguments here.
  122. */
  123. char * __devinit pcibios_setup(char *str)
  124. {
  125. return str;
  126. }
  127. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  128. resource_size_t size, resource_size_t align)
  129. {
  130. return res->start;
  131. }
  132. int pcibios_enable_device(struct pci_dev *dev, int mask)
  133. {
  134. return pci_enable_resources(dev, mask);
  135. }
  136. struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
  137. {
  138. /*
  139. * Currently the OpenBoot nodes are not connected with the PCI device,
  140. * this is because the LEON PROM does not create PCI nodes. Eventually
  141. * this will change and the same approach as pcic.c can be used to
  142. * match PROM nodes with pci devices.
  143. */
  144. return NULL;
  145. }
  146. EXPORT_SYMBOL(pci_device_to_OF_node);
  147. void __devinit pcibios_update_irq(struct pci_dev *dev, int irq)
  148. {
  149. #ifdef CONFIG_PCI_DEBUG
  150. printk(KERN_DEBUG "LEONPCI: Assigning IRQ %02d to %s\n", irq,
  151. pci_name(dev));
  152. #endif
  153. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  154. }
  155. /* in/out routines taken from pcic.c
  156. *
  157. * This probably belongs here rather than ioport.c because
  158. * we do not want this crud linked into SBus kernels.
  159. * Also, think for a moment about likes of floppy.c that
  160. * include architecture specific parts. They may want to redefine ins/outs.
  161. *
  162. * We do not use horrible macros here because we want to
  163. * advance pointer by sizeof(size).
  164. */
  165. void outsb(unsigned long addr, const void *src, unsigned long count)
  166. {
  167. while (count) {
  168. count -= 1;
  169. outb(*(const char *)src, addr);
  170. src += 1;
  171. /* addr += 1; */
  172. }
  173. }
  174. EXPORT_SYMBOL(outsb);
  175. void outsw(unsigned long addr, const void *src, unsigned long count)
  176. {
  177. while (count) {
  178. count -= 2;
  179. outw(*(const short *)src, addr);
  180. src += 2;
  181. /* addr += 2; */
  182. }
  183. }
  184. EXPORT_SYMBOL(outsw);
  185. void outsl(unsigned long addr, const void *src, unsigned long count)
  186. {
  187. while (count) {
  188. count -= 4;
  189. outl(*(const long *)src, addr);
  190. src += 4;
  191. /* addr += 4; */
  192. }
  193. }
  194. EXPORT_SYMBOL(outsl);
  195. void insb(unsigned long addr, void *dst, unsigned long count)
  196. {
  197. while (count) {
  198. count -= 1;
  199. *(unsigned char *)dst = inb(addr);
  200. dst += 1;
  201. /* addr += 1; */
  202. }
  203. }
  204. EXPORT_SYMBOL(insb);
  205. void insw(unsigned long addr, void *dst, unsigned long count)
  206. {
  207. while (count) {
  208. count -= 2;
  209. *(unsigned short *)dst = inw(addr);
  210. dst += 2;
  211. /* addr += 2; */
  212. }
  213. }
  214. EXPORT_SYMBOL(insw);
  215. void insl(unsigned long addr, void *dst, unsigned long count)
  216. {
  217. while (count) {
  218. count -= 4;
  219. /*
  220. * XXX I am sure we are in for an unaligned trap here.
  221. */
  222. *(unsigned long *)dst = inl(addr);
  223. dst += 4;
  224. /* addr += 4; */
  225. }
  226. }
  227. EXPORT_SYMBOL(insl);