flash support (only full erase/write) for 568013 and 568037
[fw/openocd] / src / flash / nor / dsp5680xx_flash.c
1 /***************************************************************************
2  *   Copyright (C) 2011 by Rodrigo L. Rosa                                 *
3  *   rodrigorosa.LG@gmail.com                                              *
4  *                                                                         *
5  *   Based on a file written by:                                           *
6  *   Kevin McGuire                                                         * 
7  *   Marcel Wijlaars                                                       *
8  *   Michael Ashton                                                        *
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program; if not, write to the                         *
22  *   Free Software Foundation, Inc.,                                       *
23  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  ***************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #ifndef DSP5680XX_FLASH_H
31 #define DSP5680XX_FLASH_H
32
33 #include "imp.h"
34 #include <helper/binarybuffer.h>
35 #include <helper/time_support.h>
36 #include <target/algorithm.h>
37 #include <target/dsp5680xx.h>
38
39 struct dsp5680xx_flash_bank {
40         struct working_area *write_algorithm;
41 };
42
43 static int dsp5680xx_build_sector_list(struct flash_bank *bank){
44   //LOG_USER("%s not implemented",__FUNCTION__);
45   //return ERROR_OK;
46
47   // sector size is 512
48   // bank->num_sectors = bank->size / 512; // Bank size is actually 0x2000, but it is set much higher as part of the workaround for byte/word addressing issues.
49   bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
50   int i;
51   for (i = 0; i < bank->num_sectors; ++i){
52     bank->sectors[i].offset = 0;// not implemented.
53     bank->sectors[i].size = HFM_SECTOR_SIZE;
54     //offset += bank->sectors[i].size;
55     bank->sectors[i].is_erased = -1;
56     bank->sectors[i].is_protected = -1;
57   }
58   LOG_USER("%s not tested yet.",__FUNCTION__);
59   return ERROR_OK;
60
61 }
62
63 // flash bank dsp5680xx 0 0 0 0 <target#>
64 FLASH_BANK_COMMAND_HANDLER(dsp5680xx_flash_bank_command){
65   struct dsp5680xx_flash_bank *nbank;
66
67   nbank = malloc(sizeof(struct dsp5680xx_flash_bank));
68
69   bank->base = HFM_FLASH_BASE_ADDR;
70   bank->size = HFM_SIZE; // top 4k not accessible
71   bank->driver_priv = nbank;
72   bank->num_sectors = HFM_SECTOR_COUNT;// This number is anything >0. not really used.
73   dsp5680xx_build_sector_list(bank);
74
75   return ERROR_OK;
76 }
77
78 static int dsp5680xx_flash_protect_check(struct flash_bank *bank){
79   int retval = ERROR_OK;
80   uint8_t protected = 0; 
81   if(bank->sectors[0].is_protected == -1){
82     retval = dsp5680xx_f_protect_check(bank->target,&protected);
83     if(retval == ERROR_OK)
84       if(protected)
85         bank->sectors[0].is_protected = 1;
86       else
87         bank->sectors[0].is_protected = 0;
88     else
89       bank->sectors[0].is_protected = -1;
90   }
91   return retval;
92 }
93
94 static int dsp5680xx_flash_protect(struct flash_bank *bank, int set, int first, int last){
95   int retval;
96   if(set){
97     retval = dsp5680xx_f_lock(bank->target);
98     if(retval == ERROR_OK)
99       bank->sectors[0].is_protected = 1;
100   }else{    
101     retval = dsp5680xx_f_unlock(bank->target);
102     if(retval == ERROR_OK)
103       bank->sectors[0].is_protected = 0;
104   }
105   return retval;
106 }
107
108 /*
109 static int dsp5680xx_write_block(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count){
110   LOG_USER("%s not implemented",__FUNCTION__);
111   return ERROR_OK;
112 }
113
114 static int dsp5680xx_write_single(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count){
115   LOG_USER("%s not implemented",__FUNCTION__);
116   return ERROR_OK;
117 }
118 */
119
120 //-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
121 //-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
122 //  Flash stuff test
123 //-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
124 //-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
125
126 static int dsp5680xx_flash_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count){
127   int retval;
128   if((offset + count/2)>bank->size){
129     LOG_ERROR("%s: Flash bank cannot fit data.",__FUNCTION__);
130     return ERROR_FAIL;
131   }
132   if(offset%2){
133     LOG_ERROR("%s: Writing to odd addresses not supported. This chip uses word addressing, Openocd only supports byte addressing. The workaround results in disabling writing to odd byte addresses.",__FUNCTION__);
134     return ERROR_FAIL;
135   }
136   retval = dsp5680xx_f_wr(bank->target,  buffer, bank->base + offset/2,  count);
137   if(retval == ERROR_OK)
138     bank->sectors[0].is_erased = 0;
139   else
140     bank->sectors[0].is_erased = -1;
141   return retval;
142 }
143
144 static int dsp5680xx_probe(struct flash_bank *bank){
145   //LOG_USER("%s not implemented",__FUNCTION__);
146         return ERROR_OK;
147 }
148
149 static int dsp5680xx_flash_info(struct flash_bank *bank, char *buf, int buf_size){
150         snprintf(buf, buf_size, "\ndsp5680xx flash driver info:\n - Currently only full erase/lock/unlock are implemented. \n - Call with bank==0 and sector 0 to 0.\n - Protect requires arp_init-reset to complete. \n - Before removing protection the master tap must be selected, and arp_init-reset is required to complete unlocking.");
151         return ERROR_OK;
152 }
153 /*
154 static int dsp5680xx_set_write_enable(struct target *target, int enable){
155         LOG_USER("%s not implemented",__FUNCTION__);
156         return ERROR_OK;
157 }
158
159
160 static int dsp5680xx_check_flash_completion(struct target* target, unsigned int timeout_ms){
161   LOG_USER("%s not implemented",__FUNCTION__);
162   return ERROR_OK;
163 }
164 */
165
166 static int dsp5680xx_flash_erase(struct flash_bank * bank, int first, int last){
167   int retval;
168   retval = dsp5680xx_f_erase(bank->target, (uint32_t) first, (uint32_t) last);
169   if(retval == ERROR_OK)
170     bank->sectors[0].is_erased = 1;
171   else
172     bank->sectors[0].is_erased = -1;
173   return retval;
174 }
175
176 static int dsp5680xx_flash_erase_check(struct flash_bank * bank){
177   int retval = ERROR_OK;
178   uint8_t erased = 0;
179   if(bank->sectors[0].is_erased == -1){
180     retval = dsp5680xx_f_erase_check(bank->target,&erased);
181     if (retval != ERROR_OK){
182       bank->sectors[0].is_erased = -1;
183     }else{
184       if(erased)
185         bank->sectors[0].is_erased = 1;
186       else
187         bank->sectors[0].is_erased = 0;
188     }
189   }
190   return retval;
191 }
192
193 struct flash_driver dsp5680xx_flash = {
194   .name = "dsp5680xx_flash",
195   .flash_bank_command = dsp5680xx_flash_bank_command,
196   .erase = dsp5680xx_flash_erase,
197   .protect = dsp5680xx_flash_protect,
198   .write = dsp5680xx_flash_write,
199   //.read = default_flash_read,
200   //.probe = dsp5680xx_probe,
201   .auto_probe = dsp5680xx_probe,
202   .erase_check = dsp5680xx_flash_erase_check,
203   .protect_check = dsp5680xx_flash_protect_check,
204   .info = dsp5680xx_flash_info
205 };
206 #endif // dsp5680xx_flash.h