github.com/usbarmory/tamago@v0.0.0-20240508072735-8612bbe1e454/soc/sifive/clint/clint.go (about)

     1  // SiFive Core-Local Interruptor (CLINT) 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 clint implements a driver for SiFive Core-Local Interruptor (CLINT)
    11  // block adopting the following reference specifications:
    12  //   - FU540C00RM - SiFive FU540-C000 Manual - v1p4 2021/03/25
    13  //
    14  // This package is only meant to be used with `GOOS=tamago GOARCH=riscv64` as
    15  // supported by the TamaGo framework for bare metal Go on RISC-V SoCs, see
    16  // https://github.com/usbarmory/tamago.
    17  package clint
    18  
    19  import (
    20  	_ "unsafe"
    21  
    22  	"github.com/usbarmory/tamago/internal/reg"
    23  )
    24  
    25  // CLINT registers
    26  const (
    27  	MTIME = 0xbff8
    28  )
    29  
    30  // CLINT represents a Core-Local Interruptor (CLINT) instance.
    31  type CLINT struct {
    32  	// Base register
    33  	Base uint64
    34  	// CPU real time clock
    35  	RTCCLK uint64
    36  	// Timer offset in nanoseconds
    37  	TimerOffset int64
    38  }
    39  
    40  // Mtime returns the number of cycles counted from the RTCCLK input.
    41  func (hw *CLINT) Mtime() uint64 {
    42  	return reg.Read64(hw.Base + MTIME)
    43  }
    44  
    45  // SetTimer sets the timer to the argument nanoseconds value.
    46  func (hw *CLINT) SetTimer(t int64) {
    47  	hw.TimerOffset = t - hw.Nanotime()
    48  }