raspi4.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-02-06 RT-Thread first version
  9. */
  10. #ifndef __RASPI4_H__
  11. #define __RASPI4_H__
  12. #include <rtthread.h>
  13. //https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2711/rpi_DATA_2711_1p0.pdf
  14. #define __REG32(x) (*((volatile unsigned int *)(x)))
  15. #define __REG16(x) (*((volatile unsigned short *)(x)))
  16. /* GIC IRQ MAX */
  17. #define MAX_HANDLERS (256)
  18. /* base address */
  19. #define PER_BASE (0xFE000000)
  20. //gpio offset
  21. #define GPIO_BASE_OFFSET (0x00200000)
  22. #define PL011_UART_BASE_OFFSET (0x00201000)
  23. //pl011 offset
  24. #define PL011_UART0_BASE_OFFSET (0x00201000)
  25. #define PL011_UART2_BASE_OFFSET (0x00201400)
  26. #define PL011_UART3_BASE_OFFSET (0x00201600)
  27. #define PL011_UART4_BASE_OFFSET (0x00201800)
  28. #define PL011_UART5_BASE_OFFSET (0x00201A00)
  29. //pactl cs offset
  30. #define PACTL_CS_OFFSET (0x00204E00)
  31. //aux offset
  32. #define AUX_BASE_OFFSET (0x00215000)
  33. /* GPIO */
  34. #define GPIO_BASE_ADDR (PER_BASE + GPIO_BASE_OFFSET)
  35. extern size_t gpio_base_addr;
  36. #define GPIO_BASE (gpio_base_addr)
  37. #define GPIO_IRQ_NUM (3) //40 pin mode
  38. #define IRQ_GPIO0 (96 + 49) //bank0 (0 to 27)
  39. #define IRQ_GPIO1 (96 + 50) //bank1 (28 to 45)
  40. #define IRQ_GPIO2 (96 + 51) //bank2 (46 to 57)
  41. #define IRQ_GPIO3 (96 + 52) //bank3
  42. /* Timer (ARM side) */
  43. #define ARM_TIMER_IRQ (64)
  44. extern size_t arm_timer_base;
  45. #define ARM_TIMER_BASE (PER_BASE + 0xB000)
  46. #define ARM_TIMER_LOAD HWREG32(arm_timer_base + 0x400)
  47. #define ARM_TIMER_VALUE HWREG32(arm_timer_base + 0x404)
  48. #define ARM_TIMER_CTRL HWREG32(arm_timer_base + 0x408)
  49. #define ARM_TIMER_IRQCLR HWREG32(arm_timer_base + 0x40C)
  50. #define ARM_TIMER_RAWIRQ HWREG32(arm_timer_base + 0x410)
  51. #define ARM_TIMER_MASKIRQ HWREG32(arm_timer_base + 0x414)
  52. #define ARM_TIMER_RELOAD HWREG32(arm_timer_base + 0x418)
  53. #define ARM_TIMER_PREDIV HWREG32(arm_timer_base + 0x41C)
  54. #define ARM_TIMER_CNTR HWREG32(arm_timer_base + 0x420)
  55. /* UART PL011 */
  56. #define UART_BASE (PER_BASE + PL011_UART_BASE_OFFSET)
  57. //extern uint32_t uart_base_addr;
  58. #define UART0_BASE (UART_BASE + 0x0)
  59. #define UART2_BASE (UART_BASE + 0x400)
  60. #define UART3_BASE (UART_BASE + 0x600)
  61. #define UART4_BASE (UART_BASE + 0x800)
  62. #define UART5_BASE (UART_BASE + 0xA00)
  63. #define IRQ_AUX_UART (96 + 29)
  64. #define UART_REFERENCE_CLOCK (48000000)
  65. /* AUX */
  66. //#define AUX_BASE_ADDR (PER_BASE + AUX_BASE_OFFSET)
  67. //extern uint32_t aux_addr;
  68. //#define AUX_BASE (aux_addr + 0x0)
  69. #define AUX_BASE (PER_BASE + AUX_BASE_OFFSET)
  70. #define IRQ_PL011 (96 + 57)
  71. /* Peripheral IRQ OR-ing */
  72. #define PACTL_CS_ADDR (PER_BASE + PACTL_CS_OFFSET)
  73. extern size_t pactl_cs_base;
  74. #define PACTL_CS HWREG32(pactl_cs_base)
  75. typedef enum
  76. {
  77. IRQ_SPI0 = 0x00000000,
  78. IRQ_SPI1 = 0x00000002,
  79. IRQ_SPI2 = 0x00000004,
  80. IRQ_SPI3 = 0x00000008,
  81. IRQ_SPI4 = 0x00000010,
  82. IRQ_SPI5 = 0x00000020,
  83. IRQ_SPI6 = 0x00000040,
  84. IRQ_I2C0 = 0x00000100,
  85. IRQ_I2C1 = 0x00000200,
  86. IRQ_I2C2 = 0x00000400,
  87. IRQ_I2C3 = 0x00000800,
  88. IRQ_I2C4 = 0x00001000,
  89. IRQ_I2C5 = 0x00002000,
  90. IRQ_I2C6 = 0x00004000,
  91. IRQ_I2C7 = 0x00008000,
  92. IRQ_UART5 = 0x00010000,
  93. IRQ_UART4 = 0x00020000,
  94. IRQ_UART3 = 0x00040000,
  95. IRQ_UART2 = 0x00080000,
  96. IRQ_UART0 = 0x00100000
  97. } PACTL_CS_VAL;
  98. // 0x40, 0x44, 0x48, 0x4c: Core 0~3 Timers interrupt control
  99. #define CORE0_TIMER_IRQ_CTRL HWREG32(0xFF800040)
  100. #define TIMER_IRQ 30
  101. #define NON_SECURE_TIMER_IRQ (1 << 1)
  102. /* GIC */
  103. #define INTC_BASE (0xff800000)
  104. #define ARM_GIC_NR_IRQS (512)
  105. #define ARM_GIC_MAX_NR (512)
  106. #define GIC_V2_BASE (INTC_BASE + 0x00040000)
  107. extern size_t gic_base_addr;
  108. #define GIC_V2_DISTRIBUTOR_BASE (gic_base_addr + 0x1000)
  109. #define GIC_V2_CPU_INTERFACE_BASE (gic_base_addr + 0x2000)
  110. #define GIC_V2_HYPERVISOR_BASE (gic_base_addr + 0x4000)
  111. #define GIC_V2_VIRTUAL_CPU_BASE (gic_base_addr + 0x6000)
  112. #define GIC_PL400_DISTRIBUTOR_PPTR GIC_V2_DISTRIBUTOR_BASE
  113. #define GIC_PL400_CONTROLLER_PPTR GIC_V2_CPU_INTERFACE_BASE
  114. #define GIC_IRQ_START 0
  115. #define GIC_ACK_INTID_MASK 0x000003ff
  116. //watchdog
  117. #define WDT_BASE (PER_BASE + 0x00100000)
  118. extern size_t wdt_base_addr;
  119. #define PM_RSTC HWREG32(wdt_base_addr + 0x1c)
  120. #define PM_RSTS HWREG32(wdt_base_addr + 0x20)
  121. #define PM_WDOG HWREG32(wdt_base_addr + 0x24)
  122. #define PM_PASSWORD (0x5A000000)
  123. #define PM_WDOG_TIME_SET (0x000fffff)
  124. #define PM_RSTS_HADWRH_SET (0x00000040)
  125. #define PM_RSTC_WRCFG_FULL_RESET (0x00000020)
  126. #define PM_RSTC_WRCFG_CLR (0xffffffcf)
  127. #define PM_RSTC_RESET (0x00000102)
  128. //timer
  129. #define ST_BASE_OFFSET (0x003000)
  130. #define STIMER_BASE (PER_BASE + ST_BASE_OFFSET)
  131. extern size_t stimer_base_addr;
  132. #define STIMER_CS __REG32(stimer_base_addr + 0x0000)
  133. #define STIMER_CLO __REG32(stimer_base_addr + 0x0004)
  134. #define STIMER_CHI __REG32(stimer_base_addr + 0x0008)
  135. #define STIMER_C0 __REG32(stimer_base_addr + 0x000C)
  136. #define STIMER_C1 __REG32(stimer_base_addr + 0x0010)
  137. #define STIMER_C2 __REG32(stimer_base_addr + 0x0014)
  138. #define STIMER_C3 __REG32(stimer_base_addr + 0x0018)
  139. #define DELAY_MICROS(micros) \
  140. do{ \
  141. rt_uint32_t compare = STIMER_CLO + micros * 25; \
  142. while (STIMER_CLO < compare); \
  143. } while (0)
  144. //External Mass Media Controller (SD Card)
  145. #define MMC0_BASE_ADDR (PER_BASE+0x300000)
  146. extern size_t mmc0_base_addr;
  147. #define MMC2_BASE_ADDR (PER_BASE+0x340000)
  148. extern size_t mmc2_base_addr;
  149. //mac
  150. #define MAC_REG (void *)(0xfd580000)
  151. extern uint8_t * mac_reg_base_addr;
  152. #define ETH_IRQ (160+29)
  153. #define SEND_DATA_NO_CACHE (0x08200000)
  154. extern uint8_t * eth_send_no_cache;
  155. #define RECV_DATA_NO_CACHE (0x08400000)
  156. extern uint8_t * eth_recv_no_cache;
  157. /* the basic constants and interfaces needed by gic */
  158. rt_inline size_t platform_get_gic_dist_base(void)
  159. {
  160. return GIC_PL400_DISTRIBUTOR_PPTR;
  161. }
  162. rt_inline size_t platform_get_gic_cpu_base(void)
  163. {
  164. return GIC_PL400_CONTROLLER_PPTR;
  165. }
  166. #endif