leon_pci.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * leon_pci.c: LEON Host PCI support
  4. *
  5. * Copyright (C) 2011 Aeroflex Gaisler AB, Daniel Hellstrom
  6. *
  7. * Code is partially derived from pcic.c
  8. */
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/pci.h>
  13. #include <linux/export.h>
  14. #include <asm/leon.h>
  15. #include <asm/leon_pci.h>
  16. /* The LEON architecture does not rely on a BIOS or bootloader to setup
  17. * PCI for us. The Linux generic routines are used to setup resources,
  18. * reset values of configuration-space register settings are preserved.
  19. *
  20. * PCI Memory and Prefetchable Memory is direct-mapped. However I/O Space is
  21. * accessed through a Window which is translated to low 64KB in PCI space, the
  22. * first 4KB is not used so 60KB is available.
  23. */
  24. void leon_pci_init(struct platform_device *ofdev, struct leon_pci_info *info)
  25. {
  26. LIST_HEAD(resources);
  27. struct pci_bus *root_bus;
  28. struct pci_host_bridge *bridge;
  29. int ret;
  30. bridge = pci_alloc_host_bridge(0);
  31. if (!bridge)
  32. return;
  33. pci_add_resource_offset(&resources, &info->io_space,
  34. info->io_space.start - 0x1000);
  35. pci_add_resource(&resources, &info->mem_space);
  36. info->busn.flags = IORESOURCE_BUS;
  37. pci_add_resource(&resources, &info->busn);
  38. list_splice_init(&resources, &bridge->windows);
  39. bridge->dev.parent = &ofdev->dev;
  40. bridge->sysdata = info;
  41. bridge->busnr = 0;
  42. bridge->ops = info->ops;
  43. bridge->swizzle_irq = pci_common_swizzle;
  44. bridge->map_irq = info->map_irq;
  45. ret = pci_scan_root_bus_bridge(bridge);
  46. if (ret) {
  47. pci_free_host_bridge(bridge);
  48. return;
  49. }
  50. root_bus = bridge->bus;
  51. /* Assign devices with resources */
  52. pci_assign_unassigned_resources();
  53. pci_bus_add_devices(root_bus);
  54. }
  55. int pcibios_enable_device(struct pci_dev *dev, int mask)
  56. {
  57. struct resource *res;
  58. u16 cmd, oldcmd;
  59. int i;
  60. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  61. oldcmd = cmd;
  62. pci_dev_for_each_resource(dev, res, i) {
  63. /* Only set up the requested stuff */
  64. if (!(mask & (1<<i)))
  65. continue;
  66. if (res->flags & IORESOURCE_IO)
  67. cmd |= PCI_COMMAND_IO;
  68. if (res->flags & IORESOURCE_MEM)
  69. cmd |= PCI_COMMAND_MEMORY;
  70. }
  71. if (cmd != oldcmd) {
  72. pci_info(dev, "enabling device (%04x -> %04x)\n", oldcmd, cmd);
  73. pci_write_config_word(dev, PCI_COMMAND, cmd);
  74. }
  75. return 0;
  76. }