winbond.c 3.6 KB

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