flash/nor: Do not update 'is_erased'
[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, see <http://www.gnu.org/licenses/>. *
22  ***************************************************************************/
23
24 /**
25  * @file   dsp5680xx_flash.c
26  * @author Rodrigo L. Rosa <rodrigorosa.LG@gmail.com>
27  * @date   Thu Jun  9 18:21:58 2011
28  *
29  * @brief  This file implements the basic functions to run flashing commands
30  * from the TCL interface.
31  * It allows the user to flash the Freescale 5680xx DSP.
32  *
33  *
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "imp.h"
41 #include <helper/binarybuffer.h>
42 #include <helper/time_support.h>
43 #include <target/algorithm.h>
44 #include <target/dsp5680xx.h>
45
46 static int dsp5680xx_build_sector_list(struct flash_bank *bank)
47 {
48         uint32_t offset = HFM_FLASH_BASE_ADDR;
49
50         bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
51
52         for (unsigned int i = 0; i < bank->num_sectors; ++i) {
53                 bank->sectors[i].offset = i * HFM_SECTOR_SIZE;
54                 bank->sectors[i].size = HFM_SECTOR_SIZE;
55                 offset += bank->sectors[i].size;
56                 bank->sectors[i].is_erased = -1;
57                 bank->sectors[i].is_protected = -1;
58         }
59         LOG_USER("%s not tested yet.", __func__);
60         return ERROR_OK;
61
62 }
63
64 /* flash bank dsp5680xx 0 0 0 0 <target#> */
65 FLASH_BANK_COMMAND_HANDLER(dsp5680xx_flash_bank_command)
66 {
67         bank->base = HFM_FLASH_BASE_ADDR;
68         bank->size = HFM_SIZE_BYTES; /* top 4k not accessible */
69         bank->num_sectors = HFM_SECTOR_COUNT;
70         dsp5680xx_build_sector_list(bank);
71
72         return ERROR_OK;
73 }
74
75 /**
76  * A memory mapped register (PROT) holds information regarding sector protection.
77  * Protection refers to undesired core access.
78  * The value in this register is loaded from flash upon reset.
79  *
80  * @param bank
81  *
82  * @return
83  */
84 static int dsp5680xx_flash_protect_check(struct flash_bank *bank)
85 {
86         int retval = ERROR_OK;
87
88         uint16_t protected = 0;
89
90         retval = dsp5680xx_f_protect_check(bank->target, &protected);
91         if (retval != ERROR_OK) {
92                 for (int i = 0; i < HFM_SECTOR_COUNT; i++)
93                         bank->sectors[i].is_protected = -1;
94                 return ERROR_OK;
95         }
96         for (int i = 0; i < HFM_SECTOR_COUNT / 2; i++) {
97                 if (protected & 1) {
98                         bank->sectors[2 * i].is_protected = 1;
99                         bank->sectors[2 * i + 1].is_protected = 1;
100                 } else {
101                         bank->sectors[2 * i].is_protected = 0;
102                         bank->sectors[2 * i + 1].is_protected = 0;
103                 }
104                 protected = (protected >> 1);
105         }
106         return retval;
107 }
108
109 /**
110  * Protection functionality is not implemented.
111  * The current implementation applies/removes security on the chip.
112  * The chip is effectively secured/unsecured after the first reset
113  * following the execution of this function.
114  *
115  * @param bank
116  * @param set Apply or remove security on the chip.
117  * @param first This parameter is ignored.
118  * @param last This parameter is ignored.
119  *
120  * @return
121  */
122 static int dsp5680xx_flash_protect(struct flash_bank *bank, int set,
123                 unsigned int first, unsigned int last)
124 {
125 /**
126  * This applies security to flash module after next reset, it does
127  * not actually apply protection (protection refers to undesired access from the core)
128  */
129         int retval;
130
131         if (set)
132                 retval = dsp5680xx_f_lock(bank->target);
133         else
134                 retval = dsp5680xx_f_unlock(bank->target);
135
136         return retval;
137 }
138
139 /**
140  * The dsp5680xx use word addressing. The "/2" that appear in the following code
141  * are a workaround for the fact that OpenOCD uses byte addressing.
142  *
143  * @param bank
144  * @param buffer Data to write to flash.
145  * @param offset
146  * @param count In bytes (2 bytes per address).
147  *
148  * @return
149  */
150 static int dsp5680xx_flash_write(struct flash_bank *bank, const uint8_t *buffer,
151                                  uint32_t offset, uint32_t count)
152 {
153         if ((offset + count / 2) > bank->size) {
154                 LOG_ERROR("%s: Flash bank cannot fit data.", __func__);
155                 return ERROR_FAIL;
156         }
157         if (offset % 2) {
158                 /**
159                  * Writing to odd addresses not supported.
160                  * This chip uses word addressing, Openocd only supports byte addressing.
161                  * The workaround results in disabling writing to odd byte addresses
162                  */
163                 LOG_ERROR("%s: Writing to odd addresses not supported for this target", __func__);
164                 return ERROR_FAIL;
165         }
166         return dsp5680xx_f_wr(bank->target, buffer, bank->base + offset / 2, count, 0);
167 }
168
169 static int dsp5680xx_probe(struct flash_bank *bank)
170 {
171         LOG_DEBUG("%s not implemented", __func__);
172         return ERROR_OK;
173 }
174
175 /**
176  * The flash module (FM) on the dsp5680xx supports both individual sector
177  * and mass erase of the flash memory.
178  * If this function is called with @a first == @a last == 0 or if @a first is the
179  * first sector (#0) and @a last is the last sector then the mass erase command
180  * is executed (much faster than erasing each sector individually).
181  *
182  * @param bank
183  * @param first
184  * @param last
185  *
186  * @return
187  */
188 static int dsp5680xx_flash_erase(struct flash_bank *bank, unsigned int first,
189                 unsigned int last)
190 {
191         return dsp5680xx_f_erase(bank->target, (uint32_t) first, (uint32_t) last);
192 }
193
194 /**
195  * The flash module (FM) on the dsp5680xx support a blank check function.
196  * This function executes the FM's blank check functionality on each and every sector.
197  *
198  * @param bank
199  *
200  * @return
201  */
202 static int dsp5680xx_flash_erase_check(struct flash_bank *bank)
203 {
204         int retval = ERROR_OK;
205
206         uint8_t erased = 0;
207
208         uint32_t i;
209
210         for (i = 0; i < HFM_SECTOR_COUNT; i++) {
211                 retval = dsp5680xx_f_erase_check(bank->target, &erased, i);
212                 if (retval != ERROR_OK) {
213                         bank->sectors[i].is_erased = -1;
214                 } else {
215                         if (erased)
216                                 bank->sectors[i].is_erased = 1;
217                         else
218                                 bank->sectors[i].is_erased = 0;
219                 }
220         }
221         return retval;
222 }
223
224 const struct flash_driver dsp5680xx_flash = {
225         .name = "dsp5680xx_flash",
226         .flash_bank_command = dsp5680xx_flash_bank_command,
227         .erase = dsp5680xx_flash_erase,
228         .protect = dsp5680xx_flash_protect,
229         .write = dsp5680xx_flash_write,
230         /* .read = default_flash_read, */
231         .probe = dsp5680xx_probe,
232         .auto_probe = dsp5680xx_probe,
233         .erase_check = dsp5680xx_flash_erase_check,
234         .protect_check = dsp5680xx_flash_protect_check,
235 };