macronix.c 3.7 KB

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