blob: e33c1068b359228fce7a769a2f744b160e03f810 [file] [log] [blame]
Ronald G. Minnichb1934902002-06-11 19:15:55 +00001/*
2 * m29f400bt.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. Minnichb1934902002-06-11 19:15:55 +000024 */
25
26#include "flash.h"
Ronald G. Minnichb1934902002-06-11 19:15:55 +000027
Uwe Hermann51582f22007-08-23 10:20:40 +000028void toggle_ready_m29f400bt(volatile uint8_t *dst)
29{
30 unsigned int i = 0;
31 uint8_t tmp1, tmp2;
32
33 tmp1 = *dst & 0x40;
34
35 while (i++ < 0xFFFFFF) {
36 tmp2 = *dst & 0x40;
37 if (tmp1 == tmp2) {
38 break;
39 }
40 tmp1 = tmp2;
41 }
42}
43
44void data_polling_m29f400bt(volatile uint8_t *dst, uint8_t data)
45{
46 unsigned int i = 0;
47 uint8_t tmp;
48
49 data &= 0x80;
50
51 while (i++ < 0xFFFFFF) {
52 tmp = *dst & 0x80;
53 if (tmp == data) {
54 break;
55 }
56 }
57}
58
59void protect_m29f400bt(volatile uint8_t *bios)
60{
61 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
62 *(volatile uint8_t *)(bios + 0x555) = 0x55;
63 *(volatile uint8_t *)(bios + 0xAAA) = 0xA0;
64
65 usleep(200);
66}
67
68void write_page_m29f400bt(volatile uint8_t *bios, uint8_t *src,
69 volatile uint8_t *dst, int page_size)
70{
71 int i;
72
73 for (i = 0; i < page_size; i++) {
74 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
75 *(volatile uint8_t *)(bios + 0x555) = 0x55;
76 *(volatile uint8_t *)(bios + 0xAAA) = 0xA0;
77
78 /* transfer data from source to destination */
79 *dst = *src;
80 //*(volatile char *) (bios) = 0xF0;
81 //usleep(5);
82 toggle_ready_m29f400bt(dst);
83 printf
84 ("Value in the flash at address %p = %#x, want %#x\n",
85 (uint8_t *) (dst - bios), *dst, *src);
86 dst++;
87 src++;
88 }
89}
90
Ollie Lho761bf1b2004-03-20 16:46:10 +000091int probe_m29f400bt(struct flashchip *flash)
Ronald G. Minnichb1934902002-06-11 19:15:55 +000092{
Stefan Reinauerce532972007-05-23 17:20:56 +000093 volatile uint8_t *bios = flash->virtual_memory;
Ollie Lho184a4042005-11-26 21:55:36 +000094 uint8_t id1, id2;
Ronald G. Minnichb1934902002-06-11 19:15:55 +000095
Uwe Hermanna7e05482007-05-09 10:17:44 +000096 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
97 *(volatile uint8_t *)(bios + 0x555) = 0x55;
98 *(volatile uint8_t *)(bios + 0xAAA) = 0x90;
Ronald G. Minnichb1934902002-06-11 19:15:55 +000099
100 myusec_delay(10);
101
Uwe Hermanna7e05482007-05-09 10:17:44 +0000102 id1 = *(volatile uint8_t *)bios;
103 id2 = *(volatile uint8_t *)(bios + 0x02);
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000104
Uwe Hermanna7e05482007-05-09 10:17:44 +0000105 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
106 *(volatile uint8_t *)(bios + 0x555) = 0x55;
107 *(volatile uint8_t *)(bios + 0xAAA) = 0xF0;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000108
109 myusec_delay(10);
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000110
Ollie Lho184a4042005-11-26 21:55:36 +0000111 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000112
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000113 if (id1 == flash->manufacture_id && id2 == flash->model_id)
114 return 1;
115
116 return 0;
117}
118
Ollie Lho761bf1b2004-03-20 16:46:10 +0000119int erase_m29f400bt(struct flashchip *flash)
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000120{
Stefan Reinauerce532972007-05-23 17:20:56 +0000121 volatile uint8_t *bios = flash->virtual_memory;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000122
Uwe Hermanna7e05482007-05-09 10:17:44 +0000123 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
124 *(volatile uint8_t *)(bios + 0x555) = 0x55;
125 *(volatile uint8_t *)(bios + 0xAAA) = 0x80;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000126
Uwe Hermanna7e05482007-05-09 10:17:44 +0000127 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
128 *(volatile uint8_t *)(bios + 0x555) = 0x55;
129 *(volatile uint8_t *)(bios + 0xAAA) = 0x10;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000130
131 myusec_delay(10);
132 toggle_ready_m29f400bt(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000133
Ollie Lho761bf1b2004-03-20 16:46:10 +0000134 return (0);
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000135}
136
Ollie Lho184a4042005-11-26 21:55:36 +0000137int block_erase_m29f400bt(volatile uint8_t *bios, volatile uint8_t *dst)
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000138{
139
Uwe Hermanna7e05482007-05-09 10:17:44 +0000140 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
141 *(volatile uint8_t *)(bios + 0x555) = 0x55;
142 *(volatile uint8_t *)(bios + 0xAAA) = 0x80;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000143
Uwe Hermanna7e05482007-05-09 10:17:44 +0000144 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
145 *(volatile uint8_t *)(bios + 0x555) = 0x55;
Ollie Lho184a4042005-11-26 21:55:36 +0000146 //*(volatile uint8_t *) (bios + 0xAAA) = 0x10;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000147 *dst = 0x30;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000148
149 myusec_delay(10);
150 toggle_ready_m29f400bt(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000151
Ollie Lho761bf1b2004-03-20 16:46:10 +0000152 return (0);
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000153}
154
Ollie Lho184a4042005-11-26 21:55:36 +0000155int write_m29f400bt(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000156{
157 int i;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000158 int total_size = flash->total_size * 1024;
159 int page_size = flash->page_size;
Stefan Reinauerce532972007-05-23 17:20:56 +0000160 volatile uint8_t *bios = flash->virtual_memory;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000161
162 //erase_m29f400bt (flash);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000163 printf("Programming Page:\n ");
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000164 /*********************************
165 *Pages for M29F400BT:
166 * 16 0x7c000 0x7ffff TOP
167 * 8 0x7a000 0x7bfff
168 * 8 0x78000 0x79fff
169 * 32 0x70000 0x77fff
170 * 64 0x60000 0x6ffff
171 * 64 0x50000 0x5ffff
172 * 64 0x40000 0x4ffff
173 *---------------------------------
174 * 64 0x30000 0x3ffff
175 * 64 0x20000 0x2ffff
176 * 64 0x10000 0x1ffff
177 * 64 0x00000 0x0ffff BOTTOM
178 *********************************/
Ollie Lho761bf1b2004-03-20 16:46:10 +0000179 printf("total_size/page_size = %d\n", total_size / page_size);
180 for (i = 0; i < (total_size / page_size) - 1; i++) {
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000181 printf("%04d at address: 0x%08x\n", i, i * page_size);
182 block_erase_m29f400bt(bios, bios + i * page_size);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000183 write_page_m29f400bt(bios, buf + i * page_size,
184 bios + i * page_size, page_size);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000185 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. Minnichb1934902002-06-11 19:15:55 +0000186 }
187
Ollie Lho761bf1b2004-03-20 16:46:10 +0000188 printf("%04d at address: 0x%08x\n", 7, 0x70000);
189 block_erase_m29f400bt(bios, bios + 0x70000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000190 write_page_m29f400bt(bios, buf + 0x70000, bios + 0x70000, 32 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000191
192 printf("%04d at address: 0x%08x\n", 8, 0x78000);
193 block_erase_m29f400bt(bios, bios + 0x78000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000194 write_page_m29f400bt(bios, buf + 0x78000, bios + 0x78000, 8 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000195
196 printf("%04d at address: 0x%08x\n", 9, 0x7a000);
197 block_erase_m29f400bt(bios, bios + 0x7a000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000198 write_page_m29f400bt(bios, buf + 0x7a000, bios + 0x7a000, 8 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000199
200 printf("%04d at address: 0x%08x\n", 10, 0x7c000);
201 block_erase_m29f400bt(bios, bios + 0x7c000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000202 write_page_m29f400bt(bios, buf + 0x7c000, bios + 0x7c000, 16 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000203
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000204 printf("\n");
205 //protect_m29f400bt (bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000206
Ollie Lho761bf1b2004-03-20 16:46:10 +0000207 return (0);
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000208}
209
Ollie Lho184a4042005-11-26 21:55:36 +0000210int write_linuxbios_m29f400bt(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000211{
Stefan Reinauerce532972007-05-23 17:20:56 +0000212 volatile uint8_t *bios = flash->virtual_memory;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000213
Ollie Lho761bf1b2004-03-20 16:46:10 +0000214 printf("Programming Page:\n ");
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000215 /*********************************
216 *Pages for M29F400BT:
217 * 16 0x7c000 0x7ffff TOP
218 * 8 0x7a000 0x7bfff
219 * 8 0x78000 0x79fff
220 * 32 0x70000 0x77fff
221 * 64 0x60000 0x6ffff
222 * 64 0x50000 0x5ffff
223 * 64 0x40000 0x4ffff
224 *---------------------------------
225 * 64 0x30000 0x3ffff
226 * 64 0x20000 0x2ffff
227 * 64 0x10000 0x1ffff
228 * 64 0x00000 0x0ffff BOTTOM
229 *********************************/
Ollie Lho761bf1b2004-03-20 16:46:10 +0000230 printf("%04d at address: 0x%08x\n", 7, 0x00000);
231 block_erase_m29f400bt(bios, bios + 0x00000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000232 write_page_m29f400bt(bios, buf + 0x00000, bios + 0x00000, 64 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000233
234 printf("%04d at address: 0x%08x\n", 7, 0x10000);
235 block_erase_m29f400bt(bios, bios + 0x10000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000236 write_page_m29f400bt(bios, buf + 0x10000, bios + 0x10000, 64 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000237
238 printf("%04d at address: 0x%08x\n", 7, 0x20000);
239 block_erase_m29f400bt(bios, bios + 0x20000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000240 write_page_m29f400bt(bios, buf + 0x20000, bios + 0x20000, 64 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000241
242 printf("%04d at address: 0x%08x\n", 7, 0x30000);
243 block_erase_m29f400bt(bios, bios + 0x30000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000244 write_page_m29f400bt(bios, buf + 0x30000, bios + 0x30000, 64 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000245
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000246 printf("\n");
247 //protect_m29f400bt (bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000248
Ollie Lho761bf1b2004-03-20 16:46:10 +0000249 return (0);
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000250}