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