1 /***************************************************************************
2 * Copyright (C) 2019 Icenowy Zheng <icenowy@aosc.io> *
3 * Copyright (C) 2019 Caleb Szalacinski <contact@skiboy.net> *
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. *
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. *
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 ***************************************************************************/
24 #include <target/image.h>
26 #define SWM050_DELAY 100
28 #define SWM050_FLASH_PAGE_SIZE 0x200
29 #define SWM050_FLASH_PAGES 16
31 #define SWM050_CPU_ID 0xE000ED00
32 #define SWM050_CPU_ID_VAL 0x410CC200
34 #define SWM050_FLASH_REG1 0x1F000000
35 #define SWM050_FLASH_REG2 0x1F000038
36 #define SWM050_FLASH_KEY 0xAAAAAAAA
38 #define SWM050_SYSCTL_CFG_0 0x400F0000
39 #define SWM050_SYSCTL_DBLF 0x400F0008
41 static int swm050_erase(struct flash_bank *bank, unsigned int first,
44 struct target *target = bank->target;
47 if (target->state != TARGET_HALTED) {
48 LOG_ERROR("Target not halted");
49 return ERROR_TARGET_NOT_HALTED;
53 retval = target_write_u32(target, SWM050_FLASH_REG1, 0x4);
54 if (retval != ERROR_OK)
57 for (unsigned int curr_page = first; curr_page <= last; curr_page++) {
58 uint32_t curr_addr = bank->base + (SWM050_FLASH_PAGE_SIZE * curr_page);
60 retval = target_write_u32(target, curr_addr, SWM050_FLASH_KEY);
61 if (retval != ERROR_OK)
63 alive_sleep(SWM050_DELAY);
66 /* Close flash interface */
67 retval = target_write_u32(target, SWM050_FLASH_REG1, 0x0);
68 if (retval != ERROR_OK)
74 static int swm050_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
76 struct target *target = bank->target;
79 if (target->state != TARGET_HALTED) {
80 LOG_ERROR("Target not halted");
81 retval = ERROR_TARGET_NOT_HALTED;
86 retval = target_write_u32(target, SWM050_FLASH_REG1, 0x1);
87 if (retval != ERROR_OK)
90 retval = target_write_memory(target, bank->base + offset, 4, count/4, buffer);
91 if (retval != ERROR_OK)
94 /* Close flash interface */
95 retval = target_write_u32(target, SWM050_FLASH_REG1, 0x0);
96 if (retval != ERROR_OK)
102 static int swm050_probe(struct flash_bank *bank)
107 static int swm050_mass_erase(struct flash_bank *bank)
109 struct target *target = bank->target;
112 if (target->state != TARGET_HALTED) {
113 LOG_ERROR("Target not halted");
114 return ERROR_TARGET_NOT_HALTED;
117 /* Perform mass erase */
118 retval = target_write_u32(target, SWM050_FLASH_REG1, 0x6);
119 if (retval != ERROR_OK)
121 retval = target_write_u32(target, SWM050_FLASH_REG2, 0x1);
122 if (retval != ERROR_OK)
124 retval = target_write_u32(target, 0x0, SWM050_FLASH_KEY);
125 if (retval != ERROR_OK)
128 alive_sleep(SWM050_DELAY);
130 /* Close flash interface */
131 retval = target_write_u32(target, SWM050_FLASH_REG1, 0x0);
132 if (retval != ERROR_OK)
138 COMMAND_HANDLER(swm050_handle_mass_erase_command)
141 return ERROR_COMMAND_SYNTAX_ERROR;
143 struct flash_bank *bank;
144 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
145 if (retval != ERROR_OK)
148 retval = swm050_mass_erase(bank);
149 if (retval == ERROR_OK)
150 command_print(CMD, "swm050 mass erase complete");
152 command_print(CMD, "swm050 mass erase failed");
157 FLASH_BANK_COMMAND_HANDLER(swm050_flash_bank_command)
160 bank->write_start_alignment = 4;
161 bank->write_end_alignment = 4;
162 bank->size = SWM050_FLASH_PAGE_SIZE * SWM050_FLASH_PAGES;
164 bank->num_sectors = SWM050_FLASH_PAGES;
165 bank->sectors = alloc_block_array(0, SWM050_FLASH_PAGE_SIZE, SWM050_FLASH_PAGES);
169 for (unsigned int i = 0; i < bank->num_sectors; i++)
170 bank->sectors[i].is_protected = 0;
175 static const struct command_registration swm050_exec_command_handlers[] = {
177 .name = "mass_erase",
178 .handler = swm050_handle_mass_erase_command,
179 .mode = COMMAND_EXEC,
181 .help = "Erase entire flash device.",
183 COMMAND_REGISTRATION_DONE
186 static const struct command_registration swm050_command_handlers[] = {
190 .help = "swm050 flash command group",
192 .chain = swm050_exec_command_handlers,
194 COMMAND_REGISTRATION_DONE
197 struct flash_driver swm050_flash = {
199 .commands = swm050_command_handlers,
200 .flash_bank_command = swm050_flash_bank_command,
201 .erase = swm050_erase,
202 .write = swm050_write,
203 .read = default_flash_read,
204 .probe = swm050_probe,
205 .auto_probe = swm050_probe,
206 .erase_check = default_flash_blank_check,
207 .free_driver_priv = default_flash_free_driver_priv,