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