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

     1  // NXP Ultra Secured Digital Host Controller (uSDHC) driver
     2  // https://github.com/usbarmory/tamago
     3  //
     4  // IP: https://www.mobiveil.com/esdhc/
     5  //
     6  // Copyright (c) WithSecure Corporation
     7  // https://foundry.withsecure.com
     8  //
     9  // Use of this source code is governed by the license
    10  // that can be found in the LICENSE file.
    11  
    12  package usdhc
    13  
    14  import (
    15  	"bytes"
    16  	"encoding/binary"
    17  )
    18  
    19  // ADMA constants
    20  const (
    21  	ATTR_VALID = 0
    22  	ATTR_END   = 1
    23  	ATTR_INT   = 2
    24  	ATTR_ACT   = 4
    25  
    26  	ACT_TRANSFER = 0b10
    27  	ACT_LINK     = 0b11
    28  
    29  	DMASEL_NONE  = 0b00
    30  	DMASEL_ADMA1 = 0b01
    31  	DMASEL_ADMA2 = 0b10
    32  
    33  	ADMA_BD_MAX_LENGTH = 65532
    34  )
    35  
    36  // ADMABufferDescriptor implements p3964 58.4.2.4.1 ADMA Concept and Descriptor Format, IMX6ULLRM.
    37  type ADMABufferDescriptor struct {
    38  	Attribute uint8
    39  	res       uint8
    40  	Length    uint16
    41  	Address   uint32
    42  
    43  	next *ADMABufferDescriptor
    44  }
    45  
    46  // Init initializes an ADMA2 buffer descriptor.
    47  func (bd *ADMABufferDescriptor) Init(addr uint, size int) {
    48  	b := bd
    49  
    50  	for size > 0 {
    51  		if size <= ADMA_BD_MAX_LENGTH {
    52  			b.Attribute = ACT_TRANSFER<<ATTR_ACT | 1<<ATTR_END | 1<<ATTR_VALID
    53  			b.Length = uint16(size)
    54  			b.Address = uint32(addr)
    55  			break
    56  		} else {
    57  			b.Attribute = ACT_TRANSFER<<ATTR_ACT | 1<<ATTR_VALID
    58  			b.Length = uint16(ADMA_BD_MAX_LENGTH)
    59  			b.Address = uint32(addr)
    60  
    61  			addr += ADMA_BD_MAX_LENGTH
    62  			size -= ADMA_BD_MAX_LENGTH
    63  
    64  			b.next = &ADMABufferDescriptor{}
    65  			b = b.next
    66  		}
    67  	}
    68  }
    69  
    70  // Bytes converts the descriptor structure to byte array format.
    71  func (bd *ADMABufferDescriptor) Bytes() []byte {
    72  	buf := new(bytes.Buffer)
    73  
    74  	b := bd
    75  
    76  	for b != nil {
    77  		binary.Write(buf, binary.LittleEndian, b.Attribute)
    78  		binary.Write(buf, binary.LittleEndian, b.res)
    79  		binary.Write(buf, binary.LittleEndian, b.Length)
    80  		binary.Write(buf, binary.LittleEndian, b.Address)
    81  
    82  		b = b.next
    83  	}
    84  
    85  	return buf.Bytes()
    86  }