| 1 |
/* |
| 2 |
* Cisco 3600 simulation platform. |
| 3 |
* Copyright (c) 2006 Christophe Fillot ([email protected]) |
| 4 |
* Patched by Jeremy Grossmann for the GNS3 project (www.gns3.net) |
| 5 |
* |
| 6 |
* NS16552 DUART. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <sys/types.h> |
| 14 |
|
| 15 |
#include <termios.h> |
| 16 |
#include <fcntl.h> |
| 17 |
#include <pthread.h> |
| 18 |
|
| 19 |
#include "ptask.h" |
| 20 |
#include "cpu.h" |
| 21 |
#include "vm.h" |
| 22 |
#include "dynamips.h" |
| 23 |
#include "memory.h" |
| 24 |
#include "device.h" |
| 25 |
#include "dev_vtty.h" |
| 26 |
|
| 27 |
/* Debugging flags */ |
| 28 |
#define DEBUG_UNKNOWN 1 |
| 29 |
#define DEBUG_ACCESS 0 |
| 30 |
|
| 31 |
/* Interrupt Enable Register (IER) */ |
| 32 |
#define IER_ERXRDY 0x1 |
| 33 |
#define IER_ETXRDY 0x2 |
| 34 |
|
| 35 |
/* Interrupt Identification Register */ |
| 36 |
#define IIR_NPENDING 0x01 /* 0: irq pending, 1: no irq pending */ |
| 37 |
#define IIR_TXRDY 0x02 |
| 38 |
#define IIR_RXRDY 0x04 |
| 39 |
|
| 40 |
/* Line Status Register (LSR) */ |
| 41 |
#define LSR_RXRDY 0x01 |
| 42 |
#define LSR_TXRDY 0x20 |
| 43 |
#define LSR_TXEMPTY 0x40 |
| 44 |
|
| 45 |
/* Line Control Register */ |
| 46 |
#define LCR_WRL0 0x01 |
| 47 |
#define LCR_WRL1 0x02 |
| 48 |
#define LCR_NUMSTOP 0x04 |
| 49 |
#define LCR_PARITYON 0x08 |
| 50 |
#define LCR_PARITYEV 0x10 |
| 51 |
#define LCR_STICKP 0x20 |
| 52 |
#define LCR_SETBREAK 0x40 |
| 53 |
#define LCR_DIVLATCH 0x80 |
| 54 |
|
| 55 |
/* Modem Control Register */ |
| 56 |
#define MCR_DTR 0x01 |
| 57 |
#define MCR_RTS 0x02 |
| 58 |
#define MCR_OUT1 0x04 |
| 59 |
#define MCR_OUT2 0x08 |
| 60 |
#define MCR_LOOP 0x10 |
| 61 |
|
| 62 |
/* UART channel */ |
| 63 |
struct ns16552_channel { |
| 64 |
u_int ier,output; |
| 65 |
vtty_t *vtty; |
| 66 |
}; |
| 67 |
|
| 68 |
/* NS16552 structure */ |
| 69 |
struct ns16552_data { |
| 70 |
vm_obj_t vm_obj; |
| 71 |
struct vdevice dev; |
| 72 |
vm_instance_t *vm; |
| 73 |
u_int irq; |
| 74 |
|
| 75 |
/* Register offset divisor */ |
| 76 |
u_int reg_div; |
| 77 |
|
| 78 |
/* Periodic task to trigger DUART IRQ */ |
| 79 |
ptask_id_t tid; |
| 80 |
|
| 81 |
struct ns16552_channel channel[2]; |
| 82 |
u_int duart_irq_seq; |
| 83 |
|
| 84 |
u_int line_control_reg; |
| 85 |
u_int div_latch; |
| 86 |
u_int baud_divisor; |
| 87 |
}; |
| 88 |
|
| 89 |
/* Console port input */ |
| 90 |
static void tty_con_input(vtty_t *vtty) |
| 91 |
{ |
| 92 |
struct ns16552_data *d = vtty->priv_data; |
| 93 |
|
| 94 |
if (d->channel[0].ier & IER_ERXRDY) |
| 95 |
vm_set_irq(d->vm,d->irq); |
| 96 |
} |
| 97 |
|
| 98 |
/* AUX port input */ |
| 99 |
static void tty_aux_input(vtty_t *vtty) |
| 100 |
{ |
| 101 |
struct ns16552_data *d = vtty->priv_data; |
| 102 |
|
| 103 |
if (d->channel[1].ier & IER_ERXRDY) |
| 104 |
vm_set_irq(d->vm,d->irq); |
| 105 |
} |
| 106 |
|
| 107 |
/* IRQ trickery for Console and AUX ports */ |
| 108 |
static int tty_trigger_dummy_irq(struct ns16552_data *d,void *arg) |
| 109 |
{ |
| 110 |
d->duart_irq_seq++; |
| 111 |
|
| 112 |
if (d->duart_irq_seq == 2) { |
| 113 |
if (d->channel[0].ier & IER_ETXRDY) { |
| 114 |
d->channel[0].output = TRUE; |
| 115 |
vm_set_irq(d->vm,d->irq); |
| 116 |
} |
| 117 |
|
| 118 |
if (d->channel[1].ier & IER_ETXRDY) { |
| 119 |
d->channel[1].output = TRUE; |
| 120 |
vm_set_irq(d->vm,d->irq); |
| 121 |
} |
| 122 |
|
| 123 |
d->duart_irq_seq = 0; |
| 124 |
} |
| 125 |
|
| 126 |
return(0); |
| 127 |
} |
| 128 |
|
| 129 |
/* |
| 130 |
* dev_ns16552_access() |
| 131 |
*/ |
| 132 |
void *dev_ns16552_access(cpu_gen_t *cpu,struct vdevice *dev,m_uint32_t offset, |
| 133 |
u_int op_size,u_int op_type,m_uint64_t *data) |
| 134 |
{ |
| 135 |
struct ns16552_data *d = dev->priv_data; |
| 136 |
int channel = 0; |
| 137 |
u_char odata; |
| 138 |
|
| 139 |
if (op_type == MTS_READ) |
| 140 |
*data = 0; |
| 141 |
|
| 142 |
#if DEBUG_ACCESS |
| 143 |
if (op_type == MTS_READ) { |
| 144 |
cpu_log(cpu,"NS16552","read from 0x%x, pc=0x%llx\n", |
| 145 |
offset,cpu_get_pc(cpu)); |
| 146 |
} else { |
| 147 |
cpu_log(cpu,"NS16552","write to 0x%x, value=0x%llx, pc=0x%llx\n", |
| 148 |
offset,*data,cpu_get_pc(cpu)); |
| 149 |
} |
| 150 |
#endif |
| 151 |
|
| 152 |
offset >>= d->reg_div; |
| 153 |
|
| 154 |
if (offset >= 0x08) |
| 155 |
channel = 1; |
| 156 |
|
| 157 |
// From the NS16552V datasheet, the following is known about the registers |
| 158 |
// Bit 4 is channel |
| 159 |
// Value 0 Receive or transmit buffer |
| 160 |
// Value 1 Interrupt enable |
| 161 |
// Value 2 Interrupt identification (READ), FIFO Config (Write) |
| 162 |
// Value 3 Line Control (Appears in IOS) |
| 163 |
// 0x1 - Word Length Selector bit 0 |
| 164 |
// 0x2 - Word Length Selector bit 1 |
| 165 |
// 0x4 - Num stop bits |
| 166 |
// 0x8 - Parity Enable |
| 167 |
// 0x16 - Parity even |
| 168 |
// 0x32 - Stick Parity |
| 169 |
// 0x64 - Set Break |
| 170 |
// 0x128 - Division Latch |
| 171 |
// Value 4 Modem Control (Appears in IOS) |
| 172 |
// Value 5 Line status |
| 173 |
// Value 6 Modem Status |
| 174 |
// Value 7 Scratch |
| 175 |
|
| 176 |
switch(offset) { |
| 177 |
/* Receiver Buffer Reg. (RBR) / Transmitting Holding Reg. (THR) */ |
| 178 |
case 0x00: |
| 179 |
case 0x08: |
| 180 |
if (d->div_latch == 0) { |
| 181 |
if (op_type == MTS_WRITE) { |
| 182 |
vtty_put_char(d->channel[channel].vtty,(char)*data); |
| 183 |
|
| 184 |
if (d->channel[channel].ier & IER_ETXRDY) |
| 185 |
vm_set_irq(d->vm,d->irq); |
| 186 |
|
| 187 |
d->channel[channel].output = TRUE; |
| 188 |
} else |
| 189 |
*data = vtty_get_char(d->channel[channel].vtty); |
| 190 |
} else { |
| 191 |
if (op_type == MTS_WRITE) |
| 192 |
d->baud_divisor = ((*data) & 0x00ff) | (d->baud_divisor & 0xff00); |
| 193 |
} |
| 194 |
break; |
| 195 |
|
| 196 |
/* Interrupt Enable Register (IER) */ |
| 197 |
case 0x01: |
| 198 |
case 0x09: |
| 199 |
if (d->div_latch == 0) { |
| 200 |
if (op_type == MTS_READ) { |
| 201 |
*data = d->channel[channel].ier; |
| 202 |
} else { |
| 203 |
d->channel[channel].ier = *data & 0xFF; |
| 204 |
|
| 205 |
if ((*data & 0x02) == 0) { /* transmit holding register */ |
| 206 |
d->channel[channel].vtty->managed_flush = TRUE; |
| 207 |
vtty_flush(d->channel[channel].vtty); |
| 208 |
} |
| 209 |
} |
| 210 |
} else { |
| 211 |
if (op_type == MTS_WRITE) |
| 212 |
d->baud_divisor = (((*data) & 0xff)<<8)|(d->baud_divisor & 0xff); |
| 213 |
} |
| 214 |
break; |
| 215 |
|
| 216 |
/* Interrupt Ident Register (IIR) */ |
| 217 |
case 0x02: |
| 218 |
case 0x0A: |
| 219 |
if (d->div_latch == 0) { |
| 220 |
vm_clear_irq(d->vm,d->irq); |
| 221 |
if (op_type == MTS_READ) { |
| 222 |
odata = IIR_NPENDING; |
| 223 |
|
| 224 |
if (vtty_is_char_avail(d->channel[channel].vtty)) { |
| 225 |
odata = IIR_RXRDY; |
| 226 |
} else { |
| 227 |
if (d->channel[channel].output) { |
| 228 |
odata = IIR_TXRDY; |
| 229 |
d->channel[channel].output = 0; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
*data = odata; |
| 234 |
} |
| 235 |
} |
| 236 |
break; |
| 237 |
/* Line Control Register (LCR) */ |
| 238 |
case 0x03: |
| 239 |
case 0x0B: |
| 240 |
if (op_type == MTS_READ) { |
| 241 |
*data = d->line_control_reg; |
| 242 |
} else { |
| 243 |
|
| 244 |
d->line_control_reg = (uint)*data; |
| 245 |
uint bits = 5; |
| 246 |
_maybe_used char *stop = "1"; |
| 247 |
_maybe_used char *parity = "no "; |
| 248 |
_maybe_used char *parityeven = "odd"; |
| 249 |
if (*data & LCR_WRL0) bits+=1; |
| 250 |
if (*data & LCR_WRL1) bits+=2; |
| 251 |
|
| 252 |
if (*data & LCR_NUMSTOP) { |
| 253 |
if ( bits >= 6) { |
| 254 |
stop = "2"; |
| 255 |
} else { |
| 256 |
stop = "1.5"; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
if (*data & LCR_PARITYON) |
| 261 |
parity=""; //Parity on |
| 262 |
if (*data & LCR_PARITYEV) |
| 263 |
parityeven="even"; |
| 264 |
|
| 265 |
// DIV LATCH changes the behavior of 0x0,0x1,and 0x2 |
| 266 |
if (*data & LCR_DIVLATCH) { |
| 267 |
d->div_latch = 1; |
| 268 |
} else { |
| 269 |
_maybe_used uint baud; |
| 270 |
d->div_latch = 0; |
| 271 |
// 1200 divisor was 192 |
| 272 |
// 9600 divisor was 24 |
| 273 |
// 19200 divisor was 12 |
| 274 |
// Suggests a crystal of 3686400 hz |
| 275 |
if (d->baud_divisor > 0) { |
| 276 |
baud = 3686400 / (d->baud_divisor * 16); |
| 277 |
} else { |
| 278 |
baud = 0; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
break; |
| 283 |
/* MODEM Control Register (MCR) */ |
| 284 |
case 0x04: |
| 285 |
case 0x0C: |
| 286 |
if (op_type != MTS_READ) { |
| 287 |
_maybe_used char *f1 = ""; |
| 288 |
_maybe_used char *f2 = ""; |
| 289 |
_maybe_used char *f3 = ""; |
| 290 |
_maybe_used char *f4 = ""; |
| 291 |
_maybe_used char *f5 = ""; |
| 292 |
if (*data & MCR_DTR) f1 = "DTR "; |
| 293 |
if (*data & MCR_RTS) f2 = "RTS "; |
| 294 |
if (*data & MCR_OUT1) f3 = "OUT1 "; |
| 295 |
if (*data & MCR_OUT2) f4 = "OUT2 "; |
| 296 |
if (*data & MCR_LOOP) f5 = "LOOP "; |
| 297 |
} |
| 298 |
break; |
| 299 |
|
| 300 |
/* Line Status Register (LSR) */ |
| 301 |
case 0x05: |
| 302 |
case 0x0D: |
| 303 |
if (op_type == MTS_READ) { |
| 304 |
odata = 0; |
| 305 |
|
| 306 |
if (vtty_is_char_avail(d->channel[channel].vtty)) |
| 307 |
odata |= LSR_RXRDY; |
| 308 |
|
| 309 |
odata |= LSR_TXRDY|LSR_TXEMPTY; |
| 310 |
*data = odata; |
| 311 |
} |
| 312 |
break; |
| 313 |
|
| 314 |
/* MODEM Status Register (MSR)? |
| 315 |
case 0x06: |
| 316 |
case 0x0E: |
| 317 |
*/ |
| 318 |
|
| 319 |
/* Scratch Register (SCR)? |
| 320 |
case 0x07: |
| 321 |
case 0x0F: |
| 322 |
*/ |
| 323 |
|
| 324 |
#if DEBUG_UNKNOWN |
| 325 |
default: |
| 326 |
if (op_type == MTS_READ) { |
| 327 |
cpu_log(cpu,"NS16552","read from addr 0x%x, pc=0x%llx (size=%u)\n", |
| 328 |
offset,cpu_get_pc(cpu),op_size); |
| 329 |
} else { |
| 330 |
cpu_log(cpu, |
| 331 |
"NS16552","write to addr 0x%x, value=0x%llx, " |
| 332 |
"pc=0x%llx (size=%u)\n", |
| 333 |
offset,*data,cpu_get_pc(cpu),op_size); |
| 334 |
} |
| 335 |
#endif |
| 336 |
} |
| 337 |
|
| 338 |
return NULL; |
| 339 |
} |
| 340 |
|
| 341 |
/* Shutdown a NS16552 device */ |
| 342 |
void dev_ns16552_shutdown(vm_instance_t *vm,struct ns16552_data *d) |
| 343 |
{ |
| 344 |
if (d != NULL) { |
| 345 |
d->channel[0].vtty->read_notifier = NULL; |
| 346 |
d->channel[1].vtty->read_notifier = NULL; |
| 347 |
|
| 348 |
/* Remove the periodic task */ |
| 349 |
ptask_remove(d->tid); |
| 350 |
|
| 351 |
/* Remove the device */ |
| 352 |
dev_remove(vm,&d->dev); |
| 353 |
|
| 354 |
/* Free the structure itself */ |
| 355 |
free(d); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
/* Create a NS16552 device */ |
| 360 |
int dev_ns16552_init(vm_instance_t *vm,m_uint64_t paddr,m_uint32_t len, |
| 361 |
u_int reg_div,u_int irq,vtty_t *vtty_A,vtty_t *vtty_B) |
| 362 |
{ |
| 363 |
struct ns16552_data *d; |
| 364 |
|
| 365 |
/* Allocate private data structure */ |
| 366 |
if (!(d = malloc(sizeof(*d)))) { |
| 367 |
fprintf(stderr,"NS16552: out of memory\n"); |
| 368 |
return(-1); |
| 369 |
} |
| 370 |
|
| 371 |
memset(d,0,sizeof(*d)); |
| 372 |
d->vm = vm; |
| 373 |
d->irq = irq; |
| 374 |
d->reg_div = reg_div; |
| 375 |
d->channel[0].vtty = vtty_A; |
| 376 |
d->channel[1].vtty = vtty_B; |
| 377 |
d->line_control_reg = 0; |
| 378 |
d->div_latch = 0; |
| 379 |
d->baud_divisor=0; |
| 380 |
|
| 381 |
vm_object_init(&d->vm_obj); |
| 382 |
d->vm_obj.name = "ns16552"; |
| 383 |
d->vm_obj.data = d; |
| 384 |
d->vm_obj.shutdown = (vm_shutdown_t)dev_ns16552_shutdown; |
| 385 |
|
| 386 |
/* Set device properties */ |
| 387 |
dev_init(&d->dev); |
| 388 |
d->dev.name = "ns16552"; |
| 389 |
d->dev.phys_addr = paddr; |
| 390 |
d->dev.phys_len = len; |
| 391 |
d->dev.handler = dev_ns16552_access; |
| 392 |
d->dev.priv_data = d; |
| 393 |
|
| 394 |
vtty_A->priv_data = d; |
| 395 |
vtty_B->priv_data = d; |
| 396 |
vtty_A->read_notifier = tty_con_input; |
| 397 |
vtty_B->read_notifier = tty_aux_input; |
| 398 |
|
| 399 |
/* Trigger periodically a dummy IRQ to flush buffers */ |
| 400 |
d->tid = ptask_add((ptask_callback)tty_trigger_dummy_irq,d,NULL); |
| 401 |
|
| 402 |
/* Map this device to the VM */ |
| 403 |
vm_bind_device(vm,&d->dev); |
| 404 |
vm_object_add(vm,&d->vm_obj); |
| 405 |
return(0); |
| 406 |
} |