4 Copyright (c) 2005 Broadcom Corporation
6 No portions of this material may be reproduced in any form without the
10 Irvine, California 92619
11 All information contained in this document is Broadcom Corporation
12 company private, proprietary, and trade secret.
16 /***************************************************************************
17 * File Name : flash_api.c
19 * Description: This file contains the implementation of the wrapper functions
20 * for the flash device interface.
21 ***************************************************************************/
25 #include "lib_types.h"
26 #include "lib_printf.h"
27 #include "lib_string.h"
30 #include <linux/kernel.h>
35 #include "flash_api.h"
43 #if !defined(INC_CFI_FLASH_DRIVER)
44 #define INC_CFI_FLASH_DRIVER 1
47 #if !defined(INC_SPI_FLASH_DRIVER)
48 #if defined(CONFIG_BCM96348) || defined(BCM96338)
49 #define INC_SPI_FLASH_DRIVER 1
51 #define INC_SPI_FLASH_DRIVER 0
57 #if (INC_CFI_FLASH_DRIVER==1)
58 extern int cfi_flash_init(flash_device_info_t **flash_info);
60 #define cfi_flash_init(x) FLASH_API_ERROR
63 #if (INC_SPI_FLASH_DRIVER==1)
64 extern int spi_flash_init(flash_device_info_t **flash_info);
66 #define spi_flash_init(x) FLASH_API_ERROR
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;
76 /***************************************************************************
77 * Function Name: flash_init
78 * Description : Initialize flash part.
79 * Returns : FLASH_API_OK or FLASH_API_ERROR
80 ***************************************************************************/
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;
89 if( g_include_cfi_flash_driver )
91 ret = cfi_flash_init( &flash_info );
92 if( ret == FLASH_API_OK )
94 g_flash_info = flash_info;
99 cfi_devid = flash_info->flash_device_id;
102 if( ret != FLASH_API_OK && g_include_spi_flash_driver )
104 ret = spi_flash_init( &flash_info );
105 if( ret == FLASH_API_OK )
107 g_flash_info = flash_info;
112 spi_devid = flash_info->flash_device_id;
115 if( ret == FLASH_API_OK )
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 );
124 /* Flash initialization error. */
125 if( cfi_devid != 0xffff && cfi_devid != 0x0000 )
127 printk( "Parallel flash device id %4.4x is not supported.\n",
131 if( spi_devid != 0xffff && spi_devid != 0x0000 )
133 printk( "Serial flash device id %4.4x is not supported.\n",
137 printk( "Flash device is not found.\n" );
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)
150 return( (g_flash_info)
151 ? (*g_flash_info->fn_flash_sector_erase_int) (sector)
153 } /* flash_sector_erase_int */
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,
163 return( (g_flash_info)
164 ? (*g_flash_info->fn_flash_read_buf) (sector, offset, buffer, numbytes)
166 } /* flash_read_buf */
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,
176 return( (g_flash_info)
177 ? (*g_flash_info->fn_flash_write_buf) (sector, offset, buffer, numbytes)
179 } /* flash_write_buf */
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)
188 return( (g_flash_info)
189 ? (*g_flash_info->fn_flash_get_numsectors) ()
191 } /* flash_get_numsectors */
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)
200 return( (g_flash_info)
201 ? (*g_flash_info->fn_flash_get_sector_size) (sector)
203 } /* flash_get_sector_size */
205 /***************************************************************************
206 * Function Name: flash_get_memptr
207 * Description : Returns the base MIPS memory address for the specfied flash
209 * Returns : Base MIPS memory address for the specfied flash sector.
210 ***************************************************************************/
211 unsigned char *flash_get_memptr(unsigned short sector)
213 return( (g_flash_info)
214 ? (*g_flash_info->fn_flash_get_memptr) (sector)
216 } /* flash_get_memptr */
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)
225 return( (g_flash_info)
226 ? (*g_flash_info->fn_flash_get_blk) (addr)
228 } /* flash_get_blk */
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)
237 return( (g_flash_info)
238 ? (*g_flash_info->fn_flash_get_total_size) ()
240 } /* flash_get_total_size */
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)
249 return( (g_flash_info)
250 ? (*g_flash_info->fn_flash_get_total_memory_mapped_size) ()
252 } /* flash_get_total_memory_mapped_size */