xref: /freebsd-src/sys/dev/flash/at45d.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1674a9140SWarner Losh /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4f86e6000SWarner Losh  * Copyright (c) 2011-2012 Ian Lepore All rights reserved.
5f86e6000SWarner Losh  * Copyright (c) 2012 Marius Strobl <marius@FreeBSD.org> All rights reserved.
6f86e6000SWarner Losh  * Copyright (c) 2006 M. Warner Losh <imp@FreeBSD.org>
7f86e6000SWarner Losh 
8674a9140SWarner Losh  * Redistribution and use in source and binary forms, with or without
9674a9140SWarner Losh  * modification, are permitted provided that the following conditions
10674a9140SWarner Losh  * are met:
11674a9140SWarner Losh  * 1. Redistributions of source code must retain the above copyright
12674a9140SWarner Losh  *    notice, this list of conditions and the following disclaimer.
13674a9140SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
14674a9140SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
15674a9140SWarner Losh  *    documentation and/or other materials provided with the distribution.
16674a9140SWarner Losh  *
17674a9140SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18674a9140SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19674a9140SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20674a9140SWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21674a9140SWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22674a9140SWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23674a9140SWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24674a9140SWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25674a9140SWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26674a9140SWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27674a9140SWarner Losh  */
28674a9140SWarner Losh 
29674a9140SWarner Losh #include <sys/param.h>
30674a9140SWarner Losh #include <sys/systm.h>
31674a9140SWarner Losh #include <sys/bio.h>
32674a9140SWarner Losh #include <sys/bus.h>
33674a9140SWarner Losh #include <sys/conf.h>
349b9a1641SIan Lepore #include <sys/endian.h>
35674a9140SWarner Losh #include <sys/kernel.h>
36674a9140SWarner Losh #include <sys/kthread.h>
37674a9140SWarner Losh #include <sys/lock.h>
38674a9140SWarner Losh #include <sys/mbuf.h>
39674a9140SWarner Losh #include <sys/malloc.h>
40674a9140SWarner Losh #include <sys/module.h>
41674a9140SWarner Losh #include <sys/mutex.h>
42674a9140SWarner Losh #include <geom/geom_disk.h>
43674a9140SWarner Losh 
44674a9140SWarner Losh #include <dev/spibus/spi.h>
45674a9140SWarner Losh #include "spibus_if.h"
46674a9140SWarner Losh 
47b9f80455SIan Lepore #include "opt_platform.h"
48b9f80455SIan Lepore 
49b9f80455SIan Lepore #ifdef FDT
50b9f80455SIan Lepore #include <dev/fdt/fdt_common.h>
51b9f80455SIan Lepore #include <dev/ofw/ofw_bus_subr.h>
52b9f80455SIan Lepore #include <dev/ofw/openfirm.h>
53b9f80455SIan Lepore 
54b9f80455SIan Lepore static struct ofw_compat_data compat_data[] = {
55b9f80455SIan Lepore 	{ "atmel,at45",		1 },
56b9f80455SIan Lepore 	{ "atmel,dataflash",	1 },
57b9f80455SIan Lepore 	{ NULL,			0 },
58b9f80455SIan Lepore };
59b9f80455SIan Lepore #endif
60b9f80455SIan Lepore 
619b9a1641SIan Lepore /* This is the information returned by the MANUFACTURER_ID command. */
629b9a1641SIan Lepore struct at45d_mfg_info {
639b9a1641SIan Lepore 	uint32_t	jedec_id; /* Mfg ID, DevId1, DevId2, ExtLen */
649b9a1641SIan Lepore 	uint16_t	ext_id;   /* ExtId1, ExtId2 */
659b9a1641SIan Lepore };
669b9a1641SIan Lepore 
679b9a1641SIan Lepore /*
689b9a1641SIan Lepore  * This is an entry in our table of metadata describing the chips.  We match on
699b9a1641SIan Lepore  * both jedec id and extended id info returned by the MANUFACTURER_ID command.
709b9a1641SIan Lepore  */
717f2107d4SMarius Strobl struct at45d_flash_ident
727f2107d4SMarius Strobl {
737f2107d4SMarius Strobl 	const char	*name;
747f2107d4SMarius Strobl 	uint32_t	jedec;
759b9a1641SIan Lepore 	uint16_t	extid;
769b9a1641SIan Lepore 	uint16_t	extmask;
777f2107d4SMarius Strobl 	uint16_t	pagecount;
787f2107d4SMarius Strobl 	uint16_t	pageoffset;
797f2107d4SMarius Strobl 	uint16_t	pagesize;
807f2107d4SMarius Strobl 	uint16_t	pagesize2n;
817f2107d4SMarius Strobl };
827f2107d4SMarius Strobl 
83674a9140SWarner Losh struct at45d_softc
84674a9140SWarner Losh {
857f2107d4SMarius Strobl 	struct bio_queue_head	bio_queue;
86674a9140SWarner Losh 	struct mtx		sc_mtx;
87674a9140SWarner Losh 	struct disk		*disk;
88674a9140SWarner Losh 	struct proc		*p;
897f2107d4SMarius Strobl 	device_t		dev;
904b06839fSIan Lepore 	u_int			taskstate;
917f2107d4SMarius Strobl 	uint16_t		pagecount;
927f2107d4SMarius Strobl 	uint16_t		pageoffset;
937f2107d4SMarius Strobl 	uint16_t		pagesize;
94d4249d08SIan Lepore 	void			*dummybuf;
95674a9140SWarner Losh };
96674a9140SWarner Losh 
974b06839fSIan Lepore #define	TSTATE_STOPPED	0
984b06839fSIan Lepore #define	TSTATE_STOPPING	1
994b06839fSIan Lepore #define	TSTATE_RUNNING	2
1004b06839fSIan Lepore 
101674a9140SWarner Losh #define	AT45D_LOCK(_sc)			mtx_lock(&(_sc)->sc_mtx)
102674a9140SWarner Losh #define	AT45D_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
103674a9140SWarner Losh #define	AT45D_LOCK_INIT(_sc) \
104674a9140SWarner Losh 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
105674a9140SWarner Losh 	    "at45d", MTX_DEF)
106674a9140SWarner Losh #define	AT45D_LOCK_DESTROY(_sc)		mtx_destroy(&_sc->sc_mtx);
107674a9140SWarner Losh #define	AT45D_ASSERT_LOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_OWNED);
108674a9140SWarner Losh #define	AT45D_ASSERT_UNLOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
109674a9140SWarner Losh 
1107f2107d4SMarius Strobl /* bus entry points */
1117f2107d4SMarius Strobl static device_attach_t at45d_attach;
1127f2107d4SMarius Strobl static device_detach_t at45d_detach;
1137f2107d4SMarius Strobl static device_probe_t at45d_probe;
114674a9140SWarner Losh 
115674a9140SWarner Losh /* disk routines */
116674a9140SWarner Losh static int at45d_close(struct disk *dp);
1177f2107d4SMarius Strobl static int at45d_open(struct disk *dp);
118fb019471SIan Lepore static int at45d_getattr(struct bio *bp);
119674a9140SWarner Losh static void at45d_strategy(struct bio *bp);
120674a9140SWarner Losh static void at45d_task(void *arg);
121674a9140SWarner Losh 
1227f2107d4SMarius Strobl /* helper routines */
1237f2107d4SMarius Strobl static void at45d_delayed_attach(void *xsc);
1249b9a1641SIan Lepore static int at45d_get_mfg_info(device_t dev, struct at45d_mfg_info *resp);
1257f2107d4SMarius Strobl static int at45d_get_status(device_t dev, uint8_t *status);
1267f2107d4SMarius Strobl static int at45d_wait_ready(device_t dev, uint8_t *status);
127674a9140SWarner Losh 
1282274a2f7SIan Lepore #define	PAGE_TO_BUFFER_TRANSFER		0x53
1292274a2f7SIan Lepore #define	PAGE_TO_BUFFER_COMPARE		0x60
1307f2107d4SMarius Strobl #define	PROGRAM_THROUGH_BUFFER		0x82
1317f2107d4SMarius Strobl #define	MANUFACTURER_ID			0x9f
1327f2107d4SMarius Strobl #define	STATUS_REGISTER_READ		0xd7
1337f2107d4SMarius Strobl #define	CONTINUOUS_ARRAY_READ		0xe8
1347f2107d4SMarius Strobl 
1352274a2f7SIan Lepore #define	STATUS_READY			(1u << 7)
1362274a2f7SIan Lepore #define	STATUS_CMPFAIL			(1u << 6)
1372274a2f7SIan Lepore #define	STATUS_PAGE2N			(1u << 0)
1382274a2f7SIan Lepore 
1397f2107d4SMarius Strobl /*
1409b9a1641SIan Lepore  * Metadata for supported chips.
1419b9a1641SIan Lepore  *
1429b9a1641SIan Lepore  * The jedec id in this table includes the extended id length byte.  A match is
1439b9a1641SIan Lepore  * based on both jedec id and extended id matching.  The chip's extended id (not
1449b9a1641SIan Lepore  * present in most chips) is ANDed with ExtMask and the result is compared to
1459b9a1641SIan Lepore  * ExtId.  If a chip only returns 1 ext id byte it will be in the upper 8 bits
1469b9a1641SIan Lepore  * of ExtId in this table.
1479b9a1641SIan Lepore  *
1487f2107d4SMarius Strobl  * A sectorsize2n != 0 is used to indicate that a device optionally supports
1497f2107d4SMarius Strobl  * 2^N byte pages.  If support for the latter is enabled, the sector offset
1507f2107d4SMarius Strobl  * has to be reduced by one.
1517f2107d4SMarius Strobl  */
15229658c96SDimitry Andric static const struct at45d_flash_ident at45d_flash_devices[] = {
1539b9a1641SIan Lepore 	/* Part Name    Jedec ID    ExtId   ExtMask PgCnt Offs PgSz PgSz2n */
1549b9a1641SIan Lepore 	{ "AT45DB011B", 0x1f220000, 0x0000, 0x0000,   512,  9,  264,  256 },
1559b9a1641SIan Lepore 	{ "AT45DB021B", 0x1f230000, 0x0000, 0x0000,  1024,  9,  264,  256 },
1569b9a1641SIan Lepore 	{ "AT45DB041x", 0x1f240000, 0x0000, 0x0000,  2028,  9,  264,  256 },
1579b9a1641SIan Lepore 	{ "AT45DB081B", 0x1f250000, 0x0000, 0x0000,  4096,  9,  264,  256 },
1589b9a1641SIan Lepore 	{ "AT45DB161x", 0x1f260000, 0x0000, 0x0000,  4096, 10,  528,  512 },
1599b9a1641SIan Lepore 	{ "AT45DB321x", 0x1f270000, 0x0000, 0x0000,  8192, 10,  528,    0 },
1609b9a1641SIan Lepore 	{ "AT45DB321x", 0x1f270100, 0x0000, 0x0000,  8192, 10,  528,  512 },
16156e07c3eSIan Lepore 	{ "AT45DB641E", 0x1f280001, 0x0000, 0xff00, 32768,  9,  264,  256 },
1629b9a1641SIan Lepore 	{ "AT45DB642x", 0x1f280000, 0x0000, 0x0000,  8192, 11, 1056, 1024 },
1637f2107d4SMarius Strobl };
1647f2107d4SMarius Strobl 
1657f2107d4SMarius Strobl static int
at45d_get_status(device_t dev,uint8_t * status)1667f2107d4SMarius Strobl at45d_get_status(device_t dev, uint8_t *status)
167674a9140SWarner Losh {
1687f2107d4SMarius Strobl 	uint8_t rxBuf[8], txBuf[8];
169c445ffceSAdrian Chadd 	struct spi_command cmd;
170674a9140SWarner Losh 	int err;
171674a9140SWarner Losh 
172674a9140SWarner Losh 	memset(&cmd, 0, sizeof(cmd));
173674a9140SWarner Losh 	memset(txBuf, 0, sizeof(txBuf));
174674a9140SWarner Losh 	memset(rxBuf, 0, sizeof(rxBuf));
175674a9140SWarner Losh 
176674a9140SWarner Losh 	txBuf[0] = STATUS_REGISTER_READ;
177674a9140SWarner Losh 	cmd.tx_cmd = txBuf;
178674a9140SWarner Losh 	cmd.rx_cmd = rxBuf;
1797f2107d4SMarius Strobl 	cmd.rx_cmd_sz = cmd.tx_cmd_sz = 2;
180674a9140SWarner Losh 	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
1817f2107d4SMarius Strobl 	*status = rxBuf[1];
1827f2107d4SMarius Strobl 	return (err);
183674a9140SWarner Losh }
184674a9140SWarner Losh 
185674a9140SWarner Losh static int
at45d_get_mfg_info(device_t dev,struct at45d_mfg_info * resp)1869b9a1641SIan Lepore at45d_get_mfg_info(device_t dev, struct at45d_mfg_info *resp)
187674a9140SWarner Losh {
1887f2107d4SMarius Strobl 	uint8_t rxBuf[8], txBuf[8];
189c445ffceSAdrian Chadd 	struct spi_command cmd;
190674a9140SWarner Losh 	int err;
191674a9140SWarner Losh 
192674a9140SWarner Losh 	memset(&cmd, 0, sizeof(cmd));
193674a9140SWarner Losh 	memset(txBuf, 0, sizeof(txBuf));
194674a9140SWarner Losh 	memset(rxBuf, 0, sizeof(rxBuf));
195674a9140SWarner Losh 
196674a9140SWarner Losh 	txBuf[0] = MANUFACTURER_ID;
197674a9140SWarner Losh 	cmd.tx_cmd = &txBuf;
198674a9140SWarner Losh 	cmd.rx_cmd = &rxBuf;
1999b9a1641SIan Lepore 	cmd.tx_cmd_sz = cmd.rx_cmd_sz = 7;
200674a9140SWarner Losh 	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
201674a9140SWarner Losh 	if (err)
202674a9140SWarner Losh 		return (err);
2039b9a1641SIan Lepore 
2049b9a1641SIan Lepore 	resp->jedec_id = be32dec(rxBuf + 1);
2059b9a1641SIan Lepore 	resp->ext_id   = be16dec(rxBuf + 5);
2069b9a1641SIan Lepore 
207674a9140SWarner Losh 	return (0);
208674a9140SWarner Losh }
209674a9140SWarner Losh 
210674a9140SWarner Losh static int
at45d_wait_ready(device_t dev,uint8_t * status)2117f2107d4SMarius Strobl at45d_wait_ready(device_t dev, uint8_t *status)
2127f2107d4SMarius Strobl {
2137f2107d4SMarius Strobl 	struct timeval now, tout;
2147f2107d4SMarius Strobl 	int err;
2157f2107d4SMarius Strobl 
2167f2107d4SMarius Strobl 	getmicrouptime(&tout);
2177f2107d4SMarius Strobl 	tout.tv_sec += 3;
2187f2107d4SMarius Strobl 	do {
2197f2107d4SMarius Strobl 		getmicrouptime(&now);
2207f2107d4SMarius Strobl 		if (now.tv_sec > tout.tv_sec)
2217f2107d4SMarius Strobl 			err = ETIMEDOUT;
2227f2107d4SMarius Strobl 		else
2237f2107d4SMarius Strobl 			err = at45d_get_status(dev, status);
2242274a2f7SIan Lepore 	} while (err == 0 && !(*status & STATUS_READY));
2257f2107d4SMarius Strobl 	return (err);
2267f2107d4SMarius Strobl }
2277f2107d4SMarius Strobl 
2287f2107d4SMarius Strobl static int
at45d_probe(device_t dev)229674a9140SWarner Losh at45d_probe(device_t dev)
230674a9140SWarner Losh {
231b9f80455SIan Lepore 	int rv;
232b9f80455SIan Lepore 
233b9f80455SIan Lepore #ifdef FDT
234b9f80455SIan Lepore 	if (!ofw_bus_status_okay(dev))
235b9f80455SIan Lepore 		return (ENXIO);
236b9f80455SIan Lepore 
237b9f80455SIan Lepore 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
238b9f80455SIan Lepore 		return (ENXIO);
239b9f80455SIan Lepore 
240b9f80455SIan Lepore 	rv = BUS_PROBE_DEFAULT;
241b9f80455SIan Lepore #else
242b9f80455SIan Lepore 	rv = BUS_PROBE_NOWILDCARD;
243b9f80455SIan Lepore #endif
2447f2107d4SMarius Strobl 
2457f2107d4SMarius Strobl 	device_set_desc(dev, "AT45D Flash Family");
246b9f80455SIan Lepore 	return (rv);
247674a9140SWarner Losh }
248674a9140SWarner Losh 
249674a9140SWarner Losh static int
at45d_attach(device_t dev)250674a9140SWarner Losh at45d_attach(device_t dev)
251674a9140SWarner Losh {
252674a9140SWarner Losh 	struct at45d_softc *sc;
253674a9140SWarner Losh 
254674a9140SWarner Losh 	sc = device_get_softc(dev);
255674a9140SWarner Losh 	sc->dev = dev;
256674a9140SWarner Losh 	AT45D_LOCK_INIT(sc);
257674a9140SWarner Losh 
2583ea8b6b3SIan Lepore 	config_intrhook_oneshot(at45d_delayed_attach, sc);
259674a9140SWarner Losh 	return (0);
260674a9140SWarner Losh }
261674a9140SWarner Losh 
262674a9140SWarner Losh static int
at45d_detach(device_t dev)263674a9140SWarner Losh at45d_detach(device_t dev)
264674a9140SWarner Losh {
2654b06839fSIan Lepore 	struct at45d_softc *sc;
2664b06839fSIan Lepore 	int err;
2677f2107d4SMarius Strobl 
2684b06839fSIan Lepore 	sc = device_get_softc(dev);
2694b06839fSIan Lepore 	err = 0;
2704b06839fSIan Lepore 
2714b06839fSIan Lepore 	AT45D_LOCK(sc);
2724b06839fSIan Lepore 	if (sc->taskstate == TSTATE_RUNNING) {
2734b06839fSIan Lepore 		sc->taskstate = TSTATE_STOPPING;
2744b06839fSIan Lepore 		wakeup(sc);
2754b06839fSIan Lepore 		while (err == 0 && sc->taskstate != TSTATE_STOPPED) {
2764b06839fSIan Lepore 			err = msleep(sc, &sc->sc_mtx, 0, "at45dt", hz * 3);
2774b06839fSIan Lepore 			if (err != 0) {
2784b06839fSIan Lepore 				sc->taskstate = TSTATE_RUNNING;
2794b06839fSIan Lepore 				device_printf(sc->dev,
2804b06839fSIan Lepore 				    "Failed to stop queue task\n");
2814b06839fSIan Lepore 			}
2824b06839fSIan Lepore 		}
2834b06839fSIan Lepore 	}
2844b06839fSIan Lepore 	AT45D_UNLOCK(sc);
2854b06839fSIan Lepore 
2864b06839fSIan Lepore 	if (err == 0 && sc->taskstate == TSTATE_STOPPED) {
287e70ece12SIan Lepore 		if (sc->disk) {
2884b06839fSIan Lepore 			disk_destroy(sc->disk);
2894b06839fSIan Lepore 			bioq_flush(&sc->bio_queue, NULL, ENXIO);
290d4249d08SIan Lepore 			free(sc->dummybuf, M_DEVBUF);
291e70ece12SIan Lepore 		}
2924b06839fSIan Lepore 		AT45D_LOCK_DESTROY(sc);
2934b06839fSIan Lepore 	}
2944b06839fSIan Lepore 	return (err);
295674a9140SWarner Losh }
296674a9140SWarner Losh 
297674a9140SWarner Losh static void
at45d_delayed_attach(void * xsc)298674a9140SWarner Losh at45d_delayed_attach(void *xsc)
299674a9140SWarner Losh {
3007f2107d4SMarius Strobl 	struct at45d_softc *sc;
3019b9a1641SIan Lepore 	struct at45d_mfg_info mfginfo;
3027f2107d4SMarius Strobl 	const struct at45d_flash_ident *ident;
3037f2107d4SMarius Strobl 	u_int i;
304e70ece12SIan Lepore 	int sectorsize;
3057f2107d4SMarius Strobl 	uint32_t jedec;
3067f2107d4SMarius Strobl 	uint16_t pagesize;
3079b9a1641SIan Lepore 	uint8_t status;
308674a9140SWarner Losh 
3097f2107d4SMarius Strobl 	sc = xsc;
3107f2107d4SMarius Strobl 	ident = NULL;
3117f2107d4SMarius Strobl 	jedec = 0;
3127f2107d4SMarius Strobl 
3133ea8b6b3SIan Lepore 	if (at45d_wait_ready(sc->dev, &status) != 0) {
3147f2107d4SMarius Strobl 		device_printf(sc->dev, "Error waiting for device-ready.\n");
3153ea8b6b3SIan Lepore 		return;
3163ea8b6b3SIan Lepore 	}
3179b9a1641SIan Lepore 	if (at45d_get_mfg_info(sc->dev, &mfginfo) != 0) {
3187f2107d4SMarius Strobl 		device_printf(sc->dev, "Failed to get ID.\n");
3193ea8b6b3SIan Lepore 		return;
3203ea8b6b3SIan Lepore 	}
3217f2107d4SMarius Strobl 	for (i = 0; i < nitems(at45d_flash_devices); i++) {
3227f2107d4SMarius Strobl 		ident = &at45d_flash_devices[i];
3239b9a1641SIan Lepore 		if (mfginfo.jedec_id == ident->jedec &&
3249b9a1641SIan Lepore 		    (mfginfo.ext_id & ident->extmask) == ident->extid) {
3257f2107d4SMarius Strobl 			break;
3267f2107d4SMarius Strobl 		}
3277f2107d4SMarius Strobl 	}
3289b9a1641SIan Lepore 	if (i == nitems(at45d_flash_devices)) {
3297f2107d4SMarius Strobl 		device_printf(sc->dev, "JEDEC 0x%x not in list.\n", jedec);
3303ea8b6b3SIan Lepore 		return;
3313ea8b6b3SIan Lepore 	}
3323ea8b6b3SIan Lepore 
3337f2107d4SMarius Strobl 	sc->pagecount = ident->pagecount;
3347f2107d4SMarius Strobl 	sc->pageoffset = ident->pageoffset;
3352274a2f7SIan Lepore 	if (ident->pagesize2n != 0 && (status & STATUS_PAGE2N)) {
3367f2107d4SMarius Strobl 		sc->pageoffset -= 1;
3377f2107d4SMarius Strobl 		pagesize = ident->pagesize2n;
3387f2107d4SMarius Strobl 	} else
3397f2107d4SMarius Strobl 		pagesize = ident->pagesize;
3407f2107d4SMarius Strobl 	sc->pagesize = pagesize;
341674a9140SWarner Losh 
342e70ece12SIan Lepore 	/*
343e70ece12SIan Lepore 	 * By default we set up a disk with a sector size that matches the
344e70ece12SIan Lepore 	 * device page size.  If there is a device hint or fdt property
345e70ece12SIan Lepore 	 * requesting a different size, use that, as long as it is a multiple of
346e70ece12SIan Lepore 	 * the device page size).
347e70ece12SIan Lepore 	 */
348e70ece12SIan Lepore 	sectorsize = pagesize;
349e70ece12SIan Lepore #ifdef FDT
350e70ece12SIan Lepore 	{
351e70ece12SIan Lepore 		pcell_t size;
352e70ece12SIan Lepore 		if (OF_getencprop(ofw_bus_get_node(sc->dev),
353e70ece12SIan Lepore 		    "freebsd,sectorsize", &size, sizeof(size)) > 0)
354e70ece12SIan Lepore 			sectorsize = size;
355e70ece12SIan Lepore 	}
356e70ece12SIan Lepore #endif
357e70ece12SIan Lepore 	resource_int_value(device_get_name(sc->dev), device_get_unit(sc->dev),
358e70ece12SIan Lepore 	    "sectorsize", &sectorsize);
359e70ece12SIan Lepore 
360e70ece12SIan Lepore 	if ((sectorsize % pagesize) != 0) {
361e70ece12SIan Lepore 		device_printf(sc->dev, "Invalid sectorsize %d, "
362e70ece12SIan Lepore 		    "must be a multiple of %d\n", sectorsize, pagesize);
363e70ece12SIan Lepore 		return;
364e70ece12SIan Lepore 	}
365e70ece12SIan Lepore 
366d4249d08SIan Lepore 	sc->dummybuf = malloc(pagesize, M_DEVBUF, M_WAITOK | M_ZERO);
367d4249d08SIan Lepore 
368674a9140SWarner Losh 	sc->disk = disk_alloc();
369674a9140SWarner Losh 	sc->disk->d_open = at45d_open;
370674a9140SWarner Losh 	sc->disk->d_close = at45d_close;
371674a9140SWarner Losh 	sc->disk->d_strategy = at45d_strategy;
372fb019471SIan Lepore 	sc->disk->d_getattr = at45d_getattr;
373d3248c41SIan Lepore 	sc->disk->d_name = "flash/at45d";
374674a9140SWarner Losh 	sc->disk->d_drv1 = sc;
375674a9140SWarner Losh 	sc->disk->d_maxsize = DFLTPHYS;
376e70ece12SIan Lepore 	sc->disk->d_sectorsize = sectorsize;
3777f2107d4SMarius Strobl 	sc->disk->d_mediasize = pagesize * ident->pagecount;
378674a9140SWarner Losh 	sc->disk->d_unit = device_get_unit(sc->dev);
379674a9140SWarner Losh 	disk_create(sc->disk, DISK_VERSION);
380674a9140SWarner Losh 	bioq_init(&sc->bio_queue);
3813ea8b6b3SIan Lepore 	kproc_create(&at45d_task, sc, &sc->p, 0, 0, "task: at45d flash");
3824b06839fSIan Lepore 	sc->taskstate = TSTATE_RUNNING;
383e70ece12SIan Lepore 	device_printf(sc->dev,
384e70ece12SIan Lepore 	    "%s, %d bytes per page, %d pages; %d KBytes; disk sector size %d\n",
3859b9a1641SIan Lepore 	    ident->name, pagesize, ident->pagecount,
386e70ece12SIan Lepore 	    (pagesize * ident->pagecount) / 1024, sectorsize);
3877f2107d4SMarius Strobl }
388674a9140SWarner Losh 
389674a9140SWarner Losh static int
at45d_open(struct disk * dp)390674a9140SWarner Losh at45d_open(struct disk *dp)
391674a9140SWarner Losh {
3927f2107d4SMarius Strobl 
3937f2107d4SMarius Strobl 	return (0);
394674a9140SWarner Losh }
395674a9140SWarner Losh 
396674a9140SWarner Losh static int
at45d_close(struct disk * dp)397674a9140SWarner Losh at45d_close(struct disk *dp)
398674a9140SWarner Losh {
3997f2107d4SMarius Strobl 
4007f2107d4SMarius Strobl 	return (0);
401674a9140SWarner Losh }
402674a9140SWarner Losh 
403fb019471SIan Lepore static int
at45d_getattr(struct bio * bp)404fb019471SIan Lepore at45d_getattr(struct bio *bp)
405fb019471SIan Lepore {
406fb019471SIan Lepore 	struct at45d_softc *sc;
407fb019471SIan Lepore 
4082274a2f7SIan Lepore 	/*
4092274a2f7SIan Lepore 	 * This function exists to support geom_flashmap and fdt_slicer.
4102274a2f7SIan Lepore 	 */
4112274a2f7SIan Lepore 
412fb019471SIan Lepore 	if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
413fb019471SIan Lepore 		return (ENXIO);
414fb019471SIan Lepore 	if (strcmp(bp->bio_attribute, "SPI::device") != 0)
415fb019471SIan Lepore 		return (-1);
416fb019471SIan Lepore 	sc = bp->bio_disk->d_drv1;
417fb019471SIan Lepore 	if (bp->bio_length != sizeof(sc->dev))
418fb019471SIan Lepore 		return (EFAULT);
419fb019471SIan Lepore 	bcopy(&sc->dev, bp->bio_data, sizeof(sc->dev));
420fb019471SIan Lepore 	return (0);
421fb019471SIan Lepore }
422fb019471SIan Lepore 
423674a9140SWarner Losh static void
at45d_strategy(struct bio * bp)424674a9140SWarner Losh at45d_strategy(struct bio *bp)
425674a9140SWarner Losh {
426674a9140SWarner Losh 	struct at45d_softc *sc;
427674a9140SWarner Losh 
428674a9140SWarner Losh 	sc = (struct at45d_softc *)bp->bio_disk->d_drv1;
429674a9140SWarner Losh 	AT45D_LOCK(sc);
430674a9140SWarner Losh 	bioq_disksort(&sc->bio_queue, bp);
431674a9140SWarner Losh 	wakeup(sc);
432674a9140SWarner Losh 	AT45D_UNLOCK(sc);
433674a9140SWarner Losh }
434674a9140SWarner Losh 
435674a9140SWarner Losh static void
at45d_task(void * arg)436674a9140SWarner Losh at45d_task(void *arg)
437674a9140SWarner Losh {
4387f2107d4SMarius Strobl 	uint8_t rxBuf[8], txBuf[8];
4397f2107d4SMarius Strobl 	struct at45d_softc *sc;
440674a9140SWarner Losh 	struct bio *bp;
441c445ffceSAdrian Chadd 	struct spi_command cmd;
442674a9140SWarner Losh 	device_t dev, pdev;
4437f2107d4SMarius Strobl 	caddr_t buf;
4447f2107d4SMarius Strobl 	u_long len, resid;
4457f2107d4SMarius Strobl 	u_int addr, berr, err, offset, page;
4467f2107d4SMarius Strobl 	uint8_t status;
447674a9140SWarner Losh 
4487f2107d4SMarius Strobl 	sc = (struct at45d_softc*)arg;
449674a9140SWarner Losh 	dev = sc->dev;
450674a9140SWarner Losh 	pdev = device_get_parent(dev);
4517f2107d4SMarius Strobl 	memset(&cmd, 0, sizeof(cmd));
4527f2107d4SMarius Strobl 	memset(txBuf, 0, sizeof(txBuf));
4537f2107d4SMarius Strobl 	memset(rxBuf, 0, sizeof(rxBuf));
4547f2107d4SMarius Strobl 	cmd.tx_cmd = txBuf;
4557f2107d4SMarius Strobl 	cmd.rx_cmd = rxBuf;
4567f2107d4SMarius Strobl 
4577f2107d4SMarius Strobl 	for (;;) {
458674a9140SWarner Losh 		AT45D_LOCK(sc);
459674a9140SWarner Losh 		do {
4604b06839fSIan Lepore 			if (sc->taskstate == TSTATE_STOPPING) {
4614b06839fSIan Lepore 				sc->taskstate = TSTATE_STOPPED;
4624b06839fSIan Lepore 				AT45D_UNLOCK(sc);
4634b06839fSIan Lepore 				wakeup(sc);
4644b06839fSIan Lepore 				kproc_exit(0);
4654b06839fSIan Lepore 			}
4667f2107d4SMarius Strobl 			bp = bioq_takefirst(&sc->bio_queue);
467674a9140SWarner Losh 			if (bp == NULL)
4682274a2f7SIan Lepore 				msleep(sc, &sc->sc_mtx, PRIBIO, "at45dq", 0);
469674a9140SWarner Losh 		} while (bp == NULL);
470674a9140SWarner Losh 		AT45D_UNLOCK(sc);
4717f2107d4SMarius Strobl 
4727f2107d4SMarius Strobl 		berr = 0;
4737f2107d4SMarius Strobl 		buf = bp->bio_data;
4747f2107d4SMarius Strobl 		len = resid = bp->bio_bcount;
4757f2107d4SMarius Strobl 		page = bp->bio_offset / sc->pagesize;
4767f2107d4SMarius Strobl 		offset = bp->bio_offset % sc->pagesize;
4777f2107d4SMarius Strobl 
4787f2107d4SMarius Strobl 		switch (bp->bio_cmd) {
4797f2107d4SMarius Strobl 		case BIO_READ:
4807f2107d4SMarius Strobl 			txBuf[0] = CONTINUOUS_ARRAY_READ;
4817f2107d4SMarius Strobl 			cmd.tx_cmd_sz = cmd.rx_cmd_sz = 8;
482d4249d08SIan Lepore 			cmd.tx_data = sc->dummybuf;
483d4249d08SIan Lepore 			cmd.rx_data = buf;
4847f2107d4SMarius Strobl 			break;
4857f2107d4SMarius Strobl 		case BIO_WRITE:
4867f2107d4SMarius Strobl 			cmd.tx_cmd_sz = cmd.rx_cmd_sz = 4;
487d4249d08SIan Lepore 			cmd.tx_data = buf;
488d4249d08SIan Lepore 			cmd.rx_data = sc->dummybuf;
4897f2107d4SMarius Strobl 			if (resid + offset > sc->pagesize)
4907f2107d4SMarius Strobl 				len = sc->pagesize - offset;
4917f2107d4SMarius Strobl 			break;
4927f2107d4SMarius Strobl 		default:
493d176b803SScott Long 			berr = EOPNOTSUPP;
4947f2107d4SMarius Strobl 			goto out;
495674a9140SWarner Losh 		}
4967f2107d4SMarius Strobl 
4977f2107d4SMarius Strobl 		/*
4987f2107d4SMarius Strobl 		 * NB: for BIO_READ, this loop is only traversed once.
4997f2107d4SMarius Strobl 		 */
5007f2107d4SMarius Strobl 		while (resid > 0) {
5017f2107d4SMarius Strobl 			if (page > sc->pagecount) {
5027f2107d4SMarius Strobl 				berr = EINVAL;
5037f2107d4SMarius Strobl 				goto out;
5047f2107d4SMarius Strobl 			}
5057f2107d4SMarius Strobl 			addr = page << sc->pageoffset;
5067f2107d4SMarius Strobl 			if (bp->bio_cmd == BIO_WRITE) {
5072274a2f7SIan Lepore 				/*
5082274a2f7SIan Lepore 				 * If writing less than a full page, transfer
5092274a2f7SIan Lepore 				 * the existing page to the buffer, so that our
5102274a2f7SIan Lepore 				 * PROGRAM_THROUGH_BUFFER below will preserve
5112274a2f7SIan Lepore 				 * the parts of the page we're not writing.
5122274a2f7SIan Lepore 				 */
5137f2107d4SMarius Strobl 				if (len != sc->pagesize) {
5142274a2f7SIan Lepore 					txBuf[0] = PAGE_TO_BUFFER_TRANSFER;
5157f2107d4SMarius Strobl 					txBuf[1] = ((addr >> 16) & 0xff);
5167f2107d4SMarius Strobl 					txBuf[2] = ((addr >> 8) & 0xff);
517674a9140SWarner Losh 					txBuf[3] = 0;
5187f2107d4SMarius Strobl 					cmd.tx_data_sz = cmd.rx_data_sz = 0;
5192274a2f7SIan Lepore 					err = SPIBUS_TRANSFER(pdev, dev, &cmd);
5207f2107d4SMarius Strobl 					if (err == 0)
5217f2107d4SMarius Strobl 						err = at45d_wait_ready(dev,
5227f2107d4SMarius Strobl 						    &status);
5237f2107d4SMarius Strobl 					if (err != 0) {
5247f2107d4SMarius Strobl 						berr = EIO;
5257f2107d4SMarius Strobl 						goto out;
526674a9140SWarner Losh 					}
5277f2107d4SMarius Strobl 				}
5287f2107d4SMarius Strobl 				txBuf[0] = PROGRAM_THROUGH_BUFFER;
5297f2107d4SMarius Strobl 			}
5307f2107d4SMarius Strobl 
5317f2107d4SMarius Strobl 			addr += offset;
5327f2107d4SMarius Strobl 			txBuf[1] = ((addr >> 16) & 0xff);
5337f2107d4SMarius Strobl 			txBuf[2] = ((addr >> 8) & 0xff);
5347f2107d4SMarius Strobl 			txBuf[3] = (addr & 0xff);
5357f2107d4SMarius Strobl 			cmd.tx_data_sz = cmd.rx_data_sz = len;
5367f2107d4SMarius Strobl 			err = SPIBUS_TRANSFER(pdev, dev, &cmd);
5377f2107d4SMarius Strobl 			if (err == 0 && bp->bio_cmd != BIO_READ)
5387f2107d4SMarius Strobl 				err = at45d_wait_ready(dev, &status);
5397f2107d4SMarius Strobl 			if (err != 0) {
5407f2107d4SMarius Strobl 				berr = EIO;
5417f2107d4SMarius Strobl 				goto out;
5427f2107d4SMarius Strobl 			}
5437f2107d4SMarius Strobl 			if (bp->bio_cmd == BIO_WRITE) {
5447f2107d4SMarius Strobl 				addr = page << sc->pageoffset;
5452274a2f7SIan Lepore 				txBuf[0] = PAGE_TO_BUFFER_COMPARE;
5467f2107d4SMarius Strobl 				txBuf[1] = ((addr >> 16) & 0xff);
5477f2107d4SMarius Strobl 				txBuf[2] = ((addr >> 8) & 0xff);
5487f2107d4SMarius Strobl 				txBuf[3] = 0;
5497f2107d4SMarius Strobl 				cmd.tx_data_sz = cmd.rx_data_sz = 0;
5507f2107d4SMarius Strobl 				err = SPIBUS_TRANSFER(pdev, dev, &cmd);
5517f2107d4SMarius Strobl 				if (err == 0)
5527f2107d4SMarius Strobl 					err = at45d_wait_ready(dev, &status);
5532274a2f7SIan Lepore 				if (err != 0 || (status & STATUS_CMPFAIL)) {
5547f2107d4SMarius Strobl 					device_printf(dev, "comparing page "
5552274a2f7SIan Lepore 					    "%d failed (status=0x%x)\n", page,
5567f2107d4SMarius Strobl 					    status);
5577f2107d4SMarius Strobl 					berr = EIO;
5587f2107d4SMarius Strobl 					goto out;
5597f2107d4SMarius Strobl 				}
5607f2107d4SMarius Strobl 			}
5617f2107d4SMarius Strobl 			page++;
5627f2107d4SMarius Strobl 			buf += len;
5637f2107d4SMarius Strobl 			offset = 0;
5647f2107d4SMarius Strobl 			resid -= len;
5657f2107d4SMarius Strobl 			if (resid > sc->pagesize)
5667f2107d4SMarius Strobl 				len = sc->pagesize;
5677f2107d4SMarius Strobl 			else
5687f2107d4SMarius Strobl 				len = resid;
569d4249d08SIan Lepore 			if (bp->bio_cmd == BIO_READ)
570d4249d08SIan Lepore 				cmd.rx_data = buf;
571d4249d08SIan Lepore 			else
572d4249d08SIan Lepore 				cmd.tx_data = buf;
5737f2107d4SMarius Strobl 		}
5747f2107d4SMarius Strobl  out:
5757f2107d4SMarius Strobl 		if (berr != 0) {
5767f2107d4SMarius Strobl 			bp->bio_flags |= BIO_ERROR;
5777f2107d4SMarius Strobl 			bp->bio_error = berr;
5787f2107d4SMarius Strobl 		}
5797f2107d4SMarius Strobl 		bp->bio_resid = resid;
580674a9140SWarner Losh 		biodone(bp);
581674a9140SWarner Losh 	}
582674a9140SWarner Losh }
583674a9140SWarner Losh 
584674a9140SWarner Losh static device_method_t at45d_methods[] = {
585674a9140SWarner Losh 	/* Device interface */
586674a9140SWarner Losh 	DEVMETHOD(device_probe,		at45d_probe),
587674a9140SWarner Losh 	DEVMETHOD(device_attach,	at45d_attach),
588674a9140SWarner Losh 	DEVMETHOD(device_detach,	at45d_detach),
589674a9140SWarner Losh 
5907f2107d4SMarius Strobl 	DEVMETHOD_END
591674a9140SWarner Losh };
592674a9140SWarner Losh 
593674a9140SWarner Losh static driver_t at45d_driver = {
594674a9140SWarner Losh 	"at45d",
595674a9140SWarner Losh 	at45d_methods,
596674a9140SWarner Losh 	sizeof(struct at45d_softc),
597674a9140SWarner Losh };
598674a9140SWarner Losh 
5990d95fe04SJohn Baldwin DRIVER_MODULE(at45d, spibus, at45d_driver, NULL, NULL);
60042c52f36SIan Lepore MODULE_DEPEND(at45d, spibus, 1, 1, 1);
60133ec975bSIan Lepore #ifdef FDT
60233ec975bSIan Lepore MODULE_DEPEND(at45d, fdt_slicer, 1, 1, 1);
603db63d251SIan Lepore SPIBUS_FDT_PNP_INFO(compat_data);
60433ec975bSIan Lepore #endif
60533ec975bSIan Lepore 
606