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