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