xref: /utopia/UTPA2-700.0.x/modules/flash/hal/messi/flash/serial/halSERFLASH.h (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ////////////////////////////////////////////////////////////////////////////////
94 
95 #ifndef _HAL_SERFLASH_H_
96 #define _HAL_SERFLASH_H_
97 
98 //-------------------------------------------------------------------------------------------------
99 //  Macro and Define
100 //-------------------------------------------------------------------------------------------------
101 
102 // Flash IC
103 #define FLASH_IC_UNKNOWN        0x0000
104 #define FLASH_IC_SST25VF080B    0x0100      // 1M    SST
105 #define FLASH_IC_SST25VF016B    0x0101      // 2M
106 #define FLASH_IC_SST25VF032B    0x0102      // 2M
107 #define FLASH_IC_PM25LV080      0x0200      // 1M    PMC
108 #define FLASH_IC_PM25LV016      0x0201      // 2M
109 #define FLASH_IC_ST25P80_A      0x0300      // 1M    ST
110 #define FLASH_IC_ST25P16_A      0x0301      // 2M
111 #define FLASH_IC_STM25P32       0x0302      // 4M
112 #define FLASH_IC_AT26DF081A     0x0400      // 1M    ATMEL
113 #define FLASH_IC_AT26DF161      0x0401      // 2M
114 #define FLASH_IC_AT26DF321      0x0402      // 4M
115 #define FLASH_IC_AT25DF321A     0x0403      // 4M
116 #define FLASH_IC_AT25DF641      0x0404      // 8M
117 #define FLASH_IC_MX25L8005      0x0500      // 1M    MXIC
118 #define FLASH_IC_MX25L1605A     0x0501      // 2M
119 #define FLASH_IC_MX25L3205D     0x0502      // 4M
120 #define FLASH_IC_MX25L6405D     0x0503      // 8M
121 #define FLASH_IC_MX25L12805D    0x0504      // 16M
122 #define FLASH_IC_MX25L6445E     0x0505      // 8M
123 #define FLASH_IC_MX25L12845E    0x0506      // 16M
124 #define FLASH_IC_MX25L1606E     0x0507      // 2M
125 #define FLASH_IC_MX25L3206E     0x0508      // 4M
126 #define FLASH_IC_MX25L6406E     0x0509      // 8M
127 
128 #define FLASH_IC_NX25P80        0x0600      // 1M    NX
129 #define FLASH_IC_NX25P16        0x0601      // 2M
130 #define FLASH_IC_W25X80         0x0700      // 1M    WINB
131 #define FLASH_IC_W25X16         0x0701      // 2M
132 #define FLASH_IC_W25Q16         0x0711      // 2M
133 #define FLASH_IC_W25X32         0x0702      // 4M
134 #define FLASH_IC_W25Q32         0x0703      // 4M
135 #define FLASH_IC_W25X64         0x0704      // 8M
136 #define FLASH_IC_W25Q64         0x0705      // 8M
137 #define FLASH_IC_W25Q128        0x0706      // 16M
138 #define FLASH_IC_W25Q80         0x0707      // 1M
139 #define FLASH_IC_W25Q64CV       0x0708      // 8M
140 #define FLASH_IC_W25Q32BV       0x0709      // 4M
141 
142 #define FLASH_IC_S25FL008A      0x0800      // 1M    SPANSION
143 #define FLASH_IC_S25FL016A      0x0801      // 2M
144 #define FLASH_IC_S25FL128P      0x0802      // 16M
145 #define FLASH_IC_S25FL032P      0x0803      // 4M
146 #define FLASH_IC_S25FL064P      0x0804      // 8M
147 #define FLASH_IC_S25FL032K      0x0805      // 4M
148 
149 #define FLASH_IC_EN25B20T       0x0900      // 2M    EON
150 #define FLASH_IC_EN25B20B       0x0901      // 2M
151 #define FLASH_IC_EN25B10T       0x0902      // 1M
152 #define FLASH_IC_EN25B10B       0x0903      // 1M
153 #define FLASH_IC_EN25B32B       0x0904      // 4M    (Bottom Boot)
154 #define FLASH_IC_EN25B64B       0x0905      // 4M
155 #define FLASH_IC_EN25Q32A       0x0906      // 4M
156 #define FLASH_IC_EN25Q128       0x0907      // 16M
157 #define FLASH_IC_EN25Q32        0x0908      // 4M
158 #define FLASH_IC_EN25Q64        0x0909      // 4M
159 #define FLASH_IC_EN25F16        0x0910	     // 2M
160 #define FLASH_IC_EN25F32        0x0911      // 4M
161 #define FLASH_IC_EN25F80        0x0912      // 1M
162 #define FLASH_IC_EN25P16        0x0913      // 2M
163 #define FLASH_IC_EN25QH16       0x0914      // 2M
164 #define FLASH_IC_EN25F10        0x0999      // 128K for secure boot
165 #define FLASH_IC_ESMT_F25L016A  0x0A00      // 2M
166 #define FLASH_IC_ESMT_F25L032PA 0x0A01      // 4M
167 #define FLASH_IC_ESMT_F25L032QA 0x0A02      // 4M
168 
169 //#define FLASH_IC_EN25Q32        0x0A01      // 2M
170 #define FLASH_IC_EN25Q16        0x0A03      // 2M
171 #define FLASH_IC_GD25Q16        0x0B01      // 2M
172 #define FLASH_IC_GD25Q32        0x0B02      // 4M
173 #define FLASH_IC_PM25LQ032C      0x0D00      // 4M
174 #define FLASH_IC_MICRON_M25P16   0x0E00      // 2M
175 #define FLASH_IC_MICRON_N25Q32   0x0E01      // 4M
176 #define FLASH_IC_MICRON_N25Q64   0x0E02      // 8M
177 #define FLASH_IC_MICRON_N25Q128  0x0E03      // 16M
178 
179 
180 // Flash Manufacture ID
181 #define MID_MXIC                0xC2
182 #define MID_WB                  0xEF
183 #define MID_EON                 0x1C
184 #define MID_ST                  0x20
185 #define MID_SST                 0xBF
186 #define MID_PMC                 0x9D
187 #define MID_ATMEL               0x1F
188 #define MID_SPAN                0x01
189 #define MID_GD                  0xC8
190 #define MID_MICRON              0x20
191 #define MID_ESMT                0x8C
192 
193 // Flash Storage Size
194 #define SIZE_1KB                0x400
195 #define SIZE_2KB                0x800
196 #define SIZE_4KB                0x1000
197 #define SIZE_8KB                0x2000
198 #define SIZE_16KB               0x4000
199 #define SIZE_32KB               0x8000
200 #define SIZE_64KB               0x10000
201 #define SIZE_128KB              0x20000
202 #define SIZE_256KB              0x40000
203 #define SIZE_512KB              0x80000
204 #define SIZE_1MB                0x100000
205 #define SIZE_2MB                0x200000
206 #define SIZE_4MB                0x400000
207 #define SIZE_8MB                0x800000
208 #define SIZE_16MB               0x1000000
209 
210 #ifndef UNUSED
211 #define UNUSED(x) ((x)=(x))
212 #endif
213 
214 // ISP_DEV_SEL
215 #define ISP_DEV_PMC             BITS(2:0, 0)
216 #define ISP_DEV_NEXTFLASH       BITS(2:0, 1)
217 #define ISP_DEV_ST              BITS(2:0, 2)
218 #define ISP_DEV_SST             BITS(2:0, 3)
219 #define ISP_DEV_ATMEL           BITS(2:0, 4)
220 
221 // ISP_SPI_ENDIAN_SEL
222 #define ISP_SPI_ENDIAN_BIG      BITS(0:0, 1)
223 #define ISP_SPI_ENDIAN_LITTLE   BITS(0:0, 0)
224 
225 
226 #define NUMBER_OF_SERFLASH_SECTORS          (_hal_SERFLASH.u32NumSec)
227 #define SERFLASH_SECTOR_SIZE                (_hal_SERFLASH.u32SecSize)
228 #define SERFLASH_PAGE_SIZE                  (_hal_SERFLASH.u16PageSize)
229 #define SERFLASH_MAX_CHIP_WR_DONE_TIMEOUT   (_hal_SERFLASH.u16MaxChipWrDoneTimeout)
230 #define SERFLASH_WRSR_BLK_PROTECT           (_hal_SERFLASH.u8WrsrBlkProtect)
231 #define ISP_DEV_SEL                         (_hal_SERFLASH.u16DevSel)
232 #define ISP_SPI_ENDIAN_SEL                  (_hal_SERFLASH.u16SpiEndianSel)
233 
234 
235 #define DEBUG_SER_FLASH(debug_level, x)     do { if (_u8SERFLASHDbgLevel >= (debug_level)) (x); } while(0)
236 #define WAIT_SFSH_CS_STAT()             {while(ISP_READ(REG_ISP_SPI_CHIP_SELE_BUSY) == SFSH_CHIP_SELE_SWITCH){}}
237 
238 #define SERFLASH_BLOCK_SIZE   0x10000
239 
240 //-------------------------------------------------------------------------------------------------
241 //  Type and Structure
242 //-------------------------------------------------------------------------------------------------
243 
244 typedef enum
245 {
246     FLASH_ID0       = 0x00,
247     FLASH_ID1       = 0x01,
248     FLASH_ID2       = 0x02,
249     FLASH_ID3       = 0x03
250 } EN_FLASH_ID;
251 
252 typedef enum
253 {
254     WP_AREA_EXACTLY_AVAILABLE,
255     WP_AREA_PARTIALLY_AVAILABLE,
256     WP_AREA_NOT_AVAILABLE,
257     WP_TABLE_NOT_SUPPORT,
258 } EN_WP_AREA_EXISTED_RTN;
259 
260 typedef enum
261 {
262     FLASH_ERASE_04K       = SPI_CMD_SE,
263     FLASH_ERASE_32K       = SPI_CMD_32BE,
264     FLASH_ERASE_64K       = SPI_CMD_64BE
265 } EN_FLASH_ERASE;
266 
267 typedef struct
268 {
269     MS_U8   u8BlockProtectBits;  // Block Protection Bits
270     MS_U32  u32LowerBound;
271     MS_U32  u32UpperBound;
272 } ST_WRITE_PROTECT;
273 
274 typedef enum
275 {
276     E_SINGLE_MODE,
277     E_FAST_MODE,
278     E_DUAL_D_MODE,
279     E_DUAL_AD_MODE,
280     E_QUAD_MODE,
281 }SPI_READ_MODE;
282 
283 
284 typedef struct
285 {
286     MS_U16  u16Start;           // Start block # of special block size
287     MS_U16  u16End;             // End   block # of special block size
288     MS_U32  au32SizeList[8];    // List of special block sizes. Total size must be equal to FLASH_BLOCK_SIZE
289 } ST_SPECIAL_BLOCKS;
290 
291 typedef struct
292 {
293     MS_U16  u16FlashType;   // flash type
294     MS_U8   u8MID;          // Manufacture ID
295     MS_U8   u8DID0;         // Device ID (memory type)
296     MS_U8   u8DID1;         // Device ID (memory capacity)
297 
298     ST_WRITE_PROTECT   *pWriteProtectTable;
299     ST_SPECIAL_BLOCKS  *pSpecialBlocks;
300 
301     MS_U32  u32FlashSize;               // Flash Size
302     MS_U32  u32NumSec;                  // NUMBER_OF_SERFLASH_SECTORS                  // number of sectors
303     MS_U32  u32SecSize;                 // SERFLASH_SECTOR_SIZE                                // sector size
304     MS_U16  u16PageSize;                // SERFLASH_PAGE_SIZE                                    // page size
305     MS_U16  u16MaxChipWrDoneTimeout;    // SERFLASH_MAX_CHIP_WR_DONE_TIMEOUT    // max timeout for chip write done
306     MS_U8   u8WrsrBlkProtect;           // SERFLASH_WRSR_BLK_PROTECT                     // BP bits @ Serial Flash Status Register
307     MS_U16  u16DevSel;                  // ISP_DEV_SEL                                                  // reg_device_select
308     MS_U16  u16SpiEndianSel;            // ISP_SPI_ENDIAN_SEL                                     // reg_endian_sel_spi
309     MS_BOOL b2XREAD;
310 } hal_SERFLASH_t;
311 
312 //-------------------------------------------------------------------------------------------------
313 //  Function and Variable
314 //-------------------------------------------------------------------------------------------------
315 extern MS_BOOL HAL_SERFLASH_SetCKG(SPI_DrvCKG eCkgSpi);
316 extern void HAL_SERFLASH_ClkDiv(SPI_DrvClkDiv eClkDivSpi);
317 extern MS_BOOL HAL_SERFLASH_SetMode(MS_BOOL bXiuRiu);
318 extern MS_BOOL HAL_SERFLASH_Set2XREAD(MS_BOOL b2XMode);
319 extern MS_BOOL HAL_SERFLASH_ChipSelect(MS_U8 u8FlashIndex);
320 extern void HAL_SERFLASH_Config(MS_VIRT u32PMRegBaseAddr, MS_VIRT u32NonPMRegBaseAddr, MS_VIRT u32XiuBaseAddr);
321 extern void HAL_SERFLASH_Init(void);
322 extern void HAL_SERFLASH_SetGPIO(MS_BOOL bSwitch);
323 extern MS_BOOL HAL_SERFLASH_DetectType(void);
324 extern MS_BOOL HAL_SERFLASH_DetectSize(MS_U32  *u32FlashSize);
325 extern MS_BOOL HAL_SERFLASH_EraseChip(void);
326 extern MS_BOOL HAL_SERFLASH_AddressToBlock(MS_U32 u32FlashAddr, MS_U32 *pu32BlockIndex);
327 extern MS_BOOL HAL_SERFLASH_BlockToAddress(MS_U32 u32BlockIndex, MS_U32 *pu32FlashAddr);
328 extern MS_BOOL HAL_SERFLASH_BlockErase(MS_U32 u32StartBlock,MS_U32 u32EndBlock,MS_BOOL bWait);
329 extern MS_BOOL HAL_SERFLASH_SectorErase(MS_U32 u32SectorAddress);
330 extern MS_BOOL HAL_SERFLASH_CheckWriteDone(void);
331 
332 extern MS_BOOL HAL_SERFLASH_Write(MS_U32 u32Addr, MS_U32 u32Size, MS_U8 *pu8Data);
333 
334 extern MS_BOOL HAL_SERFLASH_Read(MS_U32 u32Addr, MS_U32 u32Size, MS_U8 *pu8Data);
335 
336 
337 extern EN_WP_AREA_EXISTED_RTN HAL_SERFLASH_WP_Area_Existed(MS_U32 u32UpperBound, MS_U32 u32LowerBound, MS_U8 *pu8BlockProtectBits);
338 extern MS_BOOL HAL_SERFLASH_WriteProtect_Area(MS_BOOL bEnableAllArea, MS_U8 u8BlockProtectBits);
339 extern MS_BOOL HAL_SERFLASH_WriteProtect(MS_BOOL bEnable);
340 extern MS_BOOL HAL_SERFLASH_ReadID(MS_U8 *pu8Data, MS_U32 u32Size);
341 extern MS_BOOL HAL_SERFLASH_ReadREMS4(MS_U8 * pu8Data, MS_U32 u32Size);
342 extern MS_BOOL HAL_SERFLASH_DMA(MS_U32 u32FlashStart, MS_U32 u32DRAMStart, MS_U32 u32Size);
343 extern MS_BOOL HAL_SERFLASH_ReadStatusReg(MS_U8 *pu8StatusReg);
344 extern MS_BOOL HAL_SERFLASH_ReadStatusReg2(MS_U8 *pu8StatusReg);
345 extern MS_BOOL HAL_SERFLASH_WriteStatusReg(MS_U16 u16StatusReg);
346 //#if MXIC_ONLY
347 extern MS_BOOL HAL_SPI_EnterIBPM(void);
348 extern MS_BOOL HAL_SPI_SingleBlockLock(MS_PHYADDR u32FlashAddr, MS_BOOL bLock);
349 extern MS_BOOL HAL_SPI_GangBlockLock(MS_BOOL bLock);
350 extern MS_U8 HAL_SPI_ReadBlockStatus(MS_PHYADDR u32FlashAddr);
351 extern MS_U64 HAL_SERFLASH_ReadUID(void);
352 //#endif//MXIC_ONLY
353 // DON'T USE THESE DIRECTLY
354 extern hal_SERFLASH_t _hal_SERFLASH;
355 extern MS_U8 _u8SERFLASHDbgLevel;
356 extern MS_BOOL _bIBPM;
357 extern MS_BOOL _bWPPullHigh;
358 extern MS_BOOL HAL_SERFLASH_WriteProtect_Area_Lookup(MS_U32 u32ProtectSpace, MS_U32 u32NonProtectSpace, MS_U8 *pu8BlockProtectBits);
359 extern MS_U32 HAL_SERFLASH_WriteProtect_Area_Boundary(void);
360 /*spasion flash DYB unlock*/
361 extern MS_BOOL HAL_SERFLASH_SPSNDYB_UNLOCK(MS_U32 u32StartBlock, MS_U32 u32EndBlock);
362 
363 extern MS_BOOL HAL_FSP_BlockErase(MS_U32 u32FlashAddr, EN_FLASH_ERASE eSize);
364 
365 extern MS_BOOL HAL_FSP_Write(MS_U32 u32Addr, MS_U32 u32DataSize, MS_U8 *pu8Data);
366 
367 extern MS_BOOL HAL_FSP_Write_Burst(MS_U32 u32Addr, MS_U32 u32Size, MS_U8 *pu8Data);
368 
369 extern MS_BOOL HAL_FSP_Read(MS_U32 u32Addr, MS_U32 u32DataSize, MS_U8 *pu8Data);
370 extern void HAL_FSP_Entry(void);
371 extern void HAL_FSP_Exit(void);
372 extern void HAL_SERFLASH_SelectReadMode(SPI_READ_MODE eReadMode);
373 #endif // _HAL_SERFLASH_H_
374