pci.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <log.h>
  14. #include <malloc.h>
  15. #include <pci.h>
  16. #include <asm/io.h>
  17. #include <asm/pci.h>
  18. int pci_x86_read_config(pci_dev_t bdf, uint offset, ulong *valuep,
  19. enum pci_size_t size)
  20. {
  21. outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
  22. switch (size) {
  23. case PCI_SIZE_8:
  24. *valuep = inb(PCI_REG_DATA + (offset & 3));
  25. break;
  26. case PCI_SIZE_16:
  27. *valuep = inw(PCI_REG_DATA + (offset & 2));
  28. break;
  29. case PCI_SIZE_32:
  30. *valuep = inl(PCI_REG_DATA);
  31. break;
  32. }
  33. return 0;
  34. }
  35. int pci_x86_write_config(pci_dev_t bdf, uint offset, ulong value,
  36. enum pci_size_t size)
  37. {
  38. outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
  39. switch (size) {
  40. case PCI_SIZE_8:
  41. outb(value, PCI_REG_DATA + (offset & 3));
  42. break;
  43. case PCI_SIZE_16:
  44. outw(value, PCI_REG_DATA + (offset & 2));
  45. break;
  46. case PCI_SIZE_32:
  47. outl(value, PCI_REG_DATA);
  48. break;
  49. }
  50. return 0;
  51. }
  52. int pci_x86_clrset_config(pci_dev_t bdf, uint offset, ulong clr, ulong set,
  53. enum pci_size_t size)
  54. {
  55. ulong value;
  56. int ret;
  57. ret = pci_x86_read_config(bdf, offset, &value, size);
  58. if (ret)
  59. return ret;
  60. value &= ~clr;
  61. value |= set;
  62. return pci_x86_write_config(bdf, offset, value, size);
  63. }
  64. void pci_assign_irqs(int bus, int device, u8 irq[4])
  65. {
  66. pci_dev_t bdf;
  67. int func;
  68. u16 vendor;
  69. u8 pin, line;
  70. for (func = 0; func < 8; func++) {
  71. bdf = PCI_BDF(bus, device, func);
  72. pci_read_config16(bdf, PCI_VENDOR_ID, &vendor);
  73. if (vendor == 0xffff || vendor == 0x0000)
  74. continue;
  75. pci_read_config8(bdf, PCI_INTERRUPT_PIN, &pin);
  76. /* PCI spec says all values except 1..4 are reserved */
  77. if ((pin < 1) || (pin > 4))
  78. continue;
  79. line = irq[pin - 1];
  80. if (!line)
  81. continue;
  82. debug("Assigning IRQ %d to PCI device %d.%x.%d (INT%c)\n",
  83. line, bus, device, func, 'A' + pin - 1);
  84. pci_write_config8(bdf, PCI_INTERRUPT_LINE, line);
  85. }
  86. }