mrccache.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * From Coreboot src/southbridge/intel/bd82x6x/mrccache.c
  3. *
  4. * Copyright (C) 2014 Google Inc.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0
  7. */
  8. #include <common.h>
  9. #include <errno.h>
  10. #include <fdtdec.h>
  11. #include <net.h>
  12. #include <spi.h>
  13. #include <spi_flash.h>
  14. #include <asm/mrccache.h>
  15. static struct mrc_data_container *next_mrc_block(
  16. struct mrc_data_container *mrc_cache)
  17. {
  18. /* MRC data blocks are aligned within the region */
  19. u32 mrc_size = sizeof(*mrc_cache) + mrc_cache->data_size;
  20. if (mrc_size & (MRC_DATA_ALIGN - 1UL)) {
  21. mrc_size &= ~(MRC_DATA_ALIGN - 1UL);
  22. mrc_size += MRC_DATA_ALIGN;
  23. }
  24. u8 *region_ptr = (u8 *)mrc_cache;
  25. region_ptr += mrc_size;
  26. return (struct mrc_data_container *)region_ptr;
  27. }
  28. static int is_mrc_cache(struct mrc_data_container *cache)
  29. {
  30. return cache && (cache->signature == MRC_DATA_SIGNATURE);
  31. }
  32. /*
  33. * Find the largest index block in the MRC cache. Return NULL if none is
  34. * found.
  35. */
  36. struct mrc_data_container *mrccache_find_current(struct fmap_entry *entry)
  37. {
  38. struct mrc_data_container *cache, *next;
  39. ulong base_addr, end_addr;
  40. uint id;
  41. base_addr = (1ULL << 32) - CONFIG_ROM_SIZE + entry->offset;
  42. end_addr = base_addr + entry->length;
  43. cache = NULL;
  44. /* Search for the last filled entry in the region */
  45. for (id = 0, next = (struct mrc_data_container *)base_addr;
  46. is_mrc_cache(next);
  47. id++) {
  48. cache = next;
  49. next = next_mrc_block(next);
  50. if ((ulong)next >= end_addr)
  51. break;
  52. }
  53. if (id-- == 0) {
  54. debug("%s: No valid MRC cache found.\n", __func__);
  55. return NULL;
  56. }
  57. /* Verify checksum */
  58. if (cache->checksum != compute_ip_checksum(cache->data,
  59. cache->data_size)) {
  60. printf("%s: MRC cache checksum mismatch\n", __func__);
  61. return NULL;
  62. }
  63. debug("%s: picked entry %u from cache block\n", __func__, id);
  64. return cache;
  65. }
  66. /**
  67. * find_next_mrc_cache() - get next cache entry
  68. *
  69. * @entry: MRC cache flash area
  70. * @cache: Entry to start from
  71. *
  72. * @return next cache entry if found, NULL if we got to the end
  73. */
  74. static struct mrc_data_container *find_next_mrc_cache(struct fmap_entry *entry,
  75. struct mrc_data_container *cache)
  76. {
  77. ulong base_addr, end_addr;
  78. base_addr = (1ULL << 32) - CONFIG_ROM_SIZE + entry->offset;
  79. end_addr = base_addr + entry->length;
  80. cache = next_mrc_block(cache);
  81. if ((ulong)cache >= end_addr) {
  82. /* Crossed the boundary */
  83. cache = NULL;
  84. debug("%s: no available entries found\n", __func__);
  85. } else {
  86. debug("%s: picked next entry from cache block at %p\n",
  87. __func__, cache);
  88. }
  89. return cache;
  90. }
  91. int mrccache_update(struct udevice *sf, struct fmap_entry *entry,
  92. struct mrc_data_container *cur)
  93. {
  94. struct mrc_data_container *cache;
  95. ulong offset;
  96. ulong base_addr;
  97. int ret;
  98. if (!is_mrc_cache(cur))
  99. return -EINVAL;
  100. /* Find the last used block */
  101. base_addr = (1ULL << 32) - CONFIG_ROM_SIZE + entry->offset;
  102. debug("Updating MRC cache data\n");
  103. cache = mrccache_find_current(entry);
  104. if (cache && (cache->data_size == cur->data_size) &&
  105. (!memcmp(cache, cur, cache->data_size + sizeof(*cur)))) {
  106. debug("MRC data in flash is up to date. No update\n");
  107. return -EEXIST;
  108. }
  109. /* Move to the next block, which will be the first unused block */
  110. if (cache)
  111. cache = find_next_mrc_cache(entry, cache);
  112. /*
  113. * If we have got to the end, erase the entire mrc-cache area and start
  114. * again at block 0.
  115. */
  116. if (!cache) {
  117. debug("Erasing the MRC cache region of %x bytes at %x\n",
  118. entry->length, entry->offset);
  119. ret = spi_flash_erase_dm(sf, entry->offset, entry->length);
  120. if (ret) {
  121. debug("Failed to erase flash region\n");
  122. return ret;
  123. }
  124. cache = (struct mrc_data_container *)base_addr;
  125. }
  126. /* Write the data out */
  127. offset = (ulong)cache - base_addr + entry->offset;
  128. debug("Write MRC cache update to flash at %lx\n", offset);
  129. ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur),
  130. cur);
  131. if (ret) {
  132. debug("Failed to write to SPI flash\n");
  133. return ret;
  134. }
  135. return 0;
  136. }