Import upstream u-boot 1.1.4
[u-boot.git] / board / MAI / bios_emulator / scitech / src / pm / ntdrv / irq.c
1 /****************************************************************************
2 *
3 *                   SciTech OS Portability Manager Library
4 *
5 *  ========================================================================
6 *
7 *    The contents of this file are subject to the SciTech MGL Public
8 *    License Version 1.0 (the "License"); you may not use this file
9 *    except in compliance with the License. You may obtain a copy of
10 *    the License at http://www.scitechsoft.com/mgl-license.txt
11 *
12 *    Software distributed under the License is distributed on an
13 *    "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 *    implied. See the License for the specific language governing
15 *    rights and limitations under the License.
16 *
17 *    The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
18 *
19 *    The Initial Developer of the Original Code is SciTech Software, Inc.
20 *    All Rights Reserved.
21 *
22 *  ========================================================================
23 *
24 * Language:     ANSI C
25 * Environment:  32-bit Windows NT device drivers.
26 *
27 * Description:  Implementation for the NT driver IRQ management functions
28 *               for the PM library.
29 *
30 ****************************************************************************/
31
32 #include "pmapi.h"
33 #include "pmint.h"
34 #include "drvlib/os/os.h"
35 #include "sdd/sddhelp.h"
36 #include "mtrr.h"
37 #include "oshdr.h"
38
39 /*--------------------------- Global variables ----------------------------*/
40
41 static int          globalDataStart;
42 static uchar        _PM_oldCMOSRegA;
43 static uchar        _PM_oldCMOSRegB;
44 static uchar        _PM_oldRTCPIC2;
45 static ulong        RTC_idtEntry;
46 PM_intHandler       _PM_rtcHandler = NULL;
47 PMFARPTR    _VARAPI _PM_prevRTC = PMNULL;
48
49 /*----------------------------- Implementation ----------------------------*/
50
51 /* Functions to read and write CMOS registers */
52
53 uchar   _ASMAPI _PM_readCMOS(int index);
54 void    _ASMAPI _PM_writeCMOS(int index,uchar value);
55 void    _ASMAPI _PM_rtcISR(void);
56 void    _ASMAPI _PM_getISR(int irq,PMFARPTR *handler);
57 void    _ASMAPI _PM_setISR(int irq,void *handler);
58 void    _ASMAPI _PM_restoreISR(int irq,PMFARPTR *handler);
59 void    _ASMAPI _PM_irqCodeStart(void);
60 void    _ASMAPI _PM_irqCodeEnd(void);
61
62 /****************************************************************************
63 REMARKS:
64 Set the real time clock frequency (for stereo modes).
65 ****************************************************************************/
66 void PMAPI PM_setRealTimeClockFrequency(
67     int frequency)
68 {
69     static short convert[] = {
70         8192,
71         4096,
72         2048,
73         1024,
74         512,
75         256,
76         128,
77         64,
78         32,
79         16,
80         8,
81         4,
82         2,
83         -1,
84         };
85     int i;
86
87     /* First clear any pending RTC timeout if not cleared */
88     _PM_readCMOS(0x0C);
89     if (frequency == 0) {
90         /* Disable RTC timout */
91         _PM_writeCMOS(0x0A,(uchar)_PM_oldCMOSRegA);
92         _PM_writeCMOS(0x0B,(uchar)(_PM_oldCMOSRegB & 0x0F));
93         }
94     else {
95         /* Convert frequency value to RTC clock indexes */
96         for (i = 0; convert[i] != -1; i++) {
97             if (convert[i] == frequency)
98                 break;
99             }
100
101         /* Set RTC timout value and enable timeout */
102         _PM_writeCMOS(0x0A,(uchar)(0x20 | (i+3)));
103         _PM_writeCMOS(0x0B,(uchar)((_PM_oldCMOSRegB & 0x0F) | 0x40));
104         }
105 }
106
107 ibool PMAPI PM_setRealTimeClockHandler(PM_intHandler th,int frequency)
108 {
109     static ibool    locked = false;
110
111     /* Save the old CMOS real time clock values */
112     _PM_oldCMOSRegA = _PM_readCMOS(0x0A);
113     _PM_oldCMOSRegB = _PM_readCMOS(0x0B);
114
115     /* Install the interrupt handler */
116     RTC_idtEntry = 0x38;
117     _PM_getISR(RTC_idtEntry, &_PM_prevRTC);
118     _PM_rtcHandler = th;
119     _PM_setISR(RTC_idtEntry, _PM_rtcISR);
120
121     /* Program the real time clock default frequency */
122     PM_setRealTimeClockFrequency(frequency);
123
124     /* Unmask IRQ8 in the PIC2 */
125     _PM_oldRTCPIC2 = PM_inpb(0xA1);
126     PM_outpb(0xA1,(uchar)(_PM_oldRTCPIC2 & 0xFE));
127     return true;
128 }
129
130 void PMAPI PM_restoreRealTimeClockHandler(void)
131 {
132     if (_PM_rtcHandler) {
133         /* Restore CMOS registers and mask RTC clock */
134         _PM_writeCMOS(0x0A,_PM_oldCMOSRegA);
135         _PM_writeCMOS(0x0B,_PM_oldCMOSRegB);
136         PM_outpb(0xA1,(uchar)((PM_inpb(0xA1) & 0xFE) | (_PM_oldRTCPIC2 & ~0xFE)));
137
138         /* Restore the interrupt vector */
139         _PM_restoreISR(RTC_idtEntry, &_PM_prevRTC);
140         _PM_rtcHandler = NULL;
141         }
142 }