added files
[bcm963xx.git] / shared / src / flash_api.c
1 /*
2 <:copyright-broadcom
3
4  Copyright (c) 2005 Broadcom Corporation
5  All Rights Reserved
6  No portions of this material may be reproduced in any form without the
7  written permission of:
8           Broadcom Corporation
9           16215 Alton Parkway
10           Irvine, California 92619
11  All information contained in this document is Broadcom Corporation
12  company private, proprietary, and trade secret.
13
14 :>
15 */
16 /***************************************************************************
17  * File Name  : flash_api.c
18  *
19  * Description: This file contains the implementation of the wrapper functions
20  *              for the flash device interface.
21  ***************************************************************************/
22
23 /** Includes. */
24 #ifdef _CFE_                                                
25 #include "lib_types.h"
26 #include "lib_printf.h"
27 #include "lib_string.h"
28 #define printk  printf
29 #else // Linux
30 #include <linux/kernel.h>
31 #endif
32
33 #include "bcmtypes.h"
34 #include "board.h"
35 #include "flash_api.h"
36
37
38 /** Defines. **/
39 #ifndef NULL
40 #define NULL 0
41 #endif
42
43 #if !defined(INC_CFI_FLASH_DRIVER)
44 #define INC_CFI_FLASH_DRIVER        1
45 #endif
46
47 #if !defined(INC_SPI_FLASH_DRIVER)
48 #if defined(CONFIG_BCM96348) || defined(BCM96338)
49 #define INC_SPI_FLASH_DRIVER        1
50 #else
51 #define INC_SPI_FLASH_DRIVER        0
52 #endif
53 #endif
54
55
56 /** Externs. **/
57 #if (INC_CFI_FLASH_DRIVER==1)
58 extern int cfi_flash_init(flash_device_info_t **flash_info);
59 #else
60 #define cfi_flash_init(x)           FLASH_API_ERROR
61 #endif
62
63 #if (INC_SPI_FLASH_DRIVER==1)
64 extern int spi_flash_init(flash_device_info_t **flash_info);
65 #else
66 #define spi_flash_init(x)           FLASH_API_ERROR
67 #endif
68
69
70 /** Variables. **/
71 flash_device_info_t *g_flash_info = NULL;
72 int g_include_cfi_flash_driver = INC_CFI_FLASH_DRIVER; 
73 int g_include_spi_flash_driver = INC_SPI_FLASH_DRIVER; 
74
75
76 /***************************************************************************
77  * Function Name: flash_init
78  * Description  : Initialize flash part.
79  * Returns      : FLASH_API_OK or FLASH_API_ERROR
80  ***************************************************************************/
81 int flash_init(void)
82 {
83     int ret = FLASH_API_ERROR;
84     flash_device_info_t *flash_info = NULL;
85     unsigned short cfi_devid = 0xffff;
86     unsigned short spi_devid = 0xffff;
87     char *type = "";
88
89     if( g_include_cfi_flash_driver )
90     {
91         ret = cfi_flash_init( &flash_info );
92         if( ret == FLASH_API_OK )
93         {
94             g_flash_info = flash_info;
95             type = "Parallel";
96         }
97         else
98             if( flash_info )
99                 cfi_devid = flash_info->flash_device_id;
100     }
101
102     if( ret != FLASH_API_OK && g_include_spi_flash_driver )
103     {
104         ret = spi_flash_init( &flash_info );
105         if( ret == FLASH_API_OK )
106         {
107             g_flash_info = flash_info;
108             type = "Serial";
109         }
110         else
111             if( flash_info )
112                 spi_devid = flash_info->flash_device_id;
113     }
114
115     if( ret == FLASH_API_OK )
116     {
117         /* Flash initialization OK. */
118         printk( "%s flash device: name %s, id 0x%4.4x, size %dKB\n", type,
119             g_flash_info->flash_device_name, g_flash_info->flash_device_id,
120             flash_get_total_size() / 1024 );
121     }
122     else
123     {
124         /* Flash initialization error. */
125         if( cfi_devid != 0xffff && cfi_devid != 0x0000 )
126         {
127             printk( "Parallel flash device id %4.4x is not supported.\n",
128                 cfi_devid );
129         }
130         else
131             if( spi_devid != 0xffff && spi_devid != 0x0000 )
132             {
133                 printk( "Serial flash device id %4.4x is not supported.\n",
134                     spi_devid );
135             }
136             else
137                 printk( "Flash device is not found.\n" );
138     }
139
140     return( ret );
141 } /* flash_init */
142
143 /***************************************************************************
144  * Function Name: flash_sector_erase_int
145  * Description  : Erase the specfied flash sector.
146  * Returns      : FLASH_API_OK or FLASH_API_ERROR
147  ***************************************************************************/
148 int flash_sector_erase_int(unsigned short sector)
149 {
150     return( (g_flash_info)
151         ? (*g_flash_info->fn_flash_sector_erase_int) (sector)
152         : FLASH_API_ERROR );
153 } /* flash_sector_erase_int */
154
155 /***************************************************************************
156  * Function Name: flash_read_buf
157  * Description  : Reads from flash memory.
158  * Returns      : number of bytes read or FLASH_API_ERROR
159  ***************************************************************************/
160 int flash_read_buf(unsigned short sector, int offset, unsigned char *buffer,
161     int numbytes)
162 {
163     return( (g_flash_info)
164         ? (*g_flash_info->fn_flash_read_buf) (sector, offset, buffer, numbytes)
165         : FLASH_API_ERROR );
166 } /* flash_read_buf */
167
168 /***************************************************************************
169  * Function Name: flash_write_buf
170  * Description  : Writes to flash memory.
171  * Returns      : number of bytes written or FLASH_API_ERROR
172  ***************************************************************************/
173 int flash_write_buf(unsigned short sector, int offset, unsigned char *buffer,
174     int numbytes)
175 {
176     return( (g_flash_info)
177         ? (*g_flash_info->fn_flash_write_buf) (sector, offset, buffer, numbytes)
178         : FLASH_API_ERROR );
179 } /* flash_write_buf */
180
181 /***************************************************************************
182  * Function Name: flash_get_numsectors
183  * Description  : Returns the number of sectors in the flash device.
184  * Returns      : Number of sectors in the flash device.
185  ***************************************************************************/
186 int flash_get_numsectors(void)
187 {
188     return( (g_flash_info)
189         ? (*g_flash_info->fn_flash_get_numsectors) ()
190         : FLASH_API_ERROR );
191 } /* flash_get_numsectors */
192
193 /***************************************************************************
194  * Function Name: flash_get_sector_size
195  * Description  : Returns the number of bytes in the specfied flash sector.
196  * Returns      : Number of bytes in the specfied flash sector.
197  ***************************************************************************/
198 int flash_get_sector_size(unsigned short sector)
199 {
200     return( (g_flash_info)
201         ? (*g_flash_info->fn_flash_get_sector_size) (sector)
202         : FLASH_API_ERROR );
203 } /* flash_get_sector_size */
204
205 /***************************************************************************
206  * Function Name: flash_get_memptr
207  * Description  : Returns the base MIPS memory address for the specfied flash
208  *                sector.
209  * Returns      : Base MIPS memory address for the specfied flash sector.
210  ***************************************************************************/
211 unsigned char *flash_get_memptr(unsigned short sector)
212 {
213     return( (g_flash_info)
214         ? (*g_flash_info->fn_flash_get_memptr) (sector)
215         : NULL );
216 } /* flash_get_memptr */
217
218 /***************************************************************************
219  * Function Name: flash_get_blk
220  * Description  : Returns the flash sector for the specfied MIPS address.
221  * Returns      : Flash sector for the specfied MIPS address.
222  ***************************************************************************/
223 int flash_get_blk(int addr)
224 {
225     return( (g_flash_info)
226         ? (*g_flash_info->fn_flash_get_blk) (addr)
227         : FLASH_API_ERROR );
228 } /* flash_get_blk */
229
230 /***************************************************************************
231  * Function Name: flash_get_total_size
232  * Description  : Returns the number of bytes in the flash device.
233  * Returns      : Number of bytes in the flash device.
234  ***************************************************************************/
235 int flash_get_total_size(void)
236 {
237     return( (g_flash_info)
238         ? (*g_flash_info->fn_flash_get_total_size) ()
239         : FLASH_API_ERROR );
240 } /* flash_get_total_size */
241
242 /***************************************************************************
243  * Function Name: flash_get_total_memory_mapped_size
244  * Description  : Returns the number of bytes in the flash device.
245  * Returns      : Number of bytes in the flash device.
246  ***************************************************************************/
247 int flash_get_total_memory_mapped_size(void)
248 {
249     return( (g_flash_info)
250         ? (*g_flash_info->fn_flash_get_total_memory_mapped_size) ()
251         : FLASH_API_ERROR );
252 } /* flash_get_total_memory_mapped_size */
253