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