nand_hynix.c 15 KB

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