1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#ifndef __LINUX_MTD_NAND_H
19#define __LINUX_MTD_NAND_H
20
21#include <linux/wait.h>
22#include <linux/spinlock.h>
23#include <linux/mtd/mtd.h>
24#include <linux/mtd/flashchip.h>
25#include <linux/mtd/bbm.h>
26
27struct mtd_info;
28struct nand_flash_dev;
29
30extern int nand_scan(struct mtd_info *mtd, int max_chips);
31
32
33
34
35extern int nand_scan_ident(struct mtd_info *mtd, int max_chips,
36 struct nand_flash_dev *table);
37extern int nand_scan_tail(struct mtd_info *mtd);
38
39
40extern void nand_release(struct mtd_info *mtd);
41
42
43extern void nand_wait_ready(struct mtd_info *mtd);
44
45
46extern int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
47
48
49extern int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
50
51
52#define NAND_MAX_CHIPS 8
53
54
55
56
57
58
59#define NAND_MAX_OOBSIZE 744
60#define NAND_MAX_PAGESIZE 8192
61
62
63
64
65
66
67
68
69#define NAND_NCE 0x01
70
71#define NAND_CLE 0x02
72
73#define NAND_ALE 0x04
74
75#define NAND_CTRL_CLE (NAND_NCE | NAND_CLE)
76#define NAND_CTRL_ALE (NAND_NCE | NAND_ALE)
77#define NAND_CTRL_CHANGE 0x80
78
79
80
81
82#define NAND_CMD_READ0 0
83#define NAND_CMD_READ1 1
84#define NAND_CMD_RNDOUT 5
85#define NAND_CMD_PAGEPROG 0x10
86#define NAND_CMD_READOOB 0x50
87#define NAND_CMD_ERASE1 0x60
88#define NAND_CMD_STATUS 0x70
89#define NAND_CMD_SEQIN 0x80
90#define NAND_CMD_RNDIN 0x85
91#define NAND_CMD_READID 0x90
92#define NAND_CMD_ERASE2 0xd0
93#define NAND_CMD_PARAM 0xec
94#define NAND_CMD_GET_FEATURES 0xee
95#define NAND_CMD_SET_FEATURES 0xef
96#define NAND_CMD_RESET 0xff
97
98#define NAND_CMD_LOCK 0x2a
99#define NAND_CMD_UNLOCK1 0x23
100#define NAND_CMD_UNLOCK2 0x24
101
102
103#define NAND_CMD_READSTART 0x30
104#define NAND_CMD_RNDOUTSTART 0xE0
105#define NAND_CMD_CACHEDPROG 0x15
106
107#define NAND_CMD_NONE -1
108
109
110#define NAND_STATUS_FAIL 0x01
111#define NAND_STATUS_FAIL_N1 0x02
112#define NAND_STATUS_TRUE_READY 0x20
113#define NAND_STATUS_READY 0x40
114#define NAND_STATUS_WP 0x80
115
116
117
118
119typedef enum {
120 NAND_ECC_NONE,
121 NAND_ECC_SOFT,
122 NAND_ECC_HW,
123 NAND_ECC_HW_SYNDROME,
124 NAND_ECC_HW_OOB_FIRST,
125 NAND_ECC_SOFT_BCH,
126} nand_ecc_modes_t;
127
128
129
130
131
132#define NAND_ECC_READ 0
133
134#define NAND_ECC_WRITE 1
135
136#define NAND_ECC_READSYN 2
137
138
139#define NAND_GET_DEVICE 0x80
140
141
142
143
144
145
146
147#define NAND_BUSWIDTH_16 0x00000002
148
149#define NAND_CACHEPRG 0x00000008
150
151
152
153
154
155#define NAND_NEED_READRDY 0x00000100
156
157
158#define NAND_NO_SUBPAGE_WRITE 0x00000200
159
160
161#define NAND_BROKEN_XD 0x00000400
162
163
164#define NAND_ROM 0x00000800
165
166
167#define NAND_SUBPAGE_READ 0x00001000
168
169
170#define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG
171
172
173#define NAND_HAS_CACHEPROG(chip) ((chip->options & NAND_CACHEPRG))
174#define NAND_HAS_SUBPAGE_READ(chip) ((chip->options & NAND_SUBPAGE_READ))
175
176
177
178#define NAND_SKIP_BBTSCAN 0x00010000
179
180
181
182
183#define NAND_OWN_BUFFERS 0x00020000
184
185#define NAND_SCAN_SILENT_NODEV 0x00040000
186
187
188
189
190
191
192#define NAND_BUSWIDTH_AUTO 0x00080000
193
194
195
196#define NAND_CONTROLLER_ALLOC 0x80000000
197
198
199#define NAND_CI_CHIPNR_MSK 0x03
200#define NAND_CI_CELLTYPE_MSK 0x0C
201
202
203struct nand_chip;
204
205
206#define ONFI_FEATURE_16_BIT_BUS (1 << 0)
207#define ONFI_FEATURE_EXT_PARAM_PAGE (1 << 7)
208
209
210#define ONFI_TIMING_MODE_0 (1 << 0)
211#define ONFI_TIMING_MODE_1 (1 << 1)
212#define ONFI_TIMING_MODE_2 (1 << 2)
213#define ONFI_TIMING_MODE_3 (1 << 3)
214#define ONFI_TIMING_MODE_4 (1 << 4)
215#define ONFI_TIMING_MODE_5 (1 << 5)
216#define ONFI_TIMING_MODE_UNKNOWN (1 << 6)
217
218
219#define ONFI_FEATURE_ADDR_TIMING_MODE 0x1
220
221
222#define ONFI_SUBFEATURE_PARAM_LEN 4
223
224
225#define ONFI_OPT_CMD_SET_GET_FEATURES (1 << 2)
226
227struct nand_onfi_params {
228
229
230 u8 sig[4];
231 __le16 revision;
232 __le16 features;
233 __le16 opt_cmd;
234 u8 reserved0[2];
235 __le16 ext_param_page_length;
236 u8 num_of_param_pages;
237 u8 reserved1[17];
238
239
240 char manufacturer[12];
241 char model[20];
242 u8 jedec_id;
243 __le16 date_code;
244 u8 reserved2[13];
245
246
247 __le32 byte_per_page;
248 __le16 spare_bytes_per_page;
249 __le32 data_bytes_per_ppage;
250 __le16 spare_bytes_per_ppage;
251 __le32 pages_per_block;
252 __le32 blocks_per_lun;
253 u8 lun_count;
254 u8 addr_cycles;
255 u8 bits_per_cell;
256 __le16 bb_per_lun;
257 __le16 block_endurance;
258 u8 guaranteed_good_blocks;
259 __le16 guaranteed_block_endurance;
260 u8 programs_per_page;
261 u8 ppage_attr;
262 u8 ecc_bits;
263 u8 interleaved_bits;
264 u8 interleaved_ops;
265 u8 reserved3[13];
266
267
268 u8 io_pin_capacitance_max;
269 __le16 async_timing_mode;
270 __le16 program_cache_timing_mode;
271 __le16 t_prog;
272 __le16 t_bers;
273 __le16 t_r;
274 __le16 t_ccs;
275 __le16 src_sync_timing_mode;
276 __le16 src_ssync_features;
277 __le16 clk_pin_capacitance_typ;
278 __le16 io_pin_capacitance_typ;
279 __le16 input_pin_capacitance_typ;
280 u8 input_pin_capacitance_max;
281 u8 driver_strenght_support;
282 __le16 t_int_r;
283 __le16 t_ald;
284 u8 reserved4[7];
285
286
287 u8 reserved5[90];
288
289 __le16 crc;
290} __attribute__((packed));
291
292#define ONFI_CRC_BASE 0x4F4E
293
294
295struct onfi_ext_ecc_info {
296 u8 ecc_bits;
297 u8 codeword_size;
298 __le16 bb_per_lun;
299 __le16 block_endurance;
300 u8 reserved[2];
301} __packed;
302
303#define ONFI_SECTION_TYPE_0 0
304#define ONFI_SECTION_TYPE_1 1
305#define ONFI_SECTION_TYPE_2 2
306struct onfi_ext_section {
307 u8 type;
308 u8 length;
309} __packed;
310
311#define ONFI_EXT_SECTION_MAX 8
312
313
314struct onfi_ext_param_page {
315 __le16 crc;
316 u8 sig[4];
317 u8 reserved0[10];
318 struct onfi_ext_section sections[ONFI_EXT_SECTION_MAX];
319
320
321
322
323
324
325
326} __packed;
327
328
329
330
331
332
333
334
335
336struct nand_hw_control {
337 spinlock_t lock;
338 struct nand_chip *active;
339 wait_queue_head_t wq;
340};
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373struct nand_ecc_ctrl {
374 nand_ecc_modes_t mode;
375 int steps;
376 int size;
377 int bytes;
378 int total;
379 int strength;
380 int prepad;
381 int postpad;
382 struct nand_ecclayout *layout;
383 void *priv;
384 void (*hwctl)(struct mtd_info *mtd, int mode);
385 int (*calculate)(struct mtd_info *mtd, const uint8_t *dat,
386 uint8_t *ecc_code);
387 int (*correct)(struct mtd_info *mtd, uint8_t *dat, uint8_t *read_ecc,
388 uint8_t *calc_ecc);
389 int (*read_page_raw)(struct mtd_info *mtd, struct nand_chip *chip,
390 uint8_t *buf, int oob_required, int page);
391 int (*write_page_raw)(struct mtd_info *mtd, struct nand_chip *chip,
392 const uint8_t *buf, int oob_required);
393 int (*read_page)(struct mtd_info *mtd, struct nand_chip *chip,
394 uint8_t *buf, int oob_required, int page);
395 int (*read_subpage)(struct mtd_info *mtd, struct nand_chip *chip,
396 uint32_t offs, uint32_t len, uint8_t *buf);
397 int (*write_subpage)(struct mtd_info *mtd, struct nand_chip *chip,
398 uint32_t offset, uint32_t data_len,
399 const uint8_t *data_buf, int oob_required);
400 int (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
401 const uint8_t *buf, int oob_required);
402 int (*write_oob_raw)(struct mtd_info *mtd, struct nand_chip *chip,
403 int page);
404 int (*read_oob_raw)(struct mtd_info *mtd, struct nand_chip *chip,
405 int page);
406 int (*read_oob)(struct mtd_info *mtd, struct nand_chip *chip, int page);
407 int (*write_oob)(struct mtd_info *mtd, struct nand_chip *chip,
408 int page);
409};
410
411
412
413
414
415
416
417
418
419
420struct nand_buffers {
421 uint8_t ecccalc[NAND_MAX_OOBSIZE];
422 uint8_t ecccode[NAND_MAX_OOBSIZE];
423 uint8_t databuf[NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE];
424};
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518struct nand_chip {
519 void __iomem *IO_ADDR_R;
520 void __iomem *IO_ADDR_W;
521
522 uint8_t (*read_byte)(struct mtd_info *mtd);
523 u16 (*read_word)(struct mtd_info *mtd);
524 void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
525 void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
526 void (*select_chip)(struct mtd_info *mtd, int chip);
527 int (*block_bad)(struct mtd_info *mtd, loff_t ofs, int getchip);
528 int (*block_markbad)(struct mtd_info *mtd, loff_t ofs);
529 void (*cmd_ctrl)(struct mtd_info *mtd, int dat, unsigned int ctrl);
530 int (*init_size)(struct mtd_info *mtd, struct nand_chip *this,
531 u8 *id_data);
532 int (*dev_ready)(struct mtd_info *mtd);
533 void (*cmdfunc)(struct mtd_info *mtd, unsigned command, int column,
534 int page_addr);
535 int(*waitfunc)(struct mtd_info *mtd, struct nand_chip *this);
536 void (*erase_cmd)(struct mtd_info *mtd, int page);
537 int (*scan_bbt)(struct mtd_info *mtd);
538 int (*errstat)(struct mtd_info *mtd, struct nand_chip *this, int state,
539 int status, int page);
540 int (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
541 uint32_t offset, int data_len, const uint8_t *buf,
542 int oob_required, int page, int cached, int raw);
543 int (*onfi_set_features)(struct mtd_info *mtd, struct nand_chip *chip,
544 int feature_addr, uint8_t *subfeature_para);
545 int (*onfi_get_features)(struct mtd_info *mtd, struct nand_chip *chip,
546 int feature_addr, uint8_t *subfeature_para);
547
548 int chip_delay;
549 unsigned int options;
550 unsigned int bbt_options;
551
552 int page_shift;
553 int phys_erase_shift;
554 int bbt_erase_shift;
555 int chip_shift;
556 int numchips;
557 uint64_t chipsize;
558 int pagemask;
559 int pagebuf;
560 unsigned int pagebuf_bitflips;
561 int subpagesize;
562 uint8_t cellinfo;
563 uint16_t ecc_strength_ds;
564 uint16_t ecc_step_ds;
565 int badblockpos;
566 int badblockbits;
567
568 int onfi_version;
569 struct nand_onfi_params onfi_params;
570
571 flstate_t state;
572
573 uint8_t *oob_poi;
574 struct nand_hw_control *controller;
575 struct nand_ecclayout *ecclayout;
576
577 struct nand_ecc_ctrl ecc;
578 struct nand_buffers *buffers;
579 struct nand_hw_control hwcontrol;
580
581 uint8_t *bbt;
582 struct nand_bbt_descr *bbt_td;
583 struct nand_bbt_descr *bbt_md;
584
585 struct nand_bbt_descr *badblock_pattern;
586
587 void *priv;
588};
589
590
591
592
593#define NAND_MFR_TOSHIBA 0x98
594#define NAND_MFR_SAMSUNG 0xec
595#define NAND_MFR_FUJITSU 0x04
596#define NAND_MFR_NATIONAL 0x8f
597#define NAND_MFR_RENESAS 0x07
598#define NAND_MFR_STMICRO 0x20
599#define NAND_MFR_HYNIX 0xad
600#define NAND_MFR_MICRON 0x2c
601#define NAND_MFR_AMD 0x01
602#define NAND_MFR_MACRONIX 0xc2
603#define NAND_MFR_EON 0x92
604
605
606#define NAND_MAX_ID_LEN 8
607
608
609
610
611
612
613#define LEGACY_ID_NAND(nm, devid, chipsz, erasesz, opts) \
614 { .name = (nm), {{ .dev_id = (devid) }}, .pagesize = 512, \
615 .chipsize = (chipsz), .erasesize = (erasesz), .options = (opts) }
616
617
618
619
620
621
622
623
624
625
626
627#define EXTENDED_ID_NAND(nm, devid, chipsz, opts) \
628 { .name = (nm), {{ .dev_id = (devid) }}, .chipsize = (chipsz), \
629 .options = (opts) }
630
631#define NAND_ECC_INFO(_strength, _step) \
632 { .strength_ds = (_strength), .step_ds = (_step) }
633#define NAND_ECC_STRENGTH(type) ((type)->ecc.strength_ds)
634#define NAND_ECC_STEP(type) ((type)->ecc.step_ds)
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660struct nand_flash_dev {
661 char *name;
662 union {
663 struct {
664 uint8_t mfr_id;
665 uint8_t dev_id;
666 };
667 uint8_t id[NAND_MAX_ID_LEN];
668 };
669 unsigned int pagesize;
670 unsigned int chipsize;
671 unsigned int erasesize;
672 unsigned int options;
673 uint16_t id_len;
674 uint16_t oobsize;
675 struct {
676 uint16_t strength_ds;
677 uint16_t step_ds;
678 } ecc;
679};
680
681
682
683
684
685
686struct nand_manufacturers {
687 int id;
688 char *name;
689};
690
691extern struct nand_flash_dev nand_flash_ids[];
692extern struct nand_manufacturers nand_manuf_ids[];
693
694extern int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd);
695extern int nand_default_bbt(struct mtd_info *mtd);
696extern int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs);
697extern int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt);
698extern int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
699 int allowbbt);
700extern int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
701 size_t *retlen, uint8_t *buf);
702
703
704
705
706
707
708
709
710
711
712
713
714
715struct platform_nand_chip {
716 int nr_chips;
717 int chip_offset;
718 int nr_partitions;
719 struct mtd_partition *partitions;
720 struct nand_ecclayout *ecclayout;
721 int chip_delay;
722 unsigned int options;
723 unsigned int bbt_options;
724 const char **part_probe_types;
725};
726
727
728struct platform_device;
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746struct platform_nand_ctrl {
747 int (*probe)(struct platform_device *pdev);
748 void (*remove)(struct platform_device *pdev);
749 void (*hwcontrol)(struct mtd_info *mtd, int cmd);
750 int (*dev_ready)(struct mtd_info *mtd);
751 void (*select_chip)(struct mtd_info *mtd, int chip);
752 void (*cmd_ctrl)(struct mtd_info *mtd, int dat, unsigned int ctrl);
753 void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
754 void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
755 unsigned char (*read_byte)(struct mtd_info *mtd);
756 void *priv;
757};
758
759
760
761
762
763
764struct platform_nand_data {
765 struct platform_nand_chip chip;
766 struct platform_nand_ctrl ctrl;
767};
768
769
770static inline
771struct platform_nand_chip *get_platform_nandchip(struct mtd_info *mtd)
772{
773 struct nand_chip *chip = mtd->priv;
774
775 return chip->priv;
776}
777
778
779static inline int onfi_feature(struct nand_chip *chip)
780{
781 return chip->onfi_version ? le16_to_cpu(chip->onfi_params.features) : 0;
782}
783
784
785static inline int onfi_get_async_timing_mode(struct nand_chip *chip)
786{
787 if (!chip->onfi_version)
788 return ONFI_TIMING_MODE_UNKNOWN;
789 return le16_to_cpu(chip->onfi_params.async_timing_mode);
790}
791
792
793static inline int onfi_get_sync_timing_mode(struct nand_chip *chip)
794{
795 if (!chip->onfi_version)
796 return ONFI_TIMING_MODE_UNKNOWN;
797 return le16_to_cpu(chip->onfi_params.src_sync_timing_mode);
798}
799
800#endif
801