sf_probe.c 4.2 KB

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