sf-uclass.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2014 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <spi.h>
  8. #include <spi_flash.h>
  9. #include <dm/device-internal.h>
  10. #include "sf_internal.h"
  11. DECLARE_GLOBAL_DATA_PTR;
  12. int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf)
  13. {
  14. return sf_get_ops(dev)->read(dev, offset, len, buf);
  15. }
  16. int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
  17. const void *buf)
  18. {
  19. return sf_get_ops(dev)->write(dev, offset, len, buf);
  20. }
  21. int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len)
  22. {
  23. return sf_get_ops(dev)->erase(dev, offset, len);
  24. }
  25. /*
  26. * TODO(sjg@chromium.org): This is an old-style function. We should remove
  27. * it when all SPI flash drivers use dm
  28. */
  29. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  30. unsigned int max_hz, unsigned int spi_mode)
  31. {
  32. struct udevice *dev;
  33. if (spi_flash_probe_bus_cs(bus, cs, max_hz, spi_mode, &dev))
  34. return NULL;
  35. return dev_get_uclass_priv(dev);
  36. }
  37. void spi_flash_free(struct spi_flash *flash)
  38. {
  39. device_remove(flash->spi->dev, DM_REMOVE_NORMAL);
  40. }
  41. int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
  42. unsigned int max_hz, unsigned int spi_mode,
  43. struct udevice **devp)
  44. {
  45. struct spi_slave *slave;
  46. struct udevice *bus;
  47. char *str;
  48. int ret;
  49. #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_USE_TINY_PRINTF)
  50. str = "spi_flash";
  51. #else
  52. char name[30];
  53. snprintf(name, sizeof(name), "spi_flash@%d:%d", busnum, cs);
  54. str = strdup(name);
  55. #endif
  56. ret = spi_get_bus_and_cs(busnum, cs, max_hz, spi_mode,
  57. "spi_flash_std", str, &bus, &slave);
  58. if (ret)
  59. return ret;
  60. *devp = slave->dev;
  61. return 0;
  62. }
  63. static int spi_flash_post_bind(struct udevice *dev)
  64. {
  65. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  66. struct dm_spi_flash_ops *ops = sf_get_ops(dev);
  67. static int reloc_done;
  68. if (!reloc_done) {
  69. if (ops->read)
  70. ops->read += gd->reloc_off;
  71. if (ops->write)
  72. ops->write += gd->reloc_off;
  73. if (ops->erase)
  74. ops->erase += gd->reloc_off;
  75. reloc_done++;
  76. }
  77. #endif
  78. return 0;
  79. }
  80. UCLASS_DRIVER(spi_flash) = {
  81. .id = UCLASS_SPI_FLASH,
  82. .name = "spi_flash",
  83. .post_bind = spi_flash_post_bind,
  84. .per_device_auto_alloc_size = sizeof(struct spi_flash),
  85. };