nand.c revision 1.2 1 1.2 ahoka /* $NetBSD: nand.c,v 1.2 2011/03/09 07:49:15 ahoka Exp $ */
2 1.1 ahoka
3 1.1 ahoka /*-
4 1.1 ahoka * Copyright (c) 2010 Department of Software Engineering,
5 1.1 ahoka * University of Szeged, Hungary
6 1.1 ahoka * Copyright (c) 2010 Adam Hoka <ahoka (at) NetBSD.org>
7 1.1 ahoka * All rights reserved.
8 1.1 ahoka *
9 1.1 ahoka * This code is derived from software contributed to The NetBSD Foundation
10 1.1 ahoka * by the Department of Software Engineering, University of Szeged, Hungary
11 1.1 ahoka *
12 1.1 ahoka * Redistribution and use in source and binary forms, with or without
13 1.1 ahoka * modification, are permitted provided that the following conditions
14 1.1 ahoka * are met:
15 1.1 ahoka * 1. Redistributions of source code must retain the above copyright
16 1.1 ahoka * notice, this list of conditions and the following disclaimer.
17 1.1 ahoka * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 ahoka * notice, this list of conditions and the following disclaimer in the
19 1.1 ahoka * documentation and/or other materials provided with the distribution.
20 1.1 ahoka *
21 1.1 ahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 ahoka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 ahoka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 ahoka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 ahoka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 1.1 ahoka * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.1 ahoka * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 1.1 ahoka * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 1.1 ahoka * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 ahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 ahoka * SUCH DAMAGE.
32 1.1 ahoka */
33 1.1 ahoka
34 1.1 ahoka /* Common driver for NAND chips implementing the ONFI 2.2 specification */
35 1.1 ahoka
36 1.1 ahoka #include <sys/cdefs.h>
37 1.2 ahoka __KERNEL_RCSID(0, "$NetBSD: nand.c,v 1.2 2011/03/09 07:49:15 ahoka Exp $");
38 1.1 ahoka
39 1.1 ahoka #include "locators.h"
40 1.1 ahoka
41 1.1 ahoka #include <sys/param.h>
42 1.1 ahoka #include <sys/types.h>
43 1.1 ahoka #include <sys/device.h>
44 1.1 ahoka #include <sys/kmem.h>
45 1.1 ahoka #include <sys/sysctl.h>
46 1.1 ahoka
47 1.1 ahoka #include <dev/flash/flash.h>
48 1.1 ahoka #include <dev/nand/nand.h>
49 1.1 ahoka #include <dev/nand/onfi.h>
50 1.1 ahoka #include <dev/nand/hamming.h>
51 1.1 ahoka #include <dev/nand/nand_bbt.h>
52 1.1 ahoka #include <dev/nand/nand_crc.h>
53 1.1 ahoka
54 1.1 ahoka #include "opt_nand.h"
55 1.1 ahoka
56 1.2 ahoka int nand_match(device_t, cfdata_t, void *);
57 1.2 ahoka void nand_attach(device_t, device_t, void *);
58 1.2 ahoka int nand_detach(device_t, int);
59 1.2 ahoka bool nand_shutdown(device_t, int);
60 1.2 ahoka
61 1.2 ahoka int nand_print(void *, const char *);
62 1.2 ahoka
63 1.2 ahoka static int nand_search(device_t, cfdata_t, const int *, void *);
64 1.2 ahoka static void nand_address_row(device_t, size_t);
65 1.2 ahoka static void nand_address_column(device_t, size_t, size_t);
66 1.2 ahoka static void nand_readid(device_t, struct nand_chip *);
67 1.2 ahoka static void nand_read_parameter_page(device_t, struct nand_chip *);
68 1.2 ahoka static const char *nand_midtoname(int);
69 1.2 ahoka static int nand_scan_media(device_t, struct nand_chip *);
70 1.2 ahoka static bool nand_check_wp(device_t);
71 1.1 ahoka
72 1.1 ahoka CFATTACH_DECL_NEW(nand, sizeof(struct nand_softc),
73 1.1 ahoka nand_match, nand_attach, nand_detach, NULL);
74 1.1 ahoka
75 1.1 ahoka #ifdef NAND_DEBUG
76 1.1 ahoka int nanddebug = NAND_DEBUG;
77 1.1 ahoka #endif
78 1.1 ahoka
79 1.1 ahoka int nand_cachesync_timeout = 1;
80 1.1 ahoka int nand_cachesync_nodenum;
81 1.1 ahoka
82 1.1 ahoka const struct nand_manufacturer nand_mfrs[] = {
83 1.1 ahoka { NAND_MFR_AMD, "AMD" },
84 1.1 ahoka { NAND_MFR_FUJITSU, "Fujitsu" },
85 1.1 ahoka { NAND_MFR_RENESAS, "Renesas" },
86 1.1 ahoka { NAND_MFR_STMICRO, "ST Micro" },
87 1.1 ahoka { NAND_MFR_MICRON, "Micron" },
88 1.1 ahoka { NAND_MFR_NATIONAL, "National" },
89 1.1 ahoka { NAND_MFR_TOSHIBA, "Toshiba" },
90 1.1 ahoka { NAND_MFR_HYNIX, "Hynix" },
91 1.1 ahoka { NAND_MFR_SAMSUNG, "Samsung" },
92 1.1 ahoka { NAND_MFR_UNKNOWN, "Unknown" }
93 1.1 ahoka };
94 1.1 ahoka
95 1.1 ahoka /* ARGSUSED */
96 1.1 ahoka int
97 1.1 ahoka nand_match(device_t parent, cfdata_t match, void *aux)
98 1.1 ahoka {
99 1.1 ahoka /* pseudo device, always attaches */
100 1.1 ahoka return 1;
101 1.1 ahoka }
102 1.1 ahoka
103 1.1 ahoka void
104 1.1 ahoka nand_attach(device_t parent, device_t self, void *aux)
105 1.1 ahoka {
106 1.1 ahoka struct nand_softc *sc = device_private(self);
107 1.1 ahoka struct nand_attach_args *naa = aux;
108 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
109 1.1 ahoka
110 1.1 ahoka sc->sc_dev = self;
111 1.1 ahoka sc->nand_dev = parent;
112 1.1 ahoka sc->nand_if = naa->naa_nand_if;
113 1.1 ahoka
114 1.1 ahoka aprint_naive("\n");
115 1.1 ahoka
116 1.1 ahoka if (nand_check_wp(self)) {
117 1.1 ahoka aprint_error("NAND chip is write protected!\n");
118 1.1 ahoka return;
119 1.1 ahoka }
120 1.1 ahoka if (nand_scan_media(self, chip))
121 1.1 ahoka return;
122 1.1 ahoka
123 1.1 ahoka /* allocate cache */
124 1.1 ahoka chip->nc_oob_cache = kmem_alloc(chip->nc_spare_size, KM_SLEEP);
125 1.1 ahoka chip->nc_page_cache = kmem_alloc(chip->nc_page_size, KM_SLEEP);
126 1.1 ahoka
127 1.1 ahoka mutex_init(&sc->sc_device_lock, MUTEX_DEFAULT, IPL_NONE);
128 1.1 ahoka
129 1.1 ahoka if (nand_sync_thread_start(self)) {
130 1.1 ahoka goto error;
131 1.1 ahoka }
132 1.1 ahoka
133 1.1 ahoka if (!pmf_device_register1(sc->sc_dev, NULL, NULL, nand_shutdown))
134 1.1 ahoka aprint_error_dev(sc->sc_dev,
135 1.1 ahoka "couldn't establish power handler\n");
136 1.1 ahoka
137 1.1 ahoka #ifdef NAND_BBT
138 1.1 ahoka nand_bbt_init(self);
139 1.1 ahoka nand_bbt_scan(self);
140 1.1 ahoka #endif
141 1.1 ahoka
142 1.1 ahoka /*
143 1.1 ahoka * Attach all our devices
144 1.1 ahoka */
145 1.1 ahoka config_search_ia(nand_search, self, NULL, NULL);
146 1.1 ahoka
147 1.1 ahoka return;
148 1.1 ahoka error:
149 1.1 ahoka kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
150 1.1 ahoka kmem_free(chip->nc_page_cache, chip->nc_page_size);
151 1.1 ahoka mutex_destroy(&sc->sc_device_lock);
152 1.1 ahoka }
153 1.1 ahoka
154 1.1 ahoka static int
155 1.1 ahoka nand_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
156 1.1 ahoka {
157 1.1 ahoka struct nand_softc *sc = device_private(parent);
158 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
159 1.1 ahoka struct flash_interface *flash_if;
160 1.1 ahoka struct flash_attach_args faa;
161 1.1 ahoka
162 1.1 ahoka flash_if = kmem_alloc(sizeof(*flash_if), KM_SLEEP);
163 1.1 ahoka
164 1.1 ahoka flash_if->type = FLASH_TYPE_NAND;
165 1.1 ahoka
166 1.1 ahoka flash_if->read = nand_flash_read;
167 1.1 ahoka flash_if->write = nand_flash_write;
168 1.1 ahoka flash_if->erase = nand_flash_erase;
169 1.1 ahoka flash_if->block_isbad = nand_flash_isbad;
170 1.1 ahoka flash_if->block_markbad = nand_flash_markbad;
171 1.1 ahoka
172 1.1 ahoka flash_if->submit = nand_io_submit;
173 1.1 ahoka
174 1.1 ahoka flash_if->erasesize = chip->nc_block_size;
175 1.1 ahoka flash_if->page_size = chip->nc_page_size;
176 1.1 ahoka flash_if->writesize = chip->nc_page_size;
177 1.1 ahoka
178 1.1 ahoka flash_if->partition.part_offset = cf->cf_loc[FLASHBUSCF_OFFSET];
179 1.1 ahoka
180 1.1 ahoka if (cf->cf_loc[FLASHBUSCF_SIZE] == 0) {
181 1.1 ahoka flash_if->size = chip->nc_size -
182 1.1 ahoka flash_if->partition.part_offset;
183 1.1 ahoka flash_if->partition.part_size = flash_if->size;
184 1.1 ahoka } else {
185 1.1 ahoka flash_if->size = cf->cf_loc[FLASHBUSCF_SIZE];
186 1.1 ahoka flash_if->partition.part_size = cf->cf_loc[FLASHBUSCF_SIZE];
187 1.1 ahoka }
188 1.1 ahoka
189 1.1 ahoka if (cf->cf_loc[FLASHBUSCF_READONLY])
190 1.1 ahoka flash_if->partition.part_flags = FLASH_PART_READONLY;
191 1.1 ahoka else
192 1.1 ahoka flash_if->partition.part_flags = 0;
193 1.1 ahoka
194 1.1 ahoka faa.flash_if = flash_if;
195 1.1 ahoka
196 1.1 ahoka if (config_match(parent, cf, &faa)) {
197 1.1 ahoka config_attach(parent, cf, &faa, nand_print);
198 1.1 ahoka return 0;
199 1.1 ahoka } else {
200 1.1 ahoka kmem_free(flash_if, sizeof(*flash_if));
201 1.1 ahoka }
202 1.1 ahoka
203 1.1 ahoka return 1;
204 1.1 ahoka }
205 1.1 ahoka
206 1.1 ahoka int
207 1.1 ahoka nand_detach(device_t self, int flags)
208 1.1 ahoka {
209 1.1 ahoka struct nand_softc *sc = device_private(self);
210 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
211 1.1 ahoka int ret = 0;
212 1.1 ahoka
213 1.1 ahoka #ifdef NAND_BBT
214 1.1 ahoka nand_bbt_detach(self);
215 1.1 ahoka #endif
216 1.1 ahoka nand_sync_thread_stop(self);
217 1.1 ahoka
218 1.1 ahoka /* free oob cache */
219 1.1 ahoka kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
220 1.1 ahoka kmem_free(chip->nc_page_cache, chip->nc_page_size);
221 1.1 ahoka kmem_free(chip->nc_ecc_cache, chip->nc_ecc->necc_size);
222 1.1 ahoka
223 1.1 ahoka mutex_destroy(&sc->sc_device_lock);
224 1.1 ahoka
225 1.1 ahoka pmf_device_deregister(sc->sc_dev);
226 1.1 ahoka
227 1.1 ahoka return ret;
228 1.1 ahoka }
229 1.1 ahoka
230 1.1 ahoka int
231 1.1 ahoka nand_print(void *aux, const char *pnp)
232 1.1 ahoka {
233 1.1 ahoka if (pnp != NULL)
234 1.1 ahoka aprint_normal("nand at %s\n", pnp);
235 1.1 ahoka
236 1.1 ahoka return UNCONF;
237 1.1 ahoka }
238 1.1 ahoka
239 1.1 ahoka device_t
240 1.1 ahoka nand_attach_mi(struct nand_interface *nand_if, device_t parent)
241 1.1 ahoka {
242 1.1 ahoka struct nand_attach_args arg;
243 1.1 ahoka
244 1.1 ahoka KASSERT(nand_if != NULL);
245 1.1 ahoka
246 1.1 ahoka arg.naa_nand_if = nand_if;
247 1.1 ahoka return config_found_ia(parent, "nandbus", &arg, nand_print);
248 1.1 ahoka }
249 1.1 ahoka
250 1.1 ahoka static const char *
251 1.1 ahoka nand_midtoname(int id)
252 1.1 ahoka {
253 1.1 ahoka int i;
254 1.1 ahoka
255 1.1 ahoka for (i = 0; nand_mfrs[i].id != 0; i++) {
256 1.1 ahoka if (nand_mfrs[i].id == id)
257 1.1 ahoka return nand_mfrs[i].name;
258 1.1 ahoka }
259 1.1 ahoka
260 1.1 ahoka KASSERT(nand_mfrs[i].id == 0);
261 1.1 ahoka
262 1.1 ahoka return nand_mfrs[i].name;
263 1.1 ahoka }
264 1.1 ahoka
265 1.1 ahoka #if 0
266 1.1 ahoka /* handle quirks here */
267 1.1 ahoka static void
268 1.1 ahoka nand_quirks(device_t self, struct nand_chip *chip)
269 1.1 ahoka {
270 1.1 ahoka /* this is an example only! */
271 1.1 ahoka switch (chip->nc_manf_id) {
272 1.1 ahoka case NAND_MFR_SAMSUNG:
273 1.1 ahoka if (chip->nc_dev_id == 0x00) {
274 1.1 ahoka /* do something only samsung chips need */
275 1.1 ahoka /* or */
276 1.1 ahoka /* chip->nc_quirks |= NC_QUIRK_NO_READ_START */
277 1.1 ahoka }
278 1.1 ahoka }
279 1.1 ahoka
280 1.1 ahoka return;
281 1.1 ahoka }
282 1.1 ahoka #endif
283 1.1 ahoka
284 1.1 ahoka /**
285 1.1 ahoka * scan media to determine the chip's properties
286 1.1 ahoka * this function resets the device
287 1.1 ahoka */
288 1.1 ahoka static int
289 1.1 ahoka nand_scan_media(device_t self, struct nand_chip *chip)
290 1.1 ahoka {
291 1.1 ahoka struct nand_softc *sc = device_private(self);
292 1.1 ahoka struct nand_ecc *ecc;
293 1.1 ahoka uint8_t onfi_signature[4];
294 1.1 ahoka
295 1.1 ahoka nand_select(self, true);
296 1.1 ahoka nand_command(self, ONFI_RESET);
297 1.1 ahoka nand_select(self, false);
298 1.1 ahoka
299 1.1 ahoka nand_select(self, true);
300 1.1 ahoka nand_command(self, ONFI_READ_ID);
301 1.1 ahoka nand_address(self, 0x20);
302 1.1 ahoka nand_read_byte(self, &onfi_signature[0]);
303 1.1 ahoka nand_read_byte(self, &onfi_signature[1]);
304 1.1 ahoka nand_read_byte(self, &onfi_signature[2]);
305 1.1 ahoka nand_read_byte(self, &onfi_signature[3]);
306 1.1 ahoka nand_select(self, false);
307 1.1 ahoka
308 1.1 ahoka if (onfi_signature[0] != 'O' || onfi_signature[1] != 'N' ||
309 1.1 ahoka onfi_signature[2] != 'F' || onfi_signature[3] != 'I') {
310 1.1 ahoka aprint_error_dev(self,
311 1.1 ahoka "device does not support the ONFI specification\n");
312 1.1 ahoka
313 1.1 ahoka return 1;
314 1.1 ahoka }
315 1.1 ahoka
316 1.1 ahoka nand_readid(self, chip);
317 1.1 ahoka
318 1.1 ahoka aprint_normal(": NAND Flash\n");
319 1.1 ahoka
320 1.1 ahoka aprint_debug_dev(self,
321 1.1 ahoka "manufacturer id: 0x%.2x (%s), device id: 0x%.2x\n",
322 1.1 ahoka chip->nc_manf_id,
323 1.1 ahoka nand_midtoname(chip->nc_manf_id),
324 1.1 ahoka chip->nc_dev_id);
325 1.1 ahoka
326 1.1 ahoka nand_read_parameter_page(self, chip);
327 1.1 ahoka
328 1.1 ahoka ecc = chip->nc_ecc = &sc->nand_if->ecc;
329 1.1 ahoka
330 1.1 ahoka /*
331 1.1 ahoka * calculate the place of ecc data in oob
332 1.1 ahoka * we try to be compatible with Linux here
333 1.1 ahoka */
334 1.1 ahoka switch (chip->nc_spare_size) {
335 1.1 ahoka case 8:
336 1.1 ahoka ecc->necc_offset = 0;
337 1.1 ahoka break;
338 1.1 ahoka case 16:
339 1.1 ahoka ecc->necc_offset = 0;
340 1.1 ahoka break;
341 1.1 ahoka case 64:
342 1.1 ahoka ecc->necc_offset = 40;
343 1.1 ahoka break;
344 1.1 ahoka case 128:
345 1.1 ahoka ecc->necc_offset = 80;
346 1.1 ahoka break;
347 1.1 ahoka default:
348 1.1 ahoka panic("OOB size is unexpected");
349 1.1 ahoka }
350 1.1 ahoka
351 1.1 ahoka ecc->necc_steps = chip->nc_page_size / ecc->necc_block_size;
352 1.1 ahoka ecc->necc_size = ecc->necc_steps * ecc->necc_code_size;
353 1.1 ahoka
354 1.1 ahoka /* check if we fit in oob */
355 1.1 ahoka if (ecc->necc_offset + ecc->necc_size > chip->nc_spare_size) {
356 1.1 ahoka panic("NAND ECC bits dont fit in OOB");
357 1.1 ahoka }
358 1.1 ahoka
359 1.1 ahoka /* TODO: mark free oob area available for file systems */
360 1.1 ahoka
361 1.1 ahoka chip->nc_ecc_cache = kmem_zalloc(ecc->necc_size, KM_SLEEP);
362 1.1 ahoka
363 1.1 ahoka /*
364 1.1 ahoka * calculate badblock marker offset in oob
365 1.1 ahoka * we try to be compatible with linux here
366 1.1 ahoka */
367 1.1 ahoka if (chip->nc_page_size > 512)
368 1.1 ahoka chip->nc_badmarker_offs = 0;
369 1.1 ahoka else
370 1.1 ahoka chip->nc_badmarker_offs = 5;
371 1.1 ahoka
372 1.1 ahoka /* Calculate page shift and mask */
373 1.1 ahoka chip->nc_page_shift = ffs(chip->nc_page_size) - 1;
374 1.1 ahoka chip->nc_page_mask = ~(chip->nc_page_size - 1);
375 1.1 ahoka /* same for block */
376 1.1 ahoka chip->nc_block_shift = ffs(chip->nc_block_size) - 1;
377 1.1 ahoka chip->nc_block_mask = ~(chip->nc_block_size - 1);
378 1.1 ahoka
379 1.1 ahoka /* look for quirks here if needed in future */
380 1.1 ahoka /* nand_quirks(self, chip); */
381 1.1 ahoka
382 1.1 ahoka return 0;
383 1.1 ahoka }
384 1.1 ahoka
385 1.1 ahoka static void
386 1.1 ahoka nand_readid(device_t self, struct nand_chip *chip)
387 1.1 ahoka {
388 1.1 ahoka nand_select(self, true);
389 1.1 ahoka nand_command(self, ONFI_READ_ID);
390 1.1 ahoka nand_address(self, 0x00);
391 1.1 ahoka nand_read_byte(self, &chip->nc_manf_id);
392 1.1 ahoka nand_read_byte(self, &chip->nc_dev_id);
393 1.1 ahoka nand_select(self, false);
394 1.1 ahoka }
395 1.1 ahoka
396 1.1 ahoka /* read the parameter page. TODO: check CRC! */
397 1.1 ahoka static void
398 1.1 ahoka nand_read_parameter_page(device_t self, struct nand_chip *chip)
399 1.1 ahoka {
400 1.1 ahoka struct onfi_parameter_page params;
401 1.1 ahoka uint8_t *bufp;
402 1.1 ahoka uint8_t vendor[13], model[21];
403 1.1 ahoka uint16_t crc;
404 1.1 ahoka int i;
405 1.1 ahoka
406 1.1 ahoka KASSERT(sizeof(params) == 256);
407 1.1 ahoka
408 1.1 ahoka nand_select(self, true);
409 1.1 ahoka nand_command(self, ONFI_READ_PARAMETER_PAGE);
410 1.1 ahoka nand_address(self, 0x00);
411 1.1 ahoka
412 1.1 ahoka nand_busy(self);
413 1.1 ahoka
414 1.1 ahoka bufp = (uint8_t *)¶ms;
415 1.1 ahoka for (i = 0; i < 256; i++) {
416 1.1 ahoka nand_read_byte(self, &bufp[i]);
417 1.1 ahoka }
418 1.1 ahoka nand_select(self, false);
419 1.1 ahoka
420 1.1 ahoka /* validate the parameter page with the crc */
421 1.1 ahoka crc = nand_crc16(bufp, 254);
422 1.1 ahoka
423 1.1 ahoka if (crc != params.param_integrity_crc) {
424 1.1 ahoka aprint_error_dev(self, "parameter page crc check failed\n");
425 1.1 ahoka /* TODO: we should read the next parameter page copy */
426 1.1 ahoka }
427 1.1 ahoka
428 1.1 ahoka /* strip manufacturer and model string */
429 1.1 ahoka strlcpy(vendor, params.param_manufacturer, sizeof(vendor));
430 1.1 ahoka for (i = 11; i > 0 && vendor[i] == ' '; i--)
431 1.1 ahoka vendor[i] = 0;
432 1.1 ahoka strlcpy(model, params.param_model, sizeof(model));
433 1.1 ahoka for (i = 19; i > 0 && model[i] == ' '; i--)
434 1.1 ahoka model[i] = 0;
435 1.1 ahoka
436 1.1 ahoka aprint_normal_dev(self, "vendor: %s, model: %s\n", vendor, model);
437 1.1 ahoka
438 1.1 ahoka aprint_normal_dev(self,
439 1.1 ahoka "page size: %u bytes, spare size: %u bytes, block size: %u bytes\n",
440 1.1 ahoka params.param_pagesize, params.param_sparesize,
441 1.1 ahoka params.param_blocksize * params.param_pagesize);
442 1.1 ahoka
443 1.1 ahoka aprint_normal_dev(self,
444 1.1 ahoka "LUN size: %u blocks, LUNs: %u, total storage size: %u MB\n",
445 1.1 ahoka params.param_lunsize, params.param_numluns,
446 1.1 ahoka params.param_blocksize * params.param_pagesize *
447 1.1 ahoka params.param_lunsize * params.param_numluns / 1024 / 1024);
448 1.1 ahoka
449 1.1 ahoka /* XXX TODO multiple LUNs */
450 1.1 ahoka if (__predict_false(params.param_numluns != 1))
451 1.1 ahoka panic("more than one LUNs are not supported yet!\n");
452 1.1 ahoka
453 1.1 ahoka chip->nc_size = params.param_pagesize * params.param_blocksize *
454 1.1 ahoka params.param_lunsize * params.param_numluns;
455 1.1 ahoka
456 1.1 ahoka chip->nc_page_size = params.param_pagesize;
457 1.1 ahoka chip->nc_block_pages = params.param_blocksize;
458 1.1 ahoka chip->nc_block_size = params.param_blocksize * params.param_pagesize;
459 1.1 ahoka chip->nc_spare_size = params.param_sparesize;
460 1.1 ahoka
461 1.1 ahoka /* the lower 4 bits contain the row address cycles */
462 1.1 ahoka chip->nc_addr_cycles_row = params.param_addr_cycles & 0x07;
463 1.1 ahoka /* the upper 4 bits contain the column address cycles */
464 1.1 ahoka chip->nc_addr_cycles_column = (params.param_addr_cycles & ~0x07) >> 4;
465 1.1 ahoka
466 1.1 ahoka #ifdef NAND_VERBOSE
467 1.1 ahoka aprint_normal_dev(self, "column cycles: %d, row cycles: %d\n",
468 1.1 ahoka chip->nc_addr_cycles_column, chip->nc_addr_cycles_row);
469 1.1 ahoka #endif
470 1.1 ahoka
471 1.1 ahoka if (params.param_features & ONFI_FEATURE_16BIT)
472 1.1 ahoka chip->nc_flags |= NC_BUSWIDTH_16;
473 1.1 ahoka
474 1.1 ahoka if (params.param_features & ONFI_FEATURE_EXTENDED_PARAM)
475 1.1 ahoka chip->nc_flags |= NC_EXTENDED_PARAM;
476 1.1 ahoka }
477 1.1 ahoka
478 1.1 ahoka /* ARGSUSED */
479 1.1 ahoka bool
480 1.1 ahoka nand_shutdown(device_t self, int howto)
481 1.1 ahoka {
482 1.1 ahoka return true;
483 1.1 ahoka }
484 1.1 ahoka
485 1.1 ahoka static void
486 1.1 ahoka nand_address_column(device_t self, size_t row, size_t column)
487 1.1 ahoka {
488 1.1 ahoka struct nand_softc *sc = device_private(self);
489 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
490 1.1 ahoka uint8_t i;
491 1.1 ahoka
492 1.1 ahoka DPRINTF(("addressing row: 0x%jx column: %zu\n",
493 1.1 ahoka (uintmax_t )row, column));
494 1.1 ahoka
495 1.1 ahoka /* XXX TODO */
496 1.1 ahoka row >>= chip->nc_page_shift;
497 1.1 ahoka
498 1.1 ahoka /* Write the column (subpage) address */
499 1.1 ahoka if (chip->nc_flags & NC_BUSWIDTH_16)
500 1.1 ahoka column >>= 1;
501 1.1 ahoka for (i = 0; i < chip->nc_addr_cycles_column; i++, column >>= 8)
502 1.1 ahoka nand_address(self, column & 0xff);
503 1.1 ahoka
504 1.1 ahoka /* Write the row (page) address */
505 1.1 ahoka for (i = 0; i < chip->nc_addr_cycles_row; i++, row >>= 8)
506 1.1 ahoka nand_address(self, row & 0xff);
507 1.1 ahoka }
508 1.1 ahoka
509 1.1 ahoka static void
510 1.1 ahoka nand_address_row(device_t self, size_t row)
511 1.1 ahoka {
512 1.1 ahoka struct nand_softc *sc = device_private(self);
513 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
514 1.1 ahoka off_t i;
515 1.1 ahoka
516 1.1 ahoka /* XXX TODO */
517 1.1 ahoka row >>= chip->nc_page_shift;
518 1.1 ahoka
519 1.1 ahoka /* Write the row (page) address */
520 1.1 ahoka for (i = 0; i < chip->nc_addr_cycles_row; i++, row >>= 8)
521 1.1 ahoka nand_address(self, row & 0xff);
522 1.1 ahoka }
523 1.1 ahoka
524 1.1 ahoka static inline uint8_t
525 1.1 ahoka nand_get_status(device_t self)
526 1.1 ahoka {
527 1.1 ahoka uint8_t status;
528 1.1 ahoka
529 1.1 ahoka nand_command(self, ONFI_READ_STATUS);
530 1.1 ahoka nand_busy(self);
531 1.1 ahoka nand_read_byte(self, &status);
532 1.1 ahoka
533 1.1 ahoka return status;
534 1.1 ahoka }
535 1.1 ahoka
536 1.1 ahoka static bool
537 1.1 ahoka nand_check_wp(device_t self)
538 1.1 ahoka {
539 1.1 ahoka if (nand_get_status(self) & 0x80)
540 1.1 ahoka return false;
541 1.1 ahoka else
542 1.1 ahoka return true;
543 1.1 ahoka }
544 1.1 ahoka
545 1.1 ahoka static void
546 1.1 ahoka nand_prepare_read(device_t self, flash_addr_t row, flash_addr_t column)
547 1.1 ahoka {
548 1.1 ahoka nand_command(self, ONFI_READ);
549 1.1 ahoka nand_address_column(self, row, column);
550 1.1 ahoka nand_command(self, ONFI_READ_START);
551 1.1 ahoka
552 1.1 ahoka nand_busy(self);
553 1.1 ahoka }
554 1.1 ahoka
555 1.1 ahoka /* read a page with ecc correction */
556 1.1 ahoka int
557 1.1 ahoka nand_read_page(device_t self, size_t offset, uint8_t *data)
558 1.1 ahoka {
559 1.1 ahoka struct nand_softc *sc = device_private(self);
560 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
561 1.1 ahoka size_t b, bs, e, cs;
562 1.1 ahoka uint8_t *ecc;
563 1.1 ahoka int result;
564 1.1 ahoka
565 1.1 ahoka nand_prepare_read(self, offset, 0);
566 1.1 ahoka
567 1.1 ahoka bs = chip->nc_ecc->necc_block_size;
568 1.1 ahoka cs = chip->nc_ecc->necc_code_size;
569 1.1 ahoka
570 1.1 ahoka /* decide if we access by 8 or 16 bits */
571 1.1 ahoka if (chip->nc_flags & NC_BUSWIDTH_16) {
572 1.1 ahoka for (b = 0, e = 0; b < chip->nc_page_size; b += bs, e += cs) {
573 1.1 ahoka nand_ecc_prepare(self, NAND_ECC_READ);
574 1.1 ahoka nand_read_buf_word(self, data + b, bs);
575 1.1 ahoka nand_ecc_compute(self, data + b,
576 1.1 ahoka chip->nc_ecc_cache + e);
577 1.1 ahoka }
578 1.1 ahoka } else {
579 1.1 ahoka for (b = 0, e = 0; b < chip->nc_page_size; b += bs, e += cs) {
580 1.1 ahoka nand_ecc_prepare(self, NAND_ECC_READ);
581 1.1 ahoka nand_read_buf_byte(self, data + b, bs);
582 1.1 ahoka nand_ecc_compute(self, data + b,
583 1.1 ahoka chip->nc_ecc_cache + e);
584 1.1 ahoka }
585 1.1 ahoka }
586 1.1 ahoka
587 1.2 ahoka /* for debugging new drivers */
588 1.2 ahoka #if 0
589 1.2 ahoka nand_dump_data("page", data, chip->nc_page_size);
590 1.2 ahoka #endif
591 1.1 ahoka
592 1.1 ahoka nand_read_oob(self, offset, chip->nc_oob_cache);
593 1.1 ahoka ecc = chip->nc_oob_cache + chip->nc_ecc->necc_offset;
594 1.1 ahoka
595 1.1 ahoka /* useful for debugging new ecc drivers */
596 1.1 ahoka #if 0
597 1.1 ahoka printf("dumping ecc %d\n--------------\n", chip->nc_ecc->necc_steps);
598 1.1 ahoka for (e = 0; e < chip->nc_ecc->necc_steps; e++) {
599 1.1 ahoka printf("0x");
600 1.1 ahoka for (b = 0; b < cs; b++) {
601 1.1 ahoka printf("%.2hhx", ecc[e+b]);
602 1.1 ahoka }
603 1.1 ahoka printf(" 0x");
604 1.1 ahoka for (b = 0; b < cs; b++) {
605 1.1 ahoka printf("%.2hhx", chip->nc_ecc_cache[e+b]);
606 1.1 ahoka }
607 1.1 ahoka printf("\n");
608 1.1 ahoka }
609 1.1 ahoka printf("--------------\n");
610 1.1 ahoka #endif
611 1.1 ahoka
612 1.1 ahoka for (b = 0, e = 0; b < chip->nc_page_size; b += bs, e += cs) {
613 1.1 ahoka result = nand_ecc_correct(self, data + b, ecc + e,
614 1.1 ahoka chip->nc_ecc_cache + e);
615 1.1 ahoka
616 1.1 ahoka switch (result) {
617 1.1 ahoka case NAND_ECC_OK:
618 1.1 ahoka break;
619 1.1 ahoka case NAND_ECC_CORRECTED:
620 1.1 ahoka aprint_error_dev(self,
621 1.1 ahoka "data corrected with ECC at page offset 0x%jx "
622 1.1 ahoka "block %zu\n", (uintmax_t)offset, b);
623 1.1 ahoka break;
624 1.1 ahoka case NAND_ECC_TWOBIT:
625 1.1 ahoka aprint_error_dev(self,
626 1.1 ahoka "uncorrectable ECC error at page offset 0x%jx "
627 1.1 ahoka "block %zu\n", (uintmax_t)offset, b);
628 1.1 ahoka return EIO;
629 1.1 ahoka break;
630 1.1 ahoka case NAND_ECC_INVALID:
631 1.1 ahoka aprint_error_dev(self,
632 1.1 ahoka "invalid ECC in oob at page offset 0x%jx "
633 1.1 ahoka "block %zu\n", (uintmax_t)offset, b);
634 1.1 ahoka return EIO;
635 1.1 ahoka break;
636 1.1 ahoka default:
637 1.1 ahoka panic("invalid ECC correction errno");
638 1.1 ahoka }
639 1.1 ahoka }
640 1.1 ahoka
641 1.1 ahoka return 0;
642 1.1 ahoka }
643 1.1 ahoka
644 1.1 ahoka static int
645 1.1 ahoka nand_program_page(device_t self, size_t page, const uint8_t *data)
646 1.1 ahoka {
647 1.1 ahoka struct nand_softc *sc = device_private(self);
648 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
649 1.1 ahoka size_t bs, cs, e, b;
650 1.1 ahoka uint8_t status;
651 1.1 ahoka uint8_t *ecc;
652 1.1 ahoka
653 1.1 ahoka nand_command(self, ONFI_PAGE_PROGRAM);
654 1.1 ahoka nand_address_column(self, page, 0);
655 1.1 ahoka
656 1.1 ahoka nand_busy(self);
657 1.1 ahoka
658 1.1 ahoka bs = chip->nc_ecc->necc_block_size;
659 1.1 ahoka cs = chip->nc_ecc->necc_code_size;
660 1.1 ahoka ecc = chip->nc_oob_cache + chip->nc_ecc->necc_offset;
661 1.1 ahoka
662 1.1 ahoka /* XXX code duplication */
663 1.1 ahoka /* decide if we access by 8 or 16 bits */
664 1.1 ahoka if (chip->nc_flags & NC_BUSWIDTH_16) {
665 1.1 ahoka for (b = 0, e = 0; b < chip->nc_page_size; b += bs, e += cs) {
666 1.1 ahoka nand_ecc_prepare(self, NAND_ECC_WRITE);
667 1.1 ahoka nand_write_buf_word(self, data + b, bs);
668 1.1 ahoka nand_ecc_compute(self, data + b, ecc + e);
669 1.1 ahoka }
670 1.1 ahoka /* write oob with ecc correction code */
671 1.1 ahoka nand_write_buf_word(self, chip->nc_oob_cache,
672 1.1 ahoka chip->nc_spare_size);
673 1.1 ahoka } else {
674 1.1 ahoka for (b = 0, e = 0; b < chip->nc_page_size; b += bs, e += cs) {
675 1.1 ahoka nand_ecc_prepare(self, NAND_ECC_WRITE);
676 1.1 ahoka nand_write_buf_byte(self, data + b, bs);
677 1.1 ahoka nand_ecc_compute(self, data + b, ecc + e);
678 1.1 ahoka }
679 1.1 ahoka /* write oob with ecc correction code */
680 1.1 ahoka nand_write_buf_byte(self, chip->nc_oob_cache,
681 1.1 ahoka chip->nc_spare_size);
682 1.1 ahoka }
683 1.1 ahoka
684 1.1 ahoka nand_command(self, ONFI_PAGE_PROGRAM_START);
685 1.1 ahoka
686 1.1 ahoka nand_busy(self);
687 1.1 ahoka
688 1.2 ahoka /* for debugging ecc */
689 1.1 ahoka #if 0
690 1.1 ahoka printf("dumping ecc %d\n--------------\n", chip->nc_ecc->necc_steps);
691 1.1 ahoka for (e = 0; e < chip->nc_ecc->necc_steps; e++) {
692 1.1 ahoka printf("0x");
693 1.1 ahoka for (b = 0; b < cs; b++) {
694 1.1 ahoka printf("%.2hhx", ecc[e+b]);
695 1.1 ahoka }
696 1.1 ahoka printf("\n");
697 1.1 ahoka }
698 1.1 ahoka printf("--------------\n");
699 1.1 ahoka #endif
700 1.1 ahoka
701 1.1 ahoka status = nand_get_status(self);
702 1.1 ahoka KASSERT(status & ONFI_STATUS_RDY);
703 1.1 ahoka if (status & ONFI_STATUS_FAIL) {
704 1.1 ahoka aprint_error_dev(self, "page program failed!\n");
705 1.1 ahoka return EIO;
706 1.1 ahoka }
707 1.1 ahoka
708 1.1 ahoka return 0;
709 1.1 ahoka }
710 1.1 ahoka
711 1.1 ahoka int
712 1.1 ahoka nand_read_oob(device_t self, size_t page, void *oob)
713 1.1 ahoka {
714 1.1 ahoka struct nand_softc *sc = device_private(self);
715 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
716 1.1 ahoka
717 1.1 ahoka nand_prepare_read(self, page, chip->nc_page_size);
718 1.1 ahoka
719 1.1 ahoka if (chip->nc_flags & NC_BUSWIDTH_16)
720 1.1 ahoka nand_read_buf_word(self, oob, chip->nc_spare_size);
721 1.1 ahoka else
722 1.1 ahoka nand_read_buf_byte(self, oob, chip->nc_spare_size);
723 1.1 ahoka
724 1.2 ahoka /* for debugging drivers */
725 1.2 ahoka #if 0
726 1.2 ahoka nand_dump_data("oob", oob, chip->nc_spare_size);
727 1.2 ahoka #endif
728 1.1 ahoka
729 1.1 ahoka return 0;
730 1.1 ahoka }
731 1.1 ahoka
732 1.1 ahoka static int
733 1.1 ahoka nand_write_oob(device_t self, size_t offset, const void *oob)
734 1.1 ahoka {
735 1.1 ahoka struct nand_softc *sc = device_private(self);
736 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
737 1.1 ahoka uint8_t status;
738 1.1 ahoka
739 1.1 ahoka nand_command(self, ONFI_PAGE_PROGRAM);
740 1.1 ahoka nand_address_column(self, offset, chip->nc_page_size);
741 1.1 ahoka nand_command(self, ONFI_PAGE_PROGRAM_START);
742 1.1 ahoka
743 1.1 ahoka nand_busy(self);
744 1.1 ahoka
745 1.1 ahoka if (chip->nc_flags & NC_BUSWIDTH_16)
746 1.1 ahoka nand_write_buf_word(self, oob, chip->nc_spare_size);
747 1.1 ahoka else
748 1.1 ahoka nand_write_buf_byte(self, oob, chip->nc_spare_size);
749 1.1 ahoka
750 1.1 ahoka status = nand_get_status(self);
751 1.1 ahoka KASSERT(status & ONFI_STATUS_RDY);
752 1.1 ahoka if (status & ONFI_STATUS_FAIL)
753 1.1 ahoka return EIO;
754 1.1 ahoka else
755 1.1 ahoka return 0;
756 1.1 ahoka }
757 1.1 ahoka
758 1.1 ahoka void
759 1.1 ahoka nand_markbad(device_t self, size_t offset)
760 1.1 ahoka {
761 1.1 ahoka struct nand_softc *sc = device_private(self);
762 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
763 1.1 ahoka flash_addr_t blockoffset, marker;
764 1.1 ahoka #ifdef NAND_BBT
765 1.1 ahoka flash_addr_t block;
766 1.1 ahoka
767 1.1 ahoka block = offset / chip->nc_block_size;
768 1.1 ahoka
769 1.1 ahoka nand_bbt_block_markbad(self, block);
770 1.1 ahoka #endif
771 1.1 ahoka blockoffset = offset & chip->nc_block_mask;
772 1.1 ahoka marker = chip->nc_badmarker_offs & ~0x01;
773 1.1 ahoka
774 1.1 ahoka /* check if it is already marked bad */
775 1.1 ahoka if (nand_isbad(self, blockoffset))
776 1.1 ahoka return;
777 1.1 ahoka
778 1.1 ahoka nand_read_oob(self, blockoffset, chip->nc_oob_cache);
779 1.1 ahoka
780 1.1 ahoka chip->nc_oob_cache[chip->nc_badmarker_offs] = 0x00;
781 1.1 ahoka chip->nc_oob_cache[chip->nc_badmarker_offs + 1] = 0x00;
782 1.1 ahoka
783 1.1 ahoka nand_write_oob(self, blockoffset, chip->nc_oob_cache);
784 1.1 ahoka }
785 1.1 ahoka
786 1.1 ahoka bool
787 1.1 ahoka nand_isfactorybad(device_t self, flash_addr_t offset)
788 1.1 ahoka {
789 1.1 ahoka struct nand_softc *sc = device_private(self);
790 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
791 1.1 ahoka flash_addr_t block, first_page, last_page, page;
792 1.1 ahoka int i;
793 1.1 ahoka
794 1.1 ahoka /* Check for factory bad blocks first
795 1.1 ahoka * Factory bad blocks are marked in the first or last
796 1.1 ahoka * page of the blocks, see: ONFI 2.2, 3.2.2.
797 1.1 ahoka */
798 1.1 ahoka block = offset / chip->nc_block_size;
799 1.1 ahoka first_page = block * chip->nc_block_size;
800 1.1 ahoka last_page = (block + 1) * chip->nc_block_size
801 1.1 ahoka - chip->nc_page_size;
802 1.1 ahoka
803 1.1 ahoka for (i = 0, page = first_page; i < 2; i++, page = last_page) {
804 1.1 ahoka /* address OOB */
805 1.1 ahoka nand_prepare_read(self, page, chip->nc_page_size);
806 1.1 ahoka
807 1.1 ahoka if (chip->nc_flags & NC_BUSWIDTH_16) {
808 1.1 ahoka uint16_t word;
809 1.1 ahoka nand_read_word(self, &word);
810 1.1 ahoka if (word == 0x0000)
811 1.1 ahoka return true;
812 1.1 ahoka } else {
813 1.1 ahoka uint8_t byte;
814 1.1 ahoka nand_read_byte(self, &byte);
815 1.1 ahoka if (byte == 0x00)
816 1.1 ahoka return true;
817 1.1 ahoka }
818 1.1 ahoka }
819 1.1 ahoka
820 1.1 ahoka return false;
821 1.1 ahoka }
822 1.1 ahoka
823 1.1 ahoka bool
824 1.1 ahoka nand_iswornoutbad(device_t self, flash_addr_t offset)
825 1.1 ahoka {
826 1.1 ahoka struct nand_softc *sc = device_private(self);
827 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
828 1.1 ahoka flash_addr_t block;
829 1.1 ahoka
830 1.1 ahoka /* we inspect the first page of the block */
831 1.1 ahoka block = offset & chip->nc_block_mask;
832 1.1 ahoka
833 1.1 ahoka /* Linux/u-boot compatible badblock handling */
834 1.1 ahoka if (chip->nc_flags & NC_BUSWIDTH_16) {
835 1.1 ahoka uint16_t word, mark;
836 1.1 ahoka
837 1.1 ahoka nand_prepare_read(self, block,
838 1.1 ahoka chip->nc_page_size + (chip->nc_badmarker_offs & 0xfe));
839 1.1 ahoka
840 1.1 ahoka nand_read_word(self, &word);
841 1.1 ahoka mark = htole16(word);
842 1.1 ahoka if (chip->nc_badmarker_offs & 0x01)
843 1.1 ahoka mark >>= 8;
844 1.1 ahoka if ((mark & 0xff) != 0xff)
845 1.1 ahoka return true;
846 1.1 ahoka } else {
847 1.1 ahoka uint8_t byte;
848 1.1 ahoka
849 1.1 ahoka nand_prepare_read(self, block,
850 1.1 ahoka chip->nc_page_size + chip->nc_badmarker_offs);
851 1.1 ahoka
852 1.1 ahoka nand_read_byte(self, &byte);
853 1.1 ahoka if (byte != 0xff)
854 1.1 ahoka return true;
855 1.1 ahoka }
856 1.1 ahoka
857 1.1 ahoka return false;
858 1.1 ahoka }
859 1.1 ahoka
860 1.1 ahoka bool
861 1.1 ahoka nand_isbad(device_t self, flash_addr_t offset)
862 1.1 ahoka {
863 1.1 ahoka #ifdef NAND_BBT
864 1.1 ahoka struct nand_softc *sc = device_private(self);
865 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
866 1.1 ahoka flash_addr_t block;
867 1.1 ahoka
868 1.1 ahoka block = offset / chip->nc_block_size;
869 1.1 ahoka
870 1.1 ahoka return nand_bbt_block_isbad(self, block);
871 1.1 ahoka #else
872 1.1 ahoka /* ONFI host requirement */
873 1.1 ahoka if (nand_isfactorybad(self, offset))
874 1.1 ahoka return true;
875 1.1 ahoka
876 1.1 ahoka /* Look for Linux/U-Boot compatible bad marker */
877 1.1 ahoka if (nand_iswornoutbad(self, offset))
878 1.1 ahoka return true;
879 1.1 ahoka
880 1.1 ahoka return false;
881 1.1 ahoka #endif
882 1.1 ahoka }
883 1.1 ahoka
884 1.1 ahoka int
885 1.1 ahoka nand_erase_block(device_t self, size_t offset)
886 1.1 ahoka {
887 1.1 ahoka uint8_t status;
888 1.1 ahoka
889 1.1 ahoka /* xxx calculate first page of block for address? */
890 1.1 ahoka
891 1.1 ahoka nand_command(self, ONFI_BLOCK_ERASE);
892 1.1 ahoka nand_address_row(self, offset);
893 1.1 ahoka nand_command(self, ONFI_BLOCK_ERASE_START);
894 1.1 ahoka
895 1.1 ahoka nand_busy(self);
896 1.1 ahoka
897 1.1 ahoka status = nand_get_status(self);
898 1.1 ahoka KASSERT(status & ONFI_STATUS_RDY);
899 1.1 ahoka if (status & ONFI_STATUS_FAIL) {
900 1.1 ahoka aprint_error_dev(self, "block erase failed!\n");
901 1.1 ahoka nand_markbad(self, offset);
902 1.1 ahoka return EIO;
903 1.1 ahoka } else {
904 1.1 ahoka return 0;
905 1.1 ahoka }
906 1.1 ahoka }
907 1.1 ahoka
908 1.1 ahoka /* default functions for driver development */
909 1.1 ahoka
910 1.1 ahoka /* default ECC using hamming code of 256 byte chunks */
911 1.1 ahoka int
912 1.1 ahoka nand_default_ecc_compute(device_t self, const uint8_t *data, uint8_t *code)
913 1.1 ahoka {
914 1.1 ahoka hamming_compute_256(data, code);
915 1.1 ahoka
916 1.1 ahoka return 0;
917 1.1 ahoka }
918 1.1 ahoka
919 1.1 ahoka int
920 1.1 ahoka nand_default_ecc_correct(device_t self, uint8_t *data, const uint8_t *origcode,
921 1.1 ahoka const uint8_t *compcode)
922 1.1 ahoka {
923 1.1 ahoka return hamming_correct_256(data, origcode, compcode);
924 1.1 ahoka }
925 1.1 ahoka
926 1.1 ahoka void
927 1.1 ahoka nand_default_select(device_t self, bool enable)
928 1.1 ahoka {
929 1.1 ahoka /* do nothing */
930 1.1 ahoka return;
931 1.1 ahoka }
932 1.1 ahoka
933 1.1 ahoka /* implementation of the block device API */
934 1.1 ahoka
935 1.1 ahoka /*
936 1.1 ahoka * handle (page) unaligned write to nand
937 1.1 ahoka */
938 1.1 ahoka static int
939 1.1 ahoka nand_flash_write_unaligned(device_t self, off_t offset, size_t len,
940 1.1 ahoka size_t *retlen, const uint8_t *buf)
941 1.1 ahoka {
942 1.1 ahoka struct nand_softc *sc = device_private(self);
943 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
944 1.1 ahoka flash_addr_t first, last, firstoff;
945 1.1 ahoka const uint8_t *bufp;
946 1.1 ahoka flash_addr_t addr;
947 1.1 ahoka size_t left, count;
948 1.1 ahoka int error, i;
949 1.1 ahoka
950 1.1 ahoka first = offset & chip->nc_page_mask;
951 1.1 ahoka firstoff = offset & ~chip->nc_page_mask;
952 1.1 ahoka /* XXX check if this should be len - 1 */
953 1.1 ahoka last = (offset + len) & chip->nc_page_mask;
954 1.1 ahoka count = last - first + 1;
955 1.1 ahoka
956 1.1 ahoka addr = first;
957 1.1 ahoka *retlen = 0;
958 1.1 ahoka
959 1.1 ahoka if (count == 1) {
960 1.1 ahoka if (nand_isbad(self, addr)) {
961 1.1 ahoka aprint_error_dev(self,
962 1.1 ahoka "nand_flash_write_unaligned: "
963 1.1 ahoka "bad block encountered\n");
964 1.1 ahoka return EIO;
965 1.1 ahoka }
966 1.1 ahoka
967 1.1 ahoka error = nand_read_page(self, addr, chip->nc_page_cache);
968 1.1 ahoka if (error)
969 1.1 ahoka return error;
970 1.1 ahoka
971 1.1 ahoka memcpy(chip->nc_page_cache + firstoff, buf, len);
972 1.1 ahoka
973 1.1 ahoka error = nand_program_page(self, addr, chip->nc_page_cache);
974 1.1 ahoka if (error)
975 1.1 ahoka return error;
976 1.1 ahoka
977 1.1 ahoka *retlen = len;
978 1.1 ahoka return 0;
979 1.1 ahoka }
980 1.1 ahoka
981 1.1 ahoka bufp = buf;
982 1.1 ahoka left = len;
983 1.1 ahoka
984 1.1 ahoka for (i = 0; i < count && left != 0; i++) {
985 1.1 ahoka if (nand_isbad(self, addr)) {
986 1.1 ahoka aprint_error_dev(self,
987 1.1 ahoka "nand_flash_write_unaligned: "
988 1.1 ahoka "bad block encountered\n");
989 1.1 ahoka return EIO;
990 1.1 ahoka }
991 1.1 ahoka
992 1.1 ahoka if (i == 0) {
993 1.1 ahoka error = nand_read_page(self,
994 1.1 ahoka addr, chip->nc_page_cache);
995 1.1 ahoka if (error)
996 1.1 ahoka return error;
997 1.1 ahoka
998 1.1 ahoka memcpy(chip->nc_page_cache + firstoff,
999 1.1 ahoka bufp, chip->nc_page_size - firstoff);
1000 1.1 ahoka
1001 1.1 ahoka printf("program page: %s: %d\n", __FILE__, __LINE__);
1002 1.1 ahoka error = nand_program_page(self,
1003 1.1 ahoka addr, chip->nc_page_cache);
1004 1.1 ahoka if (error)
1005 1.1 ahoka return error;
1006 1.1 ahoka
1007 1.1 ahoka bufp += chip->nc_page_size - firstoff;
1008 1.1 ahoka left -= chip->nc_page_size - firstoff;
1009 1.1 ahoka *retlen += chip->nc_page_size - firstoff;
1010 1.1 ahoka
1011 1.1 ahoka } else if (i == count - 1) {
1012 1.1 ahoka error = nand_read_page(self,
1013 1.1 ahoka addr, chip->nc_page_cache);
1014 1.1 ahoka if (error)
1015 1.1 ahoka return error;
1016 1.1 ahoka
1017 1.1 ahoka memcpy(chip->nc_page_cache, bufp, left);
1018 1.1 ahoka
1019 1.1 ahoka error = nand_program_page(self,
1020 1.1 ahoka addr, chip->nc_page_cache);
1021 1.1 ahoka if (error)
1022 1.1 ahoka return error;
1023 1.1 ahoka
1024 1.1 ahoka *retlen += left;
1025 1.1 ahoka KASSERT(left < chip->nc_page_size);
1026 1.1 ahoka
1027 1.1 ahoka } else {
1028 1.1 ahoka /* XXX debug */
1029 1.1 ahoka if (left > chip->nc_page_size) {
1030 1.1 ahoka printf("left: %zu, i: %d, count: %zu\n",
1031 1.1 ahoka (size_t )left, i, count);
1032 1.1 ahoka }
1033 1.1 ahoka KASSERT(left > chip->nc_page_size);
1034 1.1 ahoka
1035 1.1 ahoka error = nand_program_page(self, addr, bufp);
1036 1.1 ahoka if (error)
1037 1.1 ahoka return error;
1038 1.1 ahoka
1039 1.1 ahoka bufp += chip->nc_page_size;
1040 1.1 ahoka left -= chip->nc_page_size;
1041 1.1 ahoka *retlen += chip->nc_page_size;
1042 1.1 ahoka }
1043 1.1 ahoka
1044 1.1 ahoka addr += chip->nc_page_size;
1045 1.1 ahoka }
1046 1.1 ahoka
1047 1.1 ahoka KASSERT(*retlen == len);
1048 1.1 ahoka
1049 1.1 ahoka return 0;
1050 1.1 ahoka }
1051 1.1 ahoka
1052 1.1 ahoka int
1053 1.1 ahoka nand_flash_write(device_t self, off_t offset, size_t len, size_t *retlen,
1054 1.1 ahoka const uint8_t *buf)
1055 1.1 ahoka {
1056 1.1 ahoka struct nand_softc *sc = device_private(self);
1057 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
1058 1.1 ahoka const uint8_t *bufp;
1059 1.1 ahoka size_t pages, page;
1060 1.1 ahoka daddr_t addr;
1061 1.1 ahoka int error = 0;
1062 1.1 ahoka
1063 1.1 ahoka if ((offset + len) > chip->nc_size) {
1064 1.1 ahoka DPRINTF(("nand_flash_write: write (off: 0x%jx, len: %ju),"
1065 1.1 ahoka " is over device size (0x%jx)\n",
1066 1.1 ahoka (uintmax_t)offset, (uintmax_t)len,
1067 1.1 ahoka (uintmax_t)chip->nc_size));
1068 1.1 ahoka return EINVAL;
1069 1.1 ahoka }
1070 1.1 ahoka
1071 1.1 ahoka if (len % chip->nc_page_size != 0 ||
1072 1.1 ahoka offset % chip->nc_page_size != 0) {
1073 1.1 ahoka return nand_flash_write_unaligned(self,
1074 1.1 ahoka offset, len, retlen, buf);
1075 1.1 ahoka }
1076 1.1 ahoka
1077 1.1 ahoka pages = len / chip->nc_page_size;
1078 1.1 ahoka KASSERT(pages != 0);
1079 1.1 ahoka *retlen = 0;
1080 1.1 ahoka
1081 1.1 ahoka addr = offset;
1082 1.1 ahoka bufp = buf;
1083 1.1 ahoka
1084 1.1 ahoka mutex_enter(&sc->sc_device_lock);
1085 1.1 ahoka for (page = 0; page < pages; page++) {
1086 1.1 ahoka /* do we need this check here? */
1087 1.1 ahoka if (nand_isbad(self, addr)) {
1088 1.1 ahoka aprint_error_dev(self,
1089 1.1 ahoka "nand_flash_write: bad block encountered\n");
1090 1.1 ahoka
1091 1.1 ahoka error = EIO;
1092 1.1 ahoka goto out;
1093 1.1 ahoka }
1094 1.1 ahoka
1095 1.1 ahoka error = nand_program_page(self, addr, bufp);
1096 1.1 ahoka if (error)
1097 1.1 ahoka goto out;
1098 1.1 ahoka
1099 1.1 ahoka addr += chip->nc_page_size;
1100 1.1 ahoka bufp += chip->nc_page_size;
1101 1.1 ahoka *retlen += chip->nc_page_size;
1102 1.1 ahoka }
1103 1.1 ahoka out:
1104 1.1 ahoka mutex_exit(&sc->sc_device_lock);
1105 1.1 ahoka DPRINTF(("page programming: retlen: %zu, len: %zu\n", *retlen, len));
1106 1.1 ahoka
1107 1.1 ahoka return error;
1108 1.1 ahoka }
1109 1.1 ahoka
1110 1.1 ahoka /*
1111 1.1 ahoka * handle (page) unaligned read from nand
1112 1.1 ahoka */
1113 1.1 ahoka static int
1114 1.1 ahoka nand_flash_read_unaligned(device_t self, size_t offset,
1115 1.1 ahoka size_t len, size_t *retlen, uint8_t *buf)
1116 1.1 ahoka {
1117 1.1 ahoka struct nand_softc *sc = device_private(self);
1118 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
1119 1.1 ahoka daddr_t first, last, count, firstoff;
1120 1.1 ahoka uint8_t *bufp;
1121 1.1 ahoka daddr_t addr;
1122 1.1 ahoka size_t left;
1123 1.1 ahoka int error = 0, i;
1124 1.1 ahoka
1125 1.1 ahoka first = offset & chip->nc_page_mask;
1126 1.1 ahoka firstoff = offset & ~chip->nc_page_mask;
1127 1.1 ahoka last = (offset + len) & chip->nc_page_mask;
1128 1.1 ahoka count = (last - first) / chip->nc_page_size + 1;
1129 1.1 ahoka
1130 1.1 ahoka addr = first;
1131 1.1 ahoka bufp = buf;
1132 1.1 ahoka left = len;
1133 1.1 ahoka *retlen = 0;
1134 1.1 ahoka
1135 1.1 ahoka mutex_enter(&sc->sc_device_lock);
1136 1.1 ahoka if (count == 1) {
1137 1.1 ahoka error = nand_read_page(self, addr, chip->nc_page_cache);
1138 1.1 ahoka if (error)
1139 1.1 ahoka goto out;
1140 1.1 ahoka
1141 1.1 ahoka memcpy(bufp, chip->nc_page_cache + firstoff, len);
1142 1.1 ahoka
1143 1.1 ahoka *retlen = len;
1144 1.1 ahoka goto out;
1145 1.1 ahoka }
1146 1.1 ahoka
1147 1.1 ahoka for (i = 0; i < count && left != 0; i++) {
1148 1.1 ahoka error = nand_read_page(self, addr, chip->nc_page_cache);
1149 1.1 ahoka if (error)
1150 1.1 ahoka goto out;
1151 1.1 ahoka
1152 1.1 ahoka if (i == 0) {
1153 1.1 ahoka memcpy(bufp, chip->nc_page_cache + firstoff,
1154 1.1 ahoka chip->nc_page_size - firstoff);
1155 1.1 ahoka
1156 1.1 ahoka bufp += chip->nc_page_size - firstoff;
1157 1.1 ahoka left -= chip->nc_page_size - firstoff;
1158 1.1 ahoka *retlen += chip->nc_page_size - firstoff;
1159 1.1 ahoka
1160 1.1 ahoka } else if (i == count - 1) {
1161 1.1 ahoka memcpy(bufp, chip->nc_page_cache, left);
1162 1.1 ahoka *retlen += left;
1163 1.1 ahoka KASSERT(left < chip->nc_page_size);
1164 1.1 ahoka
1165 1.1 ahoka } else {
1166 1.1 ahoka memcpy(bufp, chip->nc_page_cache, chip->nc_page_size);
1167 1.1 ahoka
1168 1.1 ahoka bufp += chip->nc_page_size;
1169 1.1 ahoka left -= chip->nc_page_size;
1170 1.1 ahoka *retlen += chip->nc_page_size;
1171 1.1 ahoka }
1172 1.1 ahoka
1173 1.1 ahoka addr += chip->nc_page_size;
1174 1.1 ahoka }
1175 1.1 ahoka
1176 1.1 ahoka KASSERT(*retlen == len);
1177 1.1 ahoka
1178 1.1 ahoka out:
1179 1.1 ahoka mutex_exit(&sc->sc_device_lock);
1180 1.1 ahoka
1181 1.1 ahoka return error;
1182 1.1 ahoka }
1183 1.1 ahoka
1184 1.1 ahoka int
1185 1.1 ahoka nand_flash_read(device_t self, off_t offset, size_t len, size_t *retlen,
1186 1.1 ahoka uint8_t *buf)
1187 1.1 ahoka {
1188 1.1 ahoka struct nand_softc *sc = device_private(self);
1189 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
1190 1.1 ahoka uint8_t *bufp;
1191 1.1 ahoka size_t addr;
1192 1.1 ahoka size_t i, pages;
1193 1.1 ahoka int error = 0;
1194 1.1 ahoka
1195 1.1 ahoka *retlen = 0;
1196 1.1 ahoka
1197 1.1 ahoka DPRINTF(("nand_flash_read: off: 0x%jx, len: %zu\n",
1198 1.1 ahoka (uintmax_t)offset, len));
1199 1.1 ahoka
1200 1.1 ahoka if (__predict_false((offset + len) > chip->nc_size)) {
1201 1.1 ahoka DPRINTF(("nand_flash_read: read (off: 0x%jx, len: %zu),"
1202 1.1 ahoka " is over device size (%ju)\n", (uintmax_t)offset,
1203 1.1 ahoka len, (uintmax_t)chip->nc_size));
1204 1.1 ahoka return EINVAL;
1205 1.1 ahoka }
1206 1.1 ahoka
1207 1.1 ahoka /* Handle unaligned access, shouldnt be needed when using the
1208 1.1 ahoka * block device, as strategy handles it, so only low level
1209 1.1 ahoka * accesses will use this path
1210 1.1 ahoka */
1211 1.2 ahoka /* XXX^2 */
1212 1.2 ahoka #if 0
1213 1.2 ahoka if (len < chip->nc_page_size)
1214 1.2 ahoka panic("TODO page size is larger than read size");
1215 1.2 ahoka #endif
1216 1.2 ahoka
1217 1.1 ahoka
1218 1.1 ahoka if (len % chip->nc_page_size != 0 ||
1219 1.1 ahoka offset % chip->nc_page_size != 0) {
1220 1.1 ahoka return nand_flash_read_unaligned(self,
1221 1.1 ahoka offset, len, retlen, buf);
1222 1.1 ahoka }
1223 1.1 ahoka
1224 1.1 ahoka bufp = buf;
1225 1.1 ahoka addr = offset;
1226 1.1 ahoka pages = len / chip->nc_page_size;
1227 1.1 ahoka
1228 1.1 ahoka mutex_enter(&sc->sc_device_lock);
1229 1.1 ahoka for (i = 0; i < pages; i++) {
1230 1.2 ahoka /* XXX do we need this check here? */
1231 1.1 ahoka if (nand_isbad(self, addr)) {
1232 1.1 ahoka aprint_error_dev(self, "bad block encountered\n");
1233 1.1 ahoka error = EIO;
1234 1.1 ahoka goto out;
1235 1.1 ahoka }
1236 1.1 ahoka error = nand_read_page(self, addr, bufp);
1237 1.1 ahoka if (error)
1238 1.1 ahoka goto out;
1239 1.1 ahoka
1240 1.1 ahoka bufp += chip->nc_page_size;
1241 1.1 ahoka addr += chip->nc_page_size;
1242 1.1 ahoka *retlen += chip->nc_page_size;
1243 1.1 ahoka }
1244 1.1 ahoka
1245 1.1 ahoka out:
1246 1.1 ahoka mutex_exit(&sc->sc_device_lock);
1247 1.1 ahoka
1248 1.1 ahoka return error;
1249 1.1 ahoka }
1250 1.1 ahoka
1251 1.1 ahoka int
1252 1.1 ahoka nand_flash_isbad(device_t self, uint64_t ofs)
1253 1.1 ahoka {
1254 1.1 ahoka struct nand_softc *sc = device_private(self);
1255 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
1256 1.1 ahoka bool result;
1257 1.1 ahoka
1258 1.1 ahoka if (ofs > chip->nc_size) {
1259 1.1 ahoka DPRINTF(("nand_flash_isbad: offset 0x%jx is larger than"
1260 1.1 ahoka " device size (0x%jx)\n", (uintmax_t)ofs,
1261 1.1 ahoka (uintmax_t)chip->nc_size));
1262 1.1 ahoka return EINVAL;
1263 1.1 ahoka }
1264 1.1 ahoka
1265 1.1 ahoka if (ofs % chip->nc_block_size != 0) {
1266 1.1 ahoka panic("offset (0x%jx) is not the multiple of block size (%ju)",
1267 1.1 ahoka (uintmax_t)ofs, (uintmax_t)chip->nc_block_size);
1268 1.1 ahoka }
1269 1.1 ahoka
1270 1.1 ahoka mutex_enter(&sc->sc_device_lock);
1271 1.1 ahoka result = nand_isbad(self, ofs);
1272 1.1 ahoka mutex_exit(&sc->sc_device_lock);
1273 1.1 ahoka
1274 1.1 ahoka if (result)
1275 1.1 ahoka return 1;
1276 1.1 ahoka else
1277 1.1 ahoka return 0;
1278 1.1 ahoka }
1279 1.1 ahoka
1280 1.1 ahoka int
1281 1.1 ahoka nand_flash_markbad(device_t self, uint64_t ofs)
1282 1.1 ahoka {
1283 1.1 ahoka struct nand_softc *sc = device_private(self);
1284 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
1285 1.1 ahoka
1286 1.1 ahoka if (ofs > chip->nc_size) {
1287 1.1 ahoka DPRINTF(("nand_flash_markbad: offset 0x%jx is larger than"
1288 1.1 ahoka " device size (0x%jx)\n", ofs,
1289 1.1 ahoka (uintmax_t)chip->nc_size));
1290 1.1 ahoka return EINVAL;
1291 1.1 ahoka }
1292 1.1 ahoka
1293 1.1 ahoka if (ofs % chip->nc_block_size != 0) {
1294 1.1 ahoka panic("offset (%ju) is not the multiple of block size (%ju)",
1295 1.1 ahoka (uintmax_t)ofs, (uintmax_t)chip->nc_block_size);
1296 1.1 ahoka }
1297 1.1 ahoka
1298 1.1 ahoka mutex_enter(&sc->sc_device_lock);
1299 1.1 ahoka nand_markbad(self, ofs);
1300 1.1 ahoka mutex_exit(&sc->sc_device_lock);
1301 1.1 ahoka
1302 1.1 ahoka return 0;
1303 1.1 ahoka }
1304 1.1 ahoka
1305 1.1 ahoka int
1306 1.1 ahoka nand_flash_erase(device_t self,
1307 1.1 ahoka struct flash_erase_instruction *ei)
1308 1.1 ahoka {
1309 1.1 ahoka struct nand_softc *sc = device_private(self);
1310 1.1 ahoka struct nand_chip *chip = &sc->sc_chip;
1311 1.1 ahoka flash_addr_t addr;
1312 1.1 ahoka int error;
1313 1.1 ahoka
1314 1.1 ahoka if (ei->ei_addr < 0 || ei->ei_len < chip->nc_block_size)
1315 1.1 ahoka return EINVAL;
1316 1.1 ahoka
1317 1.1 ahoka if (ei->ei_addr + ei->ei_len > chip->nc_size) {
1318 1.1 ahoka DPRINTF(("nand_flash_erase: erase address is over the end"
1319 1.1 ahoka " of the device\n"));
1320 1.1 ahoka return EINVAL;
1321 1.1 ahoka }
1322 1.1 ahoka
1323 1.1 ahoka if (ei->ei_addr % chip->nc_block_size != 0) {
1324 1.1 ahoka aprint_error_dev(self,
1325 1.1 ahoka "nand_flash_erase: ei_addr (%ju) is not"
1326 1.1 ahoka "the multiple of block size (%ju)",
1327 1.1 ahoka (uintmax_t)ei->ei_addr,
1328 1.1 ahoka (uintmax_t)chip->nc_block_size);
1329 1.1 ahoka return EINVAL;
1330 1.1 ahoka }
1331 1.1 ahoka
1332 1.1 ahoka if (ei->ei_len % chip->nc_block_size != 0) {
1333 1.1 ahoka aprint_error_dev(self,
1334 1.1 ahoka "nand_flash_erase: ei_len (%ju) is not"
1335 1.1 ahoka "the multiple of block size (%ju)",
1336 1.1 ahoka (uintmax_t)ei->ei_addr,
1337 1.1 ahoka (uintmax_t)chip->nc_block_size);
1338 1.1 ahoka return EINVAL;
1339 1.1 ahoka }
1340 1.1 ahoka
1341 1.1 ahoka mutex_enter(&sc->sc_device_lock);
1342 1.1 ahoka addr = ei->ei_addr;
1343 1.1 ahoka while (addr < ei->ei_addr + ei->ei_len) {
1344 1.1 ahoka if (nand_isbad(self, addr)) {
1345 1.1 ahoka mutex_exit(&sc->sc_device_lock);
1346 1.1 ahoka aprint_error_dev(self, "bad block encountered\n");
1347 1.1 ahoka ei->ei_state = FLASH_ERASE_FAILED;
1348 1.1 ahoka return EIO;
1349 1.1 ahoka }
1350 1.1 ahoka
1351 1.1 ahoka error = nand_erase_block(self, addr);
1352 1.1 ahoka if (error) {
1353 1.1 ahoka mutex_exit(&sc->sc_device_lock);
1354 1.1 ahoka ei->ei_state = FLASH_ERASE_FAILED;
1355 1.1 ahoka return error;
1356 1.1 ahoka }
1357 1.1 ahoka
1358 1.1 ahoka addr += chip->nc_block_size;
1359 1.1 ahoka }
1360 1.1 ahoka mutex_exit(&sc->sc_device_lock);
1361 1.1 ahoka
1362 1.1 ahoka ei->ei_state = FLASH_ERASE_DONE;
1363 1.1 ahoka if (ei->ei_callback != NULL)
1364 1.1 ahoka ei->ei_callback(ei);
1365 1.1 ahoka
1366 1.1 ahoka return 0;
1367 1.1 ahoka }
1368 1.1 ahoka
1369 1.1 ahoka static int
1370 1.1 ahoka sysctl_nand_verify(SYSCTLFN_ARGS)
1371 1.1 ahoka {
1372 1.1 ahoka int error, t;
1373 1.1 ahoka struct sysctlnode node;
1374 1.1 ahoka
1375 1.1 ahoka node = *rnode;
1376 1.1 ahoka t = *(int *)rnode->sysctl_data;
1377 1.1 ahoka node.sysctl_data = &t;
1378 1.1 ahoka error = sysctl_lookup(SYSCTLFN_CALL(&node));
1379 1.1 ahoka if (error || newp == NULL)
1380 1.1 ahoka return error;
1381 1.1 ahoka
1382 1.1 ahoka if (node.sysctl_num == nand_cachesync_nodenum) {
1383 1.1 ahoka if (t <= 0 || t > 60)
1384 1.1 ahoka return EINVAL;
1385 1.1 ahoka } else {
1386 1.1 ahoka return EINVAL;
1387 1.1 ahoka }
1388 1.1 ahoka
1389 1.1 ahoka *(int *)rnode->sysctl_data = t;
1390 1.1 ahoka
1391 1.1 ahoka return 0;
1392 1.1 ahoka }
1393 1.1 ahoka
1394 1.1 ahoka SYSCTL_SETUP(sysctl_nand, "sysctl nand subtree setup")
1395 1.1 ahoka {
1396 1.1 ahoka int rc, nand_root_num;
1397 1.1 ahoka const struct sysctlnode *node;
1398 1.1 ahoka
1399 1.1 ahoka if ((rc = sysctl_createv(clog, 0, NULL, NULL,
1400 1.1 ahoka CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
1401 1.1 ahoka NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
1402 1.1 ahoka goto error;
1403 1.1 ahoka }
1404 1.1 ahoka
1405 1.1 ahoka if ((rc = sysctl_createv(clog, 0, NULL, &node,
1406 1.1 ahoka CTLFLAG_PERMANENT, CTLTYPE_NODE, "nand",
1407 1.1 ahoka SYSCTL_DESCR("NAND driver controls"),
1408 1.1 ahoka NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
1409 1.1 ahoka goto error;
1410 1.1 ahoka }
1411 1.1 ahoka
1412 1.1 ahoka nand_root_num = node->sysctl_num;
1413 1.1 ahoka
1414 1.1 ahoka if ((rc = sysctl_createv(clog, 0, NULL, &node,
1415 1.1 ahoka CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1416 1.1 ahoka CTLTYPE_INT, "cache_sync_timeout",
1417 1.1 ahoka SYSCTL_DESCR("NAND write cache sync timeout in seconds"),
1418 1.1 ahoka sysctl_nand_verify, 0, &nand_cachesync_timeout,
1419 1.1 ahoka 0, CTL_HW, nand_root_num, CTL_CREATE,
1420 1.1 ahoka CTL_EOL)) != 0) {
1421 1.1 ahoka goto error;
1422 1.1 ahoka }
1423 1.1 ahoka
1424 1.1 ahoka nand_cachesync_nodenum = node->sysctl_num;
1425 1.1 ahoka
1426 1.1 ahoka return;
1427 1.1 ahoka
1428 1.1 ahoka error:
1429 1.1 ahoka aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
1430 1.1 ahoka }
1431 1.1 ahoka
1432 1.1 ahoka MODULE(MODULE_CLASS_DRIVER, nand, "flash");
1433 1.1 ahoka
1434 1.1 ahoka #ifdef _MODULE
1435 1.1 ahoka #include "ioconf.c"
1436 1.1 ahoka #endif
1437 1.1 ahoka
1438 1.1 ahoka static int
1439 1.1 ahoka nand_modcmd(modcmd_t cmd, void *opaque)
1440 1.1 ahoka {
1441 1.1 ahoka switch (cmd) {
1442 1.1 ahoka case MODULE_CMD_INIT:
1443 1.1 ahoka #ifdef _MODULE
1444 1.1 ahoka return config_init_component(cfdriver_ioconf_nand,
1445 1.1 ahoka cfattach_ioconf_nand, cfdata_ioconf_nand);
1446 1.1 ahoka #else
1447 1.1 ahoka return 0;
1448 1.1 ahoka #endif
1449 1.1 ahoka case MODULE_CMD_FINI:
1450 1.1 ahoka #ifdef _MODULE
1451 1.1 ahoka return config_fini_component(cfdriver_ioconf_nand,
1452 1.1 ahoka cfattach_ioconf_nand, cfdata_ioconf_nand);
1453 1.1 ahoka #else
1454 1.1 ahoka return 0;
1455 1.1 ahoka #endif
1456 1.1 ahoka default:
1457 1.1 ahoka return ENOTTY;
1458 1.1 ahoka }
1459 1.1 ahoka }
1460