nor: use common flash driver
[openocd/ztw.git] / src / flash / nor / driver.h
blob7f4dfe38b25cea380fc3f2e8b026317775859a00
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 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
22 #ifndef FLASH_NOR_DRIVER_H
23 #define FLASH_NOR_DRIVER_H
25 struct flash_bank;
27 #include <flash/common.h>
29 /**
30 * @brief Provides the implementation-independent structure that defines
31 * all of the callbacks required by OpenOCD flash drivers.
33 * Driver authors must implement the routines defined here, providing an
34 * instance with the fields filled out. After that, the instance must
35 * be registered in flash.c, so it can be used by the driver lookup system.
37 * Specifically, the user can issue the command: @par
38 * @code
39 * flash bank name DRIVERNAME ...parameters...
40 * @endcode
41 * The arguments are: @par
42 * @code
43 * CMD_ARGV[0] = bank name
44 * CMD_ARGV[1] = driver name
45 * CMD_ARGV[2] = base address
46 * CMD_ARGV[3] = length bytes
47 * CMD_ARGV[4] = chip width in bytes
48 * CMD_ARGV[5] = bus width in bytes
49 * CMD_ARGV[6] = target id/name
50 * CMD_ARGV[7] = driver-specific parameters
51 * @endcode
53 * OpenOCD will search for the driver with a @c flash_driver_s::name
54 * that matches @c DRIVERNAME.
56 * The flash subsystem calls some of the other drivers routines a using
57 * corresponding static <code>flash_driver_<i>callback</i>()</code>
58 * routine in flash.c.
60 * For example, CMD_ARGV[4] = 16 bit flash, CMD_ARGV[5] = 32bit bus.
62 * If extra arguments are provided (@a CMD_ARGC > 7), they will
63 * start in @a CMD_ARGV[7]. These can be used to implement
64 * driver-specific extensions.
66 * @returns ERROR_OK if successful; otherwise, an error code.
69 struct flash_driver
71 /// common driver characteristics
72 struct driver driver;
74 /**
75 * Bank/sector erase routine (target-specific). When
76 * called, the flash driver should erase the specified sectors
77 * using whatever means are at its disposal.
79 * @param bank The bank of flash to be erased.
80 * @param first The number of the first sector to erase, typically 0.
81 * @param last The number of the last sector to erase, typically N-1.
82 * @returns ERROR_OK if successful; otherwise, an error code.
84 int (*erase)(struct flash_bank *bank, int first, int last);
86 /**
87 * Bank/sector protection routine (target-specific).
88 * When called, the driver should disable 'flash write' bits (or
89 * enable 'erase protection' bits) for the given @a bank and @a
90 * sectors.
92 * @param bank The bank to protect or unprotect.
93 * @param set If non-zero, enable protection; if 0, disable it.
94 * @param first The first sector to (un)protect, typicaly 0.
95 * @param last The last sector to (un)project, typically N-1.
96 * @returns ERROR_OK if successful; otherwise, an error code.
98 int (*protect)(struct flash_bank *bank, int set, int first, int last);
101 * Program data into the flash. Note CPU address will be
102 * "bank->base + offset", while the physical address is
103 * dependent upon current target MMU mappings.
105 * @param bank The bank to program
106 * @param buffer The data bytes to write.
107 * @param offset The offset into the chip to program.
108 * @param count The number of bytes to write.
109 * @returns ERROR_OK if successful; otherwise, an error code.
111 int (*write)(struct flash_bank *bank,
112 uint8_t *buffer, uint32_t offset, uint32_t count);
115 * Probe to determine what kind of flash is present.
116 * This is invoked by the "probe" script command.
118 * @param bank The bank to probe
119 * @returns ERROR_OK if successful; otherwise, an error code.
121 int (*probe)(struct flash_bank *bank);
124 * Check the erasure status of a flash bank.
125 * When called, the driver routine must perform the required
126 * checks and then set the @c flash_sector_s::is_erased field
127 * for each of the flash banks's sectors.
129 * @param bank The bank to check
130 * @returns ERROR_OK if successful; otherwise, an error code.
132 int (*erase_check)(struct flash_bank *bank);
135 * Determine if the specific bank is "protected" or not.
136 * When called, the driver routine must must perform the
137 * required protection check(s) and then set the @c
138 * flash_sector_s::is_protected field for each of the flash
139 * bank's sectors.
141 * @param bank - the bank to check
142 * @returns ERROR_OK if successful; otherwise, an error code.
144 int (*protect_check)(struct flash_bank *bank);
147 * Display human-readable information about the flash
148 * bank into the given buffer. Drivers must be careful to avoid
149 * overflowing the buffer.
151 * @param bank - the bank to get info about
152 * @param char - where to put the text for the human to read
153 * @param buf_size - the size of the human buffer.
154 * @returns ERROR_OK if successful; otherwise, an error code.
156 int (*info)(struct flash_bank *bank, char *buf, int buf_size);
159 * A more gentle flavor of filash_driver_s::probe, performing
160 * setup with less noise. Generally, driver routines should test
161 * to seee if the bank has already been probed; if it has, the
162 * driver probably should not perform its probe a second time.
164 * This callback is often called from the inside of other
165 * routines (e.g. GDB flash downloads) to autoprobe the flash as
166 * it is programing the flash.
168 * @param bank - the bank to probe
169 * @returns ERROR_OK if successful; otherwise, an error code.
171 int (*auto_probe)(struct flash_bank *bank);
174 #define FLASH_BANK_COMMAND_HANDLER(name) static OBJECT_SETUP_COMMAND(name)
176 #define flash_driver_name(__driver) (__driver)->driver.object.name
177 #define flash_driver_commands(__driver) (__driver)->driver.commands
178 #define flash_driver_bank_command(__driver) (__driver)->driver.setup
180 #define FLASH_DRIVER(__name, __setup, __commands, __args...) \
181 struct flash_driver __name##_flash = { \
182 .driver = { \
183 .object = { \
184 .name = stringify(__name), \
185 }, \
186 .setup = __setup, \
187 .commands = __commands, \
188 }, \
189 ##__args \
193 * Find a NOR flash driver by its name.
194 * @param name The name of the requested driver.
195 * @returns The flash_driver called @c name, or NULL if not found.
197 struct flash_driver *flash_driver_find_by_name(const char *name);
199 static inline struct flash_driver *
200 flash_driver_from_driver(struct driver *driver)
202 return container_of(driver, struct flash_driver, driver);
205 #endif // FLASH_NOR_DRIVER_H