nor.c revision 1.4.8.2 1 1.4.8.2 matt /* $NetBSD: nor.c,v 1.4.8.2 2011/12/27 17:35:48 matt Exp $ */
2 1.4.8.2 matt
3 1.4.8.2 matt /*-
4 1.4.8.2 matt * Copyright (c) 2011 Department of Software Engineering,
5 1.4.8.2 matt * University of Szeged, Hungary
6 1.4.8.2 matt * Copyright (c) 2011 Adam Hoka <ahoka (at) NetBSD.org>
7 1.4.8.2 matt * All rights reserved.
8 1.4.8.2 matt *
9 1.4.8.2 matt * This code is derived from software contributed to The NetBSD Foundation
10 1.4.8.2 matt * by the Department of Software Engineering, University of Szeged, Hungary
11 1.4.8.2 matt *
12 1.4.8.2 matt * Redistribution and use in source and binary forms, with or without
13 1.4.8.2 matt * modification, are permitted provided that the following conditions
14 1.4.8.2 matt * are met:
15 1.4.8.2 matt * 1. Redistributions of source code must retain the above copyright
16 1.4.8.2 matt * notice, this list of conditions and the following disclaimer.
17 1.4.8.2 matt * 2. Redistributions in binary form must reproduce the above copyright
18 1.4.8.2 matt * notice, this list of conditions and the following disclaimer in the
19 1.4.8.2 matt * documentation and/or other materials provided with the distribution.
20 1.4.8.2 matt *
21 1.4.8.2 matt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.4.8.2 matt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.4.8.2 matt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.4.8.2 matt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.4.8.2 matt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 1.4.8.2 matt * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.4.8.2 matt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 1.4.8.2 matt * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 1.4.8.2 matt * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.4.8.2 matt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.4.8.2 matt * SUCH DAMAGE.
32 1.4.8.2 matt */
33 1.4.8.2 matt
34 1.4.8.2 matt /* Common driver for NOR chips implementing the ONFI CFI specification */
35 1.4.8.2 matt
36 1.4.8.2 matt #include <sys/cdefs.h>
37 1.4.8.2 matt __KERNEL_RCSID(0, "$NetBSD: nor.c,v 1.4.8.2 2011/12/27 17:35:48 matt Exp $");
38 1.4.8.2 matt
39 1.4.8.2 matt #include "locators.h"
40 1.4.8.2 matt #include "opt_nor.h"
41 1.4.8.2 matt
42 1.4.8.2 matt #include <sys/param.h>
43 1.4.8.2 matt #include <sys/types.h>
44 1.4.8.2 matt #include <sys/device.h>
45 1.4.8.2 matt #include <sys/kmem.h>
46 1.4.8.2 matt #include <sys/sysctl.h>
47 1.4.8.2 matt #include <sys/atomic.h>
48 1.4.8.2 matt
49 1.4.8.2 matt #include <dev/flash/flash.h>
50 1.4.8.2 matt #include <dev/flash/flash_io.h>
51 1.4.8.2 matt #include <dev/nor/nor.h>
52 1.4.8.2 matt
53 1.4.8.2 matt
54 1.4.8.2 matt static int nor_match(device_t, cfdata_t, void *);
55 1.4.8.2 matt static void nor_attach(device_t, device_t, void *);
56 1.4.8.2 matt static int nor_detach(device_t, int);
57 1.4.8.2 matt static bool nor_shutdown(device_t, int);
58 1.4.8.2 matt static int nor_print(void *, const char *);
59 1.4.8.2 matt static int nor_search(device_t, cfdata_t, const int *, void *);
60 1.4.8.2 matt
61 1.4.8.2 matt /* flash interface implementation */
62 1.4.8.2 matt static int nor_flash_isbad(device_t, flash_off_t, bool *);
63 1.4.8.2 matt static int nor_flash_markbad(device_t, flash_off_t);
64 1.4.8.2 matt static int nor_flash_write(device_t, flash_off_t, size_t, size_t *,
65 1.4.8.2 matt const u_char *);
66 1.4.8.2 matt static int nor_flash_read(device_t, flash_off_t, size_t, size_t *, uint8_t *);
67 1.4.8.2 matt static int nor_flash_erase_all(device_t);
68 1.4.8.2 matt static int nor_flash_erase(device_t, struct flash_erase_instruction *);
69 1.4.8.2 matt static int nor_flash_submit(device_t, buf_t *);
70 1.4.8.2 matt
71 1.4.8.2 matt /* default functions for driver development */
72 1.4.8.2 matt static void nor_default_select(device_t, bool);
73 1.4.8.2 matt static int nor_default_read_page(device_t, flash_off_t, uint8_t *);
74 1.4.8.2 matt static int nor_default_program_page(device_t, flash_off_t, const uint8_t *);
75 1.4.8.2 matt
76 1.4.8.2 matt static int nor_scan_media(device_t, struct nor_chip *);
77 1.4.8.2 matt
78 1.4.8.2 matt CFATTACH_DECL_NEW(nor, sizeof(struct nor_softc),
79 1.4.8.2 matt nor_match, nor_attach, nor_detach, NULL);
80 1.4.8.2 matt
81 1.4.8.2 matt #ifdef NOR_DEBUG
82 1.4.8.2 matt int nordebug = NOR_DEBUG;
83 1.4.8.2 matt #endif
84 1.4.8.2 matt
85 1.4.8.2 matt int nor_cachesync_timeout = 1;
86 1.4.8.2 matt int nor_cachesync_nodenum;
87 1.4.8.2 matt
88 1.4.8.2 matt struct flash_interface nor_flash_if = {
89 1.4.8.2 matt .type = FLASH_TYPE_NOR,
90 1.4.8.2 matt
91 1.4.8.2 matt .read = nor_flash_read,
92 1.4.8.2 matt .write = nor_flash_write,
93 1.4.8.2 matt .erase = nor_flash_erase,
94 1.4.8.2 matt .block_isbad = nor_flash_isbad,
95 1.4.8.2 matt .block_markbad = nor_flash_markbad,
96 1.4.8.2 matt
97 1.4.8.2 matt .submit = nor_flash_submit
98 1.4.8.2 matt };
99 1.4.8.2 matt
100 1.4.8.2 matt #ifdef NOR_VERBOSE
101 1.4.8.2 matt const struct nor_manufacturer nor_mfrs[] = {
102 1.4.8.2 matt { NOR_MFR_AMD, "AMD" },
103 1.4.8.2 matt { NOR_MFR_FUJITSU, "Fujitsu" },
104 1.4.8.2 matt { NOR_MFR_RENESAS, "Renesas" },
105 1.4.8.2 matt { NOR_MFR_STMICRO, "ST Micro" },
106 1.4.8.2 matt { NOR_MFR_MICRON, "Micron" },
107 1.4.8.2 matt { NOR_MFR_NATIONAL, "National" },
108 1.4.8.2 matt { NOR_MFR_TOSHIBA, "Toshiba" },
109 1.4.8.2 matt { NOR_MFR_HYNIX, "Hynix" },
110 1.4.8.2 matt { NOR_MFR_SAMSUNG, "Samsung" },
111 1.4.8.2 matt { NOR_MFR_UNKNOWN, "Unknown" }
112 1.4.8.2 matt };
113 1.4.8.2 matt
114 1.4.8.2 matt static const char *
115 1.4.8.2 matt nor_midtoname(int id)
116 1.4.8.2 matt {
117 1.4.8.2 matt int i;
118 1.4.8.2 matt
119 1.4.8.2 matt for (i = 0; nor_mfrs[i].id != 0; i++) {
120 1.4.8.2 matt if (nor_mfrs[i].id == id)
121 1.4.8.2 matt return nor_mfrs[i].name;
122 1.4.8.2 matt }
123 1.4.8.2 matt
124 1.4.8.2 matt KASSERT(nor_mfrs[i].id == 0);
125 1.4.8.2 matt
126 1.4.8.2 matt return nor_mfrs[i].name;
127 1.4.8.2 matt }
128 1.4.8.2 matt #endif
129 1.4.8.2 matt
130 1.4.8.2 matt /* ARGSUSED */
131 1.4.8.2 matt static int
132 1.4.8.2 matt nor_match(device_t parent, cfdata_t match, void *aux)
133 1.4.8.2 matt {
134 1.4.8.2 matt /* pseudo device, always attaches */
135 1.4.8.2 matt return 1;
136 1.4.8.2 matt }
137 1.4.8.2 matt
138 1.4.8.2 matt static void
139 1.4.8.2 matt nor_attach(device_t parent, device_t self, void *aux)
140 1.4.8.2 matt {
141 1.4.8.2 matt struct nor_softc * const sc = device_private(self);
142 1.4.8.2 matt struct nor_attach_args * const naa = aux;
143 1.4.8.2 matt struct nor_chip * const chip = &sc->sc_chip;
144 1.4.8.2 matt
145 1.4.8.2 matt sc->sc_dev = self;
146 1.4.8.2 matt sc->sc_controller_dev = parent;
147 1.4.8.2 matt sc->sc_nor_if = naa->naa_nor_if;
148 1.4.8.2 matt
149 1.4.8.2 matt aprint_naive("\n");
150 1.4.8.2 matt aprint_normal("\n");
151 1.4.8.2 matt
152 1.4.8.2 matt if (nor_scan_media(self, chip))
153 1.4.8.2 matt return;
154 1.4.8.2 matt
155 1.4.8.2 matt sc->sc_flash_if = nor_flash_if;
156 1.4.8.2 matt sc->sc_flash_if.erasesize = chip->nc_block_size;
157 1.4.8.2 matt sc->sc_flash_if.page_size = chip->nc_page_size;
158 1.4.8.2 matt sc->sc_flash_if.writesize = chip->nc_page_size;
159 1.4.8.2 matt
160 1.4.8.2 matt /* allocate cache */
161 1.4.8.2 matt #ifdef NOTYET
162 1.4.8.2 matt chip->nc_oob_cache = kmem_alloc(chip->nc_spare_size, KM_SLEEP);
163 1.4.8.2 matt #endif
164 1.4.8.2 matt chip->nc_page_cache = kmem_alloc(chip->nc_page_size, KM_SLEEP);
165 1.4.8.2 matt
166 1.4.8.2 matt mutex_init(&sc->sc_device_lock, MUTEX_DEFAULT, IPL_NONE);
167 1.4.8.2 matt
168 1.4.8.2 matt if (flash_sync_thread_init(&sc->sc_flash_io, self, &sc->sc_flash_if)) {
169 1.4.8.2 matt goto error;
170 1.4.8.2 matt }
171 1.4.8.2 matt
172 1.4.8.2 matt if (!pmf_device_register1(sc->sc_dev, NULL, NULL, nor_shutdown))
173 1.4.8.2 matt aprint_error_dev(sc->sc_dev,
174 1.4.8.2 matt "couldn't establish power handler\n");
175 1.4.8.2 matt
176 1.4.8.2 matt #ifdef NOR_BBT
177 1.4.8.2 matt nor_bbt_init(self);
178 1.4.8.2 matt nor_bbt_scan(self);
179 1.4.8.2 matt #endif
180 1.4.8.2 matt
181 1.4.8.2 matt /*
182 1.4.8.2 matt * Attach all our devices
183 1.4.8.2 matt */
184 1.4.8.2 matt config_search_ia(nor_search, self, NULL, NULL);
185 1.4.8.2 matt
186 1.4.8.2 matt return;
187 1.4.8.2 matt
188 1.4.8.2 matt error:
189 1.4.8.2 matt #ifdef NOTET
190 1.4.8.2 matt kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
191 1.4.8.2 matt #endif
192 1.4.8.2 matt kmem_free(chip->nc_page_cache, chip->nc_page_size);
193 1.4.8.2 matt mutex_destroy(&sc->sc_device_lock);
194 1.4.8.2 matt }
195 1.4.8.2 matt
196 1.4.8.2 matt static int
197 1.4.8.2 matt nor_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
198 1.4.8.2 matt {
199 1.4.8.2 matt struct nor_softc * const sc = device_private(parent);
200 1.4.8.2 matt struct nor_chip * const chip = &sc->sc_chip;
201 1.4.8.2 matt struct flash_attach_args faa;
202 1.4.8.2 matt
203 1.4.8.2 matt faa.partinfo.part_offset = cf->cf_loc[FLASHBUSCF_OFFSET];
204 1.4.8.2 matt
205 1.4.8.2 matt if (cf->cf_loc[FLASHBUSCF_SIZE] == 0) {
206 1.4.8.2 matt faa.partinfo.part_size =
207 1.4.8.2 matt chip->nc_size - faa.partinfo.part_offset;
208 1.4.8.2 matt } else {
209 1.4.8.2 matt faa.partinfo.part_size = cf->cf_loc[FLASHBUSCF_SIZE];
210 1.4.8.2 matt }
211 1.4.8.2 matt
212 1.4.8.2 matt if (cf->cf_loc[FLASHBUSCF_READONLY])
213 1.4.8.2 matt faa.partinfo.part_flags = FLASH_PART_READONLY;
214 1.4.8.2 matt else
215 1.4.8.2 matt faa.partinfo.part_flags = 0;
216 1.4.8.2 matt
217 1.4.8.2 matt faa.flash_if = &sc->sc_flash_if;
218 1.4.8.2 matt
219 1.4.8.2 matt if (config_match(parent, cf, &faa)) {
220 1.4.8.2 matt if (config_attach(parent, cf, &faa, nor_print) != NULL) {
221 1.4.8.2 matt return 0;
222 1.4.8.2 matt } else {
223 1.4.8.2 matt return 1;
224 1.4.8.2 matt }
225 1.4.8.2 matt }
226 1.4.8.2 matt
227 1.4.8.2 matt return 1;
228 1.4.8.2 matt }
229 1.4.8.2 matt
230 1.4.8.2 matt static int
231 1.4.8.2 matt nor_detach(device_t self, int flags)
232 1.4.8.2 matt {
233 1.4.8.2 matt struct nor_softc * const sc = device_private(self);
234 1.4.8.2 matt struct nor_chip * const chip = &sc->sc_chip;
235 1.4.8.2 matt int error = 0;
236 1.4.8.2 matt
237 1.4.8.2 matt error = config_detach_children(self, flags);
238 1.4.8.2 matt if (error) {
239 1.4.8.2 matt return error;
240 1.4.8.2 matt }
241 1.4.8.2 matt
242 1.4.8.2 matt flash_sync_thread_destroy(&sc->sc_flash_io);
243 1.4.8.2 matt #ifdef NOR_BBT
244 1.4.8.2 matt nor_bbt_detach(self);
245 1.4.8.2 matt #endif
246 1.4.8.2 matt #ifdef NOTET
247 1.4.8.2 matt /* free oob cache */
248 1.4.8.2 matt kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
249 1.4.8.2 matt #endif
250 1.4.8.2 matt kmem_free(chip->nc_page_cache, chip->nc_page_size);
251 1.4.8.2 matt
252 1.4.8.2 matt mutex_destroy(&sc->sc_device_lock);
253 1.4.8.2 matt
254 1.4.8.2 matt pmf_device_deregister(sc->sc_dev);
255 1.4.8.2 matt
256 1.4.8.2 matt return error;
257 1.4.8.2 matt }
258 1.4.8.2 matt
259 1.4.8.2 matt static int
260 1.4.8.2 matt nor_print(void *aux, const char *pnp)
261 1.4.8.2 matt {
262 1.4.8.2 matt if (pnp != NULL)
263 1.4.8.2 matt aprint_normal("nor at %s\n", pnp);
264 1.4.8.2 matt
265 1.4.8.2 matt return UNCONF;
266 1.4.8.2 matt }
267 1.4.8.2 matt
268 1.4.8.2 matt /* ask for a nor driver to attach to the controller */
269 1.4.8.2 matt device_t
270 1.4.8.2 matt nor_attach_mi(struct nor_interface * const nor_if, device_t parent)
271 1.4.8.2 matt {
272 1.4.8.2 matt struct nor_attach_args arg;
273 1.4.8.2 matt
274 1.4.8.2 matt KASSERT(nor_if != NULL);
275 1.4.8.2 matt
276 1.4.8.2 matt if (nor_if->select == NULL)
277 1.4.8.2 matt nor_if->select = &nor_default_select;
278 1.4.8.2 matt if (nor_if->read_page == NULL)
279 1.4.8.2 matt nor_if->read_page = &nor_default_read_page;
280 1.4.8.2 matt if (nor_if->program_page == NULL)
281 1.4.8.2 matt nor_if->program_page = &nor_default_program_page;
282 1.4.8.2 matt
283 1.4.8.2 matt arg.naa_nor_if = nor_if;
284 1.4.8.2 matt
285 1.4.8.2 matt device_t dev = config_found_ia(parent, "norbus", &arg, nor_print);
286 1.4.8.2 matt
287 1.4.8.2 matt return dev;
288 1.4.8.2 matt }
289 1.4.8.2 matt
290 1.4.8.2 matt static void
291 1.4.8.2 matt nor_default_select(device_t self, bool n)
292 1.4.8.2 matt {
293 1.4.8.2 matt /* do nothing */
294 1.4.8.2 matt return;
295 1.4.8.2 matt }
296 1.4.8.2 matt
297 1.4.8.2 matt static int
298 1.4.8.2 matt nor_flash_submit(device_t self, buf_t * const bp)
299 1.4.8.2 matt {
300 1.4.8.2 matt struct nor_softc * const sc = device_private(self);
301 1.4.8.2 matt
302 1.4.8.2 matt return flash_io_submit(&sc->sc_flash_io, bp);
303 1.4.8.2 matt }
304 1.4.8.2 matt
305 1.4.8.2 matt
306 1.4.8.2 matt /* default everything to reasonable values, to ease future api changes */
307 1.4.8.2 matt void
308 1.4.8.2 matt nor_init_interface(struct nor_interface * const nor_if)
309 1.4.8.2 matt {
310 1.4.8.2 matt nor_if->select = &nor_default_select;
311 1.4.8.2 matt nor_if->read_1 = NULL;
312 1.4.8.2 matt nor_if->read_2 = NULL;
313 1.4.8.2 matt nor_if->read_4 = NULL;
314 1.4.8.2 matt nor_if->read_buf_1 = NULL;
315 1.4.8.2 matt nor_if->read_buf_2 = NULL;
316 1.4.8.2 matt nor_if->read_buf_4 = NULL;
317 1.4.8.2 matt nor_if->write_1 = NULL;
318 1.4.8.2 matt nor_if->write_2 = NULL;
319 1.4.8.2 matt nor_if->write_4 = NULL;
320 1.4.8.2 matt nor_if->write_buf_1 = NULL;
321 1.4.8.2 matt nor_if->write_buf_2 = NULL;
322 1.4.8.2 matt nor_if->write_buf_4 = NULL;
323 1.4.8.2 matt nor_if->busy = NULL;
324 1.4.8.2 matt }
325 1.4.8.2 matt
326 1.4.8.2 matt #ifdef NOTYET
327 1.4.8.2 matt /* handle quirks here */
328 1.4.8.2 matt static void
329 1.4.8.2 matt nor_quirks(device_t self, struct nor_chip * const chip)
330 1.4.8.2 matt {
331 1.4.8.2 matt /* this is an example only! */
332 1.4.8.2 matt switch (chip->nc_manf_id) {
333 1.4.8.2 matt case NOR_MFR_SAMSUNG:
334 1.4.8.2 matt if (chip->nc_dev_id == 0x00) {
335 1.4.8.2 matt /* do something only samsung chips need */
336 1.4.8.2 matt /* or */
337 1.4.8.2 matt /* chip->nc_quirks |= NC_QUIRK_NO_READ_START */
338 1.4.8.2 matt }
339 1.4.8.2 matt }
340 1.4.8.2 matt
341 1.4.8.2 matt return;
342 1.4.8.2 matt }
343 1.4.8.2 matt #endif
344 1.4.8.2 matt
345 1.4.8.2 matt /**
346 1.4.8.2 matt * scan media to determine the chip's properties
347 1.4.8.2 matt * this function resets the device
348 1.4.8.2 matt */
349 1.4.8.2 matt static int
350 1.4.8.2 matt nor_scan_media(device_t self, struct nor_chip * const chip)
351 1.4.8.2 matt {
352 1.4.8.2 matt struct nor_softc * const sc = device_private(self);
353 1.4.8.2 matt char pbuf[3][sizeof("XXXX MB")];
354 1.4.8.2 matt
355 1.4.8.2 matt KASSERT(sc->sc_nor_if != NULL);
356 1.4.8.2 matt KASSERT(sc->sc_nor_if->scan_media != NULL);
357 1.4.8.2 matt int error = sc->sc_nor_if->scan_media(self, chip);
358 1.4.8.2 matt if (error != 0)
359 1.4.8.2 matt return error;
360 1.4.8.2 matt
361 1.4.8.2 matt #ifdef NOR_VERBOSE
362 1.4.8.2 matt aprint_normal_dev(self,
363 1.4.8.2 matt "manufacturer id: 0x%.4x (%s), device id: 0x%.4x\n",
364 1.4.8.2 matt chip->nc_manf_id,
365 1.4.8.2 matt nor_midtoname(chip->nc_manf_id),
366 1.4.8.2 matt chip->nc_dev_id);
367 1.4.8.2 matt #endif
368 1.4.8.2 matt
369 1.4.8.2 matt format_bytes(pbuf[0], sizeof(pbuf[0]), chip->nc_page_size);
370 1.4.8.2 matt format_bytes(pbuf[1], sizeof(pbuf[1]), chip->nc_spare_size);
371 1.4.8.2 matt format_bytes(pbuf[2], sizeof(pbuf[2]), chip->nc_block_size);
372 1.4.8.2 matt aprint_normal_dev(self,
373 1.4.8.2 matt "page size: %s, spare size: %s, block size: %s\n",
374 1.4.8.2 matt pbuf[0], pbuf[1], pbuf[2]);
375 1.4.8.2 matt
376 1.4.8.2 matt format_bytes(pbuf[0], sizeof(pbuf[0]), chip->nc_size);
377 1.4.8.2 matt aprint_normal_dev(self,
378 1.4.8.2 matt "LUN size: %" PRIu32 " blocks, LUNs: %" PRIu8
379 1.4.8.2 matt ", total storage size: %s\n",
380 1.4.8.2 matt chip->nc_lun_blocks, chip->nc_num_luns, pbuf[0]);
381 1.4.8.2 matt
382 1.4.8.2 matt #ifdef NOTYET
383 1.4.8.2 matt /* XXX does this apply to nor? */
384 1.4.8.2 matt /*
385 1.4.8.2 matt * calculate badblock marker offset in oob
386 1.4.8.2 matt * we try to be compatible with linux here
387 1.4.8.2 matt */
388 1.4.8.2 matt if (chip->nc_page_size > 512)
389 1.4.8.2 matt chip->nc_badmarker_offs = 0;
390 1.4.8.2 matt else
391 1.4.8.2 matt chip->nc_badmarker_offs = 5;
392 1.4.8.2 matt #endif
393 1.4.8.2 matt
394 1.4.8.2 matt /* Calculate page shift and mask */
395 1.4.8.2 matt chip->nc_page_shift = ffs(chip->nc_page_size) - 1;
396 1.4.8.2 matt chip->nc_page_mask = ~(chip->nc_page_size - 1);
397 1.4.8.2 matt /* same for block */
398 1.4.8.2 matt chip->nc_block_shift = ffs(chip->nc_block_size) - 1;
399 1.4.8.2 matt chip->nc_block_mask = ~(chip->nc_block_size - 1);
400 1.4.8.2 matt
401 1.4.8.2 matt #ifdef NOTYET
402 1.4.8.2 matt /* look for quirks here if needed in future */
403 1.4.8.2 matt nor_quirks(self, chip);
404 1.4.8.2 matt #endif
405 1.4.8.2 matt
406 1.4.8.2 matt return 0;
407 1.4.8.2 matt }
408 1.4.8.2 matt
409 1.4.8.2 matt /* ARGSUSED */
410 1.4.8.2 matt static bool
411 1.4.8.2 matt nor_shutdown(device_t self, int howto)
412 1.4.8.2 matt {
413 1.4.8.2 matt return true;
414 1.4.8.2 matt }
415 1.4.8.2 matt
416 1.4.8.2 matt /* implementation of the block device API */
417 1.4.8.2 matt
418 1.4.8.2 matt /* read a page, default implementation */
419 1.4.8.2 matt static int
420 1.4.8.2 matt nor_default_read_page(device_t self, flash_off_t offset, uint8_t * const data)
421 1.4.8.2 matt {
422 1.4.8.2 matt struct nor_softc * const sc = device_private(self);
423 1.4.8.2 matt struct nor_chip * const chip = &sc->sc_chip;
424 1.4.8.2 matt
425 1.4.8.2 matt /*
426 1.4.8.2 matt * access by specified access_width
427 1.4.8.2 matt * note: #bits == 1 << width
428 1.4.8.2 matt */
429 1.4.8.2 matt switch(sc->sc_nor_if->access_width) {
430 1.4.8.2 matt case 0:
431 1.4.8.2 matt nor_read_buf_1(self, offset, data, chip->nc_page_size);
432 1.4.8.2 matt break;
433 1.4.8.2 matt case 1:
434 1.4.8.2 matt nor_read_buf_2(self, offset, data, chip->nc_page_size);
435 1.4.8.2 matt break;
436 1.4.8.2 matt case 2:
437 1.4.8.2 matt nor_read_buf_4(self, offset, data, chip->nc_page_size);
438 1.4.8.2 matt break;
439 1.4.8.2 matt #ifdef NOTYET
440 1.4.8.2 matt case 3:
441 1.4.8.2 matt nor_read_buf_8(self, offset, data, chip->nc_page_size);
442 1.4.8.2 matt break;
443 1.4.8.2 matt #endif
444 1.4.8.2 matt default:
445 1.4.8.2 matt panic("%s: bad width %d\n", __func__, sc->sc_nor_if->access_width);
446 1.4.8.2 matt }
447 1.4.8.2 matt
448 1.4.8.2 matt #if 0
449 1.4.8.2 matt /* for debugging new drivers */
450 1.4.8.2 matt nor_dump_data("page", data, chip->nc_page_size);
451 1.4.8.2 matt #endif
452 1.4.8.2 matt
453 1.4.8.2 matt return 0;
454 1.4.8.2 matt }
455 1.4.8.2 matt
456 1.4.8.2 matt /* write a page, default implementation */
457 1.4.8.2 matt static int
458 1.4.8.2 matt nor_default_program_page(device_t self, flash_off_t offset,
459 1.4.8.2 matt const uint8_t * const data)
460 1.4.8.2 matt {
461 1.4.8.2 matt struct nor_softc * const sc = device_private(self);
462 1.4.8.2 matt struct nor_chip * const chip = &sc->sc_chip;
463 1.4.8.2 matt
464 1.4.8.2 matt /*
465 1.4.8.2 matt * access by specified width
466 1.4.8.2 matt * #bits == 1 << access_width
467 1.4.8.2 matt */
468 1.4.8.2 matt switch(sc->sc_nor_if->access_width) {
469 1.4.8.2 matt case 0:
470 1.4.8.2 matt nor_write_buf_1(self, offset, data, chip->nc_page_size);
471 1.4.8.2 matt break;
472 1.4.8.2 matt case 1:
473 1.4.8.2 matt nor_write_buf_2(self, offset, data, chip->nc_page_size);
474 1.4.8.2 matt break;
475 1.4.8.2 matt case 2:
476 1.4.8.2 matt nor_write_buf_4(self, offset, data, chip->nc_page_size);
477 1.4.8.2 matt break;
478 1.4.8.2 matt #ifdef NOTYET
479 1.4.8.2 matt case 3:
480 1.4.8.2 matt nor_write_buf_8(self, offset, data, chip->nc_page_size);
481 1.4.8.2 matt break;
482 1.4.8.2 matt #endif
483 1.4.8.2 matt default:
484 1.4.8.2 matt panic("%s: bad width %d\n", __func__,
485 1.4.8.2 matt sc->sc_nor_if->access_width);
486 1.4.8.2 matt }
487 1.4.8.2 matt
488 1.4.8.2 matt #if 0
489 1.4.8.2 matt /* for debugging new drivers */
490 1.4.8.2 matt nor_dump_data("page", data, chip->nc_page_size);
491 1.4.8.2 matt #endif
492 1.4.8.2 matt
493 1.4.8.2 matt return 0;
494 1.4.8.2 matt }
495 1.4.8.2 matt
496 1.4.8.2 matt /*
497 1.4.8.2 matt * nor_flash_erase_all - erase the entire chip
498 1.4.8.2 matt *
499 1.4.8.2 matt * XXX a good way to brick your system
500 1.4.8.2 matt */
501 1.4.8.2 matt static int
502 1.4.8.2 matt nor_flash_erase_all(device_t self)
503 1.4.8.2 matt {
504 1.4.8.2 matt struct nor_softc * const sc = device_private(self);
505 1.4.8.2 matt int error;
506 1.4.8.2 matt
507 1.4.8.2 matt mutex_enter(&sc->sc_device_lock);
508 1.4.8.2 matt error = nor_erase_all(self);
509 1.4.8.2 matt mutex_exit(&sc->sc_device_lock);
510 1.4.8.2 matt
511 1.4.8.2 matt return error;
512 1.4.8.2 matt }
513 1.4.8.2 matt
514 1.4.8.2 matt static int
515 1.4.8.2 matt nor_flash_erase(device_t self, struct flash_erase_instruction * const ei)
516 1.4.8.2 matt {
517 1.4.8.2 matt struct nor_softc * const sc = device_private(self);
518 1.4.8.2 matt struct nor_chip * const chip = &sc->sc_chip;
519 1.4.8.2 matt flash_off_t addr;
520 1.4.8.2 matt int error = 0;
521 1.4.8.2 matt
522 1.4.8.2 matt if (ei->ei_addr < 0 || ei->ei_len < chip->nc_block_size)
523 1.4.8.2 matt return EINVAL;
524 1.4.8.2 matt
525 1.4.8.2 matt if (ei->ei_addr + ei->ei_len > chip->nc_size) {
526 1.4.8.2 matt DPRINTF(("%s: erase address is past the end"
527 1.4.8.2 matt " of the device\n", __func__));
528 1.4.8.2 matt return EINVAL;
529 1.4.8.2 matt }
530 1.4.8.2 matt
531 1.4.8.2 matt if ((ei->ei_addr == 0) && (ei->ei_len == chip->nc_size)
532 1.4.8.2 matt && (sc->sc_nor_if->erase_all != NULL)) {
533 1.4.8.2 matt return nor_flash_erase_all(self);
534 1.4.8.2 matt }
535 1.4.8.2 matt
536 1.4.8.2 matt if (ei->ei_addr % chip->nc_block_size != 0) {
537 1.4.8.2 matt aprint_error_dev(self,
538 1.4.8.2 matt "nor_flash_erase: ei_addr (%ju) is not"
539 1.4.8.2 matt " a multiple of block size (%ju)\n",
540 1.4.8.2 matt (uintmax_t)ei->ei_addr,
541 1.4.8.2 matt (uintmax_t)chip->nc_block_size);
542 1.4.8.2 matt return EINVAL;
543 1.4.8.2 matt }
544 1.4.8.2 matt
545 1.4.8.2 matt if (ei->ei_len % chip->nc_block_size != 0) {
546 1.4.8.2 matt aprint_error_dev(self,
547 1.4.8.2 matt "nor_flash_erase: ei_len (%ju) is not"
548 1.4.8.2 matt " a multiple of block size (%ju)",
549 1.4.8.2 matt (uintmax_t)ei->ei_len,
550 1.4.8.2 matt (uintmax_t)chip->nc_block_size);
551 1.4.8.2 matt return EINVAL;
552 1.4.8.2 matt }
553 1.4.8.2 matt
554 1.4.8.2 matt mutex_enter(&sc->sc_device_lock);
555 1.4.8.2 matt addr = ei->ei_addr;
556 1.4.8.2 matt while (addr < ei->ei_addr + ei->ei_len) {
557 1.4.8.2 matt #ifdef NOTYET
558 1.4.8.2 matt if (nor_isbad(self, addr)) {
559 1.4.8.2 matt aprint_error_dev(self, "bad block encountered\n");
560 1.4.8.2 matt ei->ei_state = FLASH_ERASE_FAILED;
561 1.4.8.2 matt error = EIO;
562 1.4.8.2 matt goto out;
563 1.4.8.2 matt }
564 1.4.8.2 matt #endif
565 1.4.8.2 matt
566 1.4.8.2 matt error = nor_erase_block(self, addr);
567 1.4.8.2 matt if (error) {
568 1.4.8.2 matt ei->ei_state = FLASH_ERASE_FAILED;
569 1.4.8.2 matt goto out;
570 1.4.8.2 matt }
571 1.4.8.2 matt
572 1.4.8.2 matt addr += chip->nc_block_size;
573 1.4.8.2 matt }
574 1.4.8.2 matt mutex_exit(&sc->sc_device_lock);
575 1.4.8.2 matt
576 1.4.8.2 matt ei->ei_state = FLASH_ERASE_DONE;
577 1.4.8.2 matt if (ei->ei_callback != NULL) {
578 1.4.8.2 matt ei->ei_callback(ei);
579 1.4.8.2 matt }
580 1.4.8.2 matt
581 1.4.8.2 matt return 0;
582 1.4.8.2 matt out:
583 1.4.8.2 matt mutex_exit(&sc->sc_device_lock);
584 1.4.8.2 matt
585 1.4.8.2 matt return error;
586 1.4.8.2 matt }
587 1.4.8.2 matt
588 1.4.8.2 matt /*
589 1.4.8.2 matt * handle (page) unaligned write to nor
590 1.4.8.2 matt */
591 1.4.8.2 matt static int
592 1.4.8.2 matt nor_flash_write_unaligned(device_t self, flash_off_t offset, size_t len,
593 1.4.8.2 matt size_t * const retlen, const uint8_t * const buf)
594 1.4.8.2 matt {
595 1.4.8.2 matt struct nor_softc * const sc = device_private(self);
596 1.4.8.2 matt struct nor_chip * const chip = &sc->sc_chip;
597 1.4.8.2 matt flash_off_t first, last, firstoff;
598 1.4.8.2 matt const uint8_t *bufp;
599 1.4.8.2 matt flash_off_t addr;
600 1.4.8.2 matt size_t left, count;
601 1.4.8.2 matt int error = 0, i;
602 1.4.8.2 matt
603 1.4.8.2 matt first = offset & chip->nc_page_mask;
604 1.4.8.2 matt firstoff = offset & ~chip->nc_page_mask;
605 1.4.8.2 matt /* XXX check if this should be len - 1 */
606 1.4.8.2 matt last = (offset + len) & chip->nc_page_mask;
607 1.4.8.2 matt count = last - first + 1;
608 1.4.8.2 matt
609 1.4.8.2 matt addr = first;
610 1.4.8.2 matt *retlen = 0;
611 1.4.8.2 matt
612 1.4.8.2 matt mutex_enter(&sc->sc_device_lock);
613 1.4.8.2 matt if (count == 1) {
614 1.4.8.2 matt #ifdef NOTYET
615 1.4.8.2 matt if (nor_isbad(self, addr)) {
616 1.4.8.2 matt aprint_error_dev(self,
617 1.4.8.2 matt "nor_flash_write_unaligned: "
618 1.4.8.2 matt "bad block encountered\n");
619 1.4.8.2 matt error = EIO;
620 1.4.8.2 matt goto out;
621 1.4.8.2 matt }
622 1.4.8.2 matt #endif
623 1.4.8.2 matt
624 1.4.8.2 matt error = nor_read_page(self, addr, chip->nc_page_cache);
625 1.4.8.2 matt if (error) {
626 1.4.8.2 matt goto out;
627 1.4.8.2 matt }
628 1.4.8.2 matt
629 1.4.8.2 matt memcpy(chip->nc_page_cache + firstoff, buf, len);
630 1.4.8.2 matt
631 1.4.8.2 matt error = nor_program_page(self, addr, chip->nc_page_cache);
632 1.4.8.2 matt if (error) {
633 1.4.8.2 matt goto out;
634 1.4.8.2 matt }
635 1.4.8.2 matt
636 1.4.8.2 matt *retlen = len;
637 1.4.8.2 matt goto out;
638 1.4.8.2 matt }
639 1.4.8.2 matt
640 1.4.8.2 matt bufp = buf;
641 1.4.8.2 matt left = len;
642 1.4.8.2 matt
643 1.4.8.2 matt for (i = 0; i < count && left != 0; i++) {
644 1.4.8.2 matt #ifdef NOTYET
645 1.4.8.2 matt if (nor_isbad(self, addr)) {
646 1.4.8.2 matt aprint_error_dev(self,
647 1.4.8.2 matt "nor_flash_write_unaligned: "
648 1.4.8.2 matt "bad block encountered\n");
649 1.4.8.2 matt error = EIO;
650 1.4.8.2 matt goto out;
651 1.4.8.2 matt }
652 1.4.8.2 matt #endif
653 1.4.8.2 matt
654 1.4.8.2 matt if (i == 0) {
655 1.4.8.2 matt error = nor_read_page(self, addr, chip->nc_page_cache);
656 1.4.8.2 matt if (error) {
657 1.4.8.2 matt goto out;
658 1.4.8.2 matt }
659 1.4.8.2 matt
660 1.4.8.2 matt memcpy(chip->nc_page_cache + firstoff,
661 1.4.8.2 matt bufp, chip->nc_page_size - firstoff);
662 1.4.8.2 matt
663 1.4.8.2 matt printf("write page: %s: %d\n", __FILE__, __LINE__);
664 1.4.8.2 matt error = nor_program_page(self, addr,
665 1.4.8.2 matt chip->nc_page_cache);
666 1.4.8.2 matt if (error) {
667 1.4.8.2 matt goto out;
668 1.4.8.2 matt }
669 1.4.8.2 matt
670 1.4.8.2 matt bufp += chip->nc_page_size - firstoff;
671 1.4.8.2 matt left -= chip->nc_page_size - firstoff;
672 1.4.8.2 matt *retlen += chip->nc_page_size - firstoff;
673 1.4.8.2 matt
674 1.4.8.2 matt } else if (i == count - 1) {
675 1.4.8.2 matt error = nor_read_page(self, addr, chip->nc_page_cache);
676 1.4.8.2 matt if (error) {
677 1.4.8.2 matt goto out;
678 1.4.8.2 matt }
679 1.4.8.2 matt
680 1.4.8.2 matt memcpy(chip->nc_page_cache, bufp, left);
681 1.4.8.2 matt
682 1.4.8.2 matt error = nor_program_page(self, addr,
683 1.4.8.2 matt chip->nc_page_cache);
684 1.4.8.2 matt if (error) {
685 1.4.8.2 matt goto out;
686 1.4.8.2 matt }
687 1.4.8.2 matt
688 1.4.8.2 matt *retlen += left;
689 1.4.8.2 matt KASSERT(left < chip->nc_page_size);
690 1.4.8.2 matt
691 1.4.8.2 matt } else {
692 1.4.8.2 matt /* XXX debug */
693 1.4.8.2 matt if (left > chip->nc_page_size) {
694 1.4.8.2 matt printf("left: %zu, i: %d, count: %zu\n",
695 1.4.8.2 matt (size_t )left, i, count);
696 1.4.8.2 matt }
697 1.4.8.2 matt KASSERT(left > chip->nc_page_size);
698 1.4.8.2 matt
699 1.4.8.2 matt error = nor_program_page(self, addr, bufp);
700 1.4.8.2 matt if (error) {
701 1.4.8.2 matt goto out;
702 1.4.8.2 matt }
703 1.4.8.2 matt
704 1.4.8.2 matt bufp += chip->nc_page_size;
705 1.4.8.2 matt left -= chip->nc_page_size;
706 1.4.8.2 matt *retlen += chip->nc_page_size;
707 1.4.8.2 matt }
708 1.4.8.2 matt
709 1.4.8.2 matt addr += chip->nc_page_size;
710 1.4.8.2 matt }
711 1.4.8.2 matt
712 1.4.8.2 matt KASSERT(*retlen == len);
713 1.4.8.2 matt out:
714 1.4.8.2 matt mutex_exit(&sc->sc_device_lock);
715 1.4.8.2 matt
716 1.4.8.2 matt return error;
717 1.4.8.2 matt }
718 1.4.8.2 matt
719 1.4.8.2 matt static int
720 1.4.8.2 matt nor_flash_write(device_t self, flash_off_t offset, size_t len,
721 1.4.8.2 matt size_t * const retlen, const uint8_t * const buf)
722 1.4.8.2 matt {
723 1.4.8.2 matt struct nor_softc * const sc = device_private(self);
724 1.4.8.2 matt struct nor_chip * const chip = &sc->sc_chip;
725 1.4.8.2 matt const uint8_t *bufp;
726 1.4.8.2 matt size_t pages, page;
727 1.4.8.2 matt daddr_t addr;
728 1.4.8.2 matt int error = 0;
729 1.4.8.2 matt
730 1.4.8.2 matt if ((offset + len) > chip->nc_size) {
731 1.4.8.2 matt DPRINTF(("%s: write (off: 0x%jx, len: %ju),"
732 1.4.8.2 matt " exceeds device size (0x%jx)\n", __func__,
733 1.4.8.2 matt (uintmax_t)offset, (uintmax_t)len,
734 1.4.8.2 matt (uintmax_t)chip->nc_size));
735 1.4.8.2 matt return EINVAL;
736 1.4.8.2 matt }
737 1.4.8.2 matt
738 1.4.8.2 matt if (len % chip->nc_page_size != 0 ||
739 1.4.8.2 matt offset % chip->nc_page_size != 0) {
740 1.4.8.2 matt return nor_flash_write_unaligned(self,
741 1.4.8.2 matt offset, len, retlen, buf);
742 1.4.8.2 matt }
743 1.4.8.2 matt
744 1.4.8.2 matt pages = len / chip->nc_page_size;
745 1.4.8.2 matt KASSERT(pages != 0);
746 1.4.8.2 matt *retlen = 0;
747 1.4.8.2 matt
748 1.4.8.2 matt addr = offset;
749 1.4.8.2 matt bufp = buf;
750 1.4.8.2 matt
751 1.4.8.2 matt mutex_enter(&sc->sc_device_lock);
752 1.4.8.2 matt for (page = 0; page < pages; page++) {
753 1.4.8.2 matt #ifdef NOTYET
754 1.4.8.2 matt /* do we need this check here? */
755 1.4.8.2 matt if (nor_isbad(self, addr)) {
756 1.4.8.2 matt aprint_error_dev(self,
757 1.4.8.2 matt "nor_flash_write: bad block encountered\n");
758 1.4.8.2 matt
759 1.4.8.2 matt error = EIO;
760 1.4.8.2 matt goto out;
761 1.4.8.2 matt }
762 1.4.8.2 matt #endif
763 1.4.8.2 matt
764 1.4.8.2 matt error = nor_program_page(self, addr, bufp);
765 1.4.8.2 matt if (error) {
766 1.4.8.2 matt goto out;
767 1.4.8.2 matt }
768 1.4.8.2 matt
769 1.4.8.2 matt addr += chip->nc_page_size;
770 1.4.8.2 matt bufp += chip->nc_page_size;
771 1.4.8.2 matt *retlen += chip->nc_page_size;
772 1.4.8.2 matt }
773 1.4.8.2 matt out:
774 1.4.8.2 matt mutex_exit(&sc->sc_device_lock);
775 1.4.8.2 matt DPRINTF(("%s: retlen: %zu, len: %zu\n", __func__, *retlen, len));
776 1.4.8.2 matt
777 1.4.8.2 matt return error;
778 1.4.8.2 matt }
779 1.4.8.2 matt
780 1.4.8.2 matt /*
781 1.4.8.2 matt * handle (page) unaligned read from nor
782 1.4.8.2 matt */
783 1.4.8.2 matt static int
784 1.4.8.2 matt nor_flash_read_unaligned(device_t self, flash_off_t offset, size_t len,
785 1.4.8.2 matt size_t * const retlen, uint8_t * const buf)
786 1.4.8.2 matt {
787 1.4.8.2 matt struct nor_softc * const sc = device_private(self);
788 1.4.8.2 matt struct nor_chip * const chip = &sc->sc_chip;
789 1.4.8.2 matt daddr_t first, last, count, firstoff;
790 1.4.8.2 matt uint8_t *bufp;
791 1.4.8.2 matt daddr_t addr;
792 1.4.8.2 matt size_t left;
793 1.4.8.2 matt int error = 0, i;
794 1.4.8.2 matt
795 1.4.8.2 matt first = offset & chip->nc_page_mask;
796 1.4.8.2 matt firstoff = offset & ~chip->nc_page_mask;
797 1.4.8.2 matt last = (offset + len) & chip->nc_page_mask;
798 1.4.8.2 matt count = (last - first) / chip->nc_page_size + 1;
799 1.4.8.2 matt
800 1.4.8.2 matt addr = first;
801 1.4.8.2 matt bufp = buf;
802 1.4.8.2 matt left = len;
803 1.4.8.2 matt *retlen = 0;
804 1.4.8.2 matt
805 1.4.8.2 matt mutex_enter(&sc->sc_device_lock);
806 1.4.8.2 matt if (count == 1) {
807 1.4.8.2 matt error = nor_read_page(self, addr, chip->nc_page_cache);
808 1.4.8.2 matt if (error) {
809 1.4.8.2 matt goto out;
810 1.4.8.2 matt }
811 1.4.8.2 matt
812 1.4.8.2 matt memcpy(bufp, chip->nc_page_cache + firstoff, len);
813 1.4.8.2 matt
814 1.4.8.2 matt *retlen = len;
815 1.4.8.2 matt goto out;
816 1.4.8.2 matt }
817 1.4.8.2 matt
818 1.4.8.2 matt for (i = 0; i < count && left != 0; i++) {
819 1.4.8.2 matt /* XXX Why use the page cache here ? */
820 1.4.8.2 matt error = nor_read_page(self, addr, chip->nc_page_cache);
821 1.4.8.2 matt if (error) {
822 1.4.8.2 matt goto out;
823 1.4.8.2 matt }
824 1.4.8.2 matt
825 1.4.8.2 matt if (i == 0) {
826 1.4.8.2 matt memcpy(bufp, chip->nc_page_cache + firstoff,
827 1.4.8.2 matt chip->nc_page_size - firstoff);
828 1.4.8.2 matt
829 1.4.8.2 matt bufp += chip->nc_page_size - firstoff;
830 1.4.8.2 matt left -= chip->nc_page_size - firstoff;
831 1.4.8.2 matt *retlen += chip->nc_page_size - firstoff;
832 1.4.8.2 matt
833 1.4.8.2 matt } else if (i == count - 1) {
834 1.4.8.2 matt memcpy(bufp, chip->nc_page_cache, left);
835 1.4.8.2 matt *retlen += left;
836 1.4.8.2 matt KASSERT(left < chip->nc_page_size);
837 1.4.8.2 matt
838 1.4.8.2 matt } else {
839 1.4.8.2 matt memcpy(bufp, chip->nc_page_cache, chip->nc_page_size);
840 1.4.8.2 matt
841 1.4.8.2 matt bufp += chip->nc_page_size;
842 1.4.8.2 matt left -= chip->nc_page_size;
843 1.4.8.2 matt *retlen += chip->nc_page_size;
844 1.4.8.2 matt }
845 1.4.8.2 matt
846 1.4.8.2 matt addr += chip->nc_page_size;
847 1.4.8.2 matt }
848 1.4.8.2 matt KASSERT(*retlen == len);
849 1.4.8.2 matt out:
850 1.4.8.2 matt mutex_exit(&sc->sc_device_lock);
851 1.4.8.2 matt
852 1.4.8.2 matt return error;
853 1.4.8.2 matt }
854 1.4.8.2 matt
855 1.4.8.2 matt static int
856 1.4.8.2 matt nor_flash_read(device_t self, flash_off_t offset, size_t len,
857 1.4.8.2 matt size_t * const retlen, uint8_t * const buf)
858 1.4.8.2 matt {
859 1.4.8.2 matt struct nor_softc * const sc = device_private(self);
860 1.4.8.2 matt struct nor_chip * const chip = &sc->sc_chip;
861 1.4.8.2 matt uint8_t *bufp;
862 1.4.8.2 matt size_t addr;
863 1.4.8.2 matt size_t i, pages;
864 1.4.8.2 matt int error = 0;
865 1.4.8.2 matt
866 1.4.8.2 matt *retlen = 0;
867 1.4.8.2 matt
868 1.4.8.2 matt DPRINTF(("%s: off: 0x%jx, len: %zu\n",
869 1.4.8.2 matt __func__, (uintmax_t)offset, len));
870 1.4.8.2 matt
871 1.4.8.2 matt if (__predict_false((offset + len) > chip->nc_size)) {
872 1.4.8.2 matt DPRINTF(("%s: read (off: 0x%jx, len: %zu),"
873 1.4.8.2 matt " exceeds device size (%ju)\n", __func__,
874 1.4.8.2 matt (uintmax_t)offset, len, (uintmax_t)chip->nc_size));
875 1.4.8.2 matt return EINVAL;
876 1.4.8.2 matt }
877 1.4.8.2 matt
878 1.4.8.2 matt /* Handle unaligned access, shouldnt be needed when using the
879 1.4.8.2 matt * block device, as strategy handles it, so only low level
880 1.4.8.2 matt * accesses will use this path
881 1.4.8.2 matt */
882 1.4.8.2 matt /* XXX^2 */
883 1.4.8.2 matt #if 0
884 1.4.8.2 matt if (len < chip->nc_page_size)
885 1.4.8.2 matt panic("TODO page size is larger than read size");
886 1.4.8.2 matt #endif
887 1.4.8.2 matt
888 1.4.8.2 matt if (len % chip->nc_page_size != 0 ||
889 1.4.8.2 matt offset % chip->nc_page_size != 0) {
890 1.4.8.2 matt return nor_flash_read_unaligned(self,
891 1.4.8.2 matt offset, len, retlen, buf);
892 1.4.8.2 matt }
893 1.4.8.2 matt
894 1.4.8.2 matt bufp = buf;
895 1.4.8.2 matt addr = offset;
896 1.4.8.2 matt pages = len / chip->nc_page_size;
897 1.4.8.2 matt
898 1.4.8.2 matt mutex_enter(&sc->sc_device_lock);
899 1.4.8.2 matt for (i = 0; i < pages; i++) {
900 1.4.8.2 matt #ifdef NOTYET
901 1.4.8.2 matt /* XXX do we need this check here? */
902 1.4.8.2 matt if (nor_isbad(self, addr)) {
903 1.4.8.2 matt aprint_error_dev(self, "bad block encountered\n");
904 1.4.8.2 matt error = EIO;
905 1.4.8.2 matt goto out;
906 1.4.8.2 matt }
907 1.4.8.2 matt #endif
908 1.4.8.2 matt error = nor_read_page(self, addr, bufp);
909 1.4.8.2 matt if (error)
910 1.4.8.2 matt goto out;
911 1.4.8.2 matt
912 1.4.8.2 matt bufp += chip->nc_page_size;
913 1.4.8.2 matt addr += chip->nc_page_size;
914 1.4.8.2 matt *retlen += chip->nc_page_size;
915 1.4.8.2 matt }
916 1.4.8.2 matt out:
917 1.4.8.2 matt mutex_exit(&sc->sc_device_lock);
918 1.4.8.2 matt
919 1.4.8.2 matt return error;
920 1.4.8.2 matt }
921 1.4.8.2 matt
922 1.4.8.2 matt static int
923 1.4.8.2 matt nor_flash_isbad(device_t self, flash_off_t ofs, bool * const isbad)
924 1.4.8.2 matt {
925 1.4.8.2 matt struct nor_softc * const sc = device_private(self);
926 1.4.8.2 matt struct nor_chip * const chip = &sc->sc_chip;
927 1.4.8.2 matt #ifdef NOTYET
928 1.4.8.2 matt bool result;
929 1.4.8.2 matt #endif
930 1.4.8.2 matt
931 1.4.8.2 matt if (ofs > chip->nc_size) {
932 1.4.8.2 matt DPRINTF(("%s: offset 0x%jx is larger than"
933 1.4.8.2 matt " device size (0x%jx)\n", __func__,
934 1.4.8.2 matt (uintmax_t)ofs, (uintmax_t)chip->nc_size));
935 1.4.8.2 matt return EINVAL;
936 1.4.8.2 matt }
937 1.4.8.2 matt
938 1.4.8.2 matt if (ofs % chip->nc_block_size != 0) {
939 1.4.8.2 matt DPRINTF(("offset (0x%jx) is not the multiple of block size "
940 1.4.8.2 matt "(%ju)",
941 1.4.8.2 matt (uintmax_t)ofs, (uintmax_t)chip->nc_block_size));
942 1.4.8.2 matt return EINVAL;
943 1.4.8.2 matt }
944 1.4.8.2 matt
945 1.4.8.2 matt #ifdef NOTYET
946 1.4.8.2 matt mutex_enter(&sc->sc_device_lock);
947 1.4.8.2 matt result = nor_isbad(self, ofs);
948 1.4.8.2 matt mutex_exit(&sc->sc_device_lock);
949 1.4.8.2 matt
950 1.4.8.2 matt *isbad = result;
951 1.4.8.2 matt #else
952 1.4.8.2 matt *isbad = false;
953 1.4.8.2 matt #endif
954 1.4.8.2 matt
955 1.4.8.2 matt return 0;
956 1.4.8.2 matt }
957 1.4.8.2 matt
958 1.4.8.2 matt static int
959 1.4.8.2 matt nor_flash_markbad(device_t self, flash_off_t ofs)
960 1.4.8.2 matt {
961 1.4.8.2 matt struct nor_softc * const sc = device_private(self);
962 1.4.8.2 matt struct nor_chip * const chip = &sc->sc_chip;
963 1.4.8.2 matt
964 1.4.8.2 matt if (ofs > chip->nc_size) {
965 1.4.8.2 matt DPRINTF(("%s: offset 0x%jx is larger than"
966 1.4.8.2 matt " device size (0x%jx)\n", __func__,
967 1.4.8.2 matt ofs, (uintmax_t)chip->nc_size));
968 1.4.8.2 matt return EINVAL;
969 1.4.8.2 matt }
970 1.4.8.2 matt
971 1.4.8.2 matt if (ofs % chip->nc_block_size != 0) {
972 1.4.8.2 matt panic("offset (%ju) is not the multiple of block size (%ju)",
973 1.4.8.2 matt (uintmax_t)ofs, (uintmax_t)chip->nc_block_size);
974 1.4.8.2 matt }
975 1.4.8.2 matt
976 1.4.8.2 matt /* TODO: implement this */
977 1.4.8.2 matt
978 1.4.8.2 matt return 0;
979 1.4.8.2 matt }
980 1.4.8.2 matt
981 1.4.8.2 matt static int
982 1.4.8.2 matt sysctl_nor_verify(SYSCTLFN_ARGS)
983 1.4.8.2 matt {
984 1.4.8.2 matt int error, t;
985 1.4.8.2 matt struct sysctlnode node;
986 1.4.8.2 matt
987 1.4.8.2 matt node = *rnode;
988 1.4.8.2 matt t = *(int *)rnode->sysctl_data;
989 1.4.8.2 matt node.sysctl_data = &t;
990 1.4.8.2 matt error = sysctl_lookup(SYSCTLFN_CALL(&node));
991 1.4.8.2 matt if (error || newp == NULL)
992 1.4.8.2 matt return error;
993 1.4.8.2 matt
994 1.4.8.2 matt if (node.sysctl_num == nor_cachesync_nodenum) {
995 1.4.8.2 matt if (t <= 0 || t > 60)
996 1.4.8.2 matt return EINVAL;
997 1.4.8.2 matt } else {
998 1.4.8.2 matt return EINVAL;
999 1.4.8.2 matt }
1000 1.4.8.2 matt
1001 1.4.8.2 matt *(int *)rnode->sysctl_data = t;
1002 1.4.8.2 matt
1003 1.4.8.2 matt return 0;
1004 1.4.8.2 matt }
1005 1.4.8.2 matt
1006 1.4.8.2 matt SYSCTL_SETUP(sysctl_nor, "sysctl nor subtree setup")
1007 1.4.8.2 matt {
1008 1.4.8.2 matt int rc, nor_root_num;
1009 1.4.8.2 matt const struct sysctlnode *node;
1010 1.4.8.2 matt
1011 1.4.8.2 matt if ((rc = sysctl_createv(clog, 0, NULL, NULL,
1012 1.4.8.2 matt CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
1013 1.4.8.2 matt NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
1014 1.4.8.2 matt goto error;
1015 1.4.8.2 matt }
1016 1.4.8.2 matt
1017 1.4.8.2 matt if ((rc = sysctl_createv(clog, 0, NULL, &node,
1018 1.4.8.2 matt CTLFLAG_PERMANENT, CTLTYPE_NODE, "nor",
1019 1.4.8.2 matt SYSCTL_DESCR("NOR driver controls"),
1020 1.4.8.2 matt NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
1021 1.4.8.2 matt goto error;
1022 1.4.8.2 matt }
1023 1.4.8.2 matt
1024 1.4.8.2 matt nor_root_num = node->sysctl_num;
1025 1.4.8.2 matt
1026 1.4.8.2 matt if ((rc = sysctl_createv(clog, 0, NULL, &node,
1027 1.4.8.2 matt CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1028 1.4.8.2 matt CTLTYPE_INT, "cache_sync_timeout",
1029 1.4.8.2 matt SYSCTL_DESCR("NOR write cache sync timeout in seconds"),
1030 1.4.8.2 matt sysctl_nor_verify, 0, &nor_cachesync_timeout,
1031 1.4.8.2 matt 0, CTL_HW, nor_root_num, CTL_CREATE,
1032 1.4.8.2 matt CTL_EOL)) != 0) {
1033 1.4.8.2 matt goto error;
1034 1.4.8.2 matt }
1035 1.4.8.2 matt
1036 1.4.8.2 matt nor_cachesync_nodenum = node->sysctl_num;
1037 1.4.8.2 matt
1038 1.4.8.2 matt return;
1039 1.4.8.2 matt
1040 1.4.8.2 matt error:
1041 1.4.8.2 matt aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
1042 1.4.8.2 matt }
1043 1.4.8.2 matt
1044 1.4.8.2 matt MODULE(MODULE_CLASS_DRIVER, nor, "flash");
1045 1.4.8.2 matt
1046 1.4.8.2 matt #ifdef _MODULE
1047 1.4.8.2 matt #include "ioconf.c"
1048 1.4.8.2 matt #endif
1049 1.4.8.2 matt
1050 1.4.8.2 matt static int
1051 1.4.8.2 matt nor_modcmd(modcmd_t cmd, void *opaque)
1052 1.4.8.2 matt {
1053 1.4.8.2 matt switch (cmd) {
1054 1.4.8.2 matt case MODULE_CMD_INIT:
1055 1.4.8.2 matt #ifdef _MODULE
1056 1.4.8.2 matt return config_init_component(cfdriver_ioconf_nor,
1057 1.4.8.2 matt cfattach_ioconf_nor, cfdata_ioconf_nor);
1058 1.4.8.2 matt #else
1059 1.4.8.2 matt return 0;
1060 1.4.8.2 matt #endif
1061 1.4.8.2 matt case MODULE_CMD_FINI:
1062 1.4.8.2 matt #ifdef _MODULE
1063 1.4.8.2 matt return config_fini_component(cfdriver_ioconf_nor,
1064 1.4.8.2 matt cfattach_ioconf_nor, cfdata_ioconf_nor);
1065 1.4.8.2 matt #else
1066 1.4.8.2 matt return 0;
1067 1.4.8.2 matt #endif
1068 1.4.8.2 matt default:
1069 1.4.8.2 matt return ENOTTY;
1070 1.4.8.2 matt }
1071 1.4.8.2 matt }
1072