blob: c3712dec6c607b263a5e0405f68f51f1dedc798c [file] [log] [blame]
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00001/*
2 * jedec.c: driver for programming JEDEC standard flash parts
3 *
4 *
5 * Copyright 2000 Silicon Integrated System Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 *
22 * Reference:
23 *
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000024 */
25
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000026#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +000027#include <stdint.h>
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000028#include "flash.h"
29#include "jedec.h"
Ollie Lho184a4042005-11-26 21:55:36 +000030#include "debug.h"
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000031
Ollie Lho761bf1b2004-03-20 16:46:10 +000032int probe_jedec(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000033{
Ollie Lho184a4042005-11-26 21:55:36 +000034 volatile uint8_t *bios = flash->virt_addr;
35 uint8_t id1, id2;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000036
Ollie Lho761bf1b2004-03-20 16:46:10 +000037 /* Issue JEDEC Product ID Entry command */
Ollie Lho184a4042005-11-26 21:55:36 +000038 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ollie Lho761bf1b2004-03-20 16:46:10 +000039 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000040 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho761bf1b2004-03-20 16:46:10 +000041 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000042 *(volatile uint8_t *) (bios + 0x5555) = 0x90;
Ollie Lho761bf1b2004-03-20 16:46:10 +000043 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000044
Ollie Lho761bf1b2004-03-20 16:46:10 +000045 /* Read product ID */
Ollie Lho184a4042005-11-26 21:55:36 +000046 id1 = *(volatile uint8_t *) bios;
47 id2 = *(volatile uint8_t *) (bios + 0x01);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000048
Ollie Lho761bf1b2004-03-20 16:46:10 +000049 /* Issue JEDEC Product ID Exit command */
Ollie Lho184a4042005-11-26 21:55:36 +000050 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ollie Lho761bf1b2004-03-20 16:46:10 +000051 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000052 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho761bf1b2004-03-20 16:46:10 +000053 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000054 *(volatile uint8_t *) (bios + 0x5555) = 0xF0;
Ollie Lho761bf1b2004-03-20 16:46:10 +000055 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000056
Ollie Lho184a4042005-11-26 21:55:36 +000057 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
Ollie Lho761bf1b2004-03-20 16:46:10 +000058 if (id1 == flash->manufacture_id && id2 == flash->model_id)
59 return 1;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000060
Ollie Lho761bf1b2004-03-20 16:46:10 +000061 return 0;
Ollie Lho73eca802004-03-19 22:10:07 +000062}
63
Ollie Lho184a4042005-11-26 21:55:36 +000064int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
Ollie Lho73eca802004-03-19 22:10:07 +000065{
Ollie Lho761bf1b2004-03-20 16:46:10 +000066 /* Issue the Sector Erase command */
Ollie Lho184a4042005-11-26 21:55:36 +000067 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +000068 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000069 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +000070 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000071 *(volatile uint8_t *) (bios + 0x5555) = 0x80;
Ollie Lho73eca802004-03-19 22:10:07 +000072 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +000073
Ollie Lho184a4042005-11-26 21:55:36 +000074 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +000075 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000076 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +000077 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000078 *(volatile uint8_t *) (bios + page) = 0x30;
Ollie Lho761bf1b2004-03-20 16:46:10 +000079 myusec_delay(10);
80
Ollie Lho73eca802004-03-19 22:10:07 +000081 /* wait for Toggle bit ready */
82 toggle_ready_jedec(bios);
83
Ollie Lho761bf1b2004-03-20 16:46:10 +000084 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000085}
Ollie Lho98bea8a2004-12-07 03:15:51 +000086
Ollie Lho184a4042005-11-26 21:55:36 +000087int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000088{
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000089 /* Issue the Sector Erase command */
Ollie Lho184a4042005-11-26 21:55:36 +000090 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000091 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000092 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000093 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000094 *(volatile uint8_t *) (bios + 0x5555) = 0x80;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000095 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +000096
Ollie Lho184a4042005-11-26 21:55:36 +000097 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000098 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +000099 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000100 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +0000101 *(volatile uint8_t *) (bios + block) = 0x50;
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +0000102 myusec_delay(10);
103
104 /* wait for Toggle bit ready */
105 toggle_ready_jedec(bios);
106
107 return (0);
108}
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000109
Ollie Lho761bf1b2004-03-20 16:46:10 +0000110int erase_chip_jedec(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000111{
Ollie Lho184a4042005-11-26 21:55:36 +0000112 volatile uint8_t *bios = flash->virt_addr;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000113
Ollie Lho761bf1b2004-03-20 16:46:10 +0000114 /* Issue the JEDEC Chip Erase command */
Ollie Lho184a4042005-11-26 21:55:36 +0000115 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000116 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +0000117 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +0000118 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +0000119 *(volatile uint8_t *) (bios + 0x5555) = 0x80;
Ollie Lho73eca802004-03-19 22:10:07 +0000120 myusec_delay(10);
Ollie Lhoefa28582004-12-08 20:10:01 +0000121
Ollie Lho184a4042005-11-26 21:55:36 +0000122 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
Ollie Lho73eca802004-03-19 22:10:07 +0000123 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +0000124 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
Ollie Lho73eca802004-03-19 22:10:07 +0000125 myusec_delay(10);
Ollie Lho184a4042005-11-26 21:55:36 +0000126 *(volatile uint8_t *) (bios + 0x5555) = 0x10;
Ollie Lho73eca802004-03-19 22:10:07 +0000127 myusec_delay(10);
128
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000129 toggle_ready_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000130
Ollie Lho761bf1b2004-03-20 16:46:10 +0000131 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000132}
133
Ollie Lho184a4042005-11-26 21:55:36 +0000134int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
135 volatile uint8_t *dst, int page_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000136{
137 int i;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000138
Ollie Lho761bf1b2004-03-20 16:46:10 +0000139 /* Issue JEDEC Data Unprotect comand */
Ollie Lho184a4042005-11-26 21:55:36 +0000140 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
141 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
142 *(volatile uint8_t *) (bios + 0x5555) = 0xA0;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000143
Ollie Lho98bea8a2004-12-07 03:15:51 +0000144 /* transfer data from source to destination */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000145 for (i = 0; i < page_size; i++) {
Ollie Lho98bea8a2004-12-07 03:15:51 +0000146 /* If the data is 0xFF, don't program it */
147 if (*src == 0xFF)
148 continue;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000149 *dst++ = *src++;
150 }
151
Ollie Lho761bf1b2004-03-20 16:46:10 +0000152 toggle_ready_jedec(dst - 1);
Ollie Lho98bea8a2004-12-07 03:15:51 +0000153
154 return 0;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000155}
156
Ollie Lho184a4042005-11-26 21:55:36 +0000157int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src,
158 volatile uint8_t *dst)
Ollie Lho070647d2004-03-22 22:19:17 +0000159{
Ollie Lho1b8b6602004-12-08 02:10:33 +0000160 int tried = 0;
161
Ollie Lho98bea8a2004-12-07 03:15:51 +0000162 /* If the data is 0xFF, don't program it */
Ollie Lho070647d2004-03-22 22:19:17 +0000163 if (*src == 0xFF) {
Ollie Lho070647d2004-03-22 22:19:17 +0000164 return 0;
165 }
Ollie Lho98bea8a2004-12-07 03:15:51 +0000166
Ollie Lho1b8b6602004-12-08 02:10:33 +0000167retry:
Ollie Lho070647d2004-03-22 22:19:17 +0000168 /* Issue JEDEC Byte Program command */
Ollie Lho184a4042005-11-26 21:55:36 +0000169 *(volatile uint8_t *) (bios + 0x5555) = 0xAA;
170 *(volatile uint8_t *) (bios + 0x2AAA) = 0x55;
171 *(volatile uint8_t *) (bios + 0x5555) = 0xA0;
Ollie Lho98bea8a2004-12-07 03:15:51 +0000172
173 /* transfer data from source to destination */
Ollie Lho070647d2004-03-22 22:19:17 +0000174 *dst = *src;
175 toggle_ready_jedec(bios);
Ollie Lho8b8897a2004-03-27 00:18:15 +0000176
Ollie Lho1b8b6602004-12-08 02:10:33 +0000177 if (*dst != *src && tried++ < 0x10) {
178 goto retry;
179 }
180
Ollie Lho070647d2004-03-22 22:19:17 +0000181 return 0;
182}
183
Ollie Lho184a4042005-11-26 21:55:36 +0000184int write_sector_jedec(volatile uint8_t *bios, uint8_t *src,
185 volatile uint8_t *dst, unsigned int page_size)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000186{
187 int i;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000188
189 for (i = 0; i < page_size; i++) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000190 write_byte_program_jedec(bios, src, dst);
191 dst++, src++;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000192 }
193
194 return (0);
195}
196
Ollie Lho184a4042005-11-26 21:55:36 +0000197int write_jedec(struct flashchip *flash, uint8_t *buf)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000198{
199 int i;
Ollie Lho070647d2004-03-22 22:19:17 +0000200 int total_size = flash->total_size * 1024;
201 int page_size = flash->page_size;
Ollie Lho184a4042005-11-26 21:55:36 +0000202 volatile uint8_t *bios = flash->virt_addr;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000203
204 erase_chip_jedec(flash);
Ollie Lho184a4042005-11-26 21:55:36 +0000205 if (*bios != (uint8_t) 0xff) {
Ronald G. Minnich56439422002-09-06 16:58:14 +0000206 printf("ERASE FAILED\n");
207 return -1;
208 }
Ollie Lho761bf1b2004-03-20 16:46:10 +0000209 printf("Programming Page: ");
210 for (i = 0; i < total_size / page_size; i++) {
211 printf("%04d at address: 0x%08x", i, i * page_size);
Ollie Lho8b8897a2004-03-27 00:18:15 +0000212 write_page_write_jedec(bios, buf + i * page_size,
213 bios + i * page_size, page_size);
Ollie Lho070647d2004-03-22 22:19:17 +0000214 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");
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000215 }
216 printf("\n");
Ollie Lho761bf1b2004-03-20 16:46:10 +0000217 protect_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000218
Ollie Lho761bf1b2004-03-20 16:46:10 +0000219 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000220}