Annotation of kernel/bsd/dev/ppc/PowerSurgeMB.m, revision 1.1.1.2

1.1       root        1: /*
                      2:  * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
                      3:  *
                      4:  * @APPLE_LICENSE_HEADER_START@
                      5:  * 
                      6:  * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
                      7:  * Reserved.  This file contains Original Code and/or Modifications of
                      8:  * Original Code as defined in and that are subject to the Apple Public
                      9:  * Source License Version 1.1 (the "License").  You may not use this file
                     10:  * except in compliance with the License.  Please obtain a copy of the
                     11:  * License at http://www.apple.com/publicsource and read it before using
                     12:  * this file.
                     13:  * 
                     14:  * The Original Code and all software distributed under the License are
                     15:  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
                     16:  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
                     17:  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
                     18:  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
                     19:  * License for the specific language governing rights and limitations
                     20:  * under the License.
                     21:  * 
                     22:  * @APPLE_LICENSE_HEADER_END@
                     23:  */
                     24: 
                     25: /*     Copyright (c) 1997 Apple Computer, Inc.  All rights reserved. 
                     26:  *
                     27:  * PowerSurgeMB.c
                     28:  *
                     29:  * 20-May-97   Simon Douglas
                     30:  *      Created.
                     31:  */
                     32: 
                     33: #include <mach/mach_types.h>
                     34: #include <machdep/ppc/proc_reg.h>
                     35: #include <machdep/ppc/powermac.h>
                     36: #import <driverkit/IODevice.h>
                     37: #include "busses.h"
                     38: #import <bsd/dev/ppc/drvPMU/pmu.h>
                     39: #import <bsd/dev/ppc/drvPMU/pmupriv.h>
                     40: #import <bsd/dev/ppc/drvPMU/pmumisc.h>
                     41: 
                     42: 
                     43: // seconds between 1904 (Mac) & 1970 (UNIX) - many.
                     44: #define JAN11970       0x7c25b080
                     45: // MacOS xpram var for local secs from GMT
                     46: #define MACTIMEZONE    0xec
                     47: 
                     48: #define printf kprintf
                     49: 
                     50: void MBCallback(id unused, UInt32 refnum, UInt32 length, UInt8* buffer);
                     51: void wait_for_callback(void);
                     52: 
                     53: thread_t  our_thread;
                     54: id ApplePMUId;         // loaded driver
                     55: 
                     56: 
                     57: IOReturn
                     58: cuda_time_of_day( unsigned int * secs, int set )
                     59: {
                     60:     unsigned int  newTime;
                     61: 
                     62: kprintf("cuda_time_of_day: ApplePMUId = %08x, secs = %08x, *secs = %08x, set = %08x\n",
                     63:        ApplePMUId, secs, *secs, set);
                     64: 
                     65:     if ( set == 0 ) {
                     66:       if ( ApplePMUId == NULL ) {
                     67:                *secs = JAN11970;
                     68:       } else {
                     69:                [ApplePMUId getRealTimeClock:(UInt8 *)&newTime :0 :0 :MBCallback];
                     70:                wait_for_callback();
                     71:        
                     72:                *secs = newTime;
                     73: kprintf("newTime =%x\n", newTime);
                     74:       }
                     75:     } else {
                     76:       if ( ApplePMUId != NULL ) {
                     77:                newTime = *secs;
                     78: 
                     79:                [ApplePMUId setRealTimeClock:(UInt8 *)&newTime :0 :0 :MBCallback];
                     80:                wait_for_callback();
                     81:       }
                     82:     }
                     83:     
                     84:     return 0;
                     85: }
                     86: 
                     87: 
                     88: void
                     89: cuda_restart(int powerOff)
                     90: {
                     91:     unsigned char commandBuffer[4] = {'M', 'A', 'T', 'T'};
                     92:     unsigned char inputBuffer[1];
                     93:     
                     94:     kprintf("cuda_restart\n");
                     95:     
                     96:     if ( ApplePMUId == NULL ) {
                     97:       kprintf("cuda_restart, no PMU driver\n");
                     98:     } else {
                     99:       if( powerOff ) {
                    100:        [ApplePMUId sendMiscCommand:kPMUPmgrPWRoff :4 :commandBuffer :inputBuffer :0 :0 :MBCallback];
                    101:       } else {
                    102:        [ApplePMUId sendMiscCommand:kPMUresetCPU :0 :NULL :NULL :0 :0 :MBCallback];
                    103:       }
                    104:       wait_for_callback();
                    105:     }
                    106: }
                    107: 
                    108: void PowerSurgeSendIIC( unsigned char iicAddr, unsigned char iicReg, unsigned char iicData)
                    109: {
                    110: #if 0 //No adb_send() in adb.c any more
                    111:     adb_request_t   cmd;
                    112: 
                    113:     adb_init_request(&cmd);
                    114:     cmd.a_cmd.a_header[0] = ADB_PACKET_PSEUDO;
                    115:     cmd.a_cmd.a_header[1] = ADB_PSEUDOCMD_GET_SET_IIC;
                    116:     cmd.a_cmd.a_header[2] = iicAddr;
                    117:     cmd.a_cmd.a_header[3] = iicReg;
                    118:     cmd.a_cmd.a_header[4] = iicData;
                    119:     cmd.a_cmd.a_hcount = 5;
                    120:     adb_send(&cmd, TRUE);
                    121: #endif
                    122:     UInt8       buffer[5] = {   1,
                    123:                     0x22,      //ADB_PSEUDOCMD_GET_SET_IIC in adb.h 
                    124:                     iicAddr, 
                    125:                     iicReg,
                    126:                     iicData};
                    127: 
                    128:     if ( ApplePMUId != NULL ) {       //A.W. combined from Titan C
                    129:         // old, Simon says is buggy [ApplePMUId CudaMisc:buffer:5:NULL:0:NULL];
                    130:         [ApplePMUId CudaMisc:buffer:5:(UInt32)current_thread() : 0: MBCallback];
                    131: 
                    132:                wait_for_callback();
                    133:        }
                    134: 
                    135: }
                    136: 
                    137: // Can't be used anymore...
                    138: #if 0
                    139: enum {
                    140:     kXPRAMNVPartition          = 0x1300,
                    141:     kNameRegistryNVPartition   = 0x1400,
                    142:     kOpenFirmwareNVPartition   = 0x1800,
                    143: };
                    144: #endif
                    145: 
1.1.1.2 ! root      146: 
        !           147: static long Core99NVRAMInited;
        !           148: static char Core99NVRAM[0x2000];
        !           149: 
        !           150: IOReturn InitCore99NVRAM(void)
        !           151: {
        !           152:   volatile unsigned char *nvAddrReg =
        !           153:     (volatile unsigned char *) (POWERMAC_IO(PCI_NVRAM_ADDR_PHYS));
        !           154:   
        !           155:   kprintf("InitCore99NVRAM: nvAddrReg = %x\n", nvAddrReg);
        !           156: 
        !           157:   bcopy(nvAddrReg, Core99NVRAM, 0x2000);
        !           158:   
        !           159:   Core99NVRAMInited = 1;
        !           160:   
        !           161:   return 0;
        !           162: }
        !           163: 
1.1       root      164: IOReturn
                    165: ReadNVRAM( unsigned int offset, unsigned int length, unsigned char * buffer )
                    166: {
                    167:   volatile unsigned char *nvAddrReg =
                    168:     (volatile unsigned char *) (POWERMAC_IO(PCI_NVRAM_ADDR_PHYS));
                    169:   volatile unsigned char *nvDataReg =
                    170:     (volatile unsigned char *) (POWERMAC_IO(PCI_NVRAM_DATA_PHYS));
                    171: int    i;
                    172: 
                    173:     if( offset + length > 0x2000 )
                    174:        return( IO_R_UNSUPPORTED);
                    175: 
1.1.1.2 ! root      176:     if (IsSawtooth()) {
        !           177:       if (!Core99NVRAMInited && (InitCore99NVRAM != 0)) return -1;
        !           178:       for (i = 0; i < length; i++) {
        !           179:        buffer[i] = Core99NVRAM[offset + i];
        !           180:       }
        !           181:     } else if (HasPMU()) {
1.1       root      182:       // This is a powerbook
                    183:       if (ApplePMUId == NULL) {
                    184:        for ( i = 0; i < length; i++ ) {
                    185:          buffer[i] = 0;
                    186:        }
                    187:       } else {
                    188:        [ApplePMUId readNVRAM: offset: length: buffer: 0: 0: MBCallback];
                    189:        wait_for_callback();
                    190:       }
                    191:     } else {
                    192:       // This is a desktop mac
                    193:       if (IsPowerSurge()) {
                    194:         for( i = 0; i < length; i++) {
                    195:          *nvAddrReg = (0xff & ((offset + i) >> 5));
                    196:          eieio();
                    197:          buffer[i] = *(nvDataReg + 16 * ((offset + i) & 0x1f));
                    198:          eieio();
                    199:         }
                    200:       } else {
                    201:         for (i = 0; i < length; i++) {
                    202:          buffer[i] = nvAddrReg[(offset + i) * 16];
                    203:         }
                    204:       }
                    205:     }
                    206: 
                    207:     return 0;
                    208: }
                    209: 
                    210: IOReturn
                    211: WriteNVRAM( unsigned int offset, unsigned int length, unsigned char * buffer )
                    212: {
                    213:   volatile unsigned char *nvAddrReg =
                    214:     (volatile unsigned char *) (POWERMAC_IO(PCI_NVRAM_ADDR_PHYS));
                    215:   volatile unsigned char *nvDataReg =
                    216:     (volatile unsigned char *) (POWERMAC_IO(PCI_NVRAM_DATA_PHYS));
                    217: int    i;
                    218: 
                    219:     if( offset + length > 0x2000 )
                    220:        return( IO_R_UNSUPPORTED);
                    221: 
1.1.1.2 ! root      222:     if (IsSawtooth()) {
        !           223:       if (!Core99NVRAMInited && (InitCore99NVRAM != 0)) return -1;
        !           224:       for (i = 0; i < length; i++) {
        !           225:        Core99NVRAM[offset + i] = buffer[i];
        !           226:       }
        !           227:     } if (HasPMU()) { 
1.1       root      228:       // This is a powerbook
                    229:       if (ApplePMUId == NULL) {
                    230:       } else {
                    231:        [ApplePMUId writeNVRAM: offset: length: buffer: 0: 0: MBCallback];
                    232:        wait_for_callback();
                    233:       }
                    234:     } else {
                    235:       // This is a desktop mac
                    236:       if (IsPowerSurge()) {
                    237:         for( i = 0; i < length; i++) {
                    238:          *nvAddrReg = (0xff & ((offset + i) >> 5));
                    239:          eieio();
                    240:          *(nvDataReg + 16 * ((offset + i) & 0x1f)) = buffer[i];
                    241:          eieio();
                    242:         }
                    243:       } else {
                    244:         for( i = 0; i < length; i++) {
                    245:          nvAddrReg[(offset + i) * 16] = buffer[i];
                    246:         }
                    247:       }
                    248:     }
                    249: 
                    250:     return 0;
                    251: }
                    252: 
                    253: 
                    254: int
                    255: GetMacOSTimeZone( void )
                    256: {
                    257:     int                        macZone;
                    258:     IOReturn           err;
                    259: 
                    260:     err = ReadNVRAM( NVRAM_XPRAM_Offset + MACTIMEZONE, 4,
                    261:                     (unsigned char *)&macZone);
                    262: 
                    263:     printf( " macZone: %x",macZone );
                    264: 
                    265:     if( err == 0) {
                    266:        if( macZone & 0x00800000)
                    267:            macZone |= 0xff000000;      // sign ext 24->32
                    268:        else
                    269:            macZone &= 0x00ffffff;      // sign ext 24->32
                    270:     } else
                    271:        macZone = 0;
                    272:     return macZone;
                    273: }
                    274: 
                    275: unsigned int
                    276: get_unix_time_of_day( void )
                    277: {
                    278:     int                        secs, macZone;
                    279:     int                        err;
                    280: 
                    281:     err = cuda_time_of_day( &secs, FALSE );
                    282: /*     printf( "TOD secs: %x,",secs ); */
                    283:     secs -= (JAN11970 + GetMacOSTimeZone());
                    284: /*     printf( " = Mac OS X Server: %x\n",secs ); */
                    285:     return( secs );
                    286: }
                    287: 
                    288: void
                    289: set_unix_time_of_day( unsigned int unixSecs )
                    290: {
                    291:     int                        secs, macZone;
                    292:     int                        err;
                    293: 
                    294: /*     printf( "Mac OS X Server: %x,", unixSecs ); */
                    295:     secs = unixSecs + JAN11970 + GetMacOSTimeZone();
                    296: /*     printf( " = TOD secs: %x\n",secs ); */
                    297:    err = cuda_time_of_day( &secs, TRUE );
                    298:    return;
                    299: }
                    300: 
                    301: void MBCallback(id unused, UInt32 refnum, UInt32 length, UInt8* buffer)
                    302: {
                    303:     clear_wait(our_thread,0,FALSE);
                    304: }
                    305: 
                    306: 
                    307: // We have sent a command to the PMU driver.  Sleep until it has sent the
                    308: // command to the PMU.
                    309: void wait_for_callback(void)
                    310: {
                    311:     our_thread = current_thread();
                    312:     assert_wait(our_thread, FALSE);
                    313:     thread_block();
                    314: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.