warnings: use more 'const' for char *
[fw/openocd] / src / flash / nor / driver.h
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de>              *
3  *   Copyright (C) 2007,2008 Ã˜yvind Harboe <oyvind.harboe@zylin.com>       *
4  *   Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk>           *
5  *   Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net>             *
6  *   Copyright (C) 2010 by Antonio Borneo <borneo.antonio@gmail.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_NOR_DRIVER_H
24 #define FLASH_NOR_DRIVER_H
25
26 struct flash_bank;
27
28 #define __FLASH_BANK_COMMAND(name) \
29                 COMMAND_HELPER(name, struct flash_bank *bank)
30
31 /**
32  * @brief Provides the implementation-independent structure that defines
33  * all of the callbacks required by OpenOCD flash drivers.
34  *
35  * Driver authors must implement the routines defined here, providing an
36  * instance with the fields filled out.  After that, the instance must
37  * be registered in flash.c, so it can be used by the driver lookup system.
38  *
39  * Specifically, the user can issue the command: @par
40  * @code
41  * flash bank DRIVERNAME ...parameters...
42  * @endcode
43  *
44  * OpenOCD will search for the driver with a @c flash_driver_s::name
45  * that matches @c DRIVERNAME.
46  *
47  * The flash subsystem calls some of the other drivers routines a using
48  * corresponding static <code>flash_driver_<i>callback</i>()</code>
49  * routine in flash.c.
50  */
51 struct flash_driver
52 {
53         /**
54          * Gives a human-readable name of this flash driver,
55          * This field is used to select and initialize the driver.
56          */
57         const char *name;
58
59         /**
60          * An array of driver-specific commands to register.  When called
61          * during the "flash bank" command, the driver can register addition
62          * commands to support new flash chip functions.
63          */
64         const struct command_registration *commands;
65
66         /**
67          * Finish the "flash bank" command for @a bank.  The
68          * @a bank parameter will have been filled in by the core flash
69          * layer when this routine is called, and the driver can store
70          * additional information in its struct flash_bank::driver_priv field.
71          *
72          * The CMD_ARGV are: @par
73          * @code
74          * CMD_ARGV[0] = bank
75          * CMD_ARGV[1] = drivername {name above}
76          * CMD_ARGV[2] = baseaddress
77          * CMD_ARGV[3] = lengthbytes
78          * CMD_ARGV[4] = chip_width_in bytes
79          * CMD_ARGV[5] = bus_width_in_bytes
80          * CMD_ARGV[6] = driver-specific parameters
81          * @endcode
82          *
83          * For example, CMD_ARGV[4] = 2 (for 16 bit flash),
84          *      CMD_ARGV[5] = 4 (for 32 bit bus).
85          *
86          * If extra arguments are provided (@a CMD_ARGC > 6), they will
87          * start in @a CMD_ARGV[6].  These can be used to implement
88          * driver-specific extensions.
89          *
90          * @returns ERROR_OK if successful; otherwise, an error code.
91          */
92         __FLASH_BANK_COMMAND((*flash_bank_command));
93
94         /**
95          * Bank/sector erase routine (target-specific).  When
96          * called, the flash driver should erase the specified sectors
97          * using whatever means are at its disposal.
98          *
99          * @param bank The bank of flash to be erased.
100          * @param first The number of the first sector to erase, typically 0.
101          * @param last The number of the last sector to erase, typically N-1.
102          * @returns ERROR_OK if successful; otherwise, an error code.
103          */
104         int (*erase)(struct flash_bank *bank, int first, int last);
105
106         /**
107          * Bank/sector protection routine (target-specific).
108          *
109          * When called, the driver should enable/disable protection
110          * for MINIMUM the range covered by first..last sectors
111          * inclusive. Some chips have alignment requirements will
112          * cause the actual range to be protected / unprotected to
113          * be larger than the first..last range.
114          *
115          * @param bank The bank to protect or unprotect.
116          * @param set If non-zero, enable protection; if 0, disable it.
117          * @param first The first sector to (un)protect, typicaly 0.
118          * @param last The last sector to (un)project, typically N-1.
119          * @returns ERROR_OK if successful; otherwise, an error code.
120          */
121         int (*protect)(struct flash_bank *bank, int set, int first, int last);
122
123         /**
124          * Program data into the flash.  Note CPU address will be
125          * "bank->base + offset", while the physical address is
126          * dependent upon current target MMU mappings.
127          *
128          * @param bank The bank to program
129          * @param buffer The data bytes to write.
130          * @param offset The offset into the chip to program.
131          * @param count The number of bytes to write.
132          * @returns ERROR_OK if successful; otherwise, an error code.
133          */
134         int (*write)(struct flash_bank *bank,
135                         uint8_t *buffer, uint32_t offset, uint32_t count);
136
137         /**
138          * Read data from the flash. Note CPU address will be
139          * "bank->base + offset", while the physical address is
140          * dependent upon current target MMU mappings.
141          *
142          * @param bank The bank to read.
143          * @param buffer The data bytes read.
144          * @param offset The offset into the chip to read.
145          * @param count The number of bytes to read.
146          * @returns ERROR_OK if successful; otherwise, an error code.
147          */
148          int (*read)(struct flash_bank *bank,
149                         uint8_t *buffer, uint32_t offset, uint32_t count);
150
151         /**
152          * Probe to determine what kind of flash is present.
153          * This is invoked by the "probe" script command.
154          *
155          * @param bank The bank to probe
156          * @returns ERROR_OK if successful; otherwise, an error code.
157          */
158         int (*probe)(struct flash_bank *bank);
159
160         /**
161          * Check the erasure status of a flash bank.
162          * When called, the driver routine must perform the required
163          * checks and then set the @c flash_sector_s::is_erased field
164          * for each of the flash banks's sectors.
165          *
166          * @param bank The bank to check
167          * @returns ERROR_OK if successful; otherwise, an error code.
168          */
169         int (*erase_check)(struct flash_bank *bank);
170
171         /**
172          * Determine if the specific bank is "protected" or not.
173          * When called, the driver routine must must perform the
174          * required protection check(s) and then set the @c
175          * flash_sector_s::is_protected field for each of the flash
176          * bank's sectors.
177          *
178          * @param bank - the bank to check
179          * @returns ERROR_OK if successful; otherwise, an error code.
180          */
181         int (*protect_check)(struct flash_bank *bank);
182
183         /**
184          * Display human-readable information about the flash
185          * bank into the given buffer.  Drivers must be careful to avoid
186          * overflowing the buffer.
187          *
188          * @param bank - the bank to get info about
189          * @param char - where to put the text for the human to read
190          * @param buf_size - the size of the human buffer.
191          * @returns ERROR_OK if successful; otherwise, an error code.
192          */
193         int (*info)(struct flash_bank *bank, char *buf, int buf_size);
194
195         /**
196          * A more gentle flavor of filash_driver_s::probe, performing
197          * setup with less noise.  Generally, driver routines should test
198          * to seee if the bank has already been probed; if it has, the
199          * driver probably should not perform its probe a second time.
200          *
201          * This callback is often called from the inside of other
202          * routines (e.g. GDB flash downloads) to autoprobe the flash as
203          * it is programing the flash.
204          *
205          * @param bank - the bank to probe
206          * @returns ERROR_OK if successful; otherwise, an error code.
207          */
208         int (*auto_probe)(struct flash_bank *bank);
209 };
210
211 #define FLASH_BANK_COMMAND_HANDLER(name) static __FLASH_BANK_COMMAND(name)
212
213 /**
214  * Find a NOR flash driver by its name.
215  * @param name The name of the requested driver.
216  * @returns The flash_driver called @c name, or NULL if not found.
217  */
218 struct flash_driver *flash_driver_find_by_name(const char *name);
219
220 #endif // FLASH_NOR_DRIVER_H