blob: 79fa8f66b4ec13b9cc7c74a380c7a40209130f9c [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"
bellard6508fe52005-01-15 12:02:56 +000029
30//#define DEBUG_PARALLEL
31
ths5867c882007-02-17 23:44:43 +000032#ifdef DEBUG_PARALLEL
Blue Swirl001faf32009-05-13 17:53:17 +000033#define pdebug(fmt, ...) printf("pp: " fmt, ## __VA_ARGS__)
ths5867c882007-02-17 23:44:43 +000034#else
Blue Swirl001faf32009-05-13 17:53:17 +000035#define pdebug(fmt, ...) ((void)0)
ths5867c882007-02-17 23:44:43 +000036#endif
37
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
43
bellard6508fe52005-01-15 12:02:56 +000044/*
45 * These are the definitions for the Printer Status Register
46 */
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 */
ths5867c882007-02-17 23:44:43 +000052#define PARA_STS_TMOUT 0x01 /* EPP timeout */
bellard6508fe52005-01-15 12:02:56 +000053
54/*
55 * These are the definitions for the Printer Control Register
56 */
ths5867c882007-02-17 23:44:43 +000057#define PARA_CTR_DIR 0x20 /* Direction (1=read, 0=write) */
bellard6508fe52005-01-15 12:02:56 +000058#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 */
63
ths5867c882007-02-17 23:44:43 +000064#define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
65
bellard6508fe52005-01-15 12:02:56 +000066struct ParallelState {
ths5867c882007-02-17 23:44:43 +000067 uint8_t dataw;
68 uint8_t datar;
69 uint8_t status;
bellard6508fe52005-01-15 12:02:56 +000070 uint8_t control;
pbrookd537cf62007-04-07 18:14:41 +000071 qemu_irq irq;
bellard6508fe52005-01-15 12:02:56 +000072 int irq_pending;
73 CharDriverState *chr;
bellarde57a8c02005-11-10 23:58:52 +000074 int hw_driver;
ths5867c882007-02-17 23:44:43 +000075 int epp_timeout;
76 uint32_t last_read_offset; /* For debugging */
thsd60532c2007-06-18 18:55:46 +000077 /* Memory-mapped interface */
thsd60532c2007-06-18 18:55:46 +000078 int it_shift;
bellard6508fe52005-01-15 12:02:56 +000079};
80
Gerd Hoffmann021f0672009-09-22 13:53:22 +020081typedef struct ISAParallelState {
82 ISADevice dev;
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +020083 uint32_t index;
Gerd Hoffmann021f0672009-09-22 13:53:22 +020084 uint32_t iobase;
85 uint32_t isairq;
86 ParallelState state;
87} ISAParallelState;
88
bellard6508fe52005-01-15 12:02:56 +000089static void parallel_update_irq(ParallelState *s)
90{
91 if (s->irq_pending)
pbrookd537cf62007-04-07 18:14:41 +000092 qemu_irq_raise(s->irq);
bellard6508fe52005-01-15 12:02:56 +000093 else
pbrookd537cf62007-04-07 18:14:41 +000094 qemu_irq_lower(s->irq);
bellard6508fe52005-01-15 12:02:56 +000095}
96
ths5867c882007-02-17 23:44:43 +000097static void
98parallel_ioport_write_sw(void *opaque, uint32_t addr, uint32_t val)
bellard6508fe52005-01-15 12:02:56 +000099{
100 ParallelState *s = opaque;
ths3b46e622007-09-17 08:09:54 +0000101
ths5867c882007-02-17 23:44:43 +0000102 pdebug("write addr=0x%02x val=0x%02x\n", addr, val);
103
bellard6508fe52005-01-15 12:02:56 +0000104 addr &= 7;
bellard6508fe52005-01-15 12:02:56 +0000105 switch(addr) {
ths5867c882007-02-17 23:44:43 +0000106 case PARA_REG_DATA:
ths0fa7f152007-06-07 21:07:11 +0000107 s->dataw = val;
108 parallel_update_irq(s);
bellard6508fe52005-01-15 12:02:56 +0000109 break;
ths5867c882007-02-17 23:44:43 +0000110 case PARA_REG_CTR:
balrog52ccc5e2008-02-10 13:34:48 +0000111 val |= 0xc0;
ths0fa7f152007-06-07 21:07:11 +0000112 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;
117 }
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);
123 } else {
124 if (s->control & PARA_CTR_INTEN) {
125 s->irq_pending = 1;
126 }
127 }
128 }
129 parallel_update_irq(s);
130 s->control = val;
bellard6508fe52005-01-15 12:02:56 +0000131 break;
132 }
133}
134
ths5867c882007-02-17 23:44:43 +0000135static void parallel_ioport_write_hw(void *opaque, uint32_t addr, uint32_t val)
136{
137 ParallelState *s = opaque;
138 uint8_t parm = val;
aurel32563e3c62008-08-22 08:57:09 +0000139 int dir;
ths5867c882007-02-17 23:44:43 +0000140
141 /* Sometimes programs do several writes for timing purposes on old
142 HW. Take care not to waste time on writes that do nothing. */
143
144 s->last_read_offset = ~0U;
145
146 addr &= 7;
147 switch(addr) {
148 case PARA_REG_DATA:
149 if (s->dataw == val)
ths0fa7f152007-06-07 21:07:11 +0000150 return;
151 pdebug("wd%02x\n", val);
152 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_WRITE_DATA, &parm);
153 s->dataw = val;
ths5867c882007-02-17 23:44:43 +0000154 break;
155 case PARA_REG_STS:
ths0fa7f152007-06-07 21:07:11 +0000156 pdebug("ws%02x\n", val);
157 if (val & PARA_STS_TMOUT)
158 s->epp_timeout = 0;
159 break;
ths5867c882007-02-17 23:44:43 +0000160 case PARA_REG_CTR:
161 val |= 0xc0;
162 if (s->control == val)
ths0fa7f152007-06-07 21:07:11 +0000163 return;
164 pdebug("wc%02x\n", val);
aurel32563e3c62008-08-22 08:57:09 +0000165
166 if ((val & PARA_CTR_DIR) != (s->control & PARA_CTR_DIR)) {
167 if (val & PARA_CTR_DIR) {
168 dir = 1;
169 } else {
170 dir = 0;
171 }
172 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_DATA_DIR, &dir);
173 parm &= ~PARA_CTR_DIR;
174 }
175
ths0fa7f152007-06-07 21:07:11 +0000176 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_WRITE_CONTROL, &parm);
177 s->control = val;
ths5867c882007-02-17 23:44:43 +0000178 break;
179 case PARA_REG_EPP_ADDR:
ths0fa7f152007-06-07 21:07:11 +0000180 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);
183 else {
184 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
185 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE_ADDR, &ioarg)) {
186 s->epp_timeout = 1;
187 pdebug("wa%02x t\n", val);
188 }
189 else
190 pdebug("wa%02x\n", val);
191 }
192 break;
ths5867c882007-02-17 23:44:43 +0000193 case PARA_REG_EPP_DATA:
ths0fa7f152007-06-07 21:07:11 +0000194 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);
197 else {
198 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
199 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg)) {
200 s->epp_timeout = 1;
201 pdebug("we%02x t\n", val);
202 }
203 else
204 pdebug("we%02x\n", val);
205 }
206 break;
ths5867c882007-02-17 23:44:43 +0000207 }
208}
209
210static void
211parallel_ioport_eppdata_write_hw2(void *opaque, uint32_t addr, uint32_t val)
212{
213 ParallelState *s = opaque;
214 uint16_t eppdata = cpu_to_le16(val);
215 int err;
216 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000217 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000218 };
219 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
ths0fa7f152007-06-07 21:07:11 +0000220 /* Controls not correct for EPP data cycle, so do nothing */
221 pdebug("we%04x s\n", val);
222 return;
ths5867c882007-02-17 23:44:43 +0000223 }
224 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
225 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000226 s->epp_timeout = 1;
227 pdebug("we%04x t\n", val);
ths5867c882007-02-17 23:44:43 +0000228 }
229 else
ths0fa7f152007-06-07 21:07:11 +0000230 pdebug("we%04x\n", val);
ths5867c882007-02-17 23:44:43 +0000231}
232
233static void
234parallel_ioport_eppdata_write_hw4(void *opaque, uint32_t addr, uint32_t val)
235{
236 ParallelState *s = opaque;
237 uint32_t eppdata = cpu_to_le32(val);
238 int err;
239 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000240 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000241 };
242 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
ths0fa7f152007-06-07 21:07:11 +0000243 /* Controls not correct for EPP data cycle, so do nothing */
244 pdebug("we%08x s\n", val);
245 return;
ths5867c882007-02-17 23:44:43 +0000246 }
247 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
248 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000249 s->epp_timeout = 1;
250 pdebug("we%08x t\n", val);
ths5867c882007-02-17 23:44:43 +0000251 }
252 else
ths0fa7f152007-06-07 21:07:11 +0000253 pdebug("we%08x\n", val);
ths5867c882007-02-17 23:44:43 +0000254}
255
256static uint32_t parallel_ioport_read_sw(void *opaque, uint32_t addr)
bellard6508fe52005-01-15 12:02:56 +0000257{
258 ParallelState *s = opaque;
259 uint32_t ret = 0xff;
260
261 addr &= 7;
262 switch(addr) {
ths5867c882007-02-17 23:44:43 +0000263 case PARA_REG_DATA:
ths0fa7f152007-06-07 21:07:11 +0000264 if (s->control & PARA_CTR_DIR)
265 ret = s->datar;
266 else
267 ret = s->dataw;
bellard6508fe52005-01-15 12:02:56 +0000268 break;
ths5867c882007-02-17 23:44:43 +0000269 case PARA_REG_STS:
ths0fa7f152007-06-07 21:07:11 +0000270 ret = s->status;
271 s->irq_pending = 0;
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;
276 else {
277 /* XXX Fixme: wait 5 microseconds */
278 s->status |= PARA_STS_ACK;
279 s->status |= PARA_STS_BUSY;
280 }
281 }
282 parallel_update_irq(s);
bellard6508fe52005-01-15 12:02:56 +0000283 break;
ths5867c882007-02-17 23:44:43 +0000284 case PARA_REG_CTR:
bellard6508fe52005-01-15 12:02:56 +0000285 ret = s->control;
286 break;
287 }
ths5867c882007-02-17 23:44:43 +0000288 pdebug("read addr=0x%02x val=0x%02x\n", addr, ret);
289 return ret;
290}
291
292static uint32_t parallel_ioport_read_hw(void *opaque, uint32_t addr)
293{
294 ParallelState *s = opaque;
295 uint8_t ret = 0xff;
296 addr &= 7;
297 switch(addr) {
298 case PARA_REG_DATA:
ths0fa7f152007-06-07 21:07:11 +0000299 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);
ths5867c882007-02-17 23:44:43 +0000302 s->datar = ret;
303 break;
304 case PARA_REG_STS:
ths0fa7f152007-06-07 21:07:11 +0000305 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &ret);
306 ret &= ~PARA_STS_TMOUT;
307 if (s->epp_timeout)
308 ret |= PARA_STS_TMOUT;
309 if (s->last_read_offset != addr || s->status != ret)
310 pdebug("rs%02x\n", ret);
311 s->status = ret;
ths5867c882007-02-17 23:44:43 +0000312 break;
313 case PARA_REG_CTR:
314 /* s->control has some bits fixed to 1. It is zero only when
ths0fa7f152007-06-07 21:07:11 +0000315 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);
320 s->control = ret;
321 }
322 else {
323 ret = s->control;
324 if (s->last_read_offset != addr)
325 pdebug("rc%02x\n", ret);
326 }
ths5867c882007-02-17 23:44:43 +0000327 break;
328 case PARA_REG_EPP_ADDR:
ths0fa7f152007-06-07 21:07:11 +0000329 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);
332 else {
333 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
334 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ_ADDR, &ioarg)) {
335 s->epp_timeout = 1;
336 pdebug("ra%02x t\n", ret);
337 }
338 else
339 pdebug("ra%02x\n", ret);
340 }
341 break;
ths5867c882007-02-17 23:44:43 +0000342 case PARA_REG_EPP_DATA:
ths0fa7f152007-06-07 21:07:11 +0000343 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);
346 else {
347 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
348 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg)) {
349 s->epp_timeout = 1;
350 pdebug("re%02x t\n", ret);
351 }
352 else
353 pdebug("re%02x\n", ret);
354 }
355 break;
ths5867c882007-02-17 23:44:43 +0000356 }
357 s->last_read_offset = addr;
358 return ret;
359}
360
361static uint32_t
362parallel_ioport_eppdata_read_hw2(void *opaque, uint32_t addr)
363{
364 ParallelState *s = opaque;
365 uint32_t ret;
366 uint16_t eppdata = ~0;
367 int err;
368 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000369 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000370 };
371 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
ths0fa7f152007-06-07 21:07:11 +0000372 /* Controls not correct for EPP data cycle, so do nothing */
373 pdebug("re%04x s\n", eppdata);
374 return eppdata;
ths5867c882007-02-17 23:44:43 +0000375 }
376 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
377 ret = le16_to_cpu(eppdata);
378
379 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000380 s->epp_timeout = 1;
381 pdebug("re%04x t\n", ret);
ths5867c882007-02-17 23:44:43 +0000382 }
383 else
ths0fa7f152007-06-07 21:07:11 +0000384 pdebug("re%04x\n", ret);
ths5867c882007-02-17 23:44:43 +0000385 return ret;
386}
387
388static uint32_t
389parallel_ioport_eppdata_read_hw4(void *opaque, uint32_t addr)
390{
391 ParallelState *s = opaque;
392 uint32_t ret;
393 uint32_t eppdata = ~0U;
394 int err;
395 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000396 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000397 };
398 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
ths0fa7f152007-06-07 21:07:11 +0000399 /* Controls not correct for EPP data cycle, so do nothing */
400 pdebug("re%08x s\n", eppdata);
401 return eppdata;
ths5867c882007-02-17 23:44:43 +0000402 }
403 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
404 ret = le32_to_cpu(eppdata);
405
406 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000407 s->epp_timeout = 1;
408 pdebug("re%08x t\n", ret);
ths5867c882007-02-17 23:44:43 +0000409 }
410 else
ths0fa7f152007-06-07 21:07:11 +0000411 pdebug("re%08x\n", ret);
ths5867c882007-02-17 23:44:43 +0000412 return ret;
413}
414
415static void parallel_ioport_ecp_write(void *opaque, uint32_t addr, uint32_t val)
416{
417 addr &= 7;
418 pdebug("wecp%d=%02x\n", addr, val);
419}
420
421static uint32_t parallel_ioport_ecp_read(void *opaque, uint32_t addr)
422{
423 uint8_t ret = 0xff;
424 addr &= 7;
425 pdebug("recp%d:%02x\n", addr, ret);
bellard6508fe52005-01-15 12:02:56 +0000426 return ret;
427}
428
aurel3233093a02008-12-07 23:26:09 +0000429static void parallel_reset(void *opaque)
bellard6508fe52005-01-15 12:02:56 +0000430{
aurel3233093a02008-12-07 23:26:09 +0000431 ParallelState *s = opaque;
432
ths5867c882007-02-17 23:44:43 +0000433 s->datar = ~0;
434 s->dataw = ~0;
bellard6508fe52005-01-15 12:02:56 +0000435 s->status = PARA_STS_BUSY;
436 s->status |= PARA_STS_ACK;
437 s->status |= PARA_STS_ONLINE;
438 s->status |= PARA_STS_ERROR;
balrog52ccc5e2008-02-10 13:34:48 +0000439 s->status |= PARA_STS_TMOUT;
bellard6508fe52005-01-15 12:02:56 +0000440 s->control = PARA_CTR_SELECT;
441 s->control |= PARA_CTR_INIT;
balrog52ccc5e2008-02-10 13:34:48 +0000442 s->control |= 0xc0;
ths5867c882007-02-17 23:44:43 +0000443 s->irq_pending = 0;
ths5867c882007-02-17 23:44:43 +0000444 s->hw_driver = 0;
445 s->epp_timeout = 0;
446 s->last_read_offset = ~0U;
thsd60532c2007-06-18 18:55:46 +0000447}
448
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200449static const int isa_parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
450
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200451static int parallel_isa_initfn(ISADevice *dev)
thsd60532c2007-06-18 18:55:46 +0000452{
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200453 static int index;
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200454 ISAParallelState *isa = DO_UPCAST(ISAParallelState, dev, dev);
455 ParallelState *s = &isa->state;
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200456 int base;
thsd60532c2007-06-18 18:55:46 +0000457 uint8_t dummy;
458
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200459 if (!s->chr) {
460 fprintf(stderr, "Can't create parallel device, empty char device\n");
461 exit(1);
462 }
463
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200464 if (isa->index == -1)
465 isa->index = index;
466 if (isa->index >= MAX_PARALLEL_PORTS)
467 return -1;
468 if (isa->iobase == -1)
469 isa->iobase = isa_parallel_io[isa->index];
470 index++;
471
472 base = isa->iobase;
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200473 isa_init_irq(dev, &s->irq, isa->isairq);
aurel3233093a02008-12-07 23:26:09 +0000474 parallel_reset(s);
Jan Kiszkaa08d4362009-06-27 09:25:07 +0200475 qemu_register_reset(parallel_reset, s);
bellard6508fe52005-01-15 12:02:56 +0000476
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200477 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &dummy) == 0) {
ths5867c882007-02-17 23:44:43 +0000478 s->hw_driver = 1;
ths0fa7f152007-06-07 21:07:11 +0000479 s->status = dummy;
ths5867c882007-02-17 23:44:43 +0000480 }
481
482 if (s->hw_driver) {
ths0fa7f152007-06-07 21:07:11 +0000483 register_ioport_write(base, 8, 1, parallel_ioport_write_hw, s);
484 register_ioport_read(base, 8, 1, parallel_ioport_read_hw, s);
485 register_ioport_write(base+4, 1, 2, parallel_ioport_eppdata_write_hw2, s);
486 register_ioport_read(base+4, 1, 2, parallel_ioport_eppdata_read_hw2, s);
487 register_ioport_write(base+4, 1, 4, parallel_ioport_eppdata_write_hw4, s);
488 register_ioport_read(base+4, 1, 4, parallel_ioport_eppdata_read_hw4, s);
489 register_ioport_write(base+0x400, 8, 1, parallel_ioport_ecp_write, s);
490 register_ioport_read(base+0x400, 8, 1, parallel_ioport_ecp_read, s);
ths5867c882007-02-17 23:44:43 +0000491 }
492 else {
ths0fa7f152007-06-07 21:07:11 +0000493 register_ioport_write(base, 8, 1, parallel_ioport_write_sw, s);
494 register_ioport_read(base, 8, 1, parallel_ioport_read_sw, s);
ths5867c882007-02-17 23:44:43 +0000495 }
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200496 return 0;
497}
498
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200499ParallelState *parallel_init(int index, CharDriverState *chr)
500{
501 ISADevice *dev;
502
503 dev = isa_create("isa-parallel");
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200504 qdev_prop_set_uint32(&dev->qdev, "index", index);
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200505 qdev_prop_set_chr(&dev->qdev, "chardev", chr);
Markus Armbruster5c17ca22009-10-07 01:16:01 +0200506 if (qdev_init(&dev->qdev) < 0)
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200507 return NULL;
508 return &DO_UPCAST(ISAParallelState, dev, dev)->state;
bellard6508fe52005-01-15 12:02:56 +0000509}
thsd60532c2007-06-18 18:55:46 +0000510
511/* Memory mapped interface */
Anthony Liguoric227f092009-10-01 16:12:16 -0500512static uint32_t parallel_mm_readb (void *opaque, target_phys_addr_t addr)
thsd60532c2007-06-18 18:55:46 +0000513{
514 ParallelState *s = opaque;
515
pbrook8da3ff12008-12-01 18:59:50 +0000516 return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFF;
thsd60532c2007-06-18 18:55:46 +0000517}
518
pbrook9596ebb2007-11-18 01:44:38 +0000519static void parallel_mm_writeb (void *opaque,
Anthony Liguoric227f092009-10-01 16:12:16 -0500520 target_phys_addr_t addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000521{
522 ParallelState *s = opaque;
523
pbrook8da3ff12008-12-01 18:59:50 +0000524 parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFF);
thsd60532c2007-06-18 18:55:46 +0000525}
526
Anthony Liguoric227f092009-10-01 16:12:16 -0500527static uint32_t parallel_mm_readw (void *opaque, target_phys_addr_t addr)
thsd60532c2007-06-18 18:55:46 +0000528{
529 ParallelState *s = opaque;
530
pbrook8da3ff12008-12-01 18:59:50 +0000531 return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFFFF;
thsd60532c2007-06-18 18:55:46 +0000532}
533
pbrook9596ebb2007-11-18 01:44:38 +0000534static void parallel_mm_writew (void *opaque,
Anthony Liguoric227f092009-10-01 16:12:16 -0500535 target_phys_addr_t addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000536{
537 ParallelState *s = opaque;
538
pbrook8da3ff12008-12-01 18:59:50 +0000539 parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFFFF);
thsd60532c2007-06-18 18:55:46 +0000540}
541
Anthony Liguoric227f092009-10-01 16:12:16 -0500542static uint32_t parallel_mm_readl (void *opaque, target_phys_addr_t addr)
thsd60532c2007-06-18 18:55:46 +0000543{
544 ParallelState *s = opaque;
545
pbrook8da3ff12008-12-01 18:59:50 +0000546 return parallel_ioport_read_sw(s, addr >> s->it_shift);
thsd60532c2007-06-18 18:55:46 +0000547}
548
pbrook9596ebb2007-11-18 01:44:38 +0000549static void parallel_mm_writel (void *opaque,
Anthony Liguoric227f092009-10-01 16:12:16 -0500550 target_phys_addr_t addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000551{
552 ParallelState *s = opaque;
553
pbrook8da3ff12008-12-01 18:59:50 +0000554 parallel_ioport_write_sw(s, addr >> s->it_shift, value);
thsd60532c2007-06-18 18:55:46 +0000555}
556
Blue Swirld60efc62009-08-25 18:29:31 +0000557static CPUReadMemoryFunc * const parallel_mm_read_sw[] = {
thsd60532c2007-06-18 18:55:46 +0000558 &parallel_mm_readb,
559 &parallel_mm_readw,
560 &parallel_mm_readl,
561};
562
Blue Swirld60efc62009-08-25 18:29:31 +0000563static CPUWriteMemoryFunc * const parallel_mm_write_sw[] = {
thsd60532c2007-06-18 18:55:46 +0000564 &parallel_mm_writeb,
565 &parallel_mm_writew,
566 &parallel_mm_writel,
567};
568
569/* If fd is zero, it means that the parallel device uses the console */
Anthony Liguoric227f092009-10-01 16:12:16 -0500570ParallelState *parallel_mm_init(target_phys_addr_t base, int it_shift, qemu_irq irq, CharDriverState *chr)
thsd60532c2007-06-18 18:55:46 +0000571{
572 ParallelState *s;
573 int io_sw;
574
575 s = qemu_mallocz(sizeof(ParallelState));
aurel3233093a02008-12-07 23:26:09 +0000576 s->irq = irq;
577 s->chr = chr;
thsd60532c2007-06-18 18:55:46 +0000578 s->it_shift = it_shift;
aurel3233093a02008-12-07 23:26:09 +0000579 parallel_reset(s);
Jan Kiszkaa08d4362009-06-27 09:25:07 +0200580 qemu_register_reset(parallel_reset, s);
thsd60532c2007-06-18 18:55:46 +0000581
Avi Kivity1eed09c2009-06-14 11:38:51 +0300582 io_sw = cpu_register_io_memory(parallel_mm_read_sw, parallel_mm_write_sw, s);
thsd60532c2007-06-18 18:55:46 +0000583 cpu_register_physical_memory(base, 8 << it_shift, io_sw);
584 return s;
585}
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200586
587static ISADeviceInfo parallel_isa_info = {
588 .qdev.name = "isa-parallel",
589 .qdev.size = sizeof(ISAParallelState),
590 .init = parallel_isa_initfn,
591 .qdev.props = (Property[]) {
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200592 DEFINE_PROP_HEX32("index", ISAParallelState, index, -1),
593 DEFINE_PROP_HEX32("iobase", ISAParallelState, iobase, -1),
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200594 DEFINE_PROP_UINT32("irq", ISAParallelState, isairq, 7),
595 DEFINE_PROP_CHR("chardev", ISAParallelState, state.chr),
596 DEFINE_PROP_END_OF_LIST(),
597 },
598};
599
600static void parallel_register_devices(void)
601{
602 isa_qdev_register(&parallel_isa_info);
603}
604
605device_init(parallel_register_devices)