atppc.c revision 1.29 1 /* $NetBSD: atppc.c,v 1.29 2011/05/26 02:29:23 jakllsch Exp $ */
2
3 /*
4 * Copyright (c) 2001 Alcove - Nicolas Souchu
5 * Copyright (c) 2003, 2004 Gary Thorpe <gathorpe (at) users.sourceforge.net>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * FreeBSD: src/sys/isa/ppc.c,v 1.26.2.5 2001/10/02 05:21:45 nsouch Exp
30 *
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: atppc.c,v 1.29 2011/05/26 02:29:23 jakllsch Exp $");
35
36 #include "opt_atppc.h"
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43 #include <sys/proc.h>
44 #include <sys/systm.h>
45 #include <sys/vnode.h>
46 #include <sys/syslog.h>
47
48 #include <sys/bus.h>
49 /*#include <sys/intr.h>*/
50
51 #include <dev/isa/isareg.h>
52
53 #include <dev/ic/atppcreg.h>
54 #include <dev/ic/atppcvar.h>
55
56 #include <dev/ppbus/ppbus_conf.h>
57 #include <dev/ppbus/ppbus_msq.h>
58 #include <dev/ppbus/ppbus_io.h>
59 #include <dev/ppbus/ppbus_var.h>
60
61 #ifdef ATPPC_DEBUG
62 int atppc_debug = 1;
63 #endif
64
65 #ifdef ATPPC_VERBOSE
66 int atppc_verbose = 1;
67 #endif
68
69 /* List of supported chipsets detection routines */
70 static int (*chipset_detect[])(struct atppc_softc *) = {
71 /* XXX Add these LATER: maybe as separate devices?
72 atppc_pc873xx_detect,
73 atppc_smc37c66xgt_detect,
74 atppc_w83877f_detect,
75 atppc_smc37c935_detect,
76 */
77 NULL
78 };
79
80
81 /* Prototypes for functions. */
82
83 /* Print function for config_found() */
84 static int atppc_print(void *, const char *);
85
86 /* Detection routines */
87 static int atppc_detect_fifo(struct atppc_softc *);
88 static int atppc_detect_chipset(struct atppc_softc *);
89 static int atppc_detect_generic(struct atppc_softc *);
90
91 /* Routines for ppbus interface (bus + device) */
92 static int atppc_read(device_t, char *, int, int, size_t *);
93 static int atppc_write(device_t, char *, int, int, size_t *);
94 static int atppc_setmode(device_t, int);
95 static int atppc_getmode(device_t);
96 static int atppc_check_epp_timeout(device_t);
97 static void atppc_reset_epp_timeout(device_t);
98 static void atppc_ecp_sync(device_t);
99 static int atppc_exec_microseq(device_t, struct ppbus_microseq * *);
100 static u_int8_t atppc_io(device_t, int, u_char *, int, u_char);
101 static int atppc_read_ivar(device_t, int, unsigned int *);
102 static int atppc_write_ivar(device_t, int, unsigned int *);
103 static int atppc_add_handler(device_t, void (*)(void *), void *);
104 static int atppc_remove_handler(device_t, void (*)(void *));
105
106 /* Utility functions */
107
108 /* Functions to read bytes into device's input buffer */
109 static void atppc_nibble_read(struct atppc_softc * const);
110 static void atppc_byte_read(struct atppc_softc * const);
111 static void atppc_epp_read(struct atppc_softc * const);
112 static void atppc_ecp_read(struct atppc_softc * const);
113 static void atppc_ecp_read_dma(struct atppc_softc *, unsigned int *,
114 unsigned char);
115 static void atppc_ecp_read_pio(struct atppc_softc *, unsigned int *,
116 unsigned char);
117 static void atppc_ecp_read_error(struct atppc_softc *);
118
119
120 /* Functions to write bytes to device's output buffer */
121 static void atppc_std_write(struct atppc_softc * const);
122 static void atppc_epp_write(struct atppc_softc * const);
123 static void atppc_fifo_write(struct atppc_softc * const);
124 static void atppc_fifo_write_dma(struct atppc_softc * const, unsigned char,
125 unsigned char);
126 static void atppc_fifo_write_pio(struct atppc_softc * const, unsigned char,
127 unsigned char);
128 static void atppc_fifo_write_error(struct atppc_softc * const,
129 const unsigned int);
130
131 /* Miscellaneous */
132 static int atppc_poll_str(const struct atppc_softc * const, const u_int8_t,
133 const u_int8_t);
134 static int atppc_wait_interrupt(struct atppc_softc * const, kcondvar_t *,
135 const u_int8_t);
136
137
138 /*
139 * Generic attach and detach functions for atppc device. If sc_dev_ok in soft
140 * configuration data is not ATPPC_ATTACHED, these should be skipped altogether.
141 */
142
143 /* Soft configuration attach for atppc */
144 void
145 atppc_sc_attach(struct atppc_softc *lsc)
146 {
147 /* Adapter used to configure ppbus device */
148 struct parport_adapter sc_parport_adapter;
149 char buf[64];
150
151 mutex_init(&lsc->sc_lock, MUTEX_DEFAULT, IPL_TTY);
152 cv_init(&lsc->sc_out_cv, "atppcout");
153 cv_init(&lsc->sc_in_cv, "atppcin");
154
155 /* Probe and set up chipset */
156 if (atppc_detect_chipset(lsc) != 0) {
157 if (atppc_detect_generic(lsc) != 0) {
158 ATPPC_DPRINTF(("%s: Error detecting chipset\n",
159 device_xname(lsc->sc_dev)));
160 }
161 }
162
163 /* Probe and setup FIFO queue */
164 if (atppc_detect_fifo(lsc) == 0) {
165 printf("%s: FIFO <depth,wthr,rthr>=<%d,%d,%d>\n",
166 device_xname(lsc->sc_dev), lsc->sc_fifo, lsc->sc_wthr,
167 lsc->sc_rthr);
168 }
169
170 /* Print out chipset capabilities */
171 snprintb(buf, sizeof(buf), "\20\1INTR\2DMA\3FIFO\4PS2\5ECP\6EPP",
172 lsc->sc_has);
173 printf("%s: capabilities=%s\n", device_xname(lsc->sc_dev), buf);
174
175 /* Initialize device's buffer pointers */
176 lsc->sc_outb = lsc->sc_outbstart = lsc->sc_inb = lsc->sc_inbstart
177 = NULL;
178 lsc->sc_inb_nbytes = lsc->sc_outb_nbytes = 0;
179
180 /* Last configuration step: set mode to standard mode */
181 if (atppc_setmode(lsc->sc_dev, PPBUS_COMPATIBLE) != 0) {
182 ATPPC_DPRINTF(("%s: unable to initialize mode.\n",
183 device_xname(lsc->sc_dev)));
184 }
185
186 /* Set up parport_adapter structure */
187
188 /* Set capabilites */
189 sc_parport_adapter.capabilities = 0;
190 if (lsc->sc_has & ATPPC_HAS_INTR) {
191 sc_parport_adapter.capabilities |= PPBUS_HAS_INTR;
192 }
193 if (lsc->sc_has & ATPPC_HAS_DMA) {
194 sc_parport_adapter.capabilities |= PPBUS_HAS_DMA;
195 }
196 if (lsc->sc_has & ATPPC_HAS_FIFO) {
197 sc_parport_adapter.capabilities |= PPBUS_HAS_FIFO;
198 }
199 if (lsc->sc_has & ATPPC_HAS_PS2) {
200 sc_parport_adapter.capabilities |= PPBUS_HAS_PS2;
201 }
202 if (lsc->sc_has & ATPPC_HAS_EPP) {
203 sc_parport_adapter.capabilities |= PPBUS_HAS_EPP;
204 }
205 if (lsc->sc_has & ATPPC_HAS_ECP) {
206 sc_parport_adapter.capabilities |= PPBUS_HAS_ECP;
207 }
208
209 /* Set function pointers */
210 sc_parport_adapter.parport_io = atppc_io;
211 sc_parport_adapter.parport_exec_microseq = atppc_exec_microseq;
212 sc_parport_adapter.parport_reset_epp_timeout =
213 atppc_reset_epp_timeout;
214 sc_parport_adapter.parport_setmode = atppc_setmode;
215 sc_parport_adapter.parport_getmode = atppc_getmode;
216 sc_parport_adapter.parport_ecp_sync = atppc_ecp_sync;
217 sc_parport_adapter.parport_read = atppc_read;
218 sc_parport_adapter.parport_write = atppc_write;
219 sc_parport_adapter.parport_read_ivar = atppc_read_ivar;
220 sc_parport_adapter.parport_write_ivar = atppc_write_ivar;
221 sc_parport_adapter.parport_dma_malloc = lsc->sc_dma_malloc;
222 sc_parport_adapter.parport_dma_free = lsc->sc_dma_free;
223 sc_parport_adapter.parport_add_handler = atppc_add_handler;
224 sc_parport_adapter.parport_remove_handler = atppc_remove_handler;
225
226 /* Initialize handler list, may be added to by grandchildren */
227 SLIST_INIT(&(lsc->sc_handler_listhead));
228
229 /* Initialize interrupt state */
230 lsc->sc_irqstat = ATPPC_IRQ_NONE;
231 lsc->sc_ecr_intr = lsc->sc_ctr_intr = lsc->sc_str_intr = 0;
232
233 /* Disable DMA/interrupts (each ppbus driver selects usage itself) */
234 lsc->sc_use = 0;
235
236 /* Configure child of the device. */
237 lsc->child = config_found(lsc->sc_dev, &(sc_parport_adapter),
238 atppc_print);
239
240 return;
241 }
242
243 /* Soft configuration detach */
244 int
245 atppc_sc_detach(struct atppc_softc *lsc, int flag)
246 {
247 device_t dev = lsc->sc_dev;
248
249 /* Detach children devices */
250 if (config_detach(lsc->child, flag) && !(flag & DETACH_QUIET)) {
251 aprint_error_dev(dev, "not able to detach child device, ");
252
253 if (!(flag & DETACH_FORCE)) {
254 printf("cannot detach\n");
255 return 1;
256 } else {
257 printf("continuing (DETACH_FORCE)\n");
258 }
259 }
260
261 if (!(flag & DETACH_QUIET))
262 printf("%s detached", device_xname(dev));
263
264 return 0;
265 }
266
267 /* Used by config_found() to print out device information */
268 static int
269 atppc_print(void *aux, const char *name)
270 {
271 /* Print out something on failure. */
272 if (name != NULL) {
273 printf("%s: child devices", name);
274 return UNCONF;
275 }
276
277 return QUIET;
278 }
279
280 /*
281 * Machine independent detection routines for atppc driver.
282 */
283
284 /* Detect parallel port I/O port: taken from FreeBSD code directly. */
285 int
286 atppc_detect_port(bus_space_tag_t iot, bus_space_handle_t ioh)
287 {
288 /*
289 * Much shorter than scheme used by lpt_isa_probe() and lpt_port_test()
290 * in original lpt driver.
291 * Write to data register common to all controllers and read back the
292 * values. Also tests control and status registers.
293 */
294
295 /*
296 * Cannot use convenient macros because the device's config structure
297 * may not have been created yet: major change from FreeBSD code.
298 */
299
300 int rval;
301 u_int8_t ctr_sav, dtr_sav, str_sav;
302
303 /* Store writtable registers' values and test if they can be read */
304 str_sav = bus_space_read_1(iot, ioh, ATPPC_SPP_STR);
305 ctr_sav = bus_space_read_1(iot, ioh, ATPPC_SPP_CTR);
306 dtr_sav = bus_space_read_1(iot, ioh, ATPPC_SPP_DTR);
307 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
308 BUS_SPACE_BARRIER_READ);
309
310 /*
311 * Ensure PS2 ports in output mode, also read back value of control
312 * register.
313 */
314 bus_space_write_1(iot, ioh, ATPPC_SPP_CTR, 0x0c);
315 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
316 BUS_SPACE_BARRIER_WRITE);
317
318 if (bus_space_read_1(iot, ioh, ATPPC_SPP_CTR) != 0x0c) {
319 rval = 0;
320 } else {
321 /*
322 * Test if two values can be written and read from the data
323 * register.
324 */
325 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
326 BUS_SPACE_BARRIER_READ);
327 bus_space_write_1(iot, ioh, ATPPC_SPP_DTR, 0xaa);
328 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
329 BUS_SPACE_BARRIER_WRITE);
330 if (bus_space_read_1(iot, ioh, ATPPC_SPP_DTR) != 0xaa) {
331 rval = 1;
332 } else {
333 /* Second value to test */
334 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
335 BUS_SPACE_BARRIER_READ);
336 bus_space_write_1(iot, ioh, ATPPC_SPP_DTR, 0x55);
337 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
338 BUS_SPACE_BARRIER_WRITE);
339 if (bus_space_read_1(iot, ioh, ATPPC_SPP_DTR) != 0x55) {
340 rval = 1;
341 } else {
342 rval = 0;
343 }
344 }
345
346 }
347
348 /* Restore registers */
349 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
350 BUS_SPACE_BARRIER_READ);
351 bus_space_write_1(iot, ioh, ATPPC_SPP_CTR, ctr_sav);
352 bus_space_write_1(iot, ioh, ATPPC_SPP_DTR, dtr_sav);
353 bus_space_write_1(iot, ioh, ATPPC_SPP_STR, str_sav);
354 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
355 BUS_SPACE_BARRIER_WRITE);
356
357 return rval;
358 }
359
360 /* Detect parallel port chipset. */
361 static int
362 atppc_detect_chipset(struct atppc_softc *atppc)
363 {
364 /* Try each detection routine. */
365 int i, mode;
366 for (i = 0; chipset_detect[i] != NULL; i++) {
367 if ((mode = chipset_detect[i](atppc)) != -1) {
368 atppc->sc_mode = mode;
369 return 0;
370 }
371 }
372
373 return 1;
374 }
375
376 /* Detect generic capabilities. */
377 static int
378 atppc_detect_generic(struct atppc_softc *atppc)
379 {
380 u_int8_t ecr_sav = atppc_r_ecr(atppc);
381 u_int8_t ctr_sav = atppc_r_ctr(atppc);
382 u_int8_t str_sav = atppc_r_str(atppc);
383 u_int8_t tmp;
384 atppc_barrier_r(atppc);
385
386 /* Default to generic */
387 atppc->sc_type = ATPPC_TYPE_GENERIC;
388 atppc->sc_model = GENERIC;
389
390 /* Check for ECP */
391 tmp = atppc_r_ecr(atppc);
392 atppc_barrier_r(atppc);
393 if ((tmp & ATPPC_FIFO_EMPTY) && !(tmp & ATPPC_FIFO_FULL)) {
394 atppc_w_ecr(atppc, 0x34);
395 atppc_barrier_w(atppc);
396 tmp = atppc_r_ecr(atppc);
397 atppc_barrier_r(atppc);
398 if (tmp == 0x35) {
399 atppc->sc_has |= ATPPC_HAS_ECP;
400 }
401 }
402
403 /* Allow search for SMC style ECP+EPP mode */
404 if (atppc->sc_has & ATPPC_HAS_ECP) {
405 atppc_w_ecr(atppc, ATPPC_ECR_EPP);
406 atppc_barrier_w(atppc);
407 }
408 /* Check for EPP by checking for timeout bit */
409 if (atppc_check_epp_timeout(atppc->sc_dev) != 0) {
410 atppc->sc_has |= ATPPC_HAS_EPP;
411 atppc->sc_epp = ATPPC_EPP_1_9;
412 if (atppc->sc_has & ATPPC_HAS_ECP) {
413 /* SMC like chipset found */
414 atppc->sc_model = SMC_LIKE;
415 atppc->sc_type = ATPPC_TYPE_SMCLIKE;
416 }
417 }
418
419 /* Detect PS2 mode */
420 if (atppc->sc_has & ATPPC_HAS_ECP) {
421 /* Put ECP port into PS2 mode */
422 atppc_w_ecr(atppc, ATPPC_ECR_PS2);
423 atppc_barrier_w(atppc);
424 }
425 /* Put PS2 port in input mode: writes should not be readable */
426 atppc_w_ctr(atppc, 0x20);
427 atppc_barrier_w(atppc);
428 /*
429 * Write two values to data port: if neither are read back,
430 * bidirectional mode is functional.
431 */
432 atppc_w_dtr(atppc, 0xaa);
433 atppc_barrier_w(atppc);
434 tmp = atppc_r_dtr(atppc);
435 atppc_barrier_r(atppc);
436 if (tmp != 0xaa) {
437 atppc_w_dtr(atppc, 0x55);
438 atppc_barrier_w(atppc);
439 tmp = atppc_r_dtr(atppc);
440 atppc_barrier_r(atppc);
441 if (tmp != 0x55) {
442 atppc->sc_has |= ATPPC_HAS_PS2;
443 }
444 }
445
446 /* Restore to previous state */
447 atppc_w_ecr(atppc, ecr_sav);
448 atppc_w_ctr(atppc, ctr_sav);
449 atppc_w_str(atppc, str_sav);
450 atppc_barrier_w(atppc);
451
452 return 0;
453 }
454
455 /*
456 * Detect parallel port FIFO: taken from FreeBSD code directly.
457 */
458 static int
459 atppc_detect_fifo(struct atppc_softc *atppc)
460 {
461 #ifdef ATPPC_DEBUG
462 device_t dev = atppc->sc_dev;
463 #endif
464 u_int8_t ecr_sav;
465 u_int8_t ctr_sav;
466 u_int8_t str_sav;
467 u_int8_t cc;
468 short i;
469
470 /* If there is no ECP mode, we cannot config a FIFO */
471 if (!(atppc->sc_has & ATPPC_HAS_ECP)) {
472 return (EINVAL);
473 }
474
475 /* save registers */
476 ecr_sav = atppc_r_ecr(atppc);
477 ctr_sav = atppc_r_ctr(atppc);
478 str_sav = atppc_r_str(atppc);
479 atppc_barrier_r(atppc);
480
481 /* Enter ECP configuration mode, no interrupt, no DMA */
482 atppc_w_ecr(atppc, (ATPPC_ECR_CFG | ATPPC_SERVICE_INTR) &
483 ~ATPPC_ENABLE_DMA);
484 atppc_barrier_w(atppc);
485
486 /* read PWord size - transfers in FIFO mode must be PWord aligned */
487 atppc->sc_pword = (atppc_r_cnfgA(atppc) & ATPPC_PWORD_MASK);
488 atppc_barrier_r(atppc);
489
490 /* XXX 16 and 32 bits implementations not supported */
491 if (atppc->sc_pword != ATPPC_PWORD_8) {
492 ATPPC_DPRINTF(("%s(%s): FIFO PWord(%d) not supported.\n",
493 __func__, device_xname(dev), atppc->sc_pword));
494 goto error;
495 }
496
497 /* Byte mode, reverse direction, no interrupt, no DMA */
498 atppc_w_ecr(atppc, ATPPC_ECR_PS2 | ATPPC_SERVICE_INTR);
499 atppc_w_ctr(atppc, (ctr_sav & ~IRQENABLE) | PCD);
500 /* enter ECP test mode, no interrupt, no DMA */
501 atppc_w_ecr(atppc, ATPPC_ECR_TST | ATPPC_SERVICE_INTR);
502 atppc_barrier_w(atppc);
503
504 /* flush the FIFO */
505 for (i = 0; i < 1024; i++) {
506 atppc_r_fifo(atppc);
507 atppc_barrier_r(atppc);
508 cc = atppc_r_ecr(atppc);
509 atppc_barrier_r(atppc);
510 if (cc & ATPPC_FIFO_EMPTY)
511 break;
512 }
513 if (i >= 1024) {
514 ATPPC_DPRINTF(("%s(%s): cannot flush FIFO.\n", __func__,
515 device_xname(dev)));
516 goto error;
517 }
518
519 /* Test mode, enable interrupts, no DMA */
520 atppc_w_ecr(atppc, ATPPC_ECR_TST);
521 atppc_barrier_w(atppc);
522
523 /* Determine readIntrThreshold - fill FIFO until serviceIntr is set */
524 for (i = atppc->sc_rthr = atppc->sc_fifo = 0; i < 1024; i++) {
525 atppc_w_fifo(atppc, (char)i);
526 atppc_barrier_w(atppc);
527 cc = atppc_r_ecr(atppc);
528 atppc_barrier_r(atppc);
529 if ((atppc->sc_rthr == 0) && (cc & ATPPC_SERVICE_INTR)) {
530 /* readThreshold reached */
531 atppc->sc_rthr = i + 1;
532 }
533 if (cc & ATPPC_FIFO_FULL) {
534 atppc->sc_fifo = i + 1;
535 break;
536 }
537 }
538 if (i >= 1024) {
539 ATPPC_DPRINTF(("%s(%s): cannot fill FIFO.\n", __func__,
540 device_xname(dev)));
541 goto error;
542 }
543
544 /* Change direction */
545 atppc_w_ctr(atppc, (ctr_sav & ~IRQENABLE) & ~PCD);
546 atppc_barrier_w(atppc);
547
548 /* Clear the serviceIntr bit we've already set in the above loop */
549 atppc_w_ecr(atppc, ATPPC_ECR_TST);
550 atppc_barrier_w(atppc);
551
552 /* Determine writeIntrThreshold - empty FIFO until serviceIntr is set */
553 for (atppc->sc_wthr = 0; i > -1; i--) {
554 cc = atppc_r_fifo(atppc);
555 atppc_barrier_r(atppc);
556 if (cc != (char)(atppc->sc_fifo - i - 1)) {
557 ATPPC_DPRINTF(("%s(%s): invalid data in FIFO.\n",
558 __func__, device_xname(dev)));
559 goto error;
560 }
561
562 cc = atppc_r_ecr(atppc);
563 atppc_barrier_r(atppc);
564 if ((atppc->sc_wthr == 0) && (cc & ATPPC_SERVICE_INTR)) {
565 /* writeIntrThreshold reached */
566 atppc->sc_wthr = atppc->sc_fifo - i;
567 }
568
569 if (i > 0 && (cc & ATPPC_FIFO_EMPTY)) {
570 /* If FIFO empty before the last byte, error */
571 ATPPC_DPRINTF(("%s(%s): data lost in FIFO.\n", __func__,
572 device_xname(dev)));
573 goto error;
574 }
575 }
576
577 /* FIFO must be empty after the last byte */
578 cc = atppc_r_ecr(atppc);
579 atppc_barrier_r(atppc);
580 if (!(cc & ATPPC_FIFO_EMPTY)) {
581 ATPPC_DPRINTF(("%s(%s): cannot empty the FIFO.\n", __func__,
582 device_xname(dev)));
583 goto error;
584 }
585
586 /* Restore original registers */
587 atppc_w_ctr(atppc, ctr_sav);
588 atppc_w_str(atppc, str_sav);
589 atppc_w_ecr(atppc, ecr_sav);
590 atppc_barrier_w(atppc);
591
592 /* Update capabilities */
593 atppc->sc_has |= ATPPC_HAS_FIFO;
594
595 return 0;
596
597 error:
598 /* Restore original registers */
599 atppc_w_ctr(atppc, ctr_sav);
600 atppc_w_str(atppc, str_sav);
601 atppc_w_ecr(atppc, ecr_sav);
602 atppc_barrier_w(atppc);
603
604 return (EINVAL);
605 }
606
607 /* Interrupt handler for atppc device: wakes up read/write functions */
608 int
609 atppcintr(void *arg)
610 {
611 device_t dev = arg;
612 struct atppc_softc *atppc = device_private(dev);
613 int claim = 1;
614 enum { NONE, READER, WRITER } wake_up = NONE;
615
616 mutex_enter(&atppc->sc_lock);
617
618 /* Record registers' status */
619 atppc->sc_str_intr = atppc_r_str(atppc);
620 atppc->sc_ctr_intr = atppc_r_ctr(atppc);
621 atppc->sc_ecr_intr = atppc_r_ecr(atppc);
622 atppc_barrier_r(atppc);
623
624 /* Determine cause of interrupt and wake up top half */
625 switch (atppc->sc_mode) {
626 case ATPPC_MODE_STD:
627 /* nAck pulsed for 5 usec, too fast to check reliably, assume */
628 atppc->sc_irqstat = ATPPC_IRQ_nACK;
629 if (atppc->sc_outb)
630 wake_up = WRITER;
631 else
632 claim = 0;
633 break;
634
635 case ATPPC_MODE_NIBBLE:
636 case ATPPC_MODE_PS2:
637 /* nAck is set low by device and then high on ack */
638 if (!(atppc->sc_str_intr & nACK)) {
639 claim = 0;
640 break;
641 }
642 atppc->sc_irqstat = ATPPC_IRQ_nACK;
643 if (atppc->sc_inb)
644 wake_up = READER;
645 break;
646
647 case ATPPC_MODE_ECP:
648 case ATPPC_MODE_FAST:
649 /* Confirm interrupt cause: these are not pulsed as in nAck. */
650 if (atppc->sc_ecr_intr & ATPPC_SERVICE_INTR) {
651 if (atppc->sc_ecr_intr & ATPPC_ENABLE_DMA)
652 atppc->sc_irqstat |= ATPPC_IRQ_DMA;
653 else
654 atppc->sc_irqstat |= ATPPC_IRQ_FIFO;
655
656 /* Decide where top half will be waiting */
657 if (atppc->sc_mode & ATPPC_MODE_ECP) {
658 if (atppc->sc_ctr_intr & PCD) {
659 if (atppc->sc_inb)
660 wake_up = READER;
661 else
662 claim = 0;
663 } else {
664 if (atppc->sc_outb)
665 wake_up = WRITER;
666 else
667 claim = 0;
668 }
669 } else {
670 if (atppc->sc_outb)
671 wake_up = WRITER;
672 else
673 claim = 0;
674 }
675 }
676 /* Determine if nFault has occurred */
677 if ((atppc->sc_mode & ATPPC_MODE_ECP) &&
678 (atppc->sc_ecr_intr & ATPPC_nFAULT_INTR) &&
679 !(atppc->sc_str_intr & nFAULT)) {
680
681 /* Device is requesting the channel */
682 atppc->sc_irqstat |= ATPPC_IRQ_nFAULT;
683 claim = 1;
684 }
685 break;
686
687 case ATPPC_MODE_EPP:
688 /* nAck pulsed for 5 usec, too fast to check reliably */
689 atppc->sc_irqstat = ATPPC_IRQ_nACK;
690 if (atppc->sc_inb)
691 wake_up = WRITER;
692 else if (atppc->sc_outb)
693 wake_up = READER;
694 else
695 claim = 0;
696 break;
697
698 default:
699 panic("%s: chipset is in invalid mode.", device_xname(dev));
700 }
701
702 if (claim) {
703 switch (wake_up) {
704 case NONE:
705 break;
706
707 case READER:
708 cv_broadcast(&atppc->sc_in_cv);
709 break;
710
711 case WRITER:
712 cv_broadcast(&atppc->sc_out_cv);
713 break;
714 }
715 }
716
717 /* Call all of the installed handlers */
718 if (claim) {
719 struct atppc_handler_node * callback;
720 SLIST_FOREACH(callback, &(atppc->sc_handler_listhead),
721 entries) {
722 (*callback->func)(callback->arg);
723 }
724 }
725
726 mutex_exit(&atppc->sc_lock);
727
728 return claim;
729 }
730
731
732 /* Functions which support ppbus interface */
733
734
735 /* Check EPP mode timeout */
736 static int
737 atppc_check_epp_timeout(device_t dev)
738 {
739 struct atppc_softc *atppc = device_private(dev);
740 int error;
741
742 mutex_enter(&atppc->sc_lock);
743
744 atppc_reset_epp_timeout(dev);
745 error = !(atppc_r_str(atppc) & TIMEOUT);
746 atppc_barrier_r(atppc);
747
748 mutex_exit(&atppc->sc_lock);
749
750 return (error);
751 }
752
753 /*
754 * EPP timeout, according to the PC87332 manual
755 * Semantics of clearing EPP timeout bit.
756 * PC87332 - reading SPP_STR does it...
757 * SMC - write 1 to EPP timeout bit XXX
758 * Others - (?) write 0 to EPP timeout bit
759 */
760 static void
761 atppc_reset_epp_timeout(device_t dev)
762 {
763 struct atppc_softc *atppc = device_private(dev);
764 register unsigned char r;
765
766 r = atppc_r_str(atppc);
767 atppc_barrier_r(atppc);
768 atppc_w_str(atppc, r | 0x1);
769 atppc_barrier_w(atppc);
770 atppc_w_str(atppc, r & 0xfe);
771 atppc_barrier_w(atppc);
772
773 return;
774 }
775
776
777 /* Read from atppc device: returns 0 on success. */
778 static int
779 atppc_read(device_t dev, char *buf, int len, int ioflag,
780 size_t *cnt)
781 {
782 struct atppc_softc *atppc = device_private(dev);
783 int error = 0;
784
785 mutex_enter(&atppc->sc_lock);
786
787 *cnt = 0;
788
789 /* Initialize buffer */
790 atppc->sc_inb = atppc->sc_inbstart = buf;
791 atppc->sc_inb_nbytes = len;
792
793 /* Initialize device input error state for new operation */
794 atppc->sc_inerr = 0;
795
796 /* Call appropriate function to read bytes */
797 switch(atppc->sc_mode) {
798 case ATPPC_MODE_STD:
799 case ATPPC_MODE_FAST:
800 error = ENODEV;
801 break;
802
803 case ATPPC_MODE_NIBBLE:
804 atppc_nibble_read(atppc);
805 break;
806
807 case ATPPC_MODE_PS2:
808 atppc_byte_read(atppc);
809 break;
810
811 case ATPPC_MODE_ECP:
812 atppc_ecp_read(atppc);
813 break;
814
815 case ATPPC_MODE_EPP:
816 atppc_epp_read(atppc);
817 break;
818
819 default:
820 panic("%s(%s): chipset in invalid mode.\n", __func__,
821 device_xname(dev));
822 }
823
824 /* Update counter*/
825 *cnt = (atppc->sc_inbstart - atppc->sc_inb);
826
827 /* Reset buffer */
828 atppc->sc_inb = atppc->sc_inbstart = NULL;
829 atppc->sc_inb_nbytes = 0;
830
831 if (!(error))
832 error = atppc->sc_inerr;
833
834 mutex_exit(&atppc->sc_lock);
835
836 return (error);
837 }
838
839 /* Write to atppc device: returns 0 on success. */
840 static int
841 atppc_write(device_t dev, char *buf, int len, int ioflag, size_t *cnt)
842 {
843 struct atppc_softc * const atppc = device_private(dev);
844 int error = 0;
845
846 *cnt = 0;
847
848 mutex_enter(&atppc->sc_lock);
849
850 /* Set up line buffer */
851 atppc->sc_outb = atppc->sc_outbstart = buf;
852 atppc->sc_outb_nbytes = len;
853
854 /* Initialize device output error state for new operation */
855 atppc->sc_outerr = 0;
856
857 /* Call appropriate function to write bytes */
858 switch (atppc->sc_mode) {
859 case ATPPC_MODE_STD:
860 atppc_std_write(atppc);
861 break;
862
863 case ATPPC_MODE_NIBBLE:
864 case ATPPC_MODE_PS2:
865 error = ENODEV;
866 break;
867
868 case ATPPC_MODE_FAST:
869 case ATPPC_MODE_ECP:
870 atppc_fifo_write(atppc);
871 break;
872
873 case ATPPC_MODE_EPP:
874 atppc_epp_write(atppc);
875 break;
876
877 default:
878 panic("%s(%s): chipset in invalid mode.\n", __func__,
879 device_xname(dev));
880 }
881
882 /* Update counter*/
883 *cnt = (atppc->sc_outbstart - atppc->sc_outb);
884
885 /* Reset output buffer */
886 atppc->sc_outb = atppc->sc_outbstart = NULL;
887 atppc->sc_outb_nbytes = 0;
888
889 if (!(error))
890 error = atppc->sc_outerr;
891
892 mutex_exit(&atppc->sc_lock);
893
894 return (error);
895 }
896
897 /*
898 * Set mode of chipset to mode argument. Modes not supported are ignored. If
899 * multiple modes are flagged, the mode is not changed. Mode's are those
900 * defined for ppbus_softc.sc_mode in ppbus_conf.h. Only ECP-capable chipsets
901 * can change their mode of operation. However, ALL operation modes support
902 * centronics mode and nibble mode. Modes determine both hardware AND software
903 * behaviour.
904 * NOTE: the mode for ECP should only be changed when the channel is in
905 * forward idle mode. This function does not make sure FIFO's have flushed or
906 * any consistency checks.
907 */
908 static int
909 atppc_setmode(device_t dev, int mode)
910 {
911 struct atppc_softc *atppc = device_private(dev);
912 u_int8_t ecr;
913 u_int8_t chipset_mode;
914 int rval = 0;
915
916 mutex_enter(&atppc->sc_lock);
917
918 /* If ECP capable, configure ecr register */
919 if (atppc->sc_has & ATPPC_HAS_ECP) {
920 /* Read ECR with mode masked out */
921 ecr = (atppc_r_ecr(atppc) & 0x1f);
922 atppc_barrier_r(atppc);
923
924 switch (mode) {
925 case PPBUS_ECP:
926 /* Set ECP mode */
927 ecr |= ATPPC_ECR_ECP;
928 chipset_mode = ATPPC_MODE_ECP;
929 break;
930
931 case PPBUS_EPP:
932 /* Set EPP mode */
933 if (atppc->sc_has & ATPPC_HAS_EPP) {
934 ecr |= ATPPC_ECR_EPP;
935 chipset_mode = ATPPC_MODE_EPP;
936 } else {
937 rval = ENODEV;
938 goto end;
939 }
940 break;
941
942 case PPBUS_FAST:
943 /* Set fast centronics mode */
944 ecr |= ATPPC_ECR_FIFO;
945 chipset_mode = ATPPC_MODE_FAST;
946 break;
947
948 case PPBUS_PS2:
949 /* Set PS2 mode */
950 ecr |= ATPPC_ECR_PS2;
951 chipset_mode = ATPPC_MODE_PS2;
952 break;
953
954 case PPBUS_COMPATIBLE:
955 /* Set standard mode */
956 ecr |= ATPPC_ECR_STD;
957 chipset_mode = ATPPC_MODE_STD;
958 break;
959
960 case PPBUS_NIBBLE:
961 /* Set nibble mode: uses chipset standard mode */
962 ecr |= ATPPC_ECR_STD;
963 chipset_mode = ATPPC_MODE_NIBBLE;
964 break;
965
966 default:
967 /* Invalid mode specified for ECP chip */
968 ATPPC_DPRINTF(("%s(%s): invalid mode passed as "
969 "argument.\n", __func__, device_xname(dev)));
970 rval = ENODEV;
971 goto end;
972 }
973
974 /* Switch to byte mode to be able to change modes. */
975 atppc_w_ecr(atppc, ATPPC_ECR_PS2);
976 atppc_barrier_w(atppc);
977
978 /* Update mode */
979 atppc_w_ecr(atppc, ecr);
980 atppc_barrier_w(atppc);
981 } else {
982 switch (mode) {
983 case PPBUS_EPP:
984 if (atppc->sc_has & ATPPC_HAS_EPP) {
985 chipset_mode = ATPPC_MODE_EPP;
986 } else {
987 rval = ENODEV;
988 goto end;
989 }
990 break;
991
992 case PPBUS_PS2:
993 if (atppc->sc_has & ATPPC_HAS_PS2) {
994 chipset_mode = ATPPC_MODE_PS2;
995 } else {
996 rval = ENODEV;
997 goto end;
998 }
999 break;
1000
1001 case PPBUS_NIBBLE:
1002 /* Set nibble mode (virtual) */
1003 chipset_mode = ATPPC_MODE_NIBBLE;
1004 break;
1005
1006 case PPBUS_COMPATIBLE:
1007 chipset_mode = ATPPC_MODE_STD;
1008 break;
1009
1010 case PPBUS_ECP:
1011 rval = ENODEV;
1012 goto end;
1013
1014 default:
1015 ATPPC_DPRINTF(("%s(%s): invalid mode passed as "
1016 "argument.\n", __func__, device_xname(dev)));
1017 rval = ENODEV;
1018 goto end;
1019 }
1020 }
1021
1022 atppc->sc_mode = chipset_mode;
1023 if (chipset_mode == ATPPC_MODE_PS2) {
1024 /* Set direction bit to reverse */
1025 ecr = atppc_r_ctr(atppc);
1026 atppc_barrier_r(atppc);
1027 ecr |= PCD;
1028 atppc_w_ctr(atppc, ecr);
1029 atppc_barrier_w(atppc);
1030 }
1031
1032 end:
1033 mutex_exit(&atppc->sc_lock);
1034
1035 return rval;
1036 }
1037
1038 /* Get the current mode of chipset */
1039 static int
1040 atppc_getmode(device_t dev)
1041 {
1042 struct atppc_softc *atppc = device_private(dev);
1043 int mode;
1044
1045 mutex_enter(&atppc->sc_lock);
1046
1047 /* The chipset can only be in one mode at a time logically */
1048 switch (atppc->sc_mode) {
1049 case ATPPC_MODE_ECP:
1050 mode = PPBUS_ECP;
1051 break;
1052
1053 case ATPPC_MODE_EPP:
1054 mode = PPBUS_EPP;
1055 break;
1056
1057 case ATPPC_MODE_PS2:
1058 mode = PPBUS_PS2;
1059 break;
1060
1061 case ATPPC_MODE_STD:
1062 mode = PPBUS_COMPATIBLE;
1063 break;
1064
1065 case ATPPC_MODE_NIBBLE:
1066 mode = PPBUS_NIBBLE;
1067 break;
1068
1069 case ATPPC_MODE_FAST:
1070 mode = PPBUS_FAST;
1071 break;
1072
1073 default:
1074 panic("%s(%s): device is in invalid mode!", __func__,
1075 device_xname(dev));
1076 break;
1077 }
1078
1079 mutex_exit(&atppc->sc_lock);
1080
1081 return mode;
1082 }
1083
1084
1085 /* Wait for FIFO buffer to empty for ECP-capable chipset */
1086 static void
1087 atppc_ecp_sync(device_t dev)
1088 {
1089 struct atppc_softc *atppc = device_private(dev);
1090 int i;
1091 u_int8_t r;
1092
1093 mutex_enter(&atppc->sc_lock);
1094
1095 /*
1096 * Only wait for FIFO to empty if mode is chipset is ECP-capable AND
1097 * the mode is either ECP or Fast Centronics.
1098 */
1099 r = atppc_r_ecr(atppc);
1100 atppc_barrier_r(atppc);
1101 r &= 0xe0;
1102 if (!(atppc->sc_has & ATPPC_HAS_ECP) || ((r != ATPPC_ECR_ECP)
1103 && (r != ATPPC_ECR_FIFO))) {
1104 goto end;
1105 }
1106
1107 /* Wait for FIFO to empty */
1108 for (i = 0; i < ((MAXBUSYWAIT/hz) * 1000000); i += 100) {
1109 r = atppc_r_ecr(atppc);
1110 atppc_barrier_r(atppc);
1111 if (r & ATPPC_FIFO_EMPTY) {
1112 goto end;
1113 }
1114 delay(100); /* Supposed to be a 100 usec delay */
1115 }
1116
1117 ATPPC_DPRINTF(("%s: ECP sync failed, data still in FIFO.\n",
1118 device_xname(dev)));
1119
1120 end:
1121 mutex_exit(&atppc->sc_lock);
1122
1123 return;
1124 }
1125
1126 /* Execute a microsequence to handle fast I/O operations. */
1127 static int
1128 atppc_exec_microseq(device_t dev, struct ppbus_microseq **p_msq)
1129 {
1130 struct atppc_softc *atppc = device_private(dev);
1131 struct ppbus_microseq *mi = *p_msq;
1132 char cc, *p;
1133 int i, iter, len;
1134 int error;
1135 register int reg;
1136 register unsigned char mask;
1137 register int accum = 0;
1138 register char *ptr = NULL;
1139 struct ppbus_microseq *stack = NULL;
1140
1141 mutex_enter(&atppc->sc_lock);
1142
1143 /* microsequence registers are equivalent to PC-like port registers */
1144
1145 #define r_reg(register,atppc) bus_space_read_1((atppc)->sc_iot, \
1146 (atppc)->sc_ioh, (register))
1147 #define w_reg(register, atppc, byte) bus_space_write_1((atppc)->sc_iot, \
1148 (atppc)->sc_ioh, (register), (byte))
1149
1150 /* Loop until microsequence execution finishes (ending op code) */
1151 for (;;) {
1152 switch (mi->opcode) {
1153 case MS_OP_RSET:
1154 cc = r_reg(mi->arg[0].i, atppc);
1155 atppc_barrier_r(atppc);
1156 cc &= (char)mi->arg[2].i; /* clear mask */
1157 cc |= (char)mi->arg[1].i; /* assert mask */
1158 w_reg(mi->arg[0].i, atppc, cc);
1159 atppc_barrier_w(atppc);
1160 mi++;
1161 break;
1162
1163 case MS_OP_RASSERT_P:
1164 reg = mi->arg[1].i;
1165 ptr = atppc->sc_ptr;
1166
1167 if ((len = mi->arg[0].i) == MS_ACCUM) {
1168 accum = atppc->sc_accum;
1169 for (; accum; accum--) {
1170 w_reg(reg, atppc, *ptr++);
1171 atppc_barrier_w(atppc);
1172 }
1173 atppc->sc_accum = accum;
1174 } else {
1175 for (i = 0; i < len; i++) {
1176 w_reg(reg, atppc, *ptr++);
1177 atppc_barrier_w(atppc);
1178 }
1179 }
1180
1181 atppc->sc_ptr = ptr;
1182 mi++;
1183 break;
1184
1185 case MS_OP_RFETCH_P:
1186 reg = mi->arg[1].i;
1187 mask = (char)mi->arg[2].i;
1188 ptr = atppc->sc_ptr;
1189
1190 if ((len = mi->arg[0].i) == MS_ACCUM) {
1191 accum = atppc->sc_accum;
1192 for (; accum; accum--) {
1193 *ptr++ = r_reg(reg, atppc) & mask;
1194 atppc_barrier_r(atppc);
1195 }
1196 atppc->sc_accum = accum;
1197 } else {
1198 for (i = 0; i < len; i++) {
1199 *ptr++ = r_reg(reg, atppc) & mask;
1200 atppc_barrier_r(atppc);
1201 }
1202 }
1203
1204 atppc->sc_ptr = ptr;
1205 mi++;
1206 break;
1207
1208 case MS_OP_RFETCH:
1209 *((char *)mi->arg[2].p) = r_reg(mi->arg[0].i, atppc) &
1210 (char)mi->arg[1].i;
1211 atppc_barrier_r(atppc);
1212 mi++;
1213 break;
1214
1215 case MS_OP_RASSERT:
1216 case MS_OP_DELAY:
1217 /* let's suppose the next instr. is the same */
1218 do {
1219 for (;mi->opcode == MS_OP_RASSERT; mi++) {
1220 w_reg(mi->arg[0].i, atppc,
1221 (char)mi->arg[1].i);
1222 atppc_barrier_w(atppc);
1223 }
1224
1225 for (;mi->opcode == MS_OP_DELAY; mi++) {
1226 delay(mi->arg[0].i);
1227 }
1228 } while (mi->opcode == MS_OP_RASSERT);
1229 break;
1230
1231 case MS_OP_ADELAY:
1232 if (mi->arg[0].i) {
1233 tsleep(atppc, PPBUSPRI, "atppcdelay",
1234 mi->arg[0].i * (hz/1000));
1235 }
1236 mi++;
1237 break;
1238
1239 case MS_OP_TRIG:
1240 reg = mi->arg[0].i;
1241 iter = mi->arg[1].i;
1242 p = (char *)mi->arg[2].p;
1243
1244 /* XXX delay limited to 255 us */
1245 for (i = 0; i < iter; i++) {
1246 w_reg(reg, atppc, *p++);
1247 atppc_barrier_w(atppc);
1248 delay((unsigned char)*p++);
1249 }
1250
1251 mi++;
1252 break;
1253
1254 case MS_OP_SET:
1255 atppc->sc_accum = mi->arg[0].i;
1256 mi++;
1257 break;
1258
1259 case MS_OP_DBRA:
1260 if (--atppc->sc_accum > 0) {
1261 mi += mi->arg[0].i;
1262 }
1263
1264 mi++;
1265 break;
1266
1267 case MS_OP_BRSET:
1268 cc = atppc_r_str(atppc);
1269 atppc_barrier_r(atppc);
1270 if ((cc & (char)mi->arg[0].i) == (char)mi->arg[0].i) {
1271 mi += mi->arg[1].i;
1272 }
1273 mi++;
1274 break;
1275
1276 case MS_OP_BRCLEAR:
1277 cc = atppc_r_str(atppc);
1278 atppc_barrier_r(atppc);
1279 if ((cc & (char)mi->arg[0].i) == 0) {
1280 mi += mi->arg[1].i;
1281 }
1282 mi++;
1283 break;
1284
1285 case MS_OP_BRSTAT:
1286 cc = atppc_r_str(atppc);
1287 atppc_barrier_r(atppc);
1288 if ((cc & ((char)mi->arg[0].i | (char)mi->arg[1].i)) ==
1289 (char)mi->arg[0].i) {
1290 mi += mi->arg[2].i;
1291 }
1292 mi++;
1293 break;
1294
1295 case MS_OP_C_CALL:
1296 /*
1297 * If the C call returns !0 then end the microseq.
1298 * The current state of ptr is passed to the C function
1299 */
1300 if ((error = mi->arg[0].f(mi->arg[1].p,
1301 atppc->sc_ptr))) {
1302 mutex_exit(&atppc->sc_lock);
1303 return (error);
1304 }
1305 mi++;
1306 break;
1307
1308 case MS_OP_PTR:
1309 atppc->sc_ptr = (char *)mi->arg[0].p;
1310 mi++;
1311 break;
1312
1313 case MS_OP_CALL:
1314 if (stack) {
1315 panic("%s - %s: too much calls", device_xname(dev),
1316 __func__);
1317 }
1318
1319 if (mi->arg[0].p) {
1320 /* store state of the actual microsequence */
1321 stack = mi;
1322
1323 /* jump to the new microsequence */
1324 mi = (struct ppbus_microseq *)mi->arg[0].p;
1325 } else {
1326 mi++;
1327 }
1328 break;
1329
1330 case MS_OP_SUBRET:
1331 /* retrieve microseq and pc state before the call */
1332 mi = stack;
1333
1334 /* reset the stack */
1335 stack = 0;
1336
1337 /* XXX return code */
1338
1339 mi++;
1340 break;
1341
1342 case MS_OP_PUT:
1343 case MS_OP_GET:
1344 case MS_OP_RET:
1345 /*
1346 * Can't return to atppc level during the execution
1347 * of a submicrosequence.
1348 */
1349 if (stack) {
1350 panic("%s: cannot return to atppc level",
1351 __func__);
1352 }
1353 /* update pc for atppc level of execution */
1354 *p_msq = mi;
1355
1356 mutex_exit(&atppc->sc_lock);
1357 return (0);
1358 break;
1359
1360 default:
1361 panic("%s: unknown microsequence "
1362 "opcode 0x%x", __func__, mi->opcode);
1363 break;
1364 }
1365 }
1366
1367 /* Should not be reached! */
1368 #ifdef ATPPC_DEBUG
1369 panic("%s: unexpected code reached!\n", __func__);
1370 #endif
1371 }
1372
1373 /* General I/O routine */
1374 static u_int8_t
1375 atppc_io(device_t dev, int iop, u_char *addr, int cnt, u_char byte)
1376 {
1377 struct atppc_softc *atppc = device_private(dev);
1378 u_int8_t val = 0;
1379
1380 mutex_enter(&atppc->sc_lock);
1381
1382 switch (iop) {
1383 case PPBUS_OUTSB_EPP:
1384 bus_space_write_multi_1(atppc->sc_iot, atppc->sc_ioh,
1385 ATPPC_EPP_DATA, addr, cnt);
1386 break;
1387 case PPBUS_OUTSW_EPP:
1388 bus_space_write_multi_2(atppc->sc_iot, atppc->sc_ioh,
1389 ATPPC_EPP_DATA, (u_int16_t *)addr, cnt);
1390 break;
1391 case PPBUS_OUTSL_EPP:
1392 bus_space_write_multi_4(atppc->sc_iot, atppc->sc_ioh,
1393 ATPPC_EPP_DATA, (u_int32_t *)addr, cnt);
1394 break;
1395 case PPBUS_INSB_EPP:
1396 bus_space_read_multi_1(atppc->sc_iot, atppc->sc_ioh,
1397 ATPPC_EPP_DATA, addr, cnt);
1398 break;
1399 case PPBUS_INSW_EPP:
1400 bus_space_read_multi_2(atppc->sc_iot, atppc->sc_ioh,
1401 ATPPC_EPP_DATA, (u_int16_t *)addr, cnt);
1402 break;
1403 case PPBUS_INSL_EPP:
1404 bus_space_read_multi_4(atppc->sc_iot, atppc->sc_ioh,
1405 ATPPC_EPP_DATA, (u_int32_t *)addr, cnt);
1406 break;
1407 case PPBUS_RDTR:
1408 val = (atppc_r_dtr(atppc));
1409 break;
1410 case PPBUS_RSTR:
1411 val = (atppc_r_str(atppc));
1412 break;
1413 case PPBUS_RCTR:
1414 val = (atppc_r_ctr(atppc));
1415 break;
1416 case PPBUS_REPP_A:
1417 val = (atppc_r_eppA(atppc));
1418 break;
1419 case PPBUS_REPP_D:
1420 val = (atppc_r_eppD(atppc));
1421 break;
1422 case PPBUS_RECR:
1423 val = (atppc_r_ecr(atppc));
1424 break;
1425 case PPBUS_RFIFO:
1426 val = (atppc_r_fifo(atppc));
1427 break;
1428 case PPBUS_WDTR:
1429 atppc_w_dtr(atppc, byte);
1430 break;
1431 case PPBUS_WSTR:
1432 atppc_w_str(atppc, byte);
1433 break;
1434 case PPBUS_WCTR:
1435 atppc_w_ctr(atppc, byte);
1436 break;
1437 case PPBUS_WEPP_A:
1438 atppc_w_eppA(atppc, byte);
1439 break;
1440 case PPBUS_WEPP_D:
1441 atppc_w_eppD(atppc, byte);
1442 break;
1443 case PPBUS_WECR:
1444 atppc_w_ecr(atppc, byte);
1445 break;
1446 case PPBUS_WFIFO:
1447 atppc_w_fifo(atppc, byte);
1448 break;
1449 default:
1450 panic("%s(%s): unknown I/O operation", device_xname(dev),
1451 __func__);
1452 break;
1453 }
1454
1455 atppc_barrier(atppc);
1456
1457 mutex_exit(&atppc->sc_lock);
1458
1459 return val;
1460 }
1461
1462 /* Read "instance variables" of atppc device */
1463 static int
1464 atppc_read_ivar(device_t dev, int index, unsigned int *val)
1465 {
1466 struct atppc_softc *atppc = device_private(dev);
1467 int rval = 0;
1468
1469 mutex_enter(&atppc->sc_lock);
1470
1471 switch(index) {
1472 case PPBUS_IVAR_EPP_PROTO:
1473 if (atppc->sc_epp == ATPPC_EPP_1_9)
1474 *val = PPBUS_EPP_1_9;
1475 else if (atppc->sc_epp == ATPPC_EPP_1_7)
1476 *val = PPBUS_EPP_1_7;
1477 /* XXX what if not using EPP ? */
1478 break;
1479
1480 case PPBUS_IVAR_INTR:
1481 *val = ((atppc->sc_use & ATPPC_USE_INTR) != 0);
1482 break;
1483
1484 case PPBUS_IVAR_DMA:
1485 *val = ((atppc->sc_use & ATPPC_USE_DMA) != 0);
1486 break;
1487
1488 default:
1489 rval = ENODEV;
1490 }
1491
1492 mutex_exit(&atppc->sc_lock);
1493
1494 return rval;
1495 }
1496
1497 /* Write "instance varaibles" of atppc device */
1498 static int
1499 atppc_write_ivar(device_t dev, int index, unsigned int *val)
1500 {
1501 struct atppc_softc *atppc = device_private(dev);
1502 int rval = 0;
1503
1504 mutex_enter(&atppc->sc_lock);
1505
1506 switch(index) {
1507 case PPBUS_IVAR_EPP_PROTO:
1508 if (*val == PPBUS_EPP_1_9 || *val == PPBUS_EPP_1_7)
1509 atppc->sc_epp = *val;
1510 else
1511 rval = EINVAL;
1512 break;
1513
1514 case PPBUS_IVAR_INTR:
1515 if (*val == 0)
1516 atppc->sc_use &= ~ATPPC_USE_INTR;
1517 else if (atppc->sc_has & ATPPC_HAS_INTR)
1518 atppc->sc_use |= ATPPC_USE_INTR;
1519 else
1520 rval = ENODEV;
1521 break;
1522
1523 case PPBUS_IVAR_DMA:
1524 if (*val == 0)
1525 atppc->sc_use &= ~ATPPC_USE_DMA;
1526 else if (atppc->sc_has & ATPPC_HAS_DMA)
1527 atppc->sc_use |= ATPPC_USE_DMA;
1528 else
1529 rval = ENODEV;
1530 break;
1531
1532 default:
1533 rval = ENODEV;
1534 }
1535
1536 mutex_exit(&atppc->sc_lock);
1537
1538 return rval;
1539 }
1540
1541 /* Add a handler routine to be called by the interrupt handler */
1542 static int
1543 atppc_add_handler(device_t dev, void (*handler)(void *), void *arg)
1544 {
1545 struct atppc_softc *atppc = device_private(dev);
1546 struct atppc_handler_node *callback;
1547 int rval = 0;
1548
1549 if (handler == NULL) {
1550 ATPPC_DPRINTF(("%s(%s): attempt to register NULL handler.\n",
1551 __func__, device_xname(dev)));
1552 rval = EINVAL;
1553 } else {
1554 callback = malloc(sizeof(struct atppc_handler_node), M_DEVBUF,
1555 M_NOWAIT);
1556 if (callback) {
1557 callback->func = handler;
1558 callback->arg = arg;
1559 mutex_enter(&atppc->sc_lock);
1560 SLIST_INSERT_HEAD(&(atppc->sc_handler_listhead),
1561 callback, entries);
1562 mutex_exit(&atppc->sc_lock);
1563 } else {
1564 rval = ENOMEM;
1565 }
1566 }
1567
1568 return rval;
1569 }
1570
1571 /* Remove a handler added by atppc_add_handler() */
1572 static int
1573 atppc_remove_handler(device_t dev, void (*handler)(void *))
1574 {
1575 struct atppc_softc *atppc = device_private(dev);
1576 struct atppc_handler_node *callback;
1577 int rval = EINVAL;
1578
1579 mutex_enter(&atppc->sc_lock);
1580
1581 if (SLIST_EMPTY(&(atppc->sc_handler_listhead)))
1582 panic("%s(%s): attempt to remove handler from empty list.\n",
1583 __func__, device_xname(dev));
1584
1585 /* Search list for handler */
1586 SLIST_FOREACH(callback, &(atppc->sc_handler_listhead), entries) {
1587 if (callback->func == handler) {
1588 SLIST_REMOVE(&(atppc->sc_handler_listhead), callback,
1589 atppc_handler_node, entries);
1590 rval = 0;
1591 break;
1592 }
1593 }
1594
1595 mutex_exit(&atppc->sc_lock);
1596
1597 if (rval == 0) {
1598 free(callback, M_DEVBUF);
1599 }
1600
1601 return rval;
1602 }
1603
1604 /* Utility functions */
1605
1606
1607 /*
1608 * Functions that read bytes from port into buffer: called from interrupt
1609 * handler depending on current chipset mode and cause of interrupt. Return
1610 * value: number of bytes moved.
1611 */
1612
1613 /* Only the lower 4 bits of the final value are valid */
1614 #define nibble2char(s) ((((s) & ~nACK) >> 3) | (~(s) & nBUSY) >> 4)
1615
1616 /* Read bytes in nibble mode */
1617 static void
1618 atppc_nibble_read(struct atppc_softc *atppc)
1619 {
1620 int i;
1621 u_int8_t nibble[2];
1622 u_int8_t ctr;
1623 u_int8_t str;
1624
1625 /* Enable interrupts if needed */
1626 if (atppc->sc_use & ATPPC_USE_INTR) {
1627 ctr = atppc_r_ctr(atppc);
1628 atppc_barrier_r(atppc);
1629 if (!(ctr & IRQENABLE)) {
1630 ctr |= IRQENABLE;
1631 atppc_w_ctr(atppc, ctr);
1632 atppc_barrier_w(atppc);
1633 }
1634 }
1635
1636 while (atppc->sc_inbstart < (atppc->sc_inb + atppc->sc_inb_nbytes)) {
1637 /* Check if device has data to send in idle phase */
1638 str = atppc_r_str(atppc);
1639 atppc_barrier_r(atppc);
1640 if (str & nDATAVAIL) {
1641 return;
1642 }
1643
1644 /* Nibble-mode handshake transfer */
1645 for (i = 0; i < 2; i++) {
1646 /* Event 7 - ready to take data (HOSTBUSY low) */
1647 ctr = atppc_r_ctr(atppc);
1648 atppc_barrier_r(atppc);
1649 ctr |= HOSTBUSY;
1650 atppc_w_ctr(atppc, ctr);
1651 atppc_barrier_w(atppc);
1652
1653 /* Event 8 - peripheral writes the first nibble */
1654
1655 /* Event 9 - peripheral set nAck low */
1656 atppc->sc_inerr = atppc_poll_str(atppc, 0, PTRCLK);
1657 if (atppc->sc_inerr)
1658 return;
1659
1660 /* read nibble */
1661 nibble[i] = atppc_r_str(atppc);
1662
1663 /* Event 10 - ack, nibble received */
1664 ctr &= ~HOSTBUSY;
1665 atppc_w_ctr(atppc, ctr);
1666
1667 /* Event 11 - wait ack from peripherial */
1668 if (atppc->sc_use & ATPPC_USE_INTR)
1669 atppc->sc_inerr = atppc_wait_interrupt(atppc,
1670 &atppc->sc_in_cv, ATPPC_IRQ_nACK);
1671 else
1672 atppc->sc_inerr = atppc_poll_str(atppc, PTRCLK,
1673 PTRCLK);
1674 if (atppc->sc_inerr)
1675 return;
1676 }
1677
1678 /* Store byte transfered */
1679 *(atppc->sc_inbstart) = ((nibble2char(nibble[1]) << 4) & 0xf0) |
1680 (nibble2char(nibble[0]) & 0x0f);
1681 atppc->sc_inbstart++;
1682 }
1683 }
1684
1685 /* Read bytes in bidirectional mode */
1686 static void
1687 atppc_byte_read(struct atppc_softc * const atppc)
1688 {
1689 u_int8_t ctr;
1690 u_int8_t str;
1691
1692 /* Check direction bit */
1693 ctr = atppc_r_ctr(atppc);
1694 atppc_barrier_r(atppc);
1695 if (!(ctr & PCD)) {
1696 ATPPC_DPRINTF(("%s: byte-mode read attempted without direction "
1697 "bit set.", device_xname(atppc->sc_dev)));
1698 atppc->sc_inerr = ENODEV;
1699 return;
1700 }
1701 /* Enable interrupts if needed */
1702 if (atppc->sc_use & ATPPC_USE_INTR) {
1703 if (!(ctr & IRQENABLE)) {
1704 ctr |= IRQENABLE;
1705 atppc_w_ctr(atppc, ctr);
1706 atppc_barrier_w(atppc);
1707 }
1708 }
1709
1710 /* Byte-mode handshake transfer */
1711 while (atppc->sc_inbstart < (atppc->sc_inb + atppc->sc_inb_nbytes)) {
1712 /* Check if device has data to send */
1713 str = atppc_r_str(atppc);
1714 atppc_barrier_r(atppc);
1715 if (str & nDATAVAIL) {
1716 return;
1717 }
1718
1719 /* Event 7 - ready to take data (nAUTO low) */
1720 ctr |= HOSTBUSY;
1721 atppc_w_ctr(atppc, ctr);
1722 atppc_barrier_w(atppc);
1723
1724 /* Event 9 - peripheral set nAck low */
1725 atppc->sc_inerr = atppc_poll_str(atppc, 0, PTRCLK);
1726 if (atppc->sc_inerr)
1727 return;
1728
1729 /* Store byte transfered */
1730 *(atppc->sc_inbstart) = atppc_r_dtr(atppc);
1731 atppc_barrier_r(atppc);
1732
1733 /* Event 10 - data received, can't accept more */
1734 ctr &= ~HOSTBUSY;
1735 atppc_w_ctr(atppc, ctr);
1736 atppc_barrier_w(atppc);
1737
1738 /* Event 11 - peripheral ack */
1739 if (atppc->sc_use & ATPPC_USE_INTR)
1740 atppc->sc_inerr = atppc_wait_interrupt(atppc,
1741 &atppc->sc_in_cv, ATPPC_IRQ_nACK);
1742 else
1743 atppc->sc_inerr = atppc_poll_str(atppc, PTRCLK, PTRCLK);
1744 if (atppc->sc_inerr)
1745 return;
1746
1747 /* Event 16 - strobe */
1748 str |= HOSTCLK;
1749 atppc_w_str(atppc, str);
1750 atppc_barrier_w(atppc);
1751 DELAY(1);
1752 str &= ~HOSTCLK;
1753 atppc_w_str(atppc, str);
1754 atppc_barrier_w(atppc);
1755
1756 /* Update counter */
1757 atppc->sc_inbstart++;
1758 }
1759 }
1760
1761 /* Read bytes in EPP mode */
1762 static void
1763 atppc_epp_read(struct atppc_softc * atppc)
1764 {
1765 if (atppc->sc_epp == ATPPC_EPP_1_9) {
1766 {
1767 uint8_t str;
1768 int i;
1769
1770 atppc_reset_epp_timeout(atppc->sc_dev);
1771 for (i = 0; i < atppc->sc_inb_nbytes; i++) {
1772 *(atppc->sc_inbstart) = atppc_r_eppD(atppc);
1773 atppc_barrier_r(atppc);
1774 str = atppc_r_str(atppc);
1775 atppc_barrier_r(atppc);
1776 if (str & TIMEOUT) {
1777 atppc->sc_inerr = EIO;
1778 break;
1779 }
1780 atppc->sc_inbstart++;
1781 }
1782 }
1783 } else {
1784 /* Read data block from EPP data register */
1785 atppc_r_eppD_multi(atppc, atppc->sc_inbstart,
1786 atppc->sc_inb_nbytes);
1787 atppc_barrier_r(atppc);
1788 /* Update buffer position, byte count and counter */
1789 atppc->sc_inbstart += atppc->sc_inb_nbytes;
1790 }
1791
1792 return;
1793 }
1794
1795 /* Read bytes in ECP mode */
1796 static void
1797 atppc_ecp_read(struct atppc_softc *atppc)
1798 {
1799 u_int8_t ecr;
1800 u_int8_t ctr;
1801 u_int8_t str;
1802 const unsigned char ctr_sav = atppc_r_ctr(atppc);
1803 const unsigned char ecr_sav = atppc_r_ecr(atppc);
1804 unsigned int worklen;
1805
1806 /* Check direction bit */
1807 ctr = ctr_sav;
1808 atppc_barrier_r(atppc);
1809 if (!(ctr & PCD)) {
1810 ATPPC_DPRINTF(("%s: ecp-mode read attempted without direction "
1811 "bit set.", device_xname(atppc->sc_dev)));
1812 atppc->sc_inerr = ENODEV;
1813 goto end;
1814 }
1815
1816 /* Clear device request if any */
1817 if (atppc->sc_use & ATPPC_USE_INTR)
1818 atppc->sc_irqstat &= ~ATPPC_IRQ_nFAULT;
1819
1820 while (atppc->sc_inbstart < (atppc->sc_inb + atppc->sc_inb_nbytes)) {
1821 ecr = atppc_r_ecr(atppc);
1822 atppc_barrier_r(atppc);
1823 if (ecr & ATPPC_FIFO_EMPTY) {
1824 /* Check for invalid state */
1825 if (ecr & ATPPC_FIFO_FULL) {
1826 atppc_ecp_read_error(atppc);
1827 break;
1828 }
1829
1830 /* Check if device has data to send */
1831 str = atppc_r_str(atppc);
1832 atppc_barrier_r(atppc);
1833 if (str & nDATAVAIL) {
1834 break;
1835 }
1836
1837 if (atppc->sc_use & ATPPC_USE_INTR) {
1838 /* Enable interrupts */
1839 ecr &= ~ATPPC_SERVICE_INTR;
1840 atppc_w_ecr(atppc, ecr);
1841 atppc_barrier_w(atppc);
1842 /* Wait for FIFO to fill */
1843 atppc->sc_inerr = atppc_wait_interrupt(atppc,
1844 &atppc->sc_in_cv, ATPPC_IRQ_FIFO);
1845 if (atppc->sc_inerr)
1846 break;
1847 } else {
1848 DELAY(1);
1849 }
1850 continue;
1851 }
1852 else if (ecr & ATPPC_FIFO_FULL) {
1853 /* Transfer sc_fifo bytes */
1854 worklen = atppc->sc_fifo;
1855 }
1856 else if (ecr & ATPPC_SERVICE_INTR) {
1857 /* Transfer sc_rthr bytes */
1858 worklen = atppc->sc_rthr;
1859 } else {
1860 /* At least one byte is in the FIFO */
1861 worklen = 1;
1862 }
1863
1864 if ((atppc->sc_use & ATPPC_USE_INTR) &&
1865 (atppc->sc_use & ATPPC_USE_DMA)) {
1866
1867 atppc_ecp_read_dma(atppc, &worklen, ecr);
1868 } else {
1869 atppc_ecp_read_pio(atppc, &worklen, ecr);
1870 }
1871
1872 if (atppc->sc_inerr) {
1873 atppc_ecp_read_error(atppc);
1874 break;
1875 }
1876
1877 /* Update counter */
1878 atppc->sc_inbstart += worklen;
1879 }
1880 end:
1881 atppc_w_ctr(atppc, ctr_sav);
1882 atppc_w_ecr(atppc, ecr_sav);
1883 atppc_barrier_w(atppc);
1884 }
1885
1886 /* Read bytes in ECP mode using DMA transfers */
1887 static void
1888 atppc_ecp_read_dma(struct atppc_softc *atppc, unsigned int *length,
1889 unsigned char ecr)
1890 {
1891 /* Limit transfer to maximum DMA size and start it */
1892 *length = min(*length, atppc->sc_dma_maxsize);
1893 atppc->sc_dmastat = ATPPC_DMA_INIT;
1894 atppc->sc_dma_start(atppc, atppc->sc_inbstart, *length,
1895 ATPPC_DMA_MODE_READ);
1896
1897 atppc->sc_dmastat = ATPPC_DMA_STARTED;
1898
1899 /* Enable interrupts, DMA */
1900 ecr &= ~ATPPC_SERVICE_INTR;
1901 ecr |= ATPPC_ENABLE_DMA;
1902 atppc_w_ecr(atppc, ecr);
1903 atppc_barrier_w(atppc);
1904
1905 /* Wait for DMA completion */
1906 atppc->sc_inerr = atppc_wait_interrupt(atppc, &atppc->sc_in_cv,
1907 ATPPC_IRQ_DMA);
1908 if (atppc->sc_inerr)
1909 return;
1910
1911 /* Get register value recorded by interrupt handler */
1912 ecr = atppc->sc_ecr_intr;
1913 /* Clear DMA programming */
1914 atppc->sc_dma_finish(atppc);
1915 atppc->sc_dmastat = ATPPC_DMA_COMPLETE;
1916 /* Disable DMA */
1917 ecr &= ~ATPPC_ENABLE_DMA;
1918 atppc_w_ecr(atppc, ecr);
1919 atppc_barrier_w(atppc);
1920 }
1921
1922 /* Read bytes in ECP mode using PIO transfers */
1923 static void
1924 atppc_ecp_read_pio(struct atppc_softc *atppc, unsigned int *length,
1925 unsigned char ecr)
1926 {
1927 /* Disable DMA */
1928 ecr &= ~ATPPC_ENABLE_DMA;
1929 atppc_w_ecr(atppc, ecr);
1930 atppc_barrier_w(atppc);
1931
1932 /* Read from FIFO */
1933 atppc_r_fifo_multi(atppc, atppc->sc_inbstart, *length);
1934 }
1935
1936 /* Handle errors for ECP reads */
1937 static void
1938 atppc_ecp_read_error(struct atppc_softc *atppc)
1939 {
1940 unsigned char ecr = atppc_r_ecr(atppc);
1941
1942 /* Abort DMA if not finished */
1943 if (atppc->sc_dmastat == ATPPC_DMA_STARTED) {
1944 atppc->sc_dma_abort(atppc);
1945 ATPPC_DPRINTF(("%s: DMA interrupted.\n", __func__));
1946 }
1947
1948 /* Check for invalid states */
1949 if ((ecr & ATPPC_FIFO_EMPTY) && (ecr & ATPPC_FIFO_FULL)) {
1950 ATPPC_DPRINTF(("%s: FIFO full+empty bits set.\n", __func__));
1951 ATPPC_DPRINTF(("%s: reseting FIFO.\n", __func__));
1952 atppc_w_ecr(atppc, ATPPC_ECR_PS2);
1953 atppc_barrier_w(atppc);
1954 }
1955 }
1956
1957 /*
1958 * Functions that write bytes to port from buffer: called from atppc_write()
1959 * function depending on current chipset mode. Returns number of bytes moved.
1960 */
1961
1962 /* Write bytes in std/bidirectional mode */
1963 static void
1964 atppc_std_write(struct atppc_softc * const atppc)
1965 {
1966 unsigned int timecount;
1967 unsigned char ctr;
1968
1969 ctr = atppc_r_ctr(atppc);
1970 atppc_barrier_r(atppc);
1971 /* Enable interrupts if needed */
1972 if (atppc->sc_use & ATPPC_USE_INTR) {
1973 if (!(ctr & IRQENABLE)) {
1974 ctr |= IRQENABLE;
1975 atppc_w_ctr(atppc, ctr);
1976 atppc_barrier_w(atppc);
1977 }
1978 }
1979
1980 while (atppc->sc_outbstart < (atppc->sc_outb + atppc->sc_outb_nbytes)) {
1981 /* Wait for peripheral to become ready for MAXBUSYWAIT */
1982 atppc->sc_outerr = atppc_poll_str(atppc, SPP_READY, SPP_MASK);
1983 if (atppc->sc_outerr)
1984 return;
1985
1986 /* Put data in data register */
1987 atppc_w_dtr(atppc, *(atppc->sc_outbstart));
1988 atppc_barrier_w(atppc);
1989 DELAY(1);
1990
1991 /* Pulse strobe to indicate valid data on lines */
1992 ctr |= STROBE;
1993 atppc_w_ctr(atppc, ctr);
1994 atppc_barrier_w(atppc);
1995 DELAY(1);
1996 ctr &= ~STROBE;
1997 atppc_w_ctr(atppc, ctr);
1998 atppc_barrier_w(atppc);
1999
2000 /* Wait for nACK for MAXBUSYWAIT */
2001 timecount = 0;
2002 if (atppc->sc_use & ATPPC_USE_INTR) {
2003 atppc->sc_outerr = atppc_wait_interrupt(atppc,
2004 &atppc->sc_out_cv, ATPPC_IRQ_nACK);
2005 if (atppc->sc_outerr)
2006 return;
2007 } else {
2008 /* Try to catch the pulsed acknowledgement */
2009 atppc->sc_outerr = atppc_poll_str(atppc, 0, nACK);
2010 if (atppc->sc_outerr)
2011 return;
2012 atppc->sc_outerr = atppc_poll_str(atppc, nACK, nACK);
2013 if (atppc->sc_outerr)
2014 return;
2015 }
2016
2017 /* Update buffer position, byte count and counter */
2018 atppc->sc_outbstart++;
2019 }
2020 }
2021
2022
2023 /* Write bytes in EPP mode */
2024 static void
2025 atppc_epp_write(struct atppc_softc *atppc)
2026 {
2027 if (atppc->sc_epp == ATPPC_EPP_1_9) {
2028 {
2029 uint8_t str;
2030 int i;
2031
2032 atppc_reset_epp_timeout(atppc->sc_dev);
2033 for (i = 0; i < atppc->sc_outb_nbytes; i++) {
2034 atppc_w_eppD(atppc, *(atppc->sc_outbstart));
2035 atppc_barrier_w(atppc);
2036 str = atppc_r_str(atppc);
2037 atppc_barrier_r(atppc);
2038 if (str & TIMEOUT) {
2039 atppc->sc_outerr = EIO;
2040 break;
2041 }
2042 atppc->sc_outbstart++;
2043 }
2044 }
2045 } else {
2046 /* Write data block to EPP data register */
2047 atppc_w_eppD_multi(atppc, atppc->sc_outbstart,
2048 atppc->sc_outb_nbytes);
2049 atppc_barrier_w(atppc);
2050 /* Update buffer position, byte count and counter */
2051 atppc->sc_outbstart += atppc->sc_outb_nbytes;
2052 }
2053
2054 return;
2055 }
2056
2057
2058 /* Write bytes in ECP/Fast Centronics mode */
2059 static void
2060 atppc_fifo_write(struct atppc_softc * const atppc)
2061 {
2062 unsigned char ctr;
2063 unsigned char ecr;
2064 const unsigned char ctr_sav = atppc_r_ctr(atppc);
2065 const unsigned char ecr_sav = atppc_r_ecr(atppc);
2066
2067 ctr = ctr_sav;
2068 ecr = ecr_sav;
2069 atppc_barrier_r(atppc);
2070
2071 /* Reset and flush FIFO */
2072 atppc_w_ecr(atppc, ATPPC_ECR_PS2);
2073 atppc_barrier_w(atppc);
2074 /* Disable nAck interrupts and initialize port bits */
2075 ctr &= ~(IRQENABLE | STROBE | AUTOFEED);
2076 atppc_w_ctr(atppc, ctr);
2077 atppc_barrier_w(atppc);
2078 /* Restore mode */
2079 atppc_w_ecr(atppc, ecr);
2080 atppc_barrier_w(atppc);
2081
2082 /* DMA or Programmed IO */
2083 if ((atppc->sc_use & ATPPC_USE_DMA) &&
2084 (atppc->sc_use & ATPPC_USE_INTR)) {
2085
2086 atppc_fifo_write_dma(atppc, ecr, ctr);
2087 } else {
2088 atppc_fifo_write_pio(atppc, ecr, ctr);
2089 }
2090
2091 /* Restore original register values */
2092 atppc_w_ctr(atppc, ctr_sav);
2093 atppc_w_ecr(atppc, ecr_sav);
2094 atppc_barrier_w(atppc);
2095 }
2096
2097 static void
2098 atppc_fifo_write_dma(struct atppc_softc * const atppc, unsigned char ecr,
2099 unsigned char ctr)
2100 {
2101 unsigned int len;
2102 unsigned int worklen;
2103
2104 for (len = (atppc->sc_outb + atppc->sc_outb_nbytes) -
2105 atppc->sc_outbstart; len > 0; len = (atppc->sc_outb +
2106 atppc->sc_outb_nbytes) - atppc->sc_outbstart) {
2107
2108 /* Wait for device to become ready */
2109 atppc->sc_outerr = atppc_poll_str(atppc, SPP_READY, SPP_MASK);
2110 if (atppc->sc_outerr)
2111 return;
2112
2113 /* Reset chipset for next DMA transfer */
2114 atppc_w_ecr(atppc, ATPPC_ECR_PS2);
2115 atppc_barrier_w(atppc);
2116 atppc_w_ecr(atppc, ecr);
2117 atppc_barrier_w(atppc);
2118
2119 /* Limit transfer to maximum DMA size and start it */
2120 worklen = min(len, atppc->sc_dma_maxsize);
2121 atppc->sc_dmastat = ATPPC_DMA_INIT;
2122 atppc->sc_dma_start(atppc, atppc->sc_outbstart,
2123 worklen, ATPPC_DMA_MODE_WRITE);
2124 atppc->sc_dmastat = ATPPC_DMA_STARTED;
2125
2126 /* Enable interrupts, DMA */
2127 ecr &= ~ATPPC_SERVICE_INTR;
2128 ecr |= ATPPC_ENABLE_DMA;
2129 atppc_w_ecr(atppc, ecr);
2130 atppc_barrier_w(atppc);
2131
2132 /* Wait for DMA completion */
2133 atppc->sc_outerr = atppc_wait_interrupt(atppc,
2134 &atppc->sc_out_cv, ATPPC_IRQ_DMA);
2135 if (atppc->sc_outerr) {
2136 atppc_fifo_write_error(atppc, worklen);
2137 return;
2138 }
2139 /* Get register value recorded by interrupt handler */
2140 ecr = atppc->sc_ecr_intr;
2141 /* Clear DMA programming */
2142 atppc->sc_dma_finish(atppc);
2143 atppc->sc_dmastat = ATPPC_DMA_COMPLETE;
2144 /* Disable DMA */
2145 ecr &= ~ATPPC_ENABLE_DMA;
2146 atppc_w_ecr(atppc, ecr);
2147 atppc_barrier_w(atppc);
2148
2149 /* Wait for FIFO to empty */
2150 for (;;) {
2151 if (ecr & ATPPC_FIFO_EMPTY) {
2152 if (ecr & ATPPC_FIFO_FULL) {
2153 atppc->sc_outerr = EIO;
2154 atppc_fifo_write_error(atppc, worklen);
2155 return;
2156 } else {
2157 break;
2158 }
2159 }
2160
2161 /* Enable service interrupt */
2162 ecr &= ~ATPPC_SERVICE_INTR;
2163 atppc_w_ecr(atppc, ecr);
2164 atppc_barrier_w(atppc);
2165
2166 atppc->sc_outerr = atppc_wait_interrupt(atppc,
2167 &atppc->sc_out_cv, ATPPC_IRQ_FIFO);
2168 if (atppc->sc_outerr) {
2169 atppc_fifo_write_error(atppc, worklen);
2170 return;
2171 }
2172
2173 /* Get register value recorded by interrupt handler */
2174 ecr = atppc->sc_ecr_intr;
2175 }
2176
2177 /* Update pointer */
2178 atppc->sc_outbstart += worklen;
2179 }
2180 }
2181
2182 static void
2183 atppc_fifo_write_pio(struct atppc_softc * const atppc, unsigned char ecr,
2184 unsigned char ctr)
2185 {
2186 unsigned int len;
2187 unsigned int worklen;
2188 unsigned int timecount;
2189
2190 /* Disable DMA */
2191 ecr &= ~ATPPC_ENABLE_DMA;
2192 atppc_w_ecr(atppc, ecr);
2193 atppc_barrier_w(atppc);
2194
2195 for (len = (atppc->sc_outb + atppc->sc_outb_nbytes) -
2196 atppc->sc_outbstart; len > 0; len = (atppc->sc_outb +
2197 atppc->sc_outb_nbytes) - atppc->sc_outbstart) {
2198
2199 /* Wait for device to become ready */
2200 atppc->sc_outerr = atppc_poll_str(atppc, SPP_READY, SPP_MASK);
2201 if (atppc->sc_outerr)
2202 return;
2203
2204 /* Limit transfer to minimum of space in FIFO and buffer */
2205 worklen = min(len, atppc->sc_fifo);
2206
2207 /* Write to FIFO */
2208 atppc_w_fifo_multi(atppc, atppc->sc_outbstart, worklen);
2209
2210 timecount = 0;
2211 if (atppc->sc_use & ATPPC_USE_INTR) {
2212 ecr = atppc_r_ecr(atppc);
2213 atppc_barrier_w(atppc);
2214
2215 /* Wait for interrupt */
2216 for (;;) {
2217 if (ecr & ATPPC_FIFO_EMPTY) {
2218 if (ecr & ATPPC_FIFO_FULL) {
2219 atppc->sc_outerr = EIO;
2220 atppc_fifo_write_error(atppc,
2221 worklen);
2222 return;
2223 } else {
2224 break;
2225 }
2226 }
2227
2228 /* Enable service interrupt */
2229 ecr &= ~ATPPC_SERVICE_INTR;
2230 atppc_w_ecr(atppc, ecr);
2231 atppc_barrier_w(atppc);
2232
2233 atppc->sc_outerr = atppc_wait_interrupt(atppc,
2234 &atppc->sc_out_cv, ATPPC_IRQ_FIFO);
2235 if (atppc->sc_outerr) {
2236 atppc_fifo_write_error(atppc, worklen);
2237 return;
2238 }
2239
2240 /* Get ECR value saved by interrupt handler */
2241 ecr = atppc->sc_ecr_intr;
2242 }
2243 } else {
2244 for (; timecount < ((MAXBUSYWAIT/hz)*1000000);
2245 timecount++) {
2246
2247 ecr = atppc_r_ecr(atppc);
2248 atppc_barrier_r(atppc);
2249 if (ecr & ATPPC_FIFO_EMPTY) {
2250 if (ecr & ATPPC_FIFO_FULL) {
2251 atppc->sc_outerr = EIO;
2252 atppc_fifo_write_error(atppc,
2253 worklen);
2254 return;
2255 } else {
2256 break;
2257 }
2258 }
2259 DELAY(1);
2260 }
2261
2262 if (((timecount*hz)/1000000) >= MAXBUSYWAIT) {
2263 atppc->sc_outerr = EIO;
2264 atppc_fifo_write_error(atppc, worklen);
2265 return;
2266 }
2267 }
2268
2269 /* Update pointer */
2270 atppc->sc_outbstart += worklen;
2271 }
2272 }
2273
2274 static void
2275 atppc_fifo_write_error(struct atppc_softc * const atppc,
2276 const unsigned int worklen)
2277 {
2278 unsigned char ecr = atppc_r_ecr(atppc);
2279
2280 /* Abort DMA if not finished */
2281 if (atppc->sc_dmastat == ATPPC_DMA_STARTED) {
2282 atppc->sc_dma_abort(atppc);
2283 ATPPC_DPRINTF(("%s: DMA interrupted.\n", __func__));
2284 }
2285
2286 /* Check for invalid states */
2287 if ((ecr & ATPPC_FIFO_EMPTY) && (ecr & ATPPC_FIFO_FULL)) {
2288 ATPPC_DPRINTF(("%s: FIFO full+empty bits set.\n", __func__));
2289 } else if (!(ecr & ATPPC_FIFO_EMPTY)) {
2290 unsigned char ctr = atppc_r_ctr(atppc);
2291 int bytes_left;
2292 int i;
2293
2294 ATPPC_DPRINTF(("%s(%s): FIFO not empty.\n", __func__,
2295 device_xname(atppc->sc_dev)));
2296
2297 /* Drive strobe low to stop data transfer */
2298 ctr &= ~STROBE;
2299 atppc_w_ctr(atppc, ctr);
2300 atppc_barrier_w(atppc);
2301
2302 /* Determine how many bytes remain in FIFO */
2303 for (i = 0; i < atppc->sc_fifo; i++) {
2304 atppc_w_fifo(atppc, (unsigned char)i);
2305 ecr = atppc_r_ecr(atppc);
2306 atppc_barrier_r(atppc);
2307 if (ecr & ATPPC_FIFO_FULL)
2308 break;
2309 }
2310 bytes_left = (atppc->sc_fifo) - (i + 1);
2311 ATPPC_DPRINTF(("%s: %d bytes left in FIFO.\n", __func__,
2312 bytes_left));
2313
2314 /* Update counter */
2315 atppc->sc_outbstart += (worklen - bytes_left);
2316 } else {
2317 /* Update counter */
2318 atppc->sc_outbstart += worklen;
2319 }
2320
2321 ATPPC_DPRINTF(("%s: reseting FIFO.\n", __func__));
2322 atppc_w_ecr(atppc, ATPPC_ECR_PS2);
2323 atppc_barrier_w(atppc);
2324 }
2325
2326 /*
2327 * Poll status register using mask and status for MAXBUSYWAIT.
2328 * Returns 0 if device ready, error value otherwise.
2329 */
2330 static int
2331 atppc_poll_str(const struct atppc_softc * const atppc, const u_int8_t status,
2332 const u_int8_t mask)
2333 {
2334 unsigned int timecount;
2335 u_int8_t str;
2336 int error = EIO;
2337
2338 /* Wait for str to have status for MAXBUSYWAIT */
2339 for (timecount = 0; timecount < ((MAXBUSYWAIT/hz)*1000000);
2340 timecount++) {
2341
2342 str = atppc_r_str(atppc);
2343 atppc_barrier_r(atppc);
2344 if ((str & mask) == status) {
2345 error = 0;
2346 break;
2347 }
2348 DELAY(1);
2349 }
2350
2351 return error;
2352 }
2353
2354 /* Wait for interrupt for MAXBUSYWAIT: returns 0 if acknowledge received. */
2355 static int
2356 atppc_wait_interrupt(struct atppc_softc * const atppc, kcondvar_t *cv,
2357 const u_int8_t irqstat)
2358 {
2359 int error = EIO;
2360
2361 atppc->sc_irqstat &= ~irqstat;
2362
2363 /* Wait for interrupt for MAXBUSYWAIT */
2364 error = cv_timedwait_sig(cv, &atppc->sc_lock, MAXBUSYWAIT);
2365
2366 if (!(error) && (atppc->sc_irqstat & irqstat)) {
2367 atppc->sc_irqstat &= ~irqstat;
2368 error = 0;
2369 }
2370
2371 return error;
2372 }
2373