2 * QEMU GE IP-Octal 232 IndustryPack emulation
4 * Copyright (C) 2012 Igalia, S.L.
5 * Author: Alberto Garcia <berto@igalia.com>
7 * This code is licensed under the GNU GPL v2 or (at your option) any
11 #include "qemu/osdep.h"
12 #include "hw/ipack/ipack.h"
14 #include "hw/qdev-properties.h"
15 #include "hw/qdev-properties-system.h"
16 #include "migration/vmstate.h"
17 #include "qemu/bitops.h"
18 #include "qemu/module.h"
19 #include "chardev/char-fe.h"
20 #include "qom/object.h"
22 /* #define DEBUG_IPOCTAL */
25 #define DPRINTF2(fmt, ...) \
26 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
28 #define DPRINTF2(fmt, ...) do { } while (0)
31 #define DPRINTF(fmt, ...) DPRINTF2("IP-Octal: " fmt, ## __VA_ARGS__)
33 #define RX_FIFO_SIZE 3
35 /* The IP-Octal has 8 channels (a-h)
36 divided into 4 blocks (A-D) */
57 #define CR_ENABLE_RX BIT(0)
58 #define CR_DISABLE_RX BIT(1)
59 #define CR_ENABLE_TX BIT(2)
60 #define CR_DISABLE_TX BIT(3)
61 #define CR_CMD(cr) ((cr) >> 4)
66 #define CR_RESET_ERR 4
67 #define CR_RESET_BRKINT 5
68 #define CR_START_BRK 6
70 #define CR_ASSERT_RTSN 8
71 #define CR_NEGATE_RTSN 9
72 #define CR_TIMEOUT_ON 10
73 #define CR_TIMEOUT_OFF 12
75 #define SR_RXRDY BIT(0)
76 #define SR_FFULL BIT(1)
77 #define SR_TXRDY BIT(2)
78 #define SR_TXEMT BIT(3)
79 #define SR_OVERRUN BIT(4)
80 #define SR_PARITY BIT(5)
81 #define SR_FRAMING BIT(6)
82 #define SR_BREAK BIT(7)
84 #define ISR_TXRDYA BIT(0)
85 #define ISR_RXRDYA BIT(1)
86 #define ISR_BREAKA BIT(2)
87 #define ISR_CNTRDY BIT(3)
88 #define ISR_TXRDYB BIT(4)
89 #define ISR_RXRDYB BIT(5)
90 #define ISR_BREAKB BIT(6)
91 #define ISR_MPICHG BIT(7)
92 #define ISR_TXRDY(CH) (((CH) & 1) ? BIT(4) : BIT(0))
93 #define ISR_RXRDY(CH) (((CH) & 1) ? BIT(5) : BIT(1))
94 #define ISR_BREAK(CH) (((CH) & 1) ? BIT(6) : BIT(2))
96 typedef struct IPOctalState IPOctalState
;
97 typedef struct SCC2698Channel SCC2698Channel
;
98 typedef struct SCC2698Block SCC2698Block
;
100 struct SCC2698Channel
{
101 IPOctalState
*ipoctal
;
107 uint8_t rhr
[RX_FIFO_SIZE
];
112 struct SCC2698Block
{
117 struct IPOctalState
{
118 IPackDevice parent_obj
;
120 SCC2698Channel ch
[N_CHANNELS
];
121 SCC2698Block blk
[N_BLOCKS
];
125 #define TYPE_IPOCTAL "ipoctal232"
127 OBJECT_DECLARE_SIMPLE_TYPE(IPOctalState
, IPOCTAL
)
129 static const VMStateDescription vmstate_scc2698_channel
= {
130 .name
= "scc2698_channel",
132 .minimum_version_id
= 1,
133 .fields
= (VMStateField
[]) {
134 VMSTATE_BOOL(rx_enabled
, SCC2698Channel
),
135 VMSTATE_UINT8_ARRAY(mr
, SCC2698Channel
, 2),
136 VMSTATE_UINT8(mr_idx
, SCC2698Channel
),
137 VMSTATE_UINT8(sr
, SCC2698Channel
),
138 VMSTATE_UINT8_ARRAY(rhr
, SCC2698Channel
, RX_FIFO_SIZE
),
139 VMSTATE_UINT8(rhr_idx
, SCC2698Channel
),
140 VMSTATE_UINT8(rx_pending
, SCC2698Channel
),
141 VMSTATE_END_OF_LIST()
145 static const VMStateDescription vmstate_scc2698_block
= {
146 .name
= "scc2698_block",
148 .minimum_version_id
= 1,
149 .fields
= (VMStateField
[]) {
150 VMSTATE_UINT8(imr
, SCC2698Block
),
151 VMSTATE_UINT8(isr
, SCC2698Block
),
152 VMSTATE_END_OF_LIST()
156 static const VMStateDescription vmstate_ipoctal
= {
157 .name
= "ipoctal232",
159 .minimum_version_id
= 1,
160 .fields
= (VMStateField
[]) {
161 VMSTATE_IPACK_DEVICE(parent_obj
, IPOctalState
),
162 VMSTATE_STRUCT_ARRAY(ch
, IPOctalState
, N_CHANNELS
, 1,
163 vmstate_scc2698_channel
, SCC2698Channel
),
164 VMSTATE_STRUCT_ARRAY(blk
, IPOctalState
, N_BLOCKS
, 1,
165 vmstate_scc2698_block
, SCC2698Block
),
166 VMSTATE_UINT8(irq_vector
, IPOctalState
),
167 VMSTATE_END_OF_LIST()
171 /* data[10] is 0x0C, not 0x0B as the doc says */
172 static const uint8_t id_prom_data
[] = {
173 0x49, 0x50, 0x41, 0x43, 0xF0, 0x22,
174 0xA1, 0x00, 0x00, 0x00, 0x0C, 0xCC
177 static void update_irq(IPOctalState
*dev
, unsigned block
)
179 IPackDevice
*idev
= IPACK_DEVICE(dev
);
180 /* Blocks A and B interrupt on INT0#, C and D on INT1#.
181 Thus, to get the status we have to check two blocks. */
182 SCC2698Block
*blk0
= &dev
->blk
[block
];
183 SCC2698Block
*blk1
= &dev
->blk
[block
^1];
184 unsigned intno
= block
/ 2;
186 if ((blk0
->isr
& blk0
->imr
) || (blk1
->isr
& blk1
->imr
)) {
187 qemu_irq_raise(idev
->irq
[intno
]);
189 qemu_irq_lower(idev
->irq
[intno
]);
193 static void write_cr(IPOctalState
*dev
, unsigned channel
, uint8_t val
)
195 SCC2698Channel
*ch
= &dev
->ch
[channel
];
196 SCC2698Block
*blk
= &dev
->blk
[channel
/ 2];
198 DPRINTF("Write CR%c %u: ", channel
+ 'a', val
);
200 /* The lower 4 bits are used to enable and disable Tx and Rx */
201 if (val
& CR_ENABLE_RX
) {
203 ch
->rx_enabled
= true;
205 if (val
& CR_DISABLE_RX
) {
206 DPRINTF2("Rx off, ");
207 ch
->rx_enabled
= false;
209 if (val
& CR_ENABLE_TX
) {
211 ch
->sr
|= SR_TXRDY
| SR_TXEMT
;
212 blk
->isr
|= ISR_TXRDY(channel
);
214 if (val
& CR_DISABLE_TX
) {
215 DPRINTF2("Tx off, ");
216 ch
->sr
&= ~(SR_TXRDY
| SR_TXEMT
);
217 blk
->isr
&= ~ISR_TXRDY(channel
);
222 /* The rest of the bits implement different commands */
223 switch (CR_CMD(val
)) {
228 DPRINTF2("reset MR");
232 DPRINTF2("reset Rx");
233 ch
->rx_enabled
= false;
236 blk
->isr
&= ~ISR_RXRDY(channel
);
239 DPRINTF2("reset Tx");
240 ch
->sr
&= ~(SR_TXRDY
| SR_TXEMT
);
241 blk
->isr
&= ~ISR_TXRDY(channel
);
244 DPRINTF2("reset err");
245 ch
->sr
&= ~(SR_OVERRUN
| SR_PARITY
| SR_FRAMING
| SR_BREAK
);
247 case CR_RESET_BRKINT
:
248 DPRINTF2("reset brk ch int");
249 blk
->isr
&= ~(ISR_BREAKA
| ISR_BREAKB
);
252 DPRINTF2("unsupported 0x%x", CR_CMD(val
));
258 static uint16_t io_read(IPackDevice
*ip
, uint8_t addr
)
260 IPOctalState
*dev
= IPOCTAL(ip
);
262 /* addr[7:6]: block (A-D)
263 addr[7:5]: channel (a-h)
264 addr[5:0]: register */
265 unsigned block
= addr
>> 5;
266 unsigned channel
= addr
>> 4;
267 /* Big endian, accessed using 8-bit bytes at odd locations */
268 unsigned offset
= (addr
& 0x1F) ^ 1;
269 SCC2698Channel
*ch
= &dev
->ch
[channel
];
270 SCC2698Block
*blk
= &dev
->blk
[block
];
271 uint8_t old_isr
= blk
->isr
;
277 ret
= ch
->mr
[ch
->mr_idx
];
278 DPRINTF("Read MR%u%c: 0x%x\n", ch
->mr_idx
+ 1, channel
+ 'a', ret
);
285 DPRINTF("Read SR%c: 0x%x\n", channel
+ 'a', ret
);
290 ret
= ch
->rhr
[ch
->rhr_idx
];
291 if (ch
->rx_pending
> 0) {
293 if (ch
->rx_pending
== 0) {
295 blk
->isr
&= ~ISR_RXRDY(channel
);
296 qemu_chr_fe_accept_input(&ch
->dev
);
298 ch
->rhr_idx
= (ch
->rhr_idx
+ 1) % RX_FIFO_SIZE
;
300 if (ch
->sr
& SR_BREAK
) {
302 blk
->isr
|= ISR_BREAK(channel
);
305 DPRINTF("Read RHR%c (0x%x)\n", channel
+ 'a', ret
);
310 DPRINTF("Read ISR%c: 0x%x\n", block
+ 'A', ret
);
314 DPRINTF("Read unknown/unsupported register 0x%02x\n", offset
);
317 if (old_isr
!= blk
->isr
) {
318 update_irq(dev
, block
);
324 static void io_write(IPackDevice
*ip
, uint8_t addr
, uint16_t val
)
326 IPOctalState
*dev
= IPOCTAL(ip
);
327 unsigned reg
= val
& 0xFF;
328 /* addr[7:6]: block (A-D)
329 addr[7:5]: channel (a-h)
330 addr[5:0]: register */
331 unsigned block
= addr
>> 5;
332 unsigned channel
= addr
>> 4;
333 /* Big endian, accessed using 8-bit bytes at odd locations */
334 unsigned offset
= (addr
& 0x1F) ^ 1;
335 SCC2698Channel
*ch
= &dev
->ch
[channel
];
336 SCC2698Block
*blk
= &dev
->blk
[block
];
337 uint8_t old_isr
= blk
->isr
;
338 uint8_t old_imr
= blk
->imr
;
344 ch
->mr
[ch
->mr_idx
] = reg
;
345 DPRINTF("Write MR%u%c 0x%x\n", ch
->mr_idx
+ 1, channel
+ 'a', reg
);
349 /* Not implemented */
352 DPRINTF("Write CSR%c: 0x%x\n", channel
+ 'a', reg
);
357 write_cr(dev
, channel
, reg
);
362 if (ch
->sr
& SR_TXRDY
) {
364 DPRINTF("Write THR%c (0x%x)\n", channel
+ 'a', reg
);
365 /* XXX this blocks entire thread. Rewrite to use
366 * qemu_chr_fe_write and background I/O callbacks */
367 qemu_chr_fe_write_all(&ch
->dev
, &thr
, 1);
369 DPRINTF("Write THR%c (0x%x), Tx disabled\n", channel
+ 'a', reg
);
373 /* Not implemented */
375 DPRINTF("Write ACR%c 0x%x\n", block
+ 'A', val
);
379 DPRINTF("Write IMR%c 0x%x\n", block
+ 'A', val
);
383 /* Not implemented */
385 DPRINTF("Write OPCR%c 0x%x\n", block
+ 'A', val
);
389 DPRINTF("Write unknown/unsupported register 0x%02x %u\n", offset
, val
);
392 if (old_isr
!= blk
->isr
|| old_imr
!= blk
->imr
) {
393 update_irq(dev
, block
);
397 static uint16_t id_read(IPackDevice
*ip
, uint8_t addr
)
400 unsigned pos
= addr
/ 2; /* The ID PROM data is stored every other byte */
402 if (pos
< ARRAY_SIZE(id_prom_data
)) {
403 ret
= id_prom_data
[pos
];
405 DPRINTF("Attempt to read unavailable PROM data at 0x%x\n", addr
);
411 static void id_write(IPackDevice
*ip
, uint8_t addr
, uint16_t val
)
413 IPOctalState
*dev
= IPOCTAL(ip
);
415 DPRINTF("Write IRQ vector: %u\n", (unsigned) val
);
416 dev
->irq_vector
= val
; /* Undocumented, but the hw works like that */
418 DPRINTF("Attempt to write 0x%x to 0x%x\n", val
, addr
);
422 static uint16_t int_read(IPackDevice
*ip
, uint8_t addr
)
424 IPOctalState
*dev
= IPOCTAL(ip
);
425 /* Read address 0 to ACK INT0# and address 2 to ACK INT1# */
426 if (addr
!= 0 && addr
!= 2) {
427 DPRINTF("Attempt to read from 0x%x\n", addr
);
430 /* Update interrupts if necessary */
431 update_irq(dev
, addr
);
432 return dev
->irq_vector
;
436 static void int_write(IPackDevice
*ip
, uint8_t addr
, uint16_t val
)
438 DPRINTF("Attempt to write 0x%x to 0x%x\n", val
, addr
);
441 static uint16_t mem_read16(IPackDevice
*ip
, uint32_t addr
)
443 DPRINTF("Attempt to read from 0x%x\n", addr
);
447 static void mem_write16(IPackDevice
*ip
, uint32_t addr
, uint16_t val
)
449 DPRINTF("Attempt to write 0x%x to 0x%x\n", val
, addr
);
452 static uint8_t mem_read8(IPackDevice
*ip
, uint32_t addr
)
454 DPRINTF("Attempt to read from 0x%x\n", addr
);
458 static void mem_write8(IPackDevice
*ip
, uint32_t addr
, uint8_t val
)
460 IPOctalState
*dev
= IPOCTAL(ip
);
462 DPRINTF("Write IRQ vector: %u\n", (unsigned) val
);
463 dev
->irq_vector
= val
;
465 DPRINTF("Attempt to write 0x%x to 0x%x\n", val
, addr
);
469 static int hostdev_can_receive(void *opaque
)
471 SCC2698Channel
*ch
= opaque
;
472 int available_bytes
= RX_FIFO_SIZE
- ch
->rx_pending
;
473 return ch
->rx_enabled
? available_bytes
: 0;
476 static void hostdev_receive(void *opaque
, const uint8_t *buf
, int size
)
478 SCC2698Channel
*ch
= opaque
;
479 IPOctalState
*dev
= ch
->ipoctal
;
480 unsigned pos
= ch
->rhr_idx
+ ch
->rx_pending
;
483 assert(size
+ ch
->rx_pending
<= RX_FIFO_SIZE
);
485 /* Copy data to the RxFIFO */
486 for (i
= 0; i
< size
; i
++) {
488 ch
->rhr
[pos
++] = buf
[i
];
491 ch
->rx_pending
+= size
;
493 /* If the RxFIFO was empty raise an interrupt */
494 if (!(ch
->sr
& SR_RXRDY
)) {
495 unsigned block
, channel
= 0;
496 /* Find channel number to update the ISR register */
497 while (&dev
->ch
[channel
] != ch
) {
501 dev
->blk
[block
].isr
|= ISR_RXRDY(channel
);
503 update_irq(dev
, block
);
507 static void hostdev_event(void *opaque
, QEMUChrEvent event
)
509 SCC2698Channel
*ch
= opaque
;
511 case CHR_EVENT_OPENED
:
512 DPRINTF("Device %s opened\n", ch
->dev
->label
);
514 case CHR_EVENT_BREAK
: {
516 DPRINTF("Device %s received break\n", ch
->dev
->label
);
518 if (!(ch
->sr
& SR_BREAK
)) {
519 IPOctalState
*dev
= ch
->ipoctal
;
520 unsigned block
, channel
= 0;
522 while (&dev
->ch
[channel
] != ch
) {
528 dev
->blk
[block
].isr
|= ISR_BREAK(channel
);
531 /* Put a zero character in the buffer */
532 hostdev_receive(ch
, &zero
, 1);
536 DPRINTF("Device %s received event %d\n", ch
->dev
->label
, event
);
540 static void ipoctal_realize(DeviceState
*dev
, Error
**errp
)
542 IPOctalState
*s
= IPOCTAL(dev
);
545 for (i
= 0; i
< N_CHANNELS
; i
++) {
546 SCC2698Channel
*ch
= &s
->ch
[i
];
549 /* Redirect IP-Octal channels to host character devices */
550 if (qemu_chr_fe_backend_connected(&ch
->dev
)) {
551 qemu_chr_fe_set_handlers(&ch
->dev
, hostdev_can_receive
,
552 hostdev_receive
, hostdev_event
,
553 NULL
, ch
, NULL
, true);
554 DPRINTF("Redirecting channel %u to %s\n", i
, ch
->dev
->label
);
556 DPRINTF("Could not redirect channel %u, no chardev set\n", i
);
561 static Property ipoctal_properties
[] = {
562 DEFINE_PROP_CHR("chardev0", IPOctalState
, ch
[0].dev
),
563 DEFINE_PROP_CHR("chardev1", IPOctalState
, ch
[1].dev
),
564 DEFINE_PROP_CHR("chardev2", IPOctalState
, ch
[2].dev
),
565 DEFINE_PROP_CHR("chardev3", IPOctalState
, ch
[3].dev
),
566 DEFINE_PROP_CHR("chardev4", IPOctalState
, ch
[4].dev
),
567 DEFINE_PROP_CHR("chardev5", IPOctalState
, ch
[5].dev
),
568 DEFINE_PROP_CHR("chardev6", IPOctalState
, ch
[6].dev
),
569 DEFINE_PROP_CHR("chardev7", IPOctalState
, ch
[7].dev
),
570 DEFINE_PROP_END_OF_LIST(),
573 static void ipoctal_class_init(ObjectClass
*klass
, void *data
)
575 DeviceClass
*dc
= DEVICE_CLASS(klass
);
576 IPackDeviceClass
*ic
= IPACK_DEVICE_CLASS(klass
);
578 ic
->realize
= ipoctal_realize
;
579 ic
->io_read
= io_read
;
580 ic
->io_write
= io_write
;
581 ic
->id_read
= id_read
;
582 ic
->id_write
= id_write
;
583 ic
->int_read
= int_read
;
584 ic
->int_write
= int_write
;
585 ic
->mem_read16
= mem_read16
;
586 ic
->mem_write16
= mem_write16
;
587 ic
->mem_read8
= mem_read8
;
588 ic
->mem_write8
= mem_write8
;
590 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
591 dc
->desc
= "GE IP-Octal 232 8-channel RS-232 IndustryPack";
592 device_class_set_props(dc
, ipoctal_properties
);
593 dc
->vmsd
= &vmstate_ipoctal
;
596 static const TypeInfo ipoctal_info
= {
597 .name
= TYPE_IPOCTAL
,
598 .parent
= TYPE_IPACK_DEVICE
,
599 .instance_size
= sizeof(IPOctalState
),
600 .class_init
= ipoctal_class_init
,
603 static void ipoctal_register_types(void)
605 type_register_static(&ipoctal_info
);
608 type_init(ipoctal_register_types
)