athflash.c revision 1.1.6.2 1 1.1.6.2 chap /* $NetBSD: athflash.c,v 1.1.6.2 2006/06/19 03:44:52 chap Exp $ */
2 1.1.6.2 chap
3 1.1.6.2 chap /*
4 1.1.6.2 chap * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
5 1.1.6.2 chap * Copyright (c) 2006 Garrett D'Amore.
6 1.1.6.2 chap * All rights reserved.
7 1.1.6.2 chap *
8 1.1.6.2 chap * Portions of this code were written by Garrett D'Amore for the
9 1.1.6.2 chap * Champaign-Urbana Community Wireless Network Project.
10 1.1.6.2 chap *
11 1.1.6.2 chap * Redistribution and use in source and binary forms, with or
12 1.1.6.2 chap * without modification, are permitted provided that the following
13 1.1.6.2 chap * conditions are met:
14 1.1.6.2 chap * 1. Redistributions of source code must retain the above copyright
15 1.1.6.2 chap * notice, this list of conditions and the following disclaimer.
16 1.1.6.2 chap * 2. Redistributions in binary form must reproduce the above
17 1.1.6.2 chap * copyright notice, this list of conditions and the following
18 1.1.6.2 chap * disclaimer in the documentation and/or other materials provided
19 1.1.6.2 chap * with the distribution.
20 1.1.6.2 chap * 3. All advertising materials mentioning features or use of this
21 1.1.6.2 chap * software must display the following acknowledgements:
22 1.1.6.2 chap * This product includes software developed by the Urbana-Champaign
23 1.1.6.2 chap * Independent Media Center.
24 1.1.6.2 chap * This product includes software developed by Garrett D'Amore.
25 1.1.6.2 chap * 4. Urbana-Champaign Independent Media Center's name and Garrett
26 1.1.6.2 chap * D'Amore's name may not be used to endorse or promote products
27 1.1.6.2 chap * derived from this software without specific prior written permission.
28 1.1.6.2 chap *
29 1.1.6.2 chap * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
30 1.1.6.2 chap * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
31 1.1.6.2 chap * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 1.1.6.2 chap * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.1.6.2 chap * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
34 1.1.6.2 chap * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
35 1.1.6.2 chap * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 1.1.6.2 chap * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 1.1.6.2 chap * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 1.1.6.2 chap * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1.6.2 chap * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 1.1.6.2 chap * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 1.1.6.2 chap * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 1.1.6.2 chap */
43 1.1.6.2 chap /*
44 1.1.6.2 chap * Copyright (c) 2002 The NetBSD Foundation, Inc.
45 1.1.6.2 chap * All rights reserved.
46 1.1.6.2 chap *
47 1.1.6.2 chap * This code is derived from software contributed to The NetBSD Foundation
48 1.1.6.2 chap * by Naoto Shimazaki of YOKOGAWA Electric Corporation.
49 1.1.6.2 chap *
50 1.1.6.2 chap * Redistribution and use in source and binary forms, with or without
51 1.1.6.2 chap * modification, are permitted provided that the following conditions
52 1.1.6.2 chap * are met:
53 1.1.6.2 chap * 1. Redistributions of source code must retain the above copyright
54 1.1.6.2 chap * notice, this list of conditions and the following disclaimer.
55 1.1.6.2 chap * 2. Redistributions in binary form must reproduce the above copyright
56 1.1.6.2 chap * notice, this list of conditions and the following disclaimer in the
57 1.1.6.2 chap * documentation and/or other materials provided with the distribution.
58 1.1.6.2 chap * 3. All advertising materials mentioning features or use of this software
59 1.1.6.2 chap * must display the following acknowledgement:
60 1.1.6.2 chap * This product includes software developed by the NetBSD
61 1.1.6.2 chap * Foundation, Inc. and its contributors.
62 1.1.6.2 chap * 4. Neither the name of The NetBSD Foundation nor the names of its
63 1.1.6.2 chap * contributors may be used to endorse or promote products derived
64 1.1.6.2 chap * from this software without specific prior written permission.
65 1.1.6.2 chap *
66 1.1.6.2 chap * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
67 1.1.6.2 chap * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
68 1.1.6.2 chap * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
69 1.1.6.2 chap * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
70 1.1.6.2 chap * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
71 1.1.6.2 chap * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
72 1.1.6.2 chap * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
73 1.1.6.2 chap * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
74 1.1.6.2 chap * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
75 1.1.6.2 chap * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
76 1.1.6.2 chap * POSSIBILITY OF SUCH DAMAGE.
77 1.1.6.2 chap */
78 1.1.6.2 chap
79 1.1.6.2 chap /*
80 1.1.6.2 chap * Flash Memory Driver
81 1.1.6.2 chap *
82 1.1.6.2 chap * XXX This primitive flash driver does *not* support boot sectored devices,
83 1.1.6.2 chap * XXX and only supports a fairly limited set of devices, that we are likely to
84 1.1.6.2 chap * XXX to find in an AP30.
85 1.1.6.2 chap * XXX
86 1.1.6.2 chap * XXX We also are only supporting flash widths of 16 _for the moment_, and
87 1.1.6.2 chap * XXX we are only supporting flash devices that use the AMD command sets.
88 1.1.6.2 chap * XXX All this should be reviewed and improved to be much more generic.
89 1.1.6.2 chap */
90 1.1.6.2 chap
91 1.1.6.2 chap #include <sys/cdefs.h>
92 1.1.6.2 chap __KERNEL_RCSID(0, "$NetBSD: athflash.c,v 1.1.6.2 2006/06/19 03:44:52 chap Exp $");
93 1.1.6.2 chap
94 1.1.6.2 chap #include <sys/param.h>
95 1.1.6.2 chap #include <sys/conf.h>
96 1.1.6.2 chap #include <sys/device.h>
97 1.1.6.2 chap #include <sys/kernel.h>
98 1.1.6.2 chap #include <sys/malloc.h>
99 1.1.6.2 chap #include <sys/proc.h>
100 1.1.6.2 chap #include <sys/systm.h>
101 1.1.6.2 chap
102 1.1.6.2 chap #include <machine/bus.h>
103 1.1.6.2 chap
104 1.1.6.2 chap #include <mips/atheros/include/arbusvar.h>
105 1.1.6.2 chap
106 1.1.6.2 chap #ifdef FLASH_DEBUG
107 1.1.6.2 chap int flash_debug = 0;
108 1.1.6.2 chap #define DPRINTF(x) if (flash_debug) printf x
109 1.1.6.2 chap #else
110 1.1.6.2 chap #define DPRINTF(x)
111 1.1.6.2 chap #endif
112 1.1.6.2 chap
113 1.1.6.2 chap struct flash_softc {
114 1.1.6.2 chap struct device sc_dev;
115 1.1.6.2 chap bus_space_tag_t sc_iot;
116 1.1.6.2 chap bus_space_handle_t sc_ioh;
117 1.1.6.2 chap size_t sc_size;
118 1.1.6.2 chap size_t sc_sector_size;
119 1.1.6.2 chap int sc_status;
120 1.1.6.2 chap u_int8_t *sc_buf;
121 1.1.6.2 chap };
122 1.1.6.2 chap
123 1.1.6.2 chap #define FLASH_ST_BUSY 0x1
124 1.1.6.2 chap
125 1.1.6.2 chap static int flash_probe(struct device *, struct cfdata *, void *);
126 1.1.6.2 chap static void flash_attach(struct device *, struct device *, void *);
127 1.1.6.2 chap
128 1.1.6.2 chap static int is_block_same(struct flash_softc *, bus_size_t, const void *);
129 1.1.6.2 chap static int toggle_bit_wait(struct flash_softc *, bus_size_t, int, int, int);
130 1.1.6.2 chap
131 1.1.6.2 chap static int flash_sector_erase(struct flash_softc *, bus_size_t);
132 1.1.6.2 chap static int flash_sector_write(struct flash_softc *, bus_size_t);
133 1.1.6.2 chap
134 1.1.6.2 chap extern struct cfdriver athflash_cd;
135 1.1.6.2 chap
136 1.1.6.2 chap CFATTACH_DECL(athflash, sizeof(struct flash_softc),
137 1.1.6.2 chap flash_probe, flash_attach, NULL, NULL);
138 1.1.6.2 chap
139 1.1.6.2 chap dev_type_open(flashopen);
140 1.1.6.2 chap dev_type_close(flashclose);
141 1.1.6.2 chap dev_type_read(flashread);
142 1.1.6.2 chap dev_type_write(flashwrite);
143 1.1.6.2 chap
144 1.1.6.2 chap const struct cdevsw athflash_cdevsw = {
145 1.1.6.2 chap flashopen, flashclose, flashread, flashwrite, noioctl,
146 1.1.6.2 chap nostop, notty, nopoll, nommap, nokqfilter,
147 1.1.6.2 chap };
148 1.1.6.2 chap
149 1.1.6.2 chap static struct {
150 1.1.6.2 chap uint16_t vendor_id;
151 1.1.6.2 chap uint16_t device_id;
152 1.1.6.2 chap const char *name;
153 1.1.6.2 chap int sector_size;
154 1.1.6.2 chap int flash_size;
155 1.1.6.2 chap } flash_ids[] = {
156 1.1.6.2 chap { 0x00bf, 0x2780, "SST 39VF400", 0x01000, 0x080000 }, /* 512KB */
157 1.1.6.2 chap { 0x00bf, 0x2782, "SST 39VF160", 0x01000, 0x200000 }, /* 2MB */
158 1.1.6.2 chap { 0xffff, 0xffff, NULL, 0, 0 } /* end list */
159 1.1.6.2 chap };
160 1.1.6.2 chap
161 1.1.6.2 chap static int
162 1.1.6.2 chap flash_probe(struct device *parent, struct cfdata *cf, void *aux)
163 1.1.6.2 chap {
164 1.1.6.2 chap struct arbus_attach_args *aa = aux;
165 1.1.6.2 chap bus_space_handle_t ioh;
166 1.1.6.2 chap int rv = 0, i;
167 1.1.6.2 chap uint16_t venid, devid;
168 1.1.6.2 chap
169 1.1.6.2 chap if (strcmp(aa->aa_name, cf->cf_name) != 0)
170 1.1.6.2 chap return 0;
171 1.1.6.2 chap
172 1.1.6.2 chap DPRINTF(("trying to map address %x\n", (unsigned)aa->aa_addr));
173 1.1.6.2 chap if (bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 0, &ioh))
174 1.1.6.2 chap return 0;
175 1.1.6.2 chap
176 1.1.6.2 chap /* issue JEDEC query */
177 1.1.6.2 chap DPRINTF(("issuing JEDEC query\n"));
178 1.1.6.2 chap bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0xAAAA);
179 1.1.6.2 chap bus_space_write_2(aa->aa_bst, ioh, (0x2AAA << 1), 0x5555);
180 1.1.6.2 chap bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0x9090);
181 1.1.6.2 chap
182 1.1.6.2 chap delay(100);
183 1.1.6.2 chap venid = bus_space_read_2(aa->aa_bst, ioh, 0);
184 1.1.6.2 chap devid = bus_space_read_2(aa->aa_bst, ioh, 2);
185 1.1.6.2 chap
186 1.1.6.2 chap /* issue software exit */
187 1.1.6.2 chap bus_space_write_2(aa->aa_bst, ioh, 0x0, 0xF0F0);
188 1.1.6.2 chap
189 1.1.6.2 chap for (i = 0; flash_ids[i].name != NULL; i++) {
190 1.1.6.2 chap if ((venid == flash_ids[i].vendor_id) &&
191 1.1.6.2 chap (devid == flash_ids[i].device_id)) {
192 1.1.6.2 chap rv = 1;
193 1.1.6.2 chap break;
194 1.1.6.2 chap }
195 1.1.6.2 chap }
196 1.1.6.2 chap
197 1.1.6.2 chap bus_space_unmap(aa->aa_bst, ioh, aa->aa_size);
198 1.1.6.2 chap return rv;
199 1.1.6.2 chap }
200 1.1.6.2 chap
201 1.1.6.2 chap static void
202 1.1.6.2 chap flash_attach(struct device *parent, struct device *self, void *aux)
203 1.1.6.2 chap {
204 1.1.6.2 chap struct flash_softc *sc = (void *) self;
205 1.1.6.2 chap struct arbus_attach_args *aa = aux;
206 1.1.6.2 chap int i;
207 1.1.6.2 chap bus_space_tag_t iot = aa->aa_bst;
208 1.1.6.2 chap bus_space_handle_t ioh;
209 1.1.6.2 chap uint16_t venid, devid;
210 1.1.6.2 chap
211 1.1.6.2 chap if (bus_space_map(iot, aa->aa_addr, aa->aa_size, 0, &ioh)) {
212 1.1.6.2 chap printf(": can't map i/o space\n");
213 1.1.6.2 chap return;
214 1.1.6.2 chap }
215 1.1.6.2 chap
216 1.1.6.2 chap sc->sc_iot = iot;
217 1.1.6.2 chap sc->sc_ioh = ioh;
218 1.1.6.2 chap sc->sc_status = 0;
219 1.1.6.2 chap
220 1.1.6.2 chap /* issue JEDEC query */
221 1.1.6.2 chap bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0xAAAA);
222 1.1.6.2 chap bus_space_write_2(aa->aa_bst, ioh, (0x2AAA << 1), 0x5555);
223 1.1.6.2 chap bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0x9090);
224 1.1.6.2 chap
225 1.1.6.2 chap delay(100);
226 1.1.6.2 chap venid = bus_space_read_2(aa->aa_bst, ioh, 0);
227 1.1.6.2 chap devid = bus_space_read_2(aa->aa_bst, ioh, 2);
228 1.1.6.2 chap
229 1.1.6.2 chap /* issue software exit */
230 1.1.6.2 chap bus_space_write_2(aa->aa_bst, ioh, 0x0, 0xF0F0);
231 1.1.6.2 chap
232 1.1.6.2 chap for (i = 0; flash_ids[i].name != NULL; i++) {
233 1.1.6.2 chap if ((venid == flash_ids[i].vendor_id) &&
234 1.1.6.2 chap (devid == flash_ids[i].device_id)) {
235 1.1.6.2 chap break;
236 1.1.6.2 chap }
237 1.1.6.2 chap }
238 1.1.6.2 chap
239 1.1.6.2 chap KASSERT(flash_ids[i].name != NULL);
240 1.1.6.2 chap printf(": %s ", flash_ids[i].name);
241 1.1.6.2 chap if (i >= 0x100000)
242 1.1.6.2 chap printf("(%d MB)", flash_ids[i].flash_size >> 20);
243 1.1.6.2 chap else
244 1.1.6.2 chap printf("(%d KB)", flash_ids[i].flash_size >> 10);
245 1.1.6.2 chap
246 1.1.6.2 chap /*
247 1.1.6.2 chap * determine size of the largest block
248 1.1.6.2 chap */
249 1.1.6.2 chap sc->sc_size = flash_ids[i].flash_size;
250 1.1.6.2 chap sc->sc_sector_size = flash_ids[i].sector_size;
251 1.1.6.2 chap
252 1.1.6.2 chap if ((sc->sc_buf = malloc(sc->sc_sector_size, M_DEVBUF, M_NOWAIT))
253 1.1.6.2 chap == NULL) {
254 1.1.6.2 chap printf(": can't alloc buffer space\n");
255 1.1.6.2 chap return;
256 1.1.6.2 chap }
257 1.1.6.2 chap
258 1.1.6.2 chap printf("\n");
259 1.1.6.2 chap }
260 1.1.6.2 chap
261 1.1.6.2 chap int
262 1.1.6.2 chap flashopen(dev_t dev, int flag, int mode, struct lwp *l)
263 1.1.6.2 chap {
264 1.1.6.2 chap struct flash_softc *sc;
265 1.1.6.2 chap
266 1.1.6.2 chap if ((sc = device_lookup(&athflash_cd, minor(dev))) == NULL)
267 1.1.6.2 chap return ENXIO;
268 1.1.6.2 chap if (sc->sc_status & FLASH_ST_BUSY)
269 1.1.6.2 chap return EBUSY;
270 1.1.6.2 chap sc->sc_status |= FLASH_ST_BUSY;
271 1.1.6.2 chap return 0;
272 1.1.6.2 chap }
273 1.1.6.2 chap
274 1.1.6.2 chap int
275 1.1.6.2 chap flashclose(dev_t dev, int flag, int mode, struct lwp *l)
276 1.1.6.2 chap {
277 1.1.6.2 chap struct flash_softc *sc;
278 1.1.6.2 chap
279 1.1.6.2 chap sc = device_lookup(&athflash_cd, minor(dev));
280 1.1.6.2 chap sc->sc_status &= ~FLASH_ST_BUSY;
281 1.1.6.2 chap return 0;
282 1.1.6.2 chap }
283 1.1.6.2 chap
284 1.1.6.2 chap int
285 1.1.6.2 chap flashread(dev_t dev, struct uio *uio, int flag)
286 1.1.6.2 chap {
287 1.1.6.2 chap struct flash_softc *sc;
288 1.1.6.2 chap bus_space_tag_t iot;
289 1.1.6.2 chap bus_space_handle_t ioh;
290 1.1.6.2 chap bus_size_t off;
291 1.1.6.2 chap int total;
292 1.1.6.2 chap int count;
293 1.1.6.2 chap int error;
294 1.1.6.2 chap
295 1.1.6.2 chap sc = device_lookup(&athflash_cd, minor(dev));
296 1.1.6.2 chap iot = sc->sc_iot;
297 1.1.6.2 chap ioh = sc->sc_ioh;
298 1.1.6.2 chap
299 1.1.6.2 chap off = uio->uio_offset;
300 1.1.6.2 chap total = min(sc->sc_size - off, uio->uio_resid);
301 1.1.6.2 chap
302 1.1.6.2 chap while (total > 0) {
303 1.1.6.2 chap count = min(sc->sc_sector_size, uio->uio_resid);
304 1.1.6.2 chap bus_space_read_region_1(iot, ioh, off, sc->sc_buf, count);
305 1.1.6.2 chap if ((error = uiomove(sc->sc_buf, count, uio)) != 0)
306 1.1.6.2 chap return error;
307 1.1.6.2 chap off += count;
308 1.1.6.2 chap total -= count;
309 1.1.6.2 chap }
310 1.1.6.2 chap return 0;
311 1.1.6.2 chap }
312 1.1.6.2 chap
313 1.1.6.2 chap
314 1.1.6.2 chap int
315 1.1.6.2 chap flashwrite(dev_t dev, struct uio *uio, int flag)
316 1.1.6.2 chap {
317 1.1.6.2 chap struct flash_softc *sc;
318 1.1.6.2 chap bus_space_tag_t iot;
319 1.1.6.2 chap bus_space_handle_t ioh;
320 1.1.6.2 chap bus_size_t off;
321 1.1.6.2 chap int stat;
322 1.1.6.2 chap int error;
323 1.1.6.2 chap
324 1.1.6.2 chap sc = device_lookup(&athflash_cd, minor(dev));
325 1.1.6.2 chap
326 1.1.6.2 chap if (sc->sc_size < uio->uio_offset + uio->uio_resid)
327 1.1.6.2 chap return ENOSPC;
328 1.1.6.2 chap if (uio->uio_offset % sc->sc_sector_size)
329 1.1.6.2 chap return EINVAL;
330 1.1.6.2 chap if (uio->uio_resid % sc->sc_sector_size)
331 1.1.6.2 chap return EINVAL;
332 1.1.6.2 chap
333 1.1.6.2 chap iot = sc->sc_iot;
334 1.1.6.2 chap ioh = sc->sc_ioh;
335 1.1.6.2 chap
336 1.1.6.2 chap for (off = uio->uio_offset;
337 1.1.6.2 chap uio->uio_resid > 0;
338 1.1.6.2 chap off += sc->sc_sector_size) {
339 1.1.6.2 chap error = uiomove(sc->sc_buf, sc->sc_sector_size, uio);
340 1.1.6.2 chap if (error != 0)
341 1.1.6.2 chap return error;
342 1.1.6.2 chap if (is_block_same(sc, off, sc->sc_buf))
343 1.1.6.2 chap continue;
344 1.1.6.2 chap if ((stat = flash_sector_erase(sc, off)) != 0) {
345 1.1.6.2 chap printf("sector erase failed status = 0x%x\n", stat);
346 1.1.6.2 chap return EIO;
347 1.1.6.2 chap }
348 1.1.6.2 chap if ((stat = flash_sector_write(sc, off)) != 0) {
349 1.1.6.2 chap printf("sector write failed status = 0x%x\n", stat);
350 1.1.6.2 chap return EIO;
351 1.1.6.2 chap }
352 1.1.6.2 chap }
353 1.1.6.2 chap return 0;
354 1.1.6.2 chap }
355 1.1.6.2 chap
356 1.1.6.2 chap static int
357 1.1.6.2 chap is_block_same(struct flash_softc *sc, bus_size_t offset, const void *bufp)
358 1.1.6.2 chap {
359 1.1.6.2 chap bus_space_tag_t iot = sc->sc_iot;
360 1.1.6.2 chap bus_space_handle_t ioh = sc->sc_ioh;
361 1.1.6.2 chap const u_int8_t *p = bufp;
362 1.1.6.2 chap int count = sc->sc_sector_size;
363 1.1.6.2 chap
364 1.1.6.2 chap while (count-- > 0) {
365 1.1.6.2 chap if (bus_space_read_1(iot, ioh, offset++) != *p++)
366 1.1.6.2 chap return 0;
367 1.1.6.2 chap }
368 1.1.6.2 chap return 1;
369 1.1.6.2 chap }
370 1.1.6.2 chap
371 1.1.6.2 chap static int
372 1.1.6.2 chap toggle_bit_wait(struct flash_softc *sc, bus_size_t offset,
373 1.1.6.2 chap int typtmo, int maxtmo, int spin)
374 1.1.6.2 chap {
375 1.1.6.2 chap bus_space_tag_t iot = sc->sc_iot;
376 1.1.6.2 chap bus_space_handle_t ioh = sc->sc_ioh;
377 1.1.6.2 chap uint8_t d1, d2;
378 1.1.6.2 chap
379 1.1.6.2 chap while (maxtmo > 0) {
380 1.1.6.2 chap
381 1.1.6.2 chap if (spin) {
382 1.1.6.2 chap DELAY(typtmo);
383 1.1.6.2 chap } else {
384 1.1.6.2 chap tsleep(sc, PRIBIO, "blockerase",
385 1.1.6.2 chap (typtmo / hz) + 1);
386 1.1.6.2 chap }
387 1.1.6.2 chap
388 1.1.6.2 chap d1 = bus_space_read_1(iot, ioh, offset);
389 1.1.6.2 chap d2 = bus_space_read_2(iot, ioh, offset);
390 1.1.6.2 chap
391 1.1.6.2 chap /* watch for the toggle bit to stop toggling */
392 1.1.6.2 chap if ((d1 & 0x40) == (d2 & 0x40)) {
393 1.1.6.2 chap return 0;
394 1.1.6.2 chap }
395 1.1.6.2 chap
396 1.1.6.2 chap maxtmo -= typtmo;
397 1.1.6.2 chap }
398 1.1.6.2 chap return (ETIMEDOUT);
399 1.1.6.2 chap }
400 1.1.6.2 chap
401 1.1.6.2 chap static int
402 1.1.6.2 chap flash_sector_erase(struct flash_softc *sc, bus_size_t offset)
403 1.1.6.2 chap {
404 1.1.6.2 chap bus_space_tag_t iot = sc->sc_iot;
405 1.1.6.2 chap bus_space_handle_t ioh = sc->sc_ioh;
406 1.1.6.2 chap
407 1.1.6.2 chap DPRINTF(("flash_sector_erase offset = %08lx\n", offset));
408 1.1.6.2 chap
409 1.1.6.2 chap bus_space_write_2(iot, ioh, (0x5555 << 1), 0xAAAA);
410 1.1.6.2 chap bus_space_write_2(iot, ioh, (0x2AAA << 1), 0x5555);
411 1.1.6.2 chap bus_space_write_2(iot, ioh, (0x5555 << 1), 0x8080);
412 1.1.6.2 chap bus_space_write_2(iot, ioh, (0x5555 << 1), 0xAAAA);
413 1.1.6.2 chap bus_space_write_2(iot, ioh, (0x2AAA << 1), 0x5555);
414 1.1.6.2 chap
415 1.1.6.2 chap bus_space_write_2(iot, ioh, offset, 0x3030);
416 1.1.6.2 chap
417 1.1.6.2 chap /*
418 1.1.6.2 chap * NB: with CFI, we could get more meaningful timeout data for
419 1.1.6.2 chap * now we just assign reasonable values - 10 msec typical, and
420 1.1.6.2 chap * up to 60 secs to erase the whole sector.
421 1.1.6.2 chap */
422 1.1.6.2 chap
423 1.1.6.2 chap return toggle_bit_wait(sc, offset, 10000, 60000000, 0);
424 1.1.6.2 chap }
425 1.1.6.2 chap
426 1.1.6.2 chap static int
427 1.1.6.2 chap flash_sector_write(struct flash_softc *sc, bus_size_t offset)
428 1.1.6.2 chap {
429 1.1.6.2 chap bus_space_tag_t iot = sc->sc_iot;
430 1.1.6.2 chap bus_space_handle_t ioh = sc->sc_ioh;
431 1.1.6.2 chap bus_size_t fence;
432 1.1.6.2 chap const u_int16_t *p;
433 1.1.6.2 chap
434 1.1.6.2 chap p = (u_int16_t *) sc->sc_buf;
435 1.1.6.2 chap fence = offset + sc->sc_sector_size;
436 1.1.6.2 chap do {
437 1.1.6.2 chap bus_space_write_2(iot, ioh, (0x5555 << 1), 0xAAAA);
438 1.1.6.2 chap bus_space_write_2(iot, ioh, (0x2AAA << 1), 0x5555);
439 1.1.6.2 chap bus_space_write_2(iot, ioh, (0xAAAA << 1), 0xA0A0);
440 1.1.6.2 chap
441 1.1.6.2 chap bus_space_write_2(iot, ioh, offset, *p);
442 1.1.6.2 chap
443 1.1.6.2 chap /* wait up to 1 msec, in 10 usec increments, no sleeping */
444 1.1.6.2 chap if (toggle_bit_wait(sc, offset, 10, 1000, 1) != 0)
445 1.1.6.2 chap return ETIMEDOUT;
446 1.1.6.2 chap p++;
447 1.1.6.2 chap offset += 2;
448 1.1.6.2 chap } while (offset < fence);
449 1.1.6.2 chap
450 1.1.6.2 chap return 0;
451 1.1.6.2 chap }
452