pci.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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(pci_dev_t bdf, uint offset, ulong *valuep,
  18. 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(pci_dev_t bdf, uint offset, ulong value,
  35. 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. int pci_x86_clrset_config(pci_dev_t bdf, uint offset, ulong clr, ulong set,
  52. enum pci_size_t size)
  53. {
  54. ulong value;
  55. int ret;
  56. ret = pci_x86_read_config(bdf, offset, &value, size);
  57. if (ret)
  58. return ret;
  59. value &= ~clr;
  60. value |= set;
  61. return pci_x86_write_config(bdf, offset, value, size);
  62. }
  63. void pci_assign_irqs(int bus, int device, u8 irq[4])
  64. {
  65. pci_dev_t bdf;
  66. int func;
  67. u16 vendor;
  68. u8 pin, line;
  69. for (func = 0; func < 8; func++) {
  70. bdf = PCI_BDF(bus, device, func);
  71. pci_read_config16(bdf, PCI_VENDOR_ID, &vendor);
  72. if (vendor == 0xffff || vendor == 0x0000)
  73. continue;
  74. pci_read_config8(bdf, PCI_INTERRUPT_PIN, &pin);
  75. /* PCI spec says all values except 1..4 are reserved */
  76. if ((pin < 1) || (pin > 4))
  77. continue;
  78. line = irq[pin - 1];
  79. if (!line)
  80. continue;
  81. debug("Assigning IRQ %d to PCI device %d.%x.%d (INT%c)\n",
  82. line, bus, device, func, 'A' + pin - 1);
  83. pci_write_config8(bdf, PCI_INTERRUPT_LINE, line);
  84. }
  85. }