xref: /netbsd-src/sys/arch/mips/atheros/dev/athflash.c (revision 9e26d3f35bec42a735e4f9727e8d4fd54e4716f5)
1*9e26d3f3Sthorpej /* $NetBSD: athflash.c,v 1.12 2021/01/04 17:42:29 thorpej Exp $ */
260a298a6Sgdamore 
360a298a6Sgdamore /*
460a298a6Sgdamore  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
560a298a6Sgdamore  * Copyright (c) 2006 Garrett D'Amore.
660a298a6Sgdamore  * All rights reserved.
760a298a6Sgdamore  *
860a298a6Sgdamore  * Portions of this code were written by Garrett D'Amore for the
960a298a6Sgdamore  * Champaign-Urbana Community Wireless Network Project.
1060a298a6Sgdamore  *
1160a298a6Sgdamore  * Redistribution and use in source and binary forms, with or
1260a298a6Sgdamore  * without modification, are permitted provided that the following
1360a298a6Sgdamore  * conditions are met:
1460a298a6Sgdamore  * 1. Redistributions of source code must retain the above copyright
1560a298a6Sgdamore  *    notice, this list of conditions and the following disclaimer.
1660a298a6Sgdamore  * 2. Redistributions in binary form must reproduce the above
1760a298a6Sgdamore  *    copyright notice, this list of conditions and the following
1860a298a6Sgdamore  *    disclaimer in the documentation and/or other materials provided
1960a298a6Sgdamore  *    with the distribution.
2060a298a6Sgdamore  * 3. All advertising materials mentioning features or use of this
2160a298a6Sgdamore  *    software must display the following acknowledgements:
2260a298a6Sgdamore  *      This product includes software developed by the Urbana-Champaign
2360a298a6Sgdamore  *      Independent Media Center.
2460a298a6Sgdamore  *	This product includes software developed by Garrett D'Amore.
2560a298a6Sgdamore  * 4. Urbana-Champaign Independent Media Center's name and Garrett
2660a298a6Sgdamore  *    D'Amore's name may not be used to endorse or promote products
2760a298a6Sgdamore  *    derived from this software without specific prior written permission.
2860a298a6Sgdamore  *
2960a298a6Sgdamore  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
3060a298a6Sgdamore  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
3160a298a6Sgdamore  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
3260a298a6Sgdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3360a298a6Sgdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
3460a298a6Sgdamore  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
3560a298a6Sgdamore  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
3660a298a6Sgdamore  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
3760a298a6Sgdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
3860a298a6Sgdamore  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3960a298a6Sgdamore  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4060a298a6Sgdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
4160a298a6Sgdamore  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4260a298a6Sgdamore  */
4360a298a6Sgdamore /*
4460a298a6Sgdamore  * Copyright (c) 2002 The NetBSD Foundation, Inc.
4560a298a6Sgdamore  * All rights reserved.
4660a298a6Sgdamore  *
4760a298a6Sgdamore  * This code is derived from software contributed to The NetBSD Foundation
4860a298a6Sgdamore  * by Naoto Shimazaki of YOKOGAWA Electric Corporation.
4960a298a6Sgdamore  *
5060a298a6Sgdamore  * Redistribution and use in source and binary forms, with or without
5160a298a6Sgdamore  * modification, are permitted provided that the following conditions
5260a298a6Sgdamore  * are met:
5360a298a6Sgdamore  * 1. Redistributions of source code must retain the above copyright
5460a298a6Sgdamore  *    notice, this list of conditions and the following disclaimer.
5560a298a6Sgdamore  * 2. Redistributions in binary form must reproduce the above copyright
5660a298a6Sgdamore  *    notice, this list of conditions and the following disclaimer in the
5760a298a6Sgdamore  *    documentation and/or other materials provided with the distribution.
5860a298a6Sgdamore  *
5960a298a6Sgdamore  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
6060a298a6Sgdamore  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
6160a298a6Sgdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
6260a298a6Sgdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
6360a298a6Sgdamore  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
6460a298a6Sgdamore  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
6560a298a6Sgdamore  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
6660a298a6Sgdamore  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
6760a298a6Sgdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6860a298a6Sgdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6960a298a6Sgdamore  * POSSIBILITY OF SUCH DAMAGE.
7060a298a6Sgdamore  */
7160a298a6Sgdamore 
7260a298a6Sgdamore /*
7360a298a6Sgdamore  * Flash Memory Driver
7460a298a6Sgdamore  *
7560a298a6Sgdamore  * XXX This primitive flash driver does *not* support boot sectored devices,
7660a298a6Sgdamore  * XXX and only supports a fairly limited set of devices, that we are likely to
7760a298a6Sgdamore  * XXX to find in an AP30.
7860a298a6Sgdamore  * XXX
7960a298a6Sgdamore  * XXX We also are only supporting flash widths of 16 _for the moment_, and
8060a298a6Sgdamore  * XXX we are only supporting flash devices that use the AMD command sets.
8160a298a6Sgdamore  * XXX All this should be reviewed and improved to be much more generic.
8260a298a6Sgdamore  */
8360a298a6Sgdamore 
8460a298a6Sgdamore #include <sys/cdefs.h>
85*9e26d3f3Sthorpej __KERNEL_RCSID(0, "$NetBSD: athflash.c,v 1.12 2021/01/04 17:42:29 thorpej Exp $");
8660a298a6Sgdamore 
8760a298a6Sgdamore #include <sys/param.h>
8860a298a6Sgdamore #include <sys/conf.h>
8960a298a6Sgdamore #include <sys/device.h>
9060a298a6Sgdamore #include <sys/kernel.h>
91*9e26d3f3Sthorpej #include <sys/kmem.h>
9260a298a6Sgdamore #include <sys/proc.h>
9360a298a6Sgdamore #include <sys/systm.h>
9460a298a6Sgdamore 
95e265f67bSdyoung #include <sys/bus.h>
9660a298a6Sgdamore 
9760a298a6Sgdamore #include <mips/atheros/include/arbusvar.h>
9860a298a6Sgdamore 
9960a298a6Sgdamore #ifdef FLASH_DEBUG
10060a298a6Sgdamore int	flash_debug = 0;
10160a298a6Sgdamore #define DPRINTF(x)	if (flash_debug) printf x
10260a298a6Sgdamore #else
10360a298a6Sgdamore #define DPRINTF(x)
10460a298a6Sgdamore #endif
10560a298a6Sgdamore 
10660a298a6Sgdamore struct flash_softc {
10760a298a6Sgdamore 	bus_space_tag_t		sc_iot;
10860a298a6Sgdamore 	bus_space_handle_t	sc_ioh;
10960a298a6Sgdamore 	size_t			sc_size;
11060a298a6Sgdamore 	size_t			sc_sector_size;
11160a298a6Sgdamore 	int			sc_status;
11260a298a6Sgdamore 	u_int8_t		*sc_buf;
11360a298a6Sgdamore };
11460a298a6Sgdamore 
11560a298a6Sgdamore #define	FLASH_ST_BUSY	0x1
11660a298a6Sgdamore 
117cbab9cadSchs static int flash_probe(device_t, cfdata_t, void *);
118cbab9cadSchs static void flash_attach(device_t, device_t, void *);
11960a298a6Sgdamore 
12060a298a6Sgdamore static int is_block_same(struct flash_softc *, bus_size_t, const void *);
12160a298a6Sgdamore static int toggle_bit_wait(struct flash_softc *, bus_size_t, int, int, int);
12260a298a6Sgdamore 
12360a298a6Sgdamore static int flash_sector_erase(struct flash_softc *, bus_size_t);
12460a298a6Sgdamore static int flash_sector_write(struct flash_softc *, bus_size_t);
12560a298a6Sgdamore 
12660a298a6Sgdamore extern struct cfdriver athflash_cd;
12760a298a6Sgdamore 
128cbab9cadSchs CFATTACH_DECL_NEW(athflash, sizeof(struct flash_softc),
12960a298a6Sgdamore 	      flash_probe, flash_attach, NULL, NULL);
13060a298a6Sgdamore 
13160a298a6Sgdamore dev_type_open(flashopen);
13260a298a6Sgdamore dev_type_close(flashclose);
13360a298a6Sgdamore dev_type_read(flashread);
13460a298a6Sgdamore dev_type_write(flashwrite);
13560a298a6Sgdamore 
13660a298a6Sgdamore const struct cdevsw athflash_cdevsw = {
137a68f9396Sdholland 	.d_open = flashopen,
138a68f9396Sdholland 	.d_close = flashclose,
139a68f9396Sdholland 	.d_read = flashread,
140a68f9396Sdholland 	.d_write = flashwrite,
141a68f9396Sdholland 	.d_ioctl = noioctl,
142a68f9396Sdholland 	.d_stop = nostop,
143a68f9396Sdholland 	.d_tty = notty,
144a68f9396Sdholland 	.d_poll = nopoll,
145a68f9396Sdholland 	.d_mmap = nommap,
146a68f9396Sdholland 	.d_kqfilter = nokqfilter,
147f9228f42Sdholland 	.d_discard = nodiscard,
148a68f9396Sdholland 	.d_flag = 0
14960a298a6Sgdamore };
15060a298a6Sgdamore 
15160a298a6Sgdamore static struct {
15260a298a6Sgdamore 	uint16_t		vendor_id;
15360a298a6Sgdamore 	uint16_t		device_id;
15460a298a6Sgdamore 	const char		*name;
15560a298a6Sgdamore 	int			sector_size;
15660a298a6Sgdamore 	int			flash_size;
15760a298a6Sgdamore } flash_ids[] = {
15860a298a6Sgdamore 	{ 0x00bf, 0x2780, "SST 39VF400", 0x01000, 0x080000 },	/* 512KB */
15960a298a6Sgdamore 	{ 0x00bf, 0x2782, "SST 39VF160", 0x01000, 0x200000 },	/* 2MB */
16060a298a6Sgdamore 	{ 0xffff, 0xffff, NULL, 0, 0 }				/* end list */
16160a298a6Sgdamore };
16260a298a6Sgdamore 
16360a298a6Sgdamore static int
flash_probe(device_t parent,cfdata_t cf,void * aux)164cbab9cadSchs flash_probe(device_t parent, cfdata_t cf, void *aux)
16560a298a6Sgdamore {
16660a298a6Sgdamore 	struct arbus_attach_args	*aa = aux;
16760a298a6Sgdamore 	bus_space_handle_t		ioh;
16860a298a6Sgdamore 	int				rv = 0, i;
16960a298a6Sgdamore 	uint16_t			venid, devid;
17060a298a6Sgdamore 
17160a298a6Sgdamore 	if (strcmp(aa->aa_name, cf->cf_name) != 0)
17260a298a6Sgdamore 		return 0;
17360a298a6Sgdamore 
17460a298a6Sgdamore 	DPRINTF(("trying to map address %x\n", (unsigned)aa->aa_addr));
17560a298a6Sgdamore 	if (bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 0, &ioh))
17660a298a6Sgdamore 		return 0;
17760a298a6Sgdamore 
17860a298a6Sgdamore 	/* issue JEDEC query */
17960a298a6Sgdamore 	DPRINTF(("issuing JEDEC query\n"));
18060a298a6Sgdamore 	bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0xAAAA);
18160a298a6Sgdamore 	bus_space_write_2(aa->aa_bst, ioh, (0x2AAA << 1), 0x5555);
18260a298a6Sgdamore 	bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0x9090);
18360a298a6Sgdamore 
18460a298a6Sgdamore 	delay(100);
18560a298a6Sgdamore 	venid = bus_space_read_2(aa->aa_bst, ioh, 0);
18660a298a6Sgdamore 	devid = bus_space_read_2(aa->aa_bst, ioh, 2);
18760a298a6Sgdamore 
18860a298a6Sgdamore 	/* issue software exit */
18960a298a6Sgdamore 	bus_space_write_2(aa->aa_bst, ioh, 0x0, 0xF0F0);
19060a298a6Sgdamore 
19160a298a6Sgdamore 	for (i = 0; flash_ids[i].name != NULL; i++) {
19260a298a6Sgdamore 		if ((venid == flash_ids[i].vendor_id) &&
19360a298a6Sgdamore 		    (devid == flash_ids[i].device_id)) {
19460a298a6Sgdamore 			rv = 1;
19560a298a6Sgdamore 			break;
19660a298a6Sgdamore 		}
19760a298a6Sgdamore 	}
19860a298a6Sgdamore 
19960a298a6Sgdamore 	bus_space_unmap(aa->aa_bst, ioh, aa->aa_size);
20060a298a6Sgdamore 	return rv;
20160a298a6Sgdamore }
20260a298a6Sgdamore 
20360a298a6Sgdamore static void
flash_attach(device_t parent,device_t self,void * aux)204cbab9cadSchs flash_attach(device_t parent, device_t self, void *aux)
20560a298a6Sgdamore {
206bf9e3945Sdyoung 	char nbuf[32];
207cbab9cadSchs 	struct flash_softc		*sc = device_private(self);
20860a298a6Sgdamore 	struct arbus_attach_args	*aa = aux;
20960a298a6Sgdamore 	int				i;
21060a298a6Sgdamore 	bus_space_tag_t			iot = aa->aa_bst;
21160a298a6Sgdamore 	bus_space_handle_t		ioh;
21260a298a6Sgdamore 	uint16_t			venid, devid;
21360a298a6Sgdamore 
21460a298a6Sgdamore 	if (bus_space_map(iot, aa->aa_addr, aa->aa_size, 0, &ioh)) {
21560a298a6Sgdamore 		printf(": can't map i/o space\n");
21660a298a6Sgdamore                 return;
21760a298a6Sgdamore   	}
21860a298a6Sgdamore 
21960a298a6Sgdamore 	sc->sc_iot = iot;
22060a298a6Sgdamore 	sc->sc_ioh = ioh;
22160a298a6Sgdamore 	sc->sc_status = 0;
22260a298a6Sgdamore 
22360a298a6Sgdamore 	/* issue JEDEC query */
22460a298a6Sgdamore 	bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0xAAAA);
22560a298a6Sgdamore 	bus_space_write_2(aa->aa_bst, ioh, (0x2AAA << 1), 0x5555);
22660a298a6Sgdamore 	bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0x9090);
22760a298a6Sgdamore 
22860a298a6Sgdamore 	delay(100);
22960a298a6Sgdamore 	venid = bus_space_read_2(aa->aa_bst, ioh, 0);
23060a298a6Sgdamore 	devid = bus_space_read_2(aa->aa_bst, ioh, 2);
23160a298a6Sgdamore 
23260a298a6Sgdamore 	/* issue software exit */
23360a298a6Sgdamore 	bus_space_write_2(aa->aa_bst, ioh, 0x0, 0xF0F0);
23460a298a6Sgdamore 
23560a298a6Sgdamore 	for (i = 0; flash_ids[i].name != NULL; i++) {
23660a298a6Sgdamore 		if ((venid == flash_ids[i].vendor_id) &&
23760a298a6Sgdamore 		    (devid == flash_ids[i].device_id)) {
23860a298a6Sgdamore 			break;
23960a298a6Sgdamore 		}
24060a298a6Sgdamore 	}
24160a298a6Sgdamore 
24260a298a6Sgdamore 	KASSERT(flash_ids[i].name != NULL);
24360a298a6Sgdamore 	printf(": %s", flash_ids[i].name);
244bf9e3945Sdyoung 	if (humanize_number(nbuf, sizeof(nbuf), flash_ids[i].flash_size, "B",
245bf9e3945Sdyoung 	    1024) > 0)
246bf9e3945Sdyoung 		printf(" (%s)", nbuf);
24760a298a6Sgdamore 
24860a298a6Sgdamore 	/*
24960a298a6Sgdamore 	 * determine size of the largest block
25060a298a6Sgdamore 	 */
25160a298a6Sgdamore 	sc->sc_size = flash_ids[i].flash_size;
25260a298a6Sgdamore 	sc->sc_sector_size = flash_ids[i].sector_size;
25360a298a6Sgdamore 
254*9e26d3f3Sthorpej 	sc->sc_buf = kmem_alloc(sc->sc_sector_size, KM_SLEEP);
25560a298a6Sgdamore 
25660a298a6Sgdamore 	printf("\n");
25760a298a6Sgdamore }
25860a298a6Sgdamore 
25960a298a6Sgdamore int
flashopen(dev_t dev,int flag,int mode,struct lwp * l)26060a298a6Sgdamore flashopen(dev_t dev, int flag, int mode, struct lwp *l)
26160a298a6Sgdamore {
26260a298a6Sgdamore 	struct flash_softc	*sc;
26360a298a6Sgdamore 
2643d8fe45fScegger 	sc = device_lookup_private(&athflash_cd, minor(dev));
2653d8fe45fScegger 	if (sc == NULL)
26660a298a6Sgdamore 		return ENXIO;
26760a298a6Sgdamore 	if (sc->sc_status & FLASH_ST_BUSY)
26860a298a6Sgdamore 		return EBUSY;
26960a298a6Sgdamore 	sc->sc_status |= FLASH_ST_BUSY;
27060a298a6Sgdamore 	return 0;
27160a298a6Sgdamore }
27260a298a6Sgdamore 
27360a298a6Sgdamore int
flashclose(dev_t dev,int flag,int mode,struct lwp * l)27460a298a6Sgdamore flashclose(dev_t dev, int flag, int mode, struct lwp *l)
27560a298a6Sgdamore {
27660a298a6Sgdamore 	struct flash_softc	*sc;
27760a298a6Sgdamore 
2783d8fe45fScegger 	sc = device_lookup_private(&athflash_cd, minor(dev));
27960a298a6Sgdamore 	sc->sc_status &= ~FLASH_ST_BUSY;
28060a298a6Sgdamore 	return 0;
28160a298a6Sgdamore }
28260a298a6Sgdamore 
28360a298a6Sgdamore int
flashread(dev_t dev,struct uio * uio,int flag)28460a298a6Sgdamore flashread(dev_t dev, struct uio *uio, int flag)
28560a298a6Sgdamore {
28660a298a6Sgdamore 	struct flash_softc	*sc;
28760a298a6Sgdamore 	bus_space_tag_t		iot;
28860a298a6Sgdamore 	bus_space_handle_t	ioh;
28960a298a6Sgdamore 	bus_size_t		off;
29060a298a6Sgdamore 	int			total;
29160a298a6Sgdamore 	int			count;
29260a298a6Sgdamore 	int			error;
29360a298a6Sgdamore 
2943d8fe45fScegger 	sc = device_lookup_private(&athflash_cd, minor(dev));
29560a298a6Sgdamore 	iot = sc->sc_iot;
29660a298a6Sgdamore 	ioh = sc->sc_ioh;
29760a298a6Sgdamore 
29860a298a6Sgdamore 	off = uio->uio_offset;
299d1579b2dSriastradh 	total = uimin(sc->sc_size - off, uio->uio_resid);
30060a298a6Sgdamore 
30160a298a6Sgdamore 	while (total > 0) {
302d1579b2dSriastradh 		count = uimin(sc->sc_sector_size, uio->uio_resid);
30360a298a6Sgdamore 		bus_space_read_region_1(iot, ioh, off, sc->sc_buf, count);
30460a298a6Sgdamore 		if ((error = uiomove(sc->sc_buf, count, uio)) != 0)
30560a298a6Sgdamore 			return error;
30660a298a6Sgdamore 		off += count;
30760a298a6Sgdamore 		total -= count;
30860a298a6Sgdamore 	}
30960a298a6Sgdamore 	return 0;
31060a298a6Sgdamore }
31160a298a6Sgdamore 
31260a298a6Sgdamore 
31360a298a6Sgdamore int
flashwrite(dev_t dev,struct uio * uio,int flag)31460a298a6Sgdamore flashwrite(dev_t dev, struct uio *uio, int flag)
31560a298a6Sgdamore {
31660a298a6Sgdamore 	struct flash_softc	*sc;
31760a298a6Sgdamore 	bus_size_t		off;
31860a298a6Sgdamore 	int			stat;
31960a298a6Sgdamore 	int			error;
32060a298a6Sgdamore 
3213d8fe45fScegger 	sc = device_lookup_private(&athflash_cd, minor(dev));
32260a298a6Sgdamore 
32360a298a6Sgdamore 	if (sc->sc_size < uio->uio_offset + uio->uio_resid)
32460a298a6Sgdamore 		return ENOSPC;
32560a298a6Sgdamore 	if (uio->uio_offset % sc->sc_sector_size)
32660a298a6Sgdamore 		return EINVAL;
32760a298a6Sgdamore 	if (uio->uio_resid % sc->sc_sector_size)
32860a298a6Sgdamore 		return EINVAL;
32960a298a6Sgdamore 
33060a298a6Sgdamore 	for (off = uio->uio_offset;
33160a298a6Sgdamore 	     uio->uio_resid > 0;
33260a298a6Sgdamore 	     off += sc->sc_sector_size) {
33360a298a6Sgdamore 		error = uiomove(sc->sc_buf, sc->sc_sector_size, uio);
33460a298a6Sgdamore 		if (error != 0)
33560a298a6Sgdamore 			return error;
33660a298a6Sgdamore 		if (is_block_same(sc, off, sc->sc_buf))
33760a298a6Sgdamore 			continue;
33860a298a6Sgdamore 		if ((stat = flash_sector_erase(sc, off)) != 0) {
33960a298a6Sgdamore 			printf("sector erase failed status = 0x%x\n", stat);
34060a298a6Sgdamore 			return EIO;
34160a298a6Sgdamore 		}
34260a298a6Sgdamore 		if ((stat = flash_sector_write(sc, off)) != 0) {
34360a298a6Sgdamore 			printf("sector write failed status = 0x%x\n", stat);
34460a298a6Sgdamore 			return EIO;
34560a298a6Sgdamore 		}
34660a298a6Sgdamore 	}
34760a298a6Sgdamore 	return 0;
34860a298a6Sgdamore }
34960a298a6Sgdamore 
35060a298a6Sgdamore static int
is_block_same(struct flash_softc * sc,bus_size_t offset,const void * bufp)35160a298a6Sgdamore is_block_same(struct flash_softc *sc, bus_size_t offset, const void *bufp)
35260a298a6Sgdamore {
35360a298a6Sgdamore 	bus_space_tag_t		iot = sc->sc_iot;
35460a298a6Sgdamore 	bus_space_handle_t	ioh = sc->sc_ioh;
35560a298a6Sgdamore 	const u_int8_t		*p = bufp;
35660a298a6Sgdamore 	int			count = sc->sc_sector_size;
35760a298a6Sgdamore 
35860a298a6Sgdamore 	while (count-- > 0) {
35960a298a6Sgdamore 		if (bus_space_read_1(iot, ioh, offset++) != *p++)
36060a298a6Sgdamore 			return 0;
36160a298a6Sgdamore 	}
36260a298a6Sgdamore 	return 1;
36360a298a6Sgdamore }
36460a298a6Sgdamore 
36560a298a6Sgdamore static int
toggle_bit_wait(struct flash_softc * sc,bus_size_t offset,int typtmo,int maxtmo,int spin)36660a298a6Sgdamore toggle_bit_wait(struct flash_softc *sc, bus_size_t offset,
36760a298a6Sgdamore     int typtmo, int maxtmo, int spin)
36860a298a6Sgdamore {
36960a298a6Sgdamore 	bus_space_tag_t		iot = sc->sc_iot;
37060a298a6Sgdamore 	bus_space_handle_t	ioh = sc->sc_ioh;
37160a298a6Sgdamore 	uint8_t			d1, d2;
37260a298a6Sgdamore 
37360a298a6Sgdamore 	while (maxtmo > 0) {
37460a298a6Sgdamore 
37560a298a6Sgdamore 		if (spin) {
37660a298a6Sgdamore 			DELAY(typtmo);
37760a298a6Sgdamore 		} else {
37860a298a6Sgdamore 			tsleep(sc, PRIBIO, "blockerase",
37960a298a6Sgdamore 			    (typtmo / hz) + 1);
38060a298a6Sgdamore 		}
38160a298a6Sgdamore 
38260a298a6Sgdamore 		d1 = bus_space_read_1(iot, ioh, offset);
38360a298a6Sgdamore 		d2 = bus_space_read_2(iot, ioh, offset);
38460a298a6Sgdamore 
38560a298a6Sgdamore 		/* watch for the toggle bit to stop toggling */
38660a298a6Sgdamore 		if ((d1 & 0x40) == (d2 & 0x40)) {
38760a298a6Sgdamore 			return 0;
38860a298a6Sgdamore 		}
38960a298a6Sgdamore 
39060a298a6Sgdamore 		maxtmo -= typtmo;
39160a298a6Sgdamore 	}
39260a298a6Sgdamore 	return (ETIMEDOUT);
39360a298a6Sgdamore }
39460a298a6Sgdamore 
39560a298a6Sgdamore static int
flash_sector_erase(struct flash_softc * sc,bus_size_t offset)39660a298a6Sgdamore flash_sector_erase(struct flash_softc *sc, bus_size_t offset)
39760a298a6Sgdamore {
39860a298a6Sgdamore 	bus_space_tag_t		iot = sc->sc_iot;
39960a298a6Sgdamore 	bus_space_handle_t	ioh = sc->sc_ioh;
40060a298a6Sgdamore 
40160a298a6Sgdamore 	DPRINTF(("flash_sector_erase offset = %08lx\n", offset));
40260a298a6Sgdamore 
40360a298a6Sgdamore 	bus_space_write_2(iot, ioh, (0x5555 << 1), 0xAAAA);
40460a298a6Sgdamore 	bus_space_write_2(iot, ioh, (0x2AAA << 1), 0x5555);
40560a298a6Sgdamore 	bus_space_write_2(iot, ioh, (0x5555 << 1), 0x8080);
40660a298a6Sgdamore 	bus_space_write_2(iot, ioh, (0x5555 << 1), 0xAAAA);
40760a298a6Sgdamore 	bus_space_write_2(iot, ioh, (0x2AAA << 1), 0x5555);
40860a298a6Sgdamore 
40960a298a6Sgdamore 	bus_space_write_2(iot, ioh, offset, 0x3030);
41060a298a6Sgdamore 
41160a298a6Sgdamore 	/*
41260a298a6Sgdamore 	 * NB: with CFI, we could get more meaningful timeout data for
41360a298a6Sgdamore 	 * now we just assign reasonable values - 10 msec typical, and
41460a298a6Sgdamore 	 * up to 60 secs to erase the whole sector.
41560a298a6Sgdamore 	 */
41660a298a6Sgdamore 
41760a298a6Sgdamore 	return toggle_bit_wait(sc, offset, 10000, 60000000, 0);
41860a298a6Sgdamore }
41960a298a6Sgdamore 
42060a298a6Sgdamore static int
flash_sector_write(struct flash_softc * sc,bus_size_t offset)42160a298a6Sgdamore flash_sector_write(struct flash_softc *sc, bus_size_t offset)
42260a298a6Sgdamore {
42360a298a6Sgdamore 	bus_space_tag_t		iot = sc->sc_iot;
42460a298a6Sgdamore 	bus_space_handle_t	ioh = sc->sc_ioh;
42560a298a6Sgdamore 	bus_size_t		fence;
42660a298a6Sgdamore 	const u_int16_t		*p;
42760a298a6Sgdamore 
42860a298a6Sgdamore 	p = (u_int16_t *) sc->sc_buf;
42960a298a6Sgdamore 	fence = offset + sc->sc_sector_size;
43060a298a6Sgdamore 	do {
43160a298a6Sgdamore 		bus_space_write_2(iot, ioh, (0x5555 << 1), 0xAAAA);
43260a298a6Sgdamore 		bus_space_write_2(iot, ioh, (0x2AAA << 1), 0x5555);
43360a298a6Sgdamore 		bus_space_write_2(iot, ioh, (0xAAAA << 1), 0xA0A0);
43460a298a6Sgdamore 
43560a298a6Sgdamore 		bus_space_write_2(iot, ioh, offset, *p);
43660a298a6Sgdamore 
43760a298a6Sgdamore 		/* wait up to 1 msec, in 10 usec increments, no sleeping */
43860a298a6Sgdamore 		if (toggle_bit_wait(sc, offset, 10, 1000, 1) != 0)
43960a298a6Sgdamore 			return ETIMEDOUT;
44060a298a6Sgdamore 		p++;
44160a298a6Sgdamore 		offset += 2;
44260a298a6Sgdamore 	} while (offset < fence);
44360a298a6Sgdamore 
44460a298a6Sgdamore 	return 0;
44560a298a6Sgdamore }
446