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