nand_onfi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
  4. * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
  5. *
  6. * Credits:
  7. * David Woodhouse for adding multichip support
  8. *
  9. * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
  10. * rework for 2K page size chips
  11. *
  12. * This file contains all ONFI helpers.
  13. */
  14. #include <linux/slab.h>
  15. #include "internals.h"
  16. u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
  17. {
  18. int i;
  19. while (len--) {
  20. crc ^= *p++ << 8;
  21. for (i = 0; i < 8; i++)
  22. crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
  23. }
  24. return crc;
  25. }
  26. /* Parse the Extended Parameter Page. */
  27. static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
  28. struct nand_onfi_params *p)
  29. {
  30. struct onfi_ext_param_page *ep;
  31. struct onfi_ext_section *s;
  32. struct onfi_ext_ecc_info *ecc;
  33. uint8_t *cursor;
  34. int ret;
  35. int len;
  36. int i;
  37. len = le16_to_cpu(p->ext_param_page_length) * 16;
  38. ep = kmalloc(len, GFP_KERNEL);
  39. if (!ep)
  40. return -ENOMEM;
  41. /* Send our own NAND_CMD_PARAM. */
  42. ret = nand_read_param_page_op(chip, 0, NULL, 0);
  43. if (ret)
  44. goto ext_out;
  45. /* Use the Change Read Column command to skip the ONFI param pages. */
  46. ret = nand_change_read_column_op(chip,
  47. sizeof(*p) * p->num_of_param_pages,
  48. ep, len, true);
  49. if (ret)
  50. goto ext_out;
  51. ret = -EINVAL;
  52. if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
  53. != le16_to_cpu(ep->crc))) {
  54. pr_debug("fail in the CRC.\n");
  55. goto ext_out;
  56. }
  57. /*
  58. * Check the signature.
  59. * Do not strictly follow the ONFI spec, maybe changed in future.
  60. */
  61. if (strncmp(ep->sig, "EPPS", 4)) {
  62. pr_debug("The signature is invalid.\n");
  63. goto ext_out;
  64. }
  65. /* find the ECC section. */
  66. cursor = (uint8_t *)(ep + 1);
  67. for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
  68. s = ep->sections + i;
  69. if (s->type == ONFI_SECTION_TYPE_2)
  70. break;
  71. cursor += s->length * 16;
  72. }
  73. if (i == ONFI_EXT_SECTION_MAX) {
  74. pr_debug("We can not find the ECC section.\n");
  75. goto ext_out;
  76. }
  77. /* get the info we want. */
  78. ecc = (struct onfi_ext_ecc_info *)cursor;
  79. if (!ecc->codeword_size) {
  80. pr_debug("Invalid codeword size\n");
  81. goto ext_out;
  82. }
  83. chip->ecc_strength_ds = ecc->ecc_bits;
  84. chip->ecc_step_ds = 1 << ecc->codeword_size;
  85. ret = 0;
  86. ext_out:
  87. kfree(ep);
  88. return ret;
  89. }
  90. /*
  91. * Recover data with bit-wise majority
  92. */
  93. static void nand_bit_wise_majority(const void **srcbufs,
  94. unsigned int nsrcbufs,
  95. void *dstbuf,
  96. unsigned int bufsize)
  97. {
  98. int i, j, k;
  99. for (i = 0; i < bufsize; i++) {
  100. u8 val = 0;
  101. for (j = 0; j < 8; j++) {
  102. unsigned int cnt = 0;
  103. for (k = 0; k < nsrcbufs; k++) {
  104. const u8 *srcbuf = srcbufs[k];
  105. if (srcbuf[i] & BIT(j))
  106. cnt++;
  107. }
  108. if (cnt > nsrcbufs / 2)
  109. val |= BIT(j);
  110. }
  111. ((u8 *)dstbuf)[i] = val;
  112. }
  113. }
  114. /*
  115. * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
  116. */
  117. int nand_onfi_detect(struct nand_chip *chip)
  118. {
  119. struct mtd_info *mtd = nand_to_mtd(chip);
  120. struct nand_onfi_params *p;
  121. struct onfi_params *onfi;
  122. int onfi_version = 0;
  123. char id[4];
  124. int i, ret, val;
  125. /* Try ONFI for unknown chip or LP */
  126. ret = nand_readid_op(chip, 0x20, id, sizeof(id));
  127. if (ret || strncmp(id, "ONFI", 4))
  128. return 0;
  129. /* ONFI chip: allocate a buffer to hold its parameter page */
  130. p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
  131. if (!p)
  132. return -ENOMEM;
  133. ret = nand_read_param_page_op(chip, 0, NULL, 0);
  134. if (ret) {
  135. ret = 0;
  136. goto free_onfi_param_page;
  137. }
  138. for (i = 0; i < 3; i++) {
  139. ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
  140. if (ret) {
  141. ret = 0;
  142. goto free_onfi_param_page;
  143. }
  144. if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
  145. le16_to_cpu(p->crc)) {
  146. if (i)
  147. memcpy(p, &p[i], sizeof(*p));
  148. break;
  149. }
  150. }
  151. if (i == 3) {
  152. const void *srcbufs[3] = {p, p + 1, p + 2};
  153. pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
  154. nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
  155. sizeof(*p));
  156. if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
  157. le16_to_cpu(p->crc)) {
  158. pr_err("ONFI parameter recovery failed, aborting\n");
  159. goto free_onfi_param_page;
  160. }
  161. }
  162. if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
  163. chip->manufacturer.desc->ops->fixup_onfi_param_page)
  164. chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
  165. /* Check version */
  166. val = le16_to_cpu(p->revision);
  167. if (val & ONFI_VERSION_2_3)
  168. onfi_version = 23;
  169. else if (val & ONFI_VERSION_2_2)
  170. onfi_version = 22;
  171. else if (val & ONFI_VERSION_2_1)
  172. onfi_version = 21;
  173. else if (val & ONFI_VERSION_2_0)
  174. onfi_version = 20;
  175. else if (val & ONFI_VERSION_1_0)
  176. onfi_version = 10;
  177. if (!onfi_version) {
  178. pr_info("unsupported ONFI version: %d\n", val);
  179. goto free_onfi_param_page;
  180. }
  181. sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  182. sanitize_string(p->model, sizeof(p->model));
  183. chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
  184. if (!chip->parameters.model) {
  185. ret = -ENOMEM;
  186. goto free_onfi_param_page;
  187. }
  188. mtd->writesize = le32_to_cpu(p->byte_per_page);
  189. /*
  190. * pages_per_block and blocks_per_lun may not be a power-of-2 size
  191. * (don't ask me who thought of this...). MTD assumes that these
  192. * dimensions will be power-of-2, so just truncate the remaining area.
  193. */
  194. mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
  195. mtd->erasesize *= mtd->writesize;
  196. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  197. /* See erasesize comment */
  198. chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
  199. chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
  200. chip->bits_per_cell = p->bits_per_cell;
  201. chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
  202. chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
  203. if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
  204. chip->options |= NAND_BUSWIDTH_16;
  205. if (p->ecc_bits != 0xff) {
  206. chip->ecc_strength_ds = p->ecc_bits;
  207. chip->ecc_step_ds = 512;
  208. } else if (onfi_version >= 21 &&
  209. (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
  210. /*
  211. * The nand_flash_detect_ext_param_page() uses the
  212. * Change Read Column command which maybe not supported
  213. * by the chip->legacy.cmdfunc. So try to update the
  214. * chip->legacy.cmdfunc now. We do not replace user supplied
  215. * command function.
  216. */
  217. nand_legacy_adjust_cmdfunc(chip);
  218. /* The Extended Parameter Page is supported since ONFI 2.1. */
  219. if (nand_flash_detect_ext_param_page(chip, p))
  220. pr_warn("Failed to detect ONFI extended param page\n");
  221. } else {
  222. pr_warn("Could not retrieve ONFI ECC requirements\n");
  223. }
  224. /* Save some parameters from the parameter page for future use */
  225. if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
  226. chip->parameters.supports_set_get_features = true;
  227. bitmap_set(chip->parameters.get_feature_list,
  228. ONFI_FEATURE_ADDR_TIMING_MODE, 1);
  229. bitmap_set(chip->parameters.set_feature_list,
  230. ONFI_FEATURE_ADDR_TIMING_MODE, 1);
  231. }
  232. onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
  233. if (!onfi) {
  234. ret = -ENOMEM;
  235. goto free_model;
  236. }
  237. onfi->version = onfi_version;
  238. onfi->tPROG = le16_to_cpu(p->t_prog);
  239. onfi->tBERS = le16_to_cpu(p->t_bers);
  240. onfi->tR = le16_to_cpu(p->t_r);
  241. onfi->tCCS = le16_to_cpu(p->t_ccs);
  242. onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode);
  243. onfi->vendor_revision = le16_to_cpu(p->vendor_revision);
  244. memcpy(onfi->vendor, p->vendor, sizeof(p->vendor));
  245. chip->parameters.onfi = onfi;
  246. /* Identification done, free the full ONFI parameter page and exit */
  247. kfree(p);
  248. return 1;
  249. free_model:
  250. kfree(chip->parameters.model);
  251. free_onfi_param_page:
  252. kfree(p);
  253. return ret;
  254. }