2007-10-08 17:26:33 +04:00
|
|
|
/*
|
|
|
|
* QEMU ETRAX System Emulator
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Edgar E. Iglesias, Axis Communications AB.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
2007-11-17 20:14:51 +03:00
|
|
|
#include "hw.h"
|
2008-05-03 02:21:55 +04:00
|
|
|
#include "qemu-char.h"
|
2009-02-22 14:59:59 +03:00
|
|
|
#include "etraxfs.h"
|
2007-10-08 17:26:33 +04:00
|
|
|
|
2008-03-01 20:25:33 +03:00
|
|
|
#define D(x)
|
|
|
|
|
2009-05-05 15:13:18 +04:00
|
|
|
#define RW_TR_CTRL (0x00 / 4)
|
|
|
|
#define RW_TR_DMA_EN (0x04 / 4)
|
|
|
|
#define RW_REC_CTRL (0x08 / 4)
|
|
|
|
#define RW_DOUT (0x1c / 4)
|
|
|
|
#define RS_STAT_DIN (0x20 / 4)
|
|
|
|
#define R_STAT_DIN (0x24 / 4)
|
|
|
|
#define RW_INTR_MASK (0x2c / 4)
|
|
|
|
#define RW_ACK_INTR (0x30 / 4)
|
|
|
|
#define R_INTR (0x34 / 4)
|
|
|
|
#define R_MASKED_INTR (0x38 / 4)
|
|
|
|
#define R_MAX (0x3c / 4)
|
2007-10-08 17:26:33 +04:00
|
|
|
|
2008-05-03 02:21:55 +04:00
|
|
|
#define STAT_DAV 16
|
|
|
|
#define STAT_TR_IDLE 22
|
|
|
|
#define STAT_TR_RDY 24
|
|
|
|
|
2009-05-05 14:50:45 +04:00
|
|
|
struct etrax_serial
|
2007-10-08 17:26:33 +04:00
|
|
|
{
|
2008-05-03 02:21:55 +04:00
|
|
|
CPUState *env;
|
|
|
|
CharDriverState *chr;
|
|
|
|
qemu_irq *irq;
|
|
|
|
|
2009-05-05 15:13:18 +04:00
|
|
|
/* This pending thing is a hack. */
|
2008-05-03 02:21:55 +04:00
|
|
|
int pending_tx;
|
|
|
|
|
|
|
|
/* Control registers. */
|
2009-05-05 15:13:18 +04:00
|
|
|
uint32_t regs[R_MAX];
|
2008-05-03 02:21:55 +04:00
|
|
|
};
|
|
|
|
|
2009-05-05 14:50:45 +04:00
|
|
|
static void ser_update_irq(struct etrax_serial *s)
|
2008-05-03 02:21:55 +04:00
|
|
|
{
|
2009-05-05 15:13:18 +04:00
|
|
|
s->regs[R_INTR] &= ~(s->regs[RW_ACK_INTR]);
|
|
|
|
s->regs[R_MASKED_INTR] = s->regs[R_INTR] & s->regs[RW_INTR_MASK];
|
|
|
|
|
|
|
|
qemu_set_irq(s->irq[0], !!s->regs[R_MASKED_INTR]);
|
|
|
|
s->regs[RW_ACK_INTR] = 0;
|
2007-10-08 17:26:33 +04:00
|
|
|
}
|
2008-05-03 02:21:55 +04:00
|
|
|
|
2007-10-08 17:26:33 +04:00
|
|
|
static uint32_t ser_readl (void *opaque, target_phys_addr_t addr)
|
|
|
|
{
|
2009-05-05 14:50:45 +04:00
|
|
|
struct etrax_serial *s = opaque;
|
2008-05-03 02:21:55 +04:00
|
|
|
D(CPUState *env = s->env);
|
2007-10-08 17:26:33 +04:00
|
|
|
uint32_t r = 0;
|
|
|
|
|
2009-05-05 15:13:18 +04:00
|
|
|
addr >>= 2;
|
2008-12-07 02:19:03 +03:00
|
|
|
switch (addr)
|
2007-10-08 17:26:33 +04:00
|
|
|
{
|
|
|
|
case R_STAT_DIN:
|
2009-05-05 15:13:18 +04:00
|
|
|
r = s->regs[RS_STAT_DIN];
|
2008-05-03 02:21:55 +04:00
|
|
|
break;
|
2009-05-05 15:13:18 +04:00
|
|
|
case RS_STAT_DIN:
|
|
|
|
r = s->regs[addr];
|
|
|
|
/* Read side-effect: clear dav. */
|
|
|
|
s->regs[addr] &= ~(1 << STAT_DAV);
|
2007-10-08 17:26:33 +04:00
|
|
|
break;
|
|
|
|
default:
|
2009-05-05 15:13:18 +04:00
|
|
|
r = s->regs[addr];
|
|
|
|
D(printf ("%s %x=%x\n", __func__, addr, r));
|
2007-10-08 17:26:33 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ser_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
|
|
|
|
{
|
2009-05-05 14:50:45 +04:00
|
|
|
struct etrax_serial *s = opaque;
|
2008-05-03 02:21:55 +04:00
|
|
|
unsigned char ch = value;
|
|
|
|
D(CPUState *env = s->env);
|
2007-10-08 17:26:33 +04:00
|
|
|
|
2009-05-05 15:13:18 +04:00
|
|
|
D(printf ("%s %x %x\n", __func__, addr, value));
|
|
|
|
addr >>= 2;
|
2008-12-07 02:19:03 +03:00
|
|
|
switch (addr)
|
2007-10-08 17:26:33 +04:00
|
|
|
{
|
|
|
|
case RW_DOUT:
|
2008-05-03 02:21:55 +04:00
|
|
|
qemu_chr_write(s->chr, &ch, 1);
|
2009-05-05 15:13:18 +04:00
|
|
|
s->regs[R_INTR] |= 1;
|
2008-05-03 02:21:55 +04:00
|
|
|
s->pending_tx = 1;
|
2009-05-05 15:13:18 +04:00
|
|
|
s->regs[addr] = value;
|
2008-05-03 02:21:55 +04:00
|
|
|
break;
|
|
|
|
case RW_ACK_INTR:
|
2009-05-05 15:13:18 +04:00
|
|
|
s->regs[addr] = value;
|
|
|
|
if (s->pending_tx && (s->regs[addr] & 1)) {
|
|
|
|
s->regs[R_INTR] |= 1;
|
2008-05-03 02:21:55 +04:00
|
|
|
s->pending_tx = 0;
|
2009-05-05 15:13:18 +04:00
|
|
|
s->regs[addr] &= ~1;
|
2008-05-03 02:21:55 +04:00
|
|
|
}
|
|
|
|
break;
|
2007-10-08 17:26:33 +04:00
|
|
|
default:
|
2009-05-05 15:13:18 +04:00
|
|
|
s->regs[addr] = value;
|
2007-10-08 17:26:33 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-05-03 02:21:55 +04:00
|
|
|
ser_update_irq(s);
|
2007-10-08 17:26:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static CPUReadMemoryFunc *ser_read[] = {
|
2009-05-05 14:48:54 +04:00
|
|
|
NULL, NULL,
|
2008-03-14 04:50:49 +03:00
|
|
|
&ser_readl,
|
2007-10-08 17:26:33 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static CPUWriteMemoryFunc *ser_write[] = {
|
2009-05-05 14:48:54 +04:00
|
|
|
NULL, NULL,
|
2008-03-14 04:50:49 +03:00
|
|
|
&ser_writel,
|
2007-10-08 17:26:33 +04:00
|
|
|
};
|
|
|
|
|
2008-05-03 02:21:55 +04:00
|
|
|
static void serial_receive(void *opaque, const uint8_t *buf, int size)
|
2007-10-08 17:26:33 +04:00
|
|
|
{
|
2009-05-05 14:50:45 +04:00
|
|
|
struct etrax_serial *s = opaque;
|
2008-05-03 02:21:55 +04:00
|
|
|
|
2009-05-05 15:13:18 +04:00
|
|
|
s->regs[R_INTR] |= 8;
|
|
|
|
s->regs[RS_STAT_DIN] &= ~0xff;
|
|
|
|
s->regs[RS_STAT_DIN] |= (buf[0] & 0xff);
|
|
|
|
s->regs[RS_STAT_DIN] |= (1 << STAT_DAV); /* dav. */
|
2008-05-03 02:21:55 +04:00
|
|
|
ser_update_irq(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int serial_can_receive(void *opaque)
|
|
|
|
{
|
2009-05-05 14:50:45 +04:00
|
|
|
struct etrax_serial *s = opaque;
|
2008-05-03 02:21:55 +04:00
|
|
|
int r;
|
|
|
|
|
|
|
|
/* Is the receiver enabled? */
|
2009-05-05 15:13:18 +04:00
|
|
|
r = s->regs[RW_REC_CTRL] & 1;
|
2008-05-03 02:21:55 +04:00
|
|
|
|
|
|
|
/* Pending rx data? */
|
2009-05-05 15:13:18 +04:00
|
|
|
r |= !(s->regs[R_INTR] & 8);
|
2008-05-03 02:21:55 +04:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_event(void *opaque, int event)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void etraxfs_ser_init(CPUState *env, qemu_irq *irq, CharDriverState *chr,
|
|
|
|
target_phys_addr_t base)
|
|
|
|
{
|
2009-05-05 14:50:45 +04:00
|
|
|
struct etrax_serial *s;
|
2007-10-08 17:26:33 +04:00
|
|
|
int ser_regs;
|
2008-05-03 02:21:55 +04:00
|
|
|
|
|
|
|
s = qemu_mallocz(sizeof *s);
|
|
|
|
|
|
|
|
s->env = env;
|
|
|
|
s->irq = irq;
|
|
|
|
s->chr = chr;
|
|
|
|
|
|
|
|
/* transmitter begins ready and idle. */
|
2009-05-05 15:13:18 +04:00
|
|
|
s->regs[RS_STAT_DIN] |= (1 << STAT_TR_RDY);
|
|
|
|
s->regs[RS_STAT_DIN] |= (1 << STAT_TR_IDLE);
|
2008-05-03 02:21:55 +04:00
|
|
|
|
|
|
|
qemu_chr_add_handlers(chr, serial_can_receive, serial_receive,
|
|
|
|
serial_event, s);
|
|
|
|
|
|
|
|
ser_regs = cpu_register_io_memory(0, ser_read, ser_write, s);
|
2009-05-05 15:13:18 +04:00
|
|
|
cpu_register_physical_memory (base, R_MAX * 4, ser_regs);
|
2007-10-08 17:26:33 +04:00
|
|
|
}
|