flash: nor: ath79: remove base calculation
[openocd.git] / src / flash / nor / lpc288x.c
1 /***************************************************************************
2 * Copyright (C) 2008 by *
3 * Karl RobinSod <karl.robinsod@gmail.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, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
18
19 /***************************************************************************
20 * There are some things to notice
21 *
22 * You need to unprotect flash sectors each time you connect the OpenOCD
23 * Dumping 1MB takes about 60 Seconds
24 * Full erase (sectors 0-22 inclusive) takes 2-4 seconds
25 * Writing 1MB takes 88 seconds
26 *
27 ***************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include "imp.h"
33 #include <helper/binarybuffer.h>
34
35 #define LOAD_TIMER_ERASE 0
36 #define LOAD_TIMER_WRITE 1
37
38 #define FLASH_PAGE_SIZE 512
39
40 /* LPC288X control registers */
41 #define DBGU_CIDR 0x8000507C
42 /* LPC288X flash registers */
43 #define F_CTRL 0x80102000 /* Flash control register R/W 0x5 */
44 #define F_STAT 0x80102004 /* Flash status register RO 0x45 */
45 #define F_PROG_TIME 0x80102008 /* Flash program time register R/W 0 */
46 #define F_WAIT 0x80102010 /* Flash read wait state register R/W 0xC004 */
47 #define F_CLK_TIME 0x8010201C /* Flash clock divider for 66 kHz generation R/W 0
48 **/
49 #define F_INTEN_CLR 0x80102FD8 /* Clear interrupt enable bits WO - */
50 #define F_INTEN_SET 0x80102FDC /* Set interrupt enable bits WO - */
51 #define F_INT_STAT 0x80102FE0 /* Interrupt status bits RO 0 */
52 #define F_INTEN 0x80102FE4 /* Interrupt enable bits RO 0 */
53 #define F_INT_CLR 0x80102FE8 /* Clear interrupt status bits WO */
54 #define F_INT_SET 0x80102FEC /* Set interrupt status bits WO - */
55 #define FLASH_PD 0x80005030 /* Allows turning off the Flash memory for power
56 *savings. R/W 1*/
57 #define FLASH_INIT 0x80005034 /* Monitors Flash readiness, such as recovery from
58 *Power Down mode. R/W -*/
59
60 /* F_CTRL bits */
61 #define FC_CS 0x0001
62 #define FC_FUNC 0x0002
63 #define FC_WEN 0x0004
64 #define FC_RD_LATCH 0x0020
65 #define FC_PROTECT 0x0080
66 #define FC_SET_DATA 0x0400
67 #define FC_RSSL 0x0800
68 #define FC_PROG_REQ 0x1000
69 #define FC_CLR_BUF 0x4000
70 #define FC_LOAD_REQ 0x8000
71 /* F_STAT bits */
72 #define FS_DONE 0x0001
73 #define FS_PROGGNT 0x0002
74 #define FS_RDY 0x0004
75 #define FS_ERR 0x0020
76 /* F_PROG_TIME */
77 #define FPT_TIME_MASK 0x7FFF
78
79 #define FPT_ENABLE 0x8000
80 /* F_WAIT */
81 #define FW_WAIT_STATES_MASK 0x00FF
82 #define FW_SET_MASK 0xC000
83
84 /* F_CLK_TIME */
85 #define FCT_CLK_DIV_MASK 0x0FFF
86
87 struct lpc288x_flash_bank {
88 uint32_t working_area;
89 uint32_t working_area_size;
90
91 /* chip id register */
92 uint32_t cidr;
93 const char *target_name;
94 uint32_t cclk;
95
96 uint32_t sector_size_break;
97 };
98
99 static uint32_t lpc288x_wait_status_busy(struct flash_bank *bank, int timeout);
100 static void lpc288x_load_timer(int erase, struct target *target);
101 static void lpc288x_set_flash_clk(struct flash_bank *bank);
102 static uint32_t lpc288x_system_ready(struct flash_bank *bank);
103
104 static uint32_t lpc288x_wait_status_busy(struct flash_bank *bank, int timeout)
105 {
106 uint32_t status;
107 struct target *target = bank->target;
108 do {
109 alive_sleep(1);
110 timeout--;
111 target_read_u32(target, F_STAT, &status);
112 } while (((status & FS_DONE) == 0) && timeout);
113
114 if (timeout == 0) {
115 LOG_DEBUG("Timedout!");
116 return ERROR_FLASH_OPERATION_FAILED;
117 }
118 return ERROR_OK;
119 }
120
121 /* Read device id register and fill in driver info structure */
122 static int lpc288x_read_part_info(struct flash_bank *bank)
123 {
124 struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
125 struct target *target = bank->target;
126 uint32_t cidr;
127
128 int i = 0;
129 uint32_t offset;
130
131 if (lpc288x_info->cidr == 0x0102100A)
132 return ERROR_OK;/* already probed, multiple probes may cause memory leak, not
133 *allowed */
134
135 /* Read and parse chip identification register */
136 target_read_u32(target, DBGU_CIDR, &cidr);
137
138 if (cidr != 0x0102100A) {
139 LOG_WARNING("Cannot identify target as an LPC288X (%08" PRIx32 ")", cidr);
140 return ERROR_FLASH_OPERATION_FAILED;
141 }
142
143 lpc288x_info->cidr = cidr;
144 lpc288x_info->sector_size_break = 0x000F0000;
145 lpc288x_info->target_name = "LPC288x";
146
147 /* setup the sector info... */
148 offset = bank->base;
149 bank->num_sectors = 23;
150 bank->sectors = malloc(sizeof(struct flash_sector) * 23);
151
152 for (i = 0; i < 15; i++) {
153 bank->sectors[i].offset = offset;
154 bank->sectors[i].size = 64 * 1024;
155 offset += bank->sectors[i].size;
156 bank->sectors[i].is_erased = -1;
157 bank->sectors[i].is_protected = 1;
158 }
159 for (i = 15; i < 23; i++) {
160 bank->sectors[i].offset = offset;
161 bank->sectors[i].size = 8 * 1024;
162 offset += bank->sectors[i].size;
163 bank->sectors[i].is_erased = -1;
164 bank->sectors[i].is_protected = 1;
165 }
166
167 return ERROR_OK;
168 }
169
170 /* TODO: Revisit! Is it impossible to read protection status? */
171 static int lpc288x_protect_check(struct flash_bank *bank)
172 {
173 return ERROR_OK;
174 }
175
176 /* flash_bank LPC288x 0 0 0 0 <target#> <cclk> */
177 FLASH_BANK_COMMAND_HANDLER(lpc288x_flash_bank_command)
178 {
179 struct lpc288x_flash_bank *lpc288x_info;
180
181 if (CMD_ARGC < 6)
182 return ERROR_COMMAND_SYNTAX_ERROR;
183
184 lpc288x_info = malloc(sizeof(struct lpc288x_flash_bank));
185 bank->driver_priv = lpc288x_info;
186
187 /* part wasn't probed for info yet */
188 lpc288x_info->cidr = 0;
189 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], lpc288x_info->cclk);
190
191 return ERROR_OK;
192 }
193
194 /* The frequency is the AHB clock frequency divided by (CLK_DIV ×3) + 1.
195 * This must be programmed such that the Flash Programming clock frequency is 66 kHz ± 20%.
196 * AHB = 12 MHz ?
197 * 12000000/66000 = 182
198 * CLK_DIV = 60 ? */
199 static void lpc288x_set_flash_clk(struct flash_bank *bank)
200 {
201 uint32_t clk_time;
202 struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
203 clk_time = (lpc288x_info->cclk / 66000) / 3;
204 target_write_u32(bank->target, F_CTRL, FC_CS | FC_WEN);
205 target_write_u32(bank->target, F_CLK_TIME, clk_time);
206 }
207
208 /* AHB tcyc (in ns) 83 ns
209 * LOAD_TIMER_ERASE FPT_TIME = ((400,000,000 / AHB tcyc (in ns)) - 2) / 512
210 * = 9412 (9500) (AN10548 9375)
211 * LOAD_TIMER_WRITE FPT_TIME = ((1,000,000 / AHB tcyc (in ns)) - 2) / 512
212 * = 23 (75) (AN10548 72 - is this wrong?)
213 * TODO: Sort out timing calcs ;) */
214 static void lpc288x_load_timer(int erase, struct target *target)
215 {
216 if (erase == LOAD_TIMER_ERASE)
217 target_write_u32(target, F_PROG_TIME, FPT_ENABLE | 9500);
218 else
219 target_write_u32(target, F_PROG_TIME, FPT_ENABLE | 75);
220 }
221
222 static uint32_t lpc288x_system_ready(struct flash_bank *bank)
223 {
224 struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
225 if (lpc288x_info->cidr == 0)
226 return ERROR_FLASH_BANK_NOT_PROBED;
227
228 if (bank->target->state != TARGET_HALTED) {
229 LOG_ERROR("Target not halted");
230 return ERROR_TARGET_NOT_HALTED;
231 }
232 return ERROR_OK;
233 }
234
235 static int lpc288x_erase(struct flash_bank *bank, int first, int last)
236 {
237 uint32_t status;
238 int sector;
239 struct target *target = bank->target;
240
241 status = lpc288x_system_ready(bank); /* probed? halted? */
242 if (status != ERROR_OK)
243 return status;
244
245 if ((first < 0) || (last < first) || (last >= bank->num_sectors)) {
246 LOG_INFO("Bad sector range");
247 return ERROR_FLASH_SECTOR_INVALID;
248 }
249
250 /* Configure the flash controller timing */
251 lpc288x_set_flash_clk(bank);
252
253 for (sector = first; sector <= last; sector++) {
254 if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
255 return ERROR_FLASH_OPERATION_FAILED;
256
257 lpc288x_load_timer(LOAD_TIMER_ERASE, target);
258
259 target_write_u32(target, bank->sectors[sector].offset, 0x00);
260
261 target_write_u32(target, F_CTRL, FC_PROG_REQ | FC_PROTECT | FC_CS);
262 }
263 if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
264 return ERROR_FLASH_OPERATION_FAILED;
265 return ERROR_OK;
266 }
267
268 static int lpc288x_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
269 {
270 uint8_t page_buffer[FLASH_PAGE_SIZE];
271 uint32_t status, source_offset, dest_offset;
272 struct target *target = bank->target;
273 uint32_t bytes_remaining = count;
274 uint32_t first_sector, last_sector, sector, page;
275 int i;
276
277 /* probed? halted? */
278 status = lpc288x_system_ready(bank);
279 if (status != ERROR_OK)
280 return status;
281
282 /* Initialise search indices */
283 first_sector = last_sector = 0xffffffff;
284
285 /* validate the write range... */
286 for (i = 0; i < bank->num_sectors; i++) {
287 if ((offset >= bank->sectors[i].offset) &&
288 (offset < (bank->sectors[i].offset + bank->sectors[i].size)) &&
289 (first_sector == 0xffffffff)) {
290 first_sector = i;
291 /* all writes must start on a sector boundary... */
292 if (offset % bank->sectors[i].size) {
293 LOG_INFO(
294 "offset 0x%" PRIx32 " breaks required alignment 0x%" PRIx32 "",
295 offset,
296 bank->sectors[i].size);
297 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
298 }
299 }
300 if (((offset + count) > bank->sectors[i].offset) &&
301 ((offset + count) <= (bank->sectors[i].offset + bank->sectors[i].size)) &&
302 (last_sector == 0xffffffff))
303 last_sector = i;
304 }
305
306 /* Range check... */
307 if (first_sector == 0xffffffff || last_sector == 0xffffffff) {
308 LOG_INFO("Range check failed %" PRIx32 " %" PRIx32 "", offset, count);
309 return ERROR_FLASH_DST_OUT_OF_BANK;
310 }
311
312 /* Configure the flash controller timing */
313 lpc288x_set_flash_clk(bank);
314
315 /* initialise the offsets */
316 source_offset = 0;
317 dest_offset = 0;
318
319 for (sector = first_sector; sector <= last_sector; sector++) {
320 for (page = 0; page < bank->sectors[sector].size / FLASH_PAGE_SIZE; page++) {
321 if (bytes_remaining == 0) {
322 count = 0;
323 memset(page_buffer, 0xFF, FLASH_PAGE_SIZE);
324 } else if (bytes_remaining < FLASH_PAGE_SIZE) {
325 count = bytes_remaining;
326 memset(page_buffer, 0xFF, FLASH_PAGE_SIZE);
327 memcpy(page_buffer, &buffer[source_offset], count);
328 } else {
329 count = FLASH_PAGE_SIZE;
330 memcpy(page_buffer, &buffer[source_offset], count);
331 }
332
333 /* Wait for flash to become ready */
334 if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
335 return ERROR_FLASH_OPERATION_FAILED;
336
337 /* fill flash data latches with 1's */
338 target_write_u32(target, F_CTRL, FC_CS | FC_SET_DATA | FC_WEN | FC_FUNC);
339
340 target_write_u32(target, F_CTRL, FC_CS | FC_WEN | FC_FUNC);
341
342 if (target_write_buffer(target, offset + dest_offset, FLASH_PAGE_SIZE,
343 page_buffer) != ERROR_OK) {
344 LOG_INFO("Write to flash buffer failed");
345 return ERROR_FLASH_OPERATION_FAILED;
346 }
347
348 dest_offset += FLASH_PAGE_SIZE;
349 source_offset += count;
350 bytes_remaining -= count;
351
352 lpc288x_load_timer(LOAD_TIMER_WRITE, target);
353
354 target_write_u32(target, F_CTRL, FC_PROG_REQ | FC_PROTECT | FC_FUNC |
355 FC_CS);
356 }
357 }
358
359 return ERROR_OK;
360 }
361
362 static int lpc288x_probe(struct flash_bank *bank)
363 {
364 /* we only deal with LPC2888 so flash config is fixed */
365 struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
366 int retval;
367
368 if (lpc288x_info->cidr != 0)
369 return ERROR_OK;/* already probed */
370
371 if (bank->target->state != TARGET_HALTED) {
372 LOG_ERROR("Target not halted");
373 return ERROR_TARGET_NOT_HALTED;
374 }
375
376 retval = lpc288x_read_part_info(bank);
377 if (retval != ERROR_OK)
378 return retval;
379 return ERROR_OK;
380 }
381
382 static int lpc288x_protect(struct flash_bank *bank, int set, int first, int last)
383 {
384 int lockregion, status;
385 uint32_t value;
386 struct target *target = bank->target;
387
388 /* probed? halted? */
389 status = lpc288x_system_ready(bank);
390 if (status != ERROR_OK)
391 return status;
392
393 if ((first < 0) || (last < first) || (last >= bank->num_sectors))
394 return ERROR_FLASH_SECTOR_INVALID;
395
396 /* Configure the flash controller timing */
397 lpc288x_set_flash_clk(bank);
398
399 for (lockregion = first; lockregion <= last; lockregion++) {
400 if (set) {
401 /* write an odd value to base addy to protect... */
402 value = 0x01;
403 } else {
404 /* write an even value to base addy to unprotect... */
405 value = 0x00;
406 }
407 target_write_u32(target, bank->sectors[lockregion].offset, value);
408 target_write_u32(target, F_CTRL, FC_LOAD_REQ | FC_PROTECT | FC_WEN | FC_FUNC |
409 FC_CS);
410 }
411
412 return ERROR_OK;
413 }
414
415 struct flash_driver lpc288x_flash = {
416 .name = "lpc288x",
417 .flash_bank_command = lpc288x_flash_bank_command,
418 .erase = lpc288x_erase,
419 .protect = lpc288x_protect,
420 .write = lpc288x_write,
421 .read = default_flash_read,
422 .probe = lpc288x_probe,
423 .auto_probe = lpc288x_probe,
424 .erase_check = default_flash_blank_check,
425 .protect_check = lpc288x_protect_check,
426 .free_driver_priv = default_flash_free_driver_priv,
427 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)