root / hw / onenand.c @ c65fc1df
History | View | Annotate | Download (24.4 kB)
1 | 7e7c5e4c | balrog | /*
|
---|---|---|---|
2 | 7e7c5e4c | balrog | * OneNAND flash memories emulation.
|
3 | 7e7c5e4c | balrog | *
|
4 | 7e7c5e4c | balrog | * Copyright (C) 2008 Nokia Corporation
|
5 | 7e7c5e4c | balrog | * Written by Andrzej Zaborowski <andrew@openedhand.com>
|
6 | 7e7c5e4c | balrog | *
|
7 | 7e7c5e4c | balrog | * This program is free software; you can redistribute it and/or
|
8 | 7e7c5e4c | balrog | * modify it under the terms of the GNU General Public License as
|
9 | 7e7c5e4c | balrog | * published by the Free Software Foundation; either version 2 or
|
10 | 7e7c5e4c | balrog | * (at your option) version 3 of the License.
|
11 | 7e7c5e4c | balrog | *
|
12 | 7e7c5e4c | balrog | * This program is distributed in the hope that it will be useful,
|
13 | 7e7c5e4c | balrog | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 | 7e7c5e4c | balrog | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 | 7e7c5e4c | balrog | * GNU General Public License for more details.
|
16 | 7e7c5e4c | balrog | *
|
17 | fad6cb1a | aurel32 | * You should have received a copy of the GNU General Public License along
|
18 | 8167ee88 | Blue Swirl | * with this program; if not, see <http://www.gnu.org/licenses/>.
|
19 | 7e7c5e4c | balrog | */
|
20 | 7e7c5e4c | balrog | |
21 | 7e7c5e4c | balrog | #include "qemu-common.h" |
22 | 34f9f0b5 | Dmitry Eremin-Solenikov | #include "hw.h" |
23 | 7e7c5e4c | balrog | #include "flash.h" |
24 | 7e7c5e4c | balrog | #include "irq.h" |
25 | 666daa68 | Markus Armbruster | #include "blockdev.h" |
26 | 689a1921 | Avi Kivity | #include "memory.h" |
27 | 689a1921 | Avi Kivity | #include "exec-memory.h" |
28 | 500954e3 | Juha Riihimäki | #include "sysbus.h" |
29 | a3efecb8 | Juha Riihimäki | #include "qemu-error.h" |
30 | 7e7c5e4c | balrog | |
31 | 7e7c5e4c | balrog | /* 11 for 2kB-page OneNAND ("2nd generation") and 10 for 1kB-page chips */
|
32 | 7e7c5e4c | balrog | #define PAGE_SHIFT 11 |
33 | 7e7c5e4c | balrog | |
34 | 7e7c5e4c | balrog | /* Fixed */
|
35 | 7e7c5e4c | balrog | #define BLOCK_SHIFT (PAGE_SHIFT + 6) |
36 | 7e7c5e4c | balrog | |
37 | bc24a225 | Paul Brook | typedef struct { |
38 | 500954e3 | Juha Riihimäki | SysBusDevice busdev; |
39 | 5923ba42 | Juha Riihimäki | struct {
|
40 | 5923ba42 | Juha Riihimäki | uint16_t man; |
41 | 5923ba42 | Juha Riihimäki | uint16_t dev; |
42 | 5923ba42 | Juha Riihimäki | uint16_t ver; |
43 | 5923ba42 | Juha Riihimäki | } id; |
44 | 7e7c5e4c | balrog | int shift;
|
45 | c227f099 | Anthony Liguori | target_phys_addr_t base; |
46 | 7e7c5e4c | balrog | qemu_irq intr; |
47 | 7e7c5e4c | balrog | qemu_irq rdy; |
48 | 7e7c5e4c | balrog | BlockDriverState *bdrv; |
49 | 7e7c5e4c | balrog | BlockDriverState *bdrv_cur; |
50 | 7e7c5e4c | balrog | uint8_t *image; |
51 | 7e7c5e4c | balrog | uint8_t *otp; |
52 | 7e7c5e4c | balrog | uint8_t *current; |
53 | 689a1921 | Avi Kivity | MemoryRegion ram; |
54 | 689a1921 | Avi Kivity | MemoryRegion mapped_ram; |
55 | 500954e3 | Juha Riihimäki | uint8_t current_direction; |
56 | 7e7c5e4c | balrog | uint8_t *boot[2];
|
57 | 7e7c5e4c | balrog | uint8_t *data[2][2]; |
58 | 689a1921 | Avi Kivity | MemoryRegion iomem; |
59 | 689a1921 | Avi Kivity | MemoryRegion container; |
60 | 7e7c5e4c | balrog | int cycle;
|
61 | 7e7c5e4c | balrog | int otpmode;
|
62 | 7e7c5e4c | balrog | |
63 | 7e7c5e4c | balrog | uint16_t addr[8];
|
64 | 7e7c5e4c | balrog | uint16_t unladdr[8];
|
65 | 7e7c5e4c | balrog | int bufaddr;
|
66 | 7e7c5e4c | balrog | int count;
|
67 | 7e7c5e4c | balrog | uint16_t command; |
68 | 7e7c5e4c | balrog | uint16_t config[2];
|
69 | 7e7c5e4c | balrog | uint16_t status; |
70 | 7e7c5e4c | balrog | uint16_t intstatus; |
71 | 7e7c5e4c | balrog | uint16_t wpstatus; |
72 | 7e7c5e4c | balrog | |
73 | bc24a225 | Paul Brook | ECCState ecc; |
74 | 7e7c5e4c | balrog | |
75 | 7e7c5e4c | balrog | int density_mask;
|
76 | 7e7c5e4c | balrog | int secs;
|
77 | 7e7c5e4c | balrog | int secs_cur;
|
78 | 7e7c5e4c | balrog | int blocks;
|
79 | 7e7c5e4c | balrog | uint8_t *blockwp; |
80 | bc24a225 | Paul Brook | } OneNANDState; |
81 | 7e7c5e4c | balrog | |
82 | 7e7c5e4c | balrog | enum {
|
83 | 7e7c5e4c | balrog | ONEN_BUF_BLOCK = 0,
|
84 | 7e7c5e4c | balrog | ONEN_BUF_BLOCK2 = 1,
|
85 | 7e7c5e4c | balrog | ONEN_BUF_DEST_BLOCK = 2,
|
86 | 7e7c5e4c | balrog | ONEN_BUF_DEST_PAGE = 3,
|
87 | 7e7c5e4c | balrog | ONEN_BUF_PAGE = 7,
|
88 | 7e7c5e4c | balrog | }; |
89 | 7e7c5e4c | balrog | |
90 | 7e7c5e4c | balrog | enum {
|
91 | 7e7c5e4c | balrog | ONEN_ERR_CMD = 1 << 10, |
92 | 7e7c5e4c | balrog | ONEN_ERR_ERASE = 1 << 11, |
93 | 7e7c5e4c | balrog | ONEN_ERR_PROG = 1 << 12, |
94 | 7e7c5e4c | balrog | ONEN_ERR_LOAD = 1 << 13, |
95 | 7e7c5e4c | balrog | }; |
96 | 7e7c5e4c | balrog | |
97 | 7e7c5e4c | balrog | enum {
|
98 | 7e7c5e4c | balrog | ONEN_INT_RESET = 1 << 4, |
99 | 7e7c5e4c | balrog | ONEN_INT_ERASE = 1 << 5, |
100 | 7e7c5e4c | balrog | ONEN_INT_PROG = 1 << 6, |
101 | 7e7c5e4c | balrog | ONEN_INT_LOAD = 1 << 7, |
102 | 7e7c5e4c | balrog | ONEN_INT = 1 << 15, |
103 | 7e7c5e4c | balrog | }; |
104 | 7e7c5e4c | balrog | |
105 | 7e7c5e4c | balrog | enum {
|
106 | 7e7c5e4c | balrog | ONEN_LOCK_LOCKTIGHTEN = 1 << 0, |
107 | 7e7c5e4c | balrog | ONEN_LOCK_LOCKED = 1 << 1, |
108 | 7e7c5e4c | balrog | ONEN_LOCK_UNLOCKED = 1 << 2, |
109 | 7e7c5e4c | balrog | }; |
110 | 7e7c5e4c | balrog | |
111 | 689a1921 | Avi Kivity | static void onenand_mem_setup(OneNANDState *s) |
112 | 689a1921 | Avi Kivity | { |
113 | 689a1921 | Avi Kivity | /* XXX: We should use IO_MEM_ROMD but we broke it earlier...
|
114 | 689a1921 | Avi Kivity | * Both 0x0000 ... 0x01ff and 0x8000 ... 0x800f can be used to
|
115 | 689a1921 | Avi Kivity | * write boot commands. Also take note of the BWPS bit. */
|
116 | 689a1921 | Avi Kivity | memory_region_init(&s->container, "onenand", 0x10000 << s->shift); |
117 | 689a1921 | Avi Kivity | memory_region_add_subregion(&s->container, 0, &s->iomem);
|
118 | 689a1921 | Avi Kivity | memory_region_init_alias(&s->mapped_ram, "onenand-mapped-ram",
|
119 | 689a1921 | Avi Kivity | &s->ram, 0x0200 << s->shift,
|
120 | 689a1921 | Avi Kivity | 0xbe00 << s->shift);
|
121 | 689a1921 | Avi Kivity | memory_region_add_subregion_overlap(&s->container, |
122 | 689a1921 | Avi Kivity | 0x0200 << s->shift,
|
123 | 689a1921 | Avi Kivity | &s->mapped_ram, |
124 | 689a1921 | Avi Kivity | 1);
|
125 | 689a1921 | Avi Kivity | } |
126 | 689a1921 | Avi Kivity | |
127 | 500954e3 | Juha Riihimäki | static void onenand_intr_update(OneNANDState *s) |
128 | 7e7c5e4c | balrog | { |
129 | 500954e3 | Juha Riihimäki | qemu_set_irq(s->intr, ((s->intstatus >> 15) ^ (~s->config[0] >> 6)) & 1); |
130 | 7e7c5e4c | balrog | } |
131 | 7e7c5e4c | balrog | |
132 | 500954e3 | Juha Riihimäki | static void onenand_pre_save(void *opaque) |
133 | 7e7c5e4c | balrog | { |
134 | 500954e3 | Juha Riihimäki | OneNANDState *s = opaque; |
135 | 500954e3 | Juha Riihimäki | if (s->current == s->otp) {
|
136 | 500954e3 | Juha Riihimäki | s->current_direction = 1;
|
137 | 500954e3 | Juha Riihimäki | } else if (s->current == s->image) { |
138 | 500954e3 | Juha Riihimäki | s->current_direction = 2;
|
139 | 500954e3 | Juha Riihimäki | } else {
|
140 | 500954e3 | Juha Riihimäki | s->current_direction = 0;
|
141 | 500954e3 | Juha Riihimäki | } |
142 | 7e7c5e4c | balrog | } |
143 | 7e7c5e4c | balrog | |
144 | 500954e3 | Juha Riihimäki | static int onenand_post_load(void *opaque, int version_id) |
145 | 7e7c5e4c | balrog | { |
146 | 500954e3 | Juha Riihimäki | OneNANDState *s = opaque; |
147 | 500954e3 | Juha Riihimäki | switch (s->current_direction) {
|
148 | 500954e3 | Juha Riihimäki | case 0: |
149 | 500954e3 | Juha Riihimäki | break;
|
150 | 500954e3 | Juha Riihimäki | case 1: |
151 | 500954e3 | Juha Riihimäki | s->current = s->otp; |
152 | 500954e3 | Juha Riihimäki | break;
|
153 | 500954e3 | Juha Riihimäki | case 2: |
154 | 500954e3 | Juha Riihimäki | s->current = s->image; |
155 | 500954e3 | Juha Riihimäki | break;
|
156 | 500954e3 | Juha Riihimäki | default:
|
157 | 500954e3 | Juha Riihimäki | return -1; |
158 | 500954e3 | Juha Riihimäki | } |
159 | 500954e3 | Juha Riihimäki | onenand_intr_update(s); |
160 | 500954e3 | Juha Riihimäki | return 0; |
161 | 7e7c5e4c | balrog | } |
162 | 7e7c5e4c | balrog | |
163 | 500954e3 | Juha Riihimäki | static const VMStateDescription vmstate_onenand = { |
164 | 500954e3 | Juha Riihimäki | .name = "onenand",
|
165 | 500954e3 | Juha Riihimäki | .version_id = 1,
|
166 | 500954e3 | Juha Riihimäki | .minimum_version_id = 1,
|
167 | 500954e3 | Juha Riihimäki | .minimum_version_id_old = 1,
|
168 | 500954e3 | Juha Riihimäki | .pre_save = onenand_pre_save, |
169 | 500954e3 | Juha Riihimäki | .post_load = onenand_post_load, |
170 | 500954e3 | Juha Riihimäki | .fields = (VMStateField[]) { |
171 | 500954e3 | Juha Riihimäki | VMSTATE_UINT8(current_direction, OneNANDState), |
172 | 500954e3 | Juha Riihimäki | VMSTATE_INT32(cycle, OneNANDState), |
173 | 500954e3 | Juha Riihimäki | VMSTATE_INT32(otpmode, OneNANDState), |
174 | 500954e3 | Juha Riihimäki | VMSTATE_UINT16_ARRAY(addr, OneNANDState, 8),
|
175 | 500954e3 | Juha Riihimäki | VMSTATE_UINT16_ARRAY(unladdr, OneNANDState, 8),
|
176 | 500954e3 | Juha Riihimäki | VMSTATE_INT32(bufaddr, OneNANDState), |
177 | 500954e3 | Juha Riihimäki | VMSTATE_INT32(count, OneNANDState), |
178 | 500954e3 | Juha Riihimäki | VMSTATE_UINT16(command, OneNANDState), |
179 | 500954e3 | Juha Riihimäki | VMSTATE_UINT16_ARRAY(config, OneNANDState, 2),
|
180 | 500954e3 | Juha Riihimäki | VMSTATE_UINT16(status, OneNANDState), |
181 | 500954e3 | Juha Riihimäki | VMSTATE_UINT16(intstatus, OneNANDState), |
182 | 500954e3 | Juha Riihimäki | VMSTATE_UINT16(wpstatus, OneNANDState), |
183 | 500954e3 | Juha Riihimäki | VMSTATE_INT32(secs_cur, OneNANDState), |
184 | 500954e3 | Juha Riihimäki | VMSTATE_PARTIAL_VBUFFER(blockwp, OneNANDState, blocks), |
185 | 500954e3 | Juha Riihimäki | VMSTATE_UINT8(ecc.cp, OneNANDState), |
186 | 500954e3 | Juha Riihimäki | VMSTATE_UINT16_ARRAY(ecc.lp, OneNANDState, 2),
|
187 | 500954e3 | Juha Riihimäki | VMSTATE_UINT16(ecc.count, OneNANDState), |
188 | 500954e3 | Juha Riihimäki | VMSTATE_BUFFER_UNSAFE(otp, OneNANDState, 0, ((64 + 2) << PAGE_SHIFT)), |
189 | 500954e3 | Juha Riihimäki | VMSTATE_END_OF_LIST() |
190 | 500954e3 | Juha Riihimäki | } |
191 | 500954e3 | Juha Riihimäki | }; |
192 | 500954e3 | Juha Riihimäki | |
193 | 7e7c5e4c | balrog | /* Hot reset (Reset OneNAND command) or warm reset (RP pin low) */
|
194 | bc24a225 | Paul Brook | static void onenand_reset(OneNANDState *s, int cold) |
195 | 7e7c5e4c | balrog | { |
196 | 7e7c5e4c | balrog | memset(&s->addr, 0, sizeof(s->addr)); |
197 | 7e7c5e4c | balrog | s->command = 0;
|
198 | 7e7c5e4c | balrog | s->count = 1;
|
199 | 7e7c5e4c | balrog | s->bufaddr = 0;
|
200 | 7e7c5e4c | balrog | s->config[0] = 0x40c0; |
201 | 7e7c5e4c | balrog | s->config[1] = 0x0000; |
202 | 7e7c5e4c | balrog | onenand_intr_update(s); |
203 | 7e7c5e4c | balrog | qemu_irq_raise(s->rdy); |
204 | 7e7c5e4c | balrog | s->status = 0x0000;
|
205 | 7e7c5e4c | balrog | s->intstatus = cold ? 0x8080 : 0x8010; |
206 | 7e7c5e4c | balrog | s->unladdr[0] = 0; |
207 | 7e7c5e4c | balrog | s->unladdr[1] = 0; |
208 | 7e7c5e4c | balrog | s->wpstatus = 0x0002;
|
209 | 7e7c5e4c | balrog | s->cycle = 0;
|
210 | 7e7c5e4c | balrog | s->otpmode = 0;
|
211 | 7e7c5e4c | balrog | s->bdrv_cur = s->bdrv; |
212 | 7e7c5e4c | balrog | s->current = s->image; |
213 | 7e7c5e4c | balrog | s->secs_cur = s->secs; |
214 | 7e7c5e4c | balrog | |
215 | 7e7c5e4c | balrog | if (cold) {
|
216 | 7e7c5e4c | balrog | /* Lock the whole flash */
|
217 | 7e7c5e4c | balrog | memset(s->blockwp, ONEN_LOCK_LOCKED, s->blocks); |
218 | 7e7c5e4c | balrog | |
219 | 500954e3 | Juha Riihimäki | if (s->bdrv_cur && bdrv_read(s->bdrv_cur, 0, s->boot[0], 8) < 0) { |
220 | 500954e3 | Juha Riihimäki | hw_error("%s: Loading the BootRAM failed.\n", __func__);
|
221 | 500954e3 | Juha Riihimäki | } |
222 | 7e7c5e4c | balrog | } |
223 | 7e7c5e4c | balrog | } |
224 | 7e7c5e4c | balrog | |
225 | 500954e3 | Juha Riihimäki | static void onenand_system_reset(DeviceState *dev) |
226 | 500954e3 | Juha Riihimäki | { |
227 | 500954e3 | Juha Riihimäki | onenand_reset(FROM_SYSBUS(OneNANDState, sysbus_from_qdev(dev)), 1);
|
228 | 500954e3 | Juha Riihimäki | } |
229 | 500954e3 | Juha Riihimäki | |
230 | bc24a225 | Paul Brook | static inline int onenand_load_main(OneNANDState *s, int sec, int secn, |
231 | 7e7c5e4c | balrog | void *dest)
|
232 | 7e7c5e4c | balrog | { |
233 | 7e7c5e4c | balrog | if (s->bdrv_cur)
|
234 | 7e7c5e4c | balrog | return bdrv_read(s->bdrv_cur, sec, dest, secn) < 0; |
235 | 7e7c5e4c | balrog | else if (sec + secn > s->secs_cur) |
236 | 7e7c5e4c | balrog | return 1; |
237 | 7e7c5e4c | balrog | |
238 | 7e7c5e4c | balrog | memcpy(dest, s->current + (sec << 9), secn << 9); |
239 | 7e7c5e4c | balrog | |
240 | 7e7c5e4c | balrog | return 0; |
241 | 7e7c5e4c | balrog | } |
242 | 7e7c5e4c | balrog | |
243 | bc24a225 | Paul Brook | static inline int onenand_prog_main(OneNANDState *s, int sec, int secn, |
244 | 7e7c5e4c | balrog | void *src)
|
245 | 7e7c5e4c | balrog | { |
246 | f1588dd2 | Juha Riihimäki | int result = 0; |
247 | f1588dd2 | Juha Riihimäki | |
248 | f1588dd2 | Juha Riihimäki | if (secn > 0) { |
249 | 7c00b9de | Juha Riihimäki | uint32_t size = (uint32_t)secn * 512;
|
250 | 7c00b9de | Juha Riihimäki | const uint8_t *sp = (const uint8_t *)src; |
251 | f1588dd2 | Juha Riihimäki | uint8_t *dp = 0;
|
252 | f1588dd2 | Juha Riihimäki | if (s->bdrv_cur) {
|
253 | 7267c094 | Anthony Liguori | dp = g_malloc(size); |
254 | f1588dd2 | Juha Riihimäki | if (!dp || bdrv_read(s->bdrv_cur, sec, dp, secn) < 0) { |
255 | f1588dd2 | Juha Riihimäki | result = 1;
|
256 | f1588dd2 | Juha Riihimäki | } |
257 | f1588dd2 | Juha Riihimäki | } else {
|
258 | f1588dd2 | Juha Riihimäki | if (sec + secn > s->secs_cur) {
|
259 | f1588dd2 | Juha Riihimäki | result = 1;
|
260 | f1588dd2 | Juha Riihimäki | } else {
|
261 | 7c00b9de | Juha Riihimäki | dp = (uint8_t *)s->current + (sec << 9);
|
262 | f1588dd2 | Juha Riihimäki | } |
263 | f1588dd2 | Juha Riihimäki | } |
264 | f1588dd2 | Juha Riihimäki | if (!result) {
|
265 | f1588dd2 | Juha Riihimäki | uint32_t i; |
266 | f1588dd2 | Juha Riihimäki | for (i = 0; i < size; i++) { |
267 | f1588dd2 | Juha Riihimäki | dp[i] &= sp[i]; |
268 | f1588dd2 | Juha Riihimäki | } |
269 | f1588dd2 | Juha Riihimäki | if (s->bdrv_cur) {
|
270 | f1588dd2 | Juha Riihimäki | result = bdrv_write(s->bdrv_cur, sec, dp, secn) < 0;
|
271 | f1588dd2 | Juha Riihimäki | } |
272 | f1588dd2 | Juha Riihimäki | } |
273 | f1588dd2 | Juha Riihimäki | if (dp && s->bdrv_cur) {
|
274 | 7267c094 | Anthony Liguori | g_free(dp); |
275 | f1588dd2 | Juha Riihimäki | } |
276 | f1588dd2 | Juha Riihimäki | } |
277 | 7e7c5e4c | balrog | |
278 | f1588dd2 | Juha Riihimäki | return result;
|
279 | 7e7c5e4c | balrog | } |
280 | 7e7c5e4c | balrog | |
281 | bc24a225 | Paul Brook | static inline int onenand_load_spare(OneNANDState *s, int sec, int secn, |
282 | 7e7c5e4c | balrog | void *dest)
|
283 | 7e7c5e4c | balrog | { |
284 | 7e7c5e4c | balrog | uint8_t buf[512];
|
285 | 7e7c5e4c | balrog | |
286 | 7e7c5e4c | balrog | if (s->bdrv_cur) {
|
287 | 7e7c5e4c | balrog | if (bdrv_read(s->bdrv_cur, s->secs_cur + (sec >> 5), buf, 1) < 0) |
288 | 7e7c5e4c | balrog | return 1; |
289 | 7e7c5e4c | balrog | memcpy(dest, buf + ((sec & 31) << 4), secn << 4); |
290 | 7e7c5e4c | balrog | } else if (sec + secn > s->secs_cur) |
291 | 7e7c5e4c | balrog | return 1; |
292 | 7e7c5e4c | balrog | else
|
293 | 7e7c5e4c | balrog | memcpy(dest, s->current + (s->secs_cur << 9) + (sec << 4), secn << 4); |
294 | 7e7c5e4c | balrog | |
295 | 7e7c5e4c | balrog | return 0; |
296 | 7e7c5e4c | balrog | } |
297 | 7e7c5e4c | balrog | |
298 | bc24a225 | Paul Brook | static inline int onenand_prog_spare(OneNANDState *s, int sec, int secn, |
299 | 7e7c5e4c | balrog | void *src)
|
300 | 7e7c5e4c | balrog | { |
301 | f1588dd2 | Juha Riihimäki | int result = 0; |
302 | f1588dd2 | Juha Riihimäki | if (secn > 0) { |
303 | 7c00b9de | Juha Riihimäki | const uint8_t *sp = (const uint8_t *)src; |
304 | f1588dd2 | Juha Riihimäki | uint8_t *dp = 0, *dpp = 0; |
305 | f1588dd2 | Juha Riihimäki | if (s->bdrv_cur) {
|
306 | 7267c094 | Anthony Liguori | dp = g_malloc(512);
|
307 | f1588dd2 | Juha Riihimäki | if (!dp || bdrv_read(s->bdrv_cur,
|
308 | 7c00b9de | Juha Riihimäki | s->secs_cur + (sec >> 5),
|
309 | 7c00b9de | Juha Riihimäki | dp, 1) < 0) { |
310 | f1588dd2 | Juha Riihimäki | result = 1;
|
311 | f1588dd2 | Juha Riihimäki | } else {
|
312 | f1588dd2 | Juha Riihimäki | dpp = dp + ((sec & 31) << 4); |
313 | f1588dd2 | Juha Riihimäki | } |
314 | f1588dd2 | Juha Riihimäki | } else {
|
315 | f1588dd2 | Juha Riihimäki | if (sec + secn > s->secs_cur) {
|
316 | f1588dd2 | Juha Riihimäki | result = 1;
|
317 | f1588dd2 | Juha Riihimäki | } else {
|
318 | f1588dd2 | Juha Riihimäki | dpp = s->current + (s->secs_cur << 9) + (sec << 4); |
319 | f1588dd2 | Juha Riihimäki | } |
320 | f1588dd2 | Juha Riihimäki | } |
321 | f1588dd2 | Juha Riihimäki | if (!result) {
|
322 | f1588dd2 | Juha Riihimäki | uint32_t i; |
323 | f1588dd2 | Juha Riihimäki | for (i = 0; i < (secn << 4); i++) { |
324 | f1588dd2 | Juha Riihimäki | dpp[i] &= sp[i]; |
325 | f1588dd2 | Juha Riihimäki | } |
326 | f1588dd2 | Juha Riihimäki | if (s->bdrv_cur) {
|
327 | f1588dd2 | Juha Riihimäki | result = bdrv_write(s->bdrv_cur, s->secs_cur + (sec >> 5),
|
328 | 7c00b9de | Juha Riihimäki | dp, 1) < 0; |
329 | f1588dd2 | Juha Riihimäki | } |
330 | f1588dd2 | Juha Riihimäki | } |
331 | f1588dd2 | Juha Riihimäki | if (dp) {
|
332 | 7267c094 | Anthony Liguori | g_free(dp); |
333 | f1588dd2 | Juha Riihimäki | } |
334 | f1588dd2 | Juha Riihimäki | } |
335 | f1588dd2 | Juha Riihimäki | return result;
|
336 | 7e7c5e4c | balrog | } |
337 | 7e7c5e4c | balrog | |
338 | bc24a225 | Paul Brook | static inline int onenand_erase(OneNANDState *s, int sec, int num) |
339 | 7e7c5e4c | balrog | { |
340 | f1588dd2 | Juha Riihimäki | uint8_t *blankbuf, *tmpbuf; |
341 | 7267c094 | Anthony Liguori | blankbuf = g_malloc(512);
|
342 | f1588dd2 | Juha Riihimäki | if (!blankbuf) {
|
343 | f1588dd2 | Juha Riihimäki | return 1; |
344 | f1588dd2 | Juha Riihimäki | } |
345 | 7267c094 | Anthony Liguori | tmpbuf = g_malloc(512);
|
346 | f1588dd2 | Juha Riihimäki | if (!tmpbuf) {
|
347 | 7267c094 | Anthony Liguori | g_free(blankbuf); |
348 | f1588dd2 | Juha Riihimäki | return 1; |
349 | f1588dd2 | Juha Riihimäki | } |
350 | f1588dd2 | Juha Riihimäki | memset(blankbuf, 0xff, 512); |
351 | f1588dd2 | Juha Riihimäki | for (; num > 0; num--, sec++) { |
352 | f1588dd2 | Juha Riihimäki | if (s->bdrv_cur) {
|
353 | f1588dd2 | Juha Riihimäki | int erasesec = s->secs_cur + (sec >> 5); |
354 | f1588dd2 | Juha Riihimäki | if (bdrv_write(s->bdrv_cur, sec, blankbuf, 1)) { |
355 | f1588dd2 | Juha Riihimäki | goto fail;
|
356 | f1588dd2 | Juha Riihimäki | } |
357 | f1588dd2 | Juha Riihimäki | if (bdrv_read(s->bdrv_cur, erasesec, tmpbuf, 1) < 0) { |
358 | f1588dd2 | Juha Riihimäki | goto fail;
|
359 | f1588dd2 | Juha Riihimäki | } |
360 | f1588dd2 | Juha Riihimäki | memcpy(tmpbuf + ((sec & 31) << 4), blankbuf, 1 << 4); |
361 | f1588dd2 | Juha Riihimäki | if (bdrv_write(s->bdrv_cur, erasesec, tmpbuf, 1) < 0) { |
362 | f1588dd2 | Juha Riihimäki | goto fail;
|
363 | f1588dd2 | Juha Riihimäki | } |
364 | f1588dd2 | Juha Riihimäki | } else {
|
365 | f1588dd2 | Juha Riihimäki | if (sec + 1 > s->secs_cur) { |
366 | f1588dd2 | Juha Riihimäki | goto fail;
|
367 | f1588dd2 | Juha Riihimäki | } |
368 | f1588dd2 | Juha Riihimäki | memcpy(s->current + (sec << 9), blankbuf, 512); |
369 | f1588dd2 | Juha Riihimäki | memcpy(s->current + (s->secs_cur << 9) + (sec << 4), |
370 | f1588dd2 | Juha Riihimäki | blankbuf, 1 << 4); |
371 | f1588dd2 | Juha Riihimäki | } |
372 | 7e7c5e4c | balrog | } |
373 | 7e7c5e4c | balrog | |
374 | 7267c094 | Anthony Liguori | g_free(tmpbuf); |
375 | 7267c094 | Anthony Liguori | g_free(blankbuf); |
376 | 7e7c5e4c | balrog | return 0; |
377 | f1588dd2 | Juha Riihimäki | |
378 | f1588dd2 | Juha Riihimäki | fail:
|
379 | 7267c094 | Anthony Liguori | g_free(tmpbuf); |
380 | 7267c094 | Anthony Liguori | g_free(blankbuf); |
381 | f1588dd2 | Juha Riihimäki | return 1; |
382 | 7e7c5e4c | balrog | } |
383 | 7e7c5e4c | balrog | |
384 | 82866965 | Juha Riihimäki | static void onenand_command(OneNANDState *s) |
385 | 7e7c5e4c | balrog | { |
386 | 7e7c5e4c | balrog | int b;
|
387 | 7e7c5e4c | balrog | int sec;
|
388 | 7e7c5e4c | balrog | void *buf;
|
389 | 7e7c5e4c | balrog | #define SETADDR(block, page) \
|
390 | 7e7c5e4c | balrog | sec = (s->addr[page] & 3) + \
|
391 | 7e7c5e4c | balrog | ((((s->addr[page] >> 2) & 0x3f) + \ |
392 | 7e7c5e4c | balrog | (((s->addr[block] & 0xfff) | \
|
393 | 7e7c5e4c | balrog | (s->addr[block] >> 15 ? \
|
394 | 7e7c5e4c | balrog | s->density_mask : 0)) << 6)) << (PAGE_SHIFT - 9)); |
395 | 7e7c5e4c | balrog | #define SETBUF_M() \
|
396 | 7e7c5e4c | balrog | buf = (s->bufaddr & 8) ? \
|
397 | 7e7c5e4c | balrog | s->data[(s->bufaddr >> 2) & 1][0] : s->boot[0]; \ |
398 | 7e7c5e4c | balrog | buf += (s->bufaddr & 3) << 9; |
399 | 7e7c5e4c | balrog | #define SETBUF_S() \
|
400 | 7e7c5e4c | balrog | buf = (s->bufaddr & 8) ? \
|
401 | 7e7c5e4c | balrog | s->data[(s->bufaddr >> 2) & 1][1] : s->boot[1]; \ |
402 | 7e7c5e4c | balrog | buf += (s->bufaddr & 3) << 4; |
403 | 7e7c5e4c | balrog | |
404 | 82866965 | Juha Riihimäki | switch (s->command) {
|
405 | 7e7c5e4c | balrog | case 0x00: /* Load single/multiple sector data unit into buffer */ |
406 | 7e7c5e4c | balrog | SETADDR(ONEN_BUF_BLOCK, ONEN_BUF_PAGE) |
407 | 7e7c5e4c | balrog | |
408 | 7e7c5e4c | balrog | SETBUF_M() |
409 | 7e7c5e4c | balrog | if (onenand_load_main(s, sec, s->count, buf))
|
410 | 7e7c5e4c | balrog | s->status |= ONEN_ERR_CMD | ONEN_ERR_LOAD; |
411 | 7e7c5e4c | balrog | |
412 | 7e7c5e4c | balrog | #if 0
|
413 | 7e7c5e4c | balrog | SETBUF_S()
|
414 | 7e7c5e4c | balrog | if (onenand_load_spare(s, sec, s->count, buf))
|
415 | 7e7c5e4c | balrog | s->status |= ONEN_ERR_CMD | ONEN_ERR_LOAD;
|
416 | 7e7c5e4c | balrog | #endif
|
417 | 7e7c5e4c | balrog | |
418 | 7e7c5e4c | balrog | /* TODO: if (s->bufaddr & 3) + s->count was > 4 (2k-pages)
|
419 | 7e7c5e4c | balrog | * or if (s->bufaddr & 1) + s->count was > 2 (1k-pages)
|
420 | 7e7c5e4c | balrog | * then we need two split the read/write into two chunks.
|
421 | 7e7c5e4c | balrog | */
|
422 | 7e7c5e4c | balrog | s->intstatus |= ONEN_INT | ONEN_INT_LOAD; |
423 | 7e7c5e4c | balrog | break;
|
424 | 7e7c5e4c | balrog | case 0x13: /* Load single/multiple spare sector into buffer */ |
425 | 7e7c5e4c | balrog | SETADDR(ONEN_BUF_BLOCK, ONEN_BUF_PAGE) |
426 | 7e7c5e4c | balrog | |
427 | 7e7c5e4c | balrog | SETBUF_S() |
428 | 7e7c5e4c | balrog | if (onenand_load_spare(s, sec, s->count, buf))
|
429 | 7e7c5e4c | balrog | s->status |= ONEN_ERR_CMD | ONEN_ERR_LOAD; |
430 | 7e7c5e4c | balrog | |
431 | 7e7c5e4c | balrog | /* TODO: if (s->bufaddr & 3) + s->count was > 4 (2k-pages)
|
432 | 7e7c5e4c | balrog | * or if (s->bufaddr & 1) + s->count was > 2 (1k-pages)
|
433 | 7e7c5e4c | balrog | * then we need two split the read/write into two chunks.
|
434 | 7e7c5e4c | balrog | */
|
435 | 7e7c5e4c | balrog | s->intstatus |= ONEN_INT | ONEN_INT_LOAD; |
436 | 7e7c5e4c | balrog | break;
|
437 | 7e7c5e4c | balrog | case 0x80: /* Program single/multiple sector data unit from buffer */ |
438 | 7e7c5e4c | balrog | SETADDR(ONEN_BUF_BLOCK, ONEN_BUF_PAGE) |
439 | 7e7c5e4c | balrog | |
440 | 7e7c5e4c | balrog | SETBUF_M() |
441 | 7e7c5e4c | balrog | if (onenand_prog_main(s, sec, s->count, buf))
|
442 | 7e7c5e4c | balrog | s->status |= ONEN_ERR_CMD | ONEN_ERR_PROG; |
443 | 7e7c5e4c | balrog | |
444 | 7e7c5e4c | balrog | #if 0
|
445 | 7e7c5e4c | balrog | SETBUF_S()
|
446 | 7e7c5e4c | balrog | if (onenand_prog_spare(s, sec, s->count, buf))
|
447 | 7e7c5e4c | balrog | s->status |= ONEN_ERR_CMD | ONEN_ERR_PROG;
|
448 | 7e7c5e4c | balrog | #endif
|
449 | 7e7c5e4c | balrog | |
450 | 7e7c5e4c | balrog | /* TODO: if (s->bufaddr & 3) + s->count was > 4 (2k-pages)
|
451 | 7e7c5e4c | balrog | * or if (s->bufaddr & 1) + s->count was > 2 (1k-pages)
|
452 | 7e7c5e4c | balrog | * then we need two split the read/write into two chunks.
|
453 | 7e7c5e4c | balrog | */
|
454 | 7e7c5e4c | balrog | s->intstatus |= ONEN_INT | ONEN_INT_PROG; |
455 | 7e7c5e4c | balrog | break;
|
456 | 7e7c5e4c | balrog | case 0x1a: /* Program single/multiple spare area sector from buffer */ |
457 | 7e7c5e4c | balrog | SETADDR(ONEN_BUF_BLOCK, ONEN_BUF_PAGE) |
458 | 7e7c5e4c | balrog | |
459 | 7e7c5e4c | balrog | SETBUF_S() |
460 | 7e7c5e4c | balrog | if (onenand_prog_spare(s, sec, s->count, buf))
|
461 | 7e7c5e4c | balrog | s->status |= ONEN_ERR_CMD | ONEN_ERR_PROG; |
462 | 7e7c5e4c | balrog | |
463 | 7e7c5e4c | balrog | /* TODO: if (s->bufaddr & 3) + s->count was > 4 (2k-pages)
|
464 | 7e7c5e4c | balrog | * or if (s->bufaddr & 1) + s->count was > 2 (1k-pages)
|
465 | 7e7c5e4c | balrog | * then we need two split the read/write into two chunks.
|
466 | 7e7c5e4c | balrog | */
|
467 | 7e7c5e4c | balrog | s->intstatus |= ONEN_INT | ONEN_INT_PROG; |
468 | 7e7c5e4c | balrog | break;
|
469 | 7e7c5e4c | balrog | case 0x1b: /* Copy-back program */ |
470 | 7e7c5e4c | balrog | SETBUF_S() |
471 | 7e7c5e4c | balrog | |
472 | 7e7c5e4c | balrog | SETADDR(ONEN_BUF_BLOCK, ONEN_BUF_PAGE) |
473 | 7e7c5e4c | balrog | if (onenand_load_main(s, sec, s->count, buf))
|
474 | 7e7c5e4c | balrog | s->status |= ONEN_ERR_CMD | ONEN_ERR_PROG; |
475 | 7e7c5e4c | balrog | |
476 | 7e7c5e4c | balrog | SETADDR(ONEN_BUF_DEST_BLOCK, ONEN_BUF_DEST_PAGE) |
477 | 7e7c5e4c | balrog | if (onenand_prog_main(s, sec, s->count, buf))
|
478 | 7e7c5e4c | balrog | s->status |= ONEN_ERR_CMD | ONEN_ERR_PROG; |
479 | 7e7c5e4c | balrog | |
480 | 7e7c5e4c | balrog | /* TODO: spare areas */
|
481 | 7e7c5e4c | balrog | |
482 | 7e7c5e4c | balrog | s->intstatus |= ONEN_INT | ONEN_INT_PROG; |
483 | 7e7c5e4c | balrog | break;
|
484 | 7e7c5e4c | balrog | |
485 | 7e7c5e4c | balrog | case 0x23: /* Unlock NAND array block(s) */ |
486 | 7e7c5e4c | balrog | s->intstatus |= ONEN_INT; |
487 | 7e7c5e4c | balrog | |
488 | 7e7c5e4c | balrog | /* XXX the previous (?) area should be locked automatically */
|
489 | 7e7c5e4c | balrog | for (b = s->unladdr[0]; b <= s->unladdr[1]; b ++) { |
490 | 7e7c5e4c | balrog | if (b >= s->blocks) {
|
491 | 7e7c5e4c | balrog | s->status |= ONEN_ERR_CMD; |
492 | 7e7c5e4c | balrog | break;
|
493 | 7e7c5e4c | balrog | } |
494 | 7e7c5e4c | balrog | if (s->blockwp[b] == ONEN_LOCK_LOCKTIGHTEN)
|
495 | 7e7c5e4c | balrog | break;
|
496 | 7e7c5e4c | balrog | |
497 | 7e7c5e4c | balrog | s->wpstatus = s->blockwp[b] = ONEN_LOCK_UNLOCKED; |
498 | 7e7c5e4c | balrog | } |
499 | 7e7c5e4c | balrog | break;
|
500 | 89588a4b | balrog | case 0x27: /* Unlock All NAND array blocks */ |
501 | 89588a4b | balrog | s->intstatus |= ONEN_INT; |
502 | 89588a4b | balrog | |
503 | 89588a4b | balrog | for (b = 0; b < s->blocks; b ++) { |
504 | 89588a4b | balrog | if (b >= s->blocks) {
|
505 | 89588a4b | balrog | s->status |= ONEN_ERR_CMD; |
506 | 89588a4b | balrog | break;
|
507 | 89588a4b | balrog | } |
508 | 89588a4b | balrog | if (s->blockwp[b] == ONEN_LOCK_LOCKTIGHTEN)
|
509 | 89588a4b | balrog | break;
|
510 | 89588a4b | balrog | |
511 | 89588a4b | balrog | s->wpstatus = s->blockwp[b] = ONEN_LOCK_UNLOCKED; |
512 | 89588a4b | balrog | } |
513 | 89588a4b | balrog | break;
|
514 | 89588a4b | balrog | |
515 | 7e7c5e4c | balrog | case 0x2a: /* Lock NAND array block(s) */ |
516 | 7e7c5e4c | balrog | s->intstatus |= ONEN_INT; |
517 | 7e7c5e4c | balrog | |
518 | 7e7c5e4c | balrog | for (b = s->unladdr[0]; b <= s->unladdr[1]; b ++) { |
519 | 7e7c5e4c | balrog | if (b >= s->blocks) {
|
520 | 7e7c5e4c | balrog | s->status |= ONEN_ERR_CMD; |
521 | 7e7c5e4c | balrog | break;
|
522 | 7e7c5e4c | balrog | } |
523 | 7e7c5e4c | balrog | if (s->blockwp[b] == ONEN_LOCK_LOCKTIGHTEN)
|
524 | 7e7c5e4c | balrog | break;
|
525 | 7e7c5e4c | balrog | |
526 | 7e7c5e4c | balrog | s->wpstatus = s->blockwp[b] = ONEN_LOCK_LOCKED; |
527 | 7e7c5e4c | balrog | } |
528 | 7e7c5e4c | balrog | break;
|
529 | 7e7c5e4c | balrog | case 0x2c: /* Lock-tight NAND array block(s) */ |
530 | 7e7c5e4c | balrog | s->intstatus |= ONEN_INT; |
531 | 7e7c5e4c | balrog | |
532 | 7e7c5e4c | balrog | for (b = s->unladdr[0]; b <= s->unladdr[1]; b ++) { |
533 | 7e7c5e4c | balrog | if (b >= s->blocks) {
|
534 | 7e7c5e4c | balrog | s->status |= ONEN_ERR_CMD; |
535 | 7e7c5e4c | balrog | break;
|
536 | 7e7c5e4c | balrog | } |
537 | 7e7c5e4c | balrog | if (s->blockwp[b] == ONEN_LOCK_UNLOCKED)
|
538 | 7e7c5e4c | balrog | continue;
|
539 | 7e7c5e4c | balrog | |
540 | 7e7c5e4c | balrog | s->wpstatus = s->blockwp[b] = ONEN_LOCK_LOCKTIGHTEN; |
541 | 7e7c5e4c | balrog | } |
542 | 7e7c5e4c | balrog | break;
|
543 | 7e7c5e4c | balrog | |
544 | 7e7c5e4c | balrog | case 0x71: /* Erase-Verify-Read */ |
545 | 7e7c5e4c | balrog | s->intstatus |= ONEN_INT; |
546 | 7e7c5e4c | balrog | break;
|
547 | 7e7c5e4c | balrog | case 0x95: /* Multi-block erase */ |
548 | 7e7c5e4c | balrog | qemu_irq_pulse(s->intr); |
549 | 7e7c5e4c | balrog | /* Fall through. */
|
550 | 7e7c5e4c | balrog | case 0x94: /* Block erase */ |
551 | 7e7c5e4c | balrog | sec = ((s->addr[ONEN_BUF_BLOCK] & 0xfff) |
|
552 | 7e7c5e4c | balrog | (s->addr[ONEN_BUF_BLOCK] >> 15 ? s->density_mask : 0)) |
553 | 7e7c5e4c | balrog | << (BLOCK_SHIFT - 9);
|
554 | 7e7c5e4c | balrog | if (onenand_erase(s, sec, 1 << (BLOCK_SHIFT - 9))) |
555 | 7e7c5e4c | balrog | s->status |= ONEN_ERR_CMD | ONEN_ERR_ERASE; |
556 | 7e7c5e4c | balrog | |
557 | 7e7c5e4c | balrog | s->intstatus |= ONEN_INT | ONEN_INT_ERASE; |
558 | 7e7c5e4c | balrog | break;
|
559 | 7e7c5e4c | balrog | case 0xb0: /* Erase suspend */ |
560 | 7e7c5e4c | balrog | break;
|
561 | 7e7c5e4c | balrog | case 0x30: /* Erase resume */ |
562 | 7e7c5e4c | balrog | s->intstatus |= ONEN_INT | ONEN_INT_ERASE; |
563 | 7e7c5e4c | balrog | break;
|
564 | 7e7c5e4c | balrog | |
565 | 7e7c5e4c | balrog | case 0xf0: /* Reset NAND Flash core */ |
566 | 7e7c5e4c | balrog | onenand_reset(s, 0);
|
567 | 7e7c5e4c | balrog | break;
|
568 | 7e7c5e4c | balrog | case 0xf3: /* Reset OneNAND */ |
569 | 7e7c5e4c | balrog | onenand_reset(s, 0);
|
570 | 7e7c5e4c | balrog | break;
|
571 | 7e7c5e4c | balrog | |
572 | 7e7c5e4c | balrog | case 0x65: /* OTP Access */ |
573 | 7e7c5e4c | balrog | s->intstatus |= ONEN_INT; |
574 | b9d38e95 | Blue Swirl | s->bdrv_cur = NULL;
|
575 | 7e7c5e4c | balrog | s->current = s->otp; |
576 | 7e7c5e4c | balrog | s->secs_cur = 1 << (BLOCK_SHIFT - 9); |
577 | 7e7c5e4c | balrog | s->addr[ONEN_BUF_BLOCK] = 0;
|
578 | 7e7c5e4c | balrog | s->otpmode = 1;
|
579 | 7e7c5e4c | balrog | break;
|
580 | 7e7c5e4c | balrog | |
581 | 7e7c5e4c | balrog | default:
|
582 | 7e7c5e4c | balrog | s->status |= ONEN_ERR_CMD; |
583 | 7e7c5e4c | balrog | s->intstatus |= ONEN_INT; |
584 | 7e7c5e4c | balrog | fprintf(stderr, "%s: unknown OneNAND command %x\n",
|
585 | 82866965 | Juha Riihimäki | __func__, s->command); |
586 | 7e7c5e4c | balrog | } |
587 | 7e7c5e4c | balrog | |
588 | 7e7c5e4c | balrog | onenand_intr_update(s); |
589 | 7e7c5e4c | balrog | } |
590 | 7e7c5e4c | balrog | |
591 | 689a1921 | Avi Kivity | static uint64_t onenand_read(void *opaque, target_phys_addr_t addr, |
592 | 689a1921 | Avi Kivity | unsigned size)
|
593 | 7e7c5e4c | balrog | { |
594 | bc24a225 | Paul Brook | OneNANDState *s = (OneNANDState *) opaque; |
595 | 8da3ff18 | pbrook | int offset = addr >> s->shift;
|
596 | 7e7c5e4c | balrog | |
597 | 7e7c5e4c | balrog | switch (offset) {
|
598 | 7e7c5e4c | balrog | case 0x0000 ... 0xc000: |
599 | 8da3ff18 | pbrook | return lduw_le_p(s->boot[0] + addr); |
600 | 7e7c5e4c | balrog | |
601 | 7e7c5e4c | balrog | case 0xf000: /* Manufacturer ID */ |
602 | 5923ba42 | Juha Riihimäki | return s->id.man;
|
603 | 7e7c5e4c | balrog | case 0xf001: /* Device ID */ |
604 | 5923ba42 | Juha Riihimäki | return s->id.dev;
|
605 | 7e7c5e4c | balrog | case 0xf002: /* Version ID */ |
606 | 5923ba42 | Juha Riihimäki | return s->id.ver;
|
607 | 5923ba42 | Juha Riihimäki | /* TODO: get the following values from a real chip! */
|
608 | 7e7c5e4c | balrog | case 0xf003: /* Data Buffer size */ |
609 | 7e7c5e4c | balrog | return 1 << PAGE_SHIFT; |
610 | 7e7c5e4c | balrog | case 0xf004: /* Boot Buffer size */ |
611 | 7e7c5e4c | balrog | return 0x200; |
612 | 7e7c5e4c | balrog | case 0xf005: /* Amount of buffers */ |
613 | 7e7c5e4c | balrog | return 1 | (2 << 8); |
614 | 7e7c5e4c | balrog | case 0xf006: /* Technology */ |
615 | 7e7c5e4c | balrog | return 0; |
616 | 7e7c5e4c | balrog | |
617 | 7e7c5e4c | balrog | case 0xf100 ... 0xf107: /* Start addresses */ |
618 | 7e7c5e4c | balrog | return s->addr[offset - 0xf100]; |
619 | 7e7c5e4c | balrog | |
620 | 7e7c5e4c | balrog | case 0xf200: /* Start buffer */ |
621 | 7e7c5e4c | balrog | return (s->bufaddr << 8) | ((s->count - 1) & (1 << (PAGE_SHIFT - 10))); |
622 | 7e7c5e4c | balrog | |
623 | 7e7c5e4c | balrog | case 0xf220: /* Command */ |
624 | 7e7c5e4c | balrog | return s->command;
|
625 | 7e7c5e4c | balrog | case 0xf221: /* System Configuration 1 */ |
626 | 7e7c5e4c | balrog | return s->config[0] & 0xffe0; |
627 | 7e7c5e4c | balrog | case 0xf222: /* System Configuration 2 */ |
628 | 7e7c5e4c | balrog | return s->config[1]; |
629 | 7e7c5e4c | balrog | |
630 | 7e7c5e4c | balrog | case 0xf240: /* Controller Status */ |
631 | 7e7c5e4c | balrog | return s->status;
|
632 | 7e7c5e4c | balrog | case 0xf241: /* Interrupt */ |
633 | 7e7c5e4c | balrog | return s->intstatus;
|
634 | 7e7c5e4c | balrog | case 0xf24c: /* Unlock Start Block Address */ |
635 | 7e7c5e4c | balrog | return s->unladdr[0]; |
636 | 7e7c5e4c | balrog | case 0xf24d: /* Unlock End Block Address */ |
637 | 7e7c5e4c | balrog | return s->unladdr[1]; |
638 | 7e7c5e4c | balrog | case 0xf24e: /* Write Protection Status */ |
639 | 7e7c5e4c | balrog | return s->wpstatus;
|
640 | 7e7c5e4c | balrog | |
641 | 7e7c5e4c | balrog | case 0xff00: /* ECC Status */ |
642 | 7e7c5e4c | balrog | return 0x00; |
643 | 7e7c5e4c | balrog | case 0xff01: /* ECC Result of main area data */ |
644 | 7e7c5e4c | balrog | case 0xff02: /* ECC Result of spare area data */ |
645 | 7e7c5e4c | balrog | case 0xff03: /* ECC Result of main area data */ |
646 | 7e7c5e4c | balrog | case 0xff04: /* ECC Result of spare area data */ |
647 | 2ac71179 | Paul Brook | hw_error("%s: imeplement ECC\n", __FUNCTION__);
|
648 | 7e7c5e4c | balrog | return 0x0000; |
649 | 7e7c5e4c | balrog | } |
650 | 7e7c5e4c | balrog | |
651 | 7e7c5e4c | balrog | fprintf(stderr, "%s: unknown OneNAND register %x\n",
|
652 | 7e7c5e4c | balrog | __FUNCTION__, offset); |
653 | 7e7c5e4c | balrog | return 0; |
654 | 7e7c5e4c | balrog | } |
655 | 7e7c5e4c | balrog | |
656 | c227f099 | Anthony Liguori | static void onenand_write(void *opaque, target_phys_addr_t addr, |
657 | 689a1921 | Avi Kivity | uint64_t value, unsigned size)
|
658 | 7e7c5e4c | balrog | { |
659 | bc24a225 | Paul Brook | OneNANDState *s = (OneNANDState *) opaque; |
660 | 8da3ff18 | pbrook | int offset = addr >> s->shift;
|
661 | 7e7c5e4c | balrog | int sec;
|
662 | 7e7c5e4c | balrog | |
663 | 7e7c5e4c | balrog | switch (offset) {
|
664 | 7e7c5e4c | balrog | case 0x0000 ... 0x01ff: |
665 | 7e7c5e4c | balrog | case 0x8000 ... 0x800f: |
666 | 7e7c5e4c | balrog | if (s->cycle) {
|
667 | 7e7c5e4c | balrog | s->cycle = 0;
|
668 | 7e7c5e4c | balrog | |
669 | 7e7c5e4c | balrog | if (value == 0x0000) { |
670 | 7e7c5e4c | balrog | SETADDR(ONEN_BUF_BLOCK, ONEN_BUF_PAGE) |
671 | 7e7c5e4c | balrog | onenand_load_main(s, sec, |
672 | 7e7c5e4c | balrog | 1 << (PAGE_SHIFT - 9), s->data[0][0]); |
673 | 7e7c5e4c | balrog | s->addr[ONEN_BUF_PAGE] += 4;
|
674 | 7e7c5e4c | balrog | s->addr[ONEN_BUF_PAGE] &= 0xff;
|
675 | 7e7c5e4c | balrog | } |
676 | 7e7c5e4c | balrog | break;
|
677 | 7e7c5e4c | balrog | } |
678 | 7e7c5e4c | balrog | |
679 | 7e7c5e4c | balrog | switch (value) {
|
680 | 7e7c5e4c | balrog | case 0x00f0: /* Reset OneNAND */ |
681 | 7e7c5e4c | balrog | onenand_reset(s, 0);
|
682 | 7e7c5e4c | balrog | break;
|
683 | 7e7c5e4c | balrog | |
684 | 7e7c5e4c | balrog | case 0x00e0: /* Load Data into Buffer */ |
685 | 7e7c5e4c | balrog | s->cycle = 1;
|
686 | 7e7c5e4c | balrog | break;
|
687 | 7e7c5e4c | balrog | |
688 | 7e7c5e4c | balrog | case 0x0090: /* Read Identification Data */ |
689 | 7e7c5e4c | balrog | memset(s->boot[0], 0, 3 << s->shift); |
690 | 5923ba42 | Juha Riihimäki | s->boot[0][0 << s->shift] = s->id.man & 0xff; |
691 | 5923ba42 | Juha Riihimäki | s->boot[0][1 << s->shift] = s->id.dev & 0xff; |
692 | 7e7c5e4c | balrog | s->boot[0][2 << s->shift] = s->wpstatus & 0xff; |
693 | 7e7c5e4c | balrog | break;
|
694 | 7e7c5e4c | balrog | |
695 | 7e7c5e4c | balrog | default:
|
696 | 689a1921 | Avi Kivity | fprintf(stderr, "%s: unknown OneNAND boot command %"PRIx64"\n", |
697 | 7e7c5e4c | balrog | __FUNCTION__, value); |
698 | 7e7c5e4c | balrog | } |
699 | 7e7c5e4c | balrog | break;
|
700 | 7e7c5e4c | balrog | |
701 | 7e7c5e4c | balrog | case 0xf100 ... 0xf107: /* Start addresses */ |
702 | 7e7c5e4c | balrog | s->addr[offset - 0xf100] = value;
|
703 | 7e7c5e4c | balrog | break;
|
704 | 7e7c5e4c | balrog | |
705 | 7e7c5e4c | balrog | case 0xf200: /* Start buffer */ |
706 | 7e7c5e4c | balrog | s->bufaddr = (value >> 8) & 0xf; |
707 | 7e7c5e4c | balrog | if (PAGE_SHIFT == 11) |
708 | 7e7c5e4c | balrog | s->count = (value & 3) ?: 4; |
709 | 7e7c5e4c | balrog | else if (PAGE_SHIFT == 10) |
710 | 7e7c5e4c | balrog | s->count = (value & 1) ?: 2; |
711 | 7e7c5e4c | balrog | break;
|
712 | 7e7c5e4c | balrog | |
713 | 7e7c5e4c | balrog | case 0xf220: /* Command */ |
714 | 7e7c5e4c | balrog | if (s->intstatus & (1 << 15)) |
715 | 7e7c5e4c | balrog | break;
|
716 | 7e7c5e4c | balrog | s->command = value; |
717 | 82866965 | Juha Riihimäki | onenand_command(s); |
718 | 7e7c5e4c | balrog | break;
|
719 | 7e7c5e4c | balrog | case 0xf221: /* System Configuration 1 */ |
720 | 7e7c5e4c | balrog | s->config[0] = value;
|
721 | 7e7c5e4c | balrog | onenand_intr_update(s); |
722 | 7e7c5e4c | balrog | qemu_set_irq(s->rdy, (s->config[0] >> 7) & 1); |
723 | 7e7c5e4c | balrog | break;
|
724 | 7e7c5e4c | balrog | case 0xf222: /* System Configuration 2 */ |
725 | 7e7c5e4c | balrog | s->config[1] = value;
|
726 | 7e7c5e4c | balrog | break;
|
727 | 7e7c5e4c | balrog | |
728 | 7e7c5e4c | balrog | case 0xf241: /* Interrupt */ |
729 | 7e7c5e4c | balrog | s->intstatus &= value; |
730 | 7e7c5e4c | balrog | if ((1 << 15) & ~s->intstatus) |
731 | 7e7c5e4c | balrog | s->status &= ~(ONEN_ERR_CMD | ONEN_ERR_ERASE | |
732 | 7e7c5e4c | balrog | ONEN_ERR_PROG | ONEN_ERR_LOAD); |
733 | 7e7c5e4c | balrog | onenand_intr_update(s); |
734 | 7e7c5e4c | balrog | break;
|
735 | 7e7c5e4c | balrog | case 0xf24c: /* Unlock Start Block Address */ |
736 | 7e7c5e4c | balrog | s->unladdr[0] = value & (s->blocks - 1); |
737 | 7e7c5e4c | balrog | /* For some reason we have to set the end address to by default
|
738 | 7e7c5e4c | balrog | * be same as start because the software forgets to write anything
|
739 | 7e7c5e4c | balrog | * in there. */
|
740 | 7e7c5e4c | balrog | s->unladdr[1] = value & (s->blocks - 1); |
741 | 7e7c5e4c | balrog | break;
|
742 | 7e7c5e4c | balrog | case 0xf24d: /* Unlock End Block Address */ |
743 | 7e7c5e4c | balrog | s->unladdr[1] = value & (s->blocks - 1); |
744 | 7e7c5e4c | balrog | break;
|
745 | 7e7c5e4c | balrog | |
746 | 7e7c5e4c | balrog | default:
|
747 | 7e7c5e4c | balrog | fprintf(stderr, "%s: unknown OneNAND register %x\n",
|
748 | 7e7c5e4c | balrog | __FUNCTION__, offset); |
749 | 7e7c5e4c | balrog | } |
750 | 7e7c5e4c | balrog | } |
751 | 7e7c5e4c | balrog | |
752 | 689a1921 | Avi Kivity | static const MemoryRegionOps onenand_ops = { |
753 | 689a1921 | Avi Kivity | .read = onenand_read, |
754 | 689a1921 | Avi Kivity | .write = onenand_write, |
755 | 689a1921 | Avi Kivity | .endianness = DEVICE_NATIVE_ENDIAN, |
756 | 7e7c5e4c | balrog | }; |
757 | 7e7c5e4c | balrog | |
758 | 500954e3 | Juha Riihimäki | static int onenand_initfn(SysBusDevice *dev) |
759 | 7e7c5e4c | balrog | { |
760 | 500954e3 | Juha Riihimäki | OneNANDState *s = (OneNANDState *)dev; |
761 | 500954e3 | Juha Riihimäki | uint32_t size = 1 << (24 + ((s->id.dev >> 4) & 7)); |
762 | 7e7c5e4c | balrog | void *ram;
|
763 | 500954e3 | Juha Riihimäki | s->base = (target_phys_addr_t)-1;
|
764 | b9d38e95 | Blue Swirl | s->rdy = NULL;
|
765 | 7e7c5e4c | balrog | s->blocks = size >> BLOCK_SHIFT; |
766 | 7e7c5e4c | balrog | s->secs = size >> 9;
|
767 | 7267c094 | Anthony Liguori | s->blockwp = g_malloc(s->blocks); |
768 | 500954e3 | Juha Riihimäki | s->density_mask = (s->id.dev & 0x08)
|
769 | 500954e3 | Juha Riihimäki | ? (1 << (6 + ((s->id.dev >> 4) & 7))) : 0; |
770 | 689a1921 | Avi Kivity | memory_region_init_io(&s->iomem, &onenand_ops, s, "onenand",
|
771 | 689a1921 | Avi Kivity | 0x10000 << s->shift);
|
772 | af5a75f4 | Peter Maydell | if (!s->bdrv) {
|
773 | 7267c094 | Anthony Liguori | s->image = memset(g_malloc(size + (size >> 5)),
|
774 | 500954e3 | Juha Riihimäki | 0xff, size + (size >> 5)); |
775 | 500954e3 | Juha Riihimäki | } else {
|
776 | a3efecb8 | Juha Riihimäki | if (bdrv_is_read_only(s->bdrv)) {
|
777 | a3efecb8 | Juha Riihimäki | error_report("Can't use a read-only drive");
|
778 | a3efecb8 | Juha Riihimäki | return -1; |
779 | a3efecb8 | Juha Riihimäki | } |
780 | 500954e3 | Juha Riihimäki | s->bdrv_cur = s->bdrv; |
781 | 63efb1d9 | Andrzej Zaborowski | } |
782 | 7267c094 | Anthony Liguori | s->otp = memset(g_malloc((64 + 2) << PAGE_SHIFT), |
783 | 7e7c5e4c | balrog | 0xff, (64 + 2) << PAGE_SHIFT); |
784 | 689a1921 | Avi Kivity | memory_region_init_ram(&s->ram, NULL, "onenand.ram", 0xc000 << s->shift); |
785 | 689a1921 | Avi Kivity | ram = memory_region_get_ram_ptr(&s->ram); |
786 | 7e7c5e4c | balrog | s->boot[0] = ram + (0x0000 << s->shift); |
787 | 7e7c5e4c | balrog | s->boot[1] = ram + (0x8000 << s->shift); |
788 | 7e7c5e4c | balrog | s->data[0][0] = ram + ((0x0200 + (0 << (PAGE_SHIFT - 1))) << s->shift); |
789 | 7e7c5e4c | balrog | s->data[0][1] = ram + ((0x8010 + (0 << (PAGE_SHIFT - 6))) << s->shift); |
790 | 7e7c5e4c | balrog | s->data[1][0] = ram + ((0x0200 + (1 << (PAGE_SHIFT - 1))) << s->shift); |
791 | 7e7c5e4c | balrog | s->data[1][1] = ram + ((0x8010 + (1 << (PAGE_SHIFT - 6))) << s->shift); |
792 | 689a1921 | Avi Kivity | onenand_mem_setup(s); |
793 | 500954e3 | Juha Riihimäki | sysbus_init_irq(dev, &s->intr); |
794 | 500954e3 | Juha Riihimäki | sysbus_init_mmio_region(dev, &s->container); |
795 | 500954e3 | Juha Riihimäki | vmstate_register(&dev->qdev, |
796 | 500954e3 | Juha Riihimäki | ((s->shift & 0x7f) << 24) |
797 | 500954e3 | Juha Riihimäki | | ((s->id.man & 0xff) << 16) |
798 | 500954e3 | Juha Riihimäki | | ((s->id.dev & 0xff) << 8) |
799 | 500954e3 | Juha Riihimäki | | (s->id.ver & 0xff),
|
800 | 500954e3 | Juha Riihimäki | &vmstate_onenand, s); |
801 | 500954e3 | Juha Riihimäki | return 0; |
802 | 500954e3 | Juha Riihimäki | } |
803 | 7e7c5e4c | balrog | |
804 | 500954e3 | Juha Riihimäki | static SysBusDeviceInfo onenand_info = {
|
805 | 500954e3 | Juha Riihimäki | .init = onenand_initfn, |
806 | 500954e3 | Juha Riihimäki | .qdev.name = "onenand",
|
807 | 500954e3 | Juha Riihimäki | .qdev.size = sizeof(OneNANDState),
|
808 | 500954e3 | Juha Riihimäki | .qdev.reset = onenand_system_reset, |
809 | 500954e3 | Juha Riihimäki | .qdev.props = (Property[]) { |
810 | 500954e3 | Juha Riihimäki | DEFINE_PROP_UINT16("manufacturer_id", OneNANDState, id.man, 0), |
811 | 500954e3 | Juha Riihimäki | DEFINE_PROP_UINT16("device_id", OneNANDState, id.dev, 0), |
812 | 500954e3 | Juha Riihimäki | DEFINE_PROP_UINT16("version_id", OneNANDState, id.ver, 0), |
813 | 500954e3 | Juha Riihimäki | DEFINE_PROP_INT32("shift", OneNANDState, shift, 0), |
814 | 500954e3 | Juha Riihimäki | DEFINE_PROP_DRIVE("drive", OneNANDState, bdrv),
|
815 | 500954e3 | Juha Riihimäki | DEFINE_PROP_END_OF_LIST() |
816 | 500954e3 | Juha Riihimäki | } |
817 | 500954e3 | Juha Riihimäki | }; |
818 | 7e7c5e4c | balrog | |
819 | 500954e3 | Juha Riihimäki | static void onenand_register_device(void) |
820 | 500954e3 | Juha Riihimäki | { |
821 | 500954e3 | Juha Riihimäki | sysbus_register_withprop(&onenand_info); |
822 | 7e7c5e4c | balrog | } |
823 | c580d92b | balrog | |
824 | 500954e3 | Juha Riihimäki | void *onenand_raw_otp(DeviceState *onenand_device)
|
825 | c580d92b | balrog | { |
826 | 500954e3 | Juha Riihimäki | return FROM_SYSBUS(OneNANDState, sysbus_from_qdev(onenand_device))->otp;
|
827 | c580d92b | balrog | } |
828 | 500954e3 | Juha Riihimäki | |
829 | 500954e3 | Juha Riihimäki | device_init(onenand_register_device) |