]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/bcm/nvm.c
beceem: remove dead code
[mv-sheeva.git] / drivers / staging / bcm / nvm.c
1 #include "headers.h"
2
3 #define DWORD unsigned int
4 // Procedure:   ReadEEPROMStatusRegister
5 //
6 // Description: Reads the standard EEPROM Status Register.
7 //
8 // Arguments:
9 //              Adapter    - ptr to Adapter object instance
10 // Returns:
11 //              OSAL_STATUS_CODE
12 //
13 //-----------------------------------------------------------------------------
14
15 static UCHAR ReadEEPROMStatusRegister( PMINI_ADAPTER Adapter )
16 {
17         UCHAR uiData = 0;
18         DWORD dwRetries = MAX_EEPROM_RETRIES*RETRIES_PER_DELAY;
19         UINT uiStatus = 0;
20         UINT value = 0;
21         UINT value1 = 0;
22
23         /* Read the EEPROM status register */
24         value = EEPROM_READ_STATUS_REGISTER ;
25         wrmalt( Adapter, EEPROM_CMDQ_SPI_REG, &value, sizeof(value));
26
27         while ( dwRetries != 0 )
28         {
29                 value=0;
30                 uiStatus = 0 ;
31                 rdmalt( Adapter, EEPROM_SPI_Q_STATUS1_REG,&uiStatus, sizeof(uiStatus));
32                 if(Adapter->device_removed == TRUE)
33                 {
34                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Modem has got removed hence exiting....");
35                         break;
36                 }
37
38                 /* Wait for Avail bit to be set. */
39                 if ( ( uiStatus & EEPROM_READ_DATA_AVAIL) != 0 )
40                 {
41                         /* Clear the Avail/Full bits - which ever is set. */
42                         value = uiStatus & (EEPROM_READ_DATA_AVAIL | EEPROM_READ_DATA_FULL);
43                         wrmalt( Adapter, EEPROM_SPI_Q_STATUS1_REG, &value, sizeof(value));
44
45                         value =0;
46                         rdmalt(Adapter, EEPROM_READ_DATAQ_REG,&value, sizeof(value));
47                         uiData = (UCHAR)value;
48
49                         break;
50                 }
51
52                 dwRetries-- ;
53                 if ( dwRetries == 0 )
54                 {
55                          rdmalt(Adapter, EEPROM_SPI_Q_STATUS1_REG,&value, sizeof(value));
56                          rdmalt(Adapter, EEPROM_SPI_Q_STATUS_REG,&value1, sizeof(value1));
57                          BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"0x3004 = %x 0x3008 = %x, retries = %d failed.\n",value,value1,  MAX_EEPROM_RETRIES*RETRIES_PER_DELAY);
58                         return uiData;
59                 }
60                 if( !(dwRetries%RETRIES_PER_DELAY) )
61                         msleep(1);
62                 uiStatus = 0 ;
63         }
64         return uiData;
65 } /* ReadEEPROMStatusRegister */
66
67 //-----------------------------------------------------------------------------
68 // Procedure:   ReadBeceemEEPROMBulk
69 //
70 // Description: This routine reads 16Byte data from EEPROM
71 //
72 // Arguments:
73 //              Adapter    - ptr to Adapter object instance
74 //      dwAddress   - EEPROM Offset to read the data from.
75 //      pdwData     - Pointer to double word where data needs to be stored in.  //              dwNumWords  - Number of words.  Valid values are 4 ONLY.
76 //
77 // Returns:
78 //              OSAL_STATUS_CODE:
79 //-----------------------------------------------------------------------------
80
81 INT ReadBeceemEEPROMBulk( PMINI_ADAPTER Adapter,
82                                                                            DWORD dwAddress,
83                                                                            DWORD *pdwData,
84                                                                            DWORD dwNumWords
85                                                                          )
86 {
87         DWORD dwIndex = 0;
88         DWORD dwRetries = MAX_EEPROM_RETRIES*RETRIES_PER_DELAY;
89         UINT uiStatus  = 0;
90         UINT value= 0;
91         UINT value1 = 0;
92         UCHAR *pvalue;
93
94         /* Flush the read and cmd queue. */
95         value=( EEPROM_READ_QUEUE_FLUSH | EEPROM_CMD_QUEUE_FLUSH );
96         wrmalt( Adapter, SPI_FLUSH_REG, &value, sizeof(value) );
97         value=0;
98         wrmalt( Adapter, SPI_FLUSH_REG, &value, sizeof(value));
99
100         /* Clear the Avail/Full bits. */
101         value=( EEPROM_READ_DATA_AVAIL | EEPROM_READ_DATA_FULL );
102         wrmalt( Adapter, EEPROM_SPI_Q_STATUS1_REG,&value, sizeof(value));
103
104         value= dwAddress | ( (dwNumWords == 4) ? EEPROM_16_BYTE_PAGE_READ : EEPROM_4_BYTE_PAGE_READ );
105         wrmalt( Adapter, EEPROM_CMDQ_SPI_REG, &value, sizeof(value));
106
107         while ( dwRetries != 0 )
108                 {
109
110                 uiStatus = 0;
111                 rdmalt( Adapter, EEPROM_SPI_Q_STATUS1_REG, &uiStatus, sizeof(uiStatus));
112                 if(Adapter->device_removed == TRUE)
113                 {
114                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Modem has got Removed.hence exiting from loop...");
115                         return -ENODEV;
116                 }
117
118                 /* If we are reading 16 bytes we want to be sure that the queue
119                  * is full before we read.  In the other cases we are ok if the
120                  * queue has data available */
121                 if ( dwNumWords == 4 )
122                 {
123                         if ( ( uiStatus & EEPROM_READ_DATA_FULL ) != 0 )
124                         {
125                                 /* Clear the Avail/Full bits - which ever is set. */
126                                 value = ( uiStatus & (EEPROM_READ_DATA_AVAIL | EEPROM_READ_DATA_FULL) ) ;
127                                 wrmalt( Adapter, EEPROM_SPI_Q_STATUS1_REG,&value, sizeof(value));
128                                 break;
129                         }
130                 }
131                 else if ( dwNumWords == 1 )
132                 {
133
134                         if ( ( uiStatus & EEPROM_READ_DATA_AVAIL ) != 0 )
135                         {
136                                 /* We just got Avail and we have to read 32bits so we
137                                  * need this sleep for Cardbus kind of devices. */
138                                 if (Adapter->chip_id == 0xBECE0210 )
139                                                 udelay(800);
140
141                                 /* Clear the Avail/Full bits - which ever is set. */
142                                 value=( uiStatus & (EEPROM_READ_DATA_AVAIL | EEPROM_READ_DATA_FULL) );
143                                 wrmalt( Adapter, EEPROM_SPI_Q_STATUS1_REG,&value, sizeof(value));
144                                 break;
145                         }
146                 }
147
148                 uiStatus = 0;
149
150                 dwRetries--;
151                 if(dwRetries == 0)
152                 {
153                         value=0;
154                         value1=0;
155                         rdmalt(Adapter, EEPROM_SPI_Q_STATUS1_REG,&value, sizeof(value));
156                         rdmalt(Adapter, EEPROM_SPI_Q_STATUS_REG,&value1, sizeof(value1));
157                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "dwNumWords %d 0x3004 = %x 0x3008 = %x  retries = %d failed.\n", dwNumWords, value,  value1,  MAX_EEPROM_RETRIES*RETRIES_PER_DELAY);
158                         return STATUS_FAILURE;
159                 }
160                 if( !(dwRetries%RETRIES_PER_DELAY) )
161                         msleep(1);
162         }
163
164         for ( dwIndex = 0; dwIndex < dwNumWords ; dwIndex++ )
165         {
166                 /* We get only a byte at a time - from LSB to MSB. We shift it into an integer. */
167                 pvalue = (PUCHAR)(pdwData + dwIndex);
168
169                 value =0;
170                 rdmalt(Adapter, EEPROM_READ_DATAQ_REG,&value, sizeof(value));
171
172                 pvalue[0] = value;
173
174                 value = 0;
175                 rdmalt(Adapter, EEPROM_READ_DATAQ_REG,&value, sizeof(value));
176
177                 pvalue[1] = value;
178
179                 value =0;
180                 rdmalt(Adapter, EEPROM_READ_DATAQ_REG,&value, sizeof(value));
181
182                 pvalue[2] = value;
183
184                 value = 0;
185                 rdmalt(Adapter, EEPROM_READ_DATAQ_REG,&value, sizeof(value));
186
187                 pvalue[3] = value;
188         }
189
190         return STATUS_SUCCESS;
191 } /* ReadBeceemEEPROMBulk() */
192
193 //-----------------------------------------------------------------------------
194 // Procedure:   ReadBeceemEEPROM
195 //
196 // Description: This routine reads 4 data from EEPROM.  It uses 1 or 2 page
197 //                              reads to do this operation.
198 //
199 // Arguments:
200 //              Adapter     - ptr to Adapter object instance
201 //      uiOffset        - EEPROM Offset to read the data from.
202 //      pBuffer         - Pointer to word where data needs to be stored in.
203 //
204 // Returns:
205 //              OSAL_STATUS_CODE:
206 //-----------------------------------------------------------------------------
207
208 INT ReadBeceemEEPROM( PMINI_ADAPTER Adapter,
209                                                                    DWORD uiOffset,
210                                                                    DWORD *pBuffer
211                                                                  )
212 {
213         UINT uiData[8]          = {0};
214         UINT uiByteOffset       = 0;
215         UINT uiTempOffset       = 0;
216
217         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL," ====> ");
218
219         uiTempOffset = uiOffset - (uiOffset % MAX_RW_SIZE);
220         uiByteOffset = uiOffset - uiTempOffset;
221
222         ReadBeceemEEPROMBulk(Adapter, uiTempOffset, (PUINT)&uiData[0], 4);
223
224         /* A word can overlap at most over 2 pages. In that case we read the
225          * next page too. */
226         if ( uiByteOffset > 12 )
227         {
228                 ReadBeceemEEPROMBulk(Adapter, uiTempOffset + MAX_RW_SIZE, (PUINT)&uiData[4], 4);
229         }
230
231         memcpy( (PUCHAR) pBuffer, ( ((PUCHAR)&uiData[0]) + uiByteOffset ), 4);
232
233         return STATUS_SUCCESS;
234 } /* ReadBeceemEEPROM() */
235
236
237
238 INT ReadMacAddressFromNVM(PMINI_ADAPTER Adapter)
239 {
240         INT Status=0, i;
241         unsigned char puMacAddr[6] = {0};
242         INT AllZeroMac = 0;
243         INT AllFFMac = 0;
244
245         Status = BeceemNVMRead(Adapter,
246                         (PUINT)&puMacAddr[0],
247                         INIT_PARAMS_1_MACADDRESS_ADDRESS,
248                         MAC_ADDRESS_SIZE);
249
250         if(Status != STATUS_SUCCESS)
251         {
252                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Error in Reading the mac Addres with status :%d", Status);
253                 return Status;
254         }
255
256         memcpy(Adapter->dev->dev_addr, puMacAddr, MAC_ADDRESS_SIZE);
257         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Modem MAC Addr :");
258     BCM_DEBUG_PRINT_BUFFER(Adapter,DBG_TYPE_PRINTK, 0, DBG_LVL_ALL,&Adapter->dev->dev_addr[0],MAC_ADDRESS_SIZE);
259         for(i=0;i<MAC_ADDRESS_SIZE;i++)
260         {
261
262                 if(Adapter->dev->dev_addr[i] == 0x00)
263                         AllZeroMac++;
264                 if(Adapter->dev->dev_addr[i] == 0xFF)
265                         AllFFMac++;
266
267         }
268         //BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "\n");
269         if(AllZeroMac == MAC_ADDRESS_SIZE)
270                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Warning :: MAC Address has all 00's");
271         if(AllFFMac == MAC_ADDRESS_SIZE)
272                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Warning :: MAC Address has all FF's");
273
274         return Status;
275
276 }
277
278 //-----------------------------------------------------------------------------
279 // Procedure:   BeceemEEPROMBulkRead
280 //
281 // Description: Reads the EEPROM and returns the Data.
282 //
283 // Arguments:
284 //              Adapter    - ptr to Adapter object instance
285 //              pBuffer    - Buffer to store the data read from EEPROM
286 //              uiOffset   - Offset of EEPROM from where data should be read
287 //              uiNumBytes - Number of bytes to be read from the EEPROM.
288 //
289 // Returns:
290 //              OSAL_STATUS_SUCCESS - if EEPROM read is successfull.
291 //              <FAILURE>                       - if failed.
292 //-----------------------------------------------------------------------------
293
294 INT BeceemEEPROMBulkRead(
295         PMINI_ADAPTER Adapter,
296         PUINT pBuffer,
297         UINT uiOffset,
298         UINT uiNumBytes)
299 {
300         UINT uiData[4]            = {0};
301         //UINT uiAddress                  = 0;
302         UINT uiBytesRemaining = uiNumBytes;
303         UINT uiIndex              = 0;
304         UINT uiTempOffset         = 0;
305         UINT uiExtraBytes     = 0;
306         UINT uiFailureRetries = 0;
307         PUCHAR pcBuff = (PUCHAR)pBuffer;
308
309
310         if(uiOffset%MAX_RW_SIZE&& uiBytesRemaining)
311         {
312                 uiTempOffset = uiOffset - (uiOffset%MAX_RW_SIZE);
313                 uiExtraBytes = uiOffset-uiTempOffset;
314                 ReadBeceemEEPROMBulk(Adapter,uiTempOffset,(PUINT)&uiData[0],4);
315                 if(uiBytesRemaining >= (MAX_RW_SIZE - uiExtraBytes))
316                 {
317                         memcpy(pBuffer,(((PUCHAR)&uiData[0])+uiExtraBytes),MAX_RW_SIZE - uiExtraBytes);
318
319                         uiBytesRemaining -= (MAX_RW_SIZE - uiExtraBytes);
320                         uiIndex += (MAX_RW_SIZE - uiExtraBytes);
321                         uiOffset += (MAX_RW_SIZE - uiExtraBytes);
322                 }
323                 else
324                 {
325                         memcpy(pBuffer,(((PUCHAR)&uiData[0])+uiExtraBytes),uiBytesRemaining);
326                         uiIndex += uiBytesRemaining;
327                         uiOffset += uiBytesRemaining;
328                         uiBytesRemaining = 0;
329                 }
330
331
332         }
333
334
335         while(uiBytesRemaining && uiFailureRetries != 128)
336         {
337                 if(Adapter->device_removed )
338                 {
339                         return -1;
340                 }
341
342                 if(uiBytesRemaining >= MAX_RW_SIZE)
343                 {
344                         /* For the requests more than or equal to 16 bytes, use bulk
345                          * read function to make the access faster.
346                          * We read 4 Dwords of data */
347                         if(0 == ReadBeceemEEPROMBulk(Adapter,uiOffset,&uiData[0],4))
348                         {
349                                 memcpy(pcBuff+uiIndex,&uiData[0],MAX_RW_SIZE);
350                                 uiOffset += MAX_RW_SIZE;
351                                 uiBytesRemaining -= MAX_RW_SIZE;
352                                 uiIndex += MAX_RW_SIZE;
353                         }
354                         else
355                         {
356                                 uiFailureRetries++;
357                                 mdelay(3);//sleep for a while before retry...
358                         }
359                 }
360                 else if(uiBytesRemaining >= 4)
361                 {
362                         if(0 == ReadBeceemEEPROM(Adapter,uiOffset,&uiData[0]))
363                         {
364                                 memcpy(pcBuff+uiIndex,&uiData[0],4);
365                                 uiOffset += 4;
366                                 uiBytesRemaining -= 4;
367                                 uiIndex +=4;
368                         }
369                         else
370                         {
371                                 uiFailureRetries++;
372                                 mdelay(3);//sleep for a while before retry...
373                         }
374                 }
375                 else
376                 { // Handle the reads less than 4 bytes...
377                         PUCHAR pCharBuff = (PUCHAR)pBuffer;
378                         pCharBuff += uiIndex;
379                         if(0 == ReadBeceemEEPROM(Adapter,uiOffset,&uiData[0]))
380                         {
381                                 memcpy(pCharBuff,&uiData[0],uiBytesRemaining);//copy only bytes requested.
382                                 uiBytesRemaining = 0;
383                         }
384                         else
385                         {
386                                 uiFailureRetries++;
387                                 mdelay(3);//sleep for a while before retry...
388                         }
389                 }
390
391         }
392
393         return 0;
394 }
395
396 //-----------------------------------------------------------------------------
397 // Procedure:   BeceemFlashBulkRead
398 //
399 // Description: Reads the FLASH and returns the Data.
400 //
401 // Arguments:
402 //              Adapter    - ptr to Adapter object instance
403 //              pBuffer    - Buffer to store the data read from FLASH
404 //              uiOffset   - Offset of FLASH from where data should be read
405 //              uiNumBytes - Number of bytes to be read from the FLASH.
406 //
407 // Returns:
408 //              OSAL_STATUS_SUCCESS - if FLASH read is successfull.
409 //              <FAILURE>                       - if failed.
410 //-----------------------------------------------------------------------------
411
412 INT BeceemFlashBulkRead(
413         PMINI_ADAPTER Adapter,
414         PUINT pBuffer,
415         UINT uiOffset,
416         UINT uiNumBytes)
417 {
418         UINT uiIndex = 0;
419         UINT uiBytesToRead = uiNumBytes;
420         INT Status = 0;
421         UINT uiPartOffset = 0;
422
423         if(Adapter->device_removed )
424         {
425                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Device Got Removed ");
426                 return -ENODEV;
427         }
428
429         //Adding flash Base address
430 //      uiOffset = uiOffset + GetFlashBaseAddr(Adapter);
431 #if defined(BCM_SHM_INTERFACE) && !defined(FLASH_DIRECT_ACCESS)
432   Status = bcmflash_raw_read((uiOffset/FLASH_PART_SIZE),(uiOffset % FLASH_PART_SIZE),( unsigned char *)pBuffer,uiNumBytes);
433   return Status;
434 #endif
435
436         Adapter->SelectedChip = RESET_CHIP_SELECT;
437
438         if(uiOffset % MAX_RW_SIZE)
439         {
440                 BcmDoChipSelect(Adapter,uiOffset);
441                 uiPartOffset = (uiOffset & (FLASH_PART_SIZE - 1)) + GetFlashBaseAddr(Adapter);
442
443                 uiBytesToRead = MAX_RW_SIZE - (uiOffset%MAX_RW_SIZE);
444                 uiBytesToRead = MIN(uiNumBytes,uiBytesToRead);
445
446                 if(rdm(Adapter,uiPartOffset, (PCHAR)pBuffer+uiIndex,uiBytesToRead))
447                 {
448                         Status = -1;
449                         Adapter->SelectedChip = RESET_CHIP_SELECT;
450                         return Status;
451                 }
452
453                 uiIndex += uiBytesToRead;
454                 uiOffset += uiBytesToRead;
455                 uiNumBytes -= uiBytesToRead;
456         }
457
458         while(uiNumBytes)
459         {
460                 BcmDoChipSelect(Adapter,uiOffset);
461                 uiPartOffset = (uiOffset & (FLASH_PART_SIZE - 1)) + GetFlashBaseAddr(Adapter);
462
463                 uiBytesToRead = MIN(uiNumBytes,MAX_RW_SIZE);
464
465                 if(rdm(Adapter,uiPartOffset, (PCHAR)pBuffer+uiIndex,uiBytesToRead))
466                 {
467                         Status = -1;
468                         break;
469                 }
470
471
472                 uiIndex += uiBytesToRead;
473                 uiOffset += uiBytesToRead;
474                 uiNumBytes -= uiBytesToRead;
475
476         }
477         Adapter->SelectedChip = RESET_CHIP_SELECT;
478         return Status;
479 }
480
481 //-----------------------------------------------------------------------------
482 // Procedure:   BcmGetFlashSize
483 //
484 // Description: Finds the size of FLASH.
485 //
486 // Arguments:
487 //              Adapter    - ptr to Adapter object instance
488 //
489 // Returns:
490 //              UINT - size of the FLASH Storage.
491 //
492 //-----------------------------------------------------------------------------
493
494 UINT BcmGetFlashSize(PMINI_ADAPTER Adapter)
495 {
496         if(IsFlash2x(Adapter))
497                 return  (Adapter->psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader + sizeof(DSD_HEADER));
498         else
499                 return 32*1024;
500
501
502 }
503
504 //-----------------------------------------------------------------------------
505 // Procedure:   BcmGetEEPROMSize
506 //
507 // Description: Finds the size of EEPROM.
508 //
509 // Arguments:
510 //              Adapter    - ptr to Adapter object instance
511 //
512 // Returns:
513 //              UINT - size of the EEPROM Storage.
514 //
515 //-----------------------------------------------------------------------------
516
517 UINT BcmGetEEPROMSize(PMINI_ADAPTER Adapter)
518 {
519         UINT uiData = 0;
520         UINT uiIndex = 0;
521
522 //
523 // if EEPROM is present and already Calibrated,it will have
524 // 'BECM' string at 0th offset.
525 //      To find the EEPROM size read the possible boundaries of the
526 // EEPROM like 4K,8K etc..accessing the EEPROM beyond its size will
527 // result in wrap around. So when we get the End of the EEPROM we will
528 // get 'BECM' string which is indeed at offset 0.
529 //
530         BeceemEEPROMBulkRead(Adapter,&uiData,0x0,4);
531         if(uiData == BECM)
532         {
533                 for(uiIndex = 2;uiIndex <=256; uiIndex*=2)
534                 {
535                         BeceemEEPROMBulkRead(Adapter,&uiData,uiIndex*1024,4);
536                         if(uiData == BECM)
537                         {
538                                 return uiIndex*1024;
539                         }
540                 }
541         }
542         else
543         {
544 //
545 // EEPROM may not be present or not programmed
546 //
547
548         uiData = 0xBABEFACE;
549                 if(0 == BeceemEEPROMBulkWrite(Adapter,(PUCHAR)&uiData,0,4,TRUE))
550                 {
551                         uiData = 0;
552                         for(uiIndex = 2;uiIndex <=256; uiIndex*=2)
553                         {
554                                 BeceemEEPROMBulkRead(Adapter,&uiData,uiIndex*1024,4);
555                                 if(uiData == 0xBABEFACE)
556                                 {
557                                         return uiIndex*1024;
558                                 }
559                         }
560                 }
561
562         }
563         return 0;
564 }
565
566
567 //-----------------------------------------------------------------------------
568 // Procedure:   FlashSectorErase
569 //
570 // Description: Finds the sector size of the FLASH.
571 //
572 // Arguments:
573 //              Adapter    - ptr to Adapter object instance
574 //              addr       - sector start address
575 //              numOfSectors - number of sectors to  be erased.
576 //
577 // Returns:
578 //              OSAL_STATUS_CODE
579 //
580 //-----------------------------------------------------------------------------
581
582
583 static INT FlashSectorErase(PMINI_ADAPTER Adapter,
584         UINT addr,
585         UINT numOfSectors)
586 {
587         UINT iIndex = 0, iRetries = 0;
588         UINT uiStatus = 0;
589         UINT value;
590
591         for(iIndex=0;iIndex<numOfSectors;iIndex++)
592         {
593                 value = 0x06000000;
594                 wrmalt(Adapter, FLASH_SPI_CMDQ_REG, &value, sizeof(value));
595
596                 value = (0xd8000000 | (addr & 0xFFFFFF));
597                 wrmalt(Adapter, FLASH_SPI_CMDQ_REG, &value, sizeof(value));
598                 iRetries = 0;
599
600                 do
601                 {
602                         value = (FLASH_CMD_STATUS_REG_READ << 24);
603                         if(wrmalt(Adapter, FLASH_SPI_CMDQ_REG, &value, sizeof(value)) < 0)
604                         {
605                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Programing of FLASH_SPI_CMDQ_REG fails");
606                                 return STATUS_FAILURE;
607                         }
608
609                         if(rdmalt(Adapter, FLASH_SPI_READQ_REG, &uiStatus, sizeof(uiStatus)) < 0 )
610                         {
611                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Reading status of FLASH_SPI_READQ_REG fails");
612                                 return STATUS_FAILURE;
613                         }
614                         iRetries++;
615                         //After every try lets make the CPU free for 10 ms. generally time taken by the
616                         //the sector erase cycle is 500 ms to 40000 msec. hence sleeping 10 ms
617                         //won't hamper performance in any case.
618                         msleep(10);
619                 }while((uiStatus & 0x1) && (iRetries < 400));
620
621                 if(uiStatus & 0x1)
622                 {
623                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"iRetries crossing the limit of 80000\n");
624                         return STATUS_FAILURE;
625                 }
626
627                 addr += Adapter->uiSectorSize;
628         }
629         return 0;
630 }
631 //-----------------------------------------------------------------------------
632 // Procedure:   flashByteWrite
633 //
634 // Description: Performs Byte by Byte write to flash
635 //
636 // Arguments:
637 //              Adapter   - ptr to Adapter object instance
638 //              uiOffset   - Offset of the flash where data needs to be written to.
639 //              pData   - Address of Data to be written.
640 // Returns:
641 //              OSAL_STATUS_CODE
642 //
643 //-----------------------------------------------------------------------------
644
645 static INT flashByteWrite(
646         PMINI_ADAPTER Adapter,
647         UINT uiOffset,
648         PVOID pData)
649 {
650
651         UINT uiStatus = 0;
652         INT  iRetries = MAX_FLASH_RETRIES * FLASH_PER_RETRIES_DELAY; //3
653
654         UINT value;
655         ULONG ulData = *(PUCHAR)pData;
656
657 //
658 // need not write 0xFF because write requires an erase and erase will
659 // make whole sector 0xFF.
660 //
661
662         if(0xFF == ulData)
663         {
664                 return STATUS_SUCCESS;
665         }
666
667 //      DumpDebug(NVM_RW,("flashWrite ====>\n"));
668         value = (FLASH_CMD_WRITE_ENABLE << 24);
669         if(wrmalt(Adapter, FLASH_SPI_CMDQ_REG,&value, sizeof(value)) < 0)
670         {
671                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Write enable in FLASH_SPI_CMDQ_REG register fails");
672                 return STATUS_FAILURE;
673         }
674         if(wrm(Adapter,FLASH_SPI_WRITEQ_REG, (PCHAR)&ulData, 4) < 0 )
675         {
676                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"DATA Write on FLASH_SPI_WRITEQ_REG fails");
677                 return STATUS_FAILURE;
678         }
679         value = (0x02000000 | (uiOffset & 0xFFFFFF));
680         if(wrmalt(Adapter, FLASH_SPI_CMDQ_REG,&value, sizeof(value)) < 0 )
681         {
682                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Programming of FLASH_SPI_CMDQ_REG fails");
683                 return STATUS_FAILURE;
684         }
685
686         //__udelay(950);
687
688         do
689         {
690                 value = (FLASH_CMD_STATUS_REG_READ << 24);
691                 if(wrmalt(Adapter, FLASH_SPI_CMDQ_REG, &value, sizeof(value)) < 0)
692                 {
693                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Programing of FLASH_SPI_CMDQ_REG fails");
694                         return STATUS_FAILURE;
695                 }
696                 //__udelay(1);
697                 if(rdmalt(Adapter, FLASH_SPI_READQ_REG, &uiStatus, sizeof(uiStatus)) < 0)
698                 {
699                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Reading status of FLASH_SPI_READQ_REG fails");
700                         return STATUS_FAILURE;
701                 }
702                 iRetries--;
703                 if( iRetries && ((iRetries % FLASH_PER_RETRIES_DELAY) == 0))
704                          msleep(1);
705
706         }while((uiStatus & 0x1) && (iRetries  >0) );
707
708         if(uiStatus & 0x1)
709         {
710                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Flash Write fails even after checking status for 200 times.");
711                 return STATUS_FAILURE ;
712         }
713
714         return STATUS_SUCCESS;
715 }
716
717
718
719 //-----------------------------------------------------------------------------
720 // Procedure:   flashWrite
721 //
722 // Description: Performs write to flash
723 //
724 // Arguments:
725 //              Adapter    - ptr to Adapter object instance
726 //              uiOffset   - Offset of the flash where data needs to be written to.
727 //              pData   - Address of Data to be written.
728 // Returns:
729 //              OSAL_STATUS_CODE
730 //
731 //-----------------------------------------------------------------------------
732
733 static INT flashWrite(
734         PMINI_ADAPTER Adapter,
735         UINT uiOffset,
736         PVOID pData)
737
738 {
739         //UINT uiStatus = 0;
740         //INT  iRetries = 0;
741         //UINT uiReadBack = 0;
742
743         UINT uiStatus = 0;
744         INT  iRetries = MAX_FLASH_RETRIES * FLASH_PER_RETRIES_DELAY; //3
745
746         UINT value;
747         UINT uiErasePattern[4] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF};
748 //
749 // need not write 0xFFFFFFFF because write requires an erase and erase will
750 // make whole sector 0xFFFFFFFF.
751 //
752         if (!memcmp(pData, uiErasePattern, MAX_RW_SIZE))
753         {
754                 return 0;
755         }
756
757         value = (FLASH_CMD_WRITE_ENABLE << 24);
758
759         if(wrmalt(Adapter, FLASH_SPI_CMDQ_REG,&value, sizeof(value)) < 0 )
760         {
761                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Write Enable of FLASH_SPI_CMDQ_REG fails");
762                 return STATUS_FAILURE;
763         }
764         if(wrm(Adapter, uiOffset, (PCHAR)pData, MAX_RW_SIZE) < 0)
765         {
766                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Data write fails...");
767                 return STATUS_FAILURE;
768         }
769
770         //__udelay(950);
771         do
772         {
773                 value = (FLASH_CMD_STATUS_REG_READ << 24);
774                 if(wrmalt(Adapter, FLASH_SPI_CMDQ_REG, &value, sizeof(value)) < 0)
775                 {
776                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Programing of FLASH_SPI_CMDQ_REG fails");
777                         return STATUS_FAILURE;
778                 }
779                 //__udelay(1);
780                 if(rdmalt(Adapter, FLASH_SPI_READQ_REG, &uiStatus, sizeof(uiStatus)) < 0 )
781                 {
782                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Reading status of FLASH_SPI_READQ_REG fails");
783                         return STATUS_FAILURE;
784                 }
785
786                 iRetries--;
787                 //this will ensure that in there will be no changes in the current path.
788                 //currently one rdm/wrm takes 125 us.
789                 //Hence  125 *2 * FLASH_PER_RETRIES_DELAY > 3 ms(worst case delay)
790                 //Hence current implementation cycle will intoduce no delay in current path
791                 if(iRetries && ((iRetries % FLASH_PER_RETRIES_DELAY) == 0))
792                                 msleep(1);
793         }while((uiStatus & 0x1) && (iRetries > 0));
794
795         if(uiStatus & 0x1)
796         {
797                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Flash Write fails even after checking status for 200 times.");
798                 return STATUS_FAILURE ;
799         }
800
801         return STATUS_SUCCESS;
802 }
803
804 //-----------------------------------------------------------------------------
805 // Procedure:   flashByteWriteStatus
806 //
807 // Description: Performs byte by byte write to flash with write done status check
808 //
809 // Arguments:
810 //              Adapter    - ptr to Adapter object instance
811 //              uiOffset    - Offset of the flash where data needs to be written to.
812 //              pData    - Address of the Data to be written.
813 // Returns:
814 //              OSAL_STATUS_CODE
815 //
816 //-----------------------------------------------------------------------------
817 static INT flashByteWriteStatus(
818         PMINI_ADAPTER Adapter,
819         UINT uiOffset,
820         PVOID pData)
821 {
822         UINT uiStatus = 0;
823         INT  iRetries = MAX_FLASH_RETRIES * FLASH_PER_RETRIES_DELAY; //3
824         ULONG ulData  = *(PUCHAR)pData;
825         UINT value;
826
827 //
828 // need not write 0xFFFFFFFF because write requires an erase and erase will
829 // make whole sector 0xFFFFFFFF.
830 //
831
832         if(0xFF == ulData)
833         {
834                 return STATUS_SUCCESS;
835         }
836
837         //      DumpDebug(NVM_RW,("flashWrite ====>\n"));
838
839         value = (FLASH_CMD_WRITE_ENABLE << 24);
840         if(wrmalt(Adapter, FLASH_SPI_CMDQ_REG,&value, sizeof(value)) < 0)
841         {
842                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Write enable in FLASH_SPI_CMDQ_REG register fails");
843                 return STATUS_SUCCESS;
844         }
845         if(wrm(Adapter,FLASH_SPI_WRITEQ_REG, (PCHAR)&ulData, 4) < 0)
846         {
847                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"DATA Write on FLASH_SPI_WRITEQ_REG fails");
848                 return STATUS_FAILURE;
849         }
850         value = (0x02000000 | (uiOffset & 0xFFFFFF));
851         if(wrmalt(Adapter, FLASH_SPI_CMDQ_REG,&value, sizeof(value)) < 0)
852         {
853                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Programming of FLASH_SPI_CMDQ_REG fails");
854                 return STATUS_FAILURE;
855         }
856
857     //msleep(1);
858
859         do
860         {
861                 value = (FLASH_CMD_STATUS_REG_READ << 24);
862                 if(wrmalt(Adapter, FLASH_SPI_CMDQ_REG, &value, sizeof(value)) < 0)
863                 {
864                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Programing of FLASH_SPI_CMDQ_REG fails");
865                         return STATUS_FAILURE;
866                 }
867                 //__udelay(1);
868                 if(rdmalt(Adapter, FLASH_SPI_READQ_REG, &uiStatus, sizeof(uiStatus)) < 0)
869                 {
870                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Reading status of FLASH_SPI_READQ_REG fails");
871                         return STATUS_FAILURE;
872                 }
873
874                 iRetries--;
875                 if( iRetries && ((iRetries % FLASH_PER_RETRIES_DELAY) == 0))
876                                 msleep(1);
877         }while((uiStatus & 0x1) && (iRetries > 0));
878
879         if(uiStatus & 0x1)
880         {
881                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Flash Write fails even after checking status for 200 times.");
882                 return STATUS_FAILURE ;
883         }
884
885         return STATUS_SUCCESS;
886
887 }
888 //-----------------------------------------------------------------------------
889 // Procedure:   flashWriteStatus
890 //
891 // Description: Performs write to flash with write done status check
892 //
893 // Arguments:
894 //              Adapter    - ptr to Adapter object instance
895 //              uiOffset    - Offset of the flash where data needs to be written to.
896 //              pData    - Address of the Data to be written.
897 // Returns:
898 //              OSAL_STATUS_CODE
899 //
900 //-----------------------------------------------------------------------------
901
902 static INT flashWriteStatus(
903         PMINI_ADAPTER Adapter,
904         UINT uiOffset,
905         PVOID pData)
906 {
907         UINT uiStatus = 0;
908         INT  iRetries = MAX_FLASH_RETRIES * FLASH_PER_RETRIES_DELAY; //3
909         //UINT uiReadBack = 0;
910         UINT value;
911         UINT uiErasePattern[4] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF};
912
913 //
914 // need not write 0xFFFFFFFF because write requires an erase and erase will
915 // make whole sector 0xFFFFFFFF.
916 //
917         if (!memcmp(pData,uiErasePattern,MAX_RW_SIZE))
918         {
919                 return 0;
920         }
921
922         value = (FLASH_CMD_WRITE_ENABLE << 24);
923         if(wrmalt(Adapter, FLASH_SPI_CMDQ_REG,&value, sizeof(value)) < 0)
924         {
925                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Write Enable of FLASH_SPI_CMDQ_REG fails");
926                 return STATUS_FAILURE;
927         }
928         if(wrm(Adapter, uiOffset, (PCHAR)pData, MAX_RW_SIZE) < 0)
929         {
930                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Data write fails...");
931                 return STATUS_FAILURE;
932         }
933    // __udelay(1);
934
935         do
936         {
937                 value = (FLASH_CMD_STATUS_REG_READ << 24);
938                 if(wrmalt(Adapter, FLASH_SPI_CMDQ_REG, &value, sizeof(value)) < 0)
939                 {
940                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Programing of FLASH_SPI_CMDQ_REG fails");
941                         return STATUS_FAILURE;
942                 }
943                 //__udelay(1);
944                 if(rdmalt(Adapter, FLASH_SPI_READQ_REG, &uiStatus, sizeof(uiStatus)) < 0)
945                 {
946                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Reading status of FLASH_SPI_READQ_REG fails");
947                         return STATUS_FAILURE;
948                 }
949                 iRetries--;
950                 //this will ensure that in there will be no changes in the current path.
951                 //currently one rdm/wrm takes 125 us.
952                 //Hence  125 *2  * FLASH_PER_RETRIES_DELAY  >3 ms(worst case delay)
953                 //Hence current implementation cycle will intoduce no delay in current path
954                 if(iRetries && ((iRetries % FLASH_PER_RETRIES_DELAY) == 0))
955                                 msleep(1);
956         }while((uiStatus & 0x1) && (iRetries >0));
957
958         if(uiStatus & 0x1)
959         {
960                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Flash Write fails even after checking status for 200 times.");
961                 return STATUS_FAILURE ;
962         }
963
964         return STATUS_SUCCESS;
965 }
966
967 //-----------------------------------------------------------------------------
968 // Procedure:   BcmRestoreBlockProtectStatus
969 //
970 // Description: Restores the original block protection status.
971 //
972 // Arguments:
973 //              Adapter    - ptr to Adapter object instance
974 //              ulWriteStatus   -Original status
975 // Returns:
976 //              <VOID>
977 //
978 //-----------------------------------------------------------------------------
979
980 static VOID BcmRestoreBlockProtectStatus(PMINI_ADAPTER Adapter,ULONG ulWriteStatus)
981 {
982         UINT value;
983         value = (FLASH_CMD_WRITE_ENABLE<< 24);
984         wrmalt(Adapter, FLASH_SPI_CMDQ_REG, &value, sizeof(value));
985
986         udelay(20);
987         value = (FLASH_CMD_STATUS_REG_WRITE<<24)|(ulWriteStatus << 16);
988         wrmalt(Adapter, FLASH_SPI_CMDQ_REG, &value, sizeof(value));
989         udelay(20);
990 }
991 //-----------------------------------------------------------------------------
992 // Procedure:   BcmFlashUnProtectBlock
993 //
994 // Description: UnProtects appropriate blocks for writing.
995 //
996 // Arguments:
997 //              Adapter    - ptr to Adapter object instance
998 //              uiOffset   - Offset of the flash where data needs to be written to. This should be Sector aligned.
999 // Returns:
1000 //              ULONG   - Status value before UnProtect.
1001 //
1002 //-----------------------------------------------------------------------------
1003 static ULONG BcmFlashUnProtectBlock(PMINI_ADAPTER Adapter,UINT uiOffset, UINT uiLength)
1004 {
1005         ULONG ulStatus      = 0;
1006         ULONG ulWriteStatus = 0;
1007         UINT value;
1008         uiOffset = uiOffset&0x000FFFFF;
1009
1010 //
1011 // Implemented only for 1MB Flash parts.
1012 //
1013         if(FLASH_PART_SST25VF080B == Adapter->ulFlashID)
1014         {
1015         //
1016         // Get Current BP status.
1017         //
1018                 value = (FLASH_CMD_STATUS_REG_READ << 24);
1019                 wrmalt(Adapter, FLASH_SPI_CMDQ_REG, &value, sizeof(value));
1020                 udelay(10);
1021         //
1022         // Read status will be WWXXYYZZ. We have to take only WW.
1023         //
1024                 rdmalt(Adapter, FLASH_SPI_READQ_REG, (PUINT)&ulStatus, sizeof(ulStatus));
1025                 ulStatus >>= 24;
1026                 ulWriteStatus = ulStatus;
1027
1028         //
1029         // Bits [5-2] give current block level protection status.
1030         // Bit5: BP3 - DONT CARE
1031         // BP2-BP0: 0 - NO PROTECTION, 1 - UPPER 1/16, 2 - UPPER 1/8, 3 - UPPER 1/4
1032         //                4 - UPPER 1/2. 5 to 7 - ALL BLOCKS
1033         //
1034
1035                 if(ulStatus)
1036                 {
1037                         if((uiOffset+uiLength) <= 0x80000)
1038                         {
1039                         //
1040                         // Offset comes in lower half of 1MB. Protect the upper half.
1041                         // Clear BP1 and BP0 and set BP2.
1042                         //
1043                                 ulWriteStatus |= (0x4<<2);
1044                                 ulWriteStatus &= ~(0x3<<2);
1045                         }
1046                         else if((uiOffset+uiLength) <= 0xC0000)
1047                         {
1048                         //
1049                         // Offset comes below Upper 1/4. Upper 1/4 can be protected.
1050                         //  Clear BP2 and set BP1 and BP0.
1051                         //
1052                                 ulWriteStatus |= (0x3<<2);
1053                                 ulWriteStatus &= ~(0x1<<4);
1054                         }
1055                         else if((uiOffset+uiLength) <= 0xE0000)
1056                     {
1057                     //
1058                     // Offset comes below Upper 1/8. Upper 1/8 can be protected.
1059                     // Clear BP2 and BP0  and set BP1
1060                     //
1061                         ulWriteStatus |= (0x1<<3);
1062                         ulWriteStatus &= ~(0x5<<2);
1063
1064                     }
1065                     else if((uiOffset+uiLength) <= 0xF0000)
1066                     {
1067                     //
1068                     // Offset comes below Upper 1/16. Only upper 1/16 can be protected.
1069                     // Set BP0 and Clear BP2,BP1.
1070                     //
1071                         ulWriteStatus |= (0x1<<2);
1072                         ulWriteStatus &= ~(0x3<<3);
1073                     }
1074                     else
1075                     {
1076                     //
1077                     // Unblock all.
1078                     // Clear BP2,BP1 and BP0.
1079                     //
1080                         ulWriteStatus &= ~(0x7<<2);
1081                     }
1082
1083                         value = (FLASH_CMD_WRITE_ENABLE<< 24);
1084                         wrmalt(Adapter, FLASH_SPI_CMDQ_REG, &value, sizeof(value));
1085                         udelay(20);
1086                         value = (FLASH_CMD_STATUS_REG_WRITE<<24)|(ulWriteStatus << 16);
1087                         wrmalt(Adapter, FLASH_SPI_CMDQ_REG, &value, sizeof(value));
1088                         udelay(20);
1089
1090                 }
1091
1092         }
1093         return ulStatus;
1094 }
1095 //-----------------------------------------------------------------------------
1096 // Procedure:   BeceemFlashBulkWrite
1097 //
1098 // Description: Performs write to the flash
1099 //
1100 // Arguments:
1101 //              Adapter    - ptr to Adapter object instance
1102 //              pBuffer         - Data to be written.
1103 //              uiOffset   - Offset of the flash where data needs to be written to.
1104 //              uiNumBytes - Number of bytes to be written.
1105 //              bVerify    - read verify flag.
1106 // Returns:
1107 //              OSAL_STATUS_CODE
1108 //
1109 //-----------------------------------------------------------------------------
1110
1111 INT BeceemFlashBulkWrite(
1112         PMINI_ADAPTER Adapter,
1113         PUINT pBuffer,
1114         UINT uiOffset,
1115         UINT uiNumBytes,
1116         BOOLEAN bVerify)
1117 {
1118         PCHAR  pTempBuff                        = NULL;
1119         PUCHAR pcBuffer             = (PUCHAR)pBuffer;
1120         UINT  uiIndex                           = 0;
1121         UINT  uiOffsetFromSectStart = 0;
1122         UINT  uiSectAlignAddr           = 0;
1123         UINT  uiCurrSectOffsetAddr      = 0;
1124         UINT  uiSectBoundary            = 0;
1125         UINT  uiNumSectTobeRead         = 0;
1126         UCHAR ucReadBk[16]              = {0};
1127         ULONG ulStatus              = 0;
1128         INT Status                                      = STATUS_SUCCESS;
1129         UINT uiTemp                             = 0;
1130         UINT index                                      = 0;
1131         UINT uiPartOffset                       = 0;
1132
1133 #if defined(BCM_SHM_INTERFACE) && !defined(FLASH_DIRECT_ACCESS)
1134   Status = bcmflash_raw_write((uiOffset/FLASH_PART_SIZE),(uiOffset % FLASH_PART_SIZE),( unsigned char *)pBuffer,uiNumBytes);
1135   return Status;
1136 #endif
1137
1138         uiOffsetFromSectStart   = uiOffset & ~(Adapter->uiSectorSize - 1);
1139
1140         //Adding flash Base address
1141 //      uiOffset = uiOffset + GetFlashBaseAddr(Adapter);
1142
1143         uiSectAlignAddr                 = uiOffset & ~(Adapter->uiSectorSize - 1);
1144         uiCurrSectOffsetAddr    = uiOffset & (Adapter->uiSectorSize - 1);
1145         uiSectBoundary                  = uiSectAlignAddr + Adapter->uiSectorSize;
1146
1147         pTempBuff = kmalloc(Adapter->uiSectorSize, GFP_KERNEL);
1148         if(NULL == pTempBuff)
1149                 goto BeceemFlashBulkWrite_EXIT;
1150 //
1151 // check if the data to be written is overlapped accross sectors
1152 //
1153         if(uiOffset+uiNumBytes < uiSectBoundary)
1154         {
1155                 uiNumSectTobeRead = 1;
1156         }
1157         else
1158         {
1159                 //      Number of sectors  = Last sector start address/First sector start address
1160                 uiNumSectTobeRead =  (uiCurrSectOffsetAddr+uiNumBytes)/Adapter->uiSectorSize;
1161                 if((uiCurrSectOffsetAddr+uiNumBytes)%Adapter->uiSectorSize)
1162                 {
1163                         uiNumSectTobeRead++;
1164                 }
1165         }
1166         //Check whether Requested sector is writable or not in case of flash2x write. But if  write call is
1167         // for DSD calibration, allow it without checking of sector permission
1168
1169         if(IsFlash2x(Adapter) && (Adapter->bAllDSDWriteAllow == FALSE))
1170         {
1171                 index = 0;
1172                 uiTemp = uiNumSectTobeRead ;
1173                 while(uiTemp)
1174                 {
1175                          if(IsOffsetWritable(Adapter, uiOffsetFromSectStart + index * Adapter->uiSectorSize ) == FALSE)
1176                          {
1177                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Sector Starting at offset <0X%X> is not writable",
1178                                                                                         (uiOffsetFromSectStart + index * Adapter->uiSectorSize));
1179                                 Status = SECTOR_IS_NOT_WRITABLE;
1180                                 goto BeceemFlashBulkWrite_EXIT;
1181                          }
1182                          uiTemp = uiTemp - 1;
1183                          index = index + 1 ;
1184                 }
1185         }
1186         Adapter->SelectedChip = RESET_CHIP_SELECT;
1187         while(uiNumSectTobeRead)
1188         {
1189                 //do_gettimeofday(&tv1);
1190                 //BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "\nTime In start of write :%ld ms\n",(tv1.tv_sec *1000 + tv1.tv_usec /1000));
1191                 uiPartOffset = (uiSectAlignAddr & (FLASH_PART_SIZE - 1)) + GetFlashBaseAddr(Adapter);
1192
1193                 BcmDoChipSelect(Adapter,uiSectAlignAddr);
1194
1195                 if(0 != BeceemFlashBulkRead(Adapter,
1196                                                 (PUINT)pTempBuff,
1197                                                 uiOffsetFromSectStart,
1198                                                 Adapter->uiSectorSize))
1199                 {
1200                         Status = -1;
1201                         goto BeceemFlashBulkWrite_EXIT;
1202                 }
1203
1204                 //do_gettimeofday(&tr);
1205                 //BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Total time taken by Read :%ld ms\n", (tr.tv_sec *1000 + tr.tv_usec/1000) - (tv1.tv_sec *1000 + tv1.tv_usec/1000));
1206
1207                 ulStatus = BcmFlashUnProtectBlock(Adapter,uiSectAlignAddr,Adapter->uiSectorSize);
1208
1209
1210                 if(uiNumSectTobeRead > 1)
1211                 {
1212
1213                         memcpy(&pTempBuff[uiCurrSectOffsetAddr],pcBuffer,uiSectBoundary-(uiSectAlignAddr+uiCurrSectOffsetAddr));
1214                         pcBuffer += ((uiSectBoundary-(uiSectAlignAddr+uiCurrSectOffsetAddr)));
1215                         uiNumBytes -= (uiSectBoundary-(uiSectAlignAddr+uiCurrSectOffsetAddr));
1216                 }
1217                 else
1218                 {
1219                                 memcpy(&pTempBuff[uiCurrSectOffsetAddr],pcBuffer,uiNumBytes);
1220                 }
1221
1222                 if(IsFlash2x(Adapter))
1223                 {
1224                         SaveHeaderIfPresent(Adapter,(PUCHAR)pTempBuff,uiOffsetFromSectStart);
1225                 }
1226
1227                 FlashSectorErase(Adapter,uiPartOffset,1);
1228                 //do_gettimeofday(&te);
1229                 //BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Total time taken by Erase :%ld ms\n", (te.tv_sec *1000 + te.tv_usec/1000) - (tr.tv_sec *1000 + tr.tv_usec/1000));
1230
1231                 for(uiIndex = 0; uiIndex < Adapter->uiSectorSize; uiIndex +=Adapter->ulFlashWriteSize)
1232                 {
1233                         if(Adapter->device_removed)
1234                         {
1235                                 Status = -1;
1236                                 goto BeceemFlashBulkWrite_EXIT;
1237                         }
1238                         if(STATUS_SUCCESS != (*Adapter->fpFlashWrite)(Adapter,uiPartOffset+uiIndex,(&pTempBuff[uiIndex])))
1239                         {
1240                                 Status = -1;
1241                                 goto BeceemFlashBulkWrite_EXIT;
1242                         }
1243                 }
1244
1245                 //do_gettimeofday(&tw);
1246                 //BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Total time taken in Write  to Flash :%ld ms\n", (tw.tv_sec *1000 + tw.tv_usec/1000) - (te.tv_sec *1000 + te.tv_usec/1000));
1247                 for(uiIndex = 0;uiIndex < Adapter->uiSectorSize;uiIndex += MAX_RW_SIZE)
1248                 {
1249                         if(STATUS_SUCCESS == BeceemFlashBulkRead(Adapter,(PUINT)ucReadBk,uiOffsetFromSectStart+uiIndex,MAX_RW_SIZE))
1250                         {
1251                                 if(Adapter->ulFlashWriteSize == 1)
1252                                 {
1253                                         UINT uiReadIndex = 0;
1254                                         for(uiReadIndex = 0; uiReadIndex < 16; uiReadIndex++)
1255                                         {
1256                                                 if(ucReadBk[uiReadIndex] != pTempBuff[uiIndex+uiReadIndex])
1257                                                 {
1258                                                         if(STATUS_SUCCESS != (*Adapter->fpFlashWriteWithStatusCheck)(Adapter,uiPartOffset+uiIndex+uiReadIndex,&pTempBuff[uiIndex+uiReadIndex]))
1259                                                         {
1260                                                                 Status = STATUS_FAILURE;
1261                                                                 goto BeceemFlashBulkWrite_EXIT;
1262                                                         }
1263                                                 }
1264                                         }
1265                                 }
1266                                 else
1267                                 {
1268                                         if(memcmp(ucReadBk,&pTempBuff[uiIndex],MAX_RW_SIZE))
1269                                         {
1270                                                 if(STATUS_SUCCESS != (*Adapter->fpFlashWriteWithStatusCheck)(Adapter,uiPartOffset+uiIndex,&pTempBuff[uiIndex]))
1271                                                 {
1272                                                         Status = STATUS_FAILURE;
1273                                                         goto BeceemFlashBulkWrite_EXIT;
1274                                                 }
1275                                         }
1276                                 }
1277                         }
1278                 }
1279                 //do_gettimeofday(&twv);
1280                 //BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Total time taken in Write  to Flash verification :%ld ms\n", (twv.tv_sec *1000 + twv.tv_usec/1000) - (tw.tv_sec *1000 + tw.tv_usec/1000));
1281
1282
1283                 if(ulStatus)
1284                 {
1285                         BcmRestoreBlockProtectStatus(Adapter,ulStatus);
1286                         ulStatus = 0;
1287                 }
1288
1289                 uiCurrSectOffsetAddr = 0;
1290                 uiSectAlignAddr = uiSectBoundary;
1291                 uiSectBoundary += Adapter->uiSectorSize;
1292                 uiOffsetFromSectStart += Adapter->uiSectorSize;
1293                 uiNumSectTobeRead--;
1294         }
1295         //do_gettimeofday(&tv2);
1296         //BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Time after Write :%ld ms\n",(tv2.tv_sec *1000 + tv2.tv_usec/1000));
1297         //BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Total time taken by in Write is :%ld ms\n", (tv2.tv_sec *1000 + tv2.tv_usec/1000) - (tv1.tv_sec *1000 + tv1.tv_usec/1000));
1298 //
1299 // Cleanup.
1300 //
1301 BeceemFlashBulkWrite_EXIT:
1302         if(ulStatus)
1303         {
1304                 BcmRestoreBlockProtectStatus(Adapter,ulStatus);
1305         }
1306         
1307         kfree(pTempBuff);
1308
1309         Adapter->SelectedChip = RESET_CHIP_SELECT;
1310         return Status;
1311 }
1312
1313
1314 //-----------------------------------------------------------------------------
1315 // Procedure:   BeceemFlashBulkWriteStatus
1316 //
1317 // Description: Writes to Flash. Checks the SPI status after each write.
1318 //
1319 // Arguments:
1320 //              Adapter    - ptr to Adapter object instance
1321 //              pBuffer         - Data to be written.
1322 //              uiOffset   - Offset of the flash where data needs to be written to.
1323 //              uiNumBytes - Number of bytes to be written.
1324 //              bVerify    - read verify flag.
1325 // Returns:
1326 //              OSAL_STATUS_CODE
1327 //
1328 //-----------------------------------------------------------------------------
1329
1330 static INT BeceemFlashBulkWriteStatus(
1331         PMINI_ADAPTER Adapter,
1332         PUINT pBuffer,
1333         UINT uiOffset,
1334         UINT uiNumBytes,
1335         BOOLEAN bVerify)
1336 {
1337         PCHAR  pTempBuff                        = NULL;
1338         PUCHAR pcBuffer             = (PUCHAR)pBuffer;
1339         UINT  uiIndex                           = 0;
1340         UINT  uiOffsetFromSectStart = 0;
1341         UINT  uiSectAlignAddr           = 0;
1342         UINT  uiCurrSectOffsetAddr      = 0;
1343         UINT  uiSectBoundary            = 0;
1344         UINT  uiNumSectTobeRead         = 0;
1345         UCHAR ucReadBk[16]                      = {0};
1346         ULONG ulStatus              = 0;
1347         UINT  Status                            = STATUS_SUCCESS;
1348         UINT uiTemp                             = 0;
1349         UINT index                                      = 0;
1350         UINT uiPartOffset                       = 0;
1351
1352         uiOffsetFromSectStart   = uiOffset & ~(Adapter->uiSectorSize - 1);
1353
1354         //uiOffset += Adapter->ulFlashCalStart;
1355         //Adding flash Base address
1356 //      uiOffset = uiOffset + GetFlashBaseAddr(Adapter);
1357
1358         uiSectAlignAddr                 = uiOffset & ~(Adapter->uiSectorSize - 1);
1359         uiCurrSectOffsetAddr    = uiOffset & (Adapter->uiSectorSize - 1);
1360         uiSectBoundary                  = uiSectAlignAddr + Adapter->uiSectorSize;
1361
1362         pTempBuff = kmalloc(Adapter->uiSectorSize, GFP_KERNEL);
1363         if(NULL == pTempBuff)
1364                 goto BeceemFlashBulkWriteStatus_EXIT;
1365
1366 //
1367 // check if the data to be written is overlapped accross sectors
1368 //
1369         if(uiOffset+uiNumBytes < uiSectBoundary)
1370         {
1371                 uiNumSectTobeRead = 1;
1372         }
1373         else
1374         {
1375 //      Number of sectors  = Last sector start address/First sector start address
1376                 uiNumSectTobeRead =  (uiCurrSectOffsetAddr+uiNumBytes)/Adapter->uiSectorSize;
1377                 if((uiCurrSectOffsetAddr+uiNumBytes)%Adapter->uiSectorSize)
1378                 {
1379                         uiNumSectTobeRead++;
1380                 }
1381         }
1382
1383         if(IsFlash2x(Adapter) && (Adapter->bAllDSDWriteAllow == FALSE))
1384         {
1385                 index = 0;
1386                 uiTemp = uiNumSectTobeRead ;
1387                 while(uiTemp)
1388                 {
1389                          if(IsOffsetWritable(Adapter,uiOffsetFromSectStart + index * Adapter->uiSectorSize ) == FALSE)
1390                          {
1391                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Sector Starting at offset <0X%x> is not writable",
1392                                                                                         (uiOffsetFromSectStart + index * Adapter->uiSectorSize));
1393                                 Status = SECTOR_IS_NOT_WRITABLE;
1394                                 goto BeceemFlashBulkWriteStatus_EXIT;
1395                          }
1396                          uiTemp = uiTemp - 1;
1397                          index = index + 1 ;
1398                 }
1399         }
1400
1401         Adapter->SelectedChip = RESET_CHIP_SELECT;
1402         while(uiNumSectTobeRead)
1403         {
1404                 uiPartOffset = (uiSectAlignAddr & (FLASH_PART_SIZE - 1)) + GetFlashBaseAddr(Adapter);
1405
1406                 BcmDoChipSelect(Adapter,uiSectAlignAddr);
1407                 if(0 != BeceemFlashBulkRead(Adapter,
1408                                                 (PUINT)pTempBuff,
1409                                                 uiOffsetFromSectStart,
1410                                                 Adapter->uiSectorSize))
1411                 {
1412                         Status = -1;
1413                         goto BeceemFlashBulkWriteStatus_EXIT;
1414                 }
1415
1416                 ulStatus = BcmFlashUnProtectBlock(Adapter,uiOffsetFromSectStart,Adapter->uiSectorSize);
1417
1418                 if(uiNumSectTobeRead > 1)
1419                 {
1420
1421                         memcpy(&pTempBuff[uiCurrSectOffsetAddr],pcBuffer,uiSectBoundary-(uiSectAlignAddr+uiCurrSectOffsetAddr));
1422                         pcBuffer += ((uiSectBoundary-(uiSectAlignAddr+uiCurrSectOffsetAddr)));
1423                         uiNumBytes -= (uiSectBoundary-(uiSectAlignAddr+uiCurrSectOffsetAddr));
1424                 }
1425                 else
1426                 {
1427                         memcpy(&pTempBuff[uiCurrSectOffsetAddr],pcBuffer,uiNumBytes);
1428                 }
1429
1430                 if(IsFlash2x(Adapter))
1431                 {
1432                         SaveHeaderIfPresent(Adapter,(PUCHAR)pTempBuff,uiOffsetFromSectStart);
1433                 }
1434
1435                 FlashSectorErase(Adapter,uiPartOffset,1);
1436
1437                 for(uiIndex = 0; uiIndex < Adapter->uiSectorSize; uiIndex +=Adapter->ulFlashWriteSize)
1438
1439                 {
1440                         if(Adapter->device_removed)
1441                         {
1442                                 Status = -1;
1443                                 goto BeceemFlashBulkWriteStatus_EXIT;
1444                         }
1445
1446                         if(STATUS_SUCCESS != (*Adapter->fpFlashWriteWithStatusCheck)(Adapter,uiPartOffset+uiIndex,&pTempBuff[uiIndex]))
1447                         {
1448                                 Status = -1;
1449                                 goto BeceemFlashBulkWriteStatus_EXIT;
1450                         }
1451                 }
1452
1453                 if(bVerify)
1454                 {
1455                         for(uiIndex = 0;uiIndex < Adapter->uiSectorSize;uiIndex += MAX_RW_SIZE)
1456                         {
1457
1458                                 if(STATUS_SUCCESS == BeceemFlashBulkRead(Adapter,(PUINT)ucReadBk,uiOffsetFromSectStart+uiIndex,MAX_RW_SIZE))
1459                                 {
1460                                         if(memcmp(ucReadBk,&pTempBuff[uiIndex],MAX_RW_SIZE))
1461                                         {
1462                                                 Status = STATUS_FAILURE;
1463                                                 goto BeceemFlashBulkWriteStatus_EXIT;
1464                                         }
1465
1466                                 }
1467
1468                         }
1469                 }
1470
1471                 if(ulStatus)
1472                 {
1473                         BcmRestoreBlockProtectStatus(Adapter,ulStatus);
1474                         ulStatus = 0;
1475                 }
1476
1477                 uiCurrSectOffsetAddr = 0;
1478                 uiSectAlignAddr = uiSectBoundary;
1479                 uiSectBoundary += Adapter->uiSectorSize;
1480                 uiOffsetFromSectStart += Adapter->uiSectorSize;
1481                 uiNumSectTobeRead--;
1482         }
1483 //
1484 // Cleanup.
1485 //
1486 BeceemFlashBulkWriteStatus_EXIT:
1487         if(ulStatus)
1488         {
1489                 BcmRestoreBlockProtectStatus(Adapter,ulStatus);
1490         }
1491
1492         kfree(pTempBuff);
1493         Adapter->SelectedChip = RESET_CHIP_SELECT;
1494         return Status;
1495
1496 }
1497
1498 //-----------------------------------------------------------------------------
1499 // Procedure:   PropagateCalParamsFromEEPROMToMemory
1500 //
1501 // Description: Dumps the calibration section of EEPROM to DDR.
1502 //
1503 // Arguments:
1504 //              Adapter    - ptr to Adapter object instance
1505 // Returns:
1506 //              OSAL_STATUS_CODE
1507 //
1508 //-----------------------------------------------------------------------------
1509
1510
1511 INT PropagateCalParamsFromEEPROMToMemory(PMINI_ADAPTER Adapter)
1512 {
1513         PCHAR pBuff = kmalloc(BUFFER_4K, GFP_KERNEL);
1514         UINT uiEepromSize = 0;
1515         UINT uiIndex = 0;
1516         UINT uiBytesToCopy = 0;
1517         UINT uiCalStartAddr = EEPROM_CALPARAM_START;
1518         UINT uiMemoryLoc = EEPROM_CAL_DATA_INTERNAL_LOC;
1519         UINT value;
1520         INT Status = 0;
1521         if(pBuff == NULL)
1522         {
1523                 return -1;
1524         }
1525
1526         if(0 != BeceemEEPROMBulkRead(Adapter,&uiEepromSize,EEPROM_SIZE_OFFSET,4))
1527         {
1528
1529                 kfree(pBuff);
1530                 return -1;
1531         }
1532
1533         uiEepromSize >>= 16;
1534         if(uiEepromSize > 1024*1024)
1535         {
1536                 kfree(pBuff);
1537                 return -1;
1538         }
1539
1540
1541         uiBytesToCopy = MIN(BUFFER_4K,uiEepromSize);
1542
1543         while(uiBytesToCopy)
1544         {
1545                 if(0 != BeceemEEPROMBulkRead(Adapter,(PUINT)pBuff,uiCalStartAddr,uiBytesToCopy))
1546                 {
1547                         Status = -1;
1548                         break;
1549                 }
1550                 wrm(Adapter,uiMemoryLoc,(PCHAR)(((PULONG)pBuff)+uiIndex),uiBytesToCopy);
1551                 uiMemoryLoc += uiBytesToCopy;
1552                 uiEepromSize -= uiBytesToCopy;
1553                 uiCalStartAddr += uiBytesToCopy;
1554                 uiIndex += uiBytesToCopy/4;
1555                 uiBytesToCopy = MIN(BUFFER_4K,uiEepromSize);
1556
1557         }
1558         value = 0xbeadbead;
1559         wrmalt(Adapter, EEPROM_CAL_DATA_INTERNAL_LOC-4,&value, sizeof(value));
1560         value = 0xbeadbead;
1561         wrmalt(Adapter, EEPROM_CAL_DATA_INTERNAL_LOC-8,&value, sizeof(value));
1562         kfree(pBuff);
1563
1564         return Status;
1565
1566 }
1567
1568 //-----------------------------------------------------------------------------
1569 // Procedure:   PropagateCalParamsFromFlashToMemory
1570 //
1571 // Description: Dumps the calibration section of EEPROM to DDR.
1572 //
1573 // Arguments:
1574 //              Adapter    - ptr to Adapter object instance
1575 // Returns:
1576 //              OSAL_STATUS_CODE
1577 //
1578 //-----------------------------------------------------------------------------
1579
1580 INT PropagateCalParamsFromFlashToMemory(PMINI_ADAPTER Adapter)
1581 {
1582         PCHAR pBuff, pPtr;
1583         UINT uiEepromSize = 0;
1584         UINT uiBytesToCopy = 0;
1585         //UINT uiIndex = 0;
1586         UINT uiCalStartAddr = EEPROM_CALPARAM_START;
1587         UINT uiMemoryLoc = EEPROM_CAL_DATA_INTERNAL_LOC;
1588         UINT value;
1589         INT Status = 0;
1590 //
1591 // Write the signature first. This will ensure firmware does not access EEPROM.
1592 //
1593         value = 0xbeadbead;
1594         wrmalt(Adapter, EEPROM_CAL_DATA_INTERNAL_LOC - 4, &value, sizeof(value));
1595         value = 0xbeadbead;
1596         wrmalt(Adapter, EEPROM_CAL_DATA_INTERNAL_LOC - 8, &value, sizeof(value));
1597
1598         if(0 != BeceemNVMRead(Adapter,&uiEepromSize,EEPROM_SIZE_OFFSET, 4))
1599         {
1600                 return -1;
1601         }
1602         uiEepromSize = ntohl(uiEepromSize);
1603         uiEepromSize >>= 16;
1604
1605 //
1606 //      subtract the auto init section size
1607 //
1608         uiEepromSize -= EEPROM_CALPARAM_START;
1609
1610         if(uiEepromSize > 1024*1024)
1611         {
1612                 return -1;
1613         }
1614
1615         pBuff = kmalloc(uiEepromSize, GFP_KERNEL);
1616
1617         if ( pBuff == NULL )
1618         {
1619                 return -1;
1620         }
1621
1622         if(0 != BeceemNVMRead(Adapter,(PUINT)pBuff,uiCalStartAddr, uiEepromSize))
1623         {
1624                 kfree(pBuff);
1625                 return -1;
1626         }
1627
1628         pPtr = pBuff;
1629
1630         uiBytesToCopy = MIN(BUFFER_4K,uiEepromSize);
1631
1632         while(uiBytesToCopy)
1633         {
1634                 Status = wrm(Adapter,uiMemoryLoc,(PCHAR)pPtr,uiBytesToCopy);
1635                 if(Status)
1636                 {
1637                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"wrm failed with status :%d",Status);
1638                         break;
1639                 }
1640
1641                 pPtr += uiBytesToCopy;
1642                 uiEepromSize -= uiBytesToCopy;
1643                 uiMemoryLoc += uiBytesToCopy;
1644                 uiBytesToCopy = MIN(BUFFER_4K,uiEepromSize);
1645         }
1646
1647         kfree(pBuff);
1648         return Status;
1649
1650 }
1651
1652 //-----------------------------------------------------------------------------
1653 // Procedure:   BeceemEEPROMReadBackandVerify
1654 //
1655 // Description: Read back the data written and verifies.
1656 //
1657 // Arguments:
1658 //              Adapter       - ptr to Adapter object instance
1659 //              pBuffer             - Data to be written.
1660 //              uiOffset       - Offset of the flash where data needs to be written to.
1661 //              uiNumBytes - Number of bytes to be written.
1662 // Returns:
1663 //              OSAL_STATUS_CODE
1664 //
1665 //-----------------------------------------------------------------------------
1666
1667 static INT BeceemEEPROMReadBackandVerify(
1668         PMINI_ADAPTER Adapter,
1669         PUINT pBuffer,
1670         UINT uiOffset,
1671         UINT uiNumBytes)
1672 {
1673         UINT uiRdbk     = 0;
1674         UINT uiIndex    = 0;
1675         UINT uiData     = 0;
1676         UINT auiData[4] = {0};
1677
1678         while(uiNumBytes)
1679         {
1680                 if(Adapter->device_removed )
1681                 {
1682                         return -1;
1683                 }
1684
1685                 if(uiNumBytes >= MAX_RW_SIZE)
1686                 {// for the requests more than or equal to MAX_RW_SIZE bytes, use bulk read function to make the access faster.
1687                         BeceemEEPROMBulkRead(Adapter,&auiData[0],uiOffset,MAX_RW_SIZE);
1688
1689                         if(memcmp(&pBuffer[uiIndex],&auiData[0],MAX_RW_SIZE))
1690                         {
1691                                 // re-write
1692                                 BeceemEEPROMBulkWrite(Adapter,(PUCHAR)(pBuffer+uiIndex),uiOffset,MAX_RW_SIZE,FALSE);
1693                                 mdelay(3);
1694                                 BeceemEEPROMBulkRead(Adapter,&auiData[0],uiOffset,MAX_RW_SIZE);
1695
1696                                 if(memcmp(&pBuffer[uiIndex],&auiData[0],MAX_RW_SIZE))
1697                                 {
1698                                         return -1;
1699                                 }
1700                         }
1701                         uiOffset += MAX_RW_SIZE;
1702                         uiNumBytes -= MAX_RW_SIZE;
1703                         uiIndex += 4;
1704
1705                 }
1706                 else if(uiNumBytes >= 4)
1707                 {
1708                         BeceemEEPROMBulkRead(Adapter,&uiData,uiOffset,4);
1709                         if(uiData != pBuffer[uiIndex])
1710                         {
1711                                 //re-write
1712                                 BeceemEEPROMBulkWrite(Adapter,(PUCHAR)(pBuffer+uiIndex),uiOffset,4,FALSE);
1713                                 mdelay(3);
1714                                 BeceemEEPROMBulkRead(Adapter,&uiData,uiOffset,4);
1715                                 if(uiData != pBuffer[uiIndex])
1716                                 {
1717                                         return -1;
1718                                 }
1719                         }
1720                         uiOffset += 4;
1721                         uiNumBytes -= 4;
1722                         uiIndex++;
1723
1724                 }
1725                 else
1726                 { // Handle the reads less than 4 bytes...
1727                         uiData = 0;
1728                         memcpy(&uiData,((PUCHAR)pBuffer)+(uiIndex*sizeof(UINT)),uiNumBytes);
1729                         BeceemEEPROMBulkRead(Adapter,&uiRdbk,uiOffset,4);
1730
1731                         if(memcmp(&uiData, &uiRdbk, uiNumBytes))
1732                                 return -1;
1733
1734                         uiNumBytes = 0;
1735                 }
1736
1737         }
1738
1739         return 0;
1740 }
1741
1742 static VOID BcmSwapWord(UINT *ptr1) {
1743
1744         UINT  tempval = (UINT)*ptr1;
1745         char *ptr2 = (char *)&tempval;
1746         char *ptr = (char *)ptr1;
1747
1748         ptr[0] = ptr2[3];
1749         ptr[1] = ptr2[2];
1750         ptr[2] = ptr2[1];
1751         ptr[3] = ptr2[0];
1752 }
1753
1754 //-----------------------------------------------------------------------------
1755 // Procedure:   BeceemEEPROMWritePage
1756 //
1757 // Description: Performs page write (16bytes) to the EEPROM
1758 //
1759 // Arguments:
1760 //              Adapter       - ptr to Adapter object instance
1761 //              uiData            - Data to be written.
1762 //              uiOffset      - Offset of the EEPROM where data needs to be written to.
1763 // Returns:
1764 //              OSAL_STATUS_CODE
1765 //
1766 //-----------------------------------------------------------------------------
1767 static INT BeceemEEPROMWritePage( PMINI_ADAPTER Adapter, UINT uiData[], UINT uiOffset )
1768 {
1769         UINT uiRetries = MAX_EEPROM_RETRIES*RETRIES_PER_DELAY;
1770         UINT uiStatus = 0;
1771         UCHAR uiEpromStatus = 0;
1772         UINT value =0 ;
1773
1774         /* Flush the Write/Read/Cmd queues. */
1775         value = ( EEPROM_WRITE_QUEUE_FLUSH | EEPROM_CMD_QUEUE_FLUSH | EEPROM_READ_QUEUE_FLUSH );
1776         wrmalt( Adapter, SPI_FLUSH_REG, &value, sizeof(value));
1777         value = 0 ;
1778         wrmalt( Adapter, SPI_FLUSH_REG, &value, sizeof(value) );
1779
1780         /* Clear the Empty/Avail/Full bits.  After this it has been confirmed
1781          * that the bit was cleared by reading back the register. See NOTE below.
1782          * We also clear the Read queues as we do a EEPROM status register read
1783          * later. */
1784         value = ( EEPROM_WRITE_QUEUE_EMPTY | EEPROM_WRITE_QUEUE_AVAIL | EEPROM_WRITE_QUEUE_FULL | EEPROM_READ_DATA_AVAIL | EEPROM_READ_DATA_FULL ) ;
1785         wrmalt( Adapter, EEPROM_SPI_Q_STATUS1_REG,&value, sizeof(value));
1786
1787         /* Enable write */
1788         value = EEPROM_WRITE_ENABLE ;
1789         wrmalt( Adapter, EEPROM_CMDQ_SPI_REG,&value, sizeof(value) );
1790
1791         /* We can write back to back 8bits * 16 into the queue and as we have
1792          * checked for the queue to be empty we can write in a burst. */
1793
1794         value = uiData[0];
1795         BcmSwapWord(&value);
1796         wrm( Adapter, EEPROM_WRITE_DATAQ_REG, (PUCHAR)&value, 4);
1797
1798         value = uiData[1];
1799         BcmSwapWord(&value);
1800         wrm( Adapter, EEPROM_WRITE_DATAQ_REG, (PUCHAR)&value, 4);
1801
1802         value = uiData[2];
1803         BcmSwapWord(&value);
1804         wrm( Adapter, EEPROM_WRITE_DATAQ_REG, (PUCHAR)&value, 4);
1805
1806         value = uiData[3];
1807         BcmSwapWord(&value);
1808         wrm( Adapter, EEPROM_WRITE_DATAQ_REG, (PUCHAR)&value, 4);
1809
1810         /* NOTE : After this write, on readback of EEPROM_SPI_Q_STATUS1_REG
1811          * shows that we see 7 for the EEPROM data write.  Which means that
1812          * queue got full, also space is available as well as the queue is empty.
1813          * This may happen in sequence. */
1814         value =  EEPROM_16_BYTE_PAGE_WRITE | uiOffset ;
1815         wrmalt( Adapter, EEPROM_CMDQ_SPI_REG, &value, sizeof(value) );
1816
1817         /* Ideally we should loop here without tries and eventually succeed.
1818          * What we are checking if the previous write has completed, and this
1819          * may take time. We should wait till the Empty bit is set. */
1820         uiStatus = 0;
1821         rdmalt( Adapter, EEPROM_SPI_Q_STATUS1_REG,&uiStatus, sizeof(uiStatus)) ;
1822         while ( ( uiStatus & EEPROM_WRITE_QUEUE_EMPTY ) == 0 )
1823         {
1824                 uiRetries--;
1825                 if ( uiRetries == 0 )
1826                 {
1827                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "0x0f003004 = %x, %d retries failed.\n", uiStatus, MAX_EEPROM_RETRIES *RETRIES_PER_DELAY);
1828                         return STATUS_FAILURE ;
1829                 }
1830
1831                 if( !(uiRetries%RETRIES_PER_DELAY) )
1832                                         msleep(1);
1833
1834                 uiStatus = 0;
1835                 rdmalt( Adapter, EEPROM_SPI_Q_STATUS1_REG,&uiStatus, sizeof(uiStatus)) ;
1836                 if(Adapter->device_removed == TRUE)
1837                 {
1838                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Modem got removed hence exiting from loop....");
1839                         return -ENODEV;
1840                 }
1841
1842         }
1843
1844         if ( uiRetries != 0 )
1845         {
1846                 /* Clear the ones that are set - either, Empty/Full/Avail bits */
1847                 value = ( uiStatus & ( EEPROM_WRITE_QUEUE_EMPTY | EEPROM_WRITE_QUEUE_AVAIL | EEPROM_WRITE_QUEUE_FULL ) );
1848                 wrmalt( Adapter, EEPROM_SPI_Q_STATUS1_REG, &value, sizeof(value));
1849         }
1850
1851         /* Here we should check if the EEPROM status register is correct before
1852          * proceeding. Bit 0 in the EEPROM Status register should be 0 before
1853          * we proceed further.  A 1 at Bit 0 indicates that the EEPROM is busy
1854          * with the previous write. Note also that issuing this read finally
1855          * means the previous write to the EEPROM has completed. */
1856         uiRetries = MAX_EEPROM_RETRIES*RETRIES_PER_DELAY;
1857         uiEpromStatus = 0;
1858         while ( uiRetries != 0 )
1859         {
1860                 uiEpromStatus = ReadEEPROMStatusRegister( Adapter) ;
1861                 if(Adapter->device_removed == TRUE)
1862                 {
1863                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Modem has got removed hence exiting from loop...");
1864                         return -ENODEV;
1865                 }
1866                 if ( ( EEPROM_STATUS_REG_WRITE_BUSY & uiEpromStatus ) == 0 )
1867                 {
1868                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "EEPROM status register = %x tries = %d\n", uiEpromStatus, (MAX_EEPROM_RETRIES * RETRIES_PER_DELAY- uiRetries) );
1869                         return STATUS_SUCCESS ;
1870                 }
1871                 uiRetries--;
1872                 if ( uiRetries == 0 )
1873                 {
1874                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "0x0f003004 = %x, for EEPROM status read %d retries failed.\n", uiEpromStatus, MAX_EEPROM_RETRIES *RETRIES_PER_DELAY);
1875                         return STATUS_FAILURE ;
1876                 }
1877                 uiEpromStatus = 0;
1878                 if( !(uiRetries%RETRIES_PER_DELAY) )
1879                                 msleep(1);
1880         }
1881
1882         return STATUS_SUCCESS ;
1883 } /* BeceemEEPROMWritePage */
1884
1885
1886 //-----------------------------------------------------------------------------
1887 // Procedure:   BeceemEEPROMBulkWrite
1888 //
1889 // Description: Performs write to the EEPROM
1890 //
1891 // Arguments:
1892 //              Adapter       - ptr to Adapter object instance
1893 //              pBuffer             - Data to be written.
1894 //              uiOffset       - Offset of the EEPROM where data needs to be written to.
1895 //              uiNumBytes - Number of bytes to be written.
1896 //              bVerify        - read verify flag.
1897 // Returns:
1898 //              OSAL_STATUS_CODE
1899 //
1900 //-----------------------------------------------------------------------------
1901
1902 INT BeceemEEPROMBulkWrite(
1903         PMINI_ADAPTER Adapter,
1904         PUCHAR pBuffer,
1905         UINT uiOffset,
1906         UINT uiNumBytes,
1907         BOOLEAN bVerify)
1908 {
1909         UINT  uiBytesToCopy = uiNumBytes;
1910         //UINT  uiRdbk          = 0;
1911         UINT  uiData[4]         = {0};
1912         UINT  uiIndex           = 0;
1913         UINT  uiTempOffset  = 0;
1914         UINT  uiExtraBytes  = 0;
1915         //PUINT puiBuffer       = (PUINT)pBuffer;
1916         //INT value;
1917
1918         if(uiOffset%MAX_RW_SIZE && uiBytesToCopy)
1919         {
1920                 uiTempOffset = uiOffset - (uiOffset%MAX_RW_SIZE);
1921                 uiExtraBytes = uiOffset-uiTempOffset;
1922
1923
1924                 BeceemEEPROMBulkRead(Adapter,&uiData[0],uiTempOffset,MAX_RW_SIZE);
1925
1926                 if(uiBytesToCopy >= (16 -uiExtraBytes))
1927                 {
1928                         memcpy((((PUCHAR)&uiData[0])+uiExtraBytes),pBuffer,MAX_RW_SIZE- uiExtraBytes);
1929
1930                         if ( STATUS_FAILURE == BeceemEEPROMWritePage( Adapter, uiData, uiTempOffset ) )
1931                                         return STATUS_FAILURE;
1932
1933                         uiBytesToCopy -= (MAX_RW_SIZE - uiExtraBytes);
1934                         uiIndex += (MAX_RW_SIZE - uiExtraBytes);
1935                         uiOffset += (MAX_RW_SIZE - uiExtraBytes);
1936                 }
1937                 else
1938                 {
1939                         memcpy((((PUCHAR)&uiData[0])+uiExtraBytes),pBuffer,uiBytesToCopy);
1940
1941                         if ( STATUS_FAILURE == BeceemEEPROMWritePage( Adapter, uiData, uiTempOffset ) )
1942                                         return STATUS_FAILURE;
1943
1944                         uiIndex += uiBytesToCopy;
1945                         uiOffset += uiBytesToCopy;
1946                         uiBytesToCopy = 0;
1947                 }
1948
1949
1950         }
1951
1952         while(uiBytesToCopy)
1953         {
1954                 if(Adapter->device_removed)
1955                 {
1956                         return -1;
1957                 }
1958
1959                 if(uiBytesToCopy >= MAX_RW_SIZE)
1960                 {
1961
1962                         if (STATUS_FAILURE == BeceemEEPROMWritePage( Adapter, (PUINT) &pBuffer[uiIndex], uiOffset ) )
1963                                                 return STATUS_FAILURE;
1964
1965                         uiIndex += MAX_RW_SIZE;
1966                         uiOffset += MAX_RW_SIZE;
1967                         uiBytesToCopy   -= MAX_RW_SIZE;
1968                 }
1969                 else
1970                 {
1971         //
1972         // To program non 16byte aligned data, read 16byte and then update.
1973         //
1974                         BeceemEEPROMBulkRead(Adapter,&uiData[0],uiOffset,16);
1975                         memcpy(&uiData[0],pBuffer+uiIndex,uiBytesToCopy);
1976
1977
1978                         if ( STATUS_FAILURE == BeceemEEPROMWritePage( Adapter, uiData, uiOffset ) )
1979                                         return STATUS_FAILURE;
1980                         uiBytesToCopy = 0;
1981                 }
1982
1983         }
1984
1985         return 0;
1986 }
1987
1988 //-----------------------------------------------------------------------------
1989 // Procedure:   BeceemNVMRead
1990 //
1991 // Description: Reads n number of bytes from NVM.
1992 //
1993 // Arguments:
1994 //              Adapter      - ptr to Adapter object instance
1995 //              pBuffer       - Buffer to store the data read from NVM
1996 //              uiOffset       - Offset of NVM from where data should be read
1997 //              uiNumBytes - Number of bytes to be read from the NVM.
1998 //
1999 // Returns:
2000 //              OSAL_STATUS_SUCCESS - if NVM read is successfull.
2001 //              <FAILURE>                       - if failed.
2002 //-----------------------------------------------------------------------------
2003
2004 INT BeceemNVMRead(
2005         PMINI_ADAPTER Adapter,
2006         PUINT pBuffer,
2007         UINT uiOffset,
2008         UINT uiNumBytes)
2009 {
2010         INT Status = 0;
2011 #if !defined(BCM_SHM_INTERFACE) || defined(FLASH_DIRECT_ACCESS)
2012         UINT uiTemp = 0, value;
2013 #endif
2014
2015         if(Adapter->eNVMType == NVM_FLASH)
2016         {
2017                 if(Adapter->bFlashRawRead == FALSE)
2018                 {
2019                         if (IsSectionExistInVendorInfo(Adapter,Adapter->eActiveDSD))
2020                                 return vendorextnReadSection(Adapter,(PUCHAR)pBuffer,Adapter->eActiveDSD,uiOffset,uiNumBytes);
2021                         uiOffset = uiOffset+ Adapter->ulFlashCalStart ;
2022                 }
2023 #if defined(BCM_SHM_INTERFACE) && !defined(FLASH_DIRECT_ACCESS)
2024                 Status = bcmflash_raw_read((uiOffset/FLASH_PART_SIZE),(uiOffset % FLASH_PART_SIZE),( unsigned char *)pBuffer,uiNumBytes);
2025 #else
2026
2027                 rdmalt(Adapter, 0x0f000C80, &uiTemp, sizeof(uiTemp));
2028                 value = 0;
2029                 wrmalt(Adapter, 0x0f000C80,&value, sizeof(value));
2030                 Status = BeceemFlashBulkRead(Adapter,
2031                                                 pBuffer,
2032                                                 uiOffset,
2033                                                 uiNumBytes);
2034                 wrmalt(Adapter, 0x0f000C80, &uiTemp, sizeof(uiTemp));
2035 #endif
2036         }
2037         else if(Adapter->eNVMType == NVM_EEPROM)
2038         {
2039                 Status = BeceemEEPROMBulkRead(Adapter,
2040                                         pBuffer,
2041                                         uiOffset,
2042                                         uiNumBytes);
2043         }
2044         else
2045         {
2046                 Status = -1;
2047         }
2048         return Status;
2049 }
2050
2051 //-----------------------------------------------------------------------------
2052 // Procedure:   BeceemNVMWrite
2053 //
2054 // Description: Writes n number of bytes to NVM.
2055 //
2056 // Arguments:
2057 //              Adapter      - ptr to Adapter object instance
2058 //              pBuffer       - Buffer contains the data to be written.
2059 //              uiOffset       - Offset of NVM where data to be written to.
2060 //              uiNumBytes - Number of bytes to be written..
2061 //
2062 // Returns:
2063 //              OSAL_STATUS_SUCCESS - if NVM write is successfull.
2064 //              <FAILURE>                       - if failed.
2065 //-----------------------------------------------------------------------------
2066
2067 INT BeceemNVMWrite(
2068         PMINI_ADAPTER Adapter,
2069         PUINT pBuffer,
2070         UINT uiOffset,
2071         UINT uiNumBytes,
2072         BOOLEAN bVerify)
2073 {
2074         INT Status = 0;
2075         UINT uiTemp = 0;
2076         UINT uiMemoryLoc = EEPROM_CAL_DATA_INTERNAL_LOC;
2077         UINT uiIndex = 0;
2078 #if !defined(BCM_SHM_INTERFACE) || defined(FLASH_DIRECT_ACCESS)
2079         UINT value;
2080 #endif
2081         UINT uiFlashOffset = 0;
2082
2083         if(Adapter->eNVMType == NVM_FLASH)
2084         {
2085                 if (IsSectionExistInVendorInfo(Adapter,Adapter->eActiveDSD))
2086                         Status = vendorextnWriteSection(Adapter,(PUCHAR)pBuffer,Adapter->eActiveDSD,uiOffset,uiNumBytes,bVerify);
2087                 else
2088                 {
2089                         uiFlashOffset = uiOffset + Adapter->ulFlashCalStart;
2090
2091 #if defined(BCM_SHM_INTERFACE) && !defined(FLASH_DIRECT_ACCESS)
2092                         Status = bcmflash_raw_write((uiFlashOffset/FLASH_PART_SIZE), (uiFlashOffset % FLASH_PART_SIZE), (unsigned char *)pBuffer,uiNumBytes);
2093 #else
2094                         rdmalt(Adapter, 0x0f000C80, &uiTemp, sizeof(uiTemp));
2095                         value = 0;
2096                         wrmalt(Adapter, 0x0f000C80, &value, sizeof(value));
2097
2098                         if(Adapter->bStatusWrite == TRUE)
2099                         {
2100                                 Status = BeceemFlashBulkWriteStatus(Adapter,
2101                                                         pBuffer,
2102                                                         uiFlashOffset,
2103                                                         uiNumBytes ,
2104                                                         bVerify);
2105                         }
2106                         else
2107                         {
2108
2109                                 Status = BeceemFlashBulkWrite(Adapter,
2110                                                         pBuffer,
2111                                                         uiFlashOffset,
2112                                                         uiNumBytes,
2113                                                         bVerify);
2114                         }
2115 #endif
2116                 }
2117
2118
2119                 if(uiOffset >= EEPROM_CALPARAM_START)
2120                 {
2121                         uiMemoryLoc += (uiOffset - EEPROM_CALPARAM_START);
2122                         while(uiNumBytes)
2123                         {
2124                                 if(uiNumBytes > BUFFER_4K)
2125                                 {
2126                                         wrm(Adapter,(uiMemoryLoc+uiIndex),(PCHAR)(pBuffer+(uiIndex/4)),BUFFER_4K);
2127                                         uiNumBytes -= BUFFER_4K;
2128                                         uiIndex += BUFFER_4K;
2129                                 }
2130                                 else
2131                                 {
2132                                         wrm(Adapter,uiMemoryLoc+uiIndex,(PCHAR)(pBuffer+(uiIndex/4)),uiNumBytes);
2133                                         uiNumBytes = 0;
2134                                         break;
2135                                 }
2136                         }
2137                 }
2138                 else
2139                 {
2140                         if((uiOffset+uiNumBytes) > EEPROM_CALPARAM_START)
2141                         {
2142                                 ULONG ulBytesTobeSkipped = 0;
2143                                 PUCHAR pcBuffer = (PUCHAR)pBuffer;// char pointer to take care of odd byte cases.
2144                                 uiNumBytes -= (EEPROM_CALPARAM_START - uiOffset);
2145                                 ulBytesTobeSkipped += (EEPROM_CALPARAM_START - uiOffset);
2146                                 uiOffset += (EEPROM_CALPARAM_START - uiOffset);
2147                                 while(uiNumBytes)
2148                                 {
2149                                         if(uiNumBytes > BUFFER_4K)
2150                                         {
2151                                                 wrm(Adapter,uiMemoryLoc+uiIndex,(PCHAR )&pcBuffer[ulBytesTobeSkipped+uiIndex],BUFFER_4K);
2152                                                 uiNumBytes -= BUFFER_4K;
2153                                                 uiIndex += BUFFER_4K;
2154                                         }
2155                                         else
2156                                         {
2157                                                 wrm(Adapter,uiMemoryLoc+uiIndex,(PCHAR)&pcBuffer[ulBytesTobeSkipped+uiIndex],uiNumBytes);
2158                                                 uiNumBytes = 0;
2159                                                 break;
2160                                         }
2161                                 }
2162
2163                         }
2164                 }
2165
2166         // restore the values.
2167                 wrmalt(Adapter,0x0f000C80,&uiTemp, sizeof(uiTemp));
2168         }
2169         else if(Adapter->eNVMType == NVM_EEPROM)
2170         {
2171                 Status = BeceemEEPROMBulkWrite(Adapter,
2172                                         (PUCHAR)pBuffer,
2173                                         uiOffset,
2174                                         uiNumBytes,
2175                                         bVerify);
2176                 if(bVerify)
2177                 {
2178                         Status = BeceemEEPROMReadBackandVerify(Adapter,(PUINT)pBuffer,uiOffset,uiNumBytes);
2179                 }
2180         }
2181         else
2182         {
2183                 Status = -1;
2184         }
2185         return Status;
2186 }
2187
2188 //-----------------------------------------------------------------------------
2189 // Procedure:   BcmUpdateSectorSize
2190 //
2191 // Description: Updates the sector size to FLASH.
2192 //
2193 // Arguments:
2194 //              Adapter       - ptr to Adapter object instance
2195 //          uiSectorSize - sector size
2196 //
2197 // Returns:
2198 //              OSAL_STATUS_SUCCESS - if NVM write is successfull.
2199 //              <FAILURE>                       - if failed.
2200 //-----------------------------------------------------------------------------
2201
2202 INT BcmUpdateSectorSize(PMINI_ADAPTER Adapter,UINT uiSectorSize)
2203 {
2204         INT Status = -1;
2205         FLASH_CS_INFO sFlashCsInfo = {0};
2206         UINT uiTemp = 0;
2207
2208         UINT uiSectorSig = 0;
2209         UINT uiCurrentSectorSize = 0;
2210
2211         UINT value;
2212
2213
2214
2215         rdmalt(Adapter, 0x0f000C80, &uiTemp, sizeof(uiTemp));
2216         value = 0;
2217         wrmalt(Adapter, 0x0f000C80,&value, sizeof(value));
2218
2219 //
2220 // Before updating the sector size in the reserved area, check if already present.
2221 //
2222         BeceemFlashBulkRead(Adapter,(PUINT)&sFlashCsInfo,Adapter->ulFlashControlSectionStart,sizeof(sFlashCsInfo));
2223         uiSectorSig = ntohl(sFlashCsInfo.FlashSectorSizeSig);
2224         uiCurrentSectorSize = ntohl(sFlashCsInfo.FlashSectorSize);
2225
2226         if(uiSectorSig == FLASH_SECTOR_SIZE_SIG)
2227         {
2228
2229                 if((uiCurrentSectorSize <= MAX_SECTOR_SIZE) && (uiCurrentSectorSize >= MIN_SECTOR_SIZE))
2230                 {
2231                         if(uiSectorSize == uiCurrentSectorSize)
2232                         {
2233                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Provided sector size is same as programmed in Flash");
2234                                 Status = STATUS_SUCCESS;
2235                                 goto Restore ;
2236                         }
2237                 }
2238         }
2239
2240         if((uiSectorSize <= MAX_SECTOR_SIZE) && (uiSectorSize >= MIN_SECTOR_SIZE))
2241         {
2242
2243                 sFlashCsInfo.FlashSectorSize = htonl(uiSectorSize);
2244                 sFlashCsInfo.FlashSectorSizeSig = htonl(FLASH_SECTOR_SIZE_SIG);
2245
2246                 Status = BeceemFlashBulkWrite(Adapter,
2247                                         (PUINT)&sFlashCsInfo,
2248                                         Adapter->ulFlashControlSectionStart,
2249                                         sizeof(sFlashCsInfo),
2250                                         TRUE);
2251
2252
2253         }
2254
2255         Restore :
2256         // restore the values.
2257         wrmalt(Adapter, 0x0f000C80,&uiTemp, sizeof(uiTemp));
2258
2259
2260         return Status;
2261
2262 }
2263
2264 //-----------------------------------------------------------------------------
2265 // Procedure:   BcmGetFlashSectorSize
2266 //
2267 // Description: Finds the sector size of the FLASH.
2268 //
2269 // Arguments:
2270 //              Adapter    - ptr to Adapter object instance
2271 //
2272 // Returns:
2273 //              UINT - sector size.
2274 //
2275 //-----------------------------------------------------------------------------
2276
2277 UINT BcmGetFlashSectorSize(PMINI_ADAPTER Adapter, UINT FlashSectorSizeSig, UINT FlashSectorSize)
2278 {
2279         UINT uiSectorSize = 0;
2280         UINT uiSectorSig = 0;
2281
2282         if(Adapter->bSectorSizeOverride &&
2283                 (Adapter->uiSectorSizeInCFG <= MAX_SECTOR_SIZE &&
2284                 Adapter->uiSectorSizeInCFG >= MIN_SECTOR_SIZE))
2285         {
2286                 Adapter->uiSectorSize = Adapter->uiSectorSizeInCFG;
2287         }
2288         else
2289         {
2290
2291                 uiSectorSig = FlashSectorSizeSig;
2292
2293                 if(uiSectorSig == FLASH_SECTOR_SIZE_SIG)
2294                 {
2295                         uiSectorSize = FlashSectorSize;
2296         //
2297         // If the sector size stored in the FLASH makes sense then use it.
2298         //
2299                         if(uiSectorSize <= MAX_SECTOR_SIZE && uiSectorSize >= MIN_SECTOR_SIZE)
2300                         {
2301                                 Adapter->uiSectorSize = uiSectorSize;
2302                         }
2303         //No valid size in FLASH, check if Config file has it.
2304                         else if(Adapter->uiSectorSizeInCFG <= MAX_SECTOR_SIZE &&
2305                                         Adapter->uiSectorSizeInCFG >= MIN_SECTOR_SIZE)
2306                         {
2307                                 Adapter->uiSectorSize = Adapter->uiSectorSizeInCFG;
2308                         }
2309         // Init to Default, if none of the above works.
2310                         else
2311                         {
2312                                 Adapter->uiSectorSize = DEFAULT_SECTOR_SIZE;
2313                         }
2314
2315                 }
2316                 else
2317                 {
2318                         if(Adapter->uiSectorSizeInCFG <= MAX_SECTOR_SIZE &&
2319                                         Adapter->uiSectorSizeInCFG >= MIN_SECTOR_SIZE)
2320                         {
2321                                 Adapter->uiSectorSize = Adapter->uiSectorSizeInCFG;
2322                         }
2323                         else
2324                         {
2325                                 Adapter->uiSectorSize = DEFAULT_SECTOR_SIZE;
2326                         }
2327                 }
2328         }
2329
2330         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "Sector size  :%x \n", Adapter->uiSectorSize);
2331         return Adapter->uiSectorSize;
2332 }
2333
2334 //-----------------------------------------------------------------------------
2335 // Procedure:   BcmInitEEPROMQueues
2336 //
2337 // Description: Initialization of EEPROM queues.
2338 //
2339 // Arguments:
2340 //              Adapter    - ptr to Adapter object instance
2341 //
2342 // Returns:
2343 //              <OSAL_STATUS_CODE>
2344 //-----------------------------------------------------------------------------
2345
2346 static INT BcmInitEEPROMQueues(PMINI_ADAPTER Adapter)
2347 {
2348         UINT value = 0;
2349         /* CHIP Bug : Clear the Avail bits on the Read queue. The default
2350          * value on this register is supposed to be 0x00001102.
2351          * But we get 0x00001122. */
2352         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Fixing reset value on 0x0f003004 register\n" );
2353         value = EEPROM_READ_DATA_AVAIL;
2354         wrmalt( Adapter, EEPROM_SPI_Q_STATUS1_REG, &value, sizeof(value));
2355
2356         /* Flush the all the EEPROM queues. */
2357         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, " Flushing the queues\n");
2358         value =EEPROM_ALL_QUEUE_FLUSH ;
2359         wrmalt( Adapter, SPI_FLUSH_REG, &value, sizeof(value));
2360
2361         value = 0;
2362         wrmalt( Adapter, SPI_FLUSH_REG, &value, sizeof(value) );
2363
2364         /* Read the EEPROM Status Register. Just to see, no real purpose. */
2365         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "EEPROM Status register value = %x\n", ReadEEPROMStatusRegister(Adapter) );
2366
2367         return STATUS_SUCCESS;
2368 } /* BcmInitEEPROMQueues() */
2369
2370 //-----------------------------------------------------------------------------
2371 // Procedure:   BcmInitNVM
2372 //
2373 // Description: Initialization of NVM, EEPROM size,FLASH size, sector size etc.
2374 //
2375 // Arguments:
2376 //              Adapter    - ptr to Adapter object instance
2377 //
2378 // Returns:
2379 //              <OSAL_STATUS_CODE>
2380 //-----------------------------------------------------------------------------
2381
2382 INT BcmInitNVM(PMINI_ADAPTER ps_adapter)
2383 {
2384         BcmValidateNvmType(ps_adapter);
2385         BcmInitEEPROMQueues(ps_adapter);
2386
2387         if(ps_adapter->eNVMType == NVM_AUTODETECT)
2388         {
2389                 ps_adapter->eNVMType = BcmGetNvmType(ps_adapter);
2390                 if(ps_adapter->eNVMType == NVM_UNKNOWN)
2391                 {
2392                         BCM_DEBUG_PRINT(ps_adapter,DBG_TYPE_PRINTK, 0, 0, "NVM Type is unknown!!\n");
2393                 }
2394         }
2395         else if(ps_adapter->eNVMType == NVM_FLASH)
2396         {
2397                 BcmGetFlashCSInfo(ps_adapter);
2398         }
2399
2400         BcmGetNvmSize(ps_adapter);
2401
2402         return STATUS_SUCCESS;
2403 }
2404 /***************************************************************************/
2405 /*BcmGetNvmSize : set the EEPROM or flash size in Adapter.
2406 *
2407 *Input Parameter:
2408 *               Adapter data structure
2409 *Return Value :
2410 *               0. means sucess;
2411 */
2412 /***************************************************************************/
2413
2414 INT BcmGetNvmSize(PMINI_ADAPTER Adapter)
2415 {
2416         if(Adapter->eNVMType == NVM_EEPROM)
2417         {
2418                 Adapter->uiNVMDSDSize = BcmGetEEPROMSize(Adapter);
2419         }
2420         else if(Adapter->eNVMType == NVM_FLASH)
2421         {
2422                 Adapter->uiNVMDSDSize = BcmGetFlashSize(Adapter);
2423         }
2424         return 0;
2425 }
2426
2427 //-----------------------------------------------------------------------------
2428 // Procedure:   BcmValidateNvm
2429 //
2430 // Description: Validates the NVM Type option selected against the device
2431 //
2432 // Arguments:
2433 //              Adapter    - ptr to Adapter object instance
2434 //
2435 // Returns:
2436 //              <VOID>
2437 //-----------------------------------------------------------------------------
2438 VOID BcmValidateNvmType(PMINI_ADAPTER Adapter)
2439 {
2440
2441         //
2442         // if forcing the FLASH through CFG file, we should ensure device really has a FLASH.
2443         // Accessing the FLASH address without the FLASH being present can cause hang/freeze etc.
2444         // So if NVM_FLASH is selected for older chipsets, change it to AUTODETECT where EEPROM is 1st choice.
2445         //
2446
2447         if(Adapter->eNVMType == NVM_FLASH &&
2448                 Adapter->chip_id < 0xBECE3300)
2449         {
2450                 Adapter->eNVMType = NVM_AUTODETECT;
2451         }
2452 }
2453 //-----------------------------------------------------------------------------
2454 // Procedure:   BcmReadFlashRDID
2455 //
2456 // Description: Reads ID from Serial Flash
2457 //
2458 // Arguments:
2459 //              Adapter    - ptr to Adapter object instance
2460 //
2461 // Returns:
2462 //              Flash ID
2463 //-----------------------------------------------------------------------------
2464 static ULONG BcmReadFlashRDID(PMINI_ADAPTER Adapter)
2465 {
2466         ULONG ulRDID = 0;
2467         UINT value;
2468 //
2469 // Read ID Instruction.
2470 //
2471         value = (FLASH_CMD_READ_ID<<24);
2472         wrmalt(Adapter, FLASH_SPI_CMDQ_REG,&value, sizeof(value));
2473
2474 //Delay
2475         udelay(10);
2476 //
2477 // Read SPI READQ REG. The output will be WWXXYYZZ.
2478 // The ID is 3Bytes long and is WWXXYY. ZZ needs to be Ignored.
2479 //
2480         rdmalt(Adapter, FLASH_SPI_READQ_REG,(PUINT)&ulRDID, sizeof(ulRDID));
2481
2482         return (ulRDID >>8);
2483
2484
2485 }
2486
2487 INT BcmAllocFlashCSStructure(PMINI_ADAPTER psAdapter)
2488 {
2489         if(psAdapter == NULL)
2490         {
2491                 BCM_DEBUG_PRINT(psAdapter,DBG_TYPE_PRINTK, 0, 0, "Adapter structure point is NULL");
2492                 return -EINVAL;
2493         }
2494         psAdapter->psFlashCSInfo = (PFLASH_CS_INFO)kzalloc(sizeof(FLASH_CS_INFO), GFP_KERNEL);
2495         if(psAdapter->psFlashCSInfo == NULL)
2496         {
2497                 BCM_DEBUG_PRINT(psAdapter,DBG_TYPE_PRINTK, 0, 0,"Can't Allocate memory for Flash 1.x");
2498                 return -ENOMEM;
2499         }
2500
2501         psAdapter->psFlash2xCSInfo = (PFLASH2X_CS_INFO)kzalloc(sizeof(FLASH2X_CS_INFO), GFP_KERNEL);
2502         if(psAdapter->psFlash2xCSInfo == NULL)
2503         {
2504                 BCM_DEBUG_PRINT(psAdapter,DBG_TYPE_PRINTK, 0, 0,"Can't Allocate memory for Flash 2.x");
2505                 kfree(psAdapter->psFlashCSInfo);
2506                 return -ENOMEM;
2507         }
2508
2509         psAdapter->psFlash2xVendorInfo = (PFLASH2X_VENDORSPECIFIC_INFO)kzalloc(sizeof(FLASH2X_VENDORSPECIFIC_INFO), GFP_KERNEL);
2510         if(psAdapter->psFlash2xVendorInfo == NULL)
2511         {
2512                 BCM_DEBUG_PRINT(psAdapter,DBG_TYPE_PRINTK, 0, 0,"Can't Allocate Vendor Info Memory for Flash 2.x");
2513                 kfree(psAdapter->psFlashCSInfo);
2514                 kfree(psAdapter->psFlash2xCSInfo);
2515                 return -ENOMEM;
2516         }
2517
2518         return STATUS_SUCCESS;
2519 }
2520
2521 INT BcmDeAllocFlashCSStructure(PMINI_ADAPTER psAdapter)
2522 {
2523         if(psAdapter == NULL)
2524         {
2525                 BCM_DEBUG_PRINT(psAdapter,DBG_TYPE_PRINTK, 0, 0," Adapter structure point is NULL");
2526                 return -EINVAL;
2527         }
2528         kfree(psAdapter->psFlashCSInfo);
2529         kfree(psAdapter->psFlash2xCSInfo);
2530         kfree(psAdapter->psFlash2xVendorInfo);
2531         return STATUS_SUCCESS ;
2532 }
2533
2534 static INT      BcmDumpFlash2XCSStructure(PFLASH2X_CS_INFO psFlash2xCSInfo,PMINI_ADAPTER Adapter)
2535 {
2536         UINT Index = 0;
2537     BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "**********************FLASH2X CS Structure *******************");
2538         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "Signature is  :%x", (psFlash2xCSInfo->MagicNumber));
2539         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "Flash Major Version :%d", MAJOR_VERSION(psFlash2xCSInfo->FlashLayoutVersion));
2540         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "Flash Minor Version :%d", MINOR_VERSION(psFlash2xCSInfo->FlashLayoutVersion));
2541         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, " ISOImageMajorVersion:0x%x", (psFlash2xCSInfo->ISOImageVersion));
2542         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "SCSIFirmwareMajorVersion :0x%x", (psFlash2xCSInfo->SCSIFirmwareVersion));
2543         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForPart1ISOImage :0x%x", (psFlash2xCSInfo->OffsetFromZeroForPart1ISOImage));
2544         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForScsiFirmware :0x%x", (psFlash2xCSInfo->OffsetFromZeroForScsiFirmware));
2545         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "SizeOfScsiFirmware  :0x%x", (psFlash2xCSInfo->SizeOfScsiFirmware ));
2546         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForPart2ISOImage :0x%x", (psFlash2xCSInfo->OffsetFromZeroForPart2ISOImage));
2547         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForDSDStart :0x%x", (psFlash2xCSInfo->OffsetFromZeroForDSDStart));
2548         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForDSDEnd :0x%x", (psFlash2xCSInfo->OffsetFromZeroForDSDEnd));
2549         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForVSAStart :0x%x", (psFlash2xCSInfo->OffsetFromZeroForVSAStart));
2550         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForVSAEnd :0x%x", (psFlash2xCSInfo->OffsetFromZeroForVSAEnd));
2551         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForControlSectionStart :0x%x", (psFlash2xCSInfo->OffsetFromZeroForControlSectionStart));
2552         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForControlSectionData :0x%x", (psFlash2xCSInfo->OffsetFromZeroForControlSectionData));
2553         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "CDLessInactivityTimeout :0x%x", (psFlash2xCSInfo->CDLessInactivityTimeout));
2554         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "NewImageSignature :0x%x", (psFlash2xCSInfo->NewImageSignature));
2555         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "FlashSectorSizeSig :0x%x", (psFlash2xCSInfo->FlashSectorSizeSig));
2556         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "FlashSectorSize :0x%x", (psFlash2xCSInfo->FlashSectorSize));
2557         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "FlashWriteSupportSize :0x%x", (psFlash2xCSInfo->FlashWriteSupportSize));
2558         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "TotalFlashSize :0x%X", (psFlash2xCSInfo->TotalFlashSize));
2559         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "FlashBaseAddr :0x%x", (psFlash2xCSInfo->FlashBaseAddr));
2560         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "FlashPartMaxSize :0x%x", (psFlash2xCSInfo->FlashPartMaxSize));
2561         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "IsCDLessDeviceBootSig :0x%x", (psFlash2xCSInfo->IsCDLessDeviceBootSig));
2562         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "MassStorageTimeout :0x%x", (psFlash2xCSInfo->MassStorageTimeout));
2563         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetISOImage1Part1Start :0x%x", (psFlash2xCSInfo->OffsetISOImage1Part1Start));
2564         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetISOImage1Part1End :0x%x", (psFlash2xCSInfo->OffsetISOImage1Part1End));
2565         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetISOImage1Part2Start :0x%x", (psFlash2xCSInfo->OffsetISOImage1Part2Start));
2566         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetISOImage1Part2End :0x%x", (psFlash2xCSInfo->OffsetISOImage1Part2End));
2567         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetISOImage1Part3Start :0x%x", (psFlash2xCSInfo->OffsetISOImage1Part3Start));
2568         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetISOImage1Part3End :0x%x", (psFlash2xCSInfo->OffsetISOImage1Part3End));
2569         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetISOImage2Part1Start :0x%x", (psFlash2xCSInfo->OffsetISOImage2Part1Start));
2570         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetISOImage2Part1End  :0x%x", (psFlash2xCSInfo->OffsetISOImage2Part1End));
2571         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetISOImage2Part2Start :0x%x", (psFlash2xCSInfo->OffsetISOImage2Part2Start));
2572         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetISOImage2Part2End :0x%x", (psFlash2xCSInfo->OffsetISOImage2Part2End));
2573         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetISOImage2Part3Start :0x%x", (psFlash2xCSInfo->OffsetISOImage2Part3Start));
2574         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetISOImage2Part3End :0x%x", (psFlash2xCSInfo->OffsetISOImage2Part3End));
2575         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromDSDStartForDSDHeader :0x%x", (psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader));
2576         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForDSD1Start :0x%x", (psFlash2xCSInfo->OffsetFromZeroForDSD1Start));
2577         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForDSD1End :0x%x", (psFlash2xCSInfo->OffsetFromZeroForDSD1End));
2578         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForDSD2Start :0x%x", (psFlash2xCSInfo->OffsetFromZeroForDSD2Start));
2579         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForDSD2End :0x%x", (psFlash2xCSInfo->OffsetFromZeroForDSD2End));
2580         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForVSA1Start :0x%x", (psFlash2xCSInfo->OffsetFromZeroForVSA1Start));
2581         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForVSA1End :0x%x", (psFlash2xCSInfo->OffsetFromZeroForVSA1End));
2582         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForVSA2Start :0x%x", (psFlash2xCSInfo->OffsetFromZeroForVSA2Start));
2583         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "OffsetFromZeroForVSA2End :0x%x", (psFlash2xCSInfo->OffsetFromZeroForVSA2End));
2584         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "Sector Access Bit Map is Defined as :");
2585         for(Index =0; Index <(FLASH2X_TOTAL_SIZE/(DEFAULT_SECTOR_SIZE *16)); Index++)
2586         {
2587                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "SectorAccessBitMap[%d] :0x%x", Index,
2588                                 (psFlash2xCSInfo->SectorAccessBitMap[Index]));
2589         }
2590
2591         return STATUS_SUCCESS;
2592 }
2593
2594
2595 static INT      ConvertEndianOf2XCSStructure(PFLASH2X_CS_INFO psFlash2xCSInfo)
2596 {
2597         UINT Index = 0;
2598         psFlash2xCSInfo->MagicNumber = ntohl(psFlash2xCSInfo->MagicNumber);
2599         psFlash2xCSInfo->FlashLayoutVersion= ntohl(psFlash2xCSInfo->FlashLayoutVersion);
2600         //psFlash2xCSInfo->FlashLayoutMinorVersion = ntohs(psFlash2xCSInfo->FlashLayoutMinorVersion);
2601         psFlash2xCSInfo->ISOImageVersion = ntohl(psFlash2xCSInfo->ISOImageVersion);
2602         psFlash2xCSInfo->SCSIFirmwareVersion =ntohl(psFlash2xCSInfo->SCSIFirmwareVersion);
2603         psFlash2xCSInfo->OffsetFromZeroForPart1ISOImage = ntohl(psFlash2xCSInfo->OffsetFromZeroForPart1ISOImage);
2604         psFlash2xCSInfo->OffsetFromZeroForScsiFirmware = ntohl(psFlash2xCSInfo->OffsetFromZeroForScsiFirmware);
2605         psFlash2xCSInfo->SizeOfScsiFirmware = ntohl(psFlash2xCSInfo->SizeOfScsiFirmware );
2606         psFlash2xCSInfo->OffsetFromZeroForPart2ISOImage = ntohl(psFlash2xCSInfo->OffsetFromZeroForPart2ISOImage);
2607         psFlash2xCSInfo->OffsetFromZeroForDSDStart = ntohl(psFlash2xCSInfo->OffsetFromZeroForDSDStart);
2608         psFlash2xCSInfo->OffsetFromZeroForDSDEnd = ntohl(psFlash2xCSInfo->OffsetFromZeroForDSDEnd);
2609         psFlash2xCSInfo->OffsetFromZeroForVSAStart = ntohl(psFlash2xCSInfo->OffsetFromZeroForVSAStart);
2610         psFlash2xCSInfo->OffsetFromZeroForVSAEnd = ntohl(psFlash2xCSInfo->OffsetFromZeroForVSAEnd);
2611         psFlash2xCSInfo->OffsetFromZeroForControlSectionStart = ntohl(psFlash2xCSInfo->OffsetFromZeroForControlSectionStart);
2612         psFlash2xCSInfo->OffsetFromZeroForControlSectionData = ntohl(psFlash2xCSInfo->OffsetFromZeroForControlSectionData);
2613         psFlash2xCSInfo->CDLessInactivityTimeout = ntohl(psFlash2xCSInfo->CDLessInactivityTimeout);
2614         psFlash2xCSInfo->NewImageSignature = ntohl(psFlash2xCSInfo->NewImageSignature);
2615         psFlash2xCSInfo->FlashSectorSizeSig = ntohl(psFlash2xCSInfo->FlashSectorSizeSig);
2616         psFlash2xCSInfo->FlashSectorSize = ntohl(psFlash2xCSInfo->FlashSectorSize);
2617         psFlash2xCSInfo->FlashWriteSupportSize = ntohl(psFlash2xCSInfo->FlashWriteSupportSize);
2618         psFlash2xCSInfo->TotalFlashSize = ntohl(psFlash2xCSInfo->TotalFlashSize);
2619         psFlash2xCSInfo->FlashBaseAddr = ntohl(psFlash2xCSInfo->FlashBaseAddr);
2620         psFlash2xCSInfo->FlashPartMaxSize = ntohl(psFlash2xCSInfo->FlashPartMaxSize);
2621         psFlash2xCSInfo->IsCDLessDeviceBootSig = ntohl(psFlash2xCSInfo->IsCDLessDeviceBootSig);
2622         psFlash2xCSInfo->MassStorageTimeout = ntohl(psFlash2xCSInfo->MassStorageTimeout);
2623         psFlash2xCSInfo->OffsetISOImage1Part1Start = ntohl(psFlash2xCSInfo->OffsetISOImage1Part1Start);
2624         psFlash2xCSInfo->OffsetISOImage1Part1End = ntohl(psFlash2xCSInfo->OffsetISOImage1Part1End);
2625         psFlash2xCSInfo->OffsetISOImage1Part2Start = ntohl(psFlash2xCSInfo->OffsetISOImage1Part2Start);
2626         psFlash2xCSInfo->OffsetISOImage1Part2End = ntohl(psFlash2xCSInfo->OffsetISOImage1Part2End);
2627         psFlash2xCSInfo->OffsetISOImage1Part3Start = ntohl(psFlash2xCSInfo->OffsetISOImage1Part3Start);
2628         psFlash2xCSInfo->OffsetISOImage1Part3End = ntohl(psFlash2xCSInfo->OffsetISOImage1Part3End);
2629         psFlash2xCSInfo->OffsetISOImage2Part1Start = ntohl(psFlash2xCSInfo->OffsetISOImage2Part1Start);
2630         psFlash2xCSInfo->OffsetISOImage2Part1End = ntohl(psFlash2xCSInfo->OffsetISOImage2Part1End);
2631         psFlash2xCSInfo->OffsetISOImage2Part2Start = ntohl(psFlash2xCSInfo->OffsetISOImage2Part2Start);
2632         psFlash2xCSInfo->OffsetISOImage2Part2End = ntohl(psFlash2xCSInfo->OffsetISOImage2Part2End);
2633         psFlash2xCSInfo->OffsetISOImage2Part3Start = ntohl(psFlash2xCSInfo->OffsetISOImage2Part3Start);
2634         psFlash2xCSInfo->OffsetISOImage2Part3End = ntohl(psFlash2xCSInfo->OffsetISOImage2Part3End);
2635         psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader = ntohl(psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader);
2636         psFlash2xCSInfo->OffsetFromZeroForDSD1Start = ntohl(psFlash2xCSInfo->OffsetFromZeroForDSD1Start);
2637         psFlash2xCSInfo->OffsetFromZeroForDSD1End = ntohl(psFlash2xCSInfo->OffsetFromZeroForDSD1End);
2638         psFlash2xCSInfo->OffsetFromZeroForDSD2Start = ntohl(psFlash2xCSInfo->OffsetFromZeroForDSD2Start);
2639         psFlash2xCSInfo->OffsetFromZeroForDSD2End = ntohl(psFlash2xCSInfo->OffsetFromZeroForDSD2End);
2640         psFlash2xCSInfo->OffsetFromZeroForVSA1Start = ntohl(psFlash2xCSInfo->OffsetFromZeroForVSA1Start);
2641         psFlash2xCSInfo->OffsetFromZeroForVSA1End = ntohl(psFlash2xCSInfo->OffsetFromZeroForVSA1End);
2642         psFlash2xCSInfo->OffsetFromZeroForVSA2Start = ntohl(psFlash2xCSInfo->OffsetFromZeroForVSA2Start);
2643         psFlash2xCSInfo->OffsetFromZeroForVSA2End = ntohl(psFlash2xCSInfo->OffsetFromZeroForVSA2End);
2644         for(Index =0; Index <(FLASH2X_TOTAL_SIZE/(DEFAULT_SECTOR_SIZE *16)); Index++)
2645         {
2646                         psFlash2xCSInfo->SectorAccessBitMap[Index] = ntohl(psFlash2xCSInfo->SectorAccessBitMap[Index]);
2647         }
2648         return STATUS_SUCCESS;
2649 }
2650
2651 static INT      ConvertEndianOfCSStructure(PFLASH_CS_INFO psFlashCSInfo)
2652 {
2653         //UINT Index = 0;
2654         psFlashCSInfo->MagicNumber                                                      =ntohl(psFlashCSInfo->MagicNumber);
2655         psFlashCSInfo->FlashLayoutVersion                                       =ntohl(psFlashCSInfo->FlashLayoutVersion);
2656         psFlashCSInfo->ISOImageVersion                                          = ntohl(psFlashCSInfo->ISOImageVersion);
2657         //won't convert according to old assumption
2658         psFlashCSInfo->SCSIFirmwareVersion =(psFlashCSInfo->SCSIFirmwareVersion);
2659
2660         psFlashCSInfo->OffsetFromZeroForPart1ISOImage           = ntohl(psFlashCSInfo->OffsetFromZeroForPart1ISOImage);
2661         psFlashCSInfo->OffsetFromZeroForScsiFirmware        = ntohl(psFlashCSInfo->OffsetFromZeroForScsiFirmware);
2662         psFlashCSInfo->SizeOfScsiFirmware                   = ntohl(psFlashCSInfo->SizeOfScsiFirmware );
2663         psFlashCSInfo->OffsetFromZeroForPart2ISOImage       = ntohl(psFlashCSInfo->OffsetFromZeroForPart2ISOImage);
2664         psFlashCSInfo->OffsetFromZeroForCalibrationStart    = ntohl(psFlashCSInfo->OffsetFromZeroForCalibrationStart);
2665         psFlashCSInfo->OffsetFromZeroForCalibrationEnd      = ntohl(psFlashCSInfo->OffsetFromZeroForCalibrationEnd);
2666         psFlashCSInfo->OffsetFromZeroForVSAStart            = ntohl(psFlashCSInfo->OffsetFromZeroForVSAStart);
2667         psFlashCSInfo->OffsetFromZeroForVSAEnd              = ntohl(psFlashCSInfo->OffsetFromZeroForVSAEnd);
2668         psFlashCSInfo->OffsetFromZeroForControlSectionStart = ntohl(psFlashCSInfo->OffsetFromZeroForControlSectionStart);
2669         psFlashCSInfo->OffsetFromZeroForControlSectionData  = ntohl(psFlashCSInfo->OffsetFromZeroForControlSectionData);
2670         psFlashCSInfo->CDLessInactivityTimeout                          = ntohl(psFlashCSInfo->CDLessInactivityTimeout);
2671         psFlashCSInfo->NewImageSignature                    = ntohl(psFlashCSInfo->NewImageSignature);
2672         psFlashCSInfo->FlashSectorSizeSig                   = ntohl(psFlashCSInfo->FlashSectorSizeSig);
2673         psFlashCSInfo->FlashSectorSize                      = ntohl(psFlashCSInfo->FlashSectorSize);
2674         psFlashCSInfo->FlashWriteSupportSize                = ntohl(psFlashCSInfo->FlashWriteSupportSize);
2675         psFlashCSInfo->TotalFlashSize                                   = ntohl(psFlashCSInfo->TotalFlashSize);
2676         psFlashCSInfo->FlashBaseAddr                                    = ntohl(psFlashCSInfo->FlashBaseAddr);
2677         psFlashCSInfo->FlashPartMaxSize                                 = ntohl(psFlashCSInfo->FlashPartMaxSize);
2678         psFlashCSInfo->IsCDLessDeviceBootSig                            = ntohl(psFlashCSInfo->IsCDLessDeviceBootSig);
2679         psFlashCSInfo->MassStorageTimeout                               = ntohl(psFlashCSInfo->MassStorageTimeout);
2680
2681         return STATUS_SUCCESS;
2682 }
2683
2684 INT IsSectionExistInVendorInfo(PMINI_ADAPTER Adapter, FLASH2X_SECTION_VAL section)
2685 {
2686         return ( Adapter->uiVendorExtnFlag &&
2687                 (Adapter->psFlash2xVendorInfo->VendorSection[section].AccessFlags & FLASH2X_SECTION_PRESENT) &&
2688                 (Adapter->psFlash2xVendorInfo->VendorSection[section].OffsetFromZeroForSectionStart != UNINIT_PTR_IN_CS) );
2689 }
2690
2691 static VOID UpdateVendorInfo(PMINI_ADAPTER Adapter)
2692 {
2693         B_UINT32 i = 0;
2694         UINT uiSizeSection = 0;
2695
2696         Adapter->uiVendorExtnFlag = FALSE;
2697
2698         for(i = 0;i < TOTAL_SECTIONS;i++)
2699                 Adapter->psFlash2xVendorInfo->VendorSection[i].OffsetFromZeroForSectionStart = UNINIT_PTR_IN_CS;
2700
2701         if(STATUS_SUCCESS != vendorextnGetSectionInfo(Adapter, Adapter->psFlash2xVendorInfo))
2702                 return;
2703
2704         i = 0;
2705         while(i < TOTAL_SECTIONS)
2706         {
2707                 if(!(Adapter->psFlash2xVendorInfo->VendorSection[i].AccessFlags & FLASH2X_SECTION_PRESENT))
2708                 {
2709                         i++;
2710                         continue;
2711                 }
2712
2713                 Adapter->uiVendorExtnFlag = TRUE;
2714                 uiSizeSection = (Adapter->psFlash2xVendorInfo->VendorSection[i].OffsetFromZeroForSectionEnd -
2715                                                 Adapter->psFlash2xVendorInfo->VendorSection[i].OffsetFromZeroForSectionStart);
2716
2717                 switch(i)
2718                 {
2719                 case DSD0:
2720                         if(( uiSizeSection >= (Adapter->psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader + sizeof(DSD_HEADER))) &&
2721                         (UNINIT_PTR_IN_CS != Adapter->psFlash2xVendorInfo->VendorSection[i].OffsetFromZeroForSectionStart))
2722                                 Adapter->psFlash2xCSInfo->OffsetFromZeroForDSDStart = Adapter->psFlash2xCSInfo->OffsetFromZeroForDSDEnd = VENDOR_PTR_IN_CS;
2723                         else
2724                                 Adapter->psFlash2xCSInfo->OffsetFromZeroForDSDStart = Adapter->psFlash2xCSInfo->OffsetFromZeroForDSDEnd = UNINIT_PTR_IN_CS;
2725                         break;
2726
2727                 case DSD1:
2728                         if(( uiSizeSection >= (Adapter->psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader + sizeof(DSD_HEADER))) &&
2729                         (UNINIT_PTR_IN_CS != Adapter->psFlash2xVendorInfo->VendorSection[i].OffsetFromZeroForSectionStart))
2730                                 Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD1Start = Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD1End = VENDOR_PTR_IN_CS;
2731                         else
2732                                 Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD1Start = Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD1End = UNINIT_PTR_IN_CS;
2733                         break;
2734
2735                 case DSD2:
2736                         if(( uiSizeSection >= (Adapter->psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader + sizeof(DSD_HEADER))) &&
2737                         (UNINIT_PTR_IN_CS != Adapter->psFlash2xVendorInfo->VendorSection[i].OffsetFromZeroForSectionStart))
2738                                 Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD2Start = Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD2End = VENDOR_PTR_IN_CS;
2739                         else
2740                                 Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD2Start = Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD2End = UNINIT_PTR_IN_CS;
2741                         break;
2742                 case VSA0:
2743                         if(UNINIT_PTR_IN_CS != Adapter->psFlash2xVendorInfo->VendorSection[i].OffsetFromZeroForSectionStart)
2744                                 Adapter->psFlash2xCSInfo->OffsetFromZeroForVSAStart = Adapter->psFlash2xCSInfo->OffsetFromZeroForVSAEnd = VENDOR_PTR_IN_CS;
2745                         else
2746                                 Adapter->psFlash2xCSInfo->OffsetFromZeroForVSAStart = Adapter->psFlash2xCSInfo->OffsetFromZeroForVSAEnd = UNINIT_PTR_IN_CS;
2747                         break;
2748
2749                 case VSA1:
2750                         if(UNINIT_PTR_IN_CS != Adapter->psFlash2xVendorInfo->VendorSection[i].OffsetFromZeroForSectionStart)
2751                                 Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA1Start = Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA1End = VENDOR_PTR_IN_CS;
2752                         else
2753                                 Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA1Start = Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA1End = UNINIT_PTR_IN_CS;
2754                         break;
2755                 case VSA2:
2756                         if(UNINIT_PTR_IN_CS != Adapter->psFlash2xVendorInfo->VendorSection[i].OffsetFromZeroForSectionStart)
2757                                 Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA2Start = Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA2End = VENDOR_PTR_IN_CS;
2758                         else
2759                                 Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA2Start = Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA2End = UNINIT_PTR_IN_CS;
2760                         break;
2761
2762                 default:
2763                         break;
2764                 }
2765                 i++;
2766         }
2767
2768 }
2769
2770 //-----------------------------------------------------------------------------
2771 // Procedure:   BcmGetFlashCSInfo
2772 //
2773 // Description: Reads control structure and gets Cal section addresses.
2774 //
2775 // Arguments:
2776 //              Adapter    - ptr to Adapter object instance
2777 //
2778 // Returns:
2779 //              <VOID>
2780 //-----------------------------------------------------------------------------
2781
2782 INT BcmGetFlashCSInfo(PMINI_ADAPTER Adapter)
2783 {
2784         //FLASH_CS_INFO sFlashCsInfo = {0};
2785
2786 #if !defined(BCM_SHM_INTERFACE) || defined(FLASH_DIRECT_ACCESS)
2787         UINT value;
2788 #endif
2789         UINT uiFlashLayoutMajorVersion;
2790         Adapter->uiFlashLayoutMinorVersion = 0;
2791         Adapter->uiFlashLayoutMajorVersion = 0;
2792         Adapter->ulFlashControlSectionStart = FLASH_CS_INFO_START_ADDR;
2793
2794
2795         Adapter->uiFlashBaseAdd = 0;
2796         Adapter->ulFlashCalStart = 0;
2797         memset(Adapter->psFlashCSInfo, 0 ,sizeof(FLASH_CS_INFO));
2798         memset(Adapter->psFlash2xCSInfo, 0 ,sizeof(FLASH2X_CS_INFO));
2799
2800         if(!Adapter->bDDRInitDone)
2801         {
2802                 {
2803                         value = FLASH_CONTIGIOUS_START_ADDR_BEFORE_INIT;
2804                         wrmalt(Adapter, 0xAF00A080, &value, sizeof(value));
2805                 }
2806         }
2807
2808
2809         // Reading first 8 Bytes to get the Flash Layout
2810         // MagicNumber(4 bytes) +FlashLayoutMinorVersion(2 Bytes) +FlashLayoutMajorVersion(2 Bytes)
2811         BeceemFlashBulkRead(Adapter,(PUINT)Adapter->psFlashCSInfo,Adapter->ulFlashControlSectionStart,8);
2812
2813         Adapter->psFlashCSInfo->FlashLayoutVersion =  ntohl(Adapter->psFlashCSInfo->FlashLayoutVersion);
2814         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "Flash Layout Version :%X", (Adapter->psFlashCSInfo->FlashLayoutVersion));
2815         //BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "Flash Layout Minor Version :%d\n", ntohs(sFlashCsInfo.FlashLayoutMinorVersion));
2816         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "Signature is  :%x\n", ntohl(Adapter->psFlashCSInfo->MagicNumber));
2817
2818         if(FLASH_CONTROL_STRUCT_SIGNATURE == ntohl(Adapter->psFlashCSInfo->MagicNumber))
2819         {
2820                 uiFlashLayoutMajorVersion = MAJOR_VERSION((Adapter->psFlashCSInfo->FlashLayoutVersion));
2821                 Adapter->uiFlashLayoutMinorVersion = MINOR_VERSION((Adapter->psFlashCSInfo->FlashLayoutVersion));
2822         }
2823         else
2824         {
2825                 Adapter->uiFlashLayoutMinorVersion = 0;
2826                 uiFlashLayoutMajorVersion = 0;
2827         }
2828
2829         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"FLASH LAYOUT MAJOR VERSION :%X", uiFlashLayoutMajorVersion);
2830
2831         if(uiFlashLayoutMajorVersion < FLASH_2X_MAJOR_NUMBER)
2832         {
2833                 BeceemFlashBulkRead(Adapter,(PUINT)Adapter->psFlashCSInfo,Adapter->ulFlashControlSectionStart,sizeof(FLASH_CS_INFO));
2834                 ConvertEndianOfCSStructure(Adapter->psFlashCSInfo);
2835                 Adapter->ulFlashCalStart = (Adapter->psFlashCSInfo->OffsetFromZeroForCalibrationStart);
2836
2837                 if(!((Adapter->uiFlashLayoutMajorVersion == 1) && (Adapter->uiFlashLayoutMinorVersion == 1)))
2838                 {
2839                         Adapter->ulFlashControlSectionStart = Adapter->psFlashCSInfo->OffsetFromZeroForControlSectionStart;
2840                 }
2841
2842                 if((FLASH_CONTROL_STRUCT_SIGNATURE == (Adapter->psFlashCSInfo->MagicNumber)) &&
2843                    (SCSI_FIRMWARE_MINOR_VERSION <= MINOR_VERSION(Adapter->psFlashCSInfo->SCSIFirmwareVersion)) &&
2844                    (FLASH_SECTOR_SIZE_SIG == (Adapter->psFlashCSInfo->FlashSectorSizeSig)) &&
2845                    (BYTE_WRITE_SUPPORT == (Adapter->psFlashCSInfo->FlashWriteSupportSize)))
2846                 {
2847                         Adapter->ulFlashWriteSize = (Adapter->psFlashCSInfo->FlashWriteSupportSize);
2848                         Adapter->fpFlashWrite = flashByteWrite;
2849                         Adapter->fpFlashWriteWithStatusCheck = flashByteWriteStatus;
2850                 }
2851                 else
2852                 {
2853                         Adapter->ulFlashWriteSize = MAX_RW_SIZE;
2854                         Adapter->fpFlashWrite = flashWrite;
2855                         Adapter->fpFlashWriteWithStatusCheck = flashWriteStatus;
2856                 }
2857
2858                 BcmGetFlashSectorSize(Adapter, (Adapter->psFlashCSInfo->FlashSectorSizeSig),
2859                                          (Adapter->psFlashCSInfo->FlashSectorSize));
2860
2861
2862                 Adapter->uiFlashBaseAdd = Adapter->psFlashCSInfo->FlashBaseAddr & 0xFCFFFFFF;
2863
2864
2865         }
2866         else
2867         {
2868                 if(BcmFlash2xBulkRead(Adapter,(PUINT)Adapter->psFlash2xCSInfo,NO_SECTION_VAL,
2869                                 Adapter->ulFlashControlSectionStart,sizeof(FLASH2X_CS_INFO)))
2870                 {
2871                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Unable to read CS structure \n");
2872                         return STATUS_FAILURE;
2873                 }
2874                 ConvertEndianOf2XCSStructure(Adapter->psFlash2xCSInfo);
2875                 BcmDumpFlash2XCSStructure(Adapter->psFlash2xCSInfo,Adapter);
2876                 if((FLASH_CONTROL_STRUCT_SIGNATURE == Adapter->psFlash2xCSInfo->MagicNumber) &&
2877                    (SCSI_FIRMWARE_MINOR_VERSION <= MINOR_VERSION(Adapter->psFlash2xCSInfo->SCSIFirmwareVersion)) &&
2878                    (FLASH_SECTOR_SIZE_SIG == Adapter->psFlash2xCSInfo->FlashSectorSizeSig) &&
2879                    (BYTE_WRITE_SUPPORT == Adapter->psFlash2xCSInfo->FlashWriteSupportSize))
2880                 {
2881                         Adapter->ulFlashWriteSize = Adapter->psFlash2xCSInfo->FlashWriteSupportSize;
2882                         Adapter->fpFlashWrite = flashByteWrite;
2883                         Adapter->fpFlashWriteWithStatusCheck = flashByteWriteStatus;
2884                 }
2885                 else
2886                 {
2887                         Adapter->ulFlashWriteSize = MAX_RW_SIZE;
2888                         Adapter->fpFlashWrite = flashWrite;
2889                         Adapter->fpFlashWriteWithStatusCheck = flashWriteStatus;
2890                 }
2891
2892                 BcmGetFlashSectorSize(Adapter, Adapter->psFlash2xCSInfo->FlashSectorSizeSig,
2893                                         Adapter->psFlash2xCSInfo->FlashSectorSize);
2894
2895                 UpdateVendorInfo(Adapter);
2896
2897                 BcmGetActiveDSD(Adapter);
2898                 BcmGetActiveISO(Adapter);
2899                 Adapter->uiFlashBaseAdd = Adapter->psFlash2xCSInfo->FlashBaseAddr & 0xFCFFFFFF;
2900                 Adapter->ulFlashControlSectionStart = Adapter->psFlash2xCSInfo->OffsetFromZeroForControlSectionStart;
2901
2902         }
2903         /*
2904         Concerns: what if CS sector size does not match with this sector size ???
2905         what is the indication of AccessBitMap  in CS in flash 2.x ????
2906         */
2907         Adapter->ulFlashID = BcmReadFlashRDID(Adapter);
2908
2909         Adapter->uiFlashLayoutMajorVersion = uiFlashLayoutMajorVersion;
2910
2911
2912         return STATUS_SUCCESS ;
2913 }
2914
2915
2916 //-----------------------------------------------------------------------------
2917 // Procedure:   BcmGetNvmType
2918 //
2919 // Description: Finds the type of NVM used.
2920 //
2921 // Arguments:
2922 //              Adapter    - ptr to Adapter object instance
2923 //
2924 // Returns:
2925 //              NVM_TYPE
2926 //
2927 //-----------------------------------------------------------------------------
2928
2929 NVM_TYPE BcmGetNvmType(PMINI_ADAPTER Adapter)
2930 {
2931         UINT uiData = 0;
2932
2933         BeceemEEPROMBulkRead(Adapter,&uiData,0x0,4);
2934         if(uiData == BECM)
2935         {
2936                 return NVM_EEPROM;
2937         }
2938         //
2939         // Read control struct and get cal addresses before accessing the flash
2940         //
2941         BcmGetFlashCSInfo(Adapter);
2942
2943         BeceemFlashBulkRead(Adapter,&uiData,0x0 + Adapter->ulFlashCalStart,4);
2944         if(uiData == BECM)
2945         {
2946                 return NVM_FLASH;
2947         }
2948 //
2949 // even if there is no valid signature on EEPROM/FLASH find out if they really exist.
2950 // if exist select it.
2951 //
2952         if(BcmGetEEPROMSize(Adapter))
2953         {
2954                 return NVM_EEPROM;
2955         }
2956
2957 //TBD for Flash.
2958
2959
2960         return NVM_UNKNOWN;
2961 }
2962
2963 /**
2964 *       BcmGetSectionValStartOffset - this will calculate the section's starting offset if section val is given
2965 *       @Adapter : Drivers Private Data structure
2966 *       @eFlashSectionVal : Flash secion value defined in enum FLASH2X_SECTION_VAL
2967 *
2968 *       Return value:-
2969 *       On success it return the start offset of the provided section val
2970 *       On Failure -returns STATUS_FAILURE
2971 **/
2972
2973 INT BcmGetSectionValStartOffset(PMINI_ADAPTER Adapter, FLASH2X_SECTION_VAL eFlashSectionVal)
2974 {
2975         /*
2976         *       Considering all the section for which end offset can be calculated or directly given
2977         *       in CS Structure. if matching case does not exist, return STATUS_FAILURE indicating section
2978         *       endoffset can't be calculated or given in CS Stucture.
2979         */
2980
2981         INT SectStartOffset = 0 ;
2982
2983         SectStartOffset = INVALID_OFFSET ;
2984
2985         if(IsSectionExistInVendorInfo(Adapter,eFlashSectionVal))
2986         {
2987                 return Adapter->psFlash2xVendorInfo->VendorSection[eFlashSectionVal].OffsetFromZeroForSectionStart;
2988         }
2989
2990         switch(eFlashSectionVal)
2991         {
2992                 case ISO_IMAGE1 :
2993                           if((Adapter->psFlash2xCSInfo->OffsetISOImage1Part1Start != UNINIT_PTR_IN_CS) &&
2994                                 (IsNonCDLessDevice(Adapter) == FALSE))
2995                                   SectStartOffset = (Adapter->psFlash2xCSInfo->OffsetISOImage1Part1Start);
2996                            break;
2997                 case ISO_IMAGE2 :
2998                                 if((Adapter->psFlash2xCSInfo->OffsetISOImage2Part1Start != UNINIT_PTR_IN_CS) &&
2999                                         (IsNonCDLessDevice(Adapter) == FALSE))
3000                                         SectStartOffset = (Adapter->psFlash2xCSInfo->OffsetISOImage2Part1Start);
3001                           break;
3002                 case DSD0 :
3003                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForDSDStart != UNINIT_PTR_IN_CS)
3004                                         SectStartOffset = (Adapter->psFlash2xCSInfo->OffsetFromZeroForDSDStart);
3005                                 break;
3006                 case DSD1 :
3007                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD1Start != UNINIT_PTR_IN_CS)
3008                                         SectStartOffset =  (Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD1Start);
3009                                 break;
3010                 case DSD2 :
3011                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD2Start != UNINIT_PTR_IN_CS)
3012                                         SectStartOffset =  (Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD2Start);
3013                                 break;
3014                 case VSA0 :
3015                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForVSAStart != UNINIT_PTR_IN_CS)
3016                                         SectStartOffset =  (Adapter->psFlash2xCSInfo->OffsetFromZeroForVSAStart);
3017                                 break;
3018                 case VSA1 :
3019                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA1Start != UNINIT_PTR_IN_CS)
3020                                         SectStartOffset =  (Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA1Start);
3021                                 break;
3022                 case VSA2 :
3023                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA2Start != UNINIT_PTR_IN_CS)
3024                                         SectStartOffset =  (Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA2Start);
3025                                 break;
3026                 case SCSI :
3027                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForScsiFirmware != UNINIT_PTR_IN_CS)
3028                                         SectStartOffset =  (Adapter->psFlash2xCSInfo->OffsetFromZeroForScsiFirmware);
3029                                 break;
3030                 case CONTROL_SECTION :
3031                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForControlSectionStart != UNINIT_PTR_IN_CS)
3032                                         SectStartOffset =  (Adapter->psFlash2xCSInfo->OffsetFromZeroForControlSectionStart);
3033                                 break;
3034                 case ISO_IMAGE1_PART2 :
3035                                 if(Adapter->psFlash2xCSInfo->OffsetISOImage1Part2Start != UNINIT_PTR_IN_CS)
3036                                          SectStartOffset =  (Adapter->psFlash2xCSInfo->OffsetISOImage1Part2Start);
3037                                  break;
3038                 case ISO_IMAGE1_PART3 :
3039                                 if(Adapter->psFlash2xCSInfo->OffsetISOImage1Part3Start != UNINIT_PTR_IN_CS)
3040                                   SectStartOffset =  (Adapter->psFlash2xCSInfo->OffsetISOImage1Part3Start);
3041                                 break;
3042                 case ISO_IMAGE2_PART2 :
3043                                 if(Adapter->psFlash2xCSInfo->OffsetISOImage2Part2Start != UNINIT_PTR_IN_CS)
3044                                          SectStartOffset =  (Adapter->psFlash2xCSInfo->OffsetISOImage2Part2Start);
3045                             break;
3046                 case ISO_IMAGE2_PART3 :
3047                           if(Adapter->psFlash2xCSInfo->OffsetISOImage2Part3Start != UNINIT_PTR_IN_CS)
3048                                   SectStartOffset =  (Adapter->psFlash2xCSInfo->OffsetISOImage2Part3Start);
3049                           break;
3050                 default :
3051                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Section Does not exist in Flash 2.x");
3052                         SectStartOffset =  INVALID_OFFSET;
3053         }
3054         return SectStartOffset;
3055 }
3056
3057 /**
3058 *       BcmGetSectionValEndOffset - this will calculate the section's Ending offset if section val is given
3059 *       @Adapter : Drivers Private Data structure
3060 *       @eFlashSectionVal : Flash secion value defined in enum FLASH2X_SECTION_VAL
3061 *
3062 *       Return value:-
3063 *       On success it return the end offset of the provided section val
3064 *       On Failure -returns STATUS_FAILURE
3065 **/
3066
3067 INT BcmGetSectionValEndOffset(PMINI_ADAPTER Adapter, FLASH2X_SECTION_VAL eFlash2xSectionVal)
3068 {
3069         INT SectEndOffset = 0 ;
3070         SectEndOffset = INVALID_OFFSET;
3071
3072         if(IsSectionExistInVendorInfo(Adapter,eFlash2xSectionVal))
3073         {
3074                 return Adapter->psFlash2xVendorInfo->VendorSection[eFlash2xSectionVal].OffsetFromZeroForSectionEnd;
3075         }
3076
3077         switch(eFlash2xSectionVal)
3078         {
3079                 case ISO_IMAGE1 :
3080                          if((Adapter->psFlash2xCSInfo->OffsetISOImage1Part1End!= UNINIT_PTR_IN_CS) &&
3081                                  (IsNonCDLessDevice(Adapter) == FALSE))
3082                                   SectEndOffset = (Adapter->psFlash2xCSInfo->OffsetISOImage1Part1End);
3083                            break;
3084                 case ISO_IMAGE2 :
3085                         if((Adapter->psFlash2xCSInfo->OffsetISOImage2Part1End!= UNINIT_PTR_IN_CS) &&
3086                                 (IsNonCDLessDevice(Adapter) == FALSE))
3087                                         SectEndOffset = (Adapter->psFlash2xCSInfo->OffsetISOImage2Part1End);
3088                          break;
3089                 case DSD0 :
3090                         if(Adapter->psFlash2xCSInfo->OffsetFromZeroForDSDEnd != UNINIT_PTR_IN_CS)
3091                                 SectEndOffset = (Adapter->psFlash2xCSInfo->OffsetFromZeroForDSDEnd);
3092                         break;
3093                 case DSD1 :
3094                         if(Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD1End != UNINIT_PTR_IN_CS)
3095                                 SectEndOffset = (Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD1End);
3096                         break;
3097                 case DSD2 :
3098                         if(Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD2End != UNINIT_PTR_IN_CS)
3099                                 SectEndOffset = (Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD2End);
3100                         break;
3101                 case VSA0 :
3102                         if(Adapter->psFlash2xCSInfo->OffsetFromZeroForVSAEnd != UNINIT_PTR_IN_CS)
3103                                 SectEndOffset = (Adapter->psFlash2xCSInfo->OffsetFromZeroForVSAEnd);
3104                         break;
3105                 case VSA1 :
3106                         if(Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA1End != UNINIT_PTR_IN_CS)
3107                                 SectEndOffset = (Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA1End);
3108                         break;
3109                 case VSA2 :
3110                         if(Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA2End != UNINIT_PTR_IN_CS)
3111                                 SectEndOffset = (Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA2End);
3112                         break;
3113                 case SCSI :
3114                         if(Adapter->psFlash2xCSInfo->OffsetFromZeroForScsiFirmware != UNINIT_PTR_IN_CS)
3115                                 SectEndOffset = ((Adapter->psFlash2xCSInfo->OffsetFromZeroForScsiFirmware) +
3116                                         (Adapter->psFlash2xCSInfo->SizeOfScsiFirmware));
3117                         break;
3118                 case CONTROL_SECTION :
3119                                 //Not Clear So Putting failure. confirm and fix it.
3120                                 SectEndOffset = STATUS_FAILURE;
3121                 case ISO_IMAGE1_PART2 :
3122                                 if(Adapter->psFlash2xCSInfo->OffsetISOImage1Part2End!= UNINIT_PTR_IN_CS)
3123                                          SectEndOffset =  (Adapter->psFlash2xCSInfo->OffsetISOImage1Part2End);
3124                                  break;
3125                 case ISO_IMAGE1_PART3 :
3126                                 if(Adapter->psFlash2xCSInfo->OffsetISOImage1Part3End!= UNINIT_PTR_IN_CS)
3127                                   SectEndOffset =  (Adapter->psFlash2xCSInfo->OffsetISOImage1Part3End);
3128                                 break;
3129                 case ISO_IMAGE2_PART2 :
3130                                 if(Adapter->psFlash2xCSInfo->OffsetISOImage2Part2End != UNINIT_PTR_IN_CS)
3131                                          SectEndOffset =  (Adapter->psFlash2xCSInfo->OffsetISOImage2Part2End);
3132                             break;
3133                 case ISO_IMAGE2_PART3 :
3134                           if(Adapter->psFlash2xCSInfo->OffsetISOImage2Part3End!= UNINIT_PTR_IN_CS)
3135                                   SectEndOffset =  (Adapter->psFlash2xCSInfo->OffsetISOImage2Part3End);
3136                           break;
3137
3138                 default :
3139                         SectEndOffset = INVALID_OFFSET;
3140         }
3141         return SectEndOffset ;
3142 }
3143
3144 /*
3145 *       BcmFlash2xBulkRead:- Read API for Flash Map 2.x .
3146 *       @Adapter :Driver Private Data Structure
3147 *       @pBuffer : Buffer where data has to be put after reading
3148 *       @eFlashSectionVal :Flash Section Val defined in FLASH2X_SECTION_VAL
3149 *       @uiOffsetWithinSectionVal :- Offset with in provided section
3150 *       @uiNumBytes : Number of Bytes for Read
3151 *
3152 *       Return value:-
3153 *               return true on sucess and STATUS_FAILURE on fail.
3154 */
3155
3156 INT BcmFlash2xBulkRead(
3157         PMINI_ADAPTER Adapter,
3158         PUINT pBuffer,
3159         FLASH2X_SECTION_VAL eFlash2xSectionVal,
3160         UINT uiOffsetWithinSectionVal,
3161         UINT uiNumBytes)
3162 {
3163
3164         INT Status = STATUS_SUCCESS;
3165         INT SectionStartOffset = 0;
3166         UINT uiAbsoluteOffset = 0 ;
3167         UINT uiTemp =0, value =0 ;
3168         if(Adapter == NULL)
3169         {
3170                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Adapter structure is NULL");
3171                 return -EINVAL;
3172         }
3173         if(Adapter->device_removed )
3174         {
3175                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Device has been removed");
3176                 return -ENODEV;
3177         }
3178
3179         //NO_SECTION_VAL means absolute offset is given.
3180         if(eFlash2xSectionVal == NO_SECTION_VAL)
3181                 SectionStartOffset = 0;
3182         else
3183                 SectionStartOffset = BcmGetSectionValStartOffset(Adapter,eFlash2xSectionVal);
3184
3185         if(SectionStartOffset == STATUS_FAILURE )
3186         {
3187                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"This Section<%d> does not exixt in Flash 2.x Map ",eFlash2xSectionVal);
3188                 return -EINVAL;
3189         }
3190
3191         if(IsSectionExistInVendorInfo(Adapter,eFlash2xSectionVal))
3192                 return vendorextnReadSection(Adapter,(PUCHAR)pBuffer, eFlash2xSectionVal, uiOffsetWithinSectionVal, uiNumBytes);
3193
3194         //calculating  the absolute offset from FLASH;
3195         uiAbsoluteOffset = uiOffsetWithinSectionVal + SectionStartOffset;
3196         rdmalt(Adapter, 0x0f000C80, &uiTemp, sizeof(uiTemp));
3197         value = 0;
3198         wrmalt(Adapter, 0x0f000C80,&value, sizeof(value));
3199
3200         Status= BeceemFlashBulkRead(Adapter, pBuffer,uiAbsoluteOffset,uiNumBytes) ;
3201
3202         wrmalt(Adapter, 0x0f000C80, &uiTemp, sizeof(uiTemp));
3203         if(Status)
3204         {
3205                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Flash Read Failed with Status :%d", Status);
3206                 return Status ;
3207         }
3208
3209         return Status;
3210 }
3211
3212 /*
3213 *       BcmFlash2xBulkWrite :-API for Writing on the Flash Map 2.x.
3214 *       @Adapter :Driver Private Data Structure
3215 *       @pBuffer : Buffer From where data has to taken for writing
3216 *       @eFlashSectionVal :Flash Section Val defined in FLASH2X_SECTION_VAL
3217 *       @uiOffsetWithinSectionVal :- Offset with in provided section
3218 *       @uiNumBytes : Number of Bytes for Write
3219 *
3220 *       Return value:-
3221 *               return true on sucess and STATUS_FAILURE on fail.
3222 *
3223 */
3224
3225 INT BcmFlash2xBulkWrite(
3226         PMINI_ADAPTER Adapter,
3227         PUINT pBuffer,
3228         FLASH2X_SECTION_VAL eFlash2xSectVal,
3229         UINT uiOffset,
3230         UINT uiNumBytes,
3231         UINT bVerify)
3232 {
3233
3234         INT Status      = STATUS_SUCCESS;
3235         UINT FlashSectValStartOffset = 0;
3236         UINT uiTemp = 0, value = 0;
3237         if(Adapter == NULL)
3238         {
3239                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Adapter structure is NULL");
3240                 return -EINVAL;
3241         }
3242         if(Adapter->device_removed )
3243         {
3244                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Device has been removed");
3245                 return -ENODEV;
3246         }
3247
3248         //NO_SECTION_VAL means absolute offset is given.
3249         if(eFlash2xSectVal == NO_SECTION_VAL)
3250                 FlashSectValStartOffset = 0;
3251         else
3252                 FlashSectValStartOffset = BcmGetSectionValStartOffset(Adapter,eFlash2xSectVal);
3253
3254         if(FlashSectValStartOffset == STATUS_FAILURE )
3255         {
3256                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"This Section<%d> does not exixt in Flash Map 2.x",eFlash2xSectVal);
3257                 return -EINVAL;
3258         }
3259
3260         if(IsSectionExistInVendorInfo(Adapter,eFlash2xSectVal))
3261                 return vendorextnWriteSection(Adapter, (PUCHAR)pBuffer, eFlash2xSectVal, uiOffset, uiNumBytes, bVerify);
3262
3263         //calculating  the absolute offset from FLASH;
3264         uiOffset = uiOffset + FlashSectValStartOffset;
3265
3266         rdmalt(Adapter, 0x0f000C80, &uiTemp, sizeof(uiTemp));
3267         value = 0;
3268         wrmalt(Adapter, 0x0f000C80,&value, sizeof(value));
3269
3270         Status = BeceemFlashBulkWrite(Adapter, pBuffer,uiOffset,uiNumBytes,bVerify);
3271
3272         wrmalt(Adapter, 0x0f000C80, &uiTemp, sizeof(uiTemp));
3273         if(Status)
3274         {
3275                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Flash Write failed with Status :%d", Status);
3276                 return Status ;
3277         }
3278
3279         return Status;
3280
3281 }
3282
3283 /**
3284 *       ReadDSDHeader : Read the DSD map for the DSD Section val provided in Argument.
3285 *       @Adapter : Beceem Private Data Structure
3286 *       @psDSDHeader :Pointer of the buffer where header has to be read
3287 *       @dsd :value of the Dyanmic DSD like DSD0 of DSD1 or DSD2
3288 *
3289 *       Return Value:-
3290 *               if suceeds return STATUS_SUCCESS or negative error code.
3291 **/
3292 INT ReadDSDHeader(PMINI_ADAPTER Adapter, PDSD_HEADER psDSDHeader, FLASH2X_SECTION_VAL dsd)
3293 {
3294         INT Status = STATUS_SUCCESS;
3295
3296         Status =BcmFlash2xBulkRead(Adapter,
3297                                                     (PUINT)psDSDHeader,
3298                                                         dsd,
3299                                                         Adapter->psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader,
3300                                                         sizeof(DSD_HEADER));
3301         if(Status == STATUS_SUCCESS)
3302         {
3303                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "DSDImageMagicNumber :0X%x", ntohl(psDSDHeader->DSDImageMagicNumber));
3304                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "DSDImageSize :0X%x ",ntohl(psDSDHeader->DSDImageSize));
3305                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "DSDImageCRC :0X%x",ntohl(psDSDHeader->DSDImageCRC));
3306                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "DSDImagePriority :0X%x",ntohl(psDSDHeader->DSDImagePriority));
3307         }
3308         else
3309         {
3310                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"DSD Header read is failed with status :%d", Status);
3311         }
3312
3313         return Status;
3314 }
3315
3316 /**
3317 *       BcmGetActiveDSD : Set the Active DSD in Adapter Structure which has to be dumped in DDR
3318 *       @Adapter :-Drivers private Data Structure
3319 *
3320 *       Return Value:-
3321 *               Return STATUS_SUCESS if get sucess in setting the right DSD else negaive error code
3322 *
3323 **/
3324 INT BcmGetActiveDSD(PMINI_ADAPTER Adapter)
3325 {
3326         FLASH2X_SECTION_VAL uiHighestPriDSD = 0 ;
3327
3328         uiHighestPriDSD = getHighestPriDSD(Adapter);
3329         Adapter->eActiveDSD = uiHighestPriDSD;
3330
3331         if(DSD0  == uiHighestPriDSD)
3332                 Adapter->ulFlashCalStart = Adapter->psFlash2xCSInfo->OffsetFromZeroForDSDStart;
3333         if(DSD1 == uiHighestPriDSD)
3334                 Adapter->ulFlashCalStart = Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD1Start;
3335         if(DSD2 == uiHighestPriDSD)
3336                 Adapter->ulFlashCalStart = Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD2Start;
3337         if(Adapter->eActiveDSD)
3338                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "Active DSD :%d", Adapter->eActiveDSD);
3339         if(Adapter->eActiveDSD == 0)
3340         {
3341                 //if No DSD gets Active, Make Active the DSD with WR  permission
3342                 if(IsSectionWritable(Adapter,DSD2))
3343                 {
3344                         Adapter->eActiveDSD = DSD2;
3345                         Adapter->ulFlashCalStart = Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD2Start;
3346                 }
3347                 else if(IsSectionWritable(Adapter,DSD1))
3348                 {
3349                         Adapter->eActiveDSD = DSD1;
3350                         Adapter->ulFlashCalStart = Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD1Start;
3351                 }
3352                 else if(IsSectionWritable(Adapter,DSD0))
3353                 {
3354                         Adapter->eActiveDSD = DSD0;
3355                         Adapter->ulFlashCalStart = Adapter->psFlash2xCSInfo->OffsetFromZeroForDSDStart;
3356                 }
3357         }
3358
3359         return STATUS_SUCCESS;
3360 }
3361
3362 /**
3363 *       ReadISOUnReservedBytes : Read the ISO map for the ISO Section val provided in Argument.
3364 *       @Adapter : Driver Private Data Structure
3365 *       @psISOHeader :Pointer of the location where header has to be read
3366 *       @IsoImage :value of the Dyanmic ISO like ISO_IMAGE1 of ISO_IMAGE2
3367 *
3368 *       Return Value:-
3369 *               if suceeds return STATUS_SUCCESS or negative error code.
3370 **/
3371
3372 INT ReadISOHeader(PMINI_ADAPTER Adapter, PISO_HEADER psISOHeader, FLASH2X_SECTION_VAL IsoImage)
3373 {
3374         INT Status = STATUS_SUCCESS;
3375
3376         Status = BcmFlash2xBulkRead(Adapter,
3377                                             (PUINT)psISOHeader,
3378                                                 IsoImage,
3379                                                 0,
3380                                                 sizeof(ISO_HEADER));
3381
3382         if(Status == STATUS_SUCCESS)
3383         {
3384                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "ISOImageMagicNumber :0X%x", ntohl(psISOHeader->ISOImageMagicNumber));
3385                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "ISOImageSize :0X%x ",ntohl(psISOHeader->ISOImageSize));
3386                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "ISOImageCRC :0X%x",ntohl(psISOHeader->ISOImageCRC));
3387                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "ISOImagePriority :0X%x",ntohl(psISOHeader->ISOImagePriority));
3388         }
3389         else
3390         {
3391                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "ISO Header Read failed");
3392         }
3393         return Status;
3394 }
3395
3396 /**
3397 *       BcmGetActiveISO :- Set the Active ISO in Adapter Data Structue
3398 *       @Adapter : Driver private Data Structure
3399 *
3400 *       Return Value:-
3401 *               Sucsess:- STATUS_SUCESS
3402 *               Failure- : negative erro code
3403 *
3404 **/
3405
3406 INT BcmGetActiveISO(PMINI_ADAPTER Adapter)
3407 {
3408
3409         INT HighestPriISO = 0 ;
3410         HighestPriISO = getHighestPriISO(Adapter);
3411
3412         Adapter->eActiveISO = HighestPriISO ;
3413         if(Adapter->eActiveISO == ISO_IMAGE2)
3414                 Adapter->uiActiveISOOffset = (Adapter->psFlash2xCSInfo->OffsetISOImage2Part1Start);
3415         else if(Adapter->eActiveISO == ISO_IMAGE1)
3416                 Adapter->uiActiveISOOffset = (Adapter->psFlash2xCSInfo->OffsetISOImage1Part1Start);
3417
3418         if(Adapter->eActiveISO)
3419                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Active ISO :%x", Adapter->eActiveISO);
3420
3421         return STATUS_SUCCESS;
3422 }
3423
3424 /**
3425 *       IsOffsetWritable :- it will tell the access permission of the sector having passed offset
3426 *       @Adapter : Drivers Private Data Structure
3427 *       @uiOffset : Offset provided in the Flash
3428 *
3429 *       Return Value:-
3430 *       Sucess:-TRUE ,  offset is writable
3431 *       Failure:-FALSE, offset is RO
3432 *
3433 **/
3434 B_UINT8 IsOffsetWritable(PMINI_ADAPTER Adapter, UINT uiOffset)
3435 {
3436         UINT uiSectorNum = 0;
3437         UINT uiWordOfSectorPermission =0;
3438         UINT uiBitofSectorePermission = 0;
3439         B_UINT32 permissionBits = 0;
3440         uiSectorNum = uiOffset/Adapter->uiSectorSize;
3441
3442         //calculating the word having this Sector Access permission from SectorAccessBitMap Array
3443         uiWordOfSectorPermission = Adapter->psFlash2xCSInfo->SectorAccessBitMap[uiSectorNum /16];
3444
3445         //calculating the bit index inside the word for  this sector
3446         uiBitofSectorePermission = 2*(15 - uiSectorNum %16);
3447
3448         //Setting Access permission
3449         permissionBits = uiWordOfSectorPermission & (0x3 << uiBitofSectorePermission) ;
3450         permissionBits = (permissionBits >> uiBitofSectorePermission) & 0x3;
3451         if(permissionBits == SECTOR_READWRITE_PERMISSION)
3452                 return  TRUE;
3453         else
3454                 return FALSE;
3455 }
3456
3457 static INT BcmDumpFlash2xSectionBitMap(PFLASH2X_BITMAP psFlash2xBitMap)
3458 {
3459     PMINI_ADAPTER Adapter = GET_BCM_ADAPTER(gblpnetdev);
3460         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "***************Flash 2.x Section Bitmap***************");
3461         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"ISO_IMAGE1  :0X%x", psFlash2xBitMap->ISO_IMAGE1);
3462         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"ISO_IMAGE2  :0X%x", psFlash2xBitMap->ISO_IMAGE2);
3463         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"DSD0  :0X%x", psFlash2xBitMap->DSD0);
3464         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"DSD1  :0X%x", psFlash2xBitMap->DSD1);
3465         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"DSD2  :0X%x", psFlash2xBitMap->DSD2);
3466         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"VSA0  :0X%x", psFlash2xBitMap->VSA0);
3467         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"VSA1  :0X%x", psFlash2xBitMap->VSA1);
3468         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"VSA2  :0X%x", psFlash2xBitMap->VSA2);
3469         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"SCSI  :0X%x", psFlash2xBitMap->SCSI);
3470         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"CONTROL_SECTION  :0X%x", psFlash2xBitMap->CONTROL_SECTION);
3471
3472         return STATUS_SUCCESS;
3473 }
3474
3475 /**
3476 *       BcmGetFlash2xSectionalBitMap :- It will provide the bit map of all the section present in Flash
3477 *       8bit has been assigned to every section.
3478         bit[0] :Section present or not
3479         bit[1] :section is valid or not
3480         bit[2] : Secton is read only or has write permission too.
3481         bit[3] : Active Section -
3482         bit[7...4] = Reserved .
3483
3484         @Adapter:-Driver private Data Structure
3485 *
3486 *       Return value:-
3487 *       Sucess:- STATUS_SUCESS
3488 *       Failure:- negative error code
3489 **/
3490
3491 INT BcmGetFlash2xSectionalBitMap(PMINI_ADAPTER Adapter, PFLASH2X_BITMAP psFlash2xBitMap)
3492 {
3493
3494
3495         PFLASH2X_CS_INFO psFlash2xCSInfo = Adapter->psFlash2xCSInfo;
3496         FLASH2X_SECTION_VAL uiHighestPriDSD = 0 ;
3497         FLASH2X_SECTION_VAL uiHighestPriISO= 0 ;
3498         BOOLEAN SetActiveDSDDone = FALSE ;
3499         BOOLEAN SetActiveISODone = FALSE ;
3500
3501         //For 1.x map all the section except DSD0 will be shown as not present
3502         //This part will be used by calibration tool to detect the number of DSD present in Flash.
3503         if(IsFlash2x(Adapter) == FALSE)
3504         {
3505                 psFlash2xBitMap->ISO_IMAGE2 = 0;
3506                 psFlash2xBitMap->ISO_IMAGE1 = 0;
3507                 psFlash2xBitMap->DSD0 = FLASH2X_SECTION_VALID | FLASH2X_SECTION_ACT | FLASH2X_SECTION_PRESENT; //0xF;   //0000(Reseved)1(Active)0(RW)1(valid)1(present)
3508                 psFlash2xBitMap->DSD1  = 0 ;
3509                 psFlash2xBitMap->DSD2 = 0 ;
3510                 psFlash2xBitMap->VSA0 = 0 ;
3511                 psFlash2xBitMap->VSA1 = 0 ;
3512                 psFlash2xBitMap->VSA2 = 0 ;
3513                 psFlash2xBitMap->CONTROL_SECTION = 0 ;
3514                 psFlash2xBitMap->SCSI= 0 ;
3515                 psFlash2xBitMap->Reserved0 = 0 ;
3516                 psFlash2xBitMap->Reserved1 = 0 ;
3517                 psFlash2xBitMap->Reserved2 = 0 ;
3518                 return STATUS_SUCCESS ;
3519
3520         }
3521
3522         uiHighestPriDSD = getHighestPriDSD(Adapter);
3523         uiHighestPriISO = getHighestPriISO(Adapter);
3524
3525         ///
3526         //      IS0 IMAGE 2
3527         ///
3528         if((psFlash2xCSInfo->OffsetISOImage2Part1Start) != UNINIT_PTR_IN_CS)
3529         {
3530                 //Setting the 0th Bit representing the Section is present or not.
3531                 psFlash2xBitMap->ISO_IMAGE2= psFlash2xBitMap->ISO_IMAGE2 | FLASH2X_SECTION_PRESENT;
3532
3533
3534                 if(ReadISOSignature(Adapter,ISO_IMAGE2)== ISO_IMAGE_MAGIC_NUMBER)
3535                         psFlash2xBitMap->ISO_IMAGE2 |= FLASH2X_SECTION_VALID;
3536
3537
3538                 //Calculation for extrating the Access permission
3539                 if(IsSectionWritable(Adapter, ISO_IMAGE2) == FALSE)
3540                         psFlash2xBitMap->ISO_IMAGE2 |= FLASH2X_SECTION_RO;
3541
3542                 if(SetActiveISODone == FALSE && uiHighestPriISO == ISO_IMAGE2)
3543                 {
3544                         psFlash2xBitMap->ISO_IMAGE2 |= FLASH2X_SECTION_ACT ;
3545                         SetActiveISODone = TRUE;
3546                 }
3547
3548         }
3549
3550         ///
3551         //      IS0 IMAGE 1
3552         ///
3553         if((psFlash2xCSInfo->OffsetISOImage1Part1Start) != UNINIT_PTR_IN_CS)
3554         {
3555                 //Setting the 0th Bit representing the Section is present or not.
3556                 psFlash2xBitMap->ISO_IMAGE1 = psFlash2xBitMap->ISO_IMAGE1 | FLASH2X_SECTION_PRESENT;
3557
3558                 if(ReadISOSignature(Adapter,ISO_IMAGE1) == ISO_IMAGE_MAGIC_NUMBER)
3559                         psFlash2xBitMap->ISO_IMAGE1 |= FLASH2X_SECTION_VALID;
3560
3561                 //      Calculation for extrating the Access permission
3562                 if(IsSectionWritable(Adapter, ISO_IMAGE1) == FALSE)
3563                         psFlash2xBitMap->ISO_IMAGE1 |= FLASH2X_SECTION_RO;
3564
3565                 if(SetActiveISODone == FALSE && uiHighestPriISO == ISO_IMAGE1)
3566                 {
3567                         psFlash2xBitMap->ISO_IMAGE1 |= FLASH2X_SECTION_ACT ;
3568                         SetActiveISODone = TRUE;
3569                 }
3570         }
3571
3572
3573
3574         ///
3575         // DSD2
3576         ///
3577         if((psFlash2xCSInfo->OffsetFromZeroForDSD2Start) != UNINIT_PTR_IN_CS)
3578         {
3579                 //Setting the 0th Bit representing the Section is present or not.
3580                 psFlash2xBitMap->DSD2= psFlash2xBitMap->DSD2 | FLASH2X_SECTION_PRESENT;
3581
3582                 if(ReadDSDSignature(Adapter,DSD2)== DSD_IMAGE_MAGIC_NUMBER)
3583                         psFlash2xBitMap->DSD2 |= FLASH2X_SECTION_VALID;
3584
3585                 //Calculation for extrating the Access permission
3586                 if(IsSectionWritable(Adapter, DSD2) == FALSE)
3587                 {
3588                         psFlash2xBitMap->DSD2 |= FLASH2X_SECTION_RO;
3589
3590                 }
3591                 else
3592                 {
3593                         //Means section is writable
3594                         if((SetActiveDSDDone == FALSE) && (uiHighestPriDSD == DSD2))
3595                         {
3596                                 psFlash2xBitMap->DSD2 |= FLASH2X_SECTION_ACT ;
3597                                 SetActiveDSDDone =TRUE ;
3598                         }
3599                 }
3600         }
3601
3602         ///
3603         //      DSD 1
3604         ///
3605         if((psFlash2xCSInfo->OffsetFromZeroForDSD1Start) != UNINIT_PTR_IN_CS)
3606         {
3607                 //Setting the 0th Bit representing the Section is present or not.
3608                 psFlash2xBitMap->DSD1= psFlash2xBitMap->DSD1 | FLASH2X_SECTION_PRESENT;
3609
3610
3611                 if(ReadDSDSignature(Adapter,DSD1)== DSD_IMAGE_MAGIC_NUMBER)
3612                         psFlash2xBitMap->DSD1 |= FLASH2X_SECTION_VALID;
3613
3614                 //Calculation for extrating the Access permission
3615                 if(IsSectionWritable(Adapter, DSD1) == FALSE)
3616                 {
3617                         psFlash2xBitMap->DSD1 |= FLASH2X_SECTION_RO;
3618                 }
3619                 else
3620                 {
3621                         //Means section is writable
3622                         if((SetActiveDSDDone == FALSE) && (uiHighestPriDSD == DSD1))
3623                         {
3624                                         psFlash2xBitMap->DSD1 |= FLASH2X_SECTION_ACT ;
3625                                         SetActiveDSDDone =TRUE ;
3626                         }
3627                 }
3628
3629         }
3630
3631         ///
3632         //For DSD 0
3633         //
3634         if((psFlash2xCSInfo->OffsetFromZeroForDSDStart) != UNINIT_PTR_IN_CS)
3635         {
3636                 //Setting the 0th Bit representing the Section is present or not.
3637                 psFlash2xBitMap->DSD0 = psFlash2xBitMap->DSD0 | FLASH2X_SECTION_PRESENT;
3638
3639                 if(ReadDSDSignature(Adapter,DSD0) == DSD_IMAGE_MAGIC_NUMBER)
3640                         psFlash2xBitMap->DSD0 |= FLASH2X_SECTION_VALID;
3641
3642                 //Setting Access permission
3643                 if(IsSectionWritable(Adapter, DSD0) == FALSE)
3644                 {
3645                         psFlash2xBitMap->DSD0 |= FLASH2X_SECTION_RO;
3646                 }
3647                 else
3648                 {
3649                         //Means section is writable
3650                         if((SetActiveDSDDone == FALSE) &&(uiHighestPriDSD == DSD0))
3651                         {
3652                                         psFlash2xBitMap->DSD0 |= FLASH2X_SECTION_ACT ;
3653                                         SetActiveDSDDone =TRUE ;
3654                         }
3655                 }
3656         }
3657
3658         ///
3659         //      VSA 0
3660         ///
3661         if((psFlash2xCSInfo->OffsetFromZeroForVSAStart) != UNINIT_PTR_IN_CS)
3662         {
3663                 //Setting the 0th Bit representing the Section is present or not.
3664                 psFlash2xBitMap->VSA0= psFlash2xBitMap->VSA0 | FLASH2X_SECTION_PRESENT;
3665
3666                 //Setting the Access Bit. Map is not defined hece setting it always valid
3667                 psFlash2xBitMap->VSA0 |= FLASH2X_SECTION_VALID;
3668
3669                 //Calculation for extrating the Access permission
3670                 if(IsSectionWritable(Adapter, VSA0) == FALSE)
3671                         psFlash2xBitMap->VSA0 |=  FLASH2X_SECTION_RO;
3672
3673                 //By Default section is Active
3674                 psFlash2xBitMap->VSA0 |= FLASH2X_SECTION_ACT ;
3675
3676         }
3677
3678
3679         ///
3680         //       VSA 1
3681         ///
3682
3683         if((psFlash2xCSInfo->OffsetFromZeroForVSA1Start) != UNINIT_PTR_IN_CS)
3684         {
3685                 //Setting the 0th Bit representing the Section is present or not.
3686                 psFlash2xBitMap->VSA1= psFlash2xBitMap->VSA1 | FLASH2X_SECTION_PRESENT;
3687
3688                 //Setting the Access Bit. Map is not defined hece setting it always valid
3689                 psFlash2xBitMap->VSA1|= FLASH2X_SECTION_VALID;
3690
3691                 //Checking For Access permission
3692                 if(IsSectionWritable(Adapter, VSA1) == FALSE)
3693                         psFlash2xBitMap->VSA1 |= FLASH2X_SECTION_RO;
3694
3695                 //By Default section is Active
3696                 psFlash2xBitMap->VSA1 |= FLASH2X_SECTION_ACT ;
3697
3698         }
3699
3700
3701         ///
3702         //      VSA 2
3703         ///
3704
3705         if((psFlash2xCSInfo->OffsetFromZeroForVSA2Start) != UNINIT_PTR_IN_CS)
3706         {
3707                 //Setting the 0th Bit representing the Section is present or not.
3708                 psFlash2xBitMap->VSA2= psFlash2xBitMap->VSA2 | FLASH2X_SECTION_PRESENT;
3709
3710
3711                 //Setting the Access Bit. Map is not defined hece setting it always valid
3712                 psFlash2xBitMap->VSA2 |= FLASH2X_SECTION_VALID;
3713
3714                 //Checking For Access permission
3715                 if(IsSectionWritable(Adapter, VSA2) == FALSE)
3716                         psFlash2xBitMap->VSA2 |= FLASH2X_SECTION_RO;
3717
3718                 //By Default section is Active
3719                 psFlash2xBitMap->VSA2 |= FLASH2X_SECTION_ACT ;
3720         }
3721
3722         ///
3723         // SCSI Section
3724         ///
3725         if((psFlash2xCSInfo->OffsetFromZeroForScsiFirmware) != UNINIT_PTR_IN_CS)
3726         {
3727                 //Setting the 0th Bit representing the Section is present or not.
3728                 psFlash2xBitMap->SCSI= psFlash2xBitMap->SCSI | FLASH2X_SECTION_PRESENT;
3729
3730
3731                 //Setting the Access Bit. Map is not defined hece setting it always valid
3732                 psFlash2xBitMap->SCSI|= FLASH2X_SECTION_VALID;
3733
3734                 //Checking For Access permission
3735                 if(IsSectionWritable(Adapter, SCSI) == FALSE)
3736                         psFlash2xBitMap->SCSI |= FLASH2X_SECTION_RO;
3737
3738                 //By Default section is Active
3739                 psFlash2xBitMap->SCSI |= FLASH2X_SECTION_ACT ;
3740
3741         }
3742
3743
3744         ///
3745         //      Control Section
3746         ///
3747         if((psFlash2xCSInfo->OffsetFromZeroForControlSectionStart) != UNINIT_PTR_IN_CS)
3748         {
3749                 //Setting the 0th Bit representing the Section is present or not.
3750                 psFlash2xBitMap->CONTROL_SECTION = psFlash2xBitMap->CONTROL_SECTION | (FLASH2X_SECTION_PRESENT);
3751
3752
3753                 //Setting the Access Bit. Map is not defined hece setting it always valid
3754                 psFlash2xBitMap->CONTROL_SECTION |= FLASH2X_SECTION_VALID;
3755
3756                 //Checking For Access permission
3757                 if(IsSectionWritable(Adapter, CONTROL_SECTION) == FALSE)
3758                         psFlash2xBitMap->CONTROL_SECTION |= FLASH2X_SECTION_RO;
3759
3760                 //By Default section is Active
3761                 psFlash2xBitMap->CONTROL_SECTION |= FLASH2X_SECTION_ACT ;
3762
3763         }
3764
3765         ///
3766         //      For Reserved Sections
3767         ///
3768         psFlash2xBitMap->Reserved0 = 0;
3769         psFlash2xBitMap->Reserved0 = 0;
3770         psFlash2xBitMap->Reserved0 = 0;
3771
3772         BcmDumpFlash2xSectionBitMap(psFlash2xBitMap);
3773
3774         return STATUS_SUCCESS ;
3775
3776 }
3777 /**
3778 BcmSetActiveSection :- Set Active section is used to make priority field highest over other
3779                                         section of same type.
3780
3781 @Adapater :- Bcm Driver Private Data Structure
3782 @eFlash2xSectionVal :- Flash section val whose priority has to be made highest.
3783
3784 Return Value:- Make the priorit highest else return erorr code
3785
3786 **/
3787 INT BcmSetActiveSection(PMINI_ADAPTER Adapter, FLASH2X_SECTION_VAL eFlash2xSectVal)
3788 {
3789         unsigned int SectImagePriority = 0;
3790         INT Status =STATUS_SUCCESS;
3791
3792         //DSD_HEADER sDSD = {0};
3793         //ISO_HEADER sISO = {0};
3794         INT HighestPriDSD = 0 ;
3795         INT HighestPriISO = 0;
3796
3797
3798
3799         Status = IsSectionWritable(Adapter,eFlash2xSectVal) ;
3800         if(Status != TRUE )
3801         {
3802                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Provided Section <%d> is not writable",eFlash2xSectVal);
3803                 return STATUS_FAILURE;
3804         }
3805
3806         Adapter->bHeaderChangeAllowed = TRUE ;
3807         switch(eFlash2xSectVal)
3808         {
3809                 case ISO_IMAGE1 :
3810                 case ISO_IMAGE2 :
3811                         if(ReadISOSignature(Adapter,eFlash2xSectVal)== ISO_IMAGE_MAGIC_NUMBER )
3812                         {
3813                                 HighestPriISO = getHighestPriISO(Adapter);
3814
3815                                 if(HighestPriISO == eFlash2xSectVal     )
3816                                 {
3817                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Given ISO<%x> already  has highest priority",eFlash2xSectVal );
3818                                         Status = STATUS_SUCCESS ;
3819                                         break;
3820                                 }
3821
3822                                 SectImagePriority = ReadISOPriority(Adapter, HighestPriISO) + 1;
3823
3824                                 if((SectImagePriority <= 0) && IsSectionWritable(Adapter,HighestPriISO))
3825                                 {
3826                                         // This is a SPECIAL Case which will only happen if the current highest priority ISO has priority value = 0x7FFFFFFF.
3827                                         // We will write 1 to the current Highest priority ISO And then shall increase the priority of the requested ISO
3828                                         // by user
3829                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "SectImagePriority wraparound happend, eFlash2xSectVal: 0x%x\n",eFlash2xSectVal);
3830                                         SectImagePriority = htonl(0x1);
3831                                         Status = BcmFlash2xBulkWrite(Adapter,
3832                                                                 &SectImagePriority,
3833                                                                 HighestPriISO,
3834                                                                 0 + FIELD_OFFSET_IN_HEADER(PISO_HEADER, ISOImagePriority),
3835                                                                 SIGNATURE_SIZE,
3836                                                                 TRUE);
3837
3838                                         if(Status)
3839                                         {
3840                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Priority has not been written properly");
3841                                                 Status = STATUS_FAILURE;
3842                                                 break ;
3843                                         }
3844
3845                                         HighestPriISO = getHighestPriISO(Adapter);
3846
3847                                         if(HighestPriISO == eFlash2xSectVal     )
3848                                         {
3849                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Given ISO<%x> already  has highest priority",eFlash2xSectVal );
3850                                                 Status = STATUS_SUCCESS ;
3851                                                 break;
3852                                         }
3853
3854                                         SectImagePriority = 2;
3855                                  }
3856
3857
3858                                 SectImagePriority = htonl(SectImagePriority);
3859
3860                                 Status = BcmFlash2xBulkWrite(Adapter,
3861                                                                 &SectImagePriority,
3862                                                                 eFlash2xSectVal,
3863                                                                 0 + FIELD_OFFSET_IN_HEADER(PISO_HEADER, ISOImagePriority),
3864                                                                 SIGNATURE_SIZE,
3865                                                                 TRUE);
3866                                 if(Status)
3867                                 {
3868                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Priority has not been written properly");
3869                                         break ;
3870                                 }
3871                         }
3872                         else
3873                         {
3874                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Signature is currupted. Hence can't increase the priority");
3875                                 Status = STATUS_FAILURE ;
3876                                 break;
3877                         }
3878                         break;
3879                 case DSD0 :
3880                 case DSD1 :
3881                 case DSD2 :
3882                         if(ReadDSDSignature(Adapter,eFlash2xSectVal)== DSD_IMAGE_MAGIC_NUMBER)
3883                         {
3884                                 HighestPriDSD = getHighestPriDSD(Adapter);
3885
3886                                 if((HighestPriDSD == eFlash2xSectVal))
3887                                 {
3888                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Given DSD<%x> already  has highest priority", eFlash2xSectVal);
3889                                         Status = STATUS_SUCCESS ;
3890                                         break;
3891                                 }
3892
3893                                 SectImagePriority = ReadDSDPriority(Adapter, HighestPriDSD) + 1 ;
3894                                 if(SectImagePriority <= 0)
3895                                 {
3896                                         // This is a SPECIAL Case which will only happen if the current highest priority DSD has priority value = 0x7FFFFFFF.
3897                                         // We will write 1 to the current Highest priority DSD And then shall increase the priority of the requested DSD
3898                                         // by user
3899                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, NVM_RW, DBG_LVL_ALL, "SectImagePriority wraparound happend, eFlash2xSectVal: 0x%x\n",eFlash2xSectVal);
3900                                         SectImagePriority = htonl(0x1);
3901
3902                                         Status = BcmFlash2xBulkWrite(Adapter,
3903                                                                         &SectImagePriority,
3904                                                                         HighestPriDSD,
3905                                                                         Adapter->psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader + FIELD_OFFSET_IN_HEADER(PDSD_HEADER, DSDImagePriority),
3906                                                                         SIGNATURE_SIZE,
3907                                                                         TRUE);
3908
3909                                         if(Status)
3910                                         {
3911                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Priority has not been written properly");
3912                                                 break ;
3913                                         }
3914
3915                                         HighestPriDSD = getHighestPriDSD(Adapter);
3916
3917                                         if((HighestPriDSD == eFlash2xSectVal))
3918                                         {
3919                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Made the DSD: %x highest by reducing priority of other\n", eFlash2xSectVal);
3920                                                 Status = STATUS_SUCCESS ;
3921                                                 break;
3922                                         }
3923
3924                                         SectImagePriority = htonl(0x2);
3925                                         Status = BcmFlash2xBulkWrite(Adapter,
3926                                                                         &SectImagePriority,
3927                                                                         HighestPriDSD,
3928                                                                         Adapter->psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader + FIELD_OFFSET_IN_HEADER(PDSD_HEADER, DSDImagePriority),
3929                                                                         SIGNATURE_SIZE,
3930                                                                         TRUE);
3931
3932                                         if(Status)
3933                                         {
3934                                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Priority has not been written properly");
3935                                                 break ;
3936                                         }
3937
3938                                         HighestPriDSD = getHighestPriDSD(Adapter);
3939
3940                                         if((HighestPriDSD == eFlash2xSectVal))
3941                                         {
3942                                                 Status = STATUS_SUCCESS ;
3943                                                 break;
3944                                         }
3945                                         SectImagePriority = 3 ;
3946
3947                                 }
3948                                 SectImagePriority = htonl(SectImagePriority);
3949                                 Status = BcmFlash2xBulkWrite(Adapter,
3950                                                                 &SectImagePriority,
3951                                                                 eFlash2xSectVal,
3952                                                                 Adapter->psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader + FIELD_OFFSET_IN_HEADER(PDSD_HEADER, DSDImagePriority),
3953                                                                 SIGNATURE_SIZE ,
3954                                                                 TRUE);
3955                                 if(Status)
3956                                 {
3957                                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Priority has not been written properly");
3958                                         Status = STATUS_FAILURE ;
3959                                         break ;
3960                                 }
3961                         }
3962                         else
3963                         {
3964                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Signature is currupted. Hence can't increase the priority");
3965                                 Status = STATUS_FAILURE ;
3966                                 break;
3967                         }
3968                         break;
3969                 case VSA0 :
3970                 case VSA1 :
3971                 case VSA2 :
3972                         //Has to be decided
3973                         break ;
3974                 default :
3975                                 Status = STATUS_FAILURE ;
3976                                 break;
3977
3978         }
3979
3980         Adapter->bHeaderChangeAllowed = FALSE ;
3981         return Status;
3982
3983 }
3984
3985 /**
3986 BcmCopyISO - Used only for copying the ISO section
3987 @Adapater :- Bcm Driver Private Data Structure
3988 @sCopySectStrut :- Section copy structure
3989
3990 Return value:- SUCCESS if copies successfully else negative error code
3991
3992 **/
3993 INT BcmCopyISO(PMINI_ADAPTER Adapter, FLASH2X_COPY_SECTION sCopySectStrut)
3994 {
3995
3996         PCHAR Buff = NULL;
3997         FLASH2X_SECTION_VAL eISOReadPart = 0,eISOWritePart = 0;
3998         UINT uiReadOffsetWithinPart = 0, uiWriteOffsetWithinPart = 0;
3999         UINT uiTotalDataToCopy = 0;
4000         BOOLEAN IsThisHeaderSector = FALSE ;
4001         UINT sigOffset = 0;
4002         UINT ISOLength = 0;
4003         UINT Status = STATUS_SUCCESS;
4004         UINT SigBuff[MAX_RW_SIZE];
4005         UINT i = 0;
4006
4007         if(ReadISOSignature(Adapter,sCopySectStrut.SrcSection) != ISO_IMAGE_MAGIC_NUMBER)
4008         {
4009                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "error as Source ISO Section does not have valid signature");
4010                 return STATUS_FAILURE;
4011         }
4012
4013         Status = BcmFlash2xBulkRead(Adapter,
4014                                            &ISOLength,
4015                                            sCopySectStrut.SrcSection,
4016                                            0 + FIELD_OFFSET_IN_HEADER(PISO_HEADER,ISOImageSize),
4017                                            4);
4018
4019         if(Status)
4020         {
4021                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Read failed while copying ISO\n");
4022                 return Status;
4023         }
4024
4025         ISOLength = htonl(ISOLength);
4026
4027         if(ISOLength % Adapter->uiSectorSize)
4028         {
4029                 ISOLength = Adapter->uiSectorSize*(1 + ISOLength/Adapter->uiSectorSize);
4030         }
4031
4032         sigOffset = FIELD_OFFSET_IN_HEADER(PISO_HEADER, ISOImageMagicNumber);
4033
4034         Buff = kzalloc(Adapter->uiSectorSize, GFP_KERNEL);
4035
4036         if(Buff == NULL)
4037         {
4038                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Memory allocation failed for section size");
4039                         return -ENOMEM;
4040         }
4041
4042         if(sCopySectStrut.SrcSection ==ISO_IMAGE1 && sCopySectStrut.DstSection ==ISO_IMAGE2)
4043         {
4044                 eISOReadPart = ISO_IMAGE1 ;
4045                 eISOWritePart = ISO_IMAGE2 ;
4046                 uiReadOffsetWithinPart =  0;
4047                 uiWriteOffsetWithinPart = 0 ;
4048
4049                 uiTotalDataToCopy =(Adapter->psFlash2xCSInfo->OffsetISOImage1Part1End) -
4050                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage1Part1Start)+
4051                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage1Part2End) -
4052                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage1Part2Start)+
4053                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage1Part3End) -
4054                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage1Part3Start);
4055
4056                 if(uiTotalDataToCopy < ISOLength)
4057                 {
4058                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"error as Source ISO Section does not have valid signature");
4059                         return STATUS_FAILURE;
4060                 }
4061
4062                 uiTotalDataToCopy =(Adapter->psFlash2xCSInfo->OffsetISOImage2Part1End) -
4063                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage2Part1Start)+
4064                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage2Part2End) -
4065                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage2Part2Start)+
4066                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage2Part3End) -
4067                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage2Part3Start);
4068
4069                 if(uiTotalDataToCopy < ISOLength)
4070                 {
4071                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"error as Dest ISO Section does not have enough section size");
4072                         return STATUS_FAILURE;
4073                 }
4074
4075                 uiTotalDataToCopy = ISOLength;
4076
4077                 CorruptISOSig(Adapter,ISO_IMAGE2);
4078
4079                 while(uiTotalDataToCopy)
4080                 {
4081                         if(uiTotalDataToCopy == Adapter->uiSectorSize)
4082                         {
4083                                 //Setting for write of first sector. First sector is assumed to be written in last
4084                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Writing the signature sector");
4085                                 eISOReadPart = ISO_IMAGE1 ;
4086                                 uiReadOffsetWithinPart = 0;
4087                                 eISOWritePart = ISO_IMAGE2;
4088                                 uiWriteOffsetWithinPart = 0 ;
4089                                 IsThisHeaderSector = TRUE ;
4090
4091                         }
4092                         else
4093                         {
4094                                 uiReadOffsetWithinPart = uiReadOffsetWithinPart + Adapter->uiSectorSize ;
4095                                 uiWriteOffsetWithinPart = uiWriteOffsetWithinPart + Adapter->uiSectorSize ;
4096
4097                                 if((eISOReadPart == ISO_IMAGE1) && (uiReadOffsetWithinPart == (Adapter->psFlash2xCSInfo->OffsetISOImage1Part1End - Adapter->psFlash2xCSInfo->OffsetISOImage1Part1Start) ))
4098                                 {
4099                                         eISOReadPart = ISO_IMAGE1_PART2 ;
4100                                         uiReadOffsetWithinPart = 0;
4101                                 }
4102                                 if((eISOReadPart == ISO_IMAGE1_PART2) && (uiReadOffsetWithinPart == (Adapter->psFlash2xCSInfo->OffsetISOImage1Part2End - Adapter->psFlash2xCSInfo->OffsetISOImage1Part2Start)))
4103                                 {
4104                                         eISOReadPart = ISO_IMAGE1_PART3 ;
4105                                         uiReadOffsetWithinPart = 0;
4106                                 }
4107                                 if((eISOWritePart == ISO_IMAGE2) && (uiWriteOffsetWithinPart == (Adapter->psFlash2xCSInfo->OffsetISOImage2Part1End - Adapter->psFlash2xCSInfo->OffsetISOImage2Part1Start)))
4108                                 {
4109                                         eISOWritePart = ISO_IMAGE2_PART2 ;
4110                                         uiWriteOffsetWithinPart = 0;
4111                                 }
4112                                 if((eISOWritePart == ISO_IMAGE2_PART2) && (uiWriteOffsetWithinPart == (Adapter->psFlash2xCSInfo->OffsetISOImage2Part2End - Adapter->psFlash2xCSInfo->OffsetISOImage2Part2Start)))
4113                                 {
4114                                         eISOWritePart = ISO_IMAGE2_PART3 ;
4115                                         uiWriteOffsetWithinPart = 0;
4116                                 }
4117                         }
4118
4119                         Status = BcmFlash2xBulkRead(Adapter,
4120                                                                    (PUINT)Buff,
4121                                                                    eISOReadPart,
4122                                                                    uiReadOffsetWithinPart,
4123                                                                    Adapter->uiSectorSize
4124                                                                 );
4125
4126                         if(Status)
4127                         {
4128                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Read failed while copying ISO: Part: %x, OffsetWithinPart: %x\n", eISOReadPart, uiReadOffsetWithinPart);
4129                                 break;
4130                         }
4131
4132                         if(IsThisHeaderSector == TRUE)
4133                         {
4134                                 //If this is header sector write 0xFFFFFFFF at the sig time and in last write sig
4135                                 memcpy(SigBuff, Buff + sigOffset, MAX_RW_SIZE);
4136
4137                                 for(i = 0; i < MAX_RW_SIZE;i++)
4138                                         *(Buff + sigOffset + i) = 0xFF;
4139                         }
4140                         Adapter->bHeaderChangeAllowed = TRUE ;
4141
4142                         Status = BcmFlash2xBulkWrite(Adapter,
4143                                                                  (PUINT)Buff,
4144                                                                  eISOWritePart,
4145                                                                  uiWriteOffsetWithinPart,
4146                                                                  Adapter->uiSectorSize,
4147                                                                  TRUE);
4148                         if(Status)
4149                         {
4150                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Write failed while copying ISO: Part: %x, OffsetWithinPart: %x\n", eISOWritePart, uiWriteOffsetWithinPart);
4151                                 break;
4152                         }
4153
4154                         Adapter->bHeaderChangeAllowed = FALSE;
4155
4156                         if(IsThisHeaderSector == TRUE)
4157                         {
4158                                 WriteToFlashWithoutSectorErase(Adapter,
4159                                                                                                 SigBuff,
4160                                                                                                 eISOWritePart,
4161                                                                                                 sigOffset,
4162                                                                                                 MAX_RW_SIZE);
4163                                 IsThisHeaderSector = FALSE ;
4164                         }
4165                         //substracting the written Data
4166                         uiTotalDataToCopy = uiTotalDataToCopy - Adapter->uiSectorSize ;
4167                 }
4168
4169
4170         }
4171
4172         if(sCopySectStrut.SrcSection ==ISO_IMAGE2 && sCopySectStrut.DstSection ==ISO_IMAGE1)
4173         {
4174                 eISOReadPart = ISO_IMAGE2 ;
4175                 eISOWritePart = ISO_IMAGE1 ;
4176                 uiReadOffsetWithinPart =        0;
4177                 uiWriteOffsetWithinPart = 0 ;
4178
4179                 uiTotalDataToCopy =(Adapter->psFlash2xCSInfo->OffsetISOImage2Part1End) -
4180                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage2Part1Start)+
4181                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage2Part2End) -
4182                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage2Part2Start)+
4183                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage2Part3End) -
4184                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage2Part3Start);
4185
4186                 if(uiTotalDataToCopy < ISOLength)
4187                 {
4188                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"error as Source ISO Section does not have valid signature");
4189                         return STATUS_FAILURE;
4190                 }
4191
4192                 uiTotalDataToCopy =(Adapter->psFlash2xCSInfo->OffsetISOImage1Part1End) -
4193                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage1Part1Start)+
4194                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage1Part2End) -
4195                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage1Part2Start)+
4196                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage1Part3End) -
4197                                                    (Adapter->psFlash2xCSInfo->OffsetISOImage1Part3Start);
4198
4199                 if(uiTotalDataToCopy < ISOLength)
4200                 {
4201                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"error as Dest ISO Section does not have enough section size");
4202                         return STATUS_FAILURE;
4203                 }
4204
4205                 uiTotalDataToCopy = ISOLength;
4206
4207                 CorruptISOSig(Adapter,ISO_IMAGE1);
4208
4209                 while(uiTotalDataToCopy)
4210                 {
4211                         if(uiTotalDataToCopy == Adapter->uiSectorSize)
4212                         {
4213                                 //Setting for write of first sector. First sector is assumed to be written in last
4214                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Writing the signature sector");
4215                                 eISOReadPart = ISO_IMAGE2 ;
4216                                 uiReadOffsetWithinPart = 0;
4217                                 eISOWritePart = ISO_IMAGE1;
4218                                 uiWriteOffsetWithinPart = 0 ;
4219                                 IsThisHeaderSector = TRUE;
4220
4221                         }
4222                         else
4223                         {
4224                                 uiReadOffsetWithinPart = uiReadOffsetWithinPart + Adapter->uiSectorSize ;
4225                                 uiWriteOffsetWithinPart = uiWriteOffsetWithinPart + Adapter->uiSectorSize ;
4226
4227                                 if((eISOReadPart == ISO_IMAGE2) && (uiReadOffsetWithinPart == (Adapter->psFlash2xCSInfo->OffsetISOImage2Part1End - Adapter->psFlash2xCSInfo->OffsetISOImage2Part1Start) ))
4228                                 {
4229                                         eISOReadPart = ISO_IMAGE2_PART2 ;
4230                                         uiReadOffsetWithinPart = 0;
4231                                 }
4232                                 if((eISOReadPart == ISO_IMAGE2_PART2) && (uiReadOffsetWithinPart == (Adapter->psFlash2xCSInfo->OffsetISOImage2Part2End - Adapter->psFlash2xCSInfo->OffsetISOImage2Part2Start)))
4233                                 {
4234                                         eISOReadPart = ISO_IMAGE2_PART3 ;
4235                                         uiReadOffsetWithinPart = 0;
4236                                 }
4237                                 if((eISOWritePart == ISO_IMAGE1) && (uiWriteOffsetWithinPart == (Adapter->psFlash2xCSInfo->OffsetISOImage1Part1End - Adapter->psFlash2xCSInfo->OffsetISOImage1Part1Start)))
4238                                 {
4239                                         eISOWritePart = ISO_IMAGE1_PART2 ;
4240                                         uiWriteOffsetWithinPart = 0;
4241                                 }
4242                                 if((eISOWritePart == ISO_IMAGE1_PART2) && (uiWriteOffsetWithinPart == (Adapter->psFlash2xCSInfo->OffsetISOImage1Part2End - Adapter->psFlash2xCSInfo->OffsetISOImage1Part2Start)))
4243                                 {
4244                                         eISOWritePart = ISO_IMAGE1_PART3 ;
4245                                         uiWriteOffsetWithinPart = 0;
4246                                 }
4247                         }
4248
4249                         Status = BcmFlash2xBulkRead(Adapter,
4250                                                                    (PUINT)Buff,
4251                                                                    eISOReadPart,
4252                                                                    uiReadOffsetWithinPart,
4253                                                                    Adapter->uiSectorSize
4254                                                                 );
4255                         if(Status)
4256                         {
4257                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Read failed while copying ISO: Part: %x, OffsetWithinPart: %x\n", eISOReadPart, uiReadOffsetWithinPart);
4258                                 break;
4259                         }
4260
4261                         if(IsThisHeaderSector == TRUE)
4262                         {
4263                                 //If this is header sector write 0xFFFFFFFF at the sig time and in last write sig
4264                                 memcpy(SigBuff, Buff + sigOffset, MAX_RW_SIZE);
4265
4266                                 for(i = 0; i < MAX_RW_SIZE;i++)
4267                                         *(Buff + sigOffset + i) = 0xFF;
4268
4269                         }
4270                         Adapter->bHeaderChangeAllowed = TRUE ;
4271                         Status = BcmFlash2xBulkWrite(Adapter,
4272                                                                  (PUINT)Buff,
4273                                                                  eISOWritePart,
4274                                                                  uiWriteOffsetWithinPart,
4275                                                                  Adapter->uiSectorSize,
4276                                                                  TRUE);
4277
4278                         if(Status)
4279                         {
4280                                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Write failed while copying ISO: Part: %x, OffsetWithinPart: %x\n", eISOWritePart, uiWriteOffsetWithinPart);
4281                                 break;
4282                         }
4283
4284                         Adapter->bHeaderChangeAllowed = FALSE ;
4285
4286                         if(IsThisHeaderSector == TRUE)
4287                         {
4288                                 WriteToFlashWithoutSectorErase(Adapter,
4289                                                                                                 SigBuff,
4290                                                                                                 eISOWritePart,
4291                                                                                                 sigOffset,
4292                                                                                                 MAX_RW_SIZE);
4293                                 IsThisHeaderSector = FALSE ;
4294                         }
4295
4296                         //substracting the written Data
4297                         uiTotalDataToCopy = uiTotalDataToCopy - Adapter->uiSectorSize ;
4298                 }
4299
4300
4301         }
4302
4303         kfree(Buff);
4304
4305         return Status;
4306 }
4307 /**
4308 BcmFlash2xCorruptSig : this API is used to corrupt the written sig in Bcm Header present in flash section.
4309                                              It will corrupt the sig, if Section is writable, by making first bytes as zero.
4310 @Adapater :- Bcm Driver Private Data Structure
4311 @eFlash2xSectionVal :- Flash section val which has header
4312
4313 Return Value :-
4314         Sucess :- If Section is present and writable, corrupt the sig and return STATUS_SUCCESS
4315         Failure :-Return negative error code
4316
4317
4318 **/
4319 INT BcmFlash2xCorruptSig(PMINI_ADAPTER Adapter, FLASH2X_SECTION_VAL eFlash2xSectionVal)
4320 {
4321
4322         INT Status = STATUS_SUCCESS ;
4323         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Section Value :%x \n", eFlash2xSectionVal);
4324
4325         if((eFlash2xSectionVal == DSD0) || (eFlash2xSectionVal == DSD1) || (eFlash2xSectionVal == DSD2))
4326         {
4327                 Status = CorruptDSDSig(Adapter, eFlash2xSectionVal);
4328         }
4329         else if(eFlash2xSectionVal == ISO_IMAGE1 || eFlash2xSectionVal == ISO_IMAGE2)
4330         {
4331                 Status = CorruptISOSig(Adapter, eFlash2xSectionVal);
4332         }
4333         else
4334         {
4335                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Given Section <%d>does not have Header",eFlash2xSectionVal);
4336                 return STATUS_SUCCESS;
4337         }
4338         return Status;
4339 }
4340 /**
4341 BcmFlash2xWriteSig :-this API is used to Write the sig if requested Section has
4342                                           header and  Write Permission.
4343 @Adapater :- Bcm Driver Private Data Structure
4344 @eFlashSectionVal :- Flash section val which has header
4345
4346 Return Value :-
4347         Sucess :- If Section is present and writable write the sig and return STATUS_SUCCESS
4348         Failure :-Return negative error code
4349
4350 **/
4351 INT BcmFlash2xWriteSig(PMINI_ADAPTER Adapter, FLASH2X_SECTION_VAL eFlashSectionVal)
4352 {
4353
4354         UINT uiSignature = 0 ;
4355         UINT uiOffset = 0;
4356         //DSD_HEADER dsdHeader = {0};
4357
4358         if(Adapter->bSigCorrupted == FALSE)
4359         {
4360                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Signature is not corrupted by driver, hence not restoring\n");
4361                 return STATUS_SUCCESS;
4362         }
4363         if(Adapter->bAllDSDWriteAllow == FALSE)
4364         {
4365                 if(IsSectionWritable(Adapter,eFlashSectionVal) == FALSE)
4366                 {
4367                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Section is not Writable...Hence can't Write signature");
4368                         return SECTOR_IS_NOT_WRITABLE;
4369                 }
4370         }
4371         if((eFlashSectionVal == DSD0) ||(eFlashSectionVal == DSD1) || (eFlashSectionVal == DSD2))
4372         {
4373                 uiSignature = htonl(DSD_IMAGE_MAGIC_NUMBER) ;
4374                 uiOffset = Adapter->psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader ;
4375
4376                 uiOffset += FIELD_OFFSET_IN_HEADER(PDSD_HEADER,DSDImageMagicNumber);
4377
4378                 if((ReadDSDSignature(Adapter,eFlashSectionVal) & 0xFF000000) != CORRUPTED_PATTERN)
4379                 {
4380                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Corrupted Pattern is not there. Hence won't write sig");
4381                         return STATUS_FAILURE;
4382                 }
4383
4384         }
4385         else if((eFlashSectionVal == ISO_IMAGE1) || (eFlashSectionVal == ISO_IMAGE2))
4386         {
4387                 uiSignature = htonl(ISO_IMAGE_MAGIC_NUMBER);
4388                 //uiOffset = 0;
4389                 uiOffset = FIELD_OFFSET_IN_HEADER(PISO_HEADER,ISOImageMagicNumber);
4390                 if((ReadISOSignature(Adapter,eFlashSectionVal) & 0xFF000000) != CORRUPTED_PATTERN)
4391                 {
4392                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Currupted Pattern is not there. Hence won't write sig");
4393                         return STATUS_FAILURE;
4394                 }
4395         }
4396         else
4397         {
4398                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"GIVEN SECTION< %d > IS NOT VALID FOR SIG WRITE...", eFlashSectionVal);
4399                 return STATUS_FAILURE;
4400         }
4401
4402         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Restoring the signature");
4403
4404
4405         Adapter->bHeaderChangeAllowed = TRUE;
4406         Adapter->bSigCorrupted = FALSE;
4407         BcmFlash2xBulkWrite(Adapter, &uiSignature,eFlashSectionVal,uiOffset,SIGNATURE_SIZE,TRUE);
4408         Adapter->bHeaderChangeAllowed = FALSE;
4409
4410
4411
4412         return STATUS_SUCCESS;
4413 }
4414 /**
4415 validateFlash2xReadWrite :- This API is used to validate the user request for Read/Write.
4416                                                       if requested Bytes goes beyond the Requested section, it reports error.
4417 @Adapater :- Bcm Driver Private Data Structure
4418 @psFlash2xReadWrite :-Flash2x Read/write structure pointer
4419
4420 Return values:-Return TRUE is request is valid else FALSE.
4421
4422
4423 **/
4424 INT     validateFlash2xReadWrite(PMINI_ADAPTER Adapter, PFLASH2X_READWRITE psFlash2xReadWrite)
4425 {
4426         UINT uiNumOfBytes = 0 ;
4427         UINT uiSectStartOffset = 0 ;
4428         UINT uiSectEndOffset = 0;
4429         uiNumOfBytes = psFlash2xReadWrite->numOfBytes;
4430
4431         if(IsSectionExistInFlash(Adapter,psFlash2xReadWrite->Section) != TRUE)
4432         {
4433                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Section<%x> does not exixt in Flash",psFlash2xReadWrite->Section);
4434                 return FALSE;
4435         }
4436         uiSectStartOffset = BcmGetSectionValStartOffset(Adapter,psFlash2xReadWrite->Section);
4437         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "Start offset :%x ,section :%d\n",uiSectStartOffset,psFlash2xReadWrite->Section);
4438         if((psFlash2xReadWrite->Section == ISO_IMAGE1) ||(psFlash2xReadWrite->Section == ISO_IMAGE2))
4439         {
4440                 if(psFlash2xReadWrite->Section == ISO_IMAGE1)
4441                 {
4442                         uiSectEndOffset = BcmGetSectionValEndOffset(Adapter,ISO_IMAGE1) -
4443                                                           BcmGetSectionValStartOffset(Adapter,ISO_IMAGE1)+
4444                                                           BcmGetSectionValEndOffset(Adapter,ISO_IMAGE1_PART2) -
4445                                                           BcmGetSectionValStartOffset(Adapter,ISO_IMAGE1_PART2)+
4446                                                           BcmGetSectionValEndOffset(Adapter,ISO_IMAGE1_PART3) -
4447                                                           BcmGetSectionValStartOffset(Adapter,ISO_IMAGE1_PART3);
4448                 }
4449                 else if(psFlash2xReadWrite->Section == ISO_IMAGE2)
4450                 {
4451                         uiSectEndOffset = BcmGetSectionValEndOffset(Adapter,ISO_IMAGE2) -
4452                                                           BcmGetSectionValStartOffset(Adapter,ISO_IMAGE2)+
4453                                                           BcmGetSectionValEndOffset(Adapter,ISO_IMAGE2_PART2) -
4454                                                           BcmGetSectionValStartOffset(Adapter,ISO_IMAGE2_PART2)+
4455                                                           BcmGetSectionValEndOffset(Adapter,ISO_IMAGE2_PART3) -
4456                                                           BcmGetSectionValStartOffset(Adapter,ISO_IMAGE2_PART3);
4457
4458                 }
4459
4460                 //since this uiSectEndoffset is the size of iso Image. hence for calculating the vitual endoffset
4461                 //it should be added in startoffset. so that check done in last of this function can be valued.
4462                 uiSectEndOffset = uiSectStartOffset + uiSectEndOffset ;
4463
4464                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Total size of the ISO Image :%x",uiSectEndOffset);
4465         }
4466         else
4467                 uiSectEndOffset   = BcmGetSectionValEndOffset(Adapter,psFlash2xReadWrite->Section);
4468         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "End offset :%x \n",uiSectEndOffset);
4469
4470         //Checking the boundary condition
4471         if((uiSectStartOffset + psFlash2xReadWrite->offset + uiNumOfBytes) <= uiSectEndOffset)
4472                 return TRUE;
4473         else
4474         {
4475                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Invalid Request....");
4476                 return FALSE;
4477         }
4478
4479 }
4480
4481 /**
4482 IsFlash2x :- check for Flash 2.x
4483 @Adapater :- Bcm Driver Private Data Structure
4484
4485 Return value:-
4486         return TRUE if flah2.x of hgher version else return false.
4487 **/
4488
4489 INT IsFlash2x(PMINI_ADAPTER Adapter)
4490 {
4491         if(Adapter->uiFlashLayoutMajorVersion >= FLASH_2X_MAJOR_NUMBER)
4492                 return TRUE ;
4493         else
4494                 return FALSE;
4495 }
4496 /**
4497 GetFlashBaseAddr :- Calculate the Flash Base address
4498 @Adapater :- Bcm Driver Private Data Structure
4499
4500 Return Value:-
4501         Success :- Base Address of the Flash
4502 **/
4503
4504 INT GetFlashBaseAddr(PMINI_ADAPTER Adapter)
4505 {
4506
4507         UINT uiBaseAddr = 0;
4508
4509         if(Adapter->bDDRInitDone)
4510         {
4511                 /*
4512                 For All Valid Flash Versions... except 1.1, take the value from FlashBaseAddr
4513                 In case of Raw Read... use the default value
4514                 */
4515                 if(Adapter->uiFlashLayoutMajorVersion && (Adapter->bFlashRawRead == FALSE) &&
4516                         !((Adapter->uiFlashLayoutMajorVersion == 1) && (Adapter->uiFlashLayoutMinorVersion == 1))
4517                         )
4518                         uiBaseAddr = Adapter->uiFlashBaseAdd ;
4519                 else
4520                         uiBaseAddr = FLASH_CONTIGIOUS_START_ADDR_AFTER_INIT;
4521         }
4522         else
4523         {
4524                 /*
4525                 For All Valid Flash Versions... except 1.1, take the value from FlashBaseAddr
4526                 In case of Raw Read... use the default value
4527                 */
4528                 if(Adapter->uiFlashLayoutMajorVersion && (Adapter->bFlashRawRead == FALSE) &&
4529                         !((Adapter->uiFlashLayoutMajorVersion == 1) && (Adapter->uiFlashLayoutMinorVersion == 1))
4530                         )
4531                         uiBaseAddr = Adapter->uiFlashBaseAdd | FLASH_CONTIGIOUS_START_ADDR_BEFORE_INIT;
4532                 else
4533                         uiBaseAddr = FLASH_CONTIGIOUS_START_ADDR_BEFORE_INIT;
4534         }
4535
4536         return uiBaseAddr ;
4537 }
4538 /**
4539 BcmCopySection :- This API is used to copy the One section in another. Both section should
4540                                     be contiuous and of same size. Hence this Will not be applicabe to copy ISO.
4541
4542 @Adapater :- Bcm Driver Private Data Structure
4543 @SrcSection :- Source section From where data has to be copied
4544 @DstSection :- Destination section to which data has to be copied
4545 @offset :- Offset from/to  where data has to be copied from one section to another.
4546 @numOfBytes :- number of byes that has to be copyed from one section to another at given offset.
4547                              in case of numofBytes  equal zero complete section will be copied.
4548
4549 Return Values-
4550         Sucess : Return STATUS_SUCCESS
4551         Faillure :- return negative error code
4552
4553 **/
4554
4555 INT     BcmCopySection(PMINI_ADAPTER Adapter,
4556                                                 FLASH2X_SECTION_VAL SrcSection,
4557                                                 FLASH2X_SECTION_VAL DstSection,
4558                                                 UINT offset,
4559                                                 UINT numOfBytes)
4560 {
4561         UINT BuffSize = 0 ;
4562         UINT BytesToBeCopied = 0;
4563         PUCHAR pBuff = NULL ;
4564         INT Status = STATUS_SUCCESS ;
4565         if(SrcSection == DstSection)
4566         {
4567                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Source and Destination should be different ...try again");
4568                 return -EINVAL;
4569         }
4570         if((SrcSection != DSD0) && (SrcSection != DSD1) && (SrcSection != DSD2))
4571         {
4572                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Source should be DSD subsection");
4573                 return  -EINVAL;
4574         }
4575         if((DstSection != DSD0) && (DstSection != DSD1) && (DstSection != DSD2))
4576         {
4577                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Destination should be DSD subsection");
4578                 return  -EINVAL;
4579         }
4580
4581         //if offset zero means have to copy complete secton
4582
4583         if(numOfBytes == 0)
4584         {
4585                 numOfBytes = BcmGetSectionValEndOffset(Adapter,SrcSection)
4586                                   - BcmGetSectionValStartOffset(Adapter,SrcSection);
4587
4588                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL," Section Size :0x%x",numOfBytes);
4589         }
4590
4591         if((offset + numOfBytes) > BcmGetSectionValEndOffset(Adapter,SrcSection)
4592                                   - BcmGetSectionValStartOffset(Adapter,SrcSection))
4593         {
4594                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0," Input parameters going beyond the section offS: %x numB: %x of Source Section\n",
4595                                                 offset, numOfBytes);
4596                 return -EINVAL;
4597         }
4598
4599         if((offset + numOfBytes) > BcmGetSectionValEndOffset(Adapter,DstSection)
4600                                   - BcmGetSectionValStartOffset(Adapter,DstSection))
4601         {
4602                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0," Input parameters going beyond the section offS: %x numB: %x of Destination Section\n",
4603                                                 offset, numOfBytes);
4604                 return -EINVAL;
4605         }
4606
4607
4608         if(numOfBytes > Adapter->uiSectorSize )
4609                 BuffSize = Adapter->uiSectorSize;
4610         else
4611                 BuffSize = numOfBytes ;
4612
4613         pBuff = (PCHAR)kzalloc(BuffSize, GFP_KERNEL);
4614         if(pBuff == NULL)
4615         {
4616                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Memory allocation failed.. ");
4617                 return -ENOMEM;
4618         }
4619
4620
4621         BytesToBeCopied = Adapter->uiSectorSize ;
4622         if(offset % Adapter->uiSectorSize)
4623                 BytesToBeCopied = Adapter->uiSectorSize - (offset % Adapter->uiSectorSize);
4624         if(BytesToBeCopied > numOfBytes)
4625                 BytesToBeCopied = numOfBytes ;
4626
4627
4628
4629         Adapter->bHeaderChangeAllowed = TRUE;
4630
4631         do
4632         {
4633                 Status = BcmFlash2xBulkRead(Adapter, (PUINT)pBuff, SrcSection , offset,BytesToBeCopied);
4634                 if(Status)
4635                 {
4636                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Read failed at offset :%d for NOB :%d", SrcSection,BytesToBeCopied);
4637                         break;
4638                 }
4639                 Status = BcmFlash2xBulkWrite(Adapter,(PUINT)pBuff,DstSection,offset,BytesToBeCopied,FALSE);
4640                 if(Status)
4641                 {
4642                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Write failed at offset :%d for NOB :%d", DstSection,BytesToBeCopied);
4643                         break;
4644                 }
4645                 offset = offset + BytesToBeCopied;
4646                 numOfBytes = numOfBytes - BytesToBeCopied ;
4647                 if(numOfBytes)
4648                 {
4649                         if(numOfBytes > Adapter->uiSectorSize )
4650                                 BytesToBeCopied = Adapter->uiSectorSize;
4651                         else
4652                                 BytesToBeCopied = numOfBytes;
4653                 }
4654         }while(numOfBytes > 0) ;
4655         kfree(pBuff);
4656         Adapter->bHeaderChangeAllowed = FALSE ;
4657         return Status;
4658 }
4659
4660 /**
4661 SaveHeaderIfPresent :- This API is use to Protect the Header in case of Header Sector write
4662 @Adapater :- Bcm Driver Private Data Structure
4663 @pBuff :- Data buffer that has to be written in sector having the header map.
4664 @uiOffset :- Flash offset that has to be written.
4665
4666 Return value :-
4667         Sucess :- On sucess return STATUS_SUCCESS
4668         Faillure :- Return negative error code
4669
4670 **/
4671
4672 INT SaveHeaderIfPresent(PMINI_ADAPTER Adapter, PUCHAR pBuff, UINT uiOffset)
4673 {
4674         UINT offsetToProtect = 0,HeaderSizeToProtect =0;
4675         BOOLEAN bHasHeader = FALSE ;
4676         PUCHAR pTempBuff =NULL;
4677         UINT uiSectAlignAddr = 0;
4678         UINT sig = 0;
4679
4680         //making the offset sector alligned
4681         uiSectAlignAddr = uiOffset & ~(Adapter->uiSectorSize - 1);
4682
4683
4684         if((uiSectAlignAddr == BcmGetSectionValEndOffset(Adapter,DSD2)- Adapter->uiSectorSize)||
4685         (uiSectAlignAddr == BcmGetSectionValEndOffset(Adapter,DSD1)- Adapter->uiSectorSize)||
4686         (uiSectAlignAddr == BcmGetSectionValEndOffset(Adapter,DSD0)- Adapter->uiSectorSize))
4687         {
4688
4689                 //offset from the sector boundry having the header map
4690                 offsetToProtect = Adapter->psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader % Adapter->uiSectorSize;
4691                 HeaderSizeToProtect = sizeof(DSD_HEADER);
4692                 bHasHeader = TRUE ;
4693         }
4694
4695         if(uiSectAlignAddr == BcmGetSectionValStartOffset(Adapter,ISO_IMAGE1) ||
4696                 uiSectAlignAddr == BcmGetSectionValStartOffset(Adapter,ISO_IMAGE2))
4697         {
4698                 offsetToProtect = 0;
4699                 HeaderSizeToProtect = sizeof(ISO_HEADER);
4700                 bHasHeader = TRUE;
4701         }
4702         //If Header is present overwrite passed buffer with this
4703         if(bHasHeader && (Adapter->bHeaderChangeAllowed == FALSE))
4704         {
4705                 pTempBuff = (PUCHAR)kzalloc(HeaderSizeToProtect, GFP_KERNEL);
4706                 if(pTempBuff == NULL)
4707                 {
4708                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Memory allocation failed ");
4709                         return -ENOMEM;
4710                 }
4711                 //Read header
4712                 BeceemFlashBulkRead(Adapter,(PUINT)pTempBuff,(uiSectAlignAddr + offsetToProtect),HeaderSizeToProtect);
4713                 BCM_DEBUG_PRINT_BUFFER(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,pTempBuff ,HeaderSizeToProtect);
4714                 //Replace Buffer content with Header
4715                 memcpy(pBuff +offsetToProtect,pTempBuff,HeaderSizeToProtect);
4716
4717                 kfree(pTempBuff);
4718         }
4719         if(bHasHeader && Adapter->bSigCorrupted)
4720         {
4721                 sig = *((PUINT)(pBuff + offsetToProtect + FIELD_OFFSET_IN_HEADER(PDSD_HEADER,DSDImageMagicNumber)));
4722                 sig = ntohl(sig);
4723                 if((sig & 0xFF000000) != CORRUPTED_PATTERN)
4724                 {
4725                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Desired pattern is not at sig offset. Hence won't restore");
4726                         Adapter->bSigCorrupted = FALSE;
4727                         return STATUS_SUCCESS;
4728                 }
4729                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL," Corrupted sig is :%X", sig);
4730                 *((PUINT)(pBuff + offsetToProtect + FIELD_OFFSET_IN_HEADER(PDSD_HEADER,DSDImageMagicNumber)))= htonl(DSD_IMAGE_MAGIC_NUMBER);
4731                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Restoring the signature in Header Write only");
4732                 Adapter->bSigCorrupted = FALSE;
4733         }
4734
4735         return STATUS_SUCCESS ;
4736 }
4737 INT BcmMakeFlashCSActive(PMINI_ADAPTER Adapter, UINT offset)
4738 {
4739         UINT GPIOConfig = 0 ;
4740
4741
4742         if(Adapter->bFlashRawRead == FALSE)
4743         {
4744                 //Applicable for Flash2.x
4745                 if(IsFlash2x(Adapter) == FALSE)
4746                         return STATUS_SUCCESS;
4747         }
4748
4749         if(offset/FLASH_PART_SIZE)
4750         {
4751                 //bit[14..12] -> will select make Active CS1, CS2 or CS3
4752                 // Select CS1, CS2 and CS3 (CS0 is dedicated pin)
4753                 rdmalt(Adapter,FLASH_GPIO_CONFIG_REG, &GPIOConfig, 4);
4754                 GPIOConfig |= (7 << 12);
4755                 wrmalt(Adapter,FLASH_GPIO_CONFIG_REG, &GPIOConfig, 4);
4756         }
4757
4758         return STATUS_SUCCESS ;
4759 }
4760 /**
4761 BcmDoChipSelect : This will selcet the appropriate chip for writing.
4762 @Adapater :- Bcm Driver Private Data Structure
4763
4764 OutPut:-
4765         Select the Appropriate chip and retrn status Sucess
4766 **/
4767 INT BcmDoChipSelect(PMINI_ADAPTER Adapter, UINT offset)
4768 {
4769         UINT FlashConfig = 0;
4770         INT ChipNum = 0;
4771         UINT GPIOConfig = 0;
4772         UINT PartNum = 0;
4773
4774         ChipNum = offset / FLASH_PART_SIZE ;
4775
4776         //
4777         // Chip Select mapping to enable flash0.
4778         // To select flash 0, we have to OR with (0<<12).
4779         // ORing 0 will have no impact so not doing that part.
4780         // In future if Chip select value changes from 0 to non zero,
4781         // That needs be taken care with backward comaptibility. No worries for now.
4782         //
4783
4784         /*
4785         SelectedChip Variable is the selection that the host is 100% Sure the same as what the register will hold. This can be ONLY ensured
4786         if the Chip doesn't goes to low power mode while the flash operation is in progress (NVMRdmWrmLock is taken)
4787         Before every new Flash Write operation, we reset the variable. This is to ensure that after any wake-up from
4788         power down modes (Idle mode/shutdown mode), the values in the register will be different.
4789         */
4790
4791         if(Adapter->SelectedChip == ChipNum)
4792                 return STATUS_SUCCESS;
4793
4794         //BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL, "Selected Chip :%x", ChipNum);
4795         Adapter->SelectedChip = ChipNum ;
4796
4797         //bit[13..12]  will select the appropriate chip
4798         rdmalt(Adapter,FLASH_CONFIG_REG, &FlashConfig, 4);
4799         rdmalt(Adapter,FLASH_GPIO_CONFIG_REG, &GPIOConfig, 4);
4800
4801         {
4802                 switch(ChipNum)
4803                 {
4804                 case 0:
4805                         PartNum = 0;
4806                         break;
4807                 case 1:
4808                         PartNum = 3;
4809                         GPIOConfig |= (0x4 << CHIP_SELECT_BIT12);
4810                         break;
4811                 case 2:
4812                         PartNum = 1;
4813                         GPIOConfig |= (0x1 << CHIP_SELECT_BIT12);
4814                         break;
4815                 case 3:
4816                         PartNum = 2;
4817                         GPIOConfig |= (0x2 << CHIP_SELECT_BIT12);
4818                         break;
4819                 }
4820         }
4821         /* In case the bits already written in the FLASH_CONFIG_REG is same as what the user desired,
4822             nothing to do... can return immediately.
4823             ASSUMPTION: FLASH_GPIO_CONFIG_REG will be in sync with FLASH_CONFIG_REG.
4824             Even if the chip goes to low power mode, it should wake with values in each register in sync with each other.
4825             These values are not written by host other than during CHIP_SELECT.
4826         */
4827         if(PartNum == ((FlashConfig >> CHIP_SELECT_BIT12) & 0x3))
4828                 return STATUS_SUCCESS;
4829
4830         //clearing the bit[13..12]
4831         FlashConfig &= 0xFFFFCFFF;
4832         FlashConfig = (FlashConfig | (PartNum<<CHIP_SELECT_BIT12)); //00
4833
4834         wrmalt(Adapter,FLASH_GPIO_CONFIG_REG, &GPIOConfig, 4);
4835         udelay(100);
4836
4837         wrmalt(Adapter,FLASH_CONFIG_REG, &FlashConfig, 4);
4838         udelay(100);
4839
4840         return STATUS_SUCCESS;
4841
4842 }
4843 INT ReadDSDSignature(PMINI_ADAPTER Adapter, FLASH2X_SECTION_VAL dsd)
4844 {
4845                 UINT uiDSDsig = 0;
4846                 //UINT sigoffsetInMap = 0;
4847                 //DSD_HEADER dsdHeader = {0};
4848
4849
4850                 //sigoffsetInMap =(PUCHAR)&(dsdHeader.DSDImageMagicNumber) -(PUCHAR)&dsdHeader;
4851
4852                 if(dsd != DSD0 && dsd != DSD1 && dsd != DSD2)
4853                 {
4854                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"passed section value is not for DSDs");
4855                         return STATUS_FAILURE;
4856                 }
4857                 BcmFlash2xBulkRead(Adapter,
4858                                                    &uiDSDsig,
4859                                                    dsd,
4860                                                    Adapter->psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader + FIELD_OFFSET_IN_HEADER(PDSD_HEADER,DSDImageMagicNumber),
4861                                                    SIGNATURE_SIZE);
4862
4863                 uiDSDsig = ntohl(uiDSDsig);
4864                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"DSD SIG :%x", uiDSDsig);
4865
4866                 return uiDSDsig ;
4867 }
4868 INT ReadDSDPriority(PMINI_ADAPTER Adapter, FLASH2X_SECTION_VAL dsd)
4869 {
4870         //UINT priOffsetInMap = 0 ;
4871         unsigned int uiDSDPri = STATUS_FAILURE;
4872         //DSD_HEADER dsdHeader = {0};
4873         //priOffsetInMap = (PUCHAR)&(dsdHeader.DSDImagePriority) -(PUCHAR)&dsdHeader;
4874         if(IsSectionWritable(Adapter,dsd))
4875         {
4876                 if(ReadDSDSignature(Adapter,dsd)== DSD_IMAGE_MAGIC_NUMBER)
4877                 {
4878                         BcmFlash2xBulkRead(Adapter,
4879                                                            &uiDSDPri,
4880                                                            dsd,
4881                                                            Adapter->psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader +FIELD_OFFSET_IN_HEADER(PDSD_HEADER, DSDImagePriority),
4882                                                            4);
4883
4884                         uiDSDPri = ntohl(uiDSDPri);
4885                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"DSD<%x> Priority :%x", dsd, uiDSDPri);
4886
4887                 }
4888         }
4889         return uiDSDPri;
4890 }
4891 FLASH2X_SECTION_VAL getHighestPriDSD(PMINI_ADAPTER Adapter)
4892 {
4893         INT DSDHighestPri = STATUS_FAILURE;
4894         INT  DsdPri= 0 ;
4895         FLASH2X_SECTION_VAL HighestPriDSD = 0 ;
4896
4897         if(IsSectionWritable(Adapter,DSD2))
4898         {
4899                 DSDHighestPri = ReadDSDPriority(Adapter,DSD2);
4900                 HighestPriDSD = DSD2 ;
4901         }
4902         if(IsSectionWritable(Adapter,DSD1))
4903         {
4904                  DsdPri = ReadDSDPriority(Adapter,DSD1);
4905                  if(DSDHighestPri  < DsdPri)
4906                  {
4907                         DSDHighestPri = DsdPri ;
4908                         HighestPriDSD = DSD1;
4909                  }
4910         }
4911         if(IsSectionWritable(Adapter,DSD0))
4912         {
4913                  DsdPri = ReadDSDPriority(Adapter,DSD0);
4914                  if(DSDHighestPri  < DsdPri)
4915                  {
4916                         DSDHighestPri = DsdPri ;
4917                         HighestPriDSD = DSD0;
4918                  }
4919         }
4920         if(HighestPriDSD)
4921                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Highest DSD :%x , and its  Pri :%x", HighestPriDSD, DSDHighestPri);
4922         return  HighestPriDSD ;
4923 }
4924
4925 INT ReadISOSignature(PMINI_ADAPTER Adapter, FLASH2X_SECTION_VAL iso)
4926 {
4927                 UINT uiISOsig = 0;
4928                 //UINT sigoffsetInMap = 0;
4929                 //ISO_HEADER ISOHeader = {0};
4930
4931
4932                 //sigoffsetInMap =(PUCHAR)&(ISOHeader.ISOImageMagicNumber) -(PUCHAR)&ISOHeader;
4933
4934                 if(iso != ISO_IMAGE1 && iso != ISO_IMAGE2)
4935                 {
4936                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"passed section value is not for ISOs");
4937                         return STATUS_FAILURE;
4938                 }
4939                 BcmFlash2xBulkRead(Adapter,
4940                                                    &uiISOsig,
4941                                                    iso,
4942                                                    0 + FIELD_OFFSET_IN_HEADER(PISO_HEADER,ISOImageMagicNumber),
4943                                                    SIGNATURE_SIZE);
4944
4945                 uiISOsig = ntohl(uiISOsig);
4946                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"ISO SIG :%x", uiISOsig);
4947
4948                 return uiISOsig ;
4949 }
4950 INT ReadISOPriority(PMINI_ADAPTER Adapter, FLASH2X_SECTION_VAL iso)
4951 {
4952
4953         unsigned int ISOPri = STATUS_FAILURE;
4954         if(IsSectionWritable(Adapter,iso))
4955         {
4956                 if(ReadISOSignature(Adapter,iso)== ISO_IMAGE_MAGIC_NUMBER)
4957                 {
4958                         BcmFlash2xBulkRead(Adapter,
4959                                                            &ISOPri,
4960                                                            iso,
4961                                                            0 + FIELD_OFFSET_IN_HEADER(PISO_HEADER, ISOImagePriority),
4962                                                            4);
4963
4964                         ISOPri = ntohl(ISOPri);
4965                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"ISO<%x> Priority :%x", iso, ISOPri);
4966
4967                 }
4968         }
4969         return ISOPri;
4970 }
4971 FLASH2X_SECTION_VAL getHighestPriISO(PMINI_ADAPTER Adapter)
4972 {
4973         INT ISOHighestPri = STATUS_FAILURE;
4974         INT  ISOPri= 0 ;
4975         FLASH2X_SECTION_VAL HighestPriISO = NO_SECTION_VAL ;
4976
4977         if(IsSectionWritable(Adapter,ISO_IMAGE2))
4978         {
4979                 ISOHighestPri = ReadISOPriority(Adapter,ISO_IMAGE2);
4980                 HighestPriISO = ISO_IMAGE2 ;
4981         }
4982         if(IsSectionWritable(Adapter,ISO_IMAGE1))
4983         {
4984                  ISOPri = ReadISOPriority(Adapter,ISO_IMAGE1);
4985                  if(ISOHighestPri  < ISOPri)
4986                  {
4987                         ISOHighestPri = ISOPri ;
4988                         HighestPriISO = ISO_IMAGE1;
4989                  }
4990         }
4991         if(HighestPriISO)
4992                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Highest ISO :%x and its Pri :%x",HighestPriISO,ISOHighestPri);
4993         return  HighestPriISO ;
4994 }
4995 INT WriteToFlashWithoutSectorErase(PMINI_ADAPTER Adapter,
4996                                                                                 PUINT pBuff,
4997                                                                                 FLASH2X_SECTION_VAL eFlash2xSectionVal,
4998                                                                                 UINT uiOffset,
4999                                                                                 UINT uiNumBytes
5000                                                                                 )
5001 {
5002 #if !defined(BCM_SHM_INTERFACE) || defined(FLASH_DIRECT_ACCESS)
5003         UINT uiTemp = 0, value = 0 ;
5004         UINT i = 0;
5005         UINT uiPartOffset = 0;
5006 #endif
5007         UINT uiStartOffset = 0;
5008         //Adding section start address
5009         INT Status = STATUS_SUCCESS;
5010         PUCHAR pcBuff = (PUCHAR)pBuff;
5011
5012         if(uiNumBytes % Adapter->ulFlashWriteSize)
5013         {
5014                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Writing without Sector Erase for non-FlashWriteSize number of bytes 0x%x\n", uiNumBytes);
5015                 return STATUS_FAILURE;
5016         }
5017
5018         uiStartOffset = BcmGetSectionValStartOffset(Adapter,eFlash2xSectionVal);
5019
5020         if(IsSectionExistInVendorInfo(Adapter,eFlash2xSectionVal))
5021         {
5022                 return vendorextnWriteSectionWithoutErase(Adapter, pcBuff, eFlash2xSectionVal, uiOffset, uiNumBytes);
5023         }
5024
5025         uiOffset = uiOffset + uiStartOffset;
5026
5027 #if defined(BCM_SHM_INTERFACE) && !defined(FLASH_DIRECT_ACCESS)
5028   Status = bcmflash_raw_writenoerase((uiOffset/FLASH_PART_SIZE),(uiOffset % FLASH_PART_SIZE), pcBuff,uiNumBytes);
5029 #else
5030         rdmalt(Adapter, 0x0f000C80, &uiTemp, sizeof(uiTemp));
5031         value = 0;
5032         wrmalt(Adapter, 0x0f000C80,&value, sizeof(value));
5033
5034         Adapter->SelectedChip = RESET_CHIP_SELECT;
5035         BcmDoChipSelect(Adapter,uiOffset);
5036         uiPartOffset = (uiOffset & (FLASH_PART_SIZE - 1)) + GetFlashBaseAddr(Adapter);
5037
5038         for(i = 0 ; i< uiNumBytes; i += Adapter->ulFlashWriteSize)
5039         {
5040                 if(Adapter->ulFlashWriteSize == BYTE_WRITE_SUPPORT)
5041                         Status = flashByteWrite(Adapter,uiPartOffset, pcBuff);
5042                 else
5043                         Status = flashWrite(Adapter,uiPartOffset, pcBuff);
5044
5045                 if(Status != STATUS_SUCCESS)
5046                         break;
5047
5048                 pcBuff = pcBuff + Adapter->ulFlashWriteSize;
5049                 uiPartOffset = uiPartOffset +  Adapter->ulFlashWriteSize;
5050         }
5051         wrmalt(Adapter, 0x0f000C80, &uiTemp, sizeof(uiTemp));
5052         Adapter->SelectedChip = RESET_CHIP_SELECT;
5053 #endif
5054
5055         return Status;
5056 }
5057
5058 BOOLEAN IsSectionExistInFlash(PMINI_ADAPTER Adapter, FLASH2X_SECTION_VAL section)
5059 {
5060
5061         BOOLEAN SectionPresent = FALSE ;
5062
5063         switch(section)
5064         {
5065
5066                 case ISO_IMAGE1 :
5067                           if((Adapter->psFlash2xCSInfo->OffsetISOImage1Part1Start != UNINIT_PTR_IN_CS) &&
5068                                         (IsNonCDLessDevice(Adapter) == FALSE))
5069                                   SectionPresent = TRUE ;
5070                            break;
5071                 case ISO_IMAGE2 :
5072                                 if((Adapter->psFlash2xCSInfo->OffsetISOImage2Part1Start != UNINIT_PTR_IN_CS) &&
5073                                         (IsNonCDLessDevice(Adapter) == FALSE))
5074                                          SectionPresent = TRUE ;
5075                           break;
5076                 case DSD0 :
5077                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForDSDStart != UNINIT_PTR_IN_CS)
5078                                          SectionPresent = TRUE ;
5079                                 break;
5080                 case DSD1 :
5081                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD1Start != UNINIT_PTR_IN_CS)
5082                                          SectionPresent = TRUE ;
5083                                 break;
5084                 case DSD2 :
5085                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForDSD2Start != UNINIT_PTR_IN_CS)
5086                                          SectionPresent = TRUE ;
5087                                 break;
5088                 case VSA0 :
5089                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForVSAStart != UNINIT_PTR_IN_CS)
5090                                          SectionPresent = TRUE ;
5091                                 break;
5092                 case VSA1 :
5093                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA1Start != UNINIT_PTR_IN_CS)
5094                                          SectionPresent = TRUE ;
5095                                 break;
5096                 case VSA2 :
5097                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForVSA2Start != UNINIT_PTR_IN_CS)
5098                                          SectionPresent = TRUE ;
5099                                 break;
5100                 case SCSI :
5101                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForScsiFirmware != UNINIT_PTR_IN_CS)
5102                                          SectionPresent = TRUE ;
5103                                 break;
5104                 case CONTROL_SECTION :
5105                                 if(Adapter->psFlash2xCSInfo->OffsetFromZeroForControlSectionStart != UNINIT_PTR_IN_CS)
5106                                          SectionPresent = TRUE ;
5107                                 break;
5108                 default :
5109                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Section Does not exist in Flash 2.x");
5110                         SectionPresent =  FALSE;
5111         }
5112         return SectionPresent ;
5113 }
5114 INT IsSectionWritable(PMINI_ADAPTER Adapter, FLASH2X_SECTION_VAL Section)
5115 {
5116                 INT offset = STATUS_FAILURE;
5117                 INT Status = FALSE;
5118                 if(IsSectionExistInFlash(Adapter,Section) == FALSE)
5119                 {
5120                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Section <%d> does not exixt", Section);
5121                         return FALSE;
5122                 }
5123                 offset = BcmGetSectionValStartOffset(Adapter,Section);
5124                 if(offset == INVALID_OFFSET)
5125                 {
5126                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Section<%d> does not exixt", Section);
5127                         return FALSE;
5128                 }
5129
5130                 if(IsSectionExistInVendorInfo(Adapter,Section))
5131                 {
5132                         return !(Adapter->psFlash2xVendorInfo->VendorSection[Section].AccessFlags & FLASH2X_SECTION_RO);
5133                 }
5134
5135                 Status = IsOffsetWritable(Adapter,offset);
5136                 return Status ;
5137 }
5138
5139 INT CorruptDSDSig(PMINI_ADAPTER Adapter, FLASH2X_SECTION_VAL eFlash2xSectionVal)
5140 {
5141
5142         PUCHAR pBuff = NULL;
5143         UINT sig = 0;
5144         UINT uiOffset = 0;
5145         UINT BlockStatus = 0;
5146         UINT uiSectAlignAddr = 0;
5147
5148         Adapter->bSigCorrupted = FALSE;
5149
5150         if(Adapter->bAllDSDWriteAllow == FALSE)
5151         {
5152                 if(IsSectionWritable(Adapter,eFlash2xSectionVal) != TRUE)
5153                 {
5154                         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Section is not Writable...Hence cant Corrupt signature");
5155                         return SECTOR_IS_NOT_WRITABLE;
5156                 }
5157         }
5158
5159         pBuff = (PUCHAR)kzalloc(MAX_RW_SIZE, GFP_KERNEL);
5160         if(pBuff == NULL)
5161         {
5162                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0, "Can't allocate memorey");
5163                 return -ENOMEM ;
5164         }
5165
5166         uiOffset = Adapter->psFlash2xCSInfo->OffsetFromDSDStartForDSDHeader + sizeof(DSD_HEADER);
5167         uiOffset -= MAX_RW_SIZE ;
5168
5169         BcmFlash2xBulkRead(Adapter, (PUINT)pBuff,eFlash2xSectionVal,uiOffset,MAX_RW_SIZE);
5170
5171
5172         sig = *((PUINT)(pBuff +12));
5173         sig =ntohl(sig);
5174         BCM_DEBUG_PRINT_BUFFER(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,pBuff,MAX_RW_SIZE);
5175         //Now corrupting the sig by corrupting 4th last Byte.
5176         *(pBuff + 12) = 0;
5177
5178         if(sig == DSD_IMAGE_MAGIC_NUMBER)
5179         {
5180                 Adapter->bSigCorrupted = TRUE;
5181                 if(Adapter->ulFlashWriteSize == BYTE_WRITE_SUPPORT)
5182                 {
5183                         uiSectAlignAddr = uiOffset & ~(Adapter->uiSectorSize -1);
5184                         BlockStatus = BcmFlashUnProtectBlock(Adapter,uiSectAlignAddr,Adapter->uiSectorSize);
5185
5186                         WriteToFlashWithoutSectorErase(Adapter,(PUINT)(pBuff + 12),eFlash2xSectionVal,
5187                                                                                                 (uiOffset + 12),BYTE_WRITE_SUPPORT);
5188                         if(BlockStatus)
5189                         {
5190                                 BcmRestoreBlockProtectStatus(Adapter,BlockStatus);
5191                                 BlockStatus = 0;
5192                         }
5193                 }
5194                 else
5195                 {
5196                         WriteToFlashWithoutSectorErase(Adapter,(PUINT)pBuff,eFlash2xSectionVal,
5197                                                                                                 uiOffset ,MAX_RW_SIZE);
5198                 }
5199         }
5200         else
5201         {
5202                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"BCM Signature is not present in header");
5203                 kfree(pBuff);
5204                 return STATUS_FAILURE;
5205         }
5206
5207         kfree(pBuff);
5208         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Corrupted the signature");
5209         return STATUS_SUCCESS ;
5210 }
5211
5212 INT CorruptISOSig(PMINI_ADAPTER Adapter, FLASH2X_SECTION_VAL eFlash2xSectionVal)
5213 {
5214
5215         PUCHAR pBuff = NULL;
5216         UINT sig = 0;
5217         UINT uiOffset = 0;
5218
5219         Adapter->bSigCorrupted = FALSE;
5220
5221         if(IsSectionWritable(Adapter,eFlash2xSectionVal) != TRUE)
5222         {
5223                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Section is not Writable...Hence cant Corrupt signature");
5224                 return SECTOR_IS_NOT_WRITABLE;
5225         }
5226
5227         pBuff = (PUCHAR)kzalloc(MAX_RW_SIZE, GFP_KERNEL);
5228         if(pBuff == NULL)
5229         {
5230                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"Can't allocate memorey");
5231                 return -ENOMEM ;
5232         }
5233
5234         uiOffset = 0;
5235
5236         BcmFlash2xBulkRead(Adapter, (PUINT)pBuff,eFlash2xSectionVal,uiOffset, MAX_RW_SIZE);
5237
5238         sig = *((PUINT)pBuff);
5239         sig =ntohl(sig);
5240
5241         //corrupt signature
5242         *pBuff = 0;
5243
5244         if(sig == ISO_IMAGE_MAGIC_NUMBER)
5245         {
5246                 Adapter->bSigCorrupted = TRUE;
5247                 WriteToFlashWithoutSectorErase(Adapter,(PUINT)pBuff,eFlash2xSectionVal,
5248                                                                                         uiOffset ,Adapter->ulFlashWriteSize);
5249         }
5250         else
5251         {
5252                 BCM_DEBUG_PRINT(Adapter,DBG_TYPE_PRINTK, 0, 0,"BCM Signature is not present in header");
5253                 kfree(pBuff);
5254                 return STATUS_FAILURE;
5255         }
5256
5257         BCM_DEBUG_PRINT(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,"Corrupted the signature");
5258         BCM_DEBUG_PRINT_BUFFER(Adapter,DBG_TYPE_OTHERS, NVM_RW, DBG_LVL_ALL,pBuff,MAX_RW_SIZE);
5259
5260         kfree(pBuff);
5261         return STATUS_SUCCESS ;
5262 }
5263
5264 BOOLEAN IsNonCDLessDevice(PMINI_ADAPTER Adapter)
5265 {
5266         if(Adapter->psFlash2xCSInfo->IsCDLessDeviceBootSig == NON_CDLESS_DEVICE_BOOT_SIG)
5267                 return TRUE;
5268         else
5269                 return FALSE ;
5270 }
5271