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 "char/char.h"
27 #include "hw/isa/isa.h"
28 #include "hw/i386/pc.h"
29 #include "sysemu/sysemu.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
{
78 uint32_t last_read_offset
; /* For debugging */
79 /* Memory-mapped interface */
83 typedef struct ISAParallelState
{
91 static void parallel_update_irq(ParallelState
*s
)
94 qemu_irq_raise(s
->irq
);
96 qemu_irq_lower(s
->irq
);
100 parallel_ioport_write_sw(void *opaque
, uint32_t addr
, uint32_t val
)
102 ParallelState
*s
= opaque
;
104 pdebug("write addr=0x%02x val=0x%02x\n", addr
, val
);
110 parallel_update_irq(s
);
114 if ((val
& PARA_CTR_INIT
) == 0 ) {
115 s
->status
= PARA_STS_BUSY
;
116 s
->status
|= PARA_STS_ACK
;
117 s
->status
|= PARA_STS_ONLINE
;
118 s
->status
|= PARA_STS_ERROR
;
120 else if (val
& PARA_CTR_SELECT
) {
121 if (val
& PARA_CTR_STROBE
) {
122 s
->status
&= ~PARA_STS_BUSY
;
123 if ((s
->control
& PARA_CTR_STROBE
) == 0)
124 qemu_chr_fe_write(s
->chr
, &s
->dataw
, 1);
126 if (s
->control
& PARA_CTR_INTEN
) {
131 parallel_update_irq(s
);
137 static void parallel_ioport_write_hw(void *opaque
, uint32_t addr
, uint32_t val
)
139 ParallelState
*s
= opaque
;
143 /* Sometimes programs do several writes for timing purposes on old
144 HW. Take care not to waste time on writes that do nothing. */
146 s
->last_read_offset
= ~0U;
153 pdebug("wd%02x\n", val
);
154 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_WRITE_DATA
, &parm
);
158 pdebug("ws%02x\n", val
);
159 if (val
& PARA_STS_TMOUT
)
164 if (s
->control
== val
)
166 pdebug("wc%02x\n", val
);
168 if ((val
& PARA_CTR_DIR
) != (s
->control
& PARA_CTR_DIR
)) {
169 if (val
& PARA_CTR_DIR
) {
174 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_DATA_DIR
, &dir
);
175 parm
&= ~PARA_CTR_DIR
;
178 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_WRITE_CONTROL
, &parm
);
181 case PARA_REG_EPP_ADDR
:
182 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
183 /* Controls not correct for EPP address cycle, so do nothing */
184 pdebug("wa%02x s\n", val
);
186 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
187 if (qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE_ADDR
, &ioarg
)) {
189 pdebug("wa%02x t\n", val
);
192 pdebug("wa%02x\n", val
);
195 case PARA_REG_EPP_DATA
:
196 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
197 /* Controls not correct for EPP data cycle, so do nothing */
198 pdebug("we%02x s\n", val
);
200 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
201 if (qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
)) {
203 pdebug("we%02x t\n", val
);
206 pdebug("we%02x\n", val
);
213 parallel_ioport_eppdata_write_hw2(void *opaque
, uint32_t addr
, uint32_t val
)
215 ParallelState
*s
= opaque
;
216 uint16_t eppdata
= cpu_to_le16(val
);
218 struct ParallelIOArg ioarg
= {
219 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
221 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
222 /* Controls not correct for EPP data cycle, so do nothing */
223 pdebug("we%04x s\n", val
);
226 err
= qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
229 pdebug("we%04x t\n", val
);
232 pdebug("we%04x\n", val
);
236 parallel_ioport_eppdata_write_hw4(void *opaque
, uint32_t addr
, uint32_t val
)
238 ParallelState
*s
= opaque
;
239 uint32_t eppdata
= cpu_to_le32(val
);
241 struct ParallelIOArg ioarg
= {
242 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
244 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
245 /* Controls not correct for EPP data cycle, so do nothing */
246 pdebug("we%08x s\n", val
);
249 err
= qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
252 pdebug("we%08x t\n", val
);
255 pdebug("we%08x\n", val
);
258 static uint32_t parallel_ioport_read_sw(void *opaque
, uint32_t addr
)
260 ParallelState
*s
= opaque
;
266 if (s
->control
& PARA_CTR_DIR
)
274 if ((s
->status
& PARA_STS_BUSY
) == 0 && (s
->control
& PARA_CTR_STROBE
) == 0) {
275 /* XXX Fixme: wait 5 microseconds */
276 if (s
->status
& PARA_STS_ACK
)
277 s
->status
&= ~PARA_STS_ACK
;
279 /* XXX Fixme: wait 5 microseconds */
280 s
->status
|= PARA_STS_ACK
;
281 s
->status
|= PARA_STS_BUSY
;
284 parallel_update_irq(s
);
290 pdebug("read addr=0x%02x val=0x%02x\n", addr
, ret
);
294 static uint32_t parallel_ioport_read_hw(void *opaque
, uint32_t addr
)
296 ParallelState
*s
= opaque
;
301 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_READ_DATA
, &ret
);
302 if (s
->last_read_offset
!= addr
|| s
->datar
!= ret
)
303 pdebug("rd%02x\n", ret
);
307 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &ret
);
308 ret
&= ~PARA_STS_TMOUT
;
310 ret
|= PARA_STS_TMOUT
;
311 if (s
->last_read_offset
!= addr
|| s
->status
!= ret
)
312 pdebug("rs%02x\n", ret
);
316 /* s->control has some bits fixed to 1. It is zero only when
317 it has not been yet written to. */
318 if (s
->control
== 0) {
319 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_READ_CONTROL
, &ret
);
320 if (s
->last_read_offset
!= addr
)
321 pdebug("rc%02x\n", ret
);
326 if (s
->last_read_offset
!= addr
)
327 pdebug("rc%02x\n", ret
);
330 case PARA_REG_EPP_ADDR
:
331 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
))
332 /* Controls not correct for EPP addr cycle, so do nothing */
333 pdebug("ra%02x s\n", ret
);
335 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
336 if (qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ_ADDR
, &ioarg
)) {
338 pdebug("ra%02x t\n", ret
);
341 pdebug("ra%02x\n", ret
);
344 case PARA_REG_EPP_DATA
:
345 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
))
346 /* Controls not correct for EPP data cycle, so do nothing */
347 pdebug("re%02x s\n", ret
);
349 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
350 if (qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
)) {
352 pdebug("re%02x t\n", ret
);
355 pdebug("re%02x\n", ret
);
359 s
->last_read_offset
= addr
;
364 parallel_ioport_eppdata_read_hw2(void *opaque
, uint32_t addr
)
366 ParallelState
*s
= opaque
;
368 uint16_t eppdata
= ~0;
370 struct ParallelIOArg ioarg
= {
371 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
373 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
374 /* Controls not correct for EPP data cycle, so do nothing */
375 pdebug("re%04x s\n", eppdata
);
378 err
= qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
379 ret
= le16_to_cpu(eppdata
);
383 pdebug("re%04x t\n", ret
);
386 pdebug("re%04x\n", ret
);
391 parallel_ioport_eppdata_read_hw4(void *opaque
, uint32_t addr
)
393 ParallelState
*s
= opaque
;
395 uint32_t eppdata
= ~0U;
397 struct ParallelIOArg ioarg
= {
398 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
400 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
401 /* Controls not correct for EPP data cycle, so do nothing */
402 pdebug("re%08x s\n", eppdata
);
405 err
= qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
406 ret
= le32_to_cpu(eppdata
);
410 pdebug("re%08x t\n", ret
);
413 pdebug("re%08x\n", ret
);
417 static void parallel_ioport_ecp_write(void *opaque
, uint32_t addr
, uint32_t val
)
419 pdebug("wecp%d=%02x\n", addr
& 7, val
);
422 static uint32_t parallel_ioport_ecp_read(void *opaque
, uint32_t addr
)
426 pdebug("recp%d:%02x\n", addr
& 7, ret
);
430 static void parallel_reset(void *opaque
)
432 ParallelState
*s
= opaque
;
436 s
->status
= PARA_STS_BUSY
;
437 s
->status
|= PARA_STS_ACK
;
438 s
->status
|= PARA_STS_ONLINE
;
439 s
->status
|= PARA_STS_ERROR
;
440 s
->status
|= PARA_STS_TMOUT
;
441 s
->control
= PARA_CTR_SELECT
;
442 s
->control
|= PARA_CTR_INIT
;
447 s
->last_read_offset
= ~0U;
450 static const int isa_parallel_io
[MAX_PARALLEL_PORTS
] = { 0x378, 0x278, 0x3bc };
452 static const MemoryRegionPortio isa_parallel_portio_hw_list
[] = {
454 .read
= parallel_ioport_read_hw
,
455 .write
= parallel_ioport_write_hw
},
457 .read
= parallel_ioport_eppdata_read_hw2
,
458 .write
= parallel_ioport_eppdata_write_hw2
},
460 .read
= parallel_ioport_eppdata_read_hw4
,
461 .write
= parallel_ioport_eppdata_write_hw4
},
463 .read
= parallel_ioport_ecp_read
,
464 .write
= parallel_ioport_ecp_write
},
465 PORTIO_END_OF_LIST(),
468 static const MemoryRegionPortio isa_parallel_portio_sw_list
[] = {
470 .read
= parallel_ioport_read_sw
,
471 .write
= parallel_ioport_write_sw
},
472 PORTIO_END_OF_LIST(),
475 static int parallel_isa_initfn(ISADevice
*dev
)
478 ISAParallelState
*isa
= DO_UPCAST(ISAParallelState
, dev
, dev
);
479 ParallelState
*s
= &isa
->state
;
484 fprintf(stderr
, "Can't create parallel device, empty char device\n");
488 if (isa
->index
== -1)
490 if (isa
->index
>= MAX_PARALLEL_PORTS
)
492 if (isa
->iobase
== -1)
493 isa
->iobase
= isa_parallel_io
[isa
->index
];
497 isa_init_irq(dev
, &s
->irq
, isa
->isairq
);
498 qemu_register_reset(parallel_reset
, s
);
500 if (qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &dummy
) == 0) {
505 isa_register_portio_list(dev
, base
,
507 ? &isa_parallel_portio_hw_list
[0]
508 : &isa_parallel_portio_sw_list
[0]),
513 /* Memory mapped interface */
514 static uint32_t parallel_mm_readb (void *opaque
, hwaddr addr
)
516 ParallelState
*s
= opaque
;
518 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
) & 0xFF;
521 static void parallel_mm_writeb (void *opaque
,
522 hwaddr addr
, uint32_t value
)
524 ParallelState
*s
= opaque
;
526 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
& 0xFF);
529 static uint32_t parallel_mm_readw (void *opaque
, hwaddr addr
)
531 ParallelState
*s
= opaque
;
533 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
) & 0xFFFF;
536 static void parallel_mm_writew (void *opaque
,
537 hwaddr addr
, uint32_t value
)
539 ParallelState
*s
= opaque
;
541 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
& 0xFFFF);
544 static uint32_t parallel_mm_readl (void *opaque
, hwaddr addr
)
546 ParallelState
*s
= opaque
;
548 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
);
551 static void parallel_mm_writel (void *opaque
,
552 hwaddr addr
, uint32_t value
)
554 ParallelState
*s
= opaque
;
556 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
);
559 static const MemoryRegionOps parallel_mm_ops
= {
561 .read
= { parallel_mm_readb
, parallel_mm_readw
, parallel_mm_readl
},
562 .write
= { parallel_mm_writeb
, parallel_mm_writew
, parallel_mm_writel
},
564 .endianness
= DEVICE_NATIVE_ENDIAN
,
567 /* If fd is zero, it means that the parallel device uses the console */
568 bool parallel_mm_init(MemoryRegion
*address_space
,
569 hwaddr base
, int it_shift
, qemu_irq irq
,
570 CharDriverState
*chr
)
574 s
= g_malloc0(sizeof(ParallelState
));
577 s
->it_shift
= it_shift
;
578 qemu_register_reset(parallel_reset
, s
);
580 memory_region_init_io(&s
->iomem
, ¶llel_mm_ops
, s
,
581 "parallel", 8 << it_shift
);
582 memory_region_add_subregion(address_space
, base
, &s
->iomem
);
586 static Property parallel_isa_properties
[] = {
587 DEFINE_PROP_UINT32("index", ISAParallelState
, index
, -1),
588 DEFINE_PROP_HEX32("iobase", ISAParallelState
, iobase
, -1),
589 DEFINE_PROP_UINT32("irq", ISAParallelState
, isairq
, 7),
590 DEFINE_PROP_CHR("chardev", ISAParallelState
, state
.chr
),
591 DEFINE_PROP_END_OF_LIST(),
594 static void parallel_isa_class_initfn(ObjectClass
*klass
, void *data
)
596 DeviceClass
*dc
= DEVICE_CLASS(klass
);
597 ISADeviceClass
*ic
= ISA_DEVICE_CLASS(klass
);
598 ic
->init
= parallel_isa_initfn
;
599 dc
->props
= parallel_isa_properties
;
602 static const TypeInfo parallel_isa_info
= {
603 .name
= "isa-parallel",
604 .parent
= TYPE_ISA_DEVICE
,
605 .instance_size
= sizeof(ISAParallelState
),
606 .class_init
= parallel_isa_class_initfn
,
609 static void parallel_register_types(void)
611 type_register_static(¶llel_isa_info
);
614 type_init(parallel_register_types
)