Home | History | Annotate | Line # | Download | only in dev
pm_direct.c revision 1.13
      1  1.13     wiz /*	$NetBSD: pm_direct.c,v 1.13 2001/07/22 11:29:47 wiz 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 /* this function is MRG-Based (for testing) */
    189   1.1  tsubai int	pm_pmgrop_mrg __P((PMData *));
    190   1.1  tsubai 
    191   1.1  tsubai /* these functions are called from adb_direct.c */
    192   1.1  tsubai void	pm_setup_adb __P((void));
    193   1.1  tsubai void	pm_check_adb_devices __P((int));
    194   1.1  tsubai void	pm_intr __P((void));
    195   1.1  tsubai int	pm_adb_op __P((u_char *, void *, void *, int));
    196   1.1  tsubai 
    197   1.1  tsubai /* these functions also use the variables of adb_direct.c */
    198   1.1  tsubai void	pm_adb_get_TALK_result __P((PMData *));
    199   1.1  tsubai void	pm_adb_get_ADB_data __P((PMData *));
    200   1.1  tsubai void	pm_adb_poll_next_device_pm1 __P((PMData *));
    201   1.1  tsubai 
    202   1.1  tsubai 
    203   1.1  tsubai /*
    204   1.1  tsubai  * These variables are in adb_direct.c.
    205   1.1  tsubai  */
    206   1.1  tsubai extern u_char	*adbBuffer;	/* pointer to user data area */
    207   1.1  tsubai extern void	*adbCompRout;	/* pointer to the completion routine */
    208   1.1  tsubai extern void	*adbCompData;	/* pointer to the completion routine data */
    209   1.1  tsubai extern int	adbWaiting;	/* waiting for return data from the device */
    210   1.1  tsubai extern int	adbWaitingCmd;	/* ADB command we are waiting for */
    211   1.1  tsubai extern int	adbStarting;	/* doing ADB reinit, so do "polling" differently */
    212   1.1  tsubai 
    213   1.1  tsubai #define	ADB_MAX_MSG_LENGTH	16
    214   1.1  tsubai #define	ADB_MAX_HDR_LENGTH	8
    215   1.1  tsubai struct adbCommand {
    216   1.1  tsubai 	u_char	header[ADB_MAX_HDR_LENGTH];	/* not used yet */
    217   1.1  tsubai 	u_char	data[ADB_MAX_MSG_LENGTH];	/* packet data only */
    218   1.1  tsubai 	u_char	*saveBuf;	/* where to save result */
    219   1.1  tsubai 	u_char	*compRout;	/* completion routine pointer */
    220   1.1  tsubai 	u_char	*compData;	/* completion routine data pointer */
    221   1.1  tsubai 	u_int	cmd;		/* the original command for this data */
    222   1.1  tsubai 	u_int	unsol;		/* 1 if packet was unsolicited */
    223   1.1  tsubai 	u_int	ack_only;	/* 1 for no special processing */
    224   1.1  tsubai };
    225   1.1  tsubai extern	void	adb_pass_up __P((struct adbCommand *));
    226   1.1  tsubai 
    227   1.1  tsubai #if 0
    228   1.1  tsubai /*
    229   1.1  tsubai  * Define the external functions
    230   1.1  tsubai  */
    231   1.1  tsubai extern int	zshard __P((int));		/* from zs.c */
    232   1.1  tsubai #endif
    233   1.1  tsubai 
    234   1.1  tsubai #ifdef ADB_DEBUG
    235   1.1  tsubai /*
    236   1.1  tsubai  * This function dumps contents of the PMData
    237   1.1  tsubai  */
    238   1.1  tsubai void
    239   1.1  tsubai pm_printerr(ttl, rval, num, data)
    240   1.1  tsubai 	char *ttl;
    241   1.1  tsubai 	int rval;
    242   1.1  tsubai 	int num;
    243   1.1  tsubai 	char *data;
    244   1.1  tsubai {
    245   1.1  tsubai 	int i;
    246   1.1  tsubai 
    247   1.1  tsubai 	printf("pm: %s:%04x %02x ", ttl, rval, num);
    248   1.1  tsubai 	for (i = 0; i < num; i++)
    249   1.1  tsubai 		printf("%02x ", data[i]);
    250   1.1  tsubai 	printf("\n");
    251   1.1  tsubai }
    252   1.1  tsubai #endif
    253   1.1  tsubai 
    254   1.1  tsubai 
    255   1.1  tsubai 
    256   1.1  tsubai /*
    257   1.1  tsubai  * Check the hardware type of the Power Manager
    258   1.1  tsubai  */
    259   1.1  tsubai void
    260   1.1  tsubai pm_setup_adb()
    261   1.1  tsubai {
    262   1.1  tsubai 	pmHardware = PM_HW_PB5XX;	/* XXX */
    263   1.1  tsubai }
    264   1.1  tsubai 
    265   1.1  tsubai 
    266   1.1  tsubai /*
    267   1.1  tsubai  * Check the existent ADB devices
    268   1.1  tsubai  */
    269   1.1  tsubai void
    270   1.1  tsubai pm_check_adb_devices(id)
    271   1.1  tsubai 	int id;
    272   1.1  tsubai {
    273   1.1  tsubai 	u_short ed = 0x1;
    274   1.1  tsubai 
    275   1.1  tsubai 	ed <<= id;
    276   1.1  tsubai 	pm_existent_ADB_devices |= ed;
    277   1.1  tsubai }
    278   1.1  tsubai 
    279   1.1  tsubai 
    280   1.1  tsubai /*
    281   1.1  tsubai  * Wait until PM IC is busy
    282   1.1  tsubai  */
    283   1.1  tsubai int
    284   1.1  tsubai pm_wait_busy(delay)
    285   1.1  tsubai 	int delay;
    286   1.1  tsubai {
    287   1.1  tsubai 	while (PM_IS_ON) {
    288   1.1  tsubai #ifdef PM_GRAB_SI
    289   1.1  tsubai #if 0
    290   1.1  tsubai 		zshard(0);		/* grab any serial interrupts */
    291   1.1  tsubai #else
    292   1.1  tsubai 		(void)intr_dispatch(0x70);
    293   1.1  tsubai #endif
    294   1.1  tsubai #endif
    295   1.1  tsubai 		if ((--delay) < 0)
    296   1.1  tsubai 			return 1;	/* timeout */
    297   1.1  tsubai 	}
    298   1.1  tsubai 	return 0;
    299   1.1  tsubai }
    300   1.1  tsubai 
    301   1.1  tsubai 
    302   1.1  tsubai /*
    303   1.1  tsubai  * Wait until PM IC is free
    304   1.1  tsubai  */
    305   1.1  tsubai int
    306   1.1  tsubai pm_wait_free(delay)
    307   1.1  tsubai 	int delay;
    308   1.1  tsubai {
    309   1.1  tsubai 	while (PM_IS_OFF) {
    310   1.1  tsubai #ifdef PM_GRAB_SI
    311   1.1  tsubai #if 0
    312   1.1  tsubai 		zshard(0);		/* grab any serial interrupts */
    313   1.1  tsubai #else
    314   1.1  tsubai 		(void)intr_dispatch(0x70);
    315   1.1  tsubai #endif
    316   1.1  tsubai #endif
    317   1.1  tsubai 		if ((--delay) < 0)
    318   1.1  tsubai 			return 0;	/* timeout */
    319   1.1  tsubai 	}
    320   1.1  tsubai 	return 1;
    321   1.1  tsubai }
    322   1.1  tsubai 
    323   1.1  tsubai 
    324   1.1  tsubai 
    325   1.1  tsubai /*
    326   1.1  tsubai  * Functions for the PB1XX series
    327   1.1  tsubai  */
    328   1.1  tsubai 
    329   1.1  tsubai /*
    330   1.1  tsubai  * Receive data from PM for the PB1XX series
    331   1.1  tsubai  */
    332   1.1  tsubai int
    333   1.1  tsubai pm_receive_pm1(data)
    334   1.1  tsubai 	u_char *data;
    335   1.1  tsubai {
    336   1.1  tsubai #if 0
    337   1.1  tsubai 	int rval = 0xffffcd34;
    338   1.1  tsubai 
    339   1.1  tsubai 	via_reg(VIA2, vDirA) = 0x00;
    340   1.1  tsubai 
    341   1.1  tsubai 	switch (1) {
    342   1.1  tsubai 		default:
    343   1.1  tsubai 			if (pm_wait_busy(0x40) != 0)
    344   1.1  tsubai 				break;			/* timeout */
    345   1.1  tsubai 
    346   1.1  tsubai 			PM_SET_STATE_ACKOFF();
    347   1.1  tsubai 			*data = via_reg(VIA2, 0x200);
    348   1.1  tsubai 
    349   1.1  tsubai 			rval = 0xffffcd33;
    350   1.1  tsubai 			if (pm_wait_free(0x40) == 0)
    351   1.1  tsubai 				break;			/* timeout */
    352   1.1  tsubai 
    353   1.1  tsubai 			rval = 0x00;
    354   1.1  tsubai 			break;
    355   1.1  tsubai 	}
    356   1.1  tsubai 
    357   1.1  tsubai 	PM_SET_STATE_ACKON();
    358   1.1  tsubai 	via_reg(VIA2, vDirA) = 0x00;
    359   1.1  tsubai 
    360   1.1  tsubai 	return rval;
    361   1.1  tsubai #else
    362   1.1  tsubai 	panic("pm_receive_pm1");
    363   1.1  tsubai #endif
    364   1.1  tsubai }
    365   1.1  tsubai 
    366   1.1  tsubai 
    367   1.1  tsubai 
    368   1.1  tsubai /*
    369   1.1  tsubai  * Send data to PM for the PB1XX series
    370   1.1  tsubai  */
    371   1.1  tsubai int
    372   1.1  tsubai pm_send_pm1(data, delay)
    373   1.1  tsubai 	u_char data;
    374   1.1  tsubai 	int delay;
    375   1.1  tsubai {
    376   1.1  tsubai #if 0
    377   1.1  tsubai 	int rval;
    378   1.1  tsubai 
    379   1.1  tsubai 	via_reg(VIA2, vDirA) = 0xff;
    380   1.1  tsubai 	via_reg(VIA2, 0x200) = data;
    381   1.1  tsubai 
    382   1.1  tsubai 	PM_SET_STATE_ACKOFF();
    383   1.1  tsubai 	if (pm_wait_busy(0x400) != 0) {
    384   1.1  tsubai 		PM_SET_STATE_ACKON();
    385   1.1  tsubai 		via_reg(VIA2, vDirA) = 0x00;
    386   1.1  tsubai 
    387   1.1  tsubai 		return 0xffffcd36;
    388   1.1  tsubai 	}
    389   1.1  tsubai 
    390   1.1  tsubai 	rval = 0x0;
    391   1.1  tsubai 	PM_SET_STATE_ACKON();
    392   1.1  tsubai 	if (pm_wait_free(0x40) == 0)
    393   1.1  tsubai 		rval = 0xffffcd35;
    394   1.1  tsubai 
    395   1.1  tsubai 	PM_SET_STATE_ACKON();
    396   1.1  tsubai 	via_reg(VIA2, vDirA) = 0x00;
    397   1.1  tsubai 
    398   1.1  tsubai 	return rval;
    399   1.1  tsubai #else
    400   1.1  tsubai 	panic("pm_send_pm1");
    401   1.1  tsubai #endif
    402   1.1  tsubai }
    403   1.1  tsubai 
    404   1.1  tsubai 
    405   1.1  tsubai /*
    406   1.1  tsubai  * My PMgrOp routine for the PB1XX series
    407   1.1  tsubai  */
    408   1.1  tsubai int
    409   1.1  tsubai pm_pmgrop_pm1(pmdata)
    410   1.1  tsubai 	PMData *pmdata;
    411   1.1  tsubai {
    412   1.1  tsubai #if 0
    413   1.1  tsubai 	int i;
    414   1.1  tsubai 	int s = 0x81815963;
    415   1.1  tsubai 	u_char via1_vIER, via1_vDirA;
    416   1.1  tsubai 	int rval = 0;
    417   1.1  tsubai 	int num_pm_data = 0;
    418   1.1  tsubai 	u_char pm_cmd;
    419   1.1  tsubai 	u_char pm_data;
    420   1.1  tsubai 	u_char *pm_buf;
    421   1.1  tsubai 
    422   1.1  tsubai 	/* disable all inetrrupts but PM */
    423   1.1  tsubai 	via1_vIER = via_reg(VIA1, vIER);
    424   1.1  tsubai 	PM_VIA_INTR_DISABLE();
    425   1.1  tsubai 
    426   1.1  tsubai 	via1_vDirA = via_reg(VIA1, vDirA);
    427   1.1  tsubai 
    428   1.1  tsubai 	switch (pmdata->command) {
    429   1.1  tsubai 		default:
    430   1.1  tsubai 			for (i = 0; i < 7; i++) {
    431   1.1  tsubai 				via_reg(VIA2, vDirA) = 0x00;
    432   1.1  tsubai 
    433   1.1  tsubai 				/* wait until PM is free */
    434   1.1  tsubai 				if (pm_wait_free(ADBDelay) == 0) {	/* timeout */
    435   1.1  tsubai 					via_reg(VIA2, vDirA) = 0x00;
    436   1.1  tsubai 					/* restore formar value */
    437   1.1  tsubai 					via_reg(VIA1, vDirA) = via1_vDirA;
    438   1.1  tsubai 					via_reg(VIA1, vIER) = via1_vIER;
    439   1.1  tsubai 					return 0xffffcd38;
    440   1.1  tsubai 				}
    441   1.1  tsubai 
    442   1.1  tsubai 				switch (mac68k_machine.machineid) {
    443   1.1  tsubai 					case MACH_MACPB160:
    444   1.1  tsubai 					case MACH_MACPB165:
    445   1.1  tsubai 					case MACH_MACPB165C:
    446   1.1  tsubai 					case MACH_MACPB180:
    447   1.1  tsubai 					case MACH_MACPB180C:
    448   1.1  tsubai 						{
    449   1.1  tsubai 							int delay = ADBDelay * 16;
    450   1.1  tsubai 
    451   1.1  tsubai 							via_reg(VIA2, vDirA) = 0x00;
    452   1.1  tsubai 							while ((via_reg(VIA2, 0x200) == 0x7f) && (delay >= 0))
    453   1.1  tsubai 								delay--;
    454   1.1  tsubai 
    455   1.1  tsubai 							if (delay < 0) {	/* timeout */
    456   1.1  tsubai 								via_reg(VIA2, vDirA) = 0x00;
    457   1.1  tsubai 								/* restore formar value */
    458   1.1  tsubai 								via_reg(VIA1, vIER) = via1_vIER;
    459   1.1  tsubai 								return 0xffffcd38;
    460   1.1  tsubai 							}
    461   1.1  tsubai 						}
    462   1.1  tsubai 				} /* end switch */
    463   1.1  tsubai 
    464   1.1  tsubai 				s = splhigh();
    465   1.1  tsubai 
    466   1.1  tsubai 				via1_vDirA = via_reg(VIA1, vDirA);
    467   1.1  tsubai 				via_reg(VIA1, vDirA) &= 0x7f;
    468   1.1  tsubai 
    469   1.1  tsubai 				pm_cmd = (u_char)(pmdata->command & 0xff);
    470   1.1  tsubai 				if ((rval = pm_send_pm1(pm_cmd, ADBDelay * 8)) == 0)
    471   1.1  tsubai 					break;	/* send command succeeded */
    472   1.1  tsubai 
    473   1.1  tsubai 				via_reg(VIA1, vDirA) = via1_vDirA;
    474   1.1  tsubai 				splx(s);
    475   1.1  tsubai 			} /* end for */
    476   1.1  tsubai 
    477   1.1  tsubai 			/* failed to send a command */
    478   1.1  tsubai 			if (i == 7) {
    479   1.1  tsubai 				via_reg(VIA2, vDirA) = 0x00;
    480   1.1  tsubai 				/* restore formar value */
    481   1.1  tsubai 				via_reg(VIA1, vDirA) = via1_vDirA;
    482   1.1  tsubai 				via_reg(VIA1, vIER) = via1_vIER;
    483   1.9  tsubai 				if (s != 0x81815963)
    484   1.9  tsubai 					splx(s);
    485   1.9  tsubai 				return 0xffffcd38;
    486   1.1  tsubai 			}
    487   1.1  tsubai 
    488   1.1  tsubai 			/* send # of PM data */
    489   1.1  tsubai 			num_pm_data = pmdata->num_data;
    490   1.1  tsubai 			if ((rval = pm_send_pm1((u_char)(num_pm_data & 0xff), ADBDelay * 8)) != 0)
    491   1.1  tsubai 				break;			/* timeout */
    492   1.1  tsubai 
    493   1.1  tsubai 			/* send PM data */
    494   1.1  tsubai 			pm_buf = (u_char *)pmdata->s_buf;
    495   1.1  tsubai 			for (i = 0; i < num_pm_data; i++)
    496   1.1  tsubai 				if ((rval = pm_send_pm1(pm_buf[i], ADBDelay * 8)) != 0)
    497   1.1  tsubai 					break;		/* timeout */
    498   1.1  tsubai 			if ((i != num_pm_data) && (num_pm_data != 0))
    499   1.1  tsubai 				break;			/* timeout */
    500   1.1  tsubai 
    501   1.1  tsubai 			/* Will PM IC return data? */
    502   1.1  tsubai 			if ((pm_cmd & 0x08) == 0) {
    503   1.1  tsubai 				rval = 0;
    504   1.1  tsubai 				break;			/* no returned data */
    505   1.1  tsubai 			}
    506   1.1  tsubai 
    507   1.1  tsubai 			rval = 0xffffcd37;
    508   1.1  tsubai 			if (pm_wait_busy(ADBDelay) != 0)
    509   1.1  tsubai 				break;			/* timeout */
    510   1.1  tsubai 
    511   1.1  tsubai 			/* receive PM command */
    512   1.1  tsubai 			if ((rval = pm_receive_pm1(&pm_data)) != 0)
    513   1.1  tsubai 				break;
    514   1.1  tsubai 
    515   1.1  tsubai 			pmdata->command = pm_data;
    516   1.1  tsubai 
    517   1.1  tsubai 			/* receive number of PM data */
    518   1.1  tsubai 			if ((rval = pm_receive_pm1(&pm_data)) != 0)
    519   1.1  tsubai 				break;			/* timeout */
    520   1.1  tsubai 			num_pm_data = pm_data;
    521   1.1  tsubai 			pmdata->num_data = num_pm_data;
    522   1.1  tsubai 
    523   1.1  tsubai 			/* receive PM data */
    524   1.1  tsubai 			pm_buf = (u_char *)pmdata->r_buf;
    525   1.1  tsubai 			for (i = 0; i < num_pm_data; i++) {
    526   1.1  tsubai 				if ((rval = pm_receive_pm1(&pm_data)) != 0)
    527   1.9  tsubai 					break;		/* timeout */
    528   1.1  tsubai 				pm_buf[i] = pm_data;
    529   1.1  tsubai 			}
    530   1.1  tsubai 
    531   1.1  tsubai 			rval = 0;
    532   1.1  tsubai 	}
    533   1.1  tsubai 
    534   1.1  tsubai 	via_reg(VIA2, vDirA) = 0x00;
    535   1.1  tsubai 
    536   1.1  tsubai 	/* restore formar value */
    537   1.1  tsubai 	via_reg(VIA1, vDirA) = via1_vDirA;
    538   1.1  tsubai 	via_reg(VIA1, vIER) = via1_vIER;
    539   1.1  tsubai 	if (s != 0x81815963)
    540   1.1  tsubai 		splx(s);
    541   1.1  tsubai 
    542   1.1  tsubai 	return rval;
    543   1.1  tsubai #else
    544   1.1  tsubai 	panic("pm_pmgrop_pm1");
    545   1.1  tsubai #endif
    546   1.1  tsubai }
    547   1.1  tsubai 
    548   1.1  tsubai 
    549   1.1  tsubai /*
    550   1.1  tsubai  * My PM interrupt routine for PB1XX series
    551   1.1  tsubai  */
    552   1.1  tsubai void
    553   1.1  tsubai pm_intr_pm1()
    554   1.1  tsubai {
    555   1.1  tsubai #if 0
    556   1.1  tsubai 	int s;
    557   1.1  tsubai 	int rval;
    558   1.1  tsubai 	PMData pmdata;
    559   1.1  tsubai 
    560   1.1  tsubai 	s = splhigh();
    561   1.1  tsubai 
    562   1.1  tsubai 	PM_VIA_CLR_INTR();				/* clear VIA1 interrupt */
    563   1.1  tsubai 
    564   1.1  tsubai 	/* ask PM what happend */
    565   1.1  tsubai 	pmdata.command = 0x78;
    566   1.1  tsubai 	pmdata.num_data = 0;
    567   1.1  tsubai 	pmdata.data[0] = pmdata.data[1] = 0;
    568   1.1  tsubai 	pmdata.s_buf = &pmdata.data[2];
    569   1.1  tsubai 	pmdata.r_buf = &pmdata.data[2];
    570   1.1  tsubai 	rval = pm_pmgrop_pm1(&pmdata);
    571   1.1  tsubai 	if (rval != 0) {
    572   1.1  tsubai #ifdef ADB_DEBUG
    573   1.1  tsubai 		if (adb_debug)
    574   1.1  tsubai 			printf("pm: PM is not ready. error code=%08x\n", rval);
    575   1.1  tsubai #endif
    576   1.1  tsubai 		splx(s);
    577   1.1  tsubai 	}
    578   1.1  tsubai 
    579   1.1  tsubai 	if ((pmdata.data[2] & 0x10) == 0x10) {
    580   1.1  tsubai 		if ((pmdata.data[2] & 0x0f) == 0) {
    581   1.1  tsubai 			/* ADB data that were requested by TALK command */
    582   1.1  tsubai 			pm_adb_get_TALK_result(&pmdata);
    583   1.1  tsubai 		} else if ((pmdata.data[2] & 0x08) == 0x8) {
    584   1.1  tsubai 			/* PM is requesting to poll  */
    585   1.1  tsubai 			pm_adb_poll_next_device_pm1(&pmdata);
    586   1.1  tsubai 		} else if ((pmdata.data[2] & 0x04) == 0x4) {
    587   1.1  tsubai 			/* ADB device event */
    588   1.1  tsubai 			pm_adb_get_ADB_data(&pmdata);
    589   1.1  tsubai 		}
    590   1.1  tsubai 	} else {
    591   1.1  tsubai #ifdef ADB_DEBUG
    592   1.1  tsubai 		if (adb_debug)
    593   1.1  tsubai 			pm_printerr("driver does not supported this event.",
    594   1.1  tsubai 			    rval, pmdata.num_data, pmdata.data);
    595   1.1  tsubai #endif
    596   1.1  tsubai 	}
    597   1.1  tsubai 
    598   1.1  tsubai 	splx(s);
    599   1.1  tsubai #else
    600   1.1  tsubai 	panic("pm_intr_pm1");
    601   1.1  tsubai #endif
    602   1.1  tsubai }
    603   1.1  tsubai 
    604   1.1  tsubai 
    605   1.1  tsubai 
    606   1.1  tsubai /*
    607   1.1  tsubai  * Functions for the PB Duo series and the PB 5XX series
    608   1.1  tsubai  */
    609   1.1  tsubai 
    610   1.1  tsubai /*
    611   1.1  tsubai  * Receive data from PM for the PB Duo series and the PB 5XX series
    612   1.1  tsubai  */
    613   1.1  tsubai int
    614   1.1  tsubai pm_receive_pm2(data)
    615   1.1  tsubai 	u_char *data;
    616   1.1  tsubai {
    617   1.1  tsubai 	int i;
    618   1.1  tsubai 	int rval;
    619   1.1  tsubai 
    620   1.1  tsubai 	rval = 0xffffcd34;
    621   1.1  tsubai 
    622   1.1  tsubai 	switch (1) {
    623   1.1  tsubai 		default:
    624   1.1  tsubai 			/* set VIA SR to input mode */
    625   1.1  tsubai 			via_reg_or(VIA1, vACR, 0x0c);
    626   1.1  tsubai 			via_reg_and(VIA1, vACR, ~0x10);
    627   1.1  tsubai 			i = PM_SR();
    628   1.1  tsubai 
    629   1.1  tsubai 			PM_SET_STATE_ACKOFF();
    630   1.1  tsubai 			if (pm_wait_busy((int)ADBDelay*32) != 0)
    631   1.1  tsubai 				break;		/* timeout */
    632   1.1  tsubai 
    633   1.1  tsubai 			PM_SET_STATE_ACKON();
    634   1.1  tsubai 			rval = 0xffffcd33;
    635   1.1  tsubai 			if (pm_wait_free((int)ADBDelay*32) == 0)
    636   1.1  tsubai 				break;		/* timeout */
    637   1.1  tsubai 
    638   1.1  tsubai 			*data = PM_SR();
    639   1.1  tsubai 			rval = 0;
    640   1.1  tsubai 
    641   1.1  tsubai 			break;
    642   1.1  tsubai 	}
    643   1.1  tsubai 
    644   1.1  tsubai 	PM_SET_STATE_ACKON();
    645   1.1  tsubai 	via_reg_or(VIA1, vACR, 0x1c);
    646   1.1  tsubai 
    647   1.1  tsubai 	return rval;
    648   1.1  tsubai }
    649   1.1  tsubai 
    650   1.1  tsubai 
    651   1.1  tsubai 
    652   1.1  tsubai /*
    653   1.1  tsubai  * Send data to PM for the PB Duo series and the PB 5XX series
    654   1.1  tsubai  */
    655   1.1  tsubai int
    656   1.1  tsubai pm_send_pm2(data)
    657   1.1  tsubai 	u_char data;
    658   1.1  tsubai {
    659   1.1  tsubai 	int rval;
    660   1.1  tsubai 
    661   1.1  tsubai 	via_reg_or(VIA1, vACR, 0x1c);
    662   1.1  tsubai 	write_via_reg(VIA1, vSR, data);	/* PM_SR() = data; */
    663   1.1  tsubai 
    664   1.1  tsubai 	PM_SET_STATE_ACKOFF();
    665   1.1  tsubai 	rval = 0xffffcd36;
    666   1.1  tsubai 	if (pm_wait_busy((int)ADBDelay*32) != 0) {
    667   1.1  tsubai 		PM_SET_STATE_ACKON();
    668   1.1  tsubai 
    669   1.1  tsubai 		via_reg_or(VIA1, vACR, 0x1c);
    670   1.1  tsubai 
    671   1.1  tsubai 		return rval;
    672   1.1  tsubai 	}
    673   1.1  tsubai 
    674   1.1  tsubai 	PM_SET_STATE_ACKON();
    675   1.1  tsubai 	rval = 0xffffcd35;
    676   1.1  tsubai 	if (pm_wait_free((int)ADBDelay*32) != 0)
    677   1.1  tsubai 		rval = 0;
    678   1.1  tsubai 
    679   1.1  tsubai 	PM_SET_STATE_ACKON();
    680   1.1  tsubai 	via_reg_or(VIA1, vACR, 0x1c);
    681   1.1  tsubai 
    682   1.1  tsubai 	return rval;
    683   1.1  tsubai }
    684   1.1  tsubai 
    685   1.1  tsubai 
    686   1.1  tsubai 
    687   1.1  tsubai /*
    688   1.1  tsubai  * My PMgrOp routine for the PB Duo series and the PB 5XX series
    689   1.1  tsubai  */
    690   1.1  tsubai int
    691   1.1  tsubai pm_pmgrop_pm2(pmdata)
    692   1.1  tsubai 	PMData *pmdata;
    693   1.1  tsubai {
    694   1.1  tsubai 	int i;
    695   1.1  tsubai 	int s;
    696   1.1  tsubai 	u_char via1_vIER;
    697   1.1  tsubai 	int rval = 0;
    698   1.1  tsubai 	int num_pm_data = 0;
    699   1.1  tsubai 	u_char pm_cmd;
    700   1.1  tsubai 	short pm_num_rx_data;
    701   1.1  tsubai 	u_char pm_data;
    702   1.1  tsubai 	u_char *pm_buf;
    703   1.1  tsubai 
    704   1.1  tsubai 	s = splhigh();
    705   1.1  tsubai 
    706   1.1  tsubai 	/* disable all inetrrupts but PM */
    707   1.1  tsubai 	via1_vIER = 0x10;
    708   1.1  tsubai 	via1_vIER &= read_via_reg(VIA1, vIER);
    709   1.1  tsubai 	write_via_reg(VIA1, vIER, via1_vIER);
    710   1.1  tsubai 	if (via1_vIER != 0x0)
    711   1.1  tsubai 		via1_vIER |= 0x80;
    712   1.1  tsubai 
    713   1.1  tsubai 	switch (pmdata->command) {
    714   1.1  tsubai 		default:
    715   1.1  tsubai 			/* wait until PM is free */
    716   1.1  tsubai 			pm_cmd = (u_char)(pmdata->command & 0xff);
    717   1.1  tsubai 			rval = 0xcd38;
    718   1.1  tsubai 			if (pm_wait_free(ADBDelay * 4) == 0)
    719   1.1  tsubai 				break;			/* timeout */
    720   1.1  tsubai 
    721   1.1  tsubai 			if (HwCfgFlags3 & 0x00200000) {
    722   1.1  tsubai 				/* PB 160, PB 165(c), PB 180(c)? */
    723   1.1  tsubai 				int delay = ADBDelay * 16;
    724   1.1  tsubai 
    725   1.1  tsubai 				write_via_reg(VIA2, vDirA, 0x00);
    726   1.1  tsubai 				while ((read_via_reg(VIA2, 0x200) == 0x07) &&
    727   1.1  tsubai 				    (delay >= 0))
    728   1.1  tsubai 					delay--;
    729   1.1  tsubai 
    730   1.1  tsubai 				if (delay < 0) {
    731   1.1  tsubai 					rval = 0xffffcd38;
    732   1.1  tsubai 					break;		/* timeout */
    733   1.1  tsubai 				}
    734   1.1  tsubai 			}
    735   1.1  tsubai 
    736   1.1  tsubai 			/* send PM command */
    737   1.1  tsubai 			if ((rval = pm_send_pm2((u_char)(pm_cmd & 0xff))))
    738   1.1  tsubai 				break;				/* timeout */
    739   1.1  tsubai 
    740   1.1  tsubai 			/* send number of PM data */
    741   1.1  tsubai 			num_pm_data = pmdata->num_data;
    742   1.1  tsubai 			if (HwCfgFlags3 & 0x00020000) {		/* PB Duo, PB 5XX */
    743   1.1  tsubai 				if (pm_send_cmd_type[pm_cmd] < 0) {
    744   1.1  tsubai 					if ((rval = pm_send_pm2((u_char)(num_pm_data & 0xff))) != 0)
    745   1.1  tsubai 						break;		/* timeout */
    746   1.1  tsubai 					pmdata->command = 0;
    747   1.1  tsubai 				}
    748   1.1  tsubai 			} else {				/* PB 1XX series ? */
    749   1.1  tsubai 				if ((rval = pm_send_pm2((u_char)(num_pm_data & 0xff))) != 0)
    750   1.1  tsubai 					break;			/* timeout */
    751   1.1  tsubai 			}
    752   1.1  tsubai 			/* send PM data */
    753   1.1  tsubai 			pm_buf = (u_char *)pmdata->s_buf;
    754   1.1  tsubai 			for (i = 0 ; i < num_pm_data; i++)
    755   1.1  tsubai 				if ((rval = pm_send_pm2(pm_buf[i])) != 0)
    756   1.1  tsubai 					break;			/* timeout */
    757   1.1  tsubai 			if (i != num_pm_data)
    758   1.1  tsubai 				break;				/* timeout */
    759   1.1  tsubai 
    760   1.1  tsubai 
    761   1.1  tsubai 			/* check if PM will send me data  */
    762   1.1  tsubai 			pm_num_rx_data = pm_receive_cmd_type[pm_cmd];
    763   1.1  tsubai 			pmdata->num_data = pm_num_rx_data;
    764   1.1  tsubai 			if (pm_num_rx_data == 0) {
    765   1.1  tsubai 				rval = 0;
    766   1.1  tsubai 				break;				/* no return data */
    767   1.1  tsubai 			}
    768   1.1  tsubai 
    769   1.1  tsubai 			/* receive PM command */
    770   1.1  tsubai 			pm_data = pmdata->command;
    771   1.1  tsubai 			if (HwCfgFlags3 & 0x00020000) {		/* PB Duo, PB 5XX */
    772   1.1  tsubai 				pm_num_rx_data--;
    773   1.1  tsubai 				if (pm_num_rx_data == 0)
    774   1.1  tsubai 					if ((rval = pm_receive_pm2(&pm_data)) != 0) {
    775   1.1  tsubai 						rval = 0xffffcd37;
    776   1.1  tsubai 						break;
    777   1.1  tsubai 					}
    778   1.1  tsubai 				pmdata->command = pm_data;
    779   1.1  tsubai 			} else {				/* PB 1XX series ? */
    780   1.1  tsubai 				if ((rval = pm_receive_pm2(&pm_data)) != 0) {
    781   1.1  tsubai 					rval = 0xffffcd37;
    782   1.1  tsubai 					break;
    783   1.1  tsubai 				}
    784   1.1  tsubai 				pmdata->command = pm_data;
    785   1.1  tsubai 			}
    786   1.1  tsubai 
    787   1.1  tsubai 			/* receive number of PM data */
    788   1.1  tsubai 			if (HwCfgFlags3 & 0x00020000) {		/* PB Duo, PB 5XX */
    789   1.1  tsubai 				if (pm_num_rx_data < 0) {
    790   1.1  tsubai 					if ((rval = pm_receive_pm2(&pm_data)) != 0)
    791   1.1  tsubai 						break;		/* timeout */
    792   1.1  tsubai 					num_pm_data = pm_data;
    793   1.1  tsubai 				} else
    794   1.1  tsubai 					num_pm_data = pm_num_rx_data;
    795   1.1  tsubai 				pmdata->num_data = num_pm_data;
    796   1.1  tsubai 			} else {				/* PB 1XX serias ? */
    797   1.1  tsubai 				if ((rval = pm_receive_pm2(&pm_data)) != 0)
    798   1.1  tsubai 					break;			/* timeout */
    799   1.1  tsubai 				num_pm_data = pm_data;
    800   1.1  tsubai 				pmdata->num_data = num_pm_data;
    801   1.1  tsubai 			}
    802   1.1  tsubai 
    803   1.1  tsubai 			/* receive PM data */
    804   1.1  tsubai 			pm_buf = (u_char *)pmdata->r_buf;
    805   1.1  tsubai 			for (i = 0; i < num_pm_data; i++) {
    806   1.1  tsubai 				if ((rval = pm_receive_pm2(&pm_data)) != 0)
    807   1.1  tsubai 					break;			/* timeout */
    808   1.1  tsubai 				pm_buf[i] = pm_data;
    809   1.1  tsubai 			}
    810   1.1  tsubai 
    811   1.1  tsubai 			rval = 0;
    812   1.1  tsubai 	}
    813   1.1  tsubai 
    814   1.1  tsubai 	/* restore former value */
    815   1.1  tsubai 	write_via_reg(VIA1, vIER, via1_vIER);
    816   1.1  tsubai 	splx(s);
    817   1.1  tsubai 
    818   1.1  tsubai 	return rval;
    819   1.1  tsubai }
    820   1.1  tsubai 
    821   1.1  tsubai 
    822   1.1  tsubai /*
    823   1.1  tsubai  * My PM interrupt routine for the PB Duo series and the PB 5XX series
    824   1.1  tsubai  */
    825   1.1  tsubai void
    826   1.1  tsubai pm_intr_pm2()
    827   1.1  tsubai {
    828   1.1  tsubai 	int s;
    829   1.1  tsubai 	int rval;
    830   1.1  tsubai 	PMData pmdata;
    831   1.1  tsubai 
    832   1.1  tsubai 	s = splhigh();
    833   1.1  tsubai 
    834   1.1  tsubai 	PM_VIA_CLR_INTR();			/* clear VIA1 interrupt */
    835   1.1  tsubai 						/* ask PM what happend */
    836   1.1  tsubai 	pmdata.command = 0x78;
    837   1.1  tsubai 	pmdata.num_data = 0;
    838   1.1  tsubai 	pmdata.s_buf = &pmdata.data[2];
    839   1.1  tsubai 	pmdata.r_buf = &pmdata.data[2];
    840   1.1  tsubai 	rval = pm_pmgrop_pm2(&pmdata);
    841   1.1  tsubai 	if (rval != 0) {
    842   1.1  tsubai #ifdef ADB_DEBUG
    843   1.1  tsubai 		if (adb_debug)
    844   1.1  tsubai 			printf("pm: PM is not ready. error code: %08x\n", rval);
    845   1.1  tsubai #endif
    846   1.1  tsubai 		splx(s);
    847   1.1  tsubai 	}
    848   1.1  tsubai 
    849   1.1  tsubai 	switch ((u_int)(pmdata.data[2] & 0xff)) {
    850   1.1  tsubai 		case 0x00:			/* 1 sec interrupt? */
    851   1.1  tsubai 			break;
    852   1.1  tsubai 		case 0x80:			/* 1 sec interrupt? */
    853   1.1  tsubai 			pm_counter++;
    854   1.1  tsubai 			break;
    855   1.1  tsubai 		case 0x08:			/* Brightness/Contrast button on LCD panel */
    856   1.1  tsubai 			/* get brightness and contrast of the LCD */
    857   1.1  tsubai 			pm_LCD_brightness = (u_int)pmdata.data[3] & 0xff;
    858   1.1  tsubai 			pm_LCD_contrast = (u_int)pmdata.data[4] & 0xff;
    859   1.1  tsubai /*
    860   1.1  tsubai 			pm_printerr("#08", rval, pmdata.num_data, pmdata.data);
    861   1.1  tsubai 			pmdata.command = 0x33;
    862   1.1  tsubai 			pmdata.num_data = 1;
    863   1.1  tsubai 			pmdata.s_buf = pmdata.data;
    864   1.1  tsubai 			pmdata.r_buf = pmdata.data;
    865   1.1  tsubai 			pmdata.data[0] = pm_LCD_contrast;
    866   1.1  tsubai 			rval = pm_pmgrop_pm2(&pmdata);
    867   1.1  tsubai 			pm_printerr("#33", rval, pmdata.num_data, pmdata.data);
    868   1.1  tsubai */
    869   1.1  tsubai 			/* this is an experimental code */
    870   1.1  tsubai 			pmdata.command = 0x41;
    871   1.1  tsubai 			pmdata.num_data = 1;
    872   1.1  tsubai 			pmdata.s_buf = pmdata.data;
    873   1.1  tsubai 			pmdata.r_buf = pmdata.data;
    874   1.1  tsubai 			pm_LCD_brightness = 0x7f - pm_LCD_brightness / 2;
    875   1.2  tsubai 			if (pm_LCD_brightness < 0x08)
    876   1.2  tsubai 				pm_LCD_brightness = 0x08;
    877   1.2  tsubai 			if (pm_LCD_brightness > 0x78)
    878   1.2  tsubai 				pm_LCD_brightness = 0x78;
    879   1.1  tsubai 			pmdata.data[0] = pm_LCD_brightness;
    880   1.1  tsubai 			rval = pm_pmgrop_pm2(&pmdata);
    881   1.1  tsubai 			break;
    882   1.1  tsubai 		case 0x10:			/* ADB data that were requested by TALK command */
    883   1.1  tsubai 		case 0x14:
    884   1.1  tsubai 			pm_adb_get_TALK_result(&pmdata);
    885   1.1  tsubai 			break;
    886   1.1  tsubai 		case 0x16:			/* ADB device event */
    887   1.1  tsubai 		case 0x18:
    888   1.1  tsubai 		case 0x1e:
    889   1.1  tsubai 			pm_adb_get_ADB_data(&pmdata);
    890   1.1  tsubai 			break;
    891   1.1  tsubai 		default:
    892   1.1  tsubai #ifdef ADB_DEBUG
    893   1.1  tsubai 			if (adb_debug)
    894   1.1  tsubai 				pm_printerr("driver does not supported this event.",
    895   1.1  tsubai 				    pmdata.data[2], pmdata.num_data,
    896   1.1  tsubai 				    pmdata.data);
    897   1.1  tsubai #endif
    898   1.1  tsubai 			break;
    899   1.1  tsubai 	}
    900   1.1  tsubai 
    901   1.1  tsubai 	splx(s);
    902   1.1  tsubai }
    903   1.1  tsubai 
    904   1.1  tsubai 
    905   1.1  tsubai #if 0
    906   1.1  tsubai /*
    907   1.1  tsubai  * MRG-based PMgrOp routine
    908   1.1  tsubai  */
    909   1.1  tsubai int
    910   1.1  tsubai pm_pmgrop_mrg(pmdata)
    911   1.1  tsubai 	PMData *pmdata;
    912   1.1  tsubai {
    913   1.1  tsubai 	u_int32_t rval=0;
    914   1.1  tsubai 
    915   1.1  tsubai 	asm("
    916   1.1  tsubai 		movl	%1, a0
    917   1.1  tsubai 		.word	0xa085
    918   1.1  tsubai 		movl	d0, %0"
    919   1.1  tsubai 		: "=g" (rval)
    920   1.1  tsubai 		: "g" (pmdata)
    921   1.1  tsubai 		: "a0", "d0" );
    922   1.1  tsubai 
    923   1.1  tsubai 	return rval;
    924   1.1  tsubai }
    925   1.1  tsubai #endif
    926   1.1  tsubai 
    927   1.1  tsubai 
    928   1.1  tsubai /*
    929   1.1  tsubai  * My PMgrOp routine
    930   1.1  tsubai  */
    931   1.1  tsubai int
    932   1.1  tsubai pmgrop(pmdata)
    933   1.1  tsubai 	PMData *pmdata;
    934   1.1  tsubai {
    935   1.1  tsubai 	switch (pmHardware) {
    936   1.1  tsubai 		case PM_HW_PB1XX:
    937   1.1  tsubai 			return (pm_pmgrop_pm1(pmdata));
    938   1.1  tsubai 			break;
    939   1.1  tsubai 		case PM_HW_PB5XX:
    940   1.1  tsubai 			return (pm_pmgrop_pm2(pmdata));
    941   1.1  tsubai 			break;
    942   1.1  tsubai 		default:
    943   1.1  tsubai 			/* return (pmgrop_mrg(pmdata)); */
    944   1.1  tsubai 			return 1;
    945   1.1  tsubai 	}
    946   1.1  tsubai }
    947   1.1  tsubai 
    948   1.1  tsubai 
    949   1.1  tsubai /*
    950   1.1  tsubai  * My PM interrupt routine
    951   1.1  tsubai  */
    952   1.1  tsubai void
    953   1.1  tsubai pm_intr()
    954   1.1  tsubai {
    955   1.1  tsubai 	switch (pmHardware) {
    956   1.1  tsubai 		case PM_HW_PB1XX:
    957   1.1  tsubai 			pm_intr_pm1();
    958   1.1  tsubai 			break;
    959   1.1  tsubai 		case PM_HW_PB5XX:
    960   1.1  tsubai 			pm_intr_pm2();
    961   1.1  tsubai 			break;
    962   1.1  tsubai 		default:
    963   1.1  tsubai 			break;
    964   1.1  tsubai 	}
    965   1.1  tsubai }
    966   1.1  tsubai 
    967   1.1  tsubai 
    968   1.1  tsubai 
    969   1.1  tsubai /*
    970   1.1  tsubai  * Synchronous ADBOp routine for the Power Manager
    971   1.1  tsubai  */
    972   1.1  tsubai int
    973   1.1  tsubai pm_adb_op(buffer, compRout, data, command)
    974   1.1  tsubai 	u_char *buffer;
    975   1.1  tsubai 	void *compRout;
    976   1.1  tsubai 	void *data;
    977   1.1  tsubai 	int command;
    978   1.1  tsubai {
    979   1.1  tsubai 	int i;
    980   1.1  tsubai 	int s;
    981   1.1  tsubai 	int rval;
    982  1.12  tsubai 	int timo;
    983   1.1  tsubai 	PMData pmdata;
    984   1.1  tsubai 	struct adbCommand packet;
    985   1.1  tsubai 
    986   1.1  tsubai 	if (adbWaiting == 1)
    987   1.1  tsubai 		return 1;
    988   1.1  tsubai 
    989   1.1  tsubai 	s = splhigh();
    990   1.1  tsubai 	write_via_reg(VIA1, vIER, 0x10);
    991   1.1  tsubai 
    992   1.1  tsubai  	adbBuffer = buffer;
    993   1.1  tsubai 	adbCompRout = compRout;
    994   1.1  tsubai 	adbCompData = data;
    995   1.1  tsubai 
    996   1.1  tsubai 	pmdata.command = 0x20;
    997   1.1  tsubai 	pmdata.s_buf = pmdata.data;
    998   1.1  tsubai 	pmdata.r_buf = pmdata.data;
    999   1.1  tsubai 
   1000   1.1  tsubai 	/* if the command is LISTEN, add number of ADB data to number of PM data */
   1001   1.1  tsubai 	if ((command & 0xc) == 0x8) {
   1002   1.1  tsubai 		if (buffer != (u_char *)0)
   1003   1.1  tsubai 			pmdata.num_data = buffer[0] + 3;
   1004   1.1  tsubai 	} else {
   1005   1.1  tsubai 		pmdata.num_data = 3;
   1006   1.1  tsubai 	}
   1007   1.1  tsubai 
   1008   1.1  tsubai 	pmdata.data[0] = (u_char)(command & 0xff);
   1009   1.1  tsubai 	pmdata.data[1] = 0;
   1010   1.1  tsubai 	if ((command & 0xc) == 0x8) {		/* if the command is LISTEN, copy ADB data to PM buffer */
   1011   1.1  tsubai 		if ((buffer != (u_char *)0) && (buffer[0] <= 24)) {
   1012   1.1  tsubai 			pmdata.data[2] = buffer[0];		/* number of data */
   1013   1.1  tsubai 			for (i = 0; i < buffer[0]; i++)
   1014   1.1  tsubai 				pmdata.data[3 + i] = buffer[1 + i];
   1015   1.1  tsubai 		} else
   1016   1.1  tsubai 			pmdata.data[2] = 0;
   1017   1.1  tsubai 	} else
   1018   1.1  tsubai 		pmdata.data[2] = 0;
   1019   1.1  tsubai 
   1020   1.1  tsubai 	if ((command & 0xc) != 0xc) {		/* if the command is not TALK */
   1021   1.1  tsubai 		/* set up stuff for adb_pass_up */
   1022   1.1  tsubai 		packet.data[0] = 1 + pmdata.data[2];
   1023   1.1  tsubai 		packet.data[1] = command;
   1024   1.1  tsubai 		for (i = 0; i < pmdata.data[2]; i++)
   1025   1.1  tsubai 			packet.data[i+2] = pmdata.data[i+3];
   1026   1.1  tsubai 		packet.saveBuf = adbBuffer;
   1027   1.1  tsubai 		packet.compRout = adbCompRout;
   1028   1.1  tsubai 		packet.compData = adbCompData;
   1029   1.1  tsubai 		packet.cmd = command;
   1030   1.1  tsubai 		packet.unsol = 0;
   1031   1.1  tsubai 		packet.ack_only = 1;
   1032   1.1  tsubai 		adb_polling = 1;
   1033   1.1  tsubai 		adb_pass_up(&packet);
   1034   1.1  tsubai 		adb_polling = 0;
   1035   1.1  tsubai 	}
   1036   1.1  tsubai 
   1037   1.1  tsubai 	rval = pmgrop(&pmdata);
   1038   1.9  tsubai 	if (rval != 0) {
   1039   1.9  tsubai 		splx(s);
   1040   1.1  tsubai 		return 1;
   1041   1.9  tsubai 	}
   1042   1.1  tsubai 
   1043  1.12  tsubai 	delay(10000);
   1044  1.12  tsubai 
   1045   1.1  tsubai 	adbWaiting = 1;
   1046   1.1  tsubai 	adbWaitingCmd = command;
   1047   1.1  tsubai 
   1048   1.1  tsubai 	PM_VIA_INTR_ENABLE();
   1049   1.1  tsubai 
   1050   1.1  tsubai 	/* wait until the PM interrupt is occured */
   1051  1.12  tsubai 	timo = 0x80000;
   1052   1.1  tsubai 	while (adbWaiting == 1) {
   1053   1.1  tsubai 		if (read_via_reg(VIA1, vIFR) & 0x14)
   1054   1.1  tsubai 			pm_intr();
   1055   1.1  tsubai #ifdef PM_GRAB_SI
   1056   1.1  tsubai #if 0
   1057   1.1  tsubai 			zshard(0);		/* grab any serial interrupts */
   1058   1.1  tsubai #else
   1059   1.1  tsubai 			(void)intr_dispatch(0x70);
   1060   1.1  tsubai #endif
   1061   1.1  tsubai #endif
   1062  1.12  tsubai 		if ((--timo) < 0) {
   1063   1.9  tsubai 			splx(s);
   1064   1.1  tsubai 			return 1;
   1065   1.9  tsubai 		}
   1066   1.1  tsubai 	}
   1067   1.1  tsubai 
   1068   1.1  tsubai 	/* this command enables the interrupt by operating ADB devices */
   1069   1.1  tsubai 	if (HwCfgFlags3 & 0x00020000) {		/* PB Duo series, PB 5XX series */
   1070   1.1  tsubai 		pmdata.command = 0x20;
   1071   1.1  tsubai 		pmdata.num_data = 4;
   1072   1.1  tsubai 		pmdata.s_buf = pmdata.data;
   1073   1.1  tsubai 		pmdata.r_buf = pmdata.data;
   1074   1.1  tsubai 		pmdata.data[0] = 0x00;
   1075   1.1  tsubai 		pmdata.data[1] = 0x86;	/* magic spell for awaking the PM */
   1076   1.1  tsubai 		pmdata.data[2] = 0x00;
   1077   1.1  tsubai 		pmdata.data[3] = 0x0c;	/* each bit may express the existent ADB device */
   1078   1.1  tsubai 	} else {				/* PB 1XX series */
   1079   1.1  tsubai 		pmdata.command = 0x20;
   1080   1.1  tsubai 		pmdata.num_data = 3;
   1081   1.1  tsubai 		pmdata.s_buf = pmdata.data;
   1082   1.1  tsubai 		pmdata.r_buf = pmdata.data;
   1083   1.1  tsubai 		pmdata.data[0] = (u_char)(command & 0xf0) | 0xc;
   1084   1.1  tsubai 		pmdata.data[1] = 0x04;
   1085   1.1  tsubai 		pmdata.data[2] = 0x00;
   1086   1.1  tsubai 	}
   1087   1.1  tsubai 	rval = pmgrop(&pmdata);
   1088   1.1  tsubai 
   1089   1.1  tsubai 	splx(s);
   1090   1.1  tsubai 	return rval;
   1091   1.1  tsubai }
   1092   1.1  tsubai 
   1093   1.1  tsubai 
   1094   1.1  tsubai void
   1095   1.1  tsubai pm_adb_get_TALK_result(pmdata)
   1096   1.1  tsubai 	PMData *pmdata;
   1097   1.1  tsubai {
   1098   1.1  tsubai 	int i;
   1099   1.1  tsubai 	struct adbCommand packet;
   1100   1.1  tsubai 
   1101   1.1  tsubai 	/* set up data for adb_pass_up */
   1102   1.1  tsubai 	packet.data[0] = pmdata->num_data-1;
   1103   1.1  tsubai 	packet.data[1] = pmdata->data[3];
   1104   1.1  tsubai 	for (i = 0; i <packet.data[0]-1; i++)
   1105   1.1  tsubai 		packet.data[i+2] = pmdata->data[i+4];
   1106   1.1  tsubai 
   1107   1.1  tsubai 	packet.saveBuf = adbBuffer;
   1108   1.1  tsubai 	packet.compRout = adbCompRout;
   1109   1.1  tsubai 	packet.compData = adbCompData;
   1110   1.1  tsubai 	packet.unsol = 0;
   1111   1.1  tsubai 	packet.ack_only = 0;
   1112   1.1  tsubai 	adb_polling = 1;
   1113   1.1  tsubai 	adb_pass_up(&packet);
   1114   1.1  tsubai 	adb_polling = 0;
   1115   1.1  tsubai 
   1116   1.1  tsubai 	adbWaiting = 0;
   1117   1.1  tsubai 	adbBuffer = (long)0;
   1118   1.1  tsubai 	adbCompRout = (long)0;
   1119   1.1  tsubai 	adbCompData = (long)0;
   1120   1.1  tsubai }
   1121   1.1  tsubai 
   1122   1.1  tsubai 
   1123   1.1  tsubai void
   1124   1.1  tsubai pm_adb_get_ADB_data(pmdata)
   1125   1.1  tsubai 	PMData *pmdata;
   1126   1.1  tsubai {
   1127   1.1  tsubai 	int i;
   1128   1.1  tsubai 	struct adbCommand packet;
   1129   1.1  tsubai 
   1130   1.1  tsubai 	/* set up data for adb_pass_up */
   1131   1.1  tsubai 	packet.data[0] = pmdata->num_data-1;	/* number of raw data */
   1132   1.1  tsubai 	packet.data[1] = pmdata->data[3];	/* ADB command */
   1133   1.1  tsubai 	for (i = 0; i <packet.data[0]-1; i++)
   1134   1.1  tsubai 		packet.data[i+2] = pmdata->data[i+4];
   1135   1.1  tsubai 	packet.unsol = 1;
   1136   1.1  tsubai 	packet.ack_only = 0;
   1137   1.1  tsubai 	adb_pass_up(&packet);
   1138   1.1  tsubai }
   1139   1.1  tsubai 
   1140   1.1  tsubai 
   1141   1.1  tsubai void
   1142   1.1  tsubai pm_adb_poll_next_device_pm1(pmdata)
   1143   1.1  tsubai 	PMData *pmdata;
   1144   1.1  tsubai {
   1145   1.1  tsubai 	int i;
   1146   1.1  tsubai 	int ndid;
   1147   1.1  tsubai 	u_short bendid = 0x1;
   1148   1.1  tsubai 	int rval;
   1149   1.1  tsubai 	PMData tmp_pmdata;
   1150   1.1  tsubai 
   1151   1.1  tsubai 	/* find another existent ADB device to poll */
   1152   1.1  tsubai 	for (i = 1; i < 16; i++) {
   1153   1.9  tsubai 		ndid = (ADB_CMDADDR(pmdata->data[3]) + i) & 0xf;
   1154   1.1  tsubai 		bendid <<= ndid;
   1155   1.1  tsubai 		if ((pm_existent_ADB_devices & bendid) != 0)
   1156   1.1  tsubai 			break;
   1157   1.1  tsubai 	}
   1158   1.1  tsubai 
   1159   1.1  tsubai 	/* poll the other device */
   1160   1.1  tsubai 	tmp_pmdata.command = 0x20;
   1161   1.1  tsubai 	tmp_pmdata.num_data = 3;
   1162   1.1  tsubai 	tmp_pmdata.s_buf = tmp_pmdata.data;
   1163   1.1  tsubai 	tmp_pmdata.r_buf = tmp_pmdata.data;
   1164   1.1  tsubai 	tmp_pmdata.data[0] = (u_char)(ndid << 4) | 0xc;
   1165   1.1  tsubai 	tmp_pmdata.data[1] = 0x04;	/* magic spell for awaking the PM */
   1166   1.1  tsubai 	tmp_pmdata.data[2] = 0x00;
   1167   1.1  tsubai 	rval = pmgrop(&tmp_pmdata);
   1168   1.1  tsubai }
   1169   1.1  tsubai 
   1170   1.1  tsubai void
   1171   1.1  tsubai pm_adb_restart()
   1172   1.1  tsubai {
   1173   1.1  tsubai 	PMData p;
   1174   1.1  tsubai 
   1175   1.4  tsubai 	p.command = PMU_RESET_CPU;
   1176   1.1  tsubai 	p.num_data = 0;
   1177   1.1  tsubai 	p.s_buf = p.data;
   1178   1.1  tsubai 	p.r_buf = p.data;
   1179   1.6  tsubai 	pmgrop(&p);
   1180   1.6  tsubai }
   1181   1.6  tsubai 
   1182   1.6  tsubai void
   1183   1.6  tsubai pm_adb_poweroff()
   1184   1.6  tsubai {
   1185   1.6  tsubai 	PMData p;
   1186   1.6  tsubai 
   1187   1.6  tsubai 	p.command = PMU_POWER_OFF;
   1188   1.6  tsubai 	p.num_data = 4;
   1189   1.6  tsubai 	p.s_buf = p.data;
   1190   1.6  tsubai 	p.r_buf = p.data;
   1191   1.6  tsubai 	strcpy(p.data, "MATT");
   1192   1.1  tsubai 	pmgrop(&p);
   1193   1.2  tsubai }
   1194   1.2  tsubai 
   1195   1.2  tsubai void
   1196   1.2  tsubai pm_read_date_time(time)
   1197   1.2  tsubai 	u_long *time;
   1198   1.2  tsubai {
   1199   1.2  tsubai 	PMData p;
   1200   1.2  tsubai 
   1201   1.4  tsubai 	p.command = PMU_READ_RTC;
   1202   1.2  tsubai 	p.num_data = 0;
   1203   1.2  tsubai 	p.s_buf = p.data;
   1204   1.2  tsubai 	p.r_buf = p.data;
   1205   1.2  tsubai 	pmgrop(&p);
   1206   1.2  tsubai 
   1207  1.13     wiz 	memcpy(time, p.data, 4);
   1208   1.4  tsubai }
   1209   1.4  tsubai 
   1210   1.4  tsubai void
   1211   1.4  tsubai pm_set_date_time(time)
   1212   1.4  tsubai 	u_long time;
   1213   1.4  tsubai {
   1214   1.4  tsubai 	PMData p;
   1215   1.4  tsubai 
   1216   1.4  tsubai 	p.command = PMU_SET_RTC;
   1217   1.4  tsubai 	p.num_data = 4;
   1218   1.4  tsubai 	p.s_buf = p.r_buf = p.data;
   1219  1.13     wiz 	memcpy(p.data, &time, 4);
   1220   1.7  tsubai 	pmgrop(&p);
   1221   1.7  tsubai }
   1222   1.7  tsubai 
   1223   1.7  tsubai int
   1224   1.7  tsubai pm_read_brightness()
   1225   1.7  tsubai {
   1226   1.7  tsubai 	PMData p;
   1227   1.7  tsubai 
   1228   1.7  tsubai 	p.command = PMU_READ_BRIGHTNESS;
   1229   1.7  tsubai 	p.num_data = 1;		/* XXX why 1? */
   1230   1.7  tsubai 	p.s_buf = p.r_buf = p.data;
   1231   1.7  tsubai 	p.data[0] = 0;
   1232   1.7  tsubai 	pmgrop(&p);
   1233   1.7  tsubai 
   1234   1.7  tsubai 	return p.data[0];
   1235   1.7  tsubai }
   1236   1.7  tsubai 
   1237   1.7  tsubai void
   1238   1.7  tsubai pm_set_brightness(val)
   1239   1.7  tsubai 	int val;
   1240   1.7  tsubai {
   1241   1.7  tsubai 	PMData p;
   1242   1.7  tsubai 
   1243   1.7  tsubai 	val = 0x7f - val / 2;
   1244   1.7  tsubai 	if (val < 0x08)
   1245   1.7  tsubai 		val = 0x08;
   1246   1.7  tsubai 	if (val > 0x78)
   1247   1.7  tsubai 		val = 0x78;
   1248   1.7  tsubai 
   1249   1.7  tsubai 	p.command = PMU_SET_BRIGHTNESS;
   1250   1.7  tsubai 	p.num_data = 1;
   1251   1.7  tsubai 	p.s_buf = p.r_buf = p.data;
   1252   1.7  tsubai 	p.data[0] = val;
   1253   1.7  tsubai 	pmgrop(&p);
   1254   1.7  tsubai }
   1255   1.7  tsubai 
   1256   1.7  tsubai void
   1257   1.7  tsubai pm_init_brightness()
   1258   1.7  tsubai {
   1259   1.7  tsubai 	int val;
   1260   1.7  tsubai 
   1261   1.7  tsubai 	val = pm_read_brightness();
   1262   1.7  tsubai 	pm_set_brightness(val);
   1263   1.7  tsubai }
   1264   1.7  tsubai 
   1265   1.7  tsubai void
   1266   1.7  tsubai pm_eject_pcmcia(slot)
   1267   1.7  tsubai 	int slot;
   1268   1.7  tsubai {
   1269   1.7  tsubai 	PMData p;
   1270   1.7  tsubai 
   1271   1.7  tsubai 	if (slot != 0 && slot != 1)
   1272   1.7  tsubai 		return;
   1273   1.7  tsubai 
   1274   1.7  tsubai 	p.command = PMU_EJECT_PCMCIA;
   1275   1.7  tsubai 	p.num_data = 1;
   1276   1.7  tsubai 	p.s_buf = p.r_buf = p.data;
   1277   1.8  tsubai 	p.data[0] = 5 + slot;	/* XXX */
   1278   1.5  tsubai 	pmgrop(&p);
   1279   1.5  tsubai }
   1280   1.5  tsubai 
   1281   1.5  tsubai int
   1282   1.5  tsubai pm_read_nvram(addr)
   1283   1.5  tsubai 	int addr;
   1284   1.5  tsubai {
   1285   1.5  tsubai 	PMData p;
   1286   1.5  tsubai 
   1287   1.5  tsubai 	p.command = PMU_READ_NVRAM;
   1288   1.5  tsubai 	p.num_data = 2;
   1289   1.5  tsubai 	p.s_buf = p.r_buf = p.data;
   1290   1.5  tsubai 	p.data[0] = addr >> 8;
   1291   1.5  tsubai 	p.data[1] = addr;
   1292   1.5  tsubai 	pmgrop(&p);
   1293   1.5  tsubai 
   1294   1.5  tsubai 	return p.data[0];
   1295   1.5  tsubai }
   1296   1.5  tsubai 
   1297   1.5  tsubai void
   1298   1.5  tsubai pm_write_nvram(addr, val)
   1299   1.5  tsubai 	int addr, val;
   1300   1.5  tsubai {
   1301   1.5  tsubai 	PMData p;
   1302   1.5  tsubai 
   1303   1.5  tsubai 	p.command = PMU_WRITE_NVRAM;
   1304   1.5  tsubai 	p.num_data = 3;
   1305   1.5  tsubai 	p.s_buf = p.r_buf = p.data;
   1306   1.5  tsubai 	p.data[0] = addr >> 8;
   1307   1.5  tsubai 	p.data[1] = addr;
   1308   1.5  tsubai 	p.data[2] = val;
   1309   1.4  tsubai 	pmgrop(&p);
   1310   1.1  tsubai }
   1311