Home | History | Annotate | Line # | Download | only in dev
athflash.c revision 1.7
      1  1.7  dholland /* $NetBSD: athflash.c,v 1.7 2014/03/16 05:20:25 dholland Exp $ */
      2  1.1   gdamore 
      3  1.1   gdamore /*
      4  1.1   gdamore  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      5  1.1   gdamore  * Copyright (c) 2006 Garrett D'Amore.
      6  1.1   gdamore  * All rights reserved.
      7  1.1   gdamore  *
      8  1.1   gdamore  * Portions of this code were written by Garrett D'Amore for the
      9  1.1   gdamore  * Champaign-Urbana Community Wireless Network Project.
     10  1.1   gdamore  *
     11  1.1   gdamore  * Redistribution and use in source and binary forms, with or
     12  1.1   gdamore  * without modification, are permitted provided that the following
     13  1.1   gdamore  * conditions are met:
     14  1.1   gdamore  * 1. Redistributions of source code must retain the above copyright
     15  1.1   gdamore  *    notice, this list of conditions and the following disclaimer.
     16  1.1   gdamore  * 2. Redistributions in binary form must reproduce the above
     17  1.1   gdamore  *    copyright notice, this list of conditions and the following
     18  1.1   gdamore  *    disclaimer in the documentation and/or other materials provided
     19  1.1   gdamore  *    with the distribution.
     20  1.1   gdamore  * 3. All advertising materials mentioning features or use of this
     21  1.1   gdamore  *    software must display the following acknowledgements:
     22  1.1   gdamore  *      This product includes software developed by the Urbana-Champaign
     23  1.1   gdamore  *      Independent Media Center.
     24  1.1   gdamore  *	This product includes software developed by Garrett D'Amore.
     25  1.1   gdamore  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     26  1.1   gdamore  *    D'Amore's name may not be used to endorse or promote products
     27  1.1   gdamore  *    derived from this software without specific prior written permission.
     28  1.1   gdamore  *
     29  1.1   gdamore  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     30  1.1   gdamore  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     31  1.1   gdamore  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     32  1.1   gdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33  1.1   gdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     34  1.1   gdamore  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     35  1.1   gdamore  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     36  1.1   gdamore  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     37  1.1   gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     38  1.1   gdamore  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  1.1   gdamore  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40  1.1   gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     41  1.1   gdamore  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     42  1.1   gdamore  */
     43  1.1   gdamore /*
     44  1.1   gdamore  * Copyright (c) 2002 The NetBSD Foundation, Inc.
     45  1.1   gdamore  * All rights reserved.
     46  1.1   gdamore  *
     47  1.1   gdamore  * This code is derived from software contributed to The NetBSD Foundation
     48  1.1   gdamore  * by Naoto Shimazaki of YOKOGAWA Electric Corporation.
     49  1.1   gdamore  *
     50  1.1   gdamore  * Redistribution and use in source and binary forms, with or without
     51  1.1   gdamore  * modification, are permitted provided that the following conditions
     52  1.1   gdamore  * are met:
     53  1.1   gdamore  * 1. Redistributions of source code must retain the above copyright
     54  1.1   gdamore  *    notice, this list of conditions and the following disclaimer.
     55  1.1   gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     56  1.1   gdamore  *    notice, this list of conditions and the following disclaimer in the
     57  1.1   gdamore  *    documentation and/or other materials provided with the distribution.
     58  1.1   gdamore  *
     59  1.1   gdamore  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     60  1.1   gdamore  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     61  1.1   gdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     62  1.1   gdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     63  1.1   gdamore  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     64  1.1   gdamore  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     65  1.1   gdamore  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     66  1.1   gdamore  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     67  1.1   gdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     68  1.1   gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     69  1.1   gdamore  * POSSIBILITY OF SUCH DAMAGE.
     70  1.1   gdamore  */
     71  1.1   gdamore 
     72  1.1   gdamore /*
     73  1.1   gdamore  * Flash Memory Driver
     74  1.1   gdamore  *
     75  1.1   gdamore  * XXX This primitive flash driver does *not* support boot sectored devices,
     76  1.1   gdamore  * XXX and only supports a fairly limited set of devices, that we are likely to
     77  1.1   gdamore  * XXX to find in an AP30.
     78  1.1   gdamore  * XXX
     79  1.1   gdamore  * XXX We also are only supporting flash widths of 16 _for the moment_, and
     80  1.1   gdamore  * XXX we are only supporting flash devices that use the AMD command sets.
     81  1.1   gdamore  * XXX All this should be reviewed and improved to be much more generic.
     82  1.1   gdamore  */
     83  1.1   gdamore 
     84  1.1   gdamore #include <sys/cdefs.h>
     85  1.7  dholland __KERNEL_RCSID(0, "$NetBSD: athflash.c,v 1.7 2014/03/16 05:20:25 dholland Exp $");
     86  1.1   gdamore 
     87  1.1   gdamore #include <sys/param.h>
     88  1.1   gdamore #include <sys/conf.h>
     89  1.1   gdamore #include <sys/device.h>
     90  1.1   gdamore #include <sys/kernel.h>
     91  1.1   gdamore #include <sys/malloc.h>
     92  1.1   gdamore #include <sys/proc.h>
     93  1.1   gdamore #include <sys/systm.h>
     94  1.1   gdamore 
     95  1.4    dyoung #include <sys/bus.h>
     96  1.1   gdamore 
     97  1.1   gdamore #include <mips/atheros/include/arbusvar.h>
     98  1.1   gdamore 
     99  1.1   gdamore #ifdef FLASH_DEBUG
    100  1.1   gdamore int	flash_debug = 0;
    101  1.1   gdamore #define DPRINTF(x)	if (flash_debug) printf x
    102  1.1   gdamore #else
    103  1.1   gdamore #define DPRINTF(x)
    104  1.1   gdamore #endif
    105  1.1   gdamore 
    106  1.1   gdamore struct flash_softc {
    107  1.1   gdamore 	bus_space_tag_t		sc_iot;
    108  1.1   gdamore 	bus_space_handle_t	sc_ioh;
    109  1.1   gdamore 	size_t			sc_size;
    110  1.1   gdamore 	size_t			sc_sector_size;
    111  1.1   gdamore 	int			sc_status;
    112  1.1   gdamore 	u_int8_t		*sc_buf;
    113  1.1   gdamore };
    114  1.1   gdamore 
    115  1.1   gdamore #define	FLASH_ST_BUSY	0x1
    116  1.1   gdamore 
    117  1.6       chs static int flash_probe(device_t, cfdata_t, void *);
    118  1.6       chs static void flash_attach(device_t, device_t, void *);
    119  1.1   gdamore 
    120  1.1   gdamore static int is_block_same(struct flash_softc *, bus_size_t, const void *);
    121  1.1   gdamore static int toggle_bit_wait(struct flash_softc *, bus_size_t, int, int, int);
    122  1.1   gdamore 
    123  1.1   gdamore static int flash_sector_erase(struct flash_softc *, bus_size_t);
    124  1.1   gdamore static int flash_sector_write(struct flash_softc *, bus_size_t);
    125  1.1   gdamore 
    126  1.1   gdamore extern struct cfdriver athflash_cd;
    127  1.1   gdamore 
    128  1.6       chs CFATTACH_DECL_NEW(athflash, sizeof(struct flash_softc),
    129  1.1   gdamore 	      flash_probe, flash_attach, NULL, NULL);
    130  1.1   gdamore 
    131  1.1   gdamore dev_type_open(flashopen);
    132  1.1   gdamore dev_type_close(flashclose);
    133  1.1   gdamore dev_type_read(flashread);
    134  1.1   gdamore dev_type_write(flashwrite);
    135  1.1   gdamore 
    136  1.1   gdamore const struct cdevsw athflash_cdevsw = {
    137  1.7  dholland 	.d_open = flashopen,
    138  1.7  dholland 	.d_close = flashclose,
    139  1.7  dholland 	.d_read = flashread,
    140  1.7  dholland 	.d_write = flashwrite,
    141  1.7  dholland 	.d_ioctl = noioctl,
    142  1.7  dholland 	.d_stop = nostop,
    143  1.7  dholland 	.d_tty = notty,
    144  1.7  dholland 	.d_poll = nopoll,
    145  1.7  dholland 	.d_mmap = nommap,
    146  1.7  dholland 	.d_kqfilter = nokqfilter,
    147  1.7  dholland 	.d_flag = 0
    148  1.1   gdamore };
    149  1.1   gdamore 
    150  1.1   gdamore static struct {
    151  1.1   gdamore 	uint16_t		vendor_id;
    152  1.1   gdamore 	uint16_t		device_id;
    153  1.1   gdamore 	const char		*name;
    154  1.1   gdamore 	int			sector_size;
    155  1.1   gdamore 	int			flash_size;
    156  1.1   gdamore } flash_ids[] = {
    157  1.1   gdamore 	{ 0x00bf, 0x2780, "SST 39VF400", 0x01000, 0x080000 },	/* 512KB */
    158  1.1   gdamore 	{ 0x00bf, 0x2782, "SST 39VF160", 0x01000, 0x200000 },	/* 2MB */
    159  1.1   gdamore 	{ 0xffff, 0xffff, NULL, 0, 0 }				/* end list */
    160  1.1   gdamore };
    161  1.1   gdamore 
    162  1.1   gdamore static int
    163  1.6       chs flash_probe(device_t parent, cfdata_t cf, void *aux)
    164  1.1   gdamore {
    165  1.1   gdamore 	struct arbus_attach_args	*aa = aux;
    166  1.1   gdamore 	bus_space_handle_t		ioh;
    167  1.1   gdamore 	int				rv = 0, i;
    168  1.1   gdamore 	uint16_t			venid, devid;
    169  1.1   gdamore 
    170  1.1   gdamore 	if (strcmp(aa->aa_name, cf->cf_name) != 0)
    171  1.1   gdamore 		return 0;
    172  1.1   gdamore 
    173  1.1   gdamore 	DPRINTF(("trying to map address %x\n", (unsigned)aa->aa_addr));
    174  1.1   gdamore 	if (bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 0, &ioh))
    175  1.1   gdamore 		return 0;
    176  1.1   gdamore 
    177  1.1   gdamore 	/* issue JEDEC query */
    178  1.1   gdamore 	DPRINTF(("issuing JEDEC query\n"));
    179  1.1   gdamore 	bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0xAAAA);
    180  1.1   gdamore 	bus_space_write_2(aa->aa_bst, ioh, (0x2AAA << 1), 0x5555);
    181  1.1   gdamore 	bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0x9090);
    182  1.1   gdamore 
    183  1.1   gdamore 	delay(100);
    184  1.1   gdamore 	venid = bus_space_read_2(aa->aa_bst, ioh, 0);
    185  1.1   gdamore 	devid = bus_space_read_2(aa->aa_bst, ioh, 2);
    186  1.1   gdamore 
    187  1.1   gdamore 	/* issue software exit */
    188  1.1   gdamore 	bus_space_write_2(aa->aa_bst, ioh, 0x0, 0xF0F0);
    189  1.1   gdamore 
    190  1.1   gdamore 	for (i = 0; flash_ids[i].name != NULL; i++) {
    191  1.1   gdamore 		if ((venid == flash_ids[i].vendor_id) &&
    192  1.1   gdamore 		    (devid == flash_ids[i].device_id)) {
    193  1.1   gdamore 			rv = 1;
    194  1.1   gdamore 			break;
    195  1.1   gdamore 		}
    196  1.1   gdamore 	}
    197  1.1   gdamore 
    198  1.1   gdamore 	bus_space_unmap(aa->aa_bst, ioh, aa->aa_size);
    199  1.1   gdamore 	return rv;
    200  1.1   gdamore }
    201  1.1   gdamore 
    202  1.1   gdamore static void
    203  1.6       chs flash_attach(device_t parent, device_t self, void *aux)
    204  1.1   gdamore {
    205  1.5    dyoung 	char nbuf[32];
    206  1.6       chs 	struct flash_softc		*sc = device_private(self);
    207  1.1   gdamore 	struct arbus_attach_args	*aa = aux;
    208  1.1   gdamore 	int				i;
    209  1.1   gdamore 	bus_space_tag_t			iot = aa->aa_bst;
    210  1.1   gdamore 	bus_space_handle_t		ioh;
    211  1.1   gdamore 	uint16_t			venid, devid;
    212  1.1   gdamore 
    213  1.1   gdamore 	if (bus_space_map(iot, aa->aa_addr, aa->aa_size, 0, &ioh)) {
    214  1.1   gdamore 		printf(": can't map i/o space\n");
    215  1.1   gdamore                 return;
    216  1.1   gdamore   	}
    217  1.1   gdamore 
    218  1.1   gdamore 	sc->sc_iot = iot;
    219  1.1   gdamore 	sc->sc_ioh = ioh;
    220  1.1   gdamore 	sc->sc_status = 0;
    221  1.1   gdamore 
    222  1.1   gdamore 	/* issue JEDEC query */
    223  1.1   gdamore 	bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0xAAAA);
    224  1.1   gdamore 	bus_space_write_2(aa->aa_bst, ioh, (0x2AAA << 1), 0x5555);
    225  1.1   gdamore 	bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0x9090);
    226  1.1   gdamore 
    227  1.1   gdamore 	delay(100);
    228  1.1   gdamore 	venid = bus_space_read_2(aa->aa_bst, ioh, 0);
    229  1.1   gdamore 	devid = bus_space_read_2(aa->aa_bst, ioh, 2);
    230  1.1   gdamore 
    231  1.1   gdamore 	/* issue software exit */
    232  1.1   gdamore 	bus_space_write_2(aa->aa_bst, ioh, 0x0, 0xF0F0);
    233  1.1   gdamore 
    234  1.1   gdamore 	for (i = 0; flash_ids[i].name != NULL; i++) {
    235  1.1   gdamore 		if ((venid == flash_ids[i].vendor_id) &&
    236  1.1   gdamore 		    (devid == flash_ids[i].device_id)) {
    237  1.1   gdamore 			break;
    238  1.1   gdamore 		}
    239  1.1   gdamore 	}
    240  1.1   gdamore 
    241  1.1   gdamore 	KASSERT(flash_ids[i].name != NULL);
    242  1.5    dyoung 	printf(": %s", flash_ids[i].name);
    243  1.5    dyoung 	if (humanize_number(nbuf, sizeof(nbuf), flash_ids[i].flash_size, "B",
    244  1.5    dyoung 	    1024) > 0)
    245  1.5    dyoung 		printf(" (%s)", nbuf);
    246  1.1   gdamore 
    247  1.1   gdamore 	/*
    248  1.1   gdamore 	 * determine size of the largest block
    249  1.1   gdamore 	 */
    250  1.1   gdamore 	sc->sc_size = flash_ids[i].flash_size;
    251  1.1   gdamore 	sc->sc_sector_size = flash_ids[i].sector_size;
    252  1.5    dyoung 
    253  1.1   gdamore 	if ((sc->sc_buf = malloc(sc->sc_sector_size, M_DEVBUF, M_NOWAIT))
    254  1.1   gdamore 	    == NULL) {
    255  1.1   gdamore 		printf(": can't alloc buffer space\n");
    256  1.1   gdamore 		return;
    257  1.1   gdamore 	}
    258  1.1   gdamore 
    259  1.1   gdamore 	printf("\n");
    260  1.1   gdamore }
    261  1.1   gdamore 
    262  1.1   gdamore int
    263  1.1   gdamore flashopen(dev_t dev, int flag, int mode, struct lwp *l)
    264  1.1   gdamore {
    265  1.1   gdamore 	struct flash_softc	*sc;
    266  1.1   gdamore 
    267  1.3    cegger 	sc = device_lookup_private(&athflash_cd, minor(dev));
    268  1.3    cegger 	if (sc == NULL)
    269  1.1   gdamore 		return ENXIO;
    270  1.1   gdamore 	if (sc->sc_status & FLASH_ST_BUSY)
    271  1.1   gdamore 		return EBUSY;
    272  1.1   gdamore 	sc->sc_status |= FLASH_ST_BUSY;
    273  1.1   gdamore 	return 0;
    274  1.1   gdamore }
    275  1.1   gdamore 
    276  1.1   gdamore int
    277  1.1   gdamore flashclose(dev_t dev, int flag, int mode, struct lwp *l)
    278  1.1   gdamore {
    279  1.1   gdamore 	struct flash_softc	*sc;
    280  1.1   gdamore 
    281  1.3    cegger 	sc = device_lookup_private(&athflash_cd, minor(dev));
    282  1.1   gdamore 	sc->sc_status &= ~FLASH_ST_BUSY;
    283  1.1   gdamore 	return 0;
    284  1.1   gdamore }
    285  1.1   gdamore 
    286  1.1   gdamore int
    287  1.1   gdamore flashread(dev_t dev, struct uio *uio, int flag)
    288  1.1   gdamore {
    289  1.1   gdamore 	struct flash_softc	*sc;
    290  1.1   gdamore 	bus_space_tag_t		iot;
    291  1.1   gdamore 	bus_space_handle_t	ioh;
    292  1.1   gdamore 	bus_size_t		off;
    293  1.1   gdamore 	int			total;
    294  1.1   gdamore 	int			count;
    295  1.1   gdamore 	int			error;
    296  1.1   gdamore 
    297  1.3    cegger 	sc = device_lookup_private(&athflash_cd, minor(dev));
    298  1.1   gdamore 	iot = sc->sc_iot;
    299  1.1   gdamore 	ioh = sc->sc_ioh;
    300  1.1   gdamore 
    301  1.1   gdamore 	off = uio->uio_offset;
    302  1.1   gdamore 	total = min(sc->sc_size - off, uio->uio_resid);
    303  1.1   gdamore 
    304  1.1   gdamore 	while (total > 0) {
    305  1.1   gdamore 		count = min(sc->sc_sector_size, uio->uio_resid);
    306  1.1   gdamore 		bus_space_read_region_1(iot, ioh, off, sc->sc_buf, count);
    307  1.1   gdamore 		if ((error = uiomove(sc->sc_buf, count, uio)) != 0)
    308  1.1   gdamore 			return error;
    309  1.1   gdamore 		off += count;
    310  1.1   gdamore 		total -= count;
    311  1.1   gdamore 	}
    312  1.1   gdamore 	return 0;
    313  1.1   gdamore }
    314  1.1   gdamore 
    315  1.1   gdamore 
    316  1.1   gdamore int
    317  1.1   gdamore flashwrite(dev_t dev, struct uio *uio, int flag)
    318  1.1   gdamore {
    319  1.1   gdamore 	struct flash_softc	*sc;
    320  1.1   gdamore 	bus_space_tag_t		iot;
    321  1.1   gdamore 	bus_space_handle_t	ioh;
    322  1.1   gdamore 	bus_size_t		off;
    323  1.1   gdamore 	int			stat;
    324  1.1   gdamore 	int			error;
    325  1.1   gdamore 
    326  1.3    cegger 	sc = device_lookup_private(&athflash_cd, minor(dev));
    327  1.1   gdamore 
    328  1.1   gdamore 	if (sc->sc_size < uio->uio_offset + uio->uio_resid)
    329  1.1   gdamore 		return ENOSPC;
    330  1.1   gdamore 	if (uio->uio_offset % sc->sc_sector_size)
    331  1.1   gdamore 		return EINVAL;
    332  1.1   gdamore 	if (uio->uio_resid % sc->sc_sector_size)
    333  1.1   gdamore 		return EINVAL;
    334  1.1   gdamore 
    335  1.1   gdamore 	iot = sc->sc_iot;
    336  1.1   gdamore 	ioh = sc->sc_ioh;
    337  1.1   gdamore 
    338  1.1   gdamore 	for (off = uio->uio_offset;
    339  1.1   gdamore 	     uio->uio_resid > 0;
    340  1.1   gdamore 	     off += sc->sc_sector_size) {
    341  1.1   gdamore 		error = uiomove(sc->sc_buf, sc->sc_sector_size, uio);
    342  1.1   gdamore 		if (error != 0)
    343  1.1   gdamore 			return error;
    344  1.1   gdamore 		if (is_block_same(sc, off, sc->sc_buf))
    345  1.1   gdamore 			continue;
    346  1.1   gdamore 		if ((stat = flash_sector_erase(sc, off)) != 0) {
    347  1.1   gdamore 			printf("sector erase failed status = 0x%x\n", stat);
    348  1.1   gdamore 			return EIO;
    349  1.1   gdamore 		}
    350  1.1   gdamore 		if ((stat = flash_sector_write(sc, off)) != 0) {
    351  1.1   gdamore 			printf("sector write failed status = 0x%x\n", stat);
    352  1.1   gdamore 			return EIO;
    353  1.1   gdamore 		}
    354  1.1   gdamore 	}
    355  1.1   gdamore 	return 0;
    356  1.1   gdamore }
    357  1.1   gdamore 
    358  1.1   gdamore static int
    359  1.1   gdamore is_block_same(struct flash_softc *sc, bus_size_t offset, const void *bufp)
    360  1.1   gdamore {
    361  1.1   gdamore 	bus_space_tag_t		iot = sc->sc_iot;
    362  1.1   gdamore 	bus_space_handle_t	ioh = sc->sc_ioh;
    363  1.1   gdamore 	const u_int8_t		*p = bufp;
    364  1.1   gdamore 	int			count = sc->sc_sector_size;
    365  1.1   gdamore 
    366  1.1   gdamore 	while (count-- > 0) {
    367  1.1   gdamore 		if (bus_space_read_1(iot, ioh, offset++) != *p++)
    368  1.1   gdamore 			return 0;
    369  1.1   gdamore 	}
    370  1.1   gdamore 	return 1;
    371  1.1   gdamore }
    372  1.1   gdamore 
    373  1.1   gdamore static int
    374  1.1   gdamore toggle_bit_wait(struct flash_softc *sc, bus_size_t offset,
    375  1.1   gdamore     int typtmo, int maxtmo, int spin)
    376  1.1   gdamore {
    377  1.1   gdamore 	bus_space_tag_t		iot = sc->sc_iot;
    378  1.1   gdamore 	bus_space_handle_t	ioh = sc->sc_ioh;
    379  1.1   gdamore 	uint8_t			d1, d2;
    380  1.1   gdamore 
    381  1.1   gdamore 	while (maxtmo > 0) {
    382  1.1   gdamore 
    383  1.1   gdamore 		if (spin) {
    384  1.1   gdamore 			DELAY(typtmo);
    385  1.1   gdamore 		} else {
    386  1.1   gdamore 			tsleep(sc, PRIBIO, "blockerase",
    387  1.1   gdamore 			    (typtmo / hz) + 1);
    388  1.1   gdamore 		}
    389  1.1   gdamore 
    390  1.1   gdamore 		d1 = bus_space_read_1(iot, ioh, offset);
    391  1.1   gdamore 		d2 = bus_space_read_2(iot, ioh, offset);
    392  1.1   gdamore 
    393  1.1   gdamore 		/* watch for the toggle bit to stop toggling */
    394  1.1   gdamore 		if ((d1 & 0x40) == (d2 & 0x40)) {
    395  1.1   gdamore 			return 0;
    396  1.1   gdamore 		}
    397  1.1   gdamore 
    398  1.1   gdamore 		maxtmo -= typtmo;
    399  1.1   gdamore 	}
    400  1.1   gdamore 	return (ETIMEDOUT);
    401  1.1   gdamore }
    402  1.1   gdamore 
    403  1.1   gdamore static int
    404  1.1   gdamore flash_sector_erase(struct flash_softc *sc, bus_size_t offset)
    405  1.1   gdamore {
    406  1.1   gdamore 	bus_space_tag_t		iot = sc->sc_iot;
    407  1.1   gdamore 	bus_space_handle_t	ioh = sc->sc_ioh;
    408  1.1   gdamore 
    409  1.1   gdamore 	DPRINTF(("flash_sector_erase offset = %08lx\n", offset));
    410  1.1   gdamore 
    411  1.1   gdamore 	bus_space_write_2(iot, ioh, (0x5555 << 1), 0xAAAA);
    412  1.1   gdamore 	bus_space_write_2(iot, ioh, (0x2AAA << 1), 0x5555);
    413  1.1   gdamore 	bus_space_write_2(iot, ioh, (0x5555 << 1), 0x8080);
    414  1.1   gdamore 	bus_space_write_2(iot, ioh, (0x5555 << 1), 0xAAAA);
    415  1.1   gdamore 	bus_space_write_2(iot, ioh, (0x2AAA << 1), 0x5555);
    416  1.1   gdamore 
    417  1.1   gdamore 	bus_space_write_2(iot, ioh, offset, 0x3030);
    418  1.1   gdamore 
    419  1.1   gdamore 	/*
    420  1.1   gdamore 	 * NB: with CFI, we could get more meaningful timeout data for
    421  1.1   gdamore 	 * now we just assign reasonable values - 10 msec typical, and
    422  1.1   gdamore 	 * up to 60 secs to erase the whole sector.
    423  1.1   gdamore 	 */
    424  1.1   gdamore 
    425  1.1   gdamore 	return toggle_bit_wait(sc, offset, 10000, 60000000, 0);
    426  1.1   gdamore }
    427  1.1   gdamore 
    428  1.1   gdamore static int
    429  1.1   gdamore flash_sector_write(struct flash_softc *sc, bus_size_t offset)
    430  1.1   gdamore {
    431  1.1   gdamore 	bus_space_tag_t		iot = sc->sc_iot;
    432  1.1   gdamore 	bus_space_handle_t	ioh = sc->sc_ioh;
    433  1.1   gdamore 	bus_size_t		fence;
    434  1.1   gdamore 	const u_int16_t		*p;
    435  1.1   gdamore 
    436  1.1   gdamore 	p = (u_int16_t *) sc->sc_buf;
    437  1.1   gdamore 	fence = offset + sc->sc_sector_size;
    438  1.1   gdamore 	do {
    439  1.1   gdamore 		bus_space_write_2(iot, ioh, (0x5555 << 1), 0xAAAA);
    440  1.1   gdamore 		bus_space_write_2(iot, ioh, (0x2AAA << 1), 0x5555);
    441  1.1   gdamore 		bus_space_write_2(iot, ioh, (0xAAAA << 1), 0xA0A0);
    442  1.1   gdamore 
    443  1.1   gdamore 		bus_space_write_2(iot, ioh, offset, *p);
    444  1.1   gdamore 
    445  1.1   gdamore 		/* wait up to 1 msec, in 10 usec increments, no sleeping */
    446  1.1   gdamore 		if (toggle_bit_wait(sc, offset, 10, 1000, 1) != 0)
    447  1.1   gdamore 			return ETIMEDOUT;
    448  1.1   gdamore 		p++;
    449  1.1   gdamore 		offset += 2;
    450  1.1   gdamore 	} while (offset < fence);
    451  1.1   gdamore 
    452  1.1   gdamore 	return 0;
    453  1.1   gdamore }
    454