add flash/nor/core.[ch]

The newly moved flash TCL routines access the internals of the module
too much.  Fix the layering issues by adding new core NOR flash APIs:

<flash/nor/core.h>:
  - flash_driver_find_by_name() - self-descriptive

<flash/nor/imp.h>:
  - flash_bank_add()            - encapsulates adding banks to bank list
  - flash_bank_list()           - encapsulates retreiving bank list

This allows the externs in flash/nor/imp.h to be removed, and
these mechanisms may now be re-used by other flash module code.
This commit is contained in:
Zachary T Welch 2009-12-03 17:14:07 -08:00
parent 3cb0b56005
commit c65d94f7d0
5 changed files with 154 additions and 67 deletions

View File

@ -2,6 +2,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/src
noinst_LTLIBRARIES = libocdflashnor.la
libocdflashnor_la_SOURCES = \
core.c \
tcl.c \
$(NOR_DRIVERS)

65
src/flash/nor/core.c Normal file
View File

@ -0,0 +1,65 @@
/***************************************************************************
* Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <flash/flash.h>
#include <flash/nor/imp.h>
// in flash.c, to be moved here
extern struct flash_driver *flash_drivers[];
extern struct flash_bank *flash_banks;
struct flash_driver *flash_driver_find_by_name(const char *name)
{
for (unsigned i = 0; flash_drivers[i]; i++)
{
if (strcmp(name, flash_drivers[i]->name) == 0)
return flash_drivers[i];
}
return NULL;
}
void flash_bank_add(struct flash_bank *bank)
{
/* put flash bank in linked list */
unsigned bank_num = 0;
if (flash_banks)
{
/* find last flash bank */
struct flash_bank *p = flash_banks;
while (NULL != p->next)
{
bank_num += 1;
p = p->next;
}
p->next = bank;
bank_num += 1;
}
else
flash_banks = bank;
bank->bank_number = bank_num;
}
struct flash_bank *flash_bank_list(void)
{
return flash_banks;
}

31
src/flash/nor/core.h Normal file
View File

@ -0,0 +1,31 @@
/***************************************************************************
* Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef FLASH_NOR_CORE_H
#define FLASH_NOR_CORE_H
#include <flash/flash.h>
/**
* Find a NOR flash driver by its name.
* @param name The name of the requested driver.
* @returns The flash_driver called @c name, or NULL if not found.
*/
struct flash_driver *flash_driver_find_by_name(const char *name);
#endif // FLASH_NOR_CORE_H

View File

@ -19,9 +19,18 @@
#ifndef FLASH_NOR_IMP_H
#define FLASH_NOR_IMP_H
#include <flash/flash.h>
// this is an internal header
#include "core.h"
extern struct flash_driver *flash_drivers[];
extern struct flash_bank *flash_banks;
/**
* Adds a new NOR bank to the global list of banks.
* @params bank The bank that should be added.
*/
void flash_bank_add(struct flash_bank *bank);
/**
* @return The first bank in the global list.
*/
struct flash_bank *flash_bank_list(void);
#endif // FLASH_NOR_IMP_H

View File

@ -44,84 +44,65 @@ COMMAND_HANDLER(handle_flash_bank_command)
}
const char *driver_name = CMD_ARGV[0];
for (unsigned i = 0; flash_drivers[i]; i++)
struct flash_driver *driver = flash_driver_find_by_name(driver_name);
if (NULL == driver)
{
if (strcmp(driver_name, flash_drivers[i]->name) != 0)
continue;
/* register flash specific commands */
if (NULL != flash_drivers[i]->commands)
{
int retval = register_commands(CMD_CTX, NULL,
flash_drivers[i]->commands);
if (ERROR_OK != retval)
{
LOG_ERROR("couldn't register '%s' commands",
driver_name);
return ERROR_FAIL;
}
}
struct flash_bank *p, *c;
c = malloc(sizeof(struct flash_bank));
c->name = strdup(bank_name);
c->target = target;
c->driver = flash_drivers[i];
c->driver_priv = NULL;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], c->base);
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], c->size);
COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], c->chip_width);
COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], c->bus_width);
c->num_sectors = 0;
c->sectors = NULL;
c->next = NULL;
int retval;
retval = CALL_COMMAND_HANDLER(flash_drivers[i]->flash_bank_command, c);
if (ERROR_OK != retval)
{
LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32,
driver_name, c->base);
free(c);
return retval;
}
/* put flash bank in linked list */
if (flash_banks)
{
int bank_num = 0;
/* find last flash bank */
for (p = flash_banks; p && p->next; p = p->next) bank_num++;
if (p)
p->next = c;
c->bank_number = bank_num + 1;
}
else
{
flash_banks = c;
c->bank_number = 0;
}
return ERROR_OK;
/* no matching flash driver found */
LOG_ERROR("flash driver '%s' not found", driver_name);
return ERROR_FAIL;
}
/* no matching flash driver found */
LOG_ERROR("flash driver '%s' not found", driver_name);
return ERROR_FAIL;
/* register flash specific commands */
if (NULL != driver->commands)
{
int retval = register_commands(CMD_CTX, NULL,
driver->commands);
if (ERROR_OK != retval)
{
LOG_ERROR("couldn't register '%s' commands",
driver_name);
return ERROR_FAIL;
}
}
struct flash_bank *c = malloc(sizeof(*c));
c->name = strdup(bank_name);
c->target = target;
c->driver = driver;
c->driver_priv = NULL;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], c->base);
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], c->size);
COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], c->chip_width);
COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], c->bus_width);
c->num_sectors = 0;
c->sectors = NULL;
c->next = NULL;
int retval;
retval = CALL_COMMAND_HANDLER(driver->flash_bank_command, c);
if (ERROR_OK != retval)
{
LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32,
driver_name, c->base);
free(c);
return retval;
}
return ERROR_OK;
}
static int jim_flash_banks(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
struct flash_bank *p;
if (argc != 1) {
Jim_WrongNumArgs(interp, 1, argv, "no arguments to flash_banks command");
return JIM_ERR;
}
Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
for (p = flash_banks; p; p = p->next)
for (struct flash_bank *p = flash_bank_list(); p; p = p->next)
{
Jim_Obj *elem = Jim_NewListObj(interp, NULL, 0);