xilinx.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2005, Intec Automation Inc.
  4. * Copyright (C) 2014, Freescale Semiconductor, Inc.
  5. */
  6. #include <linux/mtd/spi-nor.h>
  7. #include "core.h"
  8. #define XILINX_OP_SE 0x50 /* Sector erase */
  9. #define XILINX_OP_PP 0x82 /* Page program */
  10. #define XILINX_OP_RDSR 0xd7 /* Read status register */
  11. #define XSR_PAGESIZE BIT(0) /* Page size in Po2 or Linear */
  12. #define XSR_RDY BIT(7) /* Ready */
  13. #define XILINX_RDSR_OP(buf) \
  14. SPI_MEM_OP(SPI_MEM_OP_CMD(XILINX_OP_RDSR, 0), \
  15. SPI_MEM_OP_NO_ADDR, \
  16. SPI_MEM_OP_NO_DUMMY, \
  17. SPI_MEM_OP_DATA_IN(1, buf, 0))
  18. #define S3AN_INFO(_jedec_id, _n_sectors, _page_size) \
  19. .id = { \
  20. ((_jedec_id) >> 16) & 0xff, \
  21. ((_jedec_id) >> 8) & 0xff, \
  22. (_jedec_id) & 0xff \
  23. }, \
  24. .id_len = 3, \
  25. .sector_size = (8 * (_page_size)), \
  26. .n_sectors = (_n_sectors), \
  27. .page_size = (_page_size), \
  28. .addr_nbytes = 3, \
  29. .flags = SPI_NOR_NO_FR
  30. /* Xilinx S3AN share MFR with Atmel SPI NOR */
  31. static const struct flash_info xilinx_nor_parts[] = {
  32. /* Xilinx S3AN Internal Flash */
  33. { "3S50AN", S3AN_INFO(0x1f2200, 64, 264) },
  34. { "3S200AN", S3AN_INFO(0x1f2400, 256, 264) },
  35. { "3S400AN", S3AN_INFO(0x1f2400, 256, 264) },
  36. { "3S700AN", S3AN_INFO(0x1f2500, 512, 264) },
  37. { "3S1400AN", S3AN_INFO(0x1f2600, 512, 528) },
  38. };
  39. /*
  40. * This code converts an address to the Default Address Mode, that has non
  41. * power of two page sizes. We must support this mode because it is the default
  42. * mode supported by Xilinx tools, it can access the whole flash area and
  43. * changing over to the Power-of-two mode is irreversible and corrupts the
  44. * original data.
  45. * Addr can safely be unsigned int, the biggest S3AN device is smaller than
  46. * 4 MiB.
  47. */
  48. static u32 s3an_nor_convert_addr(struct spi_nor *nor, u32 addr)
  49. {
  50. u32 page_size = nor->params->page_size;
  51. u32 offset, page;
  52. offset = addr % page_size;
  53. page = addr / page_size;
  54. page <<= (page_size > 512) ? 10 : 9;
  55. return page | offset;
  56. }
  57. /**
  58. * xilinx_nor_read_sr() - Read the Status Register on S3AN flashes.
  59. * @nor: pointer to 'struct spi_nor'.
  60. * @sr: pointer to a DMA-able buffer where the value of the
  61. * Status Register will be written.
  62. *
  63. * Return: 0 on success, -errno otherwise.
  64. */
  65. static int xilinx_nor_read_sr(struct spi_nor *nor, u8 *sr)
  66. {
  67. int ret;
  68. if (nor->spimem) {
  69. struct spi_mem_op op = XILINX_RDSR_OP(sr);
  70. spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
  71. ret = spi_mem_exec_op(nor->spimem, &op);
  72. } else {
  73. ret = spi_nor_controller_ops_read_reg(nor, XILINX_OP_RDSR, sr,
  74. 1);
  75. }
  76. if (ret)
  77. dev_dbg(nor->dev, "error %d reading SR\n", ret);
  78. return ret;
  79. }
  80. /**
  81. * xilinx_nor_sr_ready() - Query the Status Register of the S3AN flash to see
  82. * if the flash is ready for new commands.
  83. * @nor: pointer to 'struct spi_nor'.
  84. *
  85. * Return: 1 if ready, 0 if not ready, -errno on errors.
  86. */
  87. static int xilinx_nor_sr_ready(struct spi_nor *nor)
  88. {
  89. int ret;
  90. ret = xilinx_nor_read_sr(nor, nor->bouncebuf);
  91. if (ret)
  92. return ret;
  93. return !!(nor->bouncebuf[0] & XSR_RDY);
  94. }
  95. static int xilinx_nor_setup(struct spi_nor *nor,
  96. const struct spi_nor_hwcaps *hwcaps)
  97. {
  98. u32 page_size;
  99. int ret;
  100. ret = xilinx_nor_read_sr(nor, nor->bouncebuf);
  101. if (ret)
  102. return ret;
  103. nor->erase_opcode = XILINX_OP_SE;
  104. nor->program_opcode = XILINX_OP_PP;
  105. nor->read_opcode = SPINOR_OP_READ;
  106. nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
  107. /*
  108. * This flashes have a page size of 264 or 528 bytes (known as
  109. * Default addressing mode). It can be changed to a more standard
  110. * Power of two mode where the page size is 256/512. This comes
  111. * with a price: there is 3% less of space, the data is corrupted
  112. * and the page size cannot be changed back to default addressing
  113. * mode.
  114. *
  115. * The current addressing mode can be read from the XRDSR register
  116. * and should not be changed, because is a destructive operation.
  117. */
  118. if (nor->bouncebuf[0] & XSR_PAGESIZE) {
  119. /* Flash in Power of 2 mode */
  120. page_size = (nor->params->page_size == 264) ? 256 : 512;
  121. nor->params->page_size = page_size;
  122. nor->mtd.writebufsize = page_size;
  123. nor->params->size = 8 * page_size * nor->info->n_sectors;
  124. nor->mtd.erasesize = 8 * page_size;
  125. } else {
  126. /* Flash in Default addressing mode */
  127. nor->params->convert_addr = s3an_nor_convert_addr;
  128. nor->mtd.erasesize = nor->info->sector_size;
  129. }
  130. return 0;
  131. }
  132. static void xilinx_nor_late_init(struct spi_nor *nor)
  133. {
  134. nor->params->setup = xilinx_nor_setup;
  135. nor->params->ready = xilinx_nor_sr_ready;
  136. }
  137. static const struct spi_nor_fixups xilinx_nor_fixups = {
  138. .late_init = xilinx_nor_late_init,
  139. };
  140. const struct spi_nor_manufacturer spi_nor_xilinx = {
  141. .name = "xilinx",
  142. .parts = xilinx_nor_parts,
  143. .nparts = ARRAY_SIZE(xilinx_nor_parts),
  144. .fixups = &xilinx_nor_fixups,
  145. };