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