Statistics
| Branch: | Revision:

root / hw / parallel.c @ bc927e48

History | View | Annotate | Download (17.9 kB)

1 6508fe59 bellard
/*
2 6508fe59 bellard
 * QEMU Parallel PORT emulation
3 5fafdf24 ths
 *
4 e57a8c0e bellard
 * Copyright (c) 2003-2005 Fabrice Bellard
5 5867c88a ths
 * Copyright (c) 2007 Marko Kohtala
6 5fafdf24 ths
 *
7 6508fe59 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 6508fe59 bellard
 * of this software and associated documentation files (the "Software"), to deal
9 6508fe59 bellard
 * in the Software without restriction, including without limitation the rights
10 6508fe59 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 6508fe59 bellard
 * copies of the Software, and to permit persons to whom the Software is
12 6508fe59 bellard
 * furnished to do so, subject to the following conditions:
13 6508fe59 bellard
 *
14 6508fe59 bellard
 * The above copyright notice and this permission notice shall be included in
15 6508fe59 bellard
 * all copies or substantial portions of the Software.
16 6508fe59 bellard
 *
17 6508fe59 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 6508fe59 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 6508fe59 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 6508fe59 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 6508fe59 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 6508fe59 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 6508fe59 bellard
 * THE SOFTWARE.
24 6508fe59 bellard
 */
25 87ecb68b pbrook
#include "hw.h"
26 87ecb68b pbrook
#include "qemu-char.h"
27 87ecb68b pbrook
#include "isa.h"
28 87ecb68b pbrook
#include "pc.h"
29 666daa68 Markus Armbruster
#include "sysemu.h"
30 6508fe59 bellard
31 6508fe59 bellard
//#define DEBUG_PARALLEL
32 6508fe59 bellard
33 5867c88a ths
#ifdef DEBUG_PARALLEL
34 001faf32 Blue Swirl
#define pdebug(fmt, ...) printf("pp: " fmt, ## __VA_ARGS__)
35 5867c88a ths
#else
36 001faf32 Blue Swirl
#define pdebug(fmt, ...) ((void)0)
37 5867c88a ths
#endif
38 5867c88a ths
39 5867c88a ths
#define PARA_REG_DATA 0
40 5867c88a ths
#define PARA_REG_STS 1
41 5867c88a ths
#define PARA_REG_CTR 2
42 5867c88a ths
#define PARA_REG_EPP_ADDR 3
43 5867c88a ths
#define PARA_REG_EPP_DATA 4
44 5867c88a ths
45 6508fe59 bellard
/*
46 6508fe59 bellard
 * These are the definitions for the Printer Status Register
47 6508fe59 bellard
 */
48 6508fe59 bellard
#define PARA_STS_BUSY        0x80        /* Busy complement */
49 6508fe59 bellard
#define PARA_STS_ACK        0x40        /* Acknowledge */
50 6508fe59 bellard
#define PARA_STS_PAPER        0x20        /* Out of paper */
51 6508fe59 bellard
#define PARA_STS_ONLINE        0x10        /* Online */
52 6508fe59 bellard
#define PARA_STS_ERROR        0x08        /* Error complement */
53 5867c88a ths
#define PARA_STS_TMOUT        0x01        /* EPP timeout */
54 6508fe59 bellard
55 6508fe59 bellard
/*
56 6508fe59 bellard
 * These are the definitions for the Printer Control Register
57 6508fe59 bellard
 */
58 5867c88a ths
#define PARA_CTR_DIR        0x20        /* Direction (1=read, 0=write) */
59 6508fe59 bellard
#define PARA_CTR_INTEN        0x10        /* IRQ Enable */
60 6508fe59 bellard
#define PARA_CTR_SELECT        0x08        /* Select In complement */
61 6508fe59 bellard
#define PARA_CTR_INIT        0x04        /* Initialize Printer complement */
62 6508fe59 bellard
#define PARA_CTR_AUTOLF        0x02        /* Auto linefeed complement */
63 6508fe59 bellard
#define PARA_CTR_STROBE        0x01        /* Strobe complement */
64 6508fe59 bellard
65 5867c88a ths
#define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
66 5867c88a ths
67 defdb20e Blue Swirl
typedef struct ParallelState {
68 63858cd9 Avi Kivity
    MemoryRegion iomem;
69 5867c88a ths
    uint8_t dataw;
70 5867c88a ths
    uint8_t datar;
71 5867c88a ths
    uint8_t status;
72 6508fe59 bellard
    uint8_t control;
73 d537cf6c pbrook
    qemu_irq irq;
74 6508fe59 bellard
    int irq_pending;
75 6508fe59 bellard
    CharDriverState *chr;
76 e57a8c0e bellard
    int hw_driver;
77 5867c88a ths
    int epp_timeout;
78 5867c88a ths
    uint32_t last_read_offset; /* For debugging */
79 d60532ca ths
    /* Memory-mapped interface */
80 d60532ca ths
    int it_shift;
81 defdb20e Blue Swirl
} ParallelState;
82 6508fe59 bellard
83 021f0674 Gerd Hoffmann
typedef struct ISAParallelState {
84 021f0674 Gerd Hoffmann
    ISADevice dev;
85 e8ee28fb Gerd Hoffmann
    uint32_t index;
86 021f0674 Gerd Hoffmann
    uint32_t iobase;
87 021f0674 Gerd Hoffmann
    uint32_t isairq;
88 021f0674 Gerd Hoffmann
    ParallelState state;
89 021f0674 Gerd Hoffmann
} ISAParallelState;
90 021f0674 Gerd Hoffmann
91 6508fe59 bellard
static void parallel_update_irq(ParallelState *s)
92 6508fe59 bellard
{
93 6508fe59 bellard
    if (s->irq_pending)
94 d537cf6c pbrook
        qemu_irq_raise(s->irq);
95 6508fe59 bellard
    else
96 d537cf6c pbrook
        qemu_irq_lower(s->irq);
97 6508fe59 bellard
}
98 6508fe59 bellard
99 5867c88a ths
static void
100 5867c88a ths
parallel_ioport_write_sw(void *opaque, uint32_t addr, uint32_t val)
101 6508fe59 bellard
{
102 6508fe59 bellard
    ParallelState *s = opaque;
103 3b46e624 ths
104 5867c88a ths
    pdebug("write addr=0x%02x val=0x%02x\n", addr, val);
105 5867c88a ths
106 5867c88a ths
    addr &= 7;
107 5867c88a ths
    switch(addr) {
108 5867c88a ths
    case PARA_REG_DATA:
109 0fa7f157 ths
        s->dataw = val;
110 0fa7f157 ths
        parallel_update_irq(s);
111 5867c88a ths
        break;
112 5867c88a ths
    case PARA_REG_CTR:
113 52ccc5e0 balrog
        val |= 0xc0;
114 0fa7f157 ths
        if ((val & PARA_CTR_INIT) == 0 ) {
115 0fa7f157 ths
            s->status = PARA_STS_BUSY;
116 0fa7f157 ths
            s->status |= PARA_STS_ACK;
117 0fa7f157 ths
            s->status |= PARA_STS_ONLINE;
118 0fa7f157 ths
            s->status |= PARA_STS_ERROR;
119 0fa7f157 ths
        }
120 0fa7f157 ths
        else if (val & PARA_CTR_SELECT) {
121 0fa7f157 ths
            if (val & PARA_CTR_STROBE) {
122 0fa7f157 ths
                s->status &= ~PARA_STS_BUSY;
123 0fa7f157 ths
                if ((s->control & PARA_CTR_STROBE) == 0)
124 2cc6e0a1 Anthony Liguori
                    qemu_chr_fe_write(s->chr, &s->dataw, 1);
125 0fa7f157 ths
            } else {
126 0fa7f157 ths
                if (s->control & PARA_CTR_INTEN) {
127 0fa7f157 ths
                    s->irq_pending = 1;
128 0fa7f157 ths
                }
129 0fa7f157 ths
            }
130 0fa7f157 ths
        }
131 0fa7f157 ths
        parallel_update_irq(s);
132 0fa7f157 ths
        s->control = val;
133 5867c88a ths
        break;
134 5867c88a ths
    }
135 5867c88a ths
}
136 5867c88a ths
137 5867c88a ths
static void parallel_ioport_write_hw(void *opaque, uint32_t addr, uint32_t val)
138 5867c88a ths
{
139 5867c88a ths
    ParallelState *s = opaque;
140 5867c88a ths
    uint8_t parm = val;
141 563e3c6e aurel32
    int dir;
142 5867c88a ths
143 5867c88a ths
    /* Sometimes programs do several writes for timing purposes on old
144 5867c88a ths
       HW. Take care not to waste time on writes that do nothing. */
145 5867c88a ths
146 5867c88a ths
    s->last_read_offset = ~0U;
147 5867c88a ths
148 6508fe59 bellard
    addr &= 7;
149 6508fe59 bellard
    switch(addr) {
150 5867c88a ths
    case PARA_REG_DATA:
151 5867c88a ths
        if (s->dataw == val)
152 0fa7f157 ths
            return;
153 0fa7f157 ths
        pdebug("wd%02x\n", val);
154 41084f1b Anthony Liguori
        qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_WRITE_DATA, &parm);
155 0fa7f157 ths
        s->dataw = val;
156 6508fe59 bellard
        break;
157 5867c88a ths
    case PARA_REG_STS:
158 0fa7f157 ths
        pdebug("ws%02x\n", val);
159 0fa7f157 ths
        if (val & PARA_STS_TMOUT)
160 0fa7f157 ths
            s->epp_timeout = 0;
161 0fa7f157 ths
        break;
162 5867c88a ths
    case PARA_REG_CTR:
163 5867c88a ths
        val |= 0xc0;
164 5867c88a ths
        if (s->control == val)
165 0fa7f157 ths
            return;
166 0fa7f157 ths
        pdebug("wc%02x\n", val);
167 563e3c6e aurel32
168 563e3c6e aurel32
        if ((val & PARA_CTR_DIR) != (s->control & PARA_CTR_DIR)) {
169 563e3c6e aurel32
            if (val & PARA_CTR_DIR) {
170 563e3c6e aurel32
                dir = 1;
171 563e3c6e aurel32
            } else {
172 563e3c6e aurel32
                dir = 0;
173 563e3c6e aurel32
            }
174 41084f1b Anthony Liguori
            qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_DATA_DIR, &dir);
175 563e3c6e aurel32
            parm &= ~PARA_CTR_DIR;
176 563e3c6e aurel32
        }
177 563e3c6e aurel32
178 41084f1b Anthony Liguori
        qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_WRITE_CONTROL, &parm);
179 0fa7f157 ths
        s->control = val;
180 6508fe59 bellard
        break;
181 5867c88a ths
    case PARA_REG_EPP_ADDR:
182 0fa7f157 ths
        if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
183 0fa7f157 ths
            /* Controls not correct for EPP address cycle, so do nothing */
184 0fa7f157 ths
            pdebug("wa%02x s\n", val);
185 0fa7f157 ths
        else {
186 0fa7f157 ths
            struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
187 41084f1b Anthony Liguori
            if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE_ADDR, &ioarg)) {
188 0fa7f157 ths
                s->epp_timeout = 1;
189 0fa7f157 ths
                pdebug("wa%02x t\n", val);
190 0fa7f157 ths
            }
191 0fa7f157 ths
            else
192 0fa7f157 ths
                pdebug("wa%02x\n", val);
193 0fa7f157 ths
        }
194 0fa7f157 ths
        break;
195 5867c88a ths
    case PARA_REG_EPP_DATA:
196 0fa7f157 ths
        if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
197 0fa7f157 ths
            /* Controls not correct for EPP data cycle, so do nothing */
198 0fa7f157 ths
            pdebug("we%02x s\n", val);
199 0fa7f157 ths
        else {
200 0fa7f157 ths
            struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
201 41084f1b Anthony Liguori
            if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg)) {
202 0fa7f157 ths
                s->epp_timeout = 1;
203 0fa7f157 ths
                pdebug("we%02x t\n", val);
204 0fa7f157 ths
            }
205 0fa7f157 ths
            else
206 0fa7f157 ths
                pdebug("we%02x\n", val);
207 0fa7f157 ths
        }
208 0fa7f157 ths
        break;
209 5867c88a ths
    }
210 5867c88a ths
}
211 5867c88a ths
212 5867c88a ths
static void
213 5867c88a ths
parallel_ioport_eppdata_write_hw2(void *opaque, uint32_t addr, uint32_t val)
214 5867c88a ths
{
215 5867c88a ths
    ParallelState *s = opaque;
216 5867c88a ths
    uint16_t eppdata = cpu_to_le16(val);
217 5867c88a ths
    int err;
218 5867c88a ths
    struct ParallelIOArg ioarg = {
219 0fa7f157 ths
        .buffer = &eppdata, .count = sizeof(eppdata)
220 5867c88a ths
    };
221 5867c88a ths
    if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
222 0fa7f157 ths
        /* Controls not correct for EPP data cycle, so do nothing */
223 0fa7f157 ths
        pdebug("we%04x s\n", val);
224 0fa7f157 ths
        return;
225 5867c88a ths
    }
226 41084f1b Anthony Liguori
    err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
227 5867c88a ths
    if (err) {
228 0fa7f157 ths
        s->epp_timeout = 1;
229 0fa7f157 ths
        pdebug("we%04x t\n", val);
230 5867c88a ths
    }
231 5867c88a ths
    else
232 0fa7f157 ths
        pdebug("we%04x\n", val);
233 5867c88a ths
}
234 5867c88a ths
235 5867c88a ths
static void
236 5867c88a ths
parallel_ioport_eppdata_write_hw4(void *opaque, uint32_t addr, uint32_t val)
237 5867c88a ths
{
238 5867c88a ths
    ParallelState *s = opaque;
239 5867c88a ths
    uint32_t eppdata = cpu_to_le32(val);
240 5867c88a ths
    int err;
241 5867c88a ths
    struct ParallelIOArg ioarg = {
242 0fa7f157 ths
        .buffer = &eppdata, .count = sizeof(eppdata)
243 5867c88a ths
    };
244 5867c88a ths
    if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
245 0fa7f157 ths
        /* Controls not correct for EPP data cycle, so do nothing */
246 0fa7f157 ths
        pdebug("we%08x s\n", val);
247 0fa7f157 ths
        return;
248 5867c88a ths
    }
249 41084f1b Anthony Liguori
    err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
250 5867c88a ths
    if (err) {
251 0fa7f157 ths
        s->epp_timeout = 1;
252 0fa7f157 ths
        pdebug("we%08x t\n", val);
253 6508fe59 bellard
    }
254 5867c88a ths
    else
255 0fa7f157 ths
        pdebug("we%08x\n", val);
256 6508fe59 bellard
}
257 6508fe59 bellard
258 5867c88a ths
static uint32_t parallel_ioport_read_sw(void *opaque, uint32_t addr)
259 6508fe59 bellard
{
260 6508fe59 bellard
    ParallelState *s = opaque;
261 6508fe59 bellard
    uint32_t ret = 0xff;
262 6508fe59 bellard
263 6508fe59 bellard
    addr &= 7;
264 6508fe59 bellard
    switch(addr) {
265 5867c88a ths
    case PARA_REG_DATA:
266 0fa7f157 ths
        if (s->control & PARA_CTR_DIR)
267 0fa7f157 ths
            ret = s->datar;
268 0fa7f157 ths
        else
269 0fa7f157 ths
            ret = s->dataw;
270 6508fe59 bellard
        break;
271 5867c88a ths
    case PARA_REG_STS:
272 0fa7f157 ths
        ret = s->status;
273 0fa7f157 ths
        s->irq_pending = 0;
274 0fa7f157 ths
        if ((s->status & PARA_STS_BUSY) == 0 && (s->control & PARA_CTR_STROBE) == 0) {
275 0fa7f157 ths
            /* XXX Fixme: wait 5 microseconds */
276 0fa7f157 ths
            if (s->status & PARA_STS_ACK)
277 0fa7f157 ths
                s->status &= ~PARA_STS_ACK;
278 0fa7f157 ths
            else {
279 0fa7f157 ths
                /* XXX Fixme: wait 5 microseconds */
280 0fa7f157 ths
                s->status |= PARA_STS_ACK;
281 0fa7f157 ths
                s->status |= PARA_STS_BUSY;
282 0fa7f157 ths
            }
283 0fa7f157 ths
        }
284 0fa7f157 ths
        parallel_update_irq(s);
285 6508fe59 bellard
        break;
286 5867c88a ths
    case PARA_REG_CTR:
287 6508fe59 bellard
        ret = s->control;
288 6508fe59 bellard
        break;
289 6508fe59 bellard
    }
290 5867c88a ths
    pdebug("read addr=0x%02x val=0x%02x\n", addr, ret);
291 5867c88a ths
    return ret;
292 5867c88a ths
}
293 5867c88a ths
294 5867c88a ths
static uint32_t parallel_ioport_read_hw(void *opaque, uint32_t addr)
295 5867c88a ths
{
296 5867c88a ths
    ParallelState *s = opaque;
297 5867c88a ths
    uint8_t ret = 0xff;
298 5867c88a ths
    addr &= 7;
299 5867c88a ths
    switch(addr) {
300 5867c88a ths
    case PARA_REG_DATA:
301 41084f1b Anthony Liguori
        qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_DATA, &ret);
302 0fa7f157 ths
        if (s->last_read_offset != addr || s->datar != ret)
303 0fa7f157 ths
            pdebug("rd%02x\n", ret);
304 5867c88a ths
        s->datar = ret;
305 5867c88a ths
        break;
306 5867c88a ths
    case PARA_REG_STS:
307 41084f1b Anthony Liguori
        qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &ret);
308 0fa7f157 ths
        ret &= ~PARA_STS_TMOUT;
309 0fa7f157 ths
        if (s->epp_timeout)
310 0fa7f157 ths
            ret |= PARA_STS_TMOUT;
311 0fa7f157 ths
        if (s->last_read_offset != addr || s->status != ret)
312 0fa7f157 ths
            pdebug("rs%02x\n", ret);
313 0fa7f157 ths
        s->status = ret;
314 5867c88a ths
        break;
315 5867c88a ths
    case PARA_REG_CTR:
316 5867c88a ths
        /* s->control has some bits fixed to 1. It is zero only when
317 0fa7f157 ths
           it has not been yet written to.  */
318 0fa7f157 ths
        if (s->control == 0) {
319 41084f1b Anthony Liguori
            qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_CONTROL, &ret);
320 0fa7f157 ths
            if (s->last_read_offset != addr)
321 0fa7f157 ths
                pdebug("rc%02x\n", ret);
322 0fa7f157 ths
            s->control = ret;
323 0fa7f157 ths
        }
324 0fa7f157 ths
        else {
325 0fa7f157 ths
            ret = s->control;
326 0fa7f157 ths
            if (s->last_read_offset != addr)
327 0fa7f157 ths
                pdebug("rc%02x\n", ret);
328 0fa7f157 ths
        }
329 5867c88a ths
        break;
330 5867c88a ths
    case PARA_REG_EPP_ADDR:
331 0fa7f157 ths
        if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT))
332 0fa7f157 ths
            /* Controls not correct for EPP addr cycle, so do nothing */
333 0fa7f157 ths
            pdebug("ra%02x s\n", ret);
334 0fa7f157 ths
        else {
335 0fa7f157 ths
            struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
336 41084f1b Anthony Liguori
            if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ_ADDR, &ioarg)) {
337 0fa7f157 ths
                s->epp_timeout = 1;
338 0fa7f157 ths
                pdebug("ra%02x t\n", ret);
339 0fa7f157 ths
            }
340 0fa7f157 ths
            else
341 0fa7f157 ths
                pdebug("ra%02x\n", ret);
342 0fa7f157 ths
        }
343 0fa7f157 ths
        break;
344 5867c88a ths
    case PARA_REG_EPP_DATA:
345 0fa7f157 ths
        if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT))
346 0fa7f157 ths
            /* Controls not correct for EPP data cycle, so do nothing */
347 0fa7f157 ths
            pdebug("re%02x s\n", ret);
348 0fa7f157 ths
        else {
349 0fa7f157 ths
            struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
350 41084f1b Anthony Liguori
            if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg)) {
351 0fa7f157 ths
                s->epp_timeout = 1;
352 0fa7f157 ths
                pdebug("re%02x t\n", ret);
353 0fa7f157 ths
            }
354 0fa7f157 ths
            else
355 0fa7f157 ths
                pdebug("re%02x\n", ret);
356 0fa7f157 ths
        }
357 0fa7f157 ths
        break;
358 5867c88a ths
    }
359 5867c88a ths
    s->last_read_offset = addr;
360 5867c88a ths
    return ret;
361 5867c88a ths
}
362 5867c88a ths
363 5867c88a ths
static uint32_t
364 5867c88a ths
parallel_ioport_eppdata_read_hw2(void *opaque, uint32_t addr)
365 5867c88a ths
{
366 5867c88a ths
    ParallelState *s = opaque;
367 5867c88a ths
    uint32_t ret;
368 5867c88a ths
    uint16_t eppdata = ~0;
369 5867c88a ths
    int err;
370 5867c88a ths
    struct ParallelIOArg ioarg = {
371 0fa7f157 ths
        .buffer = &eppdata, .count = sizeof(eppdata)
372 5867c88a ths
    };
373 5867c88a ths
    if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
374 0fa7f157 ths
        /* Controls not correct for EPP data cycle, so do nothing */
375 0fa7f157 ths
        pdebug("re%04x s\n", eppdata);
376 0fa7f157 ths
        return eppdata;
377 5867c88a ths
    }
378 41084f1b Anthony Liguori
    err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
379 5867c88a ths
    ret = le16_to_cpu(eppdata);
380 5867c88a ths
381 5867c88a ths
    if (err) {
382 0fa7f157 ths
        s->epp_timeout = 1;
383 0fa7f157 ths
        pdebug("re%04x t\n", ret);
384 5867c88a ths
    }
385 5867c88a ths
    else
386 0fa7f157 ths
        pdebug("re%04x\n", ret);
387 5867c88a ths
    return ret;
388 5867c88a ths
}
389 5867c88a ths
390 5867c88a ths
static uint32_t
391 5867c88a ths
parallel_ioport_eppdata_read_hw4(void *opaque, uint32_t addr)
392 5867c88a ths
{
393 5867c88a ths
    ParallelState *s = opaque;
394 5867c88a ths
    uint32_t ret;
395 5867c88a ths
    uint32_t eppdata = ~0U;
396 5867c88a ths
    int err;
397 5867c88a ths
    struct ParallelIOArg ioarg = {
398 0fa7f157 ths
        .buffer = &eppdata, .count = sizeof(eppdata)
399 5867c88a ths
    };
400 5867c88a ths
    if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
401 0fa7f157 ths
        /* Controls not correct for EPP data cycle, so do nothing */
402 0fa7f157 ths
        pdebug("re%08x s\n", eppdata);
403 0fa7f157 ths
        return eppdata;
404 5867c88a ths
    }
405 41084f1b Anthony Liguori
    err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
406 5867c88a ths
    ret = le32_to_cpu(eppdata);
407 5867c88a ths
408 5867c88a ths
    if (err) {
409 0fa7f157 ths
        s->epp_timeout = 1;
410 0fa7f157 ths
        pdebug("re%08x t\n", ret);
411 5867c88a ths
    }
412 5867c88a ths
    else
413 0fa7f157 ths
        pdebug("re%08x\n", ret);
414 5867c88a ths
    return ret;
415 5867c88a ths
}
416 5867c88a ths
417 5867c88a ths
static void parallel_ioport_ecp_write(void *opaque, uint32_t addr, uint32_t val)
418 5867c88a ths
{
419 7f5b7d3e Blue Swirl
    pdebug("wecp%d=%02x\n", addr & 7, val);
420 5867c88a ths
}
421 5867c88a ths
422 5867c88a ths
static uint32_t parallel_ioport_ecp_read(void *opaque, uint32_t addr)
423 5867c88a ths
{
424 5867c88a ths
    uint8_t ret = 0xff;
425 7f5b7d3e Blue Swirl
426 7f5b7d3e Blue Swirl
    pdebug("recp%d:%02x\n", addr & 7, ret);
427 6508fe59 bellard
    return ret;
428 6508fe59 bellard
}
429 6508fe59 bellard
430 33093a0a aurel32
static void parallel_reset(void *opaque)
431 6508fe59 bellard
{
432 33093a0a aurel32
    ParallelState *s = opaque;
433 33093a0a aurel32
434 5867c88a ths
    s->datar = ~0;
435 5867c88a ths
    s->dataw = ~0;
436 6508fe59 bellard
    s->status = PARA_STS_BUSY;
437 6508fe59 bellard
    s->status |= PARA_STS_ACK;
438 6508fe59 bellard
    s->status |= PARA_STS_ONLINE;
439 6508fe59 bellard
    s->status |= PARA_STS_ERROR;
440 52ccc5e0 balrog
    s->status |= PARA_STS_TMOUT;
441 6508fe59 bellard
    s->control = PARA_CTR_SELECT;
442 6508fe59 bellard
    s->control |= PARA_CTR_INIT;
443 52ccc5e0 balrog
    s->control |= 0xc0;
444 5867c88a ths
    s->irq_pending = 0;
445 5867c88a ths
    s->hw_driver = 0;
446 5867c88a ths
    s->epp_timeout = 0;
447 5867c88a ths
    s->last_read_offset = ~0U;
448 d60532ca ths
}
449 d60532ca ths
450 e8ee28fb Gerd Hoffmann
static const int isa_parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
451 e8ee28fb Gerd Hoffmann
452 1922abd0 Richard Henderson
static const MemoryRegionPortio isa_parallel_portio_hw_list[] = {
453 1922abd0 Richard Henderson
    { 0, 8, 1,
454 1922abd0 Richard Henderson
      .read = parallel_ioport_read_hw,
455 1922abd0 Richard Henderson
      .write = parallel_ioport_write_hw },
456 1922abd0 Richard Henderson
    { 4, 1, 2,
457 1922abd0 Richard Henderson
      .read = parallel_ioport_eppdata_read_hw2,
458 1922abd0 Richard Henderson
      .write = parallel_ioport_eppdata_write_hw2 },
459 1922abd0 Richard Henderson
    { 4, 1, 4,
460 1922abd0 Richard Henderson
      .read = parallel_ioport_eppdata_read_hw4,
461 1922abd0 Richard Henderson
      .write = parallel_ioport_eppdata_write_hw4 },
462 1922abd0 Richard Henderson
    { 0x400, 8, 1,
463 1922abd0 Richard Henderson
      .read = parallel_ioport_ecp_read,
464 1922abd0 Richard Henderson
      .write = parallel_ioport_ecp_write },
465 1922abd0 Richard Henderson
    PORTIO_END_OF_LIST(),
466 1922abd0 Richard Henderson
};
467 1922abd0 Richard Henderson
468 1922abd0 Richard Henderson
static const MemoryRegionPortio isa_parallel_portio_sw_list[] = {
469 1922abd0 Richard Henderson
    { 0, 8, 1,
470 1922abd0 Richard Henderson
      .read = parallel_ioport_read_sw,
471 1922abd0 Richard Henderson
      .write = parallel_ioport_write_sw },
472 1922abd0 Richard Henderson
    PORTIO_END_OF_LIST(),
473 1922abd0 Richard Henderson
};
474 1922abd0 Richard Henderson
475 021f0674 Gerd Hoffmann
static int parallel_isa_initfn(ISADevice *dev)
476 d60532ca ths
{
477 e8ee28fb Gerd Hoffmann
    static int index;
478 021f0674 Gerd Hoffmann
    ISAParallelState *isa = DO_UPCAST(ISAParallelState, dev, dev);
479 021f0674 Gerd Hoffmann
    ParallelState *s = &isa->state;
480 e8ee28fb Gerd Hoffmann
    int base;
481 d60532ca ths
    uint8_t dummy;
482 d60532ca ths
483 021f0674 Gerd Hoffmann
    if (!s->chr) {
484 021f0674 Gerd Hoffmann
        fprintf(stderr, "Can't create parallel device, empty char device\n");
485 021f0674 Gerd Hoffmann
        exit(1);
486 021f0674 Gerd Hoffmann
    }
487 021f0674 Gerd Hoffmann
488 e8ee28fb Gerd Hoffmann
    if (isa->index == -1)
489 e8ee28fb Gerd Hoffmann
        isa->index = index;
490 e8ee28fb Gerd Hoffmann
    if (isa->index >= MAX_PARALLEL_PORTS)
491 e8ee28fb Gerd Hoffmann
        return -1;
492 e8ee28fb Gerd Hoffmann
    if (isa->iobase == -1)
493 e8ee28fb Gerd Hoffmann
        isa->iobase = isa_parallel_io[isa->index];
494 e8ee28fb Gerd Hoffmann
    index++;
495 e8ee28fb Gerd Hoffmann
496 e8ee28fb Gerd Hoffmann
    base = isa->iobase;
497 021f0674 Gerd Hoffmann
    isa_init_irq(dev, &s->irq, isa->isairq);
498 a08d4367 Jan Kiszka
    qemu_register_reset(parallel_reset, s);
499 6508fe59 bellard
500 41084f1b Anthony Liguori
    if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &dummy) == 0) {
501 5867c88a ths
        s->hw_driver = 1;
502 0fa7f157 ths
        s->status = dummy;
503 5867c88a ths
    }
504 5867c88a ths
505 1922abd0 Richard Henderson
    isa_register_portio_list(dev, base,
506 1922abd0 Richard Henderson
                             (s->hw_driver
507 1922abd0 Richard Henderson
                              ? &isa_parallel_portio_hw_list[0]
508 1922abd0 Richard Henderson
                              : &isa_parallel_portio_sw_list[0]),
509 1922abd0 Richard Henderson
                             s, "parallel");
510 021f0674 Gerd Hoffmann
    return 0;
511 021f0674 Gerd Hoffmann
}
512 021f0674 Gerd Hoffmann
513 d60532ca ths
/* Memory mapped interface */
514 a8170e5e Avi Kivity
static uint32_t parallel_mm_readb (void *opaque, hwaddr addr)
515 d60532ca ths
{
516 d60532ca ths
    ParallelState *s = opaque;
517 d60532ca ths
518 8da3ff18 pbrook
    return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFF;
519 d60532ca ths
}
520 d60532ca ths
521 9596ebb7 pbrook
static void parallel_mm_writeb (void *opaque,
522 a8170e5e Avi Kivity
                                hwaddr addr, uint32_t value)
523 d60532ca ths
{
524 d60532ca ths
    ParallelState *s = opaque;
525 d60532ca ths
526 8da3ff18 pbrook
    parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFF);
527 d60532ca ths
}
528 d60532ca ths
529 a8170e5e Avi Kivity
static uint32_t parallel_mm_readw (void *opaque, hwaddr addr)
530 d60532ca ths
{
531 d60532ca ths
    ParallelState *s = opaque;
532 d60532ca ths
533 8da3ff18 pbrook
    return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFFFF;
534 d60532ca ths
}
535 d60532ca ths
536 9596ebb7 pbrook
static void parallel_mm_writew (void *opaque,
537 a8170e5e Avi Kivity
                                hwaddr addr, uint32_t value)
538 d60532ca ths
{
539 d60532ca ths
    ParallelState *s = opaque;
540 d60532ca ths
541 8da3ff18 pbrook
    parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFFFF);
542 d60532ca ths
}
543 d60532ca ths
544 a8170e5e Avi Kivity
static uint32_t parallel_mm_readl (void *opaque, hwaddr addr)
545 d60532ca ths
{
546 d60532ca ths
    ParallelState *s = opaque;
547 d60532ca ths
548 8da3ff18 pbrook
    return parallel_ioport_read_sw(s, addr >> s->it_shift);
549 d60532ca ths
}
550 d60532ca ths
551 9596ebb7 pbrook
static void parallel_mm_writel (void *opaque,
552 a8170e5e Avi Kivity
                                hwaddr addr, uint32_t value)
553 d60532ca ths
{
554 d60532ca ths
    ParallelState *s = opaque;
555 d60532ca ths
556 8da3ff18 pbrook
    parallel_ioport_write_sw(s, addr >> s->it_shift, value);
557 d60532ca ths
}
558 d60532ca ths
559 63858cd9 Avi Kivity
static const MemoryRegionOps parallel_mm_ops = {
560 63858cd9 Avi Kivity
    .old_mmio = {
561 63858cd9 Avi Kivity
        .read = { parallel_mm_readb, parallel_mm_readw, parallel_mm_readl },
562 63858cd9 Avi Kivity
        .write = { parallel_mm_writeb, parallel_mm_writew, parallel_mm_writel },
563 63858cd9 Avi Kivity
    },
564 63858cd9 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
565 d60532ca ths
};
566 d60532ca ths
567 d60532ca ths
/* If fd is zero, it means that the parallel device uses the console */
568 63858cd9 Avi Kivity
bool parallel_mm_init(MemoryRegion *address_space,
569 a8170e5e Avi Kivity
                      hwaddr base, int it_shift, qemu_irq irq,
570 defdb20e Blue Swirl
                      CharDriverState *chr)
571 d60532ca ths
{
572 d60532ca ths
    ParallelState *s;
573 d60532ca ths
574 7267c094 Anthony Liguori
    s = g_malloc0(sizeof(ParallelState));
575 33093a0a aurel32
    s->irq = irq;
576 33093a0a aurel32
    s->chr = chr;
577 d60532ca ths
    s->it_shift = it_shift;
578 a08d4367 Jan Kiszka
    qemu_register_reset(parallel_reset, s);
579 d60532ca ths
580 63858cd9 Avi Kivity
    memory_region_init_io(&s->iomem, &parallel_mm_ops, s,
581 63858cd9 Avi Kivity
                          "parallel", 8 << it_shift);
582 63858cd9 Avi Kivity
    memory_region_add_subregion(address_space, base, &s->iomem);
583 defdb20e Blue Swirl
    return true;
584 d60532ca ths
}
585 021f0674 Gerd Hoffmann
586 39bffca2 Anthony Liguori
static Property parallel_isa_properties[] = {
587 39bffca2 Anthony Liguori
    DEFINE_PROP_UINT32("index", ISAParallelState, index,   -1),
588 39bffca2 Anthony Liguori
    DEFINE_PROP_HEX32("iobase", ISAParallelState, iobase,  -1),
589 39bffca2 Anthony Liguori
    DEFINE_PROP_UINT32("irq",   ISAParallelState, isairq,  7),
590 39bffca2 Anthony Liguori
    DEFINE_PROP_CHR("chardev",  ISAParallelState, state.chr),
591 39bffca2 Anthony Liguori
    DEFINE_PROP_END_OF_LIST(),
592 39bffca2 Anthony Liguori
};
593 39bffca2 Anthony Liguori
594 8f04ee08 Anthony Liguori
static void parallel_isa_class_initfn(ObjectClass *klass, void *data)
595 8f04ee08 Anthony Liguori
{
596 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
597 8f04ee08 Anthony Liguori
    ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
598 8f04ee08 Anthony Liguori
    ic->init = parallel_isa_initfn;
599 39bffca2 Anthony Liguori
    dc->props = parallel_isa_properties;
600 8f04ee08 Anthony Liguori
}
601 8f04ee08 Anthony Liguori
602 39bffca2 Anthony Liguori
static TypeInfo parallel_isa_info = {
603 39bffca2 Anthony Liguori
    .name          = "isa-parallel",
604 39bffca2 Anthony Liguori
    .parent        = TYPE_ISA_DEVICE,
605 39bffca2 Anthony Liguori
    .instance_size = sizeof(ISAParallelState),
606 39bffca2 Anthony Liguori
    .class_init    = parallel_isa_class_initfn,
607 021f0674 Gerd Hoffmann
};
608 021f0674 Gerd Hoffmann
609 83f7d43a Andreas Färber
static void parallel_register_types(void)
610 021f0674 Gerd Hoffmann
{
611 39bffca2 Anthony Liguori
    type_register_static(&parallel_isa_info);
612 021f0674 Gerd Hoffmann
}
613 021f0674 Gerd Hoffmann
614 83f7d43a Andreas Färber
type_init(parallel_register_types)