blob: 3c310a775401d5686251716d6ffce39bfbe19e35 [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
28#include "flash.h"
29#include "jedec.h"
30
31#define AUTO_PG_ERASE1 0x20
32#define AUTO_PG_ERASE2 0xD0
33#define AUTO_PGRM 0x10
34#define CHIP_ERASE 0x30
35#define RESET 0xFF
36#define READ_ID 0x90
37
38static __inline__ void protect_39sf020 (volatile char * bios)
39{
40 /* ask compiler not to optimize this */
41 volatile unsigned char tmp;
42
43 tmp = *(volatile unsigned char *) (bios + 0x1823);
44 tmp = *(volatile unsigned char *) (bios + 0x1820);
45 tmp = *(volatile unsigned char *) (bios + 0x1822);
46 tmp = *(volatile unsigned char *) (bios + 0x0418);
47 tmp = *(volatile unsigned char *) (bios + 0x041B);
48 tmp = *(volatile unsigned char *) (bios + 0x0419);
49 tmp = *(volatile unsigned char *) (bios + 0x040A);
50}
51
52static __inline__ void unprotect_39sf020 (volatile char * bios)
53{
54 /* ask compiler not to optimize this */
55 volatile unsigned char tmp;
56
57 tmp = *(volatile unsigned char *) (bios + 0x1823);
58 tmp = *(volatile unsigned char *) (bios + 0x1820);
59 tmp = *(volatile unsigned char *) (bios + 0x1822);
60 tmp = *(volatile unsigned char *) (bios + 0x0418);
61 tmp = *(volatile unsigned char *) (bios + 0x041B);
62 tmp = *(volatile unsigned char *) (bios + 0x0419);
63 tmp = *(volatile unsigned char *) (bios + 0x041A);
64}
65
66static __inline__ erase_sector_39sf020 (volatile char * bios, unsigned long address)
67{
68 *bios = AUTO_PG_ERASE1;
69 *(bios + address) = AUTO_PG_ERASE2;
70
71 /* wait for Toggle bit ready */
72 toggle_ready_jedec(bios);
73}
74
75static __inline__ write_sector_39sf020(volatile char * bios, unsigned char * src,
76 volatile unsigned char * dst, unsigned int page_size)
77{
78 int i;
79 volatile char *Temp;
80
81 for (i = 0; i < page_size; i++) {
82 /* transfer data from source to destination */
83 if (*src == 0xFF) {
84 dst++, src++;
85 /* If the data is 0xFF, don't program it */
86 continue;
87 }
88 Temp = (bios + 0x5555);
89 *Temp = 0xAA;
90 Temp = bios + 0x2AAA;
91 *Temp = 0x55;
92 Temp = bios + 0x5555;
93 *Temp = 0xA0;
94 *dst++ = *src++;
95 toggle_ready_jedec(bios);
96 }
97}
98
99int probe_39sf020 (struct flashchip * flash)
100{
101 volatile char * bios = flash->virt_addr;
102 unsigned char id1, id2;
103
104 *(volatile char *) (bios + 0x5555) = 0xAA;
105 myusec_delay(10);
106 *(volatile char *) (bios + 0x2AAA) = 0x55;
107 myusec_delay(10);
108 *(volatile char *) (bios + 0x5555) = 0x90;
109
110 myusec_delay(10);
111
112 id1 = *(volatile unsigned char *) bios;
113 id2 = *(volatile unsigned char *) (bios + 0x01);
114
115 *(volatile char *) (bios + 0x5555) = 0xAA;
116 *(volatile char *) (bios + 0x2AAA) = 0x55;
117 *(volatile char *) (bios + 0x5555) = 0xF0;
118
119 myusec_delay(10);
120
121 printf(__FUNCTION__ "id1 %d, id2 %d\n", id1, id2);
122 if (id1 == flash->manufacture_id && id2 == flash->model_id)
123 return 1;
124
125 return 0;
126}
127
128int erase_39sf020 (struct flashchip * flash)
129{
130 volatile char * bios = flash->virt_addr;
131
132 unprotect_39sf020 (bios);
133 *bios = CHIP_ERASE;
134 *bios = CHIP_ERASE;
135 protect_39sf020 (bios);
136
137 myusec_delay(10);
138 toggle_ready_jedec(bios);
139}
140
141int write_39sf020 (struct flashchip * flash, char * buf)
142{
143 int i;
144 int total_size = flash->total_size * 1024, page_size = flash->page_size;
145 volatile char * bios = flash->virt_addr;
146
147// unprotect_39sf020 (bios);
148
149 printf ("Programming Page: ");
150 for (i = 0; i < total_size/page_size; i++) {
151 /* erase the page before programming */
152 //erase_sector_39sf020(bios, i * page_size);
153
154 /* write to the sector */
155 printf ("%04d at address: 0x%08x", i, i * page_size);
156 write_sector_39sf020(bios, buf + i * page_size, bios + i * page_size,
157 page_size);
158 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");
159 }
160 printf("\n");
161
162// protect_39sf020 (bios);
163}