ppb_1284.c revision 28257
1/*-
2 * Copyright (c) 1997 Nicolas Souchu
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 *	$Id$
27 *
28 */
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/errno.h>
32#include <sys/conf.h>
33#include <sys/proc.h>
34#include <sys/buf.h>
35#include <sys/kernel.h>
36#include <sys/malloc.h>
37#include <sys/uio.h>
38#include <sys/syslog.h>
39
40#include <machine/clock.h>
41
42#include <dev/ppbus/ppbconf.h>
43#include <dev/ppbus/ppb_1284.h>
44
45/*
46 * nibble_1284_wait()
47 *
48 * Wait for the peripherial up to 40ms
49 */
50int
51nibble_1284_wait(struct ppb_device *dev, char mask, char status)
52{
53	int i;
54
55	return (ppb_poll_device(dev, 4, mask, status, PPB_NOINTR));
56}
57
58#define nibble2char(s) (((s & ~nACK) >> 3) | (~s & nBUSY) >> 4)
59
60/*
61 * nibble_1284_inbyte()
62 *
63 * Read data in NIBBLE mode
64 */
65int
66nibble_1284_inbyte(struct ppb_device *dev, char *buffer)
67{
68	char nibble[2], r;
69	int i, error;
70
71	r = ppb_rctr(dev);
72
73	for (i = 0; i < 2; i++) {
74		/* ready to take data (nAUTO low) */
75		ppb_wctr(dev, r | AUTOFEED);
76
77		if ((error = nibble_1284_wait(dev, nACK, 0))) {
78			ppb_wctr(dev, r);
79			return (error);
80		}
81
82		/* read nibble */
83		nibble[i] = ppb_rstr(dev);
84
85#ifdef DEBUG_1284
86		printf("nibble_1284_inbyte: nibble[%d]=0x%x\n", i, nibble[i]);
87#endif
88
89		/* ack, not ready for another nibble */
90		ppb_wctr(dev, r & ~AUTOFEED);
91
92		/* wait ack from peripherial */
93		if ((error = nibble_1284_wait(dev, nACK, nACK))) {
94			ppb_wctr(dev, r);
95			return (error);
96		}
97	}
98
99	*buffer = ((nibble2char(nibble[1]) << 4) & 0xf0) |
100				(nibble2char(nibble[0]) & 0x0f);
101
102#ifdef DEBUG_1284
103	printf("nibble_1284_inbyte: byte=0x%x\n", *buffer);
104#endif
105
106	return (0);
107}
108
109/*
110 * nibble_1284_sync()
111 */
112void
113nibble_1284_sync(struct ppb_device *dev)
114{
115	char ctr;
116
117	ctr = ppb_rctr(dev);
118
119	ppb_wctr(dev, (ctr & ~AUTOFEED) | SELECTIN);
120	if (nibble_1284_wait(dev, nACK, 0))
121		return;
122
123	ppb_wctr(dev, ctr | AUTOFEED);
124	nibble_1284_wait(dev, nACK, nACK);
125
126	ppb_wctr(dev, (ctr & ~AUTOFEED) | SELECTIN);
127
128	return;
129}
130
131/*
132 * nibble_1284_mode()
133 *
134 * Normal nibble mode or request device id mode (see ppb_1284.h)
135 */
136int
137nibble_1284_mode(struct ppb_device *dev, int mode)
138{
139	char ctrl;
140	int error;
141
142	ctrl = ppb_rctr(dev);
143
144	ppb_wdtr(dev, mode);
145	DELAY(5);
146
147	ppb_wctr(dev, (ctrl & ~SELECTIN) | AUTOFEED);
148	if ((error = nibble_1284_wait(dev, nACK | ERROR | SELECT | nFAULT,
149			ERROR | SELECT | nFAULT))) {
150		ppb_wctr(dev, ctrl);
151		return (error);
152	}
153
154	ppb_wctr(dev, ppb_rctr(dev) | STROBE);
155	DELAY(5);
156
157	ppb_wctr(dev, ppb_rctr(dev) & ~STROBE);
158	DELAY(5);
159
160	ppb_wctr(dev, ppb_rctr(dev) & ~AUTOFEED);
161
162	return (0);
163}
164