sh_serial.c 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * QEMU SCI/SCIF serial port emulation
 *
 * Copyright (c) 2007 Magnus Damm
 *
 * Based on serial.c - QEMU 16450 UART emulation
 * Copyright (c) 2003-2004 Fabrice Bellard
 *
 * 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.
 */
P
pbrook 已提交
27 28 29
#include "hw.h"
#include "sh.h"
#include "qemu-char.h"
30 31 32 33 34 35 36 37 38

//#define DEBUG_SERIAL

#define SH_SERIAL_FLAG_TEND (1 << 0)
#define SH_SERIAL_FLAG_TDE  (1 << 1)
#define SH_SERIAL_FLAG_RDF  (1 << 2)
#define SH_SERIAL_FLAG_BRK  (1 << 3)
#define SH_SERIAL_FLAG_DR   (1 << 4)

A
aurel32 已提交
39 40
#define SH_RX_FIFO_LENGTH (16)

41 42 43 44 45 46 47 48 49
typedef struct {
    uint8_t smr;
    uint8_t brr;
    uint8_t scr;
    uint8_t dr; /* ftdr / tdr */
    uint8_t sr; /* fsr / ssr */
    uint16_t fcr;
    uint8_t sptr;

A
aurel32 已提交
50
    uint8_t rx_fifo[SH_RX_FIFO_LENGTH]; /* frdr / rdr */
51
    uint8_t rx_cnt;
A
aurel32 已提交
52 53
    uint8_t rx_tail;
    uint8_t rx_head;
54 55 56 57

    int freq;
    int feat;
    int flags;
A
aurel32 已提交
58
    int rtrg;
59 60

    CharDriverState *chr;
A
aurel32 已提交
61

A
aurel32 已提交
62 63 64 65 66
    qemu_irq eri;
    qemu_irq rxi;
    qemu_irq txi;
    qemu_irq tei;
    qemu_irq bri;
67 68
} sh_serial_state;

A
aurel32 已提交
69 70 71 72 73 74 75 76
static void sh_serial_clear_fifo(sh_serial_state * s)
{
    memset(s->rx_fifo, 0, SH_RX_FIFO_LENGTH);
    s->rx_cnt = 0;
    s->rx_head = 0;
    s->rx_tail = 0;
}

77
static void sh_serial_write(void *opaque, uint32_t offs, uint32_t val)
78 79 80 81 82
{
    sh_serial_state *s = opaque;
    unsigned char ch;

#ifdef DEBUG_SERIAL
83 84
    printf("sh_serial: write offs=0x%02x val=0x%02x\n",
	   offs, val);
85 86 87 88 89 90 91 92 93
#endif
    switch(offs) {
    case 0x00: /* SMR */
        s->smr = val & ((s->feat & SH_SERIAL_FEAT_SCIF) ? 0x7b : 0xff);
        return;
    case 0x04: /* BRR */
        s->brr = val;
	return;
    case 0x08: /* SCR */
A
aurel32 已提交
94
        /* TODO : For SH7751, SCIF mask should be 0xfb. */
A
aurel32 已提交
95
        s->scr = val & ((s->feat & SH_SERIAL_FEAT_SCIF) ? 0xfa : 0xff);
96 97
        if (!(val & (1 << 5)))
            s->flags |= SH_SERIAL_FLAG_TEND;
A
aurel32 已提交
98
        if ((s->feat & SH_SERIAL_FEAT_SCIF) && s->txi) {
A
aurel32 已提交
99
	    qemu_set_irq(s->txi, val & (1 << 7));
A
aurel32 已提交
100
        }
A
aurel32 已提交
101 102
        if (!(val & (1 << 6))) {
	    qemu_set_irq(s->rxi, 0);
A
aurel32 已提交
103
        }
104 105 106 107
        return;
    case 0x0c: /* FTDR / TDR */
        if (s->chr) {
            ch = val;
108
            qemu_chr_fe_write(s->chr, &ch, 1);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	}
	s->dr = val;
	s->flags &= ~SH_SERIAL_FLAG_TDE;
        return;
#if 0
    case 0x14: /* FRDR / RDR */
        ret = 0;
        break;
#endif
    }
    if (s->feat & SH_SERIAL_FEAT_SCIF) {
        switch(offs) {
        case 0x10: /* FSR */
            if (!(val & (1 << 6)))
                s->flags &= ~SH_SERIAL_FLAG_TEND;
            if (!(val & (1 << 5)))
                s->flags &= ~SH_SERIAL_FLAG_TDE;
            if (!(val & (1 << 4)))
                s->flags &= ~SH_SERIAL_FLAG_BRK;
            if (!(val & (1 << 1)))
                s->flags &= ~SH_SERIAL_FLAG_RDF;
            if (!(val & (1 << 0)))
                s->flags &= ~SH_SERIAL_FLAG_DR;
A
aurel32 已提交
132 133

            if (!(val & (1 << 1)) || !(val & (1 << 0))) {
A
aurel32 已提交
134 135
                if (s->rxi) {
                    qemu_set_irq(s->rxi, 0);
A
aurel32 已提交
136 137
                }
            }
138 139 140
            return;
        case 0x18: /* FCR */
            s->fcr = val;
A
aurel32 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
            switch ((val >> 6) & 3) {
            case 0:
                s->rtrg = 1;
                break;
            case 1:
                s->rtrg = 4;
                break;
            case 2:
                s->rtrg = 8;
                break;
            case 3:
                s->rtrg = 14;
                break;
            }
            if (val & (1 << 1)) {
                sh_serial_clear_fifo(s);
                s->sr &= ~(1 << 1);
            }

160 161
            return;
        case 0x20: /* SPTR */
A
aurel32 已提交
162
            s->sptr = val & 0xf3;
163 164 165 166 167 168 169
            return;
        case 0x24: /* LSR */
            return;
        }
    }
    else {
        switch(offs) {
A
aurel32 已提交
170
#if 0
171 172 173 174 175 176
        case 0x0c:
            ret = s->dr;
            break;
        case 0x10:
            ret = 0;
            break;
A
aurel32 已提交
177
#endif
178
        case 0x1c:
A
aurel32 已提交
179 180
            s->sptr = val & 0x8f;
            return;
181 182 183 184
        }
    }

    fprintf(stderr, "sh_serial: unsupported write to 0x%02x\n", offs);
185
    abort();
186 187
}

188
static uint32_t sh_serial_read(void *opaque, uint32_t offs)
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
{
    sh_serial_state *s = opaque;
    uint32_t ret = ~0;

#if 0
    switch(offs) {
    case 0x00:
        ret = s->smr;
        break;
    case 0x04:
        ret = s->brr;
	break;
    case 0x08:
        ret = s->scr;
        break;
    case 0x14:
        ret = 0;
        break;
    }
#endif
    if (s->feat & SH_SERIAL_FEAT_SCIF) {
        switch(offs) {
A
aurel32 已提交
211 212 213 214 215 216
        case 0x00: /* SMR */
            ret = s->smr;
            break;
        case 0x08: /* SCR */
            ret = s->scr;
            break;
217 218 219 220 221 222 223 224 225 226 227 228 229
        case 0x10: /* FSR */
            ret = 0;
            if (s->flags & SH_SERIAL_FLAG_TEND)
                ret |= (1 << 6);
            if (s->flags & SH_SERIAL_FLAG_TDE)
                ret |= (1 << 5);
            if (s->flags & SH_SERIAL_FLAG_BRK)
                ret |= (1 << 4);
            if (s->flags & SH_SERIAL_FLAG_RDF)
                ret |= (1 << 1);
            if (s->flags & SH_SERIAL_FLAG_DR)
                ret |= (1 << 0);

A
aurel32 已提交
230
            if (s->scr & (1 << 5))
231 232
                s->flags |= SH_SERIAL_FLAG_TDE | SH_SERIAL_FLAG_TEND;

A
aurel32 已提交
233 234 235 236 237 238 239 240 241 242
            break;
        case 0x14:
            if (s->rx_cnt > 0) {
                ret = s->rx_fifo[s->rx_tail++];
                s->rx_cnt--;
                if (s->rx_tail == SH_RX_FIFO_LENGTH)
                    s->rx_tail = 0;
                if (s->rx_cnt < s->rtrg)
                    s->flags &= ~SH_SERIAL_FLAG_RDF;
            }
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
            break;
#if 0
        case 0x18:
            ret = s->fcr;
            break;
#endif
        case 0x1c:
            ret = s->rx_cnt;
            break;
        case 0x20:
            ret = s->sptr;
            break;
        case 0x24:
            ret = 0;
            break;
        }
    }
    else {
        switch(offs) {
A
aurel32 已提交
262
#if 0
263 264 265 266 267 268
        case 0x0c:
            ret = s->dr;
            break;
        case 0x10:
            ret = 0;
            break;
A
aurel32 已提交
269 270 271
        case 0x14:
            ret = s->rx_fifo[0];
            break;
A
aurel32 已提交
272
#endif
273 274 275 276 277 278
        case 0x1c:
            ret = s->sptr;
            break;
        }
    }
#ifdef DEBUG_SERIAL
279 280
    printf("sh_serial: read offs=0x%02x val=0x%x\n",
	   offs, ret);
281 282 283 284
#endif

    if (ret & ~((1 << 16) - 1)) {
        fprintf(stderr, "sh_serial: unsupported read from 0x%02x\n", offs);
285
        abort();
286 287 288 289 290 291 292
    }

    return ret;
}

static int sh_serial_can_receive(sh_serial_state *s)
{
A
aurel32 已提交
293
    return s->scr & (1 << 4);
294 295 296 297
}

static void sh_serial_receive_break(sh_serial_state *s)
{
A
aurel32 已提交
298 299
    if (s->feat & SH_SERIAL_FEAT_SCIF)
        s->sr |= (1 << 4);
300 301 302 303 304 305 306 307 308 309 310
}

static int sh_serial_can_receive1(void *opaque)
{
    sh_serial_state *s = opaque;
    return sh_serial_can_receive(s);
}

static void sh_serial_receive1(void *opaque, const uint8_t *buf, int size)
{
    sh_serial_state *s = opaque;
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

    if (s->feat & SH_SERIAL_FEAT_SCIF) {
        int i;
        for (i = 0; i < size; i++) {
            if (s->rx_cnt < SH_RX_FIFO_LENGTH) {
                s->rx_fifo[s->rx_head++] = buf[i];
                if (s->rx_head == SH_RX_FIFO_LENGTH) {
                    s->rx_head = 0;
                }
                s->rx_cnt++;
                if (s->rx_cnt >= s->rtrg) {
                    s->flags |= SH_SERIAL_FLAG_RDF;
                    if (s->scr & (1 << 6) && s->rxi) {
                        qemu_set_irq(s->rxi, 1);
                    }
                }
            }
        }
    } else {
        s->rx_fifo[0] = buf[0];
    }
332 333 334 335 336 337 338 339 340
}

static void sh_serial_event(void *opaque, int event)
{
    sh_serial_state *s = opaque;
    if (event == CHR_EVENT_BREAK)
        sh_serial_receive_break(s);
}

341
static CPUReadMemoryFunc * const sh_serial_readfn[] = {
342 343 344 345 346
    &sh_serial_read,
    &sh_serial_read,
    &sh_serial_read,
};

347
static CPUWriteMemoryFunc * const sh_serial_writefn[] = {
348 349 350 351 352
    &sh_serial_write,
    &sh_serial_write,
    &sh_serial_write,
};

A
Anthony Liguori 已提交
353
void sh_serial_init (target_phys_addr_t base, int feat,
A
aurel32 已提交
354
		     uint32_t freq, CharDriverState *chr,
A
aurel32 已提交
355 356 357 358 359
		     qemu_irq eri_source,
		     qemu_irq rxi_source,
		     qemu_irq txi_source,
		     qemu_irq tei_source,
		     qemu_irq bri_source)
360 361 362 363
{
    sh_serial_state *s;
    int s_io_memory;

364
    s = g_malloc0(sizeof(sh_serial_state));
365 366 367

    s->feat = feat;
    s->flags = SH_SERIAL_FLAG_TEND | SH_SERIAL_FLAG_TDE;
A
aurel32 已提交
368
    s->rtrg = 1;
369 370 371

    s->smr = 0;
    s->brr = 0xff;
372
    s->scr = 1 << 5; /* pretend that TX is enabled so early printk works */
373 374 375 376 377 378 379 380 381
    s->sptr = 0;

    if (feat & SH_SERIAL_FEAT_SCIF) {
        s->fcr = 0;
    }
    else {
        s->dr = 0xff;
    }

A
aurel32 已提交
382
    sh_serial_clear_fifo(s);
383

384
    s_io_memory = cpu_register_io_memory(sh_serial_readfn,
385 386
					 sh_serial_writefn, s,
                                         DEVICE_NATIVE_ENDIAN);
387 388
    cpu_register_physical_memory(P4ADDR(base), 0x28, s_io_memory);
    cpu_register_physical_memory(A7ADDR(base), 0x28, s_io_memory);
389 390 391 392 393 394

    s->chr = chr;

    if (chr)
        qemu_chr_add_handlers(chr, sh_serial_can_receive1, sh_serial_receive1,
			      sh_serial_event, s);
A
aurel32 已提交
395 396 397 398 399 400

    s->eri = eri_source;
    s->rxi = rxi_source;
    s->txi = txi_source;
    s->tei = tei_source;
    s->bri = bri_source;
401
}