OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_63/] [or1ksim/] [peripheral/] [16450.c] - Blame information for rev 997

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 31 lampret
/* 16450.c -- Simulation of 8250/16450 serial UART
2
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
/* This is functional simulation of 8250/16450 UARTs. Since we RX/TX data
21
   via file streams, we can't simulate modem control lines coming from the
22
   DCE and similar details of communication with the DCE.
23
 
24
   This simulated UART device is intended for basic UART device driver
25
   verification. From device driver perspective this device looks like a
26
   regular UART but never reports and modem control lines changes (the
27
   only DCE responses are incoming characters from the file stream).
28
*/
29
 
30
#include <stdlib.h>
31
#include <stdio.h>
32
#include <string.h>
33
 
34 235 erez
#include "abstract.h"
35 31 lampret
#include "16450.h"
36
#include "sim-config.h"
37 102 lampret
#include "pic.h"
38 336 markom
#include "vapi.h"
39 805 markom
#include "sched.h"
40 31 lampret
 
41 409 markom
#define MIN(a,b) ((a) < (b) ? (a) : (b))
42
 
43 424 markom
static struct dev_16450 uarts[MAX_UARTS];
44 221 markom
static int thre_int;
45 31 lampret
 
46
/* Number of clock cycles (one clock cycle is one call to the uart_clock())
47
   before a single character is transmitted or received. */
48 355 markom
static unsigned long char_clks(int dll, int dlh, int lcr)
49 31 lampret
{
50 409 markom
  float bauds_per_char = 1.;
51 806 markom
  unsigned long char_clks = ((dlh << 8) + dll);
52 355 markom
 
53
  if (lcr & UART_LCR_PARITY)
54
    bauds_per_char = bauds_per_char + 1.;
55 31 lampret
 
56 355 markom
  /* stop bits 1 or two */
57
  if (lcr & UART_LCR_STOP)
58
    bauds_per_char = bauds_per_char + 2.;
59
  else
60
    if ((lcr & 0x3) != 0)
61
      bauds_per_char = bauds_per_char + 1.;
62
    else
63
      bauds_per_char = bauds_per_char + 1.5;
64
 
65
  bauds_per_char = bauds_per_char + (5. + (lcr & 0x3));
66
 
67
  return char_clks * bauds_per_char;
68 31 lampret
}
69
 
70
/* Set a specific UART register with value. */
71 238 erez
void uart_write_byte(unsigned long addr, unsigned long value)
72 31 lampret
{
73 355 markom
  int chipsel;
74
 
75
  debug(4, "uart_write_byte(%x,%02x)\n", addr, (unsigned)value);
76 341 markom
 
77 424 markom
  for(chipsel = 0; chipsel < MAX_UARTS; chipsel++)
78 355 markom
    if ((addr & ~(UART_ADDR_SPACE-1)) == config.uarts[chipsel].baseaddr)
79
      break;
80 424 markom
  if (chipsel >= MAX_UARTS) return;
81 31 lampret
 
82 355 markom
  if (uarts[chipsel].regs.lcr & UART_LCR_DLAB) {
83
    switch (addr % UART_ADDR_SPACE) {
84
      case UART_DLL:
85
        uarts[chipsel].regs.dll = value;
86
        uarts[chipsel].char_clks = char_clks(uarts[chipsel].regs.dll, uarts[chipsel].regs.dlh, uarts[chipsel].regs.lcr);
87
        return;
88
      case UART_DLH:
89
        uarts[chipsel].regs.dlh = value;
90
        return;
91
    }
92
  }
93
 
94
  switch (addr % UART_ADDR_SPACE) {
95
    case UART_TXBUF:
96
      if (uarts[chipsel].istat.txbuf_full < uarts[chipsel].fifo_len) {
97
        uarts[chipsel].istat.txbuf_full++;
98
        uarts[chipsel].regs.txbuf[uarts[chipsel].istat.txbuf_head] = value;
99
        uarts[chipsel].istat.txbuf_head = (uarts[chipsel].istat.txbuf_head + 1) % uarts[chipsel].fifo_len;
100
      } else
101
        uarts[chipsel].regs.txbuf[uarts[chipsel].istat.txbuf_head] = value;
102 341 markom
 
103 411 markom
      uarts[chipsel].regs.lsr &= ~(UART_LSR_TXSERE | UART_LSR_TXBUFE);
104 355 markom
      uarts[chipsel].istat.thre_int = 0;
105
      break;
106 409 markom
    case UART_FCR:
107
      uarts[chipsel].regs.fcr = value & UART_VALID_FCR;
108
      if (uarts[chipsel].fifo_len == 1 && (value & UART_FCR_FIE)
109
       || uarts[chipsel].fifo_len != 1 && !(value & UART_FCR_FIE))
110
        value |= UART_FCR_RRXFI | UART_FCR_RTXFI;
111
      uarts[chipsel].fifo_len = (value & UART_FCR_FIE) ? 16 : 1;
112
      if (value & UART_FCR_RTXFI) {
113
        uarts[chipsel].istat.txbuf_head = uarts[chipsel].istat.txbuf_tail = 0;
114
        uarts[chipsel].istat.txbuf_full = 0;
115 411 markom
        uarts[chipsel].regs.lsr &= ~UART_LSR_TXBUFE;
116 409 markom
      }
117
      if (value & UART_FCR_RRXFI) {
118
        uarts[chipsel].istat.rxbuf_head = uarts[chipsel].istat.rxbuf_tail = 0;
119
        uarts[chipsel].istat.rxbuf_full = 0;
120 411 markom
        uarts[chipsel].regs.lsr &= ~UART_LSR_RDRDY;
121 409 markom
      }
122
      break;
123 355 markom
    case UART_IER:
124
      uarts[chipsel].regs.ier = value & UART_VALID_IER;
125 423 markom
#if 0
126 492 markom
      if (uarts[chipsel].regs.ier & UART_IER_THRI)
127
        uarts[chipsel].istat.thre_int = 1;
128 423 markom
#endif
129 355 markom
      break;
130
    case UART_LCR:
131
      uarts[chipsel].regs.lcr = value & UART_VALID_LCR;
132 409 markom
      uarts[chipsel].char_clks = char_clks(uarts[chipsel].regs.dll, uarts[chipsel].regs.dlh, uarts[chipsel].regs.lcr);
133 355 markom
      break;
134
    case UART_MCR:
135
      uarts[chipsel].regs.mcr = value & UART_VALID_MCR;
136
      break;
137
    case UART_SCR:
138
      uarts[chipsel].regs.scr = value;
139
      break;
140
    default:
141
      debug(1, "write out of range (addr %x)\n", addr);
142
  }
143 31 lampret
}
144
 
145
/* Read a specific UART register. */
146 238 erez
unsigned long uart_read_byte(unsigned long addr)
147 31 lampret
{
148 355 markom
  unsigned char value = 0;
149
  int chipsel;
150
 
151
  debug(4, "uart_read_byte(%x)", addr);
152
 
153 424 markom
  for(chipsel = 0; chipsel < MAX_UARTS; chipsel++)
154 355 markom
    if ((addr & ~(UART_ADDR_SPACE-1)) == config.uarts[chipsel].baseaddr)
155
      break;
156 31 lampret
 
157 424 markom
  if (chipsel >= MAX_UARTS)
158 355 markom
    return 0;
159 344 markom
 
160 355 markom
  if (uarts[chipsel].regs.lcr & UART_LCR_DLAB) {
161
    switch (addr % UART_ADDR_SPACE) {
162
      case UART_DLL:
163
        value = uarts[chipsel].regs.dll;
164
        debug(4, "= %x\n", value);
165
        return value;
166
      case UART_DLH:
167
        value = uarts[chipsel].regs.dlh;
168
        debug(4, "= %x\n", value);
169
        return value;
170
    }
171
  }
172
 
173
  switch (addr % UART_ADDR_SPACE) {
174
    case UART_RXBUF:
175 409 markom
      { /* Print out FIFO for debugging */
176
        int i;
177
        debug(4, "(%i/%i,%i,%i:", uarts[chipsel].istat.rxbuf_full, uarts[chipsel].fifo_len,
178
                  uarts[chipsel].istat.rxbuf_head, uarts[chipsel].istat.rxbuf_tail);
179
        for (i = 0; i < uarts[chipsel].istat.rxbuf_full; i++)
180
          debug(4, "%02x ", uarts[chipsel].regs.rxbuf[(uarts[chipsel].istat.rxbuf_tail + i) % uarts[chipsel].fifo_len]);
181
        debug(4, ")");
182
      }
183 355 markom
      if (uarts[chipsel].istat.rxbuf_full) {
184
        value = uarts[chipsel].regs.rxbuf[uarts[chipsel].istat.rxbuf_tail];
185
        uarts[chipsel].istat.rxbuf_tail = (uarts[chipsel].istat.rxbuf_tail + 1) % uarts[chipsel].fifo_len;
186
        uarts[chipsel].istat.rxbuf_full--;
187
      }
188
 
189
      if (uarts[chipsel].istat.rxbuf_full)
190
        uarts[chipsel].regs.lsr |= UART_LSR_RDRDY;
191
      else
192
        uarts[chipsel].regs.lsr &= ~UART_LSR_RDRDY;
193 409 markom
 
194
      uarts[chipsel].istat.timeout_count = 0;
195 355 markom
      break;
196
    case UART_IER:
197
      value = uarts[chipsel].regs.ier & UART_VALID_IER;
198
      break;
199
    case UART_IIR:
200
      value = (uarts[chipsel].regs.iir & UART_VALID_IIR) | 0xc0;
201 492 markom
      if (uarts[chipsel].regs.ier & UART_IER_THRI)
202 490 markom
        uarts[chipsel].istat.thre_int = 0;
203 355 markom
      break;
204
    case UART_LCR:
205
      value = uarts[chipsel].regs.lcr & UART_VALID_LCR;
206
      break;
207
    case UART_MCR:
208
      value = 0;
209
      break;
210
    case UART_LSR:
211
      value = uarts[chipsel].regs.lsr & UART_VALID_LSR;
212
      uarts[chipsel].regs.lsr &=
213 409 markom
        ~(UART_LSR_OVRRUN | UART_LSR_BREAK | UART_LSR_PARITY
214
         | UART_LSR_FRAME | UART_LSR_RXERR);
215 355 markom
      break;
216
    case UART_MSR:
217
      value = uarts[chipsel].regs.msr & UART_VALID_MSR;
218
      uarts[chipsel].regs.msr = 0;
219
      break;
220
    case UART_SCR:
221
      value = uarts[chipsel].regs.scr;
222
      break;
223
    default:
224
      debug(1, "read out of range (addr %x)\n", addr);
225
  }
226
  debug(4, " = %x\n", value);
227
  return value;
228 31 lampret
}
229
 
230 336 markom
/* Function that handles incoming VAPI data.  */
231
void uart_vapi_read (unsigned long id, unsigned long data)
232
{
233 341 markom
  int uart;
234 344 markom
  debug(4, "UART: id %08x, data %08x\n", id, data);
235 341 markom
  uart = id & VAPI_DEVICE_ID;
236
  uarts[uart].vapi_buf[uarts[uart].vapi_buf_head_ptr] = data;
237 409 markom
  uarts[uart].vapi_buf_head_ptr = (uarts[uart].vapi_buf_head_ptr + 1) % UART_VAPI_BUF_LEN;
238 341 markom
  if (uarts[uart].vapi_buf_tail_ptr == uarts[uart].vapi_buf_head_ptr) {
239
    fprintf (stderr, "FATAL: uart VAPI buffer to small.\n");
240
    exit (1);
241
  }
242
}
243 336 markom
 
244 409 markom
static void send_char (int uart, int bits_send)
245
{
246 997 markom
  PRINTF ("'%c'\n", uarts[uart].iregs.txser);
247 409 markom
  debug(4, "TX \'%c\' via UART%d...\n", uarts[uart].iregs.txser, uart);
248
  if (uarts[uart].regs.mcr & UART_MCR_LOOP)
249
    uarts[uart].iregs.loopback = uarts[uart].iregs.txser;
250
  else {
251
    /* Send to either VAPI or to file */
252
    if (config.uarts[uart].vapi_id) {
253
      int par, pe, fe, nbits;
254
      int j, data;
255
      unsigned long packet = 0;
256
 
257
      nbits = MIN (bits_send, (uarts[uart].regs.lcr & UART_LCR_WLEN8) + 5);
258
      /* Encode a packet */
259
      packet = uarts[uart].iregs.txser & ((1 << nbits) - 1);
260
 
261
      /* Calculate parity */
262
      for (j = 0, par = 0; j < nbits; j++)
263
        par ^= (packet >> j) & 1;
264
 
265
      if (uarts[uart].regs.lcr & UART_LCR_PARITY) {
266
        if (uarts[uart].regs.lcr & UART_LCR_SPAR) {
267
          packet |= 1 << nbits;
268
        } else {
269
          if (uarts[uart].regs.lcr & UART_LCR_EPAR)
270
            packet |= par << nbits;
271
          else
272
            packet |= (par ^ 1) << nbits;
273
        }
274
        nbits++;
275
      }
276
      packet |= 1 << (nbits++);
277
      if (uarts[uart].regs.lcr & UART_LCR_STOP)
278
        packet |= 1 << (nbits++);
279
 
280
      /* Decode a packet */
281
      nbits = (uarts[uart].vapi.lcr & UART_LCR_WLEN8) + 5;
282
      data = packet & ((1 << nbits) - 1);
283
 
284
      /* Calculate parity, including parity bit */
285
      for (j = 0, par = 0; j < nbits + 1; j++)
286
        par ^= (packet >> j) & 1;
287
 
288
      if (uarts[uart].vapi.lcr & UART_LCR_PARITY) {
289
        if (uarts[uart].vapi.lcr & UART_LCR_SPAR) {
290
          pe = !((packet >> nbits) & 1);
291
        } else {
292
          if (uarts[uart].vapi.lcr & UART_LCR_EPAR)
293
            pe = par != 0;
294
          else
295
            pe = par != 1;
296
        }
297
        nbits++;
298
      } else
299
        pe = 0;
300
 
301
      fe = ((packet >> (nbits++)) & 1) ^ 1;
302
      if (uarts[uart].vapi.lcr & UART_LCR_STOP)
303
        fe |= ((packet >> (nbits++)) & 1) ^ 1;
304
 
305
      debug (4, "lcr vapi %02x, uart %02x\n", uarts[uart].vapi.lcr, uarts[uart].regs.lcr);
306
      data |= (uarts[uart].vapi.lcr << 8) | (pe << 16) | (fe << 17) | (uarts[uart].vapi.lcr << 8);
307 997 markom
      PRINTF ("vapi_send (%08x, %08x)\n", config.uarts[uart].vapi_id, data);
308 409 markom
      debug (4, "vapi_send (%08x, %08x)\n", config.uarts[uart].vapi_id, data);
309
      vapi_send (config.uarts[uart].vapi_id, data);
310
    } else {
311
      fputc((int)(uarts[uart].iregs.txser & 0xFF), uarts[uart].txfs);
312
      fflush(uarts[uart].txfs);
313
    }
314
  }
315
  uarts[uart].istat.txser_full = 0;
316
  uarts[uart].istat.txser_clks = 0;
317
}
318
 
319 411 markom
/* Adds a character to the FIFO */
320
 
321
void uart_add_char (int uart, int ch)
322
{
323
  if (uarts[uart].istat.rxbuf_full + 1 > uarts[uart].fifo_len)
324
    uarts[uart].regs.lsr |= UART_LSR_OVRRUN | UART_LSR_RXERR;
325
  else {
326
    debug(4, "add %02x\n", ch);
327
    uarts[uart].regs.rxbuf[uarts[uart].istat.rxbuf_head] = ch;
328
    uarts[uart].istat.rxbuf_head = (uarts[uart].istat.rxbuf_head + 1) % uarts[uart].fifo_len;
329
    uarts[uart].istat.rxbuf_full++;
330
  }
331
  uarts[uart].regs.lsr |= UART_LSR_RDRDY;
332
  uarts[uart].istat.timeout_count = 0;
333
}
334
 
335 806 markom
/* Simulation hook. Must be called every clock cycle to simulate all UART
336
   devices. It does internal functional UART simulation. */
337 821 markom
void uart_clock16 (int i)
338 806 markom
{
339
  int retval;
340 821 markom
 
341
  /* Schedule for later */
342 884 markom
  SCHED_ADD (uart_clock16, i, runtime.sim.cycles + UART_CLOCK_DIVIDER);
343 821 markom
 
344 806 markom
  /* If VAPI is not selected, UART communicates with two file streams;
345
     if VAPI is selected, we use VAPI streams.  */
346
  /* if txfs is corrupted, skip this uart. */
347
  if (!config.uarts[i].vapi_id && !uarts[i].txfs) return;
348
 
349
  if (uarts[i].vapi.next_break_cnt >= 0)
350
    if (--uarts[i].vapi.next_break_cnt < 0) {
351
      if (!(uarts[i].vapi.cur_break = uarts[i].vapi.next_break))
352
        uarts[i].istat.break_set = 0;
353
    }
354 821 markom
 
355 806 markom
  /***************** Transmit *****************/
356
  if (!uarts[i].istat.txser_full) {
357
//      uarts[i].regs.lsr |= UART_LSR_TXBUFE;
358
    if (uarts[i].istat.txbuf_full) {
359
      uarts[i].iregs.txser = uarts[i].regs.txbuf[uarts[i].istat.txbuf_tail];
360
      uarts[i].istat.txbuf_tail = (uarts[i].istat.txbuf_tail + 1) % uarts[i].fifo_len;
361
      uarts[i].istat.txser_full = 1;
362
      uarts[i].istat.txbuf_full--;
363
      uarts[i].regs.lsr &= ~UART_LSR_TXSERE;
364
      uarts[i].istat.thre_int = 1;
365
    } else {
366
      uarts[i].regs.lsr |= UART_LSR_TXSERE;
367
      uarts[i].regs.lsr |= UART_LSR_TXBUFE;
368
    }
369
  } else if (uarts[i].char_clks <= uarts[i].istat.txser_clks++) {
370
    send_char(i, (uarts[i].regs.lcr & UART_LCR_WLEN8) + 5); /* We've sent all bits */
371
  } else {
372
    /* We are still sending char here*/
373
 
374
    /* Check if we set the break bit */
375
    if (uarts[i].regs.lcr & UART_LCR_SBC) {
376
      if (!uarts[i].vapi.break_sent) {
377
#if 0
378
        /* Send broken frame */
379
        int nbits_sent = ((uarts[i].regs.lcr & UART_LCR_WLEN8) + 5) * (uarts[i].istat.txser_clks - 1) / uarts[i].char_clks;
380
        send_char(i, nbits_sent);
381
#endif
382
        /* Send one break signal */
383
        vapi_send (config.uarts[i].vapi_id, UART_LCR_SBC << 8);
384
        uarts[i].vapi.break_sent = 1;
385
      }
386
      /* mark as character was sent */
387
      uarts[i].istat.txser_full = 0;
388
      uarts[i].istat.txser_clks = 0;
389
    } else
390
      uarts[i].vapi.break_sent = 0;
391
  }
392
 
393
  /***************** Receive *****************/
394
 
395
  /* Is there a break? */
396
  if (uarts[i].vapi.cur_break) {
397
    uarts[i].vapi.cur_break_cnt++;
398
    if (uarts[i].vapi.cur_break_cnt > UART_BREAK_COUNT * uarts[i].vapi.char_clks) {
399
      if (!uarts[i].istat.break_set) {
400
        unsigned lsr;
401
        uarts[i].istat.break_set = 1;
402
        lsr = UART_LSR_BREAK | UART_LSR_RXERR | UART_LSR_RDRDY;
403 997 markom
        PRINTF ("[%x]\n", uarts[i].regs.lsr);
404 806 markom
        uarts[i].istat.rxser_full = 0;
405
        uarts[i].istat.rxser_clks = 0;
406
        uart_add_char (i, lsr << 8);
407
      } else
408
        uarts[i].vapi.cur_break_cnt = 0;
409
    }
410
    if (uarts[i].istat.rxser_full) {
411
      uarts[i].istat.rxser_full = 0;
412
      uarts[i].istat.rxser_clks = 0;
413
    }
414
  } else {
415
    if (uarts[i].istat.rxser_full) {
416
      if (uarts[i].char_clks <= uarts[i].istat.rxser_clks++) {
417
        /* Set unused character bits to zero and allow lsr register in fifo */
418
        uarts[i].iregs.rxser &= ((1 << ((uarts[i].regs.lcr & 3) + 5)) - 1) | 0xff00;
419
        debug(4, "Receiving 0x%02x'%c' via UART%d...\n", uarts[i].iregs.rxser, uarts[i].iregs.rxser, i);
420
        uarts[i].istat.rxser_full = 0;
421
        uarts[i].istat.rxser_clks = 0;
422
        uart_add_char (i, uarts[i].iregs.rxser);
423
      }
424
    }
425
  }
426
 
427
  /* Check if there is something waiting, and put it into rxser */
428
  if (uarts[i].regs.mcr & UART_MCR_LOOP) {
429
    uarts[i].iregs.rxser = uarts[i].iregs.loopback;
430
    uarts[i].istat.rxser_full = 1;
431
  } else {
432
    if (!config.uarts[i].vapi_id) {
433
      if(uarts[i].istat.rxser_full == 0) {
434
        if (uarts[i].slowdown)
435
          uarts[i].slowdown--;
436
        else if((retval = fgetc(uarts[i].rxfs)) != EOF) {
437
          uarts[i].iregs.rxser = (char)retval;
438
          uarts[i].istat.rxser_full = 1;
439
        } else uarts[i].slowdown = UART_FGETC_SLOWDOWN;
440
      }
441
    } else { /* VAPI */
442
      int received = 0;
443
      /* do not handle commands while receiving */
444
      if (uarts[i].istat.rxser_full) return;
445
      while (!received) {
446
        if (uarts[i].vapi_buf_head_ptr != uarts[i].vapi_buf_tail_ptr) {
447
          unsigned long data = uarts[i].vapi_buf[uarts[i].vapi_buf_tail_ptr];
448
          debug(4, "Handling: %08x (%i,%i)\n", data, uarts[i].vapi_buf_head_ptr, uarts[i].vapi_buf_tail_ptr);
449
          uarts[i].vapi_buf_tail_ptr = (uarts[i].vapi_buf_tail_ptr + 1) % UART_VAPI_BUF_LEN;
450
          switch (data >> 24) {
451
            case 0x00:
452
              uarts[i].vapi.lcr = (data >> 8) & 0xff;
453
              /* Put data into rx fifo */
454
              uarts[i].iregs.rxser = data & 0xff;
455
              uarts[i].vapi.char_clks = char_clks (uarts[i].vapi.dll, uarts[i].vapi.dlh, uarts[i].vapi.lcr);
456
              if ((uarts[i].vapi.lcr & ~UART_LCR_SBC) != (uarts[i].regs.lcr & ~UART_LCR_SBC)
457
               || uarts[i].vapi.char_clks != uarts[i].char_clks
458
               || uarts[i].vapi.skew < -MAX_SKEW || uarts[i].vapi.skew > MAX_SKEW) {
459
                debug (3, "WARNING: unmatched VAPI (%02x) and uart (%02x) modes.\n",
460
                      uarts[i].vapi.lcr & ~UART_LCR_SBC, uarts[i].regs.lcr & ~UART_LCR_SBC);
461
                /* Set error bits */
462
                uarts[i].iregs.rxser |= (UART_LSR_FRAME | UART_LSR_RXERR) << 8;
463
                if (uarts[i].regs.lcr & UART_LCR_PARITY) uarts[i].iregs.rxser |= UART_LSR_PARITY << 8;
464
              }
465
              uarts[i].istat.rxser_full = 1;
466
              received = 1;
467
              break;
468
            case 0x01:
469
              uarts[i].vapi.dll = (data >> 0) & 0xff;
470
              uarts[i].vapi.dlh = (data >> 8) & 0xff;
471
              break;
472
            case 0x02:
473
              uarts[i].vapi.lcr = (data >> 8) & 0xff;
474
              break;
475
            case 0x03:
476
              uarts[i].vapi.skew = (signed short)(data & 0xffff);
477
              break;
478
            case 0x04:
479
              uarts[i].vapi.next_break_cnt = data & 0xffff;
480
              uarts[i].vapi.next_break = (data >> 16) & 1;
481
              break;
482
            default:
483
              debug (0, "WARNING: Invalid vapi command %02x\n", data >> 24);
484
              break;
485
          }
486
        } else break;
487
      }
488
    }
489
  }
490
 
491
  /***************** Loopback *****************/
492
  if (uarts[i].regs.mcr & UART_MCR_LOOP) {
493
    debug(5, "uart_clock: Loopback\n");
494
    if ((uarts[i].regs.mcr & UART_MCR_AUX2) !=
495
        ((uarts[i].regs.msr & UART_MSR_DCD) >> 4))
496
      uarts[i].regs.msr |= UART_MSR_DDCD;
497
    if ((uarts[i].regs.mcr & UART_MCR_AUX1) <
498
        ((uarts[i].regs.msr & UART_MSR_RI) >> 4))
499
      uarts[i].regs.msr |= UART_MSR_TERI;
500
    if ((uarts[i].regs.mcr & UART_MCR_RTS) !=
501
        ((uarts[i].regs.msr & UART_MSR_CTS) >> 3))
502
      uarts[i].regs.msr |= UART_MSR_DCTS;
503
    if ((uarts[i].regs.mcr & UART_MCR_DTR) !=
504
        ((uarts[i].regs.msr & UART_MSR_DSR) >> 5))
505
      uarts[i].regs.msr |= UART_MSR_DDSR;
506
    uarts[i].regs.msr &= ~(UART_MSR_DCD | UART_MSR_RI
507
              | UART_MSR_DSR | UART_MSR_CTS);
508
    uarts[i].regs.msr |= ((uarts[i].regs.mcr & UART_MCR_AUX2) << 4);
509
    uarts[i].regs.msr |= ((uarts[i].regs.mcr & UART_MCR_AUX1) << 4);
510
    uarts[i].regs.msr |= ((uarts[i].regs.mcr & UART_MCR_RTS) << 3);
511
    uarts[i].regs.msr |= ((uarts[i].regs.mcr & UART_MCR_DTR) << 5);
512
  }
513
 
514
  if (uarts[i].regs.lsr & UART_LSR_RDRDY)
515
    uarts[i].istat.timeout_count++;
516
 
517
  /* Update LSR error bits from the ones from rx FIFO */
518
  if (uarts[i].istat.rxbuf_full) {
519
    uarts[i].regs.lsr |= uarts[i].regs.rxbuf[uarts[i].istat.rxbuf_tail] >> 8;
520
    /* we must delete the lsr status, so that we can clear it from lsr */
521
    uarts[i].regs.rxbuf[uarts[i].istat.rxbuf_tail] &= 0xff;
522
  }
523
 
524
  /* Interrupt detection in proper priority order. */
525
  uarts[i].regs.iir = UART_IIR_NO_INT;
526
  if (uarts[i].regs.ier & UART_IER_RLSI &&                    /* Receiver LS */
527
      uarts[i].regs.lsr & (UART_LSR_OVRRUN | UART_LSR_PARITY
528
        | UART_LSR_FRAME | UART_LSR_BREAK)) {
529
    uarts[i].regs.iir = UART_IIR_RLSI;
530
  } else if ((uarts[i].regs.ier & UART_IER_RDI)               /* RD available */
531
      && (uarts[i].istat.rxbuf_full >= UART_FIFO_TRIGGER(uarts[i].regs.fcr >> 6))
532
      && (uarts[i].regs.lsr & UART_LSR_RDRDY)) {
533
    uarts[i].regs.iir = UART_IIR_RDI;
534
  } else if ((uarts[i].regs.ier & UART_IER_RDI)               /* timeout */
535
      && (uarts[i].istat.timeout_count >= UART_CHAR_TIMEOUT * uarts[i].char_clks)) {
536
    uarts[i].regs.iir = UART_IIR_CTI;
537
  } else if (uarts[i].regs.ier & UART_IER_THRI &&             /* Transm. empty */
538
      uarts[i].regs.lsr & UART_LSR_TXBUFE &&
539
      uarts[i].istat.thre_int == 1) {
540
    uarts[i].regs.iir = UART_IIR_THRI;
541
  } else if (uarts[i].regs.ier & UART_IER_MSI &&              /* Modem status */
542
      uarts[i].regs.msr & (UART_MSR_DCTS | UART_MSR_DDSR
543
        | UART_MSR_TERI | UART_MSR_DDCD)) {
544
    uarts[i].regs.iir = UART_IIR_MSI;
545
  }
546
  if (!(uarts[i].regs.iir & UART_IIR_NO_INT)) {
547
    debug (4, "uarts[i].regs.iir = %i\t", uarts[i].regs.iir);
548
    report_interrupt(config.uarts[i].irq);
549
  }
550
}
551
 
552 341 markom
/* Reset.  It initializes all registers of all UART devices to zero values,
553 31 lampret
   (re)opens all RX/TX file streams and places devices in memory address
554 341 markom
   space.  */
555 31 lampret
void uart_reset()
556
{
557 355 markom
  int i;
558 997 markom
  if (config.sim.verbose && config.nuarts) PRINTF("Resetting %u UART(s).\n", config.nuarts);
559 355 markom
  memset(uarts, 0, sizeof(uarts));
560 261 markom
 
561 355 markom
  for(i = 0; i < config.nuarts; i++) {
562 341 markom
    if (config.uarts[i].vapi_id) {
563
      if ((config.uarts[i].vapi_id & VAPI_DEVICE_ID) != i) {
564
        fprintf (stderr, "ERROR: Wrong vapi_id (0x%x) for uart %i, last byte is required to be %02x; ignoring.\n", config.uarts[i].vapi_id, i, i);
565
        config.uarts[i].vapi_id = 0;
566
        uarts[i].txfs = 0;
567
      } else {
568 355 markom
        vapi_install_handler (config.uarts[i].vapi_id, uart_vapi_read);
569 970 simons
        register_memoryarea(config.uarts[i].baseaddr, UART_ADDR_SPACE, 1, 0, uart_read_byte, uart_write_byte);
570 355 markom
      }
571
    } else if (config.uarts[i].txfile) { /* MM: Try to create stream.  */
572
      if (!(uarts[i].rxfs = fopen(config.uarts[i].rxfile, "r"))
573
        && !(uarts[i].rxfs = fopen(config.uarts[i].rxfile, "r+"))) {
574 361 markom
        debug (0, "WARNING: UART%d has problems with RX file stream.\n", i);
575 355 markom
        continue;
576
      }
577
      uarts[i].txfs = fopen(config.uarts[i].txfile, "a");
578
      if (uarts[i].rxfs && uarts[i].txfs && config.sim.verbose) {
579 997 markom
        PRINTF("UART%d at 0x%.8x uses ", i, config.uarts[i].baseaddr);
580
        PRINTF("%s for RX and %s for TX.\n", config.uarts[i].rxfile, config.uarts[i].txfile);
581 355 markom
      } else
582 361 markom
        debug (1, "WARNING: UART%d has problems with TX file stream.\n", i);
583 970 simons
      register_memoryarea(config.uarts[i].baseaddr, UART_ADDR_SPACE, 1, 0, uart_read_byte, uart_write_byte);
584 341 markom
    }
585
 
586
    if (config.uarts[i].uart16550)
587
      uarts[i].fifo_len = 16;
588
    else
589
      uarts[i].fifo_len = 1;
590 344 markom
 
591 341 markom
    uarts[i].istat.rxbuf_head = uarts[i].istat.rxbuf_tail = 0;
592
    uarts[i].istat.txbuf_head = uarts[i].istat.txbuf_tail = 0;
593 344 markom
 
594 409 markom
    uarts[i].istat.break_set = 0;
595
    uarts[i].istat.timeout_count = 0;
596 492 markom
    uarts[i].istat.thre_int = 1; /* FIFO is empty at start */
597 713 markom
    uarts[i].slowdown = UART_FGETC_SLOWDOWN;
598 492 markom
 
599 344 markom
    uarts[i].regs.lcr = UART_LCR_RESET;
600 385 markom
    uarts[i].vapi.cur_break = uarts[i].vapi.cur_break_cnt = uarts[i].vapi.next_break = 0;
601
    uarts[i].vapi.next_break_cnt = -1;
602 997 markom
    PRINTF ("%i\n", i);
603 884 markom
    SCHED_ADD (uart_clock16, i, runtime.sim.cycles + UART_CLOCK_DIVIDER);
604 336 markom
  }
605 31 lampret
}
606 261 markom
 
607 31 lampret
/* Print register values on stdout. */
608
void uart_status()
609
{
610 355 markom
  int i, j;
611
 
612
  for(i = 0; i < config.nuarts; i++) {
613
    if ( !config.uarts[i].baseaddr )
614
      continue;
615 997 markom
    PRINTF("\nUART%d visible registers at 0x%.8x:\n", i, config.uarts[i].baseaddr);
616
    PRINTF("RXBUF:");
617 355 markom
    for (j = uarts[i].istat.rxbuf_head; j != uarts[i].istat.rxbuf_tail; j = (j + 1) % uarts[i].fifo_len)
618 997 markom
      PRINTF (" %.2x", uarts[i].regs.rxbuf[j]);
619
    PRINTF("  TXBUF: %.2x\n", uarts[i].regs.txbuf);
620
    PRINTF("DLL  : %.2x  DLH  : %.2x\n", uarts[i].regs.dll, uarts[i].regs.dlh);
621
    PRINTF("IER  : %.2x  IIR  : %.2x\n", uarts[i].regs.ier, uarts[i].regs.iir);
622
    PRINTF("LCR  : %.2x  MCR  : %.2x\n", uarts[i].regs.lcr, uarts[i].regs.mcr);
623
    PRINTF("LSR  : %.2x  MSR  : %.2x\n", uarts[i].regs.lsr, uarts[i].regs.msr);
624
    PRINTF("SCR  : %.2x\n", uarts[i].regs.scr);
625 31 lampret
 
626 997 markom
    PRINTF("\nInternal registers (sim debug):\n");
627
    PRINTF("RXSER: %.2x  TXSER: %.2x\n", uarts[i].iregs.rxser, uarts[i].iregs.txser);
628 31 lampret
 
629 997 markom
    PRINTF("\nInternal status (sim debug):\n");
630
    PRINTF("char_clks: %d\n", uarts[i].char_clks);
631
    PRINTF("rxser_clks: %d  txser_clks: %d\n", uarts[i].istat.rxser_clks, uarts[i].istat.txser_clks);
632
    PRINTF("rxser: %d  txser: %d\n", uarts[i].istat.rxser_full, uarts[i].istat.txser_full);
633
    PRINTF("rxbuf: %d  txbuf: %d\n", uarts[i].istat.rxbuf_full, uarts[i].istat.txbuf_full);
634
    PRINTF("Using IRQ%i\n", config.uarts[i].irq);
635 336 markom
    if (config.uarts[i].vapi_id)
636 997 markom
      PRINTF ("Connected to vapi ID=%x\n\n", config.uarts[i].vapi_id);
637 336 markom
    else
638 997 markom
      PRINTF("RX fs: %p  TX fs: %p\n\n", uarts[i].rxfs, uarts[i].txfs);
639 355 markom
  }
640 31 lampret
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.