flash.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Modified 4/5/2001
  9. * Wait for completion of each sector erase command issued
  10. * 4/5/2001
  11. * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
  12. */
  13. #include <common.h>
  14. #include <asm/ppc4xx.h>
  15. #include <asm/processor.h>
  16. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
  17. #undef DEBUG
  18. #ifdef DEBUG
  19. #define DEBUGF(x...) printf(x)
  20. #else
  21. #define DEBUGF(x...)
  22. #endif /* DEBUG */
  23. /*
  24. * include common flash code (for amcc boards)
  25. */
  26. #include "../common/flash.c"
  27. /*-----------------------------------------------------------------------
  28. * Functions
  29. */
  30. static ulong flash_get_size(vu_long * addr, flash_info_t * info);
  31. static void flash_get_offsets(ulong base, flash_info_t * info);
  32. unsigned long flash_init(void)
  33. {
  34. unsigned long size_b0, size_b1;
  35. int i;
  36. uint pbcr;
  37. unsigned long base_b0, base_b1;
  38. /* Init: no FLASHes known */
  39. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
  40. flash_info[i].flash_id = FLASH_UNKNOWN;
  41. }
  42. /* Static FLASH Bank configuration here - FIXME XXX */
  43. size_b0 =
  44. flash_get_size((vu_long *) FLASH_BASE0_PRELIM, &flash_info[0]);
  45. if (flash_info[0].flash_id == FLASH_UNKNOWN) {
  46. printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
  47. size_b0, size_b0 << 20);
  48. }
  49. /* Only one bank */
  50. if (CONFIG_SYS_MAX_FLASH_BANKS == 1) {
  51. /* Setup offsets */
  52. flash_get_offsets(FLASH_BASE0_PRELIM, &flash_info[0]);
  53. /* Monitor protection ON by default */
  54. (void)flash_protect(FLAG_PROTECT_SET,
  55. CONFIG_SYS_MONITOR_BASE,
  56. CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN - 1,
  57. &flash_info[0]);
  58. #ifdef CONFIG_ENV_IS_IN_FLASH
  59. (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR,
  60. CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
  61. &flash_info[0]);
  62. (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR_REDUND,
  63. CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
  64. &flash_info[0]);
  65. #endif
  66. size_b1 = 0;
  67. flash_info[0].size = size_b0;
  68. }
  69. /* 2 banks */
  70. else {
  71. size_b1 =
  72. flash_get_size((vu_long *) FLASH_BASE1_PRELIM,
  73. &flash_info[1]);
  74. /* Re-do sizing to get full correct info */
  75. if (size_b1) {
  76. mtdcr(EBC0_CFGADDR, PB0CR);
  77. pbcr = mfdcr(EBC0_CFGDATA);
  78. mtdcr(EBC0_CFGADDR, PB0CR);
  79. base_b1 = -size_b1;
  80. pbcr = (pbcr & 0x0001ffff) | base_b1 |
  81. (((size_b1 / 1024 / 1024) - 1) << 17);
  82. mtdcr(EBC0_CFGDATA, pbcr);
  83. /* printf("PB1CR = %x\n", pbcr); */
  84. }
  85. if (size_b0) {
  86. mtdcr(EBC0_CFGADDR, PB1CR);
  87. pbcr = mfdcr(EBC0_CFGDATA);
  88. mtdcr(EBC0_CFGADDR, PB1CR);
  89. base_b0 = base_b1 - size_b0;
  90. pbcr = (pbcr & 0x0001ffff) | base_b0 |
  91. (((size_b0 / 1024 / 1024) - 1) << 17);
  92. mtdcr(EBC0_CFGDATA, pbcr);
  93. /* printf("PB0CR = %x\n", pbcr); */
  94. }
  95. size_b0 = flash_get_size((vu_long *) base_b0, &flash_info[0]);
  96. flash_get_offsets(base_b0, &flash_info[0]);
  97. /* monitor protection ON by default */
  98. (void)flash_protect(FLAG_PROTECT_SET,
  99. base_b0 + size_b0 - CONFIG_SYS_MONITOR_LEN,
  100. base_b0 + size_b0 - 1, &flash_info[0]);
  101. /* Also protect sector containing initial power-up instruction */
  102. /* (flash_protect() checks address range - other call ignored) */
  103. (void)flash_protect(FLAG_PROTECT_SET,
  104. 0xFFFFFFFC, 0xFFFFFFFF, &flash_info[0]);
  105. (void)flash_protect(FLAG_PROTECT_SET,
  106. 0xFFFFFFFC, 0xFFFFFFFF, &flash_info[1]);
  107. if (size_b1) {
  108. /* Re-do sizing to get full correct info */
  109. size_b1 =
  110. flash_get_size((vu_long *) base_b1, &flash_info[1]);
  111. flash_get_offsets(base_b1, &flash_info[1]);
  112. /* monitor protection ON by default */
  113. (void)flash_protect(FLAG_PROTECT_SET,
  114. base_b1 + size_b1 - CONFIG_SYS_MONITOR_LEN,
  115. base_b1 + size_b1 - 1,
  116. &flash_info[1]);
  117. /* monitor protection OFF by default (one is enough) */
  118. (void)flash_protect(FLAG_PROTECT_CLEAR,
  119. base_b0 + size_b0 - CONFIG_SYS_MONITOR_LEN,
  120. base_b0 + size_b0 - 1,
  121. &flash_info[0]);
  122. } else {
  123. flash_info[1].flash_id = FLASH_UNKNOWN;
  124. flash_info[1].sector_count = -1;
  125. }
  126. flash_info[0].size = size_b0;
  127. flash_info[1].size = size_b1;
  128. } /* else 2 banks */
  129. return (size_b0 + size_b1);
  130. }
  131. static void flash_get_offsets(ulong base, flash_info_t * info)
  132. {
  133. int i;
  134. /* set up sector start address table */
  135. if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
  136. (info->flash_id == FLASH_AM040)) {
  137. for (i = 0; i < info->sector_count; i++)
  138. info->start[i] = base + (i * 0x00010000);
  139. } else {
  140. if (info->flash_id & FLASH_BTYPE) {
  141. /* set sector offsets for bottom boot block type */
  142. info->start[0] = base + 0x00000000;
  143. info->start[1] = base + 0x00004000;
  144. info->start[2] = base + 0x00006000;
  145. info->start[3] = base + 0x00008000;
  146. for (i = 4; i < info->sector_count; i++) {
  147. info->start[i] =
  148. base + (i * 0x00010000) - 0x00030000;
  149. }
  150. } else {
  151. /* set sector offsets for top boot block type */
  152. i = info->sector_count - 1;
  153. info->start[i--] = base + info->size - 0x00004000;
  154. info->start[i--] = base + info->size - 0x00006000;
  155. info->start[i--] = base + info->size - 0x00008000;
  156. for (; i >= 0; i--) {
  157. info->start[i] = base + i * 0x00010000;
  158. }
  159. }
  160. }
  161. }