adb_direct.c revision 1.35 1 1.35 wiz /* $NetBSD: adb_direct.c,v 1.35 2006/11/24 22:04:23 wiz Exp $ */
2 1.1 tsubai
3 1.1 tsubai /* From: adb_direct.c 2.02 4/18/97 jpw */
4 1.1 tsubai
5 1.1 tsubai /*
6 1.1 tsubai * Copyright (C) 1996, 1997 John P. Wittkoski
7 1.1 tsubai * All rights reserved.
8 1.1 tsubai *
9 1.1 tsubai * Redistribution and use in source and binary forms, with or without
10 1.1 tsubai * modification, are permitted provided that the following conditions
11 1.1 tsubai * are met:
12 1.1 tsubai * 1. Redistributions of source code must retain the above copyright
13 1.1 tsubai * notice, this list of conditions and the following disclaimer.
14 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 tsubai * notice, this list of conditions and the following disclaimer in the
16 1.1 tsubai * documentation and/or other materials provided with the distribution.
17 1.1 tsubai * 3. All advertising materials mentioning features or use of this software
18 1.1 tsubai * must display the following acknowledgement:
19 1.1 tsubai * This product includes software developed by John P. Wittkoski.
20 1.1 tsubai * 4. The name of the author may not be used to endorse or promote products
21 1.1 tsubai * derived from this software without specific prior written permission.
22 1.1 tsubai *
23 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.1 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.1 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.1 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.1 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 1.1 tsubai * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 tsubai */
34 1.1 tsubai
35 1.1 tsubai /*
36 1.1 tsubai * This code is rather messy, but I don't have time right now
37 1.1 tsubai * to clean it up as much as I would like.
38 1.1 tsubai * But it works, so I'm happy. :-) jpw
39 1.1 tsubai */
40 1.1 tsubai
41 1.1 tsubai /*
42 1.1 tsubai * TO DO:
43 1.1 tsubai * - We could reduce the time spent in the adb_intr_* routines
44 1.1 tsubai * by having them save the incoming and outgoing data directly
45 1.1 tsubai * in the adbInbound and adbOutbound queues, as it would reduce
46 1.1 tsubai * the number of times we need to copy the data around. It
47 1.1 tsubai * would also make the code more readable and easier to follow.
48 1.1 tsubai * - (Related to above) Use the header part of adbCommand to
49 1.1 tsubai * reduce the number of copies we have to do of the data.
50 1.1 tsubai * - (Related to above) Actually implement the adbOutbound queue.
51 1.1 tsubai * This is fairly easy once you switch all the intr routines
52 1.1 tsubai * over to using adbCommand structs directly.
53 1.1 tsubai * - There is a bug in the state machine of adb_intr_cuda
54 1.1 tsubai * code that causes hangs, especially on 030 machines, probably
55 1.1 tsubai * because of some timing issues. Because I have been unable to
56 1.1 tsubai * determine the exact cause of this bug, I used the timeout function
57 1.1 tsubai * to check for and recover from this condition. If anyone finds
58 1.1 tsubai * the actual cause of this bug, the calls to timeout and the
59 1.1 tsubai * adb_cuda_tickle routine can be removed.
60 1.1 tsubai */
61 1.25 lukem
62 1.25 lukem #include <sys/cdefs.h>
63 1.35 wiz __KERNEL_RCSID(0, "$NetBSD: adb_direct.c,v 1.35 2006/11/24 22:04:23 wiz Exp $");
64 1.1 tsubai
65 1.1 tsubai #include <sys/param.h>
66 1.1 tsubai #include <sys/cdefs.h>
67 1.1 tsubai #include <sys/systm.h>
68 1.13 thorpej #include <sys/callout.h>
69 1.1 tsubai #include <sys/device.h>
70 1.1 tsubai
71 1.1 tsubai #include <machine/param.h>
72 1.1 tsubai #include <machine/cpu.h>
73 1.1 tsubai #include <machine/adbsys.h>
74 1.1 tsubai
75 1.1 tsubai #include <macppc/dev/viareg.h>
76 1.1 tsubai #include <macppc/dev/adbvar.h>
77 1.17 matt #include <macppc/dev/pm_direct.h>
78 1.1 tsubai
79 1.1 tsubai #define printf_intr printf
80 1.1 tsubai
81 1.6 tsubai #ifdef DEBUG
82 1.6 tsubai #ifndef ADB_DEBUG
83 1.6 tsubai #define ADB_DEBUG
84 1.6 tsubai #endif
85 1.6 tsubai #endif
86 1.6 tsubai
87 1.1 tsubai /* some misc. leftovers */
88 1.1 tsubai #define vPB 0x0000
89 1.1 tsubai #define vPB3 0x08
90 1.1 tsubai #define vPB4 0x10
91 1.1 tsubai #define vPB5 0x20
92 1.1 tsubai #define vSR_INT 0x04
93 1.1 tsubai #define vSR_OUT 0x10
94 1.1 tsubai
95 1.1 tsubai /* the type of ADB action that we are currently preforming */
96 1.6 tsubai #define ADB_ACTION_NOTREADY 0x1 /* has not been initialized yet */
97 1.6 tsubai #define ADB_ACTION_IDLE 0x2 /* the bus is currently idle */
98 1.6 tsubai #define ADB_ACTION_OUT 0x3 /* sending out a command */
99 1.6 tsubai #define ADB_ACTION_IN 0x4 /* receiving data */
100 1.6 tsubai #define ADB_ACTION_POLLING 0x5 /* polling - II only */
101 1.1 tsubai
102 1.1 tsubai /*
103 1.1 tsubai * These describe the state of the ADB bus itself, although they
104 1.1 tsubai * don't necessarily correspond directly to ADB states.
105 1.1 tsubai * Note: these are not really used in the IIsi code.
106 1.1 tsubai */
107 1.6 tsubai #define ADB_BUS_UNKNOWN 0x1 /* we don't know yet - all models */
108 1.6 tsubai #define ADB_BUS_IDLE 0x2 /* bus is idle - all models */
109 1.6 tsubai #define ADB_BUS_CMD 0x3 /* starting a command - II models */
110 1.6 tsubai #define ADB_BUS_ODD 0x4 /* the "odd" state - II models */
111 1.6 tsubai #define ADB_BUS_EVEN 0x5 /* the "even" state - II models */
112 1.6 tsubai #define ADB_BUS_ACTIVE 0x6 /* active state - IIsi models */
113 1.6 tsubai #define ADB_BUS_ACK 0x7 /* currently ACKing - IIsi models */
114 1.1 tsubai
115 1.1 tsubai /*
116 1.1 tsubai * Shortcuts for setting or testing the VIA bit states.
117 1.1 tsubai * Not all shortcuts are used for every type of ADB hardware.
118 1.1 tsubai */
119 1.1 tsubai #define ADB_SET_STATE_IDLE_CUDA() via_reg_or(VIA1, vBufB, (vPB4 | vPB5))
120 1.1 tsubai #define ADB_SET_STATE_TIP() via_reg_and(VIA1, vBufB, ~vPB5)
121 1.1 tsubai #define ADB_CLR_STATE_TIP() via_reg_or(VIA1, vBufB, vPB5)
122 1.1 tsubai #define ADB_TOGGLE_STATE_ACK_CUDA() via_reg_xor(VIA1, vBufB, vPB4)
123 1.1 tsubai #define ADB_SET_STATE_ACKOFF_CUDA() via_reg_or(VIA1, vBufB, vPB4)
124 1.1 tsubai #define ADB_SET_SR_INPUT() via_reg_and(VIA1, vACR, ~vSR_OUT)
125 1.1 tsubai #define ADB_SET_SR_OUTPUT() via_reg_or(VIA1, vACR, vSR_OUT)
126 1.1 tsubai #define ADB_SR() read_via_reg(VIA1, vSR)
127 1.1 tsubai #define ADB_VIA_INTR_ENABLE() write_via_reg(VIA1, vIER, 0x84)
128 1.1 tsubai #define ADB_VIA_INTR_DISABLE() write_via_reg(VIA1, vIER, 0x04)
129 1.1 tsubai #define ADB_INTR_IS_OFF (vPB3 == (read_via_reg(VIA1, vBufB) & vPB3))
130 1.1 tsubai #define ADB_INTR_IS_ON (0 == (read_via_reg(VIA1, vBufB) & vPB3))
131 1.1 tsubai #define ADB_SR_INTR_IS_OFF (0 == (read_via_reg(VIA1, vIFR) & vSR_INT))
132 1.1 tsubai #define ADB_SR_INTR_IS_ON (vSR_INT == (read_via_reg(VIA1, \
133 1.1 tsubai vIFR) & vSR_INT))
134 1.1 tsubai
135 1.1 tsubai /*
136 1.1 tsubai * This is the delay that is required (in uS) between certain
137 1.1 tsubai * ADB transactions. The actual timing delay for for each uS is
138 1.1 tsubai * calculated at boot time to account for differences in machine speed.
139 1.1 tsubai */
140 1.8 tsubai #define ADB_DELAY 150
141 1.1 tsubai
142 1.1 tsubai /*
143 1.1 tsubai * Maximum ADB message length; includes space for data, result, and
144 1.1 tsubai * device code - plus a little for safety.
145 1.1 tsubai */
146 1.1 tsubai #define ADB_MAX_MSG_LENGTH 16
147 1.1 tsubai #define ADB_MAX_HDR_LENGTH 8
148 1.1 tsubai
149 1.1 tsubai #define ADB_QUEUE 32
150 1.1 tsubai #define ADB_TICKLE_TICKS 4
151 1.1 tsubai
152 1.1 tsubai /*
153 1.1 tsubai * A structure for storing information about each ADB device.
154 1.1 tsubai */
155 1.1 tsubai struct ADBDevEntry {
156 1.1 tsubai void (*ServiceRtPtr) __P((void));
157 1.1 tsubai void *DataAreaAddr;
158 1.14 tsubai int devType;
159 1.14 tsubai int origAddr;
160 1.14 tsubai int currentAddr;
161 1.1 tsubai };
162 1.1 tsubai
163 1.1 tsubai /*
164 1.1 tsubai * Used to hold ADB commands that are waiting to be sent out.
165 1.1 tsubai */
166 1.1 tsubai struct adbCmdHoldEntry {
167 1.1 tsubai u_char outBuf[ADB_MAX_MSG_LENGTH]; /* our message */
168 1.1 tsubai u_char *saveBuf; /* buffer to know where to save result */
169 1.32 nathanw adbComp *compRout; /* completion routine pointer */
170 1.32 nathanw int *data; /* completion routine data pointer */
171 1.1 tsubai };
172 1.1 tsubai
173 1.1 tsubai /*
174 1.1 tsubai * Eventually used for two separate queues, the queue between
175 1.1 tsubai * the upper and lower halves, and the outgoing packet queue.
176 1.1 tsubai * TO DO: adbCommand can replace all of adbCmdHoldEntry eventually
177 1.1 tsubai */
178 1.1 tsubai struct adbCommand {
179 1.1 tsubai u_char header[ADB_MAX_HDR_LENGTH]; /* not used yet */
180 1.1 tsubai u_char data[ADB_MAX_MSG_LENGTH]; /* packet data only */
181 1.1 tsubai u_char *saveBuf; /* where to save result */
182 1.32 nathanw adbComp *compRout; /* completion routine pointer */
183 1.32 nathanw volatile int *compData; /* completion routine data pointer */
184 1.1 tsubai u_int cmd; /* the original command for this data */
185 1.1 tsubai u_int unsol; /* 1 if packet was unsolicited */
186 1.1 tsubai u_int ack_only; /* 1 for no special processing */
187 1.1 tsubai };
188 1.1 tsubai
189 1.1 tsubai /*
190 1.1 tsubai * A few variables that we need and their initial values.
191 1.1 tsubai */
192 1.1 tsubai int adbHardware = ADB_HW_UNKNOWN;
193 1.1 tsubai int adbActionState = ADB_ACTION_NOTREADY;
194 1.1 tsubai int adbWaiting = 0; /* waiting for return data from the device */
195 1.1 tsubai int adbWriteDelay = 0; /* working on (or waiting to do) a write */
196 1.1 tsubai
197 1.1 tsubai int adbWaitingCmd = 0; /* ADB command we are waiting for */
198 1.1 tsubai u_char *adbBuffer = (long)0; /* pointer to user data area */
199 1.32 nathanw adbComp *adbCompRout = NULL; /* pointer to the completion routine */
200 1.32 nathanw volatile int *adbCompData = NULL; /* pointer to the completion routine data */
201 1.1 tsubai int adbStarting = 1; /* doing ADBReInit so do polling differently */
202 1.1 tsubai
203 1.1 tsubai u_char adbInputBuffer[ADB_MAX_MSG_LENGTH]; /* data input buffer */
204 1.1 tsubai u_char adbOutputBuffer[ADB_MAX_MSG_LENGTH]; /* data output buffer */
205 1.1 tsubai
206 1.1 tsubai int adbSentChars = 0; /* how many characters we have sent */
207 1.1 tsubai
208 1.1 tsubai struct ADBDevEntry ADBDevTable[16]; /* our ADB device table */
209 1.1 tsubai int ADBNumDevices; /* num. of ADB devices found with ADBReInit */
210 1.1 tsubai
211 1.1 tsubai struct adbCommand adbInbound[ADB_QUEUE]; /* incoming queue */
212 1.1 tsubai int adbInCount = 0; /* how many packets in in queue */
213 1.1 tsubai int adbInHead = 0; /* head of in queue */
214 1.1 tsubai int adbInTail = 0; /* tail of in queue */
215 1.1 tsubai struct adbCommand adbOutbound[ADB_QUEUE]; /* outgoing queue - not used yet */
216 1.1 tsubai int adbOutCount = 0; /* how many packets in out queue */
217 1.1 tsubai int adbOutHead = 0; /* head of out queue */
218 1.1 tsubai int adbOutTail = 0; /* tail of out queue */
219 1.1 tsubai
220 1.1 tsubai int tickle_count = 0; /* how many tickles seen for this packet? */
221 1.1 tsubai int tickle_serial = 0; /* the last packet tickled */
222 1.1 tsubai int adb_cuda_serial = 0; /* the current packet */
223 1.1 tsubai
224 1.13 thorpej struct callout adb_cuda_tickle_ch = CALLOUT_INITIALIZER;
225 1.13 thorpej struct callout adb_soft_intr_ch = CALLOUT_INITIALIZER;
226 1.13 thorpej
227 1.3 tsubai volatile u_char *Via1Base;
228 1.6 tsubai extern int adb_polling; /* Are we polling? */
229 1.1 tsubai
230 1.1 tsubai void pm_setup_adb __P((void));
231 1.1 tsubai void pm_check_adb_devices __P((int));
232 1.32 nathanw int pm_adb_op __P((u_char *, void *, volatile void *, int));
233 1.1 tsubai void pm_init_adb_device __P((void));
234 1.1 tsubai
235 1.1 tsubai /*
236 1.1 tsubai * The following are private routines.
237 1.1 tsubai */
238 1.6 tsubai #ifdef ADB_DEBUG
239 1.1 tsubai void print_single __P((u_char *));
240 1.6 tsubai #endif
241 1.1 tsubai void adb_soft_intr __P((void));
242 1.32 nathanw int send_adb_cuda __P((u_char *, u_char *, adbComp *, volatile void *, int));
243 1.1 tsubai void adb_intr_cuda_test __P((void));
244 1.1 tsubai void adb_cuda_tickle __P((void));
245 1.1 tsubai void adb_pass_up __P((struct adbCommand *));
246 1.32 nathanw void adb_op_comprout __P((caddr_t, volatile int *, int));
247 1.1 tsubai void adb_reinit __P((void));
248 1.1 tsubai int count_adbs __P((void));
249 1.1 tsubai int get_ind_adb_info __P((ADBDataBlock *, int));
250 1.1 tsubai int get_adb_info __P((ADBDataBlock *, int));
251 1.1 tsubai int set_adb_info __P((ADBSetInfoBlock *, int));
252 1.1 tsubai void adb_setup_hw_type __P((void));
253 1.32 nathanw int adb_op (Ptr, adbComp *, volatile void *, short);
254 1.32 nathanw int adb_op_sync __P((Ptr, adbComp *, Ptr, short));
255 1.1 tsubai void adb_hw_setup __P((void));
256 1.1 tsubai int adb_cmd_result __P((u_char *));
257 1.1 tsubai int adb_cmd_extra __P((u_char *));
258 1.1 tsubai /* we should create this and it will be the public version */
259 1.1 tsubai int send_adb __P((u_char *, void *, void *));
260 1.1 tsubai
261 1.17 matt int setsoftadb __P((void));
262 1.17 matt
263 1.6 tsubai #ifdef ADB_DEBUG
264 1.1 tsubai /*
265 1.1 tsubai * print_single
266 1.1 tsubai * Diagnostic display routine. Displays the hex values of the
267 1.1 tsubai * specified elements of the u_char. The length of the "string"
268 1.1 tsubai * is in [0].
269 1.1 tsubai */
270 1.1 tsubai void
271 1.14 tsubai print_single(str)
272 1.14 tsubai u_char *str;
273 1.1 tsubai {
274 1.1 tsubai int x;
275 1.1 tsubai
276 1.14 tsubai if (str == 0) {
277 1.14 tsubai printf_intr("no data - null pointer\n");
278 1.1 tsubai return;
279 1.1 tsubai }
280 1.14 tsubai if (*str == 0) {
281 1.14 tsubai printf_intr("nothing returned\n");
282 1.1 tsubai return;
283 1.1 tsubai }
284 1.14 tsubai if (*str > 20) {
285 1.1 tsubai printf_intr("ADB: ACK > 20 no way!\n");
286 1.14 tsubai *str = 20;
287 1.1 tsubai }
288 1.14 tsubai printf_intr("(length=0x%x):", *str);
289 1.14 tsubai for (x = 1; x <= *str; x++)
290 1.14 tsubai printf_intr(" 0x%02x", str[x]);
291 1.1 tsubai printf_intr("\n");
292 1.1 tsubai }
293 1.6 tsubai #endif
294 1.1 tsubai
295 1.1 tsubai void
296 1.1 tsubai adb_cuda_tickle(void)
297 1.1 tsubai {
298 1.1 tsubai volatile int s;
299 1.1 tsubai
300 1.1 tsubai if (adbActionState == ADB_ACTION_IN) {
301 1.1 tsubai if (tickle_serial == adb_cuda_serial) {
302 1.1 tsubai if (++tickle_count > 0) {
303 1.1 tsubai s = splhigh();
304 1.1 tsubai adbActionState = ADB_ACTION_IDLE;
305 1.1 tsubai adbInputBuffer[0] = 0;
306 1.1 tsubai ADB_SET_STATE_IDLE_CUDA();
307 1.1 tsubai splx(s);
308 1.1 tsubai }
309 1.1 tsubai } else {
310 1.1 tsubai tickle_serial = adb_cuda_serial;
311 1.1 tsubai tickle_count = 0;
312 1.1 tsubai }
313 1.1 tsubai } else {
314 1.1 tsubai tickle_serial = adb_cuda_serial;
315 1.1 tsubai tickle_count = 0;
316 1.1 tsubai }
317 1.1 tsubai
318 1.13 thorpej callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
319 1.13 thorpej (void *)adb_cuda_tickle, NULL);
320 1.1 tsubai }
321 1.1 tsubai
322 1.1 tsubai /*
323 1.1 tsubai * called when when an adb interrupt happens
324 1.1 tsubai *
325 1.1 tsubai * Cuda version of adb_intr
326 1.6 tsubai * TO DO: do we want to add some calls to intr_dispatch() here to
327 1.6 tsubai * grab serial interrupts?
328 1.1 tsubai */
329 1.30 briggs int
330 1.30 briggs adb_intr_cuda(void *arg)
331 1.1 tsubai {
332 1.1 tsubai volatile int i, ending;
333 1.1 tsubai volatile unsigned int s;
334 1.1 tsubai struct adbCommand packet;
335 1.29 briggs uint8_t reg;
336 1.1 tsubai
337 1.1 tsubai s = splhigh(); /* can't be too careful - might be called */
338 1.29 briggs /* from a routine, NOT an interrupt */
339 1.29 briggs
340 1.29 briggs reg = read_via_reg(VIA1, vIFR); /* Read the interrupts */
341 1.29 briggs if ((reg & 0x80) == 0) {
342 1.29 briggs splx(s);
343 1.30 briggs return 0; /* No interrupts to process */
344 1.29 briggs }
345 1.29 briggs
346 1.29 briggs write_via_reg(VIA1, vIFR, reg & 0x7f); /* Clear 'em */
347 1.1 tsubai
348 1.1 tsubai ADB_VIA_INTR_DISABLE(); /* disable ADB interrupt on IIs. */
349 1.1 tsubai
350 1.1 tsubai switch_start:
351 1.1 tsubai switch (adbActionState) {
352 1.1 tsubai case ADB_ACTION_IDLE:
353 1.1 tsubai /*
354 1.1 tsubai * This is an unexpected packet, so grab the first (dummy)
355 1.1 tsubai * byte, set up the proper vars, and tell the chip we are
356 1.1 tsubai * starting to receive the packet by setting the TIP bit.
357 1.1 tsubai */
358 1.1 tsubai adbInputBuffer[1] = ADB_SR();
359 1.1 tsubai adb_cuda_serial++;
360 1.1 tsubai if (ADB_INTR_IS_OFF) /* must have been a fake start */
361 1.1 tsubai break;
362 1.1 tsubai
363 1.1 tsubai ADB_SET_SR_INPUT();
364 1.1 tsubai ADB_SET_STATE_TIP();
365 1.1 tsubai
366 1.1 tsubai adbInputBuffer[0] = 1;
367 1.1 tsubai adbActionState = ADB_ACTION_IN;
368 1.1 tsubai #ifdef ADB_DEBUG
369 1.1 tsubai if (adb_debug)
370 1.1 tsubai printf_intr("idle 0x%02x ", adbInputBuffer[1]);
371 1.1 tsubai #endif
372 1.1 tsubai break;
373 1.1 tsubai
374 1.1 tsubai case ADB_ACTION_IN:
375 1.1 tsubai adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();
376 1.1 tsubai /* intr off means this is the last byte (end of frame) */
377 1.1 tsubai if (ADB_INTR_IS_OFF)
378 1.1 tsubai ending = 1;
379 1.1 tsubai else
380 1.1 tsubai ending = 0;
381 1.1 tsubai
382 1.1 tsubai if (1 == ending) { /* end of message? */
383 1.1 tsubai #ifdef ADB_DEBUG
384 1.1 tsubai if (adb_debug) {
385 1.1 tsubai printf_intr("in end 0x%02x ",
386 1.1 tsubai adbInputBuffer[adbInputBuffer[0]]);
387 1.1 tsubai print_single(adbInputBuffer);
388 1.1 tsubai }
389 1.1 tsubai #endif
390 1.1 tsubai
391 1.1 tsubai /*
392 1.1 tsubai * Are we waiting AND does this packet match what we
393 1.1 tsubai * are waiting for AND is it coming from either the
394 1.1 tsubai * ADB or RTC/PRAM sub-device? This section _should_
395 1.1 tsubai * recognize all ADB and RTC/PRAM type commands, but
396 1.1 tsubai * there may be more... NOTE: commands are always at
397 1.1 tsubai * [4], even for RTC/PRAM commands.
398 1.1 tsubai */
399 1.1 tsubai /* set up data for adb_pass_up */
400 1.14 tsubai memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
401 1.1 tsubai
402 1.1 tsubai if ((adbWaiting == 1) &&
403 1.1 tsubai (adbInputBuffer[4] == adbWaitingCmd) &&
404 1.1 tsubai ((adbInputBuffer[2] == 0x00) ||
405 1.1 tsubai (adbInputBuffer[2] == 0x01))) {
406 1.1 tsubai packet.saveBuf = adbBuffer;
407 1.1 tsubai packet.compRout = adbCompRout;
408 1.1 tsubai packet.compData = adbCompData;
409 1.1 tsubai packet.unsol = 0;
410 1.1 tsubai packet.ack_only = 0;
411 1.1 tsubai adb_pass_up(&packet);
412 1.1 tsubai
413 1.1 tsubai adbWaitingCmd = 0; /* reset "waiting" vars */
414 1.1 tsubai adbWaiting = 0;
415 1.1 tsubai adbBuffer = (long)0;
416 1.1 tsubai adbCompRout = (long)0;
417 1.1 tsubai adbCompData = (long)0;
418 1.1 tsubai } else {
419 1.1 tsubai packet.unsol = 1;
420 1.1 tsubai packet.ack_only = 0;
421 1.1 tsubai adb_pass_up(&packet);
422 1.1 tsubai }
423 1.1 tsubai
424 1.1 tsubai
425 1.1 tsubai /* reset vars and signal the end of this frame */
426 1.1 tsubai adbActionState = ADB_ACTION_IDLE;
427 1.1 tsubai adbInputBuffer[0] = 0;
428 1.1 tsubai ADB_SET_STATE_IDLE_CUDA();
429 1.1 tsubai /*ADB_SET_SR_INPUT();*/
430 1.1 tsubai
431 1.1 tsubai /*
432 1.1 tsubai * If there is something waiting to be sent out,
433 1.1 tsubai * the set everything up and send the first byte.
434 1.1 tsubai */
435 1.1 tsubai if (adbWriteDelay == 1) {
436 1.1 tsubai delay(ADB_DELAY); /* required */
437 1.1 tsubai adbSentChars = 0;
438 1.1 tsubai adbActionState = ADB_ACTION_OUT;
439 1.1 tsubai /*
440 1.1 tsubai * If the interrupt is on, we were too slow
441 1.1 tsubai * and the chip has already started to send
442 1.1 tsubai * something to us, so back out of the write
443 1.1 tsubai * and start a read cycle.
444 1.1 tsubai */
445 1.1 tsubai if (ADB_INTR_IS_ON) {
446 1.1 tsubai ADB_SET_SR_INPUT();
447 1.1 tsubai ADB_SET_STATE_IDLE_CUDA();
448 1.1 tsubai adbSentChars = 0;
449 1.1 tsubai adbActionState = ADB_ACTION_IDLE;
450 1.1 tsubai adbInputBuffer[0] = 0;
451 1.1 tsubai break;
452 1.1 tsubai }
453 1.1 tsubai /*
454 1.1 tsubai * If we got here, it's ok to start sending
455 1.1 tsubai * so load the first byte and tell the chip
456 1.1 tsubai * we want to send.
457 1.1 tsubai */
458 1.1 tsubai ADB_SET_STATE_TIP();
459 1.1 tsubai ADB_SET_SR_OUTPUT();
460 1.1 tsubai write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);
461 1.1 tsubai }
462 1.1 tsubai } else {
463 1.1 tsubai ADB_TOGGLE_STATE_ACK_CUDA();
464 1.1 tsubai #ifdef ADB_DEBUG
465 1.1 tsubai if (adb_debug)
466 1.1 tsubai printf_intr("in 0x%02x ",
467 1.1 tsubai adbInputBuffer[adbInputBuffer[0]]);
468 1.1 tsubai #endif
469 1.1 tsubai }
470 1.1 tsubai break;
471 1.1 tsubai
472 1.1 tsubai case ADB_ACTION_OUT:
473 1.1 tsubai i = ADB_SR(); /* reset SR-intr in IFR */
474 1.1 tsubai #ifdef ADB_DEBUG
475 1.1 tsubai if (adb_debug)
476 1.1 tsubai printf_intr("intr out 0x%02x ", i);
477 1.1 tsubai #endif
478 1.1 tsubai
479 1.1 tsubai adbSentChars++;
480 1.1 tsubai if (ADB_INTR_IS_ON) { /* ADB intr low during write */
481 1.1 tsubai #ifdef ADB_DEBUG
482 1.1 tsubai if (adb_debug)
483 1.1 tsubai printf_intr("intr was on ");
484 1.1 tsubai #endif
485 1.1 tsubai ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
486 1.1 tsubai ADB_SET_STATE_IDLE_CUDA();
487 1.1 tsubai adbSentChars = 0; /* must start all over */
488 1.1 tsubai adbActionState = ADB_ACTION_IDLE; /* new state */
489 1.1 tsubai adbInputBuffer[0] = 0;
490 1.1 tsubai adbWriteDelay = 1; /* must retry when done with
491 1.1 tsubai * read */
492 1.1 tsubai delay(ADB_DELAY);
493 1.1 tsubai goto switch_start; /* process next state right
494 1.1 tsubai * now */
495 1.1 tsubai break;
496 1.1 tsubai }
497 1.1 tsubai if (adbOutputBuffer[0] == adbSentChars) { /* check for done */
498 1.1 tsubai if (0 == adb_cmd_result(adbOutputBuffer)) { /* do we expect data
499 1.1 tsubai * back? */
500 1.1 tsubai adbWaiting = 1; /* signal waiting for return */
501 1.1 tsubai adbWaitingCmd = adbOutputBuffer[2]; /* save waiting command */
502 1.1 tsubai } else { /* no talk, so done */
503 1.1 tsubai /* set up stuff for adb_pass_up */
504 1.14 tsubai memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
505 1.1 tsubai packet.saveBuf = adbBuffer;
506 1.1 tsubai packet.compRout = adbCompRout;
507 1.1 tsubai packet.compData = adbCompData;
508 1.1 tsubai packet.cmd = adbWaitingCmd;
509 1.1 tsubai packet.unsol = 0;
510 1.1 tsubai packet.ack_only = 1;
511 1.1 tsubai adb_pass_up(&packet);
512 1.1 tsubai
513 1.1 tsubai /* reset "waiting" vars, just in case */
514 1.1 tsubai adbWaitingCmd = 0;
515 1.1 tsubai adbBuffer = (long)0;
516 1.32 nathanw adbCompRout = NULL;
517 1.32 nathanw adbCompData = NULL;
518 1.1 tsubai }
519 1.1 tsubai
520 1.1 tsubai adbWriteDelay = 0; /* done writing */
521 1.1 tsubai adbActionState = ADB_ACTION_IDLE; /* signal bus is idle */
522 1.1 tsubai ADB_SET_SR_INPUT();
523 1.1 tsubai ADB_SET_STATE_IDLE_CUDA();
524 1.1 tsubai #ifdef ADB_DEBUG
525 1.1 tsubai if (adb_debug)
526 1.1 tsubai printf_intr("write done ");
527 1.1 tsubai #endif
528 1.1 tsubai } else {
529 1.1 tsubai write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]); /* send next byte */
530 1.1 tsubai ADB_TOGGLE_STATE_ACK_CUDA(); /* signal byte ready to
531 1.1 tsubai * shift */
532 1.1 tsubai #ifdef ADB_DEBUG
533 1.1 tsubai if (adb_debug)
534 1.1 tsubai printf_intr("toggle ");
535 1.1 tsubai #endif
536 1.1 tsubai }
537 1.1 tsubai break;
538 1.1 tsubai
539 1.1 tsubai case ADB_ACTION_NOTREADY:
540 1.6 tsubai #ifdef ADB_DEBUG
541 1.6 tsubai if (adb_debug)
542 1.6 tsubai printf_intr("adb: not yet initialized\n");
543 1.6 tsubai #endif
544 1.1 tsubai break;
545 1.1 tsubai
546 1.1 tsubai default:
547 1.6 tsubai #ifdef ADB_DEBUG
548 1.6 tsubai if (adb_debug)
549 1.6 tsubai printf_intr("intr: unknown ADB state\n");
550 1.6 tsubai #endif
551 1.19 tsubai break;
552 1.1 tsubai }
553 1.1 tsubai
554 1.1 tsubai ADB_VIA_INTR_ENABLE(); /* enable ADB interrupt on IIs. */
555 1.1 tsubai
556 1.1 tsubai splx(s); /* restore */
557 1.1 tsubai
558 1.30 briggs return 1;
559 1.1 tsubai } /* end adb_intr_cuda */
560 1.1 tsubai
561 1.1 tsubai
562 1.1 tsubai int
563 1.32 nathanw send_adb_cuda(u_char * in, u_char * buffer, adbComp *compRout,
564 1.32 nathanw volatile void *data, int command)
565 1.1 tsubai {
566 1.14 tsubai int s, len;
567 1.1 tsubai
568 1.1 tsubai #ifdef ADB_DEBUG
569 1.1 tsubai if (adb_debug)
570 1.1 tsubai printf_intr("SEND\n");
571 1.1 tsubai #endif
572 1.1 tsubai
573 1.1 tsubai if (adbActionState == ADB_ACTION_NOTREADY)
574 1.1 tsubai return 1;
575 1.1 tsubai
576 1.1 tsubai /* Don't interrupt while we are messing with the ADB */
577 1.1 tsubai s = splhigh();
578 1.1 tsubai
579 1.1 tsubai if ((adbActionState == ADB_ACTION_IDLE) && /* ADB available? */
580 1.1 tsubai (ADB_INTR_IS_OFF)) { /* and no incoming interrupt? */
581 1.1 tsubai } else
582 1.1 tsubai if (adbWriteDelay == 0) /* it's busy, but is anything waiting? */
583 1.1 tsubai adbWriteDelay = 1; /* if no, then we'll "queue"
584 1.1 tsubai * it up */
585 1.1 tsubai else {
586 1.1 tsubai splx(s);
587 1.1 tsubai return 1; /* really busy! */
588 1.1 tsubai }
589 1.1 tsubai
590 1.1 tsubai #ifdef ADB_DEBUG
591 1.1 tsubai if (adb_debug)
592 1.1 tsubai printf_intr("QUEUE\n");
593 1.1 tsubai #endif
594 1.1 tsubai if ((long)in == (long)0) { /* need to convert? */
595 1.1 tsubai /*
596 1.1 tsubai * Don't need to use adb_cmd_extra here because this section
597 1.1 tsubai * will be called ONLY when it is an ADB command (no RTC or
598 1.1 tsubai * PRAM)
599 1.1 tsubai */
600 1.1 tsubai if ((command & 0x0c) == 0x08) /* copy addl data ONLY if
601 1.1 tsubai * doing a listen! */
602 1.1 tsubai len = buffer[0]; /* length of additional data */
603 1.1 tsubai else
604 1.1 tsubai len = 0;/* no additional data */
605 1.1 tsubai
606 1.1 tsubai adbOutputBuffer[0] = 2 + len; /* dev. type + command + addl.
607 1.1 tsubai * data */
608 1.1 tsubai adbOutputBuffer[1] = 0x00; /* mark as an ADB command */
609 1.1 tsubai adbOutputBuffer[2] = (u_char)command; /* load command */
610 1.1 tsubai
611 1.14 tsubai /* copy additional output data, if any */
612 1.14 tsubai memcpy(adbOutputBuffer + 3, buffer + 1, len);
613 1.1 tsubai } else
614 1.14 tsubai /* if data ready, just copy over */
615 1.14 tsubai memcpy(adbOutputBuffer, in, in[0] + 2);
616 1.1 tsubai
617 1.1 tsubai adbSentChars = 0; /* nothing sent yet */
618 1.1 tsubai adbBuffer = buffer; /* save buffer to know where to save result */
619 1.1 tsubai adbCompRout = compRout; /* save completion routine pointer */
620 1.1 tsubai adbCompData = data; /* save completion routine data pointer */
621 1.1 tsubai adbWaitingCmd = adbOutputBuffer[2]; /* save wait command */
622 1.1 tsubai
623 1.1 tsubai if (adbWriteDelay != 1) { /* start command now? */
624 1.1 tsubai #ifdef ADB_DEBUG
625 1.1 tsubai if (adb_debug)
626 1.1 tsubai printf_intr("out start NOW");
627 1.1 tsubai #endif
628 1.1 tsubai delay(ADB_DELAY);
629 1.1 tsubai adbActionState = ADB_ACTION_OUT; /* set next state */
630 1.1 tsubai ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
631 1.1 tsubai write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]); /* load byte for output */
632 1.1 tsubai ADB_SET_STATE_ACKOFF_CUDA();
633 1.1 tsubai ADB_SET_STATE_TIP(); /* tell ADB that we want to send */
634 1.1 tsubai }
635 1.1 tsubai adbWriteDelay = 1; /* something in the write "queue" */
636 1.1 tsubai
637 1.1 tsubai splx(s);
638 1.1 tsubai
639 1.1 tsubai if ((s & (1 << 18)) || adb_polling) /* XXX were VIA1 interrupts blocked ? */
640 1.1 tsubai /* poll until byte done */
641 1.1 tsubai while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
642 1.1 tsubai || (adbWaiting == 1))
643 1.1 tsubai if (ADB_SR_INTR_IS_ON) { /* wait for "interrupt" */
644 1.30 briggs adb_intr_cuda(NULL); /* process it */
645 1.1 tsubai adb_soft_intr();
646 1.1 tsubai }
647 1.1 tsubai
648 1.1 tsubai return 0;
649 1.1 tsubai } /* send_adb_cuda */
650 1.1 tsubai
651 1.30 briggs int
652 1.30 briggs adb_intr(void *arg)
653 1.1 tsubai {
654 1.1 tsubai switch (adbHardware) {
655 1.28 briggs case ADB_HW_PMU:
656 1.30 briggs return pm_intr(arg);
657 1.1 tsubai break;
658 1.1 tsubai
659 1.1 tsubai case ADB_HW_CUDA:
660 1.30 briggs return adb_intr_cuda(arg);
661 1.1 tsubai break;
662 1.1 tsubai
663 1.1 tsubai case ADB_HW_UNKNOWN:
664 1.1 tsubai break;
665 1.1 tsubai }
666 1.30 briggs return 0;
667 1.1 tsubai }
668 1.1 tsubai
669 1.1 tsubai
670 1.1 tsubai /*
671 1.1 tsubai * adb_pass_up is called by the interrupt-time routines.
672 1.1 tsubai * It takes the raw packet data that was received from the
673 1.1 tsubai * device and puts it into the queue that the upper half
674 1.1 tsubai * processes. It then signals for a soft ADB interrupt which
675 1.1 tsubai * will eventually call the upper half routine (adb_soft_intr).
676 1.1 tsubai *
677 1.1 tsubai * If in->unsol is 0, then this is either the notification
678 1.1 tsubai * that the packet was sent (on a LISTEN, for example), or the
679 1.1 tsubai * response from the device (on a TALK). The completion routine
680 1.1 tsubai * is called only if the user specified one.
681 1.1 tsubai *
682 1.1 tsubai * If in->unsol is 1, then this packet was unsolicited and
683 1.1 tsubai * so we look up the device in the ADB device table to determine
684 1.1 tsubai * what it's default service routine is.
685 1.1 tsubai *
686 1.1 tsubai * If in->ack_only is 1, then we really only need to call
687 1.1 tsubai * the completion routine, so don't do any other stuff.
688 1.1 tsubai *
689 1.1 tsubai * Note that in->data contains the packet header AND data,
690 1.1 tsubai * while adbInbound[]->data contains ONLY data.
691 1.1 tsubai *
692 1.1 tsubai * Note: Called only at interrupt time. Assumes this.
693 1.1 tsubai */
694 1.1 tsubai void
695 1.1 tsubai adb_pass_up(struct adbCommand *in)
696 1.1 tsubai {
697 1.14 tsubai int start = 0, len = 0, cmd = 0;
698 1.1 tsubai ADBDataBlock block;
699 1.1 tsubai
700 1.1 tsubai /* temp for testing */
701 1.1 tsubai /*u_char *buffer = 0;*/
702 1.1 tsubai /*u_char *compdata = 0;*/
703 1.1 tsubai /*u_char *comprout = 0;*/
704 1.1 tsubai
705 1.1 tsubai if (adbInCount >= ADB_QUEUE) {
706 1.6 tsubai #ifdef ADB_DEBUG
707 1.6 tsubai if (adb_debug)
708 1.6 tsubai printf_intr("adb: ring buffer overflow\n");
709 1.6 tsubai #endif
710 1.1 tsubai return;
711 1.1 tsubai }
712 1.1 tsubai
713 1.1 tsubai if (in->ack_only) {
714 1.1 tsubai len = in->data[0];
715 1.1 tsubai cmd = in->cmd;
716 1.1 tsubai start = 0;
717 1.1 tsubai } else {
718 1.1 tsubai switch (adbHardware) {
719 1.1 tsubai case ADB_HW_CUDA:
720 1.1 tsubai /* If it's unsolicited, accept only ADB data for now */
721 1.1 tsubai if (in->unsol)
722 1.1 tsubai if (0 != in->data[2])
723 1.1 tsubai return;
724 1.1 tsubai cmd = in->data[4];
725 1.1 tsubai if (in->data[0] < 5)
726 1.1 tsubai len = 0;
727 1.1 tsubai else
728 1.1 tsubai len = in->data[0]-4;
729 1.1 tsubai start = 4;
730 1.1 tsubai break;
731 1.1 tsubai
732 1.28 briggs case ADB_HW_PMU:
733 1.4 tsubai cmd = in->data[1];
734 1.4 tsubai if (in->data[0] < 2)
735 1.4 tsubai len = 0;
736 1.4 tsubai else
737 1.4 tsubai len = in->data[0]-1;
738 1.4 tsubai start = 1;
739 1.4 tsubai break;
740 1.1 tsubai
741 1.1 tsubai case ADB_HW_UNKNOWN:
742 1.1 tsubai return;
743 1.1 tsubai }
744 1.1 tsubai
745 1.1 tsubai /* Make sure there is a valid device entry for this device */
746 1.1 tsubai if (in->unsol) {
747 1.1 tsubai /* ignore unsolicited data during adbreinit */
748 1.1 tsubai if (adbStarting)
749 1.1 tsubai return;
750 1.1 tsubai /* get device's comp. routine and data area */
751 1.14 tsubai if (-1 == get_adb_info(&block, ADB_CMDADDR(cmd)))
752 1.1 tsubai return;
753 1.1 tsubai }
754 1.1 tsubai }
755 1.1 tsubai
756 1.1 tsubai /*
757 1.1 tsubai * If this is an unsolicited packet, we need to fill in
758 1.1 tsubai * some info so adb_soft_intr can process this packet
759 1.1 tsubai * properly. If it's not unsolicited, then use what
760 1.1 tsubai * the caller sent us.
761 1.1 tsubai */
762 1.1 tsubai if (in->unsol) {
763 1.1 tsubai adbInbound[adbInTail].compRout = (void *)block.dbServiceRtPtr;
764 1.1 tsubai adbInbound[adbInTail].compData = (void *)block.dbDataAreaAddr;
765 1.1 tsubai adbInbound[adbInTail].saveBuf = (void *)adbInbound[adbInTail].data;
766 1.1 tsubai } else {
767 1.32 nathanw adbInbound[adbInTail].compRout = in->compRout;
768 1.32 nathanw adbInbound[adbInTail].compData = in->compData;
769 1.32 nathanw adbInbound[adbInTail].saveBuf = in->saveBuf;
770 1.1 tsubai }
771 1.1 tsubai
772 1.1 tsubai #ifdef ADB_DEBUG
773 1.1 tsubai if (adb_debug && in->data[1] == 2)
774 1.1 tsubai printf_intr("adb: caught error\n");
775 1.1 tsubai #endif
776 1.1 tsubai
777 1.1 tsubai /* copy the packet data over */
778 1.1 tsubai /*
779 1.1 tsubai * TO DO: If the *_intr routines fed their incoming data
780 1.1 tsubai * directly into an adbCommand struct, which is passed to
781 1.1 tsubai * this routine, then we could eliminate this copy.
782 1.1 tsubai */
783 1.14 tsubai memcpy(adbInbound[adbInTail].data + 1, in->data + start + 1, len);
784 1.1 tsubai adbInbound[adbInTail].data[0] = len;
785 1.1 tsubai adbInbound[adbInTail].cmd = cmd;
786 1.1 tsubai
787 1.1 tsubai adbInCount++;
788 1.1 tsubai if (++adbInTail >= ADB_QUEUE)
789 1.1 tsubai adbInTail = 0;
790 1.1 tsubai
791 1.1 tsubai /*
792 1.1 tsubai * If the debugger is running, call upper half manually.
793 1.1 tsubai * Otherwise, trigger a soft interrupt to handle the rest later.
794 1.1 tsubai */
795 1.1 tsubai if (adb_polling)
796 1.1 tsubai adb_soft_intr();
797 1.1 tsubai else
798 1.1 tsubai setsoftadb();
799 1.1 tsubai
800 1.1 tsubai return;
801 1.1 tsubai }
802 1.1 tsubai
803 1.1 tsubai
804 1.1 tsubai /*
805 1.1 tsubai * Called to process the packets after they have been
806 1.1 tsubai * placed in the incoming queue.
807 1.1 tsubai *
808 1.1 tsubai */
809 1.1 tsubai void
810 1.1 tsubai adb_soft_intr(void)
811 1.1 tsubai {
812 1.14 tsubai int s;
813 1.1 tsubai int cmd = 0;
814 1.1 tsubai u_char *buffer = 0;
815 1.32 nathanw adbComp *comprout = NULL;
816 1.32 nathanw volatile int *compdata = 0;
817 1.1 tsubai
818 1.1 tsubai #if 0
819 1.1 tsubai s = splhigh();
820 1.1 tsubai printf_intr("sr: %x\n", (s & 0x0700));
821 1.1 tsubai splx(s);
822 1.1 tsubai #endif
823 1.1 tsubai
824 1.1 tsubai /*delay(2*ADB_DELAY);*/
825 1.1 tsubai
826 1.1 tsubai while (adbInCount) {
827 1.1 tsubai #ifdef ADB_DEBUG
828 1.1 tsubai if (adb_debug & 0x80)
829 1.1 tsubai printf_intr("%x %x %x ",
830 1.1 tsubai adbInCount, adbInHead, adbInTail);
831 1.1 tsubai #endif
832 1.1 tsubai /* get the data we need from the queue */
833 1.1 tsubai buffer = adbInbound[adbInHead].saveBuf;
834 1.1 tsubai comprout = adbInbound[adbInHead].compRout;
835 1.1 tsubai compdata = adbInbound[adbInHead].compData;
836 1.1 tsubai cmd = adbInbound[adbInHead].cmd;
837 1.1 tsubai
838 1.1 tsubai /* copy over data to data area if it's valid */
839 1.1 tsubai /*
840 1.1 tsubai * Note that for unsol packets we don't want to copy the
841 1.1 tsubai * data anywhere, so buffer was already set to 0.
842 1.1 tsubai * For ack_only buffer was set to 0, so don't copy.
843 1.1 tsubai */
844 1.1 tsubai if (buffer)
845 1.14 tsubai memcpy(buffer, adbInbound[adbInHead].data,
846 1.14 tsubai adbInbound[adbInHead].data[0] + 1);
847 1.1 tsubai
848 1.1 tsubai #ifdef ADB_DEBUG
849 1.1 tsubai if (adb_debug & 0x80) {
850 1.1 tsubai printf_intr("%p %p %p %x ",
851 1.1 tsubai buffer, comprout, compdata, (short)cmd);
852 1.1 tsubai printf_intr("buf: ");
853 1.1 tsubai print_single(adbInbound[adbInHead].data);
854 1.1 tsubai }
855 1.1 tsubai #endif
856 1.21 dbj /* Remove the packet from the queue before calling
857 1.21 dbj * the completion routine, so that the completion
858 1.21 dbj * routine can reentrantly process the queue. For
859 1.21 dbj * example, this happens when polling is turned on
860 1.21 dbj * by entering the debuger by keystroke.
861 1.21 dbj */
862 1.21 dbj s = splhigh();
863 1.21 dbj adbInCount--;
864 1.21 dbj if (++adbInHead >= ADB_QUEUE)
865 1.21 dbj adbInHead = 0;
866 1.21 dbj splx(s);
867 1.1 tsubai
868 1.1 tsubai /* call default completion routine if it's valid */
869 1.32 nathanw if (comprout)
870 1.32 nathanw (*comprout)(buffer, compdata, cmd);
871 1.1 tsubai }
872 1.1 tsubai return;
873 1.1 tsubai }
874 1.1 tsubai
875 1.1 tsubai
876 1.1 tsubai /*
877 1.1 tsubai * This is my version of the ADBOp routine. It mainly just calls the
878 1.1 tsubai * hardware-specific routine.
879 1.1 tsubai *
880 1.1 tsubai * data : pointer to data area to be used by compRout
881 1.1 tsubai * compRout : completion routine
882 1.1 tsubai * buffer : for LISTEN: points to data to send - MAX 8 data bytes,
883 1.1 tsubai * byte 0 = # of bytes
884 1.1 tsubai * : for TALK: points to place to save return data
885 1.1 tsubai * command : the adb command to send
886 1.1 tsubai * result : 0 = success
887 1.1 tsubai * : -1 = could not complete
888 1.1 tsubai */
889 1.1 tsubai int
890 1.32 nathanw adb_op(Ptr buffer, adbComp *compRout, volatile void *data, short command)
891 1.1 tsubai {
892 1.1 tsubai int result;
893 1.1 tsubai
894 1.1 tsubai switch (adbHardware) {
895 1.28 briggs case ADB_HW_PMU:
896 1.32 nathanw result = pm_adb_op((u_char *)buffer, compRout,
897 1.32 nathanw data, (int)command);
898 1.1 tsubai
899 1.1 tsubai if (result == 0)
900 1.1 tsubai return 0;
901 1.1 tsubai else
902 1.1 tsubai return -1;
903 1.1 tsubai break;
904 1.1 tsubai
905 1.1 tsubai case ADB_HW_CUDA:
906 1.1 tsubai result = send_adb_cuda((u_char *)0, (u_char *)buffer,
907 1.32 nathanw compRout, data, (int)command);
908 1.1 tsubai if (result == 0)
909 1.1 tsubai return 0;
910 1.1 tsubai else
911 1.1 tsubai return -1;
912 1.1 tsubai break;
913 1.1 tsubai
914 1.1 tsubai case ADB_HW_UNKNOWN:
915 1.1 tsubai default:
916 1.1 tsubai return -1;
917 1.1 tsubai }
918 1.1 tsubai }
919 1.1 tsubai
920 1.1 tsubai
921 1.1 tsubai /*
922 1.1 tsubai * adb_hw_setup
923 1.1 tsubai * This routine sets up the possible machine specific hardware
924 1.1 tsubai * config (mainly VIA settings) for the various models.
925 1.1 tsubai */
926 1.1 tsubai void
927 1.1 tsubai adb_hw_setup(void)
928 1.1 tsubai {
929 1.1 tsubai volatile int i;
930 1.1 tsubai
931 1.1 tsubai switch (adbHardware) {
932 1.28 briggs case ADB_HW_PMU:
933 1.1 tsubai /*
934 1.1 tsubai * XXX - really PM_VIA_CLR_INTR - should we put it in
935 1.1 tsubai * pm_direct.h?
936 1.1 tsubai */
937 1.4 tsubai write_via_reg(VIA1, vIFR, 0x90); /* clear interrupt */
938 1.1 tsubai break;
939 1.1 tsubai
940 1.1 tsubai case ADB_HW_CUDA:
941 1.1 tsubai via_reg_or(VIA1, vDirB, 0x30); /* register B bits 4 and 5:
942 1.1 tsubai * outputs */
943 1.1 tsubai via_reg_and(VIA1, vDirB, 0xf7); /* register B bit 3: input */
944 1.1 tsubai via_reg_and(VIA1, vACR, ~vSR_OUT); /* make sure SR is set
945 1.1 tsubai * to IN */
946 1.1 tsubai write_via_reg(VIA1, vACR, (read_via_reg(VIA1, vACR) | 0x0c) & ~0x10);
947 1.1 tsubai adbActionState = ADB_ACTION_IDLE; /* used by all types of
948 1.1 tsubai * hardware */
949 1.1 tsubai write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
950 1.1 tsubai * are on */
951 1.1 tsubai ADB_SET_STATE_IDLE_CUDA(); /* set ADB bus state to idle */
952 1.1 tsubai
953 1.1 tsubai /* sort of a device reset */
954 1.1 tsubai i = ADB_SR(); /* clear interrupt */
955 1.1 tsubai ADB_VIA_INTR_DISABLE(); /* no interrupts while clearing */
956 1.1 tsubai ADB_SET_STATE_IDLE_CUDA(); /* reset state to idle */
957 1.1 tsubai delay(ADB_DELAY);
958 1.1 tsubai ADB_SET_STATE_TIP(); /* signal start of frame */
959 1.1 tsubai delay(ADB_DELAY);
960 1.1 tsubai ADB_TOGGLE_STATE_ACK_CUDA();
961 1.1 tsubai delay(ADB_DELAY);
962 1.1 tsubai ADB_CLR_STATE_TIP();
963 1.1 tsubai delay(ADB_DELAY);
964 1.1 tsubai ADB_SET_STATE_IDLE_CUDA(); /* back to idle state */
965 1.1 tsubai i = ADB_SR(); /* clear interrupt */
966 1.1 tsubai ADB_VIA_INTR_ENABLE(); /* ints ok now */
967 1.1 tsubai break;
968 1.1 tsubai
969 1.1 tsubai case ADB_HW_UNKNOWN:
970 1.1 tsubai default:
971 1.4 tsubai write_via_reg(VIA1, vIER, 0x04);/* turn interrupts off - TO
972 1.1 tsubai * DO: turn PB ints off? */
973 1.1 tsubai return;
974 1.1 tsubai break;
975 1.1 tsubai }
976 1.1 tsubai }
977 1.1 tsubai
978 1.1 tsubai /*
979 1.1 tsubai * adb_reinit sets up the adb stuff
980 1.1 tsubai *
981 1.1 tsubai */
982 1.1 tsubai void
983 1.1 tsubai adb_reinit(void)
984 1.1 tsubai {
985 1.1 tsubai u_char send_string[ADB_MAX_MSG_LENGTH];
986 1.14 tsubai ADBDataBlock data; /* temp. holder for getting device info */
987 1.1 tsubai volatile int i, x;
988 1.27 dyoung int s = 0; /* XXX: gcc */
989 1.1 tsubai int command;
990 1.1 tsubai int result;
991 1.1 tsubai int saveptr; /* point to next free relocation address */
992 1.1 tsubai int device;
993 1.1 tsubai int nonewtimes; /* times thru loop w/o any new devices */
994 1.1 tsubai
995 1.1 tsubai /* Make sure we are not interrupted while building the table. */
996 1.28 briggs if (adbHardware != ADB_HW_PMU) /* ints must be on for PMU? */
997 1.1 tsubai s = splhigh();
998 1.1 tsubai
999 1.1 tsubai ADBNumDevices = 0; /* no devices yet */
1000 1.1 tsubai
1001 1.1 tsubai /* Let intr routines know we are running reinit */
1002 1.1 tsubai adbStarting = 1;
1003 1.1 tsubai
1004 1.1 tsubai /*
1005 1.1 tsubai * Initialize the ADB table. For now, we'll always use the same table
1006 1.1 tsubai * that is defined at the beginning of this file - no mallocs.
1007 1.1 tsubai */
1008 1.1 tsubai for (i = 0; i < 16; i++)
1009 1.1 tsubai ADBDevTable[i].devType = 0;
1010 1.1 tsubai
1011 1.1 tsubai adb_setup_hw_type(); /* setup hardware type */
1012 1.1 tsubai
1013 1.1 tsubai adb_hw_setup(); /* init the VIA bits and hard reset ADB */
1014 1.1 tsubai
1015 1.8 tsubai delay(1000);
1016 1.1 tsubai
1017 1.1 tsubai /* send an ADB reset first */
1018 1.32 nathanw result = adb_op_sync((Ptr)0, NULL, (Ptr)0, (short)0x00);
1019 1.16 tsubai delay(200000);
1020 1.1 tsubai
1021 1.20 dbj #ifdef ADB_DEBUG
1022 1.20 dbj if (result && adb_debug) {
1023 1.20 dbj printf_intr("adb_reinit: failed to reset, result = %d\n",result);
1024 1.20 dbj }
1025 1.20 dbj #endif
1026 1.20 dbj
1027 1.1 tsubai /*
1028 1.1 tsubai * Probe for ADB devices. Probe devices 1-15 quickly to determine
1029 1.1 tsubai * which device addresses are in use and which are free. For each
1030 1.1 tsubai * address that is in use, move the device at that address to a higher
1031 1.1 tsubai * free address. Continue doing this at that address until no device
1032 1.1 tsubai * responds at that address. Then move the last device that was moved
1033 1.1 tsubai * back to the original address. Do this for the remaining addresses
1034 1.1 tsubai * that we determined were in use.
1035 1.1 tsubai *
1036 1.1 tsubai * When finished, do this entire process over again with the updated
1037 1.1 tsubai * list of in use addresses. Do this until no new devices have been
1038 1.1 tsubai * found in 20 passes though the in use address list. (This probably
1039 1.1 tsubai * seems long and complicated, but it's the best way to detect multiple
1040 1.1 tsubai * devices at the same address - sometimes it takes a couple of tries
1041 1.1 tsubai * before the collision is detected.)
1042 1.1 tsubai */
1043 1.1 tsubai
1044 1.1 tsubai /* initial scan through the devices */
1045 1.1 tsubai for (i = 1; i < 16; i++) {
1046 1.12 tsubai send_string[0] = 0;
1047 1.14 tsubai command = ADBTALK(i, 3);
1048 1.32 nathanw result = adb_op_sync((Ptr)send_string, NULL,
1049 1.1 tsubai (Ptr)0, (short)command);
1050 1.20 dbj
1051 1.20 dbj #ifdef ADB_DEBUG
1052 1.20 dbj if (result && adb_debug) {
1053 1.20 dbj printf_intr("adb_reinit: scan of device %d, result = %d, str = 0x%x\n",
1054 1.20 dbj i,result,send_string[0]);
1055 1.20 dbj }
1056 1.20 dbj #endif
1057 1.14 tsubai
1058 1.14 tsubai if (send_string[0] != 0) {
1059 1.14 tsubai /* check for valid device handler */
1060 1.14 tsubai switch (send_string[2]) {
1061 1.14 tsubai case 0:
1062 1.14 tsubai case 0xfd:
1063 1.14 tsubai case 0xfe:
1064 1.14 tsubai case 0xff:
1065 1.14 tsubai continue; /* invalid, skip */
1066 1.14 tsubai }
1067 1.14 tsubai
1068 1.14 tsubai /* found a device */
1069 1.14 tsubai ++ADBNumDevices;
1070 1.14 tsubai KASSERT(ADBNumDevices < 16);
1071 1.14 tsubai ADBDevTable[ADBNumDevices].devType =
1072 1.14 tsubai (int)send_string[2];
1073 1.1 tsubai ADBDevTable[ADBNumDevices].origAddr = i;
1074 1.1 tsubai ADBDevTable[ADBNumDevices].currentAddr = i;
1075 1.1 tsubai ADBDevTable[ADBNumDevices].DataAreaAddr =
1076 1.1 tsubai (long)0;
1077 1.1 tsubai ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
1078 1.1 tsubai pm_check_adb_devices(i); /* tell pm driver device
1079 1.1 tsubai * is here */
1080 1.1 tsubai }
1081 1.1 tsubai }
1082 1.1 tsubai
1083 1.1 tsubai /* find highest unused address */
1084 1.1 tsubai for (saveptr = 15; saveptr > 0; saveptr--)
1085 1.1 tsubai if (-1 == get_adb_info(&data, saveptr))
1086 1.1 tsubai break;
1087 1.1 tsubai
1088 1.1 tsubai #ifdef ADB_DEBUG
1089 1.1 tsubai if (adb_debug & 0x80) {
1090 1.1 tsubai printf_intr("first free is: 0x%02x\n", saveptr);
1091 1.1 tsubai printf_intr("devices: %i\n", ADBNumDevices);
1092 1.1 tsubai }
1093 1.1 tsubai #endif
1094 1.1 tsubai
1095 1.1 tsubai nonewtimes = 0; /* no loops w/o new devices */
1096 1.14 tsubai while (saveptr > 0 && nonewtimes++ < 11) {
1097 1.1 tsubai for (i = 1; i <= ADBNumDevices; i++) {
1098 1.1 tsubai device = ADBDevTable[i].currentAddr;
1099 1.1 tsubai #ifdef ADB_DEBUG
1100 1.1 tsubai if (adb_debug & 0x80)
1101 1.1 tsubai printf_intr("moving device 0x%02x to 0x%02x "
1102 1.1 tsubai "(index 0x%02x) ", device, saveptr, i);
1103 1.1 tsubai #endif
1104 1.1 tsubai
1105 1.1 tsubai /* send TALK R3 to address */
1106 1.14 tsubai command = ADBTALK(device, 3);
1107 1.32 nathanw adb_op_sync((Ptr)send_string, NULL,
1108 1.1 tsubai (Ptr)0, (short)command);
1109 1.1 tsubai
1110 1.1 tsubai /* move device to higher address */
1111 1.14 tsubai command = ADBLISTEN(device, 3);
1112 1.1 tsubai send_string[0] = 2;
1113 1.1 tsubai send_string[1] = (u_char)(saveptr | 0x60);
1114 1.1 tsubai send_string[2] = 0xfe;
1115 1.32 nathanw adb_op_sync((Ptr)send_string, NULL,
1116 1.1 tsubai (Ptr)0, (short)command);
1117 1.8 tsubai delay(500);
1118 1.1 tsubai
1119 1.14 tsubai /* send TALK R3 - anything at new address? */
1120 1.14 tsubai command = ADBTALK(saveptr, 3);
1121 1.32 nathanw adb_op_sync((Ptr)send_string, NULL,
1122 1.14 tsubai (Ptr)0, (short)command);
1123 1.14 tsubai delay(500);
1124 1.14 tsubai
1125 1.14 tsubai if (send_string[0] == 0) {
1126 1.14 tsubai #ifdef ADB_DEBUG
1127 1.14 tsubai if (adb_debug & 0x80)
1128 1.14 tsubai printf_intr("failed, continuing\n");
1129 1.14 tsubai #endif
1130 1.14 tsubai continue;
1131 1.14 tsubai }
1132 1.14 tsubai
1133 1.1 tsubai /* send TALK R3 - anything at old address? */
1134 1.14 tsubai command = ADBTALK(device, 3);
1135 1.32 nathanw result = adb_op_sync((Ptr)send_string, NULL,
1136 1.1 tsubai (Ptr)0, (short)command);
1137 1.1 tsubai if (send_string[0] != 0) {
1138 1.14 tsubai /* check for valid device handler */
1139 1.14 tsubai switch (send_string[2]) {
1140 1.14 tsubai case 0:
1141 1.14 tsubai case 0xfd:
1142 1.14 tsubai case 0xfe:
1143 1.14 tsubai case 0xff:
1144 1.14 tsubai continue; /* invalid, skip */
1145 1.14 tsubai }
1146 1.14 tsubai
1147 1.1 tsubai /* new device found */
1148 1.1 tsubai /* update data for previously moved device */
1149 1.1 tsubai ADBDevTable[i].currentAddr = saveptr;
1150 1.1 tsubai #ifdef ADB_DEBUG
1151 1.1 tsubai if (adb_debug & 0x80)
1152 1.1 tsubai printf_intr("old device at index %i\n",i);
1153 1.1 tsubai #endif
1154 1.1 tsubai /* add new device in table */
1155 1.1 tsubai #ifdef ADB_DEBUG
1156 1.1 tsubai if (adb_debug & 0x80)
1157 1.1 tsubai printf_intr("new device found\n");
1158 1.1 tsubai #endif
1159 1.14 tsubai if (saveptr > ADBNumDevices) {
1160 1.14 tsubai ++ADBNumDevices;
1161 1.14 tsubai KASSERT(ADBNumDevices < 16);
1162 1.14 tsubai }
1163 1.14 tsubai ADBDevTable[ADBNumDevices].devType =
1164 1.14 tsubai (int)send_string[2];
1165 1.1 tsubai ADBDevTable[ADBNumDevices].origAddr = device;
1166 1.1 tsubai ADBDevTable[ADBNumDevices].currentAddr = device;
1167 1.1 tsubai /* These will be set correctly in adbsys.c */
1168 1.1 tsubai /* Until then, unsol. data will be ignored. */
1169 1.1 tsubai ADBDevTable[ADBNumDevices].DataAreaAddr =
1170 1.1 tsubai (long)0;
1171 1.1 tsubai ADBDevTable[ADBNumDevices].ServiceRtPtr =
1172 1.1 tsubai (void *)0;
1173 1.1 tsubai /* find next unused address */
1174 1.14 tsubai for (x = saveptr; x > 0; x--) {
1175 1.1 tsubai if (-1 == get_adb_info(&data, x)) {
1176 1.1 tsubai saveptr = x;
1177 1.1 tsubai break;
1178 1.1 tsubai }
1179 1.14 tsubai }
1180 1.14 tsubai if (x == 0)
1181 1.14 tsubai saveptr = 0;
1182 1.1 tsubai #ifdef ADB_DEBUG
1183 1.1 tsubai if (adb_debug & 0x80)
1184 1.1 tsubai printf_intr("new free is 0x%02x\n",
1185 1.1 tsubai saveptr);
1186 1.1 tsubai #endif
1187 1.1 tsubai nonewtimes = 0;
1188 1.1 tsubai /* tell pm driver device is here */
1189 1.1 tsubai pm_check_adb_devices(device);
1190 1.1 tsubai } else {
1191 1.1 tsubai #ifdef ADB_DEBUG
1192 1.1 tsubai if (adb_debug & 0x80)
1193 1.1 tsubai printf_intr("moving back...\n");
1194 1.1 tsubai #endif
1195 1.1 tsubai /* move old device back */
1196 1.14 tsubai command = ADBLISTEN(saveptr, 3);
1197 1.1 tsubai send_string[0] = 2;
1198 1.1 tsubai send_string[1] = (u_char)(device | 0x60);
1199 1.1 tsubai send_string[2] = 0xfe;
1200 1.32 nathanw adb_op_sync((Ptr)send_string, NULL,
1201 1.1 tsubai (Ptr)0, (short)command);
1202 1.8 tsubai delay(1000);
1203 1.1 tsubai }
1204 1.1 tsubai }
1205 1.1 tsubai }
1206 1.1 tsubai
1207 1.1 tsubai #ifdef ADB_DEBUG
1208 1.1 tsubai if (adb_debug) {
1209 1.1 tsubai for (i = 1; i <= ADBNumDevices; i++) {
1210 1.1 tsubai x = get_ind_adb_info(&data, i);
1211 1.1 tsubai if (x != -1)
1212 1.1 tsubai printf_intr("index 0x%x, addr 0x%x, type 0x%x\n",
1213 1.1 tsubai i, x, data.devType);
1214 1.1 tsubai }
1215 1.1 tsubai }
1216 1.1 tsubai #endif
1217 1.1 tsubai
1218 1.6 tsubai #ifdef ADB_DEBUG
1219 1.6 tsubai if (adb_debug) {
1220 1.6 tsubai if (0 == ADBNumDevices) /* tell user if no devices found */
1221 1.6 tsubai printf_intr("adb: no devices found\n");
1222 1.6 tsubai }
1223 1.6 tsubai #endif
1224 1.1 tsubai
1225 1.1 tsubai adbStarting = 0; /* not starting anymore */
1226 1.1 tsubai #ifdef ADB_DEBUG
1227 1.6 tsubai if (adb_debug)
1228 1.6 tsubai printf_intr("adb: ADBReInit complete\n");
1229 1.1 tsubai #endif
1230 1.1 tsubai
1231 1.1 tsubai if (adbHardware == ADB_HW_CUDA)
1232 1.13 thorpej callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
1233 1.13 thorpej (void *)adb_cuda_tickle, NULL);
1234 1.1 tsubai
1235 1.28 briggs if (adbHardware != ADB_HW_PMU) /* ints must be on for PMU? */
1236 1.1 tsubai splx(s);
1237 1.1 tsubai }
1238 1.1 tsubai
1239 1.1 tsubai /*
1240 1.1 tsubai * adb_cmd_result
1241 1.1 tsubai *
1242 1.1 tsubai * This routine lets the caller know whether the specified adb command string
1243 1.1 tsubai * should expect a returned result, such as a TALK command.
1244 1.1 tsubai *
1245 1.1 tsubai * returns: 0 if a result should be expected
1246 1.1 tsubai * 1 if a result should NOT be expected
1247 1.1 tsubai */
1248 1.1 tsubai int
1249 1.1 tsubai adb_cmd_result(u_char *in)
1250 1.1 tsubai {
1251 1.1 tsubai switch (adbHardware) {
1252 1.1 tsubai case ADB_HW_CUDA:
1253 1.1 tsubai /* was it an ADB talk command? */
1254 1.1 tsubai if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
1255 1.1 tsubai return 0;
1256 1.1 tsubai /* was it an RTC/PRAM read date/time? */
1257 1.1 tsubai if ((in[1] == 0x01) && (in[2] == 0x03))
1258 1.1 tsubai return 0;
1259 1.1 tsubai return 1;
1260 1.1 tsubai
1261 1.28 briggs case ADB_HW_PMU:
1262 1.1 tsubai return 1;
1263 1.1 tsubai
1264 1.1 tsubai case ADB_HW_UNKNOWN:
1265 1.1 tsubai default:
1266 1.1 tsubai return 1;
1267 1.1 tsubai }
1268 1.1 tsubai }
1269 1.1 tsubai
1270 1.1 tsubai
1271 1.1 tsubai /*
1272 1.1 tsubai * adb_cmd_extra
1273 1.1 tsubai *
1274 1.1 tsubai * This routine lets the caller know whether the specified adb command string
1275 1.1 tsubai * may have extra data appended to the end of it, such as a LISTEN command.
1276 1.1 tsubai *
1277 1.1 tsubai * returns: 0 if extra data is allowed
1278 1.1 tsubai * 1 if extra data is NOT allowed
1279 1.1 tsubai */
1280 1.1 tsubai int
1281 1.1 tsubai adb_cmd_extra(u_char *in)
1282 1.1 tsubai {
1283 1.1 tsubai switch (adbHardware) {
1284 1.1 tsubai case ADB_HW_CUDA:
1285 1.1 tsubai /*
1286 1.1 tsubai * TO DO: support needs to be added to recognize RTC and PRAM
1287 1.1 tsubai * commands
1288 1.1 tsubai */
1289 1.1 tsubai if ((in[2] & 0x0c) == 0x08) /* was it a listen command? */
1290 1.1 tsubai return 0;
1291 1.1 tsubai /* add others later */
1292 1.1 tsubai return 1;
1293 1.1 tsubai
1294 1.28 briggs case ADB_HW_PMU:
1295 1.1 tsubai return 1;
1296 1.1 tsubai
1297 1.1 tsubai case ADB_HW_UNKNOWN:
1298 1.1 tsubai default:
1299 1.1 tsubai return 1;
1300 1.1 tsubai }
1301 1.1 tsubai }
1302 1.1 tsubai
1303 1.1 tsubai /*
1304 1.1 tsubai * adb_op_sync
1305 1.1 tsubai *
1306 1.1 tsubai * This routine does exactly what the adb_op routine does, except that after
1307 1.1 tsubai * the adb_op is called, it waits until the return value is present before
1308 1.1 tsubai * returning.
1309 1.1 tsubai *
1310 1.1 tsubai * NOTE: The user specified compRout is ignored, since this routine specifies
1311 1.1 tsubai * it's own to adb_op, which is why you really called this in the first place
1312 1.1 tsubai * anyway.
1313 1.1 tsubai */
1314 1.1 tsubai int
1315 1.32 nathanw adb_op_sync(Ptr buffer, adbComp *compRout, Ptr data, short command)
1316 1.1 tsubai {
1317 1.15 tsubai int tmout;
1318 1.1 tsubai int result;
1319 1.1 tsubai volatile int flag = 0;
1320 1.1 tsubai
1321 1.32 nathanw result = adb_op(buffer, adb_op_comprout,
1322 1.32 nathanw &flag, command); /* send command */
1323 1.15 tsubai if (result == 0) { /* send ok? */
1324 1.15 tsubai /*
1325 1.15 tsubai * Total time to wait is calculated as follows:
1326 1.15 tsubai * - Tlt (stop to start time): 260 usec
1327 1.15 tsubai * - start bit: 100 usec
1328 1.15 tsubai * - up to 8 data bytes: 64 * 100 usec = 6400 usec
1329 1.15 tsubai * - stop bit (with SRQ): 140 usec
1330 1.15 tsubai * Total: 6900 usec
1331 1.15 tsubai *
1332 1.15 tsubai * This is the total time allowed by the specification. Any
1333 1.15 tsubai * device that doesn't conform to this will fail to operate
1334 1.15 tsubai * properly on some Apple systems. In spite of this we
1335 1.15 tsubai * double the time to wait; some Cuda-based apparently
1336 1.15 tsubai * queues some commands and allows the main CPU to continue
1337 1.15 tsubai * processing (radical concept, eh?). To be safe, allow
1338 1.15 tsubai * time for two complete ADB transactions to occur.
1339 1.15 tsubai */
1340 1.15 tsubai for (tmout = 13800; !flag && tmout >= 10; tmout -= 10)
1341 1.15 tsubai delay(10);
1342 1.15 tsubai if (!flag && tmout > 0)
1343 1.15 tsubai delay(tmout);
1344 1.15 tsubai
1345 1.15 tsubai if (!flag)
1346 1.15 tsubai result = -2;
1347 1.15 tsubai }
1348 1.1 tsubai
1349 1.1 tsubai return result;
1350 1.1 tsubai }
1351 1.1 tsubai
1352 1.1 tsubai /*
1353 1.1 tsubai * adb_op_comprout
1354 1.1 tsubai *
1355 1.1 tsubai * This function is used by the adb_op_sync routine so it knows when the
1356 1.1 tsubai * function is done.
1357 1.1 tsubai */
1358 1.32 nathanw void
1359 1.32 nathanw adb_op_comprout(caddr_t buffer, volatile int *compdata, int cmd)
1360 1.1 tsubai {
1361 1.32 nathanw volatile int *p = compdata;
1362 1.1 tsubai
1363 1.1 tsubai *p = 1;
1364 1.1 tsubai }
1365 1.1 tsubai
1366 1.1 tsubai void
1367 1.1 tsubai adb_setup_hw_type(void)
1368 1.1 tsubai {
1369 1.6 tsubai switch (adbHardware) {
1370 1.6 tsubai case ADB_HW_CUDA:
1371 1.4 tsubai return;
1372 1.4 tsubai
1373 1.28 briggs case ADB_HW_PMU:
1374 1.4 tsubai pm_setup_adb();
1375 1.4 tsubai return;
1376 1.6 tsubai
1377 1.6 tsubai default:
1378 1.6 tsubai panic("unknown adb hardware");
1379 1.4 tsubai }
1380 1.1 tsubai }
1381 1.1 tsubai
1382 1.1 tsubai int
1383 1.1 tsubai count_adbs(void)
1384 1.1 tsubai {
1385 1.1 tsubai int i;
1386 1.1 tsubai int found;
1387 1.1 tsubai
1388 1.1 tsubai found = 0;
1389 1.1 tsubai
1390 1.1 tsubai for (i = 1; i < 16; i++)
1391 1.1 tsubai if (0 != ADBDevTable[i].devType)
1392 1.1 tsubai found++;
1393 1.1 tsubai
1394 1.1 tsubai return found;
1395 1.1 tsubai }
1396 1.1 tsubai
1397 1.1 tsubai int
1398 1.1 tsubai get_ind_adb_info(ADBDataBlock * info, int index)
1399 1.1 tsubai {
1400 1.1 tsubai if ((index < 1) || (index > 15)) /* check range 1-15 */
1401 1.1 tsubai return (-1);
1402 1.1 tsubai
1403 1.1 tsubai #ifdef ADB_DEBUG
1404 1.1 tsubai if (adb_debug & 0x80)
1405 1.1 tsubai printf_intr("index 0x%x devType is: 0x%x\n", index,
1406 1.1 tsubai ADBDevTable[index].devType);
1407 1.1 tsubai #endif
1408 1.1 tsubai if (0 == ADBDevTable[index].devType) /* make sure it's a valid entry */
1409 1.1 tsubai return (-1);
1410 1.1 tsubai
1411 1.1 tsubai info->devType = ADBDevTable[index].devType;
1412 1.1 tsubai info->origADBAddr = ADBDevTable[index].origAddr;
1413 1.1 tsubai info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
1414 1.1 tsubai info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
1415 1.1 tsubai
1416 1.1 tsubai return (ADBDevTable[index].currentAddr);
1417 1.1 tsubai }
1418 1.1 tsubai
1419 1.1 tsubai int
1420 1.1 tsubai get_adb_info(ADBDataBlock * info, int adbAddr)
1421 1.1 tsubai {
1422 1.1 tsubai int i;
1423 1.1 tsubai
1424 1.1 tsubai if ((adbAddr < 1) || (adbAddr > 15)) /* check range 1-15 */
1425 1.1 tsubai return (-1);
1426 1.1 tsubai
1427 1.1 tsubai for (i = 1; i < 15; i++)
1428 1.1 tsubai if (ADBDevTable[i].currentAddr == adbAddr) {
1429 1.1 tsubai info->devType = ADBDevTable[i].devType;
1430 1.1 tsubai info->origADBAddr = ADBDevTable[i].origAddr;
1431 1.1 tsubai info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
1432 1.1 tsubai info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
1433 1.1 tsubai return 0; /* found */
1434 1.1 tsubai }
1435 1.1 tsubai
1436 1.1 tsubai return (-1); /* not found */
1437 1.1 tsubai }
1438 1.1 tsubai
1439 1.1 tsubai int
1440 1.1 tsubai set_adb_info(ADBSetInfoBlock * info, int adbAddr)
1441 1.1 tsubai {
1442 1.1 tsubai int i;
1443 1.1 tsubai
1444 1.1 tsubai if ((adbAddr < 1) || (adbAddr > 15)) /* check range 1-15 */
1445 1.1 tsubai return (-1);
1446 1.1 tsubai
1447 1.1 tsubai for (i = 1; i < 15; i++)
1448 1.1 tsubai if (ADBDevTable[i].currentAddr == adbAddr) {
1449 1.1 tsubai ADBDevTable[i].ServiceRtPtr =
1450 1.1 tsubai (void *)(info->siServiceRtPtr);
1451 1.1 tsubai ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
1452 1.1 tsubai return 0; /* found */
1453 1.1 tsubai }
1454 1.1 tsubai
1455 1.1 tsubai return (-1); /* not found */
1456 1.1 tsubai
1457 1.1 tsubai }
1458 1.1 tsubai
1459 1.6 tsubai #ifndef MRG_ADB
1460 1.6 tsubai
1461 1.35 wiz /* caller should really use machine-independent version: getPramTime */
1462 1.1 tsubai /* this version does pseudo-adb access only */
1463 1.1 tsubai int
1464 1.34 kardel adb_read_date_time(unsigned long *t)
1465 1.1 tsubai {
1466 1.1 tsubai u_char output[ADB_MAX_MSG_LENGTH];
1467 1.1 tsubai int result;
1468 1.1 tsubai volatile int flag = 0;
1469 1.1 tsubai
1470 1.1 tsubai switch (adbHardware) {
1471 1.28 briggs case ADB_HW_PMU:
1472 1.34 kardel pm_read_date_time(t);
1473 1.10 tsubai return 0;
1474 1.1 tsubai
1475 1.1 tsubai case ADB_HW_CUDA:
1476 1.1 tsubai output[0] = 0x02; /* 2 byte message */
1477 1.1 tsubai output[1] = 0x01; /* to pram/rtc device */
1478 1.1 tsubai output[2] = 0x03; /* read date/time */
1479 1.1 tsubai result = send_adb_cuda((u_char *)output, (u_char *)output,
1480 1.32 nathanw adb_op_comprout, &flag, (int)0);
1481 1.1 tsubai if (result != 0) /* exit if not sent */
1482 1.1 tsubai return -1;
1483 1.1 tsubai
1484 1.1 tsubai while (0 == flag) /* wait for result */
1485 1.1 tsubai ;
1486 1.1 tsubai
1487 1.34 kardel memcpy(t, output + 1, 4);
1488 1.1 tsubai return 0;
1489 1.1 tsubai
1490 1.1 tsubai case ADB_HW_UNKNOWN:
1491 1.1 tsubai default:
1492 1.1 tsubai return -1;
1493 1.1 tsubai }
1494 1.1 tsubai }
1495 1.1 tsubai
1496 1.35 wiz /* caller should really use machine-independent version: setPramTime */
1497 1.1 tsubai /* this version does pseudo-adb access only */
1498 1.1 tsubai int
1499 1.34 kardel adb_set_date_time(unsigned long t)
1500 1.1 tsubai {
1501 1.1 tsubai u_char output[ADB_MAX_MSG_LENGTH];
1502 1.1 tsubai int result;
1503 1.1 tsubai volatile int flag = 0;
1504 1.1 tsubai
1505 1.1 tsubai switch (adbHardware) {
1506 1.1 tsubai
1507 1.1 tsubai case ADB_HW_CUDA:
1508 1.1 tsubai output[0] = 0x06; /* 6 byte message */
1509 1.1 tsubai output[1] = 0x01; /* to pram/rtc device */
1510 1.1 tsubai output[2] = 0x09; /* set date/time */
1511 1.34 kardel output[3] = (u_char)(t >> 24);
1512 1.34 kardel output[4] = (u_char)(t >> 16);
1513 1.34 kardel output[5] = (u_char)(t >> 8);
1514 1.34 kardel output[6] = (u_char)(t);
1515 1.1 tsubai result = send_adb_cuda((u_char *)output, (u_char *)0,
1516 1.32 nathanw adb_op_comprout, &flag, (int)0);
1517 1.1 tsubai if (result != 0) /* exit if not sent */
1518 1.1 tsubai return -1;
1519 1.1 tsubai
1520 1.1 tsubai while (0 == flag) /* wait for send to finish */
1521 1.1 tsubai ;
1522 1.1 tsubai
1523 1.1 tsubai return 0;
1524 1.1 tsubai
1525 1.28 briggs case ADB_HW_PMU:
1526 1.34 kardel pm_set_date_time(t);
1527 1.10 tsubai return 0;
1528 1.10 tsubai
1529 1.1 tsubai case ADB_HW_UNKNOWN:
1530 1.1 tsubai default:
1531 1.1 tsubai return -1;
1532 1.1 tsubai }
1533 1.1 tsubai }
1534 1.1 tsubai
1535 1.1 tsubai
1536 1.1 tsubai int
1537 1.1 tsubai adb_poweroff(void)
1538 1.1 tsubai {
1539 1.1 tsubai u_char output[ADB_MAX_MSG_LENGTH];
1540 1.1 tsubai int result;
1541 1.1 tsubai
1542 1.9 tsubai adb_polling = 1;
1543 1.9 tsubai
1544 1.1 tsubai switch (adbHardware) {
1545 1.28 briggs case ADB_HW_PMU:
1546 1.11 tsubai pm_adb_poweroff();
1547 1.11 tsubai
1548 1.11 tsubai for (;;); /* wait for power off */
1549 1.11 tsubai
1550 1.11 tsubai return 0;
1551 1.1 tsubai
1552 1.1 tsubai case ADB_HW_CUDA:
1553 1.1 tsubai output[0] = 0x02; /* 2 byte message */
1554 1.1 tsubai output[1] = 0x01; /* to pram/rtc/soft-power device */
1555 1.1 tsubai output[2] = 0x0a; /* set date/time */
1556 1.1 tsubai result = send_adb_cuda((u_char *)output, (u_char *)0,
1557 1.1 tsubai (void *)0, (void *)0, (int)0);
1558 1.1 tsubai if (result != 0) /* exit if not sent */
1559 1.1 tsubai return -1;
1560 1.1 tsubai
1561 1.1 tsubai for (;;); /* wait for power off */
1562 1.1 tsubai
1563 1.1 tsubai return 0;
1564 1.1 tsubai
1565 1.1 tsubai case ADB_HW_UNKNOWN:
1566 1.1 tsubai default:
1567 1.1 tsubai return -1;
1568 1.1 tsubai }
1569 1.1 tsubai }
1570 1.1 tsubai
1571 1.1 tsubai int
1572 1.1 tsubai CountADBs(void)
1573 1.1 tsubai {
1574 1.1 tsubai return (count_adbs());
1575 1.1 tsubai }
1576 1.1 tsubai
1577 1.1 tsubai void
1578 1.1 tsubai ADBReInit(void)
1579 1.1 tsubai {
1580 1.1 tsubai adb_reinit();
1581 1.1 tsubai }
1582 1.1 tsubai
1583 1.1 tsubai int
1584 1.1 tsubai GetIndADB(ADBDataBlock * info, int index)
1585 1.1 tsubai {
1586 1.1 tsubai return (get_ind_adb_info(info, index));
1587 1.1 tsubai }
1588 1.1 tsubai
1589 1.1 tsubai int
1590 1.1 tsubai GetADBInfo(ADBDataBlock * info, int adbAddr)
1591 1.1 tsubai {
1592 1.1 tsubai return (get_adb_info(info, adbAddr));
1593 1.1 tsubai }
1594 1.1 tsubai
1595 1.1 tsubai int
1596 1.1 tsubai SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
1597 1.1 tsubai {
1598 1.1 tsubai return (set_adb_info(info, adbAddr));
1599 1.1 tsubai }
1600 1.1 tsubai
1601 1.1 tsubai int
1602 1.32 nathanw ADBOp(Ptr buffer, adbComp *compRout, Ptr data, short commandNum)
1603 1.1 tsubai {
1604 1.1 tsubai return (adb_op(buffer, compRout, data, commandNum));
1605 1.1 tsubai }
1606 1.1 tsubai
1607 1.1 tsubai #endif
1608 1.1 tsubai
1609 1.1 tsubai int
1610 1.1 tsubai setsoftadb()
1611 1.1 tsubai {
1612 1.13 thorpej callout_reset(&adb_soft_intr_ch, 1, (void *)adb_soft_intr, NULL);
1613 1.1 tsubai return 0;
1614 1.1 tsubai }
1615 1.1 tsubai
1616 1.1 tsubai void
1617 1.3 tsubai adb_cuda_autopoll()
1618 1.1 tsubai {
1619 1.1 tsubai volatile int flag = 0;
1620 1.1 tsubai int result;
1621 1.1 tsubai u_char output[16];
1622 1.1 tsubai
1623 1.1 tsubai output[0] = 0x03; /* 3-byte message */
1624 1.1 tsubai output[1] = 0x01; /* to pram/rtc device */
1625 1.1 tsubai output[2] = 0x01; /* cuda autopoll */
1626 1.1 tsubai output[3] = 0x01;
1627 1.32 nathanw result = send_adb_cuda(output, output, adb_op_comprout,
1628 1.32 nathanw &flag, 0);
1629 1.1 tsubai if (result != 0) /* exit if not sent */
1630 1.1 tsubai return;
1631 1.1 tsubai
1632 1.1 tsubai while (flag == 0); /* wait for result */
1633 1.1 tsubai }
1634 1.1 tsubai
1635 1.1 tsubai void
1636 1.17 matt adb_restart(void)
1637 1.1 tsubai {
1638 1.1 tsubai int result;
1639 1.1 tsubai u_char output[16];
1640 1.1 tsubai
1641 1.9 tsubai adb_polling = 1;
1642 1.9 tsubai
1643 1.4 tsubai switch (adbHardware) {
1644 1.4 tsubai case ADB_HW_CUDA:
1645 1.4 tsubai output[0] = 0x02; /* 2 byte message */
1646 1.4 tsubai output[1] = 0x01; /* to pram/rtc/soft-power device */
1647 1.4 tsubai output[2] = 0x11; /* restart */
1648 1.15 tsubai result = send_adb_cuda(output, NULL, NULL, NULL, 0);
1649 1.4 tsubai if (result != 0) /* exit if not sent */
1650 1.4 tsubai return;
1651 1.4 tsubai while (1); /* not return */
1652 1.4 tsubai
1653 1.28 briggs case ADB_HW_PMU:
1654 1.4 tsubai pm_adb_restart();
1655 1.10 tsubai while (1); /* not return */
1656 1.4 tsubai }
1657 1.1 tsubai }
1658