Home | History | Annotate | Line # | Download | only in nand
nand.c revision 1.27.20.4
      1  1.27.20.4   thorpej /*	$NetBSD: nand.c,v 1.27.20.4 2021/04/04 22:01:22 thorpej Exp $	*/
      2        1.1     ahoka 
      3        1.1     ahoka /*-
      4        1.1     ahoka  * Copyright (c) 2010 Department of Software Engineering,
      5        1.1     ahoka  *		      University of Szeged, Hungary
      6        1.1     ahoka  * Copyright (c) 2010 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 NAND chips implementing the ONFI 2.2 specification */
     35        1.1     ahoka 
     36        1.1     ahoka #include <sys/cdefs.h>
     37  1.27.20.4   thorpej __KERNEL_RCSID(0, "$NetBSD: nand.c,v 1.27.20.4 2021/04/04 22:01:22 thorpej 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.9     ahoka #include <sys/atomic.h>
     46        1.1     ahoka 
     47        1.1     ahoka #include <dev/flash/flash.h>
     48       1.13     ahoka #include <dev/flash/flash_io.h>
     49        1.1     ahoka #include <dev/nand/nand.h>
     50        1.1     ahoka #include <dev/nand/onfi.h>
     51        1.1     ahoka #include <dev/nand/hamming.h>
     52        1.1     ahoka #include <dev/nand/nand_bbt.h>
     53        1.1     ahoka #include <dev/nand/nand_crc.h>
     54        1.1     ahoka 
     55        1.1     ahoka #include "opt_nand.h"
     56        1.1     ahoka 
     57        1.2     ahoka int nand_match(device_t, cfdata_t, void *);
     58        1.2     ahoka void nand_attach(device_t, device_t, void *);
     59        1.2     ahoka int nand_detach(device_t, int);
     60        1.2     ahoka bool nand_shutdown(device_t, int);
     61        1.2     ahoka 
     62        1.2     ahoka int nand_print(void *, const char *);
     63        1.2     ahoka 
     64        1.2     ahoka static int nand_search(device_t, cfdata_t, const int *, void *);
     65        1.2     ahoka static void nand_address_row(device_t, size_t);
     66       1.24  kiyohara static inline uint8_t nand_get_status(device_t);
     67        1.2     ahoka static void nand_address_column(device_t, size_t, size_t);
     68        1.6     ahoka static int nand_fill_chip_structure(device_t, struct nand_chip *);
     69        1.2     ahoka static int nand_scan_media(device_t, struct nand_chip *);
     70        1.2     ahoka static bool nand_check_wp(device_t);
     71        1.1     ahoka 
     72       1.10     ahoka CFATTACH_DECL_NEW(nand, sizeof(struct nand_softc),
     73       1.10     ahoka     nand_match, nand_attach, nand_detach, NULL);
     74        1.1     ahoka 
     75        1.1     ahoka #ifdef NAND_DEBUG
     76        1.1     ahoka int	nanddebug = NAND_DEBUG;
     77        1.1     ahoka #endif
     78        1.1     ahoka 
     79       1.13     ahoka struct flash_interface nand_flash_if = {
     80       1.13     ahoka 	.type = FLASH_TYPE_NAND,
     81       1.13     ahoka 
     82       1.13     ahoka 	.read = nand_flash_read,
     83       1.13     ahoka 	.write = nand_flash_write,
     84       1.13     ahoka 	.erase = nand_flash_erase,
     85       1.13     ahoka 	.block_isbad = nand_flash_isbad,
     86       1.13     ahoka 	.block_markbad = nand_flash_markbad,
     87       1.13     ahoka 
     88       1.14     ahoka 	.submit = nand_flash_submit
     89       1.13     ahoka };
     90        1.1     ahoka 
     91        1.1     ahoka const struct nand_manufacturer nand_mfrs[] = {
     92        1.1     ahoka 	{ NAND_MFR_AMD,		"AMD" },
     93        1.1     ahoka 	{ NAND_MFR_FUJITSU,	"Fujitsu" },
     94        1.1     ahoka 	{ NAND_MFR_RENESAS,	"Renesas" },
     95        1.1     ahoka 	{ NAND_MFR_STMICRO,	"ST Micro" },
     96        1.1     ahoka 	{ NAND_MFR_MICRON,	"Micron" },
     97        1.1     ahoka 	{ NAND_MFR_NATIONAL,	"National" },
     98        1.1     ahoka 	{ NAND_MFR_TOSHIBA,	"Toshiba" },
     99        1.1     ahoka 	{ NAND_MFR_HYNIX,	"Hynix" },
    100        1.1     ahoka 	{ NAND_MFR_SAMSUNG,	"Samsung" },
    101        1.1     ahoka 	{ NAND_MFR_UNKNOWN,	"Unknown" }
    102        1.1     ahoka };
    103        1.1     ahoka 
    104        1.6     ahoka static const char *
    105        1.6     ahoka nand_midtoname(int id)
    106        1.6     ahoka {
    107        1.6     ahoka 	int i;
    108        1.6     ahoka 
    109        1.6     ahoka 	for (i = 0; nand_mfrs[i].id != 0; i++) {
    110        1.6     ahoka 		if (nand_mfrs[i].id == id)
    111        1.6     ahoka 			return nand_mfrs[i].name;
    112        1.6     ahoka 	}
    113        1.6     ahoka 
    114        1.6     ahoka 	KASSERT(nand_mfrs[i].id == 0);
    115        1.6     ahoka 
    116        1.6     ahoka 	return nand_mfrs[i].name;
    117        1.6     ahoka }
    118        1.6     ahoka 
    119        1.1     ahoka /* ARGSUSED */
    120        1.1     ahoka int
    121        1.1     ahoka nand_match(device_t parent, cfdata_t match, void *aux)
    122        1.1     ahoka {
    123        1.1     ahoka 	/* pseudo device, always attaches */
    124        1.1     ahoka 	return 1;
    125        1.1     ahoka }
    126        1.1     ahoka 
    127        1.1     ahoka void
    128        1.1     ahoka nand_attach(device_t parent, device_t self, void *aux)
    129        1.1     ahoka {
    130        1.1     ahoka 	struct nand_softc *sc = device_private(self);
    131        1.1     ahoka 	struct nand_attach_args *naa = aux;
    132        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
    133        1.1     ahoka 
    134        1.1     ahoka 	sc->sc_dev = self;
    135        1.6     ahoka 	sc->controller_dev = parent;
    136        1.1     ahoka 	sc->nand_if = naa->naa_nand_if;
    137        1.1     ahoka 
    138        1.1     ahoka 	aprint_naive("\n");
    139        1.1     ahoka 
    140        1.1     ahoka 	if (nand_check_wp(self)) {
    141        1.1     ahoka 		aprint_error("NAND chip is write protected!\n");
    142        1.1     ahoka 		return;
    143        1.1     ahoka 	}
    144       1.12     ahoka 
    145        1.3     ahoka 	if (nand_scan_media(self, chip)) {
    146        1.1     ahoka 		return;
    147        1.3     ahoka 	}
    148        1.1     ahoka 
    149       1.13     ahoka 	nand_flash_if.erasesize = chip->nc_block_size;
    150       1.13     ahoka 	nand_flash_if.page_size = chip->nc_page_size;
    151       1.13     ahoka 	nand_flash_if.writesize = chip->nc_page_size;
    152       1.13     ahoka 
    153        1.1     ahoka 	/* allocate cache */
    154        1.1     ahoka 	chip->nc_oob_cache = kmem_alloc(chip->nc_spare_size, KM_SLEEP);
    155        1.1     ahoka 	chip->nc_page_cache = kmem_alloc(chip->nc_page_size, KM_SLEEP);
    156        1.1     ahoka 
    157        1.1     ahoka 	mutex_init(&sc->sc_device_lock, MUTEX_DEFAULT, IPL_NONE);
    158        1.1     ahoka 
    159       1.15     cliff 	if (flash_sync_thread_init(&sc->sc_flash_io, self, &nand_flash_if)) {
    160        1.1     ahoka 		goto error;
    161        1.1     ahoka 	}
    162        1.1     ahoka 
    163        1.1     ahoka 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, nand_shutdown))
    164        1.1     ahoka 		aprint_error_dev(sc->sc_dev,
    165        1.1     ahoka 		    "couldn't establish power handler\n");
    166        1.1     ahoka 
    167        1.1     ahoka #ifdef NAND_BBT
    168        1.1     ahoka 	nand_bbt_init(self);
    169        1.1     ahoka 	nand_bbt_scan(self);
    170        1.1     ahoka #endif
    171        1.1     ahoka 
    172        1.1     ahoka 	/*
    173        1.1     ahoka 	 * Attach all our devices
    174        1.1     ahoka 	 */
    175  1.27.20.1   thorpej 	config_search(self, NULL,
    176  1.27.20.4   thorpej 	    CFARG_SEARCH, nand_search,
    177  1.27.20.1   thorpej 	    CFARG_EOL);
    178        1.1     ahoka 
    179        1.1     ahoka 	return;
    180        1.1     ahoka error:
    181        1.1     ahoka 	kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
    182        1.1     ahoka 	kmem_free(chip->nc_page_cache, chip->nc_page_size);
    183        1.1     ahoka 	mutex_destroy(&sc->sc_device_lock);
    184        1.1     ahoka }
    185        1.1     ahoka 
    186        1.1     ahoka static int
    187        1.1     ahoka nand_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    188        1.1     ahoka {
    189        1.1     ahoka 	struct nand_softc *sc = device_private(parent);
    190        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
    191        1.1     ahoka 	struct flash_attach_args faa;
    192        1.1     ahoka 
    193       1.27  jmcneill 	if (cf->cf_loc[FLASHBUSCF_DYNAMIC] != 0)
    194       1.27  jmcneill 		return 0;
    195       1.27  jmcneill 
    196       1.13     ahoka 	faa.flash_if = &nand_flash_if;
    197        1.1     ahoka 
    198       1.27  jmcneill 	faa.partinfo.part_name = NULL;
    199       1.13     ahoka 	faa.partinfo.part_offset = cf->cf_loc[FLASHBUSCF_OFFSET];
    200        1.1     ahoka 
    201        1.1     ahoka 	if (cf->cf_loc[FLASHBUSCF_SIZE] == 0) {
    202       1.13     ahoka 		faa.partinfo.part_size = chip->nc_size -
    203       1.13     ahoka 		    faa.partinfo.part_offset;
    204        1.1     ahoka 	} else {
    205       1.13     ahoka 		faa.partinfo.part_size = cf->cf_loc[FLASHBUSCF_SIZE];
    206        1.1     ahoka 	}
    207        1.1     ahoka 
    208        1.1     ahoka 	if (cf->cf_loc[FLASHBUSCF_READONLY])
    209       1.13     ahoka 		faa.partinfo.part_flags = FLASH_PART_READONLY;
    210        1.1     ahoka 	else
    211       1.13     ahoka 		faa.partinfo.part_flags = 0;
    212        1.1     ahoka 
    213        1.1     ahoka 	if (config_match(parent, cf, &faa)) {
    214  1.27.20.3   thorpej 		if (config_attach(parent, cf, &faa, nand_print,
    215  1.27.20.3   thorpej 				  CFARG_EOL) != NULL) {
    216        1.9     ahoka 			return 0;
    217        1.9     ahoka 		} else {
    218        1.9     ahoka 			return 1;
    219        1.9     ahoka 		}
    220        1.1     ahoka 	}
    221        1.1     ahoka 
    222        1.1     ahoka 	return 1;
    223        1.1     ahoka }
    224        1.1     ahoka 
    225       1.27  jmcneill void
    226       1.27  jmcneill nand_attach_mtdparts(device_t parent, const char *mtd_id, const char *cmdline)
    227       1.27  jmcneill {
    228       1.27  jmcneill 	struct nand_softc *sc = device_private(parent);
    229       1.27  jmcneill 	struct nand_chip *chip = &sc->sc_chip;
    230       1.27  jmcneill 
    231       1.27  jmcneill 	flash_attach_mtdparts(&nand_flash_if, parent, chip->nc_size,
    232       1.27  jmcneill 	    mtd_id, cmdline);
    233       1.27  jmcneill }
    234       1.27  jmcneill 
    235        1.1     ahoka int
    236        1.1     ahoka nand_detach(device_t self, int flags)
    237        1.1     ahoka {
    238        1.1     ahoka 	struct nand_softc *sc = device_private(self);
    239        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
    240       1.10     ahoka 	int error = 0;
    241        1.1     ahoka 
    242       1.10     ahoka 	error = config_detach_children(self, flags);
    243       1.10     ahoka 	if (error) {
    244       1.10     ahoka 		return error;
    245        1.9     ahoka 	}
    246        1.9     ahoka 
    247       1.14     ahoka 	flash_sync_thread_destroy(&sc->sc_flash_io);
    248        1.1     ahoka #ifdef NAND_BBT
    249        1.1     ahoka 	nand_bbt_detach(self);
    250        1.1     ahoka #endif
    251        1.1     ahoka 	/* free oob cache */
    252        1.1     ahoka 	kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
    253        1.1     ahoka 	kmem_free(chip->nc_page_cache, chip->nc_page_size);
    254        1.1     ahoka 	kmem_free(chip->nc_ecc_cache, chip->nc_ecc->necc_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.10     ahoka 	return error;
    261        1.9     ahoka }
    262        1.9     ahoka 
    263        1.1     ahoka int
    264        1.1     ahoka nand_print(void *aux, const char *pnp)
    265        1.1     ahoka {
    266        1.1     ahoka 	if (pnp != NULL)
    267        1.1     ahoka 		aprint_normal("nand at %s\n", pnp);
    268        1.1     ahoka 
    269        1.1     ahoka 	return UNCONF;
    270        1.1     ahoka }
    271        1.1     ahoka 
    272        1.6     ahoka /* ask for a nand driver to attach to the controller */
    273        1.1     ahoka device_t
    274        1.1     ahoka nand_attach_mi(struct nand_interface *nand_if, device_t parent)
    275        1.1     ahoka {
    276        1.1     ahoka 	struct nand_attach_args arg;
    277        1.1     ahoka 
    278        1.1     ahoka 	KASSERT(nand_if != NULL);
    279        1.1     ahoka 
    280        1.6     ahoka 	/* fill the defaults if we have null pointers */
    281        1.6     ahoka 	if (nand_if->program_page == NULL) {
    282        1.6     ahoka 		nand_if->program_page = &nand_default_program_page;
    283        1.6     ahoka 	}
    284        1.6     ahoka 
    285        1.6     ahoka 	if (nand_if->read_page == NULL) {
    286        1.6     ahoka 		nand_if->read_page = &nand_default_read_page;
    287        1.6     ahoka 	}
    288        1.6     ahoka 
    289        1.1     ahoka 	arg.naa_nand_if = nand_if;
    290  1.27.20.2   thorpej 	return config_found(parent, &arg, nand_print,
    291  1.27.20.2   thorpej 	    CFARG_IATTR, "nandbus",
    292  1.27.20.2   thorpej 	    CFARG_EOL);
    293        1.1     ahoka }
    294        1.1     ahoka 
    295        1.6     ahoka /* default everything to reasonable values, to ease future api changes */
    296        1.6     ahoka void
    297        1.6     ahoka nand_init_interface(struct nand_interface *interface)
    298        1.1     ahoka {
    299        1.6     ahoka 	interface->select = &nand_default_select;
    300        1.6     ahoka 	interface->command = NULL;
    301        1.6     ahoka 	interface->address = NULL;
    302       1.12     ahoka 	interface->read_buf_1 = NULL;
    303       1.12     ahoka 	interface->read_buf_2 = NULL;
    304       1.12     ahoka 	interface->read_1 = NULL;
    305       1.12     ahoka 	interface->read_2 = NULL;
    306       1.12     ahoka 	interface->write_buf_1 = NULL;
    307       1.12     ahoka 	interface->write_buf_2 = NULL;
    308       1.12     ahoka 	interface->write_1 = NULL;
    309       1.12     ahoka 	interface->write_2 = NULL;
    310        1.6     ahoka 	interface->busy = NULL;
    311        1.6     ahoka 
    312        1.6     ahoka 	/*-
    313        1.6     ahoka 	 * most drivers dont want to change this, but some implement
    314        1.6     ahoka 	 * read/program in one step
    315        1.6     ahoka 	 */
    316        1.6     ahoka 	interface->program_page = &nand_default_program_page;
    317        1.6     ahoka 	interface->read_page = &nand_default_read_page;
    318        1.1     ahoka 
    319        1.6     ahoka 	/* default to soft ecc, that should work everywhere */
    320        1.6     ahoka 	interface->ecc_compute = &nand_default_ecc_compute;
    321        1.6     ahoka 	interface->ecc_correct = &nand_default_ecc_correct;
    322        1.6     ahoka 	interface->ecc_prepare = NULL;
    323        1.6     ahoka 	interface->ecc.necc_code_size = 3;
    324        1.6     ahoka 	interface->ecc.necc_block_size = 256;
    325        1.6     ahoka 	interface->ecc.necc_type = NAND_ECC_TYPE_SW;
    326        1.1     ahoka }
    327        1.1     ahoka 
    328        1.1     ahoka #if 0
    329        1.1     ahoka /* handle quirks here */
    330        1.1     ahoka static void
    331        1.1     ahoka nand_quirks(device_t self, struct nand_chip *chip)
    332        1.1     ahoka {
    333        1.1     ahoka 	/* this is an example only! */
    334        1.1     ahoka 	switch (chip->nc_manf_id) {
    335        1.1     ahoka 	case NAND_MFR_SAMSUNG:
    336        1.1     ahoka 		if (chip->nc_dev_id == 0x00) {
    337        1.1     ahoka 			/* do something only samsung chips need */
    338        1.1     ahoka 			/* or */
    339        1.1     ahoka 			/* chip->nc_quirks |= NC_QUIRK_NO_READ_START */
    340        1.1     ahoka 		}
    341        1.1     ahoka 	}
    342        1.1     ahoka 
    343        1.1     ahoka 	return;
    344        1.1     ahoka }
    345        1.1     ahoka #endif
    346        1.1     ahoka 
    347        1.3     ahoka static int
    348        1.6     ahoka nand_fill_chip_structure_legacy(device_t self, struct nand_chip *chip)
    349        1.3     ahoka {
    350        1.3     ahoka 	switch (chip->nc_manf_id) {
    351        1.3     ahoka 	case NAND_MFR_MICRON:
    352        1.3     ahoka 		return nand_read_parameters_micron(self, chip);
    353       1.18       riz 	case NAND_MFR_SAMSUNG:
    354       1.18       riz 		return nand_read_parameters_samsung(self, chip);
    355       1.26  jmcneill 	case NAND_MFR_TOSHIBA:
    356       1.26  jmcneill 		return nand_read_parameters_toshiba(self, chip);
    357        1.3     ahoka 	default:
    358        1.3     ahoka 		return 1;
    359        1.3     ahoka 	}
    360       1.11     rmind 
    361        1.3     ahoka 	return 0;
    362        1.3     ahoka }
    363        1.3     ahoka 
    364        1.1     ahoka /**
    365        1.1     ahoka  * scan media to determine the chip's properties
    366        1.1     ahoka  * this function resets the device
    367        1.1     ahoka  */
    368        1.1     ahoka static int
    369        1.1     ahoka nand_scan_media(device_t self, struct nand_chip *chip)
    370        1.1     ahoka {
    371        1.1     ahoka 	struct nand_softc *sc = device_private(self);
    372        1.1     ahoka 	struct nand_ecc *ecc;
    373        1.1     ahoka 	uint8_t onfi_signature[4];
    374        1.1     ahoka 
    375        1.1     ahoka 	nand_select(self, true);
    376        1.1     ahoka 	nand_command(self, ONFI_RESET);
    377       1.24  kiyohara 	KASSERT(nand_get_status(self) & ONFI_STATUS_RDY);
    378        1.1     ahoka 	nand_select(self, false);
    379        1.1     ahoka 
    380        1.3     ahoka 	/* check if the device implements the ONFI standard */
    381        1.1     ahoka 	nand_select(self, true);
    382        1.1     ahoka 	nand_command(self, ONFI_READ_ID);
    383        1.1     ahoka 	nand_address(self, 0x20);
    384       1.12     ahoka 	nand_read_1(self, &onfi_signature[0]);
    385       1.12     ahoka 	nand_read_1(self, &onfi_signature[1]);
    386       1.12     ahoka 	nand_read_1(self, &onfi_signature[2]);
    387       1.12     ahoka 	nand_read_1(self, &onfi_signature[3]);
    388        1.1     ahoka 	nand_select(self, false);
    389        1.1     ahoka 
    390       1.26  jmcneill #ifdef NAND_DEBUG
    391       1.26  jmcneill 	device_printf(self, "signature: %02x %02x %02x %02x\n",
    392       1.26  jmcneill 	    onfi_signature[0], onfi_signature[1],
    393       1.26  jmcneill 	    onfi_signature[2], onfi_signature[3]);
    394       1.26  jmcneill #endif
    395       1.26  jmcneill 
    396        1.1     ahoka 	if (onfi_signature[0] != 'O' || onfi_signature[1] != 'N' ||
    397        1.1     ahoka 	    onfi_signature[2] != 'F' || onfi_signature[3] != 'I') {
    398        1.3     ahoka 		chip->nc_isonfi = false;
    399       1.11     rmind 
    400        1.3     ahoka 		aprint_normal(": Legacy NAND Flash\n");
    401       1.11     rmind 
    402        1.6     ahoka 		nand_read_id(self, &chip->nc_manf_id, &chip->nc_dev_id);
    403        1.1     ahoka 
    404        1.6     ahoka 		if (nand_fill_chip_structure_legacy(self, chip)) {
    405        1.3     ahoka 			aprint_error_dev(self,
    406        1.3     ahoka 			    "can't read device parameters for legacy chip\n");
    407        1.3     ahoka 			return 1;
    408        1.3     ahoka 		}
    409        1.3     ahoka 	} else {
    410        1.3     ahoka 		chip->nc_isonfi = true;
    411        1.1     ahoka 
    412        1.3     ahoka 		aprint_normal(": ONFI NAND Flash\n");
    413        1.1     ahoka 
    414        1.6     ahoka 		nand_read_id(self, &chip->nc_manf_id, &chip->nc_dev_id);
    415       1.11     rmind 
    416        1.6     ahoka 		if (nand_fill_chip_structure(self, chip)) {
    417        1.6     ahoka 			aprint_error_dev(self,
    418        1.6     ahoka 			    "can't read device parameters\n");
    419        1.6     ahoka 			return 1;
    420        1.6     ahoka 		}
    421        1.3     ahoka 	}
    422        1.1     ahoka 
    423        1.3     ahoka 	aprint_normal_dev(self,
    424        1.1     ahoka 	    "manufacturer id: 0x%.2x (%s), device id: 0x%.2x\n",
    425        1.1     ahoka 	    chip->nc_manf_id,
    426        1.1     ahoka 	    nand_midtoname(chip->nc_manf_id),
    427        1.1     ahoka 	    chip->nc_dev_id);
    428        1.1     ahoka 
    429        1.3     ahoka 	aprint_normal_dev(self,
    430       1.19     ahoka 	    "page size: %" PRIu32 " bytes, spare size: %" PRIu32 " bytes, "
    431       1.19     ahoka 	    "block size: %" PRIu32 " bytes\n",
    432        1.3     ahoka 	    chip->nc_page_size, chip->nc_spare_size, chip->nc_block_size);
    433        1.3     ahoka 
    434        1.3     ahoka 	aprint_normal_dev(self,
    435        1.5     ahoka 	    "LUN size: %" PRIu32 " blocks, LUNs: %" PRIu8
    436       1.19     ahoka 	    ", total storage size: %" PRIu64 " MB\n",
    437        1.3     ahoka 	    chip->nc_lun_blocks, chip->nc_num_luns,
    438        1.3     ahoka 	    chip->nc_size / 1024 / 1024);
    439        1.1     ahoka 
    440        1.5     ahoka 	aprint_normal_dev(self, "column cycles: %" PRIu8 ", row cycles: %"
    441       1.21     ahoka 	    PRIu8 ", width: %s\n",
    442       1.21     ahoka 	    chip->nc_addr_cycles_column, chip->nc_addr_cycles_row,
    443       1.21     ahoka 	    (chip->nc_flags & NC_BUSWIDTH_16) ? "x16" : "x8");
    444       1.12     ahoka 
    445        1.1     ahoka 	ecc = chip->nc_ecc = &sc->nand_if->ecc;
    446        1.1     ahoka 
    447        1.1     ahoka 	/*
    448        1.1     ahoka 	 * calculate the place of ecc data in oob
    449        1.1     ahoka 	 * we try to be compatible with Linux here
    450        1.1     ahoka 	 */
    451        1.1     ahoka 	switch (chip->nc_spare_size) {
    452        1.1     ahoka 	case 8:
    453        1.1     ahoka 		ecc->necc_offset = 0;
    454        1.1     ahoka 		break;
    455        1.1     ahoka 	case 16:
    456        1.1     ahoka 		ecc->necc_offset = 0;
    457        1.1     ahoka 		break;
    458       1.17      matt 	case 32:
    459       1.17      matt 		ecc->necc_offset = 0;
    460       1.17      matt 		break;
    461        1.1     ahoka 	case 64:
    462        1.1     ahoka 		ecc->necc_offset = 40;
    463        1.1     ahoka 		break;
    464        1.1     ahoka 	case 128:
    465        1.1     ahoka 		ecc->necc_offset = 80;
    466        1.1     ahoka 		break;
    467        1.1     ahoka 	default:
    468       1.19     ahoka 		panic("OOB size %" PRIu32 " is unexpected", chip->nc_spare_size);
    469        1.1     ahoka 	}
    470        1.1     ahoka 
    471        1.1     ahoka 	ecc->necc_steps = chip->nc_page_size / ecc->necc_block_size;
    472        1.1     ahoka 	ecc->necc_size = ecc->necc_steps * ecc->necc_code_size;
    473        1.1     ahoka 
    474        1.1     ahoka 	/* check if we fit in oob */
    475        1.1     ahoka 	if (ecc->necc_offset + ecc->necc_size > chip->nc_spare_size) {
    476        1.1     ahoka 		panic("NAND ECC bits dont fit in OOB");
    477        1.1     ahoka 	}
    478        1.1     ahoka 
    479        1.1     ahoka 	/* TODO: mark free oob area available for file systems */
    480        1.1     ahoka 
    481        1.1     ahoka 	chip->nc_ecc_cache = kmem_zalloc(ecc->necc_size, KM_SLEEP);
    482        1.1     ahoka 
    483        1.1     ahoka 	/*
    484        1.1     ahoka 	 * calculate badblock marker offset in oob
    485        1.1     ahoka 	 * we try to be compatible with linux here
    486        1.1     ahoka 	 */
    487        1.1     ahoka 	if (chip->nc_page_size > 512)
    488        1.1     ahoka 		chip->nc_badmarker_offs = 0;
    489        1.1     ahoka 	else
    490        1.1     ahoka 		chip->nc_badmarker_offs = 5;
    491        1.1     ahoka 
    492        1.1     ahoka 	/* Calculate page shift and mask */
    493        1.1     ahoka 	chip->nc_page_shift = ffs(chip->nc_page_size) - 1;
    494        1.1     ahoka 	chip->nc_page_mask = ~(chip->nc_page_size - 1);
    495        1.1     ahoka 	/* same for block */
    496        1.1     ahoka 	chip->nc_block_shift = ffs(chip->nc_block_size) - 1;
    497        1.1     ahoka 	chip->nc_block_mask = ~(chip->nc_block_size - 1);
    498        1.1     ahoka 
    499        1.1     ahoka 	/* look for quirks here if needed in future */
    500        1.1     ahoka 	/* nand_quirks(self, chip); */
    501        1.1     ahoka 
    502        1.1     ahoka 	return 0;
    503        1.1     ahoka }
    504        1.1     ahoka 
    505        1.6     ahoka void
    506        1.6     ahoka nand_read_id(device_t self, uint8_t *manf, uint8_t *dev)
    507        1.1     ahoka {
    508        1.1     ahoka 	nand_select(self, true);
    509        1.1     ahoka 	nand_command(self, ONFI_READ_ID);
    510        1.1     ahoka 	nand_address(self, 0x00);
    511       1.11     rmind 
    512       1.12     ahoka 	nand_read_1(self, manf);
    513       1.12     ahoka 	nand_read_1(self, dev);
    514       1.11     rmind 
    515        1.1     ahoka 	nand_select(self, false);
    516        1.1     ahoka }
    517        1.1     ahoka 
    518        1.6     ahoka int
    519        1.6     ahoka nand_read_parameter_page(device_t self, struct onfi_parameter_page *params)
    520        1.1     ahoka {
    521        1.1     ahoka 	uint8_t *bufp;
    522        1.1     ahoka 	uint16_t crc;
    523        1.6     ahoka 	int i;//, tries = 0;
    524        1.1     ahoka 
    525        1.6     ahoka 	KASSERT(sizeof(*params) == 256);
    526        1.1     ahoka 
    527        1.6     ahoka //read_params:
    528        1.6     ahoka //	tries++;
    529       1.12     ahoka 
    530        1.1     ahoka 	nand_select(self, true);
    531        1.1     ahoka 	nand_command(self, ONFI_READ_PARAMETER_PAGE);
    532        1.1     ahoka 	nand_address(self, 0x00);
    533        1.1     ahoka 
    534        1.1     ahoka 	nand_busy(self);
    535        1.1     ahoka 
    536        1.6     ahoka 	/* TODO check the signature if it contains at least 2 letters */
    537        1.6     ahoka 
    538        1.6     ahoka 	bufp = (uint8_t *)params;
    539        1.6     ahoka 	/* XXX why i am not using read_buf? */
    540        1.1     ahoka 	for (i = 0; i < 256; i++) {
    541       1.12     ahoka 		nand_read_1(self, &bufp[i]);
    542        1.1     ahoka 	}
    543        1.1     ahoka 	nand_select(self, false);
    544       1.12     ahoka 
    545        1.1     ahoka 	/* validate the parameter page with the crc */
    546        1.1     ahoka 	crc = nand_crc16(bufp, 254);
    547        1.1     ahoka 
    548        1.6     ahoka 	if (crc != params->param_integrity_crc) {
    549        1.1     ahoka 		aprint_error_dev(self, "parameter page crc check failed\n");
    550        1.1     ahoka 		/* TODO: we should read the next parameter page copy */
    551        1.6     ahoka 		return 1;
    552        1.6     ahoka 	}
    553        1.6     ahoka 
    554        1.6     ahoka 	return 0;
    555        1.6     ahoka }
    556        1.6     ahoka 
    557        1.6     ahoka static int
    558        1.6     ahoka nand_fill_chip_structure(device_t self, struct nand_chip *chip)
    559        1.6     ahoka {
    560        1.6     ahoka 	struct onfi_parameter_page params;
    561        1.6     ahoka 	uint8_t	vendor[13], model[21];
    562        1.6     ahoka 	int i;
    563        1.6     ahoka 
    564        1.6     ahoka 	if (nand_read_parameter_page(self, &params)) {
    565        1.6     ahoka 		return 1;
    566        1.1     ahoka 	}
    567        1.1     ahoka 
    568        1.1     ahoka 	/* strip manufacturer and model string */
    569        1.1     ahoka 	strlcpy(vendor, params.param_manufacturer, sizeof(vendor));
    570        1.1     ahoka 	for (i = 11; i > 0 && vendor[i] == ' '; i--)
    571        1.1     ahoka 		vendor[i] = 0;
    572        1.1     ahoka 	strlcpy(model, params.param_model, sizeof(model));
    573        1.1     ahoka 	for (i = 19; i > 0 && model[i] == ' '; i--)
    574        1.1     ahoka 		model[i] = 0;
    575        1.1     ahoka 
    576        1.1     ahoka 	aprint_normal_dev(self, "vendor: %s, model: %s\n", vendor, model);
    577        1.1     ahoka 
    578       1.22     ahoka 	chip->nc_page_size = le32toh(params.param_pagesize);
    579       1.22     ahoka 	chip->nc_block_size =
    580       1.22     ahoka 	    le32toh(params.param_blocksize) * chip->nc_page_size;
    581       1.22     ahoka 	chip->nc_spare_size = le16toh(params.param_sparesize);
    582       1.22     ahoka 	chip->nc_lun_blocks = le32toh(params.param_lunsize);
    583       1.22     ahoka 	chip->nc_num_luns = params.param_numluns;
    584        1.1     ahoka 
    585       1.22     ahoka 	chip->nc_size =
    586       1.22     ahoka 	    chip->nc_block_size * chip->nc_lun_blocks * chip->nc_num_luns;
    587        1.1     ahoka 
    588        1.1     ahoka 	/* the lower 4 bits contain the row address cycles */
    589        1.1     ahoka 	chip->nc_addr_cycles_row = params.param_addr_cycles & 0x07;
    590        1.1     ahoka 	/* the upper 4 bits contain the column address cycles */
    591        1.1     ahoka 	chip->nc_addr_cycles_column = (params.param_addr_cycles & ~0x07) >> 4;
    592        1.1     ahoka 
    593       1.22     ahoka 	uint16_t features = le16toh(params.param_features);
    594       1.22     ahoka 	if (features & ONFI_FEATURE_16BIT) {
    595        1.1     ahoka 		chip->nc_flags |= NC_BUSWIDTH_16;
    596       1.22     ahoka 	}
    597        1.1     ahoka 
    598       1.22     ahoka 	if (features & ONFI_FEATURE_EXTENDED_PARAM) {
    599        1.1     ahoka 		chip->nc_flags |= NC_EXTENDED_PARAM;
    600       1.22     ahoka 	}
    601        1.6     ahoka 
    602        1.6     ahoka 	return 0;
    603        1.1     ahoka }
    604        1.1     ahoka 
    605        1.1     ahoka /* ARGSUSED */
    606        1.1     ahoka bool
    607        1.1     ahoka nand_shutdown(device_t self, int howto)
    608        1.1     ahoka {
    609        1.1     ahoka 	return true;
    610        1.1     ahoka }
    611        1.1     ahoka 
    612        1.1     ahoka static void
    613        1.1     ahoka nand_address_column(device_t self, size_t row, size_t column)
    614        1.1     ahoka {
    615        1.1     ahoka 	struct nand_softc *sc = device_private(self);
    616        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
    617        1.1     ahoka 	uint8_t i;
    618        1.1     ahoka 
    619       1.19     ahoka 	DPRINTF(("addressing row: 0x%jx column: %" PRIu32 "\n",
    620        1.1     ahoka 		(uintmax_t )row, column));
    621        1.1     ahoka 
    622        1.1     ahoka 	/* XXX TODO */
    623        1.1     ahoka 	row >>= chip->nc_page_shift;
    624        1.1     ahoka 
    625        1.1     ahoka 	/* Write the column (subpage) address */
    626        1.1     ahoka 	if (chip->nc_flags & NC_BUSWIDTH_16)
    627        1.1     ahoka 		column >>= 1;
    628        1.1     ahoka 	for (i = 0; i < chip->nc_addr_cycles_column; i++, column >>= 8)
    629        1.1     ahoka 		nand_address(self, column & 0xff);
    630        1.1     ahoka 
    631        1.1     ahoka 	/* Write the row (page) address */
    632        1.1     ahoka 	for (i = 0; i < chip->nc_addr_cycles_row; i++, row >>= 8)
    633        1.1     ahoka 		nand_address(self, row & 0xff);
    634        1.1     ahoka }
    635        1.1     ahoka 
    636        1.1     ahoka static void
    637        1.1     ahoka nand_address_row(device_t self, size_t row)
    638        1.1     ahoka {
    639        1.1     ahoka 	struct nand_softc *sc = device_private(self);
    640        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
    641        1.7     ahoka 	int i;
    642        1.1     ahoka 
    643        1.1     ahoka 	/* XXX TODO */
    644        1.1     ahoka 	row >>= chip->nc_page_shift;
    645        1.1     ahoka 
    646        1.1     ahoka 	/* Write the row (page) address */
    647        1.1     ahoka 	for (i = 0; i < chip->nc_addr_cycles_row; i++, row >>= 8)
    648        1.1     ahoka 		nand_address(self, row & 0xff);
    649        1.1     ahoka }
    650        1.1     ahoka 
    651        1.1     ahoka static inline uint8_t
    652        1.1     ahoka nand_get_status(device_t self)
    653        1.1     ahoka {
    654        1.1     ahoka 	uint8_t status;
    655        1.1     ahoka 
    656        1.1     ahoka 	nand_command(self, ONFI_READ_STATUS);
    657        1.1     ahoka 	nand_busy(self);
    658       1.12     ahoka 	nand_read_1(self, &status);
    659        1.1     ahoka 
    660        1.1     ahoka 	return status;
    661        1.1     ahoka }
    662        1.1     ahoka 
    663        1.1     ahoka static bool
    664        1.1     ahoka nand_check_wp(device_t self)
    665        1.1     ahoka {
    666       1.25  kiyohara 	if (nand_get_status(self) & ONFI_STATUS_WP)
    667        1.1     ahoka 		return false;
    668        1.1     ahoka 	else
    669        1.1     ahoka 		return true;
    670        1.1     ahoka }
    671        1.1     ahoka 
    672        1.1     ahoka static void
    673        1.7     ahoka nand_prepare_read(device_t self, flash_off_t row, flash_off_t column)
    674        1.1     ahoka {
    675        1.1     ahoka 	nand_command(self, ONFI_READ);
    676        1.1     ahoka 	nand_address_column(self, row, column);
    677        1.1     ahoka 	nand_command(self, ONFI_READ_START);
    678        1.1     ahoka 
    679        1.1     ahoka 	nand_busy(self);
    680        1.1     ahoka }
    681        1.1     ahoka 
    682        1.6     ahoka /* read a page with ecc correction, default implementation */
    683        1.1     ahoka int
    684        1.6     ahoka nand_default_read_page(device_t self, size_t offset, uint8_t *data)
    685        1.1     ahoka {
    686        1.1     ahoka 	struct nand_softc *sc = device_private(self);
    687        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
    688        1.1     ahoka 	size_t b, bs, e, cs;
    689        1.1     ahoka 	uint8_t *ecc;
    690        1.1     ahoka 	int result;
    691        1.1     ahoka 
    692        1.1     ahoka 	nand_prepare_read(self, offset, 0);
    693        1.1     ahoka 
    694        1.1     ahoka 	bs = chip->nc_ecc->necc_block_size;
    695        1.1     ahoka 	cs = chip->nc_ecc->necc_code_size;
    696        1.1     ahoka 
    697        1.1     ahoka 	/* decide if we access by 8 or 16 bits */
    698        1.1     ahoka 	if (chip->nc_flags & NC_BUSWIDTH_16) {
    699        1.1     ahoka 		for (b = 0, e = 0; b < chip->nc_page_size; b += bs, e += cs) {
    700        1.1     ahoka 			nand_ecc_prepare(self, NAND_ECC_READ);
    701       1.12     ahoka 			nand_read_buf_2(self, data + b, bs);
    702        1.1     ahoka 			nand_ecc_compute(self, data + b,
    703        1.1     ahoka 			    chip->nc_ecc_cache + e);
    704        1.1     ahoka 		}
    705        1.1     ahoka 	} else {
    706        1.1     ahoka 		for (b = 0, e = 0; b < chip->nc_page_size; b += bs, e += cs) {
    707        1.1     ahoka 			nand_ecc_prepare(self, NAND_ECC_READ);
    708       1.12     ahoka 			nand_read_buf_1(self, data + b, bs);
    709        1.1     ahoka 			nand_ecc_compute(self, data + b,
    710        1.1     ahoka 			    chip->nc_ecc_cache + e);
    711        1.1     ahoka 		}
    712        1.1     ahoka 	}
    713        1.1     ahoka 
    714        1.2     ahoka 	/* for debugging new drivers */
    715        1.2     ahoka #if 0
    716        1.2     ahoka 	nand_dump_data("page", data, chip->nc_page_size);
    717        1.2     ahoka #endif
    718        1.1     ahoka 
    719        1.1     ahoka 	nand_read_oob(self, offset, chip->nc_oob_cache);
    720        1.1     ahoka 	ecc = chip->nc_oob_cache + chip->nc_ecc->necc_offset;
    721        1.1     ahoka 
    722        1.1     ahoka 	/* useful for debugging new ecc drivers */
    723        1.1     ahoka #if 0
    724        1.1     ahoka 	printf("dumping ecc %d\n--------------\n", chip->nc_ecc->necc_steps);
    725        1.1     ahoka 	for (e = 0; e < chip->nc_ecc->necc_steps; e++) {
    726        1.1     ahoka 		printf("0x");
    727        1.1     ahoka 		for (b = 0; b < cs; b++) {
    728        1.1     ahoka 			printf("%.2hhx", ecc[e+b]);
    729        1.1     ahoka 		}
    730        1.1     ahoka 		printf(" 0x");
    731        1.1     ahoka 		for (b = 0; b < cs; b++) {
    732        1.1     ahoka 			printf("%.2hhx", chip->nc_ecc_cache[e+b]);
    733        1.1     ahoka 		}
    734        1.1     ahoka 		printf("\n");
    735        1.1     ahoka 	}
    736        1.1     ahoka 	printf("--------------\n");
    737        1.1     ahoka #endif
    738        1.1     ahoka 
    739        1.1     ahoka 	for (b = 0, e = 0; b < chip->nc_page_size; b += bs, e += cs) {
    740        1.1     ahoka 		result = nand_ecc_correct(self, data + b, ecc + e,
    741        1.1     ahoka 		    chip->nc_ecc_cache + e);
    742        1.1     ahoka 
    743        1.1     ahoka 		switch (result) {
    744        1.1     ahoka 		case NAND_ECC_OK:
    745        1.1     ahoka 			break;
    746        1.1     ahoka 		case NAND_ECC_CORRECTED:
    747        1.1     ahoka 			aprint_error_dev(self,
    748        1.1     ahoka 			    "data corrected with ECC at page offset 0x%jx "
    749       1.20  pgoyette 			    "block %zu\n", (uintmax_t)offset, b);
    750        1.1     ahoka 			break;
    751        1.1     ahoka 		case NAND_ECC_TWOBIT:
    752        1.1     ahoka 			aprint_error_dev(self,
    753        1.1     ahoka 			    "uncorrectable ECC error at page offset 0x%jx "
    754       1.20  pgoyette 			    "block %zu\n", (uintmax_t)offset, b);
    755        1.1     ahoka 			return EIO;
    756        1.1     ahoka 			break;
    757        1.1     ahoka 		case NAND_ECC_INVALID:
    758        1.1     ahoka 			aprint_error_dev(self,
    759        1.1     ahoka 			    "invalid ECC in oob at page offset 0x%jx "
    760       1.20  pgoyette 			    "block %zu\n", (uintmax_t)offset, b);
    761        1.1     ahoka 			return EIO;
    762        1.1     ahoka 			break;
    763        1.1     ahoka 		default:
    764        1.1     ahoka 			panic("invalid ECC correction errno");
    765        1.1     ahoka 		}
    766        1.1     ahoka 	}
    767        1.1     ahoka 
    768        1.1     ahoka 	return 0;
    769        1.1     ahoka }
    770        1.1     ahoka 
    771        1.6     ahoka int
    772        1.6     ahoka nand_default_program_page(device_t self, size_t page, const uint8_t *data)
    773        1.1     ahoka {
    774        1.1     ahoka 	struct nand_softc *sc = device_private(self);
    775        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
    776        1.1     ahoka 	size_t bs, cs, e, b;
    777        1.1     ahoka 	uint8_t status;
    778        1.1     ahoka 	uint8_t *ecc;
    779        1.1     ahoka 
    780        1.1     ahoka 	nand_command(self, ONFI_PAGE_PROGRAM);
    781        1.1     ahoka 	nand_address_column(self, page, 0);
    782        1.1     ahoka 
    783        1.1     ahoka 	nand_busy(self);
    784        1.1     ahoka 
    785        1.1     ahoka 	bs = chip->nc_ecc->necc_block_size;
    786        1.1     ahoka 	cs = chip->nc_ecc->necc_code_size;
    787        1.1     ahoka 	ecc = chip->nc_oob_cache + chip->nc_ecc->necc_offset;
    788        1.1     ahoka 
    789        1.1     ahoka 	/* XXX code duplication */
    790        1.1     ahoka 	/* decide if we access by 8 or 16 bits */
    791        1.1     ahoka 	if (chip->nc_flags & NC_BUSWIDTH_16) {
    792        1.1     ahoka 		for (b = 0, e = 0; b < chip->nc_page_size; b += bs, e += cs) {
    793        1.1     ahoka 			nand_ecc_prepare(self, NAND_ECC_WRITE);
    794       1.12     ahoka 			nand_write_buf_2(self, data + b, bs);
    795        1.1     ahoka 			nand_ecc_compute(self, data + b, ecc + e);
    796        1.1     ahoka 		}
    797        1.1     ahoka 		/* write oob with ecc correction code */
    798       1.12     ahoka 		nand_write_buf_2(self, chip->nc_oob_cache,
    799        1.1     ahoka 		    chip->nc_spare_size);
    800        1.1     ahoka 	} else {
    801        1.1     ahoka 		for (b = 0, e = 0; b < chip->nc_page_size; b += bs, e += cs) {
    802        1.1     ahoka 			nand_ecc_prepare(self, NAND_ECC_WRITE);
    803       1.12     ahoka 			nand_write_buf_1(self, data + b, bs);
    804        1.1     ahoka 			nand_ecc_compute(self, data + b, ecc + e);
    805        1.1     ahoka 		}
    806        1.1     ahoka 		/* write oob with ecc correction code */
    807       1.12     ahoka 		nand_write_buf_1(self, chip->nc_oob_cache,
    808        1.1     ahoka 		    chip->nc_spare_size);
    809        1.1     ahoka 	}
    810        1.1     ahoka 
    811        1.1     ahoka 	nand_command(self, ONFI_PAGE_PROGRAM_START);
    812        1.1     ahoka 
    813        1.1     ahoka 	nand_busy(self);
    814        1.1     ahoka 
    815        1.2     ahoka 	/* for debugging ecc */
    816        1.1     ahoka #if 0
    817        1.1     ahoka 	printf("dumping ecc %d\n--------------\n", chip->nc_ecc->necc_steps);
    818        1.1     ahoka 	for (e = 0; e < chip->nc_ecc->necc_steps; e++) {
    819        1.1     ahoka 		printf("0x");
    820        1.1     ahoka 		for (b = 0; b < cs; b++) {
    821        1.1     ahoka 			printf("%.2hhx", ecc[e+b]);
    822        1.1     ahoka 		}
    823        1.1     ahoka 		printf("\n");
    824        1.1     ahoka 	}
    825        1.1     ahoka 	printf("--------------\n");
    826        1.1     ahoka #endif
    827        1.1     ahoka 
    828        1.1     ahoka 	status = nand_get_status(self);
    829        1.1     ahoka 	KASSERT(status & ONFI_STATUS_RDY);
    830        1.1     ahoka 	if (status & ONFI_STATUS_FAIL) {
    831        1.1     ahoka 		aprint_error_dev(self, "page program failed!\n");
    832        1.1     ahoka 		return EIO;
    833        1.1     ahoka 	}
    834        1.1     ahoka 
    835        1.1     ahoka 	return 0;
    836        1.1     ahoka }
    837        1.1     ahoka 
    838        1.6     ahoka /* read the OOB of a page */
    839        1.1     ahoka int
    840        1.6     ahoka nand_read_oob(device_t self, size_t page, uint8_t *oob)
    841        1.1     ahoka {
    842        1.1     ahoka 	struct nand_softc *sc = device_private(self);
    843        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
    844        1.1     ahoka 
    845        1.1     ahoka 	nand_prepare_read(self, page, chip->nc_page_size);
    846        1.1     ahoka 
    847        1.1     ahoka 	if (chip->nc_flags & NC_BUSWIDTH_16)
    848       1.12     ahoka 		nand_read_buf_2(self, oob, chip->nc_spare_size);
    849        1.1     ahoka 	else
    850       1.12     ahoka 		nand_read_buf_1(self, oob, chip->nc_spare_size);
    851        1.1     ahoka 
    852        1.2     ahoka 	/* for debugging drivers */
    853        1.2     ahoka #if 0
    854        1.2     ahoka 	nand_dump_data("oob", oob, chip->nc_spare_size);
    855        1.2     ahoka #endif
    856        1.1     ahoka 
    857        1.1     ahoka 	return 0;
    858        1.1     ahoka }
    859        1.1     ahoka 
    860        1.1     ahoka static int
    861        1.1     ahoka nand_write_oob(device_t self, size_t offset, const void *oob)
    862        1.1     ahoka {
    863        1.1     ahoka 	struct nand_softc *sc = device_private(self);
    864        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
    865        1.1     ahoka 	uint8_t status;
    866        1.1     ahoka 
    867        1.1     ahoka 	nand_command(self, ONFI_PAGE_PROGRAM);
    868        1.1     ahoka 	nand_address_column(self, offset, chip->nc_page_size);
    869        1.1     ahoka 	nand_command(self, ONFI_PAGE_PROGRAM_START);
    870        1.1     ahoka 
    871        1.1     ahoka 	nand_busy(self);
    872        1.1     ahoka 
    873        1.1     ahoka 	if (chip->nc_flags & NC_BUSWIDTH_16)
    874       1.12     ahoka 		nand_write_buf_2(self, oob, chip->nc_spare_size);
    875        1.1     ahoka 	else
    876       1.12     ahoka 		nand_write_buf_1(self, oob, chip->nc_spare_size);
    877        1.1     ahoka 
    878        1.1     ahoka 	status = nand_get_status(self);
    879        1.1     ahoka 	KASSERT(status & ONFI_STATUS_RDY);
    880        1.1     ahoka 	if (status & ONFI_STATUS_FAIL)
    881        1.1     ahoka 		return EIO;
    882        1.1     ahoka 	else
    883        1.1     ahoka 		return 0;
    884        1.1     ahoka }
    885        1.1     ahoka 
    886        1.1     ahoka void
    887        1.1     ahoka nand_markbad(device_t self, size_t offset)
    888        1.1     ahoka {
    889        1.1     ahoka 	struct nand_softc *sc = device_private(self);
    890        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
    891       1.23  christos 	flash_off_t blockoffset;
    892        1.1     ahoka #ifdef NAND_BBT
    893        1.7     ahoka 	flash_off_t block;
    894        1.1     ahoka 
    895        1.1     ahoka 	block = offset / chip->nc_block_size;
    896        1.1     ahoka 
    897        1.1     ahoka 	nand_bbt_block_markbad(self, block);
    898        1.1     ahoka #endif
    899        1.1     ahoka 	blockoffset = offset & chip->nc_block_mask;
    900        1.1     ahoka 
    901        1.1     ahoka 	/* check if it is already marked bad */
    902        1.1     ahoka 	if (nand_isbad(self, blockoffset))
    903        1.1     ahoka 		return;
    904        1.1     ahoka 
    905        1.1     ahoka 	nand_read_oob(self, blockoffset, chip->nc_oob_cache);
    906        1.1     ahoka 
    907        1.1     ahoka 	chip->nc_oob_cache[chip->nc_badmarker_offs] = 0x00;
    908        1.1     ahoka 	chip->nc_oob_cache[chip->nc_badmarker_offs + 1] = 0x00;
    909        1.1     ahoka 
    910        1.1     ahoka 	nand_write_oob(self, blockoffset, chip->nc_oob_cache);
    911        1.1     ahoka }
    912        1.1     ahoka 
    913        1.1     ahoka bool
    914        1.7     ahoka nand_isfactorybad(device_t self, flash_off_t offset)
    915        1.1     ahoka {
    916        1.1     ahoka 	struct nand_softc *sc = device_private(self);
    917        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
    918        1.7     ahoka 	flash_off_t block, first_page, last_page, page;
    919        1.1     ahoka 	int i;
    920        1.1     ahoka 
    921        1.1     ahoka 	/* Check for factory bad blocks first
    922        1.1     ahoka 	 * Factory bad blocks are marked in the first or last
    923        1.1     ahoka 	 * page of the blocks, see: ONFI 2.2, 3.2.2.
    924        1.1     ahoka 	 */
    925        1.1     ahoka 	block = offset / chip->nc_block_size;
    926        1.1     ahoka 	first_page = block * chip->nc_block_size;
    927        1.1     ahoka 	last_page = (block + 1) * chip->nc_block_size
    928        1.1     ahoka 	    - chip->nc_page_size;
    929        1.1     ahoka 
    930        1.1     ahoka 	for (i = 0, page = first_page; i < 2; i++, page = last_page) {
    931        1.1     ahoka 		/* address OOB */
    932        1.1     ahoka 		nand_prepare_read(self, page, chip->nc_page_size);
    933        1.1     ahoka 
    934        1.1     ahoka 		if (chip->nc_flags & NC_BUSWIDTH_16) {
    935        1.1     ahoka 			uint16_t word;
    936       1.12     ahoka 			nand_read_2(self, &word);
    937        1.1     ahoka 			if (word == 0x0000)
    938        1.1     ahoka 				return true;
    939        1.1     ahoka 		} else {
    940        1.1     ahoka 			uint8_t byte;
    941       1.12     ahoka 			nand_read_1(self, &byte);
    942        1.1     ahoka 			if (byte == 0x00)
    943        1.1     ahoka 				return true;
    944        1.1     ahoka 		}
    945        1.1     ahoka 	}
    946        1.1     ahoka 
    947        1.1     ahoka 	return false;
    948        1.1     ahoka }
    949        1.1     ahoka 
    950        1.1     ahoka bool
    951        1.7     ahoka nand_iswornoutbad(device_t self, flash_off_t offset)
    952        1.1     ahoka {
    953        1.1     ahoka 	struct nand_softc *sc = device_private(self);
    954        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
    955        1.7     ahoka 	flash_off_t block;
    956        1.1     ahoka 
    957        1.1     ahoka 	/* we inspect the first page of the block */
    958        1.1     ahoka 	block = offset & chip->nc_block_mask;
    959        1.1     ahoka 
    960        1.1     ahoka 	/* Linux/u-boot compatible badblock handling */
    961        1.1     ahoka 	if (chip->nc_flags & NC_BUSWIDTH_16) {
    962        1.1     ahoka 		uint16_t word, mark;
    963        1.1     ahoka 
    964        1.1     ahoka 		nand_prepare_read(self, block,
    965        1.1     ahoka 		    chip->nc_page_size + (chip->nc_badmarker_offs & 0xfe));
    966        1.1     ahoka 
    967       1.12     ahoka 		nand_read_2(self, &word);
    968        1.1     ahoka 		mark = htole16(word);
    969        1.1     ahoka 		if (chip->nc_badmarker_offs & 0x01)
    970        1.1     ahoka 			mark >>= 8;
    971        1.1     ahoka 		if ((mark & 0xff) != 0xff)
    972        1.1     ahoka 			return true;
    973        1.1     ahoka 	} else {
    974        1.1     ahoka 		uint8_t byte;
    975        1.1     ahoka 
    976        1.1     ahoka 		nand_prepare_read(self, block,
    977        1.1     ahoka 		    chip->nc_page_size + chip->nc_badmarker_offs);
    978        1.1     ahoka 
    979       1.12     ahoka 		nand_read_1(self, &byte);
    980        1.1     ahoka 		if (byte != 0xff)
    981        1.1     ahoka 			return true;
    982        1.1     ahoka 	}
    983        1.1     ahoka 
    984        1.1     ahoka 	return false;
    985        1.1     ahoka }
    986        1.1     ahoka 
    987        1.1     ahoka bool
    988        1.7     ahoka nand_isbad(device_t self, flash_off_t offset)
    989        1.1     ahoka {
    990        1.1     ahoka #ifdef NAND_BBT
    991        1.1     ahoka 	struct nand_softc *sc = device_private(self);
    992        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
    993        1.7     ahoka 	flash_off_t block;
    994        1.1     ahoka 
    995        1.1     ahoka 	block = offset / chip->nc_block_size;
    996        1.1     ahoka 
    997        1.1     ahoka 	return nand_bbt_block_isbad(self, block);
    998        1.1     ahoka #else
    999        1.1     ahoka 	/* ONFI host requirement */
   1000        1.1     ahoka 	if (nand_isfactorybad(self, offset))
   1001        1.1     ahoka 		return true;
   1002        1.1     ahoka 
   1003        1.1     ahoka 	/* Look for Linux/U-Boot compatible bad marker */
   1004        1.1     ahoka 	if (nand_iswornoutbad(self, offset))
   1005        1.1     ahoka 		return true;
   1006        1.1     ahoka 
   1007        1.1     ahoka 	return false;
   1008        1.1     ahoka #endif
   1009        1.1     ahoka }
   1010        1.1     ahoka 
   1011        1.1     ahoka int
   1012        1.1     ahoka nand_erase_block(device_t self, size_t offset)
   1013        1.1     ahoka {
   1014        1.1     ahoka 	uint8_t status;
   1015        1.1     ahoka 
   1016        1.1     ahoka 	/* xxx calculate first page of block for address? */
   1017        1.1     ahoka 
   1018        1.1     ahoka 	nand_command(self, ONFI_BLOCK_ERASE);
   1019        1.1     ahoka 	nand_address_row(self, offset);
   1020        1.1     ahoka 	nand_command(self, ONFI_BLOCK_ERASE_START);
   1021        1.1     ahoka 
   1022        1.1     ahoka 	nand_busy(self);
   1023        1.1     ahoka 
   1024        1.1     ahoka 	status = nand_get_status(self);
   1025        1.1     ahoka 	KASSERT(status & ONFI_STATUS_RDY);
   1026        1.1     ahoka 	if (status & ONFI_STATUS_FAIL) {
   1027        1.1     ahoka 		aprint_error_dev(self, "block erase failed!\n");
   1028        1.1     ahoka 		nand_markbad(self, offset);
   1029        1.1     ahoka 		return EIO;
   1030        1.1     ahoka 	} else {
   1031        1.1     ahoka 		return 0;
   1032        1.1     ahoka 	}
   1033        1.1     ahoka }
   1034        1.1     ahoka 
   1035        1.1     ahoka /* default functions for driver development */
   1036        1.1     ahoka 
   1037        1.1     ahoka /* default ECC using hamming code of 256 byte chunks */
   1038        1.1     ahoka int
   1039        1.1     ahoka nand_default_ecc_compute(device_t self, const uint8_t *data, uint8_t *code)
   1040        1.1     ahoka {
   1041        1.1     ahoka 	hamming_compute_256(data, code);
   1042        1.1     ahoka 
   1043        1.1     ahoka 	return 0;
   1044        1.1     ahoka }
   1045        1.1     ahoka 
   1046        1.1     ahoka int
   1047        1.1     ahoka nand_default_ecc_correct(device_t self, uint8_t *data, const uint8_t *origcode,
   1048        1.1     ahoka 	const uint8_t *compcode)
   1049        1.1     ahoka {
   1050        1.1     ahoka 	return hamming_correct_256(data, origcode, compcode);
   1051        1.1     ahoka }
   1052        1.1     ahoka 
   1053        1.1     ahoka void
   1054        1.1     ahoka nand_default_select(device_t self, bool enable)
   1055        1.1     ahoka {
   1056        1.1     ahoka 	/* do nothing */
   1057        1.1     ahoka 	return;
   1058        1.1     ahoka }
   1059        1.1     ahoka 
   1060        1.1     ahoka /* implementation of the block device API */
   1061        1.1     ahoka 
   1062       1.14     ahoka int
   1063       1.15     cliff nand_flash_submit(device_t self, struct buf * const bp)
   1064       1.14     ahoka {
   1065       1.14     ahoka 	struct nand_softc *sc = device_private(self);
   1066       1.14     ahoka 
   1067       1.14     ahoka 	return flash_io_submit(&sc->sc_flash_io, bp);
   1068       1.14     ahoka }
   1069       1.14     ahoka 
   1070        1.1     ahoka /*
   1071        1.1     ahoka  * handle (page) unaligned write to nand
   1072        1.1     ahoka  */
   1073        1.1     ahoka static int
   1074        1.7     ahoka nand_flash_write_unaligned(device_t self, flash_off_t offset, size_t len,
   1075        1.1     ahoka     size_t *retlen, const uint8_t *buf)
   1076        1.1     ahoka {
   1077        1.1     ahoka 	struct nand_softc *sc = device_private(self);
   1078        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
   1079        1.7     ahoka 	flash_off_t first, last, firstoff;
   1080        1.1     ahoka 	const uint8_t *bufp;
   1081        1.7     ahoka 	flash_off_t addr;
   1082        1.1     ahoka 	size_t left, count;
   1083        1.8     ahoka 	int error = 0, i;
   1084        1.1     ahoka 
   1085        1.1     ahoka 	first = offset & chip->nc_page_mask;
   1086        1.1     ahoka 	firstoff = offset & ~chip->nc_page_mask;
   1087        1.1     ahoka 	/* XXX check if this should be len - 1 */
   1088        1.1     ahoka 	last = (offset + len) & chip->nc_page_mask;
   1089        1.1     ahoka 	count = last - first + 1;
   1090        1.1     ahoka 
   1091        1.1     ahoka 	addr = first;
   1092        1.1     ahoka 	*retlen = 0;
   1093        1.1     ahoka 
   1094        1.8     ahoka 	mutex_enter(&sc->sc_device_lock);
   1095        1.1     ahoka 	if (count == 1) {
   1096        1.1     ahoka 		if (nand_isbad(self, addr)) {
   1097        1.1     ahoka 			aprint_error_dev(self,
   1098        1.1     ahoka 			    "nand_flash_write_unaligned: "
   1099        1.1     ahoka 			    "bad block encountered\n");
   1100        1.8     ahoka 			error = EIO;
   1101        1.8     ahoka 			goto out;
   1102        1.1     ahoka 		}
   1103        1.1     ahoka 
   1104        1.1     ahoka 		error = nand_read_page(self, addr, chip->nc_page_cache);
   1105        1.8     ahoka 		if (error) {
   1106        1.8     ahoka 			goto out;
   1107        1.8     ahoka 		}
   1108        1.1     ahoka 
   1109        1.1     ahoka 		memcpy(chip->nc_page_cache + firstoff, buf, len);
   1110        1.1     ahoka 
   1111        1.1     ahoka 		error = nand_program_page(self, addr, chip->nc_page_cache);
   1112        1.8     ahoka 		if (error) {
   1113        1.8     ahoka 			goto out;
   1114        1.8     ahoka 		}
   1115        1.1     ahoka 
   1116        1.1     ahoka 		*retlen = len;
   1117        1.8     ahoka 		goto out;
   1118        1.1     ahoka 	}
   1119        1.1     ahoka 
   1120        1.1     ahoka 	bufp = buf;
   1121        1.1     ahoka 	left = len;
   1122        1.1     ahoka 
   1123        1.1     ahoka 	for (i = 0; i < count && left != 0; i++) {
   1124        1.1     ahoka 		if (nand_isbad(self, addr)) {
   1125        1.1     ahoka 			aprint_error_dev(self,
   1126        1.1     ahoka 			    "nand_flash_write_unaligned: "
   1127        1.1     ahoka 			    "bad block encountered\n");
   1128        1.8     ahoka 			error = EIO;
   1129        1.8     ahoka 			goto out;
   1130        1.1     ahoka 		}
   1131        1.1     ahoka 
   1132        1.1     ahoka 		if (i == 0) {
   1133        1.1     ahoka 			error = nand_read_page(self,
   1134        1.1     ahoka 			    addr, chip->nc_page_cache);
   1135        1.8     ahoka 			if (error) {
   1136        1.8     ahoka 				goto out;
   1137        1.8     ahoka 			}
   1138        1.1     ahoka 
   1139        1.1     ahoka 			memcpy(chip->nc_page_cache + firstoff,
   1140        1.1     ahoka 			    bufp, chip->nc_page_size - firstoff);
   1141       1.12     ahoka 
   1142        1.1     ahoka 			printf("program page: %s: %d\n", __FILE__, __LINE__);
   1143        1.1     ahoka 			error = nand_program_page(self,
   1144        1.1     ahoka 			    addr, chip->nc_page_cache);
   1145        1.8     ahoka 			if (error) {
   1146        1.8     ahoka 				goto out;
   1147        1.8     ahoka 			}
   1148        1.1     ahoka 
   1149        1.1     ahoka 			bufp += chip->nc_page_size - firstoff;
   1150        1.1     ahoka 			left -= chip->nc_page_size - firstoff;
   1151        1.1     ahoka 			*retlen += chip->nc_page_size - firstoff;
   1152        1.1     ahoka 
   1153        1.1     ahoka 		} else if (i == count - 1) {
   1154        1.1     ahoka 			error = nand_read_page(self,
   1155        1.1     ahoka 			    addr, chip->nc_page_cache);
   1156        1.8     ahoka 			if (error) {
   1157        1.8     ahoka 				goto out;
   1158        1.8     ahoka 			}
   1159        1.1     ahoka 
   1160        1.1     ahoka 			memcpy(chip->nc_page_cache, bufp, left);
   1161        1.1     ahoka 
   1162        1.1     ahoka 			error = nand_program_page(self,
   1163        1.1     ahoka 			    addr, chip->nc_page_cache);
   1164        1.8     ahoka 			if (error) {
   1165        1.8     ahoka 				goto out;
   1166        1.8     ahoka 			}
   1167        1.1     ahoka 
   1168        1.1     ahoka 			*retlen += left;
   1169        1.1     ahoka 			KASSERT(left < chip->nc_page_size);
   1170        1.1     ahoka 
   1171        1.1     ahoka 		} else {
   1172        1.1     ahoka 			/* XXX debug */
   1173        1.1     ahoka 			if (left > chip->nc_page_size) {
   1174       1.20  pgoyette 				printf("left: %zu, i: %d, count: %zu\n",
   1175       1.19     ahoka 				    left, i, count);
   1176        1.1     ahoka 			}
   1177        1.1     ahoka 			KASSERT(left > chip->nc_page_size);
   1178        1.1     ahoka 
   1179        1.1     ahoka 			error = nand_program_page(self, addr, bufp);
   1180        1.8     ahoka 			if (error) {
   1181        1.8     ahoka 				goto out;
   1182        1.8     ahoka 			}
   1183        1.1     ahoka 
   1184        1.1     ahoka 			bufp += chip->nc_page_size;
   1185        1.1     ahoka 			left -= chip->nc_page_size;
   1186        1.1     ahoka 			*retlen += chip->nc_page_size;
   1187        1.1     ahoka 		}
   1188        1.1     ahoka 
   1189        1.1     ahoka 		addr += chip->nc_page_size;
   1190        1.1     ahoka 	}
   1191        1.1     ahoka 
   1192        1.1     ahoka 	KASSERT(*retlen == len);
   1193        1.8     ahoka out:
   1194        1.8     ahoka 	mutex_exit(&sc->sc_device_lock);
   1195        1.1     ahoka 
   1196        1.8     ahoka 	return error;
   1197        1.1     ahoka }
   1198        1.1     ahoka 
   1199        1.1     ahoka int
   1200        1.7     ahoka nand_flash_write(device_t self, flash_off_t offset, size_t len, size_t *retlen,
   1201        1.1     ahoka     const uint8_t *buf)
   1202        1.1     ahoka {
   1203        1.1     ahoka 	struct nand_softc *sc = device_private(self);
   1204        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
   1205        1.1     ahoka 	const uint8_t *bufp;
   1206        1.1     ahoka 	size_t pages, page;
   1207        1.1     ahoka 	daddr_t addr;
   1208        1.1     ahoka 	int error = 0;
   1209        1.1     ahoka 
   1210        1.1     ahoka 	if ((offset + len) > chip->nc_size) {
   1211        1.1     ahoka 		DPRINTF(("nand_flash_write: write (off: 0x%jx, len: %ju),"
   1212        1.1     ahoka 			" is over device size (0x%jx)\n",
   1213        1.1     ahoka 			(uintmax_t)offset, (uintmax_t)len,
   1214        1.1     ahoka 			(uintmax_t)chip->nc_size));
   1215        1.1     ahoka 		return EINVAL;
   1216        1.1     ahoka 	}
   1217        1.1     ahoka 
   1218        1.1     ahoka 	if (len % chip->nc_page_size != 0 ||
   1219        1.1     ahoka 	    offset % chip->nc_page_size != 0) {
   1220        1.1     ahoka 		return nand_flash_write_unaligned(self,
   1221        1.1     ahoka 		    offset, len, retlen, buf);
   1222        1.1     ahoka 	}
   1223        1.1     ahoka 
   1224        1.1     ahoka 	pages = len / chip->nc_page_size;
   1225        1.1     ahoka 	KASSERT(pages != 0);
   1226        1.1     ahoka 	*retlen = 0;
   1227        1.1     ahoka 
   1228        1.1     ahoka 	addr = offset;
   1229        1.1     ahoka 	bufp = buf;
   1230        1.1     ahoka 
   1231        1.1     ahoka 	mutex_enter(&sc->sc_device_lock);
   1232        1.1     ahoka 	for (page = 0; page < pages; page++) {
   1233        1.1     ahoka 		/* do we need this check here? */
   1234        1.1     ahoka 		if (nand_isbad(self, addr)) {
   1235        1.1     ahoka 			aprint_error_dev(self,
   1236        1.1     ahoka 			    "nand_flash_write: bad block encountered\n");
   1237        1.1     ahoka 
   1238        1.1     ahoka 			error = EIO;
   1239        1.1     ahoka 			goto out;
   1240        1.1     ahoka 		}
   1241        1.1     ahoka 
   1242        1.1     ahoka 		error = nand_program_page(self, addr, bufp);
   1243        1.8     ahoka 		if (error) {
   1244        1.1     ahoka 			goto out;
   1245        1.8     ahoka 		}
   1246        1.1     ahoka 
   1247        1.1     ahoka 		addr += chip->nc_page_size;
   1248        1.1     ahoka 		bufp += chip->nc_page_size;
   1249        1.1     ahoka 		*retlen += chip->nc_page_size;
   1250        1.1     ahoka 	}
   1251        1.1     ahoka out:
   1252        1.1     ahoka 	mutex_exit(&sc->sc_device_lock);
   1253       1.19     ahoka 	DPRINTF(("page programming: retlen: %" PRIu32 ", len: %" PRIu32 "\n", *retlen, len));
   1254        1.1     ahoka 
   1255        1.1     ahoka 	return error;
   1256        1.1     ahoka }
   1257        1.1     ahoka 
   1258        1.1     ahoka /*
   1259        1.1     ahoka  * handle (page) unaligned read from nand
   1260        1.1     ahoka  */
   1261        1.1     ahoka static int
   1262        1.1     ahoka nand_flash_read_unaligned(device_t self, size_t offset,
   1263        1.1     ahoka     size_t len, size_t *retlen, uint8_t *buf)
   1264        1.1     ahoka {
   1265        1.1     ahoka 	struct nand_softc *sc = device_private(self);
   1266        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
   1267        1.1     ahoka 	daddr_t first, last, count, firstoff;
   1268        1.1     ahoka 	uint8_t *bufp;
   1269        1.1     ahoka 	daddr_t addr;
   1270        1.1     ahoka 	size_t left;
   1271        1.1     ahoka 	int error = 0, i;
   1272        1.1     ahoka 
   1273        1.1     ahoka 	first = offset & chip->nc_page_mask;
   1274        1.1     ahoka 	firstoff = offset & ~chip->nc_page_mask;
   1275        1.1     ahoka 	last = (offset + len) & chip->nc_page_mask;
   1276        1.1     ahoka 	count = (last - first) / chip->nc_page_size + 1;
   1277        1.1     ahoka 
   1278        1.1     ahoka 	addr = first;
   1279        1.1     ahoka 	bufp = buf;
   1280        1.1     ahoka 	left = len;
   1281        1.1     ahoka 	*retlen = 0;
   1282        1.1     ahoka 
   1283        1.1     ahoka 	mutex_enter(&sc->sc_device_lock);
   1284        1.1     ahoka 	if (count == 1) {
   1285        1.1     ahoka 		error = nand_read_page(self, addr, chip->nc_page_cache);
   1286        1.8     ahoka 		if (error) {
   1287        1.1     ahoka 			goto out;
   1288        1.8     ahoka 		}
   1289        1.1     ahoka 
   1290        1.1     ahoka 		memcpy(bufp, chip->nc_page_cache + firstoff, len);
   1291        1.1     ahoka 
   1292        1.1     ahoka 		*retlen = len;
   1293        1.1     ahoka 		goto out;
   1294        1.1     ahoka 	}
   1295        1.1     ahoka 
   1296        1.1     ahoka 	for (i = 0; i < count && left != 0; i++) {
   1297        1.1     ahoka 		error = nand_read_page(self, addr, chip->nc_page_cache);
   1298        1.8     ahoka 		if (error) {
   1299        1.1     ahoka 			goto out;
   1300        1.8     ahoka 		}
   1301        1.1     ahoka 
   1302        1.1     ahoka 		if (i == 0) {
   1303        1.1     ahoka 			memcpy(bufp, chip->nc_page_cache + firstoff,
   1304        1.1     ahoka 			    chip->nc_page_size - firstoff);
   1305        1.1     ahoka 
   1306        1.1     ahoka 			bufp += chip->nc_page_size - firstoff;
   1307        1.1     ahoka 			left -= chip->nc_page_size - firstoff;
   1308        1.1     ahoka 			*retlen += chip->nc_page_size - firstoff;
   1309        1.1     ahoka 
   1310        1.1     ahoka 		} else if (i == count - 1) {
   1311        1.1     ahoka 			memcpy(bufp, chip->nc_page_cache, left);
   1312        1.1     ahoka 			*retlen += left;
   1313        1.1     ahoka 			KASSERT(left < chip->nc_page_size);
   1314        1.1     ahoka 
   1315        1.1     ahoka 		} else {
   1316        1.1     ahoka 			memcpy(bufp, chip->nc_page_cache, chip->nc_page_size);
   1317        1.1     ahoka 
   1318        1.1     ahoka 			bufp += chip->nc_page_size;
   1319        1.1     ahoka 			left -= chip->nc_page_size;
   1320        1.1     ahoka 			*retlen += chip->nc_page_size;
   1321        1.1     ahoka 		}
   1322        1.1     ahoka 
   1323        1.1     ahoka 		addr += chip->nc_page_size;
   1324        1.1     ahoka 	}
   1325        1.1     ahoka 	KASSERT(*retlen == len);
   1326        1.1     ahoka out:
   1327        1.1     ahoka 	mutex_exit(&sc->sc_device_lock);
   1328        1.1     ahoka 
   1329        1.1     ahoka 	return error;
   1330        1.1     ahoka }
   1331        1.1     ahoka 
   1332        1.1     ahoka int
   1333        1.7     ahoka nand_flash_read(device_t self, flash_off_t offset, size_t len, size_t *retlen,
   1334        1.1     ahoka     uint8_t *buf)
   1335        1.1     ahoka {
   1336        1.1     ahoka 	struct nand_softc *sc = device_private(self);
   1337        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
   1338        1.1     ahoka 	uint8_t *bufp;
   1339        1.1     ahoka 	size_t addr;
   1340        1.1     ahoka 	size_t i, pages;
   1341        1.1     ahoka 	int error = 0;
   1342        1.1     ahoka 
   1343        1.1     ahoka 	*retlen = 0;
   1344        1.1     ahoka 
   1345       1.19     ahoka 	DPRINTF(("nand_flash_read: off: 0x%jx, len: %" PRIu32 "\n",
   1346        1.1     ahoka 		(uintmax_t)offset, len));
   1347        1.1     ahoka 
   1348        1.1     ahoka 	if (__predict_false((offset + len) > chip->nc_size)) {
   1349       1.19     ahoka 		DPRINTF(("nand_flash_read: read (off: 0x%jx, len: %" PRIu32 "),"
   1350        1.1     ahoka 			" is over device size (%ju)\n", (uintmax_t)offset,
   1351        1.1     ahoka 			len, (uintmax_t)chip->nc_size));
   1352        1.1     ahoka 		return EINVAL;
   1353        1.1     ahoka 	}
   1354        1.1     ahoka 
   1355        1.1     ahoka 	/* Handle unaligned access, shouldnt be needed when using the
   1356        1.1     ahoka 	 * block device, as strategy handles it, so only low level
   1357        1.1     ahoka 	 * accesses will use this path
   1358        1.1     ahoka 	 */
   1359        1.2     ahoka 	/* XXX^2 */
   1360        1.2     ahoka #if 0
   1361        1.2     ahoka 	if (len < chip->nc_page_size)
   1362        1.2     ahoka 		panic("TODO page size is larger than read size");
   1363        1.2     ahoka #endif
   1364        1.1     ahoka 
   1365        1.1     ahoka 	if (len % chip->nc_page_size != 0 ||
   1366        1.1     ahoka 	    offset % chip->nc_page_size != 0) {
   1367        1.1     ahoka 		return nand_flash_read_unaligned(self,
   1368        1.1     ahoka 		    offset, len, retlen, buf);
   1369        1.1     ahoka 	}
   1370        1.1     ahoka 
   1371        1.1     ahoka 	bufp = buf;
   1372        1.1     ahoka 	addr = offset;
   1373        1.1     ahoka 	pages = len / chip->nc_page_size;
   1374        1.1     ahoka 
   1375        1.1     ahoka 	mutex_enter(&sc->sc_device_lock);
   1376        1.1     ahoka 	for (i = 0; i < pages; i++) {
   1377        1.2     ahoka 		/* XXX do we need this check here? */
   1378        1.1     ahoka 		if (nand_isbad(self, addr)) {
   1379        1.1     ahoka 			aprint_error_dev(self, "bad block encountered\n");
   1380        1.1     ahoka 			error = EIO;
   1381        1.1     ahoka 			goto out;
   1382        1.1     ahoka 		}
   1383        1.1     ahoka 		error = nand_read_page(self, addr, bufp);
   1384        1.1     ahoka 		if (error)
   1385        1.1     ahoka 			goto out;
   1386        1.1     ahoka 
   1387        1.1     ahoka 		bufp += chip->nc_page_size;
   1388        1.1     ahoka 		addr += chip->nc_page_size;
   1389        1.1     ahoka 		*retlen += chip->nc_page_size;
   1390        1.1     ahoka 	}
   1391        1.1     ahoka out:
   1392        1.1     ahoka 	mutex_exit(&sc->sc_device_lock);
   1393        1.1     ahoka 
   1394        1.1     ahoka 	return error;
   1395        1.1     ahoka }
   1396        1.1     ahoka 
   1397        1.1     ahoka int
   1398       1.16    martin nand_flash_isbad(device_t self, flash_off_t ofs, bool *is_bad)
   1399        1.1     ahoka {
   1400        1.1     ahoka 	struct nand_softc *sc = device_private(self);
   1401        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
   1402        1.1     ahoka 	bool result;
   1403        1.1     ahoka 
   1404        1.1     ahoka 	if (ofs > chip->nc_size) {
   1405        1.1     ahoka 		DPRINTF(("nand_flash_isbad: offset 0x%jx is larger than"
   1406        1.1     ahoka 			" device size (0x%jx)\n", (uintmax_t)ofs,
   1407        1.1     ahoka 			(uintmax_t)chip->nc_size));
   1408        1.1     ahoka 		return EINVAL;
   1409        1.1     ahoka 	}
   1410        1.1     ahoka 
   1411        1.1     ahoka 	if (ofs % chip->nc_block_size != 0) {
   1412       1.15     cliff 		DPRINTF(("offset (0x%jx) is not a multiple of block size "
   1413        1.7     ahoka 			"(%ju)",
   1414        1.7     ahoka 			(uintmax_t)ofs, (uintmax_t)chip->nc_block_size));
   1415        1.7     ahoka 		return EINVAL;
   1416        1.1     ahoka 	}
   1417        1.1     ahoka 
   1418        1.1     ahoka 	mutex_enter(&sc->sc_device_lock);
   1419        1.1     ahoka 	result = nand_isbad(self, ofs);
   1420        1.1     ahoka 	mutex_exit(&sc->sc_device_lock);
   1421        1.1     ahoka 
   1422       1.16    martin 	*is_bad = result;
   1423       1.11     rmind 
   1424        1.7     ahoka 	return 0;
   1425        1.1     ahoka }
   1426        1.1     ahoka 
   1427        1.1     ahoka int
   1428        1.7     ahoka nand_flash_markbad(device_t self, flash_off_t ofs)
   1429        1.1     ahoka {
   1430        1.1     ahoka 	struct nand_softc *sc = device_private(self);
   1431        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
   1432        1.1     ahoka 
   1433        1.1     ahoka 	if (ofs > chip->nc_size) {
   1434        1.1     ahoka 		DPRINTF(("nand_flash_markbad: offset 0x%jx is larger than"
   1435        1.1     ahoka 			" device size (0x%jx)\n", ofs,
   1436        1.1     ahoka 			(uintmax_t)chip->nc_size));
   1437        1.1     ahoka 		return EINVAL;
   1438        1.1     ahoka 	}
   1439        1.1     ahoka 
   1440        1.1     ahoka 	if (ofs % chip->nc_block_size != 0) {
   1441       1.15     cliff 		panic("offset (%ju) is not a multiple of block size (%ju)",
   1442        1.1     ahoka 		    (uintmax_t)ofs, (uintmax_t)chip->nc_block_size);
   1443        1.1     ahoka 	}
   1444        1.1     ahoka 
   1445        1.1     ahoka 	mutex_enter(&sc->sc_device_lock);
   1446        1.1     ahoka 	nand_markbad(self, ofs);
   1447        1.1     ahoka 	mutex_exit(&sc->sc_device_lock);
   1448        1.1     ahoka 
   1449        1.1     ahoka 	return 0;
   1450        1.1     ahoka }
   1451        1.1     ahoka 
   1452        1.1     ahoka int
   1453        1.1     ahoka nand_flash_erase(device_t self,
   1454        1.1     ahoka     struct flash_erase_instruction *ei)
   1455        1.1     ahoka {
   1456        1.1     ahoka 	struct nand_softc *sc = device_private(self);
   1457        1.1     ahoka 	struct nand_chip *chip = &sc->sc_chip;
   1458        1.7     ahoka 	flash_off_t addr;
   1459        1.8     ahoka 	int error = 0;
   1460        1.1     ahoka 
   1461        1.1     ahoka 	if (ei->ei_addr < 0 || ei->ei_len < chip->nc_block_size)
   1462        1.1     ahoka 		return EINVAL;
   1463        1.1     ahoka 
   1464        1.1     ahoka 	if (ei->ei_addr + ei->ei_len > chip->nc_size) {
   1465        1.1     ahoka 		DPRINTF(("nand_flash_erase: erase address is over the end"
   1466        1.1     ahoka 			" of the device\n"));
   1467        1.1     ahoka 		return EINVAL;
   1468        1.1     ahoka 	}
   1469        1.1     ahoka 
   1470        1.1     ahoka 	if (ei->ei_addr % chip->nc_block_size != 0) {
   1471        1.1     ahoka 		aprint_error_dev(self,
   1472        1.1     ahoka 		    "nand_flash_erase: ei_addr (%ju) is not"
   1473       1.15     cliff 		    " a multiple of block size (%ju)",
   1474        1.1     ahoka 		    (uintmax_t)ei->ei_addr,
   1475        1.1     ahoka 		    (uintmax_t)chip->nc_block_size);
   1476        1.1     ahoka 		return EINVAL;
   1477        1.1     ahoka 	}
   1478        1.1     ahoka 
   1479        1.1     ahoka 	if (ei->ei_len % chip->nc_block_size != 0) {
   1480        1.1     ahoka 		aprint_error_dev(self,
   1481        1.1     ahoka 		    "nand_flash_erase: ei_len (%ju) is not"
   1482       1.15     cliff 		    " a multiple of block size (%ju)",
   1483       1.15     cliff 		    (uintmax_t)ei->ei_len,
   1484        1.1     ahoka 		    (uintmax_t)chip->nc_block_size);
   1485        1.1     ahoka 		return EINVAL;
   1486        1.1     ahoka 	}
   1487        1.1     ahoka 
   1488        1.1     ahoka 	mutex_enter(&sc->sc_device_lock);
   1489        1.1     ahoka 	addr = ei->ei_addr;
   1490        1.1     ahoka 	while (addr < ei->ei_addr + ei->ei_len) {
   1491        1.1     ahoka 		if (nand_isbad(self, addr)) {
   1492        1.1     ahoka 			aprint_error_dev(self, "bad block encountered\n");
   1493        1.1     ahoka 			ei->ei_state = FLASH_ERASE_FAILED;
   1494        1.8     ahoka 			error = EIO;
   1495        1.8     ahoka 			goto out;
   1496        1.1     ahoka 		}
   1497        1.1     ahoka 
   1498        1.1     ahoka 		error = nand_erase_block(self, addr);
   1499        1.1     ahoka 		if (error) {
   1500        1.1     ahoka 			ei->ei_state = FLASH_ERASE_FAILED;
   1501        1.8     ahoka 			goto out;
   1502        1.1     ahoka 		}
   1503        1.1     ahoka 
   1504        1.1     ahoka 		addr += chip->nc_block_size;
   1505        1.1     ahoka 	}
   1506        1.1     ahoka 	mutex_exit(&sc->sc_device_lock);
   1507        1.1     ahoka 
   1508        1.1     ahoka 	ei->ei_state = FLASH_ERASE_DONE;
   1509        1.8     ahoka 	if (ei->ei_callback != NULL) {
   1510        1.1     ahoka 		ei->ei_callback(ei);
   1511        1.8     ahoka 	}
   1512        1.1     ahoka 
   1513        1.1     ahoka 	return 0;
   1514        1.8     ahoka out:
   1515        1.8     ahoka 	mutex_exit(&sc->sc_device_lock);
   1516        1.8     ahoka 
   1517        1.8     ahoka 	return error;
   1518        1.1     ahoka }
   1519        1.1     ahoka 
   1520        1.1     ahoka MODULE(MODULE_CLASS_DRIVER, nand, "flash");
   1521        1.1     ahoka 
   1522        1.1     ahoka #ifdef _MODULE
   1523        1.1     ahoka #include "ioconf.c"
   1524        1.1     ahoka #endif
   1525        1.1     ahoka 
   1526        1.1     ahoka static int
   1527        1.1     ahoka nand_modcmd(modcmd_t cmd, void *opaque)
   1528        1.1     ahoka {
   1529        1.1     ahoka 	switch (cmd) {
   1530        1.1     ahoka 	case MODULE_CMD_INIT:
   1531        1.1     ahoka #ifdef _MODULE
   1532        1.1     ahoka 		return config_init_component(cfdriver_ioconf_nand,
   1533        1.1     ahoka 		    cfattach_ioconf_nand, cfdata_ioconf_nand);
   1534        1.1     ahoka #else
   1535        1.1     ahoka 		return 0;
   1536        1.1     ahoka #endif
   1537        1.1     ahoka 	case MODULE_CMD_FINI:
   1538        1.1     ahoka #ifdef _MODULE
   1539        1.1     ahoka 		return config_fini_component(cfdriver_ioconf_nand,
   1540        1.1     ahoka 		    cfattach_ioconf_nand, cfdata_ioconf_nand);
   1541        1.1     ahoka #else
   1542        1.1     ahoka 		return 0;
   1543        1.1     ahoka #endif
   1544        1.1     ahoka 	default:
   1545        1.1     ahoka 		return ENOTTY;
   1546        1.1     ahoka 	}
   1547        1.1     ahoka }
   1548