github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/device/nxp/mimxrt1062_mpu.go (about)

     1  // Hand created file. DO NOT DELETE.
     2  // Type definitions, fields, and constants associated with the MPU peripheral
     3  // of the NXP MIMXRT1062.
     4  
     5  //go:build nxp && mimxrt1062
     6  
     7  package nxp
     8  
     9  import (
    10  	"device/arm"
    11  	"runtime/volatile"
    12  	"unsafe"
    13  )
    14  
    15  type MPU_Type struct {
    16  	TYPE    volatile.Register32 // 0x000 (R/ ) - MPU Type Register
    17  	CTRL    volatile.Register32 // 0x004 (R/W) - MPU Control Register
    18  	RNR     volatile.Register32 // 0x008 (R/W) - MPU Region RNRber Register
    19  	RBAR    volatile.Register32 // 0x00C (R/W) - MPU Region Base Address Register
    20  	RASR    volatile.Register32 // 0x010 (R/W) - MPU Region Attribute and Size Register
    21  	RBAR_A1 volatile.Register32 // 0x014 (R/W) - MPU Alias 1 Region Base Address Register
    22  	RASR_A1 volatile.Register32 // 0x018 (R/W) - MPU Alias 1 Region Attribute and Size Register
    23  	RBAR_A2 volatile.Register32 // 0x01C (R/W) - MPU Alias 2 Region Base Address Register
    24  	RASR_A2 volatile.Register32 // 0x020 (R/W) - MPU Alias 2 Region Attribute and Size Register
    25  	RBAR_A3 volatile.Register32 // 0x024 (R/W) - MPU Alias 3 Region Base Address Register
    26  	RASR_A3 volatile.Register32 // 0x028 (R/W) - MPU Alias 3 Region Attribute and Size Register
    27  }
    28  
    29  var MPU = (*MPU_Type)(unsafe.Pointer(uintptr(0xe000ed90)))
    30  
    31  type (
    32  	RegionSize  uint32
    33  	AccessPerms uint32
    34  	Extension   uint32
    35  )
    36  
    37  // MPU Control Register Definitions
    38  const (
    39  	MPU_CTRL_PRIVDEFENA_Pos = 2                            // MPU CTRL: PRIVDEFENA Position
    40  	MPU_CTRL_PRIVDEFENA_Msk = 1 << MPU_CTRL_PRIVDEFENA_Pos // MPU CTRL: PRIVDEFENA Mask
    41  	MPU_CTRL_HFNMIENA_Pos   = 1                            // MPU CTRL: HFNMIENA Position
    42  	MPU_CTRL_HFNMIENA_Msk   = 1 << MPU_CTRL_HFNMIENA_Pos   // MPU CTRL: HFNMIENA Mask
    43  	MPU_CTRL_ENABLE_Pos     = 0                            // MPU CTRL: ENABLE Position
    44  	MPU_CTRL_ENABLE_Msk     = 1                            // MPU CTRL: ENABLE Mask
    45  )
    46  
    47  // MPU Region Base Address Register Definitions
    48  const (
    49  	MPU_RBAR_ADDR_Pos   = 5                              // MPU RBAR: ADDR Position
    50  	MPU_RBAR_ADDR_Msk   = 0x7FFFFFF << MPU_RBAR_ADDR_Pos // MPU RBAR: ADDR Mask
    51  	MPU_RBAR_VALID_Pos  = 4                              // MPU RBAR: VALID Position
    52  	MPU_RBAR_VALID_Msk  = 1 << MPU_RBAR_VALID_Pos        // MPU RBAR: VALID Mask
    53  	MPU_RBAR_REGION_Pos = 0                              // MPU RBAR: REGION Position
    54  	MPU_RBAR_REGION_Msk = 0xF                            // MPU RBAR: REGION Mask
    55  )
    56  
    57  // MPU Region Attribute and Size Register Definitions
    58  const (
    59  	MPU_RASR_ATTRS_Pos  = 16                           // MPU RASR: MPU Region Attribute field Position
    60  	MPU_RASR_ATTRS_Msk  = 0xFFFF << MPU_RASR_ATTRS_Pos // MPU RASR: MPU Region Attribute field Mask
    61  	MPU_RASR_XN_Pos     = 28                           // MPU RASR: ATTRS.XN Position
    62  	MPU_RASR_XN_Msk     = 1 << MPU_RASR_XN_Pos         // MPU RASR: ATTRS.XN Mask
    63  	MPU_RASR_AP_Pos     = 24                           // MPU RASR: ATTRS.AP Position
    64  	MPU_RASR_AP_Msk     = 0x7 << MPU_RASR_AP_Pos       // MPU RASR: ATTRS.AP Mask
    65  	MPU_RASR_TEX_Pos    = 19                           // MPU RASR: ATTRS.TEX Position
    66  	MPU_RASR_TEX_Msk    = 0x7 << MPU_RASR_TEX_Pos      // MPU RASR: ATTRS.TEX Mask
    67  	MPU_RASR_S_Pos      = 18                           // MPU RASR: ATTRS.S Position
    68  	MPU_RASR_S_Msk      = 1 << MPU_RASR_S_Pos          // MPU RASR: ATTRS.S Mask
    69  	MPU_RASR_C_Pos      = 17                           // MPU RASR: ATTRS.C Position
    70  	MPU_RASR_C_Msk      = 1 << MPU_RASR_C_Pos          // MPU RASR: ATTRS.C Mask
    71  	MPU_RASR_B_Pos      = 16                           // MPU RASR: ATTRS.B Position
    72  	MPU_RASR_B_Msk      = 1 << MPU_RASR_B_Pos          // MPU RASR: ATTRS.B Mask
    73  	MPU_RASR_SRD_Pos    = 8                            // MPU RASR: Sub-Region Disable Position
    74  	MPU_RASR_SRD_Msk    = 0xFF << MPU_RASR_SRD_Pos     // MPU RASR: Sub-Region Disable Mask
    75  	MPU_RASR_SIZE_Pos   = 1                            // MPU RASR: Region Size Field Position
    76  	MPU_RASR_SIZE_Msk   = 0x1F << MPU_RASR_SIZE_Pos    // MPU RASR: Region Size Field Mask
    77  	MPU_RASR_ENABLE_Pos = 0                            // MPU RASR: Region enable bit Position
    78  	MPU_RASR_ENABLE_Msk = 1                            // MPU RASR: Region enable bit Disable Mask
    79  )
    80  
    81  const (
    82  	SCB_DCISW_WAY_Pos = 30                         // SCB DCISW: Way Position
    83  	SCB_DCISW_WAY_Msk = 3 << SCB_DCISW_WAY_Pos     // SCB DCISW: Way Mask
    84  	SCB_DCISW_SET_Pos = 5                          // SCB DCISW: Set Position
    85  	SCB_DCISW_SET_Msk = 0x1FF << SCB_DCISW_SET_Pos // SCB DCISW: Set Mask
    86  )
    87  
    88  const (
    89  	SCB_DCCISW_WAY_Pos = 30                          // SCB DCCISW: Way Position
    90  	SCB_DCCISW_WAY_Msk = 3 << SCB_DCCISW_WAY_Pos     // SCB DCCISW: Way Mask
    91  	SCB_DCCISW_SET_Pos = 5                           // SCB DCCISW: Set Position
    92  	SCB_DCCISW_SET_Msk = 0x1FF << SCB_DCCISW_SET_Pos // SCB DCCISW: Set Mask
    93  )
    94  
    95  const (
    96  	RGNSZ_32B   RegionSize = 0x04 // MPU Region Size 32 Bytes
    97  	RGNSZ_64B   RegionSize = 0x05 // MPU Region Size 64 Bytes
    98  	RGNSZ_128B  RegionSize = 0x06 // MPU Region Size 128 Bytes
    99  	RGNSZ_256B  RegionSize = 0x07 // MPU Region Size 256 Bytes
   100  	RGNSZ_512B  RegionSize = 0x08 // MPU Region Size 512 Bytes
   101  	RGNSZ_1KB   RegionSize = 0x09 // MPU Region Size 1 KByte
   102  	RGNSZ_2KB   RegionSize = 0x0A // MPU Region Size 2 KBytes
   103  	RGNSZ_4KB   RegionSize = 0x0B // MPU Region Size 4 KBytes
   104  	RGNSZ_8KB   RegionSize = 0x0C // MPU Region Size 8 KBytes
   105  	RGNSZ_16KB  RegionSize = 0x0D // MPU Region Size 16 KBytes
   106  	RGNSZ_32KB  RegionSize = 0x0E // MPU Region Size 32 KBytes
   107  	RGNSZ_64KB  RegionSize = 0x0F // MPU Region Size 64 KBytes
   108  	RGNSZ_128KB RegionSize = 0x10 // MPU Region Size 128 KBytes
   109  	RGNSZ_256KB RegionSize = 0x11 // MPU Region Size 256 KBytes
   110  	RGNSZ_512KB RegionSize = 0x12 // MPU Region Size 512 KBytes
   111  	RGNSZ_1MB   RegionSize = 0x13 // MPU Region Size 1 MByte
   112  	RGNSZ_2MB   RegionSize = 0x14 // MPU Region Size 2 MBytes
   113  	RGNSZ_4MB   RegionSize = 0x15 // MPU Region Size 4 MBytes
   114  	RGNSZ_8MB   RegionSize = 0x16 // MPU Region Size 8 MBytes
   115  	RGNSZ_16MB  RegionSize = 0x17 // MPU Region Size 16 MBytes
   116  	RGNSZ_32MB  RegionSize = 0x18 // MPU Region Size 32 MBytes
   117  	RGNSZ_64MB  RegionSize = 0x19 // MPU Region Size 64 MBytes
   118  	RGNSZ_128MB RegionSize = 0x1A // MPU Region Size 128 MBytes
   119  	RGNSZ_256MB RegionSize = 0x1B // MPU Region Size 256 MBytes
   120  	RGNSZ_512MB RegionSize = 0x1C // MPU Region Size 512 MBytes
   121  	RGNSZ_1GB   RegionSize = 0x1D // MPU Region Size 1 GByte
   122  	RGNSZ_2GB   RegionSize = 0x1E // MPU Region Size 2 GBytes
   123  	RGNSZ_4GB   RegionSize = 0x1F // MPU Region Size 4 GBytes
   124  )
   125  
   126  const (
   127  	PERM_NONE AccessPerms = 0 // MPU Access Permission no access
   128  	PERM_PRIV AccessPerms = 1 // MPU Access Permission privileged access only
   129  	PERM_URO  AccessPerms = 2 // MPU Access Permission unprivileged access read-only
   130  	PERM_FULL AccessPerms = 3 // MPU Access Permission full access
   131  	PERM_PRO  AccessPerms = 5 // MPU Access Permission privileged access read-only
   132  	PERM_RO   AccessPerms = 6 // MPU Access Permission read-only access
   133  )
   134  
   135  const (
   136  	EXTN_NORMAL Extension = 0
   137  	EXTN_DEVICE Extension = 2
   138  )
   139  
   140  func (mpu *MPU_Type) Enable(enable bool) {
   141  	if enable {
   142  		mpu.CTRL.Set(MPU_CTRL_PRIVDEFENA_Msk | MPU_CTRL_ENABLE_Msk)
   143  		SystemControl.SHCSR.SetBits(SCB_SHCSR_MEMFAULTENA_Msk)
   144  		arm.Asm("dsb 0xF")
   145  		arm.Asm("isb 0xF")
   146  		enableDcache(true)
   147  		enableIcache(true)
   148  	} else {
   149  		enableIcache(false)
   150  		enableDcache(false)
   151  		arm.Asm("dmb 0xF")
   152  		SystemControl.SHCSR.ClearBits(SCB_SHCSR_MEMFAULTENA_Msk)
   153  		mpu.CTRL.ClearBits(MPU_CTRL_ENABLE_Msk)
   154  	}
   155  }
   156  
   157  // MPU Region Base Address Register value
   158  func (mpu *MPU_Type) SetRBAR(region uint32, baseAddress uint32) {
   159  	mpu.RBAR.Set((baseAddress & MPU_RBAR_ADDR_Msk) |
   160  		(region & MPU_RBAR_REGION_Msk) | MPU_RBAR_VALID_Msk)
   161  }
   162  
   163  // MPU Region Attribute and Size Register value
   164  func (mpu *MPU_Type) SetRASR(size RegionSize, access AccessPerms, ext Extension, exec, share, cache, buffer, disable bool) {
   165  	boolBit := func(b bool) uint32 {
   166  		if b {
   167  			return 1
   168  		}
   169  		return 0
   170  	}
   171  	attr := ((uint32(ext) << MPU_RASR_TEX_Pos) & MPU_RASR_TEX_Msk) |
   172  		((boolBit(share) << MPU_RASR_S_Pos) & MPU_RASR_S_Msk) |
   173  		((boolBit(cache) << MPU_RASR_C_Pos) & MPU_RASR_C_Msk) |
   174  		((boolBit(buffer) << MPU_RASR_B_Pos) & MPU_RASR_B_Msk)
   175  	mpu.RASR.Set(((boolBit(!exec) << MPU_RASR_XN_Pos) & MPU_RASR_XN_Msk) |
   176  		((uint32(access) << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk) |
   177  		(attr & (MPU_RASR_TEX_Msk | MPU_RASR_S_Msk | MPU_RASR_C_Msk | MPU_RASR_B_Msk)) |
   178  		((boolBit(disable) << MPU_RASR_SRD_Pos) & MPU_RASR_SRD_Msk) |
   179  		((uint32(size) << MPU_RASR_SIZE_Pos) & MPU_RASR_SIZE_Msk) |
   180  		MPU_RASR_ENABLE_Msk)
   181  }
   182  
   183  func enableIcache(enable bool) {
   184  	if enable != SystemControl.CCR.HasBits(SCB_CCR_IC_Msk) {
   185  		if enable {
   186  			arm.Asm("dsb 0xF")
   187  			arm.Asm("isb 0xF")
   188  			SystemControl.ICIALLU.Set(0)
   189  			arm.Asm("dsb 0xF")
   190  			arm.Asm("isb 0xF")
   191  			SystemControl.CCR.SetBits(SCB_CCR_IC_Msk)
   192  			arm.Asm("dsb 0xF")
   193  			arm.Asm("isb 0xF")
   194  		} else {
   195  			arm.Asm("dsb 0xF")
   196  			arm.Asm("isb 0xF")
   197  			SystemControl.CCR.ClearBits(SCB_CCR_IC_Msk)
   198  			SystemControl.ICIALLU.Set(0)
   199  			arm.Asm("dsb 0xF")
   200  			arm.Asm("isb 0xF")
   201  		}
   202  	}
   203  }
   204  
   205  var (
   206  	dcacheCcsidr volatile.Register32
   207  	dcacheSets   volatile.Register32
   208  	dcacheWays   volatile.Register32
   209  )
   210  
   211  func enableDcache(enable bool) {
   212  	if enable != SystemControl.CCR.HasBits(SCB_CCR_DC_Msk) {
   213  		if enable {
   214  			SystemControl.CSSELR.Set(0)
   215  			arm.Asm("dsb 0xF")
   216  			ccsidr := SystemControl.CCSIDR.Get()
   217  			sets := (ccsidr & SCB_CCSIDR_NUMSETS_Msk) >> SCB_CCSIDR_NUMSETS_Pos
   218  			for sets != 0 {
   219  				ways := (ccsidr & SCB_CCSIDR_ASSOCIATIVITY_Msk) >> SCB_CCSIDR_ASSOCIATIVITY_Pos
   220  				for ways != 0 {
   221  					SystemControl.DCISW.Set(
   222  						((sets << SCB_DCISW_SET_Pos) & SCB_DCISW_SET_Msk) |
   223  							((ways << SCB_DCISW_WAY_Pos) & SCB_DCISW_WAY_Msk))
   224  					ways--
   225  				}
   226  				sets--
   227  			}
   228  			arm.Asm("dsb 0xF")
   229  			SystemControl.CCR.SetBits(SCB_CCR_DC_Msk)
   230  			arm.Asm("dsb 0xF")
   231  			arm.Asm("isb 0xF")
   232  		} else {
   233  			SystemControl.CSSELR.Set(0)
   234  			arm.Asm("dsb 0xF")
   235  			SystemControl.CCR.ClearBits(SCB_CCR_DC_Msk)
   236  			arm.Asm("dsb 0xF")
   237  			dcacheCcsidr.Set(SystemControl.CCSIDR.Get())
   238  			dcacheSets.Set((dcacheCcsidr.Get() & SCB_CCSIDR_NUMSETS_Msk) >> SCB_CCSIDR_NUMSETS_Pos)
   239  			for dcacheSets.Get() != 0 {
   240  				dcacheWays.Set((dcacheCcsidr.Get() & SCB_CCSIDR_ASSOCIATIVITY_Msk) >> SCB_CCSIDR_ASSOCIATIVITY_Pos)
   241  				for dcacheWays.Get() != 0 {
   242  					SystemControl.DCCISW.Set(
   243  						((dcacheSets.Get() << SCB_DCCISW_SET_Pos) & SCB_DCCISW_SET_Msk) |
   244  							((dcacheWays.Get() << SCB_DCCISW_WAY_Pos) & SCB_DCCISW_WAY_Msk))
   245  					dcacheWays.Set(dcacheWays.Get() - 1)
   246  				}
   247  				dcacheSets.Set(dcacheSets.Get() - 1)
   248  			}
   249  			arm.Asm("dsb 0xF")
   250  			arm.Asm("isb 0xF")
   251  		}
   252  	}
   253  }