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