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