2 * QEMU Parallel PORT emulation
4 * Copyright (c) 2003-2005 Fabrice Bellard
5 * Copyright (c) 2007 Marko Kohtala
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu-char.h"
31 //#define DEBUG_PARALLEL
34 #define pdebug(fmt, ...) printf("pp: " fmt, ## __VA_ARGS__)
36 #define pdebug(fmt, ...) ((void)0)
39 #define PARA_REG_DATA 0
40 #define PARA_REG_STS 1
41 #define PARA_REG_CTR 2
42 #define PARA_REG_EPP_ADDR 3
43 #define PARA_REG_EPP_DATA 4
46 * These are the definitions for the Printer Status Register
48 #define PARA_STS_BUSY 0x80 /* Busy complement */
49 #define PARA_STS_ACK 0x40 /* Acknowledge */
50 #define PARA_STS_PAPER 0x20 /* Out of paper */
51 #define PARA_STS_ONLINE 0x10 /* Online */
52 #define PARA_STS_ERROR 0x08 /* Error complement */
53 #define PARA_STS_TMOUT 0x01 /* EPP timeout */
56 * These are the definitions for the Printer Control Register
58 #define PARA_CTR_DIR 0x20 /* Direction (1=read, 0=write) */
59 #define PARA_CTR_INTEN 0x10 /* IRQ Enable */
60 #define PARA_CTR_SELECT 0x08 /* Select In complement */
61 #define PARA_CTR_INIT 0x04 /* Initialize Printer complement */
62 #define PARA_CTR_AUTOLF 0x02 /* Auto linefeed complement */
63 #define PARA_CTR_STROBE 0x01 /* Strobe complement */
65 #define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
67 typedef struct ParallelState
{
77 uint32_t last_read_offset
; /* For debugging */
78 /* Memory-mapped interface */
82 typedef struct ISAParallelState
{
90 static void parallel_update_irq(ParallelState
*s
)
93 qemu_irq_raise(s
->irq
);
95 qemu_irq_lower(s
->irq
);
99 parallel_ioport_write_sw(void *opaque
, uint32_t addr
, uint32_t val
)
101 ParallelState
*s
= opaque
;
103 pdebug("write addr=0x%02x val=0x%02x\n", addr
, val
);
109 parallel_update_irq(s
);
113 if ((val
& PARA_CTR_INIT
) == 0 ) {
114 s
->status
= PARA_STS_BUSY
;
115 s
->status
|= PARA_STS_ACK
;
116 s
->status
|= PARA_STS_ONLINE
;
117 s
->status
|= PARA_STS_ERROR
;
119 else if (val
& PARA_CTR_SELECT
) {
120 if (val
& PARA_CTR_STROBE
) {
121 s
->status
&= ~PARA_STS_BUSY
;
122 if ((s
->control
& PARA_CTR_STROBE
) == 0)
123 qemu_chr_write(s
->chr
, &s
->dataw
, 1);
125 if (s
->control
& PARA_CTR_INTEN
) {
130 parallel_update_irq(s
);
136 static void parallel_ioport_write_hw(void *opaque
, uint32_t addr
, uint32_t val
)
138 ParallelState
*s
= opaque
;
142 /* Sometimes programs do several writes for timing purposes on old
143 HW. Take care not to waste time on writes that do nothing. */
145 s
->last_read_offset
= ~0U;
152 pdebug("wd%02x\n", val
);
153 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_WRITE_DATA
, &parm
);
157 pdebug("ws%02x\n", val
);
158 if (val
& PARA_STS_TMOUT
)
163 if (s
->control
== val
)
165 pdebug("wc%02x\n", val
);
167 if ((val
& PARA_CTR_DIR
) != (s
->control
& PARA_CTR_DIR
)) {
168 if (val
& PARA_CTR_DIR
) {
173 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_DATA_DIR
, &dir
);
174 parm
&= ~PARA_CTR_DIR
;
177 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_WRITE_CONTROL
, &parm
);
180 case PARA_REG_EPP_ADDR
:
181 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
182 /* Controls not correct for EPP address cycle, so do nothing */
183 pdebug("wa%02x s\n", val
);
185 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
186 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE_ADDR
, &ioarg
)) {
188 pdebug("wa%02x t\n", val
);
191 pdebug("wa%02x\n", val
);
194 case PARA_REG_EPP_DATA
:
195 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
196 /* Controls not correct for EPP data cycle, so do nothing */
197 pdebug("we%02x s\n", val
);
199 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
200 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
)) {
202 pdebug("we%02x t\n", val
);
205 pdebug("we%02x\n", val
);
212 parallel_ioport_eppdata_write_hw2(void *opaque
, uint32_t addr
, uint32_t val
)
214 ParallelState
*s
= opaque
;
215 uint16_t eppdata
= cpu_to_le16(val
);
217 struct ParallelIOArg ioarg
= {
218 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
220 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
221 /* Controls not correct for EPP data cycle, so do nothing */
222 pdebug("we%04x s\n", val
);
225 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
228 pdebug("we%04x t\n", val
);
231 pdebug("we%04x\n", val
);
235 parallel_ioport_eppdata_write_hw4(void *opaque
, uint32_t addr
, uint32_t val
)
237 ParallelState
*s
= opaque
;
238 uint32_t eppdata
= cpu_to_le32(val
);
240 struct ParallelIOArg ioarg
= {
241 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
243 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
244 /* Controls not correct for EPP data cycle, so do nothing */
245 pdebug("we%08x s\n", val
);
248 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
251 pdebug("we%08x t\n", val
);
254 pdebug("we%08x\n", val
);
257 static uint32_t parallel_ioport_read_sw(void *opaque
, uint32_t addr
)
259 ParallelState
*s
= opaque
;
265 if (s
->control
& PARA_CTR_DIR
)
273 if ((s
->status
& PARA_STS_BUSY
) == 0 && (s
->control
& PARA_CTR_STROBE
) == 0) {
274 /* XXX Fixme: wait 5 microseconds */
275 if (s
->status
& PARA_STS_ACK
)
276 s
->status
&= ~PARA_STS_ACK
;
278 /* XXX Fixme: wait 5 microseconds */
279 s
->status
|= PARA_STS_ACK
;
280 s
->status
|= PARA_STS_BUSY
;
283 parallel_update_irq(s
);
289 pdebug("read addr=0x%02x val=0x%02x\n", addr
, ret
);
293 static uint32_t parallel_ioport_read_hw(void *opaque
, uint32_t addr
)
295 ParallelState
*s
= opaque
;
300 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_DATA
, &ret
);
301 if (s
->last_read_offset
!= addr
|| s
->datar
!= ret
)
302 pdebug("rd%02x\n", ret
);
306 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &ret
);
307 ret
&= ~PARA_STS_TMOUT
;
309 ret
|= PARA_STS_TMOUT
;
310 if (s
->last_read_offset
!= addr
|| s
->status
!= ret
)
311 pdebug("rs%02x\n", ret
);
315 /* s->control has some bits fixed to 1. It is zero only when
316 it has not been yet written to. */
317 if (s
->control
== 0) {
318 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_CONTROL
, &ret
);
319 if (s
->last_read_offset
!= addr
)
320 pdebug("rc%02x\n", ret
);
325 if (s
->last_read_offset
!= addr
)
326 pdebug("rc%02x\n", ret
);
329 case PARA_REG_EPP_ADDR
:
330 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
))
331 /* Controls not correct for EPP addr cycle, so do nothing */
332 pdebug("ra%02x s\n", ret
);
334 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
335 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ_ADDR
, &ioarg
)) {
337 pdebug("ra%02x t\n", ret
);
340 pdebug("ra%02x\n", ret
);
343 case PARA_REG_EPP_DATA
:
344 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
))
345 /* Controls not correct for EPP data cycle, so do nothing */
346 pdebug("re%02x s\n", ret
);
348 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
349 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
)) {
351 pdebug("re%02x t\n", ret
);
354 pdebug("re%02x\n", ret
);
358 s
->last_read_offset
= addr
;
363 parallel_ioport_eppdata_read_hw2(void *opaque
, uint32_t addr
)
365 ParallelState
*s
= opaque
;
367 uint16_t eppdata
= ~0;
369 struct ParallelIOArg ioarg
= {
370 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
372 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
373 /* Controls not correct for EPP data cycle, so do nothing */
374 pdebug("re%04x s\n", eppdata
);
377 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
378 ret
= le16_to_cpu(eppdata
);
382 pdebug("re%04x t\n", ret
);
385 pdebug("re%04x\n", ret
);
390 parallel_ioport_eppdata_read_hw4(void *opaque
, uint32_t addr
)
392 ParallelState
*s
= opaque
;
394 uint32_t eppdata
= ~0U;
396 struct ParallelIOArg ioarg
= {
397 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
399 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
400 /* Controls not correct for EPP data cycle, so do nothing */
401 pdebug("re%08x s\n", eppdata
);
404 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
405 ret
= le32_to_cpu(eppdata
);
409 pdebug("re%08x t\n", ret
);
412 pdebug("re%08x\n", ret
);
416 static void parallel_ioport_ecp_write(void *opaque
, uint32_t addr
, uint32_t val
)
418 pdebug("wecp%d=%02x\n", addr
& 7, val
);
421 static uint32_t parallel_ioport_ecp_read(void *opaque
, uint32_t addr
)
425 pdebug("recp%d:%02x\n", addr
& 7, ret
);
429 static void parallel_reset(void *opaque
)
431 ParallelState
*s
= opaque
;
435 s
->status
= PARA_STS_BUSY
;
436 s
->status
|= PARA_STS_ACK
;
437 s
->status
|= PARA_STS_ONLINE
;
438 s
->status
|= PARA_STS_ERROR
;
439 s
->status
|= PARA_STS_TMOUT
;
440 s
->control
= PARA_CTR_SELECT
;
441 s
->control
|= PARA_CTR_INIT
;
446 s
->last_read_offset
= ~0U;
449 static const int isa_parallel_io
[MAX_PARALLEL_PORTS
] = { 0x378, 0x278, 0x3bc };
451 static int parallel_isa_initfn(ISADevice
*dev
)
454 ISAParallelState
*isa
= DO_UPCAST(ISAParallelState
, dev
, dev
);
455 ParallelState
*s
= &isa
->state
;
460 fprintf(stderr
, "Can't create parallel device, empty char device\n");
464 if (isa
->index
== -1)
466 if (isa
->index
>= MAX_PARALLEL_PORTS
)
468 if (isa
->iobase
== -1)
469 isa
->iobase
= isa_parallel_io
[isa
->index
];
473 isa_init_irq(dev
, &s
->irq
, isa
->isairq
);
474 qemu_register_reset(parallel_reset
, s
);
476 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &dummy
) == 0) {
482 register_ioport_write(base
, 8, 1, parallel_ioport_write_hw
, s
);
483 register_ioport_read(base
, 8, 1, parallel_ioport_read_hw
, s
);
484 isa_init_ioport_range(dev
, base
, 8);
486 register_ioport_write(base
+4, 1, 2, parallel_ioport_eppdata_write_hw2
, s
);
487 register_ioport_read(base
+4, 1, 2, parallel_ioport_eppdata_read_hw2
, s
);
488 register_ioport_write(base
+4, 1, 4, parallel_ioport_eppdata_write_hw4
, s
);
489 register_ioport_read(base
+4, 1, 4, parallel_ioport_eppdata_read_hw4
, s
);
490 isa_init_ioport(dev
, base
+4);
491 register_ioport_write(base
+0x400, 8, 1, parallel_ioport_ecp_write
, s
);
492 register_ioport_read(base
+0x400, 8, 1, parallel_ioport_ecp_read
, s
);
493 isa_init_ioport_range(dev
, base
+0x400, 8);
496 register_ioport_write(base
, 8, 1, parallel_ioport_write_sw
, s
);
497 register_ioport_read(base
, 8, 1, parallel_ioport_read_sw
, s
);
498 isa_init_ioport_range(dev
, base
, 8);
503 /* Memory mapped interface */
504 static uint32_t parallel_mm_readb (void *opaque
, target_phys_addr_t addr
)
506 ParallelState
*s
= opaque
;
508 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
) & 0xFF;
511 static void parallel_mm_writeb (void *opaque
,
512 target_phys_addr_t addr
, uint32_t value
)
514 ParallelState
*s
= opaque
;
516 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
& 0xFF);
519 static uint32_t parallel_mm_readw (void *opaque
, target_phys_addr_t addr
)
521 ParallelState
*s
= opaque
;
523 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
) & 0xFFFF;
526 static void parallel_mm_writew (void *opaque
,
527 target_phys_addr_t addr
, uint32_t value
)
529 ParallelState
*s
= opaque
;
531 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
& 0xFFFF);
534 static uint32_t parallel_mm_readl (void *opaque
, target_phys_addr_t addr
)
536 ParallelState
*s
= opaque
;
538 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
);
541 static void parallel_mm_writel (void *opaque
,
542 target_phys_addr_t addr
, uint32_t value
)
544 ParallelState
*s
= opaque
;
546 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
);
549 static CPUReadMemoryFunc
* const parallel_mm_read_sw
[] = {
555 static CPUWriteMemoryFunc
* const parallel_mm_write_sw
[] = {
561 /* If fd is zero, it means that the parallel device uses the console */
562 bool parallel_mm_init(target_phys_addr_t base
, int it_shift
, qemu_irq irq
,
563 CharDriverState
*chr
)
568 s
= g_malloc0(sizeof(ParallelState
));
571 s
->it_shift
= it_shift
;
572 qemu_register_reset(parallel_reset
, s
);
574 io_sw
= cpu_register_io_memory(parallel_mm_read_sw
, parallel_mm_write_sw
,
575 s
, DEVICE_NATIVE_ENDIAN
);
576 cpu_register_physical_memory(base
, 8 << it_shift
, io_sw
);
580 static ISADeviceInfo parallel_isa_info
= {
581 .qdev
.name
= "isa-parallel",
582 .qdev
.size
= sizeof(ISAParallelState
),
583 .init
= parallel_isa_initfn
,
584 .qdev
.props
= (Property
[]) {
585 DEFINE_PROP_UINT32("index", ISAParallelState
, index
, -1),
586 DEFINE_PROP_HEX32("iobase", ISAParallelState
, iobase
, -1),
587 DEFINE_PROP_UINT32("irq", ISAParallelState
, isairq
, 7),
588 DEFINE_PROP_CHR("chardev", ISAParallelState
, state
.chr
),
589 DEFINE_PROP_END_OF_LIST(),
593 static void parallel_register_devices(void)
595 isa_qdev_register(¶llel_isa_info
);
598 device_init(parallel_register_devices
)