blob: 219f38436ffa43bee30636a7a70338f057292171 [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 */
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw.h"
26#include "qemu-char.h"
27#include "isa.h"
28#include "pc.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020029#include "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
Gerd Hoffmann021f0672009-09-22 13:53:22 +020083typedef struct ISAParallelState {
84 ISADevice dev;
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +020085 uint32_t index;
Gerd Hoffmann021f0672009-09-22 13:53:22 +020086 uint32_t iobase;
87 uint32_t isairq;
88 ParallelState state;
89} ISAParallelState;
90
bellard6508fe52005-01-15 12:02:56 +000091static void parallel_update_irq(ParallelState *s)
92{
93 if (s->irq_pending)
pbrookd537cf62007-04-07 18:14:41 +000094 qemu_irq_raise(s->irq);
bellard6508fe52005-01-15 12:02:56 +000095 else
pbrookd537cf62007-04-07 18:14:41 +000096 qemu_irq_lower(s->irq);
bellard6508fe52005-01-15 12:02:56 +000097}
98
ths5867c882007-02-17 23:44:43 +000099static void
100parallel_ioport_write_sw(void *opaque, uint32_t addr, uint32_t val)
bellard6508fe52005-01-15 12:02:56 +0000101{
102 ParallelState *s = opaque;
ths3b46e622007-09-17 08:09:54 +0000103
ths5867c882007-02-17 23:44:43 +0000104 pdebug("write addr=0x%02x val=0x%02x\n", addr, val);
105
bellard6508fe52005-01-15 12:02:56 +0000106 addr &= 7;
bellard6508fe52005-01-15 12:02:56 +0000107 switch(addr) {
ths5867c882007-02-17 23:44:43 +0000108 case PARA_REG_DATA:
ths0fa7f152007-06-07 21:07:11 +0000109 s->dataw = val;
110 parallel_update_irq(s);
bellard6508fe52005-01-15 12:02:56 +0000111 break;
ths5867c882007-02-17 23:44:43 +0000112 case PARA_REG_CTR:
balrog52ccc5e2008-02-10 13:34:48 +0000113 val |= 0xc0;
ths0fa7f152007-06-07 21:07:11 +0000114 if ((val & PARA_CTR_INIT) == 0 ) {
115 s->status = PARA_STS_BUSY;
116 s->status |= PARA_STS_ACK;
117 s->status |= PARA_STS_ONLINE;
118 s->status |= PARA_STS_ERROR;
119 }
120 else if (val & PARA_CTR_SELECT) {
121 if (val & PARA_CTR_STROBE) {
122 s->status &= ~PARA_STS_BUSY;
123 if ((s->control & PARA_CTR_STROBE) == 0)
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500124 qemu_chr_fe_write(s->chr, &s->dataw, 1);
ths0fa7f152007-06-07 21:07:11 +0000125 } else {
126 if (s->control & PARA_CTR_INTEN) {
127 s->irq_pending = 1;
128 }
129 }
130 }
131 parallel_update_irq(s);
132 s->control = val;
bellard6508fe52005-01-15 12:02:56 +0000133 break;
134 }
135}
136
ths5867c882007-02-17 23:44:43 +0000137static void parallel_ioport_write_hw(void *opaque, uint32_t addr, uint32_t val)
138{
139 ParallelState *s = opaque;
140 uint8_t parm = val;
aurel32563e3c62008-08-22 08:57:09 +0000141 int dir;
ths5867c882007-02-17 23:44:43 +0000142
143 /* Sometimes programs do several writes for timing purposes on old
144 HW. Take care not to waste time on writes that do nothing. */
145
146 s->last_read_offset = ~0U;
147
148 addr &= 7;
149 switch(addr) {
150 case PARA_REG_DATA:
151 if (s->dataw == val)
ths0fa7f152007-06-07 21:07:11 +0000152 return;
153 pdebug("wd%02x\n", val);
Anthony Liguori41084f12011-08-15 11:17:34 -0500154 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_WRITE_DATA, &parm);
ths0fa7f152007-06-07 21:07:11 +0000155 s->dataw = val;
ths5867c882007-02-17 23:44:43 +0000156 break;
157 case PARA_REG_STS:
ths0fa7f152007-06-07 21:07:11 +0000158 pdebug("ws%02x\n", val);
159 if (val & PARA_STS_TMOUT)
160 s->epp_timeout = 0;
161 break;
ths5867c882007-02-17 23:44:43 +0000162 case PARA_REG_CTR:
163 val |= 0xc0;
164 if (s->control == val)
ths0fa7f152007-06-07 21:07:11 +0000165 return;
166 pdebug("wc%02x\n", val);
aurel32563e3c62008-08-22 08:57:09 +0000167
168 if ((val & PARA_CTR_DIR) != (s->control & PARA_CTR_DIR)) {
169 if (val & PARA_CTR_DIR) {
170 dir = 1;
171 } else {
172 dir = 0;
173 }
Anthony Liguori41084f12011-08-15 11:17:34 -0500174 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_DATA_DIR, &dir);
aurel32563e3c62008-08-22 08:57:09 +0000175 parm &= ~PARA_CTR_DIR;
176 }
177
Anthony Liguori41084f12011-08-15 11:17:34 -0500178 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_WRITE_CONTROL, &parm);
ths0fa7f152007-06-07 21:07:11 +0000179 s->control = val;
ths5867c882007-02-17 23:44:43 +0000180 break;
181 case PARA_REG_EPP_ADDR:
ths0fa7f152007-06-07 21:07:11 +0000182 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
183 /* Controls not correct for EPP address cycle, so do nothing */
184 pdebug("wa%02x s\n", val);
185 else {
186 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
Anthony Liguori41084f12011-08-15 11:17:34 -0500187 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE_ADDR, &ioarg)) {
ths0fa7f152007-06-07 21:07:11 +0000188 s->epp_timeout = 1;
189 pdebug("wa%02x t\n", val);
190 }
191 else
192 pdebug("wa%02x\n", val);
193 }
194 break;
ths5867c882007-02-17 23:44:43 +0000195 case PARA_REG_EPP_DATA:
ths0fa7f152007-06-07 21:07:11 +0000196 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%02x s\n", val);
199 else {
200 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
Anthony Liguori41084f12011-08-15 11:17:34 -0500201 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg)) {
ths0fa7f152007-06-07 21:07:11 +0000202 s->epp_timeout = 1;
203 pdebug("we%02x t\n", val);
204 }
205 else
206 pdebug("we%02x\n", val);
207 }
208 break;
ths5867c882007-02-17 23:44:43 +0000209 }
210}
211
212static void
213parallel_ioport_eppdata_write_hw2(void *opaque, uint32_t addr, uint32_t val)
214{
215 ParallelState *s = opaque;
216 uint16_t eppdata = cpu_to_le16(val);
217 int err;
218 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000219 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000220 };
221 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
ths0fa7f152007-06-07 21:07:11 +0000222 /* Controls not correct for EPP data cycle, so do nothing */
223 pdebug("we%04x s\n", val);
224 return;
ths5867c882007-02-17 23:44:43 +0000225 }
Anthony Liguori41084f12011-08-15 11:17:34 -0500226 err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
ths5867c882007-02-17 23:44:43 +0000227 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000228 s->epp_timeout = 1;
229 pdebug("we%04x t\n", val);
ths5867c882007-02-17 23:44:43 +0000230 }
231 else
ths0fa7f152007-06-07 21:07:11 +0000232 pdebug("we%04x\n", val);
ths5867c882007-02-17 23:44:43 +0000233}
234
235static void
236parallel_ioport_eppdata_write_hw4(void *opaque, uint32_t addr, uint32_t val)
237{
238 ParallelState *s = opaque;
239 uint32_t eppdata = cpu_to_le32(val);
240 int err;
241 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000242 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000243 };
244 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
ths0fa7f152007-06-07 21:07:11 +0000245 /* Controls not correct for EPP data cycle, so do nothing */
246 pdebug("we%08x s\n", val);
247 return;
ths5867c882007-02-17 23:44:43 +0000248 }
Anthony Liguori41084f12011-08-15 11:17:34 -0500249 err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
ths5867c882007-02-17 23:44:43 +0000250 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000251 s->epp_timeout = 1;
252 pdebug("we%08x t\n", val);
ths5867c882007-02-17 23:44:43 +0000253 }
254 else
ths0fa7f152007-06-07 21:07:11 +0000255 pdebug("we%08x\n", val);
ths5867c882007-02-17 23:44:43 +0000256}
257
258static uint32_t parallel_ioport_read_sw(void *opaque, uint32_t addr)
bellard6508fe52005-01-15 12:02:56 +0000259{
260 ParallelState *s = opaque;
261 uint32_t ret = 0xff;
262
263 addr &= 7;
264 switch(addr) {
ths5867c882007-02-17 23:44:43 +0000265 case PARA_REG_DATA:
ths0fa7f152007-06-07 21:07:11 +0000266 if (s->control & PARA_CTR_DIR)
267 ret = s->datar;
268 else
269 ret = s->dataw;
bellard6508fe52005-01-15 12:02:56 +0000270 break;
ths5867c882007-02-17 23:44:43 +0000271 case PARA_REG_STS:
ths0fa7f152007-06-07 21:07:11 +0000272 ret = s->status;
273 s->irq_pending = 0;
274 if ((s->status & PARA_STS_BUSY) == 0 && (s->control & PARA_CTR_STROBE) == 0) {
275 /* XXX Fixme: wait 5 microseconds */
276 if (s->status & PARA_STS_ACK)
277 s->status &= ~PARA_STS_ACK;
278 else {
279 /* XXX Fixme: wait 5 microseconds */
280 s->status |= PARA_STS_ACK;
281 s->status |= PARA_STS_BUSY;
282 }
283 }
284 parallel_update_irq(s);
bellard6508fe52005-01-15 12:02:56 +0000285 break;
ths5867c882007-02-17 23:44:43 +0000286 case PARA_REG_CTR:
bellard6508fe52005-01-15 12:02:56 +0000287 ret = s->control;
288 break;
289 }
ths5867c882007-02-17 23:44:43 +0000290 pdebug("read addr=0x%02x val=0x%02x\n", addr, ret);
291 return ret;
292}
293
294static uint32_t parallel_ioport_read_hw(void *opaque, uint32_t addr)
295{
296 ParallelState *s = opaque;
297 uint8_t ret = 0xff;
298 addr &= 7;
299 switch(addr) {
300 case PARA_REG_DATA:
Anthony Liguori41084f12011-08-15 11:17:34 -0500301 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_DATA, &ret);
ths0fa7f152007-06-07 21:07:11 +0000302 if (s->last_read_offset != addr || s->datar != ret)
303 pdebug("rd%02x\n", ret);
ths5867c882007-02-17 23:44:43 +0000304 s->datar = ret;
305 break;
306 case PARA_REG_STS:
Anthony Liguori41084f12011-08-15 11:17:34 -0500307 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &ret);
ths0fa7f152007-06-07 21:07:11 +0000308 ret &= ~PARA_STS_TMOUT;
309 if (s->epp_timeout)
310 ret |= PARA_STS_TMOUT;
311 if (s->last_read_offset != addr || s->status != ret)
312 pdebug("rs%02x\n", ret);
313 s->status = ret;
ths5867c882007-02-17 23:44:43 +0000314 break;
315 case PARA_REG_CTR:
316 /* s->control has some bits fixed to 1. It is zero only when
ths0fa7f152007-06-07 21:07:11 +0000317 it has not been yet written to. */
318 if (s->control == 0) {
Anthony Liguori41084f12011-08-15 11:17:34 -0500319 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_CONTROL, &ret);
ths0fa7f152007-06-07 21:07:11 +0000320 if (s->last_read_offset != addr)
321 pdebug("rc%02x\n", ret);
322 s->control = ret;
323 }
324 else {
325 ret = s->control;
326 if (s->last_read_offset != addr)
327 pdebug("rc%02x\n", ret);
328 }
ths5867c882007-02-17 23:44:43 +0000329 break;
330 case PARA_REG_EPP_ADDR:
ths0fa7f152007-06-07 21:07:11 +0000331 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT))
332 /* Controls not correct for EPP addr cycle, so do nothing */
333 pdebug("ra%02x s\n", ret);
334 else {
335 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
Anthony Liguori41084f12011-08-15 11:17:34 -0500336 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ_ADDR, &ioarg)) {
ths0fa7f152007-06-07 21:07:11 +0000337 s->epp_timeout = 1;
338 pdebug("ra%02x t\n", ret);
339 }
340 else
341 pdebug("ra%02x\n", ret);
342 }
343 break;
ths5867c882007-02-17 23:44:43 +0000344 case PARA_REG_EPP_DATA:
ths0fa7f152007-06-07 21:07:11 +0000345 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT))
346 /* Controls not correct for EPP data cycle, so do nothing */
347 pdebug("re%02x s\n", ret);
348 else {
349 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
Anthony Liguori41084f12011-08-15 11:17:34 -0500350 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg)) {
ths0fa7f152007-06-07 21:07:11 +0000351 s->epp_timeout = 1;
352 pdebug("re%02x t\n", ret);
353 }
354 else
355 pdebug("re%02x\n", ret);
356 }
357 break;
ths5867c882007-02-17 23:44:43 +0000358 }
359 s->last_read_offset = addr;
360 return ret;
361}
362
363static uint32_t
364parallel_ioport_eppdata_read_hw2(void *opaque, uint32_t addr)
365{
366 ParallelState *s = opaque;
367 uint32_t ret;
368 uint16_t eppdata = ~0;
369 int err;
370 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000371 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000372 };
373 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
ths0fa7f152007-06-07 21:07:11 +0000374 /* Controls not correct for EPP data cycle, so do nothing */
375 pdebug("re%04x s\n", eppdata);
376 return eppdata;
ths5867c882007-02-17 23:44:43 +0000377 }
Anthony Liguori41084f12011-08-15 11:17:34 -0500378 err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
ths5867c882007-02-17 23:44:43 +0000379 ret = le16_to_cpu(eppdata);
380
381 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000382 s->epp_timeout = 1;
383 pdebug("re%04x t\n", ret);
ths5867c882007-02-17 23:44:43 +0000384 }
385 else
ths0fa7f152007-06-07 21:07:11 +0000386 pdebug("re%04x\n", ret);
ths5867c882007-02-17 23:44:43 +0000387 return ret;
388}
389
390static uint32_t
391parallel_ioport_eppdata_read_hw4(void *opaque, uint32_t addr)
392{
393 ParallelState *s = opaque;
394 uint32_t ret;
395 uint32_t eppdata = ~0U;
396 int err;
397 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000398 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000399 };
400 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
ths0fa7f152007-06-07 21:07:11 +0000401 /* Controls not correct for EPP data cycle, so do nothing */
402 pdebug("re%08x s\n", eppdata);
403 return eppdata;
ths5867c882007-02-17 23:44:43 +0000404 }
Anthony Liguori41084f12011-08-15 11:17:34 -0500405 err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
ths5867c882007-02-17 23:44:43 +0000406 ret = le32_to_cpu(eppdata);
407
408 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000409 s->epp_timeout = 1;
410 pdebug("re%08x t\n", ret);
ths5867c882007-02-17 23:44:43 +0000411 }
412 else
ths0fa7f152007-06-07 21:07:11 +0000413 pdebug("re%08x\n", ret);
ths5867c882007-02-17 23:44:43 +0000414 return ret;
415}
416
417static void parallel_ioport_ecp_write(void *opaque, uint32_t addr, uint32_t val)
418{
Blue Swirl7f5b7d32010-04-25 18:58:25 +0000419 pdebug("wecp%d=%02x\n", addr & 7, val);
ths5867c882007-02-17 23:44:43 +0000420}
421
422static uint32_t parallel_ioport_ecp_read(void *opaque, uint32_t addr)
423{
424 uint8_t ret = 0xff;
Blue Swirl7f5b7d32010-04-25 18:58:25 +0000425
426 pdebug("recp%d:%02x\n", addr & 7, ret);
bellard6508fe52005-01-15 12:02:56 +0000427 return ret;
428}
429
aurel3233093a02008-12-07 23:26:09 +0000430static void parallel_reset(void *opaque)
bellard6508fe52005-01-15 12:02:56 +0000431{
aurel3233093a02008-12-07 23:26:09 +0000432 ParallelState *s = opaque;
433
ths5867c882007-02-17 23:44:43 +0000434 s->datar = ~0;
435 s->dataw = ~0;
bellard6508fe52005-01-15 12:02:56 +0000436 s->status = PARA_STS_BUSY;
437 s->status |= PARA_STS_ACK;
438 s->status |= PARA_STS_ONLINE;
439 s->status |= PARA_STS_ERROR;
balrog52ccc5e2008-02-10 13:34:48 +0000440 s->status |= PARA_STS_TMOUT;
bellard6508fe52005-01-15 12:02:56 +0000441 s->control = PARA_CTR_SELECT;
442 s->control |= PARA_CTR_INIT;
balrog52ccc5e2008-02-10 13:34:48 +0000443 s->control |= 0xc0;
ths5867c882007-02-17 23:44:43 +0000444 s->irq_pending = 0;
ths5867c882007-02-17 23:44:43 +0000445 s->hw_driver = 0;
446 s->epp_timeout = 0;
447 s->last_read_offset = ~0U;
thsd60532c2007-06-18 18:55:46 +0000448}
449
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200450static const int isa_parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
451
Richard Henderson1922abd2011-08-15 15:55:09 -0700452static const MemoryRegionPortio isa_parallel_portio_hw_list[] = {
453 { 0, 8, 1,
454 .read = parallel_ioport_read_hw,
455 .write = parallel_ioport_write_hw },
456 { 4, 1, 2,
457 .read = parallel_ioport_eppdata_read_hw2,
458 .write = parallel_ioport_eppdata_write_hw2 },
459 { 4, 1, 4,
460 .read = parallel_ioport_eppdata_read_hw4,
461 .write = parallel_ioport_eppdata_write_hw4 },
462 { 0x400, 8, 1,
463 .read = parallel_ioport_ecp_read,
464 .write = parallel_ioport_ecp_write },
465 PORTIO_END_OF_LIST(),
466};
467
468static const MemoryRegionPortio isa_parallel_portio_sw_list[] = {
469 { 0, 8, 1,
470 .read = parallel_ioport_read_sw,
471 .write = parallel_ioport_write_sw },
472 PORTIO_END_OF_LIST(),
473};
474
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200475static int parallel_isa_initfn(ISADevice *dev)
thsd60532c2007-06-18 18:55:46 +0000476{
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200477 static int index;
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200478 ISAParallelState *isa = DO_UPCAST(ISAParallelState, dev, dev);
479 ParallelState *s = &isa->state;
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200480 int base;
thsd60532c2007-06-18 18:55:46 +0000481 uint8_t dummy;
482
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200483 if (!s->chr) {
484 fprintf(stderr, "Can't create parallel device, empty char device\n");
485 exit(1);
486 }
487
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200488 if (isa->index == -1)
489 isa->index = index;
490 if (isa->index >= MAX_PARALLEL_PORTS)
491 return -1;
492 if (isa->iobase == -1)
493 isa->iobase = isa_parallel_io[isa->index];
494 index++;
495
496 base = isa->iobase;
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200497 isa_init_irq(dev, &s->irq, isa->isairq);
Jan Kiszkaa08d4362009-06-27 09:25:07 +0200498 qemu_register_reset(parallel_reset, s);
bellard6508fe52005-01-15 12:02:56 +0000499
Anthony Liguori41084f12011-08-15 11:17:34 -0500500 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &dummy) == 0) {
ths5867c882007-02-17 23:44:43 +0000501 s->hw_driver = 1;
ths0fa7f152007-06-07 21:07:11 +0000502 s->status = dummy;
ths5867c882007-02-17 23:44:43 +0000503 }
504
Richard Henderson1922abd2011-08-15 15:55:09 -0700505 isa_register_portio_list(dev, base,
506 (s->hw_driver
507 ? &isa_parallel_portio_hw_list[0]
508 : &isa_parallel_portio_sw_list[0]),
509 s, "parallel");
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200510 return 0;
511}
512
thsd60532c2007-06-18 18:55:46 +0000513/* Memory mapped interface */
Anthony Liguoric227f092009-10-01 16:12:16 -0500514static uint32_t parallel_mm_readb (void *opaque, target_phys_addr_t addr)
thsd60532c2007-06-18 18:55:46 +0000515{
516 ParallelState *s = opaque;
517
pbrook8da3ff12008-12-01 18:59:50 +0000518 return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFF;
thsd60532c2007-06-18 18:55:46 +0000519}
520
pbrook9596ebb2007-11-18 01:44:38 +0000521static void parallel_mm_writeb (void *opaque,
Anthony Liguoric227f092009-10-01 16:12:16 -0500522 target_phys_addr_t addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000523{
524 ParallelState *s = opaque;
525
pbrook8da3ff12008-12-01 18:59:50 +0000526 parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFF);
thsd60532c2007-06-18 18:55:46 +0000527}
528
Anthony Liguoric227f092009-10-01 16:12:16 -0500529static uint32_t parallel_mm_readw (void *opaque, target_phys_addr_t addr)
thsd60532c2007-06-18 18:55:46 +0000530{
531 ParallelState *s = opaque;
532
pbrook8da3ff12008-12-01 18:59:50 +0000533 return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFFFF;
thsd60532c2007-06-18 18:55:46 +0000534}
535
pbrook9596ebb2007-11-18 01:44:38 +0000536static void parallel_mm_writew (void *opaque,
Anthony Liguoric227f092009-10-01 16:12:16 -0500537 target_phys_addr_t addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000538{
539 ParallelState *s = opaque;
540
pbrook8da3ff12008-12-01 18:59:50 +0000541 parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFFFF);
thsd60532c2007-06-18 18:55:46 +0000542}
543
Anthony Liguoric227f092009-10-01 16:12:16 -0500544static uint32_t parallel_mm_readl (void *opaque, target_phys_addr_t addr)
thsd60532c2007-06-18 18:55:46 +0000545{
546 ParallelState *s = opaque;
547
pbrook8da3ff12008-12-01 18:59:50 +0000548 return parallel_ioport_read_sw(s, addr >> s->it_shift);
thsd60532c2007-06-18 18:55:46 +0000549}
550
pbrook9596ebb2007-11-18 01:44:38 +0000551static void parallel_mm_writel (void *opaque,
Anthony Liguoric227f092009-10-01 16:12:16 -0500552 target_phys_addr_t addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000553{
554 ParallelState *s = opaque;
555
pbrook8da3ff12008-12-01 18:59:50 +0000556 parallel_ioport_write_sw(s, addr >> s->it_shift, value);
thsd60532c2007-06-18 18:55:46 +0000557}
558
Avi Kivity63858cd2011-10-06 16:44:26 +0200559static const MemoryRegionOps parallel_mm_ops = {
560 .old_mmio = {
561 .read = { parallel_mm_readb, parallel_mm_readw, parallel_mm_readl },
562 .write = { parallel_mm_writeb, parallel_mm_writew, parallel_mm_writel },
563 },
564 .endianness = DEVICE_NATIVE_ENDIAN,
thsd60532c2007-06-18 18:55:46 +0000565};
566
567/* If fd is zero, it means that the parallel device uses the console */
Avi Kivity63858cd2011-10-06 16:44:26 +0200568bool parallel_mm_init(MemoryRegion *address_space,
569 target_phys_addr_t base, int it_shift, qemu_irq irq,
Blue Swirldefdb202011-02-05 14:51:57 +0000570 CharDriverState *chr)
thsd60532c2007-06-18 18:55:46 +0000571{
572 ParallelState *s;
thsd60532c2007-06-18 18:55:46 +0000573
Anthony Liguori7267c092011-08-20 22:09:37 -0500574 s = g_malloc0(sizeof(ParallelState));
aurel3233093a02008-12-07 23:26:09 +0000575 s->irq = irq;
576 s->chr = chr;
thsd60532c2007-06-18 18:55:46 +0000577 s->it_shift = it_shift;
Jan Kiszkaa08d4362009-06-27 09:25:07 +0200578 qemu_register_reset(parallel_reset, s);
thsd60532c2007-06-18 18:55:46 +0000579
Avi Kivity63858cd2011-10-06 16:44:26 +0200580 memory_region_init_io(&s->iomem, &parallel_mm_ops, s,
581 "parallel", 8 << it_shift);
582 memory_region_add_subregion(address_space, base, &s->iomem);
Blue Swirldefdb202011-02-05 14:51:57 +0000583 return true;
thsd60532c2007-06-18 18:55:46 +0000584}
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200585
Anthony Liguori39bffca2011-12-07 21:34:16 -0600586static Property parallel_isa_properties[] = {
587 DEFINE_PROP_UINT32("index", ISAParallelState, index, -1),
588 DEFINE_PROP_HEX32("iobase", ISAParallelState, iobase, -1),
589 DEFINE_PROP_UINT32("irq", ISAParallelState, isairq, 7),
590 DEFINE_PROP_CHR("chardev", ISAParallelState, state.chr),
591 DEFINE_PROP_END_OF_LIST(),
592};
593
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600594static void parallel_isa_class_initfn(ObjectClass *klass, void *data)
595{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600596 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600597 ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
598 ic->init = parallel_isa_initfn;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600599 dc->props = parallel_isa_properties;
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600600}
601
Anthony Liguori39bffca2011-12-07 21:34:16 -0600602static TypeInfo parallel_isa_info = {
603 .name = "isa-parallel",
604 .parent = TYPE_ISA_DEVICE,
605 .instance_size = sizeof(ISAParallelState),
606 .class_init = parallel_isa_class_initfn,
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200607};
608
Andreas Färber83f7d432012-02-09 15:20:55 +0100609static void parallel_register_types(void)
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200610{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600611 type_register_static(&parallel_isa_info);
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200612}
613
Andreas Färber83f7d432012-02-09 15:20:55 +0100614type_init(parallel_register_types)