comparison loadtools/flashops.c @ 0:e7502631a0f9

initial import from freecalypso-sw rev 1033:5ab737ac3ad7
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Jun 2016 00:13:35 +0000
parents
children 545e1718f5fb
comparison
equal deleted inserted replaced
-1:000000000000 0:e7502631a0f9
1 /*
2 * This module implements those flash operations which are dependent
3 * on the AMD vs. Intel command set style.
4 */
5
6 #include <sys/types.h>
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <time.h>
11 #include "flash.h"
12
13 /* common stub functions */
14
15 static
16 noop()
17 {
18 return(0);
19 }
20
21 static
22 invalid()
23 {
24 fprintf(stderr,
25 "This operation is not applicable to the selected flash type\n");
26 return(-1);
27 }
28
29 /* AMD flash functions */
30
31 amd_reset_cmd(bi)
32 struct flash_bank_info *bi;
33 {
34 if (do_w16(bi->base_addr + 0xAAA, 0xF0)) {
35 fprintf(stderr,
36 "unexpected response to w16 when resetting flash to read mode!\n");
37 return(-1);
38 }
39 return(0);
40 }
41
42 amd_sector_erase(bi, sp)
43 struct flash_bank_info *bi;
44 struct sector_info *sp;
45 {
46 int stat;
47 uint16_t flstat;
48 time_t start_time, curtime;
49
50 stat = do_w16(bi->base_addr + sp->start + 0xAAA, 0xAA);
51 if (stat) {
52 bad_w16: fprintf(stderr,
53 "unexpected response to w16 in erase cmd sequence - aborting\n");
54 return(-1);
55 }
56 stat = do_w16(bi->base_addr + sp->start + 0x554, 0x55);
57 if (stat)
58 goto bad_w16;
59 stat = do_w16(bi->base_addr + sp->start + 0xAAA, 0x80);
60 if (stat)
61 goto bad_w16;
62 stat = do_w16(bi->base_addr + sp->start + 0xAAA, 0xAA);
63 if (stat)
64 goto bad_w16;
65 stat = do_w16(bi->base_addr + sp->start + 0x554, 0x55);
66 if (stat)
67 goto bad_w16;
68 stat = do_w16(bi->base_addr + sp->start + 0xAAA, 0x30);
69 if (stat)
70 goto bad_w16;
71 start_time = time(0);
72 for (;;) {
73 stat = do_r16(bi->base_addr + sp->start, &flstat);
74 if (stat)
75 return(stat); /* error msg already printed */
76 if (flstat == 0xFFFF)
77 return(0);
78 curtime = time(0);
79 if (curtime >= start_time + 20) {
80 fprintf(stderr, "erase timeout, aborting\n");
81 return(-1);
82 }
83 }
84 }
85
86 struct flash_cmdset flash_cmdset_amd = {
87 .cmdset_name = "AMD",
88 .reset_cmd = amd_reset_cmd,
89 .status_cmd = invalid,
90 .unlock_sector = invalid,
91 .erase_sector = amd_sector_erase,
92 .prep_for_program = noop,
93 .loadagent_setbase_cmd = "AMFB",
94 .loadagent_program_cmd = "AMFW",
95 .needs_unlock = 0,
96 };
97
98 /* Intel flash functions */
99
100 intel_reset_cmd(bi)
101 struct flash_bank_info *bi;
102 {
103 if (do_w16(bi->base_addr, 0xFF)) {
104 fprintf(stderr,
105 "unexpected response to w16 when resetting flash to read mode!\n");
106 return(-1);
107 }
108 return(0);
109 }
110
111 intel_status_cmd(bi)
112 struct flash_bank_info *bi;
113 {
114 int stat;
115 uint16_t sr;
116
117 /* issue Read SR command */
118 stat = do_w16(bi->base_addr, 0x70);
119 if (stat) {
120 fprintf(stderr,
121 "unexpected response to w16 for Read SR command\n");
122 return(-1);
123 }
124 stat = do_r16(bi->base_addr, &sr);
125 if (stat)
126 return(stat); /* error msg already printed */
127 sr &= 0xFF;
128 printf("Status Register: %02X\n", sr);
129 return(0);
130 }
131
132 intel_sector_unlock(bi, sp)
133 struct flash_bank_info *bi;
134 struct sector_info *sp;
135 {
136 int stat;
137
138 stat = do_w16(bi->base_addr + sp->start, 0x60);
139 if (stat) {
140 bad_w16: fprintf(stderr,
141 "unexpected response to w16 in block unlock cmd sequence - aborting\n");
142 return(-1);
143 }
144 stat = do_w16(bi->base_addr + sp->start, 0xD0);
145 if (stat)
146 goto bad_w16;
147 return(0);
148 }
149
150 intel_sector_erase(bi, sp)
151 struct flash_bank_info *bi;
152 struct sector_info *sp;
153 {
154 int stat;
155 uint16_t flstat;
156 time_t start_time, curtime;
157
158 stat = intel_sector_unlock(bi, sp);
159 if (stat)
160 return(stat); /* error msg already printed */
161 /* clear SR */
162 stat = do_w16(bi->base_addr + sp->start, 0x50);
163 if (stat) {
164 bad_w16: fprintf(stderr,
165 "unexpected response to w16 in erase cmd sequence - aborting\n");
166 return(-1);
167 }
168 /* send the actual block erase command */
169 stat = do_w16(bi->base_addr + sp->start, 0x20);
170 if (stat)
171 goto bad_w16;
172 stat = do_w16(bi->base_addr + sp->start, 0xD0);
173 if (stat)
174 goto bad_w16;
175 /* wait for completion */
176 start_time = time(0);
177 for (;;) {
178 stat = do_r16(bi->base_addr + sp->start, &flstat);
179 if (stat)
180 return(stat); /* error msg already printed */
181 if (flstat & 0x80)
182 break;
183 curtime = time(0);
184 if (curtime >= start_time + 20) {
185 fprintf(stderr, "erase timeout, aborting\n");
186 return(-1);
187 }
188 }
189 if (flstat & 0x20) {
190 fprintf(stderr, "block erase failed!\n");
191 return(-1);
192 } else
193 return(0);
194 }
195
196 intel_clear_sr(bi)
197 struct flash_bank_info *bi;
198 {
199 printf("Clearing Intel flash SR\n");
200 if (do_w16(bi->base_addr, 0x50)) {
201 fprintf(stderr,
202 "unexpected response to w16 for Clear SR command\n");
203 return(-1);
204 }
205 return(0);
206 }
207
208 struct flash_cmdset flash_cmdset_intel = {
209 .cmdset_name = "Intel",
210 .reset_cmd = intel_reset_cmd,
211 .status_cmd = intel_status_cmd,
212 .unlock_sector = intel_sector_unlock,
213 .erase_sector = intel_sector_erase,
214 .prep_for_program = intel_clear_sr,
215 .loadagent_setbase_cmd = "INFB",
216 .loadagent_program_cmd = "INFW",
217 .needs_unlock = 1,
218 };