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"
13 #include "qemu/bitops.h"
14 #include "sysemu/char.h"
16 /* #define DEBUG_IPOCTAL */
19 #define DPRINTF2(fmt, ...) \
20 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
22 #define DPRINTF2(fmt, ...) do { } while (0)
25 #define DPRINTF(fmt, ...) DPRINTF2("IP-Octal: " fmt, ## __VA_ARGS__)
27 #define RX_FIFO_SIZE 3
29 /* The IP-Octal has 8 channels (a-h)
30 divided into 4 blocks (A-D) */
51 #define CR_ENABLE_RX BIT(0)
52 #define CR_DISABLE_RX BIT(1)
53 #define CR_ENABLE_TX BIT(2)
54 #define CR_DISABLE_TX BIT(3)
55 #define CR_CMD(cr) ((cr) >> 4)
60 #define CR_RESET_ERR 4
61 #define CR_RESET_BRKINT 5
62 #define CR_START_BRK 6
64 #define CR_ASSERT_RTSN 8
65 #define CR_NEGATE_RTSN 9
66 #define CR_TIMEOUT_ON 10
67 #define CR_TIMEOUT_OFF 12
69 #define SR_RXRDY BIT(0)
70 #define SR_FFULL BIT(1)
71 #define SR_TXRDY BIT(2)
72 #define SR_TXEMT BIT(3)
73 #define SR_OVERRUN BIT(4)
74 #define SR_PARITY BIT(5)
75 #define SR_FRAMING BIT(6)
76 #define SR_BREAK BIT(7)
78 #define ISR_TXRDYA BIT(0)
79 #define ISR_RXRDYA BIT(1)
80 #define ISR_BREAKA BIT(2)
81 #define ISR_CNTRDY BIT(3)
82 #define ISR_TXRDYB BIT(4)
83 #define ISR_RXRDYB BIT(5)
84 #define ISR_BREAKB BIT(6)
85 #define ISR_MPICHG BIT(7)
86 #define ISR_TXRDY(CH) (((CH) & 1) ? BIT(4) : BIT(0))
87 #define ISR_RXRDY(CH) (((CH) & 1) ? BIT(5) : BIT(1))
88 #define ISR_BREAK(CH) (((CH) & 1) ? BIT(6) : BIT(2))
90 typedef struct IPOctalState IPOctalState
;
91 typedef struct SCC2698Channel SCC2698Channel
;
92 typedef struct SCC2698Block SCC2698Block
;
94 struct SCC2698Channel
{
95 IPOctalState
*ipoctal
;
101 uint8_t rhr
[RX_FIFO_SIZE
];
106 struct SCC2698Block
{
111 struct IPOctalState
{
112 IPackDevice parent_obj
;
114 SCC2698Channel ch
[N_CHANNELS
];
115 SCC2698Block blk
[N_BLOCKS
];
119 #define TYPE_IPOCTAL "ipoctal232"
121 #define IPOCTAL(obj) \
122 OBJECT_CHECK(IPOctalState, (obj), TYPE_IPOCTAL)
124 static const VMStateDescription vmstate_scc2698_channel
= {
125 .name
= "scc2698_channel",
127 .minimum_version_id
= 1,
128 .fields
= (VMStateField
[]) {
129 VMSTATE_BOOL(rx_enabled
, SCC2698Channel
),
130 VMSTATE_UINT8_ARRAY(mr
, SCC2698Channel
, 2),
131 VMSTATE_UINT8(mr_idx
, SCC2698Channel
),
132 VMSTATE_UINT8(sr
, SCC2698Channel
),
133 VMSTATE_UINT8_ARRAY(rhr
, SCC2698Channel
, RX_FIFO_SIZE
),
134 VMSTATE_UINT8(rhr_idx
, SCC2698Channel
),
135 VMSTATE_UINT8(rx_pending
, SCC2698Channel
),
136 VMSTATE_END_OF_LIST()
140 static const VMStateDescription vmstate_scc2698_block
= {
141 .name
= "scc2698_block",
143 .minimum_version_id
= 1,
144 .fields
= (VMStateField
[]) {
145 VMSTATE_UINT8(imr
, SCC2698Block
),
146 VMSTATE_UINT8(isr
, SCC2698Block
),
147 VMSTATE_END_OF_LIST()
151 static const VMStateDescription vmstate_ipoctal
= {
152 .name
= "ipoctal232",
154 .minimum_version_id
= 1,
155 .fields
= (VMStateField
[]) {
156 VMSTATE_IPACK_DEVICE(parent_obj
, IPOctalState
),
157 VMSTATE_STRUCT_ARRAY(ch
, IPOctalState
, N_CHANNELS
, 1,
158 vmstate_scc2698_channel
, SCC2698Channel
),
159 VMSTATE_STRUCT_ARRAY(blk
, IPOctalState
, N_BLOCKS
, 1,
160 vmstate_scc2698_block
, SCC2698Block
),
161 VMSTATE_UINT8(irq_vector
, IPOctalState
),
162 VMSTATE_END_OF_LIST()
166 /* data[10] is 0x0C, not 0x0B as the doc says */
167 static const uint8_t id_prom_data
[] = {
168 0x49, 0x50, 0x41, 0x43, 0xF0, 0x22,
169 0xA1, 0x00, 0x00, 0x00, 0x0C, 0xCC
172 static void update_irq(IPOctalState
*dev
, unsigned block
)
174 IPackDevice
*idev
= IPACK_DEVICE(dev
);
175 /* Blocks A and B interrupt on INT0#, C and D on INT1#.
176 Thus, to get the status we have to check two blocks. */
177 SCC2698Block
*blk0
= &dev
->blk
[block
];
178 SCC2698Block
*blk1
= &dev
->blk
[block
^1];
179 unsigned intno
= block
/ 2;
181 if ((blk0
->isr
& blk0
->imr
) || (blk1
->isr
& blk1
->imr
)) {
182 qemu_irq_raise(idev
->irq
[intno
]);
184 qemu_irq_lower(idev
->irq
[intno
]);
188 static void write_cr(IPOctalState
*dev
, unsigned channel
, uint8_t val
)
190 SCC2698Channel
*ch
= &dev
->ch
[channel
];
191 SCC2698Block
*blk
= &dev
->blk
[channel
/ 2];
193 DPRINTF("Write CR%c %u: ", channel
+ 'a', val
);
195 /* The lower 4 bits are used to enable and disable Tx and Rx */
196 if (val
& CR_ENABLE_RX
) {
198 ch
->rx_enabled
= true;
200 if (val
& CR_DISABLE_RX
) {
201 DPRINTF2("Rx off, ");
202 ch
->rx_enabled
= false;
204 if (val
& CR_ENABLE_TX
) {
206 ch
->sr
|= SR_TXRDY
| SR_TXEMT
;
207 blk
->isr
|= ISR_TXRDY(channel
);
209 if (val
& CR_DISABLE_TX
) {
210 DPRINTF2("Tx off, ");
211 ch
->sr
&= ~(SR_TXRDY
| SR_TXEMT
);
212 blk
->isr
&= ~ISR_TXRDY(channel
);
217 /* The rest of the bits implement different commands */
218 switch (CR_CMD(val
)) {
223 DPRINTF2("reset MR");
227 DPRINTF2("reset Rx");
228 ch
->rx_enabled
= false;
231 blk
->isr
&= ~ISR_RXRDY(channel
);
234 DPRINTF2("reset Tx");
235 ch
->sr
&= ~(SR_TXRDY
| SR_TXEMT
);
236 blk
->isr
&= ~ISR_TXRDY(channel
);
239 DPRINTF2("reset err");
240 ch
->sr
&= ~(SR_OVERRUN
| SR_PARITY
| SR_FRAMING
| SR_BREAK
);
242 case CR_RESET_BRKINT
:
243 DPRINTF2("reset brk ch int");
244 blk
->isr
&= ~(ISR_BREAKA
| ISR_BREAKB
);
247 DPRINTF2("unsupported 0x%x", CR_CMD(val
));
253 static uint16_t io_read(IPackDevice
*ip
, uint8_t addr
)
255 IPOctalState
*dev
= IPOCTAL(ip
);
257 /* addr[7:6]: block (A-D)
258 addr[7:5]: channel (a-h)
259 addr[5:0]: register */
260 unsigned block
= addr
>> 5;
261 unsigned channel
= addr
>> 4;
262 /* Big endian, accessed using 8-bit bytes at odd locations */
263 unsigned offset
= (addr
& 0x1F) ^ 1;
264 SCC2698Channel
*ch
= &dev
->ch
[channel
];
265 SCC2698Block
*blk
= &dev
->blk
[block
];
266 uint8_t old_isr
= blk
->isr
;
272 ret
= ch
->mr
[ch
->mr_idx
];
273 DPRINTF("Read MR%u%c: 0x%x\n", ch
->mr_idx
+ 1, channel
+ 'a', ret
);
280 DPRINTF("Read SR%c: 0x%x\n", channel
+ 'a', ret
);
285 ret
= ch
->rhr
[ch
->rhr_idx
];
286 if (ch
->rx_pending
> 0) {
288 if (ch
->rx_pending
== 0) {
290 blk
->isr
&= ~ISR_RXRDY(channel
);
292 qemu_chr_accept_input(ch
->dev
);
295 ch
->rhr_idx
= (ch
->rhr_idx
+ 1) % RX_FIFO_SIZE
;
297 if (ch
->sr
& SR_BREAK
) {
299 blk
->isr
|= ISR_BREAK(channel
);
302 DPRINTF("Read RHR%c (0x%x)\n", channel
+ 'a', ret
);
307 DPRINTF("Read ISR%c: 0x%x\n", block
+ 'A', ret
);
311 DPRINTF("Read unknown/unsupported register 0x%02x\n", offset
);
314 if (old_isr
!= blk
->isr
) {
315 update_irq(dev
, block
);
321 static void io_write(IPackDevice
*ip
, uint8_t addr
, uint16_t val
)
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
;
341 ch
->mr
[ch
->mr_idx
] = reg
;
342 DPRINTF("Write MR%u%c 0x%x\n", ch
->mr_idx
+ 1, channel
+ 'a', reg
);
346 /* Not implemented */
349 DPRINTF("Write CSR%c: 0x%x\n", channel
+ 'a', reg
);
354 write_cr(dev
, channel
, reg
);
359 if (ch
->sr
& SR_TXRDY
) {
360 DPRINTF("Write THR%c (0x%x)\n", channel
+ 'a', reg
);
363 /* XXX this blocks entire thread. Rewrite to use
364 * qemu_chr_fe_write and background I/O callbacks */
365 qemu_chr_fe_write_all(ch
->dev
, &thr
, 1);
368 DPRINTF("Write THR%c (0x%x), Tx disabled\n", channel
+ 'a', reg
);
372 /* Not implemented */
374 DPRINTF("Write ACR%c 0x%x\n", block
+ 'A', val
);
378 DPRINTF("Write IMR%c 0x%x\n", block
+ 'A', val
);
382 /* Not implemented */
384 DPRINTF("Write OPCR%c 0x%x\n", block
+ 'A', val
);
388 DPRINTF("Write unknown/unsupported register 0x%02x %u\n", offset
, val
);
391 if (old_isr
!= blk
->isr
|| old_imr
!= blk
->imr
) {
392 update_irq(dev
, block
);
396 static uint16_t id_read(IPackDevice
*ip
, uint8_t addr
)
399 unsigned pos
= addr
/ 2; /* The ID PROM data is stored every other byte */
401 if (pos
< ARRAY_SIZE(id_prom_data
)) {
402 ret
= id_prom_data
[pos
];
404 DPRINTF("Attempt to read unavailable PROM data at 0x%x\n", addr
);
410 static void id_write(IPackDevice
*ip
, uint8_t addr
, uint16_t val
)
412 IPOctalState
*dev
= IPOCTAL(ip
);
414 DPRINTF("Write IRQ vector: %u\n", (unsigned) val
);
415 dev
->irq_vector
= val
; /* Undocumented, but the hw works like that */
417 DPRINTF("Attempt to write 0x%x to 0x%x\n", val
, addr
);
421 static uint16_t int_read(IPackDevice
*ip
, uint8_t addr
)
423 IPOctalState
*dev
= IPOCTAL(ip
);
424 /* Read address 0 to ACK INT0# and address 2 to ACK INT1# */
425 if (addr
!= 0 && addr
!= 2) {
426 DPRINTF("Attempt to read from 0x%x\n", addr
);
429 /* Update interrupts if necessary */
430 update_irq(dev
, addr
);
431 return dev
->irq_vector
;
435 static void int_write(IPackDevice
*ip
, uint8_t addr
, uint16_t val
)
437 DPRINTF("Attempt to write 0x%x to 0x%x\n", val
, addr
);
440 static uint16_t mem_read16(IPackDevice
*ip
, uint32_t addr
)
442 DPRINTF("Attempt to read from 0x%x\n", addr
);
446 static void mem_write16(IPackDevice
*ip
, uint32_t addr
, uint16_t val
)
448 DPRINTF("Attempt to write 0x%x to 0x%x\n", val
, addr
);
451 static uint8_t mem_read8(IPackDevice
*ip
, uint32_t addr
)
453 DPRINTF("Attempt to read from 0x%x\n", addr
);
457 static void mem_write8(IPackDevice
*ip
, uint32_t addr
, uint8_t val
)
459 IPOctalState
*dev
= IPOCTAL(ip
);
461 DPRINTF("Write IRQ vector: %u\n", (unsigned) val
);
462 dev
->irq_vector
= val
;
464 DPRINTF("Attempt to write 0x%x to 0x%x\n", val
, addr
);
468 static int hostdev_can_receive(void *opaque
)
470 SCC2698Channel
*ch
= opaque
;
471 int available_bytes
= RX_FIFO_SIZE
- ch
->rx_pending
;
472 return ch
->rx_enabled
? available_bytes
: 0;
475 static void hostdev_receive(void *opaque
, const uint8_t *buf
, int size
)
477 SCC2698Channel
*ch
= opaque
;
478 IPOctalState
*dev
= ch
->ipoctal
;
479 unsigned pos
= ch
->rhr_idx
+ ch
->rx_pending
;
482 assert(size
+ ch
->rx_pending
<= RX_FIFO_SIZE
);
484 /* Copy data to the RxFIFO */
485 for (i
= 0; i
< size
; i
++) {
487 ch
->rhr
[pos
++] = buf
[i
];
490 ch
->rx_pending
+= size
;
492 /* If the RxFIFO was empty raise an interrupt */
493 if (!(ch
->sr
& SR_RXRDY
)) {
494 unsigned block
, channel
= 0;
495 /* Find channel number to update the ISR register */
496 while (&dev
->ch
[channel
] != ch
) {
500 dev
->blk
[block
].isr
|= ISR_RXRDY(channel
);
502 update_irq(dev
, block
);
506 static void hostdev_event(void *opaque
, int event
)
508 SCC2698Channel
*ch
= opaque
;
510 case CHR_EVENT_OPENED
:
511 DPRINTF("Device %s opened\n", ch
->dev
->label
);
513 case CHR_EVENT_BREAK
: {
515 DPRINTF("Device %s received break\n", ch
->dev
->label
);
517 if (!(ch
->sr
& SR_BREAK
)) {
518 IPOctalState
*dev
= ch
->ipoctal
;
519 unsigned block
, channel
= 0;
521 while (&dev
->ch
[channel
] != ch
) {
527 dev
->blk
[block
].isr
|= ISR_BREAK(channel
);
530 /* Put a zero character in the buffer */
531 hostdev_receive(ch
, &zero
, 1);
535 DPRINTF("Device %s received event %d\n", ch
->dev
->label
, event
);
539 static void ipoctal_realize(DeviceState
*dev
, Error
**errp
)
541 IPOctalState
*s
= IPOCTAL(dev
);
544 for (i
= 0; i
< N_CHANNELS
; i
++) {
545 SCC2698Channel
*ch
= &s
->ch
[i
];
548 /* Redirect IP-Octal channels to host character devices */
550 qemu_chr_add_handlers(ch
->dev
, hostdev_can_receive
,
551 hostdev_receive
, hostdev_event
, ch
);
552 DPRINTF("Redirecting channel %u to %s\n", i
, ch
->dev
->label
);
554 DPRINTF("Could not redirect channel %u, no chardev set\n", i
);
559 static Property ipoctal_properties
[] = {
560 DEFINE_PROP_CHR("chardev0", IPOctalState
, ch
[0].dev
),
561 DEFINE_PROP_CHR("chardev1", IPOctalState
, ch
[1].dev
),
562 DEFINE_PROP_CHR("chardev2", IPOctalState
, ch
[2].dev
),
563 DEFINE_PROP_CHR("chardev3", IPOctalState
, ch
[3].dev
),
564 DEFINE_PROP_CHR("chardev4", IPOctalState
, ch
[4].dev
),
565 DEFINE_PROP_CHR("chardev5", IPOctalState
, ch
[5].dev
),
566 DEFINE_PROP_CHR("chardev6", IPOctalState
, ch
[6].dev
),
567 DEFINE_PROP_CHR("chardev7", IPOctalState
, ch
[7].dev
),
568 DEFINE_PROP_END_OF_LIST(),
571 static void ipoctal_class_init(ObjectClass
*klass
, void *data
)
573 DeviceClass
*dc
= DEVICE_CLASS(klass
);
574 IPackDeviceClass
*ic
= IPACK_DEVICE_CLASS(klass
);
576 ic
->realize
= ipoctal_realize
;
577 ic
->io_read
= io_read
;
578 ic
->io_write
= io_write
;
579 ic
->id_read
= id_read
;
580 ic
->id_write
= id_write
;
581 ic
->int_read
= int_read
;
582 ic
->int_write
= int_write
;
583 ic
->mem_read16
= mem_read16
;
584 ic
->mem_write16
= mem_write16
;
585 ic
->mem_read8
= mem_read8
;
586 ic
->mem_write8
= mem_write8
;
588 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
589 dc
->desc
= "GE IP-Octal 232 8-channel RS-232 IndustryPack";
590 dc
->props
= ipoctal_properties
;
591 dc
->vmsd
= &vmstate_ipoctal
;
594 static const TypeInfo ipoctal_info
= {
595 .name
= TYPE_IPOCTAL
,
596 .parent
= TYPE_IPACK_DEVICE
,
597 .instance_size
= sizeof(IPOctalState
),
598 .class_init
= ipoctal_class_init
,
601 static void ipoctal_register_types(void)
603 type_register_static(&ipoctal_info
);
606 type_init(ipoctal_register_types
)