#ifndef _HW_GLUE_C_
#define _HW_GLUE_C_
#include "device_table.h"
enum {
max_nr_interrupts = 2048,
};
typedef enum _hw_glue_type {
glue_undefined = 0,
glue_io,
glue_and,
glue_nand,
glue_or,
glue_xor,
glue_nor,
glue_not,
} hw_glue_type;
typedef struct _hw_glue_device {
hw_glue_type type;
int int_number;
int *input;
int nr_inputs;
unsigned sizeof_input;
int space;
unsigned_word address;
unsigned sizeof_output;
int *output;
int nr_outputs;
} hw_glue_device;
static void
hw_glue_init_address(device *me)
{
hw_glue_device *glue = (hw_glue_device*)device_data(me);
generic_device_init_address(me);
if (glue->output != NULL) {
memset(glue->output, 0, glue->sizeof_output);
}
else {
reg_property_spec unit;
int reg_nr;
reg_nr = 0;
while (device_find_reg_array_property(me, "reg", reg_nr, &unit)
&& !device_size_to_attach_size(device_parent(me), &unit.size,
&glue->sizeof_output, me))
reg_nr++;
if (glue->sizeof_output == 0)
device_error(me, "at least one reg property size must be nonzero");
if (glue->sizeof_output % sizeof(unsigned_word) != 0)
device_error(me, "reg property size must be %d aligned", sizeof(unsigned_word));
device_address_to_attach_address(device_parent(me),
&unit.address, &glue->space, &glue->address,
me);
if (glue->address % (sizeof(unsigned_word) * max_nr_interrupts) != 0)
device_error(me, "reg property address must be %d aligned",
sizeof(unsigned_word) * max_nr_interrupts);
glue->nr_outputs = glue->sizeof_output / sizeof(unsigned_word);
glue->output = zalloc(glue->sizeof_output);
}
if (glue->input != NULL) {
memset(glue->input, 0, glue->sizeof_input);
}
else {
const device_property *ranges = device_find_property(me, "interrupt-ranges");
if (ranges == NULL) {
glue->int_number = 0;
glue->nr_inputs = glue->nr_outputs;
}
else if (ranges->sizeof_array != sizeof(unsigned_cell) * 2) {
device_error(me, "invalid interrupt-ranges property (incorrect size)");
}
else {
const unsigned_cell *int_range = ranges->array;
glue->int_number = BE2H_cell(int_range[0]);
glue->nr_inputs = BE2H_cell(int_range[1]);
}
glue->sizeof_input = glue->nr_inputs * sizeof(unsigned);
glue->input = zalloc(glue->sizeof_input);
}
if (glue->type == glue_undefined) {
const char *name = device_name(me);
if (strcmp(name, "glue") == 0)
glue->type = glue_io;
else if (strcmp(name, "glue-and") == 0)
glue->type = glue_and;
else
device_error(me, "unimplemented glue type");
}
DTRACE(glue, ("int-number %d, nr_inputs %d, nr_outputs %d\n",
glue->int_number, glue->nr_inputs, glue->nr_outputs));
}
static unsigned
hw_glue_io_read_buffer_callback(device *me,
void *dest,
int space,
unsigned_word addr,
unsigned nr_bytes,
cpu *processor,
unsigned_word cia)
{
hw_glue_device *glue = (hw_glue_device*)device_data(me);
int reg = ((addr - glue->address) / sizeof(unsigned_word)) % glue->nr_outputs;
if (nr_bytes != sizeof(unsigned_word)
|| (addr % sizeof(unsigned_word)) != 0)
device_error(me, "missaligned read access (%d:0x%lx:%d) not supported",
space, (unsigned long)addr, nr_bytes);
*(unsigned_word*)dest = H2BE_4(glue->output[reg]);
DTRACE(glue, ("read - interrupt %d (0x%lx), level %d\n",
reg, (unsigned long) addr, glue->output[reg]));
return nr_bytes;
}
static unsigned
hw_glue_io_write_buffer_callback(device *me,
const void *source,
int space,
unsigned_word addr,
unsigned nr_bytes,
cpu *processor,
unsigned_word cia)
{
hw_glue_device *glue = (hw_glue_device*)device_data(me);
int reg = ((addr - glue->address) / sizeof(unsigned_word)) % max_nr_interrupts;
if (nr_bytes != sizeof(unsigned_word)
|| (addr % sizeof(unsigned_word)) != 0)
device_error(me, "missaligned write access (%d:0x%lx:%d) not supported",
space, (unsigned long)addr, nr_bytes);
glue->output[reg] = H2BE_4(*(unsigned_word*)source);
DTRACE(glue, ("write - interrupt %d (0x%lx), level %d\n",
reg, (unsigned long) addr, glue->output[reg]));
device_interrupt_event(me, reg, glue->output[reg], processor, cia);
return nr_bytes;
}
static void
hw_glue_interrupt_event(device *me,
int my_port,
device *source,
int source_port,
int level,
cpu *processor,
unsigned_word cia)
{
hw_glue_device *glue = (hw_glue_device*)device_data(me);
int i;
if (my_port < glue->int_number
|| my_port >= glue->int_number + glue->nr_inputs)
device_error(me, "interrupt %d outside of valid range", my_port);
glue->input[my_port - glue->int_number] = level;
switch (glue->type) {
case glue_io:
{
int port = my_port % glue->nr_outputs;
glue->output[port] = level;
DTRACE(glue, ("input - interrupt %d (0x%lx), level %d\n",
my_port,
(unsigned long)glue->address + port * sizeof(unsigned_word),
level));
break;
}
case glue_and:
glue->output[0] = glue->input[0];
for (i = 1; i < glue->nr_inputs; i++)
glue->output[0] &= glue->input[i];
DTRACE(glue, ("and - interrupt %d, level %d arrived - output %d\n",
my_port, level, glue->output[0]));
device_interrupt_event(me, 0, glue->output[0], processor, cia);
break;
default:
device_error(me, "operator not implemented");
break;
}
}
static const device_interrupt_port_descriptor hw_glue_interrupt_ports[] = {
{ "int", 0, max_nr_interrupts },
{ NULL }
};
static device_callbacks const hw_glue_callbacks = {
{ hw_glue_init_address, NULL },
{ NULL, },
{ hw_glue_io_read_buffer_callback,
hw_glue_io_write_buffer_callback, },
{ NULL, },
{ hw_glue_interrupt_event, NULL, hw_glue_interrupt_ports },
{ NULL, },
NULL,
};
static void *
hw_glue_create(const char *name,
const device_unit *unit_address,
const char *args)
{
hw_glue_device *glue = ZALLOC(hw_glue_device);
return glue;
}
const device_descriptor hw_glue_device_descriptor[] = {
{ "glue", hw_glue_create, &hw_glue_callbacks },
{ "glue-and", hw_glue_create, &hw_glue_callbacks },
{ "glue-nand", hw_glue_create, &hw_glue_callbacks },
{ "glue-or", hw_glue_create, &hw_glue_callbacks },
{ "glue-xor", hw_glue_create, &hw_glue_callbacks },
{ "glue-nor", hw_glue_create, &hw_glue_callbacks },
{ "glue-not", hw_glue_create, &hw_glue_callbacks },
{ NULL },
};
#endif