2003-10-25 19:01:29 +02:00
|
|
|
/*
|
|
|
|
* jedec.c: driver for programming JEDEC standard flash parts
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright 2000 Silicon Integrated System Corporation
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Reference:
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2005-11-26 22:55:36 +01:00
|
|
|
#include <stdint.h>
|
2003-10-25 19:01:29 +02:00
|
|
|
#include "flash.h"
|
|
|
|
#include "jedec.h"
|
2005-11-26 22:55:36 +01:00
|
|
|
#include "debug.h"
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
int probe_jedec(struct flashchip *flash)
|
2003-10-25 19:01:29 +02:00
|
|
|
{
|
2005-11-26 22:55:36 +01:00
|
|
|
volatile uint8_t *bios = flash->virt_addr;
|
|
|
|
uint8_t id1, id2;
|
2004-03-20 17:46:10 +01:00
|
|
|
|
|
|
|
/* Issue JEDEC Product ID Entry command */
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
|
2004-03-20 17:46:10 +01:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
|
2004-03-20 17:46:10 +01:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0x90;
|
2004-03-20 17:46:10 +01:00
|
|
|
myusec_delay(10);
|
|
|
|
|
|
|
|
/* Read product ID */
|
2005-11-26 22:55:36 +01:00
|
|
|
id1 = *(volatile uint8_t *) bios;
|
|
|
|
id2 = *(volatile uint8_t *) (bios + 0x01);
|
2004-03-20 17:46:10 +01:00
|
|
|
|
|
|
|
/* Issue JEDEC Product ID Exit command */
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
|
2004-03-20 17:46:10 +01:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
|
2004-03-20 17:46:10 +01:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0xF0;
|
2004-03-20 17:46:10 +01:00
|
|
|
myusec_delay(10);
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
|
2004-03-20 17:46:10 +01:00
|
|
|
if (id1 == flash->manufacture_id && id2 == flash->model_id)
|
|
|
|
return 1;
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
return 0;
|
2004-03-19 23:10:07 +01:00
|
|
|
}
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
|
2004-03-19 23:10:07 +01:00
|
|
|
{
|
2004-03-20 17:46:10 +01:00
|
|
|
/* Issue the Sector Erase command */
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
|
2004-03-20 17:46:10 +01:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
|
2003-10-25 19:01:29 +02:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0x80;
|
2004-03-19 23:10:07 +01:00
|
|
|
myusec_delay(10);
|
2004-12-08 21:10:01 +01:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
|
2004-03-19 23:10:07 +01:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
|
2004-03-19 23:10:07 +01:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + page) = 0x30;
|
2004-03-19 23:10:07 +01:00
|
|
|
myusec_delay(10);
|
2004-09-30 18:37:01 +02:00
|
|
|
|
|
|
|
/* wait for Toggle bit ready */
|
|
|
|
toggle_ready_jedec(bios);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2004-12-07 04:15:51 +01:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
|
2004-09-30 18:37:01 +02:00
|
|
|
{
|
|
|
|
/* Issue the Sector Erase command */
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
|
2004-09-30 18:37:01 +02:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
|
2004-09-30 18:37:01 +02:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0x80;
|
2004-09-30 18:37:01 +02:00
|
|
|
myusec_delay(10);
|
2004-12-08 21:10:01 +01:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
|
2004-09-30 18:37:01 +02:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
|
2004-09-30 18:37:01 +02:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + block) = 0x50;
|
2004-09-30 18:37:01 +02:00
|
|
|
myusec_delay(10);
|
2004-03-20 17:46:10 +01:00
|
|
|
|
2004-03-19 23:10:07 +01:00
|
|
|
/* wait for Toggle bit ready */
|
|
|
|
toggle_ready_jedec(bios);
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
return (0);
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
int erase_chip_jedec(struct flashchip *flash)
|
2003-10-25 19:01:29 +02:00
|
|
|
{
|
2005-11-26 22:55:36 +01:00
|
|
|
volatile uint8_t *bios = flash->virt_addr;
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
/* Issue the JEDEC Chip Erase command */
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
|
2004-03-19 23:10:07 +01:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
|
2003-10-25 19:01:29 +02:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0x80;
|
2004-03-19 23:10:07 +01:00
|
|
|
myusec_delay(10);
|
2004-12-08 21:10:01 +01:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
|
2004-03-19 23:10:07 +01:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
|
2004-03-19 23:10:07 +01:00
|
|
|
myusec_delay(10);
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0x10;
|
2004-03-19 23:10:07 +01:00
|
|
|
myusec_delay(10);
|
|
|
|
|
2003-10-25 19:01:29 +02:00
|
|
|
toggle_ready_jedec(bios);
|
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
|
|
|
|
volatile uint8_t *dst, int page_size)
|
2004-03-20 17:46:10 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Issue JEDEC Data Unprotect comand */
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
|
|
|
|
*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
|
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0xA0;
|
2004-03-20 17:46:10 +01:00
|
|
|
|
2004-12-07 04:15:51 +01:00
|
|
|
/* transfer data from source to destination */
|
2004-03-20 17:46:10 +01:00
|
|
|
for (i = 0; i < page_size; i++) {
|
2004-12-07 04:15:51 +01:00
|
|
|
/* If the data is 0xFF, don't program it */
|
|
|
|
if (*src == 0xFF)
|
|
|
|
continue;
|
2004-03-20 17:46:10 +01:00
|
|
|
*dst++ = *src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
toggle_ready_jedec(dst - 1);
|
2004-12-07 04:15:51 +01:00
|
|
|
|
|
|
|
return 0;
|
2004-03-20 17:46:10 +01:00
|
|
|
}
|
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
|
|
|
|
volatile uint8_t *dst)
|
2004-03-22 23:19:17 +01:00
|
|
|
{
|
2004-12-08 03:10:33 +01:00
|
|
|
int tried = 0;
|
|
|
|
|
2004-12-07 04:15:51 +01:00
|
|
|
/* If the data is 0xFF, don't program it */
|
2004-03-22 23:19:17 +01:00
|
|
|
if (*src == 0xFF) {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-12-07 04:15:51 +01:00
|
|
|
|
2004-12-08 03:10:33 +01:00
|
|
|
retry:
|
2004-03-22 23:19:17 +01:00
|
|
|
/* Issue JEDEC Byte Program command */
|
2005-11-26 22:55:36 +01:00
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0xAA;
|
|
|
|
*(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
|
|
|
|
*(volatile uint8_t *) (bios + 0x5555) = 0xA0;
|
2004-12-07 04:15:51 +01:00
|
|
|
|
|
|
|
/* transfer data from source to destination */
|
2004-03-22 23:19:17 +01:00
|
|
|
*dst = *src;
|
|
|
|
toggle_ready_jedec(bios);
|
2004-03-27 01:18:15 +01:00
|
|
|
|
2004-12-08 03:10:33 +01:00
|
|
|
if (*dst != *src && tried++ < 0x10) {
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
2004-03-22 23:19:17 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
int write_sector_jedec(volatile uint8_t *bios, uint8_t *src,
|
|
|
|
volatile uint8_t *dst, unsigned int page_size)
|
2004-03-20 17:46:10 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < page_size; i++) {
|
2004-03-27 01:18:15 +01:00
|
|
|
write_byte_program_jedec(bios, src, dst);
|
|
|
|
dst++, src++;
|
2004-03-20 17:46:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
int write_jedec(struct flashchip *flash, uint8_t *buf)
|
2003-10-25 19:01:29 +02:00
|
|
|
{
|
|
|
|
int i;
|
2004-03-22 23:19:17 +01:00
|
|
|
int total_size = flash->total_size * 1024;
|
|
|
|
int page_size = flash->page_size;
|
2005-11-26 22:55:36 +01:00
|
|
|
volatile uint8_t *bios = flash->virt_addr;
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
erase_chip_jedec(flash);
|
2005-11-26 22:55:36 +01:00
|
|
|
if (*bios != (uint8_t) 0xff) {
|
2003-10-25 19:01:29 +02:00
|
|
|
printf("ERASE FAILED\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2004-03-20 17:46:10 +01:00
|
|
|
printf("Programming Page: ");
|
|
|
|
for (i = 0; i < total_size / page_size; i++) {
|
|
|
|
printf("%04d at address: 0x%08x", i, i * page_size);
|
2004-03-27 01:18:15 +01:00
|
|
|
write_page_write_jedec(bios, buf + i * page_size,
|
|
|
|
bios + i * page_size, page_size);
|
2004-03-22 23:19:17 +01:00
|
|
|
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
|
|
|
printf("\n");
|
2004-03-20 17:46:10 +01:00
|
|
|
protect_jedec(bios);
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
return (0);
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|