root / hw / nand.c @ 298e01b6
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 | 3e3d5815 | balrog | struct nand_flash_s {
|
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 | 3e3d5815 | balrog | void (*blk_write)(struct nand_flash_s *s); |
68 | 3e3d5815 | balrog | void (*blk_erase)(struct nand_flash_s *s); |
69 | 3e3d5815 | balrog | void (*blk_load)(struct nand_flash_s *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 | 3e3d5815 | balrog | struct nand_info_s {
|
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 | 3e3d5815 | balrog | static void nand_reset(struct nand_flash_s *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 | 3e3d5815 | balrog | static void nand_command(struct nand_flash_s *s) |
214 | 3e3d5815 | balrog | { |
215 | 3e3d5815 | balrog | switch (s->cmd) {
|
216 | 3e3d5815 | balrog | case NAND_CMD_READ0:
|
217 | 3e3d5815 | balrog | s->iolen = 0;
|
218 | 3e3d5815 | balrog | break;
|
219 | 3e3d5815 | balrog | |
220 | 3e3d5815 | balrog | case NAND_CMD_READID:
|
221 | 3e3d5815 | balrog | s->io[0] = s->manf_id;
|
222 | 3e3d5815 | balrog | s->io[1] = s->chip_id;
|
223 | 3e3d5815 | balrog | s->io[2] = 'Q'; /* Don't-care byte (often 0xa5) */ |
224 | 3e3d5815 | balrog | if (nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP)
|
225 | 3e3d5815 | balrog | s->io[3] = 0x15; /* Page Size, Block Size, Spare Size.. */ |
226 | 3e3d5815 | balrog | else
|
227 | 3e3d5815 | balrog | s->io[3] = 0xc0; /* Multi-plane */ |
228 | 3e3d5815 | balrog | s->ioaddr = s->io; |
229 | 3e3d5815 | balrog | s->iolen = 4;
|
230 | 3e3d5815 | balrog | break;
|
231 | 3e3d5815 | balrog | |
232 | 3e3d5815 | balrog | case NAND_CMD_RANDOMREAD2:
|
233 | 3e3d5815 | balrog | case NAND_CMD_NOSERIALREAD2:
|
234 | 3e3d5815 | balrog | if (!(nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP))
|
235 | 3e3d5815 | balrog | break;
|
236 | 3e3d5815 | balrog | |
237 | 3e3d5815 | balrog | s->blk_load(s, s->addr, s->addr & ((1 << s->addr_shift) - 1)); |
238 | 3e3d5815 | balrog | break;
|
239 | 3e3d5815 | balrog | |
240 | 3e3d5815 | balrog | case NAND_CMD_RESET:
|
241 | 3e3d5815 | balrog | nand_reset(s); |
242 | 3e3d5815 | balrog | break;
|
243 | 3e3d5815 | balrog | |
244 | 3e3d5815 | balrog | case NAND_CMD_PAGEPROGRAM1:
|
245 | 3e3d5815 | balrog | s->ioaddr = s->io; |
246 | 3e3d5815 | balrog | s->iolen = 0;
|
247 | 3e3d5815 | balrog | break;
|
248 | 3e3d5815 | balrog | |
249 | 3e3d5815 | balrog | case NAND_CMD_PAGEPROGRAM2:
|
250 | 3e3d5815 | balrog | if (s->wp) {
|
251 | 3e3d5815 | balrog | s->blk_write(s); |
252 | 3e3d5815 | balrog | } |
253 | 3e3d5815 | balrog | break;
|
254 | 3e3d5815 | balrog | |
255 | 3e3d5815 | balrog | case NAND_CMD_BLOCKERASE1:
|
256 | 3e3d5815 | balrog | break;
|
257 | 3e3d5815 | balrog | |
258 | 3e3d5815 | balrog | case NAND_CMD_BLOCKERASE2:
|
259 | 3e3d5815 | balrog | if (nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP)
|
260 | 3e3d5815 | balrog | s->addr <<= 16;
|
261 | 3e3d5815 | balrog | else
|
262 | 3e3d5815 | balrog | s->addr <<= 8;
|
263 | 3e3d5815 | balrog | |
264 | 3e3d5815 | balrog | if (s->wp) {
|
265 | 3e3d5815 | balrog | s->blk_erase(s); |
266 | 3e3d5815 | balrog | } |
267 | 3e3d5815 | balrog | break;
|
268 | 3e3d5815 | balrog | |
269 | 3e3d5815 | balrog | case NAND_CMD_READSTATUS:
|
270 | 3e3d5815 | balrog | s->io[0] = s->status;
|
271 | 3e3d5815 | balrog | s->ioaddr = s->io; |
272 | 3e3d5815 | balrog | s->iolen = 1;
|
273 | 3e3d5815 | balrog | break;
|
274 | 3e3d5815 | balrog | |
275 | 3e3d5815 | balrog | default:
|
276 | 3e3d5815 | balrog | printf("%s: Unknown NAND command 0x%02x\n", __FUNCTION__, s->cmd);
|
277 | 3e3d5815 | balrog | } |
278 | 3e3d5815 | balrog | } |
279 | 3e3d5815 | balrog | |
280 | aa941b94 | balrog | static void nand_save(QEMUFile *f, void *opaque) |
281 | aa941b94 | balrog | { |
282 | aa941b94 | balrog | struct nand_flash_s *s = (struct nand_flash_s *) opaque; |
283 | aa941b94 | balrog | qemu_put_byte(f, s->cle); |
284 | aa941b94 | balrog | qemu_put_byte(f, s->ale); |
285 | aa941b94 | balrog | qemu_put_byte(f, s->ce); |
286 | aa941b94 | balrog | qemu_put_byte(f, s->wp); |
287 | aa941b94 | balrog | qemu_put_byte(f, s->gnd); |
288 | aa941b94 | balrog | qemu_put_buffer(f, s->io, sizeof(s->io));
|
289 | aa941b94 | balrog | qemu_put_be32(f, s->ioaddr - s->io); |
290 | aa941b94 | balrog | qemu_put_be32(f, s->iolen); |
291 | aa941b94 | balrog | |
292 | aa941b94 | balrog | qemu_put_be32s(f, &s->cmd); |
293 | aa941b94 | balrog | qemu_put_be32s(f, &s->addr); |
294 | aa941b94 | balrog | qemu_put_be32(f, s->addrlen); |
295 | aa941b94 | balrog | qemu_put_be32(f, s->status); |
296 | aa941b94 | balrog | qemu_put_be32(f, s->offset); |
297 | aa941b94 | balrog | /* XXX: do we want to save s->storage too? */
|
298 | aa941b94 | balrog | } |
299 | aa941b94 | balrog | |
300 | aa941b94 | balrog | static int nand_load(QEMUFile *f, void *opaque, int version_id) |
301 | aa941b94 | balrog | { |
302 | aa941b94 | balrog | struct nand_flash_s *s = (struct nand_flash_s *) opaque; |
303 | aa941b94 | balrog | s->cle = qemu_get_byte(f); |
304 | aa941b94 | balrog | s->ale = qemu_get_byte(f); |
305 | aa941b94 | balrog | s->ce = qemu_get_byte(f); |
306 | aa941b94 | balrog | s->wp = qemu_get_byte(f); |
307 | aa941b94 | balrog | s->gnd = qemu_get_byte(f); |
308 | aa941b94 | balrog | qemu_get_buffer(f, s->io, sizeof(s->io));
|
309 | aa941b94 | balrog | s->ioaddr = s->io + qemu_get_be32(f); |
310 | aa941b94 | balrog | s->iolen = qemu_get_be32(f); |
311 | aa941b94 | balrog | if (s->ioaddr >= s->io + sizeof(s->io) || s->ioaddr < s->io) |
312 | aa941b94 | balrog | return -EINVAL;
|
313 | aa941b94 | balrog | |
314 | aa941b94 | balrog | qemu_get_be32s(f, &s->cmd); |
315 | aa941b94 | balrog | qemu_get_be32s(f, &s->addr); |
316 | aa941b94 | balrog | s->addrlen = qemu_get_be32(f); |
317 | aa941b94 | balrog | s->status = qemu_get_be32(f); |
318 | aa941b94 | balrog | s->offset = qemu_get_be32(f); |
319 | aa941b94 | balrog | return 0; |
320 | aa941b94 | balrog | } |
321 | aa941b94 | balrog | |
322 | aa941b94 | balrog | static int nand_iid = 0; |
323 | aa941b94 | balrog | |
324 | 3e3d5815 | balrog | /*
|
325 | 3e3d5815 | balrog | * Chip inputs are CLE, ALE, CE, WP, GND and eight I/O pins. Chip
|
326 | 3e3d5815 | balrog | * outputs are R/B and eight I/O pins.
|
327 | 3e3d5815 | balrog | *
|
328 | 3e3d5815 | balrog | * CE, WP and R/B are active low.
|
329 | 3e3d5815 | balrog | */
|
330 | 5fafdf24 | ths | void nand_setpins(struct nand_flash_s *s, |
331 | 3e3d5815 | balrog | int cle, int ale, int ce, int wp, int gnd) |
332 | 3e3d5815 | balrog | { |
333 | 3e3d5815 | balrog | s->cle = cle; |
334 | 3e3d5815 | balrog | s->ale = ale; |
335 | 3e3d5815 | balrog | s->ce = ce; |
336 | 3e3d5815 | balrog | s->wp = wp; |
337 | 3e3d5815 | balrog | s->gnd = gnd; |
338 | 3e3d5815 | balrog | if (wp)
|
339 | 3e3d5815 | balrog | s->status |= NAND_IOSTATUS_UNPROTCT; |
340 | 3e3d5815 | balrog | else
|
341 | 3e3d5815 | balrog | s->status &= ~NAND_IOSTATUS_UNPROTCT; |
342 | 3e3d5815 | balrog | } |
343 | 3e3d5815 | balrog | |
344 | 3e3d5815 | balrog | void nand_getpins(struct nand_flash_s *s, int *rb) |
345 | 3e3d5815 | balrog | { |
346 | 3e3d5815 | balrog | *rb = 1;
|
347 | 3e3d5815 | balrog | } |
348 | 3e3d5815 | balrog | |
349 | 3e3d5815 | balrog | void nand_setio(struct nand_flash_s *s, uint8_t value) |
350 | 3e3d5815 | balrog | { |
351 | 3e3d5815 | balrog | if (!s->ce && s->cle) {
|
352 | 3e3d5815 | balrog | if (nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) {
|
353 | 3e3d5815 | balrog | if (s->cmd == NAND_CMD_READ0 && value == NAND_CMD_LPREAD2)
|
354 | 3e3d5815 | balrog | return;
|
355 | 3e3d5815 | balrog | if (value == NAND_CMD_RANDOMREAD1) {
|
356 | 3e3d5815 | balrog | s->addr &= ~((1 << s->addr_shift) - 1); |
357 | 3e3d5815 | balrog | s->addrlen = 0;
|
358 | 3e3d5815 | balrog | return;
|
359 | 3e3d5815 | balrog | } |
360 | 3e3d5815 | balrog | } |
361 | 3e3d5815 | balrog | if (value == NAND_CMD_READ0)
|
362 | 3e3d5815 | balrog | s->offset = 0;
|
363 | 3e3d5815 | balrog | else if (value == NAND_CMD_READ1) { |
364 | 3e3d5815 | balrog | s->offset = 0x100;
|
365 | 3e3d5815 | balrog | value = NAND_CMD_READ0; |
366 | 3e3d5815 | balrog | } |
367 | 3e3d5815 | balrog | else if (value == NAND_CMD_READ2) { |
368 | 3e3d5815 | balrog | s->offset = 1 << s->page_shift;
|
369 | 3e3d5815 | balrog | value = NAND_CMD_READ0; |
370 | 3e3d5815 | balrog | } |
371 | 3e3d5815 | balrog | |
372 | 3e3d5815 | balrog | s->cmd = value; |
373 | 3e3d5815 | balrog | |
374 | 3e3d5815 | balrog | if (s->cmd == NAND_CMD_READSTATUS ||
|
375 | 3e3d5815 | balrog | s->cmd == NAND_CMD_PAGEPROGRAM2 || |
376 | 3e3d5815 | balrog | s->cmd == NAND_CMD_BLOCKERASE1 || |
377 | 3e3d5815 | balrog | s->cmd == NAND_CMD_BLOCKERASE2 || |
378 | 3e3d5815 | balrog | s->cmd == NAND_CMD_NOSERIALREAD2 || |
379 | 3e3d5815 | balrog | s->cmd == NAND_CMD_RANDOMREAD2 || |
380 | 3e3d5815 | balrog | s->cmd == NAND_CMD_RESET) |
381 | 3e3d5815 | balrog | nand_command(s); |
382 | 3e3d5815 | balrog | |
383 | 3e3d5815 | balrog | if (s->cmd != NAND_CMD_RANDOMREAD2) {
|
384 | 3e3d5815 | balrog | s->addrlen = 0;
|
385 | 3e3d5815 | balrog | s->addr = 0;
|
386 | 3e3d5815 | balrog | } |
387 | 3e3d5815 | balrog | } |
388 | 3e3d5815 | balrog | |
389 | 3e3d5815 | balrog | if (s->ale) {
|
390 | 3e3d5815 | balrog | s->addr |= value << (s->addrlen * 8);
|
391 | 3e3d5815 | balrog | s->addrlen ++; |
392 | 3e3d5815 | balrog | |
393 | 3e3d5815 | balrog | if (s->addrlen == 1 && s->cmd == NAND_CMD_READID) |
394 | 3e3d5815 | balrog | nand_command(s); |
395 | 3e3d5815 | balrog | |
396 | 3e3d5815 | balrog | if (!(nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) &&
|
397 | 3e3d5815 | balrog | s->addrlen == 3 && (
|
398 | 3e3d5815 | balrog | s->cmd == NAND_CMD_READ0 || |
399 | 3e3d5815 | balrog | s->cmd == NAND_CMD_PAGEPROGRAM1)) |
400 | 3e3d5815 | balrog | nand_command(s); |
401 | 3e3d5815 | balrog | if ((nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) &&
|
402 | 3e3d5815 | balrog | s->addrlen == 4 && (
|
403 | 3e3d5815 | balrog | s->cmd == NAND_CMD_READ0 || |
404 | 3e3d5815 | balrog | s->cmd == NAND_CMD_PAGEPROGRAM1)) |
405 | 3e3d5815 | balrog | nand_command(s); |
406 | 3e3d5815 | balrog | } |
407 | 3e3d5815 | balrog | |
408 | 3e3d5815 | balrog | if (!s->cle && !s->ale && s->cmd == NAND_CMD_PAGEPROGRAM1) {
|
409 | 3e3d5815 | balrog | if (s->iolen < (1 << s->page_shift) + (1 << s->oob_shift)) |
410 | 3e3d5815 | balrog | s->io[s->iolen ++] = value; |
411 | 3e3d5815 | balrog | } else if (!s->cle && !s->ale && s->cmd == NAND_CMD_COPYBACKPRG1) { |
412 | 3e3d5815 | balrog | if ((s->addr & ((1 << s->addr_shift) - 1)) < |
413 | 3e3d5815 | balrog | (1 << s->page_shift) + (1 << s->oob_shift)) { |
414 | 3e3d5815 | balrog | s->io[s->iolen + (s->addr & ((1 << s->addr_shift) - 1))] = value; |
415 | 3e3d5815 | balrog | s->addr ++; |
416 | 3e3d5815 | balrog | } |
417 | 3e3d5815 | balrog | } |
418 | 3e3d5815 | balrog | } |
419 | 3e3d5815 | balrog | |
420 | 3e3d5815 | balrog | uint8_t nand_getio(struct nand_flash_s *s)
|
421 | 3e3d5815 | balrog | { |
422 | 3e3d5815 | balrog | int offset;
|
423 | 5fafdf24 | ths | |
424 | 3e3d5815 | balrog | /* Allow sequential reading */
|
425 | 3e3d5815 | balrog | if (!s->iolen && s->cmd == NAND_CMD_READ0) {
|
426 | 3e3d5815 | balrog | offset = (s->addr & ((1 << s->addr_shift) - 1)) + s->offset; |
427 | 3e3d5815 | balrog | s->offset = 0;
|
428 | 3e3d5815 | balrog | |
429 | 3e3d5815 | balrog | s->blk_load(s, s->addr, offset); |
430 | 3e3d5815 | balrog | if (s->gnd)
|
431 | 3e3d5815 | balrog | s->iolen = (1 << s->page_shift) - offset;
|
432 | 3e3d5815 | balrog | else
|
433 | 3e3d5815 | balrog | s->iolen = (1 << s->page_shift) + (1 << s->oob_shift) - offset; |
434 | 3e3d5815 | balrog | } |
435 | 3e3d5815 | balrog | |
436 | 3e3d5815 | balrog | if (s->ce || s->iolen <= 0) |
437 | 3e3d5815 | balrog | return 0; |
438 | 3e3d5815 | balrog | |
439 | 3e3d5815 | balrog | s->iolen --; |
440 | 3e3d5815 | balrog | return *(s->ioaddr ++);
|
441 | 3e3d5815 | balrog | } |
442 | 3e3d5815 | balrog | |
443 | 3e3d5815 | balrog | struct nand_flash_s *nand_init(int manf_id, int chip_id) |
444 | 3e3d5815 | balrog | { |
445 | 3e3d5815 | balrog | int pagesize;
|
446 | 3e3d5815 | balrog | struct nand_flash_s *s;
|
447 | e4bcb14c | ths | int index;
|
448 | 3e3d5815 | balrog | |
449 | 3e3d5815 | balrog | if (nand_flash_ids[chip_id].size == 0) { |
450 | 3e3d5815 | balrog | cpu_abort(cpu_single_env, "%s: Unsupported NAND chip ID.\n",
|
451 | 3e3d5815 | balrog | __FUNCTION__); |
452 | 3e3d5815 | balrog | } |
453 | e4bcb14c | ths | index = drive_get_index(IF_MTD, 0, 0); |
454 | e4bcb14c | ths | if (index == -1) { |
455 | e4bcb14c | ths | cpu_abort(cpu_single_env, "%s: missing MTD device\n",
|
456 | e4bcb14c | ths | __FUNCTION__); |
457 | e4bcb14c | ths | } |
458 | 3e3d5815 | balrog | |
459 | 3e3d5815 | balrog | s = (struct nand_flash_s *) qemu_mallocz(sizeof(struct nand_flash_s)); |
460 | e4bcb14c | ths | s->bdrv = drives_table[index].bdrv; |
461 | 3e3d5815 | balrog | s->manf_id = manf_id; |
462 | 3e3d5815 | balrog | s->chip_id = chip_id; |
463 | 3e3d5815 | balrog | s->size = nand_flash_ids[s->chip_id].size << 20;
|
464 | 3e3d5815 | balrog | if (nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) {
|
465 | 3e3d5815 | balrog | s->page_shift = 11;
|
466 | 3e3d5815 | balrog | s->erase_shift = 6;
|
467 | 3e3d5815 | balrog | } else {
|
468 | 3e3d5815 | balrog | s->page_shift = nand_flash_ids[s->chip_id].page_shift; |
469 | 3e3d5815 | balrog | s->erase_shift = nand_flash_ids[s->chip_id].erase_shift; |
470 | 3e3d5815 | balrog | } |
471 | 3e3d5815 | balrog | |
472 | 3e3d5815 | balrog | switch (1 << s->page_shift) { |
473 | 3e3d5815 | balrog | case 256: |
474 | 3e3d5815 | balrog | nand_init_256(s); |
475 | 3e3d5815 | balrog | break;
|
476 | 3e3d5815 | balrog | case 512: |
477 | 3e3d5815 | balrog | nand_init_512(s); |
478 | 3e3d5815 | balrog | break;
|
479 | 3e3d5815 | balrog | case 2048: |
480 | 3e3d5815 | balrog | nand_init_2048(s); |
481 | 3e3d5815 | balrog | break;
|
482 | 3e3d5815 | balrog | default:
|
483 | 3e3d5815 | balrog | cpu_abort(cpu_single_env, "%s: Unsupported NAND block size.\n",
|
484 | 3e3d5815 | balrog | __FUNCTION__); |
485 | 3e3d5815 | balrog | } |
486 | 3e3d5815 | balrog | |
487 | 3e3d5815 | balrog | pagesize = 1 << s->oob_shift;
|
488 | 3e3d5815 | balrog | s->mem_oob = 1;
|
489 | 3e3d5815 | balrog | if (s->bdrv && bdrv_getlength(s->bdrv) >=
|
490 | 3e3d5815 | balrog | (s->pages << s->page_shift) + (s->pages << s->oob_shift)) { |
491 | 3e3d5815 | balrog | pagesize = 0;
|
492 | 3e3d5815 | balrog | s->mem_oob = 0;
|
493 | 3e3d5815 | balrog | } |
494 | 3e3d5815 | balrog | |
495 | 3e3d5815 | balrog | if (!s->bdrv)
|
496 | 3e3d5815 | balrog | pagesize += 1 << s->page_shift;
|
497 | 3e3d5815 | balrog | if (pagesize)
|
498 | 3e3d5815 | balrog | s->storage = (uint8_t *) memset(qemu_malloc(s->pages * pagesize), |
499 | 3e3d5815 | balrog | 0xff, s->pages * pagesize);
|
500 | aa941b94 | balrog | |
501 | aa941b94 | balrog | register_savevm("nand", nand_iid ++, 0, nand_save, nand_load, s); |
502 | aa941b94 | balrog | |
503 | 3e3d5815 | balrog | return s;
|
504 | 3e3d5815 | balrog | } |
505 | 3e3d5815 | balrog | |
506 | 3e3d5815 | balrog | void nand_done(struct nand_flash_s *s) |
507 | 3e3d5815 | balrog | { |
508 | 3e3d5815 | balrog | if (s->bdrv) {
|
509 | 3e3d5815 | balrog | bdrv_close(s->bdrv); |
510 | 3e3d5815 | balrog | bdrv_delete(s->bdrv); |
511 | 3e3d5815 | balrog | } |
512 | 3e3d5815 | balrog | |
513 | 3e3d5815 | balrog | if (!s->bdrv || s->mem_oob)
|
514 | 3e3d5815 | balrog | free(s->storage); |
515 | 3e3d5815 | balrog | |
516 | 3e3d5815 | balrog | free(s); |
517 | 3e3d5815 | balrog | } |
518 | 3e3d5815 | balrog | |
519 | 3e3d5815 | balrog | #else
|
520 | 3e3d5815 | balrog | |
521 | 3e3d5815 | balrog | /* Program a single page */
|
522 | 3e3d5815 | balrog | static void glue(nand_blk_write_, PAGE_SIZE)(struct nand_flash_s *s) |
523 | 3e3d5815 | balrog | { |
524 | 3e3d5815 | balrog | uint32_t off, page, sector, soff; |
525 | 3e3d5815 | balrog | uint8_t iobuf[(PAGE_SECTORS + 2) * 0x200]; |
526 | 3e3d5815 | balrog | if (PAGE(s->addr) >= s->pages)
|
527 | 3e3d5815 | balrog | return;
|
528 | 3e3d5815 | balrog | |
529 | 3e3d5815 | balrog | if (!s->bdrv) {
|
530 | 3e3d5815 | balrog | memcpy(s->storage + PAGE_START(s->addr) + (s->addr & PAGE_MASK) + |
531 | 3e3d5815 | balrog | s->offset, s->io, s->iolen); |
532 | 3e3d5815 | balrog | } else if (s->mem_oob) { |
533 | 3e3d5815 | balrog | sector = SECTOR(s->addr); |
534 | 3e3d5815 | balrog | off = (s->addr & PAGE_MASK) + s->offset; |
535 | 3e3d5815 | balrog | soff = SECTOR_OFFSET(s->addr); |
536 | 3e3d5815 | balrog | if (bdrv_read(s->bdrv, sector, iobuf, PAGE_SECTORS) == -1) { |
537 | 3e3d5815 | balrog | printf("%s: read error in sector %i\n", __FUNCTION__, sector);
|
538 | 3e3d5815 | balrog | return;
|
539 | 3e3d5815 | balrog | } |
540 | 3e3d5815 | balrog | |
541 | 3e3d5815 | balrog | memcpy(iobuf + (soff | off), s->io, MIN(s->iolen, PAGE_SIZE - off)); |
542 | 3e3d5815 | balrog | if (off + s->iolen > PAGE_SIZE) {
|
543 | 3e3d5815 | balrog | page = PAGE(s->addr); |
544 | 3e3d5815 | balrog | memcpy(s->storage + (page << OOB_SHIFT), s->io + PAGE_SIZE - off, |
545 | 3e3d5815 | balrog | MIN(OOB_SIZE, off + s->iolen - PAGE_SIZE)); |
546 | 3e3d5815 | balrog | } |
547 | 3e3d5815 | balrog | |
548 | 3e3d5815 | balrog | if (bdrv_write(s->bdrv, sector, iobuf, PAGE_SECTORS) == -1) |
549 | 3e3d5815 | balrog | printf("%s: write error in sector %i\n", __FUNCTION__, sector);
|
550 | 3e3d5815 | balrog | } else {
|
551 | 3e3d5815 | balrog | off = PAGE_START(s->addr) + (s->addr & PAGE_MASK) + s->offset; |
552 | 3e3d5815 | balrog | sector = off >> 9;
|
553 | 3e3d5815 | balrog | soff = off & 0x1ff;
|
554 | 3e3d5815 | balrog | if (bdrv_read(s->bdrv, sector, iobuf, PAGE_SECTORS + 2) == -1) { |
555 | 3e3d5815 | balrog | printf("%s: read error in sector %i\n", __FUNCTION__, sector);
|
556 | 3e3d5815 | balrog | return;
|
557 | 3e3d5815 | balrog | } |
558 | 3e3d5815 | balrog | |
559 | 3e3d5815 | balrog | memcpy(iobuf + soff, s->io, s->iolen); |
560 | 3e3d5815 | balrog | |
561 | 3e3d5815 | balrog | if (bdrv_write(s->bdrv, sector, iobuf, PAGE_SECTORS + 2) == -1) |
562 | 3e3d5815 | balrog | printf("%s: write error in sector %i\n", __FUNCTION__, sector);
|
563 | 3e3d5815 | balrog | } |
564 | 3e3d5815 | balrog | s->offset = 0;
|
565 | 3e3d5815 | balrog | } |
566 | 3e3d5815 | balrog | |
567 | 3e3d5815 | balrog | /* Erase a single block */
|
568 | 3e3d5815 | balrog | static void glue(nand_blk_erase_, PAGE_SIZE)(struct nand_flash_s *s) |
569 | 3e3d5815 | balrog | { |
570 | 3e3d5815 | balrog | uint32_t i, page, addr; |
571 | 3e3d5815 | balrog | uint8_t iobuf[0x200] = { [0 ... 0x1ff] = 0xff, }; |
572 | 3e3d5815 | balrog | addr = s->addr & ~((1 << (ADDR_SHIFT + s->erase_shift)) - 1); |
573 | 3e3d5815 | balrog | |
574 | 3e3d5815 | balrog | if (PAGE(addr) >= s->pages)
|
575 | 3e3d5815 | balrog | return;
|
576 | 3e3d5815 | balrog | |
577 | 3e3d5815 | balrog | if (!s->bdrv) {
|
578 | 3e3d5815 | balrog | memset(s->storage + PAGE_START(addr), |
579 | 3e3d5815 | balrog | 0xff, (PAGE_SIZE + OOB_SIZE) << s->erase_shift);
|
580 | 3e3d5815 | balrog | } else if (s->mem_oob) { |
581 | 3e3d5815 | balrog | memset(s->storage + (PAGE(addr) << OOB_SHIFT), |
582 | 3e3d5815 | balrog | 0xff, OOB_SIZE << s->erase_shift);
|
583 | 3e3d5815 | balrog | i = SECTOR(addr); |
584 | 3e3d5815 | balrog | page = SECTOR(addr + (ADDR_SHIFT + s->erase_shift)); |
585 | 3e3d5815 | balrog | for (; i < page; i ++)
|
586 | 3e3d5815 | balrog | if (bdrv_write(s->bdrv, i, iobuf, 1) == -1) |
587 | 3e3d5815 | balrog | printf("%s: write error in sector %i\n", __FUNCTION__, i);
|
588 | 3e3d5815 | balrog | } else {
|
589 | 3e3d5815 | balrog | addr = PAGE_START(addr); |
590 | 3e3d5815 | balrog | page = addr >> 9;
|
591 | 3e3d5815 | balrog | if (bdrv_read(s->bdrv, page, iobuf, 1) == -1) |
592 | 3e3d5815 | balrog | printf("%s: read error in sector %i\n", __FUNCTION__, page);
|
593 | 3e3d5815 | balrog | memset(iobuf + (addr & 0x1ff), 0xff, (~addr & 0x1ff) + 1); |
594 | 3e3d5815 | balrog | if (bdrv_write(s->bdrv, page, iobuf, 1) == -1) |
595 | 3e3d5815 | balrog | printf("%s: write error in sector %i\n", __FUNCTION__, page);
|
596 | 3e3d5815 | balrog | |
597 | 3e3d5815 | balrog | memset(iobuf, 0xff, 0x200); |
598 | 3e3d5815 | balrog | i = (addr & ~0x1ff) + 0x200; |
599 | 3e3d5815 | balrog | for (addr += ((PAGE_SIZE + OOB_SIZE) << s->erase_shift) - 0x200; |
600 | 3e3d5815 | balrog | i < addr; i += 0x200)
|
601 | 3e3d5815 | balrog | if (bdrv_write(s->bdrv, i >> 9, iobuf, 1) == -1) |
602 | 3e3d5815 | balrog | printf("%s: write error in sector %i\n", __FUNCTION__, i >> 9); |
603 | 3e3d5815 | balrog | |
604 | 3e3d5815 | balrog | page = i >> 9;
|
605 | 3e3d5815 | balrog | if (bdrv_read(s->bdrv, page, iobuf, 1) == -1) |
606 | 3e3d5815 | balrog | printf("%s: read error in sector %i\n", __FUNCTION__, page);
|
607 | a07dec22 | balrog | memset(iobuf, 0xff, ((addr - 1) & 0x1ff) + 1); |
608 | 3e3d5815 | balrog | if (bdrv_write(s->bdrv, page, iobuf, 1) == -1) |
609 | 3e3d5815 | balrog | printf("%s: write error in sector %i\n", __FUNCTION__, page);
|
610 | 3e3d5815 | balrog | } |
611 | 3e3d5815 | balrog | } |
612 | 3e3d5815 | balrog | |
613 | 3e3d5815 | balrog | static void glue(nand_blk_load_, PAGE_SIZE)(struct nand_flash_s *s, |
614 | 3e3d5815 | balrog | uint32_t addr, int offset)
|
615 | 3e3d5815 | balrog | { |
616 | 3e3d5815 | balrog | if (PAGE(addr) >= s->pages)
|
617 | 3e3d5815 | balrog | return;
|
618 | 3e3d5815 | balrog | |
619 | 3e3d5815 | balrog | if (s->bdrv) {
|
620 | 3e3d5815 | balrog | if (s->mem_oob) {
|
621 | 3e3d5815 | balrog | if (bdrv_read(s->bdrv, SECTOR(addr), s->io, PAGE_SECTORS) == -1) |
622 | 3e3d5815 | balrog | printf("%s: read error in sector %i\n",
|
623 | 3e3d5815 | balrog | __FUNCTION__, SECTOR(addr)); |
624 | 3e3d5815 | balrog | memcpy(s->io + SECTOR_OFFSET(s->addr) + PAGE_SIZE, |
625 | 3e3d5815 | balrog | s->storage + (PAGE(s->addr) << OOB_SHIFT), |
626 | 3e3d5815 | balrog | OOB_SIZE); |
627 | 3e3d5815 | balrog | s->ioaddr = s->io + SECTOR_OFFSET(s->addr) + offset; |
628 | 3e3d5815 | balrog | } else {
|
629 | 3e3d5815 | balrog | if (bdrv_read(s->bdrv, PAGE_START(addr) >> 9, |
630 | 3e3d5815 | balrog | s->io, (PAGE_SECTORS + 2)) == -1) |
631 | 3e3d5815 | balrog | printf("%s: read error in sector %i\n",
|
632 | 3e3d5815 | balrog | __FUNCTION__, PAGE_START(addr) >> 9);
|
633 | 3e3d5815 | balrog | s->ioaddr = s->io + (PAGE_START(addr) & 0x1ff) + offset;
|
634 | 3e3d5815 | balrog | } |
635 | 3e3d5815 | balrog | } else {
|
636 | 3e3d5815 | balrog | memcpy(s->io, s->storage + PAGE_START(s->addr) + |
637 | 3e3d5815 | balrog | offset, PAGE_SIZE + OOB_SIZE - offset); |
638 | 3e3d5815 | balrog | s->ioaddr = s->io; |
639 | 3e3d5815 | balrog | } |
640 | 3e3d5815 | balrog | |
641 | 3e3d5815 | balrog | s->addr &= PAGE_SIZE - 1;
|
642 | 3e3d5815 | balrog | s->addr += PAGE_SIZE; |
643 | 3e3d5815 | balrog | } |
644 | 3e3d5815 | balrog | |
645 | 3e3d5815 | balrog | static void glue(nand_init_, PAGE_SIZE)(struct nand_flash_s *s) |
646 | 3e3d5815 | balrog | { |
647 | 3e3d5815 | balrog | s->oob_shift = PAGE_SHIFT - 5;
|
648 | 3e3d5815 | balrog | s->pages = s->size >> PAGE_SHIFT; |
649 | 3e3d5815 | balrog | s->addr_shift = ADDR_SHIFT; |
650 | 3e3d5815 | balrog | |
651 | 3e3d5815 | balrog | s->blk_erase = glue(nand_blk_erase_, PAGE_SIZE); |
652 | 3e3d5815 | balrog | s->blk_write = glue(nand_blk_write_, PAGE_SIZE); |
653 | 3e3d5815 | balrog | s->blk_load = glue(nand_blk_load_, PAGE_SIZE); |
654 | 3e3d5815 | balrog | } |
655 | 3e3d5815 | balrog | |
656 | 3e3d5815 | balrog | # undef PAGE_SIZE
|
657 | 3e3d5815 | balrog | # undef PAGE_SHIFT
|
658 | 3e3d5815 | balrog | # undef PAGE_SECTORS
|
659 | 3e3d5815 | balrog | # undef ADDR_SHIFT
|
660 | 3e3d5815 | balrog | #endif /* NAND_IO */ |