os/kernelhwsrv/bsptemplate/asspandvariant/template_variant/specific/power.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// template\template_variant\specific\power.cpp
sl@0
    15
// Template Power Management
sl@0
    16
// (see also variant.cpp for a discussion on Sleep modes and xyin.cpp for example
sl@0
    17
// of usage of Resource Manager and Peripheral self power down and interaction
sl@0
    18
// with Power Controller for Wakeup Events)
sl@0
    19
// 
sl@0
    20
//
sl@0
    21
sl@0
    22
#include "template_power.h"
sl@0
    23
sl@0
    24
static TemplateResourceManager TheResourceManager;
sl@0
    25
sl@0
    26
DTemplatePowerController* TTemplatePowerController::iPowerController;
sl@0
    27
sl@0
    28
sl@0
    29
//-/-/-/-/-/-/-/-/-/ class DTemplatePowerController /-/-/-/-/-/-/-/-/-/
sl@0
    30
sl@0
    31
DTemplatePowerController::DTemplatePowerController()
sl@0
    32
	{
sl@0
    33
	Register();			// register Power Controller with Power Manager
sl@0
    34
	TTemplatePowerController::RegisterPowerController(this);
sl@0
    35
	}
sl@0
    36
sl@0
    37
void DTemplatePowerController::CpuIdle()
sl@0
    38
	{
sl@0
    39
	Arch::TheAsic()->Idle();
sl@0
    40
	}
sl@0
    41
sl@0
    42
void DTemplatePowerController::EnableWakeupEvents()
sl@0
    43
	{
sl@0
    44
	//
sl@0
    45
	// TO DO: (mandatory)
sl@0
    46
	//
sl@0
    47
	// Enable tracking of wake-up events directly in hardware. If the hardware is controlled by a Driver
sl@0
    48
	// or Extension, may need to disable interrupts and preemption around the code that accesses the hardware
sl@0
    49
	// and set up a flag which the Driver/Extension code need to read before modifying the state of that piece
sl@0
    50
	// of hardware. Note in that case the Driver/Extension may need to link to this Library.
sl@0
    51
	//
sl@0
    52
sl@0
    53
	//
sl@0
    54
	// EXAMPLE ONLY
sl@0
    55
	// In this example we simply assume that the driver will call the Power Controller every time a 
sl@0
    56
	// wakeup event occurr. It is up to the Power Controller to know if it is tracking them or not.
sl@0
    57
	// We also assume that if a wakeup event occurrs when the CPU is in Standby, this will automatically
sl@0
    58
	// bring it back from that state.
sl@0
    59
	iWakeupEventsOn = ETrue;	// start tracking wakeup events
sl@0
    60
	}
sl@0
    61
sl@0
    62
void DTemplatePowerController::DisableWakeupEvents()
sl@0
    63
	{
sl@0
    64
	//
sl@0
    65
	// TO DO: (mandatory)
sl@0
    66
	//
sl@0
    67
	// Disable tracking of wake-up events directly in hardware or if the hardware is controlled by a Driver or
sl@0
    68
	// Extension need to set up a flag which the Driver/Extension reads whenever the event occurs, in order to
sl@0
    69
	// find out if it needs to deliver notification to the Power Controller
sl@0
    70
	//
sl@0
    71
	iWakeupEventsOn = EFalse;	// stop tracking wakeup events
sl@0
    72
	}
sl@0
    73
sl@0
    74
void DTemplatePowerController::AbsoluteTimerExpired()
sl@0
    75
	{
sl@0
    76
	if (iTargetState == EPwStandby && iWakeupEventsOn)
sl@0
    77
		{
sl@0
    78
		iWakeupEventsOn = EFalse;		// one occurred, no longer track wakeup events
sl@0
    79
		WakeupEvent();
sl@0
    80
		}
sl@0
    81
	}
sl@0
    82
sl@0
    83
void DTemplatePowerController::PowerDown(TTimeK aWakeupST)	
sl@0
    84
	{
sl@0
    85
	if (iTargetState == EPwStandby)
sl@0
    86
		{
sl@0
    87
		//
sl@0
    88
		// TO DO: (mandatory)
sl@0
    89
		//
sl@0
    90
		// Converts between the Wakeup time in System Time units as passed in to this function and a Wakeup
sl@0
    91
		// time in RTC units. The following code is given as an example how to convert between System time units
sl@0
    92
		// RTC time units on a system with a 32 bit RTC timer and which is incremented on a second interval:
sl@0
    93
		//
sl@0
    94
		TUint32 wakeupRTC;
sl@0
    95
		if (aWakeupST)
sl@0
    96
			{
sl@0
    97
			TUint32 nowRTC = TTemplate::RtcData();
sl@0
    98
			TTimeK nowST = Kern::SystemTime();
sl@0
    99
			__KTRACE_OPT(KPOWER,Kern::Printf("system time: now = 0x%lx(us) wakeup = 0x%lx(us)", nowST, aWakeupST));
sl@0
   100
			if (aWakeupST < nowST)
sl@0
   101
				return;
sl@0
   102
			Int64 deltaSecs = (aWakeupST - nowST) / 1000000;
sl@0
   103
			if (deltaSecs <= 0)
sl@0
   104
				return;
sl@0
   105
			if (deltaSecs + (Int64)nowRTC > (Int64)(KMaxTInt - 2))
sl@0
   106
				wakeupRTC = (KMaxTInt - 2); // RTC can't wrap around during standby
sl@0
   107
			else
sl@0
   108
				wakeupRTC = nowRTC + deltaSecs;
sl@0
   109
			__KTRACE_OPT(KPOWER,Kern::Printf("RTC: now = %d(s) wakeup = %d(s)", nowRTC, wakeupRTC));
sl@0
   110
			}
sl@0
   111
		else
sl@0
   112
			wakeupRTC = 0;
sl@0
   113
		//
sl@0
   114
		// TO DO: (optional)
sl@0
   115
		//
sl@0
   116
		// It then uses the calculated value to program the RTC to wakeup the System at the Wakeup
sl@0
   117
		// time ans sets the CPU and remaining hardware to go to the correponding low power mode. When the 
sl@0
   118
		// state of the Core and Core Peripherals is not preserved in this mode the following is usually 
sl@0
   119
		// required:
sl@0
   120
		//	- save current Core state (current Mode, banked registers for each Mode and Stack Pointer for 
sl@0
   121
		//	  both current and User Modes
sl@0
   122
		//	- save MMU state: Control Register, TTB and Domain Access Control
sl@0
   123
		//	- Flush Dta Cache and drain Write Buffer
sl@0
   124
		//	- save Core Peripherals state: Interrupt Controller, Pin Function, Bus State and Clock settings
sl@0
   125
		// SDRAM should be put in self refresh mode. Peripheral devices involved in detection of Wakeup events
sl@0
   126
		// should be left powered.
sl@0
   127
		// The Tick timer should be disabled and the current count of this and other System timers shall be
sl@0
   128
		// saved.
sl@0
   129
		// On wakeing up the state should be restored from the save state as above. SDRAM shall be brought back
sl@0
   130
		// under CPU control, The Tick count shall be restored and timers re-enabled.
sl@0
   131
sl@0
   132
		// We assume that if a wakeup event occurrs when the CPU is in Standby, this will automatically
sl@0
   133
		// bring it back from that state. Therefore we stop tracking wakeup events as the Power Manager will
sl@0
   134
		// complete any pending notifications anyway. When the driver delivers its notification, we just ignore
sl@0
   135
		// it.
sl@0
   136
		iWakeupEventsOn = EFalse;		// tracking of wakeup events is now done in hardware
sl@0
   137
		}
sl@0
   138
	else
sl@0
   139
		{
sl@0
   140
		Kern::Restart(0x80000000);
sl@0
   141
		}
sl@0
   142
	}
sl@0
   143
sl@0
   144
//-/-/-/-/-/-/-/-/-/ class TTemplatePowerController /-/-/-/-/-/-/-/-/-/
sl@0
   145
sl@0
   146
EXPORT_C TemplateResourceManager* TTemplatePowerController::ResourceManager()
sl@0
   147
	{
sl@0
   148
	return &TheResourceManager;
sl@0
   149
	}
sl@0
   150
sl@0
   151
sl@0
   152
EXPORT_C void TTemplatePowerController::WakeupEvent()
sl@0
   153
	{
sl@0
   154
	if(!iPowerController)
sl@0
   155
		__PM_PANIC("Power Controller not present");
sl@0
   156
	else if(iPowerController->iWakeupEventsOn)
sl@0
   157
		{
sl@0
   158
		iPowerController->iWakeupEventsOn=EFalse;		// one occurred, no longer track wakeup events
sl@0
   159
		iPowerController->WakeupEvent();
sl@0
   160
		}
sl@0
   161
	}
sl@0
   162
sl@0
   163
//-/-/-/-/-/-/-/-/-/ class TemplateResourceManager /-/-/-/-/-/-/-/-/-/
sl@0
   164
sl@0
   165
void TemplateResourceManager::InitResources()
sl@0
   166
	{
sl@0
   167
	//
sl@0
   168
	// TO DO: (optional)
sl@0
   169
	//
sl@0
   170
	// Initialise any power resources required by the platform and not initialised in the Bootstrap
sl@0
   171
	//
sl@0
   172
	}
sl@0
   173
sl@0
   174
//-/-/-/-/-/-/-/-/-/ interface for shared resources /-/-/-/-/-/-/-/-/-/
sl@0
   175
sl@0
   176
void SharedBinaryResource1::Use()
sl@0
   177
	{
sl@0
   178
	NKern::Lock();		// lock Kernel as shared resource is likely to be modified from different threads
sl@0
   179
	if (iCount++ == 0)
sl@0
   180
		{
sl@0
   181
		//
sl@0
   182
		// TO DO: (optional)
sl@0
   183
		//
sl@0
   184
		// Modify hardware register bit or bits to switch the resource On. If the resource
sl@0
   185
		// can be accessed from an ISR need to disable/enable interrupts around it.
sl@0
   186
		//
sl@0
   187
		NKern::Unlock();
sl@0
   188
		//
sl@0
   189
		// TO DO: (optional)
sl@0
   190
		//
sl@0
   191
		// If the resource is asynchronous may need to sleep or block the thread until the change is complete
sl@0
   192
		//
sl@0
   193
		}
sl@0
   194
	else
sl@0
   195
		NKern::Unlock();
sl@0
   196
	}
sl@0
   197
sl@0
   198
void SharedBinaryResource1::Release()
sl@0
   199
	{
sl@0
   200
	NKern::Lock();
sl@0
   201
	__PM_ASSERT(iCount);
sl@0
   202
	if (--iCount == 0)
sl@0
   203
		{
sl@0
   204
		//
sl@0
   205
		// TO DO: (optional)
sl@0
   206
		//
sl@0
   207
		// Modify hardware register bit or bits to switch the resource Off. If the resource
sl@0
   208
		// can be accessed from an ISR need to disable/enable interrupts around it.
sl@0
   209
		//
sl@0
   210
		NKern::Unlock();
sl@0
   211
		//
sl@0
   212
		// TO DO: (optional)
sl@0
   213
		//
sl@0
   214
		// If the resource is asynchronous may need to sleep or block the thread until the change is complete
sl@0
   215
		//
sl@0
   216
		}
sl@0
   217
	else
sl@0
   218
		NKern::Unlock();
sl@0
   219
	}
sl@0
   220
sl@0
   221
TUint SharedBinaryResource1::GetCount()
sl@0
   222
	{
sl@0
   223
	return iCount;
sl@0
   224
	}
sl@0
   225
sl@0
   226
SharedMultilevelResource1::SharedMultilevelResource1()
sl@0
   227
	//
sl@0
   228
	// TO DO: (optional)
sl@0
   229
	//
sl@0
   230
	// May need to initialise current level and the Id of its owner if these have been initialised in the Bootstrap
sl@0
   231
	//
sl@0
   232
	// : iCurrentLevel(/* a level for this resource as initialised in the Bootstrap */),
sl@0
   233
	//	 iCurrentLevelOwnerId(/* the Id of the requester of this resource that requires the initial value */)
sl@0
   234
	{
sl@0
   235
	}
sl@0
   236
sl@0
   237
void SharedMultilevelResource1::IncreaseToLevel(TUint aLevel, TInt aRequester)
sl@0
   238
	{
sl@0
   239
	//
sl@0
   240
	// Drivers should use this API if they wish to request a level higher than the previous level they required 
sl@0
   241
	// Drivers should keep track of the level they require and be disciplined
sl@0
   242
	//
sl@0
   243
	NKern::Lock();
sl@0
   244
	__PM_ASSERT(aLevel<Levels[aRequester]);
sl@0
   245
	Levels[aRequester]=aLevel;
sl@0
   246
	if(aLevel > iCurrentLevel)			// need to increase the level
sl@0
   247
		{
sl@0
   248
		// if(aLevel <= MAXLEVEL)
sl@0
   249
		//	aLevel = MAXLEVEL;
sl@0
   250
		iCurrentLevel = aLevel;
sl@0
   251
		iCurrentLevelOwnerId = aRequester;
sl@0
   252
		//
sl@0
   253
		// TO DO: (optional)
sl@0
   254
		//
sl@0
   255
		// Modify hardware register bits to set the level of the resource to aLevel
sl@0
   256
		NKern::Unlock();
sl@0
   257
		//
sl@0
   258
		// TO DO: (optional)
sl@0
   259
		//
sl@0
   260
		// If the resource is asynchronous may need to sleep or block the thread until the change is complete
sl@0
   261
		//
sl@0
   262
		}
sl@0
   263
	else
sl@0
   264
		NKern::Unlock();
sl@0
   265
	}
sl@0
   266
sl@0
   267
void SharedMultilevelResource1::ReduceToLevel(TUint aLevel, TInt aRequester)
sl@0
   268
	{
sl@0
   269
	//
sl@0
   270
	// Drivers should use this API if they wish to request a level higher than the previous level they required 
sl@0
   271
	//
sl@0
   272
	NKern::Lock();
sl@0
   273
	__PM_ASSERT(aLevel>Levels[aRequester]);
sl@0
   274
sl@0
   275
	Levels[aRequester]=aLevel;
sl@0
   276
	if(aLevel < iCurrentLevel && aRequester == iCurrentLevelOwnerId)	// the holder of the current level as lowered its request
sl@0
   277
		{
sl@0
   278
		FindMaxLevel(&iCurrentLevel, &iCurrentLevelOwnerId);			// find max level required and the ID of its holder
sl@0
   279
		//
sl@0
   280
		// TO DO: (optional)
sl@0
   281
		//
sl@0
   282
		// Modify hardware register bits to set the level of the resource to iCurrentLevel
sl@0
   283
		NKern::Unlock();
sl@0
   284
		//
sl@0
   285
		// TO DO: (optional)
sl@0
   286
		//
sl@0
   287
		// If the resource is asynchronous may need to sleep or block the thread until the change is complete
sl@0
   288
		//
sl@0
   289
		}
sl@0
   290
	else
sl@0
   291
		NKern::Unlock();
sl@0
   292
	}
sl@0
   293
sl@0
   294
TUint SharedMultilevelResource1::GetResourceLevel()
sl@0
   295
	{
sl@0
   296
	return iCurrentLevel;
sl@0
   297
	}
sl@0
   298
sl@0
   299
void SharedMultilevelResource1::FindMaxLevel(TUint* aLevel, TInt* aId)
sl@0
   300
	{
sl@0
   301
	//
sl@0
   302
	// TO DO: (optional)
sl@0
   303
	//
sl@0
   304
	// Place your clever array search algorithm here...
sl@0
   305
	// return max level and id of owner
sl@0
   306
	}
sl@0
   307
sl@0
   308
TInt BinaryPowerInit();		// the Symbian example Battery Monitor and Power HAL handling
sl@0
   309
sl@0
   310
GLDEF_C TInt KernelModuleEntry(TInt aReason)
sl@0
   311
	{
sl@0
   312
	if(aReason==KModuleEntryReasonVariantInit0)
sl@0
   313
		{
sl@0
   314
		//
sl@0
   315
		// TO DO: (optional)
sl@0
   316
		//
sl@0
   317
		// Start the Resource Manager earlier so that Variant and other extension could make use of Power Resources
sl@0
   318
		//
sl@0
   319
		__KTRACE_OPT(KPOWER, Kern::Printf("Starting Template Resource controller"));
sl@0
   320
		new(&TheResourceManager)TemplateResourceManager;
sl@0
   321
		TheResourceManager.InitResources();
sl@0
   322
		return KErrNone;
sl@0
   323
		}
sl@0
   324
	else if(aReason==KModuleEntryReasonExtensionInit0)
sl@0
   325
		{
sl@0
   326
		__KTRACE_OPT(KPOWER, Kern::Printf("Starting Template power controller"));
sl@0
   327
		//
sl@0
   328
		// TO DO: (optional)
sl@0
   329
		//
sl@0
   330
		// Start the Kernel-side Battery Monitor and hook a Power HAL handling function.
sl@0
   331
		// Symbian provides example code for both of the above in \e32\include\driver\binpower.h
sl@0
   332
		// You may want to write your own versions.
sl@0
   333
		// The call below starts the example Battery Monitor and hooks the example Power HAL handling function
sl@0
   334
		// At the end we return an error to make sure that the entry point is not called again with
sl@0
   335
		// KModuleEntryReasonExtensionInit1 (which would call the constructor of TheResourceManager again)
sl@0
   336
		//
sl@0
   337
		TInt r = BinaryPowerInit();
sl@0
   338
		if (r!= KErrNone)
sl@0
   339
			__PM_PANIC("Can't initialise Binary Power model");
sl@0
   340
		DTemplatePowerController* c = new DTemplatePowerController();
sl@0
   341
		if(c)
sl@0
   342
			return KErrGeneral;
sl@0
   343
		else
sl@0
   344
			__PM_PANIC("Can't create Power Controller");
sl@0
   345
		}
sl@0
   346
	else if(aReason==KModuleEntryReasonExtensionInit1)
sl@0
   347
		{
sl@0
   348
		// does not get called...
sl@0
   349
		}
sl@0
   350
	return KErrArgument;
sl@0
   351
	}