dram.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * LPC32xx dram init
  4. *
  5. * (C) Copyright 2014 DENX Software Engineering GmbH
  6. * Written-by: Albert ARIBAUD <albert.aribaud@3adev.fr>
  7. *
  8. * This is called by SPL to gain access to the SDR DRAM.
  9. *
  10. * This code runs from SRAM.
  11. *
  12. * Actual CONFIG_LPC32XX_SDRAM_* parameters must be provided
  13. * by the board configuration file.
  14. */
  15. #include <common.h>
  16. #include <netdev.h>
  17. #include <asm/arch/cpu.h>
  18. #include <asm/arch/clk.h>
  19. #include <asm/arch/wdt.h>
  20. #include <asm/arch/emc.h>
  21. #include <asm/io.h>
  22. static struct clk_pm_regs *clk = (struct clk_pm_regs *)CLK_PM_BASE;
  23. static struct emc_regs *emc = (struct emc_regs *)EMC_BASE;
  24. void ddr_init(struct emc_dram_settings *dram)
  25. {
  26. uint32_t ck;
  27. /* Enable EMC interface and choose little endian mode */
  28. writel(1, &emc->ctrl);
  29. writel(0, &emc->config);
  30. /* Select maximum EMC Dynamic Memory Refresh Time */
  31. writel(0x7FF, &emc->refresh);
  32. /* Determine CLK */
  33. ck = get_sdram_clk_rate();
  34. /* Configure SDRAM */
  35. writel(dram->cmddelay, &clk->sdramclk_ctrl);
  36. writel(dram->config0, &emc->config0);
  37. writel(dram->rascas0, &emc->rascas0);
  38. writel(dram->rdconfig, &emc->read_config);
  39. /* Set timings */
  40. writel((ck / dram->trp) & 0x0000000F, &emc->t_rp);
  41. writel((ck / dram->tras) & 0x0000000F, &emc->t_ras);
  42. writel((ck / dram->tsrex) & 0x0000007F, &emc->t_srex);
  43. writel((ck / dram->twr) & 0x0000000F, &emc->t_wr);
  44. writel((ck / dram->trc) & 0x0000001F, &emc->t_rc);
  45. writel((ck / dram->trfc) & 0x0000001F, &emc->t_rfc);
  46. writel((ck / dram->txsr) & 0x000000FF, &emc->t_xsr);
  47. writel(dram->trrd, &emc->t_rrd);
  48. writel(dram->tmrd, &emc->t_mrd);
  49. writel(dram->tcdlr, &emc->t_cdlr);
  50. /* Dynamic refresh */
  51. writel((((ck / dram->refresh) >> 4) & 0x7FF), &emc->refresh);
  52. udelay(10);
  53. /* Force all clocks, enable inverted ck, issue NOP command */
  54. writel(0x00000193, &emc->control);
  55. udelay(100);
  56. /* Keep all clocks enabled, issue a PRECHARGE ALL command */
  57. writel(0x00000113, &emc->control);
  58. /* Fast dynamic refresh for at least a few SDRAM ck cycles */
  59. writel((((128) >> 4) & 0x7FF), &emc->refresh);
  60. udelay(10);
  61. /* set correct dynamic refresh timing */
  62. writel((((ck / dram->refresh) >> 4) & 0x7FF), &emc->refresh);
  63. udelay(10);
  64. /* set normal mode to CAS=3 */
  65. writel(0x00000093, &emc->control);
  66. readl(EMC_DYCS0_BASE | dram->mode);
  67. /* set extended mode to all zeroes */
  68. writel(0x00000093, &emc->control);
  69. readl(EMC_DYCS0_BASE | dram->emode);
  70. /* stop forcing clocks, keep inverted clock, issue normal mode */
  71. writel(0x00000010, &emc->control);
  72. }