sf_probe.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * SPI flash probing
  4. *
  5. * Copyright (C) 2008 Atmel Corporation
  6. * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
  7. * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <errno.h>
  12. #include <malloc.h>
  13. #include <spi.h>
  14. #include <spi_flash.h>
  15. #include "sf_internal.h"
  16. /**
  17. * spi_flash_probe_slave() - Probe for a SPI flash device on a bus
  18. *
  19. * @flashp: Pointer to place to put flash info, which may be NULL if the
  20. * space should be allocated
  21. */
  22. static int spi_flash_probe_slave(struct spi_flash *flash)
  23. {
  24. struct spi_slave *spi = flash->spi;
  25. int ret;
  26. /* Setup spi_slave */
  27. if (!spi) {
  28. printf("SF: Failed to set up slave\n");
  29. return -ENODEV;
  30. }
  31. /* Claim spi bus */
  32. ret = spi_claim_bus(spi);
  33. if (ret) {
  34. debug("SF: Failed to claim SPI bus: %d\n", ret);
  35. return ret;
  36. }
  37. ret = spi_flash_scan(flash);
  38. if (ret)
  39. goto err_read_id;
  40. #ifdef CONFIG_SPI_FLASH_MTD
  41. ret = spi_flash_mtd_register(flash);
  42. #endif
  43. err_read_id:
  44. spi_release_bus(spi);
  45. return ret;
  46. }
  47. #ifndef CONFIG_DM_SPI_FLASH
  48. struct spi_flash *spi_flash_probe(unsigned int busnum, unsigned int cs,
  49. unsigned int max_hz, unsigned int spi_mode)
  50. {
  51. struct spi_slave *bus;
  52. struct spi_flash *flash;
  53. bus = spi_setup_slave(busnum, cs, max_hz, spi_mode);
  54. if (!bus)
  55. return NULL;
  56. /* Allocate space if needed (not used by sf-uclass */
  57. flash = calloc(1, sizeof(*flash));
  58. if (!flash) {
  59. debug("SF: Failed to allocate spi_flash\n");
  60. return NULL;
  61. }
  62. flash->spi = bus;
  63. if (spi_flash_probe_slave(flash)) {
  64. spi_free_slave(bus);
  65. free(flash);
  66. return NULL;
  67. }
  68. return flash;
  69. }
  70. void spi_flash_free(struct spi_flash *flash)
  71. {
  72. #ifdef CONFIG_SPI_FLASH_MTD
  73. spi_flash_mtd_unregister();
  74. #endif
  75. spi_free_slave(flash->spi);
  76. free(flash);
  77. }
  78. #else /* defined CONFIG_DM_SPI_FLASH */
  79. static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len,
  80. void *buf)
  81. {
  82. struct spi_flash *flash = dev_get_uclass_priv(dev);
  83. return spi_flash_cmd_read_ops(flash, offset, len, buf);
  84. }
  85. static int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
  86. const void *buf)
  87. {
  88. struct spi_flash *flash = dev_get_uclass_priv(dev);
  89. #if defined(CONFIG_SPI_FLASH_SST)
  90. if (flash->flags & SNOR_F_SST_WR) {
  91. if (flash->spi->mode & SPI_TX_BYTE)
  92. return sst_write_bp(flash, offset, len, buf);
  93. else
  94. return sst_write_wp(flash, offset, len, buf);
  95. }
  96. #endif
  97. return spi_flash_cmd_write_ops(flash, offset, len, buf);
  98. }
  99. static int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
  100. {
  101. struct spi_flash *flash = dev_get_uclass_priv(dev);
  102. return spi_flash_cmd_erase_ops(flash, offset, len);
  103. }
  104. static int spi_flash_std_probe(struct udevice *dev)
  105. {
  106. struct spi_slave *slave = dev_get_parent_priv(dev);
  107. struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
  108. struct spi_flash *flash;
  109. flash = dev_get_uclass_priv(dev);
  110. flash->dev = dev;
  111. flash->spi = slave;
  112. debug("%s: slave=%p, cs=%d\n", __func__, slave, plat->cs);
  113. return spi_flash_probe_slave(flash);
  114. }
  115. static const struct dm_spi_flash_ops spi_flash_std_ops = {
  116. .read = spi_flash_std_read,
  117. .write = spi_flash_std_write,
  118. .erase = spi_flash_std_erase,
  119. };
  120. static const struct udevice_id spi_flash_std_ids[] = {
  121. { .compatible = "spi-flash" },
  122. { }
  123. };
  124. U_BOOT_DRIVER(spi_flash_std) = {
  125. .name = "spi_flash_std",
  126. .id = UCLASS_SPI_FLASH,
  127. .of_match = spi_flash_std_ids,
  128. .probe = spi_flash_std_probe,
  129. .priv_auto_alloc_size = sizeof(struct spi_flash),
  130. .ops = &spi_flash_std_ops,
  131. };
  132. #endif /* CONFIG_DM_SPI_FLASH */