]>
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. | |
29133e9a | 32 | * It does not implement software data protection as found in many real chips |
29133e9a FB |
33 | */ |
34 | ||
80c71a24 | 35 | #include "qemu/osdep.h" |
83c9f4ca | 36 | #include "hw/hw.h" |
06f15217 | 37 | #include "hw/block/block.h" |
0d09e41a | 38 | #include "hw/block/flash.h" |
da34e65c | 39 | #include "qapi/error.h" |
ddb6f225 | 40 | #include "qemu/bitmap.h" |
1de7afc9 | 41 | #include "qemu/timer.h" |
4be74634 | 42 | #include "sysemu/block-backend.h" |
1de7afc9 | 43 | #include "qemu/host-utils.h" |
0b8fa32f | 44 | #include "qemu/module.h" |
83c9f4ca | 45 | #include "hw/sysbus.h" |
13019f1f | 46 | #include "trace.h" |
29133e9a | 47 | |
6536987f | 48 | #define PFLASH_DEBUG false |
ec9ea489 PC |
49 | #define DPRINTF(fmt, ...) \ |
50 | do { \ | |
6536987f PMD |
51 | if (PFLASH_DEBUG) { \ |
52 | fprintf(stderr, "PFLASH: " fmt, ## __VA_ARGS__); \ | |
53 | } \ | |
29133e9a | 54 | } while (0) |
29133e9a | 55 | |
661bfc80 JK |
56 | #define PFLASH_LAZY_ROMD_THRESHOLD 42 |
57 | ||
64659053 SC |
58 | /* |
59 | * The size of the cfi_table indirectly depends on this and the start of the | |
60 | * PRI table directly depends on it. 4 is the maximum size (and also what | |
61 | * seems common) without changing the PRT table address. | |
62 | */ | |
63 | #define PFLASH_MAX_ERASE_REGIONS 4 | |
64 | ||
aeaf6c20 PMD |
65 | /* Special write cycles for CFI queries. */ |
66 | enum { | |
67 | WCYCLE_CFI = 7, | |
46fb7809 | 68 | WCYCLE_AUTOSELECT_CFI = 8, |
aeaf6c20 PMD |
69 | }; |
70 | ||
16434065 | 71 | struct PFlashCFI02 { |
3509c396 HT |
72 | /*< private >*/ |
73 | SysBusDevice parent_obj; | |
74 | /*< public >*/ | |
75 | ||
4be74634 | 76 | BlockBackend *blk; |
64659053 SC |
77 | uint32_t uniform_nb_blocs; |
78 | uint32_t uniform_sector_len; | |
ddb6f225 | 79 | uint32_t total_sectors; |
64659053 SC |
80 | uint32_t nb_blocs[PFLASH_MAX_ERASE_REGIONS]; |
81 | uint32_t sector_len[PFLASH_MAX_ERASE_REGIONS]; | |
4fbd24ba | 82 | uint32_t chip_len; |
368a354f PC |
83 | uint8_t mappings; |
84 | uint8_t width; | |
85 | uint8_t be; | |
29133e9a FB |
86 | int wcycle; /* if 0, the flash is read normally */ |
87 | int bypass; | |
88 | int ro; | |
89 | uint8_t cmd; | |
90 | uint8_t status; | |
368a354f PC |
91 | /* FIXME: implement array device properties */ |
92 | uint16_t ident0; | |
93 | uint16_t ident1; | |
94 | uint16_t ident2; | |
95 | uint16_t ident3; | |
96 | uint16_t unlock_addr0; | |
97 | uint16_t unlock_addr1; | |
64659053 | 98 | uint8_t cfi_table[0x4d]; |
d80cf1eb | 99 | QEMUTimer timer; |
cfe5f011 AK |
100 | /* The device replicates the flash memory across its memory space. Emulate |
101 | * that by having a container (.mem) filled with an array of aliases | |
102 | * (.mem_mappings) pointing to the flash memory (.orig_mem). | |
103 | */ | |
104 | MemoryRegion mem; | |
105 | MemoryRegion *mem_mappings; /* array; one per mapping */ | |
106 | MemoryRegion orig_mem; | |
9c9bb6c8 | 107 | int rom_mode; |
661bfc80 | 108 | int read_counter; /* used for lazy switch-back to rom mode */ |
a50547ac | 109 | int sectors_to_erase; |
ddb6f225 SC |
110 | uint64_t erase_time_remaining; |
111 | unsigned long *sector_erase_map; | |
368a354f | 112 | char *name; |
29133e9a FB |
113 | void *storage; |
114 | }; | |
115 | ||
1d311e73 PMD |
116 | /* |
117 | * Toggle status bit DQ7. | |
118 | */ | |
119 | static inline void toggle_dq7(PFlashCFI02 *pfl) | |
120 | { | |
121 | pfl->status ^= 0x80; | |
122 | } | |
123 | ||
124 | /* | |
125 | * Set status bit DQ7 to bit 7 of value. | |
126 | */ | |
127 | static inline void set_dq7(PFlashCFI02 *pfl, uint8_t value) | |
128 | { | |
129 | pfl->status &= 0x7F; | |
130 | pfl->status |= value & 0x80; | |
131 | } | |
132 | ||
133 | /* | |
134 | * Toggle status bit DQ6. | |
135 | */ | |
136 | static inline void toggle_dq6(PFlashCFI02 *pfl) | |
137 | { | |
138 | pfl->status ^= 0x40; | |
139 | } | |
140 | ||
a50547ac SC |
141 | /* |
142 | * Turn on DQ3. | |
143 | */ | |
144 | static inline void assert_dq3(PFlashCFI02 *pfl) | |
145 | { | |
146 | pfl->status |= 0x08; | |
147 | } | |
148 | ||
149 | /* | |
150 | * Turn off DQ3. | |
151 | */ | |
152 | static inline void reset_dq3(PFlashCFI02 *pfl) | |
153 | { | |
154 | pfl->status &= ~0x08; | |
155 | } | |
156 | ||
ddb6f225 SC |
157 | /* |
158 | * Toggle status bit DQ2. | |
159 | */ | |
160 | static inline void toggle_dq2(PFlashCFI02 *pfl) | |
161 | { | |
162 | pfl->status ^= 0x04; | |
163 | } | |
164 | ||
cfe5f011 AK |
165 | /* |
166 | * Set up replicated mappings of the same region. | |
167 | */ | |
16434065 | 168 | static void pflash_setup_mappings(PFlashCFI02 *pfl) |
c8a50e59 | 169 | { |
cfe5f011 | 170 | unsigned i; |
a8170e5e | 171 | hwaddr size = memory_region_size(&pfl->orig_mem); |
cfe5f011 | 172 | |
2d256e6f | 173 | memory_region_init(&pfl->mem, OBJECT(pfl), "pflash", pfl->mappings * size); |
cfe5f011 AK |
174 | pfl->mem_mappings = g_new(MemoryRegion, pfl->mappings); |
175 | for (i = 0; i < pfl->mappings; ++i) { | |
2d256e6f PB |
176 | memory_region_init_alias(&pfl->mem_mappings[i], OBJECT(pfl), |
177 | "pflash-alias", &pfl->orig_mem, 0, size); | |
cfe5f011 AK |
178 | memory_region_add_subregion(&pfl->mem, i * size, &pfl->mem_mappings[i]); |
179 | } | |
180 | } | |
01e0451a | 181 | |
16434065 | 182 | static void pflash_register_memory(PFlashCFI02 *pfl, int rom_mode) |
cfe5f011 | 183 | { |
5f9a5ea1 | 184 | memory_region_rom_device_set_romd(&pfl->orig_mem, rom_mode); |
bda254da | 185 | pfl->rom_mode = rom_mode; |
4fbd24ba AZ |
186 | } |
187 | ||
102f0f79 PMD |
188 | static size_t pflash_regions_count(PFlashCFI02 *pfl) |
189 | { | |
190 | return pfl->cfi_table[0x2c]; | |
191 | } | |
192 | ||
ddb6f225 SC |
193 | /* |
194 | * Returns the time it takes to erase the number of sectors scheduled for | |
195 | * erasure based on CFI address 0x21 which is "Typical timeout per individual | |
196 | * block erase 2^N ms." | |
197 | */ | |
198 | static uint64_t pflash_erase_time(PFlashCFI02 *pfl) | |
199 | { | |
200 | /* | |
201 | * If there are no sectors to erase (which can happen if all of the sectors | |
202 | * to be erased are protected), then erase takes 100 us. Protected sectors | |
203 | * aren't supported so this should never happen. | |
204 | */ | |
205 | return ((1ULL << pfl->cfi_table[0x21]) * pfl->sectors_to_erase) * SCALE_US; | |
206 | } | |
207 | ||
208 | /* | |
209 | * Returns true if the device is currently in erase suspend mode. | |
210 | */ | |
211 | static inline bool pflash_erase_suspend_mode(PFlashCFI02 *pfl) | |
212 | { | |
213 | return pfl->erase_time_remaining > 0; | |
214 | } | |
215 | ||
a50547ac | 216 | static void pflash_timer(void *opaque) |
29133e9a | 217 | { |
16434065 | 218 | PFlashCFI02 *pfl = opaque; |
29133e9a | 219 | |
13019f1f | 220 | trace_pflash_timer_expired(pfl->cmd); |
a50547ac SC |
221 | if (pfl->cmd == 0x30) { |
222 | /* | |
223 | * Sector erase. If DQ3 is 0 when the timer expires, then the 50 | |
224 | * us erase timeout has expired so we need to start the timer for the | |
225 | * sector erase algorithm. Otherwise, the erase completed and we should | |
226 | * go back to read array mode. | |
227 | */ | |
228 | if ((pfl->status & 0x08) == 0) { | |
229 | assert_dq3(pfl); | |
ddb6f225 | 230 | uint64_t timeout = pflash_erase_time(pfl); |
a50547ac SC |
231 | timer_mod(&pfl->timer, |
232 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + timeout); | |
233 | DPRINTF("%s: erase timeout fired; erasing %d sectors\n", | |
234 | __func__, pfl->sectors_to_erase); | |
235 | return; | |
236 | } | |
237 | DPRINTF("%s: sector erase complete\n", __func__); | |
ddb6f225 | 238 | bitmap_zero(pfl->sector_erase_map, pfl->total_sectors); |
a50547ac SC |
239 | pfl->sectors_to_erase = 0; |
240 | reset_dq3(pfl); | |
241 | } | |
242 | ||
29133e9a | 243 | /* Reset flash */ |
1d311e73 | 244 | toggle_dq7(pfl); |
29133e9a FB |
245 | if (pfl->bypass) { |
246 | pfl->wcycle = 2; | |
247 | } else { | |
4fbd24ba | 248 | pflash_register_memory(pfl, 1); |
29133e9a FB |
249 | pfl->wcycle = 0; |
250 | } | |
251 | pfl->cmd = 0; | |
252 | } | |
253 | ||
06e8b8e3 PMD |
254 | /* |
255 | * Read data from flash. | |
256 | */ | |
257 | static uint64_t pflash_data_read(PFlashCFI02 *pfl, hwaddr offset, | |
258 | unsigned int width) | |
259 | { | |
260 | uint8_t *p = (uint8_t *)pfl->storage + offset; | |
261 | uint64_t ret = pfl->be ? ldn_be_p(p, width) : ldn_le_p(p, width); | |
262 | trace_pflash_data_read(offset, width << 1, ret); | |
263 | return ret; | |
264 | } | |
265 | ||
ddb6f225 SC |
266 | typedef struct { |
267 | uint32_t len; | |
268 | uint32_t num; | |
269 | } SectorInfo; | |
270 | ||
64659053 SC |
271 | /* |
272 | * offset should be a byte offset of the QEMU device and _not_ a device | |
273 | * offset. | |
274 | */ | |
ddb6f225 | 275 | static SectorInfo pflash_sector_info(PFlashCFI02 *pfl, hwaddr offset) |
64659053 SC |
276 | { |
277 | assert(offset < pfl->chip_len); | |
64659053 | 278 | hwaddr addr = 0; |
ddb6f225 | 279 | uint32_t sector_num = 0; |
102f0f79 | 280 | for (int i = 0; i < pflash_regions_count(pfl); ++i) { |
64659053 SC |
281 | uint64_t region_size = (uint64_t)pfl->nb_blocs[i] * pfl->sector_len[i]; |
282 | if (addr <= offset && offset < addr + region_size) { | |
ddb6f225 SC |
283 | return (SectorInfo) { |
284 | .len = pfl->sector_len[i], | |
285 | .num = sector_num + (offset - addr) / pfl->sector_len[i], | |
286 | }; | |
64659053 | 287 | } |
ddb6f225 | 288 | sector_num += pfl->nb_blocs[i]; |
64659053 SC |
289 | addr += region_size; |
290 | } | |
291 | abort(); | |
292 | } | |
293 | ||
ddb6f225 SC |
294 | /* |
295 | * Returns true if the offset refers to a flash sector that is currently being | |
296 | * erased. | |
297 | */ | |
298 | static bool pflash_sector_is_erasing(PFlashCFI02 *pfl, hwaddr offset) | |
299 | { | |
300 | long sector_num = pflash_sector_info(pfl, offset).num; | |
301 | return test_bit(sector_num, pfl->sector_erase_map); | |
302 | } | |
303 | ||
aff498cf | 304 | static uint64_t pflash_read(void *opaque, hwaddr offset, unsigned int width) |
29133e9a | 305 | { |
aff498cf | 306 | PFlashCFI02 *pfl = opaque; |
a8170e5e | 307 | hwaddr boff; |
aff498cf | 308 | uint64_t ret; |
29133e9a | 309 | |
29133e9a | 310 | ret = -1; |
661bfc80 JK |
311 | /* Lazy reset to ROMD mode after a certain amount of read accesses */ |
312 | if (!pfl->rom_mode && pfl->wcycle == 0 && | |
313 | ++pfl->read_counter > PFLASH_LAZY_ROMD_THRESHOLD) { | |
314 | pflash_register_memory(pfl, 1); | |
0f459d16 | 315 | } |
4fbd24ba | 316 | offset &= pfl->chip_len - 1; |
29133e9a | 317 | boff = offset & 0xFF; |
64659053 | 318 | if (pfl->width == 2) { |
29133e9a | 319 | boff = boff >> 1; |
64659053 | 320 | } |
29133e9a FB |
321 | switch (pfl->cmd) { |
322 | default: | |
323 | /* This should never happen : reset state & treat it as a read*/ | |
324 | DPRINTF("%s: unknown command state: %x\n", __func__, pfl->cmd); | |
325 | pfl->wcycle = 0; | |
326 | pfl->cmd = 0; | |
30954850 | 327 | /* fall through to the read code */ |
b0349937 | 328 | case 0x80: /* Erase (unlock) */ |
29133e9a FB |
329 | /* We accept reads during second unlock sequence... */ |
330 | case 0x00: | |
ddb6f225 SC |
331 | if (pflash_erase_suspend_mode(pfl) && |
332 | pflash_sector_is_erasing(pfl, offset)) { | |
333 | /* Toggle bit 2, but not 6. */ | |
334 | toggle_dq2(pfl); | |
335 | /* Status register read */ | |
336 | ret = pfl->status; | |
337 | DPRINTF("%s: status %" PRIx64 "\n", __func__, ret); | |
338 | break; | |
339 | } | |
29133e9a | 340 | /* Flash area read */ |
06e8b8e3 | 341 | ret = pflash_data_read(pfl, offset, width); |
29133e9a | 342 | break; |
b0349937 | 343 | case 0x90: /* flash ID read */ |
29133e9a FB |
344 | switch (boff) { |
345 | case 0x00: | |
346 | case 0x01: | |
368a354f | 347 | ret = boff & 0x01 ? pfl->ident1 : pfl->ident0; |
29133e9a FB |
348 | break; |
349 | case 0x02: | |
350 | ret = 0x00; /* Pretend all sectors are unprotected */ | |
351 | break; | |
352 | case 0x0E: | |
353 | case 0x0F: | |
368a354f | 354 | ret = boff & 0x01 ? pfl->ident3 : pfl->ident2; |
7f7bdcaf PMD |
355 | if (ret != (uint8_t)-1) { |
356 | break; | |
368a354f | 357 | } |
7f7bdcaf | 358 | /* Fall through to data read. */ |
29133e9a | 359 | default: |
06e8b8e3 | 360 | ret = pflash_data_read(pfl, offset, width); |
29133e9a | 361 | } |
aff498cf | 362 | DPRINTF("%s: ID " TARGET_FMT_plx " %" PRIx64 "\n", __func__, boff, ret); |
29133e9a | 363 | break; |
b0349937 PMD |
364 | case 0x10: /* Chip Erase */ |
365 | case 0x30: /* Sector Erase */ | |
ddb6f225 SC |
366 | /* Toggle bit 2 during erase, but not program. */ |
367 | toggle_dq2(pfl); | |
b0349937 | 368 | case 0xA0: /* Program */ |
ddb6f225 SC |
369 | /* Toggle bit 6 */ |
370 | toggle_dq6(pfl); | |
29133e9a FB |
371 | /* Status register read */ |
372 | ret = pfl->status; | |
aff498cf | 373 | DPRINTF("%s: status %" PRIx64 "\n", __func__, ret); |
29133e9a FB |
374 | break; |
375 | case 0x98: | |
376 | /* CFI query mode */ | |
07c13a71 | 377 | if (boff < sizeof(pfl->cfi_table)) { |
29133e9a | 378 | ret = pfl->cfi_table[boff]; |
07c13a71 PMD |
379 | } else { |
380 | ret = 0; | |
381 | } | |
29133e9a FB |
382 | break; |
383 | } | |
e8aa2d95 | 384 | trace_pflash_io_read(offset, width, width << 1, ret, pfl->cmd, pfl->wcycle); |
29133e9a FB |
385 | |
386 | return ret; | |
387 | } | |
388 | ||
389 | /* update flash content on disk */ | |
aff498cf | 390 | static void pflash_update(PFlashCFI02 *pfl, int offset, int size) |
29133e9a FB |
391 | { |
392 | int offset_end; | |
4be74634 | 393 | if (pfl->blk) { |
29133e9a | 394 | offset_end = offset + size; |
098e732d EB |
395 | /* widen to sector boundaries */ |
396 | offset = QEMU_ALIGN_DOWN(offset, BDRV_SECTOR_SIZE); | |
397 | offset_end = QEMU_ALIGN_UP(offset_end, BDRV_SECTOR_SIZE); | |
398 | blk_pwrite(pfl->blk, offset, pfl->storage + offset, | |
399 | offset_end - offset, 0); | |
29133e9a FB |
400 | } |
401 | } | |
402 | ||
a50547ac SC |
403 | static void pflash_sector_erase(PFlashCFI02 *pfl, hwaddr offset) |
404 | { | |
ddb6f225 SC |
405 | SectorInfo sector_info = pflash_sector_info(pfl, offset); |
406 | uint64_t sector_len = sector_info.len; | |
a50547ac SC |
407 | offset &= ~(sector_len - 1); |
408 | DPRINTF("%s: start sector erase at %0*" PRIx64 "-%0*" PRIx64 "\n", | |
409 | __func__, pfl->width * 2, offset, | |
410 | pfl->width * 2, offset + sector_len - 1); | |
411 | if (!pfl->ro) { | |
412 | uint8_t *p = pfl->storage; | |
413 | memset(p + offset, 0xff, sector_len); | |
414 | pflash_update(pfl, offset, sector_len); | |
415 | } | |
416 | set_dq7(pfl, 0x00); | |
417 | ++pfl->sectors_to_erase; | |
ddb6f225 | 418 | set_bit(sector_info.num, pfl->sector_erase_map); |
a50547ac SC |
419 | /* Set (or reset) the 50 us timer for additional erase commands. */ |
420 | timer_mod(&pfl->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 50000); | |
421 | } | |
422 | ||
aff498cf PMD |
423 | static void pflash_write(void *opaque, hwaddr offset, uint64_t value, |
424 | unsigned int width) | |
29133e9a | 425 | { |
aff498cf | 426 | PFlashCFI02 *pfl = opaque; |
a8170e5e | 427 | hwaddr boff; |
29133e9a FB |
428 | uint8_t *p; |
429 | uint8_t cmd; | |
430 | ||
e8aa2d95 | 431 | trace_pflash_io_write(offset, width, width << 1, value, pfl->wcycle); |
95d1f3ed | 432 | cmd = value; |
8a508e70 | 433 | if (pfl->cmd != 0xA0) { |
a9791042 SC |
434 | /* Reset does nothing during chip erase and sector erase. */ |
435 | if (cmd == 0xF0 && pfl->cmd != 0x10 && pfl->cmd != 0x30) { | |
46fb7809 SC |
436 | if (pfl->wcycle == WCYCLE_AUTOSELECT_CFI) { |
437 | /* Return to autoselect mode. */ | |
438 | pfl->wcycle = 3; | |
439 | pfl->cmd = 0x90; | |
440 | return; | |
441 | } | |
8a508e70 PMD |
442 | goto reset_flash; |
443 | } | |
95d1f3ed | 444 | } |
4fbd24ba | 445 | offset &= pfl->chip_len - 1; |
3b46e624 | 446 | |
6682bc1e | 447 | boff = offset; |
64659053 | 448 | if (pfl->width == 2) { |
29133e9a | 449 | boff = boff >> 1; |
64659053 | 450 | } |
6682bc1e SC |
451 | /* Only the least-significant 11 bits are used in most cases. */ |
452 | boff &= 0x7FF; | |
29133e9a FB |
453 | switch (pfl->wcycle) { |
454 | case 0: | |
9c9bb6c8 AZ |
455 | /* Set the device in I/O access mode if required */ |
456 | if (pfl->rom_mode) | |
457 | pflash_register_memory(pfl, 0); | |
661bfc80 | 458 | pfl->read_counter = 0; |
29133e9a FB |
459 | /* We're in read mode */ |
460 | check_unlock0: | |
461 | if (boff == 0x55 && cmd == 0x98) { | |
29133e9a | 462 | /* Enter CFI query mode */ |
aeaf6c20 | 463 | pfl->wcycle = WCYCLE_CFI; |
29133e9a FB |
464 | pfl->cmd = 0x98; |
465 | return; | |
466 | } | |
ddb6f225 | 467 | /* Handle erase resume in erase suspend mode, otherwise reset. */ |
b0349937 | 468 | if (cmd == 0x30) { /* Erase Resume */ |
ddb6f225 SC |
469 | if (pflash_erase_suspend_mode(pfl)) { |
470 | /* Resume the erase. */ | |
471 | timer_mod(&pfl->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + | |
472 | pfl->erase_time_remaining); | |
473 | pfl->erase_time_remaining = 0; | |
474 | pfl->wcycle = 6; | |
475 | pfl->cmd = 0x30; | |
476 | set_dq7(pfl, 0x00); | |
477 | assert_dq3(pfl); | |
478 | return; | |
479 | } | |
480 | goto reset_flash; | |
481 | } | |
482 | /* Ignore erase suspend. */ | |
b0349937 | 483 | if (cmd == 0xB0) { /* Erase Suspend */ |
ddb6f225 SC |
484 | return; |
485 | } | |
368a354f | 486 | if (boff != pfl->unlock_addr0 || cmd != 0xAA) { |
f8be67ee | 487 | DPRINTF("%s: unlock0 failed " TARGET_FMT_plx " %02x %04x\n", |
368a354f | 488 | __func__, boff, cmd, pfl->unlock_addr0); |
29133e9a FB |
489 | goto reset_flash; |
490 | } | |
491 | DPRINTF("%s: unlock sequence started\n", __func__); | |
492 | break; | |
493 | case 1: | |
494 | /* We started an unlock sequence */ | |
495 | check_unlock1: | |
368a354f | 496 | if (boff != pfl->unlock_addr1 || cmd != 0x55) { |
f8be67ee | 497 | DPRINTF("%s: unlock1 failed " TARGET_FMT_plx " %02x\n", __func__, |
e96efcfc | 498 | boff, cmd); |
29133e9a FB |
499 | goto reset_flash; |
500 | } | |
501 | DPRINTF("%s: unlock sequence done\n", __func__); | |
502 | break; | |
503 | case 2: | |
504 | /* We finished an unlock sequence */ | |
368a354f | 505 | if (!pfl->bypass && boff != pfl->unlock_addr0) { |
f8be67ee | 506 | DPRINTF("%s: command failed " TARGET_FMT_plx " %02x\n", __func__, |
e96efcfc | 507 | boff, cmd); |
29133e9a FB |
508 | goto reset_flash; |
509 | } | |
510 | switch (cmd) { | |
511 | case 0x20: | |
512 | pfl->bypass = 1; | |
513 | goto do_bypass; | |
b0349937 PMD |
514 | case 0x80: /* Erase */ |
515 | case 0x90: /* Autoselect */ | |
516 | case 0xA0: /* Program */ | |
29133e9a FB |
517 | pfl->cmd = cmd; |
518 | DPRINTF("%s: starting command %02x\n", __func__, cmd); | |
519 | break; | |
520 | default: | |
521 | DPRINTF("%s: unknown command %02x\n", __func__, cmd); | |
522 | goto reset_flash; | |
523 | } | |
524 | break; | |
525 | case 3: | |
526 | switch (pfl->cmd) { | |
b0349937 | 527 | case 0x80: /* Erase */ |
29133e9a FB |
528 | /* We need another unlock sequence */ |
529 | goto check_unlock0; | |
b0349937 | 530 | case 0xA0: /* Program */ |
ddb6f225 SC |
531 | if (pflash_erase_suspend_mode(pfl) && |
532 | pflash_sector_is_erasing(pfl, offset)) { | |
533 | /* Ignore writes to erasing sectors. */ | |
534 | if (pfl->bypass) { | |
535 | goto do_bypass; | |
536 | } | |
537 | goto reset_flash; | |
538 | } | |
c1474acd | 539 | trace_pflash_data_write(offset, width << 1, value, 0); |
de8efe8f | 540 | if (!pfl->ro) { |
c3d25271 PMD |
541 | p = (uint8_t *)pfl->storage + offset; |
542 | if (pfl->be) { | |
543 | uint64_t current = ldn_be_p(p, width); | |
544 | stn_be_p(p, width, current & value); | |
545 | } else { | |
546 | uint64_t current = ldn_le_p(p, width); | |
547 | stn_le_p(p, width, current & value); | |
5f9fc5ad | 548 | } |
c3d25271 | 549 | pflash_update(pfl, offset, width); |
29133e9a | 550 | } |
1d311e73 PMD |
551 | /* |
552 | * While programming, status bit DQ7 should hold the opposite | |
553 | * value from how it was programmed. | |
554 | */ | |
555 | set_dq7(pfl, ~value); | |
29133e9a FB |
556 | /* Let's pretend write is immediate */ |
557 | if (pfl->bypass) | |
558 | goto do_bypass; | |
559 | goto reset_flash; | |
b0349937 | 560 | case 0x90: /* Autoselect */ |
29133e9a FB |
561 | if (pfl->bypass && cmd == 0x00) { |
562 | /* Unlock bypass reset */ | |
563 | goto reset_flash; | |
564 | } | |
46fb7809 SC |
565 | /* |
566 | * We can enter CFI query mode from autoselect mode, but we must | |
567 | * return to autoselect mode after a reset. | |
568 | */ | |
569 | if (boff == 0x55 && cmd == 0x98) { | |
570 | /* Enter autoselect CFI query mode */ | |
571 | pfl->wcycle = WCYCLE_AUTOSELECT_CFI; | |
572 | pfl->cmd = 0x98; | |
573 | return; | |
574 | } | |
29133e9a FB |
575 | /* No break here */ |
576 | default: | |
577 | DPRINTF("%s: invalid write for command %02x\n", | |
578 | __func__, pfl->cmd); | |
579 | goto reset_flash; | |
580 | } | |
581 | case 4: | |
582 | switch (pfl->cmd) { | |
b0349937 | 583 | case 0xA0: /* Program */ |
a1c7273b | 584 | /* Ignore writes while flash data write is occurring */ |
29133e9a FB |
585 | /* As we suppose write is immediate, this should never happen */ |
586 | return; | |
b0349937 | 587 | case 0x80: /* Erase */ |
29133e9a FB |
588 | goto check_unlock1; |
589 | default: | |
590 | /* Should never happen */ | |
591 | DPRINTF("%s: invalid command state %02x (wc 4)\n", | |
592 | __func__, pfl->cmd); | |
593 | goto reset_flash; | |
594 | } | |
595 | break; | |
596 | case 5: | |
ddb6f225 SC |
597 | if (pflash_erase_suspend_mode(pfl)) { |
598 | /* Erasing is not supported in erase suspend mode. */ | |
599 | goto reset_flash; | |
600 | } | |
29133e9a | 601 | switch (cmd) { |
b0349937 | 602 | case 0x10: /* Chip Erase */ |
368a354f | 603 | if (boff != pfl->unlock_addr0) { |
f8be67ee | 604 | DPRINTF("%s: chip erase: invalid address " TARGET_FMT_plx "\n", |
29133e9a FB |
605 | __func__, offset); |
606 | goto reset_flash; | |
607 | } | |
608 | /* Chip erase */ | |
609 | DPRINTF("%s: start chip erase\n", __func__); | |
de8efe8f | 610 | if (!pfl->ro) { |
1eb27d69 | 611 | memset(pfl->storage, 0xff, pfl->chip_len); |
de8efe8f JJ |
612 | pflash_update(pfl, 0, pfl->chip_len); |
613 | } | |
1d311e73 | 614 | set_dq7(pfl, 0x00); |
80f2c625 | 615 | /* Wait the time specified at CFI address 0x22. */ |
d80cf1eb | 616 | timer_mod(&pfl->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
80f2c625 | 617 | (1ULL << pfl->cfi_table[0x22]) * SCALE_MS); |
29133e9a | 618 | break; |
b0349937 | 619 | case 0x30: /* Sector erase */ |
a50547ac | 620 | pflash_sector_erase(pfl, offset); |
29133e9a FB |
621 | break; |
622 | default: | |
623 | DPRINTF("%s: invalid command %02x (wc 5)\n", __func__, cmd); | |
624 | goto reset_flash; | |
625 | } | |
626 | pfl->cmd = cmd; | |
627 | break; | |
628 | case 6: | |
629 | switch (pfl->cmd) { | |
b0349937 | 630 | case 0x10: /* Chip Erase */ |
29133e9a FB |
631 | /* Ignore writes during chip erase */ |
632 | return; | |
b0349937 | 633 | case 0x30: /* Sector erase */ |
ddb6f225 SC |
634 | if (cmd == 0xB0) { |
635 | /* | |
636 | * If erase suspend happens during the erase timeout (so DQ3 is | |
637 | * 0), then the device suspends erasing immediately. Set the | |
638 | * remaining time to be the total time to erase. Otherwise, | |
639 | * there is a maximum amount of time it can take to enter | |
640 | * suspend mode. Let's ignore that and suspend immediately and | |
641 | * set the remaining time to the actual time remaining on the | |
642 | * timer. | |
643 | */ | |
644 | if ((pfl->status & 0x08) == 0) { | |
645 | pfl->erase_time_remaining = pflash_erase_time(pfl); | |
646 | } else { | |
647 | int64_t delta = timer_expire_time_ns(&pfl->timer) - | |
648 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
649 | /* Make sure we have a positive time remaining. */ | |
650 | pfl->erase_time_remaining = delta <= 0 ? 1 : delta; | |
651 | } | |
652 | reset_dq3(pfl); | |
653 | timer_del(&pfl->timer); | |
654 | pfl->wcycle = 0; | |
655 | pfl->cmd = 0; | |
656 | return; | |
657 | } | |
a50547ac SC |
658 | /* |
659 | * If DQ3 is 0, additional sector erase commands can be | |
660 | * written and anything else (other than an erase suspend) resets | |
661 | * the device. | |
662 | */ | |
663 | if ((pfl->status & 0x08) == 0) { | |
664 | if (cmd == 0x30) { | |
665 | pflash_sector_erase(pfl, offset); | |
666 | } else { | |
667 | goto reset_flash; | |
668 | } | |
669 | } | |
670 | /* Ignore writes during the actual erase. */ | |
29133e9a FB |
671 | return; |
672 | default: | |
673 | /* Should never happen */ | |
674 | DPRINTF("%s: invalid command state %02x (wc 6)\n", | |
675 | __func__, pfl->cmd); | |
676 | goto reset_flash; | |
677 | } | |
678 | break; | |
aeaf6c20 PMD |
679 | /* Special values for CFI queries */ |
680 | case WCYCLE_CFI: | |
46fb7809 | 681 | case WCYCLE_AUTOSELECT_CFI: |
29133e9a FB |
682 | DPRINTF("%s: invalid write in CFI query mode\n", __func__); |
683 | goto reset_flash; | |
684 | default: | |
685 | /* Should never happen */ | |
686 | DPRINTF("%s: invalid write state (wc 7)\n", __func__); | |
687 | goto reset_flash; | |
688 | } | |
689 | pfl->wcycle++; | |
690 | ||
691 | return; | |
692 | ||
693 | /* Reset flash */ | |
694 | reset_flash: | |
13019f1f | 695 | trace_pflash_reset(); |
29133e9a FB |
696 | pfl->bypass = 0; |
697 | pfl->wcycle = 0; | |
698 | pfl->cmd = 0; | |
699 | return; | |
700 | ||
701 | do_bypass: | |
702 | pfl->wcycle = 2; | |
703 | pfl->cmd = 0; | |
29133e9a FB |
704 | } |
705 | ||
aff498cf PMD |
706 | static const MemoryRegionOps pflash_cfi02_ops = { |
707 | .read = pflash_read, | |
708 | .write = pflash_write, | |
3ae0343d | 709 | .impl.max_access_size = 2, |
a4afb28d PM |
710 | .valid.min_access_size = 1, |
711 | .valid.max_access_size = 4, | |
cfe5f011 | 712 | .endianness = DEVICE_NATIVE_ENDIAN, |
29133e9a FB |
713 | }; |
714 | ||
da3bd642 | 715 | static void pflash_cfi02_realize(DeviceState *dev, Error **errp) |
29133e9a | 716 | { |
e7b62741 | 717 | PFlashCFI02 *pfl = PFLASH_CFI02(dev); |
d0e7605e | 718 | int ret; |
33e0eb52 | 719 | Error *local_err = NULL; |
29133e9a | 720 | |
64659053 | 721 | if (pfl->uniform_sector_len == 0 && pfl->sector_len[0] == 0) { |
8929fc3a ZY |
722 | error_setg(errp, "attribute \"sector-length\" not specified or zero."); |
723 | return; | |
724 | } | |
64659053 | 725 | if (pfl->uniform_nb_blocs == 0 && pfl->nb_blocs[0] == 0) { |
8929fc3a ZY |
726 | error_setg(errp, "attribute \"num-blocks\" not specified or zero."); |
727 | return; | |
728 | } | |
729 | if (pfl->name == NULL) { | |
730 | error_setg(errp, "attribute \"name\" not specified."); | |
731 | return; | |
732 | } | |
733 | ||
64659053 SC |
734 | int nb_regions; |
735 | pfl->chip_len = 0; | |
ddb6f225 | 736 | pfl->total_sectors = 0; |
64659053 SC |
737 | for (nb_regions = 0; nb_regions < PFLASH_MAX_ERASE_REGIONS; ++nb_regions) { |
738 | if (pfl->nb_blocs[nb_regions] == 0) { | |
739 | break; | |
740 | } | |
ddb6f225 | 741 | pfl->total_sectors += pfl->nb_blocs[nb_regions]; |
64659053 SC |
742 | uint64_t sector_len_per_device = pfl->sector_len[nb_regions]; |
743 | ||
744 | /* | |
745 | * The size of each flash sector must be a power of 2 and it must be | |
746 | * aligned at the same power of 2. | |
747 | */ | |
748 | if (sector_len_per_device & 0xff || | |
749 | sector_len_per_device >= (1 << 24) || | |
750 | !is_power_of_2(sector_len_per_device)) | |
751 | { | |
752 | error_setg(errp, "unsupported configuration: " | |
753 | "sector length[%d] per device = %" PRIx64 ".", | |
754 | nb_regions, sector_len_per_device); | |
755 | return; | |
756 | } | |
757 | if (pfl->chip_len & (sector_len_per_device - 1)) { | |
758 | error_setg(errp, "unsupported configuration: " | |
759 | "flash region %d not correctly aligned.", | |
760 | nb_regions); | |
761 | return; | |
762 | } | |
763 | ||
764 | pfl->chip_len += (uint64_t)pfl->sector_len[nb_regions] * | |
765 | pfl->nb_blocs[nb_regions]; | |
766 | } | |
767 | ||
768 | uint64_t uniform_len = (uint64_t)pfl->uniform_nb_blocs * | |
769 | pfl->uniform_sector_len; | |
770 | if (nb_regions == 0) { | |
771 | nb_regions = 1; | |
772 | pfl->nb_blocs[0] = pfl->uniform_nb_blocs; | |
773 | pfl->sector_len[0] = pfl->uniform_sector_len; | |
774 | pfl->chip_len = uniform_len; | |
ddb6f225 | 775 | pfl->total_sectors = pfl->uniform_nb_blocs; |
64659053 SC |
776 | } else if (uniform_len != 0 && uniform_len != pfl->chip_len) { |
777 | error_setg(errp, "\"num-blocks\"*\"sector-length\" " | |
778 | "different from \"num-blocks0\"*\'sector-length0\" + ... + " | |
779 | "\"num-blocks3\"*\"sector-length3\""); | |
780 | return; | |
781 | } | |
368a354f | 782 | |
aff498cf PMD |
783 | memory_region_init_rom_device(&pfl->orig_mem, OBJECT(pfl), |
784 | &pflash_cfi02_ops, pfl, pfl->name, | |
1eb27d69 | 785 | pfl->chip_len, &local_err); |
33e0eb52 HT |
786 | if (local_err) { |
787 | error_propagate(errp, local_err); | |
788 | return; | |
789 | } | |
790 | ||
cfe5f011 | 791 | pfl->storage = memory_region_get_ram_ptr(&pfl->orig_mem); |
a17c17a2 KW |
792 | |
793 | if (pfl->blk) { | |
794 | uint64_t perm; | |
795 | pfl->ro = blk_is_read_only(pfl->blk); | |
796 | perm = BLK_PERM_CONSISTENT_READ | (pfl->ro ? 0 : BLK_PERM_WRITE); | |
797 | ret = blk_set_perm(pfl->blk, perm, BLK_PERM_ALL, errp); | |
798 | if (ret < 0) { | |
799 | return; | |
800 | } | |
801 | } else { | |
802 | pfl->ro = 0; | |
803 | } | |
804 | ||
4be74634 | 805 | if (pfl->blk) { |
1eb27d69 PMD |
806 | if (!blk_check_size_and_read_all(pfl->blk, pfl->storage, |
807 | pfl->chip_len, errp)) { | |
da3bd642 | 808 | vmstate_unregister_ram(&pfl->orig_mem, DEVICE(pfl)); |
da3bd642 | 809 | return; |
d0e7605e | 810 | } |
29133e9a | 811 | } |
de8efe8f | 812 | |
6682bc1e SC |
813 | /* Only 11 bits are used in the comparison. */ |
814 | pfl->unlock_addr0 &= 0x7FF; | |
815 | pfl->unlock_addr1 &= 0x7FF; | |
816 | ||
ddb6f225 SC |
817 | /* Allocate memory for a bitmap for sectors being erased. */ |
818 | pfl->sector_erase_map = bitmap_new(pfl->total_sectors); | |
819 | ||
cfe5f011 AK |
820 | pflash_setup_mappings(pfl); |
821 | pfl->rom_mode = 1; | |
da3bd642 | 822 | sysbus_init_mmio(SYS_BUS_DEVICE(dev), &pfl->mem); |
de8efe8f | 823 | |
d80cf1eb | 824 | timer_init_ns(&pfl->timer, QEMU_CLOCK_VIRTUAL, pflash_timer, pfl); |
29133e9a FB |
825 | pfl->wcycle = 0; |
826 | pfl->cmd = 0; | |
827 | pfl->status = 0; | |
9ac45b88 | 828 | |
29133e9a | 829 | /* Hardcoded CFI table (mostly from SG29 Spansion flash) */ |
64659053 | 830 | const uint16_t pri_ofs = 0x40; |
29133e9a FB |
831 | /* Standard "QRY" string */ |
832 | pfl->cfi_table[0x10] = 'Q'; | |
833 | pfl->cfi_table[0x11] = 'R'; | |
834 | pfl->cfi_table[0x12] = 'Y'; | |
835 | /* Command set (AMD/Fujitsu) */ | |
836 | pfl->cfi_table[0x13] = 0x02; | |
837 | pfl->cfi_table[0x14] = 0x00; | |
78556820 | 838 | /* Primary extended table address */ |
d6874c83 PMD |
839 | pfl->cfi_table[0x15] = pri_ofs; |
840 | pfl->cfi_table[0x16] = pri_ofs >> 8; | |
29133e9a FB |
841 | /* Alternate command set (none) */ |
842 | pfl->cfi_table[0x17] = 0x00; | |
843 | pfl->cfi_table[0x18] = 0x00; | |
844 | /* Alternate extended table (none) */ | |
845 | pfl->cfi_table[0x19] = 0x00; | |
846 | pfl->cfi_table[0x1A] = 0x00; | |
847 | /* Vcc min */ | |
848 | pfl->cfi_table[0x1B] = 0x27; | |
849 | /* Vcc max */ | |
850 | pfl->cfi_table[0x1C] = 0x36; | |
851 | /* Vpp min (no Vpp pin) */ | |
852 | pfl->cfi_table[0x1D] = 0x00; | |
853 | /* Vpp max (no Vpp pin) */ | |
854 | pfl->cfi_table[0x1E] = 0x00; | |
9ac45b88 | 855 | /* Timeout per single byte/word write (128 ms) */ |
29133e9a | 856 | pfl->cfi_table[0x1F] = 0x07; |
78556820 EI |
857 | /* Timeout for min size buffer write (NA) */ |
858 | pfl->cfi_table[0x20] = 0x00; | |
29133e9a FB |
859 | /* Typical timeout for block erase (512 ms) */ |
860 | pfl->cfi_table[0x21] = 0x09; | |
861 | /* Typical timeout for full chip erase (4096 ms) */ | |
862 | pfl->cfi_table[0x22] = 0x0C; | |
863 | /* Reserved */ | |
864 | pfl->cfi_table[0x23] = 0x01; | |
78556820 EI |
865 | /* Max timeout for buffer write (NA) */ |
866 | pfl->cfi_table[0x24] = 0x00; | |
29133e9a FB |
867 | /* Max timeout for block erase */ |
868 | pfl->cfi_table[0x25] = 0x0A; | |
869 | /* Max timeout for chip erase */ | |
870 | pfl->cfi_table[0x26] = 0x0D; | |
871 | /* Device size */ | |
1eb27d69 | 872 | pfl->cfi_table[0x27] = ctz32(pfl->chip_len); |
29133e9a FB |
873 | /* Flash device interface (8 & 16 bits) */ |
874 | pfl->cfi_table[0x28] = 0x02; | |
875 | pfl->cfi_table[0x29] = 0x00; | |
876 | /* Max number of bytes in multi-bytes write */ | |
95d1f3ed JM |
877 | /* XXX: disable buffered write as it's not supported */ |
878 | // pfl->cfi_table[0x2A] = 0x05; | |
879 | pfl->cfi_table[0x2A] = 0x00; | |
29133e9a | 880 | pfl->cfi_table[0x2B] = 0x00; |
64659053 SC |
881 | /* Number of erase block regions */ |
882 | pfl->cfi_table[0x2c] = nb_regions; | |
883 | /* Erase block regions */ | |
884 | for (int i = 0; i < nb_regions; ++i) { | |
885 | uint32_t sector_len_per_device = pfl->sector_len[i]; | |
886 | pfl->cfi_table[0x2d + 4 * i] = pfl->nb_blocs[i] - 1; | |
887 | pfl->cfi_table[0x2e + 4 * i] = (pfl->nb_blocs[i] - 1) >> 8; | |
888 | pfl->cfi_table[0x2f + 4 * i] = sector_len_per_device >> 8; | |
889 | pfl->cfi_table[0x30 + 4 * i] = sector_len_per_device >> 16; | |
890 | } | |
891 | assert(0x2c + 4 * nb_regions < pri_ofs); | |
29133e9a | 892 | |
78556820 | 893 | /* Extended */ |
d6874c83 PMD |
894 | pfl->cfi_table[0x00 + pri_ofs] = 'P'; |
895 | pfl->cfi_table[0x01 + pri_ofs] = 'R'; | |
896 | pfl->cfi_table[0x02 + pri_ofs] = 'I'; | |
78556820 | 897 | |
9ac45b88 | 898 | /* Extended version 1.0 */ |
d6874c83 PMD |
899 | pfl->cfi_table[0x03 + pri_ofs] = '1'; |
900 | pfl->cfi_table[0x04 + pri_ofs] = '0'; | |
78556820 | 901 | |
9ac45b88 | 902 | /* Address sensitive unlock required. */ |
d6874c83 | 903 | pfl->cfi_table[0x05 + pri_ofs] = 0x00; |
ddb6f225 SC |
904 | /* Erase suspend to read/write. */ |
905 | pfl->cfi_table[0x06 + pri_ofs] = 0x02; | |
9ac45b88 | 906 | /* Sector protect not supported. */ |
d6874c83 | 907 | pfl->cfi_table[0x07 + pri_ofs] = 0x00; |
9ac45b88 | 908 | /* Temporary sector unprotect not supported. */ |
d6874c83 | 909 | pfl->cfi_table[0x08 + pri_ofs] = 0x00; |
78556820 | 910 | |
9ac45b88 | 911 | /* Sector protect/unprotect scheme. */ |
d6874c83 | 912 | pfl->cfi_table[0x09 + pri_ofs] = 0x00; |
78556820 | 913 | |
9ac45b88 | 914 | /* Simultaneous operation not supported. */ |
d6874c83 | 915 | pfl->cfi_table[0x0a + pri_ofs] = 0x00; |
9ac45b88 | 916 | /* Burst mode not supported. */ |
d6874c83 | 917 | pfl->cfi_table[0x0b + pri_ofs] = 0x00; |
c2c1bf44 PMD |
918 | /* Page mode not supported. */ |
919 | pfl->cfi_table[0x0c + pri_ofs] = 0x00; | |
920 | assert(0x0c + pri_ofs < ARRAY_SIZE(pfl->cfi_table)); | |
368a354f PC |
921 | } |
922 | ||
923 | static Property pflash_cfi02_properties[] = { | |
16434065 | 924 | DEFINE_PROP_DRIVE("drive", PFlashCFI02, blk), |
64659053 SC |
925 | DEFINE_PROP_UINT32("num-blocks", PFlashCFI02, uniform_nb_blocs, 0), |
926 | DEFINE_PROP_UINT32("sector-length", PFlashCFI02, uniform_sector_len, 0), | |
927 | DEFINE_PROP_UINT32("num-blocks0", PFlashCFI02, nb_blocs[0], 0), | |
928 | DEFINE_PROP_UINT32("sector-length0", PFlashCFI02, sector_len[0], 0), | |
929 | DEFINE_PROP_UINT32("num-blocks1", PFlashCFI02, nb_blocs[1], 0), | |
930 | DEFINE_PROP_UINT32("sector-length1", PFlashCFI02, sector_len[1], 0), | |
931 | DEFINE_PROP_UINT32("num-blocks2", PFlashCFI02, nb_blocs[2], 0), | |
932 | DEFINE_PROP_UINT32("sector-length2", PFlashCFI02, sector_len[2], 0), | |
933 | DEFINE_PROP_UINT32("num-blocks3", PFlashCFI02, nb_blocs[3], 0), | |
934 | DEFINE_PROP_UINT32("sector-length3", PFlashCFI02, sector_len[3], 0), | |
16434065 MA |
935 | DEFINE_PROP_UINT8("width", PFlashCFI02, width, 0), |
936 | DEFINE_PROP_UINT8("mappings", PFlashCFI02, mappings, 0), | |
937 | DEFINE_PROP_UINT8("big-endian", PFlashCFI02, be, 0), | |
938 | DEFINE_PROP_UINT16("id0", PFlashCFI02, ident0, 0), | |
939 | DEFINE_PROP_UINT16("id1", PFlashCFI02, ident1, 0), | |
940 | DEFINE_PROP_UINT16("id2", PFlashCFI02, ident2, 0), | |
941 | DEFINE_PROP_UINT16("id3", PFlashCFI02, ident3, 0), | |
942 | DEFINE_PROP_UINT16("unlock-addr0", PFlashCFI02, unlock_addr0, 0), | |
943 | DEFINE_PROP_UINT16("unlock-addr1", PFlashCFI02, unlock_addr1, 0), | |
944 | DEFINE_PROP_STRING("name", PFlashCFI02, name), | |
368a354f PC |
945 | DEFINE_PROP_END_OF_LIST(), |
946 | }; | |
947 | ||
d80cf1eb SC |
948 | static void pflash_cfi02_unrealize(DeviceState *dev, Error **errp) |
949 | { | |
e7b62741 | 950 | PFlashCFI02 *pfl = PFLASH_CFI02(dev); |
d80cf1eb | 951 | timer_del(&pfl->timer); |
ddb6f225 | 952 | g_free(pfl->sector_erase_map); |
d80cf1eb SC |
953 | } |
954 | ||
368a354f PC |
955 | static void pflash_cfi02_class_init(ObjectClass *klass, void *data) |
956 | { | |
957 | DeviceClass *dc = DEVICE_CLASS(klass); | |
368a354f | 958 | |
da3bd642 | 959 | dc->realize = pflash_cfi02_realize; |
d80cf1eb | 960 | dc->unrealize = pflash_cfi02_unrealize; |
368a354f | 961 | dc->props = pflash_cfi02_properties; |
df6f9318 | 962 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
368a354f PC |
963 | } |
964 | ||
965 | static const TypeInfo pflash_cfi02_info = { | |
e7b62741 | 966 | .name = TYPE_PFLASH_CFI02, |
368a354f | 967 | .parent = TYPE_SYS_BUS_DEVICE, |
16434065 | 968 | .instance_size = sizeof(PFlashCFI02), |
368a354f PC |
969 | .class_init = pflash_cfi02_class_init, |
970 | }; | |
971 | ||
972 | static void pflash_cfi02_register_types(void) | |
973 | { | |
974 | type_register_static(&pflash_cfi02_info); | |
975 | } | |
976 | ||
977 | type_init(pflash_cfi02_register_types) | |
978 | ||
16434065 | 979 | PFlashCFI02 *pflash_cfi02_register(hwaddr base, |
940d5b13 | 980 | const char *name, |
16434065 MA |
981 | hwaddr size, |
982 | BlockBackend *blk, | |
ce14710f | 983 | uint32_t sector_len, |
16434065 MA |
984 | int nb_mappings, int width, |
985 | uint16_t id0, uint16_t id1, | |
986 | uint16_t id2, uint16_t id3, | |
987 | uint16_t unlock_addr0, | |
988 | uint16_t unlock_addr1, | |
989 | int be) | |
368a354f | 990 | { |
e7b62741 | 991 | DeviceState *dev = qdev_create(NULL, TYPE_PFLASH_CFI02); |
368a354f | 992 | |
9b3d111a MA |
993 | if (blk) { |
994 | qdev_prop_set_drive(dev, "drive", blk, &error_abort); | |
368a354f | 995 | } |
ce14710f MA |
996 | assert(size % sector_len == 0); |
997 | qdev_prop_set_uint32(dev, "num-blocks", size / sector_len); | |
368a354f PC |
998 | qdev_prop_set_uint32(dev, "sector-length", sector_len); |
999 | qdev_prop_set_uint8(dev, "width", width); | |
1000 | qdev_prop_set_uint8(dev, "mappings", nb_mappings); | |
1001 | qdev_prop_set_uint8(dev, "big-endian", !!be); | |
1002 | qdev_prop_set_uint16(dev, "id0", id0); | |
1003 | qdev_prop_set_uint16(dev, "id1", id1); | |
1004 | qdev_prop_set_uint16(dev, "id2", id2); | |
1005 | qdev_prop_set_uint16(dev, "id3", id3); | |
1006 | qdev_prop_set_uint16(dev, "unlock-addr0", unlock_addr0); | |
1007 | qdev_prop_set_uint16(dev, "unlock-addr1", unlock_addr1); | |
1008 | qdev_prop_set_string(dev, "name", name); | |
1009 | qdev_init_nofail(dev); | |
1010 | ||
3509c396 | 1011 | sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base); |
e7b62741 | 1012 | return PFLASH_CFI02(dev); |
29133e9a | 1013 | } |