0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef __LINUX_MTD_RAWNAND_H
0014 #define __LINUX_MTD_RAWNAND_H
0015
0016 #include <linux/mtd/mtd.h>
0017 #include <linux/mtd/nand.h>
0018 #include <linux/mtd/flashchip.h>
0019 #include <linux/mtd/bbm.h>
0020 #include <linux/mtd/jedec.h>
0021 #include <linux/mtd/onfi.h>
0022 #include <linux/mutex.h>
0023 #include <linux/of.h>
0024 #include <linux/types.h>
0025
0026 struct nand_chip;
0027 struct gpio_desc;
0028
0029
0030 #define NAND_MAX_CHIPS 8
0031
0032
0033
0034
0035
0036
0037
0038
0039 #define NAND_NCE 0x01
0040
0041 #define NAND_CLE 0x02
0042
0043 #define NAND_ALE 0x04
0044
0045 #define NAND_CTRL_CLE (NAND_NCE | NAND_CLE)
0046 #define NAND_CTRL_ALE (NAND_NCE | NAND_ALE)
0047 #define NAND_CTRL_CHANGE 0x80
0048
0049
0050
0051
0052 #define NAND_CMD_READ0 0
0053 #define NAND_CMD_READ1 1
0054 #define NAND_CMD_RNDOUT 5
0055 #define NAND_CMD_PAGEPROG 0x10
0056 #define NAND_CMD_READOOB 0x50
0057 #define NAND_CMD_ERASE1 0x60
0058 #define NAND_CMD_STATUS 0x70
0059 #define NAND_CMD_SEQIN 0x80
0060 #define NAND_CMD_RNDIN 0x85
0061 #define NAND_CMD_READID 0x90
0062 #define NAND_CMD_ERASE2 0xd0
0063 #define NAND_CMD_PARAM 0xec
0064 #define NAND_CMD_GET_FEATURES 0xee
0065 #define NAND_CMD_SET_FEATURES 0xef
0066 #define NAND_CMD_RESET 0xff
0067
0068
0069 #define NAND_CMD_READSTART 0x30
0070 #define NAND_CMD_RNDOUTSTART 0xE0
0071 #define NAND_CMD_CACHEDPROG 0x15
0072
0073 #define NAND_CMD_NONE -1
0074
0075
0076 #define NAND_STATUS_FAIL 0x01
0077 #define NAND_STATUS_FAIL_N1 0x02
0078 #define NAND_STATUS_TRUE_READY 0x20
0079 #define NAND_STATUS_READY 0x40
0080 #define NAND_STATUS_WP 0x80
0081
0082 #define NAND_DATA_IFACE_CHECK_ONLY -1
0083
0084
0085
0086
0087
0088 #define NAND_ECC_READ 0
0089
0090 #define NAND_ECC_WRITE 1
0091
0092 #define NAND_ECC_READSYN 2
0093
0094
0095
0096
0097
0098
0099
0100 #define NAND_ECC_GENERIC_ERASED_CHECK BIT(0)
0101
0102
0103
0104
0105
0106
0107
0108 #define NAND_BUSWIDTH_16 BIT(1)
0109
0110
0111
0112
0113
0114 #define NAND_ECC_SOFT_HAMMING_SM_ORDER BIT(2)
0115
0116
0117 #define NAND_CACHEPRG BIT(3)
0118
0119 #define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG
0120
0121
0122
0123
0124
0125
0126 #define NAND_NEED_READRDY BIT(8)
0127
0128
0129 #define NAND_NO_SUBPAGE_WRITE BIT(9)
0130
0131
0132 #define NAND_BROKEN_XD BIT(10)
0133
0134
0135 #define NAND_ROM BIT(11)
0136
0137
0138 #define NAND_SUBPAGE_READ BIT(12)
0139
0140 #define NAND_HAS_SUBPAGE_READ(chip) ((chip->options & NAND_SUBPAGE_READ))
0141
0142
0143
0144
0145
0146 #define NAND_NEED_SCRAMBLING BIT(13)
0147
0148
0149 #define NAND_ROW_ADDR_3 BIT(14)
0150
0151
0152
0153 #define NAND_SKIP_BBTSCAN BIT(16)
0154
0155 #define NAND_SCAN_SILENT_NODEV BIT(18)
0156
0157
0158
0159
0160
0161
0162
0163 #define NAND_BUSWIDTH_AUTO BIT(19)
0164
0165
0166
0167
0168
0169 #define NAND_USES_DMA BIT(20)
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179 #define NAND_WAIT_TCCS BIT(21)
0180
0181
0182
0183
0184
0185 #define NAND_IS_BOOT_MEDIUM BIT(22)
0186
0187
0188
0189
0190
0191
0192 #define NAND_KEEP_TIMINGS BIT(23)
0193
0194
0195
0196
0197
0198
0199
0200
0201 #define NAND_BBM_FIRSTPAGE BIT(24)
0202 #define NAND_BBM_SECONDPAGE BIT(25)
0203 #define NAND_BBM_LASTPAGE BIT(26)
0204
0205
0206
0207
0208
0209
0210
0211 #define NAND_NO_BBM_QUIRK BIT(27)
0212
0213
0214 #define NAND_CI_CHIPNR_MSK 0x03
0215 #define NAND_CI_CELLTYPE_MSK 0x0C
0216 #define NAND_CI_CELLTYPE_SHIFT 2
0217
0218
0219 #define NAND_BBM_POS_SMALL 5
0220 #define NAND_BBM_POS_LARGE 0
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230 struct nand_parameters {
0231
0232 const char *model;
0233 bool supports_set_get_features;
0234 DECLARE_BITMAP(set_feature_list, ONFI_FEATURE_NUMBER);
0235 DECLARE_BITMAP(get_feature_list, ONFI_FEATURE_NUMBER);
0236
0237
0238 struct onfi_params *onfi;
0239 };
0240
0241
0242 #define NAND_MAX_ID_LEN 8
0243
0244
0245
0246
0247
0248
0249 struct nand_id {
0250 u8 data[NAND_MAX_ID_LEN];
0251 int len;
0252 };
0253
0254
0255
0256
0257
0258
0259
0260 struct nand_ecc_step_info {
0261 int stepsize;
0262 const int *strengths;
0263 int nstrengths;
0264 };
0265
0266
0267
0268
0269
0270
0271
0272 struct nand_ecc_caps {
0273 const struct nand_ecc_step_info *stepinfos;
0274 int nstepinfos;
0275 int (*calc_ecc_bytes)(int step_size, int strength);
0276 };
0277
0278
0279 #define NAND_ECC_CAPS_SINGLE(__name, __calc, __step, ...) \
0280 static const int __name##_strengths[] = { __VA_ARGS__ }; \
0281 static const struct nand_ecc_step_info __name##_stepinfo = { \
0282 .stepsize = __step, \
0283 .strengths = __name##_strengths, \
0284 .nstrengths = ARRAY_SIZE(__name##_strengths), \
0285 }; \
0286 static const struct nand_ecc_caps __name = { \
0287 .stepinfos = &__name##_stepinfo, \
0288 .nstepinfos = 1, \
0289 .calc_ecc_bytes = __calc, \
0290 }
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345 struct nand_ecc_ctrl {
0346 enum nand_ecc_engine_type engine_type;
0347 enum nand_ecc_placement placement;
0348 enum nand_ecc_algo algo;
0349 int steps;
0350 int size;
0351 int bytes;
0352 int total;
0353 int strength;
0354 int prepad;
0355 int postpad;
0356 unsigned int options;
0357 u8 *calc_buf;
0358 u8 *code_buf;
0359 void (*hwctl)(struct nand_chip *chip, int mode);
0360 int (*calculate)(struct nand_chip *chip, const uint8_t *dat,
0361 uint8_t *ecc_code);
0362 int (*correct)(struct nand_chip *chip, uint8_t *dat, uint8_t *read_ecc,
0363 uint8_t *calc_ecc);
0364 int (*read_page_raw)(struct nand_chip *chip, uint8_t *buf,
0365 int oob_required, int page);
0366 int (*write_page_raw)(struct nand_chip *chip, const uint8_t *buf,
0367 int oob_required, int page);
0368 int (*read_page)(struct nand_chip *chip, uint8_t *buf,
0369 int oob_required, int page);
0370 int (*read_subpage)(struct nand_chip *chip, uint32_t offs,
0371 uint32_t len, uint8_t *buf, int page);
0372 int (*write_subpage)(struct nand_chip *chip, uint32_t offset,
0373 uint32_t data_len, const uint8_t *data_buf,
0374 int oob_required, int page);
0375 int (*write_page)(struct nand_chip *chip, const uint8_t *buf,
0376 int oob_required, int page);
0377 int (*write_oob_raw)(struct nand_chip *chip, int page);
0378 int (*read_oob_raw)(struct nand_chip *chip, int page);
0379 int (*read_oob)(struct nand_chip *chip, int page);
0380 int (*write_oob)(struct nand_chip *chip, int page);
0381 };
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434 struct nand_sdr_timings {
0435 u64 tBERS_max;
0436 u32 tCCS_min;
0437 u64 tPROG_max;
0438 u64 tR_max;
0439 u32 tALH_min;
0440 u32 tADL_min;
0441 u32 tALS_min;
0442 u32 tAR_min;
0443 u32 tCEA_max;
0444 u32 tCEH_min;
0445 u32 tCH_min;
0446 u32 tCHZ_max;
0447 u32 tCLH_min;
0448 u32 tCLR_min;
0449 u32 tCLS_min;
0450 u32 tCOH_min;
0451 u32 tCS_min;
0452 u32 tDH_min;
0453 u32 tDS_min;
0454 u32 tFEAT_max;
0455 u32 tIR_min;
0456 u32 tITC_max;
0457 u32 tRC_min;
0458 u32 tREA_max;
0459 u32 tREH_min;
0460 u32 tRHOH_min;
0461 u32 tRHW_min;
0462 u32 tRHZ_max;
0463 u32 tRLOH_min;
0464 u32 tRP_min;
0465 u32 tRR_min;
0466 u64 tRST_max;
0467 u32 tWB_max;
0468 u32 tWC_min;
0469 u32 tWH_min;
0470 u32 tWHR_min;
0471 u32 tWP_min;
0472 u32 tWW_min;
0473 };
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523 struct nand_nvddr_timings {
0524 u64 tBERS_max;
0525 u32 tCCS_min;
0526 u64 tPROG_max;
0527 u64 tR_max;
0528 u32 tAC_min;
0529 u32 tAC_max;
0530 u32 tADL_min;
0531 u32 tCAD_min;
0532 u32 tCAH_min;
0533 u32 tCALH_min;
0534 u32 tCALS_min;
0535 u32 tCAS_min;
0536 u32 tCEH_min;
0537 u32 tCH_min;
0538 u32 tCK_min;
0539 u32 tCS_min;
0540 u32 tDH_min;
0541 u32 tDQSCK_min;
0542 u32 tDQSCK_max;
0543 u32 tDQSD_min;
0544 u32 tDQSD_max;
0545 u32 tDQSHZ_max;
0546 u32 tDQSQ_max;
0547 u32 tDS_min;
0548 u32 tDSC_min;
0549 u32 tFEAT_max;
0550 u32 tITC_max;
0551 u32 tQHS_max;
0552 u32 tRHW_min;
0553 u32 tRR_min;
0554 u32 tRST_max;
0555 u32 tWB_max;
0556 u32 tWHR_min;
0557 u32 tWRCK_min;
0558 u32 tWW_min;
0559 };
0560
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576
0577
0578 #define NAND_COMMON_TIMING_PS(conf, timing_name) \
0579 nand_interface_is_sdr(conf) ? \
0580 nand_get_sdr_timings(conf)->timing_name : \
0581 nand_get_nvddr_timings(conf)->timing_name
0582
0583 #define NAND_COMMON_TIMING_MS(conf, timing_name) \
0584 PSEC_TO_MSEC(NAND_COMMON_TIMING_PS((conf), timing_name))
0585
0586 #define NAND_COMMON_TIMING_NS(conf, timing_name) \
0587 PSEC_TO_NSEC(NAND_COMMON_TIMING_PS((conf), timing_name))
0588
0589
0590
0591
0592
0593
0594 enum nand_interface_type {
0595 NAND_SDR_IFACE,
0596 NAND_NVDDR_IFACE,
0597 };
0598
0599
0600
0601
0602
0603
0604
0605
0606
0607 struct nand_interface_config {
0608 enum nand_interface_type type;
0609 struct nand_timings {
0610 unsigned int mode;
0611 union {
0612 struct nand_sdr_timings sdr;
0613 struct nand_nvddr_timings nvddr;
0614 };
0615 } timings;
0616 };
0617
0618
0619
0620
0621
0622 static bool nand_interface_is_sdr(const struct nand_interface_config *conf)
0623 {
0624 return conf->type == NAND_SDR_IFACE;
0625 }
0626
0627
0628
0629
0630
0631 static bool nand_interface_is_nvddr(const struct nand_interface_config *conf)
0632 {
0633 return conf->type == NAND_NVDDR_IFACE;
0634 }
0635
0636
0637
0638
0639
0640 static inline const struct nand_sdr_timings *
0641 nand_get_sdr_timings(const struct nand_interface_config *conf)
0642 {
0643 if (!nand_interface_is_sdr(conf))
0644 return ERR_PTR(-EINVAL);
0645
0646 return &conf->timings.sdr;
0647 }
0648
0649
0650
0651
0652
0653 static inline const struct nand_nvddr_timings *
0654 nand_get_nvddr_timings(const struct nand_interface_config *conf)
0655 {
0656 if (!nand_interface_is_nvddr(conf))
0657 return ERR_PTR(-EINVAL);
0658
0659 return &conf->timings.nvddr;
0660 }
0661
0662
0663
0664
0665
0666 struct nand_op_cmd_instr {
0667 u8 opcode;
0668 };
0669
0670
0671
0672
0673
0674
0675 struct nand_op_addr_instr {
0676 unsigned int naddrs;
0677 const u8 *addrs;
0678 };
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691
0692 struct nand_op_data_instr {
0693 unsigned int len;
0694 union {
0695 void *in;
0696 const void *out;
0697 } buf;
0698 bool force_8bit;
0699 };
0700
0701
0702
0703
0704
0705 struct nand_op_waitrdy_instr {
0706 unsigned int timeout_ms;
0707 };
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717 enum nand_op_instr_type {
0718 NAND_OP_CMD_INSTR,
0719 NAND_OP_ADDR_INSTR,
0720 NAND_OP_DATA_IN_INSTR,
0721 NAND_OP_DATA_OUT_INSTR,
0722 NAND_OP_WAITRDY_INSTR,
0723 };
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740 struct nand_op_instr {
0741 enum nand_op_instr_type type;
0742 union {
0743 struct nand_op_cmd_instr cmd;
0744 struct nand_op_addr_instr addr;
0745 struct nand_op_data_instr data;
0746 struct nand_op_waitrdy_instr waitrdy;
0747 } ctx;
0748 unsigned int delay_ns;
0749 };
0750
0751
0752
0753
0754
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764 #define __DIVIDE(dividend, divisor) ({ \
0765 (__typeof__(dividend))(sizeof(dividend) <= sizeof(unsigned long) ? \
0766 DIV_ROUND_UP(dividend, divisor) : \
0767 DIV_ROUND_UP_ULL(dividend, divisor)); \
0768 })
0769 #define PSEC_TO_NSEC(x) __DIVIDE(x, 1000)
0770 #define PSEC_TO_MSEC(x) __DIVIDE(x, 1000000000)
0771
0772 #define NAND_OP_CMD(id, ns) \
0773 { \
0774 .type = NAND_OP_CMD_INSTR, \
0775 .ctx.cmd.opcode = id, \
0776 .delay_ns = ns, \
0777 }
0778
0779 #define NAND_OP_ADDR(ncycles, cycles, ns) \
0780 { \
0781 .type = NAND_OP_ADDR_INSTR, \
0782 .ctx.addr = { \
0783 .naddrs = ncycles, \
0784 .addrs = cycles, \
0785 }, \
0786 .delay_ns = ns, \
0787 }
0788
0789 #define NAND_OP_DATA_IN(l, b, ns) \
0790 { \
0791 .type = NAND_OP_DATA_IN_INSTR, \
0792 .ctx.data = { \
0793 .len = l, \
0794 .buf.in = b, \
0795 .force_8bit = false, \
0796 }, \
0797 .delay_ns = ns, \
0798 }
0799
0800 #define NAND_OP_DATA_OUT(l, b, ns) \
0801 { \
0802 .type = NAND_OP_DATA_OUT_INSTR, \
0803 .ctx.data = { \
0804 .len = l, \
0805 .buf.out = b, \
0806 .force_8bit = false, \
0807 }, \
0808 .delay_ns = ns, \
0809 }
0810
0811 #define NAND_OP_8BIT_DATA_IN(l, b, ns) \
0812 { \
0813 .type = NAND_OP_DATA_IN_INSTR, \
0814 .ctx.data = { \
0815 .len = l, \
0816 .buf.in = b, \
0817 .force_8bit = true, \
0818 }, \
0819 .delay_ns = ns, \
0820 }
0821
0822 #define NAND_OP_8BIT_DATA_OUT(l, b, ns) \
0823 { \
0824 .type = NAND_OP_DATA_OUT_INSTR, \
0825 .ctx.data = { \
0826 .len = l, \
0827 .buf.out = b, \
0828 .force_8bit = true, \
0829 }, \
0830 .delay_ns = ns, \
0831 }
0832
0833 #define NAND_OP_WAIT_RDY(tout_ms, ns) \
0834 { \
0835 .type = NAND_OP_WAITRDY_INSTR, \
0836 .ctx.waitrdy.timeout_ms = tout_ms, \
0837 .delay_ns = ns, \
0838 }
0839
0840
0841
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855
0856
0857 struct nand_subop {
0858 unsigned int cs;
0859 const struct nand_op_instr *instrs;
0860 unsigned int ninstrs;
0861 unsigned int first_instr_start_off;
0862 unsigned int last_instr_end_off;
0863 };
0864
0865 unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
0866 unsigned int op_id);
0867 unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
0868 unsigned int op_id);
0869 unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
0870 unsigned int op_id);
0871 unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
0872 unsigned int op_id);
0873
0874
0875
0876
0877
0878
0879 struct nand_op_parser_addr_constraints {
0880 unsigned int maxcycles;
0881 };
0882
0883
0884
0885
0886
0887 struct nand_op_parser_data_constraints {
0888 unsigned int maxlen;
0889 };
0890
0891
0892
0893
0894
0895
0896
0897
0898
0899 struct nand_op_parser_pattern_elem {
0900 enum nand_op_instr_type type;
0901 bool optional;
0902 union {
0903 struct nand_op_parser_addr_constraints addr;
0904 struct nand_op_parser_data_constraints data;
0905 } ctx;
0906 };
0907
0908 #define NAND_OP_PARSER_PAT_CMD_ELEM(_opt) \
0909 { \
0910 .type = NAND_OP_CMD_INSTR, \
0911 .optional = _opt, \
0912 }
0913
0914 #define NAND_OP_PARSER_PAT_ADDR_ELEM(_opt, _maxcycles) \
0915 { \
0916 .type = NAND_OP_ADDR_INSTR, \
0917 .optional = _opt, \
0918 .ctx.addr.maxcycles = _maxcycles, \
0919 }
0920
0921 #define NAND_OP_PARSER_PAT_DATA_IN_ELEM(_opt, _maxlen) \
0922 { \
0923 .type = NAND_OP_DATA_IN_INSTR, \
0924 .optional = _opt, \
0925 .ctx.data.maxlen = _maxlen, \
0926 }
0927
0928 #define NAND_OP_PARSER_PAT_DATA_OUT_ELEM(_opt, _maxlen) \
0929 { \
0930 .type = NAND_OP_DATA_OUT_INSTR, \
0931 .optional = _opt, \
0932 .ctx.data.maxlen = _maxlen, \
0933 }
0934
0935 #define NAND_OP_PARSER_PAT_WAITRDY_ELEM(_opt) \
0936 { \
0937 .type = NAND_OP_WAITRDY_INSTR, \
0938 .optional = _opt, \
0939 }
0940
0941
0942
0943
0944
0945
0946
0947
0948
0949
0950
0951
0952
0953
0954
0955
0956
0957
0958
0959 struct nand_op_parser_pattern {
0960 const struct nand_op_parser_pattern_elem *elems;
0961 unsigned int nelems;
0962 int (*exec)(struct nand_chip *chip, const struct nand_subop *subop);
0963 };
0964
0965 #define NAND_OP_PARSER_PATTERN(_exec, ...) \
0966 { \
0967 .exec = _exec, \
0968 .elems = (const struct nand_op_parser_pattern_elem[]) { __VA_ARGS__ }, \
0969 .nelems = sizeof((struct nand_op_parser_pattern_elem[]) { __VA_ARGS__ }) / \
0970 sizeof(struct nand_op_parser_pattern_elem), \
0971 }
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987 struct nand_op_parser {
0988 const struct nand_op_parser_pattern *patterns;
0989 unsigned int npatterns;
0990 };
0991
0992 #define NAND_OP_PARSER(...) \
0993 { \
0994 .patterns = (const struct nand_op_parser_pattern[]) { __VA_ARGS__ }, \
0995 .npatterns = sizeof((struct nand_op_parser_pattern[]) { __VA_ARGS__ }) / \
0996 sizeof(struct nand_op_parser_pattern), \
0997 }
0998
0999
1000
1001
1002
1003
1004
1005
1006
1007 struct nand_operation {
1008 unsigned int cs;
1009 const struct nand_op_instr *instrs;
1010 unsigned int ninstrs;
1011 };
1012
1013 #define NAND_OPERATION(_cs, _instrs) \
1014 { \
1015 .cs = _cs, \
1016 .instrs = _instrs, \
1017 .ninstrs = ARRAY_SIZE(_instrs), \
1018 }
1019
1020 int nand_op_parser_exec_op(struct nand_chip *chip,
1021 const struct nand_op_parser *parser,
1022 const struct nand_operation *op, bool check_only);
1023
1024 static inline void nand_op_trace(const char *prefix,
1025 const struct nand_op_instr *instr)
1026 {
1027 #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
1028 switch (instr->type) {
1029 case NAND_OP_CMD_INSTR:
1030 pr_debug("%sCMD [0x%02x]\n", prefix,
1031 instr->ctx.cmd.opcode);
1032 break;
1033 case NAND_OP_ADDR_INSTR:
1034 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
1035 instr->ctx.addr.naddrs,
1036 instr->ctx.addr.naddrs < 64 ?
1037 instr->ctx.addr.naddrs : 64,
1038 instr->ctx.addr.addrs);
1039 break;
1040 case NAND_OP_DATA_IN_INSTR:
1041 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
1042 instr->ctx.data.len,
1043 instr->ctx.data.force_8bit ?
1044 ", force 8-bit" : "");
1045 break;
1046 case NAND_OP_DATA_OUT_INSTR:
1047 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
1048 instr->ctx.data.len,
1049 instr->ctx.data.force_8bit ?
1050 ", force 8-bit" : "");
1051 break;
1052 case NAND_OP_WAITRDY_INSTR:
1053 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
1054 instr->ctx.waitrdy.timeout_ms);
1055 break;
1056 }
1057 #endif
1058 }
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082 struct nand_controller_ops {
1083 int (*attach_chip)(struct nand_chip *chip);
1084 void (*detach_chip)(struct nand_chip *chip);
1085 int (*exec_op)(struct nand_chip *chip,
1086 const struct nand_operation *op,
1087 bool check_only);
1088 int (*setup_interface)(struct nand_chip *chip, int chipnr,
1089 const struct nand_interface_config *conf);
1090 };
1091
1092
1093
1094
1095
1096
1097
1098 struct nand_controller {
1099 struct mutex lock;
1100 const struct nand_controller_ops *ops;
1101 };
1102
1103 static inline void nand_controller_init(struct nand_controller *nfc)
1104 {
1105 mutex_init(&nfc->lock);
1106 }
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136 struct nand_legacy {
1137 void __iomem *IO_ADDR_R;
1138 void __iomem *IO_ADDR_W;
1139 void (*select_chip)(struct nand_chip *chip, int cs);
1140 u8 (*read_byte)(struct nand_chip *chip);
1141 void (*write_byte)(struct nand_chip *chip, u8 byte);
1142 void (*write_buf)(struct nand_chip *chip, const u8 *buf, int len);
1143 void (*read_buf)(struct nand_chip *chip, u8 *buf, int len);
1144 void (*cmd_ctrl)(struct nand_chip *chip, int dat, unsigned int ctrl);
1145 void (*cmdfunc)(struct nand_chip *chip, unsigned command, int column,
1146 int page_addr);
1147 int (*dev_ready)(struct nand_chip *chip);
1148 int (*waitfunc)(struct nand_chip *chip);
1149 int (*block_bad)(struct nand_chip *chip, loff_t ofs);
1150 int (*block_markbad)(struct nand_chip *chip, loff_t ofs);
1151 int (*set_features)(struct nand_chip *chip, int feature_addr,
1152 u8 *subfeature_para);
1153 int (*get_features)(struct nand_chip *chip, int feature_addr,
1154 u8 *subfeature_para);
1155 int chip_delay;
1156 struct nand_controller dummy_controller;
1157 };
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168 struct nand_chip_ops {
1169 int (*suspend)(struct nand_chip *chip);
1170 void (*resume)(struct nand_chip *chip);
1171 int (*lock_area)(struct nand_chip *chip, loff_t ofs, uint64_t len);
1172 int (*unlock_area)(struct nand_chip *chip, loff_t ofs, uint64_t len);
1173 int (*setup_read_retry)(struct nand_chip *chip, int retry_mode);
1174 int (*choose_interface_config)(struct nand_chip *chip,
1175 struct nand_interface_config *iface);
1176 };
1177
1178
1179
1180
1181
1182
1183 struct nand_manufacturer {
1184 const struct nand_manufacturer_desc *desc;
1185 void *priv;
1186 };
1187
1188
1189
1190
1191
1192
1193 struct nand_secure_region {
1194 u64 offset;
1195 u64 size;
1196 };
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256 struct nand_chip {
1257 struct nand_device base;
1258 struct nand_id id;
1259 struct nand_parameters parameters;
1260 struct nand_manufacturer manufacturer;
1261 struct nand_chip_ops ops;
1262 struct nand_legacy legacy;
1263 unsigned int options;
1264
1265
1266 const struct nand_interface_config *current_interface_config;
1267 struct nand_interface_config *best_interface_config;
1268
1269
1270 unsigned int bbt_erase_shift;
1271 unsigned int bbt_options;
1272 unsigned int badblockpos;
1273 unsigned int badblockbits;
1274 struct nand_bbt_descr *bbt_td;
1275 struct nand_bbt_descr *bbt_md;
1276 struct nand_bbt_descr *badblock_pattern;
1277 u8 *bbt;
1278
1279
1280 unsigned int page_shift;
1281 unsigned int phys_erase_shift;
1282 unsigned int chip_shift;
1283 unsigned int pagemask;
1284 unsigned int subpagesize;
1285
1286
1287 u8 *data_buf;
1288 u8 *oob_poi;
1289 struct {
1290 unsigned int bitflips;
1291 int page;
1292 } pagecache;
1293 unsigned long buf_align;
1294
1295
1296 struct mutex lock;
1297 unsigned int suspended : 1;
1298 wait_queue_head_t resume_wq;
1299 int cur_cs;
1300 int read_retries;
1301 struct nand_secure_region *secure_regions;
1302 u8 nr_secure_regions;
1303
1304
1305 struct nand_controller *controller;
1306 struct nand_ecc_ctrl ecc;
1307 void *priv;
1308 };
1309
1310 static inline struct nand_chip *mtd_to_nand(struct mtd_info *mtd)
1311 {
1312 return container_of(mtd, struct nand_chip, base.mtd);
1313 }
1314
1315 static inline struct mtd_info *nand_to_mtd(struct nand_chip *chip)
1316 {
1317 return &chip->base.mtd;
1318 }
1319
1320 static inline void *nand_get_controller_data(struct nand_chip *chip)
1321 {
1322 return chip->priv;
1323 }
1324
1325 static inline void nand_set_controller_data(struct nand_chip *chip, void *priv)
1326 {
1327 chip->priv = priv;
1328 }
1329
1330 static inline void nand_set_manufacturer_data(struct nand_chip *chip,
1331 void *priv)
1332 {
1333 chip->manufacturer.priv = priv;
1334 }
1335
1336 static inline void *nand_get_manufacturer_data(struct nand_chip *chip)
1337 {
1338 return chip->manufacturer.priv;
1339 }
1340
1341 static inline void nand_set_flash_node(struct nand_chip *chip,
1342 struct device_node *np)
1343 {
1344 mtd_set_of_node(nand_to_mtd(chip), np);
1345 }
1346
1347 static inline struct device_node *nand_get_flash_node(struct nand_chip *chip)
1348 {
1349 return mtd_get_of_node(nand_to_mtd(chip));
1350 }
1351
1352
1353
1354
1355
1356
1357 static inline const struct nand_interface_config *
1358 nand_get_interface_config(struct nand_chip *chip)
1359 {
1360 return chip->current_interface_config;
1361 }
1362
1363
1364
1365
1366
1367
1368 #define LEGACY_ID_NAND(nm, devid, chipsz, erasesz, opts) \
1369 { .name = (nm), {{ .dev_id = (devid) }}, .pagesize = 512, \
1370 .chipsize = (chipsz), .erasesize = (erasesz), .options = (opts) }
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382 #define EXTENDED_ID_NAND(nm, devid, chipsz, opts) \
1383 { .name = (nm), {{ .dev_id = (devid) }}, .chipsize = (chipsz), \
1384 .options = (opts) }
1385
1386 #define NAND_ECC_INFO(_strength, _step) \
1387 { .strength_ds = (_strength), .step_ds = (_step) }
1388 #define NAND_ECC_STRENGTH(type) ((type)->ecc.strength_ds)
1389 #define NAND_ECC_STEP(type) ((type)->ecc.step_ds)
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416 struct nand_flash_dev {
1417 char *name;
1418 union {
1419 struct {
1420 uint8_t mfr_id;
1421 uint8_t dev_id;
1422 };
1423 uint8_t id[NAND_MAX_ID_LEN];
1424 };
1425 unsigned int pagesize;
1426 unsigned int chipsize;
1427 unsigned int erasesize;
1428 unsigned int options;
1429 uint16_t id_len;
1430 uint16_t oobsize;
1431 struct {
1432 uint16_t strength_ds;
1433 uint16_t step_ds;
1434 } ecc;
1435 };
1436
1437 int nand_create_bbt(struct nand_chip *chip);
1438
1439
1440
1441
1442
1443
1444 static inline bool nand_is_slc(struct nand_chip *chip)
1445 {
1446 WARN(nanddev_bits_per_cell(&chip->base) == 0,
1447 "chip->bits_per_cell is used uninitialized\n");
1448 return nanddev_bits_per_cell(&chip->base) == 1;
1449 }
1450
1451
1452
1453
1454
1455
1456 static inline int nand_opcode_8bits(unsigned int command)
1457 {
1458 switch (command) {
1459 case NAND_CMD_READID:
1460 case NAND_CMD_PARAM:
1461 case NAND_CMD_GET_FEATURES:
1462 case NAND_CMD_SET_FEATURES:
1463 return 1;
1464 default:
1465 break;
1466 }
1467 return 0;
1468 }
1469
1470 int rawnand_sw_hamming_init(struct nand_chip *chip);
1471 int rawnand_sw_hamming_calculate(struct nand_chip *chip,
1472 const unsigned char *buf,
1473 unsigned char *code);
1474 int rawnand_sw_hamming_correct(struct nand_chip *chip,
1475 unsigned char *buf,
1476 unsigned char *read_ecc,
1477 unsigned char *calc_ecc);
1478 void rawnand_sw_hamming_cleanup(struct nand_chip *chip);
1479 int rawnand_sw_bch_init(struct nand_chip *chip);
1480 int rawnand_sw_bch_correct(struct nand_chip *chip, unsigned char *buf,
1481 unsigned char *read_ecc, unsigned char *calc_ecc);
1482 void rawnand_sw_bch_cleanup(struct nand_chip *chip);
1483
1484 int nand_check_erased_ecc_chunk(void *data, int datalen,
1485 void *ecc, int ecclen,
1486 void *extraoob, int extraooblen,
1487 int threshold);
1488
1489 int nand_ecc_choose_conf(struct nand_chip *chip,
1490 const struct nand_ecc_caps *caps, int oobavail);
1491
1492
1493 int nand_write_oob_std(struct nand_chip *chip, int page);
1494
1495
1496 int nand_read_oob_std(struct nand_chip *chip, int page);
1497
1498
1499 int nand_get_set_features_notsupp(struct nand_chip *chip, int addr,
1500 u8 *subfeature_param);
1501
1502
1503 int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
1504 int page);
1505 int nand_monolithic_read_page_raw(struct nand_chip *chip, uint8_t *buf,
1506 int oob_required, int page);
1507
1508
1509 int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
1510 int oob_required, int page);
1511 int nand_monolithic_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
1512 int oob_required, int page);
1513
1514
1515 int nand_reset(struct nand_chip *chip, int chipnr);
1516
1517
1518 int nand_reset_op(struct nand_chip *chip);
1519 int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1520 unsigned int len);
1521 int nand_status_op(struct nand_chip *chip, u8 *status);
1522 int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock);
1523 int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1524 unsigned int offset_in_page, void *buf, unsigned int len);
1525 int nand_change_read_column_op(struct nand_chip *chip,
1526 unsigned int offset_in_page, void *buf,
1527 unsigned int len, bool force_8bit);
1528 int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1529 unsigned int offset_in_page, void *buf, unsigned int len);
1530 int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1531 unsigned int offset_in_page, const void *buf,
1532 unsigned int len);
1533 int nand_prog_page_end_op(struct nand_chip *chip);
1534 int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1535 unsigned int offset_in_page, const void *buf,
1536 unsigned int len);
1537 int nand_change_write_column_op(struct nand_chip *chip,
1538 unsigned int offset_in_page, const void *buf,
1539 unsigned int len, bool force_8bit);
1540 int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1541 bool force_8bit, bool check_only);
1542 int nand_write_data_op(struct nand_chip *chip, const void *buf,
1543 unsigned int len, bool force_8bit);
1544 int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
1545 int oob_required, int page);
1546
1547
1548 int nand_scan_with_ids(struct nand_chip *chip, unsigned int max_chips,
1549 struct nand_flash_dev *ids);
1550
1551 static inline int nand_scan(struct nand_chip *chip, unsigned int max_chips)
1552 {
1553 return nand_scan_with_ids(chip, max_chips, NULL);
1554 }
1555
1556
1557 void nand_wait_ready(struct nand_chip *chip);
1558
1559
1560
1561
1562
1563 void nand_cleanup(struct nand_chip *chip);
1564
1565
1566
1567
1568
1569 int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms);
1570 int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod,
1571 unsigned long timeout_ms);
1572
1573
1574 void nand_select_target(struct nand_chip *chip, unsigned int cs);
1575 void nand_deselect_target(struct nand_chip *chip);
1576
1577
1578 void nand_extract_bits(u8 *dst, unsigned int dst_off, const u8 *src,
1579 unsigned int src_off, unsigned int nbits);
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595 static inline void *nand_get_data_buf(struct nand_chip *chip)
1596 {
1597 chip->pagecache.page = -1;
1598
1599 return chip->data_buf;
1600 }
1601
1602
1603 int rawnand_dt_parse_gpio_cs(struct device *dev, struct gpio_desc ***cs_array,
1604 unsigned int *ncs_array);
1605
1606 #endif