root / hw / nand.c @ a4d5f62c
History | View | Annotate | Download (19.3 kB)
1 | 3e3d5815 | balrog | /*
|
---|---|---|---|
2 | 3e3d5815 | balrog | * Flash NAND memory emulation. Based on "16M x 8 Bit NAND Flash
|
3 | 3e3d5815 | balrog | * Memory" datasheet for the KM29U128AT / K9F2808U0A chips from
|
4 | 3e3d5815 | balrog | * Samsung Electronic.
|
5 | 3e3d5815 | balrog | *
|
6 | 3e3d5815 | balrog | * Copyright (c) 2006 Openedhand Ltd.
|
7 | 3e3d5815 | balrog | * Written by Andrzej Zaborowski <balrog@zabor.org>
|
8 | 3e3d5815 | balrog | *
|
9 | 3e3d5815 | balrog | * This code is licensed under the GNU GPL v2.
|
10 | 3e3d5815 | balrog | */
|
11 | 3e3d5815 | balrog | |
12 | 3e3d5815 | balrog | #ifndef NAND_IO
|
13 | 3e3d5815 | balrog | |
14 | 87ecb68b | pbrook | # include "hw.h" |
15 | 87ecb68b | pbrook | # include "flash.h" |
16 | 87ecb68b | pbrook | # include "block.h" |
17 | 87ecb68b | pbrook | /* FIXME: Pass block device as an argument. */
|
18 | 87ecb68b | pbrook | # include "sysemu.h" |
19 | 3e3d5815 | balrog | |
20 | 3e3d5815 | balrog | # define NAND_CMD_READ0 0x00 |
21 | 3e3d5815 | balrog | # define NAND_CMD_READ1 0x01 |
22 | 3e3d5815 | balrog | # define NAND_CMD_READ2 0x50 |
23 | 3e3d5815 | balrog | # define NAND_CMD_LPREAD2 0x30 |
24 | 3e3d5815 | balrog | # define NAND_CMD_NOSERIALREAD2 0x35 |
25 | 3e3d5815 | balrog | # define NAND_CMD_RANDOMREAD1 0x05 |
26 | 3e3d5815 | balrog | # define NAND_CMD_RANDOMREAD2 0xe0 |
27 | 3e3d5815 | balrog | # define NAND_CMD_READID 0x90 |
28 | 3e3d5815 | balrog | # define NAND_CMD_RESET 0xff |
29 | 3e3d5815 | balrog | # define NAND_CMD_PAGEPROGRAM1 0x80 |
30 | 3e3d5815 | balrog | # define NAND_CMD_PAGEPROGRAM2 0x10 |
31 | 3e3d5815 | balrog | # define NAND_CMD_CACHEPROGRAM2 0x15 |
32 | 3e3d5815 | balrog | # define NAND_CMD_BLOCKERASE1 0x60 |
33 | 3e3d5815 | balrog | # define NAND_CMD_BLOCKERASE2 0xd0 |
34 | 3e3d5815 | balrog | # define NAND_CMD_READSTATUS 0x70 |
35 | 3e3d5815 | balrog | # define NAND_CMD_COPYBACKPRG1 0x85 |
36 | 3e3d5815 | balrog | |
37 | 3e3d5815 | balrog | # define NAND_IOSTATUS_ERROR (1 << 0) |
38 | 3e3d5815 | balrog | # define NAND_IOSTATUS_PLANE0 (1 << 1) |
39 | 3e3d5815 | balrog | # define NAND_IOSTATUS_PLANE1 (1 << 2) |
40 | 3e3d5815 | balrog | # define NAND_IOSTATUS_PLANE2 (1 << 3) |
41 | 3e3d5815 | balrog | # define NAND_IOSTATUS_PLANE3 (1 << 4) |
42 | 3e3d5815 | balrog | # define NAND_IOSTATUS_BUSY (1 << 6) |
43 | 3e3d5815 | balrog | # define NAND_IOSTATUS_UNPROTCT (1 << 7) |
44 | 3e3d5815 | balrog | |
45 | 3e3d5815 | balrog | # define MAX_PAGE 0x800 |
46 | 3e3d5815 | balrog | # define MAX_OOB 0x40 |
47 | 3e3d5815 | balrog | |
48 | bc24a225 | Paul Brook | struct NANDFlashState {
|
49 | 3e3d5815 | balrog | uint8_t manf_id, chip_id; |
50 | 3e3d5815 | balrog | int size, pages;
|
51 | 3e3d5815 | balrog | int page_shift, oob_shift, erase_shift, addr_shift;
|
52 | 3e3d5815 | balrog | uint8_t *storage; |
53 | 3e3d5815 | balrog | BlockDriverState *bdrv; |
54 | 3e3d5815 | balrog | int mem_oob;
|
55 | 3e3d5815 | balrog | |
56 | 3e3d5815 | balrog | int cle, ale, ce, wp, gnd;
|
57 | 3e3d5815 | balrog | |
58 | 3e3d5815 | balrog | uint8_t io[MAX_PAGE + MAX_OOB + 0x400];
|
59 | 3e3d5815 | balrog | uint8_t *ioaddr; |
60 | 3e3d5815 | balrog | int iolen;
|
61 | 3e3d5815 | balrog | |
62 | 3e3d5815 | balrog | uint32_t cmd, addr; |
63 | 3e3d5815 | balrog | int addrlen;
|
64 | 3e3d5815 | balrog | int status;
|
65 | 3e3d5815 | balrog | int offset;
|
66 | 3e3d5815 | balrog | |
67 | bc24a225 | Paul Brook | void (*blk_write)(NANDFlashState *s);
|
68 | bc24a225 | Paul Brook | void (*blk_erase)(NANDFlashState *s);
|
69 | bc24a225 | Paul Brook | void (*blk_load)(NANDFlashState *s, uint32_t addr, int offset); |
70 | 3e3d5815 | balrog | }; |
71 | 3e3d5815 | balrog | |
72 | 3e3d5815 | balrog | # define NAND_NO_AUTOINCR 0x00000001 |
73 | 3e3d5815 | balrog | # define NAND_BUSWIDTH_16 0x00000002 |
74 | 3e3d5815 | balrog | # define NAND_NO_PADDING 0x00000004 |
75 | 3e3d5815 | balrog | # define NAND_CACHEPRG 0x00000008 |
76 | 3e3d5815 | balrog | # define NAND_COPYBACK 0x00000010 |
77 | 3e3d5815 | balrog | # define NAND_IS_AND 0x00000020 |
78 | 3e3d5815 | balrog | # define NAND_4PAGE_ARRAY 0x00000040 |
79 | 3e3d5815 | balrog | # define NAND_NO_READRDY 0x00000100 |
80 | 3e3d5815 | balrog | # define NAND_SAMSUNG_LP (NAND_NO_PADDING | NAND_COPYBACK)
|
81 | 3e3d5815 | balrog | |
82 | 3e3d5815 | balrog | # define NAND_IO
|
83 | 3e3d5815 | balrog | |
84 | 3e3d5815 | balrog | # define PAGE(addr) ((addr) >> ADDR_SHIFT)
|
85 | 3e3d5815 | balrog | # define PAGE_START(page) (PAGE(page) * (PAGE_SIZE + OOB_SIZE))
|
86 | 3e3d5815 | balrog | # define PAGE_MASK ((1 << ADDR_SHIFT) - 1) |
87 | 3e3d5815 | balrog | # define OOB_SHIFT (PAGE_SHIFT - 5) |
88 | 3e3d5815 | balrog | # define OOB_SIZE (1 << OOB_SHIFT) |
89 | 3e3d5815 | balrog | # define SECTOR(addr) ((addr) >> (9 + ADDR_SHIFT - PAGE_SHIFT)) |
90 | 3e3d5815 | balrog | # define SECTOR_OFFSET(addr) ((addr) & ((511 >> PAGE_SHIFT) << 8)) |
91 | 3e3d5815 | balrog | |
92 | 3e3d5815 | balrog | # define PAGE_SIZE 256 |
93 | 3e3d5815 | balrog | # define PAGE_SHIFT 8 |
94 | 3e3d5815 | balrog | # define PAGE_SECTORS 1 |
95 | 3e3d5815 | balrog | # define ADDR_SHIFT 8 |
96 | 3e3d5815 | balrog | # include "nand.c" |
97 | 3e3d5815 | balrog | # define PAGE_SIZE 512 |
98 | 3e3d5815 | balrog | # define PAGE_SHIFT 9 |
99 | 3e3d5815 | balrog | # define PAGE_SECTORS 1 |
100 | 3e3d5815 | balrog | # define ADDR_SHIFT 8 |
101 | 3e3d5815 | balrog | # include "nand.c" |
102 | 3e3d5815 | balrog | # define PAGE_SIZE 2048 |
103 | 3e3d5815 | balrog | # define PAGE_SHIFT 11 |
104 | 3e3d5815 | balrog | # define PAGE_SECTORS 4 |
105 | 3e3d5815 | balrog | # define ADDR_SHIFT 16 |
106 | 3e3d5815 | balrog | # include "nand.c" |
107 | 3e3d5815 | balrog | |
108 | 3e3d5815 | balrog | /* Information based on Linux drivers/mtd/nand/nand_ids.c */
|
109 | bc24a225 | Paul Brook | static const struct { |
110 | 3e3d5815 | balrog | int size;
|
111 | 3e3d5815 | balrog | int width;
|
112 | 3e3d5815 | balrog | int page_shift;
|
113 | 3e3d5815 | balrog | int erase_shift;
|
114 | 3e3d5815 | balrog | uint32_t options; |
115 | 3e3d5815 | balrog | } nand_flash_ids[0x100] = {
|
116 | 3e3d5815 | balrog | [0 ... 0xff] = { 0 }, |
117 | 3e3d5815 | balrog | |
118 | 3e3d5815 | balrog | [0x6e] = { 1, 8, 8, 4, 0 }, |
119 | 3e3d5815 | balrog | [0x64] = { 2, 8, 8, 4, 0 }, |
120 | 3e3d5815 | balrog | [0x6b] = { 4, 8, 9, 4, 0 }, |
121 | 3e3d5815 | balrog | [0xe8] = { 1, 8, 8, 4, 0 }, |
122 | 3e3d5815 | balrog | [0xec] = { 1, 8, 8, 4, 0 }, |
123 | 3e3d5815 | balrog | [0xea] = { 2, 8, 8, 4, 0 }, |
124 | 3e3d5815 | balrog | [0xd5] = { 4, 8, 9, 4, 0 }, |
125 | 3e3d5815 | balrog | [0xe3] = { 4, 8, 9, 4, 0 }, |
126 | 3e3d5815 | balrog | [0xe5] = { 4, 8, 9, 4, 0 }, |
127 | 3e3d5815 | balrog | [0xd6] = { 8, 8, 9, 4, 0 }, |
128 | 3e3d5815 | balrog | |
129 | 3e3d5815 | balrog | [0x39] = { 8, 8, 9, 4, 0 }, |
130 | 3e3d5815 | balrog | [0xe6] = { 8, 8, 9, 4, 0 }, |
131 | 3e3d5815 | balrog | [0x49] = { 8, 16, 9, 4, NAND_BUSWIDTH_16 }, |
132 | 3e3d5815 | balrog | [0x59] = { 8, 16, 9, 4, NAND_BUSWIDTH_16 }, |
133 | 3e3d5815 | balrog | |
134 | 3e3d5815 | balrog | [0x33] = { 16, 8, 9, 5, 0 }, |
135 | 3e3d5815 | balrog | [0x73] = { 16, 8, 9, 5, 0 }, |
136 | 3e3d5815 | balrog | [0x43] = { 16, 16, 9, 5, NAND_BUSWIDTH_16 }, |
137 | 3e3d5815 | balrog | [0x53] = { 16, 16, 9, 5, NAND_BUSWIDTH_16 }, |
138 | 3e3d5815 | balrog | |
139 | 3e3d5815 | balrog | [0x35] = { 32, 8, 9, 5, 0 }, |
140 | 3e3d5815 | balrog | [0x75] = { 32, 8, 9, 5, 0 }, |
141 | 3e3d5815 | balrog | [0x45] = { 32, 16, 9, 5, NAND_BUSWIDTH_16 }, |
142 | 3e3d5815 | balrog | [0x55] = { 32, 16, 9, 5, NAND_BUSWIDTH_16 }, |
143 | 3e3d5815 | balrog | |
144 | 3e3d5815 | balrog | [0x36] = { 64, 8, 9, 5, 0 }, |
145 | 3e3d5815 | balrog | [0x76] = { 64, 8, 9, 5, 0 }, |
146 | 3e3d5815 | balrog | [0x46] = { 64, 16, 9, 5, NAND_BUSWIDTH_16 }, |
147 | 3e3d5815 | balrog | [0x56] = { 64, 16, 9, 5, NAND_BUSWIDTH_16 }, |
148 | 3e3d5815 | balrog | |
149 | 3e3d5815 | balrog | [0x78] = { 128, 8, 9, 5, 0 }, |
150 | 3e3d5815 | balrog | [0x39] = { 128, 8, 9, 5, 0 }, |
151 | 3e3d5815 | balrog | [0x79] = { 128, 8, 9, 5, 0 }, |
152 | 3e3d5815 | balrog | [0x72] = { 128, 16, 9, 5, NAND_BUSWIDTH_16 }, |
153 | 3e3d5815 | balrog | [0x49] = { 128, 16, 9, 5, NAND_BUSWIDTH_16 }, |
154 | 3e3d5815 | balrog | [0x74] = { 128, 16, 9, 5, NAND_BUSWIDTH_16 }, |
155 | 3e3d5815 | balrog | [0x59] = { 128, 16, 9, 5, NAND_BUSWIDTH_16 }, |
156 | 3e3d5815 | balrog | |
157 | 3e3d5815 | balrog | [0x71] = { 256, 8, 9, 5, 0 }, |
158 | 3e3d5815 | balrog | |
159 | 3e3d5815 | balrog | /*
|
160 | 3e3d5815 | balrog | * These are the new chips with large page size. The pagesize and the
|
161 | 3e3d5815 | balrog | * erasesize is determined from the extended id bytes
|
162 | 3e3d5815 | balrog | */
|
163 | 3e3d5815 | balrog | # define LP_OPTIONS (NAND_SAMSUNG_LP | NAND_NO_READRDY | NAND_NO_AUTOINCR)
|
164 | 3e3d5815 | balrog | # define LP_OPTIONS16 (LP_OPTIONS | NAND_BUSWIDTH_16)
|
165 | 3e3d5815 | balrog | |
166 | 3e3d5815 | balrog | /* 512 Megabit */
|
167 | 3e3d5815 | balrog | [0xa2] = { 64, 8, 0, 0, LP_OPTIONS }, |
168 | 3e3d5815 | balrog | [0xf2] = { 64, 8, 0, 0, LP_OPTIONS }, |
169 | 3e3d5815 | balrog | [0xb2] = { 64, 16, 0, 0, LP_OPTIONS16 }, |
170 | 3e3d5815 | balrog | [0xc2] = { 64, 16, 0, 0, LP_OPTIONS16 }, |
171 | 3e3d5815 | balrog | |
172 | 3e3d5815 | balrog | /* 1 Gigabit */
|
173 | 3e3d5815 | balrog | [0xa1] = { 128, 8, 0, 0, LP_OPTIONS }, |
174 | 3e3d5815 | balrog | [0xf1] = { 128, 8, 0, 0, LP_OPTIONS }, |
175 | 3e3d5815 | balrog | [0xb1] = { 128, 16, 0, 0, LP_OPTIONS16 }, |
176 | 3e3d5815 | balrog | [0xc1] = { 128, 16, 0, 0, LP_OPTIONS16 }, |
177 | 3e3d5815 | balrog | |
178 | 3e3d5815 | balrog | /* 2 Gigabit */
|
179 | 3e3d5815 | balrog | [0xaa] = { 256, 8, 0, 0, LP_OPTIONS }, |
180 | 3e3d5815 | balrog | [0xda] = { 256, 8, 0, 0, LP_OPTIONS }, |
181 | 3e3d5815 | balrog | [0xba] = { 256, 16, 0, 0, LP_OPTIONS16 }, |
182 | 3e3d5815 | balrog | [0xca] = { 256, 16, 0, 0, LP_OPTIONS16 }, |
183 | 3e3d5815 | balrog | |
184 | 3e3d5815 | balrog | /* 4 Gigabit */
|
185 | 3e3d5815 | balrog | [0xac] = { 512, 8, 0, 0, LP_OPTIONS }, |
186 | 3e3d5815 | balrog | [0xdc] = { 512, 8, 0, 0, LP_OPTIONS }, |
187 | 3e3d5815 | balrog | [0xbc] = { 512, 16, 0, 0, LP_OPTIONS16 }, |
188 | 3e3d5815 | balrog | [0xcc] = { 512, 16, 0, 0, LP_OPTIONS16 }, |
189 | 3e3d5815 | balrog | |
190 | 3e3d5815 | balrog | /* 8 Gigabit */
|
191 | 3e3d5815 | balrog | [0xa3] = { 1024, 8, 0, 0, LP_OPTIONS }, |
192 | 3e3d5815 | balrog | [0xd3] = { 1024, 8, 0, 0, LP_OPTIONS }, |
193 | 3e3d5815 | balrog | [0xb3] = { 1024, 16, 0, 0, LP_OPTIONS16 }, |
194 | 3e3d5815 | balrog | [0xc3] = { 1024, 16, 0, 0, LP_OPTIONS16 }, |
195 | 3e3d5815 | balrog | |
196 | 3e3d5815 | balrog | /* 16 Gigabit */
|
197 | 3e3d5815 | balrog | [0xa5] = { 2048, 8, 0, 0, LP_OPTIONS }, |
198 | 3e3d5815 | balrog | [0xd5] = { 2048, 8, 0, 0, LP_OPTIONS }, |
199 | 3e3d5815 | balrog | [0xb5] = { 2048, 16, 0, 0, LP_OPTIONS16 }, |
200 | 3e3d5815 | balrog | [0xc5] = { 2048, 16, 0, 0, LP_OPTIONS16 }, |
201 | 3e3d5815 | balrog | }; |
202 | 3e3d5815 | balrog | |
203 | bc24a225 | Paul Brook | static void nand_reset(NANDFlashState *s) |
204 | 3e3d5815 | balrog | { |
205 | 3e3d5815 | balrog | s->cmd = NAND_CMD_READ0; |
206 | 3e3d5815 | balrog | s->addr = 0;
|
207 | 3e3d5815 | balrog | s->addrlen = 0;
|
208 | 3e3d5815 | balrog | s->iolen = 0;
|
209 | 3e3d5815 | balrog | s->offset = 0;
|
210 | 3e3d5815 | balrog | s->status &= NAND_IOSTATUS_UNPROTCT; |
211 | 3e3d5815 | balrog | } |
212 | 3e3d5815 | balrog | |
213 | bc24a225 | Paul Brook | static void nand_command(NANDFlashState *s) |
214 | 3e3d5815 | balrog | { |
215 | fccd2613 | Edgar E. Iglesias | unsigned int offset; |
216 | 3e3d5815 | balrog | switch (s->cmd) {
|
217 | 3e3d5815 | balrog | case NAND_CMD_READ0:
|
218 | 3e3d5815 | balrog | s->iolen = 0;
|
219 | 3e3d5815 | balrog | break;
|
220 | 3e3d5815 | balrog | |
221 | 3e3d5815 | balrog | case NAND_CMD_READID:
|
222 | 3e3d5815 | balrog | s->io[0] = s->manf_id;
|
223 | 3e3d5815 | balrog | s->io[1] = s->chip_id;
|
224 | 3e3d5815 | balrog | s->io[2] = 'Q'; /* Don't-care byte (often 0xa5) */ |
225 | 3e3d5815 | balrog | if (nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP)
|
226 | 3e3d5815 | balrog | s->io[3] = 0x15; /* Page Size, Block Size, Spare Size.. */ |
227 | 3e3d5815 | balrog | else
|
228 | 3e3d5815 | balrog | s->io[3] = 0xc0; /* Multi-plane */ |
229 | 3e3d5815 | balrog | s->ioaddr = s->io; |
230 | 3e3d5815 | balrog | s->iolen = 4;
|
231 | 3e3d5815 | balrog | break;
|
232 | 3e3d5815 | balrog | |
233 | 3e3d5815 | balrog | case NAND_CMD_RANDOMREAD2:
|
234 | 3e3d5815 | balrog | case NAND_CMD_NOSERIALREAD2:
|
235 | 3e3d5815 | balrog | if (!(nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP))
|
236 | 3e3d5815 | balrog | break;
|
237 | fccd2613 | Edgar E. Iglesias | offset = s->addr & ((1 << s->addr_shift) - 1); |
238 | fccd2613 | Edgar E. Iglesias | s->blk_load(s, s->addr, offset); |
239 | fccd2613 | Edgar E. Iglesias | if (s->gnd)
|
240 | fccd2613 | Edgar E. Iglesias | s->iolen = (1 << s->page_shift) - offset;
|
241 | fccd2613 | Edgar E. Iglesias | else
|
242 | fccd2613 | Edgar E. Iglesias | s->iolen = (1 << s->page_shift) + (1 << s->oob_shift) - offset; |
243 | 3e3d5815 | balrog | break;
|
244 | 3e3d5815 | balrog | |
245 | 3e3d5815 | balrog | case NAND_CMD_RESET:
|
246 | 3e3d5815 | balrog | nand_reset(s); |
247 | 3e3d5815 | balrog | break;
|
248 | 3e3d5815 | balrog | |
249 | 3e3d5815 | balrog | case NAND_CMD_PAGEPROGRAM1:
|
250 | 3e3d5815 | balrog | s->ioaddr = s->io; |
251 | 3e3d5815 | balrog | s->iolen = 0;
|
252 | 3e3d5815 | balrog | break;
|
253 | 3e3d5815 | balrog | |
254 | 3e3d5815 | balrog | case NAND_CMD_PAGEPROGRAM2:
|
255 | 3e3d5815 | balrog | if (s->wp) {
|
256 | 3e3d5815 | balrog | s->blk_write(s); |
257 | 3e3d5815 | balrog | } |
258 | 3e3d5815 | balrog | break;
|
259 | 3e3d5815 | balrog | |
260 | 3e3d5815 | balrog | case NAND_CMD_BLOCKERASE1:
|
261 | 3e3d5815 | balrog | break;
|
262 | 3e3d5815 | balrog | |
263 | 3e3d5815 | balrog | case NAND_CMD_BLOCKERASE2:
|
264 | 3e3d5815 | balrog | if (nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP)
|
265 | 3e3d5815 | balrog | s->addr <<= 16;
|
266 | 3e3d5815 | balrog | else
|
267 | 3e3d5815 | balrog | s->addr <<= 8;
|
268 | 3e3d5815 | balrog | |
269 | 3e3d5815 | balrog | if (s->wp) {
|
270 | 3e3d5815 | balrog | s->blk_erase(s); |
271 | 3e3d5815 | balrog | } |
272 | 3e3d5815 | balrog | break;
|
273 | 3e3d5815 | balrog | |
274 | 3e3d5815 | balrog | case NAND_CMD_READSTATUS:
|
275 | 3e3d5815 | balrog | s->io[0] = s->status;
|
276 | 3e3d5815 | balrog | s->ioaddr = s->io; |
277 | 3e3d5815 | balrog | s->iolen = 1;
|
278 | 3e3d5815 | balrog | break;
|
279 | 3e3d5815 | balrog | |
280 | 3e3d5815 | balrog | default:
|
281 | 3e3d5815 | balrog | printf("%s: Unknown NAND command 0x%02x\n", __FUNCTION__, s->cmd);
|
282 | 3e3d5815 | balrog | } |
283 | 3e3d5815 | balrog | } |
284 | 3e3d5815 | balrog | |
285 | aa941b94 | balrog | static void nand_save(QEMUFile *f, void *opaque) |
286 | aa941b94 | balrog | { |
287 | bc24a225 | Paul Brook | NANDFlashState *s = (NANDFlashState *) opaque; |
288 | aa941b94 | balrog | qemu_put_byte(f, s->cle); |
289 | aa941b94 | balrog | qemu_put_byte(f, s->ale); |
290 | aa941b94 | balrog | qemu_put_byte(f, s->ce); |
291 | aa941b94 | balrog | qemu_put_byte(f, s->wp); |
292 | aa941b94 | balrog | qemu_put_byte(f, s->gnd); |
293 | aa941b94 | balrog | qemu_put_buffer(f, s->io, sizeof(s->io));
|
294 | aa941b94 | balrog | qemu_put_be32(f, s->ioaddr - s->io); |
295 | aa941b94 | balrog | qemu_put_be32(f, s->iolen); |
296 | aa941b94 | balrog | |
297 | aa941b94 | balrog | qemu_put_be32s(f, &s->cmd); |
298 | aa941b94 | balrog | qemu_put_be32s(f, &s->addr); |
299 | aa941b94 | balrog | qemu_put_be32(f, s->addrlen); |
300 | aa941b94 | balrog | qemu_put_be32(f, s->status); |
301 | aa941b94 | balrog | qemu_put_be32(f, s->offset); |
302 | aa941b94 | balrog | /* XXX: do we want to save s->storage too? */
|
303 | aa941b94 | balrog | } |
304 | aa941b94 | balrog | |
305 | aa941b94 | balrog | static int nand_load(QEMUFile *f, void *opaque, int version_id) |
306 | aa941b94 | balrog | { |
307 | bc24a225 | Paul Brook | NANDFlashState *s = (NANDFlashState *) opaque; |
308 | aa941b94 | balrog | s->cle = qemu_get_byte(f); |
309 | aa941b94 | balrog | s->ale = qemu_get_byte(f); |
310 | aa941b94 | balrog | s->ce = qemu_get_byte(f); |
311 | aa941b94 | balrog | s->wp = qemu_get_byte(f); |
312 | aa941b94 | balrog | s->gnd = qemu_get_byte(f); |
313 | aa941b94 | balrog | qemu_get_buffer(f, s->io, sizeof(s->io));
|
314 | aa941b94 | balrog | s->ioaddr = s->io + qemu_get_be32(f); |
315 | aa941b94 | balrog | s->iolen = qemu_get_be32(f); |
316 | aa941b94 | balrog | if (s->ioaddr >= s->io + sizeof(s->io) || s->ioaddr < s->io) |
317 | aa941b94 | balrog | return -EINVAL;
|
318 | aa941b94 | balrog | |
319 | aa941b94 | balrog | qemu_get_be32s(f, &s->cmd); |
320 | aa941b94 | balrog | qemu_get_be32s(f, &s->addr); |
321 | aa941b94 | balrog | s->addrlen = qemu_get_be32(f); |
322 | aa941b94 | balrog | s->status = qemu_get_be32(f); |
323 | aa941b94 | balrog | s->offset = qemu_get_be32(f); |
324 | aa941b94 | balrog | return 0; |
325 | aa941b94 | balrog | } |
326 | aa941b94 | balrog | |
327 | 3e3d5815 | balrog | /*
|
328 | 3e3d5815 | balrog | * Chip inputs are CLE, ALE, CE, WP, GND and eight I/O pins. Chip
|
329 | 3e3d5815 | balrog | * outputs are R/B and eight I/O pins.
|
330 | 3e3d5815 | balrog | *
|
331 | 3e3d5815 | balrog | * CE, WP and R/B are active low.
|
332 | 3e3d5815 | balrog | */
|
333 | bc24a225 | Paul Brook | void nand_setpins(NANDFlashState *s,
|
334 | 3e3d5815 | balrog | int cle, int ale, int ce, int wp, int gnd) |
335 | 3e3d5815 | balrog | { |
336 | 3e3d5815 | balrog | s->cle = cle; |
337 | 3e3d5815 | balrog | s->ale = ale; |
338 | 3e3d5815 | balrog | s->ce = ce; |
339 | 3e3d5815 | balrog | s->wp = wp; |
340 | 3e3d5815 | balrog | s->gnd = gnd; |
341 | 3e3d5815 | balrog | if (wp)
|
342 | 3e3d5815 | balrog | s->status |= NAND_IOSTATUS_UNPROTCT; |
343 | 3e3d5815 | balrog | else
|
344 | 3e3d5815 | balrog | s->status &= ~NAND_IOSTATUS_UNPROTCT; |
345 | 3e3d5815 | balrog | } |
346 | 3e3d5815 | balrog | |
347 | bc24a225 | Paul Brook | void nand_getpins(NANDFlashState *s, int *rb) |
348 | 3e3d5815 | balrog | { |
349 | 3e3d5815 | balrog | *rb = 1;
|
350 | 3e3d5815 | balrog | } |
351 | 3e3d5815 | balrog | |
352 | bc24a225 | Paul Brook | void nand_setio(NANDFlashState *s, uint8_t value)
|
353 | 3e3d5815 | balrog | { |
354 | 3e3d5815 | balrog | if (!s->ce && s->cle) {
|
355 | 3e3d5815 | balrog | if (nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) {
|
356 | 3e3d5815 | balrog | if (s->cmd == NAND_CMD_READ0 && value == NAND_CMD_LPREAD2)
|
357 | 3e3d5815 | balrog | return;
|
358 | 3e3d5815 | balrog | if (value == NAND_CMD_RANDOMREAD1) {
|
359 | 3e3d5815 | balrog | s->addr &= ~((1 << s->addr_shift) - 1); |
360 | 3e3d5815 | balrog | s->addrlen = 0;
|
361 | 3e3d5815 | balrog | return;
|
362 | 3e3d5815 | balrog | } |
363 | 3e3d5815 | balrog | } |
364 | 3e3d5815 | balrog | if (value == NAND_CMD_READ0)
|
365 | 3e3d5815 | balrog | s->offset = 0;
|
366 | 3e3d5815 | balrog | else if (value == NAND_CMD_READ1) { |
367 | 3e3d5815 | balrog | s->offset = 0x100;
|
368 | 3e3d5815 | balrog | value = NAND_CMD_READ0; |
369 | 3e3d5815 | balrog | } |
370 | 3e3d5815 | balrog | else if (value == NAND_CMD_READ2) { |
371 | 3e3d5815 | balrog | s->offset = 1 << s->page_shift;
|
372 | 3e3d5815 | balrog | value = NAND_CMD_READ0; |
373 | 3e3d5815 | balrog | } |
374 | 3e3d5815 | balrog | |
375 | 3e3d5815 | balrog | s->cmd = value; |
376 | 3e3d5815 | balrog | |
377 | 3e3d5815 | balrog | if (s->cmd == NAND_CMD_READSTATUS ||
|
378 | 3e3d5815 | balrog | s->cmd == NAND_CMD_PAGEPROGRAM2 || |
379 | 3e3d5815 | balrog | s->cmd == NAND_CMD_BLOCKERASE1 || |
380 | 3e3d5815 | balrog | s->cmd == NAND_CMD_BLOCKERASE2 || |
381 | 3e3d5815 | balrog | s->cmd == NAND_CMD_NOSERIALREAD2 || |
382 | 3e3d5815 | balrog | s->cmd == NAND_CMD_RANDOMREAD2 || |
383 | 3e3d5815 | balrog | s->cmd == NAND_CMD_RESET) |
384 | 3e3d5815 | balrog | nand_command(s); |
385 | 3e3d5815 | balrog | |
386 | 3e3d5815 | balrog | if (s->cmd != NAND_CMD_RANDOMREAD2) {
|
387 | 3e3d5815 | balrog | s->addrlen = 0;
|
388 | 3e3d5815 | balrog | } |
389 | 3e3d5815 | balrog | } |
390 | 3e3d5815 | balrog | |
391 | 3e3d5815 | balrog | if (s->ale) {
|
392 | fccd2613 | Edgar E. Iglesias | unsigned int shift = s->addrlen * 8; |
393 | fccd2613 | Edgar E. Iglesias | unsigned int mask = ~(0xff << shift); |
394 | fccd2613 | Edgar E. Iglesias | unsigned int v = value << shift; |
395 | fccd2613 | Edgar E. Iglesias | |
396 | fccd2613 | Edgar E. Iglesias | s->addr = (s->addr & mask) | v; |
397 | 3e3d5815 | balrog | s->addrlen ++; |
398 | 3e3d5815 | balrog | |
399 | 3e3d5815 | balrog | if (s->addrlen == 1 && s->cmd == NAND_CMD_READID) |
400 | 3e3d5815 | balrog | nand_command(s); |
401 | 3e3d5815 | balrog | |
402 | 3e3d5815 | balrog | if (!(nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) &&
|
403 | 3e3d5815 | balrog | s->addrlen == 3 && (
|
404 | 3e3d5815 | balrog | s->cmd == NAND_CMD_READ0 || |
405 | 3e3d5815 | balrog | s->cmd == NAND_CMD_PAGEPROGRAM1)) |
406 | 3e3d5815 | balrog | nand_command(s); |
407 | 3e3d5815 | balrog | if ((nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) &&
|
408 | 3e3d5815 | balrog | s->addrlen == 4 && (
|
409 | 3e3d5815 | balrog | s->cmd == NAND_CMD_READ0 || |
410 | 3e3d5815 | balrog | s->cmd == NAND_CMD_PAGEPROGRAM1)) |
411 | 3e3d5815 | balrog | nand_command(s); |
412 | 3e3d5815 | balrog | } |
413 | 3e3d5815 | balrog | |
414 | 3e3d5815 | balrog | if (!s->cle && !s->ale && s->cmd == NAND_CMD_PAGEPROGRAM1) {
|
415 | 3e3d5815 | balrog | if (s->iolen < (1 << s->page_shift) + (1 << s->oob_shift)) |
416 | 3e3d5815 | balrog | s->io[s->iolen ++] = value; |
417 | 3e3d5815 | balrog | } else if (!s->cle && !s->ale && s->cmd == NAND_CMD_COPYBACKPRG1) { |
418 | 3e3d5815 | balrog | if ((s->addr & ((1 << s->addr_shift) - 1)) < |
419 | 3e3d5815 | balrog | (1 << s->page_shift) + (1 << s->oob_shift)) { |
420 | 3e3d5815 | balrog | s->io[s->iolen + (s->addr & ((1 << s->addr_shift) - 1))] = value; |
421 | 3e3d5815 | balrog | s->addr ++; |
422 | 3e3d5815 | balrog | } |
423 | 3e3d5815 | balrog | } |
424 | 3e3d5815 | balrog | } |
425 | 3e3d5815 | balrog | |
426 | bc24a225 | Paul Brook | uint8_t nand_getio(NANDFlashState *s) |
427 | 3e3d5815 | balrog | { |
428 | 3e3d5815 | balrog | int offset;
|
429 | 5fafdf24 | ths | |
430 | 3e3d5815 | balrog | /* Allow sequential reading */
|
431 | 3e3d5815 | balrog | if (!s->iolen && s->cmd == NAND_CMD_READ0) {
|
432 | 3e3d5815 | balrog | offset = (s->addr & ((1 << s->addr_shift) - 1)) + s->offset; |
433 | 3e3d5815 | balrog | s->offset = 0;
|
434 | 3e3d5815 | balrog | |
435 | 3e3d5815 | balrog | s->blk_load(s, s->addr, offset); |
436 | 3e3d5815 | balrog | if (s->gnd)
|
437 | 3e3d5815 | balrog | s->iolen = (1 << s->page_shift) - offset;
|
438 | 3e3d5815 | balrog | else
|
439 | 3e3d5815 | balrog | s->iolen = (1 << s->page_shift) + (1 << s->oob_shift) - offset; |
440 | 3e3d5815 | balrog | } |
441 | 3e3d5815 | balrog | |
442 | 3e3d5815 | balrog | if (s->ce || s->iolen <= 0) |
443 | 3e3d5815 | balrog | return 0; |
444 | 3e3d5815 | balrog | |
445 | 3e3d5815 | balrog | s->iolen --; |
446 | fccd2613 | Edgar E. Iglesias | s->addr++; |
447 | 3e3d5815 | balrog | return *(s->ioaddr ++);
|
448 | 3e3d5815 | balrog | } |
449 | 3e3d5815 | balrog | |
450 | bc24a225 | Paul Brook | NANDFlashState *nand_init(int manf_id, int chip_id) |
451 | 3e3d5815 | balrog | { |
452 | 3e3d5815 | balrog | int pagesize;
|
453 | bc24a225 | Paul Brook | NANDFlashState *s; |
454 | 751c6a17 | Gerd Hoffmann | DriveInfo *dinfo; |
455 | 3e3d5815 | balrog | |
456 | 3e3d5815 | balrog | if (nand_flash_ids[chip_id].size == 0) { |
457 | 2ac71179 | Paul Brook | hw_error("%s: Unsupported NAND chip ID.\n", __FUNCTION__);
|
458 | 3e3d5815 | balrog | } |
459 | 3e3d5815 | balrog | |
460 | bc24a225 | Paul Brook | s = (NANDFlashState *) qemu_mallocz(sizeof(NANDFlashState));
|
461 | 751c6a17 | Gerd Hoffmann | dinfo = drive_get(IF_MTD, 0, 0); |
462 | 751c6a17 | Gerd Hoffmann | if (dinfo)
|
463 | 751c6a17 | Gerd Hoffmann | s->bdrv = dinfo->bdrv; |
464 | 3e3d5815 | balrog | s->manf_id = manf_id; |
465 | 3e3d5815 | balrog | s->chip_id = chip_id; |
466 | 3e3d5815 | balrog | s->size = nand_flash_ids[s->chip_id].size << 20;
|
467 | 3e3d5815 | balrog | if (nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) {
|
468 | 3e3d5815 | balrog | s->page_shift = 11;
|
469 | 3e3d5815 | balrog | s->erase_shift = 6;
|
470 | 3e3d5815 | balrog | } else {
|
471 | 3e3d5815 | balrog | s->page_shift = nand_flash_ids[s->chip_id].page_shift; |
472 | 3e3d5815 | balrog | s->erase_shift = nand_flash_ids[s->chip_id].erase_shift; |
473 | 3e3d5815 | balrog | } |
474 | 3e3d5815 | balrog | |
475 | 3e3d5815 | balrog | switch (1 << s->page_shift) { |
476 | 3e3d5815 | balrog | case 256: |
477 | 3e3d5815 | balrog | nand_init_256(s); |
478 | 3e3d5815 | balrog | break;
|
479 | 3e3d5815 | balrog | case 512: |
480 | 3e3d5815 | balrog | nand_init_512(s); |
481 | 3e3d5815 | balrog | break;
|
482 | 3e3d5815 | balrog | case 2048: |
483 | 3e3d5815 | balrog | nand_init_2048(s); |
484 | 3e3d5815 | balrog | break;
|
485 | 3e3d5815 | balrog | default:
|
486 | 2ac71179 | Paul Brook | hw_error("%s: Unsupported NAND block size.\n", __FUNCTION__);
|
487 | 3e3d5815 | balrog | } |
488 | 3e3d5815 | balrog | |
489 | 3e3d5815 | balrog | pagesize = 1 << s->oob_shift;
|
490 | 3e3d5815 | balrog | s->mem_oob = 1;
|
491 | 3e3d5815 | balrog | if (s->bdrv && bdrv_getlength(s->bdrv) >=
|
492 | 3e3d5815 | balrog | (s->pages << s->page_shift) + (s->pages << s->oob_shift)) { |
493 | 3e3d5815 | balrog | pagesize = 0;
|
494 | 3e3d5815 | balrog | s->mem_oob = 0;
|
495 | 3e3d5815 | balrog | } |
496 | 3e3d5815 | balrog | |
497 | 3e3d5815 | balrog | if (!s->bdrv)
|
498 | 3e3d5815 | balrog | pagesize += 1 << s->page_shift;
|
499 | 3e3d5815 | balrog | if (pagesize)
|
500 | 3e3d5815 | balrog | s->storage = (uint8_t *) memset(qemu_malloc(s->pages * pagesize), |
501 | 3e3d5815 | balrog | 0xff, s->pages * pagesize);
|
502 | 48927926 | pbrook | /* Give s->ioaddr a sane value in case we save state before it
|
503 | 48927926 | pbrook | is used. */
|
504 | 48927926 | pbrook | s->ioaddr = s->io; |
505 | aa941b94 | balrog | |
506 | 18be5187 | pbrook | register_savevm("nand", -1, 0, nand_save, nand_load, s); |
507 | aa941b94 | balrog | |
508 | 3e3d5815 | balrog | return s;
|
509 | 3e3d5815 | balrog | } |
510 | 3e3d5815 | balrog | |
511 | bc24a225 | Paul Brook | void nand_done(NANDFlashState *s)
|
512 | 3e3d5815 | balrog | { |
513 | 3e3d5815 | balrog | if (s->bdrv) {
|
514 | 3e3d5815 | balrog | bdrv_close(s->bdrv); |
515 | 3e3d5815 | balrog | bdrv_delete(s->bdrv); |
516 | 3e3d5815 | balrog | } |
517 | 3e3d5815 | balrog | |
518 | 3e3d5815 | balrog | if (!s->bdrv || s->mem_oob)
|
519 | 5f6eab3f | Jean-Christophe DUBOIS | qemu_free(s->storage); |
520 | 3e3d5815 | balrog | |
521 | 5f6eab3f | Jean-Christophe DUBOIS | qemu_free(s); |
522 | 3e3d5815 | balrog | } |
523 | 3e3d5815 | balrog | |
524 | 3e3d5815 | balrog | #else
|
525 | 3e3d5815 | balrog | |
526 | 3e3d5815 | balrog | /* Program a single page */
|
527 | bc24a225 | Paul Brook | static void glue(nand_blk_write_, PAGE_SIZE)(NANDFlashState *s) |
528 | 3e3d5815 | balrog | { |
529 | 3e3d5815 | balrog | uint32_t off, page, sector, soff; |
530 | 3e3d5815 | balrog | uint8_t iobuf[(PAGE_SECTORS + 2) * 0x200]; |
531 | 3e3d5815 | balrog | if (PAGE(s->addr) >= s->pages)
|
532 | 3e3d5815 | balrog | return;
|
533 | 3e3d5815 | balrog | |
534 | 3e3d5815 | balrog | if (!s->bdrv) {
|
535 | 3e3d5815 | balrog | memcpy(s->storage + PAGE_START(s->addr) + (s->addr & PAGE_MASK) + |
536 | 3e3d5815 | balrog | s->offset, s->io, s->iolen); |
537 | 3e3d5815 | balrog | } else if (s->mem_oob) { |
538 | 3e3d5815 | balrog | sector = SECTOR(s->addr); |
539 | 3e3d5815 | balrog | off = (s->addr & PAGE_MASK) + s->offset; |
540 | 3e3d5815 | balrog | soff = SECTOR_OFFSET(s->addr); |
541 | 3e3d5815 | balrog | if (bdrv_read(s->bdrv, sector, iobuf, PAGE_SECTORS) == -1) { |
542 | 3e3d5815 | balrog | printf("%s: read error in sector %i\n", __FUNCTION__, sector);
|
543 | 3e3d5815 | balrog | return;
|
544 | 3e3d5815 | balrog | } |
545 | 3e3d5815 | balrog | |
546 | 3e3d5815 | balrog | memcpy(iobuf + (soff | off), s->io, MIN(s->iolen, PAGE_SIZE - off)); |
547 | 3e3d5815 | balrog | if (off + s->iolen > PAGE_SIZE) {
|
548 | 3e3d5815 | balrog | page = PAGE(s->addr); |
549 | 3e3d5815 | balrog | memcpy(s->storage + (page << OOB_SHIFT), s->io + PAGE_SIZE - off, |
550 | 3e3d5815 | balrog | MIN(OOB_SIZE, off + s->iolen - PAGE_SIZE)); |
551 | 3e3d5815 | balrog | } |
552 | 3e3d5815 | balrog | |
553 | 3e3d5815 | balrog | if (bdrv_write(s->bdrv, sector, iobuf, PAGE_SECTORS) == -1) |
554 | 3e3d5815 | balrog | printf("%s: write error in sector %i\n", __FUNCTION__, sector);
|
555 | 3e3d5815 | balrog | } else {
|
556 | 3e3d5815 | balrog | off = PAGE_START(s->addr) + (s->addr & PAGE_MASK) + s->offset; |
557 | 3e3d5815 | balrog | sector = off >> 9;
|
558 | 3e3d5815 | balrog | soff = off & 0x1ff;
|
559 | 3e3d5815 | balrog | if (bdrv_read(s->bdrv, sector, iobuf, PAGE_SECTORS + 2) == -1) { |
560 | 3e3d5815 | balrog | printf("%s: read error in sector %i\n", __FUNCTION__, sector);
|
561 | 3e3d5815 | balrog | return;
|
562 | 3e3d5815 | balrog | } |
563 | 3e3d5815 | balrog | |
564 | 3e3d5815 | balrog | memcpy(iobuf + soff, s->io, s->iolen); |
565 | 3e3d5815 | balrog | |
566 | 3e3d5815 | balrog | if (bdrv_write(s->bdrv, sector, iobuf, PAGE_SECTORS + 2) == -1) |
567 | 3e3d5815 | balrog | printf("%s: write error in sector %i\n", __FUNCTION__, sector);
|
568 | 3e3d5815 | balrog | } |
569 | 3e3d5815 | balrog | s->offset = 0;
|
570 | 3e3d5815 | balrog | } |
571 | 3e3d5815 | balrog | |
572 | 3e3d5815 | balrog | /* Erase a single block */
|
573 | bc24a225 | Paul Brook | static void glue(nand_blk_erase_, PAGE_SIZE)(NANDFlashState *s) |
574 | 3e3d5815 | balrog | { |
575 | 3e3d5815 | balrog | uint32_t i, page, addr; |
576 | 3e3d5815 | balrog | uint8_t iobuf[0x200] = { [0 ... 0x1ff] = 0xff, }; |
577 | 3e3d5815 | balrog | addr = s->addr & ~((1 << (ADDR_SHIFT + s->erase_shift)) - 1); |
578 | 3e3d5815 | balrog | |
579 | 3e3d5815 | balrog | if (PAGE(addr) >= s->pages)
|
580 | 3e3d5815 | balrog | return;
|
581 | 3e3d5815 | balrog | |
582 | 3e3d5815 | balrog | if (!s->bdrv) {
|
583 | 3e3d5815 | balrog | memset(s->storage + PAGE_START(addr), |
584 | 3e3d5815 | balrog | 0xff, (PAGE_SIZE + OOB_SIZE) << s->erase_shift);
|
585 | 3e3d5815 | balrog | } else if (s->mem_oob) { |
586 | 3e3d5815 | balrog | memset(s->storage + (PAGE(addr) << OOB_SHIFT), |
587 | 3e3d5815 | balrog | 0xff, OOB_SIZE << s->erase_shift);
|
588 | 3e3d5815 | balrog | i = SECTOR(addr); |
589 | 3e3d5815 | balrog | page = SECTOR(addr + (ADDR_SHIFT + s->erase_shift)); |
590 | 3e3d5815 | balrog | for (; i < page; i ++)
|
591 | 3e3d5815 | balrog | if (bdrv_write(s->bdrv, i, iobuf, 1) == -1) |
592 | 3e3d5815 | balrog | printf("%s: write error in sector %i\n", __FUNCTION__, i);
|
593 | 3e3d5815 | balrog | } else {
|
594 | 3e3d5815 | balrog | addr = PAGE_START(addr); |
595 | 3e3d5815 | balrog | page = addr >> 9;
|
596 | 3e3d5815 | balrog | if (bdrv_read(s->bdrv, page, iobuf, 1) == -1) |
597 | 3e3d5815 | balrog | printf("%s: read error in sector %i\n", __FUNCTION__, page);
|
598 | 3e3d5815 | balrog | memset(iobuf + (addr & 0x1ff), 0xff, (~addr & 0x1ff) + 1); |
599 | 3e3d5815 | balrog | if (bdrv_write(s->bdrv, page, iobuf, 1) == -1) |
600 | 3e3d5815 | balrog | printf("%s: write error in sector %i\n", __FUNCTION__, page);
|
601 | 3e3d5815 | balrog | |
602 | 3e3d5815 | balrog | memset(iobuf, 0xff, 0x200); |
603 | 3e3d5815 | balrog | i = (addr & ~0x1ff) + 0x200; |
604 | 3e3d5815 | balrog | for (addr += ((PAGE_SIZE + OOB_SIZE) << s->erase_shift) - 0x200; |
605 | 3e3d5815 | balrog | i < addr; i += 0x200)
|
606 | 3e3d5815 | balrog | if (bdrv_write(s->bdrv, i >> 9, iobuf, 1) == -1) |
607 | 3e3d5815 | balrog | printf("%s: write error in sector %i\n", __FUNCTION__, i >> 9); |
608 | 3e3d5815 | balrog | |
609 | 3e3d5815 | balrog | page = i >> 9;
|
610 | 3e3d5815 | balrog | if (bdrv_read(s->bdrv, page, iobuf, 1) == -1) |
611 | 3e3d5815 | balrog | printf("%s: read error in sector %i\n", __FUNCTION__, page);
|
612 | a07dec22 | balrog | memset(iobuf, 0xff, ((addr - 1) & 0x1ff) + 1); |
613 | 3e3d5815 | balrog | if (bdrv_write(s->bdrv, page, iobuf, 1) == -1) |
614 | 3e3d5815 | balrog | printf("%s: write error in sector %i\n", __FUNCTION__, page);
|
615 | 3e3d5815 | balrog | } |
616 | 3e3d5815 | balrog | } |
617 | 3e3d5815 | balrog | |
618 | bc24a225 | Paul Brook | static void glue(nand_blk_load_, PAGE_SIZE)(NANDFlashState *s, |
619 | 3e3d5815 | balrog | uint32_t addr, int offset)
|
620 | 3e3d5815 | balrog | { |
621 | 3e3d5815 | balrog | if (PAGE(addr) >= s->pages)
|
622 | 3e3d5815 | balrog | return;
|
623 | 3e3d5815 | balrog | |
624 | 3e3d5815 | balrog | if (s->bdrv) {
|
625 | 3e3d5815 | balrog | if (s->mem_oob) {
|
626 | 3e3d5815 | balrog | if (bdrv_read(s->bdrv, SECTOR(addr), s->io, PAGE_SECTORS) == -1) |
627 | 3e3d5815 | balrog | printf("%s: read error in sector %i\n",
|
628 | 3e3d5815 | balrog | __FUNCTION__, SECTOR(addr)); |
629 | 3e3d5815 | balrog | memcpy(s->io + SECTOR_OFFSET(s->addr) + PAGE_SIZE, |
630 | 3e3d5815 | balrog | s->storage + (PAGE(s->addr) << OOB_SHIFT), |
631 | 3e3d5815 | balrog | OOB_SIZE); |
632 | 3e3d5815 | balrog | s->ioaddr = s->io + SECTOR_OFFSET(s->addr) + offset; |
633 | 3e3d5815 | balrog | } else {
|
634 | 3e3d5815 | balrog | if (bdrv_read(s->bdrv, PAGE_START(addr) >> 9, |
635 | 3e3d5815 | balrog | s->io, (PAGE_SECTORS + 2)) == -1) |
636 | 3e3d5815 | balrog | printf("%s: read error in sector %i\n",
|
637 | 3e3d5815 | balrog | __FUNCTION__, PAGE_START(addr) >> 9);
|
638 | 3e3d5815 | balrog | s->ioaddr = s->io + (PAGE_START(addr) & 0x1ff) + offset;
|
639 | 3e3d5815 | balrog | } |
640 | 3e3d5815 | balrog | } else {
|
641 | 3e3d5815 | balrog | memcpy(s->io, s->storage + PAGE_START(s->addr) + |
642 | 3e3d5815 | balrog | offset, PAGE_SIZE + OOB_SIZE - offset); |
643 | 3e3d5815 | balrog | s->ioaddr = s->io; |
644 | 3e3d5815 | balrog | } |
645 | 3e3d5815 | balrog | } |
646 | 3e3d5815 | balrog | |
647 | bc24a225 | Paul Brook | static void glue(nand_init_, PAGE_SIZE)(NANDFlashState *s) |
648 | 3e3d5815 | balrog | { |
649 | 3e3d5815 | balrog | s->oob_shift = PAGE_SHIFT - 5;
|
650 | 3e3d5815 | balrog | s->pages = s->size >> PAGE_SHIFT; |
651 | 3e3d5815 | balrog | s->addr_shift = ADDR_SHIFT; |
652 | 3e3d5815 | balrog | |
653 | 3e3d5815 | balrog | s->blk_erase = glue(nand_blk_erase_, PAGE_SIZE); |
654 | 3e3d5815 | balrog | s->blk_write = glue(nand_blk_write_, PAGE_SIZE); |
655 | 3e3d5815 | balrog | s->blk_load = glue(nand_blk_load_, PAGE_SIZE); |
656 | 3e3d5815 | balrog | } |
657 | 3e3d5815 | balrog | |
658 | 3e3d5815 | balrog | # undef PAGE_SIZE
|
659 | 3e3d5815 | balrog | # undef PAGE_SHIFT
|
660 | 3e3d5815 | balrog | # undef PAGE_SECTORS
|
661 | 3e3d5815 | balrog | # undef ADDR_SHIFT
|
662 | 3e3d5815 | balrog | #endif /* NAND_IO */ |