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 typedef struct ISAParallelState {
89 static void parallel_update_irq(ParallelState *s)
92 qemu_irq_raise(s->irq);
94 qemu_irq_lower(s->irq);
98 parallel_ioport_write_sw(void *opaque, uint32_t addr, uint32_t val)
100 ParallelState *s = opaque;
102 pdebug("write addr=0x%02x val=0x%02x\n", addr, val);
108 parallel_update_irq(s);
112 if ((val & PARA_CTR_INIT) == 0 ) {
113 s->status = PARA_STS_BUSY;
114 s->status |= PARA_STS_ACK;
115 s->status |= PARA_STS_ONLINE;
116 s->status |= PARA_STS_ERROR;
118 else if (val & PARA_CTR_SELECT) {
119 if (val & PARA_CTR_STROBE) {
120 s->status &= ~PARA_STS_BUSY;
121 if ((s->control & PARA_CTR_STROBE) == 0)
122 qemu_chr_write(s->chr, &s->dataw, 1);
124 if (s->control & PARA_CTR_INTEN) {
129 parallel_update_irq(s);
135 static void parallel_ioport_write_hw(void *opaque, uint32_t addr, uint32_t val)
137 ParallelState *s = opaque;
141 /* Sometimes programs do several writes for timing purposes on old
142 HW. Take care not to waste time on writes that do nothing. */
144 s->last_read_offset = ~0U;
151 pdebug("wd%02x\n", val);
152 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_WRITE_DATA, &parm);
156 pdebug("ws%02x\n", val);
157 if (val & PARA_STS_TMOUT)
162 if (s->control == val)
164 pdebug("wc%02x\n", val);
166 if ((val & PARA_CTR_DIR) != (s->control & PARA_CTR_DIR)) {
167 if (val & PARA_CTR_DIR) {
172 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_DATA_DIR, &dir);
173 parm &= ~PARA_CTR_DIR;
176 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_WRITE_CONTROL, &parm);
179 case PARA_REG_EPP_ADDR:
180 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
181 /* Controls not correct for EPP address cycle, so do nothing */
182 pdebug("wa%02x s\n", val);
184 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
185 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE_ADDR, &ioarg)) {
187 pdebug("wa%02x t\n", val);
190 pdebug("wa%02x\n", val);
193 case PARA_REG_EPP_DATA:
194 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
195 /* Controls not correct for EPP data cycle, so do nothing */
196 pdebug("we%02x s\n", val);
198 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
199 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg)) {
201 pdebug("we%02x t\n", val);
204 pdebug("we%02x\n", val);
211 parallel_ioport_eppdata_write_hw2(void *opaque, uint32_t addr, uint32_t val)
213 ParallelState *s = opaque;
214 uint16_t eppdata = cpu_to_le16(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%04x s\n", val);
224 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
227 pdebug("we%04x t\n", val);
230 pdebug("we%04x\n", val);
234 parallel_ioport_eppdata_write_hw4(void *opaque, uint32_t addr, uint32_t val)
236 ParallelState *s = opaque;
237 uint32_t eppdata = cpu_to_le32(val);
239 struct ParallelIOArg ioarg = {
240 .buffer = &eppdata, .count = sizeof(eppdata)
242 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
243 /* Controls not correct for EPP data cycle, so do nothing */
244 pdebug("we%08x s\n", val);
247 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
250 pdebug("we%08x t\n", val);
253 pdebug("we%08x\n", val);
256 static uint32_t parallel_ioport_read_sw(void *opaque, uint32_t addr)
258 ParallelState *s = opaque;
264 if (s->control & PARA_CTR_DIR)
272 if ((s->status & PARA_STS_BUSY) == 0 && (s->control & PARA_CTR_STROBE) == 0) {
273 /* XXX Fixme: wait 5 microseconds */
274 if (s->status & PARA_STS_ACK)
275 s->status &= ~PARA_STS_ACK;
277 /* XXX Fixme: wait 5 microseconds */
278 s->status |= PARA_STS_ACK;
279 s->status |= PARA_STS_BUSY;
282 parallel_update_irq(s);
288 pdebug("read addr=0x%02x val=0x%02x\n", addr, ret);
292 static uint32_t parallel_ioport_read_hw(void *opaque, uint32_t addr)
294 ParallelState *s = opaque;
299 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_DATA, &ret);
300 if (s->last_read_offset != addr || s->datar != ret)
301 pdebug("rd%02x\n", ret);
305 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &ret);
306 ret &= ~PARA_STS_TMOUT;
308 ret |= PARA_STS_TMOUT;
309 if (s->last_read_offset != addr || s->status != ret)
310 pdebug("rs%02x\n", ret);
314 /* s->control has some bits fixed to 1. It is zero only when
315 it has not been yet written to. */
316 if (s->control == 0) {
317 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_CONTROL, &ret);
318 if (s->last_read_offset != addr)
319 pdebug("rc%02x\n", ret);
324 if (s->last_read_offset != addr)
325 pdebug("rc%02x\n", ret);
328 case PARA_REG_EPP_ADDR:
329 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT))
330 /* Controls not correct for EPP addr cycle, so do nothing */
331 pdebug("ra%02x s\n", ret);
333 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
334 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ_ADDR, &ioarg)) {
336 pdebug("ra%02x t\n", ret);
339 pdebug("ra%02x\n", ret);
342 case PARA_REG_EPP_DATA:
343 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT))
344 /* Controls not correct for EPP data cycle, so do nothing */
345 pdebug("re%02x s\n", ret);
347 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
348 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg)) {
350 pdebug("re%02x t\n", ret);
353 pdebug("re%02x\n", ret);
357 s->last_read_offset = addr;
362 parallel_ioport_eppdata_read_hw2(void *opaque, uint32_t addr)
364 ParallelState *s = opaque;
366 uint16_t eppdata = ~0;
368 struct ParallelIOArg ioarg = {
369 .buffer = &eppdata, .count = sizeof(eppdata)
371 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
372 /* Controls not correct for EPP data cycle, so do nothing */
373 pdebug("re%04x s\n", eppdata);
376 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
377 ret = le16_to_cpu(eppdata);
381 pdebug("re%04x t\n", ret);
384 pdebug("re%04x\n", ret);
389 parallel_ioport_eppdata_read_hw4(void *opaque, uint32_t addr)
391 ParallelState *s = opaque;
393 uint32_t eppdata = ~0U;
395 struct ParallelIOArg ioarg = {
396 .buffer = &eppdata, .count = sizeof(eppdata)
398 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
399 /* Controls not correct for EPP data cycle, so do nothing */
400 pdebug("re%08x s\n", eppdata);
403 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
404 ret = le32_to_cpu(eppdata);
408 pdebug("re%08x t\n", ret);
411 pdebug("re%08x\n", ret);
415 static void parallel_ioport_ecp_write(void *opaque, uint32_t addr, uint32_t val)
417 pdebug("wecp%d=%02x\n", addr & 7, val);
420 static uint32_t parallel_ioport_ecp_read(void *opaque, uint32_t addr)
424 pdebug("recp%d:%02x\n", addr & 7, ret);
428 static void parallel_reset(void *opaque)
430 ParallelState *s = opaque;
434 s->status = PARA_STS_BUSY;
435 s->status |= PARA_STS_ACK;
436 s->status |= PARA_STS_ONLINE;
437 s->status |= PARA_STS_ERROR;
438 s->status |= PARA_STS_TMOUT;
439 s->control = PARA_CTR_SELECT;
440 s->control |= PARA_CTR_INIT;
445 s->last_read_offset = ~0U;
448 static const int isa_parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
450 static int parallel_isa_initfn(ISADevice *dev)
453 ISAParallelState *isa = DO_UPCAST(ISAParallelState, dev, dev);
454 ParallelState *s = &isa->state;
459 fprintf(stderr, "Can't create parallel device, empty char device\n");
463 if (isa->index == -1)
465 if (isa->index >= MAX_PARALLEL_PORTS)
467 if (isa->iobase == -1)
468 isa->iobase = isa_parallel_io[isa->index];
472 isa_init_irq(dev, &s->irq, isa->isairq);
473 qemu_register_reset(parallel_reset, s);
475 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &dummy) == 0) {
481 register_ioport_write(base, 8, 1, parallel_ioport_write_hw, s);
482 register_ioport_read(base, 8, 1, parallel_ioport_read_hw, s);
483 register_ioport_write(base+4, 1, 2, parallel_ioport_eppdata_write_hw2, s);
484 register_ioport_read(base+4, 1, 2, parallel_ioport_eppdata_read_hw2, s);
485 register_ioport_write(base+4, 1, 4, parallel_ioport_eppdata_write_hw4, s);
486 register_ioport_read(base+4, 1, 4, parallel_ioport_eppdata_read_hw4, s);
487 register_ioport_write(base+0x400, 8, 1, parallel_ioport_ecp_write, s);
488 register_ioport_read(base+0x400, 8, 1, parallel_ioport_ecp_read, s);
491 register_ioport_write(base, 8, 1, parallel_ioport_write_sw, s);
492 register_ioport_read(base, 8, 1, parallel_ioport_read_sw, s);
497 ParallelState *parallel_init(int index, CharDriverState *chr)
501 dev = isa_create("isa-parallel");
502 qdev_prop_set_uint32(&dev->qdev, "index", index);
503 qdev_prop_set_chr(&dev->qdev, "chardev", chr);
504 if (qdev_init(&dev->qdev) < 0)
506 return &DO_UPCAST(ISAParallelState, dev, dev)->state;
509 /* Memory mapped interface */
510 static uint32_t parallel_mm_readb (void *opaque, target_phys_addr_t addr)
512 ParallelState *s = opaque;
514 return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFF;
517 static void parallel_mm_writeb (void *opaque,
518 target_phys_addr_t addr, uint32_t value)
520 ParallelState *s = opaque;
522 parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFF);
525 static uint32_t parallel_mm_readw (void *opaque, target_phys_addr_t addr)
527 ParallelState *s = opaque;
529 return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFFFF;
532 static void parallel_mm_writew (void *opaque,
533 target_phys_addr_t addr, uint32_t value)
535 ParallelState *s = opaque;
537 parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFFFF);
540 static uint32_t parallel_mm_readl (void *opaque, target_phys_addr_t addr)
542 ParallelState *s = opaque;
544 return parallel_ioport_read_sw(s, addr >> s->it_shift);
547 static void parallel_mm_writel (void *opaque,
548 target_phys_addr_t addr, uint32_t value)
550 ParallelState *s = opaque;
552 parallel_ioport_write_sw(s, addr >> s->it_shift, value);
555 static CPUReadMemoryFunc * const parallel_mm_read_sw[] = {
561 static CPUWriteMemoryFunc * const parallel_mm_write_sw[] = {
567 /* If fd is zero, it means that the parallel device uses the console */
568 ParallelState *parallel_mm_init(target_phys_addr_t base, int it_shift, qemu_irq irq, CharDriverState *chr)
573 s = qemu_mallocz(sizeof(ParallelState));
576 s->it_shift = it_shift;
577 qemu_register_reset(parallel_reset, s);
579 io_sw = cpu_register_io_memory(parallel_mm_read_sw, parallel_mm_write_sw, s);
580 cpu_register_physical_memory(base, 8 << it_shift, io_sw);
584 static ISADeviceInfo parallel_isa_info = {
585 .qdev.name = "isa-parallel",
586 .qdev.size = sizeof(ISAParallelState),
587 .init = parallel_isa_initfn,
588 .qdev.props = (Property[]) {
589 DEFINE_PROP_UINT32("index", ISAParallelState, index, -1),
590 DEFINE_PROP_HEX32("iobase", ISAParallelState, iobase, -1),
591 DEFINE_PROP_UINT32("irq", ISAParallelState, isairq, 7),
592 DEFINE_PROP_CHR("chardev", ISAParallelState, state.chr),
593 DEFINE_PROP_END_OF_LIST(),
597 static void parallel_register_devices(void)
599 isa_qdev_register(¶llel_isa_info);
602 device_init(parallel_register_devices)