1 /* 2 * QEMU GE IP-Octal 232 IndustryPack emulation 3 * 4 * Copyright (C) 2012 Igalia, S.L. 5 * Author: Alberto Garcia <berto@igalia.com> 6 * 7 * This code is licensed under the GNU GPL v2 or (at your option) any 8 * later version. 9 */ 10 11 #include "qemu/osdep.h" 12 #include "hw/ipack/ipack.h" 13 #include "hw/irq.h" 14 #include "qemu/bitops.h" 15 #include "qemu/module.h" 16 #include "chardev/char-fe.h" 17 18 /* #define DEBUG_IPOCTAL */ 19 20 #ifdef DEBUG_IPOCTAL 21 #define DPRINTF2(fmt, ...) \ 22 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) 23 #else 24 #define DPRINTF2(fmt, ...) do { } while (0) 25 #endif 26 27 #define DPRINTF(fmt, ...) DPRINTF2("IP-Octal: " fmt, ## __VA_ARGS__) 28 29 #define RX_FIFO_SIZE 3 30 31 /* The IP-Octal has 8 channels (a-h) 32 divided into 4 blocks (A-D) */ 33 #define N_CHANNELS 8 34 #define N_BLOCKS 4 35 36 #define REG_MRa 0x01 37 #define REG_MRb 0x11 38 #define REG_SRa 0x03 39 #define REG_SRb 0x13 40 #define REG_CSRa 0x03 41 #define REG_CSRb 0x13 42 #define REG_CRa 0x05 43 #define REG_CRb 0x15 44 #define REG_RHRa 0x07 45 #define REG_RHRb 0x17 46 #define REG_THRa 0x07 47 #define REG_THRb 0x17 48 #define REG_ACR 0x09 49 #define REG_ISR 0x0B 50 #define REG_IMR 0x0B 51 #define REG_OPCR 0x1B 52 53 #define CR_ENABLE_RX BIT(0) 54 #define CR_DISABLE_RX BIT(1) 55 #define CR_ENABLE_TX BIT(2) 56 #define CR_DISABLE_TX BIT(3) 57 #define CR_CMD(cr) ((cr) >> 4) 58 #define CR_NO_OP 0 59 #define CR_RESET_MR 1 60 #define CR_RESET_RX 2 61 #define CR_RESET_TX 3 62 #define CR_RESET_ERR 4 63 #define CR_RESET_BRKINT 5 64 #define CR_START_BRK 6 65 #define CR_STOP_BRK 7 66 #define CR_ASSERT_RTSN 8 67 #define CR_NEGATE_RTSN 9 68 #define CR_TIMEOUT_ON 10 69 #define CR_TIMEOUT_OFF 12 70 71 #define SR_RXRDY BIT(0) 72 #define SR_FFULL BIT(1) 73 #define SR_TXRDY BIT(2) 74 #define SR_TXEMT BIT(3) 75 #define SR_OVERRUN BIT(4) 76 #define SR_PARITY BIT(5) 77 #define SR_FRAMING BIT(6) 78 #define SR_BREAK BIT(7) 79 80 #define ISR_TXRDYA BIT(0) 81 #define ISR_RXRDYA BIT(1) 82 #define ISR_BREAKA BIT(2) 83 #define ISR_CNTRDY BIT(3) 84 #define ISR_TXRDYB BIT(4) 85 #define ISR_RXRDYB BIT(5) 86 #define ISR_BREAKB BIT(6) 87 #define ISR_MPICHG BIT(7) 88 #define ISR_TXRDY(CH) (((CH) & 1) ? BIT(4) : BIT(0)) 89 #define ISR_RXRDY(CH) (((CH) & 1) ? BIT(5) : BIT(1)) 90 #define ISR_BREAK(CH) (((CH) & 1) ? BIT(6) : BIT(2)) 91 92 typedef struct IPOctalState IPOctalState; 93 typedef struct SCC2698Channel SCC2698Channel; 94 typedef struct SCC2698Block SCC2698Block; 95 96 struct SCC2698Channel { 97 IPOctalState *ipoctal; 98 CharBackend dev; 99 bool rx_enabled; 100 uint8_t mr[2]; 101 uint8_t mr_idx; 102 uint8_t sr; 103 uint8_t rhr[RX_FIFO_SIZE]; 104 uint8_t rhr_idx; 105 uint8_t rx_pending; 106 }; 107 108 struct SCC2698Block { 109 uint8_t imr; 110 uint8_t isr; 111 }; 112 113 struct IPOctalState { 114 IPackDevice parent_obj; 115 116 SCC2698Channel ch[N_CHANNELS]; 117 SCC2698Block blk[N_BLOCKS]; 118 uint8_t irq_vector; 119 }; 120 121 #define TYPE_IPOCTAL "ipoctal232" 122 123 #define IPOCTAL(obj) \ 124 OBJECT_CHECK(IPOctalState, (obj), TYPE_IPOCTAL) 125 126 static const VMStateDescription vmstate_scc2698_channel = { 127 .name = "scc2698_channel", 128 .version_id = 1, 129 .minimum_version_id = 1, 130 .fields = (VMStateField[]) { 131 VMSTATE_BOOL(rx_enabled, SCC2698Channel), 132 VMSTATE_UINT8_ARRAY(mr, SCC2698Channel, 2), 133 VMSTATE_UINT8(mr_idx, SCC2698Channel), 134 VMSTATE_UINT8(sr, SCC2698Channel), 135 VMSTATE_UINT8_ARRAY(rhr, SCC2698Channel, RX_FIFO_SIZE), 136 VMSTATE_UINT8(rhr_idx, SCC2698Channel), 137 VMSTATE_UINT8(rx_pending, SCC2698Channel), 138 VMSTATE_END_OF_LIST() 139 } 140 }; 141 142 static const VMStateDescription vmstate_scc2698_block = { 143 .name = "scc2698_block", 144 .version_id = 1, 145 .minimum_version_id = 1, 146 .fields = (VMStateField[]) { 147 VMSTATE_UINT8(imr, SCC2698Block), 148 VMSTATE_UINT8(isr, SCC2698Block), 149 VMSTATE_END_OF_LIST() 150 } 151 }; 152 153 static const VMStateDescription vmstate_ipoctal = { 154 .name = "ipoctal232", 155 .version_id = 1, 156 .minimum_version_id = 1, 157 .fields = (VMStateField[]) { 158 VMSTATE_IPACK_DEVICE(parent_obj, IPOctalState), 159 VMSTATE_STRUCT_ARRAY(ch, IPOctalState, N_CHANNELS, 1, 160 vmstate_scc2698_channel, SCC2698Channel), 161 VMSTATE_STRUCT_ARRAY(blk, IPOctalState, N_BLOCKS, 1, 162 vmstate_scc2698_block, SCC2698Block), 163 VMSTATE_UINT8(irq_vector, IPOctalState), 164 VMSTATE_END_OF_LIST() 165 } 166 }; 167 168 /* data[10] is 0x0C, not 0x0B as the doc says */ 169 static const uint8_t id_prom_data[] = { 170 0x49, 0x50, 0x41, 0x43, 0xF0, 0x22, 171 0xA1, 0x00, 0x00, 0x00, 0x0C, 0xCC 172 }; 173 174 static void update_irq(IPOctalState *dev, unsigned block) 175 { 176 IPackDevice *idev = IPACK_DEVICE(dev); 177 /* Blocks A and B interrupt on INT0#, C and D on INT1#. 178 Thus, to get the status we have to check two blocks. */ 179 SCC2698Block *blk0 = &dev->blk[block]; 180 SCC2698Block *blk1 = &dev->blk[block^1]; 181 unsigned intno = block / 2; 182 183 if ((blk0->isr & blk0->imr) || (blk1->isr & blk1->imr)) { 184 qemu_irq_raise(idev->irq[intno]); 185 } else { 186 qemu_irq_lower(idev->irq[intno]); 187 } 188 } 189 190 static void write_cr(IPOctalState *dev, unsigned channel, uint8_t val) 191 { 192 SCC2698Channel *ch = &dev->ch[channel]; 193 SCC2698Block *blk = &dev->blk[channel / 2]; 194 195 DPRINTF("Write CR%c %u: ", channel + 'a', val); 196 197 /* The lower 4 bits are used to enable and disable Tx and Rx */ 198 if (val & CR_ENABLE_RX) { 199 DPRINTF2("Rx on, "); 200 ch->rx_enabled = true; 201 } 202 if (val & CR_DISABLE_RX) { 203 DPRINTF2("Rx off, "); 204 ch->rx_enabled = false; 205 } 206 if (val & CR_ENABLE_TX) { 207 DPRINTF2("Tx on, "); 208 ch->sr |= SR_TXRDY | SR_TXEMT; 209 blk->isr |= ISR_TXRDY(channel); 210 } 211 if (val & CR_DISABLE_TX) { 212 DPRINTF2("Tx off, "); 213 ch->sr &= ~(SR_TXRDY | SR_TXEMT); 214 blk->isr &= ~ISR_TXRDY(channel); 215 } 216 217 DPRINTF2("cmd: "); 218 219 /* The rest of the bits implement different commands */ 220 switch (CR_CMD(val)) { 221 case CR_NO_OP: 222 DPRINTF2("none"); 223 break; 224 case CR_RESET_MR: 225 DPRINTF2("reset MR"); 226 ch->mr_idx = 0; 227 break; 228 case CR_RESET_RX: 229 DPRINTF2("reset Rx"); 230 ch->rx_enabled = false; 231 ch->rx_pending = 0; 232 ch->sr &= ~SR_RXRDY; 233 blk->isr &= ~ISR_RXRDY(channel); 234 break; 235 case CR_RESET_TX: 236 DPRINTF2("reset Tx"); 237 ch->sr &= ~(SR_TXRDY | SR_TXEMT); 238 blk->isr &= ~ISR_TXRDY(channel); 239 break; 240 case CR_RESET_ERR: 241 DPRINTF2("reset err"); 242 ch->sr &= ~(SR_OVERRUN | SR_PARITY | SR_FRAMING | SR_BREAK); 243 break; 244 case CR_RESET_BRKINT: 245 DPRINTF2("reset brk ch int"); 246 blk->isr &= ~(ISR_BREAKA | ISR_BREAKB); 247 break; 248 default: 249 DPRINTF2("unsupported 0x%x", CR_CMD(val)); 250 } 251 252 DPRINTF2("\n"); 253 } 254 255 static uint16_t io_read(IPackDevice *ip, uint8_t addr) 256 { 257 IPOctalState *dev = IPOCTAL(ip); 258 uint16_t ret = 0; 259 /* addr[7:6]: block (A-D) 260 addr[7:5]: channel (a-h) 261 addr[5:0]: register */ 262 unsigned block = addr >> 5; 263 unsigned channel = addr >> 4; 264 /* Big endian, accessed using 8-bit bytes at odd locations */ 265 unsigned offset = (addr & 0x1F) ^ 1; 266 SCC2698Channel *ch = &dev->ch[channel]; 267 SCC2698Block *blk = &dev->blk[block]; 268 uint8_t old_isr = blk->isr; 269 270 switch (offset) { 271 272 case REG_MRa: 273 case REG_MRb: 274 ret = ch->mr[ch->mr_idx]; 275 DPRINTF("Read MR%u%c: 0x%x\n", ch->mr_idx + 1, channel + 'a', ret); 276 ch->mr_idx = 1; 277 break; 278 279 case REG_SRa: 280 case REG_SRb: 281 ret = ch->sr; 282 DPRINTF("Read SR%c: 0x%x\n", channel + 'a', ret); 283 break; 284 285 case REG_RHRa: 286 case REG_RHRb: 287 ret = ch->rhr[ch->rhr_idx]; 288 if (ch->rx_pending > 0) { 289 ch->rx_pending--; 290 if (ch->rx_pending == 0) { 291 ch->sr &= ~SR_RXRDY; 292 blk->isr &= ~ISR_RXRDY(channel); 293 qemu_chr_fe_accept_input(&ch->dev); 294 } else { 295 ch->rhr_idx = (ch->rhr_idx + 1) % RX_FIFO_SIZE; 296 } 297 if (ch->sr & SR_BREAK) { 298 ch->sr &= ~SR_BREAK; 299 blk->isr |= ISR_BREAK(channel); 300 } 301 } 302 DPRINTF("Read RHR%c (0x%x)\n", channel + 'a', ret); 303 break; 304 305 case REG_ISR: 306 ret = blk->isr; 307 DPRINTF("Read ISR%c: 0x%x\n", block + 'A', ret); 308 break; 309 310 default: 311 DPRINTF("Read unknown/unsupported register 0x%02x\n", offset); 312 } 313 314 if (old_isr != blk->isr) { 315 update_irq(dev, block); 316 } 317 318 return ret; 319 } 320 321 static void io_write(IPackDevice *ip, uint8_t addr, uint16_t val) 322 { 323 IPOctalState *dev = IPOCTAL(ip); 324 unsigned reg = val & 0xFF; 325 /* addr[7:6]: block (A-D) 326 addr[7:5]: channel (a-h) 327 addr[5:0]: register */ 328 unsigned block = addr >> 5; 329 unsigned channel = addr >> 4; 330 /* Big endian, accessed using 8-bit bytes at odd locations */ 331 unsigned offset = (addr & 0x1F) ^ 1; 332 SCC2698Channel *ch = &dev->ch[channel]; 333 SCC2698Block *blk = &dev->blk[block]; 334 uint8_t old_isr = blk->isr; 335 uint8_t old_imr = blk->imr; 336 337 switch (offset) { 338 339 case REG_MRa: 340 case REG_MRb: 341 ch->mr[ch->mr_idx] = reg; 342 DPRINTF("Write MR%u%c 0x%x\n", ch->mr_idx + 1, channel + 'a', reg); 343 ch->mr_idx = 1; 344 break; 345 346 /* Not implemented */ 347 case REG_CSRa: 348 case REG_CSRb: 349 DPRINTF("Write CSR%c: 0x%x\n", channel + 'a', reg); 350 break; 351 352 case REG_CRa: 353 case REG_CRb: 354 write_cr(dev, channel, reg); 355 break; 356 357 case REG_THRa: 358 case REG_THRb: 359 if (ch->sr & SR_TXRDY) { 360 uint8_t thr = reg; 361 DPRINTF("Write THR%c (0x%x)\n", channel + 'a', reg); 362 /* XXX this blocks entire thread. Rewrite to use 363 * qemu_chr_fe_write and background I/O callbacks */ 364 qemu_chr_fe_write_all(&ch->dev, &thr, 1); 365 } else { 366 DPRINTF("Write THR%c (0x%x), Tx disabled\n", channel + 'a', reg); 367 } 368 break; 369 370 /* Not implemented */ 371 case REG_ACR: 372 DPRINTF("Write ACR%c 0x%x\n", block + 'A', val); 373 break; 374 375 case REG_IMR: 376 DPRINTF("Write IMR%c 0x%x\n", block + 'A', val); 377 blk->imr = reg; 378 break; 379 380 /* Not implemented */ 381 case REG_OPCR: 382 DPRINTF("Write OPCR%c 0x%x\n", block + 'A', val); 383 break; 384 385 default: 386 DPRINTF("Write unknown/unsupported register 0x%02x %u\n", offset, val); 387 } 388 389 if (old_isr != blk->isr || old_imr != blk->imr) { 390 update_irq(dev, block); 391 } 392 } 393 394 static uint16_t id_read(IPackDevice *ip, uint8_t addr) 395 { 396 uint16_t ret = 0; 397 unsigned pos = addr / 2; /* The ID PROM data is stored every other byte */ 398 399 if (pos < ARRAY_SIZE(id_prom_data)) { 400 ret = id_prom_data[pos]; 401 } else { 402 DPRINTF("Attempt to read unavailable PROM data at 0x%x\n", addr); 403 } 404 405 return ret; 406 } 407 408 static void id_write(IPackDevice *ip, uint8_t addr, uint16_t val) 409 { 410 IPOctalState *dev = IPOCTAL(ip); 411 if (addr == 1) { 412 DPRINTF("Write IRQ vector: %u\n", (unsigned) val); 413 dev->irq_vector = val; /* Undocumented, but the hw works like that */ 414 } else { 415 DPRINTF("Attempt to write 0x%x to 0x%x\n", val, addr); 416 } 417 } 418 419 static uint16_t int_read(IPackDevice *ip, uint8_t addr) 420 { 421 IPOctalState *dev = IPOCTAL(ip); 422 /* Read address 0 to ACK INT0# and address 2 to ACK INT1# */ 423 if (addr != 0 && addr != 2) { 424 DPRINTF("Attempt to read from 0x%x\n", addr); 425 return 0; 426 } else { 427 /* Update interrupts if necessary */ 428 update_irq(dev, addr); 429 return dev->irq_vector; 430 } 431 } 432 433 static void int_write(IPackDevice *ip, uint8_t addr, uint16_t val) 434 { 435 DPRINTF("Attempt to write 0x%x to 0x%x\n", val, addr); 436 } 437 438 static uint16_t mem_read16(IPackDevice *ip, uint32_t addr) 439 { 440 DPRINTF("Attempt to read from 0x%x\n", addr); 441 return 0; 442 } 443 444 static void mem_write16(IPackDevice *ip, uint32_t addr, uint16_t val) 445 { 446 DPRINTF("Attempt to write 0x%x to 0x%x\n", val, addr); 447 } 448 449 static uint8_t mem_read8(IPackDevice *ip, uint32_t addr) 450 { 451 DPRINTF("Attempt to read from 0x%x\n", addr); 452 return 0; 453 } 454 455 static void mem_write8(IPackDevice *ip, uint32_t addr, uint8_t val) 456 { 457 IPOctalState *dev = IPOCTAL(ip); 458 if (addr == 1) { 459 DPRINTF("Write IRQ vector: %u\n", (unsigned) val); 460 dev->irq_vector = val; 461 } else { 462 DPRINTF("Attempt to write 0x%x to 0x%x\n", val, addr); 463 } 464 } 465 466 static int hostdev_can_receive(void *opaque) 467 { 468 SCC2698Channel *ch = opaque; 469 int available_bytes = RX_FIFO_SIZE - ch->rx_pending; 470 return ch->rx_enabled ? available_bytes : 0; 471 } 472 473 static void hostdev_receive(void *opaque, const uint8_t *buf, int size) 474 { 475 SCC2698Channel *ch = opaque; 476 IPOctalState *dev = ch->ipoctal; 477 unsigned pos = ch->rhr_idx + ch->rx_pending; 478 int i; 479 480 assert(size + ch->rx_pending <= RX_FIFO_SIZE); 481 482 /* Copy data to the RxFIFO */ 483 for (i = 0; i < size; i++) { 484 pos %= RX_FIFO_SIZE; 485 ch->rhr[pos++] = buf[i]; 486 } 487 488 ch->rx_pending += size; 489 490 /* If the RxFIFO was empty raise an interrupt */ 491 if (!(ch->sr & SR_RXRDY)) { 492 unsigned block, channel = 0; 493 /* Find channel number to update the ISR register */ 494 while (&dev->ch[channel] != ch) { 495 channel++; 496 } 497 block = channel / 2; 498 dev->blk[block].isr |= ISR_RXRDY(channel); 499 ch->sr |= SR_RXRDY; 500 update_irq(dev, block); 501 } 502 } 503 504 static void hostdev_event(void *opaque, int event) 505 { 506 SCC2698Channel *ch = opaque; 507 switch (event) { 508 case CHR_EVENT_OPENED: 509 DPRINTF("Device %s opened\n", ch->dev->label); 510 break; 511 case CHR_EVENT_BREAK: { 512 uint8_t zero = 0; 513 DPRINTF("Device %s received break\n", ch->dev->label); 514 515 if (!(ch->sr & SR_BREAK)) { 516 IPOctalState *dev = ch->ipoctal; 517 unsigned block, channel = 0; 518 519 while (&dev->ch[channel] != ch) { 520 channel++; 521 } 522 block = channel / 2; 523 524 ch->sr |= SR_BREAK; 525 dev->blk[block].isr |= ISR_BREAK(channel); 526 } 527 528 /* Put a zero character in the buffer */ 529 hostdev_receive(ch, &zero, 1); 530 } 531 break; 532 default: 533 DPRINTF("Device %s received event %d\n", ch->dev->label, event); 534 } 535 } 536 537 static void ipoctal_realize(DeviceState *dev, Error **errp) 538 { 539 IPOctalState *s = IPOCTAL(dev); 540 unsigned i; 541 542 for (i = 0; i < N_CHANNELS; i++) { 543 SCC2698Channel *ch = &s->ch[i]; 544 ch->ipoctal = s; 545 546 /* Redirect IP-Octal channels to host character devices */ 547 if (qemu_chr_fe_backend_connected(&ch->dev)) { 548 qemu_chr_fe_set_handlers(&ch->dev, hostdev_can_receive, 549 hostdev_receive, hostdev_event, 550 NULL, ch, NULL, true); 551 DPRINTF("Redirecting channel %u to %s\n", i, ch->dev->label); 552 } else { 553 DPRINTF("Could not redirect channel %u, no chardev set\n", i); 554 } 555 } 556 } 557 558 static Property ipoctal_properties[] = { 559 DEFINE_PROP_CHR("chardev0", IPOctalState, ch[0].dev), 560 DEFINE_PROP_CHR("chardev1", IPOctalState, ch[1].dev), 561 DEFINE_PROP_CHR("chardev2", IPOctalState, ch[2].dev), 562 DEFINE_PROP_CHR("chardev3", IPOctalState, ch[3].dev), 563 DEFINE_PROP_CHR("chardev4", IPOctalState, ch[4].dev), 564 DEFINE_PROP_CHR("chardev5", IPOctalState, ch[5].dev), 565 DEFINE_PROP_CHR("chardev6", IPOctalState, ch[6].dev), 566 DEFINE_PROP_CHR("chardev7", IPOctalState, ch[7].dev), 567 DEFINE_PROP_END_OF_LIST(), 568 }; 569 570 static void ipoctal_class_init(ObjectClass *klass, void *data) 571 { 572 DeviceClass *dc = DEVICE_CLASS(klass); 573 IPackDeviceClass *ic = IPACK_DEVICE_CLASS(klass); 574 575 ic->realize = ipoctal_realize; 576 ic->io_read = io_read; 577 ic->io_write = io_write; 578 ic->id_read = id_read; 579 ic->id_write = id_write; 580 ic->int_read = int_read; 581 ic->int_write = int_write; 582 ic->mem_read16 = mem_read16; 583 ic->mem_write16 = mem_write16; 584 ic->mem_read8 = mem_read8; 585 ic->mem_write8 = mem_write8; 586 587 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 588 dc->desc = "GE IP-Octal 232 8-channel RS-232 IndustryPack"; 589 dc->props = ipoctal_properties; 590 dc->vmsd = &vmstate_ipoctal; 591 } 592 593 static const TypeInfo ipoctal_info = { 594 .name = TYPE_IPOCTAL, 595 .parent = TYPE_IPACK_DEVICE, 596 .instance_size = sizeof(IPOctalState), 597 .class_init = ipoctal_class_init, 598 }; 599 600 static void ipoctal_register_types(void) 601 { 602 type_register_static(&ipoctal_info); 603 } 604 605 type_init(ipoctal_register_types) 606