sf-uclass.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. spi_flash_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 name[30], *str;
  49. int ret;
  50. snprintf(name, sizeof(name), "spi_flash@%d:%d", busnum, cs);
  51. str = strdup(name);
  52. ret = spi_get_bus_and_cs(busnum, cs, max_hz, spi_mode,
  53. "spi_flash_std", str, &bus, &slave);
  54. if (ret)
  55. return ret;
  56. *devp = slave->dev;
  57. return 0;
  58. }
  59. int spi_flash_remove(struct udevice *dev)
  60. {
  61. return device_remove(dev);
  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. };