0bbda7bc850603d15c1d35e453dc36ead4e5113e
[mw/openocd-lm32.git] / ecosflash / flash.c
1 /***************************************************************************
2  *   Copyright (C) 2008 √ėyvind Harboe                                      *
3  *   oyvind.harboe@zylin.com                                               *
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
21 #include <string.h>
22 #define _FLASH_PRIVATE_
23 #include <cyg/io/flash.h>
24
25
26
27 int myprintf(char *format, ...)
28 {
29   return 0;
30 }
31
32 extern char _start_bss_clear;
33 extern char __bss_end__;
34
35 int init()
36 {
37         // set up runtime environment
38         char *t;
39         for (t=&_start_bss_clear; t<&__bss_end__; t++)
40         {
41                 *t=0;
42         }
43         return flash_init((_printf *)&myprintf);
44         
45 }
46
47
48 int checkFlash(void *addr, int len)
49 {
50     // Return error for illegal addresses
51     if ((addr<flash_info.start)||(addr>flash_info.end))
52         return FLASH_ERR_INVALID;
53     if ((((cyg_uint8 *)addr)+len)>(cyg_uint8 *)flash_info.end)
54         return FLASH_ERR_INVALID;
55     return FLASH_ERR_OK;
56 }
57
58
59 int erase(void *address, int len)       
60 {
61         int retval;
62         void *failAddress;
63         
64         retval=checkFlash(address, len);
65         if (retval!=0)
66                 return retval;
67         
68         retval=init();
69         if (retval!=0)
70                 return retval;
71         return flash_erase(address, len, &failAddress);
72 }
73
74
75
76 extern char _end;
77
78 // Data follows immediately after program, long word aligned.
79 int program(void *buffer, void *address, int len)       
80 {
81         int retval;
82         void *failAddress;
83         retval=checkFlash(address, len);
84         if (retval!=0)
85                 return retval;
86         
87         retval=init();
88         if (retval!=0)
89                 return retval;
90         //int flash_program(void *_addr, void *_data, int len, void **err_addr)
91         return flash_program(address, buffer, len, &failAddress);
92 }