Home | History | Annotate | Line # | Download | only in spi
spiflash.c revision 1.6
      1 /* $NetBSD: spiflash.c,v 1.6 2007/07/29 12:15:44 ad 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 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: spiflash.c,v 1.6 2007/07/29 12:15:44 ad Exp $");
     46 
     47 #include <sys/param.h>
     48 #include <sys/conf.h>
     49 #include <sys/proc.h>
     50 #include <sys/systm.h>
     51 #include <sys/device.h>
     52 #include <sys/kernel.h>
     53 #include <sys/file.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/disk.h>
     56 #include <sys/disklabel.h>
     57 #include <sys/buf.h>
     58 #include <sys/bufq.h>
     59 #include <sys/uio.h>
     60 #include <sys/kthread.h>
     61 #include <sys/malloc.h>
     62 #include <sys/errno.h>
     63 
     64 #include <dev/spi/spivar.h>
     65 #include <dev/spi/spiflash.h>
     66 
     67 /*
     68  * This is an MI block driver for SPI flash devices.  It could probably be
     69  * converted to some more generic framework, if someone wanted to create one
     70  * for NOR flashes.  Note that some flashes have the ability to handle
     71  * interrupts.
     72  */
     73 
     74 struct spiflash_softc {
     75 	struct device		sc_dev;
     76 	struct disk		sc_dk;
     77 
     78 	struct spiflash_hw_if	sc_hw;
     79 	void			*sc_cookie;
     80 
     81 	const char		*sc_name;
     82 	struct spi_handle	*sc_handle;
     83 	int			sc_device_size;
     84 	int			sc_write_size;
     85 	int			sc_erase_size;
     86 	int			sc_read_size;
     87 	int			sc_device_blks;
     88 
     89 	struct bufq_state	*sc_waitq;
     90 	struct bufq_state	*sc_workq;
     91 	struct bufq_state	*sc_doneq;
     92 	lwp_t			*sc_thread;
     93 };
     94 
     95 #define	sc_getname	sc_hw.sf_getname
     96 #define	sc_gethandle	sc_hw.sf_gethandle
     97 #define	sc_getsize	sc_hw.sf_getsize
     98 #define	sc_getflags	sc_hw.sf_getflags
     99 #define	sc_erase	sc_hw.sf_erase
    100 #define	sc_write	sc_hw.sf_write
    101 #define	sc_read		sc_hw.sf_read
    102 #define	sc_getstatus	sc_hw.sf_getstatus
    103 #define	sc_setstatus	sc_hw.sf_setstatus
    104 
    105 struct spiflash_attach_args {
    106 	const struct spiflash_hw_if	*hw;
    107 	void				*cookie;
    108 };
    109 
    110 #define	STATIC
    111 STATIC int spiflash_match(struct device *, struct cfdata *, void *);
    112 STATIC void spiflash_attach(struct device *, struct device *, void *);
    113 STATIC int spiflash_print(void *, const char *);
    114 STATIC int spiflash_common_erase(spiflash_handle_t, size_t, size_t);
    115 STATIC int spiflash_common_write(spiflash_handle_t, size_t, size_t,
    116     const uint8_t *);
    117 STATIC int spiflash_common_read(spiflash_handle_t, size_t, size_t, uint8_t *);
    118 STATIC void spiflash_process_done(spiflash_handle_t, int);
    119 STATIC void spiflash_process_read(spiflash_handle_t);
    120 STATIC void spiflash_process_write(spiflash_handle_t);
    121 STATIC void spiflash_thread(void *);
    122 STATIC int spiflash_nsectors(spiflash_handle_t, struct buf *);
    123 STATIC int spiflash_nsectors(spiflash_handle_t, struct buf *);
    124 STATIC int spiflash_sector(spiflash_handle_t, struct buf *);
    125 
    126 CFATTACH_DECL(spiflash, sizeof(struct spiflash_softc),
    127 	      spiflash_match, spiflash_attach, NULL, NULL);
    128 
    129 #ifdef	SPIFLASH_DEBUG
    130 #define	DPRINTF(x)	do { printf x; } while (0/*CONSTCOND*/)
    131 #else
    132 #define	DPRINTF(x)	do {  } while (0/*CONSTCOND*/)
    133 #endif
    134 
    135 extern struct cfdriver spiflash_cd;
    136 
    137 dev_type_open(spiflash_open);
    138 dev_type_close(spiflash_close);
    139 dev_type_read(spiflash_read);
    140 dev_type_write(spiflash_write);
    141 dev_type_ioctl(spiflash_ioctl);
    142 dev_type_strategy(spiflash_strategy);
    143 
    144 const struct bdevsw spiflash_bdevsw = {
    145 	.d_open = spiflash_open,
    146 	.d_close = spiflash_close,
    147 	.d_strategy = spiflash_strategy,
    148 	.d_ioctl = spiflash_ioctl,
    149 	.d_dump = nodump,
    150 	.d_psize = nosize,
    151 	.d_flag = D_DISK,
    152 };
    153 
    154 const struct cdevsw spiflash_cdevsw = {
    155 	.d_open = spiflash_open,
    156 	.d_close = spiflash_close,
    157 	.d_read = spiflash_read,
    158 	.d_write = spiflash_write,
    159 	.d_ioctl = spiflash_ioctl,
    160 	.d_stop = nostop,
    161 	.d_tty = notty,
    162 	.d_poll = nopoll,
    163 	.d_mmap = nommap,
    164 	.d_kqfilter = nokqfilter,
    165 	.d_flag = D_DISK,
    166 };
    167 
    168 static struct dkdriver spiflash_dkdriver = { spiflash_strategy, NULL };
    169 
    170 spiflash_handle_t
    171 spiflash_attach_mi(const struct spiflash_hw_if *hw, void *cookie,
    172     struct device *dev)
    173 {
    174 	struct spiflash_attach_args sfa;
    175 	sfa.hw = hw;
    176 	sfa.cookie = cookie;
    177 
    178 	return (spiflash_handle_t)config_found(dev, &sfa, spiflash_print);
    179 }
    180 
    181 int
    182 spiflash_print(void *aux, const char *pnp)
    183 {
    184 	if (pnp != NULL)
    185 		printf("spiflash at %s\n", pnp);
    186 
    187 	return UNCONF;
    188 }
    189 
    190 int
    191 spiflash_match(struct device *parent, struct cfdata *cf, void *aux)
    192 {
    193 
    194 	return 1;
    195 }
    196 
    197 void
    198 spiflash_attach(struct device *parent, struct device *self, void *aux)
    199 {
    200 	struct spiflash_softc *sc = device_private(self);
    201 	struct spiflash_attach_args *sfa = aux;
    202 	void *cookie = sfa->cookie;
    203 
    204 	sc->sc_hw = *sfa->hw;
    205 	sc->sc_cookie = cookie;
    206 	sc->sc_name = sc->sc_getname(cookie);
    207 	sc->sc_handle = sc->sc_gethandle(cookie);
    208 	sc->sc_device_size = sc->sc_getsize(cookie, SPIFLASH_SIZE_DEVICE);
    209 	sc->sc_erase_size = sc->sc_getsize(cookie, SPIFLASH_SIZE_ERASE);
    210 	sc->sc_write_size = sc->sc_getsize(cookie, SPIFLASH_SIZE_WRITE);
    211 	sc->sc_read_size = sc->sc_getsize(cookie, SPIFLASH_SIZE_READ);
    212 	sc->sc_device_blks = sc->sc_device_size / DEV_BSIZE;
    213 
    214 	if (sc->sc_read == NULL)
    215 		sc->sc_read = spiflash_common_read;
    216 	if (sc->sc_write == NULL)
    217 		sc->sc_write = spiflash_common_write;
    218 	if (sc->sc_erase == NULL)
    219 		sc->sc_erase = spiflash_common_erase;
    220 
    221 	aprint_naive(": SPI flash\n");
    222 	aprint_normal(": %s SPI flash\n", sc->sc_name);
    223 	/* XXX: note that this has to change for boot-sectored flash */
    224 	aprint_normal("%s: %d KB, %d sectors of %d KB each\n",
    225 	    sc->sc_dev.dv_xname, sc->sc_device_size / 1024,
    226 	    sc->sc_device_size / sc->sc_erase_size,
    227 	    sc->sc_erase_size / 1024);
    228 
    229 	/* first-come first-served strategy works best for us */
    230 	bufq_alloc(&sc->sc_waitq, "fcfs", BUFQ_SORT_RAWBLOCK);
    231 	bufq_alloc(&sc->sc_workq, "fcfs", BUFQ_SORT_RAWBLOCK);
    232 	bufq_alloc(&sc->sc_doneq, "fcfs", BUFQ_SORT_RAWBLOCK);
    233 
    234 	sc->sc_dk.dk_driver = &spiflash_dkdriver;
    235 	sc->sc_dk.dk_name = sc->sc_dev.dv_xname;
    236 
    237 	disk_attach(&sc->sc_dk);
    238 
    239 	/* arrange to allocate the kthread */
    240 	kthread_create(PRI_NONE, 0, NULL, spiflash_thread, sc,
    241 	    &sc->sc_thread, "spiflash");
    242 }
    243 
    244 int
    245 spiflash_open(dev_t dev, int flags, int mode, struct lwp *l)
    246 {
    247 	spiflash_handle_t sc;
    248 
    249 	if ((sc = device_lookup(&spiflash_cd, DISKUNIT(dev))) == NULL)
    250 		return ENXIO;
    251 
    252 	/*
    253 	 * XXX: We need to handle partitions here.  The problem is
    254 	 * that it isn't entirely clear to me how to deal with this.
    255 	 * There are devices that could be used "in the raw" with a
    256 	 * NetBSD label, but then you get into devices that have other
    257 	 * kinds of data on them -- some have VxWorks data, some have
    258 	 * RedBoot data, and some have other contraints -- for example
    259 	 * some devices might have a portion that is read-only,
    260 	 * whereas others might have a portion that is read-write.
    261 	 *
    262 	 * For now we just permit access to the entire device.
    263 	 */
    264 	return 0;
    265 }
    266 
    267 int
    268 spiflash_close(dev_t dev, int flags, int mode, struct lwp *l)
    269 {
    270 	spiflash_handle_t sc;
    271 
    272 	if ((sc = device_lookup(&spiflash_cd, DISKUNIT(dev))) == NULL)
    273 		return ENXIO;
    274 
    275 	return 0;
    276 }
    277 
    278 int
    279 spiflash_read(dev_t dev, struct uio *uio, int ioflag)
    280 {
    281 
    282 	return physio(spiflash_strategy, NULL, dev, B_READ, minphys, uio);
    283 }
    284 
    285 int
    286 spiflash_write(dev_t dev, struct uio *uio, int ioflag)
    287 {
    288 
    289 	return physio(spiflash_strategy, NULL, dev, B_WRITE, minphys, uio);
    290 }
    291 
    292 int
    293 spiflash_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    294 {
    295 	spiflash_handle_t sc;
    296 
    297 	if ((sc = device_lookup(&spiflash_cd, DISKUNIT(dev))) == NULL)
    298 		return ENXIO;
    299 
    300 	return EINVAL;
    301 }
    302 
    303 void
    304 spiflash_strategy(struct buf *bp)
    305 {
    306 	spiflash_handle_t sc;
    307 	int	s;
    308 
    309 	sc = device_lookup(&spiflash_cd, DISKUNIT(bp->b_dev));
    310 	if (sc == NULL) {
    311 		bp->b_error = ENXIO;
    312 		biodone(bp);
    313 		return;
    314 	}
    315 
    316 	if (((bp->b_bcount % sc->sc_write_size) != 0) ||
    317 	    (bp->b_blkno < 0)) {
    318 		bp->b_error = EINVAL;
    319 		biodone(bp);
    320 		return;
    321 	}
    322 
    323 	/* no work? */
    324 	if (bp->b_bcount == 0) {
    325 		biodone(bp);
    326 		return;
    327 	}
    328 
    329 	if (bounds_check_with_mediasize(bp, DEV_BSIZE,
    330 		sc->sc_device_blks) <= 0) {
    331 		biodone(bp);
    332 		return;
    333 	}
    334 
    335 	bp->b_resid = bp->b_bcount;
    336 
    337 	/* all ready, hand off to thread for async processing */
    338 	s = splbio();
    339 	BUFQ_PUT(sc->sc_waitq, bp);
    340 	wakeup(&sc->sc_thread);
    341 	splx(s);
    342 }
    343 
    344 void
    345 spiflash_process_done(spiflash_handle_t sc, int err)
    346 {
    347 	struct buf	*bp;
    348 	int		cnt = 0;
    349 	int		flag = 0;
    350 
    351 	while ((bp = BUFQ_GET(sc->sc_doneq)) != NULL) {
    352 		flag = bp->b_flags & B_READ;
    353 		if ((bp->b_error = err) == 0)
    354 			bp->b_resid = 0;
    355 		cnt += bp->b_bcount - bp->b_resid;
    356 		biodone(bp);
    357 	}
    358 	disk_unbusy(&sc->sc_dk, cnt, flag);
    359 }
    360 
    361 void
    362 spiflash_process_read(spiflash_handle_t sc)
    363 {
    364 	struct buf	*bp;
    365 	int		err = 0;
    366 
    367 	disk_busy(&sc->sc_dk);
    368 	while ((bp = BUFQ_GET(sc->sc_workq)) != NULL) {
    369 		size_t addr = bp->b_blkno * DEV_BSIZE;
    370 		uint8_t *data = bp->b_data;
    371 		int cnt = bp->b_resid;
    372 
    373 		BUFQ_PUT(sc->sc_doneq, bp);
    374 
    375 		DPRINTF(("read from addr %x, cnt %d\n", (unsigned)addr, cnt));
    376 
    377 		if ((err = sc->sc_read(sc, addr, cnt, data)) != 0) {
    378 			/* error occurred, fail all pending workq bufs */
    379 			bufq_move(sc->sc_doneq, sc->sc_workq);
    380 			break;
    381 		}
    382 
    383 		bp->b_resid -= cnt;
    384 		data += cnt;
    385 		addr += cnt;
    386 	}
    387 	spiflash_process_done(sc, err);
    388 }
    389 
    390 void
    391 spiflash_process_write(spiflash_handle_t sc)
    392 {
    393 	int	len;
    394 	size_t	base;
    395 	daddr_t	blkno;
    396 	uint8_t	*save;
    397 	int	err = 0, neederase = 0;
    398 	struct buf *bp;
    399 
    400 	/*
    401 	 * due to other considerations, we are guaranteed that
    402 	 * we will only have multiple buffers if they are all in
    403 	 * the same erase sector.  Therefore we never need to look
    404 	 * beyond the first block to determine how much data we need
    405 	 * to save.
    406 	 */
    407 
    408 	bp = BUFQ_PEEK(sc->sc_workq);
    409 	len = spiflash_nsectors(sc, bp)  * sc->sc_erase_size;
    410 	blkno = bp->b_blkno;
    411 	base = (blkno * DEV_BSIZE) & ~ (sc->sc_erase_size - 1);
    412 
    413 	/* get ourself a scratch buffer */
    414 	save = malloc(len, M_DEVBUF, M_WAITOK);
    415 
    416 	disk_busy(&sc->sc_dk);
    417 	/* read in as much of the data as we need */
    418 	DPRINTF(("reading in %d bytes\n", len));
    419 	if ((err = sc->sc_read(sc, base, len, save)) != 0) {
    420 		bufq_move(sc->sc_doneq, sc->sc_workq);
    421 		spiflash_process_done(sc, err);
    422 		return;
    423 	}
    424 
    425 	/*
    426 	 * now coalesce the writes into the save area, but also
    427 	 * check to see if we need to do an erase
    428 	 */
    429 	while ((bp = BUFQ_GET(sc->sc_workq)) != NULL) {
    430 		uint8_t	*data, *dst;
    431 		int resid = bp->b_resid;
    432 
    433 		DPRINTF(("coalesce write, blkno %x, count %d, resid %d\n",
    434 			    (unsigned)bp->b_blkno, bp->b_bcount, resid));
    435 
    436 		data = bp->b_data;
    437 		dst = save + (bp->b_blkno - blkno) * DEV_BSIZE;
    438 
    439 		/*
    440 		 * NOR flash bits.  We can clear a bit, but we cannot
    441 		 * set a bit, without erasing.  This should help reduce
    442 		 * unnecessary erases.
    443 		 */
    444 		while (resid) {
    445 			if ((*data) & ~(*dst))
    446 				neederase = 1;
    447 			*dst++ = *data++;
    448 			resid--;
    449 		}
    450 
    451 		BUFQ_PUT(sc->sc_doneq, bp);
    452 	}
    453 
    454 	/*
    455 	 * do the erase, if we need to.
    456 	 */
    457 	if (neederase) {
    458 		DPRINTF(("erasing from %x - %x\n", base, base + len));
    459 		if ((err = sc->sc_erase(sc, base, len)) != 0) {
    460 			spiflash_process_done(sc, err);
    461 			return;
    462 		}
    463 	}
    464 
    465 	/*
    466 	 * now write our save area, and finish up.
    467 	 */
    468 	DPRINTF(("flashing %d bytes to %x from %x\n", len,
    469 		    base, (unsigned)save));
    470 	err = sc->sc_write(sc, base, len, save);
    471 	spiflash_process_done(sc, err);
    472 }
    473 
    474 
    475 int
    476 spiflash_nsectors(spiflash_handle_t sc, struct buf *bp)
    477 {
    478 	unsigned	addr, sector;
    479 
    480 	addr = bp->b_blkno * DEV_BSIZE;
    481 	sector = addr / sc->sc_erase_size;
    482 
    483 	addr += bp->b_bcount;
    484 	addr--;
    485 	return (((addr / sc->sc_erase_size)  - sector) + 1);
    486 }
    487 
    488 int
    489 spiflash_sector(spiflash_handle_t sc, struct buf *bp)
    490 {
    491 	unsigned	addr, sector;
    492 
    493 	addr = bp->b_blkno * DEV_BSIZE;
    494 	sector = addr / sc->sc_erase_size;
    495 
    496 	/* if it spans multiple blocks, error it */
    497 	addr += bp->b_bcount;
    498 	addr--;
    499 	if (sector != (addr / sc->sc_erase_size))
    500 		return -1;
    501 
    502 	return sector;
    503 }
    504 
    505 void
    506 spiflash_thread(void *arg)
    507 {
    508 	spiflash_handle_t sc = arg;
    509 	struct buf	*bp;
    510 	int		s;
    511 	int		sector;
    512 
    513 	s = splbio();
    514 	for (;;) {
    515 		if ((bp = BUFQ_GET(sc->sc_waitq)) == NULL) {
    516 			tsleep(&sc->sc_thread, PRIBIO, "spiflash_thread", 0);
    517 			continue;
    518 		}
    519 
    520 		BUFQ_PUT(sc->sc_workq, bp);
    521 
    522 		if (bp->b_flags & B_READ) {
    523 			/* just do the read */
    524 			spiflash_process_read(sc);
    525 			continue;
    526 		}
    527 
    528 		/*
    529 		 * Because writing a flash filesystem is particularly
    530 		 * painful, involving erase, modify, write, we prefer
    531 		 * to coalesce writes to the same sector together.
    532 		 */
    533 
    534 		sector = spiflash_sector(sc, bp);
    535 
    536 		/*
    537 		 * if the write spans multiple sectors, skip
    538 		 * coalescing.  (It would be nice if we could break
    539 		 * these up.  minphys is honored for read/write, but
    540 		 * not necessarily for bread.)
    541 		 */
    542 		if (sector < 0)
    543 			goto dowrite;
    544 
    545 		while ((bp = BUFQ_PEEK(sc->sc_waitq)) != NULL) {
    546 			/* can't deal with read requests! */
    547 			if (bp->b_flags & B_READ)
    548 				break;
    549 
    550 			/* is it for the same sector? */
    551 			if (spiflash_sector(sc, bp) != sector)
    552 				break;
    553 
    554 			bp = BUFQ_GET(sc->sc_waitq);
    555 			BUFQ_PUT(sc->sc_workq, bp);
    556 		}
    557 
    558 	dowrite:
    559 		spiflash_process_write(sc);
    560 	}
    561 }
    562 /*
    563  * SPI flash common implementation.
    564  */
    565 
    566 /*
    567  * Most devices take on the order of 1 second for each block that they
    568  * delete.
    569  */
    570 int
    571 spiflash_common_erase(spiflash_handle_t sc, size_t start, size_t size)
    572 {
    573 	int		rv;
    574 
    575 	if ((start % sc->sc_erase_size) || (size % sc->sc_erase_size))
    576 		return EINVAL;
    577 
    578 	/* the second test is to test against wrap */
    579 	if ((start > sc->sc_device_size) ||
    580 	    ((start + size) > sc->sc_device_size))
    581 		return EINVAL;
    582 
    583 	/*
    584 	 * XXX: check protection status?  Requires master table mapping
    585 	 * sectors to status bits, and so forth.
    586 	 */
    587 
    588 	while (size) {
    589 		if ((rv = spiflash_write_enable(sc)) != 0) {
    590 			spiflash_write_disable(sc);
    591 			return rv;
    592 		}
    593 		if ((rv = spiflash_cmd(sc, SPIFLASH_CMD_ERASE, 3, start, 0,
    594 			 NULL, NULL)) != 0) {
    595 			spiflash_write_disable(sc);
    596 			return rv;
    597 		}
    598 
    599 		/*
    600 		 * The devices I have all say typical for sector erase
    601 		 * is ~1sec.  We check ten times that often.  (There
    602 		 * is no way to interrupt on this.)
    603 		 */
    604 		if ((rv = spiflash_wait(sc, hz / 10)) != 0)
    605 			return rv;
    606 
    607 		start += sc->sc_erase_size;
    608 		size -= sc->sc_erase_size;
    609 
    610 		/* NB: according to the docs I have, the write enable
    611 		 * is automatically cleared upon completion of an erase
    612 		 * command, so there is no need to explicitly disable it.
    613 		 */
    614 	}
    615 
    616 	return 0;
    617 }
    618 
    619 int
    620 spiflash_common_write(spiflash_handle_t sc, size_t start, size_t size,
    621     const uint8_t *data)
    622 {
    623 	int		rv;
    624 
    625 	if ((start % sc->sc_write_size) || (size % sc->sc_write_size))
    626 		return EINVAL;
    627 
    628 	while (size) {
    629 		int cnt;
    630 
    631 		if ((rv = spiflash_write_enable(sc)) != 0) {
    632 			spiflash_write_disable(sc);
    633 			return rv;
    634 		}
    635 
    636 		cnt = min(size, sc->sc_write_size);
    637 		if ((rv = spiflash_cmd(sc, SPIFLASH_CMD_PROGRAM, 3, start,
    638 			 cnt, data, NULL)) != 0) {
    639 			spiflash_write_disable(sc);
    640 			return rv;
    641 		}
    642 
    643 		/*
    644 		 * It seems that most devices can write bits fairly
    645 		 * quickly.  For example, one part I have access to
    646 		 * takes ~5msec to process the entire 256 byte page.
    647 		 * Probably this should be modified to cope with
    648 		 * device-specific timing, and maybe also take into
    649 		 * account systems with higher values of HZ (which
    650 		 * could benefit from sleeping.)
    651 		 */
    652 		if ((rv = spiflash_wait(sc, 0)) != 0)
    653 			return rv;
    654 
    655 		data += cnt;
    656 		start += cnt;
    657 		size -= cnt;
    658 	}
    659 
    660 	return 0;
    661 }
    662 
    663 int
    664 spiflash_common_read(spiflash_handle_t sc, size_t start, size_t size,
    665     uint8_t *data)
    666 {
    667 	int		rv;
    668 
    669 	while (size) {
    670 		int cnt;
    671 
    672 		if (sc->sc_read_size > 0)
    673 			cnt = min(size, sc->sc_read_size);
    674 		else
    675 			cnt = size;
    676 
    677 		if ((rv = spiflash_cmd(sc, SPIFLASH_CMD_READ, 3, start,
    678 			 cnt, NULL, data)) != 0) {
    679 			return rv;
    680 		}
    681 
    682 		start += cnt;
    683 		size -= cnt;
    684 	}
    685 
    686 	return 0;
    687 }
    688 
    689 /* read status register */
    690 int
    691 spiflash_read_status(spiflash_handle_t sc, uint8_t *sr)
    692 {
    693 
    694 	return spiflash_cmd(sc, SPIFLASH_CMD_RDSR, 0, 0, 1, NULL, sr);
    695 }
    696 
    697 int
    698 spiflash_write_enable(spiflash_handle_t sc)
    699 {
    700 
    701 	return spiflash_cmd(sc, SPIFLASH_CMD_WREN, 0, 0, 0, NULL, NULL);
    702 }
    703 
    704 int
    705 spiflash_write_disable(spiflash_handle_t sc)
    706 {
    707 
    708 	return spiflash_cmd(sc, SPIFLASH_CMD_WRDI, 0, 0, 0, NULL, NULL);
    709 }
    710 
    711 int
    712 spiflash_cmd(spiflash_handle_t sc, uint8_t cmd,
    713     size_t addrlen, uint32_t addr,
    714     size_t cnt, const uint8_t *wdata, uint8_t *rdata)
    715 {
    716 	struct spi_transfer	trans;
    717 	struct spi_chunk	chunk1, chunk2;
    718 	char buf[4];
    719 	int i;
    720 
    721 	buf[0] = cmd;
    722 
    723 	if (addrlen > 3)
    724 		return EINVAL;
    725 
    726 	for (i = addrlen; i > 0; i--) {
    727 		buf[i] = addr & 0xff;
    728 		addr >>= 8;
    729 	}
    730 	spi_transfer_init(&trans);
    731 	spi_chunk_init(&chunk1, addrlen + 1, buf, NULL);
    732 	spi_transfer_add(&trans, &chunk1);
    733 	if (cnt) {
    734 		spi_chunk_init(&chunk2, cnt, wdata, rdata);
    735 		spi_transfer_add(&trans, &chunk2);
    736 	}
    737 
    738 	spi_transfer(sc->sc_handle, &trans);
    739 	spi_wait(&trans);
    740 
    741 	if (trans.st_flags & SPI_F_ERROR)
    742 		return trans.st_errno;
    743 	return 0;
    744 }
    745 
    746 int
    747 spiflash_wait(spiflash_handle_t sc, int tmo)
    748 {
    749 	int	rv;
    750 	uint8_t	sr;
    751 
    752 	for (;;) {
    753 		if ((rv = spiflash_read_status(sc, &sr)) != 0)
    754 			return rv;
    755 
    756 		if ((sr & SPIFLASH_SR_BUSY) == 0)
    757 			break;
    758 		/*
    759 		 * The devices I have all say typical for sector
    760 		 * erase is ~1sec.  We check time times that often.
    761 		 * (There is no way to interrupt on this.)
    762 		 */
    763 		if (tmo)
    764 			tsleep(&sr, PWAIT, "spiflash_wait", tmo);
    765 	}
    766 	return 0;
    767 }
    768