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