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
48 #if (INC_CFI_FLASH_DRIVER==1)
49 extern int cfi_flash_init(flash_device_info_t **flash_info);
51 #define cfi_flash_init(x) FLASH_API_ERROR
54 #if (INC_SPI_FLASH_DRIVER==1)
55 extern int spi_flash_init(flash_device_info_t **flash_info);
57 #define spi_flash_init(x) FLASH_API_ERROR
62 flash_device_info_t *g_flash_info = NULL;
63 int g_include_cfi_flash_driver = INC_CFI_FLASH_DRIVER;
64 int g_include_spi_flash_driver = INC_SPI_FLASH_DRIVER;
67 /***************************************************************************
68 * Function Name: flash_init
69 * Description : Initialize flash part.
70 * Returns : FLASH_API_OK or FLASH_API_ERROR
71 ***************************************************************************/
74 int ret = FLASH_API_ERROR;
75 flash_device_info_t *flash_info = NULL;
76 unsigned short cfi_devid = 0xffff;
77 unsigned short spi_devid = 0xffff;
80 if( g_include_cfi_flash_driver )
82 ret = cfi_flash_init( &flash_info );
83 if( ret == FLASH_API_OK )
85 g_flash_info = flash_info;
90 cfi_devid = flash_info->flash_device_id;
93 if( ret != FLASH_API_OK && g_include_spi_flash_driver )
95 ret = spi_flash_init( &flash_info );
96 if( ret == FLASH_API_OK )
98 g_flash_info = flash_info;
103 spi_devid = flash_info->flash_device_id;
106 if( ret == FLASH_API_OK )
108 /* Flash initialization OK. */
109 printk( "%s flash device: name %s, id 0x%4.4x, size %dKB\n", type,
110 g_flash_info->flash_device_name, g_flash_info->flash_device_id,
111 flash_get_total_size() / 1024 );
115 /* Flash initialization error. */
116 if( cfi_devid != 0xffff && cfi_devid != 0x0000 )
118 printk( "Parallel flash device id %4.4x is not supported.\n",
122 if( spi_devid != 0xffff && spi_devid != 0x0000 )
124 printk( "Serial flash device id %4.4x is not supported.\n",
128 printk( "Flash device is not found.\n" );
134 /***************************************************************************
135 * Function Name: flash_sector_erase_int
136 * Description : Erase the specfied flash sector.
137 * Returns : FLASH_API_OK or FLASH_API_ERROR
138 ***************************************************************************/
139 int flash_sector_erase_int(unsigned short sector)
141 return( (g_flash_info)
142 ? (*g_flash_info->fn_flash_sector_erase_int) (sector)
144 } /* flash_sector_erase_int */
146 /***************************************************************************
147 * Function Name: flash_read_buf
148 * Description : Reads from flash memory.
149 * Returns : number of bytes read or FLASH_API_ERROR
150 ***************************************************************************/
151 int flash_read_buf(unsigned short sector, int offset, unsigned char *buffer,
154 return( (g_flash_info)
155 ? (*g_flash_info->fn_flash_read_buf) (sector, offset, buffer, numbytes)
157 } /* flash_read_buf */
159 /***************************************************************************
160 * Function Name: flash_write_buf
161 * Description : Writes to flash memory.
162 * Returns : number of bytes written or FLASH_API_ERROR
163 ***************************************************************************/
164 int flash_write_buf(unsigned short sector, int offset, unsigned char *buffer,
167 return( (g_flash_info)
168 ? (*g_flash_info->fn_flash_write_buf) (sector, offset, buffer, numbytes)
170 } /* flash_write_buf */
172 /***************************************************************************
173 * Function Name: flash_get_numsectors
174 * Description : Returns the number of sectors in the flash device.
175 * Returns : Number of sectors in the flash device.
176 ***************************************************************************/
177 int flash_get_numsectors(void)
179 return( (g_flash_info)
180 ? (*g_flash_info->fn_flash_get_numsectors) ()
182 } /* flash_get_numsectors */
184 /***************************************************************************
185 * Function Name: flash_get_sector_size
186 * Description : Returns the number of bytes in the specfied flash sector.
187 * Returns : Number of bytes in the specfied flash sector.
188 ***************************************************************************/
189 int flash_get_sector_size(unsigned short sector)
191 return( (g_flash_info)
192 ? (*g_flash_info->fn_flash_get_sector_size) (sector)
194 } /* flash_get_sector_size */
196 /***************************************************************************
197 * Function Name: flash_get_memptr
198 * Description : Returns the base MIPS memory address for the specfied flash
200 * Returns : Base MIPS memory address for the specfied flash sector.
201 ***************************************************************************/
202 unsigned char *flash_get_memptr(unsigned short sector)
204 return( (g_flash_info)
205 ? (*g_flash_info->fn_flash_get_memptr) (sector)
207 } /* flash_get_memptr */
209 /***************************************************************************
210 * Function Name: flash_get_blk
211 * Description : Returns the flash sector for the specfied MIPS address.
212 * Returns : Flash sector for the specfied MIPS address.
213 ***************************************************************************/
214 int flash_get_blk(int addr)
216 return( (g_flash_info)
217 ? (*g_flash_info->fn_flash_get_blk) (addr)
219 } /* flash_get_blk */
221 /***************************************************************************
222 * Function Name: flash_get_total_size
223 * Description : Returns the number of bytes in the flash device.
224 * Returns : Number of bytes in the flash device.
225 ***************************************************************************/
226 int flash_get_total_size(void)
228 return( (g_flash_info)
229 ? (*g_flash_info->fn_flash_get_total_size) ()
231 } /* flash_get_total_size */
233 /***************************************************************************
234 * Function Name: flash_get_total_memory_mapped_size
235 * Description : Returns the number of bytes in the flash device.
236 * Returns : Number of bytes in the flash device.
237 ***************************************************************************/
238 int flash_get_total_memory_mapped_size(void)
240 return( (g_flash_info)
241 ? (*g_flash_info->fn_flash_get_total_memory_mapped_size) ()
243 } /* flash_get_total_memory_mapped_size */