Pavel Chromy's on chip flash loader
[openocd.git] / src / flash / ocl / at91sam7x / main.c
1 /***************************************************************************
2 * Copyright (C) 2007 by Pavel Chromy *
3 * chromy@asix.cz *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include "platform.h"
21
22 #include "ocl.h"
23 #include "dcc.h"
24 #include "samflash.h"
25
26
27 #define BUFSIZE 1024 /* words, i.e. 4 KiB */
28 uint32 buffer[1024];
29
30 void cmd_flash(uint32 cmd)
31 {
32 unsigned int len;
33 uint32 adr;
34 uint32 chksum;
35 unsigned int bi; /* buffer index */
36 unsigned int bi_start; /* receive start mark */
37 unsigned int bi_end; /* receive end mark */
38 unsigned int ofs;
39 int pagenum;
40 int result;
41
42 adr=dcc_rd();
43 len=cmd&0xffff;
44 ofs=adr%flash_page_size;
45 bi_start=ofs/4;
46 bi_end=(ofs+len+3)/4;
47
48 if (bi_end>BUFSIZE) {
49 dcc_wr(OCL_BUFF_OVER);
50 return;
51 }
52
53 chksum=OCL_CHKS_INIT;
54 for (bi=0; bi<bi_end; bi++) chksum^=buffer[bi]=dcc_rd();
55
56 if (dcc_rd()!=chksum) {
57 dcc_wr(OCL_CHKS_FAIL);
58 return;
59 }
60
61 /* fill in unused positions with unprogrammed values */
62 for (bi=0; bi<bi_start; bi++) buffer[bi]=0xffffffff;
63 for (bi=bi_end; bi%flash_page_size; bi++) buffer[bi]=0xffffffff;
64
65 result=0;
66 pagenum=adr/flash_page_size;
67 for (bi=0; bi<bi_end; bi+=flash_page_size/4) {
68 result=flash_page_program(buffer+bi, pagenum++);
69 if (result) break;
70 }
71
72 /* verify written data */
73 if (!result) result=flash_verify(adr, len, ((uint8 *)buffer)+ofs);
74
75 dcc_wr(OCL_CMD_DONE|result);
76 }
77
78
79 int main (void)
80 {
81 uint32 cmd;
82
83 for (;;) {
84 cmd=dcc_rd();
85 switch (cmd&OCL_CMD_MASK) {
86 case OCL_PROBE:
87 dcc_wr(OCL_CMD_DONE|flash_init());
88 dcc_wr(0x100000); /* base */
89 dcc_wr(flash_page_count*flash_page_size); /* size */
90 dcc_wr(1); /* num_sectors */
91 dcc_wr(4096 | ((unsigned long) flash_page_size<<16)); /* buflen and bufalign */
92 break;
93 case OCL_ERASE_ALL:
94 dcc_wr(OCL_CMD_DONE|flash_erase_all());
95 break;
96 case OCL_FLASH_BLOCK:
97 cmd_flash(cmd);
98 break;
99 default:
100 /* unknown command */
101 dcc_wr(OCL_CMD_ERR);
102 break;
103 }
104 }
105
106 return(0); /* we shall never get here, just to supress compiler warning */
107 }