##############################################################################
## This file is part of 'SLAC Firmware Standard Library'.
## It is subject to the license terms in the LICENSE.txt file found in the 
## top-level directory of this distribution and at: 
##    https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html. 
## No part of 'SLAC Firmware Standard Library', including this file, 
## may be copied, modified, propagated, or distributed except according to 
## the terms contained in the LICENSE.txt file.
##############################################################################
#schemaversion 3.0.0
#once JesdRx.yaml
JesdRx: &JesdRx
  class: MMIODev
  configPrio: 1
  description: JESD RX Module
  size: 0x200
  metadata:
    numRxLanes: &numRxLanes 6
  #########
  children:
    #########################################################
    Enable:
      at:
        offset: 0x00
      class: IntField
      name: Enable
      sizeBits: *numRxLanes
      mode: RW
      description: "Enable mask. Example: 0x3F Enable ln0 to ln5."
    #########################################################    
    SysrefDelay: 
      at:
        offset: 0x04
      class: IntField
      name: SysrefDelay
      sizeBits: 5
      mode: RW
      description: "Sets the system reference delay in clock cycles. Use if you want to reduce the latency (The latency is indicated by ElBuffLatency status). "
    #########################################################    
    SubClass:    
      at:
        offset: 0x10
      class: IntField
      name: SubClass
      sizeBits: 1
      lsBit: 0
      mode: RW
      description: "Jesd204b SubClass. 0 - For designs without sysref (no fixed latency). 1 - Fixed latency."
    #########################################################    
    ReplaceEnable:  
      at:
        offset: 0x10
      class: IntField
      name: ReplaceEnable
      sizeBits: 1
      lsBit: 1
      mode: RW
      enums:
        - name: Disabled
          class: Enum
          value: 0
        - name:  Enabled
          class: Enum
          value: 1
      description: "ReplaceEnable. Replace the control characters with data. (Should be 1 use 0 only for debug)."
    #########################################################    
    ResetGTs:  
      at:
        offset: 0x10
      class: IntField
      name: ResetGTs
      sizeBits: 1
      lsBit: 2
      mode: RW
      description: "ResetGTs. Request reset of the GT modules."
    #########################################################     
    ClearErrors:   
      at:
        offset: 0x10
      class: IntField
      name: ClearErrors
      sizeBits: 1
      lsBit: 3
      mode: RW
      description: "Clear Jesd Errors and reset the status counters."
    #########################################################    
    InvertSync: 
      at:
        offset: 0x10
      class: IntField
      name: InvertSync
      sizeBits: 1
      lsBit: 4
      mode: RW
      enums:
        - name: Regular
          class: Enum
          value: 0
        - name: Inverted
          class: Enum
          value: 1
      description: "Invert Sync. Sync output has to be inverted in some systems depending on signal polarities on the PCB."
    #########################################################    
    ScrambleEnable: 
      at:
        offset: 0x10
      class: IntField
      name: ScrambleEnable
      sizeBits: 1
      lsBit: 5
      mode: RW
      enums:
        - name: Disabled
          class: Enum
          value: 0
        - name: Enabled
          class: Enum
          value: 1
      description: "ScrambleEnable. Enable data scrambling (More info in Jesd204b standard)."
    #########################################################
    LinkErrMask:   
      at:
        offset: 0x14
      class: IntField
      name: LinkErrMask
      sizeBits: 6
      mode: RW
      description: "Mask Enable the errors that are required to brake the link. bit 5-0: positionErr - s_bufOvf - s_bufUnf - dispErr - decErr - s_alignErr"
    #########################################################      
    GTReady:
      at:
        offset: 0x040
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: GTReady
      sizeBits: 1
      lsBit: 0
      mode: RO
      description: "GT Ready. Jesd clock ok PLLs are locked and GT is ready to receive data."
    #########################################################    
    DataValid:        
      at:
        offset: 0x040
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: DataValid
      sizeBits: 1
      lsBit: 1
      mode: RO
      description: "Jesd Data Valid. Goes high after the code synchronisation and ILAS sequence is complete (More info in Jesd204b standard)."
    #########################################################     
    AlignErr:       
      at:
        offset: 0x040
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: AlignErr
      sizeBits: 1
      lsBit: 2
      mode: RO
      description: "Jesd Character Alignment Error. The control characters in the data are missaligned. This error will trigger JESD re-synchronisation."
    #########################################################    
    nSync:        
      at:
        offset: 0x040
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: nSync
      sizeBits: 1
      lsBit: 3
      mode: RO
      description: "Synchronisation request. 0 - Not synchronised. 1 - Indicades that code group synchronisation has been completed."
    #########################################################    
    RxBuffUfl:        
      at:
        offset: 0x040
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: RxBuffUfl
      sizeBits: 1
      lsBit: 4
      mode: RO
      description: "Jesd sync fifo buffer undeflow. This error will trigger JESD re-synchronisation."
    #########################################################    
    RxBuffOfl:        
      at:
        offset: 0x040
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: RxBuffOfl
      sizeBits: 1
      lsBit: 5
      mode: RO
      description: "Jesd elastic buffer overflow. This error will trigger JESD re-synchronisation."
    #########################################################    
    PositionErr:        
      at:
        offset: 0x040
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: PositionErr
      sizeBits: 1
      lsBit: 6
      mode: RO
      description: "The position of K28.5 character during code group synchronisation is wrong. This error will trigger JESD re-synchronisation."
    #########################################################    
    RxEnabled:        
      at:
        offset: 0x040
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: RxEnabled
      sizeBits: 1
      lsBit: 7
      mode: RO
      description: "Rx Lane Enabled. Indicates if the lane had been enabled in configuration."
    #########################################################     
    SysRefDetected:       
      at:
        offset: 0x041
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: SysRefDetected
      sizeBits: 1
      lsBit: 0
      mode: RO
      description: "System Reference input has been Detected."
    #########################################################    
    CommaDetected:        
      at:
        offset: 0x041
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: CommaDetected
      sizeBits: 1
      lsBit: 1
      mode: RO
      description: "The K28.5 characters detected in the serial stream. "
    #########################################################     
    DisparityErr:       
      at:
        offset: 0x041
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: DisparityErr
      sizeBits: 4
      lsBit: 2
      mode: RO
      description:  "Latched High when the data byte on RXDATA arrives with the wrong disparity. Indicates bad serial connection (Check HW)."
    #########################################################    
    NotInTableErr:        
      at:
        offset: 0x041
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: NotInTableErr
      sizeBits: 4
      lsBit: 6
      mode: RO
      description: "NotInTableErr. When GT decoder received s 10-bit character that cannot be mapped into a valid 8B/10B character. Indicates bad serial connection (Check HW)."
    #########################################################    
    ElBuffLatency:        
      at:
        offset: 0x042
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: ElBuffLatency
      sizeBits: 8
      lsBit: 2
      mode: RO
      description: "Jesd204b elastic buffer latency in c-c. Can be adjusted by Sysref delay."
    #########################################################
    CDRStable:        
      at:
        offset: 0x043
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: CDRStable
      sizeBits: 1
      lsBit: 2
      mode: RO
      description: "GT CDR stable status."
    #########################################################     
    ThresholdLow:     
      at:
        offset: 0xC0
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: ThresholdLow
      sizeBits: 16
      mode: RW
      description: "Threshold_Low. Debug funtionality. Threshold for generating a digital signal from the ADC data."
    #########################################################    
    ThresholdHigh:   
      at:
        offset: 0xC2
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: ThresholdHigh
      sizeBits: 16
      mode: RW
      description: "Threshold_High. Debug funtionality. Threshold for generating a digital signal from the ADC data."
    #########################################################    
    StatusValidCnt:   
      at:
        offset: 0x100
        stride: 4
        nelms: *numRxLanes
      class: IntField
      name: StatusValidCnt
      sizeBits: 32
      mode: RO
      description: "StatusValidCnt. Shows stability of JESD lanes. Counts number of JESD re-syncronisations."
    #########################################################    
    ClearRxErrors:
      name: ClearRxErrors
      class: SequenceCommand
      at:
        offset: 0x0
      description: Clear the registered errors of all RX lanes
      sequence:
      - entry: ClearErrors
        value: 0x1
      - entry: ClearErrors
        value: 0x0
    #########################################################
    ResetRxGTs:
      name: ResetRxGTs
      class: SequenceCommand
      at:
        offset: 0x0
      description: Toggle the reset of all RX MGTs
      sequence:
      - entry: ResetGTs
        value: 0x1
      - entry: ResetGTs
        value: 0x0
    #########################################################