blob: a23686a461905e7fefecf61d103f758e22a4231c [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
81static void parallel_update_irq(ParallelState *s)
82{
83 if (s->irq_pending)
pbrookd537cf62007-04-07 18:14:41 +000084 qemu_irq_raise(s->irq);
bellard6508fe52005-01-15 12:02:56 +000085 else
pbrookd537cf62007-04-07 18:14:41 +000086 qemu_irq_lower(s->irq);
bellard6508fe52005-01-15 12:02:56 +000087}
88
ths5867c882007-02-17 23:44:43 +000089static void
90parallel_ioport_write_sw(void *opaque, uint32_t addr, uint32_t val)
bellard6508fe52005-01-15 12:02:56 +000091{
92 ParallelState *s = opaque;
ths3b46e622007-09-17 08:09:54 +000093
ths5867c882007-02-17 23:44:43 +000094 pdebug("write addr=0x%02x val=0x%02x\n", addr, val);
95
bellard6508fe52005-01-15 12:02:56 +000096 addr &= 7;
bellard6508fe52005-01-15 12:02:56 +000097 switch(addr) {
ths5867c882007-02-17 23:44:43 +000098 case PARA_REG_DATA:
ths0fa7f152007-06-07 21:07:11 +000099 s->dataw = val;
100 parallel_update_irq(s);
bellard6508fe52005-01-15 12:02:56 +0000101 break;
ths5867c882007-02-17 23:44:43 +0000102 case PARA_REG_CTR:
balrog52ccc5e2008-02-10 13:34:48 +0000103 val |= 0xc0;
ths0fa7f152007-06-07 21:07:11 +0000104 if ((val & PARA_CTR_INIT) == 0 ) {
105 s->status = PARA_STS_BUSY;
106 s->status |= PARA_STS_ACK;
107 s->status |= PARA_STS_ONLINE;
108 s->status |= PARA_STS_ERROR;
109 }
110 else if (val & PARA_CTR_SELECT) {
111 if (val & PARA_CTR_STROBE) {
112 s->status &= ~PARA_STS_BUSY;
113 if ((s->control & PARA_CTR_STROBE) == 0)
114 qemu_chr_write(s->chr, &s->dataw, 1);
115 } else {
116 if (s->control & PARA_CTR_INTEN) {
117 s->irq_pending = 1;
118 }
119 }
120 }
121 parallel_update_irq(s);
122 s->control = val;
bellard6508fe52005-01-15 12:02:56 +0000123 break;
124 }
125}
126
ths5867c882007-02-17 23:44:43 +0000127static void parallel_ioport_write_hw(void *opaque, uint32_t addr, uint32_t val)
128{
129 ParallelState *s = opaque;
130 uint8_t parm = val;
aurel32563e3c62008-08-22 08:57:09 +0000131 int dir;
ths5867c882007-02-17 23:44:43 +0000132
133 /* Sometimes programs do several writes for timing purposes on old
134 HW. Take care not to waste time on writes that do nothing. */
135
136 s->last_read_offset = ~0U;
137
138 addr &= 7;
139 switch(addr) {
140 case PARA_REG_DATA:
141 if (s->dataw == val)
ths0fa7f152007-06-07 21:07:11 +0000142 return;
143 pdebug("wd%02x\n", val);
144 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_WRITE_DATA, &parm);
145 s->dataw = val;
ths5867c882007-02-17 23:44:43 +0000146 break;
147 case PARA_REG_STS:
ths0fa7f152007-06-07 21:07:11 +0000148 pdebug("ws%02x\n", val);
149 if (val & PARA_STS_TMOUT)
150 s->epp_timeout = 0;
151 break;
ths5867c882007-02-17 23:44:43 +0000152 case PARA_REG_CTR:
153 val |= 0xc0;
154 if (s->control == val)
ths0fa7f152007-06-07 21:07:11 +0000155 return;
156 pdebug("wc%02x\n", val);
aurel32563e3c62008-08-22 08:57:09 +0000157
158 if ((val & PARA_CTR_DIR) != (s->control & PARA_CTR_DIR)) {
159 if (val & PARA_CTR_DIR) {
160 dir = 1;
161 } else {
162 dir = 0;
163 }
164 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_DATA_DIR, &dir);
165 parm &= ~PARA_CTR_DIR;
166 }
167
ths0fa7f152007-06-07 21:07:11 +0000168 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_WRITE_CONTROL, &parm);
169 s->control = val;
ths5867c882007-02-17 23:44:43 +0000170 break;
171 case PARA_REG_EPP_ADDR:
ths0fa7f152007-06-07 21:07:11 +0000172 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
173 /* Controls not correct for EPP address cycle, so do nothing */
174 pdebug("wa%02x s\n", val);
175 else {
176 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
177 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE_ADDR, &ioarg)) {
178 s->epp_timeout = 1;
179 pdebug("wa%02x t\n", val);
180 }
181 else
182 pdebug("wa%02x\n", val);
183 }
184 break;
ths5867c882007-02-17 23:44:43 +0000185 case PARA_REG_EPP_DATA:
ths0fa7f152007-06-07 21:07:11 +0000186 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
187 /* Controls not correct for EPP data cycle, so do nothing */
188 pdebug("we%02x s\n", val);
189 else {
190 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
191 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg)) {
192 s->epp_timeout = 1;
193 pdebug("we%02x t\n", val);
194 }
195 else
196 pdebug("we%02x\n", val);
197 }
198 break;
ths5867c882007-02-17 23:44:43 +0000199 }
200}
201
202static void
203parallel_ioport_eppdata_write_hw2(void *opaque, uint32_t addr, uint32_t val)
204{
205 ParallelState *s = opaque;
206 uint16_t eppdata = cpu_to_le16(val);
207 int err;
208 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000209 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000210 };
211 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
ths0fa7f152007-06-07 21:07:11 +0000212 /* Controls not correct for EPP data cycle, so do nothing */
213 pdebug("we%04x s\n", val);
214 return;
ths5867c882007-02-17 23:44:43 +0000215 }
216 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
217 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000218 s->epp_timeout = 1;
219 pdebug("we%04x t\n", val);
ths5867c882007-02-17 23:44:43 +0000220 }
221 else
ths0fa7f152007-06-07 21:07:11 +0000222 pdebug("we%04x\n", val);
ths5867c882007-02-17 23:44:43 +0000223}
224
225static void
226parallel_ioport_eppdata_write_hw4(void *opaque, uint32_t addr, uint32_t val)
227{
228 ParallelState *s = opaque;
229 uint32_t eppdata = cpu_to_le32(val);
230 int err;
231 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000232 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000233 };
234 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
ths0fa7f152007-06-07 21:07:11 +0000235 /* Controls not correct for EPP data cycle, so do nothing */
236 pdebug("we%08x s\n", val);
237 return;
ths5867c882007-02-17 23:44:43 +0000238 }
239 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
240 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000241 s->epp_timeout = 1;
242 pdebug("we%08x t\n", val);
ths5867c882007-02-17 23:44:43 +0000243 }
244 else
ths0fa7f152007-06-07 21:07:11 +0000245 pdebug("we%08x\n", val);
ths5867c882007-02-17 23:44:43 +0000246}
247
248static uint32_t parallel_ioport_read_sw(void *opaque, uint32_t addr)
bellard6508fe52005-01-15 12:02:56 +0000249{
250 ParallelState *s = opaque;
251 uint32_t ret = 0xff;
252
253 addr &= 7;
254 switch(addr) {
ths5867c882007-02-17 23:44:43 +0000255 case PARA_REG_DATA:
ths0fa7f152007-06-07 21:07:11 +0000256 if (s->control & PARA_CTR_DIR)
257 ret = s->datar;
258 else
259 ret = s->dataw;
bellard6508fe52005-01-15 12:02:56 +0000260 break;
ths5867c882007-02-17 23:44:43 +0000261 case PARA_REG_STS:
ths0fa7f152007-06-07 21:07:11 +0000262 ret = s->status;
263 s->irq_pending = 0;
264 if ((s->status & PARA_STS_BUSY) == 0 && (s->control & PARA_CTR_STROBE) == 0) {
265 /* XXX Fixme: wait 5 microseconds */
266 if (s->status & PARA_STS_ACK)
267 s->status &= ~PARA_STS_ACK;
268 else {
269 /* XXX Fixme: wait 5 microseconds */
270 s->status |= PARA_STS_ACK;
271 s->status |= PARA_STS_BUSY;
272 }
273 }
274 parallel_update_irq(s);
bellard6508fe52005-01-15 12:02:56 +0000275 break;
ths5867c882007-02-17 23:44:43 +0000276 case PARA_REG_CTR:
bellard6508fe52005-01-15 12:02:56 +0000277 ret = s->control;
278 break;
279 }
ths5867c882007-02-17 23:44:43 +0000280 pdebug("read addr=0x%02x val=0x%02x\n", addr, ret);
281 return ret;
282}
283
284static uint32_t parallel_ioport_read_hw(void *opaque, uint32_t addr)
285{
286 ParallelState *s = opaque;
287 uint8_t ret = 0xff;
288 addr &= 7;
289 switch(addr) {
290 case PARA_REG_DATA:
ths0fa7f152007-06-07 21:07:11 +0000291 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_DATA, &ret);
292 if (s->last_read_offset != addr || s->datar != ret)
293 pdebug("rd%02x\n", ret);
ths5867c882007-02-17 23:44:43 +0000294 s->datar = ret;
295 break;
296 case PARA_REG_STS:
ths0fa7f152007-06-07 21:07:11 +0000297 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &ret);
298 ret &= ~PARA_STS_TMOUT;
299 if (s->epp_timeout)
300 ret |= PARA_STS_TMOUT;
301 if (s->last_read_offset != addr || s->status != ret)
302 pdebug("rs%02x\n", ret);
303 s->status = ret;
ths5867c882007-02-17 23:44:43 +0000304 break;
305 case PARA_REG_CTR:
306 /* s->control has some bits fixed to 1. It is zero only when
ths0fa7f152007-06-07 21:07:11 +0000307 it has not been yet written to. */
308 if (s->control == 0) {
309 qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_CONTROL, &ret);
310 if (s->last_read_offset != addr)
311 pdebug("rc%02x\n", ret);
312 s->control = ret;
313 }
314 else {
315 ret = s->control;
316 if (s->last_read_offset != addr)
317 pdebug("rc%02x\n", ret);
318 }
ths5867c882007-02-17 23:44:43 +0000319 break;
320 case PARA_REG_EPP_ADDR:
ths0fa7f152007-06-07 21:07:11 +0000321 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT))
322 /* Controls not correct for EPP addr cycle, so do nothing */
323 pdebug("ra%02x s\n", ret);
324 else {
325 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
326 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ_ADDR, &ioarg)) {
327 s->epp_timeout = 1;
328 pdebug("ra%02x t\n", ret);
329 }
330 else
331 pdebug("ra%02x\n", ret);
332 }
333 break;
ths5867c882007-02-17 23:44:43 +0000334 case PARA_REG_EPP_DATA:
ths0fa7f152007-06-07 21:07:11 +0000335 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT))
336 /* Controls not correct for EPP data cycle, so do nothing */
337 pdebug("re%02x s\n", ret);
338 else {
339 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
340 if (qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg)) {
341 s->epp_timeout = 1;
342 pdebug("re%02x t\n", ret);
343 }
344 else
345 pdebug("re%02x\n", ret);
346 }
347 break;
ths5867c882007-02-17 23:44:43 +0000348 }
349 s->last_read_offset = addr;
350 return ret;
351}
352
353static uint32_t
354parallel_ioport_eppdata_read_hw2(void *opaque, uint32_t addr)
355{
356 ParallelState *s = opaque;
357 uint32_t ret;
358 uint16_t eppdata = ~0;
359 int err;
360 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000361 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000362 };
363 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
ths0fa7f152007-06-07 21:07:11 +0000364 /* Controls not correct for EPP data cycle, so do nothing */
365 pdebug("re%04x s\n", eppdata);
366 return eppdata;
ths5867c882007-02-17 23:44:43 +0000367 }
368 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
369 ret = le16_to_cpu(eppdata);
370
371 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000372 s->epp_timeout = 1;
373 pdebug("re%04x t\n", ret);
ths5867c882007-02-17 23:44:43 +0000374 }
375 else
ths0fa7f152007-06-07 21:07:11 +0000376 pdebug("re%04x\n", ret);
ths5867c882007-02-17 23:44:43 +0000377 return ret;
378}
379
380static uint32_t
381parallel_ioport_eppdata_read_hw4(void *opaque, uint32_t addr)
382{
383 ParallelState *s = opaque;
384 uint32_t ret;
385 uint32_t eppdata = ~0U;
386 int err;
387 struct ParallelIOArg ioarg = {
ths0fa7f152007-06-07 21:07:11 +0000388 .buffer = &eppdata, .count = sizeof(eppdata)
ths5867c882007-02-17 23:44:43 +0000389 };
390 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
ths0fa7f152007-06-07 21:07:11 +0000391 /* Controls not correct for EPP data cycle, so do nothing */
392 pdebug("re%08x s\n", eppdata);
393 return eppdata;
ths5867c882007-02-17 23:44:43 +0000394 }
395 err = qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
396 ret = le32_to_cpu(eppdata);
397
398 if (err) {
ths0fa7f152007-06-07 21:07:11 +0000399 s->epp_timeout = 1;
400 pdebug("re%08x t\n", ret);
ths5867c882007-02-17 23:44:43 +0000401 }
402 else
ths0fa7f152007-06-07 21:07:11 +0000403 pdebug("re%08x\n", ret);
ths5867c882007-02-17 23:44:43 +0000404 return ret;
405}
406
407static void parallel_ioport_ecp_write(void *opaque, uint32_t addr, uint32_t val)
408{
409 addr &= 7;
410 pdebug("wecp%d=%02x\n", addr, val);
411}
412
413static uint32_t parallel_ioport_ecp_read(void *opaque, uint32_t addr)
414{
415 uint8_t ret = 0xff;
416 addr &= 7;
417 pdebug("recp%d:%02x\n", addr, ret);
bellard6508fe52005-01-15 12:02:56 +0000418 return ret;
419}
420
aurel3233093a02008-12-07 23:26:09 +0000421static void parallel_reset(void *opaque)
bellard6508fe52005-01-15 12:02:56 +0000422{
aurel3233093a02008-12-07 23:26:09 +0000423 ParallelState *s = opaque;
424
ths5867c882007-02-17 23:44:43 +0000425 s->datar = ~0;
426 s->dataw = ~0;
bellard6508fe52005-01-15 12:02:56 +0000427 s->status = PARA_STS_BUSY;
428 s->status |= PARA_STS_ACK;
429 s->status |= PARA_STS_ONLINE;
430 s->status |= PARA_STS_ERROR;
balrog52ccc5e2008-02-10 13:34:48 +0000431 s->status |= PARA_STS_TMOUT;
bellard6508fe52005-01-15 12:02:56 +0000432 s->control = PARA_CTR_SELECT;
433 s->control |= PARA_CTR_INIT;
balrog52ccc5e2008-02-10 13:34:48 +0000434 s->control |= 0xc0;
ths5867c882007-02-17 23:44:43 +0000435 s->irq_pending = 0;
ths5867c882007-02-17 23:44:43 +0000436 s->hw_driver = 0;
437 s->epp_timeout = 0;
438 s->last_read_offset = ~0U;
thsd60532c2007-06-18 18:55:46 +0000439}
440
441/* If fd is zero, it means that the parallel device uses the console */
442ParallelState *parallel_init(int base, qemu_irq irq, CharDriverState *chr)
443{
444 ParallelState *s;
445 uint8_t dummy;
446
447 s = qemu_mallocz(sizeof(ParallelState));
aurel3233093a02008-12-07 23:26:09 +0000448 s->irq = irq;
449 s->chr = chr;
450 parallel_reset(s);
Jan Kiszka82176062009-05-02 00:29:37 +0200451 qemu_register_reset(parallel_reset, 0, s);
bellard6508fe52005-01-15 12:02:56 +0000452
ths5867c882007-02-17 23:44:43 +0000453 if (qemu_chr_ioctl(chr, CHR_IOCTL_PP_READ_STATUS, &dummy) == 0) {
454 s->hw_driver = 1;
ths0fa7f152007-06-07 21:07:11 +0000455 s->status = dummy;
ths5867c882007-02-17 23:44:43 +0000456 }
457
458 if (s->hw_driver) {
ths0fa7f152007-06-07 21:07:11 +0000459 register_ioport_write(base, 8, 1, parallel_ioport_write_hw, s);
460 register_ioport_read(base, 8, 1, parallel_ioport_read_hw, s);
461 register_ioport_write(base+4, 1, 2, parallel_ioport_eppdata_write_hw2, s);
462 register_ioport_read(base+4, 1, 2, parallel_ioport_eppdata_read_hw2, s);
463 register_ioport_write(base+4, 1, 4, parallel_ioport_eppdata_write_hw4, s);
464 register_ioport_read(base+4, 1, 4, parallel_ioport_eppdata_read_hw4, s);
465 register_ioport_write(base+0x400, 8, 1, parallel_ioport_ecp_write, s);
466 register_ioport_read(base+0x400, 8, 1, parallel_ioport_ecp_read, s);
ths5867c882007-02-17 23:44:43 +0000467 }
468 else {
ths0fa7f152007-06-07 21:07:11 +0000469 register_ioport_write(base, 8, 1, parallel_ioport_write_sw, s);
470 register_ioport_read(base, 8, 1, parallel_ioport_read_sw, s);
ths5867c882007-02-17 23:44:43 +0000471 }
bellard6508fe52005-01-15 12:02:56 +0000472 return s;
473}
thsd60532c2007-06-18 18:55:46 +0000474
475/* Memory mapped interface */
pbrook9596ebb2007-11-18 01:44:38 +0000476static uint32_t parallel_mm_readb (void *opaque, target_phys_addr_t addr)
thsd60532c2007-06-18 18:55:46 +0000477{
478 ParallelState *s = opaque;
479
pbrook8da3ff12008-12-01 18:59:50 +0000480 return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFF;
thsd60532c2007-06-18 18:55:46 +0000481}
482
pbrook9596ebb2007-11-18 01:44:38 +0000483static void parallel_mm_writeb (void *opaque,
484 target_phys_addr_t addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000485{
486 ParallelState *s = opaque;
487
pbrook8da3ff12008-12-01 18:59:50 +0000488 parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFF);
thsd60532c2007-06-18 18:55:46 +0000489}
490
pbrook9596ebb2007-11-18 01:44:38 +0000491static uint32_t parallel_mm_readw (void *opaque, target_phys_addr_t addr)
thsd60532c2007-06-18 18:55:46 +0000492{
493 ParallelState *s = opaque;
494
pbrook8da3ff12008-12-01 18:59:50 +0000495 return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFFFF;
thsd60532c2007-06-18 18:55:46 +0000496}
497
pbrook9596ebb2007-11-18 01:44:38 +0000498static void parallel_mm_writew (void *opaque,
499 target_phys_addr_t addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000500{
501 ParallelState *s = opaque;
502
pbrook8da3ff12008-12-01 18:59:50 +0000503 parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFFFF);
thsd60532c2007-06-18 18:55:46 +0000504}
505
pbrook9596ebb2007-11-18 01:44:38 +0000506static uint32_t parallel_mm_readl (void *opaque, target_phys_addr_t addr)
thsd60532c2007-06-18 18:55:46 +0000507{
508 ParallelState *s = opaque;
509
pbrook8da3ff12008-12-01 18:59:50 +0000510 return parallel_ioport_read_sw(s, addr >> s->it_shift);
thsd60532c2007-06-18 18:55:46 +0000511}
512
pbrook9596ebb2007-11-18 01:44:38 +0000513static void parallel_mm_writel (void *opaque,
514 target_phys_addr_t addr, uint32_t value)
thsd60532c2007-06-18 18:55:46 +0000515{
516 ParallelState *s = opaque;
517
pbrook8da3ff12008-12-01 18:59:50 +0000518 parallel_ioport_write_sw(s, addr >> s->it_shift, value);
thsd60532c2007-06-18 18:55:46 +0000519}
520
521static CPUReadMemoryFunc *parallel_mm_read_sw[] = {
522 &parallel_mm_readb,
523 &parallel_mm_readw,
524 &parallel_mm_readl,
525};
526
527static CPUWriteMemoryFunc *parallel_mm_write_sw[] = {
528 &parallel_mm_writeb,
529 &parallel_mm_writew,
530 &parallel_mm_writel,
531};
532
533/* If fd is zero, it means that the parallel device uses the console */
534ParallelState *parallel_mm_init(target_phys_addr_t base, int it_shift, qemu_irq irq, CharDriverState *chr)
535{
536 ParallelState *s;
537 int io_sw;
538
539 s = qemu_mallocz(sizeof(ParallelState));
aurel3233093a02008-12-07 23:26:09 +0000540 s->irq = irq;
541 s->chr = chr;
thsd60532c2007-06-18 18:55:46 +0000542 s->it_shift = it_shift;
aurel3233093a02008-12-07 23:26:09 +0000543 parallel_reset(s);
Jan Kiszka82176062009-05-02 00:29:37 +0200544 qemu_register_reset(parallel_reset, 0, s);
thsd60532c2007-06-18 18:55:46 +0000545
Avi Kivity1eed09c2009-06-14 11:38:51 +0300546 io_sw = cpu_register_io_memory(parallel_mm_read_sw, parallel_mm_write_sw, s);
thsd60532c2007-06-18 18:55:46 +0000547 cpu_register_physical_memory(base, 8 << it_shift, io_sw);
548 return s;
549}