pci.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  4. * (C) Copyright 2008,2009
  5. * Graeme Russ, <graeme.russ@gmail.com>
  6. *
  7. * (C) Copyright 2002
  8. * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
  9. */
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <errno.h>
  13. #include <malloc.h>
  14. #include <pci.h>
  15. #include <asm/io.h>
  16. #include <asm/pci.h>
  17. int pci_x86_read_config(struct udevice *bus, pci_dev_t bdf, uint offset,
  18. ulong *valuep, enum pci_size_t size)
  19. {
  20. outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
  21. switch (size) {
  22. case PCI_SIZE_8:
  23. *valuep = inb(PCI_REG_DATA + (offset & 3));
  24. break;
  25. case PCI_SIZE_16:
  26. *valuep = inw(PCI_REG_DATA + (offset & 2));
  27. break;
  28. case PCI_SIZE_32:
  29. *valuep = inl(PCI_REG_DATA);
  30. break;
  31. }
  32. return 0;
  33. }
  34. int pci_x86_write_config(struct udevice *bus, pci_dev_t bdf, uint offset,
  35. ulong value, enum pci_size_t size)
  36. {
  37. outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
  38. switch (size) {
  39. case PCI_SIZE_8:
  40. outb(value, PCI_REG_DATA + (offset & 3));
  41. break;
  42. case PCI_SIZE_16:
  43. outw(value, PCI_REG_DATA + (offset & 2));
  44. break;
  45. case PCI_SIZE_32:
  46. outl(value, PCI_REG_DATA);
  47. break;
  48. }
  49. return 0;
  50. }
  51. void pci_assign_irqs(int bus, int device, u8 irq[4])
  52. {
  53. pci_dev_t bdf;
  54. int func;
  55. u16 vendor;
  56. u8 pin, line;
  57. for (func = 0; func < 8; func++) {
  58. bdf = PCI_BDF(bus, device, func);
  59. pci_read_config16(bdf, PCI_VENDOR_ID, &vendor);
  60. if (vendor == 0xffff || vendor == 0x0000)
  61. continue;
  62. pci_read_config8(bdf, PCI_INTERRUPT_PIN, &pin);
  63. /* PCI spec says all values except 1..4 are reserved */
  64. if ((pin < 1) || (pin > 4))
  65. continue;
  66. line = irq[pin - 1];
  67. if (!line)
  68. continue;
  69. debug("Assigning IRQ %d to PCI device %d.%x.%d (INT%c)\n",
  70. line, bus, device, func, 'A' + pin - 1);
  71. pci_write_config8(bdf, PCI_INTERRUPT_LINE, line);
  72. }
  73. }