root / hw / pflash_cfi02.c @ 86cc1ce0
History | View | Annotate | Download (17.9 kB)
1 | 29133e9a | bellard | /*
|
---|---|---|---|
2 | 29133e9a | bellard | * CFI parallel flash with AMD command set emulation
|
3 | 29133e9a | bellard | *
|
4 | 29133e9a | bellard | * Copyright (c) 2005 Jocelyn Mayer
|
5 | 29133e9a | bellard | *
|
6 | 29133e9a | bellard | * This library is free software; you can redistribute it and/or
|
7 | 29133e9a | bellard | * modify it under the terms of the GNU Lesser General Public
|
8 | 29133e9a | bellard | * License as published by the Free Software Foundation; either
|
9 | 29133e9a | bellard | * version 2 of the License, or (at your option) any later version.
|
10 | 29133e9a | bellard | *
|
11 | 29133e9a | bellard | * This library is distributed in the hope that it will be useful,
|
12 | 29133e9a | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 29133e9a | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 29133e9a | bellard | * Lesser General Public License for more details.
|
15 | 29133e9a | bellard | *
|
16 | 29133e9a | bellard | * You should have received a copy of the GNU Lesser General Public
|
17 | 29133e9a | bellard | * License along with this library; if not, write to the Free Software
|
18 | 29133e9a | bellard | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 | 29133e9a | bellard | */
|
20 | 29133e9a | bellard | |
21 | 29133e9a | bellard | /*
|
22 | 29133e9a | bellard | * For now, this code can emulate flashes of 1, 2 or 4 bytes width.
|
23 | 29133e9a | bellard | * Supported commands/modes are:
|
24 | 29133e9a | bellard | * - flash read
|
25 | 29133e9a | bellard | * - flash write
|
26 | 29133e9a | bellard | * - flash ID read
|
27 | 29133e9a | bellard | * - sector erase
|
28 | 29133e9a | bellard | * - chip erase
|
29 | 29133e9a | bellard | * - unlock bypass command
|
30 | 29133e9a | bellard | * - CFI queries
|
31 | 29133e9a | bellard | *
|
32 | 29133e9a | bellard | * It does not support flash interleaving.
|
33 | 29133e9a | bellard | * It does not implement boot blocs with reduced size
|
34 | 29133e9a | bellard | * It does not implement software data protection as found in many real chips
|
35 | 29133e9a | bellard | * It does not implement erase suspend/resume commands
|
36 | 29133e9a | bellard | * It does not implement multiple sectors erase
|
37 | 29133e9a | bellard | */
|
38 | 29133e9a | bellard | |
39 | 29133e9a | bellard | #include "vl.h" |
40 | 29133e9a | bellard | |
41 | 29133e9a | bellard | //#define PFLASH_DEBUG
|
42 | 29133e9a | bellard | #ifdef PFLASH_DEBUG
|
43 | 29133e9a | bellard | #define DPRINTF(fmt, args...) \
|
44 | 29133e9a | bellard | do { \
|
45 | 29133e9a | bellard | printf("PFLASH: " fmt , ##args); \ |
46 | 29133e9a | bellard | } while (0) |
47 | 29133e9a | bellard | #else
|
48 | 29133e9a | bellard | #define DPRINTF(fmt, args...) do { } while (0) |
49 | 29133e9a | bellard | #endif
|
50 | 29133e9a | bellard | |
51 | 29133e9a | bellard | struct pflash_t {
|
52 | 29133e9a | bellard | BlockDriverState *bs; |
53 | 29133e9a | bellard | target_ulong base; |
54 | 29133e9a | bellard | target_ulong sector_len; |
55 | 29133e9a | bellard | target_ulong total_len; |
56 | 29133e9a | bellard | int width;
|
57 | 29133e9a | bellard | int wcycle; /* if 0, the flash is read normally */ |
58 | 29133e9a | bellard | int bypass;
|
59 | 29133e9a | bellard | int ro;
|
60 | 29133e9a | bellard | uint8_t cmd; |
61 | 29133e9a | bellard | uint8_t status; |
62 | 29133e9a | bellard | uint16_t ident[4];
|
63 | 29133e9a | bellard | uint8_t cfi_len; |
64 | 29133e9a | bellard | uint8_t cfi_table[0x52];
|
65 | 29133e9a | bellard | QEMUTimer *timer; |
66 | 29133e9a | bellard | ram_addr_t off; |
67 | 29133e9a | bellard | int fl_mem;
|
68 | 29133e9a | bellard | void *storage;
|
69 | 29133e9a | bellard | }; |
70 | 29133e9a | bellard | |
71 | 29133e9a | bellard | static void pflash_timer (void *opaque) |
72 | 29133e9a | bellard | { |
73 | 29133e9a | bellard | pflash_t *pfl = opaque; |
74 | 29133e9a | bellard | |
75 | 29133e9a | bellard | DPRINTF("%s: command %02x done\n", __func__, pfl->cmd);
|
76 | 29133e9a | bellard | /* Reset flash */
|
77 | 29133e9a | bellard | pfl->status ^= 0x80;
|
78 | 29133e9a | bellard | if (pfl->bypass) {
|
79 | 29133e9a | bellard | pfl->wcycle = 2;
|
80 | 29133e9a | bellard | } else {
|
81 | 29133e9a | bellard | cpu_register_physical_memory(pfl->base, pfl->total_len, |
82 | 29133e9a | bellard | pfl->off | IO_MEM_ROMD | pfl->fl_mem); |
83 | 29133e9a | bellard | pfl->wcycle = 0;
|
84 | 29133e9a | bellard | } |
85 | 29133e9a | bellard | pfl->cmd = 0;
|
86 | 29133e9a | bellard | } |
87 | 29133e9a | bellard | |
88 | 29133e9a | bellard | static uint32_t pflash_read (pflash_t *pfl, target_ulong offset, int width) |
89 | 29133e9a | bellard | { |
90 | 29133e9a | bellard | target_ulong boff; |
91 | 29133e9a | bellard | uint32_t ret; |
92 | 29133e9a | bellard | uint8_t *p; |
93 | 29133e9a | bellard | |
94 | 29133e9a | bellard | DPRINTF("%s: offset %08x\n", __func__, offset);
|
95 | 29133e9a | bellard | ret = -1;
|
96 | 29133e9a | bellard | offset -= pfl->base; |
97 | 29133e9a | bellard | boff = offset & 0xFF;
|
98 | 29133e9a | bellard | if (pfl->width == 2) |
99 | 29133e9a | bellard | boff = boff >> 1;
|
100 | 29133e9a | bellard | else if (pfl->width == 4) |
101 | 29133e9a | bellard | boff = boff >> 2;
|
102 | 29133e9a | bellard | switch (pfl->cmd) {
|
103 | 29133e9a | bellard | default:
|
104 | 29133e9a | bellard | /* This should never happen : reset state & treat it as a read*/
|
105 | 29133e9a | bellard | DPRINTF("%s: unknown command state: %x\n", __func__, pfl->cmd);
|
106 | 29133e9a | bellard | pfl->wcycle = 0;
|
107 | 29133e9a | bellard | pfl->cmd = 0;
|
108 | 29133e9a | bellard | case 0x80: |
109 | 29133e9a | bellard | /* We accept reads during second unlock sequence... */
|
110 | 29133e9a | bellard | case 0x00: |
111 | 29133e9a | bellard | flash_read:
|
112 | 29133e9a | bellard | /* Flash area read */
|
113 | 29133e9a | bellard | p = pfl->storage; |
114 | 29133e9a | bellard | switch (width) {
|
115 | 29133e9a | bellard | case 1: |
116 | 29133e9a | bellard | ret = p[offset]; |
117 | 29133e9a | bellard | // DPRINTF("%s: data offset %08x %02x\n", __func__, offset, ret);
|
118 | 29133e9a | bellard | break;
|
119 | 29133e9a | bellard | case 2: |
120 | 29133e9a | bellard | #if defined(TARGET_WORDS_BIGENDIAN)
|
121 | 29133e9a | bellard | ret = p[offset] << 8;
|
122 | 29133e9a | bellard | ret |= p[offset + 1];
|
123 | 29133e9a | bellard | #else
|
124 | 29133e9a | bellard | ret = p[offset]; |
125 | 29133e9a | bellard | ret |= p[offset + 1] << 8; |
126 | 29133e9a | bellard | #endif
|
127 | 29133e9a | bellard | // DPRINTF("%s: data offset %08x %04x\n", __func__, offset, ret);
|
128 | 29133e9a | bellard | break;
|
129 | 29133e9a | bellard | case 4: |
130 | 29133e9a | bellard | #if defined(TARGET_WORDS_BIGENDIAN)
|
131 | 29133e9a | bellard | ret = p[offset] << 24;
|
132 | 29133e9a | bellard | ret |= p[offset + 1] << 16; |
133 | 29133e9a | bellard | ret |= p[offset + 2] << 8; |
134 | 29133e9a | bellard | ret |= p[offset + 3];
|
135 | 29133e9a | bellard | #else
|
136 | 29133e9a | bellard | ret = p[offset]; |
137 | 29133e9a | bellard | ret |= p[offset + 1] << 8; |
138 | 29133e9a | bellard | ret |= p[offset + 2] << 16; |
139 | 29133e9a | bellard | ret |= p[offset + 3] << 24; |
140 | 29133e9a | bellard | #endif
|
141 | 29133e9a | bellard | // DPRINTF("%s: data offset %08x %08x\n", __func__, offset, ret);
|
142 | 29133e9a | bellard | break;
|
143 | 29133e9a | bellard | } |
144 | 29133e9a | bellard | break;
|
145 | 29133e9a | bellard | case 0x90: |
146 | 29133e9a | bellard | /* flash ID read */
|
147 | 29133e9a | bellard | switch (boff) {
|
148 | 29133e9a | bellard | case 0x00: |
149 | 29133e9a | bellard | case 0x01: |
150 | 29133e9a | bellard | ret = pfl->ident[boff & 0x01];
|
151 | 29133e9a | bellard | break;
|
152 | 29133e9a | bellard | case 0x02: |
153 | 29133e9a | bellard | ret = 0x00; /* Pretend all sectors are unprotected */ |
154 | 29133e9a | bellard | break;
|
155 | 29133e9a | bellard | case 0x0E: |
156 | 29133e9a | bellard | case 0x0F: |
157 | 29133e9a | bellard | if (pfl->ident[2 + (boff & 0x01)] == (uint8_t)-1) |
158 | 29133e9a | bellard | goto flash_read;
|
159 | 29133e9a | bellard | ret = pfl->ident[2 + (boff & 0x01)]; |
160 | 29133e9a | bellard | break;
|
161 | 29133e9a | bellard | default:
|
162 | 29133e9a | bellard | goto flash_read;
|
163 | 29133e9a | bellard | } |
164 | 29133e9a | bellard | DPRINTF("%s: ID %d %x\n", __func__, boff, ret);
|
165 | 29133e9a | bellard | break;
|
166 | 29133e9a | bellard | case 0xA0: |
167 | 29133e9a | bellard | case 0x10: |
168 | 29133e9a | bellard | case 0x30: |
169 | 29133e9a | bellard | /* Status register read */
|
170 | 29133e9a | bellard | ret = pfl->status; |
171 | 29133e9a | bellard | DPRINTF("%s: status %x\n", __func__, ret);
|
172 | 29133e9a | bellard | /* Toggle bit 6 */
|
173 | 29133e9a | bellard | pfl->status ^= 0x40;
|
174 | 29133e9a | bellard | break;
|
175 | 29133e9a | bellard | case 0x98: |
176 | 29133e9a | bellard | /* CFI query mode */
|
177 | 29133e9a | bellard | if (boff > pfl->cfi_len)
|
178 | 29133e9a | bellard | ret = 0;
|
179 | 29133e9a | bellard | else
|
180 | 29133e9a | bellard | ret = pfl->cfi_table[boff]; |
181 | 29133e9a | bellard | break;
|
182 | 29133e9a | bellard | } |
183 | 29133e9a | bellard | |
184 | 29133e9a | bellard | return ret;
|
185 | 29133e9a | bellard | } |
186 | 29133e9a | bellard | |
187 | 29133e9a | bellard | /* update flash content on disk */
|
188 | 29133e9a | bellard | static void pflash_update(pflash_t *pfl, int offset, |
189 | 29133e9a | bellard | int size)
|
190 | 29133e9a | bellard | { |
191 | 29133e9a | bellard | int offset_end;
|
192 | 29133e9a | bellard | if (pfl->bs) {
|
193 | 29133e9a | bellard | offset_end = offset + size; |
194 | 29133e9a | bellard | /* round to sectors */
|
195 | 29133e9a | bellard | offset = offset >> 9;
|
196 | 29133e9a | bellard | offset_end = (offset_end + 511) >> 9; |
197 | 29133e9a | bellard | bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
|
198 | 29133e9a | bellard | offset_end - offset); |
199 | 29133e9a | bellard | } |
200 | 29133e9a | bellard | } |
201 | 29133e9a | bellard | |
202 | 29133e9a | bellard | static void pflash_write (pflash_t *pfl, target_ulong offset, uint32_t value, |
203 | 29133e9a | bellard | int width)
|
204 | 29133e9a | bellard | { |
205 | 29133e9a | bellard | target_ulong boff; |
206 | 29133e9a | bellard | uint8_t *p; |
207 | 29133e9a | bellard | uint8_t cmd; |
208 | 29133e9a | bellard | |
209 | 29133e9a | bellard | /* WARNING: when the memory area is in ROMD mode, the offset is a
|
210 | 29133e9a | bellard | ram offset, not a physical address */
|
211 | 29133e9a | bellard | if (pfl->wcycle == 0) |
212 | 99ba31b4 | bellard | offset -= (target_ulong)(long)pfl->storage;
|
213 | 29133e9a | bellard | else
|
214 | 29133e9a | bellard | offset -= pfl->base; |
215 | 29133e9a | bellard | |
216 | 29133e9a | bellard | cmd = value; |
217 | 29133e9a | bellard | DPRINTF("%s: offset %08x %08x %d\n", __func__, offset, value, width);
|
218 | 29133e9a | bellard | if (pfl->cmd != 0xA0 && cmd == 0xF0) { |
219 | 29133e9a | bellard | DPRINTF("%s: flash reset asked (%02x %02x)\n",
|
220 | 29133e9a | bellard | __func__, pfl->cmd, cmd); |
221 | 29133e9a | bellard | goto reset_flash;
|
222 | 29133e9a | bellard | } |
223 | 29133e9a | bellard | /* Set the device in I/O access mode */
|
224 | 29133e9a | bellard | cpu_register_physical_memory(pfl->base, pfl->total_len, pfl->fl_mem); |
225 | 29133e9a | bellard | boff = offset & (pfl->sector_len - 1);
|
226 | 29133e9a | bellard | if (pfl->width == 2) |
227 | 29133e9a | bellard | boff = boff >> 1;
|
228 | 29133e9a | bellard | else if (pfl->width == 4) |
229 | 29133e9a | bellard | boff = boff >> 2;
|
230 | 29133e9a | bellard | switch (pfl->wcycle) {
|
231 | 29133e9a | bellard | case 0: |
232 | 29133e9a | bellard | /* We're in read mode */
|
233 | 29133e9a | bellard | check_unlock0:
|
234 | 29133e9a | bellard | if (boff == 0x55 && cmd == 0x98) { |
235 | 29133e9a | bellard | enter_CFI_mode:
|
236 | 29133e9a | bellard | /* Enter CFI query mode */
|
237 | 29133e9a | bellard | pfl->wcycle = 7;
|
238 | 29133e9a | bellard | pfl->cmd = 0x98;
|
239 | 29133e9a | bellard | return;
|
240 | 29133e9a | bellard | } |
241 | 29133e9a | bellard | if (boff != 0x555 || cmd != 0xAA) { |
242 | 29133e9a | bellard | DPRINTF("%s: unlock0 failed %04x %02x %04x\n",
|
243 | 29133e9a | bellard | __func__, boff, cmd, 0x555);
|
244 | 29133e9a | bellard | goto reset_flash;
|
245 | 29133e9a | bellard | } |
246 | 29133e9a | bellard | DPRINTF("%s: unlock sequence started\n", __func__);
|
247 | 29133e9a | bellard | break;
|
248 | 29133e9a | bellard | case 1: |
249 | 29133e9a | bellard | /* We started an unlock sequence */
|
250 | 29133e9a | bellard | check_unlock1:
|
251 | 29133e9a | bellard | if (boff != 0x2AA || cmd != 0x55) { |
252 | 29133e9a | bellard | DPRINTF("%s: unlock1 failed %04x %02x\n", __func__, boff, cmd);
|
253 | 29133e9a | bellard | goto reset_flash;
|
254 | 29133e9a | bellard | } |
255 | 29133e9a | bellard | DPRINTF("%s: unlock sequence done\n", __func__);
|
256 | 29133e9a | bellard | break;
|
257 | 29133e9a | bellard | case 2: |
258 | 29133e9a | bellard | /* We finished an unlock sequence */
|
259 | 29133e9a | bellard | if (!pfl->bypass && boff != 0x555) { |
260 | 29133e9a | bellard | DPRINTF("%s: command failed %04x %02x\n", __func__, boff, cmd);
|
261 | 29133e9a | bellard | goto reset_flash;
|
262 | 29133e9a | bellard | } |
263 | 29133e9a | bellard | switch (cmd) {
|
264 | 29133e9a | bellard | case 0x20: |
265 | 29133e9a | bellard | pfl->bypass = 1;
|
266 | 29133e9a | bellard | goto do_bypass;
|
267 | 29133e9a | bellard | case 0x80: |
268 | 29133e9a | bellard | case 0x90: |
269 | 29133e9a | bellard | case 0xA0: |
270 | 29133e9a | bellard | pfl->cmd = cmd; |
271 | 29133e9a | bellard | DPRINTF("%s: starting command %02x\n", __func__, cmd);
|
272 | 29133e9a | bellard | break;
|
273 | 29133e9a | bellard | default:
|
274 | 29133e9a | bellard | DPRINTF("%s: unknown command %02x\n", __func__, cmd);
|
275 | 29133e9a | bellard | goto reset_flash;
|
276 | 29133e9a | bellard | } |
277 | 29133e9a | bellard | break;
|
278 | 29133e9a | bellard | case 3: |
279 | 29133e9a | bellard | switch (pfl->cmd) {
|
280 | 29133e9a | bellard | case 0x80: |
281 | 29133e9a | bellard | /* We need another unlock sequence */
|
282 | 29133e9a | bellard | goto check_unlock0;
|
283 | 29133e9a | bellard | case 0xA0: |
284 | 29133e9a | bellard | DPRINTF("%s: write data offset %08x %08x %d\n",
|
285 | 29133e9a | bellard | __func__, offset, value, width); |
286 | 29133e9a | bellard | p = pfl->storage; |
287 | 29133e9a | bellard | switch (width) {
|
288 | 29133e9a | bellard | case 1: |
289 | 29133e9a | bellard | p[offset] &= value; |
290 | 29133e9a | bellard | pflash_update(pfl, offset, 1);
|
291 | 29133e9a | bellard | break;
|
292 | 29133e9a | bellard | case 2: |
293 | 29133e9a | bellard | #if defined(TARGET_WORDS_BIGENDIAN)
|
294 | 29133e9a | bellard | p[offset] &= value >> 8;
|
295 | 29133e9a | bellard | p[offset + 1] &= value;
|
296 | 29133e9a | bellard | #else
|
297 | 29133e9a | bellard | p[offset] &= value; |
298 | 29133e9a | bellard | p[offset + 1] &= value >> 8; |
299 | 29133e9a | bellard | #endif
|
300 | 29133e9a | bellard | pflash_update(pfl, offset, 2);
|
301 | 29133e9a | bellard | break;
|
302 | 29133e9a | bellard | case 4: |
303 | 29133e9a | bellard | #if defined(TARGET_WORDS_BIGENDIAN)
|
304 | 29133e9a | bellard | p[offset] &= value >> 24;
|
305 | 29133e9a | bellard | p[offset + 1] &= value >> 16; |
306 | 29133e9a | bellard | p[offset + 2] &= value >> 8; |
307 | 29133e9a | bellard | p[offset + 3] &= value;
|
308 | 29133e9a | bellard | #else
|
309 | 29133e9a | bellard | p[offset] &= value; |
310 | 29133e9a | bellard | p[offset + 1] &= value >> 8; |
311 | 29133e9a | bellard | p[offset + 2] &= value >> 16; |
312 | 29133e9a | bellard | p[offset + 3] &= value >> 24; |
313 | 29133e9a | bellard | #endif
|
314 | 29133e9a | bellard | pflash_update(pfl, offset, 4);
|
315 | 29133e9a | bellard | break;
|
316 | 29133e9a | bellard | } |
317 | 29133e9a | bellard | pfl->status = 0x00 | ~(value & 0x80); |
318 | 29133e9a | bellard | /* Let's pretend write is immediate */
|
319 | 29133e9a | bellard | if (pfl->bypass)
|
320 | 29133e9a | bellard | goto do_bypass;
|
321 | 29133e9a | bellard | goto reset_flash;
|
322 | 29133e9a | bellard | case 0x90: |
323 | 29133e9a | bellard | if (pfl->bypass && cmd == 0x00) { |
324 | 29133e9a | bellard | /* Unlock bypass reset */
|
325 | 29133e9a | bellard | goto reset_flash;
|
326 | 29133e9a | bellard | } |
327 | 29133e9a | bellard | /* We can enter CFI query mode from autoselect mode */
|
328 | 29133e9a | bellard | if (boff == 0x55 && cmd == 0x98) |
329 | 29133e9a | bellard | goto enter_CFI_mode;
|
330 | 29133e9a | bellard | /* No break here */
|
331 | 29133e9a | bellard | default:
|
332 | 29133e9a | bellard | DPRINTF("%s: invalid write for command %02x\n",
|
333 | 29133e9a | bellard | __func__, pfl->cmd); |
334 | 29133e9a | bellard | goto reset_flash;
|
335 | 29133e9a | bellard | } |
336 | 29133e9a | bellard | case 4: |
337 | 29133e9a | bellard | switch (pfl->cmd) {
|
338 | 29133e9a | bellard | case 0xA0: |
339 | 29133e9a | bellard | /* Ignore writes while flash data write is occuring */
|
340 | 29133e9a | bellard | /* As we suppose write is immediate, this should never happen */
|
341 | 29133e9a | bellard | return;
|
342 | 29133e9a | bellard | case 0x80: |
343 | 29133e9a | bellard | goto check_unlock1;
|
344 | 29133e9a | bellard | default:
|
345 | 29133e9a | bellard | /* Should never happen */
|
346 | 29133e9a | bellard | DPRINTF("%s: invalid command state %02x (wc 4)\n",
|
347 | 29133e9a | bellard | __func__, pfl->cmd); |
348 | 29133e9a | bellard | goto reset_flash;
|
349 | 29133e9a | bellard | } |
350 | 29133e9a | bellard | break;
|
351 | 29133e9a | bellard | case 5: |
352 | 29133e9a | bellard | switch (cmd) {
|
353 | 29133e9a | bellard | case 0x10: |
354 | 29133e9a | bellard | if (boff != 0x555) { |
355 | 29133e9a | bellard | DPRINTF("%s: chip erase: invalid address %04x\n",
|
356 | 29133e9a | bellard | __func__, offset); |
357 | 29133e9a | bellard | goto reset_flash;
|
358 | 29133e9a | bellard | } |
359 | 29133e9a | bellard | /* Chip erase */
|
360 | 29133e9a | bellard | DPRINTF("%s: start chip erase\n", __func__);
|
361 | 29133e9a | bellard | memset(pfl->storage, 0xFF, pfl->total_len);
|
362 | 29133e9a | bellard | pfl->status = 0x00;
|
363 | 29133e9a | bellard | pflash_update(pfl, 0, pfl->total_len);
|
364 | 29133e9a | bellard | /* Let's wait 5 seconds before chip erase is done */
|
365 | 29133e9a | bellard | qemu_mod_timer(pfl->timer, |
366 | 29133e9a | bellard | qemu_get_clock(vm_clock) + (ticks_per_sec * 5));
|
367 | 29133e9a | bellard | break;
|
368 | 29133e9a | bellard | case 0x30: |
369 | 29133e9a | bellard | /* Sector erase */
|
370 | 29133e9a | bellard | p = pfl->storage; |
371 | 29133e9a | bellard | offset &= ~(pfl->sector_len - 1);
|
372 | 29133e9a | bellard | DPRINTF("%s: start sector erase at %08x\n", __func__, offset);
|
373 | 29133e9a | bellard | memset(p + offset, 0xFF, pfl->sector_len);
|
374 | 29133e9a | bellard | pflash_update(pfl, offset, pfl->sector_len); |
375 | 29133e9a | bellard | pfl->status = 0x00;
|
376 | 29133e9a | bellard | /* Let's wait 1/2 second before sector erase is done */
|
377 | 29133e9a | bellard | qemu_mod_timer(pfl->timer, |
378 | 29133e9a | bellard | qemu_get_clock(vm_clock) + (ticks_per_sec / 2));
|
379 | 29133e9a | bellard | break;
|
380 | 29133e9a | bellard | default:
|
381 | 29133e9a | bellard | DPRINTF("%s: invalid command %02x (wc 5)\n", __func__, cmd);
|
382 | 29133e9a | bellard | goto reset_flash;
|
383 | 29133e9a | bellard | } |
384 | 29133e9a | bellard | pfl->cmd = cmd; |
385 | 29133e9a | bellard | break;
|
386 | 29133e9a | bellard | case 6: |
387 | 29133e9a | bellard | switch (pfl->cmd) {
|
388 | 29133e9a | bellard | case 0x10: |
389 | 29133e9a | bellard | /* Ignore writes during chip erase */
|
390 | 29133e9a | bellard | return;
|
391 | 29133e9a | bellard | case 0x30: |
392 | 29133e9a | bellard | /* Ignore writes during sector erase */
|
393 | 29133e9a | bellard | return;
|
394 | 29133e9a | bellard | default:
|
395 | 29133e9a | bellard | /* Should never happen */
|
396 | 29133e9a | bellard | DPRINTF("%s: invalid command state %02x (wc 6)\n",
|
397 | 29133e9a | bellard | __func__, pfl->cmd); |
398 | 29133e9a | bellard | goto reset_flash;
|
399 | 29133e9a | bellard | } |
400 | 29133e9a | bellard | break;
|
401 | 29133e9a | bellard | case 7: /* Special value for CFI queries */ |
402 | 29133e9a | bellard | DPRINTF("%s: invalid write in CFI query mode\n", __func__);
|
403 | 29133e9a | bellard | goto reset_flash;
|
404 | 29133e9a | bellard | default:
|
405 | 29133e9a | bellard | /* Should never happen */
|
406 | 29133e9a | bellard | DPRINTF("%s: invalid write state (wc 7)\n", __func__);
|
407 | 29133e9a | bellard | goto reset_flash;
|
408 | 29133e9a | bellard | } |
409 | 29133e9a | bellard | pfl->wcycle++; |
410 | 29133e9a | bellard | |
411 | 29133e9a | bellard | return;
|
412 | 29133e9a | bellard | |
413 | 29133e9a | bellard | /* Reset flash */
|
414 | 29133e9a | bellard | reset_flash:
|
415 | 29133e9a | bellard | if (pfl->wcycle != 0) { |
416 | 29133e9a | bellard | cpu_register_physical_memory(pfl->base, pfl->total_len, |
417 | 29133e9a | bellard | pfl->off | IO_MEM_ROMD | pfl->fl_mem); |
418 | 29133e9a | bellard | } |
419 | 29133e9a | bellard | pfl->bypass = 0;
|
420 | 29133e9a | bellard | pfl->wcycle = 0;
|
421 | 29133e9a | bellard | pfl->cmd = 0;
|
422 | 29133e9a | bellard | return;
|
423 | 29133e9a | bellard | |
424 | 29133e9a | bellard | do_bypass:
|
425 | 29133e9a | bellard | pfl->wcycle = 2;
|
426 | 29133e9a | bellard | pfl->cmd = 0;
|
427 | 29133e9a | bellard | return;
|
428 | 29133e9a | bellard | } |
429 | 29133e9a | bellard | |
430 | 29133e9a | bellard | |
431 | 29133e9a | bellard | static uint32_t pflash_readb (void *opaque, target_phys_addr_t addr) |
432 | 29133e9a | bellard | { |
433 | 29133e9a | bellard | return pflash_read(opaque, addr, 1); |
434 | 29133e9a | bellard | } |
435 | 29133e9a | bellard | |
436 | 29133e9a | bellard | static uint32_t pflash_readw (void *opaque, target_phys_addr_t addr) |
437 | 29133e9a | bellard | { |
438 | 29133e9a | bellard | pflash_t *pfl = opaque; |
439 | 29133e9a | bellard | |
440 | 29133e9a | bellard | return pflash_read(pfl, addr, 2); |
441 | 29133e9a | bellard | } |
442 | 29133e9a | bellard | |
443 | 29133e9a | bellard | static uint32_t pflash_readl (void *opaque, target_phys_addr_t addr) |
444 | 29133e9a | bellard | { |
445 | 29133e9a | bellard | pflash_t *pfl = opaque; |
446 | 29133e9a | bellard | |
447 | 29133e9a | bellard | return pflash_read(pfl, addr, 4); |
448 | 29133e9a | bellard | } |
449 | 29133e9a | bellard | |
450 | 29133e9a | bellard | static void pflash_writeb (void *opaque, target_phys_addr_t addr, |
451 | 29133e9a | bellard | uint32_t value) |
452 | 29133e9a | bellard | { |
453 | 29133e9a | bellard | pflash_write(opaque, addr, value, 1);
|
454 | 29133e9a | bellard | } |
455 | 29133e9a | bellard | |
456 | 29133e9a | bellard | static void pflash_writew (void *opaque, target_phys_addr_t addr, |
457 | 29133e9a | bellard | uint32_t value) |
458 | 29133e9a | bellard | { |
459 | 29133e9a | bellard | pflash_t *pfl = opaque; |
460 | 29133e9a | bellard | |
461 | 29133e9a | bellard | pflash_write(pfl, addr, value, 2);
|
462 | 29133e9a | bellard | } |
463 | 29133e9a | bellard | |
464 | 29133e9a | bellard | static void pflash_writel (void *opaque, target_phys_addr_t addr, |
465 | 29133e9a | bellard | uint32_t value) |
466 | 29133e9a | bellard | { |
467 | 29133e9a | bellard | pflash_t *pfl = opaque; |
468 | 29133e9a | bellard | |
469 | 29133e9a | bellard | pflash_write(pfl, addr, value, 4);
|
470 | 29133e9a | bellard | } |
471 | 29133e9a | bellard | |
472 | 29133e9a | bellard | static CPUWriteMemoryFunc *pflash_write_ops[] = {
|
473 | 29133e9a | bellard | &pflash_writeb, |
474 | 29133e9a | bellard | &pflash_writew, |
475 | 29133e9a | bellard | &pflash_writel, |
476 | 29133e9a | bellard | }; |
477 | 29133e9a | bellard | |
478 | 29133e9a | bellard | static CPUReadMemoryFunc *pflash_read_ops[] = {
|
479 | 29133e9a | bellard | &pflash_readb, |
480 | 29133e9a | bellard | &pflash_readw, |
481 | 29133e9a | bellard | &pflash_readl, |
482 | 29133e9a | bellard | }; |
483 | 29133e9a | bellard | |
484 | 29133e9a | bellard | /* Count trailing zeroes of a 32 bits quantity */
|
485 | 29133e9a | bellard | static int ctz32 (uint32_t n) |
486 | 29133e9a | bellard | { |
487 | 29133e9a | bellard | int ret;
|
488 | 29133e9a | bellard | |
489 | 29133e9a | bellard | ret = 0;
|
490 | 29133e9a | bellard | if (!(n & 0xFFFF)) { |
491 | 29133e9a | bellard | ret += 16;
|
492 | 29133e9a | bellard | n = n >> 16;
|
493 | 29133e9a | bellard | } |
494 | 29133e9a | bellard | if (!(n & 0xFF)) { |
495 | 29133e9a | bellard | ret += 8;
|
496 | 29133e9a | bellard | n = n >> 8;
|
497 | 29133e9a | bellard | } |
498 | 29133e9a | bellard | if (!(n & 0xF)) { |
499 | 29133e9a | bellard | ret += 4;
|
500 | 29133e9a | bellard | n = n >> 4;
|
501 | 29133e9a | bellard | } |
502 | 29133e9a | bellard | if (!(n & 0x3)) { |
503 | 29133e9a | bellard | ret += 2;
|
504 | 29133e9a | bellard | n = n >> 2;
|
505 | 29133e9a | bellard | } |
506 | 29133e9a | bellard | if (!(n & 0x1)) { |
507 | 29133e9a | bellard | ret++; |
508 | 29133e9a | bellard | n = n >> 1;
|
509 | 29133e9a | bellard | } |
510 | 29133e9a | bellard | #if 0 /* This is not necessary as n is never 0 */
|
511 | 29133e9a | bellard | if (!n)
|
512 | 29133e9a | bellard | ret++;
|
513 | 29133e9a | bellard | #endif
|
514 | 29133e9a | bellard | |
515 | 29133e9a | bellard | return ret;
|
516 | 29133e9a | bellard | } |
517 | 29133e9a | bellard | |
518 | 29133e9a | bellard | pflash_t *pflash_register (target_ulong base, ram_addr_t off, |
519 | 29133e9a | bellard | BlockDriverState *bs, |
520 | 29133e9a | bellard | target_ulong sector_len, int nb_blocs, int width, |
521 | 29133e9a | bellard | uint16_t id0, uint16_t id1, |
522 | 29133e9a | bellard | uint16_t id2, uint16_t id3) |
523 | 29133e9a | bellard | { |
524 | 29133e9a | bellard | pflash_t *pfl; |
525 | 29133e9a | bellard | target_long total_len; |
526 | 29133e9a | bellard | |
527 | 29133e9a | bellard | total_len = sector_len * nb_blocs; |
528 | 29133e9a | bellard | /* XXX: to be fixed */
|
529 | 29133e9a | bellard | if (total_len != (8 * 1024 * 1024) && total_len != (16 * 1024 * 1024) && |
530 | 29133e9a | bellard | total_len != (32 * 1024 * 1024) && total_len != (64 * 1024 * 1024)) |
531 | 29133e9a | bellard | return NULL; |
532 | 29133e9a | bellard | pfl = qemu_mallocz(sizeof(pflash_t));
|
533 | 29133e9a | bellard | if (pfl == NULL) |
534 | 29133e9a | bellard | return NULL; |
535 | 29133e9a | bellard | pfl->storage = phys_ram_base + off; |
536 | 29133e9a | bellard | pfl->fl_mem = cpu_register_io_memory(0, pflash_read_ops, pflash_write_ops, pfl);
|
537 | 29133e9a | bellard | pfl->off = off; |
538 | 29133e9a | bellard | cpu_register_physical_memory(base, total_len, |
539 | 29133e9a | bellard | off | pfl->fl_mem | IO_MEM_ROMD); |
540 | 29133e9a | bellard | pfl->bs = bs; |
541 | 29133e9a | bellard | if (pfl->bs) {
|
542 | 29133e9a | bellard | /* read the initial flash content */
|
543 | 29133e9a | bellard | bdrv_read(pfl->bs, 0, pfl->storage, total_len >> 9); |
544 | 29133e9a | bellard | } |
545 | 29133e9a | bellard | #if 0 /* XXX: there should be a bit to set up read-only,
|
546 | 29133e9a | bellard | * the same way the hardware does (with WP pin).
|
547 | 29133e9a | bellard | */
|
548 | 29133e9a | bellard | pfl->ro = 1;
|
549 | 29133e9a | bellard | #else
|
550 | 29133e9a | bellard | pfl->ro = 0;
|
551 | 29133e9a | bellard | #endif
|
552 | 29133e9a | bellard | pfl->timer = qemu_new_timer(vm_clock, pflash_timer, pfl); |
553 | 29133e9a | bellard | pfl->base = base; |
554 | 29133e9a | bellard | pfl->sector_len = sector_len; |
555 | 29133e9a | bellard | pfl->total_len = total_len; |
556 | 29133e9a | bellard | pfl->width = width; |
557 | 29133e9a | bellard | pfl->wcycle = 0;
|
558 | 29133e9a | bellard | pfl->cmd = 0;
|
559 | 29133e9a | bellard | pfl->status = 0;
|
560 | 29133e9a | bellard | pfl->ident[0] = id0;
|
561 | 29133e9a | bellard | pfl->ident[1] = id1;
|
562 | 29133e9a | bellard | pfl->ident[2] = id2;
|
563 | 29133e9a | bellard | pfl->ident[3] = id3;
|
564 | 29133e9a | bellard | /* Hardcoded CFI table (mostly from SG29 Spansion flash) */
|
565 | 29133e9a | bellard | pfl->cfi_len = 0x52;
|
566 | 29133e9a | bellard | /* Standard "QRY" string */
|
567 | 29133e9a | bellard | pfl->cfi_table[0x10] = 'Q'; |
568 | 29133e9a | bellard | pfl->cfi_table[0x11] = 'R'; |
569 | 29133e9a | bellard | pfl->cfi_table[0x12] = 'Y'; |
570 | 29133e9a | bellard | /* Command set (AMD/Fujitsu) */
|
571 | 29133e9a | bellard | pfl->cfi_table[0x13] = 0x02; |
572 | 29133e9a | bellard | pfl->cfi_table[0x14] = 0x00; |
573 | 29133e9a | bellard | /* Primary extended table address (none) */
|
574 | 29133e9a | bellard | pfl->cfi_table[0x15] = 0x00; |
575 | 29133e9a | bellard | pfl->cfi_table[0x16] = 0x00; |
576 | 29133e9a | bellard | /* Alternate command set (none) */
|
577 | 29133e9a | bellard | pfl->cfi_table[0x17] = 0x00; |
578 | 29133e9a | bellard | pfl->cfi_table[0x18] = 0x00; |
579 | 29133e9a | bellard | /* Alternate extended table (none) */
|
580 | 29133e9a | bellard | pfl->cfi_table[0x19] = 0x00; |
581 | 29133e9a | bellard | pfl->cfi_table[0x1A] = 0x00; |
582 | 29133e9a | bellard | /* Vcc min */
|
583 | 29133e9a | bellard | pfl->cfi_table[0x1B] = 0x27; |
584 | 29133e9a | bellard | /* Vcc max */
|
585 | 29133e9a | bellard | pfl->cfi_table[0x1C] = 0x36; |
586 | 29133e9a | bellard | /* Vpp min (no Vpp pin) */
|
587 | 29133e9a | bellard | pfl->cfi_table[0x1D] = 0x00; |
588 | 29133e9a | bellard | /* Vpp max (no Vpp pin) */
|
589 | 29133e9a | bellard | pfl->cfi_table[0x1E] = 0x00; |
590 | 29133e9a | bellard | /* Reserved */
|
591 | 29133e9a | bellard | pfl->cfi_table[0x1F] = 0x07; |
592 | 29133e9a | bellard | /* Timeout for min size buffer write (16 ?s) */
|
593 | 29133e9a | bellard | pfl->cfi_table[0x20] = 0x04; |
594 | 29133e9a | bellard | /* Typical timeout for block erase (512 ms) */
|
595 | 29133e9a | bellard | pfl->cfi_table[0x21] = 0x09; |
596 | 29133e9a | bellard | /* Typical timeout for full chip erase (4096 ms) */
|
597 | 29133e9a | bellard | pfl->cfi_table[0x22] = 0x0C; |
598 | 29133e9a | bellard | /* Reserved */
|
599 | 29133e9a | bellard | pfl->cfi_table[0x23] = 0x01; |
600 | 29133e9a | bellard | /* Max timeout for buffer write */
|
601 | 29133e9a | bellard | pfl->cfi_table[0x24] = 0x04; |
602 | 29133e9a | bellard | /* Max timeout for block erase */
|
603 | 29133e9a | bellard | pfl->cfi_table[0x25] = 0x0A; |
604 | 29133e9a | bellard | /* Max timeout for chip erase */
|
605 | 29133e9a | bellard | pfl->cfi_table[0x26] = 0x0D; |
606 | 29133e9a | bellard | /* Device size */
|
607 | 29133e9a | bellard | pfl->cfi_table[0x27] = ctz32(total_len) + 1; |
608 | 29133e9a | bellard | /* Flash device interface (8 & 16 bits) */
|
609 | 29133e9a | bellard | pfl->cfi_table[0x28] = 0x02; |
610 | 29133e9a | bellard | pfl->cfi_table[0x29] = 0x00; |
611 | 29133e9a | bellard | /* Max number of bytes in multi-bytes write */
|
612 | 29133e9a | bellard | pfl->cfi_table[0x2A] = 0x05; |
613 | 29133e9a | bellard | pfl->cfi_table[0x2B] = 0x00; |
614 | 29133e9a | bellard | /* Number of erase block regions (uniform) */
|
615 | 29133e9a | bellard | pfl->cfi_table[0x2C] = 0x01; |
616 | 29133e9a | bellard | /* Erase block region 1 */
|
617 | 29133e9a | bellard | pfl->cfi_table[0x2D] = nb_blocs - 1; |
618 | 29133e9a | bellard | pfl->cfi_table[0x2E] = (nb_blocs - 1) >> 8; |
619 | 29133e9a | bellard | pfl->cfi_table[0x2F] = sector_len >> 8; |
620 | 29133e9a | bellard | pfl->cfi_table[0x30] = sector_len >> 16; |
621 | 29133e9a | bellard | |
622 | 29133e9a | bellard | return pfl;
|
623 | 29133e9a | bellard | } |