]> git.gag.com Git - fw/openocd/blob - src/flash/flash.h
8cd50f68253cade7cd92c5afa336ebb2d9a963d6
[fw/openocd] / src / flash / flash.h
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                 *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   Copyright (C) 2008 by Spencer Oliver                                  *
9  *   spen@spen-soft.co.uk                                                  *
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  *   This program is distributed in the hope that it will be useful,       *
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19  *   GNU General Public License for more details.                          *
20  *                                                                         *
21  *   You should have received a copy of the GNU General Public License     *
22  *   along with this program; if not, write to the                         *
23  *   Free Software Foundation, Inc.,                                       *
24  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
25  ***************************************************************************/
26 #ifndef FLASH_H
27 #define FLASH_H
28
29 #include <flash/common.h>
30
31 struct image;
32
33 #define FLASH_MAX_ERROR_STR     (128)
34
35 /**
36  * Describes the geometry and status of a single flash sector
37  * within a flash bank.  A single bank typically consists of multiple
38  * sectors, each of which can be erased and protected independently.
39  */
40 struct flash_sector
41 {
42         /// Bus offset from start of the flash chip (in bytes).
43         uint32_t offset;
44         /// Number of bytes in this flash sector.
45         uint32_t size;
46         /**
47          * Indication of erasure status: 0 = not erased, 1 = erased,
48          * other = unknown.  Set by @c flash_driver_s::erase_check.
49          */
50         int is_erased;
51         /**
52          * Indication of protection status: 0 = unprotected/unlocked,
53          * 1 = protected/locked, other = unknown.  Set by
54          * @c flash_driver_s::protect_check.
55          */
56         int is_protected;
57 };
58
59 struct flash_bank;
60
61 #define __FLASH_BANK_COMMAND(name) \
62                 COMMAND_HELPER(name, struct flash_bank *bank)
63
64 /**
65  * @brief Provides the implementation-independent structure that defines
66  * all of the callbacks required by OpenOCD flash drivers.
67  *
68  * Driver authors must implement the routines defined here, providing an
69  * instance with the fields filled out.  After that, the instance must
70  * be registered in flash.c, so it can be used by the driver lookup system.
71  *
72  * Specifically, the user can issue the command: @par
73  * @code
74  * flash bank DRIVERNAME ...parameters...
75  * @endcode
76  *
77  * OpenOCD will search for the driver with a @c flash_driver_s::name
78  * that matches @c DRIVERNAME.
79  *
80  * The flash subsystem calls some of the other drivers routines a using
81  * corresponding static <code>flash_driver_<i>callback</i>()</code>
82  * routine in flash.c.
83  */
84 struct flash_driver
85 {
86         /**
87          * Gives a human-readable name of this flash driver,
88          * This field is used to select and initialize the driver.
89          */
90         char *name;
91
92         /**
93          * An array of driver-specific commands to register.  When called
94          * during the "flash bank" command, the driver can register addition
95          * commands to support new flash chip functions.
96          */
97         const struct command_registration *commands;
98
99         /**
100          * Finish the "flash bank" command for @a bank.  The
101          * @a bank parameter will have been filled in by the core flash
102          * layer when this routine is called, and the driver can store
103          * additional information in its struct flash_bank::driver_priv field.
104          *
105          * The CMD_ARGV are: @par
106          * @code
107          * CMD_ARGV[0] = bank
108          * CMD_ARGV[1] = drivername {name above}
109          * CMD_ARGV[2] = baseaddress
110          * CMD_ARGV[3] = lengthbytes
111          * CMD_ARGV[4] = chip_width_in bytes
112          * CMD_ARGV[5] = bus_width_bytes
113          * CMD_ARGV[6] = driver-specific parameters
114          * @endcode
115          *
116          * For example, CMD_ARGV[4] = 16 bit flash, CMD_ARGV[5] = 32bit bus.
117          *
118          * If extra arguments are provided (@a CMD_ARGC > 6), they will
119          * start in @a CMD_ARGV[6].  These can be used to implement
120          * driver-specific extensions.
121          *
122          * @returns ERROR_OK if successful; otherwise, an error code.
123          */
124         __FLASH_BANK_COMMAND((*flash_bank_command));
125
126         /**
127          * Bank/sector erase routine (target-specific).  When
128          * called, the flash driver should erase the specified sectors
129          * using whatever means are at its disposal.
130          *
131          * @param bank The bank of flash to be erased.
132          * @param first The number of the first sector to erase, typically 0.
133          * @param last The number of the last sector to erase, typically N-1.
134          * @returns ERROR_OK if successful; otherwise, an error code.
135          */
136         int (*erase)(struct flash_bank *bank, int first, int last);
137
138         /**
139          * Bank/sector protection routine (target-specific).
140          * When called, the driver should disable 'flash write' bits (or
141          * enable 'erase protection' bits) for the given @a bank and @a
142          * sectors.
143          *
144          * @param bank The bank to protect or unprotect.
145          * @param set If non-zero, enable protection; if 0, disable it.
146          * @param first The first sector to (un)protect, typicaly 0.
147          * @param last The last sector to (un)project, typically N-1.
148          * @returns ERROR_OK if successful; otherwise, an error code.
149          */
150         int (*protect)(struct flash_bank *bank, int set, int first, int last);
151
152         /**
153          * Program data into the flash.  Note CPU address will be
154          * "bank->base + offset", while the physical address is
155          * dependent upon current target MMU mappings.
156          *
157          * @param bank The bank to program
158          * @param buffer The data bytes to write.
159          * @param offset The offset into the chip to program.
160          * @param count The number of bytes to write.
161          * @returns ERROR_OK if successful; otherwise, an error code.
162          */
163         int (*write)(struct flash_bank *bank,
164                         uint8_t *buffer, uint32_t offset, uint32_t count);
165
166         /**
167          * Probe to determine what kind of flash is present.
168          * This is invoked by the "probe" script command.
169          *
170          * @param bank The bank to probe
171          * @returns ERROR_OK if successful; otherwise, an error code.
172          */
173         int (*probe)(struct flash_bank *bank);
174
175         /**
176          * Check the erasure status of a flash bank.
177          * When called, the driver routine must perform the required
178          * checks and then set the @c flash_sector_s::is_erased field
179          * for each of the flash banks's sectors.
180          *
181          * @param bank The bank to check
182          * @returns ERROR_OK if successful; otherwise, an error code.
183          */
184         int (*erase_check)(struct flash_bank *bank);
185
186         /**
187          * Determine if the specific bank is "protected" or not.
188          * When called, the driver routine must must perform the
189          * required protection check(s) and then set the @c
190          * flash_sector_s::is_protected field for each of the flash
191          * bank's sectors.
192          *
193          * @param bank - the bank to check
194          * @returns ERROR_OK if successful; otherwise, an error code.
195          */
196         int (*protect_check)(struct flash_bank *bank);
197
198         /**
199          * Display human-readable information about the flash
200          * bank into the given buffer.  Drivers must be careful to avoid
201          * overflowing the buffer.
202          *
203          * @param bank - the bank to get info about
204          * @param char - where to put the text for the human to read
205          * @param buf_size - the size of the human buffer.
206          * @returns ERROR_OK if successful; otherwise, an error code.
207          */
208         int (*info)(struct flash_bank *bank, char *buf, int buf_size);
209
210         /**
211          * A more gentle flavor of filash_driver_s::probe, performing
212          * setup with less noise.  Generally, driver routines should test
213          * to seee if the bank has already been probed; if it has, the
214          * driver probably should not perform its probe a second time.
215          *
216          * This callback is often called from the inside of other
217          * routines (e.g. GDB flash downloads) to autoprobe the flash as
218          * it is programing the flash.
219          *
220          * @param bank - the bank to probe
221          * @returns ERROR_OK if successful; otherwise, an error code.
222          */
223         int (*auto_probe)(struct flash_bank *bank);
224 };
225
226 #define FLASH_BANK_COMMAND_HANDLER(name) static __FLASH_BANK_COMMAND(name)
227
228 /**
229  * Provides details of a flash bank, available either on-chip or through
230  * a major interface.
231  *
232  * This structure will be passed as a parameter to the callbacks in the
233  * flash_driver_s structure, some of which may modify the contents of
234  * this structure of the area of flash that it defines.  Driver writers
235  * may use the @c driver_priv member to store additional data on a
236  * per-bank basis, if required.
237  */
238 struct flash_bank
239 {
240         char *name;
241
242         struct target *target; /**< Target to which this bank belongs. */
243
244         struct flash_driver *driver; /**< Driver for this bank. */
245         void *driver_priv; /**< Private driver storage pointer */
246
247         int bank_number; /**< The 'bank' (or chip number) of this instance. */
248         uint32_t base; /**< The base address of this bank */
249         uint32_t size; /**< The size of this chip bank, in bytes */
250
251         int chip_width; /**< Width of the chip in bytes (1,2,4 bytes) */
252         int bus_width; /**< Maximum bus width, in bytes (1,2,4 bytes) */
253
254         /**
255          * The number of sectors on this chip.  This value will
256          * be set intially to 0, and the flash driver must set this to
257          * some non-zero value during "probe()" or "auto_probe()".
258          */
259         int num_sectors;
260         /// Array of sectors, allocated and initilized by the flash driver
261         struct flash_sector *sectors;
262
263         struct flash_bank *next; /**< The next flash bank on this chip */
264 };
265
266 /// Registers the 'flash' subsystem commands
267 int flash_register_commands(struct command_context *cmd_ctx);
268 /// Initializes the 'flash' subsystem drivers
269 int flash_init_drivers(struct command_context *cmd_ctx);
270
271 /**
272  * Erases @a length bytes in the @a target flash, starting at @a addr.
273  * @returns ERROR_OK if successful; otherwise, an error code.
274  */
275 int flash_erase_address_range(struct target *target,
276                 uint32_t addr, uint32_t length);
277 /**
278  * Writes @a image into the @a target flash.  The @a written parameter
279  * will contain the
280  * @param target The target with the flash to be programmed.
281  * @param image The image that will be programmed to flash.
282  * @param written On return, contains the number of bytes written.
283  * @param erase If non-zero, indicates the flash driver should first
284  * erase the corresponding banks or sectors before programming.
285  * @returns ERROR_OK if successful; otherwise, an error code.
286  */
287 int flash_write(struct target *target,
288                 struct image *image, uint32_t *written, int erase);
289 /**
290  * Forces targets to re-examine their erase/protection state.
291  * This routine must be called when the system may modify the status.
292  */
293 void flash_set_dirty(void);
294 /// @returns The number of flash banks currently defined.
295 int flash_get_bank_count(void);
296 /**
297  * Provides default erased-bank check handling. Checks to see if
298  * the flash driver knows they are erased; if things look uncertain,
299  * this routine will call default_flash_mem_blank_check() to confirm.
300  * @returns ERROR_OK if successful; otherwise, an error code.
301  */
302 int default_flash_blank_check(struct flash_bank *bank);
303 /**
304  * Provides a default blank flash memory check.  Ensures the contents
305  * of the given bank have truly been erased.
306  * @param bank The flash bank.
307  * @returns ERROR_OK if successful; otherwise, an error code.
308  */
309 int default_flash_mem_blank_check(struct flash_bank *bank);
310
311 /**
312  * Returns the flash bank specified by @a name, which matches the
313  * driver name and a suffix (option) specify the driver-specific
314  * bank number. The suffix consists of the '.' and the driver-specific
315  * bank number: when two str9x banks are defined, then 'str9x.1' refers
316  * to the second.
317  */
318 struct flash_bank *get_flash_bank_by_name(const char *name);
319 /**
320  * Returns a flash bank by the specified flash_bank_s bank_number, @a num.
321  * @param num The flash bank number.
322  * @returns A struct flash_bank for flash bank @a num, or NULL
323  */
324 struct flash_bank *get_flash_bank_by_num(int num);
325 /**
326  * Retreives @a bank from a command argument, reporting errors parsing
327  * the bank identifier or retreiving the specified bank.  The bank
328  * may be identified by its bank number or by @c name.instance, where
329  * @a instance is driver-specific.
330  * @param name_index The index to the string in args containing the
331  * bank identifier.
332  * @param bank On output, contians a pointer to the bank or NULL.
333  * @returns ERROR_OK on success, or an error indicating the problem.
334  */
335 COMMAND_HELPER(flash_command_get_bank, unsigned name_index,
336                 struct flash_bank **bank);
337 /**
338  * Returns the flash bank like get_flash_bank_by_num(), without probing.
339  * @param num The flash bank number.
340  * @returns A struct flash_bank for flash bank @a num, or NULL.
341  */
342 struct flash_bank *get_flash_bank_by_num_noprobe(int num);
343 /**
344  * Returns the flash bank located at a specified address.
345  * @param target The target, presumed to contain one or more banks.
346  * @param addr An address that is within the range of the bank.
347  * @returns The struct flash_bank located at @a addr, or NULL.
348  */
349 struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr);
350
351 #endif /* FLASH_H */