1/*******************************************************************************
2 * Copyright 2021 Microchip Corporation.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * MPFS HAL Embedded Software
7 *
8 */
9
10/***************************************************************************
11 * @file mss_utils.S
12 * @author Microchip-FPGA Embedded Systems Solutions
13 * @brief utilities used by mpfs-hal startup code
14 *
15 */
16  .section .text.init,"ax", %progbits
17  .align 3
18
19/***********************************************************************************
20 *
21 * pdma_transfer
22 * Only used by the mpfs hal. App code uses the provided driver.
23 *
24 *   a0 = dest
25 *   a1 = src
26 *   a2 = length
27 *   a3 = PDMA Base Address - 0x3000000 + (0x01000 * PDMA_CHANNEL)
28 */
29    .globl  pdma_transfer
30    .type   pdma_transfer, @function
31pdma_transfer:
32    mv  t1,a0
33    mv t0, a3           // Base address
34    li t1, 1
35    sw t1, 0(t0)        // claim
36    li t1, 0
37    sw t1, 4(t0)        // read[31:28]/write[27:24] size 0=>1byte, 1 =>2 bytes etx
38    mv t1, a2           // SIZE
39    sd t1, 8(t0)        // bytes
40    mv t1, a0           // dest address
41    sd t1, 16(t0)       // dest
42    mv t1, a1           // source address
43    sd t1, 24(t0)       // src
44    li t1, 0xff000000
45    sw t1, 4(t0)        // full speed copy
46    li t1, 3
47    sw t1, 0(t0)        // start transfer
48    fence
49    ret
50
51/***********************************************************************************
52 *
53 * pdma_transfer_complete
54 * Loops until transfer complete
55 * Only used by the mpfs hal. App code uses the provided driver.
56 *
57 *    a0 = PDMA Base Address - 0x3000000 + (0x01000 * PDMA_CHANNEL)
58 */
59    //
60    .globl  pdma_transfer_complete
61    .type   pdma_transfer_complete, @function
62pdma_transfer_complete:
63    mv t0, a0           // Base address
641: // wait for completion
65    lw t1, 0(t0)
66    andi t1, t1, 2
67    bnez t1, 1b
68    // release DMA
69    sw zero, 0(t0)
70    ret
71
72
73 /***********************************************************************************
74 *
75 * memfill() - fills memory, alternate to lib function when not available
76 */
77    // memfill helper function:
78    //  a0 = dest
79    //  a1 = value to fill
80    //  a2 = length
81    .globl  memfill
82    .type   memfill, @function
83memfill:
84    mv  t1,a0
85    mv  t2,a1
86    beqz    a2,2f
871:
88    sb  t2,0(t1)
89    addi    a2,a2,-1
90    addi    t1,t1,1
91    bnez    a2,1b
922:
93    ret
94
95/***********************************************************************************
96 *
97 * The following config_copy() symbol overrides the weak symbol in the HAL and does
98 * a safe copy of HW config data
99 */
100    // config_copy helper function:
101    //  a0 = dest
102    //  a1 = src
103    //  a2 = length
104    .globl  config_copy
105    .type   config_copy, @function
106config_copy:
107    mv  t1,a0
108    beqz    a2,2f
1091:
110    lb  t2,0(a1)
111    sb  t2,0(t1)
112    addi    a2,a2,-1
113    addi    t1,t1,1
114    addi    a1,a1,1
115    bnez    a2,1b
1162:
117    ret
118
119 /***********************************************************************************
120 *
121 * config_16_copy () Copies a word at a time, used when copying to contigous registers
122 */
123    // config_16_copy helper function:
124    //  a0 = dest
125    //  a1 = src
126    //  a2 = length
127    .globl  config_16_copy
128    .type   config_16_copy, @function
129config_16_copy:
130    mv  t1,a0
131    beqz    a2,2f
1321:
133    lh  t2,0(a1)
134    sh  t2,0(t1)
135    addi    a2,a2,-2
136    addi    t1,t1,2
137    addi    a1,a1,2
138    bnez    a2,1b
1392:
140    ret
141
142/***********************************************************************************
143 *
144 * config_32_copy () Copies a word at a time, used when copying to contigous registers
145 */
146    // config_copy helper function:
147    //  a0 = dest
148    //  a1 = src
149    //  a2 = length
150    .globl  config_32_copy
151    .type   config_32_copy, @function
152config_32_copy:
153    mv  t1,a0
154    beqz    a2,2f
1551:
156    lw  t2,0(a1)
157    sw  t2,0(t1)
158    addi    a2,a2,-4
159    addi    t1,t1,4
160    addi    a1,a1,4
161    bnez    a2,1b
1622:
163    ret
164
165 /***********************************************************************************
166 *
167 * config_64_copy - copying using 64 bit loads, addresses must be on 64 bit boundary
168 */
169    // config_64_copy helper function:
170    //  a0 = dest
171    //  a1 = src
172    //  a2 = length
173    .globl  config_64_copy
174    .type   config_64_copy, @function
175config_64_copy:
176    mv  t1,a0
177    beqz    a2,2f
1781:
179    ld  t2,0(a1)
180    sd  t2,0(t1)
181    addi    a2,a2,-8
182    addi    t1,t1,8
183    addi    a1,a1,8
184    bnez    a2,1b
1852:
186    ret
187
188
189