root / hw / pflash_cfi02.c @ c596defd
History | View | Annotate | Download (19.2 kB)
1 | 29133e9a | bellard | /*
|
---|---|---|---|
2 | 29133e9a | bellard | * CFI parallel flash with AMD command set emulation
|
3 | 5fafdf24 | ths | *
|
4 | 29133e9a | bellard | * Copyright (c) 2005 Jocelyn Mayer
|
5 | 29133e9a | bellard | *
|
6 | 29133e9a | bellard | * This library is free software; you can redistribute it and/or
|
7 | 29133e9a | bellard | * modify it under the terms of the GNU Lesser General Public
|
8 | 29133e9a | bellard | * License as published by the Free Software Foundation; either
|
9 | 29133e9a | bellard | * version 2 of the License, or (at your option) any later version.
|
10 | 29133e9a | bellard | *
|
11 | 29133e9a | bellard | * This library is distributed in the hope that it will be useful,
|
12 | 29133e9a | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 29133e9a | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 29133e9a | bellard | * Lesser General Public License for more details.
|
15 | 29133e9a | bellard | *
|
16 | 29133e9a | bellard | * You should have received a copy of the GNU Lesser General Public
|
17 | 29133e9a | bellard | * License along with this library; if not, write to the Free Software
|
18 | 29133e9a | bellard | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 | 29133e9a | bellard | */
|
20 | 29133e9a | bellard | |
21 | 29133e9a | bellard | /*
|
22 | 29133e9a | bellard | * For now, this code can emulate flashes of 1, 2 or 4 bytes width.
|
23 | 29133e9a | bellard | * Supported commands/modes are:
|
24 | 29133e9a | bellard | * - flash read
|
25 | 29133e9a | bellard | * - flash write
|
26 | 29133e9a | bellard | * - flash ID read
|
27 | 29133e9a | bellard | * - sector erase
|
28 | 29133e9a | bellard | * - chip erase
|
29 | 29133e9a | bellard | * - unlock bypass command
|
30 | 29133e9a | bellard | * - CFI queries
|
31 | 29133e9a | bellard | *
|
32 | 29133e9a | bellard | * It does not support flash interleaving.
|
33 | 29133e9a | bellard | * It does not implement boot blocs with reduced size
|
34 | 29133e9a | bellard | * It does not implement software data protection as found in many real chips
|
35 | 29133e9a | bellard | * It does not implement erase suspend/resume commands
|
36 | 29133e9a | bellard | * It does not implement multiple sectors erase
|
37 | 29133e9a | bellard | */
|
38 | 29133e9a | bellard | |
39 | 87ecb68b | pbrook | #include "hw.h" |
40 | 87ecb68b | pbrook | #include "flash.h" |
41 | 87ecb68b | pbrook | #include "qemu-timer.h" |
42 | 87ecb68b | pbrook | #include "block.h" |
43 | 29133e9a | bellard | |
44 | 29133e9a | bellard | //#define PFLASH_DEBUG
|
45 | 29133e9a | bellard | #ifdef PFLASH_DEBUG
|
46 | 29133e9a | bellard | #define DPRINTF(fmt, args...) \
|
47 | 29133e9a | bellard | do { \
|
48 | 29133e9a | bellard | printf("PFLASH: " fmt , ##args); \ |
49 | 29133e9a | bellard | } while (0) |
50 | 29133e9a | bellard | #else
|
51 | 29133e9a | bellard | #define DPRINTF(fmt, args...) do { } while (0) |
52 | 29133e9a | bellard | #endif
|
53 | 29133e9a | bellard | |
54 | 29133e9a | bellard | struct pflash_t {
|
55 | 29133e9a | bellard | BlockDriverState *bs; |
56 | 71db710f | blueswir1 | target_phys_addr_t base; |
57 | 71db710f | blueswir1 | uint32_t sector_len; |
58 | 4fbd24ba | balrog | uint32_t chip_len; |
59 | 4fbd24ba | balrog | int mappings;
|
60 | 29133e9a | bellard | int width;
|
61 | 29133e9a | bellard | int wcycle; /* if 0, the flash is read normally */ |
62 | 29133e9a | bellard | int bypass;
|
63 | 29133e9a | bellard | int ro;
|
64 | 29133e9a | bellard | uint8_t cmd; |
65 | 29133e9a | bellard | uint8_t status; |
66 | 29133e9a | bellard | uint16_t ident[4];
|
67 | 6725070d | balrog | uint16_t unlock_addr[2];
|
68 | 29133e9a | bellard | uint8_t cfi_len; |
69 | 29133e9a | bellard | uint8_t cfi_table[0x52];
|
70 | 29133e9a | bellard | QEMUTimer *timer; |
71 | 29133e9a | bellard | ram_addr_t off; |
72 | 29133e9a | bellard | int fl_mem;
|
73 | 9c9bb6c8 | balrog | int rom_mode;
|
74 | 29133e9a | bellard | void *storage;
|
75 | 29133e9a | bellard | }; |
76 | 29133e9a | bellard | |
77 | 4fbd24ba | balrog | static void pflash_register_memory(pflash_t *pfl, int rom_mode) |
78 | 4fbd24ba | balrog | { |
79 | 4fbd24ba | balrog | unsigned long phys_offset = pfl->fl_mem; |
80 | 4fbd24ba | balrog | int i;
|
81 | 4fbd24ba | balrog | |
82 | 4fbd24ba | balrog | if (rom_mode)
|
83 | 4fbd24ba | balrog | phys_offset |= pfl->off | IO_MEM_ROMD; |
84 | 9c9bb6c8 | balrog | pfl->rom_mode = rom_mode; |
85 | 4fbd24ba | balrog | |
86 | 4fbd24ba | balrog | for (i = 0; i < pfl->mappings; i++) |
87 | 4fbd24ba | balrog | cpu_register_physical_memory(pfl->base + i * pfl->chip_len, |
88 | 4fbd24ba | balrog | pfl->chip_len, phys_offset); |
89 | 4fbd24ba | balrog | } |
90 | 4fbd24ba | balrog | |
91 | 29133e9a | bellard | static void pflash_timer (void *opaque) |
92 | 29133e9a | bellard | { |
93 | 29133e9a | bellard | pflash_t *pfl = opaque; |
94 | 29133e9a | bellard | |
95 | 29133e9a | bellard | DPRINTF("%s: command %02x done\n", __func__, pfl->cmd);
|
96 | 29133e9a | bellard | /* Reset flash */
|
97 | 29133e9a | bellard | pfl->status ^= 0x80;
|
98 | 29133e9a | bellard | if (pfl->bypass) {
|
99 | 29133e9a | bellard | pfl->wcycle = 2;
|
100 | 29133e9a | bellard | } else {
|
101 | 4fbd24ba | balrog | pflash_register_memory(pfl, 1);
|
102 | 29133e9a | bellard | pfl->wcycle = 0;
|
103 | 29133e9a | bellard | } |
104 | 29133e9a | bellard | pfl->cmd = 0;
|
105 | 29133e9a | bellard | } |
106 | 29133e9a | bellard | |
107 | 71db710f | blueswir1 | static uint32_t pflash_read (pflash_t *pfl, uint32_t offset, int width) |
108 | 29133e9a | bellard | { |
109 | 71db710f | blueswir1 | uint32_t boff; |
110 | 29133e9a | bellard | uint32_t ret; |
111 | 29133e9a | bellard | uint8_t *p; |
112 | 29133e9a | bellard | |
113 | e96efcfc | j_mayer | DPRINTF("%s: offset " TARGET_FMT_lx "\n", __func__, offset); |
114 | 29133e9a | bellard | ret = -1;
|
115 | 0f459d16 | pbrook | offset -= pfl->base; |
116 | 9c9bb6c8 | balrog | if (pfl->rom_mode) {
|
117 | 9c9bb6c8 | balrog | /* Lazy reset of to ROMD mode */
|
118 | 9c9bb6c8 | balrog | if (pfl->wcycle == 0) |
119 | 9c9bb6c8 | balrog | pflash_register_memory(pfl, 1);
|
120 | 0f459d16 | pbrook | } |
121 | 4fbd24ba | balrog | offset &= pfl->chip_len - 1;
|
122 | 29133e9a | bellard | boff = offset & 0xFF;
|
123 | 29133e9a | bellard | if (pfl->width == 2) |
124 | 29133e9a | bellard | boff = boff >> 1;
|
125 | 29133e9a | bellard | else if (pfl->width == 4) |
126 | 29133e9a | bellard | boff = boff >> 2;
|
127 | 29133e9a | bellard | switch (pfl->cmd) {
|
128 | 29133e9a | bellard | default:
|
129 | 29133e9a | bellard | /* This should never happen : reset state & treat it as a read*/
|
130 | 29133e9a | bellard | DPRINTF("%s: unknown command state: %x\n", __func__, pfl->cmd);
|
131 | 29133e9a | bellard | pfl->wcycle = 0;
|
132 | 29133e9a | bellard | pfl->cmd = 0;
|
133 | 29133e9a | bellard | case 0x80: |
134 | 29133e9a | bellard | /* We accept reads during second unlock sequence... */
|
135 | 29133e9a | bellard | case 0x00: |
136 | 29133e9a | bellard | flash_read:
|
137 | 29133e9a | bellard | /* Flash area read */
|
138 | 29133e9a | bellard | p = pfl->storage; |
139 | 29133e9a | bellard | switch (width) {
|
140 | 29133e9a | bellard | case 1: |
141 | 29133e9a | bellard | ret = p[offset]; |
142 | 29133e9a | bellard | // DPRINTF("%s: data offset %08x %02x\n", __func__, offset, ret);
|
143 | 29133e9a | bellard | break;
|
144 | 29133e9a | bellard | case 2: |
145 | 29133e9a | bellard | #if defined(TARGET_WORDS_BIGENDIAN)
|
146 | 29133e9a | bellard | ret = p[offset] << 8;
|
147 | 29133e9a | bellard | ret |= p[offset + 1];
|
148 | 29133e9a | bellard | #else
|
149 | 29133e9a | bellard | ret = p[offset]; |
150 | 29133e9a | bellard | ret |= p[offset + 1] << 8; |
151 | 29133e9a | bellard | #endif
|
152 | 29133e9a | bellard | // DPRINTF("%s: data offset %08x %04x\n", __func__, offset, ret);
|
153 | 29133e9a | bellard | break;
|
154 | 29133e9a | bellard | case 4: |
155 | 29133e9a | bellard | #if defined(TARGET_WORDS_BIGENDIAN)
|
156 | 29133e9a | bellard | ret = p[offset] << 24;
|
157 | 29133e9a | bellard | ret |= p[offset + 1] << 16; |
158 | 29133e9a | bellard | ret |= p[offset + 2] << 8; |
159 | 29133e9a | bellard | ret |= p[offset + 3];
|
160 | 29133e9a | bellard | #else
|
161 | 29133e9a | bellard | ret = p[offset]; |
162 | 29133e9a | bellard | ret |= p[offset + 1] << 8; |
163 | 29133e9a | bellard | ret |= p[offset + 2] << 16; |
164 | 29133e9a | bellard | ret |= p[offset + 3] << 24; |
165 | 29133e9a | bellard | #endif
|
166 | 29133e9a | bellard | // DPRINTF("%s: data offset %08x %08x\n", __func__, offset, ret);
|
167 | 29133e9a | bellard | break;
|
168 | 29133e9a | bellard | } |
169 | 29133e9a | bellard | break;
|
170 | 29133e9a | bellard | case 0x90: |
171 | 29133e9a | bellard | /* flash ID read */
|
172 | 29133e9a | bellard | switch (boff) {
|
173 | 29133e9a | bellard | case 0x00: |
174 | 29133e9a | bellard | case 0x01: |
175 | 29133e9a | bellard | ret = pfl->ident[boff & 0x01];
|
176 | 29133e9a | bellard | break;
|
177 | 29133e9a | bellard | case 0x02: |
178 | 29133e9a | bellard | ret = 0x00; /* Pretend all sectors are unprotected */ |
179 | 29133e9a | bellard | break;
|
180 | 29133e9a | bellard | case 0x0E: |
181 | 29133e9a | bellard | case 0x0F: |
182 | 29133e9a | bellard | if (pfl->ident[2 + (boff & 0x01)] == (uint8_t)-1) |
183 | 29133e9a | bellard | goto flash_read;
|
184 | 29133e9a | bellard | ret = pfl->ident[2 + (boff & 0x01)]; |
185 | 29133e9a | bellard | break;
|
186 | 29133e9a | bellard | default:
|
187 | 29133e9a | bellard | goto flash_read;
|
188 | 29133e9a | bellard | } |
189 | e96efcfc | j_mayer | DPRINTF("%s: ID " TARGET_FMT_ld " %x\n", __func__, boff, ret); |
190 | 29133e9a | bellard | break;
|
191 | 29133e9a | bellard | case 0xA0: |
192 | 29133e9a | bellard | case 0x10: |
193 | 29133e9a | bellard | case 0x30: |
194 | 29133e9a | bellard | /* Status register read */
|
195 | 29133e9a | bellard | ret = pfl->status; |
196 | 29133e9a | bellard | DPRINTF("%s: status %x\n", __func__, ret);
|
197 | 29133e9a | bellard | /* Toggle bit 6 */
|
198 | 29133e9a | bellard | pfl->status ^= 0x40;
|
199 | 29133e9a | bellard | break;
|
200 | 29133e9a | bellard | case 0x98: |
201 | 29133e9a | bellard | /* CFI query mode */
|
202 | 29133e9a | bellard | if (boff > pfl->cfi_len)
|
203 | 29133e9a | bellard | ret = 0;
|
204 | 29133e9a | bellard | else
|
205 | 29133e9a | bellard | ret = pfl->cfi_table[boff]; |
206 | 29133e9a | bellard | break;
|
207 | 29133e9a | bellard | } |
208 | 29133e9a | bellard | |
209 | 29133e9a | bellard | return ret;
|
210 | 29133e9a | bellard | } |
211 | 29133e9a | bellard | |
212 | 29133e9a | bellard | /* update flash content on disk */
|
213 | 5fafdf24 | ths | static void pflash_update(pflash_t *pfl, int offset, |
214 | 29133e9a | bellard | int size)
|
215 | 29133e9a | bellard | { |
216 | 29133e9a | bellard | int offset_end;
|
217 | 29133e9a | bellard | if (pfl->bs) {
|
218 | 29133e9a | bellard | offset_end = offset + size; |
219 | 29133e9a | bellard | /* round to sectors */
|
220 | 29133e9a | bellard | offset = offset >> 9;
|
221 | 29133e9a | bellard | offset_end = (offset_end + 511) >> 9; |
222 | 5fafdf24 | ths | bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
|
223 | 29133e9a | bellard | offset_end - offset); |
224 | 29133e9a | bellard | } |
225 | 29133e9a | bellard | } |
226 | 29133e9a | bellard | |
227 | 71db710f | blueswir1 | static void pflash_write (pflash_t *pfl, uint32_t offset, uint32_t value, |
228 | 29133e9a | bellard | int width)
|
229 | 29133e9a | bellard | { |
230 | 71db710f | blueswir1 | uint32_t boff; |
231 | 29133e9a | bellard | uint8_t *p; |
232 | 29133e9a | bellard | uint8_t cmd; |
233 | 29133e9a | bellard | |
234 | 95d1f3ed | j_mayer | cmd = value; |
235 | 95d1f3ed | j_mayer | if (pfl->cmd != 0xA0 && cmd == 0xF0) { |
236 | 95d1f3ed | j_mayer | #if 0
|
237 | 95d1f3ed | j_mayer | DPRINTF("%s: flash reset asked (%02x %02x)\n",
|
238 | 95d1f3ed | j_mayer | __func__, pfl->cmd, cmd);
|
239 | 95d1f3ed | j_mayer | #endif
|
240 | 95d1f3ed | j_mayer | goto reset_flash;
|
241 | 95d1f3ed | j_mayer | } |
242 | 95d1f3ed | j_mayer | DPRINTF("%s: offset " TARGET_FMT_lx " %08x %d %d\n", __func__, |
243 | 95d1f3ed | j_mayer | offset, value, width, pfl->wcycle); |
244 | 0f459d16 | pbrook | offset -= pfl->base; |
245 | 4fbd24ba | balrog | offset &= pfl->chip_len - 1;
|
246 | 3b46e624 | ths | |
247 | e96efcfc | j_mayer | DPRINTF("%s: offset " TARGET_FMT_lx " %08x %d\n", __func__, |
248 | e96efcfc | j_mayer | offset, value, width); |
249 | 29133e9a | bellard | boff = offset & (pfl->sector_len - 1);
|
250 | 29133e9a | bellard | if (pfl->width == 2) |
251 | 29133e9a | bellard | boff = boff >> 1;
|
252 | 29133e9a | bellard | else if (pfl->width == 4) |
253 | 29133e9a | bellard | boff = boff >> 2;
|
254 | 29133e9a | bellard | switch (pfl->wcycle) {
|
255 | 29133e9a | bellard | case 0: |
256 | 9c9bb6c8 | balrog | /* Set the device in I/O access mode if required */
|
257 | 9c9bb6c8 | balrog | if (pfl->rom_mode)
|
258 | 9c9bb6c8 | balrog | pflash_register_memory(pfl, 0);
|
259 | 29133e9a | bellard | /* We're in read mode */
|
260 | 29133e9a | bellard | check_unlock0:
|
261 | 29133e9a | bellard | if (boff == 0x55 && cmd == 0x98) { |
262 | 29133e9a | bellard | enter_CFI_mode:
|
263 | 29133e9a | bellard | /* Enter CFI query mode */
|
264 | 29133e9a | bellard | pfl->wcycle = 7;
|
265 | 29133e9a | bellard | pfl->cmd = 0x98;
|
266 | 29133e9a | bellard | return;
|
267 | 29133e9a | bellard | } |
268 | 6725070d | balrog | if (boff != pfl->unlock_addr[0] || cmd != 0xAA) { |
269 | e96efcfc | j_mayer | DPRINTF("%s: unlock0 failed " TARGET_FMT_lx " %02x %04x\n", |
270 | 6725070d | balrog | __func__, boff, cmd, pfl->unlock_addr[0]);
|
271 | 29133e9a | bellard | goto reset_flash;
|
272 | 29133e9a | bellard | } |
273 | 29133e9a | bellard | DPRINTF("%s: unlock sequence started\n", __func__);
|
274 | 29133e9a | bellard | break;
|
275 | 29133e9a | bellard | case 1: |
276 | 29133e9a | bellard | /* We started an unlock sequence */
|
277 | 29133e9a | bellard | check_unlock1:
|
278 | 6725070d | balrog | if (boff != pfl->unlock_addr[1] || cmd != 0x55) { |
279 | e96efcfc | j_mayer | DPRINTF("%s: unlock1 failed " TARGET_FMT_lx " %02x\n", __func__, |
280 | e96efcfc | j_mayer | boff, cmd); |
281 | 29133e9a | bellard | goto reset_flash;
|
282 | 29133e9a | bellard | } |
283 | 29133e9a | bellard | DPRINTF("%s: unlock sequence done\n", __func__);
|
284 | 29133e9a | bellard | break;
|
285 | 29133e9a | bellard | case 2: |
286 | 29133e9a | bellard | /* We finished an unlock sequence */
|
287 | 6725070d | balrog | if (!pfl->bypass && boff != pfl->unlock_addr[0]) { |
288 | e96efcfc | j_mayer | DPRINTF("%s: command failed " TARGET_FMT_lx " %02x\n", __func__, |
289 | e96efcfc | j_mayer | boff, cmd); |
290 | 29133e9a | bellard | goto reset_flash;
|
291 | 29133e9a | bellard | } |
292 | 29133e9a | bellard | switch (cmd) {
|
293 | 29133e9a | bellard | case 0x20: |
294 | 29133e9a | bellard | pfl->bypass = 1;
|
295 | 29133e9a | bellard | goto do_bypass;
|
296 | 29133e9a | bellard | case 0x80: |
297 | 29133e9a | bellard | case 0x90: |
298 | 29133e9a | bellard | case 0xA0: |
299 | 29133e9a | bellard | pfl->cmd = cmd; |
300 | 29133e9a | bellard | DPRINTF("%s: starting command %02x\n", __func__, cmd);
|
301 | 29133e9a | bellard | break;
|
302 | 29133e9a | bellard | default:
|
303 | 29133e9a | bellard | DPRINTF("%s: unknown command %02x\n", __func__, cmd);
|
304 | 29133e9a | bellard | goto reset_flash;
|
305 | 29133e9a | bellard | } |
306 | 29133e9a | bellard | break;
|
307 | 29133e9a | bellard | case 3: |
308 | 29133e9a | bellard | switch (pfl->cmd) {
|
309 | 29133e9a | bellard | case 0x80: |
310 | 29133e9a | bellard | /* We need another unlock sequence */
|
311 | 29133e9a | bellard | goto check_unlock0;
|
312 | 29133e9a | bellard | case 0xA0: |
313 | e96efcfc | j_mayer | DPRINTF("%s: write data offset " TARGET_FMT_lx " %08x %d\n", |
314 | 29133e9a | bellard | __func__, offset, value, width); |
315 | 29133e9a | bellard | p = pfl->storage; |
316 | 29133e9a | bellard | switch (width) {
|
317 | 29133e9a | bellard | case 1: |
318 | 29133e9a | bellard | p[offset] &= value; |
319 | 29133e9a | bellard | pflash_update(pfl, offset, 1);
|
320 | 29133e9a | bellard | break;
|
321 | 29133e9a | bellard | case 2: |
322 | 29133e9a | bellard | #if defined(TARGET_WORDS_BIGENDIAN)
|
323 | 29133e9a | bellard | p[offset] &= value >> 8;
|
324 | 29133e9a | bellard | p[offset + 1] &= value;
|
325 | 29133e9a | bellard | #else
|
326 | 29133e9a | bellard | p[offset] &= value; |
327 | 29133e9a | bellard | p[offset + 1] &= value >> 8; |
328 | 29133e9a | bellard | #endif
|
329 | 29133e9a | bellard | pflash_update(pfl, offset, 2);
|
330 | 29133e9a | bellard | break;
|
331 | 29133e9a | bellard | case 4: |
332 | 29133e9a | bellard | #if defined(TARGET_WORDS_BIGENDIAN)
|
333 | 29133e9a | bellard | p[offset] &= value >> 24;
|
334 | 29133e9a | bellard | p[offset + 1] &= value >> 16; |
335 | 29133e9a | bellard | p[offset + 2] &= value >> 8; |
336 | 29133e9a | bellard | p[offset + 3] &= value;
|
337 | 29133e9a | bellard | #else
|
338 | 29133e9a | bellard | p[offset] &= value; |
339 | 29133e9a | bellard | p[offset + 1] &= value >> 8; |
340 | 29133e9a | bellard | p[offset + 2] &= value >> 16; |
341 | 29133e9a | bellard | p[offset + 3] &= value >> 24; |
342 | 29133e9a | bellard | #endif
|
343 | 29133e9a | bellard | pflash_update(pfl, offset, 4);
|
344 | 29133e9a | bellard | break;
|
345 | 29133e9a | bellard | } |
346 | 29133e9a | bellard | pfl->status = 0x00 | ~(value & 0x80); |
347 | 29133e9a | bellard | /* Let's pretend write is immediate */
|
348 | 29133e9a | bellard | if (pfl->bypass)
|
349 | 29133e9a | bellard | goto do_bypass;
|
350 | 29133e9a | bellard | goto reset_flash;
|
351 | 29133e9a | bellard | case 0x90: |
352 | 29133e9a | bellard | if (pfl->bypass && cmd == 0x00) { |
353 | 29133e9a | bellard | /* Unlock bypass reset */
|
354 | 29133e9a | bellard | goto reset_flash;
|
355 | 29133e9a | bellard | } |
356 | 29133e9a | bellard | /* We can enter CFI query mode from autoselect mode */
|
357 | 29133e9a | bellard | if (boff == 0x55 && cmd == 0x98) |
358 | 29133e9a | bellard | goto enter_CFI_mode;
|
359 | 29133e9a | bellard | /* No break here */
|
360 | 29133e9a | bellard | default:
|
361 | 29133e9a | bellard | DPRINTF("%s: invalid write for command %02x\n",
|
362 | 29133e9a | bellard | __func__, pfl->cmd); |
363 | 29133e9a | bellard | goto reset_flash;
|
364 | 29133e9a | bellard | } |
365 | 29133e9a | bellard | case 4: |
366 | 29133e9a | bellard | switch (pfl->cmd) {
|
367 | 29133e9a | bellard | case 0xA0: |
368 | 29133e9a | bellard | /* Ignore writes while flash data write is occuring */
|
369 | 29133e9a | bellard | /* As we suppose write is immediate, this should never happen */
|
370 | 29133e9a | bellard | return;
|
371 | 29133e9a | bellard | case 0x80: |
372 | 29133e9a | bellard | goto check_unlock1;
|
373 | 29133e9a | bellard | default:
|
374 | 29133e9a | bellard | /* Should never happen */
|
375 | 29133e9a | bellard | DPRINTF("%s: invalid command state %02x (wc 4)\n",
|
376 | 29133e9a | bellard | __func__, pfl->cmd); |
377 | 29133e9a | bellard | goto reset_flash;
|
378 | 29133e9a | bellard | } |
379 | 29133e9a | bellard | break;
|
380 | 29133e9a | bellard | case 5: |
381 | 29133e9a | bellard | switch (cmd) {
|
382 | 29133e9a | bellard | case 0x10: |
383 | 6725070d | balrog | if (boff != pfl->unlock_addr[0]) { |
384 | e96efcfc | j_mayer | DPRINTF("%s: chip erase: invalid address " TARGET_FMT_lx "\n", |
385 | 29133e9a | bellard | __func__, offset); |
386 | 29133e9a | bellard | goto reset_flash;
|
387 | 29133e9a | bellard | } |
388 | 29133e9a | bellard | /* Chip erase */
|
389 | 29133e9a | bellard | DPRINTF("%s: start chip erase\n", __func__);
|
390 | 4fbd24ba | balrog | memset(pfl->storage, 0xFF, pfl->chip_len);
|
391 | 29133e9a | bellard | pfl->status = 0x00;
|
392 | 4fbd24ba | balrog | pflash_update(pfl, 0, pfl->chip_len);
|
393 | 29133e9a | bellard | /* Let's wait 5 seconds before chip erase is done */
|
394 | 5fafdf24 | ths | qemu_mod_timer(pfl->timer, |
395 | 29133e9a | bellard | qemu_get_clock(vm_clock) + (ticks_per_sec * 5));
|
396 | 29133e9a | bellard | break;
|
397 | 29133e9a | bellard | case 0x30: |
398 | 29133e9a | bellard | /* Sector erase */
|
399 | 29133e9a | bellard | p = pfl->storage; |
400 | 29133e9a | bellard | offset &= ~(pfl->sector_len - 1);
|
401 | e96efcfc | j_mayer | DPRINTF("%s: start sector erase at " TARGET_FMT_lx "\n", __func__, |
402 | e96efcfc | j_mayer | offset); |
403 | 29133e9a | bellard | memset(p + offset, 0xFF, pfl->sector_len);
|
404 | 29133e9a | bellard | pflash_update(pfl, offset, pfl->sector_len); |
405 | 29133e9a | bellard | pfl->status = 0x00;
|
406 | 29133e9a | bellard | /* Let's wait 1/2 second before sector erase is done */
|
407 | 5fafdf24 | ths | qemu_mod_timer(pfl->timer, |
408 | 29133e9a | bellard | qemu_get_clock(vm_clock) + (ticks_per_sec / 2));
|
409 | 29133e9a | bellard | break;
|
410 | 29133e9a | bellard | default:
|
411 | 29133e9a | bellard | DPRINTF("%s: invalid command %02x (wc 5)\n", __func__, cmd);
|
412 | 29133e9a | bellard | goto reset_flash;
|
413 | 29133e9a | bellard | } |
414 | 29133e9a | bellard | pfl->cmd = cmd; |
415 | 29133e9a | bellard | break;
|
416 | 29133e9a | bellard | case 6: |
417 | 29133e9a | bellard | switch (pfl->cmd) {
|
418 | 29133e9a | bellard | case 0x10: |
419 | 29133e9a | bellard | /* Ignore writes during chip erase */
|
420 | 29133e9a | bellard | return;
|
421 | 29133e9a | bellard | case 0x30: |
422 | 29133e9a | bellard | /* Ignore writes during sector erase */
|
423 | 29133e9a | bellard | return;
|
424 | 29133e9a | bellard | default:
|
425 | 29133e9a | bellard | /* Should never happen */
|
426 | 29133e9a | bellard | DPRINTF("%s: invalid command state %02x (wc 6)\n",
|
427 | 29133e9a | bellard | __func__, pfl->cmd); |
428 | 29133e9a | bellard | goto reset_flash;
|
429 | 29133e9a | bellard | } |
430 | 29133e9a | bellard | break;
|
431 | 29133e9a | bellard | case 7: /* Special value for CFI queries */ |
432 | 29133e9a | bellard | DPRINTF("%s: invalid write in CFI query mode\n", __func__);
|
433 | 29133e9a | bellard | goto reset_flash;
|
434 | 29133e9a | bellard | default:
|
435 | 29133e9a | bellard | /* Should never happen */
|
436 | 29133e9a | bellard | DPRINTF("%s: invalid write state (wc 7)\n", __func__);
|
437 | 29133e9a | bellard | goto reset_flash;
|
438 | 29133e9a | bellard | } |
439 | 29133e9a | bellard | pfl->wcycle++; |
440 | 29133e9a | bellard | |
441 | 29133e9a | bellard | return;
|
442 | 29133e9a | bellard | |
443 | 29133e9a | bellard | /* Reset flash */
|
444 | 29133e9a | bellard | reset_flash:
|
445 | 29133e9a | bellard | pfl->bypass = 0;
|
446 | 29133e9a | bellard | pfl->wcycle = 0;
|
447 | 29133e9a | bellard | pfl->cmd = 0;
|
448 | 29133e9a | bellard | return;
|
449 | 29133e9a | bellard | |
450 | 29133e9a | bellard | do_bypass:
|
451 | 29133e9a | bellard | pfl->wcycle = 2;
|
452 | 29133e9a | bellard | pfl->cmd = 0;
|
453 | 29133e9a | bellard | return;
|
454 | 29133e9a | bellard | } |
455 | 29133e9a | bellard | |
456 | 29133e9a | bellard | |
457 | 29133e9a | bellard | static uint32_t pflash_readb (void *opaque, target_phys_addr_t addr) |
458 | 29133e9a | bellard | { |
459 | 29133e9a | bellard | return pflash_read(opaque, addr, 1); |
460 | 29133e9a | bellard | } |
461 | 29133e9a | bellard | |
462 | 29133e9a | bellard | static uint32_t pflash_readw (void *opaque, target_phys_addr_t addr) |
463 | 29133e9a | bellard | { |
464 | 29133e9a | bellard | pflash_t *pfl = opaque; |
465 | 29133e9a | bellard | |
466 | 29133e9a | bellard | return pflash_read(pfl, addr, 2); |
467 | 29133e9a | bellard | } |
468 | 29133e9a | bellard | |
469 | 29133e9a | bellard | static uint32_t pflash_readl (void *opaque, target_phys_addr_t addr) |
470 | 29133e9a | bellard | { |
471 | 29133e9a | bellard | pflash_t *pfl = opaque; |
472 | 29133e9a | bellard | |
473 | 29133e9a | bellard | return pflash_read(pfl, addr, 4); |
474 | 29133e9a | bellard | } |
475 | 29133e9a | bellard | |
476 | 29133e9a | bellard | static void pflash_writeb (void *opaque, target_phys_addr_t addr, |
477 | 29133e9a | bellard | uint32_t value) |
478 | 29133e9a | bellard | { |
479 | 29133e9a | bellard | pflash_write(opaque, addr, value, 1);
|
480 | 29133e9a | bellard | } |
481 | 29133e9a | bellard | |
482 | 29133e9a | bellard | static void pflash_writew (void *opaque, target_phys_addr_t addr, |
483 | 29133e9a | bellard | uint32_t value) |
484 | 29133e9a | bellard | { |
485 | 29133e9a | bellard | pflash_t *pfl = opaque; |
486 | 29133e9a | bellard | |
487 | 29133e9a | bellard | pflash_write(pfl, addr, value, 2);
|
488 | 29133e9a | bellard | } |
489 | 29133e9a | bellard | |
490 | 29133e9a | bellard | static void pflash_writel (void *opaque, target_phys_addr_t addr, |
491 | 29133e9a | bellard | uint32_t value) |
492 | 29133e9a | bellard | { |
493 | 29133e9a | bellard | pflash_t *pfl = opaque; |
494 | 29133e9a | bellard | |
495 | 29133e9a | bellard | pflash_write(pfl, addr, value, 4);
|
496 | 29133e9a | bellard | } |
497 | 29133e9a | bellard | |
498 | 29133e9a | bellard | static CPUWriteMemoryFunc *pflash_write_ops[] = {
|
499 | 29133e9a | bellard | &pflash_writeb, |
500 | 29133e9a | bellard | &pflash_writew, |
501 | 29133e9a | bellard | &pflash_writel, |
502 | 29133e9a | bellard | }; |
503 | 29133e9a | bellard | |
504 | 29133e9a | bellard | static CPUReadMemoryFunc *pflash_read_ops[] = {
|
505 | 29133e9a | bellard | &pflash_readb, |
506 | 29133e9a | bellard | &pflash_readw, |
507 | 29133e9a | bellard | &pflash_readl, |
508 | 29133e9a | bellard | }; |
509 | 29133e9a | bellard | |
510 | 29133e9a | bellard | /* Count trailing zeroes of a 32 bits quantity */
|
511 | 29133e9a | bellard | static int ctz32 (uint32_t n) |
512 | 29133e9a | bellard | { |
513 | 29133e9a | bellard | int ret;
|
514 | 29133e9a | bellard | |
515 | 29133e9a | bellard | ret = 0;
|
516 | 29133e9a | bellard | if (!(n & 0xFFFF)) { |
517 | 29133e9a | bellard | ret += 16;
|
518 | 29133e9a | bellard | n = n >> 16;
|
519 | 29133e9a | bellard | } |
520 | 29133e9a | bellard | if (!(n & 0xFF)) { |
521 | 29133e9a | bellard | ret += 8;
|
522 | 29133e9a | bellard | n = n >> 8;
|
523 | 29133e9a | bellard | } |
524 | 29133e9a | bellard | if (!(n & 0xF)) { |
525 | 29133e9a | bellard | ret += 4;
|
526 | 29133e9a | bellard | n = n >> 4;
|
527 | 29133e9a | bellard | } |
528 | 29133e9a | bellard | if (!(n & 0x3)) { |
529 | 29133e9a | bellard | ret += 2;
|
530 | 29133e9a | bellard | n = n >> 2;
|
531 | 29133e9a | bellard | } |
532 | 29133e9a | bellard | if (!(n & 0x1)) { |
533 | 29133e9a | bellard | ret++; |
534 | 29133e9a | bellard | n = n >> 1;
|
535 | 29133e9a | bellard | } |
536 | 29133e9a | bellard | #if 0 /* This is not necessary as n is never 0 */
|
537 | 29133e9a | bellard | if (!n)
|
538 | 29133e9a | bellard | ret++;
|
539 | 29133e9a | bellard | #endif
|
540 | 29133e9a | bellard | |
541 | 29133e9a | bellard | return ret;
|
542 | 29133e9a | bellard | } |
543 | 29133e9a | bellard | |
544 | 88eeee0a | balrog | pflash_t *pflash_cfi02_register(target_phys_addr_t base, ram_addr_t off, |
545 | cf6d9118 | balrog | BlockDriverState *bs, uint32_t sector_len, |
546 | 4fbd24ba | balrog | int nb_blocs, int nb_mappings, int width, |
547 | 88eeee0a | balrog | uint16_t id0, uint16_t id1, |
548 | 6725070d | balrog | uint16_t id2, uint16_t id3, |
549 | 6725070d | balrog | uint16_t unlock_addr0, uint16_t unlock_addr1) |
550 | 29133e9a | bellard | { |
551 | 29133e9a | bellard | pflash_t *pfl; |
552 | 4fbd24ba | balrog | int32_t chip_len; |
553 | 29133e9a | bellard | |
554 | 4fbd24ba | balrog | chip_len = sector_len * nb_blocs; |
555 | 29133e9a | bellard | /* XXX: to be fixed */
|
556 | 95d1f3ed | j_mayer | #if 0
|
557 | 29133e9a | bellard | if (total_len != (8 * 1024 * 1024) && total_len != (16 * 1024 * 1024) &&
|
558 | 29133e9a | bellard | total_len != (32 * 1024 * 1024) && total_len != (64 * 1024 * 1024))
|
559 | 29133e9a | bellard | return NULL;
|
560 | 95d1f3ed | j_mayer | #endif
|
561 | 29133e9a | bellard | pfl = qemu_mallocz(sizeof(pflash_t));
|
562 | 29133e9a | bellard | if (pfl == NULL) |
563 | 29133e9a | bellard | return NULL; |
564 | 29133e9a | bellard | pfl->storage = phys_ram_base + off; |
565 | 95d1f3ed | j_mayer | pfl->fl_mem = cpu_register_io_memory(0, pflash_read_ops, pflash_write_ops,
|
566 | 95d1f3ed | j_mayer | pfl); |
567 | 29133e9a | bellard | pfl->off = off; |
568 | 4fbd24ba | balrog | pfl->base = base; |
569 | 4fbd24ba | balrog | pfl->chip_len = chip_len; |
570 | 4fbd24ba | balrog | pfl->mappings = nb_mappings; |
571 | 4fbd24ba | balrog | pflash_register_memory(pfl, 1);
|
572 | 29133e9a | bellard | pfl->bs = bs; |
573 | 29133e9a | bellard | if (pfl->bs) {
|
574 | 29133e9a | bellard | /* read the initial flash content */
|
575 | 4fbd24ba | balrog | bdrv_read(pfl->bs, 0, pfl->storage, chip_len >> 9); |
576 | 29133e9a | bellard | } |
577 | 29133e9a | bellard | #if 0 /* XXX: there should be a bit to set up read-only,
|
578 | 29133e9a | bellard | * the same way the hardware does (with WP pin).
|
579 | 29133e9a | bellard | */
|
580 | 29133e9a | bellard | pfl->ro = 1;
|
581 | 29133e9a | bellard | #else
|
582 | 29133e9a | bellard | pfl->ro = 0;
|
583 | 29133e9a | bellard | #endif
|
584 | 29133e9a | bellard | pfl->timer = qemu_new_timer(vm_clock, pflash_timer, pfl); |
585 | 29133e9a | bellard | pfl->sector_len = sector_len; |
586 | 29133e9a | bellard | pfl->width = width; |
587 | 29133e9a | bellard | pfl->wcycle = 0;
|
588 | 29133e9a | bellard | pfl->cmd = 0;
|
589 | 29133e9a | bellard | pfl->status = 0;
|
590 | 29133e9a | bellard | pfl->ident[0] = id0;
|
591 | 29133e9a | bellard | pfl->ident[1] = id1;
|
592 | 29133e9a | bellard | pfl->ident[2] = id2;
|
593 | 29133e9a | bellard | pfl->ident[3] = id3;
|
594 | 6725070d | balrog | pfl->unlock_addr[0] = unlock_addr0;
|
595 | 6725070d | balrog | pfl->unlock_addr[1] = unlock_addr1;
|
596 | 29133e9a | bellard | /* Hardcoded CFI table (mostly from SG29 Spansion flash) */
|
597 | 29133e9a | bellard | pfl->cfi_len = 0x52;
|
598 | 29133e9a | bellard | /* Standard "QRY" string */
|
599 | 29133e9a | bellard | pfl->cfi_table[0x10] = 'Q'; |
600 | 29133e9a | bellard | pfl->cfi_table[0x11] = 'R'; |
601 | 29133e9a | bellard | pfl->cfi_table[0x12] = 'Y'; |
602 | 29133e9a | bellard | /* Command set (AMD/Fujitsu) */
|
603 | 29133e9a | bellard | pfl->cfi_table[0x13] = 0x02; |
604 | 29133e9a | bellard | pfl->cfi_table[0x14] = 0x00; |
605 | 78556820 | edgar_igl | /* Primary extended table address */
|
606 | 78556820 | edgar_igl | pfl->cfi_table[0x15] = 0x31; |
607 | 29133e9a | bellard | pfl->cfi_table[0x16] = 0x00; |
608 | 29133e9a | bellard | /* Alternate command set (none) */
|
609 | 29133e9a | bellard | pfl->cfi_table[0x17] = 0x00; |
610 | 29133e9a | bellard | pfl->cfi_table[0x18] = 0x00; |
611 | 29133e9a | bellard | /* Alternate extended table (none) */
|
612 | 29133e9a | bellard | pfl->cfi_table[0x19] = 0x00; |
613 | 29133e9a | bellard | pfl->cfi_table[0x1A] = 0x00; |
614 | 29133e9a | bellard | /* Vcc min */
|
615 | 29133e9a | bellard | pfl->cfi_table[0x1B] = 0x27; |
616 | 29133e9a | bellard | /* Vcc max */
|
617 | 29133e9a | bellard | pfl->cfi_table[0x1C] = 0x36; |
618 | 29133e9a | bellard | /* Vpp min (no Vpp pin) */
|
619 | 29133e9a | bellard | pfl->cfi_table[0x1D] = 0x00; |
620 | 29133e9a | bellard | /* Vpp max (no Vpp pin) */
|
621 | 29133e9a | bellard | pfl->cfi_table[0x1E] = 0x00; |
622 | 29133e9a | bellard | /* Reserved */
|
623 | 29133e9a | bellard | pfl->cfi_table[0x1F] = 0x07; |
624 | 78556820 | edgar_igl | /* Timeout for min size buffer write (NA) */
|
625 | 78556820 | edgar_igl | pfl->cfi_table[0x20] = 0x00; |
626 | 29133e9a | bellard | /* Typical timeout for block erase (512 ms) */
|
627 | 29133e9a | bellard | pfl->cfi_table[0x21] = 0x09; |
628 | 29133e9a | bellard | /* Typical timeout for full chip erase (4096 ms) */
|
629 | 29133e9a | bellard | pfl->cfi_table[0x22] = 0x0C; |
630 | 29133e9a | bellard | /* Reserved */
|
631 | 29133e9a | bellard | pfl->cfi_table[0x23] = 0x01; |
632 | 78556820 | edgar_igl | /* Max timeout for buffer write (NA) */
|
633 | 78556820 | edgar_igl | pfl->cfi_table[0x24] = 0x00; |
634 | 29133e9a | bellard | /* Max timeout for block erase */
|
635 | 29133e9a | bellard | pfl->cfi_table[0x25] = 0x0A; |
636 | 29133e9a | bellard | /* Max timeout for chip erase */
|
637 | 29133e9a | bellard | pfl->cfi_table[0x26] = 0x0D; |
638 | 29133e9a | bellard | /* Device size */
|
639 | 78556820 | edgar_igl | pfl->cfi_table[0x27] = ctz32(chip_len);
|
640 | 29133e9a | bellard | /* Flash device interface (8 & 16 bits) */
|
641 | 29133e9a | bellard | pfl->cfi_table[0x28] = 0x02; |
642 | 29133e9a | bellard | pfl->cfi_table[0x29] = 0x00; |
643 | 29133e9a | bellard | /* Max number of bytes in multi-bytes write */
|
644 | 95d1f3ed | j_mayer | /* XXX: disable buffered write as it's not supported */
|
645 | 95d1f3ed | j_mayer | // pfl->cfi_table[0x2A] = 0x05;
|
646 | 95d1f3ed | j_mayer | pfl->cfi_table[0x2A] = 0x00; |
647 | 29133e9a | bellard | pfl->cfi_table[0x2B] = 0x00; |
648 | 29133e9a | bellard | /* Number of erase block regions (uniform) */
|
649 | 29133e9a | bellard | pfl->cfi_table[0x2C] = 0x01; |
650 | 29133e9a | bellard | /* Erase block region 1 */
|
651 | 29133e9a | bellard | pfl->cfi_table[0x2D] = nb_blocs - 1; |
652 | 29133e9a | bellard | pfl->cfi_table[0x2E] = (nb_blocs - 1) >> 8; |
653 | 29133e9a | bellard | pfl->cfi_table[0x2F] = sector_len >> 8; |
654 | 29133e9a | bellard | pfl->cfi_table[0x30] = sector_len >> 16; |
655 | 29133e9a | bellard | |
656 | 78556820 | edgar_igl | /* Extended */
|
657 | 78556820 | edgar_igl | pfl->cfi_table[0x31] = 'P'; |
658 | 78556820 | edgar_igl | pfl->cfi_table[0x32] = 'R'; |
659 | 78556820 | edgar_igl | pfl->cfi_table[0x33] = 'I'; |
660 | 78556820 | edgar_igl | |
661 | 78556820 | edgar_igl | pfl->cfi_table[0x34] = '1'; |
662 | 78556820 | edgar_igl | pfl->cfi_table[0x35] = '0'; |
663 | 78556820 | edgar_igl | |
664 | 78556820 | edgar_igl | pfl->cfi_table[0x36] = 0x00; |
665 | 78556820 | edgar_igl | pfl->cfi_table[0x37] = 0x00; |
666 | 78556820 | edgar_igl | pfl->cfi_table[0x38] = 0x00; |
667 | 78556820 | edgar_igl | pfl->cfi_table[0x39] = 0x00; |
668 | 78556820 | edgar_igl | |
669 | 78556820 | edgar_igl | pfl->cfi_table[0x3a] = 0x00; |
670 | 78556820 | edgar_igl | |
671 | 78556820 | edgar_igl | pfl->cfi_table[0x3b] = 0x00; |
672 | 78556820 | edgar_igl | pfl->cfi_table[0x3c] = 0x00; |
673 | 78556820 | edgar_igl | |
674 | 29133e9a | bellard | return pfl;
|
675 | 29133e9a | bellard | } |