blob: 65fc623825a2ed6987133182336228bf8d421f3e [file] [log] [blame]
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +00001/*
2 * sst39sf020.c: driver for SST28SF040C flash models.
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 * 4 MEgabit (512K x 8) SuperFlash EEPROM, SST28SF040 data sheet
24 *
25 * $Id$
26 */
27
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000028#include <stdio.h>
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +000029#include "flash.h"
30#include "jedec.h"
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000031#include "sst39sf020.h"
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +000032
33#define AUTO_PG_ERASE1 0x20
34#define AUTO_PG_ERASE2 0xD0
35#define AUTO_PGRM 0x10
36#define CHIP_ERASE 0x30
37#define RESET 0xFF
38#define READ_ID 0x90
39
40static __inline__ void protect_39sf020 (volatile char * bios)
41{
42 /* ask compiler not to optimize this */
43 volatile unsigned char tmp;
44
45 tmp = *(volatile unsigned char *) (bios + 0x1823);
46 tmp = *(volatile unsigned char *) (bios + 0x1820);
47 tmp = *(volatile unsigned char *) (bios + 0x1822);
48 tmp = *(volatile unsigned char *) (bios + 0x0418);
49 tmp = *(volatile unsigned char *) (bios + 0x041B);
50 tmp = *(volatile unsigned char *) (bios + 0x0419);
51 tmp = *(volatile unsigned char *) (bios + 0x040A);
52}
53
54static __inline__ void unprotect_39sf020 (volatile char * bios)
55{
56 /* ask compiler not to optimize this */
57 volatile unsigned char tmp;
58
59 tmp = *(volatile unsigned char *) (bios + 0x1823);
60 tmp = *(volatile unsigned char *) (bios + 0x1820);
61 tmp = *(volatile unsigned char *) (bios + 0x1822);
62 tmp = *(volatile unsigned char *) (bios + 0x0418);
63 tmp = *(volatile unsigned char *) (bios + 0x041B);
64 tmp = *(volatile unsigned char *) (bios + 0x0419);
65 tmp = *(volatile unsigned char *) (bios + 0x041A);
66}
67
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000068static __inline__ int erase_sector_39sf020 (volatile char * bios, unsigned long address)
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +000069{
70 *bios = AUTO_PG_ERASE1;
71 *(bios + address) = AUTO_PG_ERASE2;
72
73 /* wait for Toggle bit ready */
74 toggle_ready_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000075
76 return(0);
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +000077}
78
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000079static __inline__ int write_sector_39sf020(volatile char * bios,
Ronald G. Minnich213ee712002-04-10 16:01:38 +000080 unsigned char * src,
81 volatile unsigned char * dst,
82 unsigned int page_size)
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +000083{
84 int i;
85 volatile char *Temp;
86
87 for (i = 0; i < page_size; i++) {
Ollie Lho6041bcd2002-07-18 03:32:00 +000088 if (*dst != 0xff) {
89 printf("FATAL: dst %p not erased (val 0x%x\n", dst, *dst);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000090 return(-1);
Ollie Lho6041bcd2002-07-18 03:32:00 +000091 }
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +000092 /* transfer data from source to destination */
93 if (*src == 0xFF) {
94 dst++, src++;
95 /* If the data is 0xFF, don't program it */
96 continue;
97 }
Ollie Lho6041bcd2002-07-18 03:32:00 +000098 Temp = (bios + 0x5555);
99 *Temp = 0xAA;
100 Temp = bios + 0x2AAA;
101 *Temp = 0x55;
102 Temp = bios + 0x5555;
103 *Temp = 0xA0;
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000104 *dst = *src;
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000105 toggle_ready_jedec(bios);
Ollie Lho6041bcd2002-07-18 03:32:00 +0000106 if (*dst != *src)
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000107 printf("BAD! dst 0x%lx val 0x%x src 0x%x\n",
108 (unsigned long)dst, *dst, *src);
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000109 dst++, src++;
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000110 }
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000111
112 return(0);
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000113}
114
115int probe_39sf020 (struct flashchip * flash)
116{
117 volatile char * bios = flash->virt_addr;
118 unsigned char id1, id2;
119
120 *(volatile char *) (bios + 0x5555) = 0xAA;
121 myusec_delay(10);
122 *(volatile char *) (bios + 0x2AAA) = 0x55;
123 myusec_delay(10);
124 *(volatile char *) (bios + 0x5555) = 0x90;
125
126 myusec_delay(10);
127
128 id1 = *(volatile unsigned char *) bios;
129 id2 = *(volatile unsigned char *) (bios + 0x01);
130
131 *(volatile char *) (bios + 0x5555) = 0xAA;
132 *(volatile char *) (bios + 0x2AAA) = 0x55;
133 *(volatile char *) (bios + 0x5555) = 0xF0;
134
135 myusec_delay(10);
136
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000137 printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
138
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000139 if (id1 == flash->manufacture_id && id2 == flash->model_id)
140 return 1;
141
142 return 0;
143}
144
145int erase_39sf020 (struct flashchip * flash)
146{
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000147 volatile unsigned char * bios = flash->virt_addr;
Ollie Lho6041bcd2002-07-18 03:32:00 +0000148 volatile unsigned char *Temp;
149
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000150 /* Issue the Sector Erase command to 39SF020 */
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000151 Temp = bios + 0x5555; /* set up address to be C000:5555h */
152 *Temp = 0xAA; /* write data 0xAA to the address */
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000153 myusec_delay(10);
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000154 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
155 *Temp = 0x55; /* write data 0x55 to the address */
156 myusec_delay(10);
157 Temp = bios + 0x5555; /* set up address to be C000:5555h */
158 *Temp = 0x80; /* write data 0x80 to the address */
159 myusec_delay(10);
160 Temp = bios + 0x5555; /* set up address to be C000:5555h */
161 *Temp = 0xAA; /* write data 0xAA to the address */
162 myusec_delay(10);
163 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
164 *Temp = 0x55; /* write data 0x55 to the address */
165 myusec_delay(10);
166 Temp = bios + 0x5555; /* set up address to be C000:5555h */
167 *Temp = 0x10; /* write data 0x55 to the address */
168
Ronald G. Minniche5559572003-03-28 03:29:43 +0000169 myusec_delay(50000);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000170
171 return(0);
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000172}
173
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000174int write_39sf020 (struct flashchip * flash, unsigned char * buf)
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000175{
176 int i;
177 int total_size = flash->total_size * 1024, page_size = flash->page_size;
178 volatile char * bios = flash->virt_addr;
179
180// unprotect_39sf020 (bios);
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000181 erase_39sf020(flash);
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000182 printf ("Programming Page: ");
183 for (i = 0; i < total_size/page_size; i++) {
184 /* erase the page before programming */
185 //erase_sector_39sf020(bios, i * page_size);
186
187 /* write to the sector */
188 printf ("%04d at address: 0x%08x", i, i * page_size);
189 write_sector_39sf020(bios, buf + i * page_size, bios + i * page_size,
190 page_size);
191 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. Minniche5559572003-03-28 03:29:43 +0000192 fflush(stdout);
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000193 }
194 printf("\n");
195
196// protect_39sf020 (bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000197
198 return(0);
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000199}