/[gxemul]/trunk/src/devices/dev_ns16550.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/src/devices/dev_ns16550.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 14 by dpavlin, Mon Oct 8 16:18:51 2007 UTC revision 34 by dpavlin, Mon Oct 8 16:21:17 2007 UTC
# Line 1  Line 1 
1  /*  /*
2   *  Copyright (C) 2003-2005  Anders Gavare.  All rights reserved.   *  Copyright (C) 2003-2007  Anders Gavare.  All rights reserved.
3   *   *
4   *  Redistribution and use in source and binary forms, with or without   *  Redistribution and use in source and binary forms, with or without
5   *  modification, are permitted provided that the following conditions are met:   *  modification, are permitted provided that the following conditions are met:
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *     *  
27   *   *
28   *  $Id: dev_ns16550.c,v 1.41 2005/09/30 13:33:01 debug Exp $   *  $Id: dev_ns16550.c,v 1.58 2006/12/30 13:30:58 debug Exp $
29   *     *  
30   *  NS16550 serial controller.   *  NS16550 serial controller.
31   *   *
# Line 40  Line 40 
40  #include "console.h"  #include "console.h"
41  #include "cpu.h"  #include "cpu.h"
42  #include "device.h"  #include "device.h"
43    #include "interrupt.h"
44  #include "machine.h"  #include "machine.h"
45  #include "memory.h"  #include "memory.h"
46  #include "misc.h"  #include "misc.h"
# Line 55  Line 56 
56  struct ns_data {  struct ns_data {
57          int             addrmult;          int             addrmult;
58          int             in_use;          int             in_use;
         int             irqnr;  
59          char            *name;          char            *name;
60          int             console_handle;          int             console_handle;
61          int             enable_fifo;          int             enable_fifo;
62    
63            struct interrupt irq;
64    
65          unsigned char   reg[DEV_NS16550_LENGTH];          unsigned char   reg[DEV_NS16550_LENGTH];
66          unsigned char   fcr;            /*  FIFO control register  */          unsigned char   fcr;            /*  FIFO control register  */
67            int             int_asserted;
68          int             dlab;           /*  Divisor Latch Access bit  */          int             dlab;           /*  Divisor Latch Access bit  */
69          int             divisor;          int             divisor;
70    
# Line 72  struct ns_data { Line 74  struct ns_data {
74  };  };
75    
76    
77  /*  DEVICE_TICK(ns16550)
  *  dev_ns16550_tick():  
  *  
  *  This function is called at regular intervals. An interrupt is caused to the  
  *  CPU if there is a character available for reading, or if the transmitter  
  *  slot is empty (i.e. the ns16550 is ready to transmit).  
  */  
 void dev_ns16550_tick(struct cpu *cpu, void *extra)  
78  {  {
79            /*
80             *  This function is called at regular intervals. An interrupt is
81             *  asserted if there is a character available for reading, or if the
82             *  transmitter slot is empty (i.e. the ns16550 is ready to transmit).
83             */
84          struct ns_data *d = extra;          struct ns_data *d = extra;
85    
86          d->reg[com_iir] &= ~IIR_RXRDY;          d->reg[com_iir] &= ~IIR_RXRDY;
87          if (d->in_use && console_charavail(d->console_handle))          if (console_charavail(d->console_handle))
88                  d->reg[com_iir] |= IIR_RXRDY;                  d->reg[com_iir] |= IIR_RXRDY;
89    
90          /*          /*
91           *  If interrupts are enabled, and interrupts are pending, then           *  If interrupts are enabled, and interrupts are pending, then
92           *  cause a CPU interrupt.           *  cause a CPU interrupt.
93           */           */
94    
95          if (((d->reg[com_ier] & IER_ETXRDY) && (d->reg[com_iir] & IIR_TXRDY)) ||          if (((d->reg[com_ier] & IER_ETXRDY) && (d->reg[com_iir] & IIR_TXRDY)) ||
96              ((d->reg[com_ier] & IER_ERXRDY) && (d->reg[com_iir] & IIR_RXRDY))) {              ((d->reg[com_ier] & IER_ERXRDY) && (d->reg[com_iir] & IIR_RXRDY))) {
97                  d->reg[com_iir] &= ~IIR_NOPEND;                  d->reg[com_iir] &= ~IIR_NOPEND;
98                  if (d->reg[com_mcr] & MCR_IENABLE)                  if (d->reg[com_mcr] & MCR_IENABLE) {
99                          cpu_interrupt(cpu, d->irqnr);                          INTERRUPT_ASSERT(d->irq);
100                            d->int_asserted = 1;
101                    }
102          } else {          } else {
103                  d->reg[com_iir] |= IIR_NOPEND;                  d->reg[com_iir] |= IIR_NOPEND;
104                  cpu_interrupt_ack(cpu, d->irqnr);                  if (d->int_asserted)
105                            INTERRUPT_DEASSERT(d->irq);
106                    d->int_asserted = 0;
107          }          }
108  }  }
109    
110    
111  /*  DEVICE_ACCESS(ns16550)
  *  dev_ns16550_access():  
  */  
 int dev_ns16550_access(struct cpu *cpu, struct memory *mem,  
         uint64_t relative_addr, unsigned char *data, size_t len,  
         int writeflag, void *extra)  
112  {  {
113          uint64_t idata = 0, odata=0;          uint64_t idata = 0, odata=0;
114          int i;          size_t i;
115          struct ns_data *d = extra;          struct ns_data *d = extra;
116    
117          idata = memory_readmax64(cpu, data, len);          if (writeflag == MEM_WRITE)
118                    idata = memory_readmax64(cpu, data, len);
119    
120    #if 0
121          /*  The NS16550 should be accessed using byte read/writes:  */          /*  The NS16550 should be accessed using byte read/writes:  */
122          if (len != 1)          if (len != 1)
123                  fatal("[ ns16550 (%s): len=%i, idata=0x%16llx! ]\n",                  fatal("[ ns16550 (%s): len=%i, idata=0x%16llx! ]\n",
124                      d->name, len, (long long)idata);                      d->name, len, (long long)idata);
125    #endif
126    
127          /*          /*
128           *  Always ready to transmit:           *  Always ready to transmit:
# Line 132  int dev_ns16550_access(struct cpu *cpu, Line 135  int dev_ns16550_access(struct cpu *cpu,
135                  d->reg[com_iir] |= ((d->fcr << 5) & 0xc0);                  d->reg[com_iir] |= ((d->fcr << 5) & 0xc0);
136    
137          d->reg[com_lsr] &= ~LSR_RXRDY;          d->reg[com_lsr] &= ~LSR_RXRDY;
138          if (d->in_use && console_charavail(d->console_handle))          if (console_charavail(d->console_handle))
139                  d->reg[com_lsr] |= LSR_RXRDY;                  d->reg[com_lsr] |= LSR_RXRDY;
140    
141          relative_addr /= d->addrmult;          relative_addr /= d->addrmult;
# Line 165  int dev_ns16550_access(struct cpu *cpu, Line 168  int dev_ns16550_access(struct cpu *cpu,
168                                  console_putchar(d->console_handle, idata);                                  console_putchar(d->console_handle, idata);
169                          d->reg[com_iir] |= IIR_TXRDY;                          d->reg[com_iir] |= IIR_TXRDY;
170                  } else {                  } else {
171                          if (d->in_use)                          int x = console_readchar(d->console_handle);
172                                  odata = console_readchar(d->console_handle);                          odata = x < 0? 0 : x;
                         else  
                                 odata = 0;  
173                  }                  }
174                  dev_ns16550_tick(cpu, d);                  dev_ns16550_tick(cpu, d);
175                  break;                  break;
# Line 211  int dev_ns16550_access(struct cpu *cpu, Line 212  int dev_ns16550_access(struct cpu *cpu,
212                          d->fcr = idata;                          d->fcr = idata;
213                  } else {                  } else {
214                          odata = d->reg[com_iir];                          odata = d->reg[com_iir];
215                            if (d->reg[com_iir] & IIR_TXRDY)
216                                    d->reg[com_iir] &= ~IIR_TXRDY;
217                          debug("[ ns16550 (%s): read from iir: 0x%02x ]\n",                          debug("[ ns16550 (%s): read from iir: 0x%02x ]\n",
218                              d->name, (int)odata);                              d->name, (int)odata);
219                          dev_ns16550_tick(cpu, d);                          dev_ns16550_tick(cpu, d);
# Line 284  int dev_ns16550_access(struct cpu *cpu, Line 287  int dev_ns16550_access(struct cpu *cpu,
287                          d->reg[com_mcr] = idata;                          d->reg[com_mcr] = idata;
288                          debug("[ ns16550 (%s): write to mcr: 0x%02x ]\n",                          debug("[ ns16550 (%s): write to mcr: 0x%02x ]\n",
289                              d->name, (int)idata);                              d->name, (int)idata);
290                            if (!(d->reg[com_iir] & IIR_TXRDY)
291                                && (idata & MCR_IENABLE))
292                                    d->reg[com_iir] |= IIR_TXRDY;
293                            dev_ns16550_tick(cpu, d);
294                  } else {                  } else {
295                          odata = d->reg[com_mcr];                          odata = d->reg[com_mcr];
296                          debug("[ ns16550 (%s): read from mcr: 0x%02x ]\n",                          debug("[ ns16550 (%s): read from mcr: 0x%02x ]\n",
# Line 313  int dev_ns16550_access(struct cpu *cpu, Line 320  int dev_ns16550_access(struct cpu *cpu,
320  }  }
321    
322    
323  /*  DEVINIT(ns16550)
  *  devinit_ns16550():  
  */  
 int devinit_ns16550(struct devinit *devinit)  
324  {  {
325          struct ns_data *d = malloc(sizeof(struct ns_data));          struct ns_data *d = malloc(sizeof(struct ns_data));
326          size_t nlen;          size_t nlen;
# Line 327  int devinit_ns16550(struct devinit *devi Line 331  int devinit_ns16550(struct devinit *devi
331                  exit(1);                  exit(1);
332          }          }
333          memset(d, 0, sizeof(struct ns_data));          memset(d, 0, sizeof(struct ns_data));
334          d->irqnr        = devinit->irq_nr;  
335          d->addrmult     = devinit->addr_mult;          d->addrmult     = devinit->addr_mult;
336          d->in_use       = devinit->in_use;          d->in_use       = devinit->in_use;
337          d->enable_fifo  = 1;          d->enable_fifo  = 1;
# Line 338  int devinit_ns16550(struct devinit *devi Line 342  int devinit_ns16550(struct devinit *devi
342          d->stopbits     = "1";          d->stopbits     = "1";
343          d->name         = devinit->name2 != NULL? devinit->name2 : "";          d->name         = devinit->name2 != NULL? devinit->name2 : "";
344          d->console_handle =          d->console_handle =
345              console_start_slave(devinit->machine, devinit->name);              console_start_slave(devinit->machine, devinit->name2 != NULL?
346                devinit->name2 : devinit->name, d->in_use);
347    
348            INTERRUPT_CONNECT(devinit->interrupt_path, d->irq);
349    
350          nlen = strlen(devinit->name) + 10;          nlen = strlen(devinit->name) + 10;
351          if (devinit->name2 != NULL)          if (devinit->name2 != NULL)
# Line 355  int devinit_ns16550(struct devinit *devi Line 362  int devinit_ns16550(struct devinit *devi
362    
363          memory_device_register(devinit->machine->memory, name, devinit->addr,          memory_device_register(devinit->machine->memory, name, devinit->addr,
364              DEV_NS16550_LENGTH * d->addrmult, dev_ns16550_access, d,              DEV_NS16550_LENGTH * d->addrmult, dev_ns16550_access, d,
365              MEM_DEFAULT, NULL);              DM_DEFAULT, NULL);
366          machine_add_tickfunction(devinit->machine,          machine_add_tickfunction(devinit->machine,
367              dev_ns16550_tick, d, TICK_SHIFT);              dev_ns16550_tick, d, TICK_SHIFT, 0.0);
368    
369          /*          /*
370           *  NOTE:  Ugly cast into a pointer, because this is a convenient way           *  NOTE:  Ugly cast into a pointer, because this is a convenient way

Legend:
Removed from v.14  
changed lines
  Added in v.34

  ViewVC Help
Powered by ViewVC 1.1.26