Statistics
| Branch: | Revision:

root / hw / nand.c @ 37873241

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 b1d8e52e blueswir1
static const 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 3e3d5815 balrog
/*
323 3e3d5815 balrog
 * Chip inputs are CLE, ALE, CE, WP, GND and eight I/O pins.  Chip
324 3e3d5815 balrog
 * outputs are R/B and eight I/O pins.
325 3e3d5815 balrog
 *
326 3e3d5815 balrog
 * CE, WP and R/B are active low.
327 3e3d5815 balrog
 */
328 5fafdf24 ths
void nand_setpins(struct nand_flash_s *s,
329 3e3d5815 balrog
                int cle, int ale, int ce, int wp, int gnd)
330 3e3d5815 balrog
{
331 3e3d5815 balrog
    s->cle = cle;
332 3e3d5815 balrog
    s->ale = ale;
333 3e3d5815 balrog
    s->ce = ce;
334 3e3d5815 balrog
    s->wp = wp;
335 3e3d5815 balrog
    s->gnd = gnd;
336 3e3d5815 balrog
    if (wp)
337 3e3d5815 balrog
        s->status |= NAND_IOSTATUS_UNPROTCT;
338 3e3d5815 balrog
    else
339 3e3d5815 balrog
        s->status &= ~NAND_IOSTATUS_UNPROTCT;
340 3e3d5815 balrog
}
341 3e3d5815 balrog
342 3e3d5815 balrog
void nand_getpins(struct nand_flash_s *s, int *rb)
343 3e3d5815 balrog
{
344 3e3d5815 balrog
    *rb = 1;
345 3e3d5815 balrog
}
346 3e3d5815 balrog
347 3e3d5815 balrog
void nand_setio(struct nand_flash_s *s, uint8_t value)
348 3e3d5815 balrog
{
349 3e3d5815 balrog
    if (!s->ce && s->cle) {
350 3e3d5815 balrog
        if (nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) {
351 3e3d5815 balrog
            if (s->cmd == NAND_CMD_READ0 && value == NAND_CMD_LPREAD2)
352 3e3d5815 balrog
                return;
353 3e3d5815 balrog
            if (value == NAND_CMD_RANDOMREAD1) {
354 3e3d5815 balrog
                s->addr &= ~((1 << s->addr_shift) - 1);
355 3e3d5815 balrog
                s->addrlen = 0;
356 3e3d5815 balrog
                return;
357 3e3d5815 balrog
            }
358 3e3d5815 balrog
        }
359 3e3d5815 balrog
        if (value == NAND_CMD_READ0)
360 3e3d5815 balrog
            s->offset = 0;
361 3e3d5815 balrog
        else if (value == NAND_CMD_READ1) {
362 3e3d5815 balrog
            s->offset = 0x100;
363 3e3d5815 balrog
            value = NAND_CMD_READ0;
364 3e3d5815 balrog
        }
365 3e3d5815 balrog
        else if (value == NAND_CMD_READ2) {
366 3e3d5815 balrog
            s->offset = 1 << s->page_shift;
367 3e3d5815 balrog
            value = NAND_CMD_READ0;
368 3e3d5815 balrog
        }
369 3e3d5815 balrog
370 3e3d5815 balrog
        s->cmd = value;
371 3e3d5815 balrog
372 3e3d5815 balrog
        if (s->cmd == NAND_CMD_READSTATUS ||
373 3e3d5815 balrog
                s->cmd == NAND_CMD_PAGEPROGRAM2 ||
374 3e3d5815 balrog
                s->cmd == NAND_CMD_BLOCKERASE1 ||
375 3e3d5815 balrog
                s->cmd == NAND_CMD_BLOCKERASE2 ||
376 3e3d5815 balrog
                s->cmd == NAND_CMD_NOSERIALREAD2 ||
377 3e3d5815 balrog
                s->cmd == NAND_CMD_RANDOMREAD2 ||
378 3e3d5815 balrog
                s->cmd == NAND_CMD_RESET)
379 3e3d5815 balrog
            nand_command(s);
380 3e3d5815 balrog
381 3e3d5815 balrog
        if (s->cmd != NAND_CMD_RANDOMREAD2) {
382 3e3d5815 balrog
            s->addrlen = 0;
383 3e3d5815 balrog
            s->addr = 0;
384 3e3d5815 balrog
        }
385 3e3d5815 balrog
    }
386 3e3d5815 balrog
387 3e3d5815 balrog
    if (s->ale) {
388 3e3d5815 balrog
        s->addr |= value << (s->addrlen * 8);
389 3e3d5815 balrog
        s->addrlen ++;
390 3e3d5815 balrog
391 3e3d5815 balrog
        if (s->addrlen == 1 && s->cmd == NAND_CMD_READID)
392 3e3d5815 balrog
            nand_command(s);
393 3e3d5815 balrog
394 3e3d5815 balrog
        if (!(nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) &&
395 3e3d5815 balrog
                s->addrlen == 3 && (
396 3e3d5815 balrog
                    s->cmd == NAND_CMD_READ0 ||
397 3e3d5815 balrog
                    s->cmd == NAND_CMD_PAGEPROGRAM1))
398 3e3d5815 balrog
            nand_command(s);
399 3e3d5815 balrog
        if ((nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) &&
400 3e3d5815 balrog
               s->addrlen == 4 && (
401 3e3d5815 balrog
                    s->cmd == NAND_CMD_READ0 ||
402 3e3d5815 balrog
                    s->cmd == NAND_CMD_PAGEPROGRAM1))
403 3e3d5815 balrog
            nand_command(s);
404 3e3d5815 balrog
    }
405 3e3d5815 balrog
406 3e3d5815 balrog
    if (!s->cle && !s->ale && s->cmd == NAND_CMD_PAGEPROGRAM1) {
407 3e3d5815 balrog
        if (s->iolen < (1 << s->page_shift) + (1 << s->oob_shift))
408 3e3d5815 balrog
            s->io[s->iolen ++] = value;
409 3e3d5815 balrog
    } else if (!s->cle && !s->ale && s->cmd == NAND_CMD_COPYBACKPRG1) {
410 3e3d5815 balrog
        if ((s->addr & ((1 << s->addr_shift) - 1)) <
411 3e3d5815 balrog
                (1 << s->page_shift) + (1 << s->oob_shift)) {
412 3e3d5815 balrog
            s->io[s->iolen + (s->addr & ((1 << s->addr_shift) - 1))] = value;
413 3e3d5815 balrog
            s->addr ++;
414 3e3d5815 balrog
        }
415 3e3d5815 balrog
    }
416 3e3d5815 balrog
}
417 3e3d5815 balrog
418 3e3d5815 balrog
uint8_t nand_getio(struct nand_flash_s *s)
419 3e3d5815 balrog
{
420 3e3d5815 balrog
    int offset;
421 5fafdf24 ths
422 3e3d5815 balrog
    /* Allow sequential reading */
423 3e3d5815 balrog
    if (!s->iolen && s->cmd == NAND_CMD_READ0) {
424 3e3d5815 balrog
        offset = (s->addr & ((1 << s->addr_shift) - 1)) + s->offset;
425 3e3d5815 balrog
        s->offset = 0;
426 3e3d5815 balrog
427 3e3d5815 balrog
        s->blk_load(s, s->addr, offset);
428 3e3d5815 balrog
        if (s->gnd)
429 3e3d5815 balrog
            s->iolen = (1 << s->page_shift) - offset;
430 3e3d5815 balrog
        else
431 3e3d5815 balrog
            s->iolen = (1 << s->page_shift) + (1 << s->oob_shift) - offset;
432 3e3d5815 balrog
    }
433 3e3d5815 balrog
434 3e3d5815 balrog
    if (s->ce || s->iolen <= 0)
435 3e3d5815 balrog
        return 0;
436 3e3d5815 balrog
437 3e3d5815 balrog
    s->iolen --;
438 3e3d5815 balrog
    return *(s->ioaddr ++);
439 3e3d5815 balrog
}
440 3e3d5815 balrog
441 3e3d5815 balrog
struct nand_flash_s *nand_init(int manf_id, int chip_id)
442 3e3d5815 balrog
{
443 3e3d5815 balrog
    int pagesize;
444 3e3d5815 balrog
    struct nand_flash_s *s;
445 e4bcb14c ths
    int index;
446 3e3d5815 balrog
447 3e3d5815 balrog
    if (nand_flash_ids[chip_id].size == 0) {
448 3e3d5815 balrog
        cpu_abort(cpu_single_env, "%s: Unsupported NAND chip ID.\n",
449 3e3d5815 balrog
                        __FUNCTION__);
450 3e3d5815 balrog
    }
451 3e3d5815 balrog
452 3e3d5815 balrog
    s = (struct nand_flash_s *) qemu_mallocz(sizeof(struct nand_flash_s));
453 130b0c98 balrog
    index = drive_get_index(IF_MTD, 0, 0);
454 130b0c98 balrog
    if (index != -1)
455 130b0c98 balrog
        s->bdrv = drives_table[index].bdrv;
456 3e3d5815 balrog
    s->manf_id = manf_id;
457 3e3d5815 balrog
    s->chip_id = chip_id;
458 3e3d5815 balrog
    s->size = nand_flash_ids[s->chip_id].size << 20;
459 3e3d5815 balrog
    if (nand_flash_ids[s->chip_id].options & NAND_SAMSUNG_LP) {
460 3e3d5815 balrog
        s->page_shift = 11;
461 3e3d5815 balrog
        s->erase_shift = 6;
462 3e3d5815 balrog
    } else {
463 3e3d5815 balrog
        s->page_shift = nand_flash_ids[s->chip_id].page_shift;
464 3e3d5815 balrog
        s->erase_shift = nand_flash_ids[s->chip_id].erase_shift;
465 3e3d5815 balrog
    }
466 3e3d5815 balrog
467 3e3d5815 balrog
    switch (1 << s->page_shift) {
468 3e3d5815 balrog
    case 256:
469 3e3d5815 balrog
        nand_init_256(s);
470 3e3d5815 balrog
        break;
471 3e3d5815 balrog
    case 512:
472 3e3d5815 balrog
        nand_init_512(s);
473 3e3d5815 balrog
        break;
474 3e3d5815 balrog
    case 2048:
475 3e3d5815 balrog
        nand_init_2048(s);
476 3e3d5815 balrog
        break;
477 3e3d5815 balrog
    default:
478 3e3d5815 balrog
        cpu_abort(cpu_single_env, "%s: Unsupported NAND block size.\n",
479 3e3d5815 balrog
                        __FUNCTION__);
480 3e3d5815 balrog
    }
481 3e3d5815 balrog
482 3e3d5815 balrog
    pagesize = 1 << s->oob_shift;
483 3e3d5815 balrog
    s->mem_oob = 1;
484 3e3d5815 balrog
    if (s->bdrv && bdrv_getlength(s->bdrv) >=
485 3e3d5815 balrog
                    (s->pages << s->page_shift) + (s->pages << s->oob_shift)) {
486 3e3d5815 balrog
        pagesize = 0;
487 3e3d5815 balrog
        s->mem_oob = 0;
488 3e3d5815 balrog
    }
489 3e3d5815 balrog
490 3e3d5815 balrog
    if (!s->bdrv)
491 3e3d5815 balrog
        pagesize += 1 << s->page_shift;
492 3e3d5815 balrog
    if (pagesize)
493 3e3d5815 balrog
        s->storage = (uint8_t *) memset(qemu_malloc(s->pages * pagesize),
494 3e3d5815 balrog
                        0xff, s->pages * pagesize);
495 48927926 pbrook
    /* Give s->ioaddr a sane value in case we save state before it
496 48927926 pbrook
       is used.  */
497 48927926 pbrook
    s->ioaddr = s->io;
498 aa941b94 balrog
499 18be5187 pbrook
    register_savevm("nand", -1, 0, nand_save, nand_load, s);
500 aa941b94 balrog
501 3e3d5815 balrog
    return s;
502 3e3d5815 balrog
}
503 3e3d5815 balrog
504 3e3d5815 balrog
void nand_done(struct nand_flash_s *s)
505 3e3d5815 balrog
{
506 3e3d5815 balrog
    if (s->bdrv) {
507 3e3d5815 balrog
        bdrv_close(s->bdrv);
508 3e3d5815 balrog
        bdrv_delete(s->bdrv);
509 3e3d5815 balrog
    }
510 3e3d5815 balrog
511 3e3d5815 balrog
    if (!s->bdrv || s->mem_oob)
512 3e3d5815 balrog
        free(s->storage);
513 3e3d5815 balrog
514 3e3d5815 balrog
    free(s);
515 3e3d5815 balrog
}
516 3e3d5815 balrog
517 3e3d5815 balrog
#else
518 3e3d5815 balrog
519 3e3d5815 balrog
/* Program a single page */
520 3e3d5815 balrog
static void glue(nand_blk_write_, PAGE_SIZE)(struct nand_flash_s *s)
521 3e3d5815 balrog
{
522 3e3d5815 balrog
    uint32_t off, page, sector, soff;
523 3e3d5815 balrog
    uint8_t iobuf[(PAGE_SECTORS + 2) * 0x200];
524 3e3d5815 balrog
    if (PAGE(s->addr) >= s->pages)
525 3e3d5815 balrog
        return;
526 3e3d5815 balrog
527 3e3d5815 balrog
    if (!s->bdrv) {
528 3e3d5815 balrog
        memcpy(s->storage + PAGE_START(s->addr) + (s->addr & PAGE_MASK) +
529 3e3d5815 balrog
                        s->offset, s->io, s->iolen);
530 3e3d5815 balrog
    } else if (s->mem_oob) {
531 3e3d5815 balrog
        sector = SECTOR(s->addr);
532 3e3d5815 balrog
        off = (s->addr & PAGE_MASK) + s->offset;
533 3e3d5815 balrog
        soff = SECTOR_OFFSET(s->addr);
534 3e3d5815 balrog
        if (bdrv_read(s->bdrv, sector, iobuf, PAGE_SECTORS) == -1) {
535 3e3d5815 balrog
            printf("%s: read error in sector %i\n", __FUNCTION__, sector);
536 3e3d5815 balrog
            return;
537 3e3d5815 balrog
        }
538 3e3d5815 balrog
539 3e3d5815 balrog
        memcpy(iobuf + (soff | off), s->io, MIN(s->iolen, PAGE_SIZE - off));
540 3e3d5815 balrog
        if (off + s->iolen > PAGE_SIZE) {
541 3e3d5815 balrog
            page = PAGE(s->addr);
542 3e3d5815 balrog
            memcpy(s->storage + (page << OOB_SHIFT), s->io + PAGE_SIZE - off,
543 3e3d5815 balrog
                            MIN(OOB_SIZE, off + s->iolen - PAGE_SIZE));
544 3e3d5815 balrog
        }
545 3e3d5815 balrog
546 3e3d5815 balrog
        if (bdrv_write(s->bdrv, sector, iobuf, PAGE_SECTORS) == -1)
547 3e3d5815 balrog
            printf("%s: write error in sector %i\n", __FUNCTION__, sector);
548 3e3d5815 balrog
    } else {
549 3e3d5815 balrog
        off = PAGE_START(s->addr) + (s->addr & PAGE_MASK) + s->offset;
550 3e3d5815 balrog
        sector = off >> 9;
551 3e3d5815 balrog
        soff = off & 0x1ff;
552 3e3d5815 balrog
        if (bdrv_read(s->bdrv, sector, iobuf, PAGE_SECTORS + 2) == -1) {
553 3e3d5815 balrog
            printf("%s: read error in sector %i\n", __FUNCTION__, sector);
554 3e3d5815 balrog
            return;
555 3e3d5815 balrog
        }
556 3e3d5815 balrog
557 3e3d5815 balrog
        memcpy(iobuf + soff, s->io, s->iolen);
558 3e3d5815 balrog
559 3e3d5815 balrog
        if (bdrv_write(s->bdrv, sector, iobuf, PAGE_SECTORS + 2) == -1)
560 3e3d5815 balrog
            printf("%s: write error in sector %i\n", __FUNCTION__, sector);
561 3e3d5815 balrog
    }
562 3e3d5815 balrog
    s->offset = 0;
563 3e3d5815 balrog
}
564 3e3d5815 balrog
565 3e3d5815 balrog
/* Erase a single block */
566 3e3d5815 balrog
static void glue(nand_blk_erase_, PAGE_SIZE)(struct nand_flash_s *s)
567 3e3d5815 balrog
{
568 3e3d5815 balrog
    uint32_t i, page, addr;
569 3e3d5815 balrog
    uint8_t iobuf[0x200] = { [0 ... 0x1ff] = 0xff, };
570 3e3d5815 balrog
    addr = s->addr & ~((1 << (ADDR_SHIFT + s->erase_shift)) - 1);
571 3e3d5815 balrog
572 3e3d5815 balrog
    if (PAGE(addr) >= s->pages)
573 3e3d5815 balrog
        return;
574 3e3d5815 balrog
575 3e3d5815 balrog
    if (!s->bdrv) {
576 3e3d5815 balrog
        memset(s->storage + PAGE_START(addr),
577 3e3d5815 balrog
                        0xff, (PAGE_SIZE + OOB_SIZE) << s->erase_shift);
578 3e3d5815 balrog
    } else if (s->mem_oob) {
579 3e3d5815 balrog
        memset(s->storage + (PAGE(addr) << OOB_SHIFT),
580 3e3d5815 balrog
                        0xff, OOB_SIZE << s->erase_shift);
581 3e3d5815 balrog
        i = SECTOR(addr);
582 3e3d5815 balrog
        page = SECTOR(addr + (ADDR_SHIFT + s->erase_shift));
583 3e3d5815 balrog
        for (; i < page; i ++)
584 3e3d5815 balrog
            if (bdrv_write(s->bdrv, i, iobuf, 1) == -1)
585 3e3d5815 balrog
                printf("%s: write error in sector %i\n", __FUNCTION__, i);
586 3e3d5815 balrog
    } else {
587 3e3d5815 balrog
        addr = PAGE_START(addr);
588 3e3d5815 balrog
        page = addr >> 9;
589 3e3d5815 balrog
        if (bdrv_read(s->bdrv, page, iobuf, 1) == -1)
590 3e3d5815 balrog
            printf("%s: read error in sector %i\n", __FUNCTION__, page);
591 3e3d5815 balrog
        memset(iobuf + (addr & 0x1ff), 0xff, (~addr & 0x1ff) + 1);
592 3e3d5815 balrog
        if (bdrv_write(s->bdrv, page, iobuf, 1) == -1)
593 3e3d5815 balrog
            printf("%s: write error in sector %i\n", __FUNCTION__, page);
594 3e3d5815 balrog
595 3e3d5815 balrog
        memset(iobuf, 0xff, 0x200);
596 3e3d5815 balrog
        i = (addr & ~0x1ff) + 0x200;
597 3e3d5815 balrog
        for (addr += ((PAGE_SIZE + OOB_SIZE) << s->erase_shift) - 0x200;
598 3e3d5815 balrog
                        i < addr; i += 0x200)
599 3e3d5815 balrog
            if (bdrv_write(s->bdrv, i >> 9, iobuf, 1) == -1)
600 3e3d5815 balrog
                printf("%s: write error in sector %i\n", __FUNCTION__, i >> 9);
601 3e3d5815 balrog
602 3e3d5815 balrog
        page = i >> 9;
603 3e3d5815 balrog
        if (bdrv_read(s->bdrv, page, iobuf, 1) == -1)
604 3e3d5815 balrog
            printf("%s: read error in sector %i\n", __FUNCTION__, page);
605 a07dec22 balrog
        memset(iobuf, 0xff, ((addr - 1) & 0x1ff) + 1);
606 3e3d5815 balrog
        if (bdrv_write(s->bdrv, page, iobuf, 1) == -1)
607 3e3d5815 balrog
            printf("%s: write error in sector %i\n", __FUNCTION__, page);
608 3e3d5815 balrog
    }
609 3e3d5815 balrog
}
610 3e3d5815 balrog
611 3e3d5815 balrog
static void glue(nand_blk_load_, PAGE_SIZE)(struct nand_flash_s *s,
612 3e3d5815 balrog
                uint32_t addr, int offset)
613 3e3d5815 balrog
{
614 3e3d5815 balrog
    if (PAGE(addr) >= s->pages)
615 3e3d5815 balrog
        return;
616 3e3d5815 balrog
617 3e3d5815 balrog
    if (s->bdrv) {
618 3e3d5815 balrog
        if (s->mem_oob) {
619 3e3d5815 balrog
            if (bdrv_read(s->bdrv, SECTOR(addr), s->io, PAGE_SECTORS) == -1)
620 3e3d5815 balrog
                printf("%s: read error in sector %i\n",
621 3e3d5815 balrog
                                __FUNCTION__, SECTOR(addr));
622 3e3d5815 balrog
            memcpy(s->io + SECTOR_OFFSET(s->addr) + PAGE_SIZE,
623 3e3d5815 balrog
                            s->storage + (PAGE(s->addr) << OOB_SHIFT),
624 3e3d5815 balrog
                            OOB_SIZE);
625 3e3d5815 balrog
            s->ioaddr = s->io + SECTOR_OFFSET(s->addr) + offset;
626 3e3d5815 balrog
        } else {
627 3e3d5815 balrog
            if (bdrv_read(s->bdrv, PAGE_START(addr) >> 9,
628 3e3d5815 balrog
                                    s->io, (PAGE_SECTORS + 2)) == -1)
629 3e3d5815 balrog
                printf("%s: read error in sector %i\n",
630 3e3d5815 balrog
                                __FUNCTION__, PAGE_START(addr) >> 9);
631 3e3d5815 balrog
            s->ioaddr = s->io + (PAGE_START(addr) & 0x1ff) + offset;
632 3e3d5815 balrog
        }
633 3e3d5815 balrog
    } else {
634 3e3d5815 balrog
        memcpy(s->io, s->storage + PAGE_START(s->addr) +
635 3e3d5815 balrog
                        offset, PAGE_SIZE + OOB_SIZE - offset);
636 3e3d5815 balrog
        s->ioaddr = s->io;
637 3e3d5815 balrog
    }
638 3e3d5815 balrog
639 3e3d5815 balrog
    s->addr &= PAGE_SIZE - 1;
640 3e3d5815 balrog
    s->addr += PAGE_SIZE;
641 3e3d5815 balrog
}
642 3e3d5815 balrog
643 3e3d5815 balrog
static void glue(nand_init_, PAGE_SIZE)(struct nand_flash_s *s)
644 3e3d5815 balrog
{
645 3e3d5815 balrog
    s->oob_shift = PAGE_SHIFT - 5;
646 3e3d5815 balrog
    s->pages = s->size >> PAGE_SHIFT;
647 3e3d5815 balrog
    s->addr_shift = ADDR_SHIFT;
648 3e3d5815 balrog
649 3e3d5815 balrog
    s->blk_erase = glue(nand_blk_erase_, PAGE_SIZE);
650 3e3d5815 balrog
    s->blk_write = glue(nand_blk_write_, PAGE_SIZE);
651 3e3d5815 balrog
    s->blk_load = glue(nand_blk_load_, PAGE_SIZE);
652 3e3d5815 balrog
}
653 3e3d5815 balrog
654 3e3d5815 balrog
# undef PAGE_SIZE
655 3e3d5815 balrog
# undef PAGE_SHIFT
656 3e3d5815 balrog
# undef PAGE_SECTORS
657 3e3d5815 balrog
# undef ADDR_SHIFT
658 3e3d5815 balrog
#endif        /* NAND_IO */