Kumar Gala | 2e6ba7d | 2008-08-26 15:01:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Freescale Semiconductor, Inc. |
| 3 | * |
Tom Rini | d3e6816 | 2018-02-14 21:34:05 -0500 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0 |
Kumar Gala | 2e6ba7d | 2008-08-26 15:01:34 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <asm/io.h> |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 9 | #include <fsl_ddr_sdram.h> |
Kumar Gala | 2e6ba7d | 2008-08-26 15:01:34 -0500 | [diff] [blame] | 10 | |
| 11 | #if (CONFIG_CHIP_SELECTS_PER_CTRL > 4) |
| 12 | #error Invalid setting for CONFIG_CHIP_SELECTS_PER_CTRL |
| 13 | #endif |
| 14 | |
| 15 | void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs, |
York Sun | 5e15555 | 2013-06-25 11:37:48 -0700 | [diff] [blame] | 16 | unsigned int ctrl_num, int step) |
Kumar Gala | 2e6ba7d | 2008-08-26 15:01:34 -0500 | [diff] [blame] | 17 | { |
| 18 | unsigned int i; |
York Sun | a21803d | 2013-11-18 10:29:32 -0800 | [diff] [blame] | 19 | struct ccsr_ddr __iomem *ddr; |
Kumar Gala | 2e6ba7d | 2008-08-26 15:01:34 -0500 | [diff] [blame] | 20 | |
| 21 | switch (ctrl_num) { |
| 22 | case 0: |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 23 | ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR; |
Kumar Gala | 2e6ba7d | 2008-08-26 15:01:34 -0500 | [diff] [blame] | 24 | break; |
| 25 | case 1: |
York Sun | f062659 | 2013-09-30 09:22:09 -0700 | [diff] [blame] | 26 | ddr = (void *)CONFIG_SYS_FSL_DDR2_ADDR; |
Kumar Gala | 2e6ba7d | 2008-08-26 15:01:34 -0500 | [diff] [blame] | 27 | break; |
| 28 | default: |
| 29 | printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num); |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) { |
| 34 | if (i == 0) { |
| 35 | out_be32(&ddr->cs0_bnds, regs->cs[i].bnds); |
Kumar Gala | 2e6ba7d | 2008-08-26 15:01:34 -0500 | [diff] [blame] | 36 | out_be32(&ddr->cs0_config, regs->cs[i].config); |
| 37 | |
| 38 | } else if (i == 1) { |
| 39 | out_be32(&ddr->cs1_bnds, regs->cs[i].bnds); |
| 40 | out_be32(&ddr->cs1_config, regs->cs[i].config); |
| 41 | |
| 42 | } else if (i == 2) { |
| 43 | out_be32(&ddr->cs2_bnds, regs->cs[i].bnds); |
| 44 | out_be32(&ddr->cs2_config, regs->cs[i].config); |
| 45 | |
| 46 | } else if (i == 3) { |
| 47 | out_be32(&ddr->cs3_bnds, regs->cs[i].bnds); |
| 48 | out_be32(&ddr->cs3_config, regs->cs[i].config); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | out_be32(&ddr->timing_cfg_3, regs->timing_cfg_3); |
| 53 | out_be32(&ddr->timing_cfg_0, regs->timing_cfg_0); |
| 54 | out_be32(&ddr->timing_cfg_1, regs->timing_cfg_1); |
| 55 | out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2); |
| 56 | out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2); |
Peter Tyser | af5829cb | 2009-07-17 10:14:45 -0500 | [diff] [blame] | 57 | out_be32(&ddr->sdram_mode, regs->ddr_sdram_mode); |
Kumar Gala | 2e6ba7d | 2008-08-26 15:01:34 -0500 | [diff] [blame] | 58 | out_be32(&ddr->sdram_mode_2, regs->ddr_sdram_mode_2); |
York Sun | a21803d | 2013-11-18 10:29:32 -0800 | [diff] [blame] | 59 | out_be32(&ddr->sdram_md_cntl, regs->ddr_sdram_md_cntl); |
Kumar Gala | 2e6ba7d | 2008-08-26 15:01:34 -0500 | [diff] [blame] | 60 | out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval); |
| 61 | out_be32(&ddr->sdram_data_init, regs->ddr_data_init); |
| 62 | out_be32(&ddr->sdram_clk_cntl, regs->ddr_sdram_clk_cntl); |
| 63 | out_be32(&ddr->init_addr, regs->ddr_init_addr); |
| 64 | out_be32(&ddr->init_ext_addr, regs->ddr_init_ext_addr); |
| 65 | |
| 66 | debug("before go\n"); |
| 67 | |
| 68 | /* |
| 69 | * 200 painful micro-seconds must elapse between |
| 70 | * the DDR clock setup and the DDR config enable. |
| 71 | */ |
| 72 | udelay(200); |
| 73 | asm volatile("sync;isync"); |
| 74 | |
Peter Tyser | af5829cb | 2009-07-17 10:14:45 -0500 | [diff] [blame] | 75 | out_be32(&ddr->sdram_cfg, regs->ddr_sdram_cfg); |
Kumar Gala | 2e6ba7d | 2008-08-26 15:01:34 -0500 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * Poll DDR_SDRAM_CFG_2[D_INIT] bit until auto-data init is done |
| 79 | */ |
| 80 | while (in_be32(&ddr->sdram_cfg_2) & 0x10) { |
| 81 | udelay(10000); /* throttle polling rate */ |
| 82 | } |
| 83 | } |