Home | History | Annotate | Line # | Download | only in nand
      1 /*	$NetBSD: nandemulator.c,v 1.10 2023/05/10 00:11:16 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2011 Department of Software Engineering,
      5  *		      University of Szeged, Hungary
      6  * Copyright (c) 2011 Adam Hoka <ahoka (at) 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.10 2023/05/10 00:11:16 riastradh Exp $");
     36 
     37 /* XXX this code likely needs work */
     38 
     39 #include <sys/param.h>
     40 #include <sys/device.h>
     41 #include <sys/conf.h>
     42 #include <sys/kmem.h>
     43 #include <sys/kernel.h>
     44 
     45 #include "nandemulator.h"
     46 
     47 #include <dev/nand/nand.h>
     48 #include <dev/nand/onfi.h>
     49 #include <dev/nand/nand_crc.h>
     50 
     51 #include "ioconf.h"
     52 
     53 extern struct cfdriver nandemulator_cd;
     54 
     55 static int nandemulator_match(device_t, cfdata_t, void *);
     56 static void nandemulator_attach(device_t, device_t, void *);
     57 static int nandemulator_detach(device_t, int);
     58 
     59 static void nandemulator_device_reset(device_t);
     60 static void nandemulator_command(device_t, uint8_t);
     61 static void nandemulator_address(device_t, uint8_t);
     62 static void nandemulator_busy(device_t);
     63 static void nandemulator_read_1(device_t, uint8_t *);
     64 static void nandemulator_write_1(device_t, uint8_t);
     65 static void nandemulator_read_2(device_t, uint16_t *);
     66 static void nandemulator_write_2(device_t, uint16_t);
     67 static void nandemulator_read_buf_1(device_t, void *, size_t);
     68 static void nandemulator_read_buf_2(device_t, void *, size_t);
     69 static void nandemulator_write_buf_1(device_t, const void *, size_t);
     70 static void nandemulator_write_buf_2(device_t, const void *, size_t);
     71 
     72 static size_t nandemulator_address_to_page(device_t);
     73 static size_t nandemulator_page_to_backend_offset(device_t, size_t);
     74 static size_t nandemulator_column_address_to_subpage(device_t);
     75 /*
     76 #define NANDEMULATOR_DEBUG 1
     77 
     78 #ifdef NANDEMULATOR_DEBUG
     79 #warning debug enabled
     80 #define DPRINTF(x)	if (nandemulatordebug) printf x
     81 #define DPRINTFN(n,x)	if (nandemulatordebug>(n)) printf x
     82 #else
     83 #error no debug
     84 #define DPRINTF(x)
     85 #define DPRINTFN(n,x)
     86 #endif
     87 
     88 #ifdef NANDEMULATOR_DEBUG
     89 int	nandemulatordebug = NANDEMULATOR_DEBUG;
     90 #endif
     91 */
     92 
     93 extern int nanddebug;
     94 
     95 enum {
     96 	NANDEMULATOR_8BIT,
     97 	NANDEMULATOR_16BIT
     98 };
     99 
    100 struct nandemulator_softc {
    101 	device_t		sc_dev;
    102 	device_t		sc_nanddev;
    103 
    104 	int			sc_buswidth;
    105 
    106 	struct nand_interface	sc_nand_if;
    107 
    108 	uint8_t			sc_command;
    109 	size_t			sc_io_len;
    110 	uint8_t			*sc_io_pointer;
    111 	uint64_t		sc_address;
    112 
    113 	uint8_t			*sc_backend;
    114 	size_t			sc_backend_size;
    115 	size_t			sc_device_size;
    116 	bool			sc_register_writable;
    117 
    118 	uint8_t			sc_status_register;
    119 	uint8_t			sc_ids[2];
    120 	uint8_t			sc_onfi[4];
    121 
    122 	size_t			sc_page_size;
    123 	size_t			sc_block_size;
    124 	size_t			sc_spare_size;
    125 	size_t			sc_lun_size;
    126 	uint8_t			sc_row_cycles;
    127 	uint8_t			sc_column_cycles;
    128 	uint64_t		sc_row_mask;
    129 
    130 	int			sc_address_counter;
    131 
    132 	struct onfi_parameter_page	*sc_parameter_page;
    133 };
    134 
    135 CFATTACH_DECL_NEW(nandemulator, sizeof(struct nandemulator_softc),
    136     nandemulator_match, nandemulator_attach, nandemulator_detach, NULL);
    137 
    138 void
    139 nandemulatorattach(int n)
    140 {
    141 	int i, err;
    142 	cfdata_t cf;
    143 
    144 	aprint_debug("nandemulator: requested %d units\n", n);
    145 
    146 	err = config_cfattach_attach(nandemulator_cd.cd_name,
    147 	    &nandemulator_ca);
    148 	if (err) {
    149 		aprint_error("%s: couldn't register cfattach: %d\n",
    150 		    nandemulator_cd.cd_name, err);
    151 		config_cfdriver_detach(&nandemulator_cd);
    152 		return;
    153 	}
    154 	for (i = 0; i < n; i++) {
    155 		cf = kmem_alloc(sizeof(struct cfdata), KM_SLEEP);
    156 		cf->cf_name = nandemulator_cd.cd_name;
    157 		cf->cf_atname = nandemulator_cd.cd_name;
    158 		cf->cf_unit = i;
    159 		cf->cf_fstate = FSTATE_STAR;
    160 
    161 		(void)config_attach_pseudo(cf);
    162 	}
    163 }
    164 
    165 /* ARGSUSED */
    166 static int
    167 nandemulator_match(device_t parent, cfdata_t match, void *aux)
    168 {
    169 	/* pseudo device, always attaches */
    170 	return 1;
    171 }
    172 
    173 static void
    174 nandemulator_attach(device_t parent, device_t self, void *aux)
    175 {
    176 	struct nandemulator_softc *sc = device_private(self);
    177 	int i;
    178 
    179 	aprint_normal_dev(self, "NAND emulator\n");
    180 
    181 	sc->sc_dev = self;
    182 
    183 	nand_init_interface(&sc->sc_nand_if);
    184 
    185 	sc->sc_nand_if.command = &nandemulator_command;
    186 	sc->sc_nand_if.address = &nandemulator_address;
    187 	sc->sc_nand_if.read_buf_1 = &nandemulator_read_buf_1;
    188 	sc->sc_nand_if.read_buf_2 = &nandemulator_read_buf_2;
    189 	sc->sc_nand_if.read_1 = &nandemulator_read_1;
    190 	sc->sc_nand_if.read_2 = &nandemulator_read_2;
    191 	sc->sc_nand_if.write_buf_1 = &nandemulator_write_buf_1;
    192 	sc->sc_nand_if.write_buf_2 = &nandemulator_write_buf_2;
    193 	sc->sc_nand_if.write_1 = &nandemulator_write_1;
    194 	sc->sc_nand_if.write_2 = &nandemulator_write_2;
    195 	sc->sc_nand_if.busy = &nandemulator_busy;
    196 
    197 	sc->sc_nand_if.ecc.necc_code_size = 3;
    198 	sc->sc_nand_if.ecc.necc_block_size = 256;
    199 
    200 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, NULL))
    201 		aprint_error_dev(sc->sc_dev,
    202 		    "couldn't establish power handler\n");
    203 
    204 	sc->sc_buswidth = NANDEMULATOR_16BIT;	/* 16bit for now */
    205 
    206 	/* hardcode these now, make it configurable later */
    207 	sc->sc_device_size = 32 * 1024 * 1024; /* 32MB */
    208 	sc->sc_page_size = 2048;
    209 	sc->sc_block_size = 64;
    210 	sc->sc_lun_size =
    211 	    sc->sc_device_size / (sc->sc_page_size * sc->sc_block_size);
    212 	KASSERT(sc->sc_device_size %
    213 	    (sc->sc_page_size * sc->sc_block_size) == 0);
    214 	sc->sc_spare_size = 64;
    215 
    216 	sc->sc_column_cycles = 2;
    217 	sc->sc_row_cycles = 3;
    218 
    219 	/* init the emulator data structures */
    220 	sc->sc_backend_size =
    221 	    sc->sc_device_size +
    222 	    sc->sc_device_size / sc->sc_page_size * sc->sc_spare_size;
    223 
    224 	sc->sc_backend = kmem_alloc(sc->sc_backend_size, KM_SLEEP);
    225 	memset(sc->sc_backend, 0xff, sc->sc_backend_size);
    226 
    227 	sc->sc_parameter_page =
    228 	    kmem_zalloc(sizeof(struct onfi_parameter_page) * 4, KM_SLEEP);
    229 
    230 	struct onfi_parameter_page *opp;
    231 	uint8_t sig[4] = { 'O', 'N', 'F', 'I' };
    232 
    233 	for (i = 0; i < 4; i++) {
    234 		opp = &sc->sc_parameter_page[i];
    235 
    236 		opp->param_signature = htole32(*(uint32_t *)sig);
    237 		opp->param_pagesize = htole32(sc->sc_page_size);
    238 		opp->param_blocksize = htole32(sc->sc_block_size);
    239 		opp->param_sparesize = htole16(sc->sc_spare_size);
    240 		opp->param_lunsize = htole32(sc->sc_lun_size);
    241 		opp->param_numluns = 1;
    242 
    243 		opp->param_manufacturer_id = 0x00;
    244 		memcpy(opp->param_manufacturer,
    245 		    "NETBSD", strlen("NETBSD"));
    246 		memcpy(opp->param_model,
    247 		    "NANDEMULATOR", strlen("NANDEMULATOR"));
    248 
    249 		uint16_t features = ONFI_FEATURE_16BIT;
    250 		opp->param_features = htole16(features);
    251 
    252 		/* the lower 4 bits contain the row address cycles
    253 		 * the upper 4 bits contain the column address cycles
    254 		 */
    255 		opp->param_addr_cycles = sc->sc_row_cycles;
    256 		opp->param_addr_cycles |= (sc->sc_column_cycles << 4);
    257 
    258 		opp->param_integrity_crc = nand_crc16((uint8_t *)opp, 254);
    259 	}
    260 
    261 	sc->sc_ids[0] = 0x00;
    262 	sc->sc_ids[1] = 0x00;
    263 
    264 	sc->sc_onfi[0] = 'O';
    265 	sc->sc_onfi[1] = 'N';
    266 	sc->sc_onfi[2] = 'F';
    267 	sc->sc_onfi[3] = 'I';
    268 
    269 	sc->sc_row_mask = 0x00;
    270 	for (i = 0; i < sc->sc_row_cycles; i++) {
    271 		sc->sc_row_mask <<= 8;
    272 		sc->sc_row_mask |= 0xff;
    273 	}
    274 
    275 	nandemulator_device_reset(self);
    276 
    277 	sc->sc_nanddev = nand_attach_mi(&sc->sc_nand_if, sc->sc_dev);
    278 }
    279 
    280 static int
    281 nandemulator_detach(device_t self, int flags)
    282 {
    283 	struct nandemulator_softc *sc = device_private(self);
    284 	int error;
    285 
    286 	aprint_normal_dev(sc->sc_dev, "detaching emulator\n");
    287 
    288 	error = config_detach_children(self, flags);
    289 	if (error)
    290 		return error;
    291 
    292 	pmf_device_deregister(sc->sc_dev);
    293 	kmem_free(sc->sc_backend, sc->sc_backend_size);
    294 	kmem_free(sc->sc_parameter_page,
    295 	    sizeof(struct onfi_parameter_page) * 4);
    296 	return 0;
    297 }
    298 
    299 /**
    300  * bring the emulated device to a known state
    301  */
    302 static void
    303 nandemulator_device_reset(device_t self)
    304 {
    305 	struct nandemulator_softc *sc = device_private(self);
    306 
    307 	DPRINTF(("device reset\n"));
    308 
    309 	sc->sc_command = 0;
    310 	sc->sc_register_writable = false;
    311 	sc->sc_io_len = 0;
    312 	sc->sc_io_pointer = NULL;
    313 	sc->sc_address = 0;
    314 	sc->sc_address_counter = 0;
    315 
    316 	sc->sc_status_register = ONFI_STATUS_RDY | ONFI_STATUS_WP;
    317 }
    318 
    319 static void
    320 nandemulator_address_chip(device_t self)
    321 {
    322 	struct nandemulator_softc *sc = device_private(self);
    323 	size_t page, offset;
    324 
    325 	KASSERT(sc->sc_address_counter ==
    326 	    sc->sc_column_cycles + sc->sc_row_cycles);
    327 
    328 	if (sc->sc_address_counter !=
    329 	    sc->sc_column_cycles + sc->sc_row_cycles) {
    330 		aprint_error_dev(self, "incorrect number of address cycles\n");
    331 		aprint_error_dev(self, "cc: %d, rc: %d, ac: %d\n",
    332 		    sc->sc_column_cycles, sc->sc_row_cycles,
    333 		    sc->sc_address_counter);
    334 	}
    335 
    336 	page = nandemulator_address_to_page(self);
    337 	offset = sc->sc_page_size * page;
    338 
    339 	DPRINTF(("READ/PROGRAM; page: 0x%jx (row addr: 0x%jx)\n",
    340 		(uintmax_t )page,
    341 		(uintmax_t )offset));
    342 
    343 	KASSERT(offset < sc->sc_device_size);
    344 
    345 	if (offset >= sc->sc_device_size) {
    346 		aprint_error_dev(self, "address > device size!\n");
    347 		sc->sc_io_len = 0;
    348 	} else {
    349 		size_t addr =
    350 		    nandemulator_page_to_backend_offset(self, page);
    351 		size_t pageoff =
    352 		    nandemulator_column_address_to_subpage(self);
    353 
    354 		DPRINTF(("subpage: 0x%jx\n", (uintmax_t )pageoff));
    355 
    356 		KASSERT(pageoff <
    357 		    sc->sc_page_size + sc->sc_spare_size);
    358 		KASSERT(addr < sc->sc_backend_size);
    359 
    360 		sc->sc_io_pointer = sc->sc_backend + addr + pageoff;
    361 		sc->sc_io_len =
    362 		    sc->sc_page_size + sc->sc_spare_size - pageoff;
    363 	}
    364 }
    365 
    366 static void
    367 nandemulator_command(device_t self, uint8_t command)
    368 {
    369 	struct nandemulator_softc *sc = device_private(self);
    370 	size_t offset, page;
    371 
    372 	sc->sc_command = command;
    373 	sc->sc_register_writable = false;
    374 
    375 	DPRINTF(("nandemulator command: 0x%hhx\n", command));
    376 
    377 	switch (command) {
    378 	case ONFI_READ_STATUS:
    379 		sc->sc_io_pointer = &sc->sc_status_register;
    380 		sc->sc_io_len = 1;
    381 		break;
    382 	case ONFI_RESET:
    383 		nandemulator_device_reset(self);
    384 		break;
    385 	case ONFI_PAGE_PROGRAM:
    386 		sc->sc_register_writable = true;
    387 		/* FALLTHROUGH */
    388 	case ONFI_READ:
    389 	case ONFI_BLOCK_ERASE:
    390 		sc->sc_address_counter = 0;
    391 		/* FALLTHROUGH */
    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