blob: c2b553f0d19206ebc1dad2539a6c2b91fae414b8 [file] [log] [blame]
bellard6508fe52005-01-15 12:02:56 +00001/*
2 * QEMU Parallel PORT emulation
ths5fafdf22007-09-16 21:08:06 +00003 *
bellarde57a8c02005-11-10 23:58:52 +00004 * Copyright (c) 2003-2005 Fabrice Bellard
ths5867c882007-02-17 23:44:43 +00005 * Copyright (c) 2007 Marko Kohtala
ths5fafdf22007-09-16 21:08:06 +00006 *
bellard6508fe52005-01-15 12:02:56 +00007 * 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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 */
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010025#include "hw/hw.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020026#include "sysemu/char.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010027#include "hw/isa/isa.h"
28#include "hw/i386/pc.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010029#include "sysemu/sysemu.h"
bellard6508fe52005-01-15 12:02:56 +000030
31//#define DEBUG_PARALLEL
32
ths5867c882007-02-17 23:44:43 +000033#ifdef DEBUG_PARALLEL
Blue Swirl001faf32009-05-13 17:53:17 +000034#define pdebug(fmt, ...) printf("pp: " fmt, ## __VA_ARGS__)
ths5867c882007-02-17 23:44:43 +000035#else
Blue Swirl001faf32009-05-13 17:53:17 +000036#define pdebug(fmt, ...) ((void)0)
ths5867c882007-02-17 23:44:43 +000037#endif
38
39#define PARA_REG_DATA 0
40#define PARA_REG_STS 1
41#define PARA_REG_CTR 2
42#define PARA_REG_EPP_ADDR 3
43#define PARA_REG_EPP_DATA 4
44
bellard6508fe52005-01-15 12:02:56 +000045/*
46 * These are the definitions for the Printer Status Register
47 */
48#define PARA_STS_BUSY 0x80 /* Busy complement */
49#define PARA_STS_ACK 0x40 /* Acknowledge */
50#define PARA_STS_PAPER 0x20 /* Out of paper */
51#define PARA_STS_ONLINE 0x10 /* Online */
52#define PARA_STS_ERROR 0x08 /* Error complement */
ths5867c882007-02-17 23:44:43 +000053#define PARA_STS_TMOUT 0x01 /* EPP timeout */
bellard6508fe52005-01-15 12:02:56 +000054
55/*
56 * These are the definitions for the Printer Control Register
57 */
ths5867c882007-02-17 23:44:43 +000058#define PARA_CTR_DIR 0x20 /* Direction (1=read, 0=write) */
bellard6508fe52005-01-15 12:02:56 +000059#define PARA_CTR_INTEN 0x10 /* IRQ Enable */
60#define PARA_CTR_SELECT 0x08 /* Select In complement */
61#define PARA_CTR_INIT 0x04 /* Initialize Printer complement */
62#define PARA_CTR_AUTOLF 0x02 /* Auto linefeed complement */
63#define PARA_CTR_STROBE 0x01 /* Strobe complement */
64
ths5867c882007-02-17 23:44:43 +000065#define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
66
Blue Swirldefdb202011-02-05 14:51:57 +000067typedef struct ParallelState {
Avi Kivity63858cd2011-10-06 16:44:26 +020068 MemoryRegion iomem;
ths5867c882007-02-17 23:44:43 +000069 uint8_t dataw;
70 uint8_t datar;
71 uint8_t status;
bellard6508fe52005-01-15 12:02:56 +000072 uint8_t control;
pbrookd537cf62007-04-07 18:14:41 +000073 qemu_irq irq;
bellard6508fe52005-01-15 12:02:56 +000074 int irq_pending;
75 CharDriverState *chr;
bellarde57a8c02005-11-10 23:58:52 +000076 int hw_driver;
ths5867c882007-02-17 23:44:43 +000077 int epp_timeout;
78 uint32_t last_read_offset; /* For debugging */
thsd60532c2007-06-18 18:55:46 +000079 /* Memory-mapped interface */
thsd60532c2007-06-18 18:55:46 +000080 int it_shift;
Blue Swirldefdb202011-02-05 14:51:57 +000081} ParallelState;
bellard6508fe52005-01-15 12:02:56 +000082
Andreas Färberb0dc5ee2013-04-27 22:18:45 +020083#define TYPE_ISA_PARALLEL "isa-parallel"
84#define ISA_PARALLEL(obj) \
85 OBJECT_CHECK(ISAParallelState, (obj), TYPE_ISA_PARALLEL)
86
Gerd Hoffmann021f0672009-09-22 13:53:22 +020087typedef struct ISAParallelState {
Andreas Färberb0dc5ee2013-04-27 22:18:45 +020088 ISADevice parent_obj;
89
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +020090 uint32_t index;
Gerd Hoffmann021f0672009-09-22 13:53:22 +020091 uint32_t iobase;
92 uint32_t isairq;
93 ParallelState state;
94} ISAParallelState;
95
bellard6508fe52005-01-15 12:02:56 +000096static void parallel_update_irq(ParallelState *s)
97{
98 if (s->irq_pending)
pbrookd537cf62007-04-07 18:14:41 +000099 qemu_irq_raise(s->irq);
bellard6508fe52005-01-15 12:02:56 +0000100 else
pbrookd537cf62007-04-07 18:14:41 +0000101 qemu_irq_lower(s->irq);
bellard6508fe52005-01-15 12:02:56 +0000102}
103
ths5867c882007-02-17 23:44:43 +0000104static void
105parallel_ioport_write_sw(void *opaque, uint32_t addr, uint32_t val)
bellard6508fe52005-01-15 12:02:56 +0000106{
107 ParallelState *s = opaque;
ths3b46e622007-09-17 08:09:54 +0000108
ths5867c882007-02-17 23:44:43 +0000109 pdebug("write addr=0x%02x val=0x%02x\n", addr, val);
110
bellard6508fe52005-01-15 12:02:56 +0000111 addr &= 7;
bellard6508fe52005-01-15 12:02:56 +0000112 switch(addr) {
ths5867c882007-02-17 23:44:43 +0000113 case PARA_REG_DATA:
ths0fa7f152007-06-07 21:07:11 +0000114 s->dataw = val;
115 parallel_update_irq(s);
bellard6508fe52005-01-15 12:02:56 +0000116 break;
ths5867c882007-02-17 23:44:43 +0000117 case PARA_REG_CTR:
balrog52ccc5e2008-02-10 13:34:48 +0000118 val |= 0xc0;
ths0fa7f152007-06-07 21:07:11 +0000119 if ((val & PARA_CTR_INIT) == 0 ) {
120 s->status = PARA_STS_BUSY;
121 s->status |= PARA_STS_ACK;
122 s->status |= PARA_STS_ONLINE;
123 s->status |= PARA_STS_ERROR;
124 }
125 else if (val & PARA_CTR_SELECT) {
126 if (val & PARA_CTR_STROBE) {
127 s->status &= ~PARA_STS_BUSY;
128 if ((s->control & PARA_CTR_STROBE) == 0)
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500129 qemu_chr_fe_write(s->chr, &s->dataw, 1);
ths0fa7f152007-06-07 21:07:11 +0000130 } else {
131 if (s->control & PARA_CTR_INTEN) {
132 s->irq_pending = 1;
133 }
134 }
135 }
136 parallel_update_irq(s);
137 s->control = val;
bellard6508fe52005-01-15 12:02:56 +0000138 break;
139 }
140}
141
ths5867c882007-02-17 23:44:43 +0000142static void parallel_ioport_write_hw(void *opaque, uint32_t addr, uint32_t val)
143{
144 ParallelState *s = opaque;
145 uint8_t parm = val;
aurel32563e3c62008-08-22 08:57:09 +0000146 int dir;
ths5867c882007-02-17 23:44:43 +0000147
148 /* Sometimes programs do several writes for timing purposes on old
149 HW. Take care not to waste time on writes that do nothing. */
150
151 s->last_read_offset = ~0U;
152
153 addr &= 7;
154 switch(addr) {
155 case PARA_REG_DATA:
156 if (s->dataw == val)
ths0fa7f152007-06-07 21:07:11 +0000157 return;
158 pdebug("wd%02x\n", val);
Anthony Liguori41084f12011-08-15 11:17:34 -0500159 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_WRITE_DATA, &parm);
ths0fa7f152007-06-07 21:07:11 +0000160 s->dataw = val;
ths5867c882007-02-17 23:44:43 +0000161 break;
162 case PARA_REG_STS:
ths0fa7f152007-06-07 21:07:11 +0000163 pdebug("ws%02x\n", val);
164 if (val & PARA_STS_TMOUT)
165 s->epp_timeout = 0;
166 break;
ths5867c882007-02-17 23:44:43 +0000167 case PARA_REG_CTR:
168 val |= 0xc0;
169 if (s->control == val)
ths0fa7f152007-06-07 21:07:11 +0000170 return;
171 pdebug("wc%02x\n", val);
aurel32563e3c62008-08-22 08:57:09 +0000172
173 if ((val & PARA_CTR_DIR) != (s->control & PARA_CTR_DIR)) {
174 if (val & PARA_CTR_DIR) {
175 dir = 1;
176 } else {
177 dir = 0;
178 }
Anthony Liguori41084f12011-08-15 11:17:34 -0500179 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_DATA_DIR, &dir);
aurel32563e3c62008-08-22 08:57:09 +0000180 parm &= ~PARA_CTR_DIR;
181 }
182
Anthony Liguori41084f12011-08-15 11:17:34 -0500183 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_WRITE_CONTROL, &parm);
ths0fa7f152007-06-07 21:07:11 +0000184 s->control = val;
ths5867c882007-02-17 23:44:43 +0000185 break;
186 case PARA_REG_EPP_ADDR:
ths0fa7f152007-06-07 21:07:11 +0000187 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
188 /* Controls not correct for EPP address cycle, so do nothing */
189 pdebug("wa%02x s\n", val);
190 else {
191 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
Anthony Liguori41084f12011-08-15 11:17:34 -0500192 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE_ADDR, &ioarg)) {
ths0fa7f152007-06-07 21:07:11 +0000193 s->epp_timeout = 1;
194 pdebug("wa%02x t\n", val);
195 }
196 else
197 pdebug("wa%02x\n", val);
198 }
199 break;
ths5867c882007-02-17 23:44:43 +0000200 case PARA_REG_EPP_DATA:
ths0fa7f152007-06-07 21:07:11 +0000201 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
202 /* Controls not correct for EPP data cycle, so do nothing */
203 pdebug("we%02x s\n", val);
204 else {
205 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
Anthony Liguori41084f12011-08-15 11:17:34 -0500206 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg)) {
ths0fa7f152007-06-07 21:07:11 +0000207 s->epp_timeout = 1;
208 pdebug("we%02x t\n", val);
209 }
210 else
211 pdebug("we%02x\n", val);
212 }
213 break;
ths5867c882007-02-17 23:44:43 +0000214 }
215}
216
217static void
218parallel_ioport_eppdata_write_hw2(void *opaque, uint32_t addr, uint32_t val)
219{
220 ParallelState *s = opaque;
221 uint16_t eppdata = cpu_to_le16(val);
222 int err;
223 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000224 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000225 };
226 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
ths0fa7f152007-06-07 21:07:11 +0000227 /* Controls not correct for EPP data cycle, so do nothing */
228 pdebug("we%04x s\n", val);
229 return;
ths5867c882007-02-17 23:44:43 +0000230 }
Anthony Liguori41084f12011-08-15 11:17:34 -0500231 err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
ths5867c882007-02-17 23:44:43 +0000232 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000233 s->epp_timeout = 1;
234 pdebug("we%04x t\n", val);
ths5867c882007-02-17 23:44:43 +0000235 }
236 else
ths0fa7f152007-06-07 21:07:11 +0000237 pdebug("we%04x\n", val);
ths5867c882007-02-17 23:44:43 +0000238}
239
240static void
241parallel_ioport_eppdata_write_hw4(void *opaque, uint32_t addr, uint32_t val)
242{
243 ParallelState *s = opaque;
244 uint32_t eppdata = cpu_to_le32(val);
245 int err;
246 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000247 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000248 };
249 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
ths0fa7f152007-06-07 21:07:11 +0000250 /* Controls not correct for EPP data cycle, so do nothing */
251 pdebug("we%08x s\n", val);
252 return;
ths5867c882007-02-17 23:44:43 +0000253 }
Anthony Liguori41084f12011-08-15 11:17:34 -0500254 err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
ths5867c882007-02-17 23:44:43 +0000255 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000256 s->epp_timeout = 1;
257 pdebug("we%08x t\n", val);
ths5867c882007-02-17 23:44:43 +0000258 }
259 else
ths0fa7f152007-06-07 21:07:11 +0000260 pdebug("we%08x\n", val);
ths5867c882007-02-17 23:44:43 +0000261}
262
263static uint32_t parallel_ioport_read_sw(void *opaque, uint32_t addr)
bellard6508fe52005-01-15 12:02:56 +0000264{
265 ParallelState *s = opaque;
266 uint32_t ret = 0xff;
267
268 addr &= 7;
269 switch(addr) {
ths5867c882007-02-17 23:44:43 +0000270 case PARA_REG_DATA:
ths0fa7f152007-06-07 21:07:11 +0000271 if (s->control & PARA_CTR_DIR)
272 ret = s->datar;
273 else
274 ret = s->dataw;
bellard6508fe52005-01-15 12:02:56 +0000275 break;
ths5867c882007-02-17 23:44:43 +0000276 case PARA_REG_STS:
ths0fa7f152007-06-07 21:07:11 +0000277 ret = s->status;
278 s->irq_pending = 0;
279 if ((s->status & PARA_STS_BUSY) == 0 && (s->control & PARA_CTR_STROBE) == 0) {
280 /* XXX Fixme: wait 5 microseconds */
281 if (s->status & PARA_STS_ACK)
282 s->status &= ~PARA_STS_ACK;
283 else {
284 /* XXX Fixme: wait 5 microseconds */
285 s->status |= PARA_STS_ACK;
286 s->status |= PARA_STS_BUSY;
287 }
288 }
289 parallel_update_irq(s);
bellard6508fe52005-01-15 12:02:56 +0000290 break;
ths5867c882007-02-17 23:44:43 +0000291 case PARA_REG_CTR:
bellard6508fe52005-01-15 12:02:56 +0000292 ret = s->control;
293 break;
294 }
ths5867c882007-02-17 23:44:43 +0000295 pdebug("read addr=0x%02x val=0x%02x\n", addr, ret);
296 return ret;
297}
298
299static uint32_t parallel_ioport_read_hw(void *opaque, uint32_t addr)
300{
301 ParallelState *s = opaque;
302 uint8_t ret = 0xff;
303 addr &= 7;
304 switch(addr) {
305 case PARA_REG_DATA:
Anthony Liguori41084f12011-08-15 11:17:34 -0500306 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_DATA, &ret);
ths0fa7f152007-06-07 21:07:11 +0000307 if (s->last_read_offset != addr || s->datar != ret)
308 pdebug("rd%02x\n", ret);
ths5867c882007-02-17 23:44:43 +0000309 s->datar = ret;
310 break;
311 case PARA_REG_STS:
Anthony Liguori41084f12011-08-15 11:17:34 -0500312 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &ret);
ths0fa7f152007-06-07 21:07:11 +0000313 ret &= ~PARA_STS_TMOUT;
314 if (s->epp_timeout)
315 ret |= PARA_STS_TMOUT;
316 if (s->last_read_offset != addr || s->status != ret)
317 pdebug("rs%02x\n", ret);
318 s->status = ret;
ths5867c882007-02-17 23:44:43 +0000319 break;
320 case PARA_REG_CTR:
321 /* s->control has some bits fixed to 1. It is zero only when
ths0fa7f152007-06-07 21:07:11 +0000322 it has not been yet written to. */
323 if (s->control == 0) {
Anthony Liguori41084f12011-08-15 11:17:34 -0500324 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_CONTROL, &ret);
ths0fa7f152007-06-07 21:07:11 +0000325 if (s->last_read_offset != addr)
326 pdebug("rc%02x\n", ret);
327 s->control = ret;
328 }
329 else {
330 ret = s->control;
331 if (s->last_read_offset != addr)
332 pdebug("rc%02x\n", ret);
333 }
ths5867c882007-02-17 23:44:43 +0000334 break;
335 case PARA_REG_EPP_ADDR:
ths0fa7f152007-06-07 21:07:11 +0000336 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT))
337 /* Controls not correct for EPP addr cycle, so do nothing */
338 pdebug("ra%02x s\n", ret);
339 else {
340 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
Anthony Liguori41084f12011-08-15 11:17:34 -0500341 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ_ADDR, &ioarg)) {
ths0fa7f152007-06-07 21:07:11 +0000342 s->epp_timeout = 1;
343 pdebug("ra%02x t\n", ret);
344 }
345 else
346 pdebug("ra%02x\n", ret);
347 }
348 break;
ths5867c882007-02-17 23:44:43 +0000349 case PARA_REG_EPP_DATA:
ths0fa7f152007-06-07 21:07:11 +0000350 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT))
351 /* Controls not correct for EPP data cycle, so do nothing */
352 pdebug("re%02x s\n", ret);
353 else {
354 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
Anthony Liguori41084f12011-08-15 11:17:34 -0500355 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg)) {
ths0fa7f152007-06-07 21:07:11 +0000356 s->epp_timeout = 1;
357 pdebug("re%02x t\n", ret);
358 }
359 else
360 pdebug("re%02x\n", ret);
361 }
362 break;
ths5867c882007-02-17 23:44:43 +0000363 }
364 s->last_read_offset = addr;
365 return ret;
366}
367
368static uint32_t
369parallel_ioport_eppdata_read_hw2(void *opaque, uint32_t addr)
370{
371 ParallelState *s = opaque;
372 uint32_t ret;
373 uint16_t eppdata = ~0;
374 int err;
375 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000376 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000377 };
378 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
ths0fa7f152007-06-07 21:07:11 +0000379 /* Controls not correct for EPP data cycle, so do nothing */
380 pdebug("re%04x s\n", eppdata);
381 return eppdata;
ths5867c882007-02-17 23:44:43 +0000382 }
Anthony Liguori41084f12011-08-15 11:17:34 -0500383 err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
ths5867c882007-02-17 23:44:43 +0000384 ret = le16_to_cpu(eppdata);
385
386 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000387 s->epp_timeout = 1;
388 pdebug("re%04x t\n", ret);
ths5867c882007-02-17 23:44:43 +0000389 }
390 else
ths0fa7f152007-06-07 21:07:11 +0000391 pdebug("re%04x\n", ret);
ths5867c882007-02-17 23:44:43 +0000392 return ret;
393}
394
395static uint32_t
396parallel_ioport_eppdata_read_hw4(void *opaque, uint32_t addr)
397{
398 ParallelState *s = opaque;
399 uint32_t ret;
400 uint32_t eppdata = ~0U;
401 int err;
402 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000403 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000404 };
405 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
ths0fa7f152007-06-07 21:07:11 +0000406 /* Controls not correct for EPP data cycle, so do nothing */
407 pdebug("re%08x s\n", eppdata);
408 return eppdata;
ths5867c882007-02-17 23:44:43 +0000409 }
Anthony Liguori41084f12011-08-15 11:17:34 -0500410 err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
ths5867c882007-02-17 23:44:43 +0000411 ret = le32_to_cpu(eppdata);
412
413 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000414 s->epp_timeout = 1;
415 pdebug("re%08x t\n", ret);
ths5867c882007-02-17 23:44:43 +0000416 }
417 else
ths0fa7f152007-06-07 21:07:11 +0000418 pdebug("re%08x\n", ret);
ths5867c882007-02-17 23:44:43 +0000419 return ret;
420}
421
422static void parallel_ioport_ecp_write(void *opaque, uint32_t addr, uint32_t val)
423{
Blue Swirl7f5b7d32010-04-25 18:58:25 +0000424 pdebug("wecp%d=%02x\n", addr & 7, val);
ths5867c882007-02-17 23:44:43 +0000425}
426
427static uint32_t parallel_ioport_ecp_read(void *opaque, uint32_t addr)
428{
429 uint8_t ret = 0xff;
Blue Swirl7f5b7d32010-04-25 18:58:25 +0000430
431 pdebug("recp%d:%02x\n", addr & 7, ret);
bellard6508fe52005-01-15 12:02:56 +0000432 return ret;
433}
434
aurel3233093a02008-12-07 23:26:09 +0000435static void parallel_reset(void *opaque)
bellard6508fe52005-01-15 12:02:56 +0000436{
aurel3233093a02008-12-07 23:26:09 +0000437 ParallelState *s = opaque;
438
ths5867c882007-02-17 23:44:43 +0000439 s->datar = ~0;
440 s->dataw = ~0;
bellard6508fe52005-01-15 12:02:56 +0000441 s->status = PARA_STS_BUSY;
442 s->status |= PARA_STS_ACK;
443 s->status |= PARA_STS_ONLINE;
444 s->status |= PARA_STS_ERROR;
balrog52ccc5e2008-02-10 13:34:48 +0000445 s->status |= PARA_STS_TMOUT;
bellard6508fe52005-01-15 12:02:56 +0000446 s->control = PARA_CTR_SELECT;
447 s->control |= PARA_CTR_INIT;
balrog52ccc5e2008-02-10 13:34:48 +0000448 s->control |= 0xc0;
ths5867c882007-02-17 23:44:43 +0000449 s->irq_pending = 0;
ths5867c882007-02-17 23:44:43 +0000450 s->hw_driver = 0;
451 s->epp_timeout = 0;
452 s->last_read_offset = ~0U;
thsd60532c2007-06-18 18:55:46 +0000453}
454
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200455static const int isa_parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
456
Richard Henderson1922abd2011-08-15 15:55:09 -0700457static const MemoryRegionPortio isa_parallel_portio_hw_list[] = {
458 { 0, 8, 1,
459 .read = parallel_ioport_read_hw,
460 .write = parallel_ioport_write_hw },
461 { 4, 1, 2,
462 .read = parallel_ioport_eppdata_read_hw2,
463 .write = parallel_ioport_eppdata_write_hw2 },
464 { 4, 1, 4,
465 .read = parallel_ioport_eppdata_read_hw4,
466 .write = parallel_ioport_eppdata_write_hw4 },
467 { 0x400, 8, 1,
468 .read = parallel_ioport_ecp_read,
469 .write = parallel_ioport_ecp_write },
470 PORTIO_END_OF_LIST(),
471};
472
473static const MemoryRegionPortio isa_parallel_portio_sw_list[] = {
474 { 0, 8, 1,
475 .read = parallel_ioport_read_sw,
476 .write = parallel_ioport_write_sw },
477 PORTIO_END_OF_LIST(),
478};
479
Pavel Dovgalyuk461a2752014-08-28 15:18:46 +0400480
481static const VMStateDescription vmstate_parallel_isa = {
482 .name = "parallel_isa",
483 .version_id = 1,
484 .minimum_version_id = 1,
485 .fields = (VMStateField[]) {
486 VMSTATE_UINT8(state.dataw, ISAParallelState),
487 VMSTATE_UINT8(state.datar, ISAParallelState),
488 VMSTATE_UINT8(state.status, ISAParallelState),
489 VMSTATE_UINT8(state.control, ISAParallelState),
490 VMSTATE_INT32(state.irq_pending, ISAParallelState),
491 VMSTATE_INT32(state.epp_timeout, ISAParallelState),
492 VMSTATE_END_OF_LIST()
493 }
494};
495
496
Andreas Färberdb895a12012-11-25 02:37:14 +0100497static void parallel_isa_realizefn(DeviceState *dev, Error **errp)
thsd60532c2007-06-18 18:55:46 +0000498{
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200499 static int index;
Andreas Färberdb895a12012-11-25 02:37:14 +0100500 ISADevice *isadev = ISA_DEVICE(dev);
Andreas Färberb0dc5ee2013-04-27 22:18:45 +0200501 ISAParallelState *isa = ISA_PARALLEL(dev);
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200502 ParallelState *s = &isa->state;
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200503 int base;
thsd60532c2007-06-18 18:55:46 +0000504 uint8_t dummy;
505
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200506 if (!s->chr) {
Andreas Färberdb895a12012-11-25 02:37:14 +0100507 error_setg(errp, "Can't create parallel device, empty char device");
508 return;
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200509 }
510
Andreas Färberdb895a12012-11-25 02:37:14 +0100511 if (isa->index == -1) {
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200512 isa->index = index;
Andreas Färberdb895a12012-11-25 02:37:14 +0100513 }
514 if (isa->index >= MAX_PARALLEL_PORTS) {
515 error_setg(errp, "Max. supported number of parallel ports is %d.",
516 MAX_PARALLEL_PORTS);
517 return;
518 }
519 if (isa->iobase == -1) {
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200520 isa->iobase = isa_parallel_io[isa->index];
Andreas Färberdb895a12012-11-25 02:37:14 +0100521 }
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200522 index++;
523
524 base = isa->iobase;
Andreas Färberdb895a12012-11-25 02:37:14 +0100525 isa_init_irq(isadev, &s->irq, isa->isairq);
Jan Kiszkaa08d4362009-06-27 09:25:07 +0200526 qemu_register_reset(parallel_reset, s);
bellard6508fe52005-01-15 12:02:56 +0000527
Anthony Liguori41084f12011-08-15 11:17:34 -0500528 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &dummy) == 0) {
ths5867c882007-02-17 23:44:43 +0000529 s->hw_driver = 1;
ths0fa7f152007-06-07 21:07:11 +0000530 s->status = dummy;
ths5867c882007-02-17 23:44:43 +0000531 }
532
Andreas Färberdb895a12012-11-25 02:37:14 +0100533 isa_register_portio_list(isadev, base,
Richard Henderson1922abd2011-08-15 15:55:09 -0700534 (s->hw_driver
535 ? &isa_parallel_portio_hw_list[0]
536 : &isa_parallel_portio_sw_list[0]),
537 s, "parallel");
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200538}
539
thsd60532c2007-06-18 18:55:46 +0000540/* Memory mapped interface */
Avi Kivitya8170e52012-10-23 12:30:10 +0200541static uint32_t parallel_mm_readb (void *opaque, hwaddr addr)
thsd60532c2007-06-18 18:55:46 +0000542{
543 ParallelState *s = opaque;
544
pbrook8da3ff12008-12-01 18:59:50 +0000545 return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFF;
thsd60532c2007-06-18 18:55:46 +0000546}
547
pbrook9596ebb2007-11-18 01:44:38 +0000548static void parallel_mm_writeb (void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +0200549 hwaddr addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000550{
551 ParallelState *s = opaque;
552
pbrook8da3ff12008-12-01 18:59:50 +0000553 parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFF);
thsd60532c2007-06-18 18:55:46 +0000554}
555
Avi Kivitya8170e52012-10-23 12:30:10 +0200556static uint32_t parallel_mm_readw (void *opaque, hwaddr addr)
thsd60532c2007-06-18 18:55:46 +0000557{
558 ParallelState *s = opaque;
559
pbrook8da3ff12008-12-01 18:59:50 +0000560 return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFFFF;
thsd60532c2007-06-18 18:55:46 +0000561}
562
pbrook9596ebb2007-11-18 01:44:38 +0000563static void parallel_mm_writew (void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +0200564 hwaddr addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000565{
566 ParallelState *s = opaque;
567
pbrook8da3ff12008-12-01 18:59:50 +0000568 parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFFFF);
thsd60532c2007-06-18 18:55:46 +0000569}
570
Avi Kivitya8170e52012-10-23 12:30:10 +0200571static uint32_t parallel_mm_readl (void *opaque, hwaddr addr)
thsd60532c2007-06-18 18:55:46 +0000572{
573 ParallelState *s = opaque;
574
pbrook8da3ff12008-12-01 18:59:50 +0000575 return parallel_ioport_read_sw(s, addr >> s->it_shift);
thsd60532c2007-06-18 18:55:46 +0000576}
577
pbrook9596ebb2007-11-18 01:44:38 +0000578static void parallel_mm_writel (void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +0200579 hwaddr addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000580{
581 ParallelState *s = opaque;
582
pbrook8da3ff12008-12-01 18:59:50 +0000583 parallel_ioport_write_sw(s, addr >> s->it_shift, value);
thsd60532c2007-06-18 18:55:46 +0000584}
585
Avi Kivity63858cd2011-10-06 16:44:26 +0200586static const MemoryRegionOps parallel_mm_ops = {
587 .old_mmio = {
588 .read = { parallel_mm_readb, parallel_mm_readw, parallel_mm_readl },
589 .write = { parallel_mm_writeb, parallel_mm_writew, parallel_mm_writel },
590 },
591 .endianness = DEVICE_NATIVE_ENDIAN,
thsd60532c2007-06-18 18:55:46 +0000592};
593
594/* If fd is zero, it means that the parallel device uses the console */
Avi Kivity63858cd2011-10-06 16:44:26 +0200595bool parallel_mm_init(MemoryRegion *address_space,
Avi Kivitya8170e52012-10-23 12:30:10 +0200596 hwaddr base, int it_shift, qemu_irq irq,
Blue Swirldefdb202011-02-05 14:51:57 +0000597 CharDriverState *chr)
thsd60532c2007-06-18 18:55:46 +0000598{
599 ParallelState *s;
thsd60532c2007-06-18 18:55:46 +0000600
Anthony Liguori7267c092011-08-20 22:09:37 -0500601 s = g_malloc0(sizeof(ParallelState));
aurel3233093a02008-12-07 23:26:09 +0000602 s->irq = irq;
603 s->chr = chr;
thsd60532c2007-06-18 18:55:46 +0000604 s->it_shift = it_shift;
Jan Kiszkaa08d4362009-06-27 09:25:07 +0200605 qemu_register_reset(parallel_reset, s);
thsd60532c2007-06-18 18:55:46 +0000606
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400607 memory_region_init_io(&s->iomem, NULL, &parallel_mm_ops, s,
Avi Kivity63858cd2011-10-06 16:44:26 +0200608 "parallel", 8 << it_shift);
609 memory_region_add_subregion(address_space, base, &s->iomem);
Blue Swirldefdb202011-02-05 14:51:57 +0000610 return true;
thsd60532c2007-06-18 18:55:46 +0000611}
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200612
Anthony Liguori39bffca2011-12-07 21:34:16 -0600613static Property parallel_isa_properties[] = {
614 DEFINE_PROP_UINT32("index", ISAParallelState, index, -1),
Paolo Bonzinic7bcc852014-02-08 11:01:53 +0100615 DEFINE_PROP_UINT32("iobase", ISAParallelState, iobase, -1),
Anthony Liguori39bffca2011-12-07 21:34:16 -0600616 DEFINE_PROP_UINT32("irq", ISAParallelState, isairq, 7),
617 DEFINE_PROP_CHR("chardev", ISAParallelState, state.chr),
618 DEFINE_PROP_END_OF_LIST(),
619};
620
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600621static void parallel_isa_class_initfn(ObjectClass *klass, void *data)
622{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600623 DeviceClass *dc = DEVICE_CLASS(klass);
Andreas Färberdb895a12012-11-25 02:37:14 +0100624
625 dc->realize = parallel_isa_realizefn;
Pavel Dovgalyuk461a2752014-08-28 15:18:46 +0400626 dc->vmsd = &vmstate_parallel_isa;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600627 dc->props = parallel_isa_properties;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300628 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600629}
630
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100631static const TypeInfo parallel_isa_info = {
Andreas Färberb0dc5ee2013-04-27 22:18:45 +0200632 .name = TYPE_ISA_PARALLEL,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600633 .parent = TYPE_ISA_DEVICE,
634 .instance_size = sizeof(ISAParallelState),
635 .class_init = parallel_isa_class_initfn,
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200636};
637
Andreas Färber83f7d432012-02-09 15:20:55 +0100638static void parallel_register_types(void)
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200639{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600640 type_register_static(&parallel_isa_info);
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200641}
642
Andreas Färber83f7d432012-02-09 15:20:55 +0100643type_init(parallel_register_types)