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