xref: /freebsd/sys/dev/flash/mx25l.c (revision 145992504973bd16cf3518af9ba5ce185fefa82a)
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 void mx25l_task(void *arg);
97 
98 struct mx25l_flash_ident flash_devices[] = {
99 	{ "en25f32",	0x1c, 0x3116, 64 * 1024, 64, FL_NONE },
100 	{ "en25p32",	0x1c, 0x2016, 64 * 1024, 64, FL_NONE },
101 	{ "en25p64",	0x1c, 0x2017, 64 * 1024, 128, FL_NONE },
102 	{ "en25q64",	0x1c, 0x3017, 64 * 1024, 128, FL_ERASE_4K },
103 	{ "m25p64",	0x20, 0x2017, 64 * 1024, 128, FL_NONE },
104 	{ "mx25ll32",	0xc2, 0x2016, 64 * 1024, 64, FL_NONE },
105 	{ "mx25ll64",	0xc2, 0x2017, 64 * 1024, 128, FL_NONE },
106 	{ "mx25ll128",	0xc2, 0x2018, 64 * 1024, 256, FL_ERASE_4K | FL_ERASE_32K },
107 	{ "s25fl128",	0x01, 0x2018, 64 * 1024, 256, FL_NONE },
108 	{ "s25s1032",	0x01, 0x0215, 64 * 1024, 64, FL_NONE },
109 	{ "s25sl064a",	0x01, 0x0216, 64 * 1024, 128, FL_NONE },
110 	{ "SST25VF032B", 0xbf, 0x254a, 64 * 1024, 64, FL_ERASE_4K | FL_ERASE_32K },
111 	{ "w25q32",	0xef, 0x4016, 64 * 1024, 64, FL_ERASE_4K },
112 	{ "w25q64",	0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
113 	{ "w25q64bv",	0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
114 	{ "w25x32",	0xef, 0x3016, 64 * 1024, 64, FL_ERASE_4K },
115 };
116 
117 static uint8_t
118 mx25l_get_status(device_t dev)
119 {
120 	uint8_t txBuf[2], rxBuf[2];
121 	struct spi_command cmd;
122 	int err;
123 
124 	memset(&cmd, 0, sizeof(cmd));
125 	memset(txBuf, 0, sizeof(txBuf));
126 	memset(rxBuf, 0, sizeof(rxBuf));
127 
128 	txBuf[0] = CMD_READ_STATUS;
129 	cmd.tx_cmd = txBuf;
130 	cmd.rx_cmd = rxBuf;
131 	cmd.rx_cmd_sz = 2;
132 	cmd.tx_cmd_sz = 2;
133 	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
134 	return (rxBuf[1]);
135 }
136 
137 static void
138 mx25l_wait_for_device_ready(device_t dev)
139 {
140 	while ((mx25l_get_status(dev) & STATUS_WIP))
141 		continue;
142 }
143 
144 static struct mx25l_flash_ident*
145 mx25l_get_device_ident(struct mx25l_softc *sc)
146 {
147 	device_t dev = sc->sc_dev;
148 	uint8_t txBuf[8], rxBuf[8];
149 	struct spi_command cmd;
150 	uint8_t manufacturer_id;
151 	uint16_t dev_id;
152 	int err, i;
153 
154 	memset(&cmd, 0, sizeof(cmd));
155 	memset(txBuf, 0, sizeof(txBuf));
156 	memset(rxBuf, 0, sizeof(rxBuf));
157 
158 	txBuf[0] = CMD_READ_IDENT;
159 	cmd.tx_cmd = &txBuf;
160 	cmd.rx_cmd = &rxBuf;
161 	/*
162 	 * Some compatible devices has extended two-bytes ID
163 	 * We'll use only manufacturer/deviceid atm
164 	 */
165 	cmd.tx_cmd_sz = 4;
166 	cmd.rx_cmd_sz = 4;
167 	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
168 	if (err)
169 		return (NULL);
170 
171 	manufacturer_id = rxBuf[1];
172 	dev_id = (rxBuf[2] << 8) | (rxBuf[3]);
173 
174 	for (i = 0;
175 	    i < sizeof(flash_devices)/sizeof(struct mx25l_flash_ident); i++) {
176 		if ((flash_devices[i].manufacturer_id == manufacturer_id) &&
177 		    (flash_devices[i].device_id == dev_id))
178 			return &flash_devices[i];
179 	}
180 
181 	printf("Unknown SPI flash device. Vendor: %02x, device id: %04x\n",
182 	    manufacturer_id, dev_id);
183 	return (NULL);
184 }
185 
186 static void
187 mx25l_set_writable(device_t dev, int writable)
188 {
189 	uint8_t txBuf[1], rxBuf[1];
190 	struct spi_command cmd;
191 	int err;
192 
193 	memset(&cmd, 0, sizeof(cmd));
194 	memset(txBuf, 0, sizeof(txBuf));
195 	memset(rxBuf, 0, sizeof(rxBuf));
196 
197 	txBuf[0] = writable ? CMD_WRITE_ENABLE : CMD_WRITE_DISABLE;
198 	cmd.tx_cmd = txBuf;
199 	cmd.rx_cmd = rxBuf;
200 	cmd.rx_cmd_sz = 1;
201 	cmd.tx_cmd_sz = 1;
202 	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
203 }
204 
205 static void
206 mx25l_erase_cmd(device_t dev, off_t sector, uint8_t ecmd)
207 {
208 	uint8_t txBuf[4], rxBuf[4];
209 	struct spi_command cmd;
210 	int err;
211 
212 	mx25l_wait_for_device_ready(dev);
213 	mx25l_set_writable(dev, 1);
214 
215 	memset(&cmd, 0, sizeof(cmd));
216 	memset(txBuf, 0, sizeof(txBuf));
217 	memset(rxBuf, 0, sizeof(rxBuf));
218 
219 	txBuf[0] = ecmd;
220 	cmd.tx_cmd = txBuf;
221 	cmd.rx_cmd = rxBuf;
222 	cmd.rx_cmd_sz = 4;
223 	cmd.tx_cmd_sz = 4;
224 	txBuf[1] = ((sector >> 16) & 0xff);
225 	txBuf[2] = ((sector >> 8) & 0xff);
226 	txBuf[3] = (sector & 0xff);
227 	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
228 }
229 
230 static int
231 mx25l_write(device_t dev, off_t offset, caddr_t data, off_t count)
232 {
233 	struct mx25l_softc *sc;
234 	uint8_t txBuf[8], rxBuf[8];
235 	struct spi_command cmd;
236 	off_t write_offset;
237 	long bytes_to_write, bytes_writen;
238 	device_t pdev;
239 	int err = 0;
240 
241 	pdev = device_get_parent(dev);
242 	sc = device_get_softc(dev);
243 
244 	cmd.tx_cmd_sz = 4;
245 	cmd.rx_cmd_sz = 4;
246 
247 	bytes_writen = 0;
248 	write_offset = offset;
249 
250 	/*
251 	 * Use the erase sectorsize here since blocks are fully erased
252 	 * first before they're written to.
253 	 */
254 	if (count % sc->sc_sectorsize != 0 || offset % sc->sc_sectorsize != 0)
255 		return (EIO);
256 
257 	/*
258 	 * Assume here that we write per-sector only
259 	 * and sector size should be 256 bytes aligned
260 	 */
261 	KASSERT(write_offset % FLASH_PAGE_SIZE == 0,
262 	    ("offset for BIO_WRITE is not page size (%d bytes) aligned",
263 		FLASH_PAGE_SIZE));
264 
265 	/*
266 	 * Maximum write size for CMD_PAGE_PROGRAM is
267 	 * FLASH_PAGE_SIZE, so split data to chunks
268 	 * FLASH_PAGE_SIZE bytes eash and write them
269 	 * one by one
270 	 */
271 	while (bytes_writen < count) {
272 		/*
273 		 * If we crossed sector boundary - erase next sector
274 		 */
275 		if (((offset + bytes_writen) % sc->sc_sectorsize) == 0)
276 			mx25l_erase_cmd(dev, offset + bytes_writen, CMD_SECTOR_ERASE);
277 
278 		txBuf[0] = CMD_PAGE_PROGRAM;
279 		txBuf[1] = ((write_offset >> 16) & 0xff);
280 		txBuf[2] = ((write_offset >> 8) & 0xff);
281 		txBuf[3] = (write_offset & 0xff);
282 
283 		bytes_to_write = MIN(FLASH_PAGE_SIZE,
284 		    count - bytes_writen);
285 		cmd.tx_cmd = txBuf;
286 		cmd.rx_cmd = rxBuf;
287 		cmd.tx_data = data + bytes_writen;
288 		cmd.tx_data_sz = bytes_to_write;
289 		cmd.rx_data = data + bytes_writen;
290 		cmd.rx_data_sz = bytes_to_write;
291 
292 		/*
293 		 * Eash completed write operation resets WEL
294 		 * (write enable latch) to disabled state,
295 		 * so we re-enable it here
296 		 */
297 		mx25l_wait_for_device_ready(dev);
298 		mx25l_set_writable(dev, 1);
299 
300 		err = SPIBUS_TRANSFER(pdev, dev, &cmd);
301 		if (err)
302 			break;
303 
304 		bytes_writen += bytes_to_write;
305 		write_offset += bytes_to_write;
306 	}
307 
308 	return (err);
309 }
310 
311 static int
312 mx25l_read(device_t dev, off_t offset, caddr_t data, off_t count)
313 {
314 	struct mx25l_softc *sc;
315 	uint8_t txBuf[8], rxBuf[8];
316 	struct spi_command cmd;
317 	device_t pdev;
318 	int err = 0;
319 
320 	pdev = device_get_parent(dev);
321 	sc = device_get_softc(dev);
322 
323 	/*
324 	 * Enforce the disk read sectorsize not the erase sectorsize.
325 	 * In this way, smaller read IO is possible,dramatically
326 	 * speeding up filesystem/geom_compress access.
327 	 */
328 	if (count % sc->sc_disk->d_sectorsize != 0
329 	    || offset % sc->sc_disk->d_sectorsize != 0)
330 		return (EIO);
331 
332 	txBuf[0] = CMD_FAST_READ;
333 	cmd.tx_cmd_sz = 5;
334 	cmd.rx_cmd_sz = 5;
335 
336 	txBuf[1] = ((offset >> 16) & 0xff);
337 	txBuf[2] = ((offset >> 8) & 0xff);
338 	txBuf[3] = (offset & 0xff);
339 	/* Dummy byte */
340 	txBuf[4] = 0;
341 
342 	cmd.tx_cmd = txBuf;
343 	cmd.rx_cmd = rxBuf;
344 	cmd.tx_data = data;
345 	cmd.tx_data_sz = count;
346 	cmd.rx_data = data;
347 	cmd.rx_data_sz = count;
348 
349 	err = SPIBUS_TRANSFER(pdev, dev, &cmd);
350 
351 	return (err);
352 }
353 
354 static int
355 mx25l_probe(device_t dev)
356 {
357 	device_set_desc(dev, "M25Pxx Flash Family");
358 	return (0);
359 }
360 
361 static int
362 mx25l_attach(device_t dev)
363 {
364 	struct mx25l_softc *sc;
365 	struct mx25l_flash_ident *ident;
366 
367 	sc = device_get_softc(dev);
368 	sc->sc_dev = dev;
369 	M25PXX_LOCK_INIT(sc);
370 
371 	ident = mx25l_get_device_ident(sc);
372 	if (ident == NULL)
373 		return (ENXIO);
374 
375 	mx25l_wait_for_device_ready(sc->sc_dev);
376 
377 	sc->sc_disk = disk_alloc();
378 	sc->sc_disk->d_open = mx25l_open;
379 	sc->sc_disk->d_close = mx25l_close;
380 	sc->sc_disk->d_strategy = mx25l_strategy;
381 	sc->sc_disk->d_ioctl = mx25l_ioctl;
382 	sc->sc_disk->d_name = "flash/spi";
383 	sc->sc_disk->d_drv1 = sc;
384 	sc->sc_disk->d_maxsize = DFLTPHYS;
385 	sc->sc_disk->d_sectorsize = MX25L_SECTORSIZE;
386 	sc->sc_disk->d_mediasize = ident->sectorsize * ident->sectorcount;
387 	sc->sc_disk->d_unit = device_get_unit(sc->sc_dev);
388 	sc->sc_disk->d_dump = NULL;		/* NB: no dumps */
389 	/* Sectorsize for erase operations */
390 	sc->sc_sectorsize =  ident->sectorsize;
391 	sc->sc_flags = ident->flags;
392 
393         /* NB: use stripesize to hold the erase/region size for RedBoot */
394 	sc->sc_disk->d_stripesize = ident->sectorsize;
395 
396 	disk_create(sc->sc_disk, DISK_VERSION);
397 	bioq_init(&sc->sc_bio_queue);
398 
399 	kproc_create(&mx25l_task, sc, &sc->sc_p, 0, 0, "task: mx25l flash");
400 	device_printf(sc->sc_dev, "%s, sector %d bytes, %d sectors\n",
401 	    ident->name, ident->sectorsize, ident->sectorcount);
402 
403 	return (0);
404 }
405 
406 static int
407 mx25l_detach(device_t dev)
408 {
409 
410 	return (EIO);
411 }
412 
413 static int
414 mx25l_open(struct disk *dp)
415 {
416 	return (0);
417 }
418 
419 static int
420 mx25l_close(struct disk *dp)
421 {
422 
423 	return (0);
424 }
425 
426 static int
427 mx25l_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
428 	struct thread *td)
429 {
430 
431 	return (EINVAL);
432 }
433 
434 static void
435 mx25l_strategy(struct bio *bp)
436 {
437 	struct mx25l_softc *sc;
438 
439 	sc = (struct mx25l_softc *)bp->bio_disk->d_drv1;
440 	M25PXX_LOCK(sc);
441 	bioq_disksort(&sc->sc_bio_queue, bp);
442 	wakeup(sc);
443 	M25PXX_UNLOCK(sc);
444 }
445 
446 static void
447 mx25l_task(void *arg)
448 {
449 	struct mx25l_softc *sc = (struct mx25l_softc*)arg;
450 	struct bio *bp;
451 	device_t dev;
452 
453 	for (;;) {
454 		dev = sc->sc_dev;
455 		M25PXX_LOCK(sc);
456 		do {
457 			bp = bioq_first(&sc->sc_bio_queue);
458 			if (bp == NULL)
459 				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
460 		} while (bp == NULL);
461 		bioq_remove(&sc->sc_bio_queue, bp);
462 		M25PXX_UNLOCK(sc);
463 
464 		switch (bp->bio_cmd) {
465 		case BIO_READ:
466 			bp->bio_error = mx25l_read(dev, bp->bio_offset,
467 			    bp->bio_data, bp->bio_bcount);
468 			break;
469 		case BIO_WRITE:
470 			bp->bio_error = mx25l_write(dev, bp->bio_offset,
471 			    bp->bio_data, bp->bio_bcount);
472 			break;
473 		default:
474 			bp->bio_error = EINVAL;
475 		}
476 
477 
478 		biodone(bp);
479 	}
480 }
481 
482 static devclass_t mx25l_devclass;
483 
484 static device_method_t mx25l_methods[] = {
485 	/* Device interface */
486 	DEVMETHOD(device_probe,		mx25l_probe),
487 	DEVMETHOD(device_attach,	mx25l_attach),
488 	DEVMETHOD(device_detach,	mx25l_detach),
489 
490 	{ 0, 0 }
491 };
492 
493 static driver_t mx25l_driver = {
494 	"mx25l",
495 	mx25l_methods,
496 	sizeof(struct mx25l_softc),
497 };
498 
499 DRIVER_MODULE(mx25l, spibus, mx25l_driver, mx25l_devclass, 0, 0);
500