| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <comdef.h> |
| 23 |
#include <sys.h> |
| 24 |
#include <spi_flash.h> |
| 25 |
#include <spi_flash_internal.h> |
| 26 |
#include <spi.h> |
| 27 |
#include <uart.h> |
| 28 |
|
| 29 |
#define IDCODE_CONT_LEN 0 |
| 30 |
#define IDCODE_PART_LEN 3 |
| 31 |
#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN) |
| 32 |
|
| 33 |
#define CMD_READ_ID 0x9f |
| 34 |
|
| 35 |
#define NOR 0 |
| 36 |
#define GIGANAND 1 |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static struct spi_flash g_spi_flash[1]; |
| 51 |
|
| 52 |
static const struct spi_flash_params spi_flash_table[] = |
| 53 |
{ |
| 54 |
{"Common_flash", 0x534654, 8,16,16,256, NOR}, |
| 55 |
}; |
| 56 |
|
| 57 |
struct spi_flash *spi_flash_probe_nor(struct spi_slave *spi, u8 *idcode) |
| 58 |
{ |
| 59 |
struct spi_flash_params *params; |
| 60 |
struct spi_flash *flash; |
| 61 |
u32 id = 0; |
| 62 |
static int i = 0; |
| 63 |
|
| 64 |
#if 0 |
| 65 |
id = ((idcode[2] << 16) | (idcode[1] << 8) | idcode[0]); |
| 66 |
if(id == 0x0) |
| 67 |
{ |
| 68 |
return NULL; |
| 69 |
} |
| 70 |
|
| 71 |
params = spi_flash_table; |
| 72 |
for (i = 0; spi_flash_table[i].name != NULL; i++) |
| 73 |
{ |
| 74 |
if ((spi_flash_table[i].id & 0xFFFFFF) == id) |
| 75 |
{ |
| 76 |
break; |
| 77 |
} |
| 78 |
} |
| 79 |
#endif |
| 80 |
flash = &g_spi_flash[0]; |
| 81 |
if (!flash) |
| 82 |
{ |
| 83 |
|
| 84 |
return NULL; |
| 85 |
} |
| 86 |
|
| 87 |
flash->name = spi_flash_table[i].name; |
| 88 |
if(spi_flash_table[i].flags == NOR) |
| 89 |
{ |
| 90 |
|
| 91 |
flash->write = spi_flash_cmd_write_mode; |
| 92 |
flash->erase = spi_flash_erase_mode; |
| 93 |
flash->read = spi_flash_read_mode; |
| 94 |
flash->page_size = 1 << spi_flash_table[i].l2_page_size; |
| 95 |
flash->sector_size = flash->page_size * spi_flash_table[i].pages_per_sector; |
| 96 |
flash->block_size = flash->sector_size * spi_flash_table[i].sectors_per_block; |
| 97 |
flash->size = flash->page_size * spi_flash_table[i].pages_per_sector |
| 98 |
* spi_flash_table[i].sectors_per_block |
| 99 |
* spi_flash_table[i].nr_blocks; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
return flash; |
| 105 |
} |
| 106 |
#if 1 |
| 107 |
static int print_id(u8 *idcode, int len) |
| 108 |
{ |
| 109 |
int i; |
| 110 |
|
| 111 |
uart_printf("idcode:0x"); |
| 112 |
for (i=len-1; i>=0; i--) |
| 113 |
print_ubyte_hex(idcode[i]); |
| 114 |
uart_printf("\r\n"); |
| 115 |
return 0; |
| 116 |
} |
| 117 |
#endif |
| 118 |
static int spi_read_id(struct spi_slave *spi, unsigned char cmd, void *response, u32 len) |
| 119 |
{ |
| 120 |
int ret = -1,ret1 = -1,ret2 = -1; |
| 121 |
unsigned char buf[4] = {0}; |
| 122 |
unsigned char buf_nor[1]; |
| 123 |
u8 *idcode = (u8 *)response; |
| 124 |
|
| 125 |
buf[0] = cmd; |
| 126 |
buf[1] = 0; |
| 127 |
buf[2] = 0; |
| 128 |
buf[3] = 0; |
| 129 |
|
| 130 |
buf_nor[0] = cmd; |
| 131 |
|
| 132 |
ret1 = spi_xfer(spi, 1*8, &buf[0], NULL, SPI_XFER_BEGIN, 8); |
| 133 |
ret2 = spi_xfer(spi, len*8, NULL, response, SPI_XFER_END, 8); |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
static struct spi_flash aic_flash; |
| 138 |
|
| 139 |
struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 140 |
unsigned int max_hz, unsigned int mode, unsigned int bus_width) |
| 141 |
{ |
| 142 |
struct spi_slave *spi; |
| 143 |
struct spi_flash *flash = &aic_flash; |
| 144 |
int ret = 0; |
| 145 |
u8 idcode[IDCODE_LEN]; |
| 146 |
|
| 147 |
spi = spi_setup_slave(bus, cs, max_hz, mode, bus_width); |
| 148 |
if (!spi) { |
| 149 |
|
| 150 |
return NULL; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
ret = spi_read_id(spi, CMD_READ_ID, idcode, sizeof(idcode)); |
| 155 |
if (ret) |
| 156 |
{ |
| 157 |
|
| 158 |
goto err_read_id; |
| 159 |
} |
| 160 |
|
| 161 |
print_id(idcode, sizeof(idcode)); |
| 162 |
|
| 163 |
flash = spi_flash_probe_nor(spi,idcode); |
| 164 |
if (!flash) |
| 165 |
{ |
| 166 |
goto err_manufacturer_probe; |
| 167 |
} |
| 168 |
|
| 169 |
flash->spi = spi; |
| 170 |
return flash; |
| 171 |
|
| 172 |
err_manufacturer_probe: |
| 173 |
err_read_id: |
| 174 |
|
| 175 |
return NULL; |
| 176 |
} |