commit | author | age
|
8df7c2
|
1 |
/********************************************************************************* |
d973d6
|
2 |
* Copyright: (C) 2023 LingYun IoT System Studio |
G |
3 |
* All rights reserved. |
|
4 |
* |
|
5 |
* Filename: at24c.c |
|
6 |
* Description: This file is AT24Cxx EEPROM code |
|
7 |
* |
|
8 |
* Version: 1.0.0(10/08/23) |
8df7c2
|
9 |
* Author: Guo Wenxue <guowenxue@gmail.com> |
d973d6
|
10 |
* ChangeLog: 1, Release initial version on "10/08/23 17:52:00" |
8df7c2
|
11 |
* |
d973d6
|
12 |
* Pin connection: |
G |
13 |
* W25QXX Raspberry Pi 40Pin |
|
14 |
* VCC <---> Pin#1 (3.3V) |
|
15 |
* CS <---> Pin#24(CS) |
|
16 |
* DO <---> Pin#21(MISO) |
|
17 |
* GND <---> Pin#9 (GND) |
|
18 |
* CLK <---> Pin#23(SCLK) |
|
19 |
* DI <---> Pin#19(MOSI) |
8df7c2
|
20 |
* |
d973d6
|
21 |
* /boot/config.txt: |
G |
22 |
* dtparam=spi=on |
8df7c2
|
23 |
* |
G |
24 |
********************************************************************************/ |
|
25 |
|
|
26 |
#include <stdint.h> |
|
27 |
#include <unistd.h> |
|
28 |
#include <stdio.h> |
|
29 |
#include <stdlib.h> |
|
30 |
#include <string.h> |
|
31 |
#include <errno.h> |
|
32 |
#include <getopt.h> |
|
33 |
#include <fcntl.h> |
|
34 |
#include <time.h> |
|
35 |
#include <sys/ioctl.h> |
|
36 |
#include <linux/ioctl.h> |
|
37 |
#include <sys/stat.h> |
|
38 |
#include <linux/types.h> |
|
39 |
#include <linux/spi/spidev.h> |
|
40 |
|
|
41 |
#include "w25qflash.h" |
|
42 |
|
|
43 |
#define CONFIG_SPINOR_DEBUG |
|
44 |
|
|
45 |
#ifdef CONFIG_SPINOR_DEBUG |
|
46 |
#include <stdio.h> |
|
47 |
#define spinor_print(format,args...) printf(format, ##args) |
|
48 |
#else |
|
49 |
#define spinor_print(format,args...) do{} while(0) |
|
50 |
#endif |
|
51 |
|
|
52 |
#define spinor_Delay(delay) usleep(delay*1000) |
|
53 |
|
|
54 |
/*+-----------------------+ |
|
55 |
*| Entry Functions | |
|
56 |
*+-----------------------+*/ |
|
57 |
|
75843d
|
58 |
void dump_buf(const char *prompt, char *buffer, size_t length); |
8df7c2
|
59 |
|
G |
60 |
int main (int argc, char **argv) |
|
61 |
{ |
|
62 |
spinor_test(); |
|
63 |
|
|
64 |
return 0; |
|
65 |
} |
|
66 |
|
|
67 |
|
|
68 |
/*+-----------------------+ |
|
69 |
*| SPI API Functions | |
|
70 |
*+-----------------------+*/ |
|
71 |
|
|
72 |
#define SPI_DEV "/dev/spidev0.0" |
|
73 |
#define SPI_BITS 8 |
|
74 |
#define SPI_MODE 0//(SPI_CPHA|SPI_CPOL) |
|
75 |
#define SPI_SPEED 500000 |
|
76 |
|
|
77 |
#define SPI_DUMMY_BYTE 0xA5 |
|
78 |
|
|
79 |
void spinor_dev_init(struct spi_info *spi) |
|
80 |
{ |
|
81 |
uint8_t bits = SPI_BITS; |
|
82 |
uint32_t speed = SPI_SPEED; |
|
83 |
uint32_t mode = SPI_MODE; |
|
84 |
uint32_t request; |
|
85 |
int ret; |
|
86 |
|
|
87 |
spi->hspi = open(SPI_DEV, O_RDWR); |
|
88 |
if (spi->hspi < 0) |
|
89 |
{ |
|
90 |
spinor_print("ERROR: open device %s failure: %s\r\n", SPI_DEV, strerror(errno)); |
|
91 |
return ; |
|
92 |
} |
|
93 |
|
|
94 |
/* |
|
95 |
* spi mode |
|
96 |
*/ |
|
97 |
request = mode; |
|
98 |
if( ioctl(spi->hspi, SPI_IOC_WR_MODE32, &mode) < 0 ) |
|
99 |
{ |
|
100 |
spinor_print("ERROR: can't set spi mode\n"); |
|
101 |
return ; |
|
102 |
} |
|
103 |
|
|
104 |
if( ioctl(spi->hspi, SPI_IOC_RD_MODE32, &mode) < 0 ) |
|
105 |
{ |
|
106 |
spinor_print("ERROR: can't get spi mode\n"); |
|
107 |
return ; |
|
108 |
} |
|
109 |
|
|
110 |
if (request != mode) |
|
111 |
{ |
|
112 |
spinor_print("WARNING: device does not support requested mode 0x%x\n", request); |
|
113 |
} |
|
114 |
|
|
115 |
/* |
|
116 |
* bits per word |
|
117 |
*/ |
|
118 |
if( ioctl(spi->hspi, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0 ) |
|
119 |
{ |
|
120 |
spinor_print("ERROR: can't set bits per word"); |
|
121 |
return ; |
|
122 |
} |
|
123 |
|
|
124 |
if( ioctl(spi->hspi, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0 ) |
|
125 |
{ |
|
126 |
spinor_print("ERROR: can't get bits per word"); |
|
127 |
return ; |
|
128 |
} |
|
129 |
|
|
130 |
/* |
|
131 |
* max speed hz |
|
132 |
*/ |
|
133 |
if( ioctl(spi->hspi, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0 ) |
|
134 |
{ |
|
135 |
spinor_print("ERROR: can't set max speed hz"); |
|
136 |
return ; |
|
137 |
} |
|
138 |
|
|
139 |
if( ioctl(spi->hspi, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0 ) |
|
140 |
{ |
|
141 |
spinor_print("ERROR: can't get max speed hz"); |
|
142 |
return ; |
|
143 |
} |
|
144 |
|
|
145 |
printf("spi mode: 0x%x\n", mode); |
|
146 |
printf("bits per word: %u\n", bits); |
|
147 |
printf("max speed: %u Hz (%u kHz)\n", speed, speed/1000); |
|
148 |
} |
|
149 |
|
|
150 |
void spi_cs_enable(struct spi_info *spi) |
|
151 |
{ |
|
152 |
/* |
|
153 |
* No need set CS in Linux because the device name /dev/spi0.0 |
|
154 |
* will choose the first slave device, second slave is spi0.1 |
|
155 |
*/ |
|
156 |
(void)0; |
|
157 |
} |
|
158 |
|
|
159 |
void spi_cs_disable(struct spi_info *spi) |
|
160 |
{ |
|
161 |
(void)0; |
|
162 |
} |
|
163 |
|
|
164 |
void spi_xcmd(struct spi_info *spi, uint8_t command) |
|
165 |
{ |
|
166 |
uint8_t rxbyte; |
|
167 |
struct spi_ioc_transfer tr = { |
|
168 |
.tx_buf = (unsigned long)&command, |
|
169 |
.rx_buf = (unsigned long)&rxbyte, |
|
170 |
.len = 1, |
|
171 |
.delay_usecs = 0, |
|
172 |
.speed_hz = SPI_SPEED, |
|
173 |
.bits_per_word = SPI_BITS, |
|
174 |
}; |
|
175 |
|
|
176 |
spi->select(spi); |
|
177 |
|
|
178 |
if( ioctl(spi->hspi, SPI_IOC_MESSAGE(1), &tr) < 0 ) |
|
179 |
{ |
|
180 |
spinor_print("ERROR: can't send spi message:%s\n", strerror(errno)); |
|
181 |
} |
|
182 |
|
|
183 |
spi->deselect(spi); |
|
184 |
|
|
185 |
return; |
|
186 |
} |
|
187 |
|
|
188 |
void spi_xfer(struct spi_info *spi, uint8_t *send_buf, uint8_t *recv_buf, int bytes) |
|
189 |
{ |
|
190 |
struct spi_ioc_transfer tr = { |
|
191 |
.tx_buf = (unsigned long)send_buf, |
|
192 |
.rx_buf = (unsigned long)recv_buf, |
|
193 |
.len = bytes, |
|
194 |
.delay_usecs = 0, |
|
195 |
.speed_hz = SPI_SPEED, |
|
196 |
.bits_per_word = SPI_BITS, |
|
197 |
}; |
|
198 |
|
|
199 |
spi->select(spi); |
|
200 |
|
|
201 |
if( ioctl(spi->hspi, SPI_IOC_MESSAGE(1), &tr) < 0 ) |
|
202 |
{ |
|
203 |
spinor_print("ERROR: can't send spi message:%s\n", strerror(errno)); |
|
204 |
} |
|
205 |
|
|
206 |
spi->deselect(spi); |
|
207 |
|
|
208 |
return; |
|
209 |
} |
|
210 |
|
|
211 |
#define SPI_INFO(_hspi, _cs_gpio, _cs_pin) {\ |
|
212 |
.hspi = _hspi, \ |
|
213 |
.select = spi_cs_enable, \ |
|
214 |
.deselect = spi_cs_disable, \ |
|
215 |
.xcmd = spi_xcmd, \ |
|
216 |
.xfer = spi_xfer, \ |
|
217 |
} |
|
218 |
|
|
219 |
static struct spi_info spinor_spi = SPI_INFO(-1, W25Q_CS_PORT, W25Q_CS_PIN); |
|
220 |
|
|
221 |
|
|
222 |
/*+-----------------------+ |
|
223 |
*| W25Q SPI Norflash ID | |
|
224 |
*+-----------------------+*/ |
|
225 |
|
|
226 |
#define W25Q_PAGSIZE 256 /* 1Page=256B */ |
|
227 |
#define W25Q_SECSIZE 4096 /* 1Sector=16Pages=4KB */ |
|
228 |
#define W25Q_BLKSIZE 65536 /* 1Block=16Sector=64KB */ |
|
229 |
|
|
230 |
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) |
|
231 |
|
|
232 |
/* JEDEC ID the 3rd bytes is the storage capacity */ |
|
233 |
#pragma GCC diagnostic ignored "-Wshift-count-overflow" |
|
234 |
#define CAPCITY_ID(id) (1UL<<(id&0xFF)) |
|
235 |
#define NOR_INFO(_name, _jedec_id) \ |
|
236 |
.name = _name, \ |
|
237 |
.jedec_id = _jedec_id, \ |
|
238 |
.block_size = W25Q_BLKSIZE, \ |
|
239 |
.sector_size= W25Q_SECSIZE, \ |
|
240 |
.page_size = W25Q_PAGSIZE, \ |
|
241 |
.capacity = CAPCITY_ID(_jedec_id), \ |
|
242 |
.n_blocks = CAPCITY_ID(_jedec_id)/W25Q_BLKSIZE, \ |
|
243 |
.n_sectors = CAPCITY_ID(_jedec_id)/W25Q_SECSIZE, \ |
|
244 |
.n_pages = CAPCITY_ID(_jedec_id)/W25Q_PAGSIZE, \ |
|
245 |
|
|
246 |
static struct flash_info spinor_ids[] = { |
|
247 |
{ NOR_INFO("W25Q512", 0xef4020) }, |
|
248 |
{ NOR_INFO("W25Q256", 0xef4019) }, |
|
249 |
{ NOR_INFO("W25Q128", 0xef4018) }, |
|
250 |
{ NOR_INFO("W25Q64", 0xef4017) }, |
|
251 |
{ NOR_INFO("W25Q32", 0xef4016) }, |
|
252 |
{ NOR_INFO("W25Q16", 0xef4015) }, |
|
253 |
{ NOR_INFO("W25Q80", 0xef4014) }, |
|
254 |
{ NOR_INFO("W25Q40", 0xef4013) }, |
|
255 |
{ NOR_INFO("W25Q20", 0xef4012) }, |
|
256 |
{ NOR_INFO("W25Q10", 0xef4011) }, |
|
257 |
}; |
|
258 |
|
|
259 |
/*+-------------------------------+ |
|
260 |
*| SPI Norflash HighLevel API | |
|
261 |
*+-------------------------------+*/ |
|
262 |
|
|
263 |
/* SPI Norflash API test function */ |
|
264 |
void spinor_test(void) |
|
265 |
{ |
|
266 |
spinor_info_t spinor; |
|
267 |
int i; |
|
268 |
uint8_t buf[W25Q_PAGSIZE*2]; |
|
269 |
|
|
270 |
if( spinor_init(&spinor) < 0 ) |
|
271 |
return ; |
|
272 |
|
|
273 |
//spinor_erase_chip(&spinor); |
|
274 |
//spinor_erase_block(&spinor, 1, W25Q_BLKSIZE); |
|
275 |
spinor_erase_sector(&spinor, 1, W25Q_SECSIZE); |
|
276 |
|
|
277 |
memset(buf, 0, sizeof(buf)); |
|
278 |
spinor_read(&spinor, 0, buf, sizeof(buf)); |
|
279 |
dump_buf("<<<Read data after erase:\n", (char *)buf, sizeof(buf)); |
|
280 |
|
|
281 |
/* Read/Write data test on address not page align */ |
|
282 |
for(i=0; i<sizeof(buf); i++) |
|
283 |
buf[i] = i; |
|
284 |
spinor_write(&spinor, 16, buf, W25Q_PAGSIZE); |
|
285 |
|
|
286 |
memset(buf, 0, sizeof(buf)); |
|
287 |
spinor_read(&spinor, 0, buf, W25Q_PAGSIZE*2); |
|
288 |
dump_buf("<<<Read data after write:\n", (char *)buf, sizeof(buf)); |
|
289 |
|
|
290 |
return ; |
|
291 |
} |
|
292 |
|
|
293 |
/* Initial SPI and detect the flash chip. */ |
|
294 |
int spinor_init(struct spinor_info *spinor) |
|
295 |
{ |
|
296 |
spinor->spi = &spinor_spi; |
|
297 |
|
|
298 |
spinor_dev_init(spinor->spi); |
|
299 |
|
|
300 |
if( !spinor_detect_by_jedec(spinor) ) |
|
301 |
return -1; |
|
302 |
|
|
303 |
printf("Norflash %s detected, capacity %llu KB, %u blocks, %u sectors, %u pages.\r\n", |
|
304 |
spinor->flash->name, spinor->flash->capacity>>10, |
|
305 |
spinor->flash->n_blocks, spinor->flash->n_sectors, spinor->flash->n_pages); |
|
306 |
|
|
307 |
return 0; |
|
308 |
} |
|
309 |
|
|
310 |
/* Description: Erase whole flash chip. |
|
311 |
* Reference : P60, 8.2.32 Chip Erase (C7h / 60h) |
|
312 |
*/ |
|
313 |
int spinor_erase_chip(struct spinor_info *spinor) |
|
314 |
{ |
|
315 |
struct spi_info *spi = spinor->spi; |
|
316 |
|
|
317 |
while (spinor->lock == 1) |
|
318 |
spinor_Delay(1); |
|
319 |
|
|
320 |
spinor->lock = 1; |
|
321 |
|
|
322 |
#ifdef CONFIG_SPINOR_DEBUG |
|
323 |
printf("Norflash EraseChip Begin...\r\n"); |
|
324 |
#endif |
|
325 |
|
|
326 |
spinor_write_enable(spi); |
|
327 |
|
|
328 |
spi->xcmd(spi, SPINOR_OP_CHIP_ERASE); |
|
329 |
spinor_WaitForWriteEnd(spi); |
|
330 |
|
|
331 |
#ifdef CONFIG_SPINOR_DEBUG |
|
332 |
printf("Norflash EraseChip done.\r\n"); |
|
333 |
#endif |
|
334 |
|
|
335 |
spinor_Delay(10); |
|
336 |
spinor->lock = 0; |
|
337 |
|
|
338 |
return 0; |
|
339 |
} |
|
340 |
|
|
341 |
/* Description: Erase blocks by 64KiB, |
|
342 |
* Reference : P59, 8.2.31 64KB Block Erase with 4-Byte Address (DCh) |
|
343 |
* @address is the erase start physical address, which can be not block alignment such as 0x10001. |
|
344 |
* @size is the erase size, which can be larger than a block such as 4097, and it will erase 2 blocks; |
|
345 |
*/ |
|
346 |
int spinor_erase_block(struct spinor_info *spinor, uint32_t address, uint32_t size) |
|
347 |
{ |
|
348 |
struct spi_info *spi = spinor->spi; |
|
349 |
struct flash_info *flash = spinor->flash; |
|
350 |
uint32_t block, first, last; |
|
351 |
uint32_t addr; |
|
352 |
uint8_t buf[5]; |
|
353 |
int bytes = 0; |
|
354 |
|
|
355 |
while (spinor->lock == 1) |
|
356 |
spinor_Delay(1); |
|
357 |
|
|
358 |
spinor->lock = 1; |
|
359 |
|
|
360 |
/* find first and last erase block */ |
|
361 |
first = address / flash->block_size; |
|
362 |
last = (address+size-1) / flash->block_size; |
|
363 |
|
|
364 |
#ifdef CONFIG_SPINOR_DEBUG |
|
365 |
printf("Norflash Erase %d Bytes Block@0x%x Begin...\r\n", size, address); |
|
366 |
#endif |
|
367 |
|
|
368 |
/* start erase all the blocks */ |
|
369 |
for( block=first; block<=last; block++) |
|
370 |
{ |
|
371 |
addr = block * flash->sector_size; |
|
372 |
#ifdef CONFIG_SPINOR_DEBUG |
|
373 |
printf("Norflash Erase Block@%x ...\r\n", addr); |
|
374 |
#endif |
|
375 |
spinor_WaitForWriteEnd(spi); |
|
376 |
spinor_write_enable(spi); |
|
377 |
|
|
378 |
if (spinor->flash->n_blocks >= 512 ) /* larger than W25Q256 */ |
|
379 |
{ |
|
380 |
buf[bytes++] = SPINOR_OP_BE_4K_4B; |
|
381 |
buf[bytes++] = (addr & 0xFF000000) >> 24; |
|
382 |
} |
|
383 |
else |
|
384 |
{ |
|
385 |
buf[bytes++] = SPINOR_OP_BE_4K; |
|
386 |
} |
|
387 |
buf[bytes++] = (addr & 0xFF0000) >> 16 ; |
|
388 |
buf[bytes++] = (addr & 0xFF00) >> 8 ; |
|
389 |
buf[bytes++] = (addr & 0xFF); |
|
390 |
|
|
391 |
spi->xfer(spi, buf, NULL, bytes); |
|
392 |
|
|
393 |
spinor_WaitForWriteEnd(spi); |
|
394 |
} |
|
395 |
|
|
396 |
#ifdef CONFIG_SPINOR_DEBUG |
|
397 |
printf("Norflash EraseBlock@0x%x done.\r\n", address); |
|
398 |
spinor_Delay(100); |
|
399 |
#endif |
|
400 |
|
|
401 |
spinor_Delay(1); |
|
402 |
spinor->lock = 0; |
|
403 |
|
|
404 |
return 0; |
|
405 |
} |
|
406 |
|
|
407 |
/* Description: Erase sectors by 4KiB |
|
408 |
* Reference : P56, 8.2.28 Sector Erase with 4-Byte Address (21h) |
|
409 |
* @address is the erase start physical address, which can be not sector alignment such as 0x1001. |
|
410 |
* @size is the erase size, which can be larger than a sector such as 4097, and it will erase 2 sectors; |
|
411 |
*/ |
|
412 |
int spinor_erase_sector(struct spinor_info *spinor, uint32_t address, uint32_t size) |
|
413 |
{ |
|
414 |
struct spi_info *spi = spinor->spi; |
|
415 |
struct flash_info *flash = spinor->flash; |
|
416 |
uint32_t sector, first, last; |
|
417 |
uint32_t addr; |
|
418 |
uint8_t buf[5]; |
|
419 |
int bytes = 0; |
|
420 |
|
|
421 |
while (spinor->lock == 1) |
|
422 |
spinor_Delay(1); |
|
423 |
|
|
424 |
spinor->lock = 1; |
|
425 |
|
|
426 |
/* find first and last erase sector */ |
|
427 |
first = address / flash->sector_size; |
|
428 |
last = (address+size-1) / flash->sector_size; |
|
429 |
|
|
430 |
#ifdef CONFIG_SPINOR_DEBUG |
|
431 |
printf("Norflash Erase %d Bytes Sector@0x%x Begin...\r\n", size, address); |
|
432 |
#endif |
|
433 |
|
|
434 |
/* start erase all the sectors */ |
|
435 |
for( sector=first; sector<=last; sector++) |
|
436 |
{ |
|
437 |
addr = sector * flash->sector_size; |
|
438 |
#ifdef CONFIG_SPINOR_DEBUG |
|
439 |
printf("Norflash Erase Sector@%x ...\r\n", addr); |
|
440 |
#endif |
|
441 |
|
|
442 |
spinor_WaitForWriteEnd(spi); |
|
443 |
spinor_write_enable(spi); |
|
444 |
|
|
445 |
if (spinor->flash->n_blocks >= 512 ) /* larger than W25Q256 */ |
|
446 |
{ |
|
447 |
buf[bytes++] = SPINOR_OP_SE_4B; |
|
448 |
buf[bytes++] = (addr & 0xFF000000) >> 24; |
|
449 |
} |
|
450 |
else |
|
451 |
{ |
|
452 |
buf[bytes++] = SPINOR_OP_SE; |
|
453 |
} |
|
454 |
buf[bytes++] = (addr & 0xFF0000) >> 16 ; |
|
455 |
buf[bytes++] = (addr & 0xFF00) >> 8 ; |
|
456 |
buf[bytes++] = (addr & 0xFF); |
|
457 |
|
|
458 |
spi->xfer(spi, buf, NULL, bytes); |
|
459 |
|
|
460 |
spinor_WaitForWriteEnd(spi); |
|
461 |
} |
|
462 |
|
|
463 |
#ifdef CONFIG_SPINOR_DEBUG |
|
464 |
printf("Norflash EraseSector@0x%x done.\r\n", address); |
|
465 |
#endif |
|
466 |
|
|
467 |
spinor_Delay(1); |
|
468 |
spinor->lock = 0; |
|
469 |
|
|
470 |
return 0; |
|
471 |
} |
|
472 |
|
|
473 |
/* P32: 10.2.14 Page Program (02h) */ |
|
474 |
int spinor_write(struct spinor_info *spinor, uint32_t address, uint8_t *data, uint32_t size) |
|
475 |
{ |
|
476 |
struct spi_info *spi = spinor->spi; |
|
477 |
struct flash_info *flash = spinor->flash; |
|
478 |
uint32_t page, first, last; |
|
479 |
uint32_t addr, ofset, len; |
|
480 |
uint8_t buf[W25Q_PAGSIZE+5]; |
|
481 |
int bytes = 0; |
|
482 |
|
|
483 |
if( address+size > spinor->flash->capacity ) |
|
484 |
return -1; |
|
485 |
|
|
486 |
while (spinor->lock == 1) |
|
487 |
spinor_Delay(1); |
|
488 |
|
|
489 |
spinor->lock = 1; |
|
490 |
|
|
491 |
/* find first and last write page */ |
|
492 |
first = address / flash->page_size; |
|
493 |
last = (address+size-1) / flash->page_size; |
|
494 |
|
|
495 |
#ifdef CONFIG_SPINOR_DEBUG |
|
496 |
printf("Norflash Write %d Bytes to addr@0x%x Begin...\r\n", size, address); |
|
497 |
#endif |
|
498 |
|
|
499 |
/* address in page and offset in buffer */ |
|
500 |
addr = address; |
|
501 |
ofset = 0; |
|
502 |
|
|
503 |
/* start write all the pages */ |
|
504 |
for( page=first; page<=last; page++) |
|
505 |
{ |
|
506 |
len = flash->page_size - (addr%flash->page_size); |
|
507 |
len = len > size ? size : len; |
|
508 |
bytes = 0; |
|
509 |
|
|
510 |
#ifdef CONFIG_SPINOR_DEBUG |
|
511 |
printf("Norflash write addr@0x%x, %u bytes\r\n", addr, len); |
|
512 |
#endif |
|
513 |
|
|
514 |
spinor_WaitForWriteEnd(spi); |
|
515 |
spinor_write_enable(spi); |
|
516 |
|
|
517 |
if (spinor->flash->n_blocks >= 512 ) |
|
518 |
{ |
|
519 |
buf[bytes++] = SPINOR_OP_PP_4B; |
|
520 |
buf[bytes++] = (addr & 0xFF000000) >> 24; |
|
521 |
} |
|
522 |
else |
|
523 |
{ |
|
524 |
buf[bytes++] = SPINOR_OP_PP; |
|
525 |
} |
|
526 |
buf[bytes++] = (addr & 0xFF0000) >> 16 ; |
|
527 |
buf[bytes++] = (addr & 0xFF00) >> 8 ; |
|
528 |
buf[bytes++] = (addr & 0xFF); |
|
529 |
|
|
530 |
/* send command and data */ |
|
531 |
memcpy(&buf[bytes], data+ofset, len); |
|
532 |
bytes += len; |
|
533 |
spi->xfer(spi, buf, NULL, bytes); |
|
534 |
|
|
535 |
spinor_WaitForWriteEnd(spi); |
|
536 |
|
|
537 |
addr += len; |
|
538 |
ofset += len; |
|
539 |
size -= len; |
|
540 |
} |
|
541 |
|
|
542 |
#ifdef CONFIG_SPINOR_DEBUG |
|
543 |
printf("Norflash WriteByte@0x%x done.\r\n", address); |
|
544 |
#endif |
|
545 |
|
|
546 |
spinor_Delay(1); |
|
547 |
spinor->lock = 0; |
|
548 |
|
|
549 |
return 0; |
|
550 |
} |
|
551 |
|
|
552 |
/* Description: The Fast Read instruction can read the entire memory chip. |
|
553 |
* Reference : P41, 8.2.13 Fast Read with 4-Byte Address (0Ch) |
|
554 |
* @address is the read start physical address, which can be not page alignment such as 0x101. |
|
555 |
* @size is the read size, which can be larger than a page such as 257, and it will read 2 pages; |
|
556 |
*/ |
|
557 |
int spinor_read(struct spinor_info *spinor, uint32_t address, uint8_t *data, uint32_t size) |
|
558 |
{ |
|
559 |
struct spi_info *spi = spinor->spi; |
|
560 |
uint8_t buf[W25Q_PAGSIZE+6]; |
|
561 |
int bytes = 0; |
|
562 |
int ofset; |
|
563 |
uint32_t addr = address; |
|
564 |
|
|
565 |
if( address+size > spinor->flash->capacity ) |
|
566 |
return -1; |
|
567 |
|
|
568 |
while (spinor->lock == 1) |
|
569 |
spinor_Delay(1); |
|
570 |
|
|
571 |
spinor->lock = 1; |
|
572 |
|
|
573 |
#ifdef CONFIG_SPINOR_DEBUG |
|
574 |
printf("Norflash Read %d Bytes from addr@0x%x Begin...\r\n", size, address); |
|
575 |
#endif |
|
576 |
|
|
577 |
while( size > 0 ) |
|
578 |
{ |
|
579 |
bytes = size>W25Q_PAGSIZE ? W25Q_PAGSIZE : size; |
|
580 |
memset(buf, SPI_DUMMY_BYTE, sizeof(buf)); |
|
581 |
ofset = 0; |
|
582 |
|
|
583 |
#ifdef CONFIG_SPINOR_DEBUG |
|
584 |
printf("Norflash read addr@0x%x, %d bytes\r\n", addr, bytes); |
|
585 |
#endif |
|
586 |
|
|
587 |
/* send instruction and address */ |
|
588 |
if (spinor->flash->n_blocks >= 512 ) |
|
589 |
{ |
|
590 |
buf[ofset++] = SPINOR_OP_READ_FAST_4B; |
|
591 |
buf[ofset++] = (addr & 0xFF000000) >> 24; |
|
592 |
} |
|
593 |
else |
|
594 |
{ |
|
595 |
buf[ofset++] = SPINOR_OP_READ_FAST; |
|
596 |
} |
|
597 |
buf[ofset++] = (addr & 0xFF0000) >> 16 ; |
|
598 |
buf[ofset++] = (addr & 0xFF00) >> 8 ; |
|
599 |
buf[ofset++] = (addr & 0xFF); |
|
600 |
|
|
601 |
ofset += 1; /* Skip first dummy byte */ |
|
602 |
|
|
603 |
/* Send command and read data out */ |
|
604 |
spi->xfer(spi, buf, buf, ofset+bytes); |
|
605 |
memcpy(data, &buf[ofset], bytes); |
|
606 |
|
|
607 |
size -= bytes; |
|
608 |
addr += bytes; |
|
609 |
data += bytes; |
|
610 |
} |
|
611 |
|
|
612 |
#ifdef CONFIG_SPINOR_DEBUG |
|
613 |
printf("Norflash ReadBytes@0x%x done.\r\n", address); |
|
614 |
#endif |
|
615 |
spinor->lock = 0; |
|
616 |
|
|
617 |
return 0; |
|
618 |
} |
|
619 |
|
|
620 |
/*+-------------------------------+ |
|
621 |
*| SPI Norflash LowLevel API | |
|
622 |
*+-------------------------------+*/ |
|
623 |
|
|
624 |
/* Detect the norflash by JEDEC ID */ |
|
625 |
int spinor_detect_by_jedec(struct spinor_info *spinor) |
|
626 |
{ |
|
627 |
uint32_t jedec_id; |
|
628 |
int i, found = 0; |
|
629 |
|
|
630 |
jedec_id = spinor_read_jedecid(spinor->spi); |
|
631 |
|
|
632 |
for(i=0; i<ARRAY_SIZE(spinor_ids); i++) |
|
633 |
{ |
|
634 |
if(spinor_ids[i].jedec_id == jedec_id) |
|
635 |
{ |
|
636 |
found = 1; |
|
637 |
spinor->flash = &spinor_ids[i]; |
|
638 |
break; |
|
639 |
} |
|
640 |
} |
|
641 |
|
|
642 |
printf("Detect JEDEC ID[0x%x], Norflash %s found\r\n", jedec_id, found?spinor->flash->name:"not"); |
|
643 |
return found; |
|
644 |
} |
|
645 |
|
|
646 |
/* Description: Read the chipset UNIQUE ID. |
|
647 |
* Reference : P68, 8.2.40 Read Unique ID Number (4Bh) |
|
648 |
*/ |
|
649 |
int spinor_read_uniqid(struct spi_info *spi, uint8_t *uniq_id) |
|
650 |
{ |
|
651 |
uint8_t i; |
|
652 |
uint8_t buf[13]; /* Instruction(1B) + Dummy(4B) + UID(8B)*/ |
|
653 |
|
|
654 |
if( !uniq_id ) |
|
655 |
return -1; |
|
656 |
|
|
657 |
buf[0] = SPINOR_OP_RDUID; |
|
658 |
spi->xfer(spi, buf, buf, sizeof(buf)); |
|
659 |
|
|
660 |
/* Skip 4 bytes dummy bytes */ |
|
661 |
for (i=0; i<8; i++) |
|
662 |
{ |
|
663 |
uniq_id[i] = buf[5+i]; |
|
664 |
} |
|
665 |
|
|
666 |
return 0; |
|
667 |
} |
|
668 |
|
|
669 |
/* Description: Read the chipset JEDEC ID. |
|
670 |
* Reference : P69, 8.2.41 Read JEDEC ID (9Fh) |
|
671 |
*/ |
|
672 |
uint32_t spinor_read_jedecid(struct spi_info *spi) |
|
673 |
{ |
|
674 |
uint32_t jedec_id = 0x0; |
|
675 |
uint8_t buf[4]; |
|
676 |
|
|
677 |
buf[0] = SPINOR_OP_RDID; |
|
678 |
spi->xfer(spi, buf, buf, sizeof(buf)); |
|
679 |
jedec_id = (buf[1] << 16) | (buf[2] << 8) | buf[3]; |
|
680 |
|
|
681 |
return jedec_id; |
|
682 |
} |
|
683 |
|
|
684 |
/* Description: Write Enable |
|
685 |
* Reference : P31, 8.2.1 Write Enable (06h) |
|
686 |
*/ |
|
687 |
void spinor_write_enable(struct spi_info *spi) |
|
688 |
{ |
|
689 |
spi->xcmd(spi, SPINOR_OP_WREN); |
|
690 |
|
|
691 |
spinor_Delay(1); |
|
692 |
} |
|
693 |
|
|
694 |
/* Description: Write Disable |
|
695 |
* Reference : P32, 8.2.3 Write Disable (04h) |
|
696 |
*/ |
|
697 |
void spinor_write_disable(struct spi_info *spi) |
|
698 |
{ |
|
699 |
spi->xcmd(spi, SPINOR_OP_WRDI); |
|
700 |
|
|
701 |
spinor_Delay(1); |
|
702 |
} |
|
703 |
|
|
704 |
/* Description: Read Status Register |
|
705 |
* Reference : P32, 8.2.4 Read Status Register-1 (05h), Status Register-2 (35h) & Status Register-3 (15h) |
|
706 |
*/ |
|
707 |
uint8_t spinor_read_status_reg(struct spi_info *spi, uint8_t reg) |
|
708 |
{ |
|
709 |
uint8_t cmd[REG_STATUS_MAX] = { SPINOR_OP_RDSR1 , SPINOR_OP_RDSR2, SPINOR_OP_RDSR3 }; /* Status Register 1~3 */ |
|
710 |
uint8_t buf[2]; |
|
711 |
|
|
712 |
if( reg>= REG_STATUS_MAX ) |
|
713 |
return 0xFF; |
|
714 |
|
|
715 |
buf[0] = cmd[reg]; |
|
716 |
buf[1] = SPI_DUMMY_BYTE; |
|
717 |
spi->xfer(spi, buf, buf, sizeof(buf)); |
|
718 |
|
|
719 |
return buf[1]; |
|
720 |
} |
|
721 |
|
|
722 |
/* Description: Write Status Register |
|
723 |
* Reference : P33, 8.2.5 Write Status Register-1 (01h), Status Register-2 (31h) & Status Register-3 (11h) |
|
724 |
*/ |
|
725 |
void spinor_write_status_reg(struct spi_info *spi, uint8_t reg, uint8_t value) |
|
726 |
{ |
|
727 |
uint8_t cmd[REG_STATUS_MAX] = { SPINOR_OP_WRSR1 , SPINOR_OP_WRSR2, SPINOR_OP_WRSR3 }; /* Status Register 1~3 */ |
|
728 |
uint8_t buf[2]; |
|
729 |
|
|
730 |
if( reg>= REG_STATUS_MAX ) |
|
731 |
return ; |
|
732 |
|
|
733 |
buf[0] = cmd[reg]; |
|
734 |
buf[1] = value; |
|
735 |
spi->xfer(spi, buf, buf, sizeof(buf)); |
|
736 |
} |
|
737 |
|
|
738 |
/* Description: Wait flash program/erase finished by read Status Register for BUSY bit |
|
739 |
* Reference : P15, 7.1 Status Registers |
|
740 |
*/ |
|
741 |
void spinor_WaitForWriteEnd(struct spi_info *spi) |
|
742 |
{ |
|
743 |
uint8_t buf[2]; |
|
744 |
|
|
745 |
spinor_Delay(1); |
|
746 |
|
|
747 |
do |
|
748 |
{ |
|
749 |
buf[0] = SPINOR_OP_RDSR1; |
|
750 |
buf[1] = SPI_DUMMY_BYTE; |
|
751 |
spi->xfer(spi, buf, buf, sizeof(buf)); |
|
752 |
|
|
753 |
spinor_Delay(1); |
|
754 |
} while ((buf[1] & 0x01) == 0x01); |
|
755 |
} |
|
756 |
|
d973d6
|
757 |
/*+----------------------+ |
G |
758 |
*| Misc functions | |
|
759 |
*+----------------------+*/ |
8df7c2
|
760 |
|
G |
761 |
void print_buf(const char *prompt, uint8_t *buf, int size) |
|
762 |
{ |
|
763 |
int i; |
|
764 |
|
|
765 |
if( !buf ) |
|
766 |
{ |
|
767 |
return ; |
|
768 |
} |
|
769 |
|
|
770 |
if( prompt ) |
|
771 |
{ |
|
772 |
printf("%-32s ", prompt); |
|
773 |
} |
|
774 |
|
|
775 |
for(i=0; i<size; i++) |
|
776 |
{ |
|
777 |
printf("%02X ", buf[i]); |
|
778 |
} |
|
779 |
printf("\r\n"); |
|
780 |
|
|
781 |
return ; |
|
782 |
} |
|
783 |
|
75843d
|
784 |
void dump_buf(const char *prompt, char *buffer, size_t length) |
8df7c2
|
785 |
{ |
75843d
|
786 |
size_t i, j; |
8df7c2
|
787 |
|
75843d
|
788 |
if (prompt) |
8df7c2
|
789 |
{ |
75843d
|
790 |
printf("%s\n", prompt); |
8df7c2
|
791 |
} |
G |
792 |
|
75843d
|
793 |
for (i = 0; i < length; i += 16) |
8df7c2
|
794 |
{ |
75843d
|
795 |
printf("%08zx: ", i); |
8df7c2
|
796 |
|
75843d
|
797 |
for (j = 0; j < 16; j++) |
GW |
798 |
{ |
|
799 |
if (i + j < length) |
|
800 |
{ |
|
801 |
printf("%02x ", buffer[i + j]); |
|
802 |
} |
|
803 |
else |
|
804 |
{ |
|
805 |
printf(" "); |
|
806 |
} |
|
807 |
} |
8df7c2
|
808 |
|
75843d
|
809 |
printf(" "); |
8df7c2
|
810 |
|
75843d
|
811 |
for (j = 0; j < 16; j++) |
GW |
812 |
{ |
|
813 |
if (i + j < length) |
|
814 |
{ |
|
815 |
unsigned char c = buffer[i + j]; |
|
816 |
printf("%c", (c >= 32 && c <= 126) ? c : '.'); |
|
817 |
} |
|
818 |
else |
|
819 |
{ |
|
820 |
printf(" "); |
|
821 |
} |
|
822 |
} |
|
823 |
|
|
824 |
printf("\n"); |
8df7c2
|
825 |
} |
G |
826 |
} |
75843d
|
827 |
|