2017-11-13 07:05:28 -06:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2017 by STMicroelectronics *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
.text
|
|
|
|
.syntax unified
|
2019-11-27 12:10:34 -06:00
|
|
|
.cpu cortex-m4
|
2017-11-13 07:05:28 -06:00
|
|
|
.thumb
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Code limitations:
|
|
|
|
* The workarea must have size multiple of 4 bytes, since R/W
|
|
|
|
* operations are all at 32 bits.
|
2020-07-12 17:32:49 -05:00
|
|
|
* The workarea must be big enough to contain rp, wp and data, thus the minimum
|
2020-02-06 17:12:48 -06:00
|
|
|
* workarea size is: min_wa_size = sizeof(rp, wp, data) = 4 + 4 + sizeof(data).
|
|
|
|
* - for 0x450 devices: sizeof(data) = 32 bytes, thus min_wa_size = 40 bytes.
|
|
|
|
* - for 0x480 devices: sizeof(data) = 16 bytes, thus min_wa_size = 24 bytes.
|
2017-11-13 07:05:28 -06:00
|
|
|
* To benefit from concurrent host write-to-buffer and target
|
|
|
|
* write-to-flash, the workarea must be way bigger than the minimum.
|
2020-02-06 17:12:48 -06:00
|
|
|
*
|
|
|
|
* To avoid confusions the write word size is got from .block_size member of
|
|
|
|
* struct stm32h7x_part_info defined in stm32h7x.c
|
|
|
|
*/
|
2017-11-13 07:05:28 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Params :
|
|
|
|
* r0 = workarea start, status (out)
|
|
|
|
* r1 = workarea end
|
|
|
|
* r2 = target address
|
2020-02-06 17:12:48 -06:00
|
|
|
* r3 = count (of write words)
|
|
|
|
* r4 = size of write word
|
|
|
|
* r5 = flash reg base
|
2017-11-13 07:05:28 -06:00
|
|
|
*
|
|
|
|
* Clobbered:
|
2020-02-06 17:12:48 -06:00
|
|
|
* r6 - rp
|
|
|
|
* r7 - wp, status, tmp
|
|
|
|
* r8 - loop index, tmp
|
2017-11-13 07:05:28 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define STM32_FLASH_CR_OFFSET 0x0C /* offset of CR register in FLASH struct */
|
|
|
|
#define STM32_FLASH_SR_OFFSET 0x10 /* offset of SR register in FLASH struct */
|
2020-02-06 17:12:48 -06:00
|
|
|
#define STM32_CR_PROG 0x00000002 /* PG */
|
2019-02-07 11:30:10 -06:00
|
|
|
#define STM32_SR_QW_MASK 0x00000004 /* QW */
|
|
|
|
#define STM32_SR_ERROR_MASK 0x07ee0000 /* DBECCERR | SNECCERR | RDSERR | RDPERR | OPERR
|
2017-11-13 07:05:28 -06:00
|
|
|
| INCERR | STRBERR | PGSERR | WRPERR */
|
|
|
|
|
2018-03-01 15:57:08 -06:00
|
|
|
.thumb_func
|
|
|
|
.global _start
|
|
|
|
_start:
|
2020-02-06 17:12:48 -06:00
|
|
|
ldr r6, [r0, #4] /* read rp */
|
2017-11-13 07:05:28 -06:00
|
|
|
|
|
|
|
wait_fifo:
|
2020-02-06 17:12:48 -06:00
|
|
|
ldr r7, [r0, #0] /* read wp */
|
|
|
|
cbz r7, exit /* abort if wp == 0, status = 0 */
|
|
|
|
subs r7, r7, r6 /* number of bytes available for read in r7 */
|
2017-11-13 07:05:28 -06:00
|
|
|
ittt mi /* if wrapped around */
|
2020-02-06 17:12:48 -06:00
|
|
|
addmi r7, r1 /* add size of buffer */
|
|
|
|
submi r7, r0
|
|
|
|
submi r7, #8
|
|
|
|
cmp r7, r4 /* wait until data buffer is full */
|
2017-11-13 07:05:28 -06:00
|
|
|
bcc wait_fifo
|
|
|
|
|
2020-02-06 17:12:48 -06:00
|
|
|
mov r7, #STM32_CR_PROG
|
|
|
|
str r7, [r5, #STM32_FLASH_CR_OFFSET]
|
2017-11-13 07:05:28 -06:00
|
|
|
|
2020-02-06 17:12:48 -06:00
|
|
|
mov r8, #4
|
2020-07-12 17:32:49 -05:00
|
|
|
udiv r8, r4, r8 /* number of words is size of write word divided by 4*/
|
2017-11-13 07:05:28 -06:00
|
|
|
write_flash:
|
2019-02-07 11:30:10 -06:00
|
|
|
dsb
|
2020-02-06 17:12:48 -06:00
|
|
|
ldr r7, [r6], #0x04 /* read one word from src, increment ptr */
|
|
|
|
str r7, [r2], #0x04 /* write one word to dst, increment ptr */
|
2017-11-13 07:05:28 -06:00
|
|
|
dsb
|
2020-02-06 17:12:48 -06:00
|
|
|
cmp r6, r1 /* if rp >= end of buffer ... */
|
2017-11-13 07:05:28 -06:00
|
|
|
it cs
|
2020-02-06 17:12:48 -06:00
|
|
|
addcs r6, r0, #8 /* ... then wrap at buffer start */
|
|
|
|
subs r8, r8, #1 /* decrement loop index */
|
2017-11-13 07:05:28 -06:00
|
|
|
bne write_flash /* loop if not done */
|
|
|
|
|
|
|
|
busy:
|
2020-02-06 17:12:48 -06:00
|
|
|
ldr r7, [r5, #STM32_FLASH_SR_OFFSET]
|
|
|
|
tst r7, #STM32_SR_QW_MASK
|
2017-11-13 07:05:28 -06:00
|
|
|
bne busy /* operation in progress, wait ... */
|
|
|
|
|
2020-02-06 17:12:48 -06:00
|
|
|
ldr r8, =STM32_SR_ERROR_MASK
|
|
|
|
tst r7, r8
|
2017-11-13 07:05:28 -06:00
|
|
|
bne error /* fail... */
|
|
|
|
|
2020-02-06 17:12:48 -06:00
|
|
|
str r6, [r0, #4] /* store rp */
|
2017-11-13 07:05:28 -06:00
|
|
|
subs r3, r3, #1 /* decrement count */
|
|
|
|
bne wait_fifo /* loop if not done */
|
|
|
|
b exit
|
|
|
|
|
|
|
|
error:
|
2020-02-06 17:12:48 -06:00
|
|
|
movs r8, #0
|
|
|
|
str r8, [r0, #4] /* set rp = 0 on error */
|
2017-11-13 07:05:28 -06:00
|
|
|
|
|
|
|
exit:
|
2020-02-06 17:12:48 -06:00
|
|
|
mov r0, r7 /* return status in r0 */
|
2017-11-13 07:05:28 -06:00
|
|
|
bkpt #0x00
|
|
|
|
|
2018-03-01 15:57:08 -06:00
|
|
|
.pool
|