nand.h revision 1.6 1 /* $NetBSD: nand.h,v 1.6 2011/04/10 12:48:09 ahoka Exp $ */
2
3 /*-
4 * Copyright (c) 2010 Department of Software Engineering,
5 * University of Szeged, Hungary
6 * Copyright (c) 2010 Adam Hoka <ahoka (at) NetBSD.org>
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by the Department of Software Engineering, University of Szeged, Hungary
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef _NAND_H_
35 #define _NAND_H_
36
37 #include <sys/param.h>
38 #include <sys/cdefs.h>
39
40 #include <sys/bufq.h>
41 #include <sys/buf.h>
42 #include <sys/time.h>
43
44 #include <dev/nand/onfi.h>
45 #include <dev/flash/flash.h>
46
47 #ifdef NAND_DEBUG
48 #define DPRINTF(x) if (nanddebug) printf x
49 #define DPRINTFN(n,x) if (nanddebug>(n)) printf x
50 #else
51 #define DPRINTF(x)
52 #define DPRINTFN(n,x)
53 #endif
54
55 //#define NAND_VERBOSE
56
57 /* same as in linux for compatibility */
58 enum {
59 NAND_BAD_MARKER_OFFSET = 0,
60 NAND_BAD_MARKER_OFFSET_SMALL = 5
61 };
62
63 /* feature flags use in nc_flags */
64 enum {
65 NC_BUSWIDTH_16 = (1<<0),
66 NC_SOURCE_SYNC = (1<<2),
67 NC_INTERLEAVED_PE = (1<<1),
68 NC_INTERLEAVED_R = (1<<3),
69 NC_EXTENDED_PARAM = (1<<4)
70 };
71
72 /* various quirks used in nc_quirks */
73 enum {
74 NC_QUIRK_NO_READ_START = (1<<0)
75 };
76
77 enum {
78 NAND_ECC_READ,
79 NAND_ECC_WRITE
80 };
81
82 enum {
83 NAND_ECC_OK,
84 NAND_ECC_CORRECTED,
85 NAND_ECC_INVALID,
86 NAND_ECC_TWOBIT
87 };
88
89 enum {
90 NAND_ECC_TYPE_HW,
91 NAND_ECC_TYPE_SW
92 };
93
94 struct nand_bbt {
95 uint8_t *nbbt_bitmap;
96 size_t nbbt_size;
97 };
98
99 struct nand_ecc {
100 size_t necc_offset; /* offset of ecc data in oob */
101 size_t necc_size; /* size of ecc data in oob */
102 size_t necc_block_size; /* block size used in ecc calc */
103 size_t necc_code_size; /* reduntant bytes per block */
104 int necc_steps; /* pagesize / code size */
105 int necc_type; /* type of the ecc engine */
106 };
107
108 /**
109 * nand_chip: structure containing the required information
110 * about the NAND chip.
111 */
112 struct nand_chip {
113 struct nand_ecc *nc_ecc; /* ecc information */
114 uint8_t *nc_oob_cache; /* buffer for oob cache */
115 uint8_t *nc_page_cache; /* buffer for page cache */
116 uint8_t *nc_ecc_cache;
117 size_t nc_size; /* storage size in bytes */
118 size_t nc_page_size; /* page size in bytes */
119 size_t nc_block_pages; /* block size in pages */
120 size_t nc_block_size; /* block size in bytes */
121 size_t nc_spare_size; /* spare (oob) size in bytes */
122 uint32_t nc_lun_blocks; /* LUN size in blocks */
123 uint32_t nc_flags; /* bitfield flags */
124 uint32_t nc_quirks; /* bitfield quirks */
125 unsigned int nc_page_shift; /* page shift for page alignment */
126 unsigned int nc_page_mask; /* page mask for page alignment */
127 unsigned int nc_block_shift; /* write shift */
128 unsigned int nc_block_mask; /* write mask */
129 uint8_t nc_num_luns; /* number of LUNs */
130 uint8_t nc_manf_id; /* manufacturer id */
131 uint8_t nc_dev_id; /* device id */
132 uint8_t nc_addr_cycles_row; /* row cycles for addressing */
133 uint8_t nc_addr_cycles_column; /* column cycles for addressing */
134 uint8_t nc_badmarker_offs; /* offset for marking bad blocks */
135 bool nc_isonfi; /* if the device is onfi compliant */
136 };
137
138 struct nand_write_cache {
139 struct bintime nwc_creation;
140 struct bintime nwc_last_write;
141 struct bufq_state *nwc_bufq;
142 uint8_t *nwc_data;
143 daddr_t nwc_block;
144 kmutex_t nwc_lock;
145 bool nwc_write_pending;
146 };
147
148 /* driver softc for nand */
149 struct nand_softc {
150 device_t sc_dev;
151 device_t controller_dev;
152 struct nand_interface *nand_if;
153 void *nand_softc;
154 struct nand_chip sc_chip;
155 struct nand_bbt sc_bbt;
156 size_t sc_part_offset;
157 size_t sc_part_size;
158 kmutex_t sc_device_lock; /* serialize access to chip */
159
160 /* for the i/o thread */
161 struct lwp *sc_sync_thread;
162 struct nand_write_cache sc_cache;
163 kmutex_t sc_io_lock;
164 kmutex_t sc_waitq_lock;
165 kcondvar_t sc_io_cv;
166 bool sc_io_running;
167 };
168
169 /* structure holding the nand api */
170 struct nand_interface
171 {
172 /* basic nand controller commands */
173 void (*select) (device_t, bool); /* optional */
174 void (*command) (device_t, uint8_t);
175 void (*address) (device_t, uint8_t);
176 void (*read_buf_byte) (device_t, void *, size_t);
177 void (*read_buf_word) (device_t, void *, size_t);
178 void (*read_byte) (device_t, uint8_t *);
179 void (*read_word) (device_t, uint16_t *);
180 void (*write_buf_byte) (device_t, const void *, size_t);
181 void (*write_buf_word) (device_t, const void *, size_t);
182 void (*write_byte) (device_t, uint8_t);
183 void (*write_word) (device_t, uint16_t);
184 void (*busy) (device_t);
185
186 /* "smart" controllers may override read/program functions */
187 int (*read_page) (device_t, size_t, uint8_t *); /* optional */
188 int (*program_page) (device_t, size_t, const uint8_t *); /* optional */
189
190 /* functions specific to ecc computation */
191 int (*ecc_prepare)(device_t, int); /* optional */
192 int (*ecc_compute)(device_t, const uint8_t *, uint8_t *);
193 int (*ecc_correct)(device_t, uint8_t *, const uint8_t *,
194 const uint8_t *);
195
196 /* information for the ecc engine */
197 struct nand_ecc ecc;
198
199 /* flash partition information */
200 const struct flash_partition *part_info;
201 int part_num;
202 };
203
204 /* attach args */
205 struct nand_attach_args {
206 struct nand_interface *naa_nand_if;
207 };
208
209 static inline void
210 nand_busy(device_t device)
211 {
212 struct nand_softc *sc = device_private(device);
213
214 KASSERT(sc->nand_if->select != NULL);
215 KASSERT(sc->controller_dev != NULL);
216
217 sc->nand_if->select(sc->controller_dev, true);
218
219 if (sc->nand_if->busy != NULL) {
220 sc->nand_if->busy(sc->controller_dev);
221 }
222
223 sc->nand_if->select(sc->controller_dev, false);
224 }
225
226 static inline void
227 nand_select(device_t self, bool enable)
228 {
229 struct nand_softc *sc = device_private(self);
230
231 KASSERT(sc->nand_if->select != NULL);
232 KASSERT(sc->controller_dev != NULL);
233
234 sc->nand_if->select(sc->controller_dev, enable);
235 }
236
237 static inline void
238 nand_address(device_t self, uint32_t address)
239 {
240 struct nand_softc *sc = device_private(self);
241
242 KASSERT(sc->nand_if->address != NULL);
243 KASSERT(sc->controller_dev != NULL);
244
245 sc->nand_if->address(sc->controller_dev, address);
246 }
247
248 static inline void
249 nand_command(device_t self, uint8_t command)
250 {
251 struct nand_softc *sc = device_private(self);
252
253 KASSERT(sc->nand_if->command != NULL);
254 KASSERT(sc->controller_dev != NULL);
255
256 sc->nand_if->command(sc->controller_dev, command);
257 }
258
259 static inline void
260 nand_read_byte(device_t self, uint8_t *data)
261 {
262 struct nand_softc *sc = device_private(self);
263
264 KASSERT(sc->nand_if->read_byte != NULL);
265 KASSERT(sc->controller_dev != NULL);
266
267 sc->nand_if->read_byte(sc->controller_dev, data);
268 }
269
270 static inline void
271 nand_write_byte(device_t self, uint8_t data)
272 {
273 struct nand_softc *sc = device_private(self);
274
275 KASSERT(sc->nand_if->write_byte != NULL);
276 KASSERT(sc->controller_dev != NULL);
277
278 sc->nand_if->write_byte(sc->controller_dev, data);
279 }
280
281 static inline void
282 nand_read_word(device_t self, uint16_t *data)
283 {
284 struct nand_softc *sc = device_private(self);
285
286 KASSERT(sc->nand_if->read_word != NULL);
287 KASSERT(sc->controller_dev != NULL);
288
289 sc->nand_if->read_word(sc->controller_dev, data);
290 }
291
292 static inline void
293 nand_write_word(device_t self, uint16_t data)
294 {
295 struct nand_softc *sc = device_private(self);
296
297 KASSERT(sc->nand_if->write_word != NULL);
298 KASSERT(sc->controller_dev != NULL);
299
300 sc->nand_if->write_word(sc->controller_dev, data);
301 }
302
303 static inline void
304 nand_read_buf_byte(device_t self, void *buf, size_t size)
305 {
306 struct nand_softc *sc = device_private(self);
307
308 KASSERT(sc->nand_if->read_buf_byte != NULL);
309 KASSERT(sc->controller_dev != NULL);
310
311 sc->nand_if->read_buf_byte(sc->controller_dev, buf, size);
312 }
313
314 static inline void
315 nand_read_buf_word(device_t self, void *buf, size_t size)
316 {
317 struct nand_softc *sc = device_private(self);
318
319 KASSERT(sc->nand_if->read_buf_word != NULL);
320 KASSERT(sc->controller_dev != NULL);
321
322 sc->nand_if->read_buf_word(sc->controller_dev, buf, size);
323 }
324
325 static inline void
326 nand_write_buf_byte(device_t self, const void *buf, size_t size)
327 {
328 struct nand_softc *sc = device_private(self);
329
330 KASSERT(sc->nand_if->write_buf_byte != NULL);
331 KASSERT(sc->controller_dev != NULL);
332
333 sc->nand_if->write_buf_byte(sc->controller_dev, buf, size);
334 }
335
336 static inline void
337 nand_write_buf_word(device_t self, const void *buf, size_t size)
338 {
339 struct nand_softc *sc = device_private(self);
340
341 KASSERT(sc->nand_if->write_buf_word != NULL);
342 KASSERT(sc->controller_dev != NULL);
343
344 sc->nand_if->write_buf_word(sc->controller_dev, buf, size);
345 }
346
347 static inline int
348 nand_ecc_correct(device_t self, uint8_t *data, const uint8_t *oldcode,
349 const uint8_t *newcode)
350 {
351 struct nand_softc *sc = device_private(self);
352
353 KASSERT(sc->nand_if->ecc_correct != NULL);
354 KASSERT(sc->controller_dev != NULL);
355
356 return sc->nand_if->ecc_correct(sc->controller_dev, data, oldcode, newcode);
357 }
358
359 static inline void
360 nand_ecc_compute(device_t self, const uint8_t *data, uint8_t *code)
361 {
362 struct nand_softc *sc = device_private(self);
363
364 KASSERT(sc->nand_if->ecc_compute != NULL);
365 KASSERT(sc->controller_dev != NULL);
366
367 sc->nand_if->ecc_compute(sc->controller_dev, data, code);
368 }
369
370 static inline void
371 nand_ecc_prepare(device_t self, int mode)
372 {
373 struct nand_softc *sc = device_private(self);
374
375 KASSERT(sc->controller_dev != NULL);
376
377 if (sc->nand_if->ecc_prepare != NULL)
378 sc->nand_if->ecc_prepare(sc->controller_dev, mode);
379 }
380
381 static inline int
382 nand_program_page(device_t self, size_t offset, const uint8_t *data)
383 {
384 struct nand_softc *sc = device_private(self);
385
386 KASSERT(sc->nand_if->program_page != NULL);
387
388 return sc->nand_if->program_page(self, offset, data);
389 }
390
391 static inline int
392 nand_read_page(device_t self, size_t offset, uint8_t *data)
393 {
394 struct nand_softc *sc = device_private(self);
395
396 KASSERT(sc->nand_if->read_page != NULL);
397
398 return sc->nand_if->read_page(self, offset, data);
399 }
400
401 #if 0
402 static inline bool
403 nand_block_isbad(device_t self, flash_off_t block)
404 {
405 struct nand_softc *sc = device_private(self);
406
407 KASSERT(sc->nand_if->block_isbad != NULL);
408 KASSERT(sc->controller_dev != NULL);
409
410 return sc->nand_if->block_isbad(sc->controller_dev, block);
411 }
412 #endif
413
414 /* Manufacturer IDs defined by JEDEC */
415 enum {
416 NAND_MFR_UNKNOWN = 0x00,
417 NAND_MFR_AMD = 0x01,
418 NAND_MFR_FUJITSU = 0x04,
419 NAND_MFR_RENESAS = 0x07,
420 NAND_MFR_STMICRO = 0x20,
421 NAND_MFR_MICRON = 0x2c,
422 NAND_MFR_NATIONAL = 0x8f,
423 NAND_MFR_TOSHIBA = 0x98,
424 NAND_MFR_HYNIX = 0xad,
425 NAND_MFR_SAMSUNG = 0xec
426 };
427
428 struct nand_manufacturer {
429 int id;
430 const char *name;
431 };
432
433 extern const struct nand_manufacturer nand_mfrs[];
434
435 /*
436 * Manufacturer specific parameter functions
437 */
438 int nand_read_parameters_micron(device_t, struct nand_chip *);
439
440 /* debug inlines */
441
442 static inline void
443 nand_dump_data(const char *name, void *data, size_t len)
444 {
445 uint8_t *dump = data;
446 int i;
447
448 printf("dumping %s\n--------------\n", name);
449 for (i = 0; i < len; i++) {
450 printf("0x%.2hhx ", *dump);
451 dump++;
452 }
453 printf("\n--------------\n");
454 }
455
456 /* flash interface implementation */
457 int nand_flash_isbad(device_t, flash_off_t, bool *);
458 int nand_flash_markbad(device_t, flash_off_t);
459 int nand_flash_write(device_t, flash_off_t, size_t, size_t *, const u_char *);
460 int nand_flash_read(device_t, flash_off_t, size_t, size_t *, uint8_t *);
461 int nand_flash_erase(device_t, struct flash_erase_instruction *);
462
463 /* nand specific functions */
464 int nand_erase_block(device_t, size_t);
465
466 int nand_io_submit(device_t, struct buf *);
467 void nand_sync_thread(void *);
468 int nand_sync_thread_start(device_t);
469 void nand_sync_thread_stop(device_t);
470
471 bool nand_isfactorybad(device_t, flash_off_t);
472 bool nand_iswornoutbad(device_t, flash_off_t);
473 bool nand_isbad(device_t, flash_off_t);
474 void nand_markbad(device_t, size_t);
475
476 //int nand_read_page(device_t, size_t, uint8_t *);
477 int nand_read_oob(device_t, size_t, uint8_t *);
478 //int nand_program_page(device_t, size_t, const uint8_t *);
479
480 device_t nand_attach_mi(struct nand_interface *, device_t);
481 void nand_init_interface(struct nand_interface *);
482
483 /* controller drivers may use these functions to get info about the chip */
484 void nand_read_id(device_t, uint8_t *, uint8_t *);
485 int nand_read_parameter_page(device_t, struct onfi_parameter_page *);
486
487 /*
488 * default functions for driver development
489 */
490 void nand_default_select(device_t, bool);
491 int nand_default_ecc_compute(device_t, const uint8_t *, uint8_t *);
492 int nand_default_ecc_correct(device_t, uint8_t *, const uint8_t *,
493 const uint8_t *);
494 int nand_default_read_page(device_t, size_t, uint8_t *);
495 int nand_default_program_page(device_t, size_t, const uint8_t *);
496
497 static inline void nand_busy(device_t);
498 static inline void nand_select(device_t, bool);
499 static inline void nand_command(device_t, uint8_t);
500 static inline void nand_address(device_t, uint32_t);
501 static inline void nand_read_buf_byte(device_t, void *, size_t);
502 static inline void nand_read_buf_word(device_t, void *, size_t);
503 static inline void nand_read_byte(device_t, uint8_t *);
504 static inline void nand_write_buf_byte(device_t, const void *, size_t);
505 static inline void nand_write_buf_word(device_t, const void *, size_t);
506 //static inline bool nand_block_isbad(device_t, off_t);
507 //static inline void nand_block_markbad(device_t, off_t);
508 //static inline bool nand_isbusy(device_t);
509
510 #endif /* _NAND_H_ */
511