blob: cc853a57d2a6006f48a625f847c98ae2cebb58fc [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 {
ths5867c882007-02-17 23:44:43 +000068 uint8_t dataw;
69 uint8_t datar;
70 uint8_t status;
bellard6508fe52005-01-15 12:02:56 +000071 uint8_t control;
pbrookd537cf62007-04-07 18:14:41 +000072 qemu_irq irq;
bellard6508fe52005-01-15 12:02:56 +000073 int irq_pending;
74 CharDriverState *chr;
bellarde57a8c02005-11-10 23:58:52 +000075 int hw_driver;
ths5867c882007-02-17 23:44:43 +000076 int epp_timeout;
77 uint32_t last_read_offset; /* For debugging */
thsd60532c2007-06-18 18:55:46 +000078 /* Memory-mapped interface */
thsd60532c2007-06-18 18:55:46 +000079 int it_shift;
Blue Swirldefdb202011-02-05 14:51:57 +000080} ParallelState;
bellard6508fe52005-01-15 12:02:56 +000081
Gerd Hoffmann021f0672009-09-22 13:53:22 +020082typedef struct ISAParallelState {
83 ISADevice dev;
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +020084 uint32_t index;
Gerd Hoffmann021f0672009-09-22 13:53:22 +020085 uint32_t iobase;
86 uint32_t isairq;
87 ParallelState state;
88} ISAParallelState;
89
bellard6508fe52005-01-15 12:02:56 +000090static void parallel_update_irq(ParallelState *s)
91{
92 if (s->irq_pending)
pbrookd537cf62007-04-07 18:14:41 +000093 qemu_irq_raise(s->irq);
bellard6508fe52005-01-15 12:02:56 +000094 else
pbrookd537cf62007-04-07 18:14:41 +000095 qemu_irq_lower(s->irq);
bellard6508fe52005-01-15 12:02:56 +000096}
97
ths5867c882007-02-17 23:44:43 +000098static void
99parallel_ioport_write_sw(void *opaque, uint32_t addr, uint32_t val)
bellard6508fe52005-01-15 12:02:56 +0000100{
101 ParallelState *s = opaque;
ths3b46e622007-09-17 08:09:54 +0000102
ths5867c882007-02-17 23:44:43 +0000103 pdebug("write addr=0x%02x val=0x%02x\n", addr, val);
104
bellard6508fe52005-01-15 12:02:56 +0000105 addr &= 7;
bellard6508fe52005-01-15 12:02:56 +0000106 switch(addr) {
ths5867c882007-02-17 23:44:43 +0000107 case PARA_REG_DATA:
ths0fa7f152007-06-07 21:07:11 +0000108 s->dataw = val;
109 parallel_update_irq(s);
bellard6508fe52005-01-15 12:02:56 +0000110 break;
ths5867c882007-02-17 23:44:43 +0000111 case PARA_REG_CTR:
balrog52ccc5e2008-02-10 13:34:48 +0000112 val |= 0xc0;
ths0fa7f152007-06-07 21:07:11 +0000113 if ((val & PARA_CTR_INIT) == 0 ) {
114 s->status = PARA_STS_BUSY;
115 s->status |= PARA_STS_ACK;
116 s->status |= PARA_STS_ONLINE;
117 s->status |= PARA_STS_ERROR;
118 }
119 else if (val & PARA_CTR_SELECT) {
120 if (val & PARA_CTR_STROBE) {
121 s->status &= ~PARA_STS_BUSY;
122 if ((s->control & PARA_CTR_STROBE) == 0)
123 qemu_chr_write(s->chr, &s->dataw, 1);
124 } else {
125 if (s->control & PARA_CTR_INTEN) {
126 s->irq_pending = 1;
127 }
128 }
129 }
130 parallel_update_irq(s);
131 s->control = val;
bellard6508fe52005-01-15 12:02:56 +0000132 break;
133 }
134}
135
ths5867c882007-02-17 23:44:43 +0000136static void parallel_ioport_write_hw(void *opaque, uint32_t addr, uint32_t val)
137{
138 ParallelState *s = opaque;
139 uint8_t parm = val;
aurel32563e3c62008-08-22 08:57:09 +0000140 int dir;
ths5867c882007-02-17 23:44:43 +0000141
142 /* Sometimes programs do several writes for timing purposes on old
143 HW. Take care not to waste time on writes that do nothing. */
144
145 s->last_read_offset = ~0U;
146
147 addr &= 7;
148 switch(addr) {
149 case PARA_REG_DATA:
150 if (s->dataw == val)
ths0fa7f152007-06-07 21:07:11 +0000151 return;
152 pdebug("wd%02x\n", val);
153 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_WRITE_DATA, &parm);
154 s->dataw = val;
ths5867c882007-02-17 23:44:43 +0000155 break;
156 case PARA_REG_STS:
ths0fa7f152007-06-07 21:07:11 +0000157 pdebug("ws%02x\n", val);
158 if (val & PARA_STS_TMOUT)
159 s->epp_timeout = 0;
160 break;
ths5867c882007-02-17 23:44:43 +0000161 case PARA_REG_CTR:
162 val |= 0xc0;
163 if (s->control == val)
ths0fa7f152007-06-07 21:07:11 +0000164 return;
165 pdebug("wc%02x\n", val);
aurel32563e3c62008-08-22 08:57:09 +0000166
167 if ((val & PARA_CTR_DIR) != (s->control & PARA_CTR_DIR)) {
168 if (val & PARA_CTR_DIR) {
169 dir = 1;
170 } else {
171 dir = 0;
172 }
173 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_DATA_DIR, &dir);
174 parm &= ~PARA_CTR_DIR;
175 }
176
ths0fa7f152007-06-07 21:07:11 +0000177 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_WRITE_CONTROL, &parm);
178 s->control = val;
ths5867c882007-02-17 23:44:43 +0000179 break;
180 case PARA_REG_EPP_ADDR:
ths0fa7f152007-06-07 21:07:11 +0000181 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
182 /* Controls not correct for EPP address cycle, so do nothing */
183 pdebug("wa%02x s\n", val);
184 else {
185 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
186 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE_ADDR, &ioarg)) {
187 s->epp_timeout = 1;
188 pdebug("wa%02x t\n", val);
189 }
190 else
191 pdebug("wa%02x\n", val);
192 }
193 break;
ths5867c882007-02-17 23:44:43 +0000194 case PARA_REG_EPP_DATA:
ths0fa7f152007-06-07 21:07:11 +0000195 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
196 /* Controls not correct for EPP data cycle, so do nothing */
197 pdebug("we%02x s\n", val);
198 else {
199 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
200 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg)) {
201 s->epp_timeout = 1;
202 pdebug("we%02x t\n", val);
203 }
204 else
205 pdebug("we%02x\n", val);
206 }
207 break;
ths5867c882007-02-17 23:44:43 +0000208 }
209}
210
211static void
212parallel_ioport_eppdata_write_hw2(void *opaque, uint32_t addr, uint32_t val)
213{
214 ParallelState *s = opaque;
215 uint16_t eppdata = cpu_to_le16(val);
216 int err;
217 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000218 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000219 };
220 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
ths0fa7f152007-06-07 21:07:11 +0000221 /* Controls not correct for EPP data cycle, so do nothing */
222 pdebug("we%04x s\n", val);
223 return;
ths5867c882007-02-17 23:44:43 +0000224 }
225 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
226 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000227 s->epp_timeout = 1;
228 pdebug("we%04x t\n", val);
ths5867c882007-02-17 23:44:43 +0000229 }
230 else
ths0fa7f152007-06-07 21:07:11 +0000231 pdebug("we%04x\n", val);
ths5867c882007-02-17 23:44:43 +0000232}
233
234static void
235parallel_ioport_eppdata_write_hw4(void *opaque, uint32_t addr, uint32_t val)
236{
237 ParallelState *s = opaque;
238 uint32_t eppdata = cpu_to_le32(val);
239 int err;
240 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000241 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000242 };
243 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
ths0fa7f152007-06-07 21:07:11 +0000244 /* Controls not correct for EPP data cycle, so do nothing */
245 pdebug("we%08x s\n", val);
246 return;
ths5867c882007-02-17 23:44:43 +0000247 }
248 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
249 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000250 s->epp_timeout = 1;
251 pdebug("we%08x t\n", val);
ths5867c882007-02-17 23:44:43 +0000252 }
253 else
ths0fa7f152007-06-07 21:07:11 +0000254 pdebug("we%08x\n", val);
ths5867c882007-02-17 23:44:43 +0000255}
256
257static uint32_t parallel_ioport_read_sw(void *opaque, uint32_t addr)
bellard6508fe52005-01-15 12:02:56 +0000258{
259 ParallelState *s = opaque;
260 uint32_t ret = 0xff;
261
262 addr &= 7;
263 switch(addr) {
ths5867c882007-02-17 23:44:43 +0000264 case PARA_REG_DATA:
ths0fa7f152007-06-07 21:07:11 +0000265 if (s->control & PARA_CTR_DIR)
266 ret = s->datar;
267 else
268 ret = s->dataw;
bellard6508fe52005-01-15 12:02:56 +0000269 break;
ths5867c882007-02-17 23:44:43 +0000270 case PARA_REG_STS:
ths0fa7f152007-06-07 21:07:11 +0000271 ret = s->status;
272 s->irq_pending = 0;
273 if ((s->status & PARA_STS_BUSY) == 0 && (s->control & PARA_CTR_STROBE) == 0) {
274 /* XXX Fixme: wait 5 microseconds */
275 if (s->status & PARA_STS_ACK)
276 s->status &= ~PARA_STS_ACK;
277 else {
278 /* XXX Fixme: wait 5 microseconds */
279 s->status |= PARA_STS_ACK;
280 s->status |= PARA_STS_BUSY;
281 }
282 }
283 parallel_update_irq(s);
bellard6508fe52005-01-15 12:02:56 +0000284 break;
ths5867c882007-02-17 23:44:43 +0000285 case PARA_REG_CTR:
bellard6508fe52005-01-15 12:02:56 +0000286 ret = s->control;
287 break;
288 }
ths5867c882007-02-17 23:44:43 +0000289 pdebug("read addr=0x%02x val=0x%02x\n", addr, ret);
290 return ret;
291}
292
293static uint32_t parallel_ioport_read_hw(void *opaque, uint32_t addr)
294{
295 ParallelState *s = opaque;
296 uint8_t ret = 0xff;
297 addr &= 7;
298 switch(addr) {
299 case PARA_REG_DATA:
ths0fa7f152007-06-07 21:07:11 +0000300 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_DATA, &ret);
301 if (s->last_read_offset != addr || s->datar != ret)
302 pdebug("rd%02x\n", ret);
ths5867c882007-02-17 23:44:43 +0000303 s->datar = ret;
304 break;
305 case PARA_REG_STS:
ths0fa7f152007-06-07 21:07:11 +0000306 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &ret);
307 ret &= ~PARA_STS_TMOUT;
308 if (s->epp_timeout)
309 ret |= PARA_STS_TMOUT;
310 if (s->last_read_offset != addr || s->status != ret)
311 pdebug("rs%02x\n", ret);
312 s->status = ret;
ths5867c882007-02-17 23:44:43 +0000313 break;
314 case PARA_REG_CTR:
315 /* s->control has some bits fixed to 1. It is zero only when
ths0fa7f152007-06-07 21:07:11 +0000316 it has not been yet written to. */
317 if (s->control == 0) {
318 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_CONTROL, &ret);
319 if (s->last_read_offset != addr)
320 pdebug("rc%02x\n", ret);
321 s->control = ret;
322 }
323 else {
324 ret = s->control;
325 if (s->last_read_offset != addr)
326 pdebug("rc%02x\n", ret);
327 }
ths5867c882007-02-17 23:44:43 +0000328 break;
329 case PARA_REG_EPP_ADDR:
ths0fa7f152007-06-07 21:07:11 +0000330 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT))
331 /* Controls not correct for EPP addr cycle, so do nothing */
332 pdebug("ra%02x s\n", ret);
333 else {
334 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
335 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ_ADDR, &ioarg)) {
336 s->epp_timeout = 1;
337 pdebug("ra%02x t\n", ret);
338 }
339 else
340 pdebug("ra%02x\n", ret);
341 }
342 break;
ths5867c882007-02-17 23:44:43 +0000343 case PARA_REG_EPP_DATA:
ths0fa7f152007-06-07 21:07:11 +0000344 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT))
345 /* Controls not correct for EPP data cycle, so do nothing */
346 pdebug("re%02x s\n", ret);
347 else {
348 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
349 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg)) {
350 s->epp_timeout = 1;
351 pdebug("re%02x t\n", ret);
352 }
353 else
354 pdebug("re%02x\n", ret);
355 }
356 break;
ths5867c882007-02-17 23:44:43 +0000357 }
358 s->last_read_offset = addr;
359 return ret;
360}
361
362static uint32_t
363parallel_ioport_eppdata_read_hw2(void *opaque, uint32_t addr)
364{
365 ParallelState *s = opaque;
366 uint32_t ret;
367 uint16_t eppdata = ~0;
368 int err;
369 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000370 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000371 };
372 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
ths0fa7f152007-06-07 21:07:11 +0000373 /* Controls not correct for EPP data cycle, so do nothing */
374 pdebug("re%04x s\n", eppdata);
375 return eppdata;
ths5867c882007-02-17 23:44:43 +0000376 }
377 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
378 ret = le16_to_cpu(eppdata);
379
380 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000381 s->epp_timeout = 1;
382 pdebug("re%04x t\n", ret);
ths5867c882007-02-17 23:44:43 +0000383 }
384 else
ths0fa7f152007-06-07 21:07:11 +0000385 pdebug("re%04x\n", ret);
ths5867c882007-02-17 23:44:43 +0000386 return ret;
387}
388
389static uint32_t
390parallel_ioport_eppdata_read_hw4(void *opaque, uint32_t addr)
391{
392 ParallelState *s = opaque;
393 uint32_t ret;
394 uint32_t eppdata = ~0U;
395 int err;
396 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000397 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000398 };
399 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
ths0fa7f152007-06-07 21:07:11 +0000400 /* Controls not correct for EPP data cycle, so do nothing */
401 pdebug("re%08x s\n", eppdata);
402 return eppdata;
ths5867c882007-02-17 23:44:43 +0000403 }
404 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
405 ret = le32_to_cpu(eppdata);
406
407 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000408 s->epp_timeout = 1;
409 pdebug("re%08x t\n", ret);
ths5867c882007-02-17 23:44:43 +0000410 }
411 else
ths0fa7f152007-06-07 21:07:11 +0000412 pdebug("re%08x\n", ret);
ths5867c882007-02-17 23:44:43 +0000413 return ret;
414}
415
416static void parallel_ioport_ecp_write(void *opaque, uint32_t addr, uint32_t val)
417{
Blue Swirl7f5b7d32010-04-25 18:58:25 +0000418 pdebug("wecp%d=%02x\n", addr & 7, val);
ths5867c882007-02-17 23:44:43 +0000419}
420
421static uint32_t parallel_ioport_ecp_read(void *opaque, uint32_t addr)
422{
423 uint8_t ret = 0xff;
Blue Swirl7f5b7d32010-04-25 18:58:25 +0000424
425 pdebug("recp%d:%02x\n", addr & 7, 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);
Jan Kiszkaa08d4362009-06-27 09:25:07 +0200474 qemu_register_reset(parallel_reset, s);
bellard6508fe52005-01-15 12:02:56 +0000475
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200476 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &dummy) == 0) {
ths5867c882007-02-17 23:44:43 +0000477 s->hw_driver = 1;
ths0fa7f152007-06-07 21:07:11 +0000478 s->status = dummy;
ths5867c882007-02-17 23:44:43 +0000479 }
480
481 if (s->hw_driver) {
ths0fa7f152007-06-07 21:07:11 +0000482 register_ioport_write(base, 8, 1, parallel_ioport_write_hw, s);
483 register_ioport_read(base, 8, 1, parallel_ioport_read_hw, s);
Gleb Natapovdee41d52010-12-08 13:34:56 +0200484 isa_init_ioport_range(dev, base, 8);
485
ths0fa7f152007-06-07 21:07:11 +0000486 register_ioport_write(base+4, 1, 2, parallel_ioport_eppdata_write_hw2, s);
487 register_ioport_read(base+4, 1, 2, parallel_ioport_eppdata_read_hw2, s);
488 register_ioport_write(base+4, 1, 4, parallel_ioport_eppdata_write_hw4, s);
489 register_ioport_read(base+4, 1, 4, parallel_ioport_eppdata_read_hw4, s);
Gleb Natapovdee41d52010-12-08 13:34:56 +0200490 isa_init_ioport(dev, base+4);
ths0fa7f152007-06-07 21:07:11 +0000491 register_ioport_write(base+0x400, 8, 1, parallel_ioport_ecp_write, s);
492 register_ioport_read(base+0x400, 8, 1, parallel_ioport_ecp_read, s);
Gleb Natapovdee41d52010-12-08 13:34:56 +0200493 isa_init_ioport_range(dev, base+0x400, 8);
ths5867c882007-02-17 23:44:43 +0000494 }
495 else {
ths0fa7f152007-06-07 21:07:11 +0000496 register_ioport_write(base, 8, 1, parallel_ioport_write_sw, s);
497 register_ioport_read(base, 8, 1, parallel_ioport_read_sw, s);
Gleb Natapovdee41d52010-12-08 13:34:56 +0200498 isa_init_ioport_range(dev, base, 8);
ths5867c882007-02-17 23:44:43 +0000499 }
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200500 return 0;
501}
502
thsd60532c2007-06-18 18:55:46 +0000503/* Memory mapped interface */
Anthony Liguoric227f092009-10-01 16:12:16 -0500504static uint32_t parallel_mm_readb (void *opaque, target_phys_addr_t addr)
thsd60532c2007-06-18 18:55:46 +0000505{
506 ParallelState *s = opaque;
507
pbrook8da3ff12008-12-01 18:59:50 +0000508 return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFF;
thsd60532c2007-06-18 18:55:46 +0000509}
510
pbrook9596ebb2007-11-18 01:44:38 +0000511static void parallel_mm_writeb (void *opaque,
Anthony Liguoric227f092009-10-01 16:12:16 -0500512 target_phys_addr_t addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000513{
514 ParallelState *s = opaque;
515
pbrook8da3ff12008-12-01 18:59:50 +0000516 parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFF);
thsd60532c2007-06-18 18:55:46 +0000517}
518
Anthony Liguoric227f092009-10-01 16:12:16 -0500519static uint32_t parallel_mm_readw (void *opaque, target_phys_addr_t addr)
thsd60532c2007-06-18 18:55:46 +0000520{
521 ParallelState *s = opaque;
522
pbrook8da3ff12008-12-01 18:59:50 +0000523 return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFFFF;
thsd60532c2007-06-18 18:55:46 +0000524}
525
pbrook9596ebb2007-11-18 01:44:38 +0000526static void parallel_mm_writew (void *opaque,
Anthony Liguoric227f092009-10-01 16:12:16 -0500527 target_phys_addr_t addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000528{
529 ParallelState *s = opaque;
530
pbrook8da3ff12008-12-01 18:59:50 +0000531 parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFFFF);
thsd60532c2007-06-18 18:55:46 +0000532}
533
Anthony Liguoric227f092009-10-01 16:12:16 -0500534static uint32_t parallel_mm_readl (void *opaque, target_phys_addr_t addr)
thsd60532c2007-06-18 18:55:46 +0000535{
536 ParallelState *s = opaque;
537
pbrook8da3ff12008-12-01 18:59:50 +0000538 return parallel_ioport_read_sw(s, addr >> s->it_shift);
thsd60532c2007-06-18 18:55:46 +0000539}
540
pbrook9596ebb2007-11-18 01:44:38 +0000541static void parallel_mm_writel (void *opaque,
Anthony Liguoric227f092009-10-01 16:12:16 -0500542 target_phys_addr_t addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000543{
544 ParallelState *s = opaque;
545
pbrook8da3ff12008-12-01 18:59:50 +0000546 parallel_ioport_write_sw(s, addr >> s->it_shift, value);
thsd60532c2007-06-18 18:55:46 +0000547}
548
Blue Swirld60efc62009-08-25 18:29:31 +0000549static CPUReadMemoryFunc * const parallel_mm_read_sw[] = {
thsd60532c2007-06-18 18:55:46 +0000550 &parallel_mm_readb,
551 &parallel_mm_readw,
552 &parallel_mm_readl,
553};
554
Blue Swirld60efc62009-08-25 18:29:31 +0000555static CPUWriteMemoryFunc * const parallel_mm_write_sw[] = {
thsd60532c2007-06-18 18:55:46 +0000556 &parallel_mm_writeb,
557 &parallel_mm_writew,
558 &parallel_mm_writel,
559};
560
561/* If fd is zero, it means that the parallel device uses the console */
Blue Swirldefdb202011-02-05 14:51:57 +0000562bool parallel_mm_init(target_phys_addr_t base, int it_shift, qemu_irq irq,
563 CharDriverState *chr)
thsd60532c2007-06-18 18:55:46 +0000564{
565 ParallelState *s;
566 int io_sw;
567
568 s = qemu_mallocz(sizeof(ParallelState));
aurel3233093a02008-12-07 23:26:09 +0000569 s->irq = irq;
570 s->chr = chr;
thsd60532c2007-06-18 18:55:46 +0000571 s->it_shift = it_shift;
Jan Kiszkaa08d4362009-06-27 09:25:07 +0200572 qemu_register_reset(parallel_reset, s);
thsd60532c2007-06-18 18:55:46 +0000573
Alexander Graf2507c122010-12-08 12:05:37 +0100574 io_sw = cpu_register_io_memory(parallel_mm_read_sw, parallel_mm_write_sw,
575 s, DEVICE_NATIVE_ENDIAN);
thsd60532c2007-06-18 18:55:46 +0000576 cpu_register_physical_memory(base, 8 << it_shift, io_sw);
Blue Swirldefdb202011-02-05 14:51:57 +0000577 return true;
thsd60532c2007-06-18 18:55:46 +0000578}
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200579
580static ISADeviceInfo parallel_isa_info = {
581 .qdev.name = "isa-parallel",
582 .qdev.size = sizeof(ISAParallelState),
583 .init = parallel_isa_initfn,
584 .qdev.props = (Property[]) {
Gerd Hoffmann51954d52009-11-17 11:28:41 +0100585 DEFINE_PROP_UINT32("index", ISAParallelState, index, -1),
Gerd Hoffmanne8ee28f2009-10-13 13:38:39 +0200586 DEFINE_PROP_HEX32("iobase", ISAParallelState, iobase, -1),
Gerd Hoffmann021f0672009-09-22 13:53:22 +0200587 DEFINE_PROP_UINT32("irq", ISAParallelState, isairq, 7),
588 DEFINE_PROP_CHR("chardev", ISAParallelState, state.chr),
589 DEFINE_PROP_END_OF_LIST(),
590 },
591};
592
593static void parallel_register_devices(void)
594{
595 isa_qdev_register(&parallel_isa_info);
596}
597
598device_init(parallel_register_devices)