807914ade81e1005ced809764db50495ed8f62d2
[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  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22  ***************************************************************************/
23 #ifndef FLASH_H
24 #define FLASH_H
25
26 #include "target.h"
27 #include "image.h"
28
29 #define FLASH_MAX_ERROR_STR     (128)
30
31 typedef struct flash_sector_s
32 {
33         u32 offset;
34         u32 size;
35         int is_erased;
36         int is_protected;
37 } flash_sector_t;
38
39 struct flash_bank_s;
40
41 typedef struct flash_driver_s
42 {
43         char *name;
44         int (*register_commands)(struct command_context_s *cmd_ctx);
45         int (*flash_bank_command)(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
46
47         /* use flash_driver_erase() wrapper to invoke */
48         int (*erase)(struct flash_bank_s *bank, int first, int last);
49
50         /* use flash_driver_protect() wrapper to invoke */
51         int (*protect)(struct flash_bank_s *bank, int set, int first, int last);
52
53         /* use the flash_driver_write() wrapper to invoke. */
54         int (*write)(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count);
55
56         int (*probe)(struct flash_bank_s *bank);
57         int (*erase_check)(struct flash_bank_s *bank);
58         int (*protect_check)(struct flash_bank_s *bank);
59         int (*info)(struct flash_bank_s *bank, char *buf, int buf_size);
60         int (*auto_probe)(struct flash_bank_s *bank);
61 } flash_driver_t;
62
63 typedef struct flash_bank_s
64 {
65         target_t *target;
66         flash_driver_t *driver;
67         void *driver_priv;
68         u32 base;
69         u32 size;
70         int chip_width;
71         int bus_width;
72         int num_sectors;
73         flash_sector_t *sectors;
74         struct flash_bank_s *next;
75 } flash_bank_t;
76
77 extern int flash_register_commands(struct command_context_s *cmd_ctx);
78 extern int flash_init_drivers(struct command_context_s *cmd_ctx);
79
80 extern int flash_erase_address_range(target_t *target, u32 addr, u32 length);
81 extern int flash_write(target_t *target, image_t *image, u32 *written, int erase);
82 extern void flash_set_dirty(void);
83 extern int flash_get_bank_count();
84 extern int default_flash_blank_check(struct flash_bank_s *bank);
85 extern int default_flash_mem_blank_check(struct flash_bank_s *bank);
86
87 extern flash_bank_t *get_flash_bank_by_num(int num);
88 extern flash_bank_t *get_flash_bank_by_num_noprobe(int num);
89 extern flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr);
90
91 #define         ERROR_FLASH_BANK_INVALID                (-900)
92 #define         ERROR_FLASH_SECTOR_INVALID              (-901)
93 #define         ERROR_FLASH_OPERATION_FAILED    (-902)
94 #define         ERROR_FLASH_DST_OUT_OF_BANK             (-903)
95 #define         ERROR_FLASH_DST_BREAKS_ALIGNMENT (-904)
96 #define         ERROR_FLASH_BUSY                                (-905)
97 #define         ERROR_FLASH_SECTOR_NOT_ERASED   (-906)
98 #define         ERROR_FLASH_BANK_NOT_PROBED             (-907)
99
100 #endif /* FLASH_H */