Home | History | Annotate | Line # | Download | only in nor
nor.c revision 1.1
      1  1.1  ahoka /*	$NetBSD: nor.c,v 1.1 2011/06/22 21:59:15 ahoka 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.1  ahoka __KERNEL_RCSID(0, "$NetBSD: nor.c,v 1.1 2011/06/22 21:59:15 ahoka Exp $");
     38  1.1  ahoka 
     39  1.1  ahoka #include "locators.h"
     40  1.1  ahoka 
     41  1.1  ahoka #include <sys/param.h>
     42  1.1  ahoka #include <sys/types.h>
     43  1.1  ahoka #include <sys/device.h>
     44  1.1  ahoka #include <sys/kmem.h>
     45  1.1  ahoka #include <sys/sysctl.h>
     46  1.1  ahoka #include <sys/atomic.h>
     47  1.1  ahoka 
     48  1.1  ahoka #include <dev/flash/flash.h>
     49  1.1  ahoka #include <dev/nor/nor.h>
     50  1.1  ahoka #include <dev/nor/cfi.h>
     51  1.1  ahoka 
     52  1.1  ahoka //#include "opt_nor.h"
     53  1.1  ahoka 
     54  1.1  ahoka int nor_match(device_t, cfdata_t, void *);
     55  1.1  ahoka void nor_attach(device_t, device_t, void *);
     56  1.1  ahoka int nor_detach(device_t, int);
     57  1.1  ahoka bool nor_shutdown(device_t, int);
     58  1.1  ahoka 
     59  1.1  ahoka int nor_print(void *, const char *);
     60  1.1  ahoka static int nor_search(device_t, cfdata_t, const int *, void *);
     61  1.1  ahoka 
     62  1.1  ahoka CFATTACH_DECL_NEW(nor, sizeof(struct nor_softc),
     63  1.1  ahoka     nor_match, nor_attach, nor_detach, NULL);
     64  1.1  ahoka 
     65  1.1  ahoka #ifdef NOR_DEBUG
     66  1.1  ahoka int	nordebug = NOR_DEBUG;
     67  1.1  ahoka #endif
     68  1.1  ahoka 
     69  1.1  ahoka int nor_cachesync_timeout = 1;
     70  1.1  ahoka int nor_cachesync_nodenum;
     71  1.1  ahoka 
     72  1.1  ahoka #ifdef NOR_VERBOSE
     73  1.1  ahoka const struct nor_manufacturer nor_mfrs[] = {
     74  1.1  ahoka 	{ NOR_MFR_AMD,		"AMD" },
     75  1.1  ahoka 	{ NOR_MFR_FUJITSU,	"Fujitsu" },
     76  1.1  ahoka 	{ NOR_MFR_RENESAS,	"Renesas" },
     77  1.1  ahoka 	{ NOR_MFR_STMICRO,	"ST Micro" },
     78  1.1  ahoka 	{ NOR_MFR_MICRON,	"Micron" },
     79  1.1  ahoka 	{ NOR_MFR_NATIONAL,	"National" },
     80  1.1  ahoka 	{ NOR_MFR_TOSHIBA,	"Toshiba" },
     81  1.1  ahoka 	{ NOR_MFR_HYNIX,	"Hynix" },
     82  1.1  ahoka 	{ NOR_MFR_SAMSUNG,	"Samsung" },
     83  1.1  ahoka 	{ NOR_MFR_UNKNOWN,	"Unknown" }
     84  1.1  ahoka };
     85  1.1  ahoka 
     86  1.1  ahoka static const char *
     87  1.1  ahoka nor_midtoname(int id)
     88  1.1  ahoka {
     89  1.1  ahoka 	int i;
     90  1.1  ahoka 
     91  1.1  ahoka 	for (i = 0; nor_mfrs[i].id != 0; i++) {
     92  1.1  ahoka 		if (nor_mfrs[i].id == id)
     93  1.1  ahoka 			return nor_mfrs[i].name;
     94  1.1  ahoka 	}
     95  1.1  ahoka 
     96  1.1  ahoka 	KASSERT(nor_mfrs[i].id == 0);
     97  1.1  ahoka 
     98  1.1  ahoka 	return nor_mfrs[i].name;
     99  1.1  ahoka }
    100  1.1  ahoka #endif
    101  1.1  ahoka 
    102  1.1  ahoka /* ARGSUSED */
    103  1.1  ahoka int
    104  1.1  ahoka nor_match(device_t parent, cfdata_t match, void *aux)
    105  1.1  ahoka {
    106  1.1  ahoka 	/* pseudo device, always attaches */
    107  1.1  ahoka 	return 1;
    108  1.1  ahoka }
    109  1.1  ahoka 
    110  1.1  ahoka void
    111  1.1  ahoka nor_attach(device_t parent, device_t self, void *aux)
    112  1.1  ahoka {
    113  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    114  1.1  ahoka 	struct nor_attach_args *naa = aux;
    115  1.1  ahoka 	struct nor_chip *chip = &sc->sc_chip;
    116  1.1  ahoka 
    117  1.1  ahoka 	sc->sc_dev = self;
    118  1.1  ahoka 	sc->controller_dev = parent;
    119  1.1  ahoka 	sc->nor_if = naa->naa_nor_if;
    120  1.1  ahoka 
    121  1.1  ahoka 	aprint_naive("\n");
    122  1.1  ahoka 
    123  1.1  ahoka 	/* allocate cache */
    124  1.1  ahoka 	chip->nc_oob_cache = kmem_alloc(chip->nc_spare_size, KM_SLEEP);
    125  1.1  ahoka 	chip->nc_page_cache = kmem_alloc(chip->nc_page_size, KM_SLEEP);
    126  1.1  ahoka 
    127  1.1  ahoka 	mutex_init(&sc->sc_device_lock, MUTEX_DEFAULT, IPL_NONE);
    128  1.1  ahoka 
    129  1.1  ahoka 	if (nor_sync_thread_start(self)) {
    130  1.1  ahoka 		goto error;
    131  1.1  ahoka 	}
    132  1.1  ahoka 
    133  1.1  ahoka 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, nor_shutdown))
    134  1.1  ahoka 		aprint_error_dev(sc->sc_dev,
    135  1.1  ahoka 		    "couldn't establish power handler\n");
    136  1.1  ahoka 
    137  1.1  ahoka #ifdef NOR_BBT
    138  1.1  ahoka 	nor_bbt_init(self);
    139  1.1  ahoka 	nor_bbt_scan(self);
    140  1.1  ahoka #endif
    141  1.1  ahoka 
    142  1.1  ahoka 	/*
    143  1.1  ahoka 	 * Attach all our devices
    144  1.1  ahoka 	 */
    145  1.1  ahoka 	config_search_ia(nor_search, self, NULL, NULL);
    146  1.1  ahoka 
    147  1.1  ahoka 	return;
    148  1.1  ahoka error:
    149  1.1  ahoka 	kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
    150  1.1  ahoka 	kmem_free(chip->nc_page_cache, chip->nc_page_size);
    151  1.1  ahoka 	mutex_destroy(&sc->sc_device_lock);
    152  1.1  ahoka }
    153  1.1  ahoka 
    154  1.1  ahoka static int
    155  1.1  ahoka nor_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    156  1.1  ahoka {
    157  1.1  ahoka 	struct nor_softc *sc = device_private(parent);
    158  1.1  ahoka 	struct nor_chip *chip = &sc->sc_chip;
    159  1.1  ahoka 	struct flash_interface *flash_if;
    160  1.1  ahoka 	struct flash_attach_args faa;
    161  1.1  ahoka 
    162  1.1  ahoka 	flash_if = kmem_alloc(sizeof(*flash_if), KM_SLEEP);
    163  1.1  ahoka 
    164  1.1  ahoka 	flash_if->type = FLASH_TYPE_NOR;
    165  1.1  ahoka 
    166  1.1  ahoka 	flash_if->read = nor_flash_read;
    167  1.1  ahoka 	flash_if->write = nor_flash_write;
    168  1.1  ahoka 	flash_if->erase = nor_flash_erase;
    169  1.1  ahoka 	flash_if->block_isbad = nor_flash_isbad;
    170  1.1  ahoka 	flash_if->block_markbad = nor_flash_markbad;
    171  1.1  ahoka 
    172  1.1  ahoka 	flash_if->submit = nor_io_submit;
    173  1.1  ahoka 
    174  1.1  ahoka 	flash_if->erasesize = chip->nc_block_size;
    175  1.1  ahoka 	flash_if->page_size = chip->nc_page_size;
    176  1.1  ahoka 	flash_if->writesize = chip->nc_page_size;
    177  1.1  ahoka 
    178  1.1  ahoka 	flash_if->partition.part_offset = cf->cf_loc[FLASHBUSCF_OFFSET];
    179  1.1  ahoka 
    180  1.1  ahoka 	if (cf->cf_loc[FLASHBUSCF_SIZE] == 0) {
    181  1.1  ahoka 		flash_if->size = chip->nc_size -
    182  1.1  ahoka 		    flash_if->partition.part_offset;
    183  1.1  ahoka 		flash_if->partition.part_size = flash_if->size;
    184  1.1  ahoka 	} else {
    185  1.1  ahoka 		flash_if->size = cf->cf_loc[FLASHBUSCF_SIZE];
    186  1.1  ahoka 		flash_if->partition.part_size = cf->cf_loc[FLASHBUSCF_SIZE];
    187  1.1  ahoka 	}
    188  1.1  ahoka 
    189  1.1  ahoka 	if (cf->cf_loc[FLASHBUSCF_READONLY])
    190  1.1  ahoka 		flash_if->partition.part_flags = FLASH_PART_READONLY;
    191  1.1  ahoka 	else
    192  1.1  ahoka 		flash_if->partition.part_flags = 0;
    193  1.1  ahoka 
    194  1.1  ahoka 	faa.flash_if = flash_if;
    195  1.1  ahoka 
    196  1.1  ahoka 	if (config_match(parent, cf, &faa)) {
    197  1.1  ahoka 		if (config_attach(parent, cf, &faa, nor_print) != NULL) {
    198  1.1  ahoka 			return 0;
    199  1.1  ahoka 		} else {
    200  1.1  ahoka 			return 1;
    201  1.1  ahoka 		}
    202  1.1  ahoka 	} else {
    203  1.1  ahoka 		kmem_free(flash_if, sizeof(*flash_if));
    204  1.1  ahoka 	}
    205  1.1  ahoka 
    206  1.1  ahoka 	return 1;
    207  1.1  ahoka }
    208  1.1  ahoka 
    209  1.1  ahoka int
    210  1.1  ahoka nor_detach(device_t self, int flags)
    211  1.1  ahoka {
    212  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    213  1.1  ahoka 	struct nor_chip *chip = &sc->sc_chip;
    214  1.1  ahoka 	int error = 0;
    215  1.1  ahoka 
    216  1.1  ahoka 	error = config_detach_children(self, flags);
    217  1.1  ahoka 	if (error) {
    218  1.1  ahoka 		return error;
    219  1.1  ahoka 	}
    220  1.1  ahoka 
    221  1.1  ahoka 	nor_sync_thread_stop(self);
    222  1.1  ahoka #ifdef NOR_BBT
    223  1.1  ahoka 	nor_bbt_detach(self);
    224  1.1  ahoka #endif
    225  1.1  ahoka 	/* free oob cache */
    226  1.1  ahoka 	kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
    227  1.1  ahoka 	kmem_free(chip->nc_page_cache, chip->nc_page_size);
    228  1.1  ahoka 
    229  1.1  ahoka 	mutex_destroy(&sc->sc_device_lock);
    230  1.1  ahoka 
    231  1.1  ahoka 	pmf_device_deregister(sc->sc_dev);
    232  1.1  ahoka 
    233  1.1  ahoka 	return error;
    234  1.1  ahoka }
    235  1.1  ahoka 
    236  1.1  ahoka int
    237  1.1  ahoka nor_print(void *aux, const char *pnp)
    238  1.1  ahoka {
    239  1.1  ahoka 	if (pnp != NULL)
    240  1.1  ahoka 		aprint_normal("nor at %s\n", pnp);
    241  1.1  ahoka 
    242  1.1  ahoka 	return UNCONF;
    243  1.1  ahoka }
    244  1.1  ahoka 
    245  1.1  ahoka /* ask for a nor driver to attach to the controller */
    246  1.1  ahoka device_t
    247  1.1  ahoka nor_attach_mi(struct nor_interface *nor_if, device_t parent)
    248  1.1  ahoka {
    249  1.1  ahoka 	struct nor_attach_args arg;
    250  1.1  ahoka 
    251  1.1  ahoka 	KASSERT(nor_if != NULL);
    252  1.1  ahoka 
    253  1.1  ahoka 	arg.naa_nor_if = nor_if;
    254  1.1  ahoka 	return config_found_ia(parent, "norbus", &arg, nor_print);
    255  1.1  ahoka }
    256  1.1  ahoka 
    257  1.1  ahoka /* default everything to reasonable values, to ease future api changes */
    258  1.1  ahoka void
    259  1.1  ahoka nor_init_interface(struct nor_interface *interface)
    260  1.1  ahoka {
    261  1.1  ahoka 	interface->select = &nor_default_select;
    262  1.1  ahoka 	interface->read_1 = NULL;
    263  1.1  ahoka 	interface->read_2 = NULL;
    264  1.1  ahoka 	interface->read_4 = NULL;
    265  1.1  ahoka 	interface->read_buf_1 = NULL;
    266  1.1  ahoka 	interface->read_buf_2 = NULL;
    267  1.1  ahoka 	interface->read_buf_4 = NULL;
    268  1.1  ahoka 	interface->write_1 = NULL;
    269  1.1  ahoka 	interface->write_2 = NULL;
    270  1.1  ahoka 	interface->write_4 = NULL;
    271  1.1  ahoka 	interface->write_buf_1 = NULL;
    272  1.1  ahoka 	interface->write_buf_2 = NULL;
    273  1.1  ahoka 	interface->write_buf_4 = NULL;
    274  1.1  ahoka 	interface->busy = NULL;
    275  1.1  ahoka }
    276  1.1  ahoka 
    277  1.1  ahoka #if 0
    278  1.1  ahoka /* handle quirks here */
    279  1.1  ahoka static void
    280  1.1  ahoka nor_quirks(device_t self, struct nor_chip *chip)
    281  1.1  ahoka {
    282  1.1  ahoka 	/* this is an example only! */
    283  1.1  ahoka 	switch (chip->nc_manf_id) {
    284  1.1  ahoka 	case NOR_MFR_SAMSUNG:
    285  1.1  ahoka 		if (chip->nc_dev_id == 0x00) {
    286  1.1  ahoka 			/* do something only samsung chips need */
    287  1.1  ahoka 			/* or */
    288  1.1  ahoka 			/* chip->nc_quirks |= NC_QUIRK_NO_READ_START */
    289  1.1  ahoka 		}
    290  1.1  ahoka 	}
    291  1.1  ahoka 
    292  1.1  ahoka 	return;
    293  1.1  ahoka }
    294  1.1  ahoka #endif
    295  1.1  ahoka 
    296  1.1  ahoka #if 0
    297  1.1  ahoka /**
    298  1.1  ahoka  * scan media to determine the chip's properties
    299  1.1  ahoka  * this function resets the device
    300  1.1  ahoka  */
    301  1.1  ahoka static int
    302  1.1  ahoka nor_scan_media(device_t self, struct nor_chip *chip)
    303  1.1  ahoka {
    304  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    305  1.1  ahoka 	uint8_t onfi_signature[4];
    306  1.1  ahoka 
    307  1.1  ahoka 
    308  1.1  ahoka 	/* TODO: enter query mode, check for signature */
    309  1.1  ahoka 
    310  1.1  ahoka 	/* TODO: get parameters in query mode */
    311  1.1  ahoka 
    312  1.1  ahoka #ifdef NOR_VERBOSE
    313  1.1  ahoka 	aprint_normal_dev(self,
    314  1.1  ahoka 	    "manufacturer id: 0x%.2x (%s), device id: 0x%.2x\n",
    315  1.1  ahoka 	    chip->nc_manf_id,
    316  1.1  ahoka 	    nor_midtoname(chip->nc_manf_id),
    317  1.1  ahoka 	    chip->nc_dev_id);
    318  1.1  ahoka #endif
    319  1.1  ahoka 
    320  1.1  ahoka 	aprint_normal_dev(self,
    321  1.1  ahoka 	    "page size: %zu bytes, spare size: %zu bytes, "
    322  1.1  ahoka 	    "block size: %zu bytes\n",
    323  1.1  ahoka 	    chip->nc_page_size, chip->nc_spare_size, chip->nc_block_size);
    324  1.1  ahoka 
    325  1.1  ahoka 	aprint_normal_dev(self,
    326  1.1  ahoka 	    "LUN size: %" PRIu32 " blocks, LUNs: %" PRIu8
    327  1.1  ahoka 	    ", total storage size: %zu MB\n",
    328  1.1  ahoka 	    chip->nc_lun_blocks, chip->nc_num_luns,
    329  1.1  ahoka 	    chip->nc_size / 1024 / 1024);
    330  1.1  ahoka 
    331  1.1  ahoka #ifdef NOR_VERBOSE
    332  1.1  ahoka 	aprint_normal_dev(self, "column cycles: %" PRIu8 ", row cycles: %"
    333  1.1  ahoka 	    PRIu8 "\n",
    334  1.1  ahoka 	    chip->nc_addr_cycles_column, chip->nc_addr_cycles_row);
    335  1.1  ahoka #endif
    336  1.1  ahoka 
    337  1.1  ahoka 	/* XXX does this apply to nor? */
    338  1.1  ahoka 	/*
    339  1.1  ahoka 	 * calculate badblock marker offset in oob
    340  1.1  ahoka 	 * we try to be compatible with linux here
    341  1.1  ahoka 	 */
    342  1.1  ahoka 	if (chip->nc_page_size > 512)
    343  1.1  ahoka 		chip->nc_badmarker_offs = 0;
    344  1.1  ahoka 	else
    345  1.1  ahoka 		chip->nc_badmarker_offs = 5;
    346  1.1  ahoka 
    347  1.1  ahoka 	/* Calculate page shift and mask */
    348  1.1  ahoka 	chip->nc_page_shift = ffs(chip->nc_page_size) - 1;
    349  1.1  ahoka 	chip->nc_page_mask = ~(chip->nc_page_size - 1);
    350  1.1  ahoka 	/* same for block */
    351  1.1  ahoka 	chip->nc_block_shift = ffs(chip->nc_block_size) - 1;
    352  1.1  ahoka 	chip->nc_block_mask = ~(chip->nc_block_size - 1);
    353  1.1  ahoka 
    354  1.1  ahoka 	/* look for quirks here if needed in future */
    355  1.1  ahoka 	/* nor_quirks(self, chip); */
    356  1.1  ahoka 
    357  1.1  ahoka 	return 0;
    358  1.1  ahoka }
    359  1.1  ahoka #endif
    360  1.1  ahoka 
    361  1.1  ahoka /* ARGSUSED */
    362  1.1  ahoka bool
    363  1.1  ahoka nor_shutdown(device_t self, int howto)
    364  1.1  ahoka {
    365  1.1  ahoka 	return true;
    366  1.1  ahoka }
    367  1.1  ahoka 
    368  1.1  ahoka /* implementation of the block device API */
    369  1.1  ahoka 
    370  1.1  ahoka int
    371  1.1  ahoka nor_flash_write(device_t self, flash_off_t offset, size_t len, size_t *retlen,
    372  1.1  ahoka     const uint8_t *buf)
    373  1.1  ahoka {
    374  1.1  ahoka 	/* TODO: implement this based on nand.c */
    375  1.1  ahoka 	return 0;
    376  1.1  ahoka }
    377  1.1  ahoka 
    378  1.1  ahoka int
    379  1.1  ahoka nor_flash_read(device_t self, flash_off_t offset, size_t len, size_t *retlen,
    380  1.1  ahoka     uint8_t *buf)
    381  1.1  ahoka {
    382  1.1  ahoka 	/* TODO: implement this based on nand.c */
    383  1.1  ahoka 	return 0;
    384  1.1  ahoka }
    385  1.1  ahoka 
    386  1.1  ahoka int
    387  1.1  ahoka nor_flash_isbad(device_t self, flash_off_t ofs, bool *isbad)
    388  1.1  ahoka {
    389  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    390  1.1  ahoka 	struct nor_chip *chip = &sc->sc_chip;
    391  1.1  ahoka //	bool result;
    392  1.1  ahoka 
    393  1.1  ahoka 	if (ofs > chip->nc_size) {
    394  1.1  ahoka 		DPRINTF(("nor_flash_isbad: offset 0x%jx is larger than"
    395  1.1  ahoka 			" device size (0x%jx)\n", (uintmax_t)ofs,
    396  1.1  ahoka 			(uintmax_t)chip->nc_size));
    397  1.1  ahoka 		return EINVAL;
    398  1.1  ahoka 	}
    399  1.1  ahoka 
    400  1.1  ahoka 	if (ofs % chip->nc_block_size != 0) {
    401  1.1  ahoka 		DPRINTF(("offset (0x%jx) is not the multiple of block size "
    402  1.1  ahoka 			"(%ju)",
    403  1.1  ahoka 			(uintmax_t)ofs, (uintmax_t)chip->nc_block_size));
    404  1.1  ahoka 		return EINVAL;
    405  1.1  ahoka 	}
    406  1.1  ahoka 
    407  1.1  ahoka 	mutex_enter(&sc->sc_device_lock);
    408  1.1  ahoka //	result = nor_isbad(self, ofs);
    409  1.1  ahoka 	mutex_exit(&sc->sc_device_lock);
    410  1.1  ahoka 
    411  1.1  ahoka //	*isbad = result;
    412  1.1  ahoka 	*isbad = false;
    413  1.1  ahoka 
    414  1.1  ahoka 	return 0;
    415  1.1  ahoka }
    416  1.1  ahoka 
    417  1.1  ahoka int
    418  1.1  ahoka nor_flash_markbad(device_t self, flash_off_t ofs)
    419  1.1  ahoka {
    420  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    421  1.1  ahoka 	struct nor_chip *chip = &sc->sc_chip;
    422  1.1  ahoka 
    423  1.1  ahoka 	if (ofs > chip->nc_size) {
    424  1.1  ahoka 		DPRINTF(("nor_flash_markbad: offset 0x%jx is larger than"
    425  1.1  ahoka 			" device size (0x%jx)\n", ofs,
    426  1.1  ahoka 			(uintmax_t)chip->nc_size));
    427  1.1  ahoka 		return EINVAL;
    428  1.1  ahoka 	}
    429  1.1  ahoka 
    430  1.1  ahoka 	if (ofs % chip->nc_block_size != 0) {
    431  1.1  ahoka 		panic("offset (%ju) is not the multiple of block size (%ju)",
    432  1.1  ahoka 		    (uintmax_t)ofs, (uintmax_t)chip->nc_block_size);
    433  1.1  ahoka 	}
    434  1.1  ahoka 
    435  1.1  ahoka 	/* TODO: implement this */
    436  1.1  ahoka 
    437  1.1  ahoka 	return 0;
    438  1.1  ahoka }
    439  1.1  ahoka 
    440  1.1  ahoka int
    441  1.1  ahoka nor_flash_erase(device_t self,
    442  1.1  ahoka     struct flash_erase_instruction *ei)
    443  1.1  ahoka {
    444  1.1  ahoka 	struct nor_softc *sc = device_private(self);
    445  1.1  ahoka 	struct nor_chip *chip = &sc->sc_chip;
    446  1.1  ahoka //	flash_off_t addr;
    447  1.1  ahoka //	int error = 0;
    448  1.1  ahoka 
    449  1.1  ahoka 	if (ei->ei_addr < 0 || ei->ei_len < chip->nc_block_size)
    450  1.1  ahoka 		return EINVAL;
    451  1.1  ahoka 
    452  1.1  ahoka 	if (ei->ei_addr + ei->ei_len > chip->nc_size) {
    453  1.1  ahoka 		DPRINTF(("nor_flash_erase: erase address is over the end"
    454  1.1  ahoka 			" of the device\n"));
    455  1.1  ahoka 		return EINVAL;
    456  1.1  ahoka 	}
    457  1.1  ahoka 
    458  1.1  ahoka 	if (ei->ei_addr % chip->nc_block_size != 0) {
    459  1.1  ahoka 		aprint_error_dev(self,
    460  1.1  ahoka 		    "nor_flash_erase: ei_addr (%ju) is not"
    461  1.1  ahoka 		    "the multiple of block size (%ju)",
    462  1.1  ahoka 		    (uintmax_t)ei->ei_addr,
    463  1.1  ahoka 		    (uintmax_t)chip->nc_block_size);
    464  1.1  ahoka 		return EINVAL;
    465  1.1  ahoka 	}
    466  1.1  ahoka 
    467  1.1  ahoka 	if (ei->ei_len % chip->nc_block_size != 0) {
    468  1.1  ahoka 		aprint_error_dev(self,
    469  1.1  ahoka 		    "nor_flash_erase: ei_len (%ju) is not"
    470  1.1  ahoka 		    "the multiple of block size (%ju)",
    471  1.1  ahoka 		    (uintmax_t)ei->ei_addr,
    472  1.1  ahoka 		    (uintmax_t)chip->nc_block_size);
    473  1.1  ahoka 		return EINVAL;
    474  1.1  ahoka 	}
    475  1.1  ahoka 
    476  1.1  ahoka 	/* TODO: do erase here */
    477  1.1  ahoka 
    478  1.1  ahoka 	ei->ei_state = FLASH_ERASE_DONE;
    479  1.1  ahoka 	if (ei->ei_callback != NULL) {
    480  1.1  ahoka 		ei->ei_callback(ei);
    481  1.1  ahoka 	}
    482  1.1  ahoka 
    483  1.1  ahoka 	return 0;
    484  1.1  ahoka #if 0
    485  1.1  ahoka out:
    486  1.1  ahoka 	mutex_exit(&sc->sc_device_lock);
    487  1.1  ahoka 
    488  1.1  ahoka 	return error;
    489  1.1  ahoka #endif
    490  1.1  ahoka }
    491  1.1  ahoka 
    492  1.1  ahoka static int
    493  1.1  ahoka sysctl_nor_verify(SYSCTLFN_ARGS)
    494  1.1  ahoka {
    495  1.1  ahoka 	int error, t;
    496  1.1  ahoka 	struct sysctlnode node;
    497  1.1  ahoka 
    498  1.1  ahoka 	node = *rnode;
    499  1.1  ahoka 	t = *(int *)rnode->sysctl_data;
    500  1.1  ahoka 	node.sysctl_data = &t;
    501  1.1  ahoka 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    502  1.1  ahoka 	if (error || newp == NULL)
    503  1.1  ahoka 		return error;
    504  1.1  ahoka 
    505  1.1  ahoka 	if (node.sysctl_num == nor_cachesync_nodenum) {
    506  1.1  ahoka 		if (t <= 0 || t > 60)
    507  1.1  ahoka 			return EINVAL;
    508  1.1  ahoka 	} else {
    509  1.1  ahoka 		return EINVAL;
    510  1.1  ahoka 	}
    511  1.1  ahoka 
    512  1.1  ahoka 	*(int *)rnode->sysctl_data = t;
    513  1.1  ahoka 
    514  1.1  ahoka 	return 0;
    515  1.1  ahoka }
    516  1.1  ahoka 
    517  1.1  ahoka SYSCTL_SETUP(sysctl_nor, "sysctl nor subtree setup")
    518  1.1  ahoka {
    519  1.1  ahoka 	int rc, nor_root_num;
    520  1.1  ahoka 	const struct sysctlnode *node;
    521  1.1  ahoka 
    522  1.1  ahoka 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
    523  1.1  ahoka 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
    524  1.1  ahoka 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
    525  1.1  ahoka 		goto error;
    526  1.1  ahoka 	}
    527  1.1  ahoka 
    528  1.1  ahoka 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    529  1.1  ahoka 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "nor",
    530  1.1  ahoka 	    SYSCTL_DESCR("NOR driver controls"),
    531  1.1  ahoka 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
    532  1.1  ahoka 		goto error;
    533  1.1  ahoka 	}
    534  1.1  ahoka 
    535  1.1  ahoka 	nor_root_num = node->sysctl_num;
    536  1.1  ahoka 
    537  1.1  ahoka 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    538  1.1  ahoka 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    539  1.1  ahoka 	    CTLTYPE_INT, "cache_sync_timeout",
    540  1.1  ahoka 	    SYSCTL_DESCR("NOR write cache sync timeout in seconds"),
    541  1.1  ahoka 	    sysctl_nor_verify, 0, &nor_cachesync_timeout,
    542  1.1  ahoka 	    0, CTL_HW, nor_root_num, CTL_CREATE,
    543  1.1  ahoka 	    CTL_EOL)) != 0) {
    544  1.1  ahoka 		goto error;
    545  1.1  ahoka 	}
    546  1.1  ahoka 
    547  1.1  ahoka 	nor_cachesync_nodenum = node->sysctl_num;
    548  1.1  ahoka 
    549  1.1  ahoka 	return;
    550  1.1  ahoka 
    551  1.1  ahoka error:
    552  1.1  ahoka 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
    553  1.1  ahoka }
    554  1.1  ahoka 
    555  1.1  ahoka MODULE(MODULE_CLASS_DRIVER, nor, "flash");
    556  1.1  ahoka 
    557  1.1  ahoka #ifdef _MODULE
    558  1.1  ahoka #include "ioconf.c"
    559  1.1  ahoka #endif
    560  1.1  ahoka 
    561  1.1  ahoka static int
    562  1.1  ahoka nor_modcmd(modcmd_t cmd, void *opaque)
    563  1.1  ahoka {
    564  1.1  ahoka 	switch (cmd) {
    565  1.1  ahoka 	case MODULE_CMD_INIT:
    566  1.1  ahoka #ifdef _MODULE
    567  1.1  ahoka 		return config_init_component(cfdriver_ioconf_nor,
    568  1.1  ahoka 		    cfattach_ioconf_nor, cfdata_ioconf_nor);
    569  1.1  ahoka #else
    570  1.1  ahoka 		return 0;
    571  1.1  ahoka #endif
    572  1.1  ahoka 	case MODULE_CMD_FINI:
    573  1.1  ahoka #ifdef _MODULE
    574  1.1  ahoka 		return config_fini_component(cfdriver_ioconf_nor,
    575  1.1  ahoka 		    cfattach_ioconf_nor, cfdata_ioconf_nor);
    576  1.1  ahoka #else
    577  1.1  ahoka 		return 0;
    578  1.1  ahoka #endif
    579  1.1  ahoka 	default:
    580  1.1  ahoka 		return ENOTTY;
    581  1.1  ahoka 	}
    582  1.1  ahoka }
    583