Statistics
| Branch: | Revision:

root / hw / etraxfs_ser.c @ 37873241

History | View | Annotate | Download (5.7 kB)

1 83fa1010 ths
/*
2 83fa1010 ths
 * QEMU ETRAX System Emulator
3 83fa1010 ths
 *
4 83fa1010 ths
 * Copyright (c) 2007 Edgar E. Iglesias, Axis Communications AB.
5 83fa1010 ths
 *
6 83fa1010 ths
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 83fa1010 ths
 * of this software and associated documentation files (the "Software"), to deal
8 83fa1010 ths
 * in the Software without restriction, including without limitation the rights
9 83fa1010 ths
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 83fa1010 ths
 * copies of the Software, and to permit persons to whom the Software is
11 83fa1010 ths
 * furnished to do so, subject to the following conditions:
12 83fa1010 ths
 *
13 83fa1010 ths
 * The above copyright notice and this permission notice shall be included in
14 83fa1010 ths
 * all copies or substantial portions of the Software.
15 83fa1010 ths
 *
16 83fa1010 ths
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 83fa1010 ths
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 83fa1010 ths
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 83fa1010 ths
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 83fa1010 ths
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 83fa1010 ths
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 83fa1010 ths
 * THE SOFTWARE.
23 83fa1010 ths
 */
24 83fa1010 ths
25 83fa1010 ths
#include <stdio.h>
26 83fa1010 ths
#include <ctype.h>
27 87ecb68b pbrook
#include "hw.h"
28 f062058f edgar_igl
#include "qemu-char.h"
29 cc53adbc edgar_igl
#include "etraxfs.h"
30 83fa1010 ths
31 bbaf29c7 edgar_igl
#define D(x)
32 bbaf29c7 edgar_igl
33 f062058f edgar_igl
#define RW_TR_CTRL     0x00
34 f062058f edgar_igl
#define RW_TR_DMA_EN   0x04
35 f062058f edgar_igl
#define RW_REC_CTRL    0x08
36 f062058f edgar_igl
#define RW_DOUT        0x1c
37 f062058f edgar_igl
#define RS_STAT_DIN    0x20
38 f062058f edgar_igl
#define R_STAT_DIN     0x24
39 f062058f edgar_igl
#define RW_INTR_MASK   0x2c
40 f062058f edgar_igl
#define RW_ACK_INTR    0x30
41 f062058f edgar_igl
#define R_INTR         0x34
42 f062058f edgar_igl
#define R_MASKED_INTR  0x38
43 83fa1010 ths
44 f062058f edgar_igl
#define STAT_DAV     16
45 f062058f edgar_igl
#define STAT_TR_IDLE 22
46 f062058f edgar_igl
#define STAT_TR_RDY  24
47 f062058f edgar_igl
48 f062058f edgar_igl
struct etrax_serial_t
49 83fa1010 ths
{
50 f062058f edgar_igl
        CPUState *env;
51 f062058f edgar_igl
        CharDriverState *chr;
52 f062058f edgar_igl
        qemu_irq *irq;
53 f062058f edgar_igl
54 f062058f edgar_igl
        int pending_tx;
55 f062058f edgar_igl
56 f062058f edgar_igl
        /* Control registers.  */
57 f062058f edgar_igl
        uint32_t rw_tr_ctrl;
58 f062058f edgar_igl
        uint32_t rw_tr_dma_en;
59 f062058f edgar_igl
        uint32_t rw_rec_ctrl;
60 f062058f edgar_igl
        uint32_t rs_stat_din;
61 f062058f edgar_igl
        uint32_t r_stat_din;
62 f062058f edgar_igl
        uint32_t rw_intr_mask;
63 f062058f edgar_igl
        uint32_t rw_ack_intr;
64 f062058f edgar_igl
        uint32_t r_intr;
65 f062058f edgar_igl
        uint32_t r_masked_intr;
66 f062058f edgar_igl
};
67 f062058f edgar_igl
68 f062058f edgar_igl
static void ser_update_irq(struct etrax_serial_t *s)
69 f062058f edgar_igl
{
70 f062058f edgar_igl
        uint32_t o_irq = s->r_masked_intr;
71 f062058f edgar_igl
72 f062058f edgar_igl
        s->r_intr &= ~(s->rw_ack_intr);
73 f062058f edgar_igl
        s->r_masked_intr = s->r_intr & s->rw_intr_mask;
74 f062058f edgar_igl
75 f062058f edgar_igl
        if (o_irq != s->r_masked_intr) {
76 f062058f edgar_igl
                D(printf("irq_mask=%x r_intr=%x rmi=%x airq=%x \n", 
77 f062058f edgar_igl
                         s->rw_intr_mask, s->r_intr, 
78 f062058f edgar_igl
                         s->r_masked_intr, s->rw_ack_intr));
79 f062058f edgar_igl
                if (s->r_masked_intr)
80 f062058f edgar_igl
                        qemu_irq_raise(s->irq[0]);
81 f062058f edgar_igl
                else
82 f062058f edgar_igl
                        qemu_irq_lower(s->irq[0]);
83 f062058f edgar_igl
        }
84 f062058f edgar_igl
        s->rw_ack_intr = 0;
85 83fa1010 ths
}
86 f062058f edgar_igl
87 f062058f edgar_igl
88 f062058f edgar_igl
static uint32_t ser_readb (void *opaque, target_phys_addr_t addr)
89 83fa1010 ths
{
90 ca87d03b edgar_igl
        D(CPUState *env = opaque);
91 d27b2e50 edgar_igl
        D(printf ("%s %x\n", __func__, addr));
92 ca87d03b edgar_igl
        return 0;
93 83fa1010 ths
}
94 83fa1010 ths
95 83fa1010 ths
static uint32_t ser_readl (void *opaque, target_phys_addr_t addr)
96 83fa1010 ths
{
97 f062058f edgar_igl
        struct etrax_serial_t *s = opaque;
98 f062058f edgar_igl
        D(CPUState *env = s->env);
99 83fa1010 ths
        uint32_t r = 0;
100 83fa1010 ths
101 0db74b07 edgar_igl
        switch (addr)
102 83fa1010 ths
        {
103 f062058f edgar_igl
                case RW_TR_CTRL:
104 f062058f edgar_igl
                        r = s->rw_tr_ctrl;
105 f062058f edgar_igl
                        break;
106 83fa1010 ths
                case RW_TR_DMA_EN:
107 f062058f edgar_igl
                        r = s->rw_tr_dma_en;
108 f062058f edgar_igl
                        break;
109 f062058f edgar_igl
                case RS_STAT_DIN:
110 f062058f edgar_igl
                        r = s->rs_stat_din;
111 f062058f edgar_igl
                        /* clear dav.  */
112 f062058f edgar_igl
                        s->rs_stat_din &= ~(1 << STAT_DAV);
113 83fa1010 ths
                        break;
114 83fa1010 ths
                case R_STAT_DIN:
115 f062058f edgar_igl
                        r = s->rs_stat_din;
116 f062058f edgar_igl
                        break;
117 f062058f edgar_igl
                case RW_ACK_INTR:
118 f062058f edgar_igl
                        D(printf("load rw_ack_intr=%x\n", s->rw_ack_intr));
119 f062058f edgar_igl
                        r = s->rw_ack_intr;
120 f062058f edgar_igl
                        break;
121 f062058f edgar_igl
                case RW_INTR_MASK:
122 f062058f edgar_igl
                        r = s->rw_intr_mask;
123 f062058f edgar_igl
                        break;
124 f062058f edgar_igl
                case R_INTR:
125 f062058f edgar_igl
                        D(printf("load r_intr=%x\n", s->r_intr));
126 f062058f edgar_igl
                        r = s->r_intr;
127 f062058f edgar_igl
                        break;
128 f062058f edgar_igl
                case R_MASKED_INTR:
129 f062058f edgar_igl
                        D(printf("load r_maked_intr=%x\n", s->r_masked_intr));
130 f062058f edgar_igl
                        r = s->r_masked_intr;
131 83fa1010 ths
                        break;
132 83fa1010 ths
133 83fa1010 ths
                default:
134 d27b2e50 edgar_igl
                        D(printf ("%s %x\n", __func__, addr));
135 83fa1010 ths
                        break;
136 83fa1010 ths
        }
137 83fa1010 ths
        return r;
138 83fa1010 ths
}
139 83fa1010 ths
140 83fa1010 ths
static void
141 83fa1010 ths
ser_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
142 83fa1010 ths
{
143 f062058f edgar_igl
        D(struct etrax_serial_t *s = opaque);
144 f062058f edgar_igl
        D(CPUState *env = s->env);
145 d27b2e50 edgar_igl
         D(printf ("%s %x %x\n", __func__, addr, value));
146 83fa1010 ths
}
147 83fa1010 ths
static void
148 83fa1010 ths
ser_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
149 83fa1010 ths
{
150 f062058f edgar_igl
        struct etrax_serial_t *s = opaque;
151 f062058f edgar_igl
        unsigned char ch = value;
152 f062058f edgar_igl
        D(CPUState *env = s->env);
153 83fa1010 ths
154 0db74b07 edgar_igl
        switch (addr)
155 83fa1010 ths
        {
156 f062058f edgar_igl
                case RW_TR_CTRL:
157 f062058f edgar_igl
                        D(printf("rw_tr_ctrl=%x\n", value));
158 f062058f edgar_igl
                        s->rw_tr_ctrl = value;
159 f062058f edgar_igl
                        break;
160 83fa1010 ths
                case RW_TR_DMA_EN:
161 f062058f edgar_igl
                        D(printf("rw_tr_dma_en=%x\n", value));
162 f062058f edgar_igl
                        s->rw_tr_dma_en = value;
163 83fa1010 ths
                        break;
164 83fa1010 ths
                case RW_DOUT:
165 f062058f edgar_igl
                        qemu_chr_write(s->chr, &ch, 1);
166 f062058f edgar_igl
                        s->r_intr |= 1;
167 f062058f edgar_igl
                        s->pending_tx = 1;
168 f062058f edgar_igl
                        break;
169 f062058f edgar_igl
                case RW_ACK_INTR:
170 f062058f edgar_igl
                        D(printf("rw_ack_intr=%x\n", value));
171 f062058f edgar_igl
                        s->rw_ack_intr = value;
172 f062058f edgar_igl
                        if (s->pending_tx && (s->rw_ack_intr & 1)) {
173 f062058f edgar_igl
                                s->r_intr |= 1;
174 f062058f edgar_igl
                                s->pending_tx = 0;
175 f062058f edgar_igl
                                s->rw_ack_intr &= ~1;
176 f062058f edgar_igl
                        }
177 f062058f edgar_igl
                        break;
178 f062058f edgar_igl
                case RW_INTR_MASK:
179 f062058f edgar_igl
                        D(printf("r_intr_mask=%x\n", value));
180 f062058f edgar_igl
                        s->rw_intr_mask = value;
181 83fa1010 ths
                        break;
182 83fa1010 ths
                default:
183 d27b2e50 edgar_igl
                        D(printf ("%s %x %x\n",  __func__, addr, value));
184 83fa1010 ths
                        break;
185 83fa1010 ths
        }
186 f062058f edgar_igl
        ser_update_irq(s);
187 83fa1010 ths
}
188 83fa1010 ths
189 83fa1010 ths
static CPUReadMemoryFunc *ser_read[] = {
190 ca87d03b edgar_igl
        &ser_readb,
191 f062058f edgar_igl
        &ser_readb,
192 ca87d03b edgar_igl
        &ser_readl,
193 83fa1010 ths
};
194 83fa1010 ths
195 83fa1010 ths
static CPUWriteMemoryFunc *ser_write[] = {
196 ca87d03b edgar_igl
        &ser_writeb,
197 f062058f edgar_igl
        &ser_writeb,
198 ca87d03b edgar_igl
        &ser_writel,
199 83fa1010 ths
};
200 83fa1010 ths
201 f062058f edgar_igl
static void serial_receive(void *opaque, const uint8_t *buf, int size)
202 83fa1010 ths
{
203 f062058f edgar_igl
        struct etrax_serial_t *s = opaque;
204 f062058f edgar_igl
205 f062058f edgar_igl
        s->r_intr |= 8;
206 f062058f edgar_igl
        s->rs_stat_din &= ~0xff;
207 f062058f edgar_igl
        s->rs_stat_din |= (buf[0] & 0xff);
208 f062058f edgar_igl
        s->rs_stat_din |= (1 << STAT_DAV); /* dav.  */
209 f062058f edgar_igl
        ser_update_irq(s);
210 f062058f edgar_igl
}
211 f062058f edgar_igl
212 f062058f edgar_igl
static int serial_can_receive(void *opaque)
213 f062058f edgar_igl
{
214 f062058f edgar_igl
        struct etrax_serial_t *s = opaque;
215 f062058f edgar_igl
        int r;
216 f062058f edgar_igl
217 f062058f edgar_igl
        /* Is the receiver enabled?  */
218 f062058f edgar_igl
        r = s->rw_rec_ctrl & 1;
219 f062058f edgar_igl
220 f062058f edgar_igl
        /* Pending rx data?  */
221 f062058f edgar_igl
        r |= !(s->r_intr & 8);
222 f062058f edgar_igl
        return r;
223 f062058f edgar_igl
}
224 f062058f edgar_igl
225 f062058f edgar_igl
static void serial_event(void *opaque, int event)
226 f062058f edgar_igl
{
227 f062058f edgar_igl
228 f062058f edgar_igl
}
229 f062058f edgar_igl
230 f062058f edgar_igl
void etraxfs_ser_init(CPUState *env, qemu_irq *irq, CharDriverState *chr,
231 f062058f edgar_igl
                      target_phys_addr_t base)
232 f062058f edgar_igl
{
233 f062058f edgar_igl
        struct etrax_serial_t *s;
234 83fa1010 ths
        int ser_regs;
235 f062058f edgar_igl
236 f062058f edgar_igl
        s = qemu_mallocz(sizeof *s);
237 f062058f edgar_igl
238 f062058f edgar_igl
        s->env = env;
239 f062058f edgar_igl
        s->irq = irq;
240 f062058f edgar_igl
        s->chr = chr;
241 f062058f edgar_igl
242 f062058f edgar_igl
        /* transmitter begins ready and idle.  */
243 f062058f edgar_igl
        s->rs_stat_din |= (1 << STAT_TR_RDY);
244 f062058f edgar_igl
        s->rs_stat_din |= (1 << STAT_TR_IDLE);
245 f062058f edgar_igl
246 f062058f edgar_igl
        qemu_chr_add_handlers(chr, serial_can_receive, serial_receive,
247 f062058f edgar_igl
                              serial_event, s);
248 f062058f edgar_igl
249 f062058f edgar_igl
        ser_regs = cpu_register_io_memory(0, ser_read, ser_write, s);
250 ca87d03b edgar_igl
        cpu_register_physical_memory (base, 0x3c, ser_regs);
251 83fa1010 ths
}