Home | History | Annotate | Line # | Download | only in nand
nandemulator.c revision 1.5.4.1
      1  1.5.4.1   yamt /*	$NetBSD: nandemulator.c,v 1.5.4.1 2013/01/16 05:33:17 yamt 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.5.4.1   yamt __KERNEL_RCSID(0, "$NetBSD: nandemulator.c,v 1.5.4.1 2013/01/16 05:33:17 yamt Exp $");
     36      1.1  ahoka 
     37      1.1  ahoka #include <sys/param.h>
     38      1.1  ahoka #include <sys/device.h>
     39      1.1  ahoka #include <sys/conf.h>
     40      1.1  ahoka #include <sys/kmem.h>
     41      1.1  ahoka #include <sys/kernel.h>
     42      1.1  ahoka 
     43      1.1  ahoka #include "nandemulator.h"
     44      1.1  ahoka 
     45      1.1  ahoka #include <dev/nand/nand.h>
     46      1.1  ahoka #include <dev/nand/onfi.h>
     47      1.1  ahoka #include <dev/nand/nand_crc.h>
     48      1.1  ahoka 
     49      1.1  ahoka extern struct cfdriver nandemulator_cd;
     50      1.1  ahoka void nandemulatorattach(int n);
     51      1.1  ahoka 
     52      1.1  ahoka static int nandemulator_match(device_t, cfdata_t, void *);
     53      1.1  ahoka static void nandemulator_attach(device_t, device_t, void *);
     54      1.1  ahoka static int nandemulator_detach(device_t, int);
     55      1.1  ahoka 
     56      1.1  ahoka static void nandemulator_device_reset(device_t);
     57      1.1  ahoka static void nandemulator_command(device_t, uint8_t);
     58      1.1  ahoka static void nandemulator_address(device_t, uint8_t);
     59      1.1  ahoka static void nandemulator_busy(device_t);
     60      1.5  ahoka static void nandemulator_read_1(device_t, uint8_t *);
     61      1.5  ahoka static void nandemulator_write_1(device_t, uint8_t);
     62      1.5  ahoka static void nandemulator_read_2(device_t, uint16_t *);
     63      1.5  ahoka static void nandemulator_write_2(device_t, uint16_t);
     64      1.5  ahoka static void nandemulator_read_buf_1(device_t, void *, size_t);
     65      1.5  ahoka static void nandemulator_read_buf_2(device_t, void *, size_t);
     66      1.5  ahoka static void nandemulator_write_buf_1(device_t, const void *, size_t);
     67      1.5  ahoka static void nandemulator_write_buf_2(device_t, const void *, size_t);
     68      1.1  ahoka 
     69      1.1  ahoka static size_t nandemulator_address_to_page(device_t);
     70      1.1  ahoka static size_t nandemulator_page_to_backend_offset(device_t, size_t);
     71      1.1  ahoka static size_t nandemulator_column_address_to_subpage(device_t);
     72      1.1  ahoka /*
     73      1.1  ahoka #define NANDEMULATOR_DEBUG 1
     74      1.1  ahoka 
     75      1.1  ahoka #ifdef NANDEMULATOR_DEBUG
     76      1.1  ahoka #warning debug enabled
     77      1.1  ahoka #define DPRINTF(x)	if (nandemulatordebug) printf x
     78      1.1  ahoka #define DPRINTFN(n,x)	if (nandemulatordebug>(n)) printf x
     79      1.1  ahoka #else
     80      1.1  ahoka #error no debug
     81      1.1  ahoka #define DPRINTF(x)
     82      1.1  ahoka #define DPRINTFN(n,x)
     83      1.1  ahoka #endif
     84      1.1  ahoka 
     85      1.1  ahoka #ifdef NANDEMULATOR_DEBUG
     86      1.1  ahoka int	nandemulatordebug = NANDEMULATOR_DEBUG;
     87      1.1  ahoka #endif
     88      1.1  ahoka */
     89      1.1  ahoka 
     90      1.1  ahoka extern int nanddebug;
     91      1.1  ahoka 
     92      1.1  ahoka enum {
     93      1.1  ahoka 	NANDEMULATOR_8BIT,
     94      1.1  ahoka 	NANDEMULATOR_16BIT
     95      1.1  ahoka };
     96      1.1  ahoka 
     97      1.1  ahoka struct nandemulator_softc {
     98      1.1  ahoka 	device_t		sc_dev;
     99      1.1  ahoka 	device_t		sc_nanddev;
    100      1.1  ahoka 
    101      1.1  ahoka 	int			sc_buswidth;
    102      1.1  ahoka 
    103      1.1  ahoka 	struct nand_interface	sc_nand_if;
    104      1.1  ahoka 
    105      1.1  ahoka 	uint8_t			sc_command;
    106      1.1  ahoka 	size_t			sc_io_len;
    107      1.1  ahoka 	uint8_t			*sc_io_pointer;
    108      1.1  ahoka 	uint64_t		sc_address;
    109      1.1  ahoka 
    110      1.1  ahoka 	uint8_t			*sc_backend;
    111      1.1  ahoka 	size_t			sc_backend_size;
    112      1.1  ahoka 	size_t			sc_device_size;
    113      1.1  ahoka 	bool			sc_register_writable;
    114      1.1  ahoka 
    115      1.1  ahoka 	uint8_t			sc_status_register;
    116      1.1  ahoka 	uint8_t			sc_ids[2];
    117      1.1  ahoka 	uint8_t			sc_onfi[4];
    118      1.1  ahoka 
    119      1.1  ahoka 	size_t			sc_page_size;
    120      1.1  ahoka 	size_t			sc_block_size;
    121      1.1  ahoka 	size_t			sc_spare_size;
    122      1.1  ahoka 	size_t			sc_lun_size;
    123      1.1  ahoka 	uint8_t			sc_row_cycles;
    124      1.1  ahoka 	uint8_t			sc_column_cycles;
    125      1.1  ahoka 	uint64_t		sc_row_mask;
    126      1.1  ahoka 
    127      1.1  ahoka 	int			sc_address_counter;
    128      1.1  ahoka 
    129      1.1  ahoka 	struct onfi_parameter_page	*sc_parameter_page;
    130      1.1  ahoka };
    131      1.1  ahoka 
    132      1.1  ahoka CFATTACH_DECL_NEW(nandemulator, sizeof(struct nandemulator_softc),
    133      1.1  ahoka     nandemulator_match, nandemulator_attach, nandemulator_detach, NULL);
    134      1.1  ahoka 
    135      1.1  ahoka void
    136      1.1  ahoka nandemulatorattach(int n)
    137      1.1  ahoka {
    138      1.1  ahoka 	int i, err;
    139      1.1  ahoka 	cfdata_t cf;
    140      1.1  ahoka 
    141      1.1  ahoka 	aprint_debug("nandemulator: requested %d units\n", n);
    142      1.1  ahoka 
    143      1.1  ahoka 	err = config_cfattach_attach(nandemulator_cd.cd_name,
    144      1.1  ahoka 	    &nandemulator_ca);
    145      1.1  ahoka 	if (err) {
    146      1.1  ahoka 		aprint_error("%s: couldn't register cfattach: %d\n",
    147      1.1  ahoka 		    nandemulator_cd.cd_name, err);
    148      1.1  ahoka 		config_cfdriver_detach(&nandemulator_cd);
    149      1.1  ahoka 		return;
    150      1.1  ahoka 	}
    151      1.1  ahoka 	for (i = 0; i < n; i++) {
    152      1.1  ahoka 		cf = kmem_alloc(sizeof(struct cfdata), KM_NOSLEEP);
    153      1.1  ahoka 		if (cf == NULL) {
    154      1.1  ahoka 			aprint_error("%s: couldn't allocate cfdata\n",
    155      1.1  ahoka 			    nandemulator_cd.cd_name);
    156      1.1  ahoka 			continue;
    157      1.1  ahoka 		}
    158      1.1  ahoka 		cf->cf_name = nandemulator_cd.cd_name;
    159      1.1  ahoka 		cf->cf_atname = nandemulator_cd.cd_name;
    160      1.1  ahoka 		cf->cf_unit = i;
    161      1.1  ahoka 		cf->cf_fstate = FSTATE_STAR;
    162      1.1  ahoka 
    163      1.1  ahoka 		(void)config_attach_pseudo(cf);
    164      1.1  ahoka 	}
    165      1.1  ahoka }
    166      1.1  ahoka 
    167      1.1  ahoka /* ARGSUSED */
    168      1.1  ahoka static int
    169      1.1  ahoka nandemulator_match(device_t parent, cfdata_t match, void *aux)
    170      1.1  ahoka {
    171      1.1  ahoka 	/* pseudo device, always attaches */
    172      1.1  ahoka 	return 1;
    173      1.1  ahoka }
    174      1.1  ahoka 
    175      1.1  ahoka static void
    176      1.1  ahoka nandemulator_attach(device_t parent, device_t self, void *aux)
    177      1.1  ahoka {
    178      1.1  ahoka 	struct nandemulator_softc *sc = device_private(self);
    179      1.1  ahoka 	int i;
    180      1.1  ahoka 
    181      1.1  ahoka 	aprint_normal_dev(self, "NAND emulator\n");
    182      1.1  ahoka 
    183      1.1  ahoka 	sc->sc_dev = self;
    184      1.1  ahoka 
    185      1.2  ahoka 	nand_init_interface(&sc->sc_nand_if);
    186      1.2  ahoka 
    187      1.1  ahoka 	sc->sc_nand_if.command = &nandemulator_command;
    188      1.1  ahoka 	sc->sc_nand_if.address = &nandemulator_address;
    189      1.5  ahoka 	sc->sc_nand_if.read_buf_1 = &nandemulator_read_buf_1;
    190      1.5  ahoka 	sc->sc_nand_if.read_buf_2 = &nandemulator_read_buf_2;
    191      1.5  ahoka 	sc->sc_nand_if.read_1 = &nandemulator_read_1;
    192      1.5  ahoka 	sc->sc_nand_if.read_2 = &nandemulator_read_2;
    193      1.5  ahoka 	sc->sc_nand_if.write_buf_1 = &nandemulator_write_buf_1;
    194      1.5  ahoka 	sc->sc_nand_if.write_buf_2 = &nandemulator_write_buf_2;
    195      1.5  ahoka 	sc->sc_nand_if.write_1 = &nandemulator_write_1;
    196      1.5  ahoka 	sc->sc_nand_if.write_2 = &nandemulator_write_2;
    197      1.1  ahoka 	sc->sc_nand_if.busy = &nandemulator_busy;
    198      1.1  ahoka 
    199      1.1  ahoka 	sc->sc_nand_if.ecc.necc_code_size = 3;
    200      1.1  ahoka 	sc->sc_nand_if.ecc.necc_block_size = 256;
    201      1.1  ahoka 
    202      1.1  ahoka 	if (!pmf_device_register1(sc->sc_dev, NULL, NULL, NULL))
    203      1.1  ahoka 		aprint_error_dev(sc->sc_dev,
    204      1.1  ahoka 		    "couldn't establish power handler\n");
    205      1.1  ahoka 
    206      1.1  ahoka 	sc->sc_buswidth = NANDEMULATOR_16BIT;	/* 16bit for now */
    207      1.1  ahoka 
    208      1.1  ahoka 	/* hardcode these now, make it configurable later */
    209      1.1  ahoka 	sc->sc_device_size = 32 * 1024 * 1024; /* 32MB */
    210      1.1  ahoka 	sc->sc_page_size = 2048;
    211      1.1  ahoka 	sc->sc_block_size = 64;
    212      1.1  ahoka 	sc->sc_lun_size =
    213      1.1  ahoka 	    sc->sc_device_size / (sc->sc_page_size * sc->sc_block_size);
    214      1.1  ahoka 	KASSERT(sc->sc_device_size %
    215      1.1  ahoka 	    (sc->sc_page_size * sc->sc_block_size) == 0);
    216      1.1  ahoka 	sc->sc_spare_size = 64;
    217      1.1  ahoka 
    218      1.1  ahoka 	sc->sc_column_cycles = 2;
    219      1.1  ahoka 	sc->sc_row_cycles = 3;
    220      1.1  ahoka 
    221      1.1  ahoka 	/* init the emulator data structures */
    222      1.1  ahoka 	sc->sc_backend_size =
    223      1.1  ahoka 	    sc->sc_device_size +
    224      1.1  ahoka 	    sc->sc_device_size / sc->sc_page_size * sc->sc_spare_size;
    225      1.1  ahoka 
    226      1.1  ahoka 	sc->sc_backend = kmem_alloc(sc->sc_backend_size, KM_SLEEP);
    227      1.1  ahoka 	memset(sc->sc_backend, 0xff, sc->sc_backend_size);
    228      1.1  ahoka 
    229      1.1  ahoka 	sc->sc_parameter_page =
    230      1.1  ahoka 	    kmem_zalloc(sizeof(struct onfi_parameter_page) * 4, KM_SLEEP);
    231      1.1  ahoka 
    232      1.1  ahoka 	struct onfi_parameter_page *opp;
    233      1.1  ahoka 	uint8_t sig[4] = { 'O', 'N', 'F', 'I' };
    234      1.1  ahoka 
    235      1.1  ahoka 	for (i = 0; i < 4; i++) {
    236      1.1  ahoka 		opp = &sc->sc_parameter_page[i];
    237      1.1  ahoka 
    238  1.5.4.1   yamt 		opp->param_signature = htole32(*(uint32_t *)sig);
    239  1.5.4.1   yamt 		opp->param_pagesize = htole32(sc->sc_page_size);
    240  1.5.4.1   yamt 		opp->param_blocksize = htole32(sc->sc_block_size);
    241  1.5.4.1   yamt 		opp->param_sparesize = htole16(sc->sc_spare_size);
    242  1.5.4.1   yamt 		opp->param_lunsize = htole32(sc->sc_lun_size);
    243      1.1  ahoka 		opp->param_numluns = 1;
    244      1.1  ahoka 
    245      1.1  ahoka 		opp->param_manufacturer_id = 0x00;
    246      1.1  ahoka 		memcpy(opp->param_manufacturer,
    247      1.1  ahoka 		    "NETBSD", strlen("NETBSD"));
    248      1.1  ahoka 		memcpy(opp->param_model,
    249      1.1  ahoka 		    "NANDEMULATOR", strlen("NANDEMULATOR"));
    250      1.1  ahoka 
    251  1.5.4.1   yamt 		uint16_t features = ONFI_FEATURE_16BIT;
    252  1.5.4.1   yamt 		opp->param_features = htole16(features);
    253      1.1  ahoka 
    254      1.1  ahoka 		/* the lower 4 bits contain the row address cycles
    255      1.1  ahoka 		 * the upper 4 bits contain the column address cycles
    256      1.1  ahoka 		 */
    257      1.1  ahoka 		opp->param_addr_cycles = sc->sc_row_cycles;
    258      1.1  ahoka 		opp->param_addr_cycles |= (sc->sc_column_cycles << 4);
    259      1.1  ahoka 
    260      1.1  ahoka 		opp->param_integrity_crc = nand_crc16((uint8_t *)opp, 254);
    261      1.1  ahoka 	}
    262      1.1  ahoka 
    263      1.1  ahoka 	sc->sc_ids[0] = 0x00;
    264      1.1  ahoka 	sc->sc_ids[1] = 0x00;
    265      1.1  ahoka 
    266      1.1  ahoka 	sc->sc_onfi[0] = 'O';
    267      1.1  ahoka 	sc->sc_onfi[1] = 'N';
    268      1.1  ahoka 	sc->sc_onfi[2] = 'F';
    269      1.1  ahoka 	sc->sc_onfi[3] = 'I';
    270      1.1  ahoka 
    271      1.1  ahoka 	sc->sc_row_mask = 0x00;
    272      1.1  ahoka 	for (i = 0; i < sc->sc_row_cycles; i++) {
    273      1.1  ahoka 		sc->sc_row_mask <<= 8;
    274      1.1  ahoka 		sc->sc_row_mask |= 0xff;
    275      1.1  ahoka 	}
    276      1.1  ahoka 
    277      1.1  ahoka 	nandemulator_device_reset(self);
    278      1.1  ahoka 
    279      1.1  ahoka 	sc->sc_nanddev = nand_attach_mi(&sc->sc_nand_if, sc->sc_dev);
    280      1.1  ahoka }
    281      1.1  ahoka 
    282      1.1  ahoka static int
    283      1.1  ahoka nandemulator_detach(device_t self, int flags)
    284      1.1  ahoka {
    285      1.1  ahoka 	struct nandemulator_softc *sc = device_private(self);
    286      1.1  ahoka 	int ret = 0;
    287      1.1  ahoka 
    288      1.1  ahoka 	aprint_normal_dev(sc->sc_dev, "detaching emulator\n");
    289      1.1  ahoka 
    290      1.1  ahoka 	pmf_device_deregister(sc->sc_dev);
    291      1.1  ahoka 
    292      1.1  ahoka 	if (sc->sc_nanddev != NULL)
    293      1.1  ahoka 		ret = config_detach(sc->sc_nanddev, flags);
    294      1.1  ahoka 
    295      1.1  ahoka 	kmem_free(sc->sc_backend, sc->sc_backend_size);
    296      1.1  ahoka 	kmem_free(sc->sc_parameter_page,
    297      1.1  ahoka 	    sizeof(struct onfi_parameter_page) * 4);
    298      1.1  ahoka 
    299      1.1  ahoka 	return ret;
    300      1.1  ahoka }
    301      1.1  ahoka 
    302      1.1  ahoka /**
    303      1.1  ahoka  * bring the emulated device to a known state
    304      1.1  ahoka  */
    305      1.1  ahoka static void
    306      1.1  ahoka nandemulator_device_reset(device_t self)
    307      1.1  ahoka {
    308      1.1  ahoka 	struct nandemulator_softc *sc = device_private(self);
    309      1.1  ahoka 
    310      1.3  ahoka 	DPRINTF(("device reset\n"));
    311      1.3  ahoka 
    312      1.1  ahoka 	sc->sc_command = 0;
    313      1.1  ahoka 	sc->sc_register_writable = false;
    314      1.1  ahoka 	sc->sc_io_len = 0;
    315      1.1  ahoka 	sc->sc_io_pointer = NULL;
    316      1.1  ahoka 	sc->sc_address = 0;
    317      1.1  ahoka 	sc->sc_address_counter = 0;
    318      1.1  ahoka 
    319      1.1  ahoka 	sc->sc_status_register = ONFI_STATUS_RDY | ONFI_STATUS_WP;
    320      1.1  ahoka }
    321      1.1  ahoka 
    322      1.1  ahoka static void
    323      1.1  ahoka nandemulator_address_chip(device_t self)
    324      1.1  ahoka {
    325      1.1  ahoka 	struct nandemulator_softc *sc = device_private(self);
    326      1.1  ahoka 	size_t page, offset;
    327      1.3  ahoka 
    328      1.3  ahoka 	KASSERT(sc->sc_address_counter ==
    329      1.3  ahoka 	    sc->sc_column_cycles + sc->sc_row_cycles);
    330      1.5  ahoka 
    331      1.1  ahoka 	if (sc->sc_address_counter !=
    332      1.1  ahoka 	    sc->sc_column_cycles + sc->sc_row_cycles) {
    333      1.1  ahoka 		aprint_error_dev(self, "incorrect number of address cycles\n");
    334      1.1  ahoka 		aprint_error_dev(self, "cc: %d, rc: %d, ac: %d\n",
    335      1.1  ahoka 		    sc->sc_column_cycles, sc->sc_row_cycles,
    336      1.1  ahoka 		    sc->sc_address_counter);
    337      1.1  ahoka 	}
    338      1.1  ahoka 
    339      1.1  ahoka 	page = nandemulator_address_to_page(self);
    340      1.1  ahoka 	offset = sc->sc_page_size * page;
    341      1.1  ahoka 
    342      1.1  ahoka 	DPRINTF(("READ/PROGRAM; page: 0x%jx (row addr: 0x%jx)\n",
    343      1.1  ahoka 		(uintmax_t )page,
    344      1.1  ahoka 		(uintmax_t )offset));
    345      1.1  ahoka 
    346      1.3  ahoka 	KASSERT(offset < sc->sc_device_size);
    347      1.3  ahoka 
    348      1.1  ahoka 	if (offset >= sc->sc_device_size) {
    349      1.1  ahoka 		aprint_error_dev(self, "address > device size!\n");
    350      1.1  ahoka 		sc->sc_io_len = 0;
    351      1.1  ahoka 	} else {
    352      1.1  ahoka 		size_t addr =
    353      1.1  ahoka 		    nandemulator_page_to_backend_offset(self, page);
    354      1.1  ahoka 		size_t pageoff =
    355      1.1  ahoka 		    nandemulator_column_address_to_subpage(self);
    356      1.1  ahoka 
    357      1.1  ahoka 		DPRINTF(("subpage: 0x%jx\n", (uintmax_t )pageoff));
    358      1.1  ahoka 
    359      1.1  ahoka 		KASSERT(pageoff <
    360      1.1  ahoka 		    sc->sc_page_size + sc->sc_spare_size);
    361      1.1  ahoka 		KASSERT(addr < sc->sc_backend_size);
    362      1.1  ahoka 
    363      1.1  ahoka 		sc->sc_io_pointer = sc->sc_backend + addr + pageoff;
    364      1.1  ahoka 		sc->sc_io_len =
    365      1.1  ahoka 		    sc->sc_page_size + sc->sc_spare_size - pageoff;
    366      1.1  ahoka 	}
    367      1.1  ahoka }
    368      1.1  ahoka 
    369      1.1  ahoka static void
    370      1.1  ahoka nandemulator_command(device_t self, uint8_t command)
    371      1.1  ahoka {
    372      1.1  ahoka 	struct nandemulator_softc *sc = device_private(self);
    373      1.1  ahoka 	size_t offset, page;
    374      1.1  ahoka 
    375      1.1  ahoka 	sc->sc_command = command;
    376      1.1  ahoka 	sc->sc_register_writable = false;
    377      1.1  ahoka 
    378      1.1  ahoka 	DPRINTF(("nandemulator command: 0x%hhx\n", command));
    379      1.1  ahoka 
    380      1.1  ahoka 	switch (command) {
    381      1.1  ahoka 	case ONFI_READ_STATUS:
    382      1.1  ahoka 		sc->sc_io_pointer = &sc->sc_status_register;
    383      1.1  ahoka 		sc->sc_io_len = 1;
    384      1.1  ahoka 		break;
    385      1.1  ahoka 	case ONFI_RESET:
    386      1.1  ahoka 		nandemulator_device_reset(self);
    387      1.1  ahoka 		break;
    388      1.1  ahoka 	case ONFI_PAGE_PROGRAM:
    389      1.1  ahoka 		sc->sc_register_writable = true;
    390      1.1  ahoka 	case ONFI_READ:
    391      1.1  ahoka 	case ONFI_BLOCK_ERASE:
    392      1.1  ahoka 		sc->sc_address_counter = 0;
    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