Home | History | Annotate | Line # | Download | only in vr
flash_vrip.c revision 1.9
      1  1.9  dholland /* $NetBSD: flash_vrip.c,v 1.9 2014/03/16 05:20:24 dholland Exp $ */
      2  1.1       igy 
      3  1.1       igy /*
      4  1.1       igy  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  1.1       igy  * All rights reserved.
      6  1.1       igy  *
      7  1.1       igy  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1       igy  * by Naoto Shimazaki of YOKOGAWA Electric Corporation.
      9  1.1       igy  *
     10  1.1       igy  * Redistribution and use in source and binary forms, with or without
     11  1.1       igy  * modification, are permitted provided that the following conditions
     12  1.1       igy  * are met:
     13  1.1       igy  * 1. Redistributions of source code must retain the above copyright
     14  1.1       igy  *    notice, this list of conditions and the following disclaimer.
     15  1.1       igy  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1       igy  *    notice, this list of conditions and the following disclaimer in the
     17  1.1       igy  *    documentation and/or other materials provided with the distribution.
     18  1.1       igy  *
     19  1.1       igy  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1       igy  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1       igy  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1       igy  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1       igy  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1       igy  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1       igy  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1       igy  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1       igy  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1       igy  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1       igy  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1       igy  */
     31  1.1       igy 
     32  1.1       igy /*
     33  1.1       igy  * Flash Memory Driver
     34  1.1       igy  */
     35  1.2     lukem 
     36  1.2     lukem #include <sys/cdefs.h>
     37  1.9  dholland __KERNEL_RCSID(0, "$NetBSD: flash_vrip.c,v 1.9 2014/03/16 05:20:24 dholland Exp $");
     38  1.1       igy 
     39  1.1       igy #include <sys/param.h>
     40  1.1       igy #include <sys/conf.h>
     41  1.1       igy #include <sys/device.h>
     42  1.1       igy #include <sys/kernel.h>
     43  1.1       igy #include <sys/malloc.h>
     44  1.1       igy #include <sys/proc.h>
     45  1.1       igy #include <sys/systm.h>
     46  1.1       igy 
     47  1.1       igy #include <machine/bus.h>
     48  1.1       igy 
     49  1.1       igy #include <hpcmips/vr/vripif.h>
     50  1.1       igy #include <hpcmips/vr/cfireg.h>
     51  1.1       igy #include <hpcmips/vr/flashreg.h>
     52  1.1       igy #include <hpcmips/vr/flashvar.h>
     53  1.1       igy 
     54  1.1       igy #ifdef FLASH_DEBUG
     55  1.1       igy int	flash_debug = 0;
     56  1.1       igy #define DPRINTF(x)	if (flash_debug) printf x
     57  1.1       igy #else
     58  1.1       igy #define DPRINTF(x)
     59  1.1       igy #endif
     60  1.1       igy 
     61  1.8       chs static int flash_probe(device_t, cfdata_t, void *);
     62  1.8       chs static void flash_attach(device_t, device_t, void *);
     63  1.1       igy 
     64  1.1       igy const static struct flashops * find_command_set(u_int8_t cmdset0,
     65  1.1       igy 						u_int8_t cmdset1);
     66  1.1       igy static int i28f128_probe(bus_space_tag_t, bus_space_handle_t);
     67  1.1       igy static int mbm29160_probe(bus_space_tag_t, bus_space_handle_t);
     68  1.1       igy static int is_block_same(struct flash_softc *, bus_size_t, const void *);
     69  1.1       igy static int probe_cfi(bus_space_tag_t iot, bus_space_handle_t ioh);
     70  1.1       igy 
     71  1.1       igy static int intel_erase(struct flash_softc *, bus_size_t);
     72  1.1       igy static int intel_write(struct flash_softc *, bus_size_t);
     73  1.1       igy static int amd_erase(struct flash_softc *, bus_size_t);
     74  1.1       igy static int amd_write(struct flash_softc *, bus_size_t);
     75  1.1       igy 
     76  1.1       igy extern struct cfdriver flash_cd;
     77  1.1       igy 
     78  1.8       chs CFATTACH_DECL_NEW(flash_vrip, sizeof(struct flash_softc),
     79  1.1       igy 	      flash_probe, flash_attach, NULL, NULL);
     80  1.1       igy 
     81  1.1       igy dev_type_open(flashopen);
     82  1.1       igy dev_type_close(flashclose);
     83  1.1       igy dev_type_read(flashread);
     84  1.1       igy dev_type_write(flashwrite);
     85  1.1       igy 
     86  1.1       igy const struct cdevsw flash_cdevsw = {
     87  1.9  dholland 	.d_open = flashopen,
     88  1.9  dholland 	.d_close = flashclose,
     89  1.9  dholland 	.d_read = flashread,
     90  1.9  dholland 	.d_write = flashwrite,
     91  1.9  dholland 	.d_ioctl = noioctl,
     92  1.9  dholland 	.d_stop = nostop,
     93  1.9  dholland 	.d_tty = notty,
     94  1.9  dholland 	.d_poll = nopoll,
     95  1.9  dholland 	.d_mmap = nommap,
     96  1.9  dholland 	.d_kqfilter = nokqfilter,
     97  1.9  dholland 	.d_flag = 0
     98  1.1       igy };
     99  1.1       igy 
    100  1.1       igy static const struct flash_command_set {
    101  1.1       igy 	u_int8_t	fc_set0;
    102  1.1       igy 	u_int8_t	fc_set1;
    103  1.1       igy 	struct flashops	fc_ops;
    104  1.1       igy } flash_cmd[] = {
    105  1.1       igy 	{
    106  1.1       igy 		.fc_set0	= CFI_COMMSET_INTEL0,
    107  1.1       igy 		.fc_set1	= CFI_COMMSET_INTEL1,
    108  1.1       igy 		.fc_ops		= {
    109  1.1       igy 			.fo_name	= "Intel",
    110  1.1       igy 			.fo_erase	= intel_erase,
    111  1.1       igy 			.fo_write	= intel_write,
    112  1.1       igy 		}
    113  1.1       igy 	},
    114  1.1       igy 	{
    115  1.1       igy 		.fc_set0	= CFI_COMMSET_AMDFJITU0,
    116  1.1       igy 		.fc_set1	= CFI_COMMSET_AMDFJITU1,
    117  1.1       igy 		.fc_ops		= {
    118  1.1       igy 			.fo_name	= "AMD/Fujitsu",
    119  1.1       igy 			.fo_erase	= amd_erase,
    120  1.1       igy 			.fo_write	= amd_write,
    121  1.1       igy 		}
    122  1.1       igy 	},
    123  1.1       igy 	{
    124  1.1       igy 		.fc_set0	= 0,
    125  1.1       igy 		.fc_set1	= 0,
    126  1.1       igy 		.fc_ops		= {
    127  1.1       igy 			.fo_name	= NULL,
    128  1.1       igy 			.fo_erase	= NULL,
    129  1.1       igy 			.fo_write	= NULL,
    130  1.1       igy 		}
    131  1.1       igy 	}
    132  1.1       igy };
    133  1.1       igy 
    134  1.1       igy 
    135  1.1       igy const static struct flashops *
    136  1.1       igy find_command_set(u_int8_t cmdset0, u_int8_t cmdset1)
    137  1.1       igy {
    138  1.1       igy 	const struct flash_command_set	*fc;
    139  1.1       igy 
    140  1.1       igy 	for (fc = flash_cmd; fc->fc_ops.fo_name; fc++) {
    141  1.1       igy 		if (cmdset0 == fc->fc_set0 && cmdset1 == fc->fc_set1)
    142  1.1       igy 			return &fc->fc_ops;
    143  1.1       igy 	}
    144  1.1       igy 	return NULL;
    145  1.1       igy }
    146  1.1       igy 
    147  1.1       igy static int
    148  1.1       igy probe_cfi(bus_space_tag_t iot, bus_space_handle_t ioh)
    149  1.1       igy {
    150  1.1       igy 	const u_int8_t	*idstr = CFI_QUERY_ID_STR;
    151  1.1       igy 	int		i;
    152  1.1       igy 	u_int8_t	cmdset0;
    153  1.1       igy 	u_int8_t	cmdset1;
    154  1.1       igy 
    155  1.1       igy 	/* start Common Flash Interface Query */
    156  1.1       igy 	bus_space_write_2(iot, ioh, CFI_QUERY_OFFSET, CFI_READ_CFI_QUERY);
    157  1.1       igy 
    158  1.1       igy 	/* read CFI Query ID string */
    159  1.1       igy 	i = CFI_QUERY_ID_STR_REG << 1;
    160  1.1       igy 	do {
    161  1.1       igy 		if (bus_space_read_2(iot, ioh, i) != *idstr) {
    162  1.1       igy 			bus_space_write_2(iot, ioh, 0, FLASH_RESET);
    163  1.1       igy 			return 1;
    164  1.1       igy 		}
    165  1.1       igy 		i += 2;
    166  1.1       igy 		idstr++;
    167  1.1       igy 	} while (*idstr);
    168  1.1       igy 
    169  1.1       igy 	cmdset0 = bus_space_read_2(iot, ioh, CFI_PRIM_COMM_REG0 << 1);
    170  1.1       igy 	cmdset1 = bus_space_read_2(iot, ioh, CFI_PRIM_COMM_REG1 << 1);
    171  1.1       igy 
    172  1.1       igy 	/* switch flash to read mode */
    173  1.1       igy 	bus_space_write_2(iot, ioh, 0, FLASH_RESET);
    174  1.1       igy 
    175  1.1       igy 	if (!find_command_set(cmdset0, cmdset1))
    176  1.1       igy 		return 1;
    177  1.1       igy 
    178  1.1       igy 	return 0;
    179  1.1       igy }
    180  1.1       igy 
    181  1.1       igy static int
    182  1.8       chs flash_probe(device_t parent, cfdata_t match, void *aux)
    183  1.1       igy {
    184  1.1       igy 	struct vrip_attach_args	*va = aux;
    185  1.1       igy 	bus_space_handle_t	ioh;
    186  1.1       igy 
    187  1.1       igy 	if (bus_space_map(va->va_iot, va->va_addr, va->va_size, 0, &ioh))
    188  1.1       igy 		return 0;
    189  1.1       igy 	if (!probe_cfi(va->va_iot, ioh)) {
    190  1.1       igy 		DPRINTF("CFI ID str and command set recognized\n");
    191  1.1       igy 		goto detect;
    192  1.1       igy 	}
    193  1.1       igy 	if (!i28f128_probe(va->va_iot, ioh)) {
    194  1.1       igy 		DPRINTF("28F128 detected\n");
    195  1.1       igy 		goto detect;
    196  1.1       igy 	}
    197  1.1       igy 	if (!mbm29160_probe(va->va_iot, ioh)) {
    198  1.1       igy 		DPRINTF("29LV160 detected\n");
    199  1.1       igy 		goto detect;
    200  1.1       igy 	}
    201  1.1       igy 	return 0;
    202  1.1       igy 
    203  1.1       igy detect:
    204  1.1       igy 	bus_space_unmap(va->va_iot, ioh, va->va_size);
    205  1.1       igy 	return 1;
    206  1.1       igy }
    207  1.1       igy 
    208  1.1       igy static void
    209  1.8       chs flash_attach(device_t parent, device_t self, void *aux)
    210  1.1       igy {
    211  1.8       chs 	struct flash_softc	*sc = device_private(self);
    212  1.1       igy 	struct vrip_attach_args	*va = aux;
    213  1.1       igy 	int			i;
    214  1.1       igy 	int			fence;
    215  1.1       igy 	bus_space_tag_t		iot = va->va_iot;
    216  1.1       igy 	bus_space_handle_t	ioh;
    217  1.1       igy 	size_t			block_size;
    218  1.1       igy 
    219  1.1       igy 	if (bus_space_map(iot, va->va_addr, va->va_size, 0, &ioh)) {
    220  1.1       igy 		printf(": can't map i/o space\n");
    221  1.1       igy                 return;
    222  1.1       igy   	}
    223  1.1       igy 
    224  1.1       igy 	sc->sc_iot = iot;
    225  1.1       igy 	sc->sc_ioh = ioh;
    226  1.1       igy 	sc->sc_size = va->va_size;
    227  1.1       igy 	sc->sc_status = 0;
    228  1.1       igy 
    229  1.1       igy 	/*
    230  1.1       igy 	 * Read entire CFI structure
    231  1.1       igy 	 */
    232  1.1       igy 	bus_space_write_2(iot, ioh, CFI_QUERY_OFFSET, CFI_READ_CFI_QUERY);
    233  1.1       igy 	for (i = 0; i < CFI_TOTAL_SIZE; i++) {
    234  1.1       igy 		sc->sc_cfi_raw[i] = bus_space_read_2(iot, ioh, i << 1);
    235  1.1       igy 	}
    236  1.1       igy 	bus_space_write_2(iot, ioh, 0, FLASH_RESET);
    237  1.1       igy 
    238  1.1       igy 	sc->sc_ops = find_command_set(sc->sc_cfi_raw[CFI_PRIM_COMM_REG0],
    239  1.1       igy 				      sc->sc_cfi_raw[CFI_PRIM_COMM_REG1]);
    240  1.1       igy 	if (sc->sc_ops) {
    241  1.1       igy 		printf(": using %s command set", sc->sc_ops->fo_name);
    242  1.1       igy 	} else {
    243  1.1       igy 		printf("opps sc->sc_ops is NULL\n");
    244  1.1       igy 	}
    245  1.1       igy 
    246  1.1       igy 	/*
    247  1.1       igy 	 * determine size of the largest block
    248  1.1       igy 	 */
    249  1.1       igy 	sc->sc_block_size = 0;
    250  1.1       igy 	i = CFI_EBLK1_INFO_REG;
    251  1.1       igy 	fence = sc->sc_cfi_raw[CFI_NUM_ERASE_BLK_REG] * CFI_EBLK_INFO_SIZE
    252  1.1       igy 		+ i;
    253  1.1       igy 	for (; i < fence; i += CFI_EBLK_INFO_SIZE) {
    254  1.1       igy 		if (sc->sc_cfi_raw[i + CFI_EBLK_INFO_NSECT0] == 0
    255  1.1       igy 		    && sc->sc_cfi_raw[i + CFI_EBLK_INFO_NSECT1] == 0)
    256  1.1       igy 			continue;
    257  1.1       igy 		block_size
    258  1.1       igy 			= (sc->sc_cfi_raw[i + CFI_EBLK_INFO_SECSIZE0] << 8)
    259  1.1       igy 			+ (sc->sc_cfi_raw[i + CFI_EBLK_INFO_SECSIZE1] << 16);
    260  1.1       igy 		if (sc->sc_block_size < block_size)
    261  1.1       igy 			sc->sc_block_size = block_size;
    262  1.1       igy 	}
    263  1.1       igy 
    264  1.1       igy 	if ((sc->sc_buf = malloc(sc->sc_block_size, M_DEVBUF, M_NOWAIT))
    265  1.1       igy 	    == NULL) {
    266  1.1       igy 		printf(": can't alloc buffer space\n");
    267  1.1       igy 		return;
    268  1.1       igy 	}
    269  1.1       igy 
    270  1.1       igy 	sc->sc_write_buffer_size
    271  1.1       igy 		= 1 << (sc->sc_cfi_raw[CFI_MAX_WBUF_SIZE_REG0]
    272  1.1       igy 			+ (sc->sc_cfi_raw[CFI_MAX_WBUF_SIZE_REG1] << 8));
    273  1.1       igy 	sc->sc_typ_word_prog_timo
    274  1.1       igy 		= 1 << sc->sc_cfi_raw[CFI_TYP_WORD_PROG_REG];
    275  1.1       igy 	sc->sc_max_word_prog_timo
    276  1.1       igy 		= 1 << sc->sc_cfi_raw[CFI_MAX_WORD_PROG_REG];
    277  1.1       igy 	sc->sc_typ_buffer_write_timo
    278  1.1       igy 		= 1 << sc->sc_cfi_raw[CFI_TYP_BUF_WRITE_REG];
    279  1.1       igy 	sc->sc_max_buffer_write_timo
    280  1.1       igy 		= 1 << sc->sc_cfi_raw[CFI_MAX_BUF_WRITE_REG];
    281  1.1       igy 	sc->sc_typ_block_erase_timo
    282  1.1       igy 		= 1 << sc->sc_cfi_raw[CFI_TYP_BLOCK_ERASE_REG];
    283  1.1       igy 	sc->sc_max_block_erase_timo
    284  1.1       igy 		= 1 << sc->sc_cfi_raw[CFI_MAX_BLOCK_ERASE_REG];
    285  1.1       igy 
    286  1.1       igy 	printf("\n");
    287  1.1       igy 
    288  1.1       igy #ifdef FLASH_DEBUG
    289  1.1       igy 	printf("read_cfi: extract cfi\n");
    290  1.1       igy 	printf("max block size: %dbyte\n", sc->sc_block_size);
    291  1.1       igy 	printf("write buffer size: %dbyte\n", sc->sc_write_buffer_size);
    292  1.1       igy 	printf("typical word program timeout: %dusec\n",
    293  1.1       igy 	       sc->sc_typ_word_prog_timo);
    294  1.1       igy 	printf("maximam word program timeout: %dusec (%d time of typ)\n",
    295  1.1       igy 	       sc->sc_typ_word_prog_timo * sc->sc_max_word_prog_timo,
    296  1.1       igy 	       sc->sc_max_word_prog_timo);
    297  1.1       igy 	printf("typical buffer write timeout: %dusec\n",
    298  1.1       igy 	       sc->sc_typ_buffer_write_timo);
    299  1.1       igy 	printf("maximam buffer write timeout: %dusec (%d time of typ)\n",
    300  1.1       igy 	       sc->sc_typ_buffer_write_timo * sc->sc_max_buffer_write_timo,
    301  1.1       igy 	       sc->sc_max_buffer_write_timo);
    302  1.1       igy 	printf("typical block erase timeout: %dmsec\n",
    303  1.1       igy 	       sc->sc_typ_block_erase_timo);
    304  1.1       igy 	printf("maximam block erase timeout: %dmsec (%d time of typ)\n",
    305  1.1       igy 	       sc->sc_typ_block_erase_timo * sc->sc_max_block_erase_timo,
    306  1.1       igy 	       sc->sc_max_block_erase_timo);
    307  1.1       igy 
    308  1.1       igy 	printf("read_cfi: dump cfi\n");
    309  1.1       igy 	for (i = 0; i < CFI_TOTAL_SIZE;) {
    310  1.1       igy 		int	j;
    311  1.1       igy 		for (j = 0; j < 16; j++) {
    312  1.1       igy 			printf("%02x ", sc->sc_cfi_raw[i++]);
    313  1.1       igy 		}
    314  1.1       igy 		printf("\n");
    315  1.1       igy 	}
    316  1.1       igy #endif
    317  1.1       igy }
    318  1.1       igy 
    319  1.1       igy int
    320  1.5  christos flashopen(dev_t dev, int flag, int mode, struct lwp *l)
    321  1.1       igy {
    322  1.1       igy 	struct flash_softc	*sc;
    323  1.1       igy 
    324  1.7    cegger 	sc = device_lookup_private(&flash_cd, minor(dev));
    325  1.7    cegger 	if (sc == NULL)
    326  1.1       igy 		return ENXIO;
    327  1.1       igy 	if (sc->sc_status & FLASH_ST_BUSY)
    328  1.1       igy 		return EBUSY;
    329  1.1       igy 	sc->sc_status |= FLASH_ST_BUSY;
    330  1.1       igy 	return 0;
    331  1.1       igy }
    332  1.1       igy 
    333  1.1       igy int
    334  1.5  christos flashclose(dev_t dev, int flag, int mode, struct lwp *l)
    335  1.1       igy {
    336  1.1       igy 	struct flash_softc	*sc;
    337  1.1       igy 
    338  1.7    cegger 	sc = device_lookup_private(&flash_cd, minor(dev));
    339  1.1       igy 	sc->sc_status &= ~FLASH_ST_BUSY;
    340  1.1       igy 	return 0;
    341  1.1       igy }
    342  1.1       igy 
    343  1.1       igy int
    344  1.1       igy flashread(dev_t dev, struct uio *uio, int flag)
    345  1.1       igy {
    346  1.1       igy 	struct flash_softc	*sc;
    347  1.1       igy 	bus_space_tag_t		iot;
    348  1.1       igy 	bus_space_handle_t	ioh;
    349  1.1       igy 	bus_size_t		off;
    350  1.1       igy 	int			total;
    351  1.1       igy 	int			count;
    352  1.1       igy 	int			error;
    353  1.1       igy 
    354  1.7    cegger 	sc = device_lookup_private(&flash_cd, minor(dev));
    355  1.1       igy 	iot = sc->sc_iot;
    356  1.1       igy 	ioh = sc->sc_ioh;
    357  1.1       igy 
    358  1.1       igy 	off = uio->uio_offset;
    359  1.1       igy 	total = min(sc->sc_size - off, uio->uio_resid);
    360  1.1       igy 
    361  1.1       igy 	while (total > 0) {
    362  1.1       igy 		count = min(sc->sc_block_size, uio->uio_resid);
    363  1.1       igy 		bus_space_read_region_1(iot, ioh, off, sc->sc_buf, count);
    364  1.1       igy 		if ((error = uiomove(sc->sc_buf, count, uio)) != 0)
    365  1.1       igy 			return error;
    366  1.1       igy 		off += count;
    367  1.1       igy 		total -= count;
    368  1.1       igy 	}
    369  1.1       igy 	return 0;
    370  1.1       igy }
    371  1.1       igy 
    372  1.1       igy 
    373  1.1       igy int
    374  1.1       igy flashwrite(dev_t dev, struct uio *uio, int flag)
    375  1.1       igy {
    376  1.1       igy 	struct flash_softc	*sc;
    377  1.1       igy 	bus_space_tag_t		iot;
    378  1.1       igy 	bus_space_handle_t	ioh;
    379  1.1       igy 	bus_size_t		off;
    380  1.1       igy 	int			stat;
    381  1.1       igy 	int			error;
    382  1.1       igy 
    383  1.7    cegger 	sc = device_lookup_private(&flash_cd, minor(dev));
    384  1.1       igy 
    385  1.1       igy 	if (sc->sc_size < uio->uio_offset + uio->uio_resid)
    386  1.1       igy 		return ENOSPC;
    387  1.1       igy 	if (uio->uio_offset % sc->sc_block_size)
    388  1.1       igy 		return EINVAL;
    389  1.1       igy 	if (uio->uio_resid % sc->sc_block_size)
    390  1.1       igy 		return EINVAL;
    391  1.1       igy 
    392  1.1       igy 	iot = sc->sc_iot;
    393  1.1       igy 	ioh = sc->sc_ioh;
    394  1.1       igy 
    395  1.1       igy 	for (off = uio->uio_offset;
    396  1.1       igy 	     uio->uio_resid > 0;
    397  1.1       igy 	     off += sc->sc_block_size) {
    398  1.1       igy 		if ((error = uiomove(sc->sc_buf, sc->sc_block_size, uio)) != 0)
    399  1.1       igy 			return error;
    400  1.1       igy 		if (is_block_same(sc, off, sc->sc_buf))
    401  1.1       igy 			continue;
    402  1.1       igy 		if ((stat = flash_block_erase(sc, off)) != 0) {
    403  1.1       igy 			printf("block erase failed status = 0x%x\n", stat);
    404  1.1       igy 			return EIO;
    405  1.1       igy 		}
    406  1.1       igy 		if ((stat = flash_block_write(sc, off)) != 0) {
    407  1.1       igy 			printf("block write failed status = 0x%x\n", stat);
    408  1.1       igy 			return EIO;
    409  1.1       igy 		}
    410  1.1       igy 	}
    411  1.1       igy 	return 0;
    412  1.1       igy }
    413  1.1       igy 
    414  1.1       igy /*
    415  1.1       igy  * XXX
    416  1.1       igy  * this function is too much specific for the device.
    417  1.1       igy  */
    418  1.1       igy static int
    419  1.1       igy i28f128_probe(bus_space_tag_t iot, bus_space_handle_t ioh)
    420  1.1       igy {
    421  1.1       igy 	static const u_int8_t	vendor_code[] = {
    422  1.1       igy 		0x89,	/* manufacturer code: 	intel */
    423  1.1       igy 		0x18,	/* device code:		28F128 */
    424  1.1       igy 	};
    425  1.1       igy 
    426  1.1       igy 	static const u_int8_t	idstr[] = {
    427  1.1       igy 		'Q', 'R', 'Y',
    428  1.1       igy 		0x01, 0x00,
    429  1.1       igy 		0x31, 0x00,
    430  1.1       igy 		0xff
    431  1.1       igy 	};
    432  1.1       igy 
    433  1.1       igy 	int	i;
    434  1.1       igy 
    435  1.1       igy 	/* start Common Flash Interface Query */
    436  1.1       igy 	bus_space_write_2(iot, ioh, 0, CFI_READ_CFI_QUERY);
    437  1.1       igy 	/* read CFI Query ID string */
    438  1.1       igy 	for (i = 0; idstr[i] != 0xff; i++) {
    439  1.1       igy 		if (bus_space_read_2(iot, ioh, (0x10 + i) << 1) != idstr[i])
    440  1.1       igy 			return 1;
    441  1.1       igy 	}
    442  1.1       igy 
    443  1.1       igy 	/* read manufacturer code and device code */
    444  1.1       igy 	if (bus_space_read_2(iot, ioh, 0x00) != vendor_code[0])
    445  1.1       igy 		return 1;
    446  1.1       igy 	if (bus_space_read_2(iot, ioh, 0x02) != vendor_code[1])
    447  1.1       igy 		return 1;
    448  1.1       igy 
    449  1.1       igy 	bus_space_write_2(iot, ioh, 0, I28F128_RESET);
    450  1.1       igy 	return 0;
    451  1.1       igy }
    452  1.1       igy 
    453  1.1       igy /*
    454  1.1       igy  * XXX
    455  1.1       igy  * this function is too much specific for the device.
    456  1.1       igy  */
    457  1.1       igy static int
    458  1.1       igy mbm29160_probe(bus_space_tag_t iot, bus_space_handle_t ioh)
    459  1.1       igy {
    460  1.1       igy 	static const u_int16_t	vendor_code[] = {
    461  1.1       igy 		0x0004,	/* manufacturer code: 	intel */
    462  1.1       igy 		0x2249,	/* device code:		29LV160BE */
    463  1.1       igy 	};
    464  1.1       igy 
    465  1.1       igy 	static const u_int8_t	idstr[] = {
    466  1.1       igy 		'Q', 'R', 'Y',
    467  1.1       igy 		0x02, 0x00,
    468  1.1       igy 		0x40, 0x00,
    469  1.1       igy 		0xff
    470  1.1       igy 	};
    471  1.1       igy 
    472  1.1       igy 	int	i;
    473  1.1       igy 
    474  1.1       igy 	/* start Common Flash Interface Query */
    475  1.1       igy 	bus_space_write_2(iot, ioh, 0xaa, CFI_READ_CFI_QUERY);
    476  1.1       igy 	/* read CFI Query ID string */
    477  1.1       igy 	for (i = 0; idstr[i] != 0xff; i++) {
    478  1.1       igy 		if (bus_space_read_2(iot, ioh, (0x10 + i) << 1) != idstr[i])
    479  1.1       igy 			return 1;
    480  1.1       igy 	}
    481  1.1       igy 
    482  1.1       igy 	bus_space_write_2(iot, ioh, 0, 0xff);
    483  1.1       igy 
    484  1.1       igy 	/* read manufacturer code and device code */
    485  1.1       igy 	bus_space_write_2(iot, ioh, 0x555 << 1, 0xaa);
    486  1.1       igy 	bus_space_write_2(iot, ioh, 0x2aa << 1, 0x55);
    487  1.1       igy 	bus_space_write_2(iot, ioh, 0x555 << 1, 0x90);
    488  1.1       igy 	if (bus_space_read_2(iot, ioh, 0x00) != vendor_code[0])
    489  1.1       igy 		return 1;
    490  1.1       igy 	if (bus_space_read_2(iot, ioh, 0x02) != vendor_code[1])
    491  1.1       igy 		return 1;
    492  1.1       igy 
    493  1.1       igy 	bus_space_write_2(iot, ioh, 0, 0xff);
    494  1.1       igy 	return 0;
    495  1.1       igy }
    496  1.1       igy 
    497  1.1       igy static int
    498  1.1       igy is_block_same(struct flash_softc *sc, bus_size_t offset, const void *bufp)
    499  1.1       igy {
    500  1.1       igy 	bus_space_tag_t		iot = sc->sc_iot;
    501  1.1       igy 	bus_space_handle_t	ioh = sc->sc_ioh;
    502  1.1       igy 	const u_int8_t		*p = bufp;
    503  1.1       igy 	int			count = sc->sc_block_size;
    504  1.1       igy 
    505  1.1       igy 	while (count-- > 0) {
    506  1.1       igy 		if (bus_space_read_1(iot, ioh, offset++) != *p++)
    507  1.1       igy 			return 0;
    508  1.1       igy 	}
    509  1.1       igy 	return 1;
    510  1.1       igy }
    511  1.1       igy 
    512  1.1       igy static int
    513  1.1       igy intel_erase(struct flash_softc *sc, bus_size_t offset)
    514  1.1       igy {
    515  1.1       igy 	bus_space_tag_t		iot = sc->sc_iot;
    516  1.1       igy 	bus_space_handle_t	ioh = sc->sc_ioh;
    517  1.1       igy 	int			status;
    518  1.1       igy 	int			i;
    519  1.1       igy 
    520  1.1       igy 	bus_space_write_2(iot, ioh, offset, I28F128_BLK_ERASE_1ST);
    521  1.1       igy 	bus_space_write_2(iot, ioh, offset, I28F128_BLK_ERASE_2ND);
    522  1.1       igy 
    523  1.3       igy 	status = 0;
    524  1.1       igy 	for (i = sc->sc_max_block_erase_timo; i > 0; i--) {
    525  1.1       igy 		tsleep(sc, PRIBIO, "blockerase",
    526  1.1       igy 		       1 + (sc->sc_typ_block_erase_timo * hz) / 1000);
    527  1.1       igy 		if ((status = bus_space_read_2(iot, ioh, offset))
    528  1.1       igy 		    & I28F128_S_READY)
    529  1.1       igy 			break;
    530  1.1       igy 	}
    531  1.4       igy 	if (i == 0)
    532  1.4       igy 		status |= FLASH_TIMEOUT;
    533  1.1       igy 
    534  1.1       igy 	bus_space_write_2(iot, ioh, offset, I28F128_CLEAR_STATUS);
    535  1.1       igy 	bus_space_write_2(iot, ioh, offset, I28F128_RESET);
    536  1.1       igy 
    537  1.4       igy 	return status & (FLASH_TIMEOUT
    538  1.4       igy 			 | I28F128_S_ERASE_SUSPEND
    539  1.1       igy 			 | I28F128_S_COMSEQ_ERROR
    540  1.1       igy 			 | I28F128_S_ERASE_ERROR
    541  1.1       igy 			 | I28F128_S_BLOCK_LOCKED);
    542  1.1       igy }
    543  1.1       igy 
    544  1.1       igy static int
    545  1.1       igy intel_write(struct flash_softc *sc, bus_size_t offset)
    546  1.1       igy {
    547  1.1       igy 	bus_space_tag_t		iot = sc->sc_iot;
    548  1.1       igy 	bus_space_handle_t	ioh = sc->sc_ioh;
    549  1.1       igy 	int			wbuf_size;
    550  1.1       igy 	int			timo;
    551  1.1       igy 	int			status;
    552  1.1       igy 	bus_size_t		fence;
    553  1.1       igy 	int			i;
    554  1.1       igy 	const u_int16_t		*p;
    555  1.1       igy 
    556  1.1       igy 	/* wbuf_size = size in u_int16_t */
    557  1.1       igy 	wbuf_size = sc->sc_write_buffer_size >> 1;
    558  1.1       igy 
    559  1.1       igy 	p = (u_int16_t *) sc->sc_buf;
    560  1.1       igy 	fence = offset + sc->sc_block_size;
    561  1.1       igy 	do {
    562  1.3       igy 		status = 0;
    563  1.1       igy 		for (timo = sc->sc_max_buffer_write_timo; timo > 0; timo--) {
    564  1.1       igy 			bus_space_write_2(iot, ioh, offset,
    565  1.1       igy 					  I28F128_WRITE_BUFFER);
    566  1.1       igy 			status = bus_space_read_2(iot, ioh, offset);
    567  1.1       igy 			if (status & I28F128_XS_BUF_AVAIL)
    568  1.1       igy 				break;
    569  1.1       igy 			DELAY(sc->sc_typ_buffer_write_timo);
    570  1.1       igy 		}
    571  1.1       igy 		if (timo == 0) {
    572  1.1       igy 			status |= FLASH_TIMEOUT;
    573  1.1       igy 			goto errout;
    574  1.1       igy 		}
    575  1.1       igy 
    576  1.1       igy 		bus_space_write_2(iot, ioh, offset, wbuf_size - 1);
    577  1.1       igy 
    578  1.1       igy 		for (i = wbuf_size; i > 0; i--, p++, offset += 2)
    579  1.1       igy 			bus_space_write_2(iot, ioh, offset, *p);
    580  1.1       igy 
    581  1.1       igy 		bus_space_write_2(iot, ioh, offset, I28F128_WBUF_CONFIRM);
    582  1.1       igy 
    583  1.1       igy 		do {
    584  1.1       igy 			bus_space_write_2(iot, ioh, offset,
    585  1.1       igy 					  I28F128_READ_STATUS);
    586  1.1       igy 			status = bus_space_read_2(iot, ioh, offset);
    587  1.1       igy 		} while (!(status & I28F128_S_READY));
    588  1.1       igy 
    589  1.1       igy 	} while (offset < fence);
    590  1.1       igy 
    591  1.1       igy 	bus_space_write_2(iot, ioh, offset, I28F128_CLEAR_STATUS);
    592  1.1       igy 	bus_space_write_2(iot, ioh, offset, I28F128_RESET);
    593  1.1       igy 
    594  1.1       igy 	return 0;
    595  1.1       igy 
    596  1.1       igy errout:
    597  1.1       igy 	bus_space_write_2(iot, ioh, offset, I28F128_CLEAR_STATUS);
    598  1.1       igy 	bus_space_write_2(iot, ioh, offset, I28F128_RESET);
    599  1.1       igy 
    600  1.1       igy 	status &= (FLASH_TIMEOUT
    601  1.1       igy 		   | I28F128_S_PROG_ERROR
    602  1.1       igy 		   | I28F128_S_COMSEQ_ERROR
    603  1.1       igy 		   | I28F128_S_LOW_VOLTAGE
    604  1.1       igy 		   | I28F128_S_PROG_SUSPEND
    605  1.1       igy 		   | I28F128_S_BLOCK_LOCKED);
    606  1.1       igy 	return status;
    607  1.1       igy }
    608  1.1       igy 
    609  1.1       igy static int
    610  1.1       igy amd_erase_sector(struct flash_softc *sc, bus_size_t offset)
    611  1.1       igy {
    612  1.1       igy 	bus_space_tag_t		iot = sc->sc_iot;
    613  1.1       igy 	bus_space_handle_t	ioh = sc->sc_ioh;
    614  1.1       igy 	int			i;
    615  1.1       igy 
    616  1.1       igy 	DPRINTF(("amd_erase_sector offset = %08lx\n", offset));
    617  1.1       igy 
    618  1.1       igy 	bus_space_write_2(iot, ioh,
    619  1.1       igy 			  MBM29LV160_COMM_ADDR0, MBM29LV160_COMM_CMD0);
    620  1.1       igy 	bus_space_write_2(iot, ioh,
    621  1.1       igy 			  MBM29LV160_COMM_ADDR1, MBM29LV160_COMM_CMD1);
    622  1.1       igy 	bus_space_write_2(iot, ioh,
    623  1.1       igy 			  MBM29LV160_COMM_ADDR2, MBM29LV160_ESECT_CMD2);
    624  1.1       igy 	bus_space_write_2(iot, ioh,
    625  1.1       igy 			  MBM29LV160_COMM_ADDR3, MBM29LV160_ESECT_CMD3);
    626  1.1       igy 	bus_space_write_2(iot, ioh,
    627  1.1       igy 			  MBM29LV160_COMM_ADDR4, MBM29LV160_ESECT_CMD4);
    628  1.1       igy 	bus_space_write_2(iot, ioh, offset, MBM29LV160_ESECT_CMD5);
    629  1.1       igy 
    630  1.1       igy 	for (i = sc->sc_max_block_erase_timo; i > 0; i--) {
    631  1.1       igy 		tsleep(sc, PRIBIO, "blockerase",
    632  1.1       igy 		       1 + (sc->sc_typ_block_erase_timo * hz) / 1000);
    633  1.1       igy 		if (bus_space_read_2(iot, ioh, offset) == 0xffff)
    634  1.1       igy 			return 0;
    635  1.1       igy 	}
    636  1.1       igy 
    637  1.1       igy 	return FLASH_TIMEOUT;
    638  1.1       igy }
    639  1.1       igy 
    640  1.1       igy static int
    641  1.1       igy amd_erase(struct flash_softc *sc, bus_size_t offset)
    642  1.1       igy {
    643  1.1       igy 	static const struct mbm29lv_subsect {
    644  1.1       igy 		u_int16_t	devcode;
    645  1.1       igy 		u_int32_t	subsect_mask;
    646  1.1       igy 		u_int32_t	subsect_addr;
    647  1.1       igy 	} subsect[] = {
    648  1.1       igy 		{
    649  1.1       igy 			MBM29LV160TE_DEVCODE,
    650  1.1       igy 			MBM29LV160_SUBSECT_MASK,
    651  1.1       igy 			MBM29LV160TE_SUBSECT_ADDR
    652  1.1       igy 		},
    653  1.1       igy 		{
    654  1.1       igy 			MBM29LV160BE_DEVCODE,
    655  1.1       igy 			MBM29LV160_SUBSECT_MASK,
    656  1.1       igy 			MBM29LV160BE_SUBSECT_ADDR
    657  1.1       igy 		},
    658  1.1       igy 		{ 0, 0, 0 }
    659  1.1       igy 	};
    660  1.1       igy 
    661  1.1       igy 	bus_space_tag_t			iot = sc->sc_iot;
    662  1.1       igy 	bus_space_handle_t		ioh = sc->sc_ioh;
    663  1.1       igy 	u_int16_t			devcode;
    664  1.1       igy 	const struct mbm29lv_subsect	*ss;
    665  1.1       igy 	bus_size_t			fence;
    666  1.1       igy 	int				step;
    667  1.1       igy 	int				status;
    668  1.1       igy 
    669  1.1       igy 	bus_space_write_2(iot, ioh,
    670  1.1       igy 			  MBM29LV160_COMM_ADDR0, MBM29LV160_COMM_CMD0);
    671  1.1       igy 	bus_space_write_2(iot, ioh,
    672  1.1       igy 			  MBM29LV160_COMM_ADDR1, MBM29LV160_COMM_CMD1);
    673  1.1       igy 	bus_space_write_2(iot, ioh,
    674  1.1       igy 			  MBM29LV160_COMM_ADDR2, MBM29LV160_SIGN_CMD2);
    675  1.1       igy 	devcode = bus_space_read_2(iot, ioh, MBM29LV160_DEVCODE_REG);
    676  1.1       igy 
    677  1.1       igy 	for (ss = subsect; ss->devcode; ss++) {
    678  1.1       igy 		if (ss->devcode == devcode)
    679  1.1       igy 			break;
    680  1.1       igy 	}
    681  1.1       igy 	if (ss->devcode == 0) {
    682  1.1       igy 		printf("flash: amd_erase(): unknown device code %04x\n",
    683  1.1       igy 		       devcode);
    684  1.1       igy 		return -1;
    685  1.1       igy 	}
    686  1.1       igy 
    687  1.1       igy 	DPRINTF(("flash: amd_erase(): devcode = %04x subsect = %08x\n",
    688  1.1       igy 		 devcode, ss->subsect_addr));
    689  1.1       igy 
    690  1.1       igy 	fence = offset + sc->sc_block_size;
    691  1.1       igy 	step = (offset & ss->subsect_mask) == ss->subsect_addr
    692  1.1       igy 		? MBM29LV160_SUBSECT_SIZE : MBM29LV160_SECT_SIZE;
    693  1.1       igy 	do {
    694  1.1       igy 		if ((status = amd_erase_sector(sc, offset)) != 0)
    695  1.1       igy 			return status;
    696  1.1       igy 		offset += step;
    697  1.1       igy 	} while (offset < fence);
    698  1.1       igy 
    699  1.1       igy 	return 0;
    700  1.1       igy }
    701  1.1       igy 
    702  1.1       igy static int
    703  1.1       igy amd_write(struct flash_softc *sc, bus_size_t offset)
    704  1.1       igy {
    705  1.1       igy 	bus_space_tag_t		iot = sc->sc_iot;
    706  1.1       igy 	bus_space_handle_t	ioh = sc->sc_ioh;
    707  1.1       igy 	int			timo;
    708  1.1       igy 	bus_size_t		fence;
    709  1.1       igy 	const u_int16_t		*p;
    710  1.1       igy 
    711  1.1       igy 	p = (u_int16_t *) sc->sc_buf;
    712  1.1       igy 	fence = offset + sc->sc_block_size;
    713  1.1       igy 	do {
    714  1.1       igy 		bus_space_write_2(iot, ioh,
    715  1.1       igy 				  MBM29LV160_COMM_ADDR0,
    716  1.1       igy 				  MBM29LV160_COMM_CMD0);
    717  1.1       igy 		bus_space_write_2(iot, ioh,
    718  1.1       igy 				  MBM29LV160_COMM_ADDR1,
    719  1.1       igy 				  MBM29LV160_COMM_CMD1);
    720  1.1       igy 		bus_space_write_2(iot, ioh,
    721  1.1       igy 				  MBM29LV160_COMM_ADDR2,
    722  1.1       igy 				  MBM29LV160_PROG_CMD2);
    723  1.1       igy 		bus_space_write_2(iot, ioh, offset, *p);
    724  1.1       igy 
    725  1.1       igy 		for (timo = sc->sc_max_word_prog_timo; timo > 0; timo--) {
    726  1.1       igy 			if (bus_space_read_2(iot, ioh, offset) == *p)
    727  1.1       igy 				break;
    728  1.1       igy 			DELAY(sc->sc_typ_word_prog_timo);
    729  1.1       igy 		}
    730  1.1       igy 		if (timo == 0)
    731  1.1       igy 			return FLASH_TIMEOUT;
    732  1.1       igy 
    733  1.1       igy 		p++;
    734  1.1       igy 		offset += 2;
    735  1.1       igy 	} while (offset < fence);
    736  1.1       igy 
    737  1.1       igy 	return 0;
    738  1.1       igy }
    739