Home | History | Annotate | Line # | Download | only in nor
nor.c revision 1.4.8.2
      1 /*	$NetBSD: nor.c,v 1.4.8.2 2011/12/27 17:35:48 matt 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 /* Common driver for NOR chips implementing the ONFI CFI specification */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: nor.c,v 1.4.8.2 2011/12/27 17:35:48 matt Exp $");
     38 
     39 #include "locators.h"
     40 #include "opt_nor.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/types.h>
     44 #include <sys/device.h>
     45 #include <sys/kmem.h>
     46 #include <sys/sysctl.h>
     47 #include <sys/atomic.h>
     48 
     49 #include <dev/flash/flash.h>
     50 #include <dev/flash/flash_io.h>
     51 #include <dev/nor/nor.h>
     52 
     53 
     54 static int nor_match(device_t, cfdata_t, void *);
     55 static void nor_attach(device_t, device_t, void *);
     56 static int nor_detach(device_t, int);
     57 static bool nor_shutdown(device_t, int);
     58 static int nor_print(void *, const char *);
     59 static int nor_search(device_t, cfdata_t, const int *, void *);
     60 
     61 /* flash interface implementation */
     62 static int nor_flash_isbad(device_t, flash_off_t, bool *);
     63 static int nor_flash_markbad(device_t, flash_off_t);
     64 static int nor_flash_write(device_t, flash_off_t, size_t, size_t *,
     65 	const u_char *);
     66 static int nor_flash_read(device_t, flash_off_t, size_t, size_t *, uint8_t *);
     67 static int nor_flash_erase_all(device_t);
     68 static int nor_flash_erase(device_t, struct flash_erase_instruction *);
     69 static int nor_flash_submit(device_t, buf_t *);
     70 
     71 /* default functions for driver development */
     72 static void nor_default_select(device_t, bool);
     73 static int  nor_default_read_page(device_t, flash_off_t, uint8_t *);
     74 static int  nor_default_program_page(device_t, flash_off_t, const uint8_t *);
     75 
     76 static int nor_scan_media(device_t, struct nor_chip *);
     77 
     78 CFATTACH_DECL_NEW(nor, sizeof(struct nor_softc),
     79     nor_match, nor_attach, nor_detach, NULL);
     80 
     81 #ifdef NOR_DEBUG
     82 int	nordebug = NOR_DEBUG;
     83 #endif
     84 
     85 int nor_cachesync_timeout = 1;
     86 int nor_cachesync_nodenum;
     87 
     88 struct flash_interface nor_flash_if = {
     89 	.type = FLASH_TYPE_NOR,
     90 
     91 	.read = nor_flash_read,
     92 	.write = nor_flash_write,
     93 	.erase = nor_flash_erase,
     94 	.block_isbad = nor_flash_isbad,
     95 	.block_markbad = nor_flash_markbad,
     96 
     97 	.submit = nor_flash_submit
     98 };
     99 
    100 #ifdef NOR_VERBOSE
    101 const struct nor_manufacturer nor_mfrs[] = {
    102 	{ NOR_MFR_AMD,		"AMD" },
    103 	{ NOR_MFR_FUJITSU,	"Fujitsu" },
    104 	{ NOR_MFR_RENESAS,	"Renesas" },
    105 	{ NOR_MFR_STMICRO,	"ST Micro" },
    106 	{ NOR_MFR_MICRON,	"Micron" },
    107 	{ NOR_MFR_NATIONAL,	"National" },
    108 	{ NOR_MFR_TOSHIBA,	"Toshiba" },
    109 	{ NOR_MFR_HYNIX,	"Hynix" },
    110 	{ NOR_MFR_SAMSUNG,	"Samsung" },
    111 	{ NOR_MFR_UNKNOWN,	"Unknown" }
    112 };
    113 
    114 static const char *
    115 nor_midtoname(int id)
    116 {
    117 	int i;
    118 
    119 	for (i = 0; nor_mfrs[i].id != 0; i++) {
    120 		if (nor_mfrs[i].id == id)
    121 			return nor_mfrs[i].name;
    122 	}
    123 
    124 	KASSERT(nor_mfrs[i].id == 0);
    125 
    126 	return nor_mfrs[i].name;
    127 }
    128 #endif
    129 
    130 /* ARGSUSED */
    131 static int
    132 nor_match(device_t parent, cfdata_t match, void *aux)
    133 {
    134 	/* pseudo device, always attaches */
    135 	return 1;
    136 }
    137 
    138 static void
    139 nor_attach(device_t parent, device_t self, void *aux)
    140 {
    141 	struct nor_softc * const sc = device_private(self);
    142 	struct nor_attach_args * const naa = aux;
    143 	struct nor_chip * const chip = &sc->sc_chip;
    144 
    145 	sc->sc_dev = self;
    146 	sc->sc_controller_dev = parent;
    147 	sc->sc_nor_if = naa->naa_nor_if;
    148 
    149 	aprint_naive("\n");
    150 	aprint_normal("\n");
    151 
    152 	if (nor_scan_media(self, chip))
    153 		return;
    154 
    155 	sc->sc_flash_if = nor_flash_if;
    156 	sc->sc_flash_if.erasesize = chip->nc_block_size;
    157 	sc->sc_flash_if.page_size = chip->nc_page_size;
    158 	sc->sc_flash_if.writesize = chip->nc_page_size;
    159 
    160 	/* allocate cache */
    161 #ifdef NOTYET
    162 	chip->nc_oob_cache = kmem_alloc(chip->nc_spare_size, KM_SLEEP);
    163 #endif
    164 	chip->nc_page_cache = kmem_alloc(chip->nc_page_size, KM_SLEEP);
    165 
    166 	mutex_init(&sc->sc_device_lock, MUTEX_DEFAULT, IPL_NONE);
    167 
    168 	if (flash_sync_thread_init(&sc->sc_flash_io, self, &sc->sc_flash_if)) {
    169 		goto error;
    170 	}
    171 
    172 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, nor_shutdown))
    173 		aprint_error_dev(sc->sc_dev,
    174 		    "couldn't establish power handler\n");
    175 
    176 #ifdef NOR_BBT
    177 	nor_bbt_init(self);
    178 	nor_bbt_scan(self);
    179 #endif
    180 
    181 	/*
    182 	 * Attach all our devices
    183 	 */
    184 	config_search_ia(nor_search, self, NULL, NULL);
    185 
    186 	return;
    187 
    188 error:
    189 #ifdef NOTET
    190 	kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
    191 #endif
    192 	kmem_free(chip->nc_page_cache, chip->nc_page_size);
    193 	mutex_destroy(&sc->sc_device_lock);
    194 }
    195 
    196 static int
    197 nor_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    198 {
    199 	struct nor_softc * const sc = device_private(parent);
    200 	struct nor_chip * const chip = &sc->sc_chip;
    201 	struct flash_attach_args faa;
    202 
    203 	faa.partinfo.part_offset = cf->cf_loc[FLASHBUSCF_OFFSET];
    204 
    205 	if (cf->cf_loc[FLASHBUSCF_SIZE] == 0) {
    206 		faa.partinfo.part_size =
    207 		    chip->nc_size - faa.partinfo.part_offset;
    208 	} else {
    209 		faa.partinfo.part_size = cf->cf_loc[FLASHBUSCF_SIZE];
    210 	}
    211 
    212 	if (cf->cf_loc[FLASHBUSCF_READONLY])
    213 		faa.partinfo.part_flags = FLASH_PART_READONLY;
    214 	else
    215 		faa.partinfo.part_flags = 0;
    216 
    217 	faa.flash_if = &sc->sc_flash_if;
    218 
    219 	if (config_match(parent, cf, &faa)) {
    220 		if (config_attach(parent, cf, &faa, nor_print) != NULL) {
    221 			return 0;
    222 		} else {
    223 			return 1;
    224 		}
    225 	}
    226 
    227 	return 1;
    228 }
    229 
    230 static int
    231 nor_detach(device_t self, int flags)
    232 {
    233 	struct nor_softc * const sc = device_private(self);
    234 	struct nor_chip * const chip = &sc->sc_chip;
    235 	int error = 0;
    236 
    237 	error = config_detach_children(self, flags);
    238 	if (error) {
    239 		return error;
    240 	}
    241 
    242 	flash_sync_thread_destroy(&sc->sc_flash_io);
    243 #ifdef NOR_BBT
    244 	nor_bbt_detach(self);
    245 #endif
    246 #ifdef NOTET
    247 	/* free oob cache */
    248 	kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
    249 #endif
    250 	kmem_free(chip->nc_page_cache, chip->nc_page_size);
    251 
    252 	mutex_destroy(&sc->sc_device_lock);
    253 
    254 	pmf_device_deregister(sc->sc_dev);
    255 
    256 	return error;
    257 }
    258 
    259 static int
    260 nor_print(void *aux, const char *pnp)
    261 {
    262 	if (pnp != NULL)
    263 		aprint_normal("nor at %s\n", pnp);
    264 
    265 	return UNCONF;
    266 }
    267 
    268 /* ask for a nor driver to attach to the controller */
    269 device_t
    270 nor_attach_mi(struct nor_interface * const nor_if, device_t parent)
    271 {
    272 	struct nor_attach_args arg;
    273 
    274 	KASSERT(nor_if != NULL);
    275 
    276 	if (nor_if->select == NULL)
    277 		nor_if->select = &nor_default_select;
    278 	if (nor_if->read_page == NULL)
    279 		nor_if->read_page = &nor_default_read_page;
    280 	if (nor_if->program_page == NULL)
    281 		nor_if->program_page = &nor_default_program_page;
    282 
    283 	arg.naa_nor_if = nor_if;
    284 
    285 	device_t dev = config_found_ia(parent, "norbus", &arg, nor_print);
    286 
    287 	return dev;
    288 }
    289 
    290 static void
    291 nor_default_select(device_t self, bool n)
    292 {
    293 	/* do nothing */
    294 	return;
    295 }
    296 
    297 static int
    298 nor_flash_submit(device_t self, buf_t * const bp)
    299 {
    300 	struct nor_softc * const sc = device_private(self);
    301 
    302 	return flash_io_submit(&sc->sc_flash_io, bp);
    303 }
    304 
    305 
    306 /* default everything to reasonable values, to ease future api changes */
    307 void
    308 nor_init_interface(struct nor_interface * const nor_if)
    309 {
    310 	nor_if->select = &nor_default_select;
    311 	nor_if->read_1 = NULL;
    312 	nor_if->read_2 = NULL;
    313 	nor_if->read_4 = NULL;
    314 	nor_if->read_buf_1 = NULL;
    315 	nor_if->read_buf_2 = NULL;
    316 	nor_if->read_buf_4 = NULL;
    317 	nor_if->write_1 = NULL;
    318 	nor_if->write_2 = NULL;
    319 	nor_if->write_4 = NULL;
    320 	nor_if->write_buf_1 = NULL;
    321 	nor_if->write_buf_2 = NULL;
    322 	nor_if->write_buf_4 = NULL;
    323 	nor_if->busy = NULL;
    324 }
    325 
    326 #ifdef NOTYET
    327 /* handle quirks here */
    328 static void
    329 nor_quirks(device_t self, struct nor_chip * const chip)
    330 {
    331 	/* this is an example only! */
    332 	switch (chip->nc_manf_id) {
    333 	case NOR_MFR_SAMSUNG:
    334 		if (chip->nc_dev_id == 0x00) {
    335 			/* do something only samsung chips need */
    336 			/* or */
    337 			/* chip->nc_quirks |= NC_QUIRK_NO_READ_START */
    338 		}
    339 	}
    340 
    341 	return;
    342 }
    343 #endif
    344 
    345 /**
    346  * scan media to determine the chip's properties
    347  * this function resets the device
    348  */
    349 static int
    350 nor_scan_media(device_t self, struct nor_chip * const chip)
    351 {
    352 	struct nor_softc * const sc = device_private(self);
    353 	char pbuf[3][sizeof("XXXX MB")];
    354 
    355 	KASSERT(sc->sc_nor_if != NULL);
    356 	KASSERT(sc->sc_nor_if->scan_media != NULL);
    357 	int error = sc->sc_nor_if->scan_media(self, chip);
    358 	if (error != 0)
    359 		return error;
    360 
    361 #ifdef NOR_VERBOSE
    362 	aprint_normal_dev(self,
    363 	    "manufacturer id: 0x%.4x (%s), device id: 0x%.4x\n",
    364 	    chip->nc_manf_id,
    365 	    nor_midtoname(chip->nc_manf_id),
    366 	    chip->nc_dev_id);
    367 #endif
    368 
    369 	format_bytes(pbuf[0], sizeof(pbuf[0]), chip->nc_page_size);
    370 	format_bytes(pbuf[1], sizeof(pbuf[1]), chip->nc_spare_size);
    371 	format_bytes(pbuf[2], sizeof(pbuf[2]), chip->nc_block_size);
    372 	aprint_normal_dev(self,
    373 	    "page size: %s, spare size: %s, block size: %s\n",
    374 	    pbuf[0], pbuf[1], pbuf[2]);
    375 
    376 	format_bytes(pbuf[0], sizeof(pbuf[0]), chip->nc_size);
    377 	aprint_normal_dev(self,
    378 	    "LUN size: %" PRIu32 " blocks, LUNs: %" PRIu8
    379 	    ", total storage size: %s\n",
    380 	    chip->nc_lun_blocks, chip->nc_num_luns, pbuf[0]);
    381 
    382 #ifdef NOTYET
    383 	/* XXX does this apply to nor? */
    384 	/*
    385 	 * calculate badblock marker offset in oob
    386 	 * we try to be compatible with linux here
    387 	 */
    388 	if (chip->nc_page_size > 512)
    389 		chip->nc_badmarker_offs = 0;
    390 	else
    391 		chip->nc_badmarker_offs = 5;
    392 #endif
    393 
    394 	/* Calculate page shift and mask */
    395 	chip->nc_page_shift = ffs(chip->nc_page_size) - 1;
    396 	chip->nc_page_mask = ~(chip->nc_page_size - 1);
    397 	/* same for block */
    398 	chip->nc_block_shift = ffs(chip->nc_block_size) - 1;
    399 	chip->nc_block_mask = ~(chip->nc_block_size - 1);
    400 
    401 #ifdef NOTYET
    402 	/* look for quirks here if needed in future */
    403 	nor_quirks(self, chip);
    404 #endif
    405 
    406 	return 0;
    407 }
    408 
    409 /* ARGSUSED */
    410 static bool
    411 nor_shutdown(device_t self, int howto)
    412 {
    413 	return true;
    414 }
    415 
    416 /* implementation of the block device API */
    417 
    418 /* read a page, default implementation */
    419 static int
    420 nor_default_read_page(device_t self, flash_off_t offset, uint8_t * const data)
    421 {
    422 	struct nor_softc * const sc = device_private(self);
    423 	struct nor_chip * const chip = &sc->sc_chip;
    424 
    425 	/*
    426 	 * access by specified access_width
    427 	 * note: #bits == 1 << width
    428 	 */
    429 	switch(sc->sc_nor_if->access_width) {
    430 	case 0:
    431 		nor_read_buf_1(self, offset, data, chip->nc_page_size);
    432 		break;
    433 	case 1:
    434 		nor_read_buf_2(self, offset, data, chip->nc_page_size);
    435 		break;
    436 	case 2:
    437 		nor_read_buf_4(self, offset, data, chip->nc_page_size);
    438 		break;
    439 #ifdef NOTYET
    440 	case 3:
    441 		nor_read_buf_8(self, offset, data, chip->nc_page_size);
    442 		break;
    443 #endif
    444 	default:
    445 		panic("%s: bad width %d\n", __func__, sc->sc_nor_if->access_width);
    446 	}
    447 
    448 #if 0
    449 	/* for debugging new drivers */
    450 	nor_dump_data("page", data, chip->nc_page_size);
    451 #endif
    452 
    453 	return 0;
    454 }
    455 
    456 /* write a page, default implementation */
    457 static int
    458 nor_default_program_page(device_t self, flash_off_t offset,
    459     const uint8_t * const data)
    460 {
    461 	struct nor_softc * const sc = device_private(self);
    462 	struct nor_chip * const chip = &sc->sc_chip;
    463 
    464 	/*
    465 	 * access by specified width
    466 	 * #bits == 1 << access_width
    467 	 */
    468 	switch(sc->sc_nor_if->access_width) {
    469 	case 0:
    470 		nor_write_buf_1(self, offset, data, chip->nc_page_size);
    471 		break;
    472 	case 1:
    473 		nor_write_buf_2(self, offset, data, chip->nc_page_size);
    474 		break;
    475 	case 2:
    476 		nor_write_buf_4(self, offset, data, chip->nc_page_size);
    477 		break;
    478 #ifdef NOTYET
    479 	case 3:
    480 		nor_write_buf_8(self, offset, data, chip->nc_page_size);
    481 		break;
    482 #endif
    483 	default:
    484 		panic("%s: bad width %d\n", __func__,
    485 			sc->sc_nor_if->access_width);
    486 	}
    487 
    488 #if 0
    489 	/* for debugging new drivers */
    490 	nor_dump_data("page", data, chip->nc_page_size);
    491 #endif
    492 
    493 	return 0;
    494 }
    495 
    496 /*
    497  * nor_flash_erase_all - erase the entire chip
    498  *
    499  * XXX a good way to brick your system
    500  */
    501 static int
    502 nor_flash_erase_all(device_t self)
    503 {
    504 	struct nor_softc * const sc = device_private(self);
    505 	int error;
    506 
    507 	mutex_enter(&sc->sc_device_lock);
    508 	error = nor_erase_all(self);
    509 	mutex_exit(&sc->sc_device_lock);
    510 
    511 	return error;
    512 }
    513 
    514 static int
    515 nor_flash_erase(device_t self, struct flash_erase_instruction * const ei)
    516 {
    517 	struct nor_softc * const sc = device_private(self);
    518 	struct nor_chip * const chip = &sc->sc_chip;
    519 	flash_off_t addr;
    520 	int error = 0;
    521 
    522 	if (ei->ei_addr < 0 || ei->ei_len < chip->nc_block_size)
    523 		return EINVAL;
    524 
    525 	if (ei->ei_addr + ei->ei_len > chip->nc_size) {
    526 		DPRINTF(("%s: erase address is past the end"
    527 			" of the device\n", __func__));
    528 		return EINVAL;
    529 	}
    530 
    531 	if ((ei->ei_addr == 0) && (ei->ei_len == chip->nc_size)
    532 	&&  (sc->sc_nor_if->erase_all != NULL)) {
    533 		return nor_flash_erase_all(self);
    534 	}
    535 
    536 	if (ei->ei_addr % chip->nc_block_size != 0) {
    537 		aprint_error_dev(self,
    538 		    "nor_flash_erase: ei_addr (%ju) is not"
    539 		    " a multiple of block size (%ju)\n",
    540 		    (uintmax_t)ei->ei_addr,
    541 		    (uintmax_t)chip->nc_block_size);
    542 		return EINVAL;
    543 	}
    544 
    545 	if (ei->ei_len % chip->nc_block_size != 0) {
    546 		aprint_error_dev(self,
    547 		    "nor_flash_erase: ei_len (%ju) is not"
    548 		    " a multiple of block size (%ju)",
    549 		    (uintmax_t)ei->ei_len,
    550 		    (uintmax_t)chip->nc_block_size);
    551 		return EINVAL;
    552 	}
    553 
    554 	mutex_enter(&sc->sc_device_lock);
    555 	addr = ei->ei_addr;
    556 	while (addr < ei->ei_addr + ei->ei_len) {
    557 #ifdef NOTYET
    558 		if (nor_isbad(self, addr)) {
    559 			aprint_error_dev(self, "bad block encountered\n");
    560 			ei->ei_state = FLASH_ERASE_FAILED;
    561 			error = EIO;
    562 			goto out;
    563 		}
    564 #endif
    565 
    566 		error = nor_erase_block(self, addr);
    567 		if (error) {
    568 			ei->ei_state = FLASH_ERASE_FAILED;
    569 			goto out;
    570 		}
    571 
    572 		addr += chip->nc_block_size;
    573 	}
    574 	mutex_exit(&sc->sc_device_lock);
    575 
    576 	ei->ei_state = FLASH_ERASE_DONE;
    577 	if (ei->ei_callback != NULL) {
    578 		ei->ei_callback(ei);
    579 	}
    580 
    581 	return 0;
    582 out:
    583 	mutex_exit(&sc->sc_device_lock);
    584 
    585 	return error;
    586 }
    587 
    588 /*
    589  * handle (page) unaligned write to nor
    590  */
    591 static int
    592 nor_flash_write_unaligned(device_t self, flash_off_t offset, size_t len,
    593     size_t * const retlen, const uint8_t * const buf)
    594 {
    595 	struct nor_softc * const sc = device_private(self);
    596 	struct nor_chip * const chip = &sc->sc_chip;
    597 	flash_off_t first, last, firstoff;
    598 	const uint8_t *bufp;
    599 	flash_off_t addr;
    600 	size_t left, count;
    601 	int error = 0, i;
    602 
    603 	first = offset & chip->nc_page_mask;
    604 	firstoff = offset & ~chip->nc_page_mask;
    605 	/* XXX check if this should be len - 1 */
    606 	last = (offset + len) & chip->nc_page_mask;
    607 	count = last - first + 1;
    608 
    609 	addr = first;
    610 	*retlen = 0;
    611 
    612 	mutex_enter(&sc->sc_device_lock);
    613 	if (count == 1) {
    614 #ifdef NOTYET
    615 		if (nor_isbad(self, addr)) {
    616 			aprint_error_dev(self,
    617 			    "nor_flash_write_unaligned: "
    618 			    "bad block encountered\n");
    619 			error = EIO;
    620 			goto out;
    621 		}
    622 #endif
    623 
    624 		error = nor_read_page(self, addr, chip->nc_page_cache);
    625 		if (error) {
    626 			goto out;
    627 		}
    628 
    629 		memcpy(chip->nc_page_cache + firstoff, buf, len);
    630 
    631 		error = nor_program_page(self, addr, chip->nc_page_cache);
    632 		if (error) {
    633 			goto out;
    634 		}
    635 
    636 		*retlen = len;
    637 		goto out;
    638 	}
    639 
    640 	bufp = buf;
    641 	left = len;
    642 
    643 	for (i = 0; i < count && left != 0; i++) {
    644 #ifdef NOTYET
    645 		if (nor_isbad(self, addr)) {
    646 			aprint_error_dev(self,
    647 			    "nor_flash_write_unaligned: "
    648 			    "bad block encountered\n");
    649 			error = EIO;
    650 			goto out;
    651 		}
    652 #endif
    653 
    654 		if (i == 0) {
    655 			error = nor_read_page(self, addr, chip->nc_page_cache);
    656 			if (error) {
    657 				goto out;
    658 			}
    659 
    660 			memcpy(chip->nc_page_cache + firstoff,
    661 			    bufp, chip->nc_page_size - firstoff);
    662 
    663 			printf("write page: %s: %d\n", __FILE__, __LINE__);
    664 			error = nor_program_page(self, addr,
    665 				chip->nc_page_cache);
    666 			if (error) {
    667 				goto out;
    668 			}
    669 
    670 			bufp += chip->nc_page_size - firstoff;
    671 			left -= chip->nc_page_size - firstoff;
    672 			*retlen += chip->nc_page_size - firstoff;
    673 
    674 		} else if (i == count - 1) {
    675 			error = nor_read_page(self, addr, chip->nc_page_cache);
    676 			if (error) {
    677 				goto out;
    678 			}
    679 
    680 			memcpy(chip->nc_page_cache, bufp, left);
    681 
    682 			error = nor_program_page(self, addr,
    683 				chip->nc_page_cache);
    684 			if (error) {
    685 				goto out;
    686 			}
    687 
    688 			*retlen += left;
    689 			KASSERT(left < chip->nc_page_size);
    690 
    691 		} else {
    692 			/* XXX debug */
    693 			if (left > chip->nc_page_size) {
    694 				printf("left: %zu, i: %d, count: %zu\n",
    695 				    (size_t )left, i, count);
    696 			}
    697 			KASSERT(left > chip->nc_page_size);
    698 
    699 			error = nor_program_page(self, addr, bufp);
    700 			if (error) {
    701 				goto out;
    702 			}
    703 
    704 			bufp += chip->nc_page_size;
    705 			left -= chip->nc_page_size;
    706 			*retlen += chip->nc_page_size;
    707 		}
    708 
    709 		addr += chip->nc_page_size;
    710 	}
    711 
    712 	KASSERT(*retlen == len);
    713 out:
    714 	mutex_exit(&sc->sc_device_lock);
    715 
    716 	return error;
    717 }
    718 
    719 static int
    720 nor_flash_write(device_t self, flash_off_t offset, size_t len,
    721     size_t * const retlen, const uint8_t * const buf)
    722 {
    723 	struct nor_softc * const sc = device_private(self);
    724 	struct nor_chip * const chip = &sc->sc_chip;
    725 	const uint8_t *bufp;
    726 	size_t pages, page;
    727 	daddr_t addr;
    728 	int error = 0;
    729 
    730 	if ((offset + len) > chip->nc_size) {
    731 		DPRINTF(("%s: write (off: 0x%jx, len: %ju),"
    732 			" exceeds device size (0x%jx)\n", __func__,
    733 			(uintmax_t)offset, (uintmax_t)len,
    734 			(uintmax_t)chip->nc_size));
    735 		return EINVAL;
    736 	}
    737 
    738 	if (len % chip->nc_page_size != 0 ||
    739 	    offset % chip->nc_page_size != 0) {
    740 		return nor_flash_write_unaligned(self,
    741 		    offset, len, retlen, buf);
    742 	}
    743 
    744 	pages = len / chip->nc_page_size;
    745 	KASSERT(pages != 0);
    746 	*retlen = 0;
    747 
    748 	addr = offset;
    749 	bufp = buf;
    750 
    751 	mutex_enter(&sc->sc_device_lock);
    752 	for (page = 0; page < pages; page++) {
    753 #ifdef NOTYET
    754 		/* do we need this check here? */
    755 		if (nor_isbad(self, addr)) {
    756 			aprint_error_dev(self,
    757 			    "nor_flash_write: bad block encountered\n");
    758 
    759 			error = EIO;
    760 			goto out;
    761 		}
    762 #endif
    763 
    764 		error = nor_program_page(self, addr, bufp);
    765 		if (error) {
    766 			goto out;
    767 		}
    768 
    769 		addr += chip->nc_page_size;
    770 		bufp += chip->nc_page_size;
    771 		*retlen += chip->nc_page_size;
    772 	}
    773 out:
    774 	mutex_exit(&sc->sc_device_lock);
    775 	DPRINTF(("%s: retlen: %zu, len: %zu\n", __func__, *retlen, len));
    776 
    777 	return error;
    778 }
    779 
    780 /*
    781  * handle (page) unaligned read from nor
    782  */
    783 static int
    784 nor_flash_read_unaligned(device_t self, flash_off_t offset, size_t len,
    785     size_t * const retlen, uint8_t * const buf)
    786 {
    787 	struct nor_softc * const sc = device_private(self);
    788 	struct nor_chip * const chip = &sc->sc_chip;
    789 	daddr_t first, last, count, firstoff;
    790 	uint8_t *bufp;
    791 	daddr_t addr;
    792 	size_t left;
    793 	int error = 0, i;
    794 
    795 	first = offset & chip->nc_page_mask;
    796 	firstoff = offset & ~chip->nc_page_mask;
    797 	last = (offset + len) & chip->nc_page_mask;
    798 	count = (last - first) / chip->nc_page_size + 1;
    799 
    800 	addr = first;
    801 	bufp = buf;
    802 	left = len;
    803 	*retlen = 0;
    804 
    805 	mutex_enter(&sc->sc_device_lock);
    806 	if (count == 1) {
    807 		error = nor_read_page(self, addr, chip->nc_page_cache);
    808 		if (error) {
    809 			goto out;
    810 		}
    811 
    812 		memcpy(bufp, chip->nc_page_cache + firstoff, len);
    813 
    814 		*retlen = len;
    815 		goto out;
    816 	}
    817 
    818 	for (i = 0; i < count && left != 0; i++) {
    819 		/* XXX Why use the page cache here ? */
    820 		error = nor_read_page(self, addr, chip->nc_page_cache);
    821 		if (error) {
    822 			goto out;
    823 		}
    824 
    825 		if (i == 0) {
    826 			memcpy(bufp, chip->nc_page_cache + firstoff,
    827 			    chip->nc_page_size - firstoff);
    828 
    829 			bufp += chip->nc_page_size - firstoff;
    830 			left -= chip->nc_page_size - firstoff;
    831 			*retlen += chip->nc_page_size - firstoff;
    832 
    833 		} else if (i == count - 1) {
    834 			memcpy(bufp, chip->nc_page_cache, left);
    835 			*retlen += left;
    836 			KASSERT(left < chip->nc_page_size);
    837 
    838 		} else {
    839 			memcpy(bufp, chip->nc_page_cache, chip->nc_page_size);
    840 
    841 			bufp += chip->nc_page_size;
    842 			left -= chip->nc_page_size;
    843 			*retlen += chip->nc_page_size;
    844 		}
    845 
    846 		addr += chip->nc_page_size;
    847 	}
    848 	KASSERT(*retlen == len);
    849 out:
    850 	mutex_exit(&sc->sc_device_lock);
    851 
    852 	return error;
    853 }
    854 
    855 static int
    856 nor_flash_read(device_t self, flash_off_t offset, size_t len,
    857     size_t * const retlen, uint8_t * const buf)
    858 {
    859 	struct nor_softc * const sc = device_private(self);
    860 	struct nor_chip * const chip = &sc->sc_chip;
    861 	uint8_t *bufp;
    862 	size_t addr;
    863 	size_t i, pages;
    864 	int error = 0;
    865 
    866 	*retlen = 0;
    867 
    868 	DPRINTF(("%s: off: 0x%jx, len: %zu\n",
    869 		__func__, (uintmax_t)offset, len));
    870 
    871 	if (__predict_false((offset + len) > chip->nc_size)) {
    872 		DPRINTF(("%s: read (off: 0x%jx, len: %zu),"
    873 			" exceeds device size (%ju)\n", __func__,
    874 			(uintmax_t)offset, len, (uintmax_t)chip->nc_size));
    875 		return EINVAL;
    876 	}
    877 
    878 	/* Handle unaligned access, shouldnt be needed when using the
    879 	 * block device, as strategy handles it, so only low level
    880 	 * accesses will use this path
    881 	 */
    882 	/* XXX^2 */
    883 #if 0
    884 	if (len < chip->nc_page_size)
    885 		panic("TODO page size is larger than read size");
    886 #endif
    887 
    888 	if (len % chip->nc_page_size != 0 ||
    889 	    offset % chip->nc_page_size != 0) {
    890 		return nor_flash_read_unaligned(self,
    891 		    offset, len, retlen, buf);
    892 	}
    893 
    894 	bufp = buf;
    895 	addr = offset;
    896 	pages = len / chip->nc_page_size;
    897 
    898 	mutex_enter(&sc->sc_device_lock);
    899 	for (i = 0; i < pages; i++) {
    900 #ifdef NOTYET
    901 		/* XXX do we need this check here? */
    902 		if (nor_isbad(self, addr)) {
    903 			aprint_error_dev(self, "bad block encountered\n");
    904 			error = EIO;
    905 			goto out;
    906 		}
    907 #endif
    908 		error = nor_read_page(self, addr, bufp);
    909 		if (error)
    910 			goto out;
    911 
    912 		bufp += chip->nc_page_size;
    913 		addr += chip->nc_page_size;
    914 		*retlen += chip->nc_page_size;
    915 	}
    916 out:
    917 	mutex_exit(&sc->sc_device_lock);
    918 
    919 	return error;
    920 }
    921 
    922 static int
    923 nor_flash_isbad(device_t self, flash_off_t ofs, bool * const isbad)
    924 {
    925 	struct nor_softc * const sc = device_private(self);
    926 	struct nor_chip * const chip = &sc->sc_chip;
    927 #ifdef NOTYET
    928 	bool result;
    929 #endif
    930 
    931 	if (ofs > chip->nc_size) {
    932 		DPRINTF(("%s: offset 0x%jx is larger than"
    933 			" device size (0x%jx)\n", __func__,
    934 			(uintmax_t)ofs, (uintmax_t)chip->nc_size));
    935 		return EINVAL;
    936 	}
    937 
    938 	if (ofs % chip->nc_block_size != 0) {
    939 		DPRINTF(("offset (0x%jx) is not the multiple of block size "
    940 			"(%ju)",
    941 			(uintmax_t)ofs, (uintmax_t)chip->nc_block_size));
    942 		return EINVAL;
    943 	}
    944 
    945 #ifdef NOTYET
    946 	mutex_enter(&sc->sc_device_lock);
    947 	result = nor_isbad(self, ofs);
    948 	mutex_exit(&sc->sc_device_lock);
    949 
    950 	*isbad = result;
    951 #else
    952 	*isbad = false;
    953 #endif
    954 
    955 	return 0;
    956 }
    957 
    958 static int
    959 nor_flash_markbad(device_t self, flash_off_t ofs)
    960 {
    961 	struct nor_softc * const sc = device_private(self);
    962 	struct nor_chip * const chip = &sc->sc_chip;
    963 
    964 	if (ofs > chip->nc_size) {
    965 		DPRINTF(("%s: offset 0x%jx is larger than"
    966 			" device size (0x%jx)\n", __func__,
    967 			ofs, (uintmax_t)chip->nc_size));
    968 		return EINVAL;
    969 	}
    970 
    971 	if (ofs % chip->nc_block_size != 0) {
    972 		panic("offset (%ju) is not the multiple of block size (%ju)",
    973 		    (uintmax_t)ofs, (uintmax_t)chip->nc_block_size);
    974 	}
    975 
    976 	/* TODO: implement this */
    977 
    978 	return 0;
    979 }
    980 
    981 static int
    982 sysctl_nor_verify(SYSCTLFN_ARGS)
    983 {
    984 	int error, t;
    985 	struct sysctlnode node;
    986 
    987 	node = *rnode;
    988 	t = *(int *)rnode->sysctl_data;
    989 	node.sysctl_data = &t;
    990 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    991 	if (error || newp == NULL)
    992 		return error;
    993 
    994 	if (node.sysctl_num == nor_cachesync_nodenum) {
    995 		if (t <= 0 || t > 60)
    996 			return EINVAL;
    997 	} else {
    998 		return EINVAL;
    999 	}
   1000 
   1001 	*(int *)rnode->sysctl_data = t;
   1002 
   1003 	return 0;
   1004 }
   1005 
   1006 SYSCTL_SETUP(sysctl_nor, "sysctl nor subtree setup")
   1007 {
   1008 	int rc, nor_root_num;
   1009 	const struct sysctlnode *node;
   1010 
   1011 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
   1012 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
   1013 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
   1014 		goto error;
   1015 	}
   1016 
   1017 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   1018 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "nor",
   1019 	    SYSCTL_DESCR("NOR driver controls"),
   1020 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
   1021 		goto error;
   1022 	}
   1023 
   1024 	nor_root_num = node->sysctl_num;
   1025 
   1026 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   1027 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1028 	    CTLTYPE_INT, "cache_sync_timeout",
   1029 	    SYSCTL_DESCR("NOR write cache sync timeout in seconds"),
   1030 	    sysctl_nor_verify, 0, &nor_cachesync_timeout,
   1031 	    0, CTL_HW, nor_root_num, CTL_CREATE,
   1032 	    CTL_EOL)) != 0) {
   1033 		goto error;
   1034 	}
   1035 
   1036 	nor_cachesync_nodenum = node->sysctl_num;
   1037 
   1038 	return;
   1039 
   1040 error:
   1041 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
   1042 }
   1043 
   1044 MODULE(MODULE_CLASS_DRIVER, nor, "flash");
   1045 
   1046 #ifdef _MODULE
   1047 #include "ioconf.c"
   1048 #endif
   1049 
   1050 static int
   1051 nor_modcmd(modcmd_t cmd, void *opaque)
   1052 {
   1053 	switch (cmd) {
   1054 	case MODULE_CMD_INIT:
   1055 #ifdef _MODULE
   1056 		return config_init_component(cfdriver_ioconf_nor,
   1057 		    cfattach_ioconf_nor, cfdata_ioconf_nor);
   1058 #else
   1059 		return 0;
   1060 #endif
   1061 	case MODULE_CMD_FINI:
   1062 #ifdef _MODULE
   1063 		return config_fini_component(cfdriver_ioconf_nor,
   1064 		    cfattach_ioconf_nor, cfdata_ioconf_nor);
   1065 #else
   1066 		return 0;
   1067 #endif
   1068 	default:
   1069 		return ENOTTY;
   1070 	}
   1071 }
   1072