sf-uclass.c 2.2 KB

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