flash.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * (C) Copyright 2015
  3. * Kamil Lulko, <kamil.lulko@gmail.com>
  4. *
  5. * Copyright 2015 ATS Advanced Telematics Systems GmbH
  6. * Copyright 2015 Konsulko Group, Matt Porter <mporter@konsulko.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <asm/io.h>
  12. #include <asm/arch/stm32.h>
  13. #define STM32_FLASH_KEY1 0x45670123
  14. #define STM32_FLASH_KEY2 0xcdef89ab
  15. #define STM32_NUM_BANKS 2
  16. #define STM32_MAX_BANK 0x200
  17. flash_info_t flash_info[STM32_NUM_BANKS];
  18. static struct stm32_flash_bank_regs *flash_bank[STM32_NUM_BANKS];
  19. static void stm32f1_flash_lock(u8 bank, u8 lock)
  20. {
  21. if (lock) {
  22. setbits_le32(&flash_bank[bank]->cr, STM32_FLASH_CR_LOCK);
  23. } else {
  24. writel(STM32_FLASH_KEY1, &flash_bank[bank]->keyr);
  25. writel(STM32_FLASH_KEY2, &flash_bank[bank]->keyr);
  26. }
  27. }
  28. /* Only XL devices are supported (2 KiB sector size) */
  29. unsigned long flash_init(void)
  30. {
  31. u8 i, banks;
  32. u16 j, size;
  33. /* Set up accessors for XL devices with wonky register layout */
  34. flash_bank[0] = (struct stm32_flash_bank_regs *)&STM32_FLASH->keyr;
  35. flash_bank[1] = (struct stm32_flash_bank_regs *)&STM32_FLASH->keyr2;
  36. /*
  37. * Get total flash size (in KiB) and configure number of banks
  38. * present and sector count per bank.
  39. */
  40. size = readw(&STM32_DES->flash_size);
  41. if (size <= STM32_MAX_BANK) {
  42. banks = 1;
  43. flash_info[0].sector_count = size >> 1;
  44. } else if (size > STM32_MAX_BANK) {
  45. banks = 2;
  46. flash_info[0].sector_count = STM32_MAX_BANK >> 1;
  47. flash_info[1].sector_count = (size - STM32_MAX_BANK) >> 1;
  48. }
  49. /* Configure start/size for all sectors */
  50. for (i = 0; i < banks; i++) {
  51. flash_info[i].flash_id = FLASH_STM32F1;
  52. flash_info[i].start[0] = CONFIG_SYS_FLASH_BASE + (i << 19);
  53. flash_info[i].size = 2048;
  54. for (j = 1; (j < flash_info[i].sector_count); j++) {
  55. flash_info[i].start[j] = flash_info[i].start[j - 1]
  56. + 2048;
  57. flash_info[i].size += 2048;
  58. }
  59. }
  60. return size << 10;
  61. }
  62. void flash_print_info(flash_info_t *info)
  63. {
  64. int i;
  65. if (info->flash_id == FLASH_UNKNOWN) {
  66. printf("Missing or unknown FLASH type\n");
  67. return;
  68. } else if (info->flash_id == FLASH_STM32F1) {
  69. printf("STM32F1 Embedded Flash\n");
  70. }
  71. printf(" Size: %ld MB in %d Sectors\n",
  72. info->size >> 10, info->sector_count);
  73. printf(" Sector Start Addresses:");
  74. for (i = 0; i < info->sector_count; ++i) {
  75. if ((i % 5) == 0)
  76. printf("\n ");
  77. printf(" %08lX%s",
  78. info->start[i],
  79. info->protect[i] ? " (RO)" : " ");
  80. }
  81. printf("\n");
  82. return;
  83. }
  84. int flash_erase(flash_info_t *info, int first, int last)
  85. {
  86. u8 bank = 0xff;
  87. int i;
  88. for (i = 0; i < STM32_NUM_BANKS; i++) {
  89. if (info == &flash_info[i]) {
  90. bank = i;
  91. break;
  92. }
  93. }
  94. if (bank == 0xff)
  95. return -1;
  96. stm32f1_flash_lock(bank, 0);
  97. for (i = first; i <= last; i++) {
  98. while (readl(&flash_bank[bank]->sr) & STM32_FLASH_SR_BSY)
  99. ;
  100. setbits_le32(&flash_bank[bank]->cr, STM32_FLASH_CR_PER);
  101. writel(info->start[i], &flash_bank[bank]->ar);
  102. setbits_le32(&flash_bank[bank]->cr, STM32_FLASH_CR_STRT);
  103. while (readl(&flash_bank[bank]->sr) & STM32_FLASH_SR_BSY)
  104. ;
  105. }
  106. clrbits_le32(&flash_bank[bank]->cr, STM32_FLASH_CR_PER);
  107. stm32f1_flash_lock(bank, 1);
  108. return 0;
  109. }
  110. int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
  111. {
  112. ulong i;
  113. u8 bank = 0xff;
  114. if (addr & 1) {
  115. printf("Flash address must be half word aligned\n");
  116. return -1;
  117. }
  118. if (cnt & 1) {
  119. printf("Flash length must be half word aligned\n");
  120. return -1;
  121. }
  122. for (i = 0; i < 2; i++) {
  123. if (info == &flash_info[i]) {
  124. bank = i;
  125. break;
  126. }
  127. }
  128. if (bank == 0xff)
  129. return -1;
  130. while (readl(&flash_bank[bank]->sr) & STM32_FLASH_SR_BSY)
  131. ;
  132. stm32f1_flash_lock(bank, 0);
  133. setbits_le32(&flash_bank[bank]->cr, STM32_FLASH_CR_PG);
  134. /* STM32F1 requires half word writes */
  135. for (i = 0; i < cnt >> 1; i++) {
  136. *(u16 *)(addr + i * 2) = ((u16 *)src)[i];
  137. while (readl(&flash_bank[bank]->sr) & STM32_FLASH_SR_BSY)
  138. ;
  139. }
  140. clrbits_le32(&flash_bank[bank]->cr, STM32_FLASH_CR_PG);
  141. stm32f1_flash_lock(bank, 1);
  142. return 0;
  143. }