pci.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-08-04 JasonHu first version
  9. */
  10. #ifndef __PCI_H__
  11. #define __PCI_H__
  12. #include <rtdef.h>
  13. #define PCI_CONFIG_ADDR 0xCF8 /* PCI configuration space address port */
  14. #define PCI_CONFIG_DATA 0xCFC /* PCI configuration space data port */
  15. #define PCI_DEVICE_VENDER 0x00
  16. #define PCI_STATUS_COMMAND 0x04
  17. #define PCI_CLASS_CODE_REVISION_ID 0x08
  18. #define PCI_BIST_HEADER_TYPE 0x0C
  19. #define PCI_BASS_ADDRESS0 0x10
  20. #define PCI_BASS_ADDRESS1 0x14
  21. #define PCI_BASS_ADDRESS2 0x18
  22. #define PCI_BASS_ADDRESS3 0x1C
  23. #define PCI_BASS_ADDRESS4 0x20
  24. #define PCI_BASS_ADDRESS5 0x24
  25. #define PCI_CARD_BUS_POINTER 0x28
  26. #define PCI_SUBSYSTEM_ID 0x2C
  27. #define PCI_EXPANSION_ROM_BASE_ADDR 0x30
  28. #define PCI_CAPABILITY_LIST 0x34
  29. #define PCI_RESERVED 0x38
  30. #define PCI_IRQ_PIN_IRQ_LINE 0x3C
  31. #define PCI_COMMAND_IO 0x1 /* Enable response in I/O space */
  32. #define PCI_COMMAND_MEMORY 0x2 /* Enable response in Memory space */
  33. #define PCI_COMMAND_MASTER 0x4 /* Enable bus mastering */
  34. #define PCI_COMMAND_SPECIAL 0x8 /* Enable response to special cycles */
  35. #define PCI_COMMAND_INVALIDATE 0x10 /* Use memory write and invalidate */
  36. #define PCI_COMMAND_VGA_PALETTE 0x20 /* Enable palette snooping */
  37. #define PCI_COMMAND_PARITY 0x40 /* Enable parity checking */
  38. #define PCI_COMMAND_WAIT 0x80 /* Enable address/data stepping */
  39. #define PCI_COMMAND_SERR 0x100 /* Enable SERR */
  40. #define PCI_COMMAND_FAST_BACK 0x200 /* Enable back-to-back writes */
  41. #define PCI_COMMAND_INTX_DISABLE 0x400 /* INTx Emulation Disable */
  42. #define PCI_BASE_ADDR_MEM_MASK (~0x0FUL)
  43. #define PCI_BASE_ADDR_IO_MASK (~0x03UL)
  44. #define PCI_BAR_TYPE_INVALID 0
  45. #define PCI_BAR_TYPE_MEM 1
  46. #define PCI_BAR_TYPE_IO 2
  47. #define PCI_MAX_BAR_NR 6 /* Each device has up to 6 address information */
  48. #define PCI_MAX_BUS_NR 256 /* PCI has a total of 256 buses */
  49. #define PCI_MAX_DEV_NR 32 /* There are a total of 32 devices on each PCI bus */
  50. #define PCI_MAX_FUN_NR 8 /* PCI devices have a total of 8 function numbers */
  51. #ifndef PCI_ANY_ID
  52. #define PCI_ANY_ID (~0)
  53. #endif
  54. struct rt_pci_device_id
  55. {
  56. rt_uint32_t vendor, device; /* vendor and device id or PCI_ANY_ID */
  57. rt_uint32_t subvendor, subdevice; /* subsystem's id or PCI_ANY_ID */
  58. rt_uint32_t class_value, class_mask;
  59. };
  60. typedef struct rt_pci_device_id rt_pci_device_id_t;
  61. struct rt_pci_device_bar
  62. {
  63. rt_uint32_t type; /* Type of address bar (IO address/MEM address) */
  64. rt_uint32_t base_addr;
  65. rt_uint32_t length; /* Length of address */
  66. };
  67. typedef struct rt_pci_device_bar rt_pci_device_bar_t;
  68. struct rt_pci_device
  69. {
  70. rt_list_t list; /* list for all pci device */
  71. rt_uint8_t bus; /* bus number */
  72. rt_uint8_t dev; /* device number */
  73. rt_uint8_t function; /* Function number */
  74. rt_uint16_t vendor_id; /* Configuration space:Vendor ID */
  75. rt_uint16_t device_id; /* Configuration space:Device ID */
  76. rt_uint16_t command; /* Configuration space:Command */
  77. rt_uint16_t status; /* Configuration space:Status */
  78. rt_uint32_t class_code; /* Configuration space:Class Code */
  79. rt_uint8_t revision_id; /* Configuration space:Revision ID */
  80. rt_uint8_t multi_function;
  81. rt_uint32_t card_bus_pointer;
  82. rt_uint16_t subsystem_vendor_id;
  83. rt_uint16_t subsystem_device_id;
  84. rt_uint32_t expansion_rom_base_addr;
  85. rt_uint32_t capability_list;
  86. rt_uint8_t irq_line; /*Configuration space:IRQ line*/
  87. rt_uint8_t irq_pin; /*Configuration space:IRQ pin*/
  88. rt_uint8_t min_gnt;
  89. rt_uint8_t max_lat;
  90. rt_pci_device_bar_t bars[PCI_MAX_BAR_NR];
  91. };
  92. typedef struct rt_pci_device rt_pci_device_t;
  93. rt_uint32_t rt_pci_device_get_io_addr(rt_pci_device_t *device);
  94. rt_uint32_t rt_pci_device_get_mem_addr(rt_pci_device_t *device);
  95. rt_uint32_t rt_pci_device_get_mem_len(rt_pci_device_t *device);
  96. rt_uint32_t rt_pci_device_get_irq_line(rt_pci_device_t *device);
  97. void rt_pci_enable_bus_mastering(rt_pci_device_t *device);
  98. rt_pci_device_t* rt_pci_device_get(rt_uint32_t vendor_id, rt_uint32_t device_id);
  99. rt_pci_device_t* rt_pci_device_get_by_class_code(rt_uint32_t class_value, rt_uint32_t sub_class);
  100. void rt_pci_device_bar_dump(rt_pci_device_bar_t *bar);
  101. void rt_pci_device_dump(rt_pci_device_t *device);
  102. rt_uint32_t rt_pci_device_read(rt_pci_device_t *device, rt_uint32_t reg);
  103. void rt_pci_device_write(rt_pci_device_t *device, rt_uint32_t reg, rt_uint32_t value);
  104. void rt_pci_init(void);
  105. #endif /* __PCI_H__ */