1 /* $NetBSD: spiflash.c,v 1.9 2008/06/11 19:31:10 cegger Exp $ */
4 * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
5 * Copyright (c) 2006 Garrett D'Amore.
8 * Portions of this code were written by Garrett D'Amore for the
9 * Champaign-Urbana Community Wireless Network Project.
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
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.
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.
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: spiflash.c,v 1.9 2008/06/11 19:31:10 cegger Exp $");
47 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/device.h>
52 #include <sys/kernel.h>
54 #include <sys/ioctl.h>
56 #include <sys/disklabel.h>
60 #include <sys/kthread.h>
61 #include <sys/malloc.h>
62 #include <sys/errno.h>
64 #include <dev/spi/spivar.h>
65 #include <dev/spi/spiflash.h>
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
74 struct spiflash_softc
{
77 struct spiflash_hw_if sc_hw
;
81 struct spi_handle
*sc_handle
;
88 struct bufq_state
*sc_waitq
;
89 struct bufq_state
*sc_workq
;
90 struct bufq_state
*sc_doneq
;
94 #define sc_getname sc_hw.sf_getname
95 #define sc_gethandle sc_hw.sf_gethandle
96 #define sc_getsize sc_hw.sf_getsize
97 #define sc_getflags sc_hw.sf_getflags
98 #define sc_erase sc_hw.sf_erase
99 #define sc_write sc_hw.sf_write
100 #define sc_read sc_hw.sf_read
101 #define sc_getstatus sc_hw.sf_getstatus
102 #define sc_setstatus sc_hw.sf_setstatus
104 struct spiflash_attach_args
{
105 const struct spiflash_hw_if
*hw
;
110 STATIC
int spiflash_match(device_t
, cfdata_t
, void *);
111 STATIC
void spiflash_attach(device_t
, device_t
, void *);
112 STATIC
int spiflash_print(void *, const char *);
113 STATIC
int spiflash_common_erase(spiflash_handle_t
, size_t, size_t);
114 STATIC
int spiflash_common_write(spiflash_handle_t
, size_t, size_t,
116 STATIC
int spiflash_common_read(spiflash_handle_t
, size_t, size_t, uint8_t *);
117 STATIC
void spiflash_process_done(spiflash_handle_t
, int);
118 STATIC
void spiflash_process_read(spiflash_handle_t
);
119 STATIC
void spiflash_process_write(spiflash_handle_t
);
120 STATIC
void spiflash_thread(void *);
121 STATIC
int spiflash_nsectors(spiflash_handle_t
, struct buf
*);
122 STATIC
int spiflash_nsectors(spiflash_handle_t
, struct buf
*);
123 STATIC
int spiflash_sector(spiflash_handle_t
, struct buf
*);
125 CFATTACH_DECL_NEW(spiflash
, sizeof(struct spiflash_softc
),
126 spiflash_match
, spiflash_attach
, NULL
, NULL
);
128 #ifdef SPIFLASH_DEBUG
129 #define DPRINTF(x) do { printf x; } while (0/*CONSTCOND*/)
131 #define DPRINTF(x) do { } while (0/*CONSTCOND*/)
134 extern struct cfdriver spiflash_cd
;
136 dev_type_open(spiflash_open
);
137 dev_type_close(spiflash_close
);
138 dev_type_read(spiflash_read
);
139 dev_type_write(spiflash_write
);
140 dev_type_ioctl(spiflash_ioctl
);
141 dev_type_strategy(spiflash_strategy
);
143 const struct bdevsw spiflash_bdevsw
= {
144 .d_open
= spiflash_open
,
145 .d_close
= spiflash_close
,
146 .d_strategy
= spiflash_strategy
,
147 .d_ioctl
= spiflash_ioctl
,
153 const struct cdevsw spiflash_cdevsw
= {
154 .d_open
= spiflash_open
,
155 .d_close
= spiflash_close
,
156 .d_read
= spiflash_read
,
157 .d_write
= spiflash_write
,
158 .d_ioctl
= spiflash_ioctl
,
163 .d_kqfilter
= nokqfilter
,
167 static struct dkdriver spiflash_dkdriver
= { spiflash_strategy
, NULL
};
170 spiflash_attach_mi(const struct spiflash_hw_if
*hw
, void *cookie
,
173 struct spiflash_attach_args sfa
;
177 return (spiflash_handle_t
)config_found(dev
, &sfa
, spiflash_print
);
181 spiflash_print(void *aux
, const char *pnp
)
184 printf("spiflash at %s\n", pnp
);
190 spiflash_match(device_t parent
, cfdata_t cf
, void *aux
)
197 spiflash_attach(device_t parent
, device_t self
, void *aux
)
199 struct spiflash_softc
*sc
= device_private(self
);
200 struct spiflash_attach_args
*sfa
= aux
;
201 void *cookie
= sfa
->cookie
;
203 sc
->sc_hw
= *sfa
->hw
;
204 sc
->sc_cookie
= cookie
;
205 sc
->sc_name
= sc
->sc_getname(cookie
);
206 sc
->sc_handle
= sc
->sc_gethandle(cookie
);
207 sc
->sc_device_size
= sc
->sc_getsize(cookie
, SPIFLASH_SIZE_DEVICE
);
208 sc
->sc_erase_size
= sc
->sc_getsize(cookie
, SPIFLASH_SIZE_ERASE
);
209 sc
->sc_write_size
= sc
->sc_getsize(cookie
, SPIFLASH_SIZE_WRITE
);
210 sc
->sc_read_size
= sc
->sc_getsize(cookie
, SPIFLASH_SIZE_READ
);
211 sc
->sc_device_blks
= sc
->sc_device_size
/ DEV_BSIZE
;
213 if (sc
->sc_read
== NULL
)
214 sc
->sc_read
= spiflash_common_read
;
215 if (sc
->sc_write
== NULL
)
216 sc
->sc_write
= spiflash_common_write
;
217 if (sc
->sc_erase
== NULL
)
218 sc
->sc_erase
= spiflash_common_erase
;
220 aprint_naive(": SPI flash\n");
221 aprint_normal(": %s SPI flash\n", sc
->sc_name
);
222 /* XXX: note that this has to change for boot-sectored flash */
223 aprint_normal_dev(self
, "%d KB, %d sectors of %d KB each\n",
224 sc
->sc_device_size
/ 1024,
225 sc
->sc_device_size
/ sc
->sc_erase_size
,
226 sc
->sc_erase_size
/ 1024);
228 /* first-come first-served strategy works best for us */
229 bufq_alloc(&sc
->sc_waitq
, "fcfs", BUFQ_SORT_RAWBLOCK
);
230 bufq_alloc(&sc
->sc_workq
, "fcfs", BUFQ_SORT_RAWBLOCK
);
231 bufq_alloc(&sc
->sc_doneq
, "fcfs", BUFQ_SORT_RAWBLOCK
);
233 sc
->sc_dk
.dk_driver
= &spiflash_dkdriver
;
234 sc
->sc_dk
.dk_name
= device_xname(self
);
236 disk_attach(&sc
->sc_dk
);
238 /* arrange to allocate the kthread */
239 kthread_create(PRI_NONE
, 0, NULL
, spiflash_thread
, sc
,
240 &sc
->sc_thread
, "spiflash");
244 spiflash_open(dev_t dev
, int flags
, int mode
, struct lwp
*l
)
246 spiflash_handle_t sc
;
248 sc
= device_lookup_private(&spiflash_cd
, DISKUNIT(dev
));
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.
262 * For now we just permit access to the entire device.
268 spiflash_close(dev_t dev
, int flags
, int mode
, struct lwp
*l
)
270 spiflash_handle_t sc
;
272 sc
= device_lookup_private(&spiflash_cd
, DISKUNIT(dev
));
280 spiflash_read(dev_t dev
, struct uio
*uio
, int ioflag
)
283 return physio(spiflash_strategy
, NULL
, dev
, B_READ
, minphys
, uio
);
287 spiflash_write(dev_t dev
, struct uio
*uio
, int ioflag
)
290 return physio(spiflash_strategy
, NULL
, dev
, B_WRITE
, minphys
, uio
);
294 spiflash_ioctl(dev_t dev
, u_long cmd
, void *data
, int flags
, struct lwp
*l
)
296 spiflash_handle_t sc
;
298 sc
= device_lookup_private(&spiflash_cd
, DISKUNIT(dev
));
306 spiflash_strategy(struct buf
*bp
)
308 spiflash_handle_t sc
;
311 sc
= device_lookup_private(&spiflash_cd
, DISKUNIT(bp
->b_dev
));
318 if (((bp
->b_bcount
% sc
->sc_write_size
) != 0) ||
320 bp
->b_error
= EINVAL
;
326 if (bp
->b_bcount
== 0) {
331 if (bounds_check_with_mediasize(bp
, DEV_BSIZE
,
332 sc
->sc_device_blks
) <= 0) {
337 bp
->b_resid
= bp
->b_bcount
;
339 /* all ready, hand off to thread for async processing */
341 bufq_put(sc
->sc_waitq
, bp
);
342 wakeup(&sc
->sc_thread
);
347 spiflash_process_done(spiflash_handle_t sc
, int err
)
353 while ((bp
= bufq_get(sc
->sc_doneq
)) != NULL
) {
354 flag
= bp
->b_flags
& B_READ
;
355 if ((bp
->b_error
= err
) == 0)
357 cnt
+= bp
->b_bcount
- bp
->b_resid
;
360 disk_unbusy(&sc
->sc_dk
, cnt
, flag
);
364 spiflash_process_read(spiflash_handle_t sc
)
369 disk_busy(&sc
->sc_dk
);
370 while ((bp
= bufq_get(sc
->sc_workq
)) != NULL
) {
371 size_t addr
= bp
->b_blkno
* DEV_BSIZE
;
372 uint8_t *data
= bp
->b_data
;
373 int cnt
= bp
->b_resid
;
375 bufq_put(sc
->sc_doneq
, bp
);
377 DPRINTF(("read from addr %x, cnt %d\n", (unsigned)addr
, cnt
));
379 if ((err
= sc
->sc_read(sc
, addr
, cnt
, data
)) != 0) {
380 /* error occurred, fail all pending workq bufs */
381 bufq_move(sc
->sc_doneq
, sc
->sc_workq
);
389 spiflash_process_done(sc
, err
);
393 spiflash_process_write(spiflash_handle_t sc
)
399 int err
= 0, neederase
= 0;
403 * due to other considerations, we are guaranteed that
404 * we will only have multiple buffers if they are all in
405 * the same erase sector. Therefore we never need to look
406 * beyond the first block to determine how much data we need
410 bp
= bufq_peek(sc
->sc_workq
);
411 len
= spiflash_nsectors(sc
, bp
) * sc
->sc_erase_size
;
413 base
= (blkno
* DEV_BSIZE
) & ~ (sc
->sc_erase_size
- 1);
415 /* get ourself a scratch buffer */
416 save
= malloc(len
, M_DEVBUF
, M_WAITOK
);
418 disk_busy(&sc
->sc_dk
);
419 /* read in as much of the data as we need */
420 DPRINTF(("reading in %d bytes\n", len
));
421 if ((err
= sc
->sc_read(sc
, base
, len
, save
)) != 0) {
422 bufq_move(sc
->sc_doneq
, sc
->sc_workq
);
423 spiflash_process_done(sc
, err
);
428 * now coalesce the writes into the save area, but also
429 * check to see if we need to do an erase
431 while ((bp
= bufq_get(sc
->sc_workq
)) != NULL
) {
433 int resid
= bp
->b_resid
;
435 DPRINTF(("coalesce write, blkno %x, count %d, resid %d\n",
436 (unsigned)bp
->b_blkno
, bp
->b_bcount
, resid
));
439 dst
= save
+ (bp
->b_blkno
- blkno
) * DEV_BSIZE
;
442 * NOR flash bits. We can clear a bit, but we cannot
443 * set a bit, without erasing. This should help reduce
444 * unnecessary erases.
447 if ((*data
) & ~(*dst
))
453 bufq_put(sc
->sc_doneq
, bp
);
457 * do the erase, if we need to.
460 DPRINTF(("erasing from %x - %x\n", base
, base
+ len
));
461 if ((err
= sc
->sc_erase(sc
, base
, len
)) != 0) {
462 spiflash_process_done(sc
, err
);
468 * now write our save area, and finish up.
470 DPRINTF(("flashing %d bytes to %x from %x\n", len
,
471 base
, (unsigned)save
));
472 err
= sc
->sc_write(sc
, base
, len
, save
);
473 spiflash_process_done(sc
, err
);
478 spiflash_nsectors(spiflash_handle_t sc
, struct buf
*bp
)
480 unsigned addr
, sector
;
482 addr
= bp
->b_blkno
* DEV_BSIZE
;
483 sector
= addr
/ sc
->sc_erase_size
;
485 addr
+= bp
->b_bcount
;
487 return (((addr
/ sc
->sc_erase_size
) - sector
) + 1);
491 spiflash_sector(spiflash_handle_t sc
, struct buf
*bp
)
493 unsigned addr
, sector
;
495 addr
= bp
->b_blkno
* DEV_BSIZE
;
496 sector
= addr
/ sc
->sc_erase_size
;
498 /* if it spans multiple blocks, error it */
499 addr
+= bp
->b_bcount
;
501 if (sector
!= (addr
/ sc
->sc_erase_size
))
508 spiflash_thread(void *arg
)
510 spiflash_handle_t sc
= arg
;
517 if ((bp
= bufq_get(sc
->sc_waitq
)) == NULL
) {
518 tsleep(&sc
->sc_thread
, PRIBIO
, "spiflash_thread", 0);
522 bufq_put(sc
->sc_workq
, bp
);
524 if (bp
->b_flags
& B_READ
) {
525 /* just do the read */
526 spiflash_process_read(sc
);
531 * Because writing a flash filesystem is particularly
532 * painful, involving erase, modify, write, we prefer
533 * to coalesce writes to the same sector together.
536 sector
= spiflash_sector(sc
, bp
);
539 * if the write spans multiple sectors, skip
540 * coalescing. (It would be nice if we could break
541 * these up. minphys is honored for read/write, but
542 * not necessarily for bread.)
547 while ((bp
= bufq_peek(sc
->sc_waitq
)) != NULL
) {
548 /* can't deal with read requests! */
549 if (bp
->b_flags
& B_READ
)
552 /* is it for the same sector? */
553 if (spiflash_sector(sc
, bp
) != sector
)
556 bp
= bufq_get(sc
->sc_waitq
);
557 bufq_put(sc
->sc_workq
, bp
);
561 spiflash_process_write(sc
);
565 * SPI flash common implementation.
569 * Most devices take on the order of 1 second for each block that they
573 spiflash_common_erase(spiflash_handle_t sc
, size_t start
, size_t size
)
577 if ((start
% sc
->sc_erase_size
) || (size
% sc
->sc_erase_size
))
580 /* the second test is to test against wrap */
581 if ((start
> sc
->sc_device_size
) ||
582 ((start
+ size
) > sc
->sc_device_size
))
586 * XXX: check protection status? Requires master table mapping
587 * sectors to status bits, and so forth.
591 if ((rv
= spiflash_write_enable(sc
)) != 0) {
592 spiflash_write_disable(sc
);
595 if ((rv
= spiflash_cmd(sc
, SPIFLASH_CMD_ERASE
, 3, start
, 0,
597 spiflash_write_disable(sc
);
602 * The devices I have all say typical for sector erase
603 * is ~1sec. We check ten times that often. (There
604 * is no way to interrupt on this.)
606 if ((rv
= spiflash_wait(sc
, hz
/ 10)) != 0)
609 start
+= sc
->sc_erase_size
;
610 size
-= sc
->sc_erase_size
;
612 /* NB: according to the docs I have, the write enable
613 * is automatically cleared upon completion of an erase
614 * command, so there is no need to explicitly disable it.
622 spiflash_common_write(spiflash_handle_t sc
, size_t start
, size_t size
,
627 if ((start
% sc
->sc_write_size
) || (size
% sc
->sc_write_size
))
633 if ((rv
= spiflash_write_enable(sc
)) != 0) {
634 spiflash_write_disable(sc
);
638 cnt
= min(size
, sc
->sc_write_size
);
639 if ((rv
= spiflash_cmd(sc
, SPIFLASH_CMD_PROGRAM
, 3, start
,
640 cnt
, data
, NULL
)) != 0) {
641 spiflash_write_disable(sc
);
646 * It seems that most devices can write bits fairly
647 * quickly. For example, one part I have access to
648 * takes ~5msec to process the entire 256 byte page.
649 * Probably this should be modified to cope with
650 * device-specific timing, and maybe also take into
651 * account systems with higher values of HZ (which
652 * could benefit from sleeping.)
654 if ((rv
= spiflash_wait(sc
, 0)) != 0)
666 spiflash_common_read(spiflash_handle_t sc
, size_t start
, size_t size
,
674 if (sc
->sc_read_size
> 0)
675 cnt
= min(size
, sc
->sc_read_size
);
679 if ((rv
= spiflash_cmd(sc
, SPIFLASH_CMD_READ
, 3, start
,
680 cnt
, NULL
, data
)) != 0) {
691 /* read status register */
693 spiflash_read_status(spiflash_handle_t sc
, uint8_t *sr
)
696 return spiflash_cmd(sc
, SPIFLASH_CMD_RDSR
, 0, 0, 1, NULL
, sr
);
700 spiflash_write_enable(spiflash_handle_t sc
)
703 return spiflash_cmd(sc
, SPIFLASH_CMD_WREN
, 0, 0, 0, NULL
, NULL
);
707 spiflash_write_disable(spiflash_handle_t sc
)
710 return spiflash_cmd(sc
, SPIFLASH_CMD_WRDI
, 0, 0, 0, NULL
, NULL
);
714 spiflash_cmd(spiflash_handle_t sc
, uint8_t cmd
,
715 size_t addrlen
, uint32_t addr
,
716 size_t cnt
, const uint8_t *wdata
, uint8_t *rdata
)
718 struct spi_transfer trans
;
719 struct spi_chunk chunk1
, chunk2
;
728 for (i
= addrlen
; i
> 0; i
--) {
729 buf
[i
] = addr
& 0xff;
732 spi_transfer_init(&trans
);
733 spi_chunk_init(&chunk1
, addrlen
+ 1, buf
, NULL
);
734 spi_transfer_add(&trans
, &chunk1
);
736 spi_chunk_init(&chunk2
, cnt
, wdata
, rdata
);
737 spi_transfer_add(&trans
, &chunk2
);
740 spi_transfer(sc
->sc_handle
, &trans
);
743 if (trans
.st_flags
& SPI_F_ERROR
)
744 return trans
.st_errno
;
749 spiflash_wait(spiflash_handle_t sc
, int tmo
)
755 if ((rv
= spiflash_read_status(sc
, &sr
)) != 0)
758 if ((sr
& SPIFLASH_SR_BUSY
) == 0)
761 * The devices I have all say typical for sector
762 * erase is ~1sec. We check time times that often.
763 * (There is no way to interrupt on this.)
766 tsleep(&sr
, PWAIT
, "spiflash_wait", tmo
);