main.c 3.28 KB
Newer Older
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
/***************************************************************************
 *   Copyright (C) 2007 by Pavel Chromy                                    *
 *   chromy@asix.cz                                                        *
 *                                                                         *
 *   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.             *
 ***************************************************************************/
#include "platform.h"

#include "ocl.h"
#include "dcc.h"
#include "samflash.h"


#define BUFSIZE 1024 /* words, i.e. 4 KiB */
uint32 buffer[1024];

void cmd_flash(uint32 cmd)
{
	unsigned int len;
	uint32 adr;
	uint32 chksum;
	unsigned int bi; /* buffer index */
	unsigned int bi_start; /* receive start mark */
	unsigned int bi_end; /* receive end mark */
	unsigned int ofs;
	int pagenum;
	int result;

zwelch's avatar
zwelch committed
42
43
44
45
	adr = dcc_rd();
	len = cmd&0xffff;
	ofs = adr%flash_page_size;
	bi_start = ofs/4;
zwelch's avatar
zwelch committed
46
	bi_end = (ofs + len + 3)/4;
47
48
49
50
51
52

	if (bi_end>BUFSIZE) {
		dcc_wr(OCL_BUFF_OVER);
		return;
	}

zwelch's avatar
zwelch committed
53
54
	chksum = OCL_CHKS_INIT;
	for (bi = 0; bi<bi_end; bi++) chksum^=buffer[bi]=dcc_rd();
55

zwelch's avatar
zwelch committed
56
	if (dcc_rd() != chksum) {
57
58
59
60
61
		dcc_wr(OCL_CHKS_FAIL);
		return;
	}

	/* fill in unused positions with unprogrammed values */
zwelch's avatar
zwelch committed
62
63
	for (bi = 0; bi<bi_start; bi++) buffer[bi]=0xffffffff;
	for (bi = bi_end; bi%flash_page_size; bi++) buffer[bi]=0xffffffff;
64

zwelch's avatar
zwelch committed
65
66
67
	result = 0;
	pagenum = adr/flash_page_size;
	for (bi = 0; bi<bi_end; bi += flash_page_size/4) {
zwelch's avatar
zwelch committed
68
		result = flash_page_program(buffer + bi, pagenum++);
69
70
71
72
		if (result) break;
	}

	/* verify written data */
zwelch's avatar
zwelch committed
73
	if (!result) result = flash_verify(adr, len, ((uint8 *)buffer) + ofs);
74

zwelch's avatar
zwelch committed
75
	dcc_wr(OCL_CMD_DONE | result);
76
77
78
79
80
81
82
83
}


int main (void)
{
	uint32 cmd;

	for (;;) {
zwelch's avatar
zwelch committed
84
		cmd = dcc_rd();
85
86
		switch (cmd&OCL_CMD_MASK) {
			case OCL_PROBE:
zwelch's avatar
zwelch committed
87
				dcc_wr(OCL_CMD_DONE | flash_init());
88
89
90
				dcc_wr(0x100000); /* base */
				dcc_wr(flash_page_count*flash_page_size); /* size */
				dcc_wr(1); /* num_sectors */
zwelch's avatar
zwelch committed
91
				dcc_wr(4096 | ((unsigned long) flash_page_size << 16)); /* buflen and bufalign */
92
93
				break;
			case OCL_ERASE_ALL:
zwelch's avatar
zwelch committed
94
				dcc_wr(OCL_CMD_DONE | flash_erase_all());
95
96
97
98
99
100
101
102
103
104
105
106
107
				break;
			case OCL_FLASH_BLOCK:
				cmd_flash(cmd);
				break;
			default:
				/* unknown command */
				dcc_wr(OCL_CMD_ERR);
				break;
		}
	}

	return(0); /* we shall never get here, just to supress compiler warning */
}