sf-uclass.c 2.4 KB

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