- Fixes '=' whitespace
[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 "target.h"
30 #include "log.h"
31
32 struct image_s;
33
34 #define FLASH_MAX_ERROR_STR     (128)
35
36 /**
37  * Describes the geometry and status of a single flash sector
38  * within a flash bank.  A single bank typically consists of multiple
39  * sectors, each of which can be erased and protected independently.
40  */
41 typedef struct flash_sector_s
42 {
43         /// Bus offset from start of the flash chip (in bytes).
44         uint32_t offset;
45         /// Number of bytes in this flash sector.
46         uint32_t size;
47         /**
48          * Indication of erasure status: 0 = not erased, 1 = erased,
49          * other = unknown.  Set by @c flash_driver_s::erase_check.
50          */
51         int is_erased;
52         /**
53          * Indication of protection status: 0 = unprotected/unlocked,
54          * 1 = protected/locked, other = unknown.  Set by
55          * @c flash_driver_s::protect_check.
56          */
57         int is_protected;
58 } flash_sector_t;
59
60 struct flash_bank_s;
61
62 /**
63  * @brief Provides the implementation-independent structure that defines
64  * all of the callbacks required by OpenOCD flash drivers.
65  *
66  * Driver authors must implement the routines defined here, providing an
67  * instance with the fields filled out.  After that, the instance must
68  * be registered in flash.c, so it can be used by the driver lookup system.
69  *
70  * Specifically, the user can issue the command: @par
71  * @code
72  * flash bank DRIVERNAME ...parameters...
73  * @endcode
74  *
75  * OpenOCD will search for the driver with a @c flash_driver_s::name
76  * that matches @c DRIVERNAME.
77  *
78  * The flash subsystem calls some of the other drivers routines a using
79  * corresponding static <code>flash_driver_<i>callback</i>()</code>
80  * routine in flash.c.
81  */
82 typedef struct flash_driver_s
83 {
84         /** 
85          * Gives a human-readable name of this flash driver,
86          * This field is used to select and initialize the driver.
87          */
88         char *name;
89
90         /** 
91          * Registers driver-specific commands.  When called (during the
92          * "flash bank" command), the driver may register addition
93          * commands to support new flash chip functions.
94          *
95          * @returns ERROR_OK if successful; otherwise, an error code.
96          */
97         int (*register_commands)(struct command_context_s *cmd_ctx);
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 flash_bank_t::driver_priv field.
104          * 
105          * @param cmd_ctx - the command context
106          * @param cmd     - the command, in this case 'flash'
107          * @param args    - parameters, see below
108          * @param argc    - number of parameters on command line
109          * @param bank    - new filled in flash bank.
110          *
111          * The args are: @par
112          * @code
113          * args[0] = bank
114          * args[1] = drivername {name above}
115          * args[2] = baseaddress 
116          * args[3] = lengthbytes
117          * args[4] = chip_width_in bytes
118          * args[5] = bus_width_bytes
119          * args[6] = driver-specific parameters
120          * @endcode
121          *
122          * For example, args[4] = 16 bit flash, args[5] = 32bit bus.
123          *
124          * If extra arguments are provided (@a argc > 6), they will
125          * start in @a args[6].  These can be used to implement
126          * driver-specific extensions.
127          *
128          * @returns ERROR_OK if successful; otherwise, an error code.
129          */
130         int (*flash_bank_command)(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
131
132         /** 
133          * Bank/sector erase routine (target-specific).  When
134          * called, the flash driver should erase the specified sectors
135          * using whatever means are at its disposal.
136          *
137          * @param bank The bank of flash to be erased.
138          * @param first The number of the first sector to erase, typically 0.
139          * @param last The number of the last sector to erase, typically N-1.
140          * @returns ERROR_OK if successful; otherwise, an error code.
141          */
142         int (*erase)(struct flash_bank_s *bank, int first, int last);
143
144         /** 
145          * Bank/sector protection routine (target-specific).
146          * When called, the driver should disable 'flash write' bits (or
147          * enable 'erase protection' bits) for the given @a bank and @a
148          * sectors.
149          *
150          * @param bank The bank to protect or unprotect.
151          * @param set If non-zero, enable protection; if 0, disable it.
152          * @param first The first sector to (un)protect, typicaly 0.
153          * @param last The last sector to (un)project, typically N-1.
154          * @returns ERROR_OK if successful; otherwise, an error code.
155          */
156         int (*protect)(struct flash_bank_s *bank, int set, int first, int last);
157
158         /** 
159          * Program data into the flash.  Note CPU address will be
160          * "bank->base + offset", while the physical address is
161          * dependent upon current target MMU mappings.
162          *
163          * @param bank The bank to program
164          * @param buffer The data bytes to write.
165          * @param offset The offset into the chip to program.
166          * @param count The number of bytes to write.
167          * @returns ERROR_OK if successful; otherwise, an error code.
168          */
169         int (*write)(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
170
171         /** 
172          * Probe to determine what kind of flash is present.
173          * This is invoked by the "probe" script command.
174          *
175          * @param bank The bank to probe
176          * @returns ERROR_OK if successful; otherwise, an error code.
177          */
178         int (*probe)(struct flash_bank_s *bank);
179         
180         /** 
181          * Check the erasure status of a flash bank.
182          * When called, the driver routine must perform the required
183          * checks and then set the @c flash_sector_s::is_erased field
184          * for each of the flash banks's sectors.
185          *
186          * @param bank The bank to check
187          * @returns ERROR_OK if successful; otherwise, an error code.
188          */
189         int (*erase_check)(struct flash_bank_s *bank);
190
191         /**
192          * Determine if the specific bank is "protected" or not.
193          * When called, the driver routine must must perform the
194          * required protection check(s) and then set the @c
195          * flash_sector_s::is_protected field for each of the flash
196          * bank's sectors.
197          *
198          * @param bank - the bank to check
199          * @returns ERROR_OK if successful; otherwise, an error code.
200          */
201         int (*protect_check)(struct flash_bank_s *bank);
202
203         /**
204          * Display human-readable information about the flash
205          * bank into the given buffer.  Drivers must be careful to avoid
206          * overflowing the buffer.
207          *
208          * @param bank - the bank to get info about
209          * @param char - where to put the text for the human to read
210          * @param buf_size - the size of the human buffer.
211          * @returns ERROR_OK if successful; otherwise, an error code.
212          */ 
213         int (*info)(struct flash_bank_s *bank, char *buf, int buf_size);
214
215         /**
216          * A more gentle flavor of filash_driver_s::probe, performing
217          * setup with less noise.  Generally, driver routines should test
218          * to seee if the bank has already been probed; if it has, the
219          * driver probably should not perform its probe a second time.
220          *
221          * This callback is often called from the inside of other
222          * routines (e.g. GDB flash downloads) to autoprobe the flash as
223          * it is programing the flash.
224          *
225          * @param bank - the bank to probe
226          * @returns ERROR_OK if successful; otherwise, an error code.
227          */
228         int (*auto_probe)(struct flash_bank_s *bank);
229 } flash_driver_t;
230
231 /** 
232  * Provides details of a flash bank, available either on-chip or through
233  * a major interface.
234  *
235  * This structure will be passed as a parameter to the callbacks in the
236  * flash_driver_s structure, some of which may modify the contents of
237  * this structure of the area of flash that it defines.  Driver writers
238  * may use the @c driver_priv member to store additional data on a
239  * per-bank basis, if required.
240  */
241 typedef struct flash_bank_s
242 {
243         struct target_s *target; /**< Target to which this bank belongs. */
244
245         flash_driver_t *driver; /**< Driver for this bank. */
246         void *driver_priv; /**< Private driver storage pointer */
247
248         int bank_number; /**< The 'bank' (or chip number) of this instance. */
249         uint32_t base; /**< The base address of this bank */
250         uint32_t size; /**< The size of this chip bank, in bytes */
251
252         int chip_width; /**< Width of the chip in bytes (1,2,4 bytes) */
253         int bus_width; /**< Maximum bus width, in bytes (1,2,4 bytes) */
254
255         /**
256          * The number of sectors on this chip.  This value will
257          * be set intially to 0, and the flash driver must set this to
258          * some non-zero value during "probe()" or "auto_probe()".
259          */
260         int num_sectors;
261         /// Array of sectors, allocated and initilized by the flash driver
262         flash_sector_t *sectors;
263
264         struct flash_bank_s *next; /**< The next flash bank on this chip */
265 } flash_bank_t;
266
267 /// Registers the 'flash' subsystem commands
268 extern int flash_register_commands(struct command_context_s *cmd_ctx);
269 /// Initializes the 'flash' subsystem drivers
270 extern int flash_init_drivers(struct command_context_s *cmd_ctx);
271
272 /**
273  * Erases @a length bytes in the @a target flash, starting at @a addr.
274  * @returns ERROR_OK if successful; otherwise, an error code.
275  */
276 extern int flash_erase_address_range(struct target_s *target, 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 extern int flash_write(struct target_s *target, struct image_s *image, uint32_t *written, int erase);
288 /**
289  * Forces targets to re-examine their erase/protection state.
290  * This routine must be called when the system may modify the status.
291  */
292 extern void flash_set_dirty(void);
293 /// @returns The number of flash banks currently defined.
294 extern int flash_get_bank_count(void);
295 /**
296  * Provides default erased-bank check handling. Checks to see if
297  * the flash driver knows they are erased; if things look uncertain,
298  * this routine will call default_flash_mem_blank_check() to confirm.
299  * @returns ERROR_OK if successful; otherwise, an error code.
300  */
301 extern int default_flash_blank_check(struct flash_bank_s *bank);
302 /**
303  * Provides a default blank flash memory check.  Ensures the contents
304  * of the given bank have truly been erased.
305  * @param bank The flash bank.
306  * @returns ERROR_OK if successful; otherwise, an error code.
307  */
308 extern int default_flash_mem_blank_check(struct flash_bank_s *bank);
309
310 /**
311  * Returns a flash bank by the specified flash_bank_s bank_number, @a num.
312  * @param num The flash bank number.
313  * @returns A flash_bank_t for flash bank @a num, or NULL
314  */
315 extern flash_bank_t *get_flash_bank_by_num(int num);
316 /**
317  * Returns the flash bank like get_flash_bank_by_num(), without probing.
318  * @param num The flash bank number.
319  * @returns A flash_bank_t for flash bank @a num, or NULL.
320  */
321 extern flash_bank_t *get_flash_bank_by_num_noprobe(int num);
322 /**
323  * Returns the flash bank located at a specified address.
324  * @param target The target, presumed to contain one or more banks.
325  * @param addr An address that is within the range of the bank.
326  * @returns The flash_bank_t located at @a addr, or NULL.
327  */
328 extern flash_bank_t *get_flash_bank_by_addr(struct target_s *target, uint32_t addr);
329
330 #define ERROR_FLASH_BANK_INVALID                        (-900)
331 #define ERROR_FLASH_SECTOR_INVALID                      (-901)
332 #define ERROR_FLASH_OPERATION_FAILED            (-902)
333 #define ERROR_FLASH_DST_OUT_OF_BANK                     (-903)
334 #define ERROR_FLASH_DST_BREAKS_ALIGNMENT        (-904)
335 #define ERROR_FLASH_BUSY                                        (-905)
336 #define ERROR_FLASH_SECTOR_NOT_ERASED           (-906)
337 #define ERROR_FLASH_BANK_NOT_PROBED                     (-907)
338
339 #endif /* FLASH_H */