1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
/*
skyeye_device.c - implement the device framework for skyeye
Copyright (C) 2004 Skyeye Develop Group
for help please send mail to <skyeye-developer@lists.gro.clinux.org>
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
*/
/*
* 05/04/2004 initial version
* desgin a device framework for skyeye to providing good exetension of device simulations
*
* walimis <wlm@student.dlut.edu.cn>
* */
#include "skyeye_device.h"
#include "skyeye_config.h"
static struct device_module_set **global_mod_set;
static int mod_set_count = 0, mod_set_count_max = 0;
void
initialize_all_devices ()
{
//uart_register();
//timer_register();
#ifndef NO_NET
net_register ();
#endif
#ifdef LCD
lcd_register ();
#endif
flash_register ();
touchscreen_register ();
sound_register ();
nandflash_register();
}
void
set_device_default (struct device_desc *dev, struct device_default_value *def)
{
struct device_default_value *def_p = def;
struct machine_config *mc = (struct machine_config *) dev->mach;
while (def_p->name != NULL) {
if ((strcasecmp (dev->name, def_p->name) == 0) ||
(strcasecmp (mc->machine_name, def_p->name) == 0)) {
if (dev->base == 0)
dev->base = def_p->base;
if (dev->size == 0)
dev->size = def_p->size;
if (dev->intr.interrupts[0] == 0)
memcpy (dev->intr.interrupts,
def_p->interrupts,
sizeof (def_p->interrupts));
return;
}
def_p++;
}
}
/* find a module from mod_set
* */
static struct device_module *
find_module (const char *name, struct device_module_set *mod_set)
{
int i;
struct device_module *dev_mod = NULL;
if (!name || *name == '\0')
return NULL;
for (i = 0; i < mod_set->count; i++) {
dev_mod = mod_set->mod[i];
if (strcmp (dev_mod->type_name, name) == 0)
break;
}
return dev_mod;
}
static struct device_module_set *
find_module_set (const char *name)
{
int i;
struct device_module_set *mod_set = NULL;
for (i = 0; i < mod_set_count; i++) {
mod_set = global_mod_set[i];
if (strcmp (mod_set->name, name) == 0)
break;
}
return mod_set;
}
int
setup_device (char *dev_name, struct common_config *conf,
void *option, machine_config_t * mc)
{
struct device_module_set *mod_set = NULL;
struct device_module *dev_mod = NULL;
struct device_desc *dev;
if ((mod_set = find_module_set (dev_name)) == NULL) {
SKYEYE_ERR ("can't find module set: %s\n", dev_name);
return 1;
}
if (mod_set->initialized == 0) {
mod_set->init (mod_set);
mod_set->initialized = 1;
}
if ((dev_mod = find_module (conf->type, mod_set)) == NULL) {
SKYEYE_ERR ("can't find device module: (name:%s, type:%s)\n",
mod_set->name, conf->type);
return 1;
}
mc->devices = realloc (mc->devices,
sizeof (struct device_desc *) *
(mc->dev_count + 1));
if (mc->devices == NULL) {
SKYEYE_ERR ("can't alloc memory for devices pionter: (name:%s, type:%s)\n",
mod_set->name, conf->type);
return 1;
}
dev = malloc (sizeof (struct device_desc));
if (dev == NULL) {
SKYEYE_ERR ("can't alloc memory for new device: (name:%s, type:%s)\n",
mod_set->name, conf->type);
return 1;
}
/* init device
* */
memset (dev, 0, sizeof (struct device_desc));
strncpy (dev->type, conf->type, MAX_STR_NAME);
dev->type[MAX_STR_NAME - 1] = '\0';
if (conf->name && *(conf->name) != '\0') {
strncpy (dev->name, conf->name, MAX_STR_NAME);
dev->name[MAX_STR_NAME - 1] = '\0';
}
dev->base = conf->base;
dev->size = conf->size;
memcpy (dev->intr.interrupts, conf->interrupts,
sizeof (conf->interrupts));
dev->intr.set_interrupt = mc->mach_set_intr;
dev->intr.pending_interrupt = mc->mach_pending_intr;
dev->intr.update_interrupt = mc->mach_update_intr;
dev->mem_op.write_byte = mc->mach_mem_write_byte;
dev->mem_op.read_byte = mc->mach_mem_read_byte;
dev->mach = (void *) mc;
/* setup device module data
* */
if (mod_set->setup_module) {
if (mod_set->setup_module (dev, option) != 0) {
free(dev);
SKYEYE_ERR ("failed to setup_module (name:%s, type:%s)\n",
mod_set->name, conf->type);
return 1;
}
}
mc->devices[mc->dev_count] = dev;
mc->dev_count++;
/* setup device data itself.
* */
dev_mod->setup (dev);
return 0;
}
/* register a "device module set" to the global module set "global_mod_set".
* called by the device register funtions. e.g. uart_register.
* */
int
register_device_module_set (struct device_module_set *mod_set)
{
if (mod_set_count == mod_set_count_max) {
mod_set_count_max += 4;
global_mod_set = realloc (global_mod_set,
sizeof (struct device_module_set *)
* mod_set_count_max);
if (global_mod_set == NULL)
return 1;
}
global_mod_set[mod_set_count] = mod_set;
mod_set_count++;
return 0;
}
/* register a "device module" to a specific "device module set",
* e.g. uart or net module set
* called by device module initialize funtions. e.g. uart_s3c4510b_init.
* */
int
register_device_module (char *name, struct device_module_set *mod_set,
int (*setup) (struct device_desc * dev))
{
struct device_module *dev_mod;
if (mod_set->count == mod_set->count_max) {
mod_set->count_max += 4;
mod_set->mod = realloc (mod_set->mod,
sizeof (struct device_module *) *
mod_set->count_max);
if (mod_set->mod == NULL)
return 1;
}
dev_mod = malloc (sizeof (struct device_module));
if (dev_mod == NULL)
return 1;
dev_mod->type_name = strdup (name);
dev_mod->setup = setup;
mod_set->mod[mod_set->count] = dev_mod;
mod_set->count++;
return 0;
}
|