sim: dv-cfi: include common headers only
[binutils-gdb.git] / sim / common / dv-cfi.c
1 /* Common Flash Memory Interface (CFI) model.
2 http://www.spansion.com/Support/AppNotes/CFI_Spec_AN_03.pdf
3 http://www.spansion.com/Support/AppNotes/cfi_100_20011201.pdf
4
5 Copyright (C) 2010-2011 Free Software Foundation, Inc.
6 Contributed by Analog Devices, Inc.
7
8 This file is part of simulators.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23 /* TODO: support vendor query tables. */
24
25 #include "cconfig.h"
26
27 #include <math.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #ifdef HAVE_SYS_MMAN_H
32 #include <sys/mman.h>
33 #endif
34
35 #include "sim-main.h"
36 #include "hw-base.h"
37 #include "hw-main.h"
38 #include "dv-cfi.h"
39
40 /* Flashes are simple state machines, so here we cover all the
41 different states a device might be in at any particular time. */
42 enum cfi_state
43 {
44 CFI_STATE_READ,
45 CFI_STATE_READ_ID,
46 CFI_STATE_CFI_QUERY,
47 CFI_STATE_PROTECT,
48 CFI_STATE_STATUS,
49 CFI_STATE_ERASE,
50 CFI_STATE_WRITE,
51 CFI_STATE_WRITE_BUFFER,
52 CFI_STATE_WRITE_BUFFER_CONFIRM,
53 };
54
55 /* This is the structure that all CFI conforming devices must provided
56 when asked for it. This allows a single driver to dynamically support
57 different flash geometries without having to hardcode specs.
58
59 If you want to start mucking about here, you should just grab the
60 CFI spec and review that (see top of this file for URIs). */
61 struct cfi_query
62 {
63 /* This is always 'Q' 'R' 'Y'. */
64 unsigned char qry[3];
65 /* Primary vendor ID. */
66 unsigned char p_id[2];
67 /* Primary query table address. */
68 unsigned char p_adr[2];
69 /* Alternate vendor ID. */
70 unsigned char a_id[2];
71 /* Alternate query table address. */
72 unsigned char a_adr[2];
73 union
74 {
75 /* Voltage levels. */
76 unsigned char voltages[4];
77 struct
78 {
79 /* Normal min voltage level. */
80 unsigned char vcc_min;
81 /* Normal max voltage level. */
82 unsigned char vcc_max;
83 /* Programming min volage level. */
84 unsigned char vpp_min;
85 /* Programming max volage level. */
86 unsigned char vpp_max;
87 };
88 };
89 union
90 {
91 /* Operational timeouts. */
92 unsigned char timeouts[8];
93 struct
94 {
95 /* Typical timeout for writing a single "unit". */
96 unsigned char timeout_typ_unit_write;
97 /* Typical timeout for writing a single "buffer". */
98 unsigned char timeout_typ_buf_write;
99 /* Typical timeout for erasing a block. */
100 unsigned char timeout_typ_block_erase;
101 /* Typical timeout for erasing the chip. */
102 unsigned char timeout_typ_chip_erase;
103 /* Max timeout for writing a single "unit". */
104 unsigned char timeout_max_unit_write;
105 /* Max timeout for writing a single "buffer". */
106 unsigned char timeout_max_buf_write;
107 /* Max timeout for erasing a block. */
108 unsigned char timeout_max_block_erase;
109 /* Max timeout for erasing the chip. */
110 unsigned char timeout_max_chip_erase;
111 };
112 };
113 /* Flash size is 2^dev_size bytes. */
114 unsigned char dev_size;
115 /* Flash device interface description. */
116 unsigned char iface_desc[2];
117 /* Max length of a single buffer write is 2^max_buf_write_len bytes. */
118 unsigned char max_buf_write_len[2];
119 /* Number of erase regions. */
120 unsigned char num_erase_regions;
121 /* The erase regions would now be an array after this point, but since
122 it is dynamic, we'll provide that from "struct cfi" when requested. */
123 /*unsigned char erase_region_info;*/
124 };
125
126 /* Flashes may have regions with different erase sizes. There is one
127 structure per erase region. */
128 struct cfi_erase_region
129 {
130 unsigned blocks;
131 unsigned size;
132 unsigned start;
133 unsigned end;
134 };
135
136 struct cfi;
137
138 /* Flashes are accessed via commands -- you write a certain number to
139 a special address to change the flash state and access info other
140 than the data. Diff companies have implemented their own command
141 set. This structure abstracts the different command sets so that
142 we can support multiple ones with just a single sim driver. */
143 struct cfi_cmdset
144 {
145 unsigned id;
146 void (*setup) (struct hw *me, struct cfi *cfi);
147 bool (*write) (struct hw *me, struct cfi *cfi, const void *source,
148 unsigned offset, unsigned value, unsigned nr_bytes);
149 bool (*read) (struct hw *me, struct cfi *cfi, void *dest,
150 unsigned offset, unsigned shifted_offset, unsigned nr_bytes);
151 };
152
153 /* The per-flash state. Much of this comes from the device tree which
154 people declare themselves. See top of attach_cfi_regs() for more
155 info. */
156 struct cfi
157 {
158 unsigned width, dev_size, status;
159 enum cfi_state state;
160 unsigned char *data, *mmap;
161
162 struct cfi_query query;
163 const struct cfi_cmdset *cmdset;
164
165 unsigned char *erase_region_info;
166 struct cfi_erase_region *erase_regions;
167 };
168
169 /* Helpful strings which are used with HW_TRACE. */
170 static const char * const state_names[] =
171 {
172 "READ", "READ_ID", "CFI_QUERY", "PROTECT", "STATUS", "ERASE", "WRITE",
173 "WRITE_BUFFER", "WRITE_BUFFER_CONFIRM",
174 };
175 \f
176 /* Erase the block specified by the offset into the given CFI flash. */
177 static void
178 cfi_erase_block (struct hw *me, struct cfi *cfi, unsigned offset)
179 {
180 unsigned i;
181 struct cfi_erase_region *region;
182
183 /* If no erase regions, then we can only do whole chip erase. */
184 /* XXX: Is this within spec ? Or must there always be at least one ? */
185 if (!cfi->query.num_erase_regions)
186 memset (cfi->data, 0xff, cfi->dev_size);
187
188 for (i = 0; i < cfi->query.num_erase_regions; ++i)
189 {
190 region = &cfi->erase_regions[i];
191
192 if (offset >= region->end)
193 continue;
194
195 /* XXX: Does spec require the erase addr to be erase block aligned ?
196 Maybe this is check is overly cautious ... */
197 offset &= ~(region->size - 1);
198 memset (cfi->data + offset, 0xff, region->size);
199 break;
200 }
201 }
202
203 /* Depending on the bus width, addresses might be bit shifted. This
204 helps us normalize everything without cluttering up the rest of
205 the code. */
206 static unsigned
207 cfi_unshift_addr (struct cfi *cfi, unsigned addr)
208 {
209 switch (cfi->width)
210 {
211 case 4: addr >>= 1; /* fallthrough. */
212 case 2: addr >>= 1;
213 }
214 return addr;
215 }
216
217 /* CFI requires all values to be little endian in its structure, so
218 this helper writes a 16bit value into a little endian byte buffer. */
219 static void
220 cfi_encode_16bit (unsigned char *data, unsigned num)
221 {
222 data[0] = num;
223 data[1] = num >> 8;
224 }
225 \f
226 /* The functions required to implement the Intel command set. */
227
228 static bool
229 cmdset_intel_write (struct hw *me, struct cfi *cfi, const void *source,
230 unsigned offset, unsigned value, unsigned nr_bytes)
231 {
232 switch (cfi->state)
233 {
234 case CFI_STATE_READ:
235 case CFI_STATE_READ_ID:
236 switch (value)
237 {
238 case INTEL_CMD_ERASE_BLOCK:
239 cfi->state = CFI_STATE_ERASE;
240 break;
241 case INTEL_CMD_WRITE:
242 case INTEL_CMD_WRITE_ALT:
243 cfi->state = CFI_STATE_WRITE;
244 break;
245 case INTEL_CMD_STATUS_CLEAR:
246 cfi->status = INTEL_SR_DWS;
247 break;
248 case INTEL_CMD_LOCK_SETUP:
249 cfi->state = CFI_STATE_PROTECT;
250 break;
251 default:
252 return false;
253 }
254 break;
255
256 case CFI_STATE_ERASE:
257 if (value == INTEL_CMD_ERASE_CONFIRM)
258 {
259 cfi_erase_block (me, cfi, offset);
260 cfi->status &= ~(INTEL_SR_PS | INTEL_SR_ES);
261 }
262 else
263 cfi->status |= INTEL_SR_PS | INTEL_SR_ES;
264 cfi->state = CFI_STATE_STATUS;
265 break;
266
267 case CFI_STATE_PROTECT:
268 switch (value)
269 {
270 case INTEL_CMD_LOCK_BLOCK:
271 case INTEL_CMD_UNLOCK_BLOCK:
272 case INTEL_CMD_LOCK_DOWN_BLOCK:
273 /* XXX: Handle the command. */
274 break;
275 default:
276 /* Kick out. */
277 cfi->status |= INTEL_SR_PS | INTEL_SR_ES;
278 break;
279 }
280 cfi->state = CFI_STATE_STATUS;
281 break;
282
283 default:
284 return false;
285 }
286
287 return true;
288 }
289
290 static bool
291 cmdset_intel_read (struct hw *me, struct cfi *cfi, void *dest,
292 unsigned offset, unsigned shifted_offset, unsigned nr_bytes)
293 {
294 unsigned char *sdest = dest;
295
296 switch (cfi->state)
297 {
298 case CFI_STATE_STATUS:
299 case CFI_STATE_ERASE:
300 *sdest = cfi->status;
301 break;
302
303 case CFI_STATE_READ_ID:
304 switch (shifted_offset & 0x1ff)
305 {
306 case 0x00: /* Manufacturer Code. */
307 cfi_encode_16bit (dest, INTEL_ID_MANU);
308 break;
309 case 0x01: /* Device ID Code. */
310 /* XXX: Push to device tree ? */
311 cfi_encode_16bit (dest, 0xad);
312 break;
313 case 0x02: /* Block lock state. */
314 /* XXX: This is per-block ... */
315 *sdest = 0x00;
316 break;
317 case 0x05: /* Read Configuration Register. */
318 cfi_encode_16bit (dest, (1 << 15));
319 break;
320 default:
321 return false;
322 }
323 break;
324
325 default:
326 return false;
327 }
328
329 return true;
330 }
331
332 static void
333 cmdset_intel_setup (struct hw *me, struct cfi *cfi)
334 {
335 cfi->status = INTEL_SR_DWS;
336 }
337
338 static const struct cfi_cmdset cfi_cmdset_intel =
339 {
340 CFI_CMDSET_INTEL, cmdset_intel_setup, cmdset_intel_write, cmdset_intel_read,
341 };
342 \f
343 /* All of the supported command sets get listed here. We then walk this
344 array to see if the user requested command set is implemented. */
345 static const struct cfi_cmdset * const cfi_cmdsets[] =
346 {
347 &cfi_cmdset_intel,
348 };
349 \f
350 /* All writes to the flash address space come here. Using the state
351 machine, we figure out what to do with this specific write. All
352 common code sits here and if there is a request we can't process,
353 we hand it off to the command set-specific write function. */
354 static unsigned
355 cfi_io_write_buffer (struct hw *me, const void *source, int space,
356 address_word addr, unsigned nr_bytes)
357 {
358 struct cfi *cfi = hw_data (me);
359 const unsigned char *ssource = source;
360 enum cfi_state old_state;
361 unsigned offset, shifted_offset, value;
362
363 offset = addr & (cfi->dev_size - 1);
364 shifted_offset = cfi_unshift_addr (cfi, offset);
365
366 if (cfi->width != nr_bytes)
367 {
368 HW_TRACE ((me, "write 0x%08lx length %u does not match flash width %u",
369 (unsigned long) addr, nr_bytes, cfi->width));
370 return nr_bytes;
371 }
372
373 if (cfi->state == CFI_STATE_WRITE)
374 {
375 /* NOR flash can only go from 1 to 0. */
376 unsigned i;
377
378 HW_TRACE ((me, "program %#x length %u", offset, nr_bytes));
379
380 for (i = 0; i < nr_bytes; ++i)
381 cfi->data[offset + i] &= ssource[i];
382
383 cfi->state = CFI_STATE_STATUS;
384
385 return nr_bytes;
386 }
387
388 value = ssource[0];
389
390 old_state = cfi->state;
391
392 if (value == CFI_CMD_READ || value == CFI_CMD_RESET)
393 {
394 cfi->state = CFI_STATE_READ;
395 goto done;
396 }
397
398 switch (cfi->state)
399 {
400 case CFI_STATE_READ:
401 case CFI_STATE_READ_ID:
402 if (value == CFI_CMD_CFI_QUERY)
403 {
404 if (shifted_offset == CFI_ADDR_CFI_QUERY_START)
405 cfi->state = CFI_STATE_CFI_QUERY;
406 goto done;
407 }
408
409 if (value == CFI_CMD_READ_ID)
410 {
411 cfi->state = CFI_STATE_READ_ID;
412 goto done;
413 }
414
415 /* Fall through. */
416
417 default:
418 if (!cfi->cmdset->write (me, cfi, source, offset, value, nr_bytes))
419 HW_TRACE ((me, "unhandled command %#x at %#x", value, offset));
420 break;
421 }
422
423 done:
424 HW_TRACE ((me, "write 0x%08lx command {%#x,%#x,%#x,%#x}; state %s -> %s",
425 (unsigned long) addr, ssource[0],
426 nr_bytes > 1 ? ssource[1] : 0,
427 nr_bytes > 2 ? ssource[2] : 0,
428 nr_bytes > 3 ? ssource[3] : 0,
429 state_names[old_state], state_names[cfi->state]));
430
431 return nr_bytes;
432 }
433
434 /* All reads to the flash address space come here. Using the state
435 machine, we figure out what to return -- actual data stored in the
436 flash, the CFI query structure, some status info, or something else ?
437 Any requests that we can't handle are passed to the command set-
438 specific read function. */
439 static unsigned
440 cfi_io_read_buffer (struct hw *me, void *dest, int space,
441 address_word addr, unsigned nr_bytes)
442 {
443 struct cfi *cfi = hw_data (me);
444 unsigned char *sdest = dest;
445 unsigned offset, shifted_offset;
446
447 offset = addr & (cfi->dev_size - 1);
448 shifted_offset = cfi_unshift_addr (cfi, offset);
449
450 /* XXX: Is this OK to enforce ? */
451 #if 0
452 if (cfi->state != CFI_STATE_READ && cfi->width != nr_bytes)
453 {
454 HW_TRACE ((me, "read 0x%08lx length %u does not match flash width %u",
455 (unsigned long) addr, nr_bytes, cfi->width));
456 return nr_bytes;
457 }
458 #endif
459
460 HW_TRACE ((me, "%s read 0x%08lx length %u",
461 state_names[cfi->state], (unsigned long) addr, nr_bytes));
462
463 switch (cfi->state)
464 {
465 case CFI_STATE_READ:
466 memcpy (dest, cfi->data + offset, nr_bytes);
467 break;
468
469 case CFI_STATE_CFI_QUERY:
470 if (shifted_offset >= CFI_ADDR_CFI_QUERY_RESULT &&
471 shifted_offset < CFI_ADDR_CFI_QUERY_RESULT + sizeof (cfi->query) +
472 (cfi->query.num_erase_regions * 4))
473 {
474 unsigned char *qry;
475
476 shifted_offset -= CFI_ADDR_CFI_QUERY_RESULT;
477 if (shifted_offset >= sizeof (cfi->query))
478 {
479 qry = cfi->erase_region_info;
480 shifted_offset -= sizeof (cfi->query);
481 }
482 else
483 qry = (void *) &cfi->query;
484
485 sdest[0] = qry[shifted_offset];
486 memset (sdest + 1, 0, nr_bytes - 1);
487
488 break;
489 }
490
491 default:
492 if (!cfi->cmdset->read (me, cfi, dest, offset, shifted_offset, nr_bytes))
493 HW_TRACE ((me, "unhandled state %s", state_names[cfi->state]));
494 break;
495 }
496
497 return nr_bytes;
498 }
499
500 /* Clean up any state when this device is removed (e.g. when shutting
501 down, or when reloading via gdb). */
502 static void
503 cfi_delete_callback (struct hw *me)
504 {
505 #ifdef HAVE_MMAP
506 struct cfi *cfi = hw_data (me);
507
508 if (cfi->mmap)
509 munmap (cfi->mmap, cfi->dev_size);
510 #endif
511 }
512
513 /* Helper function to easily add CFI erase regions to the existing set. */
514 static void
515 cfi_add_erase_region (struct hw *me, struct cfi *cfi,
516 unsigned blocks, unsigned size)
517 {
518 unsigned num_regions = cfi->query.num_erase_regions;
519 struct cfi_erase_region *region;
520 unsigned char *qry_region;
521
522 /* Store for our own usage. */
523 region = &cfi->erase_regions[num_regions];
524 region->blocks = blocks;
525 region->size = size;
526 if (num_regions == 0)
527 region->start = 0;
528 else
529 region->start = region[-1].end;
530 region->end = region->start + (blocks * size);
531
532 /* Regions are 4 bytes long. */
533 qry_region = cfi->erase_region_info + 4 * num_regions;
534
535 /* [0][1] = number erase blocks - 1 */
536 if (blocks > 0xffff + 1)
537 hw_abort (me, "erase blocks %u too big to fit into region info", blocks);
538 cfi_encode_16bit (&qry_region[0], blocks - 1);
539
540 /* [2][3] = block size / 256 bytes */
541 if (size > 0xffff * 256)
542 hw_abort (me, "erase size %u too big to fit into region info", size);
543 cfi_encode_16bit (&qry_region[2], size / 256);
544
545 /* Yet another region. */
546 cfi->query.num_erase_regions = num_regions + 1;
547 }
548
549 /* Device tree options:
550 Required:
551 .../reg <addr> <len>
552 .../cmdset <primary; integer> [alt; integer]
553 Optional:
554 .../size <device size (must be pow of 2)>
555 .../width <8|16|32>
556 .../write_size <integer (must be pow of 2)>
557 .../erase_regions <number blocks> <block size> \
558 [<number blocks> <block size> ...]
559 .../voltage <vcc min> <vcc max> <vpp min> <vpp max>
560 .../timeouts <typ unit write> <typ buf write> \
561 <typ block erase> <typ chip erase> \
562 <max unit write> <max buf write> \
563 <max block erase> <max chip erase>
564 .../file <file> [ro|rw]
565 Defaults:
566 size: <len> from "reg"
567 width: 8
568 write_size: 0 (not supported)
569 erase_region: 1 (can only erase whole chip)
570 voltage: 0.0V (for all)
571 timeouts: typ: 1µs, not supported, 1ms, not supported
572 max: 1µs, 1ms, 1ms, not supported
573
574 TODO: Verify user args are valid (e.g. voltage is 8 bits). */
575 static void
576 attach_cfi_regs (struct hw *me, struct cfi *cfi)
577 {
578 address_word attach_address;
579 int attach_space;
580 unsigned attach_size;
581 reg_property_spec reg;
582 bool fd_writable;
583 int i, ret, fd;
584 signed_cell ival;
585
586 if (hw_find_property (me, "reg") == NULL)
587 hw_abort (me, "Missing \"reg\" property");
588 if (hw_find_property (me, "cmdset") == NULL)
589 hw_abort (me, "Missing \"cmdset\" property");
590
591 if (!hw_find_reg_array_property (me, "reg", 0, &reg))
592 hw_abort (me, "\"reg\" property must contain three addr/size entries");
593
594 hw_unit_address_to_attach_address (hw_parent (me),
595 &reg.address,
596 &attach_space, &attach_address, me);
597 hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
598
599 hw_attach_address (hw_parent (me),
600 0, attach_space, attach_address, attach_size, me);
601
602 /* Extract the desired flash command set. */
603 ret = hw_find_integer_array_property (me, "cmdset", 0, &ival);
604 if (ret != 1 && ret != 2)
605 hw_abort (me, "\"cmdset\" property takes 1 or 2 entries");
606 cfi_encode_16bit (cfi->query.p_id, ival);
607
608 for (i = 0; i < ARRAY_SIZE (cfi_cmdsets); ++i)
609 if (cfi_cmdsets[i]->id == ival)
610 cfi->cmdset = cfi_cmdsets[i];
611 if (cfi->cmdset == NULL)
612 hw_abort (me, "cmdset %u not supported", ival);
613
614 if (ret == 2)
615 {
616 hw_find_integer_array_property (me, "cmdset", 1, &ival);
617 cfi_encode_16bit (cfi->query.a_id, ival);
618 }
619
620 /* Extract the desired device size. */
621 if (hw_find_property (me, "size"))
622 cfi->dev_size = hw_find_integer_property (me, "size");
623 else
624 cfi->dev_size = attach_size;
625 cfi->query.dev_size = log2 (cfi->dev_size);
626
627 /* Extract the desired flash width. */
628 if (hw_find_property (me, "width"))
629 {
630 cfi->width = hw_find_integer_property (me, "width");
631 if (cfi->width != 8 && cfi->width != 16 && cfi->width != 32)
632 hw_abort (me, "\"width\" must be 8 or 16 or 32, not %u", cfi->width);
633 }
634 else
635 /* Default to 8 bit. */
636 cfi->width = 8;
637 /* Turn 8/16/32 into 1/2/4. */
638 cfi->width /= 8;
639
640 /* Extract optional write buffer size. */
641 if (hw_find_property (me, "write_size"))
642 {
643 ival = hw_find_integer_property (me, "write_size");
644 cfi_encode_16bit (cfi->query.max_buf_write_len, log2 (ival));
645 }
646
647 /* Extract optional erase regions. */
648 if (hw_find_property (me, "erase_regions"))
649 {
650 ret = hw_find_integer_array_property (me, "erase_regions", 0, &ival);
651 if (ret % 2)
652 hw_abort (me, "\"erase_regions\" must be specified in sets of 2");
653
654 cfi->erase_region_info = HW_NALLOC (me, unsigned char, ret / 2);
655 cfi->erase_regions = HW_NALLOC (me, struct cfi_erase_region, ret / 2);
656
657 for (i = 0; i < ret; i += 2)
658 {
659 unsigned blocks, size;
660
661 hw_find_integer_array_property (me, "erase_regions", i, &ival);
662 blocks = ival;
663
664 hw_find_integer_array_property (me, "erase_regions", i + 1, &ival);
665 size = ival;
666
667 cfi_add_erase_region (me, cfi, blocks, size);
668 }
669 }
670
671 /* Extract optional voltages. */
672 if (hw_find_property (me, "voltage"))
673 {
674 unsigned num = ARRAY_SIZE (cfi->query.voltages);
675
676 ret = hw_find_integer_array_property (me, "voltage", 0, &ival);
677 if (ret > num)
678 hw_abort (me, "\"voltage\" may have only %u arguments", num);
679
680 for (i = 0; i < ret; ++i)
681 {
682 hw_find_integer_array_property (me, "voltage", i, &ival);
683 cfi->query.voltages[i] = ival;
684 }
685 }
686
687 /* Extract optional timeouts. */
688 if (hw_find_property (me, "timeout"))
689 {
690 unsigned num = ARRAY_SIZE (cfi->query.timeouts);
691
692 ret = hw_find_integer_array_property (me, "timeout", 0, &ival);
693 if (ret > num)
694 hw_abort (me, "\"timeout\" may have only %u arguments", num);
695
696 for (i = 0; i < ret; ++i)
697 {
698 hw_find_integer_array_property (me, "timeout", i, &ival);
699 cfi->query.timeouts[i] = ival;
700 }
701 }
702
703 /* Extract optional file. */
704 fd = -1;
705 fd_writable = false;
706 if (hw_find_property (me, "file"))
707 {
708 const char *file;
709
710 ret = hw_find_string_array_property (me, "file", 0, &file);
711 if (ret > 2)
712 hw_abort (me, "\"file\" may take only one argument");
713 if (ret == 2)
714 {
715 const char *writable;
716
717 hw_find_string_array_property (me, "file", 1, &writable);
718 fd_writable = !strcmp (writable, "rw");
719 }
720
721 fd = open (file, fd_writable ? O_RDWR : O_RDONLY);
722 if (fd < 0)
723 hw_abort (me, "unable to read file `%s': %s", file, strerror (errno));
724 }
725
726 /* Figure out where our initial flash data is coming from. */
727 if (fd != -1 && fd_writable)
728 {
729 #ifdef HAVE_MMAP
730 posix_fallocate (fd, 0, cfi->dev_size);
731
732 cfi->mmap = mmap (NULL, cfi->dev_size,
733 PROT_READ | (fd_writable ? PROT_WRITE : 0),
734 MAP_SHARED, fd, 0);
735
736 if (cfi->mmap == MAP_FAILED)
737 cfi->mmap = NULL;
738 else
739 cfi->data = cfi->mmap;
740 #else
741 sim_io_eprintf (hw_system (me),
742 "cfi: sorry, file write support requires mmap()\n");
743 #endif
744 }
745 if (!cfi->data)
746 {
747 size_t read_len;
748
749 cfi->data = HW_NALLOC (me, unsigned char, cfi->dev_size);
750
751 if (fd != -1)
752 {
753 /* Use stdio to avoid EINTR issues with read(). */
754 FILE *fp = fdopen (fd, "r");
755
756 if (fp)
757 read_len = fread (cfi->data, 1, cfi->dev_size, fp);
758 else
759 read_len = 0;
760
761 /* Don't need to fclose() with fdopen("r"). */
762 }
763 else
764 read_len = 0;
765
766 memset (cfi->data, 0xff, cfi->dev_size - read_len);
767 }
768
769 close (fd);
770 }
771
772 /* Once we've been declared in the device tree, this is the main
773 entry point. So allocate state, attach memory addresses, and
774 all that fun stuff. */
775 static void
776 cfi_finish (struct hw *me)
777 {
778 struct cfi *cfi;
779
780 cfi = HW_ZALLOC (me, struct cfi);
781
782 set_hw_data (me, cfi);
783 set_hw_io_read_buffer (me, cfi_io_read_buffer);
784 set_hw_io_write_buffer (me, cfi_io_write_buffer);
785 set_hw_delete (me, cfi_delete_callback);
786
787 attach_cfi_regs (me, cfi);
788
789 /* Initialize the CFI. */
790 cfi->state = CFI_STATE_READ;
791 memcpy (cfi->query.qry, "QRY", 3);
792 cfi->cmdset->setup (me, cfi);
793 }
794
795 /* Every device is required to declare this. */
796 const struct hw_descriptor dv_cfi_descriptor[] =
797 {
798 {"cfi", cfi_finish,},
799 {NULL, NULL},
800 };