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.
 
 
 
 
 
 

340 lines
13 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 "log.h"
  30. struct image_s;
  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. typedef struct flash_sector_s
  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. } flash_sector_t;
  55. struct flash_bank_s;
  56. /**
  57. * @brief Provides the implementation-independent structure that defines
  58. * all of the callbacks required by OpenOCD flash drivers.
  59. *
  60. * Driver authors must implement the routines defined here, providing an
  61. * instance with the fields filled out. After that, the instance must
  62. * be registered in flash.c, so it can be used by the driver lookup system.
  63. *
  64. * Specifically, the user can issue the command: @par
  65. * @code
  66. * flash bank DRIVERNAME ...parameters...
  67. * @endcode
  68. *
  69. * OpenOCD will search for the driver with a @c flash_driver_s::name
  70. * that matches @c DRIVERNAME.
  71. *
  72. * The flash subsystem calls some of the other drivers routines a using
  73. * corresponding static <code > flash_driver_ < i>callback</i > ()</code>
  74. * routine in flash.c.
  75. */
  76. typedef struct flash_driver_s
  77. {
  78. /**
  79. * Gives a human-readable name of this flash driver,
  80. * This field is used to select and initialize the driver.
  81. */
  82. char *name;
  83. /**
  84. * Registers driver-specific commands. When called (during the
  85. * "flash bank" command), the driver may register addition
  86. * commands to support new flash chip functions.
  87. *
  88. * @returns ERROR_OK if successful; otherwise, an error code.
  89. */
  90. int (*register_commands)(struct command_context_s *cmd_ctx);
  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 flash_bank_t::driver_priv field.
  96. *
  97. * @param cmd_ctx - the command context
  98. * @param cmd - the command, in this case 'flash'
  99. * @param args - parameters, see below
  100. * @param argc - number of parameters on command line
  101. * @param bank - new filled in flash bank.
  102. *
  103. * The args are: @par
  104. * @code
  105. * args[0] = bank
  106. * args[1] = drivername {name above}
  107. * args[2] = baseaddress
  108. * args[3] = lengthbytes
  109. * args[4] = chip_width_in bytes
  110. * args[5] = bus_width_bytes
  111. * args[6] = driver-specific parameters
  112. * @endcode
  113. *
  114. * For example, args[4] = 16 bit flash, args[5] = 32bit bus.
  115. *
  116. * If extra arguments are provided (@a argc > 6), they will
  117. * start in @a args[6]. These can be used to implement
  118. * driver-specific extensions.
  119. *
  120. * @returns ERROR_OK if successful; otherwise, an error code.
  121. */
  122. int (*flash_bank_command)(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
  123. /**
  124. * Bank/sector erase routine (target-specific). When
  125. * called, the flash driver should erase the specified sectors
  126. * using whatever means are at its disposal.
  127. *
  128. * @param bank The bank of flash to be erased.
  129. * @param first The number of the first sector to erase, typically 0.
  130. * @param last The number of the last sector to erase, typically N-1.
  131. * @returns ERROR_OK if successful; otherwise, an error code.
  132. */
  133. int (*erase)(struct flash_bank_s *bank, int first, int last);
  134. /**
  135. * Bank/sector protection routine (target-specific).
  136. * When called, the driver should disable 'flash write' bits (or
  137. * enable 'erase protection' bits) for the given @a bank and @a
  138. * sectors.
  139. *
  140. * @param bank The bank to protect or unprotect.
  141. * @param set If non-zero, enable protection; if 0, disable it.
  142. * @param first The first sector to (un)protect, typicaly 0.
  143. * @param last The last sector to (un)project, typically N-1.
  144. * @returns ERROR_OK if successful; otherwise, an error code.
  145. */
  146. int (*protect)(struct flash_bank_s *bank, int set, int first, int last);
  147. /**
  148. * Program data into the 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 program
  153. * @param buffer The data bytes to write.
  154. * @param offset The offset into the chip to program.
  155. * @param count The number of bytes to write.
  156. * @returns ERROR_OK if successful; otherwise, an error code.
  157. */
  158. int (*write)(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
  159. /**
  160. * Probe to determine what kind of flash is present.
  161. * This is invoked by the "probe" script command.
  162. *
  163. * @param bank The bank to probe
  164. * @returns ERROR_OK if successful; otherwise, an error code.
  165. */
  166. int (*probe)(struct flash_bank_s *bank);
  167. /**
  168. * Check the erasure status of a flash bank.
  169. * When called, the driver routine must perform the required
  170. * checks and then set the @c flash_sector_s::is_erased field
  171. * for each of the flash banks's sectors.
  172. *
  173. * @param bank The bank to check
  174. * @returns ERROR_OK if successful; otherwise, an error code.
  175. */
  176. int (*erase_check)(struct flash_bank_s *bank);
  177. /**
  178. * Determine if the specific bank is "protected" or not.
  179. * When called, the driver routine must must perform the
  180. * required protection check(s) and then set the @c
  181. * flash_sector_s::is_protected field for each of the flash
  182. * bank's sectors.
  183. *
  184. * @param bank - the bank to check
  185. * @returns ERROR_OK if successful; otherwise, an error code.
  186. */
  187. int (*protect_check)(struct flash_bank_s *bank);
  188. /**
  189. * Display human-readable information about the flash
  190. * bank into the given buffer. Drivers must be careful to avoid
  191. * overflowing the buffer.
  192. *
  193. * @param bank - the bank to get info about
  194. * @param char - where to put the text for the human to read
  195. * @param buf_size - the size of the human buffer.
  196. * @returns ERROR_OK if successful; otherwise, an error code.
  197. */
  198. int (*info)(struct flash_bank_s *bank, char *buf, int buf_size);
  199. /**
  200. * A more gentle flavor of filash_driver_s::probe, performing
  201. * setup with less noise. Generally, driver routines should test
  202. * to seee if the bank has already been probed; if it has, the
  203. * driver probably should not perform its probe a second time.
  204. *
  205. * This callback is often called from the inside of other
  206. * routines (e.g. GDB flash downloads) to autoprobe the flash as
  207. * it is programing the flash.
  208. *
  209. * @param bank - the bank to probe
  210. * @returns ERROR_OK if successful; otherwise, an error code.
  211. */
  212. int (*auto_probe)(struct flash_bank_s *bank);
  213. } flash_driver_t;
  214. /**
  215. * Provides details of a flash bank, available either on-chip or through
  216. * a major interface.
  217. *
  218. * This structure will be passed as a parameter to the callbacks in the
  219. * flash_driver_s structure, some of which may modify the contents of
  220. * this structure of the area of flash that it defines. Driver writers
  221. * may use the @c driver_priv member to store additional data on a
  222. * per-bank basis, if required.
  223. */
  224. typedef struct flash_bank_s
  225. {
  226. struct target_s *target; /**< Target to which this bank belongs. */
  227. flash_driver_t *driver; /**< Driver for this bank. */
  228. void *driver_priv; /**< Private driver storage pointer */
  229. int bank_number; /**< The 'bank' (or chip number) of this instance. */
  230. uint32_t base; /**< The base address of this bank */
  231. uint32_t size; /**< The size of this chip bank, in bytes */
  232. int chip_width; /**< Width of the chip in bytes (1,2,4 bytes) */
  233. int bus_width; /**< Maximum bus width, in bytes (1,2,4 bytes) */
  234. /**
  235. * The number of sectors on this chip. This value will
  236. * be set intially to 0, and the flash driver must set this to
  237. * some non-zero value during "probe()" or "auto_probe()".
  238. */
  239. int num_sectors;
  240. /// Array of sectors, allocated and initilized by the flash driver
  241. flash_sector_t *sectors;
  242. struct flash_bank_s *next; /**< The next flash bank on this chip */
  243. } flash_bank_t;
  244. /// Registers the 'flash' subsystem commands
  245. extern int flash_register_commands(struct command_context_s *cmd_ctx);
  246. /// Initializes the 'flash' subsystem drivers
  247. extern int flash_init_drivers(struct command_context_s *cmd_ctx);
  248. /**
  249. * Erases @a length bytes in the @a target flash, starting at @a addr.
  250. * @returns ERROR_OK if successful; otherwise, an error code.
  251. */
  252. extern int flash_erase_address_range(struct target_s *target, uint32_t addr, uint32_t length);
  253. /**
  254. * Writes @a image into the @a target flash. The @a written parameter
  255. * will contain the
  256. * @param target The target with the flash to be programmed.
  257. * @param image The image that will be programmed to flash.
  258. * @param written On return, contains the number of bytes written.
  259. * @param erase If non-zero, indicates the flash driver should first
  260. * erase the corresponding banks or sectors before programming.
  261. * @returns ERROR_OK if successful; otherwise, an error code.
  262. */
  263. extern int flash_write(struct target_s *target, struct image_s *image, uint32_t *written, int erase);
  264. /**
  265. * Forces targets to re-examine their erase/protection state.
  266. * This routine must be called when the system may modify the status.
  267. */
  268. extern void flash_set_dirty(void);
  269. /// @returns The number of flash banks currently defined.
  270. extern int flash_get_bank_count(void);
  271. /**
  272. * Provides default erased-bank check handling. Checks to see if
  273. * the flash driver knows they are erased; if things look uncertain,
  274. * this routine will call default_flash_mem_blank_check() to confirm.
  275. * @returns ERROR_OK if successful; otherwise, an error code.
  276. */
  277. extern int default_flash_blank_check(struct flash_bank_s *bank);
  278. /**
  279. * Provides a default blank flash memory check. Ensures the contents
  280. * of the given bank have truly been erased.
  281. * @param bank The flash bank.
  282. * @returns ERROR_OK if successful; otherwise, an error code.
  283. */
  284. extern int default_flash_mem_blank_check(struct flash_bank_s *bank);
  285. /**
  286. * Returns a flash bank by the specified flash_bank_s bank_number, @a num.
  287. * @param num The flash bank number.
  288. * @returns A flash_bank_t for flash bank @a num, or NULL
  289. */
  290. extern flash_bank_t *get_flash_bank_by_num(int num);
  291. /**
  292. * Returns the flash bank like get_flash_bank_by_num(), without probing.
  293. * @param num The flash bank number.
  294. * @returns A flash_bank_t for flash bank @a num, or NULL.
  295. */
  296. extern flash_bank_t *get_flash_bank_by_num_noprobe(int num);
  297. /**
  298. * Returns the flash bank located at a specified address.
  299. * @param target The target, presumed to contain one or more banks.
  300. * @param addr An address that is within the range of the bank.
  301. * @returns The flash_bank_t located at @a addr, or NULL.
  302. */
  303. extern flash_bank_t *get_flash_bank_by_addr(struct target_s *target, uint32_t addr);
  304. #define ERROR_FLASH_BANK_INVALID (-900)
  305. #define ERROR_FLASH_SECTOR_INVALID (-901)
  306. #define ERROR_FLASH_OPERATION_FAILED (-902)
  307. #define ERROR_FLASH_DST_OUT_OF_BANK (-903)
  308. #define ERROR_FLASH_DST_BREAKS_ALIGNMENT (-904)
  309. #define ERROR_FLASH_BUSY (-905)
  310. #define ERROR_FLASH_SECTOR_NOT_ERASED (-906)
  311. #define ERROR_FLASH_BANK_NOT_PROBED (-907)
  312. #endif /* FLASH_H */