Statistics
| Branch: | Revision:

root / hw / nand.c @ aa941b94

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