hsdramc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include <asm/io.h>
  24. #include <asm/sdram.h>
  25. #include <asm/arch/clk.h>
  26. #include <asm/arch/memory-map.h>
  27. #include "hsdramc1.h"
  28. unsigned long sdram_init(void *sdram_base, const struct sdram_config *config)
  29. {
  30. unsigned long sdram_size;
  31. uint32_t cfgreg;
  32. unsigned int i;
  33. cfgreg = (HSDRAMC1_BF(NC, config->col_bits - 8)
  34. | HSDRAMC1_BF(NR, config->row_bits - 11)
  35. | HSDRAMC1_BF(NB, config->bank_bits - 1)
  36. | HSDRAMC1_BF(CAS, config->cas)
  37. | HSDRAMC1_BF(TWR, config->twr)
  38. | HSDRAMC1_BF(TRC, config->trc)
  39. | HSDRAMC1_BF(TRP, config->trp)
  40. | HSDRAMC1_BF(TRCD, config->trcd)
  41. | HSDRAMC1_BF(TRAS, config->tras)
  42. | HSDRAMC1_BF(TXSR, config->txsr));
  43. if (config->data_bits == SDRAM_DATA_16BIT)
  44. cfgreg |= HSDRAMC1_BIT(DBW);
  45. hsdramc1_writel(CR, cfgreg);
  46. /* Send a NOP to turn on the clock (necessary on some chips) */
  47. hsdramc1_writel(MR, HSDRAMC1_MODE_NOP);
  48. hsdramc1_readl(MR);
  49. writel(0, sdram_base);
  50. /*
  51. * Initialization sequence for SDRAM, from the data sheet:
  52. *
  53. * 1. A minimum pause of 200 us is provided to precede any
  54. * signal toggle.
  55. */
  56. udelay(200);
  57. /*
  58. * 2. A Precharge All command is issued to the SDRAM
  59. */
  60. hsdramc1_writel(MR, HSDRAMC1_MODE_BANKS_PRECHARGE);
  61. hsdramc1_readl(MR);
  62. writel(0, sdram_base);
  63. /*
  64. * 3. Eight auto-refresh (CBR) cycles are provided
  65. */
  66. hsdramc1_writel(MR, HSDRAMC1_MODE_AUTO_REFRESH);
  67. hsdramc1_readl(MR);
  68. for (i = 0; i < 8; i++)
  69. writel(0, sdram_base);
  70. /*
  71. * 4. A mode register set (MRS) cycle is issued to program
  72. * SDRAM parameters, in particular CAS latency and burst
  73. * length.
  74. *
  75. * The address will be chosen by the SDRAMC automatically; we
  76. * just have to make sure BA[1:0] are set to 0.
  77. */
  78. hsdramc1_writel(MR, HSDRAMC1_MODE_LOAD_MODE);
  79. hsdramc1_readl(MR);
  80. writel(0, sdram_base);
  81. /*
  82. * 5. The application must go into Normal Mode, setting Mode
  83. * to 0 in the Mode Register and performing a write access
  84. * at any location in the SDRAM.
  85. */
  86. hsdramc1_writel(MR, HSDRAMC1_MODE_NORMAL);
  87. hsdramc1_readl(MR);
  88. writel(0, sdram_base);
  89. /*
  90. * 6. Write refresh rate into SDRAMC refresh timer count
  91. * register (refresh rate = timing between refresh cycles).
  92. */
  93. hsdramc1_writel(TR, config->refresh_period);
  94. if (config->data_bits == SDRAM_DATA_16BIT)
  95. sdram_size = 1 << (config->row_bits + config->col_bits
  96. + config->bank_bits + 1);
  97. else
  98. sdram_size = 1 << (config->row_bits + config->col_bits
  99. + config->bank_bits + 2);
  100. return sdram_size;
  101. }