Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) Freescale Semiconductor, Inc. 2006-2007 |
| 4 | * |
| 5 | * Authors: Nick.Spence@freescale.com |
| 6 | * Wilson.Lo@freescale.com |
| 7 | * scottwood@freescale.com |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 11 | #include <init.h> |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 12 | #include <mpc83xx.h> |
| 13 | #include <spd_sdram.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame^] | 14 | #include <asm/global_data.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 15 | #include <linux/delay.h> |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 16 | |
| 17 | #include <asm/bitops.h> |
| 18 | #include <asm/io.h> |
| 19 | |
| 20 | #include <asm/processor.h> |
| 21 | |
Wolfgang Denk | d112a2c | 2007-09-15 20:48:41 +0200 | [diff] [blame] | 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 24 | #ifndef CONFIG_SYS_8313ERDB_BROKEN_PMC |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 25 | static void resume_from_sleep(void) |
| 26 | { |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 27 | u32 magic = *(u32 *)0; |
| 28 | |
| 29 | typedef void (*func_t)(void); |
| 30 | func_t resume = *(func_t *)4; |
| 31 | |
| 32 | if (magic == 0xf5153ae5) |
| 33 | resume(); |
| 34 | |
| 35 | gd->flags &= ~GD_FLG_SILENT; |
| 36 | puts("\nResume from sleep failed: bad magic word\n"); |
| 37 | } |
| 38 | #endif |
| 39 | |
| 40 | /* Fixed sdram init -- doesn't use serial presence detect. |
| 41 | * |
| 42 | * This is useful for faster booting in configs where the RAM is unlikely |
| 43 | * to be changed, or for things like NAND booting where space is tight. |
| 44 | */ |
| 45 | static long fixed_sdram(void) |
| 46 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 47 | u32 msize = CONFIG_SYS_DDR_SIZE * 1024 * 1024; |
Scott Wood | b71689b | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 48 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 49 | #ifndef CONFIG_SYS_RAMBOOT |
| 50 | volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR; |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 51 | u32 msize_log2 = __ilog2(msize); |
| 52 | |
Mario Six | 805cac1 | 2019-01-21 09:18:16 +0100 | [diff] [blame] | 53 | im->sysconf.ddrlaw[0].bar = CONFIG_SYS_SDRAM_BASE & 0xfffff000; |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 54 | im->sysconf.ddrlaw[0].ar = LBLAWAR_EN | (msize_log2 - 1); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 55 | im->sysconf.ddrcdr = CONFIG_SYS_DDRCDR_VALUE; |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Erratum DDR3 requires a 50ms delay after clearing DDRCDR[DDR_cfg], |
| 59 | * or the DDR2 controller may fail to initialize correctly. |
| 60 | */ |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 61 | __udelay(50000); |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 62 | |
Mario Six | 805cac1 | 2019-01-21 09:18:16 +0100 | [diff] [blame] | 63 | #if ((CONFIG_SYS_SDRAM_BASE & 0x00FFFFFF) != 0) |
Joe Hershberger | 5ade390 | 2011-10-11 23:57:31 -0500 | [diff] [blame] | 64 | #warning Chip select bounds is only configurable in 16MB increments |
| 65 | #endif |
| 66 | im->ddr.csbnds[0].csbnds = |
Mario Six | 805cac1 | 2019-01-21 09:18:16 +0100 | [diff] [blame] | 67 | ((CONFIG_SYS_SDRAM_BASE >> CSBNDS_SA_SHIFT) & CSBNDS_SA) | |
| 68 | (((CONFIG_SYS_SDRAM_BASE + msize - 1) >> CSBNDS_EA_SHIFT) & |
Joe Hershberger | 5ade390 | 2011-10-11 23:57:31 -0500 | [diff] [blame] | 69 | CSBNDS_EA); |
| 70 | im->ddr.cs_config[0] = CONFIG_SYS_DDR_CS0_CONFIG; |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 71 | |
| 72 | /* Currently we use only one CS, so disable the other bank. */ |
| 73 | im->ddr.cs_config[1] = 0; |
| 74 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 75 | im->ddr.sdram_clk_cntl = CONFIG_SYS_DDR_CLK_CNTL; |
| 76 | im->ddr.timing_cfg_3 = CONFIG_SYS_DDR_TIMING_3; |
| 77 | im->ddr.timing_cfg_1 = CONFIG_SYS_DDR_TIMING_1; |
| 78 | im->ddr.timing_cfg_2 = CONFIG_SYS_DDR_TIMING_2; |
| 79 | im->ddr.timing_cfg_0 = CONFIG_SYS_DDR_TIMING_0; |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 80 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 81 | #ifndef CONFIG_SYS_8313ERDB_BROKEN_PMC |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 82 | if (im->pmc.pmccr1 & PMCCR1_POWER_OFF) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 83 | im->ddr.sdram_cfg = CONFIG_SYS_SDRAM_CFG | SDRAM_CFG_BI; |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 84 | else |
| 85 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 86 | im->ddr.sdram_cfg = CONFIG_SYS_SDRAM_CFG; |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 87 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 88 | im->ddr.sdram_cfg2 = CONFIG_SYS_SDRAM_CFG2; |
| 89 | im->ddr.sdram_mode = CONFIG_SYS_DDR_MODE; |
| 90 | im->ddr.sdram_mode2 = CONFIG_SYS_DDR_MODE_2; |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 91 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 92 | im->ddr.sdram_interval = CONFIG_SYS_DDR_INTERVAL; |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 93 | sync(); |
| 94 | |
| 95 | /* enable DDR controller */ |
| 96 | im->ddr.sdram_cfg |= SDRAM_CFG_MEM_EN; |
Scott Wood | b71689b | 2008-06-30 14:13:28 -0500 | [diff] [blame] | 97 | #endif |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 98 | |
| 99 | return msize; |
| 100 | } |
| 101 | |
Simon Glass | d35f338 | 2017-04-06 12:47:05 -0600 | [diff] [blame] | 102 | int dram_init(void) |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 103 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 104 | volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR; |
Becky Bruce | 0d4cee1 | 2010-06-17 11:37:20 -0500 | [diff] [blame] | 105 | volatile fsl_lbc_t *lbc = &im->im_lbc; |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 106 | u32 msize; |
| 107 | |
| 108 | if ((im->sysconf.immrbar & IMMRBAR_BASE_ADDR) != (u32)im) |
Simon Glass | 39f90ba | 2017-03-31 08:40:25 -0600 | [diff] [blame] | 109 | return -ENXIO; |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 110 | |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 111 | /* DDR SDRAM - Main SODIMM */ |
| 112 | msize = fixed_sdram(); |
| 113 | |
| 114 | /* Local Bus setup lbcr and mrtpr */ |
Mario Six | dc00300 | 2019-01-21 09:18:17 +0100 | [diff] [blame] | 115 | lbc->lbcr = (0x00040000 | (0xFF << LBCR_BMT_SHIFT) | 0xF); |
| 116 | /* LB refresh timer prescal, 266MHz/32 */ |
| 117 | lbc->mrtpr = 0x20000000; |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 118 | sync(); |
| 119 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 120 | #ifndef CONFIG_SYS_8313ERDB_BROKEN_PMC |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 121 | if (im->pmc.pmccr1 & PMCCR1_POWER_OFF) |
| 122 | resume_from_sleep(); |
| 123 | #endif |
| 124 | |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 125 | /* return total bus SDRAM size(bytes) -- DDR */ |
Simon Glass | 39f90ba | 2017-03-31 08:40:25 -0600 | [diff] [blame] | 126 | gd->ram_size = msize; |
| 127 | |
| 128 | return 0; |
Scott Wood | 865b8ae | 2007-04-16 14:54:15 -0500 | [diff] [blame] | 129 | } |