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