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