github.com/f-secure-foundry/tamago@v0.0.0-20220307101044-d73fcdd7f11b/soc/imx6/csu/csu.go (about)

     1  // i.MX Central Security Unit (CSU) driver
     2  // https://github.com/f-secure-foundry/tamago
     3  //
     4  // Copyright (c) F-Secure Corporation
     5  // https://foundry.f-secure.com
     6  //
     7  // Use of this source code is governed by the license
     8  // that can be found in the LICENSE file.
     9  
    10  // Package csu implements a driver for the Central Security Unit (CSU) included
    11  // in NXP i.MX6ULL/i.MX6ULZ SoCs.
    12  //
    13  // This package is only meant to be used with `GOOS=tamago GOARCH=arm` as
    14  // supported by the TamaGo framework for bare metal Go on ARM SoCs, see
    15  // https://github.com/f-secure-foundry/tamago.
    16  package csu
    17  
    18  import (
    19  	"errors"
    20  
    21  	"github.com/f-secure-foundry/tamago/internal/reg"
    22  	"github.com/f-secure-foundry/tamago/soc/imx6"
    23  )
    24  
    25  // CSU registers
    26  const (
    27  	CSU_BASE = 0x021c0000
    28  
    29  	CSU_CSL0 = CSU_BASE
    30  	CSU_SA   = CSU_BASE + 0x218
    31  )
    32  
    33  // Init initializes the Central Security Unit (CSU).
    34  func Init() {
    35  	// enable clock
    36  	reg.SetN(imx6.CCM_CCGR1, imx6.CCGRx_CG14, 0b11, 0b11)
    37  }
    38  
    39  // GetAccess returns the security access (SA) for one of the 16 masters IDs.
    40  // The lock return value indicates whether the SA is locked for changes until
    41  // the next power cycle.
    42  func GetAccess(id int) (secure bool, lock bool, err error) {
    43  	if id < SA_MIN || id > SA_MAX {
    44  		return false, false, errors.New("index out of range")
    45  	}
    46  
    47  	val := reg.Get(CSU_SA, id*2, 0b11)
    48  
    49  	if val&0b01 == 0 {
    50  		secure = true
    51  	}
    52  
    53  	if val&0b10 != 0 {
    54  		lock = true
    55  	}
    56  
    57  	return
    58  }
    59  
    60  // SetAccess configures the security access (SA) for one of the 16 masters IDs.
    61  // The lock argument controls whether the SA is locked for changes until the
    62  // next power cycle.
    63  func SetAccess(id int, secure bool, lock bool) (err error) {
    64  	if id < SA_MIN || id > SA_MAX {
    65  		return errors.New("index out of range")
    66  	}
    67  
    68  	if secure {
    69  		reg.Clear(CSU_SA, id*2)
    70  	} else {
    71  		reg.Set(CSU_SA, id*2)
    72  	}
    73  
    74  	if lock {
    75  		reg.Set(CSU_SA, id*2+1)
    76  	}
    77  
    78  	return
    79  }