| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2003-2012, 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: breg_mem.h $ |
|---|
| 11 | * $brcm_Revision: Hydra_Software_Devel/15 $ |
|---|
| 12 | * $brcm_Date: 3/9/12 4:48p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /magnum/basemodules/reg/breg_mem.h $ |
|---|
| 19 | * |
|---|
| 20 | * Hydra_Software_Devel/15 3/9/12 4:48p erickson |
|---|
| 21 | * SW7231-705: add BDBG_OBJECT_ASSERT |
|---|
| 22 | * |
|---|
| 23 | * Hydra_Software_Devel/14 8/24/10 8:25a erickson |
|---|
| 24 | * SW3548-815: allow BREG_Read/Write to be used inside a |
|---|
| 25 | * BREG_AtomicUpdate32 callback without warning. this avoids the need for |
|---|
| 26 | * the app to create a second method of accessing registers. |
|---|
| 27 | * |
|---|
| 28 | * Hydra_Software_Devel/13 5/3/10 10:41a erickson |
|---|
| 29 | * SW3548-815: document BREG_AtomicUpdate32 and |
|---|
| 30 | * BREG_SetAtomicUpdate32Callback more |
|---|
| 31 | * |
|---|
| 32 | * Hydra_Software_Devel/12 9/24/08 7:32p vsilyaev |
|---|
| 33 | * PR 46950: Use unified declaration of BREG handle for debug and release |
|---|
| 34 | * builds |
|---|
| 35 | * |
|---|
| 36 | * Hydra_Software_Devel/11 9/23/08 3:43p erickson |
|---|
| 37 | * PR46950: added BREG_SetAtomicUpdate32Callback |
|---|
| 38 | * |
|---|
| 39 | * Hydra_Software_Devel/10 6/5/08 5:24p vsilyaev |
|---|
| 40 | * PR43119: PR 43119: Added API for atomic register access |
|---|
| 41 | * |
|---|
| 42 | * Hydra_Software_Devel/PR43119/1 5/29/08 12:14p vsilyaev |
|---|
| 43 | * PR 43119: Added API for atomic register access |
|---|
| 44 | * |
|---|
| 45 | * Hydra_Software_Devel/9 7/21/06 11:27a vsilyaev |
|---|
| 46 | * PR 22695: Changes to make BREG_MEM compatible between debug and release |
|---|
| 47 | * builds |
|---|
| 48 | * |
|---|
| 49 | * Hydra_Software_Devel/8 12/22/04 4:06p marcusk |
|---|
| 50 | * PR13660: Added _isr flavors for each function. |
|---|
| 51 | * |
|---|
| 52 | * Hydra_Software_Devel/7 8/10/04 10:40a vsilyaev |
|---|
| 53 | * PR 12172: Unified type of the BREG_Handle, it removes space for |
|---|
| 54 | * different compiler behaviour in debug and release builds |
|---|
| 55 | * |
|---|
| 56 | * Hydra_Software_Devel/6 9/15/03 6:08p marcusk |
|---|
| 57 | * Updated to use void * as register base address. |
|---|
| 58 | * |
|---|
| 59 | * Hydra_Software_Devel/5 5/30/03 12:03p marcusk |
|---|
| 60 | * Changed CreateRegHandle/DestroyRegHandle to Open/Close to be more |
|---|
| 61 | * consistant. |
|---|
| 62 | * |
|---|
| 63 | * Hydra_Software_Devel/4 3/26/03 2:08p marcusk |
|---|
| 64 | * Added comments. |
|---|
| 65 | * |
|---|
| 66 | * Hydra_Software_Devel/3 3/7/03 9:24a marcusk |
|---|
| 67 | * Minor cleanup. |
|---|
| 68 | * |
|---|
| 69 | * Hydra_Software_Devel/2 3/5/03 4:19p marcusk |
|---|
| 70 | * Fixed minor issues (got it to compile). |
|---|
| 71 | * |
|---|
| 72 | * Hydra_Software_Devel/1 3/5/03 3:34p marcusk |
|---|
| 73 | * Initial version. |
|---|
| 74 | * |
|---|
| 75 | ***************************************************************************/ |
|---|
| 76 | |
|---|
| 77 | /*= Module Overview ******************************************************** |
|---|
| 78 | This module supplies the function required to access memory mapped registers. |
|---|
| 79 | Before any registers can be accessed an appropriate register handle should |
|---|
| 80 | be created (normally done at system init time). This register handle is |
|---|
| 81 | then given to any modules that require access to the corresponding memory |
|---|
| 82 | mapped registers. |
|---|
| 83 | ***************************************************************************/ |
|---|
| 84 | |
|---|
| 85 | #ifndef BREG_MEM_H |
|---|
| 86 | #define BREG_MEM_H |
|---|
| 87 | |
|---|
| 88 | #ifdef __cplusplus |
|---|
| 89 | extern "C" { |
|---|
| 90 | #endif |
|---|
| 91 | |
|---|
| 92 | /* |
|---|
| 93 | Summary: |
|---|
| 94 | This is an opaque handle that is used for memory mapped register functions. |
|---|
| 95 | */ |
|---|
| 96 | typedef struct BREG_Impl *BREG_Handle; |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | /* |
|---|
| 102 | Summary: |
|---|
| 103 | This function creates a register handle for memory mapped registers. |
|---|
| 104 | |
|---|
| 105 | Description: |
|---|
| 106 | This function must be called before any memory mapped registers can be |
|---|
| 107 | accessed. This normally is done by platform specific code during init |
|---|
| 108 | time. The resulting register handle is then passed to any modules that |
|---|
| 109 | must access the corresponding registers. |
|---|
| 110 | */ |
|---|
| 111 | void BREG_Open( |
|---|
| 112 | BREG_Handle *pRegHandle, /* [out] Returns a register handle */ |
|---|
| 113 | void *Address, /* Base address of the memory mapped register range */ |
|---|
| 114 | size_t MaxRegOffset /* Maximum offset for this memory mapped register range */ |
|---|
| 115 | ); |
|---|
| 116 | |
|---|
| 117 | /* |
|---|
| 118 | Summary: |
|---|
| 119 | This function destroys a previous created register handle. |
|---|
| 120 | |
|---|
| 121 | Description: |
|---|
| 122 | This function frees any resources associate with a memory mapped register |
|---|
| 123 | handle. After this function is called the register handle can no longer |
|---|
| 124 | be used for subsequent register accesses. |
|---|
| 125 | */ |
|---|
| 126 | void BREG_Close( |
|---|
| 127 | BREG_Handle RegHandle /* Register handle created by BREG_CreateRegHandle() */ |
|---|
| 128 | ); |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | #if BDBG_DEBUG_BUILD != 1 |
|---|
| 132 | |
|---|
| 133 | #include "breg_mem_opt.h" /* Contains optimized versions of these routines */ |
|---|
| 134 | |
|---|
| 135 | #else |
|---|
| 136 | |
|---|
| 137 | /* |
|---|
| 138 | Summary: |
|---|
| 139 | This function writes 32 bits to a register. |
|---|
| 140 | |
|---|
| 141 | Description: |
|---|
| 142 | Although this fuction will never return an error it will assert if the |
|---|
| 143 | RegHandle is invalid or during a debug build if the reg offset is larger |
|---|
| 144 | than the MaxRegOffset specified in the BREG_CreateRegHandle function. |
|---|
| 145 | */ |
|---|
| 146 | void BREG_Write32( |
|---|
| 147 | BREG_Handle RegHandle, /* Register handle created by BREG_CreateRegHandle() */ |
|---|
| 148 | uint32_t reg, /* Register offset to write */ |
|---|
| 149 | uint32_t data /* Data value to write to register */ |
|---|
| 150 | ); |
|---|
| 151 | |
|---|
| 152 | /* |
|---|
| 153 | Summary: |
|---|
| 154 | This function writes 16 bits to a register. |
|---|
| 155 | |
|---|
| 156 | Description: |
|---|
| 157 | Although this fuction will never return an error it will assert if the |
|---|
| 158 | RegHandle is invalid or during a debug build if the reg offset is larger |
|---|
| 159 | than the MaxRegOffset specified in the BREG_CreateRegHandle function. |
|---|
| 160 | */ |
|---|
| 161 | void BREG_Write16( |
|---|
| 162 | BREG_Handle RegHandle, /* Register handle created by BREG_CreateRegHandle() */ |
|---|
| 163 | uint32_t reg, /* Register offset to write */ |
|---|
| 164 | uint16_t data /* Data value to write to register */ |
|---|
| 165 | ); |
|---|
| 166 | |
|---|
| 167 | /* |
|---|
| 168 | Summary: |
|---|
| 169 | This function writes 8 bits to a register. |
|---|
| 170 | |
|---|
| 171 | Description: |
|---|
| 172 | Although this fuction will never return an error it will assert if the |
|---|
| 173 | RegHandle is invalid or during a debug build if the reg offset is larger |
|---|
| 174 | than the MaxRegOffset specified in the BREG_CreateRegHandle function. |
|---|
| 175 | */ |
|---|
| 176 | void BREG_Write8( |
|---|
| 177 | BREG_Handle RegHandle, /* Register handle created by BREG_CreateRegHandle() */ |
|---|
| 178 | uint32_t reg, /* Register offset to write */ |
|---|
| 179 | uint8_t data /* Data value to write to register */ |
|---|
| 180 | ); |
|---|
| 181 | |
|---|
| 182 | /* |
|---|
| 183 | Summary: |
|---|
| 184 | This function reads 32 bits from a register. |
|---|
| 185 | |
|---|
| 186 | Description: |
|---|
| 187 | Although this fuction cannot return an error it will assert if the |
|---|
| 188 | RegHandle is invalid or during a debug build if the reg offset is larger |
|---|
| 189 | than the MaxRegOffset specified in the BREG_CreateRegHandle function. |
|---|
| 190 | */ |
|---|
| 191 | uint32_t BREG_Read32( |
|---|
| 192 | BREG_Handle RegHandle, /* Register handle created by BREG_CreateRegHandle() */ |
|---|
| 193 | uint32_t reg /* Register offset to read */ |
|---|
| 194 | ); |
|---|
| 195 | |
|---|
| 196 | /* |
|---|
| 197 | Summary: |
|---|
| 198 | This function reads 16 bits from a register. |
|---|
| 199 | |
|---|
| 200 | Description: |
|---|
| 201 | Although this fuction cannot return an error it will assert if the |
|---|
| 202 | RegHandle is invalid or during a debug build if the reg offset is larger |
|---|
| 203 | than the MaxRegOffset specified in the BREG_CreateRegHandle function. |
|---|
| 204 | */ |
|---|
| 205 | uint16_t BREG_Read16( |
|---|
| 206 | BREG_Handle RegHandle, /* Register handle created by BREG_CreateRegHandle() */ |
|---|
| 207 | uint32_t reg /* Register offset to read */ |
|---|
| 208 | ); |
|---|
| 209 | |
|---|
| 210 | /* |
|---|
| 211 | Summary: |
|---|
| 212 | This function reads 8 bits from a register. |
|---|
| 213 | |
|---|
| 214 | Description: |
|---|
| 215 | Although this fuction cannot return an error it will assert if the |
|---|
| 216 | RegHandle is invalid or during a debug build if the reg offset is larger |
|---|
| 217 | than the MaxRegOffset specified in the BREG_CreateRegHandle function. |
|---|
| 218 | */ |
|---|
| 219 | uint8_t BREG_Read8( |
|---|
| 220 | BREG_Handle RegHandle, /* Register handle created by BREG_CreateRegHandle() */ |
|---|
| 221 | uint32_t reg /* Register offset to read */ |
|---|
| 222 | ); |
|---|
| 223 | |
|---|
| 224 | #endif /* BDBG_DEBUG_BUILD != 1 */ |
|---|
| 225 | |
|---|
| 226 | /* |
|---|
| 227 | Summary: |
|---|
| 228 | This function atomically updates a 32 bit register value. |
|---|
| 229 | |
|---|
| 230 | Description: |
|---|
| 231 | This function atomically update 32 bit value stored in the hardware register. |
|---|
| 232 | It's equivalent to the following code: |
|---|
| 233 | |
|---|
| 234 | BREG_Write32(regHandle, reg, (BREG_Read32(regHandle, reg) & (~mask)) | value); |
|---|
| 235 | |
|---|
| 236 | with the added guarantee that the read/modify/write will be performed atomically across all magnum modules. |
|---|
| 237 | |
|---|
| 238 | See BREG_SetAtomicUpdate32Callback for extending the synchronization domain outside of magnum and into the OS as well. |
|---|
| 239 | */ |
|---|
| 240 | void BREG_AtomicUpdate32( |
|---|
| 241 | BREG_Handle RegHandle, /* Register handle created by BREG_CreateRegHandle() */ |
|---|
| 242 | uint32_t reg, /* Register offset to write */ |
|---|
| 243 | uint32_t mask, |
|---|
| 244 | uint32_t value |
|---|
| 245 | ); |
|---|
| 246 | |
|---|
| 247 | /* |
|---|
| 248 | Summary: |
|---|
| 249 | This function atomically updates a 32 bit register value. Used for _isr context. |
|---|
| 250 | |
|---|
| 251 | Description: |
|---|
| 252 | See BREG_AtomicUpdate32. |
|---|
| 253 | */ |
|---|
| 254 | void BREG_AtomicUpdate32_isr( |
|---|
| 255 | BREG_Handle RegHandle, /* Register handle created by BREG_CreateRegHandle() */ |
|---|
| 256 | uint32_t reg, /* Register offset to write */ |
|---|
| 257 | uint32_t mask, |
|---|
| 258 | uint32_t value |
|---|
| 259 | ); |
|---|
| 260 | |
|---|
| 261 | /* |
|---|
| 262 | Summary: |
|---|
| 263 | Callback for BREG_SetAtomicUpdate32Callback |
|---|
| 264 | |
|---|
| 265 | Description: |
|---|
| 266 | Operation should be implemented as: |
|---|
| 267 | |
|---|
| 268 | uint32_t value; |
|---|
| 269 | lock(); |
|---|
| 270 | value = *CONVERT_TO_ADDR(reg); |
|---|
| 271 | value &= ~mask; |
|---|
| 272 | value |= value; |
|---|
| 273 | *CONVERT_TO_ADDR(reg) = value; |
|---|
| 274 | unlock(); |
|---|
| 275 | */ |
|---|
| 276 | typedef void (*BREG_AtomicUpdate32Callback)( |
|---|
| 277 | void *callbackContext, /* callbackContext passed into BREG_SetAtomicUpdate32Callback */ |
|---|
| 278 | uint32_t reg, /* Register offset to write */ |
|---|
| 279 | uint32_t mask, |
|---|
| 280 | uint32_t value |
|---|
| 281 | ); |
|---|
| 282 | |
|---|
| 283 | /* |
|---|
| 284 | Summary: |
|---|
| 285 | Set optional atomic update callback |
|---|
| 286 | |
|---|
| 287 | Description: |
|---|
| 288 | On some systems, there are registers which are shared between magnum and the OS. |
|---|
| 289 | In these cases, a platform-specific atomic update must be implemented outside of Magnum by means of this isr callback. |
|---|
| 290 | |
|---|
| 291 | The required implementation varies per OS. For example: |
|---|
| 292 | - In linux kernel mode, the atomic update callback will acquire a spinlock. |
|---|
| 293 | - In linux user mode, the atomic update callback will call an ioctl to the user mode driver which will then acquire a spinlock. |
|---|
| 294 | - In vxworks, the atomic update callback could acquire a mutex shared by the OS. |
|---|
| 295 | |
|---|
| 296 | This callback is not required if there are no registers shared between magnum and the OS. |
|---|
| 297 | Also, if the kernel can call BKNI_EnterCriticalSection for shared registers, this callback is not required. |
|---|
| 298 | */ |
|---|
| 299 | void BREG_SetAtomicUpdate32Callback( |
|---|
| 300 | BREG_Handle RegHandle, /* Register handle created by BREG_CreateRegHandle() */ |
|---|
| 301 | BREG_AtomicUpdate32Callback callback_isr, /* If NULL, default behavior will be restored. |
|---|
| 302 | This callback is called in isr context. It must be very fast. */ |
|---|
| 303 | void *callbackContext /* User context passed to callback */ |
|---|
| 304 | ); |
|---|
| 305 | |
|---|
| 306 | /* |
|---|
| 307 | Summary: |
|---|
| 308 | This function atomically compares and updates 32 bit register value. |
|---|
| 309 | |
|---|
| 310 | Description: |
|---|
| 311 | This function atomically reads 32 bit value from the hardware register, compares with value |
|---|
| 312 | given by user, and if match newValue stored in the hardware register. It's equivalent to the following |
|---|
| 313 | code: |
|---|
| 314 | |
|---|
| 315 | uint32_t val = BREG_Read32(regHandle, reg); |
|---|
| 316 | if(val==oldValue) BREG_Write32(regHandle, reg, newValue) |
|---|
| 317 | return val; |
|---|
| 318 | |
|---|
| 319 | with an added guarantee that the read and write are performed atomically. |
|---|
| 320 | */ |
|---|
| 321 | uint32_t BREG_CompareAndSwap32( |
|---|
| 322 | BREG_Handle RegHandle, /* Register handle created by BREG_CreateRegHandle() */ |
|---|
| 323 | uint32_t reg, /* Register offset to write */ |
|---|
| 324 | uint32_t oldValue, /* value to compare */ |
|---|
| 325 | uint32_t newValue /* value to write */ |
|---|
| 326 | ); |
|---|
| 327 | |
|---|
| 328 | /* |
|---|
| 329 | Summary: |
|---|
| 330 | This function atomically compares and updates 32 bit register value. Used for _isr context. |
|---|
| 331 | */ |
|---|
| 332 | uint32_t BREG_CompareAndSwap32_isr( |
|---|
| 333 | BREG_Handle RegHandle, /* Register handle created by BREG_CreateRegHandle() */ |
|---|
| 334 | uint32_t reg, /* Register offset to write */ |
|---|
| 335 | uint32_t oldValue, /* value to compare */ |
|---|
| 336 | uint32_t newValue /* value to write */ |
|---|
| 337 | ); |
|---|
| 338 | |
|---|
| 339 | /* ISR() flavors go here so they are picked up for both debug and release builds */ |
|---|
| 340 | |
|---|
| 341 | /* |
|---|
| 342 | Summary: |
|---|
| 343 | This function writes 16 bits to a register. Used for _isr context. |
|---|
| 344 | */ |
|---|
| 345 | #define BREG_Write16_isr BREG_Write16 |
|---|
| 346 | |
|---|
| 347 | /* |
|---|
| 348 | Summary: |
|---|
| 349 | This function writes 8 bits to a register. Used for _isr context. |
|---|
| 350 | */ |
|---|
| 351 | #define BREG_Write8_isr BREG_Write8 |
|---|
| 352 | |
|---|
| 353 | /* |
|---|
| 354 | Summary: |
|---|
| 355 | This function writes 32 bits to a register. Used for _isr context. |
|---|
| 356 | */ |
|---|
| 357 | #define BREG_Write32_isr BREG_Write32 |
|---|
| 358 | |
|---|
| 359 | /* |
|---|
| 360 | Summary: |
|---|
| 361 | This function reads 8 bits from a register. Used for _isr context. |
|---|
| 362 | */ |
|---|
| 363 | #define BREG_Read8_isr BREG_Read8 |
|---|
| 364 | |
|---|
| 365 | /* |
|---|
| 366 | Summary: |
|---|
| 367 | This function reads 16 bits from a register. Used for _isr context. |
|---|
| 368 | */ |
|---|
| 369 | #define BREG_Read16_isr BREG_Read16 |
|---|
| 370 | |
|---|
| 371 | /* |
|---|
| 372 | Summary: |
|---|
| 373 | This function reads 32 bits from a register. Used for _isr context. |
|---|
| 374 | */ |
|---|
| 375 | #define BREG_Read32_isr BREG_Read32 |
|---|
| 376 | |
|---|
| 377 | /* Internal representation of the BREG handle */ |
|---|
| 378 | BDBG_OBJECT_ID_DECLARE(BREG); |
|---|
| 379 | typedef struct BREG_Impl |
|---|
| 380 | { |
|---|
| 381 | BDBG_OBJECT(BREG) |
|---|
| 382 | uint32_t BaseAddr; /* BaseAddr shall be the first member to keep it run-time compatible with the release builds */ |
|---|
| 383 | size_t MaxRegOffset; |
|---|
| 384 | |
|---|
| 385 | struct { |
|---|
| 386 | BREG_AtomicUpdate32Callback callback_isr; |
|---|
| 387 | void *callbackContext; |
|---|
| 388 | } atomicUpdate32; |
|---|
| 389 | bool inAtomicUpdateCallback; |
|---|
| 390 | } BREG_Impl; |
|---|
| 391 | |
|---|
| 392 | #ifdef __cplusplus |
|---|
| 393 | } |
|---|
| 394 | #endif |
|---|
| 395 | |
|---|
| 396 | #endif |
|---|
| 397 | /* End of File */ |
|---|