macronix.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018 Macronix
  4. *
  5. * Author: Boris Brezillon <boris.brezillon@bootlin.com>
  6. */
  7. #include <linux/device.h>
  8. #include <linux/kernel.h>
  9. #include <linux/mtd/spinand.h>
  10. #define SPINAND_MFR_MACRONIX 0xC2
  11. #define MACRONIX_ECCSR_MASK 0x0F
  12. static SPINAND_OP_VARIANTS(read_cache_variants,
  13. SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
  14. SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
  15. SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
  16. SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
  17. static SPINAND_OP_VARIANTS(write_cache_variants,
  18. SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
  19. SPINAND_PROG_LOAD(true, 0, NULL, 0));
  20. static SPINAND_OP_VARIANTS(update_cache_variants,
  21. SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
  22. SPINAND_PROG_LOAD(false, 0, NULL, 0));
  23. static int mx35lfxge4ab_ooblayout_ecc(struct mtd_info *mtd, int section,
  24. struct mtd_oob_region *region)
  25. {
  26. return -ERANGE;
  27. }
  28. static int mx35lfxge4ab_ooblayout_free(struct mtd_info *mtd, int section,
  29. struct mtd_oob_region *region)
  30. {
  31. if (section)
  32. return -ERANGE;
  33. region->offset = 2;
  34. region->length = mtd->oobsize - 2;
  35. return 0;
  36. }
  37. static const struct mtd_ooblayout_ops mx35lfxge4ab_ooblayout = {
  38. .ecc = mx35lfxge4ab_ooblayout_ecc,
  39. .free = mx35lfxge4ab_ooblayout_free,
  40. };
  41. static int mx35lf1ge4ab_get_eccsr(struct spinand_device *spinand, u8 *eccsr)
  42. {
  43. struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x7c, 1),
  44. SPI_MEM_OP_NO_ADDR,
  45. SPI_MEM_OP_DUMMY(1, 1),
  46. SPI_MEM_OP_DATA_IN(1, eccsr, 1));
  47. int ret = spi_mem_exec_op(spinand->spimem, &op);
  48. if (ret)
  49. return ret;
  50. *eccsr &= MACRONIX_ECCSR_MASK;
  51. return 0;
  52. }
  53. static int mx35lf1ge4ab_ecc_get_status(struct spinand_device *spinand,
  54. u8 status)
  55. {
  56. struct nand_device *nand = spinand_to_nand(spinand);
  57. u8 eccsr;
  58. switch (status & STATUS_ECC_MASK) {
  59. case STATUS_ECC_NO_BITFLIPS:
  60. return 0;
  61. case STATUS_ECC_UNCOR_ERROR:
  62. return -EBADMSG;
  63. case STATUS_ECC_HAS_BITFLIPS:
  64. /*
  65. * Let's try to retrieve the real maximum number of bitflips
  66. * in order to avoid forcing the wear-leveling layer to move
  67. * data around if it's not necessary.
  68. */
  69. if (mx35lf1ge4ab_get_eccsr(spinand, &eccsr))
  70. return nand->eccreq.strength;
  71. if (WARN_ON(eccsr > nand->eccreq.strength || !eccsr))
  72. return nand->eccreq.strength;
  73. return eccsr;
  74. default:
  75. break;
  76. }
  77. return -EINVAL;
  78. }
  79. static const struct spinand_info macronix_spinand_table[] = {
  80. SPINAND_INFO("MX35LF1GE4AB", 0x12,
  81. NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1),
  82. NAND_ECCREQ(4, 512),
  83. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  84. &write_cache_variants,
  85. &update_cache_variants),
  86. SPINAND_HAS_QE_BIT,
  87. SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
  88. mx35lf1ge4ab_ecc_get_status)),
  89. SPINAND_INFO("MX35LF2GE4AB", 0x22,
  90. NAND_MEMORG(1, 2048, 64, 64, 2048, 2, 1, 1),
  91. NAND_ECCREQ(4, 512),
  92. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  93. &write_cache_variants,
  94. &update_cache_variants),
  95. SPINAND_HAS_QE_BIT,
  96. SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL)),
  97. };
  98. static int macronix_spinand_detect(struct spinand_device *spinand)
  99. {
  100. u8 *id = spinand->id.data;
  101. int ret;
  102. /*
  103. * Macronix SPI NAND read ID needs a dummy byte, so the first byte in
  104. * raw_id is garbage.
  105. */
  106. if (id[1] != SPINAND_MFR_MACRONIX)
  107. return 0;
  108. ret = spinand_match_and_init(spinand, macronix_spinand_table,
  109. ARRAY_SIZE(macronix_spinand_table),
  110. id[2]);
  111. if (ret)
  112. return ret;
  113. return 1;
  114. }
  115. static const struct spinand_manufacturer_ops macronix_spinand_manuf_ops = {
  116. .detect = macronix_spinand_detect,
  117. };
  118. const struct spinand_manufacturer macronix_spinand_manufacturer = {
  119. .id = SPINAND_MFR_MACRONIX,
  120. .name = "Macronix",
  121. .ops = &macronix_spinand_manuf_ops,
  122. };