add flash/nor/core.[ch]
[fw/openocd] / src / flash / nor / tcl.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de>              *
3  *   Copyright (C) 2007,2008 Ã˜yvind Harboe <oyvind.harboe@zylin.com>       *
4  *   Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk>           *
5  *   Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net>             *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  *   This program is distributed in the hope that it will be useful,       *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15  *   GNU General Public License for more details.                          *
16  *                                                                         *
17  *   You should have received a copy of the GNU General Public License     *
18  *   along with this program; if not, write to the                         *
19  *   Free Software Foundation, Inc.,                                       *
20  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21  ***************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "imp.h"
26
27 COMMAND_HANDLER(handle_flash_bank_command)
28 {
29         if (CMD_ARGC < 7)
30         {
31                 LOG_ERROR("usage: flash bank <name> <driver> "
32                                 "<base> <size> <chip_width> <bus_width>");
33                 return ERROR_COMMAND_SYNTAX_ERROR;
34         }
35         // save bank name and advance arguments for compatibility
36         const char *bank_name = *CMD_ARGV++;
37         CMD_ARGC--;
38
39         struct target *target;
40         if ((target = get_target(CMD_ARGV[5])) == NULL)
41         {
42                 LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
43                 return ERROR_FAIL;
44         }
45
46         const char *driver_name = CMD_ARGV[0];
47         struct flash_driver *driver = flash_driver_find_by_name(driver_name);
48         if (NULL == driver)
49         {
50                 /* no matching flash driver found */
51                 LOG_ERROR("flash driver '%s' not found", driver_name);
52                 return ERROR_FAIL;
53         }
54
55         /* register flash specific commands */
56         if (NULL != driver->commands)
57         {
58                 int retval = register_commands(CMD_CTX, NULL,
59                                 driver->commands);
60                 if (ERROR_OK != retval)
61                 {
62                         LOG_ERROR("couldn't register '%s' commands",
63                                         driver_name);
64                         return ERROR_FAIL;
65                 }
66         }
67
68         struct flash_bank *c = malloc(sizeof(*c));
69         c->name = strdup(bank_name);
70         c->target = target;
71         c->driver = driver;
72         c->driver_priv = NULL;
73         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], c->base);
74         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], c->size);
75         COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], c->chip_width);
76         COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], c->bus_width);
77         c->num_sectors = 0;
78         c->sectors = NULL;
79         c->next = NULL;
80
81         int retval;
82         retval = CALL_COMMAND_HANDLER(driver->flash_bank_command, c);
83         if (ERROR_OK != retval)
84         {
85                 LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32,
86                                 driver_name, c->base);
87                 free(c);
88                 return retval;
89         }
90
91         return ERROR_OK;
92
93 }
94
95
96 static int jim_flash_banks(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
97 {
98         if (argc != 1) {
99                 Jim_WrongNumArgs(interp, 1, argv, "no arguments to flash_banks command");
100                 return JIM_ERR;
101         }
102
103         Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
104
105         for (struct flash_bank *p = flash_bank_list(); p; p = p->next)
106         {
107                 Jim_Obj *elem = Jim_NewListObj(interp, NULL, 0);
108
109                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "name", -1));
110                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, p->driver->name, -1));
111                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "base", -1));
112                 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->base));
113                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "size", -1));
114                 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->size));
115                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "bus_width", -1));
116                 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->bus_width));
117                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "chip_width", -1));
118                 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->chip_width));
119
120                 Jim_ListAppendElement(interp, list, elem);
121         }
122
123         Jim_SetResult(interp, list);
124
125         return JIM_OK;
126 }
127
128
129 COMMAND_HANDLER(handle_flash_init_command)
130 {
131         if (CMD_ARGC != 0)
132                 return ERROR_COMMAND_SYNTAX_ERROR;
133
134         static bool flash_initialized = false;
135         if (flash_initialized)
136         {
137                 LOG_INFO("'flash init' has already been called");
138                 return ERROR_OK;
139         }
140         flash_initialized = true;
141
142         LOG_DEBUG("Initializing flash devices...");
143         return flash_init_drivers(CMD_CTX);
144 }
145
146 static const struct command_registration flash_config_command_handlers[] = {
147         {
148                 .name = "bank",
149                 .handler = &handle_flash_bank_command,
150                 .mode = COMMAND_CONFIG,
151                 .usage = "<name> <driver> <base> <size> "
152                         "<chip_width> <bus_width> <target> "
153                         "[driver_options ...]",
154                 .help = "Define a new bank with the given name, "
155                         "using the specified NOR flash driver.",
156         },
157         {
158                 .name = "init",
159                 .mode = COMMAND_CONFIG,
160                 .handler = &handle_flash_init_command,
161                 .help = "initialize flash devices",
162         },
163         {
164                 .name = "banks",
165                 .mode = COMMAND_ANY,
166                 .jim_handler = &jim_flash_banks,
167                 .help = "return information about the flash banks",
168         },
169         COMMAND_REGISTRATION_DONE
170 };
171 static const struct command_registration flash_command_handlers[] = {
172         {
173                 .name = "flash",
174                 .mode = COMMAND_ANY,
175                 .help = "NOR flash command group",
176                 .chain = flash_config_command_handlers,
177         },
178         COMMAND_REGISTRATION_DONE
179 };
180
181 int flash_register_commands(struct command_context *cmd_ctx)
182 {
183         return register_commands(cmd_ctx, NULL, flash_command_handlers);
184 }