xref: /freebsd/sys/dev/flash/mx25l.c (revision ee5cf11617a9b7f034d95c639bd4d27d1f09e848)
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 	{ "w25x64",	0xef, 0x3017, 64 * 1024, 128, FL_ERASE_4K },
128 	{ "w25q32",	0xef, 0x4016, 64 * 1024, 64, FL_ERASE_4K },
129 	{ "w25q64",	0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
130 	{ "w25q64bv",	0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
131 	{ "w25q128",	0xef, 0x4018, 64 * 1024, 256, FL_ERASE_4K },
132 	{ "w25q256",	0xef, 0x4019, 64 * 1024, 512, FL_ERASE_4K },
133 };
134 
135 static uint8_t
136 mx25l_get_status(device_t dev)
137 {
138 	uint8_t txBuf[2], rxBuf[2];
139 	struct spi_command cmd;
140 	int err;
141 
142 	memset(&cmd, 0, sizeof(cmd));
143 	memset(txBuf, 0, sizeof(txBuf));
144 	memset(rxBuf, 0, sizeof(rxBuf));
145 
146 	txBuf[0] = CMD_READ_STATUS;
147 	cmd.tx_cmd = txBuf;
148 	cmd.rx_cmd = rxBuf;
149 	cmd.rx_cmd_sz = 2;
150 	cmd.tx_cmd_sz = 2;
151 	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
152 	return (rxBuf[1]);
153 }
154 
155 static void
156 mx25l_wait_for_device_ready(device_t dev)
157 {
158 	while ((mx25l_get_status(dev) & STATUS_WIP))
159 		continue;
160 }
161 
162 static struct mx25l_flash_ident*
163 mx25l_get_device_ident(struct mx25l_softc *sc)
164 {
165 	device_t dev = sc->sc_dev;
166 	uint8_t txBuf[8], rxBuf[8];
167 	struct spi_command cmd;
168 	uint8_t manufacturer_id;
169 	uint16_t dev_id;
170 	int err, i;
171 
172 	memset(&cmd, 0, sizeof(cmd));
173 	memset(txBuf, 0, sizeof(txBuf));
174 	memset(rxBuf, 0, sizeof(rxBuf));
175 
176 	txBuf[0] = CMD_READ_IDENT;
177 	cmd.tx_cmd = &txBuf;
178 	cmd.rx_cmd = &rxBuf;
179 	/*
180 	 * Some compatible devices has extended two-bytes ID
181 	 * We'll use only manufacturer/deviceid atm
182 	 */
183 	cmd.tx_cmd_sz = 4;
184 	cmd.rx_cmd_sz = 4;
185 	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
186 	if (err)
187 		return (NULL);
188 
189 	manufacturer_id = rxBuf[1];
190 	dev_id = (rxBuf[2] << 8) | (rxBuf[3]);
191 
192 	for (i = 0;
193 	    i < nitems(flash_devices); i++) {
194 		if ((flash_devices[i].manufacturer_id == manufacturer_id) &&
195 		    (flash_devices[i].device_id == dev_id))
196 			return &flash_devices[i];
197 	}
198 
199 	printf("Unknown SPI flash device. Vendor: %02x, device id: %04x\n",
200 	    manufacturer_id, dev_id);
201 	return (NULL);
202 }
203 
204 static void
205 mx25l_set_writable(device_t dev, int writable)
206 {
207 	uint8_t txBuf[1], rxBuf[1];
208 	struct spi_command cmd;
209 	int err;
210 
211 	memset(&cmd, 0, sizeof(cmd));
212 	memset(txBuf, 0, sizeof(txBuf));
213 	memset(rxBuf, 0, sizeof(rxBuf));
214 
215 	txBuf[0] = writable ? CMD_WRITE_ENABLE : CMD_WRITE_DISABLE;
216 	cmd.tx_cmd = txBuf;
217 	cmd.rx_cmd = rxBuf;
218 	cmd.rx_cmd_sz = 1;
219 	cmd.tx_cmd_sz = 1;
220 	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
221 }
222 
223 static void
224 mx25l_erase_cmd(device_t dev, off_t sector, uint8_t ecmd)
225 {
226 	struct mx25l_softc *sc;
227 	uint8_t txBuf[5], rxBuf[5];
228 	struct spi_command cmd;
229 	int err;
230 
231 	sc = device_get_softc(dev);
232 
233 	mx25l_wait_for_device_ready(dev);
234 	mx25l_set_writable(dev, 1);
235 
236 	memset(&cmd, 0, sizeof(cmd));
237 	memset(txBuf, 0, sizeof(txBuf));
238 	memset(rxBuf, 0, sizeof(rxBuf));
239 
240 	txBuf[0] = ecmd;
241 	cmd.tx_cmd = txBuf;
242 	cmd.rx_cmd = rxBuf;
243 	if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
244 		cmd.rx_cmd_sz = 5;
245 		cmd.tx_cmd_sz = 5;
246 		txBuf[1] = ((sector >> 24) & 0xff);
247 		txBuf[2] = ((sector >> 16) & 0xff);
248 		txBuf[3] = ((sector >> 8) & 0xff);
249 		txBuf[4] = (sector & 0xff);
250 	} else {
251 		cmd.rx_cmd_sz = 4;
252 		cmd.tx_cmd_sz = 4;
253 		txBuf[1] = ((sector >> 16) & 0xff);
254 		txBuf[2] = ((sector >> 8) & 0xff);
255 		txBuf[3] = (sector & 0xff);
256 	}
257 	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
258 }
259 
260 static int
261 mx25l_write(device_t dev, off_t offset, caddr_t data, off_t count)
262 {
263 	struct mx25l_softc *sc;
264 	uint8_t txBuf[8], rxBuf[8];
265 	struct spi_command cmd;
266 	off_t write_offset;
267 	long bytes_to_write, bytes_writen;
268 	device_t pdev;
269 	int err = 0;
270 
271 	pdev = device_get_parent(dev);
272 	sc = device_get_softc(dev);
273 
274 	if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
275 		cmd.tx_cmd_sz = 5;
276 		cmd.rx_cmd_sz = 5;
277 	} else {
278 		cmd.tx_cmd_sz = 4;
279 		cmd.rx_cmd_sz = 4;
280 	}
281 
282 	bytes_writen = 0;
283 	write_offset = offset;
284 
285 	/*
286 	 * Use the erase sectorsize here since blocks are fully erased
287 	 * first before they're written to.
288 	 */
289 	if (count % sc->sc_sectorsize != 0 || offset % sc->sc_sectorsize != 0)
290 		return (EIO);
291 
292 	/*
293 	 * Assume here that we write per-sector only
294 	 * and sector size should be 256 bytes aligned
295 	 */
296 	KASSERT(write_offset % FLASH_PAGE_SIZE == 0,
297 	    ("offset for BIO_WRITE is not page size (%d bytes) aligned",
298 		FLASH_PAGE_SIZE));
299 
300 	/*
301 	 * Maximum write size for CMD_PAGE_PROGRAM is
302 	 * FLASH_PAGE_SIZE, so split data to chunks
303 	 * FLASH_PAGE_SIZE bytes eash and write them
304 	 * one by one
305 	 */
306 	while (bytes_writen < count) {
307 		/*
308 		 * If we crossed sector boundary - erase next sector
309 		 */
310 		if (((offset + bytes_writen) % sc->sc_sectorsize) == 0)
311 			mx25l_erase_cmd(dev, offset + bytes_writen, CMD_SECTOR_ERASE);
312 
313 		txBuf[0] = CMD_PAGE_PROGRAM;
314 		if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
315 			txBuf[1] = ((write_offset >> 24) & 0xff);
316 			txBuf[2] = ((write_offset >> 16) & 0xff);
317 			txBuf[3] = ((write_offset >> 8) & 0xff);
318 			txBuf[4] = (write_offset & 0xff);
319 		} else {
320 			txBuf[1] = ((write_offset >> 16) & 0xff);
321 			txBuf[2] = ((write_offset >> 8) & 0xff);
322 			txBuf[3] = (write_offset & 0xff);
323 		}
324 
325 		bytes_to_write = MIN(FLASH_PAGE_SIZE,
326 		    count - bytes_writen);
327 		cmd.tx_cmd = txBuf;
328 		cmd.rx_cmd = rxBuf;
329 		cmd.tx_data = data + bytes_writen;
330 		cmd.tx_data_sz = bytes_to_write;
331 		cmd.rx_data = data + bytes_writen;
332 		cmd.rx_data_sz = bytes_to_write;
333 
334 		/*
335 		 * Eash completed write operation resets WEL
336 		 * (write enable latch) to disabled state,
337 		 * so we re-enable it here
338 		 */
339 		mx25l_wait_for_device_ready(dev);
340 		mx25l_set_writable(dev, 1);
341 
342 		err = SPIBUS_TRANSFER(pdev, dev, &cmd);
343 		if (err)
344 			break;
345 
346 		bytes_writen += bytes_to_write;
347 		write_offset += bytes_to_write;
348 	}
349 
350 	return (err);
351 }
352 
353 static int
354 mx25l_read(device_t dev, off_t offset, caddr_t data, off_t count)
355 {
356 	struct mx25l_softc *sc;
357 	uint8_t txBuf[8], rxBuf[8];
358 	struct spi_command cmd;
359 	device_t pdev;
360 	int err = 0;
361 
362 	pdev = device_get_parent(dev);
363 	sc = device_get_softc(dev);
364 
365 	/*
366 	 * Enforce the disk read sectorsize not the erase sectorsize.
367 	 * In this way, smaller read IO is possible,dramatically
368 	 * speeding up filesystem/geom_compress access.
369 	 */
370 	if (count % sc->sc_disk->d_sectorsize != 0
371 	    || offset % sc->sc_disk->d_sectorsize != 0)
372 		return (EIO);
373 
374 	txBuf[0] = CMD_FAST_READ;
375 	if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
376 		cmd.tx_cmd_sz = 6;
377 		cmd.rx_cmd_sz = 6;
378 
379 		txBuf[1] = ((offset >> 24) & 0xff);
380 		txBuf[2] = ((offset >> 16) & 0xff);
381 		txBuf[3] = ((offset >> 8) & 0xff);
382 		txBuf[4] = (offset & 0xff);
383 		/* Dummy byte */
384 		txBuf[5] = 0;
385 	} else {
386 		cmd.tx_cmd_sz = 5;
387 		cmd.rx_cmd_sz = 5;
388 
389 		txBuf[1] = ((offset >> 16) & 0xff);
390 		txBuf[2] = ((offset >> 8) & 0xff);
391 		txBuf[3] = (offset & 0xff);
392 		/* Dummy byte */
393 		txBuf[4] = 0;
394 	}
395 
396 	cmd.tx_cmd = txBuf;
397 	cmd.rx_cmd = rxBuf;
398 	cmd.tx_data = data;
399 	cmd.tx_data_sz = count;
400 	cmd.rx_data = data;
401 	cmd.rx_data_sz = count;
402 
403 	err = SPIBUS_TRANSFER(pdev, dev, &cmd);
404 
405 	return (err);
406 }
407 
408 static int
409 mx25l_set_4b_mode(device_t dev, uint8_t command)
410 {
411 	uint8_t txBuf[1], rxBuf[1];
412 	struct spi_command cmd;
413 	device_t pdev;
414 	int err;
415 
416 	memset(&cmd, 0, sizeof(cmd));
417 	memset(txBuf, 0, sizeof(txBuf));
418 	memset(rxBuf, 0, sizeof(rxBuf));
419 
420 	pdev = device_get_parent(dev);
421 
422 	cmd.tx_cmd_sz = cmd.rx_cmd_sz = 1;
423 
424 	cmd.tx_cmd = txBuf;
425 	cmd.rx_cmd = rxBuf;
426 
427 	txBuf[0] = command;
428 
429 	err = SPIBUS_TRANSFER(pdev, dev, &cmd);
430 
431 	mx25l_wait_for_device_ready(dev);
432 
433 	return (err);
434 }
435 
436 #ifdef	FDT
437 static struct ofw_compat_data compat_data[] = {
438 	{ "st,m25p",		1 },
439 	{ "jedec,spi-nor",	1 },
440 	{ NULL,			0 },
441 };
442 #endif
443 
444 static int
445 mx25l_probe(device_t dev)
446 {
447 #ifdef FDT
448 	int i;
449 
450 	if (!ofw_bus_status_okay(dev))
451 		return (ENXIO);
452 
453 	/* First try to match the compatible property to the compat_data */
454 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 1)
455 		goto found;
456 
457 	/*
458 	 * Next, try to find a compatible device using the names in the
459 	 * flash_devices structure
460 	 */
461 	for (i = 0; i < nitems(flash_devices); i++)
462 		if (ofw_bus_is_compatible(dev, flash_devices[i].name))
463 			goto found;
464 
465 	return (ENXIO);
466 found:
467 #endif
468 	device_set_desc(dev, "M25Pxx Flash Family");
469 
470 	return (0);
471 }
472 
473 static int
474 mx25l_attach(device_t dev)
475 {
476 	struct mx25l_softc *sc;
477 	struct mx25l_flash_ident *ident;
478 
479 	sc = device_get_softc(dev);
480 	sc->sc_dev = dev;
481 	M25PXX_LOCK_INIT(sc);
482 
483 	ident = mx25l_get_device_ident(sc);
484 	if (ident == NULL)
485 		return (ENXIO);
486 
487 	mx25l_wait_for_device_ready(sc->sc_dev);
488 
489 	sc->sc_disk = disk_alloc();
490 	sc->sc_disk->d_open = mx25l_open;
491 	sc->sc_disk->d_close = mx25l_close;
492 	sc->sc_disk->d_strategy = mx25l_strategy;
493 	sc->sc_disk->d_getattr = mx25l_getattr;
494 	sc->sc_disk->d_ioctl = mx25l_ioctl;
495 	sc->sc_disk->d_name = "flash/spi";
496 	sc->sc_disk->d_drv1 = sc;
497 	sc->sc_disk->d_maxsize = DFLTPHYS;
498 	sc->sc_disk->d_sectorsize = MX25L_SECTORSIZE;
499 	sc->sc_disk->d_mediasize = ident->sectorsize * ident->sectorcount;
500 	sc->sc_disk->d_unit = device_get_unit(sc->sc_dev);
501 	sc->sc_disk->d_dump = NULL;		/* NB: no dumps */
502 	/* Sectorsize for erase operations */
503 	sc->sc_sectorsize =  ident->sectorsize;
504 	sc->sc_flags = ident->flags;
505 
506 	if (sc->sc_flags & FL_ENABLE_4B_ADDR)
507 		mx25l_set_4b_mode(dev, CMD_ENTER_4B_MODE);
508 
509 	if (sc->sc_flags & FL_DISABLE_4B_ADDR)
510 		mx25l_set_4b_mode(dev, CMD_EXIT_4B_MODE);
511 
512         /* NB: use stripesize to hold the erase/region size for RedBoot */
513 	sc->sc_disk->d_stripesize = ident->sectorsize;
514 
515 	disk_create(sc->sc_disk, DISK_VERSION);
516 	bioq_init(&sc->sc_bio_queue);
517 
518 	kproc_create(&mx25l_task, sc, &sc->sc_p, 0, 0, "task: mx25l flash");
519 	device_printf(sc->sc_dev, "%s, sector %d bytes, %d sectors\n",
520 	    ident->name, ident->sectorsize, ident->sectorcount);
521 
522 	return (0);
523 }
524 
525 static int
526 mx25l_detach(device_t dev)
527 {
528 
529 	return (EIO);
530 }
531 
532 static int
533 mx25l_open(struct disk *dp)
534 {
535 	return (0);
536 }
537 
538 static int
539 mx25l_close(struct disk *dp)
540 {
541 
542 	return (0);
543 }
544 
545 static int
546 mx25l_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
547 	struct thread *td)
548 {
549 
550 	return (EINVAL);
551 }
552 
553 static void
554 mx25l_strategy(struct bio *bp)
555 {
556 	struct mx25l_softc *sc;
557 
558 	sc = (struct mx25l_softc *)bp->bio_disk->d_drv1;
559 	M25PXX_LOCK(sc);
560 	bioq_disksort(&sc->sc_bio_queue, bp);
561 	wakeup(sc);
562 	M25PXX_UNLOCK(sc);
563 }
564 
565 static int
566 mx25l_getattr(struct bio *bp)
567 {
568 	struct mx25l_softc *sc;
569 	device_t dev;
570 
571 	if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
572 		return (ENXIO);
573 
574 	sc = bp->bio_disk->d_drv1;
575 	dev = sc->sc_dev;
576 
577 	if (strcmp(bp->bio_attribute, "SPI::device") == 0) {
578 		if (bp->bio_length != sizeof(dev))
579 			return (EFAULT);
580 		bcopy(&dev, bp->bio_data, sizeof(dev));
581 	} else
582 		return (-1);
583 	return (0);
584 }
585 
586 static void
587 mx25l_task(void *arg)
588 {
589 	struct mx25l_softc *sc = (struct mx25l_softc*)arg;
590 	struct bio *bp;
591 	device_t dev;
592 
593 	for (;;) {
594 		dev = sc->sc_dev;
595 		M25PXX_LOCK(sc);
596 		do {
597 			bp = bioq_first(&sc->sc_bio_queue);
598 			if (bp == NULL)
599 				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
600 		} while (bp == NULL);
601 		bioq_remove(&sc->sc_bio_queue, bp);
602 		M25PXX_UNLOCK(sc);
603 
604 		switch (bp->bio_cmd) {
605 		case BIO_READ:
606 			bp->bio_error = mx25l_read(dev, bp->bio_offset,
607 			    bp->bio_data, bp->bio_bcount);
608 			break;
609 		case BIO_WRITE:
610 			bp->bio_error = mx25l_write(dev, bp->bio_offset,
611 			    bp->bio_data, bp->bio_bcount);
612 			break;
613 		default:
614 			bp->bio_error = EINVAL;
615 		}
616 
617 
618 		biodone(bp);
619 	}
620 }
621 
622 static devclass_t mx25l_devclass;
623 
624 static device_method_t mx25l_methods[] = {
625 	/* Device interface */
626 	DEVMETHOD(device_probe,		mx25l_probe),
627 	DEVMETHOD(device_attach,	mx25l_attach),
628 	DEVMETHOD(device_detach,	mx25l_detach),
629 
630 	{ 0, 0 }
631 };
632 
633 static driver_t mx25l_driver = {
634 	"mx25l",
635 	mx25l_methods,
636 	sizeof(struct mx25l_softc),
637 };
638 
639 DRIVER_MODULE(mx25l, spibus, mx25l_driver, mx25l_devclass, 0, 0);
640