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