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