1 /* $NetBSD: nand.h,v 1.21 2022/08/07 11:06:19 andvar Exp $ */
2
3 /*-
4 * Copyright (c) 2010 Department of Software Engineering,
5 * University of Szeged, Hungary
6 * Copyright (c) 2010 Adam Hoka <ahoka@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 #ifdef _KERNEL_OPT
41 #include "opt_nand.h"
42 #endif
43
44 #include <sys/bufq.h>
45 #include <sys/buf.h>
46 #include <sys/time.h>
47
48 #include <dev/nand/onfi.h>
49 #include <dev/flash/flash.h>
50 #include <dev/flash/flash_io.h>
51
52 #ifdef NAND_DEBUG
53 #define DPRINTF(x) printf x
54 #else
55 #define DPRINTF(x)
56 #endif
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; /* redundant 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; /* buffer for ecc */
118 uint64_t nc_size; /* storage size in bytes */
119 uint32_t nc_page_size; /* page size in bytes */
120 uint32_t nc_block_size; /* block size in bytes */
121 uint32_t nc_lun_blocks; /* LUN size in blocks */
122 uint32_t nc_flags; /* bitfield flags */
123 uint32_t nc_quirks; /* bitfield quirks */
124 uint32_t nc_page_shift; /* page shift for page alignment */
125 uint32_t nc_page_mask; /* page mask for page alignment */
126 uint32_t nc_block_shift; /* write shift */
127 uint32_t nc_block_mask; /* write mask */
128 uint16_t nc_spare_size; /* spare (oob) size in bytes */
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 struct lwp *nwc_thread;
147 kcondvar_t nwc_cv;
148 bool nwc_exiting;
149 };
150
151 /* driver softc for nand */
152 struct nand_softc {
153 device_t sc_dev;
154 device_t controller_dev;
155 struct nand_interface *nand_if;
156 void *nand_softc;
157 struct nand_chip sc_chip;
158 struct nand_bbt sc_bbt;
159 size_t sc_part_offset;
160 size_t sc_part_size;
161 kmutex_t sc_device_lock; /* serialize access to chip */
162 struct flash_io sc_flash_io;
163 };
164
165 /* structure holding the nand api */
166 struct nand_interface {
167 /* basic nand controller commands */
168 void (*select) (device_t, bool); /* optional */
169 void (*command) (device_t, uint8_t);
170 void (*address) (device_t, uint8_t);
171 void (*read_buf_1) (device_t, void *, size_t);
172 void (*read_buf_2) (device_t, void *, size_t);
173 void (*read_1) (device_t, uint8_t *);
174 void (*read_2) (device_t, uint16_t *);
175 void (*write_buf_1) (device_t, const void *, size_t);
176 void (*write_buf_2) (device_t, const void *, size_t);
177 void (*write_1) (device_t, uint8_t);
178 void (*write_2) (device_t, uint16_t);
179 void (*busy) (device_t);
180
181 /* "smart" controllers may override read/program functions */
182 int (*read_page) (device_t, size_t, uint8_t *); /* optional */
183 int (*program_page) (device_t, size_t, const uint8_t *); /* optional */
184
185 /* functions specific to ecc computation */
186 int (*ecc_prepare)(device_t, int); /* optional */
187 int (*ecc_compute)(device_t, const uint8_t *, uint8_t *);
188 int (*ecc_correct)(device_t, uint8_t *, const uint8_t *,
189 const uint8_t *);
190
191 /* information for the ecc engine */
192 struct nand_ecc ecc;
193
194 /* flash partition information */
195 const struct flash_partition *part_info;
196 int part_num;
197 };
198
199 /* attach args */
200 struct nand_attach_args {
201 struct nand_interface *naa_nand_if;
202 };
203
204 static __inline void
nand_busy(device_t device)205 nand_busy(device_t device)
206 {
207 struct nand_softc * const sc = device_private(device);
208
209 KASSERT(sc->nand_if->select != NULL);
210 KASSERT(sc->controller_dev != NULL);
211
212 sc->nand_if->select(sc->controller_dev, true);
213
214 if (sc->nand_if->busy != NULL) {
215 sc->nand_if->busy(sc->controller_dev);
216 }
217
218 sc->nand_if->select(sc->controller_dev, false);
219 }
220
221 static __inline void
nand_select(device_t self,bool enable)222 nand_select(device_t self, bool enable)
223 {
224 struct nand_softc * const sc = device_private(self);
225
226 KASSERT(sc->nand_if->select != NULL);
227 KASSERT(sc->controller_dev != NULL);
228
229 sc->nand_if->select(sc->controller_dev, enable);
230 }
231
232 static __inline void
nand_address(device_t self,uint32_t address)233 nand_address(device_t self, uint32_t address)
234 {
235 struct nand_softc * const sc = device_private(self);
236
237 KASSERT(sc->nand_if->address != NULL);
238 KASSERT(sc->controller_dev != NULL);
239
240 sc->nand_if->address(sc->controller_dev, address);
241 }
242
243 static __inline void
nand_command(device_t self,uint8_t command)244 nand_command(device_t self, uint8_t command)
245 {
246 struct nand_softc * const sc = device_private(self);
247
248 KASSERT(sc->nand_if->command != NULL);
249 KASSERT(sc->controller_dev != NULL);
250
251 sc->nand_if->command(sc->controller_dev, command);
252 }
253
254 static __inline void
nand_read_1(device_t self,uint8_t * data)255 nand_read_1(device_t self, uint8_t *data)
256 {
257 struct nand_softc * const sc = device_private(self);
258
259 KASSERT(sc->nand_if->read_1 != NULL);
260 KASSERT(sc->controller_dev != NULL);
261
262 sc->nand_if->read_1(sc->controller_dev, data);
263 }
264
265 static __inline void
nand_write_1(device_t self,uint8_t data)266 nand_write_1(device_t self, uint8_t data)
267 {
268 struct nand_softc * const sc = device_private(self);
269
270 KASSERT(sc->nand_if->write_1 != NULL);
271 KASSERT(sc->controller_dev != NULL);
272
273 sc->nand_if->write_1(sc->controller_dev, data);
274 }
275
276 static __inline void
nand_read_2(device_t self,uint16_t * data)277 nand_read_2(device_t self, uint16_t *data)
278 {
279 struct nand_softc * const sc = device_private(self);
280
281 KASSERT(sc->nand_if->read_2 != NULL);
282 KASSERT(sc->controller_dev != NULL);
283
284 sc->nand_if->read_2(sc->controller_dev, data);
285 }
286
287 static __inline void
nand_write_2(device_t self,uint16_t data)288 nand_write_2(device_t self, uint16_t data)
289 {
290 struct nand_softc * const sc = device_private(self);
291
292 KASSERT(sc->nand_if->write_2 != NULL);
293 KASSERT(sc->controller_dev != NULL);
294
295 sc->nand_if->write_2(sc->controller_dev, data);
296 }
297
298 static __inline void
nand_read_buf_1(device_t self,void * buf,size_t size)299 nand_read_buf_1(device_t self, void *buf, size_t size)
300 {
301 struct nand_softc * const sc = device_private(self);
302
303 KASSERT(sc->nand_if->read_buf_1 != NULL);
304 KASSERT(sc->controller_dev != NULL);
305
306 sc->nand_if->read_buf_1(sc->controller_dev, buf, size);
307 }
308
309 static __inline void
nand_read_buf_2(device_t self,void * buf,size_t size)310 nand_read_buf_2(device_t self, void *buf, size_t size)
311 {
312 struct nand_softc * const sc = device_private(self);
313
314 KASSERT(sc->nand_if->read_buf_2 != NULL);
315 KASSERT(sc->controller_dev != NULL);
316
317 sc->nand_if->read_buf_2(sc->controller_dev, buf, size);
318 }
319
320 static __inline void
nand_write_buf_1(device_t self,const void * buf,size_t size)321 nand_write_buf_1(device_t self, const void *buf, size_t size)
322 {
323 struct nand_softc * const sc = device_private(self);
324
325 KASSERT(sc->nand_if->write_buf_1 != NULL);
326 KASSERT(sc->controller_dev != NULL);
327
328 sc->nand_if->write_buf_1(sc->controller_dev, buf, size);
329 }
330
331 static __inline void
nand_write_buf_2(device_t self,const void * buf,size_t size)332 nand_write_buf_2(device_t self, const void *buf, size_t size)
333 {
334 struct nand_softc * const sc = device_private(self);
335
336 KASSERT(sc->nand_if->write_buf_2 != NULL);
337 KASSERT(sc->controller_dev != NULL);
338
339 sc->nand_if->write_buf_2(sc->controller_dev, buf, size);
340 }
341
342 static __inline int
nand_ecc_correct(device_t self,uint8_t * data,const uint8_t * oldcode,const uint8_t * newcode)343 nand_ecc_correct(device_t self, uint8_t *data, const uint8_t *oldcode,
344 const uint8_t *newcode)
345 {
346 struct nand_softc * const sc = device_private(self);
347
348 KASSERT(sc->nand_if->ecc_correct != NULL);
349 KASSERT(sc->controller_dev != NULL);
350
351 return sc->nand_if->ecc_correct(sc->controller_dev, data, oldcode, newcode);
352 }
353
354 static __inline void
nand_ecc_compute(device_t self,const uint8_t * data,uint8_t * code)355 nand_ecc_compute(device_t self, const uint8_t *data, uint8_t *code)
356 {
357 struct nand_softc * const sc = device_private(self);
358
359 KASSERT(sc->nand_if->ecc_compute != NULL);
360 KASSERT(sc->controller_dev != NULL);
361
362 sc->nand_if->ecc_compute(sc->controller_dev, data, code);
363 }
364
365 static __inline void
nand_ecc_prepare(device_t self,int mode)366 nand_ecc_prepare(device_t self, int mode)
367 {
368 struct nand_softc * const sc = device_private(self);
369
370 KASSERT(sc->controller_dev != NULL);
371
372 if (sc->nand_if->ecc_prepare != NULL)
373 sc->nand_if->ecc_prepare(sc->controller_dev, mode);
374 }
375
376 static __inline int
nand_program_page(device_t self,size_t offset,const uint8_t * data)377 nand_program_page(device_t self, size_t offset, const uint8_t *data)
378 {
379 struct nand_softc * const sc = device_private(self);
380
381 KASSERT(sc->nand_if->program_page != NULL);
382
383 return sc->nand_if->program_page(self, offset, data);
384 }
385
386 static __inline int
nand_read_page(device_t self,size_t offset,uint8_t * data)387 nand_read_page(device_t self, size_t offset, uint8_t *data)
388 {
389 struct nand_softc * const sc = device_private(self);
390
391 KASSERT(sc->nand_if->read_page != NULL);
392
393 return sc->nand_if->read_page(self, offset, data);
394 }
395
396 #if 0
397 static __inline bool
398 nand_block_isbad(device_t self, flash_off_t block)
399 {
400 struct nand_softc * const sc = device_private(self);
401
402 KASSERT(sc->nand_if->block_isbad != NULL);
403 KASSERT(sc->controller_dev != NULL);
404
405 return sc->nand_if->block_isbad(sc->controller_dev, block);
406 }
407 #endif
408
409 /* Manufacturer IDs defined by JEDEC */
410 enum {
411 NAND_MFR_UNKNOWN = 0x00,
412 NAND_MFR_AMD = 0x01,
413 NAND_MFR_FUJITSU = 0x04,
414 NAND_MFR_RENESAS = 0x07,
415 NAND_MFR_STMICRO = 0x20,
416 NAND_MFR_MICRON = 0x2c,
417 NAND_MFR_NATIONAL = 0x8f,
418 NAND_MFR_TOSHIBA = 0x98,
419 NAND_MFR_HYNIX = 0xad,
420 NAND_MFR_SAMSUNG = 0xec
421 };
422
423 struct nand_manufacturer {
424 int id;
425 const char *name;
426 };
427
428 extern const struct nand_manufacturer nand_mfrs[];
429
430 /*
431 * Manufacturer specific parameter functions
432 */
433 int nand_read_parameters_micron(device_t, struct nand_chip *);
434 int nand_read_parameters_samsung(device_t, struct nand_chip *);
435 int nand_read_parameters_toshiba(device_t, struct nand_chip *);
436
437 /* debug inlines */
438
439 static __inline void
nand_dump_data(const char * name,void * data,size_t len)440 nand_dump_data(const char *name, void *data, size_t len)
441 {
442 uint8_t *dump = data;
443 int i;
444
445 printf("dumping %s\n--------------\n", name);
446 for (i = 0; i < len; i++) {
447 printf("0x%.2hhx ", *dump);
448 dump++;
449 }
450 printf("\n--------------\n");
451 }
452
453 /* flash interface implementation */
454 int nand_flash_isbad(device_t, flash_off_t, bool *);
455 int nand_flash_markbad(device_t, flash_off_t);
456 int nand_flash_write(device_t, flash_off_t, size_t, size_t *, const u_char *);
457 int nand_flash_read(device_t, flash_off_t, size_t, size_t *, uint8_t *);
458 int nand_flash_erase(device_t, struct flash_erase_instruction *);
459 int nand_flash_submit(device_t, struct buf *);
460
461 /* nand specific functions */
462 int nand_erase_block(device_t, size_t);
463
464 bool nand_isfactorybad(device_t, flash_off_t);
465 bool nand_iswornoutbad(device_t, flash_off_t);
466 bool nand_isbad(device_t, flash_off_t);
467 void nand_markbad(device_t, size_t);
468
469 //int nand_read_page(device_t, size_t, uint8_t *);
470 int nand_read_oob(device_t, size_t, uint8_t *);
471 //int nand_program_page(device_t, size_t, const uint8_t *);
472
473 device_t nand_attach_mi(struct nand_interface *, device_t);
474 void nand_init_interface(struct nand_interface *);
475 void nand_attach_mtdparts(device_t, const char *, const char *);
476
477 /* controller drivers may use these functions to get info about the chip */
478 void nand_read_id(device_t, uint8_t *, uint8_t *);
479 int nand_read_parameter_page(device_t, struct onfi_parameter_page *);
480
481 /*
482 * default functions for driver development
483 */
484 void nand_default_select(device_t, bool);
485 int nand_default_ecc_compute(device_t, const uint8_t *, uint8_t *);
486 int nand_default_ecc_correct(device_t, uint8_t *, const uint8_t *,
487 const uint8_t *);
488 int nand_default_read_page(device_t, size_t, uint8_t *);
489 int nand_default_program_page(device_t, size_t, const uint8_t *);
490
491 static __inline void nand_busy(device_t);
492 static __inline void nand_select(device_t, bool);
493 static __inline void nand_command(device_t, uint8_t);
494 static __inline void nand_address(device_t, uint32_t);
495 static __inline void nand_read_buf_1(device_t, void *, size_t);
496 static __inline void nand_read_buf_2(device_t, void *, size_t);
497 static __inline void nand_read_1(device_t, uint8_t *);
498 static __inline void nand_write_buf_1(device_t, const void *, size_t);
499 static __inline void nand_write_buf_2(device_t, const void *, size_t);
500 //static __inline bool nand_block_isbad(device_t, off_t);
501 //static __inline void nand_block_markbad(device_t, off_t);
502 //static __inline bool nand_isbusy(device_t);
503
504 #endif /* _NAND_H_ */
505