]>
Commit | Line | Data |
---|---|---|
29133e9a FB |
1 | /* |
2 | * CFI parallel flash with AMD command set emulation | |
5fafdf24 | 3 | * |
29133e9a FB |
4 | * Copyright (c) 2005 Jocelyn Mayer |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
29133e9a FB |
18 | */ |
19 | ||
20 | /* | |
21 | * For now, this code can emulate flashes of 1, 2 or 4 bytes width. | |
22 | * Supported commands/modes are: | |
23 | * - flash read | |
24 | * - flash write | |
25 | * - flash ID read | |
26 | * - sector erase | |
27 | * - chip erase | |
28 | * - unlock bypass command | |
29 | * - CFI queries | |
30 | * | |
31 | * It does not support flash interleaving. | |
32 | * It does not implement boot blocs with reduced size | |
33 | * It does not implement software data protection as found in many real chips | |
34 | * It does not implement erase suspend/resume commands | |
35 | * It does not implement multiple sectors erase | |
36 | */ | |
37 | ||
80c71a24 | 38 | #include "qemu/osdep.h" |
83c9f4ca | 39 | #include "hw/hw.h" |
06f15217 | 40 | #include "hw/block/block.h" |
0d09e41a | 41 | #include "hw/block/flash.h" |
da34e65c | 42 | #include "qapi/error.h" |
1de7afc9 | 43 | #include "qemu/timer.h" |
4be74634 | 44 | #include "sysemu/block-backend.h" |
1de7afc9 | 45 | #include "qemu/host-utils.h" |
0b8fa32f | 46 | #include "qemu/module.h" |
83c9f4ca | 47 | #include "hw/sysbus.h" |
13019f1f | 48 | #include "trace.h" |
29133e9a | 49 | |
6536987f | 50 | #define PFLASH_DEBUG false |
ec9ea489 PC |
51 | #define DPRINTF(fmt, ...) \ |
52 | do { \ | |
6536987f PMD |
53 | if (PFLASH_DEBUG) { \ |
54 | fprintf(stderr, "PFLASH: " fmt, ## __VA_ARGS__); \ | |
55 | } \ | |
29133e9a | 56 | } while (0) |
29133e9a | 57 | |
661bfc80 JK |
58 | #define PFLASH_LAZY_ROMD_THRESHOLD 42 |
59 | ||
aeaf6c20 PMD |
60 | /* Special write cycles for CFI queries. */ |
61 | enum { | |
62 | WCYCLE_CFI = 7, | |
63 | }; | |
64 | ||
16434065 | 65 | struct PFlashCFI02 { |
3509c396 HT |
66 | /*< private >*/ |
67 | SysBusDevice parent_obj; | |
68 | /*< public >*/ | |
69 | ||
4be74634 | 70 | BlockBackend *blk; |
71db710f | 71 | uint32_t sector_len; |
368a354f | 72 | uint32_t nb_blocs; |
4fbd24ba | 73 | uint32_t chip_len; |
368a354f PC |
74 | uint8_t mappings; |
75 | uint8_t width; | |
76 | uint8_t be; | |
29133e9a FB |
77 | int wcycle; /* if 0, the flash is read normally */ |
78 | int bypass; | |
79 | int ro; | |
80 | uint8_t cmd; | |
81 | uint8_t status; | |
368a354f PC |
82 | /* FIXME: implement array device properties */ |
83 | uint16_t ident0; | |
84 | uint16_t ident1; | |
85 | uint16_t ident2; | |
86 | uint16_t ident3; | |
87 | uint16_t unlock_addr0; | |
88 | uint16_t unlock_addr1; | |
29133e9a | 89 | uint8_t cfi_table[0x52]; |
d80cf1eb | 90 | QEMUTimer timer; |
cfe5f011 AK |
91 | /* The device replicates the flash memory across its memory space. Emulate |
92 | * that by having a container (.mem) filled with an array of aliases | |
93 | * (.mem_mappings) pointing to the flash memory (.orig_mem). | |
94 | */ | |
95 | MemoryRegion mem; | |
96 | MemoryRegion *mem_mappings; /* array; one per mapping */ | |
97 | MemoryRegion orig_mem; | |
9c9bb6c8 | 98 | int rom_mode; |
661bfc80 | 99 | int read_counter; /* used for lazy switch-back to rom mode */ |
368a354f | 100 | char *name; |
29133e9a FB |
101 | void *storage; |
102 | }; | |
103 | ||
1d311e73 PMD |
104 | /* |
105 | * Toggle status bit DQ7. | |
106 | */ | |
107 | static inline void toggle_dq7(PFlashCFI02 *pfl) | |
108 | { | |
109 | pfl->status ^= 0x80; | |
110 | } | |
111 | ||
112 | /* | |
113 | * Set status bit DQ7 to bit 7 of value. | |
114 | */ | |
115 | static inline void set_dq7(PFlashCFI02 *pfl, uint8_t value) | |
116 | { | |
117 | pfl->status &= 0x7F; | |
118 | pfl->status |= value & 0x80; | |
119 | } | |
120 | ||
121 | /* | |
122 | * Toggle status bit DQ6. | |
123 | */ | |
124 | static inline void toggle_dq6(PFlashCFI02 *pfl) | |
125 | { | |
126 | pfl->status ^= 0x40; | |
127 | } | |
128 | ||
cfe5f011 AK |
129 | /* |
130 | * Set up replicated mappings of the same region. | |
131 | */ | |
16434065 | 132 | static void pflash_setup_mappings(PFlashCFI02 *pfl) |
c8a50e59 | 133 | { |
cfe5f011 | 134 | unsigned i; |
a8170e5e | 135 | hwaddr size = memory_region_size(&pfl->orig_mem); |
cfe5f011 | 136 | |
2d256e6f | 137 | memory_region_init(&pfl->mem, OBJECT(pfl), "pflash", pfl->mappings * size); |
cfe5f011 AK |
138 | pfl->mem_mappings = g_new(MemoryRegion, pfl->mappings); |
139 | for (i = 0; i < pfl->mappings; ++i) { | |
2d256e6f PB |
140 | memory_region_init_alias(&pfl->mem_mappings[i], OBJECT(pfl), |
141 | "pflash-alias", &pfl->orig_mem, 0, size); | |
cfe5f011 AK |
142 | memory_region_add_subregion(&pfl->mem, i * size, &pfl->mem_mappings[i]); |
143 | } | |
144 | } | |
01e0451a | 145 | |
16434065 | 146 | static void pflash_register_memory(PFlashCFI02 *pfl, int rom_mode) |
cfe5f011 | 147 | { |
5f9a5ea1 | 148 | memory_region_rom_device_set_romd(&pfl->orig_mem, rom_mode); |
bda254da | 149 | pfl->rom_mode = rom_mode; |
4fbd24ba AZ |
150 | } |
151 | ||
29133e9a FB |
152 | static void pflash_timer (void *opaque) |
153 | { | |
16434065 | 154 | PFlashCFI02 *pfl = opaque; |
29133e9a | 155 | |
13019f1f | 156 | trace_pflash_timer_expired(pfl->cmd); |
29133e9a | 157 | /* Reset flash */ |
1d311e73 | 158 | toggle_dq7(pfl); |
29133e9a FB |
159 | if (pfl->bypass) { |
160 | pfl->wcycle = 2; | |
161 | } else { | |
4fbd24ba | 162 | pflash_register_memory(pfl, 1); |
29133e9a FB |
163 | pfl->wcycle = 0; |
164 | } | |
165 | pfl->cmd = 0; | |
166 | } | |
167 | ||
06e8b8e3 PMD |
168 | /* |
169 | * Read data from flash. | |
170 | */ | |
171 | static uint64_t pflash_data_read(PFlashCFI02 *pfl, hwaddr offset, | |
172 | unsigned int width) | |
173 | { | |
174 | uint8_t *p = (uint8_t *)pfl->storage + offset; | |
175 | uint64_t ret = pfl->be ? ldn_be_p(p, width) : ldn_le_p(p, width); | |
176 | trace_pflash_data_read(offset, width << 1, ret); | |
177 | return ret; | |
178 | } | |
179 | ||
aff498cf | 180 | static uint64_t pflash_read(void *opaque, hwaddr offset, unsigned int width) |
29133e9a | 181 | { |
aff498cf | 182 | PFlashCFI02 *pfl = opaque; |
a8170e5e | 183 | hwaddr boff; |
aff498cf | 184 | uint64_t ret; |
29133e9a | 185 | |
29133e9a | 186 | ret = -1; |
661bfc80 JK |
187 | /* Lazy reset to ROMD mode after a certain amount of read accesses */ |
188 | if (!pfl->rom_mode && pfl->wcycle == 0 && | |
189 | ++pfl->read_counter > PFLASH_LAZY_ROMD_THRESHOLD) { | |
190 | pflash_register_memory(pfl, 1); | |
0f459d16 | 191 | } |
4fbd24ba | 192 | offset &= pfl->chip_len - 1; |
29133e9a FB |
193 | boff = offset & 0xFF; |
194 | if (pfl->width == 2) | |
195 | boff = boff >> 1; | |
196 | else if (pfl->width == 4) | |
197 | boff = boff >> 2; | |
198 | switch (pfl->cmd) { | |
199 | default: | |
200 | /* This should never happen : reset state & treat it as a read*/ | |
201 | DPRINTF("%s: unknown command state: %x\n", __func__, pfl->cmd); | |
202 | pfl->wcycle = 0; | |
203 | pfl->cmd = 0; | |
30954850 | 204 | /* fall through to the read code */ |
29133e9a FB |
205 | case 0x80: |
206 | /* We accept reads during second unlock sequence... */ | |
207 | case 0x00: | |
29133e9a | 208 | /* Flash area read */ |
06e8b8e3 | 209 | ret = pflash_data_read(pfl, offset, width); |
29133e9a FB |
210 | break; |
211 | case 0x90: | |
212 | /* flash ID read */ | |
213 | switch (boff) { | |
214 | case 0x00: | |
215 | case 0x01: | |
368a354f | 216 | ret = boff & 0x01 ? pfl->ident1 : pfl->ident0; |
29133e9a FB |
217 | break; |
218 | case 0x02: | |
219 | ret = 0x00; /* Pretend all sectors are unprotected */ | |
220 | break; | |
221 | case 0x0E: | |
222 | case 0x0F: | |
368a354f | 223 | ret = boff & 0x01 ? pfl->ident3 : pfl->ident2; |
7f7bdcaf PMD |
224 | if (ret != (uint8_t)-1) { |
225 | break; | |
368a354f | 226 | } |
7f7bdcaf | 227 | /* Fall through to data read. */ |
29133e9a | 228 | default: |
06e8b8e3 | 229 | ret = pflash_data_read(pfl, offset, width); |
29133e9a | 230 | } |
aff498cf | 231 | DPRINTF("%s: ID " TARGET_FMT_plx " %" PRIx64 "\n", __func__, boff, ret); |
29133e9a FB |
232 | break; |
233 | case 0xA0: | |
234 | case 0x10: | |
235 | case 0x30: | |
236 | /* Status register read */ | |
237 | ret = pfl->status; | |
aff498cf | 238 | DPRINTF("%s: status %" PRIx64 "\n", __func__, ret); |
1d311e73 | 239 | toggle_dq6(pfl); |
29133e9a FB |
240 | break; |
241 | case 0x98: | |
242 | /* CFI query mode */ | |
07c13a71 | 243 | if (boff < sizeof(pfl->cfi_table)) { |
29133e9a | 244 | ret = pfl->cfi_table[boff]; |
07c13a71 PMD |
245 | } else { |
246 | ret = 0; | |
247 | } | |
29133e9a FB |
248 | break; |
249 | } | |
e8aa2d95 | 250 | trace_pflash_io_read(offset, width, width << 1, ret, pfl->cmd, pfl->wcycle); |
29133e9a FB |
251 | |
252 | return ret; | |
253 | } | |
254 | ||
255 | /* update flash content on disk */ | |
aff498cf | 256 | static void pflash_update(PFlashCFI02 *pfl, int offset, int size) |
29133e9a FB |
257 | { |
258 | int offset_end; | |
4be74634 | 259 | if (pfl->blk) { |
29133e9a | 260 | offset_end = offset + size; |
098e732d EB |
261 | /* widen to sector boundaries */ |
262 | offset = QEMU_ALIGN_DOWN(offset, BDRV_SECTOR_SIZE); | |
263 | offset_end = QEMU_ALIGN_UP(offset_end, BDRV_SECTOR_SIZE); | |
264 | blk_pwrite(pfl->blk, offset, pfl->storage + offset, | |
265 | offset_end - offset, 0); | |
29133e9a FB |
266 | } |
267 | } | |
268 | ||
aff498cf PMD |
269 | static void pflash_write(void *opaque, hwaddr offset, uint64_t value, |
270 | unsigned int width) | |
29133e9a | 271 | { |
aff498cf | 272 | PFlashCFI02 *pfl = opaque; |
a8170e5e | 273 | hwaddr boff; |
29133e9a FB |
274 | uint8_t *p; |
275 | uint8_t cmd; | |
276 | ||
e8aa2d95 | 277 | trace_pflash_io_write(offset, width, width << 1, value, pfl->wcycle); |
95d1f3ed JM |
278 | cmd = value; |
279 | if (pfl->cmd != 0xA0 && cmd == 0xF0) { | |
95d1f3ed JM |
280 | goto reset_flash; |
281 | } | |
4fbd24ba | 282 | offset &= pfl->chip_len - 1; |
3b46e624 | 283 | |
6682bc1e | 284 | boff = offset; |
29133e9a FB |
285 | if (pfl->width == 2) |
286 | boff = boff >> 1; | |
287 | else if (pfl->width == 4) | |
288 | boff = boff >> 2; | |
6682bc1e SC |
289 | /* Only the least-significant 11 bits are used in most cases. */ |
290 | boff &= 0x7FF; | |
29133e9a FB |
291 | switch (pfl->wcycle) { |
292 | case 0: | |
9c9bb6c8 AZ |
293 | /* Set the device in I/O access mode if required */ |
294 | if (pfl->rom_mode) | |
295 | pflash_register_memory(pfl, 0); | |
661bfc80 | 296 | pfl->read_counter = 0; |
29133e9a FB |
297 | /* We're in read mode */ |
298 | check_unlock0: | |
299 | if (boff == 0x55 && cmd == 0x98) { | |
300 | enter_CFI_mode: | |
301 | /* Enter CFI query mode */ | |
aeaf6c20 | 302 | pfl->wcycle = WCYCLE_CFI; |
29133e9a FB |
303 | pfl->cmd = 0x98; |
304 | return; | |
305 | } | |
368a354f | 306 | if (boff != pfl->unlock_addr0 || cmd != 0xAA) { |
f8be67ee | 307 | DPRINTF("%s: unlock0 failed " TARGET_FMT_plx " %02x %04x\n", |
368a354f | 308 | __func__, boff, cmd, pfl->unlock_addr0); |
29133e9a FB |
309 | goto reset_flash; |
310 | } | |
311 | DPRINTF("%s: unlock sequence started\n", __func__); | |
312 | break; | |
313 | case 1: | |
314 | /* We started an unlock sequence */ | |
315 | check_unlock1: | |
368a354f | 316 | if (boff != pfl->unlock_addr1 || cmd != 0x55) { |
f8be67ee | 317 | DPRINTF("%s: unlock1 failed " TARGET_FMT_plx " %02x\n", __func__, |
e96efcfc | 318 | boff, cmd); |
29133e9a FB |
319 | goto reset_flash; |
320 | } | |
321 | DPRINTF("%s: unlock sequence done\n", __func__); | |
322 | break; | |
323 | case 2: | |
324 | /* We finished an unlock sequence */ | |
368a354f | 325 | if (!pfl->bypass && boff != pfl->unlock_addr0) { |
f8be67ee | 326 | DPRINTF("%s: command failed " TARGET_FMT_plx " %02x\n", __func__, |
e96efcfc | 327 | boff, cmd); |
29133e9a FB |
328 | goto reset_flash; |
329 | } | |
330 | switch (cmd) { | |
331 | case 0x20: | |
332 | pfl->bypass = 1; | |
333 | goto do_bypass; | |
334 | case 0x80: | |
335 | case 0x90: | |
336 | case 0xA0: | |
337 | pfl->cmd = cmd; | |
338 | DPRINTF("%s: starting command %02x\n", __func__, cmd); | |
339 | break; | |
340 | default: | |
341 | DPRINTF("%s: unknown command %02x\n", __func__, cmd); | |
342 | goto reset_flash; | |
343 | } | |
344 | break; | |
345 | case 3: | |
346 | switch (pfl->cmd) { | |
347 | case 0x80: | |
348 | /* We need another unlock sequence */ | |
349 | goto check_unlock0; | |
350 | case 0xA0: | |
c1474acd | 351 | trace_pflash_data_write(offset, width << 1, value, 0); |
de8efe8f | 352 | if (!pfl->ro) { |
c3d25271 PMD |
353 | p = (uint8_t *)pfl->storage + offset; |
354 | if (pfl->be) { | |
355 | uint64_t current = ldn_be_p(p, width); | |
356 | stn_be_p(p, width, current & value); | |
357 | } else { | |
358 | uint64_t current = ldn_le_p(p, width); | |
359 | stn_le_p(p, width, current & value); | |
5f9fc5ad | 360 | } |
c3d25271 | 361 | pflash_update(pfl, offset, width); |
29133e9a | 362 | } |
1d311e73 PMD |
363 | /* |
364 | * While programming, status bit DQ7 should hold the opposite | |
365 | * value from how it was programmed. | |
366 | */ | |
367 | set_dq7(pfl, ~value); | |
29133e9a FB |
368 | /* Let's pretend write is immediate */ |
369 | if (pfl->bypass) | |
370 | goto do_bypass; | |
371 | goto reset_flash; | |
372 | case 0x90: | |
373 | if (pfl->bypass && cmd == 0x00) { | |
374 | /* Unlock bypass reset */ | |
375 | goto reset_flash; | |
376 | } | |
377 | /* We can enter CFI query mode from autoselect mode */ | |
378 | if (boff == 0x55 && cmd == 0x98) | |
379 | goto enter_CFI_mode; | |
380 | /* No break here */ | |
381 | default: | |
382 | DPRINTF("%s: invalid write for command %02x\n", | |
383 | __func__, pfl->cmd); | |
384 | goto reset_flash; | |
385 | } | |
386 | case 4: | |
387 | switch (pfl->cmd) { | |
388 | case 0xA0: | |
a1c7273b | 389 | /* Ignore writes while flash data write is occurring */ |
29133e9a FB |
390 | /* As we suppose write is immediate, this should never happen */ |
391 | return; | |
392 | case 0x80: | |
393 | goto check_unlock1; | |
394 | default: | |
395 | /* Should never happen */ | |
396 | DPRINTF("%s: invalid command state %02x (wc 4)\n", | |
397 | __func__, pfl->cmd); | |
398 | goto reset_flash; | |
399 | } | |
400 | break; | |
401 | case 5: | |
402 | switch (cmd) { | |
403 | case 0x10: | |
368a354f | 404 | if (boff != pfl->unlock_addr0) { |
f8be67ee | 405 | DPRINTF("%s: chip erase: invalid address " TARGET_FMT_plx "\n", |
29133e9a FB |
406 | __func__, offset); |
407 | goto reset_flash; | |
408 | } | |
409 | /* Chip erase */ | |
410 | DPRINTF("%s: start chip erase\n", __func__); | |
de8efe8f | 411 | if (!pfl->ro) { |
1eb27d69 | 412 | memset(pfl->storage, 0xff, pfl->chip_len); |
de8efe8f JJ |
413 | pflash_update(pfl, 0, pfl->chip_len); |
414 | } | |
1d311e73 | 415 | set_dq7(pfl, 0x00); |
29133e9a | 416 | /* Let's wait 5 seconds before chip erase is done */ |
d80cf1eb | 417 | timer_mod(&pfl->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
73bcb24d | 418 | (NANOSECONDS_PER_SECOND * 5)); |
29133e9a FB |
419 | break; |
420 | case 0x30: | |
421 | /* Sector erase */ | |
422 | p = pfl->storage; | |
423 | offset &= ~(pfl->sector_len - 1); | |
f8be67ee | 424 | DPRINTF("%s: start sector erase at " TARGET_FMT_plx "\n", __func__, |
e96efcfc | 425 | offset); |
de8efe8f JJ |
426 | if (!pfl->ro) { |
427 | memset(p + offset, 0xFF, pfl->sector_len); | |
428 | pflash_update(pfl, offset, pfl->sector_len); | |
429 | } | |
1d311e73 | 430 | set_dq7(pfl, 0x00); |
29133e9a | 431 | /* Let's wait 1/2 second before sector erase is done */ |
d80cf1eb | 432 | timer_mod(&pfl->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
73bcb24d | 433 | (NANOSECONDS_PER_SECOND / 2)); |
29133e9a FB |
434 | break; |
435 | default: | |
436 | DPRINTF("%s: invalid command %02x (wc 5)\n", __func__, cmd); | |
437 | goto reset_flash; | |
438 | } | |
439 | pfl->cmd = cmd; | |
440 | break; | |
441 | case 6: | |
442 | switch (pfl->cmd) { | |
443 | case 0x10: | |
444 | /* Ignore writes during chip erase */ | |
445 | return; | |
446 | case 0x30: | |
447 | /* Ignore writes during sector erase */ | |
448 | return; | |
449 | default: | |
450 | /* Should never happen */ | |
451 | DPRINTF("%s: invalid command state %02x (wc 6)\n", | |
452 | __func__, pfl->cmd); | |
453 | goto reset_flash; | |
454 | } | |
455 | break; | |
aeaf6c20 PMD |
456 | /* Special values for CFI queries */ |
457 | case WCYCLE_CFI: | |
29133e9a FB |
458 | DPRINTF("%s: invalid write in CFI query mode\n", __func__); |
459 | goto reset_flash; | |
460 | default: | |
461 | /* Should never happen */ | |
462 | DPRINTF("%s: invalid write state (wc 7)\n", __func__); | |
463 | goto reset_flash; | |
464 | } | |
465 | pfl->wcycle++; | |
466 | ||
467 | return; | |
468 | ||
469 | /* Reset flash */ | |
470 | reset_flash: | |
13019f1f | 471 | trace_pflash_reset(); |
29133e9a FB |
472 | pfl->bypass = 0; |
473 | pfl->wcycle = 0; | |
474 | pfl->cmd = 0; | |
475 | return; | |
476 | ||
477 | do_bypass: | |
478 | pfl->wcycle = 2; | |
479 | pfl->cmd = 0; | |
29133e9a FB |
480 | } |
481 | ||
aff498cf PMD |
482 | static const MemoryRegionOps pflash_cfi02_ops = { |
483 | .read = pflash_read, | |
484 | .write = pflash_write, | |
a4afb28d PM |
485 | .valid.min_access_size = 1, |
486 | .valid.max_access_size = 4, | |
cfe5f011 | 487 | .endianness = DEVICE_NATIVE_ENDIAN, |
29133e9a FB |
488 | }; |
489 | ||
da3bd642 | 490 | static void pflash_cfi02_realize(DeviceState *dev, Error **errp) |
29133e9a | 491 | { |
e7b62741 | 492 | PFlashCFI02 *pfl = PFLASH_CFI02(dev); |
d0e7605e | 493 | int ret; |
33e0eb52 | 494 | Error *local_err = NULL; |
29133e9a | 495 | |
8929fc3a ZY |
496 | if (pfl->sector_len == 0) { |
497 | error_setg(errp, "attribute \"sector-length\" not specified or zero."); | |
498 | return; | |
499 | } | |
500 | if (pfl->nb_blocs == 0) { | |
501 | error_setg(errp, "attribute \"num-blocks\" not specified or zero."); | |
502 | return; | |
503 | } | |
504 | if (pfl->name == NULL) { | |
505 | error_setg(errp, "attribute \"name\" not specified."); | |
506 | return; | |
507 | } | |
508 | ||
1eb27d69 | 509 | pfl->chip_len = pfl->sector_len * pfl->nb_blocs; |
368a354f | 510 | |
aff498cf PMD |
511 | memory_region_init_rom_device(&pfl->orig_mem, OBJECT(pfl), |
512 | &pflash_cfi02_ops, pfl, pfl->name, | |
1eb27d69 | 513 | pfl->chip_len, &local_err); |
33e0eb52 HT |
514 | if (local_err) { |
515 | error_propagate(errp, local_err); | |
516 | return; | |
517 | } | |
518 | ||
cfe5f011 | 519 | pfl->storage = memory_region_get_ram_ptr(&pfl->orig_mem); |
a17c17a2 KW |
520 | |
521 | if (pfl->blk) { | |
522 | uint64_t perm; | |
523 | pfl->ro = blk_is_read_only(pfl->blk); | |
524 | perm = BLK_PERM_CONSISTENT_READ | (pfl->ro ? 0 : BLK_PERM_WRITE); | |
525 | ret = blk_set_perm(pfl->blk, perm, BLK_PERM_ALL, errp); | |
526 | if (ret < 0) { | |
527 | return; | |
528 | } | |
529 | } else { | |
530 | pfl->ro = 0; | |
531 | } | |
532 | ||
4be74634 | 533 | if (pfl->blk) { |
1eb27d69 PMD |
534 | if (!blk_check_size_and_read_all(pfl->blk, pfl->storage, |
535 | pfl->chip_len, errp)) { | |
da3bd642 | 536 | vmstate_unregister_ram(&pfl->orig_mem, DEVICE(pfl)); |
da3bd642 | 537 | return; |
d0e7605e | 538 | } |
29133e9a | 539 | } |
de8efe8f | 540 | |
6682bc1e SC |
541 | /* Only 11 bits are used in the comparison. */ |
542 | pfl->unlock_addr0 &= 0x7FF; | |
543 | pfl->unlock_addr1 &= 0x7FF; | |
544 | ||
cfe5f011 AK |
545 | pflash_setup_mappings(pfl); |
546 | pfl->rom_mode = 1; | |
da3bd642 | 547 | sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem); |
de8efe8f | 548 | |
d80cf1eb | 549 | timer_init_ns(&pfl->timer, QEMU_CLOCK_VIRTUAL, pflash_timer, pfl); |
29133e9a FB |
550 | pfl->wcycle = 0; |
551 | pfl->cmd = 0; | |
552 | pfl->status = 0; | |
9ac45b88 | 553 | |
29133e9a | 554 | /* Hardcoded CFI table (mostly from SG29 Spansion flash) */ |
d6874c83 | 555 | const uint16_t pri_ofs = 0x31; |
29133e9a FB |
556 | /* Standard "QRY" string */ |
557 | pfl->cfi_table[0x10] = 'Q'; | |
558 | pfl->cfi_table[0x11] = 'R'; | |
559 | pfl->cfi_table[0x12] = 'Y'; | |
560 | /* Command set (AMD/Fujitsu) */ | |
561 | pfl->cfi_table[0x13] = 0x02; | |
562 | pfl->cfi_table[0x14] = 0x00; | |
78556820 | 563 | /* Primary extended table address */ |
d6874c83 PMD |
564 | pfl->cfi_table[0x15] = pri_ofs; |
565 | pfl->cfi_table[0x16] = pri_ofs >> 8; | |
29133e9a FB |
566 | /* Alternate command set (none) */ |
567 | pfl->cfi_table[0x17] = 0x00; | |
568 | pfl->cfi_table[0x18] = 0x00; | |
569 | /* Alternate extended table (none) */ | |
570 | pfl->cfi_table[0x19] = 0x00; | |
571 | pfl->cfi_table[0x1A] = 0x00; | |
572 | /* Vcc min */ | |
573 | pfl->cfi_table[0x1B] = 0x27; | |
574 | /* Vcc max */ | |
575 | pfl->cfi_table[0x1C] = 0x36; | |
576 | /* Vpp min (no Vpp pin) */ | |
577 | pfl->cfi_table[0x1D] = 0x00; | |
578 | /* Vpp max (no Vpp pin) */ | |
579 | pfl->cfi_table[0x1E] = 0x00; | |
9ac45b88 | 580 | /* Timeout per single byte/word write (128 ms) */ |
29133e9a | 581 | pfl->cfi_table[0x1F] = 0x07; |
78556820 EI |
582 | /* Timeout for min size buffer write (NA) */ |
583 | pfl->cfi_table[0x20] = 0x00; | |
29133e9a FB |
584 | /* Typical timeout for block erase (512 ms) */ |
585 | pfl->cfi_table[0x21] = 0x09; | |
586 | /* Typical timeout for full chip erase (4096 ms) */ | |
587 | pfl->cfi_table[0x22] = 0x0C; | |
588 | /* Reserved */ | |
589 | pfl->cfi_table[0x23] = 0x01; | |
78556820 EI |
590 | /* Max timeout for buffer write (NA) */ |
591 | pfl->cfi_table[0x24] = 0x00; | |
29133e9a FB |
592 | /* Max timeout for block erase */ |
593 | pfl->cfi_table[0x25] = 0x0A; | |
594 | /* Max timeout for chip erase */ | |
595 | pfl->cfi_table[0x26] = 0x0D; | |
596 | /* Device size */ | |
1eb27d69 | 597 | pfl->cfi_table[0x27] = ctz32(pfl->chip_len); |
29133e9a FB |
598 | /* Flash device interface (8 & 16 bits) */ |
599 | pfl->cfi_table[0x28] = 0x02; | |
600 | pfl->cfi_table[0x29] = 0x00; | |
601 | /* Max number of bytes in multi-bytes write */ | |
95d1f3ed JM |
602 | /* XXX: disable buffered write as it's not supported */ |
603 | // pfl->cfi_table[0x2A] = 0x05; | |
604 | pfl->cfi_table[0x2A] = 0x00; | |
29133e9a FB |
605 | pfl->cfi_table[0x2B] = 0x00; |
606 | /* Number of erase block regions (uniform) */ | |
607 | pfl->cfi_table[0x2C] = 0x01; | |
608 | /* Erase block region 1 */ | |
368a354f PC |
609 | pfl->cfi_table[0x2D] = pfl->nb_blocs - 1; |
610 | pfl->cfi_table[0x2E] = (pfl->nb_blocs - 1) >> 8; | |
611 | pfl->cfi_table[0x2F] = pfl->sector_len >> 8; | |
612 | pfl->cfi_table[0x30] = pfl->sector_len >> 16; | |
d6874c83 | 613 | assert(0x30 < pri_ofs); |
29133e9a | 614 | |
78556820 | 615 | /* Extended */ |
d6874c83 PMD |
616 | pfl->cfi_table[0x00 + pri_ofs] = 'P'; |
617 | pfl->cfi_table[0x01 + pri_ofs] = 'R'; | |
618 | pfl->cfi_table[0x02 + pri_ofs] = 'I'; | |
78556820 | 619 | |
9ac45b88 | 620 | /* Extended version 1.0 */ |
d6874c83 PMD |
621 | pfl->cfi_table[0x03 + pri_ofs] = '1'; |
622 | pfl->cfi_table[0x04 + pri_ofs] = '0'; | |
78556820 | 623 | |
9ac45b88 | 624 | /* Address sensitive unlock required. */ |
d6874c83 | 625 | pfl->cfi_table[0x05 + pri_ofs] = 0x00; |
9ac45b88 | 626 | /* Erase suspend not supported. */ |
d6874c83 | 627 | pfl->cfi_table[0x06 + pri_ofs] = 0x00; |
9ac45b88 | 628 | /* Sector protect not supported. */ |
d6874c83 | 629 | pfl->cfi_table[0x07 + pri_ofs] = 0x00; |
9ac45b88 | 630 | /* Temporary sector unprotect not supported. */ |
d6874c83 | 631 | pfl->cfi_table[0x08 + pri_ofs] = 0x00; |
78556820 | 632 | |
9ac45b88 | 633 | /* Sector protect/unprotect scheme. */ |
d6874c83 | 634 | pfl->cfi_table[0x09 + pri_ofs] = 0x00; |
78556820 | 635 | |
9ac45b88 | 636 | /* Simultaneous operation not supported. */ |
d6874c83 | 637 | pfl->cfi_table[0x0a + pri_ofs] = 0x00; |
9ac45b88 | 638 | /* Burst mode not supported. */ |
d6874c83 PMD |
639 | pfl->cfi_table[0x0b + pri_ofs] = 0x00; |
640 | assert(0x0b + pri_ofs < ARRAY_SIZE(pfl->cfi_table)); | |
368a354f PC |
641 | } |
642 | ||
643 | static Property pflash_cfi02_properties[] = { | |
16434065 MA |
644 | DEFINE_PROP_DRIVE("drive", PFlashCFI02, blk), |
645 | DEFINE_PROP_UINT32("num-blocks", PFlashCFI02, nb_blocs, 0), | |
646 | DEFINE_PROP_UINT32("sector-length", PFlashCFI02, sector_len, 0), | |
647 | DEFINE_PROP_UINT8("width", PFlashCFI02, width, 0), | |
648 | DEFINE_PROP_UINT8("mappings", PFlashCFI02, mappings, 0), | |
649 | DEFINE_PROP_UINT8("big-endian", PFlashCFI02, be, 0), | |
650 | DEFINE_PROP_UINT16("id0", PFlashCFI02, ident0, 0), | |
651 | DEFINE_PROP_UINT16("id1", PFlashCFI02, ident1, 0), | |
652 | DEFINE_PROP_UINT16("id2", PFlashCFI02, ident2, 0), | |
653 | DEFINE_PROP_UINT16("id3", PFlashCFI02, ident3, 0), | |
654 | DEFINE_PROP_UINT16("unlock-addr0", PFlashCFI02, unlock_addr0, 0), | |
655 | DEFINE_PROP_UINT16("unlock-addr1", PFlashCFI02, unlock_addr1, 0), | |
656 | DEFINE_PROP_STRING("name", PFlashCFI02, name), | |
368a354f PC |
657 | DEFINE_PROP_END_OF_LIST(), |
658 | }; | |
659 | ||
d80cf1eb SC |
660 | static void pflash_cfi02_unrealize(DeviceState *dev, Error **errp) |
661 | { | |
e7b62741 | 662 | PFlashCFI02 *pfl = PFLASH_CFI02(dev); |
d80cf1eb SC |
663 | timer_del(&pfl->timer); |
664 | } | |
665 | ||
368a354f PC |
666 | static void pflash_cfi02_class_init(ObjectClass *klass, void *data) |
667 | { | |
668 | DeviceClass *dc = DEVICE_CLASS(klass); | |
368a354f | 669 | |
da3bd642 | 670 | dc->realize = pflash_cfi02_realize; |
d80cf1eb | 671 | dc->unrealize = pflash_cfi02_unrealize; |
368a354f | 672 | dc->props = pflash_cfi02_properties; |
df6f9318 | 673 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
368a354f PC |
674 | } |
675 | ||
676 | static const TypeInfo pflash_cfi02_info = { | |
e7b62741 | 677 | .name = TYPE_PFLASH_CFI02, |
368a354f | 678 | .parent = TYPE_SYS_BUS_DEVICE, |
16434065 | 679 | .instance_size = sizeof(PFlashCFI02), |
368a354f PC |
680 | .class_init = pflash_cfi02_class_init, |
681 | }; | |
682 | ||
683 | static void pflash_cfi02_register_types(void) | |
684 | { | |
685 | type_register_static(&pflash_cfi02_info); | |
686 | } | |
687 | ||
688 | type_init(pflash_cfi02_register_types) | |
689 | ||
16434065 | 690 | PFlashCFI02 *pflash_cfi02_register(hwaddr base, |
940d5b13 | 691 | const char *name, |
16434065 MA |
692 | hwaddr size, |
693 | BlockBackend *blk, | |
ce14710f | 694 | uint32_t sector_len, |
16434065 MA |
695 | int nb_mappings, int width, |
696 | uint16_t id0, uint16_t id1, | |
697 | uint16_t id2, uint16_t id3, | |
698 | uint16_t unlock_addr0, | |
699 | uint16_t unlock_addr1, | |
700 | int be) | |
368a354f | 701 | { |
e7b62741 | 702 | DeviceState *dev = qdev_create(NULL, TYPE_PFLASH_CFI02); |
368a354f | 703 | |
9b3d111a MA |
704 | if (blk) { |
705 | qdev_prop_set_drive(dev, "drive", blk, &error_abort); | |
368a354f | 706 | } |
ce14710f MA |
707 | assert(size % sector_len == 0); |
708 | qdev_prop_set_uint32(dev, "num-blocks", size / sector_len); | |
368a354f PC |
709 | qdev_prop_set_uint32(dev, "sector-length", sector_len); |
710 | qdev_prop_set_uint8(dev, "width", width); | |
711 | qdev_prop_set_uint8(dev, "mappings", nb_mappings); | |
712 | qdev_prop_set_uint8(dev, "big-endian", !!be); | |
713 | qdev_prop_set_uint16(dev, "id0", id0); | |
714 | qdev_prop_set_uint16(dev, "id1", id1); | |
715 | qdev_prop_set_uint16(dev, "id2", id2); | |
716 | qdev_prop_set_uint16(dev, "id3", id3); | |
717 | qdev_prop_set_uint16(dev, "unlock-addr0", unlock_addr0); | |
718 | qdev_prop_set_uint16(dev, "unlock-addr1", unlock_addr1); | |
719 | qdev_prop_set_string(dev, "name", name); | |
720 | qdev_init_nofail(dev); | |
721 | ||
3509c396 | 722 | sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base); |
e7b62741 | 723 | return PFLASH_CFI02(dev); |
29133e9a | 724 | } |