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
27 //#define DEBUG_PARALLEL
30 #define pdebug(fmt, arg...) printf("pp: " fmt, ##arg)
32 #define pdebug(fmt, arg...) ((void)0)
35 #define PARA_REG_DATA 0
36 #define PARA_REG_STS 1
37 #define PARA_REG_CTR 2
38 #define PARA_REG_EPP_ADDR 3
39 #define PARA_REG_EPP_DATA 4
42 * These are the definitions for the Printer Status Register
44 #define PARA_STS_BUSY 0x80 /* Busy complement */
45 #define PARA_STS_ACK 0x40 /* Acknowledge */
46 #define PARA_STS_PAPER 0x20 /* Out of paper */
47 #define PARA_STS_ONLINE 0x10 /* Online */
48 #define PARA_STS_ERROR 0x08 /* Error complement */
49 #define PARA_STS_TMOUT 0x01 /* EPP timeout */
52 * These are the definitions for the Printer Control Register
54 #define PARA_CTR_DIR 0x20 /* Direction (1=read, 0=write) */
55 #define PARA_CTR_INTEN 0x10 /* IRQ Enable */
56 #define PARA_CTR_SELECT 0x08 /* Select In complement */
57 #define PARA_CTR_INIT 0x04 /* Initialize Printer complement */
58 #define PARA_CTR_AUTOLF 0x02 /* Auto linefeed complement */
59 #define PARA_CTR_STROBE 0x01 /* Strobe complement */
61 #define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
63 struct ParallelState
{
73 uint32_t last_read_offset
; /* For debugging */
74 /* Memory-mapped interface */
75 target_phys_addr_t base
;
79 static void parallel_update_irq(ParallelState
*s
)
82 qemu_irq_raise(s
->irq
);
84 qemu_irq_lower(s
->irq
);
88 parallel_ioport_write_sw(void *opaque
, uint32_t addr
, uint32_t val
)
90 ParallelState
*s
= opaque
;
92 pdebug("write addr=0x%02x val=0x%02x\n", addr
, val
);
98 parallel_update_irq(s
);
101 if ((val
& PARA_CTR_INIT
) == 0 ) {
102 s
->status
= PARA_STS_BUSY
;
103 s
->status
|= PARA_STS_ACK
;
104 s
->status
|= PARA_STS_ONLINE
;
105 s
->status
|= PARA_STS_ERROR
;
107 else if (val
& PARA_CTR_SELECT
) {
108 if (val
& PARA_CTR_STROBE
) {
109 s
->status
&= ~PARA_STS_BUSY
;
110 if ((s
->control
& PARA_CTR_STROBE
) == 0)
111 qemu_chr_write(s
->chr
, &s
->dataw
, 1);
113 if (s
->control
& PARA_CTR_INTEN
) {
118 parallel_update_irq(s
);
124 static void parallel_ioport_write_hw(void *opaque
, uint32_t addr
, uint32_t val
)
126 ParallelState
*s
= opaque
;
129 /* Sometimes programs do several writes for timing purposes on old
130 HW. Take care not to waste time on writes that do nothing. */
132 s
->last_read_offset
= ~0U;
139 pdebug("wd%02x\n", val
);
140 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_WRITE_DATA
, &parm
);
144 pdebug("ws%02x\n", val
);
145 if (val
& PARA_STS_TMOUT
)
150 if (s
->control
== val
)
152 pdebug("wc%02x\n", val
);
153 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_WRITE_CONTROL
, &parm
);
156 case PARA_REG_EPP_ADDR
:
157 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
158 /* Controls not correct for EPP address cycle, so do nothing */
159 pdebug("wa%02x s\n", val
);
161 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
162 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE_ADDR
, &ioarg
)) {
164 pdebug("wa%02x t\n", val
);
167 pdebug("wa%02x\n", val
);
170 case PARA_REG_EPP_DATA
:
171 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
172 /* Controls not correct for EPP data cycle, so do nothing */
173 pdebug("we%02x s\n", val
);
175 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
176 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
)) {
178 pdebug("we%02x t\n", val
);
181 pdebug("we%02x\n", val
);
188 parallel_ioport_eppdata_write_hw2(void *opaque
, uint32_t addr
, uint32_t val
)
190 ParallelState
*s
= opaque
;
191 uint16_t eppdata
= cpu_to_le16(val
);
193 struct ParallelIOArg ioarg
= {
194 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
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%04x s\n", val
);
201 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
204 pdebug("we%04x t\n", val
);
207 pdebug("we%04x\n", val
);
211 parallel_ioport_eppdata_write_hw4(void *opaque
, uint32_t addr
, uint32_t val
)
213 ParallelState
*s
= opaque
;
214 uint32_t eppdata
= cpu_to_le32(val
);
216 struct ParallelIOArg ioarg
= {
217 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
219 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
220 /* Controls not correct for EPP data cycle, so do nothing */
221 pdebug("we%08x s\n", val
);
224 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
227 pdebug("we%08x t\n", val
);
230 pdebug("we%08x\n", val
);
233 static uint32_t parallel_ioport_read_sw(void *opaque
, uint32_t addr
)
235 ParallelState
*s
= opaque
;
241 if (s
->control
& PARA_CTR_DIR
)
249 if ((s
->status
& PARA_STS_BUSY
) == 0 && (s
->control
& PARA_CTR_STROBE
) == 0) {
250 /* XXX Fixme: wait 5 microseconds */
251 if (s
->status
& PARA_STS_ACK
)
252 s
->status
&= ~PARA_STS_ACK
;
254 /* XXX Fixme: wait 5 microseconds */
255 s
->status
|= PARA_STS_ACK
;
256 s
->status
|= PARA_STS_BUSY
;
259 parallel_update_irq(s
);
265 pdebug("read addr=0x%02x val=0x%02x\n", addr
, ret
);
269 static uint32_t parallel_ioport_read_hw(void *opaque
, uint32_t addr
)
271 ParallelState
*s
= opaque
;
276 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_DATA
, &ret
);
277 if (s
->last_read_offset
!= addr
|| s
->datar
!= ret
)
278 pdebug("rd%02x\n", ret
);
282 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &ret
);
283 ret
&= ~PARA_STS_TMOUT
;
285 ret
|= PARA_STS_TMOUT
;
286 if (s
->last_read_offset
!= addr
|| s
->status
!= ret
)
287 pdebug("rs%02x\n", ret
);
291 /* s->control has some bits fixed to 1. It is zero only when
292 it has not been yet written to. */
293 if (s
->control
== 0) {
294 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_READ_CONTROL
, &ret
);
295 if (s
->last_read_offset
!= addr
)
296 pdebug("rc%02x\n", ret
);
301 if (s
->last_read_offset
!= addr
)
302 pdebug("rc%02x\n", ret
);
305 case PARA_REG_EPP_ADDR
:
306 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
))
307 /* Controls not correct for EPP addr cycle, so do nothing */
308 pdebug("ra%02x s\n", ret
);
310 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
311 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ_ADDR
, &ioarg
)) {
313 pdebug("ra%02x t\n", ret
);
316 pdebug("ra%02x\n", ret
);
319 case PARA_REG_EPP_DATA
:
320 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
))
321 /* Controls not correct for EPP data cycle, so do nothing */
322 pdebug("re%02x s\n", ret
);
324 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
325 if (qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
)) {
327 pdebug("re%02x t\n", ret
);
330 pdebug("re%02x\n", ret
);
334 s
->last_read_offset
= addr
;
339 parallel_ioport_eppdata_read_hw2(void *opaque
, uint32_t addr
)
341 ParallelState
*s
= opaque
;
343 uint16_t eppdata
= ~0;
345 struct ParallelIOArg ioarg
= {
346 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
348 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
349 /* Controls not correct for EPP data cycle, so do nothing */
350 pdebug("re%04x s\n", eppdata
);
353 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
354 ret
= le16_to_cpu(eppdata
);
358 pdebug("re%04x t\n", ret
);
361 pdebug("re%04x\n", ret
);
366 parallel_ioport_eppdata_read_hw4(void *opaque
, uint32_t addr
)
368 ParallelState
*s
= opaque
;
370 uint32_t eppdata
= ~0U;
372 struct ParallelIOArg ioarg
= {
373 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
375 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
376 /* Controls not correct for EPP data cycle, so do nothing */
377 pdebug("re%08x s\n", eppdata
);
380 err
= qemu_chr_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
381 ret
= le32_to_cpu(eppdata
);
385 pdebug("re%08x t\n", ret
);
388 pdebug("re%08x\n", ret
);
392 static void parallel_ioport_ecp_write(void *opaque
, uint32_t addr
, uint32_t val
)
395 pdebug("wecp%d=%02x\n", addr
, val
);
398 static uint32_t parallel_ioport_ecp_read(void *opaque
, uint32_t addr
)
402 pdebug("recp%d:%02x\n", addr
, ret
);
406 static void parallel_reset(ParallelState
*s
, qemu_irq irq
, CharDriverState
*chr
)
410 s
->status
= PARA_STS_BUSY
;
411 s
->status
|= PARA_STS_ACK
;
412 s
->status
|= PARA_STS_ONLINE
;
413 s
->status
|= PARA_STS_ERROR
;
414 s
->control
= PARA_CTR_SELECT
;
415 s
->control
|= PARA_CTR_INIT
;
421 s
->last_read_offset
= ~0U;
424 /* If fd is zero, it means that the parallel device uses the console */
425 ParallelState
*parallel_init(int base
, qemu_irq irq
, CharDriverState
*chr
)
430 s
= qemu_mallocz(sizeof(ParallelState
));
433 parallel_reset(s
, irq
, chr
);
435 if (qemu_chr_ioctl(chr
, CHR_IOCTL_PP_READ_STATUS
, &dummy
) == 0) {
441 register_ioport_write(base
, 8, 1, parallel_ioport_write_hw
, s
);
442 register_ioport_read(base
, 8, 1, parallel_ioport_read_hw
, s
);
443 register_ioport_write(base
+4, 1, 2, parallel_ioport_eppdata_write_hw2
, s
);
444 register_ioport_read(base
+4, 1, 2, parallel_ioport_eppdata_read_hw2
, s
);
445 register_ioport_write(base
+4, 1, 4, parallel_ioport_eppdata_write_hw4
, s
);
446 register_ioport_read(base
+4, 1, 4, parallel_ioport_eppdata_read_hw4
, s
);
447 register_ioport_write(base
+0x400, 8, 1, parallel_ioport_ecp_write
, s
);
448 register_ioport_read(base
+0x400, 8, 1, parallel_ioport_ecp_read
, s
);
451 register_ioport_write(base
, 8, 1, parallel_ioport_write_sw
, s
);
452 register_ioport_read(base
, 8, 1, parallel_ioport_read_sw
, s
);
457 /* Memory mapped interface */
458 uint32_t parallel_mm_readb (void *opaque
, target_phys_addr_t addr
)
460 ParallelState
*s
= opaque
;
462 return parallel_ioport_read_sw(s
, (addr
- s
->base
) >> s
->it_shift
) & 0xFF;
465 void parallel_mm_writeb (void *opaque
,
466 target_phys_addr_t addr
, uint32_t value
)
468 ParallelState
*s
= opaque
;
470 parallel_ioport_write_sw(s
, (addr
- s
->base
) >> s
->it_shift
, value
& 0xFF);
473 uint32_t parallel_mm_readw (void *opaque
, target_phys_addr_t addr
)
475 ParallelState
*s
= opaque
;
477 return parallel_ioport_read_sw(s
, (addr
- s
->base
) >> s
->it_shift
) & 0xFFFF;
480 void parallel_mm_writew (void *opaque
,
481 target_phys_addr_t addr
, uint32_t value
)
483 ParallelState
*s
= opaque
;
485 parallel_ioport_write_sw(s
, (addr
- s
->base
) >> s
->it_shift
, value
& 0xFFFF);
488 uint32_t parallel_mm_readl (void *opaque
, target_phys_addr_t addr
)
490 ParallelState
*s
= opaque
;
492 return parallel_ioport_read_sw(s
, (addr
- s
->base
) >> s
->it_shift
);
495 void parallel_mm_writel (void *opaque
,
496 target_phys_addr_t addr
, uint32_t value
)
498 ParallelState
*s
= opaque
;
500 parallel_ioport_write_sw(s
, (addr
- s
->base
) >> s
->it_shift
, value
);
503 static CPUReadMemoryFunc
*parallel_mm_read_sw
[] = {
509 static CPUWriteMemoryFunc
*parallel_mm_write_sw
[] = {
515 /* If fd is zero, it means that the parallel device uses the console */
516 ParallelState
*parallel_mm_init(target_phys_addr_t base
, int it_shift
, qemu_irq irq
, CharDriverState
*chr
)
521 s
= qemu_mallocz(sizeof(ParallelState
));
524 parallel_reset(s
, irq
, chr
);
526 s
->it_shift
= it_shift
;
528 io_sw
= cpu_register_io_memory(0, parallel_mm_read_sw
, parallel_mm_write_sw
, s
);
529 cpu_register_physical_memory(base
, 8 << it_shift
, io_sw
);