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