8ef42a4cf9a78bbb2c7728fc1968c2d8b0026d66
[fw/openocd] / contrib / loaders / flash / stm32 / stm32h7x.S
1 /***************************************************************************
2  *   Copyright (C) 2017 by STMicroelectronics                              *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.                                        *
17  ***************************************************************************/
18
19         .text
20         .syntax unified
21         .cpu cortex-m4
22         .thumb
23
24 /*
25  * Code limitations:
26  * The workarea must have size multiple of 4 bytes, since R/W
27  * operations are all at 32 bits.
28  * The workarea must be big enough to contain rp, wp and data, thus the minimum
29  * workarea size is: min_wa_size = sizeof(rp, wp, data) = 4 + 4 + sizeof(data).
30  *  - for 0x450 devices: sizeof(data) = 32 bytes, thus min_wa_size = 40 bytes.
31  *  - for 0x480 devices: sizeof(data) = 16 bytes, thus min_wa_size = 24 bytes.
32  * To benefit from concurrent host write-to-buffer and target
33  * write-to-flash, the workarea must be way bigger than the minimum.
34  *
35  * To avoid confusions the write word size is got from .block_size member of
36  * struct stm32h7x_part_info defined in stm32h7x.c
37 */
38
39 /*
40  * Params :
41  * r0 = workarea start, status (out)
42  * r1 = workarea end
43  * r2 = target address
44  * r3 = count (of write words)
45  * r4 = size of write word
46  * r5 = flash reg base
47  *
48  * Clobbered:
49  * r6 - rp
50  * r7 - wp, status, tmp
51  * r8 - loop index, tmp
52  */
53
54 #define STM32_FLASH_CR_OFFSET   0x0C    /* offset of CR register in FLASH struct */
55 #define STM32_FLASH_SR_OFFSET   0x10    /* offset of SR register in FLASH struct */
56 #define STM32_CR_PROG                   0x00000002      /* PG */
57 #define STM32_SR_QW_MASK                0x00000004      /* QW */
58 #define STM32_SR_ERROR_MASK             0x07ee0000      /* DBECCERR | SNECCERR | RDSERR | RDPERR | OPERR
59                                                                                            | INCERR | STRBERR | PGSERR | WRPERR */
60
61         .thumb_func
62         .global _start
63 _start:
64         ldr             r6, [r0, #4]            /* read rp */
65
66 wait_fifo:
67         ldr             r7, [r0, #0]            /* read wp */
68         cbz             r7, exit                        /* abort if wp == 0, status = 0 */
69         subs    r7, r7, r6                      /* number of bytes available for read in r7 */
70         ittt    mi                                      /* if wrapped around */
71         addmi   r7, r1                          /* add size of buffer */
72         submi   r7, r0
73         submi   r7, #8
74         cmp             r7, r4                          /* wait until data buffer is full */
75         bcc             wait_fifo
76
77         mov             r7, #STM32_CR_PROG
78         str             r7, [r5, #STM32_FLASH_CR_OFFSET]
79
80         mov             r8, #4
81         udiv    r8, r4, r8                      /* number of words is size of write word divided by 4*/
82 write_flash:
83         dsb
84         ldr             r7, [r6], #0x04         /* read one word from src, increment ptr */
85         str             r7, [r2], #0x04         /* write one word to dst, increment ptr */
86         dsb
87         cmp             r6, r1                          /* if rp >= end of buffer ... */
88         it              cs
89         addcs   r6, r0, #8                      /* ... then wrap at buffer start */
90         subs    r8, r8, #1                      /* decrement loop index */
91         bne             write_flash                     /* loop if not done */
92
93 busy:
94         ldr             r7, [r5, #STM32_FLASH_SR_OFFSET]
95         tst             r7, #STM32_SR_QW_MASK
96         bne             busy                            /* operation in progress, wait ... */
97
98         ldr             r8, =STM32_SR_ERROR_MASK
99         tst             r7, r8
100         bne             error                           /* fail... */
101
102         str             r6, [r0, #4]            /* store rp */
103         subs    r3, r3, #1                      /* decrement count */
104         bne             wait_fifo                       /* loop if not done */
105         b               exit
106
107 error:
108         movs    r8, #0
109         str             r8, [r0, #4]            /* set rp = 0 on error */
110
111 exit:
112         mov             r0, r7                          /* return status in r0 */
113         bkpt    #0x00
114
115         .pool