raspi4.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef __RASPI4_H__
  2. #define __RASPI4_H__
  3. //https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2711/rpi_DATA_2711_1p0.pdf
  4. #define __REG32(x) (*((volatile unsigned int *)(x)))
  5. #define __REG16(x) (*((volatile unsigned short *)(x)))
  6. /* GIC IRQ MAX */
  7. #define MAX_HANDLERS (256)
  8. /* base address */
  9. #define PER_BASE (0xFE000000)
  10. //gpio offset
  11. #define GPIO_BASE_OFFSET (0x00200000)
  12. //pl011 offset
  13. #define PL011_UART0_BASE_OFFSET (0x00201000)
  14. #define PL011_UART2_BASE_OFFSET (0x00201400)
  15. #define PL011_UART3_BASE_OFFSET (0x00201600)
  16. #define PL011_UART4_BASE_OFFSET (0x00201800)
  17. #define PL011_UART5_BASE_OFFSET (0x00201A00)
  18. //pactl cs offset
  19. #define PACTL_CS_OFFSET (0x00204E00)
  20. //aux offset
  21. #define AUX_BASE_OFFSET (0x00215000)
  22. /* GPIO */
  23. #define GPIO_BASE (PER_BASE + GPIO_BASE_OFFSET)
  24. #define GPIO_IRQ_NUM (3) //40 pin mode
  25. #define IRQ_GPIO0 (96 + 49) //bank0 (0 to 27)
  26. #define IRQ_GPIO1 (96 + 50) //bank1 (28 to 45)
  27. #define IRQ_GPIO2 (96 + 51) //bank2 (46 to 57)
  28. #define IRQ_GPIO3 (96 + 52) //bank3
  29. /* Timer (ARM side) */
  30. #define ARM_TIMER_IRQ (64)
  31. #define ARM_TIMER_BASE (PER_BASE + 0xB000)
  32. #define ARM_TIMER_LOAD HWREG32(ARM_TIMER_BASE + 0x400)
  33. #define ARM_TIMER_VALUE HWREG32(ARM_TIMER_BASE + 0x404)
  34. #define ARM_TIMER_CTRL HWREG32(ARM_TIMER_BASE + 0x408)
  35. #define ARM_TIMER_IRQCLR HWREG32(ARM_TIMER_BASE + 0x40C)
  36. #define ARM_TIMER_RAWIRQ HWREG32(ARM_TIMER_BASE + 0x410)
  37. #define ARM_TIMER_MASKIRQ HWREG32(ARM_TIMER_BASE + 0x414)
  38. #define ARM_TIMER_RELOAD HWREG32(ARM_TIMER_BASE + 0x418)
  39. #define ARM_TIMER_PREDIV HWREG32(ARM_TIMER_BASE + 0x41C)
  40. #define ARM_TIMER_CNTR HWREG32(ARM_TIMER_BASE + 0x420)
  41. /* UART PL011 */
  42. #define UART0_BASE (PER_BASE + PL011_UART0_BASE_OFFSET)
  43. #define UART2_BASE (PER_BASE + PL011_UART2_BASE_OFFSET)
  44. #define UART3_BASE (PER_BASE + PL011_UART3_BASE_OFFSET)
  45. #define UART4_BASE (PER_BASE + PL011_UART4_BASE_OFFSET)
  46. #define UART5_BASE (PER_BASE + PL011_UART5_BASE_OFFSET)
  47. #define IRQ_AUX_UART (96 + 29)
  48. #define UART_REFERENCE_CLOCK (48000000)
  49. /* AUX */
  50. #define AUX_BASE (PER_BASE + AUX_BASE_OFFSET)
  51. #define IRQ_PL011 (96 + 57)
  52. /* SPI */
  53. #define SPI_0_BASE_OFFSET (0x00204000)
  54. #define SPI_3_BASE_OFFSET (0x00204600)
  55. #define SPI_4_BASE_OFFSET (0x00204800)
  56. #define SPI_5_BASE_OFFSET (0x00204A00)
  57. #define SPI_6_BASE_OFFSET (0x00204C00)
  58. #define SPI_0_BASE (PER_BASE + SPI_0_BASE_OFFSET)
  59. #define SPI_3_BASE (PER_BASE + SPI_3_BASE_OFFSET)
  60. #define SPI_4_BASE (PER_BASE + SPI_4_BASE_OFFSET)
  61. #define SPI_5_BASE (PER_BASE + SPI_5_BASE_OFFSET)
  62. #define SPI_6_BASE (PER_BASE + SPI_6_BASE_OFFSET)
  63. /* Peripheral IRQ OR-ing */
  64. #define PACTL_CS HWREG32((PER_BASE + PACTL_CS_OFFSET))
  65. typedef enum {
  66. IRQ_SPI0 = 0x00000000,
  67. IRQ_SPI1 = 0x00000002,
  68. IRQ_SPI2 = 0x00000004,
  69. IRQ_SPI3 = 0x00000008,
  70. IRQ_SPI4 = 0x00000010,
  71. IRQ_SPI5 = 0x00000020,
  72. IRQ_SPI6 = 0x00000040,
  73. IRQ_I2C0 = 0x00000100,
  74. IRQ_I2C1 = 0x00000200,
  75. IRQ_I2C2 = 0x00000400,
  76. IRQ_I2C3 = 0x00000800,
  77. IRQ_I2C4 = 0x00001000,
  78. IRQ_I2C5 = 0x00002000,
  79. IRQ_I2C6 = 0x00004000,
  80. IRQ_I2C7 = 0x00008000,
  81. IRQ_UART5 = 0x00010000,
  82. IRQ_UART4 = 0x00020000,
  83. IRQ_UART3 = 0x00040000,
  84. IRQ_UART2 = 0x00080000,
  85. IRQ_UART0 = 0x00100000
  86. } PACTL_CS_VAL;
  87. // 0x40, 0x44, 0x48, 0x4c: Core 0~3 Timers interrupt control
  88. #define CORE0_TIMER_IRQ_CTRL HWREG32(0xFF800040)
  89. #define TIMER_IRQ 30
  90. #define NON_SECURE_TIMER_IRQ (1 << 1)
  91. /* GIC */
  92. #define INTC_BASE (0xff800000)
  93. #define ARM_GIC_NR_IRQS (512)
  94. #define ARM_GIC_MAX_NR (512)
  95. #define GIC_V2_DISTRIBUTOR_BASE (INTC_BASE + 0x00041000)
  96. #define GIC_V2_CPU_INTERFACE_BASE (INTC_BASE + 0x00042000)
  97. #define GIC_V2_HYPERVISOR_BASE (INTC_BASE + 0x00044000)
  98. #define GIC_V2_VIRTUAL_CPU_BASE (INTC_BASE + 0x00046000)
  99. #define GIC_PL400_DISTRIBUTOR_PPTR GIC_V2_DISTRIBUTOR_BASE
  100. #define GIC_PL400_CONTROLLER_PPTR GIC_V2_CPU_INTERFACE_BASE
  101. #define GIC_IRQ_START 0
  102. #define GIC_ACK_INTID_MASK 0x000003ff
  103. //watchdog
  104. #define PM_RSTC HWREG32(PER_BASE + 0x0010001c)
  105. #define PM_RSTS HWREG32(PER_BASE + 0x00100020)
  106. #define PM_WDOG HWREG32(PER_BASE + 0x00100024)
  107. #define PM_PASSWORD (0x5A000000)
  108. #define PM_WDOG_TIME_SET (0x000fffff)
  109. #define PM_RSTS_HADWRH_SET (0x00000040)
  110. #define PM_RSTC_WRCFG_FULL_RESET (0x00000020)
  111. #define PM_RSTC_WRCFG_CLR (0xffffffcf)
  112. #define PM_RSTC_RESET (0x00000102)
  113. //timer
  114. #define ST_BASE_OFFSET (0x003000)
  115. #define STIMER_BASE (PER_BASE + ST_BASE_OFFSET)
  116. #define STIMER_CS __REG32(STIMER_BASE + 0x0000)
  117. #define STIMER_CLO __REG32(STIMER_BASE + 0x0004)
  118. #define STIMER_CHI __REG32(STIMER_BASE + 0x0008)
  119. #define STIMER_C0 __REG32(STIMER_BASE + 0x000C)
  120. #define STIMER_C1 __REG32(STIMER_BASE + 0x0010)
  121. #define STIMER_C2 __REG32(STIMER_BASE + 0x0014)
  122. #define STIMER_C3 __REG32(STIMER_BASE + 0x0018)
  123. #define DELAY_MICROS(micros) \
  124. do{ \
  125. rt_uint32_t compare = STIMER_CLO + micros * 25; \
  126. while (STIMER_CLO < compare); \
  127. } while (0) \
  128. //External Mass Media Controller (SD Card)
  129. #define MMC0_BASE_ADDR (PER_BASE+0x300000)
  130. #define MMC2_BASE_ADDR (PER_BASE+0x340000)
  131. #define ETH_IRQ (160+29)
  132. //I2C
  133. #define BSC0_BASE_OFFSET (0x205000)
  134. #define BSC1_BASE_OFFSET (0x804000)
  135. #define BSC3_BASE_OFFSET (0x205600)
  136. #define BSC4_BASE_OFFSET (0x205800)
  137. #define BSC5_BASE_OFFSET (0x205A80)
  138. #define BSC6_BASE_OFFSET (0x205C00)
  139. //BSC2 and BSC7 masters are dedicated for use by the
  140. //HDMI interfaces and should not be accessed byuser programs.
  141. #define BSC0_BASE (PER_BASE + BSC0_BASE_OFFSET)
  142. #define BSC1_BASE (PER_BASE + BSC1_BASE_OFFSET)
  143. #define BSC3_BASE (PER_BASE + BSC3_BASE_OFFSET)
  144. #define BSC4_BASE (PER_BASE + BSC4_BASE_OFFSET)
  145. #define BSC5_BASE (PER_BASE + BSC5_BASE_OFFSET)
  146. #define BSC6_BASE (PER_BASE + BSC6_BASE_OFFSET)
  147. /* the basic constants and interfaces needed by gic */
  148. rt_inline rt_uint32_t platform_get_gic_dist_base(void)
  149. {
  150. return GIC_PL400_DISTRIBUTOR_PPTR;
  151. }
  152. rt_inline rt_uint32_t platform_get_gic_cpu_base(void)
  153. {
  154. return GIC_PL400_CONTROLLER_PPTR;
  155. }
  156. static inline void __DSB(void)
  157. {
  158. __asm__ volatile ("dsb 0xF":::"memory");
  159. }
  160. #endif