github.com/usbarmory/tamago@v0.0.0-20240508072735-8612bbe1e454/soc/nxp/csu/csl.go (about)

     1  // NXP i.MX Central Security Unit (CSU) driver
     2  // https://github.com/usbarmory/tamago
     3  //
     4  // Copyright (c) WithSecure Corporation
     5  // https://foundry.withsecure.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
    11  
    12  import (
    13  	"errors"
    14  
    15  	"github.com/usbarmory/tamago/internal/reg"
    16  )
    17  
    18  func checkArgs(periph int, slave int) (err error) {
    19  	if periph < CSL_MIN || periph > CSL_MAX {
    20  		return errors.New("peripheral index out of range")
    21  	}
    22  
    23  	if slave < 0 || slave > 1 {
    24  		return errors.New("slave index out of range")
    25  	}
    26  
    27  	return
    28  }
    29  
    30  // GetSecurityLevel returns the config security level (CSL) registers for a
    31  // peripheral slave. The lock return value indicates whether the CSL is locked
    32  // for changes until the next power cycle.
    33  func (hw *CSU) GetSecurityLevel(periph int, slave int) (csl uint8, lock bool, err error) {
    34  	if err = checkArgs(periph, slave); err != nil {
    35  		return
    36  	}
    37  
    38  	val := reg.Read(hw.csl0 + uint32(4*periph))
    39  	csl = uint8((val >> (CSL_S2 * slave)) & 0xff)
    40  
    41  	if uint8((val>>(CSL_S1_LOCK+CSL_S2*slave))&1) == 1 {
    42  		lock = true
    43  	}
    44  
    45  	return
    46  }
    47  
    48  // SetSecurityLevel sets the config security level (CSL) registers for a
    49  // peripheral slave. The lock argument controls whether the CSL is locked for
    50  // changes until the next power cycle.
    51  func (hw *CSU) SetSecurityLevel(periph int, slave int, csl uint8, lock bool) (err error) {
    52  	if err = checkArgs(periph, slave); err != nil {
    53  		return
    54  	}
    55  
    56  	addr := hw.csl0 + uint32(4*periph)
    57  
    58  	reg.SetN(addr, CSL_S2*slave, 0xff, uint32(csl))
    59  
    60  	if lock {
    61  		reg.Set(addr, CSL_S1_LOCK+CSL_S2*slave)
    62  	}
    63  
    64  	return
    65  }