toshiba.c 5.4 KB

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