xref: /netbsd-src/sys/dev/spi/m25p.c (revision 6cf6fe02a981b55727c49c3d37b0d8191a98c0ee)
1 /* $NetBSD: m25p.c,v 1.4 2013/10/26 15:18:21 rkujawa 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.4 2013/10/26 15:18:21 rkujawa 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 const char *m25p_getname(void *);
64 static struct spi_handle *m25p_gethandle(void *);
65 static int m25p_getflags(void *);
66 static int m25p_getsize(void *, int);
67 
68 struct m25p_softc {
69 	struct spi_handle	*sc_sh;
70 	const char		*sc_name;
71 	int			sc_sizes[SPIFLASH_SIZE_COUNT];
72 	int			sc_flags;
73 };
74 
75 CFATTACH_DECL_NEW(m25p, sizeof(struct m25p_softc),
76     m25p_match, m25p_attach, NULL, NULL);
77 
78 static const struct spiflash_hw_if m25p_hw_if = {
79 	.sf_getname = m25p_getname,
80 	.sf_gethandle = m25p_gethandle,
81 	.sf_getsize = m25p_getsize,
82 	.sf_getflags = m25p_getflags,
83 };
84 
85 static const struct m25p_info {
86 	uint8_t		sig;
87 	uint8_t		mfgid;
88 	uint16_t	devid;
89 	const char	*name;
90 	uint16_t	size;	/* in KB */
91 	uint16_t	sector;	/* in KB */
92 	uint16_t	mhz;
93 } m25p_infos[] = {
94 	{ 0x16, 0x20, 0x2017, "STMicro M25P64", 8192, 64 },	/* 64Mbit */
95 	{ 0x14, 0x20, 0x2015, "STMicro M25P16", 2048, 64 },	/* 16Mbit */
96 	{ 0x12,	0x20, 0x2013, "STMicro M25P40", 512, 64 },	/* 4Mbit */
97 	{ 0xc0, 0x20, 0x7117, "STMicro M25PX64", 8192, 64 },	/* 64Mbit */
98 	{ 0x0, 0x20, 0xBB18, "Numonyx N25Q128", 16384, 64 },	/* 128Mbit */
99 	{ 0 }
100 };
101 
102 static int
103 m25p_match(device_t parent, cfdata_t cf, void *aux)
104 {
105 	struct spi_attach_args *sa = aux;
106 
107 	/* configure for 20MHz, which is the max for normal reads */
108 	if (spi_configure(sa->sa_handle, SPI_MODE_0, 20000000))
109 		return 0;
110 
111 	return 1;
112 }
113 
114 static void
115 m25p_attach(device_t parent, device_t self, void *aux)
116 {
117 	struct m25p_softc *sc = device_private(self);
118 	struct spi_attach_args *sa = aux;
119 	const struct m25p_info *info;
120 	uint8_t	 buf[4];
121 	uint8_t	cmd;
122 	uint8_t mfgid;
123 	uint8_t sig;
124 	uint16_t devid;
125 
126 	sc->sc_sh = sa->sa_handle;
127 
128 	/* first we try JEDEC ID read */
129 
130 	cmd = SPIFLASH_CMD_RDJI;
131 	if (spi_send_recv(sa->sa_handle, 1, &cmd, 3, buf)) {
132 		aprint_error(": failed to get JEDEC identification\n");
133 		return;
134 	}
135 	mfgid = buf[0];
136 	devid = ((uint16_t)buf[1] << 8) | buf[2];
137 
138 	if ((mfgid == 0xff) || (mfgid == 0)) {
139 		cmd = SPIFLASH_CMD_RDID;
140 		if (spi_send_recv(sa->sa_handle, 1, &cmd, 4, buf)) {
141 			aprint_error(": failed to get legacy signature\n");
142 			return;
143 		}
144 		sig = buf[3];
145 	} else {
146 		sig = 0;
147 	}
148 
149 	/* okay did it match */
150 	for (info = m25p_infos; info->name; info++) {
151 		if ((info->mfgid == mfgid) && (info->devid == devid))
152 			break;
153 		if (sig == info->sig)
154 			break;
155 	}
156 
157 	if (info->name == NULL) {
158 		aprint_error(": unknown or unsupported device\n");
159 		return;
160 	}
161 
162 	sc->sc_name = info->name;
163 	sc->sc_sh = sa->sa_handle;
164 	sc->sc_sizes[SPIFLASH_SIZE_DEVICE] = info->size * 1024;
165 	sc->sc_sizes[SPIFLASH_SIZE_ERASE] = info->sector * 1024;
166 	sc->sc_sizes[SPIFLASH_SIZE_WRITE] = 256;
167 	sc->sc_sizes[SPIFLASH_SIZE_READ] = -1;
168 
169 	sc->sc_flags = SPIFLASH_FLAG_FAST_READ;
170 
171 	aprint_normal("\n");
172 
173 	spiflash_attach_mi(&m25p_hw_if, sc, self);
174 }
175 
176 const char *
177 m25p_getname(void *cookie)
178 {
179 	struct m25p_softc *sc = cookie;
180 
181 	return (sc->sc_name);
182 }
183 
184 struct spi_handle *
185 m25p_gethandle(void *cookie)
186 {
187 	struct m25p_softc *sc = cookie;
188 
189 	return (sc->sc_sh);
190 }
191 
192 int
193 m25p_getflags(void *cookie)
194 {
195 	struct m25p_softc *sc = cookie;
196 
197 	return (sc->sc_flags);
198 }
199 
200 int
201 m25p_getsize(void *cookie, int idx)
202 {
203 	struct m25p_softc *sc = cookie;
204 
205 	if ((idx < 0) || (idx >= SPIFLASH_SIZE_COUNT))
206 		return -1;
207 	return (sc->sc_sizes[idx]);
208 }
209