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"
30 //#define DEBUG_PARALLEL
33 #define pdebug(fmt, ...) printf("pp: " fmt, ## __VA_ARGS__)
35 #define pdebug(fmt, ...) ((void)0)
38 #define PARA_REG_DATA 0
39 #define PARA_REG_STS 1
40 #define PARA_REG_CTR 2
41 #define PARA_REG_EPP_ADDR 3
42 #define PARA_REG_EPP_DATA 4
45 * These are the definitions for the Printer Status Register
47 #define PARA_STS_BUSY 0x80 /* Busy complement */
48 #define PARA_STS_ACK 0x40 /* Acknowledge */
49 #define PARA_STS_PAPER 0x20 /* Out of paper */
50 #define PARA_STS_ONLINE 0x10 /* Online */
51 #define PARA_STS_ERROR 0x08 /* Error complement */
52 #define PARA_STS_TMOUT 0x01 /* EPP timeout */
55 * These are the definitions for the Printer Control Register
57 #define PARA_CTR_DIR 0x20 /* Direction (1=read, 0=write) */
58 #define PARA_CTR_INTEN 0x10 /* IRQ Enable */
59 #define PARA_CTR_SELECT 0x08 /* Select In complement */
60 #define PARA_CTR_INIT 0x04 /* Initialize Printer complement */
61 #define PARA_CTR_AUTOLF 0x02 /* Auto linefeed complement */
62 #define PARA_CTR_STROBE 0x01 /* Strobe complement */
64 #define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
66 struct ParallelState
{
76 uint32_t last_read_offset
; /* For debugging */
77 /* Memory-mapped interface */
81 static void parallel_update_irq(ParallelState
*s
)
84 qemu_irq_raise(s
->irq
);
86 qemu_irq_lower(s
->irq
);
90 parallel_ioport_write_sw(void *opaque
, uint32_t addr
, uint32_t val
)
92 ParallelState
*s
= opaque
;
94 pdebug("write addr=0x%02x val=0x%02x\n", addr
, val
);
100 parallel_update_irq(s
);
104 if ((val
& PARA_CTR_INIT
) == 0 ) {
105 s
->status
= PARA_STS_BUSY
;
106 s
->status
|= PARA_STS_ACK
;
107 s
->status
|= PARA_STS_ONLINE
;
108 s
->status
|= PARA_STS_ERROR
;
110 else if (val
& PARA_CTR_SELECT
) {
111 if (val
& PARA_CTR_STROBE
) {
112 s
->status
&= ~PARA_STS_BUSY
;
113 if ((s
->control
& PARA_CTR_STROBE
) == 0)
114 qemu_chr_write(s
->chr
, &s
->dataw
, 1);
116 if (s
->control
& PARA_CTR_INTEN
) {
121 parallel_update_irq(s
);
127 static void parallel_ioport_write_hw(void *opaque
, uint32_t addr
, uint32_t val
)
129 ParallelState
*s
= opaque
;
133 /* Sometimes programs do several writes for timing purposes on old
134 HW. Take care not to waste time on writes that do nothing. */
136 s
->last_read_offset
= ~0U;
143 pdebug("wd%02x\n", val
);
144 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_WRITE_DATA
, &parm
);
148 pdebug("ws%02x\n", val
);
149 if (val
& PARA_STS_TMOUT
)
154 if (s
->control
== val
)
156 pdebug("wc%02x\n", val
);
158 if ((val
& PARA_CTR_DIR
) != (s
->control
& PARA_CTR_DIR
)) {
159 if (val
& PARA_CTR_DIR
) {
164 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_DATA_DIR
, &dir
);
165 parm
&= ~PARA_CTR_DIR
;
168 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_WRITE_CONTROL
, &parm
);
171 case PARA_REG_EPP_ADDR
:
172 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
173 /* Controls not correct for EPP address cycle, so do nothing */
174 pdebug("wa%02x s\n", val
);
176 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
177 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE_ADDR
, &ioarg
)) {
179 pdebug("wa%02x t\n", val
);
182 pdebug("wa%02x\n", val
);
185 case PARA_REG_EPP_DATA
:
186 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
187 /* Controls not correct for EPP data cycle, so do nothing */
188 pdebug("we%02x s\n", val
);
190 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
191 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
)) {
193 pdebug("we%02x t\n", val
);
196 pdebug("we%02x\n", val
);
203 parallel_ioport_eppdata_write_hw2(void *opaque
, uint32_t addr
, uint32_t val
)
205 ParallelState
*s
= opaque
;
206 uint16_t eppdata
= cpu_to_le16(val
);
208 struct ParallelIOArg ioarg
= {
209 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
211 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
212 /* Controls not correct for EPP data cycle, so do nothing */
213 pdebug("we%04x s\n", val
);
216 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
219 pdebug("we%04x t\n", val
);
222 pdebug("we%04x\n", val
);
226 parallel_ioport_eppdata_write_hw4(void *opaque
, uint32_t addr
, uint32_t val
)
228 ParallelState
*s
= opaque
;
229 uint32_t eppdata
= cpu_to_le32(val
);
231 struct ParallelIOArg ioarg
= {
232 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
234 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
235 /* Controls not correct for EPP data cycle, so do nothing */
236 pdebug("we%08x s\n", val
);
239 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
242 pdebug("we%08x t\n", val
);
245 pdebug("we%08x\n", val
);
248 static uint32_t parallel_ioport_read_sw(void *opaque
, uint32_t addr
)
250 ParallelState
*s
= opaque
;
256 if (s
->control
& PARA_CTR_DIR
)
264 if ((s
->status
& PARA_STS_BUSY
) == 0 && (s
->control
& PARA_CTR_STROBE
) == 0) {
265 /* XXX Fixme: wait 5 microseconds */
266 if (s
->status
& PARA_STS_ACK
)
267 s
->status
&= ~PARA_STS_ACK
;
269 /* XXX Fixme: wait 5 microseconds */
270 s
->status
|= PARA_STS_ACK
;
271 s
->status
|= PARA_STS_BUSY
;
274 parallel_update_irq(s
);
280 pdebug("read addr=0x%02x val=0x%02x\n", addr
, ret
);
284 static uint32_t parallel_ioport_read_hw(void *opaque
, uint32_t addr
)
286 ParallelState
*s
= opaque
;
291 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_DATA
, &ret
);
292 if (s
->last_read_offset
!= addr
|| s
->datar
!= ret
)
293 pdebug("rd%02x\n", ret
);
297 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &ret
);
298 ret
&= ~PARA_STS_TMOUT
;
300 ret
|= PARA_STS_TMOUT
;
301 if (s
->last_read_offset
!= addr
|| s
->status
!= ret
)
302 pdebug("rs%02x\n", ret
);
306 /* s->control has some bits fixed to 1. It is zero only when
307 it has not been yet written to. */
308 if (s
->control
== 0) {
309 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_CONTROL
, &ret
);
310 if (s
->last_read_offset
!= addr
)
311 pdebug("rc%02x\n", ret
);
316 if (s
->last_read_offset
!= addr
)
317 pdebug("rc%02x\n", ret
);
320 case PARA_REG_EPP_ADDR
:
321 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
))
322 /* Controls not correct for EPP addr cycle, so do nothing */
323 pdebug("ra%02x s\n", ret
);
325 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
326 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ_ADDR
, &ioarg
)) {
328 pdebug("ra%02x t\n", ret
);
331 pdebug("ra%02x\n", ret
);
334 case PARA_REG_EPP_DATA
:
335 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
))
336 /* Controls not correct for EPP data cycle, so do nothing */
337 pdebug("re%02x s\n", ret
);
339 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
340 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
)) {
342 pdebug("re%02x t\n", ret
);
345 pdebug("re%02x\n", ret
);
349 s
->last_read_offset
= addr
;
354 parallel_ioport_eppdata_read_hw2(void *opaque
, uint32_t addr
)
356 ParallelState
*s
= opaque
;
358 uint16_t eppdata
= ~0;
360 struct ParallelIOArg ioarg
= {
361 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
363 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
364 /* Controls not correct for EPP data cycle, so do nothing */
365 pdebug("re%04x s\n", eppdata
);
368 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
369 ret
= le16_to_cpu(eppdata
);
373 pdebug("re%04x t\n", ret
);
376 pdebug("re%04x\n", ret
);
381 parallel_ioport_eppdata_read_hw4(void *opaque
, uint32_t addr
)
383 ParallelState
*s
= opaque
;
385 uint32_t eppdata
= ~0U;
387 struct ParallelIOArg ioarg
= {
388 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
390 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
391 /* Controls not correct for EPP data cycle, so do nothing */
392 pdebug("re%08x s\n", eppdata
);
395 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
396 ret
= le32_to_cpu(eppdata
);
400 pdebug("re%08x t\n", ret
);
403 pdebug("re%08x\n", ret
);
407 static void parallel_ioport_ecp_write(void *opaque
, uint32_t addr
, uint32_t val
)
410 pdebug("wecp%d=%02x\n", addr
, val
);
413 static uint32_t parallel_ioport_ecp_read(void *opaque
, uint32_t addr
)
417 pdebug("recp%d:%02x\n", addr
, ret
);
421 static void parallel_reset(void *opaque
)
423 ParallelState
*s
= opaque
;
427 s
->status
= PARA_STS_BUSY
;
428 s
->status
|= PARA_STS_ACK
;
429 s
->status
|= PARA_STS_ONLINE
;
430 s
->status
|= PARA_STS_ERROR
;
431 s
->status
|= PARA_STS_TMOUT
;
432 s
->control
= PARA_CTR_SELECT
;
433 s
->control
|= PARA_CTR_INIT
;
438 s
->last_read_offset
= ~0U;
441 /* If fd is zero, it means that the parallel device uses the console */
442 ParallelState
*parallel_init(int base
, qemu_irq irq
, CharDriverState
*chr
)
447 s
= qemu_mallocz(sizeof(ParallelState
));
451 qemu_register_reset(parallel_reset
, s
);
453 if (qemu_chr_ioctl(chr
, CHR_IOCTL_PP_READ_STATUS
, &dummy
) == 0) {
459 register_ioport_write(base
, 8, 1, parallel_ioport_write_hw
, s
);
460 register_ioport_read(base
, 8, 1, parallel_ioport_read_hw
, s
);
461 register_ioport_write(base
+4, 1, 2, parallel_ioport_eppdata_write_hw2
, s
);
462 register_ioport_read(base
+4, 1, 2, parallel_ioport_eppdata_read_hw2
, s
);
463 register_ioport_write(base
+4, 1, 4, parallel_ioport_eppdata_write_hw4
, s
);
464 register_ioport_read(base
+4, 1, 4, parallel_ioport_eppdata_read_hw4
, s
);
465 register_ioport_write(base
+0x400, 8, 1, parallel_ioport_ecp_write
, s
);
466 register_ioport_read(base
+0x400, 8, 1, parallel_ioport_ecp_read
, s
);
469 register_ioport_write(base
, 8, 1, parallel_ioport_write_sw
, s
);
470 register_ioport_read(base
, 8, 1, parallel_ioport_read_sw
, s
);
475 /* Memory mapped interface */
476 static uint32_t parallel_mm_readb (void *opaque
, target_phys_addr_t addr
)
478 ParallelState
*s
= opaque
;
480 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
) & 0xFF;
483 static void parallel_mm_writeb (void *opaque
,
484 target_phys_addr_t addr
, uint32_t value
)
486 ParallelState
*s
= opaque
;
488 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
& 0xFF);
491 static uint32_t parallel_mm_readw (void *opaque
, target_phys_addr_t addr
)
493 ParallelState
*s
= opaque
;
495 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
) & 0xFFFF;
498 static void parallel_mm_writew (void *opaque
,
499 target_phys_addr_t addr
, uint32_t value
)
501 ParallelState
*s
= opaque
;
503 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
& 0xFFFF);
506 static uint32_t parallel_mm_readl (void *opaque
, target_phys_addr_t addr
)
508 ParallelState
*s
= opaque
;
510 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
);
513 static void parallel_mm_writel (void *opaque
,
514 target_phys_addr_t addr
, uint32_t value
)
516 ParallelState
*s
= opaque
;
518 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
);
521 static CPUReadMemoryFunc
* const parallel_mm_read_sw
[] = {
527 static CPUWriteMemoryFunc
* const parallel_mm_write_sw
[] = {
533 /* If fd is zero, it means that the parallel device uses the console */
534 ParallelState
*parallel_mm_init(target_phys_addr_t base
, int it_shift
, qemu_irq irq
, CharDriverState
*chr
)
539 s
= qemu_mallocz(sizeof(ParallelState
));
542 s
->it_shift
= it_shift
;
544 qemu_register_reset(parallel_reset
, s
);
546 io_sw
= cpu_register_io_memory(parallel_mm_read_sw
, parallel_mm_write_sw
, s
);
547 cpu_register_physical_memory(base
, 8 << it_shift
, io_sw
);