#include <ppc/proc_reg.h>
#include <ppc/machine_routines.h>
#include <IOKit/IODeviceTreeSupport.h>
#include <IOKit/IOKitKeys.h>
#include "MacRISC2.h"
#include <IOKit/pci/IOPCIDevice.h>
static unsigned long macRISC2Speed[] = { 0, 1 };
#include <IOKit/pwr_mgt/RootDomain.h>
#include "IOPMSlotsMacRISC2.h"
#include "IOPMUSBMacRISC2.h"
extern char *gIOMacRISC2PMTree;
#define super ApplePlatformExpert
OSDefineMetaClassAndStructors(MacRISC2PE, ApplePlatformExpert);
bool MacRISC2PE::start(IOService *provider)
{
long machineType;
OSData *tmpData;
IORegistryEntry *uniNRegEntry;
IORegistryEntry *powerMgtEntry;
unsigned long *primInfo;
unsigned long uniNArbCtrl, uniNBaseAddressTemp;
const char *provider_name;
setChipSetType(kChipSetTypeCore2001);
provider_name = provider->getName();
if ( (provider_name != NULL) && (0 == strncmp(provider_name, "PowerMac", strlen("PowerMac"))) )
{
machineType = kMacRISC2TypePowerMac;
kDoFrameBufferDeepSleep = true;
}
if ( (provider_name != NULL) && (0 == strncmp(provider_name, "PowerBook", strlen("PowerBook"))) )
{
machineType = kMacRISC2TypePowerBook;
kDoFrameBufferDeepSleep = false;
}
setMachineType(machineType);
tmpData = OSDynamicCast(OSData, provider->getProperty("clock-frequency"));
if (tmpData == 0) return false;
macRISC2Speed[0] = *(unsigned long *)tmpData->getBytesNoCopy();
uniNRegEntry = provider->childFromPath("uni-n", gIODTPlane);
if (uniNRegEntry == 0) return false;
tmpData = OSDynamicCast(OSData, uniNRegEntry->getProperty("reg"));
if (tmpData == 0) return false;
uniNBaseAddressTemp = ((unsigned long *)tmpData->getBytesNoCopy())[0];
uniNBaseAddress = (unsigned long *)ml_io_map(uniNBaseAddressTemp, 0x1000);
if (uniNBaseAddress == 0) return false;
uniNVersion = readUniNReg(kUniNVersion);
uniNArbCtrl = readUniNReg(kUniNArbCtrl);
uniNArbCtrl &= ~kUniNArbCtrlQAckDelayMask;
if (uniNVersion < kUniNVersion107)
{
uniNArbCtrl |= kUniNArbCtrlQAckDelay105 << kUniNArbCtrlQAckDelayShift;
}
else
{
uniNArbCtrl |= kUniNArbCtrlQAckDelay << kUniNArbCtrlQAckDelayShift;
}
writeUniNReg(kUniNArbCtrl, uniNArbCtrl);
IOService *uniNServiceEntry = OSDynamicCast(IOService, uniNRegEntry);
if (uniNServiceEntry != NULL)
createNubs(this, uniNRegEntry->getChildIterator( gIODTPlane ));
powerMgtEntry = retrievePowerMgtEntry ();
if (powerMgtEntry == 0)
{
kprintf ("didn't find power mgt node\n");
return false;
}
tmpData = OSDynamicCast(OSData, powerMgtEntry->getProperty ("prim-info"));
if (tmpData != 0)
{
primInfo = (unsigned long *)tmpData->getBytesNoCopy();
if (primInfo != 0)
{
_pePMFeatures = primInfo[3];
_pePrivPMFeatures = primInfo[4];
_peNumBatteriesSupported = ((primInfo[6]>>16) & 0x000000FF);
kprintf ("Public PM Features: %0x.\n",_pePMFeatures);
kprintf ("Privat PM Features: %0x.\n",_pePrivPMFeatures);
kprintf ("Num Internal Batteries Supported: %0x.\n", _peNumBatteriesSupported);
}
}
mutex = IOLockAlloc();
if (mutex == NULL) {
return false;
}
else
IOLockInit( mutex );
return super::start(provider);
}
IORegistryEntry * MacRISC2PE::retrievePowerMgtEntry (void)
{
IORegistryEntry * theEntry = 0;
IORegistryEntry * anObj = 0;
IORegistryIterator * iter;
OSString * powerMgtNodeName;
iter = IORegistryIterator::iterateOver (IORegistryEntry::getPlane(kIODeviceTreePlane), kIORegistryIterateRecursively);
if (iter)
{
powerMgtNodeName = OSString::withCString("power-mgt");
anObj = iter->getNextObject ();
while (anObj)
{
if (anObj->compareName(powerMgtNodeName))
{
theEntry = anObj;
break;
}
anObj = iter->getNextObject();
}
powerMgtNodeName->release();
iter->release ();
}
return theEntry;
}
bool MacRISC2PE::platformAdjustService(IOService *service)
{
const OSSymbol *tmpSymbol, *keySymbol;
bool result;
if (IODTMatchNubWithKeys(service, "open-pic"))
{
keySymbol = OSSymbol::withCStringNoCopy("InterruptControllerName");
tmpSymbol = IODTInterruptControllerName(service);
result = service->setProperty(keySymbol, tmpSymbol);
return true;
}
if (!strcmp(service->getName(), "programmer-switch"))
{
service->setProperty("mask_NMI", service);
return true;
}
if (!strcmp(service->getName(), "pmu"))
{
OSArray *tmpArray;
OSCollectionIterator *extIntList;
IORegistryEntry *extInt;
OSObject *extIntControllerName;
OSObject *extIntControllerData;
service->setProperty("no-nvram", service);
extIntList = IODTFindMatchingEntries(getProvider(), kIODTRecursive, "'extint-gpio1'");
extInt = (IORegistryEntry *)extIntList->getNextObject();
tmpArray = (OSArray *)extInt->getProperty(gIOInterruptControllersKey);
extIntControllerName = tmpArray->getObject(0);
tmpArray = (OSArray *)extInt->getProperty(gIOInterruptSpecifiersKey);
extIntControllerData = tmpArray->getObject(0);
tmpArray = (OSArray *)service->getProperty(gIOInterruptControllersKey);
tmpArray->replaceObject(4, extIntControllerName);
tmpArray = (OSArray *)service->getProperty(gIOInterruptSpecifiersKey);
tmpArray->replaceObject(4, extIntControllerData);
extIntList->release();
return true;
}
if (!strcmp(service->getName(), "ATY,RageM3p12Parent"))
{
if (kIOReturnSuccess == IONDRVLibrariesInitialize(service))
{
createNubs(this, service->getChildIterator( gIODTPlane ));
}
return true;
}
if (!strcmp(service->getName(), "ATY,RageM3p29Parent"))
{
if (kIOReturnSuccess == IONDRVLibrariesInitialize(service))
{
createNubs(this, service->getChildIterator( gIODTPlane ));
}
return true;
}
if (!strcmp(service->getName(), "via-pmu"))
{
service->setProperty("BusSpeedCorrect", this);
return true;
}
if ( strcmp(service->getName(), "usb") == 0 )
{
service->setProperty("USBclock","");
return true;
}
return true;
}
IOReturn MacRISC2PE::callPlatformFunction(const OSSymbol *functionName,
bool waitForFunction,
void *param1, void *param2,
void *param3, void *param4)
{
if (functionName == gGetDefaultBusSpeedsKey)
{
getDefaultBusSpeeds((long *)param1, (unsigned long **)param2);
return kIOReturnSuccess;
}
if (functionName->isEqualTo("EnableUniNEthernetClock"))
{
enableUniNEthernetClock((bool)param1);
return kIOReturnSuccess;
}
if (functionName->isEqualTo("EnableFireWireClock")) {
enableUniNFireWireClock((bool)param1);
return kIOReturnSuccess;
}
if (functionName->isEqualTo("EnableFireWireCablePower")) {
enableUniNFireWireCablePower((bool)param1);
return kIOReturnSuccess;
}
return super::callPlatformFunction(functionName, waitForFunction, param1, param2, param3, param4);
}
unsigned long MacRISC2PE::readUniNReg(unsigned long offest)
{
return uniNBaseAddress[offest / 4];
}
void MacRISC2PE::writeUniNReg(unsigned long offest, unsigned long data)
{
uniNBaseAddress[offest / 4] = data;
eieio();
}
void MacRISC2PE::getDefaultBusSpeeds(long *numSpeeds, unsigned long **speedList)
{
if ((numSpeeds == 0) || (speedList == 0)) return;
*numSpeeds = 1;
*speedList = macRISC2Speed;
}
void MacRISC2PE::enableUniNEthernetClock(bool enable)
{
unsigned long regTemp;
regTemp = readUniNReg(kUniNClockControl);
if (enable)
{
regTemp |= kUniNEthernetClockEnable;
}
else
{
regTemp &= ~kUniNEthernetClockEnable;
}
writeUniNReg(kUniNClockControl, regTemp);
}
void MacRISC2PE::enableUniNFireWireClock(bool enable)
{
unsigned long regTemp;
regTemp = readUniNReg(kUniNClockControl);
IOLog("FWClock, enable = %d kFW = %x\n", enable, kUniNFirewireClockEnable);
if (enable)
{
regTemp |= kUniNFirewireClockEnable;
}
else
{
regTemp &= ~kUniNFirewireClockEnable;
}
writeUniNReg(kUniNClockControl, regTemp);
}
void MacRISC2PE::enableUniNFireWireCablePower(bool enable)
{
if(getMachineType() == kMacRISC2TypePowerBook)
{
IOService *keyLargo;
keyLargo = waitForService(serviceMatching("KeyLargo"));
if(keyLargo)
{
UInt32 gpioOffset = 0x73;
keyLargo->callPlatformFunction(OSSymbol::withCString("keyLargo_writeRegUInt8"),
true, (void *)&gpioOffset, (void *)(enable ? 0:4), 0, 0);
}
}
}
void MacRISC2PE::PMInstantiatePowerDomains ( void )
{
OSString * errorStr = new OSString;
OSObject * obj;
IOPMUSBMacRISC2 * usbMacRISC2;
IOPMSlotsMacRISC2 * slotsMacRISC2;
obj = OSUnserializeXML (gIOMacRISC2PMTree, &errorStr);
if( 0 == (thePowerTree = ( OSArray * ) obj) )
{
kprintf ("error parsing power tree: %s", errorStr->getCStringNoCopy());
}
getProvider()->setProperty ("powertreedesc", thePowerTree);
root = new IOPMrootDomain;
root->init();
root->attach(this);
root->start(this);
root->youAreRoot();
if(kDoFrameBufferDeepSleep)
{
root->setSleepSupported(kRootDomainSleepSupported | kFrameBufferDeepSleepSupported);
}
else
{
root->setSleepSupported(kRootDomainSleepSupported);
}
if (NULL == root)
{
kprintf ("PMInstantiatePowerDomains - null ROOT\n");
return;
}
PMRegisterDevice (NULL, root);
usbMacRISC2 = new IOPMUSBMacRISC2;
if (usbMacRISC2)
{
usbMacRISC2->init ();
usbMacRISC2->attach (this);
usbMacRISC2->start (this);
PMRegisterDevice (root, usbMacRISC2);
}
slotsMacRISC2 = new IOPMSlotsMacRISC2;
if (slotsMacRISC2)
{
slotsMacRISC2->init ();
slotsMacRISC2->attach (this);
slotsMacRISC2->start (this);
PMRegisterDevice (root, slotsMacRISC2);
}
}
extern const IORegistryPlane * gIOPowerPlane;
void MacRISC2PE::PMRegisterDevice(IOService * theNub, IOService * theDevice)
{
OSData * aString;
bool nodeFound = false;
IOReturn err = -1;
if (mutex != NULL)
IOLockLock(mutex);
multipleParentKeyValue = NULL;
numInstancesRegistered = 0;
nodeFound = CheckSubTree (thePowerTree, theNub, theDevice, NULL);
if(getMachineType() == kMacRISC2TypePowerMac)
{
if ( OSDynamicCast(IOPCIDevice, theDevice) )
{
aString = (OSData *)theDevice->getProperty("AAPL,slot-name");
if ( (aString != NULL) && (0 != strncmp(aString->getBytesNoCopy(), "SLOT-1", strlen("SLOT-1"))) )
{
if(kDoFrameBufferDeepSleep)
{
root->setSleepSupported(kRootDomainSleepNotSupported | kFrameBufferDeepSleepSupported);
}
else
{
root->setSleepSupported(kRootDomainSleepNotSupported);
}
}
}
}
if (0 == numInstancesRegistered)
{
if( theNub && (0 == strncmp(theNub->getName(), "ATY,RageM3p", strlen("ATY,RageM3p"))) &&
(0 != strncmp(theNub->getName(), "ATY,RageM3p1", strlen("ATY,RageM3p1"))) &&
(0 != strncmp(theNub->getName(), "ATY,RageM3p2", strlen("ATY,RageM3p2"))) )
theNub = (IOService *) theNub->getParentEntry(gIODTPlane);
if( theNub && (0 == strncmp(theNub->getName(), "ATY,RageM3p12", strlen("ATY,RageM3p12"))) )
theNub = (IOService *) theNub->getParentEntry(gIODTPlane);
if( theNub && (0 == strncmp(theNub->getName(), "ATY,RageM3p29", strlen("ATY,RageM3p29"))) )
theNub = (IOService *) theNub->getParentEntry(gIODTPlane);
while( theNub && (!theNub->inPlane(gIOPowerPlane)))
theNub = theNub->getProvider();
}
if (mutex != NULL)
IOLockUnlock(mutex);
if ( NULL != theNub )
err = theNub->addPowerChild (theDevice);
if ((err != IOPMNoErr) && (0 == numInstancesRegistered) && (theDevice != root))
root->addPowerChild (theDevice);
}