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