xref: /freebsd/sys/dev/flash/mx25l.c (revision 0d95fe04c65c371dab9ed7039440b40720e79559)
1cd5bdf03SOleksandr Tymoshenko /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
4cd5bdf03SOleksandr Tymoshenko  * Copyright (c) 2009 Oleksandr Tymoshenko.  All rights reserved.
5d8920513SIan Lepore  * Copyright (c) 2018 Ian Lepore.  All rights reserved.
6f86e6000SWarner Losh  * Copyright (c) 2006 M. Warner Losh <imp@FreeBSD.org>
7cd5bdf03SOleksandr Tymoshenko  *
8cd5bdf03SOleksandr Tymoshenko  * Redistribution and use in source and binary forms, with or without
9cd5bdf03SOleksandr Tymoshenko  * modification, are permitted provided that the following conditions
10cd5bdf03SOleksandr Tymoshenko  * are met:
11cd5bdf03SOleksandr Tymoshenko  * 1. Redistributions of source code must retain the above copyright
12cd5bdf03SOleksandr Tymoshenko  *    notice, this list of conditions and the following disclaimer.
13cd5bdf03SOleksandr Tymoshenko  * 2. Redistributions in binary form must reproduce the above copyright
14cd5bdf03SOleksandr Tymoshenko  *    notice, this list of conditions and the following disclaimer in the
15cd5bdf03SOleksandr Tymoshenko  *    documentation and/or other materials provided with the distribution.
16cd5bdf03SOleksandr Tymoshenko  *
17cd5bdf03SOleksandr Tymoshenko  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18cd5bdf03SOleksandr Tymoshenko  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19cd5bdf03SOleksandr Tymoshenko  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20cd5bdf03SOleksandr Tymoshenko  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21cd5bdf03SOleksandr Tymoshenko  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22cd5bdf03SOleksandr Tymoshenko  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23cd5bdf03SOleksandr Tymoshenko  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24cd5bdf03SOleksandr Tymoshenko  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25cd5bdf03SOleksandr Tymoshenko  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26cd5bdf03SOleksandr Tymoshenko  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27cd5bdf03SOleksandr Tymoshenko  */
28cd5bdf03SOleksandr Tymoshenko 
29cd5bdf03SOleksandr Tymoshenko #include <sys/cdefs.h>
30cd5bdf03SOleksandr Tymoshenko __FBSDID("$FreeBSD$");
31cd5bdf03SOleksandr Tymoshenko 
328f74c88aSLuiz Otavio O Souza #include "opt_platform.h"
338f74c88aSLuiz Otavio O Souza 
34cd5bdf03SOleksandr Tymoshenko #include <sys/param.h>
35cd5bdf03SOleksandr Tymoshenko #include <sys/systm.h>
36cd5bdf03SOleksandr Tymoshenko #include <sys/bio.h>
37cd5bdf03SOleksandr Tymoshenko #include <sys/bus.h>
38cd5bdf03SOleksandr Tymoshenko #include <sys/conf.h>
39cd5bdf03SOleksandr Tymoshenko #include <sys/kernel.h>
40cd5bdf03SOleksandr Tymoshenko #include <sys/kthread.h>
41cd5bdf03SOleksandr Tymoshenko #include <sys/lock.h>
42cd5bdf03SOleksandr Tymoshenko #include <sys/mbuf.h>
43cd5bdf03SOleksandr Tymoshenko #include <sys/malloc.h>
44cd5bdf03SOleksandr Tymoshenko #include <sys/module.h>
45cd5bdf03SOleksandr Tymoshenko #include <sys/mutex.h>
46cd5bdf03SOleksandr Tymoshenko #include <geom/geom_disk.h>
47cd5bdf03SOleksandr Tymoshenko 
488f74c88aSLuiz Otavio O Souza #ifdef FDT
498f74c88aSLuiz Otavio O Souza #include <dev/fdt/fdt_common.h>
508f74c88aSLuiz Otavio O Souza #include <dev/ofw/ofw_bus_subr.h>
518f74c88aSLuiz Otavio O Souza #include <dev/ofw/openfirm.h>
528f74c88aSLuiz Otavio O Souza #endif
538f74c88aSLuiz Otavio O Souza 
54cd5bdf03SOleksandr Tymoshenko #include <dev/spibus/spi.h>
55cd5bdf03SOleksandr Tymoshenko #include "spibus_if.h"
56cd5bdf03SOleksandr Tymoshenko 
57cd5bdf03SOleksandr Tymoshenko #include <dev/flash/mx25lreg.h>
58cd5bdf03SOleksandr Tymoshenko 
59d2cf1fd5SAdrian Chadd #define	FL_NONE			0x00
60d2cf1fd5SAdrian Chadd #define	FL_ERASE_4K		0x01
61d2cf1fd5SAdrian Chadd #define	FL_ERASE_32K		0x02
622d46b036SStanislav Galabov #define	FL_ENABLE_4B_ADDR	0x04
632d46b036SStanislav Galabov #define	FL_DISABLE_4B_ADDR	0x08
64d2cf1fd5SAdrian Chadd 
65917721a4SAdrian Chadd /*
66917721a4SAdrian Chadd  * Define the sectorsize to be a smaller size rather than the flash
67917721a4SAdrian Chadd  * sector size. Trying to run FFS off of a 64k flash sector size
68917721a4SAdrian Chadd  * results in a completely un-usable system.
69917721a4SAdrian Chadd  */
70917721a4SAdrian Chadd #define	MX25L_SECTORSIZE	512
71917721a4SAdrian Chadd 
72cd5bdf03SOleksandr Tymoshenko struct mx25l_flash_ident
73cd5bdf03SOleksandr Tymoshenko {
74cd5bdf03SOleksandr Tymoshenko 	const char	*name;
75cd5bdf03SOleksandr Tymoshenko 	uint8_t		manufacturer_id;
76cd5bdf03SOleksandr Tymoshenko 	uint16_t	device_id;
77cd5bdf03SOleksandr Tymoshenko 	unsigned int	sectorsize;
78cd5bdf03SOleksandr Tymoshenko 	unsigned int	sectorcount;
79d2cf1fd5SAdrian Chadd 	unsigned int	flags;
80cd5bdf03SOleksandr Tymoshenko };
81cd5bdf03SOleksandr Tymoshenko 
82cd5bdf03SOleksandr Tymoshenko struct mx25l_softc
83cd5bdf03SOleksandr Tymoshenko {
84cd5bdf03SOleksandr Tymoshenko 	device_t	sc_dev;
8589a1585bSIan Lepore 	device_t	sc_parent;
86cd5bdf03SOleksandr Tymoshenko 	uint8_t		sc_manufacturer_id;
87cd5bdf03SOleksandr Tymoshenko 	uint16_t	sc_device_id;
883c9af13cSIan Lepore 	unsigned int	sc_erasesize;
89cd5bdf03SOleksandr Tymoshenko 	struct mtx	sc_mtx;
90cd5bdf03SOleksandr Tymoshenko 	struct disk	*sc_disk;
91cd5bdf03SOleksandr Tymoshenko 	struct proc	*sc_p;
92cd5bdf03SOleksandr Tymoshenko 	struct bio_queue_head sc_bio_queue;
9362c3a412SAdrian Chadd 	unsigned int	sc_flags;
94aae18dccSIan Lepore 	unsigned int	sc_taskstate;
95dac94adbSIan Lepore 	uint8_t		sc_dummybuf[FLASH_PAGE_SIZE];
96cd5bdf03SOleksandr Tymoshenko };
97cd5bdf03SOleksandr Tymoshenko 
98aae18dccSIan Lepore #define	TSTATE_STOPPED	0
99aae18dccSIan Lepore #define	TSTATE_STOPPING	1
100aae18dccSIan Lepore #define	TSTATE_RUNNING	2
101aae18dccSIan Lepore 
102cd5bdf03SOleksandr Tymoshenko #define M25PXX_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
103cd5bdf03SOleksandr Tymoshenko #define	M25PXX_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
104cd5bdf03SOleksandr Tymoshenko #define M25PXX_LOCK_INIT(_sc) \
105cd5bdf03SOleksandr Tymoshenko 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
106cd5bdf03SOleksandr Tymoshenko 	    "mx25l", MTX_DEF)
107cd5bdf03SOleksandr Tymoshenko #define M25PXX_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
108cd5bdf03SOleksandr Tymoshenko #define M25PXX_ASSERT_LOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_OWNED);
109cd5bdf03SOleksandr Tymoshenko #define M25PXX_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
110cd5bdf03SOleksandr Tymoshenko 
111cd5bdf03SOleksandr Tymoshenko /* disk routines */
112cd5bdf03SOleksandr Tymoshenko static int mx25l_open(struct disk *dp);
113cd5bdf03SOleksandr Tymoshenko static int mx25l_close(struct disk *dp);
114cd5bdf03SOleksandr Tymoshenko static int mx25l_ioctl(struct disk *, u_long, void *, int, struct thread *);
115cd5bdf03SOleksandr Tymoshenko static void mx25l_strategy(struct bio *bp);
1161b468038SAdrian Chadd static int mx25l_getattr(struct bio *bp);
117cd5bdf03SOleksandr Tymoshenko static void mx25l_task(void *arg);
118cd5bdf03SOleksandr Tymoshenko 
1196dfd0500SIan Lepore static struct mx25l_flash_ident flash_devices[] = {
120869785b4SAleksandr Rybalko 	{ "en25f32",	0x1c, 0x3116, 64 * 1024, 64, FL_NONE },
121869785b4SAleksandr Rybalko 	{ "en25p32",	0x1c, 0x2016, 64 * 1024, 64, FL_NONE },
122869785b4SAleksandr Rybalko 	{ "en25p64",	0x1c, 0x2017, 64 * 1024, 128, FL_NONE },
1232f57841eSAdrian Chadd 	{ "en25q32",	0x1c, 0x3016, 64 * 1024, 64, FL_NONE },
124869785b4SAleksandr Rybalko 	{ "en25q64",	0x1c, 0x3017, 64 * 1024, 128, FL_ERASE_4K },
1252f57841eSAdrian Chadd 	{ "m25p32",	0x20, 0x2016, 64 * 1024, 64, FL_NONE },
126a565668bSAdrian Chadd 	{ "m25p64",	0x20, 0x2017, 64 * 1024, 128, FL_NONE },
127bb5125a4SEmmanuel Vadot 	{ "mx25l1606e", 0xc2, 0x2015, 64 * 1024, 32, FL_ERASE_4K},
128869785b4SAleksandr Rybalko 	{ "mx25ll32",	0xc2, 0x2016, 64 * 1024, 64, FL_NONE },
129d2cf1fd5SAdrian Chadd 	{ "mx25ll64",	0xc2, 0x2017, 64 * 1024, 128, FL_NONE },
130d2cf1fd5SAdrian Chadd 	{ "mx25ll128",	0xc2, 0x2018, 64 * 1024, 256, FL_ERASE_4K | FL_ERASE_32K },
1312d46b036SStanislav Galabov 	{ "mx25ll256",	0xc2, 0x2019, 64 * 1024, 512, FL_ERASE_4K | FL_ERASE_32K | FL_ENABLE_4B_ADDR },
132c6905524SLuiz Otavio O Souza 	{ "s25fl032",	0x01, 0x0215, 64 * 1024, 64, FL_NONE },
133c6905524SLuiz Otavio O Souza 	{ "s25fl064",	0x01, 0x0216, 64 * 1024, 128, FL_NONE },
134d2cf1fd5SAdrian Chadd 	{ "s25fl128",	0x01, 0x2018, 64 * 1024, 256, FL_NONE },
1354a4ae982SAdrian Chadd 	{ "s25fl256s",	0x01, 0x0219, 64 * 1024, 512, FL_NONE },
1363f59a7f9SNick O'Brien 	{ "s25fl512s",	0x01, 0x0220, 64 * 1024, 1024, FL_NONE },
137fa68dcb1SJustin Hibbits 	{ "SST25VF010A", 0xbf, 0x2549, 4 * 1024, 32, FL_ERASE_4K | FL_ERASE_32K },
138869785b4SAleksandr Rybalko 	{ "SST25VF032B", 0xbf, 0x254a, 64 * 1024, 64, FL_ERASE_4K | FL_ERASE_32K },
13970234acbSAdrian Chadd 
14070234acbSAdrian Chadd 	/* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
14170234acbSAdrian Chadd 	{ "w25x32",	0xef, 0x3016, 64 * 1024, 64, FL_ERASE_4K },
14240424a25SAdrian Chadd 	{ "w25x64",	0xef, 0x3017, 64 * 1024, 128, FL_ERASE_4K },
143869785b4SAleksandr Rybalko 	{ "w25q32",	0xef, 0x4016, 64 * 1024, 64, FL_ERASE_4K },
144869785b4SAleksandr Rybalko 	{ "w25q64",	0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
145450a186cSAdrian Chadd 	{ "w25q64bv",	0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
14670234acbSAdrian Chadd 	{ "w25q128",	0xef, 0x4018, 64 * 1024, 256, FL_ERASE_4K },
14770234acbSAdrian Chadd 	{ "w25q256",	0xef, 0x4019, 64 * 1024, 512, FL_ERASE_4K },
148fe56b741SBaptiste Daroussin 
149fe56b741SBaptiste Daroussin 	 /* Atmel */
150fe56b741SBaptiste Daroussin 	{ "at25df641",  0x1f, 0x4800, 64 * 1024, 128, FL_ERASE_4K },
151303c3562SAdrian Chadd 
152303c3562SAdrian Chadd 	/* GigaDevice */
153303c3562SAdrian Chadd 	{ "gd25q64",	0xc8, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
154d1ecd3acSEmmanuel Vadot 	{ "gd25q128",	0xc8, 0x4018, 64 * 1024, 256, FL_ERASE_4K },
155416ac155SThomas Skibo 
156416ac155SThomas Skibo 	/* Integrated Silicon Solution */
157416ac155SThomas Skibo 	{ "is25wp256",	0x9d, 0x7019, 64 * 1024, 512, FL_ERASE_4K | FL_ENABLE_4B_ADDR},
158cd5bdf03SOleksandr Tymoshenko };
159cd5bdf03SOleksandr Tymoshenko 
160c03ab159SIan Lepore static int
161c03ab159SIan Lepore mx25l_wait_for_device_ready(struct mx25l_softc *sc)
162cd5bdf03SOleksandr Tymoshenko {
163cd5bdf03SOleksandr Tymoshenko 	uint8_t txBuf[2], rxBuf[2];
164cd5bdf03SOleksandr Tymoshenko 	struct spi_command cmd;
165cd5bdf03SOleksandr Tymoshenko 	int err;
166cd5bdf03SOleksandr Tymoshenko 
167cd5bdf03SOleksandr Tymoshenko 	memset(&cmd, 0, sizeof(cmd));
168cd5bdf03SOleksandr Tymoshenko 
169c03ab159SIan Lepore 	do {
170cd5bdf03SOleksandr Tymoshenko 		txBuf[0] = CMD_READ_STATUS;
171cd5bdf03SOleksandr Tymoshenko 		cmd.tx_cmd = txBuf;
172cd5bdf03SOleksandr Tymoshenko 		cmd.rx_cmd = rxBuf;
173cd5bdf03SOleksandr Tymoshenko 		cmd.rx_cmd_sz = 2;
174cd5bdf03SOleksandr Tymoshenko 		cmd.tx_cmd_sz = 2;
17589a1585bSIan Lepore 		err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
176c03ab159SIan Lepore 	} while (err == 0 && (rxBuf[1] & STATUS_WIP));
177cd5bdf03SOleksandr Tymoshenko 
178c03ab159SIan Lepore 	return (err);
179cd5bdf03SOleksandr Tymoshenko }
180cd5bdf03SOleksandr Tymoshenko 
181cd5bdf03SOleksandr Tymoshenko static struct mx25l_flash_ident*
182cd5bdf03SOleksandr Tymoshenko mx25l_get_device_ident(struct mx25l_softc *sc)
183cd5bdf03SOleksandr Tymoshenko {
184cd5bdf03SOleksandr Tymoshenko 	uint8_t txBuf[8], rxBuf[8];
185cd5bdf03SOleksandr Tymoshenko 	struct spi_command cmd;
186cd5bdf03SOleksandr Tymoshenko 	uint8_t manufacturer_id;
187cd5bdf03SOleksandr Tymoshenko 	uint16_t dev_id;
188cd5bdf03SOleksandr Tymoshenko 	int err, i;
189cd5bdf03SOleksandr Tymoshenko 
190cd5bdf03SOleksandr Tymoshenko 	memset(&cmd, 0, sizeof(cmd));
191cd5bdf03SOleksandr Tymoshenko 	memset(txBuf, 0, sizeof(txBuf));
192cd5bdf03SOleksandr Tymoshenko 	memset(rxBuf, 0, sizeof(rxBuf));
193cd5bdf03SOleksandr Tymoshenko 
194cd5bdf03SOleksandr Tymoshenko 	txBuf[0] = CMD_READ_IDENT;
195cd5bdf03SOleksandr Tymoshenko 	cmd.tx_cmd = &txBuf;
196cd5bdf03SOleksandr Tymoshenko 	cmd.rx_cmd = &rxBuf;
197cd5bdf03SOleksandr Tymoshenko 	/*
198cd5bdf03SOleksandr Tymoshenko 	 * Some compatible devices has extended two-bytes ID
199cd5bdf03SOleksandr Tymoshenko 	 * We'll use only manufacturer/deviceid atm
200cd5bdf03SOleksandr Tymoshenko 	 */
201cd5bdf03SOleksandr Tymoshenko 	cmd.tx_cmd_sz = 4;
202cd5bdf03SOleksandr Tymoshenko 	cmd.rx_cmd_sz = 4;
20389a1585bSIan Lepore 	err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
204cd5bdf03SOleksandr Tymoshenko 	if (err)
205cd5bdf03SOleksandr Tymoshenko 		return (NULL);
206cd5bdf03SOleksandr Tymoshenko 
207cd5bdf03SOleksandr Tymoshenko 	manufacturer_id = rxBuf[1];
208cd5bdf03SOleksandr Tymoshenko 	dev_id = (rxBuf[2] << 8) | (rxBuf[3]);
209cd5bdf03SOleksandr Tymoshenko 
210c03ab159SIan Lepore 	for (i = 0; i < nitems(flash_devices); i++) {
211cd5bdf03SOleksandr Tymoshenko 		if ((flash_devices[i].manufacturer_id == manufacturer_id) &&
212cd5bdf03SOleksandr Tymoshenko 		    (flash_devices[i].device_id == dev_id))
213cd5bdf03SOleksandr Tymoshenko 			return &flash_devices[i];
214cd5bdf03SOleksandr Tymoshenko 	}
215cd5bdf03SOleksandr Tymoshenko 
21689a1585bSIan Lepore 	device_printf(sc->sc_dev,
21789a1585bSIan Lepore 	    "Unknown SPI flash device. Vendor: %02x, device id: %04x\n",
218cd5bdf03SOleksandr Tymoshenko 	    manufacturer_id, dev_id);
219cd5bdf03SOleksandr Tymoshenko 	return (NULL);
220cd5bdf03SOleksandr Tymoshenko }
221cd5bdf03SOleksandr Tymoshenko 
222c03ab159SIan Lepore static int
22389a1585bSIan Lepore mx25l_set_writable(struct mx25l_softc *sc, int writable)
224c3655ab0SOleksandr Tymoshenko {
225c3655ab0SOleksandr Tymoshenko 	uint8_t txBuf[1], rxBuf[1];
226c3655ab0SOleksandr Tymoshenko 	struct spi_command cmd;
227c3655ab0SOleksandr Tymoshenko 	int err;
228c3655ab0SOleksandr Tymoshenko 
229c3655ab0SOleksandr Tymoshenko 	memset(&cmd, 0, sizeof(cmd));
230c3655ab0SOleksandr Tymoshenko 	memset(txBuf, 0, sizeof(txBuf));
231c3655ab0SOleksandr Tymoshenko 	memset(rxBuf, 0, sizeof(rxBuf));
232c3655ab0SOleksandr Tymoshenko 
233c3655ab0SOleksandr Tymoshenko 	txBuf[0] = writable ? CMD_WRITE_ENABLE : CMD_WRITE_DISABLE;
234c3655ab0SOleksandr Tymoshenko 	cmd.tx_cmd = txBuf;
235c3655ab0SOleksandr Tymoshenko 	cmd.rx_cmd = rxBuf;
236c3655ab0SOleksandr Tymoshenko 	cmd.rx_cmd_sz = 1;
237c3655ab0SOleksandr Tymoshenko 	cmd.tx_cmd_sz = 1;
23889a1585bSIan Lepore 	err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
239c03ab159SIan Lepore 	return (err);
240c3655ab0SOleksandr Tymoshenko }
241c3655ab0SOleksandr Tymoshenko 
242c03ab159SIan Lepore static int
2433c9af13cSIan Lepore mx25l_erase_cmd(struct mx25l_softc *sc, off_t sector)
244c3655ab0SOleksandr Tymoshenko {
2452d46b036SStanislav Galabov 	uint8_t txBuf[5], rxBuf[5];
246c3655ab0SOleksandr Tymoshenko 	struct spi_command cmd;
247c3655ab0SOleksandr Tymoshenko 	int err;
248c3655ab0SOleksandr Tymoshenko 
249c03ab159SIan Lepore 	if ((err = mx25l_set_writable(sc, 1)) != 0)
250c03ab159SIan Lepore 		return (err);
251c3655ab0SOleksandr Tymoshenko 
252c3655ab0SOleksandr Tymoshenko 	memset(&cmd, 0, sizeof(cmd));
253c3655ab0SOleksandr Tymoshenko 	memset(txBuf, 0, sizeof(txBuf));
254c3655ab0SOleksandr Tymoshenko 	memset(rxBuf, 0, sizeof(rxBuf));
255c3655ab0SOleksandr Tymoshenko 
256c3655ab0SOleksandr Tymoshenko 	cmd.tx_cmd = txBuf;
257c3655ab0SOleksandr Tymoshenko 	cmd.rx_cmd = rxBuf;
2583c9af13cSIan Lepore 
2593c9af13cSIan Lepore 	if (sc->sc_flags & FL_ERASE_4K)
2603c9af13cSIan Lepore 		txBuf[0] = CMD_BLOCK_4K_ERASE;
2613c9af13cSIan Lepore 	else if (sc->sc_flags & FL_ERASE_32K)
2623c9af13cSIan Lepore 		txBuf[0] = CMD_BLOCK_32K_ERASE;
2633c9af13cSIan Lepore 	else
2643c9af13cSIan Lepore 		txBuf[0] = CMD_SECTOR_ERASE;
2653c9af13cSIan Lepore 
2662d46b036SStanislav Galabov 	if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
2672d46b036SStanislav Galabov 		cmd.rx_cmd_sz = 5;
2682d46b036SStanislav Galabov 		cmd.tx_cmd_sz = 5;
2692d46b036SStanislav Galabov 		txBuf[1] = ((sector >> 24) & 0xff);
2702d46b036SStanislav Galabov 		txBuf[2] = ((sector >> 16) & 0xff);
2712d46b036SStanislav Galabov 		txBuf[3] = ((sector >> 8) & 0xff);
2722d46b036SStanislav Galabov 		txBuf[4] = (sector & 0xff);
2732d46b036SStanislav Galabov 	} else {
274c3655ab0SOleksandr Tymoshenko 		cmd.rx_cmd_sz = 4;
275c3655ab0SOleksandr Tymoshenko 		cmd.tx_cmd_sz = 4;
276c3655ab0SOleksandr Tymoshenko 		txBuf[1] = ((sector >> 16) & 0xff);
277c3655ab0SOleksandr Tymoshenko 		txBuf[2] = ((sector >> 8) & 0xff);
278c3655ab0SOleksandr Tymoshenko 		txBuf[3] = (sector & 0xff);
2792d46b036SStanislav Galabov 	}
280c03ab159SIan Lepore 	if ((err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd)) != 0)
281c03ab159SIan Lepore 		return (err);
282c03ab159SIan Lepore 	err = mx25l_wait_for_device_ready(sc);
283c03ab159SIan Lepore 	return (err);
284c3655ab0SOleksandr Tymoshenko }
285c3655ab0SOleksandr Tymoshenko 
286cd5bdf03SOleksandr Tymoshenko static int
28789a1585bSIan Lepore mx25l_write(struct mx25l_softc *sc, off_t offset, caddr_t data, off_t count)
288b3918833SOleksandr Tymoshenko {
289b3918833SOleksandr Tymoshenko 	uint8_t txBuf[8], rxBuf[8];
290b3918833SOleksandr Tymoshenko 	struct spi_command cmd;
29119aa9f71SIan Lepore 	off_t bytes_to_write;
292b3918833SOleksandr Tymoshenko 	int err = 0;
293b3918833SOleksandr Tymoshenko 
2942d46b036SStanislav Galabov 	if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
2952d46b036SStanislav Galabov 		cmd.tx_cmd_sz = 5;
2962d46b036SStanislav Galabov 		cmd.rx_cmd_sz = 5;
2972d46b036SStanislav Galabov 	} else {
298b3918833SOleksandr Tymoshenko 		cmd.tx_cmd_sz = 4;
299b3918833SOleksandr Tymoshenko 		cmd.rx_cmd_sz = 4;
3002d46b036SStanislav Galabov 	}
301b3918833SOleksandr Tymoshenko 
302b3918833SOleksandr Tymoshenko 	/*
303f432eb7eSIan Lepore 	 * Writes must be aligned to the erase sectorsize, since blocks are
304f432eb7eSIan Lepore 	 * fully erased before they're written to.
305b3918833SOleksandr Tymoshenko 	 */
3063c9af13cSIan Lepore 	if (count % sc->sc_erasesize != 0 || offset % sc->sc_erasesize != 0)
307917721a4SAdrian Chadd 		return (EIO);
308b3918833SOleksandr Tymoshenko 
309b3918833SOleksandr Tymoshenko 	/*
31019aa9f71SIan Lepore 	 * Maximum write size for CMD_PAGE_PROGRAM is FLASH_PAGE_SIZE, so loop
31119aa9f71SIan Lepore 	 * to write chunks of FLASH_PAGE_SIZE bytes each.
312b3918833SOleksandr Tymoshenko 	 */
31319aa9f71SIan Lepore 	while (count != 0) {
31419aa9f71SIan Lepore 		/* If we crossed a sector boundary, erase the next sector. */
3153c9af13cSIan Lepore 		if (((offset) % sc->sc_erasesize) == 0) {
3163c9af13cSIan Lepore 			err = mx25l_erase_cmd(sc, offset);
317c03ab159SIan Lepore 			if (err)
318c03ab159SIan Lepore 				break;
319c03ab159SIan Lepore 		}
320b3918833SOleksandr Tymoshenko 
321b3918833SOleksandr Tymoshenko 		txBuf[0] = CMD_PAGE_PROGRAM;
3222d46b036SStanislav Galabov 		if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
32319aa9f71SIan Lepore 			txBuf[1] = (offset >> 24) & 0xff;
32419aa9f71SIan Lepore 			txBuf[2] = (offset >> 16) & 0xff;
32519aa9f71SIan Lepore 			txBuf[3] = (offset >> 8) & 0xff;
32619aa9f71SIan Lepore 			txBuf[4] = offset & 0xff;
3272d46b036SStanislav Galabov 		} else {
32819aa9f71SIan Lepore 			txBuf[1] = (offset >> 16) & 0xff;
32919aa9f71SIan Lepore 			txBuf[2] = (offset >> 8) & 0xff;
33019aa9f71SIan Lepore 			txBuf[3] = offset & 0xff;
3312d46b036SStanislav Galabov 		}
332b3918833SOleksandr Tymoshenko 
33319aa9f71SIan Lepore 		bytes_to_write = MIN(FLASH_PAGE_SIZE, count);
334b3918833SOleksandr Tymoshenko 		cmd.tx_cmd = txBuf;
335b3918833SOleksandr Tymoshenko 		cmd.rx_cmd = rxBuf;
33619aa9f71SIan Lepore 		cmd.tx_data = data;
337dac94adbSIan Lepore 		cmd.rx_data = sc->sc_dummybuf;
33819aa9f71SIan Lepore 		cmd.tx_data_sz = (uint32_t)bytes_to_write;
33919aa9f71SIan Lepore 		cmd.rx_data_sz = (uint32_t)bytes_to_write;
340b3918833SOleksandr Tymoshenko 
341b3918833SOleksandr Tymoshenko 		/*
34219aa9f71SIan Lepore 		 * Each completed write operation resets WEL (write enable
34319aa9f71SIan Lepore 		 * latch) to disabled state, so we re-enable it here.
344b3918833SOleksandr Tymoshenko 		 */
345c03ab159SIan Lepore 		if ((err = mx25l_wait_for_device_ready(sc)) != 0)
346c03ab159SIan Lepore 			break;
347c03ab159SIan Lepore 		if ((err = mx25l_set_writable(sc, 1)) != 0)
348c03ab159SIan Lepore 			break;
349b3918833SOleksandr Tymoshenko 
35089a1585bSIan Lepore 		err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
351c03ab159SIan Lepore 		if (err != 0)
352c03ab159SIan Lepore 			break;
353c03ab159SIan Lepore 		err = mx25l_wait_for_device_ready(sc);
354b3918833SOleksandr Tymoshenko 		if (err)
355b3918833SOleksandr Tymoshenko 			break;
356b3918833SOleksandr Tymoshenko 
35719aa9f71SIan Lepore 		data   += bytes_to_write;
35819aa9f71SIan Lepore 		offset += bytes_to_write;
35919aa9f71SIan Lepore 		count  -= bytes_to_write;
360b3918833SOleksandr Tymoshenko 	}
361b3918833SOleksandr Tymoshenko 
362b3918833SOleksandr Tymoshenko 	return (err);
363b3918833SOleksandr Tymoshenko }
364b3918833SOleksandr Tymoshenko 
365b3918833SOleksandr Tymoshenko static int
36689a1585bSIan Lepore mx25l_read(struct mx25l_softc *sc, off_t offset, caddr_t data, off_t count)
367b3918833SOleksandr Tymoshenko {
368b3918833SOleksandr Tymoshenko 	uint8_t txBuf[8], rxBuf[8];
369b3918833SOleksandr Tymoshenko 	struct spi_command cmd;
370b3918833SOleksandr Tymoshenko 	int err = 0;
371b3918833SOleksandr Tymoshenko 
372b3918833SOleksandr Tymoshenko 	/*
37319aa9f71SIan Lepore 	 * Enforce that reads are aligned to the disk sectorsize, not the
37419aa9f71SIan Lepore 	 * erase sectorsize.  In this way, smaller read IO is possible,
37519aa9f71SIan Lepore 	 * dramatically speeding up filesystem/geom_compress access.
376b3918833SOleksandr Tymoshenko 	 */
37719aa9f71SIan Lepore 	if (count % sc->sc_disk->d_sectorsize != 0 ||
37819aa9f71SIan Lepore 	    offset % sc->sc_disk->d_sectorsize != 0)
379917721a4SAdrian Chadd 		return (EIO);
380b3918833SOleksandr Tymoshenko 
381b3918833SOleksandr Tymoshenko 	txBuf[0] = CMD_FAST_READ;
3822d46b036SStanislav Galabov 	if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
3832d46b036SStanislav Galabov 		cmd.tx_cmd_sz = 6;
3842d46b036SStanislav Galabov 		cmd.rx_cmd_sz = 6;
3852d46b036SStanislav Galabov 
38619aa9f71SIan Lepore 		txBuf[1] = (offset >> 24) & 0xff;
38719aa9f71SIan Lepore 		txBuf[2] = (offset >> 16) & 0xff;
38819aa9f71SIan Lepore 		txBuf[3] = (offset >> 8) & 0xff;
38919aa9f71SIan Lepore 		txBuf[4] = offset & 0xff;
3902d46b036SStanislav Galabov 		/* Dummy byte */
3912d46b036SStanislav Galabov 		txBuf[5] = 0;
3922d46b036SStanislav Galabov 	} else {
393b3918833SOleksandr Tymoshenko 		cmd.tx_cmd_sz = 5;
394b3918833SOleksandr Tymoshenko 		cmd.rx_cmd_sz = 5;
395b3918833SOleksandr Tymoshenko 
39619aa9f71SIan Lepore 		txBuf[1] = (offset >> 16) & 0xff;
39719aa9f71SIan Lepore 		txBuf[2] = (offset >> 8) & 0xff;
39819aa9f71SIan Lepore 		txBuf[3] = offset & 0xff;
399b3918833SOleksandr Tymoshenko 		/* Dummy byte */
400b3918833SOleksandr Tymoshenko 		txBuf[4] = 0;
4012d46b036SStanislav Galabov 	}
402b3918833SOleksandr Tymoshenko 
403b3918833SOleksandr Tymoshenko 	cmd.tx_cmd = txBuf;
404b3918833SOleksandr Tymoshenko 	cmd.rx_cmd = rxBuf;
405b3918833SOleksandr Tymoshenko 	cmd.tx_data = data;
406b3918833SOleksandr Tymoshenko 	cmd.rx_data = data;
40719aa9f71SIan Lepore 	cmd.tx_data_sz = count;
408b3918833SOleksandr Tymoshenko 	cmd.rx_data_sz = count;
409b3918833SOleksandr Tymoshenko 
41089a1585bSIan Lepore 	err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
411b3918833SOleksandr Tymoshenko 	return (err);
412b3918833SOleksandr Tymoshenko }
413b3918833SOleksandr Tymoshenko 
414b3918833SOleksandr Tymoshenko static int
41589a1585bSIan Lepore mx25l_set_4b_mode(struct mx25l_softc *sc, uint8_t command)
4162d46b036SStanislav Galabov {
4172d46b036SStanislav Galabov 	uint8_t txBuf[1], rxBuf[1];
4182d46b036SStanislav Galabov 	struct spi_command cmd;
4192d46b036SStanislav Galabov 	int err;
4202d46b036SStanislav Galabov 
4212d46b036SStanislav Galabov 	memset(&cmd, 0, sizeof(cmd));
4222d46b036SStanislav Galabov 	memset(txBuf, 0, sizeof(txBuf));
4232d46b036SStanislav Galabov 	memset(rxBuf, 0, sizeof(rxBuf));
4242d46b036SStanislav Galabov 
4252d46b036SStanislav Galabov 	cmd.tx_cmd_sz = cmd.rx_cmd_sz = 1;
4262d46b036SStanislav Galabov 
4272d46b036SStanislav Galabov 	cmd.tx_cmd = txBuf;
4282d46b036SStanislav Galabov 	cmd.rx_cmd = rxBuf;
4292d46b036SStanislav Galabov 
4302d46b036SStanislav Galabov 	txBuf[0] = command;
4312d46b036SStanislav Galabov 
432c03ab159SIan Lepore 	if ((err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd)) == 0)
433c03ab159SIan Lepore 		err = mx25l_wait_for_device_ready(sc);
4342d46b036SStanislav Galabov 
4352d46b036SStanislav Galabov 	return (err);
4362d46b036SStanislav Galabov }
4372d46b036SStanislav Galabov 
43875b3cfd2SAdrian Chadd #ifdef	FDT
4391173c206SStanislav Galabov static struct ofw_compat_data compat_data[] = {
4401173c206SStanislav Galabov 	{ "st,m25p",		1 },
4411173c206SStanislav Galabov 	{ "jedec,spi-nor",	1 },
4421173c206SStanislav Galabov 	{ NULL,			0 },
4431173c206SStanislav Galabov };
44475b3cfd2SAdrian Chadd #endif
4451173c206SStanislav Galabov 
4462d46b036SStanislav Galabov static int
447cd5bdf03SOleksandr Tymoshenko mx25l_probe(device_t dev)
448cd5bdf03SOleksandr Tymoshenko {
4498f74c88aSLuiz Otavio O Souza #ifdef FDT
450009e872eSStanislav Galabov 	int i;
451009e872eSStanislav Galabov 
4528f74c88aSLuiz Otavio O Souza 	if (!ofw_bus_status_okay(dev))
4538f74c88aSLuiz Otavio O Souza 		return (ENXIO);
454009e872eSStanislav Galabov 
455009e872eSStanislav Galabov 	/* First try to match the compatible property to the compat_data */
456009e872eSStanislav Galabov 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 1)
457009e872eSStanislav Galabov 		goto found;
458009e872eSStanislav Galabov 
459009e872eSStanislav Galabov 	/*
460009e872eSStanislav Galabov 	 * Next, try to find a compatible device using the names in the
461009e872eSStanislav Galabov 	 * flash_devices structure
462009e872eSStanislav Galabov 	 */
463009e872eSStanislav Galabov 	for (i = 0; i < nitems(flash_devices); i++)
464009e872eSStanislav Galabov 		if (ofw_bus_is_compatible(dev, flash_devices[i].name))
465009e872eSStanislav Galabov 			goto found;
466009e872eSStanislav Galabov 
4678f74c88aSLuiz Otavio O Souza 	return (ENXIO);
468009e872eSStanislav Galabov found:
4698f74c88aSLuiz Otavio O Souza #endif
470cd5bdf03SOleksandr Tymoshenko 	device_set_desc(dev, "M25Pxx Flash Family");
4718f74c88aSLuiz Otavio O Souza 
472cd5bdf03SOleksandr Tymoshenko 	return (0);
473cd5bdf03SOleksandr Tymoshenko }
474cd5bdf03SOleksandr Tymoshenko 
475cd5bdf03SOleksandr Tymoshenko static int
476cd5bdf03SOleksandr Tymoshenko mx25l_attach(device_t dev)
477cd5bdf03SOleksandr Tymoshenko {
478cd5bdf03SOleksandr Tymoshenko 	struct mx25l_softc *sc;
479cd5bdf03SOleksandr Tymoshenko 	struct mx25l_flash_ident *ident;
480c03ab159SIan Lepore 	int err;
481cd5bdf03SOleksandr Tymoshenko 
482cd5bdf03SOleksandr Tymoshenko 	sc = device_get_softc(dev);
483cd5bdf03SOleksandr Tymoshenko 	sc->sc_dev = dev;
48489a1585bSIan Lepore 	sc->sc_parent = device_get_parent(sc->sc_dev);
48589a1585bSIan Lepore 
486cd5bdf03SOleksandr Tymoshenko 	M25PXX_LOCK_INIT(sc);
487cd5bdf03SOleksandr Tymoshenko 
488cd5bdf03SOleksandr Tymoshenko 	ident = mx25l_get_device_ident(sc);
489cd5bdf03SOleksandr Tymoshenko 	if (ident == NULL)
490cd5bdf03SOleksandr Tymoshenko 		return (ENXIO);
491cd5bdf03SOleksandr Tymoshenko 
492c03ab159SIan Lepore 	if ((err = mx25l_wait_for_device_ready(sc)) != 0)
493c03ab159SIan Lepore 		return (err);
494c03ab159SIan Lepore 
4953c9af13cSIan Lepore 	sc->sc_flags = ident->flags;
4963c9af13cSIan Lepore 
4973c9af13cSIan Lepore 	if (sc->sc_flags & FL_ERASE_4K)
4983c9af13cSIan Lepore 		sc->sc_erasesize = 4 * 1024;
4993c9af13cSIan Lepore 	else if (sc->sc_flags & FL_ERASE_32K)
5003c9af13cSIan Lepore 		sc->sc_erasesize = 32 * 1024;
5013c9af13cSIan Lepore 	else
5023c9af13cSIan Lepore 		sc->sc_erasesize = ident->sectorsize;
5033c9af13cSIan Lepore 
504c03ab159SIan Lepore 	if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
505c03ab159SIan Lepore 		if ((err = mx25l_set_4b_mode(sc, CMD_ENTER_4B_MODE)) != 0)
506c03ab159SIan Lepore 			return (err);
507c03ab159SIan Lepore 	} else if (sc->sc_flags & FL_DISABLE_4B_ADDR) {
508c03ab159SIan Lepore 		if ((err = mx25l_set_4b_mode(sc, CMD_EXIT_4B_MODE)) != 0)
509c03ab159SIan Lepore 			return (err);
510c03ab159SIan Lepore 	}
511cd5bdf03SOleksandr Tymoshenko 
512cd5bdf03SOleksandr Tymoshenko 	sc->sc_disk = disk_alloc();
513cd5bdf03SOleksandr Tymoshenko 	sc->sc_disk->d_open = mx25l_open;
514cd5bdf03SOleksandr Tymoshenko 	sc->sc_disk->d_close = mx25l_close;
515cd5bdf03SOleksandr Tymoshenko 	sc->sc_disk->d_strategy = mx25l_strategy;
5161b468038SAdrian Chadd 	sc->sc_disk->d_getattr = mx25l_getattr;
517cd5bdf03SOleksandr Tymoshenko 	sc->sc_disk->d_ioctl = mx25l_ioctl;
51868dd7795SIan Lepore 	sc->sc_disk->d_name = "flash/spi";
519cd5bdf03SOleksandr Tymoshenko 	sc->sc_disk->d_drv1 = sc;
520cd5bdf03SOleksandr Tymoshenko 	sc->sc_disk->d_maxsize = DFLTPHYS;
521917721a4SAdrian Chadd 	sc->sc_disk->d_sectorsize = MX25L_SECTORSIZE;
522cd5bdf03SOleksandr Tymoshenko 	sc->sc_disk->d_mediasize = ident->sectorsize * ident->sectorcount;
5233c9af13cSIan Lepore 	sc->sc_disk->d_stripesize = sc->sc_erasesize;
524cd5bdf03SOleksandr Tymoshenko 	sc->sc_disk->d_unit = device_get_unit(sc->sc_dev);
525cd5bdf03SOleksandr Tymoshenko 	sc->sc_disk->d_dump = NULL;		/* NB: no dumps */
526d8920513SIan Lepore 	strlcpy(sc->sc_disk->d_descr, ident->name,
527d8920513SIan Lepore 	    sizeof(sc->sc_disk->d_descr));
528cd5bdf03SOleksandr Tymoshenko 
529cd5bdf03SOleksandr Tymoshenko 	disk_create(sc->sc_disk, DISK_VERSION);
530cd5bdf03SOleksandr Tymoshenko 	bioq_init(&sc->sc_bio_queue);
531cd5bdf03SOleksandr Tymoshenko 
532cd5bdf03SOleksandr Tymoshenko 	kproc_create(&mx25l_task, sc, &sc->sc_p, 0, 0, "task: mx25l flash");
533aae18dccSIan Lepore 	sc->sc_taskstate = TSTATE_RUNNING;
534aae18dccSIan Lepore 
535d8920513SIan Lepore 	device_printf(sc->sc_dev,
536d8920513SIan Lepore 	    "device type %s, size %dK in %d sectors of %dK, erase size %dK\n",
537d8920513SIan Lepore 	    ident->name,
538d8920513SIan Lepore 	    ident->sectorcount * ident->sectorsize / 1024,
539d8920513SIan Lepore 	    ident->sectorcount, ident->sectorsize / 1024,
540d8920513SIan Lepore 	    sc->sc_erasesize / 1024);
541cd5bdf03SOleksandr Tymoshenko 
542cd5bdf03SOleksandr Tymoshenko 	return (0);
543cd5bdf03SOleksandr Tymoshenko }
544cd5bdf03SOleksandr Tymoshenko 
545cd5bdf03SOleksandr Tymoshenko static int
546cd5bdf03SOleksandr Tymoshenko mx25l_detach(device_t dev)
547cd5bdf03SOleksandr Tymoshenko {
548aae18dccSIan Lepore 	struct mx25l_softc *sc;
549aae18dccSIan Lepore 	int err;
550cd5bdf03SOleksandr Tymoshenko 
551aae18dccSIan Lepore 	sc = device_get_softc(dev);
552aae18dccSIan Lepore 	err = 0;
553aae18dccSIan Lepore 
554aae18dccSIan Lepore 	M25PXX_LOCK(sc);
555aae18dccSIan Lepore 	if (sc->sc_taskstate == TSTATE_RUNNING) {
556aae18dccSIan Lepore 		sc->sc_taskstate = TSTATE_STOPPING;
557aae18dccSIan Lepore 		wakeup(sc);
558aae18dccSIan Lepore 		while (err == 0 && sc->sc_taskstate != TSTATE_STOPPED) {
559aae18dccSIan Lepore 			err = msleep(sc, &sc->sc_mtx, 0, "mx25dt", hz * 3);
560aae18dccSIan Lepore 			if (err != 0) {
561aae18dccSIan Lepore 				sc->sc_taskstate = TSTATE_RUNNING;
56289a1585bSIan Lepore 				device_printf(sc->sc_dev,
563aae18dccSIan Lepore 				    "Failed to stop queue task\n");
564aae18dccSIan Lepore 			}
565aae18dccSIan Lepore 		}
566aae18dccSIan Lepore 	}
567aae18dccSIan Lepore 	M25PXX_UNLOCK(sc);
568aae18dccSIan Lepore 
569aae18dccSIan Lepore 	if (err == 0 && sc->sc_taskstate == TSTATE_STOPPED) {
570aae18dccSIan Lepore 		disk_destroy(sc->sc_disk);
571aae18dccSIan Lepore 		bioq_flush(&sc->sc_bio_queue, NULL, ENXIO);
572aae18dccSIan Lepore 		M25PXX_LOCK_DESTROY(sc);
573aae18dccSIan Lepore 	}
574aae18dccSIan Lepore 	return (err);
575cd5bdf03SOleksandr Tymoshenko }
576cd5bdf03SOleksandr Tymoshenko 
577cd5bdf03SOleksandr Tymoshenko static int
578cd5bdf03SOleksandr Tymoshenko mx25l_open(struct disk *dp)
579cd5bdf03SOleksandr Tymoshenko {
580cd5bdf03SOleksandr Tymoshenko 	return (0);
581cd5bdf03SOleksandr Tymoshenko }
582cd5bdf03SOleksandr Tymoshenko 
583cd5bdf03SOleksandr Tymoshenko static int
584cd5bdf03SOleksandr Tymoshenko mx25l_close(struct disk *dp)
585cd5bdf03SOleksandr Tymoshenko {
586cd5bdf03SOleksandr Tymoshenko 
587cd5bdf03SOleksandr Tymoshenko 	return (0);
588cd5bdf03SOleksandr Tymoshenko }
589cd5bdf03SOleksandr Tymoshenko 
590cd5bdf03SOleksandr Tymoshenko static int
591cd5bdf03SOleksandr Tymoshenko mx25l_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
592cd5bdf03SOleksandr Tymoshenko 	struct thread *td)
593cd5bdf03SOleksandr Tymoshenko {
594cd5bdf03SOleksandr Tymoshenko 
595cd5bdf03SOleksandr Tymoshenko 	return (EINVAL);
596cd5bdf03SOleksandr Tymoshenko }
597cd5bdf03SOleksandr Tymoshenko 
598cd5bdf03SOleksandr Tymoshenko static void
599cd5bdf03SOleksandr Tymoshenko mx25l_strategy(struct bio *bp)
600cd5bdf03SOleksandr Tymoshenko {
601cd5bdf03SOleksandr Tymoshenko 	struct mx25l_softc *sc;
602cd5bdf03SOleksandr Tymoshenko 
603cd5bdf03SOleksandr Tymoshenko 	sc = (struct mx25l_softc *)bp->bio_disk->d_drv1;
604cd5bdf03SOleksandr Tymoshenko 	M25PXX_LOCK(sc);
605cd5bdf03SOleksandr Tymoshenko 	bioq_disksort(&sc->sc_bio_queue, bp);
606cd5bdf03SOleksandr Tymoshenko 	wakeup(sc);
607cd5bdf03SOleksandr Tymoshenko 	M25PXX_UNLOCK(sc);
608cd5bdf03SOleksandr Tymoshenko }
609cd5bdf03SOleksandr Tymoshenko 
6101b468038SAdrian Chadd static int
6111b468038SAdrian Chadd mx25l_getattr(struct bio *bp)
6121b468038SAdrian Chadd {
6131b468038SAdrian Chadd 	struct mx25l_softc *sc;
6141b468038SAdrian Chadd 	device_t dev;
6151b468038SAdrian Chadd 
6161b468038SAdrian Chadd 	if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
6171b468038SAdrian Chadd 		return (ENXIO);
6181b468038SAdrian Chadd 
6191b468038SAdrian Chadd 	sc = bp->bio_disk->d_drv1;
6201b468038SAdrian Chadd 	dev = sc->sc_dev;
6211b468038SAdrian Chadd 
6221b468038SAdrian Chadd 	if (strcmp(bp->bio_attribute, "SPI::device") == 0) {
6231b468038SAdrian Chadd 		if (bp->bio_length != sizeof(dev))
6241b468038SAdrian Chadd 			return (EFAULT);
6251b468038SAdrian Chadd 		bcopy(&dev, bp->bio_data, sizeof(dev));
6261b468038SAdrian Chadd 	} else
6271b468038SAdrian Chadd 		return (-1);
6281b468038SAdrian Chadd 	return (0);
6291b468038SAdrian Chadd }
6301b468038SAdrian Chadd 
631cd5bdf03SOleksandr Tymoshenko static void
632cd5bdf03SOleksandr Tymoshenko mx25l_task(void *arg)
633cd5bdf03SOleksandr Tymoshenko {
634cd5bdf03SOleksandr Tymoshenko 	struct mx25l_softc *sc = (struct mx25l_softc*)arg;
635cd5bdf03SOleksandr Tymoshenko 	struct bio *bp;
636cd5bdf03SOleksandr Tymoshenko 
637cd5bdf03SOleksandr Tymoshenko 	for (;;) {
638cd5bdf03SOleksandr Tymoshenko 		M25PXX_LOCK(sc);
639cd5bdf03SOleksandr Tymoshenko 		do {
640aae18dccSIan Lepore 			if (sc->sc_taskstate == TSTATE_STOPPING) {
641aae18dccSIan Lepore 				sc->sc_taskstate = TSTATE_STOPPED;
642aae18dccSIan Lepore 				M25PXX_UNLOCK(sc);
643aae18dccSIan Lepore 				wakeup(sc);
644aae18dccSIan Lepore 				kproc_exit(0);
645aae18dccSIan Lepore 			}
646cd5bdf03SOleksandr Tymoshenko 			bp = bioq_first(&sc->sc_bio_queue);
647cd5bdf03SOleksandr Tymoshenko 			if (bp == NULL)
648aae18dccSIan Lepore 				msleep(sc, &sc->sc_mtx, PRIBIO, "mx25jq", 0);
649cd5bdf03SOleksandr Tymoshenko 		} while (bp == NULL);
650cd5bdf03SOleksandr Tymoshenko 		bioq_remove(&sc->sc_bio_queue, bp);
651cd5bdf03SOleksandr Tymoshenko 		M25PXX_UNLOCK(sc);
652cd5bdf03SOleksandr Tymoshenko 
653b3918833SOleksandr Tymoshenko 		switch (bp->bio_cmd) {
654b3918833SOleksandr Tymoshenko 		case BIO_READ:
65589a1585bSIan Lepore 			bp->bio_error = mx25l_read(sc, bp->bio_offset,
656b3918833SOleksandr Tymoshenko 			    bp->bio_data, bp->bio_bcount);
657c3655ab0SOleksandr Tymoshenko 			break;
658b3918833SOleksandr Tymoshenko 		case BIO_WRITE:
65989a1585bSIan Lepore 			bp->bio_error = mx25l_write(sc, bp->bio_offset,
660b3918833SOleksandr Tymoshenko 			    bp->bio_data, bp->bio_bcount);
661b3918833SOleksandr Tymoshenko 			break;
662b3918833SOleksandr Tymoshenko 		default:
663d176b803SScott Long 			bp->bio_error = EOPNOTSUPP;
664b3918833SOleksandr Tymoshenko 		}
665cd5bdf03SOleksandr Tymoshenko 
666c3655ab0SOleksandr Tymoshenko 
667cd5bdf03SOleksandr Tymoshenko 		biodone(bp);
668cd5bdf03SOleksandr Tymoshenko 	}
669cd5bdf03SOleksandr Tymoshenko }
670cd5bdf03SOleksandr Tymoshenko 
671cd5bdf03SOleksandr Tymoshenko static device_method_t mx25l_methods[] = {
672cd5bdf03SOleksandr Tymoshenko 	/* Device interface */
673cd5bdf03SOleksandr Tymoshenko 	DEVMETHOD(device_probe,		mx25l_probe),
674cd5bdf03SOleksandr Tymoshenko 	DEVMETHOD(device_attach,	mx25l_attach),
675cd5bdf03SOleksandr Tymoshenko 	DEVMETHOD(device_detach,	mx25l_detach),
676cd5bdf03SOleksandr Tymoshenko 
677cd5bdf03SOleksandr Tymoshenko 	{ 0, 0 }
678cd5bdf03SOleksandr Tymoshenko };
679cd5bdf03SOleksandr Tymoshenko 
680cd5bdf03SOleksandr Tymoshenko static driver_t mx25l_driver = {
681cd5bdf03SOleksandr Tymoshenko 	"mx25l",
682cd5bdf03SOleksandr Tymoshenko 	mx25l_methods,
683cd5bdf03SOleksandr Tymoshenko 	sizeof(struct mx25l_softc),
684cd5bdf03SOleksandr Tymoshenko };
685cd5bdf03SOleksandr Tymoshenko 
686*0d95fe04SJohn Baldwin DRIVER_MODULE(mx25l, spibus, mx25l_driver, 0, 0);
68742c52f36SIan Lepore MODULE_DEPEND(mx25l, spibus, 1, 1, 1);
6887de871aaSEmmanuel Vadot #ifdef	FDT
689fb4f7d70SIan Lepore MODULE_DEPEND(mx25l, fdt_slicer, 1, 1, 1);
690db63d251SIan Lepore SPIBUS_FDT_PNP_INFO(compat_data);
6917de871aaSEmmanuel Vadot #endif
692