|
1 // Copyright (c) 2010 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 // lukasz.forynski@gmail.com |
|
13 // |
|
14 // Description: |
|
15 // Implementation of IIC master channel for a SPI bus. |
|
16 // |
|
17 |
|
18 #define DBGPRINT(x) |
|
19 #define DBG_ERR(x) x |
|
20 |
|
21 |
|
22 #ifdef _DEBUG |
|
23 #define DEBUG_ONLY(x) //x |
|
24 #else |
|
25 #define DEBUG_ONLY(x) |
|
26 #endif |
|
27 |
|
28 |
|
29 // DO NOT CHANGE THESE- trying to tune the driver (unless you really know what you're doing) |
|
30 // as this this is only for development purpose to tune the driver. Fifo mode is not yet enabled, but this |
|
31 // doesn't affect operation. After development has been finished - these macros and #ifdefs will be removed |
|
32 // entirely. For now only SINGLE_MODE should ever be defined. |
|
33 //#define USE_TX_FIFO |
|
34 //#define USING_TX_COUNTER |
|
35 //#define PER_TRANSFER_MODE |
|
36 #define SINGLE_MODE |
|
37 |
|
38 #include <assp/omap3530_assp/omap3530_assp_priv.h> |
|
39 #include <assp/omap3530_assp/omap3530_prcm.h> |
|
40 #include <drivers/iic.h> |
|
41 #include "omap3530_spi.h" |
|
42 #include "psl_init.h" |
|
43 #include "master.h" |
|
44 |
|
45 DSpiMasterBeagle::DSpiMasterBeagle(TInt aChannelNumber, TBusType aBusType, TChannelDuplex aChanDuplex) : |
|
46 DIicBusChannelMaster(aBusType, aChanDuplex), |
|
47 iTransferEndDfc(TransferEndDfc, this, KIicPslDfcPriority) |
|
48 { |
|
49 iChannelNumber = aChannelNumber; |
|
50 iIrqId = KMcSpiIrqId[iChannelNumber]; |
|
51 iHwBase = KMcSpiRegBase[iChannelNumber]; |
|
52 iState = EIdle; |
|
53 DBGPRINT(Kern::Printf("DSpiMasterBeagle::DSpiMasterBeagle: at 0x%x, iChannelNumber = %d", this, iChannelNumber)); |
|
54 } |
|
55 |
|
56 TInt DSpiMasterBeagle::DoCreate() |
|
57 { |
|
58 DBGPRINT(Kern::Printf("\nDSpiMasterBeagle::DoCreate() ch: %d \n", iChannelNumber)); |
|
59 DBGPRINT(Kern::Printf("HW revision is %x", AsspRegister::Read32(iHwBase + MCSPI_REVISION))); |
|
60 |
|
61 TInt r = KErrNone; |
|
62 |
|
63 // Create the DFCQ to be used by the channel |
|
64 if(!iDfcQ) |
|
65 { |
|
66 TBuf8<KMaxName> threadName (KIicPslThreadName); |
|
67 threadName.AppendNum(iChannelNumber); |
|
68 r = Kern::DfcQCreate(iDfcQ, KIicPslThreadPriority, &threadName); |
|
69 if(r != KErrNone) |
|
70 { |
|
71 DBG_ERR(Kern::Printf("DFC Queue creation failed, channel number: %d, r = %d\n", iChannelNumber, r)); |
|
72 return r; |
|
73 } |
|
74 } |
|
75 |
|
76 // PIL Base class initialization - this must be called prior to SetDfcQ(iDfcQ) |
|
77 r = Init(); |
|
78 if(r == KErrNone) |
|
79 { |
|
80 // Call base class function to set DFCQ pointers in the required objects |
|
81 // This also enables the channel to process transaction requests |
|
82 SetDfcQ(iDfcQ); |
|
83 |
|
84 // PSL DFCQ initialisation for local DFC |
|
85 iTransferEndDfc.SetDfcQ(iDfcQ); |
|
86 |
|
87 // Bind interrupts. |
|
88 r = Interrupt::Bind(iIrqId, Isr, this); |
|
89 if(r < KErrNone) |
|
90 { |
|
91 DBG_ERR(Kern::Printf("ERROR: InterruptBind error.. %d", r)); |
|
92 return r; |
|
93 } |
|
94 } |
|
95 |
|
96 // Make sure clocks are enabled (TBD: this could go to 'PowerUp/PowerDown' if using PRM) |
|
97 Prcm::SetClockState( Prcm::EClkMcSpi3_F, Prcm::EClkOn ); |
|
98 Prcm::SetClockState( Prcm::EClkMcSpi3_I, Prcm::EClkOn ); |
|
99 // TODO:consider auto-idle for PRCM.CM_AUTOIDLE1_CORE |
|
100 |
|
101 SetupSpiPins(iChannelNumber); |
|
102 // end of system wide settings.. |
|
103 |
|
104 return r; |
|
105 } |
|
106 |
|
107 // A static method used to construct the DSpiMasterBeagle object. |
|
108 DSpiMasterBeagle* DSpiMasterBeagle::New(TInt aChannelNumber, const TBusType aBusType, const TChannelDuplex aChanDuplex) |
|
109 { |
|
110 DBGPRINT(Kern::Printf("DSpiMasterBeagle::NewL(): ChannelNumber = %d, BusType =%d", aChannelNumber, aBusType)); |
|
111 DSpiMasterBeagle *pChan = new DSpiMasterBeagle(aChannelNumber, aBusType, aChanDuplex); |
|
112 |
|
113 TInt r = KErrNoMemory; |
|
114 if(pChan) |
|
115 { |
|
116 r = pChan->DoCreate(); |
|
117 } |
|
118 if(r != KErrNone) |
|
119 { |
|
120 delete pChan; |
|
121 pChan = NULL; |
|
122 } |
|
123 return pChan; |
|
124 } |
|
125 |
|
126 // This method is called by the PIL to initiate the transaction. After finishing it's processing, |
|
127 // the PSL calls the PIL function CompleteRequest to indicate the success (or otherwise) of the request |
|
128 TInt DSpiMasterBeagle::DoRequest(TIicBusTransaction* aTransaction) |
|
129 { |
|
130 DBGPRINT(Kern::Printf("\n=>DSpiMasterBeagle::DoRequest (aTransaction=0x%x)\n", aTransaction)); |
|
131 |
|
132 // If the pointer to the transaction passed in as a parameter, or its associated pointer to the |
|
133 // header information is NULL, return KErrArgument |
|
134 if(!aTransaction || !GetTransactionHeader(aTransaction)) |
|
135 { |
|
136 return KErrArgument; |
|
137 } |
|
138 |
|
139 // The PSL operates a simple state machine to ensure that only one transaction is processed |
|
140 // at a time - if the channel is currently busy, reject the request (PIL should not try that!) |
|
141 if(iState != EIdle) |
|
142 { |
|
143 return KErrInUse; |
|
144 } |
|
145 |
|
146 // copy pointer to the transaction |
|
147 iCurrTransaction = aTransaction; |
|
148 |
|
149 // Configure the hardware to support the transaction |
|
150 TInt r = KErrNone; |
|
151 if(TransConfigDiffersFromPrev()) |
|
152 { |
|
153 r = ConfigureInterface(); |
|
154 if(r != KErrNone) |
|
155 { |
|
156 return r; |
|
157 } |
|
158 } |
|
159 |
|
160 // start processing transfers of this transaction. |
|
161 r = ProcessNextTransfers(); |
|
162 return r; |
|
163 } |
|
164 |
|
165 TBool DSpiMasterBeagle::TransConfigDiffersFromPrev() |
|
166 { |
|
167 TConfigSpiV01 &newHeader = (*(TConfigSpiBufV01*) (GetTransactionHeader(iCurrTransaction)))(); |
|
168 |
|
169 // get the slave address (i.e. known as a 'channel' for the current SPI module) |
|
170 TInt slaveAddr = GET_SLAVE_ADDR(iCurrTransaction->GetBusId()); |
|
171 DBGPRINT(Kern::Printf("slaveAddr %x", slaveAddr)); |
|
172 |
|
173 // compare it to the previous configuration.. |
|
174 if(slaveAddr != iCurrSS || |
|
175 newHeader.iWordWidth != iCurrHeader.iWordWidth || |
|
176 newHeader.iClkSpeedHz != iCurrHeader.iClkSpeedHz || |
|
177 newHeader.iClkMode != iCurrHeader.iClkMode || |
|
178 newHeader.iTimeoutPeriod != iCurrHeader.iTimeoutPeriod || |
|
179 newHeader.iBitOrder != iCurrHeader.iBitOrder || |
|
180 newHeader.iTransactionWaitCycles != iCurrHeader.iTransactionWaitCycles || |
|
181 newHeader.iSSPinActiveMode != iCurrHeader.iSSPinActiveMode) |
|
182 { |
|
183 iCurrSS = slaveAddr; |
|
184 iCurrHeader = newHeader; //copy the header.. |
|
185 return ETrue; |
|
186 } |
|
187 return ETrue; |
|
188 } |
|
189 |
|
190 // Init the hardware with the data provided in the transaction and slave-address field |
|
191 // (these values are already stored in the iCurrHeader) |
|
192 TInt DSpiMasterBeagle::ConfigureInterface() |
|
193 { |
|
194 DBGPRINT(Kern::Printf("ConfigureInterface()")); |
|
195 |
|
196 // soft reset the SPI..(Channel 3 for now) |
|
197 TUint val = AsspRegister::Read32(iHwBase + MCSPI_SYSCONFIG); |
|
198 val = MCSPI_SYSCONFIG_SOFTRESET; // issue reset |
|
199 |
|
200 AsspRegister::Write32(iHwBase + MCSPI_SYSCONFIG, MCSPI_SYSCONFIG_SOFTRESET); |
|
201 |
|
202 val = 0; // TODO will add here this 'smart-wait' stuff that was proposed earlier.. |
|
203 while (!(val & MCSPI_SYSSTATUS_RESETDONE)) |
|
204 val = AsspRegister::Read32(iHwBase + MCSPI_SYSSTATUS); |
|
205 |
|
206 //AsspRegister::Write32(iHwBase + MCSPI_SYSCONFIG, MCSPI_SYSCONFIG_CLOCKACTIVITY_ALL_ON); |
|
207 |
|
208 AsspRegister::Write32(iHwBase + MCSPI_IRQSTATUS, ~0); // clear all interrupts (for now) -- normally only for channel.. |
|
209 |
|
210 // channel configuration |
|
211 // Set the SPI1.MCSPI_CHxCONF[18] IS bit to 0 for the spi1_somi pin in receive mode. |
|
212 // val = MCSPI_CHxCONF_IS; // pin selection (somi - simo) |
|
213 // TODO configuration of PINS could also be configurable on a 'per SPI module' basis.. |
|
214 |
|
215 // Set the SPI1.MCSPI_CHxCONF[17] DPE1 bit to 0 and the SPI1.MCSPI_CHxCONF[16] DPE0 bit to 1 for the spi1.simo pin in transmit mode. |
|
216 val = MCSPI_CHxCONF_DPE0; |
|
217 |
|
218 // Set transmit & | receive mode for transmit only mode here. If needed - it will be changed dynamically. |
|
219 val |= MCSPI_CHxCONF_TRM_NO_RECEIVE; |
|
220 |
|
221 // set word length. |
|
222 val |= SpiWordWidth(iCurrHeader.iWordWidth); |
|
223 |
|
224 // use the appropriate word with (assuming the data is aligned to bytes). |
|
225 if(iCurrHeader.iWordWidth > ESpiWordWidth_16) |
|
226 { |
|
227 iWordSize = 4; |
|
228 } |
|
229 else if (iCurrHeader.iWordWidth > ESpiWordWidth_8) |
|
230 { |
|
231 iWordSize = 2; |
|
232 } |
|
233 else |
|
234 { |
|
235 iWordSize = 1; |
|
236 } |
|
237 |
|
238 // set Slave Select / Chip select signal mode |
|
239 val |= iCurrHeader.iSSPinActiveMode == ESpiCSPinActiveLow ? MCSPI_CHxCONF_EPOL_LOW : 0; |
|
240 |
|
241 // set the CLK POL and PHA (clock mode) |
|
242 val |= SpiClkMode(iCurrHeader.iClkMode); |
|
243 |
|
244 // Set clock. Note that CheckHdr() will be called prior to this function for this header, |
|
245 // so the value iClkSpeedHz is valid at this point, the KErrNotSupported is not possible |
|
246 // so the return value check can be ommited here |
|
247 val |= SpiClkValue(iCurrHeader.iClkSpeedHz); |
|
248 // __ASSERT_DEBUG(val >= KErrNone, Kern::Fault("spi/master.cpp, line: ", __LINE__)); |
|
249 |
|
250 #ifdef USE_TX_FIFO |
|
251 // enable fifo for transmission.. |
|
252 // Update me: this can only set in a 'single' mode.. or for only one channel |
|
253 // but at the momment IIC SPI is used in 'single' mode onlny.. |
|
254 val |= MCSPI_CHxCONF_FFEW; |
|
255 // val |= MCSPI_CHxCONF_FFER; // fifo enable for receive.. (TODO) |
|
256 #endif |
|
257 |
|
258 // update the register.. |
|
259 AsspRegister::Write32(iHwBase + MCSPI_CHxCONF(iCurrSS), val); |
|
260 |
|
261 // CS (SS) pin direction.. |
|
262 val = MCSPI_SYST_SPIDATDIR0; |
|
263 |
|
264 // drive csx pin hight or low |
|
265 val |= (iCurrHeader.iSSPinActiveMode == ESpiCSPinActiveLow)? 1 << iCurrSS : 0; |
|
266 AsspRegister::Write32(iHwBase + MCSPI_SYST, val); |
|
267 |
|
268 // Set the MS bit to 0 to provide the clock (ie. to setup as master) |
|
269 #ifndef SINGLE_MODE |
|
270 AsspRegister::Write32(iHwBase + MCSPI_MODULCTRL, MCSPI_MODULCTRL_MS_MASTER); |
|
271 #else |
|
272 AsspRegister::Write32(iHwBase + MCSPI_MODULCTRL, MCSPI_MODULCTRL_MS_MASTER | MCSPI_MODULCTRL_SINGLE); |
|
273 #endif |
|
274 |
|
275 return KErrNone; |
|
276 } |
|
277 |
|
278 TInt DSpiMasterBeagle::ProcessNextTransfers() |
|
279 { |
|
280 DBGPRINT(Kern::Printf("DSpiMasterBeagle::ProcessNextTransfers():%s", iState==EIdle ? "first" : "next")); |
|
281 |
|
282 // Since new transfers are strating, clear exisiting flags |
|
283 iOperation.iValue = TIicOperationType::ENop; |
|
284 |
|
285 // If this is the first transfer in the transaction the channel will be in state EIdle |
|
286 if(iState == EIdle) |
|
287 { |
|
288 // Get the pointer to half-duplex transfer object.. |
|
289 iHalfDTransfer = GetTransHalfDuplexTferPtr(iCurrTransaction); |
|
290 |
|
291 // Get the pointer to full-duplex transfer object.. |
|
292 iFullDTransfer = GetTransFullDuplexTferPtr(iCurrTransaction); |
|
293 |
|
294 // Update the channel state to EBusy and initialise the transaction status |
|
295 iState = EBusy; |
|
296 iTransactionStatus = KErrNone; |
|
297 |
|
298 // start timeout timer for this transaction |
|
299 StartSlaveTimeOutTimer(iCurrHeader.iTimeoutPeriod); |
|
300 } |
|
301 else |
|
302 // If not in state EIdle, get the next transfer in the linked-list held by the transaction |
|
303 { |
|
304 // Get the pointer the next half-duplex transfer object.. |
|
305 iHalfDTransfer = GetTferNextTfer(iHalfDTransfer); |
|
306 |
|
307 // Get the pointer to the next half-duplex transfer object.. |
|
308 if(iFullDTransfer) |
|
309 { |
|
310 iFullDTransfer = GetTferNextTfer(iFullDTransfer); |
|
311 } |
|
312 } |
|
313 |
|
314 TInt r = KErrNone; |
|
315 if(!iFullDTransfer && !iHalfDTransfer) |
|
316 { |
|
317 // There is no more to transfer - and all previous were were completed, |
|
318 DBGPRINT(Kern::Printf("All transfers completed successfully")); |
|
319 ExitComplete(KErrNone); |
|
320 } |
|
321 else |
|
322 { |
|
323 // Process next transfers |
|
324 TInt8 hDTrType = (TInt8) GetTferType(iHalfDTransfer); |
|
325 |
|
326 if(iFullDTransfer) |
|
327 { |
|
328 // For full-duplex transfer setup the read transfer first, as it doesn't |
|
329 // really start anything - SPI master starts operation when Tx (or clocks)starts.. |
|
330 |
|
331 if(hDTrType == TIicBusTransfer::EMasterRead) |
|
332 { |
|
333 r = StartTransfer(iHalfDTransfer, TIicBusTransfer::EMasterRead); |
|
334 if(r != KErrNone) |
|
335 { |
|
336 return r; |
|
337 } |
|
338 r = StartTransfer(iFullDTransfer, TIicBusTransfer::EMasterWrite); |
|
339 } |
|
340 else // hDTrType == TIicBusTransfer::EMasterWrite) |
|
341 { |
|
342 r = StartTransfer(iFullDTransfer, TIicBusTransfer::EMasterRead); |
|
343 if(r != KErrNone) |
|
344 { |
|
345 return r; |
|
346 } |
|
347 r = StartTransfer(iHalfDTransfer, TIicBusTransfer::EMasterWrite); |
|
348 } |
|
349 } |
|
350 else |
|
351 // This is a HalfDuplex transfer - so just start it |
|
352 { |
|
353 r = StartTransfer(iHalfDTransfer, hDTrType); |
|
354 } |
|
355 } |
|
356 return r; |
|
357 } |
|
358 |
|
359 TInt DSpiMasterBeagle::StartTransfer(TIicBusTransfer* aTransferPtr, TUint8 aType) |
|
360 { |
|
361 DBGPRINT(Kern::Printf("DSpiMasterBeagle::StartTransfer() @0x%x, aType: %s", |
|
362 aTransferPtr, aType == TIicBusTransfer::EMasterWrite ? "write" : "read")); |
|
363 |
|
364 if(aTransferPtr == NULL) |
|
365 { |
|
366 DBG_ERR(Kern::Printf("DSpiMasterBeagle::StartTransfer - NULL pointer\n")); |
|
367 return KErrArgument; |
|
368 } |
|
369 |
|
370 TInt r = KErrNone; |
|
371 |
|
372 switch(aType) |
|
373 { |
|
374 case TIicBusTransfer::EMasterWrite: |
|
375 { |
|
376 DBGPRINT(Kern::Printf("Starting EMasterWrite, duplex=%x", iFullDTransfer)); |
|
377 |
|
378 // Get a pointer to the transfer object's buffer, to facilitate passing arguments to DoTransfer |
|
379 const TDes8* desBufPtr = GetTferBuffer(aTransferPtr); |
|
380 |
|
381 DBGPRINT(Kern::Printf("Length %d, iWordSize %d", desBufPtr->Length(), iWordSize)); |
|
382 |
|
383 // Store the current address and ending address for Transmission - they are required by the ISR and DFC |
|
384 iTxData = (TInt8*) desBufPtr->Ptr(); |
|
385 iTxDataEnd = (TInt8*) (iTxData + desBufPtr->Length()); |
|
386 if ((TInt)iTxDataEnd % iWordSize) |
|
387 { |
|
388 DBG_ERR(Kern::Printf("Wrong configuration - word size does not match buffer length")); |
|
389 return KErrArgument; |
|
390 } |
|
391 |
|
392 DBGPRINT(Kern::Printf("Tx: Start: %x, End %x, bytes %d", iTxData, iTxDataEnd, desBufPtr->Length())); |
|
393 |
|
394 // Set the flag to indicate that we'll be transmitting data |
|
395 iOperation.iOp.iIsTransmitting = ETrue; |
|
396 |
|
397 // initiate the transmission.. |
|
398 r = DoTransfer(aType); |
|
399 if(r != KErrNone) |
|
400 { |
|
401 DBG_ERR(Kern::Printf("Starting Write failed, r = %d", r)); |
|
402 } |
|
403 break; |
|
404 } |
|
405 |
|
406 case TIicBusTransfer::EMasterRead: |
|
407 { |
|
408 DBGPRINT(Kern::Printf("Starting EMasterRead, duplex=%x", iFullDTransfer)); |
|
409 |
|
410 // Get a pointer to the transfer object's buffer, to facilitate passing arguments to DoTransfer |
|
411 const TDes8* aBufPtr = GetTferBuffer(aTransferPtr); |
|
412 |
|
413 // Store the current address and ending address for Reception - they are required by the ISR and DFC |
|
414 iRxData = (TInt8*) aBufPtr->Ptr(); |
|
415 iRxDataEnd = (TInt8*) (iRxData + aBufPtr->Length()); |
|
416 |
|
417 DBGPRINT(Kern::Printf("Rx: Start: %x, End %x, bytes %d", iRxData, iRxDataEnd, aBufPtr->Length())); |
|
418 |
|
419 // Set the flag to indicate that we'll be receiving data |
|
420 iOperation.iOp.iIsReceiving = ETrue; |
|
421 |
|
422 // initiate the reception |
|
423 r = DoTransfer(aType); |
|
424 if(r != KErrNone) |
|
425 { |
|
426 DBG_ERR(Kern::Printf("Starting Read failed, r = %d", r)); |
|
427 } |
|
428 break; |
|
429 } |
|
430 |
|
431 default: |
|
432 { |
|
433 DBG_ERR(Kern::Printf("Unsupported TransactionType %x", aType)); |
|
434 r = KErrArgument; |
|
435 break; |
|
436 } |
|
437 } |
|
438 |
|
439 return r; |
|
440 } |
|
441 |
|
442 // Method called by StartTransfer to actually initiate the transfers. |
|
443 TInt DSpiMasterBeagle::DoTransfer(TUint8 aType) |
|
444 { |
|
445 DBGPRINT(Kern::Printf("\nDSpiMasterBeagle::DoTransfer()")); |
|
446 TInt r = KErrNone; |
|
447 |
|
448 AsspRegister::Write32(iHwBase + MCSPI_IRQSTATUS, ~0); |
|
449 |
|
450 switch(aType) |
|
451 { |
|
452 case TIicBusTransfer::EMasterWrite: |
|
453 { |
|
454 // enable the channel here.. |
|
455 AsspRegister::Write32(iHwBase + MCSPI_CHxCTRL(iCurrSS), MCSPI_CHxCTRL_EN); |
|
456 |
|
457 AsspRegister::Modify32(iHwBase + MCSPI_IRQSTATUS, 0, |
|
458 MCSPI_IRQ_TX_EMPTY(iCurrSS) /*| MCSPI_IRQ_TX_UNDERFLOW(iCurrSS)*/); |
|
459 |
|
460 AsspRegister::Modify32(iHwBase + MCSPI_IRQENABLE, 0, |
|
461 MCSPI_IRQ_TX_EMPTY(iCurrSS) /*| MCSPI_IRQ_TX_UNDERFLOW(iCurrSS)*/); |
|
462 |
|
463 #ifdef SINGLE_MODE |
|
464 // in SINGLE mode needs to manually assert CS line for current |
|
465 AsspRegister::Modify32(iHwBase + MCSPI_CHxCONF(iCurrSS), 0, MCSPI_CHxCONF_FORCE); |
|
466 |
|
467 // change the pad config - now the SPI drives the line appropriately.. |
|
468 SetCsActive(iChannelNumber, iCurrSS, iCurrHeader.iSSPinActiveMode); |
|
469 #endif /*SINGLE_MODE*/ |
|
470 |
|
471 #ifdef USE_TX_FIFO |
|
472 const TInt KTxFifoThreshold = 8; |
|
473 TUint numWordsToTransfer = (iTxDataEnd - iTxData); |
|
474 TUint wordsToWrite = Min(numWordsToTransfer/iWordSize, KTxFifoThreshold/iWordSize); |
|
475 |
|
476 |
|
477 TInt iAlmostFullLevel = 0; |
|
478 TInt iAlmostEmptyLevel = 1; //KTxFifoThreshold; |
|
479 |
|
480 // setup FIFOs |
|
481 AsspRegister::Write32(iHwBase + MCSPI_XFERLEVEL, |
|
482 MCSPI_XFERLEVEL_WCNT(0) | // total num words |
|
483 MCSPI_XFERLEVEL_AFL(iAlmostFullLevel) | // Rx almost full |
|
484 MCSPI_XFERLEVEL_AEL(iAlmostEmptyLevel) ); // Tx almost empty |
|
485 |
|
486 // copy data to fifo.. |
|
487 for (TInt i = 0; i < wordsToWrite; i++) |
|
488 { |
|
489 iTxData += iWordSize; |
|
490 AsspRegister::Write32(iHwBase + MCSPI_TXx(iCurrSS), *(iTxData -iWordSize)); |
|
491 } |
|
492 |
|
493 #else /*USE_TX_FIFO*/ |
|
494 |
|
495 TUint val = 0; |
|
496 for (TInt i = 0; i < iWordSize; i++) |
|
497 { |
|
498 val |= (*iTxData++) << i * 8; |
|
499 } |
|
500 |
|
501 DEBUG_ONLY(DumpCurrentStatus("DoTransfer(Write)")); |
|
502 AsspRegister::Write32(iHwBase + MCSPI_TXx(iCurrSS), val); |
|
503 #endif /*USE_TX_FIFO*/ |
|
504 |
|
505 // enable system interrupt |
|
506 Interrupt::Enable(iIrqId); |
|
507 break; |
|
508 } |
|
509 case TIicBusTransfer::EMasterRead: |
|
510 { |
|
511 // enable transmit and receive.. |
|
512 AsspRegister::Modify32(iHwBase + MCSPI_CHxCONF(iCurrSS), MCSPI_CHxCONF_TRM_NO_RECEIVE, 0); |
|
513 |
|
514 // for single read (not duplex) one way to to allow clock generation is to enable Tx |
|
515 // and write '0' to Txregister (just like in duplex transaction). We also need to assert Cs line. |
|
516 if(!iFullDTransfer) |
|
517 { |
|
518 // enable the channel.. |
|
519 AsspRegister::Write32(iHwBase + MCSPI_CHxCTRL(iCurrSS), MCSPI_CHxCTRL_EN); |
|
520 |
|
521 // enable TX and RX Empty interrupts |
|
522 AsspRegister::Modify32(iHwBase + MCSPI_IRQSTATUS, 0, MCSPI_IRQ_TX_EMPTY(iCurrSS) | MCSPI_IRQ_RX_FULL(iCurrSS)); |
|
523 AsspRegister::Modify32(iHwBase + MCSPI_IRQENABLE, 0, MCSPI_IRQ_TX_EMPTY(iCurrSS) | MCSPI_IRQ_RX_FULL(iCurrSS)); |
|
524 #ifdef SINGLE_MODE |
|
525 // in SINGLE mode needs to manually assert CS line for current |
|
526 AsspRegister::Modify32(iHwBase + MCSPI_CHxCONF(iCurrSS), 0, MCSPI_CHxCONF_FORCE); |
|
527 |
|
528 // change the pad config - now the SPI drives the line appropriately.. |
|
529 SetCsActive(iChannelNumber, iCurrSS, iCurrHeader.iSSPinActiveMode); |
|
530 #endif /*SINGLE_MODE*/ |
|
531 } |
|
532 else |
|
533 { |
|
534 // enable only interrupts for RX here. Tx is handled in EMasterWrite case above. |
|
535 AsspRegister::Write32(iHwBase + MCSPI_IRQSTATUS, MCSPI_IRQ_RX_FULL(iCurrSS)); |
|
536 AsspRegister::Write32(iHwBase + MCSPI_IRQENABLE, MCSPI_IRQ_RX_FULL(iCurrSS)); |
|
537 } |
|
538 |
|
539 DEBUG_ONLY(DumpCurrentStatus("DoTransfer(Read)")); |
|
540 // and enable system interrupts |
|
541 if(!iFullDTransfer) |
|
542 Interrupt::Enable(iIrqId); |
|
543 break; |
|
544 } |
|
545 default: |
|
546 { |
|
547 DBG_ERR(Kern::Printf("Unsupported TransactionType %x", aType)); |
|
548 r = KErrArgument; |
|
549 break; |
|
550 } |
|
551 } |
|
552 |
|
553 return r; |
|
554 } |
|
555 |
|
556 #ifdef _DEBUG |
|
557 static TInt IsrCnt = 0; |
|
558 void DSpiMasterBeagle::DumpCurrentStatus(const TInt8* aWhere /*=NULL*/) |
|
559 { |
|
560 if(aWhere) |
|
561 Kern::Printf("------ Status (%s)--------", aWhere); |
|
562 else |
|
563 Kern::Printf("------ Status --------"); |
|
564 Kern::Printf("\niTransactionStatus: %d", iTransactionStatus); |
|
565 Kern::Printf("iTransferEndDfc %s queued", iTransferEndDfc.Queued() ? "" : "NOT"); |
|
566 |
|
567 if(iOperation.iOp.iIsTransmitting) |
|
568 { |
|
569 Kern::Printf("TX STATUS:"); |
|
570 Kern::Printf(" iTxData %x", iTxData); |
|
571 Kern::Printf(" iTxDataEnd %x", iTxDataEnd); |
|
572 Kern::Printf(" left to write: %x (words)", (iTxDataEnd - iTxData)/iWordSize); |
|
573 } |
|
574 |
|
575 if(iOperation.iOp.iIsReceiving) |
|
576 { |
|
577 Kern::Printf("RX STATUS:"); |
|
578 Kern::Printf(" iRxData %x", iRxData); |
|
579 Kern::Printf(" iRxDataEnd %x", iRxDataEnd); |
|
580 Kern::Printf(" left to read: %x (words)", (iRxDataEnd - iRxData)/iWordSize); |
|
581 } |
|
582 Kern::Printf(" iCurrSS %d",iCurrSS); |
|
583 |
|
584 Kern::Printf("IsrCnt %d", IsrCnt); |
|
585 TUint status = AsspRegister::Read32(iHwBase + MCSPI_IRQSTATUS); |
|
586 Kern::Printf("MCSPI_IRQSTATUS (0x%x):", status); |
|
587 if(status & MCSPI_IRQ_TX_EMPTY(iCurrSS)) |
|
588 Kern::Printf(" MCSPI_IRQ_TX_EMPTY"); |
|
589 if(status & MCSPI_IRQ_TX_UNDERFLOW(iCurrSS)) |
|
590 Kern::Printf(" MCSPI_IRQ_TX_UNDERFLOW"); |
|
591 if(!iCurrSS && status & MCSPI_IRQ_RX_OVERFLOW) |
|
592 Kern::Printf(" MCSPI_IRQ_RX_OVERFLOW"); |
|
593 if(status & MCSPI_IRQ_RX_FULL(iCurrSS)) |
|
594 Kern::Printf(" MCSPI_IRQ_RX_FULL"); |
|
595 |
|
596 Kern::Printf("MCSPI_CHxSTAT(%d):", iCurrSS); |
|
597 status = AsspRegister::Read32(iHwBase + MCSPI_CHxSTAT(iCurrSS)); |
|
598 if(status & MCSPI_CHxSTAT_RXFFF) |
|
599 Kern::Printf(" MCSPI_CHxSTAT_RXFFF"); |
|
600 if(status & MCSPI_CHxSTAT_RXFFE) |
|
601 Kern::Printf(" MCSPI_CHxSTAT_RXFFE"); |
|
602 if(status & MCSPI_CHxSTAT_TXFFF) |
|
603 Kern::Printf(" MCSPI_CHxSTAT_TXFFF"); |
|
604 if(status & MCSPI_CHxSTAT_TXFFE) |
|
605 Kern::Printf(" MCSPI_CHxSTAT_TXFFE"); |
|
606 if(status & MCSPI_CHxSTAT_EOT) |
|
607 Kern::Printf(" MCSPI_CHxSTAT_EOT"); |
|
608 if(status & MCSPI_CHxSTAT_TXS) |
|
609 Kern::Printf(" MCSPI_CHxSTAT_TXS"); |
|
610 if(status & MCSPI_CHxSTAT_RXS) |
|
611 Kern::Printf(" MCSPI_CHxSTAT_RXS"); |
|
612 |
|
613 Kern::Printf("MCSPI_XFERLEVEL:"); |
|
614 status = AsspRegister::Read32(iHwBase + MCSPI_XFERLEVEL); |
|
615 Kern::Printf(" MCSPI_XFERLEVEL_WCNT %d", status >> MCSPI_XFERLEVEL_WCNT_OFFSET); |
|
616 Kern::Printf(" MCSPI_XFERLEVEL_AFL %d", (status >> MCSPI_XFERLEVEL_AFL_OFFSET) & 0x3F); |
|
617 Kern::Printf(" MCSPI_XFERLEVEL_AEL %d\n", (status >> MCSPI_XFERLEVEL_AEL_OFFSET) & 0x1F); |
|
618 Kern::Printf("---------------------------------------/*\n\n\n"); |
|
619 } |
|
620 #endif |
|
621 |
|
622 void DSpiMasterBeagle::Isr(TAny* aPtr) |
|
623 { |
|
624 DSpiMasterBeagle *a = (DSpiMasterBeagle*) aPtr; |
|
625 DEBUG_ONLY(IsrCnt++); |
|
626 DEBUG_ONLY(a->DumpCurrentStatus("Isr entry")); |
|
627 |
|
628 TUint32 status = AsspRegister::Read32(a->iHwBase + MCSPI_IRQSTATUS); |
|
629 AsspRegister::Write32(a->iHwBase + MCSPI_IRQSTATUS, status); // clear status bits.. |
|
630 |
|
631 // TX_EMPTY - when an item (or number of items if FIFO is used) was transmitted.. |
|
632 if(status & MCSPI_IRQ_TX_EMPTY(a->iCurrSS)) |
|
633 { |
|
634 |
|
635 if(a->iOperation.iOp.iIsTransmitting) |
|
636 { |
|
637 #ifdef USE_TX_FIFO |
|
638 // when FIFO is used - should write (at least) the MCSPI_XFERLEVEL_AFL + 1 words to this register.. |
|
639 while(a->iTxData != a->iTxDataEnd) |
|
640 { |
|
641 AsspRegister::Write32(a->iHwBase + MCSPI_TXx(a->iCurrSS), *a->iTxData); |
|
642 a->iTxData += a->iWordSize; // Then increment the pointer to the data.s |
|
643 |
|
644 if(AsspRegister::Read32(a->iHwBase + MCSPI_CHxSTAT(a->iCurrSS)) & MCSPI_CHxSTAT_TXFFF) |
|
645 { |
|
646 break; |
|
647 } |
|
648 } |
|
649 #else |
|
650 // transfer next word.. |
|
651 if(a->iTxData != a->iTxDataEnd) |
|
652 { |
|
653 TUint val = 0; |
|
654 for (TInt i = 0; i < a->iWordSize; i++) |
|
655 { |
|
656 val |= (*a->iTxData++) << i * 8; |
|
657 } |
|
658 AsspRegister::Write32(a->iHwBase + MCSPI_TXx(a->iCurrSS), val); |
|
659 } |
|
660 |
|
661 // check again - if this was the last one..and we're not waiting for rx - end transfer |
|
662 if(a->iTxData == a->iTxDataEnd && !a->iOperation.iOp.iIsReceiving) |
|
663 { |
|
664 Interrupt::Disable(a->iIrqId); |
|
665 a->iTransferEndDfc.Add(); |
|
666 } |
|
667 #endif |
|
668 } |
|
669 else |
|
670 { |
|
671 // writing a 'dummy' word (for read only transferss (writing 0 doesn't change line state) |
|
672 AsspRegister::Write32(a->iHwBase + MCSPI_TXx(a->iCurrSS), 0); |
|
673 } |
|
674 } |
|
675 |
|
676 if(status & MCSPI_IRQ_RX_FULL(a->iCurrSS)) |
|
677 { |
|
678 if(a->iOperation.iOp.iIsReceiving) |
|
679 { |
|
680 if(a->iRxDataEnd != a->iRxData) |
|
681 { |
|
682 TUint8 nextRxValue = AsspRegister::Read32(a->iHwBase + MCSPI_RXx(a->iCurrSS)); |
|
683 *a->iRxData = nextRxValue; |
|
684 a->iRxData += a->iWordSize; |
|
685 } |
|
686 |
|
687 // If the Rx buffer is now full, finish the transmission. |
|
688 if(a->iRxDataEnd == a->iRxData) |
|
689 { |
|
690 Interrupt::Disable(a->iIrqId); |
|
691 a->iTransferEndDfc.Add(); |
|
692 } |
|
693 } |
|
694 } |
|
695 |
|
696 #if 0 // TODO - probably master, as it creates CLK for slave - will never have to bother with this.. |
|
697 if(status & MCSPI_IRQ_TX_UNDERFLOW(a->iCurrSS)) |
|
698 { |
|
699 DBG_ERR(Kern::Printf("Underflow")); |
|
700 a->iTransactionStatus = KErrUnderflow; |
|
701 |
|
702 // disable the channel.. |
|
703 AsspRegister::Modify32(a->iHwBase + MCSPI_CHxCTRL(0), MCSPI_CHxCTRL_EN, 0); |
|
704 Interrupt::Disable(a->iIrqId); |
|
705 DEBUG_ONLY(a->DumpCurrentStatus("TxUnderflow")); |
|
706 DBG_ERR(Kern::Fault("TxUnderflow", 0)); |
|
707 } |
|
708 #endif |
|
709 #if defined(USE_TX_FIFO) && defined(USING_TX_COUNTER) |
|
710 if(status & MCSPI_IRQSTATUS_EOW) |
|
711 { |
|
712 Kern::Printf("EOW"); |
|
713 // TODO: end of transfer.. |
|
714 } |
|
715 #endif |
|
716 |
|
717 // end of ISR processing |
|
718 DEBUG_ONLY(a->DumpCurrentStatus("Isr end")); |
|
719 } |
|
720 |
|
721 void DSpiMasterBeagle::TransferEndDfc(TAny* aPtr) |
|
722 { |
|
723 DBGPRINT(Kern::Printf("DSpiMasterBeagle::TransferEndDfc")); |
|
724 DSpiMasterBeagle *a = (DSpiMasterBeagle*) aPtr; |
|
725 |
|
726 TUint chanStatus = AsspRegister::Read32(a->iHwBase + MCSPI_CHxSTAT(a->iCurrSS)); |
|
727 if(a->iOperation.iOp.iIsTransmitting) |
|
728 { |
|
729 TUint expected = MCSPI_CHxSTAT_EOT | MCSPI_CHxSTAT_TXS; |
|
730 |
|
731 #ifdef USE_TX_FIFO |
|
732 while(!AsspRegister::Read32(a->iHwBase + MCSPI_CHxSTAT(a->iCurrSS)) & MCSPI_CHxSTAT_TXFFE); |
|
733 #endif |
|
734 while(chanStatus & expected != expected) |
|
735 { |
|
736 chanStatus = AsspRegister::Read32(a->iHwBase + MCSPI_CHxSTAT(a->iCurrSS)); |
|
737 } |
|
738 } |
|
739 |
|
740 if(a->iOperation.iOp.iIsReceiving) |
|
741 { |
|
742 TUint expected = MCSPI_CHxSTAT_RXS; |
|
743 |
|
744 while(chanStatus & expected != expected) |
|
745 { |
|
746 chanStatus = AsspRegister::Read32(a->iHwBase + MCSPI_CHxSTAT(a->iCurrSS)); |
|
747 } |
|
748 __ASSERT_DEBUG(a->iRxDataEnd == a->iRxData, |
|
749 Kern::Fault("SPI master: exiting not having received all?", 12)); |
|
750 } |
|
751 |
|
752 // make sure the CS pin is asserted.. |
|
753 if(a->iCurrHeader.iSSPinActiveMode == ESpiCSPinActiveLow) |
|
754 { |
|
755 AsspRegister::Modify32(a->iHwBase + MCSPI_SYST, 0, 1 << a->iCurrSS); |
|
756 } |
|
757 else |
|
758 { |
|
759 AsspRegister::Modify32(a->iHwBase + MCSPI_SYST, 1 << a->iCurrSS, 0); |
|
760 } |
|
761 |
|
762 #ifdef SINGLE_MODE |
|
763 // manually de-assert CS line for this channel |
|
764 AsspRegister::Modify32(a->iHwBase + MCSPI_CHxCONF(a->iCurrSS), MCSPI_CHxCONF_FORCE, 0); |
|
765 |
|
766 // drive csx pin high or low. Doing this here causes, that CS lines are toggled for each transfers. |
|
767 TUint val = (a->iCurrHeader.iSSPinActiveMode == ESpiCSPinActiveLow)? 1 << a->iCurrSS : 0; |
|
768 if (val) |
|
769 { |
|
770 AsspRegister::Modify32(a->iHwBase + MCSPI_SYST, 0, val); |
|
771 } |
|
772 // put the CS signal to 'inactive' state (as on channel disable it would have a glitch) |
|
773 SetCsInactive(a->iChannelNumber, a->iCurrSS, a->iCurrHeader.iSSPinActiveMode); |
|
774 |
|
775 #endif |
|
776 |
|
777 // disable the channel |
|
778 AsspRegister::Modify32(a->iHwBase + MCSPI_CHxCTRL(0), MCSPI_CHxCTRL_EN, 0); |
|
779 |
|
780 // Start the next transfer for this transaction, if any remain |
|
781 if(a->iState == EBusy) |
|
782 { |
|
783 TInt err = a->ProcessNextTransfers(); |
|
784 if(err != KErrNone) |
|
785 { |
|
786 // If the next transfer could not be started, complete the transaction with |
|
787 // the returned error code |
|
788 a->ExitComplete(err); |
|
789 } |
|
790 } |
|
791 } |
|
792 |
|
793 void DSpiMasterBeagle::ExitComplete(TInt aErr, TBool aComplete /*= ETrue*/) |
|
794 { |
|
795 DBGPRINT(Kern::Printf("DSpiMasterBeagle::ExitComplete, aErr %d, aComplete %d", aErr, aComplete)); |
|
796 |
|
797 // make sure CS is in inactive state (for the current / last transaction) on error |
|
798 if(!aComplete) |
|
799 { |
|
800 SetCsInactive(iChannelNumber, iCurrSS, iCurrHeader.iSSPinActiveMode); |
|
801 } |
|
802 |
|
803 // disable this channel (and reset..) |
|
804 AsspRegister::Modify32(iHwBase + MCSPI_CHxCTRL(iCurrSS), MCSPI_CHxCTRL_EN, 0); |
|
805 AsspRegister::Write32(iHwBase + MCSPI_SYSCONFIG, MCSPI_SYSCONFIG_SOFTRESET); |
|
806 |
|
807 // Disable interrupts for the channel |
|
808 Interrupt::Disable(iIrqId); |
|
809 |
|
810 // Cancel any timers and DFCs.. |
|
811 CancelTimeOut(); |
|
812 iTransferEndDfc.Cancel(); |
|
813 |
|
814 // Change the channel state back to EIdle |
|
815 iState = EIdle; |
|
816 |
|
817 // Call the PIL method to complete the request |
|
818 if(aComplete) |
|
819 { |
|
820 CompleteRequest(aErr); |
|
821 } |
|
822 } |
|
823 |
|
824 #ifdef _DEBUG |
|
825 void DumpHeader(TConfigSpiV01& aHeader) |
|
826 { |
|
827 Kern::Printf("header:"); |
|
828 Kern::Printf("iWordWidth %d (%d bits)", aHeader.iWordWidth, (SpiWordWidth(aHeader.iWordWidth)) >> MCSPI_CHxCONF_WL_OFFSET + 1); |
|
829 Kern::Printf("iClkSpeedHz %d", aHeader.iClkSpeedHz); |
|
830 Kern::Printf("iClkMode %d", aHeader.iClkMode); |
|
831 Kern::Printf("iTimeoutPeriod %d", aHeader.iTimeoutPeriod); |
|
832 Kern::Printf("iBitOrder %d", aHeader.iBitOrder); |
|
833 Kern::Printf("iTransactionWaitCycles %d", aHeader.iTransactionWaitCycles); |
|
834 Kern::Printf("iSSPinActiveMode %d", aHeader.iSSPinActiveMode); |
|
835 } |
|
836 #endif |
|
837 |
|
838 // virtual method called by the PIL when a transaction is queued (with QueueTransaction). |
|
839 // This is done in the context of the Client's thread. |
|
840 // The PSL is required to check that the transaction header is valid for this channel. |
|
841 TInt DSpiMasterBeagle::CheckHdr(TDes8* aHdrBuff) |
|
842 { |
|
843 TInt r = KErrNone; |
|
844 if(!aHdrBuff) |
|
845 { |
|
846 r = KErrArgument; |
|
847 } |
|
848 else |
|
849 { |
|
850 TConfigSpiV01 &header = (*(TConfigSpiBufV01*) (aHdrBuff))(); |
|
851 |
|
852 // check if word width and clock are supported |
|
853 if(SpiWordWidth(header.iWordWidth) < KMinSpiWordWidth || |
|
854 SpiClkValue(header.iClkSpeedHz) < 0 || // == KErrNotSupported |
|
855 header.iBitOrder == ELsbFirst) // this SPI only transmits MSB fist |
|
856 { |
|
857 #ifdef _DEBUG |
|
858 if(header.iBitOrder == ELsbFirst) |
|
859 DBG_ERR(Kern::Printf("iClkSpeedHz value (%d) is not supported", header.iClkSpeedHz)); |
|
860 if(SpiClkValue(header.iClkSpeedHz) < 0) |
|
861 DBG_ERR(Kern::Printf("iClkSpeedHz: %d is not supported", header.iClkSpeedHz)); |
|
862 if((SpiWordWidth(header.iWordWidth)+ 1) >> MCSPI_CHxCONF_WL_OFFSET < KMinSpiWordWidth) |
|
863 DBG_ERR(Kern::Printf("iWordWidth: %d is not supported, min value is: %d", |
|
864 SpiWordWidth(header.iWordWidth), KMinSpiWordWidth)); |
|
865 DumpHeader(header); |
|
866 #endif |
|
867 r = KErrNotSupported; |
|
868 DBG_ERR(Kern::Printf("DSpiMasterBeagle::CheckHdr()failed, r = %d", r)); |
|
869 } |
|
870 } |
|
871 return r; |
|
872 } |
|
873 |
|
874 // This method is called by the PIL in the case of expiry of a timer for a transaction. |
|
875 // TODO: this name is confusing - it could be changed in the PIL to reflect it's real purpose(TBD) |
|
876 // It has NOTHING to do with a Slave (i.e. slave might be completely silent for SPI-and master won't notice it!) |
|
877 TInt DSpiMasterBeagle::HandleSlaveTimeout() |
|
878 { |
|
879 DBG_ERR(Kern::Printf("HandleSlaveTimeout")); |
|
880 |
|
881 // Stop the PSL's operation, and inform the PIL of the timeout |
|
882 ExitComplete(KErrTimedOut, EFalse); |
|
883 |
|
884 return KErrTimedOut; |
|
885 } |
|
886 |