incaip.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * (C) Copyright 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <asm/addrspace.h>
  26. #include <asm/inca-ip.h>
  27. static ulong max_sdram_size(void)
  28. {
  29. /* The only supported SDRAM data width is 16bit.
  30. */
  31. #define CFG_DW 2
  32. /* The only supported number of SDRAM banks is 4.
  33. */
  34. #define CFG_NB 4
  35. ulong cfgpb0 = *INCA_IP_SDRAM_MC_CFGPB0;
  36. int cols = cfgpb0 & 0xF;
  37. int rows = (cfgpb0 & 0xF0) >> 4;
  38. ulong size = (1 << (rows + cols)) * CFG_DW * CFG_NB;
  39. return size;
  40. }
  41. /*
  42. * Check memory range for valid RAM. A simple memory test determines
  43. * the actually available RAM size between addresses `base' and
  44. * `base + maxsize'. Some (not all) hardware errors are detected:
  45. * - short between address lines
  46. * - short between data lines
  47. */
  48. static long int dram_size(long int *base, long int maxsize)
  49. {
  50. volatile long int *addr;
  51. ulong cnt, val;
  52. ulong save[32]; /* to make test non-destructive */
  53. unsigned char i = 0;
  54. for (cnt = (maxsize / sizeof (long)) >> 1; cnt > 0; cnt >>= 1) {
  55. addr = base + cnt; /* pointer arith! */
  56. save[i++] = *addr;
  57. *addr = ~cnt;
  58. }
  59. /* write 0 to base address */
  60. addr = base;
  61. save[i] = *addr;
  62. *addr = 0;
  63. /* check at base address */
  64. if ((val = *addr) != 0) {
  65. *addr = save[i];
  66. return (0);
  67. }
  68. for (cnt = 1; cnt < maxsize / sizeof (long); cnt <<= 1) {
  69. addr = base + cnt; /* pointer arith! */
  70. val = *addr;
  71. *addr = save[--i];
  72. if (val != (~cnt)) {
  73. return (cnt * sizeof (long));
  74. }
  75. }
  76. return (maxsize);
  77. }
  78. long int initdram(int board_type)
  79. {
  80. int rows, cols, best_val = *INCA_IP_SDRAM_MC_CFGPB0;
  81. ulong size, max_size = 0;
  82. ulong our_address;
  83. asm volatile ("move %0, $25" : "=r" (our_address) :);
  84. /* Can't probe for RAM size unless we are running from Flash.
  85. */
  86. if (PHYSADDR(our_address) < PHYSADDR(PHYS_FLASH_1))
  87. {
  88. return max_sdram_size();
  89. }
  90. for (cols = 0x8; cols <= 0xC; cols++)
  91. {
  92. for (rows = 0xB; rows <= 0xD; rows++)
  93. {
  94. *INCA_IP_SDRAM_MC_CFGPB0 = (0x14 << 8) |
  95. (rows << 4) | cols;
  96. size = dram_size((ulong *)CFG_SDRAM_BASE,
  97. max_sdram_size());
  98. if (size > max_size)
  99. {
  100. best_val = *INCA_IP_SDRAM_MC_CFGPB0;
  101. max_size = size;
  102. }
  103. }
  104. }
  105. *INCA_IP_SDRAM_MC_CFGPB0 = best_val;
  106. return max_size;
  107. }