1 /***************************************************************************
2 * Copyright (C) 2010 by Spencer Oliver *
3 * spen@spen-soft.co.uk *
5 * Copyright (C) 2011 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2015 Uwe Bonnes *
9 * bon@elektron.ikp.physik.tu-darmstadt.de *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc. *
24 ***************************************************************************/
33 * r0 = workarea start, status (out)
36 * r3 = count (64bit words)
41 * r6/7 - temp (64-bit)
45 #define STM32_FLASH_CR_OFFSET 0x14 /* offset of CR register in FLASH struct */
46 #define STM32_FLASH_SR_OFFSET 0x10 /* offset of SR register in FLASH struct */
48 #define STM32_PROG 0x1 /* PG */
54 ldr r8, [r0, #0] /* read wp */
55 cmp r8, #0 /* abort if wp == 0 */
57 ldr r5, [r0, #4] /* read rp */
58 subs r6, r8, r5 /* number of bytes available for read in r6*/
59 itt mi /* if wrapped around*/
60 addmi r6, r1 /* add size of buffer */
62 cmp r6, #8 /* wait until 8 bytes are available */
66 str r6, [r4, #STM32_FLASH_CR_OFFSET]
67 ldrd r6, [r5], #0x08 /* read one word from src, increment ptr */
68 strd r6, [r2], #0x08 /* write one word to dst, increment ptr */
71 ldr r6, [r4, #STM32_FLASH_SR_OFFSET]
72 tst r6, #0x10000 /* BSY (bit16) == 1 => operation in progress */
73 bne busy /* wait more... */
74 tst r6, #0xfa /* PGSERR | PGPERR | PGAERR | WRPERR | PROGERR*/
75 bne error /* fail... */
77 cmp r5, r1 /* wrap rp at end of buffer */
79 addcs r5, r0, #8 /* skip loader args */
80 str r5, [r0, #4] /* store rp */
81 subs r3, r3, #1 /* decrement dword count */
82 cbz r3, exit /* loop if not done */
86 str r1, [r0, #4] /* set rp = 0 on error */
88 mov r0, r6 /* return status in r0 */