2008-04-24 06:09:28 -05:00
|
|
|
/***************************************************************************
|
|
|
|
* 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 *
|
2018-06-11 14:47:52 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
2008-04-24 06:09:28 -05:00
|
|
|
***************************************************************************/
|
|
|
|
#include "platform.h"
|
|
|
|
|
2009-12-03 06:14:58 -06:00
|
|
|
#include <flash/nor/ocl.h>
|
2008-04-24 06:09:28 -05:00
|
|
|
#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;
|
|
|
|
|
2009-06-23 17:42:54 -05:00
|
|
|
adr = dcc_rd();
|
|
|
|
len = cmd&0xffff;
|
|
|
|
ofs = adr%flash_page_size;
|
|
|
|
bi_start = ofs/4;
|
2009-06-23 17:44:17 -05:00
|
|
|
bi_end = (ofs + len + 3)/4;
|
2008-04-24 06:09:28 -05:00
|
|
|
|
2009-06-23 17:45:47 -05:00
|
|
|
if (bi_end > BUFSIZE) {
|
2008-04-24 06:09:28 -05:00
|
|
|
dcc_wr(OCL_BUFF_OVER);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-23 17:42:54 -05:00
|
|
|
chksum = OCL_CHKS_INIT;
|
2009-06-23 17:45:47 -05:00
|
|
|
for (bi = 0; bi < bi_end; bi++) chksum^=buffer[bi]=dcc_rd();
|
2008-04-24 06:09:28 -05:00
|
|
|
|
2009-06-23 17:38:12 -05:00
|
|
|
if (dcc_rd() != chksum) {
|
2008-04-24 06:09:28 -05:00
|
|
|
dcc_wr(OCL_CHKS_FAIL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fill in unused positions with unprogrammed values */
|
2009-06-23 17:45:47 -05:00
|
|
|
for (bi = 0; bi < bi_start; bi++) buffer[bi]=0xffffffff;
|
2009-06-23 17:42:54 -05:00
|
|
|
for (bi = bi_end; bi%flash_page_size; bi++) buffer[bi]=0xffffffff;
|
2008-04-24 06:09:28 -05:00
|
|
|
|
2009-06-23 17:42:54 -05:00
|
|
|
result = 0;
|
|
|
|
pagenum = adr/flash_page_size;
|
2009-06-23 17:45:47 -05:00
|
|
|
for (bi = 0; bi < bi_end; bi += flash_page_size/4) {
|
2009-06-23 17:44:17 -05:00
|
|
|
result = flash_page_program(buffer + bi, pagenum++);
|
2008-04-24 06:09:28 -05:00
|
|
|
if (result) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify written data */
|
2009-06-23 17:44:17 -05:00
|
|
|
if (!result) result = flash_verify(adr, len, ((uint8 *)buffer) + ofs);
|
2008-04-24 06:09:28 -05:00
|
|
|
|
2009-06-23 17:45:15 -05:00
|
|
|
dcc_wr(OCL_CMD_DONE | result);
|
2008-04-24 06:09:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main (void)
|
|
|
|
{
|
|
|
|
uint32 cmd;
|
|
|
|
|
|
|
|
for (;;) {
|
2009-06-23 17:42:54 -05:00
|
|
|
cmd = dcc_rd();
|
2008-04-24 06:09:28 -05:00
|
|
|
switch (cmd&OCL_CMD_MASK) {
|
|
|
|
case OCL_PROBE:
|
2009-06-23 17:45:15 -05:00
|
|
|
dcc_wr(OCL_CMD_DONE | flash_init());
|
2008-04-24 06:09:28 -05:00
|
|
|
dcc_wr(0x100000); /* base */
|
|
|
|
dcc_wr(flash_page_count*flash_page_size); /* size */
|
|
|
|
dcc_wr(1); /* num_sectors */
|
2009-06-23 17:41:13 -05:00
|
|
|
dcc_wr(4096 | ((unsigned long) flash_page_size << 16)); /* buflen and bufalign */
|
2008-04-24 06:09:28 -05:00
|
|
|
break;
|
|
|
|
case OCL_ERASE_ALL:
|
2009-06-23 17:45:15 -05:00
|
|
|
dcc_wr(OCL_CMD_DONE | flash_erase_all());
|
2008-04-24 06:09:28 -05:00
|
|
|
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 */
|
|
|
|
}
|