micron.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2016-2017 Micron Technology, Inc.
  4. *
  5. * Authors:
  6. * Peter Pan <peterpandong@micron.com>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mtd/spinand.h>
  11. #define SPINAND_MFR_MICRON 0x2c
  12. #define MICRON_STATUS_ECC_MASK GENMASK(7, 4)
  13. #define MICRON_STATUS_ECC_NO_BITFLIPS (0 << 4)
  14. #define MICRON_STATUS_ECC_1TO3_BITFLIPS (1 << 4)
  15. #define MICRON_STATUS_ECC_4TO6_BITFLIPS (3 << 4)
  16. #define MICRON_STATUS_ECC_7TO8_BITFLIPS (5 << 4)
  17. static SPINAND_OP_VARIANTS(read_cache_variants,
  18. SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
  19. SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
  20. SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
  21. SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
  22. SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
  23. SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
  24. static SPINAND_OP_VARIANTS(write_cache_variants,
  25. SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
  26. SPINAND_PROG_LOAD(true, 0, NULL, 0));
  27. static SPINAND_OP_VARIANTS(update_cache_variants,
  28. SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
  29. SPINAND_PROG_LOAD(false, 0, NULL, 0));
  30. static int mt29f2g01abagd_ooblayout_ecc(struct mtd_info *mtd, int section,
  31. struct mtd_oob_region *region)
  32. {
  33. if (section)
  34. return -ERANGE;
  35. region->offset = 64;
  36. region->length = 64;
  37. return 0;
  38. }
  39. static int mt29f2g01abagd_ooblayout_free(struct mtd_info *mtd, int section,
  40. struct mtd_oob_region *region)
  41. {
  42. if (section)
  43. return -ERANGE;
  44. /* Reserve 2 bytes for the BBM. */
  45. region->offset = 2;
  46. region->length = 62;
  47. return 0;
  48. }
  49. static const struct mtd_ooblayout_ops mt29f2g01abagd_ooblayout = {
  50. .ecc = mt29f2g01abagd_ooblayout_ecc,
  51. .free = mt29f2g01abagd_ooblayout_free,
  52. };
  53. static int mt29f2g01abagd_ecc_get_status(struct spinand_device *spinand,
  54. u8 status)
  55. {
  56. switch (status & MICRON_STATUS_ECC_MASK) {
  57. case STATUS_ECC_NO_BITFLIPS:
  58. return 0;
  59. case STATUS_ECC_UNCOR_ERROR:
  60. return -EBADMSG;
  61. case MICRON_STATUS_ECC_1TO3_BITFLIPS:
  62. return 3;
  63. case MICRON_STATUS_ECC_4TO6_BITFLIPS:
  64. return 6;
  65. case MICRON_STATUS_ECC_7TO8_BITFLIPS:
  66. return 8;
  67. default:
  68. break;
  69. }
  70. return -EINVAL;
  71. }
  72. static const struct spinand_info micron_spinand_table[] = {
  73. SPINAND_INFO("MT29F2G01ABAGD", 0x24,
  74. NAND_MEMORG(1, 2048, 128, 64, 2048, 2, 1, 1),
  75. NAND_ECCREQ(8, 512),
  76. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  77. &write_cache_variants,
  78. &update_cache_variants),
  79. 0,
  80. SPINAND_ECCINFO(&mt29f2g01abagd_ooblayout,
  81. mt29f2g01abagd_ecc_get_status)),
  82. };
  83. static int micron_spinand_detect(struct spinand_device *spinand)
  84. {
  85. u8 *id = spinand->id.data;
  86. int ret;
  87. /*
  88. * Micron SPI NAND read ID need a dummy byte,
  89. * so the first byte in raw_id is dummy.
  90. */
  91. if (id[1] != SPINAND_MFR_MICRON)
  92. return 0;
  93. ret = spinand_match_and_init(spinand, micron_spinand_table,
  94. ARRAY_SIZE(micron_spinand_table), id[2]);
  95. if (ret)
  96. return ret;
  97. return 1;
  98. }
  99. static const struct spinand_manufacturer_ops micron_spinand_manuf_ops = {
  100. .detect = micron_spinand_detect,
  101. };
  102. const struct spinand_manufacturer micron_spinand_manufacturer = {
  103. .id = SPINAND_MFR_MICRON,
  104. .name = "Micron",
  105. .ops = &micron_spinand_manuf_ops,
  106. };