dram.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * (C) Copyright 2009
  3. * Marvell Semiconductor <www.marvell.com>
  4. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. * MA 02110-1301 USA
  23. */
  24. #include <config.h>
  25. #include <common.h>
  26. #include <asm/io.h>
  27. #include <asm/arch/cpu.h>
  28. #include <asm/arch/kirkwood.h>
  29. DECLARE_GLOBAL_DATA_PTR;
  30. struct kw_sdram_bank {
  31. u32 win_bar;
  32. u32 win_sz;
  33. };
  34. struct kw_sdram_addr_dec {
  35. struct kw_sdram_bank sdram_bank[4];
  36. };
  37. #define KW_REG_CPUCS_WIN_ENABLE (1 << 0)
  38. #define KW_REG_CPUCS_WIN_WR_PROTECT (1 << 1)
  39. #define KW_REG_CPUCS_WIN_WIN0_CS(x) (((x) & 0x3) << 2)
  40. #define KW_REG_CPUCS_WIN_SIZE(x) (((x) & 0xff) << 24)
  41. /*
  42. * kw_sdram_bar - reads SDRAM Base Address Register
  43. */
  44. u32 kw_sdram_bar(enum memory_bank bank)
  45. {
  46. struct kw_sdram_addr_dec *base =
  47. (struct kw_sdram_addr_dec *)KW_REGISTER(0x1500);
  48. u32 result = 0;
  49. u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
  50. if ((!enable) || (bank > BANK3))
  51. return 0;
  52. result = readl(&base->sdram_bank[bank].win_bar);
  53. return result;
  54. }
  55. /*
  56. * kw_sdram_bs_set - writes SDRAM Bank size
  57. */
  58. static void kw_sdram_bs_set(enum memory_bank bank, u32 size)
  59. {
  60. struct kw_sdram_addr_dec *base =
  61. (struct kw_sdram_addr_dec *)KW_REGISTER(0x1500);
  62. /* Read current register value */
  63. u32 reg = readl(&base->sdram_bank[bank].win_sz);
  64. /* Clear window size */
  65. reg &= ~KW_REG_CPUCS_WIN_SIZE(0xFF);
  66. /* Set new window size */
  67. reg |= KW_REG_CPUCS_WIN_SIZE((size - 1) >> 24);
  68. writel(reg, &base->sdram_bank[bank].win_sz);
  69. }
  70. /*
  71. * kw_sdram_bs - reads SDRAM Bank size
  72. */
  73. u32 kw_sdram_bs(enum memory_bank bank)
  74. {
  75. struct kw_sdram_addr_dec *base =
  76. (struct kw_sdram_addr_dec *)KW_REGISTER(0x1500);
  77. u32 result = 0;
  78. u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
  79. if ((!enable) || (bank > BANK3))
  80. return 0;
  81. result = 0xff000000 & readl(&base->sdram_bank[bank].win_sz);
  82. result += 0x01000000;
  83. return result;
  84. }
  85. void kw_sdram_size_adjust(enum memory_bank bank)
  86. {
  87. u32 size;
  88. /* probe currently equipped RAM size */
  89. size = get_ram_size((void *)kw_sdram_bar(bank), kw_sdram_bs(bank));
  90. /* adjust SDRAM window size accordingly */
  91. kw_sdram_bs_set(bank, size);
  92. }
  93. #ifndef CONFIG_SYS_BOARD_DRAM_INIT
  94. int dram_init(void)
  95. {
  96. int i;
  97. gd->ram_size = 0;
  98. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  99. gd->bd->bi_dram[i].start = kw_sdram_bar(i);
  100. gd->bd->bi_dram[i].size = kw_sdram_bs(i);
  101. /*
  102. * It is assumed that all memory banks are consecutive
  103. * and without gaps.
  104. * If the gap is found, ram_size will be reported for
  105. * consecutive memory only
  106. */
  107. if (gd->bd->bi_dram[i].start != gd->ram_size)
  108. break;
  109. gd->ram_size += gd->bd->bi_dram[i].size;
  110. }
  111. for (; i < CONFIG_NR_DRAM_BANKS; i++) {
  112. /* If above loop terminated prematurely, we need to set
  113. * remaining banks' start address & size as 0. Otherwise other
  114. * u-boot functions and Linux kernel gets wrong values which
  115. * could result in crash */
  116. gd->bd->bi_dram[i].start = 0;
  117. gd->bd->bi_dram[i].size = 0;
  118. }
  119. return 0;
  120. }
  121. /*
  122. * If this function is not defined here,
  123. * board.c alters dram bank zero configuration defined above.
  124. */
  125. void dram_init_banksize(void)
  126. {
  127. dram_init();
  128. }
  129. #endif /* CONFIG_SYS_BOARD_DRAM_INIT */