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