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