1 |
/* |
/* |
2 |
* Copyright (C) 2003-2005 Anders Gavare. All rights reserved. |
* Copyright (C) 2003-2006 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: |
25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: dev_ns16550.c,v 1.42 2005/10/26 14:37:04 debug Exp $ |
* $Id: dev_ns16550.c,v 1.54 2006/04/06 18:08:42 debug Exp $ |
29 |
* |
* |
30 |
* NS16550 serial controller. |
* NS16550 serial controller. |
31 |
* |
* |
62 |
|
|
63 |
unsigned char reg[DEV_NS16550_LENGTH]; |
unsigned char reg[DEV_NS16550_LENGTH]; |
64 |
unsigned char fcr; /* FIFO control register */ |
unsigned char fcr; /* FIFO control register */ |
65 |
|
int int_asserted; |
66 |
int dlab; /* Divisor Latch Access bit */ |
int dlab; /* Divisor Latch Access bit */ |
67 |
int divisor; |
int divisor; |
68 |
|
|
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); |
cpu_interrupt(cpu, d->irqnr); |
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 |
|
cpu_interrupt_ack(cpu, d->irqnr); |
106 |
|
d->int_asserted = 0; |
107 |
} |
} |
108 |
} |
} |
109 |
|
|
111 |
/* |
/* |
112 |
* dev_ns16550_access(): |
* dev_ns16550_access(): |
113 |
*/ |
*/ |
114 |
int dev_ns16550_access(struct cpu *cpu, struct memory *mem, |
DEVICE_ACCESS(ns16550) |
|
uint64_t relative_addr, unsigned char *data, size_t len, |
|
|
int writeflag, void *extra) |
|
115 |
{ |
{ |
116 |
uint64_t idata = 0, odata=0; |
uint64_t idata = 0, odata=0; |
117 |
int i; |
size_t i; |
118 |
struct ns_data *d = extra; |
struct ns_data *d = extra; |
119 |
|
|
120 |
if (writeflag == MEM_WRITE) |
if (writeflag == MEM_WRITE) |
121 |
idata = memory_readmax64(cpu, data, len); |
idata = memory_readmax64(cpu, data, len); |
122 |
|
|
123 |
|
#if 0 |
124 |
/* The NS16550 should be accessed using byte read/writes: */ |
/* The NS16550 should be accessed using byte read/writes: */ |
125 |
if (len != 1) |
if (len != 1) |
126 |
fatal("[ ns16550 (%s): len=%i, idata=0x%16llx! ]\n", |
fatal("[ ns16550 (%s): len=%i, idata=0x%16llx! ]\n", |
127 |
d->name, len, (long long)idata); |
d->name, len, (long long)idata); |
128 |
|
#endif |
129 |
|
|
130 |
/* |
/* |
131 |
* Always ready to transmit: |
* Always ready to transmit: |
138 |
d->reg[com_iir] |= ((d->fcr << 5) & 0xc0); |
d->reg[com_iir] |= ((d->fcr << 5) & 0xc0); |
139 |
|
|
140 |
d->reg[com_lsr] &= ~LSR_RXRDY; |
d->reg[com_lsr] &= ~LSR_RXRDY; |
141 |
if (d->in_use && console_charavail(d->console_handle)) |
if (console_charavail(d->console_handle)) |
142 |
d->reg[com_lsr] |= LSR_RXRDY; |
d->reg[com_lsr] |= LSR_RXRDY; |
143 |
|
|
144 |
relative_addr /= d->addrmult; |
relative_addr /= d->addrmult; |
171 |
console_putchar(d->console_handle, idata); |
console_putchar(d->console_handle, idata); |
172 |
d->reg[com_iir] |= IIR_TXRDY; |
d->reg[com_iir] |= IIR_TXRDY; |
173 |
} else { |
} else { |
174 |
if (d->in_use) |
int x = console_readchar(d->console_handle); |
175 |
odata = console_readchar(d->console_handle); |
odata = x < 0? 0 : x; |
|
else |
|
|
odata = 0; |
|
176 |
} |
} |
177 |
dev_ns16550_tick(cpu, d); |
dev_ns16550_tick(cpu, d); |
178 |
break; |
break; |
215 |
d->fcr = idata; |
d->fcr = idata; |
216 |
} else { |
} else { |
217 |
odata = d->reg[com_iir]; |
odata = d->reg[com_iir]; |
218 |
|
if (d->reg[com_iir] & IIR_TXRDY) |
219 |
|
d->reg[com_iir] &= ~IIR_TXRDY; |
220 |
debug("[ ns16550 (%s): read from iir: 0x%02x ]\n", |
debug("[ ns16550 (%s): read from iir: 0x%02x ]\n", |
221 |
d->name, (int)odata); |
d->name, (int)odata); |
222 |
dev_ns16550_tick(cpu, d); |
dev_ns16550_tick(cpu, d); |
290 |
d->reg[com_mcr] = idata; |
d->reg[com_mcr] = idata; |
291 |
debug("[ ns16550 (%s): write to mcr: 0x%02x ]\n", |
debug("[ ns16550 (%s): write to mcr: 0x%02x ]\n", |
292 |
d->name, (int)idata); |
d->name, (int)idata); |
293 |
|
if (!(d->reg[com_iir] & IIR_TXRDY) |
294 |
|
&& (idata & MCR_IENABLE)) |
295 |
|
d->reg[com_iir] |= IIR_TXRDY; |
296 |
|
dev_ns16550_tick(cpu, d); |
297 |
} else { |
} else { |
298 |
odata = d->reg[com_mcr]; |
odata = d->reg[com_mcr]; |
299 |
debug("[ ns16550 (%s): read from mcr: 0x%02x ]\n", |
debug("[ ns16550 (%s): read from mcr: 0x%02x ]\n", |
323 |
} |
} |
324 |
|
|
325 |
|
|
326 |
/* |
DEVINIT(ns16550) |
|
* devinit_ns16550(): |
|
|
*/ |
|
|
int devinit_ns16550(struct devinit *devinit) |
|
327 |
{ |
{ |
328 |
struct ns_data *d = malloc(sizeof(struct ns_data)); |
struct ns_data *d = malloc(sizeof(struct ns_data)); |
329 |
size_t nlen; |
size_t nlen; |
345 |
d->stopbits = "1"; |
d->stopbits = "1"; |
346 |
d->name = devinit->name2 != NULL? devinit->name2 : ""; |
d->name = devinit->name2 != NULL? devinit->name2 : ""; |
347 |
d->console_handle = |
d->console_handle = |
348 |
console_start_slave(devinit->machine, devinit->name); |
console_start_slave(devinit->machine, devinit->name2 != NULL? |
349 |
|
devinit->name2 : devinit->name, d->in_use); |
350 |
|
|
351 |
nlen = strlen(devinit->name) + 10; |
nlen = strlen(devinit->name) + 10; |
352 |
if (devinit->name2 != NULL) |
if (devinit->name2 != NULL) |
363 |
|
|
364 |
memory_device_register(devinit->machine->memory, name, devinit->addr, |
memory_device_register(devinit->machine->memory, name, devinit->addr, |
365 |
DEV_NS16550_LENGTH * d->addrmult, dev_ns16550_access, d, |
DEV_NS16550_LENGTH * d->addrmult, dev_ns16550_access, d, |
366 |
MEM_DEFAULT, NULL); |
DM_DEFAULT, NULL); |
367 |
machine_add_tickfunction(devinit->machine, |
machine_add_tickfunction(devinit->machine, |
368 |
dev_ns16550_tick, d, TICK_SHIFT); |
dev_ns16550_tick, d, TICK_SHIFT, 0.0); |
369 |
|
|
370 |
/* |
/* |
371 |
* NOTE: Ugly cast into a pointer, because this is a convenient way |
* NOTE: Ugly cast into a pointer, because this is a convenient way |