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.
 
 
 
 
 
 

437 lines
14 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2008 by *
  3. * Karl RobinSod <karl.robinsod@gmail.com> *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. /***************************************************************************
  19. * There are some things to notice
  20. *
  21. * You need to unprotect flash sectors each time you connect the OpenOCD
  22. * Dumping 1MB takes about 60 Seconds
  23. * Full erase (sectors 0-22 inclusive) takes 2-4 seconds
  24. * Writing 1MB takes 88 seconds
  25. *
  26. ***************************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "imp.h"
  31. #include <helper/binarybuffer.h>
  32. #define LOAD_TIMER_ERASE 0
  33. #define LOAD_TIMER_WRITE 1
  34. #define FLASH_PAGE_SIZE 512
  35. /* LPC288X control registers */
  36. #define DBGU_CIDR 0x8000507C
  37. /* LPC288X flash registers */
  38. #define F_CTRL 0x80102000 /* Flash control register R/W 0x5 */
  39. #define F_STAT 0x80102004 /* Flash status register RO 0x45 */
  40. #define F_PROG_TIME 0x80102008 /* Flash program time register R/W 0 */
  41. #define F_WAIT 0x80102010 /* Flash read wait state register R/W 0xC004 */
  42. #define F_CLK_TIME 0x8010201C /* Flash clock divider for 66 kHz generation R/W 0
  43. **/
  44. #define F_INTEN_CLR 0x80102FD8 /* Clear interrupt enable bits WO - */
  45. #define F_INTEN_SET 0x80102FDC /* Set interrupt enable bits WO - */
  46. #define F_INT_STAT 0x80102FE0 /* Interrupt status bits RO 0 */
  47. #define F_INTEN 0x80102FE4 /* Interrupt enable bits RO 0 */
  48. #define F_INT_CLR 0x80102FE8 /* Clear interrupt status bits WO */
  49. #define F_INT_SET 0x80102FEC /* Set interrupt status bits WO - */
  50. #define FLASH_PD 0x80005030 /* Allows turning off the Flash memory for power
  51. *savings. R/W 1*/
  52. #define FLASH_INIT 0x80005034 /* Monitors Flash readiness, such as recovery from
  53. *Power Down mode. R/W -*/
  54. /* F_CTRL bits */
  55. #define FC_CS 0x0001
  56. #define FC_FUNC 0x0002
  57. #define FC_WEN 0x0004
  58. #define FC_RD_LATCH 0x0020
  59. #define FC_PROTECT 0x0080
  60. #define FC_SET_DATA 0x0400
  61. #define FC_RSSL 0x0800
  62. #define FC_PROG_REQ 0x1000
  63. #define FC_CLR_BUF 0x4000
  64. #define FC_LOAD_REQ 0x8000
  65. /* F_STAT bits */
  66. #define FS_DONE 0x0001
  67. #define FS_PROGGNT 0x0002
  68. #define FS_RDY 0x0004
  69. #define FS_ERR 0x0020
  70. /* F_PROG_TIME */
  71. #define FPT_TIME_MASK 0x7FFF
  72. #define FPT_ENABLE 0x8000
  73. /* F_WAIT */
  74. #define FW_WAIT_STATES_MASK 0x00FF
  75. #define FW_SET_MASK 0xC000
  76. /* F_CLK_TIME */
  77. #define FCT_CLK_DIV_MASK 0x0FFF
  78. struct lpc288x_flash_bank {
  79. uint32_t working_area;
  80. uint32_t working_area_size;
  81. /* chip id register */
  82. uint32_t cidr;
  83. const char *target_name;
  84. uint32_t cclk;
  85. uint32_t sector_size_break;
  86. };
  87. static uint32_t lpc288x_wait_status_busy(struct flash_bank *bank, int timeout);
  88. static void lpc288x_load_timer(int erase, struct target *target);
  89. static void lpc288x_set_flash_clk(struct flash_bank *bank);
  90. static uint32_t lpc288x_system_ready(struct flash_bank *bank);
  91. static uint32_t lpc288x_wait_status_busy(struct flash_bank *bank, int timeout)
  92. {
  93. uint32_t status;
  94. struct target *target = bank->target;
  95. do {
  96. alive_sleep(1);
  97. timeout--;
  98. target_read_u32(target, F_STAT, &status);
  99. } while (((status & FS_DONE) == 0) && timeout);
  100. if (timeout == 0) {
  101. LOG_DEBUG("Timedout!");
  102. return ERROR_FLASH_OPERATION_FAILED;
  103. }
  104. return ERROR_OK;
  105. }
  106. /* Read device id register and fill in driver info structure */
  107. static int lpc288x_read_part_info(struct flash_bank *bank)
  108. {
  109. struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
  110. struct target *target = bank->target;
  111. uint32_t cidr;
  112. int i = 0;
  113. uint32_t offset;
  114. if (lpc288x_info->cidr == 0x0102100A)
  115. return ERROR_OK;/* already probed, multiple probes may cause memory leak, not
  116. *allowed */
  117. /* Read and parse chip identification register */
  118. target_read_u32(target, DBGU_CIDR, &cidr);
  119. if (cidr != 0x0102100A) {
  120. LOG_WARNING("Cannot identify target as an LPC288X (%08" PRIx32 ")", cidr);
  121. return ERROR_FLASH_OPERATION_FAILED;
  122. }
  123. lpc288x_info->cidr = cidr;
  124. lpc288x_info->sector_size_break = 0x000F0000;
  125. lpc288x_info->target_name = "LPC288x";
  126. /* setup the sector info... */
  127. offset = bank->base;
  128. bank->num_sectors = 23;
  129. bank->sectors = malloc(sizeof(struct flash_sector) * 23);
  130. for (i = 0; i < 15; i++) {
  131. bank->sectors[i].offset = offset;
  132. bank->sectors[i].size = 64 * 1024;
  133. offset += bank->sectors[i].size;
  134. bank->sectors[i].is_erased = -1;
  135. bank->sectors[i].is_protected = 1;
  136. }
  137. for (i = 15; i < 23; i++) {
  138. bank->sectors[i].offset = offset;
  139. bank->sectors[i].size = 8 * 1024;
  140. offset += bank->sectors[i].size;
  141. bank->sectors[i].is_erased = -1;
  142. bank->sectors[i].is_protected = 1;
  143. }
  144. return ERROR_OK;
  145. }
  146. static int lpc288x_protect_check(struct flash_bank *bank)
  147. {
  148. return ERROR_OK;
  149. }
  150. /* flash_bank LPC288x 0 0 0 0 <target#> <cclk> */
  151. FLASH_BANK_COMMAND_HANDLER(lpc288x_flash_bank_command)
  152. {
  153. struct lpc288x_flash_bank *lpc288x_info;
  154. if (CMD_ARGC < 6)
  155. return ERROR_COMMAND_SYNTAX_ERROR;
  156. lpc288x_info = malloc(sizeof(struct lpc288x_flash_bank));
  157. bank->driver_priv = lpc288x_info;
  158. /* part wasn't probed for info yet */
  159. lpc288x_info->cidr = 0;
  160. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], lpc288x_info->cclk);
  161. return ERROR_OK;
  162. }
  163. /* The frequency is the AHB clock frequency divided by (CLK_DIV ×3) + 1.
  164. * This must be programmed such that the Flash Programming clock frequency is 66 kHz ± 20%.
  165. * AHB = 12 MHz ?
  166. * 12000000/66000 = 182
  167. * CLK_DIV = 60 ? */
  168. static void lpc288x_set_flash_clk(struct flash_bank *bank)
  169. {
  170. uint32_t clk_time;
  171. struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
  172. clk_time = (lpc288x_info->cclk / 66000) / 3;
  173. target_write_u32(bank->target, F_CTRL, FC_CS | FC_WEN);
  174. target_write_u32(bank->target, F_CLK_TIME, clk_time);
  175. }
  176. /* AHB tcyc (in ns) 83 ns
  177. * LOAD_TIMER_ERASE FPT_TIME = ((400,000,000 / AHB tcyc (in ns)) - 2) / 512
  178. * = 9412 (9500) (AN10548 9375)
  179. * LOAD_TIMER_WRITE FPT_TIME = ((1,000,000 / AHB tcyc (in ns)) - 2) / 512
  180. * = 23 (75) (AN10548 72 - is this wrong?)
  181. * TODO: Sort out timing calcs ;) */
  182. static void lpc288x_load_timer(int erase, struct target *target)
  183. {
  184. if (erase == LOAD_TIMER_ERASE)
  185. target_write_u32(target, F_PROG_TIME, FPT_ENABLE | 9500);
  186. else
  187. target_write_u32(target, F_PROG_TIME, FPT_ENABLE | 75);
  188. }
  189. static uint32_t lpc288x_system_ready(struct flash_bank *bank)
  190. {
  191. struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
  192. if (lpc288x_info->cidr == 0)
  193. return ERROR_FLASH_BANK_NOT_PROBED;
  194. if (bank->target->state != TARGET_HALTED) {
  195. LOG_ERROR("Target not halted");
  196. return ERROR_TARGET_NOT_HALTED;
  197. }
  198. return ERROR_OK;
  199. }
  200. static int lpc288x_erase_check(struct flash_bank *bank)
  201. {
  202. uint32_t status = lpc288x_system_ready(bank); /* probed? halted? */
  203. if (status != ERROR_OK) {
  204. LOG_INFO("Processor not halted/not probed");
  205. return status;
  206. }
  207. return ERROR_OK;
  208. }
  209. static int lpc288x_erase(struct flash_bank *bank, int first, int last)
  210. {
  211. uint32_t status;
  212. int sector;
  213. struct target *target = bank->target;
  214. status = lpc288x_system_ready(bank); /* probed? halted? */
  215. if (status != ERROR_OK)
  216. return status;
  217. if ((first < 0) || (last < first) || (last >= bank->num_sectors)) {
  218. LOG_INFO("Bad sector range");
  219. return ERROR_FLASH_SECTOR_INVALID;
  220. }
  221. /* Configure the flash controller timing */
  222. lpc288x_set_flash_clk(bank);
  223. for (sector = first; sector <= last; sector++) {
  224. if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
  225. return ERROR_FLASH_OPERATION_FAILED;
  226. lpc288x_load_timer(LOAD_TIMER_ERASE, target);
  227. target_write_u32(target, bank->sectors[sector].offset, 0x00);
  228. target_write_u32(target, F_CTRL, FC_PROG_REQ | FC_PROTECT | FC_CS);
  229. }
  230. if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
  231. return ERROR_FLASH_OPERATION_FAILED;
  232. return ERROR_OK;
  233. }
  234. static int lpc288x_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
  235. {
  236. uint8_t page_buffer[FLASH_PAGE_SIZE];
  237. uint32_t status, source_offset, dest_offset;
  238. struct target *target = bank->target;
  239. uint32_t bytes_remaining = count;
  240. uint32_t first_sector, last_sector, sector, page;
  241. int i;
  242. /* probed? halted? */
  243. status = lpc288x_system_ready(bank);
  244. if (status != ERROR_OK)
  245. return status;
  246. /* Initialise search indices */
  247. first_sector = last_sector = 0xffffffff;
  248. /* validate the write range... */
  249. for (i = 0; i < bank->num_sectors; i++) {
  250. if ((offset >= bank->sectors[i].offset) &&
  251. (offset < (bank->sectors[i].offset + bank->sectors[i].size)) &&
  252. (first_sector == 0xffffffff)) {
  253. first_sector = i;
  254. /* all writes must start on a sector boundary... */
  255. if (offset % bank->sectors[i].size) {
  256. LOG_INFO(
  257. "offset 0x%" PRIx32 " breaks required alignment 0x%" PRIx32 "",
  258. offset,
  259. bank->sectors[i].size);
  260. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  261. }
  262. }
  263. if (((offset + count) > bank->sectors[i].offset) &&
  264. ((offset + count) <= (bank->sectors[i].offset + bank->sectors[i].size)) &&
  265. (last_sector == 0xffffffff))
  266. last_sector = i;
  267. }
  268. /* Range check... */
  269. if (first_sector == 0xffffffff || last_sector == 0xffffffff) {
  270. LOG_INFO("Range check failed %" PRIx32 " %" PRIx32 "", offset, count);
  271. return ERROR_FLASH_DST_OUT_OF_BANK;
  272. }
  273. /* Configure the flash controller timing */
  274. lpc288x_set_flash_clk(bank);
  275. /* initialise the offsets */
  276. source_offset = 0;
  277. dest_offset = 0;
  278. for (sector = first_sector; sector <= last_sector; sector++) {
  279. for (page = 0; page < bank->sectors[sector].size / FLASH_PAGE_SIZE; page++) {
  280. if (bytes_remaining == 0) {
  281. count = 0;
  282. memset(page_buffer, 0xFF, FLASH_PAGE_SIZE);
  283. } else if (bytes_remaining < FLASH_PAGE_SIZE) {
  284. count = bytes_remaining;
  285. memset(page_buffer, 0xFF, FLASH_PAGE_SIZE);
  286. memcpy(page_buffer, &buffer[source_offset], count);
  287. } else {
  288. count = FLASH_PAGE_SIZE;
  289. memcpy(page_buffer, &buffer[source_offset], count);
  290. }
  291. /* Wait for flash to become ready */
  292. if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
  293. return ERROR_FLASH_OPERATION_FAILED;
  294. /* fill flash data latches with 1's */
  295. target_write_u32(target, F_CTRL, FC_CS | FC_SET_DATA | FC_WEN | FC_FUNC);
  296. target_write_u32(target, F_CTRL, FC_CS | FC_WEN | FC_FUNC);
  297. if (target_write_buffer(target, offset + dest_offset, FLASH_PAGE_SIZE,
  298. page_buffer) != ERROR_OK) {
  299. LOG_INFO("Write to flash buffer failed");
  300. return ERROR_FLASH_OPERATION_FAILED;
  301. }
  302. dest_offset += FLASH_PAGE_SIZE;
  303. source_offset += count;
  304. bytes_remaining -= count;
  305. lpc288x_load_timer(LOAD_TIMER_WRITE, target);
  306. target_write_u32(target, F_CTRL, FC_PROG_REQ | FC_PROTECT | FC_FUNC |
  307. FC_CS);
  308. }
  309. }
  310. return ERROR_OK;
  311. }
  312. static int lpc288x_probe(struct flash_bank *bank)
  313. {
  314. /* we only deal with LPC2888 so flash config is fixed */
  315. struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
  316. int retval;
  317. if (lpc288x_info->cidr != 0)
  318. return ERROR_OK;/* already probed */
  319. if (bank->target->state != TARGET_HALTED) {
  320. LOG_ERROR("Target not halted");
  321. return ERROR_TARGET_NOT_HALTED;
  322. }
  323. retval = lpc288x_read_part_info(bank);
  324. if (retval != ERROR_OK)
  325. return retval;
  326. return ERROR_OK;
  327. }
  328. static int lpc288x_protect(struct flash_bank *bank, int set, int first, int last)
  329. {
  330. int lockregion, status;
  331. uint32_t value;
  332. struct target *target = bank->target;
  333. /* probed? halted? */
  334. status = lpc288x_system_ready(bank);
  335. if (status != ERROR_OK)
  336. return status;
  337. if ((first < 0) || (last < first) || (last >= bank->num_sectors))
  338. return ERROR_FLASH_SECTOR_INVALID;
  339. /* Configure the flash controller timing */
  340. lpc288x_set_flash_clk(bank);
  341. for (lockregion = first; lockregion <= last; lockregion++) {
  342. if (set) {
  343. /* write an odd value to base addy to protect... */
  344. value = 0x01;
  345. } else {
  346. /* write an even value to base addy to unprotect... */
  347. value = 0x00;
  348. }
  349. target_write_u32(target, bank->sectors[lockregion].offset, value);
  350. target_write_u32(target, F_CTRL, FC_LOAD_REQ | FC_PROTECT | FC_WEN | FC_FUNC |
  351. FC_CS);
  352. }
  353. return ERROR_OK;
  354. }
  355. struct flash_driver lpc288x_flash = {
  356. .name = "lpc288x",
  357. .flash_bank_command = lpc288x_flash_bank_command,
  358. .erase = lpc288x_erase,
  359. .protect = lpc288x_protect,
  360. .write = lpc288x_write,
  361. .read = default_flash_read,
  362. .probe = lpc288x_probe,
  363. .auto_probe = lpc288x_probe,
  364. .erase_check = lpc288x_erase_check,
  365. .protect_check = lpc288x_protect_check,
  366. };