summaryrefslogtreecommitdiffstats
path: root/sys/dev/ppbus/ppb_1284.c
blob: e13a69ccfb93d65eed8b5dc7b4b06b2ece2c7103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*-
 * Copyright (c) 1997 Nicolas Souchu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$Id: ppb_1284.c,v 1.4 1998/08/03 19:14:31 msmith Exp $
 *
 */

#include "opt_debug_1284.h"

#include <sys/param.h>
#include <sys/systm.h>

#include <machine/clock.h>

#include <dev/ppbus/ppbconf.h>
#include <dev/ppbus/ppb_1284.h>

/*
 * do_1284_wait()
 *
 * Wait for the peripherial up to 40ms
 */
int
do_1284_wait(struct ppb_device *dev, char mask, char status)
{
	int i;
	char r;

	/* try up to 5ms */
	for (i = 0; i < 20; i++) {
		r = ppb_rstr(dev);
		DELAY(25);
		if ((r & mask) == status)
			return (0);
	}

	return (ppb_poll_device(dev, 4, mask, status, PPB_NOINTR));
}

#define nibble2char(s) (((s & ~nACK) >> 3) | (~s & nBUSY) >> 4)

/*
 * byte_1284_inbyte()
 *
 * Read 1 byte in BYTE mode
 */
int
byte_1284_inbyte(struct ppb_device *dev, char *buffer)
{
	int error;

	/* notify the peripherial to put data on the lines */
	ppb_wctr(dev, PCD |  AUTOFEED | nSTROBE | nINIT | nSELECTIN);

	/* wait for valid byte signal */
	if ((error = do_1284_wait(dev, nACK, 0)))
		return (error);

	/* fetch data */
	*buffer = ppb_rdtr(dev);

	/* indicate that data has been received, not ready for another */
	ppb_wctr(dev, PCD | nAUTOFEED | nSTROBE | nINIT | nSELECTIN);

	/* wait peripherial's acknowledgement */
	if ((error = do_1284_wait(dev, nACK, nACK)))
		return (error);

	/* acknowledge the peripherial */
	ppb_wctr(dev, PCD | nAUTOFEED |  STROBE | nINIT | nSELECTIN);

	return (0);
}

/*
 * nibble_1284_inbyte()
 *
 * Read 1 byte in NIBBLE mode
 */
int
nibble_1284_inbyte(struct ppb_device *dev, char *buffer)
{
	char nibble[2];
	int i, error;

	for (i = 0; i < 2; i++) {
		/* ready to take data (nAUTO low) */
		ppb_wctr(dev, AUTOFEED & ~(STROBE | SELECTIN));

		if ((error = do_1284_wait(dev, nACK, 0)))
			return (error);

		/* read nibble */
		nibble[i] = ppb_rstr(dev);

		/* ack, not ready for another nibble */
		ppb_wctr(dev, 0 & ~(AUTOFEED | STROBE | SELECTIN));

		/* wait ack from peripherial */
		if ((error = do_1284_wait(dev, nACK, nACK)))
			return (error);
	}

	*buffer = ((nibble2char(nibble[1]) << 4) & 0xf0) |
				(nibble2char(nibble[0]) & 0x0f);

	return (0);
}

/*
 * nibble_1284_sync()
 */
void
nibble_1284_sync(struct ppb_device *dev)
{
	char ctr;

	ctr = ppb_rctr(dev);

	ppb_wctr(dev, (ctr & ~AUTOFEED) | SELECTIN);
	if (do_1284_wait(dev, nACK, 0))
		return;

	ppb_wctr(dev, ctr | AUTOFEED);
	do_1284_wait(dev, nACK, nACK);

	ppb_wctr(dev, (ctr & ~AUTOFEED) | SELECTIN);

	return;
}

/*
 * ppb_1284_negociate()
 *
 * Normal nibble mode or request device id mode (see ppb_1284.h)
 */
int
ppb_1284_negociate(struct ppb_device *dev, int mode)
{
	int error;
	int phase = 0;

	ppb_wctr(dev, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
	DELAY(1);

	ppb_wdtr(dev, mode);
	DELAY(1);

	ppb_wctr(dev, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));

	if ((error = do_1284_wait(dev, nACK | PERROR | SELECT | nFAULT,
			PERROR  | SELECT | nFAULT)))
		goto error;

	phase = 1;

	ppb_wctr(dev, (nINIT | STROBE | AUTOFEED) & ~SELECTIN);
	DELAY(5);

	ppb_wctr(dev, nINIT & ~(SELECTIN | AUTOFEED | STROBE));

#if 0	/* not respected by most devices */
	if ((error = do_1284_wait(dev, nACK, nACK)))
		goto error;

	if (mode == 0)
		if ((error = do_1284_wait(dev, SELECT, 0)))
			goto error;
	else
		if ((error = do_1284_wait(dev, SELECT, SELECT)))
			goto error;
#endif

	return (0);

error:
	if (bootverbose)
		printf("%s: status=0x%x %d\n", __FUNCTION__, ppb_rstr(dev), phase);

	return (error);
}

int
ppb_1284_terminate(struct ppb_device *dev, int how)
{
	int error;

	switch (how) {
	case VALID_STATE:

		ppb_wctr(dev, SELECTIN & ~(STROBE | AUTOFEED));

		if ((error = do_1284_wait(dev, nACK | nBUSY | nFAULT, nFAULT)))
			return (error);

		ppb_wctr(dev, (SELECTIN | AUTOFEED) & ~STROBE);

		if ((error = do_1284_wait(dev, nACK, nACK)))
			return (error);

		ppb_wctr(dev, SELECTIN & ~(STROBE | AUTOFEED));
		break;

	default:
		return (EINVAL);
	}

	return (0);
}
OpenPOWER on IntegriCloud