2007-10-08 21:26:33 +08: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-18 01:14:51 +08:00
|
|
|
#include "hw.h"
|
2008-05-03 06:21:55 +08:00
|
|
|
#include "qemu-char.h"
|
2009-02-22 19:59:59 +08:00
|
|
|
#include "etraxfs.h"
|
2007-10-08 21:26:33 +08:00
|
|
|
|
2008-03-02 01:25:33 +08:00
|
|
|
#define D(x)
|
|
|
|
|
2008-05-03 06:21:55 +08:00
|
|
|
#define RW_TR_CTRL 0x00
|
|
|
|
#define RW_TR_DMA_EN 0x04
|
|
|
|
#define RW_REC_CTRL 0x08
|
|
|
|
#define RW_DOUT 0x1c
|
|
|
|
#define RS_STAT_DIN 0x20
|
|
|
|
#define R_STAT_DIN 0x24
|
|
|
|
#define RW_INTR_MASK 0x2c
|
|
|
|
#define RW_ACK_INTR 0x30
|
|
|
|
#define R_INTR 0x34
|
|
|
|
#define R_MASKED_INTR 0x38
|
2007-10-08 21:26:33 +08:00
|
|
|
|
2008-05-03 06:21:55 +08:00
|
|
|
#define STAT_DAV 16
|
|
|
|
#define STAT_TR_IDLE 22
|
|
|
|
#define STAT_TR_RDY 24
|
|
|
|
|
2009-05-05 18:50:45 +08:00
|
|
|
struct etrax_serial
|
2007-10-08 21:26:33 +08:00
|
|
|
{
|
2008-05-03 06:21:55 +08:00
|
|
|
CPUState *env;
|
|
|
|
CharDriverState *chr;
|
|
|
|
qemu_irq *irq;
|
|
|
|
|
|
|
|
int pending_tx;
|
|
|
|
|
|
|
|
/* Control registers. */
|
|
|
|
uint32_t rw_tr_ctrl;
|
|
|
|
uint32_t rw_tr_dma_en;
|
|
|
|
uint32_t rw_rec_ctrl;
|
|
|
|
uint32_t rs_stat_din;
|
|
|
|
uint32_t r_stat_din;
|
|
|
|
uint32_t rw_intr_mask;
|
|
|
|
uint32_t rw_ack_intr;
|
|
|
|
uint32_t r_intr;
|
|
|
|
uint32_t r_masked_intr;
|
|
|
|
};
|
|
|
|
|
2009-05-05 18:50:45 +08:00
|
|
|
static void ser_update_irq(struct etrax_serial *s)
|
2008-05-03 06:21:55 +08:00
|
|
|
{
|
|
|
|
s->r_intr &= ~(s->rw_ack_intr);
|
|
|
|
s->r_masked_intr = s->r_intr & s->rw_intr_mask;
|
|
|
|
|
2009-05-05 18:25:35 +08:00
|
|
|
D(printf("irq_mask=%x r_intr=%x rmi=%x airq=%x \n",
|
|
|
|
s->rw_intr_mask, s->r_intr,
|
|
|
|
s->r_masked_intr, s->rw_ack_intr));
|
|
|
|
qemu_set_irq(s->irq[0], !!s->r_masked_intr);
|
2008-05-03 06:21:55 +08:00
|
|
|
s->rw_ack_intr = 0;
|
2007-10-08 21:26:33 +08:00
|
|
|
}
|
2008-05-03 06:21:55 +08:00
|
|
|
|
2007-10-08 21:26:33 +08:00
|
|
|
static uint32_t ser_readl (void *opaque, target_phys_addr_t addr)
|
|
|
|
{
|
2009-05-05 18:50:45 +08:00
|
|
|
struct etrax_serial *s = opaque;
|
2008-05-03 06:21:55 +08:00
|
|
|
D(CPUState *env = s->env);
|
2007-10-08 21:26:33 +08:00
|
|
|
uint32_t r = 0;
|
|
|
|
|
2008-12-07 07:19:03 +08:00
|
|
|
switch (addr)
|
2007-10-08 21:26:33 +08:00
|
|
|
{
|
2008-05-03 06:21:55 +08:00
|
|
|
case RW_TR_CTRL:
|
|
|
|
r = s->rw_tr_ctrl;
|
|
|
|
break;
|
2007-10-08 21:26:33 +08:00
|
|
|
case RW_TR_DMA_EN:
|
2008-05-03 06:21:55 +08:00
|
|
|
r = s->rw_tr_dma_en;
|
|
|
|
break;
|
|
|
|
case RS_STAT_DIN:
|
|
|
|
r = s->rs_stat_din;
|
|
|
|
/* clear dav. */
|
|
|
|
s->rs_stat_din &= ~(1 << STAT_DAV);
|
2007-10-08 21:26:33 +08:00
|
|
|
break;
|
|
|
|
case R_STAT_DIN:
|
2008-05-03 06:21:55 +08:00
|
|
|
r = s->rs_stat_din;
|
|
|
|
break;
|
|
|
|
case RW_ACK_INTR:
|
|
|
|
D(printf("load rw_ack_intr=%x\n", s->rw_ack_intr));
|
|
|
|
r = s->rw_ack_intr;
|
|
|
|
break;
|
|
|
|
case RW_INTR_MASK:
|
|
|
|
r = s->rw_intr_mask;
|
|
|
|
break;
|
|
|
|
case R_INTR:
|
|
|
|
D(printf("load r_intr=%x\n", s->r_intr));
|
|
|
|
r = s->r_intr;
|
|
|
|
break;
|
|
|
|
case R_MASKED_INTR:
|
|
|
|
D(printf("load r_maked_intr=%x\n", s->r_masked_intr));
|
|
|
|
r = s->r_masked_intr;
|
2007-10-08 21:26:33 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2008-06-30 19:51:12 +08:00
|
|
|
D(printf ("%s %x\n", __func__, addr));
|
2007-10-08 21:26:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ser_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
|
|
|
|
{
|
2009-05-05 18:50:45 +08:00
|
|
|
struct etrax_serial *s = opaque;
|
2008-05-03 06:21:55 +08:00
|
|
|
unsigned char ch = value;
|
|
|
|
D(CPUState *env = s->env);
|
2007-10-08 21:26:33 +08:00
|
|
|
|
2008-12-07 07:19:03 +08:00
|
|
|
switch (addr)
|
2007-10-08 21:26:33 +08:00
|
|
|
{
|
2008-05-03 06:21:55 +08:00
|
|
|
case RW_TR_CTRL:
|
|
|
|
D(printf("rw_tr_ctrl=%x\n", value));
|
|
|
|
s->rw_tr_ctrl = value;
|
|
|
|
break;
|
2007-10-08 21:26:33 +08:00
|
|
|
case RW_TR_DMA_EN:
|
2008-05-03 06:21:55 +08:00
|
|
|
D(printf("rw_tr_dma_en=%x\n", value));
|
|
|
|
s->rw_tr_dma_en = value;
|
2007-10-08 21:26:33 +08:00
|
|
|
break;
|
|
|
|
case RW_DOUT:
|
2008-05-03 06:21:55 +08:00
|
|
|
qemu_chr_write(s->chr, &ch, 1);
|
|
|
|
s->r_intr |= 1;
|
|
|
|
s->pending_tx = 1;
|
|
|
|
break;
|
|
|
|
case RW_ACK_INTR:
|
|
|
|
D(printf("rw_ack_intr=%x\n", value));
|
|
|
|
s->rw_ack_intr = value;
|
|
|
|
if (s->pending_tx && (s->rw_ack_intr & 1)) {
|
|
|
|
s->r_intr |= 1;
|
|
|
|
s->pending_tx = 0;
|
|
|
|
s->rw_ack_intr &= ~1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RW_INTR_MASK:
|
|
|
|
D(printf("r_intr_mask=%x\n", value));
|
|
|
|
s->rw_intr_mask = value;
|
2007-10-08 21:26:33 +08:00
|
|
|
break;
|
|
|
|
default:
|
2008-06-30 19:51:12 +08:00
|
|
|
D(printf ("%s %x %x\n", __func__, addr, value));
|
2007-10-08 21:26:33 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-05-03 06:21:55 +08:00
|
|
|
ser_update_irq(s);
|
2007-10-08 21:26:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static CPUReadMemoryFunc *ser_read[] = {
|
2009-05-05 18:48:54 +08:00
|
|
|
NULL, NULL,
|
2008-03-14 09:50:49 +08:00
|
|
|
&ser_readl,
|
2007-10-08 21:26:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static CPUWriteMemoryFunc *ser_write[] = {
|
2009-05-05 18:48:54 +08:00
|
|
|
NULL, NULL,
|
2008-03-14 09:50:49 +08:00
|
|
|
&ser_writel,
|
2007-10-08 21:26:33 +08:00
|
|
|
};
|
|
|
|
|
2008-05-03 06:21:55 +08:00
|
|
|
static void serial_receive(void *opaque, const uint8_t *buf, int size)
|
2007-10-08 21:26:33 +08:00
|
|
|
{
|
2009-05-05 18:50:45 +08:00
|
|
|
struct etrax_serial *s = opaque;
|
2008-05-03 06:21:55 +08:00
|
|
|
|
|
|
|
s->r_intr |= 8;
|
|
|
|
s->rs_stat_din &= ~0xff;
|
|
|
|
s->rs_stat_din |= (buf[0] & 0xff);
|
|
|
|
s->rs_stat_din |= (1 << STAT_DAV); /* dav. */
|
|
|
|
ser_update_irq(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int serial_can_receive(void *opaque)
|
|
|
|
{
|
2009-05-05 18:50:45 +08:00
|
|
|
struct etrax_serial *s = opaque;
|
2008-05-03 06:21:55 +08:00
|
|
|
int r;
|
|
|
|
|
|
|
|
/* Is the receiver enabled? */
|
|
|
|
r = s->rw_rec_ctrl & 1;
|
|
|
|
|
|
|
|
/* Pending rx data? */
|
|
|
|
r |= !(s->r_intr & 8);
|
|
|
|
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 18:50:45 +08:00
|
|
|
struct etrax_serial *s;
|
2007-10-08 21:26:33 +08:00
|
|
|
int ser_regs;
|
2008-05-03 06:21:55 +08:00
|
|
|
|
|
|
|
s = qemu_mallocz(sizeof *s);
|
|
|
|
|
|
|
|
s->env = env;
|
|
|
|
s->irq = irq;
|
|
|
|
s->chr = chr;
|
|
|
|
|
|
|
|
/* transmitter begins ready and idle. */
|
|
|
|
s->rs_stat_din |= (1 << STAT_TR_RDY);
|
|
|
|
s->rs_stat_din |= (1 << STAT_TR_IDLE);
|
|
|
|
|
|
|
|
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);
|
2008-03-14 09:50:49 +08:00
|
|
|
cpu_register_physical_memory (base, 0x3c, ser_regs);
|
2007-10-08 21:26:33 +08:00
|
|
|
}
|