Home | History | Annotate | Line # | Download | only in nand
nandemulator.c revision 1.9
      1  1.9       chs /*	$NetBSD: nandemulator.c,v 1.9 2019/10/01 18:00:08 chs 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 #include <sys/cdefs.h>
     35  1.9       chs __KERNEL_RCSID(0, "$NetBSD: nandemulator.c,v 1.9 2019/10/01 18:00:08 chs Exp $");
     36  1.8       mrg 
     37  1.8       mrg /* XXX this code likely needs work */
     38  1.1     ahoka 
     39  1.1     ahoka #include <sys/param.h>
     40  1.1     ahoka #include <sys/device.h>
     41  1.1     ahoka #include <sys/conf.h>
     42  1.1     ahoka #include <sys/kmem.h>
     43  1.1     ahoka #include <sys/kernel.h>
     44  1.1     ahoka 
     45  1.1     ahoka #include "nandemulator.h"
     46  1.1     ahoka 
     47  1.1     ahoka #include <dev/nand/nand.h>
     48  1.1     ahoka #include <dev/nand/onfi.h>
     49  1.1     ahoka #include <dev/nand/nand_crc.h>
     50  1.1     ahoka 
     51  1.7  christos #include "ioconf.h"
     52  1.7  christos 
     53  1.1     ahoka extern struct cfdriver nandemulator_cd;
     54  1.1     ahoka 
     55  1.1     ahoka static int nandemulator_match(device_t, cfdata_t, void *);
     56  1.1     ahoka static void nandemulator_attach(device_t, device_t, void *);
     57  1.1     ahoka static int nandemulator_detach(device_t, int);
     58  1.1     ahoka 
     59  1.1     ahoka static void nandemulator_device_reset(device_t);
     60  1.1     ahoka static void nandemulator_command(device_t, uint8_t);
     61  1.1     ahoka static void nandemulator_address(device_t, uint8_t);
     62  1.1     ahoka static void nandemulator_busy(device_t);
     63  1.5     ahoka static void nandemulator_read_1(device_t, uint8_t *);
     64  1.5     ahoka static void nandemulator_write_1(device_t, uint8_t);
     65  1.5     ahoka static void nandemulator_read_2(device_t, uint16_t *);
     66  1.5     ahoka static void nandemulator_write_2(device_t, uint16_t);
     67  1.5     ahoka static void nandemulator_read_buf_1(device_t, void *, size_t);
     68  1.5     ahoka static void nandemulator_read_buf_2(device_t, void *, size_t);
     69  1.5     ahoka static void nandemulator_write_buf_1(device_t, const void *, size_t);
     70  1.5     ahoka static void nandemulator_write_buf_2(device_t, const void *, size_t);
     71  1.1     ahoka 
     72  1.1     ahoka static size_t nandemulator_address_to_page(device_t);
     73  1.1     ahoka static size_t nandemulator_page_to_backend_offset(device_t, size_t);
     74  1.1     ahoka static size_t nandemulator_column_address_to_subpage(device_t);
     75  1.1     ahoka /*
     76  1.1     ahoka #define NANDEMULATOR_DEBUG 1
     77  1.1     ahoka 
     78  1.1     ahoka #ifdef NANDEMULATOR_DEBUG
     79  1.1     ahoka #warning debug enabled
     80  1.1     ahoka #define DPRINTF(x)	if (nandemulatordebug) printf x
     81  1.1     ahoka #define DPRINTFN(n,x)	if (nandemulatordebug>(n)) printf x
     82  1.1     ahoka #else
     83  1.1     ahoka #error no debug
     84  1.1     ahoka #define DPRINTF(x)
     85  1.1     ahoka #define DPRINTFN(n,x)
     86  1.1     ahoka #endif
     87  1.1     ahoka 
     88  1.1     ahoka #ifdef NANDEMULATOR_DEBUG
     89  1.1     ahoka int	nandemulatordebug = NANDEMULATOR_DEBUG;
     90  1.1     ahoka #endif
     91  1.1     ahoka */
     92  1.1     ahoka 
     93  1.1     ahoka extern int nanddebug;
     94  1.1     ahoka 
     95  1.1     ahoka enum {
     96  1.1     ahoka 	NANDEMULATOR_8BIT,
     97  1.1     ahoka 	NANDEMULATOR_16BIT
     98  1.1     ahoka };
     99  1.1     ahoka 
    100  1.1     ahoka struct nandemulator_softc {
    101  1.1     ahoka 	device_t		sc_dev;
    102  1.1     ahoka 	device_t		sc_nanddev;
    103  1.1     ahoka 
    104  1.1     ahoka 	int			sc_buswidth;
    105  1.1     ahoka 
    106  1.1     ahoka 	struct nand_interface	sc_nand_if;
    107  1.1     ahoka 
    108  1.1     ahoka 	uint8_t			sc_command;
    109  1.1     ahoka 	size_t			sc_io_len;
    110  1.1     ahoka 	uint8_t			*sc_io_pointer;
    111  1.1     ahoka 	uint64_t		sc_address;
    112  1.1     ahoka 
    113  1.1     ahoka 	uint8_t			*sc_backend;
    114  1.1     ahoka 	size_t			sc_backend_size;
    115  1.1     ahoka 	size_t			sc_device_size;
    116  1.1     ahoka 	bool			sc_register_writable;
    117  1.1     ahoka 
    118  1.1     ahoka 	uint8_t			sc_status_register;
    119  1.1     ahoka 	uint8_t			sc_ids[2];
    120  1.1     ahoka 	uint8_t			sc_onfi[4];
    121  1.1     ahoka 
    122  1.1     ahoka 	size_t			sc_page_size;
    123  1.1     ahoka 	size_t			sc_block_size;
    124  1.1     ahoka 	size_t			sc_spare_size;
    125  1.1     ahoka 	size_t			sc_lun_size;
    126  1.1     ahoka 	uint8_t			sc_row_cycles;
    127  1.1     ahoka 	uint8_t			sc_column_cycles;
    128  1.1     ahoka 	uint64_t		sc_row_mask;
    129  1.1     ahoka 
    130  1.1     ahoka 	int			sc_address_counter;
    131  1.1     ahoka 
    132  1.1     ahoka 	struct onfi_parameter_page	*sc_parameter_page;
    133  1.1     ahoka };
    134  1.1     ahoka 
    135  1.1     ahoka CFATTACH_DECL_NEW(nandemulator, sizeof(struct nandemulator_softc),
    136  1.1     ahoka     nandemulator_match, nandemulator_attach, nandemulator_detach, NULL);
    137  1.1     ahoka 
    138  1.1     ahoka void
    139  1.1     ahoka nandemulatorattach(int n)
    140  1.1     ahoka {
    141  1.1     ahoka 	int i, err;
    142  1.1     ahoka 	cfdata_t cf;
    143  1.1     ahoka 
    144  1.1     ahoka 	aprint_debug("nandemulator: requested %d units\n", n);
    145  1.1     ahoka 
    146  1.1     ahoka 	err = config_cfattach_attach(nandemulator_cd.cd_name,
    147  1.1     ahoka 	    &nandemulator_ca);
    148  1.1     ahoka 	if (err) {
    149  1.1     ahoka 		aprint_error("%s: couldn't register cfattach: %d\n",
    150  1.1     ahoka 		    nandemulator_cd.cd_name, err);
    151  1.1     ahoka 		config_cfdriver_detach(&nandemulator_cd);
    152  1.1     ahoka 		return;
    153  1.1     ahoka 	}
    154  1.1     ahoka 	for (i = 0; i < n; i++) {
    155  1.9       chs 		cf = kmem_alloc(sizeof(struct cfdata), KM_SLEEP);
    156  1.1     ahoka 		cf->cf_name = nandemulator_cd.cd_name;
    157  1.1     ahoka 		cf->cf_atname = nandemulator_cd.cd_name;
    158  1.1     ahoka 		cf->cf_unit = i;
    159  1.1     ahoka 		cf->cf_fstate = FSTATE_STAR;
    160  1.1     ahoka 
    161  1.1     ahoka 		(void)config_attach_pseudo(cf);
    162  1.1     ahoka 	}
    163  1.1     ahoka }
    164  1.1     ahoka 
    165  1.1     ahoka /* ARGSUSED */
    166  1.1     ahoka static int
    167  1.1     ahoka nandemulator_match(device_t parent, cfdata_t match, void *aux)
    168  1.1     ahoka {
    169  1.1     ahoka 	/* pseudo device, always attaches */
    170  1.1     ahoka 	return 1;
    171  1.1     ahoka }
    172  1.1     ahoka 
    173  1.1     ahoka static void
    174  1.1     ahoka nandemulator_attach(device_t parent, device_t self, void *aux)
    175  1.1     ahoka {
    176  1.1     ahoka 	struct nandemulator_softc *sc = device_private(self);
    177  1.1     ahoka 	int i;
    178  1.1     ahoka 
    179  1.1     ahoka 	aprint_normal_dev(self, "NAND emulator\n");
    180  1.1     ahoka 
    181  1.1     ahoka 	sc->sc_dev = self;
    182  1.1     ahoka 
    183  1.2     ahoka 	nand_init_interface(&sc->sc_nand_if);
    184  1.2     ahoka 
    185  1.1     ahoka 	sc->sc_nand_if.command = &nandemulator_command;
    186  1.1     ahoka 	sc->sc_nand_if.address = &nandemulator_address;
    187  1.5     ahoka 	sc->sc_nand_if.read_buf_1 = &nandemulator_read_buf_1;
    188  1.5     ahoka 	sc->sc_nand_if.read_buf_2 = &nandemulator_read_buf_2;
    189  1.5     ahoka 	sc->sc_nand_if.read_1 = &nandemulator_read_1;
    190  1.5     ahoka 	sc->sc_nand_if.read_2 = &nandemulator_read_2;
    191  1.5     ahoka 	sc->sc_nand_if.write_buf_1 = &nandemulator_write_buf_1;
    192  1.5     ahoka 	sc->sc_nand_if.write_buf_2 = &nandemulator_write_buf_2;
    193  1.5     ahoka 	sc->sc_nand_if.write_1 = &nandemulator_write_1;
    194  1.5     ahoka 	sc->sc_nand_if.write_2 = &nandemulator_write_2;
    195  1.1     ahoka 	sc->sc_nand_if.busy = &nandemulator_busy;
    196  1.1     ahoka 
    197  1.1     ahoka 	sc->sc_nand_if.ecc.necc_code_size = 3;
    198  1.1     ahoka 	sc->sc_nand_if.ecc.necc_block_size = 256;
    199  1.1     ahoka 
    200  1.1     ahoka 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, NULL))
    201  1.1     ahoka 		aprint_error_dev(sc->sc_dev,
    202  1.1     ahoka 		    "couldn't establish power handler\n");
    203  1.1     ahoka 
    204  1.1     ahoka 	sc->sc_buswidth = NANDEMULATOR_16BIT;	/* 16bit for now */
    205  1.1     ahoka 
    206  1.1     ahoka 	/* hardcode these now, make it configurable later */
    207  1.1     ahoka 	sc->sc_device_size = 32 * 1024 * 1024; /* 32MB */
    208  1.1     ahoka 	sc->sc_page_size = 2048;
    209  1.1     ahoka 	sc->sc_block_size = 64;
    210  1.1     ahoka 	sc->sc_lun_size =
    211  1.1     ahoka 	    sc->sc_device_size / (sc->sc_page_size * sc->sc_block_size);
    212  1.1     ahoka 	KASSERT(sc->sc_device_size %
    213  1.1     ahoka 	    (sc->sc_page_size * sc->sc_block_size) == 0);
    214  1.1     ahoka 	sc->sc_spare_size = 64;
    215  1.1     ahoka 
    216  1.1     ahoka 	sc->sc_column_cycles = 2;
    217  1.1     ahoka 	sc->sc_row_cycles = 3;
    218  1.1     ahoka 
    219  1.1     ahoka 	/* init the emulator data structures */
    220  1.1     ahoka 	sc->sc_backend_size =
    221  1.1     ahoka 	    sc->sc_device_size +
    222  1.1     ahoka 	    sc->sc_device_size / sc->sc_page_size * sc->sc_spare_size;
    223  1.1     ahoka 
    224  1.1     ahoka 	sc->sc_backend = kmem_alloc(sc->sc_backend_size, KM_SLEEP);
    225  1.1     ahoka 	memset(sc->sc_backend, 0xff, sc->sc_backend_size);
    226  1.1     ahoka 
    227  1.1     ahoka 	sc->sc_parameter_page =
    228  1.1     ahoka 	    kmem_zalloc(sizeof(struct onfi_parameter_page) * 4, KM_SLEEP);
    229  1.1     ahoka 
    230  1.1     ahoka 	struct onfi_parameter_page *opp;
    231  1.1     ahoka 	uint8_t sig[4] = { 'O', 'N', 'F', 'I' };
    232  1.1     ahoka 
    233  1.1     ahoka 	for (i = 0; i < 4; i++) {
    234  1.1     ahoka 		opp = &sc->sc_parameter_page[i];
    235  1.1     ahoka 
    236  1.6     ahoka 		opp->param_signature = htole32(*(uint32_t *)sig);
    237  1.6     ahoka 		opp->param_pagesize = htole32(sc->sc_page_size);
    238  1.6     ahoka 		opp->param_blocksize = htole32(sc->sc_block_size);
    239  1.6     ahoka 		opp->param_sparesize = htole16(sc->sc_spare_size);
    240  1.6     ahoka 		opp->param_lunsize = htole32(sc->sc_lun_size);
    241  1.1     ahoka 		opp->param_numluns = 1;
    242  1.1     ahoka 
    243  1.1     ahoka 		opp->param_manufacturer_id = 0x00;
    244  1.1     ahoka 		memcpy(opp->param_manufacturer,
    245  1.1     ahoka 		    "NETBSD", strlen("NETBSD"));
    246  1.1     ahoka 		memcpy(opp->param_model,
    247  1.1     ahoka 		    "NANDEMULATOR", strlen("NANDEMULATOR"));
    248  1.1     ahoka 
    249  1.6     ahoka 		uint16_t features = ONFI_FEATURE_16BIT;
    250  1.6     ahoka 		opp->param_features = htole16(features);
    251  1.1     ahoka 
    252  1.1     ahoka 		/* the lower 4 bits contain the row address cycles
    253  1.1     ahoka 		 * the upper 4 bits contain the column address cycles
    254  1.1     ahoka 		 */
    255  1.1     ahoka 		opp->param_addr_cycles = sc->sc_row_cycles;
    256  1.1     ahoka 		opp->param_addr_cycles |= (sc->sc_column_cycles << 4);
    257  1.1     ahoka 
    258  1.1     ahoka 		opp->param_integrity_crc = nand_crc16((uint8_t *)opp, 254);
    259  1.1     ahoka 	}
    260  1.1     ahoka 
    261  1.1     ahoka 	sc->sc_ids[0] = 0x00;
    262  1.1     ahoka 	sc->sc_ids[1] = 0x00;
    263  1.1     ahoka 
    264  1.1     ahoka 	sc->sc_onfi[0] = 'O';
    265  1.1     ahoka 	sc->sc_onfi[1] = 'N';
    266  1.1     ahoka 	sc->sc_onfi[2] = 'F';
    267  1.1     ahoka 	sc->sc_onfi[3] = 'I';
    268  1.1     ahoka 
    269  1.1     ahoka 	sc->sc_row_mask = 0x00;
    270  1.1     ahoka 	for (i = 0; i < sc->sc_row_cycles; i++) {
    271  1.1     ahoka 		sc->sc_row_mask <<= 8;
    272  1.1     ahoka 		sc->sc_row_mask |= 0xff;
    273  1.1     ahoka 	}
    274  1.1     ahoka 
    275  1.1     ahoka 	nandemulator_device_reset(self);
    276  1.1     ahoka 
    277  1.1     ahoka 	sc->sc_nanddev = nand_attach_mi(&sc->sc_nand_if, sc->sc_dev);
    278  1.1     ahoka }
    279  1.1     ahoka 
    280  1.1     ahoka static int
    281  1.1     ahoka nandemulator_detach(device_t self, int flags)
    282  1.1     ahoka {
    283  1.1     ahoka 	struct nandemulator_softc *sc = device_private(self);
    284  1.1     ahoka 	int ret = 0;
    285  1.1     ahoka 
    286  1.1     ahoka 	aprint_normal_dev(sc->sc_dev, "detaching emulator\n");
    287  1.1     ahoka 
    288  1.1     ahoka 	pmf_device_deregister(sc->sc_dev);
    289  1.1     ahoka 
    290  1.1     ahoka 	if (sc->sc_nanddev != NULL)
    291  1.1     ahoka 		ret = config_detach(sc->sc_nanddev, flags);
    292  1.1     ahoka 
    293  1.1     ahoka 	kmem_free(sc->sc_backend, sc->sc_backend_size);
    294  1.1     ahoka 	kmem_free(sc->sc_parameter_page,
    295  1.1     ahoka 	    sizeof(struct onfi_parameter_page) * 4);
    296  1.1     ahoka 
    297  1.1     ahoka 	return ret;
    298  1.1     ahoka }
    299  1.1     ahoka 
    300  1.1     ahoka /**
    301  1.1     ahoka  * bring the emulated device to a known state
    302  1.1     ahoka  */
    303  1.1     ahoka static void
    304  1.1     ahoka nandemulator_device_reset(device_t self)
    305  1.1     ahoka {
    306  1.1     ahoka 	struct nandemulator_softc *sc = device_private(self);
    307  1.1     ahoka 
    308  1.3     ahoka 	DPRINTF(("device reset\n"));
    309  1.3     ahoka 
    310  1.1     ahoka 	sc->sc_command = 0;
    311  1.1     ahoka 	sc->sc_register_writable = false;
    312  1.1     ahoka 	sc->sc_io_len = 0;
    313  1.1     ahoka 	sc->sc_io_pointer = NULL;
    314  1.1     ahoka 	sc->sc_address = 0;
    315  1.1     ahoka 	sc->sc_address_counter = 0;
    316  1.1     ahoka 
    317  1.1     ahoka 	sc->sc_status_register = ONFI_STATUS_RDY | ONFI_STATUS_WP;
    318  1.1     ahoka }
    319  1.1     ahoka 
    320  1.1     ahoka static void
    321  1.1     ahoka nandemulator_address_chip(device_t self)
    322  1.1     ahoka {
    323  1.1     ahoka 	struct nandemulator_softc *sc = device_private(self);
    324  1.1     ahoka 	size_t page, offset;
    325  1.3     ahoka 
    326  1.3     ahoka 	KASSERT(sc->sc_address_counter ==
    327  1.3     ahoka 	    sc->sc_column_cycles + sc->sc_row_cycles);
    328  1.5     ahoka 
    329  1.1     ahoka 	if (sc->sc_address_counter !=
    330  1.1     ahoka 	    sc->sc_column_cycles + sc->sc_row_cycles) {
    331  1.1     ahoka 		aprint_error_dev(self, "incorrect number of address cycles\n");
    332  1.1     ahoka 		aprint_error_dev(self, "cc: %d, rc: %d, ac: %d\n",
    333  1.1     ahoka 		    sc->sc_column_cycles, sc->sc_row_cycles,
    334  1.1     ahoka 		    sc->sc_address_counter);
    335  1.1     ahoka 	}
    336  1.1     ahoka 
    337  1.1     ahoka 	page = nandemulator_address_to_page(self);
    338  1.1     ahoka 	offset = sc->sc_page_size * page;
    339  1.1     ahoka 
    340  1.1     ahoka 	DPRINTF(("READ/PROGRAM; page: 0x%jx (row addr: 0x%jx)\n",
    341  1.1     ahoka 		(uintmax_t )page,
    342  1.1     ahoka 		(uintmax_t )offset));
    343  1.1     ahoka 
    344  1.3     ahoka 	KASSERT(offset < sc->sc_device_size);
    345  1.3     ahoka 
    346  1.1     ahoka 	if (offset >= sc->sc_device_size) {
    347  1.1     ahoka 		aprint_error_dev(self, "address > device size!\n");
    348  1.1     ahoka 		sc->sc_io_len = 0;
    349  1.1     ahoka 	} else {
    350  1.1     ahoka 		size_t addr =
    351  1.1     ahoka 		    nandemulator_page_to_backend_offset(self, page);
    352  1.1     ahoka 		size_t pageoff =
    353  1.1     ahoka 		    nandemulator_column_address_to_subpage(self);
    354  1.1     ahoka 
    355  1.1     ahoka 		DPRINTF(("subpage: 0x%jx\n", (uintmax_t )pageoff));
    356  1.1     ahoka 
    357  1.1     ahoka 		KASSERT(pageoff <
    358  1.1     ahoka 		    sc->sc_page_size + sc->sc_spare_size);
    359  1.1     ahoka 		KASSERT(addr < sc->sc_backend_size);
    360  1.1     ahoka 
    361  1.1     ahoka 		sc->sc_io_pointer = sc->sc_backend + addr + pageoff;
    362  1.1     ahoka 		sc->sc_io_len =
    363  1.1     ahoka 		    sc->sc_page_size + sc->sc_spare_size - pageoff;
    364  1.1     ahoka 	}
    365  1.1     ahoka }
    366  1.1     ahoka 
    367  1.1     ahoka static void
    368  1.1     ahoka nandemulator_command(device_t self, uint8_t command)
    369  1.1     ahoka {
    370  1.1     ahoka 	struct nandemulator_softc *sc = device_private(self);
    371  1.1     ahoka 	size_t offset, page;
    372  1.1     ahoka 
    373  1.1     ahoka 	sc->sc_command = command;
    374  1.1     ahoka 	sc->sc_register_writable = false;
    375  1.1     ahoka 
    376  1.1     ahoka 	DPRINTF(("nandemulator command: 0x%hhx\n", command));
    377  1.1     ahoka 
    378  1.1     ahoka 	switch (command) {
    379  1.1     ahoka 	case ONFI_READ_STATUS:
    380  1.1     ahoka 		sc->sc_io_pointer = &sc->sc_status_register;
    381  1.1     ahoka 		sc->sc_io_len = 1;
    382  1.1     ahoka 		break;
    383  1.1     ahoka 	case ONFI_RESET:
    384  1.1     ahoka 		nandemulator_device_reset(self);
    385  1.1     ahoka 		break;
    386  1.1     ahoka 	case ONFI_PAGE_PROGRAM:
    387  1.1     ahoka 		sc->sc_register_writable = true;
    388  1.8       mrg 		/* FALLTHROUGH */
    389  1.1     ahoka 	case ONFI_READ:
    390  1.1     ahoka 	case ONFI_BLOCK_ERASE:
    391  1.1     ahoka 		sc->sc_address_counter = 0;
    392  1.8       mrg 		/* FALLTHROUGH */
    393  1.1     ahoka 	case ONFI_READ_ID:
    394  1.1     ahoka 	case ONFI_READ_PARAMETER_PAGE:
    395  1.1     ahoka 		sc->sc_io_len = 0;
    396  1.1     ahoka 		sc->sc_address = 0;
    397  1.1     ahoka 		break;
    398  1.1     ahoka 	case ONFI_PAGE_PROGRAM_START:
    399  1.1     ahoka 		/* XXX the program should only happen here */
    400  1.1     ahoka 		break;
    401  1.1     ahoka 	case ONFI_READ_START:
    402  1.1     ahoka 		nandemulator_address_chip(self);
    403  1.1     ahoka 		break;
    404  1.1     ahoka 	case ONFI_BLOCK_ERASE_START:
    405  1.1     ahoka 		page = nandemulator_address_to_page(self);
    406  1.1     ahoka 		offset = sc->sc_page_size * page;
    407  1.1     ahoka 
    408  1.1     ahoka 		KASSERT(offset %
    409  1.1     ahoka 		    (sc->sc_block_size * sc->sc_page_size) == 0);
    410  1.1     ahoka 
    411  1.3     ahoka 		KASSERT(offset < sc->sc_device_size);
    412  1.3     ahoka 
    413  1.1     ahoka 		if (offset >= sc->sc_device_size) {
    414  1.1     ahoka 			aprint_error_dev(self, "address > device size!\n");
    415  1.1     ahoka 		} else {
    416  1.1     ahoka 			size_t addr =
    417  1.1     ahoka 			    nandemulator_page_to_backend_offset(self, page);
    418  1.1     ahoka 
    419  1.1     ahoka 			size_t blocklen =
    420  1.1     ahoka 			    sc->sc_block_size *
    421  1.1     ahoka 			    (sc->sc_page_size + sc->sc_spare_size);
    422  1.1     ahoka 
    423  1.1     ahoka 			KASSERT(addr < sc->sc_backend_size);
    424  1.1     ahoka 			uint8_t *block = sc->sc_backend + addr;
    425  1.1     ahoka 
    426  1.1     ahoka 			DPRINTF(("erasing block at 0x%jx\n",
    427  1.1     ahoka 				(uintmax_t )offset));
    428  1.1     ahoka 
    429  1.1     ahoka 			memset(block, 0xff, blocklen);
    430  1.1     ahoka 		}
    431  1.1     ahoka 		sc->sc_io_len = 0;
    432  1.1     ahoka 		break;
    433  1.1     ahoka 	default:
    434  1.1     ahoka 		aprint_error_dev(self,
    435  1.1     ahoka 		    "invalid nand command (0x%hhx)\n", command);
    436  1.3     ahoka 		KASSERT(false);
    437  1.1     ahoka 		sc->sc_io_len = 0;
    438  1.1     ahoka 	}
    439  1.1     ahoka };
    440  1.1     ahoka 
    441  1.1     ahoka static void
    442  1.1     ahoka nandemulator_address(device_t self, uint8_t address)
    443  1.1     ahoka {
    444  1.1     ahoka 	struct nandemulator_softc *sc = device_private(self);
    445  1.1     ahoka 
    446  1.3     ahoka 	DPRINTF(("nandemulator_address: %hhx\n", address));
    447  1.3     ahoka 
    448  1.1     ahoka 	/**
    449  1.1     ahoka 	 * we have to handle read id/parameter page here,
    450  1.1     ahoka 	 * as we can read right after giving the address.
    451  1.1     ahoka 	 */
    452  1.1     ahoka 	switch (sc->sc_command) {
    453  1.1     ahoka 	case ONFI_READ_ID:
    454  1.1     ahoka 		if (address == 0x00) {
    455  1.1     ahoka 			sc->sc_io_len = 2;
    456  1.1     ahoka 			sc->sc_io_pointer = sc->sc_ids;
    457  1.1     ahoka 		} else if (address == 0x20) {
    458  1.1     ahoka 			sc->sc_io_len = 4;
    459  1.1     ahoka 			sc->sc_io_pointer = sc->sc_onfi;
    460  1.1     ahoka 		} else {
    461  1.1     ahoka 			sc->sc_io_len = 0;
    462  1.1     ahoka 		}
    463  1.1     ahoka 		break;
    464  1.1     ahoka 	case ONFI_READ_PARAMETER_PAGE:
    465  1.1     ahoka 		if (address == 0x00) {
    466  1.1     ahoka 			sc->sc_io_len = sizeof(struct onfi_parameter_page) * 4;
    467  1.1     ahoka 			sc->sc_io_pointer = (uint8_t *)sc->sc_parameter_page;
    468  1.1     ahoka 		} else {
    469  1.1     ahoka 			sc->sc_io_len = 0;
    470  1.1     ahoka 		}
    471  1.1     ahoka 		break;
    472  1.1     ahoka 	case ONFI_PAGE_PROGRAM:
    473  1.1     ahoka 		sc->sc_address <<= 8;
    474  1.1     ahoka 		sc->sc_address |= address;
    475  1.1     ahoka 		sc->sc_address_counter++;
    476  1.5     ahoka 
    477  1.1     ahoka 		if (sc->sc_address_counter ==
    478  1.1     ahoka 		    sc->sc_column_cycles + sc->sc_row_cycles) {
    479  1.1     ahoka 			nandemulator_address_chip(self);
    480  1.1     ahoka 		}
    481  1.1     ahoka 		break;
    482  1.1     ahoka 	default:
    483  1.1     ahoka 		sc->sc_address <<= 8;
    484  1.1     ahoka 		sc->sc_address |= address;
    485  1.1     ahoka 		sc->sc_address_counter++;
    486  1.1     ahoka 	}
    487  1.1     ahoka };
    488  1.1     ahoka 
    489  1.1     ahoka static void
    490  1.1     ahoka nandemulator_busy(device_t self)
    491  1.1     ahoka {
    492  1.1     ahoka #ifdef NANDEMULATOR_DELAYS
    493  1.1     ahoka 	struct nandemulator_softc *sc = device_private(self);
    494  1.1     ahoka 
    495  1.1     ahoka 	/* do some delay depending on command */
    496  1.1     ahoka 	switch (sc->sc_command) {
    497  1.1     ahoka 	case ONFI_PAGE_PROGRAM_START:
    498  1.1     ahoka 	case ONFI_BLOCK_ERASE_START:
    499  1.1     ahoka 		DELAY(10);
    500  1.1     ahoka 		break;
    501  1.1     ahoka 	case ONFI_READ_START:
    502  1.1     ahoka 	default:
    503  1.1     ahoka 		DELAY(1);
    504  1.1     ahoka 	}
    505  1.1     ahoka #endif
    506  1.1     ahoka }
    507  1.1     ahoka 
    508  1.1     ahoka static void
    509  1.5     ahoka nandemulator_read_1(device_t self, uint8_t *data)
    510  1.1     ahoka {
    511  1.1     ahoka 	struct nandemulator_softc *sc = device_private(self);
    512  1.1     ahoka 
    513  1.3     ahoka 	KASSERT(sc->sc_io_len > 0);
    514  1.3     ahoka 
    515  1.1     ahoka 	if (sc->sc_io_len > 0) {
    516  1.1     ahoka 		*data = *sc->sc_io_pointer;
    517  1.1     ahoka 
    518  1.1     ahoka 		sc->sc_io_pointer++;
    519  1.1     ahoka 		sc->sc_io_len--;
    520  1.1     ahoka 	} else {
    521  1.1     ahoka 		aprint_error_dev(self, "reading byte from invalid location\n");
    522  1.1     ahoka 		*data = 0xff;
    523  1.1     ahoka 	}
    524  1.1     ahoka }
    525  1.1     ahoka 
    526  1.1     ahoka static void
    527  1.5     ahoka nandemulator_write_1(device_t self, uint8_t data)
    528  1.1     ahoka {
    529  1.1     ahoka 	struct nandemulator_softc *sc = device_private(self);
    530  1.1     ahoka 
    531  1.3     ahoka 	KASSERT(sc->sc_register_writable);
    532  1.3     ahoka 
    533  1.1     ahoka 	if (!sc->sc_register_writable) {
    534  1.1     ahoka 		aprint_error_dev(self,
    535  1.1     ahoka 		    "trying to write read only location without effect\n");
    536  1.1     ahoka 		return;
    537  1.1     ahoka 	}
    538  1.1     ahoka 
    539  1.3     ahoka 	KASSERT(sc->sc_io_len > 0);
    540  1.3     ahoka 
    541  1.1     ahoka 	if (sc->sc_io_len > 0) {
    542  1.1     ahoka 		*sc->sc_io_pointer = data;
    543  1.1     ahoka 
    544  1.1     ahoka 		sc->sc_io_pointer++;
    545  1.1     ahoka 		sc->sc_io_len--;
    546  1.1     ahoka 	} else {
    547  1.1     ahoka 		aprint_error_dev(self, "write to invalid location\n");
    548  1.1     ahoka 	}
    549  1.1     ahoka }
    550  1.1     ahoka 
    551  1.1     ahoka static void
    552  1.5     ahoka nandemulator_read_2(device_t self, uint16_t *data)
    553  1.1     ahoka {
    554  1.1     ahoka 	struct nandemulator_softc *sc = device_private(self);
    555  1.1     ahoka 
    556  1.3     ahoka 	KASSERT(sc->sc_buswidth == NANDEMULATOR_16BIT);
    557  1.3     ahoka 
    558  1.1     ahoka 	if (sc->sc_buswidth != NANDEMULATOR_16BIT) {
    559  1.1     ahoka 		aprint_error_dev(self,
    560  1.1     ahoka 		    "trying to read a word on an 8bit chip\n");
    561  1.1     ahoka 		return;
    562  1.1     ahoka 	}
    563  1.1     ahoka 
    564  1.3     ahoka 	KASSERT(sc->sc_io_len > 1);
    565  1.3     ahoka 
    566  1.1     ahoka 	if (sc->sc_io_len > 1) {
    567  1.1     ahoka 		*data = *(uint16_t *)sc->sc_io_pointer;
    568  1.1     ahoka 
    569  1.1     ahoka 		sc->sc_io_pointer += 2;
    570  1.1     ahoka 		sc->sc_io_len -= 2;
    571  1.1     ahoka 	} else {
    572  1.1     ahoka 		aprint_error_dev(self, "reading word from invalid location\n");
    573  1.1     ahoka 		*data = 0xffff;
    574  1.1     ahoka 	}
    575  1.1     ahoka }
    576  1.1     ahoka 
    577  1.1     ahoka static void
    578  1.5     ahoka nandemulator_write_2(device_t self, uint16_t data)
    579  1.1     ahoka {
    580  1.1     ahoka 	struct nandemulator_softc *sc = device_private(self);
    581  1.1     ahoka 
    582  1.3     ahoka 	KASSERT(sc->sc_register_writable);
    583  1.3     ahoka 
    584  1.1     ahoka 	if (!sc->sc_register_writable) {
    585  1.1     ahoka 		aprint_error_dev(self,
    586  1.1     ahoka 		    "trying to write read only location without effect\n");
    587  1.1     ahoka 		return;
    588  1.1     ahoka 	}
    589  1.1     ahoka 
    590  1.3     ahoka 	KASSERT(sc->sc_buswidth == NANDEMULATOR_16BIT);
    591  1.3     ahoka 
    592  1.1     ahoka 	if (sc->sc_buswidth != NANDEMULATOR_16BIT) {
    593  1.1     ahoka 		aprint_error_dev(self,
    594  1.1     ahoka 		    "trying to write a word to an 8bit chip");
    595  1.1     ahoka 		return;
    596  1.1     ahoka 	}
    597  1.1     ahoka 
    598  1.3     ahoka 	KASSERT(sc->sc_io_len > 1);
    599  1.3     ahoka 
    600  1.1     ahoka 	if (sc->sc_io_len > 1) {
    601  1.1     ahoka 		*(uint16_t *)sc->sc_io_pointer = data;
    602  1.1     ahoka 
    603  1.1     ahoka 		sc->sc_io_pointer += 2;
    604  1.1     ahoka 		sc->sc_io_len -= 2;
    605  1.1     ahoka 	} else {
    606  1.1     ahoka 		aprint_error_dev(self, "writing to invalid location");
    607  1.1     ahoka 	}
    608  1.1     ahoka }
    609  1.1     ahoka 
    610  1.1     ahoka static void
    611  1.5     ahoka nandemulator_read_buf_1(device_t self, void *buf, size_t len)
    612  1.1     ahoka {
    613  1.1     ahoka 	uint8_t *addr;
    614  1.1     ahoka 
    615  1.1     ahoka 	KASSERT(buf != NULL);
    616  1.1     ahoka 	KASSERT(len >= 1);
    617  1.1     ahoka 
    618  1.1     ahoka 	addr = buf;
    619  1.1     ahoka 	while (len > 0) {
    620  1.5     ahoka 		nandemulator_read_1(self, addr);
    621  1.1     ahoka 		addr++, len--;
    622  1.1     ahoka 	}
    623  1.1     ahoka }
    624  1.1     ahoka 
    625  1.1     ahoka static void
    626  1.5     ahoka nandemulator_read_buf_2(device_t self, void *buf, size_t len)
    627  1.1     ahoka {
    628  1.1     ahoka 	uint16_t *addr;
    629  1.1     ahoka 
    630  1.1     ahoka 	KASSERT(buf != NULL);
    631  1.1     ahoka 	KASSERT(len >= 2);
    632  1.1     ahoka 	KASSERT(!(len & 0x01));
    633  1.1     ahoka 
    634  1.1     ahoka 	addr = buf;
    635  1.1     ahoka 	len /= 2;
    636  1.1     ahoka 	while (len > 0) {
    637  1.5     ahoka 		nandemulator_read_2(self, addr);
    638  1.1     ahoka 		addr++, len--;
    639  1.1     ahoka 	}
    640  1.1     ahoka }
    641  1.1     ahoka 
    642  1.1     ahoka static void
    643  1.5     ahoka nandemulator_write_buf_1(device_t self, const void *buf, size_t len)
    644  1.1     ahoka {
    645  1.1     ahoka 	const uint8_t *addr;
    646  1.1     ahoka 
    647  1.1     ahoka 	KASSERT(buf != NULL);
    648  1.1     ahoka 	KASSERT(len >= 1);
    649  1.1     ahoka 
    650  1.1     ahoka 	addr = buf;
    651  1.1     ahoka 	while (len > 0) {
    652  1.5     ahoka 		nandemulator_write_1(self, *addr);
    653  1.1     ahoka 		addr++, len--;
    654  1.1     ahoka 	}
    655  1.1     ahoka }
    656  1.1     ahoka 
    657  1.1     ahoka static void
    658  1.5     ahoka nandemulator_write_buf_2(device_t self, const void *buf, size_t len)
    659  1.1     ahoka {
    660  1.1     ahoka 	const uint16_t *addr;
    661  1.1     ahoka 
    662  1.1     ahoka 	KASSERT(buf != NULL);
    663  1.1     ahoka 	KASSERT(len >= 2);
    664  1.1     ahoka 	KASSERT(!(len & 0x01));
    665  1.1     ahoka 
    666  1.1     ahoka 	addr = buf;
    667  1.1     ahoka 	len /= 2;
    668  1.1     ahoka 	while (len > 0) {
    669  1.5     ahoka 		nandemulator_write_2(self, *addr);
    670  1.1     ahoka 		addr++, len--;
    671  1.1     ahoka 	}
    672  1.1     ahoka }
    673  1.1     ahoka 
    674  1.1     ahoka static size_t
    675  1.1     ahoka nandemulator_address_to_page(device_t self)
    676  1.1     ahoka {
    677  1.1     ahoka 	struct nandemulator_softc *sc = device_private(self);
    678  1.1     ahoka 	uint64_t address, offset;
    679  1.1     ahoka 	int i;
    680  1.1     ahoka 
    681  1.1     ahoka 	address = htole64(sc->sc_address);
    682  1.1     ahoka 	address &= sc->sc_row_mask;
    683  1.1     ahoka 
    684  1.1     ahoka 	offset = 0;
    685  1.1     ahoka 	for (i = 0; i < sc->sc_row_cycles; i++) {
    686  1.1     ahoka 		offset <<= 8;
    687  1.1     ahoka 		offset |= (address & 0xff);
    688  1.1     ahoka 		address >>= 8;
    689  1.1     ahoka 	}
    690  1.1     ahoka 
    691  1.1     ahoka 	return le64toh(offset);
    692  1.1     ahoka }
    693  1.1     ahoka 
    694  1.1     ahoka static size_t
    695  1.1     ahoka nandemulator_column_address_to_subpage(device_t self)
    696  1.1     ahoka {
    697  1.1     ahoka 	struct nandemulator_softc *sc = device_private(self);
    698  1.1     ahoka 	uint64_t address, offset;
    699  1.1     ahoka 	int i;
    700  1.1     ahoka 
    701  1.1     ahoka 	address = htole64(sc->sc_address);
    702  1.1     ahoka 	address >>= (8 * sc->sc_row_cycles);
    703  1.1     ahoka 
    704  1.1     ahoka 	offset = 0;
    705  1.1     ahoka 	for (i = 0; i < sc->sc_column_cycles; i++) {
    706  1.1     ahoka 		offset <<= 8;
    707  1.1     ahoka 		offset |= (address & 0xff);
    708  1.1     ahoka 		address >>= 8;
    709  1.1     ahoka 	}
    710  1.1     ahoka 
    711  1.1     ahoka 	if (sc->sc_buswidth == NANDEMULATOR_16BIT)
    712  1.1     ahoka 		return (size_t )le64toh(offset << 1);
    713  1.1     ahoka 	else
    714  1.1     ahoka 		return (size_t )le64toh(offset);
    715  1.1     ahoka }
    716  1.1     ahoka 
    717  1.1     ahoka static size_t
    718  1.1     ahoka nandemulator_page_to_backend_offset(device_t self, size_t page)
    719  1.1     ahoka {
    720  1.1     ahoka 	struct nandemulator_softc *sc = device_private(self);
    721  1.1     ahoka 
    722  1.1     ahoka 	return (sc->sc_page_size + sc->sc_spare_size) * page;
    723  1.1     ahoka }
    724  1.1     ahoka 
    725  1.1     ahoka #ifdef _MODULE
    726  1.1     ahoka 
    727  1.1     ahoka MODULE(MODULE_CLASS_DRIVER, nandemulator, "nand");
    728  1.1     ahoka 
    729  1.1     ahoka static const struct cfiattrdata nandbuscf_iattrdata = {
    730  1.1     ahoka 	"nandbus", 0, { { NULL, NULL, 0 }, }
    731  1.1     ahoka };
    732  1.1     ahoka static const struct cfiattrdata * const nandemulator_attrs[] = {
    733  1.1     ahoka 	&nandbuscf_iattrdata, NULL
    734  1.1     ahoka };
    735  1.1     ahoka 
    736  1.1     ahoka CFDRIVER_DECL(nandemulator, DV_DULL, nandemulator_attrs);
    737  1.1     ahoka extern struct cfattach nandemulator_ca;
    738  1.1     ahoka static int nandemulatorloc[] = { -1, -1 };
    739  1.1     ahoka 
    740  1.1     ahoka static struct cfdata nandemulator_cfdata[] = {
    741  1.1     ahoka 	{
    742  1.1     ahoka 		.cf_name = "nandemulator",
    743  1.1     ahoka 		.cf_atname = "nandemulator",
    744  1.1     ahoka 		.cf_unit = 0,
    745  1.1     ahoka 		.cf_fstate = FSTATE_STAR,
    746  1.1     ahoka 		.cf_loc = nandemulatorloc,
    747  1.1     ahoka 		.cf_flags = 0,
    748  1.1     ahoka 		.cf_pspec = NULL,
    749  1.1     ahoka 	},
    750  1.1     ahoka 	{ NULL, NULL, 0, 0, NULL, 0, NULL }
    751  1.1     ahoka };
    752  1.1     ahoka 
    753  1.1     ahoka static int
    754  1.1     ahoka nandemulator_modcmd(modcmd_t cmd, void *arg)
    755  1.1     ahoka {
    756  1.1     ahoka 	int error;
    757  1.1     ahoka 
    758  1.1     ahoka 	switch (cmd) {
    759  1.1     ahoka 	case MODULE_CMD_INIT:
    760  1.1     ahoka 		error = config_cfdriver_attach(&nandemulator_cd);
    761  1.1     ahoka 		if (error) {
    762  1.1     ahoka 			return error;
    763  1.1     ahoka 		}
    764  1.1     ahoka 
    765  1.1     ahoka 		error = config_cfattach_attach(nandemulator_cd.cd_name,
    766  1.1     ahoka 		    &nandemulator_ca);
    767  1.1     ahoka 		if (error) {
    768  1.1     ahoka 			config_cfdriver_detach(&nandemulator_cd);
    769  1.1     ahoka 			aprint_error("%s: unable to register cfattach\n",
    770  1.1     ahoka 				nandemulator_cd.cd_name);
    771  1.1     ahoka 
    772  1.1     ahoka 			return error;
    773  1.1     ahoka 		}
    774  1.1     ahoka 
    775  1.1     ahoka 		error = config_cfdata_attach(nandemulator_cfdata, 1);
    776  1.1     ahoka 		if (error) {
    777  1.1     ahoka 			config_cfattach_detach(nandemulator_cd.cd_name,
    778  1.1     ahoka 			    &nandemulator_ca);
    779  1.1     ahoka 			config_cfdriver_detach(&nandemulator_cd);
    780  1.1     ahoka 			aprint_error("%s: unable to register cfdata\n",
    781  1.1     ahoka 				nandemulator_cd.cd_name);
    782  1.1     ahoka 
    783  1.1     ahoka 			return error;
    784  1.1     ahoka 		}
    785  1.1     ahoka 
    786  1.1     ahoka 		(void)config_attach_pseudo(nandemulator_cfdata);
    787  1.1     ahoka 
    788  1.1     ahoka 		return 0;
    789  1.4     ahoka 
    790  1.1     ahoka 	case MODULE_CMD_FINI:
    791  1.1     ahoka 		error = config_cfdata_detach(nandemulator_cfdata);
    792  1.1     ahoka 		if (error) {
    793  1.1     ahoka 			return error;
    794  1.1     ahoka 		}
    795  1.1     ahoka 
    796  1.1     ahoka 		config_cfattach_detach(nandemulator_cd.cd_name,
    797  1.1     ahoka 		    &nandemulator_ca);
    798  1.1     ahoka 		config_cfdriver_detach(&nandemulator_cd);
    799  1.1     ahoka 
    800  1.1     ahoka 		return 0;
    801  1.4     ahoka 
    802  1.4     ahoka 	case MODULE_CMD_AUTOUNLOAD:
    803  1.4     ahoka 		/* prevent auto-unload */
    804  1.4     ahoka 		return EBUSY;
    805  1.4     ahoka 
    806  1.1     ahoka 	default:
    807  1.1     ahoka 		return ENOTTY;
    808  1.1     ahoka 	}
    809  1.1     ahoka }
    810  1.1     ahoka 
    811  1.1     ahoka #endif
    812