common: Drop net.h from common header
[oweals/u-boot.git] / arch / x86 / cpu / quark / dram.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
4  */
5
6 #include <common.h>
7 #include <cpu_func.h>
8 #include <errno.h>
9 #include <fdtdec.h>
10 #include <init.h>
11 #include <malloc.h>
12 #include <asm/cache.h>
13 #include <asm/mrccache.h>
14 #include <asm/mtrr.h>
15 #include <asm/post.h>
16 #include <asm/arch/mrc.h>
17 #include <asm/arch/msg_port.h>
18 #include <asm/arch/quark.h>
19
20 DECLARE_GLOBAL_DATA_PTR;
21
22 static __maybe_unused int prepare_mrc_cache(struct mrc_params *mrc_params)
23 {
24         struct mrc_data_container *cache;
25         struct mrc_region entry;
26         int ret;
27
28         ret = mrccache_get_region(MRC_TYPE_NORMAL, NULL, &entry);
29         if (ret)
30                 return ret;
31
32         cache = mrccache_find_current(&entry);
33         if (!cache)
34                 return -ENOENT;
35
36         debug("%s: mrc cache at %p, size %x checksum %04x\n", __func__,
37               cache->data, cache->data_size, cache->checksum);
38
39         /* copy mrc cache to the mrc_params */
40         memcpy(&mrc_params->timings, cache->data, cache->data_size);
41
42         return 0;
43 }
44
45 static int mrc_configure_params(struct mrc_params *mrc_params)
46 {
47         const void *blob = gd->fdt_blob;
48         int node;
49         int mrc_flags;
50
51         node = fdtdec_next_compatible(blob, 0, COMPAT_INTEL_QRK_MRC);
52         if (node < 0) {
53                 debug("%s: Cannot find MRC node\n", __func__);
54                 return -EINVAL;
55         }
56
57 #ifdef CONFIG_ENABLE_MRC_CACHE
58         mrc_params->boot_mode = prepare_mrc_cache(mrc_params);
59         if (mrc_params->boot_mode)
60                 mrc_params->boot_mode = BM_COLD;
61         else
62                 mrc_params->boot_mode = BM_FAST;
63 #else
64         mrc_params->boot_mode = BM_COLD;
65 #endif
66
67         /*
68          * TODO:
69          *
70          * We need determine ECC by pin strap state
71          *
72          * Disable ECC by default for now
73          */
74         mrc_params->ecc_enables = 0;
75
76         mrc_flags = fdtdec_get_int(blob, node, "flags", 0);
77         if (mrc_flags & MRC_FLAG_SCRAMBLE_EN)
78                 mrc_params->scrambling_enables = 1;
79         else
80                 mrc_params->scrambling_enables = 0;
81
82         mrc_params->dram_width = fdtdec_get_int(blob, node, "dram-width", 0);
83         mrc_params->ddr_speed = fdtdec_get_int(blob, node, "dram-speed", 0);
84         mrc_params->ddr_type = fdtdec_get_int(blob, node, "dram-type", 0);
85
86         mrc_params->rank_enables = fdtdec_get_int(blob, node, "rank-mask", 0);
87         mrc_params->channel_enables = fdtdec_get_int(blob, node,
88                 "chan-mask", 0);
89         mrc_params->channel_width = fdtdec_get_int(blob, node,
90                 "chan-width", 0);
91         mrc_params->address_mode = fdtdec_get_int(blob, node, "addr-mode", 0);
92
93         mrc_params->refresh_rate = fdtdec_get_int(blob, node,
94                 "refresh-rate", 0);
95         mrc_params->sr_temp_range = fdtdec_get_int(blob, node,
96                 "sr-temp-range", 0);
97         mrc_params->ron_value = fdtdec_get_int(blob, node,
98                 "ron-value", 0);
99         mrc_params->rtt_nom_value = fdtdec_get_int(blob, node,
100                 "rtt-nom-value", 0);
101         mrc_params->rd_odt_value = fdtdec_get_int(blob, node,
102                 "rd-odt-value", 0);
103
104         mrc_params->params.density = fdtdec_get_int(blob, node,
105                 "dram-density", 0);
106         mrc_params->params.cl = fdtdec_get_int(blob, node, "dram-cl", 0);
107         mrc_params->params.ras = fdtdec_get_int(blob, node, "dram-ras", 0);
108         mrc_params->params.wtr = fdtdec_get_int(blob, node, "dram-wtr", 0);
109         mrc_params->params.rrd = fdtdec_get_int(blob, node, "dram-rrd", 0);
110         mrc_params->params.faw = fdtdec_get_int(blob, node, "dram-faw", 0);
111
112         debug("MRC dram_width %d\n", mrc_params->dram_width);
113         debug("MRC rank_enables %d\n", mrc_params->rank_enables);
114         debug("MRC ddr_speed %d\n", mrc_params->ddr_speed);
115         debug("MRC flags: %s\n",
116               (mrc_params->scrambling_enables) ? "SCRAMBLE_EN" : "");
117
118         debug("MRC density=%d tCL=%d tRAS=%d tWTR=%d tRRD=%d tFAW=%d\n",
119               mrc_params->params.density, mrc_params->params.cl,
120               mrc_params->params.ras, mrc_params->params.wtr,
121               mrc_params->params.rrd, mrc_params->params.faw);
122
123         return 0;
124 }
125
126 int dram_init(void)
127 {
128         struct mrc_params mrc_params;
129 #ifdef CONFIG_ENABLE_MRC_CACHE
130         char *cache;
131 #endif
132         int ret;
133
134         memset(&mrc_params, 0, sizeof(struct mrc_params));
135         ret = mrc_configure_params(&mrc_params);
136         if (ret)
137                 return ret;
138
139         /* Set up the DRAM by calling the memory reference code */
140         mrc_init(&mrc_params);
141         if (mrc_params.status)
142                 return -EIO;
143
144         gd->ram_size = mrc_params.mem_size;
145         post_code(POST_DRAM);
146
147         /* variable range MTRR#2: RAM area */
148         disable_caches();
149         msg_port_write(MSG_PORT_HOST_BRIDGE, MTRR_VAR_PHYBASE(MTRR_VAR_RAM),
150                        0 | MTRR_TYPE_WRBACK);
151         msg_port_write(MSG_PORT_HOST_BRIDGE, MTRR_VAR_PHYMASK(MTRR_VAR_RAM),
152                        (~(gd->ram_size - 1)) | MTRR_PHYS_MASK_VALID);
153         enable_caches();
154
155 #ifdef CONFIG_ENABLE_MRC_CACHE
156         cache = malloc(sizeof(struct mrc_timings));
157         if (cache) {
158                 struct mrc_output *mrc = &gd->arch.mrc[MRC_TYPE_NORMAL];
159
160                 memcpy(cache, &mrc_params.timings, sizeof(struct mrc_timings));
161                 mrc->buf = cache;
162                 mrc->len = sizeof(struct mrc_timings);
163         }
164 #endif
165
166         return 0;
167 }
168
169 int dram_init_banksize(void)
170 {
171         gd->bd->bi_dram[0].start = 0;
172         gd->bd->bi_dram[0].size = gd->ram_size;
173
174         return 0;
175 }
176
177 /*
178  * This function looks for the highest region of memory lower than 4GB which
179  * has enough space for U-Boot where U-Boot is aligned on a page boundary.
180  * It overrides the default implementation found elsewhere which simply
181  * picks the end of ram, wherever that may be. The location of the stack,
182  * the relocation address, and how far U-Boot is moved by relocation are
183  * set in the global data structure.
184  */
185 ulong board_get_usable_ram_top(ulong total_size)
186 {
187         return gd->ram_size;
188 }