nand_onfi.c 8.5 KB

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