xref: /netbsd-src/sys/dev/spi/m25p.c (revision f64c887e97cc4e0fca7184a6ff7742154fef673e)
1 /* $NetBSD: m25p.c,v 1.20 2022/01/19 05:21:44 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
5  * Copyright (c) 2006 Garrett D'Amore.
6  * All rights reserved.
7  *
8  * Portions of this code were written by Garrett D'Amore for the
9  * Champaign-Urbana Community Wireless Network Project.
10  *
11  * Redistribution and use in source and binary forms, with or
12  * without modification, are permitted provided that the following
13  * conditions are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above
17  *    copyright notice, this list of conditions and the following
18  *    disclaimer in the documentation and/or other materials provided
19  *    with the distribution.
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgements:
22  *      This product includes software developed by the Urbana-Champaign
23  *      Independent Media Center.
24  *	This product includes software developed by Garrett D'Amore.
25  * 4. Urbana-Champaign Independent Media Center's name and Garrett
26  *    D'Amore's name may not be used to endorse or promote products
27  *    derived from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
30  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
31  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
34  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
35  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: m25p.c,v 1.20 2022/01/19 05:21:44 thorpej Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/device.h>
50 #include <sys/kernel.h>
51 
52 #include <dev/sysmon/sysmonvar.h>
53 
54 #include <dev/spi/spivar.h>
55 #include <dev/spi/spiflash.h>
56 
57 /*
58  * Device driver for STMicroelectronics M25P Family SPI Flash Devices
59  */
60 
61 static int m25p_match(device_t , cfdata_t , void *);
62 static void m25p_attach(device_t , device_t , void *);
63 static void m25p_doattach(device_t);
64 static const char *m25p_getname(void *);
65 static struct spi_handle *m25p_gethandle(void *);
66 static int m25p_getflags(void *);
67 static int m25p_getsize(void *, int);
68 
69 struct m25p_softc {
70 	struct spi_handle	*sc_sh;
71 	const char		*sc_name;
72 	int			sc_sizes[SPIFLASH_SIZE_COUNT];
73 	int			sc_flags;
74 };
75 
76 CFATTACH_DECL_NEW(m25p, sizeof(struct m25p_softc),
77     m25p_match, m25p_attach, NULL, NULL);
78 
79 static const struct spiflash_hw_if m25p_hw_if = {
80 	.sf_getname = m25p_getname,
81 	.sf_gethandle = m25p_gethandle,
82 	.sf_getsize = m25p_getsize,
83 	.sf_getflags = m25p_getflags,
84 };
85 
86 static const struct m25p_info {
87 	uint8_t		sig;
88 	uint8_t		mfgid;
89 	uint16_t	devid;
90 	const char	*name;
91 	uint16_t	size;	/* in KB */
92 	uint16_t	sector;	/* in KB */
93 	uint16_t	mhz;
94 } m25p_infos[] = {
95 	{ 0x16, 0x20, 0x2017, "STMicro M25P64", 8192, 64 },	/* 64Mbit */
96 	{ 0x14, 0x20, 0x2015, "STMicro M25P16", 2048, 64 },	/* 16Mbit */
97 	{ 0x12, 0x20, 0x2013, "STMicro M25P40", 512, 64 },	/* 4Mbit */
98 	{ 0xc0, 0x20, 0x7117, "STMicro M25PX64", 8192, 64 },	/* 64Mbit */
99 	{ 0x00, 0x20, 0xBA18, "Micron MT25QL128", 16384, 64 },  /* 128Mbit (3V) */
100 	{ 0x00, 0x20, 0xBB18, "Micron MT25QU128", 16384, 64 },  /* 128Mbit (1.8V)  */
101 	{ 0x00, 0xBF, 0x2541, "Microchip SST25VF016B", 2048, 64 }, /* 16Mbit */
102 	{ 0x00, 0xC2, 0x2011, "Macronix MX25L10", 128, 64 },	/* 1Mbit */
103 	{ 0x00, 0xC2, 0x2012, "Macronix MX25L20", 256, 64 },	/* 2Mbit */
104 	{ 0x00, 0xC2, 0x2013, "Macronix MX25L40", 512, 64 },	/* 4Mbit */
105 	{ 0x00, 0xC2, 0x2014, "Macronix MX25L80", 1024, 64 },	/* 8Mbit */
106 	{ 0x00, 0xC8, 0x4018, "GigaDevice 25Q127CSIG", 16384, 64 },	/* 128Mbit */
107 	{ 0x00, 0xEF, 0x3011, "Winbond W25X10", 128, 64 },	/* 1Mbit */
108 	{ 0x00, 0xEF, 0x3012, "Winbond W25X20", 256, 64 },	/* 2Mbit */
109 	{ 0x00, 0xEF, 0x3013, "Winbond W25X40", 512, 64 },	/* 4Mbit */
110 	{ 0x00, 0xEF, 0x3014, "Winbond W25X80", 1024, 64 },	/* 8Mbit */
111 	{ 0x13, 0xEF, 0x4014, "Winbond W25Q80.V", 1024, 64 },	/* 8Mbit */
112 	{ 0x14, 0xEF, 0x4015, "Winbond W25Q16.V", 2048, 64 },	/* 16Mbit */
113 	{ 0x15, 0xEF, 0x4016, "Winbond W25Q32.V", 4096, 64 },	/* 32Mbit */
114 	{ 0x15, 0xEF, 0x4018, "Winbond W25Q128.V", 16384, 64 },	/* 128Mbit */
115 	{ 0x15, 0xEF, 0x6016, "Winbond W25Q32.W", 4096, 64 },	/* 32Mbit */
116 	{ 0 }
117 };
118 
119 static const struct device_compatible_entry compat_data[] = {
120 	{ .compat = "jedec,spi-nor" },
121 	DEVICE_COMPAT_EOL
122 };
123 
124 static int
m25p_match(device_t parent,cfdata_t cf,void * aux)125 m25p_match(device_t parent, cfdata_t cf, void *aux)
126 {
127 	struct spi_attach_args *sa = aux;
128 
129 	return spi_compatible_match(sa, cf, compat_data);
130 }
131 
132 static void
m25p_attach(device_t parent,device_t self,void * aux)133 m25p_attach(device_t parent, device_t self, void *aux)
134 {
135 	struct m25p_softc *sc = device_private(self);
136 	struct spi_attach_args *sa = aux;
137 	int error;
138 
139 	sc->sc_sh = sa->sa_handle;
140 
141 	aprint_normal("\n");
142 	aprint_naive("\n");
143 
144 	/* configure for 20MHz, which is the max for normal reads */
145 	error = spi_configure(self, sa->sa_handle, SPI_MODE_0, 20000000);
146 	if (error) {
147 		return;
148 	}
149 
150 	config_interrupts(self, m25p_doattach);
151 }
152 
153 static void
m25p_doattach(device_t self)154 m25p_doattach(device_t self)
155 {
156 	struct m25p_softc *sc = device_private(self);
157 	const struct m25p_info *info;
158 	uint8_t	buf[4];
159 	uint8_t	cmd;
160 	uint8_t mfgid;
161 	uint8_t sig;
162 	uint16_t devid;
163 
164 	/* first we try JEDEC ID read */
165 	cmd = SPIFLASH_CMD_RDJI;
166 	if (spi_send_recv(sc->sc_sh, 1, &cmd, 3, buf)) {
167 		aprint_error_dev(self, "failed to get JEDEC identification\n");
168 		return;
169 	}
170 	mfgid = buf[0];
171 	devid = ((uint16_t)buf[1] << 8) | buf[2];
172 
173 	if ((mfgid == 0xff) || (mfgid == 0)) {
174 		cmd = SPIFLASH_CMD_RDID;
175 		if (spi_send_recv(sc->sc_sh, 1, &cmd, 4, buf)) {
176 			aprint_error_dev(self,
177 			    "failed to get legacy signature\n");
178 			return;
179 		}
180 		sig = buf[3];
181 	} else {
182 		sig = 0;
183 	}
184 
185 	/* okay did it match */
186 	for (info = m25p_infos; info->name; info++) {
187 		if ((info->mfgid == mfgid) && (info->devid == devid))
188 			break;
189 		if ((sig != 0) && (sig == info->sig))
190 			break;
191 	}
192 
193 	if (info->name == NULL) {
194 		aprint_error_dev(self,
195 		    "vendor 0x%02X dev 0x%04X sig 0x%02X not supported\n",
196 		     mfgid, devid, sig);
197 		return;
198 	}
199 
200 	sc->sc_name = info->name;
201 	sc->sc_sizes[SPIFLASH_SIZE_DEVICE] = info->size * 1024;
202 	sc->sc_sizes[SPIFLASH_SIZE_ERASE] = info->sector * 1024;
203 	sc->sc_sizes[SPIFLASH_SIZE_WRITE] = 256;
204 	sc->sc_sizes[SPIFLASH_SIZE_READ] = -1;
205 
206 	sc->sc_flags = SPIFLASH_FLAG_FAST_READ;
207 
208 	aprint_normal_dev(self, "JEDEC ID mfgid:0x%02X, devid:0x%04X",
209 	    mfgid, devid);
210 	aprint_normal("\n");
211 
212 	spiflash_attach_mi(&m25p_hw_if, sc, self);
213 }
214 
215 const char *
m25p_getname(void * cookie)216 m25p_getname(void *cookie)
217 {
218 	struct m25p_softc *sc = cookie;
219 
220 	return (sc->sc_name);
221 }
222 
223 struct spi_handle *
m25p_gethandle(void * cookie)224 m25p_gethandle(void *cookie)
225 {
226 	struct m25p_softc *sc = cookie;
227 
228 	return (sc->sc_sh);
229 }
230 
231 int
m25p_getflags(void * cookie)232 m25p_getflags(void *cookie)
233 {
234 	struct m25p_softc *sc = cookie;
235 
236 	return (sc->sc_flags);
237 }
238 
239 int
m25p_getsize(void * cookie,int idx)240 m25p_getsize(void *cookie, int idx)
241 {
242 	struct m25p_softc *sc = cookie;
243 
244 	if ((idx < 0) || (idx >= SPIFLASH_SIZE_COUNT))
245 		return -1;
246 	return (sc->sc_sizes[idx]);
247 }
248