target/cortex_m: workaround Cortex-M7 erratum 3092511
[openocd.git] / src / flash / nor / faux.c
blobe76dc493fe085d50cc7eb7e9928f9beaea5699ff
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2009 Øyvind Harboe *
5 * oyvind.harboe@zylin.com *
6 ***************************************************************************/
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
12 #include "imp.h"
13 #include <target/image.h>
14 #include "hello.h"
16 struct faux_flash_bank {
17 struct target *target;
18 uint8_t *memory;
19 uint32_t start_address;
22 static const int sector_size = 0x10000;
25 /* flash bank faux <base> <size> <chip_width> <bus_width> <target#> <driverPath>
27 FLASH_BANK_COMMAND_HANDLER(faux_flash_bank_command)
29 struct faux_flash_bank *info;
31 if (CMD_ARGC < 6)
32 return ERROR_COMMAND_SYNTAX_ERROR;
34 info = malloc(sizeof(struct faux_flash_bank));
35 if (!info) {
36 LOG_ERROR("no memory for flash bank info");
37 return ERROR_FAIL;
39 info->memory = malloc(bank->size);
40 if (!info->memory) {
41 free(info);
42 LOG_ERROR("no memory for flash bank info");
43 return ERROR_FAIL;
45 bank->driver_priv = info;
47 /* Use 0x10000 as a fixed sector size. */
48 uint32_t offset = 0;
49 bank->num_sectors = bank->size/sector_size;
50 bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
51 for (unsigned int i = 0; i < bank->num_sectors; i++) {
52 bank->sectors[i].offset = offset;
53 bank->sectors[i].size = sector_size;
54 offset += bank->sectors[i].size;
55 bank->sectors[i].is_erased = -1;
56 bank->sectors[i].is_protected = 0;
59 info->target = get_target(CMD_ARGV[5]);
60 if (!info->target) {
61 LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
62 free(info->memory);
63 free(info);
64 return ERROR_FAIL;
66 return ERROR_OK;
69 static int faux_erase(struct flash_bank *bank, unsigned int first,
70 unsigned int last)
72 struct faux_flash_bank *info = bank->driver_priv;
73 memset(info->memory + first*sector_size, 0xff, sector_size*(last-first + 1));
74 return ERROR_OK;
77 static int faux_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
79 struct faux_flash_bank *info = bank->driver_priv;
80 memcpy(info->memory + offset, buffer, count);
81 return ERROR_OK;
84 static int faux_info(struct flash_bank *bank, struct command_invocation *cmd)
86 command_print_sameline(cmd, "faux flash driver");
87 return ERROR_OK;
90 static int faux_probe(struct flash_bank *bank)
92 return ERROR_OK;
95 static const struct command_registration faux_command_handlers[] = {
97 .name = "faux",
98 .mode = COMMAND_ANY,
99 .help = "faux flash command group",
100 .chain = hello_command_handlers,
101 .usage = "",
103 COMMAND_REGISTRATION_DONE
106 const struct flash_driver faux_flash = {
107 .name = "faux",
108 .commands = faux_command_handlers,
109 .flash_bank_command = faux_flash_bank_command,
110 .erase = faux_erase,
111 .write = faux_write,
112 .read = default_flash_read,
113 .probe = faux_probe,
114 .auto_probe = faux_probe,
115 .erase_check = default_flash_blank_check,
116 .info = faux_info,
117 .free_driver_priv = default_flash_free_driver_priv,