hsdramc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2005-2006 Atmel Corporation
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #ifdef CFG_HSDRAMC
  24. #include <asm/io.h>
  25. #include <asm/sdram.h>
  26. #include <asm/arch/platform.h>
  27. #include "hsdramc1.h"
  28. struct hsdramc {
  29. const struct device *hebi;
  30. void *regs;
  31. };
  32. static struct hsdramc hsdramc;
  33. unsigned long sdram_init(const struct sdram_info *info)
  34. {
  35. unsigned long *sdram = (unsigned long *)uncached(info->phys_addr);
  36. unsigned long sdram_size;
  37. unsigned long tmp;
  38. unsigned long bus_hz;
  39. unsigned int i;
  40. hsdramc.hebi = get_device(DEVICE_HEBI);
  41. if (!hsdramc.hebi)
  42. return 0;
  43. /* FIXME: Both of these lines are complete hacks */
  44. hsdramc.regs = hsdramc.hebi->regs + 0x400;
  45. bus_hz = pm_get_clock_freq(hsdramc.hebi->resource[0].u.clock.id);
  46. cpu_enable_sdram();
  47. tmp = (HSDRAMC1_BF(NC, info->col_bits - 8)
  48. | HSDRAMC1_BF(NR, info->row_bits - 11)
  49. | HSDRAMC1_BF(NB, info->bank_bits - 1)
  50. | HSDRAMC1_BF(CAS, info->cas)
  51. | HSDRAMC1_BF(TWR, info->twr)
  52. | HSDRAMC1_BF(TRC, info->trc)
  53. | HSDRAMC1_BF(TRP, info->trp)
  54. | HSDRAMC1_BF(TRCD, info->trcd)
  55. | HSDRAMC1_BF(TRAS, info->tras)
  56. | HSDRAMC1_BF(TXSR, info->txsr));
  57. #ifdef CFG_SDRAM_16BIT
  58. tmp |= HSDRAMC1_BIT(DBW);
  59. sdram_size = 1 << (info->row_bits + info->col_bits
  60. + info->bank_bits + 1);
  61. #else
  62. sdram_size = 1 << (info->row_bits + info->col_bits
  63. + info->bank_bits + 2);
  64. #endif
  65. hsdramc1_writel(&hsdramc, CR, tmp);
  66. /*
  67. * Initialization sequence for SDRAM, from the data sheet:
  68. *
  69. * 1. A minimum pause of 200 us is provided to precede any
  70. * signal toggle.
  71. */
  72. udelay(200);
  73. /*
  74. * 2. A Precharge All command is issued to the SDRAM
  75. */
  76. hsdramc1_writel(&hsdramc, MR, HSDRAMC1_MODE_BANKS_PRECHARGE);
  77. hsdramc1_readl(&hsdramc, MR);
  78. writel(0, sdram);
  79. /*
  80. * 3. Eight auto-refresh (CBR) cycles are provided
  81. */
  82. hsdramc1_writel(&hsdramc, MR, HSDRAMC1_MODE_AUTO_REFRESH);
  83. hsdramc1_readl(&hsdramc, MR);
  84. for (i = 0; i < 8; i++)
  85. writel(0, sdram);
  86. /*
  87. * 4. A mode register set (MRS) cycle is issued to program
  88. * SDRAM parameters, in particular CAS latency and burst
  89. * length.
  90. *
  91. * CAS from info struct, burst length 1, serial burst type
  92. */
  93. hsdramc1_writel(&hsdramc, MR, HSDRAMC1_MODE_LOAD_MODE);
  94. hsdramc1_readl(&hsdramc, MR);
  95. writel(0, sdram + (info->cas << 4));
  96. /*
  97. * 5. A Normal Mode command is provided, 3 clocks after tMRD
  98. * is met.
  99. *
  100. * From the timing diagram, it looks like tMRD is 3
  101. * cycles...try a dummy read from the peripheral bus.
  102. */
  103. hsdramc1_readl(&hsdramc, MR);
  104. hsdramc1_writel(&hsdramc, MR, HSDRAMC1_MODE_NORMAL);
  105. hsdramc1_readl(&hsdramc, MR);
  106. writel(0, sdram);
  107. /*
  108. * 6. Write refresh rate into SDRAMC refresh timer count
  109. * register (refresh rate = timing between refresh cycles).
  110. *
  111. * 15.6 us is a typical value for a burst of length one
  112. */
  113. hsdramc1_writel(&hsdramc, TR, (156 * (bus_hz / 1000)) / 10000);
  114. printf("SDRAM: %u MB at address 0x%08lx\n",
  115. sdram_size >> 20, info->phys_addr);
  116. printf("Testing SDRAM...");
  117. for (i = 0; i < sdram_size / 4; i++)
  118. sdram[i] = i;
  119. for (i = 0; i < sdram_size / 4; i++) {
  120. tmp = sdram[i];
  121. if (tmp != i) {
  122. printf("FAILED at address 0x%08lx\n",
  123. info->phys_addr + i * 4);
  124. printf("SDRAM: read 0x%lx, expected 0x%lx\n", tmp, i);
  125. return 0;
  126. }
  127. }
  128. puts("OK\n");
  129. return sdram_size;
  130. }
  131. #endif /* CFG_HSDRAMC */