| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2004-2011, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: bastmlib_clock_coupling_task.c $ |
|---|
| 11 | * $brcm_Revision: Hydra_Software_Devel/7 $ |
|---|
| 12 | * $brcm_Date: 5/17/11 3:13p $ |
|---|
| 13 | * |
|---|
| 14 | * Revision History: |
|---|
| 15 | * |
|---|
| 16 | * $brcm_Log: /magnum/syslib/astmlib/noarch/bastmlib_clock_coupling_task.c $ |
|---|
| 17 | * |
|---|
| 18 | * Hydra_Software_Devel/7 5/17/11 3:13p bandrews |
|---|
| 19 | * SW3548-1159: Only check for deviantes if we are locked, and ideals if |
|---|
| 20 | * we are not |
|---|
| 21 | * |
|---|
| 22 | * Hydra_Software_Devel/6 3/1/11 10:11p bandrews |
|---|
| 23 | * SW7550-691: only handle timer expiry if enabled and started |
|---|
| 24 | * |
|---|
| 25 | * Hydra_Software_Devel/5 11/2/10 4:54p bandrews |
|---|
| 26 | * SW3548-1159: updated clock coupling recovery to use a second timeout |
|---|
| 27 | * value |
|---|
| 28 | * |
|---|
| 29 | * Hydra_Software_Devel/4 7/14/10 7:44p bandrews |
|---|
| 30 | * SW3548-1161: expose default configs as public |
|---|
| 31 | * |
|---|
| 32 | * Hydra_Software_Devel/3 6/22/10 3:13p bandrews |
|---|
| 33 | * SW3548-2687: missed one copypasta error |
|---|
| 34 | * |
|---|
| 35 | * Hydra_Software_Devel/2 12/23/09 10:31p bandrews |
|---|
| 36 | * SW3548-2687: fix copy-paste error |
|---|
| 37 | * |
|---|
| 38 | * Hydra_Software_Devel/1 7/17/09 6:56p bandrews |
|---|
| 39 | * PR49215: playback support |
|---|
| 40 | ***************************************************************************/ |
|---|
| 41 | |
|---|
| 42 | #include "bstd.h" |
|---|
| 43 | #include "bkni.h" |
|---|
| 44 | #include "bsyslib.h" |
|---|
| 45 | #include "bastmlib.h" |
|---|
| 46 | #include "bastmlib_priv.h" |
|---|
| 47 | #include "bastmlib_clock_coupling_task.h" |
|---|
| 48 | #include "bastmlib_clock_reference_priv.h" |
|---|
| 49 | #include "bastmlib_presentation_task.h" |
|---|
| 50 | |
|---|
| 51 | BDBG_MODULE(astmlib); |
|---|
| 52 | |
|---|
| 53 | #define BASTMLIB_DEBUG_QUEUE 0 |
|---|
| 54 | #define BASTMLIB_DEBUG_SIGNALS 0 |
|---|
| 55 | |
|---|
| 56 | void BASTMlib_P_ClockCoupling_GetDefaultConfig( |
|---|
| 57 | BASTMlib_Config * psConfig |
|---|
| 58 | ) |
|---|
| 59 | { |
|---|
| 60 | BASTMlib_ClockReference_Config sReferenceConfig; |
|---|
| 61 | |
|---|
| 62 | BDBG_ENTER(BASTMlib_P_ClockCoupling_GetDefaultConfig); |
|---|
| 63 | |
|---|
| 64 | BDBG_ASSERT(psConfig); |
|---|
| 65 | |
|---|
| 66 | BASTMlib_ClockReference_GetDefaultConfig(&sReferenceConfig); |
|---|
| 67 | |
|---|
| 68 | psConfig->sClockCoupling.uiMinimumTimeBetweenEvents = sReferenceConfig.uiMinimumTimeBetweenEvents; |
|---|
| 69 | psConfig->sClockCoupling.eClockReferenceDomain = sReferenceConfig.eClockReferenceDomain; |
|---|
| 70 | psConfig->sClockCoupling.uiDeviationThreshold = sReferenceConfig.uiDeviationThreshold; |
|---|
| 71 | psConfig->sClockCoupling.uiDeviantCountThreshold = sReferenceConfig.uiDeviantCountThreshold; |
|---|
| 72 | psConfig->sClockCoupling.uiIdealCountThreshold = sReferenceConfig.uiIdealCountThreshold; |
|---|
| 73 | psConfig->sClockCoupling.uiInitialAcquisitionTime = BASTMLIB_CLOCK_COUPLING_DEFAULT_INITIAL_ACQUISITION_TIME; |
|---|
| 74 | psConfig->sClockCoupling.uiProcessingFrequency = BASTMLIB_CLOCK_COUPLING_DEFAULT_PROCESSING_FREQUENCY; |
|---|
| 75 | psConfig->sClockCoupling.uiIdealProcessingFrequency = BASTMLIB_CLOCK_COUPLING_DEFAULT_IDEAL_PROCESSING_FREQUENCY; |
|---|
| 76 | psConfig->sClockCoupling.uiSettlingTime = BASTMLIB_CLOCK_COUPLING_DEFAULT_SETTLING_TIME; |
|---|
| 77 | psConfig->sClockCoupling.ePreferredClockCoupling = BASTMlib_ClockCoupling_eInputClock; |
|---|
| 78 | |
|---|
| 79 | BDBG_LEAVE(BASTMlib_P_ClockCoupling_GetDefaultConfig); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | void BASTMlib_P_ClockCoupling_GetMaxAcquisitionTime( |
|---|
| 83 | BASTMlib_Handle hAstm, |
|---|
| 84 | unsigned int * puiMaxAcquisitionTime |
|---|
| 85 | ) |
|---|
| 86 | { |
|---|
| 87 | BDBG_ENTER(BASTMlib_P_ClockCoupling_GetMaxAcquisitionTime); |
|---|
| 88 | |
|---|
| 89 | BDBG_ASSERT(hAstm); |
|---|
| 90 | BDBG_ASSERT(puiMaxAcquisitionTime); |
|---|
| 91 | |
|---|
| 92 | *puiMaxAcquisitionTime = hAstm->sConfig.sClockCoupling.uiInitialAcquisitionTime |
|---|
| 93 | > hAstm->sConfig.sClockCoupling.uiProcessingFrequency |
|---|
| 94 | ? hAstm->sConfig.sClockCoupling.uiInitialAcquisitionTime |
|---|
| 95 | : hAstm->sConfig.sClockCoupling.uiProcessingFrequency; |
|---|
| 96 | |
|---|
| 97 | *puiMaxAcquisitionTime = *puiMaxAcquisitionTime |
|---|
| 98 | > hAstm->sConfig.sClockCoupling.uiIdealProcessingFrequency |
|---|
| 99 | ? *puiMaxAcquisitionTime |
|---|
| 100 | : hAstm->sConfig.sClockCoupling.uiIdealProcessingFrequency; |
|---|
| 101 | |
|---|
| 102 | BDBG_LEAVE(BASTMlib_P_ClockCoupling_GetMaxAcquisitionTime); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | BERR_Code BASTMlib_P_ClockCoupling_TimerExpired( |
|---|
| 106 | void * pvParm1, /* first user context parameter [in] */ |
|---|
| 107 | int iParm2, /* second user context parameter [in] */ |
|---|
| 108 | BSYSlib_Timer_Handle hTimer /* the handle of the timer that expired [in] */ |
|---|
| 109 | ) |
|---|
| 110 | { |
|---|
| 111 | BERR_Code rc = BERR_SUCCESS; |
|---|
| 112 | BASTMlib_Handle hAstm = pvParm1; |
|---|
| 113 | |
|---|
| 114 | BDBG_ENTER(BASTMlib_P_ClockCoupling_TimerExpired); |
|---|
| 115 | |
|---|
| 116 | BDBG_ASSERT(hAstm); |
|---|
| 117 | |
|---|
| 118 | BSTD_UNUSED(iParm2); |
|---|
| 119 | BSTD_UNUSED(hTimer); |
|---|
| 120 | |
|---|
| 121 | if (hAstm->sConfig.bEnabled && hAstm->bStarted) |
|---|
| 122 | { |
|---|
| 123 | if (hAstm->sClockCoupling.bAcquire) |
|---|
| 124 | { |
|---|
| 125 | BDBG_MSG(("Clock coupling processing timer expired")); |
|---|
| 126 | /* if we are acquiring, then this is a processing timer expiry event */ |
|---|
| 127 | rc = BASTMlib_P_ClockCoupling_Process(hAstm); |
|---|
| 128 | if (rc) goto error; |
|---|
| 129 | } |
|---|
| 130 | else |
|---|
| 131 | { |
|---|
| 132 | BDBG_MSG(("Clock coupling settling timer expired")); |
|---|
| 133 | /* if we are NOT acquiring, then this is a settling timer expiry event |
|---|
| 134 | and we need to re-enable acquisition of data */ |
|---|
| 135 | BKNI_EnterCriticalSection(); |
|---|
| 136 | hAstm->sClockCoupling.bAcquire = true; |
|---|
| 137 | BKNI_LeaveCriticalSection(); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /* reschedule for later */ |
|---|
| 141 | BDBG_MSG(("Scheduling clock coupling processing timer")); |
|---|
| 142 | BKNI_EnterCriticalSection(); |
|---|
| 143 | rc = BASTMlib_P_StartTimer_isr(hAstm, hAstm->sClockCoupling.hTimer, |
|---|
| 144 | hAstm->sStatus.eClockCoupling == BASTMlib_ClockCoupling_eInputClock |
|---|
| 145 | ? hAstm->sConfig.sClockCoupling.uiProcessingFrequency |
|---|
| 146 | : hAstm->sConfig.sClockCoupling.uiIdealProcessingFrequency, |
|---|
| 147 | &BASTMlib_P_ClockCoupling_TimerExpired, hAstm, 0); |
|---|
| 148 | BKNI_LeaveCriticalSection(); |
|---|
| 149 | if (rc) goto error; |
|---|
| 150 | } |
|---|
| 151 | else |
|---|
| 152 | { |
|---|
| 153 | BDBG_MSG(("Clock coupling timer expired, but ASTM was not started or enabled")); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | goto end; |
|---|
| 157 | |
|---|
| 158 | error: |
|---|
| 159 | |
|---|
| 160 | end: |
|---|
| 161 | |
|---|
| 162 | BDBG_LEAVE(BASTMlib_P_ClockCoupling_TimerExpired); |
|---|
| 163 | return rc; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | BERR_Code BASTMlib_P_ClockCoupling_Process( |
|---|
| 167 | BASTMlib_Handle hAstm |
|---|
| 168 | ) |
|---|
| 169 | { |
|---|
| 170 | BERR_Code rc = BERR_SUCCESS; |
|---|
| 171 | long lActual; |
|---|
| 172 | long lExpected; |
|---|
| 173 | long lDeviation; |
|---|
| 174 | unsigned int uiDeviationThreshold; |
|---|
| 175 | unsigned int uiDeviantCount; |
|---|
| 176 | unsigned int uiDeviantCountThreshold; |
|---|
| 177 | unsigned int uiIdealCount; |
|---|
| 178 | unsigned int uiIdealCountThreshold; |
|---|
| 179 | unsigned int uiSize; |
|---|
| 180 | BASTMlib_ClockReference_Handle hReference; |
|---|
| 181 | BASTMlib_ClockReference_Event sLastEvent; |
|---|
| 182 | BASTMlib_ClockReference_Event * psLastEvent; |
|---|
| 183 | |
|---|
| 184 | BDBG_ENTER(BASTMlib_P_ClockCoupling_Process); |
|---|
| 185 | |
|---|
| 186 | BDBG_ASSERT(hAstm); |
|---|
| 187 | |
|---|
| 188 | hReference = hAstm->sClockCoupling.hReference; |
|---|
| 189 | psLastEvent = NULL; |
|---|
| 190 | uiDeviationThreshold = hReference->sConfig.uiDeviationThreshold; |
|---|
| 191 | uiDeviantCountThreshold = hReference->sConfig.uiDeviantCountThreshold; |
|---|
| 192 | uiIdealCountThreshold = hReference->sConfig.uiIdealCountThreshold; |
|---|
| 193 | uiDeviantCount = 0; |
|---|
| 194 | uiIdealCount = 0; |
|---|
| 195 | |
|---|
| 196 | BKNI_EnterCriticalSection(); |
|---|
| 197 | uiSize = hReference->sEventQueue.uiSize; |
|---|
| 198 | BKNI_LeaveCriticalSection(); |
|---|
| 199 | |
|---|
| 200 | while (uiSize) |
|---|
| 201 | { |
|---|
| 202 | BASTMlib_ClockReference_Event sEvent; |
|---|
| 203 | |
|---|
| 204 | #if BASTMLIB_DEBUG_QUEUE |
|---|
| 205 | BDBG_MSG(("Removing event from clock coupling queue at %u", hReference->sEventQueue.uiRead + 1)); |
|---|
| 206 | #endif |
|---|
| 207 | BKNI_EnterCriticalSection(); |
|---|
| 208 | hReference->sEventQueue.uiRead++; |
|---|
| 209 | hReference->sEventQueue.uiRead %= hReference->sEventQueue.uiCapacity; |
|---|
| 210 | sEvent = hReference->sEventQueue.asEvents[hReference->sEventQueue.uiRead]; |
|---|
| 211 | hReference->sEventQueue.uiSize--; |
|---|
| 212 | BKNI_LeaveCriticalSection(); |
|---|
| 213 | |
|---|
| 214 | if (psLastEvent) |
|---|
| 215 | { |
|---|
| 216 | lActual = (long)sEvent.uiClockReference - (long)psLastEvent->uiClockReference; |
|---|
| 217 | lExpected = (long)sEvent.uiStc - (long)psLastEvent->uiStc; |
|---|
| 218 | lDeviation = lActual - lExpected; |
|---|
| 219 | |
|---|
| 220 | /* if deviant */ |
|---|
| 221 | if (lDeviation > (long)uiDeviationThreshold || lDeviation < -(long)uiDeviationThreshold) |
|---|
| 222 | { |
|---|
| 223 | uiDeviantCount++; |
|---|
| 224 | BDBG_MSG(("Clock coupling deviation failure %+ld", lDeviation)); |
|---|
| 225 | } |
|---|
| 226 | else |
|---|
| 227 | { |
|---|
| 228 | uiIdealCount++; |
|---|
| 229 | BDBG_MSG(("Clock coupling deviation pass %+ld", lDeviation)); |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | else |
|---|
| 233 | { |
|---|
| 234 | psLastEvent = &sLastEvent; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | sLastEvent = sEvent; |
|---|
| 238 | |
|---|
| 239 | BKNI_EnterCriticalSection(); |
|---|
| 240 | uiSize = hReference->sEventQueue.uiSize; |
|---|
| 241 | BKNI_LeaveCriticalSection(); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | /* no pcrs received during measurement period */ |
|---|
| 245 | if (uiDeviantCount + uiIdealCount == 0) |
|---|
| 246 | { |
|---|
| 247 | BDBG_MSG(("No clock references received during measurement period")); |
|---|
| 248 | BASTMlib_P_ClockCoupling_StateMachine_SendSignal(hAstm, |
|---|
| 249 | BASTMlib_ClockCoupling_StateMachineSignal_eClockReferenceFailure); |
|---|
| 250 | BASTMlib_P_Presentation_StcSourceStateMachine_SendSignal(hAstm, |
|---|
| 251 | BASTMlib_Presentation_StateMachineSignal_eClockReferenceFailure); |
|---|
| 252 | } |
|---|
| 253 | else |
|---|
| 254 | { |
|---|
| 255 | /* if we are attached to input, check for deviants */ |
|---|
| 256 | if (hAstm->sStatus.eClockCoupling == BASTMlib_ClockCoupling_eInputClock) |
|---|
| 257 | { |
|---|
| 258 | /* ignore up to threshold deviants */ |
|---|
| 259 | if (uiDeviantCount > uiDeviantCountThreshold) |
|---|
| 260 | { |
|---|
| 261 | BASTMlib_P_ClockCoupling_StateMachine_SendSignal(hAstm, |
|---|
| 262 | BASTMlib_ClockCoupling_StateMachineSignal_eClockReferenceFailure); |
|---|
| 263 | BASTMlib_P_Presentation_StcSourceStateMachine_SendSignal(hAstm, |
|---|
| 264 | BASTMlib_Presentation_StateMachineSignal_eClockReferenceFailure); |
|---|
| 265 | } |
|---|
| 266 | /* else, no change in state */ |
|---|
| 267 | } |
|---|
| 268 | else /* we are attached to xtal, check for ideals */ |
|---|
| 269 | { |
|---|
| 270 | /* the following code generates an affinity for staying in the clock |
|---|
| 271 | decoupled state, as any deviants detected while in the decoupled |
|---|
| 272 | state will prevent us from returning to a coupled state */ |
|---|
| 273 | /* ideals only get processed if there weren't any deviants */ |
|---|
| 274 | if (!uiDeviantCount && uiIdealCount > uiIdealCountThreshold) /* ignore up to threshold ideals */ |
|---|
| 275 | { |
|---|
| 276 | BASTMlib_P_ClockCoupling_StateMachine_SendSignal(hAstm, |
|---|
| 277 | BASTMlib_ClockCoupling_StateMachineSignal_eClockReferencePass); |
|---|
| 278 | BASTMlib_P_Presentation_StcSourceStateMachine_SendSignal(hAstm, |
|---|
| 279 | BASTMlib_Presentation_StateMachineSignal_eClockReferencePass); |
|---|
| 280 | } |
|---|
| 281 | /* else, no change in state */ |
|---|
| 282 | } |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | BDBG_LEAVE(BASTMlib_P_ClockCoupling_Process); |
|---|
| 286 | return rc; |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | void BASTMlib_P_ClockCoupling_StateMachine_SendSignal( |
|---|
| 290 | BASTMlib_Handle hAstm, |
|---|
| 291 | BASTMlib_ClockCoupling_StateMachineSignal eSignal |
|---|
| 292 | ) |
|---|
| 293 | { |
|---|
| 294 | BASTMlib_ClockCoupling eNewState; |
|---|
| 295 | |
|---|
| 296 | BDBG_ENTER(BASTMlib_P_ClockCoupling_StateMachine_SendSignal); |
|---|
| 297 | |
|---|
| 298 | BDBG_ASSERT(hAstm); |
|---|
| 299 | |
|---|
| 300 | #if BASTMLIB_DEBUG_SIGNALS |
|---|
| 301 | BDBG_MSG(("Clock coupling signal received: %u", eSignal)); |
|---|
| 302 | #endif |
|---|
| 303 | |
|---|
| 304 | /* |
|---|
| 305 | Clock coupling |
|---|
| 306 | |
|---|
| 307 | signal (s) |
|---|
| 308 | 0 reset |
|---|
| 309 | 1 clock reference pass |
|---|
| 310 | 2 clock reference fail |
|---|
| 311 | |
|---|
| 312 | preference (p), output (y) |
|---|
| 313 | 0 input clock |
|---|
| 314 | 1 internal clock |
|---|
| 315 | |
|---|
| 316 | p s y |
|---|
| 317 | 0 0 0 |
|---|
| 318 | 0 1 0 |
|---|
| 319 | 0 2 1 |
|---|
| 320 | 1 x 1 |
|---|
| 321 | */ |
|---|
| 322 | switch (eSignal) |
|---|
| 323 | { |
|---|
| 324 | case BASTMlib_ClockCoupling_StateMachineSignal_eReset: |
|---|
| 325 | case BASTMlib_ClockCoupling_StateMachineSignal_eClockReferencePass: |
|---|
| 326 | eNewState = hAstm->sConfig.sClockCoupling.ePreferredClockCoupling; /* don't go above preference */ |
|---|
| 327 | break; |
|---|
| 328 | case BASTMlib_ClockCoupling_StateMachineSignal_eClockReferenceFailure: |
|---|
| 329 | eNewState = BASTMlib_ClockCoupling_eInternalClock; |
|---|
| 330 | break; |
|---|
| 331 | default: |
|---|
| 332 | eNewState = hAstm->sStatus.eClockCoupling; /* copy old state */ |
|---|
| 333 | break; |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | if (hAstm->sStatus.eClockCoupling != eNewState) |
|---|
| 337 | { |
|---|
| 338 | hAstm->sStatus.eClockCoupling = eNewState; |
|---|
| 339 | BASTMlib_P_ClockCoupling_StateChangeHandler(hAstm); |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | BDBG_LEAVE(BASTMlib_P_ClockCoupling_StateMachine_SendSignal); |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | BERR_Code BASTMlib_P_ClockCoupling_StateChangeHandler( |
|---|
| 346 | BASTMlib_Handle hAstm |
|---|
| 347 | ) |
|---|
| 348 | { |
|---|
| 349 | BERR_Code rc = BERR_SUCCESS; |
|---|
| 350 | |
|---|
| 351 | BDBG_ENTER(BASTMlib_P_ClockCoupling_StateChangeHandler); |
|---|
| 352 | |
|---|
| 353 | BDBG_ASSERT(hAstm); |
|---|
| 354 | |
|---|
| 355 | /* state changed, so stop acquiring for a bit */ |
|---|
| 356 | BKNI_EnterCriticalSection(); |
|---|
| 357 | hAstm->sClockCoupling.bAcquire = false; |
|---|
| 358 | BKNI_LeaveCriticalSection(); |
|---|
| 359 | |
|---|
| 360 | /* tell external sw that the state changed */ |
|---|
| 361 | rc = BSYSlib_Callback_Invoke(&hAstm->sSettings.sClockCoupling.cbStateChange); |
|---|
| 362 | if (rc) goto error; |
|---|
| 363 | |
|---|
| 364 | /* cancel processing timer and schedule settling timer -- if started */ |
|---|
| 365 | if (hAstm->bStarted) |
|---|
| 366 | { |
|---|
| 367 | BDBG_MSG(("Scheduling clock coupling settling timer")); |
|---|
| 368 | BKNI_EnterCriticalSection(); |
|---|
| 369 | rc = BASTMlib_P_StartTimer_isr(hAstm, hAstm->sClockCoupling.hTimer, |
|---|
| 370 | hAstm->sConfig.sClockCoupling.uiSettlingTime, |
|---|
| 371 | &BASTMlib_P_ClockCoupling_TimerExpired, hAstm, 0); |
|---|
| 372 | BKNI_LeaveCriticalSection(); |
|---|
| 373 | if (rc) goto error; |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | goto end; |
|---|
| 377 | |
|---|
| 378 | error: |
|---|
| 379 | |
|---|
| 380 | end: |
|---|
| 381 | BDBG_LEAVE(BASTMlib_P_ClockCoupling_StateChangeHandler); |
|---|
| 382 | return rc; |
|---|
| 383 | } |
|---|
| 384 | |
|---|