pci.c 1.8 KB

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