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, arg...) printf("pp: " fmt, ##arg)
35 #define pdebug(fmt, arg...) ((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 */
78 target_phys_addr_t base
;
82 static void parallel_update_irq(ParallelState
*s
)
85 qemu_irq_raise(s
->irq
);
87 qemu_irq_lower(s
->irq
);
91 parallel_ioport_write_sw(void *opaque
, uint32_t addr
, uint32_t val
)
93 ParallelState
*s
= opaque
;
95 pdebug("write addr=0x%02x val=0x%02x\n", addr
, val
);
101 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
;
132 /* Sometimes programs do several writes for timing purposes on old
133 HW. Take care not to waste time on writes that do nothing. */
135 s
->last_read_offset
= ~0U;
142 pdebug("wd%02x\n", val
);
143 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_WRITE_DATA
, &parm
);
147 pdebug("ws%02x\n", val
);
148 if (val
& PARA_STS_TMOUT
)
153 if (s
->control
== val
)
155 pdebug("wc%02x\n", val
);
156 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_WRITE_CONTROL
, &parm
);
159 case PARA_REG_EPP_ADDR
:
160 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
161 /* Controls not correct for EPP address cycle, so do nothing */
162 pdebug("wa%02x s\n", val
);
164 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
165 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE_ADDR
, &ioarg
)) {
167 pdebug("wa%02x t\n", val
);
170 pdebug("wa%02x\n", val
);
173 case PARA_REG_EPP_DATA
:
174 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
175 /* Controls not correct for EPP data cycle, so do nothing */
176 pdebug("we%02x s\n", val
);
178 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
179 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
)) {
181 pdebug("we%02x t\n", val
);
184 pdebug("we%02x\n", val
);
191 parallel_ioport_eppdata_write_hw2(void *opaque
, uint32_t addr
, uint32_t val
)
193 ParallelState
*s
= opaque
;
194 uint16_t eppdata
= cpu_to_le16(val
);
196 struct ParallelIOArg ioarg
= {
197 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
199 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
200 /* Controls not correct for EPP data cycle, so do nothing */
201 pdebug("we%04x s\n", val
);
204 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
207 pdebug("we%04x t\n", val
);
210 pdebug("we%04x\n", val
);
214 parallel_ioport_eppdata_write_hw4(void *opaque
, uint32_t addr
, uint32_t val
)
216 ParallelState
*s
= opaque
;
217 uint32_t eppdata
= cpu_to_le32(val
);
219 struct ParallelIOArg ioarg
= {
220 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
222 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
223 /* Controls not correct for EPP data cycle, so do nothing */
224 pdebug("we%08x s\n", val
);
227 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
230 pdebug("we%08x t\n", val
);
233 pdebug("we%08x\n", val
);
236 static uint32_t parallel_ioport_read_sw(void *opaque
, uint32_t addr
)
238 ParallelState
*s
= opaque
;
244 if (s
->control
& PARA_CTR_DIR
)
252 if ((s
->status
& PARA_STS_BUSY
) == 0 && (s
->control
& PARA_CTR_STROBE
) == 0) {
253 /* XXX Fixme: wait 5 microseconds */
254 if (s
->status
& PARA_STS_ACK
)
255 s
->status
&= ~PARA_STS_ACK
;
257 /* XXX Fixme: wait 5 microseconds */
258 s
->status
|= PARA_STS_ACK
;
259 s
->status
|= PARA_STS_BUSY
;
262 parallel_update_irq(s
);
268 pdebug("read addr=0x%02x val=0x%02x\n", addr
, ret
);
272 static uint32_t parallel_ioport_read_hw(void *opaque
, uint32_t addr
)
274 ParallelState
*s
= opaque
;
279 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_DATA
, &ret
);
280 if (s
->last_read_offset
!= addr
|| s
->datar
!= ret
)
281 pdebug("rd%02x\n", ret
);
285 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &ret
);
286 ret
&= ~PARA_STS_TMOUT
;
288 ret
|= PARA_STS_TMOUT
;
289 if (s
->last_read_offset
!= addr
|| s
->status
!= ret
)
290 pdebug("rs%02x\n", ret
);
294 /* s->control has some bits fixed to 1. It is zero only when
295 it has not been yet written to. */
296 if (s
->control
== 0) {
297 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_CONTROL
, &ret
);
298 if (s
->last_read_offset
!= addr
)
299 pdebug("rc%02x\n", ret
);
304 if (s
->last_read_offset
!= addr
)
305 pdebug("rc%02x\n", ret
);
308 case PARA_REG_EPP_ADDR
:
309 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
))
310 /* Controls not correct for EPP addr cycle, so do nothing */
311 pdebug("ra%02x s\n", ret
);
313 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
314 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ_ADDR
, &ioarg
)) {
316 pdebug("ra%02x t\n", ret
);
319 pdebug("ra%02x\n", ret
);
322 case PARA_REG_EPP_DATA
:
323 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
))
324 /* Controls not correct for EPP data cycle, so do nothing */
325 pdebug("re%02x s\n", ret
);
327 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
328 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
)) {
330 pdebug("re%02x t\n", ret
);
333 pdebug("re%02x\n", ret
);
337 s
->last_read_offset
= addr
;
342 parallel_ioport_eppdata_read_hw2(void *opaque
, uint32_t addr
)
344 ParallelState
*s
= opaque
;
346 uint16_t eppdata
= ~0;
348 struct ParallelIOArg ioarg
= {
349 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
351 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
352 /* Controls not correct for EPP data cycle, so do nothing */
353 pdebug("re%04x s\n", eppdata
);
356 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
357 ret
= le16_to_cpu(eppdata
);
361 pdebug("re%04x t\n", ret
);
364 pdebug("re%04x\n", ret
);
369 parallel_ioport_eppdata_read_hw4(void *opaque
, uint32_t addr
)
371 ParallelState
*s
= opaque
;
373 uint32_t eppdata
= ~0U;
375 struct ParallelIOArg ioarg
= {
376 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
378 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
379 /* Controls not correct for EPP data cycle, so do nothing */
380 pdebug("re%08x s\n", eppdata
);
383 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
384 ret
= le32_to_cpu(eppdata
);
388 pdebug("re%08x t\n", ret
);
391 pdebug("re%08x\n", ret
);
395 static void parallel_ioport_ecp_write(void *opaque
, uint32_t addr
, uint32_t val
)
398 pdebug("wecp%d=%02x\n", addr
, val
);
401 static uint32_t parallel_ioport_ecp_read(void *opaque
, uint32_t addr
)
405 pdebug("recp%d:%02x\n", addr
, ret
);
409 static void parallel_reset(ParallelState
*s
, qemu_irq irq
, CharDriverState
*chr
)
413 s
->status
= PARA_STS_BUSY
;
414 s
->status
|= PARA_STS_ACK
;
415 s
->status
|= PARA_STS_ONLINE
;
416 s
->status
|= PARA_STS_ERROR
;
417 s
->control
= PARA_CTR_SELECT
;
418 s
->control
|= PARA_CTR_INIT
;
424 s
->last_read_offset
= ~0U;
427 /* If fd is zero, it means that the parallel device uses the console */
428 ParallelState
*parallel_init(int base
, qemu_irq irq
, CharDriverState
*chr
)
433 s
= qemu_mallocz(sizeof(ParallelState
));
436 parallel_reset(s
, irq
, chr
);
438 if (qemu_chr_ioctl(chr
, CHR_IOCTL_PP_READ_STATUS
, &dummy
) == 0) {
444 register_ioport_write(base
, 8, 1, parallel_ioport_write_hw
, s
);
445 register_ioport_read(base
, 8, 1, parallel_ioport_read_hw
, s
);
446 register_ioport_write(base
+4, 1, 2, parallel_ioport_eppdata_write_hw2
, s
);
447 register_ioport_read(base
+4, 1, 2, parallel_ioport_eppdata_read_hw2
, s
);
448 register_ioport_write(base
+4, 1, 4, parallel_ioport_eppdata_write_hw4
, s
);
449 register_ioport_read(base
+4, 1, 4, parallel_ioport_eppdata_read_hw4
, s
);
450 register_ioport_write(base
+0x400, 8, 1, parallel_ioport_ecp_write
, s
);
451 register_ioport_read(base
+0x400, 8, 1, parallel_ioport_ecp_read
, s
);
454 register_ioport_write(base
, 8, 1, parallel_ioport_write_sw
, s
);
455 register_ioport_read(base
, 8, 1, parallel_ioport_read_sw
, s
);
460 /* Memory mapped interface */
461 static uint32_t parallel_mm_readb (void *opaque
, target_phys_addr_t addr
)
463 ParallelState
*s
= opaque
;
465 return parallel_ioport_read_sw(s
, (addr
- s
->base
) >> s
->it_shift
) & 0xFF;
468 static void parallel_mm_writeb (void *opaque
,
469 target_phys_addr_t addr
, uint32_t value
)
471 ParallelState
*s
= opaque
;
473 parallel_ioport_write_sw(s
, (addr
- s
->base
) >> s
->it_shift
, value
& 0xFF);
476 static uint32_t parallel_mm_readw (void *opaque
, target_phys_addr_t addr
)
478 ParallelState
*s
= opaque
;
480 return parallel_ioport_read_sw(s
, (addr
- s
->base
) >> s
->it_shift
) & 0xFFFF;
483 static void parallel_mm_writew (void *opaque
,
484 target_phys_addr_t addr
, uint32_t value
)
486 ParallelState
*s
= opaque
;
488 parallel_ioport_write_sw(s
, (addr
- s
->base
) >> s
->it_shift
, value
& 0xFFFF);
491 static uint32_t parallel_mm_readl (void *opaque
, target_phys_addr_t addr
)
493 ParallelState
*s
= opaque
;
495 return parallel_ioport_read_sw(s
, (addr
- s
->base
) >> s
->it_shift
);
498 static void parallel_mm_writel (void *opaque
,
499 target_phys_addr_t addr
, uint32_t value
)
501 ParallelState
*s
= opaque
;
503 parallel_ioport_write_sw(s
, (addr
- s
->base
) >> s
->it_shift
, value
);
506 static CPUReadMemoryFunc
*parallel_mm_read_sw
[] = {
512 static CPUWriteMemoryFunc
*parallel_mm_write_sw
[] = {
518 /* If fd is zero, it means that the parallel device uses the console */
519 ParallelState
*parallel_mm_init(target_phys_addr_t base
, int it_shift
, qemu_irq irq
, CharDriverState
*chr
)
524 s
= qemu_mallocz(sizeof(ParallelState
));
527 parallel_reset(s
, irq
, chr
);
529 s
->it_shift
= it_shift
;
531 io_sw
= cpu_register_io_memory(0, parallel_mm_read_sw
, parallel_mm_write_sw
, s
);
532 cpu_register_physical_memory(base
, 8 << it_shift
, io_sw
);