Statistics
| Branch: | Revision:

root / hw / syborg_serial.c @ d55380bb

History | View | Annotate | Download (9.6 kB)

1 4af39611 Paul Brook
/*
2 4af39611 Paul Brook
 * Syborg serial port
3 4af39611 Paul Brook
 *
4 4af39611 Paul Brook
 * Copyright (c) 2008 CodeSourcery
5 4af39611 Paul Brook
 *
6 4af39611 Paul Brook
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 4af39611 Paul Brook
 * of this software and associated documentation files (the "Software"), to deal
8 4af39611 Paul Brook
 * in the Software without restriction, including without limitation the rights
9 4af39611 Paul Brook
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 4af39611 Paul Brook
 * copies of the Software, and to permit persons to whom the Software is
11 4af39611 Paul Brook
 * furnished to do so, subject to the following conditions:
12 4af39611 Paul Brook
 *
13 4af39611 Paul Brook
 * The above copyright notice and this permission notice shall be included in
14 4af39611 Paul Brook
 * all copies or substantial portions of the Software.
15 4af39611 Paul Brook
 *
16 4af39611 Paul Brook
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 4af39611 Paul Brook
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 4af39611 Paul Brook
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 4af39611 Paul Brook
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 4af39611 Paul Brook
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 4af39611 Paul Brook
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 4af39611 Paul Brook
 * THE SOFTWARE.
23 4af39611 Paul Brook
 */
24 4af39611 Paul Brook
25 4af39611 Paul Brook
#include "sysbus.h"
26 4af39611 Paul Brook
#include "qemu-char.h"
27 4af39611 Paul Brook
#include "syborg.h"
28 4af39611 Paul Brook
29 4af39611 Paul Brook
//#define DEBUG_SYBORG_SERIAL
30 4af39611 Paul Brook
31 4af39611 Paul Brook
#ifdef DEBUG_SYBORG_SERIAL
32 4af39611 Paul Brook
#define DPRINTF(fmt, ...) \
33 4af39611 Paul Brook
do { printf("syborg_serial: " fmt , ##args); } while (0)
34 4af39611 Paul Brook
#define BADF(fmt, ...) \
35 4af39611 Paul Brook
do { fprintf(stderr, "syborg_serial: error: " fmt , ## __VA_ARGS__); \
36 4af39611 Paul Brook
    exit(1);} while (0)
37 4af39611 Paul Brook
#else
38 4af39611 Paul Brook
#define DPRINTF(fmt, ...) do {} while(0)
39 4af39611 Paul Brook
#define BADF(fmt, ...) \
40 4af39611 Paul Brook
do { fprintf(stderr, "syborg_serial: error: " fmt , ## __VA_ARGS__);} while (0)
41 4af39611 Paul Brook
#endif
42 4af39611 Paul Brook
43 4af39611 Paul Brook
enum {
44 4af39611 Paul Brook
    SERIAL_ID           = 0,
45 4af39611 Paul Brook
    SERIAL_DATA         = 1,
46 4af39611 Paul Brook
    SERIAL_FIFO_COUNT   = 2,
47 4af39611 Paul Brook
    SERIAL_INT_ENABLE   = 3,
48 4af39611 Paul Brook
    SERIAL_DMA_TX_ADDR  = 4,
49 4af39611 Paul Brook
    SERIAL_DMA_TX_COUNT = 5, /* triggers dma */
50 4af39611 Paul Brook
    SERIAL_DMA_RX_ADDR  = 6,
51 4af39611 Paul Brook
    SERIAL_DMA_RX_COUNT = 7, /* triggers dma */
52 4af39611 Paul Brook
    SERIAL_FIFO_SIZE    = 8
53 4af39611 Paul Brook
};
54 4af39611 Paul Brook
55 4af39611 Paul Brook
#define SERIAL_INT_FIFO   (1u << 0)
56 4af39611 Paul Brook
#define SERIAL_INT_DMA_TX (1u << 1)
57 4af39611 Paul Brook
#define SERIAL_INT_DMA_RX (1u << 2)
58 4af39611 Paul Brook
59 4af39611 Paul Brook
typedef struct {
60 4af39611 Paul Brook
    SysBusDevice busdev;
61 4af39611 Paul Brook
    uint32_t int_enable;
62 ee6847d1 Gerd Hoffmann
    uint32_t fifo_size;
63 4af39611 Paul Brook
    uint32_t *read_fifo;
64 4af39611 Paul Brook
    int read_pos;
65 4af39611 Paul Brook
    int read_count;
66 4af39611 Paul Brook
    CharDriverState *chr;
67 4af39611 Paul Brook
    qemu_irq irq;
68 4af39611 Paul Brook
    uint32_t dma_tx_ptr;
69 4af39611 Paul Brook
    uint32_t dma_rx_ptr;
70 4af39611 Paul Brook
    uint32_t dma_rx_size;
71 4af39611 Paul Brook
} SyborgSerialState;
72 4af39611 Paul Brook
73 4af39611 Paul Brook
static void syborg_serial_update(SyborgSerialState *s)
74 4af39611 Paul Brook
{
75 4af39611 Paul Brook
    int level;
76 4af39611 Paul Brook
    level = 0;
77 4af39611 Paul Brook
    if ((s->int_enable & SERIAL_INT_FIFO) && s->read_count)
78 4af39611 Paul Brook
        level = 1;
79 4af39611 Paul Brook
    if (s->int_enable & SERIAL_INT_DMA_TX)
80 4af39611 Paul Brook
        level = 1;
81 4af39611 Paul Brook
    if ((s->int_enable & SERIAL_INT_DMA_RX) && s->dma_rx_size == 0)
82 4af39611 Paul Brook
        level = 1;
83 4af39611 Paul Brook
84 4af39611 Paul Brook
    qemu_set_irq(s->irq, level);
85 4af39611 Paul Brook
}
86 4af39611 Paul Brook
87 4af39611 Paul Brook
static uint32_t fifo_pop(SyborgSerialState *s)
88 4af39611 Paul Brook
{
89 4af39611 Paul Brook
    const uint32_t c = s->read_fifo[s->read_pos];
90 4af39611 Paul Brook
    s->read_count--;
91 4af39611 Paul Brook
    s->read_pos++;
92 4af39611 Paul Brook
    if (s->read_pos == s->fifo_size)
93 4af39611 Paul Brook
        s->read_pos = 0;
94 4af39611 Paul Brook
95 4af39611 Paul Brook
    DPRINTF("FIFO pop %x (%d)\n", c, s->read_count);
96 4af39611 Paul Brook
    return c;
97 4af39611 Paul Brook
}
98 4af39611 Paul Brook
99 4af39611 Paul Brook
static void fifo_push(SyborgSerialState *s, uint32_t new_value)
100 4af39611 Paul Brook
{
101 4af39611 Paul Brook
    int slot;
102 4af39611 Paul Brook
103 4af39611 Paul Brook
    DPRINTF("FIFO push %x (%d)\n", new_value, s->read_count);
104 4af39611 Paul Brook
    slot = s->read_pos + s->read_count;
105 4af39611 Paul Brook
    if (slot >= s->fifo_size)
106 4af39611 Paul Brook
          slot -= s->fifo_size;
107 4af39611 Paul Brook
    s->read_fifo[slot] = new_value;
108 4af39611 Paul Brook
    s->read_count++;
109 4af39611 Paul Brook
}
110 4af39611 Paul Brook
111 4af39611 Paul Brook
static void do_dma_tx(SyborgSerialState *s, uint32_t count)
112 4af39611 Paul Brook
{
113 4af39611 Paul Brook
    unsigned char ch;
114 4af39611 Paul Brook
115 4af39611 Paul Brook
    if (count == 0)
116 4af39611 Paul Brook
        return;
117 4af39611 Paul Brook
118 4af39611 Paul Brook
    if (s->chr != NULL) {
119 4af39611 Paul Brook
        /* optimize later. Now, 1 byte per iteration */
120 4af39611 Paul Brook
        while (count--) {
121 4af39611 Paul Brook
            cpu_physical_memory_read(s->dma_tx_ptr, &ch, 1);
122 4af39611 Paul Brook
            qemu_chr_write(s->chr, &ch, 1);
123 4af39611 Paul Brook
            s->dma_tx_ptr++;
124 4af39611 Paul Brook
        }
125 4af39611 Paul Brook
    } else {
126 4af39611 Paul Brook
        s->dma_tx_ptr += count;
127 4af39611 Paul Brook
    }
128 4af39611 Paul Brook
    /* QEMU char backends do not have a nonblocking mode, so we transmit all
129 4af39611 Paul Brook
       the data imediately and the interrupt status will be unchanged.  */
130 4af39611 Paul Brook
}
131 4af39611 Paul Brook
132 4af39611 Paul Brook
/* Initiate RX DMA, and transfer data from the FIFO.  */
133 4af39611 Paul Brook
static void dma_rx_start(SyborgSerialState *s, uint32_t len)
134 4af39611 Paul Brook
{
135 4af39611 Paul Brook
    uint32_t dest;
136 4af39611 Paul Brook
    unsigned char ch;
137 4af39611 Paul Brook
138 4af39611 Paul Brook
    dest = s->dma_rx_ptr;
139 4af39611 Paul Brook
    if (s->read_count < len) {
140 4af39611 Paul Brook
        s->dma_rx_size = len - s->read_count;
141 4af39611 Paul Brook
        len = s->read_count;
142 4af39611 Paul Brook
    } else {
143 4af39611 Paul Brook
        s->dma_rx_size = 0;
144 4af39611 Paul Brook
    }
145 4af39611 Paul Brook
146 4af39611 Paul Brook
    while (len--) {
147 4af39611 Paul Brook
        ch = fifo_pop(s);
148 4af39611 Paul Brook
        cpu_physical_memory_write(dest, &ch, 1);
149 4af39611 Paul Brook
        dest++;
150 4af39611 Paul Brook
    }
151 4af39611 Paul Brook
    s->dma_rx_ptr = dest;
152 4af39611 Paul Brook
    syborg_serial_update(s);
153 4af39611 Paul Brook
}
154 4af39611 Paul Brook
155 c227f099 Anthony Liguori
static uint32_t syborg_serial_read(void *opaque, target_phys_addr_t offset)
156 4af39611 Paul Brook
{
157 4af39611 Paul Brook
    SyborgSerialState *s = (SyborgSerialState *)opaque;
158 4af39611 Paul Brook
    uint32_t c;
159 4af39611 Paul Brook
160 4af39611 Paul Brook
    offset &= 0xfff;
161 4af39611 Paul Brook
    DPRINTF("read 0x%x\n", (int)offset);
162 4af39611 Paul Brook
    switch(offset >> 2) {
163 4af39611 Paul Brook
    case SERIAL_ID:
164 4af39611 Paul Brook
        return SYBORG_ID_SERIAL;
165 4af39611 Paul Brook
    case SERIAL_DATA:
166 4af39611 Paul Brook
        if (s->read_count > 0)
167 4af39611 Paul Brook
            c = fifo_pop(s);
168 4af39611 Paul Brook
        else
169 4af39611 Paul Brook
            c = -1;
170 4af39611 Paul Brook
        syborg_serial_update(s);
171 4af39611 Paul Brook
        return c;
172 4af39611 Paul Brook
    case SERIAL_FIFO_COUNT:
173 4af39611 Paul Brook
        return s->read_count;
174 4af39611 Paul Brook
    case SERIAL_INT_ENABLE:
175 4af39611 Paul Brook
        return s->int_enable;
176 4af39611 Paul Brook
    case SERIAL_DMA_TX_ADDR:
177 4af39611 Paul Brook
        return s->dma_tx_ptr;
178 4af39611 Paul Brook
    case SERIAL_DMA_TX_COUNT:
179 4af39611 Paul Brook
        return 0;
180 4af39611 Paul Brook
    case SERIAL_DMA_RX_ADDR:
181 4af39611 Paul Brook
        return s->dma_rx_ptr;
182 4af39611 Paul Brook
    case SERIAL_DMA_RX_COUNT:
183 4af39611 Paul Brook
        return s->dma_rx_size;
184 4af39611 Paul Brook
    case SERIAL_FIFO_SIZE:
185 4af39611 Paul Brook
        return s->fifo_size;
186 4af39611 Paul Brook
187 4af39611 Paul Brook
    default:
188 4af39611 Paul Brook
        cpu_abort(cpu_single_env, "syborg_serial_read: Bad offset %x\n",
189 4af39611 Paul Brook
                  (int)offset);
190 4af39611 Paul Brook
        return 0;
191 4af39611 Paul Brook
    }
192 4af39611 Paul Brook
}
193 4af39611 Paul Brook
194 c227f099 Anthony Liguori
static void syborg_serial_write(void *opaque, target_phys_addr_t offset,
195 4af39611 Paul Brook
                                uint32_t value)
196 4af39611 Paul Brook
{
197 4af39611 Paul Brook
    SyborgSerialState *s = (SyborgSerialState *)opaque;
198 4af39611 Paul Brook
    unsigned char ch;
199 4af39611 Paul Brook
200 4af39611 Paul Brook
    offset &= 0xfff;
201 4af39611 Paul Brook
    DPRINTF("Write 0x%x=0x%x\n", (int)offset, value);
202 4af39611 Paul Brook
    switch (offset >> 2) {
203 4af39611 Paul Brook
    case SERIAL_DATA:
204 4af39611 Paul Brook
        ch = value;
205 4af39611 Paul Brook
        if (s->chr)
206 4af39611 Paul Brook
            qemu_chr_write(s->chr, &ch, 1);
207 4af39611 Paul Brook
        break;
208 4af39611 Paul Brook
    case SERIAL_INT_ENABLE:
209 4af39611 Paul Brook
        s->int_enable = value;
210 4af39611 Paul Brook
        syborg_serial_update(s);
211 4af39611 Paul Brook
        break;
212 4af39611 Paul Brook
    case SERIAL_DMA_TX_ADDR:
213 4af39611 Paul Brook
        s->dma_tx_ptr = value;
214 4af39611 Paul Brook
        break;
215 4af39611 Paul Brook
    case SERIAL_DMA_TX_COUNT:
216 4af39611 Paul Brook
        do_dma_tx(s, value);
217 4af39611 Paul Brook
        break;
218 4af39611 Paul Brook
    case SERIAL_DMA_RX_ADDR:
219 4af39611 Paul Brook
        /* For safety, writes to this register cancel any pending DMA.  */
220 4af39611 Paul Brook
        s->dma_rx_size = 0;
221 4af39611 Paul Brook
        s->dma_rx_ptr = value;
222 4af39611 Paul Brook
        break;
223 4af39611 Paul Brook
    case SERIAL_DMA_RX_COUNT:
224 4af39611 Paul Brook
        dma_rx_start(s, value);
225 4af39611 Paul Brook
        break;
226 4af39611 Paul Brook
    default:
227 4af39611 Paul Brook
        cpu_abort(cpu_single_env, "syborg_serial_write: Bad offset %x\n",
228 4af39611 Paul Brook
                  (int)offset);
229 4af39611 Paul Brook
        break;
230 4af39611 Paul Brook
    }
231 4af39611 Paul Brook
}
232 4af39611 Paul Brook
233 4af39611 Paul Brook
static int syborg_serial_can_receive(void *opaque)
234 4af39611 Paul Brook
{
235 4af39611 Paul Brook
    SyborgSerialState *s = (SyborgSerialState *)opaque;
236 4af39611 Paul Brook
237 4af39611 Paul Brook
    if (s->dma_rx_size)
238 4af39611 Paul Brook
        return s->dma_rx_size;
239 4af39611 Paul Brook
    return s->fifo_size - s->read_count;
240 4af39611 Paul Brook
}
241 4af39611 Paul Brook
242 4af39611 Paul Brook
static void syborg_serial_receive(void *opaque, const uint8_t *buf, int size)
243 4af39611 Paul Brook
{
244 4af39611 Paul Brook
    SyborgSerialState *s = (SyborgSerialState *)opaque;
245 4af39611 Paul Brook
246 4af39611 Paul Brook
    if (s->dma_rx_size) {
247 4af39611 Paul Brook
        /* Place it in the DMA buffer.  */
248 4af39611 Paul Brook
        cpu_physical_memory_write(s->dma_rx_ptr, buf, size);
249 4af39611 Paul Brook
        s->dma_rx_size -= size;
250 4af39611 Paul Brook
        s->dma_rx_ptr += size;
251 4af39611 Paul Brook
    } else {
252 4af39611 Paul Brook
        while (size--)
253 4af39611 Paul Brook
            fifo_push(s, *buf);
254 4af39611 Paul Brook
    }
255 4af39611 Paul Brook
256 4af39611 Paul Brook
    syborg_serial_update(s);
257 4af39611 Paul Brook
}
258 4af39611 Paul Brook
259 4af39611 Paul Brook
static void syborg_serial_event(void *opaque, int event)
260 4af39611 Paul Brook
{
261 4af39611 Paul Brook
    /* TODO: Report BREAK events?  */
262 4af39611 Paul Brook
}
263 4af39611 Paul Brook
264 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const syborg_serial_readfn[] = {
265 4af39611 Paul Brook
     syborg_serial_read,
266 4af39611 Paul Brook
     syborg_serial_read,
267 4af39611 Paul Brook
     syborg_serial_read
268 4af39611 Paul Brook
};
269 4af39611 Paul Brook
270 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const syborg_serial_writefn[] = {
271 4af39611 Paul Brook
     syborg_serial_write,
272 4af39611 Paul Brook
     syborg_serial_write,
273 4af39611 Paul Brook
     syborg_serial_write
274 4af39611 Paul Brook
};
275 4af39611 Paul Brook
276 4af39611 Paul Brook
static void syborg_serial_save(QEMUFile *f, void *opaque)
277 4af39611 Paul Brook
{
278 4af39611 Paul Brook
    SyborgSerialState *s = opaque;
279 4af39611 Paul Brook
    int i;
280 4af39611 Paul Brook
281 4af39611 Paul Brook
    qemu_put_be32(f, s->fifo_size);
282 4af39611 Paul Brook
    qemu_put_be32(f, s->int_enable);
283 4af39611 Paul Brook
    qemu_put_be32(f, s->read_pos);
284 4af39611 Paul Brook
    qemu_put_be32(f, s->read_count);
285 4af39611 Paul Brook
    qemu_put_be32(f, s->dma_tx_ptr);
286 4af39611 Paul Brook
    qemu_put_be32(f, s->dma_rx_ptr);
287 4af39611 Paul Brook
    qemu_put_be32(f, s->dma_rx_size);
288 4af39611 Paul Brook
    for (i = 0; i < s->fifo_size; i++) {
289 4af39611 Paul Brook
        qemu_put_be32(f, s->read_fifo[i]);
290 4af39611 Paul Brook
    }
291 4af39611 Paul Brook
}
292 4af39611 Paul Brook
293 4af39611 Paul Brook
static int syborg_serial_load(QEMUFile *f, void *opaque, int version_id)
294 4af39611 Paul Brook
{
295 4af39611 Paul Brook
    SyborgSerialState *s = opaque;
296 4af39611 Paul Brook
    int i;
297 4af39611 Paul Brook
298 4af39611 Paul Brook
    if (version_id != 1)
299 4af39611 Paul Brook
        return -EINVAL;
300 4af39611 Paul Brook
301 4af39611 Paul Brook
    i = qemu_get_be32(f);
302 4af39611 Paul Brook
    if (s->fifo_size != i)
303 4af39611 Paul Brook
        return -EINVAL;
304 4af39611 Paul Brook
305 4af39611 Paul Brook
    s->int_enable = qemu_get_be32(f);
306 4af39611 Paul Brook
    s->read_pos = qemu_get_be32(f);
307 4af39611 Paul Brook
    s->read_count = qemu_get_be32(f);
308 4af39611 Paul Brook
    s->dma_tx_ptr = qemu_get_be32(f);
309 4af39611 Paul Brook
    s->dma_rx_ptr = qemu_get_be32(f);
310 4af39611 Paul Brook
    s->dma_rx_size = qemu_get_be32(f);
311 4af39611 Paul Brook
    for (i = 0; i < s->fifo_size; i++) {
312 4af39611 Paul Brook
        s->read_fifo[i] = qemu_get_be32(f);
313 4af39611 Paul Brook
    }
314 4af39611 Paul Brook
315 4af39611 Paul Brook
    return 0;
316 4af39611 Paul Brook
}
317 4af39611 Paul Brook
318 81a322d4 Gerd Hoffmann
static int syborg_serial_init(SysBusDevice *dev)
319 4af39611 Paul Brook
{
320 4af39611 Paul Brook
    SyborgSerialState *s = FROM_SYSBUS(SyborgSerialState, dev);
321 4af39611 Paul Brook
    int iomemtype;
322 4af39611 Paul Brook
323 4af39611 Paul Brook
    sysbus_init_irq(dev, &s->irq);
324 1eed09cb Avi Kivity
    iomemtype = cpu_register_io_memory(syborg_serial_readfn,
325 4af39611 Paul Brook
                                       syborg_serial_writefn, s);
326 4af39611 Paul Brook
    sysbus_init_mmio(dev, 0x1000, iomemtype);
327 4af39611 Paul Brook
    s->chr = qdev_init_chardev(&dev->qdev);
328 4af39611 Paul Brook
    if (s->chr) {
329 4af39611 Paul Brook
        qemu_chr_add_handlers(s->chr, syborg_serial_can_receive,
330 4af39611 Paul Brook
                              syborg_serial_receive, syborg_serial_event, s);
331 4af39611 Paul Brook
    }
332 4af39611 Paul Brook
    if (s->fifo_size <= 0) {
333 4af39611 Paul Brook
        fprintf(stderr, "syborg_serial: fifo too small\n");
334 4af39611 Paul Brook
        s->fifo_size = 16;
335 4af39611 Paul Brook
    }
336 4af39611 Paul Brook
    s->read_fifo = qemu_mallocz(s->fifo_size * sizeof(s->read_fifo[0]));
337 4af39611 Paul Brook
338 4af39611 Paul Brook
    register_savevm("syborg_serial", -1, 1,
339 4af39611 Paul Brook
                    syborg_serial_save, syborg_serial_load, s);
340 81a322d4 Gerd Hoffmann
    return 0;
341 4af39611 Paul Brook
}
342 4af39611 Paul Brook
343 ee6847d1 Gerd Hoffmann
static SysBusDeviceInfo syborg_serial_info = {
344 ee6847d1 Gerd Hoffmann
    .init = syborg_serial_init,
345 ee6847d1 Gerd Hoffmann
    .qdev.name  = "syborg,serial",
346 ee6847d1 Gerd Hoffmann
    .qdev.size  = sizeof(SyborgSerialState),
347 ee6847d1 Gerd Hoffmann
    .qdev.props = (Property[]) {
348 c4470b25 Gerd Hoffmann
        DEFINE_PROP_UINT32("fifo-size", SyborgSerialState, fifo_size, 16),
349 c4470b25 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
350 ee6847d1 Gerd Hoffmann
    }
351 ee6847d1 Gerd Hoffmann
};
352 ee6847d1 Gerd Hoffmann
353 4af39611 Paul Brook
static void syborg_serial_register_devices(void)
354 4af39611 Paul Brook
{
355 ee6847d1 Gerd Hoffmann
    sysbus_register_withprop(&syborg_serial_info);
356 4af39611 Paul Brook
}
357 4af39611 Paul Brook
358 4af39611 Paul Brook
device_init(syborg_serial_register_devices)