aboutsummaryrefslogtreecommitdiff
path: root/contrib/loaders/flash/pic32mx.s
blob: b26ff61134da43460d9d38ebe1ca5e819fb425a5 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/***************************************************************************
 *   Copyright (C) 2010 by Spencer Oliver                                  *
 *   spen@spen-soft.co.uk                                                  *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

	.text
	.set noreorder
	.set noat

/* params:
 * $a0 src adr - ram + result
 * $a1 dest adr - flash
 * $a2 count (32bit words)
 * vars
 *
 * temps:
 * $t0, $t1, $t2, $t3, $t4, $t5
 * $s0, $s1, $s3, $s4, $s5
 */

	.type main, @function
	.global main

.ent main
main:
	/* setup constants */
	lui		$t0, 0xaa99
	ori		$t0, 0x6655				/* NVMKEY1 */
	lui		$t1, 0x5566
	ori		$t1, 0x99AA				/* NVMKEY2 */
	lui		$t2, 0xBF80
	ori		$t2, 0xF400				/* NVMCON */
	ori		$t3, $zero, 0x4003		/* NVMCON row write cmd */
	ori		$t4, $zero, 0x8000		/* NVMCON start cmd */

write_row:
	/* can we perform a row write: 128 32bit words */
	sltiu	$s3, $a2, 128
	bne		$s3, $zero, write_word
	ori		$t5, $zero, 0x4000		/* NVMCON clear cmd */

	/* perform row write 512 bytes */
	sw		$a1, 32($t2)	/* set NVMADDR with dest addr - real addr */
	sw		$a0, 64($t2)	/* set NVMSRCADDR with src addr - real addr */

	bal		progflash
	addiu	$a0, $a0, 512
	addiu	$a1, $a1, 512
	beq		$zero, $zero, write_row
	addiu	$a2, $a2, -128

write_word:
	/* write 32bit words */
	lui		$s5, 0xa000
	ori		$s5, 0x0000
	or		$a0, $a0, $s5			/* convert to virtual addr */

	beq		$zero, $zero, next_word
	ori		$t3, $zero, 0x4001		/* NVMCON word write cmd */

prog_word:
	lw		$s4, 0($a0)		/* load data - from virtual addr */
	sw		$s4, 48($t2)	/* set NVMDATA with data */
	sw		$a1, 32($t2)	/* set NVMADDR with dest addr - real addr */

	bal		progflash
	addiu	$a0, $a0, 4
	addiu	$a1, $a1, 4
	addiu	$a2, $a2, -1
next_word:
	bne		$a2, $zero, prog_word
	nop

done:
	beq		$zero, $zero, exit
	addiu	$a0, $zero, 0

error:
	/* save result to $a0 */
	addiu	$a0, $s1, 0
	
exit:
	sdbbp
.end main

	.type progflash, @function
	.global progflash

.ent progflash
progflash:
	sw		$t3, 0($t2)		/* set NVMWREN */
	sw		$t0, 16($t2)	/* write NVMKEY1 */
	sw		$t1, 16($t2)	/* write NVMKEY2 */
	sw		$t4, 8($t2)		/* start operation */

waitflash:
	lw		$s0, 0($t2)
	and		$s0, $s0, $t4
	bne		$s0, $zero, waitflash
	nop

	/* following is to comply with errata #34
	 * 500ns delay required */
	nop
	nop
	nop
	nop
	/* check for errors */
	lw		$s1, 0($t2)
	andi	$s1, $zero, 0x3000
	bne		$s1, $zero, error
	sw		$t5, 4($t2)		/* clear NVMWREN */
	jr		$ra
	nop
	
.end progflash