pci.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2009 Corey Tabaka
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef __PCI_H
  24. #define __PCI_H
  25. #include <sys/types.h>
  26. #include <compiler.h>
  27. /*
  28. * PCI access return codes
  29. */
  30. #define _PCI_SUCCESSFUL 0x00
  31. #define _PCI_FUNC_NOT_SUPPORTED 0x81
  32. #define _PCI_BAD_VENDOR_ID 0x83
  33. #define _PCI_DEVICE_NOT_FOUND 0x86
  34. #define _PCI_BAD_REGISTER_NUMBER 0x87
  35. #define _PCI_SET_FAILED 0x88
  36. #define _PCI_BUFFER_TOO_SMALL 0x89
  37. /*
  38. * PCI configuration space offsets
  39. */
  40. #define PCI_CONFIG_VENDOR_ID 0x00
  41. #define PCI_CONFIG_DEVICE_ID 0x02
  42. #define PCI_CONFIG_COMMAND 0x04
  43. #define PCI_CONFIG_STATUS 0x06
  44. #define PCI_CONFIG_REVISION_ID 0x08
  45. #define PCI_CONFIG_CLASS_CODE 0x09
  46. #define PCI_CONFIG_CACHE_LINE_SIZE 0x0c
  47. #define PCI_CONFIG_LATENCY_TIMER 0x0d
  48. #define PCI_CONFIG_HEADER_TYPE 0x0e
  49. #define PCI_CONFIG_BIST 0x0f
  50. #define PCI_CONFIG_BASE_ADDRESSES 0x10
  51. #define PCI_CONFIG_CARDBUS_CIS_PTR 0x28
  52. #define PCI_CONFIG_SUBSYS_VENDOR_ID 0x2c
  53. #define PCI_CONFIG_SUBSYS_ID 0x2e
  54. #define PCI_CONFIG_EXP_ROM_ADDRESS 0x30
  55. #define PCI_CONFIG_CAPABILITIES 0x34
  56. #define PCI_CONFIG_INTERRUPT_LINE 0x3c
  57. #define PCI_CONFIG_INTERRUPT_PIN 0x3d
  58. #define PCI_CONFIG_MIN_GRANT 0x3e
  59. #define PCI_CONFIG_MAX_LATENCY 0x3f
  60. /*
  61. * PCI header type register bits
  62. */
  63. #define PCI_HEADER_TYPE_MASK 0x7f
  64. #define PCI_HEADER_TYPE_MULTI_FN 0x80
  65. /*
  66. * PCI header types
  67. */
  68. #define PCI_HEADER_TYPE_STANDARD 0x00
  69. #define PCI_HEADER_TYPE_PCI_BRIDGE 0x01
  70. #define PCI_HEADER_TYPE_CARD_BUS 0x02
  71. /*
  72. * PCI command register bits
  73. */
  74. #define PCI_COMMAND_IO_EN 0x0001
  75. #define PCI_COMMAND_MEM_EN 0x0002
  76. #define PCI_COMMAND_BUS_MASTER_EN 0x0004
  77. #define PCI_COMMAND_SPECIAL_EN 0x0008
  78. #define PCI_COMMAND_MEM_WR_INV_EN 0x0010
  79. #define PCI_COMMAND_PAL_SNOOP_EN 0x0020
  80. #define PCI_COMMAND_PERR_RESP_EN 0x0040
  81. #define PCI_COMMAND_AD_STEP_EN 0x0080
  82. #define PCI_COMMAND_SERR_EN 0x0100
  83. #define PCI_COMMAND_FAST_B2B_EN 0x0200
  84. /*
  85. * PCI status register bits
  86. */
  87. #define PCI_STATUS_NEW_CAPS 0x0010
  88. #define PCI_STATUS_66_MHZ 0x0020
  89. #define PCI_STATUS_FAST_B2B 0x0080
  90. #define PCI_STATUS_MSTR_PERR 0x0100
  91. #define PCI_STATUS_DEVSEL_MASK 0x0600
  92. #define PCI_STATUS_TARG_ABORT_SIG 0x0800
  93. #define PCI_STATUS_TARG_ABORT_RCV 0x1000
  94. #define PCI_STATUS_MSTR_ABORT_RCV 0x2000
  95. #define PCI_STATUS_SERR_SIG 0x4000
  96. #define PCI_STATUS_PERR 0x8000
  97. typedef struct {
  98. uint16_t vendor_id;
  99. uint16_t device_id;
  100. uint16_t command;
  101. uint16_t status;
  102. uint8_t revision_id_0;
  103. uint8_t program_interface;
  104. uint8_t sub_class;
  105. uint8_t base_class;
  106. uint8_t cache_line_size;
  107. uint8_t latency_timer;
  108. uint8_t header_type;
  109. uint8_t bist;
  110. uint32_t base_addresses[6];
  111. uint32_t cardbus_cis_ptr;
  112. uint16_t subsystem_vendor_id;
  113. uint16_t subsystem_id;
  114. uint32_t expansion_rom_address;
  115. uint8_t capabilities_ptr;
  116. uint8_t reserved_0[3];
  117. uint32_t reserved_1;
  118. uint8_t interrupt_line;
  119. uint8_t interrupt_pin;
  120. uint8_t min_grant;
  121. uint8_t max_latency;
  122. } __PACKED pci_config_t;
  123. /*
  124. * PCI address structure
  125. */
  126. typedef struct
  127. {
  128. uint8_t bus;
  129. uint8_t dev_fn;
  130. } pci_location_t;
  131. typedef struct {
  132. uint8_t id;
  133. uint8_t next;
  134. } __PACKED pci_capability_t;
  135. typedef struct {
  136. uint8_t bus;
  137. uint8_t device;
  138. uint8_t link_int_a;
  139. uint16_t irq_int_a;
  140. uint8_t link_int_b;
  141. uint16_t irq_int_b;
  142. uint8_t link_int_c;
  143. uint16_t irq_int_c;
  144. uint8_t link_int_d;
  145. uint16_t irq_int_d;
  146. uint8_t slot;
  147. uint8_t reserved;
  148. } __PACKED irq_routing_entry;
  149. void pci_init(void);
  150. int pci_get_last_bus(void);
  151. int pci_find_pci_device(pci_location_t *state, uint16_t device_id, uint16_t vendor_id, uint16_t index);
  152. int pci_find_pci_class_code(pci_location_t *state, uint32_t class_code, uint16_t index);
  153. int pci_read_config_byte(const pci_location_t *state, uint32_t reg, uint8_t *value);
  154. int pci_read_config_half(const pci_location_t *state, uint32_t reg, uint16_t *value);
  155. int pci_read_config_word(const pci_location_t *state, uint32_t reg, uint32_t *value);
  156. int pci_write_config_byte(const pci_location_t *state, uint32_t reg, uint8_t value);
  157. int pci_write_config_half(const pci_location_t *state, uint32_t reg, uint16_t value);
  158. int pci_write_config_word(const pci_location_t *state, uint32_t reg, uint32_t value);
  159. int pci_get_irq_routing_options(irq_routing_entry *entries, uint16_t *count, uint16_t *pci_irqs);
  160. int pci_set_irq_hw_int(const pci_location_t *state, uint8_t int_pin, uint8_t irq);
  161. #endif