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