base-files: define yes/no as valid boolean options
[openwrt.git] / target / linux / mvebu / patches-3.10 / 0094-mtd-get-the-ECC-info-from-the-Extended-Parameter-Pag.patch
1 From f617846b1e20a2ba59cdd9435715339eaed1e251 Mon Sep 17 00:00:00 2001
2 From: Huang Shijie <b32955@freescale.com>
3 Date: Wed, 22 May 2013 10:28:27 +0800
4 Subject: [PATCH 094/203] mtd: get the ECC info from the Extended Parameter
5  Page
6
7 Since the ONFI 2.1, the onfi spec adds the Extended Parameter Page
8 to store the ECC info.
9
10 The onfi spec tells us that if the nand chip's recommended ECC codeword
11 size is not 512 bytes, then the @ecc_bits is 0xff. The host _SHOULD_ then
12 read the Extended ECC information that is part of the extended parameter
13 page to retrieve the ECC requirements for this device.
14
15 This patch implement the reading of the Extended Parameter Page, and parses
16 the sections for ECC type, and get the ECC info from the ECC section.
17
18 Tested this patch with Micron MT29F64G08CBABAWP.
19
20 Acked-by: Pekon Gupta <pekon@ti.com>
21 Signed-off-by: Huang Shijie <b32955@freescale.com>
22 Reviewed-and-tested-by: Brian Norris <computersforpeace@gmail.com>
23 Signed-off-by: Brian Norris <computersforpeace@gmail.com>
24 Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
25 ---
26  drivers/mtd/nand/nand_base.c | 87 ++++++++++++++++++++++++++++++++++++++++++++
27  1 file changed, 87 insertions(+)
28
29 --- a/drivers/mtd/nand/nand_base.c
30 +++ b/drivers/mtd/nand/nand_base.c
31 @@ -2848,6 +2848,78 @@ static u16 onfi_crc16(u16 crc, u8 const
32         return crc;
33  }
34  
35 +/* Parse the Extended Parameter Page. */
36 +static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
37 +               struct nand_chip *chip, struct nand_onfi_params *p)
38 +{
39 +       struct onfi_ext_param_page *ep;
40 +       struct onfi_ext_section *s;
41 +       struct onfi_ext_ecc_info *ecc;
42 +       uint8_t *cursor;
43 +       int ret = -EINVAL;
44 +       int len;
45 +       int i;
46 +
47 +       len = le16_to_cpu(p->ext_param_page_length) * 16;
48 +       ep = kmalloc(len, GFP_KERNEL);
49 +       if (!ep) {
50 +               ret = -ENOMEM;
51 +               goto ext_out;
52 +       }
53 +
54 +       /* Send our own NAND_CMD_PARAM. */
55 +       chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
56 +
57 +       /* Use the Change Read Column command to skip the ONFI param pages. */
58 +       chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
59 +                       sizeof(*p) * p->num_of_param_pages , -1);
60 +
61 +       /* Read out the Extended Parameter Page. */
62 +       chip->read_buf(mtd, (uint8_t *)ep, len);
63 +       if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
64 +               != le16_to_cpu(ep->crc))) {
65 +               pr_debug("fail in the CRC.\n");
66 +               goto ext_out;
67 +       }
68 +
69 +       /*
70 +        * Check the signature.
71 +        * Do not strictly follow the ONFI spec, maybe changed in future.
72 +        */
73 +       if (strncmp(ep->sig, "EPPS", 4)) {
74 +               pr_debug("The signature is invalid.\n");
75 +               goto ext_out;
76 +       }
77 +
78 +       /* find the ECC section. */
79 +       cursor = (uint8_t *)(ep + 1);
80 +       for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
81 +               s = ep->sections + i;
82 +               if (s->type == ONFI_SECTION_TYPE_2)
83 +                       break;
84 +               cursor += s->length * 16;
85 +       }
86 +       if (i == ONFI_EXT_SECTION_MAX) {
87 +               pr_debug("We can not find the ECC section.\n");
88 +               goto ext_out;
89 +       }
90 +
91 +       /* get the info we want. */
92 +       ecc = (struct onfi_ext_ecc_info *)cursor;
93 +
94 +       if (ecc->codeword_size) {
95 +               chip->ecc_strength_ds = ecc->ecc_bits;
96 +               chip->ecc_step_ds = 1 << ecc->codeword_size;
97 +       }
98 +
99 +       pr_info("ONFI extended param page detected.\n");
100 +       return 0;
101 +
102 +ext_out:
103 +       kfree(ep);
104 +       return ret;
105 +}
106 +
107  /*
108   * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
109   */
110 @@ -2927,6 +2999,21 @@ static int nand_flash_detect_onfi(struct
111         if (p->ecc_bits != 0xff) {
112                 chip->ecc_strength_ds = p->ecc_bits;
113                 chip->ecc_step_ds = 512;
114 +       } else if (chip->onfi_version >= 21 &&
115 +               (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
116 +
117 +               /*
118 +                * The nand_flash_detect_ext_param_page() uses the
119 +                * Change Read Column command which maybe not supported
120 +                * by the chip->cmdfunc. So try to update the chip->cmdfunc
121 +                * now. We do not replace user supplied command function.
122 +                */
123 +               if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
124 +                       chip->cmdfunc = nand_command_lp;
125 +
126 +               /* The Extended Parameter Page is supported since ONFI 2.1. */
127 +               if (nand_flash_detect_ext_param_page(mtd, chip, p))
128 +                       pr_info("Failed to detect the extended param page.\n");
129         }
130  
131         pr_info("ONFI flash detected\n");