Statistics
| Branch: | Revision:

root / hw / etraxfs_ser.c @ c2f01775

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 83fa1010 ths
30 bbaf29c7 edgar_igl
#define D(x)
31 bbaf29c7 edgar_igl
32 f062058f edgar_igl
#define RW_TR_CTRL     0x00
33 f062058f edgar_igl
#define RW_TR_DMA_EN   0x04
34 f062058f edgar_igl
#define RW_REC_CTRL    0x08
35 f062058f edgar_igl
#define RW_DOUT        0x1c
36 f062058f edgar_igl
#define RS_STAT_DIN    0x20
37 f062058f edgar_igl
#define R_STAT_DIN     0x24
38 f062058f edgar_igl
#define RW_INTR_MASK   0x2c
39 f062058f edgar_igl
#define RW_ACK_INTR    0x30
40 f062058f edgar_igl
#define R_INTR         0x34
41 f062058f edgar_igl
#define R_MASKED_INTR  0x38
42 83fa1010 ths
43 f062058f edgar_igl
#define STAT_DAV     16
44 f062058f edgar_igl
#define STAT_TR_IDLE 22
45 f062058f edgar_igl
#define STAT_TR_RDY  24
46 f062058f edgar_igl
47 f062058f edgar_igl
struct etrax_serial_t
48 83fa1010 ths
{
49 f062058f edgar_igl
        CPUState *env;
50 f062058f edgar_igl
        CharDriverState *chr;
51 f062058f edgar_igl
        qemu_irq *irq;
52 f062058f edgar_igl
53 f062058f edgar_igl
        int pending_tx;
54 f062058f edgar_igl
55 f062058f edgar_igl
        /* Control registers.  */
56 f062058f edgar_igl
        uint32_t rw_tr_ctrl;
57 f062058f edgar_igl
        uint32_t rw_tr_dma_en;
58 f062058f edgar_igl
        uint32_t rw_rec_ctrl;
59 f062058f edgar_igl
        uint32_t rs_stat_din;
60 f062058f edgar_igl
        uint32_t r_stat_din;
61 f062058f edgar_igl
        uint32_t rw_intr_mask;
62 f062058f edgar_igl
        uint32_t rw_ack_intr;
63 f062058f edgar_igl
        uint32_t r_intr;
64 f062058f edgar_igl
        uint32_t r_masked_intr;
65 f062058f edgar_igl
};
66 f062058f edgar_igl
67 f062058f edgar_igl
static void ser_update_irq(struct etrax_serial_t *s)
68 f062058f edgar_igl
{
69 f062058f edgar_igl
        uint32_t o_irq = s->r_masked_intr;
70 f062058f edgar_igl
71 f062058f edgar_igl
        s->r_intr &= ~(s->rw_ack_intr);
72 f062058f edgar_igl
        s->r_masked_intr = s->r_intr & s->rw_intr_mask;
73 f062058f edgar_igl
74 f062058f edgar_igl
        if (o_irq != s->r_masked_intr) {
75 f062058f edgar_igl
                D(printf("irq_mask=%x r_intr=%x rmi=%x airq=%x \n", 
76 f062058f edgar_igl
                         s->rw_intr_mask, s->r_intr, 
77 f062058f edgar_igl
                         s->r_masked_intr, s->rw_ack_intr));
78 f062058f edgar_igl
                if (s->r_masked_intr)
79 f062058f edgar_igl
                        qemu_irq_raise(s->irq[0]);
80 f062058f edgar_igl
                else
81 f062058f edgar_igl
                        qemu_irq_lower(s->irq[0]);
82 f062058f edgar_igl
        }
83 f062058f edgar_igl
        s->rw_ack_intr = 0;
84 83fa1010 ths
}
85 f062058f edgar_igl
86 f062058f edgar_igl
87 f062058f edgar_igl
static uint32_t ser_readb (void *opaque, target_phys_addr_t addr)
88 83fa1010 ths
{
89 ca87d03b edgar_igl
        D(CPUState *env = opaque);
90 d27b2e50 edgar_igl
        D(printf ("%s %x\n", __func__, addr));
91 ca87d03b edgar_igl
        return 0;
92 83fa1010 ths
}
93 83fa1010 ths
94 83fa1010 ths
static uint32_t ser_readl (void *opaque, target_phys_addr_t addr)
95 83fa1010 ths
{
96 f062058f edgar_igl
        struct etrax_serial_t *s = opaque;
97 f062058f edgar_igl
        D(CPUState *env = s->env);
98 83fa1010 ths
        uint32_t r = 0;
99 83fa1010 ths
100 0db74b07 edgar_igl
        switch (addr)
101 83fa1010 ths
        {
102 f062058f edgar_igl
                case RW_TR_CTRL:
103 f062058f edgar_igl
                        r = s->rw_tr_ctrl;
104 f062058f edgar_igl
                        break;
105 83fa1010 ths
                case RW_TR_DMA_EN:
106 f062058f edgar_igl
                        r = s->rw_tr_dma_en;
107 f062058f edgar_igl
                        break;
108 f062058f edgar_igl
                case RS_STAT_DIN:
109 f062058f edgar_igl
                        r = s->rs_stat_din;
110 f062058f edgar_igl
                        /* clear dav.  */
111 f062058f edgar_igl
                        s->rs_stat_din &= ~(1 << STAT_DAV);
112 83fa1010 ths
                        break;
113 83fa1010 ths
                case R_STAT_DIN:
114 f062058f edgar_igl
                        r = s->rs_stat_din;
115 f062058f edgar_igl
                        break;
116 f062058f edgar_igl
                case RW_ACK_INTR:
117 f062058f edgar_igl
                        D(printf("load rw_ack_intr=%x\n", s->rw_ack_intr));
118 f062058f edgar_igl
                        r = s->rw_ack_intr;
119 f062058f edgar_igl
                        break;
120 f062058f edgar_igl
                case RW_INTR_MASK:
121 f062058f edgar_igl
                        r = s->rw_intr_mask;
122 f062058f edgar_igl
                        break;
123 f062058f edgar_igl
                case R_INTR:
124 f062058f edgar_igl
                        D(printf("load r_intr=%x\n", s->r_intr));
125 f062058f edgar_igl
                        r = s->r_intr;
126 f062058f edgar_igl
                        break;
127 f062058f edgar_igl
                case R_MASKED_INTR:
128 f062058f edgar_igl
                        D(printf("load r_maked_intr=%x\n", s->r_masked_intr));
129 f062058f edgar_igl
                        r = s->r_masked_intr;
130 83fa1010 ths
                        break;
131 83fa1010 ths
132 83fa1010 ths
                default:
133 d27b2e50 edgar_igl
                        D(printf ("%s %x\n", __func__, addr));
134 83fa1010 ths
                        break;
135 83fa1010 ths
        }
136 83fa1010 ths
        return r;
137 83fa1010 ths
}
138 83fa1010 ths
139 83fa1010 ths
static void
140 83fa1010 ths
ser_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
141 83fa1010 ths
{
142 f062058f edgar_igl
        D(struct etrax_serial_t *s = opaque);
143 f062058f edgar_igl
        D(CPUState *env = s->env);
144 d27b2e50 edgar_igl
         D(printf ("%s %x %x\n", __func__, addr, value));
145 83fa1010 ths
}
146 83fa1010 ths
static void
147 83fa1010 ths
ser_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
148 83fa1010 ths
{
149 f062058f edgar_igl
        struct etrax_serial_t *s = opaque;
150 f062058f edgar_igl
        unsigned char ch = value;
151 f062058f edgar_igl
        D(CPUState *env = s->env);
152 83fa1010 ths
153 0db74b07 edgar_igl
        switch (addr)
154 83fa1010 ths
        {
155 f062058f edgar_igl
                case RW_TR_CTRL:
156 f062058f edgar_igl
                        D(printf("rw_tr_ctrl=%x\n", value));
157 f062058f edgar_igl
                        s->rw_tr_ctrl = value;
158 f062058f edgar_igl
                        break;
159 83fa1010 ths
                case RW_TR_DMA_EN:
160 f062058f edgar_igl
                        D(printf("rw_tr_dma_en=%x\n", value));
161 f062058f edgar_igl
                        s->rw_tr_dma_en = value;
162 83fa1010 ths
                        break;
163 83fa1010 ths
                case RW_DOUT:
164 f062058f edgar_igl
                        qemu_chr_write(s->chr, &ch, 1);
165 f062058f edgar_igl
                        s->r_intr |= 1;
166 f062058f edgar_igl
                        s->pending_tx = 1;
167 f062058f edgar_igl
                        break;
168 f062058f edgar_igl
                case RW_ACK_INTR:
169 f062058f edgar_igl
                        D(printf("rw_ack_intr=%x\n", value));
170 f062058f edgar_igl
                        s->rw_ack_intr = value;
171 f062058f edgar_igl
                        if (s->pending_tx && (s->rw_ack_intr & 1)) {
172 f062058f edgar_igl
                                s->r_intr |= 1;
173 f062058f edgar_igl
                                s->pending_tx = 0;
174 f062058f edgar_igl
                                s->rw_ack_intr &= ~1;
175 f062058f edgar_igl
                        }
176 f062058f edgar_igl
                        break;
177 f062058f edgar_igl
                case RW_INTR_MASK:
178 f062058f edgar_igl
                        D(printf("r_intr_mask=%x\n", value));
179 f062058f edgar_igl
                        s->rw_intr_mask = value;
180 83fa1010 ths
                        break;
181 83fa1010 ths
                default:
182 d27b2e50 edgar_igl
                        D(printf ("%s %x %x\n",  __func__, addr, value));
183 83fa1010 ths
                        break;
184 83fa1010 ths
        }
185 f062058f edgar_igl
        ser_update_irq(s);
186 83fa1010 ths
}
187 83fa1010 ths
188 83fa1010 ths
static CPUReadMemoryFunc *ser_read[] = {
189 ca87d03b edgar_igl
        &ser_readb,
190 f062058f edgar_igl
        &ser_readb,
191 ca87d03b edgar_igl
        &ser_readl,
192 83fa1010 ths
};
193 83fa1010 ths
194 83fa1010 ths
static CPUWriteMemoryFunc *ser_write[] = {
195 ca87d03b edgar_igl
        &ser_writeb,
196 f062058f edgar_igl
        &ser_writeb,
197 ca87d03b edgar_igl
        &ser_writel,
198 83fa1010 ths
};
199 83fa1010 ths
200 f062058f edgar_igl
static void serial_receive(void *opaque, const uint8_t *buf, int size)
201 83fa1010 ths
{
202 f062058f edgar_igl
        struct etrax_serial_t *s = opaque;
203 f062058f edgar_igl
204 f062058f edgar_igl
        s->r_intr |= 8;
205 f062058f edgar_igl
        s->rs_stat_din &= ~0xff;
206 f062058f edgar_igl
        s->rs_stat_din |= (buf[0] & 0xff);
207 f062058f edgar_igl
        s->rs_stat_din |= (1 << STAT_DAV); /* dav.  */
208 f062058f edgar_igl
        ser_update_irq(s);
209 f062058f edgar_igl
}
210 f062058f edgar_igl
211 f062058f edgar_igl
static int serial_can_receive(void *opaque)
212 f062058f edgar_igl
{
213 f062058f edgar_igl
        struct etrax_serial_t *s = opaque;
214 f062058f edgar_igl
        int r;
215 f062058f edgar_igl
216 f062058f edgar_igl
        /* Is the receiver enabled?  */
217 f062058f edgar_igl
        r = s->rw_rec_ctrl & 1;
218 f062058f edgar_igl
219 f062058f edgar_igl
        /* Pending rx data?  */
220 f062058f edgar_igl
        r |= !(s->r_intr & 8);
221 f062058f edgar_igl
        return r;
222 f062058f edgar_igl
}
223 f062058f edgar_igl
224 f062058f edgar_igl
static void serial_event(void *opaque, int event)
225 f062058f edgar_igl
{
226 f062058f edgar_igl
227 f062058f edgar_igl
}
228 f062058f edgar_igl
229 f062058f edgar_igl
void etraxfs_ser_init(CPUState *env, qemu_irq *irq, CharDriverState *chr,
230 f062058f edgar_igl
                      target_phys_addr_t base)
231 f062058f edgar_igl
{
232 f062058f edgar_igl
        struct etrax_serial_t *s;
233 83fa1010 ths
        int ser_regs;
234 f062058f edgar_igl
235 f062058f edgar_igl
        s = qemu_mallocz(sizeof *s);
236 f062058f edgar_igl
        if (!s)
237 f062058f edgar_igl
                return;
238 f062058f edgar_igl
239 f062058f edgar_igl
        s->env = env;
240 f062058f edgar_igl
        s->irq = irq;
241 f062058f edgar_igl
        s->chr = chr;
242 f062058f edgar_igl
243 f062058f edgar_igl
        /* transmitter begins ready and idle.  */
244 f062058f edgar_igl
        s->rs_stat_din |= (1 << STAT_TR_RDY);
245 f062058f edgar_igl
        s->rs_stat_din |= (1 << STAT_TR_IDLE);
246 f062058f edgar_igl
247 f062058f edgar_igl
        qemu_chr_add_handlers(chr, serial_can_receive, serial_receive,
248 f062058f edgar_igl
                              serial_event, s);
249 f062058f edgar_igl
250 f062058f edgar_igl
        ser_regs = cpu_register_io_memory(0, ser_read, ser_write, s);
251 ca87d03b edgar_igl
        cpu_register_physical_memory (base, 0x3c, ser_regs);
252 83fa1010 ths
}