2616611153ad58d2fe26ad2830621b36790fdd72
[coreboot.git] / util / flashrom / sharplhf00l04.c
1 /*
2  * lhf00l04.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: http://www.intel.com/design/chipsets/datashts/290658.htm
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include "flash.h"
28
29 void toggle_ready_lhf00l04(volatile uint8_t *dst)
30 {
31         unsigned int i = 0;
32         uint8_t tmp1, tmp2;
33
34         tmp1 = *dst & 0x40;
35
36         while (i++ < 0xFFFFFF) {
37                 tmp2 = *dst & 0x40;
38                 if (tmp1 == tmp2) {
39                         break;
40                 }
41                 tmp1 = tmp2;
42         }
43 }
44
45 void data_polling_lhf00l04(volatile uint8_t *dst, uint8_t data)
46 {
47         unsigned int i = 0;
48         uint8_t tmp;
49
50         data &= 0x80;
51
52         while (i++ < 0xFFFFFF) {
53                 tmp = *dst & 0x80;
54                 if (tmp == data) {
55                         break;
56                 }
57         }
58 }
59
60 void protect_lhf00l04(volatile uint8_t *bios)
61 {
62         *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
63         *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
64         *(volatile uint8_t *)(bios + 0x5555) = 0xA0;
65
66         usleep(200);
67 }
68
69 // I need that Berkeley bit-map printer
70 void print_lhf00l04_status(uint8_t status)
71 {
72         printf("%s", status & 0x80 ? "Ready:" : "Busy:");
73         printf("%s", status & 0x40 ? "BE SUSPEND:" : "BE RUN/FINISH:");
74         printf("%s", status & 0x20 ? "BE ERROR:" : "BE OK:");
75         printf("%s", status & 0x10 ? "PROG ERR:" : "PROG OK:");
76         printf("%s", status & 0x8 ? "VP ERR:" : "VPP OK:");
77         printf("%s", status & 0x4 ? "PROG SUSPEND:" : "PROG RUN/FINISH:");
78         printf("%s", status & 0x2 ? "WP|TBL#|WP#,ABORT:" : "UNLOCK:");
79 }
80
81 int probe_lhf00l04(struct flashchip *flash)
82 {
83         volatile uint8_t *bios = flash->virtual_memory;
84         uint8_t id1, id2;
85
86 #if 0
87         /* Enter ID mode */
88         *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
89         *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
90         *(volatile uint8_t *)(bios + 0x5555) = 0x90;
91 #endif
92
93         *bios = 0xff;
94         myusec_delay(10);
95         *bios = 0x90;
96         myusec_delay(10);
97
98         id1 = *(volatile uint8_t *)bios;
99         id2 = *(volatile uint8_t *)(bios + 0x01);
100
101         /* Leave ID mode */
102         *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
103         *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
104         *(volatile uint8_t *)(bios + 0x5555) = 0xF0;
105
106         myusec_delay(10);
107
108         printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
109
110         if (id1 != flash->manufacture_id || id2 != flash->model_id)
111                 return 0;
112
113         map_flash_registers(flash);
114
115         return 1;
116 }
117
118 uint8_t wait_lhf00l04(volatile uint8_t *bios)
119 {
120
121         uint8_t status;
122         uint8_t id1, id2;
123
124         *bios = 0x70;
125         if ((*bios & 0x80) == 0) {      // it's busy
126                 while ((*bios & 0x80) == 0) ;
127         }
128
129         status = *bios;
130
131         // put another command to get out of status register mode
132
133         *bios = 0x90;
134         myusec_delay(10);
135
136         id1 = *(volatile uint8_t *)bios;
137         id2 = *(volatile uint8_t *)(bios + 0x01);
138
139         // this is needed to jam it out of "read id" mode
140         *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
141         *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
142         *(volatile uint8_t *)(bios + 0x5555) = 0xF0;
143         return status;
144
145 }
146 int erase_lhf00l04_block(struct flashchip *flash, int offset)
147 {
148         volatile uint8_t *bios = flash->virtual_memory + offset;
149         volatile uint8_t *wrprotect = flash->virtual_registers + offset + 2;
150         uint8_t status;
151
152         // clear status register
153         *bios = 0x50;
154         printf("Erase at %p\n", bios);
155         status = wait_lhf00l04(flash->virtual_memory);
156         print_lhf00l04_status(status);
157         // clear write protect
158         printf("write protect is at %p\n", (wrprotect));
159         printf("write protect is 0x%x\n", *(wrprotect));
160         *(wrprotect) = 0;
161         printf("write protect is 0x%x\n", *(wrprotect));
162
163         // now start it
164         *(volatile uint8_t *)(bios) = 0x20;
165         *(volatile uint8_t *)(bios) = 0xd0;
166         myusec_delay(10);
167         // now let's see what the register is
168         status = wait_lhf00l04(flash->virtual_memory);
169         print_lhf00l04_status(status);
170         printf("DONE BLOCK 0x%x\n", offset);
171         return (0);
172 }
173 int erase_lhf00l04(struct flashchip *flash)
174 {
175         int i;
176         unsigned int total_size = flash->total_size * 1024;
177
178         printf("total_size is %d; flash->page_size is %d\n",
179                total_size, flash->page_size);
180         for (i = 0; i < total_size; i += flash->page_size)
181                 erase_lhf00l04_block(flash, i);
182         printf("DONE ERASE\n");
183         return (0);
184 }
185
186 void write_page_lhf00l04(volatile uint8_t *bios, uint8_t *src,
187                          volatile uint8_t *dst, int page_size)
188 {
189         int i;
190
191         for (i = 0; i < page_size; i++) {
192                 /* transfer data from source to destination */
193                 *dst = 0x40;
194                 *dst++ = *src++;
195                 wait_lhf00l04(bios);
196         }
197
198 }
199
200 int write_lhf00l04(struct flashchip *flash, uint8_t *buf)
201 {
202         int i;
203         int total_size = flash->total_size * 1024;
204         int page_size = flash->page_size;
205         volatile uint8_t *bios = flash->virtual_memory;
206
207         erase_lhf00l04(flash);
208         if (*bios != 0xff) {
209                 printf("ERASE FAILED\n");
210                 return -1;
211         }
212         printf("Programming Page: ");
213         for (i = 0; i < total_size / page_size; i++) {
214                 printf("%04d at address: 0x%08x", i, i * page_size);
215                 write_page_lhf00l04(bios, buf + i * page_size,
216                                     bios + i * page_size, page_size);
217                 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");
218         }
219         printf("\n");
220         protect_lhf00l04(bios);
221         return (0);
222 }