nandemulator.c revision 1.5.2.2 1 1.5.2.2 matt /* $NetBSD: nandemulator.c,v 1.5.2.2 2011/07/26 03:22:28 matt Exp $ */
2 1.5.2.2 matt
3 1.5.2.2 matt /*-
4 1.5.2.2 matt * Copyright (c) 2011 Department of Software Engineering,
5 1.5.2.2 matt * University of Szeged, Hungary
6 1.5.2.2 matt * Copyright (c) 2011 Adam Hoka <ahoka (at) NetBSD.org>
7 1.5.2.2 matt * All rights reserved.
8 1.5.2.2 matt *
9 1.5.2.2 matt * This code is derived from software contributed to The NetBSD Foundation
10 1.5.2.2 matt * by the Department of Software Engineering, University of Szeged, Hungary
11 1.5.2.2 matt *
12 1.5.2.2 matt * Redistribution and use in source and binary forms, with or without
13 1.5.2.2 matt * modification, are permitted provided that the following conditions
14 1.5.2.2 matt * are met:
15 1.5.2.2 matt * 1. Redistributions of source code must retain the above copyright
16 1.5.2.2 matt * notice, this list of conditions and the following disclaimer.
17 1.5.2.2 matt * 2. Redistributions in binary form must reproduce the above copyright
18 1.5.2.2 matt * notice, this list of conditions and the following disclaimer in the
19 1.5.2.2 matt * documentation and/or other materials provided with the distribution.
20 1.5.2.2 matt *
21 1.5.2.2 matt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.5.2.2 matt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.5.2.2 matt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.5.2.2 matt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.5.2.2 matt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 1.5.2.2 matt * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.5.2.2 matt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 1.5.2.2 matt * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 1.5.2.2 matt * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.5.2.2 matt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.5.2.2 matt * SUCH DAMAGE.
32 1.5.2.2 matt */
33 1.5.2.2 matt
34 1.5.2.2 matt #include <sys/cdefs.h>
35 1.5.2.2 matt __KERNEL_RCSID(0, "$NetBSD: nandemulator.c,v 1.5.2.2 2011/07/26 03:22:28 matt Exp $");
36 1.5.2.2 matt
37 1.5.2.2 matt #include <sys/param.h>
38 1.5.2.2 matt #include <sys/device.h>
39 1.5.2.2 matt #include <sys/conf.h>
40 1.5.2.2 matt #include <sys/kmem.h>
41 1.5.2.2 matt #include <sys/kernel.h>
42 1.5.2.2 matt
43 1.5.2.2 matt #include "nandemulator.h"
44 1.5.2.2 matt
45 1.5.2.2 matt #include <dev/nand/nand.h>
46 1.5.2.2 matt #include <dev/nand/onfi.h>
47 1.5.2.2 matt #include <dev/nand/nand_crc.h>
48 1.5.2.2 matt
49 1.5.2.2 matt extern struct cfdriver nandemulator_cd;
50 1.5.2.2 matt void nandemulatorattach(int n);
51 1.5.2.2 matt
52 1.5.2.2 matt static int nandemulator_match(device_t, cfdata_t, void *);
53 1.5.2.2 matt static void nandemulator_attach(device_t, device_t, void *);
54 1.5.2.2 matt static int nandemulator_detach(device_t, int);
55 1.5.2.2 matt
56 1.5.2.2 matt static void nandemulator_device_reset(device_t);
57 1.5.2.2 matt static void nandemulator_command(device_t, uint8_t);
58 1.5.2.2 matt static void nandemulator_address(device_t, uint8_t);
59 1.5.2.2 matt static void nandemulator_busy(device_t);
60 1.5.2.2 matt static void nandemulator_read_1(device_t, uint8_t *);
61 1.5.2.2 matt static void nandemulator_write_1(device_t, uint8_t);
62 1.5.2.2 matt static void nandemulator_read_2(device_t, uint16_t *);
63 1.5.2.2 matt static void nandemulator_write_2(device_t, uint16_t);
64 1.5.2.2 matt static void nandemulator_read_buf_1(device_t, void *, size_t);
65 1.5.2.2 matt static void nandemulator_read_buf_2(device_t, void *, size_t);
66 1.5.2.2 matt static void nandemulator_write_buf_1(device_t, const void *, size_t);
67 1.5.2.2 matt static void nandemulator_write_buf_2(device_t, const void *, size_t);
68 1.5.2.2 matt
69 1.5.2.2 matt static size_t nandemulator_address_to_page(device_t);
70 1.5.2.2 matt static size_t nandemulator_page_to_backend_offset(device_t, size_t);
71 1.5.2.2 matt static size_t nandemulator_column_address_to_subpage(device_t);
72 1.5.2.2 matt /*
73 1.5.2.2 matt #define NANDEMULATOR_DEBUG 1
74 1.5.2.2 matt
75 1.5.2.2 matt #ifdef NANDEMULATOR_DEBUG
76 1.5.2.2 matt #warning debug enabled
77 1.5.2.2 matt #define DPRINTF(x) if (nandemulatordebug) printf x
78 1.5.2.2 matt #define DPRINTFN(n,x) if (nandemulatordebug>(n)) printf x
79 1.5.2.2 matt #else
80 1.5.2.2 matt #error no debug
81 1.5.2.2 matt #define DPRINTF(x)
82 1.5.2.2 matt #define DPRINTFN(n,x)
83 1.5.2.2 matt #endif
84 1.5.2.2 matt
85 1.5.2.2 matt #ifdef NANDEMULATOR_DEBUG
86 1.5.2.2 matt int nandemulatordebug = NANDEMULATOR_DEBUG;
87 1.5.2.2 matt #endif
88 1.5.2.2 matt */
89 1.5.2.2 matt
90 1.5.2.2 matt extern int nanddebug;
91 1.5.2.2 matt
92 1.5.2.2 matt enum {
93 1.5.2.2 matt NANDEMULATOR_8BIT,
94 1.5.2.2 matt NANDEMULATOR_16BIT
95 1.5.2.2 matt };
96 1.5.2.2 matt
97 1.5.2.2 matt struct nandemulator_softc {
98 1.5.2.2 matt device_t sc_dev;
99 1.5.2.2 matt device_t sc_nanddev;
100 1.5.2.2 matt
101 1.5.2.2 matt int sc_buswidth;
102 1.5.2.2 matt
103 1.5.2.2 matt struct nand_interface sc_nand_if;
104 1.5.2.2 matt
105 1.5.2.2 matt uint8_t sc_command;
106 1.5.2.2 matt size_t sc_io_len;
107 1.5.2.2 matt uint8_t *sc_io_pointer;
108 1.5.2.2 matt uint64_t sc_address;
109 1.5.2.2 matt
110 1.5.2.2 matt uint8_t *sc_backend;
111 1.5.2.2 matt size_t sc_backend_size;
112 1.5.2.2 matt size_t sc_device_size;
113 1.5.2.2 matt bool sc_register_writable;
114 1.5.2.2 matt
115 1.5.2.2 matt uint8_t sc_status_register;
116 1.5.2.2 matt uint8_t sc_ids[2];
117 1.5.2.2 matt uint8_t sc_onfi[4];
118 1.5.2.2 matt
119 1.5.2.2 matt size_t sc_page_size;
120 1.5.2.2 matt size_t sc_block_size;
121 1.5.2.2 matt size_t sc_spare_size;
122 1.5.2.2 matt size_t sc_lun_size;
123 1.5.2.2 matt uint8_t sc_row_cycles;
124 1.5.2.2 matt uint8_t sc_column_cycles;
125 1.5.2.2 matt uint64_t sc_row_mask;
126 1.5.2.2 matt
127 1.5.2.2 matt int sc_address_counter;
128 1.5.2.2 matt
129 1.5.2.2 matt struct onfi_parameter_page *sc_parameter_page;
130 1.5.2.2 matt };
131 1.5.2.2 matt
132 1.5.2.2 matt CFATTACH_DECL_NEW(nandemulator, sizeof(struct nandemulator_softc),
133 1.5.2.2 matt nandemulator_match, nandemulator_attach, nandemulator_detach, NULL);
134 1.5.2.2 matt
135 1.5.2.2 matt void
136 1.5.2.2 matt nandemulatorattach(int n)
137 1.5.2.2 matt {
138 1.5.2.2 matt int i, err;
139 1.5.2.2 matt cfdata_t cf;
140 1.5.2.2 matt
141 1.5.2.2 matt aprint_debug("nandemulator: requested %d units\n", n);
142 1.5.2.2 matt
143 1.5.2.2 matt err = config_cfattach_attach(nandemulator_cd.cd_name,
144 1.5.2.2 matt &nandemulator_ca);
145 1.5.2.2 matt if (err) {
146 1.5.2.2 matt aprint_error("%s: couldn't register cfattach: %d\n",
147 1.5.2.2 matt nandemulator_cd.cd_name, err);
148 1.5.2.2 matt config_cfdriver_detach(&nandemulator_cd);
149 1.5.2.2 matt return;
150 1.5.2.2 matt }
151 1.5.2.2 matt for (i = 0; i < n; i++) {
152 1.5.2.2 matt cf = kmem_alloc(sizeof(struct cfdata), KM_NOSLEEP);
153 1.5.2.2 matt if (cf == NULL) {
154 1.5.2.2 matt aprint_error("%s: couldn't allocate cfdata\n",
155 1.5.2.2 matt nandemulator_cd.cd_name);
156 1.5.2.2 matt continue;
157 1.5.2.2 matt }
158 1.5.2.2 matt cf->cf_name = nandemulator_cd.cd_name;
159 1.5.2.2 matt cf->cf_atname = nandemulator_cd.cd_name;
160 1.5.2.2 matt cf->cf_unit = i;
161 1.5.2.2 matt cf->cf_fstate = FSTATE_STAR;
162 1.5.2.2 matt
163 1.5.2.2 matt (void)config_attach_pseudo(cf);
164 1.5.2.2 matt }
165 1.5.2.2 matt }
166 1.5.2.2 matt
167 1.5.2.2 matt /* ARGSUSED */
168 1.5.2.2 matt static int
169 1.5.2.2 matt nandemulator_match(device_t parent, cfdata_t match, void *aux)
170 1.5.2.2 matt {
171 1.5.2.2 matt /* pseudo device, always attaches */
172 1.5.2.2 matt return 1;
173 1.5.2.2 matt }
174 1.5.2.2 matt
175 1.5.2.2 matt static void
176 1.5.2.2 matt nandemulator_attach(device_t parent, device_t self, void *aux)
177 1.5.2.2 matt {
178 1.5.2.2 matt struct nandemulator_softc *sc = device_private(self);
179 1.5.2.2 matt int i;
180 1.5.2.2 matt
181 1.5.2.2 matt aprint_normal_dev(self, "NAND emulator\n");
182 1.5.2.2 matt
183 1.5.2.2 matt sc->sc_dev = self;
184 1.5.2.2 matt
185 1.5.2.2 matt nand_init_interface(&sc->sc_nand_if);
186 1.5.2.2 matt
187 1.5.2.2 matt sc->sc_nand_if.command = &nandemulator_command;
188 1.5.2.2 matt sc->sc_nand_if.address = &nandemulator_address;
189 1.5.2.2 matt sc->sc_nand_if.read_buf_1 = &nandemulator_read_buf_1;
190 1.5.2.2 matt sc->sc_nand_if.read_buf_2 = &nandemulator_read_buf_2;
191 1.5.2.2 matt sc->sc_nand_if.read_1 = &nandemulator_read_1;
192 1.5.2.2 matt sc->sc_nand_if.read_2 = &nandemulator_read_2;
193 1.5.2.2 matt sc->sc_nand_if.write_buf_1 = &nandemulator_write_buf_1;
194 1.5.2.2 matt sc->sc_nand_if.write_buf_2 = &nandemulator_write_buf_2;
195 1.5.2.2 matt sc->sc_nand_if.write_1 = &nandemulator_write_1;
196 1.5.2.2 matt sc->sc_nand_if.write_2 = &nandemulator_write_2;
197 1.5.2.2 matt sc->sc_nand_if.busy = &nandemulator_busy;
198 1.5.2.2 matt
199 1.5.2.2 matt sc->sc_nand_if.ecc.necc_code_size = 3;
200 1.5.2.2 matt sc->sc_nand_if.ecc.necc_block_size = 256;
201 1.5.2.2 matt
202 1.5.2.2 matt if (!pmf_device_register1(sc->sc_dev, NULL, NULL, NULL))
203 1.5.2.2 matt aprint_error_dev(sc->sc_dev,
204 1.5.2.2 matt "couldn't establish power handler\n");
205 1.5.2.2 matt
206 1.5.2.2 matt sc->sc_buswidth = NANDEMULATOR_16BIT; /* 16bit for now */
207 1.5.2.2 matt
208 1.5.2.2 matt /* hardcode these now, make it configurable later */
209 1.5.2.2 matt sc->sc_device_size = 32 * 1024 * 1024; /* 32MB */
210 1.5.2.2 matt sc->sc_page_size = 2048;
211 1.5.2.2 matt sc->sc_block_size = 64;
212 1.5.2.2 matt sc->sc_lun_size =
213 1.5.2.2 matt sc->sc_device_size / (sc->sc_page_size * sc->sc_block_size);
214 1.5.2.2 matt KASSERT(sc->sc_device_size %
215 1.5.2.2 matt (sc->sc_page_size * sc->sc_block_size) == 0);
216 1.5.2.2 matt sc->sc_spare_size = 64;
217 1.5.2.2 matt
218 1.5.2.2 matt sc->sc_column_cycles = 2;
219 1.5.2.2 matt sc->sc_row_cycles = 3;
220 1.5.2.2 matt
221 1.5.2.2 matt /* init the emulator data structures */
222 1.5.2.2 matt sc->sc_backend_size =
223 1.5.2.2 matt sc->sc_device_size +
224 1.5.2.2 matt sc->sc_device_size / sc->sc_page_size * sc->sc_spare_size;
225 1.5.2.2 matt
226 1.5.2.2 matt sc->sc_backend = kmem_alloc(sc->sc_backend_size, KM_SLEEP);
227 1.5.2.2 matt memset(sc->sc_backend, 0xff, sc->sc_backend_size);
228 1.5.2.2 matt
229 1.5.2.2 matt sc->sc_parameter_page =
230 1.5.2.2 matt kmem_zalloc(sizeof(struct onfi_parameter_page) * 4, KM_SLEEP);
231 1.5.2.2 matt
232 1.5.2.2 matt struct onfi_parameter_page *opp;
233 1.5.2.2 matt uint8_t sig[4] = { 'O', 'N', 'F', 'I' };
234 1.5.2.2 matt
235 1.5.2.2 matt for (i = 0; i < 4; i++) {
236 1.5.2.2 matt opp = &sc->sc_parameter_page[i];
237 1.5.2.2 matt
238 1.5.2.2 matt opp->param_signature = *(uint32_t *)sig;
239 1.5.2.2 matt opp->param_pagesize = sc->sc_page_size;
240 1.5.2.2 matt opp->param_blocksize = sc->sc_block_size;
241 1.5.2.2 matt opp->param_sparesize = sc->sc_spare_size;
242 1.5.2.2 matt opp->param_lunsize = sc->sc_lun_size;
243 1.5.2.2 matt opp->param_numluns = 1;
244 1.5.2.2 matt
245 1.5.2.2 matt opp->param_manufacturer_id = 0x00;
246 1.5.2.2 matt memcpy(opp->param_manufacturer,
247 1.5.2.2 matt "NETBSD", strlen("NETBSD"));
248 1.5.2.2 matt memcpy(opp->param_model,
249 1.5.2.2 matt "NANDEMULATOR", strlen("NANDEMULATOR"));
250 1.5.2.2 matt
251 1.5.2.2 matt opp->param_features = ONFI_FEATURE_16BIT;
252 1.5.2.2 matt
253 1.5.2.2 matt /* the lower 4 bits contain the row address cycles
254 1.5.2.2 matt * the upper 4 bits contain the column address cycles
255 1.5.2.2 matt */
256 1.5.2.2 matt opp->param_addr_cycles = sc->sc_row_cycles;
257 1.5.2.2 matt opp->param_addr_cycles |= (sc->sc_column_cycles << 4);
258 1.5.2.2 matt
259 1.5.2.2 matt opp->param_integrity_crc = nand_crc16((uint8_t *)opp, 254);
260 1.5.2.2 matt }
261 1.5.2.2 matt
262 1.5.2.2 matt sc->sc_ids[0] = 0x00;
263 1.5.2.2 matt sc->sc_ids[1] = 0x00;
264 1.5.2.2 matt
265 1.5.2.2 matt sc->sc_onfi[0] = 'O';
266 1.5.2.2 matt sc->sc_onfi[1] = 'N';
267 1.5.2.2 matt sc->sc_onfi[2] = 'F';
268 1.5.2.2 matt sc->sc_onfi[3] = 'I';
269 1.5.2.2 matt
270 1.5.2.2 matt sc->sc_row_mask = 0x00;
271 1.5.2.2 matt for (i = 0; i < sc->sc_row_cycles; i++) {
272 1.5.2.2 matt sc->sc_row_mask <<= 8;
273 1.5.2.2 matt sc->sc_row_mask |= 0xff;
274 1.5.2.2 matt }
275 1.5.2.2 matt
276 1.5.2.2 matt nandemulator_device_reset(self);
277 1.5.2.2 matt
278 1.5.2.2 matt sc->sc_nanddev = nand_attach_mi(&sc->sc_nand_if, sc->sc_dev);
279 1.5.2.2 matt }
280 1.5.2.2 matt
281 1.5.2.2 matt static int
282 1.5.2.2 matt nandemulator_detach(device_t self, int flags)
283 1.5.2.2 matt {
284 1.5.2.2 matt struct nandemulator_softc *sc = device_private(self);
285 1.5.2.2 matt int ret = 0;
286 1.5.2.2 matt
287 1.5.2.2 matt aprint_normal_dev(sc->sc_dev, "detaching emulator\n");
288 1.5.2.2 matt
289 1.5.2.2 matt pmf_device_deregister(sc->sc_dev);
290 1.5.2.2 matt
291 1.5.2.2 matt if (sc->sc_nanddev != NULL)
292 1.5.2.2 matt ret = config_detach(sc->sc_nanddev, flags);
293 1.5.2.2 matt
294 1.5.2.2 matt kmem_free(sc->sc_backend, sc->sc_backend_size);
295 1.5.2.2 matt kmem_free(sc->sc_parameter_page,
296 1.5.2.2 matt sizeof(struct onfi_parameter_page) * 4);
297 1.5.2.2 matt
298 1.5.2.2 matt return ret;
299 1.5.2.2 matt }
300 1.5.2.2 matt
301 1.5.2.2 matt /**
302 1.5.2.2 matt * bring the emulated device to a known state
303 1.5.2.2 matt */
304 1.5.2.2 matt static void
305 1.5.2.2 matt nandemulator_device_reset(device_t self)
306 1.5.2.2 matt {
307 1.5.2.2 matt struct nandemulator_softc *sc = device_private(self);
308 1.5.2.2 matt
309 1.5.2.2 matt DPRINTF(("device reset\n"));
310 1.5.2.2 matt
311 1.5.2.2 matt sc->sc_command = 0;
312 1.5.2.2 matt sc->sc_register_writable = false;
313 1.5.2.2 matt sc->sc_io_len = 0;
314 1.5.2.2 matt sc->sc_io_pointer = NULL;
315 1.5.2.2 matt sc->sc_address = 0;
316 1.5.2.2 matt sc->sc_address_counter = 0;
317 1.5.2.2 matt
318 1.5.2.2 matt sc->sc_status_register = ONFI_STATUS_RDY | ONFI_STATUS_WP;
319 1.5.2.2 matt }
320 1.5.2.2 matt
321 1.5.2.2 matt static void
322 1.5.2.2 matt nandemulator_address_chip(device_t self)
323 1.5.2.2 matt {
324 1.5.2.2 matt struct nandemulator_softc *sc = device_private(self);
325 1.5.2.2 matt size_t page, offset;
326 1.5.2.2 matt
327 1.5.2.2 matt KASSERT(sc->sc_address_counter ==
328 1.5.2.2 matt sc->sc_column_cycles + sc->sc_row_cycles);
329 1.5.2.2 matt
330 1.5.2.2 matt if (sc->sc_address_counter !=
331 1.5.2.2 matt sc->sc_column_cycles + sc->sc_row_cycles) {
332 1.5.2.2 matt aprint_error_dev(self, "incorrect number of address cycles\n");
333 1.5.2.2 matt aprint_error_dev(self, "cc: %d, rc: %d, ac: %d\n",
334 1.5.2.2 matt sc->sc_column_cycles, sc->sc_row_cycles,
335 1.5.2.2 matt sc->sc_address_counter);
336 1.5.2.2 matt }
337 1.5.2.2 matt
338 1.5.2.2 matt page = nandemulator_address_to_page(self);
339 1.5.2.2 matt offset = sc->sc_page_size * page;
340 1.5.2.2 matt
341 1.5.2.2 matt DPRINTF(("READ/PROGRAM; page: 0x%jx (row addr: 0x%jx)\n",
342 1.5.2.2 matt (uintmax_t )page,
343 1.5.2.2 matt (uintmax_t )offset));
344 1.5.2.2 matt
345 1.5.2.2 matt KASSERT(offset < sc->sc_device_size);
346 1.5.2.2 matt
347 1.5.2.2 matt if (offset >= sc->sc_device_size) {
348 1.5.2.2 matt aprint_error_dev(self, "address > device size!\n");
349 1.5.2.2 matt sc->sc_io_len = 0;
350 1.5.2.2 matt } else {
351 1.5.2.2 matt size_t addr =
352 1.5.2.2 matt nandemulator_page_to_backend_offset(self, page);
353 1.5.2.2 matt size_t pageoff =
354 1.5.2.2 matt nandemulator_column_address_to_subpage(self);
355 1.5.2.2 matt
356 1.5.2.2 matt DPRINTF(("subpage: 0x%jx\n", (uintmax_t )pageoff));
357 1.5.2.2 matt
358 1.5.2.2 matt KASSERT(pageoff <
359 1.5.2.2 matt sc->sc_page_size + sc->sc_spare_size);
360 1.5.2.2 matt KASSERT(addr < sc->sc_backend_size);
361 1.5.2.2 matt
362 1.5.2.2 matt sc->sc_io_pointer = sc->sc_backend + addr + pageoff;
363 1.5.2.2 matt sc->sc_io_len =
364 1.5.2.2 matt sc->sc_page_size + sc->sc_spare_size - pageoff;
365 1.5.2.2 matt }
366 1.5.2.2 matt }
367 1.5.2.2 matt
368 1.5.2.2 matt static void
369 1.5.2.2 matt nandemulator_command(device_t self, uint8_t command)
370 1.5.2.2 matt {
371 1.5.2.2 matt struct nandemulator_softc *sc = device_private(self);
372 1.5.2.2 matt size_t offset, page;
373 1.5.2.2 matt
374 1.5.2.2 matt sc->sc_command = command;
375 1.5.2.2 matt sc->sc_register_writable = false;
376 1.5.2.2 matt
377 1.5.2.2 matt DPRINTF(("nandemulator command: 0x%hhx\n", command));
378 1.5.2.2 matt
379 1.5.2.2 matt switch (command) {
380 1.5.2.2 matt case ONFI_READ_STATUS:
381 1.5.2.2 matt sc->sc_io_pointer = &sc->sc_status_register;
382 1.5.2.2 matt sc->sc_io_len = 1;
383 1.5.2.2 matt break;
384 1.5.2.2 matt case ONFI_RESET:
385 1.5.2.2 matt nandemulator_device_reset(self);
386 1.5.2.2 matt break;
387 1.5.2.2 matt case ONFI_PAGE_PROGRAM:
388 1.5.2.2 matt sc->sc_register_writable = true;
389 1.5.2.2 matt case ONFI_READ:
390 1.5.2.2 matt case ONFI_BLOCK_ERASE:
391 1.5.2.2 matt sc->sc_address_counter = 0;
392 1.5.2.2 matt case ONFI_READ_ID:
393 1.5.2.2 matt case ONFI_READ_PARAMETER_PAGE:
394 1.5.2.2 matt sc->sc_io_len = 0;
395 1.5.2.2 matt sc->sc_address = 0;
396 1.5.2.2 matt break;
397 1.5.2.2 matt case ONFI_PAGE_PROGRAM_START:
398 1.5.2.2 matt /* XXX the program should only happen here */
399 1.5.2.2 matt break;
400 1.5.2.2 matt case ONFI_READ_START:
401 1.5.2.2 matt nandemulator_address_chip(self);
402 1.5.2.2 matt break;
403 1.5.2.2 matt case ONFI_BLOCK_ERASE_START:
404 1.5.2.2 matt page = nandemulator_address_to_page(self);
405 1.5.2.2 matt offset = sc->sc_page_size * page;
406 1.5.2.2 matt
407 1.5.2.2 matt KASSERT(offset %
408 1.5.2.2 matt (sc->sc_block_size * sc->sc_page_size) == 0);
409 1.5.2.2 matt
410 1.5.2.2 matt KASSERT(offset < sc->sc_device_size);
411 1.5.2.2 matt
412 1.5.2.2 matt if (offset >= sc->sc_device_size) {
413 1.5.2.2 matt aprint_error_dev(self, "address > device size!\n");
414 1.5.2.2 matt } else {
415 1.5.2.2 matt size_t addr =
416 1.5.2.2 matt nandemulator_page_to_backend_offset(self, page);
417 1.5.2.2 matt
418 1.5.2.2 matt size_t blocklen =
419 1.5.2.2 matt sc->sc_block_size *
420 1.5.2.2 matt (sc->sc_page_size + sc->sc_spare_size);
421 1.5.2.2 matt
422 1.5.2.2 matt KASSERT(addr < sc->sc_backend_size);
423 1.5.2.2 matt uint8_t *block = sc->sc_backend + addr;
424 1.5.2.2 matt
425 1.5.2.2 matt DPRINTF(("erasing block at 0x%jx\n",
426 1.5.2.2 matt (uintmax_t )offset));
427 1.5.2.2 matt
428 1.5.2.2 matt memset(block, 0xff, blocklen);
429 1.5.2.2 matt }
430 1.5.2.2 matt sc->sc_io_len = 0;
431 1.5.2.2 matt break;
432 1.5.2.2 matt default:
433 1.5.2.2 matt aprint_error_dev(self,
434 1.5.2.2 matt "invalid nand command (0x%hhx)\n", command);
435 1.5.2.2 matt KASSERT(false);
436 1.5.2.2 matt sc->sc_io_len = 0;
437 1.5.2.2 matt }
438 1.5.2.2 matt };
439 1.5.2.2 matt
440 1.5.2.2 matt static void
441 1.5.2.2 matt nandemulator_address(device_t self, uint8_t address)
442 1.5.2.2 matt {
443 1.5.2.2 matt struct nandemulator_softc *sc = device_private(self);
444 1.5.2.2 matt
445 1.5.2.2 matt DPRINTF(("nandemulator_address: %hhx\n", address));
446 1.5.2.2 matt
447 1.5.2.2 matt /**
448 1.5.2.2 matt * we have to handle read id/parameter page here,
449 1.5.2.2 matt * as we can read right after giving the address.
450 1.5.2.2 matt */
451 1.5.2.2 matt switch (sc->sc_command) {
452 1.5.2.2 matt case ONFI_READ_ID:
453 1.5.2.2 matt if (address == 0x00) {
454 1.5.2.2 matt sc->sc_io_len = 2;
455 1.5.2.2 matt sc->sc_io_pointer = sc->sc_ids;
456 1.5.2.2 matt } else if (address == 0x20) {
457 1.5.2.2 matt sc->sc_io_len = 4;
458 1.5.2.2 matt sc->sc_io_pointer = sc->sc_onfi;
459 1.5.2.2 matt } else {
460 1.5.2.2 matt sc->sc_io_len = 0;
461 1.5.2.2 matt }
462 1.5.2.2 matt break;
463 1.5.2.2 matt case ONFI_READ_PARAMETER_PAGE:
464 1.5.2.2 matt if (address == 0x00) {
465 1.5.2.2 matt sc->sc_io_len = sizeof(struct onfi_parameter_page) * 4;
466 1.5.2.2 matt sc->sc_io_pointer = (uint8_t *)sc->sc_parameter_page;
467 1.5.2.2 matt } else {
468 1.5.2.2 matt sc->sc_io_len = 0;
469 1.5.2.2 matt }
470 1.5.2.2 matt break;
471 1.5.2.2 matt case ONFI_PAGE_PROGRAM:
472 1.5.2.2 matt sc->sc_address <<= 8;
473 1.5.2.2 matt sc->sc_address |= address;
474 1.5.2.2 matt sc->sc_address_counter++;
475 1.5.2.2 matt
476 1.5.2.2 matt if (sc->sc_address_counter ==
477 1.5.2.2 matt sc->sc_column_cycles + sc->sc_row_cycles) {
478 1.5.2.2 matt nandemulator_address_chip(self);
479 1.5.2.2 matt }
480 1.5.2.2 matt break;
481 1.5.2.2 matt default:
482 1.5.2.2 matt sc->sc_address <<= 8;
483 1.5.2.2 matt sc->sc_address |= address;
484 1.5.2.2 matt sc->sc_address_counter++;
485 1.5.2.2 matt }
486 1.5.2.2 matt };
487 1.5.2.2 matt
488 1.5.2.2 matt static void
489 1.5.2.2 matt nandemulator_busy(device_t self)
490 1.5.2.2 matt {
491 1.5.2.2 matt #ifdef NANDEMULATOR_DELAYS
492 1.5.2.2 matt struct nandemulator_softc *sc = device_private(self);
493 1.5.2.2 matt
494 1.5.2.2 matt /* do some delay depending on command */
495 1.5.2.2 matt switch (sc->sc_command) {
496 1.5.2.2 matt case ONFI_PAGE_PROGRAM_START:
497 1.5.2.2 matt case ONFI_BLOCK_ERASE_START:
498 1.5.2.2 matt DELAY(10);
499 1.5.2.2 matt break;
500 1.5.2.2 matt case ONFI_READ_START:
501 1.5.2.2 matt default:
502 1.5.2.2 matt DELAY(1);
503 1.5.2.2 matt }
504 1.5.2.2 matt #endif
505 1.5.2.2 matt }
506 1.5.2.2 matt
507 1.5.2.2 matt static void
508 1.5.2.2 matt nandemulator_read_1(device_t self, uint8_t *data)
509 1.5.2.2 matt {
510 1.5.2.2 matt struct nandemulator_softc *sc = device_private(self);
511 1.5.2.2 matt
512 1.5.2.2 matt KASSERT(sc->sc_io_len > 0);
513 1.5.2.2 matt
514 1.5.2.2 matt if (sc->sc_io_len > 0) {
515 1.5.2.2 matt *data = *sc->sc_io_pointer;
516 1.5.2.2 matt
517 1.5.2.2 matt sc->sc_io_pointer++;
518 1.5.2.2 matt sc->sc_io_len--;
519 1.5.2.2 matt } else {
520 1.5.2.2 matt aprint_error_dev(self, "reading byte from invalid location\n");
521 1.5.2.2 matt *data = 0xff;
522 1.5.2.2 matt }
523 1.5.2.2 matt }
524 1.5.2.2 matt
525 1.5.2.2 matt static void
526 1.5.2.2 matt nandemulator_write_1(device_t self, uint8_t data)
527 1.5.2.2 matt {
528 1.5.2.2 matt struct nandemulator_softc *sc = device_private(self);
529 1.5.2.2 matt
530 1.5.2.2 matt KASSERT(sc->sc_register_writable);
531 1.5.2.2 matt
532 1.5.2.2 matt if (!sc->sc_register_writable) {
533 1.5.2.2 matt aprint_error_dev(self,
534 1.5.2.2 matt "trying to write read only location without effect\n");
535 1.5.2.2 matt return;
536 1.5.2.2 matt }
537 1.5.2.2 matt
538 1.5.2.2 matt KASSERT(sc->sc_io_len > 0);
539 1.5.2.2 matt
540 1.5.2.2 matt if (sc->sc_io_len > 0) {
541 1.5.2.2 matt *sc->sc_io_pointer = data;
542 1.5.2.2 matt
543 1.5.2.2 matt sc->sc_io_pointer++;
544 1.5.2.2 matt sc->sc_io_len--;
545 1.5.2.2 matt } else {
546 1.5.2.2 matt aprint_error_dev(self, "write to invalid location\n");
547 1.5.2.2 matt }
548 1.5.2.2 matt }
549 1.5.2.2 matt
550 1.5.2.2 matt static void
551 1.5.2.2 matt nandemulator_read_2(device_t self, uint16_t *data)
552 1.5.2.2 matt {
553 1.5.2.2 matt struct nandemulator_softc *sc = device_private(self);
554 1.5.2.2 matt
555 1.5.2.2 matt KASSERT(sc->sc_buswidth == NANDEMULATOR_16BIT);
556 1.5.2.2 matt
557 1.5.2.2 matt if (sc->sc_buswidth != NANDEMULATOR_16BIT) {
558 1.5.2.2 matt aprint_error_dev(self,
559 1.5.2.2 matt "trying to read a word on an 8bit chip\n");
560 1.5.2.2 matt return;
561 1.5.2.2 matt }
562 1.5.2.2 matt
563 1.5.2.2 matt KASSERT(sc->sc_io_len > 1);
564 1.5.2.2 matt
565 1.5.2.2 matt if (sc->sc_io_len > 1) {
566 1.5.2.2 matt *data = *(uint16_t *)sc->sc_io_pointer;
567 1.5.2.2 matt
568 1.5.2.2 matt sc->sc_io_pointer += 2;
569 1.5.2.2 matt sc->sc_io_len -= 2;
570 1.5.2.2 matt } else {
571 1.5.2.2 matt aprint_error_dev(self, "reading word from invalid location\n");
572 1.5.2.2 matt *data = 0xffff;
573 1.5.2.2 matt }
574 1.5.2.2 matt }
575 1.5.2.2 matt
576 1.5.2.2 matt static void
577 1.5.2.2 matt nandemulator_write_2(device_t self, uint16_t data)
578 1.5.2.2 matt {
579 1.5.2.2 matt struct nandemulator_softc *sc = device_private(self);
580 1.5.2.2 matt
581 1.5.2.2 matt KASSERT(sc->sc_register_writable);
582 1.5.2.2 matt
583 1.5.2.2 matt if (!sc->sc_register_writable) {
584 1.5.2.2 matt aprint_error_dev(self,
585 1.5.2.2 matt "trying to write read only location without effect\n");
586 1.5.2.2 matt return;
587 1.5.2.2 matt }
588 1.5.2.2 matt
589 1.5.2.2 matt KASSERT(sc->sc_buswidth == NANDEMULATOR_16BIT);
590 1.5.2.2 matt
591 1.5.2.2 matt if (sc->sc_buswidth != NANDEMULATOR_16BIT) {
592 1.5.2.2 matt aprint_error_dev(self,
593 1.5.2.2 matt "trying to write a word to an 8bit chip");
594 1.5.2.2 matt return;
595 1.5.2.2 matt }
596 1.5.2.2 matt
597 1.5.2.2 matt KASSERT(sc->sc_io_len > 1);
598 1.5.2.2 matt
599 1.5.2.2 matt if (sc->sc_io_len > 1) {
600 1.5.2.2 matt *(uint16_t *)sc->sc_io_pointer = data;
601 1.5.2.2 matt
602 1.5.2.2 matt sc->sc_io_pointer += 2;
603 1.5.2.2 matt sc->sc_io_len -= 2;
604 1.5.2.2 matt } else {
605 1.5.2.2 matt aprint_error_dev(self, "writing to invalid location");
606 1.5.2.2 matt }
607 1.5.2.2 matt }
608 1.5.2.2 matt
609 1.5.2.2 matt static void
610 1.5.2.2 matt nandemulator_read_buf_1(device_t self, void *buf, size_t len)
611 1.5.2.2 matt {
612 1.5.2.2 matt uint8_t *addr;
613 1.5.2.2 matt
614 1.5.2.2 matt KASSERT(buf != NULL);
615 1.5.2.2 matt KASSERT(len >= 1);
616 1.5.2.2 matt
617 1.5.2.2 matt addr = buf;
618 1.5.2.2 matt while (len > 0) {
619 1.5.2.2 matt nandemulator_read_1(self, addr);
620 1.5.2.2 matt addr++, len--;
621 1.5.2.2 matt }
622 1.5.2.2 matt }
623 1.5.2.2 matt
624 1.5.2.2 matt static void
625 1.5.2.2 matt nandemulator_read_buf_2(device_t self, void *buf, size_t len)
626 1.5.2.2 matt {
627 1.5.2.2 matt uint16_t *addr;
628 1.5.2.2 matt
629 1.5.2.2 matt KASSERT(buf != NULL);
630 1.5.2.2 matt KASSERT(len >= 2);
631 1.5.2.2 matt KASSERT(!(len & 0x01));
632 1.5.2.2 matt
633 1.5.2.2 matt addr = buf;
634 1.5.2.2 matt len /= 2;
635 1.5.2.2 matt while (len > 0) {
636 1.5.2.2 matt nandemulator_read_2(self, addr);
637 1.5.2.2 matt addr++, len--;
638 1.5.2.2 matt }
639 1.5.2.2 matt }
640 1.5.2.2 matt
641 1.5.2.2 matt static void
642 1.5.2.2 matt nandemulator_write_buf_1(device_t self, const void *buf, size_t len)
643 1.5.2.2 matt {
644 1.5.2.2 matt const uint8_t *addr;
645 1.5.2.2 matt
646 1.5.2.2 matt KASSERT(buf != NULL);
647 1.5.2.2 matt KASSERT(len >= 1);
648 1.5.2.2 matt
649 1.5.2.2 matt addr = buf;
650 1.5.2.2 matt while (len > 0) {
651 1.5.2.2 matt nandemulator_write_1(self, *addr);
652 1.5.2.2 matt addr++, len--;
653 1.5.2.2 matt }
654 1.5.2.2 matt }
655 1.5.2.2 matt
656 1.5.2.2 matt static void
657 1.5.2.2 matt nandemulator_write_buf_2(device_t self, const void *buf, size_t len)
658 1.5.2.2 matt {
659 1.5.2.2 matt const uint16_t *addr;
660 1.5.2.2 matt
661 1.5.2.2 matt KASSERT(buf != NULL);
662 1.5.2.2 matt KASSERT(len >= 2);
663 1.5.2.2 matt KASSERT(!(len & 0x01));
664 1.5.2.2 matt
665 1.5.2.2 matt addr = buf;
666 1.5.2.2 matt len /= 2;
667 1.5.2.2 matt while (len > 0) {
668 1.5.2.2 matt nandemulator_write_2(self, *addr);
669 1.5.2.2 matt addr++, len--;
670 1.5.2.2 matt }
671 1.5.2.2 matt }
672 1.5.2.2 matt
673 1.5.2.2 matt static size_t
674 1.5.2.2 matt nandemulator_address_to_page(device_t self)
675 1.5.2.2 matt {
676 1.5.2.2 matt struct nandemulator_softc *sc = device_private(self);
677 1.5.2.2 matt uint64_t address, offset;
678 1.5.2.2 matt int i;
679 1.5.2.2 matt
680 1.5.2.2 matt address = htole64(sc->sc_address);
681 1.5.2.2 matt address &= sc->sc_row_mask;
682 1.5.2.2 matt
683 1.5.2.2 matt offset = 0;
684 1.5.2.2 matt for (i = 0; i < sc->sc_row_cycles; i++) {
685 1.5.2.2 matt offset <<= 8;
686 1.5.2.2 matt offset |= (address & 0xff);
687 1.5.2.2 matt address >>= 8;
688 1.5.2.2 matt }
689 1.5.2.2 matt
690 1.5.2.2 matt return le64toh(offset);
691 1.5.2.2 matt }
692 1.5.2.2 matt
693 1.5.2.2 matt static size_t
694 1.5.2.2 matt nandemulator_column_address_to_subpage(device_t self)
695 1.5.2.2 matt {
696 1.5.2.2 matt struct nandemulator_softc *sc = device_private(self);
697 1.5.2.2 matt uint64_t address, offset;
698 1.5.2.2 matt int i;
699 1.5.2.2 matt
700 1.5.2.2 matt address = htole64(sc->sc_address);
701 1.5.2.2 matt address >>= (8 * sc->sc_row_cycles);
702 1.5.2.2 matt
703 1.5.2.2 matt offset = 0;
704 1.5.2.2 matt for (i = 0; i < sc->sc_column_cycles; i++) {
705 1.5.2.2 matt offset <<= 8;
706 1.5.2.2 matt offset |= (address & 0xff);
707 1.5.2.2 matt address >>= 8;
708 1.5.2.2 matt }
709 1.5.2.2 matt
710 1.5.2.2 matt if (sc->sc_buswidth == NANDEMULATOR_16BIT)
711 1.5.2.2 matt return (size_t )le64toh(offset << 1);
712 1.5.2.2 matt else
713 1.5.2.2 matt return (size_t )le64toh(offset);
714 1.5.2.2 matt }
715 1.5.2.2 matt
716 1.5.2.2 matt static size_t
717 1.5.2.2 matt nandemulator_page_to_backend_offset(device_t self, size_t page)
718 1.5.2.2 matt {
719 1.5.2.2 matt struct nandemulator_softc *sc = device_private(self);
720 1.5.2.2 matt
721 1.5.2.2 matt return (sc->sc_page_size + sc->sc_spare_size) * page;
722 1.5.2.2 matt }
723 1.5.2.2 matt
724 1.5.2.2 matt #ifdef _MODULE
725 1.5.2.2 matt
726 1.5.2.2 matt MODULE(MODULE_CLASS_DRIVER, nandemulator, "nand");
727 1.5.2.2 matt
728 1.5.2.2 matt static const struct cfiattrdata nandbuscf_iattrdata = {
729 1.5.2.2 matt "nandbus", 0, { { NULL, NULL, 0 }, }
730 1.5.2.2 matt };
731 1.5.2.2 matt static const struct cfiattrdata * const nandemulator_attrs[] = {
732 1.5.2.2 matt &nandbuscf_iattrdata, NULL
733 1.5.2.2 matt };
734 1.5.2.2 matt
735 1.5.2.2 matt CFDRIVER_DECL(nandemulator, DV_DULL, nandemulator_attrs);
736 1.5.2.2 matt extern struct cfattach nandemulator_ca;
737 1.5.2.2 matt static int nandemulatorloc[] = { -1, -1 };
738 1.5.2.2 matt
739 1.5.2.2 matt static struct cfdata nandemulator_cfdata[] = {
740 1.5.2.2 matt {
741 1.5.2.2 matt .cf_name = "nandemulator",
742 1.5.2.2 matt .cf_atname = "nandemulator",
743 1.5.2.2 matt .cf_unit = 0,
744 1.5.2.2 matt .cf_fstate = FSTATE_STAR,
745 1.5.2.2 matt .cf_loc = nandemulatorloc,
746 1.5.2.2 matt .cf_flags = 0,
747 1.5.2.2 matt .cf_pspec = NULL,
748 1.5.2.2 matt },
749 1.5.2.2 matt { NULL, NULL, 0, 0, NULL, 0, NULL }
750 1.5.2.2 matt };
751 1.5.2.2 matt
752 1.5.2.2 matt static int
753 1.5.2.2 matt nandemulator_modcmd(modcmd_t cmd, void *arg)
754 1.5.2.2 matt {
755 1.5.2.2 matt int error;
756 1.5.2.2 matt
757 1.5.2.2 matt switch (cmd) {
758 1.5.2.2 matt case MODULE_CMD_INIT:
759 1.5.2.2 matt error = config_cfdriver_attach(&nandemulator_cd);
760 1.5.2.2 matt if (error) {
761 1.5.2.2 matt return error;
762 1.5.2.2 matt }
763 1.5.2.2 matt
764 1.5.2.2 matt error = config_cfattach_attach(nandemulator_cd.cd_name,
765 1.5.2.2 matt &nandemulator_ca);
766 1.5.2.2 matt if (error) {
767 1.5.2.2 matt config_cfdriver_detach(&nandemulator_cd);
768 1.5.2.2 matt aprint_error("%s: unable to register cfattach\n",
769 1.5.2.2 matt nandemulator_cd.cd_name);
770 1.5.2.2 matt
771 1.5.2.2 matt return error;
772 1.5.2.2 matt }
773 1.5.2.2 matt
774 1.5.2.2 matt error = config_cfdata_attach(nandemulator_cfdata, 1);
775 1.5.2.2 matt if (error) {
776 1.5.2.2 matt config_cfattach_detach(nandemulator_cd.cd_name,
777 1.5.2.2 matt &nandemulator_ca);
778 1.5.2.2 matt config_cfdriver_detach(&nandemulator_cd);
779 1.5.2.2 matt aprint_error("%s: unable to register cfdata\n",
780 1.5.2.2 matt nandemulator_cd.cd_name);
781 1.5.2.2 matt
782 1.5.2.2 matt return error;
783 1.5.2.2 matt }
784 1.5.2.2 matt
785 1.5.2.2 matt (void)config_attach_pseudo(nandemulator_cfdata);
786 1.5.2.2 matt
787 1.5.2.2 matt return 0;
788 1.5.2.2 matt
789 1.5.2.2 matt case MODULE_CMD_FINI:
790 1.5.2.2 matt error = config_cfdata_detach(nandemulator_cfdata);
791 1.5.2.2 matt if (error) {
792 1.5.2.2 matt return error;
793 1.5.2.2 matt }
794 1.5.2.2 matt
795 1.5.2.2 matt config_cfattach_detach(nandemulator_cd.cd_name,
796 1.5.2.2 matt &nandemulator_ca);
797 1.5.2.2 matt config_cfdriver_detach(&nandemulator_cd);
798 1.5.2.2 matt
799 1.5.2.2 matt return 0;
800 1.5.2.2 matt
801 1.5.2.2 matt case MODULE_CMD_AUTOUNLOAD:
802 1.5.2.2 matt /* prevent auto-unload */
803 1.5.2.2 matt return EBUSY;
804 1.5.2.2 matt
805 1.5.2.2 matt default:
806 1.5.2.2 matt return ENOTTY;
807 1.5.2.2 matt }
808 1.5.2.2 matt }
809 1.5.2.2 matt
810 1.5.2.2 matt #endif
811