author | Lukasz Forynski <lukasz.forynski@gmail.com> |
Mon, 23 Aug 2010 02:29:41 +0100 | |
changeset 51 | 254b9435d75e |
parent 6 | b277c89230a2 |
permissions | -rwxr-xr-x |
0 | 1 |
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
2 |
// All rights reserved. |
|
3 |
// This component and the accompanying materials are made available |
|
4 |
// under the terms of the License "Eclipse Public License v1.0" |
|
5 |
// which accompanies this distribution, and is available |
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// omap3530/omap3530_drivers/i2c/i2c.cpp |
|
15 |
// I2C Driver |
|
16 |
// Main interface, I2c, is declared in omap3530_i2c.h |
|
17 |
// A more restricted register orientated interface, I2cReg, is declared in omap3530_i2creg.h |
|
18 |
// This file is part of the Beagle Base port |
|
19 |
// |
|
20 |
||
21 |
#include <assp/omap3530_assp/omap3530_i2creg.h> |
|
22 |
||
23 |
#include <assp/omap3530_assp/omap3530_irqmap.h> |
|
24 |
#include <assp/omap3530_assp/omap3530_hardware_base.h> |
|
25 |
#include <assp/omap3530_assp/omap3530_ktrace.h> |
|
26 |
//#include <assp/omap3530_assp/omap3530_prm.h> |
|
27 |
#include <assp/omap3530_assp/omap3530_prcm.h> |
|
28 |
#include <nk_priv.h> |
|
29 |
#include <nklib.h> |
|
30 |
//#include <resourceman.h> |
|
31 |
||
51
254b9435d75e
Fixed build warnings
Lukasz Forynski <lukasz.forynski@gmail.com>
parents:
6
diff
changeset
|
32 |
#ifdef USE_SYMBIAN_PRM |
0 | 33 |
_LIT(KDfcName, "I2C_DFC"); // Not used by the I2c dfc! |
51
254b9435d75e
Fixed build warnings
Lukasz Forynski <lukasz.forynski@gmail.com>
parents:
6
diff
changeset
|
34 |
#endif |
254b9435d75e
Fixed build warnings
Lukasz Forynski <lukasz.forynski@gmail.com>
parents:
6
diff
changeset
|
35 |
|
0 | 36 |
DECLARE_STANDARD_EXTENSION() |
37 |
{ |
|
38 |
return KErrNone; |
|
39 |
} |
|
40 |
||
41 |
namespace I2c |
|
42 |
{ |
|
43 |
const TInt KMaxDevicesPerUnit = 8; // arbitary - change if required |
|
44 |
const TInt KNumUnits = E3 + 1; |
|
45 |
||
46 |
// Each unit has KMaxDevicesPerUnit of these structures. At least one for each slave device on it's bus. |
|
47 |
struct TDeviceControl |
|
48 |
{ |
|
49 |
TDeviceAddress iAddress; // the slave devices address; 7 or 10 bits |
|
50 |
TDfcQue* iDfcQueue; // calling driver's DFC thread |
|
51 |
NFastSemaphore iSyncSem; // used to block the calling thread during synchronous transfers |
|
52 |
}; |
|
53 |
||
54 |
// There are three instances of this structure - one for each I2C bus on the OMAP3530 |
|
55 |
struct TUnitControl |
|
56 |
{ |
|
57 |
TUnitControl(); |
|
58 |
||
59 |
TSpinLock iLock; // prevents concurrent access to the request queue |
|
60 |
DMutex* iOpenMutex; |
|
61 |
||
62 |
enum |
|
63 |
{ |
|
64 |
EIdle, |
|
65 |
ERead, |
|
66 |
EWrite |
|
67 |
} iState; |
|
68 |
||
69 |
// Configuration stored and checked during Open() |
|
70 |
TRole iRole; |
|
71 |
TMode iMode; |
|
72 |
void* iExclusiveClient; |
|
73 |
TRate iRate; |
|
74 |
TDeviceAddress iOwnAddress; |
|
75 |
||
76 |
// The DFC for this unit - it runs on the thread associated with the active transfer |
|
77 |
TDfc iDfc; |
|
78 |
||
51
254b9435d75e
Fixed build warnings
Lukasz Forynski <lukasz.forynski@gmail.com>
parents:
6
diff
changeset
|
79 |
|
0 | 80 |
// the slave devices on this unit's bus |
81 |
TDeviceControl iDevice[KMaxDevicesPerUnit]; |
|
82 |
TInt iNumDevices; |
|
83 |
||
84 |
// The queue of requested transfers - the active transfer is the head of the queue |
|
85 |
TTransferPb* iTransferQ; |
|
86 |
TTransferPb* iTransferQTail; // the last transfer on the queue |
|
87 |
||
88 |
// the current phase of the sctive transfer |
|
89 |
TTransferPb* iCurrentPhase; |
|
90 |
}; |
|
91 |
||
92 |
// The OMAP3530 register address |
|
93 |
const TUint KI2C_IE[KNumUnits] = |
|
94 |
{Omap3530HwBase::TVirtual<0x48070004>::Value, Omap3530HwBase::TVirtual<0x48072004>::Value, Omap3530HwBase::TVirtual<0x48060004>::Value}; |
|
95 |
const TUint KI2C_STAT[KNumUnits] = |
|
96 |
{Omap3530HwBase::TVirtual<0x48070008>::Value, Omap3530HwBase::TVirtual<0x48072008>::Value, Omap3530HwBase::TVirtual<0x48060008>::Value}; |
|
97 |
//const TUint KI2C_WE[KNumUnits] = |
|
98 |
// {Omap3530HwBase::TVirtual<0x4807000C>::Value, Omap3530HwBase::TVirtual<0x4807200C>::Value, Omap3530HwBase::TVirtual<0x4806000C>::Value}; |
|
99 |
const TUint KI2C_SYSS[KNumUnits] = |
|
100 |
{Omap3530HwBase::TVirtual<0x48070010>::Value, Omap3530HwBase::TVirtual<0x48072010>::Value, Omap3530HwBase::TVirtual<0x48060010>::Value}; |
|
101 |
const TUint KI2C_BUF[KNumUnits] = |
|
102 |
{Omap3530HwBase::TVirtual<0x48070014>::Value, Omap3530HwBase::TVirtual<0x48072014>::Value, Omap3530HwBase::TVirtual<0x48060014>::Value}; |
|
103 |
const TUint KI2C_CNT[KNumUnits] = |
|
104 |
{Omap3530HwBase::TVirtual<0x48070018>::Value, Omap3530HwBase::TVirtual<0x48072018>::Value, Omap3530HwBase::TVirtual<0x48060018>::Value}; |
|
105 |
const TUint KI2C_DATA[KNumUnits] = |
|
106 |
{Omap3530HwBase::TVirtual<0x4807001C>::Value, Omap3530HwBase::TVirtual<0x4807201C>::Value, Omap3530HwBase::TVirtual<0x4806001C>::Value}; |
|
107 |
const TUint KI2C_SYSC[KNumUnits] = |
|
108 |
{Omap3530HwBase::TVirtual<0x48070020>::Value, Omap3530HwBase::TVirtual<0x48072020>::Value, Omap3530HwBase::TVirtual<0x48060020>::Value}; |
|
109 |
const TUint KI2C_CON[KNumUnits] = |
|
110 |
{Omap3530HwBase::TVirtual<0x48070024>::Value, Omap3530HwBase::TVirtual<0x48072024>::Value, Omap3530HwBase::TVirtual<0x48060024>::Value}; |
|
111 |
//const TUint KI2C_OA0[KNumUnits] = |
|
112 |
// {Omap3530HwBase::TVirtual<0x48070028>::Value, Omap3530HwBase::TVirtual<0x48072028>::Value, Omap3530HwBase::TVirtual<0x48060028>::Value}; |
|
113 |
const TUint KI2C_SA[KNumUnits] = |
|
114 |
{Omap3530HwBase::TVirtual<0x4807002C>::Value, Omap3530HwBase::TVirtual<0x4807202C>::Value, Omap3530HwBase::TVirtual<0x4806002C>::Value}; |
|
115 |
const TUint KI2C_PSC[KNumUnits] = |
|
116 |
{Omap3530HwBase::TVirtual<0x48070030>::Value, Omap3530HwBase::TVirtual<0x48072030>::Value, Omap3530HwBase::TVirtual<0x48060030>::Value}; |
|
117 |
const TUint KI2C_SCLL[KNumUnits] = |
|
118 |
{Omap3530HwBase::TVirtual<0x48070034>::Value, Omap3530HwBase::TVirtual<0x48072034>::Value, Omap3530HwBase::TVirtual<0x48060034>::Value}; |
|
119 |
const TUint KI2C_SCLH[KNumUnits] = |
|
120 |
{Omap3530HwBase::TVirtual<0x48070038>::Value, Omap3530HwBase::TVirtual<0x48072038>::Value, Omap3530HwBase::TVirtual<0x48060038>::Value}; |
|
121 |
//const TUint KI2C_SYSTEST[KNumUnits] = |
|
122 |
// {Omap3530HwBase::TVirtual<0x4807003C>::Value, Omap3530HwBase::TVirtual<0x4807203C>::Value, Omap3530HwBase::TVirtual<0x4806003C>::Value}; |
|
123 |
const TUint KI2C_BUFSTAT[KNumUnits] = |
|
124 |
{Omap3530HwBase::TVirtual<0x48070040>::Value, Omap3530HwBase::TVirtual<0x48072040>::Value, Omap3530HwBase::TVirtual<0x48060040>::Value}; |
|
125 |
const TUint KI2C_OA1[KNumUnits] = |
|
126 |
{Omap3530HwBase::TVirtual<0x48070044>::Value, Omap3530HwBase::TVirtual<0x48072044>::Value, Omap3530HwBase::TVirtual<0x48060044>::Value}; |
|
127 |
//const TUint KI2C_OA2[KNumUnits] = |
|
128 |
// {Omap3530HwBase::TVirtual<0x48070048>::Value, Omap3530HwBase::TVirtual<0x48072048>::Value, Omap3530HwBase::TVirtual<0x48060048>::Value}; |
|
129 |
//const TUint KI2C_OA3[KNumUnits] = |
|
130 |
// {Omap3530HwBase::TVirtual<0x4807004C>::Value, Omap3530HwBase::TVirtual<0x4807204C>::Value, Omap3530HwBase::TVirtual<0x4806004C>::Value}; |
|
131 |
//const TUint KI2C_ACTOA[KNumUnits] = |
|
132 |
// {Omap3530HwBase::TVirtual<0x48070050>::Value, Omap3530HwBase::TVirtual<0x48072050>::Value, Omap3530HwBase::TVirtual<0x48060050>::Value}; |
|
133 |
//const TUint KI2C_SBLOCK[KNumUnits] = |
|
134 |
// {Omap3530HwBase::TVirtual<0x48070054>::Value, Omap3530HwBase::TVirtual<0x48072054>::Value, Omap3530HwBase::TVirtual<0x48060054>::Value}; |
|
135 |
const TUint KCM_ICLKEN1_CORE = Omap3530HwBase::TVirtual<0x48004A10>::Value; |
|
136 |
const TUint KCM_FCLKEN1_CORE = Omap3530HwBase::TVirtual<0x48004A00>::Value; |
|
137 |
||
138 |
// the Id's used when binding the interrupts |
|
139 |
const TOmap3530_IRQ KIrqId[KNumUnits] = {EOmap3530_IRQ56_I2C1_IRQ, EOmap3530_IRQ57_I2C2_IRQ, EOmap3530_IRQ61_I2C3_IRQ}; |
|
140 |
||
141 |
// The three unit control blocks; one for each unit |
|
142 |
TUnitControl gUcb[KNumUnits]; |
|
143 |
//TUint prmClientId; |
|
144 |
||
145 |
TUnit RawUnit(THandle aHandle); |
|
146 |
TUnit Unit(THandle aHandle); |
|
147 |
TUnitControl& UnitCb(THandle aHandle); |
|
148 |
TDeviceAddress Device(THandle aHandle); |
|
149 |
TDeviceControl& DeviceCb(THandle aHandle); |
|
150 |
THandle Handle(TUnit aUnit, TDeviceAddress aDeviceAddress); |
|
151 |
void Complete(TUnitControl& aUnit, TInt aResult); |
|
152 |
void Configure(TUnit); // reset and configure an I2C unit |
|
153 |
void Deconfigure(TUnit); |
|
154 |
void TheIsr(void*); |
|
155 |
void TheDfc(TAny* aUnit); |
|
156 |
||
157 |
EXPORT_C TConfigPb::TConfigPb() : |
|
158 |
iUnit((TUnit)-1), // ensure that an un-initialised cpb will return KErrArgument from Open() |
|
159 |
iExclusiveClient(0), |
|
160 |
iDeviceAddress(1) |
|
161 |
{} |
|
162 |
||
163 |
EXPORT_C TTransferPb::TTransferPb() : |
|
164 |
iNextPhase(0) |
|
165 |
{} |
|
166 |
||
167 |
EXPORT_C THandle Open(const TConfigPb& aConfig) |
|
168 |
{ |
|
51
254b9435d75e
Fixed build warnings
Lukasz Forynski <lukasz.forynski@gmail.com>
parents:
6
diff
changeset
|
169 |
#ifdef USE_SYMBIAN_PRM |
0 | 170 |
//TInt r = PowerResourceManager::RegisterClient( prmClientId, KDfcName ); |
171 |
//__NK_ASSERT_ALWAYS(r==KErrNone); |
|
51
254b9435d75e
Fixed build warnings
Lukasz Forynski <lukasz.forynski@gmail.com>
parents:
6
diff
changeset
|
172 |
#error FIXME: the DFC, along with the associated dfcq have to be created (e.g. in DLL entry point?) |
254b9435d75e
Fixed build warnings
Lukasz Forynski <lukasz.forynski@gmail.com>
parents:
6
diff
changeset
|
173 |
#endif |
0 | 174 |
THandle h; |
175 |
__NK_ASSERT_ALWAYS(aConfig.iVersion == I2C_VERSION); |
|
176 |
if (aConfig.iUnit >= E1 && aConfig.iUnit <= E3) |
|
177 |
{ |
|
178 |
TUnitControl& unit = gUcb[aConfig.iUnit]; |
|
179 |
Kern::MutexWait( *unit.iOpenMutex ); |
|
180 |
||
181 |
if (unit.iNumDevices == 0) |
|
182 |
{ |
|
183 |
if (aConfig.iRole == EMaster && |
|
184 |
aConfig.iMode == E7Bit && |
|
185 |
aConfig.iRate >= E100K && aConfig.iRate <= E400K) |
|
186 |
{ |
|
187 |
unit.iRole = aConfig.iRole; |
|
188 |
unit.iMode = aConfig.iMode; |
|
189 |
unit.iExclusiveClient = aConfig.iExclusiveClient; |
|
190 |
unit.iRate = aConfig.iRate; |
|
191 |
unit.iDevice[unit.iNumDevices].iAddress = aConfig.iDeviceAddress; |
|
192 |
unit.iDevice[unit.iNumDevices++].iDfcQueue = aConfig.iDfcQueue; |
|
193 |
h = Handle(aConfig.iUnit, aConfig.iDeviceAddress); |
|
194 |
Configure(aConfig.iUnit); |
|
195 |
TInt r = Interrupt::Bind(KIrqId[aConfig.iUnit], TheIsr, (void*) aConfig.iUnit); |
|
196 |
__NK_ASSERT_DEBUG(r == KErrNone); |
|
197 |
} |
|
198 |
else |
|
199 |
{ |
|
200 |
h = KErrArgument; |
|
201 |
} |
|
202 |
} |
|
203 |
else // unit is already open |
|
204 |
{ |
|
205 |
if (unit.iNumDevices < KMaxDevicesPerUnit) |
|
206 |
{ |
|
207 |
if (unit.iRole == aConfig.iRole && |
|
208 |
unit.iMode == aConfig.iMode && |
|
209 |
unit.iExclusiveClient == aConfig.iExclusiveClient && |
|
210 |
unit.iRate == aConfig.iRate) |
|
211 |
{ |
|
212 |
h = 0; |
|
213 |
for (TInt i = 0; i < unit.iNumDevices; i++) |
|
214 |
{ |
|
215 |
if (unit.iDevice[i].iAddress == aConfig.iDeviceAddress) |
|
216 |
{ |
|
217 |
h = KErrInUse; |
|
218 |
break; |
|
219 |
} |
|
220 |
} |
|
221 |
if (h == 0) |
|
222 |
{ |
|
223 |
unit.iDevice[unit.iNumDevices].iAddress = aConfig.iDeviceAddress; |
|
224 |
unit.iDevice[unit.iNumDevices++].iDfcQueue = aConfig.iDfcQueue; |
|
225 |
h = Handle(aConfig.iUnit, aConfig.iDeviceAddress); |
|
226 |
} |
|
227 |
} |
|
228 |
else |
|
229 |
{ |
|
230 |
h = KErrInUse; |
|
231 |
} |
|
232 |
} |
|
233 |
else |
|
234 |
{ |
|
235 |
h = KErrTooBig; |
|
236 |
} |
|
237 |
} |
|
238 |
Kern::MutexSignal( *unit.iOpenMutex ); |
|
239 |
} |
|
240 |
else |
|
241 |
{ |
|
242 |
h = KErrArgument; |
|
243 |
} |
|
244 |
return h; |
|
245 |
} |
|
246 |
||
247 |
EXPORT_C void Close(THandle& aHandle) |
|
248 |
{ |
|
249 |
TUnit unitI = RawUnit(aHandle); |
|
250 |
||
251 |
if (unitI >= E1 && unitI <= E3) |
|
252 |
{ |
|
253 |
TUnitControl& unit = gUcb[unitI]; |
|
254 |
Kern::MutexWait( *unit.iOpenMutex ); |
|
255 |
||
256 |
TInt i = 0; |
|
257 |
for (; i < unit.iNumDevices; i++) |
|
258 |
{ |
|
259 |
if (unit.iDevice[i].iAddress == Device(aHandle)) |
|
260 |
{ |
|
261 |
unit.iNumDevices--; |
|
262 |
break; |
|
263 |
} |
|
264 |
} |
|
265 |
for (; i < unit.iNumDevices; i++) |
|
266 |
{ |
|
267 |
unit.iDevice[i] = unit.iDevice[i + 1]; |
|
268 |
} |
|
269 |
||
270 |
if (unit.iNumDevices == 0) |
|
271 |
{ |
|
272 |
(void) Interrupt::Unbind(KIrqId[unitI]); |
|
273 |
Deconfigure(TUnit(unitI)); |
|
274 |
} |
|
275 |
Kern::MutexSignal( *unit.iOpenMutex ); |
|
276 |
} |
|
277 |
aHandle = -1; |
|
278 |
//PowerResourceManager::DeRegisterClient(prmClientId); |
|
279 |
//prmClientId=0; |
|
280 |
} |
|
281 |
||
282 |
void AddToQueue( TUnitControl& aUnit, TDeviceControl& aDcb, TTransferPb& aWcb ) |
|
283 |
{ |
|
284 |
TInt irq = __SPIN_LOCK_IRQSAVE(aUnit.iLock); |
|
285 |
||
286 |
if (aUnit.iTransferQ == 0) |
|
287 |
{ |
|
288 |
__NK_ASSERT_DEBUG(aUnit.iState == TUnitControl::EIdle); |
|
289 |
aUnit.iTransferQ = &aWcb; |
|
290 |
aUnit.iCurrentPhase = &aWcb; |
|
291 |
aUnit.iTransferQTail = &aWcb; |
|
292 |
aUnit.iDfc.SetDfcQ(aDcb.iDfcQueue); |
|
293 |
aUnit.iDfc.Enque(); |
|
294 |
} |
|
295 |
else |
|
296 |
{ |
|
297 |
__NK_ASSERT_DEBUG(aUnit.iTransferQTail->iNextTransfer == 0); |
|
298 |
aUnit.iTransferQTail->iNextTransfer = &aWcb; |
|
299 |
aUnit.iTransferQTail = &aWcb; |
|
300 |
} |
|
301 |
__SPIN_UNLOCK_IRQRESTORE(unit.iLock, irq); |
|
302 |
} |
|
303 |
||
304 |
||
305 |
EXPORT_C TInt TransferS(THandle aHandle, TTransferPb& aWcb) |
|
306 |
{ |
|
307 |
__KTRACE_OPT(KI2C, __KTRACE_OPT(KI2C, Kern::Printf("+I2C:TransferS"))); |
|
308 |
||
309 |
CHECK_PRECONDITIONS(MASK_NOT_ISR, "I2c::TransferS"); |
|
310 |
||
311 |
aWcb.iNextTransfer = 0; |
|
312 |
aWcb.iCompletionDfc = 0; // indicate that it is a sync transfer and the FSM needs to Signal the semaphore |
|
313 |
TDeviceControl& dcb = DeviceCb(aHandle); |
|
314 |
aWcb.iDcb = &dcb; |
|
315 |
aWcb.iResult = (TInt)&dcb.iSyncSem; // use the async tranfer result member to store the semaphore // Todo: store ptr to dcb in aWcb |
|
316 |
||
317 |
NKern::FSSetOwner(&dcb.iSyncSem, 0); |
|
318 |
||
319 |
TUnitControl& unit = UnitCb(aHandle); |
|
320 |
||
321 |
AddToQueue( unit, dcb, aWcb ); |
|
322 |
||
323 |
NKern::FSWait(&dcb.iSyncSem); |
|
324 |
||
325 |
__KTRACE_OPT(KI2C, __KTRACE_OPT(KI2C, Kern::Printf("-I2C:TransferS:%d", aWcb.iResult))); |
|
326 |
||
327 |
return aWcb.iResult; |
|
328 |
} |
|
329 |
||
330 |
EXPORT_C void TransferA(THandle aHandle, TTransferPb& aWcb) |
|
331 |
{ |
|
332 |
__KTRACE_OPT(KI2C, __KTRACE_OPT(KI2C, Kern::Printf("+I2C:TransferA"))); |
|
333 |
||
334 |
CHECK_PRECONDITIONS(MASK_NOT_ISR, "I2c::TransferA"); |
|
335 |
||
336 |
aWcb.iNextTransfer = 0; |
|
337 |
TDeviceControl& dcb = DeviceCb(aHandle); |
|
338 |
aWcb.iDcb = &dcb; |
|
339 |
TUnitControl& unit = UnitCb(aHandle); |
|
340 |
||
341 |
AddToQueue( unit, dcb, aWcb ); |
|
342 |
||
343 |
__KTRACE_OPT(KI2C, __KTRACE_OPT(KI2C, Kern::Printf("-I2C:TransferA"))); |
|
344 |
} |
|
345 |
||
346 |
EXPORT_C void CancelATransfer(THandle) |
|
347 |
{ |
|
348 |
} |
|
349 |
||
350 |
inline TBool BitSet(TUint32 aWord, TUint32 aMask) |
|
351 |
{ |
|
352 |
return (aWord & aMask) != 0; |
|
353 |
} |
|
354 |
const TUint32 KStatBb = 1 << 12; |
|
355 |
const TUint32 KStatNack = 1 << 1; |
|
356 |
const TUint32 KStatAl = 1 << 0; |
|
357 |
const TUint32 KStatArdy = 1 << 2; |
|
358 |
const TUint32 KStatRdr = 1 << 13; |
|
359 |
const TUint32 KStatRRdy = 1 << 3; |
|
360 |
const TUint32 KStatXdr = 1 << 14; |
|
361 |
const TUint32 KStatXrdy = 1 << 4; |
|
362 |
const TUint32 KStatBf = 1 << 8; |
|
363 |
const TUint32 KStatInterupts = KStatXdr | KStatRdr | KStatBf | KStatXrdy | KStatRRdy | KStatArdy | KStatNack | KStatAl; |
|
364 |
||
365 |
const TUint32 KConMst = 1 << 10; |
|
366 |
const TUint32 KConI2cEn = 1 << 15; |
|
367 |
const TUint32 KConTrx = 1 << 9; |
|
368 |
const TUint32 KConStp = 1 << 1; |
|
369 |
const TUint32 KConStt = 1 << 0; |
|
370 |
||
371 |
void TheDfc(TAny* aUnit) |
|
372 |
{ |
|
373 |
TUnit unitI = (TUnit)(TInt)aUnit; |
|
374 |
TUnitControl& unit = gUcb[unitI]; |
|
375 |
||
376 |
__KTRACE_OPT(KI2C, __KTRACE_OPT(KI2C, Kern::Printf("I2C:DFC:S%d", unit.iState)) ); |
|
377 |
||
378 |
switch (unit.iState) |
|
379 |
{ |
|
380 |
case TUnitControl::EIdle: |
|
381 |
{ |
|
382 |
// 18.5.1.1.2 |
|
383 |
// 1 |
|
384 |
TTransferPb& tpb = *unit.iTransferQ; |
|
385 |
TTransferPb& ppb = *unit.iCurrentPhase; |
|
386 |
||
387 |
TUint32 con = KConI2cEn | KConMst; |
|
388 |
if (ppb.iType == TTransferPb::EWrite) |
|
389 |
{ |
|
390 |
con |= KConTrx; |
|
391 |
} |
|
392 |
AsspRegister::Write16(KI2C_CON[unitI], con); |
|
393 |
// 18.5.1.1.3 |
|
394 |
TUint32 sa = AsspRegister::Read16(KI2C_SA[unitI]); |
|
395 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:SA[%d]: 0x%04x<-0x%04x", unitI, sa, tpb.iDcb->iAddress)); |
|
396 |
AsspRegister::Write16(KI2C_SA[unitI], tpb.iDcb->iAddress); |
|
397 |
TUint32 cnt = AsspRegister::Read16(KI2C_CNT[unitI]); |
|
398 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:CNT[%d]: 0x%04x<-0x%04x", unitI, cnt, ppb.iLength)); |
|
399 |
AsspRegister::Write16(KI2C_CNT[unitI], ppb.iLength); |
|
400 |
// 18.5.1.1.4 |
|
401 |
if (ppb.iNextPhase == 0) // last phase |
|
402 |
{ |
|
403 |
con |= KConStp; // STP |
|
404 |
} |
|
405 |
con |= KConStt; // STT |
|
406 |
if (&tpb == &ppb) // first phase |
|
407 |
{ |
|
408 |
TInt im = NKern::DisableAllInterrupts(); // ensure that the transaction is started while the bus is free |
|
409 |
TUint32 stat = AsspRegister::Read16(KI2C_STAT[unitI]); |
|
410 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:STAT[%d]: 0x%04x", unitI, stat)); |
|
411 |
__NK_ASSERT_ALWAYS(!BitSet(stat, KStatBb)); // if multi-master then need a polling state with a timeout |
|
412 |
AsspRegister::Write16(KI2C_CON[unitI], con); |
|
413 |
NKern::RestoreInterrupts(im); |
|
414 |
} |
|
415 |
else // a follow on phase |
|
416 |
{ |
|
417 |
AsspRegister::Write16(KI2C_CON[unitI], con); |
|
418 |
} |
|
419 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:CON[%d]: 0x%04x", unitI, con)); |
|
420 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:..CNT[%d]: 0x%04x", unitI, AsspRegister::Read16(KI2C_CNT[unitI]))); |
|
421 |
||
422 |
if (ppb.iType == TTransferPb::ERead) |
|
423 |
{ |
|
424 |
unit.iState = TUnitControl::ERead; |
|
425 |
} |
|
426 |
else |
|
427 |
{ |
|
428 |
unit.iState = TUnitControl::EWrite; |
|
429 |
} |
|
430 |
} |
|
431 |
break; |
|
432 |
case TUnitControl::ERead: |
|
433 |
case TUnitControl::EWrite: |
|
434 |
{ |
|
435 |
TTransferPb& ppb = *unit.iCurrentPhase; |
|
436 |
TUint32 stat = AsspRegister::Read16(KI2C_STAT[unitI]); |
|
437 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:STAT[%d]: 0x%04x", unitI, stat)); |
|
438 |
do |
|
439 |
{ |
|
440 |
if (BitSet(stat, KStatNack)) |
|
441 |
{ |
|
442 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:N")); |
|
443 |
Configure(unitI); // reset the whole unit. Need more testing to determine the correct behavior. |
|
444 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:CON|STAT[%d]: 0x%04x|0x%04x", unitI, AsspRegister::Read16(KI2C_CON[unitI]), AsspRegister::Read16(KI2C_STAT[unitI]))); |
|
445 |
Complete(unit, KErrGeneral); |
|
446 |
return; |
|
447 |
} |
|
448 |
if (BitSet(stat, KStatAl)) |
|
449 |
{ |
|
450 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:A")); |
|
451 |
AsspRegister::Write16(KI2C_STAT[unitI], KStatAl); |
|
452 |
||
453 |
if((AsspRegister::Read16(KI2C_CON[unitI]) & (KConMst | KConStp)) == 0) |
|
454 |
{ |
|
455 |
AsspRegister::Modify16(KI2C_CON[unitI], KClearNone, KConStp); |
|
456 |
Complete(unit, KErrGeneral); |
|
457 |
return; |
|
458 |
} |
|
459 |
} |
|
460 |
if (BitSet(stat, KStatArdy)) |
|
461 |
{ |
|
462 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:Y")); |
|
463 |
AsspRegister::Write16(KI2C_STAT[unitI], KStatArdy); |
|
464 |
||
465 |
if (ppb.iNextPhase != 0) |
|
466 |
{ |
|
467 |
unit.iCurrentPhase = ppb.iNextPhase; |
|
468 |
unit.iState = TUnitControl::EIdle; |
|
469 |
unit.iDfc.Enque(); |
|
470 |
return; |
|
471 |
} |
|
472 |
else |
|
473 |
{ |
|
474 |
Complete(unit, KErrNone); |
|
475 |
return; |
|
476 |
} |
|
477 |
} |
|
478 |
if (BitSet(stat, KStatRdr)) |
|
479 |
{ |
|
480 |
__NK_ASSERT_DEBUG(unit.iState == TUnitControl::ERead); |
|
481 |
TUint32 rxstat = AsspRegister::Read16(KI2C_BUFSTAT[unitI]) >> 8; |
|
482 |
rxstat &= 0x3f; |
|
483 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:R%d", rxstat)); |
|
484 |
for (TUint i = 0; i < rxstat; i++) |
|
485 |
{ |
|
486 |
TUint8* d = const_cast<TUint8*>(ppb.iData++); |
|
487 |
*d = (TUint8) AsspRegister::Read16(KI2C_DATA[unitI]); |
|
488 |
} |
|
489 |
AsspRegister::Write16(KI2C_STAT[unitI], KStatRdr); |
|
490 |
} |
|
491 |
else if (BitSet(stat, KStatRRdy)) |
|
492 |
{ |
|
493 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:..BUF:%x BUFSTAT:%x", AsspRegister::Read16(KI2C_BUF[unitI]), AsspRegister::Read16(KI2C_BUFSTAT[unitI]))); |
|
494 |
__NK_ASSERT_DEBUG(unit.iState == TUnitControl::ERead); |
|
495 |
TUint32 rtrsh = AsspRegister::Read16(KI2C_BUF[unitI]) >> 8; |
|
496 |
rtrsh &= 0x3f; |
|
497 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:RD%d", rtrsh + 1)); |
|
498 |
for (TUint i = 0; i < rtrsh + 1; i++) |
|
499 |
{ |
|
500 |
TUint8* d = const_cast<TUint8*>(ppb.iData++); |
|
501 |
*d = (TUint8) AsspRegister::Read16(KI2C_DATA[unitI]); |
|
502 |
} |
|
503 |
AsspRegister::Write16(KI2C_STAT[unitI], KStatRRdy); |
|
504 |
} |
|
505 |
if (BitSet(stat, KStatXdr)) |
|
506 |
{ |
|
507 |
__NK_ASSERT_DEBUG(unit.iState == TUnitControl::EWrite); |
|
508 |
TUint32 txstat = AsspRegister::Read16(KI2C_BUFSTAT[unitI]); |
|
509 |
txstat &= 0x3f; |
|
510 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:W%d", txstat)); |
|
511 |
for (TUint i = 0; i < txstat; i++) |
|
512 |
{ |
|
513 |
AsspRegister::Write16(KI2C_DATA[unitI], *ppb.iData++); |
|
514 |
} |
|
515 |
AsspRegister::Write16(KI2C_STAT[unitI], KStatXdr); |
|
516 |
} |
|
517 |
else if (BitSet(stat, KStatXrdy)) |
|
518 |
{ |
|
519 |
__NK_ASSERT_DEBUG(unit.iState == TUnitControl::EWrite); |
|
520 |
TUint32 xtrsh = AsspRegister::Read16(KI2C_BUF[unitI]); |
|
521 |
xtrsh &= 0x3f; |
|
522 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:WD%d", xtrsh + 1)); |
|
523 |
for (TUint i = 0; i < xtrsh + 1; i++) |
|
524 |
{ |
|
525 |
AsspRegister::Write16(KI2C_DATA[unitI], *ppb.iData++); |
|
526 |
} |
|
527 |
AsspRegister::Write16(KI2C_STAT[unitI], KStatXrdy); |
|
528 |
} |
|
529 |
/* if (stat == KStatBf) |
|
530 |
{ |
|
531 |
__KTRACE_OPT(KI2C, Kern::Printf("F")); |
|
532 |
__NK_ASSERT_ALWAYS(ppb.iNextPhase == 0); |
|
533 |
AsspRegister::Write16(KI2C_STAT[unitI], KStatBf); |
|
534 |
Complete(unit, KErrNone); |
|
535 |
return; |
|
536 |
} |
|
537 |
*/ stat = AsspRegister::Read16(KI2C_STAT[unitI]); |
|
538 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:STAT[%d]: 0x%04x", unitI, stat)); |
|
539 |
} while (BitSet(stat, KStatInterupts)); |
|
540 |
} |
|
541 |
break; |
|
542 |
} |
|
543 |
Interrupt::Enable(KIrqId[unitI]); |
|
544 |
} |
|
545 |
||
546 |
TUnitControl::TUnitControl() : |
|
6
b277c89230a2
Tweaks to remove Symbian^2 workarounds which break on Symbian^3
William Roberts <williamr@symbian.org>
parents:
0
diff
changeset
|
547 |
iLock(TSpinLock::EOrderGenericIrqLow1), |
0 | 548 |
iDfc(TheDfc, 0, 1), |
549 |
iNumDevices(0), |
|
550 |
iTransferQ(0) |
|
551 |
{ |
|
552 |
iDfc.iPtr = (void*)(this - gUcb); // unit index |
|
553 |
__ASSERT_ALWAYS( Kern::MutexCreate( iOpenMutex, KNullDesC, KMutexOrdGeneral0 ) == KErrNone, Kern::Fault( "I2C", __LINE__ ) ); |
|
554 |
} |
|
555 |
||
556 |
void TheIsr(void* aUnit) |
|
557 |
{ |
|
558 |
TUnit unitI = (TUnit)(TInt)aUnit; |
|
559 |
Interrupt::Disable(KIrqId[unitI]); |
|
560 |
||
561 |
TUnitControl& unit = gUcb[unitI]; |
|
562 |
__KTRACE_OPT(KI2C, __KTRACE_OPT(KI2C, Kern::Printf("=I2C:DFC:u%x", &unit ))); |
|
563 |
unit.iDfc.Add(); |
|
564 |
} |
|
565 |
||
566 |
void Configure(TUnit aUnitI) |
|
567 |
{ |
|
568 |
__ASSERT_NO_FAST_MUTEX; |
|
569 |
__NK_ASSERT_ALWAYS(aUnitI<3); |
|
570 |
// 18.5.1.1.1 |
|
571 |
// 1 |
|
572 |
//TInt r = PowerResourceManager::ChangeResourceState( prmClientId, Omap3530Prm::EPrmClkI2c1_F+aUnitI, Prcm::EClkOn ); |
|
573 |
//r = PowerResourceManager::ChangeResourceState( prmClientId, Omap3530Prm::EPrmClkI2c1_I+aUnitI, Prcm::EClkOn ); |
|
574 |
TUint32 iClkEn = AsspRegister::Read16(KCM_ICLKEN1_CORE); |
|
575 |
TUint32 fClkEn = AsspRegister::Read16(KCM_FCLKEN1_CORE); |
|
576 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:CM_I|FCLKEN1[%d]: 0x%04x|0x%04x", aUnitI, iClkEn, fClkEn)); |
|
577 |
AsspRegister::Modify32(KCM_ICLKEN1_CORE, 0, 1 << 15 + aUnitI); |
|
578 |
AsspRegister::Modify32(KCM_FCLKEN1_CORE, 0, 1 << 15 + aUnitI); |
|
579 |
// Reset |
|
580 |
AsspRegister::Write16(KI2C_SYSC[aUnitI], 0x0002); |
|
581 |
||
582 |
if (gUcb[aUnitI].iRate == E100K) |
|
583 |
{ |
|
584 |
// 2 |
|
585 |
AsspRegister::Write16(KI2C_PSC[aUnitI], 23); |
|
586 |
// 3 + 4 |
|
587 |
AsspRegister::Write16(KI2C_SCLL[aUnitI], 0x000d); // 100kHz F/S, 400kHz HS |
|
588 |
AsspRegister::Write16(KI2C_SCLH[aUnitI], 0x000f); |
|
589 |
} |
|
590 |
else if (gUcb[aUnitI].iRate == E400K) |
|
591 |
{ |
|
592 |
// 2 |
|
593 |
AsspRegister::Write16(KI2C_PSC[aUnitI], 9); |
|
594 |
// 3 + 4 |
|
595 |
AsspRegister::Write16(KI2C_SCLL[aUnitI], 0x0005); // 400kHz F/S, 400kHz HS |
|
596 |
AsspRegister::Write16(KI2C_SCLH[aUnitI], 0x0007); |
|
597 |
} |
|
598 |
// 6 |
|
599 |
AsspRegister::Write16(KI2C_OA1[aUnitI], gUcb[aUnitI].iOwnAddress); |
|
600 |
// 7 |
|
601 |
TUint32 buf = AsspRegister::Read16(KI2C_BUF[aUnitI]); |
|
602 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:I2C_BUF[%d]: 0x%04x", aUnitI, buf)); |
|
603 |
// 8 |
|
604 |
TUint32 con = AsspRegister::Read16(KI2C_CON[aUnitI]); |
|
605 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:I2C_CON[%d]: 0x%04x<-0x%04x", aUnitI, con, con | 1 << 15)); |
|
606 |
AsspRegister::Modify16(KI2C_CON[aUnitI], 0, 1 << 15); |
|
607 |
||
608 |
TUint32 syss = AsspRegister::Read16(KI2C_SYSS[aUnitI]); |
|
609 |
__NK_ASSERT_DEBUG(syss == 0x1); |
|
610 |
||
611 |
// set-up interrupts |
|
612 |
TUint32 ie = AsspRegister::Read16(KI2C_IE[aUnitI]); |
|
613 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C:IE[%d]: 0x%04x<-0x%04x", aUnitI, ie, KStatInterupts)); |
|
614 |
AsspRegister::Write16(KI2C_IE[aUnitI], KStatInterupts); |
|
615 |
} |
|
616 |
||
617 |
void Deconfigure(TUnit aUnitI) |
|
618 |
{ |
|
619 |
__ASSERT_NO_FAST_MUTEX; |
|
620 |
__NK_ASSERT_ALWAYS(aUnitI<3); |
|
621 |
//TInt r = PowerResourceManager::ChangeResourceState( prmClientId, Omap3530Prm::EPrmClkI2c1_F+aUnitI, Prcm::EClkOff ); |
|
622 |
//__KTRACE_OPT(KBOOT, Kern::Printf("EPrmClkI2c%d_F DIS %d", aUnitI, r)); |
|
623 |
//r = PowerResourceManager::ChangeResourceState( prmClientId, Omap3530Prm::EPrmClkI2c1_I+aUnitI, Prcm::EClkOff ); |
|
624 |
//__KTRACE_OPT(KBOOT, Kern::Printf("EPrmClkI2c%d_I DIS %d", aUnitI, r)); |
|
625 |
AsspRegister::Modify32(KCM_ICLKEN1_CORE, 1 << 15 + aUnitI, 0); |
|
626 |
AsspRegister::Modify32(KCM_FCLKEN1_CORE, 1 << 15 + aUnitI, 0); |
|
627 |
} |
|
628 |
||
629 |
THandle Handle(TUnit aUnit, TDeviceAddress aDeviceAddress) |
|
630 |
{ |
|
631 |
return THandle(aUnit << 16 | aDeviceAddress); |
|
632 |
} |
|
633 |
||
634 |
TUnit RawUnit(THandle aHandle) |
|
635 |
{ |
|
636 |
TUnit r = TUnit(aHandle >> 16); |
|
637 |
return r; |
|
638 |
} |
|
639 |
||
640 |
TUnit Unit(THandle aHandle) |
|
641 |
{ |
|
642 |
TUnit r = RawUnit(aHandle); |
|
643 |
if (r < E1 || r > E3) |
|
644 |
{ |
|
645 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C Unit out of range: %d", r)); |
|
646 |
r = E1; |
|
647 |
} |
|
648 |
return r; |
|
649 |
} |
|
650 |
||
651 |
TUnitControl& UnitCb(THandle aHandle) |
|
652 |
{ |
|
653 |
return gUcb[Unit(aHandle)]; |
|
654 |
} |
|
655 |
||
656 |
TDeviceAddress Device(THandle aHandle) |
|
657 |
{ |
|
658 |
TDeviceAddress r = TDeviceAddress(aHandle & 0x0000ffff); |
|
659 |
if (r < 0 || r > 1023) |
|
660 |
{ |
|
661 |
__KTRACE_OPT(KI2C, Kern::Printf("I2C Device out of range: %d", r)); |
|
662 |
} |
|
663 |
return r; |
|
664 |
} |
|
665 |
||
666 |
TDeviceControl& DeviceCb(THandle aHandle) |
|
667 |
{ |
|
668 |
TUnitControl& unit = UnitCb(aHandle); |
|
669 |
TDeviceAddress device = Device(aHandle); |
|
670 |
TInt i = 0; |
|
671 |
for (; i < unit.iNumDevices; i++) |
|
672 |
{ |
|
673 |
if (unit.iDevice[i].iAddress == device) |
|
674 |
{ |
|
675 |
break; |
|
676 |
} |
|
677 |
} |
|
678 |
return unit.iDevice[i]; |
|
679 |
} |
|
680 |
||
681 |
void Complete(TUnitControl& aUnit, TInt aResult) |
|
682 |
{ |
|
683 |
aUnit.iTransferQ->iResult = aResult; |
|
684 |
aUnit.iState = TUnitControl::EIdle; |
|
685 |
||
686 |
TInt irq = __SPIN_LOCK_IRQSAVE(aUnit.iLock); |
|
687 |
TTransferPb& tpb = *aUnit.iTransferQ; |
|
688 |
aUnit.iTransferQ = aUnit.iTransferQ->iNextTransfer; |
|
689 |
__SPIN_UNLOCK_IRQRESTORE(aUnit.iLock, irq); |
|
690 |
||
691 |
if (tpb.iCompletionDfc == 0) |
|
692 |
{ |
|
693 |
NKern::FSSignal(&tpb.iDcb->iSyncSem); |
|
694 |
} |
|
695 |
else |
|
696 |
{ |
|
697 |
tpb.iCompletionDfc->Enque(); |
|
698 |
} |
|
699 |
} |
|
700 |
||
701 |
} // namespace I2c |
|
702 |
||
703 |
namespace I2cReg |
|
704 |
{ |
|
705 |
EXPORT_C TUint8 ReadB(I2c::THandle aH, TUint8 aAddr) |
|
706 |
{ |
|
707 |
const TUint8 KAddress = aAddr; |
|
708 |
I2c::TTransferPb addressPhase; |
|
709 |
addressPhase.iType = I2c::TTransferPb::EWrite; |
|
710 |
addressPhase.iLength = 1; |
|
711 |
addressPhase.iData = &KAddress; |
|
712 |
||
713 |
TUint8 readData; |
|
714 |
I2c::TTransferPb dataPhase; |
|
715 |
dataPhase.iType = I2c::TTransferPb::ERead; |
|
716 |
dataPhase.iLength = 1; |
|
717 |
dataPhase.iData = &readData; |
|
718 |
||
719 |
addressPhase.iNextPhase = &dataPhase; // link into a two phase transfer |
|
720 |
||
721 |
TInt r = KErrNone; |
|
722 |
TInt retryCount = 0; |
|
723 |
||
724 |
do |
|
725 |
{ |
|
726 |
r=I2c::TransferS(aH, addressPhase); |
|
727 |
retryCount++; |
|
728 |
} |
|
729 |
while (r != KErrNone && retryCount < 5); |
|
730 |
||
731 |
__NK_ASSERT_ALWAYS(r == KErrNone); |
|
732 |
||
733 |
return readData; |
|
734 |
} |
|
735 |
||
736 |
EXPORT_C void WriteB(I2c::THandle aH, TUint8 aAddr, TUint8 aData) |
|
737 |
{ |
|
738 |
const TUint8 KAddrData[2] = {aAddr, aData}; |
|
739 |
I2c::TTransferPb fullTransfer; |
|
740 |
fullTransfer.iType = I2c::TTransferPb::EWrite; |
|
741 |
fullTransfer.iLength = 2; |
|
742 |
fullTransfer.iData = KAddrData; |
|
743 |
||
744 |
TInt r = KErrNone; |
|
745 |
TInt retryCount = 0; |
|
746 |
||
747 |
do |
|
748 |
{ |
|
749 |
r=I2c::TransferS(aH, fullTransfer); |
|
750 |
retryCount++; |
|
751 |
} |
|
752 |
while (r != KErrNone && retryCount < 5); |
|
753 |
||
754 |
__NK_ASSERT_ALWAYS(r == KErrNone); |
|
755 |
} |
|
756 |
} // namespace I2cReg |