xref: /freebsd/sys/dev/flash/mx25l.c (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
1 /*-
2  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
3  * Copyright (c) 2009 Oleksandr Tymoshenko.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include "opt_platform.h"
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bio.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
38 #include <sys/lock.h>
39 #include <sys/mbuf.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <geom/geom_disk.h>
44 
45 #ifdef FDT
46 #include <dev/fdt/fdt_common.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 #include <dev/ofw/openfirm.h>
49 #endif
50 
51 #include <dev/spibus/spi.h>
52 #include "spibus_if.h"
53 
54 #include <dev/flash/mx25lreg.h>
55 
56 #define	FL_NONE			0x00
57 #define	FL_ERASE_4K		0x01
58 #define	FL_ERASE_32K		0x02
59 #define	FL_ENABLE_4B_ADDR	0x04
60 #define	FL_DISABLE_4B_ADDR	0x08
61 
62 /*
63  * Define the sectorsize to be a smaller size rather than the flash
64  * sector size. Trying to run FFS off of a 64k flash sector size
65  * results in a completely un-usable system.
66  */
67 #define	MX25L_SECTORSIZE	512
68 
69 struct mx25l_flash_ident
70 {
71 	const char	*name;
72 	uint8_t		manufacturer_id;
73 	uint16_t	device_id;
74 	unsigned int	sectorsize;
75 	unsigned int	sectorcount;
76 	unsigned int	flags;
77 };
78 
79 struct mx25l_softc
80 {
81 	device_t	sc_dev;
82 	uint8_t		sc_manufacturer_id;
83 	uint16_t	sc_device_id;
84 	unsigned int	sc_sectorsize;
85 	struct mtx	sc_mtx;
86 	struct disk	*sc_disk;
87 	struct proc	*sc_p;
88 	struct bio_queue_head sc_bio_queue;
89 	unsigned int	sc_flags;
90 };
91 
92 #define M25PXX_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
93 #define	M25PXX_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
94 #define M25PXX_LOCK_INIT(_sc) \
95 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
96 	    "mx25l", MTX_DEF)
97 #define M25PXX_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
98 #define M25PXX_ASSERT_LOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_OWNED);
99 #define M25PXX_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
100 
101 /* disk routines */
102 static int mx25l_open(struct disk *dp);
103 static int mx25l_close(struct disk *dp);
104 static int mx25l_ioctl(struct disk *, u_long, void *, int, struct thread *);
105 static void mx25l_strategy(struct bio *bp);
106 static int mx25l_getattr(struct bio *bp);
107 static void mx25l_task(void *arg);
108 
109 struct mx25l_flash_ident flash_devices[] = {
110 	{ "en25f32",	0x1c, 0x3116, 64 * 1024, 64, FL_NONE },
111 	{ "en25p32",	0x1c, 0x2016, 64 * 1024, 64, FL_NONE },
112 	{ "en25p64",	0x1c, 0x2017, 64 * 1024, 128, FL_NONE },
113 	{ "en25q64",	0x1c, 0x3017, 64 * 1024, 128, FL_ERASE_4K },
114 	{ "m25p64",	0x20, 0x2017, 64 * 1024, 128, FL_NONE },
115 	{ "mx25ll32",	0xc2, 0x2016, 64 * 1024, 64, FL_NONE },
116 	{ "mx25ll64",	0xc2, 0x2017, 64 * 1024, 128, FL_NONE },
117 	{ "mx25ll128",	0xc2, 0x2018, 64 * 1024, 256, FL_ERASE_4K | FL_ERASE_32K },
118 	{ "mx25ll256",	0xc2, 0x2019, 64 * 1024, 512, FL_ERASE_4K | FL_ERASE_32K | FL_ENABLE_4B_ADDR },
119 	{ "s25fl032",	0x01, 0x0215, 64 * 1024, 64, FL_NONE },
120 	{ "s25fl064",	0x01, 0x0216, 64 * 1024, 128, FL_NONE },
121 	{ "s25fl128",	0x01, 0x2018, 64 * 1024, 256, FL_NONE },
122 	{ "s25fl256s",	0x01, 0x0219, 64 * 1024, 512, FL_NONE },
123 	{ "SST25VF032B", 0xbf, 0x254a, 64 * 1024, 64, FL_ERASE_4K | FL_ERASE_32K },
124 
125 	/* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
126 	{ "w25x32",	0xef, 0x3016, 64 * 1024, 64, FL_ERASE_4K },
127 	{ "w25q32",	0xef, 0x4016, 64 * 1024, 64, FL_ERASE_4K },
128 	{ "w25q64",	0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
129 	{ "w25q64bv",	0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
130 	{ "w25q128",	0xef, 0x4018, 64 * 1024, 256, FL_ERASE_4K },
131 	{ "w25q256",	0xef, 0x4019, 64 * 1024, 512, FL_ERASE_4K },
132 };
133 
134 static uint8_t
135 mx25l_get_status(device_t dev)
136 {
137 	uint8_t txBuf[2], rxBuf[2];
138 	struct spi_command cmd;
139 	int err;
140 
141 	memset(&cmd, 0, sizeof(cmd));
142 	memset(txBuf, 0, sizeof(txBuf));
143 	memset(rxBuf, 0, sizeof(rxBuf));
144 
145 	txBuf[0] = CMD_READ_STATUS;
146 	cmd.tx_cmd = txBuf;
147 	cmd.rx_cmd = rxBuf;
148 	cmd.rx_cmd_sz = 2;
149 	cmd.tx_cmd_sz = 2;
150 	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
151 	return (rxBuf[1]);
152 }
153 
154 static void
155 mx25l_wait_for_device_ready(device_t dev)
156 {
157 	while ((mx25l_get_status(dev) & STATUS_WIP))
158 		continue;
159 }
160 
161 static struct mx25l_flash_ident*
162 mx25l_get_device_ident(struct mx25l_softc *sc)
163 {
164 	device_t dev = sc->sc_dev;
165 	uint8_t txBuf[8], rxBuf[8];
166 	struct spi_command cmd;
167 	uint8_t manufacturer_id;
168 	uint16_t dev_id;
169 	int err, i;
170 
171 	memset(&cmd, 0, sizeof(cmd));
172 	memset(txBuf, 0, sizeof(txBuf));
173 	memset(rxBuf, 0, sizeof(rxBuf));
174 
175 	txBuf[0] = CMD_READ_IDENT;
176 	cmd.tx_cmd = &txBuf;
177 	cmd.rx_cmd = &rxBuf;
178 	/*
179 	 * Some compatible devices has extended two-bytes ID
180 	 * We'll use only manufacturer/deviceid atm
181 	 */
182 	cmd.tx_cmd_sz = 4;
183 	cmd.rx_cmd_sz = 4;
184 	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
185 	if (err)
186 		return (NULL);
187 
188 	manufacturer_id = rxBuf[1];
189 	dev_id = (rxBuf[2] << 8) | (rxBuf[3]);
190 
191 	for (i = 0;
192 	    i < nitems(flash_devices); i++) {
193 		if ((flash_devices[i].manufacturer_id == manufacturer_id) &&
194 		    (flash_devices[i].device_id == dev_id))
195 			return &flash_devices[i];
196 	}
197 
198 	printf("Unknown SPI flash device. Vendor: %02x, device id: %04x\n",
199 	    manufacturer_id, dev_id);
200 	return (NULL);
201 }
202 
203 static void
204 mx25l_set_writable(device_t dev, int writable)
205 {
206 	uint8_t txBuf[1], rxBuf[1];
207 	struct spi_command cmd;
208 	int err;
209 
210 	memset(&cmd, 0, sizeof(cmd));
211 	memset(txBuf, 0, sizeof(txBuf));
212 	memset(rxBuf, 0, sizeof(rxBuf));
213 
214 	txBuf[0] = writable ? CMD_WRITE_ENABLE : CMD_WRITE_DISABLE;
215 	cmd.tx_cmd = txBuf;
216 	cmd.rx_cmd = rxBuf;
217 	cmd.rx_cmd_sz = 1;
218 	cmd.tx_cmd_sz = 1;
219 	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
220 }
221 
222 static void
223 mx25l_erase_cmd(device_t dev, off_t sector, uint8_t ecmd)
224 {
225 	struct mx25l_softc *sc;
226 	uint8_t txBuf[5], rxBuf[5];
227 	struct spi_command cmd;
228 	int err;
229 
230 	sc = device_get_softc(dev);
231 
232 	mx25l_wait_for_device_ready(dev);
233 	mx25l_set_writable(dev, 1);
234 
235 	memset(&cmd, 0, sizeof(cmd));
236 	memset(txBuf, 0, sizeof(txBuf));
237 	memset(rxBuf, 0, sizeof(rxBuf));
238 
239 	txBuf[0] = ecmd;
240 	cmd.tx_cmd = txBuf;
241 	cmd.rx_cmd = rxBuf;
242 	if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
243 		cmd.rx_cmd_sz = 5;
244 		cmd.tx_cmd_sz = 5;
245 		txBuf[1] = ((sector >> 24) & 0xff);
246 		txBuf[2] = ((sector >> 16) & 0xff);
247 		txBuf[3] = ((sector >> 8) & 0xff);
248 		txBuf[4] = (sector & 0xff);
249 	} else {
250 		cmd.rx_cmd_sz = 4;
251 		cmd.tx_cmd_sz = 4;
252 		txBuf[1] = ((sector >> 16) & 0xff);
253 		txBuf[2] = ((sector >> 8) & 0xff);
254 		txBuf[3] = (sector & 0xff);
255 	}
256 	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
257 }
258 
259 static int
260 mx25l_write(device_t dev, off_t offset, caddr_t data, off_t count)
261 {
262 	struct mx25l_softc *sc;
263 	uint8_t txBuf[8], rxBuf[8];
264 	struct spi_command cmd;
265 	off_t write_offset;
266 	long bytes_to_write, bytes_writen;
267 	device_t pdev;
268 	int err = 0;
269 
270 	pdev = device_get_parent(dev);
271 	sc = device_get_softc(dev);
272 
273 	if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
274 		cmd.tx_cmd_sz = 5;
275 		cmd.rx_cmd_sz = 5;
276 	} else {
277 		cmd.tx_cmd_sz = 4;
278 		cmd.rx_cmd_sz = 4;
279 	}
280 
281 	bytes_writen = 0;
282 	write_offset = offset;
283 
284 	/*
285 	 * Use the erase sectorsize here since blocks are fully erased
286 	 * first before they're written to.
287 	 */
288 	if (count % sc->sc_sectorsize != 0 || offset % sc->sc_sectorsize != 0)
289 		return (EIO);
290 
291 	/*
292 	 * Assume here that we write per-sector only
293 	 * and sector size should be 256 bytes aligned
294 	 */
295 	KASSERT(write_offset % FLASH_PAGE_SIZE == 0,
296 	    ("offset for BIO_WRITE is not page size (%d bytes) aligned",
297 		FLASH_PAGE_SIZE));
298 
299 	/*
300 	 * Maximum write size for CMD_PAGE_PROGRAM is
301 	 * FLASH_PAGE_SIZE, so split data to chunks
302 	 * FLASH_PAGE_SIZE bytes eash and write them
303 	 * one by one
304 	 */
305 	while (bytes_writen < count) {
306 		/*
307 		 * If we crossed sector boundary - erase next sector
308 		 */
309 		if (((offset + bytes_writen) % sc->sc_sectorsize) == 0)
310 			mx25l_erase_cmd(dev, offset + bytes_writen, CMD_SECTOR_ERASE);
311 
312 		txBuf[0] = CMD_PAGE_PROGRAM;
313 		if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
314 			txBuf[1] = ((write_offset >> 24) & 0xff);
315 			txBuf[2] = ((write_offset >> 16) & 0xff);
316 			txBuf[3] = ((write_offset >> 8) & 0xff);
317 			txBuf[4] = (write_offset & 0xff);
318 		} else {
319 			txBuf[1] = ((write_offset >> 16) & 0xff);
320 			txBuf[2] = ((write_offset >> 8) & 0xff);
321 			txBuf[3] = (write_offset & 0xff);
322 		}
323 
324 		bytes_to_write = MIN(FLASH_PAGE_SIZE,
325 		    count - bytes_writen);
326 		cmd.tx_cmd = txBuf;
327 		cmd.rx_cmd = rxBuf;
328 		cmd.tx_data = data + bytes_writen;
329 		cmd.tx_data_sz = bytes_to_write;
330 		cmd.rx_data = data + bytes_writen;
331 		cmd.rx_data_sz = bytes_to_write;
332 
333 		/*
334 		 * Eash completed write operation resets WEL
335 		 * (write enable latch) to disabled state,
336 		 * so we re-enable it here
337 		 */
338 		mx25l_wait_for_device_ready(dev);
339 		mx25l_set_writable(dev, 1);
340 
341 		err = SPIBUS_TRANSFER(pdev, dev, &cmd);
342 		if (err)
343 			break;
344 
345 		bytes_writen += bytes_to_write;
346 		write_offset += bytes_to_write;
347 	}
348 
349 	return (err);
350 }
351 
352 static int
353 mx25l_read(device_t dev, off_t offset, caddr_t data, off_t count)
354 {
355 	struct mx25l_softc *sc;
356 	uint8_t txBuf[8], rxBuf[8];
357 	struct spi_command cmd;
358 	device_t pdev;
359 	int err = 0;
360 
361 	pdev = device_get_parent(dev);
362 	sc = device_get_softc(dev);
363 
364 	/*
365 	 * Enforce the disk read sectorsize not the erase sectorsize.
366 	 * In this way, smaller read IO is possible,dramatically
367 	 * speeding up filesystem/geom_compress access.
368 	 */
369 	if (count % sc->sc_disk->d_sectorsize != 0
370 	    || offset % sc->sc_disk->d_sectorsize != 0)
371 		return (EIO);
372 
373 	txBuf[0] = CMD_FAST_READ;
374 	if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
375 		cmd.tx_cmd_sz = 6;
376 		cmd.rx_cmd_sz = 6;
377 
378 		txBuf[1] = ((offset >> 24) & 0xff);
379 		txBuf[2] = ((offset >> 16) & 0xff);
380 		txBuf[3] = ((offset >> 8) & 0xff);
381 		txBuf[4] = (offset & 0xff);
382 		/* Dummy byte */
383 		txBuf[5] = 0;
384 	} else {
385 		cmd.tx_cmd_sz = 5;
386 		cmd.rx_cmd_sz = 5;
387 
388 		txBuf[1] = ((offset >> 16) & 0xff);
389 		txBuf[2] = ((offset >> 8) & 0xff);
390 		txBuf[3] = (offset & 0xff);
391 		/* Dummy byte */
392 		txBuf[4] = 0;
393 	}
394 
395 	cmd.tx_cmd = txBuf;
396 	cmd.rx_cmd = rxBuf;
397 	cmd.tx_data = data;
398 	cmd.tx_data_sz = count;
399 	cmd.rx_data = data;
400 	cmd.rx_data_sz = count;
401 
402 	err = SPIBUS_TRANSFER(pdev, dev, &cmd);
403 
404 	return (err);
405 }
406 
407 static int
408 mx25l_set_4b_mode(device_t dev, uint8_t command)
409 {
410 	uint8_t txBuf[1], rxBuf[1];
411 	struct spi_command cmd;
412 	device_t pdev;
413 	int err;
414 
415 	memset(&cmd, 0, sizeof(cmd));
416 	memset(txBuf, 0, sizeof(txBuf));
417 	memset(rxBuf, 0, sizeof(rxBuf));
418 
419 	pdev = device_get_parent(dev);
420 
421 	cmd.tx_cmd_sz = cmd.rx_cmd_sz = 1;
422 
423 	cmd.tx_cmd = txBuf;
424 	cmd.rx_cmd = rxBuf;
425 
426 	txBuf[0] = command;
427 
428 	err = SPIBUS_TRANSFER(pdev, dev, &cmd);
429 
430 	mx25l_wait_for_device_ready(dev);
431 
432 	return (err);
433 }
434 
435 #ifdef	FDT
436 static struct ofw_compat_data compat_data[] = {
437 	{ "st,m25p",		1 },
438 	{ "jedec,spi-nor",	1 },
439 	{ NULL,			0 },
440 };
441 #endif
442 
443 static int
444 mx25l_probe(device_t dev)
445 {
446 #ifdef FDT
447 	int i;
448 
449 	if (!ofw_bus_status_okay(dev))
450 		return (ENXIO);
451 
452 	/* First try to match the compatible property to the compat_data */
453 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 1)
454 		goto found;
455 
456 	/*
457 	 * Next, try to find a compatible device using the names in the
458 	 * flash_devices structure
459 	 */
460 	for (i = 0; i < nitems(flash_devices); i++)
461 		if (ofw_bus_is_compatible(dev, flash_devices[i].name))
462 			goto found;
463 
464 	return (ENXIO);
465 found:
466 #endif
467 	device_set_desc(dev, "M25Pxx Flash Family");
468 
469 	return (0);
470 }
471 
472 static int
473 mx25l_attach(device_t dev)
474 {
475 	struct mx25l_softc *sc;
476 	struct mx25l_flash_ident *ident;
477 
478 	sc = device_get_softc(dev);
479 	sc->sc_dev = dev;
480 	M25PXX_LOCK_INIT(sc);
481 
482 	ident = mx25l_get_device_ident(sc);
483 	if (ident == NULL)
484 		return (ENXIO);
485 
486 	mx25l_wait_for_device_ready(sc->sc_dev);
487 
488 	sc->sc_disk = disk_alloc();
489 	sc->sc_disk->d_open = mx25l_open;
490 	sc->sc_disk->d_close = mx25l_close;
491 	sc->sc_disk->d_strategy = mx25l_strategy;
492 	sc->sc_disk->d_getattr = mx25l_getattr;
493 	sc->sc_disk->d_ioctl = mx25l_ioctl;
494 	sc->sc_disk->d_name = "flash/spi";
495 	sc->sc_disk->d_drv1 = sc;
496 	sc->sc_disk->d_maxsize = DFLTPHYS;
497 	sc->sc_disk->d_sectorsize = MX25L_SECTORSIZE;
498 	sc->sc_disk->d_mediasize = ident->sectorsize * ident->sectorcount;
499 	sc->sc_disk->d_unit = device_get_unit(sc->sc_dev);
500 	sc->sc_disk->d_dump = NULL;		/* NB: no dumps */
501 	/* Sectorsize for erase operations */
502 	sc->sc_sectorsize =  ident->sectorsize;
503 	sc->sc_flags = ident->flags;
504 
505 	if (sc->sc_flags & FL_ENABLE_4B_ADDR)
506 		mx25l_set_4b_mode(dev, CMD_ENTER_4B_MODE);
507 
508 	if (sc->sc_flags & FL_DISABLE_4B_ADDR)
509 		mx25l_set_4b_mode(dev, CMD_EXIT_4B_MODE);
510 
511         /* NB: use stripesize to hold the erase/region size for RedBoot */
512 	sc->sc_disk->d_stripesize = ident->sectorsize;
513 
514 	disk_create(sc->sc_disk, DISK_VERSION);
515 	bioq_init(&sc->sc_bio_queue);
516 
517 	kproc_create(&mx25l_task, sc, &sc->sc_p, 0, 0, "task: mx25l flash");
518 	device_printf(sc->sc_dev, "%s, sector %d bytes, %d sectors\n",
519 	    ident->name, ident->sectorsize, ident->sectorcount);
520 
521 	return (0);
522 }
523 
524 static int
525 mx25l_detach(device_t dev)
526 {
527 
528 	return (EIO);
529 }
530 
531 static int
532 mx25l_open(struct disk *dp)
533 {
534 	return (0);
535 }
536 
537 static int
538 mx25l_close(struct disk *dp)
539 {
540 
541 	return (0);
542 }
543 
544 static int
545 mx25l_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
546 	struct thread *td)
547 {
548 
549 	return (EINVAL);
550 }
551 
552 static void
553 mx25l_strategy(struct bio *bp)
554 {
555 	struct mx25l_softc *sc;
556 
557 	sc = (struct mx25l_softc *)bp->bio_disk->d_drv1;
558 	M25PXX_LOCK(sc);
559 	bioq_disksort(&sc->sc_bio_queue, bp);
560 	wakeup(sc);
561 	M25PXX_UNLOCK(sc);
562 }
563 
564 static int
565 mx25l_getattr(struct bio *bp)
566 {
567 	struct mx25l_softc *sc;
568 	device_t dev;
569 
570 	if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
571 		return (ENXIO);
572 
573 	sc = bp->bio_disk->d_drv1;
574 	dev = sc->sc_dev;
575 
576 	if (strcmp(bp->bio_attribute, "SPI::device") == 0) {
577 		if (bp->bio_length != sizeof(dev))
578 			return (EFAULT);
579 		bcopy(&dev, bp->bio_data, sizeof(dev));
580 	} else
581 		return (-1);
582 	return (0);
583 }
584 
585 static void
586 mx25l_task(void *arg)
587 {
588 	struct mx25l_softc *sc = (struct mx25l_softc*)arg;
589 	struct bio *bp;
590 	device_t dev;
591 
592 	for (;;) {
593 		dev = sc->sc_dev;
594 		M25PXX_LOCK(sc);
595 		do {
596 			bp = bioq_first(&sc->sc_bio_queue);
597 			if (bp == NULL)
598 				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
599 		} while (bp == NULL);
600 		bioq_remove(&sc->sc_bio_queue, bp);
601 		M25PXX_UNLOCK(sc);
602 
603 		switch (bp->bio_cmd) {
604 		case BIO_READ:
605 			bp->bio_error = mx25l_read(dev, bp->bio_offset,
606 			    bp->bio_data, bp->bio_bcount);
607 			break;
608 		case BIO_WRITE:
609 			bp->bio_error = mx25l_write(dev, bp->bio_offset,
610 			    bp->bio_data, bp->bio_bcount);
611 			break;
612 		default:
613 			bp->bio_error = EINVAL;
614 		}
615 
616 
617 		biodone(bp);
618 	}
619 }
620 
621 static devclass_t mx25l_devclass;
622 
623 static device_method_t mx25l_methods[] = {
624 	/* Device interface */
625 	DEVMETHOD(device_probe,		mx25l_probe),
626 	DEVMETHOD(device_attach,	mx25l_attach),
627 	DEVMETHOD(device_detach,	mx25l_detach),
628 
629 	{ 0, 0 }
630 };
631 
632 static driver_t mx25l_driver = {
633 	"mx25l",
634 	mx25l_methods,
635 	sizeof(struct mx25l_softc),
636 };
637 
638 DRIVER_MODULE(mx25l, spibus, mx25l_driver, mx25l_devclass, 0, 0);
639