xref: /freebsd/sys/dev/flash/n25q.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1eb69ed7fSRuslan Bukin /*-
2eb69ed7fSRuslan Bukin  * Copyright (c) 2009 Oleksandr Tymoshenko.  All rights reserved.
3eb69ed7fSRuslan Bukin  * Copyright (c) 2017 Ruslan Bukin <br@bsdpad.com>
4eb69ed7fSRuslan Bukin  * Copyright (c) 2018 Ian Lepore.  All rights reserved.
5f86e6000SWarner Losh  * Copyright (c) 2006 M. Warner Losh <imp@FreeBSD.org>
6eb69ed7fSRuslan Bukin  *
7eb69ed7fSRuslan Bukin  * This software was developed by SRI International and the University of
8eb69ed7fSRuslan Bukin  * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
9eb69ed7fSRuslan Bukin  * ("CTSRD"), as part of the DARPA CRASH research programme.
10eb69ed7fSRuslan Bukin  *
11eb69ed7fSRuslan Bukin  * Redistribution and use in source and binary forms, with or without
12eb69ed7fSRuslan Bukin  * modification, are permitted provided that the following conditions
13eb69ed7fSRuslan Bukin  * are met:
14eb69ed7fSRuslan Bukin  * 1. Redistributions of source code must retain the above copyright
15eb69ed7fSRuslan Bukin  *    notice, this list of conditions and the following disclaimer.
16eb69ed7fSRuslan Bukin  * 2. Redistributions in binary form must reproduce the above copyright
17eb69ed7fSRuslan Bukin  *    notice, this list of conditions and the following disclaimer in the
18eb69ed7fSRuslan Bukin  *    documentation and/or other materials provided with the distribution.
19eb69ed7fSRuslan Bukin  *
20eb69ed7fSRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21eb69ed7fSRuslan Bukin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22eb69ed7fSRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23eb69ed7fSRuslan Bukin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24eb69ed7fSRuslan Bukin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25eb69ed7fSRuslan Bukin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26eb69ed7fSRuslan Bukin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27eb69ed7fSRuslan Bukin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28eb69ed7fSRuslan Bukin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29eb69ed7fSRuslan Bukin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30eb69ed7fSRuslan Bukin  * SUCH DAMAGE.
31eb69ed7fSRuslan Bukin  */
32eb69ed7fSRuslan Bukin 
33eb69ed7fSRuslan Bukin /* n25q Quad SPI Flash driver. */
34eb69ed7fSRuslan Bukin 
35eb69ed7fSRuslan Bukin #include <sys/cdefs.h>
36eb69ed7fSRuslan Bukin #include "opt_platform.h"
37eb69ed7fSRuslan Bukin 
38eb69ed7fSRuslan Bukin #include <sys/param.h>
39eb69ed7fSRuslan Bukin #include <sys/systm.h>
40eb69ed7fSRuslan Bukin #include <sys/bio.h>
41eb69ed7fSRuslan Bukin #include <sys/bus.h>
42eb69ed7fSRuslan Bukin #include <sys/conf.h>
43eb69ed7fSRuslan Bukin #include <sys/kernel.h>
44eb69ed7fSRuslan Bukin #include <sys/kthread.h>
45eb69ed7fSRuslan Bukin #include <sys/lock.h>
46eb69ed7fSRuslan Bukin #include <sys/mbuf.h>
47eb69ed7fSRuslan Bukin #include <sys/malloc.h>
48eb69ed7fSRuslan Bukin #include <sys/module.h>
49eb69ed7fSRuslan Bukin #include <sys/mutex.h>
50eb69ed7fSRuslan Bukin #include <geom/geom_disk.h>
51eb69ed7fSRuslan Bukin 
52eb69ed7fSRuslan Bukin #include <machine/bus.h>
53eb69ed7fSRuslan Bukin 
54eb69ed7fSRuslan Bukin #include <dev/fdt/fdt_common.h>
55eb69ed7fSRuslan Bukin #include <dev/ofw/ofw_bus_subr.h>
56eb69ed7fSRuslan Bukin #include <dev/ofw/openfirm.h>
57eb69ed7fSRuslan Bukin 
58eb69ed7fSRuslan Bukin #include <dev/flash/mx25lreg.h>
59eb69ed7fSRuslan Bukin 
60eb69ed7fSRuslan Bukin #include "qspi_if.h"
61eb69ed7fSRuslan Bukin 
62eb69ed7fSRuslan Bukin #define	N25Q_DEBUG
63eb69ed7fSRuslan Bukin #undef N25Q_DEBUG
64eb69ed7fSRuslan Bukin 
65eb69ed7fSRuslan Bukin #ifdef N25Q_DEBUG
66eb69ed7fSRuslan Bukin #define dprintf(fmt, ...)  printf(fmt, ##__VA_ARGS__)
67eb69ed7fSRuslan Bukin #else
68eb69ed7fSRuslan Bukin #define dprintf(fmt, ...)
69eb69ed7fSRuslan Bukin #endif
70eb69ed7fSRuslan Bukin 
71eb69ed7fSRuslan Bukin #define	FL_NONE			0x00
72eb69ed7fSRuslan Bukin #define	FL_ERASE_4K		0x01
73eb69ed7fSRuslan Bukin #define	FL_ERASE_32K		0x02
74eb69ed7fSRuslan Bukin #define	FL_ENABLE_4B_ADDR	0x04
75eb69ed7fSRuslan Bukin #define	FL_DISABLE_4B_ADDR	0x08
76eb69ed7fSRuslan Bukin 
77eb69ed7fSRuslan Bukin /*
78eb69ed7fSRuslan Bukin  * Define the sectorsize to be a smaller size rather than the flash
79eb69ed7fSRuslan Bukin  * sector size. Trying to run FFS off of a 64k flash sector size
80eb69ed7fSRuslan Bukin  * results in a completely un-usable system.
81eb69ed7fSRuslan Bukin  */
82eb69ed7fSRuslan Bukin #define	FLASH_SECTORSIZE	512
83eb69ed7fSRuslan Bukin 
84eb69ed7fSRuslan Bukin struct n25q_flash_ident {
85eb69ed7fSRuslan Bukin 	const char	*name;
86eb69ed7fSRuslan Bukin 	uint8_t		manufacturer_id;
87eb69ed7fSRuslan Bukin 	uint16_t	device_id;
88eb69ed7fSRuslan Bukin 	unsigned int	sectorsize;
89eb69ed7fSRuslan Bukin 	unsigned int	sectorcount;
90eb69ed7fSRuslan Bukin 	unsigned int	flags;
91eb69ed7fSRuslan Bukin };
92eb69ed7fSRuslan Bukin 
93eb69ed7fSRuslan Bukin struct n25q_softc {
94eb69ed7fSRuslan Bukin 	device_t		dev;
95eb69ed7fSRuslan Bukin 	bus_space_tag_t		bst;
96eb69ed7fSRuslan Bukin 	bus_space_handle_t	bsh;
97eb69ed7fSRuslan Bukin 	void			*ih;
98eb69ed7fSRuslan Bukin 	struct resource		*res[3];
99eb69ed7fSRuslan Bukin 
100eb69ed7fSRuslan Bukin 	uint8_t			sc_manufacturer_id;
101eb69ed7fSRuslan Bukin 	uint16_t		device_id;
102eb69ed7fSRuslan Bukin 	unsigned int		sc_sectorsize;
103eb69ed7fSRuslan Bukin 	struct mtx		sc_mtx;
104eb69ed7fSRuslan Bukin 	struct disk		*sc_disk;
105eb69ed7fSRuslan Bukin 	struct proc		*sc_p;
106eb69ed7fSRuslan Bukin 	struct bio_queue_head	sc_bio_queue;
107eb69ed7fSRuslan Bukin 	unsigned int		sc_flags;
108eb69ed7fSRuslan Bukin 	unsigned int		sc_taskstate;
109eb69ed7fSRuslan Bukin };
110eb69ed7fSRuslan Bukin 
111eb69ed7fSRuslan Bukin #define	TSTATE_STOPPED	0
112eb69ed7fSRuslan Bukin #define	TSTATE_STOPPING	1
113eb69ed7fSRuslan Bukin #define	TSTATE_RUNNING	2
114eb69ed7fSRuslan Bukin 
115eb69ed7fSRuslan Bukin #define	N25Q_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
116eb69ed7fSRuslan Bukin #define	N25Q_UNLOCK(_sc)	mtx_unlock(&(_sc)->sc_mtx)
117eb69ed7fSRuslan Bukin #define N25Q_LOCK_INIT(_sc)					\
118eb69ed7fSRuslan Bukin 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev),	\
119eb69ed7fSRuslan Bukin 	    "n25q", MTX_DEF)
120eb69ed7fSRuslan Bukin #define N25Q_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
121eb69ed7fSRuslan Bukin #define N25Q_ASSERT_LOCKED(_sc)				\
122eb69ed7fSRuslan Bukin 	mtx_assert(&_sc->sc_mtx, MA_OWNED);
123eb69ed7fSRuslan Bukin #define N25Q_ASSERT_UNLOCKED(_sc)				\
124eb69ed7fSRuslan Bukin 	mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
125eb69ed7fSRuslan Bukin 
126eb69ed7fSRuslan Bukin static struct ofw_compat_data compat_data[] = {
127eb69ed7fSRuslan Bukin 	{ "n25q00aa",		1 },
128eb69ed7fSRuslan Bukin 	{ NULL,			0 },
129eb69ed7fSRuslan Bukin };
130eb69ed7fSRuslan Bukin 
131eb69ed7fSRuslan Bukin /* disk routines */
132eb69ed7fSRuslan Bukin static int n25q_open(struct disk *dp);
133eb69ed7fSRuslan Bukin static int n25q_close(struct disk *dp);
134eb69ed7fSRuslan Bukin static int n25q_ioctl(struct disk *, u_long, void *, int, struct thread *);
135eb69ed7fSRuslan Bukin static void n25q_strategy(struct bio *bp);
136eb69ed7fSRuslan Bukin static int n25q_getattr(struct bio *bp);
137eb69ed7fSRuslan Bukin static void n25q_task(void *arg);
138eb69ed7fSRuslan Bukin 
1396dfd0500SIan Lepore static struct n25q_flash_ident flash_devices[] = {
140eb69ed7fSRuslan Bukin 	{ "n25q00", 0x20, 0xbb21, (64 * 1024), 2048, FL_ENABLE_4B_ADDR},
141eb69ed7fSRuslan Bukin };
142eb69ed7fSRuslan Bukin 
143eb69ed7fSRuslan Bukin static int
n25q_wait_for_device_ready(device_t dev)144eb69ed7fSRuslan Bukin n25q_wait_for_device_ready(device_t dev)
145eb69ed7fSRuslan Bukin {
146eb69ed7fSRuslan Bukin 	device_t pdev;
147eb69ed7fSRuslan Bukin 	uint8_t status;
148eb69ed7fSRuslan Bukin 	int err;
149eb69ed7fSRuslan Bukin 
150eb69ed7fSRuslan Bukin 	pdev = device_get_parent(dev);
151eb69ed7fSRuslan Bukin 
152eb69ed7fSRuslan Bukin 	do {
153eb69ed7fSRuslan Bukin 		err = QSPI_READ_REG(pdev, dev, CMD_READ_STATUS, &status, 1);
154eb69ed7fSRuslan Bukin 	} while (err == 0 && (status & STATUS_WIP));
155eb69ed7fSRuslan Bukin 
156eb69ed7fSRuslan Bukin 	return (err);
157eb69ed7fSRuslan Bukin }
158eb69ed7fSRuslan Bukin 
159eb69ed7fSRuslan Bukin static struct n25q_flash_ident*
n25q_get_device_ident(struct n25q_softc * sc)160eb69ed7fSRuslan Bukin n25q_get_device_ident(struct n25q_softc *sc)
161eb69ed7fSRuslan Bukin {
162eb69ed7fSRuslan Bukin 	uint8_t manufacturer_id;
163eb69ed7fSRuslan Bukin 	uint16_t dev_id;
164eb69ed7fSRuslan Bukin 	device_t pdev;
165eb69ed7fSRuslan Bukin 	uint8_t data[4];
166eb69ed7fSRuslan Bukin 	int i;
167eb69ed7fSRuslan Bukin 
168eb69ed7fSRuslan Bukin 	pdev = device_get_parent(sc->dev);
169eb69ed7fSRuslan Bukin 
170eb69ed7fSRuslan Bukin 	QSPI_READ_REG(pdev, sc->dev, CMD_READ_IDENT, (uint8_t *)&data[0], 4);
171eb69ed7fSRuslan Bukin 
172eb69ed7fSRuslan Bukin 	manufacturer_id = data[0];
173eb69ed7fSRuslan Bukin 	dev_id = (data[1] << 8) | (data[2]);
174eb69ed7fSRuslan Bukin 
175eb69ed7fSRuslan Bukin 	for (i = 0; i < nitems(flash_devices); i++) {
176eb69ed7fSRuslan Bukin 		if ((flash_devices[i].manufacturer_id == manufacturer_id) &&
177eb69ed7fSRuslan Bukin 		    (flash_devices[i].device_id == dev_id))
178eb69ed7fSRuslan Bukin 			return &flash_devices[i];
179eb69ed7fSRuslan Bukin 	}
180eb69ed7fSRuslan Bukin 
181eb69ed7fSRuslan Bukin 	printf("Unknown SPI flash device. Vendor: %02x, device id: %04x\n",
182eb69ed7fSRuslan Bukin 	    manufacturer_id, dev_id);
183eb69ed7fSRuslan Bukin 
184eb69ed7fSRuslan Bukin 	return (NULL);
185eb69ed7fSRuslan Bukin }
186eb69ed7fSRuslan Bukin 
187eb69ed7fSRuslan Bukin static int
n25q_write(device_t dev,struct bio * bp,off_t offset,caddr_t data,off_t count)188eb69ed7fSRuslan Bukin n25q_write(device_t dev, struct bio *bp, off_t offset, caddr_t data, off_t count)
189eb69ed7fSRuslan Bukin {
190eb69ed7fSRuslan Bukin 	device_t pdev;
191eb69ed7fSRuslan Bukin 	int err;
192eb69ed7fSRuslan Bukin 
193eb69ed7fSRuslan Bukin 	pdev = device_get_parent(dev);
194eb69ed7fSRuslan Bukin 
195eb69ed7fSRuslan Bukin 	dprintf("%s: offset 0x%llx count %lld bytes\n", __func__, offset, count);
196eb69ed7fSRuslan Bukin 
197eb69ed7fSRuslan Bukin 	err = QSPI_ERASE(pdev, dev, offset);
198eb69ed7fSRuslan Bukin 	if (err != 0) {
199eb69ed7fSRuslan Bukin 		return (err);
200eb69ed7fSRuslan Bukin 	}
201eb69ed7fSRuslan Bukin 
202eb69ed7fSRuslan Bukin 	err = QSPI_WRITE(pdev, dev, bp, offset, data, count);
203eb69ed7fSRuslan Bukin 
204eb69ed7fSRuslan Bukin 	return (err);
205eb69ed7fSRuslan Bukin }
206eb69ed7fSRuslan Bukin 
207eb69ed7fSRuslan Bukin static int
n25q_read(device_t dev,struct bio * bp,off_t offset,caddr_t data,off_t count)208eb69ed7fSRuslan Bukin n25q_read(device_t dev, struct bio *bp, off_t offset, caddr_t data, off_t count)
209eb69ed7fSRuslan Bukin {
210eb69ed7fSRuslan Bukin 	struct n25q_softc *sc;
211eb69ed7fSRuslan Bukin 	device_t pdev;
212eb69ed7fSRuslan Bukin 	int err;
213eb69ed7fSRuslan Bukin 
214eb69ed7fSRuslan Bukin 	pdev = device_get_parent(dev);
215eb69ed7fSRuslan Bukin 	sc = device_get_softc(dev);
216eb69ed7fSRuslan Bukin 
217eb69ed7fSRuslan Bukin 	dprintf("%s: offset 0x%llx count %lld bytes\n", __func__, offset, count);
218eb69ed7fSRuslan Bukin 
219eb69ed7fSRuslan Bukin 	/*
220eb69ed7fSRuslan Bukin 	 * Enforce the disk read sectorsize not the erase sectorsize.
221eb69ed7fSRuslan Bukin 	 * In this way, smaller read IO is possible,dramatically
222eb69ed7fSRuslan Bukin 	 * speeding up filesystem/geom_compress access.
223eb69ed7fSRuslan Bukin 	 */
224eb69ed7fSRuslan Bukin 	if (count % sc->sc_disk->d_sectorsize != 0
225eb69ed7fSRuslan Bukin 	    || offset % sc->sc_disk->d_sectorsize != 0) {
226eb69ed7fSRuslan Bukin 		printf("EIO\n");
227eb69ed7fSRuslan Bukin 		return (EIO);
228eb69ed7fSRuslan Bukin 	}
229eb69ed7fSRuslan Bukin 
230eb69ed7fSRuslan Bukin 	err = QSPI_READ(pdev, dev, bp, offset, data, count);
231eb69ed7fSRuslan Bukin 
232eb69ed7fSRuslan Bukin 	return (err);
233eb69ed7fSRuslan Bukin }
234eb69ed7fSRuslan Bukin 
235eb69ed7fSRuslan Bukin static int
n25q_set_4b_mode(device_t dev,uint8_t command)236eb69ed7fSRuslan Bukin n25q_set_4b_mode(device_t dev, uint8_t command)
237eb69ed7fSRuslan Bukin {
238eb69ed7fSRuslan Bukin 	device_t pdev;
239eb69ed7fSRuslan Bukin 	int err;
240eb69ed7fSRuslan Bukin 
241eb69ed7fSRuslan Bukin 	pdev = device_get_parent(dev);
242eb69ed7fSRuslan Bukin 
243eb69ed7fSRuslan Bukin 	err = QSPI_WRITE_REG(pdev, dev, command, 0, 0);
244eb69ed7fSRuslan Bukin 
245eb69ed7fSRuslan Bukin 	return (err);
246eb69ed7fSRuslan Bukin }
247eb69ed7fSRuslan Bukin 
248eb69ed7fSRuslan Bukin static int
n25q_probe(device_t dev)249eb69ed7fSRuslan Bukin n25q_probe(device_t dev)
250eb69ed7fSRuslan Bukin {
251eb69ed7fSRuslan Bukin 	int i;
252eb69ed7fSRuslan Bukin 
253eb69ed7fSRuslan Bukin 	if (!ofw_bus_status_okay(dev))
254eb69ed7fSRuslan Bukin 		return (ENXIO);
255eb69ed7fSRuslan Bukin 
256eb69ed7fSRuslan Bukin 	/* First try to match the compatible property to the compat_data */
257eb69ed7fSRuslan Bukin 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 1)
258eb69ed7fSRuslan Bukin 		goto found;
259eb69ed7fSRuslan Bukin 
260eb69ed7fSRuslan Bukin 	/*
261eb69ed7fSRuslan Bukin 	 * Next, try to find a compatible device using the names in the
262eb69ed7fSRuslan Bukin 	 * flash_devices structure
263eb69ed7fSRuslan Bukin 	 */
264eb69ed7fSRuslan Bukin 	for (i = 0; i < nitems(flash_devices); i++)
265eb69ed7fSRuslan Bukin 		if (ofw_bus_is_compatible(dev, flash_devices[i].name))
266eb69ed7fSRuslan Bukin 			goto found;
267eb69ed7fSRuslan Bukin 
268eb69ed7fSRuslan Bukin 	return (ENXIO);
269eb69ed7fSRuslan Bukin found:
270eb69ed7fSRuslan Bukin 	device_set_desc(dev, "Micron n25q");
271eb69ed7fSRuslan Bukin 
272eb69ed7fSRuslan Bukin 	return (0);
273eb69ed7fSRuslan Bukin }
274eb69ed7fSRuslan Bukin 
275eb69ed7fSRuslan Bukin static int
n25q_attach(device_t dev)276eb69ed7fSRuslan Bukin n25q_attach(device_t dev)
277eb69ed7fSRuslan Bukin {
278eb69ed7fSRuslan Bukin 	struct n25q_flash_ident *ident;
279eb69ed7fSRuslan Bukin 	struct n25q_softc *sc;
280eb69ed7fSRuslan Bukin 
281eb69ed7fSRuslan Bukin 	sc = device_get_softc(dev);
282eb69ed7fSRuslan Bukin 	sc->dev = dev;
283eb69ed7fSRuslan Bukin 
284eb69ed7fSRuslan Bukin 	N25Q_LOCK_INIT(sc);
285eb69ed7fSRuslan Bukin 
286eb69ed7fSRuslan Bukin 	ident = n25q_get_device_ident(sc);
287eb69ed7fSRuslan Bukin 	if (ident == NULL) {
288eb69ed7fSRuslan Bukin 		return (ENXIO);
289eb69ed7fSRuslan Bukin 	}
290eb69ed7fSRuslan Bukin 
291eb69ed7fSRuslan Bukin 	n25q_wait_for_device_ready(sc->dev);
292eb69ed7fSRuslan Bukin 
293eb69ed7fSRuslan Bukin 	sc->sc_disk = disk_alloc();
294eb69ed7fSRuslan Bukin 	sc->sc_disk->d_open = n25q_open;
295eb69ed7fSRuslan Bukin 	sc->sc_disk->d_close = n25q_close;
296eb69ed7fSRuslan Bukin 	sc->sc_disk->d_strategy = n25q_strategy;
297eb69ed7fSRuslan Bukin 	sc->sc_disk->d_getattr = n25q_getattr;
298eb69ed7fSRuslan Bukin 	sc->sc_disk->d_ioctl = n25q_ioctl;
299eb69ed7fSRuslan Bukin 	sc->sc_disk->d_name = "flash/qspi";
300eb69ed7fSRuslan Bukin 	sc->sc_disk->d_drv1 = sc;
301eb69ed7fSRuslan Bukin 	sc->sc_disk->d_maxsize = DFLTPHYS;
302eb69ed7fSRuslan Bukin 	sc->sc_disk->d_sectorsize = FLASH_SECTORSIZE;
303eb69ed7fSRuslan Bukin 	sc->sc_disk->d_mediasize = (ident->sectorsize * ident->sectorcount);
304eb69ed7fSRuslan Bukin 	sc->sc_disk->d_unit = device_get_unit(sc->dev);
305eb69ed7fSRuslan Bukin 	sc->sc_disk->d_dump = NULL;
306eb69ed7fSRuslan Bukin 	/* Sectorsize for erase operations */
307eb69ed7fSRuslan Bukin 	sc->sc_sectorsize =  ident->sectorsize;
308eb69ed7fSRuslan Bukin 	sc->sc_flags = ident->flags;
309eb69ed7fSRuslan Bukin 
310eb69ed7fSRuslan Bukin 	if (sc->sc_flags & FL_ENABLE_4B_ADDR)
311eb69ed7fSRuslan Bukin 		n25q_set_4b_mode(dev, CMD_ENTER_4B_MODE);
312eb69ed7fSRuslan Bukin 
313eb69ed7fSRuslan Bukin 	if (sc->sc_flags & FL_DISABLE_4B_ADDR)
314eb69ed7fSRuslan Bukin 		n25q_set_4b_mode(dev, CMD_EXIT_4B_MODE);
315eb69ed7fSRuslan Bukin 
316eb69ed7fSRuslan Bukin         /* NB: use stripesize to hold the erase/region size for RedBoot */
317eb69ed7fSRuslan Bukin 	sc->sc_disk->d_stripesize = ident->sectorsize;
318eb69ed7fSRuslan Bukin 
319eb69ed7fSRuslan Bukin 	disk_create(sc->sc_disk, DISK_VERSION);
320eb69ed7fSRuslan Bukin 	bioq_init(&sc->sc_bio_queue);
321eb69ed7fSRuslan Bukin 
322eb69ed7fSRuslan Bukin 	kproc_create(&n25q_task, sc, &sc->sc_p, 0, 0, "task: n25q flash");
323eb69ed7fSRuslan Bukin 	sc->sc_taskstate = TSTATE_RUNNING;
324eb69ed7fSRuslan Bukin 
325eb69ed7fSRuslan Bukin 	device_printf(sc->dev, "%s, sector %d bytes, %d sectors\n",
326eb69ed7fSRuslan Bukin 	    ident->name, ident->sectorsize, ident->sectorcount);
327eb69ed7fSRuslan Bukin 
328eb69ed7fSRuslan Bukin 	return (0);
329eb69ed7fSRuslan Bukin }
330eb69ed7fSRuslan Bukin 
331eb69ed7fSRuslan Bukin static int
n25q_detach(device_t dev)332eb69ed7fSRuslan Bukin n25q_detach(device_t dev)
333eb69ed7fSRuslan Bukin {
334eb69ed7fSRuslan Bukin 	struct n25q_softc *sc;
335eb69ed7fSRuslan Bukin 	int err;
336eb69ed7fSRuslan Bukin 
337eb69ed7fSRuslan Bukin 	sc = device_get_softc(dev);
338eb69ed7fSRuslan Bukin 	err = 0;
339eb69ed7fSRuslan Bukin 
340eb69ed7fSRuslan Bukin 	N25Q_LOCK(sc);
341eb69ed7fSRuslan Bukin 	if (sc->sc_taskstate == TSTATE_RUNNING) {
342eb69ed7fSRuslan Bukin 		sc->sc_taskstate = TSTATE_STOPPING;
343eb69ed7fSRuslan Bukin 		wakeup(sc);
344eb69ed7fSRuslan Bukin 		while (err == 0 && sc->sc_taskstate != TSTATE_STOPPED) {
345eb69ed7fSRuslan Bukin 			err = msleep(sc, &sc->sc_mtx, 0, "n25q", hz * 3);
346eb69ed7fSRuslan Bukin 			if (err != 0) {
347eb69ed7fSRuslan Bukin 				sc->sc_taskstate = TSTATE_RUNNING;
348eb69ed7fSRuslan Bukin 				device_printf(sc->dev,
349eb69ed7fSRuslan Bukin 				    "Failed to stop queue task\n");
350eb69ed7fSRuslan Bukin 			}
351eb69ed7fSRuslan Bukin 		}
352eb69ed7fSRuslan Bukin 	}
353eb69ed7fSRuslan Bukin 	N25Q_UNLOCK(sc);
354eb69ed7fSRuslan Bukin 
355eb69ed7fSRuslan Bukin 	if (err == 0 && sc->sc_taskstate == TSTATE_STOPPED) {
356eb69ed7fSRuslan Bukin 		disk_destroy(sc->sc_disk);
357eb69ed7fSRuslan Bukin 		bioq_flush(&sc->sc_bio_queue, NULL, ENXIO);
358eb69ed7fSRuslan Bukin 		N25Q_LOCK_DESTROY(sc);
359eb69ed7fSRuslan Bukin 	}
360eb69ed7fSRuslan Bukin 	return (err);
361eb69ed7fSRuslan Bukin }
362eb69ed7fSRuslan Bukin 
363eb69ed7fSRuslan Bukin static int
n25q_open(struct disk * dp)364eb69ed7fSRuslan Bukin n25q_open(struct disk *dp)
365eb69ed7fSRuslan Bukin {
366eb69ed7fSRuslan Bukin 
367eb69ed7fSRuslan Bukin 	return (0);
368eb69ed7fSRuslan Bukin }
369eb69ed7fSRuslan Bukin 
370eb69ed7fSRuslan Bukin static int
n25q_close(struct disk * dp)371eb69ed7fSRuslan Bukin n25q_close(struct disk *dp)
372eb69ed7fSRuslan Bukin {
373eb69ed7fSRuslan Bukin 
374eb69ed7fSRuslan Bukin 	return (0);
375eb69ed7fSRuslan Bukin }
376eb69ed7fSRuslan Bukin 
377eb69ed7fSRuslan Bukin static int
n25q_ioctl(struct disk * dp,u_long cmd,void * data,int fflag,struct thread * td)378eb69ed7fSRuslan Bukin n25q_ioctl(struct disk *dp, u_long cmd, void *data,
379eb69ed7fSRuslan Bukin     int fflag, struct thread *td)
380eb69ed7fSRuslan Bukin {
381eb69ed7fSRuslan Bukin 
382eb69ed7fSRuslan Bukin 	return (EINVAL);
383eb69ed7fSRuslan Bukin }
384eb69ed7fSRuslan Bukin 
385eb69ed7fSRuslan Bukin static void
n25q_strategy(struct bio * bp)386eb69ed7fSRuslan Bukin n25q_strategy(struct bio *bp)
387eb69ed7fSRuslan Bukin {
388eb69ed7fSRuslan Bukin 	struct n25q_softc *sc;
389eb69ed7fSRuslan Bukin 
390eb69ed7fSRuslan Bukin 	sc = (struct n25q_softc *)bp->bio_disk->d_drv1;
391eb69ed7fSRuslan Bukin 
392eb69ed7fSRuslan Bukin 	N25Q_LOCK(sc);
393eb69ed7fSRuslan Bukin 	bioq_disksort(&sc->sc_bio_queue, bp);
394eb69ed7fSRuslan Bukin 	wakeup(sc);
395eb69ed7fSRuslan Bukin 	N25Q_UNLOCK(sc);
396eb69ed7fSRuslan Bukin }
397eb69ed7fSRuslan Bukin 
398eb69ed7fSRuslan Bukin static int
n25q_getattr(struct bio * bp)399eb69ed7fSRuslan Bukin n25q_getattr(struct bio *bp)
400eb69ed7fSRuslan Bukin {
401eb69ed7fSRuslan Bukin 	struct n25q_softc *sc;
402eb69ed7fSRuslan Bukin 	device_t dev;
403eb69ed7fSRuslan Bukin 
404eb69ed7fSRuslan Bukin 	if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL) {
405eb69ed7fSRuslan Bukin 		return (ENXIO);
406eb69ed7fSRuslan Bukin 	}
407eb69ed7fSRuslan Bukin 
408eb69ed7fSRuslan Bukin 	sc = bp->bio_disk->d_drv1;
409eb69ed7fSRuslan Bukin 	dev = sc->dev;
410eb69ed7fSRuslan Bukin 
411eb69ed7fSRuslan Bukin 	if (strcmp(bp->bio_attribute, "SPI::device") == 0) {
412eb69ed7fSRuslan Bukin 		if (bp->bio_length != sizeof(dev)) {
413eb69ed7fSRuslan Bukin 			return (EFAULT);
414eb69ed7fSRuslan Bukin 		}
415eb69ed7fSRuslan Bukin 		bcopy(&dev, bp->bio_data, sizeof(dev));
416eb69ed7fSRuslan Bukin 		return (0);
417eb69ed7fSRuslan Bukin 	}
418eb69ed7fSRuslan Bukin 
419eb69ed7fSRuslan Bukin 	return (-1);
420eb69ed7fSRuslan Bukin }
421eb69ed7fSRuslan Bukin 
422eb69ed7fSRuslan Bukin static void
n25q_task(void * arg)423eb69ed7fSRuslan Bukin n25q_task(void *arg)
424eb69ed7fSRuslan Bukin {
425eb69ed7fSRuslan Bukin 	struct n25q_softc *sc;
426eb69ed7fSRuslan Bukin 	struct bio *bp;
427eb69ed7fSRuslan Bukin 	device_t dev;
428eb69ed7fSRuslan Bukin 
429eb69ed7fSRuslan Bukin 	sc = (struct n25q_softc *)arg;
430eb69ed7fSRuslan Bukin 
431eb69ed7fSRuslan Bukin 	dev = sc->dev;
432eb69ed7fSRuslan Bukin 
433eb69ed7fSRuslan Bukin 	for (;;) {
434eb69ed7fSRuslan Bukin 		N25Q_LOCK(sc);
435eb69ed7fSRuslan Bukin 		do {
436eb69ed7fSRuslan Bukin 			if (sc->sc_taskstate == TSTATE_STOPPING) {
437eb69ed7fSRuslan Bukin 				sc->sc_taskstate = TSTATE_STOPPED;
438eb69ed7fSRuslan Bukin 				N25Q_UNLOCK(sc);
439eb69ed7fSRuslan Bukin 				wakeup(sc);
440eb69ed7fSRuslan Bukin 				kproc_exit(0);
441eb69ed7fSRuslan Bukin 			}
442eb69ed7fSRuslan Bukin 			bp = bioq_first(&sc->sc_bio_queue);
443eb69ed7fSRuslan Bukin 			if (bp == NULL)
444eb69ed7fSRuslan Bukin 				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", hz);
445eb69ed7fSRuslan Bukin 		} while (bp == NULL);
446eb69ed7fSRuslan Bukin 		bioq_remove(&sc->sc_bio_queue, bp);
447eb69ed7fSRuslan Bukin 		N25Q_UNLOCK(sc);
448eb69ed7fSRuslan Bukin 
449eb69ed7fSRuslan Bukin 		switch (bp->bio_cmd) {
450eb69ed7fSRuslan Bukin 		case BIO_READ:
451eb69ed7fSRuslan Bukin 			bp->bio_error = n25q_read(dev, bp, bp->bio_offset,
452eb69ed7fSRuslan Bukin 			    bp->bio_data, bp->bio_bcount);
453eb69ed7fSRuslan Bukin 			break;
454eb69ed7fSRuslan Bukin 		case BIO_WRITE:
455eb69ed7fSRuslan Bukin 			bp->bio_error = n25q_write(dev, bp, bp->bio_offset,
456eb69ed7fSRuslan Bukin 			    bp->bio_data, bp->bio_bcount);
457eb69ed7fSRuslan Bukin 			break;
458eb69ed7fSRuslan Bukin 		default:
459d176b803SScott Long 			bp->bio_error = EOPNOTSUPP;
460eb69ed7fSRuslan Bukin 		}
461eb69ed7fSRuslan Bukin 
462eb69ed7fSRuslan Bukin 		biodone(bp);
463eb69ed7fSRuslan Bukin 	}
464eb69ed7fSRuslan Bukin }
465eb69ed7fSRuslan Bukin 
466eb69ed7fSRuslan Bukin static device_method_t n25q_methods[] = {
467eb69ed7fSRuslan Bukin 	/* Device interface */
468eb69ed7fSRuslan Bukin 	DEVMETHOD(device_probe,		n25q_probe),
469eb69ed7fSRuslan Bukin 	DEVMETHOD(device_attach,	n25q_attach),
470eb69ed7fSRuslan Bukin 	DEVMETHOD(device_detach,	n25q_detach),
471eb69ed7fSRuslan Bukin 
472eb69ed7fSRuslan Bukin 	{ 0, 0 }
473eb69ed7fSRuslan Bukin };
474eb69ed7fSRuslan Bukin 
475eb69ed7fSRuslan Bukin static driver_t n25q_driver = {
476eb69ed7fSRuslan Bukin 	"n25q",
477eb69ed7fSRuslan Bukin 	n25q_methods,
478eb69ed7fSRuslan Bukin 	sizeof(struct n25q_softc),
479eb69ed7fSRuslan Bukin };
480eb69ed7fSRuslan Bukin 
481*0d95fe04SJohn Baldwin DRIVER_MODULE(n25q, simplebus, n25q_driver, 0, 0);
482