pci.c 3.7 KB

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