19a233b81ca29f56e078e4a723c5c9b2e8edb664
[coreboot.git] / util / flash_and_burn / jedec.c
1 /*
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  *
24  * $Id$
25  */
26
27 #include <stdio.h>
28 #include "flash.h"
29 #include "jedec.h"
30
31 int probe_jedec(struct flashchip *flash)
32 {
33         volatile unsigned char *bios = flash->virt_addr;
34         unsigned char id1, id2;
35
36         /* Issue JEDEC Product ID Entry command */
37         *(volatile char *) (bios + 0x5555) = 0xAA;
38         myusec_delay(10);
39         *(volatile char *) (bios + 0x2AAA) = 0x55;
40         myusec_delay(10);
41         *(volatile char *) (bios + 0x5555) = 0x90;
42         myusec_delay(10);
43
44         /* Read product ID */
45         id1 = *(volatile unsigned char *) bios;
46         id2 = *(volatile unsigned char *) (bios + 0x01);
47
48         /* Issue JEDEC Product ID Exit command */
49         *(volatile char *) (bios + 0x5555) = 0xAA;
50         myusec_delay(10);
51         *(volatile char *) (bios + 0x2AAA) = 0x55;
52         myusec_delay(10);
53         *(volatile char *) (bios + 0x5555) = 0xF0;
54         myusec_delay(10);
55
56         printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
57         if (id1 == flash->manufacture_id && id2 == flash->model_id)
58                 return 1;
59
60         return 0;
61 }
62
63 int erase_sector_jedec(volatile unsigned char *bios, unsigned int page)
64 {
65         volatile unsigned char *Temp;
66
67         /*  Issue the Sector Erase command   */
68         Temp = bios + 0x5555;   /* set up address to be BASE:5555h      */
69         *Temp = 0xAA;           /* write data 0xAA to the address       */
70         myusec_delay(10);
71         Temp = bios + 0x2AAA;   /* set up address to be BASE:2AAAh      */
72         *Temp = 0x55;           /* write data 0x55 to the address       */
73         myusec_delay(10);
74         Temp = bios + 0x5555;   /* set up address to be BASE:5555h      */
75         *Temp = 0x80;           /* write data 0x80 to the address       */
76         myusec_delay(10);
77         Temp = bios + 0x5555;   /* set up address to be BASE:5555h      */
78         *Temp = 0xAA;           /* write data 0xAA to the address       */
79         myusec_delay(10);
80         Temp = bios + 0x2AAA;   /* set up address to be BASE:2AAAh      */
81         *Temp = 0x55;           /* write data 0x55 to the address       */
82         myusec_delay(10);
83         Temp = bios + page;     /* set up address to be the current sector */
84         *Temp = 0x30;           /* write data 0x30 to the address       */
85         myusec_delay(10);
86
87         /* wait for Toggle bit ready         */
88         toggle_ready_jedec(bios);
89
90         return (0);
91 }
92 int erase_block_jedec(volatile unsigned char *bios, unsigned int block)
93 {
94         volatile unsigned char *Temp;
95
96         /*  Issue the Sector Erase command   */
97         Temp = bios + 0x5555;   /* set up address to be BASE:5555h      */
98         *Temp = 0xAA;           /* write data 0xAA to the address       */
99         myusec_delay(10);
100         Temp = bios + 0x2AAA;   /* set up address to be BASE:2AAAh      */
101         *Temp = 0x55;           /* write data 0x55 to the address       */
102         myusec_delay(10);
103         Temp = bios + 0x5555;   /* set up address to be BASE:5555h      */
104         *Temp = 0x80;           /* write data 0x80 to the address       */
105         myusec_delay(10);
106         Temp = bios + 0x5555;   /* set up address to be BASE:5555h      */
107         *Temp = 0xAA;           /* write data 0xAA to the address       */
108         myusec_delay(10);
109         Temp = bios + 0x2AAA;   /* set up address to be BASE:2AAAh      */
110         *Temp = 0x55;           /* write data 0x55 to the address       */
111         myusec_delay(10);
112         Temp = bios + block;    /* set up address to be the current sector */
113         *Temp = 0x50;           /* write data 0x30 to the address       */
114         myusec_delay(10);
115
116         /* wait for Toggle bit ready         */
117         toggle_ready_jedec(bios);
118
119         return (0);
120 }
121
122 int erase_chip_jedec(struct flashchip *flash)
123 {
124         volatile unsigned char *bios = flash->virt_addr;
125         volatile unsigned char *Temp;
126
127         /*  Issue the JEDEC Chip Erase command   */
128         Temp = bios + 0x5555;   /* set up address to be BASE:5555h      */
129         *Temp = 0xAA;           /* write data 0xAA to the address       */
130         myusec_delay(10);
131         Temp = bios + 0x2AAA;   /* set up address to be BASE:2AAAh      */
132         *Temp = 0x55;           /* write data 0x55 to the address       */
133         myusec_delay(10);
134         Temp = bios + 0x5555;   /* set up address to be BASE:5555h      */
135         *Temp = 0x80;           /* write data 0x80 to the address       */
136         myusec_delay(10);
137         Temp = bios + 0x5555;   /* set up address to be BASE:5555h      */
138         *Temp = 0xAA;           /* write data 0xAA to the address       */
139         myusec_delay(10);
140         Temp = bios + 0x2AAA;   /* set up address to be BASE:2AAAh      */
141         *Temp = 0x55;           /* write data 0x55 to the address       */
142         myusec_delay(10);
143         Temp = bios + 0x5555;   /* set up address to be BASEy:5555h     */
144         *Temp = 0x10;           /* write data 0x10 to the address       */
145         myusec_delay(10);
146
147         toggle_ready_jedec(bios);
148
149         return (0);
150 }
151
152 void write_page_write_jedec(volatile unsigned char *bios, unsigned char *src,
153                             volatile unsigned char *dst, int page_size)
154 {
155         int i;
156
157         /* Issue JEDEC Data Unprotect comand */
158         *(volatile char *) (bios + 0x5555) = 0xAA;
159         *(volatile char *) (bios + 0x2AAA) = 0x55;
160         *(volatile char *) (bios + 0x5555) = 0xA0;
161
162         for (i = 0; i < page_size; i++) {
163                 /* transfer data from source to destination */
164                 *dst++ = *src++;
165         }
166
167         usleep(100);
168         toggle_ready_jedec(dst - 1);
169 }
170
171 int write_byte_program_jedec(volatile unsigned char *bios, unsigned char *src,
172                              volatile unsigned char *dst)
173 {
174         volatile unsigned char *Temp;
175
176         /* transfer data from source to destination */
177         if (*src == 0xFF) {
178                 /* If the data is 0xFF, don't program it */
179                 return 0;
180         }
181         /* Issue JEDEC Byte Program command */
182         Temp = bios + 0x5555;
183         *Temp = 0xAA;
184         Temp = bios + 0x2AAA;
185         *Temp = 0x55;
186         Temp = bios + 0x5555;
187         *Temp = 0xA0;
188         *dst = *src;
189         toggle_ready_jedec(bios);
190
191         return 0;
192 }
193
194 int write_sector_jedec(volatile unsigned char *bios, unsigned char *src,
195                        volatile unsigned char *dst, unsigned int page_size)
196 {
197         int i;
198
199         for (i = 0; i < page_size; i++) {
200                 write_byte_program_jedec(bios, src, dst);
201                 dst++, src++;
202         }
203
204         return (0);
205 }
206
207 int write_jedec(struct flashchip *flash, unsigned char *buf)
208 {
209         int i;
210         int total_size = flash->total_size * 1024;
211         int page_size = flash->page_size;
212         volatile unsigned char *bios = flash->virt_addr;
213
214         erase_chip_jedec(flash);
215         if (*bios != (unsigned char) 0xff) {
216                 printf("ERASE FAILED\n");
217                 return -1;
218         }
219         printf("Programming Page: ");
220         for (i = 0; i < total_size / page_size; i++) {
221                 printf("%04d at address: 0x%08x", i, i * page_size);
222                 write_page_write_jedec(bios, buf + i * page_size,
223                                        bios + i * page_size, page_size);
224                 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");
225         }
226         printf("\n");
227         protect_jedec(bios);
228
229         return (0);
230 }