stm32_flash.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015
  4. * Kamil Lulko, <kamil.lulko@gmail.com>
  5. */
  6. #include <common.h>
  7. #include <asm/io.h>
  8. #include <asm/arch/stm32.h>
  9. #include "stm32_flash.h"
  10. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  11. #define STM32_FLASH ((struct stm32_flash_regs *)STM32_FLASH_CNTL_BASE)
  12. void stm32_flash_latency_cfg(int latency)
  13. {
  14. /* 5 wait states, Prefetch enabled, D-Cache enabled, I-Cache enabled */
  15. writel(FLASH_ACR_WS(latency) | FLASH_ACR_PRFTEN | FLASH_ACR_ICEN
  16. | FLASH_ACR_DCEN, &STM32_FLASH->acr);
  17. }
  18. static void stm32_flash_lock(u8 lock)
  19. {
  20. if (lock) {
  21. setbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_LOCK);
  22. } else {
  23. writel(STM32_FLASH_KEY1, &STM32_FLASH->key);
  24. writel(STM32_FLASH_KEY2, &STM32_FLASH->key);
  25. }
  26. }
  27. unsigned long flash_init(void)
  28. {
  29. unsigned long total_size = 0;
  30. u8 i, j;
  31. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  32. flash_info[i].flash_id = FLASH_STM32;
  33. flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  34. flash_info[i].start[0] = CONFIG_SYS_FLASH_BASE + (i << 20);
  35. flash_info[i].size = sect_sz_kb[0];
  36. for (j = 1; j < CONFIG_SYS_MAX_FLASH_SECT; j++) {
  37. flash_info[i].start[j] = flash_info[i].start[j - 1]
  38. + (sect_sz_kb[j - 1]);
  39. flash_info[i].size += sect_sz_kb[j];
  40. }
  41. total_size += flash_info[i].size;
  42. }
  43. return total_size;
  44. }
  45. void flash_print_info(flash_info_t *info)
  46. {
  47. int i;
  48. if (info->flash_id == FLASH_UNKNOWN) {
  49. printf("missing or unknown FLASH type\n");
  50. return;
  51. } else if (info->flash_id == FLASH_STM32) {
  52. printf("stm32 Embedded Flash\n");
  53. }
  54. printf(" Size: %ld MB in %d Sectors\n",
  55. info->size >> 20, info->sector_count);
  56. printf(" Sector Start Addresses:");
  57. for (i = 0; i < info->sector_count; ++i) {
  58. if ((i % 5) == 0)
  59. printf("\n ");
  60. printf(" %08lX%s",
  61. info->start[i],
  62. info->protect[i] ? " (RO)" : " ");
  63. }
  64. printf("\n");
  65. return;
  66. }
  67. int flash_erase(flash_info_t *info, int first, int last)
  68. {
  69. u8 bank = 0xFF;
  70. int i;
  71. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  72. if (info == &flash_info[i]) {
  73. bank = i;
  74. break;
  75. }
  76. }
  77. if (bank == 0xFF)
  78. return -1;
  79. stm32_flash_lock(0);
  80. for (i = first; i <= last; i++) {
  81. while (readl(&STM32_FLASH->sr) & STM32_FLASH_SR_BSY)
  82. ;
  83. /* clear old sector number before writing a new one */
  84. clrbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_SNB_MASK);
  85. if (bank == 0) {
  86. setbits_le32(&STM32_FLASH->cr,
  87. (i << STM32_FLASH_CR_SNB_OFFSET));
  88. } else if (bank == 1) {
  89. setbits_le32(&STM32_FLASH->cr,
  90. ((0x10 | i) << STM32_FLASH_CR_SNB_OFFSET));
  91. } else {
  92. stm32_flash_lock(1);
  93. return -1;
  94. }
  95. setbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_SER);
  96. setbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_STRT);
  97. while (readl(&STM32_FLASH->sr) & STM32_FLASH_SR_BSY)
  98. ;
  99. clrbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_SER);
  100. }
  101. stm32_flash_lock(1);
  102. return 0;
  103. }
  104. int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  105. {
  106. ulong i;
  107. while (readl(&STM32_FLASH->sr) & STM32_FLASH_SR_BSY)
  108. ;
  109. stm32_flash_lock(0);
  110. setbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_PG);
  111. /* To make things simple use byte writes only */
  112. for (i = 0; i < cnt; i++) {
  113. *(uchar *)(addr + i) = src[i];
  114. /* avoid re-ordering flash data write and busy status
  115. * check as flash memory space attributes are generally Normal
  116. */
  117. mb();
  118. while (readl(&STM32_FLASH->sr) & STM32_FLASH_SR_BSY)
  119. ;
  120. }
  121. clrbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_PG);
  122. stm32_flash_lock(1);
  123. return 0;
  124. }