sf-uclass.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. 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. spi_flash_remove(flash->spi->dev);
  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 name[20], *str;
  48. int ret;
  49. snprintf(name, sizeof(name), "%d:%d", busnum, cs);
  50. str = strdup(name);
  51. ret = spi_get_bus_and_cs(busnum, cs, max_hz, spi_mode,
  52. "spi_flash_std", str, &bus, &slave);
  53. if (ret)
  54. return ret;
  55. *devp = slave->dev;
  56. return 0;
  57. }
  58. int spi_flash_remove(struct udevice *dev)
  59. {
  60. return device_remove(dev);
  61. }
  62. UCLASS_DRIVER(spi_flash) = {
  63. .id = UCLASS_SPI_FLASH,
  64. .name = "spi_flash",
  65. .per_device_auto_alloc_size = sizeof(struct spi_flash),
  66. };