nand_hynix.c 16 KB

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