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