Update contrib.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
24 #include <kernel/kern_priv.h>
25 #include "cryptodriver.h"
27 #include <omap_hrp/assp/shared/omap_reg.h>
28 #include <omap_hrp/assp/shared/omap_interrupt.h>
30 //#include "cryptoh4.h"
31 #include "cryptoldd.h"
32 #include "cryptoh4rng.h"
34 inline void CryptoH4JobRandom::EnableIsr()
36 TRACE_FUNCTION("EnableIsr");
37 // Kern::Printf("EI");
40 // Enable RNG interrupt. This interrupt will then queue the
41 // "random number ready" DFC
42 TInt32 tmp = TOmap::Register32(KHwBaseRngReg + KHoRng_Mask);
44 TOmap::SetRegister32(KHwBaseRngReg + KHoRng_Mask, tmp);
46 // Not on real h/w so just queue the DFC...
47 // Queue the "random number ready" DFC
48 iRandomDfc.Enque(); // Queue from task level
52 inline void CryptoH4JobRandom::DisableIsr()
54 TRACE_FUNCTION("DisableIsr");
55 // Kern::Printf("DI");
57 TInt32 tmp = TOmap::Register32(KHwBaseRngReg + KHoRng_Mask);
59 TOmap::SetRegister32(KHwBaseRngReg + KHoRng_Mask, tmp);
65 CryptoH4JobRandom::CryptoH4JobRandom(DLddChanRandom &aLddChanRandom)
66 : iLddChanRandom(aLddChanRandom),
71 iRandomDfc(RandomDfc, this, 1) // DFC is priority '1'
73 TRACE_FUNCTION("CryptoH4JobRandom");
74 // Kern::Printf("CryptoH4JobRandom::CryptoH4JobRandom %x", this);
77 CryptoH4JobRandom::~CryptoH4JobRandom()
79 TRACE_FUNCTION("~CryptoH4JobRandom");
80 // Kern::Printf("CryptoH4JobRandom::~CryptoH4JobRandom %x", this);
85 void CryptoH4JobRandom::SetDfcQ(TDfcQue *aDfcQue)
87 TRACE_FUNCTION("SetDfcQ");
88 iRandomDfc.SetDfcQ(aDfcQue);
91 void CryptoH4JobRandom::SetDetails(DCryptoJobScheduler *aJobScheduler,
92 MCryptoJobCallbacks *aCallbacks,
95 TRACE_FUNCTION("SetDetails");
96 // Kern::Printf("CryptoH4JobRandom::SetDetails");
97 iJobScheduler = aJobScheduler;
98 iCallbacks = aCallbacks;
99 iJobSizeInBytes = aNumOfBytes;
102 void CryptoH4JobRandom::GetToPddBuffer(TUint8 * &aBuf, TUint32 &aBufLen, TBool &aMore)
104 TRACE_FUNCTION("GetToPddBuffer");
110 void CryptoH4JobRandom::BytesWrittenToPdd(TUint32)
112 TRACE_FUNCTION("BytesWrittenToPdd");
115 void CryptoH4JobRandom::GetFromPddBuffer(TUint8 * &aBuf, TUint32 &aBufLen, TBool &aMore)
117 TRACE_FUNCTION("GetFromPddBuffer");
119 TInt hw8Index = iHw32Index * 4;
120 TUint8 *p = (TUint8 *) iRandomBuffer;
121 aBuf = &p[iSwReadByteOffset];
123 TInt len = hw8Index - iSwReadByteOffset;
131 // Wrap round condition, but can only return contiguous bytes
132 aBufLen = sizeof(iRandomBuffer) - iSwReadByteOffset;
138 void CryptoH4JobRandom::BytesReadFromPdd(TUint32 aBytes)
140 TRACE_FUNCTION("BytesReadFromPdd");
141 iSwReadByteOffset += aBytes;
142 if(iSwReadByteOffset >= sizeof(iRandomBuffer))
144 iSwReadByteOffset -= sizeof(iRandomBuffer);
146 iJobSizeInBytes -= aBytes;
151 void CryptoH4JobRandom::DoSlice(TBool aFirstSlice)
153 TRACE_FUNCTION("DoSlice");
154 // Kern::Printf("DoSlice(%d)", aFirstSlice);
160 // Enable RNG interrupt. The interrupt will then queue the
161 // "random number ready" DFC when the h/w is ready.
162 // (when not on h/w, this immediately queues a DFC)
166 TBool CryptoH4JobRandom::DoSaveState()
168 TRACE_FUNCTION("DoSaveState");
170 return ETrue; // We want DoRestoreState to be called
173 void CryptoH4JobRandom::DoRestoreState()
175 TRACE_FUNCTION("DoRestoreState");
179 void CryptoH4JobRandom::DoReleaseHw()
181 TRACE_FUNCTION("DoReleaseHw");
182 // Disable RNG interrupt
185 // Disable/unhook ISR
193 TInt CryptoH4JobRandom::BytesAvailable() const
195 TRACE_FUNCTION("BytesAvailable");
196 TInt hw8Index = iHw32Index * 4;
197 TInt available = hw8Index - iSwReadByteOffset;
200 available += sizeof(iRandomBuffer);
205 void CryptoH4JobRandom::RegionsAvailable(TUint8 * &aPtr1, TInt &aLen1,
206 TUint8 * &aPtr2, TInt &aLen2) const
208 TRACE_FUNCTION("RegionsAvailable");
209 TInt hw8Index = iHw32Index * 4;
210 TUint8 *p = (TUint8 *) iRandomBuffer;
211 aPtr1 = &p[iSwReadByteOffset];
213 TInt len = hw8Index - iSwReadByteOffset;
216 // Available data crosses buffer end so return two regions
217 aLen1 = sizeof(iRandomBuffer) - iSwReadByteOffset;
223 // Available buffer is contiguous
232 void CryptoH4JobRandom::HookIsr()
234 TRACE_FUNCTION("HookIsr");
235 // Kern::Printf("CryptoH4JobRandom::HookIsr iIsrHooked=%d this=%x", iIsrHooked, this);
239 TInt r = Interrupt::Bind(EIrqRng, Isr, this);
240 if(r != KErrNone) Kern::Fault("CryptoH4JobRandom::HookIsr Bind failed", r);
241 r = Interrupt::Enable(EIrqRng);
242 if(r != KErrNone) Kern::Fault("CryptoH4JobRandom::HookIsr Enable failed", r);
248 void CryptoH4JobRandom::UnHookIsr()
250 TRACE_FUNCTION("UnHookIsr");
251 // Kern::Printf("CryptoH4JobRandom::UnHookIsr iIsrHooked=%d this=%x", iIsrHooked, this);
255 Interrupt::Disable(EIrqRng);
256 Interrupt::Unbind(EIrqRng);
265 void CryptoH4JobRandom::Isr(TAny *aPtr)
267 TRACE_FUNCTION("Isr");
268 CryptoH4JobRandom *p = static_cast<CryptoH4JobRandom *>(aPtr);
269 // Disable RNG interrupt so DFC can run.
271 // Queue DFC to read the RNG
277 Called when the current h/w opperation is complete
279 void CryptoH4JobRandom::RandomDfc(TAny* aPtr)
281 ((CryptoH4JobRandom*)aPtr)->DoRandomDfc();
284 void CryptoH4JobRandom::DoRandomDfc()
286 TRACE_FUNCTION("DoRandomDfc");
287 // Set state to not using hw, if we continue using the h/w we will
288 // call EnableIsr which will change the state back to ERunning.
290 // Kern::Printf("DoRandomDfc");
293 iRandomBuffer[iHw32Index] = TOmap::Register32(KHwBaseRngReg + KHoRng_Out);
295 static TUint32 n = 0;
296 iRandomBuffer[iHw32Index]= n++;
300 if(iHw32Index >= sizeof(iRandomBuffer)/sizeof(iRandomBuffer[0]))
305 TInt outputAvailable = BytesAvailable();
306 TInt space = sizeof(iRandomBuffer) - outputAvailable - 4;
307 if((outputAvailable >= iJobSizeInBytes) || (space <= 0))
309 // Either have enough data to finish job, or out of buffer
310 // space to read more. We pass available data to the LDD, and
311 // declare the slice/job done and return.
314 // Pass available data to LDD
316 // LDD will call GetFromPddBuffer/BytesReadFromPdd to read the data.
317 iLddChanRandom.DataAvailable();
321 if(iJobSizeInBytes <= 0)
323 // Tell the scheduler that this slice is done
324 iJobScheduler->JobComplete(this, KErrNone);
328 // Re-calculate output available and space
329 outputAvailable = BytesAvailable();
330 space = sizeof(iRandomBuffer) - outputAvailable - 4;
332 if((space != 0) && (iJobSizeInBytes-outputAvailable > 0))
334 // We have some space and we need more data
336 // Enable RNG interrupt. The interrupt will then queue the
337 // "random number ready" DFC when the h/w is ready.
338 // (when not on h/w, this immediately queues a DFC)
343 // Job stalled - either out of space or already have enough
344 // data but LDD has not take it