spi-flash.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2019 Collabora
  4. * (C) Copyright 2019 GE
  5. */
  6. #include <common.h>
  7. #include <bootcount.h>
  8. #include <dm.h>
  9. #include <spi_flash.h>
  10. static const u8 bootcount_magic = 0xbc;
  11. struct bootcount_spi_flash_priv {
  12. struct udevice *spi_flash;
  13. u32 offset;
  14. };
  15. static int bootcount_spi_flash_update(struct udevice *dev, u32 offset, u32 len, const void *buf)
  16. {
  17. struct spi_flash *flash = dev_get_uclass_priv(dev);
  18. u32 sector_size = flash->sector_size;
  19. u32 sector_offset = offset % sector_size;
  20. u32 sector = offset - sector_offset;
  21. int err = 0;
  22. /* code only supports updating a single sector */
  23. if (sector_offset + len > sector_size)
  24. return -ENOSYS;
  25. u8 *buffer = malloc(sector_size);
  26. if (!buffer)
  27. return -ENOMEM;
  28. err = spi_flash_read_dm(dev, sector, sector_size, buffer);
  29. if (err < 0)
  30. goto out;
  31. memcpy(buffer + sector_offset, buf, len);
  32. err = spi_flash_erase_dm(dev, sector, sector_size);
  33. if (err < 0)
  34. goto out;
  35. err = spi_flash_write_dm(dev, sector, sector_size, buffer);
  36. if (err < 0)
  37. goto out;
  38. out:
  39. free(buffer);
  40. return err;
  41. }
  42. static int bootcount_spi_flash_set(struct udevice *dev, const u32 a)
  43. {
  44. struct bootcount_spi_flash_priv *priv = dev_get_priv(dev);
  45. const u16 val = bootcount_magic << 8 | (a & 0xff);
  46. if (bootcount_spi_flash_update(priv->spi_flash, priv->offset, 2, &val) < 0) {
  47. debug("%s: write failed\n", __func__);
  48. return -EIO;
  49. }
  50. return 0;
  51. }
  52. static int bootcount_spi_flash_get(struct udevice *dev, u32 *a)
  53. {
  54. struct bootcount_spi_flash_priv *priv = dev_get_priv(dev);
  55. u16 val;
  56. if (spi_flash_read_dm(priv->spi_flash, priv->offset, 2, &val) < 0) {
  57. debug("%s: read failed\n", __func__);
  58. return -EIO;
  59. }
  60. if (val >> 8 == bootcount_magic) {
  61. *a = val & 0xff;
  62. return 0;
  63. }
  64. debug("%s: bootcount magic does not match on %04x\n", __func__, val);
  65. return -EIO;
  66. }
  67. static int bootcount_spi_flash_probe(struct udevice *dev)
  68. {
  69. struct ofnode_phandle_args phandle_args;
  70. struct bootcount_spi_flash_priv *priv = dev_get_priv(dev);
  71. struct udevice *spi_flash;
  72. if (dev_read_phandle_with_args(dev, "spi-flash", NULL, 0, 0, &phandle_args)) {
  73. debug("%s: spi-flash backing device not specified\n", dev->name);
  74. return -ENOENT;
  75. }
  76. if (uclass_get_device_by_ofnode(UCLASS_SPI_FLASH, phandle_args.node, &spi_flash)) {
  77. debug("%s: could not get backing device\n", dev->name);
  78. return -ENODEV;
  79. }
  80. priv->spi_flash = spi_flash;
  81. priv->offset = dev_read_u32_default(dev, "offset", 0);
  82. return 0;
  83. }
  84. static const struct bootcount_ops bootcount_spi_flash_ops = {
  85. .get = bootcount_spi_flash_get,
  86. .set = bootcount_spi_flash_set,
  87. };
  88. static const struct udevice_id bootcount_spi_flash_ids[] = {
  89. { .compatible = "u-boot,bootcount-spi-flash" },
  90. { }
  91. };
  92. U_BOOT_DRIVER(bootcount_spi_flash) = {
  93. .name = "bootcount-spi-flash",
  94. .id = UCLASS_BOOTCOUNT,
  95. .priv_auto = sizeof(struct bootcount_spi_flash_priv),
  96. .probe = bootcount_spi_flash_probe,
  97. .of_match = bootcount_spi_flash_ids,
  98. .ops = &bootcount_spi_flash_ops,
  99. };