flash.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * (C) Copyright 2015
  3. * Kamil Lulko, <kamil.lulko@gmail.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <asm/io.h>
  9. #include <asm/arch/stm32.h>
  10. #define STM32_FLASH_KEY1 0x45670123
  11. #define STM32_FLASH_KEY2 0xCDEF89AB
  12. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  13. const u32 sect_sz_kb[CONFIG_SYS_MAX_FLASH_SECT] = {
  14. [0 ... 3] = 16 * 1024,
  15. [4] = 64 * 1024,
  16. [5 ... 11] = 128 * 1024
  17. };
  18. static void stm32f4_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_STM32F4;
  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_STM32F4) {
  52. printf("STM32F4 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. stm32f4_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. stm32f4_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. stm32f4_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. stm32f4_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. while (readl(&STM32_FLASH->sr) & STM32_FLASH_SR_BSY)
  115. ;
  116. }
  117. clrbits_le32(&STM32_FLASH->cr, STM32_FLASH_CR_PG);
  118. stm32f4_flash_lock(1);
  119. return 0;
  120. }