winbond.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017 exceet electronics GmbH
  4. *
  5. * Authors:
  6. * Frieder Schrempf <frieder.schrempf@exceet.de>
  7. * Boris Brezillon <boris.brezillon@bootlin.com>
  8. */
  9. #include <linux/device.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mtd/spinand.h>
  12. #define SPINAND_MFR_WINBOND 0xEF
  13. #define WINBOND_CFG_BUF_READ BIT(3)
  14. static SPINAND_OP_VARIANTS(read_cache_variants,
  15. SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
  16. SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
  17. SPINAND_PAGE_READ_FROM_CACHE_DUALIO_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_X4(true, 0, NULL, 0),
  23. SPINAND_PROG_LOAD(true, 0, NULL, 0));
  24. static SPINAND_OP_VARIANTS(update_cache_variants,
  25. SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
  26. SPINAND_PROG_LOAD(false, 0, NULL, 0));
  27. static int w25m02gv_ooblayout_ecc(struct mtd_info *mtd, int section,
  28. struct mtd_oob_region *region)
  29. {
  30. if (section > 3)
  31. return -ERANGE;
  32. region->offset = (16 * section) + 8;
  33. region->length = 8;
  34. return 0;
  35. }
  36. static int w25m02gv_ooblayout_free(struct mtd_info *mtd, int section,
  37. struct mtd_oob_region *region)
  38. {
  39. if (section > 3)
  40. return -ERANGE;
  41. region->offset = (16 * section) + 2;
  42. region->length = 6;
  43. return 0;
  44. }
  45. static const struct mtd_ooblayout_ops w25m02gv_ooblayout = {
  46. .ecc = w25m02gv_ooblayout_ecc,
  47. .free = w25m02gv_ooblayout_free,
  48. };
  49. static int w25m02gv_select_target(struct spinand_device *spinand,
  50. unsigned int target)
  51. {
  52. struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0xc2, 1),
  53. SPI_MEM_OP_NO_ADDR,
  54. SPI_MEM_OP_NO_DUMMY,
  55. SPI_MEM_OP_DATA_OUT(1,
  56. spinand->scratchbuf,
  57. 1));
  58. *spinand->scratchbuf = target;
  59. return spi_mem_exec_op(spinand->spimem, &op);
  60. }
  61. static const struct spinand_info winbond_spinand_table[] = {
  62. SPINAND_INFO("W25M02GV", 0xAB,
  63. NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 2),
  64. NAND_ECCREQ(1, 512),
  65. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  66. &write_cache_variants,
  67. &update_cache_variants),
  68. 0,
  69. SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL),
  70. SPINAND_SELECT_TARGET(w25m02gv_select_target)),
  71. };
  72. /**
  73. * winbond_spinand_detect - initialize device related part in spinand_device
  74. * struct if it is a Winbond device.
  75. * @spinand: SPI NAND device structure
  76. */
  77. static int winbond_spinand_detect(struct spinand_device *spinand)
  78. {
  79. u8 *id = spinand->id.data;
  80. int ret;
  81. /*
  82. * Winbond SPI NAND read ID need a dummy byte,
  83. * so the first byte in raw_id is dummy.
  84. */
  85. if (id[1] != SPINAND_MFR_WINBOND)
  86. return 0;
  87. ret = spinand_match_and_init(spinand, winbond_spinand_table,
  88. ARRAY_SIZE(winbond_spinand_table), id[2]);
  89. if (ret)
  90. return ret;
  91. return 1;
  92. }
  93. static int winbond_spinand_init(struct spinand_device *spinand)
  94. {
  95. struct nand_device *nand = spinand_to_nand(spinand);
  96. unsigned int i;
  97. /*
  98. * Make sure all dies are in buffer read mode and not continuous read
  99. * mode.
  100. */
  101. for (i = 0; i < nand->memorg.ntargets; i++) {
  102. spinand_select_target(spinand, i);
  103. spinand_upd_cfg(spinand, WINBOND_CFG_BUF_READ,
  104. WINBOND_CFG_BUF_READ);
  105. }
  106. return 0;
  107. }
  108. static const struct spinand_manufacturer_ops winbond_spinand_manuf_ops = {
  109. .detect = winbond_spinand_detect,
  110. .init = winbond_spinand_init,
  111. };
  112. const struct spinand_manufacturer winbond_spinand_manufacturer = {
  113. .id = SPINAND_MFR_WINBOND,
  114. .name = "Winbond",
  115. .ops = &winbond_spinand_manuf_ops,
  116. };