You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

362 lines
14 KiB

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