aboutsummaryrefslogtreecommitdiff
path: root/contrib/loaders/flash/k1921vk01t.S
blob: b8f0b53d5e3f5b9186f5e1648ceec6c49cd55d77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/***************************************************************************
 *   Copyright (C) 2015 by Bogdan Kolbov                                   *
 *   kolbov@niiet.ru                                                       *
 *                                                                         *
 *   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
	.cpu cortex-m4
	.thumb
	.thumb_func

/* K1921VK01T has 128-bitwidth flash, so it`s able to load 4x32-bit words at the time.
 * And only after all words loaded we can start write
 */

/* Registers addresses */
#define FLASH_FMA	0x00		/* Address reg */
#define FLASH_FMD1	0x04		/* Data1 reg */
#define FLASH_FMC	0x08		/* Command reg */
#define FLASH_FCIS	0x0C		/* Operation Status reg */
#define FLASH_FCIC	0x14		/* Operation Status Clear reg */
#define FLASH_FMD2	0x50		/* Data2 reg */
#define FLASH_FMD3	0x54		/* Data3 reg */
#define FLASH_FMD4	0x58		/* Data4 reg*/

	/* Params:
	 * r0 - write cmd (in), status (out)
	 * r1 - count
	 * r2 - workarea start
	 * r3 - workarea end
	 * r4 - target address
	 * Clobbered:
	 * r5 - rp
	 * r6 - wp, tmp
	 * r7 - flash base
	 */

ldr     r7, =#0xA001C000  /* Flash reg base*/

wait_fifo:
	ldr		r6, [r2, #0]	/* read wp */
	cmp		r6, #0			/* abort if wp == 0 */
	beq		exit
	ldr		r5, [r2, #4]	/* read rp */
	cmp		r5, r6			/* wait until rp != wp */
	beq		wait_fifo


load_data:
	ldr r6, [r5]			/* read data1 */
	str r6, [r7, #FLASH_FMD1]
	adds	r5, #4

	ldr r6, [r5]			/* read data2 */
	str r6, [r7, #FLASH_FMD2]
	adds	r5, #4

	ldr r6, [r5]			/* read data3 */
	str r6, [r7, #FLASH_FMD3]
	adds	r5, #4

	ldr r6, [r5]			/* read data4 */
	str r6, [r7, #FLASH_FMD4]
	adds	r5, #4

start_write:
	str r4, [r7, #FLASH_FMA]		/* set addr */
	adds	r4, #16
	str r0, [r7, #FLASH_FMC]		/* write cmd */

busy:
	ldr		r6, [r7, #FLASH_FCIS]	/* wait until flag set */
	cmp		r6, #0x0
	beq		busy

	cmp		r6, #2			/* check the error bit */
	beq		error

	movs	r6, #1			/* clear flags */
	str r6, [r7, #FLASH_FCIC]

	cmp 	r5, r3			/* wrap rp at end of buffer */
	bcc	no_wrap
	mov	r5, r2
	adds	r5, #8
no_wrap:
	str 	r5, [r2, #4]	/* store rp */
	subs	r1, r1, #1		/* decrement 16-byte block count */
	cmp     r1, #0
	beq     exit		/* loop if not done */
	b	wait_fifo

error:
	movs	r0, #0
	str		r0, [r2, #4]	/* set rp = 0 on error */
exit:
	mov		r0, r6			/* return status in r0 */
	bkpt	#0