| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2003-2010, 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: bpwr.c $ |
|---|
| 11 | * $brcm_Revision: Hydra_Software_Devel/1 $ |
|---|
| 12 | * $brcm_Date: 11/23/10 2:10p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /magnum/portinginterface/pwr/7552/bpwr.c $ |
|---|
| 19 | * |
|---|
| 20 | * Hydra_Software_Devel/1 11/23/10 2:10p xhuang |
|---|
| 21 | * SW7552-9: Add support for 97552 |
|---|
| 22 | * |
|---|
| 23 | * |
|---|
| 24 | ***************************************************************************/ |
|---|
| 25 | #include "bstd.h" /* standard types */ |
|---|
| 26 | #include "berr.h" |
|---|
| 27 | #include "bstd_ids.h" |
|---|
| 28 | #include "bdbg.h" /* Dbglib */ |
|---|
| 29 | #include "bkni.h" /* malloc */ |
|---|
| 30 | |
|---|
| 31 | #include "bpwr.h" |
|---|
| 32 | #include "bpwr_mem.h" |
|---|
| 33 | #include "bpwr_priv.h" |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | BDBG_MODULE(BPWR); |
|---|
| 37 | |
|---|
| 38 | BDBG_OBJECT_ID(BPWR_P_Context); |
|---|
| 39 | |
|---|
| 40 | BERR_Code BPWR_GetDefaultSettings(BPWR_Settings *pDefSettings ) |
|---|
| 41 | { |
|---|
| 42 | BKNI_Memset(pDefSettings, 0, sizeof(*pDefSettings)); |
|---|
| 43 | return BERR_SUCCESS; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | /*************************************************************************** |
|---|
| 47 | * BPWR_Open() |
|---|
| 48 | * |
|---|
| 49 | * Create Power Management Object |
|---|
| 50 | * |
|---|
| 51 | */ |
|---|
| 52 | BERR_Code BPWR_Open |
|---|
| 53 | ( BPWR_Handle *phPwr, |
|---|
| 54 | BCHP_Handle hChip, |
|---|
| 55 | BREG_Handle hRegister, |
|---|
| 56 | const BPWR_Settings *pDefSettings ) |
|---|
| 57 | { |
|---|
| 58 | BPWR_P_Context *pPwr; |
|---|
| 59 | BERR_Code eStatus = BERR_SUCCESS; |
|---|
| 60 | |
|---|
| 61 | BDBG_ENTER(BPWR_Open); |
|---|
| 62 | BDBG_ASSERT(hRegister); |
|---|
| 63 | |
|---|
| 64 | #if 0 |
|---|
| 65 | BDBG_SetLevel(BDBG_eMsg); |
|---|
| 66 | BDBG_SetModuleLevel("BPWR", BDBG_eMsg); |
|---|
| 67 | #endif |
|---|
| 68 | |
|---|
| 69 | /* The handle will be NULL if create fails. */ |
|---|
| 70 | *phPwr = NULL; |
|---|
| 71 | |
|---|
| 72 | /* check PWR settings */ |
|---|
| 73 | |
|---|
| 74 | /* Alloc the main PWR context. */ |
|---|
| 75 | pPwr = (BPWR_P_Context*)(BKNI_Malloc(sizeof(BPWR_P_Context))); |
|---|
| 76 | if(!pPwr) |
|---|
| 77 | { |
|---|
| 78 | BERR_TRACE(eStatus = BERR_OUT_OF_SYSTEM_MEMORY); |
|---|
| 79 | goto done; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /* Clear out the context and set defaults. */ |
|---|
| 83 | BKNI_Memset((void*)pPwr, 0x0, sizeof(BPWR_P_Context)); |
|---|
| 84 | BDBG_OBJECT_SET(pPwr, BPWR_P_Context); |
|---|
| 85 | |
|---|
| 86 | /* Store the hRegister and hChip for later use. */ |
|---|
| 87 | pPwr->hChip = hChip; |
|---|
| 88 | pPwr->hRegister = hRegister; |
|---|
| 89 | pPwr->changeMode = BPWR_Mode_ePowerUndefined; |
|---|
| 90 | |
|---|
| 91 | /* Take in default settings. */ |
|---|
| 92 | if (pDefSettings) { |
|---|
| 93 | pPwr->stSettings = *pDefSettings; |
|---|
| 94 | } |
|---|
| 95 | else { |
|---|
| 96 | BPWR_GetDefaultSettings(&pPwr->stSettings); |
|---|
| 97 | pDefSettings = &pPwr->stSettings; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /* Allocate and initialize module*/ |
|---|
| 101 | eStatus = BPWR_P_Init(pPwr); |
|---|
| 102 | if (eStatus != BERR_SUCCESS) |
|---|
| 103 | { |
|---|
| 104 | BKNI_Free((void *)pPwr); |
|---|
| 105 | goto done; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /* All done. now return the new fresh context to user. */ |
|---|
| 109 | *phPwr = (BPWR_Handle)pPwr; |
|---|
| 110 | |
|---|
| 111 | done: |
|---|
| 112 | BDBG_LEAVE(BPWR_Open); |
|---|
| 113 | return eStatus; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | /*************************************************************************** |
|---|
| 118 | * |
|---|
| 119 | */ |
|---|
| 120 | BERR_Code BPWR_Close |
|---|
| 121 | ( BPWR_Handle hPwr ) |
|---|
| 122 | { |
|---|
| 123 | BDBG_ENTER(BPWR_Close); |
|---|
| 124 | BDBG_OBJECT_ASSERT(hPwr, BPWR_P_Context); |
|---|
| 125 | |
|---|
| 126 | BPWR_P_Destroy(hPwr); |
|---|
| 127 | BKNI_Free(hPwr); |
|---|
| 128 | |
|---|
| 129 | BDBG_LEAVE(BPWR_Close); |
|---|
| 130 | return BERR_SUCCESS; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | /*************************************************************************** |
|---|
| 135 | * |
|---|
| 136 | */ |
|---|
| 137 | BERR_Code BPWR_SetMode |
|---|
| 138 | ( BPWR_Handle hPwr, |
|---|
| 139 | BSTD_Module module, |
|---|
| 140 | BPWR_ePowerMode eMode ) |
|---|
| 141 | { |
|---|
| 142 | unsigned i; |
|---|
| 143 | |
|---|
| 144 | BDBG_OBJECT_ASSERT(hPwr, BPWR_P_Context); |
|---|
| 145 | BDBG_ASSERT(eMode != BPWR_Mode_ePowerUndefined); |
|---|
| 146 | |
|---|
| 147 | if (eMode == BPWR_Mode_ePowerUndefined) { |
|---|
| 148 | /* can't set to undefined */ |
|---|
| 149 | return BERR_TRACE(BERR_INVALID_PARAMETER); |
|---|
| 150 | } |
|---|
| 151 | else if (hPwr->changeMode == BPWR_Mode_ePowerUndefined) { |
|---|
| 152 | /* this is the first Set */ |
|---|
| 153 | hPwr->changeMode = eMode; |
|---|
| 154 | } |
|---|
| 155 | else if (hPwr->changeMode != eMode) { |
|---|
| 156 | /* this is an invalid mixed set */ |
|---|
| 157 | BDBG_ERR(("Cannot both power up and down in one apply changes set")); |
|---|
| 158 | return BERR_TRACE(BERR_INVALID_PARAMETER); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | for (i=0;i<BPWR_MAX_MODULES;i++) { |
|---|
| 162 | if (hPwr->modules[i].moduleId == module) { |
|---|
| 163 | hPwr->modules[i].change = true; |
|---|
| 164 | return 0; |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | BDBG_WRN(("Module %d is not supported on this chip", module)); |
|---|
| 169 | return -1; /* no BERR_TRACE */ |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | /*************************************************************************** |
|---|
| 174 | * |
|---|
| 175 | */ |
|---|
| 176 | BERR_Code BPWR_GetMode |
|---|
| 177 | ( const BPWR_Handle hPwr, |
|---|
| 178 | BSTD_Module module, |
|---|
| 179 | BPWR_ePowerMode *peMode ) |
|---|
| 180 | { |
|---|
| 181 | unsigned i; |
|---|
| 182 | |
|---|
| 183 | BDBG_OBJECT_ASSERT(hPwr, BPWR_P_Context); |
|---|
| 184 | for(i=0; i<BPWR_MAX_MODULES; i++) { |
|---|
| 185 | if (hPwr->modules[i].moduleId == module) { |
|---|
| 186 | *peMode = hPwr->modules[i].mode; |
|---|
| 187 | return 0; |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | return BERR_TRACE(BERR_INVALID_PARAMETER); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | /*************************************************************************** |
|---|
| 195 | * |
|---|
| 196 | */ |
|---|
| 197 | BERR_Code BPWR_GetModuleCount |
|---|
| 198 | ( BPWR_Handle hPwr, |
|---|
| 199 | uint32_t *pulModuleCount ) |
|---|
| 200 | { |
|---|
| 201 | unsigned i; |
|---|
| 202 | |
|---|
| 203 | BDBG_ENTER(BPWR_GetModuleCount); |
|---|
| 204 | BDBG_OBJECT_ASSERT(hPwr, BPWR_P_Context); |
|---|
| 205 | |
|---|
| 206 | *pulModuleCount = 0; |
|---|
| 207 | for(i=0; i<BPWR_MAX_MODULES; i++) { |
|---|
| 208 | if (hPwr->modules[i].moduleId) { |
|---|
| 209 | (*pulModuleCount)++; |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | BDBG_WRN(("Number of modules: %d", *pulModuleCount)); |
|---|
| 213 | |
|---|
| 214 | BDBG_LEAVE(BPWR_GetModuleCount); |
|---|
| 215 | return 0; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | /*************************************************************************** |
|---|
| 219 | * |
|---|
| 220 | */ |
|---|
| 221 | BERR_Code BPWR_GetModuleList |
|---|
| 222 | ( BPWR_Handle hPwr, |
|---|
| 223 | BSTD_Module *pModuleList, |
|---|
| 224 | unsigned ulModuleCount ) |
|---|
| 225 | { |
|---|
| 226 | unsigned i, total = 0; |
|---|
| 227 | |
|---|
| 228 | BDBG_ENTER(BPWR_GetModuleList); |
|---|
| 229 | BDBG_OBJECT_ASSERT(hPwr, BPWR_P_Context); |
|---|
| 230 | |
|---|
| 231 | for(i=0; i<BPWR_MAX_MODULES; i++) { |
|---|
| 232 | if (total == ulModuleCount) { |
|---|
| 233 | return BERR_TRACE(BERR_UNKNOWN); |
|---|
| 234 | } |
|---|
| 235 | if (hPwr->modules[i].moduleId) { |
|---|
| 236 | pModuleList[total++] = hPwr->modules[i].moduleId; |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | BDBG_LEAVE(BPWR_GetModuleList); |
|---|
| 241 | return 0; |
|---|
| 242 | |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | /*************************************************************************** |
|---|
| 246 | * |
|---|
| 247 | */ |
|---|
| 248 | BERR_Code BPWR_SetModes |
|---|
| 249 | ( BPWR_Handle hPwr, |
|---|
| 250 | BSTD_Module *pModuleList, |
|---|
| 251 | uint32_t ulModuleCount, |
|---|
| 252 | BPWR_ePowerMode eMode ) |
|---|
| 253 | { |
|---|
| 254 | unsigned i; |
|---|
| 255 | |
|---|
| 256 | BDBG_ENTER(BPWR_SetModes); |
|---|
| 257 | BDBG_OBJECT_ASSERT(hPwr, BPWR_P_Context); |
|---|
| 258 | |
|---|
| 259 | for (i=0;i<ulModuleCount;i++) |
|---|
| 260 | { |
|---|
| 261 | BERR_Code rc; |
|---|
| 262 | rc = BPWR_SetMode(hPwr, pModuleList[i], eMode); |
|---|
| 263 | if (rc) return BERR_TRACE(rc); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | BDBG_LEAVE(BPWR_SetModes); |
|---|
| 267 | return 0; |
|---|
| 268 | |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | /*************************************************************************** |
|---|
| 272 | * BPWR_ApplyChanges |
|---|
| 273 | * |
|---|
| 274 | * Validate/Apply User's new changes. |
|---|
| 275 | */ |
|---|
| 276 | BERR_Code BPWR_ApplyChanges |
|---|
| 277 | ( BPWR_Handle hPwr ) |
|---|
| 278 | { |
|---|
| 279 | BERR_Code rc; |
|---|
| 280 | unsigned i; |
|---|
| 281 | |
|---|
| 282 | BDBG_OBJECT_ASSERT(hPwr, BPWR_P_Context); |
|---|
| 283 | |
|---|
| 284 | /* +------------------+ |
|---|
| 285 | * | VALIDATE CHANGES | |
|---|
| 286 | * +------------------+ |
|---|
| 287 | * Check if modules will result in correct power up |
|---|
| 288 | * or power down mode. See checklist for reference. |
|---|
| 289 | * If requisite modules are not specified, return an |
|---|
| 290 | * error. |
|---|
| 291 | */ |
|---|
| 292 | rc = BPWR_P_ValidateChanges(hPwr); |
|---|
| 293 | if(BERR_SUCCESS != rc) |
|---|
| 294 | { |
|---|
| 295 | return BERR_TRACE(rc); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | /* +-------------- + |
|---|
| 299 | * | APPLY CHANGES | |
|---|
| 300 | * +---------------+ |
|---|
| 301 | * Apply power up or power down to user-specified modules. |
|---|
| 302 | * Critical sections aren't necessary as all software modules are |
|---|
| 303 | * are closed at this point. |
|---|
| 304 | */ |
|---|
| 305 | rc = BPWR_P_SetPowerMode(hPwr); |
|---|
| 306 | if(BERR_SUCCESS != rc) |
|---|
| 307 | { |
|---|
| 308 | return BERR_TRACE(rc); |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | /* verify that all changes were made */ |
|---|
| 312 | for (i=0;i<BPWR_MAX_MODULES;i++) { |
|---|
| 313 | if (hPwr->modules[i].change) { |
|---|
| 314 | #if 1 |
|---|
| 315 | hPwr->modules[i].change = false; |
|---|
| 316 | #else |
|---|
| 317 | /* this is a way of verifying that every module id listed has some PWR code associated */ |
|---|
| 318 | BDBG_ERR(("PWR unable to make requested change to module %d", hPwr->modules[i].moduleId)); |
|---|
| 319 | rc = BERR_TRACE(BERR_UNKNOWN); |
|---|
| 320 | #endif |
|---|
| 321 | } |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | hPwr->changeMode = BPWR_Mode_ePowerUndefined; |
|---|
| 325 | |
|---|
| 326 | return rc; |
|---|
| 327 | } |
|---|
| 328 | |
|---|