Added the faux flash driver and target. Used for testing.
[fw/openocd] / src / flash / faux.c
1 /***************************************************************************
2  *   Copyright (C) 2009 Ã˜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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "flash.h"
25 #include "image.h"
26
27
28 static int faux_register_commands(struct command_context_s *cmd_ctx);
29 static int faux_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
30 static int faux_erase(struct flash_bank_s *bank, int first, int last);
31 static int faux_protect(struct flash_bank_s *bank, int set, int first, int last);
32 static int faux_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
33 static int faux_probe(struct flash_bank_s *bank);
34 static int faux_protect_check(struct flash_bank_s *bank);
35 static int faux_info(struct flash_bank_s *bank, char *buf, int buf_size);
36
37 flash_driver_t faux_flash =
38 {
39         .name = "faux",
40         .register_commands = faux_register_commands,
41         .flash_bank_command = faux_flash_bank_command,
42         .erase = faux_erase,
43         .protect = faux_protect,
44         .write = faux_write,
45         .probe = faux_probe,
46         .auto_probe = faux_probe,
47         .erase_check = default_flash_blank_check,
48         .protect_check = faux_protect_check,
49         .info = faux_info
50 };
51
52 typedef struct faux_flash_bank_s
53 {
54         struct target_s *target;
55         uint8_t *memory;
56         uint32_t start_address;
57 } faux_flash_bank_t;
58
59 static const int sectorSize = 0x10000;
60
61
62 /* flash bank faux <base> <size> <chip_width> <bus_width> <target#> <driverPath>
63  */
64 static int faux_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
65 {
66         faux_flash_bank_t *info;
67
68         if (argc < 6)
69         {
70                 LOG_WARNING("incomplete flash_bank faux configuration");
71                 return ERROR_FLASH_BANK_INVALID;
72         }
73
74         info = malloc(sizeof(faux_flash_bank_t));
75         if (info == NULL)
76         {
77                 LOG_ERROR("no memory for flash bank info");
78                 return ERROR_FAIL;
79         }
80         info->memory = malloc(bank->size);
81         if (info == NULL)
82         {
83                 free(info);
84                 LOG_ERROR("no memory for flash bank info");
85                 return ERROR_FAIL;
86         }
87         bank->driver_priv = info;
88
89         /* Use 0x10000 as a fixed sector size. */
90         int i = 0;
91         uint32_t offset = 0;
92         bank->num_sectors = bank->size/sectorSize;
93         bank->sectors = malloc(sizeof(flash_sector_t) * bank->num_sectors);
94         for (i = 0; i < bank->num_sectors; i++)
95         {
96                 bank->sectors[i].offset = offset;
97                 bank->sectors[i].size = sectorSize;
98                 offset += bank->sectors[i].size;
99                 bank->sectors[i].is_erased = -1;
100                 bank->sectors[i].is_protected = 0;
101         }
102
103         info->target = get_target(args[5]);
104         if (info->target == NULL)
105         {
106                 LOG_ERROR("target '%s' not defined", args[5]);
107                 free(info->memory);
108                 free(info);
109                 return ERROR_FAIL;
110         }
111         return ERROR_OK;
112 }
113
114 static int faux_register_commands(struct command_context_s *cmd_ctx)
115 {
116         return ERROR_OK;
117 }
118
119 static int faux_erase(struct flash_bank_s *bank, int first, int last)
120 {
121         faux_flash_bank_t *info = bank->driver_priv;
122         memset(info->memory + first*sectorSize, 0xff, sectorSize*(last-first + 1));
123         return ERROR_OK;
124 }
125
126 static int faux_protect(struct flash_bank_s *bank, int set, int first, int last)
127 {
128         LOG_USER("set protection sector %d to %d to %s", first, last, set?"on":"off");
129         return ERROR_OK;
130 }
131
132 static int faux_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
133 {
134         faux_flash_bank_t *info = bank->driver_priv;
135         memcpy(info->memory + offset, buffer, count);
136         return ERROR_OK;
137 }
138
139 static int faux_protect_check(struct flash_bank_s *bank)
140 {
141         return ERROR_OK;
142 }
143
144 static int faux_info(struct flash_bank_s *bank, char *buf, int buf_size)
145 {
146         snprintf(buf, buf_size, "faux flash driver");
147         return ERROR_OK;
148 }
149
150 static int faux_probe(struct flash_bank_s *bank)
151 {
152         return ERROR_OK;
153 }