toshiba.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018 exceet electronics GmbH
  4. * Copyright (c) 2018 Kontron Electronics GmbH
  5. *
  6. * Author: Frieder Schrempf <frieder.schrempf@kontron.de>
  7. */
  8. #ifndef __UBOOT__
  9. #include <malloc.h>
  10. #include <linux/device.h>
  11. #include <linux/kernel.h>
  12. #endif
  13. #include <linux/mtd/spinand.h>
  14. #define SPINAND_MFR_TOSHIBA 0x98
  15. #define TOSH_STATUS_ECC_HAS_BITFLIPS_T (3 << 4)
  16. static SPINAND_OP_VARIANTS(read_cache_variants,
  17. SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
  18. SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
  19. SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
  20. SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
  21. static SPINAND_OP_VARIANTS(write_cache_variants,
  22. SPINAND_PROG_LOAD(true, 0, NULL, 0));
  23. static SPINAND_OP_VARIANTS(update_cache_variants,
  24. SPINAND_PROG_LOAD(false, 0, NULL, 0));
  25. static int tc58cxgxsx_ooblayout_ecc(struct mtd_info *mtd, int section,
  26. struct mtd_oob_region *region)
  27. {
  28. if (section > 0)
  29. return -ERANGE;
  30. region->offset = mtd->oobsize / 2;
  31. region->length = mtd->oobsize / 2;
  32. return 0;
  33. }
  34. static int tc58cxgxsx_ooblayout_free(struct mtd_info *mtd, int section,
  35. struct mtd_oob_region *region)
  36. {
  37. if (section > 0)
  38. return -ERANGE;
  39. /* 2 bytes reserved for BBM */
  40. region->offset = 2;
  41. region->length = (mtd->oobsize / 2) - 2;
  42. return 0;
  43. }
  44. static const struct mtd_ooblayout_ops tc58cxgxsx_ooblayout = {
  45. .ecc = tc58cxgxsx_ooblayout_ecc,
  46. .rfree = tc58cxgxsx_ooblayout_free,
  47. };
  48. static int tc58cxgxsx_ecc_get_status(struct spinand_device *spinand,
  49. u8 status)
  50. {
  51. struct nand_device *nand = spinand_to_nand(spinand);
  52. u8 mbf = 0;
  53. struct spi_mem_op op = SPINAND_GET_FEATURE_OP(0x30, &mbf);
  54. switch (status & STATUS_ECC_MASK) {
  55. case STATUS_ECC_NO_BITFLIPS:
  56. return 0;
  57. case STATUS_ECC_UNCOR_ERROR:
  58. return -EBADMSG;
  59. case STATUS_ECC_HAS_BITFLIPS:
  60. case TOSH_STATUS_ECC_HAS_BITFLIPS_T:
  61. /*
  62. * Let's try to retrieve the real maximum number of bitflips
  63. * in order to avoid forcing the wear-leveling layer to move
  64. * data around if it's not necessary.
  65. */
  66. if (spi_mem_exec_op(spinand->slave, &op))
  67. return nand->eccreq.strength;
  68. mbf >>= 4;
  69. if (WARN_ON(mbf > nand->eccreq.strength || !mbf))
  70. return nand->eccreq.strength;
  71. return mbf;
  72. default:
  73. break;
  74. }
  75. return -EINVAL;
  76. }
  77. static const struct spinand_info toshiba_spinand_table[] = {
  78. /* 3.3V 1Gb */
  79. SPINAND_INFO("TC58CVG0S3", 0xC2,
  80. NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
  81. NAND_ECCREQ(8, 512),
  82. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  83. &write_cache_variants,
  84. &update_cache_variants),
  85. 0,
  86. SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
  87. tc58cxgxsx_ecc_get_status)),
  88. /* 3.3V 2Gb */
  89. SPINAND_INFO("TC58CVG1S3", 0xCB,
  90. NAND_MEMORG(1, 2048, 128, 64, 2048, 1, 1, 1),
  91. NAND_ECCREQ(8, 512),
  92. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  93. &write_cache_variants,
  94. &update_cache_variants),
  95. 0,
  96. SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
  97. tc58cxgxsx_ecc_get_status)),
  98. /* 3.3V 4Gb */
  99. SPINAND_INFO("TC58CVG2S0", 0xCD,
  100. NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
  101. NAND_ECCREQ(8, 512),
  102. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  103. &write_cache_variants,
  104. &update_cache_variants),
  105. 0,
  106. SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
  107. tc58cxgxsx_ecc_get_status)),
  108. /* 3.3V 4Gb */
  109. SPINAND_INFO("TC58CVG2S0", 0xED,
  110. NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
  111. NAND_ECCREQ(8, 512),
  112. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  113. &write_cache_variants,
  114. &update_cache_variants),
  115. 0,
  116. SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
  117. tc58cxgxsx_ecc_get_status)),
  118. /* 1.8V 1Gb */
  119. SPINAND_INFO("TC58CYG0S3", 0xB2,
  120. NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
  121. NAND_ECCREQ(8, 512),
  122. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  123. &write_cache_variants,
  124. &update_cache_variants),
  125. 0,
  126. SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
  127. tc58cxgxsx_ecc_get_status)),
  128. /* 1.8V 2Gb */
  129. SPINAND_INFO("TC58CYG1S3", 0xBB,
  130. NAND_MEMORG(1, 2048, 128, 64, 2048, 1, 1, 1),
  131. NAND_ECCREQ(8, 512),
  132. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  133. &write_cache_variants,
  134. &update_cache_variants),
  135. 0,
  136. SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
  137. tc58cxgxsx_ecc_get_status)),
  138. /* 1.8V 4Gb */
  139. SPINAND_INFO("TC58CYG2S0", 0xBD,
  140. NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
  141. NAND_ECCREQ(8, 512),
  142. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  143. &write_cache_variants,
  144. &update_cache_variants),
  145. 0,
  146. SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
  147. tc58cxgxsx_ecc_get_status)),
  148. };
  149. static int toshiba_spinand_detect(struct spinand_device *spinand)
  150. {
  151. u8 *id = spinand->id.data;
  152. int ret;
  153. /*
  154. * Toshiba SPI NAND read ID needs a dummy byte,
  155. * so the first byte in id is garbage.
  156. */
  157. if (id[1] != SPINAND_MFR_TOSHIBA)
  158. return 0;
  159. ret = spinand_match_and_init(spinand, toshiba_spinand_table,
  160. ARRAY_SIZE(toshiba_spinand_table),
  161. id[2]);
  162. if (ret)
  163. return ret;
  164. return 1;
  165. }
  166. static const struct spinand_manufacturer_ops toshiba_spinand_manuf_ops = {
  167. .detect = toshiba_spinand_detect,
  168. };
  169. const struct spinand_manufacturer toshiba_spinand_manufacturer = {
  170. .id = SPINAND_MFR_TOSHIBA,
  171. .name = "Toshiba",
  172. .ops = &toshiba_spinand_manuf_ops,
  173. };