static int sam3x_flash_cmd(target *t, uint32_t base, uint8_t cmd, uint16_t arg) { DEBUG("%s: base = 0x%08x cmd = 0x%02X, arg = 0x%06X\n", __func__, base, cmd, arg); target_mem_write32(t, EEFC_FCR(base), EEFC_FCR_FKEY | cmd | ((uint32_t)arg << 8)); while (!(target_mem_read32(t, EEFC_FSR(base)) & EEFC_FSR_FRDY)) if(target_check_error(t)) return -1; uint32_t sr = target_mem_read32(t, EEFC_FSR(base)); return sr & EEFC_FSR_ERROR; }
//----------------------------------------------------------------------------- static void target_erase(void) { for (uint32_t plane = 0; plane < target_device.n_planes; plane++) dap_write_word(EEFC_FCR(plane), CMD_EA); for (uint32_t plane = 0; plane < target_device.n_planes; plane++) while (0 == (dap_read_word(EEFC_FSR(plane)) & FSR_FRDY)); }
//----------------------------------------------------------------------------- static void target_select(target_options_t *options) { uint32_t chip_id, chip_exid; // Set boot mode GPNVM bit as a workaraound dap_write_word(EEFC_FCR(0), CMD_SGPB | (1 << 8)); // Stop the core dap_write_word(DHCSR, 0xa05f0003); dap_write_word(DEMCR, 0x00000001); dap_write_word(AIRCR, 0x05fa0004); chip_id = dap_read_word(CHIPID_CIDR); chip_exid = dap_read_word(CHIPID_EXID); for (device_t *device = devices; device->chip_id > 0; device++) { if (device->chip_id == chip_id && device->chip_exid == chip_exid) { uint32_t fl_id, fl_size, fl_page_size, fl_nb_palne, fl_nb_lock; verbose("Target: %s\n", device->name); for (uint32_t plane = 0; plane < device->n_planes; plane++) { dap_write_word(EEFC_FCR(plane), CMD_GETD); while (0 == (dap_read_word(EEFC_FSR(plane)) & FSR_FRDY)); fl_id = dap_read_word(EEFC_FRR(plane)); check(fl_id, "Cannot read flash descriptor, check Erase pin state"); fl_size = dap_read_word(EEFC_FRR(plane)); check(fl_size == device->flash_size, "Invalid reported Flash size (%d)", fl_size); fl_page_size = dap_read_word(EEFC_FRR(plane)); check(fl_page_size == device->page_size, "Invalid reported page size (%d)", fl_page_size); fl_nb_palne = dap_read_word(EEFC_FRR(plane)); for (uint32_t i = 0; i < fl_nb_palne; i++) dap_read_word(EEFC_FRR(plane)); fl_nb_lock = dap_read_word(EEFC_FRR(plane)); for (uint32_t i = 0; i < fl_nb_lock; i++) dap_read_word(EEFC_FRR(plane)); } target_device = *device; target_options = *options; target_check_options(&target_options, device->flash_size * target_device.n_planes, device->page_size * PAGES_IN_ERASE_BLOCK); return; } } error_exit("unknown target device (CHIP_ID = 0x%08x)", chip_id); }
//----------------------------------------------------------------------------- static void target_erase(void) { verbose("Erasing... "); for (uint32_t plane = 0; plane < device->n_planes; plane++) dap_write_word(EEFC_FCR(plane), CMD_EA); for (uint32_t plane = 0; plane < device->n_planes; plane++) while (0 == (dap_read_word(EEFC_FSR(plane)) & FSR_FRDY)); verbose("done.\n"); }
//----------------------------------------------------------------------------- static void target_program(void) { uint32_t addr = target_device.flash_start + target_options.offset; uint32_t number_of_pages, plane, page_offset; uint32_t offs = 0; uint8_t *buf = target_options.file_data; uint32_t size = target_options.file_size; number_of_pages = (size + target_device.page_size - 1) / target_device.page_size; page_offset = target_options.offset / target_device.page_size; for (uint32_t page = 0; page < number_of_pages; page += PAGES_IN_ERASE_BLOCK) { plane = (page + page_offset) / (target_device.flash_size / target_device.page_size); dap_write_word(EEFC_FCR(plane), CMD_EPA | (((page + page_offset) | 1) << 8)); while (0 == (dap_read_word(EEFC_FSR(plane)) & FSR_FRDY)); verbose("."); } verbose(","); for (uint32_t page = 0; page < number_of_pages; page++) { dap_write_block(addr, &buf[offs], target_device.page_size); addr += target_device.page_size; offs += target_device.page_size; plane = (page + page_offset) / (target_device.flash_size / target_device.page_size); dap_write_word(EEFC_FCR(plane), CMD_WP | ((page + page_offset) << 8)); while (0 == (dap_read_word(EEFC_FSR(plane)) & FSR_FRDY)); verbose("."); } }
//----------------------------------------------------------------------------- static void target_program(char *name) { uint32_t addr = device->flash_start; uint32_t flash_size = device->flash_size * device->n_planes; uint32_t size, number_of_pages, plane; uint32_t offs = 0; uint8_t *buf; buf = buf_alloc(flash_size); size = load_file(name, buf, flash_size); memset(&buf[size], 0xff, flash_size - size); verbose("Programming..."); number_of_pages = (size + device->page_size - 1) / device->page_size; for (uint32_t page = 0; page < number_of_pages; page++) { dap_write_block(addr, &buf[offs], device->page_size); addr += device->page_size; offs += device->page_size; plane = page / (device->flash_size / device->page_size); dap_write_word(EEFC_FCR(plane), CMD_EWP | (page << 8)); while (0 == (dap_read_word(EEFC_FSR(plane)) & FSR_FRDY)); verbose("."); } buf_free(buf); verbose(" done.\n"); }