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