| Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Free Electrons |
| 3 | * Copyright (C) 2017 NextThing Co |
| 4 | * |
| 5 | * Author: Boris Brezillon <boris.brezillon@free-electrons.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| Boris Brezillon | d4092d7 | 2017-08-04 17:29:10 +0200 | [diff] [blame] | 18 | #include <linux/mtd/rawnand.h> |
| Boris Brezillon | 78f3482 | 2016-05-27 14:36:36 +0200 | [diff] [blame] | 19 | #include <linux/sizes.h> |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | |
| 22 | #define NAND_HYNIX_CMD_SET_PARAMS 0x36 |
| 23 | #define NAND_HYNIX_CMD_APPLY_PARAMS 0x16 |
| 24 | |
| 25 | #define NAND_HYNIX_1XNM_RR_REPEAT 8 |
| 26 | |
| 27 | /** |
| 28 | * struct hynix_read_retry - read-retry data |
| 29 | * @nregs: number of register to set when applying a new read-retry mode |
| 30 | * @regs: register offsets (NAND chip dependent) |
| 31 | * @values: array of values to set in registers. The array size is equal to |
| 32 | * (nregs * nmodes) |
| 33 | */ |
| 34 | struct hynix_read_retry { |
| 35 | int nregs; |
| 36 | const u8 *regs; |
| 37 | u8 values[0]; |
| 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * struct hynix_nand - private Hynix NAND struct |
| 42 | * @nand_technology: manufacturing process expressed in picometer |
| 43 | * @read_retry: read-retry information |
| 44 | */ |
| 45 | struct hynix_nand { |
| 46 | const struct hynix_read_retry *read_retry; |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * struct hynix_read_retry_otp - structure describing how the read-retry OTP |
| 51 | * area |
| 52 | * @nregs: number of hynix private registers to set before reading the reading |
| 53 | * the OTP area |
| 54 | * @regs: registers that should be configured |
| 55 | * @values: values that should be set in regs |
| 56 | * @page: the address to pass to the READ_PAGE command. Depends on the NAND |
| 57 | * chip |
| 58 | * @size: size of the read-retry OTP section |
| 59 | */ |
| 60 | struct hynix_read_retry_otp { |
| 61 | int nregs; |
| 62 | const u8 *regs; |
| 63 | const u8 *values; |
| 64 | int page; |
| 65 | int size; |
| 66 | }; |
| Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 67 | |
| Boris Brezillon | 78f3482 | 2016-05-27 14:36:36 +0200 | [diff] [blame] | 68 | static bool hynix_nand_has_valid_jedecid(struct nand_chip *chip) |
| Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 69 | { |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 70 | u8 jedecid[5] = { }; |
| 71 | int ret; |
| 72 | |
| 73 | ret = nand_readid_op(chip, 0x40, jedecid, sizeof(jedecid)); |
| 74 | if (ret) |
| 75 | return false; |
| 76 | |
| 77 | return !strncmp("JEDEC", jedecid, sizeof(jedecid)); |
| 78 | } |
| 79 | |
| 80 | static int hynix_nand_cmd_op(struct nand_chip *chip, u8 cmd) |
| 81 | { |
| Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 82 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 83 | |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 84 | chip->cmdfunc(mtd, cmd, -1, -1); |
| Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 85 | |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int hynix_nand_reg_write_op(struct nand_chip *chip, u8 addr, u8 val) |
| 90 | { |
| 91 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 92 | u16 column = ((u16)addr << 8) | addr; |
| 93 | |
| 94 | chip->cmdfunc(mtd, NAND_CMD_NONE, column, -1); |
| 95 | chip->write_byte(mtd, val); |
| 96 | |
| 97 | return 0; |
| Boris Brezillon | 78f3482 | 2016-05-27 14:36:36 +0200 | [diff] [blame] | 98 | } |
| Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 99 | |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 100 | static int hynix_nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) |
| 101 | { |
| 102 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 103 | struct hynix_nand *hynix = nand_get_manufacturer_data(chip); |
| 104 | const u8 *values; |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 105 | int i, ret; |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 106 | |
| 107 | values = hynix->read_retry->values + |
| 108 | (retry_mode * hynix->read_retry->nregs); |
| 109 | |
| 110 | /* Enter 'Set Hynix Parameters' mode */ |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 111 | ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_SET_PARAMS); |
| 112 | if (ret) |
| 113 | return ret; |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * Configure the NAND in the requested read-retry mode. |
| 117 | * This is done by setting pre-defined values in internal NAND |
| 118 | * registers. |
| 119 | * |
| 120 | * The set of registers is NAND specific, and the values are either |
| 121 | * predefined or extracted from an OTP area on the NAND (values are |
| 122 | * probably tweaked at production in this case). |
| 123 | */ |
| 124 | for (i = 0; i < hynix->read_retry->nregs; i++) { |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 125 | ret = hynix_nand_reg_write_op(chip, hynix->read_retry->regs[i], |
| 126 | values[i]); |
| 127 | if (ret) |
| 128 | return ret; |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* Apply the new settings. */ |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 132 | return hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_APPLY_PARAMS); |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
| 136 | * hynix_get_majority - get the value that is occurring the most in a given |
| 137 | * set of values |
| 138 | * @in: the array of values to test |
| 139 | * @repeat: the size of the in array |
| 140 | * @out: pointer used to store the output value |
| 141 | * |
| 142 | * This function implements the 'majority check' logic that is supposed to |
| 143 | * overcome the unreliability of MLC NANDs when reading the OTP area storing |
| 144 | * the read-retry parameters. |
| 145 | * |
| 146 | * It's based on a pretty simple assumption: if we repeat the same value |
| 147 | * several times and then take the one that is occurring the most, we should |
| 148 | * find the correct value. |
| 149 | * Let's hope this dummy algorithm prevents us from losing the read-retry |
| 150 | * parameters. |
| 151 | */ |
| 152 | static int hynix_get_majority(const u8 *in, int repeat, u8 *out) |
| 153 | { |
| 154 | int i, j, half = repeat / 2; |
| 155 | |
| 156 | /* |
| 157 | * We only test the first half of the in array because we must ensure |
| 158 | * that the value is at least occurring repeat / 2 times. |
| 159 | * |
| 160 | * This loop is suboptimal since we may count the occurrences of the |
| 161 | * same value several time, but we are doing that on small sets, which |
| 162 | * makes it acceptable. |
| 163 | */ |
| 164 | for (i = 0; i < half; i++) { |
| 165 | int cnt = 0; |
| 166 | u8 val = in[i]; |
| 167 | |
| 168 | /* Count all values that are matching the one at index i. */ |
| 169 | for (j = i + 1; j < repeat; j++) { |
| 170 | if (in[j] == val) |
| 171 | cnt++; |
| 172 | } |
| 173 | |
| 174 | /* We found a value occurring more than repeat / 2. */ |
| 175 | if (cnt > half) { |
| 176 | *out = val; |
| 177 | return 0; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return -EIO; |
| 182 | } |
| 183 | |
| 184 | static int hynix_read_rr_otp(struct nand_chip *chip, |
| 185 | const struct hynix_read_retry_otp *info, |
| 186 | void *buf) |
| 187 | { |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 188 | int i, ret; |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 189 | |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 190 | ret = nand_reset_op(chip); |
| 191 | if (ret) |
| 192 | return ret; |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 193 | |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 194 | ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_SET_PARAMS); |
| 195 | if (ret) |
| 196 | return ret; |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 197 | |
| 198 | for (i = 0; i < info->nregs; i++) { |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 199 | ret = hynix_nand_reg_write_op(chip, info->regs[i], |
| 200 | info->values[i]); |
| 201 | if (ret) |
| 202 | return ret; |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 205 | ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_APPLY_PARAMS); |
| 206 | if (ret) |
| 207 | return ret; |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 208 | |
| 209 | /* Sequence to enter OTP mode? */ |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 210 | ret = hynix_nand_cmd_op(chip, 0x17); |
| 211 | if (ret) |
| 212 | return ret; |
| 213 | |
| 214 | ret = hynix_nand_cmd_op(chip, 0x4); |
| 215 | if (ret) |
| 216 | return ret; |
| 217 | |
| 218 | ret = hynix_nand_cmd_op(chip, 0x19); |
| 219 | if (ret) |
| 220 | return ret; |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 221 | |
| 222 | /* Now read the page */ |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 223 | ret = nand_read_page_op(chip, info->page, 0, buf, info->size); |
| 224 | if (ret) |
| 225 | return ret; |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 226 | |
| 227 | /* Put everything back to normal */ |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 228 | ret = nand_reset_op(chip); |
| 229 | if (ret) |
| 230 | return ret; |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 231 | |
| Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame^] | 232 | ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_SET_PARAMS); |
| 233 | if (ret) |
| 234 | return ret; |
| 235 | |
| 236 | ret = hynix_nand_reg_write_op(chip, 0x38, 0); |
| 237 | if (ret) |
| 238 | return ret; |
| 239 | |
| 240 | ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_APPLY_PARAMS); |
| 241 | if (ret) |
| 242 | return ret; |
| 243 | |
| 244 | return nand_read_page_op(chip, 0, 0, NULL, 0); |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | #define NAND_HYNIX_1XNM_RR_COUNT_OFFS 0 |
| 248 | #define NAND_HYNIX_1XNM_RR_REG_COUNT_OFFS 8 |
| 249 | #define NAND_HYNIX_1XNM_RR_SET_OFFS(x, setsize, inv) \ |
| 250 | (16 + ((((x) * 2) + ((inv) ? 1 : 0)) * (setsize))) |
| 251 | |
| 252 | static int hynix_mlc_1xnm_rr_value(const u8 *buf, int nmodes, int nregs, |
| 253 | int mode, int reg, bool inv, u8 *val) |
| 254 | { |
| 255 | u8 tmp[NAND_HYNIX_1XNM_RR_REPEAT]; |
| 256 | int val_offs = (mode * nregs) + reg; |
| 257 | int set_size = nmodes * nregs; |
| 258 | int i, ret; |
| 259 | |
| 260 | for (i = 0; i < NAND_HYNIX_1XNM_RR_REPEAT; i++) { |
| 261 | int set_offs = NAND_HYNIX_1XNM_RR_SET_OFFS(i, set_size, inv); |
| 262 | |
| 263 | tmp[i] = buf[val_offs + set_offs]; |
| 264 | } |
| 265 | |
| 266 | ret = hynix_get_majority(tmp, NAND_HYNIX_1XNM_RR_REPEAT, val); |
| 267 | if (ret) |
| 268 | return ret; |
| 269 | |
| 270 | if (inv) |
| 271 | *val = ~*val; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static u8 hynix_1xnm_mlc_read_retry_regs[] = { |
| 277 | 0xcc, 0xbf, 0xaa, 0xab, 0xcd, 0xad, 0xae, 0xaf |
| 278 | }; |
| 279 | |
| 280 | static int hynix_mlc_1xnm_rr_init(struct nand_chip *chip, |
| 281 | const struct hynix_read_retry_otp *info) |
| 282 | { |
| 283 | struct hynix_nand *hynix = nand_get_manufacturer_data(chip); |
| 284 | struct hynix_read_retry *rr = NULL; |
| 285 | int ret, i, j; |
| 286 | u8 nregs, nmodes; |
| 287 | u8 *buf; |
| 288 | |
| 289 | buf = kmalloc(info->size, GFP_KERNEL); |
| 290 | if (!buf) |
| 291 | return -ENOMEM; |
| 292 | |
| 293 | ret = hynix_read_rr_otp(chip, info, buf); |
| 294 | if (ret) |
| 295 | goto out; |
| 296 | |
| 297 | ret = hynix_get_majority(buf, NAND_HYNIX_1XNM_RR_REPEAT, |
| 298 | &nmodes); |
| 299 | if (ret) |
| 300 | goto out; |
| 301 | |
| 302 | ret = hynix_get_majority(buf + NAND_HYNIX_1XNM_RR_REPEAT, |
| 303 | NAND_HYNIX_1XNM_RR_REPEAT, |
| 304 | &nregs); |
| 305 | if (ret) |
| 306 | goto out; |
| 307 | |
| 308 | rr = kzalloc(sizeof(*rr) + (nregs * nmodes), GFP_KERNEL); |
| Dan Carpenter | 4ca8c1d | 2017-03-22 12:01:45 +0300 | [diff] [blame] | 309 | if (!rr) { |
| 310 | ret = -ENOMEM; |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 311 | goto out; |
| Dan Carpenter | 4ca8c1d | 2017-03-22 12:01:45 +0300 | [diff] [blame] | 312 | } |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 313 | |
| 314 | for (i = 0; i < nmodes; i++) { |
| 315 | for (j = 0; j < nregs; j++) { |
| 316 | u8 *val = rr->values + (i * nregs); |
| 317 | |
| 318 | ret = hynix_mlc_1xnm_rr_value(buf, nmodes, nregs, i, j, |
| 319 | false, val); |
| 320 | if (!ret) |
| 321 | continue; |
| 322 | |
| 323 | ret = hynix_mlc_1xnm_rr_value(buf, nmodes, nregs, i, j, |
| 324 | true, val); |
| 325 | if (ret) |
| 326 | goto out; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | rr->nregs = nregs; |
| 331 | rr->regs = hynix_1xnm_mlc_read_retry_regs; |
| 332 | hynix->read_retry = rr; |
| 333 | chip->setup_read_retry = hynix_nand_setup_read_retry; |
| 334 | chip->read_retries = nmodes; |
| 335 | |
| 336 | out: |
| 337 | kfree(buf); |
| 338 | |
| 339 | if (ret) |
| 340 | kfree(rr); |
| 341 | |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | static const u8 hynix_mlc_1xnm_rr_otp_regs[] = { 0x38 }; |
| 346 | static const u8 hynix_mlc_1xnm_rr_otp_values[] = { 0x52 }; |
| 347 | |
| 348 | static const struct hynix_read_retry_otp hynix_mlc_1xnm_rr_otps[] = { |
| 349 | { |
| 350 | .nregs = ARRAY_SIZE(hynix_mlc_1xnm_rr_otp_regs), |
| 351 | .regs = hynix_mlc_1xnm_rr_otp_regs, |
| 352 | .values = hynix_mlc_1xnm_rr_otp_values, |
| 353 | .page = 0x21f, |
| 354 | .size = 784 |
| 355 | }, |
| 356 | { |
| 357 | .nregs = ARRAY_SIZE(hynix_mlc_1xnm_rr_otp_regs), |
| 358 | .regs = hynix_mlc_1xnm_rr_otp_regs, |
| 359 | .values = hynix_mlc_1xnm_rr_otp_values, |
| 360 | .page = 0x200, |
| 361 | .size = 528, |
| 362 | }, |
| 363 | }; |
| 364 | |
| 365 | static int hynix_nand_rr_init(struct nand_chip *chip) |
| 366 | { |
| 367 | int i, ret = 0; |
| 368 | bool valid_jedecid; |
| 369 | |
| 370 | valid_jedecid = hynix_nand_has_valid_jedecid(chip); |
| 371 | |
| 372 | /* |
| 373 | * We only support read-retry for 1xnm NANDs, and those NANDs all |
| 374 | * expose a valid JEDEC ID. |
| 375 | */ |
| 376 | if (valid_jedecid) { |
| 377 | u8 nand_tech = chip->id.data[5] >> 4; |
| 378 | |
| 379 | /* 1xnm technology */ |
| 380 | if (nand_tech == 4) { |
| 381 | for (i = 0; i < ARRAY_SIZE(hynix_mlc_1xnm_rr_otps); |
| 382 | i++) { |
| 383 | /* |
| 384 | * FIXME: Hynix recommend to copy the |
| 385 | * read-retry OTP area into a normal page. |
| 386 | */ |
| 387 | ret = hynix_mlc_1xnm_rr_init(chip, |
| 388 | hynix_mlc_1xnm_rr_otps); |
| 389 | if (!ret) |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | if (ret) |
| 396 | pr_warn("failed to initialize read-retry infrastructure"); |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| Boris Brezillon | 78f3482 | 2016-05-27 14:36:36 +0200 | [diff] [blame] | 401 | static void hynix_nand_extract_oobsize(struct nand_chip *chip, |
| 402 | bool valid_jedecid) |
| 403 | { |
| 404 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 405 | u8 oobsize; |
| 406 | |
| 407 | oobsize = ((chip->id.data[3] >> 2) & 0x3) | |
| 408 | ((chip->id.data[3] >> 4) & 0x4); |
| 409 | |
| 410 | if (valid_jedecid) { |
| 411 | switch (oobsize) { |
| 412 | case 0: |
| 413 | mtd->oobsize = 2048; |
| 414 | break; |
| 415 | case 1: |
| 416 | mtd->oobsize = 1664; |
| 417 | break; |
| 418 | case 2: |
| 419 | mtd->oobsize = 1024; |
| 420 | break; |
| 421 | case 3: |
| 422 | mtd->oobsize = 640; |
| 423 | break; |
| 424 | default: |
| 425 | /* |
| 426 | * We should never reach this case, but if that |
| 427 | * happens, this probably means Hynix decided to use |
| 428 | * a different extended ID format, and we should find |
| 429 | * a way to support it. |
| 430 | */ |
| 431 | WARN(1, "Invalid OOB size"); |
| 432 | break; |
| 433 | } |
| 434 | } else { |
| 435 | switch (oobsize) { |
| Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 436 | case 0: |
| 437 | mtd->oobsize = 128; |
| 438 | break; |
| 439 | case 1: |
| 440 | mtd->oobsize = 224; |
| 441 | break; |
| 442 | case 2: |
| 443 | mtd->oobsize = 448; |
| 444 | break; |
| 445 | case 3: |
| 446 | mtd->oobsize = 64; |
| 447 | break; |
| 448 | case 4: |
| 449 | mtd->oobsize = 32; |
| 450 | break; |
| 451 | case 5: |
| 452 | mtd->oobsize = 16; |
| 453 | break; |
| Boris Brezillon | 78f3482 | 2016-05-27 14:36:36 +0200 | [diff] [blame] | 454 | case 6: |
| Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 455 | mtd->oobsize = 640; |
| 456 | break; |
| Boris Brezillon | 78f3482 | 2016-05-27 14:36:36 +0200 | [diff] [blame] | 457 | default: |
| 458 | /* |
| 459 | * We should never reach this case, but if that |
| 460 | * happens, this probably means Hynix decided to use |
| 461 | * a different extended ID format, and we should find |
| 462 | * a way to support it. |
| 463 | */ |
| 464 | WARN(1, "Invalid OOB size"); |
| 465 | break; |
| Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 466 | } |
| Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
| Boris Brezillon | 78f3482 | 2016-05-27 14:36:36 +0200 | [diff] [blame] | 470 | static void hynix_nand_extract_ecc_requirements(struct nand_chip *chip, |
| 471 | bool valid_jedecid) |
| 472 | { |
| 473 | u8 ecc_level = (chip->id.data[4] >> 4) & 0x7; |
| 474 | |
| 475 | if (valid_jedecid) { |
| 476 | /* Reference: H27UCG8T2E datasheet */ |
| 477 | chip->ecc_step_ds = 1024; |
| 478 | |
| 479 | switch (ecc_level) { |
| 480 | case 0: |
| 481 | chip->ecc_step_ds = 0; |
| 482 | chip->ecc_strength_ds = 0; |
| 483 | break; |
| 484 | case 1: |
| 485 | chip->ecc_strength_ds = 4; |
| 486 | break; |
| 487 | case 2: |
| 488 | chip->ecc_strength_ds = 24; |
| 489 | break; |
| 490 | case 3: |
| 491 | chip->ecc_strength_ds = 32; |
| 492 | break; |
| 493 | case 4: |
| 494 | chip->ecc_strength_ds = 40; |
| 495 | break; |
| 496 | case 5: |
| 497 | chip->ecc_strength_ds = 50; |
| 498 | break; |
| 499 | case 6: |
| 500 | chip->ecc_strength_ds = 60; |
| 501 | break; |
| 502 | default: |
| 503 | /* |
| 504 | * We should never reach this case, but if that |
| 505 | * happens, this probably means Hynix decided to use |
| 506 | * a different extended ID format, and we should find |
| 507 | * a way to support it. |
| 508 | */ |
| 509 | WARN(1, "Invalid ECC requirements"); |
| 510 | } |
| 511 | } else { |
| 512 | /* |
| 513 | * The ECC requirements field meaning depends on the |
| 514 | * NAND technology. |
| 515 | */ |
| Martin Blumenstingl | fd213b5 | 2017-08-05 14:16:24 +0200 | [diff] [blame] | 516 | u8 nand_tech = chip->id.data[5] & 0x7; |
| Boris Brezillon | 78f3482 | 2016-05-27 14:36:36 +0200 | [diff] [blame] | 517 | |
| 518 | if (nand_tech < 3) { |
| 519 | /* > 26nm, reference: H27UBG8T2A datasheet */ |
| 520 | if (ecc_level < 5) { |
| 521 | chip->ecc_step_ds = 512; |
| 522 | chip->ecc_strength_ds = 1 << ecc_level; |
| 523 | } else if (ecc_level < 7) { |
| 524 | if (ecc_level == 5) |
| 525 | chip->ecc_step_ds = 2048; |
| 526 | else |
| 527 | chip->ecc_step_ds = 1024; |
| 528 | chip->ecc_strength_ds = 24; |
| 529 | } else { |
| 530 | /* |
| 531 | * We should never reach this case, but if that |
| 532 | * happens, this probably means Hynix decided |
| 533 | * to use a different extended ID format, and |
| 534 | * we should find a way to support it. |
| 535 | */ |
| 536 | WARN(1, "Invalid ECC requirements"); |
| 537 | } |
| 538 | } else { |
| 539 | /* <= 26nm, reference: H27UBG8T2B datasheet */ |
| 540 | if (!ecc_level) { |
| 541 | chip->ecc_step_ds = 0; |
| 542 | chip->ecc_strength_ds = 0; |
| 543 | } else if (ecc_level < 5) { |
| 544 | chip->ecc_step_ds = 512; |
| 545 | chip->ecc_strength_ds = 1 << (ecc_level - 1); |
| 546 | } else { |
| 547 | chip->ecc_step_ds = 1024; |
| 548 | chip->ecc_strength_ds = 24 + |
| 549 | (8 * (ecc_level - 5)); |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | static void hynix_nand_extract_scrambling_requirements(struct nand_chip *chip, |
| 556 | bool valid_jedecid) |
| 557 | { |
| 558 | u8 nand_tech; |
| 559 | |
| 560 | /* We need scrambling on all TLC NANDs*/ |
| 561 | if (chip->bits_per_cell > 2) |
| 562 | chip->options |= NAND_NEED_SCRAMBLING; |
| 563 | |
| 564 | /* And on MLC NANDs with sub-3xnm process */ |
| 565 | if (valid_jedecid) { |
| 566 | nand_tech = chip->id.data[5] >> 4; |
| 567 | |
| 568 | /* < 3xnm */ |
| 569 | if (nand_tech > 0) |
| 570 | chip->options |= NAND_NEED_SCRAMBLING; |
| 571 | } else { |
| Martin Blumenstingl | fd213b5 | 2017-08-05 14:16:24 +0200 | [diff] [blame] | 572 | nand_tech = chip->id.data[5] & 0x7; |
| Boris Brezillon | 78f3482 | 2016-05-27 14:36:36 +0200 | [diff] [blame] | 573 | |
| 574 | /* < 32nm */ |
| 575 | if (nand_tech > 2) |
| 576 | chip->options |= NAND_NEED_SCRAMBLING; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | static void hynix_nand_decode_id(struct nand_chip *chip) |
| 581 | { |
| 582 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 583 | bool valid_jedecid; |
| 584 | u8 tmp; |
| 585 | |
| 586 | /* |
| 587 | * Exclude all SLC NANDs from this advanced detection scheme. |
| 588 | * According to the ranges defined in several datasheets, it might |
| 589 | * appear that even SLC NANDs could fall in this extended ID scheme. |
| 590 | * If that the case rework the test to let SLC NANDs go through the |
| 591 | * detection process. |
| 592 | */ |
| 593 | if (chip->id.len < 6 || nand_is_slc(chip)) { |
| 594 | nand_decode_ext_id(chip); |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | /* Extract pagesize */ |
| 599 | mtd->writesize = 2048 << (chip->id.data[3] & 0x03); |
| 600 | |
| 601 | tmp = (chip->id.data[3] >> 4) & 0x3; |
| 602 | /* |
| 603 | * When bit7 is set that means we start counting at 1MiB, otherwise |
| 604 | * we start counting at 128KiB and shift this value the content of |
| 605 | * ID[3][4:5]. |
| 606 | * The only exception is when ID[3][4:5] == 3 and ID[3][7] == 0, in |
| 607 | * this case the erasesize is set to 768KiB. |
| 608 | */ |
| 609 | if (chip->id.data[3] & 0x80) |
| 610 | mtd->erasesize = SZ_1M << tmp; |
| 611 | else if (tmp == 3) |
| 612 | mtd->erasesize = SZ_512K + SZ_256K; |
| 613 | else |
| 614 | mtd->erasesize = SZ_128K << tmp; |
| 615 | |
| 616 | /* |
| 617 | * Modern Toggle DDR NANDs have a valid JEDECID even though they are |
| 618 | * not exposing a valid JEDEC parameter table. |
| 619 | * These NANDs use a different NAND ID scheme. |
| 620 | */ |
| 621 | valid_jedecid = hynix_nand_has_valid_jedecid(chip); |
| 622 | |
| 623 | hynix_nand_extract_oobsize(chip, valid_jedecid); |
| 624 | hynix_nand_extract_ecc_requirements(chip, valid_jedecid); |
| 625 | hynix_nand_extract_scrambling_requirements(chip, valid_jedecid); |
| 626 | } |
| 627 | |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 628 | static void hynix_nand_cleanup(struct nand_chip *chip) |
| 629 | { |
| 630 | struct hynix_nand *hynix = nand_get_manufacturer_data(chip); |
| 631 | |
| 632 | if (!hynix) |
| 633 | return; |
| 634 | |
| 635 | kfree(hynix->read_retry); |
| 636 | kfree(hynix); |
| 637 | nand_set_manufacturer_data(chip, NULL); |
| 638 | } |
| 639 | |
| Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 640 | static int hynix_nand_init(struct nand_chip *chip) |
| 641 | { |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 642 | struct hynix_nand *hynix; |
| 643 | int ret; |
| 644 | |
| Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 645 | if (!nand_is_slc(chip)) |
| 646 | chip->bbt_options |= NAND_BBT_SCANLASTPAGE; |
| 647 | else |
| 648 | chip->bbt_options |= NAND_BBT_SCAN2NDPAGE; |
| 649 | |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 650 | hynix = kzalloc(sizeof(*hynix), GFP_KERNEL); |
| 651 | if (!hynix) |
| 652 | return -ENOMEM; |
| 653 | |
| 654 | nand_set_manufacturer_data(chip, hynix); |
| 655 | |
| 656 | ret = hynix_nand_rr_init(chip); |
| 657 | if (ret) |
| 658 | hynix_nand_cleanup(chip); |
| 659 | |
| 660 | return ret; |
| Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | const struct nand_manufacturer_ops hynix_nand_manuf_ops = { |
| 664 | .detect = hynix_nand_decode_id, |
| 665 | .init = hynix_nand_init, |
| Boris Brezillon | 626994e | 2016-05-27 10:15:03 +0200 | [diff] [blame] | 666 | .cleanup = hynix_nand_cleanup, |
| Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 667 | }; |