| 1 | /****************************************************************************** |
|---|
| 2 | * -Copyright (c) 2004 Digital Stream Technologies Inc. All Rights Reserved. |
|---|
| 3 | * |
|---|
| 4 | * Module: dsthaldev.c |
|---|
| 5 | * Author: Jun-ku Park, hwatk@dstreamtech.com |
|---|
| 6 | * Description: DST HAL [Device] Platform/Project Indepedent Source |
|---|
| 7 | * |
|---|
| 8 | * notes: hwatk20040609 |
|---|
| 9 | * |
|---|
| 10 | * hwatk/20041116, DHL_DEV_NFLASHSetMode() ÇÔ¼ö Ãß°¡. |
|---|
| 11 | * |
|---|
| 12 | * TO DO LIST |
|---|
| 13 | * |
|---|
| 14 | ******************************************************************************/ |
|---|
| 15 | |
|---|
| 16 | /****************************************************************************** |
|---|
| 17 | * Global variable declaration |
|---|
| 18 | ******************************************************************************/ |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | /****************************************************************************** |
|---|
| 22 | * Imported variable declaration |
|---|
| 23 | ******************************************************************************/ |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | /****************************************************************************** |
|---|
| 27 | * Imported function declaration |
|---|
| 28 | ******************************************************************************/ |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | /****************************************************************************** |
|---|
| 32 | * Local definitions |
|---|
| 33 | ******************************************************************************/ |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | /****************************************************************************** |
|---|
| 37 | * Local typedefs |
|---|
| 38 | ******************************************************************************/ |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | /****************************************************************************** |
|---|
| 42 | * Local function prototypes |
|---|
| 43 | ******************************************************************************/ |
|---|
| 44 | |
|---|
| 45 | #include "dsthalcommon.h" |
|---|
| 46 | #include "dstdddev.h" |
|---|
| 47 | |
|---|
| 48 | #ifdef DMALLOC |
|---|
| 49 | #include <dmalloc.h> |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | static DST_DEV_NFLASH sNFLASH[NFLASHID_MAX-1]; |
|---|
| 53 | static DST_DEV_SFLASH sSFLASH[SFLASHID_MAX-1]; |
|---|
| 54 | |
|---|
| 55 | DS_U32 g_CaMutex = 0; |
|---|
| 56 | |
|---|
| 57 | #define LOCK_CA_MUTEX() do { \ |
|---|
| 58 | if ( OS_TakeMutex(g_CaMutex) != OS_OK ) { \ |
|---|
| 59 | printf("MUTEX ERROR, LINE=%d\n", __LINE__); \ |
|---|
| 60 | goto done; \ |
|---|
| 61 | } \ |
|---|
| 62 | } while (0) |
|---|
| 63 | |
|---|
| 64 | #define UNLOCK_CA_MUTEX() do { \ |
|---|
| 65 | if ( OS_GiveMutex(g_CaMutex) != OS_OK ) { \ |
|---|
| 66 | printf("MUTEX ERROR, LINE=%d\n", __LINE__); \ |
|---|
| 67 | } \ |
|---|
| 68 | } while (0) |
|---|
| 69 | |
|---|
| 70 | #if 0 |
|---|
| 71 | ___EEPROM_Functions__________() |
|---|
| 72 | #endif |
|---|
| 73 | #if defined(USE_EEPROM_FILE) |
|---|
| 74 | static FILE *g_EepromFlashFile=(FILE *)0; |
|---|
| 75 | #endif |
|---|
| 76 | |
|---|
| 77 | #define WR_CYCLE_DELAY 5 /* 5 ms EEPROM write cycle */ |
|---|
| 78 | #define MAX_PAGE_LEN 32 |
|---|
| 79 | #define PAGE_MASK (MAX_PAGE_LEN - 1) |
|---|
| 80 | |
|---|
| 81 | int DHL_DEV_WriteEEPROM(unsigned short WriteAddr,unsigned short length,unsigned char *wbuf) |
|---|
| 82 | { |
|---|
| 83 | DS_U32 i=CORE_OK; |
|---|
| 84 | |
|---|
| 85 | #if defined(USE_EEPROM_FILE) |
|---|
| 86 | if (g_EepromFlashFile == (FILE *)0) |
|---|
| 87 | { |
|---|
| 88 | g_EepromFlashFile = fopen(EEPROM_FILE_PATH, "w+b"); |
|---|
| 89 | if (g_EepromFlashFile == (FILE *)NULL) |
|---|
| 90 | { |
|---|
| 91 | printf("ERROR: Cannot open %s\n", EEPROM_FILE_PATH); |
|---|
| 92 | return -1; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | if ( fseek( g_EepromFlashFile, WriteAddr, SEEK_SET ) ) |
|---|
| 97 | { |
|---|
| 98 | printf("ERROR: fseek()\n"); |
|---|
| 99 | return -1; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | i=fwrite( wbuf, 1, length, g_EepromFlashFile ); |
|---|
| 103 | if ( i != length ) |
|---|
| 104 | { |
|---|
| 105 | printf("ERROR: cannot write EEPROM data into file.\n"); |
|---|
| 106 | return -1; |
|---|
| 107 | } |
|---|
| 108 | #else |
|---|
| 109 | DS_U8 WrBuf[MAX_PAGE_LEN+4]; |
|---|
| 110 | DS_U32 WrLen; |
|---|
| 111 | int result; |
|---|
| 112 | //printf("|%s| WriteAddr: 0x%X, length:%d\n", __FUNCTION__, WriteAddr, length); |
|---|
| 113 | |
|---|
| 114 | while (length) |
|---|
| 115 | { |
|---|
| 116 | WrBuf[0] = WriteAddr & 0xff; |
|---|
| 117 | |
|---|
| 118 | i = (WriteAddr & (~PAGE_MASK)) + MAX_PAGE_LEN - WriteAddr; |
|---|
| 119 | WrLen = length; |
|---|
| 120 | if (WrLen > MAX_PAGE_LEN) |
|---|
| 121 | { |
|---|
| 122 | WrLen = MAX_PAGE_LEN; |
|---|
| 123 | } |
|---|
| 124 | if (WrLen > i) |
|---|
| 125 | { |
|---|
| 126 | WrLen = i; |
|---|
| 127 | } |
|---|
| 128 | length -= WrLen; |
|---|
| 129 | memcpy (&(WrBuf[1]), wbuf, WrLen); |
|---|
| 130 | |
|---|
| 131 | result = DHL_SYS_I2cWriteEx(0,EEPROM_I2C_ADDRESS,1,(WriteAddr>>8)&0xFF,WrBuf,WrLen+1); |
|---|
| 132 | if(result < 0){ |
|---|
| 133 | printf("EEPROM I2C Write Error\n"); |
|---|
| 134 | break; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | /*================================================================= |
|---|
| 138 | * Do acknowledge polling (wait for write operation to finish) |
|---|
| 139 | *================================================================*/ |
|---|
| 140 | for (i=0; i<WR_CYCLE_DELAY; i++) |
|---|
| 141 | { |
|---|
| 142 | result = DHL_SYS_I2cWriteEx(0,EEPROM_I2C_ADDRESS,1,(WriteAddr>>8)&0xFF,WrBuf,1); |
|---|
| 143 | if(result < 0){ |
|---|
| 144 | //printf("|%s:%d| EEPROM I2C Write Error\n", __FUNCTION__, __LINE__); |
|---|
| 145 | } else { |
|---|
| 146 | break; |
|---|
| 147 | } |
|---|
| 148 | OS_mDelay(1); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | result = DHL_SYS_I2cWriteEx(0,EEPROM_I2C_ADDRESS,1,(WriteAddr>>8)&0xFF,WrBuf,1); |
|---|
| 152 | if(result < 0) { |
|---|
| 153 | printf("|%s:%d| EEPROM I2C Write Error\n", __FUNCTION__, __LINE__); |
|---|
| 154 | break; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | WriteAddr+= WrLen; |
|---|
| 158 | wbuf = (void *) ((DS_U32)wbuf + WrLen); |
|---|
| 159 | } |
|---|
| 160 | #endif |
|---|
| 161 | return (i); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | int DHL_DEV_ReadEEPROM(unsigned short addr,unsigned short length,unsigned char *rbuf) |
|---|
| 165 | { |
|---|
| 166 | int result = -1; |
|---|
| 167 | int i = -1; |
|---|
| 168 | |
|---|
| 169 | if(addr+length > EEPROM_SIZE) |
|---|
| 170 | { |
|---|
| 171 | DHL_DbgPrintf( 0, DHLDBG_SYS, "Address + Length is not avail\n"); |
|---|
| 172 | return -1; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | #if defined(USE_EEPROM_FILE) |
|---|
| 176 | if (g_EepromFlashFile == (FILE *)0) |
|---|
| 177 | { |
|---|
| 178 | g_EepromFlashFile = fopen(EEPROM_FILE_PATH, "r+b"); |
|---|
| 179 | if ( !g_EepromFlashFile ) |
|---|
| 180 | { |
|---|
| 181 | g_EepromFlashFile = fopen(EEPROM_FILE_PATH, "w+b"); |
|---|
| 182 | if ( !g_EepromFlashFile ) |
|---|
| 183 | { |
|---|
| 184 | printf("ERROR: Cannot open %s\n", EEPROM_FILE_PATH); |
|---|
| 185 | return -1; |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | if ( fseek( g_EepromFlashFile, 0L, SEEK_SET ) ) |
|---|
| 191 | { |
|---|
| 192 | printf("ERROR: fseek()\n"); |
|---|
| 193 | return -1; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | i=fread( rbuf, 1, length, g_EepromFlashFile ); |
|---|
| 197 | if (i != length) |
|---|
| 198 | { |
|---|
| 199 | printf("ERROR: cannot read EEPROM data from file. (addr: %d, length: %d, i:%d)\n", addr, length, i); |
|---|
| 200 | perror("fread()"); |
|---|
| 201 | return -1; |
|---|
| 202 | } |
|---|
| 203 | #else |
|---|
| 204 | result = DHL_SYS_I2cReadEx(0,EEPROM_I2C_ADDRESS,1,addr,rbuf,length); |
|---|
| 205 | if( result < 0){ |
|---|
| 206 | DHL_DbgPrintf( 0, DHLDBG_SYS, "I2C Read Fail\n"); |
|---|
| 207 | goto done; |
|---|
| 208 | } |
|---|
| 209 | done: |
|---|
| 210 | #endif |
|---|
| 211 | result = i; |
|---|
| 212 | return result; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | #if 0 |
|---|
| 218 | ___NFLASH_Functions__________() |
|---|
| 219 | #endif |
|---|
| 220 | /**************************************************************************** |
|---|
| 221 | |
|---|
| 222 | DHL_RESULT DHL_DEV_NFLASHInit |
|---|
| 223 | - Summary : Initialize NAND flash device, including NAND flash bus. |
|---|
| 224 | - Argument |
|---|
| 225 | NFLASHID { NFLASH_0 } |
|---|
| 226 | Size - NAND Flash Size |
|---|
| 227 | Start - NAND Flash Usable Area Start Offset |
|---|
| 228 | End - NAND Flash Usable Area End Offset |
|---|
| 229 | if ( End == 0 ) End = Start + Size |
|---|
| 230 | - Returns : DHL_OK on success, DHL_FAIL on failure. |
|---|
| 231 | |
|---|
| 232 | ***************************************************************************/ |
|---|
| 233 | DHL_RESULT DHL_DEV_InitNFLASH( NFLASHID NFlashId, DS_U32 Size, DS_U32 Start, DS_U32 End ) |
|---|
| 234 | { |
|---|
| 235 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 236 | P_DST_DEV_NFLASH pNFlash; |
|---|
| 237 | |
|---|
| 238 | if ( NFlashId >= NFLASHID_MAX ) { |
|---|
| 239 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : NFLASH Id is invalid, Id = %d\n", NFlashId ); |
|---|
| 240 | return DHL_FAIL_INVALID_HANDLE; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | pNFlash = &sNFLASH[NFlashId-1]; |
|---|
| 244 | #if 0 /* hwatk, 2004.06.30, Init¿¡¼´Â State °Ë»çÇÏÁö ¾ÊÀ½. */ |
|---|
| 245 | if ( pNFlash->CurState != NFLASHSTATE_UNKNOWN ) { |
|---|
| 246 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : NFlash state is invalid, State = %d\n", pNFlash->CurState ); |
|---|
| 247 | return DHL_FAIL; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | if ( (Size <= 0) || ((Start > End) && End) || ( Start && End && (Start==End) ) ) { |
|---|
| 251 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : Invalid argument, Size=%lx, Start=%lx, End=%lx!\n", Size, Start, End ); |
|---|
| 252 | return DHL_FAIL_INVALID_PARAM; |
|---|
| 253 | } |
|---|
| 254 | #endif |
|---|
| 255 | |
|---|
| 256 | pNFlash->NFlashId = NFlashId; |
|---|
| 257 | |
|---|
| 258 | if ( End == 0 ) End = Start + Size; |
|---|
| 259 | dhlResult = DD_DEV_NFLASHInit( pNFlash, Size, Start, End ); |
|---|
| 260 | if ( dhlResult != DHL_OK ) { |
|---|
| 261 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : DD_DEV_NFLASHInit()\n" ); |
|---|
| 262 | return dhlResult; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | pNFlash->CurState = NFLASHSTATE_INIT; |
|---|
| 266 | |
|---|
| 267 | /* Initialize MUTEX */ |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | return dhlResult; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | /**************************************************************************** |
|---|
| 275 | |
|---|
| 276 | int DHL_DEV_NFLASHRead |
|---|
| 277 | - Summary : Read data from NAND flash. |
|---|
| 278 | - Argument |
|---|
| 279 | NFLASHID { NFLASH_0 } |
|---|
| 280 | Address |
|---|
| 281 | pBuffer |
|---|
| 282 | Len |
|---|
| 283 | - Returns : Bytes read on success, -1 on failure. |
|---|
| 284 | |
|---|
| 285 | ***************************************************************************/ |
|---|
| 286 | int DHL_DEV_ReadNFLASH( NFLASHID NFlashId, DS_U32 Address, DS_U8 *pBuffer, DS_U32 Len ) |
|---|
| 287 | { |
|---|
| 288 | int RetVal; |
|---|
| 289 | P_DST_DEV_NFLASH pNFlash; |
|---|
| 290 | |
|---|
| 291 | if ( NFlashId >= NFLASHID_MAX ) { |
|---|
| 292 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : NFLASH Id is invalid, Id = %d\n", NFlashId ); |
|---|
| 293 | return -1; |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | /* Take the MUTEX, notes : MUTEX should be taken before state checking. */ |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | /* State Checking */ |
|---|
| 300 | pNFlash = &sNFLASH[NFlashId-1]; |
|---|
| 301 | if ( pNFlash->CurState != NFLASHSTATE_INIT ) {// NFLASHSTATE_START -> NFLASHSTATE_INIT |
|---|
| 302 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : NFlash state is invalid, State = %d\n", pNFlash->CurState ); |
|---|
| 303 | RetVal = -1; |
|---|
| 304 | goto done; |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | /* Argument Checking */ |
|---|
| 308 | if ( (pBuffer == (DS_U8)0) || (Len == 0) ) { |
|---|
| 309 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : Invalid Arguments, pBuffer = %lX, Len = %ld\n", |
|---|
| 310 | pBuffer, Len ); |
|---|
| 311 | RetVal = -1; |
|---|
| 312 | goto done; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | /* Checks that the Address - Len range is in usable area. */ |
|---|
| 316 | if ( ( (Address < pNFlash->uAvlStart) || (Address > pNFlash->uAvlEnd) ) || |
|---|
| 317 | ( ((Address+Len) < pNFlash->uAvlStart) || ((Address+Len) > pNFlash->uAvlEnd) ) || |
|---|
| 318 | (Len > pNFlash->uSize) ) |
|---|
| 319 | { |
|---|
| 320 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : the area is not in usable area. Address = %x, Len = %x\n", Address, Len ); |
|---|
| 321 | RetVal = -1; |
|---|
| 322 | goto done; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | /* Call the lower device driver */ |
|---|
| 326 | RetVal = DD_DEV_NFLASHRead( pNFlash, Address, pBuffer, Len ); |
|---|
| 327 | if ( RetVal < 0 ) { |
|---|
| 328 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : DD_DEV_NFLASHRead()\n" ); |
|---|
| 329 | RetVal = -1; |
|---|
| 330 | goto done; |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | done: |
|---|
| 334 | /* Give the MUTEX */ |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | return RetVal; |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | /**************************************************************************** |
|---|
| 341 | |
|---|
| 342 | int DHL_DEV_NFLASHWrite |
|---|
| 343 | - Summary : Write data on NAND flash. |
|---|
| 344 | - Argument |
|---|
| 345 | NFLASHID { NFLASH_0 } |
|---|
| 346 | Address |
|---|
| 347 | pBuffer |
|---|
| 348 | Len |
|---|
| 349 | - Returns : Bytes written on success, -1 on failure. |
|---|
| 350 | |
|---|
| 351 | ***************************************************************************/ |
|---|
| 352 | int DHL_DEV_WriteNFLASH( NFLASHID NFlashId, DS_U32 Address, DS_U8 *pBuffer, DS_U32 Len ) |
|---|
| 353 | { |
|---|
| 354 | int RetVal; |
|---|
| 355 | P_DST_DEV_NFLASH pNFlash; |
|---|
| 356 | |
|---|
| 357 | if ( NFlashId >= NFLASHID_MAX ) { |
|---|
| 358 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : NFLASH Id is invalid, Id = %d\n", NFlashId ); |
|---|
| 359 | return DHL_FAIL_INVALID_HANDLE; |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | /* Take the MUTEX, notes : MUTEX should be taken before state checking. */ |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | /* State Checking */ |
|---|
| 366 | pNFlash = &sNFLASH[NFlashId-1]; |
|---|
| 367 | if ( pNFlash->CurState != NFLASHSTATE_INIT ) { |
|---|
| 368 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : NFlash state is invalid, State = %d\n", pNFlash->CurState ); |
|---|
| 369 | RetVal = -1; |
|---|
| 370 | goto done; |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | /* Argument Checking */ |
|---|
| 374 | if ( (pBuffer == (DS_U8)0) || (Len == 0) ) { |
|---|
| 375 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : Invalid Arguments, pBuffer = %lX, Len = %ld\n", |
|---|
| 376 | pBuffer, Len ); |
|---|
| 377 | RetVal = -1; |
|---|
| 378 | goto done; |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | /* Checks that the Address - Len range is in usable area. */ |
|---|
| 382 | if ( ( (Address < pNFlash->uAvlStart) || (Address > pNFlash->uAvlEnd) ) || |
|---|
| 383 | ( ((Address+Len) < pNFlash->uAvlStart) || ((Address+Len) > pNFlash->uAvlEnd) ) || |
|---|
| 384 | (Len > pNFlash->uSize)) |
|---|
| 385 | { |
|---|
| 386 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : the area is not in usable area. Address = %x, Len = %x\n", Address, Len ); |
|---|
| 387 | RetVal = -1; |
|---|
| 388 | goto done; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | /* Call the lower device driver. */ |
|---|
| 392 | RetVal = DD_DEV_NFLASHWrite( pNFlash, Address, pBuffer, Len ); |
|---|
| 393 | if ( RetVal < 0 ) { |
|---|
| 394 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : DD_DEV_NFLASHWrite(), RetVal = %d\n", RetVal ); |
|---|
| 395 | goto done; |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | done: |
|---|
| 399 | /* Give the MUTEX */ |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | return RetVal; |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | /**************************************************************************** |
|---|
| 406 | |
|---|
| 407 | DHL_RESULT DHL_DEV_NFLASHErase |
|---|
| 408 | - Summary : Erase NAND Flash. |
|---|
| 409 | - Argument |
|---|
| 410 | NFLASHID { NFLASH_0 } |
|---|
| 411 | Address |
|---|
| 412 | Len |
|---|
| 413 | - Returns : DHL_OK on success, DHL_FAIL on failure. |
|---|
| 414 | |
|---|
| 415 | ***************************************************************************/ |
|---|
| 416 | DHL_RESULT DHL_DEV_EraseNFLASH( NFLASHID NFlashId, DS_U32 Address, DS_U32 Len ) |
|---|
| 417 | { |
|---|
| 418 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 419 | P_DST_DEV_NFLASH pNFlash; |
|---|
| 420 | |
|---|
| 421 | if ( NFlashId >= NFLASHID_MAX ) { |
|---|
| 422 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : NFLASH Id is invalid, Id = %d\n", NFlashId ); |
|---|
| 423 | return DHL_FAIL_INVALID_HANDLE; |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | /* Take the MUTEX, notes : MUTEX should be taken before state checking. */ |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | /* State Checking */ |
|---|
| 431 | pNFlash = &sNFLASH[NFlashId-1]; |
|---|
| 432 | if ( pNFlash->CurState != NFLASHSTATE_INIT ) { |
|---|
| 433 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : NFlash state is invalid, State = %d\n", pNFlash->CurState ); |
|---|
| 434 | return DHL_FAIL; |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | /* Argument Checking */ |
|---|
| 438 | if ( Len == 0 ) { |
|---|
| 439 | dhlResult = DHL_FAIL; |
|---|
| 440 | goto done; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | /* Checks that the Address - Len range is in usable area. */ |
|---|
| 444 | if ( ( (Address < pNFlash->uAvlStart) || (Address > pNFlash->uAvlEnd) ) || |
|---|
| 445 | ( ((Address+Len) < pNFlash->uAvlStart) || ((Address+Len) > pNFlash->uAvlEnd) ) || |
|---|
| 446 | (Len > pNFlash->uSize) ) |
|---|
| 447 | { |
|---|
| 448 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : the area is not in usable area. NFlashId = 0x%x, Address = %x, Len = %x\n", NFlashId, Address, Len ); |
|---|
| 449 | dhlResult = DHL_FAIL; |
|---|
| 450 | goto done; |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | /* Call the lower device driver */ |
|---|
| 454 | dhlResult = DD_DEV_NFLASHErase( pNFlash, Address, Len ); |
|---|
| 455 | if ( dhlResult != DHL_OK ) { |
|---|
| 456 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : DD_DEV_NFLASHErase()\n" ); |
|---|
| 457 | goto done; |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | done: |
|---|
| 461 | /* Give the MUTEX */ |
|---|
| 462 | |
|---|
| 463 | |
|---|
| 464 | return dhlResult; |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | int DHL_DEV_GetNFLASHSize(NFLASHID NFlashId) |
|---|
| 468 | { |
|---|
| 469 | P_DST_DEV_NFLASH pNFlash; |
|---|
| 470 | |
|---|
| 471 | pNFlash = &sNFLASH[NFlashId-1]; |
|---|
| 472 | |
|---|
| 473 | return pNFlash->uSize; |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | #if 0 |
|---|
| 477 | ___SFLASH_Functions__________() |
|---|
| 478 | #endif |
|---|
| 479 | |
|---|
| 480 | /**************************************************************************** |
|---|
| 481 | |
|---|
| 482 | DHL_RESULT DHL_DEV_SFLASHInit |
|---|
| 483 | - Summary : Initialize NAND flash device, including NAND flash bus. |
|---|
| 484 | - Argument |
|---|
| 485 | SFLASHID { SFLASH_0 } |
|---|
| 486 | Size - NAND Flash Size |
|---|
| 487 | Start - NAND Flash Usable Area Start Offset |
|---|
| 488 | End - NAND Flash Usable Area End Offset |
|---|
| 489 | if ( End == 0 ) End = Start + Size; |
|---|
| 490 | - Returns : DHL_OK on success, DHL_FAIL on failure. |
|---|
| 491 | |
|---|
| 492 | ***************************************************************************/ |
|---|
| 493 | DHL_RESULT DHL_DEV_InitSFLASH( SFLASHID SFLASHId, DS_U32 Size, DS_U32 Start, DS_U32 End ) |
|---|
| 494 | { |
|---|
| 495 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 496 | P_DST_DEV_SFLASH pSFLASH; |
|---|
| 497 | |
|---|
| 498 | if ( SFLASHId >= SFLASHID_MAX ) { |
|---|
| 499 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : SFLASH Id is invalid, Id = %d\n", SFLASHId ); |
|---|
| 500 | return DHL_FAIL_INVALID_HANDLE; |
|---|
| 501 | } |
|---|
| 502 | |
|---|
| 503 | pSFLASH = &sSFLASH[SFLASHId-1]; |
|---|
| 504 | #if 0 /* hwatk, 2004.06.30, Init¿¡¼´Â State °Ë»çÇÏÁö ¾ÊÀ½. */ |
|---|
| 505 | if ( pSFLASH->CurState != SFLASHSTATE_UNKNOWN ) { |
|---|
| 506 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : SFLASH state is invalid, State = %d\n", pSFLASH->CurState ); |
|---|
| 507 | return DHL_FAIL; |
|---|
| 508 | } |
|---|
| 509 | #endif |
|---|
| 510 | |
|---|
| 511 | if ( (Size <= 0) || ((Start > End) && End) || ( Start && End && (Start==End) ) ) { |
|---|
| 512 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : Invalid argument, Size=%lx, Start=%lx, End=%lx!\n", Size, Start, End ); |
|---|
| 513 | return DHL_FAIL_INVALID_PARAM; |
|---|
| 514 | } |
|---|
| 515 | |
|---|
| 516 | if ( End == 0 ) End = Start + Size; |
|---|
| 517 | dhlResult = DD_DEV_SFLASHInit( pSFLASH, Size, Start, End ); |
|---|
| 518 | if ( dhlResult != DHL_OK ) { |
|---|
| 519 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : DD_DEV_SFLASHInit()\n" ); |
|---|
| 520 | return dhlResult; |
|---|
| 521 | } |
|---|
| 522 | |
|---|
| 523 | pSFLASH->CurState = SFLASHSTATE_INIT; |
|---|
| 524 | |
|---|
| 525 | /* Initialize MUTEX */ |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | |
|---|
| 529 | return dhlResult; |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | /**************************************************************************** |
|---|
| 533 | |
|---|
| 534 | int DHL_DEV_SFLASHRead |
|---|
| 535 | - Summary : Read data from NAND flash. |
|---|
| 536 | - Argument |
|---|
| 537 | SFLASHID { SFLASH_0 } |
|---|
| 538 | Address |
|---|
| 539 | pBuffer |
|---|
| 540 | Len |
|---|
| 541 | - Returns : Bytes read on success, -1 on failure. |
|---|
| 542 | |
|---|
| 543 | ***************************************************************************/ |
|---|
| 544 | int DHL_DEV_ReadSFLASH( SFLASHID SFLASHId, DS_U32 Address, DS_U8 *pBuffer, DS_U32 Len ) |
|---|
| 545 | { |
|---|
| 546 | int RetVal; |
|---|
| 547 | P_DST_DEV_SFLASH pSFLASH; |
|---|
| 548 | |
|---|
| 549 | if ( SFLASHId >= SFLASHID_MAX ) { |
|---|
| 550 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : SFLASH Id is invalid, Id = %d\n", SFLASHId ); |
|---|
| 551 | return -1; |
|---|
| 552 | } |
|---|
| 553 | |
|---|
| 554 | /* Take the MUTEX, notes : MUTEX should be taken before state checking. */ |
|---|
| 555 | |
|---|
| 556 | |
|---|
| 557 | /* State Checking */ |
|---|
| 558 | pSFLASH = &sSFLASH[SFLASHId-1]; |
|---|
| 559 | if ( pSFLASH->CurState != SFLASHSTATE_START ) { |
|---|
| 560 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : SFLASH state is invalid, State = %d\n", pSFLASH->CurState ); |
|---|
| 561 | RetVal = -1; |
|---|
| 562 | goto done; |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | /* Argument Checking */ |
|---|
| 566 | if ( (pBuffer == (DS_U8)0) || (Len == 0) ) { |
|---|
| 567 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : Invalid Arguments, pBuffer = %lX, Len = %ld\n", |
|---|
| 568 | pBuffer, Len ); |
|---|
| 569 | RetVal = -1; |
|---|
| 570 | goto done; |
|---|
| 571 | } |
|---|
| 572 | |
|---|
| 573 | /* Checks that the Address - Len range is in usable area. */ |
|---|
| 574 | if ( ( (Address < pSFLASH->uAvlStart) || (Address > pSFLASH->uAvlEnd) ) || |
|---|
| 575 | ( ((Address+Len) < pSFLASH->uAvlStart) || ((Address+Len) > pSFLASH->uAvlEnd) ) || |
|---|
| 576 | ((Address+Len) > pSFLASH->uSize ) || (Len > pSFLASH->uSize) ) |
|---|
| 577 | { |
|---|
| 578 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : the area is not in usable area. Address = %x, Len = %x\n", Address, Len ); |
|---|
| 579 | RetVal = -1; |
|---|
| 580 | goto done; |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | /* Call the lower device driver */ |
|---|
| 584 | RetVal = DD_DEV_SFLASHRead( pSFLASH, Address, pBuffer, Len ); |
|---|
| 585 | if ( RetVal < 0 ) { |
|---|
| 586 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : DD_DEV_SFLASHRead()\n" ); |
|---|
| 587 | RetVal = -1; |
|---|
| 588 | goto done; |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | done: |
|---|
| 592 | /* Give the MUTEX */ |
|---|
| 593 | |
|---|
| 594 | |
|---|
| 595 | return RetVal; |
|---|
| 596 | } |
|---|
| 597 | |
|---|
| 598 | /**************************************************************************** |
|---|
| 599 | |
|---|
| 600 | int DHL_DEV_SFLASHWrite |
|---|
| 601 | - Summary : Write data on NAND flash. |
|---|
| 602 | - Argument |
|---|
| 603 | SFLASHID { SFLASH_0 } |
|---|
| 604 | Address |
|---|
| 605 | pBuffer |
|---|
| 606 | Len |
|---|
| 607 | - Returns : Bytes written on success, -1 on failure. |
|---|
| 608 | |
|---|
| 609 | ***************************************************************************/ |
|---|
| 610 | int DHL_DEV_WriteSFLASH( SFLASHID SFLASHId, DS_U32 Address, DS_U8 *pBuffer, DS_U32 Len ) |
|---|
| 611 | { |
|---|
| 612 | int RetVal; |
|---|
| 613 | P_DST_DEV_SFLASH pSFLASH; |
|---|
| 614 | |
|---|
| 615 | if ( SFLASHId >= SFLASHID_MAX ) { |
|---|
| 616 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : SFLASH Id is invalid, Id = %d\n", SFLASHId ); |
|---|
| 617 | return DHL_FAIL_INVALID_HANDLE; |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | /* Take the MUTEX, notes : MUTEX should be taken before state checking. */ |
|---|
| 621 | |
|---|
| 622 | |
|---|
| 623 | /* State Checking */ |
|---|
| 624 | pSFLASH = &sSFLASH[SFLASHId-1]; |
|---|
| 625 | if ( pSFLASH->CurState != SFLASHSTATE_START ) { |
|---|
| 626 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : SFLASH state is invalid, State = %d\n", pSFLASH->CurState ); |
|---|
| 627 | RetVal = -1; |
|---|
| 628 | goto done; |
|---|
| 629 | } |
|---|
| 630 | |
|---|
| 631 | /* Argument Checking */ |
|---|
| 632 | if ( (pBuffer == (DS_U8)0) || (Len == 0) ) { |
|---|
| 633 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : Invalid Arguments, pBuffer = %lX, Len = %ld\n", |
|---|
| 634 | pBuffer, Len ); |
|---|
| 635 | RetVal = -1; |
|---|
| 636 | goto done; |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | /* Checks that the Address - Len range is in usable area. */ |
|---|
| 640 | if ( ( (Address < pSFLASH->uAvlStart) || (Address > pSFLASH->uAvlEnd) ) || |
|---|
| 641 | ( ((Address+Len) < pSFLASH->uAvlStart) || ((Address+Len) > pSFLASH->uAvlEnd) ) || |
|---|
| 642 | ((Address+Len) > pSFLASH->uSize ) || (Len > pSFLASH->uSize) ) |
|---|
| 643 | { |
|---|
| 644 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : the area is not in usable area. Address = %x, Len = %x\n", Address, Len ); |
|---|
| 645 | RetVal = -1; |
|---|
| 646 | goto done; |
|---|
| 647 | } |
|---|
| 648 | |
|---|
| 649 | /* Call the lower device driver. */ |
|---|
| 650 | RetVal = DD_DEV_SFLASHWrite( pSFLASH, Address, pBuffer, Len ); |
|---|
| 651 | if ( RetVal < 0 ) { |
|---|
| 652 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : DD_DEV_SFLASHWrite()\n" ); |
|---|
| 653 | goto done; |
|---|
| 654 | } |
|---|
| 655 | |
|---|
| 656 | done: |
|---|
| 657 | /* Give the MUTEX */ |
|---|
| 658 | |
|---|
| 659 | |
|---|
| 660 | return RetVal; |
|---|
| 661 | } |
|---|
| 662 | |
|---|
| 663 | /**************************************************************************** |
|---|
| 664 | |
|---|
| 665 | DHL_RESULT DHL_DEV_SFLASHErase |
|---|
| 666 | - Summary : Erase NAND Flash. |
|---|
| 667 | - Argument |
|---|
| 668 | SFLASHID { SFLASH_0 } |
|---|
| 669 | Address |
|---|
| 670 | Len |
|---|
| 671 | - Returns : DHL_OK on success, DHL_FAIL on failure. |
|---|
| 672 | |
|---|
| 673 | ***************************************************************************/ |
|---|
| 674 | DHL_RESULT DHL_DEV_EraseSFLASH( SFLASHID SFLASHId, DS_U32 Address, DS_U32 Len ) |
|---|
| 675 | { |
|---|
| 676 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 677 | P_DST_DEV_SFLASH pSFLASH; |
|---|
| 678 | |
|---|
| 679 | if ( SFLASHId >= SFLASHID_MAX ) { |
|---|
| 680 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : SFLASH Id is invalid, Id = %d\n", SFLASHId ); |
|---|
| 681 | return DHL_FAIL_INVALID_HANDLE; |
|---|
| 682 | } |
|---|
| 683 | |
|---|
| 684 | /* Take the MUTEX, notes : MUTEX should be taken before state checking. */ |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | |
|---|
| 688 | /* State Checking */ |
|---|
| 689 | pSFLASH = &sSFLASH[SFLASHId-1]; |
|---|
| 690 | if ( pSFLASH->CurState != SFLASHSTATE_START ) { |
|---|
| 691 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : SFLASH state is invalid, State = %d\n", pSFLASH->CurState ); |
|---|
| 692 | return DHL_FAIL; |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | /* Argument Checking */ |
|---|
| 696 | if ( Len == 0 ) { |
|---|
| 697 | dhlResult = DHL_FAIL; |
|---|
| 698 | goto done; |
|---|
| 699 | } |
|---|
| 700 | |
|---|
| 701 | /* Checks that the Address - Len range is in usable area. */ |
|---|
| 702 | if ( ( (Address < pSFLASH->uAvlStart) || (Address > pSFLASH->uAvlEnd) ) || |
|---|
| 703 | ( ((Address+Len) < pSFLASH->uAvlStart) || ((Address+Len) > pSFLASH->uAvlEnd) ) || |
|---|
| 704 | ((Address+Len) > pSFLASH->uSize ) || (Len > pSFLASH->uSize) ) |
|---|
| 705 | { |
|---|
| 706 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : the area is not in usable area. Address = %x, Len = %x\n", Address, Len ); |
|---|
| 707 | dhlResult = DHL_FAIL; |
|---|
| 708 | goto done; |
|---|
| 709 | } |
|---|
| 710 | |
|---|
| 711 | /* Call the lower device driver */ |
|---|
| 712 | dhlResult = DD_DEV_SFLASHErase( pSFLASH, Address, Len ); |
|---|
| 713 | if ( dhlResult != DHL_OK ) { |
|---|
| 714 | DHL_DbgPrintf( 1, DHLDBG_DEV, "ERROR : DD_DEV_SFLASHErase()\n" ); |
|---|
| 715 | goto done; |
|---|
| 716 | } |
|---|
| 717 | |
|---|
| 718 | done: |
|---|
| 719 | /* Give the MUTEX */ |
|---|
| 720 | |
|---|
| 721 | |
|---|
| 722 | return dhlResult; |
|---|
| 723 | } |
|---|
| 724 | #if 0 |
|---|
| 725 | ___SMARTCARD_Functions__________() |
|---|
| 726 | #endif |
|---|
| 727 | |
|---|
| 728 | P_DHL_CARD_CALLBACK g_CardCallback = 0; |
|---|
| 729 | int delay = 0; |
|---|
| 730 | int timeout = 1000; |
|---|
| 731 | /**************************************************************************** |
|---|
| 732 | |
|---|
| 733 | DHL_RESULT DHL_DEV_InitCAS |
|---|
| 734 | - Summary : Init CAS Interface |
|---|
| 735 | - Argument |
|---|
| 736 | - Returns : DHL_OK on success, DHL_FAIL on failure. |
|---|
| 737 | |
|---|
| 738 | ***************************************************************************/ |
|---|
| 739 | DHL_RESULT DHL_DEV_InitSmartCardInterface(void *func) |
|---|
| 740 | { |
|---|
| 741 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 742 | g_CaMutex = OS_CreateMutex( "CA_Mutex" ); |
|---|
| 743 | dhlResult = DD_DEV_InitSmartCard(); |
|---|
| 744 | g_CardCallback = (P_DHL_CARD_CALLBACK)func; |
|---|
| 745 | return dhlResult; |
|---|
| 746 | } |
|---|
| 747 | /**************************************************************************** |
|---|
| 748 | |
|---|
| 749 | DHL_RESULT DHL_DEV_GetCARDStatus |
|---|
| 750 | - Summary : Get CARD Status |
|---|
| 751 | - Argument |
|---|
| 752 | Status : Pointer for Status Data |
|---|
| 753 | - Returns : DHL_OK on success, DHL_FAIL on failure. |
|---|
| 754 | |
|---|
| 755 | ***************************************************************************/ |
|---|
| 756 | DHL_RESULT DHL_DEV_GetCARDStatus(DHL_DEV_CARD_STATUS *Status) |
|---|
| 757 | { |
|---|
| 758 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 759 | |
|---|
| 760 | LOCK_CA_MUTEX(); |
|---|
| 761 | dhlResult = DD_DEV_GetCARDStatus(Status); |
|---|
| 762 | UNLOCK_CA_MUTEX(); |
|---|
| 763 | done: |
|---|
| 764 | return dhlResult; |
|---|
| 765 | } |
|---|
| 766 | |
|---|
| 767 | /**************************************************************************** |
|---|
| 768 | |
|---|
| 769 | DHL_RESULT DHL_DEV_WriteBlock |
|---|
| 770 | - Summary : Write Block Data to CARD |
|---|
| 771 | - Argument |
|---|
| 772 | data : command & data |
|---|
| 773 | format example - BCAS |
|---|
| 774 | data[0] : AD_NAD |
|---|
| 775 | data[1] : AD_PCB |
|---|
| 776 | data[2] : AD_LEN |
|---|
| 777 | data[3] : CLA - Actual Data Start to Writing |
|---|
| 778 | data[4] : INS |
|---|
| 779 | data[5] : P1 |
|---|
| 780 | data[5] : P2 |
|---|
| 781 | data[5] : LC |
|---|
| 782 | . |
|---|
| 783 | . |
|---|
| 784 | . |
|---|
| 785 | |
|---|
| 786 | - Returns : DHL_OK on success, DHL_FAIL on failure. |
|---|
| 787 | |
|---|
| 788 | ***************************************************************************/ |
|---|
| 789 | DS_S32 DHL_DEV_WriteCARDBlock( DS_U8 *data ) |
|---|
| 790 | { |
|---|
| 791 | return DD_DEV_WriteCARDBlock(data); |
|---|
| 792 | } |
|---|
| 793 | /**************************************************************************** |
|---|
| 794 | |
|---|
| 795 | DHL_RESULT DHL_DEV_ReadBlock |
|---|
| 796 | - Summary : Write Block Data to CARD |
|---|
| 797 | - Argument |
|---|
| 798 | data : command & data |
|---|
| 799 | format example - BCAS |
|---|
| 800 | data[0] : AD_NAD |
|---|
| 801 | data[1] : AD_PCB |
|---|
| 802 | data[2] : AD_LEN |
|---|
| 803 | |
|---|
| 804 | return |
|---|
| 805 | data[0] : CLA - Actual Data Start to read |
|---|
| 806 | data[1] : INS |
|---|
| 807 | data[2] : P1 |
|---|
| 808 | data[3] : P2 |
|---|
| 809 | data[4] : LC |
|---|
| 810 | . |
|---|
| 811 | . |
|---|
| 812 | . |
|---|
| 813 | |
|---|
| 814 | - Returns : DHL_OK on success, DHL_FAIL on failure. |
|---|
| 815 | |
|---|
| 816 | ***************************************************************************/ |
|---|
| 817 | DS_S32 DHL_DEV_ReadCARDBlock( DS_U8 PCB, DS_U8 *data ) |
|---|
| 818 | { |
|---|
| 819 | return DD_DEV_ReadCARDBlock(PCB,data); |
|---|
| 820 | } |
|---|
| 821 | |
|---|
| 822 | /**************************************************************************** |
|---|
| 823 | |
|---|
| 824 | DHL_RESULT DHL_DEV_SmartCARDTransaction |
|---|
| 825 | - Summary : 1 Transaction Function |
|---|
| 826 | - Argument |
|---|
| 827 | Status : Pointer for Status Data |
|---|
| 828 | - Returns : DHL_OK on success, DHL_FAIL on failure. |
|---|
| 829 | |
|---|
| 830 | ***************************************************************************/ |
|---|
| 831 | DHL_RESULT DHL_DEV_SmartCARDTransaction( DS_U8 *cmdBuf,DS_U32 CmdLen, DS_U8 *retBuf ) |
|---|
| 832 | { |
|---|
| 833 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 834 | DS_S32 ret = 0; |
|---|
| 835 | DS_U32 Time = 0; |
|---|
| 836 | DS_BOOL retry = DS_FALSE; |
|---|
| 837 | static DS_U32 c_pcb = 0; |
|---|
| 838 | LOCK_CA_MUTEX(); |
|---|
| 839 | do |
|---|
| 840 | { |
|---|
| 841 | cmdBuf[1] = (c_pcb % 2) ? S_SEQUENCE : N_SEQUENCE; // Prolog PCB byteÀÇ bit 6 |
|---|
| 842 | retry = 0; |
|---|
| 843 | ret = DHL_DEV_WriteCARDBlock(cmdBuf); |
|---|
| 844 | |
|---|
| 845 | if( delay ) // delay °ªÀÌ Á¤ÀÇµÈ °æ¿ì Command Write ÀÌÈÄ DELAY¸¦ ÁÖ°Ô µÇ¸ç, À̶§ timeoutÀÌ Àû¿ëµÈ´Ù. |
|---|
| 846 | { |
|---|
| 847 | while( Time < timeout ) |
|---|
| 848 | { |
|---|
| 849 | OS_mDelay(delay); |
|---|
| 850 | ret = DHL_DEV_ReadCARDBlock( cmdBuf[1], retBuf ); |
|---|
| 851 | if( dhlResult == DHL_OK ) |
|---|
| 852 | goto done; |
|---|
| 853 | |
|---|
| 854 | Time += delay; |
|---|
| 855 | } |
|---|
| 856 | dhlResult = DHL_FAIL; |
|---|
| 857 | goto done; |
|---|
| 858 | } |
|---|
| 859 | |
|---|
| 860 | ret = DHL_DEV_ReadCARDBlock( cmdBuf[1], retBuf ); |
|---|
| 861 | if( ret == -2) |
|---|
| 862 | retry = 1; |
|---|
| 863 | }while(retry); |
|---|
| 864 | done: |
|---|
| 865 | c_pcb++; |
|---|
| 866 | |
|---|
| 867 | if( ret != 0 ) |
|---|
| 868 | dhlResult = DHL_FAIL; |
|---|
| 869 | |
|---|
| 870 | UNLOCK_CA_MUTEX(); |
|---|
| 871 | return dhlResult; |
|---|
| 872 | } |
|---|
| 873 | |
|---|
| 874 | #if 0 |
|---|
| 875 | ___MULTI2_Functions__________() |
|---|
| 876 | #endif |
|---|
| 877 | |
|---|
| 878 | DST_CA_PID_FILTER g_PidFilter[MAX_FILTER_COUNT]; |
|---|
| 879 | |
|---|
| 880 | /**************************************************************************** |
|---|
| 881 | |
|---|
| 882 | DHL_RESULT DHL_DEV_InitMULTI2 |
|---|
| 883 | - Summary : Reset MULTI2, Clear |
|---|
| 884 | - Argument |
|---|
| 885 | - Returns : DHL_OK on success, DHL_FAIL on failure. |
|---|
| 886 | |
|---|
| 887 | ***************************************************************************/ |
|---|
| 888 | DHL_RESULT DHL_CA_InitMULTI2(DS_U8 *SystemKey, DS_U8 *CBC) |
|---|
| 889 | { |
|---|
| 890 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 891 | DS_U32 i = 0; |
|---|
| 892 | |
|---|
| 893 | for(i=0;i<MAX_FILTER_COUNT;i++) |
|---|
| 894 | { |
|---|
| 895 | g_PidFilter[i].Pid = 0; |
|---|
| 896 | g_PidFilter[i].bValid = DS_FALSE; |
|---|
| 897 | g_PidFilter[i].slot = 0; |
|---|
| 898 | } |
|---|
| 899 | |
|---|
| 900 | dhlResult = DD_CA_InitMULTI2(SystemKey,CBC); |
|---|
| 901 | |
|---|
| 902 | return dhlResult; |
|---|
| 903 | } |
|---|
| 904 | |
|---|
| 905 | /**************************************************************************** |
|---|
| 906 | |
|---|
| 907 | DHL_RESULT DHL_DEV_ResetMULTI2 |
|---|
| 908 | - Summary : Reset MULTI2, Clear |
|---|
| 909 | - Argument |
|---|
| 910 | - Returns : DHL_OK on success, DHL_FAIL on failure. |
|---|
| 911 | |
|---|
| 912 | ***************************************************************************/ |
|---|
| 913 | DHL_RESULT DHL_CA_ResetMULTI2(void) |
|---|
| 914 | { |
|---|
| 915 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 916 | DS_U32 i = 0; |
|---|
| 917 | |
|---|
| 918 | for(i=0;i<MAX_FILTER_COUNT;i++) |
|---|
| 919 | { |
|---|
| 920 | g_PidFilter[i].Pid = 0; |
|---|
| 921 | g_PidFilter[i].bValid = DS_FALSE; |
|---|
| 922 | g_PidFilter[i].slot = 0; |
|---|
| 923 | } |
|---|
| 924 | |
|---|
| 925 | dhlResult = DD_CA_ResetMULTI2(); |
|---|
| 926 | |
|---|
| 927 | return dhlResult; |
|---|
| 928 | } |
|---|
| 929 | |
|---|
| 930 | /**************************************************************************** |
|---|
| 931 | |
|---|
| 932 | DHL_RESULT DHL_DEV_SetMULTI2Key |
|---|
| 933 | - Summary : update multi2 key value |
|---|
| 934 | - Argument |
|---|
| 935 | - Returns : DHL_OK on success, DHL_FAIL on failure. |
|---|
| 936 | |
|---|
| 937 | ***************************************************************************/ |
|---|
| 938 | void DHL_CA_GetCurPid(DS_U16 *retBuf, DS_U8 *Cnt) |
|---|
| 939 | { |
|---|
| 940 | DS_U8 Length = 0; |
|---|
| 941 | DS_U8 i; |
|---|
| 942 | |
|---|
| 943 | for( i = 0 ; i < MAX_FILTER_COUNT ; i++ ) |
|---|
| 944 | { |
|---|
| 945 | if( g_PidFilter[i].bValid == DS_TRUE ) |
|---|
| 946 | { |
|---|
| 947 | *retBuf = g_PidFilter[i].Pid; |
|---|
| 948 | Length++; |
|---|
| 949 | retBuf++; |
|---|
| 950 | } |
|---|
| 951 | } |
|---|
| 952 | |
|---|
| 953 | *Cnt = Length; |
|---|
| 954 | } |
|---|
| 955 | |
|---|
| 956 | /**************************************************************************** |
|---|
| 957 | |
|---|
| 958 | DHL_RESULT DHL_DEV_SetMULTI2Key |
|---|
| 959 | - Summary : update multi2 key value |
|---|
| 960 | - Argument |
|---|
| 961 | - Returns : DHL_OK on success, DHL_FAIL on failure. |
|---|
| 962 | |
|---|
| 963 | ***************************************************************************/ |
|---|
| 964 | DHL_RESULT DHL_CA_SetMULTI2Key(DS_U16 Pid,DS_U8 *EvenData, DS_U8 *OddData,DS_BOOL bAll) |
|---|
| 965 | { |
|---|
| 966 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 967 | DS_U32 i = 0; |
|---|
| 968 | |
|---|
| 969 | if ( bAll ) // validÇÑ ¸ðµç slotÀ» setting ÇÑ´Ù. |
|---|
| 970 | { |
|---|
| 971 | for( i = 0 ; i < MAX_FILTER_COUNT ; i++ ) |
|---|
| 972 | { |
|---|
| 973 | if( g_PidFilter[i].bValid == DS_TRUE ) |
|---|
| 974 | { |
|---|
| 975 | dhlResult = DD_CA_SetMULTI2Key(g_PidFilter[i].slot,EvenData,OddData); |
|---|
| 976 | if( dhlResult != DHL_OK ) |
|---|
| 977 | return DHL_FAIL; |
|---|
| 978 | } |
|---|
| 979 | } |
|---|
| 980 | } |
|---|
| 981 | else |
|---|
| 982 | { |
|---|
| 983 | for( i = 0 ; i < MAX_FILTER_COUNT ; i++ ) |
|---|
| 984 | { |
|---|
| 985 | if( ( g_PidFilter[i].Pid == Pid ) && ( g_PidFilter[i].bValid == DS_TRUE ) ) |
|---|
| 986 | { |
|---|
| 987 | dhlResult = DD_CA_SetMULTI2Key(g_PidFilter[i].slot,EvenData,OddData); |
|---|
| 988 | return dhlResult; |
|---|
| 989 | } |
|---|
| 990 | } |
|---|
| 991 | } |
|---|
| 992 | |
|---|
| 993 | return DHL_FAIL; |
|---|
| 994 | } |
|---|
| 995 | /**************************************************************************** |
|---|
| 996 | |
|---|
| 997 | DHL_RESULT DHL_DEV_SetMULTI2Pid |
|---|
| 998 | - Summary : setup pid setting |
|---|
| 999 | - Argument |
|---|
| 1000 | - Returns : DHL_OK on success, DHL_FAIL on failure. |
|---|
| 1001 | |
|---|
| 1002 | ***************************************************************************/ |
|---|
| 1003 | |
|---|
| 1004 | DHL_RESULT DHL_CA_SetMULTI2Pid(DS_U16 Pid, DS_BOOL Enable) |
|---|
| 1005 | { |
|---|
| 1006 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 1007 | DS_U32 i = 0; |
|---|
| 1008 | DS_U32 g_Array = 0; |
|---|
| 1009 | |
|---|
| 1010 | for(i=0;i<MAX_FILTER_COUNT;i++) |
|---|
| 1011 | { |
|---|
| 1012 | if( ( g_PidFilter[i].Pid == Pid ) && ( g_PidFilter[i].bValid == DS_TRUE ) ) |
|---|
| 1013 | { |
|---|
| 1014 | g_Array = i; |
|---|
| 1015 | break; |
|---|
| 1016 | } |
|---|
| 1017 | } |
|---|
| 1018 | |
|---|
| 1019 | if( g_Array && Enable) // µ¿ÀÏ pid°¡ ÀÌ¹Ì Á¸ÀçÇϸç, PID Ãß°¡ ÀÎ °æ¿ì. |
|---|
| 1020 | { |
|---|
| 1021 | return DHL_OK; |
|---|
| 1022 | } |
|---|
| 1023 | |
|---|
| 1024 | if( !g_Array && Enable ) // µ¿ÀÏ pid°¡ Á¸ÀçÇÏÁö ¾ÊÀ¸¸ç Ãß°¡ÀÎ °æ¿ì. |
|---|
| 1025 | { |
|---|
| 1026 | for(i=0;i<MAX_FILTER_COUNT;i++) |
|---|
| 1027 | { |
|---|
| 1028 | if( g_PidFilter[i].bValid == DS_FALSE ) |
|---|
| 1029 | { |
|---|
| 1030 | dhlResult = DD_CA_SetMULTI2Pid(Pid,&g_PidFilter[i].slot,DS_TRUE); |
|---|
| 1031 | if( dhlResult == DHL_OK ) |
|---|
| 1032 | { |
|---|
| 1033 | g_PidFilter[i].bValid = DS_TRUE; |
|---|
| 1034 | g_PidFilter[i].Pid = Pid; |
|---|
| 1035 | } |
|---|
| 1036 | break; |
|---|
| 1037 | } |
|---|
| 1038 | } |
|---|
| 1039 | } |
|---|
| 1040 | |
|---|
| 1041 | if( g_Array && !Enable ) // pid Á¸Àç, Á¦°Å |
|---|
| 1042 | { |
|---|
| 1043 | dhlResult = DD_CA_SetMULTI2Pid(Pid,&g_PidFilter[g_Array].slot,DS_TRUE); |
|---|
| 1044 | if( dhlResult == DHL_OK ) |
|---|
| 1045 | { |
|---|
| 1046 | g_PidFilter[i].bValid = DS_FALSE; |
|---|
| 1047 | g_PidFilter[i].Pid = 0; |
|---|
| 1048 | } |
|---|
| 1049 | } |
|---|
| 1050 | |
|---|
| 1051 | return dhlResult; |
|---|
| 1052 | } |
|---|
| 1053 | |
|---|
| 1054 | #if 0 |
|---|
| 1055 | ___DEBUG_APIs___() |
|---|
| 1056 | #endif |
|---|
| 1057 | void set_eeprom(int addr, int value) |
|---|
| 1058 | { |
|---|
| 1059 | DS_U8 ch_buf[4]; |
|---|
| 1060 | |
|---|
| 1061 | printf("|%s| addr=0x%X, value=0x%08X\n", __FUNCTION__, addr, value); |
|---|
| 1062 | |
|---|
| 1063 | ch_buf[0] = value & 0xFF; |
|---|
| 1064 | ch_buf[1] = (value>>8) & 0xFF; |
|---|
| 1065 | ch_buf[2] = (value>>16) & 0xFF; |
|---|
| 1066 | ch_buf[3] = (value>>24) & 0xFF; |
|---|
| 1067 | |
|---|
| 1068 | DHL_DEV_WriteEEPROM(addr, 4, ch_buf); |
|---|
| 1069 | } |
|---|
| 1070 | |
|---|
| 1071 | int get_eeprom(int addr) |
|---|
| 1072 | { |
|---|
| 1073 | DS_U8 ch_buf[4]; |
|---|
| 1074 | int value; |
|---|
| 1075 | |
|---|
| 1076 | DHL_DEV_ReadEEPROM(addr, 4, ch_buf); |
|---|
| 1077 | value = ch_buf[0]; |
|---|
| 1078 | value += ch_buf[1]<<8; |
|---|
| 1079 | value += ch_buf[2]<<16; |
|---|
| 1080 | value += ch_buf[3]<<24; |
|---|
| 1081 | |
|---|
| 1082 | printf("|%s| addr=0x%X, value=0x%08X\n", __FUNCTION__, addr, value); |
|---|
| 1083 | |
|---|
| 1084 | return value; |
|---|
| 1085 | } |
|---|
| 1086 | |
|---|
| 1087 | void test_eeprom(int addr, int len) |
|---|
| 1088 | { |
|---|
| 1089 | int n; |
|---|
| 1090 | |
|---|
| 1091 | for(n=addr; n<len; n+=4) { |
|---|
| 1092 | set_eeprom(n, 0x12345678); |
|---|
| 1093 | if ( get_eeprom(n) != 0x12345678 ) { |
|---|
| 1094 | printf("addr=0x%X, expected:0x12345678, read:0x%08X\n", n, get_eeprom(n)); |
|---|
| 1095 | break; |
|---|
| 1096 | } |
|---|
| 1097 | } |
|---|
| 1098 | } |
|---|