blob: 809f33cb3af2ed84a399b6c875137ba169a427a2 [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"
27#include "m29f400bt.h"
Ollie Lho184a4042005-11-26 21:55:36 +000028#include "debug.h"
Ronald G. Minnichb1934902002-06-11 19:15:55 +000029
Uwe Hermann51582f22007-08-23 10:20:40 +000030void toggle_ready_m29f400bt(volatile uint8_t *dst)
31{
32 unsigned int i = 0;
33 uint8_t tmp1, tmp2;
34
35 tmp1 = *dst & 0x40;
36
37 while (i++ < 0xFFFFFF) {
38 tmp2 = *dst & 0x40;
39 if (tmp1 == tmp2) {
40 break;
41 }
42 tmp1 = tmp2;
43 }
44}
45
46void data_polling_m29f400bt(volatile uint8_t *dst, uint8_t data)
47{
48 unsigned int i = 0;
49 uint8_t tmp;
50
51 data &= 0x80;
52
53 while (i++ < 0xFFFFFF) {
54 tmp = *dst & 0x80;
55 if (tmp == data) {
56 break;
57 }
58 }
59}
60
61void protect_m29f400bt(volatile uint8_t *bios)
62{
63 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
64 *(volatile uint8_t *)(bios + 0x555) = 0x55;
65 *(volatile uint8_t *)(bios + 0xAAA) = 0xA0;
66
67 usleep(200);
68}
69
70void write_page_m29f400bt(volatile uint8_t *bios, uint8_t *src,
71 volatile uint8_t *dst, int page_size)
72{
73 int i;
74
75 for (i = 0; i < page_size; i++) {
76 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
77 *(volatile uint8_t *)(bios + 0x555) = 0x55;
78 *(volatile uint8_t *)(bios + 0xAAA) = 0xA0;
79
80 /* transfer data from source to destination */
81 *dst = *src;
82 //*(volatile char *) (bios) = 0xF0;
83 //usleep(5);
84 toggle_ready_m29f400bt(dst);
85 printf
86 ("Value in the flash at address %p = %#x, want %#x\n",
87 (uint8_t *) (dst - bios), *dst, *src);
88 dst++;
89 src++;
90 }
91}
92
Ollie Lho761bf1b2004-03-20 16:46:10 +000093int probe_m29f400bt(struct flashchip *flash)
Ronald G. Minnichb1934902002-06-11 19:15:55 +000094{
Stefan Reinauerce532972007-05-23 17:20:56 +000095 volatile uint8_t *bios = flash->virtual_memory;
Ollie Lho184a4042005-11-26 21:55:36 +000096 uint8_t id1, id2;
Ronald G. Minnichb1934902002-06-11 19:15:55 +000097
Uwe Hermanna7e05482007-05-09 10:17:44 +000098 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
99 *(volatile uint8_t *)(bios + 0x555) = 0x55;
100 *(volatile uint8_t *)(bios + 0xAAA) = 0x90;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000101
102 myusec_delay(10);
103
Uwe Hermanna7e05482007-05-09 10:17:44 +0000104 id1 = *(volatile uint8_t *)bios;
105 id2 = *(volatile uint8_t *)(bios + 0x02);
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000106
Uwe Hermanna7e05482007-05-09 10:17:44 +0000107 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
108 *(volatile uint8_t *)(bios + 0x555) = 0x55;
109 *(volatile uint8_t *)(bios + 0xAAA) = 0xF0;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000110
111 myusec_delay(10);
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000112
Ollie Lho184a4042005-11-26 21:55:36 +0000113 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000114
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000115 if (id1 == flash->manufacture_id && id2 == flash->model_id)
116 return 1;
117
118 return 0;
119}
120
Ollie Lho761bf1b2004-03-20 16:46:10 +0000121int erase_m29f400bt(struct flashchip *flash)
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000122{
Stefan Reinauerce532972007-05-23 17:20:56 +0000123 volatile uint8_t *bios = flash->virtual_memory;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000124
Uwe Hermanna7e05482007-05-09 10:17:44 +0000125 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
126 *(volatile uint8_t *)(bios + 0x555) = 0x55;
127 *(volatile uint8_t *)(bios + 0xAAA) = 0x80;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000128
Uwe Hermanna7e05482007-05-09 10:17:44 +0000129 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
130 *(volatile uint8_t *)(bios + 0x555) = 0x55;
131 *(volatile uint8_t *)(bios + 0xAAA) = 0x10;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000132
133 myusec_delay(10);
134 toggle_ready_m29f400bt(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000135
Ollie Lho761bf1b2004-03-20 16:46:10 +0000136 return (0);
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000137}
138
Ollie Lho184a4042005-11-26 21:55:36 +0000139int block_erase_m29f400bt(volatile uint8_t *bios, volatile uint8_t *dst)
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000140{
141
Uwe Hermanna7e05482007-05-09 10:17:44 +0000142 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
143 *(volatile uint8_t *)(bios + 0x555) = 0x55;
144 *(volatile uint8_t *)(bios + 0xAAA) = 0x80;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000145
Uwe Hermanna7e05482007-05-09 10:17:44 +0000146 *(volatile uint8_t *)(bios + 0xAAA) = 0xAA;
147 *(volatile uint8_t *)(bios + 0x555) = 0x55;
Ollie Lho184a4042005-11-26 21:55:36 +0000148 //*(volatile uint8_t *) (bios + 0xAAA) = 0x10;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000149 *dst = 0x30;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000150
151 myusec_delay(10);
152 toggle_ready_m29f400bt(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000153
Ollie Lho761bf1b2004-03-20 16:46:10 +0000154 return (0);
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000155}
156
Ollie Lho184a4042005-11-26 21:55:36 +0000157int write_m29f400bt(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000158{
159 int i;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000160 int total_size = flash->total_size * 1024;
161 int page_size = flash->page_size;
Stefan Reinauerce532972007-05-23 17:20:56 +0000162 volatile uint8_t *bios = flash->virtual_memory;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000163
164 //erase_m29f400bt (flash);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000165 printf("Programming Page:\n ");
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000166 /*********************************
167 *Pages for M29F400BT:
168 * 16 0x7c000 0x7ffff TOP
169 * 8 0x7a000 0x7bfff
170 * 8 0x78000 0x79fff
171 * 32 0x70000 0x77fff
172 * 64 0x60000 0x6ffff
173 * 64 0x50000 0x5ffff
174 * 64 0x40000 0x4ffff
175 *---------------------------------
176 * 64 0x30000 0x3ffff
177 * 64 0x20000 0x2ffff
178 * 64 0x10000 0x1ffff
179 * 64 0x00000 0x0ffff BOTTOM
180 *********************************/
Ollie Lho761bf1b2004-03-20 16:46:10 +0000181 printf("total_size/page_size = %d\n", total_size / page_size);
182 for (i = 0; i < (total_size / page_size) - 1; i++) {
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000183 printf("%04d at address: 0x%08x\n", i, i * page_size);
184 block_erase_m29f400bt(bios, bios + i * page_size);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000185 write_page_m29f400bt(bios, buf + i * page_size,
186 bios + i * page_size, page_size);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000187 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 +0000188 }
189
Ollie Lho761bf1b2004-03-20 16:46:10 +0000190 printf("%04d at address: 0x%08x\n", 7, 0x70000);
191 block_erase_m29f400bt(bios, bios + 0x70000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000192 write_page_m29f400bt(bios, buf + 0x70000, bios + 0x70000, 32 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000193
194 printf("%04d at address: 0x%08x\n", 8, 0x78000);
195 block_erase_m29f400bt(bios, bios + 0x78000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000196 write_page_m29f400bt(bios, buf + 0x78000, bios + 0x78000, 8 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000197
198 printf("%04d at address: 0x%08x\n", 9, 0x7a000);
199 block_erase_m29f400bt(bios, bios + 0x7a000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000200 write_page_m29f400bt(bios, buf + 0x7a000, bios + 0x7a000, 8 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000201
202 printf("%04d at address: 0x%08x\n", 10, 0x7c000);
203 block_erase_m29f400bt(bios, bios + 0x7c000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000204 write_page_m29f400bt(bios, buf + 0x7c000, bios + 0x7c000, 16 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000205
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000206 printf("\n");
207 //protect_m29f400bt (bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000208
Ollie Lho761bf1b2004-03-20 16:46:10 +0000209 return (0);
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000210}
211
Ollie Lho184a4042005-11-26 21:55:36 +0000212int write_linuxbios_m29f400bt(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000213{
Stefan Reinauerce532972007-05-23 17:20:56 +0000214 volatile uint8_t *bios = flash->virtual_memory;
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000215
Ollie Lho761bf1b2004-03-20 16:46:10 +0000216 printf("Programming Page:\n ");
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000217 /*********************************
218 *Pages for M29F400BT:
219 * 16 0x7c000 0x7ffff TOP
220 * 8 0x7a000 0x7bfff
221 * 8 0x78000 0x79fff
222 * 32 0x70000 0x77fff
223 * 64 0x60000 0x6ffff
224 * 64 0x50000 0x5ffff
225 * 64 0x40000 0x4ffff
226 *---------------------------------
227 * 64 0x30000 0x3ffff
228 * 64 0x20000 0x2ffff
229 * 64 0x10000 0x1ffff
230 * 64 0x00000 0x0ffff BOTTOM
231 *********************************/
Ollie Lho761bf1b2004-03-20 16:46:10 +0000232 printf("%04d at address: 0x%08x\n", 7, 0x00000);
233 block_erase_m29f400bt(bios, bios + 0x00000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000234 write_page_m29f400bt(bios, buf + 0x00000, bios + 0x00000, 64 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000235
236 printf("%04d at address: 0x%08x\n", 7, 0x10000);
237 block_erase_m29f400bt(bios, bios + 0x10000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000238 write_page_m29f400bt(bios, buf + 0x10000, bios + 0x10000, 64 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000239
240 printf("%04d at address: 0x%08x\n", 7, 0x20000);
241 block_erase_m29f400bt(bios, bios + 0x20000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000242 write_page_m29f400bt(bios, buf + 0x20000, bios + 0x20000, 64 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000243
244 printf("%04d at address: 0x%08x\n", 7, 0x30000);
245 block_erase_m29f400bt(bios, bios + 0x30000);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000246 write_page_m29f400bt(bios, buf + 0x30000, bios + 0x30000, 64 * 1024);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000247
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000248 printf("\n");
249 //protect_m29f400bt (bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000250
Ollie Lho761bf1b2004-03-20 16:46:10 +0000251 return (0);
Ronald G. Minnichb1934902002-06-11 19:15:55 +0000252}