m25p.c revision 1.1.8.2 1 1.1.8.2 yamt /* $NetBSD: m25p.c,v 1.1.8.2 2006/12/30 20:49:37 yamt Exp $ */
2 1.1.8.2 yamt
3 1.1.8.2 yamt /*-
4 1.1.8.2 yamt * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
5 1.1.8.2 yamt * Copyright (c) 2006 Garrett D'Amore.
6 1.1.8.2 yamt * All rights reserved.
7 1.1.8.2 yamt *
8 1.1.8.2 yamt * Portions of this code were written by Garrett D'Amore for the
9 1.1.8.2 yamt * Champaign-Urbana Community Wireless Network Project.
10 1.1.8.2 yamt *
11 1.1.8.2 yamt * Redistribution and use in source and binary forms, with or
12 1.1.8.2 yamt * without modification, are permitted provided that the following
13 1.1.8.2 yamt * conditions are met:
14 1.1.8.2 yamt * 1. Redistributions of source code must retain the above copyright
15 1.1.8.2 yamt * notice, this list of conditions and the following disclaimer.
16 1.1.8.2 yamt * 2. Redistributions in binary form must reproduce the above
17 1.1.8.2 yamt * copyright notice, this list of conditions and the following
18 1.1.8.2 yamt * disclaimer in the documentation and/or other materials provided
19 1.1.8.2 yamt * with the distribution.
20 1.1.8.2 yamt * 3. All advertising materials mentioning features or use of this
21 1.1.8.2 yamt * software must display the following acknowledgements:
22 1.1.8.2 yamt * This product includes software developed by the Urbana-Champaign
23 1.1.8.2 yamt * Independent Media Center.
24 1.1.8.2 yamt * This product includes software developed by Garrett D'Amore.
25 1.1.8.2 yamt * 4. Urbana-Champaign Independent Media Center's name and Garrett
26 1.1.8.2 yamt * D'Amore's name may not be used to endorse or promote products
27 1.1.8.2 yamt * derived from this software without specific prior written permission.
28 1.1.8.2 yamt *
29 1.1.8.2 yamt * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
30 1.1.8.2 yamt * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
31 1.1.8.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 1.1.8.2 yamt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.1.8.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
34 1.1.8.2 yamt * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
35 1.1.8.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 1.1.8.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 1.1.8.2 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 1.1.8.2 yamt * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1.8.2 yamt * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 1.1.8.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 1.1.8.2 yamt * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 1.1.8.2 yamt */
43 1.1.8.2 yamt
44 1.1.8.2 yamt #include <sys/cdefs.h>
45 1.1.8.2 yamt __KERNEL_RCSID(0, "$NetBSD: m25p.c,v 1.1.8.2 2006/12/30 20:49:37 yamt Exp $");
46 1.1.8.2 yamt
47 1.1.8.2 yamt #include <sys/param.h>
48 1.1.8.2 yamt #include <sys/systm.h>
49 1.1.8.2 yamt #include <sys/device.h>
50 1.1.8.2 yamt #include <sys/kernel.h>
51 1.1.8.2 yamt
52 1.1.8.2 yamt #include <dev/sysmon/sysmonvar.h>
53 1.1.8.2 yamt
54 1.1.8.2 yamt #include <dev/spi/spivar.h>
55 1.1.8.2 yamt #include <dev/spi/spiflash.h>
56 1.1.8.2 yamt
57 1.1.8.2 yamt /*
58 1.1.8.2 yamt * Device driver for STMicroelectronics M25P Family SPI Flash Devices
59 1.1.8.2 yamt */
60 1.1.8.2 yamt
61 1.1.8.2 yamt static int m25p_match(struct device *, struct cfdata *, void *);
62 1.1.8.2 yamt static void m25p_attach(struct device *, struct device *, void *);
63 1.1.8.2 yamt static const char *m25p_getname(void *);
64 1.1.8.2 yamt static struct spi_handle *m25p_gethandle(void *);
65 1.1.8.2 yamt static int m25p_getflags(void *);
66 1.1.8.2 yamt static int m25p_getsize(void *, int);
67 1.1.8.2 yamt
68 1.1.8.2 yamt struct m25p_softc {
69 1.1.8.2 yamt struct device sc_dev;
70 1.1.8.2 yamt struct spi_handle *sc_sh;
71 1.1.8.2 yamt const char *sc_name;
72 1.1.8.2 yamt int sc_sizes[SPIFLASH_SIZE_COUNT];
73 1.1.8.2 yamt int sc_flags;
74 1.1.8.2 yamt };
75 1.1.8.2 yamt
76 1.1.8.2 yamt CFATTACH_DECL(m25p, sizeof(struct m25p_softc),
77 1.1.8.2 yamt m25p_match, m25p_attach, NULL, NULL);
78 1.1.8.2 yamt
79 1.1.8.2 yamt static const struct spiflash_hw_if m25p_hw_if = {
80 1.1.8.2 yamt .sf_getname = m25p_getname,
81 1.1.8.2 yamt .sf_gethandle = m25p_gethandle,
82 1.1.8.2 yamt .sf_getsize = m25p_getsize,
83 1.1.8.2 yamt .sf_getflags = m25p_getflags,
84 1.1.8.2 yamt };
85 1.1.8.2 yamt
86 1.1.8.2 yamt static const struct m25p_info {
87 1.1.8.2 yamt uint8_t sig;
88 1.1.8.2 yamt uint8_t mfgid;
89 1.1.8.2 yamt uint16_t devid;
90 1.1.8.2 yamt const char *name;
91 1.1.8.2 yamt uint16_t size; /* in KB */
92 1.1.8.2 yamt uint16_t sector; /* in KB */
93 1.1.8.2 yamt uint16_t mhz;
94 1.1.8.2 yamt } m25p_infos[] = {
95 1.1.8.2 yamt { 0x16, 0x20, 0x2017, "STMicro M25P64", 8192, 64 }, /* 64Mbit */
96 1.1.8.2 yamt { 0x12, 0x20, 0x2013, "STMicro M25P40", 512, 64 }, /* 4Mbit */
97 1.1.8.2 yamt { 0 }
98 1.1.8.2 yamt };
99 1.1.8.2 yamt
100 1.1.8.2 yamt static int
101 1.1.8.2 yamt m25p_match(struct device *parent, struct cfdata *cf, void *aux)
102 1.1.8.2 yamt {
103 1.1.8.2 yamt struct spi_attach_args *sa = aux;
104 1.1.8.2 yamt
105 1.1.8.2 yamt /* configure for 20MHz, which is the max for normal reads */
106 1.1.8.2 yamt if (spi_configure(sa->sa_handle, SPI_MODE_0, 20000000))
107 1.1.8.2 yamt return 0;
108 1.1.8.2 yamt
109 1.1.8.2 yamt return 1;
110 1.1.8.2 yamt }
111 1.1.8.2 yamt
112 1.1.8.2 yamt static void
113 1.1.8.2 yamt m25p_attach(struct device *parent, struct device *self, void *aux)
114 1.1.8.2 yamt {
115 1.1.8.2 yamt struct m25p_softc *sc = device_private(self);
116 1.1.8.2 yamt struct spi_attach_args *sa = aux;
117 1.1.8.2 yamt const struct m25p_info *info;
118 1.1.8.2 yamt uint8_t buf[4];
119 1.1.8.2 yamt uint8_t cmd;
120 1.1.8.2 yamt uint8_t mfgid;
121 1.1.8.2 yamt uint8_t sig;
122 1.1.8.2 yamt uint16_t devid;
123 1.1.8.2 yamt
124 1.1.8.2 yamt sc->sc_sh = sa->sa_handle;
125 1.1.8.2 yamt
126 1.1.8.2 yamt /* first we try JEDEC ID read */
127 1.1.8.2 yamt
128 1.1.8.2 yamt cmd = SPIFLASH_CMD_RDJI;
129 1.1.8.2 yamt if (spi_send_recv(sa->sa_handle, 1, &cmd, 3, buf)) {
130 1.1.8.2 yamt aprint_error(": failed to get JEDEC identification\n");
131 1.1.8.2 yamt return;
132 1.1.8.2 yamt }
133 1.1.8.2 yamt mfgid = buf[0];
134 1.1.8.2 yamt devid = ((uint16_t)buf[1] << 8) | buf[2];
135 1.1.8.2 yamt
136 1.1.8.2 yamt if ((mfgid == 0xff) || (mfgid == 0)) {
137 1.1.8.2 yamt cmd = SPIFLASH_CMD_RDID;
138 1.1.8.2 yamt if (spi_send_recv(sa->sa_handle, 1, &cmd, 4, buf)) {
139 1.1.8.2 yamt aprint_error(": failed to get legacy signature\n");
140 1.1.8.2 yamt return;
141 1.1.8.2 yamt }
142 1.1.8.2 yamt sig = buf[3];
143 1.1.8.2 yamt } else {
144 1.1.8.2 yamt sig = 0;
145 1.1.8.2 yamt }
146 1.1.8.2 yamt
147 1.1.8.2 yamt /* okay did it match */
148 1.1.8.2 yamt for (info = m25p_infos; info->name; info++) {
149 1.1.8.2 yamt if ((info->mfgid == mfgid) && (info->devid == devid))
150 1.1.8.2 yamt break;
151 1.1.8.2 yamt if (sig == info->sig)
152 1.1.8.2 yamt break;
153 1.1.8.2 yamt }
154 1.1.8.2 yamt
155 1.1.8.2 yamt if (info->name == NULL) {
156 1.1.8.2 yamt aprint_error(": unknown or unsupported device\n");
157 1.1.8.2 yamt return;
158 1.1.8.2 yamt }
159 1.1.8.2 yamt
160 1.1.8.2 yamt sc->sc_name = info->name;
161 1.1.8.2 yamt sc->sc_sh = sa->sa_handle;
162 1.1.8.2 yamt sc->sc_sizes[SPIFLASH_SIZE_DEVICE] = info->size * 1024;
163 1.1.8.2 yamt sc->sc_sizes[SPIFLASH_SIZE_ERASE] = info->sector * 1024;
164 1.1.8.2 yamt sc->sc_sizes[SPIFLASH_SIZE_WRITE] = 256;
165 1.1.8.2 yamt sc->sc_sizes[SPIFLASH_SIZE_READ] = -1;
166 1.1.8.2 yamt
167 1.1.8.2 yamt sc->sc_flags = SPIFLASH_FLAG_FAST_READ;
168 1.1.8.2 yamt
169 1.1.8.2 yamt printf("\n");
170 1.1.8.2 yamt
171 1.1.8.2 yamt spiflash_attach_mi(&m25p_hw_if, sc, self);
172 1.1.8.2 yamt }
173 1.1.8.2 yamt
174 1.1.8.2 yamt const char *
175 1.1.8.2 yamt m25p_getname(void *cookie)
176 1.1.8.2 yamt {
177 1.1.8.2 yamt struct m25p_softc *sc = cookie;
178 1.1.8.2 yamt
179 1.1.8.2 yamt return (sc->sc_name);
180 1.1.8.2 yamt }
181 1.1.8.2 yamt
182 1.1.8.2 yamt struct spi_handle *
183 1.1.8.2 yamt m25p_gethandle(void *cookie)
184 1.1.8.2 yamt {
185 1.1.8.2 yamt struct m25p_softc *sc = cookie;
186 1.1.8.2 yamt
187 1.1.8.2 yamt return (sc->sc_sh);
188 1.1.8.2 yamt }
189 1.1.8.2 yamt
190 1.1.8.2 yamt int
191 1.1.8.2 yamt m25p_getflags(void *cookie)
192 1.1.8.2 yamt {
193 1.1.8.2 yamt struct m25p_softc *sc = cookie;
194 1.1.8.2 yamt
195 1.1.8.2 yamt return (sc->sc_flags);
196 1.1.8.2 yamt }
197 1.1.8.2 yamt
198 1.1.8.2 yamt int
199 1.1.8.2 yamt m25p_getsize(void *cookie, int idx)
200 1.1.8.2 yamt {
201 1.1.8.2 yamt struct m25p_softc *sc = cookie;
202 1.1.8.2 yamt
203 1.1.8.2 yamt if ((idx < 0) || (idx >= SPIFLASH_SIZE_COUNT))
204 1.1.8.2 yamt return -1;
205 1.1.8.2 yamt return (sc->sc_sizes[idx]);
206 1.1.8.2 yamt }
207