flash_vrip.c revision 1.1 1 1.1 igy /* $NetBSD: flash_vrip.c,v 1.1 2003/05/01 07:02:03 igy Exp $ */
2 1.1 igy
3 1.1 igy /*
4 1.1 igy * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 igy * All rights reserved.
6 1.1 igy *
7 1.1 igy * This code is derived from software contributed to The NetBSD Foundation
8 1.1 igy * by Naoto Shimazaki of YOKOGAWA Electric Corporation.
9 1.1 igy *
10 1.1 igy * Redistribution and use in source and binary forms, with or without
11 1.1 igy * modification, are permitted provided that the following conditions
12 1.1 igy * are met:
13 1.1 igy * 1. Redistributions of source code must retain the above copyright
14 1.1 igy * notice, this list of conditions and the following disclaimer.
15 1.1 igy * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 igy * notice, this list of conditions and the following disclaimer in the
17 1.1 igy * documentation and/or other materials provided with the distribution.
18 1.1 igy * 3. All advertising materials mentioning features or use of this software
19 1.1 igy * must display the following acknowledgement:
20 1.1 igy * This product includes software developed by the NetBSD
21 1.1 igy * Foundation, Inc. and its contributors.
22 1.1 igy * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 igy * contributors may be used to endorse or promote products derived
24 1.1 igy * from this software without specific prior written permission.
25 1.1 igy *
26 1.1 igy * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 igy * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 igy * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 igy * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 igy * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 igy * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 igy * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 igy * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 igy * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 igy * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 igy * POSSIBILITY OF SUCH DAMAGE.
37 1.1 igy */
38 1.1 igy
39 1.1 igy /*
40 1.1 igy * Flash Memory Driver
41 1.1 igy */
42 1.1 igy
43 1.1 igy #include <sys/param.h>
44 1.1 igy #include <sys/conf.h>
45 1.1 igy #include <sys/device.h>
46 1.1 igy #include <sys/kernel.h>
47 1.1 igy #include <sys/malloc.h>
48 1.1 igy #include <sys/proc.h>
49 1.1 igy #include <sys/systm.h>
50 1.1 igy
51 1.1 igy #include <machine/bus.h>
52 1.1 igy
53 1.1 igy #include <hpcmips/vr/vripif.h>
54 1.1 igy #include <hpcmips/vr/cfireg.h>
55 1.1 igy #include <hpcmips/vr/flashreg.h>
56 1.1 igy #include <hpcmips/vr/flashvar.h>
57 1.1 igy
58 1.1 igy #ifdef FLASH_DEBUG
59 1.1 igy int flash_debug = 0;
60 1.1 igy #define DPRINTF(x) if (flash_debug) printf x
61 1.1 igy #else
62 1.1 igy #define DPRINTF(x)
63 1.1 igy #endif
64 1.1 igy
65 1.1 igy static int flash_probe(struct device *, struct cfdata *, void *);
66 1.1 igy static void flash_attach(struct device *, struct device *, void *);
67 1.1 igy
68 1.1 igy const static struct flashops * find_command_set(u_int8_t cmdset0,
69 1.1 igy u_int8_t cmdset1);
70 1.1 igy static int i28f128_probe(bus_space_tag_t, bus_space_handle_t);
71 1.1 igy static int mbm29160_probe(bus_space_tag_t, bus_space_handle_t);
72 1.1 igy static int is_block_same(struct flash_softc *, bus_size_t, const void *);
73 1.1 igy static int probe_cfi(bus_space_tag_t iot, bus_space_handle_t ioh);
74 1.1 igy
75 1.1 igy static int intel_erase(struct flash_softc *, bus_size_t);
76 1.1 igy static int intel_write(struct flash_softc *, bus_size_t);
77 1.1 igy static int amd_erase(struct flash_softc *, bus_size_t);
78 1.1 igy static int amd_write(struct flash_softc *, bus_size_t);
79 1.1 igy
80 1.1 igy extern struct cfdriver flash_cd;
81 1.1 igy
82 1.1 igy CFATTACH_DECL(flash_vrip, sizeof(struct flash_softc),
83 1.1 igy flash_probe, flash_attach, NULL, NULL);
84 1.1 igy
85 1.1 igy dev_type_open(flashopen);
86 1.1 igy dev_type_close(flashclose);
87 1.1 igy dev_type_read(flashread);
88 1.1 igy dev_type_write(flashwrite);
89 1.1 igy
90 1.1 igy const struct cdevsw flash_cdevsw = {
91 1.1 igy flashopen, flashclose, flashread, flashwrite, noioctl,
92 1.1 igy nostop, notty, nopoll, nommap, nokqfilter,
93 1.1 igy };
94 1.1 igy
95 1.1 igy static const struct flash_command_set {
96 1.1 igy u_int8_t fc_set0;
97 1.1 igy u_int8_t fc_set1;
98 1.1 igy struct flashops fc_ops;
99 1.1 igy } flash_cmd[] = {
100 1.1 igy {
101 1.1 igy .fc_set0 = CFI_COMMSET_INTEL0,
102 1.1 igy .fc_set1 = CFI_COMMSET_INTEL1,
103 1.1 igy .fc_ops = {
104 1.1 igy .fo_name = "Intel",
105 1.1 igy .fo_erase = intel_erase,
106 1.1 igy .fo_write = intel_write,
107 1.1 igy }
108 1.1 igy },
109 1.1 igy {
110 1.1 igy .fc_set0 = CFI_COMMSET_AMDFJITU0,
111 1.1 igy .fc_set1 = CFI_COMMSET_AMDFJITU1,
112 1.1 igy .fc_ops = {
113 1.1 igy .fo_name = "AMD/Fujitsu",
114 1.1 igy .fo_erase = amd_erase,
115 1.1 igy .fo_write = amd_write,
116 1.1 igy }
117 1.1 igy },
118 1.1 igy {
119 1.1 igy .fc_set0 = 0,
120 1.1 igy .fc_set1 = 0,
121 1.1 igy .fc_ops = {
122 1.1 igy .fo_name = NULL,
123 1.1 igy .fo_erase = NULL,
124 1.1 igy .fo_write = NULL,
125 1.1 igy }
126 1.1 igy }
127 1.1 igy };
128 1.1 igy
129 1.1 igy
130 1.1 igy const static struct flashops *
131 1.1 igy find_command_set(u_int8_t cmdset0, u_int8_t cmdset1)
132 1.1 igy {
133 1.1 igy const struct flash_command_set *fc;
134 1.1 igy
135 1.1 igy for (fc = flash_cmd; fc->fc_ops.fo_name; fc++) {
136 1.1 igy if (cmdset0 == fc->fc_set0 && cmdset1 == fc->fc_set1)
137 1.1 igy return &fc->fc_ops;
138 1.1 igy }
139 1.1 igy return NULL;
140 1.1 igy }
141 1.1 igy
142 1.1 igy static int
143 1.1 igy probe_cfi(bus_space_tag_t iot, bus_space_handle_t ioh)
144 1.1 igy {
145 1.1 igy const u_int8_t *idstr = CFI_QUERY_ID_STR;
146 1.1 igy int i;
147 1.1 igy u_int8_t cmdset0;
148 1.1 igy u_int8_t cmdset1;
149 1.1 igy
150 1.1 igy /* start Common Flash Interface Query */
151 1.1 igy bus_space_write_2(iot, ioh, CFI_QUERY_OFFSET, CFI_READ_CFI_QUERY);
152 1.1 igy
153 1.1 igy /* read CFI Query ID string */
154 1.1 igy i = CFI_QUERY_ID_STR_REG << 1;
155 1.1 igy do {
156 1.1 igy if (bus_space_read_2(iot, ioh, i) != *idstr) {
157 1.1 igy bus_space_write_2(iot, ioh, 0, FLASH_RESET);
158 1.1 igy return 1;
159 1.1 igy }
160 1.1 igy i += 2;
161 1.1 igy idstr++;
162 1.1 igy } while (*idstr);
163 1.1 igy
164 1.1 igy cmdset0 = bus_space_read_2(iot, ioh, CFI_PRIM_COMM_REG0 << 1);
165 1.1 igy cmdset1 = bus_space_read_2(iot, ioh, CFI_PRIM_COMM_REG1 << 1);
166 1.1 igy
167 1.1 igy /* switch flash to read mode */
168 1.1 igy bus_space_write_2(iot, ioh, 0, FLASH_RESET);
169 1.1 igy
170 1.1 igy if (!find_command_set(cmdset0, cmdset1))
171 1.1 igy return 1;
172 1.1 igy
173 1.1 igy return 0;
174 1.1 igy }
175 1.1 igy
176 1.1 igy static int
177 1.1 igy flash_probe(struct device *parent, struct cfdata *match, void *aux)
178 1.1 igy {
179 1.1 igy struct vrip_attach_args *va = aux;
180 1.1 igy bus_space_handle_t ioh;
181 1.1 igy
182 1.1 igy if (bus_space_map(va->va_iot, va->va_addr, va->va_size, 0, &ioh))
183 1.1 igy return 0;
184 1.1 igy if (!probe_cfi(va->va_iot, ioh)) {
185 1.1 igy DPRINTF("CFI ID str and command set recognized\n");
186 1.1 igy goto detect;
187 1.1 igy }
188 1.1 igy if (!i28f128_probe(va->va_iot, ioh)) {
189 1.1 igy DPRINTF("28F128 detected\n");
190 1.1 igy goto detect;
191 1.1 igy }
192 1.1 igy if (!mbm29160_probe(va->va_iot, ioh)) {
193 1.1 igy DPRINTF("29LV160 detected\n");
194 1.1 igy goto detect;
195 1.1 igy }
196 1.1 igy return 0;
197 1.1 igy
198 1.1 igy detect:
199 1.1 igy bus_space_unmap(va->va_iot, ioh, va->va_size);
200 1.1 igy return 1;
201 1.1 igy }
202 1.1 igy
203 1.1 igy static void
204 1.1 igy flash_attach(struct device *parent, struct device *self, void *aux)
205 1.1 igy {
206 1.1 igy struct flash_softc *sc = (void *) self;
207 1.1 igy struct vrip_attach_args *va = aux;
208 1.1 igy int i;
209 1.1 igy int fence;
210 1.1 igy bus_space_tag_t iot = va->va_iot;
211 1.1 igy bus_space_handle_t ioh;
212 1.1 igy size_t block_size;
213 1.1 igy
214 1.1 igy if (bus_space_map(iot, va->va_addr, va->va_size, 0, &ioh)) {
215 1.1 igy printf(": can't map i/o space\n");
216 1.1 igy return;
217 1.1 igy }
218 1.1 igy
219 1.1 igy sc->sc_iot = iot;
220 1.1 igy sc->sc_ioh = ioh;
221 1.1 igy sc->sc_size = va->va_size;
222 1.1 igy sc->sc_status = 0;
223 1.1 igy
224 1.1 igy /*
225 1.1 igy * Read entire CFI structure
226 1.1 igy */
227 1.1 igy bus_space_write_2(iot, ioh, CFI_QUERY_OFFSET, CFI_READ_CFI_QUERY);
228 1.1 igy for (i = 0; i < CFI_TOTAL_SIZE; i++) {
229 1.1 igy sc->sc_cfi_raw[i] = bus_space_read_2(iot, ioh, i << 1);
230 1.1 igy }
231 1.1 igy bus_space_write_2(iot, ioh, 0, FLASH_RESET);
232 1.1 igy
233 1.1 igy sc->sc_ops = find_command_set(sc->sc_cfi_raw[CFI_PRIM_COMM_REG0],
234 1.1 igy sc->sc_cfi_raw[CFI_PRIM_COMM_REG1]);
235 1.1 igy if (sc->sc_ops) {
236 1.1 igy printf(": using %s command set", sc->sc_ops->fo_name);
237 1.1 igy } else {
238 1.1 igy printf("opps sc->sc_ops is NULL\n");
239 1.1 igy }
240 1.1 igy
241 1.1 igy /*
242 1.1 igy * determine size of the largest block
243 1.1 igy */
244 1.1 igy sc->sc_block_size = 0;
245 1.1 igy i = CFI_EBLK1_INFO_REG;
246 1.1 igy fence = sc->sc_cfi_raw[CFI_NUM_ERASE_BLK_REG] * CFI_EBLK_INFO_SIZE
247 1.1 igy + i;
248 1.1 igy for (; i < fence; i += CFI_EBLK_INFO_SIZE) {
249 1.1 igy if (sc->sc_cfi_raw[i + CFI_EBLK_INFO_NSECT0] == 0
250 1.1 igy && sc->sc_cfi_raw[i + CFI_EBLK_INFO_NSECT1] == 0)
251 1.1 igy continue;
252 1.1 igy block_size
253 1.1 igy = (sc->sc_cfi_raw[i + CFI_EBLK_INFO_SECSIZE0] << 8)
254 1.1 igy + (sc->sc_cfi_raw[i + CFI_EBLK_INFO_SECSIZE1] << 16);
255 1.1 igy if (sc->sc_block_size < block_size)
256 1.1 igy sc->sc_block_size = block_size;
257 1.1 igy }
258 1.1 igy
259 1.1 igy if ((sc->sc_buf = malloc(sc->sc_block_size, M_DEVBUF, M_NOWAIT))
260 1.1 igy == NULL) {
261 1.1 igy printf(": can't alloc buffer space\n");
262 1.1 igy return;
263 1.1 igy }
264 1.1 igy
265 1.1 igy sc->sc_write_buffer_size
266 1.1 igy = 1 << (sc->sc_cfi_raw[CFI_MAX_WBUF_SIZE_REG0]
267 1.1 igy + (sc->sc_cfi_raw[CFI_MAX_WBUF_SIZE_REG1] << 8));
268 1.1 igy sc->sc_typ_word_prog_timo
269 1.1 igy = 1 << sc->sc_cfi_raw[CFI_TYP_WORD_PROG_REG];
270 1.1 igy sc->sc_max_word_prog_timo
271 1.1 igy = 1 << sc->sc_cfi_raw[CFI_MAX_WORD_PROG_REG];
272 1.1 igy sc->sc_typ_buffer_write_timo
273 1.1 igy = 1 << sc->sc_cfi_raw[CFI_TYP_BUF_WRITE_REG];
274 1.1 igy sc->sc_max_buffer_write_timo
275 1.1 igy = 1 << sc->sc_cfi_raw[CFI_MAX_BUF_WRITE_REG];
276 1.1 igy sc->sc_typ_block_erase_timo
277 1.1 igy = 1 << sc->sc_cfi_raw[CFI_TYP_BLOCK_ERASE_REG];
278 1.1 igy sc->sc_max_block_erase_timo
279 1.1 igy = 1 << sc->sc_cfi_raw[CFI_MAX_BLOCK_ERASE_REG];
280 1.1 igy
281 1.1 igy printf("\n");
282 1.1 igy
283 1.1 igy #ifdef FLASH_DEBUG
284 1.1 igy printf("read_cfi: extract cfi\n");
285 1.1 igy printf("max block size: %dbyte\n", sc->sc_block_size);
286 1.1 igy printf("write buffer size: %dbyte\n", sc->sc_write_buffer_size);
287 1.1 igy printf("typical word program timeout: %dusec\n",
288 1.1 igy sc->sc_typ_word_prog_timo);
289 1.1 igy printf("maximam word program timeout: %dusec (%d time of typ)\n",
290 1.1 igy sc->sc_typ_word_prog_timo * sc->sc_max_word_prog_timo,
291 1.1 igy sc->sc_max_word_prog_timo);
292 1.1 igy printf("typical buffer write timeout: %dusec\n",
293 1.1 igy sc->sc_typ_buffer_write_timo);
294 1.1 igy printf("maximam buffer write timeout: %dusec (%d time of typ)\n",
295 1.1 igy sc->sc_typ_buffer_write_timo * sc->sc_max_buffer_write_timo,
296 1.1 igy sc->sc_max_buffer_write_timo);
297 1.1 igy printf("typical block erase timeout: %dmsec\n",
298 1.1 igy sc->sc_typ_block_erase_timo);
299 1.1 igy printf("maximam block erase timeout: %dmsec (%d time of typ)\n",
300 1.1 igy sc->sc_typ_block_erase_timo * sc->sc_max_block_erase_timo,
301 1.1 igy sc->sc_max_block_erase_timo);
302 1.1 igy
303 1.1 igy printf("read_cfi: dump cfi\n");
304 1.1 igy for (i = 0; i < CFI_TOTAL_SIZE;) {
305 1.1 igy int j;
306 1.1 igy for (j = 0; j < 16; j++) {
307 1.1 igy printf("%02x ", sc->sc_cfi_raw[i++]);
308 1.1 igy }
309 1.1 igy printf("\n");
310 1.1 igy }
311 1.1 igy #endif
312 1.1 igy }
313 1.1 igy
314 1.1 igy int
315 1.1 igy flashopen(dev_t dev, int flag, int mode, struct proc *p)
316 1.1 igy {
317 1.1 igy struct flash_softc *sc;
318 1.1 igy
319 1.1 igy if ((sc = device_lookup(&flash_cd, minor(dev))) == NULL)
320 1.1 igy return ENXIO;
321 1.1 igy if (sc->sc_status & FLASH_ST_BUSY)
322 1.1 igy return EBUSY;
323 1.1 igy sc->sc_status |= FLASH_ST_BUSY;
324 1.1 igy return 0;
325 1.1 igy }
326 1.1 igy
327 1.1 igy int
328 1.1 igy flashclose(dev_t dev, int flag, int mode, struct proc *p)
329 1.1 igy {
330 1.1 igy struct flash_softc *sc;
331 1.1 igy
332 1.1 igy sc = device_lookup(&flash_cd, minor(dev));
333 1.1 igy sc->sc_status &= ~FLASH_ST_BUSY;
334 1.1 igy return 0;
335 1.1 igy }
336 1.1 igy
337 1.1 igy int
338 1.1 igy flashread(dev_t dev, struct uio *uio, int flag)
339 1.1 igy {
340 1.1 igy struct flash_softc *sc;
341 1.1 igy bus_space_tag_t iot;
342 1.1 igy bus_space_handle_t ioh;
343 1.1 igy bus_size_t off;
344 1.1 igy int total;
345 1.1 igy int count;
346 1.1 igy int error;
347 1.1 igy
348 1.1 igy sc = device_lookup(&flash_cd, minor(dev));
349 1.1 igy iot = sc->sc_iot;
350 1.1 igy ioh = sc->sc_ioh;
351 1.1 igy
352 1.1 igy off = uio->uio_offset;
353 1.1 igy total = min(sc->sc_size - off, uio->uio_resid);
354 1.1 igy
355 1.1 igy while (total > 0) {
356 1.1 igy count = min(sc->sc_block_size, uio->uio_resid);
357 1.1 igy bus_space_read_region_1(iot, ioh, off, sc->sc_buf, count);
358 1.1 igy if ((error = uiomove(sc->sc_buf, count, uio)) != 0)
359 1.1 igy return error;
360 1.1 igy off += count;
361 1.1 igy total -= count;
362 1.1 igy }
363 1.1 igy return 0;
364 1.1 igy }
365 1.1 igy
366 1.1 igy
367 1.1 igy int
368 1.1 igy flashwrite(dev_t dev, struct uio *uio, int flag)
369 1.1 igy {
370 1.1 igy struct flash_softc *sc;
371 1.1 igy bus_space_tag_t iot;
372 1.1 igy bus_space_handle_t ioh;
373 1.1 igy bus_size_t off;
374 1.1 igy int stat;
375 1.1 igy int error;
376 1.1 igy
377 1.1 igy sc = device_lookup(&flash_cd, minor(dev));
378 1.1 igy
379 1.1 igy if (sc->sc_size < uio->uio_offset + uio->uio_resid)
380 1.1 igy return ENOSPC;
381 1.1 igy if (uio->uio_offset % sc->sc_block_size)
382 1.1 igy return EINVAL;
383 1.1 igy if (uio->uio_resid % sc->sc_block_size)
384 1.1 igy return EINVAL;
385 1.1 igy
386 1.1 igy iot = sc->sc_iot;
387 1.1 igy ioh = sc->sc_ioh;
388 1.1 igy
389 1.1 igy for (off = uio->uio_offset;
390 1.1 igy uio->uio_resid > 0;
391 1.1 igy off += sc->sc_block_size) {
392 1.1 igy if ((error = uiomove(sc->sc_buf, sc->sc_block_size, uio)) != 0)
393 1.1 igy return error;
394 1.1 igy if (is_block_same(sc, off, sc->sc_buf))
395 1.1 igy continue;
396 1.1 igy if ((stat = flash_block_erase(sc, off)) != 0) {
397 1.1 igy printf("block erase failed status = 0x%x\n", stat);
398 1.1 igy return EIO;
399 1.1 igy }
400 1.1 igy if ((stat = flash_block_write(sc, off)) != 0) {
401 1.1 igy printf("block write failed status = 0x%x\n", stat);
402 1.1 igy return EIO;
403 1.1 igy }
404 1.1 igy }
405 1.1 igy return 0;
406 1.1 igy }
407 1.1 igy
408 1.1 igy /*
409 1.1 igy * XXX
410 1.1 igy * this function is too much specific for the device.
411 1.1 igy */
412 1.1 igy static int
413 1.1 igy i28f128_probe(bus_space_tag_t iot, bus_space_handle_t ioh)
414 1.1 igy {
415 1.1 igy static const u_int8_t vendor_code[] = {
416 1.1 igy 0x89, /* manufacturer code: intel */
417 1.1 igy 0x18, /* device code: 28F128 */
418 1.1 igy };
419 1.1 igy
420 1.1 igy static const u_int8_t idstr[] = {
421 1.1 igy 'Q', 'R', 'Y',
422 1.1 igy 0x01, 0x00,
423 1.1 igy 0x31, 0x00,
424 1.1 igy 0xff
425 1.1 igy };
426 1.1 igy
427 1.1 igy int i;
428 1.1 igy
429 1.1 igy /* start Common Flash Interface Query */
430 1.1 igy bus_space_write_2(iot, ioh, 0, CFI_READ_CFI_QUERY);
431 1.1 igy /* read CFI Query ID string */
432 1.1 igy for (i = 0; idstr[i] != 0xff; i++) {
433 1.1 igy if (bus_space_read_2(iot, ioh, (0x10 + i) << 1) != idstr[i])
434 1.1 igy return 1;
435 1.1 igy }
436 1.1 igy
437 1.1 igy /* read manufacturer code and device code */
438 1.1 igy if (bus_space_read_2(iot, ioh, 0x00) != vendor_code[0])
439 1.1 igy return 1;
440 1.1 igy if (bus_space_read_2(iot, ioh, 0x02) != vendor_code[1])
441 1.1 igy return 1;
442 1.1 igy
443 1.1 igy bus_space_write_2(iot, ioh, 0, I28F128_RESET);
444 1.1 igy return 0;
445 1.1 igy }
446 1.1 igy
447 1.1 igy /*
448 1.1 igy * XXX
449 1.1 igy * this function is too much specific for the device.
450 1.1 igy */
451 1.1 igy static int
452 1.1 igy mbm29160_probe(bus_space_tag_t iot, bus_space_handle_t ioh)
453 1.1 igy {
454 1.1 igy static const u_int16_t vendor_code[] = {
455 1.1 igy 0x0004, /* manufacturer code: intel */
456 1.1 igy 0x2249, /* device code: 29LV160BE */
457 1.1 igy };
458 1.1 igy
459 1.1 igy static const u_int8_t idstr[] = {
460 1.1 igy 'Q', 'R', 'Y',
461 1.1 igy 0x02, 0x00,
462 1.1 igy 0x40, 0x00,
463 1.1 igy 0xff
464 1.1 igy };
465 1.1 igy
466 1.1 igy int i;
467 1.1 igy
468 1.1 igy /* start Common Flash Interface Query */
469 1.1 igy bus_space_write_2(iot, ioh, 0xaa, CFI_READ_CFI_QUERY);
470 1.1 igy /* read CFI Query ID string */
471 1.1 igy for (i = 0; idstr[i] != 0xff; i++) {
472 1.1 igy if (bus_space_read_2(iot, ioh, (0x10 + i) << 1) != idstr[i])
473 1.1 igy return 1;
474 1.1 igy }
475 1.1 igy
476 1.1 igy bus_space_write_2(iot, ioh, 0, 0xff);
477 1.1 igy
478 1.1 igy /* read manufacturer code and device code */
479 1.1 igy bus_space_write_2(iot, ioh, 0x555 << 1, 0xaa);
480 1.1 igy bus_space_write_2(iot, ioh, 0x2aa << 1, 0x55);
481 1.1 igy bus_space_write_2(iot, ioh, 0x555 << 1, 0x90);
482 1.1 igy if (bus_space_read_2(iot, ioh, 0x00) != vendor_code[0])
483 1.1 igy return 1;
484 1.1 igy if (bus_space_read_2(iot, ioh, 0x02) != vendor_code[1])
485 1.1 igy return 1;
486 1.1 igy
487 1.1 igy bus_space_write_2(iot, ioh, 0, 0xff);
488 1.1 igy return 0;
489 1.1 igy }
490 1.1 igy
491 1.1 igy static int
492 1.1 igy is_block_same(struct flash_softc *sc, bus_size_t offset, const void *bufp)
493 1.1 igy {
494 1.1 igy bus_space_tag_t iot = sc->sc_iot;
495 1.1 igy bus_space_handle_t ioh = sc->sc_ioh;
496 1.1 igy const u_int8_t *p = bufp;
497 1.1 igy int count = sc->sc_block_size;
498 1.1 igy
499 1.1 igy while (count-- > 0) {
500 1.1 igy if (bus_space_read_1(iot, ioh, offset++) != *p++)
501 1.1 igy return 0;
502 1.1 igy }
503 1.1 igy return 1;
504 1.1 igy }
505 1.1 igy
506 1.1 igy static int
507 1.1 igy intel_erase(struct flash_softc *sc, bus_size_t offset)
508 1.1 igy {
509 1.1 igy bus_space_tag_t iot = sc->sc_iot;
510 1.1 igy bus_space_handle_t ioh = sc->sc_ioh;
511 1.1 igy int status;
512 1.1 igy int i;
513 1.1 igy
514 1.1 igy bus_space_write_2(iot, ioh, offset, I28F128_BLK_ERASE_1ST);
515 1.1 igy bus_space_write_2(iot, ioh, offset, I28F128_BLK_ERASE_2ND);
516 1.1 igy
517 1.1 igy for (i = sc->sc_max_block_erase_timo; i > 0; i--) {
518 1.1 igy tsleep(sc, PRIBIO, "blockerase",
519 1.1 igy 1 + (sc->sc_typ_block_erase_timo * hz) / 1000);
520 1.1 igy if ((status = bus_space_read_2(iot, ioh, offset))
521 1.1 igy & I28F128_S_READY)
522 1.1 igy break;
523 1.1 igy }
524 1.1 igy
525 1.1 igy bus_space_write_2(iot, ioh, offset, I28F128_CLEAR_STATUS);
526 1.1 igy bus_space_write_2(iot, ioh, offset, I28F128_RESET);
527 1.1 igy
528 1.1 igy return status & (I28F128_S_ERASE_SUSPEND
529 1.1 igy | I28F128_S_COMSEQ_ERROR
530 1.1 igy | I28F128_S_ERASE_ERROR
531 1.1 igy | I28F128_S_BLOCK_LOCKED);
532 1.1 igy }
533 1.1 igy
534 1.1 igy static int
535 1.1 igy intel_write(struct flash_softc *sc, bus_size_t offset)
536 1.1 igy {
537 1.1 igy bus_space_tag_t iot = sc->sc_iot;
538 1.1 igy bus_space_handle_t ioh = sc->sc_ioh;
539 1.1 igy int wbuf_size;
540 1.1 igy int timo;
541 1.1 igy int status;
542 1.1 igy bus_size_t fence;
543 1.1 igy int i;
544 1.1 igy const u_int16_t *p;
545 1.1 igy
546 1.1 igy /* wbuf_size = size in u_int16_t */
547 1.1 igy wbuf_size = sc->sc_write_buffer_size >> 1;
548 1.1 igy
549 1.1 igy p = (u_int16_t *) sc->sc_buf;
550 1.1 igy fence = offset + sc->sc_block_size;
551 1.1 igy do {
552 1.1 igy for (timo = sc->sc_max_buffer_write_timo; timo > 0; timo--) {
553 1.1 igy bus_space_write_2(iot, ioh, offset,
554 1.1 igy I28F128_WRITE_BUFFER);
555 1.1 igy status = bus_space_read_2(iot, ioh, offset);
556 1.1 igy if (status & I28F128_XS_BUF_AVAIL)
557 1.1 igy break;
558 1.1 igy DELAY(sc->sc_typ_buffer_write_timo);
559 1.1 igy }
560 1.1 igy if (timo == 0) {
561 1.1 igy status |= FLASH_TIMEOUT;
562 1.1 igy goto errout;
563 1.1 igy }
564 1.1 igy
565 1.1 igy bus_space_write_2(iot, ioh, offset, wbuf_size - 1);
566 1.1 igy
567 1.1 igy for (i = wbuf_size; i > 0; i--, p++, offset += 2)
568 1.1 igy bus_space_write_2(iot, ioh, offset, *p);
569 1.1 igy
570 1.1 igy bus_space_write_2(iot, ioh, offset, I28F128_WBUF_CONFIRM);
571 1.1 igy
572 1.1 igy do {
573 1.1 igy bus_space_write_2(iot, ioh, offset,
574 1.1 igy I28F128_READ_STATUS);
575 1.1 igy status = bus_space_read_2(iot, ioh, offset);
576 1.1 igy } while (!(status & I28F128_S_READY));
577 1.1 igy
578 1.1 igy } while (offset < fence);
579 1.1 igy
580 1.1 igy bus_space_write_2(iot, ioh, offset, I28F128_CLEAR_STATUS);
581 1.1 igy bus_space_write_2(iot, ioh, offset, I28F128_RESET);
582 1.1 igy
583 1.1 igy return 0;
584 1.1 igy
585 1.1 igy errout:
586 1.1 igy bus_space_write_2(iot, ioh, offset, I28F128_CLEAR_STATUS);
587 1.1 igy bus_space_write_2(iot, ioh, offset, I28F128_RESET);
588 1.1 igy
589 1.1 igy status &= (FLASH_TIMEOUT
590 1.1 igy | I28F128_S_PROG_ERROR
591 1.1 igy | I28F128_S_COMSEQ_ERROR
592 1.1 igy | I28F128_S_LOW_VOLTAGE
593 1.1 igy | I28F128_S_PROG_SUSPEND
594 1.1 igy | I28F128_S_BLOCK_LOCKED);
595 1.1 igy return status;
596 1.1 igy }
597 1.1 igy
598 1.1 igy static int
599 1.1 igy amd_erase_sector(struct flash_softc *sc, bus_size_t offset)
600 1.1 igy {
601 1.1 igy bus_space_tag_t iot = sc->sc_iot;
602 1.1 igy bus_space_handle_t ioh = sc->sc_ioh;
603 1.1 igy int i;
604 1.1 igy
605 1.1 igy DPRINTF(("amd_erase_sector offset = %08lx\n", offset));
606 1.1 igy
607 1.1 igy bus_space_write_2(iot, ioh,
608 1.1 igy MBM29LV160_COMM_ADDR0, MBM29LV160_COMM_CMD0);
609 1.1 igy bus_space_write_2(iot, ioh,
610 1.1 igy MBM29LV160_COMM_ADDR1, MBM29LV160_COMM_CMD1);
611 1.1 igy bus_space_write_2(iot, ioh,
612 1.1 igy MBM29LV160_COMM_ADDR2, MBM29LV160_ESECT_CMD2);
613 1.1 igy bus_space_write_2(iot, ioh,
614 1.1 igy MBM29LV160_COMM_ADDR3, MBM29LV160_ESECT_CMD3);
615 1.1 igy bus_space_write_2(iot, ioh,
616 1.1 igy MBM29LV160_COMM_ADDR4, MBM29LV160_ESECT_CMD4);
617 1.1 igy bus_space_write_2(iot, ioh, offset, MBM29LV160_ESECT_CMD5);
618 1.1 igy
619 1.1 igy for (i = sc->sc_max_block_erase_timo; i > 0; i--) {
620 1.1 igy tsleep(sc, PRIBIO, "blockerase",
621 1.1 igy 1 + (sc->sc_typ_block_erase_timo * hz) / 1000);
622 1.1 igy if (bus_space_read_2(iot, ioh, offset) == 0xffff)
623 1.1 igy return 0;
624 1.1 igy }
625 1.1 igy
626 1.1 igy return FLASH_TIMEOUT;
627 1.1 igy }
628 1.1 igy
629 1.1 igy static int
630 1.1 igy amd_erase(struct flash_softc *sc, bus_size_t offset)
631 1.1 igy {
632 1.1 igy static const struct mbm29lv_subsect {
633 1.1 igy u_int16_t devcode;
634 1.1 igy u_int32_t subsect_mask;
635 1.1 igy u_int32_t subsect_addr;
636 1.1 igy } subsect[] = {
637 1.1 igy {
638 1.1 igy MBM29LV160TE_DEVCODE,
639 1.1 igy MBM29LV160_SUBSECT_MASK,
640 1.1 igy MBM29LV160TE_SUBSECT_ADDR
641 1.1 igy },
642 1.1 igy {
643 1.1 igy MBM29LV160BE_DEVCODE,
644 1.1 igy MBM29LV160_SUBSECT_MASK,
645 1.1 igy MBM29LV160BE_SUBSECT_ADDR
646 1.1 igy },
647 1.1 igy { 0, 0, 0 }
648 1.1 igy };
649 1.1 igy
650 1.1 igy bus_space_tag_t iot = sc->sc_iot;
651 1.1 igy bus_space_handle_t ioh = sc->sc_ioh;
652 1.1 igy u_int16_t devcode;
653 1.1 igy const struct mbm29lv_subsect *ss;
654 1.1 igy bus_size_t fence;
655 1.1 igy int step;
656 1.1 igy int status;
657 1.1 igy
658 1.1 igy bus_space_write_2(iot, ioh,
659 1.1 igy MBM29LV160_COMM_ADDR0, MBM29LV160_COMM_CMD0);
660 1.1 igy bus_space_write_2(iot, ioh,
661 1.1 igy MBM29LV160_COMM_ADDR1, MBM29LV160_COMM_CMD1);
662 1.1 igy bus_space_write_2(iot, ioh,
663 1.1 igy MBM29LV160_COMM_ADDR2, MBM29LV160_SIGN_CMD2);
664 1.1 igy devcode = bus_space_read_2(iot, ioh, MBM29LV160_DEVCODE_REG);
665 1.1 igy
666 1.1 igy for (ss = subsect; ss->devcode; ss++) {
667 1.1 igy if (ss->devcode == devcode)
668 1.1 igy break;
669 1.1 igy }
670 1.1 igy if (ss->devcode == 0) {
671 1.1 igy printf("flash: amd_erase(): unknown device code %04x\n",
672 1.1 igy devcode);
673 1.1 igy return -1;
674 1.1 igy }
675 1.1 igy
676 1.1 igy DPRINTF(("flash: amd_erase(): devcode = %04x subsect = %08x\n",
677 1.1 igy devcode, ss->subsect_addr));
678 1.1 igy
679 1.1 igy fence = offset + sc->sc_block_size;
680 1.1 igy step = (offset & ss->subsect_mask) == ss->subsect_addr
681 1.1 igy ? MBM29LV160_SUBSECT_SIZE : MBM29LV160_SECT_SIZE;
682 1.1 igy do {
683 1.1 igy if ((status = amd_erase_sector(sc, offset)) != 0)
684 1.1 igy return status;
685 1.1 igy offset += step;
686 1.1 igy } while (offset < fence);
687 1.1 igy
688 1.1 igy return 0;
689 1.1 igy }
690 1.1 igy
691 1.1 igy static int
692 1.1 igy amd_write(struct flash_softc *sc, bus_size_t offset)
693 1.1 igy {
694 1.1 igy bus_space_tag_t iot = sc->sc_iot;
695 1.1 igy bus_space_handle_t ioh = sc->sc_ioh;
696 1.1 igy int timo;
697 1.1 igy bus_size_t fence;
698 1.1 igy const u_int16_t *p;
699 1.1 igy
700 1.1 igy p = (u_int16_t *) sc->sc_buf;
701 1.1 igy fence = offset + sc->sc_block_size;
702 1.1 igy do {
703 1.1 igy bus_space_write_2(iot, ioh,
704 1.1 igy MBM29LV160_COMM_ADDR0,
705 1.1 igy MBM29LV160_COMM_CMD0);
706 1.1 igy bus_space_write_2(iot, ioh,
707 1.1 igy MBM29LV160_COMM_ADDR1,
708 1.1 igy MBM29LV160_COMM_CMD1);
709 1.1 igy bus_space_write_2(iot, ioh,
710 1.1 igy MBM29LV160_COMM_ADDR2,
711 1.1 igy MBM29LV160_PROG_CMD2);
712 1.1 igy bus_space_write_2(iot, ioh, offset, *p);
713 1.1 igy
714 1.1 igy for (timo = sc->sc_max_word_prog_timo; timo > 0; timo--) {
715 1.1 igy if (bus_space_read_2(iot, ioh, offset) == *p)
716 1.1 igy break;
717 1.1 igy DELAY(sc->sc_typ_word_prog_timo);
718 1.1 igy }
719 1.1 igy if (timo == 0)
720 1.1 igy return FLASH_TIMEOUT;
721 1.1 igy
722 1.1 igy p++;
723 1.1 igy offset += 2;
724 1.1 igy } while (offset < fence);
725 1.1 igy
726 1.1 igy return 0;
727 1.1 igy }
728