pm_direct.c revision 1.19 1 1.19 itojun /* $NetBSD: pm_direct.c,v 1.19 2002/06/18 05:22:51 itojun Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*
4 1.1 tsubai * Copyright (C) 1997 Takashi Hamada
5 1.1 tsubai * All rights reserved.
6 1.1 tsubai *
7 1.1 tsubai * Redistribution and use in source and binary forms, with or without
8 1.1 tsubai * modification, are permitted provided that the following conditions
9 1.1 tsubai * are met:
10 1.1 tsubai * 1. Redistributions of source code must retain the above copyright
11 1.1 tsubai * notice, this list of conditions and the following disclaimer.
12 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tsubai * notice, this list of conditions and the following disclaimer in the
14 1.1 tsubai * documentation and/or other materials provided with the distribution.
15 1.1 tsubai * 3. All advertising materials mentioning features or use of this software
16 1.1 tsubai * must display the following acknowledgement:
17 1.1 tsubai * This product includes software developed by Takashi Hamada
18 1.1 tsubai * 4. The name of the author may not be used to endorse or promote products
19 1.1 tsubai * derived from this software without specific prior written permission.
20 1.1 tsubai *
21 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 1.1 tsubai * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 tsubai */
32 1.1 tsubai /* From: pm_direct.c 1.3 03/18/98 Takashi Hamada */
33 1.1 tsubai
34 1.1 tsubai #ifdef DEBUG
35 1.1 tsubai #ifndef ADB_DEBUG
36 1.1 tsubai #define ADB_DEBUG
37 1.1 tsubai #endif
38 1.1 tsubai #endif
39 1.1 tsubai
40 1.1 tsubai /* #define PM_GRAB_SI 1 */
41 1.1 tsubai
42 1.1 tsubai #include <sys/param.h>
43 1.1 tsubai #include <sys/cdefs.h>
44 1.1 tsubai #include <sys/device.h>
45 1.1 tsubai #include <sys/systm.h>
46 1.1 tsubai
47 1.1 tsubai #include <machine/adbsys.h>
48 1.1 tsubai #include <machine/cpu.h>
49 1.1 tsubai
50 1.1 tsubai #include <macppc/dev/adbvar.h>
51 1.1 tsubai #include <macppc/dev/pm_direct.h>
52 1.1 tsubai #include <macppc/dev/viareg.h>
53 1.1 tsubai
54 1.1 tsubai extern int adb_polling; /* Are we polling? (Debugger mode) */
55 1.1 tsubai
56 1.1 tsubai /* hardware dependent values */
57 1.1 tsubai #define ADBDelay 100 /* XXX */
58 1.1 tsubai #define HwCfgFlags3 0x20000 /* XXX */
59 1.1 tsubai
60 1.1 tsubai /* define the types of the Power Manager */
61 1.1 tsubai #define PM_HW_UNKNOWN 0x00 /* don't know */
62 1.1 tsubai #define PM_HW_PB1XX 0x01 /* PowerBook 1XX series */
63 1.1 tsubai #define PM_HW_PB5XX 0x02 /* PowerBook Duo and 5XX series */
64 1.1 tsubai
65 1.1 tsubai /* useful macros */
66 1.1 tsubai #define PM_SR() read_via_reg(VIA1, vSR)
67 1.1 tsubai #define PM_VIA_INTR_ENABLE() write_via_reg(VIA1, vIER, 0x90)
68 1.1 tsubai #define PM_VIA_INTR_DISABLE() write_via_reg(VIA1, vIER, 0x10)
69 1.1 tsubai #define PM_VIA_CLR_INTR() write_via_reg(VIA1, vIFR, 0x90)
70 1.1 tsubai #if 0
71 1.1 tsubai #define PM_SET_STATE_ACKON() via_reg_or(VIA2, vBufB, 0x04)
72 1.1 tsubai #define PM_SET_STATE_ACKOFF() via_reg_and(VIA2, vBufB, ~0x04)
73 1.1 tsubai #define PM_IS_ON (0x02 == (read_via_reg(VIA2, vBufB) & 0x02))
74 1.1 tsubai #define PM_IS_OFF (0x00 == (read_via_reg(VIA2, vBufB) & 0x02))
75 1.1 tsubai #else
76 1.1 tsubai #define PM_SET_STATE_ACKON() via_reg_or(VIA2, vBufB, 0x10)
77 1.1 tsubai #define PM_SET_STATE_ACKOFF() via_reg_and(VIA2, vBufB, ~0x10)
78 1.1 tsubai #define PM_IS_ON (0x08 == (read_via_reg(VIA2, vBufB) & 0x08))
79 1.1 tsubai #define PM_IS_OFF (0x00 == (read_via_reg(VIA2, vBufB) & 0x08))
80 1.1 tsubai #endif
81 1.1 tsubai
82 1.1 tsubai /*
83 1.1 tsubai * Variables for internal use
84 1.1 tsubai */
85 1.1 tsubai int pmHardware = PM_HW_UNKNOWN;
86 1.1 tsubai u_short pm_existent_ADB_devices = 0x0; /* each bit expresses the existent ADB device */
87 1.1 tsubai u_int pm_LCD_brightness = 0x0;
88 1.1 tsubai u_int pm_LCD_contrast = 0x0;
89 1.1 tsubai u_int pm_counter = 0; /* clock count */
90 1.1 tsubai
91 1.1 tsubai /* these values shows that number of data returned after 'send' cmd is sent */
92 1.1 tsubai signed char pm_send_cmd_type[] = {
93 1.1 tsubai -1, -1, -1, -1, -1, -1, -1, -1,
94 1.1 tsubai -1, -1, -1, -1, -1, -1, -1, -1,
95 1.1 tsubai 0x01, 0x01, -1, -1, -1, -1, -1, -1,
96 1.1 tsubai 0x00, 0x00, -1, -1, -1, -1, -1, 0x00,
97 1.1 tsubai -1, 0x00, 0x02, 0x01, 0x01, -1, -1, -1,
98 1.1 tsubai 0x00, -1, -1, -1, -1, -1, -1, -1,
99 1.5 tsubai 0x04, 0x14, -1, 0x03, -1, -1, -1, -1,
100 1.5 tsubai 0x00, 0x00, 0x02, 0x02, -1, -1, -1, -1,
101 1.1 tsubai 0x01, 0x01, -1, -1, -1, -1, -1, -1,
102 1.7 tsubai 0x00, 0x00, -1, -1, 0x01, -1, -1, -1,
103 1.1 tsubai 0x01, 0x00, 0x02, 0x02, -1, 0x01, 0x03, 0x01,
104 1.1 tsubai 0x00, 0x01, 0x00, 0x00, 0x00, -1, -1, -1,
105 1.1 tsubai 0x02, -1, -1, -1, -1, -1, -1, -1,
106 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -1, -1,
107 1.1 tsubai 0x01, 0x01, 0x01, -1, -1, -1, -1, -1,
108 1.1 tsubai 0x00, 0x00, -1, -1, -1, -1, 0x04, 0x04,
109 1.1 tsubai 0x04, -1, 0x00, -1, -1, -1, -1, -1,
110 1.1 tsubai 0x00, -1, -1, -1, -1, -1, -1, -1,
111 1.1 tsubai 0x01, 0x02, -1, -1, -1, -1, -1, -1,
112 1.1 tsubai 0x00, 0x00, -1, -1, -1, -1, -1, -1,
113 1.1 tsubai 0x02, 0x02, 0x02, 0x04, -1, 0x00, -1, -1,
114 1.1 tsubai 0x01, 0x01, 0x03, 0x02, -1, -1, -1, -1,
115 1.1 tsubai -1, -1, -1, -1, -1, -1, -1, -1,
116 1.1 tsubai -1, -1, -1, -1, -1, -1, -1, -1,
117 1.1 tsubai -1, -1, -1, -1, -1, -1, -1, -1,
118 1.1 tsubai -1, -1, -1, -1, -1, -1, -1, -1,
119 1.1 tsubai 0x00, -1, -1, -1, -1, -1, -1, -1,
120 1.1 tsubai 0x01, 0x01, -1, -1, 0x00, 0x00, -1, -1,
121 1.1 tsubai -1, 0x04, 0x00, -1, -1, -1, -1, -1,
122 1.1 tsubai 0x03, -1, 0x00, -1, 0x00, -1, -1, 0x00,
123 1.1 tsubai -1, -1, -1, -1, -1, -1, -1, -1,
124 1.1 tsubai -1, -1, -1, -1, -1, -1, -1, -1
125 1.1 tsubai };
126 1.1 tsubai
127 1.1 tsubai /* these values shows that number of data returned after 'receive' cmd is sent */
128 1.1 tsubai signed char pm_receive_cmd_type[] = {
129 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
130 1.1 tsubai -1, -1, -1, -1, -1, -1, -1, -1,
131 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132 1.1 tsubai 0x02, 0x02, -1, -1, -1, -1, -1, 0x00,
133 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134 1.1 tsubai -1, -1, -1, -1, -1, -1, -1, -1,
135 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136 1.5 tsubai 0x05, 0x15, -1, 0x02, -1, -1, -1, -1,
137 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
138 1.1 tsubai 0x02, 0x02, -1, -1, -1, -1, -1, -1,
139 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
140 1.1 tsubai 0x02, 0x00, 0x03, 0x03, -1, -1, -1, -1,
141 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
142 1.1 tsubai 0x04, 0x04, 0x03, 0x09, -1, -1, -1, -1,
143 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
144 1.1 tsubai -1, -1, -1, -1, -1, -1, 0x01, 0x01,
145 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
146 1.1 tsubai 0x06, -1, -1, -1, -1, -1, -1, -1,
147 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
148 1.1 tsubai 0x02, 0x02, -1, -1, -1, -1, -1, -1,
149 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
150 1.1 tsubai 0x02, 0x00, 0x00, 0x00, -1, -1, -1, -1,
151 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
152 1.1 tsubai -1, -1, -1, -1, -1, -1, -1, -1,
153 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
154 1.1 tsubai -1, -1, -1, -1, -1, -1, -1, -1,
155 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
156 1.1 tsubai 0x02, 0x02, -1, -1, 0x02, -1, -1, -1,
157 1.1 tsubai 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
158 1.1 tsubai -1, -1, 0x02, -1, -1, -1, -1, 0x00,
159 1.1 tsubai 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
160 1.1 tsubai -1, -1, -1, -1, -1, -1, -1, -1,
161 1.1 tsubai };
162 1.1 tsubai
163 1.1 tsubai
164 1.1 tsubai /*
165 1.1 tsubai * Define the private functions
166 1.1 tsubai */
167 1.1 tsubai
168 1.1 tsubai /* for debugging */
169 1.1 tsubai #ifdef ADB_DEBUG
170 1.1 tsubai void pm_printerr __P((char *, int, int, char *));
171 1.1 tsubai #endif
172 1.1 tsubai
173 1.1 tsubai int pm_wait_busy __P((int));
174 1.1 tsubai int pm_wait_free __P((int));
175 1.1 tsubai
176 1.1 tsubai /* these functions are for the PB1XX series */
177 1.1 tsubai int pm_receive_pm1 __P((u_char *));
178 1.1 tsubai int pm_send_pm1 __P((u_char,int));
179 1.1 tsubai int pm_pmgrop_pm1 __P((PMData *));
180 1.1 tsubai void pm_intr_pm1 __P((void));
181 1.1 tsubai
182 1.1 tsubai /* these functions are for the PB Duo series and the PB 5XX series */
183 1.1 tsubai int pm_receive_pm2 __P((u_char *));
184 1.1 tsubai int pm_send_pm2 __P((u_char));
185 1.1 tsubai int pm_pmgrop_pm2 __P((PMData *));
186 1.1 tsubai void pm_intr_pm2 __P((void));
187 1.1 tsubai
188 1.1 tsubai /* these functions are called from adb_direct.c */
189 1.1 tsubai void pm_setup_adb __P((void));
190 1.1 tsubai void pm_check_adb_devices __P((int));
191 1.1 tsubai void pm_intr __P((void));
192 1.1 tsubai int pm_adb_op __P((u_char *, void *, void *, int));
193 1.1 tsubai
194 1.1 tsubai /* these functions also use the variables of adb_direct.c */
195 1.1 tsubai void pm_adb_get_TALK_result __P((PMData *));
196 1.1 tsubai void pm_adb_get_ADB_data __P((PMData *));
197 1.1 tsubai void pm_adb_poll_next_device_pm1 __P((PMData *));
198 1.1 tsubai
199 1.1 tsubai
200 1.1 tsubai /*
201 1.1 tsubai * These variables are in adb_direct.c.
202 1.1 tsubai */
203 1.1 tsubai extern u_char *adbBuffer; /* pointer to user data area */
204 1.1 tsubai extern void *adbCompRout; /* pointer to the completion routine */
205 1.1 tsubai extern void *adbCompData; /* pointer to the completion routine data */
206 1.1 tsubai extern int adbWaiting; /* waiting for return data from the device */
207 1.1 tsubai extern int adbWaitingCmd; /* ADB command we are waiting for */
208 1.1 tsubai extern int adbStarting; /* doing ADB reinit, so do "polling" differently */
209 1.1 tsubai
210 1.1 tsubai #define ADB_MAX_MSG_LENGTH 16
211 1.1 tsubai #define ADB_MAX_HDR_LENGTH 8
212 1.1 tsubai struct adbCommand {
213 1.1 tsubai u_char header[ADB_MAX_HDR_LENGTH]; /* not used yet */
214 1.1 tsubai u_char data[ADB_MAX_MSG_LENGTH]; /* packet data only */
215 1.1 tsubai u_char *saveBuf; /* where to save result */
216 1.1 tsubai u_char *compRout; /* completion routine pointer */
217 1.1 tsubai u_char *compData; /* completion routine data pointer */
218 1.1 tsubai u_int cmd; /* the original command for this data */
219 1.1 tsubai u_int unsol; /* 1 if packet was unsolicited */
220 1.1 tsubai u_int ack_only; /* 1 for no special processing */
221 1.1 tsubai };
222 1.1 tsubai extern void adb_pass_up __P((struct adbCommand *));
223 1.1 tsubai
224 1.1 tsubai #if 0
225 1.1 tsubai /*
226 1.1 tsubai * Define the external functions
227 1.1 tsubai */
228 1.1 tsubai extern int zshard __P((int)); /* from zs.c */
229 1.1 tsubai #endif
230 1.1 tsubai
231 1.1 tsubai #ifdef ADB_DEBUG
232 1.1 tsubai /*
233 1.1 tsubai * This function dumps contents of the PMData
234 1.1 tsubai */
235 1.1 tsubai void
236 1.1 tsubai pm_printerr(ttl, rval, num, data)
237 1.1 tsubai char *ttl;
238 1.1 tsubai int rval;
239 1.1 tsubai int num;
240 1.1 tsubai char *data;
241 1.1 tsubai {
242 1.1 tsubai int i;
243 1.1 tsubai
244 1.1 tsubai printf("pm: %s:%04x %02x ", ttl, rval, num);
245 1.1 tsubai for (i = 0; i < num; i++)
246 1.1 tsubai printf("%02x ", data[i]);
247 1.1 tsubai printf("\n");
248 1.1 tsubai }
249 1.1 tsubai #endif
250 1.1 tsubai
251 1.1 tsubai
252 1.1 tsubai
253 1.1 tsubai /*
254 1.1 tsubai * Check the hardware type of the Power Manager
255 1.1 tsubai */
256 1.1 tsubai void
257 1.1 tsubai pm_setup_adb()
258 1.1 tsubai {
259 1.1 tsubai pmHardware = PM_HW_PB5XX; /* XXX */
260 1.1 tsubai }
261 1.1 tsubai
262 1.1 tsubai
263 1.1 tsubai /*
264 1.1 tsubai * Check the existent ADB devices
265 1.1 tsubai */
266 1.1 tsubai void
267 1.1 tsubai pm_check_adb_devices(id)
268 1.1 tsubai int id;
269 1.1 tsubai {
270 1.1 tsubai u_short ed = 0x1;
271 1.1 tsubai
272 1.1 tsubai ed <<= id;
273 1.1 tsubai pm_existent_ADB_devices |= ed;
274 1.1 tsubai }
275 1.1 tsubai
276 1.1 tsubai
277 1.1 tsubai /*
278 1.1 tsubai * Wait until PM IC is busy
279 1.1 tsubai */
280 1.1 tsubai int
281 1.1 tsubai pm_wait_busy(delay)
282 1.1 tsubai int delay;
283 1.1 tsubai {
284 1.1 tsubai while (PM_IS_ON) {
285 1.1 tsubai #ifdef PM_GRAB_SI
286 1.1 tsubai #if 0
287 1.1 tsubai zshard(0); /* grab any serial interrupts */
288 1.1 tsubai #else
289 1.1 tsubai (void)intr_dispatch(0x70);
290 1.1 tsubai #endif
291 1.1 tsubai #endif
292 1.1 tsubai if ((--delay) < 0)
293 1.1 tsubai return 1; /* timeout */
294 1.1 tsubai }
295 1.1 tsubai return 0;
296 1.1 tsubai }
297 1.1 tsubai
298 1.1 tsubai
299 1.1 tsubai /*
300 1.1 tsubai * Wait until PM IC is free
301 1.1 tsubai */
302 1.1 tsubai int
303 1.1 tsubai pm_wait_free(delay)
304 1.1 tsubai int delay;
305 1.1 tsubai {
306 1.1 tsubai while (PM_IS_OFF) {
307 1.1 tsubai #ifdef PM_GRAB_SI
308 1.1 tsubai #if 0
309 1.1 tsubai zshard(0); /* grab any serial interrupts */
310 1.1 tsubai #else
311 1.1 tsubai (void)intr_dispatch(0x70);
312 1.1 tsubai #endif
313 1.1 tsubai #endif
314 1.1 tsubai if ((--delay) < 0)
315 1.1 tsubai return 0; /* timeout */
316 1.1 tsubai }
317 1.1 tsubai return 1;
318 1.1 tsubai }
319 1.1 tsubai
320 1.1 tsubai
321 1.1 tsubai
322 1.1 tsubai /*
323 1.1 tsubai * Functions for the PB1XX series
324 1.1 tsubai */
325 1.1 tsubai
326 1.1 tsubai /*
327 1.1 tsubai * Receive data from PM for the PB1XX series
328 1.1 tsubai */
329 1.1 tsubai int
330 1.1 tsubai pm_receive_pm1(data)
331 1.1 tsubai u_char *data;
332 1.1 tsubai {
333 1.1 tsubai #if 0
334 1.1 tsubai int rval = 0xffffcd34;
335 1.1 tsubai
336 1.1 tsubai via_reg(VIA2, vDirA) = 0x00;
337 1.1 tsubai
338 1.1 tsubai switch (1) {
339 1.18 itojun default:
340 1.18 itojun if (pm_wait_busy(0x40) != 0)
341 1.18 itojun break; /* timeout */
342 1.18 itojun
343 1.18 itojun PM_SET_STATE_ACKOFF();
344 1.18 itojun *data = via_reg(VIA2, 0x200);
345 1.18 itojun
346 1.18 itojun rval = 0xffffcd33;
347 1.18 itojun if (pm_wait_free(0x40) == 0)
348 1.18 itojun break; /* timeout */
349 1.1 tsubai
350 1.18 itojun rval = 0x00;
351 1.18 itojun break;
352 1.1 tsubai }
353 1.1 tsubai
354 1.1 tsubai PM_SET_STATE_ACKON();
355 1.1 tsubai via_reg(VIA2, vDirA) = 0x00;
356 1.1 tsubai
357 1.1 tsubai return rval;
358 1.1 tsubai #else
359 1.1 tsubai panic("pm_receive_pm1");
360 1.1 tsubai #endif
361 1.1 tsubai }
362 1.1 tsubai
363 1.1 tsubai
364 1.1 tsubai
365 1.1 tsubai /*
366 1.1 tsubai * Send data to PM for the PB1XX series
367 1.1 tsubai */
368 1.1 tsubai int
369 1.1 tsubai pm_send_pm1(data, delay)
370 1.1 tsubai u_char data;
371 1.1 tsubai int delay;
372 1.1 tsubai {
373 1.1 tsubai #if 0
374 1.1 tsubai int rval;
375 1.1 tsubai
376 1.1 tsubai via_reg(VIA2, vDirA) = 0xff;
377 1.1 tsubai via_reg(VIA2, 0x200) = data;
378 1.1 tsubai
379 1.1 tsubai PM_SET_STATE_ACKOFF();
380 1.1 tsubai if (pm_wait_busy(0x400) != 0) {
381 1.1 tsubai PM_SET_STATE_ACKON();
382 1.1 tsubai via_reg(VIA2, vDirA) = 0x00;
383 1.1 tsubai
384 1.1 tsubai return 0xffffcd36;
385 1.1 tsubai }
386 1.1 tsubai
387 1.1 tsubai rval = 0x0;
388 1.1 tsubai PM_SET_STATE_ACKON();
389 1.1 tsubai if (pm_wait_free(0x40) == 0)
390 1.1 tsubai rval = 0xffffcd35;
391 1.1 tsubai
392 1.1 tsubai PM_SET_STATE_ACKON();
393 1.1 tsubai via_reg(VIA2, vDirA) = 0x00;
394 1.1 tsubai
395 1.1 tsubai return rval;
396 1.1 tsubai #else
397 1.1 tsubai panic("pm_send_pm1");
398 1.1 tsubai #endif
399 1.1 tsubai }
400 1.1 tsubai
401 1.1 tsubai
402 1.1 tsubai /*
403 1.1 tsubai * My PMgrOp routine for the PB1XX series
404 1.1 tsubai */
405 1.1 tsubai int
406 1.1 tsubai pm_pmgrop_pm1(pmdata)
407 1.1 tsubai PMData *pmdata;
408 1.1 tsubai {
409 1.1 tsubai #if 0
410 1.1 tsubai int i;
411 1.1 tsubai int s = 0x81815963;
412 1.1 tsubai u_char via1_vIER, via1_vDirA;
413 1.1 tsubai int rval = 0;
414 1.1 tsubai int num_pm_data = 0;
415 1.1 tsubai u_char pm_cmd;
416 1.1 tsubai u_char pm_data;
417 1.1 tsubai u_char *pm_buf;
418 1.1 tsubai
419 1.1 tsubai /* disable all inetrrupts but PM */
420 1.1 tsubai via1_vIER = via_reg(VIA1, vIER);
421 1.1 tsubai PM_VIA_INTR_DISABLE();
422 1.1 tsubai
423 1.1 tsubai via1_vDirA = via_reg(VIA1, vDirA);
424 1.1 tsubai
425 1.1 tsubai switch (pmdata->command) {
426 1.18 itojun default:
427 1.18 itojun for (i = 0; i < 7; i++) {
428 1.18 itojun via_reg(VIA2, vDirA) = 0x00;
429 1.18 itojun
430 1.18 itojun /* wait until PM is free */
431 1.18 itojun if (pm_wait_free(ADBDelay) == 0) { /* timeout */
432 1.18 itojun via_reg(VIA2, vDirA) = 0x00;
433 1.18 itojun /* restore formar value */
434 1.18 itojun via_reg(VIA1, vDirA) = via1_vDirA;
435 1.18 itojun via_reg(VIA1, vIER) = via1_vIER;
436 1.18 itojun return 0xffffcd38;
437 1.18 itojun }
438 1.1 tsubai
439 1.18 itojun switch (mac68k_machine.machineid) {
440 1.18 itojun case MACH_MACPB160:
441 1.18 itojun case MACH_MACPB165:
442 1.18 itojun case MACH_MACPB165C:
443 1.18 itojun case MACH_MACPB180:
444 1.18 itojun case MACH_MACPB180C:
445 1.18 itojun {
446 1.18 itojun int delay = ADBDelay * 16;
447 1.18 itojun
448 1.18 itojun via_reg(VIA2, vDirA) = 0x00;
449 1.18 itojun while ((via_reg(VIA2, 0x200) == 0x7f) && (delay >= 0))
450 1.18 itojun delay--;
451 1.1 tsubai
452 1.18 itojun if (delay < 0) { /* timeout */
453 1.1 tsubai via_reg(VIA2, vDirA) = 0x00;
454 1.18 itojun /* restore formar value */
455 1.18 itojun via_reg(VIA1, vIER) = via1_vIER;
456 1.18 itojun return 0xffffcd38;
457 1.1 tsubai }
458 1.18 itojun }
459 1.18 itojun } /* end switch */
460 1.1 tsubai
461 1.18 itojun s = splhigh();
462 1.1 tsubai
463 1.18 itojun via1_vDirA = via_reg(VIA1, vDirA);
464 1.18 itojun via_reg(VIA1, vDirA) &= 0x7f;
465 1.1 tsubai
466 1.18 itojun pm_cmd = (u_char)(pmdata->command & 0xff);
467 1.18 itojun if ((rval = pm_send_pm1(pm_cmd, ADBDelay * 8)) == 0)
468 1.18 itojun break; /* send command succeeded */
469 1.1 tsubai
470 1.18 itojun via_reg(VIA1, vDirA) = via1_vDirA;
471 1.18 itojun splx(s);
472 1.18 itojun } /* end for */
473 1.18 itojun
474 1.18 itojun /* failed to send a command */
475 1.18 itojun if (i == 7) {
476 1.18 itojun via_reg(VIA2, vDirA) = 0x00;
477 1.18 itojun /* restore formar value */
478 1.18 itojun via_reg(VIA1, vDirA) = via1_vDirA;
479 1.18 itojun via_reg(VIA1, vIER) = via1_vIER;
480 1.18 itojun if (s != 0x81815963)
481 1.1 tsubai splx(s);
482 1.18 itojun return 0xffffcd38;
483 1.18 itojun }
484 1.1 tsubai
485 1.18 itojun /* send # of PM data */
486 1.18 itojun num_pm_data = pmdata->num_data;
487 1.18 itojun if ((rval = pm_send_pm1((u_char)(num_pm_data & 0xff), ADBDelay * 8)) != 0)
488 1.18 itojun break; /* timeout */
489 1.18 itojun
490 1.18 itojun /* send PM data */
491 1.18 itojun pm_buf = (u_char *)pmdata->s_buf;
492 1.18 itojun for (i = 0; i < num_pm_data; i++)
493 1.18 itojun if ((rval = pm_send_pm1(pm_buf[i], ADBDelay * 8)) != 0)
494 1.18 itojun break; /* timeout */
495 1.18 itojun if ((i != num_pm_data) && (num_pm_data != 0))
496 1.18 itojun break; /* timeout */
497 1.1 tsubai
498 1.18 itojun /* Will PM IC return data? */
499 1.18 itojun if ((pm_cmd & 0x08) == 0) {
500 1.18 itojun rval = 0;
501 1.18 itojun break; /* no returned data */
502 1.18 itojun }
503 1.1 tsubai
504 1.18 itojun rval = 0xffffcd37;
505 1.18 itojun if (pm_wait_busy(ADBDelay) != 0)
506 1.18 itojun break; /* timeout */
507 1.1 tsubai
508 1.18 itojun /* receive PM command */
509 1.18 itojun if ((rval = pm_receive_pm1(&pm_data)) != 0)
510 1.18 itojun break;
511 1.1 tsubai
512 1.18 itojun pmdata->command = pm_data;
513 1.1 tsubai
514 1.18 itojun /* receive number of PM data */
515 1.18 itojun if ((rval = pm_receive_pm1(&pm_data)) != 0)
516 1.18 itojun break; /* timeout */
517 1.18 itojun num_pm_data = pm_data;
518 1.18 itojun pmdata->num_data = num_pm_data;
519 1.1 tsubai
520 1.18 itojun /* receive PM data */
521 1.18 itojun pm_buf = (u_char *)pmdata->r_buf;
522 1.18 itojun for (i = 0; i < num_pm_data; i++) {
523 1.1 tsubai if ((rval = pm_receive_pm1(&pm_data)) != 0)
524 1.18 itojun break; /* timeout */
525 1.18 itojun pm_buf[i] = pm_data;
526 1.18 itojun }
527 1.1 tsubai
528 1.18 itojun rval = 0;
529 1.1 tsubai }
530 1.1 tsubai
531 1.1 tsubai via_reg(VIA2, vDirA) = 0x00;
532 1.1 tsubai
533 1.1 tsubai /* restore formar value */
534 1.1 tsubai via_reg(VIA1, vDirA) = via1_vDirA;
535 1.1 tsubai via_reg(VIA1, vIER) = via1_vIER;
536 1.1 tsubai if (s != 0x81815963)
537 1.1 tsubai splx(s);
538 1.1 tsubai
539 1.1 tsubai return rval;
540 1.1 tsubai #else
541 1.1 tsubai panic("pm_pmgrop_pm1");
542 1.1 tsubai #endif
543 1.1 tsubai }
544 1.1 tsubai
545 1.1 tsubai
546 1.1 tsubai /*
547 1.1 tsubai * My PM interrupt routine for PB1XX series
548 1.1 tsubai */
549 1.1 tsubai void
550 1.1 tsubai pm_intr_pm1()
551 1.1 tsubai {
552 1.1 tsubai #if 0
553 1.1 tsubai int s;
554 1.1 tsubai int rval;
555 1.1 tsubai PMData pmdata;
556 1.1 tsubai
557 1.1 tsubai s = splhigh();
558 1.1 tsubai
559 1.1 tsubai PM_VIA_CLR_INTR(); /* clear VIA1 interrupt */
560 1.1 tsubai
561 1.1 tsubai /* ask PM what happend */
562 1.1 tsubai pmdata.command = 0x78;
563 1.1 tsubai pmdata.num_data = 0;
564 1.1 tsubai pmdata.data[0] = pmdata.data[1] = 0;
565 1.1 tsubai pmdata.s_buf = &pmdata.data[2];
566 1.1 tsubai pmdata.r_buf = &pmdata.data[2];
567 1.1 tsubai rval = pm_pmgrop_pm1(&pmdata);
568 1.1 tsubai if (rval != 0) {
569 1.1 tsubai #ifdef ADB_DEBUG
570 1.1 tsubai if (adb_debug)
571 1.1 tsubai printf("pm: PM is not ready. error code=%08x\n", rval);
572 1.1 tsubai #endif
573 1.1 tsubai splx(s);
574 1.1 tsubai }
575 1.1 tsubai
576 1.1 tsubai if ((pmdata.data[2] & 0x10) == 0x10) {
577 1.1 tsubai if ((pmdata.data[2] & 0x0f) == 0) {
578 1.1 tsubai /* ADB data that were requested by TALK command */
579 1.1 tsubai pm_adb_get_TALK_result(&pmdata);
580 1.1 tsubai } else if ((pmdata.data[2] & 0x08) == 0x8) {
581 1.1 tsubai /* PM is requesting to poll */
582 1.1 tsubai pm_adb_poll_next_device_pm1(&pmdata);
583 1.1 tsubai } else if ((pmdata.data[2] & 0x04) == 0x4) {
584 1.1 tsubai /* ADB device event */
585 1.1 tsubai pm_adb_get_ADB_data(&pmdata);
586 1.1 tsubai }
587 1.1 tsubai } else {
588 1.1 tsubai #ifdef ADB_DEBUG
589 1.1 tsubai if (adb_debug)
590 1.1 tsubai pm_printerr("driver does not supported this event.",
591 1.1 tsubai rval, pmdata.num_data, pmdata.data);
592 1.1 tsubai #endif
593 1.1 tsubai }
594 1.1 tsubai
595 1.1 tsubai splx(s);
596 1.1 tsubai #else
597 1.1 tsubai panic("pm_intr_pm1");
598 1.1 tsubai #endif
599 1.1 tsubai }
600 1.1 tsubai
601 1.1 tsubai
602 1.1 tsubai
603 1.1 tsubai /*
604 1.1 tsubai * Functions for the PB Duo series and the PB 5XX series
605 1.1 tsubai */
606 1.1 tsubai
607 1.1 tsubai /*
608 1.1 tsubai * Receive data from PM for the PB Duo series and the PB 5XX series
609 1.1 tsubai */
610 1.1 tsubai int
611 1.1 tsubai pm_receive_pm2(data)
612 1.1 tsubai u_char *data;
613 1.1 tsubai {
614 1.1 tsubai int i;
615 1.1 tsubai int rval;
616 1.1 tsubai
617 1.1 tsubai rval = 0xffffcd34;
618 1.1 tsubai
619 1.1 tsubai switch (1) {
620 1.18 itojun default:
621 1.18 itojun /* set VIA SR to input mode */
622 1.18 itojun via_reg_or(VIA1, vACR, 0x0c);
623 1.18 itojun via_reg_and(VIA1, vACR, ~0x10);
624 1.18 itojun i = PM_SR();
625 1.18 itojun
626 1.18 itojun PM_SET_STATE_ACKOFF();
627 1.18 itojun if (pm_wait_busy((int)ADBDelay*32) != 0)
628 1.18 itojun break; /* timeout */
629 1.1 tsubai
630 1.18 itojun PM_SET_STATE_ACKON();
631 1.18 itojun rval = 0xffffcd33;
632 1.18 itojun if (pm_wait_free((int)ADBDelay*32) == 0)
633 1.18 itojun break; /* timeout */
634 1.1 tsubai
635 1.18 itojun *data = PM_SR();
636 1.18 itojun rval = 0;
637 1.1 tsubai
638 1.18 itojun break;
639 1.1 tsubai }
640 1.1 tsubai
641 1.1 tsubai PM_SET_STATE_ACKON();
642 1.1 tsubai via_reg_or(VIA1, vACR, 0x1c);
643 1.1 tsubai
644 1.1 tsubai return rval;
645 1.1 tsubai }
646 1.1 tsubai
647 1.1 tsubai
648 1.1 tsubai
649 1.1 tsubai /*
650 1.1 tsubai * Send data to PM for the PB Duo series and the PB 5XX series
651 1.1 tsubai */
652 1.1 tsubai int
653 1.1 tsubai pm_send_pm2(data)
654 1.1 tsubai u_char data;
655 1.1 tsubai {
656 1.1 tsubai int rval;
657 1.1 tsubai
658 1.1 tsubai via_reg_or(VIA1, vACR, 0x1c);
659 1.1 tsubai write_via_reg(VIA1, vSR, data); /* PM_SR() = data; */
660 1.1 tsubai
661 1.1 tsubai PM_SET_STATE_ACKOFF();
662 1.1 tsubai rval = 0xffffcd36;
663 1.1 tsubai if (pm_wait_busy((int)ADBDelay*32) != 0) {
664 1.1 tsubai PM_SET_STATE_ACKON();
665 1.1 tsubai
666 1.1 tsubai via_reg_or(VIA1, vACR, 0x1c);
667 1.1 tsubai
668 1.1 tsubai return rval;
669 1.1 tsubai }
670 1.1 tsubai
671 1.1 tsubai PM_SET_STATE_ACKON();
672 1.1 tsubai rval = 0xffffcd35;
673 1.1 tsubai if (pm_wait_free((int)ADBDelay*32) != 0)
674 1.1 tsubai rval = 0;
675 1.1 tsubai
676 1.1 tsubai PM_SET_STATE_ACKON();
677 1.1 tsubai via_reg_or(VIA1, vACR, 0x1c);
678 1.1 tsubai
679 1.1 tsubai return rval;
680 1.1 tsubai }
681 1.1 tsubai
682 1.1 tsubai
683 1.1 tsubai
684 1.1 tsubai /*
685 1.1 tsubai * My PMgrOp routine for the PB Duo series and the PB 5XX series
686 1.1 tsubai */
687 1.1 tsubai int
688 1.1 tsubai pm_pmgrop_pm2(pmdata)
689 1.1 tsubai PMData *pmdata;
690 1.1 tsubai {
691 1.1 tsubai int i;
692 1.1 tsubai int s;
693 1.1 tsubai u_char via1_vIER;
694 1.1 tsubai int rval = 0;
695 1.1 tsubai int num_pm_data = 0;
696 1.1 tsubai u_char pm_cmd;
697 1.1 tsubai short pm_num_rx_data;
698 1.1 tsubai u_char pm_data;
699 1.1 tsubai u_char *pm_buf;
700 1.1 tsubai
701 1.1 tsubai s = splhigh();
702 1.1 tsubai
703 1.1 tsubai /* disable all inetrrupts but PM */
704 1.1 tsubai via1_vIER = 0x10;
705 1.1 tsubai via1_vIER &= read_via_reg(VIA1, vIER);
706 1.1 tsubai write_via_reg(VIA1, vIER, via1_vIER);
707 1.1 tsubai if (via1_vIER != 0x0)
708 1.1 tsubai via1_vIER |= 0x80;
709 1.1 tsubai
710 1.1 tsubai switch (pmdata->command) {
711 1.18 itojun default:
712 1.18 itojun /* wait until PM is free */
713 1.18 itojun pm_cmd = (u_char)(pmdata->command & 0xff);
714 1.18 itojun rval = 0xcd38;
715 1.18 itojun if (pm_wait_free(ADBDelay * 4) == 0)
716 1.18 itojun break; /* timeout */
717 1.18 itojun
718 1.18 itojun if (HwCfgFlags3 & 0x00200000) {
719 1.18 itojun /* PB 160, PB 165(c), PB 180(c)? */
720 1.18 itojun int delay = ADBDelay * 16;
721 1.18 itojun
722 1.18 itojun write_via_reg(VIA2, vDirA, 0x00);
723 1.18 itojun while ((read_via_reg(VIA2, 0x200) == 0x07) &&
724 1.18 itojun (delay >= 0))
725 1.18 itojun delay--;
726 1.1 tsubai
727 1.18 itojun if (delay < 0) {
728 1.18 itojun rval = 0xffffcd38;
729 1.18 itojun break; /* timeout */
730 1.18 itojun }
731 1.18 itojun }
732 1.1 tsubai
733 1.18 itojun /* send PM command */
734 1.18 itojun if ((rval = pm_send_pm2((u_char)(pm_cmd & 0xff))))
735 1.18 itojun break; /* timeout */
736 1.18 itojun
737 1.18 itojun /* send number of PM data */
738 1.18 itojun num_pm_data = pmdata->num_data;
739 1.18 itojun if (HwCfgFlags3 & 0x00020000) { /* PB Duo, PB 5XX */
740 1.18 itojun if (pm_send_cmd_type[pm_cmd] < 0) {
741 1.18 itojun if ((rval = pm_send_pm2((u_char)(num_pm_data & 0xff))) != 0)
742 1.1 tsubai break; /* timeout */
743 1.18 itojun pmdata->command = 0;
744 1.1 tsubai }
745 1.18 itojun } else { /* PB 1XX series ? */
746 1.18 itojun if ((rval = pm_send_pm2((u_char)(num_pm_data & 0xff))) != 0)
747 1.18 itojun break; /* timeout */
748 1.18 itojun }
749 1.18 itojun /* send PM data */
750 1.18 itojun pm_buf = (u_char *)pmdata->s_buf;
751 1.18 itojun for (i = 0 ; i < num_pm_data; i++)
752 1.18 itojun if ((rval = pm_send_pm2(pm_buf[i])) != 0)
753 1.18 itojun break; /* timeout */
754 1.18 itojun if (i != num_pm_data)
755 1.18 itojun break; /* timeout */
756 1.1 tsubai
757 1.1 tsubai
758 1.18 itojun /* check if PM will send me data */
759 1.18 itojun pm_num_rx_data = pm_receive_cmd_type[pm_cmd];
760 1.18 itojun pmdata->num_data = pm_num_rx_data;
761 1.18 itojun if (pm_num_rx_data == 0) {
762 1.18 itojun rval = 0;
763 1.18 itojun break; /* no return data */
764 1.18 itojun }
765 1.18 itojun
766 1.18 itojun /* receive PM command */
767 1.18 itojun pm_data = pmdata->command;
768 1.18 itojun if (HwCfgFlags3 & 0x00020000) { /* PB Duo, PB 5XX */
769 1.18 itojun pm_num_rx_data--;
770 1.18 itojun if (pm_num_rx_data == 0)
771 1.1 tsubai if ((rval = pm_receive_pm2(&pm_data)) != 0) {
772 1.1 tsubai rval = 0xffffcd37;
773 1.1 tsubai break;
774 1.1 tsubai }
775 1.18 itojun pmdata->command = pm_data;
776 1.18 itojun } else { /* PB 1XX series ? */
777 1.18 itojun if ((rval = pm_receive_pm2(&pm_data)) != 0) {
778 1.18 itojun rval = 0xffffcd37;
779 1.18 itojun break;
780 1.1 tsubai }
781 1.18 itojun pmdata->command = pm_data;
782 1.18 itojun }
783 1.1 tsubai
784 1.18 itojun /* receive number of PM data */
785 1.18 itojun if (HwCfgFlags3 & 0x00020000) { /* PB Duo, PB 5XX */
786 1.18 itojun if (pm_num_rx_data < 0) {
787 1.1 tsubai if ((rval = pm_receive_pm2(&pm_data)) != 0)
788 1.18 itojun break; /* timeout */
789 1.1 tsubai num_pm_data = pm_data;
790 1.18 itojun } else
791 1.18 itojun num_pm_data = pm_num_rx_data;
792 1.18 itojun pmdata->num_data = num_pm_data;
793 1.18 itojun } else { /* PB 1XX serias ? */
794 1.18 itojun if ((rval = pm_receive_pm2(&pm_data)) != 0)
795 1.18 itojun break; /* timeout */
796 1.18 itojun num_pm_data = pm_data;
797 1.18 itojun pmdata->num_data = num_pm_data;
798 1.18 itojun }
799 1.1 tsubai
800 1.18 itojun /* receive PM data */
801 1.18 itojun pm_buf = (u_char *)pmdata->r_buf;
802 1.18 itojun for (i = 0; i < num_pm_data; i++) {
803 1.18 itojun if ((rval = pm_receive_pm2(&pm_data)) != 0)
804 1.18 itojun break; /* timeout */
805 1.18 itojun pm_buf[i] = pm_data;
806 1.18 itojun }
807 1.1 tsubai
808 1.18 itojun rval = 0;
809 1.1 tsubai }
810 1.1 tsubai
811 1.1 tsubai /* restore former value */
812 1.1 tsubai write_via_reg(VIA1, vIER, via1_vIER);
813 1.1 tsubai splx(s);
814 1.1 tsubai
815 1.1 tsubai return rval;
816 1.1 tsubai }
817 1.1 tsubai
818 1.1 tsubai
819 1.1 tsubai /*
820 1.1 tsubai * My PM interrupt routine for the PB Duo series and the PB 5XX series
821 1.1 tsubai */
822 1.1 tsubai void
823 1.1 tsubai pm_intr_pm2()
824 1.1 tsubai {
825 1.1 tsubai int s;
826 1.1 tsubai int rval;
827 1.1 tsubai PMData pmdata;
828 1.1 tsubai
829 1.1 tsubai s = splhigh();
830 1.1 tsubai
831 1.1 tsubai PM_VIA_CLR_INTR(); /* clear VIA1 interrupt */
832 1.1 tsubai /* ask PM what happend */
833 1.1 tsubai pmdata.command = 0x78;
834 1.1 tsubai pmdata.num_data = 0;
835 1.1 tsubai pmdata.s_buf = &pmdata.data[2];
836 1.1 tsubai pmdata.r_buf = &pmdata.data[2];
837 1.1 tsubai rval = pm_pmgrop_pm2(&pmdata);
838 1.1 tsubai if (rval != 0) {
839 1.1 tsubai #ifdef ADB_DEBUG
840 1.1 tsubai if (adb_debug)
841 1.1 tsubai printf("pm: PM is not ready. error code: %08x\n", rval);
842 1.1 tsubai #endif
843 1.1 tsubai splx(s);
844 1.1 tsubai }
845 1.1 tsubai
846 1.1 tsubai switch ((u_int)(pmdata.data[2] & 0xff)) {
847 1.18 itojun case 0x00: /* 1 sec interrupt? */
848 1.18 itojun break;
849 1.18 itojun case 0x80: /* 1 sec interrupt? */
850 1.18 itojun pm_counter++;
851 1.18 itojun break;
852 1.18 itojun case 0x08: /* Brightness/Contrast button on LCD panel */
853 1.18 itojun /* get brightness and contrast of the LCD */
854 1.18 itojun pm_LCD_brightness = (u_int)pmdata.data[3] & 0xff;
855 1.18 itojun pm_LCD_contrast = (u_int)pmdata.data[4] & 0xff;
856 1.18 itojun /*
857 1.18 itojun pm_printerr("#08", rval, pmdata.num_data, pmdata.data);
858 1.18 itojun pmdata.command = 0x33;
859 1.18 itojun pmdata.num_data = 1;
860 1.18 itojun pmdata.s_buf = pmdata.data;
861 1.18 itojun pmdata.r_buf = pmdata.data;
862 1.18 itojun pmdata.data[0] = pm_LCD_contrast;
863 1.18 itojun rval = pm_pmgrop_pm2(&pmdata);
864 1.18 itojun pm_printerr("#33", rval, pmdata.num_data, pmdata.data);
865 1.1 tsubai */
866 1.18 itojun /* this is an experimental code */
867 1.18 itojun pmdata.command = 0x41;
868 1.18 itojun pmdata.num_data = 1;
869 1.18 itojun pmdata.s_buf = pmdata.data;
870 1.18 itojun pmdata.r_buf = pmdata.data;
871 1.18 itojun pm_LCD_brightness = 0x7f - pm_LCD_brightness / 2;
872 1.18 itojun if (pm_LCD_brightness < 0x08)
873 1.18 itojun pm_LCD_brightness = 0x08;
874 1.18 itojun if (pm_LCD_brightness > 0x78)
875 1.18 itojun pm_LCD_brightness = 0x78;
876 1.18 itojun pmdata.data[0] = pm_LCD_brightness;
877 1.18 itojun rval = pm_pmgrop_pm2(&pmdata);
878 1.18 itojun break;
879 1.18 itojun case 0x10: /* ADB data that were requested by TALK command */
880 1.18 itojun case 0x14:
881 1.18 itojun pm_adb_get_TALK_result(&pmdata);
882 1.18 itojun break;
883 1.18 itojun case 0x16: /* ADB device event */
884 1.18 itojun case 0x18:
885 1.18 itojun case 0x1e:
886 1.18 itojun pm_adb_get_ADB_data(&pmdata);
887 1.18 itojun break;
888 1.18 itojun default:
889 1.1 tsubai #ifdef ADB_DEBUG
890 1.18 itojun if (adb_debug)
891 1.18 itojun pm_printerr("driver does not supported this event.",
892 1.18 itojun pmdata.data[2], pmdata.num_data,
893 1.18 itojun pmdata.data);
894 1.1 tsubai #endif
895 1.18 itojun break;
896 1.1 tsubai }
897 1.1 tsubai
898 1.1 tsubai splx(s);
899 1.1 tsubai }
900 1.1 tsubai
901 1.1 tsubai
902 1.1 tsubai /*
903 1.1 tsubai * My PMgrOp routine
904 1.1 tsubai */
905 1.1 tsubai int
906 1.1 tsubai pmgrop(pmdata)
907 1.1 tsubai PMData *pmdata;
908 1.1 tsubai {
909 1.1 tsubai switch (pmHardware) {
910 1.18 itojun case PM_HW_PB1XX:
911 1.18 itojun return (pm_pmgrop_pm1(pmdata));
912 1.18 itojun break;
913 1.18 itojun case PM_HW_PB5XX:
914 1.18 itojun return (pm_pmgrop_pm2(pmdata));
915 1.18 itojun break;
916 1.18 itojun default:
917 1.18 itojun /* return (pmgrop_mrg(pmdata)); */
918 1.18 itojun return 1;
919 1.1 tsubai }
920 1.1 tsubai }
921 1.1 tsubai
922 1.1 tsubai
923 1.1 tsubai /*
924 1.1 tsubai * My PM interrupt routine
925 1.1 tsubai */
926 1.1 tsubai void
927 1.1 tsubai pm_intr()
928 1.1 tsubai {
929 1.1 tsubai switch (pmHardware) {
930 1.18 itojun case PM_HW_PB1XX:
931 1.18 itojun pm_intr_pm1();
932 1.18 itojun break;
933 1.18 itojun case PM_HW_PB5XX:
934 1.18 itojun pm_intr_pm2();
935 1.18 itojun break;
936 1.18 itojun default:
937 1.18 itojun break;
938 1.1 tsubai }
939 1.1 tsubai }
940 1.1 tsubai
941 1.1 tsubai
942 1.1 tsubai
943 1.1 tsubai /*
944 1.1 tsubai * Synchronous ADBOp routine for the Power Manager
945 1.1 tsubai */
946 1.1 tsubai int
947 1.1 tsubai pm_adb_op(buffer, compRout, data, command)
948 1.1 tsubai u_char *buffer;
949 1.1 tsubai void *compRout;
950 1.1 tsubai void *data;
951 1.1 tsubai int command;
952 1.1 tsubai {
953 1.1 tsubai int i;
954 1.1 tsubai int s;
955 1.1 tsubai int rval;
956 1.12 tsubai int timo;
957 1.1 tsubai PMData pmdata;
958 1.1 tsubai struct adbCommand packet;
959 1.1 tsubai
960 1.1 tsubai if (adbWaiting == 1)
961 1.1 tsubai return 1;
962 1.1 tsubai
963 1.1 tsubai s = splhigh();
964 1.1 tsubai write_via_reg(VIA1, vIER, 0x10);
965 1.1 tsubai
966 1.1 tsubai adbBuffer = buffer;
967 1.1 tsubai adbCompRout = compRout;
968 1.1 tsubai adbCompData = data;
969 1.1 tsubai
970 1.1 tsubai pmdata.command = 0x20;
971 1.1 tsubai pmdata.s_buf = pmdata.data;
972 1.1 tsubai pmdata.r_buf = pmdata.data;
973 1.1 tsubai
974 1.1 tsubai /* if the command is LISTEN, add number of ADB data to number of PM data */
975 1.1 tsubai if ((command & 0xc) == 0x8) {
976 1.1 tsubai if (buffer != (u_char *)0)
977 1.1 tsubai pmdata.num_data = buffer[0] + 3;
978 1.1 tsubai } else {
979 1.1 tsubai pmdata.num_data = 3;
980 1.1 tsubai }
981 1.1 tsubai
982 1.1 tsubai pmdata.data[0] = (u_char)(command & 0xff);
983 1.1 tsubai pmdata.data[1] = 0;
984 1.1 tsubai if ((command & 0xc) == 0x8) { /* if the command is LISTEN, copy ADB data to PM buffer */
985 1.1 tsubai if ((buffer != (u_char *)0) && (buffer[0] <= 24)) {
986 1.1 tsubai pmdata.data[2] = buffer[0]; /* number of data */
987 1.1 tsubai for (i = 0; i < buffer[0]; i++)
988 1.1 tsubai pmdata.data[3 + i] = buffer[1 + i];
989 1.1 tsubai } else
990 1.1 tsubai pmdata.data[2] = 0;
991 1.1 tsubai } else
992 1.1 tsubai pmdata.data[2] = 0;
993 1.1 tsubai
994 1.1 tsubai if ((command & 0xc) != 0xc) { /* if the command is not TALK */
995 1.1 tsubai /* set up stuff for adb_pass_up */
996 1.1 tsubai packet.data[0] = 1 + pmdata.data[2];
997 1.1 tsubai packet.data[1] = command;
998 1.1 tsubai for (i = 0; i < pmdata.data[2]; i++)
999 1.1 tsubai packet.data[i+2] = pmdata.data[i+3];
1000 1.1 tsubai packet.saveBuf = adbBuffer;
1001 1.1 tsubai packet.compRout = adbCompRout;
1002 1.1 tsubai packet.compData = adbCompData;
1003 1.1 tsubai packet.cmd = command;
1004 1.1 tsubai packet.unsol = 0;
1005 1.1 tsubai packet.ack_only = 1;
1006 1.1 tsubai adb_polling = 1;
1007 1.1 tsubai adb_pass_up(&packet);
1008 1.1 tsubai adb_polling = 0;
1009 1.1 tsubai }
1010 1.1 tsubai
1011 1.1 tsubai rval = pmgrop(&pmdata);
1012 1.9 tsubai if (rval != 0) {
1013 1.9 tsubai splx(s);
1014 1.1 tsubai return 1;
1015 1.9 tsubai }
1016 1.1 tsubai
1017 1.12 tsubai delay(10000);
1018 1.12 tsubai
1019 1.1 tsubai adbWaiting = 1;
1020 1.1 tsubai adbWaitingCmd = command;
1021 1.1 tsubai
1022 1.1 tsubai PM_VIA_INTR_ENABLE();
1023 1.1 tsubai
1024 1.16 wiz /* wait until the PM interrupt has occurred */
1025 1.12 tsubai timo = 0x80000;
1026 1.1 tsubai while (adbWaiting == 1) {
1027 1.1 tsubai if (read_via_reg(VIA1, vIFR) & 0x14)
1028 1.1 tsubai pm_intr();
1029 1.1 tsubai #ifdef PM_GRAB_SI
1030 1.1 tsubai #if 0
1031 1.1 tsubai zshard(0); /* grab any serial interrupts */
1032 1.1 tsubai #else
1033 1.1 tsubai (void)intr_dispatch(0x70);
1034 1.1 tsubai #endif
1035 1.1 tsubai #endif
1036 1.12 tsubai if ((--timo) < 0) {
1037 1.17 dbj /* Try to take an interrupt anyway, just in case.
1038 1.17 dbj * This has been observed to happen on my ibook
1039 1.17 dbj * when i press a key after boot and before adb
1040 1.17 dbj * is attached; For example, when booting with -d.
1041 1.17 dbj */
1042 1.17 dbj pm_intr();
1043 1.17 dbj if (adbWaiting) {
1044 1.17 dbj printf("pm_adb_op: timeout. command = 0x%x\n",command);
1045 1.17 dbj splx(s);
1046 1.17 dbj return 1;
1047 1.17 dbj }
1048 1.17 dbj #ifdef ADB_DEBUG
1049 1.17 dbj else {
1050 1.17 dbj printf("pm_adb_op: missed interrupt. cmd=0x%x\n",command);
1051 1.17 dbj }
1052 1.17 dbj #endif
1053 1.9 tsubai }
1054 1.1 tsubai }
1055 1.1 tsubai
1056 1.1 tsubai /* this command enables the interrupt by operating ADB devices */
1057 1.1 tsubai if (HwCfgFlags3 & 0x00020000) { /* PB Duo series, PB 5XX series */
1058 1.1 tsubai pmdata.command = 0x20;
1059 1.1 tsubai pmdata.num_data = 4;
1060 1.1 tsubai pmdata.s_buf = pmdata.data;
1061 1.1 tsubai pmdata.r_buf = pmdata.data;
1062 1.1 tsubai pmdata.data[0] = 0x00;
1063 1.1 tsubai pmdata.data[1] = 0x86; /* magic spell for awaking the PM */
1064 1.1 tsubai pmdata.data[2] = 0x00;
1065 1.1 tsubai pmdata.data[3] = 0x0c; /* each bit may express the existent ADB device */
1066 1.1 tsubai } else { /* PB 1XX series */
1067 1.1 tsubai pmdata.command = 0x20;
1068 1.1 tsubai pmdata.num_data = 3;
1069 1.1 tsubai pmdata.s_buf = pmdata.data;
1070 1.1 tsubai pmdata.r_buf = pmdata.data;
1071 1.1 tsubai pmdata.data[0] = (u_char)(command & 0xf0) | 0xc;
1072 1.1 tsubai pmdata.data[1] = 0x04;
1073 1.1 tsubai pmdata.data[2] = 0x00;
1074 1.1 tsubai }
1075 1.1 tsubai rval = pmgrop(&pmdata);
1076 1.1 tsubai
1077 1.1 tsubai splx(s);
1078 1.1 tsubai return rval;
1079 1.1 tsubai }
1080 1.1 tsubai
1081 1.1 tsubai
1082 1.1 tsubai void
1083 1.1 tsubai pm_adb_get_TALK_result(pmdata)
1084 1.1 tsubai PMData *pmdata;
1085 1.1 tsubai {
1086 1.1 tsubai int i;
1087 1.1 tsubai struct adbCommand packet;
1088 1.1 tsubai
1089 1.1 tsubai /* set up data for adb_pass_up */
1090 1.1 tsubai packet.data[0] = pmdata->num_data-1;
1091 1.1 tsubai packet.data[1] = pmdata->data[3];
1092 1.1 tsubai for (i = 0; i <packet.data[0]-1; i++)
1093 1.1 tsubai packet.data[i+2] = pmdata->data[i+4];
1094 1.1 tsubai
1095 1.1 tsubai packet.saveBuf = adbBuffer;
1096 1.1 tsubai packet.compRout = adbCompRout;
1097 1.1 tsubai packet.compData = adbCompData;
1098 1.1 tsubai packet.unsol = 0;
1099 1.1 tsubai packet.ack_only = 0;
1100 1.1 tsubai adb_polling = 1;
1101 1.1 tsubai adb_pass_up(&packet);
1102 1.1 tsubai adb_polling = 0;
1103 1.1 tsubai
1104 1.1 tsubai adbWaiting = 0;
1105 1.1 tsubai adbBuffer = (long)0;
1106 1.1 tsubai adbCompRout = (long)0;
1107 1.1 tsubai adbCompData = (long)0;
1108 1.1 tsubai }
1109 1.1 tsubai
1110 1.1 tsubai
1111 1.1 tsubai void
1112 1.1 tsubai pm_adb_get_ADB_data(pmdata)
1113 1.1 tsubai PMData *pmdata;
1114 1.1 tsubai {
1115 1.1 tsubai int i;
1116 1.1 tsubai struct adbCommand packet;
1117 1.1 tsubai
1118 1.1 tsubai /* set up data for adb_pass_up */
1119 1.1 tsubai packet.data[0] = pmdata->num_data-1; /* number of raw data */
1120 1.1 tsubai packet.data[1] = pmdata->data[3]; /* ADB command */
1121 1.1 tsubai for (i = 0; i <packet.data[0]-1; i++)
1122 1.1 tsubai packet.data[i+2] = pmdata->data[i+4];
1123 1.1 tsubai packet.unsol = 1;
1124 1.1 tsubai packet.ack_only = 0;
1125 1.1 tsubai adb_pass_up(&packet);
1126 1.1 tsubai }
1127 1.1 tsubai
1128 1.1 tsubai
1129 1.1 tsubai void
1130 1.1 tsubai pm_adb_poll_next_device_pm1(pmdata)
1131 1.1 tsubai PMData *pmdata;
1132 1.1 tsubai {
1133 1.1 tsubai int i;
1134 1.1 tsubai int ndid;
1135 1.1 tsubai u_short bendid = 0x1;
1136 1.1 tsubai int rval;
1137 1.1 tsubai PMData tmp_pmdata;
1138 1.1 tsubai
1139 1.1 tsubai /* find another existent ADB device to poll */
1140 1.1 tsubai for (i = 1; i < 16; i++) {
1141 1.9 tsubai ndid = (ADB_CMDADDR(pmdata->data[3]) + i) & 0xf;
1142 1.1 tsubai bendid <<= ndid;
1143 1.1 tsubai if ((pm_existent_ADB_devices & bendid) != 0)
1144 1.1 tsubai break;
1145 1.1 tsubai }
1146 1.1 tsubai
1147 1.1 tsubai /* poll the other device */
1148 1.1 tsubai tmp_pmdata.command = 0x20;
1149 1.1 tsubai tmp_pmdata.num_data = 3;
1150 1.1 tsubai tmp_pmdata.s_buf = tmp_pmdata.data;
1151 1.1 tsubai tmp_pmdata.r_buf = tmp_pmdata.data;
1152 1.1 tsubai tmp_pmdata.data[0] = (u_char)(ndid << 4) | 0xc;
1153 1.1 tsubai tmp_pmdata.data[1] = 0x04; /* magic spell for awaking the PM */
1154 1.1 tsubai tmp_pmdata.data[2] = 0x00;
1155 1.1 tsubai rval = pmgrop(&tmp_pmdata);
1156 1.1 tsubai }
1157 1.1 tsubai
1158 1.1 tsubai void
1159 1.1 tsubai pm_adb_restart()
1160 1.1 tsubai {
1161 1.1 tsubai PMData p;
1162 1.1 tsubai
1163 1.4 tsubai p.command = PMU_RESET_CPU;
1164 1.1 tsubai p.num_data = 0;
1165 1.1 tsubai p.s_buf = p.data;
1166 1.1 tsubai p.r_buf = p.data;
1167 1.6 tsubai pmgrop(&p);
1168 1.6 tsubai }
1169 1.6 tsubai
1170 1.6 tsubai void
1171 1.6 tsubai pm_adb_poweroff()
1172 1.6 tsubai {
1173 1.6 tsubai PMData p;
1174 1.6 tsubai
1175 1.6 tsubai p.command = PMU_POWER_OFF;
1176 1.6 tsubai p.num_data = 4;
1177 1.6 tsubai p.s_buf = p.data;
1178 1.6 tsubai p.r_buf = p.data;
1179 1.6 tsubai strcpy(p.data, "MATT");
1180 1.1 tsubai pmgrop(&p);
1181 1.2 tsubai }
1182 1.2 tsubai
1183 1.2 tsubai void
1184 1.2 tsubai pm_read_date_time(time)
1185 1.2 tsubai u_long *time;
1186 1.2 tsubai {
1187 1.2 tsubai PMData p;
1188 1.2 tsubai
1189 1.4 tsubai p.command = PMU_READ_RTC;
1190 1.2 tsubai p.num_data = 0;
1191 1.2 tsubai p.s_buf = p.data;
1192 1.2 tsubai p.r_buf = p.data;
1193 1.2 tsubai pmgrop(&p);
1194 1.2 tsubai
1195 1.13 wiz memcpy(time, p.data, 4);
1196 1.4 tsubai }
1197 1.4 tsubai
1198 1.4 tsubai void
1199 1.4 tsubai pm_set_date_time(time)
1200 1.4 tsubai u_long time;
1201 1.4 tsubai {
1202 1.4 tsubai PMData p;
1203 1.4 tsubai
1204 1.4 tsubai p.command = PMU_SET_RTC;
1205 1.4 tsubai p.num_data = 4;
1206 1.4 tsubai p.s_buf = p.r_buf = p.data;
1207 1.13 wiz memcpy(p.data, &time, 4);
1208 1.7 tsubai pmgrop(&p);
1209 1.7 tsubai }
1210 1.7 tsubai
1211 1.7 tsubai int
1212 1.7 tsubai pm_read_brightness()
1213 1.7 tsubai {
1214 1.7 tsubai PMData p;
1215 1.7 tsubai
1216 1.7 tsubai p.command = PMU_READ_BRIGHTNESS;
1217 1.7 tsubai p.num_data = 1; /* XXX why 1? */
1218 1.7 tsubai p.s_buf = p.r_buf = p.data;
1219 1.7 tsubai p.data[0] = 0;
1220 1.7 tsubai pmgrop(&p);
1221 1.7 tsubai
1222 1.7 tsubai return p.data[0];
1223 1.7 tsubai }
1224 1.7 tsubai
1225 1.7 tsubai void
1226 1.7 tsubai pm_set_brightness(val)
1227 1.7 tsubai int val;
1228 1.7 tsubai {
1229 1.7 tsubai PMData p;
1230 1.7 tsubai
1231 1.7 tsubai val = 0x7f - val / 2;
1232 1.7 tsubai if (val < 0x08)
1233 1.7 tsubai val = 0x08;
1234 1.7 tsubai if (val > 0x78)
1235 1.7 tsubai val = 0x78;
1236 1.7 tsubai
1237 1.7 tsubai p.command = PMU_SET_BRIGHTNESS;
1238 1.7 tsubai p.num_data = 1;
1239 1.7 tsubai p.s_buf = p.r_buf = p.data;
1240 1.7 tsubai p.data[0] = val;
1241 1.7 tsubai pmgrop(&p);
1242 1.7 tsubai }
1243 1.7 tsubai
1244 1.7 tsubai void
1245 1.7 tsubai pm_init_brightness()
1246 1.7 tsubai {
1247 1.7 tsubai int val;
1248 1.7 tsubai
1249 1.7 tsubai val = pm_read_brightness();
1250 1.7 tsubai pm_set_brightness(val);
1251 1.7 tsubai }
1252 1.7 tsubai
1253 1.7 tsubai void
1254 1.7 tsubai pm_eject_pcmcia(slot)
1255 1.7 tsubai int slot;
1256 1.7 tsubai {
1257 1.7 tsubai PMData p;
1258 1.7 tsubai
1259 1.7 tsubai if (slot != 0 && slot != 1)
1260 1.7 tsubai return;
1261 1.7 tsubai
1262 1.7 tsubai p.command = PMU_EJECT_PCMCIA;
1263 1.7 tsubai p.num_data = 1;
1264 1.7 tsubai p.s_buf = p.r_buf = p.data;
1265 1.8 tsubai p.data[0] = 5 + slot; /* XXX */
1266 1.5 tsubai pmgrop(&p);
1267 1.19 itojun }
1268 1.19 itojun
1269 1.19 itojun /*
1270 1.19 itojun * Thanks to Paul Mackerras and Fabio Riccardi's Linux implementation
1271 1.19 itojun * for a clear description of the PMU results.
1272 1.19 itojun */
1273 1.19 itojun int
1274 1.19 itojun pm_battery_info(int battery, struct pmu_battery_info *info)
1275 1.19 itojun {
1276 1.19 itojun PMData p;
1277 1.19 itojun
1278 1.19 itojun p.command = PMU_SMART_BATTERY_STATE;
1279 1.19 itojun p.num_data = 1;
1280 1.19 itojun p.s_buf = p.r_buf = p.data;
1281 1.19 itojun p.data[0] = battery + 1;
1282 1.19 itojun pmgrop(&p);
1283 1.19 itojun
1284 1.19 itojun info->flags = p.data[1];
1285 1.19 itojun
1286 1.19 itojun switch (p.data[0]) {
1287 1.19 itojun case 3:
1288 1.19 itojun case 4:
1289 1.19 itojun info->cur_charge = p.data[2];
1290 1.19 itojun info->max_charge = p.data[3];
1291 1.19 itojun info->draw = *((signed char *)&p.data[4]);
1292 1.19 itojun info->voltage = p.data[5];
1293 1.19 itojun break;
1294 1.19 itojun case 5:
1295 1.19 itojun info->cur_charge = ((p.data[2] << 8) | (p.data[3]));
1296 1.19 itojun info->max_charge = ((p.data[4] << 8) | (p.data[5]));
1297 1.19 itojun info->draw = *((signed short *)&p.data[6]);
1298 1.19 itojun info->voltage = ((p.data[8] << 8) | (p.data[7]));
1299 1.19 itojun break;
1300 1.19 itojun default:
1301 1.19 itojun /* XXX - Error condition */
1302 1.19 itojun info->cur_charge = 0;
1303 1.19 itojun info->max_charge = 0;
1304 1.19 itojun info->draw = 0;
1305 1.19 itojun info->voltage = 0;
1306 1.19 itojun break;
1307 1.19 itojun }
1308 1.19 itojun
1309 1.19 itojun return 1;
1310 1.5 tsubai }
1311 1.5 tsubai
1312 1.5 tsubai int
1313 1.5 tsubai pm_read_nvram(addr)
1314 1.5 tsubai int addr;
1315 1.5 tsubai {
1316 1.5 tsubai PMData p;
1317 1.5 tsubai
1318 1.5 tsubai p.command = PMU_READ_NVRAM;
1319 1.5 tsubai p.num_data = 2;
1320 1.5 tsubai p.s_buf = p.r_buf = p.data;
1321 1.5 tsubai p.data[0] = addr >> 8;
1322 1.5 tsubai p.data[1] = addr;
1323 1.5 tsubai pmgrop(&p);
1324 1.5 tsubai
1325 1.5 tsubai return p.data[0];
1326 1.5 tsubai }
1327 1.5 tsubai
1328 1.5 tsubai void
1329 1.5 tsubai pm_write_nvram(addr, val)
1330 1.5 tsubai int addr, val;
1331 1.5 tsubai {
1332 1.5 tsubai PMData p;
1333 1.5 tsubai
1334 1.5 tsubai p.command = PMU_WRITE_NVRAM;
1335 1.5 tsubai p.num_data = 3;
1336 1.5 tsubai p.s_buf = p.r_buf = p.data;
1337 1.5 tsubai p.data[0] = addr >> 8;
1338 1.5 tsubai p.data[1] = addr;
1339 1.5 tsubai p.data[2] = val;
1340 1.4 tsubai pmgrop(&p);
1341 1.1 tsubai }
1342