winbond.c 3.9 KB

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