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