root / hw / block / m25p80.c @ feature-archipelago
History | View | Annotate | Download (21.3 kB)
1 | 82a24990 | Peter A. G. Crosthwaite | /*
|
---|---|---|---|
2 | 82a24990 | Peter A. G. Crosthwaite | * ST M25P80 emulator. Emulate all SPI flash devices based on the m25p80 command
|
3 | 82a24990 | Peter A. G. Crosthwaite | * set. Known devices table current as of Jun/2012 and taken from linux.
|
4 | 82a24990 | Peter A. G. Crosthwaite | * See drivers/mtd/devices/m25p80.c.
|
5 | 82a24990 | Peter A. G. Crosthwaite | *
|
6 | 82a24990 | Peter A. G. Crosthwaite | * Copyright (C) 2011 Edgar E. Iglesias <edgar.iglesias@gmail.com>
|
7 | 82a24990 | Peter A. G. Crosthwaite | * Copyright (C) 2012 Peter A. G. Crosthwaite <peter.crosthwaite@petalogix.com>
|
8 | 82a24990 | Peter A. G. Crosthwaite | * Copyright (C) 2012 PetaLogix
|
9 | 82a24990 | Peter A. G. Crosthwaite | *
|
10 | 82a24990 | Peter A. G. Crosthwaite | * This program is free software; you can redistribute it and/or
|
11 | 82a24990 | Peter A. G. Crosthwaite | * modify it under the terms of the GNU General Public License as
|
12 | 82a24990 | Peter A. G. Crosthwaite | * published by the Free Software Foundation; either version 2 or
|
13 | 82a24990 | Peter A. G. Crosthwaite | * (at your option) a later version of the License.
|
14 | 82a24990 | Peter A. G. Crosthwaite | *
|
15 | 82a24990 | Peter A. G. Crosthwaite | * This program is distributed in the hope that it will be useful,
|
16 | 82a24990 | Peter A. G. Crosthwaite | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 | 82a24990 | Peter A. G. Crosthwaite | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 | 82a24990 | Peter A. G. Crosthwaite | * GNU General Public License for more details.
|
19 | 82a24990 | Peter A. G. Crosthwaite | *
|
20 | 82a24990 | Peter A. G. Crosthwaite | * You should have received a copy of the GNU General Public License along
|
21 | 82a24990 | Peter A. G. Crosthwaite | * with this program; if not, see <http://www.gnu.org/licenses/>.
|
22 | 82a24990 | Peter A. G. Crosthwaite | */
|
23 | 82a24990 | Peter A. G. Crosthwaite | |
24 | 83c9f4ca | Paolo Bonzini | #include "hw/hw.h" |
25 | 9c17d615 | Paolo Bonzini | #include "sysemu/blockdev.h" |
26 | 83c9f4ca | Paolo Bonzini | #include "hw/ssi.h" |
27 | 82a24990 | Peter A. G. Crosthwaite | |
28 | 28097d02 | Peter Crosthwaite | #ifndef M25P80_ERR_DEBUG
|
29 | 28097d02 | Peter Crosthwaite | #define M25P80_ERR_DEBUG 0 |
30 | 82a24990 | Peter A. G. Crosthwaite | #endif
|
31 | 82a24990 | Peter A. G. Crosthwaite | |
32 | 28097d02 | Peter Crosthwaite | #define DB_PRINT_L(level, ...) do { \ |
33 | 28097d02 | Peter Crosthwaite | if (M25P80_ERR_DEBUG > (level)) { \
|
34 | 28097d02 | Peter Crosthwaite | fprintf(stderr, ": %s: ", __func__); \
|
35 | 28097d02 | Peter Crosthwaite | fprintf(stderr, ## __VA_ARGS__); \ |
36 | 28097d02 | Peter Crosthwaite | } \ |
37 | 28097d02 | Peter Crosthwaite | } while (0); |
38 | 28097d02 | Peter Crosthwaite | |
39 | 82a24990 | Peter A. G. Crosthwaite | /* Fields for FlashPartInfo->flags */
|
40 | 82a24990 | Peter A. G. Crosthwaite | |
41 | 82a24990 | Peter A. G. Crosthwaite | /* erase capabilities */
|
42 | 82a24990 | Peter A. G. Crosthwaite | #define ER_4K 1 |
43 | 82a24990 | Peter A. G. Crosthwaite | #define ER_32K 2 |
44 | 82a24990 | Peter A. G. Crosthwaite | /* set to allow the page program command to write 0s back to 1. Useful for
|
45 | 82a24990 | Peter A. G. Crosthwaite | * modelling EEPROM with SPI flash command set
|
46 | 82a24990 | Peter A. G. Crosthwaite | */
|
47 | 82a24990 | Peter A. G. Crosthwaite | #define WR_1 0x100 |
48 | 82a24990 | Peter A. G. Crosthwaite | |
49 | 82a24990 | Peter A. G. Crosthwaite | typedef struct FlashPartInfo { |
50 | 82a24990 | Peter A. G. Crosthwaite | const char *part_name; |
51 | 82a24990 | Peter A. G. Crosthwaite | /* jedec code. (jedec >> 16) & 0xff is the 1st byte, >> 8 the 2nd etc */
|
52 | 82a24990 | Peter A. G. Crosthwaite | uint32_t jedec; |
53 | 82a24990 | Peter A. G. Crosthwaite | /* extended jedec code */
|
54 | 82a24990 | Peter A. G. Crosthwaite | uint16_t ext_jedec; |
55 | 82a24990 | Peter A. G. Crosthwaite | /* there is confusion between manufacturers as to what a sector is. In this
|
56 | 82a24990 | Peter A. G. Crosthwaite | * device model, a "sector" is the size that is erased by the ERASE_SECTOR
|
57 | 82a24990 | Peter A. G. Crosthwaite | * command (opcode 0xd8).
|
58 | 82a24990 | Peter A. G. Crosthwaite | */
|
59 | 82a24990 | Peter A. G. Crosthwaite | uint32_t sector_size; |
60 | 82a24990 | Peter A. G. Crosthwaite | uint32_t n_sectors; |
61 | 82a24990 | Peter A. G. Crosthwaite | uint32_t page_size; |
62 | 82a24990 | Peter A. G. Crosthwaite | uint8_t flags; |
63 | 82a24990 | Peter A. G. Crosthwaite | } FlashPartInfo; |
64 | 82a24990 | Peter A. G. Crosthwaite | |
65 | 82a24990 | Peter A. G. Crosthwaite | /* adapted from linux */
|
66 | 82a24990 | Peter A. G. Crosthwaite | |
67 | 82a24990 | Peter A. G. Crosthwaite | #define INFO(_part_name, _jedec, _ext_jedec, _sector_size, _n_sectors, _flags)\
|
68 | 82a24990 | Peter A. G. Crosthwaite | .part_name = (_part_name),\ |
69 | 82a24990 | Peter A. G. Crosthwaite | .jedec = (_jedec),\ |
70 | 82a24990 | Peter A. G. Crosthwaite | .ext_jedec = (_ext_jedec),\ |
71 | 82a24990 | Peter A. G. Crosthwaite | .sector_size = (_sector_size),\ |
72 | 82a24990 | Peter A. G. Crosthwaite | .n_sectors = (_n_sectors),\ |
73 | 82a24990 | Peter A. G. Crosthwaite | .page_size = 256,\
|
74 | 82a24990 | Peter A. G. Crosthwaite | .flags = (_flags),\ |
75 | 82a24990 | Peter A. G. Crosthwaite | |
76 | 419336a9 | Peter Crosthwaite | #define JEDEC_NUMONYX 0x20 |
77 | 419336a9 | Peter Crosthwaite | #define JEDEC_WINBOND 0xEF |
78 | 419336a9 | Peter Crosthwaite | #define JEDEC_SPANSION 0x01 |
79 | 419336a9 | Peter Crosthwaite | |
80 | 82a24990 | Peter A. G. Crosthwaite | static const FlashPartInfo known_devices[] = { |
81 | 82a24990 | Peter A. G. Crosthwaite | /* Atmel -- some are (confusingly) marketed as "DataFlash" */
|
82 | 82a24990 | Peter A. G. Crosthwaite | { INFO("at25fs010", 0x1f6601, 0, 32 << 10, 4, ER_4K) }, |
83 | 82a24990 | Peter A. G. Crosthwaite | { INFO("at25fs040", 0x1f6604, 0, 64 << 10, 8, ER_4K) }, |
84 | 82a24990 | Peter A. G. Crosthwaite | |
85 | 82a24990 | Peter A. G. Crosthwaite | { INFO("at25df041a", 0x1f4401, 0, 64 << 10, 8, ER_4K) }, |
86 | 82a24990 | Peter A. G. Crosthwaite | { INFO("at25df321a", 0x1f4701, 0, 64 << 10, 64, ER_4K) }, |
87 | 82a24990 | Peter A. G. Crosthwaite | { INFO("at25df641", 0x1f4800, 0, 64 << 10, 128, ER_4K) }, |
88 | 82a24990 | Peter A. G. Crosthwaite | |
89 | 82a24990 | Peter A. G. Crosthwaite | { INFO("at26f004", 0x1f0400, 0, 64 << 10, 8, ER_4K) }, |
90 | 82a24990 | Peter A. G. Crosthwaite | { INFO("at26df081a", 0x1f4501, 0, 64 << 10, 16, ER_4K) }, |
91 | 82a24990 | Peter A. G. Crosthwaite | { INFO("at26df161a", 0x1f4601, 0, 64 << 10, 32, ER_4K) }, |
92 | 82a24990 | Peter A. G. Crosthwaite | { INFO("at26df321", 0x1f4700, 0, 64 << 10, 64, ER_4K) }, |
93 | 82a24990 | Peter A. G. Crosthwaite | |
94 | 3e758c1d | Ed Maste | { INFO("at45db081d", 0x1f2500, 0, 64 << 10, 16, ER_4K) }, |
95 | 3e758c1d | Ed Maste | |
96 | 82a24990 | Peter A. G. Crosthwaite | /* EON -- en25xxx */
|
97 | 82a24990 | Peter A. G. Crosthwaite | { INFO("en25f32", 0x1c3116, 0, 64 << 10, 64, ER_4K) }, |
98 | 82a24990 | Peter A. G. Crosthwaite | { INFO("en25p32", 0x1c2016, 0, 64 << 10, 64, 0) }, |
99 | 82a24990 | Peter A. G. Crosthwaite | { INFO("en25q32b", 0x1c3016, 0, 64 << 10, 64, 0) }, |
100 | 82a24990 | Peter A. G. Crosthwaite | { INFO("en25p64", 0x1c2017, 0, 64 << 10, 128, 0) }, |
101 | 3e758c1d | Ed Maste | { INFO("en25q64", 0x1c3017, 0, 64 << 10, 128, ER_4K) }, |
102 | 3e758c1d | Ed Maste | |
103 | 3e758c1d | Ed Maste | /* GigaDevice */
|
104 | 3e758c1d | Ed Maste | { INFO("gd25q32", 0xc84016, 0, 64 << 10, 64, ER_4K) }, |
105 | 3e758c1d | Ed Maste | { INFO("gd25q64", 0xc84017, 0, 64 << 10, 128, ER_4K) }, |
106 | 82a24990 | Peter A. G. Crosthwaite | |
107 | 82a24990 | Peter A. G. Crosthwaite | /* Intel/Numonyx -- xxxs33b */
|
108 | 82a24990 | Peter A. G. Crosthwaite | { INFO("160s33b", 0x898911, 0, 64 << 10, 32, 0) }, |
109 | 82a24990 | Peter A. G. Crosthwaite | { INFO("320s33b", 0x898912, 0, 64 << 10, 64, 0) }, |
110 | 82a24990 | Peter A. G. Crosthwaite | { INFO("640s33b", 0x898913, 0, 64 << 10, 128, 0) }, |
111 | 3e758c1d | Ed Maste | { INFO("n25q064", 0x20ba17, 0, 64 << 10, 128, 0) }, |
112 | 82a24990 | Peter A. G. Crosthwaite | |
113 | 82a24990 | Peter A. G. Crosthwaite | /* Macronix */
|
114 | 3e758c1d | Ed Maste | { INFO("mx25l2005a", 0xc22012, 0, 64 << 10, 4, ER_4K) }, |
115 | 82a24990 | Peter A. G. Crosthwaite | { INFO("mx25l4005a", 0xc22013, 0, 64 << 10, 8, ER_4K) }, |
116 | 82a24990 | Peter A. G. Crosthwaite | { INFO("mx25l8005", 0xc22014, 0, 64 << 10, 16, 0) }, |
117 | 82a24990 | Peter A. G. Crosthwaite | { INFO("mx25l1606e", 0xc22015, 0, 64 << 10, 32, ER_4K) }, |
118 | 82a24990 | Peter A. G. Crosthwaite | { INFO("mx25l3205d", 0xc22016, 0, 64 << 10, 64, 0) }, |
119 | 82a24990 | Peter A. G. Crosthwaite | { INFO("mx25l6405d", 0xc22017, 0, 64 << 10, 128, 0) }, |
120 | 82a24990 | Peter A. G. Crosthwaite | { INFO("mx25l12805d", 0xc22018, 0, 64 << 10, 256, 0) }, |
121 | 82a24990 | Peter A. G. Crosthwaite | { INFO("mx25l12855e", 0xc22618, 0, 64 << 10, 256, 0) }, |
122 | 82a24990 | Peter A. G. Crosthwaite | { INFO("mx25l25635e", 0xc22019, 0, 64 << 10, 512, 0) }, |
123 | 82a24990 | Peter A. G. Crosthwaite | { INFO("mx25l25655e", 0xc22619, 0, 64 << 10, 512, 0) }, |
124 | 82a24990 | Peter A. G. Crosthwaite | |
125 | 3e758c1d | Ed Maste | /* Micron */
|
126 | f5aac8e0 | Ed Maste | { INFO("n25q032a11", 0x20bb16, 0, 64 << 10, 64, ER_4K) }, |
127 | f5aac8e0 | Ed Maste | { INFO("n25q032a13", 0x20ba16, 0, 64 << 10, 64, ER_4K) }, |
128 | f5aac8e0 | Ed Maste | { INFO("n25q064a11", 0x20bb17, 0, 64 << 10, 128, ER_4K) }, |
129 | f5aac8e0 | Ed Maste | { INFO("n25q064a13", 0x20ba17, 0, 64 << 10, 128, ER_4K) }, |
130 | f5aac8e0 | Ed Maste | { INFO("n25q128a11", 0x20bb18, 0, 64 << 10, 256, ER_4K) }, |
131 | f5aac8e0 | Ed Maste | { INFO("n25q128a13", 0x20ba18, 0, 64 << 10, 256, ER_4K) }, |
132 | f5aac8e0 | Ed Maste | { INFO("n25q256a11", 0x20bb19, 0, 64 << 10, 512, ER_4K) }, |
133 | f5aac8e0 | Ed Maste | { INFO("n25q256a13", 0x20ba19, 0, 64 << 10, 512, ER_4K) }, |
134 | 3e758c1d | Ed Maste | |
135 | 82a24990 | Peter A. G. Crosthwaite | /* Spansion -- single (large) sector size only, at least
|
136 | 82a24990 | Peter A. G. Crosthwaite | * for the chips listed here (without boot sectors).
|
137 | 82a24990 | Peter A. G. Crosthwaite | */
|
138 | 82a24990 | Peter A. G. Crosthwaite | { INFO("s25sl032p", 0x010215, 0x4d00, 64 << 10, 64, ER_4K) }, |
139 | 3e758c1d | Ed Maste | { INFO("s25sl064p", 0x010216, 0x4d00, 64 << 10, 128, ER_4K) }, |
140 | 82a24990 | Peter A. G. Crosthwaite | { INFO("s25fl256s0", 0x010219, 0x4d00, 256 << 10, 128, 0) }, |
141 | 82a24990 | Peter A. G. Crosthwaite | { INFO("s25fl256s1", 0x010219, 0x4d01, 64 << 10, 512, 0) }, |
142 | 82a24990 | Peter A. G. Crosthwaite | { INFO("s25fl512s", 0x010220, 0x4d00, 256 << 10, 256, 0) }, |
143 | 82a24990 | Peter A. G. Crosthwaite | { INFO("s70fl01gs", 0x010221, 0x4d00, 256 << 10, 256, 0) }, |
144 | 82a24990 | Peter A. G. Crosthwaite | { INFO("s25sl12800", 0x012018, 0x0300, 256 << 10, 64, 0) }, |
145 | 82a24990 | Peter A. G. Crosthwaite | { INFO("s25sl12801", 0x012018, 0x0301, 64 << 10, 256, 0) }, |
146 | 82a24990 | Peter A. G. Crosthwaite | { INFO("s25fl129p0", 0x012018, 0x4d00, 256 << 10, 64, 0) }, |
147 | 82a24990 | Peter A. G. Crosthwaite | { INFO("s25fl129p1", 0x012018, 0x4d01, 64 << 10, 256, 0) }, |
148 | 3e758c1d | Ed Maste | { INFO("s25sl004a", 0x010212, 0, 64 << 10, 8, 0) }, |
149 | 3e758c1d | Ed Maste | { INFO("s25sl008a", 0x010213, 0, 64 << 10, 16, 0) }, |
150 | 3e758c1d | Ed Maste | { INFO("s25sl016a", 0x010214, 0, 64 << 10, 32, 0) }, |
151 | 3e758c1d | Ed Maste | { INFO("s25sl032a", 0x010215, 0, 64 << 10, 64, 0) }, |
152 | 3e758c1d | Ed Maste | { INFO("s25sl064a", 0x010216, 0, 64 << 10, 128, 0) }, |
153 | 82a24990 | Peter A. G. Crosthwaite | { INFO("s25fl016k", 0xef4015, 0, 64 << 10, 32, ER_4K | ER_32K) }, |
154 | 82a24990 | Peter A. G. Crosthwaite | { INFO("s25fl064k", 0xef4017, 0, 64 << 10, 128, ER_4K | ER_32K) }, |
155 | 82a24990 | Peter A. G. Crosthwaite | |
156 | 82a24990 | Peter A. G. Crosthwaite | /* SST -- large erase sizes are "overlays", "sectors" are 4<< 10 */
|
157 | 82a24990 | Peter A. G. Crosthwaite | { INFO("sst25vf040b", 0xbf258d, 0, 64 << 10, 8, ER_4K) }, |
158 | 82a24990 | Peter A. G. Crosthwaite | { INFO("sst25vf080b", 0xbf258e, 0, 64 << 10, 16, ER_4K) }, |
159 | 82a24990 | Peter A. G. Crosthwaite | { INFO("sst25vf016b", 0xbf2541, 0, 64 << 10, 32, ER_4K) }, |
160 | 82a24990 | Peter A. G. Crosthwaite | { INFO("sst25vf032b", 0xbf254a, 0, 64 << 10, 64, ER_4K) }, |
161 | 82a24990 | Peter A. G. Crosthwaite | { INFO("sst25wf512", 0xbf2501, 0, 64 << 10, 1, ER_4K) }, |
162 | 82a24990 | Peter A. G. Crosthwaite | { INFO("sst25wf010", 0xbf2502, 0, 64 << 10, 2, ER_4K) }, |
163 | 82a24990 | Peter A. G. Crosthwaite | { INFO("sst25wf020", 0xbf2503, 0, 64 << 10, 4, ER_4K) }, |
164 | 82a24990 | Peter A. G. Crosthwaite | { INFO("sst25wf040", 0xbf2504, 0, 64 << 10, 8, ER_4K) }, |
165 | 82a24990 | Peter A. G. Crosthwaite | |
166 | 82a24990 | Peter A. G. Crosthwaite | /* ST Microelectronics -- newer production may have feature updates */
|
167 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25p05", 0x202010, 0, 32 << 10, 2, 0) }, |
168 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25p10", 0x202011, 0, 32 << 10, 4, 0) }, |
169 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25p20", 0x202012, 0, 64 << 10, 4, 0) }, |
170 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25p40", 0x202013, 0, 64 << 10, 8, 0) }, |
171 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25p80", 0x202014, 0, 64 << 10, 16, 0) }, |
172 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25p16", 0x202015, 0, 64 << 10, 32, 0) }, |
173 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25p32", 0x202016, 0, 64 << 10, 64, 0) }, |
174 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25p64", 0x202017, 0, 64 << 10, 128, 0) }, |
175 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25p128", 0x202018, 0, 256 << 10, 64, 0) }, |
176 | 3e758c1d | Ed Maste | { INFO("n25q032", 0x20ba16, 0, 64 << 10, 64, 0) }, |
177 | 82a24990 | Peter A. G. Crosthwaite | |
178 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m45pe10", 0x204011, 0, 64 << 10, 2, 0) }, |
179 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m45pe80", 0x204014, 0, 64 << 10, 16, 0) }, |
180 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m45pe16", 0x204015, 0, 64 << 10, 32, 0) }, |
181 | 82a24990 | Peter A. G. Crosthwaite | |
182 | 3e758c1d | Ed Maste | { INFO("m25pe20", 0x208012, 0, 64 << 10, 4, 0) }, |
183 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25pe80", 0x208014, 0, 64 << 10, 16, 0) }, |
184 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25pe16", 0x208015, 0, 64 << 10, 32, ER_4K) }, |
185 | 82a24990 | Peter A. G. Crosthwaite | |
186 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25px32", 0x207116, 0, 64 << 10, 64, ER_4K) }, |
187 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25px32-s0", 0x207316, 0, 64 << 10, 64, ER_4K) }, |
188 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25px32-s1", 0x206316, 0, 64 << 10, 64, ER_4K) }, |
189 | 82a24990 | Peter A. G. Crosthwaite | { INFO("m25px64", 0x207117, 0, 64 << 10, 128, 0) }, |
190 | 82a24990 | Peter A. G. Crosthwaite | |
191 | 82a24990 | Peter A. G. Crosthwaite | /* Winbond -- w25x "blocks" are 64k, "sectors" are 4KiB */
|
192 | 82a24990 | Peter A. G. Crosthwaite | { INFO("w25x10", 0xef3011, 0, 64 << 10, 2, ER_4K) }, |
193 | 82a24990 | Peter A. G. Crosthwaite | { INFO("w25x20", 0xef3012, 0, 64 << 10, 4, ER_4K) }, |
194 | 82a24990 | Peter A. G. Crosthwaite | { INFO("w25x40", 0xef3013, 0, 64 << 10, 8, ER_4K) }, |
195 | 82a24990 | Peter A. G. Crosthwaite | { INFO("w25x80", 0xef3014, 0, 64 << 10, 16, ER_4K) }, |
196 | 82a24990 | Peter A. G. Crosthwaite | { INFO("w25x16", 0xef3015, 0, 64 << 10, 32, ER_4K) }, |
197 | 82a24990 | Peter A. G. Crosthwaite | { INFO("w25x32", 0xef3016, 0, 64 << 10, 64, ER_4K) }, |
198 | 82a24990 | Peter A. G. Crosthwaite | { INFO("w25q32", 0xef4016, 0, 64 << 10, 64, ER_4K) }, |
199 | 3e758c1d | Ed Maste | { INFO("w25q32dw", 0xef6016, 0, 64 << 10, 64, ER_4K) }, |
200 | 82a24990 | Peter A. G. Crosthwaite | { INFO("w25x64", 0xef3017, 0, 64 << 10, 128, ER_4K) }, |
201 | 82a24990 | Peter A. G. Crosthwaite | { INFO("w25q64", 0xef4017, 0, 64 << 10, 128, ER_4K) }, |
202 | 3e758c1d | Ed Maste | { INFO("w25q80", 0xef5014, 0, 64 << 10, 16, ER_4K) }, |
203 | 3e758c1d | Ed Maste | { INFO("w25q80bl", 0xef4014, 0, 64 << 10, 16, ER_4K) }, |
204 | 3e758c1d | Ed Maste | { INFO("w25q256", 0xef4019, 0, 64 << 10, 512, ER_4K) }, |
205 | 82a24990 | Peter A. G. Crosthwaite | |
206 | 82a24990 | Peter A. G. Crosthwaite | /* Numonyx -- n25q128 */
|
207 | 82a24990 | Peter A. G. Crosthwaite | { INFO("n25q128", 0x20ba18, 0, 64 << 10, 256, 0) }, |
208 | 82a24990 | Peter A. G. Crosthwaite | }; |
209 | 82a24990 | Peter A. G. Crosthwaite | |
210 | 82a24990 | Peter A. G. Crosthwaite | typedef enum { |
211 | 82a24990 | Peter A. G. Crosthwaite | NOP = 0,
|
212 | 03ec2f83 | Kuo-Jung Su | WRSR = 0x1,
|
213 | 82a24990 | Peter A. G. Crosthwaite | WRDI = 0x4,
|
214 | 82a24990 | Peter A. G. Crosthwaite | RDSR = 0x5,
|
215 | 82a24990 | Peter A. G. Crosthwaite | WREN = 0x6,
|
216 | 419336a9 | Peter Crosthwaite | JEDEC_READ = 0x9f,
|
217 | 419336a9 | Peter Crosthwaite | BULK_ERASE = 0xc7,
|
218 | 419336a9 | Peter Crosthwaite | |
219 | 419336a9 | Peter Crosthwaite | READ = 0x3,
|
220 | 82a24990 | Peter A. G. Crosthwaite | FAST_READ = 0xb,
|
221 | 419336a9 | Peter Crosthwaite | DOR = 0x3b,
|
222 | 419336a9 | Peter Crosthwaite | QOR = 0x6b,
|
223 | 419336a9 | Peter Crosthwaite | DIOR = 0xbb,
|
224 | 419336a9 | Peter Crosthwaite | QIOR = 0xeb,
|
225 | 419336a9 | Peter Crosthwaite | |
226 | 419336a9 | Peter Crosthwaite | PP = 0x2,
|
227 | 419336a9 | Peter Crosthwaite | DPP = 0xa2,
|
228 | 419336a9 | Peter Crosthwaite | QPP = 0x32,
|
229 | 419336a9 | Peter Crosthwaite | |
230 | 82a24990 | Peter A. G. Crosthwaite | ERASE_4K = 0x20,
|
231 | 82a24990 | Peter A. G. Crosthwaite | ERASE_32K = 0x52,
|
232 | 82a24990 | Peter A. G. Crosthwaite | ERASE_SECTOR = 0xd8,
|
233 | 82a24990 | Peter A. G. Crosthwaite | } FlashCMD; |
234 | 82a24990 | Peter A. G. Crosthwaite | |
235 | 82a24990 | Peter A. G. Crosthwaite | typedef enum { |
236 | 82a24990 | Peter A. G. Crosthwaite | STATE_IDLE, |
237 | 82a24990 | Peter A. G. Crosthwaite | STATE_PAGE_PROGRAM, |
238 | 82a24990 | Peter A. G. Crosthwaite | STATE_READ, |
239 | 82a24990 | Peter A. G. Crosthwaite | STATE_COLLECTING_DATA, |
240 | 82a24990 | Peter A. G. Crosthwaite | STATE_READING_DATA, |
241 | 82a24990 | Peter A. G. Crosthwaite | } CMDState; |
242 | 82a24990 | Peter A. G. Crosthwaite | |
243 | 82a24990 | Peter A. G. Crosthwaite | typedef struct Flash { |
244 | 82a24990 | Peter A. G. Crosthwaite | SSISlave ssidev; |
245 | 82a24990 | Peter A. G. Crosthwaite | uint32_t r; |
246 | 82a24990 | Peter A. G. Crosthwaite | |
247 | 82a24990 | Peter A. G. Crosthwaite | BlockDriverState *bdrv; |
248 | 82a24990 | Peter A. G. Crosthwaite | |
249 | 82a24990 | Peter A. G. Crosthwaite | uint8_t *storage; |
250 | 82a24990 | Peter A. G. Crosthwaite | uint32_t size; |
251 | 82a24990 | Peter A. G. Crosthwaite | int page_size;
|
252 | 82a24990 | Peter A. G. Crosthwaite | |
253 | 82a24990 | Peter A. G. Crosthwaite | uint8_t state; |
254 | 82a24990 | Peter A. G. Crosthwaite | uint8_t data[16];
|
255 | 82a24990 | Peter A. G. Crosthwaite | uint32_t len; |
256 | 82a24990 | Peter A. G. Crosthwaite | uint32_t pos; |
257 | 82a24990 | Peter A. G. Crosthwaite | uint8_t needed_bytes; |
258 | 82a24990 | Peter A. G. Crosthwaite | uint8_t cmd_in_progress; |
259 | 82a24990 | Peter A. G. Crosthwaite | uint64_t cur_addr; |
260 | 82a24990 | Peter A. G. Crosthwaite | bool write_enable;
|
261 | 82a24990 | Peter A. G. Crosthwaite | |
262 | 82a24990 | Peter A. G. Crosthwaite | int64_t dirty_page; |
263 | 82a24990 | Peter A. G. Crosthwaite | |
264 | 82a24990 | Peter A. G. Crosthwaite | const FlashPartInfo *pi;
|
265 | 82a24990 | Peter A. G. Crosthwaite | |
266 | 82a24990 | Peter A. G. Crosthwaite | } Flash; |
267 | 82a24990 | Peter A. G. Crosthwaite | |
268 | a7fd6915 | Peter Crosthwaite | typedef struct M25P80Class { |
269 | a7fd6915 | Peter Crosthwaite | SSISlaveClass parent_class; |
270 | a7fd6915 | Peter Crosthwaite | FlashPartInfo *pi; |
271 | a7fd6915 | Peter Crosthwaite | } M25P80Class; |
272 | a7fd6915 | Peter Crosthwaite | |
273 | a7fd6915 | Peter Crosthwaite | #define TYPE_M25P80 "m25p80-generic" |
274 | a7fd6915 | Peter Crosthwaite | #define M25P80(obj) \
|
275 | a7fd6915 | Peter Crosthwaite | OBJECT_CHECK(Flash, (obj), TYPE_M25P80) |
276 | a7fd6915 | Peter Crosthwaite | #define M25P80_CLASS(klass) \
|
277 | a7fd6915 | Peter Crosthwaite | OBJECT_CLASS_CHECK(M25P80Class, (klass), TYPE_M25P80) |
278 | a7fd6915 | Peter Crosthwaite | #define M25P80_GET_CLASS(obj) \
|
279 | a7fd6915 | Peter Crosthwaite | OBJECT_GET_CLASS(M25P80Class, (obj), TYPE_M25P80) |
280 | a7fd6915 | Peter Crosthwaite | |
281 | 82a24990 | Peter A. G. Crosthwaite | static void bdrv_sync_complete(void *opaque, int ret) |
282 | 82a24990 | Peter A. G. Crosthwaite | { |
283 | 82a24990 | Peter A. G. Crosthwaite | /* do nothing. Masters do not directly interact with the backing store,
|
284 | 82a24990 | Peter A. G. Crosthwaite | * only the working copy so no mutexing required.
|
285 | 82a24990 | Peter A. G. Crosthwaite | */
|
286 | 82a24990 | Peter A. G. Crosthwaite | } |
287 | 82a24990 | Peter A. G. Crosthwaite | |
288 | 82a24990 | Peter A. G. Crosthwaite | static void flash_sync_page(Flash *s, int page) |
289 | 82a24990 | Peter A. G. Crosthwaite | { |
290 | 82a24990 | Peter A. G. Crosthwaite | if (s->bdrv) {
|
291 | 82a24990 | Peter A. G. Crosthwaite | int bdrv_sector, nb_sectors;
|
292 | 82a24990 | Peter A. G. Crosthwaite | QEMUIOVector iov; |
293 | 82a24990 | Peter A. G. Crosthwaite | |
294 | 82a24990 | Peter A. G. Crosthwaite | bdrv_sector = (page * s->pi->page_size) / BDRV_SECTOR_SIZE; |
295 | 82a24990 | Peter A. G. Crosthwaite | nb_sectors = DIV_ROUND_UP(s->pi->page_size, BDRV_SECTOR_SIZE); |
296 | 82a24990 | Peter A. G. Crosthwaite | qemu_iovec_init(&iov, 1);
|
297 | 82a24990 | Peter A. G. Crosthwaite | qemu_iovec_add(&iov, s->storage + bdrv_sector * BDRV_SECTOR_SIZE, |
298 | 82a24990 | Peter A. G. Crosthwaite | nb_sectors * BDRV_SECTOR_SIZE); |
299 | 82a24990 | Peter A. G. Crosthwaite | bdrv_aio_writev(s->bdrv, bdrv_sector, &iov, nb_sectors, |
300 | 82a24990 | Peter A. G. Crosthwaite | bdrv_sync_complete, NULL);
|
301 | 82a24990 | Peter A. G. Crosthwaite | } |
302 | 82a24990 | Peter A. G. Crosthwaite | } |
303 | 82a24990 | Peter A. G. Crosthwaite | |
304 | 82a24990 | Peter A. G. Crosthwaite | static inline void flash_sync_area(Flash *s, int64_t off, int64_t len) |
305 | 82a24990 | Peter A. G. Crosthwaite | { |
306 | 82a24990 | Peter A. G. Crosthwaite | int64_t start, end, nb_sectors; |
307 | 82a24990 | Peter A. G. Crosthwaite | QEMUIOVector iov; |
308 | 82a24990 | Peter A. G. Crosthwaite | |
309 | 82a24990 | Peter A. G. Crosthwaite | if (!s->bdrv) {
|
310 | 82a24990 | Peter A. G. Crosthwaite | return;
|
311 | 82a24990 | Peter A. G. Crosthwaite | } |
312 | 82a24990 | Peter A. G. Crosthwaite | |
313 | 82a24990 | Peter A. G. Crosthwaite | assert(!(len % BDRV_SECTOR_SIZE)); |
314 | 82a24990 | Peter A. G. Crosthwaite | start = off / BDRV_SECTOR_SIZE; |
315 | 82a24990 | Peter A. G. Crosthwaite | end = (off + len) / BDRV_SECTOR_SIZE; |
316 | 82a24990 | Peter A. G. Crosthwaite | nb_sectors = end - start; |
317 | 82a24990 | Peter A. G. Crosthwaite | qemu_iovec_init(&iov, 1);
|
318 | 82a24990 | Peter A. G. Crosthwaite | qemu_iovec_add(&iov, s->storage + (start * BDRV_SECTOR_SIZE), |
319 | 82a24990 | Peter A. G. Crosthwaite | nb_sectors * BDRV_SECTOR_SIZE); |
320 | 82a24990 | Peter A. G. Crosthwaite | bdrv_aio_writev(s->bdrv, start, &iov, nb_sectors, bdrv_sync_complete, NULL);
|
321 | 82a24990 | Peter A. G. Crosthwaite | } |
322 | 82a24990 | Peter A. G. Crosthwaite | |
323 | 82a24990 | Peter A. G. Crosthwaite | static void flash_erase(Flash *s, int offset, FlashCMD cmd) |
324 | 82a24990 | Peter A. G. Crosthwaite | { |
325 | 82a24990 | Peter A. G. Crosthwaite | uint32_t len; |
326 | 82a24990 | Peter A. G. Crosthwaite | uint8_t capa_to_assert = 0;
|
327 | 82a24990 | Peter A. G. Crosthwaite | |
328 | 82a24990 | Peter A. G. Crosthwaite | switch (cmd) {
|
329 | 82a24990 | Peter A. G. Crosthwaite | case ERASE_4K:
|
330 | 82a24990 | Peter A. G. Crosthwaite | len = 4 << 10; |
331 | 82a24990 | Peter A. G. Crosthwaite | capa_to_assert = ER_4K; |
332 | 82a24990 | Peter A. G. Crosthwaite | break;
|
333 | 82a24990 | Peter A. G. Crosthwaite | case ERASE_32K:
|
334 | 82a24990 | Peter A. G. Crosthwaite | len = 32 << 10; |
335 | 82a24990 | Peter A. G. Crosthwaite | capa_to_assert = ER_32K; |
336 | 82a24990 | Peter A. G. Crosthwaite | break;
|
337 | 82a24990 | Peter A. G. Crosthwaite | case ERASE_SECTOR:
|
338 | 82a24990 | Peter A. G. Crosthwaite | len = s->pi->sector_size; |
339 | 82a24990 | Peter A. G. Crosthwaite | break;
|
340 | 82a24990 | Peter A. G. Crosthwaite | case BULK_ERASE:
|
341 | 82a24990 | Peter A. G. Crosthwaite | len = s->size; |
342 | 82a24990 | Peter A. G. Crosthwaite | break;
|
343 | 82a24990 | Peter A. G. Crosthwaite | default:
|
344 | 82a24990 | Peter A. G. Crosthwaite | abort(); |
345 | 82a24990 | Peter A. G. Crosthwaite | } |
346 | 82a24990 | Peter A. G. Crosthwaite | |
347 | 28097d02 | Peter Crosthwaite | DB_PRINT_L(0, "offset = %#x, len = %d\n", offset, len); |
348 | 82a24990 | Peter A. G. Crosthwaite | if ((s->pi->flags & capa_to_assert) != capa_to_assert) {
|
349 | e9711b4d | Peter Crosthwaite | qemu_log_mask(LOG_GUEST_ERROR, "M25P80: %d erase size not supported by"
|
350 | e9711b4d | Peter Crosthwaite | " device\n", len);
|
351 | 82a24990 | Peter A. G. Crosthwaite | } |
352 | 82a24990 | Peter A. G. Crosthwaite | |
353 | 82a24990 | Peter A. G. Crosthwaite | if (!s->write_enable) {
|
354 | e9711b4d | Peter Crosthwaite | qemu_log_mask(LOG_GUEST_ERROR, "M25P80: erase with write protect!\n");
|
355 | 82a24990 | Peter A. G. Crosthwaite | return;
|
356 | 82a24990 | Peter A. G. Crosthwaite | } |
357 | 82a24990 | Peter A. G. Crosthwaite | memset(s->storage + offset, 0xff, len);
|
358 | 82a24990 | Peter A. G. Crosthwaite | flash_sync_area(s, offset, len); |
359 | 82a24990 | Peter A. G. Crosthwaite | } |
360 | 82a24990 | Peter A. G. Crosthwaite | |
361 | 82a24990 | Peter A. G. Crosthwaite | static inline void flash_sync_dirty(Flash *s, int64_t newpage) |
362 | 82a24990 | Peter A. G. Crosthwaite | { |
363 | 82a24990 | Peter A. G. Crosthwaite | if (s->dirty_page >= 0 && s->dirty_page != newpage) { |
364 | 82a24990 | Peter A. G. Crosthwaite | flash_sync_page(s, s->dirty_page); |
365 | 82a24990 | Peter A. G. Crosthwaite | s->dirty_page = newpage; |
366 | 82a24990 | Peter A. G. Crosthwaite | } |
367 | 82a24990 | Peter A. G. Crosthwaite | } |
368 | 82a24990 | Peter A. G. Crosthwaite | |
369 | 82a24990 | Peter A. G. Crosthwaite | static inline |
370 | 82a24990 | Peter A. G. Crosthwaite | void flash_write8(Flash *s, uint64_t addr, uint8_t data)
|
371 | 82a24990 | Peter A. G. Crosthwaite | { |
372 | 82a24990 | Peter A. G. Crosthwaite | int64_t page = addr / s->pi->page_size; |
373 | 82a24990 | Peter A. G. Crosthwaite | uint8_t prev = s->storage[s->cur_addr]; |
374 | 82a24990 | Peter A. G. Crosthwaite | |
375 | 82a24990 | Peter A. G. Crosthwaite | if (!s->write_enable) {
|
376 | e9711b4d | Peter Crosthwaite | qemu_log_mask(LOG_GUEST_ERROR, "M25P80: write with write protect!\n");
|
377 | 82a24990 | Peter A. G. Crosthwaite | } |
378 | 82a24990 | Peter A. G. Crosthwaite | |
379 | 82a24990 | Peter A. G. Crosthwaite | if ((prev ^ data) & data) {
|
380 | 28097d02 | Peter Crosthwaite | DB_PRINT_L(1, "programming zero to one! addr=%" PRIx64 " %" PRIx8 |
381 | 28097d02 | Peter Crosthwaite | " -> %" PRIx8 "\n", addr, prev, data); |
382 | 82a24990 | Peter A. G. Crosthwaite | } |
383 | 82a24990 | Peter A. G. Crosthwaite | |
384 | 82a24990 | Peter A. G. Crosthwaite | if (s->pi->flags & WR_1) {
|
385 | 82a24990 | Peter A. G. Crosthwaite | s->storage[s->cur_addr] = data; |
386 | 82a24990 | Peter A. G. Crosthwaite | } else {
|
387 | 82a24990 | Peter A. G. Crosthwaite | s->storage[s->cur_addr] &= data; |
388 | 82a24990 | Peter A. G. Crosthwaite | } |
389 | 82a24990 | Peter A. G. Crosthwaite | |
390 | 82a24990 | Peter A. G. Crosthwaite | flash_sync_dirty(s, page); |
391 | 82a24990 | Peter A. G. Crosthwaite | s->dirty_page = page; |
392 | 82a24990 | Peter A. G. Crosthwaite | } |
393 | 82a24990 | Peter A. G. Crosthwaite | |
394 | 82a24990 | Peter A. G. Crosthwaite | static void complete_collecting_data(Flash *s) |
395 | 82a24990 | Peter A. G. Crosthwaite | { |
396 | 82a24990 | Peter A. G. Crosthwaite | s->cur_addr = s->data[0] << 16; |
397 | 82a24990 | Peter A. G. Crosthwaite | s->cur_addr |= s->data[1] << 8; |
398 | 82a24990 | Peter A. G. Crosthwaite | s->cur_addr |= s->data[2];
|
399 | 82a24990 | Peter A. G. Crosthwaite | |
400 | a56d305a | Peter Crosthwaite | s->state = STATE_IDLE; |
401 | a56d305a | Peter Crosthwaite | |
402 | 82a24990 | Peter A. G. Crosthwaite | switch (s->cmd_in_progress) {
|
403 | 419336a9 | Peter Crosthwaite | case DPP:
|
404 | 419336a9 | Peter Crosthwaite | case QPP:
|
405 | 82a24990 | Peter A. G. Crosthwaite | case PP:
|
406 | 82a24990 | Peter A. G. Crosthwaite | s->state = STATE_PAGE_PROGRAM; |
407 | 82a24990 | Peter A. G. Crosthwaite | break;
|
408 | 82a24990 | Peter A. G. Crosthwaite | case READ:
|
409 | 82a24990 | Peter A. G. Crosthwaite | case FAST_READ:
|
410 | 419336a9 | Peter Crosthwaite | case DOR:
|
411 | 419336a9 | Peter Crosthwaite | case QOR:
|
412 | 419336a9 | Peter Crosthwaite | case DIOR:
|
413 | 419336a9 | Peter Crosthwaite | case QIOR:
|
414 | 82a24990 | Peter A. G. Crosthwaite | s->state = STATE_READ; |
415 | 82a24990 | Peter A. G. Crosthwaite | break;
|
416 | 82a24990 | Peter A. G. Crosthwaite | case ERASE_4K:
|
417 | 82a24990 | Peter A. G. Crosthwaite | case ERASE_32K:
|
418 | 82a24990 | Peter A. G. Crosthwaite | case ERASE_SECTOR:
|
419 | 82a24990 | Peter A. G. Crosthwaite | flash_erase(s, s->cur_addr, s->cmd_in_progress); |
420 | 82a24990 | Peter A. G. Crosthwaite | break;
|
421 | 03ec2f83 | Kuo-Jung Su | case WRSR:
|
422 | 03ec2f83 | Kuo-Jung Su | if (s->write_enable) {
|
423 | 03ec2f83 | Kuo-Jung Su | s->write_enable = false;
|
424 | 03ec2f83 | Kuo-Jung Su | } |
425 | 03ec2f83 | Kuo-Jung Su | break;
|
426 | 82a24990 | Peter A. G. Crosthwaite | default:
|
427 | 82a24990 | Peter A. G. Crosthwaite | break;
|
428 | 82a24990 | Peter A. G. Crosthwaite | } |
429 | 82a24990 | Peter A. G. Crosthwaite | } |
430 | 82a24990 | Peter A. G. Crosthwaite | |
431 | 82a24990 | Peter A. G. Crosthwaite | static void decode_new_cmd(Flash *s, uint32_t value) |
432 | 82a24990 | Peter A. G. Crosthwaite | { |
433 | 82a24990 | Peter A. G. Crosthwaite | s->cmd_in_progress = value; |
434 | 28097d02 | Peter Crosthwaite | DB_PRINT_L(0, "decoded new command:%x\n", value); |
435 | 82a24990 | Peter A. G. Crosthwaite | |
436 | 82a24990 | Peter A. G. Crosthwaite | switch (value) {
|
437 | 82a24990 | Peter A. G. Crosthwaite | |
438 | 82a24990 | Peter A. G. Crosthwaite | case ERASE_4K:
|
439 | 82a24990 | Peter A. G. Crosthwaite | case ERASE_32K:
|
440 | 82a24990 | Peter A. G. Crosthwaite | case ERASE_SECTOR:
|
441 | 82a24990 | Peter A. G. Crosthwaite | case READ:
|
442 | 419336a9 | Peter Crosthwaite | case DPP:
|
443 | 419336a9 | Peter Crosthwaite | case QPP:
|
444 | 82a24990 | Peter A. G. Crosthwaite | case PP:
|
445 | 82a24990 | Peter A. G. Crosthwaite | s->needed_bytes = 3;
|
446 | 82a24990 | Peter A. G. Crosthwaite | s->pos = 0;
|
447 | 82a24990 | Peter A. G. Crosthwaite | s->len = 0;
|
448 | 82a24990 | Peter A. G. Crosthwaite | s->state = STATE_COLLECTING_DATA; |
449 | 82a24990 | Peter A. G. Crosthwaite | break;
|
450 | 82a24990 | Peter A. G. Crosthwaite | |
451 | 82a24990 | Peter A. G. Crosthwaite | case FAST_READ:
|
452 | 419336a9 | Peter Crosthwaite | case DOR:
|
453 | 419336a9 | Peter Crosthwaite | case QOR:
|
454 | 82a24990 | Peter A. G. Crosthwaite | s->needed_bytes = 4;
|
455 | 82a24990 | Peter A. G. Crosthwaite | s->pos = 0;
|
456 | 82a24990 | Peter A. G. Crosthwaite | s->len = 0;
|
457 | 82a24990 | Peter A. G. Crosthwaite | s->state = STATE_COLLECTING_DATA; |
458 | 82a24990 | Peter A. G. Crosthwaite | break;
|
459 | 82a24990 | Peter A. G. Crosthwaite | |
460 | 419336a9 | Peter Crosthwaite | case DIOR:
|
461 | 419336a9 | Peter Crosthwaite | switch ((s->pi->jedec >> 16) & 0xFF) { |
462 | 419336a9 | Peter Crosthwaite | case JEDEC_WINBOND:
|
463 | 419336a9 | Peter Crosthwaite | case JEDEC_SPANSION:
|
464 | 419336a9 | Peter Crosthwaite | s->needed_bytes = 4;
|
465 | 419336a9 | Peter Crosthwaite | break;
|
466 | 419336a9 | Peter Crosthwaite | case JEDEC_NUMONYX:
|
467 | 419336a9 | Peter Crosthwaite | default:
|
468 | 419336a9 | Peter Crosthwaite | s->needed_bytes = 5;
|
469 | 419336a9 | Peter Crosthwaite | } |
470 | 419336a9 | Peter Crosthwaite | s->pos = 0;
|
471 | 419336a9 | Peter Crosthwaite | s->len = 0;
|
472 | 419336a9 | Peter Crosthwaite | s->state = STATE_COLLECTING_DATA; |
473 | 419336a9 | Peter Crosthwaite | break;
|
474 | 419336a9 | Peter Crosthwaite | |
475 | 419336a9 | Peter Crosthwaite | case QIOR:
|
476 | 419336a9 | Peter Crosthwaite | switch ((s->pi->jedec >> 16) & 0xFF) { |
477 | 419336a9 | Peter Crosthwaite | case JEDEC_WINBOND:
|
478 | 419336a9 | Peter Crosthwaite | case JEDEC_SPANSION:
|
479 | 419336a9 | Peter Crosthwaite | s->needed_bytes = 6;
|
480 | 419336a9 | Peter Crosthwaite | break;
|
481 | 419336a9 | Peter Crosthwaite | case JEDEC_NUMONYX:
|
482 | 419336a9 | Peter Crosthwaite | default:
|
483 | 419336a9 | Peter Crosthwaite | s->needed_bytes = 8;
|
484 | 419336a9 | Peter Crosthwaite | } |
485 | 419336a9 | Peter Crosthwaite | s->pos = 0;
|
486 | 419336a9 | Peter Crosthwaite | s->len = 0;
|
487 | 419336a9 | Peter Crosthwaite | s->state = STATE_COLLECTING_DATA; |
488 | 419336a9 | Peter Crosthwaite | break;
|
489 | 419336a9 | Peter Crosthwaite | |
490 | 03ec2f83 | Kuo-Jung Su | case WRSR:
|
491 | 03ec2f83 | Kuo-Jung Su | if (s->write_enable) {
|
492 | 03ec2f83 | Kuo-Jung Su | s->needed_bytes = 1;
|
493 | 03ec2f83 | Kuo-Jung Su | s->pos = 0;
|
494 | 03ec2f83 | Kuo-Jung Su | s->len = 0;
|
495 | 03ec2f83 | Kuo-Jung Su | s->state = STATE_COLLECTING_DATA; |
496 | 03ec2f83 | Kuo-Jung Su | } |
497 | 03ec2f83 | Kuo-Jung Su | break;
|
498 | 03ec2f83 | Kuo-Jung Su | |
499 | 82a24990 | Peter A. G. Crosthwaite | case WRDI:
|
500 | 82a24990 | Peter A. G. Crosthwaite | s->write_enable = false;
|
501 | 82a24990 | Peter A. G. Crosthwaite | break;
|
502 | 82a24990 | Peter A. G. Crosthwaite | case WREN:
|
503 | 82a24990 | Peter A. G. Crosthwaite | s->write_enable = true;
|
504 | 82a24990 | Peter A. G. Crosthwaite | break;
|
505 | 82a24990 | Peter A. G. Crosthwaite | |
506 | 82a24990 | Peter A. G. Crosthwaite | case RDSR:
|
507 | 82a24990 | Peter A. G. Crosthwaite | s->data[0] = (!!s->write_enable) << 1; |
508 | 82a24990 | Peter A. G. Crosthwaite | s->pos = 0;
|
509 | 82a24990 | Peter A. G. Crosthwaite | s->len = 1;
|
510 | 82a24990 | Peter A. G. Crosthwaite | s->state = STATE_READING_DATA; |
511 | 82a24990 | Peter A. G. Crosthwaite | break;
|
512 | 82a24990 | Peter A. G. Crosthwaite | |
513 | 82a24990 | Peter A. G. Crosthwaite | case JEDEC_READ:
|
514 | 28097d02 | Peter Crosthwaite | DB_PRINT_L(0, "populated jedec code\n"); |
515 | 82a24990 | Peter A. G. Crosthwaite | s->data[0] = (s->pi->jedec >> 16) & 0xff; |
516 | 82a24990 | Peter A. G. Crosthwaite | s->data[1] = (s->pi->jedec >> 8) & 0xff; |
517 | 82a24990 | Peter A. G. Crosthwaite | s->data[2] = s->pi->jedec & 0xff; |
518 | 82a24990 | Peter A. G. Crosthwaite | if (s->pi->ext_jedec) {
|
519 | 82a24990 | Peter A. G. Crosthwaite | s->data[3] = (s->pi->ext_jedec >> 8) & 0xff; |
520 | 82a24990 | Peter A. G. Crosthwaite | s->data[4] = s->pi->ext_jedec & 0xff; |
521 | 82a24990 | Peter A. G. Crosthwaite | s->len = 5;
|
522 | 82a24990 | Peter A. G. Crosthwaite | } else {
|
523 | 82a24990 | Peter A. G. Crosthwaite | s->len = 3;
|
524 | 82a24990 | Peter A. G. Crosthwaite | } |
525 | 82a24990 | Peter A. G. Crosthwaite | s->pos = 0;
|
526 | 82a24990 | Peter A. G. Crosthwaite | s->state = STATE_READING_DATA; |
527 | 82a24990 | Peter A. G. Crosthwaite | break;
|
528 | 82a24990 | Peter A. G. Crosthwaite | |
529 | 82a24990 | Peter A. G. Crosthwaite | case BULK_ERASE:
|
530 | 82a24990 | Peter A. G. Crosthwaite | if (s->write_enable) {
|
531 | 28097d02 | Peter Crosthwaite | DB_PRINT_L(0, "chip erase\n"); |
532 | 82a24990 | Peter A. G. Crosthwaite | flash_erase(s, 0, BULK_ERASE);
|
533 | 82a24990 | Peter A. G. Crosthwaite | } else {
|
534 | e9711b4d | Peter Crosthwaite | qemu_log_mask(LOG_GUEST_ERROR, "M25P80: chip erase with write "
|
535 | e9711b4d | Peter Crosthwaite | "protect!\n");
|
536 | 82a24990 | Peter A. G. Crosthwaite | } |
537 | 82a24990 | Peter A. G. Crosthwaite | break;
|
538 | 82a24990 | Peter A. G. Crosthwaite | case NOP:
|
539 | 82a24990 | Peter A. G. Crosthwaite | break;
|
540 | 82a24990 | Peter A. G. Crosthwaite | default:
|
541 | e9711b4d | Peter Crosthwaite | qemu_log_mask(LOG_GUEST_ERROR, "M25P80: Unknown cmd %x\n", value);
|
542 | 82a24990 | Peter A. G. Crosthwaite | break;
|
543 | 82a24990 | Peter A. G. Crosthwaite | } |
544 | 82a24990 | Peter A. G. Crosthwaite | } |
545 | 82a24990 | Peter A. G. Crosthwaite | |
546 | 82a24990 | Peter A. G. Crosthwaite | static int m25p80_cs(SSISlave *ss, bool select) |
547 | 82a24990 | Peter A. G. Crosthwaite | { |
548 | 82a24990 | Peter A. G. Crosthwaite | Flash *s = FROM_SSI_SLAVE(Flash, ss); |
549 | 82a24990 | Peter A. G. Crosthwaite | |
550 | 82a24990 | Peter A. G. Crosthwaite | if (select) {
|
551 | 82a24990 | Peter A. G. Crosthwaite | s->len = 0;
|
552 | 82a24990 | Peter A. G. Crosthwaite | s->pos = 0;
|
553 | 82a24990 | Peter A. G. Crosthwaite | s->state = STATE_IDLE; |
554 | 82a24990 | Peter A. G. Crosthwaite | flash_sync_dirty(s, -1);
|
555 | 82a24990 | Peter A. G. Crosthwaite | } |
556 | 82a24990 | Peter A. G. Crosthwaite | |
557 | 28097d02 | Peter Crosthwaite | DB_PRINT_L(0, "%sselect\n", select ? "de" : ""); |
558 | 82a24990 | Peter A. G. Crosthwaite | |
559 | 82a24990 | Peter A. G. Crosthwaite | return 0; |
560 | 82a24990 | Peter A. G. Crosthwaite | } |
561 | 82a24990 | Peter A. G. Crosthwaite | |
562 | 82a24990 | Peter A. G. Crosthwaite | static uint32_t m25p80_transfer8(SSISlave *ss, uint32_t tx)
|
563 | 82a24990 | Peter A. G. Crosthwaite | { |
564 | 82a24990 | Peter A. G. Crosthwaite | Flash *s = FROM_SSI_SLAVE(Flash, ss); |
565 | 82a24990 | Peter A. G. Crosthwaite | uint32_t r = 0;
|
566 | 82a24990 | Peter A. G. Crosthwaite | |
567 | 82a24990 | Peter A. G. Crosthwaite | switch (s->state) {
|
568 | 82a24990 | Peter A. G. Crosthwaite | |
569 | 82a24990 | Peter A. G. Crosthwaite | case STATE_PAGE_PROGRAM:
|
570 | 28097d02 | Peter Crosthwaite | DB_PRINT_L(1, "page program cur_addr=%#" PRIx64 " data=%" PRIx8 "\n", |
571 | 28097d02 | Peter Crosthwaite | s->cur_addr, (uint8_t)tx); |
572 | 82a24990 | Peter A. G. Crosthwaite | flash_write8(s, s->cur_addr, (uint8_t)tx); |
573 | 82a24990 | Peter A. G. Crosthwaite | s->cur_addr++; |
574 | 82a24990 | Peter A. G. Crosthwaite | break;
|
575 | 82a24990 | Peter A. G. Crosthwaite | |
576 | 82a24990 | Peter A. G. Crosthwaite | case STATE_READ:
|
577 | 82a24990 | Peter A. G. Crosthwaite | r = s->storage[s->cur_addr]; |
578 | 28097d02 | Peter Crosthwaite | DB_PRINT_L(1, "READ 0x%" PRIx64 "=%" PRIx8 "\n", s->cur_addr, |
579 | 28097d02 | Peter Crosthwaite | (uint8_t)r); |
580 | 82a24990 | Peter A. G. Crosthwaite | s->cur_addr = (s->cur_addr + 1) % s->size;
|
581 | 82a24990 | Peter A. G. Crosthwaite | break;
|
582 | 82a24990 | Peter A. G. Crosthwaite | |
583 | 82a24990 | Peter A. G. Crosthwaite | case STATE_COLLECTING_DATA:
|
584 | 82a24990 | Peter A. G. Crosthwaite | s->data[s->len] = (uint8_t)tx; |
585 | 82a24990 | Peter A. G. Crosthwaite | s->len++; |
586 | 82a24990 | Peter A. G. Crosthwaite | |
587 | 82a24990 | Peter A. G. Crosthwaite | if (s->len == s->needed_bytes) {
|
588 | 82a24990 | Peter A. G. Crosthwaite | complete_collecting_data(s); |
589 | 82a24990 | Peter A. G. Crosthwaite | } |
590 | 82a24990 | Peter A. G. Crosthwaite | break;
|
591 | 82a24990 | Peter A. G. Crosthwaite | |
592 | 82a24990 | Peter A. G. Crosthwaite | case STATE_READING_DATA:
|
593 | 82a24990 | Peter A. G. Crosthwaite | r = s->data[s->pos]; |
594 | 82a24990 | Peter A. G. Crosthwaite | s->pos++; |
595 | 82a24990 | Peter A. G. Crosthwaite | if (s->pos == s->len) {
|
596 | 82a24990 | Peter A. G. Crosthwaite | s->pos = 0;
|
597 | 82a24990 | Peter A. G. Crosthwaite | s->state = STATE_IDLE; |
598 | 82a24990 | Peter A. G. Crosthwaite | } |
599 | 82a24990 | Peter A. G. Crosthwaite | break;
|
600 | 82a24990 | Peter A. G. Crosthwaite | |
601 | 82a24990 | Peter A. G. Crosthwaite | default:
|
602 | 82a24990 | Peter A. G. Crosthwaite | case STATE_IDLE:
|
603 | 82a24990 | Peter A. G. Crosthwaite | decode_new_cmd(s, (uint8_t)tx); |
604 | 82a24990 | Peter A. G. Crosthwaite | break;
|
605 | 82a24990 | Peter A. G. Crosthwaite | } |
606 | 82a24990 | Peter A. G. Crosthwaite | |
607 | 82a24990 | Peter A. G. Crosthwaite | return r;
|
608 | 82a24990 | Peter A. G. Crosthwaite | } |
609 | 82a24990 | Peter A. G. Crosthwaite | |
610 | 82a24990 | Peter A. G. Crosthwaite | static int m25p80_init(SSISlave *ss) |
611 | 82a24990 | Peter A. G. Crosthwaite | { |
612 | 82a24990 | Peter A. G. Crosthwaite | DriveInfo *dinfo; |
613 | 82a24990 | Peter A. G. Crosthwaite | Flash *s = FROM_SSI_SLAVE(Flash, ss); |
614 | a7fd6915 | Peter Crosthwaite | M25P80Class *mc = M25P80_GET_CLASS(s); |
615 | 82a24990 | Peter A. G. Crosthwaite | |
616 | a7fd6915 | Peter Crosthwaite | s->pi = mc->pi; |
617 | 82a24990 | Peter A. G. Crosthwaite | |
618 | 82a24990 | Peter A. G. Crosthwaite | s->size = s->pi->sector_size * s->pi->n_sectors; |
619 | 82a24990 | Peter A. G. Crosthwaite | s->dirty_page = -1;
|
620 | 82a24990 | Peter A. G. Crosthwaite | s->storage = qemu_blockalign(s->bdrv, s->size); |
621 | 82a24990 | Peter A. G. Crosthwaite | |
622 | 82a24990 | Peter A. G. Crosthwaite | dinfo = drive_get_next(IF_MTD); |
623 | 82a24990 | Peter A. G. Crosthwaite | |
624 | 82a24990 | Peter A. G. Crosthwaite | if (dinfo && dinfo->bdrv) {
|
625 | 28097d02 | Peter Crosthwaite | DB_PRINT_L(0, "Binding to IF_MTD drive\n"); |
626 | 82a24990 | Peter A. G. Crosthwaite | s->bdrv = dinfo->bdrv; |
627 | 4f8a066b | Kevin Wolf | if (bdrv_is_read_only(s->bdrv)) {
|
628 | 4f8a066b | Kevin Wolf | fprintf(stderr, "Can't use a read-only drive");
|
629 | 4f8a066b | Kevin Wolf | return 1; |
630 | 4f8a066b | Kevin Wolf | } |
631 | 4f8a066b | Kevin Wolf | |
632 | 82a24990 | Peter A. G. Crosthwaite | /* FIXME: Move to late init */
|
633 | 82a24990 | Peter A. G. Crosthwaite | if (bdrv_read(s->bdrv, 0, s->storage, DIV_ROUND_UP(s->size, |
634 | 82a24990 | Peter A. G. Crosthwaite | BDRV_SECTOR_SIZE))) { |
635 | 82a24990 | Peter A. G. Crosthwaite | fprintf(stderr, "Failed to initialize SPI flash!\n");
|
636 | 82a24990 | Peter A. G. Crosthwaite | return 1; |
637 | 82a24990 | Peter A. G. Crosthwaite | } |
638 | 82a24990 | Peter A. G. Crosthwaite | } else {
|
639 | 095b9c48 | Peter Crosthwaite | DB_PRINT_L(0, "No BDRV - binding to RAM\n"); |
640 | 82a24990 | Peter A. G. Crosthwaite | memset(s->storage, 0xFF, s->size);
|
641 | 82a24990 | Peter A. G. Crosthwaite | } |
642 | 82a24990 | Peter A. G. Crosthwaite | |
643 | 82a24990 | Peter A. G. Crosthwaite | return 0; |
644 | 82a24990 | Peter A. G. Crosthwaite | } |
645 | 82a24990 | Peter A. G. Crosthwaite | |
646 | 82a24990 | Peter A. G. Crosthwaite | static void m25p80_pre_save(void *opaque) |
647 | 82a24990 | Peter A. G. Crosthwaite | { |
648 | 82a24990 | Peter A. G. Crosthwaite | flash_sync_dirty((Flash *)opaque, -1);
|
649 | 82a24990 | Peter A. G. Crosthwaite | } |
650 | 82a24990 | Peter A. G. Crosthwaite | |
651 | 82a24990 | Peter A. G. Crosthwaite | static const VMStateDescription vmstate_m25p80 = { |
652 | 82a24990 | Peter A. G. Crosthwaite | .name = "xilinx_spi",
|
653 | 82a24990 | Peter A. G. Crosthwaite | .version_id = 1,
|
654 | 82a24990 | Peter A. G. Crosthwaite | .minimum_version_id = 1,
|
655 | 82a24990 | Peter A. G. Crosthwaite | .minimum_version_id_old = 1,
|
656 | 82a24990 | Peter A. G. Crosthwaite | .pre_save = m25p80_pre_save, |
657 | 82a24990 | Peter A. G. Crosthwaite | .fields = (VMStateField[]) { |
658 | 82a24990 | Peter A. G. Crosthwaite | VMSTATE_UINT8(state, Flash), |
659 | 82a24990 | Peter A. G. Crosthwaite | VMSTATE_UINT8_ARRAY(data, Flash, 16),
|
660 | 82a24990 | Peter A. G. Crosthwaite | VMSTATE_UINT32(len, Flash), |
661 | 82a24990 | Peter A. G. Crosthwaite | VMSTATE_UINT32(pos, Flash), |
662 | 82a24990 | Peter A. G. Crosthwaite | VMSTATE_UINT8(needed_bytes, Flash), |
663 | 82a24990 | Peter A. G. Crosthwaite | VMSTATE_UINT8(cmd_in_progress, Flash), |
664 | 82a24990 | Peter A. G. Crosthwaite | VMSTATE_UINT64(cur_addr, Flash), |
665 | 82a24990 | Peter A. G. Crosthwaite | VMSTATE_BOOL(write_enable, Flash), |
666 | 82a24990 | Peter A. G. Crosthwaite | VMSTATE_END_OF_LIST() |
667 | 82a24990 | Peter A. G. Crosthwaite | } |
668 | 82a24990 | Peter A. G. Crosthwaite | }; |
669 | 82a24990 | Peter A. G. Crosthwaite | |
670 | 82a24990 | Peter A. G. Crosthwaite | static void m25p80_class_init(ObjectClass *klass, void *data) |
671 | 82a24990 | Peter A. G. Crosthwaite | { |
672 | 82a24990 | Peter A. G. Crosthwaite | DeviceClass *dc = DEVICE_CLASS(klass); |
673 | 82a24990 | Peter A. G. Crosthwaite | SSISlaveClass *k = SSI_SLAVE_CLASS(klass); |
674 | a7fd6915 | Peter Crosthwaite | M25P80Class *mc = M25P80_CLASS(klass); |
675 | 82a24990 | Peter A. G. Crosthwaite | |
676 | 82a24990 | Peter A. G. Crosthwaite | k->init = m25p80_init; |
677 | 82a24990 | Peter A. G. Crosthwaite | k->transfer = m25p80_transfer8; |
678 | 82a24990 | Peter A. G. Crosthwaite | k->set_cs = m25p80_cs; |
679 | 82a24990 | Peter A. G. Crosthwaite | k->cs_polarity = SSI_CS_LOW; |
680 | 82a24990 | Peter A. G. Crosthwaite | dc->vmsd = &vmstate_m25p80; |
681 | a7fd6915 | Peter Crosthwaite | mc->pi = data; |
682 | 82a24990 | Peter A. G. Crosthwaite | } |
683 | 82a24990 | Peter A. G. Crosthwaite | |
684 | 82a24990 | Peter A. G. Crosthwaite | static const TypeInfo m25p80_info = { |
685 | a7fd6915 | Peter Crosthwaite | .name = TYPE_M25P80, |
686 | 82a24990 | Peter A. G. Crosthwaite | .parent = TYPE_SSI_SLAVE, |
687 | 82a24990 | Peter A. G. Crosthwaite | .instance_size = sizeof(Flash),
|
688 | a7fd6915 | Peter Crosthwaite | .class_size = sizeof(M25P80Class),
|
689 | a7fd6915 | Peter Crosthwaite | .abstract = true,
|
690 | 82a24990 | Peter A. G. Crosthwaite | }; |
691 | 82a24990 | Peter A. G. Crosthwaite | |
692 | 82a24990 | Peter A. G. Crosthwaite | static void m25p80_register_types(void) |
693 | 82a24990 | Peter A. G. Crosthwaite | { |
694 | a7fd6915 | Peter Crosthwaite | int i;
|
695 | a7fd6915 | Peter Crosthwaite | |
696 | 82a24990 | Peter A. G. Crosthwaite | type_register_static(&m25p80_info); |
697 | a7fd6915 | Peter Crosthwaite | for (i = 0; i < ARRAY_SIZE(known_devices); ++i) { |
698 | a7fd6915 | Peter Crosthwaite | TypeInfo ti = { |
699 | a7fd6915 | Peter Crosthwaite | .name = known_devices[i].part_name, |
700 | a7fd6915 | Peter Crosthwaite | .parent = TYPE_M25P80, |
701 | a7fd6915 | Peter Crosthwaite | .class_init = m25p80_class_init, |
702 | a7fd6915 | Peter Crosthwaite | .class_data = (void *)&known_devices[i],
|
703 | a7fd6915 | Peter Crosthwaite | }; |
704 | a7fd6915 | Peter Crosthwaite | type_register(&ti); |
705 | a7fd6915 | Peter Crosthwaite | } |
706 | 82a24990 | Peter A. G. Crosthwaite | } |
707 | 82a24990 | Peter A. G. Crosthwaite | |
708 | 82a24990 | Peter A. G. Crosthwaite | type_init(m25p80_register_types) |