pci.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. static struct pci_controller *get_hose(void)
  20. {
  21. if (gd->hose)
  22. return gd->hose;
  23. return pci_bus_to_hose(0);
  24. }
  25. unsigned int x86_pci_read_config8(pci_dev_t dev, unsigned where)
  26. {
  27. uint8_t value;
  28. if (pci_hose_read_config_byte(get_hose(), dev, where, &value))
  29. return -1U;
  30. return value;
  31. }
  32. unsigned int x86_pci_read_config16(pci_dev_t dev, unsigned where)
  33. {
  34. uint16_t value;
  35. if (pci_hose_read_config_word(get_hose(), dev, where, &value))
  36. return -1U;
  37. return value;
  38. }
  39. unsigned int x86_pci_read_config32(pci_dev_t dev, unsigned where)
  40. {
  41. uint32_t value;
  42. if (pci_hose_read_config_dword(get_hose(), dev, where, &value))
  43. return -1U;
  44. return value;
  45. }
  46. void x86_pci_write_config8(pci_dev_t dev, unsigned where, unsigned value)
  47. {
  48. pci_hose_write_config_byte(get_hose(), dev, where, value);
  49. }
  50. void x86_pci_write_config16(pci_dev_t dev, unsigned where, unsigned value)
  51. {
  52. pci_hose_write_config_word(get_hose(), dev, where, value);
  53. }
  54. void x86_pci_write_config32(pci_dev_t dev, unsigned where, unsigned value)
  55. {
  56. pci_hose_write_config_dword(get_hose(), dev, where, value);
  57. }
  58. int pci_x86_read_config(struct udevice *bus, pci_dev_t bdf, uint offset,
  59. ulong *valuep, enum pci_size_t size)
  60. {
  61. outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
  62. switch (size) {
  63. case PCI_SIZE_8:
  64. *valuep = inb(PCI_REG_DATA + (offset & 3));
  65. break;
  66. case PCI_SIZE_16:
  67. *valuep = inw(PCI_REG_DATA + (offset & 2));
  68. break;
  69. case PCI_SIZE_32:
  70. *valuep = inl(PCI_REG_DATA);
  71. break;
  72. }
  73. return 0;
  74. }
  75. int pci_x86_write_config(struct udevice *bus, pci_dev_t bdf, uint offset,
  76. ulong value, enum pci_size_t size)
  77. {
  78. outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
  79. switch (size) {
  80. case PCI_SIZE_8:
  81. outb(value, PCI_REG_DATA + (offset & 3));
  82. break;
  83. case PCI_SIZE_16:
  84. outw(value, PCI_REG_DATA + (offset & 2));
  85. break;
  86. case PCI_SIZE_32:
  87. outl(value, PCI_REG_DATA);
  88. break;
  89. }
  90. return 0;
  91. }
  92. void pci_assign_irqs(int bus, int device, u8 irq[4])
  93. {
  94. pci_dev_t bdf;
  95. int func;
  96. u16 vendor;
  97. u8 pin, line;
  98. for (func = 0; func < 8; func++) {
  99. bdf = PCI_BDF(bus, device, func);
  100. vendor = x86_pci_read_config16(bdf, PCI_VENDOR_ID);
  101. if (vendor == 0xffff || vendor == 0x0000)
  102. continue;
  103. pin = x86_pci_read_config8(bdf, PCI_INTERRUPT_PIN);
  104. /* PCI spec says all values except 1..4 are reserved */
  105. if ((pin < 1) || (pin > 4))
  106. continue;
  107. line = irq[pin - 1];
  108. if (!line)
  109. continue;
  110. debug("Assigning IRQ %d to PCI device %d.%x.%d (INT%c)\n",
  111. line, bus, device, func, 'A' + pin - 1);
  112. x86_pci_write_config8(bdf, PCI_INTERRUPT_LINE, line);
  113. }
  114. }