xref: /freebsd/sys/dev/flash/mx25l.c (revision a521f2116473fbd8c09db395518f060a27d02334)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Oleksandr Tymoshenko.  All rights reserved.
5  * Copyright (c) 2018 Ian Lepore.  All rights reserved.
6  * Copyright (c) 2006 M. Warner Losh <imp@FreeBSD.org>
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 static 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 	{ "mx25l1606e", 0xc2, 0x2015, 64 * 1024, 32, FL_ERASE_4K},
128 	{ "mx25ll32",	0xc2, 0x2016, 64 * 1024, 64, FL_NONE },
129 	{ "mx25ll64",	0xc2, 0x2017, 64 * 1024, 128, FL_NONE },
130 	{ "mx25ll128",	0xc2, 0x2018, 64 * 1024, 256, FL_ERASE_4K | FL_ERASE_32K },
131 	{ "mx25ll256",	0xc2, 0x2019, 64 * 1024, 512, FL_ERASE_4K | FL_ERASE_32K | FL_ENABLE_4B_ADDR },
132 	{ "s25fl032",	0x01, 0x0215, 64 * 1024, 64, FL_NONE },
133 	{ "s25fl064",	0x01, 0x0216, 64 * 1024, 128, FL_NONE },
134 	{ "s25fl128",	0x01, 0x2018, 64 * 1024, 256, FL_NONE },
135 	{ "s25fl256s",	0x01, 0x0219, 64 * 1024, 512, FL_NONE },
136 	{ "s25fl512s",	0x01, 0x0220, 64 * 1024, 1024, FL_NONE },
137 	{ "SST25VF010A", 0xbf, 0x2549, 4 * 1024, 32, FL_ERASE_4K | FL_ERASE_32K },
138 	{ "SST25VF032B", 0xbf, 0x254a, 64 * 1024, 64, FL_ERASE_4K | FL_ERASE_32K },
139 
140 	/* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
141 	{ "w25x32",	0xef, 0x3016, 64 * 1024, 64, FL_ERASE_4K },
142 	{ "w25x64",	0xef, 0x3017, 64 * 1024, 128, FL_ERASE_4K },
143 	{ "w25q32",	0xef, 0x4016, 64 * 1024, 64, FL_ERASE_4K },
144 	{ "w25q64",	0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
145 	{ "w25q64bv",	0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
146 	{ "w25q128",	0xef, 0x4018, 64 * 1024, 256, FL_ERASE_4K },
147 	{ "w25q256",	0xef, 0x4019, 64 * 1024, 512, FL_ERASE_4K },
148 
149 	 /* Atmel */
150 	{ "at25df641",  0x1f, 0x4800, 64 * 1024, 128, FL_ERASE_4K },
151 
152 	/* GigaDevice */
153 	{ "gd25q64",	0xc8, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
154 	{ "gd25q128",	0xc8, 0x4018, 64 * 1024, 256, FL_ERASE_4K },
155 };
156 
157 static int
158 mx25l_wait_for_device_ready(struct mx25l_softc *sc)
159 {
160 	uint8_t txBuf[2], rxBuf[2];
161 	struct spi_command cmd;
162 	int err;
163 
164 	memset(&cmd, 0, sizeof(cmd));
165 
166 	do {
167 		txBuf[0] = CMD_READ_STATUS;
168 		cmd.tx_cmd = txBuf;
169 		cmd.rx_cmd = rxBuf;
170 		cmd.rx_cmd_sz = 2;
171 		cmd.tx_cmd_sz = 2;
172 		err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
173 	} while (err == 0 && (rxBuf[1] & STATUS_WIP));
174 
175 	return (err);
176 }
177 
178 static struct mx25l_flash_ident*
179 mx25l_get_device_ident(struct mx25l_softc *sc)
180 {
181 	uint8_t txBuf[8], rxBuf[8];
182 	struct spi_command cmd;
183 	uint8_t manufacturer_id;
184 	uint16_t dev_id;
185 	int err, i;
186 
187 	memset(&cmd, 0, sizeof(cmd));
188 	memset(txBuf, 0, sizeof(txBuf));
189 	memset(rxBuf, 0, sizeof(rxBuf));
190 
191 	txBuf[0] = CMD_READ_IDENT;
192 	cmd.tx_cmd = &txBuf;
193 	cmd.rx_cmd = &rxBuf;
194 	/*
195 	 * Some compatible devices has extended two-bytes ID
196 	 * We'll use only manufacturer/deviceid atm
197 	 */
198 	cmd.tx_cmd_sz = 4;
199 	cmd.rx_cmd_sz = 4;
200 	err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
201 	if (err)
202 		return (NULL);
203 
204 	manufacturer_id = rxBuf[1];
205 	dev_id = (rxBuf[2] << 8) | (rxBuf[3]);
206 
207 	for (i = 0; i < nitems(flash_devices); i++) {
208 		if ((flash_devices[i].manufacturer_id == manufacturer_id) &&
209 		    (flash_devices[i].device_id == dev_id))
210 			return &flash_devices[i];
211 	}
212 
213 	device_printf(sc->sc_dev,
214 	    "Unknown SPI flash device. Vendor: %02x, device id: %04x\n",
215 	    manufacturer_id, dev_id);
216 	return (NULL);
217 }
218 
219 static int
220 mx25l_set_writable(struct mx25l_softc *sc, int writable)
221 {
222 	uint8_t txBuf[1], rxBuf[1];
223 	struct spi_command cmd;
224 	int err;
225 
226 	memset(&cmd, 0, sizeof(cmd));
227 	memset(txBuf, 0, sizeof(txBuf));
228 	memset(rxBuf, 0, sizeof(rxBuf));
229 
230 	txBuf[0] = writable ? CMD_WRITE_ENABLE : CMD_WRITE_DISABLE;
231 	cmd.tx_cmd = txBuf;
232 	cmd.rx_cmd = rxBuf;
233 	cmd.rx_cmd_sz = 1;
234 	cmd.tx_cmd_sz = 1;
235 	err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
236 	return (err);
237 }
238 
239 static int
240 mx25l_erase_cmd(struct mx25l_softc *sc, off_t sector)
241 {
242 	uint8_t txBuf[5], rxBuf[5];
243 	struct spi_command cmd;
244 	int err;
245 
246 	if ((err = mx25l_set_writable(sc, 1)) != 0)
247 		return (err);
248 
249 	memset(&cmd, 0, sizeof(cmd));
250 	memset(txBuf, 0, sizeof(txBuf));
251 	memset(rxBuf, 0, sizeof(rxBuf));
252 
253 	cmd.tx_cmd = txBuf;
254 	cmd.rx_cmd = rxBuf;
255 
256 	if (sc->sc_flags & FL_ERASE_4K)
257 		txBuf[0] = CMD_BLOCK_4K_ERASE;
258 	else if (sc->sc_flags & FL_ERASE_32K)
259 		txBuf[0] = CMD_BLOCK_32K_ERASE;
260 	else
261 		txBuf[0] = CMD_SECTOR_ERASE;
262 
263 	if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
264 		cmd.rx_cmd_sz = 5;
265 		cmd.tx_cmd_sz = 5;
266 		txBuf[1] = ((sector >> 24) & 0xff);
267 		txBuf[2] = ((sector >> 16) & 0xff);
268 		txBuf[3] = ((sector >> 8) & 0xff);
269 		txBuf[4] = (sector & 0xff);
270 	} else {
271 		cmd.rx_cmd_sz = 4;
272 		cmd.tx_cmd_sz = 4;
273 		txBuf[1] = ((sector >> 16) & 0xff);
274 		txBuf[2] = ((sector >> 8) & 0xff);
275 		txBuf[3] = (sector & 0xff);
276 	}
277 	if ((err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd)) != 0)
278 		return (err);
279 	err = mx25l_wait_for_device_ready(sc);
280 	return (err);
281 }
282 
283 static int
284 mx25l_write(struct mx25l_softc *sc, off_t offset, caddr_t data, off_t count)
285 {
286 	uint8_t txBuf[8], rxBuf[8];
287 	struct spi_command cmd;
288 	off_t bytes_to_write;
289 	int err = 0;
290 
291 	if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
292 		cmd.tx_cmd_sz = 5;
293 		cmd.rx_cmd_sz = 5;
294 	} else {
295 		cmd.tx_cmd_sz = 4;
296 		cmd.rx_cmd_sz = 4;
297 	}
298 
299 	/*
300 	 * Writes must be aligned to the erase sectorsize, since blocks are
301 	 * fully erased before they're written to.
302 	 */
303 	if (count % sc->sc_erasesize != 0 || offset % sc->sc_erasesize != 0)
304 		return (EIO);
305 
306 	/*
307 	 * Maximum write size for CMD_PAGE_PROGRAM is FLASH_PAGE_SIZE, so loop
308 	 * to write chunks of FLASH_PAGE_SIZE bytes each.
309 	 */
310 	while (count != 0) {
311 		/* If we crossed a sector boundary, erase the next sector. */
312 		if (((offset) % sc->sc_erasesize) == 0) {
313 			err = mx25l_erase_cmd(sc, offset);
314 			if (err)
315 				break;
316 		}
317 
318 		txBuf[0] = CMD_PAGE_PROGRAM;
319 		if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
320 			txBuf[1] = (offset >> 24) & 0xff;
321 			txBuf[2] = (offset >> 16) & 0xff;
322 			txBuf[3] = (offset >> 8) & 0xff;
323 			txBuf[4] = offset & 0xff;
324 		} else {
325 			txBuf[1] = (offset >> 16) & 0xff;
326 			txBuf[2] = (offset >> 8) & 0xff;
327 			txBuf[3] = offset & 0xff;
328 		}
329 
330 		bytes_to_write = MIN(FLASH_PAGE_SIZE, count);
331 		cmd.tx_cmd = txBuf;
332 		cmd.rx_cmd = rxBuf;
333 		cmd.tx_data = data;
334 		cmd.rx_data = sc->sc_dummybuf;
335 		cmd.tx_data_sz = (uint32_t)bytes_to_write;
336 		cmd.rx_data_sz = (uint32_t)bytes_to_write;
337 
338 		/*
339 		 * Each completed write operation resets WEL (write enable
340 		 * latch) to disabled state, so we re-enable it here.
341 		 */
342 		if ((err = mx25l_wait_for_device_ready(sc)) != 0)
343 			break;
344 		if ((err = mx25l_set_writable(sc, 1)) != 0)
345 			break;
346 
347 		err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
348 		if (err != 0)
349 			break;
350 		err = mx25l_wait_for_device_ready(sc);
351 		if (err)
352 			break;
353 
354 		data   += bytes_to_write;
355 		offset += bytes_to_write;
356 		count  -= bytes_to_write;
357 	}
358 
359 	return (err);
360 }
361 
362 static int
363 mx25l_read(struct mx25l_softc *sc, off_t offset, caddr_t data, off_t count)
364 {
365 	uint8_t txBuf[8], rxBuf[8];
366 	struct spi_command cmd;
367 	int err = 0;
368 
369 	/*
370 	 * Enforce that reads are aligned to the disk sectorsize, not the
371 	 * erase sectorsize.  In this way, smaller read IO is possible,
372 	 * dramatically speeding up filesystem/geom_compress access.
373 	 */
374 	if (count % sc->sc_disk->d_sectorsize != 0 ||
375 	    offset % sc->sc_disk->d_sectorsize != 0)
376 		return (EIO);
377 
378 	txBuf[0] = CMD_FAST_READ;
379 	if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
380 		cmd.tx_cmd_sz = 6;
381 		cmd.rx_cmd_sz = 6;
382 
383 		txBuf[1] = (offset >> 24) & 0xff;
384 		txBuf[2] = (offset >> 16) & 0xff;
385 		txBuf[3] = (offset >> 8) & 0xff;
386 		txBuf[4] = offset & 0xff;
387 		/* Dummy byte */
388 		txBuf[5] = 0;
389 	} else {
390 		cmd.tx_cmd_sz = 5;
391 		cmd.rx_cmd_sz = 5;
392 
393 		txBuf[1] = (offset >> 16) & 0xff;
394 		txBuf[2] = (offset >> 8) & 0xff;
395 		txBuf[3] = offset & 0xff;
396 		/* Dummy byte */
397 		txBuf[4] = 0;
398 	}
399 
400 	cmd.tx_cmd = txBuf;
401 	cmd.rx_cmd = rxBuf;
402 	cmd.tx_data = data;
403 	cmd.rx_data = data;
404 	cmd.tx_data_sz = count;
405 	cmd.rx_data_sz = count;
406 
407 	err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
408 	return (err);
409 }
410 
411 static int
412 mx25l_set_4b_mode(struct mx25l_softc *sc, uint8_t command)
413 {
414 	uint8_t txBuf[1], rxBuf[1];
415 	struct spi_command cmd;
416 	int err;
417 
418 	memset(&cmd, 0, sizeof(cmd));
419 	memset(txBuf, 0, sizeof(txBuf));
420 	memset(rxBuf, 0, sizeof(rxBuf));
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 	if ((err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd)) == 0)
430 		err = mx25l_wait_for_device_ready(sc);
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 	int err;
478 
479 	sc = device_get_softc(dev);
480 	sc->sc_dev = dev;
481 	sc->sc_parent = device_get_parent(sc->sc_dev);
482 
483 	M25PXX_LOCK_INIT(sc);
484 
485 	ident = mx25l_get_device_ident(sc);
486 	if (ident == NULL)
487 		return (ENXIO);
488 
489 	if ((err = mx25l_wait_for_device_ready(sc)) != 0)
490 		return (err);
491 
492 	sc->sc_flags = ident->flags;
493 
494 	if (sc->sc_flags & FL_ERASE_4K)
495 		sc->sc_erasesize = 4 * 1024;
496 	else if (sc->sc_flags & FL_ERASE_32K)
497 		sc->sc_erasesize = 32 * 1024;
498 	else
499 		sc->sc_erasesize = ident->sectorsize;
500 
501 	if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
502 		if ((err = mx25l_set_4b_mode(sc, CMD_ENTER_4B_MODE)) != 0)
503 			return (err);
504 	} else if (sc->sc_flags & FL_DISABLE_4B_ADDR) {
505 		if ((err = mx25l_set_4b_mode(sc, CMD_EXIT_4B_MODE)) != 0)
506 			return (err);
507 	}
508 
509 	sc->sc_disk = disk_alloc();
510 	sc->sc_disk->d_open = mx25l_open;
511 	sc->sc_disk->d_close = mx25l_close;
512 	sc->sc_disk->d_strategy = mx25l_strategy;
513 	sc->sc_disk->d_getattr = mx25l_getattr;
514 	sc->sc_disk->d_ioctl = mx25l_ioctl;
515 	sc->sc_disk->d_name = "flash/spi";
516 	sc->sc_disk->d_drv1 = sc;
517 	sc->sc_disk->d_maxsize = DFLTPHYS;
518 	sc->sc_disk->d_sectorsize = MX25L_SECTORSIZE;
519 	sc->sc_disk->d_mediasize = ident->sectorsize * ident->sectorcount;
520 	sc->sc_disk->d_stripesize = sc->sc_erasesize;
521 	sc->sc_disk->d_unit = device_get_unit(sc->sc_dev);
522 	sc->sc_disk->d_dump = NULL;		/* NB: no dumps */
523 	strlcpy(sc->sc_disk->d_descr, ident->name,
524 	    sizeof(sc->sc_disk->d_descr));
525 
526 	disk_create(sc->sc_disk, DISK_VERSION);
527 	bioq_init(&sc->sc_bio_queue);
528 
529 	kproc_create(&mx25l_task, sc, &sc->sc_p, 0, 0, "task: mx25l flash");
530 	sc->sc_taskstate = TSTATE_RUNNING;
531 
532 	device_printf(sc->sc_dev,
533 	    "device type %s, size %dK in %d sectors of %dK, erase size %dK\n",
534 	    ident->name,
535 	    ident->sectorcount * ident->sectorsize / 1024,
536 	    ident->sectorcount, ident->sectorsize / 1024,
537 	    sc->sc_erasesize / 1024);
538 
539 	return (0);
540 }
541 
542 static int
543 mx25l_detach(device_t dev)
544 {
545 	struct mx25l_softc *sc;
546 	int err;
547 
548 	sc = device_get_softc(dev);
549 	err = 0;
550 
551 	M25PXX_LOCK(sc);
552 	if (sc->sc_taskstate == TSTATE_RUNNING) {
553 		sc->sc_taskstate = TSTATE_STOPPING;
554 		wakeup(sc);
555 		while (err == 0 && sc->sc_taskstate != TSTATE_STOPPED) {
556 			err = msleep(sc, &sc->sc_mtx, 0, "mx25dt", hz * 3);
557 			if (err != 0) {
558 				sc->sc_taskstate = TSTATE_RUNNING;
559 				device_printf(sc->sc_dev,
560 				    "Failed to stop queue task\n");
561 			}
562 		}
563 	}
564 	M25PXX_UNLOCK(sc);
565 
566 	if (err == 0 && sc->sc_taskstate == TSTATE_STOPPED) {
567 		disk_destroy(sc->sc_disk);
568 		bioq_flush(&sc->sc_bio_queue, NULL, ENXIO);
569 		M25PXX_LOCK_DESTROY(sc);
570 	}
571 	return (err);
572 }
573 
574 static int
575 mx25l_open(struct disk *dp)
576 {
577 	return (0);
578 }
579 
580 static int
581 mx25l_close(struct disk *dp)
582 {
583 
584 	return (0);
585 }
586 
587 static int
588 mx25l_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
589 	struct thread *td)
590 {
591 
592 	return (EINVAL);
593 }
594 
595 static void
596 mx25l_strategy(struct bio *bp)
597 {
598 	struct mx25l_softc *sc;
599 
600 	sc = (struct mx25l_softc *)bp->bio_disk->d_drv1;
601 	M25PXX_LOCK(sc);
602 	bioq_disksort(&sc->sc_bio_queue, bp);
603 	wakeup(sc);
604 	M25PXX_UNLOCK(sc);
605 }
606 
607 static int
608 mx25l_getattr(struct bio *bp)
609 {
610 	struct mx25l_softc *sc;
611 	device_t dev;
612 
613 	if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
614 		return (ENXIO);
615 
616 	sc = bp->bio_disk->d_drv1;
617 	dev = sc->sc_dev;
618 
619 	if (strcmp(bp->bio_attribute, "SPI::device") == 0) {
620 		if (bp->bio_length != sizeof(dev))
621 			return (EFAULT);
622 		bcopy(&dev, bp->bio_data, sizeof(dev));
623 	} else
624 		return (-1);
625 	return (0);
626 }
627 
628 static void
629 mx25l_task(void *arg)
630 {
631 	struct mx25l_softc *sc = (struct mx25l_softc*)arg;
632 	struct bio *bp;
633 	device_t dev;
634 
635 	for (;;) {
636 		dev = sc->sc_dev;
637 		M25PXX_LOCK(sc);
638 		do {
639 			if (sc->sc_taskstate == TSTATE_STOPPING) {
640 				sc->sc_taskstate = TSTATE_STOPPED;
641 				M25PXX_UNLOCK(sc);
642 				wakeup(sc);
643 				kproc_exit(0);
644 			}
645 			bp = bioq_first(&sc->sc_bio_queue);
646 			if (bp == NULL)
647 				msleep(sc, &sc->sc_mtx, PRIBIO, "mx25jq", 0);
648 		} while (bp == NULL);
649 		bioq_remove(&sc->sc_bio_queue, bp);
650 		M25PXX_UNLOCK(sc);
651 
652 		switch (bp->bio_cmd) {
653 		case BIO_READ:
654 			bp->bio_error = mx25l_read(sc, bp->bio_offset,
655 			    bp->bio_data, bp->bio_bcount);
656 			break;
657 		case BIO_WRITE:
658 			bp->bio_error = mx25l_write(sc, bp->bio_offset,
659 			    bp->bio_data, bp->bio_bcount);
660 			break;
661 		default:
662 			bp->bio_error = EOPNOTSUPP;
663 		}
664 
665 
666 		biodone(bp);
667 	}
668 }
669 
670 static devclass_t mx25l_devclass;
671 
672 static device_method_t mx25l_methods[] = {
673 	/* Device interface */
674 	DEVMETHOD(device_probe,		mx25l_probe),
675 	DEVMETHOD(device_attach,	mx25l_attach),
676 	DEVMETHOD(device_detach,	mx25l_detach),
677 
678 	{ 0, 0 }
679 };
680 
681 static driver_t mx25l_driver = {
682 	"mx25l",
683 	mx25l_methods,
684 	sizeof(struct mx25l_softc),
685 };
686 
687 DRIVER_MODULE(mx25l, spibus, mx25l_driver, mx25l_devclass, 0, 0);
688 MODULE_DEPEND(mx25l, spibus, 1, 1, 1);
689 #ifdef	FDT
690 MODULE_DEPEND(mx25l, fdt_slicer, 1, 1, 1);
691 SPIBUS_FDT_PNP_INFO(compat_data);
692 #endif
693